inet_diag.c 26 KB

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