inet_diag.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. __be32 *saddr;
  36. __be32 *daddr;
  37. u16 sport;
  38. u16 dport;
  39. u16 family;
  40. u16 userlocks;
  41. };
  42. static DEFINE_MUTEX(inet_diag_table_mutex);
  43. static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
  44. {
  45. if (!inet_diag_table[proto])
  46. request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
  47. NETLINK_SOCK_DIAG, AF_INET, proto);
  48. mutex_lock(&inet_diag_table_mutex);
  49. if (!inet_diag_table[proto])
  50. return ERR_PTR(-ENOENT);
  51. return inet_diag_table[proto];
  52. }
  53. static inline void inet_diag_unlock_handler(
  54. const struct inet_diag_handler *handler)
  55. {
  56. mutex_unlock(&inet_diag_table_mutex);
  57. }
  58. int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
  59. struct sk_buff *skb, struct inet_diag_req_v2 *req,
  60. struct user_namespace *user_ns,
  61. u32 portid, u32 seq, u16 nlmsg_flags,
  62. const struct nlmsghdr *unlh)
  63. {
  64. const struct inet_sock *inet = inet_sk(sk);
  65. struct inet_diag_msg *r;
  66. struct nlmsghdr *nlh;
  67. struct nlattr *attr;
  68. void *info = NULL;
  69. const struct inet_diag_handler *handler;
  70. int ext = req->idiag_ext;
  71. handler = inet_diag_table[req->sdiag_protocol];
  72. BUG_ON(handler == NULL);
  73. nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
  74. nlmsg_flags);
  75. if (!nlh)
  76. return -EMSGSIZE;
  77. r = nlmsg_data(nlh);
  78. BUG_ON(sk->sk_state == TCP_TIME_WAIT);
  79. r->idiag_family = sk->sk_family;
  80. r->idiag_state = sk->sk_state;
  81. r->idiag_timer = 0;
  82. r->idiag_retrans = 0;
  83. r->id.idiag_if = sk->sk_bound_dev_if;
  84. sock_diag_save_cookie(sk, r->id.idiag_cookie);
  85. r->id.idiag_sport = inet->inet_sport;
  86. r->id.idiag_dport = inet->inet_dport;
  87. r->id.idiag_src[0] = inet->inet_rcv_saddr;
  88. r->id.idiag_dst[0] = inet->inet_daddr;
  89. if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
  90. goto errout;
  91. /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
  92. * hence this needs to be included regardless of socket family.
  93. */
  94. if (ext & (1 << (INET_DIAG_TOS - 1)))
  95. if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
  96. goto errout;
  97. #if IS_ENABLED(CONFIG_IPV6)
  98. if (r->idiag_family == AF_INET6) {
  99. const struct ipv6_pinfo *np = inet6_sk(sk);
  100. *(struct in6_addr *)r->id.idiag_src = np->rcv_saddr;
  101. *(struct in6_addr *)r->id.idiag_dst = np->daddr;
  102. if (ext & (1 << (INET_DIAG_TCLASS - 1)))
  103. if (nla_put_u8(skb, INET_DIAG_TCLASS, np->tclass) < 0)
  104. goto errout;
  105. }
  106. #endif
  107. r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
  108. r->idiag_inode = sock_i_ino(sk);
  109. if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
  110. struct inet_diag_meminfo minfo = {
  111. .idiag_rmem = sk_rmem_alloc_get(sk),
  112. .idiag_wmem = sk->sk_wmem_queued,
  113. .idiag_fmem = sk->sk_forward_alloc,
  114. .idiag_tmem = sk_wmem_alloc_get(sk),
  115. };
  116. if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
  117. goto errout;
  118. }
  119. if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
  120. if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
  121. goto errout;
  122. if (icsk == NULL) {
  123. handler->idiag_get_info(sk, r, NULL);
  124. goto out;
  125. }
  126. #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
  127. if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
  128. r->idiag_timer = 1;
  129. r->idiag_retrans = icsk->icsk_retransmits;
  130. r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
  131. } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
  132. r->idiag_timer = 4;
  133. r->idiag_retrans = icsk->icsk_probes_out;
  134. r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
  135. } else if (timer_pending(&sk->sk_timer)) {
  136. r->idiag_timer = 2;
  137. r->idiag_retrans = icsk->icsk_probes_out;
  138. r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
  139. } else {
  140. r->idiag_timer = 0;
  141. r->idiag_expires = 0;
  142. }
  143. #undef EXPIRES_IN_MS
  144. if (ext & (1 << (INET_DIAG_INFO - 1))) {
  145. attr = nla_reserve(skb, INET_DIAG_INFO,
  146. sizeof(struct tcp_info));
  147. if (!attr)
  148. goto errout;
  149. info = nla_data(attr);
  150. }
  151. if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops)
  152. if (nla_put_string(skb, INET_DIAG_CONG,
  153. icsk->icsk_ca_ops->name) < 0)
  154. goto errout;
  155. handler->idiag_get_info(sk, r, info);
  156. if (sk->sk_state < TCP_TIME_WAIT &&
  157. icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
  158. icsk->icsk_ca_ops->get_info(sk, ext, skb);
  159. out:
  160. return nlmsg_end(skb, nlh);
  161. errout:
  162. nlmsg_cancel(skb, nlh);
  163. return -EMSGSIZE;
  164. }
  165. EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
  166. static int inet_csk_diag_fill(struct sock *sk,
  167. struct sk_buff *skb, struct inet_diag_req_v2 *req,
  168. struct user_namespace *user_ns,
  169. u32 portid, u32 seq, u16 nlmsg_flags,
  170. const struct nlmsghdr *unlh)
  171. {
  172. return inet_sk_diag_fill(sk, inet_csk(sk),
  173. skb, req, user_ns, portid, seq, nlmsg_flags, unlh);
  174. }
  175. static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
  176. struct sk_buff *skb, struct inet_diag_req_v2 *req,
  177. u32 portid, u32 seq, u16 nlmsg_flags,
  178. const struct nlmsghdr *unlh)
  179. {
  180. long tmo;
  181. struct inet_diag_msg *r;
  182. struct nlmsghdr *nlh;
  183. nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
  184. nlmsg_flags);
  185. if (!nlh)
  186. return -EMSGSIZE;
  187. r = nlmsg_data(nlh);
  188. BUG_ON(tw->tw_state != TCP_TIME_WAIT);
  189. tmo = tw->tw_ttd - jiffies;
  190. if (tmo < 0)
  191. tmo = 0;
  192. r->idiag_family = tw->tw_family;
  193. r->idiag_retrans = 0;
  194. r->id.idiag_if = tw->tw_bound_dev_if;
  195. sock_diag_save_cookie(tw, r->id.idiag_cookie);
  196. r->id.idiag_sport = tw->tw_sport;
  197. r->id.idiag_dport = tw->tw_dport;
  198. r->id.idiag_src[0] = tw->tw_rcv_saddr;
  199. r->id.idiag_dst[0] = tw->tw_daddr;
  200. r->idiag_state = tw->tw_substate;
  201. r->idiag_timer = 3;
  202. r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ);
  203. r->idiag_rqueue = 0;
  204. r->idiag_wqueue = 0;
  205. r->idiag_uid = 0;
  206. r->idiag_inode = 0;
  207. #if IS_ENABLED(CONFIG_IPV6)
  208. if (tw->tw_family == AF_INET6) {
  209. const struct inet6_timewait_sock *tw6 =
  210. inet6_twsk((struct sock *)tw);
  211. *(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr;
  212. *(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr;
  213. }
  214. #endif
  215. return nlmsg_end(skb, nlh);
  216. }
  217. static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
  218. struct inet_diag_req_v2 *r,
  219. struct user_namespace *user_ns,
  220. u32 portid, u32 seq, u16 nlmsg_flags,
  221. const struct nlmsghdr *unlh)
  222. {
  223. if (sk->sk_state == TCP_TIME_WAIT)
  224. return inet_twsk_diag_fill((struct inet_timewait_sock *)sk,
  225. skb, r, portid, seq, nlmsg_flags,
  226. unlh);
  227. return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq, nlmsg_flags, unlh);
  228. }
  229. int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb,
  230. const struct nlmsghdr *nlh, struct inet_diag_req_v2 *req)
  231. {
  232. int err;
  233. struct sock *sk;
  234. struct sk_buff *rep;
  235. struct net *net = sock_net(in_skb->sk);
  236. err = -EINVAL;
  237. if (req->sdiag_family == AF_INET) {
  238. sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
  239. req->id.idiag_dport, req->id.idiag_src[0],
  240. req->id.idiag_sport, req->id.idiag_if);
  241. }
  242. #if IS_ENABLED(CONFIG_IPV6)
  243. else if (req->sdiag_family == AF_INET6) {
  244. sk = inet6_lookup(net, hashinfo,
  245. (struct in6_addr *)req->id.idiag_dst,
  246. req->id.idiag_dport,
  247. (struct in6_addr *)req->id.idiag_src,
  248. req->id.idiag_sport,
  249. req->id.idiag_if);
  250. }
  251. #endif
  252. else {
  253. goto out_nosk;
  254. }
  255. err = -ENOENT;
  256. if (sk == NULL)
  257. goto out_nosk;
  258. err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
  259. if (err)
  260. goto out;
  261. rep = nlmsg_new(sizeof(struct inet_diag_msg) +
  262. sizeof(struct inet_diag_meminfo) +
  263. sizeof(struct tcp_info) + 64, GFP_KERNEL);
  264. if (!rep) {
  265. err = -ENOMEM;
  266. goto out;
  267. }
  268. err = sk_diag_fill(sk, rep, req,
  269. sk_user_ns(NETLINK_CB(in_skb).ssk),
  270. NETLINK_CB(in_skb).portid,
  271. nlh->nlmsg_seq, 0, nlh);
  272. if (err < 0) {
  273. WARN_ON(err == -EMSGSIZE);
  274. nlmsg_free(rep);
  275. goto out;
  276. }
  277. err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
  278. MSG_DONTWAIT);
  279. if (err > 0)
  280. err = 0;
  281. out:
  282. if (sk) {
  283. if (sk->sk_state == TCP_TIME_WAIT)
  284. inet_twsk_put((struct inet_timewait_sock *)sk);
  285. else
  286. sock_put(sk);
  287. }
  288. out_nosk:
  289. return err;
  290. }
  291. EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
  292. static int inet_diag_get_exact(struct sk_buff *in_skb,
  293. const struct nlmsghdr *nlh,
  294. struct inet_diag_req_v2 *req)
  295. {
  296. const struct inet_diag_handler *handler;
  297. int err;
  298. handler = inet_diag_lock_handler(req->sdiag_protocol);
  299. if (IS_ERR(handler))
  300. err = PTR_ERR(handler);
  301. else
  302. err = handler->dump_one(in_skb, nlh, req);
  303. inet_diag_unlock_handler(handler);
  304. return err;
  305. }
  306. static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
  307. {
  308. int words = bits >> 5;
  309. bits &= 0x1f;
  310. if (words) {
  311. if (memcmp(a1, a2, words << 2))
  312. return 0;
  313. }
  314. if (bits) {
  315. __be32 w1, w2;
  316. __be32 mask;
  317. w1 = a1[words];
  318. w2 = a2[words];
  319. mask = htonl((0xffffffff) << (32 - bits));
  320. if ((w1 ^ w2) & mask)
  321. return 0;
  322. }
  323. return 1;
  324. }
  325. static int inet_diag_bc_run(const struct nlattr *_bc,
  326. const struct inet_diag_entry *entry)
  327. {
  328. const void *bc = nla_data(_bc);
  329. int len = nla_len(_bc);
  330. while (len > 0) {
  331. int yes = 1;
  332. const struct inet_diag_bc_op *op = bc;
  333. switch (op->code) {
  334. case INET_DIAG_BC_NOP:
  335. break;
  336. case INET_DIAG_BC_JMP:
  337. yes = 0;
  338. break;
  339. case INET_DIAG_BC_S_GE:
  340. yes = entry->sport >= op[1].no;
  341. break;
  342. case INET_DIAG_BC_S_LE:
  343. yes = entry->sport <= op[1].no;
  344. break;
  345. case INET_DIAG_BC_D_GE:
  346. yes = entry->dport >= op[1].no;
  347. break;
  348. case INET_DIAG_BC_D_LE:
  349. yes = entry->dport <= op[1].no;
  350. break;
  351. case INET_DIAG_BC_AUTO:
  352. yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
  353. break;
  354. case INET_DIAG_BC_S_COND:
  355. case INET_DIAG_BC_D_COND: {
  356. struct inet_diag_hostcond *cond;
  357. __be32 *addr;
  358. cond = (struct inet_diag_hostcond *)(op + 1);
  359. if (cond->port != -1 &&
  360. cond->port != (op->code == INET_DIAG_BC_S_COND ?
  361. entry->sport : entry->dport)) {
  362. yes = 0;
  363. break;
  364. }
  365. if (cond->prefix_len == 0)
  366. break;
  367. if (op->code == INET_DIAG_BC_S_COND)
  368. addr = entry->saddr;
  369. else
  370. addr = entry->daddr;
  371. if (bitstring_match(addr, cond->addr,
  372. cond->prefix_len))
  373. break;
  374. if (entry->family == AF_INET6 &&
  375. cond->family == AF_INET) {
  376. if (addr[0] == 0 && addr[1] == 0 &&
  377. addr[2] == htonl(0xffff) &&
  378. bitstring_match(addr + 3, cond->addr,
  379. cond->prefix_len))
  380. break;
  381. }
  382. yes = 0;
  383. break;
  384. }
  385. }
  386. if (yes) {
  387. len -= op->yes;
  388. bc += op->yes;
  389. } else {
  390. len -= op->no;
  391. bc += op->no;
  392. }
  393. }
  394. return len == 0;
  395. }
  396. int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
  397. {
  398. struct inet_diag_entry entry;
  399. struct inet_sock *inet = inet_sk(sk);
  400. if (bc == NULL)
  401. return 1;
  402. entry.family = sk->sk_family;
  403. #if IS_ENABLED(CONFIG_IPV6)
  404. if (entry.family == AF_INET6) {
  405. struct ipv6_pinfo *np = inet6_sk(sk);
  406. entry.saddr = np->rcv_saddr.s6_addr32;
  407. entry.daddr = np->daddr.s6_addr32;
  408. } else
  409. #endif
  410. {
  411. entry.saddr = &inet->inet_rcv_saddr;
  412. entry.daddr = &inet->inet_daddr;
  413. }
  414. entry.sport = inet->inet_num;
  415. entry.dport = ntohs(inet->inet_dport);
  416. entry.userlocks = sk->sk_userlocks;
  417. return inet_diag_bc_run(bc, &entry);
  418. }
  419. EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
  420. static int valid_cc(const void *bc, int len, int cc)
  421. {
  422. while (len >= 0) {
  423. const struct inet_diag_bc_op *op = bc;
  424. if (cc > len)
  425. return 0;
  426. if (cc == len)
  427. return 1;
  428. if (op->yes < 4 || op->yes & 3)
  429. return 0;
  430. len -= op->yes;
  431. bc += op->yes;
  432. }
  433. return 0;
  434. }
  435. static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
  436. {
  437. const void *bc = bytecode;
  438. int len = bytecode_len;
  439. while (len > 0) {
  440. const struct inet_diag_bc_op *op = bc;
  441. //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
  442. switch (op->code) {
  443. case INET_DIAG_BC_AUTO:
  444. case INET_DIAG_BC_S_COND:
  445. case INET_DIAG_BC_D_COND:
  446. case INET_DIAG_BC_S_GE:
  447. case INET_DIAG_BC_S_LE:
  448. case INET_DIAG_BC_D_GE:
  449. case INET_DIAG_BC_D_LE:
  450. case INET_DIAG_BC_JMP:
  451. if (op->no < 4 || op->no > len + 4 || op->no & 3)
  452. return -EINVAL;
  453. if (op->no < len &&
  454. !valid_cc(bytecode, bytecode_len, len - op->no))
  455. return -EINVAL;
  456. break;
  457. case INET_DIAG_BC_NOP:
  458. break;
  459. default:
  460. return -EINVAL;
  461. }
  462. if (op->yes < 4 || op->yes > len + 4 || op->yes & 3)
  463. return -EINVAL;
  464. bc += op->yes;
  465. len -= op->yes;
  466. }
  467. return len == 0 ? 0 : -EINVAL;
  468. }
  469. static int inet_csk_diag_dump(struct sock *sk,
  470. struct sk_buff *skb,
  471. struct netlink_callback *cb,
  472. struct inet_diag_req_v2 *r,
  473. const struct nlattr *bc)
  474. {
  475. if (!inet_diag_bc_sk(bc, sk))
  476. return 0;
  477. return inet_csk_diag_fill(sk, skb, r,
  478. sk_user_ns(NETLINK_CB(cb->skb).ssk),
  479. NETLINK_CB(cb->skb).portid,
  480. cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
  481. }
  482. static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
  483. struct sk_buff *skb,
  484. struct netlink_callback *cb,
  485. struct inet_diag_req_v2 *r,
  486. const struct nlattr *bc)
  487. {
  488. if (bc != NULL) {
  489. struct inet_diag_entry entry;
  490. entry.family = tw->tw_family;
  491. #if IS_ENABLED(CONFIG_IPV6)
  492. if (tw->tw_family == AF_INET6) {
  493. struct inet6_timewait_sock *tw6 =
  494. inet6_twsk((struct sock *)tw);
  495. entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
  496. entry.daddr = tw6->tw_v6_daddr.s6_addr32;
  497. } else
  498. #endif
  499. {
  500. entry.saddr = &tw->tw_rcv_saddr;
  501. entry.daddr = &tw->tw_daddr;
  502. }
  503. entry.sport = tw->tw_num;
  504. entry.dport = ntohs(tw->tw_dport);
  505. entry.userlocks = 0;
  506. if (!inet_diag_bc_run(bc, &entry))
  507. return 0;
  508. }
  509. return inet_twsk_diag_fill(tw, skb, r,
  510. NETLINK_CB(cb->skb).portid,
  511. cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
  512. }
  513. static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
  514. struct request_sock *req,
  515. struct user_namespace *user_ns,
  516. u32 portid, u32 seq,
  517. const struct nlmsghdr *unlh)
  518. {
  519. const struct inet_request_sock *ireq = inet_rsk(req);
  520. struct inet_sock *inet = inet_sk(sk);
  521. struct inet_diag_msg *r;
  522. struct nlmsghdr *nlh;
  523. long tmo;
  524. nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
  525. NLM_F_MULTI);
  526. if (!nlh)
  527. return -EMSGSIZE;
  528. r = nlmsg_data(nlh);
  529. r->idiag_family = sk->sk_family;
  530. r->idiag_state = TCP_SYN_RECV;
  531. r->idiag_timer = 1;
  532. r->idiag_retrans = req->retrans;
  533. r->id.idiag_if = sk->sk_bound_dev_if;
  534. sock_diag_save_cookie(req, r->id.idiag_cookie);
  535. tmo = req->expires - jiffies;
  536. if (tmo < 0)
  537. tmo = 0;
  538. r->id.idiag_sport = inet->inet_sport;
  539. r->id.idiag_dport = ireq->rmt_port;
  540. r->id.idiag_src[0] = ireq->loc_addr;
  541. r->id.idiag_dst[0] = ireq->rmt_addr;
  542. r->idiag_expires = jiffies_to_msecs(tmo);
  543. r->idiag_rqueue = 0;
  544. r->idiag_wqueue = 0;
  545. r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
  546. r->idiag_inode = 0;
  547. #if IS_ENABLED(CONFIG_IPV6)
  548. if (r->idiag_family == AF_INET6) {
  549. *(struct in6_addr *)r->id.idiag_src = inet6_rsk(req)->loc_addr;
  550. *(struct in6_addr *)r->id.idiag_dst = inet6_rsk(req)->rmt_addr;
  551. }
  552. #endif
  553. return nlmsg_end(skb, nlh);
  554. }
  555. static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
  556. struct netlink_callback *cb,
  557. struct inet_diag_req_v2 *r,
  558. const struct nlattr *bc)
  559. {
  560. struct inet_diag_entry entry;
  561. struct inet_connection_sock *icsk = inet_csk(sk);
  562. struct listen_sock *lopt;
  563. struct inet_sock *inet = inet_sk(sk);
  564. int j, s_j;
  565. int reqnum, s_reqnum;
  566. int err = 0;
  567. s_j = cb->args[3];
  568. s_reqnum = cb->args[4];
  569. if (s_j > 0)
  570. s_j--;
  571. entry.family = sk->sk_family;
  572. read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  573. lopt = icsk->icsk_accept_queue.listen_opt;
  574. if (!lopt || !lopt->qlen)
  575. goto out;
  576. if (bc != NULL) {
  577. entry.sport = inet->inet_num;
  578. entry.userlocks = sk->sk_userlocks;
  579. }
  580. for (j = s_j; j < lopt->nr_table_entries; j++) {
  581. struct request_sock *req, *head = lopt->syn_table[j];
  582. reqnum = 0;
  583. for (req = head; req; reqnum++, req = req->dl_next) {
  584. struct inet_request_sock *ireq = inet_rsk(req);
  585. if (reqnum < s_reqnum)
  586. continue;
  587. if (r->id.idiag_dport != ireq->rmt_port &&
  588. r->id.idiag_dport)
  589. continue;
  590. if (bc) {
  591. entry.saddr =
  592. #if IS_ENABLED(CONFIG_IPV6)
  593. (entry.family == AF_INET6) ?
  594. inet6_rsk(req)->loc_addr.s6_addr32 :
  595. #endif
  596. &ireq->loc_addr;
  597. entry.daddr =
  598. #if IS_ENABLED(CONFIG_IPV6)
  599. (entry.family == AF_INET6) ?
  600. inet6_rsk(req)->rmt_addr.s6_addr32 :
  601. #endif
  602. &ireq->rmt_addr;
  603. entry.dport = ntohs(ireq->rmt_port);
  604. if (!inet_diag_bc_run(bc, &entry))
  605. continue;
  606. }
  607. err = inet_diag_fill_req(skb, sk, req,
  608. sk_user_ns(NETLINK_CB(cb->skb).ssk),
  609. NETLINK_CB(cb->skb).portid,
  610. cb->nlh->nlmsg_seq, cb->nlh);
  611. if (err < 0) {
  612. cb->args[3] = j + 1;
  613. cb->args[4] = reqnum;
  614. goto out;
  615. }
  616. }
  617. s_reqnum = 0;
  618. }
  619. out:
  620. read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  621. return err;
  622. }
  623. void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
  624. struct netlink_callback *cb, struct inet_diag_req_v2 *r, struct nlattr *bc)
  625. {
  626. int i, num;
  627. int s_i, s_num;
  628. struct net *net = sock_net(skb->sk);
  629. s_i = cb->args[1];
  630. s_num = num = cb->args[2];
  631. if (cb->args[0] == 0) {
  632. if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
  633. goto skip_listen_ht;
  634. for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
  635. struct sock *sk;
  636. struct hlist_nulls_node *node;
  637. struct inet_listen_hashbucket *ilb;
  638. num = 0;
  639. ilb = &hashinfo->listening_hash[i];
  640. spin_lock_bh(&ilb->lock);
  641. sk_nulls_for_each(sk, node, &ilb->head) {
  642. struct inet_sock *inet = inet_sk(sk);
  643. if (!net_eq(sock_net(sk), net))
  644. continue;
  645. if (num < s_num) {
  646. num++;
  647. continue;
  648. }
  649. if (r->sdiag_family != AF_UNSPEC &&
  650. sk->sk_family != r->sdiag_family)
  651. goto next_listen;
  652. if (r->id.idiag_sport != inet->inet_sport &&
  653. r->id.idiag_sport)
  654. goto next_listen;
  655. if (!(r->idiag_states & TCPF_LISTEN) ||
  656. r->id.idiag_dport ||
  657. cb->args[3] > 0)
  658. goto syn_recv;
  659. if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
  660. spin_unlock_bh(&ilb->lock);
  661. goto done;
  662. }
  663. syn_recv:
  664. if (!(r->idiag_states & TCPF_SYN_RECV))
  665. goto next_listen;
  666. if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
  667. spin_unlock_bh(&ilb->lock);
  668. goto done;
  669. }
  670. next_listen:
  671. cb->args[3] = 0;
  672. cb->args[4] = 0;
  673. ++num;
  674. }
  675. spin_unlock_bh(&ilb->lock);
  676. s_num = 0;
  677. cb->args[3] = 0;
  678. cb->args[4] = 0;
  679. }
  680. skip_listen_ht:
  681. cb->args[0] = 1;
  682. s_i = num = s_num = 0;
  683. }
  684. if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
  685. goto out;
  686. for (i = s_i; i <= hashinfo->ehash_mask; i++) {
  687. struct inet_ehash_bucket *head = &hashinfo->ehash[i];
  688. spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
  689. struct sock *sk;
  690. struct hlist_nulls_node *node;
  691. num = 0;
  692. if (hlist_nulls_empty(&head->chain) &&
  693. hlist_nulls_empty(&head->twchain))
  694. continue;
  695. if (i > s_i)
  696. s_num = 0;
  697. spin_lock_bh(lock);
  698. sk_nulls_for_each(sk, node, &head->chain) {
  699. struct inet_sock *inet = inet_sk(sk);
  700. if (!net_eq(sock_net(sk), net))
  701. continue;
  702. if (num < s_num)
  703. goto next_normal;
  704. if (!(r->idiag_states & (1 << sk->sk_state)))
  705. goto next_normal;
  706. if (r->sdiag_family != AF_UNSPEC &&
  707. sk->sk_family != r->sdiag_family)
  708. goto next_normal;
  709. if (r->id.idiag_sport != inet->inet_sport &&
  710. r->id.idiag_sport)
  711. goto next_normal;
  712. if (r->id.idiag_dport != inet->inet_dport &&
  713. r->id.idiag_dport)
  714. goto next_normal;
  715. if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
  716. spin_unlock_bh(lock);
  717. goto done;
  718. }
  719. next_normal:
  720. ++num;
  721. }
  722. if (r->idiag_states & TCPF_TIME_WAIT) {
  723. struct inet_timewait_sock *tw;
  724. inet_twsk_for_each(tw, node,
  725. &head->twchain) {
  726. if (!net_eq(twsk_net(tw), net))
  727. continue;
  728. if (num < s_num)
  729. goto next_dying;
  730. if (r->sdiag_family != AF_UNSPEC &&
  731. tw->tw_family != r->sdiag_family)
  732. goto next_dying;
  733. if (r->id.idiag_sport != tw->tw_sport &&
  734. r->id.idiag_sport)
  735. goto next_dying;
  736. if (r->id.idiag_dport != tw->tw_dport &&
  737. r->id.idiag_dport)
  738. goto next_dying;
  739. if (inet_twsk_diag_dump(tw, skb, cb, r, bc) < 0) {
  740. spin_unlock_bh(lock);
  741. goto done;
  742. }
  743. next_dying:
  744. ++num;
  745. }
  746. }
  747. spin_unlock_bh(lock);
  748. }
  749. done:
  750. cb->args[1] = i;
  751. cb->args[2] = num;
  752. out:
  753. ;
  754. }
  755. EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
  756. static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  757. struct inet_diag_req_v2 *r, struct nlattr *bc)
  758. {
  759. const struct inet_diag_handler *handler;
  760. handler = inet_diag_lock_handler(r->sdiag_protocol);
  761. if (!IS_ERR(handler))
  762. handler->dump(skb, cb, r, bc);
  763. inet_diag_unlock_handler(handler);
  764. return skb->len;
  765. }
  766. static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  767. {
  768. struct nlattr *bc = NULL;
  769. int hdrlen = sizeof(struct inet_diag_req_v2);
  770. if (nlmsg_attrlen(cb->nlh, hdrlen))
  771. bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
  772. return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
  773. }
  774. static inline int inet_diag_type2proto(int type)
  775. {
  776. switch (type) {
  777. case TCPDIAG_GETSOCK:
  778. return IPPROTO_TCP;
  779. case DCCPDIAG_GETSOCK:
  780. return IPPROTO_DCCP;
  781. default:
  782. return 0;
  783. }
  784. }
  785. static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *cb)
  786. {
  787. struct inet_diag_req *rc = nlmsg_data(cb->nlh);
  788. struct inet_diag_req_v2 req;
  789. struct nlattr *bc = NULL;
  790. int hdrlen = sizeof(struct inet_diag_req);
  791. req.sdiag_family = AF_UNSPEC; /* compatibility */
  792. req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
  793. req.idiag_ext = rc->idiag_ext;
  794. req.idiag_states = rc->idiag_states;
  795. req.id = rc->id;
  796. if (nlmsg_attrlen(cb->nlh, hdrlen))
  797. bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
  798. return __inet_diag_dump(skb, cb, &req, bc);
  799. }
  800. static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
  801. const struct nlmsghdr *nlh)
  802. {
  803. struct inet_diag_req *rc = nlmsg_data(nlh);
  804. struct inet_diag_req_v2 req;
  805. req.sdiag_family = rc->idiag_family;
  806. req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
  807. req.idiag_ext = rc->idiag_ext;
  808. req.idiag_states = rc->idiag_states;
  809. req.id = rc->id;
  810. return inet_diag_get_exact(in_skb, nlh, &req);
  811. }
  812. static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
  813. {
  814. int hdrlen = sizeof(struct inet_diag_req);
  815. struct net *net = sock_net(skb->sk);
  816. if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
  817. nlmsg_len(nlh) < hdrlen)
  818. return -EINVAL;
  819. if (nlh->nlmsg_flags & NLM_F_DUMP) {
  820. if (nlmsg_attrlen(nlh, hdrlen)) {
  821. struct nlattr *attr;
  822. attr = nlmsg_find_attr(nlh, hdrlen,
  823. INET_DIAG_REQ_BYTECODE);
  824. if (attr == NULL ||
  825. nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
  826. inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
  827. return -EINVAL;
  828. }
  829. {
  830. struct netlink_dump_control c = {
  831. .dump = inet_diag_dump_compat,
  832. };
  833. return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
  834. }
  835. }
  836. return inet_diag_get_exact_compat(skb, nlh);
  837. }
  838. static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
  839. {
  840. int hdrlen = sizeof(struct inet_diag_req_v2);
  841. struct net *net = sock_net(skb->sk);
  842. if (nlmsg_len(h) < hdrlen)
  843. return -EINVAL;
  844. if (h->nlmsg_flags & NLM_F_DUMP) {
  845. if (nlmsg_attrlen(h, hdrlen)) {
  846. struct nlattr *attr;
  847. attr = nlmsg_find_attr(h, hdrlen,
  848. INET_DIAG_REQ_BYTECODE);
  849. if (attr == NULL ||
  850. nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
  851. inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
  852. return -EINVAL;
  853. }
  854. {
  855. struct netlink_dump_control c = {
  856. .dump = inet_diag_dump,
  857. };
  858. return netlink_dump_start(net->diag_nlsk, skb, h, &c);
  859. }
  860. }
  861. return inet_diag_get_exact(skb, h, nlmsg_data(h));
  862. }
  863. static const struct sock_diag_handler inet_diag_handler = {
  864. .family = AF_INET,
  865. .dump = inet_diag_handler_dump,
  866. };
  867. static const struct sock_diag_handler inet6_diag_handler = {
  868. .family = AF_INET6,
  869. .dump = inet_diag_handler_dump,
  870. };
  871. int inet_diag_register(const struct inet_diag_handler *h)
  872. {
  873. const __u16 type = h->idiag_type;
  874. int err = -EINVAL;
  875. if (type >= IPPROTO_MAX)
  876. goto out;
  877. mutex_lock(&inet_diag_table_mutex);
  878. err = -EEXIST;
  879. if (inet_diag_table[type] == NULL) {
  880. inet_diag_table[type] = h;
  881. err = 0;
  882. }
  883. mutex_unlock(&inet_diag_table_mutex);
  884. out:
  885. return err;
  886. }
  887. EXPORT_SYMBOL_GPL(inet_diag_register);
  888. void inet_diag_unregister(const struct inet_diag_handler *h)
  889. {
  890. const __u16 type = h->idiag_type;
  891. if (type >= IPPROTO_MAX)
  892. return;
  893. mutex_lock(&inet_diag_table_mutex);
  894. inet_diag_table[type] = NULL;
  895. mutex_unlock(&inet_diag_table_mutex);
  896. }
  897. EXPORT_SYMBOL_GPL(inet_diag_unregister);
  898. static int __init inet_diag_init(void)
  899. {
  900. const int inet_diag_table_size = (IPPROTO_MAX *
  901. sizeof(struct inet_diag_handler *));
  902. int err = -ENOMEM;
  903. inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
  904. if (!inet_diag_table)
  905. goto out;
  906. err = sock_diag_register(&inet_diag_handler);
  907. if (err)
  908. goto out_free_nl;
  909. err = sock_diag_register(&inet6_diag_handler);
  910. if (err)
  911. goto out_free_inet;
  912. sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
  913. out:
  914. return err;
  915. out_free_inet:
  916. sock_diag_unregister(&inet_diag_handler);
  917. out_free_nl:
  918. kfree(inet_diag_table);
  919. goto out;
  920. }
  921. static void __exit inet_diag_exit(void)
  922. {
  923. sock_diag_unregister(&inet6_diag_handler);
  924. sock_diag_unregister(&inet_diag_handler);
  925. sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
  926. kfree(inet_diag_table);
  927. }
  928. module_init(inet_diag_init);
  929. module_exit(inet_diag_exit);
  930. MODULE_LICENSE("GPL");
  931. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
  932. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);