inet_diag.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. struct sock *inet_diag_find_one_icsk(struct net *net,
  295. struct inet_hashinfo *hashinfo,
  296. const struct inet_diag_req_v2 *req)
  297. {
  298. struct sock *sk;
  299. if (req->sdiag_family == AF_INET)
  300. sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
  301. req->id.idiag_dport, req->id.idiag_src[0],
  302. req->id.idiag_sport, req->id.idiag_if);
  303. #if IS_ENABLED(CONFIG_IPV6)
  304. else if (req->sdiag_family == AF_INET6)
  305. sk = inet6_lookup(net, hashinfo,
  306. (struct in6_addr *)req->id.idiag_dst,
  307. req->id.idiag_dport,
  308. (struct in6_addr *)req->id.idiag_src,
  309. req->id.idiag_sport,
  310. req->id.idiag_if);
  311. #endif
  312. else
  313. return ERR_PTR(-EINVAL);
  314. if (!sk)
  315. return ERR_PTR(-ENOENT);
  316. if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
  317. sock_gen_put(sk);
  318. return ERR_PTR(-ENOENT);
  319. }
  320. return sk;
  321. }
  322. EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
  323. int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
  324. struct sk_buff *in_skb,
  325. const struct nlmsghdr *nlh,
  326. const struct inet_diag_req_v2 *req)
  327. {
  328. struct net *net = sock_net(in_skb->sk);
  329. struct sk_buff *rep;
  330. struct sock *sk;
  331. int err;
  332. sk = inet_diag_find_one_icsk(net, hashinfo, req);
  333. if (IS_ERR(sk))
  334. return PTR_ERR(sk);
  335. rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL);
  336. if (!rep) {
  337. err = -ENOMEM;
  338. goto out;
  339. }
  340. err = sk_diag_fill(sk, rep, req,
  341. sk_user_ns(NETLINK_CB(in_skb).sk),
  342. NETLINK_CB(in_skb).portid,
  343. nlh->nlmsg_seq, 0, nlh);
  344. if (err < 0) {
  345. WARN_ON(err == -EMSGSIZE);
  346. nlmsg_free(rep);
  347. goto out;
  348. }
  349. err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
  350. MSG_DONTWAIT);
  351. if (err > 0)
  352. err = 0;
  353. out:
  354. if (sk)
  355. sock_gen_put(sk);
  356. return err;
  357. }
  358. EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
  359. static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
  360. const struct nlmsghdr *nlh,
  361. const struct inet_diag_req_v2 *req)
  362. {
  363. const struct inet_diag_handler *handler;
  364. int err;
  365. handler = inet_diag_lock_handler(req->sdiag_protocol);
  366. if (IS_ERR(handler))
  367. err = PTR_ERR(handler);
  368. else if (cmd == SOCK_DIAG_BY_FAMILY)
  369. err = handler->dump_one(in_skb, nlh, req);
  370. else if (cmd == SOCK_DESTROY && handler->destroy)
  371. err = handler->destroy(in_skb, req);
  372. else
  373. err = -EOPNOTSUPP;
  374. inet_diag_unlock_handler(handler);
  375. return err;
  376. }
  377. static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
  378. {
  379. int words = bits >> 5;
  380. bits &= 0x1f;
  381. if (words) {
  382. if (memcmp(a1, a2, words << 2))
  383. return 0;
  384. }
  385. if (bits) {
  386. __be32 w1, w2;
  387. __be32 mask;
  388. w1 = a1[words];
  389. w2 = a2[words];
  390. mask = htonl((0xffffffff) << (32 - bits));
  391. if ((w1 ^ w2) & mask)
  392. return 0;
  393. }
  394. return 1;
  395. }
  396. static int inet_diag_bc_run(const struct nlattr *_bc,
  397. const struct inet_diag_entry *entry)
  398. {
  399. const void *bc = nla_data(_bc);
  400. int len = nla_len(_bc);
  401. while (len > 0) {
  402. int yes = 1;
  403. const struct inet_diag_bc_op *op = bc;
  404. switch (op->code) {
  405. case INET_DIAG_BC_NOP:
  406. break;
  407. case INET_DIAG_BC_JMP:
  408. yes = 0;
  409. break;
  410. case INET_DIAG_BC_S_GE:
  411. yes = entry->sport >= op[1].no;
  412. break;
  413. case INET_DIAG_BC_S_LE:
  414. yes = entry->sport <= op[1].no;
  415. break;
  416. case INET_DIAG_BC_D_GE:
  417. yes = entry->dport >= op[1].no;
  418. break;
  419. case INET_DIAG_BC_D_LE:
  420. yes = entry->dport <= op[1].no;
  421. break;
  422. case INET_DIAG_BC_AUTO:
  423. yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
  424. break;
  425. case INET_DIAG_BC_S_COND:
  426. case INET_DIAG_BC_D_COND: {
  427. const struct inet_diag_hostcond *cond;
  428. const __be32 *addr;
  429. cond = (const struct inet_diag_hostcond *)(op + 1);
  430. if (cond->port != -1 &&
  431. cond->port != (op->code == INET_DIAG_BC_S_COND ?
  432. entry->sport : entry->dport)) {
  433. yes = 0;
  434. break;
  435. }
  436. if (op->code == INET_DIAG_BC_S_COND)
  437. addr = entry->saddr;
  438. else
  439. addr = entry->daddr;
  440. if (cond->family != AF_UNSPEC &&
  441. cond->family != entry->family) {
  442. if (entry->family == AF_INET6 &&
  443. cond->family == AF_INET) {
  444. if (addr[0] == 0 && addr[1] == 0 &&
  445. addr[2] == htonl(0xffff) &&
  446. bitstring_match(addr + 3,
  447. cond->addr,
  448. cond->prefix_len))
  449. break;
  450. }
  451. yes = 0;
  452. break;
  453. }
  454. if (cond->prefix_len == 0)
  455. break;
  456. if (bitstring_match(addr, cond->addr,
  457. cond->prefix_len))
  458. break;
  459. yes = 0;
  460. break;
  461. }
  462. }
  463. if (yes) {
  464. len -= op->yes;
  465. bc += op->yes;
  466. } else {
  467. len -= op->no;
  468. bc += op->no;
  469. }
  470. }
  471. return len == 0;
  472. }
  473. /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
  474. */
  475. static void entry_fill_addrs(struct inet_diag_entry *entry,
  476. const struct sock *sk)
  477. {
  478. #if IS_ENABLED(CONFIG_IPV6)
  479. if (sk->sk_family == AF_INET6) {
  480. entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
  481. entry->daddr = sk->sk_v6_daddr.s6_addr32;
  482. } else
  483. #endif
  484. {
  485. entry->saddr = &sk->sk_rcv_saddr;
  486. entry->daddr = &sk->sk_daddr;
  487. }
  488. }
  489. int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
  490. {
  491. struct inet_sock *inet = inet_sk(sk);
  492. struct inet_diag_entry entry;
  493. if (!bc)
  494. return 1;
  495. entry.family = sk->sk_family;
  496. entry_fill_addrs(&entry, sk);
  497. entry.sport = inet->inet_num;
  498. entry.dport = ntohs(inet->inet_dport);
  499. entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
  500. return inet_diag_bc_run(bc, &entry);
  501. }
  502. EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
  503. static int valid_cc(const void *bc, int len, int cc)
  504. {
  505. while (len >= 0) {
  506. const struct inet_diag_bc_op *op = bc;
  507. if (cc > len)
  508. return 0;
  509. if (cc == len)
  510. return 1;
  511. if (op->yes < 4 || op->yes & 3)
  512. return 0;
  513. len -= op->yes;
  514. bc += op->yes;
  515. }
  516. return 0;
  517. }
  518. /* Validate an inet_diag_hostcond. */
  519. static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
  520. int *min_len)
  521. {
  522. struct inet_diag_hostcond *cond;
  523. int addr_len;
  524. /* Check hostcond space. */
  525. *min_len += sizeof(struct inet_diag_hostcond);
  526. if (len < *min_len)
  527. return false;
  528. cond = (struct inet_diag_hostcond *)(op + 1);
  529. /* Check address family and address length. */
  530. switch (cond->family) {
  531. case AF_UNSPEC:
  532. addr_len = 0;
  533. break;
  534. case AF_INET:
  535. addr_len = sizeof(struct in_addr);
  536. break;
  537. case AF_INET6:
  538. addr_len = sizeof(struct in6_addr);
  539. break;
  540. default:
  541. return false;
  542. }
  543. *min_len += addr_len;
  544. if (len < *min_len)
  545. return false;
  546. /* Check prefix length (in bits) vs address length (in bytes). */
  547. if (cond->prefix_len > 8 * addr_len)
  548. return false;
  549. return true;
  550. }
  551. /* Validate a port comparison operator. */
  552. static bool valid_port_comparison(const struct inet_diag_bc_op *op,
  553. int len, int *min_len)
  554. {
  555. /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
  556. *min_len += sizeof(struct inet_diag_bc_op);
  557. if (len < *min_len)
  558. return false;
  559. return true;
  560. }
  561. static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
  562. {
  563. const void *bc = bytecode;
  564. int len = bytecode_len;
  565. while (len > 0) {
  566. int min_len = sizeof(struct inet_diag_bc_op);
  567. const struct inet_diag_bc_op *op = bc;
  568. switch (op->code) {
  569. case INET_DIAG_BC_S_COND:
  570. case INET_DIAG_BC_D_COND:
  571. if (!valid_hostcond(bc, len, &min_len))
  572. return -EINVAL;
  573. break;
  574. case INET_DIAG_BC_S_GE:
  575. case INET_DIAG_BC_S_LE:
  576. case INET_DIAG_BC_D_GE:
  577. case INET_DIAG_BC_D_LE:
  578. if (!valid_port_comparison(bc, len, &min_len))
  579. return -EINVAL;
  580. break;
  581. case INET_DIAG_BC_AUTO:
  582. case INET_DIAG_BC_JMP:
  583. case INET_DIAG_BC_NOP:
  584. break;
  585. default:
  586. return -EINVAL;
  587. }
  588. if (op->code != INET_DIAG_BC_NOP) {
  589. if (op->no < min_len || op->no > len + 4 || op->no & 3)
  590. return -EINVAL;
  591. if (op->no < len &&
  592. !valid_cc(bytecode, bytecode_len, len - op->no))
  593. return -EINVAL;
  594. }
  595. if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
  596. return -EINVAL;
  597. bc += op->yes;
  598. len -= op->yes;
  599. }
  600. return len == 0 ? 0 : -EINVAL;
  601. }
  602. static int inet_csk_diag_dump(struct sock *sk,
  603. struct sk_buff *skb,
  604. struct netlink_callback *cb,
  605. const struct inet_diag_req_v2 *r,
  606. const struct nlattr *bc)
  607. {
  608. if (!inet_diag_bc_sk(bc, sk))
  609. return 0;
  610. return inet_csk_diag_fill(sk, skb, r,
  611. sk_user_ns(NETLINK_CB(cb->skb).sk),
  612. NETLINK_CB(cb->skb).portid,
  613. cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
  614. }
  615. static void twsk_build_assert(void)
  616. {
  617. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
  618. offsetof(struct sock, sk_family));
  619. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
  620. offsetof(struct inet_sock, inet_num));
  621. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
  622. offsetof(struct inet_sock, inet_dport));
  623. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
  624. offsetof(struct inet_sock, inet_rcv_saddr));
  625. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
  626. offsetof(struct inet_sock, inet_daddr));
  627. #if IS_ENABLED(CONFIG_IPV6)
  628. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
  629. offsetof(struct sock, sk_v6_rcv_saddr));
  630. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
  631. offsetof(struct sock, sk_v6_daddr));
  632. #endif
  633. }
  634. void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
  635. struct netlink_callback *cb,
  636. const struct inet_diag_req_v2 *r, struct nlattr *bc)
  637. {
  638. struct net *net = sock_net(skb->sk);
  639. int i, num, s_i, s_num;
  640. u32 idiag_states = r->idiag_states;
  641. if (idiag_states & TCPF_SYN_RECV)
  642. idiag_states |= TCPF_NEW_SYN_RECV;
  643. s_i = cb->args[1];
  644. s_num = num = cb->args[2];
  645. if (cb->args[0] == 0) {
  646. if (!(idiag_states & TCPF_LISTEN))
  647. goto skip_listen_ht;
  648. for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
  649. struct inet_listen_hashbucket *ilb;
  650. struct hlist_nulls_node *node;
  651. struct sock *sk;
  652. num = 0;
  653. ilb = &hashinfo->listening_hash[i];
  654. spin_lock_bh(&ilb->lock);
  655. sk_nulls_for_each(sk, node, &ilb->head) {
  656. struct inet_sock *inet = inet_sk(sk);
  657. if (!net_eq(sock_net(sk), net))
  658. continue;
  659. if (num < s_num) {
  660. num++;
  661. continue;
  662. }
  663. if (r->sdiag_family != AF_UNSPEC &&
  664. sk->sk_family != r->sdiag_family)
  665. goto next_listen;
  666. if (r->id.idiag_sport != inet->inet_sport &&
  667. r->id.idiag_sport)
  668. goto next_listen;
  669. if (r->id.idiag_dport ||
  670. cb->args[3] > 0)
  671. goto next_listen;
  672. if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
  673. spin_unlock_bh(&ilb->lock);
  674. goto done;
  675. }
  676. next_listen:
  677. cb->args[3] = 0;
  678. cb->args[4] = 0;
  679. ++num;
  680. }
  681. spin_unlock_bh(&ilb->lock);
  682. s_num = 0;
  683. cb->args[3] = 0;
  684. cb->args[4] = 0;
  685. }
  686. skip_listen_ht:
  687. cb->args[0] = 1;
  688. s_i = num = s_num = 0;
  689. }
  690. if (!(idiag_states & ~TCPF_LISTEN))
  691. goto out;
  692. for (i = s_i; i <= hashinfo->ehash_mask; i++) {
  693. struct inet_ehash_bucket *head = &hashinfo->ehash[i];
  694. spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
  695. struct hlist_nulls_node *node;
  696. struct sock *sk;
  697. num = 0;
  698. if (hlist_nulls_empty(&head->chain))
  699. continue;
  700. if (i > s_i)
  701. s_num = 0;
  702. spin_lock_bh(lock);
  703. sk_nulls_for_each(sk, node, &head->chain) {
  704. int state, res;
  705. if (!net_eq(sock_net(sk), net))
  706. continue;
  707. if (num < s_num)
  708. goto next_normal;
  709. state = (sk->sk_state == TCP_TIME_WAIT) ?
  710. inet_twsk(sk)->tw_substate : sk->sk_state;
  711. if (!(idiag_states & (1 << state)))
  712. goto next_normal;
  713. if (r->sdiag_family != AF_UNSPEC &&
  714. sk->sk_family != r->sdiag_family)
  715. goto next_normal;
  716. if (r->id.idiag_sport != htons(sk->sk_num) &&
  717. r->id.idiag_sport)
  718. goto next_normal;
  719. if (r->id.idiag_dport != sk->sk_dport &&
  720. r->id.idiag_dport)
  721. goto next_normal;
  722. twsk_build_assert();
  723. if (!inet_diag_bc_sk(bc, sk))
  724. goto next_normal;
  725. res = sk_diag_fill(sk, skb, r,
  726. sk_user_ns(NETLINK_CB(cb->skb).sk),
  727. NETLINK_CB(cb->skb).portid,
  728. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  729. cb->nlh);
  730. if (res < 0) {
  731. spin_unlock_bh(lock);
  732. goto done;
  733. }
  734. next_normal:
  735. ++num;
  736. }
  737. spin_unlock_bh(lock);
  738. }
  739. done:
  740. cb->args[1] = i;
  741. cb->args[2] = num;
  742. out:
  743. ;
  744. }
  745. EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
  746. static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  747. const struct inet_diag_req_v2 *r,
  748. struct nlattr *bc)
  749. {
  750. const struct inet_diag_handler *handler;
  751. int err = 0;
  752. handler = inet_diag_lock_handler(r->sdiag_protocol);
  753. if (!IS_ERR(handler))
  754. handler->dump(skb, cb, r, bc);
  755. else
  756. err = PTR_ERR(handler);
  757. inet_diag_unlock_handler(handler);
  758. return err ? : skb->len;
  759. }
  760. static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  761. {
  762. int hdrlen = sizeof(struct inet_diag_req_v2);
  763. struct nlattr *bc = NULL;
  764. if (nlmsg_attrlen(cb->nlh, hdrlen))
  765. bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
  766. return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
  767. }
  768. static int inet_diag_type2proto(int type)
  769. {
  770. switch (type) {
  771. case TCPDIAG_GETSOCK:
  772. return IPPROTO_TCP;
  773. case DCCPDIAG_GETSOCK:
  774. return IPPROTO_DCCP;
  775. default:
  776. return 0;
  777. }
  778. }
  779. static int inet_diag_dump_compat(struct sk_buff *skb,
  780. struct netlink_callback *cb)
  781. {
  782. struct inet_diag_req *rc = nlmsg_data(cb->nlh);
  783. int hdrlen = sizeof(struct inet_diag_req);
  784. struct inet_diag_req_v2 req;
  785. struct nlattr *bc = NULL;
  786. req.sdiag_family = AF_UNSPEC; /* compatibility */
  787. req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
  788. req.idiag_ext = rc->idiag_ext;
  789. req.idiag_states = rc->idiag_states;
  790. req.id = rc->id;
  791. if (nlmsg_attrlen(cb->nlh, hdrlen))
  792. bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
  793. return __inet_diag_dump(skb, cb, &req, bc);
  794. }
  795. static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
  796. const struct nlmsghdr *nlh)
  797. {
  798. struct inet_diag_req *rc = nlmsg_data(nlh);
  799. struct inet_diag_req_v2 req;
  800. req.sdiag_family = rc->idiag_family;
  801. req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
  802. req.idiag_ext = rc->idiag_ext;
  803. req.idiag_states = rc->idiag_states;
  804. req.id = rc->id;
  805. return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req);
  806. }
  807. static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
  808. {
  809. int hdrlen = sizeof(struct inet_diag_req);
  810. struct net *net = sock_net(skb->sk);
  811. if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
  812. nlmsg_len(nlh) < hdrlen)
  813. return -EINVAL;
  814. if (nlh->nlmsg_flags & NLM_F_DUMP) {
  815. if (nlmsg_attrlen(nlh, hdrlen)) {
  816. struct nlattr *attr;
  817. attr = nlmsg_find_attr(nlh, hdrlen,
  818. INET_DIAG_REQ_BYTECODE);
  819. if (!attr ||
  820. nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
  821. inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
  822. return -EINVAL;
  823. }
  824. {
  825. struct netlink_dump_control c = {
  826. .dump = inet_diag_dump_compat,
  827. };
  828. return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
  829. }
  830. }
  831. return inet_diag_get_exact_compat(skb, nlh);
  832. }
  833. static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
  834. {
  835. int hdrlen = sizeof(struct inet_diag_req_v2);
  836. struct net *net = sock_net(skb->sk);
  837. if (nlmsg_len(h) < hdrlen)
  838. return -EINVAL;
  839. if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
  840. h->nlmsg_flags & NLM_F_DUMP) {
  841. if (nlmsg_attrlen(h, hdrlen)) {
  842. struct nlattr *attr;
  843. attr = nlmsg_find_attr(h, hdrlen,
  844. INET_DIAG_REQ_BYTECODE);
  845. if (!attr ||
  846. nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
  847. inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
  848. return -EINVAL;
  849. }
  850. {
  851. struct netlink_dump_control c = {
  852. .dump = inet_diag_dump,
  853. };
  854. return netlink_dump_start(net->diag_nlsk, skb, h, &c);
  855. }
  856. }
  857. return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));
  858. }
  859. static
  860. int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
  861. {
  862. const struct inet_diag_handler *handler;
  863. struct nlmsghdr *nlh;
  864. struct nlattr *attr;
  865. struct inet_diag_msg *r;
  866. void *info = NULL;
  867. int err = 0;
  868. nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
  869. if (!nlh)
  870. return -ENOMEM;
  871. r = nlmsg_data(nlh);
  872. memset(r, 0, sizeof(*r));
  873. inet_diag_msg_common_fill(r, sk);
  874. if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
  875. r->id.idiag_sport = inet_sk(sk)->inet_sport;
  876. r->idiag_state = sk->sk_state;
  877. if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
  878. nlmsg_cancel(skb, nlh);
  879. return err;
  880. }
  881. handler = inet_diag_lock_handler(sk->sk_protocol);
  882. if (IS_ERR(handler)) {
  883. inet_diag_unlock_handler(handler);
  884. nlmsg_cancel(skb, nlh);
  885. return PTR_ERR(handler);
  886. }
  887. attr = handler->idiag_info_size
  888. ? nla_reserve(skb, INET_DIAG_INFO, handler->idiag_info_size)
  889. : NULL;
  890. if (attr)
  891. info = nla_data(attr);
  892. handler->idiag_get_info(sk, r, info);
  893. inet_diag_unlock_handler(handler);
  894. nlmsg_end(skb, nlh);
  895. return 0;
  896. }
  897. static const struct sock_diag_handler inet_diag_handler = {
  898. .family = AF_INET,
  899. .dump = inet_diag_handler_cmd,
  900. .get_info = inet_diag_handler_get_info,
  901. .destroy = inet_diag_handler_cmd,
  902. };
  903. static const struct sock_diag_handler inet6_diag_handler = {
  904. .family = AF_INET6,
  905. .dump = inet_diag_handler_cmd,
  906. .get_info = inet_diag_handler_get_info,
  907. .destroy = inet_diag_handler_cmd,
  908. };
  909. int inet_diag_register(const struct inet_diag_handler *h)
  910. {
  911. const __u16 type = h->idiag_type;
  912. int err = -EINVAL;
  913. if (type >= IPPROTO_MAX)
  914. goto out;
  915. mutex_lock(&inet_diag_table_mutex);
  916. err = -EEXIST;
  917. if (!inet_diag_table[type]) {
  918. inet_diag_table[type] = h;
  919. err = 0;
  920. }
  921. mutex_unlock(&inet_diag_table_mutex);
  922. out:
  923. return err;
  924. }
  925. EXPORT_SYMBOL_GPL(inet_diag_register);
  926. void inet_diag_unregister(const struct inet_diag_handler *h)
  927. {
  928. const __u16 type = h->idiag_type;
  929. if (type >= IPPROTO_MAX)
  930. return;
  931. mutex_lock(&inet_diag_table_mutex);
  932. inet_diag_table[type] = NULL;
  933. mutex_unlock(&inet_diag_table_mutex);
  934. }
  935. EXPORT_SYMBOL_GPL(inet_diag_unregister);
  936. static int __init inet_diag_init(void)
  937. {
  938. const int inet_diag_table_size = (IPPROTO_MAX *
  939. sizeof(struct inet_diag_handler *));
  940. int err = -ENOMEM;
  941. inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
  942. if (!inet_diag_table)
  943. goto out;
  944. err = sock_diag_register(&inet_diag_handler);
  945. if (err)
  946. goto out_free_nl;
  947. err = sock_diag_register(&inet6_diag_handler);
  948. if (err)
  949. goto out_free_inet;
  950. sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
  951. out:
  952. return err;
  953. out_free_inet:
  954. sock_diag_unregister(&inet_diag_handler);
  955. out_free_nl:
  956. kfree(inet_diag_table);
  957. goto out;
  958. }
  959. static void __exit inet_diag_exit(void)
  960. {
  961. sock_diag_unregister(&inet6_diag_handler);
  962. sock_diag_unregister(&inet_diag_handler);
  963. sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
  964. kfree(inet_diag_table);
  965. }
  966. module_init(inet_diag_init);
  967. module_exit(inet_diag_exit);
  968. MODULE_LICENSE("GPL");
  969. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
  970. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);