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