tcp_diag.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /*
  2. * tcp_diag.c Module for monitoring TCP sockets.
  3. *
  4. * Version: $Id: tcp_diag.c,v 1.3 2002/02/01 22:01:04 davem Exp $
  5. *
  6. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/config.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/fcntl.h>
  17. #include <linux/random.h>
  18. #include <linux/cache.h>
  19. #include <linux/init.h>
  20. #include <linux/time.h>
  21. #include <net/icmp.h>
  22. #include <net/tcp.h>
  23. #include <net/ipv6.h>
  24. #include <net/inet_common.h>
  25. #include <net/inet_connection_sock.h>
  26. #include <net/inet_hashtables.h>
  27. #include <net/inet_timewait_sock.h>
  28. #include <net/inet6_hashtables.h>
  29. #include <linux/inet.h>
  30. #include <linux/stddef.h>
  31. #include <linux/tcp_diag.h>
  32. static const struct inet_diag_handler **inet_diag_table;
  33. struct tcpdiag_entry
  34. {
  35. u32 *saddr;
  36. u32 *daddr;
  37. u16 sport;
  38. u16 dport;
  39. u16 family;
  40. u16 userlocks;
  41. };
  42. static struct sock *tcpnl;
  43. #define TCPDIAG_PUT(skb, attrtype, attrlen) \
  44. RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
  45. #ifdef CONFIG_IP_TCPDIAG_DCCP
  46. extern struct inet_hashinfo dccp_hashinfo;
  47. #endif
  48. static int tcpdiag_fill(struct sk_buff *skb, struct sock *sk,
  49. int ext, u32 pid, u32 seq, u16 nlmsg_flags,
  50. const struct nlmsghdr *unlh)
  51. {
  52. const struct inet_sock *inet = inet_sk(sk);
  53. const struct inet_connection_sock *icsk = inet_csk(sk);
  54. struct tcpdiagmsg *r;
  55. struct nlmsghdr *nlh;
  56. void *info = NULL;
  57. struct tcpdiag_meminfo *minfo = NULL;
  58. unsigned char *b = skb->tail;
  59. const struct inet_diag_handler *handler;
  60. handler = inet_diag_table[unlh->nlmsg_type];
  61. BUG_ON(handler == NULL);
  62. nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
  63. nlh->nlmsg_flags = nlmsg_flags;
  64. r = NLMSG_DATA(nlh);
  65. if (sk->sk_state != TCP_TIME_WAIT) {
  66. if (ext & (1<<(TCPDIAG_MEMINFO-1)))
  67. minfo = TCPDIAG_PUT(skb, TCPDIAG_MEMINFO, sizeof(*minfo));
  68. if (ext & (1<<(TCPDIAG_INFO-1)))
  69. info = TCPDIAG_PUT(skb, TCPDIAG_INFO,
  70. handler->idiag_info_size);
  71. if ((ext & (1 << (TCPDIAG_CONG - 1))) && icsk->icsk_ca_ops) {
  72. size_t len = strlen(icsk->icsk_ca_ops->name);
  73. strcpy(TCPDIAG_PUT(skb, TCPDIAG_CONG, len+1),
  74. icsk->icsk_ca_ops->name);
  75. }
  76. }
  77. r->tcpdiag_family = sk->sk_family;
  78. r->tcpdiag_state = sk->sk_state;
  79. r->tcpdiag_timer = 0;
  80. r->tcpdiag_retrans = 0;
  81. r->id.tcpdiag_if = sk->sk_bound_dev_if;
  82. r->id.tcpdiag_cookie[0] = (u32)(unsigned long)sk;
  83. r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
  84. if (r->tcpdiag_state == TCP_TIME_WAIT) {
  85. const struct inet_timewait_sock *tw = inet_twsk(sk);
  86. long tmo = tw->tw_ttd - jiffies;
  87. if (tmo < 0)
  88. tmo = 0;
  89. r->id.tcpdiag_sport = tw->tw_sport;
  90. r->id.tcpdiag_dport = tw->tw_dport;
  91. r->id.tcpdiag_src[0] = tw->tw_rcv_saddr;
  92. r->id.tcpdiag_dst[0] = tw->tw_daddr;
  93. r->tcpdiag_state = tw->tw_substate;
  94. r->tcpdiag_timer = 3;
  95. r->tcpdiag_expires = (tmo*1000+HZ-1)/HZ;
  96. r->tcpdiag_rqueue = 0;
  97. r->tcpdiag_wqueue = 0;
  98. r->tcpdiag_uid = 0;
  99. r->tcpdiag_inode = 0;
  100. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  101. if (r->tcpdiag_family == AF_INET6) {
  102. const struct tcp6_timewait_sock *tcp6tw = tcp6_twsk(sk);
  103. ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
  104. &tcp6tw->tw_v6_rcv_saddr);
  105. ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
  106. &tcp6tw->tw_v6_daddr);
  107. }
  108. #endif
  109. nlh->nlmsg_len = skb->tail - b;
  110. return skb->len;
  111. }
  112. r->id.tcpdiag_sport = inet->sport;
  113. r->id.tcpdiag_dport = inet->dport;
  114. r->id.tcpdiag_src[0] = inet->rcv_saddr;
  115. r->id.tcpdiag_dst[0] = inet->daddr;
  116. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  117. if (r->tcpdiag_family == AF_INET6) {
  118. struct ipv6_pinfo *np = inet6_sk(sk);
  119. ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
  120. &np->rcv_saddr);
  121. ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
  122. &np->daddr);
  123. }
  124. #endif
  125. #define EXPIRES_IN_MS(tmo) ((tmo-jiffies)*1000+HZ-1)/HZ
  126. if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
  127. r->tcpdiag_timer = 1;
  128. r->tcpdiag_retrans = icsk->icsk_retransmits;
  129. r->tcpdiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
  130. } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
  131. r->tcpdiag_timer = 4;
  132. r->tcpdiag_retrans = icsk->icsk_probes_out;
  133. r->tcpdiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
  134. } else if (timer_pending(&sk->sk_timer)) {
  135. r->tcpdiag_timer = 2;
  136. r->tcpdiag_retrans = icsk->icsk_probes_out;
  137. r->tcpdiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
  138. } else {
  139. r->tcpdiag_timer = 0;
  140. r->tcpdiag_expires = 0;
  141. }
  142. #undef EXPIRES_IN_MS
  143. r->tcpdiag_uid = sock_i_uid(sk);
  144. r->tcpdiag_inode = sock_i_ino(sk);
  145. if (minfo) {
  146. minfo->tcpdiag_rmem = atomic_read(&sk->sk_rmem_alloc);
  147. minfo->tcpdiag_wmem = sk->sk_wmem_queued;
  148. minfo->tcpdiag_fmem = sk->sk_forward_alloc;
  149. minfo->tcpdiag_tmem = atomic_read(&sk->sk_wmem_alloc);
  150. }
  151. handler->idiag_get_info(sk, r, info);
  152. if (sk->sk_state < TCP_TIME_WAIT &&
  153. icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
  154. icsk->icsk_ca_ops->get_info(sk, ext, skb);
  155. nlh->nlmsg_len = skb->tail - b;
  156. return skb->len;
  157. rtattr_failure:
  158. nlmsg_failure:
  159. skb_trim(skb, b - skb->data);
  160. return -1;
  161. }
  162. static int tcpdiag_get_exact(struct sk_buff *in_skb, const struct nlmsghdr *nlh)
  163. {
  164. int err;
  165. struct sock *sk;
  166. struct tcpdiagreq *req = NLMSG_DATA(nlh);
  167. struct sk_buff *rep;
  168. struct inet_hashinfo *hashinfo;
  169. const struct inet_diag_handler *handler;
  170. handler = inet_diag_table[nlh->nlmsg_type];
  171. BUG_ON(handler == NULL);
  172. hashinfo = handler->idiag_hashinfo;
  173. if (req->tcpdiag_family == AF_INET) {
  174. sk = inet_lookup(hashinfo, req->id.tcpdiag_dst[0],
  175. req->id.tcpdiag_dport, req->id.tcpdiag_src[0],
  176. req->id.tcpdiag_sport, req->id.tcpdiag_if);
  177. }
  178. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  179. else if (req->tcpdiag_family == AF_INET6) {
  180. sk = inet6_lookup(hashinfo,
  181. (struct in6_addr*)req->id.tcpdiag_dst,
  182. req->id.tcpdiag_dport,
  183. (struct in6_addr*)req->id.tcpdiag_src,
  184. req->id.tcpdiag_sport,
  185. req->id.tcpdiag_if);
  186. }
  187. #endif
  188. else {
  189. return -EINVAL;
  190. }
  191. if (sk == NULL)
  192. return -ENOENT;
  193. err = -ESTALE;
  194. if ((req->id.tcpdiag_cookie[0] != TCPDIAG_NOCOOKIE ||
  195. req->id.tcpdiag_cookie[1] != TCPDIAG_NOCOOKIE) &&
  196. ((u32)(unsigned long)sk != req->id.tcpdiag_cookie[0] ||
  197. (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.tcpdiag_cookie[1]))
  198. goto out;
  199. err = -ENOMEM;
  200. rep = alloc_skb(NLMSG_SPACE((sizeof(struct tcpdiagmsg) +
  201. sizeof(struct tcpdiag_meminfo) +
  202. handler->idiag_info_size + 64)),
  203. GFP_KERNEL);
  204. if (!rep)
  205. goto out;
  206. if (tcpdiag_fill(rep, sk, req->tcpdiag_ext,
  207. NETLINK_CB(in_skb).pid,
  208. nlh->nlmsg_seq, 0, nlh) <= 0)
  209. BUG();
  210. err = netlink_unicast(tcpnl, rep, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
  211. if (err > 0)
  212. err = 0;
  213. out:
  214. if (sk) {
  215. if (sk->sk_state == TCP_TIME_WAIT)
  216. inet_twsk_put((struct inet_timewait_sock *)sk);
  217. else
  218. sock_put(sk);
  219. }
  220. return err;
  221. }
  222. static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
  223. {
  224. int words = bits >> 5;
  225. bits &= 0x1f;
  226. if (words) {
  227. if (memcmp(a1, a2, words << 2))
  228. return 0;
  229. }
  230. if (bits) {
  231. __u32 w1, w2;
  232. __u32 mask;
  233. w1 = a1[words];
  234. w2 = a2[words];
  235. mask = htonl((0xffffffff) << (32 - bits));
  236. if ((w1 ^ w2) & mask)
  237. return 0;
  238. }
  239. return 1;
  240. }
  241. static int tcpdiag_bc_run(const void *bc, int len,
  242. const struct tcpdiag_entry *entry)
  243. {
  244. while (len > 0) {
  245. int yes = 1;
  246. const struct tcpdiag_bc_op *op = bc;
  247. switch (op->code) {
  248. case TCPDIAG_BC_NOP:
  249. break;
  250. case TCPDIAG_BC_JMP:
  251. yes = 0;
  252. break;
  253. case TCPDIAG_BC_S_GE:
  254. yes = entry->sport >= op[1].no;
  255. break;
  256. case TCPDIAG_BC_S_LE:
  257. yes = entry->dport <= op[1].no;
  258. break;
  259. case TCPDIAG_BC_D_GE:
  260. yes = entry->dport >= op[1].no;
  261. break;
  262. case TCPDIAG_BC_D_LE:
  263. yes = entry->dport <= op[1].no;
  264. break;
  265. case TCPDIAG_BC_AUTO:
  266. yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
  267. break;
  268. case TCPDIAG_BC_S_COND:
  269. case TCPDIAG_BC_D_COND:
  270. {
  271. struct tcpdiag_hostcond *cond = (struct tcpdiag_hostcond*)(op+1);
  272. u32 *addr;
  273. if (cond->port != -1 &&
  274. cond->port != (op->code == TCPDIAG_BC_S_COND ?
  275. entry->sport : entry->dport)) {
  276. yes = 0;
  277. break;
  278. }
  279. if (cond->prefix_len == 0)
  280. break;
  281. if (op->code == TCPDIAG_BC_S_COND)
  282. addr = entry->saddr;
  283. else
  284. addr = entry->daddr;
  285. if (bitstring_match(addr, cond->addr, cond->prefix_len))
  286. break;
  287. if (entry->family == AF_INET6 &&
  288. cond->family == AF_INET) {
  289. if (addr[0] == 0 && addr[1] == 0 &&
  290. addr[2] == htonl(0xffff) &&
  291. bitstring_match(addr+3, cond->addr, cond->prefix_len))
  292. break;
  293. }
  294. yes = 0;
  295. break;
  296. }
  297. }
  298. if (yes) {
  299. len -= op->yes;
  300. bc += op->yes;
  301. } else {
  302. len -= op->no;
  303. bc += op->no;
  304. }
  305. }
  306. return (len == 0);
  307. }
  308. static int valid_cc(const void *bc, int len, int cc)
  309. {
  310. while (len >= 0) {
  311. const struct tcpdiag_bc_op *op = bc;
  312. if (cc > len)
  313. return 0;
  314. if (cc == len)
  315. return 1;
  316. if (op->yes < 4)
  317. return 0;
  318. len -= op->yes;
  319. bc += op->yes;
  320. }
  321. return 0;
  322. }
  323. static int tcpdiag_bc_audit(const void *bytecode, int bytecode_len)
  324. {
  325. const unsigned char *bc = bytecode;
  326. int len = bytecode_len;
  327. while (len > 0) {
  328. struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)bc;
  329. //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
  330. switch (op->code) {
  331. case TCPDIAG_BC_AUTO:
  332. case TCPDIAG_BC_S_COND:
  333. case TCPDIAG_BC_D_COND:
  334. case TCPDIAG_BC_S_GE:
  335. case TCPDIAG_BC_S_LE:
  336. case TCPDIAG_BC_D_GE:
  337. case TCPDIAG_BC_D_LE:
  338. if (op->yes < 4 || op->yes > len+4)
  339. return -EINVAL;
  340. case TCPDIAG_BC_JMP:
  341. if (op->no < 4 || op->no > len+4)
  342. return -EINVAL;
  343. if (op->no < len &&
  344. !valid_cc(bytecode, bytecode_len, len-op->no))
  345. return -EINVAL;
  346. break;
  347. case TCPDIAG_BC_NOP:
  348. if (op->yes < 4 || op->yes > len+4)
  349. return -EINVAL;
  350. break;
  351. default:
  352. return -EINVAL;
  353. }
  354. bc += op->yes;
  355. len -= op->yes;
  356. }
  357. return len == 0 ? 0 : -EINVAL;
  358. }
  359. static int tcpdiag_dump_sock(struct sk_buff *skb, struct sock *sk,
  360. struct netlink_callback *cb)
  361. {
  362. struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
  363. if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
  364. struct tcpdiag_entry entry;
  365. struct rtattr *bc = (struct rtattr *)(r + 1);
  366. struct inet_sock *inet = inet_sk(sk);
  367. entry.family = sk->sk_family;
  368. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  369. if (entry.family == AF_INET6) {
  370. struct ipv6_pinfo *np = inet6_sk(sk);
  371. entry.saddr = np->rcv_saddr.s6_addr32;
  372. entry.daddr = np->daddr.s6_addr32;
  373. } else
  374. #endif
  375. {
  376. entry.saddr = &inet->rcv_saddr;
  377. entry.daddr = &inet->daddr;
  378. }
  379. entry.sport = inet->num;
  380. entry.dport = ntohs(inet->dport);
  381. entry.userlocks = sk->sk_userlocks;
  382. if (!tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
  383. return 0;
  384. }
  385. return tcpdiag_fill(skb, sk, r->tcpdiag_ext, NETLINK_CB(cb->skb).pid,
  386. cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
  387. }
  388. static int tcpdiag_fill_req(struct sk_buff *skb, struct sock *sk,
  389. struct request_sock *req,
  390. u32 pid, u32 seq,
  391. const struct nlmsghdr *unlh)
  392. {
  393. const struct inet_request_sock *ireq = inet_rsk(req);
  394. struct inet_sock *inet = inet_sk(sk);
  395. unsigned char *b = skb->tail;
  396. struct tcpdiagmsg *r;
  397. struct nlmsghdr *nlh;
  398. long tmo;
  399. nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
  400. nlh->nlmsg_flags = NLM_F_MULTI;
  401. r = NLMSG_DATA(nlh);
  402. r->tcpdiag_family = sk->sk_family;
  403. r->tcpdiag_state = TCP_SYN_RECV;
  404. r->tcpdiag_timer = 1;
  405. r->tcpdiag_retrans = req->retrans;
  406. r->id.tcpdiag_if = sk->sk_bound_dev_if;
  407. r->id.tcpdiag_cookie[0] = (u32)(unsigned long)req;
  408. r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
  409. tmo = req->expires - jiffies;
  410. if (tmo < 0)
  411. tmo = 0;
  412. r->id.tcpdiag_sport = inet->sport;
  413. r->id.tcpdiag_dport = ireq->rmt_port;
  414. r->id.tcpdiag_src[0] = ireq->loc_addr;
  415. r->id.tcpdiag_dst[0] = ireq->rmt_addr;
  416. r->tcpdiag_expires = jiffies_to_msecs(tmo),
  417. r->tcpdiag_rqueue = 0;
  418. r->tcpdiag_wqueue = 0;
  419. r->tcpdiag_uid = sock_i_uid(sk);
  420. r->tcpdiag_inode = 0;
  421. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  422. if (r->tcpdiag_family == AF_INET6) {
  423. ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
  424. &tcp6_rsk(req)->loc_addr);
  425. ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
  426. &tcp6_rsk(req)->rmt_addr);
  427. }
  428. #endif
  429. nlh->nlmsg_len = skb->tail - b;
  430. return skb->len;
  431. nlmsg_failure:
  432. skb_trim(skb, b - skb->data);
  433. return -1;
  434. }
  435. static int tcpdiag_dump_reqs(struct sk_buff *skb, struct sock *sk,
  436. struct netlink_callback *cb)
  437. {
  438. struct tcpdiag_entry entry;
  439. struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
  440. struct inet_connection_sock *icsk = inet_csk(sk);
  441. struct listen_sock *lopt;
  442. struct rtattr *bc = NULL;
  443. struct inet_sock *inet = inet_sk(sk);
  444. int j, s_j;
  445. int reqnum, s_reqnum;
  446. int err = 0;
  447. s_j = cb->args[3];
  448. s_reqnum = cb->args[4];
  449. if (s_j > 0)
  450. s_j--;
  451. entry.family = sk->sk_family;
  452. read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  453. lopt = icsk->icsk_accept_queue.listen_opt;
  454. if (!lopt || !lopt->qlen)
  455. goto out;
  456. if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
  457. bc = (struct rtattr *)(r + 1);
  458. entry.sport = inet->num;
  459. entry.userlocks = sk->sk_userlocks;
  460. }
  461. for (j = s_j; j < lopt->nr_table_entries; j++) {
  462. struct request_sock *req, *head = lopt->syn_table[j];
  463. reqnum = 0;
  464. for (req = head; req; reqnum++, req = req->dl_next) {
  465. struct inet_request_sock *ireq = inet_rsk(req);
  466. if (reqnum < s_reqnum)
  467. continue;
  468. if (r->id.tcpdiag_dport != ireq->rmt_port &&
  469. r->id.tcpdiag_dport)
  470. continue;
  471. if (bc) {
  472. entry.saddr =
  473. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  474. (entry.family == AF_INET6) ?
  475. tcp6_rsk(req)->loc_addr.s6_addr32 :
  476. #endif
  477. &ireq->loc_addr;
  478. entry.daddr =
  479. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  480. (entry.family == AF_INET6) ?
  481. tcp6_rsk(req)->rmt_addr.s6_addr32 :
  482. #endif
  483. &ireq->rmt_addr;
  484. entry.dport = ntohs(ireq->rmt_port);
  485. if (!tcpdiag_bc_run(RTA_DATA(bc),
  486. RTA_PAYLOAD(bc), &entry))
  487. continue;
  488. }
  489. err = tcpdiag_fill_req(skb, sk, req,
  490. NETLINK_CB(cb->skb).pid,
  491. cb->nlh->nlmsg_seq, cb->nlh);
  492. if (err < 0) {
  493. cb->args[3] = j + 1;
  494. cb->args[4] = reqnum;
  495. goto out;
  496. }
  497. }
  498. s_reqnum = 0;
  499. }
  500. out:
  501. read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  502. return err;
  503. }
  504. static int tcpdiag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  505. {
  506. int i, num;
  507. int s_i, s_num;
  508. struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
  509. const struct inet_diag_handler *handler;
  510. struct inet_hashinfo *hashinfo;
  511. handler = inet_diag_table[cb->nlh->nlmsg_type];
  512. BUG_ON(handler == NULL);
  513. hashinfo = handler->idiag_hashinfo;
  514. s_i = cb->args[1];
  515. s_num = num = cb->args[2];
  516. if (cb->args[0] == 0) {
  517. if (!(r->tcpdiag_states&(TCPF_LISTEN|TCPF_SYN_RECV)))
  518. goto skip_listen_ht;
  519. inet_listen_lock(hashinfo);
  520. for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
  521. struct sock *sk;
  522. struct hlist_node *node;
  523. num = 0;
  524. sk_for_each(sk, node, &hashinfo->listening_hash[i]) {
  525. struct inet_sock *inet = inet_sk(sk);
  526. if (num < s_num) {
  527. num++;
  528. continue;
  529. }
  530. if (r->id.tcpdiag_sport != inet->sport &&
  531. r->id.tcpdiag_sport)
  532. goto next_listen;
  533. if (!(r->tcpdiag_states&TCPF_LISTEN) ||
  534. r->id.tcpdiag_dport ||
  535. cb->args[3] > 0)
  536. goto syn_recv;
  537. if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
  538. inet_listen_unlock(hashinfo);
  539. goto done;
  540. }
  541. syn_recv:
  542. if (!(r->tcpdiag_states&TCPF_SYN_RECV))
  543. goto next_listen;
  544. if (tcpdiag_dump_reqs(skb, sk, cb) < 0) {
  545. inet_listen_unlock(hashinfo);
  546. goto done;
  547. }
  548. next_listen:
  549. cb->args[3] = 0;
  550. cb->args[4] = 0;
  551. ++num;
  552. }
  553. s_num = 0;
  554. cb->args[3] = 0;
  555. cb->args[4] = 0;
  556. }
  557. inet_listen_unlock(hashinfo);
  558. skip_listen_ht:
  559. cb->args[0] = 1;
  560. s_i = num = s_num = 0;
  561. }
  562. if (!(r->tcpdiag_states&~(TCPF_LISTEN|TCPF_SYN_RECV)))
  563. return skb->len;
  564. for (i = s_i; i < hashinfo->ehash_size; i++) {
  565. struct inet_ehash_bucket *head = &hashinfo->ehash[i];
  566. struct sock *sk;
  567. struct hlist_node *node;
  568. if (i > s_i)
  569. s_num = 0;
  570. read_lock_bh(&head->lock);
  571. num = 0;
  572. sk_for_each(sk, node, &head->chain) {
  573. struct inet_sock *inet = inet_sk(sk);
  574. if (num < s_num)
  575. goto next_normal;
  576. if (!(r->tcpdiag_states & (1 << sk->sk_state)))
  577. goto next_normal;
  578. if (r->id.tcpdiag_sport != inet->sport &&
  579. r->id.tcpdiag_sport)
  580. goto next_normal;
  581. if (r->id.tcpdiag_dport != inet->dport && r->id.tcpdiag_dport)
  582. goto next_normal;
  583. if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
  584. read_unlock_bh(&head->lock);
  585. goto done;
  586. }
  587. next_normal:
  588. ++num;
  589. }
  590. if (r->tcpdiag_states&TCPF_TIME_WAIT) {
  591. sk_for_each(sk, node,
  592. &hashinfo->ehash[i + hashinfo->ehash_size].chain) {
  593. struct inet_sock *inet = inet_sk(sk);
  594. if (num < s_num)
  595. goto next_dying;
  596. if (r->id.tcpdiag_sport != inet->sport &&
  597. r->id.tcpdiag_sport)
  598. goto next_dying;
  599. if (r->id.tcpdiag_dport != inet->dport &&
  600. r->id.tcpdiag_dport)
  601. goto next_dying;
  602. if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
  603. read_unlock_bh(&head->lock);
  604. goto done;
  605. }
  606. next_dying:
  607. ++num;
  608. }
  609. }
  610. read_unlock_bh(&head->lock);
  611. }
  612. done:
  613. cb->args[1] = i;
  614. cb->args[2] = num;
  615. return skb->len;
  616. }
  617. static int tcpdiag_dump_done(struct netlink_callback *cb)
  618. {
  619. return 0;
  620. }
  621. static __inline__ int
  622. tcpdiag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  623. {
  624. if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
  625. return 0;
  626. if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX)
  627. goto err_inval;
  628. if (inet_diag_table[nlh->nlmsg_type] == NULL)
  629. return -ENOENT;
  630. if (NLMSG_LENGTH(sizeof(struct tcpdiagreq)) > skb->len)
  631. goto err_inval;
  632. if (nlh->nlmsg_flags&NLM_F_DUMP) {
  633. if (nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(struct tcpdiagreq))) {
  634. struct rtattr *rta = (struct rtattr*)(NLMSG_DATA(nlh) + sizeof(struct tcpdiagreq));
  635. if (rta->rta_type != TCPDIAG_REQ_BYTECODE ||
  636. rta->rta_len < 8 ||
  637. rta->rta_len > nlh->nlmsg_len - NLMSG_SPACE(sizeof(struct tcpdiagreq)))
  638. goto err_inval;
  639. if (tcpdiag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
  640. goto err_inval;
  641. }
  642. return netlink_dump_start(tcpnl, skb, nlh,
  643. tcpdiag_dump,
  644. tcpdiag_dump_done);
  645. } else {
  646. return tcpdiag_get_exact(skb, nlh);
  647. }
  648. err_inval:
  649. return -EINVAL;
  650. }
  651. static inline void tcpdiag_rcv_skb(struct sk_buff *skb)
  652. {
  653. int err;
  654. struct nlmsghdr * nlh;
  655. if (skb->len >= NLMSG_SPACE(0)) {
  656. nlh = (struct nlmsghdr *)skb->data;
  657. if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
  658. return;
  659. err = tcpdiag_rcv_msg(skb, nlh);
  660. if (err || nlh->nlmsg_flags & NLM_F_ACK)
  661. netlink_ack(skb, nlh, err);
  662. }
  663. }
  664. static void tcpdiag_rcv(struct sock *sk, int len)
  665. {
  666. struct sk_buff *skb;
  667. unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
  668. while (qlen-- && (skb = skb_dequeue(&sk->sk_receive_queue))) {
  669. tcpdiag_rcv_skb(skb);
  670. kfree_skb(skb);
  671. }
  672. }
  673. static void tcp_diag_get_info(struct sock *sk, struct tcpdiagmsg *r,
  674. void *_info)
  675. {
  676. const struct tcp_sock *tp = tcp_sk(sk);
  677. struct tcp_info *info = _info;
  678. r->tcpdiag_rqueue = tp->rcv_nxt - tp->copied_seq;
  679. r->tcpdiag_wqueue = tp->write_seq - tp->snd_una;
  680. if (info != NULL)
  681. tcp_get_info(sk, info);
  682. }
  683. static struct inet_diag_handler tcp_diag_handler = {
  684. .idiag_hashinfo = &tcp_hashinfo,
  685. .idiag_get_info = tcp_diag_get_info,
  686. .idiag_type = TCPDIAG_GETSOCK,
  687. .idiag_info_size = sizeof(struct tcp_info),
  688. };
  689. static DEFINE_SPINLOCK(inet_diag_register_lock);
  690. int inet_diag_register(const struct inet_diag_handler *h)
  691. {
  692. const __u16 type = h->idiag_type;
  693. int err = -EINVAL;
  694. if (type >= INET_DIAG_GETSOCK_MAX)
  695. goto out;
  696. spin_lock(&inet_diag_register_lock);
  697. err = -EEXIST;
  698. if (inet_diag_table[type] == NULL) {
  699. inet_diag_table[type] = h;
  700. err = 0;
  701. }
  702. spin_unlock(&inet_diag_register_lock);
  703. out:
  704. return err;
  705. }
  706. EXPORT_SYMBOL_GPL(inet_diag_register);
  707. void inet_diag_unregister(const struct inet_diag_handler *h)
  708. {
  709. const __u16 type = h->idiag_type;
  710. if (type >= INET_DIAG_GETSOCK_MAX)
  711. return;
  712. spin_lock(&inet_diag_register_lock);
  713. inet_diag_table[type] = NULL;
  714. spin_unlock(&inet_diag_register_lock);
  715. synchronize_rcu();
  716. }
  717. EXPORT_SYMBOL_GPL(inet_diag_unregister);
  718. static int __init tcpdiag_init(void)
  719. {
  720. const int inet_diag_table_size = (INET_DIAG_GETSOCK_MAX *
  721. sizeof(struct inet_diag_handler *));
  722. int err = -ENOMEM;
  723. inet_diag_table = kmalloc(inet_diag_table_size, GFP_KERNEL);
  724. if (!inet_diag_table)
  725. goto out;
  726. memset(inet_diag_table, 0, inet_diag_table_size);
  727. tcpnl = netlink_kernel_create(NETLINK_TCPDIAG, tcpdiag_rcv,
  728. THIS_MODULE);
  729. if (tcpnl == NULL)
  730. goto out_free_table;
  731. err = inet_diag_register(&tcp_diag_handler);
  732. if (err)
  733. goto out_sock_release;
  734. out:
  735. return err;
  736. out_sock_release:
  737. sock_release(tcpnl->sk_socket);
  738. out_free_table:
  739. kfree(inet_diag_table);
  740. goto out;
  741. }
  742. static void __exit tcpdiag_exit(void)
  743. {
  744. sock_release(tcpnl->sk_socket);
  745. kfree(inet_diag_table);
  746. }
  747. module_init(tcpdiag_init);
  748. module_exit(tcpdiag_exit);
  749. MODULE_LICENSE("GPL");