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