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