inet_diag.c 31 KB

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