inet_diag.c 27 KB

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