inet_diag.c 28 KB

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