Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 1 | #include <linux/mutex.h> |
| 2 | #include <linux/socket.h> |
| 3 | #include <linux/skbuff.h> |
| 4 | #include <net/netlink.h> |
| 5 | #include <net/net_namespace.h> |
| 6 | #include <linux/module.h> |
Pavel Emelyanov | 5d2e5f2 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 7 | #include <net/sock.h> |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 8 | #include <linux/kernel.h> |
| 9 | #include <linux/tcp.h> |
| 10 | #include <linux/workqueue.h> |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 11 | |
| 12 | #include <linux/inet_diag.h> |
| 13 | #include <linux/sock_diag.h> |
| 14 | |
Shan Wei | 8dcf01f | 2012-04-24 18:21:07 +0000 | [diff] [blame] | 15 | static const struct sock_diag_handler *sock_diag_handlers[AF_MAX]; |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 16 | static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh); |
| 17 | static DEFINE_MUTEX(sock_diag_table_mutex); |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 18 | static struct workqueue_struct *broadcast_wq; |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 19 | |
Eric Dumazet | 33cf7c9 | 2015-03-11 18:53:14 -0700 | [diff] [blame] | 20 | static u64 sock_gen_cookie(struct sock *sk) |
Pavel Emelyanov | f65c1b5 | 2011-12-15 02:43:44 +0000 | [diff] [blame] | 21 | { |
Eric Dumazet | 33cf7c9 | 2015-03-11 18:53:14 -0700 | [diff] [blame] | 22 | while (1) { |
| 23 | u64 res = atomic64_read(&sk->sk_cookie); |
| 24 | |
| 25 | if (res) |
| 26 | return res; |
| 27 | res = atomic64_inc_return(&sock_net(sk)->cookie_gen); |
| 28 | atomic64_cmpxchg(&sk->sk_cookie, 0, res); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie) |
| 33 | { |
| 34 | u64 res; |
| 35 | |
| 36 | if (cookie[0] == INET_DIAG_NOCOOKIE && cookie[1] == INET_DIAG_NOCOOKIE) |
Pavel Emelyanov | f65c1b5 | 2011-12-15 02:43:44 +0000 | [diff] [blame] | 37 | return 0; |
Eric Dumazet | 33cf7c9 | 2015-03-11 18:53:14 -0700 | [diff] [blame] | 38 | |
| 39 | res = sock_gen_cookie(sk); |
| 40 | if ((u32)res != cookie[0] || (u32)(res >> 32) != cookie[1]) |
| 41 | return -ESTALE; |
| 42 | |
| 43 | return 0; |
Pavel Emelyanov | f65c1b5 | 2011-12-15 02:43:44 +0000 | [diff] [blame] | 44 | } |
| 45 | EXPORT_SYMBOL_GPL(sock_diag_check_cookie); |
| 46 | |
Eric Dumazet | 33cf7c9 | 2015-03-11 18:53:14 -0700 | [diff] [blame] | 47 | void sock_diag_save_cookie(struct sock *sk, __u32 *cookie) |
Pavel Emelyanov | f65c1b5 | 2011-12-15 02:43:44 +0000 | [diff] [blame] | 48 | { |
Eric Dumazet | 33cf7c9 | 2015-03-11 18:53:14 -0700 | [diff] [blame] | 49 | u64 res = sock_gen_cookie(sk); |
| 50 | |
| 51 | cookie[0] = (u32)res; |
| 52 | cookie[1] = (u32)(res >> 32); |
Pavel Emelyanov | f65c1b5 | 2011-12-15 02:43:44 +0000 | [diff] [blame] | 53 | } |
| 54 | EXPORT_SYMBOL_GPL(sock_diag_save_cookie); |
| 55 | |
Pavel Emelyanov | 5d2e5f2 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 56 | int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype) |
| 57 | { |
Thomas Graf | 7b46866 | 2012-06-26 23:36:11 +0000 | [diff] [blame] | 58 | u32 mem[SK_MEMINFO_VARS]; |
Pavel Emelyanov | 5d2e5f2 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 59 | |
| 60 | mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk); |
| 61 | mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf; |
| 62 | mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk); |
| 63 | mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf; |
| 64 | mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc; |
| 65 | mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued; |
| 66 | mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc); |
Eric Dumazet | d594e98 | 2012-06-04 03:50:35 +0000 | [diff] [blame] | 67 | mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len; |
Pavel Emelyanov | 5d2e5f2 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 68 | |
Thomas Graf | 7b46866 | 2012-06-26 23:36:11 +0000 | [diff] [blame] | 69 | return nla_put(skb, attrtype, sizeof(mem), &mem); |
Pavel Emelyanov | 5d2e5f2 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 70 | } |
| 71 | EXPORT_SYMBOL_GPL(sock_diag_put_meminfo); |
| 72 | |
Eric W. Biederman | a53b72c | 2014-04-23 14:26:25 -0700 | [diff] [blame] | 73 | int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk, |
Nicolas Dichtel | e8d9612 | 2013-04-25 06:53:54 +0000 | [diff] [blame] | 74 | struct sk_buff *skb, int attrtype) |
| 75 | { |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 76 | struct sock_fprog_kern *fprog; |
Nicolas Dichtel | e8d9612 | 2013-04-25 06:53:54 +0000 | [diff] [blame] | 77 | struct sk_filter *filter; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 78 | struct nlattr *attr; |
| 79 | unsigned int flen; |
Nicolas Dichtel | e8d9612 | 2013-04-25 06:53:54 +0000 | [diff] [blame] | 80 | int err = 0; |
| 81 | |
Eric W. Biederman | a53b72c | 2014-04-23 14:26:25 -0700 | [diff] [blame] | 82 | if (!may_report_filterinfo) { |
Nicolas Dichtel | e8d9612 | 2013-04-25 06:53:54 +0000 | [diff] [blame] | 83 | nla_reserve(skb, attrtype, 0); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | rcu_read_lock(); |
Nicolas Dichtel | e8d9612 | 2013-04-25 06:53:54 +0000 | [diff] [blame] | 88 | filter = rcu_dereference(sk->sk_filter); |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 89 | if (!filter) |
| 90 | goto out; |
Nicolas Dichtel | e8d9612 | 2013-04-25 06:53:54 +0000 | [diff] [blame] | 91 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 92 | fprog = filter->prog->orig_prog; |
Daniel Borkmann | b382c08 | 2015-09-02 14:00:36 +0200 | [diff] [blame] | 93 | if (!fprog) |
| 94 | goto out; |
| 95 | |
Alexei Starovoitov | 009937e | 2014-07-30 20:34:13 -0700 | [diff] [blame] | 96 | flen = bpf_classic_proglen(fprog); |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 97 | |
| 98 | attr = nla_reserve(skb, attrtype, flen); |
Nicolas Dichtel | e8d9612 | 2013-04-25 06:53:54 +0000 | [diff] [blame] | 99 | if (attr == NULL) { |
| 100 | err = -EMSGSIZE; |
| 101 | goto out; |
| 102 | } |
| 103 | |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 104 | memcpy(nla_data(attr), fprog->filter, flen); |
Nicolas Dichtel | e8d9612 | 2013-04-25 06:53:54 +0000 | [diff] [blame] | 105 | out: |
| 106 | rcu_read_unlock(); |
| 107 | return err; |
| 108 | } |
| 109 | EXPORT_SYMBOL(sock_diag_put_filterinfo); |
| 110 | |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 111 | struct broadcast_sk { |
| 112 | struct sock *sk; |
| 113 | struct work_struct work; |
| 114 | }; |
| 115 | |
| 116 | static size_t sock_diag_nlmsg_size(void) |
| 117 | { |
| 118 | return NLMSG_ALIGN(sizeof(struct inet_diag_msg) |
| 119 | + nla_total_size(sizeof(u8)) /* INET_DIAG_PROTOCOL */ |
| 120 | + nla_total_size(sizeof(struct tcp_info))); /* INET_DIAG_INFO */ |
| 121 | } |
| 122 | |
| 123 | static void sock_diag_broadcast_destroy_work(struct work_struct *work) |
| 124 | { |
| 125 | struct broadcast_sk *bsk = |
| 126 | container_of(work, struct broadcast_sk, work); |
| 127 | struct sock *sk = bsk->sk; |
| 128 | const struct sock_diag_handler *hndl; |
| 129 | struct sk_buff *skb; |
| 130 | const enum sknetlink_groups group = sock_diag_destroy_group(sk); |
| 131 | int err = -1; |
| 132 | |
| 133 | WARN_ON(group == SKNLGRP_NONE); |
| 134 | |
| 135 | skb = nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL); |
| 136 | if (!skb) |
| 137 | goto out; |
| 138 | |
| 139 | mutex_lock(&sock_diag_table_mutex); |
| 140 | hndl = sock_diag_handlers[sk->sk_family]; |
| 141 | if (hndl && hndl->get_info) |
| 142 | err = hndl->get_info(skb, sk); |
| 143 | mutex_unlock(&sock_diag_table_mutex); |
| 144 | |
| 145 | if (!err) |
| 146 | nlmsg_multicast(sock_net(sk)->diag_nlsk, skb, 0, group, |
| 147 | GFP_KERNEL); |
| 148 | else |
| 149 | kfree_skb(skb); |
| 150 | out: |
| 151 | sk_destruct(sk); |
| 152 | kfree(bsk); |
| 153 | } |
| 154 | |
| 155 | void sock_diag_broadcast_destroy(struct sock *sk) |
| 156 | { |
| 157 | /* Note, this function is often called from an interrupt context. */ |
| 158 | struct broadcast_sk *bsk = |
| 159 | kmalloc(sizeof(struct broadcast_sk), GFP_ATOMIC); |
| 160 | if (!bsk) |
| 161 | return sk_destruct(sk); |
| 162 | bsk->sk = sk; |
| 163 | INIT_WORK(&bsk->work, sock_diag_broadcast_destroy_work); |
| 164 | queue_work(broadcast_wq, &bsk->work); |
| 165 | } |
| 166 | |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 167 | void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) |
| 168 | { |
| 169 | mutex_lock(&sock_diag_table_mutex); |
| 170 | inet_rcv_compat = fn; |
| 171 | mutex_unlock(&sock_diag_table_mutex); |
| 172 | } |
| 173 | EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat); |
| 174 | |
| 175 | void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) |
| 176 | { |
| 177 | mutex_lock(&sock_diag_table_mutex); |
| 178 | inet_rcv_compat = NULL; |
| 179 | mutex_unlock(&sock_diag_table_mutex); |
| 180 | } |
| 181 | EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat); |
| 182 | |
Shan Wei | 8dcf01f | 2012-04-24 18:21:07 +0000 | [diff] [blame] | 183 | int sock_diag_register(const struct sock_diag_handler *hndl) |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 184 | { |
| 185 | int err = 0; |
| 186 | |
Dan Carpenter | 6f8e4ad | 2011-12-07 20:49:38 +0000 | [diff] [blame] | 187 | if (hndl->family >= AF_MAX) |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 188 | return -EINVAL; |
| 189 | |
| 190 | mutex_lock(&sock_diag_table_mutex); |
| 191 | if (sock_diag_handlers[hndl->family]) |
| 192 | err = -EBUSY; |
| 193 | else |
| 194 | sock_diag_handlers[hndl->family] = hndl; |
| 195 | mutex_unlock(&sock_diag_table_mutex); |
| 196 | |
| 197 | return err; |
| 198 | } |
| 199 | EXPORT_SYMBOL_GPL(sock_diag_register); |
| 200 | |
Shan Wei | 8dcf01f | 2012-04-24 18:21:07 +0000 | [diff] [blame] | 201 | void sock_diag_unregister(const struct sock_diag_handler *hnld) |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 202 | { |
| 203 | int family = hnld->family; |
| 204 | |
Dan Carpenter | 6f8e4ad | 2011-12-07 20:49:38 +0000 | [diff] [blame] | 205 | if (family >= AF_MAX) |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 206 | return; |
| 207 | |
| 208 | mutex_lock(&sock_diag_table_mutex); |
| 209 | BUG_ON(sock_diag_handlers[family] != hnld); |
| 210 | sock_diag_handlers[family] = NULL; |
| 211 | mutex_unlock(&sock_diag_table_mutex); |
| 212 | } |
| 213 | EXPORT_SYMBOL_GPL(sock_diag_unregister); |
| 214 | |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 215 | static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 216 | { |
| 217 | int err; |
Thomas Graf | 7b46866 | 2012-06-26 23:36:11 +0000 | [diff] [blame] | 218 | struct sock_diag_req *req = nlmsg_data(nlh); |
Shan Wei | 8dcf01f | 2012-04-24 18:21:07 +0000 | [diff] [blame] | 219 | const struct sock_diag_handler *hndl; |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 220 | |
| 221 | if (nlmsg_len(nlh) < sizeof(*req)) |
| 222 | return -EINVAL; |
| 223 | |
Mathias Krause | 6e601a5 | 2013-02-23 01:13:47 +0000 | [diff] [blame] | 224 | if (req->sdiag_family >= AF_MAX) |
| 225 | return -EINVAL; |
| 226 | |
Mathias Krause | 8e90455 | 2013-02-23 01:13:48 +0000 | [diff] [blame] | 227 | if (sock_diag_handlers[req->sdiag_family] == NULL) |
| 228 | request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, |
| 229 | NETLINK_SOCK_DIAG, req->sdiag_family); |
| 230 | |
| 231 | mutex_lock(&sock_diag_table_mutex); |
| 232 | hndl = sock_diag_handlers[req->sdiag_family]; |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 233 | if (hndl == NULL) |
| 234 | err = -ENOENT; |
| 235 | else |
| 236 | err = hndl->dump(skb, nlh); |
Mathias Krause | 8e90455 | 2013-02-23 01:13:48 +0000 | [diff] [blame] | 237 | mutex_unlock(&sock_diag_table_mutex); |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 238 | |
| 239 | return err; |
| 240 | } |
| 241 | |
| 242 | static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 243 | { |
| 244 | int ret; |
| 245 | |
| 246 | switch (nlh->nlmsg_type) { |
| 247 | case TCPDIAG_GETSOCK: |
| 248 | case DCCPDIAG_GETSOCK: |
| 249 | if (inet_rcv_compat == NULL) |
| 250 | request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, |
Pavel Emelyanov | aec8dc6 | 2011-12-15 02:43:27 +0000 | [diff] [blame] | 251 | NETLINK_SOCK_DIAG, AF_INET); |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 252 | |
| 253 | mutex_lock(&sock_diag_table_mutex); |
| 254 | if (inet_rcv_compat != NULL) |
| 255 | ret = inet_rcv_compat(skb, nlh); |
| 256 | else |
| 257 | ret = -EOPNOTSUPP; |
| 258 | mutex_unlock(&sock_diag_table_mutex); |
| 259 | |
| 260 | return ret; |
| 261 | case SOCK_DIAG_BY_FAMILY: |
| 262 | return __sock_diag_rcv_msg(skb, nlh); |
| 263 | default: |
| 264 | return -EINVAL; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static DEFINE_MUTEX(sock_diag_mutex); |
| 269 | |
| 270 | static void sock_diag_rcv(struct sk_buff *skb) |
| 271 | { |
| 272 | mutex_lock(&sock_diag_mutex); |
| 273 | netlink_rcv_skb(skb, &sock_diag_rcv_msg); |
| 274 | mutex_unlock(&sock_diag_mutex); |
| 275 | } |
| 276 | |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 277 | static int sock_diag_bind(struct net *net, int group) |
| 278 | { |
| 279 | switch (group) { |
| 280 | case SKNLGRP_INET_TCP_DESTROY: |
| 281 | case SKNLGRP_INET_UDP_DESTROY: |
| 282 | if (!sock_diag_handlers[AF_INET]) |
| 283 | request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, |
| 284 | NETLINK_SOCK_DIAG, AF_INET); |
| 285 | break; |
| 286 | case SKNLGRP_INET6_TCP_DESTROY: |
| 287 | case SKNLGRP_INET6_UDP_DESTROY: |
| 288 | if (!sock_diag_handlers[AF_INET6]) |
| 289 | request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, |
| 290 | NETLINK_SOCK_DIAG, AF_INET); |
| 291 | break; |
| 292 | } |
| 293 | return 0; |
| 294 | } |
| 295 | |
Andrey Vagin | 51d7ccc | 2012-07-16 04:28:49 +0000 | [diff] [blame] | 296 | static int __net_init diag_net_init(struct net *net) |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 297 | { |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 298 | struct netlink_kernel_cfg cfg = { |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 299 | .groups = SKNLGRP_MAX, |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 300 | .input = sock_diag_rcv, |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 301 | .bind = sock_diag_bind, |
| 302 | .flags = NL_CFG_F_NONROOT_RECV, |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 303 | }; |
| 304 | |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 305 | net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg); |
Andrey Vagin | 51d7ccc | 2012-07-16 04:28:49 +0000 | [diff] [blame] | 306 | return net->diag_nlsk == NULL ? -ENOMEM : 0; |
| 307 | } |
| 308 | |
| 309 | static void __net_exit diag_net_exit(struct net *net) |
| 310 | { |
| 311 | netlink_kernel_release(net->diag_nlsk); |
| 312 | net->diag_nlsk = NULL; |
| 313 | } |
| 314 | |
| 315 | static struct pernet_operations diag_net_ops = { |
| 316 | .init = diag_net_init, |
| 317 | .exit = diag_net_exit, |
| 318 | }; |
| 319 | |
| 320 | static int __init sock_diag_init(void) |
| 321 | { |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 322 | broadcast_wq = alloc_workqueue("sock_diag_events", 0, 0); |
| 323 | BUG_ON(!broadcast_wq); |
Andrey Vagin | 51d7ccc | 2012-07-16 04:28:49 +0000 | [diff] [blame] | 324 | return register_pernet_subsys(&diag_net_ops); |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static void __exit sock_diag_exit(void) |
| 328 | { |
Andrey Vagin | 51d7ccc | 2012-07-16 04:28:49 +0000 | [diff] [blame] | 329 | unregister_pernet_subsys(&diag_net_ops); |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 330 | destroy_workqueue(broadcast_wq); |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | module_init(sock_diag_init); |
| 334 | module_exit(sock_diag_exit); |
| 335 | MODULE_LICENSE("GPL"); |
| 336 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG); |