inet_diag.c 35 KB

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