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