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