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