inet_diag.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. /*
  2. * inet_diag.c Module for monitoring INET transport protocols sockets.
  3. *
  4. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/fcntl.h>
  15. #include <linux/random.h>
  16. #include <linux/slab.h>
  17. #include <linux/cache.h>
  18. #include <linux/init.h>
  19. #include <linux/time.h>
  20. #include <net/icmp.h>
  21. #include <net/tcp.h>
  22. #include <net/ipv6.h>
  23. #include <net/inet_common.h>
  24. #include <net/inet_connection_sock.h>
  25. #include <net/inet_hashtables.h>
  26. #include <net/inet_timewait_sock.h>
  27. #include <net/inet6_hashtables.h>
  28. #include <net/netlink.h>
  29. #include <linux/inet.h>
  30. #include <linux/stddef.h>
  31. #include <linux/inet_diag.h>
  32. #include <linux/sock_diag.h>
  33. static const struct inet_diag_handler **inet_diag_table;
  34. struct inet_diag_entry {
  35. const __be32 *saddr;
  36. const __be32 *daddr;
  37. u16 sport;
  38. u16 dport;
  39. u16 family;
  40. u16 userlocks;
  41. u32 ifindex;
  42. u32 mark;
  43. };
  44. static DEFINE_MUTEX(inet_diag_table_mutex);
  45. static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
  46. {
  47. if (!inet_diag_table[proto])
  48. sock_load_diag_module(AF_INET, proto);
  49. mutex_lock(&inet_diag_table_mutex);
  50. if (!inet_diag_table[proto])
  51. return ERR_PTR(-ENOENT);
  52. return inet_diag_table[proto];
  53. }
  54. static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
  55. {
  56. mutex_unlock(&inet_diag_table_mutex);
  57. }
  58. void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
  59. {
  60. r->idiag_family = sk->sk_family;
  61. r->id.idiag_sport = htons(sk->sk_num);
  62. r->id.idiag_dport = sk->sk_dport;
  63. r->id.idiag_if = sk->sk_bound_dev_if;
  64. sock_diag_save_cookie(sk, r->id.idiag_cookie);
  65. #if IS_ENABLED(CONFIG_IPV6)
  66. if (sk->sk_family == AF_INET6) {
  67. *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
  68. *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
  69. } else
  70. #endif
  71. {
  72. memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
  73. memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
  74. r->id.idiag_src[0] = sk->sk_rcv_saddr;
  75. r->id.idiag_dst[0] = sk->sk_daddr;
  76. }
  77. }
  78. EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill);
  79. static size_t inet_sk_attr_size(struct sock *sk,
  80. const struct inet_diag_req_v2 *req,
  81. bool net_admin)
  82. {
  83. const struct inet_diag_handler *handler;
  84. size_t aux = 0;
  85. handler = inet_diag_table[req->sdiag_protocol];
  86. if (handler && handler->idiag_get_aux_size)
  87. aux = handler->idiag_get_aux_size(sk, net_admin);
  88. return nla_total_size(sizeof(struct tcp_info))
  89. + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
  90. + nla_total_size(1) /* INET_DIAG_TOS */
  91. + nla_total_size(1) /* INET_DIAG_TCLASS */
  92. + nla_total_size(4) /* INET_DIAG_MARK */
  93. + nla_total_size(sizeof(struct inet_diag_meminfo))
  94. + nla_total_size(sizeof(struct inet_diag_msg))
  95. + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
  96. + nla_total_size(TCP_CA_NAME_MAX)
  97. + nla_total_size(sizeof(struct tcpvegas_info))
  98. + aux
  99. + 64;
  100. }
  101. int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
  102. struct inet_diag_msg *r, int ext,
  103. struct user_namespace *user_ns,
  104. bool net_admin)
  105. {
  106. const struct inet_sock *inet = inet_sk(sk);
  107. if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
  108. goto errout;
  109. /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
  110. * hence this needs to be included regardless of socket family.
  111. */
  112. if (ext & (1 << (INET_DIAG_TOS - 1)))
  113. if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
  114. goto errout;
  115. #if IS_ENABLED(CONFIG_IPV6)
  116. if (r->idiag_family == AF_INET6) {
  117. if (ext & (1 << (INET_DIAG_TCLASS - 1)))
  118. if (nla_put_u8(skb, INET_DIAG_TCLASS,
  119. inet6_sk(sk)->tclass) < 0)
  120. goto errout;
  121. if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
  122. nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk)))
  123. goto errout;
  124. }
  125. #endif
  126. if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, sk->sk_mark))
  127. goto errout;
  128. r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
  129. r->idiag_inode = sock_i_ino(sk);
  130. return 0;
  131. errout:
  132. return 1;
  133. }
  134. EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill);
  135. int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
  136. struct sk_buff *skb, const struct inet_diag_req_v2 *req,
  137. struct user_namespace *user_ns,
  138. u32 portid, u32 seq, u16 nlmsg_flags,
  139. const struct nlmsghdr *unlh,
  140. bool net_admin)
  141. {
  142. const struct tcp_congestion_ops *ca_ops;
  143. const struct inet_diag_handler *handler;
  144. int ext = req->idiag_ext;
  145. struct inet_diag_msg *r;
  146. struct nlmsghdr *nlh;
  147. struct nlattr *attr;
  148. void *info = NULL;
  149. handler = inet_diag_table[req->sdiag_protocol];
  150. BUG_ON(!handler);
  151. nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
  152. nlmsg_flags);
  153. if (!nlh)
  154. return -EMSGSIZE;
  155. r = nlmsg_data(nlh);
  156. BUG_ON(!sk_fullsock(sk));
  157. inet_diag_msg_common_fill(r, sk);
  158. r->idiag_state = sk->sk_state;
  159. r->idiag_timer = 0;
  160. r->idiag_retrans = 0;
  161. if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin))
  162. goto errout;
  163. if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
  164. struct inet_diag_meminfo minfo = {
  165. .idiag_rmem = sk_rmem_alloc_get(sk),
  166. .idiag_wmem = sk->sk_wmem_queued,
  167. .idiag_fmem = sk->sk_forward_alloc,
  168. .idiag_tmem = sk_wmem_alloc_get(sk),
  169. };
  170. if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
  171. goto errout;
  172. }
  173. if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
  174. if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
  175. goto errout;
  176. /*
  177. * RAW sockets might have user-defined protocols assigned,
  178. * so report the one supplied on socket creation.
  179. */
  180. if (sk->sk_type == SOCK_RAW) {
  181. if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))
  182. goto errout;
  183. }
  184. if (!icsk) {
  185. handler->idiag_get_info(sk, r, NULL);
  186. goto out;
  187. }
  188. if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
  189. icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
  190. icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
  191. r->idiag_timer = 1;
  192. r->idiag_retrans = icsk->icsk_retransmits;
  193. r->idiag_expires =
  194. jiffies_to_msecs(icsk->icsk_timeout - jiffies);
  195. } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
  196. r->idiag_timer = 4;
  197. r->idiag_retrans = icsk->icsk_probes_out;
  198. r->idiag_expires =
  199. jiffies_to_msecs(icsk->icsk_timeout - jiffies);
  200. } else if (timer_pending(&sk->sk_timer)) {
  201. r->idiag_timer = 2;
  202. r->idiag_retrans = icsk->icsk_probes_out;
  203. r->idiag_expires =
  204. jiffies_to_msecs(sk->sk_timer.expires - jiffies);
  205. } else {
  206. r->idiag_timer = 0;
  207. r->idiag_expires = 0;
  208. }
  209. if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) {
  210. attr = nla_reserve_64bit(skb, INET_DIAG_INFO,
  211. handler->idiag_info_size,
  212. INET_DIAG_PAD);
  213. if (!attr)
  214. goto errout;
  215. info = nla_data(attr);
  216. }
  217. if (ext & (1 << (INET_DIAG_CONG - 1))) {
  218. int err = 0;
  219. rcu_read_lock();
  220. ca_ops = READ_ONCE(icsk->icsk_ca_ops);
  221. if (ca_ops)
  222. err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
  223. rcu_read_unlock();
  224. if (err < 0)
  225. goto errout;
  226. }
  227. handler->idiag_get_info(sk, r, info);
  228. if (ext & (1 << (INET_DIAG_INFO - 1)) && handler->idiag_get_aux)
  229. if (handler->idiag_get_aux(sk, net_admin, skb) < 0)
  230. goto errout;
  231. if (sk->sk_state < TCP_TIME_WAIT) {
  232. union tcp_cc_info info;
  233. size_t sz = 0;
  234. int attr;
  235. rcu_read_lock();
  236. ca_ops = READ_ONCE(icsk->icsk_ca_ops);
  237. if (ca_ops && ca_ops->get_info)
  238. sz = ca_ops->get_info(sk, ext, &attr, &info);
  239. rcu_read_unlock();
  240. if (sz && nla_put(skb, attr, sz, &info) < 0)
  241. goto errout;
  242. }
  243. if (ext & (1 << (INET_DIAG_CLASS_ID - 1))) {
  244. u32 classid = 0;
  245. #ifdef CONFIG_SOCK_CGROUP_DATA
  246. classid = sock_cgroup_classid(&sk->sk_cgrp_data);
  247. #endif
  248. if (nla_put_u32(skb, INET_DIAG_CLASS_ID, classid))
  249. goto errout;
  250. }
  251. out:
  252. nlmsg_end(skb, nlh);
  253. return 0;
  254. errout:
  255. nlmsg_cancel(skb, nlh);
  256. return -EMSGSIZE;
  257. }
  258. EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
  259. static int inet_csk_diag_fill(struct sock *sk,
  260. struct sk_buff *skb,
  261. const struct inet_diag_req_v2 *req,
  262. struct user_namespace *user_ns,
  263. u32 portid, u32 seq, u16 nlmsg_flags,
  264. const struct nlmsghdr *unlh,
  265. bool net_admin)
  266. {
  267. return inet_sk_diag_fill(sk, inet_csk(sk), skb, req, user_ns,
  268. portid, seq, nlmsg_flags, unlh, net_admin);
  269. }
  270. static int inet_twsk_diag_fill(struct sock *sk,
  271. struct sk_buff *skb,
  272. u32 portid, u32 seq, u16 nlmsg_flags,
  273. const struct nlmsghdr *unlh)
  274. {
  275. struct inet_timewait_sock *tw = inet_twsk(sk);
  276. struct inet_diag_msg *r;
  277. struct nlmsghdr *nlh;
  278. long tmo;
  279. nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
  280. nlmsg_flags);
  281. if (!nlh)
  282. return -EMSGSIZE;
  283. r = nlmsg_data(nlh);
  284. BUG_ON(tw->tw_state != TCP_TIME_WAIT);
  285. tmo = tw->tw_timer.expires - jiffies;
  286. if (tmo < 0)
  287. tmo = 0;
  288. inet_diag_msg_common_fill(r, sk);
  289. r->idiag_retrans = 0;
  290. r->idiag_state = tw->tw_substate;
  291. r->idiag_timer = 3;
  292. r->idiag_expires = jiffies_to_msecs(tmo);
  293. r->idiag_rqueue = 0;
  294. r->idiag_wqueue = 0;
  295. r->idiag_uid = 0;
  296. r->idiag_inode = 0;
  297. nlmsg_end(skb, nlh);
  298. return 0;
  299. }
  300. static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
  301. u32 portid, u32 seq, u16 nlmsg_flags,
  302. const struct nlmsghdr *unlh, bool net_admin)
  303. {
  304. struct request_sock *reqsk = inet_reqsk(sk);
  305. struct inet_diag_msg *r;
  306. struct nlmsghdr *nlh;
  307. long tmo;
  308. nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
  309. nlmsg_flags);
  310. if (!nlh)
  311. return -EMSGSIZE;
  312. r = nlmsg_data(nlh);
  313. inet_diag_msg_common_fill(r, sk);
  314. r->idiag_state = TCP_SYN_RECV;
  315. r->idiag_timer = 1;
  316. r->idiag_retrans = reqsk->num_retrans;
  317. BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
  318. offsetof(struct sock, sk_cookie));
  319. tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
  320. r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0;
  321. r->idiag_rqueue = 0;
  322. r->idiag_wqueue = 0;
  323. r->idiag_uid = 0;
  324. r->idiag_inode = 0;
  325. if (net_admin && nla_put_u32(skb, INET_DIAG_MARK,
  326. inet_rsk(reqsk)->ir_mark))
  327. return -EMSGSIZE;
  328. nlmsg_end(skb, nlh);
  329. return 0;
  330. }
  331. static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
  332. const struct inet_diag_req_v2 *r,
  333. struct user_namespace *user_ns,
  334. u32 portid, u32 seq, u16 nlmsg_flags,
  335. const struct nlmsghdr *unlh, bool net_admin)
  336. {
  337. if (sk->sk_state == TCP_TIME_WAIT)
  338. return inet_twsk_diag_fill(sk, skb, portid, seq,
  339. nlmsg_flags, unlh);
  340. if (sk->sk_state == TCP_NEW_SYN_RECV)
  341. return inet_req_diag_fill(sk, skb, portid, seq,
  342. nlmsg_flags, unlh, net_admin);
  343. return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
  344. nlmsg_flags, unlh, net_admin);
  345. }
  346. struct sock *inet_diag_find_one_icsk(struct net *net,
  347. struct inet_hashinfo *hashinfo,
  348. const struct inet_diag_req_v2 *req)
  349. {
  350. struct sock *sk;
  351. rcu_read_lock();
  352. if (req->sdiag_family == AF_INET)
  353. sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0],
  354. req->id.idiag_dport, req->id.idiag_src[0],
  355. req->id.idiag_sport, req->id.idiag_if);
  356. #if IS_ENABLED(CONFIG_IPV6)
  357. else if (req->sdiag_family == AF_INET6) {
  358. if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
  359. ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
  360. sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3],
  361. req->id.idiag_dport, req->id.idiag_src[3],
  362. req->id.idiag_sport, req->id.idiag_if);
  363. else
  364. sk = inet6_lookup(net, hashinfo, NULL, 0,
  365. (struct in6_addr *)req->id.idiag_dst,
  366. req->id.idiag_dport,
  367. (struct in6_addr *)req->id.idiag_src,
  368. req->id.idiag_sport,
  369. req->id.idiag_if);
  370. }
  371. #endif
  372. else {
  373. rcu_read_unlock();
  374. return ERR_PTR(-EINVAL);
  375. }
  376. rcu_read_unlock();
  377. if (!sk)
  378. return ERR_PTR(-ENOENT);
  379. if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
  380. sock_gen_put(sk);
  381. return ERR_PTR(-ENOENT);
  382. }
  383. return sk;
  384. }
  385. EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
  386. int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
  387. struct sk_buff *in_skb,
  388. const struct nlmsghdr *nlh,
  389. const struct inet_diag_req_v2 *req)
  390. {
  391. bool net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN);
  392. struct net *net = sock_net(in_skb->sk);
  393. struct sk_buff *rep;
  394. struct sock *sk;
  395. int err;
  396. sk = inet_diag_find_one_icsk(net, hashinfo, req);
  397. if (IS_ERR(sk))
  398. return PTR_ERR(sk);
  399. rep = nlmsg_new(inet_sk_attr_size(sk, req, net_admin), GFP_KERNEL);
  400. if (!rep) {
  401. err = -ENOMEM;
  402. goto out;
  403. }
  404. err = sk_diag_fill(sk, rep, req,
  405. sk_user_ns(NETLINK_CB(in_skb).sk),
  406. NETLINK_CB(in_skb).portid,
  407. nlh->nlmsg_seq, 0, nlh, net_admin);
  408. if (err < 0) {
  409. WARN_ON(err == -EMSGSIZE);
  410. nlmsg_free(rep);
  411. goto out;
  412. }
  413. err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
  414. MSG_DONTWAIT);
  415. if (err > 0)
  416. err = 0;
  417. out:
  418. if (sk)
  419. sock_gen_put(sk);
  420. return err;
  421. }
  422. EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
  423. static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
  424. const struct nlmsghdr *nlh,
  425. const struct inet_diag_req_v2 *req)
  426. {
  427. const struct inet_diag_handler *handler;
  428. int err;
  429. handler = inet_diag_lock_handler(req->sdiag_protocol);
  430. if (IS_ERR(handler))
  431. err = PTR_ERR(handler);
  432. else if (cmd == SOCK_DIAG_BY_FAMILY)
  433. err = handler->dump_one(in_skb, nlh, req);
  434. else if (cmd == SOCK_DESTROY && handler->destroy)
  435. err = handler->destroy(in_skb, req);
  436. else
  437. err = -EOPNOTSUPP;
  438. inet_diag_unlock_handler(handler);
  439. return err;
  440. }
  441. static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
  442. {
  443. int words = bits >> 5;
  444. bits &= 0x1f;
  445. if (words) {
  446. if (memcmp(a1, a2, words << 2))
  447. return 0;
  448. }
  449. if (bits) {
  450. __be32 w1, w2;
  451. __be32 mask;
  452. w1 = a1[words];
  453. w2 = a2[words];
  454. mask = htonl((0xffffffff) << (32 - bits));
  455. if ((w1 ^ w2) & mask)
  456. return 0;
  457. }
  458. return 1;
  459. }
  460. static int inet_diag_bc_run(const struct nlattr *_bc,
  461. const struct inet_diag_entry *entry)
  462. {
  463. const void *bc = nla_data(_bc);
  464. int len = nla_len(_bc);
  465. while (len > 0) {
  466. int yes = 1;
  467. const struct inet_diag_bc_op *op = bc;
  468. switch (op->code) {
  469. case INET_DIAG_BC_NOP:
  470. break;
  471. case INET_DIAG_BC_JMP:
  472. yes = 0;
  473. break;
  474. case INET_DIAG_BC_S_EQ:
  475. yes = entry->sport == op[1].no;
  476. break;
  477. case INET_DIAG_BC_S_GE:
  478. yes = entry->sport >= op[1].no;
  479. break;
  480. case INET_DIAG_BC_S_LE:
  481. yes = entry->sport <= op[1].no;
  482. break;
  483. case INET_DIAG_BC_D_EQ:
  484. yes = entry->dport == op[1].no;
  485. break;
  486. case INET_DIAG_BC_D_GE:
  487. yes = entry->dport >= op[1].no;
  488. break;
  489. case INET_DIAG_BC_D_LE:
  490. yes = entry->dport <= op[1].no;
  491. break;
  492. case INET_DIAG_BC_AUTO:
  493. yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
  494. break;
  495. case INET_DIAG_BC_S_COND:
  496. case INET_DIAG_BC_D_COND: {
  497. const struct inet_diag_hostcond *cond;
  498. const __be32 *addr;
  499. cond = (const struct inet_diag_hostcond *)(op + 1);
  500. if (cond->port != -1 &&
  501. cond->port != (op->code == INET_DIAG_BC_S_COND ?
  502. entry->sport : entry->dport)) {
  503. yes = 0;
  504. break;
  505. }
  506. if (op->code == INET_DIAG_BC_S_COND)
  507. addr = entry->saddr;
  508. else
  509. addr = entry->daddr;
  510. if (cond->family != AF_UNSPEC &&
  511. cond->family != entry->family) {
  512. if (entry->family == AF_INET6 &&
  513. cond->family == AF_INET) {
  514. if (addr[0] == 0 && addr[1] == 0 &&
  515. addr[2] == htonl(0xffff) &&
  516. bitstring_match(addr + 3,
  517. cond->addr,
  518. cond->prefix_len))
  519. break;
  520. }
  521. yes = 0;
  522. break;
  523. }
  524. if (cond->prefix_len == 0)
  525. break;
  526. if (bitstring_match(addr, cond->addr,
  527. cond->prefix_len))
  528. break;
  529. yes = 0;
  530. break;
  531. }
  532. case INET_DIAG_BC_DEV_COND: {
  533. u32 ifindex;
  534. ifindex = *((const u32 *)(op + 1));
  535. if (ifindex != entry->ifindex)
  536. yes = 0;
  537. break;
  538. }
  539. case INET_DIAG_BC_MARK_COND: {
  540. struct inet_diag_markcond *cond;
  541. cond = (struct inet_diag_markcond *)(op + 1);
  542. if ((entry->mark & cond->mask) != cond->mark)
  543. yes = 0;
  544. break;
  545. }
  546. }
  547. if (yes) {
  548. len -= op->yes;
  549. bc += op->yes;
  550. } else {
  551. len -= op->no;
  552. bc += op->no;
  553. }
  554. }
  555. return len == 0;
  556. }
  557. /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
  558. */
  559. static void entry_fill_addrs(struct inet_diag_entry *entry,
  560. const struct sock *sk)
  561. {
  562. #if IS_ENABLED(CONFIG_IPV6)
  563. if (sk->sk_family == AF_INET6) {
  564. entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
  565. entry->daddr = sk->sk_v6_daddr.s6_addr32;
  566. } else
  567. #endif
  568. {
  569. entry->saddr = &sk->sk_rcv_saddr;
  570. entry->daddr = &sk->sk_daddr;
  571. }
  572. }
  573. int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
  574. {
  575. struct inet_sock *inet = inet_sk(sk);
  576. struct inet_diag_entry entry;
  577. if (!bc)
  578. return 1;
  579. entry.family = sk->sk_family;
  580. entry_fill_addrs(&entry, sk);
  581. entry.sport = inet->inet_num;
  582. entry.dport = ntohs(inet->inet_dport);
  583. entry.ifindex = sk->sk_bound_dev_if;
  584. entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
  585. if (sk_fullsock(sk))
  586. entry.mark = sk->sk_mark;
  587. else if (sk->sk_state == TCP_NEW_SYN_RECV)
  588. entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark;
  589. else
  590. entry.mark = 0;
  591. return inet_diag_bc_run(bc, &entry);
  592. }
  593. EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
  594. static int valid_cc(const void *bc, int len, int cc)
  595. {
  596. while (len >= 0) {
  597. const struct inet_diag_bc_op *op = bc;
  598. if (cc > len)
  599. return 0;
  600. if (cc == len)
  601. return 1;
  602. if (op->yes < 4 || op->yes & 3)
  603. return 0;
  604. len -= op->yes;
  605. bc += op->yes;
  606. }
  607. return 0;
  608. }
  609. /* data is u32 ifindex */
  610. static bool valid_devcond(const struct inet_diag_bc_op *op, int len,
  611. int *min_len)
  612. {
  613. /* Check ifindex space. */
  614. *min_len += sizeof(u32);
  615. if (len < *min_len)
  616. return false;
  617. return true;
  618. }
  619. /* Validate an inet_diag_hostcond. */
  620. static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
  621. int *min_len)
  622. {
  623. struct inet_diag_hostcond *cond;
  624. int addr_len;
  625. /* Check hostcond space. */
  626. *min_len += sizeof(struct inet_diag_hostcond);
  627. if (len < *min_len)
  628. return false;
  629. cond = (struct inet_diag_hostcond *)(op + 1);
  630. /* Check address family and address length. */
  631. switch (cond->family) {
  632. case AF_UNSPEC:
  633. addr_len = 0;
  634. break;
  635. case AF_INET:
  636. addr_len = sizeof(struct in_addr);
  637. break;
  638. case AF_INET6:
  639. addr_len = sizeof(struct in6_addr);
  640. break;
  641. default:
  642. return false;
  643. }
  644. *min_len += addr_len;
  645. if (len < *min_len)
  646. return false;
  647. /* Check prefix length (in bits) vs address length (in bytes). */
  648. if (cond->prefix_len > 8 * addr_len)
  649. return false;
  650. return true;
  651. }
  652. /* Validate a port comparison operator. */
  653. static bool valid_port_comparison(const struct inet_diag_bc_op *op,
  654. int len, int *min_len)
  655. {
  656. /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
  657. *min_len += sizeof(struct inet_diag_bc_op);
  658. if (len < *min_len)
  659. return false;
  660. return true;
  661. }
  662. static bool valid_markcond(const struct inet_diag_bc_op *op, int len,
  663. int *min_len)
  664. {
  665. *min_len += sizeof(struct inet_diag_markcond);
  666. return len >= *min_len;
  667. }
  668. static int inet_diag_bc_audit(const struct nlattr *attr,
  669. const struct sk_buff *skb)
  670. {
  671. bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN);
  672. const void *bytecode, *bc;
  673. int bytecode_len, len;
  674. if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op))
  675. return -EINVAL;
  676. bytecode = bc = nla_data(attr);
  677. len = bytecode_len = nla_len(attr);
  678. while (len > 0) {
  679. int min_len = sizeof(struct inet_diag_bc_op);
  680. const struct inet_diag_bc_op *op = bc;
  681. switch (op->code) {
  682. case INET_DIAG_BC_S_COND:
  683. case INET_DIAG_BC_D_COND:
  684. if (!valid_hostcond(bc, len, &min_len))
  685. return -EINVAL;
  686. break;
  687. case INET_DIAG_BC_DEV_COND:
  688. if (!valid_devcond(bc, len, &min_len))
  689. return -EINVAL;
  690. break;
  691. case INET_DIAG_BC_S_EQ:
  692. case INET_DIAG_BC_S_GE:
  693. case INET_DIAG_BC_S_LE:
  694. case INET_DIAG_BC_D_EQ:
  695. case INET_DIAG_BC_D_GE:
  696. case INET_DIAG_BC_D_LE:
  697. if (!valid_port_comparison(bc, len, &min_len))
  698. return -EINVAL;
  699. break;
  700. case INET_DIAG_BC_MARK_COND:
  701. if (!net_admin)
  702. return -EPERM;
  703. if (!valid_markcond(bc, len, &min_len))
  704. return -EINVAL;
  705. break;
  706. case INET_DIAG_BC_AUTO:
  707. case INET_DIAG_BC_JMP:
  708. case INET_DIAG_BC_NOP:
  709. break;
  710. default:
  711. return -EINVAL;
  712. }
  713. if (op->code != INET_DIAG_BC_NOP) {
  714. if (op->no < min_len || op->no > len + 4 || op->no & 3)
  715. return -EINVAL;
  716. if (op->no < len &&
  717. !valid_cc(bytecode, bytecode_len, len - op->no))
  718. return -EINVAL;
  719. }
  720. if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
  721. return -EINVAL;
  722. bc += op->yes;
  723. len -= op->yes;
  724. }
  725. return len == 0 ? 0 : -EINVAL;
  726. }
  727. static int inet_csk_diag_dump(struct sock *sk,
  728. struct sk_buff *skb,
  729. struct netlink_callback *cb,
  730. const struct inet_diag_req_v2 *r,
  731. const struct nlattr *bc,
  732. bool net_admin)
  733. {
  734. if (!inet_diag_bc_sk(bc, sk))
  735. return 0;
  736. return inet_csk_diag_fill(sk, skb, r,
  737. sk_user_ns(NETLINK_CB(cb->skb).sk),
  738. NETLINK_CB(cb->skb).portid,
  739. cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh,
  740. net_admin);
  741. }
  742. static void twsk_build_assert(void)
  743. {
  744. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
  745. offsetof(struct sock, sk_family));
  746. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
  747. offsetof(struct inet_sock, inet_num));
  748. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
  749. offsetof(struct inet_sock, inet_dport));
  750. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
  751. offsetof(struct inet_sock, inet_rcv_saddr));
  752. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
  753. offsetof(struct inet_sock, inet_daddr));
  754. #if IS_ENABLED(CONFIG_IPV6)
  755. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
  756. offsetof(struct sock, sk_v6_rcv_saddr));
  757. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
  758. offsetof(struct sock, sk_v6_daddr));
  759. #endif
  760. }
  761. void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
  762. struct netlink_callback *cb,
  763. const struct inet_diag_req_v2 *r, struct nlattr *bc)
  764. {
  765. bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
  766. struct net *net = sock_net(skb->sk);
  767. u32 idiag_states = r->idiag_states;
  768. int i, num, s_i, s_num;
  769. struct sock *sk;
  770. if (idiag_states & TCPF_SYN_RECV)
  771. idiag_states |= TCPF_NEW_SYN_RECV;
  772. s_i = cb->args[1];
  773. s_num = num = cb->args[2];
  774. if (cb->args[0] == 0) {
  775. if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport)
  776. goto skip_listen_ht;
  777. for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
  778. struct inet_listen_hashbucket *ilb;
  779. num = 0;
  780. ilb = &hashinfo->listening_hash[i];
  781. spin_lock(&ilb->lock);
  782. sk_for_each(sk, &ilb->head) {
  783. struct inet_sock *inet = inet_sk(sk);
  784. if (!net_eq(sock_net(sk), net))
  785. continue;
  786. if (num < s_num) {
  787. num++;
  788. continue;
  789. }
  790. if (r->sdiag_family != AF_UNSPEC &&
  791. sk->sk_family != r->sdiag_family)
  792. goto next_listen;
  793. if (r->id.idiag_sport != inet->inet_sport &&
  794. r->id.idiag_sport)
  795. goto next_listen;
  796. if (inet_csk_diag_dump(sk, skb, cb, r,
  797. bc, net_admin) < 0) {
  798. spin_unlock(&ilb->lock);
  799. goto done;
  800. }
  801. next_listen:
  802. ++num;
  803. }
  804. spin_unlock(&ilb->lock);
  805. s_num = 0;
  806. }
  807. skip_listen_ht:
  808. cb->args[0] = 1;
  809. s_i = num = s_num = 0;
  810. }
  811. if (!(idiag_states & ~TCPF_LISTEN))
  812. goto out;
  813. #define SKARR_SZ 16
  814. for (i = s_i; i <= hashinfo->ehash_mask; i++) {
  815. struct inet_ehash_bucket *head = &hashinfo->ehash[i];
  816. spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
  817. struct hlist_nulls_node *node;
  818. struct sock *sk_arr[SKARR_SZ];
  819. int num_arr[SKARR_SZ];
  820. int idx, accum, res;
  821. if (hlist_nulls_empty(&head->chain))
  822. continue;
  823. if (i > s_i)
  824. s_num = 0;
  825. next_chunk:
  826. num = 0;
  827. accum = 0;
  828. spin_lock_bh(lock);
  829. sk_nulls_for_each(sk, node, &head->chain) {
  830. int state;
  831. if (!net_eq(sock_net(sk), net))
  832. continue;
  833. if (num < s_num)
  834. goto next_normal;
  835. state = (sk->sk_state == TCP_TIME_WAIT) ?
  836. inet_twsk(sk)->tw_substate : sk->sk_state;
  837. if (!(idiag_states & (1 << state)))
  838. goto next_normal;
  839. if (r->sdiag_family != AF_UNSPEC &&
  840. sk->sk_family != r->sdiag_family)
  841. goto next_normal;
  842. if (r->id.idiag_sport != htons(sk->sk_num) &&
  843. r->id.idiag_sport)
  844. goto next_normal;
  845. if (r->id.idiag_dport != sk->sk_dport &&
  846. r->id.idiag_dport)
  847. goto next_normal;
  848. twsk_build_assert();
  849. if (!inet_diag_bc_sk(bc, sk))
  850. goto next_normal;
  851. sock_hold(sk);
  852. num_arr[accum] = num;
  853. sk_arr[accum] = sk;
  854. if (++accum == SKARR_SZ)
  855. break;
  856. next_normal:
  857. ++num;
  858. }
  859. spin_unlock_bh(lock);
  860. res = 0;
  861. for (idx = 0; idx < accum; idx++) {
  862. if (res >= 0) {
  863. res = sk_diag_fill(sk_arr[idx], skb, r,
  864. sk_user_ns(NETLINK_CB(cb->skb).sk),
  865. NETLINK_CB(cb->skb).portid,
  866. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  867. cb->nlh, net_admin);
  868. if (res < 0)
  869. num = num_arr[idx];
  870. }
  871. sock_gen_put(sk_arr[idx]);
  872. }
  873. if (res < 0)
  874. break;
  875. cond_resched();
  876. if (accum == SKARR_SZ) {
  877. s_num = num + 1;
  878. goto next_chunk;
  879. }
  880. }
  881. done:
  882. cb->args[1] = i;
  883. cb->args[2] = num;
  884. out:
  885. ;
  886. }
  887. EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
  888. static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  889. const struct inet_diag_req_v2 *r,
  890. struct nlattr *bc)
  891. {
  892. const struct inet_diag_handler *handler;
  893. int err = 0;
  894. handler = inet_diag_lock_handler(r->sdiag_protocol);
  895. if (!IS_ERR(handler))
  896. handler->dump(skb, cb, r, bc);
  897. else
  898. err = PTR_ERR(handler);
  899. inet_diag_unlock_handler(handler);
  900. return err ? : skb->len;
  901. }
  902. static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  903. {
  904. int hdrlen = sizeof(struct inet_diag_req_v2);
  905. struct nlattr *bc = NULL;
  906. if (nlmsg_attrlen(cb->nlh, hdrlen))
  907. bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
  908. return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
  909. }
  910. static int inet_diag_type2proto(int type)
  911. {
  912. switch (type) {
  913. case TCPDIAG_GETSOCK:
  914. return IPPROTO_TCP;
  915. case DCCPDIAG_GETSOCK:
  916. return IPPROTO_DCCP;
  917. default:
  918. return 0;
  919. }
  920. }
  921. static int inet_diag_dump_compat(struct sk_buff *skb,
  922. struct netlink_callback *cb)
  923. {
  924. struct inet_diag_req *rc = nlmsg_data(cb->nlh);
  925. int hdrlen = sizeof(struct inet_diag_req);
  926. struct inet_diag_req_v2 req;
  927. struct nlattr *bc = NULL;
  928. req.sdiag_family = AF_UNSPEC; /* compatibility */
  929. req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
  930. req.idiag_ext = rc->idiag_ext;
  931. req.idiag_states = rc->idiag_states;
  932. req.id = rc->id;
  933. if (nlmsg_attrlen(cb->nlh, hdrlen))
  934. bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
  935. return __inet_diag_dump(skb, cb, &req, bc);
  936. }
  937. static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
  938. const struct nlmsghdr *nlh)
  939. {
  940. struct inet_diag_req *rc = nlmsg_data(nlh);
  941. struct inet_diag_req_v2 req;
  942. req.sdiag_family = rc->idiag_family;
  943. req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
  944. req.idiag_ext = rc->idiag_ext;
  945. req.idiag_states = rc->idiag_states;
  946. req.id = rc->id;
  947. return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req);
  948. }
  949. static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
  950. {
  951. int hdrlen = sizeof(struct inet_diag_req);
  952. struct net *net = sock_net(skb->sk);
  953. if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
  954. nlmsg_len(nlh) < hdrlen)
  955. return -EINVAL;
  956. if (nlh->nlmsg_flags & NLM_F_DUMP) {
  957. if (nlmsg_attrlen(nlh, hdrlen)) {
  958. struct nlattr *attr;
  959. int err;
  960. attr = nlmsg_find_attr(nlh, hdrlen,
  961. INET_DIAG_REQ_BYTECODE);
  962. err = inet_diag_bc_audit(attr, skb);
  963. if (err)
  964. return err;
  965. }
  966. {
  967. struct netlink_dump_control c = {
  968. .dump = inet_diag_dump_compat,
  969. };
  970. return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
  971. }
  972. }
  973. return inet_diag_get_exact_compat(skb, nlh);
  974. }
  975. static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
  976. {
  977. int hdrlen = sizeof(struct inet_diag_req_v2);
  978. struct net *net = sock_net(skb->sk);
  979. if (nlmsg_len(h) < hdrlen)
  980. return -EINVAL;
  981. if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
  982. h->nlmsg_flags & NLM_F_DUMP) {
  983. if (nlmsg_attrlen(h, hdrlen)) {
  984. struct nlattr *attr;
  985. int err;
  986. attr = nlmsg_find_attr(h, hdrlen,
  987. INET_DIAG_REQ_BYTECODE);
  988. err = inet_diag_bc_audit(attr, skb);
  989. if (err)
  990. return err;
  991. }
  992. {
  993. struct netlink_dump_control c = {
  994. .dump = inet_diag_dump,
  995. };
  996. return netlink_dump_start(net->diag_nlsk, skb, h, &c);
  997. }
  998. }
  999. return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));
  1000. }
  1001. static
  1002. int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
  1003. {
  1004. const struct inet_diag_handler *handler;
  1005. struct nlmsghdr *nlh;
  1006. struct nlattr *attr;
  1007. struct inet_diag_msg *r;
  1008. void *info = NULL;
  1009. int err = 0;
  1010. nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
  1011. if (!nlh)
  1012. return -ENOMEM;
  1013. r = nlmsg_data(nlh);
  1014. memset(r, 0, sizeof(*r));
  1015. inet_diag_msg_common_fill(r, sk);
  1016. if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
  1017. r->id.idiag_sport = inet_sk(sk)->inet_sport;
  1018. r->idiag_state = sk->sk_state;
  1019. if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
  1020. nlmsg_cancel(skb, nlh);
  1021. return err;
  1022. }
  1023. handler = inet_diag_lock_handler(sk->sk_protocol);
  1024. if (IS_ERR(handler)) {
  1025. inet_diag_unlock_handler(handler);
  1026. nlmsg_cancel(skb, nlh);
  1027. return PTR_ERR(handler);
  1028. }
  1029. attr = handler->idiag_info_size
  1030. ? nla_reserve_64bit(skb, INET_DIAG_INFO,
  1031. handler->idiag_info_size,
  1032. INET_DIAG_PAD)
  1033. : NULL;
  1034. if (attr)
  1035. info = nla_data(attr);
  1036. handler->idiag_get_info(sk, r, info);
  1037. inet_diag_unlock_handler(handler);
  1038. nlmsg_end(skb, nlh);
  1039. return 0;
  1040. }
  1041. static const struct sock_diag_handler inet_diag_handler = {
  1042. .family = AF_INET,
  1043. .dump = inet_diag_handler_cmd,
  1044. .get_info = inet_diag_handler_get_info,
  1045. .destroy = inet_diag_handler_cmd,
  1046. };
  1047. static const struct sock_diag_handler inet6_diag_handler = {
  1048. .family = AF_INET6,
  1049. .dump = inet_diag_handler_cmd,
  1050. .get_info = inet_diag_handler_get_info,
  1051. .destroy = inet_diag_handler_cmd,
  1052. };
  1053. int inet_diag_register(const struct inet_diag_handler *h)
  1054. {
  1055. const __u16 type = h->idiag_type;
  1056. int err = -EINVAL;
  1057. if (type >= IPPROTO_MAX)
  1058. goto out;
  1059. mutex_lock(&inet_diag_table_mutex);
  1060. err = -EEXIST;
  1061. if (!inet_diag_table[type]) {
  1062. inet_diag_table[type] = h;
  1063. err = 0;
  1064. }
  1065. mutex_unlock(&inet_diag_table_mutex);
  1066. out:
  1067. return err;
  1068. }
  1069. EXPORT_SYMBOL_GPL(inet_diag_register);
  1070. void inet_diag_unregister(const struct inet_diag_handler *h)
  1071. {
  1072. const __u16 type = h->idiag_type;
  1073. if (type >= IPPROTO_MAX)
  1074. return;
  1075. mutex_lock(&inet_diag_table_mutex);
  1076. inet_diag_table[type] = NULL;
  1077. mutex_unlock(&inet_diag_table_mutex);
  1078. }
  1079. EXPORT_SYMBOL_GPL(inet_diag_unregister);
  1080. static int __init inet_diag_init(void)
  1081. {
  1082. const int inet_diag_table_size = (IPPROTO_MAX *
  1083. sizeof(struct inet_diag_handler *));
  1084. int err = -ENOMEM;
  1085. inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
  1086. if (!inet_diag_table)
  1087. goto out;
  1088. err = sock_diag_register(&inet_diag_handler);
  1089. if (err)
  1090. goto out_free_nl;
  1091. err = sock_diag_register(&inet6_diag_handler);
  1092. if (err)
  1093. goto out_free_inet;
  1094. sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
  1095. out:
  1096. return err;
  1097. out_free_inet:
  1098. sock_diag_unregister(&inet_diag_handler);
  1099. out_free_nl:
  1100. kfree(inet_diag_table);
  1101. goto out;
  1102. }
  1103. static void __exit inet_diag_exit(void)
  1104. {
  1105. sock_diag_unregister(&inet6_diag_handler);
  1106. sock_diag_unregister(&inet_diag_handler);
  1107. sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
  1108. kfree(inet_diag_table);
  1109. }
  1110. module_init(inet_diag_init);
  1111. module_exit(inet_diag_exit);
  1112. MODULE_LICENSE("GPL");
  1113. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
  1114. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);