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