inet_diag.c 27 KB

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