Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NETLINK Kernel-user communication protocol. |
| 3 | * |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 4 | * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 6 | * Patrick McHardy <kaber@trash.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 12 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith |
| 14 | * added netlink_proto_exit |
| 15 | * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br> |
| 16 | * use nlk_sk, as sk->protinfo is on a diet 8) |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 17 | * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org> |
| 18 | * - inc module use count of module that owns |
| 19 | * the kernel socket in case userspace opens |
| 20 | * socket of same protocol |
| 21 | * - remove all module support, since netlink is |
| 22 | * mandatory if CONFIG_NET=y these days |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | */ |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 27 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/kernel.h> |
| 29 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/signal.h> |
| 31 | #include <linux/sched.h> |
| 32 | #include <linux/errno.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <linux/stat.h> |
| 35 | #include <linux/socket.h> |
| 36 | #include <linux/un.h> |
| 37 | #include <linux/fcntl.h> |
| 38 | #include <linux/termios.h> |
| 39 | #include <linux/sockios.h> |
| 40 | #include <linux/net.h> |
| 41 | #include <linux/fs.h> |
| 42 | #include <linux/slab.h> |
| 43 | #include <asm/uaccess.h> |
| 44 | #include <linux/skbuff.h> |
| 45 | #include <linux/netdevice.h> |
| 46 | #include <linux/rtnetlink.h> |
| 47 | #include <linux/proc_fs.h> |
| 48 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/notifier.h> |
| 50 | #include <linux/security.h> |
| 51 | #include <linux/jhash.h> |
| 52 | #include <linux/jiffies.h> |
| 53 | #include <linux/random.h> |
| 54 | #include <linux/bitops.h> |
| 55 | #include <linux/mm.h> |
| 56 | #include <linux/types.h> |
Andrew Morton | 54e0f52 | 2005-04-30 07:07:04 +0100 | [diff] [blame] | 57 | #include <linux/audit.h> |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 58 | #include <linux/mutex.h> |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 59 | #include <linux/vmalloc.h> |
Daniel Borkmann | bcbde0d | 2013-06-21 19:38:07 +0200 | [diff] [blame] | 60 | #include <linux/if_arp.h> |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 61 | #include <linux/rhashtable.h> |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 62 | #include <asm/cacheflush.h> |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 63 | #include <linux/hash.h> |
Andrew Morton | 54e0f52 | 2005-04-30 07:07:04 +0100 | [diff] [blame] | 64 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 65 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | #include <net/sock.h> |
| 67 | #include <net/scm.h> |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 68 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Andrey Vagin | 0f29c76 | 2013-03-21 20:33:47 +0400 | [diff] [blame] | 70 | #include "af_netlink.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 72 | struct listeners { |
| 73 | struct rcu_head rcu; |
| 74 | unsigned long masks[0]; |
Johannes Berg | 6c04bb1 | 2009-07-10 09:51:32 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Patrick McHardy | cd967e0 | 2013-04-17 06:46:56 +0000 | [diff] [blame] | 77 | /* state bits */ |
| 78 | #define NETLINK_CONGESTED 0x0 |
| 79 | |
| 80 | /* flags */ |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 81 | #define NETLINK_KERNEL_SOCKET 0x1 |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 82 | #define NETLINK_RECV_PKTINFO 0x2 |
Pablo Neira Ayuso | be0c22a | 2009-02-18 01:40:43 +0000 | [diff] [blame] | 83 | #define NETLINK_BROADCAST_SEND_ERROR 0x4 |
Pablo Neira Ayuso | 38938bf | 2009-03-24 16:37:55 -0700 | [diff] [blame] | 84 | #define NETLINK_RECV_NO_ENOBUFS 0x8 |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 85 | |
David S. Miller | 035c4c1 | 2011-12-23 17:33:03 -0500 | [diff] [blame] | 86 | static inline int netlink_is_kernel(struct sock *sk) |
Denis V. Lunev | aed8156 | 2007-10-10 21:14:32 -0700 | [diff] [blame] | 87 | { |
| 88 | return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET; |
| 89 | } |
| 90 | |
Andrey Vagin | 0f29c76 | 2013-03-21 20:33:47 +0400 | [diff] [blame] | 91 | struct netlink_table *nl_table; |
| 92 | EXPORT_SYMBOL_GPL(nl_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait); |
| 95 | |
| 96 | static int netlink_dump(struct sock *sk); |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 97 | static void netlink_skb_destructor(struct sk_buff *skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Andrey Vagin | 0f29c76 | 2013-03-21 20:33:47 +0400 | [diff] [blame] | 99 | DEFINE_RWLOCK(nl_table_lock); |
| 100 | EXPORT_SYMBOL_GPL(nl_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | static atomic_t nl_table_users = ATOMIC_INIT(0); |
| 102 | |
Eric Dumazet | 6d772ac | 2012-10-18 03:21:55 +0000 | [diff] [blame] | 103 | #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock)); |
| 104 | |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 105 | /* Protects netlink socket hash table mutations */ |
| 106 | DEFINE_MUTEX(nl_sk_hash_lock); |
Thomas Graf | 6c8f7e7 | 2014-08-07 00:18:47 +0100 | [diff] [blame] | 107 | EXPORT_SYMBOL_GPL(nl_sk_hash_lock); |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 108 | |
| 109 | static int lockdep_nl_sk_hash_is_held(void) |
| 110 | { |
| 111 | #ifdef CONFIG_LOCKDEP |
| 112 | return (debug_locks) ? lockdep_is_held(&nl_sk_hash_lock) : 1; |
| 113 | #else |
| 114 | return 1; |
| 115 | #endif |
| 116 | } |
| 117 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 118 | static ATOMIC_NOTIFIER_HEAD(netlink_chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Daniel Borkmann | bcbde0d | 2013-06-21 19:38:07 +0200 | [diff] [blame] | 120 | static DEFINE_SPINLOCK(netlink_tap_lock); |
| 121 | static struct list_head netlink_tap_all __read_mostly; |
| 122 | |
stephen hemminger | b57ef81f | 2011-12-22 08:52:02 +0000 | [diff] [blame] | 123 | static inline u32 netlink_group_mask(u32 group) |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 124 | { |
| 125 | return group ? 1 << (group - 1) : 0; |
| 126 | } |
| 127 | |
Daniel Borkmann | bcbde0d | 2013-06-21 19:38:07 +0200 | [diff] [blame] | 128 | int netlink_add_tap(struct netlink_tap *nt) |
| 129 | { |
| 130 | if (unlikely(nt->dev->type != ARPHRD_NETLINK)) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | spin_lock(&netlink_tap_lock); |
| 134 | list_add_rcu(&nt->list, &netlink_tap_all); |
| 135 | spin_unlock(&netlink_tap_lock); |
| 136 | |
| 137 | if (nt->module) |
| 138 | __module_get(nt->module); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | EXPORT_SYMBOL_GPL(netlink_add_tap); |
| 143 | |
stephen hemminger | 2173f8d | 2013-12-30 10:49:22 -0800 | [diff] [blame] | 144 | static int __netlink_remove_tap(struct netlink_tap *nt) |
Daniel Borkmann | bcbde0d | 2013-06-21 19:38:07 +0200 | [diff] [blame] | 145 | { |
| 146 | bool found = false; |
| 147 | struct netlink_tap *tmp; |
| 148 | |
| 149 | spin_lock(&netlink_tap_lock); |
| 150 | |
| 151 | list_for_each_entry(tmp, &netlink_tap_all, list) { |
| 152 | if (nt == tmp) { |
| 153 | list_del_rcu(&nt->list); |
| 154 | found = true; |
| 155 | goto out; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | pr_warn("__netlink_remove_tap: %p not found\n", nt); |
| 160 | out: |
| 161 | spin_unlock(&netlink_tap_lock); |
| 162 | |
| 163 | if (found && nt->module) |
| 164 | module_put(nt->module); |
| 165 | |
| 166 | return found ? 0 : -ENODEV; |
| 167 | } |
Daniel Borkmann | bcbde0d | 2013-06-21 19:38:07 +0200 | [diff] [blame] | 168 | |
| 169 | int netlink_remove_tap(struct netlink_tap *nt) |
| 170 | { |
| 171 | int ret; |
| 172 | |
| 173 | ret = __netlink_remove_tap(nt); |
| 174 | synchronize_net(); |
| 175 | |
| 176 | return ret; |
| 177 | } |
| 178 | EXPORT_SYMBOL_GPL(netlink_remove_tap); |
| 179 | |
Daniel Borkmann | 5ffd5cd | 2013-09-05 17:48:47 +0200 | [diff] [blame] | 180 | static bool netlink_filter_tap(const struct sk_buff *skb) |
| 181 | { |
| 182 | struct sock *sk = skb->sk; |
Daniel Borkmann | 5ffd5cd | 2013-09-05 17:48:47 +0200 | [diff] [blame] | 183 | |
| 184 | /* We take the more conservative approach and |
| 185 | * whitelist socket protocols that may pass. |
| 186 | */ |
| 187 | switch (sk->sk_protocol) { |
| 188 | case NETLINK_ROUTE: |
| 189 | case NETLINK_USERSOCK: |
| 190 | case NETLINK_SOCK_DIAG: |
| 191 | case NETLINK_NFLOG: |
| 192 | case NETLINK_XFRM: |
| 193 | case NETLINK_FIB_LOOKUP: |
| 194 | case NETLINK_NETFILTER: |
| 195 | case NETLINK_GENERIC: |
Varka Bhadram | 498044b | 2014-07-16 10:59:47 +0530 | [diff] [blame] | 196 | return true; |
Daniel Borkmann | 5ffd5cd | 2013-09-05 17:48:47 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Varka Bhadram | 498044b | 2014-07-16 10:59:47 +0530 | [diff] [blame] | 199 | return false; |
Daniel Borkmann | 5ffd5cd | 2013-09-05 17:48:47 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Daniel Borkmann | bcbde0d | 2013-06-21 19:38:07 +0200 | [diff] [blame] | 202 | static int __netlink_deliver_tap_skb(struct sk_buff *skb, |
| 203 | struct net_device *dev) |
| 204 | { |
| 205 | struct sk_buff *nskb; |
Daniel Borkmann | 5ffd5cd | 2013-09-05 17:48:47 +0200 | [diff] [blame] | 206 | struct sock *sk = skb->sk; |
Daniel Borkmann | bcbde0d | 2013-06-21 19:38:07 +0200 | [diff] [blame] | 207 | int ret = -ENOMEM; |
| 208 | |
| 209 | dev_hold(dev); |
| 210 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 211 | if (nskb) { |
| 212 | nskb->dev = dev; |
Daniel Borkmann | 5ffd5cd | 2013-09-05 17:48:47 +0200 | [diff] [blame] | 213 | nskb->protocol = htons((u16) sk->sk_protocol); |
Daniel Borkmann | 604d13c | 2013-12-23 14:35:56 +0100 | [diff] [blame] | 214 | nskb->pkt_type = netlink_is_kernel(sk) ? |
| 215 | PACKET_KERNEL : PACKET_USER; |
Daniel Borkmann | 4e48ed8 | 2014-08-07 22:22:47 +0200 | [diff] [blame] | 216 | skb_reset_network_header(nskb); |
Daniel Borkmann | bcbde0d | 2013-06-21 19:38:07 +0200 | [diff] [blame] | 217 | ret = dev_queue_xmit(nskb); |
| 218 | if (unlikely(ret > 0)) |
| 219 | ret = net_xmit_errno(ret); |
| 220 | } |
| 221 | |
| 222 | dev_put(dev); |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | static void __netlink_deliver_tap(struct sk_buff *skb) |
| 227 | { |
| 228 | int ret; |
| 229 | struct netlink_tap *tmp; |
| 230 | |
Daniel Borkmann | 5ffd5cd | 2013-09-05 17:48:47 +0200 | [diff] [blame] | 231 | if (!netlink_filter_tap(skb)) |
| 232 | return; |
| 233 | |
Daniel Borkmann | bcbde0d | 2013-06-21 19:38:07 +0200 | [diff] [blame] | 234 | list_for_each_entry_rcu(tmp, &netlink_tap_all, list) { |
| 235 | ret = __netlink_deliver_tap_skb(skb, tmp->dev); |
| 236 | if (unlikely(ret)) |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static void netlink_deliver_tap(struct sk_buff *skb) |
| 242 | { |
| 243 | rcu_read_lock(); |
| 244 | |
| 245 | if (unlikely(!list_empty(&netlink_tap_all))) |
| 246 | __netlink_deliver_tap(skb); |
| 247 | |
| 248 | rcu_read_unlock(); |
| 249 | } |
| 250 | |
Daniel Borkmann | 73bfd37 | 2013-12-23 14:35:55 +0100 | [diff] [blame] | 251 | static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src, |
| 252 | struct sk_buff *skb) |
| 253 | { |
| 254 | if (!(netlink_is_kernel(dst) && netlink_is_kernel(src))) |
| 255 | netlink_deliver_tap(skb); |
| 256 | } |
| 257 | |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 258 | static void netlink_overrun(struct sock *sk) |
| 259 | { |
| 260 | struct netlink_sock *nlk = nlk_sk(sk); |
| 261 | |
| 262 | if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) { |
| 263 | if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) { |
| 264 | sk->sk_err = ENOBUFS; |
| 265 | sk->sk_error_report(sk); |
| 266 | } |
| 267 | } |
| 268 | atomic_inc(&sk->sk_drops); |
| 269 | } |
| 270 | |
| 271 | static void netlink_rcv_wake(struct sock *sk) |
| 272 | { |
| 273 | struct netlink_sock *nlk = nlk_sk(sk); |
| 274 | |
| 275 | if (skb_queue_empty(&sk->sk_receive_queue)) |
| 276 | clear_bit(NETLINK_CONGESTED, &nlk->state); |
| 277 | if (!test_bit(NETLINK_CONGESTED, &nlk->state)) |
| 278 | wake_up_interruptible(&nlk->wait); |
| 279 | } |
| 280 | |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 281 | #ifdef CONFIG_NETLINK_MMAP |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 282 | static bool netlink_skb_is_mmaped(const struct sk_buff *skb) |
| 283 | { |
| 284 | return NETLINK_CB(skb).flags & NETLINK_SKB_MMAPED; |
| 285 | } |
| 286 | |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 287 | static bool netlink_rx_is_mmaped(struct sock *sk) |
| 288 | { |
| 289 | return nlk_sk(sk)->rx_ring.pg_vec != NULL; |
| 290 | } |
| 291 | |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 292 | static bool netlink_tx_is_mmaped(struct sock *sk) |
| 293 | { |
| 294 | return nlk_sk(sk)->tx_ring.pg_vec != NULL; |
| 295 | } |
| 296 | |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 297 | static __pure struct page *pgvec_to_page(const void *addr) |
| 298 | { |
| 299 | if (is_vmalloc_addr(addr)) |
| 300 | return vmalloc_to_page(addr); |
| 301 | else |
| 302 | return virt_to_page(addr); |
| 303 | } |
| 304 | |
| 305 | static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len) |
| 306 | { |
| 307 | unsigned int i; |
| 308 | |
| 309 | for (i = 0; i < len; i++) { |
| 310 | if (pg_vec[i] != NULL) { |
| 311 | if (is_vmalloc_addr(pg_vec[i])) |
| 312 | vfree(pg_vec[i]); |
| 313 | else |
| 314 | free_pages((unsigned long)pg_vec[i], order); |
| 315 | } |
| 316 | } |
| 317 | kfree(pg_vec); |
| 318 | } |
| 319 | |
| 320 | static void *alloc_one_pg_vec_page(unsigned long order) |
| 321 | { |
| 322 | void *buffer; |
| 323 | gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO | |
| 324 | __GFP_NOWARN | __GFP_NORETRY; |
| 325 | |
| 326 | buffer = (void *)__get_free_pages(gfp_flags, order); |
| 327 | if (buffer != NULL) |
| 328 | return buffer; |
| 329 | |
| 330 | buffer = vzalloc((1 << order) * PAGE_SIZE); |
| 331 | if (buffer != NULL) |
| 332 | return buffer; |
| 333 | |
| 334 | gfp_flags &= ~__GFP_NORETRY; |
| 335 | return (void *)__get_free_pages(gfp_flags, order); |
| 336 | } |
| 337 | |
| 338 | static void **alloc_pg_vec(struct netlink_sock *nlk, |
| 339 | struct nl_mmap_req *req, unsigned int order) |
| 340 | { |
| 341 | unsigned int block_nr = req->nm_block_nr; |
| 342 | unsigned int i; |
Daniel Borkmann | 8a849bb | 2013-08-02 17:32:39 +0200 | [diff] [blame] | 343 | void **pg_vec; |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 344 | |
| 345 | pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL); |
| 346 | if (pg_vec == NULL) |
| 347 | return NULL; |
| 348 | |
| 349 | for (i = 0; i < block_nr; i++) { |
Daniel Borkmann | 8a849bb | 2013-08-02 17:32:39 +0200 | [diff] [blame] | 350 | pg_vec[i] = alloc_one_pg_vec_page(order); |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 351 | if (pg_vec[i] == NULL) |
| 352 | goto err1; |
| 353 | } |
| 354 | |
| 355 | return pg_vec; |
| 356 | err1: |
| 357 | free_pg_vec(pg_vec, order, block_nr); |
| 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req, |
| 362 | bool closing, bool tx_ring) |
| 363 | { |
| 364 | struct netlink_sock *nlk = nlk_sk(sk); |
| 365 | struct netlink_ring *ring; |
| 366 | struct sk_buff_head *queue; |
| 367 | void **pg_vec = NULL; |
| 368 | unsigned int order = 0; |
| 369 | int err; |
| 370 | |
| 371 | ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring; |
| 372 | queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue; |
| 373 | |
| 374 | if (!closing) { |
| 375 | if (atomic_read(&nlk->mapped)) |
| 376 | return -EBUSY; |
| 377 | if (atomic_read(&ring->pending)) |
| 378 | return -EBUSY; |
| 379 | } |
| 380 | |
| 381 | if (req->nm_block_nr) { |
| 382 | if (ring->pg_vec != NULL) |
| 383 | return -EBUSY; |
| 384 | |
| 385 | if ((int)req->nm_block_size <= 0) |
| 386 | return -EINVAL; |
Tobias Klauser | 74e83b2 | 2014-07-31 12:17:08 +0200 | [diff] [blame] | 387 | if (!PAGE_ALIGNED(req->nm_block_size)) |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 388 | return -EINVAL; |
| 389 | if (req->nm_frame_size < NL_MMAP_HDRLEN) |
| 390 | return -EINVAL; |
| 391 | if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT)) |
| 392 | return -EINVAL; |
| 393 | |
| 394 | ring->frames_per_block = req->nm_block_size / |
| 395 | req->nm_frame_size; |
| 396 | if (ring->frames_per_block == 0) |
| 397 | return -EINVAL; |
| 398 | if (ring->frames_per_block * req->nm_block_nr != |
| 399 | req->nm_frame_nr) |
| 400 | return -EINVAL; |
| 401 | |
| 402 | order = get_order(req->nm_block_size); |
| 403 | pg_vec = alloc_pg_vec(nlk, req, order); |
| 404 | if (pg_vec == NULL) |
| 405 | return -ENOMEM; |
| 406 | } else { |
| 407 | if (req->nm_frame_nr) |
| 408 | return -EINVAL; |
| 409 | } |
| 410 | |
| 411 | err = -EBUSY; |
| 412 | mutex_lock(&nlk->pg_vec_lock); |
| 413 | if (closing || atomic_read(&nlk->mapped) == 0) { |
| 414 | err = 0; |
| 415 | spin_lock_bh(&queue->lock); |
| 416 | |
| 417 | ring->frame_max = req->nm_frame_nr - 1; |
| 418 | ring->head = 0; |
| 419 | ring->frame_size = req->nm_frame_size; |
| 420 | ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE; |
| 421 | |
| 422 | swap(ring->pg_vec_len, req->nm_block_nr); |
| 423 | swap(ring->pg_vec_order, order); |
| 424 | swap(ring->pg_vec, pg_vec); |
| 425 | |
| 426 | __skb_queue_purge(queue); |
| 427 | spin_unlock_bh(&queue->lock); |
| 428 | |
| 429 | WARN_ON(atomic_read(&nlk->mapped)); |
| 430 | } |
| 431 | mutex_unlock(&nlk->pg_vec_lock); |
| 432 | |
| 433 | if (pg_vec) |
| 434 | free_pg_vec(pg_vec, order, req->nm_block_nr); |
| 435 | return err; |
| 436 | } |
| 437 | |
| 438 | static void netlink_mm_open(struct vm_area_struct *vma) |
| 439 | { |
| 440 | struct file *file = vma->vm_file; |
| 441 | struct socket *sock = file->private_data; |
| 442 | struct sock *sk = sock->sk; |
| 443 | |
| 444 | if (sk) |
| 445 | atomic_inc(&nlk_sk(sk)->mapped); |
| 446 | } |
| 447 | |
| 448 | static void netlink_mm_close(struct vm_area_struct *vma) |
| 449 | { |
| 450 | struct file *file = vma->vm_file; |
| 451 | struct socket *sock = file->private_data; |
| 452 | struct sock *sk = sock->sk; |
| 453 | |
| 454 | if (sk) |
| 455 | atomic_dec(&nlk_sk(sk)->mapped); |
| 456 | } |
| 457 | |
| 458 | static const struct vm_operations_struct netlink_mmap_ops = { |
| 459 | .open = netlink_mm_open, |
| 460 | .close = netlink_mm_close, |
| 461 | }; |
| 462 | |
| 463 | static int netlink_mmap(struct file *file, struct socket *sock, |
| 464 | struct vm_area_struct *vma) |
| 465 | { |
| 466 | struct sock *sk = sock->sk; |
| 467 | struct netlink_sock *nlk = nlk_sk(sk); |
| 468 | struct netlink_ring *ring; |
| 469 | unsigned long start, size, expected; |
| 470 | unsigned int i; |
| 471 | int err = -EINVAL; |
| 472 | |
| 473 | if (vma->vm_pgoff) |
| 474 | return -EINVAL; |
| 475 | |
| 476 | mutex_lock(&nlk->pg_vec_lock); |
| 477 | |
| 478 | expected = 0; |
| 479 | for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) { |
| 480 | if (ring->pg_vec == NULL) |
| 481 | continue; |
| 482 | expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE; |
| 483 | } |
| 484 | |
| 485 | if (expected == 0) |
| 486 | goto out; |
| 487 | |
| 488 | size = vma->vm_end - vma->vm_start; |
| 489 | if (size != expected) |
| 490 | goto out; |
| 491 | |
| 492 | start = vma->vm_start; |
| 493 | for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) { |
| 494 | if (ring->pg_vec == NULL) |
| 495 | continue; |
| 496 | |
| 497 | for (i = 0; i < ring->pg_vec_len; i++) { |
| 498 | struct page *page; |
| 499 | void *kaddr = ring->pg_vec[i]; |
| 500 | unsigned int pg_num; |
| 501 | |
| 502 | for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) { |
| 503 | page = pgvec_to_page(kaddr); |
| 504 | err = vm_insert_page(vma, start, page); |
| 505 | if (err < 0) |
| 506 | goto out; |
| 507 | start += PAGE_SIZE; |
| 508 | kaddr += PAGE_SIZE; |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | atomic_inc(&nlk->mapped); |
| 514 | vma->vm_ops = &netlink_mmap_ops; |
| 515 | err = 0; |
| 516 | out: |
| 517 | mutex_unlock(&nlk->pg_vec_lock); |
Patrick McHardy | 7cdbac7 | 2013-06-11 02:52:47 -0700 | [diff] [blame] | 518 | return err; |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 519 | } |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 520 | |
| 521 | static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr) |
| 522 | { |
| 523 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1 |
| 524 | struct page *p_start, *p_end; |
| 525 | |
| 526 | /* First page is flushed through netlink_{get,set}_status */ |
| 527 | p_start = pgvec_to_page(hdr + PAGE_SIZE); |
Stephen Rothwell | 1d5085c | 2013-04-23 17:40:35 +1000 | [diff] [blame] | 528 | p_end = pgvec_to_page((void *)hdr + NL_MMAP_HDRLEN + hdr->nm_len - 1); |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 529 | while (p_start <= p_end) { |
| 530 | flush_dcache_page(p_start); |
| 531 | p_start++; |
| 532 | } |
| 533 | #endif |
| 534 | } |
| 535 | |
| 536 | static enum nl_mmap_status netlink_get_status(const struct nl_mmap_hdr *hdr) |
| 537 | { |
| 538 | smp_rmb(); |
| 539 | flush_dcache_page(pgvec_to_page(hdr)); |
| 540 | return hdr->nm_status; |
| 541 | } |
| 542 | |
| 543 | static void netlink_set_status(struct nl_mmap_hdr *hdr, |
| 544 | enum nl_mmap_status status) |
| 545 | { |
| 546 | hdr->nm_status = status; |
| 547 | flush_dcache_page(pgvec_to_page(hdr)); |
| 548 | smp_wmb(); |
| 549 | } |
| 550 | |
| 551 | static struct nl_mmap_hdr * |
| 552 | __netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos) |
| 553 | { |
| 554 | unsigned int pg_vec_pos, frame_off; |
| 555 | |
| 556 | pg_vec_pos = pos / ring->frames_per_block; |
| 557 | frame_off = pos % ring->frames_per_block; |
| 558 | |
| 559 | return ring->pg_vec[pg_vec_pos] + (frame_off * ring->frame_size); |
| 560 | } |
| 561 | |
| 562 | static struct nl_mmap_hdr * |
| 563 | netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos, |
| 564 | enum nl_mmap_status status) |
| 565 | { |
| 566 | struct nl_mmap_hdr *hdr; |
| 567 | |
| 568 | hdr = __netlink_lookup_frame(ring, pos); |
| 569 | if (netlink_get_status(hdr) != status) |
| 570 | return NULL; |
| 571 | |
| 572 | return hdr; |
| 573 | } |
| 574 | |
| 575 | static struct nl_mmap_hdr * |
| 576 | netlink_current_frame(const struct netlink_ring *ring, |
| 577 | enum nl_mmap_status status) |
| 578 | { |
| 579 | return netlink_lookup_frame(ring, ring->head, status); |
| 580 | } |
| 581 | |
| 582 | static struct nl_mmap_hdr * |
| 583 | netlink_previous_frame(const struct netlink_ring *ring, |
| 584 | enum nl_mmap_status status) |
| 585 | { |
| 586 | unsigned int prev; |
| 587 | |
| 588 | prev = ring->head ? ring->head - 1 : ring->frame_max; |
| 589 | return netlink_lookup_frame(ring, prev, status); |
| 590 | } |
| 591 | |
| 592 | static void netlink_increment_head(struct netlink_ring *ring) |
| 593 | { |
| 594 | ring->head = ring->head != ring->frame_max ? ring->head + 1 : 0; |
| 595 | } |
| 596 | |
| 597 | static void netlink_forward_ring(struct netlink_ring *ring) |
| 598 | { |
| 599 | unsigned int head = ring->head, pos = head; |
| 600 | const struct nl_mmap_hdr *hdr; |
| 601 | |
| 602 | do { |
| 603 | hdr = __netlink_lookup_frame(ring, pos); |
| 604 | if (hdr->nm_status == NL_MMAP_STATUS_UNUSED) |
| 605 | break; |
| 606 | if (hdr->nm_status != NL_MMAP_STATUS_SKIP) |
| 607 | break; |
| 608 | netlink_increment_head(ring); |
| 609 | } while (ring->head != head); |
| 610 | } |
| 611 | |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 612 | static bool netlink_dump_space(struct netlink_sock *nlk) |
| 613 | { |
| 614 | struct netlink_ring *ring = &nlk->rx_ring; |
| 615 | struct nl_mmap_hdr *hdr; |
| 616 | unsigned int n; |
| 617 | |
| 618 | hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED); |
| 619 | if (hdr == NULL) |
| 620 | return false; |
| 621 | |
| 622 | n = ring->head + ring->frame_max / 2; |
| 623 | if (n > ring->frame_max) |
| 624 | n -= ring->frame_max; |
| 625 | |
| 626 | hdr = __netlink_lookup_frame(ring, n); |
| 627 | |
| 628 | return hdr->nm_status == NL_MMAP_STATUS_UNUSED; |
| 629 | } |
| 630 | |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 631 | static unsigned int netlink_poll(struct file *file, struct socket *sock, |
| 632 | poll_table *wait) |
| 633 | { |
| 634 | struct sock *sk = sock->sk; |
| 635 | struct netlink_sock *nlk = nlk_sk(sk); |
| 636 | unsigned int mask; |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 637 | int err; |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 638 | |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 639 | if (nlk->rx_ring.pg_vec != NULL) { |
| 640 | /* Memory mapped sockets don't call recvmsg(), so flow control |
| 641 | * for dumps is performed here. A dump is allowed to continue |
| 642 | * if at least half the ring is unused. |
| 643 | */ |
Pravin B Shelar | 16b304f | 2013-08-15 15:31:06 -0700 | [diff] [blame] | 644 | while (nlk->cb_running && netlink_dump_space(nlk)) { |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 645 | err = netlink_dump(sk); |
| 646 | if (err < 0) { |
Ben Pfaff | ac30ef8 | 2014-07-09 10:31:22 -0700 | [diff] [blame] | 647 | sk->sk_err = -err; |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 648 | sk->sk_error_report(sk); |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | netlink_rcv_wake(sk); |
| 653 | } |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 654 | |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 655 | mask = datagram_poll(file, sock, wait); |
| 656 | |
| 657 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 658 | if (nlk->rx_ring.pg_vec) { |
| 659 | netlink_forward_ring(&nlk->rx_ring); |
| 660 | if (!netlink_previous_frame(&nlk->rx_ring, NL_MMAP_STATUS_UNUSED)) |
| 661 | mask |= POLLIN | POLLRDNORM; |
| 662 | } |
| 663 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 664 | |
| 665 | spin_lock_bh(&sk->sk_write_queue.lock); |
| 666 | if (nlk->tx_ring.pg_vec) { |
| 667 | if (netlink_current_frame(&nlk->tx_ring, NL_MMAP_STATUS_UNUSED)) |
| 668 | mask |= POLLOUT | POLLWRNORM; |
| 669 | } |
| 670 | spin_unlock_bh(&sk->sk_write_queue.lock); |
| 671 | |
| 672 | return mask; |
| 673 | } |
| 674 | |
| 675 | static struct nl_mmap_hdr *netlink_mmap_hdr(struct sk_buff *skb) |
| 676 | { |
| 677 | return (struct nl_mmap_hdr *)(skb->head - NL_MMAP_HDRLEN); |
| 678 | } |
| 679 | |
| 680 | static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk, |
| 681 | struct netlink_ring *ring, |
| 682 | struct nl_mmap_hdr *hdr) |
| 683 | { |
| 684 | unsigned int size; |
| 685 | void *data; |
| 686 | |
| 687 | size = ring->frame_size - NL_MMAP_HDRLEN; |
| 688 | data = (void *)hdr + NL_MMAP_HDRLEN; |
| 689 | |
| 690 | skb->head = data; |
| 691 | skb->data = data; |
| 692 | skb_reset_tail_pointer(skb); |
| 693 | skb->end = skb->tail + size; |
| 694 | skb->len = 0; |
| 695 | |
| 696 | skb->destructor = netlink_skb_destructor; |
| 697 | NETLINK_CB(skb).flags |= NETLINK_SKB_MMAPED; |
| 698 | NETLINK_CB(skb).sk = sk; |
| 699 | } |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 700 | |
| 701 | static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg, |
| 702 | u32 dst_portid, u32 dst_group, |
| 703 | struct sock_iocb *siocb) |
| 704 | { |
| 705 | struct netlink_sock *nlk = nlk_sk(sk); |
| 706 | struct netlink_ring *ring; |
| 707 | struct nl_mmap_hdr *hdr; |
| 708 | struct sk_buff *skb; |
| 709 | unsigned int maxlen; |
| 710 | bool excl = true; |
| 711 | int err = 0, len = 0; |
| 712 | |
| 713 | /* Netlink messages are validated by the receiver before processing. |
| 714 | * In order to avoid userspace changing the contents of the message |
| 715 | * after validation, the socket and the ring may only be used by a |
| 716 | * single process, otherwise we fall back to copying. |
| 717 | */ |
| 718 | if (atomic_long_read(&sk->sk_socket->file->f_count) > 2 || |
| 719 | atomic_read(&nlk->mapped) > 1) |
| 720 | excl = false; |
| 721 | |
| 722 | mutex_lock(&nlk->pg_vec_lock); |
| 723 | |
| 724 | ring = &nlk->tx_ring; |
| 725 | maxlen = ring->frame_size - NL_MMAP_HDRLEN; |
| 726 | |
| 727 | do { |
| 728 | hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID); |
| 729 | if (hdr == NULL) { |
| 730 | if (!(msg->msg_flags & MSG_DONTWAIT) && |
| 731 | atomic_read(&nlk->tx_ring.pending)) |
| 732 | schedule(); |
| 733 | continue; |
| 734 | } |
| 735 | if (hdr->nm_len > maxlen) { |
| 736 | err = -EINVAL; |
| 737 | goto out; |
| 738 | } |
| 739 | |
| 740 | netlink_frame_flush_dcache(hdr); |
| 741 | |
| 742 | if (likely(dst_portid == 0 && dst_group == 0 && excl)) { |
| 743 | skb = alloc_skb_head(GFP_KERNEL); |
| 744 | if (skb == NULL) { |
| 745 | err = -ENOBUFS; |
| 746 | goto out; |
| 747 | } |
| 748 | sock_hold(sk); |
| 749 | netlink_ring_setup_skb(skb, sk, ring, hdr); |
| 750 | NETLINK_CB(skb).flags |= NETLINK_SKB_TX; |
| 751 | __skb_put(skb, hdr->nm_len); |
| 752 | netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED); |
| 753 | atomic_inc(&ring->pending); |
| 754 | } else { |
| 755 | skb = alloc_skb(hdr->nm_len, GFP_KERNEL); |
| 756 | if (skb == NULL) { |
| 757 | err = -ENOBUFS; |
| 758 | goto out; |
| 759 | } |
| 760 | __skb_put(skb, hdr->nm_len); |
| 761 | memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, hdr->nm_len); |
| 762 | netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED); |
| 763 | } |
| 764 | |
| 765 | netlink_increment_head(ring); |
| 766 | |
| 767 | NETLINK_CB(skb).portid = nlk->portid; |
| 768 | NETLINK_CB(skb).dst_group = dst_group; |
| 769 | NETLINK_CB(skb).creds = siocb->scm->creds; |
| 770 | |
| 771 | err = security_netlink_send(sk, skb); |
| 772 | if (err) { |
| 773 | kfree_skb(skb); |
| 774 | goto out; |
| 775 | } |
| 776 | |
| 777 | if (unlikely(dst_group)) { |
| 778 | atomic_inc(&skb->users); |
| 779 | netlink_broadcast(sk, skb, dst_portid, dst_group, |
| 780 | GFP_KERNEL); |
| 781 | } |
| 782 | err = netlink_unicast(sk, skb, dst_portid, |
| 783 | msg->msg_flags & MSG_DONTWAIT); |
| 784 | if (err < 0) |
| 785 | goto out; |
| 786 | len += err; |
| 787 | |
| 788 | } while (hdr != NULL || |
| 789 | (!(msg->msg_flags & MSG_DONTWAIT) && |
| 790 | atomic_read(&nlk->tx_ring.pending))); |
| 791 | |
| 792 | if (len > 0) |
| 793 | err = len; |
| 794 | out: |
| 795 | mutex_unlock(&nlk->pg_vec_lock); |
| 796 | return err; |
| 797 | } |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 798 | |
| 799 | static void netlink_queue_mmaped_skb(struct sock *sk, struct sk_buff *skb) |
| 800 | { |
| 801 | struct nl_mmap_hdr *hdr; |
| 802 | |
| 803 | hdr = netlink_mmap_hdr(skb); |
| 804 | hdr->nm_len = skb->len; |
| 805 | hdr->nm_group = NETLINK_CB(skb).dst_group; |
| 806 | hdr->nm_pid = NETLINK_CB(skb).creds.pid; |
Nicolas Dichtel | 1bf9310 | 2013-04-24 10:36:23 +0200 | [diff] [blame] | 807 | hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid); |
| 808 | hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid); |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 809 | netlink_frame_flush_dcache(hdr); |
| 810 | netlink_set_status(hdr, NL_MMAP_STATUS_VALID); |
| 811 | |
| 812 | NETLINK_CB(skb).flags |= NETLINK_SKB_DELIVERED; |
| 813 | kfree_skb(skb); |
| 814 | } |
| 815 | |
| 816 | static void netlink_ring_set_copied(struct sock *sk, struct sk_buff *skb) |
| 817 | { |
| 818 | struct netlink_sock *nlk = nlk_sk(sk); |
| 819 | struct netlink_ring *ring = &nlk->rx_ring; |
| 820 | struct nl_mmap_hdr *hdr; |
| 821 | |
| 822 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 823 | hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED); |
| 824 | if (hdr == NULL) { |
| 825 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 826 | kfree_skb(skb); |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 827 | netlink_overrun(sk); |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 828 | return; |
| 829 | } |
| 830 | netlink_increment_head(ring); |
| 831 | __skb_queue_tail(&sk->sk_receive_queue, skb); |
| 832 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 833 | |
| 834 | hdr->nm_len = skb->len; |
| 835 | hdr->nm_group = NETLINK_CB(skb).dst_group; |
| 836 | hdr->nm_pid = NETLINK_CB(skb).creds.pid; |
Nicolas Dichtel | 1bf9310 | 2013-04-24 10:36:23 +0200 | [diff] [blame] | 837 | hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid); |
| 838 | hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid); |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 839 | netlink_set_status(hdr, NL_MMAP_STATUS_COPY); |
| 840 | } |
| 841 | |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 842 | #else /* CONFIG_NETLINK_MMAP */ |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 843 | #define netlink_skb_is_mmaped(skb) false |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 844 | #define netlink_rx_is_mmaped(sk) false |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 845 | #define netlink_tx_is_mmaped(sk) false |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 846 | #define netlink_mmap sock_no_mmap |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 847 | #define netlink_poll datagram_poll |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 848 | #define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb) 0 |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 849 | #endif /* CONFIG_NETLINK_MMAP */ |
| 850 | |
Patrick McHardy | cf0a018 | 2013-04-17 06:47:00 +0000 | [diff] [blame] | 851 | static void netlink_skb_destructor(struct sk_buff *skb) |
| 852 | { |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 853 | #ifdef CONFIG_NETLINK_MMAP |
| 854 | struct nl_mmap_hdr *hdr; |
| 855 | struct netlink_ring *ring; |
| 856 | struct sock *sk; |
| 857 | |
| 858 | /* If a packet from the kernel to userspace was freed because of an |
| 859 | * error without being delivered to userspace, the kernel must reset |
| 860 | * the status. In the direction userspace to kernel, the status is |
| 861 | * always reset here after the packet was processed and freed. |
| 862 | */ |
| 863 | if (netlink_skb_is_mmaped(skb)) { |
| 864 | hdr = netlink_mmap_hdr(skb); |
| 865 | sk = NETLINK_CB(skb).sk; |
| 866 | |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 867 | if (NETLINK_CB(skb).flags & NETLINK_SKB_TX) { |
| 868 | netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED); |
| 869 | ring = &nlk_sk(sk)->tx_ring; |
| 870 | } else { |
| 871 | if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) { |
| 872 | hdr->nm_len = 0; |
| 873 | netlink_set_status(hdr, NL_MMAP_STATUS_VALID); |
| 874 | } |
| 875 | ring = &nlk_sk(sk)->rx_ring; |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 876 | } |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 877 | |
| 878 | WARN_ON(atomic_read(&ring->pending) == 0); |
| 879 | atomic_dec(&ring->pending); |
| 880 | sock_put(sk); |
| 881 | |
Pablo Neira | 5e71d9d | 2013-06-03 09:28:43 +0000 | [diff] [blame] | 882 | skb->head = NULL; |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 883 | } |
| 884 | #endif |
Pablo Neira Ayuso | c05cdb1 | 2013-06-03 09:46:28 +0000 | [diff] [blame] | 885 | if (is_vmalloc_addr(skb->head)) { |
Pablo Neira | 3a36515 | 2013-06-28 03:04:23 +0200 | [diff] [blame] | 886 | if (!skb->cloned || |
| 887 | !atomic_dec_return(&(skb_shinfo(skb)->dataref))) |
| 888 | vfree(skb->head); |
| 889 | |
Pablo Neira Ayuso | c05cdb1 | 2013-06-03 09:46:28 +0000 | [diff] [blame] | 890 | skb->head = NULL; |
| 891 | } |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 892 | if (skb->sk != NULL) |
| 893 | sock_rfree(skb); |
Patrick McHardy | cf0a018 | 2013-04-17 06:47:00 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk) |
| 897 | { |
| 898 | WARN_ON(skb->sk != NULL); |
| 899 | skb->sk = sk; |
| 900 | skb->destructor = netlink_skb_destructor; |
| 901 | atomic_add(skb->truesize, &sk->sk_rmem_alloc); |
| 902 | sk_mem_charge(sk, skb->truesize); |
| 903 | } |
| 904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | static void netlink_sock_destruct(struct sock *sk) |
| 906 | { |
Herbert Xu | 3f660d6 | 2007-05-03 03:17:14 -0700 | [diff] [blame] | 907 | struct netlink_sock *nlk = nlk_sk(sk); |
| 908 | |
Pravin B Shelar | 16b304f | 2013-08-15 15:31:06 -0700 | [diff] [blame] | 909 | if (nlk->cb_running) { |
| 910 | if (nlk->cb.done) |
| 911 | nlk->cb.done(&nlk->cb); |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 912 | |
Pravin B Shelar | 16b304f | 2013-08-15 15:31:06 -0700 | [diff] [blame] | 913 | module_put(nlk->cb.module); |
| 914 | kfree_skb(nlk->cb.skb); |
Herbert Xu | 3f660d6 | 2007-05-03 03:17:14 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | skb_queue_purge(&sk->sk_receive_queue); |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 918 | #ifdef CONFIG_NETLINK_MMAP |
| 919 | if (1) { |
| 920 | struct nl_mmap_req req; |
| 921 | |
| 922 | memset(&req, 0, sizeof(req)); |
| 923 | if (nlk->rx_ring.pg_vec) |
| 924 | netlink_set_ring(sk, &req, true, false); |
| 925 | memset(&req, 0, sizeof(req)); |
| 926 | if (nlk->tx_ring.pg_vec) |
| 927 | netlink_set_ring(sk, &req, true, true); |
| 928 | } |
| 929 | #endif /* CONFIG_NETLINK_MMAP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | |
| 931 | if (!sock_flag(sk, SOCK_DEAD)) { |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 932 | printk(KERN_ERR "Freeing alive netlink socket %p\n", sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | return; |
| 934 | } |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 935 | |
| 936 | WARN_ON(atomic_read(&sk->sk_rmem_alloc)); |
| 937 | WARN_ON(atomic_read(&sk->sk_wmem_alloc)); |
| 938 | WARN_ON(nlk_sk(sk)->groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 941 | /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on |
| 942 | * SMP. Look, when several writers sleep and reader wakes them up, all but one |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | * immediately hit write lock and grab all the cpus. Exclusive sleep solves |
| 944 | * this, _but_ remember, it adds useless work on UP machines. |
| 945 | */ |
| 946 | |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 947 | void netlink_table_grab(void) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 948 | __acquires(nl_table_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | { |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 950 | might_sleep(); |
| 951 | |
Arjan van de Ven | 6abd219 | 2006-07-03 00:24:07 -0700 | [diff] [blame] | 952 | write_lock_irq(&nl_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
| 954 | if (atomic_read(&nl_table_users)) { |
| 955 | DECLARE_WAITQUEUE(wait, current); |
| 956 | |
| 957 | add_wait_queue_exclusive(&nl_table_wait, &wait); |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 958 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 960 | if (atomic_read(&nl_table_users) == 0) |
| 961 | break; |
Arjan van de Ven | 6abd219 | 2006-07-03 00:24:07 -0700 | [diff] [blame] | 962 | write_unlock_irq(&nl_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | schedule(); |
Arjan van de Ven | 6abd219 | 2006-07-03 00:24:07 -0700 | [diff] [blame] | 964 | write_lock_irq(&nl_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | __set_current_state(TASK_RUNNING); |
| 968 | remove_wait_queue(&nl_table_wait, &wait); |
| 969 | } |
| 970 | } |
| 971 | |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 972 | void netlink_table_ungrab(void) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 973 | __releases(nl_table_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | { |
Arjan van de Ven | 6abd219 | 2006-07-03 00:24:07 -0700 | [diff] [blame] | 975 | write_unlock_irq(&nl_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | wake_up(&nl_table_wait); |
| 977 | } |
| 978 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 979 | static inline void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | netlink_lock_table(void) |
| 981 | { |
| 982 | /* read_lock() synchronizes us to netlink_table_grab */ |
| 983 | |
| 984 | read_lock(&nl_table_lock); |
| 985 | atomic_inc(&nl_table_users); |
| 986 | read_unlock(&nl_table_lock); |
| 987 | } |
| 988 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 989 | static inline void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | netlink_unlock_table(void) |
| 991 | { |
| 992 | if (atomic_dec_and_test(&nl_table_users)) |
| 993 | wake_up(&nl_table_wait); |
| 994 | } |
| 995 | |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 996 | struct netlink_compare_arg |
Gao feng | da12c90 | 2013-06-06 14:49:11 +0800 | [diff] [blame] | 997 | { |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 998 | struct net *net; |
| 999 | u32 portid; |
| 1000 | }; |
| 1001 | |
| 1002 | static bool netlink_compare(void *ptr, void *arg) |
| 1003 | { |
| 1004 | struct netlink_compare_arg *x = arg; |
| 1005 | struct sock *sk = ptr; |
| 1006 | |
| 1007 | return nlk_sk(sk)->portid == x->portid && |
| 1008 | net_eq(sock_net(sk), x->net); |
| 1009 | } |
| 1010 | |
| 1011 | static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid, |
| 1012 | struct net *net) |
| 1013 | { |
| 1014 | struct netlink_compare_arg arg = { |
| 1015 | .net = net, |
| 1016 | .portid = portid, |
| 1017 | }; |
| 1018 | u32 hash; |
| 1019 | |
| 1020 | hash = rhashtable_hashfn(&table->hash, &portid, sizeof(portid)); |
| 1021 | |
| 1022 | return rhashtable_lookup_compare(&table->hash, hash, |
| 1023 | &netlink_compare, &arg); |
Gao feng | da12c90 | 2013-06-06 14:49:11 +0800 | [diff] [blame] | 1024 | } |
| 1025 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1026 | static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | { |
Gao feng | da12c90 | 2013-06-06 14:49:11 +0800 | [diff] [blame] | 1028 | struct netlink_table *table = &nl_table[protocol]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | struct sock *sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 1031 | rcu_read_lock(); |
| 1032 | sk = __netlink_lookup(table, portid, net); |
| 1033 | if (sk) |
| 1034 | sock_hold(sk); |
| 1035 | rcu_read_unlock(); |
| 1036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | return sk; |
| 1038 | } |
| 1039 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1040 | static const struct proto_ops netlink_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1042 | static void |
| 1043 | netlink_update_listeners(struct sock *sk) |
| 1044 | { |
| 1045 | struct netlink_table *tbl = &nl_table[sk->sk_protocol]; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1046 | unsigned long mask; |
| 1047 | unsigned int i; |
Eric Dumazet | 6d772ac | 2012-10-18 03:21:55 +0000 | [diff] [blame] | 1048 | struct listeners *listeners; |
| 1049 | |
| 1050 | listeners = nl_deref_protected(tbl->listeners); |
| 1051 | if (!listeners) |
| 1052 | return; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1053 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1054 | for (i = 0; i < NLGRPLONGS(tbl->groups); i++) { |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1055 | mask = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1056 | sk_for_each_bound(sk, &tbl->mc_list) { |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1057 | if (i < NLGRPLONGS(nlk_sk(sk)->ngroups)) |
| 1058 | mask |= nlk_sk(sk)->groups[i]; |
| 1059 | } |
Eric Dumazet | 6d772ac | 2012-10-18 03:21:55 +0000 | [diff] [blame] | 1060 | listeners->masks[i] = mask; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1061 | } |
| 1062 | /* this function is only called with the netlink table "grabbed", which |
| 1063 | * makes sure updates are visible before bind or setsockopt return. */ |
| 1064 | } |
| 1065 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1066 | static int netlink_insert(struct sock *sk, struct net *net, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | { |
Gao feng | da12c90 | 2013-06-06 14:49:11 +0800 | [diff] [blame] | 1068 | struct netlink_table *table = &nl_table[sk->sk_protocol]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | int err = -EADDRINUSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 1071 | mutex_lock(&nl_sk_hash_lock); |
| 1072 | if (__netlink_lookup(table, portid, net)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | goto err; |
| 1074 | |
| 1075 | err = -EBUSY; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1076 | if (nlk_sk(sk)->portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | goto err; |
| 1078 | |
| 1079 | err = -ENOMEM; |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 1080 | if (BITS_PER_LONG > 32 && unlikely(table->hash.nelems >= UINT_MAX)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | goto err; |
| 1082 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1083 | nlk_sk(sk)->portid = portid; |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 1084 | sock_hold(sk); |
| 1085 | rhashtable_insert(&table->hash, &nlk_sk(sk)->node, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | err: |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 1088 | mutex_unlock(&nl_sk_hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | return err; |
| 1090 | } |
| 1091 | |
| 1092 | static void netlink_remove(struct sock *sk) |
| 1093 | { |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 1094 | struct netlink_table *table; |
| 1095 | |
| 1096 | mutex_lock(&nl_sk_hash_lock); |
| 1097 | table = &nl_table[sk->sk_protocol]; |
| 1098 | if (rhashtable_remove(&table->hash, &nlk_sk(sk)->node, GFP_KERNEL)) { |
| 1099 | WARN_ON(atomic_read(&sk->sk_refcnt) == 1); |
| 1100 | __sock_put(sk); |
| 1101 | } |
| 1102 | mutex_unlock(&nl_sk_hash_lock); |
| 1103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | netlink_table_grab(); |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1105 | if (nlk_sk(sk)->subscriptions) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | __sk_del_bind_node(sk); |
| 1107 | netlink_table_ungrab(); |
| 1108 | } |
| 1109 | |
| 1110 | static struct proto netlink_proto = { |
| 1111 | .name = "NETLINK", |
| 1112 | .owner = THIS_MODULE, |
| 1113 | .obj_size = sizeof(struct netlink_sock), |
| 1114 | }; |
| 1115 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 1116 | static int __netlink_create(struct net *net, struct socket *sock, |
| 1117 | struct mutex *cb_mutex, int protocol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | { |
| 1119 | struct sock *sk; |
| 1120 | struct netlink_sock *nlk; |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1121 | |
| 1122 | sock->ops = &netlink_ops; |
| 1123 | |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 1124 | sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto); |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1125 | if (!sk) |
| 1126 | return -ENOMEM; |
| 1127 | |
| 1128 | sock_init_data(sock, sk); |
| 1129 | |
| 1130 | nlk = nlk_sk(sk); |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 1131 | if (cb_mutex) { |
Patrick McHardy | ffa4d72 | 2007-04-25 14:01:17 -0700 | [diff] [blame] | 1132 | nlk->cb_mutex = cb_mutex; |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 1133 | } else { |
Patrick McHardy | ffa4d72 | 2007-04-25 14:01:17 -0700 | [diff] [blame] | 1134 | nlk->cb_mutex = &nlk->cb_def_mutex; |
| 1135 | mutex_init(nlk->cb_mutex); |
| 1136 | } |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1137 | init_waitqueue_head(&nlk->wait); |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 1138 | #ifdef CONFIG_NETLINK_MMAP |
| 1139 | mutex_init(&nlk->pg_vec_lock); |
| 1140 | #endif |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1141 | |
| 1142 | sk->sk_destruct = netlink_sock_destruct; |
| 1143 | sk->sk_protocol = protocol; |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 1147 | static int netlink_create(struct net *net, struct socket *sock, int protocol, |
| 1148 | int kern) |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1149 | { |
| 1150 | struct module *module = NULL; |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 1151 | struct mutex *cb_mutex; |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1152 | struct netlink_sock *nlk; |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 1153 | int (*bind)(int group); |
| 1154 | void (*unbind)(int group); |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1155 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | |
| 1157 | sock->state = SS_UNCONNECTED; |
| 1158 | |
| 1159 | if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM) |
| 1160 | return -ESOCKTNOSUPPORT; |
| 1161 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1162 | if (protocol < 0 || protocol >= MAX_LINKS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | return -EPROTONOSUPPORT; |
| 1164 | |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1165 | netlink_lock_table(); |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 1166 | #ifdef CONFIG_MODULES |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1167 | if (!nl_table[protocol].registered) { |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1168 | netlink_unlock_table(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1169 | request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1170 | netlink_lock_table(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1171 | } |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1172 | #endif |
| 1173 | if (nl_table[protocol].registered && |
| 1174 | try_module_get(nl_table[protocol].module)) |
| 1175 | module = nl_table[protocol].module; |
Alexey Dobriyan | 974c37e | 2010-01-30 10:05:05 +0000 | [diff] [blame] | 1176 | else |
| 1177 | err = -EPROTONOSUPPORT; |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 1178 | cb_mutex = nl_table[protocol].cb_mutex; |
Pablo Neira Ayuso | 0329274 | 2012-06-29 06:15:22 +0000 | [diff] [blame] | 1179 | bind = nl_table[protocol].bind; |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 1180 | unbind = nl_table[protocol].unbind; |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1181 | netlink_unlock_table(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1182 | |
Alexey Dobriyan | 974c37e | 2010-01-30 10:05:05 +0000 | [diff] [blame] | 1183 | if (err < 0) |
| 1184 | goto out; |
| 1185 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1186 | err = __netlink_create(net, sock, cb_mutex, protocol); |
| 1187 | if (err < 0) |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1188 | goto out_module; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | |
David S. Miller | 6f756a8 | 2008-11-23 17:34:03 -0800 | [diff] [blame] | 1190 | local_bh_disable(); |
Eric Dumazet | c1fd3b9 | 2008-11-23 15:48:22 -0800 | [diff] [blame] | 1191 | sock_prot_inuse_add(net, &netlink_proto, 1); |
David S. Miller | 6f756a8 | 2008-11-23 17:34:03 -0800 | [diff] [blame] | 1192 | local_bh_enable(); |
| 1193 | |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1194 | nlk = nlk_sk(sock->sk); |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1195 | nlk->module = module; |
Pablo Neira Ayuso | 0329274 | 2012-06-29 06:15:22 +0000 | [diff] [blame] | 1196 | nlk->netlink_bind = bind; |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 1197 | nlk->netlink_unbind = unbind; |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1198 | out: |
| 1199 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1201 | out_module: |
| 1202 | module_put(module); |
| 1203 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | static int netlink_release(struct socket *sock) |
| 1207 | { |
| 1208 | struct sock *sk = sock->sk; |
| 1209 | struct netlink_sock *nlk; |
| 1210 | |
| 1211 | if (!sk) |
| 1212 | return 0; |
| 1213 | |
| 1214 | netlink_remove(sk); |
Denis Lunev | ac57b3a | 2007-04-18 17:05:58 -0700 | [diff] [blame] | 1215 | sock_orphan(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | nlk = nlk_sk(sk); |
| 1217 | |
Herbert Xu | 3f660d6 | 2007-05-03 03:17:14 -0700 | [diff] [blame] | 1218 | /* |
| 1219 | * OK. Socket is unlinked, any packets that arrive now |
| 1220 | * will be purged. |
| 1221 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | sock->sk = NULL; |
| 1224 | wake_up_interruptible_all(&nlk->wait); |
| 1225 | |
| 1226 | skb_queue_purge(&sk->sk_write_queue); |
| 1227 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1228 | if (nlk->portid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | struct netlink_notify n = { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1230 | .net = sock_net(sk), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | .protocol = sk->sk_protocol, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1232 | .portid = nlk->portid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | }; |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1234 | atomic_notifier_call_chain(&netlink_chain, |
| 1235 | NETLINK_URELEASE, &n); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1236 | } |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1237 | |
Mariusz Kozlowski | 5e7c001 | 2007-01-02 15:24:30 -0800 | [diff] [blame] | 1238 | module_put(nlk->module); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1239 | |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1240 | netlink_table_grab(); |
Denis V. Lunev | aed8156 | 2007-10-10 21:14:32 -0700 | [diff] [blame] | 1241 | if (netlink_is_kernel(sk)) { |
Denis V. Lunev | 869e58f | 2008-01-18 23:53:31 -0800 | [diff] [blame] | 1242 | BUG_ON(nl_table[sk->sk_protocol].registered == 0); |
| 1243 | if (--nl_table[sk->sk_protocol].registered == 0) { |
Eric Dumazet | 6d772ac | 2012-10-18 03:21:55 +0000 | [diff] [blame] | 1244 | struct listeners *old; |
| 1245 | |
| 1246 | old = nl_deref_protected(nl_table[sk->sk_protocol].listeners); |
| 1247 | RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL); |
| 1248 | kfree_rcu(old, rcu); |
Denis V. Lunev | 869e58f | 2008-01-18 23:53:31 -0800 | [diff] [blame] | 1249 | nl_table[sk->sk_protocol].module = NULL; |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 1250 | nl_table[sk->sk_protocol].bind = NULL; |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 1251 | nl_table[sk->sk_protocol].unbind = NULL; |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 1252 | nl_table[sk->sk_protocol].flags = 0; |
Denis V. Lunev | 869e58f | 2008-01-18 23:53:31 -0800 | [diff] [blame] | 1253 | nl_table[sk->sk_protocol].registered = 0; |
| 1254 | } |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 1255 | } else if (nlk->subscriptions) { |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1256 | netlink_update_listeners(sk); |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 1257 | } |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1258 | netlink_table_ungrab(); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1259 | |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 1260 | /* Wait for readers to complete */ |
| 1261 | synchronize_net(); |
| 1262 | |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1263 | kfree(nlk->groups); |
| 1264 | nlk->groups = NULL; |
| 1265 | |
Eric Dumazet | 3755810 | 2008-11-24 14:05:22 -0800 | [diff] [blame] | 1266 | local_bh_disable(); |
Eric Dumazet | c1fd3b9 | 2008-11-23 15:48:22 -0800 | [diff] [blame] | 1267 | sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1); |
Eric Dumazet | 3755810 | 2008-11-24 14:05:22 -0800 | [diff] [blame] | 1268 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | sock_put(sk); |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | static int netlink_autobind(struct socket *sock) |
| 1274 | { |
| 1275 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1276 | struct net *net = sock_net(sk); |
Gao feng | da12c90 | 2013-06-06 14:49:11 +0800 | [diff] [blame] | 1277 | struct netlink_table *table = &nl_table[sk->sk_protocol]; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1278 | s32 portid = task_tgid_vnr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | int err; |
| 1280 | static s32 rover = -4097; |
| 1281 | |
| 1282 | retry: |
| 1283 | cond_resched(); |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 1284 | rcu_read_lock(); |
| 1285 | if (__netlink_lookup(table, portid, net)) { |
| 1286 | /* Bind collision, search negative portid values. */ |
| 1287 | portid = rover--; |
| 1288 | if (rover > -4097) |
| 1289 | rover = -4097; |
| 1290 | rcu_read_unlock(); |
| 1291 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | } |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 1293 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1295 | err = netlink_insert(sk, net, portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | if (err == -EADDRINUSE) |
| 1297 | goto retry; |
David S. Miller | d470e3b | 2005-06-26 15:31:51 -0700 | [diff] [blame] | 1298 | |
| 1299 | /* If 2 threads race to autobind, that is fine. */ |
| 1300 | if (err == -EBUSY) |
| 1301 | err = 0; |
| 1302 | |
| 1303 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
Eric W. Biederman | aa4cf94 | 2014-04-23 14:28:03 -0700 | [diff] [blame] | 1306 | /** |
| 1307 | * __netlink_ns_capable - General netlink message capability test |
| 1308 | * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace. |
| 1309 | * @user_ns: The user namespace of the capability to use |
| 1310 | * @cap: The capability to use |
| 1311 | * |
| 1312 | * Test to see if the opener of the socket we received the message |
| 1313 | * from had when the netlink socket was created and the sender of the |
| 1314 | * message has has the capability @cap in the user namespace @user_ns. |
| 1315 | */ |
| 1316 | bool __netlink_ns_capable(const struct netlink_skb_parms *nsp, |
| 1317 | struct user_namespace *user_ns, int cap) |
| 1318 | { |
Eric W. Biederman | 2d7a85f | 2014-05-30 11:04:00 -0700 | [diff] [blame] | 1319 | return ((nsp->flags & NETLINK_SKB_DST) || |
| 1320 | file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) && |
| 1321 | ns_capable(user_ns, cap); |
Eric W. Biederman | aa4cf94 | 2014-04-23 14:28:03 -0700 | [diff] [blame] | 1322 | } |
| 1323 | EXPORT_SYMBOL(__netlink_ns_capable); |
| 1324 | |
| 1325 | /** |
| 1326 | * netlink_ns_capable - General netlink message capability test |
| 1327 | * @skb: socket buffer holding a netlink command from userspace |
| 1328 | * @user_ns: The user namespace of the capability to use |
| 1329 | * @cap: The capability to use |
| 1330 | * |
| 1331 | * Test to see if the opener of the socket we received the message |
| 1332 | * from had when the netlink socket was created and the sender of the |
| 1333 | * message has has the capability @cap in the user namespace @user_ns. |
| 1334 | */ |
| 1335 | bool netlink_ns_capable(const struct sk_buff *skb, |
| 1336 | struct user_namespace *user_ns, int cap) |
| 1337 | { |
| 1338 | return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap); |
| 1339 | } |
| 1340 | EXPORT_SYMBOL(netlink_ns_capable); |
| 1341 | |
| 1342 | /** |
| 1343 | * netlink_capable - Netlink global message capability test |
| 1344 | * @skb: socket buffer holding a netlink command from userspace |
| 1345 | * @cap: The capability to use |
| 1346 | * |
| 1347 | * Test to see if the opener of the socket we received the message |
| 1348 | * from had when the netlink socket was created and the sender of the |
| 1349 | * message has has the capability @cap in all user namespaces. |
| 1350 | */ |
| 1351 | bool netlink_capable(const struct sk_buff *skb, int cap) |
| 1352 | { |
| 1353 | return netlink_ns_capable(skb, &init_user_ns, cap); |
| 1354 | } |
| 1355 | EXPORT_SYMBOL(netlink_capable); |
| 1356 | |
| 1357 | /** |
| 1358 | * netlink_net_capable - Netlink network namespace message capability test |
| 1359 | * @skb: socket buffer holding a netlink command from userspace |
| 1360 | * @cap: The capability to use |
| 1361 | * |
| 1362 | * Test to see if the opener of the socket we received the message |
| 1363 | * from had when the netlink socket was created and the sender of the |
| 1364 | * message has has the capability @cap over the network namespace of |
| 1365 | * the socket we received the message from. |
| 1366 | */ |
| 1367 | bool netlink_net_capable(const struct sk_buff *skb, int cap) |
| 1368 | { |
| 1369 | return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap); |
| 1370 | } |
| 1371 | EXPORT_SYMBOL(netlink_net_capable); |
| 1372 | |
Eric W. Biederman | 5187cd0 | 2014-04-23 14:25:48 -0700 | [diff] [blame] | 1373 | static inline int netlink_allowed(const struct socket *sock, unsigned int flag) |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1374 | { |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 1375 | return (nl_table[sock->sk->sk_protocol].flags & flag) || |
Eric W. Biederman | df008c9 | 2012-11-16 03:03:07 +0000 | [diff] [blame] | 1376 | ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1377 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1379 | static void |
| 1380 | netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions) |
| 1381 | { |
| 1382 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1383 | |
| 1384 | if (nlk->subscriptions && !subscriptions) |
| 1385 | __sk_del_bind_node(sk); |
| 1386 | else if (!nlk->subscriptions && subscriptions) |
| 1387 | sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list); |
| 1388 | nlk->subscriptions = subscriptions; |
| 1389 | } |
| 1390 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1391 | static int netlink_realloc_groups(struct sock *sk) |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1392 | { |
| 1393 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1394 | unsigned int groups; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1395 | unsigned long *new_groups; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1396 | int err = 0; |
| 1397 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1398 | netlink_table_grab(); |
| 1399 | |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1400 | groups = nl_table[sk->sk_protocol].groups; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1401 | if (!nl_table[sk->sk_protocol].registered) { |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1402 | err = -ENOENT; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1403 | goto out_unlock; |
| 1404 | } |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1405 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1406 | if (nlk->ngroups >= groups) |
| 1407 | goto out_unlock; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1408 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1409 | new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC); |
| 1410 | if (new_groups == NULL) { |
| 1411 | err = -ENOMEM; |
| 1412 | goto out_unlock; |
| 1413 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1414 | memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0, |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1415 | NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups)); |
| 1416 | |
| 1417 | nlk->groups = new_groups; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1418 | nlk->ngroups = groups; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1419 | out_unlock: |
| 1420 | netlink_table_ungrab(); |
| 1421 | return err; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 1424 | static void netlink_unbind(int group, long unsigned int groups, |
| 1425 | struct netlink_sock *nlk) |
| 1426 | { |
| 1427 | int undo; |
| 1428 | |
| 1429 | if (!nlk->netlink_unbind) |
| 1430 | return; |
| 1431 | |
| 1432 | for (undo = 0; undo < group; undo++) |
| 1433 | if (test_bit(group, &groups)) |
| 1434 | nlk->netlink_unbind(undo); |
| 1435 | } |
| 1436 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1437 | static int netlink_bind(struct socket *sock, struct sockaddr *addr, |
| 1438 | int addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | { |
| 1440 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1441 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1443 | struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr; |
| 1444 | int err; |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 1445 | long unsigned int groups = nladdr->nl_groups; |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1446 | |
Hannes Frederic Sowa | 4e4b537 | 2012-12-15 15:42:19 +0000 | [diff] [blame] | 1447 | if (addr_len < sizeof(struct sockaddr_nl)) |
| 1448 | return -EINVAL; |
| 1449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | if (nladdr->nl_family != AF_NETLINK) |
| 1451 | return -EINVAL; |
| 1452 | |
| 1453 | /* Only superuser is allowed to listen multicasts */ |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 1454 | if (groups) { |
Eric W. Biederman | 5187cd0 | 2014-04-23 14:25:48 -0700 | [diff] [blame] | 1455 | if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV)) |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1456 | return -EPERM; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1457 | err = netlink_realloc_groups(sk); |
| 1458 | if (err) |
| 1459 | return err; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1460 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 1462 | if (nlk->portid) |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1463 | if (nladdr->nl_pid != nlk->portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | return -EINVAL; |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 1465 | |
| 1466 | if (nlk->netlink_bind && groups) { |
| 1467 | int group; |
| 1468 | |
| 1469 | for (group = 0; group < nlk->ngroups; group++) { |
| 1470 | if (!test_bit(group, &groups)) |
| 1471 | continue; |
| 1472 | err = nlk->netlink_bind(group); |
| 1473 | if (!err) |
| 1474 | continue; |
| 1475 | netlink_unbind(group, groups, nlk); |
| 1476 | return err; |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | if (!nlk->portid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | err = nladdr->nl_pid ? |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1482 | netlink_insert(sk, net, nladdr->nl_pid) : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | netlink_autobind(sock); |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 1484 | if (err) { |
| 1485 | netlink_unbind(nlk->ngroups - 1, groups, nlk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | return err; |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 1487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 1490 | if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | return 0; |
| 1492 | |
| 1493 | netlink_table_grab(); |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1494 | netlink_update_subscriptions(sk, nlk->subscriptions + |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 1495 | hweight32(groups) - |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1496 | hweight32(nlk->groups[0])); |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 1497 | nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1498 | netlink_update_listeners(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | netlink_table_ungrab(); |
| 1500 | |
| 1501 | return 0; |
| 1502 | } |
| 1503 | |
| 1504 | static int netlink_connect(struct socket *sock, struct sockaddr *addr, |
| 1505 | int alen, int flags) |
| 1506 | { |
| 1507 | int err = 0; |
| 1508 | struct sock *sk = sock->sk; |
| 1509 | struct netlink_sock *nlk = nlk_sk(sk); |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1510 | struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | |
Changli Gao | 6503d96 | 2010-03-31 22:58:26 +0000 | [diff] [blame] | 1512 | if (alen < sizeof(addr->sa_family)) |
| 1513 | return -EINVAL; |
| 1514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | if (addr->sa_family == AF_UNSPEC) { |
| 1516 | sk->sk_state = NETLINK_UNCONNECTED; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1517 | nlk->dst_portid = 0; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 1518 | nlk->dst_group = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | return 0; |
| 1520 | } |
| 1521 | if (addr->sa_family != AF_NETLINK) |
| 1522 | return -EINVAL; |
| 1523 | |
Mike Pecovnik | 46833a8 | 2014-02-24 21:11:16 +0100 | [diff] [blame] | 1524 | if ((nladdr->nl_groups || nladdr->nl_pid) && |
Eric W. Biederman | 5187cd0 | 2014-04-23 14:25:48 -0700 | [diff] [blame] | 1525 | !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | return -EPERM; |
| 1527 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1528 | if (!nlk->portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | err = netlink_autobind(sock); |
| 1530 | |
| 1531 | if (err == 0) { |
| 1532 | sk->sk_state = NETLINK_CONNECTED; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1533 | nlk->dst_portid = nladdr->nl_pid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 1534 | nlk->dst_group = ffs(nladdr->nl_groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | return err; |
| 1538 | } |
| 1539 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1540 | static int netlink_getname(struct socket *sock, struct sockaddr *addr, |
| 1541 | int *addr_len, int peer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | { |
| 1543 | struct sock *sk = sock->sk; |
| 1544 | struct netlink_sock *nlk = nlk_sk(sk); |
Cyrill Gorcunov | 13cfa97 | 2009-11-08 05:51:19 +0000 | [diff] [blame] | 1545 | DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | nladdr->nl_family = AF_NETLINK; |
| 1548 | nladdr->nl_pad = 0; |
| 1549 | *addr_len = sizeof(*nladdr); |
| 1550 | |
| 1551 | if (peer) { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1552 | nladdr->nl_pid = nlk->dst_portid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 1553 | nladdr->nl_groups = netlink_group_mask(nlk->dst_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | } else { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1555 | nladdr->nl_pid = nlk->portid; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1556 | nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | } |
| 1558 | return 0; |
| 1559 | } |
| 1560 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1561 | static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | struct sock *sock; |
| 1564 | struct netlink_sock *nlk; |
| 1565 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1566 | sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | if (!sock) |
| 1568 | return ERR_PTR(-ECONNREFUSED); |
| 1569 | |
| 1570 | /* Don't bother queuing skb if kernel socket has no input function */ |
| 1571 | nlk = nlk_sk(sock); |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1572 | if (sock->sk_state == NETLINK_CONNECTED && |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1573 | nlk->dst_portid != nlk_sk(ssk)->portid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | sock_put(sock); |
| 1575 | return ERR_PTR(-ECONNREFUSED); |
| 1576 | } |
| 1577 | return sock; |
| 1578 | } |
| 1579 | |
| 1580 | struct sock *netlink_getsockbyfilp(struct file *filp) |
| 1581 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1582 | struct inode *inode = file_inode(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | struct sock *sock; |
| 1584 | |
| 1585 | if (!S_ISSOCK(inode->i_mode)) |
| 1586 | return ERR_PTR(-ENOTSOCK); |
| 1587 | |
| 1588 | sock = SOCKET_I(inode)->sk; |
| 1589 | if (sock->sk_family != AF_NETLINK) |
| 1590 | return ERR_PTR(-EINVAL); |
| 1591 | |
| 1592 | sock_hold(sock); |
| 1593 | return sock; |
| 1594 | } |
| 1595 | |
Pablo Neira | 3a36515 | 2013-06-28 03:04:23 +0200 | [diff] [blame] | 1596 | static struct sk_buff *netlink_alloc_large_skb(unsigned int size, |
| 1597 | int broadcast) |
Pablo Neira Ayuso | c05cdb1 | 2013-06-03 09:46:28 +0000 | [diff] [blame] | 1598 | { |
| 1599 | struct sk_buff *skb; |
| 1600 | void *data; |
| 1601 | |
Pablo Neira | 3a36515 | 2013-06-28 03:04:23 +0200 | [diff] [blame] | 1602 | if (size <= NLMSG_GOODSIZE || broadcast) |
Pablo Neira Ayuso | c05cdb1 | 2013-06-03 09:46:28 +0000 | [diff] [blame] | 1603 | return alloc_skb(size, GFP_KERNEL); |
| 1604 | |
Pablo Neira | 3a36515 | 2013-06-28 03:04:23 +0200 | [diff] [blame] | 1605 | size = SKB_DATA_ALIGN(size) + |
| 1606 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Pablo Neira Ayuso | c05cdb1 | 2013-06-03 09:46:28 +0000 | [diff] [blame] | 1607 | |
| 1608 | data = vmalloc(size); |
| 1609 | if (data == NULL) |
Pablo Neira | 3a36515 | 2013-06-28 03:04:23 +0200 | [diff] [blame] | 1610 | return NULL; |
Pablo Neira Ayuso | c05cdb1 | 2013-06-03 09:46:28 +0000 | [diff] [blame] | 1611 | |
Pablo Neira | 3a36515 | 2013-06-28 03:04:23 +0200 | [diff] [blame] | 1612 | skb = build_skb(data, size); |
| 1613 | if (skb == NULL) |
| 1614 | vfree(data); |
| 1615 | else { |
| 1616 | skb->head_frag = 0; |
| 1617 | skb->destructor = netlink_skb_destructor; |
| 1618 | } |
Pablo Neira Ayuso | c05cdb1 | 2013-06-03 09:46:28 +0000 | [diff] [blame] | 1619 | |
| 1620 | return skb; |
Pablo Neira Ayuso | c05cdb1 | 2013-06-03 09:46:28 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | /* |
| 1624 | * Attach a skb to a netlink socket. |
| 1625 | * The caller must hold a reference to the destination socket. On error, the |
| 1626 | * reference is dropped. The skb is not send to the destination, just all |
| 1627 | * all error checks are performed and memory in the queue is reserved. |
| 1628 | * Return values: |
| 1629 | * < 0: error. skb freed, reference to sock dropped. |
| 1630 | * 0: continue |
| 1631 | * 1: repeat lookup - reference dropped while waiting for socket memory. |
| 1632 | */ |
Denis V. Lunev | 9457afe | 2008-06-05 11:23:39 -0700 | [diff] [blame] | 1633 | int netlink_attachskb(struct sock *sk, struct sk_buff *skb, |
Patrick McHardy | c3d8d1e | 2007-11-07 02:42:09 -0800 | [diff] [blame] | 1634 | long *timeo, struct sock *ssk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | { |
| 1636 | struct netlink_sock *nlk; |
| 1637 | |
| 1638 | nlk = nlk_sk(sk); |
| 1639 | |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 1640 | if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || |
| 1641 | test_bit(NETLINK_CONGESTED, &nlk->state)) && |
| 1642 | !netlink_skb_is_mmaped(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | DECLARE_WAITQUEUE(wait, current); |
Patrick McHardy | c3d8d1e | 2007-11-07 02:42:09 -0800 | [diff] [blame] | 1644 | if (!*timeo) { |
Denis V. Lunev | aed8156 | 2007-10-10 21:14:32 -0700 | [diff] [blame] | 1645 | if (!ssk || netlink_is_kernel(ssk)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | netlink_overrun(sk); |
| 1647 | sock_put(sk); |
| 1648 | kfree_skb(skb); |
| 1649 | return -EAGAIN; |
| 1650 | } |
| 1651 | |
| 1652 | __set_current_state(TASK_INTERRUPTIBLE); |
| 1653 | add_wait_queue(&nlk->wait, &wait); |
| 1654 | |
| 1655 | if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || |
Patrick McHardy | cd967e0 | 2013-04-17 06:46:56 +0000 | [diff] [blame] | 1656 | test_bit(NETLINK_CONGESTED, &nlk->state)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | !sock_flag(sk, SOCK_DEAD)) |
Patrick McHardy | c3d8d1e | 2007-11-07 02:42:09 -0800 | [diff] [blame] | 1658 | *timeo = schedule_timeout(*timeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | |
| 1660 | __set_current_state(TASK_RUNNING); |
| 1661 | remove_wait_queue(&nlk->wait, &wait); |
| 1662 | sock_put(sk); |
| 1663 | |
| 1664 | if (signal_pending(current)) { |
| 1665 | kfree_skb(skb); |
Patrick McHardy | c3d8d1e | 2007-11-07 02:42:09 -0800 | [diff] [blame] | 1666 | return sock_intr_errno(*timeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | } |
| 1668 | return 1; |
| 1669 | } |
Patrick McHardy | cf0a018 | 2013-04-17 06:47:00 +0000 | [diff] [blame] | 1670 | netlink_skb_set_owner_r(skb, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | return 0; |
| 1672 | } |
| 1673 | |
Eric Dumazet | 4a7e7c2 | 2012-04-05 22:17:46 +0000 | [diff] [blame] | 1674 | static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | int len = skb->len; |
| 1677 | |
Daniel Borkmann | bcbde0d | 2013-06-21 19:38:07 +0200 | [diff] [blame] | 1678 | netlink_deliver_tap(skb); |
| 1679 | |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 1680 | #ifdef CONFIG_NETLINK_MMAP |
| 1681 | if (netlink_skb_is_mmaped(skb)) |
| 1682 | netlink_queue_mmaped_skb(sk, skb); |
| 1683 | else if (netlink_rx_is_mmaped(sk)) |
| 1684 | netlink_ring_set_copied(sk, skb); |
| 1685 | else |
| 1686 | #endif /* CONFIG_NETLINK_MMAP */ |
| 1687 | skb_queue_tail(&sk->sk_receive_queue, skb); |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 1688 | sk->sk_data_ready(sk); |
Eric Dumazet | 4a7e7c2 | 2012-04-05 22:17:46 +0000 | [diff] [blame] | 1689 | return len; |
| 1690 | } |
| 1691 | |
| 1692 | int netlink_sendskb(struct sock *sk, struct sk_buff *skb) |
| 1693 | { |
| 1694 | int len = __netlink_sendskb(sk, skb); |
| 1695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | sock_put(sk); |
| 1697 | return len; |
| 1698 | } |
| 1699 | |
| 1700 | void netlink_detachskb(struct sock *sk, struct sk_buff *skb) |
| 1701 | { |
| 1702 | kfree_skb(skb); |
| 1703 | sock_put(sk); |
| 1704 | } |
| 1705 | |
stephen hemminger | b57ef81f | 2011-12-22 08:52:02 +0000 | [diff] [blame] | 1706 | static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | { |
| 1708 | int delta; |
| 1709 | |
Patrick McHardy | 1298ca4 | 2013-04-17 06:46:59 +0000 | [diff] [blame] | 1710 | WARN_ON(skb->sk != NULL); |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 1711 | if (netlink_skb_is_mmaped(skb)) |
| 1712 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1714 | delta = skb->end - skb->tail; |
Pablo Neira Ayuso | c05cdb1 | 2013-06-03 09:46:28 +0000 | [diff] [blame] | 1715 | if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | return skb; |
| 1717 | |
| 1718 | if (skb_shared(skb)) { |
| 1719 | struct sk_buff *nskb = skb_clone(skb, allocation); |
| 1720 | if (!nskb) |
| 1721 | return skb; |
Eric Dumazet | 8460c00 | 2012-04-19 02:24:28 +0000 | [diff] [blame] | 1722 | consume_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | skb = nskb; |
| 1724 | } |
| 1725 | |
| 1726 | if (!pskb_expand_head(skb, 0, -delta, allocation)) |
| 1727 | skb->truesize -= delta; |
| 1728 | |
| 1729 | return skb; |
| 1730 | } |
| 1731 | |
Eric W. Biederman | 3fbc290 | 2012-05-24 17:21:27 -0600 | [diff] [blame] | 1732 | static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb, |
| 1733 | struct sock *ssk) |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1734 | { |
| 1735 | int ret; |
| 1736 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1737 | |
| 1738 | ret = -ECONNREFUSED; |
| 1739 | if (nlk->netlink_rcv != NULL) { |
| 1740 | ret = skb->len; |
Patrick McHardy | cf0a018 | 2013-04-17 06:47:00 +0000 | [diff] [blame] | 1741 | netlink_skb_set_owner_r(skb, sk); |
Patrick McHardy | e32123e | 2013-04-17 06:46:57 +0000 | [diff] [blame] | 1742 | NETLINK_CB(skb).sk = ssk; |
Daniel Borkmann | 73bfd37 | 2013-12-23 14:35:55 +0100 | [diff] [blame] | 1743 | netlink_deliver_tap_kernel(sk, ssk, skb); |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1744 | nlk->netlink_rcv(skb); |
Eric Dumazet | bfb253c | 2012-04-22 21:30:29 +0000 | [diff] [blame] | 1745 | consume_skb(skb); |
| 1746 | } else { |
| 1747 | kfree_skb(skb); |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1748 | } |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1749 | sock_put(sk); |
| 1750 | return ret; |
| 1751 | } |
| 1752 | |
| 1753 | int netlink_unicast(struct sock *ssk, struct sk_buff *skb, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1754 | u32 portid, int nonblock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | { |
| 1756 | struct sock *sk; |
| 1757 | int err; |
| 1758 | long timeo; |
| 1759 | |
| 1760 | skb = netlink_trim(skb, gfp_any()); |
| 1761 | |
| 1762 | timeo = sock_sndtimeo(ssk, nonblock); |
| 1763 | retry: |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1764 | sk = netlink_getsockbyportid(ssk, portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | if (IS_ERR(sk)) { |
| 1766 | kfree_skb(skb); |
| 1767 | return PTR_ERR(sk); |
| 1768 | } |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1769 | if (netlink_is_kernel(sk)) |
Eric W. Biederman | 3fbc290 | 2012-05-24 17:21:27 -0600 | [diff] [blame] | 1770 | return netlink_unicast_kernel(sk, skb, ssk); |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1771 | |
Stephen Hemminger | b1153f2 | 2008-03-21 15:46:12 -0700 | [diff] [blame] | 1772 | if (sk_filter(sk, skb)) { |
Wang Chen | 8487460 | 2008-07-01 19:55:09 -0700 | [diff] [blame] | 1773 | err = skb->len; |
Stephen Hemminger | b1153f2 | 2008-03-21 15:46:12 -0700 | [diff] [blame] | 1774 | kfree_skb(skb); |
| 1775 | sock_put(sk); |
| 1776 | return err; |
| 1777 | } |
| 1778 | |
Denis V. Lunev | 9457afe | 2008-06-05 11:23:39 -0700 | [diff] [blame] | 1779 | err = netlink_attachskb(sk, skb, &timeo, ssk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | if (err == 1) |
| 1781 | goto retry; |
| 1782 | if (err) |
| 1783 | return err; |
| 1784 | |
Denis V. Lunev | 7ee015e | 2007-10-10 21:14:03 -0700 | [diff] [blame] | 1785 | return netlink_sendskb(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1787 | EXPORT_SYMBOL(netlink_unicast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 1789 | struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size, |
| 1790 | u32 dst_portid, gfp_t gfp_mask) |
| 1791 | { |
| 1792 | #ifdef CONFIG_NETLINK_MMAP |
| 1793 | struct sock *sk = NULL; |
| 1794 | struct sk_buff *skb; |
| 1795 | struct netlink_ring *ring; |
| 1796 | struct nl_mmap_hdr *hdr; |
| 1797 | unsigned int maxlen; |
| 1798 | |
| 1799 | sk = netlink_getsockbyportid(ssk, dst_portid); |
| 1800 | if (IS_ERR(sk)) |
| 1801 | goto out; |
| 1802 | |
| 1803 | ring = &nlk_sk(sk)->rx_ring; |
| 1804 | /* fast-path without atomic ops for common case: non-mmaped receiver */ |
| 1805 | if (ring->pg_vec == NULL) |
| 1806 | goto out_put; |
| 1807 | |
Thomas Graf | aae9f0e | 2013-11-30 13:21:31 +0100 | [diff] [blame] | 1808 | if (ring->frame_size - NL_MMAP_HDRLEN < size) |
| 1809 | goto out_put; |
| 1810 | |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 1811 | skb = alloc_skb_head(gfp_mask); |
| 1812 | if (skb == NULL) |
| 1813 | goto err1; |
| 1814 | |
| 1815 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 1816 | /* check again under lock */ |
| 1817 | if (ring->pg_vec == NULL) |
| 1818 | goto out_free; |
| 1819 | |
Thomas Graf | aae9f0e | 2013-11-30 13:21:31 +0100 | [diff] [blame] | 1820 | /* check again under lock */ |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 1821 | maxlen = ring->frame_size - NL_MMAP_HDRLEN; |
| 1822 | if (maxlen < size) |
| 1823 | goto out_free; |
| 1824 | |
| 1825 | netlink_forward_ring(ring); |
| 1826 | hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED); |
| 1827 | if (hdr == NULL) |
| 1828 | goto err2; |
| 1829 | netlink_ring_setup_skb(skb, sk, ring, hdr); |
| 1830 | netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED); |
| 1831 | atomic_inc(&ring->pending); |
| 1832 | netlink_increment_head(ring); |
| 1833 | |
| 1834 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 1835 | return skb; |
| 1836 | |
| 1837 | err2: |
| 1838 | kfree_skb(skb); |
| 1839 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 1840 | netlink_overrun(sk); |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 1841 | err1: |
| 1842 | sock_put(sk); |
| 1843 | return NULL; |
| 1844 | |
| 1845 | out_free: |
| 1846 | kfree_skb(skb); |
| 1847 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 1848 | out_put: |
| 1849 | sock_put(sk); |
| 1850 | out: |
| 1851 | #endif |
| 1852 | return alloc_skb(size, gfp_mask); |
| 1853 | } |
| 1854 | EXPORT_SYMBOL_GPL(netlink_alloc_skb); |
| 1855 | |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1856 | int netlink_has_listeners(struct sock *sk, unsigned int group) |
| 1857 | { |
| 1858 | int res = 0; |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 1859 | struct listeners *listeners; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1860 | |
Denis V. Lunev | aed8156 | 2007-10-10 21:14:32 -0700 | [diff] [blame] | 1861 | BUG_ON(!netlink_is_kernel(sk)); |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1862 | |
| 1863 | rcu_read_lock(); |
| 1864 | listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners); |
| 1865 | |
Eric Dumazet | 6d772ac | 2012-10-18 03:21:55 +0000 | [diff] [blame] | 1866 | if (listeners && group - 1 < nl_table[sk->sk_protocol].groups) |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 1867 | res = test_bit(group - 1, listeners->masks); |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1868 | |
| 1869 | rcu_read_unlock(); |
| 1870 | |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1871 | return res; |
| 1872 | } |
| 1873 | EXPORT_SYMBOL_GPL(netlink_has_listeners); |
| 1874 | |
stephen hemminger | b57ef81f | 2011-12-22 08:52:02 +0000 | [diff] [blame] | 1875 | static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | { |
| 1877 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1878 | |
| 1879 | if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf && |
Patrick McHardy | cd967e0 | 2013-04-17 06:46:56 +0000 | [diff] [blame] | 1880 | !test_bit(NETLINK_CONGESTED, &nlk->state)) { |
Patrick McHardy | cf0a018 | 2013-04-17 06:47:00 +0000 | [diff] [blame] | 1881 | netlink_skb_set_owner_r(skb, sk); |
Eric Dumazet | 4a7e7c2 | 2012-04-05 22:17:46 +0000 | [diff] [blame] | 1882 | __netlink_sendskb(sk, skb); |
stephen hemminger | 2c645800 | 2011-12-22 08:52:03 +0000 | [diff] [blame] | 1883 | return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | } |
| 1885 | return -1; |
| 1886 | } |
| 1887 | |
| 1888 | struct netlink_broadcast_data { |
| 1889 | struct sock *exclude_sk; |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1890 | struct net *net; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1891 | u32 portid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | u32 group; |
| 1893 | int failure; |
Pablo Neira Ayuso | ff491a7 | 2009-02-05 23:56:36 -0800 | [diff] [blame] | 1894 | int delivery_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | int congested; |
| 1896 | int delivered; |
Al Viro | 7d877f3 | 2005-10-21 03:20:43 -0400 | [diff] [blame] | 1897 | gfp_t allocation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | struct sk_buff *skb, *skb2; |
Eric W. Biederman | 910a7e9 | 2010-05-04 17:36:46 -0700 | [diff] [blame] | 1899 | int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data); |
| 1900 | void *tx_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | }; |
| 1902 | |
Rami Rosen | 46c9521 | 2014-07-01 21:17:35 +0300 | [diff] [blame] | 1903 | static void do_one_broadcast(struct sock *sk, |
| 1904 | struct netlink_broadcast_data *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | { |
| 1906 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1907 | int val; |
| 1908 | |
| 1909 | if (p->exclude_sk == sk) |
Rami Rosen | 46c9521 | 2014-07-01 21:17:35 +0300 | [diff] [blame] | 1910 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1912 | if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups || |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1913 | !test_bit(p->group - 1, nlk->groups)) |
Rami Rosen | 46c9521 | 2014-07-01 21:17:35 +0300 | [diff] [blame] | 1914 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1916 | if (!net_eq(sock_net(sk), p->net)) |
Rami Rosen | 46c9521 | 2014-07-01 21:17:35 +0300 | [diff] [blame] | 1917 | return; |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | if (p->failure) { |
| 1920 | netlink_overrun(sk); |
Rami Rosen | 46c9521 | 2014-07-01 21:17:35 +0300 | [diff] [blame] | 1921 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | sock_hold(sk); |
| 1925 | if (p->skb2 == NULL) { |
Tommy S. Christensen | 68acc02 | 2005-05-19 13:06:35 -0700 | [diff] [blame] | 1926 | if (skb_shared(p->skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | p->skb2 = skb_clone(p->skb, p->allocation); |
| 1928 | } else { |
Tommy S. Christensen | 68acc02 | 2005-05-19 13:06:35 -0700 | [diff] [blame] | 1929 | p->skb2 = skb_get(p->skb); |
| 1930 | /* |
| 1931 | * skb ownership may have been set when |
| 1932 | * delivered to a previous socket. |
| 1933 | */ |
| 1934 | skb_orphan(p->skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | } |
| 1936 | } |
| 1937 | if (p->skb2 == NULL) { |
| 1938 | netlink_overrun(sk); |
| 1939 | /* Clone failed. Notify ALL listeners. */ |
| 1940 | p->failure = 1; |
Pablo Neira Ayuso | be0c22a | 2009-02-18 01:40:43 +0000 | [diff] [blame] | 1941 | if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR) |
| 1942 | p->delivery_failure = 1; |
Eric W. Biederman | 910a7e9 | 2010-05-04 17:36:46 -0700 | [diff] [blame] | 1943 | } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) { |
| 1944 | kfree_skb(p->skb2); |
| 1945 | p->skb2 = NULL; |
Stephen Hemminger | b1153f2 | 2008-03-21 15:46:12 -0700 | [diff] [blame] | 1946 | } else if (sk_filter(sk, p->skb2)) { |
| 1947 | kfree_skb(p->skb2); |
| 1948 | p->skb2 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) { |
| 1950 | netlink_overrun(sk); |
Pablo Neira Ayuso | be0c22a | 2009-02-18 01:40:43 +0000 | [diff] [blame] | 1951 | if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR) |
| 1952 | p->delivery_failure = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | } else { |
| 1954 | p->congested |= val; |
| 1955 | p->delivered = 1; |
| 1956 | p->skb2 = NULL; |
| 1957 | } |
| 1958 | sock_put(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1961 | int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid, |
Eric W. Biederman | 910a7e9 | 2010-05-04 17:36:46 -0700 | [diff] [blame] | 1962 | u32 group, gfp_t allocation, |
| 1963 | int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data), |
| 1964 | void *filter_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1966 | struct net *net = sock_net(ssk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | struct netlink_broadcast_data info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | struct sock *sk; |
| 1969 | |
| 1970 | skb = netlink_trim(skb, allocation); |
| 1971 | |
| 1972 | info.exclude_sk = ssk; |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1973 | info.net = net; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1974 | info.portid = portid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | info.group = group; |
| 1976 | info.failure = 0; |
Pablo Neira Ayuso | ff491a7 | 2009-02-05 23:56:36 -0800 | [diff] [blame] | 1977 | info.delivery_failure = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | info.congested = 0; |
| 1979 | info.delivered = 0; |
| 1980 | info.allocation = allocation; |
| 1981 | info.skb = skb; |
| 1982 | info.skb2 = NULL; |
Eric W. Biederman | 910a7e9 | 2010-05-04 17:36:46 -0700 | [diff] [blame] | 1983 | info.tx_filter = filter; |
| 1984 | info.tx_data = filter_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | |
| 1986 | /* While we sleep in clone, do not allow to change socket list */ |
| 1987 | |
| 1988 | netlink_lock_table(); |
| 1989 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1990 | sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | do_one_broadcast(sk, &info); |
| 1992 | |
Neil Horman | 70d4bf6 | 2010-07-20 06:45:56 +0000 | [diff] [blame] | 1993 | consume_skb(skb); |
Tommy S. Christensen | aa1c6a6 | 2005-05-19 13:07:32 -0700 | [diff] [blame] | 1994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | netlink_unlock_table(); |
| 1996 | |
Neil Horman | 70d4bf6 | 2010-07-20 06:45:56 +0000 | [diff] [blame] | 1997 | if (info.delivery_failure) { |
| 1998 | kfree_skb(info.skb2); |
Pablo Neira Ayuso | ff491a7 | 2009-02-05 23:56:36 -0800 | [diff] [blame] | 1999 | return -ENOBUFS; |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 2000 | } |
| 2001 | consume_skb(info.skb2); |
Pablo Neira Ayuso | ff491a7 | 2009-02-05 23:56:36 -0800 | [diff] [blame] | 2002 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | if (info.delivered) { |
| 2004 | if (info.congested && (allocation & __GFP_WAIT)) |
| 2005 | yield(); |
| 2006 | return 0; |
| 2007 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | return -ESRCH; |
| 2009 | } |
Eric W. Biederman | 910a7e9 | 2010-05-04 17:36:46 -0700 | [diff] [blame] | 2010 | EXPORT_SYMBOL(netlink_broadcast_filtered); |
| 2011 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2012 | int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid, |
Eric W. Biederman | 910a7e9 | 2010-05-04 17:36:46 -0700 | [diff] [blame] | 2013 | u32 group, gfp_t allocation) |
| 2014 | { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2015 | return netlink_broadcast_filtered(ssk, skb, portid, group, allocation, |
Eric W. Biederman | 910a7e9 | 2010-05-04 17:36:46 -0700 | [diff] [blame] | 2016 | NULL, NULL); |
| 2017 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2018 | EXPORT_SYMBOL(netlink_broadcast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | |
| 2020 | struct netlink_set_err_data { |
| 2021 | struct sock *exclude_sk; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2022 | u32 portid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | u32 group; |
| 2024 | int code; |
| 2025 | }; |
| 2026 | |
stephen hemminger | b57ef81f | 2011-12-22 08:52:02 +0000 | [diff] [blame] | 2027 | static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | { |
| 2029 | struct netlink_sock *nlk = nlk_sk(sk); |
Pablo Neira Ayuso | 1a50307 | 2010-03-18 14:24:42 +0000 | [diff] [blame] | 2030 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | |
| 2032 | if (sk == p->exclude_sk) |
| 2033 | goto out; |
| 2034 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 2035 | if (!net_eq(sock_net(sk), sock_net(p->exclude_sk))) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2036 | goto out; |
| 2037 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2038 | if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups || |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 2039 | !test_bit(p->group - 1, nlk->groups)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | goto out; |
| 2041 | |
Pablo Neira Ayuso | 1a50307 | 2010-03-18 14:24:42 +0000 | [diff] [blame] | 2042 | if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) { |
| 2043 | ret = 1; |
| 2044 | goto out; |
| 2045 | } |
| 2046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | sk->sk_err = p->code; |
| 2048 | sk->sk_error_report(sk); |
| 2049 | out: |
Pablo Neira Ayuso | 1a50307 | 2010-03-18 14:24:42 +0000 | [diff] [blame] | 2050 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
Pablo Neira Ayuso | 4843b93 | 2009-03-03 23:37:30 -0800 | [diff] [blame] | 2053 | /** |
| 2054 | * netlink_set_err - report error to broadcast listeners |
| 2055 | * @ssk: the kernel netlink socket, as returned by netlink_kernel_create() |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2056 | * @portid: the PORTID of a process that we want to skip (if any) |
Johannes Berg | 840e93f2 | 2013-11-19 10:35:40 +0100 | [diff] [blame] | 2057 | * @group: the broadcast group that will notice the error |
Pablo Neira Ayuso | 4843b93 | 2009-03-03 23:37:30 -0800 | [diff] [blame] | 2058 | * @code: error code, must be negative (as usual in kernelspace) |
Pablo Neira Ayuso | 1a50307 | 2010-03-18 14:24:42 +0000 | [diff] [blame] | 2059 | * |
| 2060 | * This function returns the number of broadcast listeners that have set the |
| 2061 | * NETLINK_RECV_NO_ENOBUFS socket option. |
Pablo Neira Ayuso | 4843b93 | 2009-03-03 23:37:30 -0800 | [diff] [blame] | 2062 | */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2063 | int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | { |
| 2065 | struct netlink_set_err_data info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | struct sock *sk; |
Pablo Neira Ayuso | 1a50307 | 2010-03-18 14:24:42 +0000 | [diff] [blame] | 2067 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | |
| 2069 | info.exclude_sk = ssk; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2070 | info.portid = portid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | info.group = group; |
Pablo Neira Ayuso | 4843b93 | 2009-03-03 23:37:30 -0800 | [diff] [blame] | 2072 | /* sk->sk_err wants a positive error value */ |
| 2073 | info.code = -code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | |
| 2075 | read_lock(&nl_table_lock); |
| 2076 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2077 | sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list) |
Pablo Neira Ayuso | 1a50307 | 2010-03-18 14:24:42 +0000 | [diff] [blame] | 2078 | ret += do_one_set_err(sk, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | |
| 2080 | read_unlock(&nl_table_lock); |
Pablo Neira Ayuso | 1a50307 | 2010-03-18 14:24:42 +0000 | [diff] [blame] | 2081 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | } |
Pablo Neira Ayuso | dd5b6ce | 2009-03-23 13:21:06 +0100 | [diff] [blame] | 2083 | EXPORT_SYMBOL(netlink_set_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | |
Johannes Berg | 84659eb | 2007-07-18 15:47:05 -0700 | [diff] [blame] | 2085 | /* must be called with netlink table grabbed */ |
| 2086 | static void netlink_update_socket_mc(struct netlink_sock *nlk, |
| 2087 | unsigned int group, |
| 2088 | int is_new) |
| 2089 | { |
| 2090 | int old, new = !!is_new, subscriptions; |
| 2091 | |
| 2092 | old = test_bit(group - 1, nlk->groups); |
| 2093 | subscriptions = nlk->subscriptions - old + new; |
| 2094 | if (new) |
| 2095 | __set_bit(group - 1, nlk->groups); |
| 2096 | else |
| 2097 | __clear_bit(group - 1, nlk->groups); |
| 2098 | netlink_update_subscriptions(&nlk->sk, subscriptions); |
| 2099 | netlink_update_listeners(&nlk->sk); |
| 2100 | } |
| 2101 | |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2102 | static int netlink_setsockopt(struct socket *sock, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2103 | char __user *optval, unsigned int optlen) |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2104 | { |
| 2105 | struct sock *sk = sock->sk; |
| 2106 | struct netlink_sock *nlk = nlk_sk(sk); |
Johannes Berg | eb49653 | 2007-07-18 02:07:51 -0700 | [diff] [blame] | 2107 | unsigned int val = 0; |
| 2108 | int err; |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2109 | |
| 2110 | if (level != SOL_NETLINK) |
| 2111 | return -ENOPROTOOPT; |
| 2112 | |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 2113 | if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING && |
| 2114 | optlen >= sizeof(int) && |
Johannes Berg | eb49653 | 2007-07-18 02:07:51 -0700 | [diff] [blame] | 2115 | get_user(val, (unsigned int __user *)optval)) |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2116 | return -EFAULT; |
| 2117 | |
| 2118 | switch (optname) { |
| 2119 | case NETLINK_PKTINFO: |
| 2120 | if (val) |
| 2121 | nlk->flags |= NETLINK_RECV_PKTINFO; |
| 2122 | else |
| 2123 | nlk->flags &= ~NETLINK_RECV_PKTINFO; |
| 2124 | err = 0; |
| 2125 | break; |
| 2126 | case NETLINK_ADD_MEMBERSHIP: |
| 2127 | case NETLINK_DROP_MEMBERSHIP: { |
Eric W. Biederman | 5187cd0 | 2014-04-23 14:25:48 -0700 | [diff] [blame] | 2128 | if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV)) |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2129 | return -EPERM; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2130 | err = netlink_realloc_groups(sk); |
| 2131 | if (err) |
| 2132 | return err; |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2133 | if (!val || val - 1 >= nlk->ngroups) |
| 2134 | return -EINVAL; |
Richard Guy Briggs | 7774d5e | 2014-04-22 21:31:55 -0400 | [diff] [blame] | 2135 | if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) { |
Richard Guy Briggs | 4f52090 | 2014-04-22 21:31:54 -0400 | [diff] [blame] | 2136 | err = nlk->netlink_bind(val); |
| 2137 | if (err) |
| 2138 | return err; |
| 2139 | } |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2140 | netlink_table_grab(); |
Johannes Berg | 84659eb | 2007-07-18 15:47:05 -0700 | [diff] [blame] | 2141 | netlink_update_socket_mc(nlk, val, |
| 2142 | optname == NETLINK_ADD_MEMBERSHIP); |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2143 | netlink_table_ungrab(); |
Richard Guy Briggs | 7774d5e | 2014-04-22 21:31:55 -0400 | [diff] [blame] | 2144 | if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind) |
| 2145 | nlk->netlink_unbind(val); |
Pablo Neira Ayuso | 0329274 | 2012-06-29 06:15:22 +0000 | [diff] [blame] | 2146 | |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2147 | err = 0; |
| 2148 | break; |
| 2149 | } |
Pablo Neira Ayuso | be0c22a | 2009-02-18 01:40:43 +0000 | [diff] [blame] | 2150 | case NETLINK_BROADCAST_ERROR: |
| 2151 | if (val) |
| 2152 | nlk->flags |= NETLINK_BROADCAST_SEND_ERROR; |
| 2153 | else |
| 2154 | nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR; |
| 2155 | err = 0; |
| 2156 | break; |
Pablo Neira Ayuso | 38938bf | 2009-03-24 16:37:55 -0700 | [diff] [blame] | 2157 | case NETLINK_NO_ENOBUFS: |
| 2158 | if (val) { |
| 2159 | nlk->flags |= NETLINK_RECV_NO_ENOBUFS; |
Patrick McHardy | cd967e0 | 2013-04-17 06:46:56 +0000 | [diff] [blame] | 2160 | clear_bit(NETLINK_CONGESTED, &nlk->state); |
Pablo Neira Ayuso | 38938bf | 2009-03-24 16:37:55 -0700 | [diff] [blame] | 2161 | wake_up_interruptible(&nlk->wait); |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 2162 | } else { |
Pablo Neira Ayuso | 38938bf | 2009-03-24 16:37:55 -0700 | [diff] [blame] | 2163 | nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS; |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 2164 | } |
Pablo Neira Ayuso | 38938bf | 2009-03-24 16:37:55 -0700 | [diff] [blame] | 2165 | err = 0; |
| 2166 | break; |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 2167 | #ifdef CONFIG_NETLINK_MMAP |
| 2168 | case NETLINK_RX_RING: |
| 2169 | case NETLINK_TX_RING: { |
| 2170 | struct nl_mmap_req req; |
| 2171 | |
| 2172 | /* Rings might consume more memory than queue limits, require |
| 2173 | * CAP_NET_ADMIN. |
| 2174 | */ |
| 2175 | if (!capable(CAP_NET_ADMIN)) |
| 2176 | return -EPERM; |
| 2177 | if (optlen < sizeof(req)) |
| 2178 | return -EINVAL; |
| 2179 | if (copy_from_user(&req, optval, sizeof(req))) |
| 2180 | return -EFAULT; |
| 2181 | err = netlink_set_ring(sk, &req, false, |
| 2182 | optname == NETLINK_TX_RING); |
| 2183 | break; |
| 2184 | } |
| 2185 | #endif /* CONFIG_NETLINK_MMAP */ |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2186 | default: |
| 2187 | err = -ENOPROTOOPT; |
| 2188 | } |
| 2189 | return err; |
| 2190 | } |
| 2191 | |
| 2192 | static int netlink_getsockopt(struct socket *sock, int level, int optname, |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 2193 | char __user *optval, int __user *optlen) |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2194 | { |
| 2195 | struct sock *sk = sock->sk; |
| 2196 | struct netlink_sock *nlk = nlk_sk(sk); |
| 2197 | int len, val, err; |
| 2198 | |
| 2199 | if (level != SOL_NETLINK) |
| 2200 | return -ENOPROTOOPT; |
| 2201 | |
| 2202 | if (get_user(len, optlen)) |
| 2203 | return -EFAULT; |
| 2204 | if (len < 0) |
| 2205 | return -EINVAL; |
| 2206 | |
| 2207 | switch (optname) { |
| 2208 | case NETLINK_PKTINFO: |
| 2209 | if (len < sizeof(int)) |
| 2210 | return -EINVAL; |
| 2211 | len = sizeof(int); |
| 2212 | val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0; |
Heiko Carstens | a27b58f | 2006-10-30 15:06:12 -0800 | [diff] [blame] | 2213 | if (put_user(len, optlen) || |
| 2214 | put_user(val, optval)) |
| 2215 | return -EFAULT; |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2216 | err = 0; |
| 2217 | break; |
Pablo Neira Ayuso | be0c22a | 2009-02-18 01:40:43 +0000 | [diff] [blame] | 2218 | case NETLINK_BROADCAST_ERROR: |
| 2219 | if (len < sizeof(int)) |
| 2220 | return -EINVAL; |
| 2221 | len = sizeof(int); |
| 2222 | val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0; |
| 2223 | if (put_user(len, optlen) || |
| 2224 | put_user(val, optval)) |
| 2225 | return -EFAULT; |
| 2226 | err = 0; |
| 2227 | break; |
Pablo Neira Ayuso | 38938bf | 2009-03-24 16:37:55 -0700 | [diff] [blame] | 2228 | case NETLINK_NO_ENOBUFS: |
| 2229 | if (len < sizeof(int)) |
| 2230 | return -EINVAL; |
| 2231 | len = sizeof(int); |
| 2232 | val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0; |
| 2233 | if (put_user(len, optlen) || |
| 2234 | put_user(val, optval)) |
| 2235 | return -EFAULT; |
| 2236 | err = 0; |
| 2237 | break; |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2238 | default: |
| 2239 | err = -ENOPROTOOPT; |
| 2240 | } |
| 2241 | return err; |
| 2242 | } |
| 2243 | |
| 2244 | static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) |
| 2245 | { |
| 2246 | struct nl_pktinfo info; |
| 2247 | |
| 2248 | info.group = NETLINK_CB(skb).dst_group; |
| 2249 | put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info); |
| 2250 | } |
| 2251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock, |
| 2253 | struct msghdr *msg, size_t len) |
| 2254 | { |
| 2255 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); |
| 2256 | struct sock *sk = sock->sk; |
| 2257 | struct netlink_sock *nlk = nlk_sk(sk); |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 2258 | DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2259 | u32 dst_portid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 2260 | u32 dst_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | struct sk_buff *skb; |
| 2262 | int err; |
| 2263 | struct scm_cookie scm; |
Eric W. Biederman | 2d7a85f | 2014-05-30 11:04:00 -0700 | [diff] [blame] | 2264 | u32 netlink_skb_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | |
| 2266 | if (msg->msg_flags&MSG_OOB) |
| 2267 | return -EOPNOTSUPP; |
| 2268 | |
Eric Dumazet | 16e5726 | 2011-09-19 05:52:27 +0000 | [diff] [blame] | 2269 | if (NULL == siocb->scm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | siocb->scm = &scm; |
Eric Dumazet | 16e5726 | 2011-09-19 05:52:27 +0000 | [diff] [blame] | 2271 | |
Eric Dumazet | e0e3cea | 2012-08-21 06:21:17 +0000 | [diff] [blame] | 2272 | err = scm_send(sock, msg, siocb->scm, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | if (err < 0) |
| 2274 | return err; |
| 2275 | |
| 2276 | if (msg->msg_namelen) { |
Eric W. Biederman | b47030c | 2010-06-13 03:31:06 +0000 | [diff] [blame] | 2277 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | if (addr->nl_family != AF_NETLINK) |
Eric W. Biederman | b47030c | 2010-06-13 03:31:06 +0000 | [diff] [blame] | 2279 | goto out; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2280 | dst_portid = addr->nl_pid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 2281 | dst_group = ffs(addr->nl_groups); |
Eric W. Biederman | b47030c | 2010-06-13 03:31:06 +0000 | [diff] [blame] | 2282 | err = -EPERM; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2283 | if ((dst_group || dst_portid) && |
Eric W. Biederman | 5187cd0 | 2014-04-23 14:25:48 -0700 | [diff] [blame] | 2284 | !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND)) |
Eric W. Biederman | b47030c | 2010-06-13 03:31:06 +0000 | [diff] [blame] | 2285 | goto out; |
Eric W. Biederman | 2d7a85f | 2014-05-30 11:04:00 -0700 | [diff] [blame] | 2286 | netlink_skb_flags |= NETLINK_SKB_DST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | } else { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2288 | dst_portid = nlk->dst_portid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 2289 | dst_group = nlk->dst_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2292 | if (!nlk->portid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | err = netlink_autobind(sock); |
| 2294 | if (err) |
| 2295 | goto out; |
| 2296 | } |
| 2297 | |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 2298 | if (netlink_tx_is_mmaped(sk) && |
| 2299 | msg->msg_iov->iov_base == NULL) { |
| 2300 | err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, |
| 2301 | siocb); |
| 2302 | goto out; |
| 2303 | } |
| 2304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | err = -EMSGSIZE; |
| 2306 | if (len > sk->sk_sndbuf - 32) |
| 2307 | goto out; |
| 2308 | err = -ENOBUFS; |
Pablo Neira | 3a36515 | 2013-06-28 03:04:23 +0200 | [diff] [blame] | 2309 | skb = netlink_alloc_large_skb(len, dst_group); |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2310 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | goto out; |
| 2312 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2313 | NETLINK_CB(skb).portid = nlk->portid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 2314 | NETLINK_CB(skb).dst_group = dst_group; |
Eric W. Biederman | dbe9a41 | 2012-09-06 18:20:01 +0000 | [diff] [blame] | 2315 | NETLINK_CB(skb).creds = siocb->scm->creds; |
Eric W. Biederman | 2d7a85f | 2014-05-30 11:04:00 -0700 | [diff] [blame] | 2316 | NETLINK_CB(skb).flags = netlink_skb_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | err = -EFAULT; |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2319 | if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | kfree_skb(skb); |
| 2321 | goto out; |
| 2322 | } |
| 2323 | |
| 2324 | err = security_netlink_send(sk, skb); |
| 2325 | if (err) { |
| 2326 | kfree_skb(skb); |
| 2327 | goto out; |
| 2328 | } |
| 2329 | |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 2330 | if (dst_group) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | atomic_inc(&skb->users); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2332 | netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | } |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2334 | err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | |
| 2336 | out: |
Eric W. Biederman | b47030c | 2010-06-13 03:31:06 +0000 | [diff] [blame] | 2337 | scm_destroy(siocb->scm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | return err; |
| 2339 | } |
| 2340 | |
| 2341 | static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock, |
| 2342 | struct msghdr *msg, size_t len, |
| 2343 | int flags) |
| 2344 | { |
| 2345 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); |
| 2346 | struct scm_cookie scm; |
| 2347 | struct sock *sk = sock->sk; |
| 2348 | struct netlink_sock *nlk = nlk_sk(sk); |
| 2349 | int noblock = flags&MSG_DONTWAIT; |
| 2350 | size_t copied; |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2351 | struct sk_buff *skb, *data_skb; |
Andrey Vagin | b44d211 | 2011-02-21 02:40:47 +0000 | [diff] [blame] | 2352 | int err, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | |
| 2354 | if (flags&MSG_OOB) |
| 2355 | return -EOPNOTSUPP; |
| 2356 | |
| 2357 | copied = 0; |
| 2358 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2359 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 2360 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | goto out; |
| 2362 | |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2363 | data_skb = skb; |
| 2364 | |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 2365 | #ifdef CONFIG_COMPAT_NETLINK_MESSAGES |
| 2366 | if (unlikely(skb_shinfo(skb)->frag_list)) { |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 2367 | /* |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2368 | * If this skb has a frag_list, then here that means that we |
| 2369 | * will have to use the frag_list skb's data for compat tasks |
| 2370 | * and the regular skb's data for normal (non-compat) tasks. |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 2371 | * |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2372 | * If we need to send the compat skb, assign it to the |
| 2373 | * 'data_skb' variable so that it will be used below for data |
| 2374 | * copying. We keep 'skb' for everything else, including |
| 2375 | * freeing both later. |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 2376 | */ |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2377 | if (flags & MSG_CMSG_COMPAT) |
| 2378 | data_skb = skb_shinfo(skb)->frag_list; |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 2379 | } |
| 2380 | #endif |
| 2381 | |
Eric Dumazet | 9063e21 | 2014-03-07 12:02:33 -0800 | [diff] [blame] | 2382 | /* Record the max length of recvmsg() calls for future allocations */ |
| 2383 | nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len); |
| 2384 | nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len, |
| 2385 | 16384); |
| 2386 | |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2387 | copied = data_skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | if (len < copied) { |
| 2389 | msg->msg_flags |= MSG_TRUNC; |
| 2390 | copied = len; |
| 2391 | } |
| 2392 | |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2393 | skb_reset_transport_header(data_skb); |
| 2394 | err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | |
| 2396 | if (msg->msg_name) { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 2397 | DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | addr->nl_family = AF_NETLINK; |
| 2399 | addr->nl_pad = 0; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2400 | addr->nl_pid = NETLINK_CB(skb).portid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 2401 | addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | msg->msg_namelen = sizeof(*addr); |
| 2403 | } |
| 2404 | |
Patrick McHardy | cc9a06c | 2006-03-12 20:34:27 -0800 | [diff] [blame] | 2405 | if (nlk->flags & NETLINK_RECV_PKTINFO) |
| 2406 | netlink_cmsg_recv_pktinfo(msg, skb); |
| 2407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | if (NULL == siocb->scm) { |
| 2409 | memset(&scm, 0, sizeof(scm)); |
| 2410 | siocb->scm = &scm; |
| 2411 | } |
| 2412 | siocb->scm->creds = *NETLINK_CREDS(skb); |
Patrick McHardy | 188ccb5 | 2007-05-03 03:27:01 -0700 | [diff] [blame] | 2413 | if (flags & MSG_TRUNC) |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2414 | copied = data_skb->len; |
David S. Miller | daa3766 | 2010-08-15 23:21:50 -0700 | [diff] [blame] | 2415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | skb_free_datagram(sk, skb); |
| 2417 | |
Pravin B Shelar | 16b304f | 2013-08-15 15:31:06 -0700 | [diff] [blame] | 2418 | if (nlk->cb_running && |
| 2419 | atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) { |
Andrey Vagin | b44d211 | 2011-02-21 02:40:47 +0000 | [diff] [blame] | 2420 | ret = netlink_dump(sk); |
| 2421 | if (ret) { |
Ben Pfaff | ac30ef8 | 2014-07-09 10:31:22 -0700 | [diff] [blame] | 2422 | sk->sk_err = -ret; |
Andrey Vagin | b44d211 | 2011-02-21 02:40:47 +0000 | [diff] [blame] | 2423 | sk->sk_error_report(sk); |
| 2424 | } |
| 2425 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | |
| 2427 | scm_recv(sock, msg, siocb->scm, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | out: |
| 2429 | netlink_rcv_wake(sk); |
| 2430 | return err ? : copied; |
| 2431 | } |
| 2432 | |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 2433 | static void netlink_data_ready(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | { |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 2435 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | /* |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 2439 | * We export these functions to other modules. They provide a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | * complete set of kernel non-blocking support for message |
| 2441 | * queueing. |
| 2442 | */ |
| 2443 | |
| 2444 | struct sock * |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 2445 | __netlink_kernel_create(struct net *net, int unit, struct module *module, |
| 2446 | struct netlink_kernel_cfg *cfg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | { |
| 2448 | struct socket *sock; |
| 2449 | struct sock *sk; |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 2450 | struct netlink_sock *nlk; |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2451 | struct listeners *listeners = NULL; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 2452 | struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL; |
| 2453 | unsigned int groups; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | |
Akinobu Mita | fab2caf | 2006-08-29 02:15:24 -0700 | [diff] [blame] | 2455 | BUG_ON(!nl_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2457 | if (unit < 0 || unit >= MAX_LINKS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | return NULL; |
| 2459 | |
| 2460 | if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock)) |
| 2461 | return NULL; |
| 2462 | |
Pavel Emelyanov | 23fe186 | 2008-01-30 19:31:06 -0800 | [diff] [blame] | 2463 | /* |
| 2464 | * We have to just have a reference on the net from sk, but don't |
| 2465 | * get_net it. Besides, we cannot get and then put the net here. |
| 2466 | * So we create one inside init_net and the move it to net. |
| 2467 | */ |
| 2468 | |
| 2469 | if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0) |
| 2470 | goto out_sock_release_nosk; |
| 2471 | |
| 2472 | sk = sock->sk; |
Denis V. Lunev | edf0208 | 2008-02-29 11:18:32 -0800 | [diff] [blame] | 2473 | sk_change_net(sk, net); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 2474 | |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 2475 | if (!cfg || cfg->groups < 32) |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 2476 | groups = 32; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 2477 | else |
| 2478 | groups = cfg->groups; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 2479 | |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2480 | listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL); |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 2481 | if (!listeners) |
| 2482 | goto out_sock_release; |
| 2483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | sk->sk_data_ready = netlink_data_ready; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 2485 | if (cfg && cfg->input) |
| 2486 | nlk_sk(sk)->netlink_rcv = cfg->input; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2487 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2488 | if (netlink_insert(sk, net, 0)) |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 2489 | goto out_sock_release; |
| 2490 | |
| 2491 | nlk = nlk_sk(sk); |
| 2492 | nlk->flags |= NETLINK_KERNEL_SOCKET; |
| 2493 | |
| 2494 | netlink_table_grab(); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2495 | if (!nl_table[unit].registered) { |
| 2496 | nl_table[unit].groups = groups; |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2497 | rcu_assign_pointer(nl_table[unit].listeners, listeners); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2498 | nl_table[unit].cb_mutex = cb_mutex; |
| 2499 | nl_table[unit].module = module; |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 2500 | if (cfg) { |
| 2501 | nl_table[unit].bind = cfg->bind; |
| 2502 | nl_table[unit].flags = cfg->flags; |
Gao feng | da12c90 | 2013-06-06 14:49:11 +0800 | [diff] [blame] | 2503 | if (cfg->compare) |
| 2504 | nl_table[unit].compare = cfg->compare; |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 2505 | } |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2506 | nl_table[unit].registered = 1; |
Jesper Juhl | f937f1f46 | 2007-10-15 01:39:12 -0700 | [diff] [blame] | 2507 | } else { |
| 2508 | kfree(listeners); |
Denis V. Lunev | 869e58f | 2008-01-18 23:53:31 -0800 | [diff] [blame] | 2509 | nl_table[unit].registered++; |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2510 | } |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 2511 | netlink_table_ungrab(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 2512 | return sk; |
| 2513 | |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 2514 | out_sock_release: |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 2515 | kfree(listeners); |
Denis V. Lunev | 9dfbec1 | 2008-02-29 11:17:56 -0800 | [diff] [blame] | 2516 | netlink_kernel_release(sk); |
Pavel Emelyanov | 23fe186 | 2008-01-30 19:31:06 -0800 | [diff] [blame] | 2517 | return NULL; |
| 2518 | |
| 2519 | out_sock_release_nosk: |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 2520 | sock_release(sock); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 2521 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | } |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 2523 | EXPORT_SYMBOL(__netlink_kernel_create); |
Denis V. Lunev | b7c6ba6 | 2008-01-28 14:41:19 -0800 | [diff] [blame] | 2524 | |
| 2525 | void |
| 2526 | netlink_kernel_release(struct sock *sk) |
| 2527 | { |
Denis V. Lunev | edf0208 | 2008-02-29 11:18:32 -0800 | [diff] [blame] | 2528 | sk_release_kernel(sk); |
Denis V. Lunev | b7c6ba6 | 2008-01-28 14:41:19 -0800 | [diff] [blame] | 2529 | } |
| 2530 | EXPORT_SYMBOL(netlink_kernel_release); |
| 2531 | |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 2532 | int __netlink_change_ngroups(struct sock *sk, unsigned int groups) |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2533 | { |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2534 | struct listeners *new, *old; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2535 | struct netlink_table *tbl = &nl_table[sk->sk_protocol]; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2536 | |
| 2537 | if (groups < 32) |
| 2538 | groups = 32; |
| 2539 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2540 | if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) { |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2541 | new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC); |
| 2542 | if (!new) |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 2543 | return -ENOMEM; |
Eric Dumazet | 6d772ac | 2012-10-18 03:21:55 +0000 | [diff] [blame] | 2544 | old = nl_deref_protected(tbl->listeners); |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2545 | memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups)); |
| 2546 | rcu_assign_pointer(tbl->listeners, new); |
| 2547 | |
Lai Jiangshan | 37b6b93 | 2011-03-15 18:01:42 +0800 | [diff] [blame] | 2548 | kfree_rcu(old, rcu); |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2549 | } |
| 2550 | tbl->groups = groups; |
| 2551 | |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 2552 | return 0; |
| 2553 | } |
| 2554 | |
| 2555 | /** |
| 2556 | * netlink_change_ngroups - change number of multicast groups |
| 2557 | * |
| 2558 | * This changes the number of multicast groups that are available |
| 2559 | * on a certain netlink family. Note that it is not possible to |
| 2560 | * change the number of groups to below 32. Also note that it does |
| 2561 | * not implicitly call netlink_clear_multicast_users() when the |
| 2562 | * number of groups is reduced. |
| 2563 | * |
| 2564 | * @sk: The kernel netlink socket, as returned by netlink_kernel_create(). |
| 2565 | * @groups: The new number of groups. |
| 2566 | */ |
| 2567 | int netlink_change_ngroups(struct sock *sk, unsigned int groups) |
| 2568 | { |
| 2569 | int err; |
| 2570 | |
| 2571 | netlink_table_grab(); |
| 2572 | err = __netlink_change_ngroups(sk, groups); |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2573 | netlink_table_ungrab(); |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 2574 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2575 | return err; |
| 2576 | } |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2577 | |
Johannes Berg | b827357 | 2009-09-24 15:44:05 -0700 | [diff] [blame] | 2578 | void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group) |
| 2579 | { |
| 2580 | struct sock *sk; |
Johannes Berg | b827357 | 2009-09-24 15:44:05 -0700 | [diff] [blame] | 2581 | struct netlink_table *tbl = &nl_table[ksk->sk_protocol]; |
| 2582 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2583 | sk_for_each_bound(sk, &tbl->mc_list) |
Johannes Berg | b827357 | 2009-09-24 15:44:05 -0700 | [diff] [blame] | 2584 | netlink_update_socket_mc(nlk_sk(sk), group, 0); |
| 2585 | } |
| 2586 | |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 2587 | struct nlmsghdr * |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2588 | __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags) |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 2589 | { |
| 2590 | struct nlmsghdr *nlh; |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 2591 | int size = nlmsg_msg_size(len); |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 2592 | |
Wang Yufen | 23b4567 | 2014-02-17 16:53:32 +0800 | [diff] [blame] | 2593 | nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size)); |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 2594 | nlh->nlmsg_type = type; |
| 2595 | nlh->nlmsg_len = size; |
| 2596 | nlh->nlmsg_flags = flags; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2597 | nlh->nlmsg_pid = portid; |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 2598 | nlh->nlmsg_seq = seq; |
| 2599 | if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0) |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 2600 | memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size); |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 2601 | return nlh; |
| 2602 | } |
| 2603 | EXPORT_SYMBOL(__nlmsg_put); |
| 2604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | /* |
| 2606 | * It looks a bit ugly. |
| 2607 | * It would be better to create kernel thread. |
| 2608 | */ |
| 2609 | |
| 2610 | static int netlink_dump(struct sock *sk) |
| 2611 | { |
| 2612 | struct netlink_sock *nlk = nlk_sk(sk); |
| 2613 | struct netlink_callback *cb; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2614 | struct sk_buff *skb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | struct nlmsghdr *nlh; |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2616 | int len, err = -ENOBUFS; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2617 | int alloc_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 2619 | mutex_lock(nlk->cb_mutex); |
Pravin B Shelar | 16b304f | 2013-08-15 15:31:06 -0700 | [diff] [blame] | 2620 | if (!nlk->cb_running) { |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2621 | err = -EINVAL; |
| 2622 | goto errout_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | } |
| 2624 | |
Pravin B Shelar | 16b304f | 2013-08-15 15:31:06 -0700 | [diff] [blame] | 2625 | cb = &nlk->cb; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2626 | alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE); |
| 2627 | |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 2628 | if (!netlink_rx_is_mmaped(sk) && |
| 2629 | atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) |
| 2630 | goto errout_skb; |
Eric Dumazet | 9063e21 | 2014-03-07 12:02:33 -0800 | [diff] [blame] | 2631 | |
| 2632 | /* NLMSG_GOODSIZE is small to avoid high order allocations being |
| 2633 | * required, but it makes sense to _attempt_ a 16K bytes allocation |
| 2634 | * to reduce number of system calls on dump operations, if user |
| 2635 | * ever provided a big enough buffer. |
| 2636 | */ |
| 2637 | if (alloc_size < nlk->max_recvmsg_len) { |
| 2638 | skb = netlink_alloc_skb(sk, |
| 2639 | nlk->max_recvmsg_len, |
| 2640 | nlk->portid, |
| 2641 | GFP_KERNEL | |
| 2642 | __GFP_NOWARN | |
| 2643 | __GFP_NORETRY); |
| 2644 | /* available room should be exact amount to avoid MSG_TRUNC */ |
| 2645 | if (skb) |
| 2646 | skb_reserve(skb, skb_tailroom(skb) - |
| 2647 | nlk->max_recvmsg_len); |
| 2648 | } |
| 2649 | if (!skb) |
| 2650 | skb = netlink_alloc_skb(sk, alloc_size, nlk->portid, |
| 2651 | GFP_KERNEL); |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2652 | if (!skb) |
Dan Carpenter | c63d6ea | 2011-06-15 03:11:42 +0000 | [diff] [blame] | 2653 | goto errout_skb; |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 2654 | netlink_skb_set_owner_r(skb, sk); |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | len = cb->dump(skb, cb); |
| 2657 | |
| 2658 | if (len > 0) { |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 2659 | mutex_unlock(nlk->cb_mutex); |
Stephen Hemminger | b1153f2 | 2008-03-21 15:46:12 -0700 | [diff] [blame] | 2660 | |
| 2661 | if (sk_filter(sk, skb)) |
| 2662 | kfree_skb(skb); |
Eric Dumazet | 4a7e7c2 | 2012-04-05 22:17:46 +0000 | [diff] [blame] | 2663 | else |
| 2664 | __netlink_sendskb(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | return 0; |
| 2666 | } |
| 2667 | |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2668 | nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI); |
| 2669 | if (!nlh) |
| 2670 | goto errout_skb; |
| 2671 | |
Johannes Berg | 670dc28 | 2011-06-20 13:40:46 +0200 | [diff] [blame] | 2672 | nl_dump_check_consistent(cb, nlh); |
| 2673 | |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2674 | memcpy(nlmsg_data(nlh), &len, sizeof(len)); |
| 2675 | |
Stephen Hemminger | b1153f2 | 2008-03-21 15:46:12 -0700 | [diff] [blame] | 2676 | if (sk_filter(sk, skb)) |
| 2677 | kfree_skb(skb); |
Eric Dumazet | 4a7e7c2 | 2012-04-05 22:17:46 +0000 | [diff] [blame] | 2678 | else |
| 2679 | __netlink_sendskb(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | |
Thomas Graf | a8f74b2 | 2005-11-10 02:25:52 +0100 | [diff] [blame] | 2681 | if (cb->done) |
| 2682 | cb->done(cb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | |
Pravin B Shelar | 16b304f | 2013-08-15 15:31:06 -0700 | [diff] [blame] | 2684 | nlk->cb_running = false; |
| 2685 | mutex_unlock(nlk->cb_mutex); |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 2686 | module_put(cb->module); |
Pravin B Shelar | 16b304f | 2013-08-15 15:31:06 -0700 | [diff] [blame] | 2687 | consume_skb(cb->skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | return 0; |
Thomas Graf | 1797754 | 2005-06-18 22:53:48 -0700 | [diff] [blame] | 2689 | |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2690 | errout_skb: |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 2691 | mutex_unlock(nlk->cb_mutex); |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2692 | kfree_skb(skb); |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2693 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | } |
| 2695 | |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 2696 | int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb, |
| 2697 | const struct nlmsghdr *nlh, |
| 2698 | struct netlink_dump_control *control) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | { |
| 2700 | struct netlink_callback *cb; |
| 2701 | struct sock *sk; |
| 2702 | struct netlink_sock *nlk; |
Andrey Vagin | b44d211 | 2011-02-21 02:40:47 +0000 | [diff] [blame] | 2703 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 2705 | /* Memory mapped dump requests need to be copied to avoid looping |
| 2706 | * on the pending state in netlink_mmap_sendmsg() while the CB hold |
| 2707 | * a reference to the skb. |
| 2708 | */ |
| 2709 | if (netlink_skb_is_mmaped(skb)) { |
| 2710 | skb = skb_copy(skb, GFP_KERNEL); |
Pravin B Shelar | 16b304f | 2013-08-15 15:31:06 -0700 | [diff] [blame] | 2711 | if (skb == NULL) |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 2712 | return -ENOBUFS; |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 2713 | } else |
| 2714 | atomic_inc(&skb->users); |
| 2715 | |
Pravin B Shelar | 16b304f | 2013-08-15 15:31:06 -0700 | [diff] [blame] | 2716 | sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid); |
| 2717 | if (sk == NULL) { |
| 2718 | ret = -ECONNREFUSED; |
| 2719 | goto error_free; |
| 2720 | } |
| 2721 | |
| 2722 | nlk = nlk_sk(sk); |
| 2723 | mutex_lock(nlk->cb_mutex); |
| 2724 | /* A dump is in progress... */ |
| 2725 | if (nlk->cb_running) { |
| 2726 | ret = -EBUSY; |
| 2727 | goto error_unlock; |
| 2728 | } |
| 2729 | /* add reference of module which cb->dump belongs to */ |
| 2730 | if (!try_module_get(control->module)) { |
| 2731 | ret = -EPROTONOSUPPORT; |
| 2732 | goto error_unlock; |
| 2733 | } |
| 2734 | |
| 2735 | cb = &nlk->cb; |
| 2736 | memset(cb, 0, sizeof(*cb)); |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 2737 | cb->dump = control->dump; |
| 2738 | cb->done = control->done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | cb->nlh = nlh; |
Pablo Neira Ayuso | 7175c88 | 2012-02-24 14:30:16 +0000 | [diff] [blame] | 2740 | cb->data = control->data; |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 2741 | cb->module = control->module; |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 2742 | cb->min_dump_alloc = control->min_dump_alloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | cb->skb = skb; |
| 2744 | |
Pravin B Shelar | 16b304f | 2013-08-15 15:31:06 -0700 | [diff] [blame] | 2745 | nlk->cb_running = true; |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 2746 | |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 2747 | mutex_unlock(nlk->cb_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | |
Andrey Vagin | b44d211 | 2011-02-21 02:40:47 +0000 | [diff] [blame] | 2749 | ret = netlink_dump(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 | sock_put(sk); |
Denis V. Lunev | 5c58298 | 2007-10-23 20:29:25 -0700 | [diff] [blame] | 2751 | |
Andrey Vagin | b44d211 | 2011-02-21 02:40:47 +0000 | [diff] [blame] | 2752 | if (ret) |
| 2753 | return ret; |
| 2754 | |
Denis V. Lunev | 5c58298 | 2007-10-23 20:29:25 -0700 | [diff] [blame] | 2755 | /* We successfully started a dump, by returning -EINTR we |
| 2756 | * signal not to send ACK even if it was requested. |
| 2757 | */ |
| 2758 | return -EINTR; |
Pravin B Shelar | 16b304f | 2013-08-15 15:31:06 -0700 | [diff] [blame] | 2759 | |
| 2760 | error_unlock: |
| 2761 | sock_put(sk); |
| 2762 | mutex_unlock(nlk->cb_mutex); |
| 2763 | error_free: |
| 2764 | kfree_skb(skb); |
| 2765 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | } |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 2767 | EXPORT_SYMBOL(__netlink_dump_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | |
| 2769 | void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err) |
| 2770 | { |
| 2771 | struct sk_buff *skb; |
| 2772 | struct nlmsghdr *rep; |
| 2773 | struct nlmsgerr *errmsg; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 2774 | size_t payload = sizeof(*errmsg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 2776 | /* error messages get the original request appened */ |
| 2777 | if (err) |
| 2778 | payload += nlmsg_len(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 2780 | skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload), |
| 2781 | NETLINK_CB(in_skb).portid, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | if (!skb) { |
| 2783 | struct sock *sk; |
| 2784 | |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2785 | sk = netlink_lookup(sock_net(in_skb->sk), |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2786 | in_skb->sk->sk_protocol, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2787 | NETLINK_CB(in_skb).portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | if (sk) { |
| 2789 | sk->sk_err = ENOBUFS; |
| 2790 | sk->sk_error_report(sk); |
| 2791 | sock_put(sk); |
| 2792 | } |
| 2793 | return; |
| 2794 | } |
| 2795 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2796 | rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, |
John Fastabend | 5dba93a | 2009-09-25 13:11:44 +0000 | [diff] [blame] | 2797 | NLMSG_ERROR, payload, 0); |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2798 | errmsg = nlmsg_data(rep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | errmsg->error = err; |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2800 | memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh)); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2801 | netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2803 | EXPORT_SYMBOL(netlink_ack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 2805 | int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *, |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 2806 | struct nlmsghdr *)) |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2807 | { |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2808 | struct nlmsghdr *nlh; |
| 2809 | int err; |
| 2810 | |
| 2811 | while (skb->len >= nlmsg_total_size(0)) { |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 2812 | int msglen; |
| 2813 | |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 2814 | nlh = nlmsg_hdr(skb); |
Thomas Graf | d35b685 | 2007-03-22 23:28:46 -0700 | [diff] [blame] | 2815 | err = 0; |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2816 | |
Martin Murray | ad8e4b7 | 2006-01-10 13:02:29 -0800 | [diff] [blame] | 2817 | if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len) |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2818 | return 0; |
| 2819 | |
Thomas Graf | d35b685 | 2007-03-22 23:28:46 -0700 | [diff] [blame] | 2820 | /* Only requests are handled by the kernel */ |
| 2821 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) |
Denis V. Lunev | 5c58298 | 2007-10-23 20:29:25 -0700 | [diff] [blame] | 2822 | goto ack; |
Thomas Graf | d35b685 | 2007-03-22 23:28:46 -0700 | [diff] [blame] | 2823 | |
Thomas Graf | 45e7ae7 | 2007-03-22 23:29:10 -0700 | [diff] [blame] | 2824 | /* Skip control messages */ |
| 2825 | if (nlh->nlmsg_type < NLMSG_MIN_TYPE) |
Denis V. Lunev | 5c58298 | 2007-10-23 20:29:25 -0700 | [diff] [blame] | 2826 | goto ack; |
Thomas Graf | 45e7ae7 | 2007-03-22 23:29:10 -0700 | [diff] [blame] | 2827 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 2828 | err = cb(skb, nlh); |
Denis V. Lunev | 5c58298 | 2007-10-23 20:29:25 -0700 | [diff] [blame] | 2829 | if (err == -EINTR) |
| 2830 | goto skip; |
| 2831 | |
| 2832 | ack: |
Thomas Graf | d35b685 | 2007-03-22 23:28:46 -0700 | [diff] [blame] | 2833 | if (nlh->nlmsg_flags & NLM_F_ACK || err) |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2834 | netlink_ack(skb, nlh, err); |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2835 | |
Denis V. Lunev | 5c58298 | 2007-10-23 20:29:25 -0700 | [diff] [blame] | 2836 | skip: |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2837 | msglen = NLMSG_ALIGN(nlh->nlmsg_len); |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 2838 | if (msglen > skb->len) |
| 2839 | msglen = skb->len; |
| 2840 | skb_pull(skb, msglen); |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | return 0; |
| 2844 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2845 | EXPORT_SYMBOL(netlink_rcv_skb); |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2846 | |
| 2847 | /** |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2848 | * nlmsg_notify - send a notification netlink message |
| 2849 | * @sk: netlink socket to use |
| 2850 | * @skb: notification message |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2851 | * @portid: destination netlink portid for reports or 0 |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2852 | * @group: destination multicast group or 0 |
| 2853 | * @report: 1 to report back, 0 to disable |
| 2854 | * @flags: allocation flags |
| 2855 | */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2856 | int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2857 | unsigned int group, int report, gfp_t flags) |
| 2858 | { |
| 2859 | int err = 0; |
| 2860 | |
| 2861 | if (group) { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2862 | int exclude_portid = 0; |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2863 | |
| 2864 | if (report) { |
| 2865 | atomic_inc(&skb->users); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2866 | exclude_portid = portid; |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2867 | } |
| 2868 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 2869 | /* errors reported via destination sk->sk_err, but propagate |
| 2870 | * delivery errors if NETLINK_BROADCAST_ERROR flag is set */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2871 | err = nlmsg_multicast(sk, skb, exclude_portid, group, flags); |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2872 | } |
| 2873 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 2874 | if (report) { |
| 2875 | int err2; |
| 2876 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2877 | err2 = nlmsg_unicast(sk, skb, portid); |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 2878 | if (!err || err == -ESRCH) |
| 2879 | err = err2; |
| 2880 | } |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2881 | |
| 2882 | return err; |
| 2883 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2884 | EXPORT_SYMBOL(nlmsg_notify); |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2886 | #ifdef CONFIG_PROC_FS |
| 2887 | struct nl_seq_iter { |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 2888 | struct seq_net_private p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2889 | int link; |
| 2890 | int hash_idx; |
| 2891 | }; |
| 2892 | |
| 2893 | static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos) |
| 2894 | { |
| 2895 | struct nl_seq_iter *iter = seq->private; |
| 2896 | int i, j; |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 2897 | struct netlink_sock *nlk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | struct sock *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | loff_t off = 0; |
| 2900 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2901 | for (i = 0; i < MAX_LINKS; i++) { |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 2902 | struct rhashtable *ht = &nl_table[i].hash; |
Eric Dumazet | 67a24ac | 2014-08-05 07:50:07 +0200 | [diff] [blame] | 2903 | const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 2905 | for (j = 0; j < tbl->size; j++) { |
| 2906 | rht_for_each_entry_rcu(nlk, tbl->buckets[j], node) { |
| 2907 | s = (struct sock *)nlk; |
| 2908 | |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 2909 | if (sock_net(s) != seq_file_net(seq)) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2910 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2911 | if (off == pos) { |
| 2912 | iter->link = i; |
| 2913 | iter->hash_idx = j; |
| 2914 | return s; |
| 2915 | } |
| 2916 | ++off; |
| 2917 | } |
| 2918 | } |
| 2919 | } |
| 2920 | return NULL; |
| 2921 | } |
| 2922 | |
| 2923 | static void *netlink_seq_start(struct seq_file *seq, loff_t *pos) |
Thomas Graf | 9ce12eb | 2014-08-13 16:38:32 +0200 | [diff] [blame] | 2924 | __acquires(RCU) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | { |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 2926 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2927 | return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 2928 | } |
| 2929 | |
| 2930 | static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2931 | { |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 2932 | struct netlink_sock *nlk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | struct nl_seq_iter *iter; |
Gao feng | da12c90 | 2013-06-06 14:49:11 +0800 | [diff] [blame] | 2934 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | int i, j; |
| 2936 | |
| 2937 | ++*pos; |
| 2938 | |
| 2939 | if (v == SEQ_START_TOKEN) |
| 2940 | return netlink_seq_socket_idx(seq, 0); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 2941 | |
Gao feng | da12c90 | 2013-06-06 14:49:11 +0800 | [diff] [blame] | 2942 | net = seq_file_net(seq); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2943 | iter = seq->private; |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 2944 | nlk = v; |
| 2945 | |
| 2946 | rht_for_each_entry_rcu(nlk, nlk->node.next, node) |
| 2947 | if (net_eq(sock_net((struct sock *)nlk), net)) |
| 2948 | return nlk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | i = iter->link; |
| 2951 | j = iter->hash_idx + 1; |
| 2952 | |
| 2953 | do { |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 2954 | struct rhashtable *ht = &nl_table[i].hash; |
Eric Dumazet | 67a24ac | 2014-08-05 07:50:07 +0200 | [diff] [blame] | 2955 | const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 2957 | for (; j < tbl->size; j++) { |
| 2958 | rht_for_each_entry_rcu(nlk, tbl->buckets[j], node) { |
| 2959 | if (net_eq(sock_net((struct sock *)nlk), net)) { |
| 2960 | iter->link = i; |
| 2961 | iter->hash_idx = j; |
| 2962 | return nlk; |
| 2963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2964 | } |
| 2965 | } |
| 2966 | |
| 2967 | j = 0; |
| 2968 | } while (++i < MAX_LINKS); |
| 2969 | |
| 2970 | return NULL; |
| 2971 | } |
| 2972 | |
| 2973 | static void netlink_seq_stop(struct seq_file *seq, void *v) |
Thomas Graf | 9ce12eb | 2014-08-13 16:38:32 +0200 | [diff] [blame] | 2974 | __releases(RCU) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | { |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 2976 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | |
| 2980 | static int netlink_seq_show(struct seq_file *seq, void *v) |
| 2981 | { |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 2982 | if (v == SEQ_START_TOKEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2983 | seq_puts(seq, |
| 2984 | "sk Eth Pid Groups " |
Masatake YAMATO | cf0aa4e | 2010-02-27 19:45:37 +0000 | [diff] [blame] | 2985 | "Rmem Wmem Dump Locks Drops Inode\n"); |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 2986 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | struct sock *s = v; |
| 2988 | struct netlink_sock *nlk = nlk_sk(s); |
| 2989 | |
Pravin B Shelar | 16b304f | 2013-08-15 15:31:06 -0700 | [diff] [blame] | 2990 | seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2991 | s, |
| 2992 | s->sk_protocol, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2993 | nlk->portid, |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 2994 | nlk->groups ? (u32)nlk->groups[0] : 0, |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 2995 | sk_rmem_alloc_get(s), |
| 2996 | sk_wmem_alloc_get(s), |
Pravin B Shelar | 16b304f | 2013-08-15 15:31:06 -0700 | [diff] [blame] | 2997 | nlk->cb_running, |
Pablo Neira Ayuso | 38938bf | 2009-03-24 16:37:55 -0700 | [diff] [blame] | 2998 | atomic_read(&s->sk_refcnt), |
Masatake YAMATO | cf0aa4e | 2010-02-27 19:45:37 +0000 | [diff] [blame] | 2999 | atomic_read(&s->sk_drops), |
| 3000 | sock_i_ino(s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | ); |
| 3002 | |
| 3003 | } |
| 3004 | return 0; |
| 3005 | } |
| 3006 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 3007 | static const struct seq_operations netlink_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | .start = netlink_seq_start, |
| 3009 | .next = netlink_seq_next, |
| 3010 | .stop = netlink_seq_stop, |
| 3011 | .show = netlink_seq_show, |
| 3012 | }; |
| 3013 | |
| 3014 | |
| 3015 | static int netlink_seq_open(struct inode *inode, struct file *file) |
| 3016 | { |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 3017 | return seq_open_net(inode, file, &netlink_seq_ops, |
| 3018 | sizeof(struct nl_seq_iter)); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 3019 | } |
| 3020 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 3021 | static const struct file_operations netlink_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | .owner = THIS_MODULE, |
| 3023 | .open = netlink_seq_open, |
| 3024 | .read = seq_read, |
| 3025 | .llseek = seq_lseek, |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 3026 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3027 | }; |
| 3028 | |
| 3029 | #endif |
| 3030 | |
| 3031 | int netlink_register_notifier(struct notifier_block *nb) |
| 3032 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 3033 | return atomic_notifier_chain_register(&netlink_chain, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 3035 | EXPORT_SYMBOL(netlink_register_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | |
| 3037 | int netlink_unregister_notifier(struct notifier_block *nb) |
| 3038 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 3039 | return atomic_notifier_chain_unregister(&netlink_chain, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 3041 | EXPORT_SYMBOL(netlink_unregister_notifier); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 3042 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 3043 | static const struct proto_ops netlink_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | .family = PF_NETLINK, |
| 3045 | .owner = THIS_MODULE, |
| 3046 | .release = netlink_release, |
| 3047 | .bind = netlink_bind, |
| 3048 | .connect = netlink_connect, |
| 3049 | .socketpair = sock_no_socketpair, |
| 3050 | .accept = sock_no_accept, |
| 3051 | .getname = netlink_getname, |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 3052 | .poll = netlink_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | .ioctl = sock_no_ioctl, |
| 3054 | .listen = sock_no_listen, |
| 3055 | .shutdown = sock_no_shutdown, |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 3056 | .setsockopt = netlink_setsockopt, |
| 3057 | .getsockopt = netlink_getsockopt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | .sendmsg = netlink_sendmsg, |
| 3059 | .recvmsg = netlink_recvmsg, |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 3060 | .mmap = netlink_mmap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | .sendpage = sock_no_sendpage, |
| 3062 | }; |
| 3063 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 3064 | static const struct net_proto_family netlink_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | .family = PF_NETLINK, |
| 3066 | .create = netlink_create, |
| 3067 | .owner = THIS_MODULE, /* for consistency 8) */ |
| 3068 | }; |
| 3069 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 3070 | static int __net_init netlink_net_init(struct net *net) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 3071 | { |
| 3072 | #ifdef CONFIG_PROC_FS |
Gao feng | d4beaa6 | 2013-02-18 01:34:54 +0000 | [diff] [blame] | 3073 | if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops)) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 3074 | return -ENOMEM; |
| 3075 | #endif |
| 3076 | return 0; |
| 3077 | } |
| 3078 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 3079 | static void __net_exit netlink_net_exit(struct net *net) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 3080 | { |
| 3081 | #ifdef CONFIG_PROC_FS |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 3082 | remove_proc_entry("netlink", net->proc_net); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 3083 | #endif |
| 3084 | } |
| 3085 | |
David S. Miller | b963ea8 | 2010-08-30 19:08:01 -0700 | [diff] [blame] | 3086 | static void __init netlink_add_usersock_entry(void) |
| 3087 | { |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 3088 | struct listeners *listeners; |
David S. Miller | b963ea8 | 2010-08-30 19:08:01 -0700 | [diff] [blame] | 3089 | int groups = 32; |
| 3090 | |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 3091 | listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL); |
David S. Miller | b963ea8 | 2010-08-30 19:08:01 -0700 | [diff] [blame] | 3092 | if (!listeners) |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 3093 | panic("netlink_add_usersock_entry: Cannot allocate listeners\n"); |
David S. Miller | b963ea8 | 2010-08-30 19:08:01 -0700 | [diff] [blame] | 3094 | |
| 3095 | netlink_table_grab(); |
| 3096 | |
| 3097 | nl_table[NETLINK_USERSOCK].groups = groups; |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 3098 | rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners); |
David S. Miller | b963ea8 | 2010-08-30 19:08:01 -0700 | [diff] [blame] | 3099 | nl_table[NETLINK_USERSOCK].module = THIS_MODULE; |
| 3100 | nl_table[NETLINK_USERSOCK].registered = 1; |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 3101 | nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND; |
David S. Miller | b963ea8 | 2010-08-30 19:08:01 -0700 | [diff] [blame] | 3102 | |
| 3103 | netlink_table_ungrab(); |
| 3104 | } |
| 3105 | |
Denis V. Lunev | 022cbae | 2007-11-13 03:23:50 -0800 | [diff] [blame] | 3106 | static struct pernet_operations __net_initdata netlink_net_ops = { |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 3107 | .init = netlink_net_init, |
| 3108 | .exit = netlink_net_exit, |
| 3109 | }; |
| 3110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | static int __init netlink_proto_init(void) |
| 3112 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | int err = proto_register(&netlink_proto, 0); |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 3115 | struct rhashtable_params ht_params = { |
| 3116 | .head_offset = offsetof(struct netlink_sock, node), |
| 3117 | .key_offset = offsetof(struct netlink_sock, portid), |
| 3118 | .key_len = sizeof(u32), /* portid */ |
| 3119 | .hashfn = arch_fast_hash, |
| 3120 | .max_shift = 16, /* 64K */ |
| 3121 | .grow_decision = rht_grow_above_75, |
| 3122 | .shrink_decision = rht_shrink_below_30, |
| 3123 | .mutex_is_held = lockdep_nl_sk_hash_is_held, |
| 3124 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | |
| 3126 | if (err != 0) |
| 3127 | goto out; |
| 3128 | |
YOSHIFUJI Hideaki / 吉藤英明 | fab2574 | 2013-01-09 07:19:48 +0000 | [diff] [blame] | 3129 | BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 3131 | nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL); |
Akinobu Mita | fab2caf | 2006-08-29 02:15:24 -0700 | [diff] [blame] | 3132 | if (!nl_table) |
| 3133 | goto panic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | for (i = 0; i < MAX_LINKS; i++) { |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 3136 | if (rhashtable_init(&nl_table[i].hash, &ht_params) < 0) { |
| 3137 | while (--i > 0) |
| 3138 | rhashtable_destroy(&nl_table[i].hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | kfree(nl_table); |
Akinobu Mita | fab2caf | 2006-08-29 02:15:24 -0700 | [diff] [blame] | 3140 | goto panic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | } |
| 3143 | |
Daniel Borkmann | bcbde0d | 2013-06-21 19:38:07 +0200 | [diff] [blame] | 3144 | INIT_LIST_HEAD(&netlink_tap_all); |
| 3145 | |
David S. Miller | b963ea8 | 2010-08-30 19:08:01 -0700 | [diff] [blame] | 3146 | netlink_add_usersock_entry(); |
| 3147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | sock_register(&netlink_family_ops); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 3149 | register_pernet_subsys(&netlink_net_ops); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 3150 | /* The netlink device handler may be needed early. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | rtnetlink_init(); |
| 3152 | out: |
| 3153 | return err; |
Akinobu Mita | fab2caf | 2006-08-29 02:15:24 -0700 | [diff] [blame] | 3154 | panic: |
| 3155 | panic("netlink_init: Cannot allocate nl_table\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | } |
| 3157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3158 | core_initcall(netlink_proto_init); |