GNU Linux-libre 4.4.285-gnu1
[releases.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 int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
354                             struct sk_buff *in_skb,
355                             const struct nlmsghdr *nlh,
356                             const struct inet_diag_req_v2 *req)
357 {
358         struct net *net = sock_net(in_skb->sk);
359         struct sk_buff *rep;
360         struct sock *sk;
361         int err;
362
363         err = -EINVAL;
364         if (req->sdiag_family == AF_INET)
365                 sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
366                                  req->id.idiag_dport, req->id.idiag_src[0],
367                                  req->id.idiag_sport, req->id.idiag_if);
368 #if IS_ENABLED(CONFIG_IPV6)
369         else if (req->sdiag_family == AF_INET6) {
370                 if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
371                     ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
372                         sk = inet_lookup(net, hashinfo, req->id.idiag_dst[3],
373                                          req->id.idiag_dport, req->id.idiag_src[3],
374                                          req->id.idiag_sport, req->id.idiag_if);
375                 else
376                         sk = inet6_lookup(net, hashinfo,
377                                           (struct in6_addr *)req->id.idiag_dst,
378                                           req->id.idiag_dport,
379                                           (struct in6_addr *)req->id.idiag_src,
380                                           req->id.idiag_sport,
381                                           req->id.idiag_if);
382         }
383 #endif
384         else
385                 goto out_nosk;
386
387         err = -ENOENT;
388         if (!sk)
389                 goto out_nosk;
390
391         err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
392         if (err)
393                 goto out;
394
395         rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL);
396         if (!rep) {
397                 err = -ENOMEM;
398                 goto out;
399         }
400
401         err = sk_diag_fill(sk, rep, req,
402                            sk_user_ns(NETLINK_CB(in_skb).sk),
403                            NETLINK_CB(in_skb).portid,
404                            nlh->nlmsg_seq, 0, nlh);
405         if (err < 0) {
406                 WARN_ON(err == -EMSGSIZE);
407                 nlmsg_free(rep);
408                 goto out;
409         }
410         err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
411                               MSG_DONTWAIT);
412         if (err > 0)
413                 err = 0;
414
415 out:
416         if (sk)
417                 sock_gen_put(sk);
418
419 out_nosk:
420         return err;
421 }
422 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
423
424 static int inet_diag_get_exact(struct sk_buff *in_skb,
425                                const struct nlmsghdr *nlh,
426                                const struct inet_diag_req_v2 *req)
427 {
428         const struct inet_diag_handler *handler;
429         int err;
430
431         handler = inet_diag_lock_handler(req->sdiag_protocol);
432         if (IS_ERR(handler))
433                 err = PTR_ERR(handler);
434         else
435                 err = handler->dump_one(in_skb, nlh, req);
436         inet_diag_unlock_handler(handler);
437
438         return err;
439 }
440
441 static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
442 {
443         int words = bits >> 5;
444
445         bits &= 0x1f;
446
447         if (words) {
448                 if (memcmp(a1, a2, words << 2))
449                         return 0;
450         }
451         if (bits) {
452                 __be32 w1, w2;
453                 __be32 mask;
454
455                 w1 = a1[words];
456                 w2 = a2[words];
457
458                 mask = htonl((0xffffffff) << (32 - bits));
459
460                 if ((w1 ^ w2) & mask)
461                         return 0;
462         }
463
464         return 1;
465 }
466
467 static int inet_diag_bc_run(const struct nlattr *_bc,
468                             const struct inet_diag_entry *entry)
469 {
470         const void *bc = nla_data(_bc);
471         int len = nla_len(_bc);
472
473         while (len > 0) {
474                 int yes = 1;
475                 const struct inet_diag_bc_op *op = bc;
476
477                 switch (op->code) {
478                 case INET_DIAG_BC_NOP:
479                         break;
480                 case INET_DIAG_BC_JMP:
481                         yes = 0;
482                         break;
483                 case INET_DIAG_BC_S_GE:
484                         yes = entry->sport >= op[1].no;
485                         break;
486                 case INET_DIAG_BC_S_LE:
487                         yes = entry->sport <= op[1].no;
488                         break;
489                 case INET_DIAG_BC_D_GE:
490                         yes = entry->dport >= op[1].no;
491                         break;
492                 case INET_DIAG_BC_D_LE:
493                         yes = entry->dport <= op[1].no;
494                         break;
495                 case INET_DIAG_BC_AUTO:
496                         yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
497                         break;
498                 case INET_DIAG_BC_S_COND:
499                 case INET_DIAG_BC_D_COND: {
500                         const struct inet_diag_hostcond *cond;
501                         const __be32 *addr;
502
503                         cond = (const struct inet_diag_hostcond *)(op + 1);
504                         if (cond->port != -1 &&
505                             cond->port != (op->code == INET_DIAG_BC_S_COND ?
506                                              entry->sport : entry->dport)) {
507                                 yes = 0;
508                                 break;
509                         }
510
511                         if (op->code == INET_DIAG_BC_S_COND)
512                                 addr = entry->saddr;
513                         else
514                                 addr = entry->daddr;
515
516                         if (cond->family != AF_UNSPEC &&
517                             cond->family != entry->family) {
518                                 if (entry->family == AF_INET6 &&
519                                     cond->family == AF_INET) {
520                                         if (addr[0] == 0 && addr[1] == 0 &&
521                                             addr[2] == htonl(0xffff) &&
522                                             bitstring_match(addr + 3,
523                                                             cond->addr,
524                                                             cond->prefix_len))
525                                                 break;
526                                 }
527                                 yes = 0;
528                                 break;
529                         }
530
531                         if (cond->prefix_len == 0)
532                                 break;
533                         if (bitstring_match(addr, cond->addr,
534                                             cond->prefix_len))
535                                 break;
536                         yes = 0;
537                         break;
538                 }
539                 }
540
541                 if (yes) {
542                         len -= op->yes;
543                         bc += op->yes;
544                 } else {
545                         len -= op->no;
546                         bc += op->no;
547                 }
548         }
549         return len == 0;
550 }
551
552 /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
553  */
554 static void entry_fill_addrs(struct inet_diag_entry *entry,
555                              const struct sock *sk)
556 {
557 #if IS_ENABLED(CONFIG_IPV6)
558         if (sk->sk_family == AF_INET6) {
559                 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
560                 entry->daddr = sk->sk_v6_daddr.s6_addr32;
561         } else
562 #endif
563         {
564                 entry->saddr = &sk->sk_rcv_saddr;
565                 entry->daddr = &sk->sk_daddr;
566         }
567 }
568
569 int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
570 {
571         struct inet_sock *inet = inet_sk(sk);
572         struct inet_diag_entry entry;
573
574         if (!bc)
575                 return 1;
576
577         entry.family = sk->sk_family;
578         entry_fill_addrs(&entry, sk);
579         entry.sport = inet->inet_num;
580         entry.dport = ntohs(inet->inet_dport);
581         entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
582
583         return inet_diag_bc_run(bc, &entry);
584 }
585 EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
586
587 static int valid_cc(const void *bc, int len, int cc)
588 {
589         while (len >= 0) {
590                 const struct inet_diag_bc_op *op = bc;
591
592                 if (cc > len)
593                         return 0;
594                 if (cc == len)
595                         return 1;
596                 if (op->yes < 4 || op->yes & 3)
597                         return 0;
598                 len -= op->yes;
599                 bc  += op->yes;
600         }
601         return 0;
602 }
603
604 /* Validate an inet_diag_hostcond. */
605 static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
606                            int *min_len)
607 {
608         struct inet_diag_hostcond *cond;
609         int addr_len;
610
611         /* Check hostcond space. */
612         *min_len += sizeof(struct inet_diag_hostcond);
613         if (len < *min_len)
614                 return false;
615         cond = (struct inet_diag_hostcond *)(op + 1);
616
617         /* Check address family and address length. */
618         switch (cond->family) {
619         case AF_UNSPEC:
620                 addr_len = 0;
621                 break;
622         case AF_INET:
623                 addr_len = sizeof(struct in_addr);
624                 break;
625         case AF_INET6:
626                 addr_len = sizeof(struct in6_addr);
627                 break;
628         default:
629                 return false;
630         }
631         *min_len += addr_len;
632         if (len < *min_len)
633                 return false;
634
635         /* Check prefix length (in bits) vs address length (in bytes). */
636         if (cond->prefix_len > 8 * addr_len)
637                 return false;
638
639         return true;
640 }
641
642 /* Validate a port comparison operator. */
643 static bool valid_port_comparison(const struct inet_diag_bc_op *op,
644                                   int len, int *min_len)
645 {
646         /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
647         *min_len += sizeof(struct inet_diag_bc_op);
648         if (len < *min_len)
649                 return false;
650         return true;
651 }
652
653 static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
654 {
655         const void *bc = bytecode;
656         int  len = bytecode_len;
657
658         while (len > 0) {
659                 int min_len = sizeof(struct inet_diag_bc_op);
660                 const struct inet_diag_bc_op *op = bc;
661
662                 switch (op->code) {
663                 case INET_DIAG_BC_S_COND:
664                 case INET_DIAG_BC_D_COND:
665                         if (!valid_hostcond(bc, len, &min_len))
666                                 return -EINVAL;
667                         break;
668                 case INET_DIAG_BC_S_GE:
669                 case INET_DIAG_BC_S_LE:
670                 case INET_DIAG_BC_D_GE:
671                 case INET_DIAG_BC_D_LE:
672                         if (!valid_port_comparison(bc, len, &min_len))
673                                 return -EINVAL;
674                         break;
675                 case INET_DIAG_BC_AUTO:
676                 case INET_DIAG_BC_JMP:
677                 case INET_DIAG_BC_NOP:
678                         break;
679                 default:
680                         return -EINVAL;
681                 }
682
683                 if (op->code != INET_DIAG_BC_NOP) {
684                         if (op->no < min_len || op->no > len + 4 || op->no & 3)
685                                 return -EINVAL;
686                         if (op->no < len &&
687                             !valid_cc(bytecode, bytecode_len, len - op->no))
688                                 return -EINVAL;
689                 }
690
691                 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
692                         return -EINVAL;
693                 bc  += op->yes;
694                 len -= op->yes;
695         }
696         return len == 0 ? 0 : -EINVAL;
697 }
698
699 static int inet_csk_diag_dump(struct sock *sk,
700                               struct sk_buff *skb,
701                               struct netlink_callback *cb,
702                               const struct inet_diag_req_v2 *r,
703                               const struct nlattr *bc)
704 {
705         if (!inet_diag_bc_sk(bc, sk))
706                 return 0;
707
708         return inet_csk_diag_fill(sk, skb, r,
709                                   sk_user_ns(NETLINK_CB(cb->skb).sk),
710                                   NETLINK_CB(cb->skb).portid,
711                                   cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
712 }
713
714 static void twsk_build_assert(void)
715 {
716         BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
717                      offsetof(struct sock, sk_family));
718
719         BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
720                      offsetof(struct inet_sock, inet_num));
721
722         BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
723                      offsetof(struct inet_sock, inet_dport));
724
725         BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
726                      offsetof(struct inet_sock, inet_rcv_saddr));
727
728         BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
729                      offsetof(struct inet_sock, inet_daddr));
730
731 #if IS_ENABLED(CONFIG_IPV6)
732         BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
733                      offsetof(struct sock, sk_v6_rcv_saddr));
734
735         BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
736                      offsetof(struct sock, sk_v6_daddr));
737 #endif
738 }
739
740 void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
741                          struct netlink_callback *cb,
742                          const struct inet_diag_req_v2 *r, struct nlattr *bc)
743 {
744         struct net *net = sock_net(skb->sk);
745         int i, num, s_i, s_num;
746         u32 idiag_states = r->idiag_states;
747
748         if (idiag_states & TCPF_SYN_RECV)
749                 idiag_states |= TCPF_NEW_SYN_RECV;
750         s_i = cb->args[1];
751         s_num = num = cb->args[2];
752
753         if (cb->args[0] == 0) {
754                 if (!(idiag_states & TCPF_LISTEN))
755                         goto skip_listen_ht;
756
757                 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
758                         struct inet_listen_hashbucket *ilb;
759                         struct hlist_nulls_node *node;
760                         struct sock *sk;
761
762                         num = 0;
763                         ilb = &hashinfo->listening_hash[i];
764                         spin_lock_bh(&ilb->lock);
765                         sk_nulls_for_each(sk, node, &ilb->head) {
766                                 struct inet_sock *inet = inet_sk(sk);
767
768                                 if (!net_eq(sock_net(sk), net))
769                                         continue;
770
771                                 if (num < s_num) {
772                                         num++;
773                                         continue;
774                                 }
775
776                                 if (r->sdiag_family != AF_UNSPEC &&
777                                     sk->sk_family != r->sdiag_family)
778                                         goto next_listen;
779
780                                 if (r->id.idiag_sport != inet->inet_sport &&
781                                     r->id.idiag_sport)
782                                         goto next_listen;
783
784                                 if (r->id.idiag_dport ||
785                                     cb->args[3] > 0)
786                                         goto next_listen;
787
788                                 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
789                                         spin_unlock_bh(&ilb->lock);
790                                         goto done;
791                                 }
792
793 next_listen:
794                                 cb->args[3] = 0;
795                                 cb->args[4] = 0;
796                                 ++num;
797                         }
798                         spin_unlock_bh(&ilb->lock);
799
800                         s_num = 0;
801                         cb->args[3] = 0;
802                         cb->args[4] = 0;
803                 }
804 skip_listen_ht:
805                 cb->args[0] = 1;
806                 s_i = num = s_num = 0;
807         }
808
809         if (!(idiag_states & ~TCPF_LISTEN))
810                 goto out;
811
812         for (i = s_i; i <= hashinfo->ehash_mask; i++) {
813                 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
814                 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
815                 struct hlist_nulls_node *node;
816                 struct sock *sk;
817
818                 num = 0;
819
820                 if (hlist_nulls_empty(&head->chain))
821                         continue;
822
823                 if (i > s_i)
824                         s_num = 0;
825
826                 spin_lock_bh(lock);
827                 sk_nulls_for_each(sk, node, &head->chain) {
828                         int state, res;
829
830                         if (!net_eq(sock_net(sk), net))
831                                 continue;
832                         if (num < s_num)
833                                 goto next_normal;
834                         state = (sk->sk_state == TCP_TIME_WAIT) ?
835                                 inet_twsk(sk)->tw_substate : sk->sk_state;
836                         if (!(idiag_states & (1 << state)))
837                                 goto next_normal;
838                         if (r->sdiag_family != AF_UNSPEC &&
839                             sk->sk_family != r->sdiag_family)
840                                 goto next_normal;
841                         if (r->id.idiag_sport != htons(sk->sk_num) &&
842                             r->id.idiag_sport)
843                                 goto next_normal;
844                         if (r->id.idiag_dport != sk->sk_dport &&
845                             r->id.idiag_dport)
846                                 goto next_normal;
847                         twsk_build_assert();
848
849                         if (!inet_diag_bc_sk(bc, sk))
850                                 goto next_normal;
851
852                         res = sk_diag_fill(sk, skb, r,
853                                            sk_user_ns(NETLINK_CB(cb->skb).sk),
854                                            NETLINK_CB(cb->skb).portid,
855                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
856                                            cb->nlh);
857                         if (res < 0) {
858                                 spin_unlock_bh(lock);
859                                 goto done;
860                         }
861 next_normal:
862                         ++num;
863                 }
864
865                 spin_unlock_bh(lock);
866         }
867
868 done:
869         cb->args[1] = i;
870         cb->args[2] = num;
871 out:
872         ;
873 }
874 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
875
876 static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
877                             const struct inet_diag_req_v2 *r,
878                             struct nlattr *bc)
879 {
880         const struct inet_diag_handler *handler;
881         int err = 0;
882
883         handler = inet_diag_lock_handler(r->sdiag_protocol);
884         if (!IS_ERR(handler))
885                 handler->dump(skb, cb, r, bc);
886         else
887                 err = PTR_ERR(handler);
888         inet_diag_unlock_handler(handler);
889
890         return err ? : skb->len;
891 }
892
893 static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
894 {
895         int hdrlen = sizeof(struct inet_diag_req_v2);
896         struct nlattr *bc = NULL;
897
898         if (nlmsg_attrlen(cb->nlh, hdrlen))
899                 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
900
901         return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
902 }
903
904 static int inet_diag_type2proto(int type)
905 {
906         switch (type) {
907         case TCPDIAG_GETSOCK:
908                 return IPPROTO_TCP;
909         case DCCPDIAG_GETSOCK:
910                 return IPPROTO_DCCP;
911         default:
912                 return 0;
913         }
914 }
915
916 static int inet_diag_dump_compat(struct sk_buff *skb,
917                                  struct netlink_callback *cb)
918 {
919         struct inet_diag_req *rc = nlmsg_data(cb->nlh);
920         int hdrlen = sizeof(struct inet_diag_req);
921         struct inet_diag_req_v2 req;
922         struct nlattr *bc = NULL;
923
924         req.sdiag_family = AF_UNSPEC; /* compatibility */
925         req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
926         req.idiag_ext = rc->idiag_ext;
927         req.idiag_states = rc->idiag_states;
928         req.id = rc->id;
929
930         if (nlmsg_attrlen(cb->nlh, hdrlen))
931                 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
932
933         return __inet_diag_dump(skb, cb, &req, bc);
934 }
935
936 static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
937                                       const struct nlmsghdr *nlh)
938 {
939         struct inet_diag_req *rc = nlmsg_data(nlh);
940         struct inet_diag_req_v2 req;
941
942         req.sdiag_family = rc->idiag_family;
943         req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
944         req.idiag_ext = rc->idiag_ext;
945         req.idiag_states = rc->idiag_states;
946         req.id = rc->id;
947
948         return inet_diag_get_exact(in_skb, nlh, &req);
949 }
950
951 static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
952 {
953         int hdrlen = sizeof(struct inet_diag_req);
954         struct net *net = sock_net(skb->sk);
955
956         if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
957             nlmsg_len(nlh) < hdrlen)
958                 return -EINVAL;
959
960         if (nlh->nlmsg_flags & NLM_F_DUMP) {
961                 if (nlmsg_attrlen(nlh, hdrlen)) {
962                         struct nlattr *attr;
963
964                         attr = nlmsg_find_attr(nlh, hdrlen,
965                                                INET_DIAG_REQ_BYTECODE);
966                         if (!attr ||
967                             nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
968                             inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
969                                 return -EINVAL;
970                 }
971                 {
972                         struct netlink_dump_control c = {
973                                 .dump = inet_diag_dump_compat,
974                         };
975                         return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
976                 }
977         }
978
979         return inet_diag_get_exact_compat(skb, nlh);
980 }
981
982 static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
983 {
984         int hdrlen = sizeof(struct inet_diag_req_v2);
985         struct net *net = sock_net(skb->sk);
986
987         if (nlmsg_len(h) < hdrlen)
988                 return -EINVAL;
989
990         if (h->nlmsg_flags & NLM_F_DUMP) {
991                 if (nlmsg_attrlen(h, hdrlen)) {
992                         struct nlattr *attr;
993
994                         attr = nlmsg_find_attr(h, hdrlen,
995                                                INET_DIAG_REQ_BYTECODE);
996                         if (!attr ||
997                             nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
998                             inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
999                                 return -EINVAL;
1000                 }
1001                 {
1002                         struct netlink_dump_control c = {
1003                                 .dump = inet_diag_dump,
1004                         };
1005                         return netlink_dump_start(net->diag_nlsk, skb, h, &c);
1006                 }
1007         }
1008
1009         return inet_diag_get_exact(skb, h, nlmsg_data(h));
1010 }
1011
1012 static
1013 int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
1014 {
1015         const struct inet_diag_handler *handler;
1016         struct nlmsghdr *nlh;
1017         struct nlattr *attr;
1018         struct inet_diag_msg *r;
1019         void *info = NULL;
1020         int err = 0;
1021
1022         nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
1023         if (!nlh)
1024                 return -ENOMEM;
1025
1026         r = nlmsg_data(nlh);
1027         memset(r, 0, sizeof(*r));
1028         inet_diag_msg_common_fill(r, sk);
1029         if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
1030                 r->id.idiag_sport = inet_sk(sk)->inet_sport;
1031         r->idiag_state = sk->sk_state;
1032
1033         if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
1034                 nlmsg_cancel(skb, nlh);
1035                 return err;
1036         }
1037
1038         handler = inet_diag_lock_handler(sk->sk_protocol);
1039         if (IS_ERR(handler)) {
1040                 inet_diag_unlock_handler(handler);
1041                 nlmsg_cancel(skb, nlh);
1042                 return PTR_ERR(handler);
1043         }
1044
1045         attr = handler->idiag_info_size
1046                 ? nla_reserve(skb, INET_DIAG_INFO, handler->idiag_info_size)
1047                 : NULL;
1048         if (attr)
1049                 info = nla_data(attr);
1050
1051         handler->idiag_get_info(sk, r, info);
1052         inet_diag_unlock_handler(handler);
1053
1054         nlmsg_end(skb, nlh);
1055         return 0;
1056 }
1057
1058 static const struct sock_diag_handler inet_diag_handler = {
1059         .family = AF_INET,
1060         .dump = inet_diag_handler_dump,
1061         .get_info = inet_diag_handler_get_info,
1062 };
1063
1064 static const struct sock_diag_handler inet6_diag_handler = {
1065         .family = AF_INET6,
1066         .dump = inet_diag_handler_dump,
1067         .get_info = inet_diag_handler_get_info,
1068 };
1069
1070 int inet_diag_register(const struct inet_diag_handler *h)
1071 {
1072         const __u16 type = h->idiag_type;
1073         int err = -EINVAL;
1074
1075         if (type >= IPPROTO_MAX)
1076                 goto out;
1077
1078         mutex_lock(&inet_diag_table_mutex);
1079         err = -EEXIST;
1080         if (!inet_diag_table[type]) {
1081                 inet_diag_table[type] = h;
1082                 err = 0;
1083         }
1084         mutex_unlock(&inet_diag_table_mutex);
1085 out:
1086         return err;
1087 }
1088 EXPORT_SYMBOL_GPL(inet_diag_register);
1089
1090 void inet_diag_unregister(const struct inet_diag_handler *h)
1091 {
1092         const __u16 type = h->idiag_type;
1093
1094         if (type >= IPPROTO_MAX)
1095                 return;
1096
1097         mutex_lock(&inet_diag_table_mutex);
1098         inet_diag_table[type] = NULL;
1099         mutex_unlock(&inet_diag_table_mutex);
1100 }
1101 EXPORT_SYMBOL_GPL(inet_diag_unregister);
1102
1103 static int __init inet_diag_init(void)
1104 {
1105         const int inet_diag_table_size = (IPPROTO_MAX *
1106                                           sizeof(struct inet_diag_handler *));
1107         int err = -ENOMEM;
1108
1109         inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1110         if (!inet_diag_table)
1111                 goto out;
1112
1113         err = sock_diag_register(&inet_diag_handler);
1114         if (err)
1115                 goto out_free_nl;
1116
1117         err = sock_diag_register(&inet6_diag_handler);
1118         if (err)
1119                 goto out_free_inet;
1120
1121         sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1122 out:
1123         return err;
1124
1125 out_free_inet:
1126         sock_diag_unregister(&inet_diag_handler);
1127 out_free_nl:
1128         kfree(inet_diag_table);
1129         goto out;
1130 }
1131
1132 static void __exit inet_diag_exit(void)
1133 {
1134         sock_diag_unregister(&inet6_diag_handler);
1135         sock_diag_unregister(&inet_diag_handler);
1136         sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1137         kfree(inet_diag_table);
1138 }
1139
1140 module_init(inet_diag_init);
1141 module_exit(inet_diag_exit);
1142 MODULE_LICENSE("GPL");
1143 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1144 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);