blob: 7586d446d7dcafc5c44b43190398840b68107d1f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NETLINK Kernel-user communication protocol.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
Patrick McHardycd1df522013-04-17 06:47:05 +00006 * Patrick McHardy <kaber@trash.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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 Hideaki746fac42007-02-09 23:25:07 +090012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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 Welte4fdb3bb2005-08-09 19:40:55 -070017 * 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 Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26
Randy Dunlap4fc268d2006-01-11 12:17:47 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
29#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080043#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#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 Torvalds1da177e2005-04-16 15:20:36 -070049#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 Morton54e0f522005-04-30 07:07:04 +010057#include <linux/audit.h>
Patrick McHardyaf65bdf2007-04-20 14:14:21 -070058#include <linux/mutex.h>
Patrick McHardyccdfcc32013-04-17 06:47:01 +000059#include <linux/vmalloc.h>
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +020060#include <linux/if_arp.h>
Thomas Grafe3416942014-08-02 11:47:45 +020061#include <linux/rhashtable.h>
Patrick McHardy9652e932013-04-17 06:47:02 +000062#include <asm/cacheflush.h>
Thomas Grafe3416942014-08-02 11:47:45 +020063#include <linux/hash.h>
Johannes Bergee1c24422015-01-16 11:37:14 +010064#include <linux/genetlink.h>
Nicolas Dichtel72124622017-06-01 10:00:07 +020065#include <linux/net_namespace.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010066
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020067#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <net/sock.h>
69#include <net/scm.h>
Thomas Graf82ace472005-11-10 02:25:53 +010070#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Andrey Vagin0f29c762013-03-21 20:33:47 +040072#include "af_netlink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Eric Dumazet5c398dc2010-10-24 04:27:10 +000074struct listeners {
75 struct rcu_head rcu;
76 unsigned long masks[0];
Johannes Berg6c04bb12009-07-10 09:51:32 +000077};
78
Patrick McHardycd967e02013-04-17 06:46:56 +000079/* state bits */
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +020080#define NETLINK_S_CONGESTED 0x0
Patrick McHardycd967e02013-04-17 06:46:56 +000081
David S. Miller035c4c12011-12-23 17:33:03 -050082static inline int netlink_is_kernel(struct sock *sk)
Denis V. Lunevaed81562007-10-10 21:14:32 -070083{
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +020084 return nlk_sk(sk)->flags & NETLINK_F_KERNEL_SOCKET;
Denis V. Lunevaed81562007-10-10 21:14:32 -070085}
86
Eric Dumazet91dd93f2015-05-12 17:24:50 -070087struct netlink_table *nl_table __read_mostly;
Andrey Vagin0f29c762013-03-21 20:33:47 +040088EXPORT_SYMBOL_GPL(nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
91
Herbert Xu8a0f5cc2017-03-14 18:25:57 +080092static struct lock_class_key nlk_cb_mutex_keys[MAX_LINKS];
93
94static const char *const nlk_cb_mutex_key_strings[MAX_LINKS + 1] = {
95 "nlk_cb_mutex-ROUTE",
96 "nlk_cb_mutex-1",
97 "nlk_cb_mutex-USERSOCK",
98 "nlk_cb_mutex-FIREWALL",
99 "nlk_cb_mutex-SOCK_DIAG",
100 "nlk_cb_mutex-NFLOG",
101 "nlk_cb_mutex-XFRM",
102 "nlk_cb_mutex-SELINUX",
103 "nlk_cb_mutex-ISCSI",
104 "nlk_cb_mutex-AUDIT",
105 "nlk_cb_mutex-FIB_LOOKUP",
106 "nlk_cb_mutex-CONNECTOR",
107 "nlk_cb_mutex-NETFILTER",
108 "nlk_cb_mutex-IP6_FW",
109 "nlk_cb_mutex-DNRTMSG",
110 "nlk_cb_mutex-KOBJECT_UEVENT",
111 "nlk_cb_mutex-GENERIC",
112 "nlk_cb_mutex-17",
113 "nlk_cb_mutex-SCSITRANSPORT",
114 "nlk_cb_mutex-ECRYPTFS",
115 "nlk_cb_mutex-RDMA",
116 "nlk_cb_mutex-CRYPTO",
117 "nlk_cb_mutex-SMC",
118 "nlk_cb_mutex-23",
119 "nlk_cb_mutex-24",
120 "nlk_cb_mutex-25",
121 "nlk_cb_mutex-26",
122 "nlk_cb_mutex-27",
123 "nlk_cb_mutex-28",
124 "nlk_cb_mutex-29",
125 "nlk_cb_mutex-30",
126 "nlk_cb_mutex-31",
127 "nlk_cb_mutex-MAX_LINKS"
128};
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static int netlink_dump(struct sock *sk);
Patrick McHardy9652e932013-04-17 06:47:02 +0000131static void netlink_skb_destructor(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Thomas Graf78fd1d02014-10-21 22:05:38 +0200133/* nl_table locking explained:
Thomas Graf21e49022015-01-02 23:00:22 +0100134 * Lookup and traversal are protected with an RCU read-side lock. Insertion
Ying Xuec5adde92015-01-12 14:52:23 +0800135 * and removal are protected with per bucket lock while using RCU list
Thomas Graf21e49022015-01-02 23:00:22 +0100136 * modification primitives and may run in parallel to RCU protected lookups.
137 * Destruction of the Netlink socket may only occur *after* nl_table_lock has
138 * been acquired * either during or after the socket has been removed from
139 * the list and after an RCU grace period.
Thomas Graf78fd1d02014-10-21 22:05:38 +0200140 */
Andrey Vagin0f29c762013-03-21 20:33:47 +0400141DEFINE_RWLOCK(nl_table_lock);
142EXPORT_SYMBOL_GPL(nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static atomic_t nl_table_users = ATOMIC_INIT(0);
144
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000145#define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
146
WANG Congefa172f2016-12-09 21:10:59 -0800147static BLOCKING_NOTIFIER_HEAD(netlink_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200149static DEFINE_SPINLOCK(netlink_tap_lock);
150static struct list_head netlink_tap_all __read_mostly;
151
Herbert Xuc428ecd2015-03-20 21:57:01 +1100152static const struct rhashtable_params netlink_rhashtable_params;
153
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000154static inline u32 netlink_group_mask(u32 group)
Patrick McHardyd629b832005-08-14 19:27:50 -0700155{
156 return group ? 1 << (group - 1) : 0;
157}
158
Daniel Borkmann1853c942015-09-10 20:05:46 +0200159static struct sk_buff *netlink_to_full_skb(const struct sk_buff *skb,
160 gfp_t gfp_mask)
161{
162 unsigned int len = skb_end_offset(skb);
163 struct sk_buff *new;
164
165 new = alloc_skb(len, gfp_mask);
166 if (new == NULL)
167 return NULL;
168
169 NETLINK_CB(new).portid = NETLINK_CB(skb).portid;
170 NETLINK_CB(new).dst_group = NETLINK_CB(skb).dst_group;
171 NETLINK_CB(new).creds = NETLINK_CB(skb).creds;
172
173 memcpy(skb_put(new, len), skb->data, len);
174 return new;
175}
176
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200177int netlink_add_tap(struct netlink_tap *nt)
178{
179 if (unlikely(nt->dev->type != ARPHRD_NETLINK))
180 return -EINVAL;
181
182 spin_lock(&netlink_tap_lock);
183 list_add_rcu(&nt->list, &netlink_tap_all);
184 spin_unlock(&netlink_tap_lock);
185
Markus Elfringfcd4d352014-11-18 21:03:13 +0100186 __module_get(nt->module);
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200187
188 return 0;
189}
190EXPORT_SYMBOL_GPL(netlink_add_tap);
191
stephen hemminger2173f8d2013-12-30 10:49:22 -0800192static int __netlink_remove_tap(struct netlink_tap *nt)
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200193{
194 bool found = false;
195 struct netlink_tap *tmp;
196
197 spin_lock(&netlink_tap_lock);
198
199 list_for_each_entry(tmp, &netlink_tap_all, list) {
200 if (nt == tmp) {
201 list_del_rcu(&nt->list);
202 found = true;
203 goto out;
204 }
205 }
206
207 pr_warn("__netlink_remove_tap: %p not found\n", nt);
208out:
209 spin_unlock(&netlink_tap_lock);
210
Markus Elfring92b80eb2015-07-02 18:38:12 +0200211 if (found)
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200212 module_put(nt->module);
213
214 return found ? 0 : -ENODEV;
215}
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200216
217int netlink_remove_tap(struct netlink_tap *nt)
218{
219 int ret;
220
221 ret = __netlink_remove_tap(nt);
222 synchronize_net();
223
224 return ret;
225}
226EXPORT_SYMBOL_GPL(netlink_remove_tap);
227
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200228static bool netlink_filter_tap(const struct sk_buff *skb)
229{
230 struct sock *sk = skb->sk;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200231
232 /* We take the more conservative approach and
233 * whitelist socket protocols that may pass.
234 */
235 switch (sk->sk_protocol) {
236 case NETLINK_ROUTE:
237 case NETLINK_USERSOCK:
238 case NETLINK_SOCK_DIAG:
239 case NETLINK_NFLOG:
240 case NETLINK_XFRM:
241 case NETLINK_FIB_LOOKUP:
242 case NETLINK_NETFILTER:
243 case NETLINK_GENERIC:
Varka Bhadram498044b2014-07-16 10:59:47 +0530244 return true;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200245 }
246
Varka Bhadram498044b2014-07-16 10:59:47 +0530247 return false;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200248}
249
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200250static int __netlink_deliver_tap_skb(struct sk_buff *skb,
251 struct net_device *dev)
252{
253 struct sk_buff *nskb;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200254 struct sock *sk = skb->sk;
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200255 int ret = -ENOMEM;
256
257 dev_hold(dev);
Daniel Borkmann1853c942015-09-10 20:05:46 +0200258
Florian Westphald1b4c682016-02-18 15:03:24 +0100259 if (is_vmalloc_addr(skb->head))
Daniel Borkmann1853c942015-09-10 20:05:46 +0200260 nskb = netlink_to_full_skb(skb, GFP_ATOMIC);
261 else
262 nskb = skb_clone(skb, GFP_ATOMIC);
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200263 if (nskb) {
264 nskb->dev = dev;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200265 nskb->protocol = htons((u16) sk->sk_protocol);
Daniel Borkmann604d13c2013-12-23 14:35:56 +0100266 nskb->pkt_type = netlink_is_kernel(sk) ?
267 PACKET_KERNEL : PACKET_USER;
Daniel Borkmann4e48ed82014-08-07 22:22:47 +0200268 skb_reset_network_header(nskb);
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200269 ret = dev_queue_xmit(nskb);
270 if (unlikely(ret > 0))
271 ret = net_xmit_errno(ret);
272 }
273
274 dev_put(dev);
275 return ret;
276}
277
278static void __netlink_deliver_tap(struct sk_buff *skb)
279{
280 int ret;
281 struct netlink_tap *tmp;
282
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200283 if (!netlink_filter_tap(skb))
284 return;
285
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200286 list_for_each_entry_rcu(tmp, &netlink_tap_all, list) {
287 ret = __netlink_deliver_tap_skb(skb, tmp->dev);
288 if (unlikely(ret))
289 break;
290 }
291}
292
293static void netlink_deliver_tap(struct sk_buff *skb)
294{
295 rcu_read_lock();
296
297 if (unlikely(!list_empty(&netlink_tap_all)))
298 __netlink_deliver_tap(skb);
299
300 rcu_read_unlock();
301}
302
Daniel Borkmann73bfd372013-12-23 14:35:55 +0100303static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src,
304 struct sk_buff *skb)
305{
306 if (!(netlink_is_kernel(dst) && netlink_is_kernel(src)))
307 netlink_deliver_tap(skb);
308}
309
Patrick McHardycd1df522013-04-17 06:47:05 +0000310static void netlink_overrun(struct sock *sk)
311{
312 struct netlink_sock *nlk = nlk_sk(sk);
313
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +0200314 if (!(nlk->flags & NETLINK_F_RECV_NO_ENOBUFS)) {
315 if (!test_and_set_bit(NETLINK_S_CONGESTED,
316 &nlk_sk(sk)->state)) {
Patrick McHardycd1df522013-04-17 06:47:05 +0000317 sk->sk_err = ENOBUFS;
318 sk->sk_error_report(sk);
319 }
320 }
321 atomic_inc(&sk->sk_drops);
322}
323
324static void netlink_rcv_wake(struct sock *sk)
325{
326 struct netlink_sock *nlk = nlk_sk(sk);
327
328 if (skb_queue_empty(&sk->sk_receive_queue))
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +0200329 clear_bit(NETLINK_S_CONGESTED, &nlk->state);
330 if (!test_bit(NETLINK_S_CONGESTED, &nlk->state))
Patrick McHardycd1df522013-04-17 06:47:05 +0000331 wake_up_interruptible(&nlk->wait);
332}
333
Patrick McHardycf0a0182013-04-17 06:47:00 +0000334static void netlink_skb_destructor(struct sk_buff *skb)
335{
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +0000336 if (is_vmalloc_addr(skb->head)) {
Pablo Neira3a365152013-06-28 03:04:23 +0200337 if (!skb->cloned ||
338 !atomic_dec_return(&(skb_shinfo(skb)->dataref)))
339 vfree(skb->head);
340
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +0000341 skb->head = NULL;
342 }
Patrick McHardy9652e932013-04-17 06:47:02 +0000343 if (skb->sk != NULL)
344 sock_rfree(skb);
Patrick McHardycf0a0182013-04-17 06:47:00 +0000345}
346
347static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
348{
349 WARN_ON(skb->sk != NULL);
350 skb->sk = sk;
351 skb->destructor = netlink_skb_destructor;
352 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
353 sk_mem_charge(sk, skb->truesize);
354}
355
Herbert Xued5d7782016-12-05 15:28:21 +0800356static void netlink_sock_destruct(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Herbert Xu3f660d62007-05-03 03:17:14 -0700358 struct netlink_sock *nlk = nlk_sk(sk);
359
Pravin B Shelar16b304f2013-08-15 15:31:06 -0700360 if (nlk->cb_running) {
Herbert Xued5d7782016-12-05 15:28:21 +0800361 if (nlk->cb.done)
362 nlk->cb.done(&nlk->cb);
Pravin B Shelar16b304f2013-08-15 15:31:06 -0700363 module_put(nlk->cb.module);
364 kfree_skb(nlk->cb.skb);
Herbert Xu3f660d62007-05-03 03:17:14 -0700365 }
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 skb_queue_purge(&sk->sk_receive_queue);
368
369 if (!sock_flag(sk, SOCK_DEAD)) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800370 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return;
372 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700373
374 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
375 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
376 WARN_ON(nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Herbert Xu707693c2016-11-28 19:22:12 +0800379static void netlink_sock_destruct_work(struct work_struct *work)
380{
381 struct netlink_sock *nlk = container_of(work, struct netlink_sock,
382 work);
383
Herbert Xued5d7782016-12-05 15:28:21 +0800384 sk_free(&nlk->sk);
Herbert Xu707693c2016-11-28 19:22:12 +0800385}
386
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800387/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
388 * SMP. Look, when several writers sleep and reader wakes them up, all but one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
390 * this, _but_ remember, it adds useless work on UP machines.
391 */
392
Johannes Bergd136f1b2009-09-12 03:03:15 +0000393void netlink_table_grab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800394 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Johannes Bergd136f1b2009-09-12 03:03:15 +0000396 might_sleep();
397
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700398 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (atomic_read(&nl_table_users)) {
401 DECLARE_WAITQUEUE(wait, current);
402
403 add_wait_queue_exclusive(&nl_table_wait, &wait);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800404 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 set_current_state(TASK_UNINTERRUPTIBLE);
406 if (atomic_read(&nl_table_users) == 0)
407 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700408 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700410 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
413 __set_current_state(TASK_RUNNING);
414 remove_wait_queue(&nl_table_wait, &wait);
415 }
416}
417
Johannes Bergd136f1b2009-09-12 03:03:15 +0000418void netlink_table_ungrab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800419 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700421 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 wake_up(&nl_table_wait);
423}
424
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800425static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426netlink_lock_table(void)
427{
428 /* read_lock() synchronizes us to netlink_table_grab */
429
430 read_lock(&nl_table_lock);
431 atomic_inc(&nl_table_users);
432 read_unlock(&nl_table_lock);
433}
434
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800435static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436netlink_unlock_table(void)
437{
438 if (atomic_dec_and_test(&nl_table_users))
439 wake_up(&nl_table_wait);
440}
441
Thomas Grafe3416942014-08-02 11:47:45 +0200442struct netlink_compare_arg
Gao fengda12c902013-06-06 14:49:11 +0800443{
Herbert Xuc428ecd2015-03-20 21:57:01 +1100444 possible_net_t pnet;
Thomas Grafe3416942014-08-02 11:47:45 +0200445 u32 portid;
446};
447
Herbert Xu8f2ddaa2015-03-21 14:14:03 +1100448/* Doing sizeof directly may yield 4 extra bytes on 64-bit. */
449#define netlink_compare_arg_len \
450 (offsetof(struct netlink_compare_arg, portid) + sizeof(u32))
Thomas Grafe3416942014-08-02 11:47:45 +0200451
Herbert Xuc428ecd2015-03-20 21:57:01 +1100452static inline int netlink_compare(struct rhashtable_compare_arg *arg,
453 const void *ptr)
454{
455 const struct netlink_compare_arg *x = arg->key;
456 const struct netlink_sock *nlk = ptr;
457
Herbert Xuda314c92015-09-22 11:38:56 +0800458 return nlk->portid != x->portid ||
Herbert Xuc428ecd2015-03-20 21:57:01 +1100459 !net_eq(sock_net(&nlk->sk), read_pnet(&x->pnet));
460}
461
462static void netlink_compare_arg_init(struct netlink_compare_arg *arg,
463 struct net *net, u32 portid)
464{
465 memset(arg, 0, sizeof(*arg));
466 write_pnet(&arg->pnet, net);
467 arg->portid = portid;
Thomas Grafe3416942014-08-02 11:47:45 +0200468}
469
470static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid,
471 struct net *net)
472{
Herbert Xuc428ecd2015-03-20 21:57:01 +1100473 struct netlink_compare_arg arg;
Thomas Grafe3416942014-08-02 11:47:45 +0200474
Herbert Xuc428ecd2015-03-20 21:57:01 +1100475 netlink_compare_arg_init(&arg, net, portid);
476 return rhashtable_lookup_fast(&table->hash, &arg,
477 netlink_rhashtable_params);
Gao fengda12c902013-06-06 14:49:11 +0800478}
479
Herbert Xuc428ecd2015-03-20 21:57:01 +1100480static int __netlink_insert(struct netlink_table *table, struct sock *sk)
Ying Xuec5adde92015-01-12 14:52:23 +0800481{
Herbert Xuc428ecd2015-03-20 21:57:01 +1100482 struct netlink_compare_arg arg;
Ying Xuec5adde92015-01-12 14:52:23 +0800483
Herbert Xuda314c92015-09-22 11:38:56 +0800484 netlink_compare_arg_init(&arg, sock_net(sk), nlk_sk(sk)->portid);
Herbert Xuc428ecd2015-03-20 21:57:01 +1100485 return rhashtable_lookup_insert_key(&table->hash, &arg,
486 &nlk_sk(sk)->node,
487 netlink_rhashtable_params);
Ying Xuec5adde92015-01-12 14:52:23 +0800488}
489
Eric W. Biederman15e47302012-09-07 20:12:54 +0000490static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Gao fengda12c902013-06-06 14:49:11 +0800492 struct netlink_table *table = &nl_table[protocol];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Thomas Grafe3416942014-08-02 11:47:45 +0200495 rcu_read_lock();
496 sk = __netlink_lookup(table, portid, net);
497 if (sk)
498 sock_hold(sk);
499 rcu_read_unlock();
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return sk;
502}
503
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800504static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Patrick McHardy4277a082006-03-20 18:52:01 -0800506static void
507netlink_update_listeners(struct sock *sk)
508{
509 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Patrick McHardy4277a082006-03-20 18:52:01 -0800510 unsigned long mask;
511 unsigned int i;
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000512 struct listeners *listeners;
513
514 listeners = nl_deref_protected(tbl->listeners);
515 if (!listeners)
516 return;
Patrick McHardy4277a082006-03-20 18:52:01 -0800517
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700518 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800519 mask = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800520 sk_for_each_bound(sk, &tbl->mc_list) {
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700521 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
522 mask |= nlk_sk(sk)->groups[i];
523 }
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000524 listeners->masks[i] = mask;
Patrick McHardy4277a082006-03-20 18:52:01 -0800525 }
526 /* this function is only called with the netlink table "grabbed", which
527 * makes sure updates are visible before bind or setsockopt return. */
528}
529
Herbert Xu8ea65f42015-01-26 14:02:56 +1100530static int netlink_insert(struct sock *sk, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Gao fengda12c902013-06-06 14:49:11 +0800532 struct netlink_table *table = &nl_table[sk->sk_protocol];
Herbert Xu919d9db2015-01-16 17:23:48 +1100533 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Ying Xuec5adde92015-01-12 14:52:23 +0800535 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Herbert Xuda314c92015-09-22 11:38:56 +0800537 err = nlk_sk(sk)->portid == portid ? 0 : -EBUSY;
538 if (nlk_sk(sk)->bound)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto err;
540
541 err = -ENOMEM;
Thomas Graf97defe12015-01-02 23:00:20 +0100542 if (BITS_PER_LONG > 32 &&
543 unlikely(atomic_read(&table->hash.nelems) >= UINT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 goto err;
545
Herbert Xuda314c92015-09-22 11:38:56 +0800546 nlk_sk(sk)->portid = portid;
Thomas Grafe3416942014-08-02 11:47:45 +0200547 sock_hold(sk);
Herbert Xu919d9db2015-01-16 17:23:48 +1100548
Herbert Xuc428ecd2015-03-20 21:57:01 +1100549 err = __netlink_insert(table, sk);
550 if (err) {
Daniel Borkmann4e7c1332015-08-07 00:26:41 +0200551 /* In case the hashtable backend returns with -EBUSY
552 * from here, it must not escape to the caller.
553 */
554 if (unlikely(err == -EBUSY))
555 err = -EOVERFLOW;
Herbert Xuc428ecd2015-03-20 21:57:01 +1100556 if (err == -EEXIST)
557 err = -EADDRINUSE;
Ying Xuec5adde92015-01-12 14:52:23 +0800558 sock_put(sk);
Herbert Xu1f770c02015-09-18 19:16:50 +0800559 goto err;
Herbert Xu919d9db2015-01-16 17:23:48 +1100560 }
561
Herbert Xuda314c92015-09-22 11:38:56 +0800562 /* We need to ensure that the socket is hashed and visible. */
563 smp_wmb();
564 nlk_sk(sk)->bound = portid;
Herbert Xu1f770c02015-09-18 19:16:50 +0800565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566err:
Ying Xuec5adde92015-01-12 14:52:23 +0800567 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return err;
569}
570
571static void netlink_remove(struct sock *sk)
572{
Thomas Grafe3416942014-08-02 11:47:45 +0200573 struct netlink_table *table;
574
Thomas Grafe3416942014-08-02 11:47:45 +0200575 table = &nl_table[sk->sk_protocol];
Herbert Xuc428ecd2015-03-20 21:57:01 +1100576 if (!rhashtable_remove_fast(&table->hash, &nlk_sk(sk)->node,
577 netlink_rhashtable_params)) {
Thomas Grafe3416942014-08-02 11:47:45 +0200578 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
579 __sock_put(sk);
580 }
Thomas Grafe3416942014-08-02 11:47:45 +0200581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 netlink_table_grab();
Johannes Bergb10dcb32014-12-22 18:56:37 +0100583 if (nlk_sk(sk)->subscriptions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 __sk_del_bind_node(sk);
Johannes Bergb10dcb32014-12-22 18:56:37 +0100585 netlink_update_listeners(sk);
586 }
Johannes Bergee1c24422015-01-16 11:37:14 +0100587 if (sk->sk_protocol == NETLINK_GENERIC)
588 atomic_inc(&genl_sk_destructing_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 netlink_table_ungrab();
590}
591
592static struct proto netlink_proto = {
593 .name = "NETLINK",
594 .owner = THIS_MODULE,
595 .obj_size = sizeof(struct netlink_sock),
596};
597
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700598static int __netlink_create(struct net *net, struct socket *sock,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500599 struct mutex *cb_mutex, int protocol,
600 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 struct sock *sk;
603 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700604
605 sock->ops = &netlink_ops;
606
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500607 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto, kern);
Patrick McHardyab33a172005-08-14 19:31:36 -0700608 if (!sk)
609 return -ENOMEM;
610
611 sock_init_data(sock, sk);
612
613 nlk = nlk_sk(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000614 if (cb_mutex) {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700615 nlk->cb_mutex = cb_mutex;
Eric Dumazet658cb352012-04-22 21:30:21 +0000616 } else {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700617 nlk->cb_mutex = &nlk->cb_def_mutex;
618 mutex_init(nlk->cb_mutex);
Herbert Xu8a0f5cc2017-03-14 18:25:57 +0800619 lockdep_set_class_and_name(nlk->cb_mutex,
620 nlk_cb_mutex_keys + protocol,
621 nlk_cb_mutex_key_strings[protocol]);
Patrick McHardyffa4d722007-04-25 14:01:17 -0700622 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700623 init_waitqueue_head(&nlk->wait);
624
625 sk->sk_destruct = netlink_sock_destruct;
626 sk->sk_protocol = protocol;
627 return 0;
628}
629
Eric Paris3f378b62009-11-05 22:18:14 -0800630static int netlink_create(struct net *net, struct socket *sock, int protocol,
631 int kern)
Patrick McHardyab33a172005-08-14 19:31:36 -0700632{
633 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700634 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700635 struct netlink_sock *nlk;
Johannes Berg023e2cf2014-12-23 21:00:06 +0100636 int (*bind)(struct net *net, int group);
637 void (*unbind)(struct net *net, int group);
Patrick McHardyab33a172005-08-14 19:31:36 -0700638 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 sock->state = SS_UNCONNECTED;
641
642 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
643 return -ESOCKTNOSUPPORT;
644
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800645 if (protocol < 0 || protocol >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return -EPROTONOSUPPORT;
647
Patrick McHardy77247bb2005-08-14 19:27:13 -0700648 netlink_lock_table();
Johannes Berg95a5afc2008-10-16 15:24:51 -0700649#ifdef CONFIG_MODULES
Patrick McHardyab33a172005-08-14 19:31:36 -0700650 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -0700651 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700652 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -0700653 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700654 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700655#endif
656 if (nl_table[protocol].registered &&
657 try_module_get(nl_table[protocol].module))
658 module = nl_table[protocol].module;
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000659 else
660 err = -EPROTONOSUPPORT;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700661 cb_mutex = nl_table[protocol].cb_mutex;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000662 bind = nl_table[protocol].bind;
Richard Guy Briggs4f520902014-04-22 21:31:54 -0400663 unbind = nl_table[protocol].unbind;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700664 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700665
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000666 if (err < 0)
667 goto out;
668
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500669 err = __netlink_create(net, sock, cb_mutex, protocol, kern);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800670 if (err < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -0700671 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
David S. Miller6f756a82008-11-23 17:34:03 -0800673 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800674 sock_prot_inuse_add(net, &netlink_proto, 1);
David S. Miller6f756a82008-11-23 17:34:03 -0800675 local_bh_enable();
676
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700677 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700678 nlk->module = module;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000679 nlk->netlink_bind = bind;
Richard Guy Briggs4f520902014-04-22 21:31:54 -0400680 nlk->netlink_unbind = unbind;
Patrick McHardyab33a172005-08-14 19:31:36 -0700681out:
682 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Patrick McHardyab33a172005-08-14 19:31:36 -0700684out_module:
685 module_put(module);
686 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Thomas Graf21e49022015-01-02 23:00:22 +0100689static void deferred_put_nlk_sk(struct rcu_head *head)
690{
691 struct netlink_sock *nlk = container_of(head, struct netlink_sock, rcu);
Herbert Xued5d7782016-12-05 15:28:21 +0800692 struct sock *sk = &nlk->sk;
Thomas Graf21e49022015-01-02 23:00:22 +0100693
Herbert Xued5d7782016-12-05 15:28:21 +0800694 if (!atomic_dec_and_test(&sk->sk_refcnt))
695 return;
696
697 if (nlk->cb_running && nlk->cb.done) {
698 INIT_WORK(&nlk->work, netlink_sock_destruct_work);
699 schedule_work(&nlk->work);
700 return;
701 }
702
703 sk_free(sk);
Thomas Graf21e49022015-01-02 23:00:22 +0100704}
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706static int netlink_release(struct socket *sock)
707{
708 struct sock *sk = sock->sk;
709 struct netlink_sock *nlk;
710
711 if (!sk)
712 return 0;
713
714 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -0700715 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 nlk = nlk_sk(sk);
717
Herbert Xu3f660d62007-05-03 03:17:14 -0700718 /*
719 * OK. Socket is unlinked, any packets that arrive now
720 * will be purged.
721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Johannes Bergee1c24422015-01-16 11:37:14 +0100723 /* must not acquire netlink_table_lock in any way again before unbind
724 * and notifying genetlink is done as otherwise it might deadlock
725 */
726 if (nlk->netlink_unbind) {
727 int i;
728
729 for (i = 0; i < nlk->ngroups; i++)
730 if (test_bit(i, nlk->groups))
731 nlk->netlink_unbind(sock_net(sk), i + 1);
732 }
733 if (sk->sk_protocol == NETLINK_GENERIC &&
734 atomic_dec_return(&genl_sk_destructing_cnt) == 0)
735 wake_up(&genl_sk_destructing_waitq);
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 sock->sk = NULL;
738 wake_up_interruptible_all(&nlk->wait);
739
740 skb_queue_purge(&sk->sk_write_queue);
741
Dmitry Ivanove2726022016-04-07 09:31:38 +0200742 if (nlk->portid && nlk->bound) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct netlink_notify n = {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900744 .net = sock_net(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 .protocol = sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000746 .portid = nlk->portid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 };
WANG Congefa172f2016-12-09 21:10:59 -0800748 blocking_notifier_call_chain(&netlink_chain,
Alan Sterne041c682006-03-27 01:16:30 -0800749 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900750 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700751
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -0800752 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700753
Denis V. Lunevaed81562007-10-10 21:14:32 -0700754 if (netlink_is_kernel(sk)) {
Johannes Bergb10dcb32014-12-22 18:56:37 +0100755 netlink_table_grab();
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800756 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
757 if (--nl_table[sk->sk_protocol].registered == 0) {
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000758 struct listeners *old;
759
760 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
761 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
762 kfree_rcu(old, rcu);
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800763 nl_table[sk->sk_protocol].module = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000764 nl_table[sk->sk_protocol].bind = NULL;
Richard Guy Briggs4f520902014-04-22 21:31:54 -0400765 nl_table[sk->sk_protocol].unbind = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000766 nl_table[sk->sk_protocol].flags = 0;
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800767 nl_table[sk->sk_protocol].registered = 0;
768 }
Johannes Bergb10dcb32014-12-22 18:56:37 +0100769 netlink_table_ungrab();
Eric Dumazet658cb352012-04-22 21:30:21 +0000770 }
Patrick McHardy77247bb2005-08-14 19:27:13 -0700771
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700772 kfree(nlk->groups);
773 nlk->groups = NULL;
774
Eric Dumazet37558102008-11-24 14:05:22 -0800775 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800776 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
Eric Dumazet37558102008-11-24 14:05:22 -0800777 local_bh_enable();
Thomas Graf21e49022015-01-02 23:00:22 +0100778 call_rcu(&nlk->rcu, deferred_put_nlk_sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return 0;
780}
781
782static int netlink_autobind(struct socket *sock)
783{
784 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900785 struct net *net = sock_net(sk);
Gao fengda12c902013-06-06 14:49:11 +0800786 struct netlink_table *table = &nl_table[sk->sk_protocol];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000787 s32 portid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int err;
Herbert Xub9fbe702015-05-17 10:45:34 +0800789 s32 rover = -4096;
790 bool ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792retry:
793 cond_resched();
Thomas Grafe3416942014-08-02 11:47:45 +0200794 rcu_read_lock();
Herbert Xub9fbe702015-05-17 10:45:34 +0800795 ok = !__netlink_lookup(table, portid, net);
796 rcu_read_unlock();
797 if (!ok) {
Thomas Grafe3416942014-08-02 11:47:45 +0200798 /* Bind collision, search negative portid values. */
Herbert Xub9fbe702015-05-17 10:45:34 +0800799 if (rover == -4096)
800 /* rover will be in range [S32_MIN, -4097] */
801 rover = S32_MIN + prandom_u32_max(-4096 - S32_MIN);
802 else if (rover >= -4096)
Thomas Grafe3416942014-08-02 11:47:45 +0200803 rover = -4097;
Herbert Xub9fbe702015-05-17 10:45:34 +0800804 portid = rover--;
Thomas Grafe3416942014-08-02 11:47:45 +0200805 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Herbert Xu8ea65f42015-01-26 14:02:56 +1100808 err = netlink_insert(sk, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (err == -EADDRINUSE)
810 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -0700811
812 /* If 2 threads race to autobind, that is fine. */
813 if (err == -EBUSY)
814 err = 0;
815
816 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Eric W. Biedermanaa4cf942014-04-23 14:28:03 -0700819/**
820 * __netlink_ns_capable - General netlink message capability test
821 * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
822 * @user_ns: The user namespace of the capability to use
823 * @cap: The capability to use
824 *
825 * Test to see if the opener of the socket we received the message
826 * from had when the netlink socket was created and the sender of the
827 * message has has the capability @cap in the user namespace @user_ns.
828 */
829bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
830 struct user_namespace *user_ns, int cap)
831{
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -0700832 return ((nsp->flags & NETLINK_SKB_DST) ||
833 file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
834 ns_capable(user_ns, cap);
Eric W. Biedermanaa4cf942014-04-23 14:28:03 -0700835}
836EXPORT_SYMBOL(__netlink_ns_capable);
837
838/**
839 * netlink_ns_capable - General netlink message capability test
840 * @skb: socket buffer holding a netlink command from userspace
841 * @user_ns: The user namespace of the capability to use
842 * @cap: The capability to use
843 *
844 * Test to see if the opener of the socket we received the message
845 * from had when the netlink socket was created and the sender of the
846 * message has has the capability @cap in the user namespace @user_ns.
847 */
848bool netlink_ns_capable(const struct sk_buff *skb,
849 struct user_namespace *user_ns, int cap)
850{
851 return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
852}
853EXPORT_SYMBOL(netlink_ns_capable);
854
855/**
856 * netlink_capable - Netlink global message capability test
857 * @skb: socket buffer holding a netlink command from userspace
858 * @cap: The capability to use
859 *
860 * Test to see if the opener of the socket we received the message
861 * from had when the netlink socket was created and the sender of the
862 * message has has the capability @cap in all user namespaces.
863 */
864bool netlink_capable(const struct sk_buff *skb, int cap)
865{
866 return netlink_ns_capable(skb, &init_user_ns, cap);
867}
868EXPORT_SYMBOL(netlink_capable);
869
870/**
871 * netlink_net_capable - Netlink network namespace message capability test
872 * @skb: socket buffer holding a netlink command from userspace
873 * @cap: The capability to use
874 *
875 * Test to see if the opener of the socket we received the message
876 * from had when the netlink socket was created and the sender of the
877 * message has has the capability @cap over the network namespace of
878 * the socket we received the message from.
879 */
880bool netlink_net_capable(const struct sk_buff *skb, int cap)
881{
882 return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
883}
884EXPORT_SYMBOL(netlink_net_capable);
885
Eric W. Biederman5187cd02014-04-23 14:25:48 -0700886static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900887{
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000888 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
Eric W. Biedermandf008c92012-11-16 03:03:07 +0000889 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900890}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700892static void
893netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
894{
895 struct netlink_sock *nlk = nlk_sk(sk);
896
897 if (nlk->subscriptions && !subscriptions)
898 __sk_del_bind_node(sk);
899 else if (!nlk->subscriptions && subscriptions)
900 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
901 nlk->subscriptions = subscriptions;
902}
903
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700904static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -0700905{
906 struct netlink_sock *nlk = nlk_sk(sk);
907 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700908 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700909 int err = 0;
910
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700911 netlink_table_grab();
912
Patrick McHardy513c2502005-09-06 15:43:59 -0700913 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700914 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -0700915 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700916 goto out_unlock;
917 }
Patrick McHardy513c2502005-09-06 15:43:59 -0700918
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700919 if (nlk->ngroups >= groups)
920 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -0700921
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700922 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
923 if (new_groups == NULL) {
924 err = -ENOMEM;
925 goto out_unlock;
926 }
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800927 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700928 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
929
930 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700931 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700932 out_unlock:
933 netlink_table_ungrab();
934 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700935}
936
Johannes Berg02c81ab2014-12-22 18:56:35 +0100937static void netlink_undo_bind(int group, long unsigned int groups,
Johannes Berg023e2cf2014-12-23 21:00:06 +0100938 struct sock *sk)
Richard Guy Briggs4f520902014-04-22 21:31:54 -0400939{
Johannes Berg023e2cf2014-12-23 21:00:06 +0100940 struct netlink_sock *nlk = nlk_sk(sk);
Richard Guy Briggs4f520902014-04-22 21:31:54 -0400941 int undo;
942
943 if (!nlk->netlink_unbind)
944 return;
945
946 for (undo = 0; undo < group; undo++)
Hiroaki SHIMODA6251edd2014-11-13 04:24:10 +0900947 if (test_bit(undo, &groups))
Pablo Neira8b7c36d2015-01-29 10:51:53 +0100948 nlk->netlink_unbind(sock_net(sk), undo + 1);
Richard Guy Briggs4f520902014-04-22 21:31:54 -0400949}
950
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800951static int netlink_bind(struct socket *sock, struct sockaddr *addr,
952 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900955 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 struct netlink_sock *nlk = nlk_sk(sk);
957 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
958 int err;
Richard Guy Briggs4f520902014-04-22 21:31:54 -0400959 long unsigned int groups = nladdr->nl_groups;
Herbert Xuda314c92015-09-22 11:38:56 +0800960 bool bound;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900961
Hannes Frederic Sowa4e4b5372012-12-15 15:42:19 +0000962 if (addr_len < sizeof(struct sockaddr_nl))
963 return -EINVAL;
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (nladdr->nl_family != AF_NETLINK)
966 return -EINVAL;
967
968 /* Only superuser is allowed to listen multicasts */
Richard Guy Briggs4f520902014-04-22 21:31:54 -0400969 if (groups) {
Eric W. Biederman5187cd02014-04-23 14:25:48 -0700970 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy513c2502005-09-06 15:43:59 -0700971 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700972 err = netlink_realloc_groups(sk);
973 if (err)
974 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Herbert Xuda314c92015-09-22 11:38:56 +0800977 bound = nlk->bound;
978 if (bound) {
979 /* Ensure nlk->portid is up-to-date. */
980 smp_rmb();
981
Eric W. Biederman15e47302012-09-07 20:12:54 +0000982 if (nladdr->nl_pid != nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return -EINVAL;
Herbert Xuda314c92015-09-22 11:38:56 +0800984 }
Richard Guy Briggs4f520902014-04-22 21:31:54 -0400985
986 if (nlk->netlink_bind && groups) {
987 int group;
988
989 for (group = 0; group < nlk->ngroups; group++) {
990 if (!test_bit(group, &groups))
991 continue;
Pablo Neira8b7c36d2015-01-29 10:51:53 +0100992 err = nlk->netlink_bind(net, group + 1);
Richard Guy Briggs4f520902014-04-22 21:31:54 -0400993 if (!err)
994 continue;
Johannes Berg023e2cf2014-12-23 21:00:06 +0100995 netlink_undo_bind(group, groups, sk);
Richard Guy Briggs4f520902014-04-22 21:31:54 -0400996 return err;
997 }
998 }
999
Herbert Xuda314c92015-09-22 11:38:56 +08001000 /* No need for barriers here as we return to user-space without
1001 * using any of the bound attributes.
1002 */
1003 if (!bound) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 err = nladdr->nl_pid ?
Herbert Xu8ea65f42015-01-26 14:02:56 +11001005 netlink_insert(sk, nladdr->nl_pid) :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 netlink_autobind(sock);
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001007 if (err) {
Johannes Berg023e2cf2014-12-23 21:00:06 +01001008 netlink_undo_bind(nlk->ngroups, groups, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return err;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001013 if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return 0;
1015
1016 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001017 netlink_update_subscriptions(sk, nlk->subscriptions +
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001018 hweight32(groups) -
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001019 hweight32(nlk->groups[0]));
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001020 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001021 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 netlink_table_ungrab();
1023
1024 return 0;
1025}
1026
1027static int netlink_connect(struct socket *sock, struct sockaddr *addr,
1028 int alen, int flags)
1029{
1030 int err = 0;
1031 struct sock *sk = sock->sk;
1032 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001033 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Changli Gao6503d962010-03-31 22:58:26 +00001035 if (alen < sizeof(addr->sa_family))
1036 return -EINVAL;
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (addr->sa_family == AF_UNSPEC) {
1039 sk->sk_state = NETLINK_UNCONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001040 nlk->dst_portid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -07001041 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return 0;
1043 }
1044 if (addr->sa_family != AF_NETLINK)
1045 return -EINVAL;
1046
Mike Pecovnik46833a82014-02-24 21:11:16 +01001047 if ((nladdr->nl_groups || nladdr->nl_pid) &&
Eric W. Biederman5187cd02014-04-23 14:25:48 -07001048 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -EPERM;
1050
Herbert Xuda314c92015-09-22 11:38:56 +08001051 /* No need for barriers here as we return to user-space without
1052 * using any of the bound attributes.
1053 */
1054 if (!nlk->bound)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 err = netlink_autobind(sock);
1056
1057 if (err == 0) {
1058 sk->sk_state = NETLINK_CONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001059 nlk->dst_portid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001060 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062
1063 return err;
1064}
1065
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001066static int netlink_getname(struct socket *sock, struct sockaddr *addr,
1067 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 struct sock *sk = sock->sk;
1070 struct netlink_sock *nlk = nlk_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001071 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 nladdr->nl_family = AF_NETLINK;
1074 nladdr->nl_pad = 0;
1075 *addr_len = sizeof(*nladdr);
1076
1077 if (peer) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001078 nladdr->nl_pid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001079 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001081 nladdr->nl_pid = nlk->portid;
Patrick McHardy513c2502005-09-06 15:43:59 -07001082 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084 return 0;
1085}
1086
David Decotigny025c6812016-03-21 10:15:35 -07001087static int netlink_ioctl(struct socket *sock, unsigned int cmd,
1088 unsigned long arg)
1089{
1090 /* try to hand this ioctl down to the NIC drivers.
1091 */
1092 return -ENOIOCTLCMD;
1093}
1094
Eric W. Biederman15e47302012-09-07 20:12:54 +00001095static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct sock *sock;
1098 struct netlink_sock *nlk;
1099
Eric W. Biederman15e47302012-09-07 20:12:54 +00001100 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (!sock)
1102 return ERR_PTR(-ECONNREFUSED);
1103
1104 /* Don't bother queuing skb if kernel socket has no input function */
1105 nlk = nlk_sk(sock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001106 if (sock->sk_state == NETLINK_CONNECTED &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001107 nlk->dst_portid != nlk_sk(ssk)->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 sock_put(sock);
1109 return ERR_PTR(-ECONNREFUSED);
1110 }
1111 return sock;
1112}
1113
1114struct sock *netlink_getsockbyfilp(struct file *filp)
1115{
Al Viro496ad9a2013-01-23 17:07:38 -05001116 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 struct sock *sock;
1118
1119 if (!S_ISSOCK(inode->i_mode))
1120 return ERR_PTR(-ENOTSOCK);
1121
1122 sock = SOCKET_I(inode)->sk;
1123 if (sock->sk_family != AF_NETLINK)
1124 return ERR_PTR(-EINVAL);
1125
1126 sock_hold(sock);
1127 return sock;
1128}
1129
Pablo Neira3a365152013-06-28 03:04:23 +02001130static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
1131 int broadcast)
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001132{
1133 struct sk_buff *skb;
1134 void *data;
1135
Pablo Neira3a365152013-06-28 03:04:23 +02001136 if (size <= NLMSG_GOODSIZE || broadcast)
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001137 return alloc_skb(size, GFP_KERNEL);
1138
Pablo Neira3a365152013-06-28 03:04:23 +02001139 size = SKB_DATA_ALIGN(size) +
1140 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001141
1142 data = vmalloc(size);
1143 if (data == NULL)
Pablo Neira3a365152013-06-28 03:04:23 +02001144 return NULL;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001145
Eric Dumazet2ea2f622015-04-24 16:05:01 -07001146 skb = __build_skb(data, size);
Pablo Neira3a365152013-06-28 03:04:23 +02001147 if (skb == NULL)
1148 vfree(data);
Eric Dumazet2ea2f622015-04-24 16:05:01 -07001149 else
Pablo Neira3a365152013-06-28 03:04:23 +02001150 skb->destructor = netlink_skb_destructor;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001151
1152 return skb;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001153}
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155/*
1156 * Attach a skb to a netlink socket.
1157 * The caller must hold a reference to the destination socket. On error, the
1158 * reference is dropped. The skb is not send to the destination, just all
1159 * all error checks are performed and memory in the queue is reserved.
1160 * Return values:
1161 * < 0: error. skb freed, reference to sock dropped.
1162 * 0: continue
1163 * 1: repeat lookup - reference dropped while waiting for socket memory.
1164 */
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001165int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001166 long *timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 struct netlink_sock *nlk;
1169
1170 nlk = nlk_sk(sk);
1171
Patrick McHardy5fd96122013-04-17 06:47:03 +00001172 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
Florian Westphald1b4c682016-02-18 15:03:24 +01001173 test_bit(NETLINK_S_CONGESTED, &nlk->state))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 DECLARE_WAITQUEUE(wait, current);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001175 if (!*timeo) {
Denis V. Lunevaed81562007-10-10 21:14:32 -07001176 if (!ssk || netlink_is_kernel(ssk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 netlink_overrun(sk);
1178 sock_put(sk);
1179 kfree_skb(skb);
1180 return -EAGAIN;
1181 }
1182
1183 __set_current_state(TASK_INTERRUPTIBLE);
1184 add_wait_queue(&nlk->wait, &wait);
1185
1186 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001187 test_bit(NETLINK_S_CONGESTED, &nlk->state)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 !sock_flag(sk, SOCK_DEAD))
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001189 *timeo = schedule_timeout(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 __set_current_state(TASK_RUNNING);
1192 remove_wait_queue(&nlk->wait, &wait);
1193 sock_put(sk);
1194
1195 if (signal_pending(current)) {
1196 kfree_skb(skb);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001197 return sock_intr_errno(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199 return 1;
1200 }
Patrick McHardycf0a0182013-04-17 06:47:00 +00001201 netlink_skb_set_owner_r(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return 0;
1203}
1204
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001205static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 int len = skb->len;
1208
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +02001209 netlink_deliver_tap(skb);
1210
Florian Westphald1b4c682016-02-18 15:03:24 +01001211 skb_queue_tail(&sk->sk_receive_queue, skb);
David S. Miller676d2362014-04-11 16:15:36 -04001212 sk->sk_data_ready(sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001213 return len;
1214}
1215
1216int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1217{
1218 int len = __netlink_sendskb(sk, skb);
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 sock_put(sk);
1221 return len;
1222}
1223
1224void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1225{
1226 kfree_skb(skb);
1227 sock_put(sk);
1228}
1229
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001230static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 int delta;
1233
Patrick McHardy1298ca42013-04-17 06:46:59 +00001234 WARN_ON(skb->sk != NULL);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001235 delta = skb->end - skb->tail;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001236 if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return skb;
1238
1239 if (skb_shared(skb)) {
1240 struct sk_buff *nskb = skb_clone(skb, allocation);
1241 if (!nskb)
1242 return skb;
Eric Dumazet8460c002012-04-19 02:24:28 +00001243 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 skb = nskb;
1245 }
1246
Eric Dumazet158f3232017-01-27 07:11:27 -08001247 pskb_expand_head(skb, 0, -delta,
1248 (allocation & ~__GFP_DIRECT_RECLAIM) |
1249 __GFP_NOWARN | __GFP_NORETRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return skb;
1251}
1252
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001253static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1254 struct sock *ssk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001255{
1256 int ret;
1257 struct netlink_sock *nlk = nlk_sk(sk);
1258
1259 ret = -ECONNREFUSED;
1260 if (nlk->netlink_rcv != NULL) {
1261 ret = skb->len;
Patrick McHardycf0a0182013-04-17 06:47:00 +00001262 netlink_skb_set_owner_r(skb, sk);
Patrick McHardye32123e2013-04-17 06:46:57 +00001263 NETLINK_CB(skb).sk = ssk;
Daniel Borkmann73bfd372013-12-23 14:35:55 +01001264 netlink_deliver_tap_kernel(sk, ssk, skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001265 nlk->netlink_rcv(skb);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00001266 consume_skb(skb);
1267 } else {
1268 kfree_skb(skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001269 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001270 sock_put(sk);
1271 return ret;
1272}
1273
1274int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001275 u32 portid, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 struct sock *sk;
1278 int err;
1279 long timeo;
1280
1281 skb = netlink_trim(skb, gfp_any());
1282
1283 timeo = sock_sndtimeo(ssk, nonblock);
1284retry:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001285 sk = netlink_getsockbyportid(ssk, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (IS_ERR(sk)) {
1287 kfree_skb(skb);
1288 return PTR_ERR(sk);
1289 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001290 if (netlink_is_kernel(sk))
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001291 return netlink_unicast_kernel(sk, skb, ssk);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001292
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001293 if (sk_filter(sk, skb)) {
Wang Chen84874602008-07-01 19:55:09 -07001294 err = skb->len;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001295 kfree_skb(skb);
1296 sock_put(sk);
1297 return err;
1298 }
1299
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001300 err = netlink_attachskb(sk, skb, &timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (err == 1)
1302 goto retry;
1303 if (err)
1304 return err;
1305
Denis V. Lunev7ee015e2007-10-10 21:14:03 -07001306 return netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001308EXPORT_SYMBOL(netlink_unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Patrick McHardy4277a082006-03-20 18:52:01 -08001310int netlink_has_listeners(struct sock *sk, unsigned int group)
1311{
1312 int res = 0;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001313 struct listeners *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -08001314
Denis V. Lunevaed81562007-10-10 21:14:32 -07001315 BUG_ON(!netlink_is_kernel(sk));
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001316
1317 rcu_read_lock();
1318 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1319
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001320 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001321 res = test_bit(group - 1, listeners->masks);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001322
1323 rcu_read_unlock();
1324
Patrick McHardy4277a082006-03-20 18:52:01 -08001325 return res;
1326}
1327EXPORT_SYMBOL_GPL(netlink_has_listeners);
1328
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001329static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 struct netlink_sock *nlk = nlk_sk(sk);
1332
1333 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001334 !test_bit(NETLINK_S_CONGESTED, &nlk->state)) {
Patrick McHardycf0a0182013-04-17 06:47:00 +00001335 netlink_skb_set_owner_r(skb, sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001336 __netlink_sendskb(sk, skb);
stephen hemminger2c6458002011-12-22 08:52:03 +00001337 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339 return -1;
1340}
1341
1342struct netlink_broadcast_data {
1343 struct sock *exclude_sk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001344 struct net *net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001345 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 u32 group;
1347 int failure;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001348 int delivery_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int congested;
1350 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -04001351 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 struct sk_buff *skb, *skb2;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001353 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1354 void *tx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355};
1356
Rami Rosen46c95212014-07-01 21:17:35 +03001357static void do_one_broadcast(struct sock *sk,
1358 struct netlink_broadcast_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 struct netlink_sock *nlk = nlk_sk(sk);
1361 int val;
1362
1363 if (p->exclude_sk == sk)
Rami Rosen46c95212014-07-01 21:17:35 +03001364 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Eric W. Biederman15e47302012-09-07 20:12:54 +00001366 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001367 !test_bit(p->group - 1, nlk->groups))
Rami Rosen46c95212014-07-01 21:17:35 +03001368 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Nicolas Dichtel59324cf2015-05-07 11:02:53 +02001370 if (!net_eq(sock_net(sk), p->net)) {
1371 if (!(nlk->flags & NETLINK_F_LISTEN_ALL_NSID))
1372 return;
1373
1374 if (!peernet_has_id(sock_net(sk), p->net))
1375 return;
1376
1377 if (!file_ns_capable(sk->sk_socket->file, p->net->user_ns,
1378 CAP_NET_BROADCAST))
1379 return;
1380 }
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (p->failure) {
1383 netlink_overrun(sk);
Rami Rosen46c95212014-07-01 21:17:35 +03001384 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386
1387 sock_hold(sk);
1388 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001389 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 p->skb2 = skb_clone(p->skb, p->allocation);
1391 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001392 p->skb2 = skb_get(p->skb);
1393 /*
1394 * skb ownership may have been set when
1395 * delivered to a previous socket.
1396 */
1397 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 }
1400 if (p->skb2 == NULL) {
1401 netlink_overrun(sk);
1402 /* Clone failed. Notify ALL listeners. */
1403 p->failure = 1;
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001404 if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001405 p->delivery_failure = 1;
Nicolas Dichtel59324cf2015-05-07 11:02:53 +02001406 goto out;
1407 }
1408 if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001409 kfree_skb(p->skb2);
1410 p->skb2 = NULL;
Nicolas Dichtel59324cf2015-05-07 11:02:53 +02001411 goto out;
1412 }
1413 if (sk_filter(sk, p->skb2)) {
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001414 kfree_skb(p->skb2);
1415 p->skb2 = NULL;
Nicolas Dichtel59324cf2015-05-07 11:02:53 +02001416 goto out;
1417 }
1418 NETLINK_CB(p->skb2).nsid = peernet2id(sock_net(sk), p->net);
Nicolas Dichtel72124622017-06-01 10:00:07 +02001419 if (NETLINK_CB(p->skb2).nsid != NETNSA_NSID_NOT_ASSIGNED)
1420 NETLINK_CB(p->skb2).nsid_is_set = true;
Nicolas Dichtel59324cf2015-05-07 11:02:53 +02001421 val = netlink_broadcast_deliver(sk, p->skb2);
1422 if (val < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 netlink_overrun(sk);
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001424 if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001425 p->delivery_failure = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 } else {
1427 p->congested |= val;
1428 p->delivered = 1;
1429 p->skb2 = NULL;
1430 }
Nicolas Dichtel59324cf2015-05-07 11:02:53 +02001431out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
Eric W. Biederman15e47302012-09-07 20:12:54 +00001435int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001436 u32 group, gfp_t allocation,
1437 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1438 void *filter_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001440 struct net *net = sock_net(ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 struct netlink_broadcast_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 struct sock *sk;
1443
1444 skb = netlink_trim(skb, allocation);
1445
1446 info.exclude_sk = ssk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001447 info.net = net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001448 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 info.group = group;
1450 info.failure = 0;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001451 info.delivery_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 info.congested = 0;
1453 info.delivered = 0;
1454 info.allocation = allocation;
1455 info.skb = skb;
1456 info.skb2 = NULL;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001457 info.tx_filter = filter;
1458 info.tx_data = filter_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 /* While we sleep in clone, do not allow to change socket list */
1461
1462 netlink_lock_table();
1463
Sasha Levinb67bfe02013-02-27 17:06:00 -08001464 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 do_one_broadcast(sk, &info);
1466
Neil Horman70d4bf62010-07-20 06:45:56 +00001467 consume_skb(skb);
Tommy S. Christensenaa1c6a6f2005-05-19 13:07:32 -07001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 netlink_unlock_table();
1470
Neil Horman70d4bf62010-07-20 06:45:56 +00001471 if (info.delivery_failure) {
1472 kfree_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001473 return -ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001474 }
1475 consume_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (info.delivered) {
Mel Gormand0164ad2015-11-06 16:28:21 -08001478 if (info.congested && gfpflags_allow_blocking(allocation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 yield();
1480 return 0;
1481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return -ESRCH;
1483}
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001484EXPORT_SYMBOL(netlink_broadcast_filtered);
1485
Eric W. Biederman15e47302012-09-07 20:12:54 +00001486int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001487 u32 group, gfp_t allocation)
1488{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001489 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001490 NULL, NULL);
1491}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001492EXPORT_SYMBOL(netlink_broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494struct netlink_set_err_data {
1495 struct sock *exclude_sk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001496 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 u32 group;
1498 int code;
1499};
1500
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001501static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 struct netlink_sock *nlk = nlk_sk(sk);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001504 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 if (sk == p->exclude_sk)
1507 goto out;
1508
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001509 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001510 goto out;
1511
Eric W. Biederman15e47302012-09-07 20:12:54 +00001512 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001513 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 goto out;
1515
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001516 if (p->code == ENOBUFS && nlk->flags & NETLINK_F_RECV_NO_ENOBUFS) {
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001517 ret = 1;
1518 goto out;
1519 }
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 sk->sk_err = p->code;
1522 sk->sk_error_report(sk);
1523out:
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001524 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001527/**
1528 * netlink_set_err - report error to broadcast listeners
1529 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
Eric W. Biederman15e47302012-09-07 20:12:54 +00001530 * @portid: the PORTID of a process that we want to skip (if any)
Johannes Berg840e93f22013-11-19 10:35:40 +01001531 * @group: the broadcast group that will notice the error
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001532 * @code: error code, must be negative (as usual in kernelspace)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001533 *
1534 * This function returns the number of broadcast listeners that have set the
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001535 * NETLINK_NO_ENOBUFS socket option.
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001536 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001537int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
1539 struct netlink_set_err_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 struct sock *sk;
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001541 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 info.exclude_sk = ssk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001544 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 info.group = group;
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001546 /* sk->sk_err wants a positive error value */
1547 info.code = -code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 read_lock(&nl_table_lock);
1550
Sasha Levinb67bfe02013-02-27 17:06:00 -08001551 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001552 ret += do_one_set_err(sk, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 read_unlock(&nl_table_lock);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001555 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +01001557EXPORT_SYMBOL(netlink_set_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Johannes Berg84659eb2007-07-18 15:47:05 -07001559/* must be called with netlink table grabbed */
1560static void netlink_update_socket_mc(struct netlink_sock *nlk,
1561 unsigned int group,
1562 int is_new)
1563{
1564 int old, new = !!is_new, subscriptions;
1565
1566 old = test_bit(group - 1, nlk->groups);
1567 subscriptions = nlk->subscriptions - old + new;
1568 if (new)
1569 __set_bit(group - 1, nlk->groups);
1570 else
1571 __clear_bit(group - 1, nlk->groups);
1572 netlink_update_subscriptions(&nlk->sk, subscriptions);
1573 netlink_update_listeners(&nlk->sk);
1574}
1575
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001576static int netlink_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001577 char __user *optval, unsigned int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001578{
1579 struct sock *sk = sock->sk;
1580 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07001581 unsigned int val = 0;
1582 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001583
1584 if (level != SOL_NETLINK)
1585 return -ENOPROTOOPT;
1586
Florian Westphald1b4c682016-02-18 15:03:24 +01001587 if (optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07001588 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001589 return -EFAULT;
1590
1591 switch (optname) {
1592 case NETLINK_PKTINFO:
1593 if (val)
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001594 nlk->flags |= NETLINK_F_RECV_PKTINFO;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001595 else
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001596 nlk->flags &= ~NETLINK_F_RECV_PKTINFO;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001597 err = 0;
1598 break;
1599 case NETLINK_ADD_MEMBERSHIP:
1600 case NETLINK_DROP_MEMBERSHIP: {
Eric W. Biederman5187cd02014-04-23 14:25:48 -07001601 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001602 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001603 err = netlink_realloc_groups(sk);
1604 if (err)
1605 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001606 if (!val || val - 1 >= nlk->ngroups)
1607 return -EINVAL;
Richard Guy Briggs7774d5e2014-04-22 21:31:55 -04001608 if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
Johannes Berg023e2cf2014-12-23 21:00:06 +01001609 err = nlk->netlink_bind(sock_net(sk), val);
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001610 if (err)
1611 return err;
1612 }
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001613 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07001614 netlink_update_socket_mc(nlk, val,
1615 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001616 netlink_table_ungrab();
Richard Guy Briggs7774d5e2014-04-22 21:31:55 -04001617 if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
Johannes Berg023e2cf2014-12-23 21:00:06 +01001618 nlk->netlink_unbind(sock_net(sk), val);
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001619
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001620 err = 0;
1621 break;
1622 }
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001623 case NETLINK_BROADCAST_ERROR:
1624 if (val)
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001625 nlk->flags |= NETLINK_F_BROADCAST_SEND_ERROR;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001626 else
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001627 nlk->flags &= ~NETLINK_F_BROADCAST_SEND_ERROR;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001628 err = 0;
1629 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001630 case NETLINK_NO_ENOBUFS:
1631 if (val) {
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001632 nlk->flags |= NETLINK_F_RECV_NO_ENOBUFS;
1633 clear_bit(NETLINK_S_CONGESTED, &nlk->state);
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001634 wake_up_interruptible(&nlk->wait);
Eric Dumazet658cb352012-04-22 21:30:21 +00001635 } else {
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001636 nlk->flags &= ~NETLINK_F_RECV_NO_ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001637 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001638 err = 0;
1639 break;
Nicolas Dichtel59324cf2015-05-07 11:02:53 +02001640 case NETLINK_LISTEN_ALL_NSID:
1641 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_BROADCAST))
1642 return -EPERM;
1643
1644 if (val)
1645 nlk->flags |= NETLINK_F_LISTEN_ALL_NSID;
1646 else
1647 nlk->flags &= ~NETLINK_F_LISTEN_ALL_NSID;
1648 err = 0;
1649 break;
Christophe Ricard0a6a3a22015-08-28 07:07:48 +02001650 case NETLINK_CAP_ACK:
1651 if (val)
1652 nlk->flags |= NETLINK_F_CAP_ACK;
1653 else
1654 nlk->flags &= ~NETLINK_F_CAP_ACK;
1655 err = 0;
1656 break;
Johannes Berg2d4bc932017-04-12 14:34:04 +02001657 case NETLINK_EXT_ACK:
1658 if (val)
1659 nlk->flags |= NETLINK_F_EXT_ACK;
1660 else
1661 nlk->flags &= ~NETLINK_F_EXT_ACK;
1662 err = 0;
1663 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001664 default:
1665 err = -ENOPROTOOPT;
1666 }
1667 return err;
1668}
1669
1670static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001671 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001672{
1673 struct sock *sk = sock->sk;
1674 struct netlink_sock *nlk = nlk_sk(sk);
1675 int len, val, err;
1676
1677 if (level != SOL_NETLINK)
1678 return -ENOPROTOOPT;
1679
1680 if (get_user(len, optlen))
1681 return -EFAULT;
1682 if (len < 0)
1683 return -EINVAL;
1684
1685 switch (optname) {
1686 case NETLINK_PKTINFO:
1687 if (len < sizeof(int))
1688 return -EINVAL;
1689 len = sizeof(int);
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001690 val = nlk->flags & NETLINK_F_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08001691 if (put_user(len, optlen) ||
1692 put_user(val, optval))
1693 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001694 err = 0;
1695 break;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001696 case NETLINK_BROADCAST_ERROR:
1697 if (len < sizeof(int))
1698 return -EINVAL;
1699 len = sizeof(int);
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001700 val = nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR ? 1 : 0;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001701 if (put_user(len, optlen) ||
1702 put_user(val, optval))
1703 return -EFAULT;
1704 err = 0;
1705 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001706 case NETLINK_NO_ENOBUFS:
1707 if (len < sizeof(int))
1708 return -EINVAL;
1709 len = sizeof(int);
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001710 val = nlk->flags & NETLINK_F_RECV_NO_ENOBUFS ? 1 : 0;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001711 if (put_user(len, optlen) ||
1712 put_user(val, optval))
1713 return -EFAULT;
1714 err = 0;
1715 break;
David Herrmannb42be382015-06-17 17:14:33 +02001716 case NETLINK_LIST_MEMBERSHIPS: {
1717 int pos, idx, shift;
1718
1719 err = 0;
David Herrmann47191d62015-10-21 11:47:43 +02001720 netlink_lock_table();
David Herrmannb42be382015-06-17 17:14:33 +02001721 for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) {
1722 if (len - pos < sizeof(u32))
1723 break;
1724
1725 idx = pos / sizeof(unsigned long);
1726 shift = (pos % sizeof(unsigned long)) * 8;
1727 if (put_user((u32)(nlk->groups[idx] >> shift),
1728 (u32 __user *)(optval + pos))) {
1729 err = -EFAULT;
1730 break;
1731 }
1732 }
1733 if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen))
1734 err = -EFAULT;
David Herrmann47191d62015-10-21 11:47:43 +02001735 netlink_unlock_table();
David Herrmannb42be382015-06-17 17:14:33 +02001736 break;
1737 }
Christophe Ricard0a6a3a22015-08-28 07:07:48 +02001738 case NETLINK_CAP_ACK:
1739 if (len < sizeof(int))
1740 return -EINVAL;
1741 len = sizeof(int);
1742 val = nlk->flags & NETLINK_F_CAP_ACK ? 1 : 0;
1743 if (put_user(len, optlen) ||
1744 put_user(val, optval))
1745 return -EFAULT;
1746 err = 0;
1747 break;
Johannes Berg2d4bc932017-04-12 14:34:04 +02001748 case NETLINK_EXT_ACK:
1749 if (len < sizeof(int))
1750 return -EINVAL;
1751 len = sizeof(int);
1752 val = nlk->flags & NETLINK_F_EXT_ACK ? 1 : 0;
1753 if (put_user(len, optlen) || put_user(val, optval))
1754 return -EFAULT;
1755 err = 0;
1756 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001757 default:
1758 err = -ENOPROTOOPT;
1759 }
1760 return err;
1761}
1762
1763static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1764{
1765 struct nl_pktinfo info;
1766
1767 info.group = NETLINK_CB(skb).dst_group;
1768 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1769}
1770
Nicolas Dichtel59324cf2015-05-07 11:02:53 +02001771static void netlink_cmsg_listen_all_nsid(struct sock *sk, struct msghdr *msg,
1772 struct sk_buff *skb)
1773{
1774 if (!NETLINK_CB(skb).nsid_is_set)
1775 return;
1776
1777 put_cmsg(msg, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, sizeof(int),
1778 &NETLINK_CB(skb).nsid);
1779}
1780
Ying Xue1b784142015-03-02 15:37:48 +08001781static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 struct sock *sk = sock->sk;
1784 struct netlink_sock *nlk = nlk_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001785 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001786 u32 dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001787 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 struct sk_buff *skb;
1789 int err;
1790 struct scm_cookie scm;
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07001791 u32 netlink_skb_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 if (msg->msg_flags&MSG_OOB)
1794 return -EOPNOTSUPP;
1795
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001796 err = scm_send(sock, msg, &scm, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (err < 0)
1798 return err;
1799
1800 if (msg->msg_namelen) {
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001801 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (addr->nl_family != AF_NETLINK)
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001803 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001804 dst_portid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001805 dst_group = ffs(addr->nl_groups);
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001806 err = -EPERM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001807 if ((dst_group || dst_portid) &&
Eric W. Biederman5187cd02014-04-23 14:25:48 -07001808 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001809 goto out;
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07001810 netlink_skb_flags |= NETLINK_SKB_DST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001812 dst_portid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001813 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
1815
Herbert Xuda314c92015-09-22 11:38:56 +08001816 if (!nlk->bound) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 err = netlink_autobind(sock);
1818 if (err)
1819 goto out;
Herbert Xuda314c92015-09-22 11:38:56 +08001820 } else {
1821 /* Ensure nlk is hashed and visible. */
1822 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824
1825 err = -EMSGSIZE;
1826 if (len > sk->sk_sndbuf - 32)
1827 goto out;
1828 err = -ENOBUFS;
Pablo Neira3a365152013-06-28 03:04:23 +02001829 skb = netlink_alloc_large_skb(len, dst_group);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001830 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 goto out;
1832
Eric W. Biederman15e47302012-09-07 20:12:54 +00001833 NETLINK_CB(skb).portid = nlk->portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001834 NETLINK_CB(skb).dst_group = dst_group;
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001835 NETLINK_CB(skb).creds = scm.creds;
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07001836 NETLINK_CB(skb).flags = netlink_skb_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 err = -EFAULT;
Al Viro6ce8e9c2014-04-06 21:25:44 -04001839 if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 kfree_skb(skb);
1841 goto out;
1842 }
1843
1844 err = security_netlink_send(sk, skb);
1845 if (err) {
1846 kfree_skb(skb);
1847 goto out;
1848 }
1849
Patrick McHardyd629b832005-08-14 19:27:50 -07001850 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001852 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001854 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856out:
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001857 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return err;
1859}
1860
Ying Xue1b784142015-03-02 15:37:48 +08001861static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 int flags)
1863{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 struct scm_cookie scm;
1865 struct sock *sk = sock->sk;
1866 struct netlink_sock *nlk = nlk_sk(sk);
1867 int noblock = flags&MSG_DONTWAIT;
1868 size_t copied;
Johannes Berg68d6ac62010-08-15 21:20:44 +00001869 struct sk_buff *skb, *data_skb;
Andrey Vaginb44d2112011-02-21 02:40:47 +00001870 int err, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 if (flags&MSG_OOB)
1873 return -EOPNOTSUPP;
1874
1875 copied = 0;
1876
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001877 skb = skb_recv_datagram(sk, flags, noblock, &err);
1878 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 goto out;
1880
Johannes Berg68d6ac62010-08-15 21:20:44 +00001881 data_skb = skb;
1882
Johannes Berg1dacc762009-07-01 11:26:02 +00001883#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1884 if (unlikely(skb_shinfo(skb)->frag_list)) {
Johannes Berg1dacc762009-07-01 11:26:02 +00001885 /*
Johannes Berg68d6ac62010-08-15 21:20:44 +00001886 * If this skb has a frag_list, then here that means that we
1887 * will have to use the frag_list skb's data for compat tasks
1888 * and the regular skb's data for normal (non-compat) tasks.
Johannes Berg1dacc762009-07-01 11:26:02 +00001889 *
Johannes Berg68d6ac62010-08-15 21:20:44 +00001890 * If we need to send the compat skb, assign it to the
1891 * 'data_skb' variable so that it will be used below for data
1892 * copying. We keep 'skb' for everything else, including
1893 * freeing both later.
Johannes Berg1dacc762009-07-01 11:26:02 +00001894 */
Johannes Berg68d6ac62010-08-15 21:20:44 +00001895 if (flags & MSG_CMSG_COMPAT)
1896 data_skb = skb_shinfo(skb)->frag_list;
Johannes Berg1dacc762009-07-01 11:26:02 +00001897 }
1898#endif
1899
Eric Dumazet9063e212014-03-07 12:02:33 -08001900 /* Record the max length of recvmsg() calls for future allocations */
1901 nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
1902 nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
Eric Dumazetd35c99f2016-10-06 04:13:18 +09001903 SKB_WITH_OVERHEAD(32768));
Eric Dumazet9063e212014-03-07 12:02:33 -08001904
Johannes Berg68d6ac62010-08-15 21:20:44 +00001905 copied = data_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (len < copied) {
1907 msg->msg_flags |= MSG_TRUNC;
1908 copied = len;
1909 }
1910
Johannes Berg68d6ac62010-08-15 21:20:44 +00001911 skb_reset_transport_header(data_skb);
David S. Miller51f3d022014-11-05 16:46:40 -05001912 err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 if (msg->msg_name) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001915 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 addr->nl_family = AF_NETLINK;
1917 addr->nl_pad = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001918 addr->nl_pid = NETLINK_CB(skb).portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001919 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 msg->msg_namelen = sizeof(*addr);
1921 }
1922
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02001923 if (nlk->flags & NETLINK_F_RECV_PKTINFO)
Patrick McHardycc9a06c2006-03-12 20:34:27 -08001924 netlink_cmsg_recv_pktinfo(msg, skb);
Nicolas Dichtel59324cf2015-05-07 11:02:53 +02001925 if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
1926 netlink_cmsg_listen_all_nsid(sk, msg, skb);
Patrick McHardycc9a06c2006-03-12 20:34:27 -08001927
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001928 memset(&scm, 0, sizeof(scm));
1929 scm.creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07001930 if (flags & MSG_TRUNC)
Johannes Berg68d6ac62010-08-15 21:20:44 +00001931 copied = data_skb->len;
David S. Millerdaa37662010-08-15 23:21:50 -07001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 skb_free_datagram(sk, skb);
1934
Pravin B Shelar16b304f2013-08-15 15:31:06 -07001935 if (nlk->cb_running &&
1936 atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
Andrey Vaginb44d2112011-02-21 02:40:47 +00001937 ret = netlink_dump(sk);
1938 if (ret) {
Ben Pfaffac30ef82014-07-09 10:31:22 -07001939 sk->sk_err = -ret;
Andrey Vaginb44d2112011-02-21 02:40:47 +00001940 sk->sk_error_report(sk);
1941 }
1942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001944 scm_recv(sock, msg, &scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945out:
1946 netlink_rcv_wake(sk);
1947 return err ? : copied;
1948}
1949
David S. Miller676d2362014-04-11 16:15:36 -04001950static void netlink_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001952 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954
1955/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001956 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 * complete set of kernel non-blocking support for message
1958 * queueing.
1959 */
1960
1961struct sock *
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00001962__netlink_kernel_create(struct net *net, int unit, struct module *module,
1963 struct netlink_kernel_cfg *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 struct socket *sock;
1966 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001967 struct netlink_sock *nlk;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001968 struct listeners *listeners = NULL;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001969 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
1970 unsigned int groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001972 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001974 if (unit < 0 || unit >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return NULL;
1976
1977 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1978 return NULL;
Eric W. Biederman13d30782015-05-08 21:11:33 -05001979
1980 if (__netlink_create(net, sock, cb_mutex, unit, 1) < 0)
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08001981 goto out_sock_release_nosk;
1982
1983 sk = sock->sk;
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001984
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001985 if (!cfg || cfg->groups < 32)
Patrick McHardy4277a082006-03-20 18:52:01 -08001986 groups = 32;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001987 else
1988 groups = cfg->groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001989
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001990 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
Patrick McHardy4277a082006-03-20 18:52:01 -08001991 if (!listeners)
1992 goto out_sock_release;
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 sk->sk_data_ready = netlink_data_ready;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001995 if (cfg && cfg->input)
1996 nlk_sk(sk)->netlink_rcv = cfg->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Herbert Xu8ea65f42015-01-26 14:02:56 +11001998 if (netlink_insert(sk, 0))
Patrick McHardy77247bb2005-08-14 19:27:13 -07001999 goto out_sock_release;
2000
2001 nlk = nlk_sk(sk);
Nicolas Dichtelcc3a5722015-05-07 11:02:52 +02002002 nlk->flags |= NETLINK_F_KERNEL_SOCKET;
Patrick McHardy77247bb2005-08-14 19:27:13 -07002003
2004 netlink_table_grab();
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002005 if (!nl_table[unit].registered) {
2006 nl_table[unit].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002007 rcu_assign_pointer(nl_table[unit].listeners, listeners);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002008 nl_table[unit].cb_mutex = cb_mutex;
2009 nl_table[unit].module = module;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002010 if (cfg) {
2011 nl_table[unit].bind = cfg->bind;
Hiroaki SHIMODA6251edd2014-11-13 04:24:10 +09002012 nl_table[unit].unbind = cfg->unbind;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002013 nl_table[unit].flags = cfg->flags;
Gao fengda12c902013-06-06 14:49:11 +08002014 if (cfg->compare)
2015 nl_table[unit].compare = cfg->compare;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002016 }
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002017 nl_table[unit].registered = 1;
Jesper Juhlf937f1f462007-10-15 01:39:12 -07002018 } else {
2019 kfree(listeners);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08002020 nl_table[unit].registered++;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002021 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07002022 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002023 return sk;
2024
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002025out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08002026 kfree(listeners);
Denis V. Lunev9dfbec12008-02-29 11:17:56 -08002027 netlink_kernel_release(sk);
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08002028 return NULL;
2029
2030out_sock_release_nosk:
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002031 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07002032 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002034EXPORT_SYMBOL(__netlink_kernel_create);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002035
2036void
2037netlink_kernel_release(struct sock *sk)
2038{
Eric W. Biederman13d30782015-05-08 21:11:33 -05002039 if (sk == NULL || sk->sk_socket == NULL)
2040 return;
2041
2042 sock_release(sk->sk_socket);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002043}
2044EXPORT_SYMBOL(netlink_kernel_release);
2045
Johannes Bergd136f1b2009-09-12 03:03:15 +00002046int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002047{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002048 struct listeners *new, *old;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002049 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002050
2051 if (groups < 32)
2052 groups = 32;
2053
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002054 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002055 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
2056 if (!new)
Johannes Bergd136f1b2009-09-12 03:03:15 +00002057 return -ENOMEM;
Eric Dumazet6d772ac2012-10-18 03:21:55 +00002058 old = nl_deref_protected(tbl->listeners);
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002059 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
2060 rcu_assign_pointer(tbl->listeners, new);
2061
Lai Jiangshan37b6b932011-03-15 18:01:42 +08002062 kfree_rcu(old, rcu);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002063 }
2064 tbl->groups = groups;
2065
Johannes Bergd136f1b2009-09-12 03:03:15 +00002066 return 0;
2067}
2068
2069/**
2070 * netlink_change_ngroups - change number of multicast groups
2071 *
2072 * This changes the number of multicast groups that are available
2073 * on a certain netlink family. Note that it is not possible to
2074 * change the number of groups to below 32. Also note that it does
2075 * not implicitly call netlink_clear_multicast_users() when the
2076 * number of groups is reduced.
2077 *
2078 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2079 * @groups: The new number of groups.
2080 */
2081int netlink_change_ngroups(struct sock *sk, unsigned int groups)
2082{
2083 int err;
2084
2085 netlink_table_grab();
2086 err = __netlink_change_ngroups(sk, groups);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002087 netlink_table_ungrab();
Johannes Bergd136f1b2009-09-12 03:03:15 +00002088
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002089 return err;
2090}
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002091
Johannes Bergb8273572009-09-24 15:44:05 -07002092void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2093{
2094 struct sock *sk;
Johannes Bergb8273572009-09-24 15:44:05 -07002095 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
2096
Sasha Levinb67bfe02013-02-27 17:06:00 -08002097 sk_for_each_bound(sk, &tbl->mc_list)
Johannes Bergb8273572009-09-24 15:44:05 -07002098 netlink_update_socket_mc(nlk_sk(sk), group, 0);
2099}
2100
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002101struct nlmsghdr *
Eric W. Biederman15e47302012-09-07 20:12:54 +00002102__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002103{
2104 struct nlmsghdr *nlh;
Hong zhi guo573ce262013-03-27 06:47:04 +00002105 int size = nlmsg_msg_size(len);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002106
Wang Yufen23b45672014-02-17 16:53:32 +08002107 nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size));
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002108 nlh->nlmsg_type = type;
2109 nlh->nlmsg_len = size;
2110 nlh->nlmsg_flags = flags;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002111 nlh->nlmsg_pid = portid;
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002112 nlh->nlmsg_seq = seq;
2113 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
Hong zhi guo573ce262013-03-27 06:47:04 +00002114 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002115 return nlh;
2116}
2117EXPORT_SYMBOL(__nlmsg_put);
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119/*
2120 * It looks a bit ugly.
2121 * It would be better to create kernel thread.
2122 */
2123
2124static int netlink_dump(struct sock *sk)
2125{
2126 struct netlink_sock *nlk = nlk_sk(sk);
2127 struct netlink_callback *cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00002128 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 struct nlmsghdr *nlh;
Herbert Xu92964c72016-05-16 17:28:16 +08002130 struct module *module;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002131 int len, err = -ENOBUFS;
Arad, Ronendb65a3a2015-10-15 01:55:17 -07002132 int alloc_min_size;
Greg Rosec7ac8672011-06-10 01:27:09 +00002133 int alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002135 mutex_lock(nlk->cb_mutex);
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002136 if (!nlk->cb_running) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002137 err = -EINVAL;
2138 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140
Florian Westphald1b4c682016-02-18 15:03:24 +01002141 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Patrick McHardyf9c22882013-04-17 06:47:04 +00002142 goto errout_skb;
Eric Dumazet9063e212014-03-07 12:02:33 -08002143
2144 /* NLMSG_GOODSIZE is small to avoid high order allocations being
2145 * required, but it makes sense to _attempt_ a 16K bytes allocation
2146 * to reduce number of system calls on dump operations, if user
2147 * ever provided a big enough buffer.
2148 */
Arad, Ronendb65a3a2015-10-15 01:55:17 -07002149 cb = &nlk->cb;
2150 alloc_min_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
2151
2152 if (alloc_min_size < nlk->max_recvmsg_len) {
2153 alloc_size = nlk->max_recvmsg_len;
Eric Dumazetd35c99f2016-10-06 04:13:18 +09002154 skb = alloc_skb(alloc_size,
2155 (GFP_KERNEL & ~__GFP_DIRECT_RECLAIM) |
2156 __GFP_NOWARN | __GFP_NORETRY);
Eric Dumazet9063e212014-03-07 12:02:33 -08002157 }
Arad, Ronendb65a3a2015-10-15 01:55:17 -07002158 if (!skb) {
2159 alloc_size = alloc_min_size;
Florian Westphalc5b0db32016-02-18 15:03:28 +01002160 skb = alloc_skb(alloc_size, GFP_KERNEL);
Arad, Ronendb65a3a2015-10-15 01:55:17 -07002161 }
Greg Rosec7ac8672011-06-10 01:27:09 +00002162 if (!skb)
Dan Carpenterc63d6ea2011-06-15 03:11:42 +00002163 goto errout_skb;
Arad, Ronendb65a3a2015-10-15 01:55:17 -07002164
2165 /* Trim skb to allocated size. User is expected to provide buffer as
2166 * large as max(min_dump_alloc, 16KiB (mac_recvmsg_len capped at
2167 * netlink_recvmsg())). dump will pack as many smaller messages as
2168 * could fit within the allocated skb. skb is typically allocated
2169 * with larger space than required (could be as much as near 2x the
2170 * requested size with align to next power of 2 approach). Allowing
2171 * dump to use the excess space makes it difficult for a user to have a
2172 * reasonable static buffer based on the expected largest dump of a
2173 * single netdev. The outcome is MSG_TRUNC error.
2174 */
Florian Westphald1b4c682016-02-18 15:03:24 +01002175 skb_reserve(skb, skb_tailroom(skb) - alloc_size);
Patrick McHardyf9c22882013-04-17 06:47:04 +00002176 netlink_skb_set_owner_r(skb, sk);
Greg Rosec7ac8672011-06-10 01:27:09 +00002177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 len = cb->dump(skb, cb);
2179
2180 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002181 mutex_unlock(nlk->cb_mutex);
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002182
2183 if (sk_filter(sk, skb))
2184 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002185 else
2186 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return 0;
2188 }
2189
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002190 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
2191 if (!nlh)
2192 goto errout_skb;
2193
Johannes Berg670dc282011-06-20 13:40:46 +02002194 nl_dump_check_consistent(cb, nlh);
2195
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002196 memcpy(nlmsg_data(nlh), &len, sizeof(len));
2197
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002198 if (sk_filter(sk, skb))
2199 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002200 else
2201 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Thomas Grafa8f74b22005-11-10 02:25:52 +01002203 if (cb->done)
2204 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002206 nlk->cb_running = false;
Herbert Xu92964c72016-05-16 17:28:16 +08002207 module = cb->module;
2208 skb = cb->skb;
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002209 mutex_unlock(nlk->cb_mutex);
Herbert Xu92964c72016-05-16 17:28:16 +08002210 module_put(module);
2211 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07002213
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002214errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002215 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002216 kfree_skb(skb);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002217 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
Gao feng6dc878a2012-10-04 20:15:48 +00002220int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2221 const struct nlmsghdr *nlh,
2222 struct netlink_dump_control *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
2224 struct netlink_callback *cb;
2225 struct sock *sk;
2226 struct netlink_sock *nlk;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002227 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Florian Westphald1b4c682016-02-18 15:03:24 +01002229 atomic_inc(&skb->users);
Patrick McHardyf9c22882013-04-17 06:47:04 +00002230
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002231 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
2232 if (sk == NULL) {
2233 ret = -ECONNREFUSED;
2234 goto error_free;
2235 }
2236
2237 nlk = nlk_sk(sk);
2238 mutex_lock(nlk->cb_mutex);
2239 /* A dump is in progress... */
2240 if (nlk->cb_running) {
2241 ret = -EBUSY;
2242 goto error_unlock;
2243 }
2244 /* add reference of module which cb->dump belongs to */
2245 if (!try_module_get(control->module)) {
2246 ret = -EPROTONOSUPPORT;
2247 goto error_unlock;
2248 }
2249
2250 cb = &nlk->cb;
2251 memset(cb, 0, sizeof(*cb));
Tom Herbertfc9e50f2015-12-15 15:41:37 -08002252 cb->start = control->start;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002253 cb->dump = control->dump;
2254 cb->done = control->done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 cb->nlh = nlh;
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +00002256 cb->data = control->data;
Gao feng6dc878a2012-10-04 20:15:48 +00002257 cb->module = control->module;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002258 cb->min_dump_alloc = control->min_dump_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 cb->skb = skb;
2260
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002261 nlk->cb_running = true;
Gao feng6dc878a2012-10-04 20:15:48 +00002262
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002263 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Tom Herbertfc9e50f2015-12-15 15:41:37 -08002265 if (cb->start)
2266 cb->start(cb);
2267
Andrey Vaginb44d2112011-02-21 02:40:47 +00002268 ret = netlink_dump(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 sock_put(sk);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002270
Andrey Vaginb44d2112011-02-21 02:40:47 +00002271 if (ret)
2272 return ret;
2273
Denis V. Lunev5c582982007-10-23 20:29:25 -07002274 /* We successfully started a dump, by returning -EINTR we
2275 * signal not to send ACK even if it was requested.
2276 */
2277 return -EINTR;
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002278
2279error_unlock:
2280 sock_put(sk);
2281 mutex_unlock(nlk->cb_mutex);
2282error_free:
2283 kfree_skb(skb);
2284 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
Gao feng6dc878a2012-10-04 20:15:48 +00002286EXPORT_SYMBOL(__netlink_dump_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Johannes Berg2d4bc932017-04-12 14:34:04 +02002288void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
2289 const struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
2291 struct sk_buff *skb;
2292 struct nlmsghdr *rep;
2293 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08002294 size_t payload = sizeof(*errmsg);
Johannes Berg2d4bc932017-04-12 14:34:04 +02002295 size_t tlvlen = 0;
Christophe Ricard0a6a3a22015-08-28 07:07:48 +02002296 struct netlink_sock *nlk = nlk_sk(NETLINK_CB(in_skb).sk);
Johannes Berg2d4bc932017-04-12 14:34:04 +02002297 unsigned int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Christophe Ricard0a6a3a22015-08-28 07:07:48 +02002299 /* Error messages get the original request appened, unless the user
Johannes Berg2d4bc932017-04-12 14:34:04 +02002300 * requests to cap the error message, and get extra error data if
2301 * requested.
Christophe Ricard0a6a3a22015-08-28 07:07:48 +02002302 */
Johannes Berg2d4bc932017-04-12 14:34:04 +02002303 if (err) {
2304 if (!(nlk->flags & NETLINK_F_CAP_ACK))
2305 payload += nlmsg_len(nlh);
2306 else
2307 flags |= NLM_F_CAPPED;
2308 if (nlk->flags & NETLINK_F_EXT_ACK && extack) {
2309 if (extack->_msg)
2310 tlvlen += nla_total_size(strlen(extack->_msg) + 1);
2311 if (extack->bad_attr)
2312 tlvlen += nla_total_size(sizeof(u32));
2313 }
2314 } else {
2315 flags |= NLM_F_CAPPED;
Johannes Bergba0dc5f2017-04-12 14:34:06 +02002316
2317 if (nlk->flags & NETLINK_F_EXT_ACK &&
2318 extack && extack->cookie_len)
2319 tlvlen += nla_total_size(extack->cookie_len);
Johannes Berg2d4bc932017-04-12 14:34:04 +02002320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Johannes Berg2d4bc932017-04-12 14:34:04 +02002322 if (tlvlen)
2323 flags |= NLM_F_ACK_TLVS;
2324
2325 skb = nlmsg_new(payload + tlvlen, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (!skb) {
2327 struct sock *sk;
2328
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002329 sk = netlink_lookup(sock_net(in_skb->sk),
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002330 in_skb->sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002331 NETLINK_CB(in_skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (sk) {
2333 sk->sk_err = ENOBUFS;
2334 sk->sk_error_report(sk);
2335 sock_put(sk);
2336 }
2337 return;
2338 }
2339
Eric W. Biederman15e47302012-09-07 20:12:54 +00002340 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
Johannes Berg2d4bc932017-04-12 14:34:04 +02002341 NLMSG_ERROR, payload, flags);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002342 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 errmsg->error = err;
Christophe Ricard0a6a3a22015-08-28 07:07:48 +02002344 memcpy(&errmsg->msg, nlh, payload > sizeof(*errmsg) ? nlh->nlmsg_len : sizeof(*nlh));
Johannes Berg2d4bc932017-04-12 14:34:04 +02002345
Johannes Bergba0dc5f2017-04-12 14:34:06 +02002346 if (nlk->flags & NETLINK_F_EXT_ACK && extack) {
2347 if (err) {
2348 if (extack->_msg)
2349 WARN_ON(nla_put_string(skb, NLMSGERR_ATTR_MSG,
2350 extack->_msg));
2351 if (extack->bad_attr &&
2352 !WARN_ON((u8 *)extack->bad_attr < in_skb->data ||
2353 (u8 *)extack->bad_attr >= in_skb->data +
2354 in_skb->len))
2355 WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS,
2356 (u8 *)extack->bad_attr -
2357 in_skb->data));
2358 } else {
2359 if (extack->cookie_len)
2360 WARN_ON(nla_put(skb, NLMSGERR_ATTR_COOKIE,
2361 extack->cookie_len,
2362 extack->cookie));
2363 }
Johannes Berg2d4bc932017-04-12 14:34:04 +02002364 }
2365
2366 nlmsg_end(skb, rep);
2367
Eric W. Biederman15e47302012-09-07 20:12:54 +00002368 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002370EXPORT_SYMBOL(netlink_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002372int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Johannes Berg2d4bc932017-04-12 14:34:04 +02002373 struct nlmsghdr *,
2374 struct netlink_ext_ack *))
Thomas Graf82ace472005-11-10 02:25:53 +01002375{
Johannes Berg2d4bc932017-04-12 14:34:04 +02002376 struct netlink_ext_ack extack = {};
Thomas Graf82ace472005-11-10 02:25:53 +01002377 struct nlmsghdr *nlh;
2378 int err;
2379
2380 while (skb->len >= nlmsg_total_size(0)) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002381 int msglen;
2382
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07002383 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07002384 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01002385
Martin Murrayad8e4b72006-01-10 13:02:29 -08002386 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01002387 return 0;
2388
Thomas Grafd35b6852007-03-22 23:28:46 -07002389 /* Only requests are handled by the kernel */
2390 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
Denis V. Lunev5c582982007-10-23 20:29:25 -07002391 goto ack;
Thomas Grafd35b6852007-03-22 23:28:46 -07002392
Thomas Graf45e7ae72007-03-22 23:29:10 -07002393 /* Skip control messages */
2394 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
Denis V. Lunev5c582982007-10-23 20:29:25 -07002395 goto ack;
Thomas Graf45e7ae72007-03-22 23:29:10 -07002396
Johannes Berg2d4bc932017-04-12 14:34:04 +02002397 err = cb(skb, nlh, &extack);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002398 if (err == -EINTR)
2399 goto skip;
2400
2401ack:
Thomas Grafd35b6852007-03-22 23:28:46 -07002402 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Johannes Berg2d4bc932017-04-12 14:34:04 +02002403 netlink_ack(skb, nlh, err, &extack);
Thomas Graf82ace472005-11-10 02:25:53 +01002404
Denis V. Lunev5c582982007-10-23 20:29:25 -07002405skip:
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002406 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002407 if (msglen > skb->len)
2408 msglen = skb->len;
2409 skb_pull(skb, msglen);
Thomas Graf82ace472005-11-10 02:25:53 +01002410 }
2411
2412 return 0;
2413}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002414EXPORT_SYMBOL(netlink_rcv_skb);
Thomas Graf82ace472005-11-10 02:25:53 +01002415
2416/**
Thomas Grafd387f6a2006-08-15 00:31:06 -07002417 * nlmsg_notify - send a notification netlink message
2418 * @sk: netlink socket to use
2419 * @skb: notification message
Eric W. Biederman15e47302012-09-07 20:12:54 +00002420 * @portid: destination netlink portid for reports or 0
Thomas Grafd387f6a2006-08-15 00:31:06 -07002421 * @group: destination multicast group or 0
2422 * @report: 1 to report back, 0 to disable
2423 * @flags: allocation flags
2424 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002425int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
Thomas Grafd387f6a2006-08-15 00:31:06 -07002426 unsigned int group, int report, gfp_t flags)
2427{
2428 int err = 0;
2429
2430 if (group) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002431 int exclude_portid = 0;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002432
2433 if (report) {
2434 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002435 exclude_portid = portid;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002436 }
2437
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002438 /* errors reported via destination sk->sk_err, but propagate
2439 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002440 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002441 }
2442
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002443 if (report) {
2444 int err2;
2445
Eric W. Biederman15e47302012-09-07 20:12:54 +00002446 err2 = nlmsg_unicast(sk, skb, portid);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002447 if (!err || err == -ESRCH)
2448 err = err2;
2449 }
Thomas Grafd387f6a2006-08-15 00:31:06 -07002450
2451 return err;
2452}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002453EXPORT_SYMBOL(nlmsg_notify);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455#ifdef CONFIG_PROC_FS
2456struct nl_seq_iter {
Denis V. Luneve372c412007-11-19 22:31:54 -08002457 struct seq_net_private p;
Herbert Xu56d28b12015-02-04 07:33:24 +11002458 struct rhashtable_iter hti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 int link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460};
2461
Herbert Xu56d28b12015-02-04 07:33:24 +11002462static int netlink_walk_start(struct nl_seq_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
Herbert Xu56d28b12015-02-04 07:33:24 +11002464 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Bob Copeland8f6fd832016-03-02 10:09:19 -05002466 err = rhashtable_walk_init(&nl_table[iter->link].hash, &iter->hti,
2467 GFP_KERNEL);
Herbert Xu56d28b12015-02-04 07:33:24 +11002468 if (err) {
2469 iter->link = MAX_LINKS;
2470 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
Herbert Xu56d28b12015-02-04 07:33:24 +11002472
2473 err = rhashtable_walk_start(&iter->hti);
2474 return err == -EAGAIN ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
Herbert Xu56d28b12015-02-04 07:33:24 +11002477static void netlink_walk_stop(struct nl_seq_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Herbert Xu56d28b12015-02-04 07:33:24 +11002479 rhashtable_walk_stop(&iter->hti);
2480 rhashtable_walk_exit(&iter->hti);
2481}
2482
2483static void *__netlink_seq_next(struct seq_file *seq)
2484{
2485 struct nl_seq_iter *iter = seq->private;
2486 struct netlink_sock *nlk;
2487
2488 do {
2489 for (;;) {
2490 int err;
2491
2492 nlk = rhashtable_walk_next(&iter->hti);
2493
2494 if (IS_ERR(nlk)) {
2495 if (PTR_ERR(nlk) == -EAGAIN)
2496 continue;
2497
2498 return nlk;
2499 }
2500
2501 if (nlk)
2502 break;
2503
2504 netlink_walk_stop(iter);
2505 if (++iter->link >= MAX_LINKS)
2506 return NULL;
2507
2508 err = netlink_walk_start(iter);
2509 if (err)
2510 return ERR_PTR(err);
2511 }
2512 } while (sock_net(&nlk->sk) != seq_file_net(seq));
2513
2514 return nlk;
2515}
2516
2517static void *netlink_seq_start(struct seq_file *seq, loff_t *posp)
2518{
2519 struct nl_seq_iter *iter = seq->private;
2520 void *obj = SEQ_START_TOKEN;
2521 loff_t pos;
2522 int err;
2523
2524 iter->link = 0;
2525
2526 err = netlink_walk_start(iter);
2527 if (err)
2528 return ERR_PTR(err);
2529
2530 for (pos = *posp; pos && obj && !IS_ERR(obj); pos--)
2531 obj = __netlink_seq_next(seq);
2532
2533 return obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
2535
2536static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2537{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 ++*pos;
Herbert Xu56d28b12015-02-04 07:33:24 +11002539 return __netlink_seq_next(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
2542static void netlink_seq_stop(struct seq_file *seq, void *v)
2543{
Herbert Xu56d28b12015-02-04 07:33:24 +11002544 struct nl_seq_iter *iter = seq->private;
2545
2546 if (iter->link >= MAX_LINKS)
2547 return;
2548
2549 netlink_walk_stop(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
2551
2552
2553static int netlink_seq_show(struct seq_file *seq, void *v)
2554{
Eric Dumazet658cb352012-04-22 21:30:21 +00002555 if (v == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 seq_puts(seq,
2557 "sk Eth Pid Groups "
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002558 "Rmem Wmem Dump Locks Drops Inode\n");
Eric Dumazet658cb352012-04-22 21:30:21 +00002559 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 struct sock *s = v;
2561 struct netlink_sock *nlk = nlk_sk(s);
2562
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002563 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 s,
2565 s->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002566 nlk->portid,
Patrick McHardy513c2502005-09-06 15:43:59 -07002567 nlk->groups ? (u32)nlk->groups[0] : 0,
Eric Dumazet31e6d362009-06-17 19:05:41 -07002568 sk_rmem_alloc_get(s),
2569 sk_wmem_alloc_get(s),
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002570 nlk->cb_running,
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002571 atomic_read(&s->sk_refcnt),
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002572 atomic_read(&s->sk_drops),
2573 sock_i_ino(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 );
2575
2576 }
2577 return 0;
2578}
2579
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002580static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 .start = netlink_seq_start,
2582 .next = netlink_seq_next,
2583 .stop = netlink_seq_stop,
2584 .show = netlink_seq_show,
2585};
2586
2587
2588static int netlink_seq_open(struct inode *inode, struct file *file)
2589{
Denis V. Luneve372c412007-11-19 22:31:54 -08002590 return seq_open_net(inode, file, &netlink_seq_ops,
2591 sizeof(struct nl_seq_iter));
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002592}
2593
Arjan van de Venda7071d2007-02-12 00:55:36 -08002594static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 .owner = THIS_MODULE,
2596 .open = netlink_seq_open,
2597 .read = seq_read,
2598 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002599 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600};
2601
2602#endif
2603
2604int netlink_register_notifier(struct notifier_block *nb)
2605{
WANG Congefa172f2016-12-09 21:10:59 -08002606 return blocking_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002608EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610int netlink_unregister_notifier(struct notifier_block *nb)
2611{
WANG Congefa172f2016-12-09 21:10:59 -08002612 return blocking_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002614EXPORT_SYMBOL(netlink_unregister_notifier);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002615
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002616static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 .family = PF_NETLINK,
2618 .owner = THIS_MODULE,
2619 .release = netlink_release,
2620 .bind = netlink_bind,
2621 .connect = netlink_connect,
2622 .socketpair = sock_no_socketpair,
2623 .accept = sock_no_accept,
2624 .getname = netlink_getname,
Florian Westphald1b4c682016-02-18 15:03:24 +01002625 .poll = datagram_poll,
David Decotigny025c6812016-03-21 10:15:35 -07002626 .ioctl = netlink_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 .listen = sock_no_listen,
2628 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002629 .setsockopt = netlink_setsockopt,
2630 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 .sendmsg = netlink_sendmsg,
2632 .recvmsg = netlink_recvmsg,
Florian Westphald1b4c682016-02-18 15:03:24 +01002633 .mmap = sock_no_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 .sendpage = sock_no_sendpage,
2635};
2636
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002637static const struct net_proto_family netlink_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 .family = PF_NETLINK,
2639 .create = netlink_create,
2640 .owner = THIS_MODULE, /* for consistency 8) */
2641};
2642
Pavel Emelyanov46650792007-10-08 20:38:39 -07002643static int __net_init netlink_net_init(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002644{
2645#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00002646 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002647 return -ENOMEM;
2648#endif
2649 return 0;
2650}
2651
Pavel Emelyanov46650792007-10-08 20:38:39 -07002652static void __net_exit netlink_net_exit(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002653{
2654#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002655 remove_proc_entry("netlink", net->proc_net);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002656#endif
2657}
2658
David S. Millerb963ea82010-08-30 19:08:01 -07002659static void __init netlink_add_usersock_entry(void)
2660{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002661 struct listeners *listeners;
David S. Millerb963ea82010-08-30 19:08:01 -07002662 int groups = 32;
2663
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002664 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
David S. Millerb963ea82010-08-30 19:08:01 -07002665 if (!listeners)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002666 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
David S. Millerb963ea82010-08-30 19:08:01 -07002667
2668 netlink_table_grab();
2669
2670 nl_table[NETLINK_USERSOCK].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002671 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
David S. Millerb963ea82010-08-30 19:08:01 -07002672 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2673 nl_table[NETLINK_USERSOCK].registered = 1;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002674 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
David S. Millerb963ea82010-08-30 19:08:01 -07002675
2676 netlink_table_ungrab();
2677}
2678
Denis V. Lunev022cbae2007-11-13 03:23:50 -08002679static struct pernet_operations __net_initdata netlink_net_ops = {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002680 .init = netlink_net_init,
2681 .exit = netlink_net_exit,
2682};
2683
Patrick McHardy49f7b332015-03-25 13:07:45 +00002684static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
Herbert Xuc428ecd2015-03-20 21:57:01 +11002685{
2686 const struct netlink_sock *nlk = data;
2687 struct netlink_compare_arg arg;
2688
Herbert Xuda314c92015-09-22 11:38:56 +08002689 netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
Herbert Xu11b58ba2015-03-24 00:50:22 +11002690 return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
Herbert Xuc428ecd2015-03-20 21:57:01 +11002691}
2692
2693static const struct rhashtable_params netlink_rhashtable_params = {
2694 .head_offset = offsetof(struct netlink_sock, node),
2695 .key_len = netlink_compare_arg_len,
Herbert Xuc428ecd2015-03-20 21:57:01 +11002696 .obj_hashfn = netlink_hash,
2697 .obj_cmpfn = netlink_compare,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002698 .automatic_shrinking = true,
Herbert Xuc428ecd2015-03-20 21:57:01 +11002699};
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701static int __init netlink_proto_init(void)
2702{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 int err = proto_register(&netlink_proto, 0);
2705
2706 if (err != 0)
2707 goto out;
2708
YOSHIFUJI Hideaki / 吉藤英明fab25742013-01-09 07:19:48 +00002709 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002711 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002712 if (!nl_table)
2713 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 for (i = 0; i < MAX_LINKS; i++) {
Herbert Xuc428ecd2015-03-20 21:57:01 +11002716 if (rhashtable_init(&nl_table[i].hash,
2717 &netlink_rhashtable_params) < 0) {
Thomas Grafe3416942014-08-02 11:47:45 +02002718 while (--i > 0)
2719 rhashtable_destroy(&nl_table[i].hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002721 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
2724
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +02002725 INIT_LIST_HEAD(&netlink_tap_all);
2726
David S. Millerb963ea82010-08-30 19:08:01 -07002727 netlink_add_usersock_entry();
2728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 sock_register(&netlink_family_ops);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002730 register_pernet_subsys(&netlink_net_ops);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002731 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 rtnetlink_init();
2733out:
2734 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002735panic:
2736 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737}
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739core_initcall(netlink_proto_init);