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