inet_diag.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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. u32 ifindex;
  42. u32 mark;
  43. };
  44. static DEFINE_MUTEX(inet_diag_table_mutex);
  45. static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
  46. {
  47. if (!inet_diag_table[proto])
  48. sock_load_diag_module(AF_INET, proto);
  49. mutex_lock(&inet_diag_table_mutex);
  50. if (!inet_diag_table[proto])
  51. return ERR_PTR(-ENOENT);
  52. return inet_diag_table[proto];
  53. }
  54. static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
  55. {
  56. mutex_unlock(&inet_diag_table_mutex);
  57. }
  58. void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
  59. {
  60. r->idiag_family = sk->sk_family;
  61. r->id.idiag_sport = htons(sk->sk_num);
  62. r->id.idiag_dport = sk->sk_dport;
  63. r->id.idiag_if = sk->sk_bound_dev_if;
  64. sock_diag_save_cookie(sk, r->id.idiag_cookie);
  65. #if IS_ENABLED(CONFIG_IPV6)
  66. if (sk->sk_family == AF_INET6) {
  67. *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
  68. *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
  69. } else
  70. #endif
  71. {
  72. memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
  73. memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
  74. r->id.idiag_src[0] = sk->sk_rcv_saddr;
  75. r->id.idiag_dst[0] = sk->sk_daddr;
  76. }
  77. }
  78. EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill);
  79. static size_t inet_sk_attr_size(struct sock *sk,
  80. const struct inet_diag_req_v2 *req,
  81. bool net_admin)
  82. {
  83. const struct inet_diag_handler *handler;
  84. size_t aux = 0;
  85. handler = inet_diag_table[req->sdiag_protocol];
  86. if (handler && handler->idiag_get_aux_size)
  87. aux = handler->idiag_get_aux_size(sk, net_admin);
  88. return nla_total_size(sizeof(struct tcp_info))
  89. + nla_total_size(sizeof(struct inet_diag_msg))
  90. + inet_diag_msg_attrs_size()
  91. + nla_total_size(sizeof(struct inet_diag_meminfo))
  92. + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
  93. + nla_total_size(TCP_CA_NAME_MAX)
  94. + nla_total_size(sizeof(struct tcpvegas_info))
  95. + aux
  96. + 64;
  97. }
  98. int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
  99. struct inet_diag_msg *r, int ext,
  100. struct user_namespace *user_ns,
  101. bool net_admin)
  102. {
  103. const struct inet_sock *inet = inet_sk(sk);
  104. if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
  105. goto errout;
  106. /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
  107. * hence this needs to be included regardless of socket family.
  108. */
  109. if (ext & (1 << (INET_DIAG_TOS - 1)))
  110. if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
  111. goto errout;
  112. #if IS_ENABLED(CONFIG_IPV6)
  113. if (r->idiag_family == AF_INET6) {
  114. if (ext & (1 << (INET_DIAG_TCLASS - 1)))
  115. if (nla_put_u8(skb, INET_DIAG_TCLASS,
  116. inet6_sk(sk)->tclass) < 0)
  117. goto errout;
  118. if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
  119. nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk)))
  120. goto errout;
  121. }
  122. #endif
  123. if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, sk->sk_mark))
  124. goto errout;
  125. if (ext & (1 << (INET_DIAG_CLASS_ID - 1)) ||
  126. ext & (1 << (INET_DIAG_TCLASS - 1))) {
  127. u32 classid = 0;
  128. #ifdef CONFIG_SOCK_CGROUP_DATA
  129. classid = sock_cgroup_classid(&sk->sk_cgrp_data);
  130. #endif
  131. /* Fallback to socket priority if class id isn't set.
  132. * Classful qdiscs use it as direct reference to class.
  133. * For cgroup2 classid is always zero.
  134. */
  135. if (!classid)
  136. classid = sk->sk_priority;
  137. if (nla_put_u32(skb, INET_DIAG_CLASS_ID, classid))
  138. goto errout;
  139. }
  140. r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
  141. r->idiag_inode = sock_i_ino(sk);
  142. return 0;
  143. errout:
  144. return 1;
  145. }
  146. EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill);
  147. int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
  148. struct sk_buff *skb, const struct inet_diag_req_v2 *req,
  149. struct user_namespace *user_ns,
  150. u32 portid, u32 seq, u16 nlmsg_flags,
  151. const struct nlmsghdr *unlh,
  152. bool net_admin)
  153. {
  154. const struct tcp_congestion_ops *ca_ops;
  155. const struct inet_diag_handler *handler;
  156. int ext = req->idiag_ext;
  157. struct inet_diag_msg *r;
  158. struct nlmsghdr *nlh;
  159. struct nlattr *attr;
  160. void *info = NULL;
  161. handler = inet_diag_table[req->sdiag_protocol];
  162. BUG_ON(!handler);
  163. nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
  164. nlmsg_flags);
  165. if (!nlh)
  166. return -EMSGSIZE;
  167. r = nlmsg_data(nlh);
  168. BUG_ON(!sk_fullsock(sk));
  169. inet_diag_msg_common_fill(r, sk);
  170. r->idiag_state = sk->sk_state;
  171. r->idiag_timer = 0;
  172. r->idiag_retrans = 0;
  173. if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin))
  174. goto errout;
  175. if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
  176. struct inet_diag_meminfo minfo = {
  177. .idiag_rmem = sk_rmem_alloc_get(sk),
  178. .idiag_wmem = sk->sk_wmem_queued,
  179. .idiag_fmem = sk->sk_forward_alloc,
  180. .idiag_tmem = sk_wmem_alloc_get(sk),
  181. };
  182. if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
  183. goto errout;
  184. }
  185. if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
  186. if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
  187. goto errout;
  188. /*
  189. * RAW sockets might have user-defined protocols assigned,
  190. * so report the one supplied on socket creation.
  191. */
  192. if (sk->sk_type == SOCK_RAW) {
  193. if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))
  194. goto errout;
  195. }
  196. if (!icsk) {
  197. handler->idiag_get_info(sk, r, NULL);
  198. goto out;
  199. }
  200. if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
  201. icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
  202. icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
  203. r->idiag_timer = 1;
  204. r->idiag_retrans = icsk->icsk_retransmits;
  205. r->idiag_expires =
  206. jiffies_to_msecs(icsk->icsk_timeout - jiffies);
  207. } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
  208. r->idiag_timer = 4;
  209. r->idiag_retrans = icsk->icsk_probes_out;
  210. r->idiag_expires =
  211. jiffies_to_msecs(icsk->icsk_timeout - jiffies);
  212. } else if (timer_pending(&sk->sk_timer)) {
  213. r->idiag_timer = 2;
  214. r->idiag_retrans = icsk->icsk_probes_out;
  215. r->idiag_expires =
  216. jiffies_to_msecs(sk->sk_timer.expires - jiffies);
  217. } else {
  218. r->idiag_timer = 0;
  219. r->idiag_expires = 0;
  220. }
  221. if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) {
  222. attr = nla_reserve_64bit(skb, INET_DIAG_INFO,
  223. handler->idiag_info_size,
  224. INET_DIAG_PAD);
  225. if (!attr)
  226. goto errout;
  227. info = nla_data(attr);
  228. }
  229. if (ext & (1 << (INET_DIAG_CONG - 1))) {
  230. int err = 0;
  231. rcu_read_lock();
  232. ca_ops = READ_ONCE(icsk->icsk_ca_ops);
  233. if (ca_ops)
  234. err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
  235. rcu_read_unlock();
  236. if (err < 0)
  237. goto errout;
  238. }
  239. handler->idiag_get_info(sk, r, info);
  240. if (ext & (1 << (INET_DIAG_INFO - 1)) && handler->idiag_get_aux)
  241. if (handler->idiag_get_aux(sk, net_admin, skb) < 0)
  242. goto errout;
  243. if (sk->sk_state < TCP_TIME_WAIT) {
  244. union tcp_cc_info info;
  245. size_t sz = 0;
  246. int attr;
  247. rcu_read_lock();
  248. ca_ops = READ_ONCE(icsk->icsk_ca_ops);
  249. if (ca_ops && ca_ops->get_info)
  250. sz = ca_ops->get_info(sk, ext, &attr, &info);
  251. rcu_read_unlock();
  252. if (sz && nla_put(skb, attr, sz, &info) < 0)
  253. goto errout;
  254. }
  255. out:
  256. nlmsg_end(skb, nlh);
  257. return 0;
  258. errout:
  259. nlmsg_cancel(skb, nlh);
  260. return -EMSGSIZE;
  261. }
  262. EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
  263. static int inet_csk_diag_fill(struct sock *sk,
  264. struct sk_buff *skb,
  265. const struct inet_diag_req_v2 *req,
  266. struct user_namespace *user_ns,
  267. u32 portid, u32 seq, u16 nlmsg_flags,
  268. const struct nlmsghdr *unlh,
  269. bool net_admin)
  270. {
  271. return inet_sk_diag_fill(sk, inet_csk(sk), skb, req, user_ns,
  272. portid, seq, nlmsg_flags, unlh, net_admin);
  273. }
  274. static int inet_twsk_diag_fill(struct sock *sk,
  275. struct sk_buff *skb,
  276. u32 portid, u32 seq, u16 nlmsg_flags,
  277. const struct nlmsghdr *unlh)
  278. {
  279. struct inet_timewait_sock *tw = inet_twsk(sk);
  280. struct inet_diag_msg *r;
  281. struct nlmsghdr *nlh;
  282. long tmo;
  283. nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
  284. nlmsg_flags);
  285. if (!nlh)
  286. return -EMSGSIZE;
  287. r = nlmsg_data(nlh);
  288. BUG_ON(tw->tw_state != TCP_TIME_WAIT);
  289. tmo = tw->tw_timer.expires - jiffies;
  290. if (tmo < 0)
  291. tmo = 0;
  292. inet_diag_msg_common_fill(r, sk);
  293. r->idiag_retrans = 0;
  294. r->idiag_state = tw->tw_substate;
  295. r->idiag_timer = 3;
  296. r->idiag_expires = jiffies_to_msecs(tmo);
  297. r->idiag_rqueue = 0;
  298. r->idiag_wqueue = 0;
  299. r->idiag_uid = 0;
  300. r->idiag_inode = 0;
  301. nlmsg_end(skb, nlh);
  302. return 0;
  303. }
  304. static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
  305. u32 portid, u32 seq, u16 nlmsg_flags,
  306. const struct nlmsghdr *unlh, bool net_admin)
  307. {
  308. struct request_sock *reqsk = inet_reqsk(sk);
  309. struct inet_diag_msg *r;
  310. struct nlmsghdr *nlh;
  311. long tmo;
  312. nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
  313. nlmsg_flags);
  314. if (!nlh)
  315. return -EMSGSIZE;
  316. r = nlmsg_data(nlh);
  317. inet_diag_msg_common_fill(r, sk);
  318. r->idiag_state = TCP_SYN_RECV;
  319. r->idiag_timer = 1;
  320. r->idiag_retrans = reqsk->num_retrans;
  321. BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
  322. offsetof(struct sock, sk_cookie));
  323. tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
  324. r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0;
  325. r->idiag_rqueue = 0;
  326. r->idiag_wqueue = 0;
  327. r->idiag_uid = 0;
  328. r->idiag_inode = 0;
  329. if (net_admin && nla_put_u32(skb, INET_DIAG_MARK,
  330. inet_rsk(reqsk)->ir_mark)) {
  331. nlmsg_cancel(skb, nlh);
  332. return -EMSGSIZE;
  333. }
  334. nlmsg_end(skb, nlh);
  335. return 0;
  336. }
  337. static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
  338. const struct inet_diag_req_v2 *r,
  339. struct user_namespace *user_ns,
  340. u32 portid, u32 seq, u16 nlmsg_flags,
  341. const struct nlmsghdr *unlh, bool net_admin)
  342. {
  343. if (sk->sk_state == TCP_TIME_WAIT)
  344. return inet_twsk_diag_fill(sk, skb, portid, seq,
  345. nlmsg_flags, unlh);
  346. if (sk->sk_state == TCP_NEW_SYN_RECV)
  347. return inet_req_diag_fill(sk, skb, portid, seq,
  348. nlmsg_flags, unlh, net_admin);
  349. return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
  350. nlmsg_flags, unlh, net_admin);
  351. }
  352. struct sock *inet_diag_find_one_icsk(struct net *net,
  353. struct inet_hashinfo *hashinfo,
  354. const struct inet_diag_req_v2 *req)
  355. {
  356. struct sock *sk;
  357. rcu_read_lock();
  358. if (req->sdiag_family == AF_INET)
  359. sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0],
  360. req->id.idiag_dport, req->id.idiag_src[0],
  361. req->id.idiag_sport, req->id.idiag_if);
  362. #if IS_ENABLED(CONFIG_IPV6)
  363. else if (req->sdiag_family == AF_INET6) {
  364. if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
  365. ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
  366. sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3],
  367. req->id.idiag_dport, req->id.idiag_src[3],
  368. req->id.idiag_sport, req->id.idiag_if);
  369. else
  370. sk = inet6_lookup(net, hashinfo, NULL, 0,
  371. (struct in6_addr *)req->id.idiag_dst,
  372. req->id.idiag_dport,
  373. (struct in6_addr *)req->id.idiag_src,
  374. req->id.idiag_sport,
  375. req->id.idiag_if);
  376. }
  377. #endif
  378. else {
  379. rcu_read_unlock();
  380. return ERR_PTR(-EINVAL);
  381. }
  382. rcu_read_unlock();
  383. if (!sk)
  384. return ERR_PTR(-ENOENT);
  385. if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
  386. sock_gen_put(sk);
  387. return ERR_PTR(-ENOENT);
  388. }
  389. return sk;
  390. }
  391. EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
  392. int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
  393. struct sk_buff *in_skb,
  394. const struct nlmsghdr *nlh,
  395. const struct inet_diag_req_v2 *req)
  396. {
  397. bool net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN);
  398. struct net *net = sock_net(in_skb->sk);
  399. struct sk_buff *rep;
  400. struct sock *sk;
  401. int err;
  402. sk = inet_diag_find_one_icsk(net, hashinfo, req);
  403. if (IS_ERR(sk))
  404. return PTR_ERR(sk);
  405. rep = nlmsg_new(inet_sk_attr_size(sk, req, net_admin), GFP_KERNEL);
  406. if (!rep) {
  407. err = -ENOMEM;
  408. goto out;
  409. }
  410. err = sk_diag_fill(sk, rep, req,
  411. sk_user_ns(NETLINK_CB(in_skb).sk),
  412. NETLINK_CB(in_skb).portid,
  413. nlh->nlmsg_seq, 0, nlh, net_admin);
  414. if (err < 0) {
  415. WARN_ON(err == -EMSGSIZE);
  416. nlmsg_free(rep);
  417. goto out;
  418. }
  419. err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
  420. MSG_DONTWAIT);
  421. if (err > 0)
  422. err = 0;
  423. out:
  424. if (sk)
  425. sock_gen_put(sk);
  426. return err;
  427. }
  428. EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
  429. static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
  430. const struct nlmsghdr *nlh,
  431. const struct inet_diag_req_v2 *req)
  432. {
  433. const struct inet_diag_handler *handler;
  434. int err;
  435. handler = inet_diag_lock_handler(req->sdiag_protocol);
  436. if (IS_ERR(handler))
  437. err = PTR_ERR(handler);
  438. else if (cmd == SOCK_DIAG_BY_FAMILY)
  439. err = handler->dump_one(in_skb, nlh, req);
  440. else if (cmd == SOCK_DESTROY && handler->destroy)
  441. err = handler->destroy(in_skb, req);
  442. else
  443. err = -EOPNOTSUPP;
  444. inet_diag_unlock_handler(handler);
  445. return err;
  446. }
  447. static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
  448. {
  449. int words = bits >> 5;
  450. bits &= 0x1f;
  451. if (words) {
  452. if (memcmp(a1, a2, words << 2))
  453. return 0;
  454. }
  455. if (bits) {
  456. __be32 w1, w2;
  457. __be32 mask;
  458. w1 = a1[words];
  459. w2 = a2[words];
  460. mask = htonl((0xffffffff) << (32 - bits));
  461. if ((w1 ^ w2) & mask)
  462. return 0;
  463. }
  464. return 1;
  465. }
  466. static int inet_diag_bc_run(const struct nlattr *_bc,
  467. const struct inet_diag_entry *entry)
  468. {
  469. const void *bc = nla_data(_bc);
  470. int len = nla_len(_bc);
  471. while (len > 0) {
  472. int yes = 1;
  473. const struct inet_diag_bc_op *op = bc;
  474. switch (op->code) {
  475. case INET_DIAG_BC_NOP:
  476. break;
  477. case INET_DIAG_BC_JMP:
  478. yes = 0;
  479. break;
  480. case INET_DIAG_BC_S_EQ:
  481. yes = entry->sport == op[1].no;
  482. break;
  483. case INET_DIAG_BC_S_GE:
  484. yes = entry->sport >= op[1].no;
  485. break;
  486. case INET_DIAG_BC_S_LE:
  487. yes = entry->sport <= op[1].no;
  488. break;
  489. case INET_DIAG_BC_D_EQ:
  490. yes = entry->dport == op[1].no;
  491. break;
  492. case INET_DIAG_BC_D_GE:
  493. yes = entry->dport >= op[1].no;
  494. break;
  495. case INET_DIAG_BC_D_LE:
  496. yes = entry->dport <= op[1].no;
  497. break;
  498. case INET_DIAG_BC_AUTO:
  499. yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
  500. break;
  501. case INET_DIAG_BC_S_COND:
  502. case INET_DIAG_BC_D_COND: {
  503. const struct inet_diag_hostcond *cond;
  504. const __be32 *addr;
  505. cond = (const struct inet_diag_hostcond *)(op + 1);
  506. if (cond->port != -1 &&
  507. cond->port != (op->code == INET_DIAG_BC_S_COND ?
  508. entry->sport : entry->dport)) {
  509. yes = 0;
  510. break;
  511. }
  512. if (op->code == INET_DIAG_BC_S_COND)
  513. addr = entry->saddr;
  514. else
  515. addr = entry->daddr;
  516. if (cond->family != AF_UNSPEC &&
  517. cond->family != entry->family) {
  518. if (entry->family == AF_INET6 &&
  519. cond->family == AF_INET) {
  520. if (addr[0] == 0 && addr[1] == 0 &&
  521. addr[2] == htonl(0xffff) &&
  522. bitstring_match(addr + 3,
  523. cond->addr,
  524. cond->prefix_len))
  525. break;
  526. }
  527. yes = 0;
  528. break;
  529. }
  530. if (cond->prefix_len == 0)
  531. break;
  532. if (bitstring_match(addr, cond->addr,
  533. cond->prefix_len))
  534. break;
  535. yes = 0;
  536. break;
  537. }
  538. case INET_DIAG_BC_DEV_COND: {
  539. u32 ifindex;
  540. ifindex = *((const u32 *)(op + 1));
  541. if (ifindex != entry->ifindex)
  542. yes = 0;
  543. break;
  544. }
  545. case INET_DIAG_BC_MARK_COND: {
  546. struct inet_diag_markcond *cond;
  547. cond = (struct inet_diag_markcond *)(op + 1);
  548. if ((entry->mark & cond->mask) != cond->mark)
  549. yes = 0;
  550. break;
  551. }
  552. }
  553. if (yes) {
  554. len -= op->yes;
  555. bc += op->yes;
  556. } else {
  557. len -= op->no;
  558. bc += op->no;
  559. }
  560. }
  561. return len == 0;
  562. }
  563. /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
  564. */
  565. static void entry_fill_addrs(struct inet_diag_entry *entry,
  566. const struct sock *sk)
  567. {
  568. #if IS_ENABLED(CONFIG_IPV6)
  569. if (sk->sk_family == AF_INET6) {
  570. entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
  571. entry->daddr = sk->sk_v6_daddr.s6_addr32;
  572. } else
  573. #endif
  574. {
  575. entry->saddr = &sk->sk_rcv_saddr;
  576. entry->daddr = &sk->sk_daddr;
  577. }
  578. }
  579. int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
  580. {
  581. struct inet_sock *inet = inet_sk(sk);
  582. struct inet_diag_entry entry;
  583. if (!bc)
  584. return 1;
  585. entry.family = sk->sk_family;
  586. entry_fill_addrs(&entry, sk);
  587. entry.sport = inet->inet_num;
  588. entry.dport = ntohs(inet->inet_dport);
  589. entry.ifindex = sk->sk_bound_dev_if;
  590. entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
  591. if (sk_fullsock(sk))
  592. entry.mark = sk->sk_mark;
  593. else if (sk->sk_state == TCP_NEW_SYN_RECV)
  594. entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark;
  595. else
  596. entry.mark = 0;
  597. return inet_diag_bc_run(bc, &entry);
  598. }
  599. EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
  600. static int valid_cc(const void *bc, int len, int cc)
  601. {
  602. while (len >= 0) {
  603. const struct inet_diag_bc_op *op = bc;
  604. if (cc > len)
  605. return 0;
  606. if (cc == len)
  607. return 1;
  608. if (op->yes < 4 || op->yes & 3)
  609. return 0;
  610. len -= op->yes;
  611. bc += op->yes;
  612. }
  613. return 0;
  614. }
  615. /* data is u32 ifindex */
  616. static bool valid_devcond(const struct inet_diag_bc_op *op, int len,
  617. int *min_len)
  618. {
  619. /* Check ifindex space. */
  620. *min_len += sizeof(u32);
  621. if (len < *min_len)
  622. return false;
  623. return true;
  624. }
  625. /* Validate an inet_diag_hostcond. */
  626. static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
  627. int *min_len)
  628. {
  629. struct inet_diag_hostcond *cond;
  630. int addr_len;
  631. /* Check hostcond space. */
  632. *min_len += sizeof(struct inet_diag_hostcond);
  633. if (len < *min_len)
  634. return false;
  635. cond = (struct inet_diag_hostcond *)(op + 1);
  636. /* Check address family and address length. */
  637. switch (cond->family) {
  638. case AF_UNSPEC:
  639. addr_len = 0;
  640. break;
  641. case AF_INET:
  642. addr_len = sizeof(struct in_addr);
  643. break;
  644. case AF_INET6:
  645. addr_len = sizeof(struct in6_addr);
  646. break;
  647. default:
  648. return false;
  649. }
  650. *min_len += addr_len;
  651. if (len < *min_len)
  652. return false;
  653. /* Check prefix length (in bits) vs address length (in bytes). */
  654. if (cond->prefix_len > 8 * addr_len)
  655. return false;
  656. return true;
  657. }
  658. /* Validate a port comparison operator. */
  659. static bool valid_port_comparison(const struct inet_diag_bc_op *op,
  660. int len, int *min_len)
  661. {
  662. /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
  663. *min_len += sizeof(struct inet_diag_bc_op);
  664. if (len < *min_len)
  665. return false;
  666. return true;
  667. }
  668. static bool valid_markcond(const struct inet_diag_bc_op *op, int len,
  669. int *min_len)
  670. {
  671. *min_len += sizeof(struct inet_diag_markcond);
  672. return len >= *min_len;
  673. }
  674. static int inet_diag_bc_audit(const struct nlattr *attr,
  675. const struct sk_buff *skb)
  676. {
  677. bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN);
  678. const void *bytecode, *bc;
  679. int bytecode_len, len;
  680. if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op))
  681. return -EINVAL;
  682. bytecode = bc = nla_data(attr);
  683. len = bytecode_len = nla_len(attr);
  684. while (len > 0) {
  685. int min_len = sizeof(struct inet_diag_bc_op);
  686. const struct inet_diag_bc_op *op = bc;
  687. switch (op->code) {
  688. case INET_DIAG_BC_S_COND:
  689. case INET_DIAG_BC_D_COND:
  690. if (!valid_hostcond(bc, len, &min_len))
  691. return -EINVAL;
  692. break;
  693. case INET_DIAG_BC_DEV_COND:
  694. if (!valid_devcond(bc, len, &min_len))
  695. return -EINVAL;
  696. break;
  697. case INET_DIAG_BC_S_EQ:
  698. case INET_DIAG_BC_S_GE:
  699. case INET_DIAG_BC_S_LE:
  700. case INET_DIAG_BC_D_EQ:
  701. case INET_DIAG_BC_D_GE:
  702. case INET_DIAG_BC_D_LE:
  703. if (!valid_port_comparison(bc, len, &min_len))
  704. return -EINVAL;
  705. break;
  706. case INET_DIAG_BC_MARK_COND:
  707. if (!net_admin)
  708. return -EPERM;
  709. if (!valid_markcond(bc, len, &min_len))
  710. return -EINVAL;
  711. break;
  712. case INET_DIAG_BC_AUTO:
  713. case INET_DIAG_BC_JMP:
  714. case INET_DIAG_BC_NOP:
  715. break;
  716. default:
  717. return -EINVAL;
  718. }
  719. if (op->code != INET_DIAG_BC_NOP) {
  720. if (op->no < min_len || op->no > len + 4 || op->no & 3)
  721. return -EINVAL;
  722. if (op->no < len &&
  723. !valid_cc(bytecode, bytecode_len, len - op->no))
  724. return -EINVAL;
  725. }
  726. if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
  727. return -EINVAL;
  728. bc += op->yes;
  729. len -= op->yes;
  730. }
  731. return len == 0 ? 0 : -EINVAL;
  732. }
  733. static int inet_csk_diag_dump(struct sock *sk,
  734. struct sk_buff *skb,
  735. struct netlink_callback *cb,
  736. const struct inet_diag_req_v2 *r,
  737. const struct nlattr *bc,
  738. bool net_admin)
  739. {
  740. if (!inet_diag_bc_sk(bc, sk))
  741. return 0;
  742. return inet_csk_diag_fill(sk, skb, r,
  743. sk_user_ns(NETLINK_CB(cb->skb).sk),
  744. NETLINK_CB(cb->skb).portid,
  745. cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh,
  746. net_admin);
  747. }
  748. static void twsk_build_assert(void)
  749. {
  750. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
  751. offsetof(struct sock, sk_family));
  752. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
  753. offsetof(struct inet_sock, inet_num));
  754. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
  755. offsetof(struct inet_sock, inet_dport));
  756. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
  757. offsetof(struct inet_sock, inet_rcv_saddr));
  758. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
  759. offsetof(struct inet_sock, inet_daddr));
  760. #if IS_ENABLED(CONFIG_IPV6)
  761. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
  762. offsetof(struct sock, sk_v6_rcv_saddr));
  763. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
  764. offsetof(struct sock, sk_v6_daddr));
  765. #endif
  766. }
  767. void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
  768. struct netlink_callback *cb,
  769. const struct inet_diag_req_v2 *r, struct nlattr *bc)
  770. {
  771. bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
  772. struct net *net = sock_net(skb->sk);
  773. u32 idiag_states = r->idiag_states;
  774. int i, num, s_i, s_num;
  775. struct sock *sk;
  776. if (idiag_states & TCPF_SYN_RECV)
  777. idiag_states |= TCPF_NEW_SYN_RECV;
  778. s_i = cb->args[1];
  779. s_num = num = cb->args[2];
  780. if (cb->args[0] == 0) {
  781. if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport)
  782. goto skip_listen_ht;
  783. for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
  784. struct inet_listen_hashbucket *ilb;
  785. struct hlist_nulls_node *node;
  786. num = 0;
  787. ilb = &hashinfo->listening_hash[i];
  788. spin_lock(&ilb->lock);
  789. sk_nulls_for_each(sk, node, &ilb->nulls_head) {
  790. struct inet_sock *inet = inet_sk(sk);
  791. if (!net_eq(sock_net(sk), net))
  792. continue;
  793. if (num < s_num) {
  794. num++;
  795. continue;
  796. }
  797. if (r->sdiag_family != AF_UNSPEC &&
  798. sk->sk_family != r->sdiag_family)
  799. goto next_listen;
  800. if (r->id.idiag_sport != inet->inet_sport &&
  801. r->id.idiag_sport)
  802. goto next_listen;
  803. if (inet_csk_diag_dump(sk, skb, cb, r,
  804. bc, net_admin) < 0) {
  805. spin_unlock(&ilb->lock);
  806. goto done;
  807. }
  808. next_listen:
  809. ++num;
  810. }
  811. spin_unlock(&ilb->lock);
  812. s_num = 0;
  813. }
  814. skip_listen_ht:
  815. cb->args[0] = 1;
  816. s_i = num = s_num = 0;
  817. }
  818. if (!(idiag_states & ~TCPF_LISTEN))
  819. goto out;
  820. #define SKARR_SZ 16
  821. for (i = s_i; i <= hashinfo->ehash_mask; i++) {
  822. struct inet_ehash_bucket *head = &hashinfo->ehash[i];
  823. spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
  824. struct hlist_nulls_node *node;
  825. struct sock *sk_arr[SKARR_SZ];
  826. int num_arr[SKARR_SZ];
  827. int idx, accum, res;
  828. if (hlist_nulls_empty(&head->chain))
  829. continue;
  830. if (i > s_i)
  831. s_num = 0;
  832. next_chunk:
  833. num = 0;
  834. accum = 0;
  835. spin_lock_bh(lock);
  836. sk_nulls_for_each(sk, node, &head->chain) {
  837. int state;
  838. if (!net_eq(sock_net(sk), net))
  839. continue;
  840. if (num < s_num)
  841. goto next_normal;
  842. state = (sk->sk_state == TCP_TIME_WAIT) ?
  843. inet_twsk(sk)->tw_substate : sk->sk_state;
  844. if (!(idiag_states & (1 << state)))
  845. goto next_normal;
  846. if (r->sdiag_family != AF_UNSPEC &&
  847. sk->sk_family != r->sdiag_family)
  848. goto next_normal;
  849. if (r->id.idiag_sport != htons(sk->sk_num) &&
  850. r->id.idiag_sport)
  851. goto next_normal;
  852. if (r->id.idiag_dport != sk->sk_dport &&
  853. r->id.idiag_dport)
  854. goto next_normal;
  855. twsk_build_assert();
  856. if (!inet_diag_bc_sk(bc, sk))
  857. goto next_normal;
  858. if (!refcount_inc_not_zero(&sk->sk_refcnt))
  859. goto next_normal;
  860. num_arr[accum] = num;
  861. sk_arr[accum] = sk;
  862. if (++accum == SKARR_SZ)
  863. break;
  864. next_normal:
  865. ++num;
  866. }
  867. spin_unlock_bh(lock);
  868. res = 0;
  869. for (idx = 0; idx < accum; idx++) {
  870. if (res >= 0) {
  871. res = sk_diag_fill(sk_arr[idx], skb, r,
  872. sk_user_ns(NETLINK_CB(cb->skb).sk),
  873. NETLINK_CB(cb->skb).portid,
  874. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  875. cb->nlh, net_admin);
  876. if (res < 0)
  877. num = num_arr[idx];
  878. }
  879. sock_gen_put(sk_arr[idx]);
  880. }
  881. if (res < 0)
  882. break;
  883. cond_resched();
  884. if (accum == SKARR_SZ) {
  885. s_num = num + 1;
  886. goto next_chunk;
  887. }
  888. }
  889. done:
  890. cb->args[1] = i;
  891. cb->args[2] = num;
  892. out:
  893. ;
  894. }
  895. EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
  896. static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  897. const struct inet_diag_req_v2 *r,
  898. struct nlattr *bc)
  899. {
  900. const struct inet_diag_handler *handler;
  901. int err = 0;
  902. handler = inet_diag_lock_handler(r->sdiag_protocol);
  903. if (!IS_ERR(handler))
  904. handler->dump(skb, cb, r, bc);
  905. else
  906. err = PTR_ERR(handler);
  907. inet_diag_unlock_handler(handler);
  908. return err ? : skb->len;
  909. }
  910. static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  911. {
  912. int hdrlen = sizeof(struct inet_diag_req_v2);
  913. struct nlattr *bc = NULL;
  914. if (nlmsg_attrlen(cb->nlh, hdrlen))
  915. bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
  916. return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
  917. }
  918. static int inet_diag_type2proto(int type)
  919. {
  920. switch (type) {
  921. case TCPDIAG_GETSOCK:
  922. return IPPROTO_TCP;
  923. case DCCPDIAG_GETSOCK:
  924. return IPPROTO_DCCP;
  925. default:
  926. return 0;
  927. }
  928. }
  929. static int inet_diag_dump_compat(struct sk_buff *skb,
  930. struct netlink_callback *cb)
  931. {
  932. struct inet_diag_req *rc = nlmsg_data(cb->nlh);
  933. int hdrlen = sizeof(struct inet_diag_req);
  934. struct inet_diag_req_v2 req;
  935. struct nlattr *bc = NULL;
  936. req.sdiag_family = AF_UNSPEC; /* compatibility */
  937. req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
  938. req.idiag_ext = rc->idiag_ext;
  939. req.idiag_states = rc->idiag_states;
  940. req.id = rc->id;
  941. if (nlmsg_attrlen(cb->nlh, hdrlen))
  942. bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
  943. return __inet_diag_dump(skb, cb, &req, bc);
  944. }
  945. static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
  946. const struct nlmsghdr *nlh)
  947. {
  948. struct inet_diag_req *rc = nlmsg_data(nlh);
  949. struct inet_diag_req_v2 req;
  950. req.sdiag_family = rc->idiag_family;
  951. req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
  952. req.idiag_ext = rc->idiag_ext;
  953. req.idiag_states = rc->idiag_states;
  954. req.id = rc->id;
  955. return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req);
  956. }
  957. static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
  958. {
  959. int hdrlen = sizeof(struct inet_diag_req);
  960. struct net *net = sock_net(skb->sk);
  961. if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
  962. nlmsg_len(nlh) < hdrlen)
  963. return -EINVAL;
  964. if (nlh->nlmsg_flags & NLM_F_DUMP) {
  965. if (nlmsg_attrlen(nlh, hdrlen)) {
  966. struct nlattr *attr;
  967. int err;
  968. attr = nlmsg_find_attr(nlh, hdrlen,
  969. INET_DIAG_REQ_BYTECODE);
  970. err = inet_diag_bc_audit(attr, skb);
  971. if (err)
  972. return err;
  973. }
  974. {
  975. struct netlink_dump_control c = {
  976. .dump = inet_diag_dump_compat,
  977. };
  978. return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
  979. }
  980. }
  981. return inet_diag_get_exact_compat(skb, nlh);
  982. }
  983. static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
  984. {
  985. int hdrlen = sizeof(struct inet_diag_req_v2);
  986. struct net *net = sock_net(skb->sk);
  987. if (nlmsg_len(h) < hdrlen)
  988. return -EINVAL;
  989. if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
  990. h->nlmsg_flags & NLM_F_DUMP) {
  991. if (nlmsg_attrlen(h, hdrlen)) {
  992. struct nlattr *attr;
  993. int err;
  994. attr = nlmsg_find_attr(h, hdrlen,
  995. INET_DIAG_REQ_BYTECODE);
  996. err = inet_diag_bc_audit(attr, skb);
  997. if (err)
  998. return err;
  999. }
  1000. {
  1001. struct netlink_dump_control c = {
  1002. .dump = inet_diag_dump,
  1003. };
  1004. return netlink_dump_start(net->diag_nlsk, skb, h, &c);
  1005. }
  1006. }
  1007. return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));
  1008. }
  1009. static
  1010. int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
  1011. {
  1012. const struct inet_diag_handler *handler;
  1013. struct nlmsghdr *nlh;
  1014. struct nlattr *attr;
  1015. struct inet_diag_msg *r;
  1016. void *info = NULL;
  1017. int err = 0;
  1018. nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
  1019. if (!nlh)
  1020. return -ENOMEM;
  1021. r = nlmsg_data(nlh);
  1022. memset(r, 0, sizeof(*r));
  1023. inet_diag_msg_common_fill(r, sk);
  1024. if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
  1025. r->id.idiag_sport = inet_sk(sk)->inet_sport;
  1026. r->idiag_state = sk->sk_state;
  1027. if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
  1028. nlmsg_cancel(skb, nlh);
  1029. return err;
  1030. }
  1031. handler = inet_diag_lock_handler(sk->sk_protocol);
  1032. if (IS_ERR(handler)) {
  1033. inet_diag_unlock_handler(handler);
  1034. nlmsg_cancel(skb, nlh);
  1035. return PTR_ERR(handler);
  1036. }
  1037. attr = handler->idiag_info_size
  1038. ? nla_reserve_64bit(skb, INET_DIAG_INFO,
  1039. handler->idiag_info_size,
  1040. INET_DIAG_PAD)
  1041. : NULL;
  1042. if (attr)
  1043. info = nla_data(attr);
  1044. handler->idiag_get_info(sk, r, info);
  1045. inet_diag_unlock_handler(handler);
  1046. nlmsg_end(skb, nlh);
  1047. return 0;
  1048. }
  1049. static const struct sock_diag_handler inet_diag_handler = {
  1050. .family = AF_INET,
  1051. .dump = inet_diag_handler_cmd,
  1052. .get_info = inet_diag_handler_get_info,
  1053. .destroy = inet_diag_handler_cmd,
  1054. };
  1055. static const struct sock_diag_handler inet6_diag_handler = {
  1056. .family = AF_INET6,
  1057. .dump = inet_diag_handler_cmd,
  1058. .get_info = inet_diag_handler_get_info,
  1059. .destroy = inet_diag_handler_cmd,
  1060. };
  1061. int inet_diag_register(const struct inet_diag_handler *h)
  1062. {
  1063. const __u16 type = h->idiag_type;
  1064. int err = -EINVAL;
  1065. if (type >= IPPROTO_MAX)
  1066. goto out;
  1067. mutex_lock(&inet_diag_table_mutex);
  1068. err = -EEXIST;
  1069. if (!inet_diag_table[type]) {
  1070. inet_diag_table[type] = h;
  1071. err = 0;
  1072. }
  1073. mutex_unlock(&inet_diag_table_mutex);
  1074. out:
  1075. return err;
  1076. }
  1077. EXPORT_SYMBOL_GPL(inet_diag_register);
  1078. void inet_diag_unregister(const struct inet_diag_handler *h)
  1079. {
  1080. const __u16 type = h->idiag_type;
  1081. if (type >= IPPROTO_MAX)
  1082. return;
  1083. mutex_lock(&inet_diag_table_mutex);
  1084. inet_diag_table[type] = NULL;
  1085. mutex_unlock(&inet_diag_table_mutex);
  1086. }
  1087. EXPORT_SYMBOL_GPL(inet_diag_unregister);
  1088. static int __init inet_diag_init(void)
  1089. {
  1090. const int inet_diag_table_size = (IPPROTO_MAX *
  1091. sizeof(struct inet_diag_handler *));
  1092. int err = -ENOMEM;
  1093. inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
  1094. if (!inet_diag_table)
  1095. goto out;
  1096. err = sock_diag_register(&inet_diag_handler);
  1097. if (err)
  1098. goto out_free_nl;
  1099. err = sock_diag_register(&inet6_diag_handler);
  1100. if (err)
  1101. goto out_free_inet;
  1102. sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
  1103. out:
  1104. return err;
  1105. out_free_inet:
  1106. sock_diag_unregister(&inet_diag_handler);
  1107. out_free_nl:
  1108. kfree(inet_diag_table);
  1109. goto out;
  1110. }
  1111. static void __exit inet_diag_exit(void)
  1112. {
  1113. sock_diag_unregister(&inet6_diag_handler);
  1114. sock_diag_unregister(&inet_diag_handler);
  1115. sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
  1116. kfree(inet_diag_table);
  1117. }
  1118. module_init(inet_diag_init);
  1119. module_exit(inet_diag_exit);
  1120. MODULE_LICENSE("GPL");
  1121. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
  1122. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);