inet_diag.c 32 KB

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