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