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