inet_diag.c 23 KB

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