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