1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Routing netlink socket interface: protocol independent part.
7 *
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * Fixes:
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
17 */
18
19 #include <linux/bitops.h>
20 #include <linux/errno.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/kernel.h>
25 #include <linux/timer.h>
26 #include <linux/string.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/fcntl.h>
30 #include <linux/mm.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/capability.h>
34 #include <linux/skbuff.h>
35 #include <linux/init.h>
36 #include <linux/security.h>
37 #include <linux/mutex.h>
38 #include <linux/if_addr.h>
39 #include <linux/if_bridge.h>
40 #include <linux/if_vlan.h>
41 #include <linux/pci.h>
42 #include <linux/etherdevice.h>
43 #include <linux/bpf.h>
44
45 #include <linux/uaccess.h>
46
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <net/switchdev.h>
50 #include <net/ip.h>
51 #include <net/protocol.h>
52 #include <net/arp.h>
53 #include <net/route.h>
54 #include <net/udp.h>
55 #include <net/tcp.h>
56 #include <net/sock.h>
57 #include <net/pkt_sched.h>
58 #include <net/fib_rules.h>
59 #include <net/rtnetlink.h>
60 #include <net/net_namespace.h>
61
62 #define RTNL_MAX_TYPE 48
63 #define RTNL_SLAVE_MAX_TYPE 36
64
65 struct rtnl_link {
66 rtnl_doit_func doit;
67 rtnl_dumpit_func dumpit;
68 struct module *owner;
69 unsigned int flags;
70 struct rcu_head rcu;
71 };
72
73 static DEFINE_MUTEX(rtnl_mutex);
74
rtnl_lock(void)75 void rtnl_lock(void)
76 {
77 mutex_lock(&rtnl_mutex);
78 }
79 EXPORT_SYMBOL(rtnl_lock);
80
rtnl_lock_killable(void)81 int rtnl_lock_killable(void)
82 {
83 return mutex_lock_killable(&rtnl_mutex);
84 }
85 EXPORT_SYMBOL(rtnl_lock_killable);
86
87 static struct sk_buff *defer_kfree_skb_list;
rtnl_kfree_skbs(struct sk_buff * head,struct sk_buff * tail)88 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
89 {
90 if (head && tail) {
91 tail->next = defer_kfree_skb_list;
92 defer_kfree_skb_list = head;
93 }
94 }
95 EXPORT_SYMBOL(rtnl_kfree_skbs);
96
__rtnl_unlock(void)97 void __rtnl_unlock(void)
98 {
99 struct sk_buff *head = defer_kfree_skb_list;
100
101 defer_kfree_skb_list = NULL;
102
103 mutex_unlock(&rtnl_mutex);
104
105 while (head) {
106 struct sk_buff *next = head->next;
107
108 kfree_skb(head);
109 cond_resched();
110 head = next;
111 }
112 }
113
rtnl_unlock(void)114 void rtnl_unlock(void)
115 {
116 /* This fellow will unlock it for us. */
117 netdev_run_todo();
118 }
119 EXPORT_SYMBOL(rtnl_unlock);
120
rtnl_trylock(void)121 int rtnl_trylock(void)
122 {
123 return mutex_trylock(&rtnl_mutex);
124 }
125 EXPORT_SYMBOL(rtnl_trylock);
126
rtnl_is_locked(void)127 int rtnl_is_locked(void)
128 {
129 return mutex_is_locked(&rtnl_mutex);
130 }
131 EXPORT_SYMBOL(rtnl_is_locked);
132
133 #ifdef CONFIG_PROVE_LOCKING
lockdep_rtnl_is_held(void)134 bool lockdep_rtnl_is_held(void)
135 {
136 return lockdep_is_held(&rtnl_mutex);
137 }
138 EXPORT_SYMBOL(lockdep_rtnl_is_held);
139 #endif /* #ifdef CONFIG_PROVE_LOCKING */
140
141 static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
142
rtm_msgindex(int msgtype)143 static inline int rtm_msgindex(int msgtype)
144 {
145 int msgindex = msgtype - RTM_BASE;
146
147 /*
148 * msgindex < 0 implies someone tried to register a netlink
149 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
150 * the message type has not been added to linux/rtnetlink.h
151 */
152 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
153
154 return msgindex;
155 }
156
rtnl_get_link(int protocol,int msgtype)157 static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
158 {
159 struct rtnl_link **tab;
160
161 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
162 protocol = PF_UNSPEC;
163
164 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
165 if (!tab)
166 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
167
168 return tab[msgtype];
169 }
170
rtnl_register_internal(struct module * owner,int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit,unsigned int flags)171 static int rtnl_register_internal(struct module *owner,
172 int protocol, int msgtype,
173 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
174 unsigned int flags)
175 {
176 struct rtnl_link *link, *old;
177 struct rtnl_link __rcu **tab;
178 int msgindex;
179 int ret = -ENOBUFS;
180
181 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
182 msgindex = rtm_msgindex(msgtype);
183
184 rtnl_lock();
185 tab = rtnl_msg_handlers[protocol];
186 if (tab == NULL) {
187 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
188 if (!tab)
189 goto unlock;
190
191 /* ensures we see the 0 stores */
192 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
193 }
194
195 old = rtnl_dereference(tab[msgindex]);
196 if (old) {
197 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
198 if (!link)
199 goto unlock;
200 } else {
201 link = kzalloc(sizeof(*link), GFP_KERNEL);
202 if (!link)
203 goto unlock;
204 }
205
206 WARN_ON(link->owner && link->owner != owner);
207 link->owner = owner;
208
209 WARN_ON(doit && link->doit && link->doit != doit);
210 if (doit)
211 link->doit = doit;
212 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
213 if (dumpit)
214 link->dumpit = dumpit;
215
216 link->flags |= flags;
217
218 /* publish protocol:msgtype */
219 rcu_assign_pointer(tab[msgindex], link);
220 ret = 0;
221 if (old)
222 kfree_rcu(old, rcu);
223 unlock:
224 rtnl_unlock();
225 return ret;
226 }
227
228 /**
229 * rtnl_register_module - Register a rtnetlink message type
230 *
231 * @owner: module registering the hook (THIS_MODULE)
232 * @protocol: Protocol family or PF_UNSPEC
233 * @msgtype: rtnetlink message type
234 * @doit: Function pointer called for each request message
235 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
236 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
237 *
238 * Like rtnl_register, but for use by removable modules.
239 */
rtnl_register_module(struct module * owner,int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit,unsigned int flags)240 int rtnl_register_module(struct module *owner,
241 int protocol, int msgtype,
242 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
243 unsigned int flags)
244 {
245 return rtnl_register_internal(owner, protocol, msgtype,
246 doit, dumpit, flags);
247 }
248 EXPORT_SYMBOL_GPL(rtnl_register_module);
249
250 /**
251 * rtnl_register - Register a rtnetlink message type
252 * @protocol: Protocol family or PF_UNSPEC
253 * @msgtype: rtnetlink message type
254 * @doit: Function pointer called for each request message
255 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
256 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
257 *
258 * Registers the specified function pointers (at least one of them has
259 * to be non-NULL) to be called whenever a request message for the
260 * specified protocol family and message type is received.
261 *
262 * The special protocol family PF_UNSPEC may be used to define fallback
263 * function pointers for the case when no entry for the specific protocol
264 * family exists.
265 */
rtnl_register(int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit,unsigned int flags)266 void rtnl_register(int protocol, int msgtype,
267 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
268 unsigned int flags)
269 {
270 int err;
271
272 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
273 flags);
274 if (err)
275 pr_err("Unable to register rtnetlink message handler, "
276 "protocol = %d, message type = %d\n", protocol, msgtype);
277 }
278
279 /**
280 * rtnl_unregister - Unregister a rtnetlink message type
281 * @protocol: Protocol family or PF_UNSPEC
282 * @msgtype: rtnetlink message type
283 *
284 * Returns 0 on success or a negative error code.
285 */
rtnl_unregister(int protocol,int msgtype)286 int rtnl_unregister(int protocol, int msgtype)
287 {
288 struct rtnl_link **tab, *link;
289 int msgindex;
290
291 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
292 msgindex = rtm_msgindex(msgtype);
293
294 rtnl_lock();
295 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
296 if (!tab) {
297 rtnl_unlock();
298 return -ENOENT;
299 }
300
301 link = tab[msgindex];
302 rcu_assign_pointer(tab[msgindex], NULL);
303 rtnl_unlock();
304
305 kfree_rcu(link, rcu);
306
307 return 0;
308 }
309 EXPORT_SYMBOL_GPL(rtnl_unregister);
310
311 /**
312 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
313 * @protocol : Protocol family or PF_UNSPEC
314 *
315 * Identical to calling rtnl_unregster() for all registered message types
316 * of a certain protocol family.
317 */
rtnl_unregister_all(int protocol)318 void rtnl_unregister_all(int protocol)
319 {
320 struct rtnl_link **tab, *link;
321 int msgindex;
322
323 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
324
325 rtnl_lock();
326 tab = rtnl_msg_handlers[protocol];
327 if (!tab) {
328 rtnl_unlock();
329 return;
330 }
331 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
332 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
333 link = tab[msgindex];
334 if (!link)
335 continue;
336
337 rcu_assign_pointer(tab[msgindex], NULL);
338 kfree_rcu(link, rcu);
339 }
340 rtnl_unlock();
341
342 synchronize_net();
343
344 kfree(tab);
345 }
346 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
347
348 static LIST_HEAD(link_ops);
349
rtnl_link_ops_get(const char * kind)350 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
351 {
352 const struct rtnl_link_ops *ops;
353
354 list_for_each_entry(ops, &link_ops, list) {
355 if (!strcmp(ops->kind, kind))
356 return ops;
357 }
358 return NULL;
359 }
360
361 /**
362 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
363 * @ops: struct rtnl_link_ops * to register
364 *
365 * The caller must hold the rtnl_mutex. This function should be used
366 * by drivers that create devices during module initialization. It
367 * must be called before registering the devices.
368 *
369 * Returns 0 on success or a negative error code.
370 */
__rtnl_link_register(struct rtnl_link_ops * ops)371 int __rtnl_link_register(struct rtnl_link_ops *ops)
372 {
373 if (rtnl_link_ops_get(ops->kind))
374 return -EEXIST;
375
376 /* The check for setup is here because if ops
377 * does not have that filled up, it is not possible
378 * to use the ops for creating device. So do not
379 * fill up dellink as well. That disables rtnl_dellink.
380 */
381 if (ops->setup && !ops->dellink)
382 ops->dellink = unregister_netdevice_queue;
383
384 list_add_tail(&ops->list, &link_ops);
385 return 0;
386 }
387 EXPORT_SYMBOL_GPL(__rtnl_link_register);
388
389 /**
390 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
391 * @ops: struct rtnl_link_ops * to register
392 *
393 * Returns 0 on success or a negative error code.
394 */
rtnl_link_register(struct rtnl_link_ops * ops)395 int rtnl_link_register(struct rtnl_link_ops *ops)
396 {
397 int err;
398
399 /* Sanity-check max sizes to avoid stack buffer overflow. */
400 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
401 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
402 return -EINVAL;
403
404 rtnl_lock();
405 err = __rtnl_link_register(ops);
406 rtnl_unlock();
407 return err;
408 }
409 EXPORT_SYMBOL_GPL(rtnl_link_register);
410
__rtnl_kill_links(struct net * net,struct rtnl_link_ops * ops)411 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
412 {
413 struct net_device *dev;
414 LIST_HEAD(list_kill);
415
416 for_each_netdev(net, dev) {
417 if (dev->rtnl_link_ops == ops)
418 ops->dellink(dev, &list_kill);
419 }
420 unregister_netdevice_many(&list_kill);
421 }
422
423 /**
424 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
425 * @ops: struct rtnl_link_ops * to unregister
426 *
427 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
428 * integrity (hold pernet_ops_rwsem for writing to close the race
429 * with setup_net() and cleanup_net()).
430 */
__rtnl_link_unregister(struct rtnl_link_ops * ops)431 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
432 {
433 struct net *net;
434
435 for_each_net(net) {
436 __rtnl_kill_links(net, ops);
437 }
438 list_del(&ops->list);
439 }
440 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
441
442 /* Return with the rtnl_lock held when there are no network
443 * devices unregistering in any network namespace.
444 */
rtnl_lock_unregistering_all(void)445 static void rtnl_lock_unregistering_all(void)
446 {
447 struct net *net;
448 bool unregistering;
449 DEFINE_WAIT_FUNC(wait, woken_wake_function);
450
451 add_wait_queue(&netdev_unregistering_wq, &wait);
452 for (;;) {
453 unregistering = false;
454 rtnl_lock();
455 /* We held write locked pernet_ops_rwsem, and parallel
456 * setup_net() and cleanup_net() are not possible.
457 */
458 for_each_net(net) {
459 if (net->dev_unreg_count > 0) {
460 unregistering = true;
461 break;
462 }
463 }
464 if (!unregistering)
465 break;
466 __rtnl_unlock();
467
468 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
469 }
470 remove_wait_queue(&netdev_unregistering_wq, &wait);
471 }
472
473 /**
474 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
475 * @ops: struct rtnl_link_ops * to unregister
476 */
rtnl_link_unregister(struct rtnl_link_ops * ops)477 void rtnl_link_unregister(struct rtnl_link_ops *ops)
478 {
479 /* Close the race with setup_net() and cleanup_net() */
480 down_write(&pernet_ops_rwsem);
481 rtnl_lock_unregistering_all();
482 __rtnl_link_unregister(ops);
483 rtnl_unlock();
484 up_write(&pernet_ops_rwsem);
485 }
486 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
487
rtnl_link_get_slave_info_data_size(const struct net_device * dev)488 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
489 {
490 struct net_device *master_dev;
491 const struct rtnl_link_ops *ops;
492 size_t size = 0;
493
494 rcu_read_lock();
495
496 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
497 if (!master_dev)
498 goto out;
499
500 ops = master_dev->rtnl_link_ops;
501 if (!ops || !ops->get_slave_size)
502 goto out;
503 /* IFLA_INFO_SLAVE_DATA + nested data */
504 size = nla_total_size(sizeof(struct nlattr)) +
505 ops->get_slave_size(master_dev, dev);
506
507 out:
508 rcu_read_unlock();
509 return size;
510 }
511
rtnl_link_get_size(const struct net_device * dev)512 static size_t rtnl_link_get_size(const struct net_device *dev)
513 {
514 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
515 size_t size;
516
517 if (!ops)
518 return 0;
519
520 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
521 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
522
523 if (ops->get_size)
524 /* IFLA_INFO_DATA + nested data */
525 size += nla_total_size(sizeof(struct nlattr)) +
526 ops->get_size(dev);
527
528 if (ops->get_xstats_size)
529 /* IFLA_INFO_XSTATS */
530 size += nla_total_size(ops->get_xstats_size(dev));
531
532 size += rtnl_link_get_slave_info_data_size(dev);
533
534 return size;
535 }
536
537 static LIST_HEAD(rtnl_af_ops);
538
rtnl_af_lookup(const int family)539 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
540 {
541 const struct rtnl_af_ops *ops;
542
543 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
544 if (ops->family == family)
545 return ops;
546 }
547
548 return NULL;
549 }
550
551 /**
552 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
553 * @ops: struct rtnl_af_ops * to register
554 *
555 * Returns 0 on success or a negative error code.
556 */
rtnl_af_register(struct rtnl_af_ops * ops)557 void rtnl_af_register(struct rtnl_af_ops *ops)
558 {
559 rtnl_lock();
560 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
561 rtnl_unlock();
562 }
563 EXPORT_SYMBOL_GPL(rtnl_af_register);
564
565 /**
566 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
567 * @ops: struct rtnl_af_ops * to unregister
568 */
rtnl_af_unregister(struct rtnl_af_ops * ops)569 void rtnl_af_unregister(struct rtnl_af_ops *ops)
570 {
571 rtnl_lock();
572 list_del_rcu(&ops->list);
573 rtnl_unlock();
574
575 synchronize_rcu();
576 }
577 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
578
rtnl_link_get_af_size(const struct net_device * dev,u32 ext_filter_mask)579 static size_t rtnl_link_get_af_size(const struct net_device *dev,
580 u32 ext_filter_mask)
581 {
582 struct rtnl_af_ops *af_ops;
583 size_t size;
584
585 /* IFLA_AF_SPEC */
586 size = nla_total_size(sizeof(struct nlattr));
587
588 rcu_read_lock();
589 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
590 if (af_ops->get_link_af_size) {
591 /* AF_* + nested data */
592 size += nla_total_size(sizeof(struct nlattr)) +
593 af_ops->get_link_af_size(dev, ext_filter_mask);
594 }
595 }
596 rcu_read_unlock();
597
598 return size;
599 }
600
rtnl_have_link_slave_info(const struct net_device * dev)601 static bool rtnl_have_link_slave_info(const struct net_device *dev)
602 {
603 struct net_device *master_dev;
604 bool ret = false;
605
606 rcu_read_lock();
607
608 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
609 if (master_dev && master_dev->rtnl_link_ops)
610 ret = true;
611 rcu_read_unlock();
612 return ret;
613 }
614
rtnl_link_slave_info_fill(struct sk_buff * skb,const struct net_device * dev)615 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
616 const struct net_device *dev)
617 {
618 struct net_device *master_dev;
619 const struct rtnl_link_ops *ops;
620 struct nlattr *slave_data;
621 int err;
622
623 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
624 if (!master_dev)
625 return 0;
626 ops = master_dev->rtnl_link_ops;
627 if (!ops)
628 return 0;
629 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
630 return -EMSGSIZE;
631 if (ops->fill_slave_info) {
632 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
633 if (!slave_data)
634 return -EMSGSIZE;
635 err = ops->fill_slave_info(skb, master_dev, dev);
636 if (err < 0)
637 goto err_cancel_slave_data;
638 nla_nest_end(skb, slave_data);
639 }
640 return 0;
641
642 err_cancel_slave_data:
643 nla_nest_cancel(skb, slave_data);
644 return err;
645 }
646
rtnl_link_info_fill(struct sk_buff * skb,const struct net_device * dev)647 static int rtnl_link_info_fill(struct sk_buff *skb,
648 const struct net_device *dev)
649 {
650 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
651 struct nlattr *data;
652 int err;
653
654 if (!ops)
655 return 0;
656 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
657 return -EMSGSIZE;
658 if (ops->fill_xstats) {
659 err = ops->fill_xstats(skb, dev);
660 if (err < 0)
661 return err;
662 }
663 if (ops->fill_info) {
664 data = nla_nest_start(skb, IFLA_INFO_DATA);
665 if (data == NULL)
666 return -EMSGSIZE;
667 err = ops->fill_info(skb, dev);
668 if (err < 0)
669 goto err_cancel_data;
670 nla_nest_end(skb, data);
671 }
672 return 0;
673
674 err_cancel_data:
675 nla_nest_cancel(skb, data);
676 return err;
677 }
678
rtnl_link_fill(struct sk_buff * skb,const struct net_device * dev)679 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
680 {
681 struct nlattr *linkinfo;
682 int err = -EMSGSIZE;
683
684 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
685 if (linkinfo == NULL)
686 goto out;
687
688 err = rtnl_link_info_fill(skb, dev);
689 if (err < 0)
690 goto err_cancel_link;
691
692 err = rtnl_link_slave_info_fill(skb, dev);
693 if (err < 0)
694 goto err_cancel_link;
695
696 nla_nest_end(skb, linkinfo);
697 return 0;
698
699 err_cancel_link:
700 nla_nest_cancel(skb, linkinfo);
701 out:
702 return err;
703 }
704
rtnetlink_send(struct sk_buff * skb,struct net * net,u32 pid,unsigned int group,int echo)705 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
706 {
707 struct sock *rtnl = net->rtnl;
708 int err = 0;
709
710 NETLINK_CB(skb).dst_group = group;
711 if (echo)
712 refcount_inc(&skb->users);
713 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
714 if (echo)
715 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
716 return err;
717 }
718
rtnl_unicast(struct sk_buff * skb,struct net * net,u32 pid)719 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
720 {
721 struct sock *rtnl = net->rtnl;
722
723 return nlmsg_unicast(rtnl, skb, pid);
724 }
725 EXPORT_SYMBOL(rtnl_unicast);
726
rtnl_notify(struct sk_buff * skb,struct net * net,u32 pid,u32 group,struct nlmsghdr * nlh,gfp_t flags)727 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
728 struct nlmsghdr *nlh, gfp_t flags)
729 {
730 struct sock *rtnl = net->rtnl;
731 int report = 0;
732
733 if (nlh)
734 report = nlmsg_report(nlh);
735
736 nlmsg_notify(rtnl, skb, pid, group, report, flags);
737 }
738 EXPORT_SYMBOL(rtnl_notify);
739
rtnl_set_sk_err(struct net * net,u32 group,int error)740 void rtnl_set_sk_err(struct net *net, u32 group, int error)
741 {
742 struct sock *rtnl = net->rtnl;
743
744 netlink_set_err(rtnl, 0, group, error);
745 }
746 EXPORT_SYMBOL(rtnl_set_sk_err);
747
rtnetlink_put_metrics(struct sk_buff * skb,u32 * metrics)748 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
749 {
750 struct nlattr *mx;
751 int i, valid = 0;
752
753 mx = nla_nest_start(skb, RTA_METRICS);
754 if (mx == NULL)
755 return -ENOBUFS;
756
757 for (i = 0; i < RTAX_MAX; i++) {
758 if (metrics[i]) {
759 if (i == RTAX_CC_ALGO - 1) {
760 char tmp[TCP_CA_NAME_MAX], *name;
761
762 name = tcp_ca_get_name_by_key(metrics[i], tmp);
763 if (!name)
764 continue;
765 if (nla_put_string(skb, i + 1, name))
766 goto nla_put_failure;
767 } else if (i == RTAX_FEATURES - 1) {
768 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
769
770 if (!user_features)
771 continue;
772 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
773 if (nla_put_u32(skb, i + 1, user_features))
774 goto nla_put_failure;
775 } else {
776 if (nla_put_u32(skb, i + 1, metrics[i]))
777 goto nla_put_failure;
778 }
779 valid++;
780 }
781 }
782
783 if (!valid) {
784 nla_nest_cancel(skb, mx);
785 return 0;
786 }
787
788 return nla_nest_end(skb, mx);
789
790 nla_put_failure:
791 nla_nest_cancel(skb, mx);
792 return -EMSGSIZE;
793 }
794 EXPORT_SYMBOL(rtnetlink_put_metrics);
795
rtnl_put_cacheinfo(struct sk_buff * skb,struct dst_entry * dst,u32 id,long expires,u32 error)796 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
797 long expires, u32 error)
798 {
799 struct rta_cacheinfo ci = {
800 .rta_error = error,
801 .rta_id = id,
802 };
803
804 if (dst) {
805 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
806 ci.rta_used = dst->__use;
807 ci.rta_clntref = atomic_read(&dst->__refcnt);
808 }
809 if (expires) {
810 unsigned long clock;
811
812 clock = jiffies_to_clock_t(abs(expires));
813 clock = min_t(unsigned long, clock, INT_MAX);
814 ci.rta_expires = (expires > 0) ? clock : -clock;
815 }
816 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
817 }
818 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
819
set_operstate(struct net_device * dev,unsigned char transition)820 static void set_operstate(struct net_device *dev, unsigned char transition)
821 {
822 unsigned char operstate = dev->operstate;
823
824 switch (transition) {
825 case IF_OPER_UP:
826 if ((operstate == IF_OPER_DORMANT ||
827 operstate == IF_OPER_UNKNOWN) &&
828 !netif_dormant(dev))
829 operstate = IF_OPER_UP;
830 break;
831
832 case IF_OPER_DORMANT:
833 if (operstate == IF_OPER_UP ||
834 operstate == IF_OPER_UNKNOWN)
835 operstate = IF_OPER_DORMANT;
836 break;
837 }
838
839 if (dev->operstate != operstate) {
840 write_lock_bh(&dev_base_lock);
841 dev->operstate = operstate;
842 write_unlock_bh(&dev_base_lock);
843 netdev_state_change(dev);
844 }
845 }
846
rtnl_dev_get_flags(const struct net_device * dev)847 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
848 {
849 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
850 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
851 }
852
rtnl_dev_combine_flags(const struct net_device * dev,const struct ifinfomsg * ifm)853 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
854 const struct ifinfomsg *ifm)
855 {
856 unsigned int flags = ifm->ifi_flags;
857
858 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
859 if (ifm->ifi_change)
860 flags = (flags & ifm->ifi_change) |
861 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
862
863 return flags;
864 }
865
copy_rtnl_link_stats(struct rtnl_link_stats * a,const struct rtnl_link_stats64 * b)866 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
867 const struct rtnl_link_stats64 *b)
868 {
869 a->rx_packets = b->rx_packets;
870 a->tx_packets = b->tx_packets;
871 a->rx_bytes = b->rx_bytes;
872 a->tx_bytes = b->tx_bytes;
873 a->rx_errors = b->rx_errors;
874 a->tx_errors = b->tx_errors;
875 a->rx_dropped = b->rx_dropped;
876 a->tx_dropped = b->tx_dropped;
877
878 a->multicast = b->multicast;
879 a->collisions = b->collisions;
880
881 a->rx_length_errors = b->rx_length_errors;
882 a->rx_over_errors = b->rx_over_errors;
883 a->rx_crc_errors = b->rx_crc_errors;
884 a->rx_frame_errors = b->rx_frame_errors;
885 a->rx_fifo_errors = b->rx_fifo_errors;
886 a->rx_missed_errors = b->rx_missed_errors;
887
888 a->tx_aborted_errors = b->tx_aborted_errors;
889 a->tx_carrier_errors = b->tx_carrier_errors;
890 a->tx_fifo_errors = b->tx_fifo_errors;
891 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
892 a->tx_window_errors = b->tx_window_errors;
893
894 a->rx_compressed = b->rx_compressed;
895 a->tx_compressed = b->tx_compressed;
896
897 a->rx_nohandler = b->rx_nohandler;
898 }
899
900 /* All VF info */
rtnl_vfinfo_size(const struct net_device * dev,u32 ext_filter_mask)901 static inline int rtnl_vfinfo_size(const struct net_device *dev,
902 u32 ext_filter_mask)
903 {
904 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
905 int num_vfs = dev_num_vf(dev->dev.parent);
906 size_t size = nla_total_size(0);
907 size += num_vfs *
908 (nla_total_size(0) +
909 nla_total_size(sizeof(struct ifla_vf_mac)) +
910 nla_total_size(sizeof(struct ifla_vf_vlan)) +
911 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
912 nla_total_size(MAX_VLAN_LIST_LEN *
913 sizeof(struct ifla_vf_vlan_info)) +
914 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
915 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
916 nla_total_size(sizeof(struct ifla_vf_rate)) +
917 nla_total_size(sizeof(struct ifla_vf_link_state)) +
918 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
919 nla_total_size(0) + /* nest IFLA_VF_STATS */
920 /* IFLA_VF_STATS_RX_PACKETS */
921 nla_total_size_64bit(sizeof(__u64)) +
922 /* IFLA_VF_STATS_TX_PACKETS */
923 nla_total_size_64bit(sizeof(__u64)) +
924 /* IFLA_VF_STATS_RX_BYTES */
925 nla_total_size_64bit(sizeof(__u64)) +
926 /* IFLA_VF_STATS_TX_BYTES */
927 nla_total_size_64bit(sizeof(__u64)) +
928 /* IFLA_VF_STATS_BROADCAST */
929 nla_total_size_64bit(sizeof(__u64)) +
930 /* IFLA_VF_STATS_MULTICAST */
931 nla_total_size_64bit(sizeof(__u64)) +
932 /* IFLA_VF_STATS_RX_DROPPED */
933 nla_total_size_64bit(sizeof(__u64)) +
934 /* IFLA_VF_STATS_TX_DROPPED */
935 nla_total_size_64bit(sizeof(__u64)) +
936 nla_total_size(sizeof(struct ifla_vf_trust)));
937 return size;
938 } else
939 return 0;
940 }
941
rtnl_port_size(const struct net_device * dev,u32 ext_filter_mask)942 static size_t rtnl_port_size(const struct net_device *dev,
943 u32 ext_filter_mask)
944 {
945 size_t port_size = nla_total_size(4) /* PORT_VF */
946 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
947 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
948 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
949 + nla_total_size(1) /* PROT_VDP_REQUEST */
950 + nla_total_size(2); /* PORT_VDP_RESPONSE */
951 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
952 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
953 + port_size;
954 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
955 + port_size;
956
957 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
958 !(ext_filter_mask & RTEXT_FILTER_VF))
959 return 0;
960 if (dev_num_vf(dev->dev.parent))
961 return port_self_size + vf_ports_size +
962 vf_port_size * dev_num_vf(dev->dev.parent);
963 else
964 return port_self_size;
965 }
966
rtnl_xdp_size(void)967 static size_t rtnl_xdp_size(void)
968 {
969 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
970 nla_total_size(1) + /* XDP_ATTACHED */
971 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
972 nla_total_size(4); /* XDP_<mode>_PROG_ID */
973
974 return xdp_size;
975 }
976
if_nlmsg_size(const struct net_device * dev,u32 ext_filter_mask)977 static noinline size_t if_nlmsg_size(const struct net_device *dev,
978 u32 ext_filter_mask)
979 {
980 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
981 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
982 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
983 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
984 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
985 + nla_total_size(sizeof(struct rtnl_link_stats))
986 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
987 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
988 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
989 + nla_total_size(4) /* IFLA_TXQLEN */
990 + nla_total_size(4) /* IFLA_WEIGHT */
991 + nla_total_size(4) /* IFLA_MTU */
992 + nla_total_size(4) /* IFLA_LINK */
993 + nla_total_size(4) /* IFLA_MASTER */
994 + nla_total_size(1) /* IFLA_CARRIER */
995 + nla_total_size(4) /* IFLA_PROMISCUITY */
996 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
997 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
998 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
999 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1000 + nla_total_size(1) /* IFLA_OPERSTATE */
1001 + nla_total_size(1) /* IFLA_LINKMODE */
1002 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1003 + nla_total_size(4) /* IFLA_LINK_NETNSID */
1004 + nla_total_size(4) /* IFLA_GROUP */
1005 + nla_total_size(ext_filter_mask
1006 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1007 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1008 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1009 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1010 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1011 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1012 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1013 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1014 + rtnl_xdp_size() /* IFLA_XDP */
1015 + nla_total_size(4) /* IFLA_EVENT */
1016 + nla_total_size(4) /* IFLA_NEW_NETNSID */
1017 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1018 + nla_total_size(1) /* IFLA_PROTO_DOWN */
1019 + nla_total_size(4) /* IFLA_IF_NETNSID */
1020 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1021 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
1022 + nla_total_size(4) /* IFLA_MIN_MTU */
1023 + nla_total_size(4) /* IFLA_MAX_MTU */
1024 + 0;
1025 }
1026
rtnl_vf_ports_fill(struct sk_buff * skb,struct net_device * dev)1027 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1028 {
1029 struct nlattr *vf_ports;
1030 struct nlattr *vf_port;
1031 int vf;
1032 int err;
1033
1034 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
1035 if (!vf_ports)
1036 return -EMSGSIZE;
1037
1038 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1039 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
1040 if (!vf_port)
1041 goto nla_put_failure;
1042 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1043 goto nla_put_failure;
1044 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1045 if (err == -EMSGSIZE)
1046 goto nla_put_failure;
1047 if (err) {
1048 nla_nest_cancel(skb, vf_port);
1049 continue;
1050 }
1051 nla_nest_end(skb, vf_port);
1052 }
1053
1054 nla_nest_end(skb, vf_ports);
1055
1056 return 0;
1057
1058 nla_put_failure:
1059 nla_nest_cancel(skb, vf_ports);
1060 return -EMSGSIZE;
1061 }
1062
rtnl_port_self_fill(struct sk_buff * skb,struct net_device * dev)1063 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1064 {
1065 struct nlattr *port_self;
1066 int err;
1067
1068 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
1069 if (!port_self)
1070 return -EMSGSIZE;
1071
1072 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1073 if (err) {
1074 nla_nest_cancel(skb, port_self);
1075 return (err == -EMSGSIZE) ? err : 0;
1076 }
1077
1078 nla_nest_end(skb, port_self);
1079
1080 return 0;
1081 }
1082
rtnl_port_fill(struct sk_buff * skb,struct net_device * dev,u32 ext_filter_mask)1083 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1084 u32 ext_filter_mask)
1085 {
1086 int err;
1087
1088 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1089 !(ext_filter_mask & RTEXT_FILTER_VF))
1090 return 0;
1091
1092 err = rtnl_port_self_fill(skb, dev);
1093 if (err)
1094 return err;
1095
1096 if (dev_num_vf(dev->dev.parent)) {
1097 err = rtnl_vf_ports_fill(skb, dev);
1098 if (err)
1099 return err;
1100 }
1101
1102 return 0;
1103 }
1104
rtnl_phys_port_id_fill(struct sk_buff * skb,struct net_device * dev)1105 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1106 {
1107 int err;
1108 struct netdev_phys_item_id ppid;
1109
1110 err = dev_get_phys_port_id(dev, &ppid);
1111 if (err) {
1112 if (err == -EOPNOTSUPP)
1113 return 0;
1114 return err;
1115 }
1116
1117 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1118 return -EMSGSIZE;
1119
1120 return 0;
1121 }
1122
rtnl_phys_port_name_fill(struct sk_buff * skb,struct net_device * dev)1123 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1124 {
1125 char name[IFNAMSIZ];
1126 int err;
1127
1128 err = dev_get_phys_port_name(dev, name, sizeof(name));
1129 if (err) {
1130 if (err == -EOPNOTSUPP)
1131 return 0;
1132 return err;
1133 }
1134
1135 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1136 return -EMSGSIZE;
1137
1138 return 0;
1139 }
1140
rtnl_phys_switch_id_fill(struct sk_buff * skb,struct net_device * dev)1141 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1142 {
1143 int err;
1144 struct switchdev_attr attr = {
1145 .orig_dev = dev,
1146 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1147 .flags = SWITCHDEV_F_NO_RECURSE,
1148 };
1149
1150 err = switchdev_port_attr_get(dev, &attr);
1151 if (err) {
1152 if (err == -EOPNOTSUPP)
1153 return 0;
1154 return err;
1155 }
1156
1157 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1158 attr.u.ppid.id))
1159 return -EMSGSIZE;
1160
1161 return 0;
1162 }
1163
rtnl_fill_stats(struct sk_buff * skb,struct net_device * dev)1164 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1165 struct net_device *dev)
1166 {
1167 struct rtnl_link_stats64 *sp;
1168 struct nlattr *attr;
1169
1170 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1171 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1172 if (!attr)
1173 return -EMSGSIZE;
1174
1175 sp = nla_data(attr);
1176 dev_get_stats(dev, sp);
1177
1178 attr = nla_reserve(skb, IFLA_STATS,
1179 sizeof(struct rtnl_link_stats));
1180 if (!attr)
1181 return -EMSGSIZE;
1182
1183 copy_rtnl_link_stats(nla_data(attr), sp);
1184
1185 return 0;
1186 }
1187
rtnl_fill_vfinfo(struct sk_buff * skb,struct net_device * dev,int vfs_num,struct nlattr * vfinfo)1188 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1189 struct net_device *dev,
1190 int vfs_num,
1191 struct nlattr *vfinfo)
1192 {
1193 struct ifla_vf_rss_query_en vf_rss_query_en;
1194 struct nlattr *vf, *vfstats, *vfvlanlist;
1195 struct ifla_vf_link_state vf_linkstate;
1196 struct ifla_vf_vlan_info vf_vlan_info;
1197 struct ifla_vf_spoofchk vf_spoofchk;
1198 struct ifla_vf_tx_rate vf_tx_rate;
1199 struct ifla_vf_stats vf_stats;
1200 struct ifla_vf_trust vf_trust;
1201 struct ifla_vf_vlan vf_vlan;
1202 struct ifla_vf_rate vf_rate;
1203 struct ifla_vf_mac vf_mac;
1204 struct ifla_vf_info ivi;
1205
1206 memset(&ivi, 0, sizeof(ivi));
1207
1208 /* Not all SR-IOV capable drivers support the
1209 * spoofcheck and "RSS query enable" query. Preset to
1210 * -1 so the user space tool can detect that the driver
1211 * didn't report anything.
1212 */
1213 ivi.spoofchk = -1;
1214 ivi.rss_query_en = -1;
1215 ivi.trusted = -1;
1216 /* The default value for VF link state is "auto"
1217 * IFLA_VF_LINK_STATE_AUTO which equals zero
1218 */
1219 ivi.linkstate = 0;
1220 /* VLAN Protocol by default is 802.1Q */
1221 ivi.vlan_proto = htons(ETH_P_8021Q);
1222 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1223 return 0;
1224
1225 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1226
1227 vf_mac.vf =
1228 vf_vlan.vf =
1229 vf_vlan_info.vf =
1230 vf_rate.vf =
1231 vf_tx_rate.vf =
1232 vf_spoofchk.vf =
1233 vf_linkstate.vf =
1234 vf_rss_query_en.vf =
1235 vf_trust.vf = ivi.vf;
1236
1237 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1238 vf_vlan.vlan = ivi.vlan;
1239 vf_vlan.qos = ivi.qos;
1240 vf_vlan_info.vlan = ivi.vlan;
1241 vf_vlan_info.qos = ivi.qos;
1242 vf_vlan_info.vlan_proto = ivi.vlan_proto;
1243 vf_tx_rate.rate = ivi.max_tx_rate;
1244 vf_rate.min_tx_rate = ivi.min_tx_rate;
1245 vf_rate.max_tx_rate = ivi.max_tx_rate;
1246 vf_spoofchk.setting = ivi.spoofchk;
1247 vf_linkstate.link_state = ivi.linkstate;
1248 vf_rss_query_en.setting = ivi.rss_query_en;
1249 vf_trust.setting = ivi.trusted;
1250 vf = nla_nest_start(skb, IFLA_VF_INFO);
1251 if (!vf)
1252 goto nla_put_vfinfo_failure;
1253 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1254 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1255 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1256 &vf_rate) ||
1257 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1258 &vf_tx_rate) ||
1259 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1260 &vf_spoofchk) ||
1261 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1262 &vf_linkstate) ||
1263 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1264 sizeof(vf_rss_query_en),
1265 &vf_rss_query_en) ||
1266 nla_put(skb, IFLA_VF_TRUST,
1267 sizeof(vf_trust), &vf_trust))
1268 goto nla_put_vf_failure;
1269 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1270 if (!vfvlanlist)
1271 goto nla_put_vf_failure;
1272 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1273 &vf_vlan_info)) {
1274 nla_nest_cancel(skb, vfvlanlist);
1275 goto nla_put_vf_failure;
1276 }
1277 nla_nest_end(skb, vfvlanlist);
1278 memset(&vf_stats, 0, sizeof(vf_stats));
1279 if (dev->netdev_ops->ndo_get_vf_stats)
1280 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1281 &vf_stats);
1282 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1283 if (!vfstats)
1284 goto nla_put_vf_failure;
1285 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1286 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1287 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1288 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1289 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1290 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1291 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1292 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1293 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1294 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1295 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1296 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1297 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1298 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1299 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1300 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1301 nla_nest_cancel(skb, vfstats);
1302 goto nla_put_vf_failure;
1303 }
1304 nla_nest_end(skb, vfstats);
1305 nla_nest_end(skb, vf);
1306 return 0;
1307
1308 nla_put_vf_failure:
1309 nla_nest_cancel(skb, vf);
1310 nla_put_vfinfo_failure:
1311 nla_nest_cancel(skb, vfinfo);
1312 return -EMSGSIZE;
1313 }
1314
rtnl_fill_vf(struct sk_buff * skb,struct net_device * dev,u32 ext_filter_mask)1315 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1316 struct net_device *dev,
1317 u32 ext_filter_mask)
1318 {
1319 struct nlattr *vfinfo;
1320 int i, num_vfs;
1321
1322 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1323 return 0;
1324
1325 num_vfs = dev_num_vf(dev->dev.parent);
1326 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1327 return -EMSGSIZE;
1328
1329 if (!dev->netdev_ops->ndo_get_vf_config)
1330 return 0;
1331
1332 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1333 if (!vfinfo)
1334 return -EMSGSIZE;
1335
1336 for (i = 0; i < num_vfs; i++) {
1337 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1338 return -EMSGSIZE;
1339 }
1340
1341 nla_nest_end(skb, vfinfo);
1342 return 0;
1343 }
1344
rtnl_fill_link_ifmap(struct sk_buff * skb,struct net_device * dev)1345 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1346 {
1347 struct rtnl_link_ifmap map;
1348
1349 memset(&map, 0, sizeof(map));
1350 map.mem_start = dev->mem_start;
1351 map.mem_end = dev->mem_end;
1352 map.base_addr = dev->base_addr;
1353 map.irq = dev->irq;
1354 map.dma = dev->dma;
1355 map.port = dev->if_port;
1356
1357 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1358 return -EMSGSIZE;
1359
1360 return 0;
1361 }
1362
rtnl_xdp_prog_skb(struct net_device * dev)1363 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1364 {
1365 const struct bpf_prog *generic_xdp_prog;
1366
1367 ASSERT_RTNL();
1368
1369 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1370 if (!generic_xdp_prog)
1371 return 0;
1372 return generic_xdp_prog->aux->id;
1373 }
1374
rtnl_xdp_prog_drv(struct net_device * dev)1375 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1376 {
1377 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1378 }
1379
rtnl_xdp_prog_hw(struct net_device * dev)1380 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1381 {
1382 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1383 XDP_QUERY_PROG_HW);
1384 }
1385
rtnl_xdp_report_one(struct sk_buff * skb,struct net_device * dev,u32 * prog_id,u8 * mode,u8 tgt_mode,u32 attr,u32 (* get_prog_id)(struct net_device * dev))1386 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1387 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1388 u32 (*get_prog_id)(struct net_device *dev))
1389 {
1390 u32 curr_id;
1391 int err;
1392
1393 curr_id = get_prog_id(dev);
1394 if (!curr_id)
1395 return 0;
1396
1397 *prog_id = curr_id;
1398 err = nla_put_u32(skb, attr, curr_id);
1399 if (err)
1400 return err;
1401
1402 if (*mode != XDP_ATTACHED_NONE)
1403 *mode = XDP_ATTACHED_MULTI;
1404 else
1405 *mode = tgt_mode;
1406
1407 return 0;
1408 }
1409
rtnl_xdp_fill(struct sk_buff * skb,struct net_device * dev)1410 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1411 {
1412 struct nlattr *xdp;
1413 u32 prog_id;
1414 int err;
1415 u8 mode;
1416
1417 xdp = nla_nest_start(skb, IFLA_XDP);
1418 if (!xdp)
1419 return -EMSGSIZE;
1420
1421 prog_id = 0;
1422 mode = XDP_ATTACHED_NONE;
1423 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1424 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1425 if (err)
1426 goto err_cancel;
1427 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1428 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1429 if (err)
1430 goto err_cancel;
1431 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1432 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1433 if (err)
1434 goto err_cancel;
1435
1436 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1437 if (err)
1438 goto err_cancel;
1439
1440 if (prog_id && mode != XDP_ATTACHED_MULTI) {
1441 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1442 if (err)
1443 goto err_cancel;
1444 }
1445
1446 nla_nest_end(skb, xdp);
1447 return 0;
1448
1449 err_cancel:
1450 nla_nest_cancel(skb, xdp);
1451 return err;
1452 }
1453
rtnl_get_event(unsigned long event)1454 static u32 rtnl_get_event(unsigned long event)
1455 {
1456 u32 rtnl_event_type = IFLA_EVENT_NONE;
1457
1458 switch (event) {
1459 case NETDEV_REBOOT:
1460 rtnl_event_type = IFLA_EVENT_REBOOT;
1461 break;
1462 case NETDEV_FEAT_CHANGE:
1463 rtnl_event_type = IFLA_EVENT_FEATURES;
1464 break;
1465 case NETDEV_BONDING_FAILOVER:
1466 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1467 break;
1468 case NETDEV_NOTIFY_PEERS:
1469 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1470 break;
1471 case NETDEV_RESEND_IGMP:
1472 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1473 break;
1474 case NETDEV_CHANGEINFODATA:
1475 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1476 break;
1477 default:
1478 break;
1479 }
1480
1481 return rtnl_event_type;
1482 }
1483
put_master_ifindex(struct sk_buff * skb,struct net_device * dev)1484 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1485 {
1486 const struct net_device *upper_dev;
1487 int ret = 0;
1488
1489 rcu_read_lock();
1490
1491 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1492 if (upper_dev)
1493 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1494
1495 rcu_read_unlock();
1496 return ret;
1497 }
1498
nla_put_iflink(struct sk_buff * skb,const struct net_device * dev,bool force)1499 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1500 bool force)
1501 {
1502 int ifindex = dev_get_iflink(dev);
1503
1504 if (force || dev->ifindex != ifindex)
1505 return nla_put_u32(skb, IFLA_LINK, ifindex);
1506
1507 return 0;
1508 }
1509
nla_put_ifalias(struct sk_buff * skb,struct net_device * dev)1510 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1511 struct net_device *dev)
1512 {
1513 char buf[IFALIASZ];
1514 int ret;
1515
1516 ret = dev_get_alias(dev, buf, sizeof(buf));
1517 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1518 }
1519
rtnl_fill_link_netnsid(struct sk_buff * skb,const struct net_device * dev,struct net * src_net,gfp_t gfp)1520 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1521 const struct net_device *dev,
1522 struct net *src_net, gfp_t gfp)
1523 {
1524 bool put_iflink = false;
1525
1526 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1527 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1528
1529 if (!net_eq(dev_net(dev), link_net)) {
1530 int id = peernet2id_alloc(src_net, link_net, gfp);
1531
1532 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1533 return -EMSGSIZE;
1534
1535 put_iflink = true;
1536 }
1537 }
1538
1539 return nla_put_iflink(skb, dev, put_iflink);
1540 }
1541
rtnl_fill_link_af(struct sk_buff * skb,const struct net_device * dev,u32 ext_filter_mask)1542 static int rtnl_fill_link_af(struct sk_buff *skb,
1543 const struct net_device *dev,
1544 u32 ext_filter_mask)
1545 {
1546 const struct rtnl_af_ops *af_ops;
1547 struct nlattr *af_spec;
1548
1549 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1550 if (!af_spec)
1551 return -EMSGSIZE;
1552
1553 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1554 struct nlattr *af;
1555 int err;
1556
1557 if (!af_ops->fill_link_af)
1558 continue;
1559
1560 af = nla_nest_start(skb, af_ops->family);
1561 if (!af)
1562 return -EMSGSIZE;
1563
1564 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1565 /*
1566 * Caller may return ENODATA to indicate that there
1567 * was no data to be dumped. This is not an error, it
1568 * means we should trim the attribute header and
1569 * continue.
1570 */
1571 if (err == -ENODATA)
1572 nla_nest_cancel(skb, af);
1573 else if (err < 0)
1574 return -EMSGSIZE;
1575
1576 nla_nest_end(skb, af);
1577 }
1578
1579 nla_nest_end(skb, af_spec);
1580 return 0;
1581 }
1582
rtnl_fill_ifinfo(struct sk_buff * skb,struct net_device * dev,struct net * src_net,int type,u32 pid,u32 seq,u32 change,unsigned int flags,u32 ext_filter_mask,u32 event,int * new_nsid,int new_ifindex,int tgt_netnsid,gfp_t gfp)1583 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1584 struct net_device *dev, struct net *src_net,
1585 int type, u32 pid, u32 seq, u32 change,
1586 unsigned int flags, u32 ext_filter_mask,
1587 u32 event, int *new_nsid, int new_ifindex,
1588 int tgt_netnsid, gfp_t gfp)
1589 {
1590 struct ifinfomsg *ifm;
1591 struct nlmsghdr *nlh;
1592
1593 ASSERT_RTNL();
1594 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1595 if (nlh == NULL)
1596 return -EMSGSIZE;
1597
1598 ifm = nlmsg_data(nlh);
1599 ifm->ifi_family = AF_UNSPEC;
1600 ifm->__ifi_pad = 0;
1601 ifm->ifi_type = dev->type;
1602 ifm->ifi_index = dev->ifindex;
1603 ifm->ifi_flags = dev_get_flags(dev);
1604 ifm->ifi_change = change;
1605
1606 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid))
1607 goto nla_put_failure;
1608
1609 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1610 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1611 nla_put_u8(skb, IFLA_OPERSTATE,
1612 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1613 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1614 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1615 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1616 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1617 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1618 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1619 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1620 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1621 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1622 #ifdef CONFIG_RPS
1623 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1624 #endif
1625 put_master_ifindex(skb, dev) ||
1626 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1627 (dev->qdisc &&
1628 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1629 nla_put_ifalias(skb, dev) ||
1630 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1631 atomic_read(&dev->carrier_up_count) +
1632 atomic_read(&dev->carrier_down_count)) ||
1633 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1634 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1635 atomic_read(&dev->carrier_up_count)) ||
1636 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1637 atomic_read(&dev->carrier_down_count)))
1638 goto nla_put_failure;
1639
1640 if (event != IFLA_EVENT_NONE) {
1641 if (nla_put_u32(skb, IFLA_EVENT, event))
1642 goto nla_put_failure;
1643 }
1644
1645 if (rtnl_fill_link_ifmap(skb, dev))
1646 goto nla_put_failure;
1647
1648 if (dev->addr_len) {
1649 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1650 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1651 goto nla_put_failure;
1652 }
1653
1654 if (rtnl_phys_port_id_fill(skb, dev))
1655 goto nla_put_failure;
1656
1657 if (rtnl_phys_port_name_fill(skb, dev))
1658 goto nla_put_failure;
1659
1660 if (rtnl_phys_switch_id_fill(skb, dev))
1661 goto nla_put_failure;
1662
1663 if (rtnl_fill_stats(skb, dev))
1664 goto nla_put_failure;
1665
1666 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1667 goto nla_put_failure;
1668
1669 if (rtnl_port_fill(skb, dev, ext_filter_mask))
1670 goto nla_put_failure;
1671
1672 if (rtnl_xdp_fill(skb, dev))
1673 goto nla_put_failure;
1674
1675 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1676 if (rtnl_link_fill(skb, dev) < 0)
1677 goto nla_put_failure;
1678 }
1679
1680 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1681 goto nla_put_failure;
1682
1683 if (new_nsid &&
1684 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1685 goto nla_put_failure;
1686 if (new_ifindex &&
1687 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1688 goto nla_put_failure;
1689
1690
1691 rcu_read_lock();
1692 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1693 goto nla_put_failure_rcu;
1694 rcu_read_unlock();
1695
1696 nlmsg_end(skb, nlh);
1697 return 0;
1698
1699 nla_put_failure_rcu:
1700 rcu_read_unlock();
1701 nla_put_failure:
1702 nlmsg_cancel(skb, nlh);
1703 return -EMSGSIZE;
1704 }
1705
1706 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1707 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1708 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1709 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1710 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1711 [IFLA_MTU] = { .type = NLA_U32 },
1712 [IFLA_LINK] = { .type = NLA_U32 },
1713 [IFLA_MASTER] = { .type = NLA_U32 },
1714 [IFLA_CARRIER] = { .type = NLA_U8 },
1715 [IFLA_TXQLEN] = { .type = NLA_U32 },
1716 [IFLA_WEIGHT] = { .type = NLA_U32 },
1717 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1718 [IFLA_LINKMODE] = { .type = NLA_U8 },
1719 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1720 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1721 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1722 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1723 * allow 0-length string (needed to remove an alias).
1724 */
1725 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1726 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1727 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1728 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1729 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1730 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1731 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1732 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1733 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1734 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1735 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
1736 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1737 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
1738 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1739 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
1740 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1741 [IFLA_XDP] = { .type = NLA_NESTED },
1742 [IFLA_EVENT] = { .type = NLA_U32 },
1743 [IFLA_GROUP] = { .type = NLA_U32 },
1744 [IFLA_IF_NETNSID] = { .type = NLA_S32 },
1745 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1746 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1747 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1748 [IFLA_MAX_MTU] = { .type = NLA_U32 },
1749 };
1750
1751 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1752 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1753 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1754 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1755 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1756 };
1757
1758 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1759 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1760 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1761 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
1762 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1763 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1764 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1765 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1766 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1767 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1768 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
1769 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1770 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1771 };
1772
1773 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1774 [IFLA_PORT_VF] = { .type = NLA_U32 },
1775 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1776 .len = PORT_PROFILE_MAX },
1777 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1778 .len = PORT_UUID_MAX },
1779 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1780 .len = PORT_UUID_MAX },
1781 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1782 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1783
1784 /* Unused, but we need to keep it here since user space could
1785 * fill it. It's also broken with regard to NLA_BINARY use in
1786 * combination with structs.
1787 */
1788 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1789 .len = sizeof(struct ifla_port_vsi) },
1790 };
1791
1792 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1793 [IFLA_XDP_FD] = { .type = NLA_S32 },
1794 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1795 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
1796 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
1797 };
1798
linkinfo_to_kind_ops(const struct nlattr * nla)1799 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1800 {
1801 const struct rtnl_link_ops *ops = NULL;
1802 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1803
1804 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1805 ifla_info_policy, NULL) < 0)
1806 return NULL;
1807
1808 if (linfo[IFLA_INFO_KIND]) {
1809 char kind[MODULE_NAME_LEN];
1810
1811 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1812 ops = rtnl_link_ops_get(kind);
1813 }
1814
1815 return ops;
1816 }
1817
link_master_filtered(struct net_device * dev,int master_idx)1818 static bool link_master_filtered(struct net_device *dev, int master_idx)
1819 {
1820 struct net_device *master;
1821
1822 if (!master_idx)
1823 return false;
1824
1825 master = netdev_master_upper_dev_get(dev);
1826 if (!master || master->ifindex != master_idx)
1827 return true;
1828
1829 return false;
1830 }
1831
link_kind_filtered(const struct net_device * dev,const struct rtnl_link_ops * kind_ops)1832 static bool link_kind_filtered(const struct net_device *dev,
1833 const struct rtnl_link_ops *kind_ops)
1834 {
1835 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1836 return true;
1837
1838 return false;
1839 }
1840
link_dump_filtered(struct net_device * dev,int master_idx,const struct rtnl_link_ops * kind_ops)1841 static bool link_dump_filtered(struct net_device *dev,
1842 int master_idx,
1843 const struct rtnl_link_ops *kind_ops)
1844 {
1845 if (link_master_filtered(dev, master_idx) ||
1846 link_kind_filtered(dev, kind_ops))
1847 return true;
1848
1849 return false;
1850 }
1851
get_target_net(struct sock * sk,int netnsid)1852 static struct net *get_target_net(struct sock *sk, int netnsid)
1853 {
1854 struct net *net;
1855
1856 net = get_net_ns_by_id(sock_net(sk), netnsid);
1857 if (!net)
1858 return ERR_PTR(-EINVAL);
1859
1860 /* For now, the caller is required to have CAP_NET_ADMIN in
1861 * the user namespace owning the target net ns.
1862 */
1863 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
1864 put_net(net);
1865 return ERR_PTR(-EACCES);
1866 }
1867 return net;
1868 }
1869
rtnl_dump_ifinfo(struct sk_buff * skb,struct netlink_callback * cb)1870 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1871 {
1872 struct net *net = sock_net(skb->sk);
1873 struct net *tgt_net = net;
1874 int h, s_h;
1875 int idx = 0, s_idx;
1876 struct net_device *dev;
1877 struct hlist_head *head;
1878 struct nlattr *tb[IFLA_MAX+1];
1879 u32 ext_filter_mask = 0;
1880 const struct rtnl_link_ops *kind_ops = NULL;
1881 unsigned int flags = NLM_F_MULTI;
1882 int master_idx = 0;
1883 int netnsid = -1;
1884 int err;
1885 int hdrlen;
1886
1887 s_h = cb->args[0];
1888 s_idx = cb->args[1];
1889
1890 /* A hack to preserve kernel<->userspace interface.
1891 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1892 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1893 * what iproute2 < v3.9.0 used.
1894 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1895 * attribute, its netlink message is shorter than struct ifinfomsg.
1896 */
1897 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1898 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1899
1900 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1901 ifla_policy, NULL) >= 0) {
1902 if (tb[IFLA_IF_NETNSID]) {
1903 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
1904 tgt_net = get_target_net(skb->sk, netnsid);
1905 if (IS_ERR(tgt_net))
1906 return PTR_ERR(tgt_net);
1907 }
1908
1909 if (tb[IFLA_EXT_MASK])
1910 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1911
1912 if (tb[IFLA_MASTER])
1913 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1914
1915 if (tb[IFLA_LINKINFO])
1916 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1917
1918 if (master_idx || kind_ops)
1919 flags |= NLM_F_DUMP_FILTERED;
1920 }
1921
1922 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1923 idx = 0;
1924 head = &tgt_net->dev_index_head[h];
1925 hlist_for_each_entry(dev, head, index_hlist) {
1926 if (link_dump_filtered(dev, master_idx, kind_ops))
1927 goto cont;
1928 if (idx < s_idx)
1929 goto cont;
1930 err = rtnl_fill_ifinfo(skb, dev, net,
1931 RTM_NEWLINK,
1932 NETLINK_CB(cb->skb).portid,
1933 cb->nlh->nlmsg_seq, 0,
1934 flags,
1935 ext_filter_mask, 0, NULL, 0,
1936 netnsid, GFP_KERNEL);
1937
1938 if (err < 0) {
1939 if (likely(skb->len))
1940 goto out;
1941
1942 goto out_err;
1943 }
1944 cont:
1945 idx++;
1946 }
1947 }
1948 out:
1949 err = skb->len;
1950 out_err:
1951 cb->args[1] = idx;
1952 cb->args[0] = h;
1953 cb->seq = net->dev_base_seq;
1954 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1955 if (netnsid >= 0)
1956 put_net(tgt_net);
1957
1958 return err;
1959 }
1960
rtnl_nla_parse_ifla(struct nlattr ** tb,const struct nlattr * head,int len,struct netlink_ext_ack * exterr)1961 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1962 struct netlink_ext_ack *exterr)
1963 {
1964 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
1965 }
1966 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1967
rtnl_link_get_net(struct net * src_net,struct nlattr * tb[])1968 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1969 {
1970 struct net *net;
1971 /* Examine the link attributes and figure out which
1972 * network namespace we are talking about.
1973 */
1974 if (tb[IFLA_NET_NS_PID])
1975 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1976 else if (tb[IFLA_NET_NS_FD])
1977 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1978 else
1979 net = get_net(src_net);
1980 return net;
1981 }
1982 EXPORT_SYMBOL(rtnl_link_get_net);
1983
1984 /* Figure out which network namespace we are talking about by
1985 * examining the link attributes in the following order:
1986 *
1987 * 1. IFLA_NET_NS_PID
1988 * 2. IFLA_NET_NS_FD
1989 * 3. IFLA_IF_NETNSID
1990 */
rtnl_link_get_net_by_nlattr(struct net * src_net,struct nlattr * tb[])1991 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
1992 struct nlattr *tb[])
1993 {
1994 struct net *net;
1995
1996 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
1997 return rtnl_link_get_net(src_net, tb);
1998
1999 if (!tb[IFLA_IF_NETNSID])
2000 return get_net(src_net);
2001
2002 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_IF_NETNSID]));
2003 if (!net)
2004 return ERR_PTR(-EINVAL);
2005
2006 return net;
2007 }
2008
rtnl_link_get_net_capable(const struct sk_buff * skb,struct net * src_net,struct nlattr * tb[],int cap)2009 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2010 struct net *src_net,
2011 struct nlattr *tb[], int cap)
2012 {
2013 struct net *net;
2014
2015 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2016 if (IS_ERR(net))
2017 return net;
2018
2019 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2020 put_net(net);
2021 return ERR_PTR(-EPERM);
2022 }
2023
2024 return net;
2025 }
2026
2027 /* Verify that rtnetlink requests do not pass additional properties
2028 * potentially referring to different network namespaces.
2029 */
rtnl_ensure_unique_netns(struct nlattr * tb[],struct netlink_ext_ack * extack,bool netns_id_only)2030 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2031 struct netlink_ext_ack *extack,
2032 bool netns_id_only)
2033 {
2034
2035 if (netns_id_only) {
2036 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2037 return 0;
2038
2039 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2040 return -EOPNOTSUPP;
2041 }
2042
2043 if (tb[IFLA_IF_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2044 goto invalid_attr;
2045
2046 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_FD]))
2047 goto invalid_attr;
2048
2049 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_PID]))
2050 goto invalid_attr;
2051
2052 return 0;
2053
2054 invalid_attr:
2055 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2056 return -EINVAL;
2057 }
2058
validate_linkmsg(struct net_device * dev,struct nlattr * tb[])2059 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2060 {
2061 if (dev) {
2062 if (tb[IFLA_ADDRESS] &&
2063 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2064 return -EINVAL;
2065
2066 if (tb[IFLA_BROADCAST] &&
2067 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2068 return -EINVAL;
2069 }
2070
2071 if (tb[IFLA_AF_SPEC]) {
2072 struct nlattr *af;
2073 int rem, err;
2074
2075 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2076 const struct rtnl_af_ops *af_ops;
2077
2078 rcu_read_lock();
2079 af_ops = rtnl_af_lookup(nla_type(af));
2080 if (!af_ops) {
2081 rcu_read_unlock();
2082 return -EAFNOSUPPORT;
2083 }
2084
2085 if (!af_ops->set_link_af) {
2086 rcu_read_unlock();
2087 return -EOPNOTSUPP;
2088 }
2089
2090 if (af_ops->validate_link_af) {
2091 err = af_ops->validate_link_af(dev, af);
2092 if (err < 0) {
2093 rcu_read_unlock();
2094 return err;
2095 }
2096 }
2097
2098 rcu_read_unlock();
2099 }
2100 }
2101
2102 return 0;
2103 }
2104
handle_infiniband_guid(struct net_device * dev,struct ifla_vf_guid * ivt,int guid_type)2105 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2106 int guid_type)
2107 {
2108 const struct net_device_ops *ops = dev->netdev_ops;
2109
2110 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2111 }
2112
handle_vf_guid(struct net_device * dev,struct ifla_vf_guid * ivt,int guid_type)2113 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2114 {
2115 if (dev->type != ARPHRD_INFINIBAND)
2116 return -EOPNOTSUPP;
2117
2118 return handle_infiniband_guid(dev, ivt, guid_type);
2119 }
2120
do_setvfinfo(struct net_device * dev,struct nlattr ** tb)2121 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2122 {
2123 const struct net_device_ops *ops = dev->netdev_ops;
2124 int err = -EINVAL;
2125
2126 if (tb[IFLA_VF_MAC]) {
2127 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2128
2129 if (ivm->vf >= INT_MAX)
2130 return -EINVAL;
2131 err = -EOPNOTSUPP;
2132 if (ops->ndo_set_vf_mac)
2133 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2134 ivm->mac);
2135 if (err < 0)
2136 return err;
2137 }
2138
2139 if (tb[IFLA_VF_VLAN]) {
2140 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2141
2142 if (ivv->vf >= INT_MAX)
2143 return -EINVAL;
2144 err = -EOPNOTSUPP;
2145 if (ops->ndo_set_vf_vlan)
2146 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2147 ivv->qos,
2148 htons(ETH_P_8021Q));
2149 if (err < 0)
2150 return err;
2151 }
2152
2153 if (tb[IFLA_VF_VLAN_LIST]) {
2154 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2155 struct nlattr *attr;
2156 int rem, len = 0;
2157
2158 err = -EOPNOTSUPP;
2159 if (!ops->ndo_set_vf_vlan)
2160 return err;
2161
2162 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2163 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2164 nla_len(attr) < NLA_HDRLEN) {
2165 return -EINVAL;
2166 }
2167 if (len >= MAX_VLAN_LIST_LEN)
2168 return -EOPNOTSUPP;
2169 ivvl[len] = nla_data(attr);
2170
2171 len++;
2172 }
2173 if (len == 0)
2174 return -EINVAL;
2175
2176 if (ivvl[0]->vf >= INT_MAX)
2177 return -EINVAL;
2178 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2179 ivvl[0]->qos, ivvl[0]->vlan_proto);
2180 if (err < 0)
2181 return err;
2182 }
2183
2184 if (tb[IFLA_VF_TX_RATE]) {
2185 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2186 struct ifla_vf_info ivf;
2187
2188 if (ivt->vf >= INT_MAX)
2189 return -EINVAL;
2190 err = -EOPNOTSUPP;
2191 if (ops->ndo_get_vf_config)
2192 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2193 if (err < 0)
2194 return err;
2195
2196 err = -EOPNOTSUPP;
2197 if (ops->ndo_set_vf_rate)
2198 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2199 ivf.min_tx_rate,
2200 ivt->rate);
2201 if (err < 0)
2202 return err;
2203 }
2204
2205 if (tb[IFLA_VF_RATE]) {
2206 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2207
2208 if (ivt->vf >= INT_MAX)
2209 return -EINVAL;
2210 err = -EOPNOTSUPP;
2211 if (ops->ndo_set_vf_rate)
2212 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2213 ivt->min_tx_rate,
2214 ivt->max_tx_rate);
2215 if (err < 0)
2216 return err;
2217 }
2218
2219 if (tb[IFLA_VF_SPOOFCHK]) {
2220 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2221
2222 if (ivs->vf >= INT_MAX)
2223 return -EINVAL;
2224 err = -EOPNOTSUPP;
2225 if (ops->ndo_set_vf_spoofchk)
2226 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2227 ivs->setting);
2228 if (err < 0)
2229 return err;
2230 }
2231
2232 if (tb[IFLA_VF_LINK_STATE]) {
2233 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2234
2235 if (ivl->vf >= INT_MAX)
2236 return -EINVAL;
2237 err = -EOPNOTSUPP;
2238 if (ops->ndo_set_vf_link_state)
2239 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2240 ivl->link_state);
2241 if (err < 0)
2242 return err;
2243 }
2244
2245 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2246 struct ifla_vf_rss_query_en *ivrssq_en;
2247
2248 err = -EOPNOTSUPP;
2249 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2250 if (ivrssq_en->vf >= INT_MAX)
2251 return -EINVAL;
2252 if (ops->ndo_set_vf_rss_query_en)
2253 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2254 ivrssq_en->setting);
2255 if (err < 0)
2256 return err;
2257 }
2258
2259 if (tb[IFLA_VF_TRUST]) {
2260 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2261
2262 if (ivt->vf >= INT_MAX)
2263 return -EINVAL;
2264 err = -EOPNOTSUPP;
2265 if (ops->ndo_set_vf_trust)
2266 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2267 if (err < 0)
2268 return err;
2269 }
2270
2271 if (tb[IFLA_VF_IB_NODE_GUID]) {
2272 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2273
2274 if (ivt->vf >= INT_MAX)
2275 return -EINVAL;
2276 if (!ops->ndo_set_vf_guid)
2277 return -EOPNOTSUPP;
2278 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2279 }
2280
2281 if (tb[IFLA_VF_IB_PORT_GUID]) {
2282 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2283
2284 if (ivt->vf >= INT_MAX)
2285 return -EINVAL;
2286 if (!ops->ndo_set_vf_guid)
2287 return -EOPNOTSUPP;
2288
2289 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2290 }
2291
2292 return err;
2293 }
2294
do_set_master(struct net_device * dev,int ifindex,struct netlink_ext_ack * extack)2295 static int do_set_master(struct net_device *dev, int ifindex,
2296 struct netlink_ext_ack *extack)
2297 {
2298 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2299 const struct net_device_ops *ops;
2300 int err;
2301
2302 if (upper_dev) {
2303 if (upper_dev->ifindex == ifindex)
2304 return 0;
2305 ops = upper_dev->netdev_ops;
2306 if (ops->ndo_del_slave) {
2307 err = ops->ndo_del_slave(upper_dev, dev);
2308 if (err)
2309 return err;
2310 } else {
2311 return -EOPNOTSUPP;
2312 }
2313 }
2314
2315 if (ifindex) {
2316 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2317 if (!upper_dev)
2318 return -EINVAL;
2319 ops = upper_dev->netdev_ops;
2320 if (ops->ndo_add_slave) {
2321 err = ops->ndo_add_slave(upper_dev, dev, extack);
2322 if (err)
2323 return err;
2324 } else {
2325 return -EOPNOTSUPP;
2326 }
2327 }
2328 return 0;
2329 }
2330
2331 #define DO_SETLINK_MODIFIED 0x01
2332 /* notify flag means notify + modified. */
2333 #define DO_SETLINK_NOTIFY 0x03
do_setlink(const struct sk_buff * skb,struct net_device * dev,struct ifinfomsg * ifm,struct netlink_ext_ack * extack,struct nlattr ** tb,char * ifname,int status)2334 static int do_setlink(const struct sk_buff *skb,
2335 struct net_device *dev, struct ifinfomsg *ifm,
2336 struct netlink_ext_ack *extack,
2337 struct nlattr **tb, char *ifname, int status)
2338 {
2339 const struct net_device_ops *ops = dev->netdev_ops;
2340 int err;
2341
2342 err = validate_linkmsg(dev, tb);
2343 if (err < 0)
2344 return err;
2345
2346 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_IF_NETNSID]) {
2347 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2348 tb, CAP_NET_ADMIN);
2349 if (IS_ERR(net)) {
2350 err = PTR_ERR(net);
2351 goto errout;
2352 }
2353
2354 err = dev_change_net_namespace(dev, net, ifname);
2355 put_net(net);
2356 if (err)
2357 goto errout;
2358 status |= DO_SETLINK_MODIFIED;
2359 }
2360
2361 if (tb[IFLA_MAP]) {
2362 struct rtnl_link_ifmap *u_map;
2363 struct ifmap k_map;
2364
2365 if (!ops->ndo_set_config) {
2366 err = -EOPNOTSUPP;
2367 goto errout;
2368 }
2369
2370 if (!netif_device_present(dev)) {
2371 err = -ENODEV;
2372 goto errout;
2373 }
2374
2375 u_map = nla_data(tb[IFLA_MAP]);
2376 k_map.mem_start = (unsigned long) u_map->mem_start;
2377 k_map.mem_end = (unsigned long) u_map->mem_end;
2378 k_map.base_addr = (unsigned short) u_map->base_addr;
2379 k_map.irq = (unsigned char) u_map->irq;
2380 k_map.dma = (unsigned char) u_map->dma;
2381 k_map.port = (unsigned char) u_map->port;
2382
2383 err = ops->ndo_set_config(dev, &k_map);
2384 if (err < 0)
2385 goto errout;
2386
2387 status |= DO_SETLINK_NOTIFY;
2388 }
2389
2390 if (tb[IFLA_ADDRESS]) {
2391 struct sockaddr *sa;
2392 int len;
2393
2394 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2395 sizeof(*sa));
2396 sa = kmalloc(len, GFP_KERNEL);
2397 if (!sa) {
2398 err = -ENOMEM;
2399 goto errout;
2400 }
2401 sa->sa_family = dev->type;
2402 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2403 dev->addr_len);
2404 err = dev_set_mac_address(dev, sa);
2405 kfree(sa);
2406 if (err)
2407 goto errout;
2408 status |= DO_SETLINK_MODIFIED;
2409 }
2410
2411 if (tb[IFLA_MTU]) {
2412 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2413 if (err < 0)
2414 goto errout;
2415 status |= DO_SETLINK_MODIFIED;
2416 }
2417
2418 if (tb[IFLA_GROUP]) {
2419 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2420 status |= DO_SETLINK_NOTIFY;
2421 }
2422
2423 /*
2424 * Interface selected by interface index but interface
2425 * name provided implies that a name change has been
2426 * requested.
2427 */
2428 if (ifm->ifi_index > 0 && ifname[0]) {
2429 err = dev_change_name(dev, ifname);
2430 if (err < 0)
2431 goto errout;
2432 status |= DO_SETLINK_MODIFIED;
2433 }
2434
2435 if (tb[IFLA_IFALIAS]) {
2436 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2437 nla_len(tb[IFLA_IFALIAS]));
2438 if (err < 0)
2439 goto errout;
2440 status |= DO_SETLINK_NOTIFY;
2441 }
2442
2443 if (tb[IFLA_BROADCAST]) {
2444 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2445 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2446 }
2447
2448 if (ifm->ifi_flags || ifm->ifi_change) {
2449 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2450 if (err < 0)
2451 goto errout;
2452 }
2453
2454 if (tb[IFLA_MASTER]) {
2455 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2456 if (err)
2457 goto errout;
2458 status |= DO_SETLINK_MODIFIED;
2459 }
2460
2461 if (tb[IFLA_CARRIER]) {
2462 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2463 if (err)
2464 goto errout;
2465 status |= DO_SETLINK_MODIFIED;
2466 }
2467
2468 if (tb[IFLA_TXQLEN]) {
2469 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2470
2471 err = dev_change_tx_queue_len(dev, value);
2472 if (err)
2473 goto errout;
2474 status |= DO_SETLINK_MODIFIED;
2475 }
2476
2477 if (tb[IFLA_GSO_MAX_SIZE]) {
2478 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2479
2480 if (max_size > GSO_MAX_SIZE) {
2481 err = -EINVAL;
2482 goto errout;
2483 }
2484
2485 if (dev->gso_max_size ^ max_size) {
2486 netif_set_gso_max_size(dev, max_size);
2487 status |= DO_SETLINK_MODIFIED;
2488 }
2489 }
2490
2491 if (tb[IFLA_GSO_MAX_SEGS]) {
2492 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2493
2494 if (max_segs > GSO_MAX_SEGS) {
2495 err = -EINVAL;
2496 goto errout;
2497 }
2498
2499 if (dev->gso_max_segs ^ max_segs) {
2500 dev->gso_max_segs = max_segs;
2501 status |= DO_SETLINK_MODIFIED;
2502 }
2503 }
2504
2505 if (tb[IFLA_OPERSTATE])
2506 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2507
2508 if (tb[IFLA_LINKMODE]) {
2509 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2510
2511 write_lock_bh(&dev_base_lock);
2512 if (dev->link_mode ^ value)
2513 status |= DO_SETLINK_NOTIFY;
2514 dev->link_mode = value;
2515 write_unlock_bh(&dev_base_lock);
2516 }
2517
2518 if (tb[IFLA_VFINFO_LIST]) {
2519 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2520 struct nlattr *attr;
2521 int rem;
2522
2523 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2524 if (nla_type(attr) != IFLA_VF_INFO ||
2525 nla_len(attr) < NLA_HDRLEN) {
2526 err = -EINVAL;
2527 goto errout;
2528 }
2529 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
2530 ifla_vf_policy, NULL);
2531 if (err < 0)
2532 goto errout;
2533 err = do_setvfinfo(dev, vfinfo);
2534 if (err < 0)
2535 goto errout;
2536 status |= DO_SETLINK_NOTIFY;
2537 }
2538 }
2539 err = 0;
2540
2541 if (tb[IFLA_VF_PORTS]) {
2542 struct nlattr *port[IFLA_PORT_MAX+1];
2543 struct nlattr *attr;
2544 int vf;
2545 int rem;
2546
2547 err = -EOPNOTSUPP;
2548 if (!ops->ndo_set_vf_port)
2549 goto errout;
2550
2551 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2552 if (nla_type(attr) != IFLA_VF_PORT ||
2553 nla_len(attr) < NLA_HDRLEN) {
2554 err = -EINVAL;
2555 goto errout;
2556 }
2557 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
2558 ifla_port_policy, NULL);
2559 if (err < 0)
2560 goto errout;
2561 if (!port[IFLA_PORT_VF]) {
2562 err = -EOPNOTSUPP;
2563 goto errout;
2564 }
2565 vf = nla_get_u32(port[IFLA_PORT_VF]);
2566 err = ops->ndo_set_vf_port(dev, vf, port);
2567 if (err < 0)
2568 goto errout;
2569 status |= DO_SETLINK_NOTIFY;
2570 }
2571 }
2572 err = 0;
2573
2574 if (tb[IFLA_PORT_SELF]) {
2575 struct nlattr *port[IFLA_PORT_MAX+1];
2576
2577 err = nla_parse_nested(port, IFLA_PORT_MAX,
2578 tb[IFLA_PORT_SELF], ifla_port_policy,
2579 NULL);
2580 if (err < 0)
2581 goto errout;
2582
2583 err = -EOPNOTSUPP;
2584 if (ops->ndo_set_vf_port)
2585 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2586 if (err < 0)
2587 goto errout;
2588 status |= DO_SETLINK_NOTIFY;
2589 }
2590
2591 if (tb[IFLA_AF_SPEC]) {
2592 struct nlattr *af;
2593 int rem;
2594
2595 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2596 const struct rtnl_af_ops *af_ops;
2597
2598 rcu_read_lock();
2599
2600 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2601
2602 err = af_ops->set_link_af(dev, af);
2603 if (err < 0) {
2604 rcu_read_unlock();
2605 goto errout;
2606 }
2607
2608 rcu_read_unlock();
2609 status |= DO_SETLINK_NOTIFY;
2610 }
2611 }
2612 err = 0;
2613
2614 if (tb[IFLA_PROTO_DOWN]) {
2615 err = dev_change_proto_down(dev,
2616 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2617 if (err)
2618 goto errout;
2619 status |= DO_SETLINK_NOTIFY;
2620 }
2621
2622 if (tb[IFLA_XDP]) {
2623 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2624 u32 xdp_flags = 0;
2625
2626 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
2627 ifla_xdp_policy, NULL);
2628 if (err < 0)
2629 goto errout;
2630
2631 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2632 err = -EINVAL;
2633 goto errout;
2634 }
2635
2636 if (xdp[IFLA_XDP_FLAGS]) {
2637 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2638 if (xdp_flags & ~XDP_FLAGS_MASK) {
2639 err = -EINVAL;
2640 goto errout;
2641 }
2642 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
2643 err = -EINVAL;
2644 goto errout;
2645 }
2646 }
2647
2648 if (xdp[IFLA_XDP_FD]) {
2649 err = dev_change_xdp_fd(dev, extack,
2650 nla_get_s32(xdp[IFLA_XDP_FD]),
2651 xdp_flags);
2652 if (err)
2653 goto errout;
2654 status |= DO_SETLINK_NOTIFY;
2655 }
2656 }
2657
2658 errout:
2659 if (status & DO_SETLINK_MODIFIED) {
2660 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
2661 netdev_state_change(dev);
2662
2663 if (err < 0)
2664 net_warn_ratelimited("A link change request failed with some changes committed already. Interface %s may have been left with an inconsistent configuration, please check.\n",
2665 dev->name);
2666 }
2667
2668 return err;
2669 }
2670
rtnl_setlink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)2671 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2672 struct netlink_ext_ack *extack)
2673 {
2674 struct net *net = sock_net(skb->sk);
2675 struct ifinfomsg *ifm;
2676 struct net_device *dev;
2677 int err;
2678 struct nlattr *tb[IFLA_MAX+1];
2679 char ifname[IFNAMSIZ];
2680
2681 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2682 extack);
2683 if (err < 0)
2684 goto errout;
2685
2686 err = rtnl_ensure_unique_netns(tb, extack, false);
2687 if (err < 0)
2688 goto errout;
2689
2690 if (tb[IFLA_IFNAME])
2691 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2692 else
2693 ifname[0] = '\0';
2694
2695 err = -EINVAL;
2696 ifm = nlmsg_data(nlh);
2697 if (ifm->ifi_index > 0)
2698 dev = __dev_get_by_index(net, ifm->ifi_index);
2699 else if (tb[IFLA_IFNAME])
2700 dev = __dev_get_by_name(net, ifname);
2701 else
2702 goto errout;
2703
2704 if (dev == NULL) {
2705 err = -ENODEV;
2706 goto errout;
2707 }
2708
2709 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2710 errout:
2711 return err;
2712 }
2713
rtnl_group_dellink(const struct net * net,int group)2714 static int rtnl_group_dellink(const struct net *net, int group)
2715 {
2716 struct net_device *dev, *aux;
2717 LIST_HEAD(list_kill);
2718 bool found = false;
2719
2720 if (!group)
2721 return -EPERM;
2722
2723 for_each_netdev(net, dev) {
2724 if (dev->group == group) {
2725 const struct rtnl_link_ops *ops;
2726
2727 found = true;
2728 ops = dev->rtnl_link_ops;
2729 if (!ops || !ops->dellink)
2730 return -EOPNOTSUPP;
2731 }
2732 }
2733
2734 if (!found)
2735 return -ENODEV;
2736
2737 for_each_netdev_safe(net, dev, aux) {
2738 if (dev->group == group) {
2739 const struct rtnl_link_ops *ops;
2740
2741 ops = dev->rtnl_link_ops;
2742 ops->dellink(dev, &list_kill);
2743 }
2744 }
2745 unregister_netdevice_many(&list_kill);
2746
2747 return 0;
2748 }
2749
rtnl_delete_link(struct net_device * dev)2750 int rtnl_delete_link(struct net_device *dev)
2751 {
2752 const struct rtnl_link_ops *ops;
2753 LIST_HEAD(list_kill);
2754
2755 ops = dev->rtnl_link_ops;
2756 if (!ops || !ops->dellink)
2757 return -EOPNOTSUPP;
2758
2759 ops->dellink(dev, &list_kill);
2760 unregister_netdevice_many(&list_kill);
2761
2762 return 0;
2763 }
2764 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2765
rtnl_dellink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)2766 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2767 struct netlink_ext_ack *extack)
2768 {
2769 struct net *net = sock_net(skb->sk);
2770 struct net *tgt_net = net;
2771 struct net_device *dev = NULL;
2772 struct ifinfomsg *ifm;
2773 char ifname[IFNAMSIZ];
2774 struct nlattr *tb[IFLA_MAX+1];
2775 int err;
2776 int netnsid = -1;
2777
2778 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2779 if (err < 0)
2780 return err;
2781
2782 err = rtnl_ensure_unique_netns(tb, extack, true);
2783 if (err < 0)
2784 return err;
2785
2786 if (tb[IFLA_IFNAME])
2787 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2788
2789 if (tb[IFLA_IF_NETNSID]) {
2790 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
2791 tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid);
2792 if (IS_ERR(tgt_net))
2793 return PTR_ERR(tgt_net);
2794 }
2795
2796 err = -EINVAL;
2797 ifm = nlmsg_data(nlh);
2798 if (ifm->ifi_index > 0)
2799 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
2800 else if (tb[IFLA_IFNAME])
2801 dev = __dev_get_by_name(tgt_net, ifname);
2802 else if (tb[IFLA_GROUP])
2803 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
2804 else
2805 goto out;
2806
2807 if (!dev) {
2808 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2809 err = -ENODEV;
2810
2811 goto out;
2812 }
2813
2814 err = rtnl_delete_link(dev);
2815
2816 out:
2817 if (netnsid >= 0)
2818 put_net(tgt_net);
2819
2820 return err;
2821 }
2822
rtnl_configure_link(struct net_device * dev,const struct ifinfomsg * ifm)2823 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2824 {
2825 unsigned int old_flags;
2826 int err;
2827
2828 old_flags = dev->flags;
2829 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2830 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2831 if (err < 0)
2832 return err;
2833 }
2834
2835 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
2836 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
2837 } else {
2838 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2839 __dev_notify_flags(dev, old_flags, ~0U);
2840 }
2841 return 0;
2842 }
2843 EXPORT_SYMBOL(rtnl_configure_link);
2844
rtnl_create_link(struct net * net,const char * ifname,unsigned char name_assign_type,const struct rtnl_link_ops * ops,struct nlattr * tb[])2845 struct net_device *rtnl_create_link(struct net *net,
2846 const char *ifname, unsigned char name_assign_type,
2847 const struct rtnl_link_ops *ops, struct nlattr *tb[])
2848 {
2849 struct net_device *dev;
2850 unsigned int num_tx_queues = 1;
2851 unsigned int num_rx_queues = 1;
2852
2853 if (tb[IFLA_NUM_TX_QUEUES])
2854 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2855 else if (ops->get_num_tx_queues)
2856 num_tx_queues = ops->get_num_tx_queues();
2857
2858 if (tb[IFLA_NUM_RX_QUEUES])
2859 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2860 else if (ops->get_num_rx_queues)
2861 num_rx_queues = ops->get_num_rx_queues();
2862
2863 if (num_tx_queues < 1 || num_tx_queues > 4096)
2864 return ERR_PTR(-EINVAL);
2865
2866 if (num_rx_queues < 1 || num_rx_queues > 4096)
2867 return ERR_PTR(-EINVAL);
2868
2869 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2870 ops->setup, num_tx_queues, num_rx_queues);
2871 if (!dev)
2872 return ERR_PTR(-ENOMEM);
2873
2874 dev_net_set(dev, net);
2875 dev->rtnl_link_ops = ops;
2876 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2877
2878 if (tb[IFLA_MTU]) {
2879 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
2880 int err;
2881
2882 err = dev_validate_mtu(dev, mtu, NULL);
2883 if (err) {
2884 free_netdev(dev);
2885 return ERR_PTR(err);
2886 }
2887 dev->mtu = mtu;
2888 }
2889 if (tb[IFLA_ADDRESS]) {
2890 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2891 nla_len(tb[IFLA_ADDRESS]));
2892 dev->addr_assign_type = NET_ADDR_SET;
2893 }
2894 if (tb[IFLA_BROADCAST])
2895 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2896 nla_len(tb[IFLA_BROADCAST]));
2897 if (tb[IFLA_TXQLEN])
2898 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2899 if (tb[IFLA_OPERSTATE])
2900 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2901 if (tb[IFLA_LINKMODE])
2902 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2903 if (tb[IFLA_GROUP])
2904 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2905 if (tb[IFLA_GSO_MAX_SIZE])
2906 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2907 if (tb[IFLA_GSO_MAX_SEGS])
2908 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2909
2910 return dev;
2911 }
2912 EXPORT_SYMBOL(rtnl_create_link);
2913
rtnl_group_changelink(const struct sk_buff * skb,struct net * net,int group,struct ifinfomsg * ifm,struct netlink_ext_ack * extack,struct nlattr ** tb)2914 static int rtnl_group_changelink(const struct sk_buff *skb,
2915 struct net *net, int group,
2916 struct ifinfomsg *ifm,
2917 struct netlink_ext_ack *extack,
2918 struct nlattr **tb)
2919 {
2920 struct net_device *dev, *aux;
2921 int err;
2922
2923 for_each_netdev_safe(net, dev, aux) {
2924 if (dev->group == group) {
2925 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
2926 if (err < 0)
2927 return err;
2928 }
2929 }
2930
2931 return 0;
2932 }
2933
rtnl_newlink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)2934 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2935 struct netlink_ext_ack *extack)
2936 {
2937 struct net *net = sock_net(skb->sk);
2938 const struct rtnl_link_ops *ops;
2939 const struct rtnl_link_ops *m_ops = NULL;
2940 struct net_device *dev;
2941 struct net_device *master_dev = NULL;
2942 struct ifinfomsg *ifm;
2943 char kind[MODULE_NAME_LEN];
2944 char ifname[IFNAMSIZ];
2945 struct nlattr *tb[IFLA_MAX+1];
2946 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2947 unsigned char name_assign_type = NET_NAME_USER;
2948 int err;
2949
2950 #ifdef CONFIG_MODULES
2951 replay:
2952 #endif
2953 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2954 if (err < 0)
2955 return err;
2956
2957 err = rtnl_ensure_unique_netns(tb, extack, false);
2958 if (err < 0)
2959 return err;
2960
2961 if (tb[IFLA_IFNAME])
2962 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2963 else
2964 ifname[0] = '\0';
2965
2966 ifm = nlmsg_data(nlh);
2967 if (ifm->ifi_index > 0)
2968 dev = __dev_get_by_index(net, ifm->ifi_index);
2969 else {
2970 if (ifname[0])
2971 dev = __dev_get_by_name(net, ifname);
2972 else
2973 dev = NULL;
2974 }
2975
2976 if (dev) {
2977 master_dev = netdev_master_upper_dev_get(dev);
2978 if (master_dev)
2979 m_ops = master_dev->rtnl_link_ops;
2980 }
2981
2982 err = validate_linkmsg(dev, tb);
2983 if (err < 0)
2984 return err;
2985
2986 if (tb[IFLA_LINKINFO]) {
2987 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2988 tb[IFLA_LINKINFO], ifla_info_policy,
2989 NULL);
2990 if (err < 0)
2991 return err;
2992 } else
2993 memset(linkinfo, 0, sizeof(linkinfo));
2994
2995 if (linkinfo[IFLA_INFO_KIND]) {
2996 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2997 ops = rtnl_link_ops_get(kind);
2998 } else {
2999 kind[0] = '\0';
3000 ops = NULL;
3001 }
3002
3003 if (1) {
3004 struct nlattr *attr[RTNL_MAX_TYPE + 1];
3005 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3006 struct nlattr **data = NULL;
3007 struct nlattr **slave_data = NULL;
3008 struct net *dest_net, *link_net = NULL;
3009
3010 if (ops) {
3011 if (ops->maxtype > RTNL_MAX_TYPE)
3012 return -EINVAL;
3013
3014 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3015 err = nla_parse_nested(attr, ops->maxtype,
3016 linkinfo[IFLA_INFO_DATA],
3017 ops->policy, NULL);
3018 if (err < 0)
3019 return err;
3020 data = attr;
3021 }
3022 if (ops->validate) {
3023 err = ops->validate(tb, data, extack);
3024 if (err < 0)
3025 return err;
3026 }
3027 }
3028
3029 if (m_ops) {
3030 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3031 return -EINVAL;
3032
3033 if (m_ops->slave_maxtype &&
3034 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3035 err = nla_parse_nested(slave_attr,
3036 m_ops->slave_maxtype,
3037 linkinfo[IFLA_INFO_SLAVE_DATA],
3038 m_ops->slave_policy,
3039 NULL);
3040 if (err < 0)
3041 return err;
3042 slave_data = slave_attr;
3043 }
3044 }
3045
3046 if (dev) {
3047 int status = 0;
3048
3049 if (nlh->nlmsg_flags & NLM_F_EXCL)
3050 return -EEXIST;
3051 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3052 return -EOPNOTSUPP;
3053
3054 if (linkinfo[IFLA_INFO_DATA]) {
3055 if (!ops || ops != dev->rtnl_link_ops ||
3056 !ops->changelink)
3057 return -EOPNOTSUPP;
3058
3059 err = ops->changelink(dev, tb, data, extack);
3060 if (err < 0)
3061 return err;
3062 status |= DO_SETLINK_NOTIFY;
3063 }
3064
3065 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3066 if (!m_ops || !m_ops->slave_changelink)
3067 return -EOPNOTSUPP;
3068
3069 err = m_ops->slave_changelink(master_dev, dev,
3070 tb, slave_data,
3071 extack);
3072 if (err < 0)
3073 return err;
3074 status |= DO_SETLINK_NOTIFY;
3075 }
3076
3077 return do_setlink(skb, dev, ifm, extack, tb, ifname,
3078 status);
3079 }
3080
3081 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3082 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3083 return rtnl_group_changelink(skb, net,
3084 nla_get_u32(tb[IFLA_GROUP]),
3085 ifm, extack, tb);
3086 return -ENODEV;
3087 }
3088
3089 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3090 return -EOPNOTSUPP;
3091
3092 if (!ops) {
3093 #ifdef CONFIG_MODULES
3094 if (kind[0]) {
3095 __rtnl_unlock();
3096 request_module("rtnl-link-%s", kind);
3097 rtnl_lock();
3098 ops = rtnl_link_ops_get(kind);
3099 if (ops)
3100 goto replay;
3101 }
3102 #endif
3103 return -EOPNOTSUPP;
3104 }
3105
3106 if (!ops->setup)
3107 return -EOPNOTSUPP;
3108
3109 if (!ifname[0]) {
3110 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3111 name_assign_type = NET_NAME_ENUM;
3112 }
3113
3114 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3115 if (IS_ERR(dest_net))
3116 return PTR_ERR(dest_net);
3117
3118 if (tb[IFLA_LINK_NETNSID]) {
3119 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3120
3121 link_net = get_net_ns_by_id(dest_net, id);
3122 if (!link_net) {
3123 err = -EINVAL;
3124 goto out;
3125 }
3126 err = -EPERM;
3127 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3128 goto out;
3129 }
3130
3131 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3132 name_assign_type, ops, tb);
3133 if (IS_ERR(dev)) {
3134 err = PTR_ERR(dev);
3135 goto out;
3136 }
3137
3138 dev->ifindex = ifm->ifi_index;
3139
3140 if (ops->newlink) {
3141 err = ops->newlink(link_net ? : net, dev, tb, data,
3142 extack);
3143 /* Drivers should call free_netdev() in ->destructor
3144 * and unregister it on failure after registration
3145 * so that device could be finally freed in rtnl_unlock.
3146 */
3147 if (err < 0) {
3148 /* If device is not registered at all, free it now */
3149 if (dev->reg_state == NETREG_UNINITIALIZED ||
3150 dev->reg_state == NETREG_UNREGISTERED)
3151 free_netdev(dev);
3152 goto out;
3153 }
3154 } else {
3155 err = register_netdevice(dev);
3156 if (err < 0) {
3157 free_netdev(dev);
3158 goto out;
3159 }
3160 }
3161 err = rtnl_configure_link(dev, ifm);
3162 if (err < 0)
3163 goto out_unregister;
3164 if (link_net) {
3165 err = dev_change_net_namespace(dev, dest_net, ifname);
3166 if (err < 0)
3167 goto out_unregister;
3168 }
3169 if (tb[IFLA_MASTER]) {
3170 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
3171 extack);
3172 if (err)
3173 goto out_unregister;
3174 }
3175 out:
3176 if (link_net)
3177 put_net(link_net);
3178 put_net(dest_net);
3179 return err;
3180 out_unregister:
3181 if (ops->newlink) {
3182 LIST_HEAD(list_kill);
3183
3184 ops->dellink(dev, &list_kill);
3185 unregister_netdevice_many(&list_kill);
3186 } else {
3187 unregister_netdevice(dev);
3188 }
3189 goto out;
3190 }
3191 }
3192
rtnl_getlink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3193 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3194 struct netlink_ext_ack *extack)
3195 {
3196 struct net *net = sock_net(skb->sk);
3197 struct net *tgt_net = net;
3198 struct ifinfomsg *ifm;
3199 char ifname[IFNAMSIZ];
3200 struct nlattr *tb[IFLA_MAX+1];
3201 struct net_device *dev = NULL;
3202 struct sk_buff *nskb;
3203 int netnsid = -1;
3204 int err;
3205 u32 ext_filter_mask = 0;
3206
3207 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3208 if (err < 0)
3209 return err;
3210
3211 err = rtnl_ensure_unique_netns(tb, extack, true);
3212 if (err < 0)
3213 return err;
3214
3215 if (tb[IFLA_IF_NETNSID]) {
3216 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
3217 tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid);
3218 if (IS_ERR(tgt_net))
3219 return PTR_ERR(tgt_net);
3220 }
3221
3222 if (tb[IFLA_IFNAME])
3223 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3224
3225 if (tb[IFLA_EXT_MASK])
3226 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3227
3228 err = -EINVAL;
3229 ifm = nlmsg_data(nlh);
3230 if (ifm->ifi_index > 0)
3231 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3232 else if (tb[IFLA_IFNAME])
3233 dev = __dev_get_by_name(tgt_net, ifname);
3234 else
3235 goto out;
3236
3237 err = -ENODEV;
3238 if (dev == NULL)
3239 goto out;
3240
3241 err = -ENOBUFS;
3242 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3243 if (nskb == NULL)
3244 goto out;
3245
3246 err = rtnl_fill_ifinfo(nskb, dev, net,
3247 RTM_NEWLINK, NETLINK_CB(skb).portid,
3248 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3249 0, NULL, 0, netnsid, GFP_KERNEL);
3250 if (err < 0) {
3251 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3252 WARN_ON(err == -EMSGSIZE);
3253 kfree_skb(nskb);
3254 } else
3255 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3256 out:
3257 if (netnsid >= 0)
3258 put_net(tgt_net);
3259
3260 return err;
3261 }
3262
rtnl_calcit(struct sk_buff * skb,struct nlmsghdr * nlh)3263 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3264 {
3265 struct net *net = sock_net(skb->sk);
3266 struct net_device *dev;
3267 struct nlattr *tb[IFLA_MAX+1];
3268 u32 ext_filter_mask = 0;
3269 u16 min_ifinfo_dump_size = 0;
3270 int hdrlen;
3271
3272 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3273 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3274 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3275
3276 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3277 if (tb[IFLA_EXT_MASK])
3278 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3279 }
3280
3281 if (!ext_filter_mask)
3282 return NLMSG_GOODSIZE;
3283 /*
3284 * traverse the list of net devices and compute the minimum
3285 * buffer size based upon the filter mask.
3286 */
3287 rcu_read_lock();
3288 for_each_netdev_rcu(net, dev) {
3289 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3290 if_nlmsg_size(dev,
3291 ext_filter_mask));
3292 }
3293 rcu_read_unlock();
3294
3295 return nlmsg_total_size(min_ifinfo_dump_size);
3296 }
3297
rtnl_dump_all(struct sk_buff * skb,struct netlink_callback * cb)3298 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3299 {
3300 int idx;
3301 int s_idx = cb->family;
3302 int type = cb->nlh->nlmsg_type - RTM_BASE;
3303
3304 if (s_idx == 0)
3305 s_idx = 1;
3306
3307 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3308 struct rtnl_link **tab;
3309 struct rtnl_link *link;
3310 rtnl_dumpit_func dumpit;
3311
3312 if (idx < s_idx || idx == PF_PACKET)
3313 continue;
3314
3315 if (type < 0 || type >= RTM_NR_MSGTYPES)
3316 continue;
3317
3318 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3319 if (!tab)
3320 continue;
3321
3322 link = tab[type];
3323 if (!link)
3324 continue;
3325
3326 dumpit = link->dumpit;
3327 if (!dumpit)
3328 continue;
3329
3330 if (idx > s_idx) {
3331 memset(&cb->args[0], 0, sizeof(cb->args));
3332 cb->prev_seq = 0;
3333 cb->seq = 0;
3334 }
3335 if (dumpit(skb, cb))
3336 break;
3337 }
3338 cb->family = idx;
3339
3340 return skb->len;
3341 }
3342
rtmsg_ifinfo_build_skb(int type,struct net_device * dev,unsigned int change,u32 event,gfp_t flags,int * new_nsid,int new_ifindex)3343 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3344 unsigned int change,
3345 u32 event, gfp_t flags, int *new_nsid,
3346 int new_ifindex)
3347 {
3348 struct net *net = dev_net(dev);
3349 struct sk_buff *skb;
3350 int err = -ENOBUFS;
3351 size_t if_info_size;
3352
3353 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
3354 if (skb == NULL)
3355 goto errout;
3356
3357 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3358 type, 0, 0, change, 0, 0, event,
3359 new_nsid, new_ifindex, -1, flags);
3360 if (err < 0) {
3361 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3362 WARN_ON(err == -EMSGSIZE);
3363 kfree_skb(skb);
3364 goto errout;
3365 }
3366 return skb;
3367 errout:
3368 if (err < 0)
3369 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3370 return NULL;
3371 }
3372
rtmsg_ifinfo_send(struct sk_buff * skb,struct net_device * dev,gfp_t flags)3373 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3374 {
3375 struct net *net = dev_net(dev);
3376
3377 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3378 }
3379
rtmsg_ifinfo_event(int type,struct net_device * dev,unsigned int change,u32 event,gfp_t flags,int * new_nsid,int new_ifindex)3380 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3381 unsigned int change, u32 event,
3382 gfp_t flags, int *new_nsid, int new_ifindex)
3383 {
3384 struct sk_buff *skb;
3385
3386 if (dev->reg_state != NETREG_REGISTERED)
3387 return;
3388
3389 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3390 new_ifindex);
3391 if (skb)
3392 rtmsg_ifinfo_send(skb, dev, flags);
3393 }
3394
rtmsg_ifinfo(int type,struct net_device * dev,unsigned int change,gfp_t flags)3395 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3396 gfp_t flags)
3397 {
3398 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3399 NULL, 0);
3400 }
3401
rtmsg_ifinfo_newnet(int type,struct net_device * dev,unsigned int change,gfp_t flags,int * new_nsid,int new_ifindex)3402 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3403 gfp_t flags, int *new_nsid, int new_ifindex)
3404 {
3405 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3406 new_nsid, new_ifindex);
3407 }
3408
nlmsg_populate_fdb_fill(struct sk_buff * skb,struct net_device * dev,u8 * addr,u16 vid,u32 pid,u32 seq,int type,unsigned int flags,int nlflags,u16 ndm_state)3409 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3410 struct net_device *dev,
3411 u8 *addr, u16 vid, u32 pid, u32 seq,
3412 int type, unsigned int flags,
3413 int nlflags, u16 ndm_state)
3414 {
3415 struct nlmsghdr *nlh;
3416 struct ndmsg *ndm;
3417
3418 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
3419 if (!nlh)
3420 return -EMSGSIZE;
3421
3422 ndm = nlmsg_data(nlh);
3423 ndm->ndm_family = AF_BRIDGE;
3424 ndm->ndm_pad1 = 0;
3425 ndm->ndm_pad2 = 0;
3426 ndm->ndm_flags = flags;
3427 ndm->ndm_type = 0;
3428 ndm->ndm_ifindex = dev->ifindex;
3429 ndm->ndm_state = ndm_state;
3430
3431 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3432 goto nla_put_failure;
3433 if (vid)
3434 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3435 goto nla_put_failure;
3436
3437 nlmsg_end(skb, nlh);
3438 return 0;
3439
3440 nla_put_failure:
3441 nlmsg_cancel(skb, nlh);
3442 return -EMSGSIZE;
3443 }
3444
rtnl_fdb_nlmsg_size(void)3445 static inline size_t rtnl_fdb_nlmsg_size(void)
3446 {
3447 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3448 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3449 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3450 0;
3451 }
3452
rtnl_fdb_notify(struct net_device * dev,u8 * addr,u16 vid,int type,u16 ndm_state)3453 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3454 u16 ndm_state)
3455 {
3456 struct net *net = dev_net(dev);
3457 struct sk_buff *skb;
3458 int err = -ENOBUFS;
3459
3460 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3461 if (!skb)
3462 goto errout;
3463
3464 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
3465 0, 0, type, NTF_SELF, 0, ndm_state);
3466 if (err < 0) {
3467 kfree_skb(skb);
3468 goto errout;
3469 }
3470
3471 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3472 return;
3473 errout:
3474 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3475 }
3476
3477 /**
3478 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3479 */
ndo_dflt_fdb_add(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u16 flags)3480 int ndo_dflt_fdb_add(struct ndmsg *ndm,
3481 struct nlattr *tb[],
3482 struct net_device *dev,
3483 const unsigned char *addr, u16 vid,
3484 u16 flags)
3485 {
3486 int err = -EINVAL;
3487
3488 /* If aging addresses are supported device will need to
3489 * implement its own handler for this.
3490 */
3491 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3492 pr_info("%s: FDB only supports static addresses\n", dev->name);
3493 return err;
3494 }
3495
3496 if (vid) {
3497 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3498 return err;
3499 }
3500
3501 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3502 err = dev_uc_add_excl(dev, addr);
3503 else if (is_multicast_ether_addr(addr))
3504 err = dev_mc_add_excl(dev, addr);
3505
3506 /* Only return duplicate errors if NLM_F_EXCL is set */
3507 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3508 err = 0;
3509
3510 return err;
3511 }
3512 EXPORT_SYMBOL(ndo_dflt_fdb_add);
3513
fdb_vid_parse(struct nlattr * vlan_attr,u16 * p_vid,struct netlink_ext_ack * extack)3514 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3515 struct netlink_ext_ack *extack)
3516 {
3517 u16 vid = 0;
3518
3519 if (vlan_attr) {
3520 if (nla_len(vlan_attr) != sizeof(u16)) {
3521 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
3522 return -EINVAL;
3523 }
3524
3525 vid = nla_get_u16(vlan_attr);
3526
3527 if (!vid || vid >= VLAN_VID_MASK) {
3528 NL_SET_ERR_MSG(extack, "invalid vlan id");
3529 return -EINVAL;
3530 }
3531 }
3532 *p_vid = vid;
3533 return 0;
3534 }
3535
rtnl_fdb_add(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3536 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3537 struct netlink_ext_ack *extack)
3538 {
3539 struct net *net = sock_net(skb->sk);
3540 struct ndmsg *ndm;
3541 struct nlattr *tb[NDA_MAX+1];
3542 struct net_device *dev;
3543 u8 *addr;
3544 u16 vid;
3545 int err;
3546
3547 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
3548 if (err < 0)
3549 return err;
3550
3551 ndm = nlmsg_data(nlh);
3552 if (ndm->ndm_ifindex == 0) {
3553 NL_SET_ERR_MSG(extack, "invalid ifindex");
3554 return -EINVAL;
3555 }
3556
3557 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3558 if (dev == NULL) {
3559 NL_SET_ERR_MSG(extack, "unknown ifindex");
3560 return -ENODEV;
3561 }
3562
3563 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3564 NL_SET_ERR_MSG(extack, "invalid address");
3565 return -EINVAL;
3566 }
3567
3568 if (dev->type != ARPHRD_ETHER) {
3569 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
3570 return -EINVAL;
3571 }
3572
3573 addr = nla_data(tb[NDA_LLADDR]);
3574
3575 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3576 if (err)
3577 return err;
3578
3579 err = -EOPNOTSUPP;
3580
3581 /* Support fdb on master device the net/bridge default case */
3582 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3583 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3584 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3585 const struct net_device_ops *ops = br_dev->netdev_ops;
3586
3587 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3588 nlh->nlmsg_flags);
3589 if (err)
3590 goto out;
3591 else
3592 ndm->ndm_flags &= ~NTF_MASTER;
3593 }
3594
3595 /* Embedded bridge, macvlan, and any other device support */
3596 if ((ndm->ndm_flags & NTF_SELF)) {
3597 if (dev->netdev_ops->ndo_fdb_add)
3598 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3599 vid,
3600 nlh->nlmsg_flags);
3601 else
3602 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
3603 nlh->nlmsg_flags);
3604
3605 if (!err) {
3606 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3607 ndm->ndm_state);
3608 ndm->ndm_flags &= ~NTF_SELF;
3609 }
3610 }
3611 out:
3612 return err;
3613 }
3614
3615 /**
3616 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3617 */
ndo_dflt_fdb_del(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid)3618 int ndo_dflt_fdb_del(struct ndmsg *ndm,
3619 struct nlattr *tb[],
3620 struct net_device *dev,
3621 const unsigned char *addr, u16 vid)
3622 {
3623 int err = -EINVAL;
3624
3625 /* If aging addresses are supported device will need to
3626 * implement its own handler for this.
3627 */
3628 if (!(ndm->ndm_state & NUD_PERMANENT)) {
3629 pr_info("%s: FDB only supports static addresses\n", dev->name);
3630 return err;
3631 }
3632
3633 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3634 err = dev_uc_del(dev, addr);
3635 else if (is_multicast_ether_addr(addr))
3636 err = dev_mc_del(dev, addr);
3637
3638 return err;
3639 }
3640 EXPORT_SYMBOL(ndo_dflt_fdb_del);
3641
rtnl_fdb_del(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3642 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3643 struct netlink_ext_ack *extack)
3644 {
3645 struct net *net = sock_net(skb->sk);
3646 struct ndmsg *ndm;
3647 struct nlattr *tb[NDA_MAX+1];
3648 struct net_device *dev;
3649 int err = -EINVAL;
3650 __u8 *addr;
3651 u16 vid;
3652
3653 if (!netlink_capable(skb, CAP_NET_ADMIN))
3654 return -EPERM;
3655
3656 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
3657 if (err < 0)
3658 return err;
3659
3660 ndm = nlmsg_data(nlh);
3661 if (ndm->ndm_ifindex == 0) {
3662 NL_SET_ERR_MSG(extack, "invalid ifindex");
3663 return -EINVAL;
3664 }
3665
3666 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3667 if (dev == NULL) {
3668 NL_SET_ERR_MSG(extack, "unknown ifindex");
3669 return -ENODEV;
3670 }
3671
3672 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3673 NL_SET_ERR_MSG(extack, "invalid address");
3674 return -EINVAL;
3675 }
3676
3677 if (dev->type != ARPHRD_ETHER) {
3678 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3679 return -EINVAL;
3680 }
3681
3682 addr = nla_data(tb[NDA_LLADDR]);
3683
3684 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
3685 if (err)
3686 return err;
3687
3688 err = -EOPNOTSUPP;
3689
3690 /* Support fdb on master device the net/bridge default case */
3691 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3692 (dev->priv_flags & IFF_BRIDGE_PORT)) {
3693 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3694 const struct net_device_ops *ops = br_dev->netdev_ops;
3695
3696 if (ops->ndo_fdb_del)
3697 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
3698
3699 if (err)
3700 goto out;
3701 else
3702 ndm->ndm_flags &= ~NTF_MASTER;
3703 }
3704
3705 /* Embedded bridge, macvlan, and any other device support */
3706 if (ndm->ndm_flags & NTF_SELF) {
3707 if (dev->netdev_ops->ndo_fdb_del)
3708 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3709 vid);
3710 else
3711 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
3712
3713 if (!err) {
3714 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3715 ndm->ndm_state);
3716 ndm->ndm_flags &= ~NTF_SELF;
3717 }
3718 }
3719 out:
3720 return err;
3721 }
3722
nlmsg_populate_fdb(struct sk_buff * skb,struct netlink_callback * cb,struct net_device * dev,int * idx,struct netdev_hw_addr_list * list)3723 static int nlmsg_populate_fdb(struct sk_buff *skb,
3724 struct netlink_callback *cb,
3725 struct net_device *dev,
3726 int *idx,
3727 struct netdev_hw_addr_list *list)
3728 {
3729 struct netdev_hw_addr *ha;
3730 int err;
3731 u32 portid, seq;
3732
3733 portid = NETLINK_CB(cb->skb).portid;
3734 seq = cb->nlh->nlmsg_seq;
3735
3736 list_for_each_entry(ha, &list->list, list) {
3737 if (*idx < cb->args[2])
3738 goto skip;
3739
3740 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3741 portid, seq,
3742 RTM_NEWNEIGH, NTF_SELF,
3743 NLM_F_MULTI, NUD_PERMANENT);
3744 if (err < 0)
3745 return err;
3746 skip:
3747 *idx += 1;
3748 }
3749 return 0;
3750 }
3751
3752 /**
3753 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3754 * @nlh: netlink message header
3755 * @dev: netdevice
3756 *
3757 * Default netdevice operation to dump the existing unicast address list.
3758 * Returns number of addresses from list put in skb.
3759 */
ndo_dflt_fdb_dump(struct sk_buff * skb,struct netlink_callback * cb,struct net_device * dev,struct net_device * filter_dev,int * idx)3760 int ndo_dflt_fdb_dump(struct sk_buff *skb,
3761 struct netlink_callback *cb,
3762 struct net_device *dev,
3763 struct net_device *filter_dev,
3764 int *idx)
3765 {
3766 int err;
3767
3768 if (dev->type != ARPHRD_ETHER)
3769 return -EINVAL;
3770
3771 netif_addr_lock_bh(dev);
3772 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
3773 if (err)
3774 goto out;
3775 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
3776 out:
3777 netif_addr_unlock_bh(dev);
3778 return err;
3779 }
3780 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3781
rtnl_fdb_dump(struct sk_buff * skb,struct netlink_callback * cb)3782 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3783 {
3784 struct net_device *dev;
3785 struct nlattr *tb[IFLA_MAX+1];
3786 struct net_device *br_dev = NULL;
3787 const struct net_device_ops *ops = NULL;
3788 const struct net_device_ops *cops = NULL;
3789 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3790 struct net *net = sock_net(skb->sk);
3791 struct hlist_head *head;
3792 int brport_idx = 0;
3793 int br_idx = 0;
3794 int h, s_h;
3795 int idx = 0, s_idx;
3796 int err = 0;
3797 int fidx = 0;
3798
3799 /* A hack to preserve kernel<->userspace interface.
3800 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
3801 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
3802 * So, check for ndmsg with an optional u32 attribute (not used here).
3803 * Fortunately these sizes don't conflict with the size of ifinfomsg
3804 * with an optional attribute.
3805 */
3806 if (nlmsg_len(cb->nlh) != sizeof(struct ndmsg) &&
3807 (nlmsg_len(cb->nlh) != sizeof(struct ndmsg) +
3808 nla_attr_size(sizeof(u32)))) {
3809 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3810 IFLA_MAX, ifla_policy, NULL);
3811 if (err < 0) {
3812 return -EINVAL;
3813 } else if (err == 0) {
3814 if (tb[IFLA_MASTER])
3815 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3816 }
3817
3818 brport_idx = ifm->ifi_index;
3819 }
3820
3821 if (br_idx) {
3822 br_dev = __dev_get_by_index(net, br_idx);
3823 if (!br_dev)
3824 return -ENODEV;
3825
3826 ops = br_dev->netdev_ops;
3827 }
3828
3829 s_h = cb->args[0];
3830 s_idx = cb->args[1];
3831
3832 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3833 idx = 0;
3834 head = &net->dev_index_head[h];
3835 hlist_for_each_entry(dev, head, index_hlist) {
3836
3837 if (brport_idx && (dev->ifindex != brport_idx))
3838 continue;
3839
3840 if (!br_idx) { /* user did not specify a specific bridge */
3841 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3842 br_dev = netdev_master_upper_dev_get(dev);
3843 cops = br_dev->netdev_ops;
3844 }
3845 } else {
3846 if (dev != br_dev &&
3847 !(dev->priv_flags & IFF_BRIDGE_PORT))
3848 continue;
3849
3850 if (br_dev != netdev_master_upper_dev_get(dev) &&
3851 !(dev->priv_flags & IFF_EBRIDGE))
3852 continue;
3853 cops = ops;
3854 }
3855
3856 if (idx < s_idx)
3857 goto cont;
3858
3859 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3860 if (cops && cops->ndo_fdb_dump) {
3861 err = cops->ndo_fdb_dump(skb, cb,
3862 br_dev, dev,
3863 &fidx);
3864 if (err == -EMSGSIZE)
3865 goto out;
3866 }
3867 }
3868
3869 if (dev->netdev_ops->ndo_fdb_dump)
3870 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3871 dev, NULL,
3872 &fidx);
3873 else
3874 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3875 &fidx);
3876 if (err == -EMSGSIZE)
3877 goto out;
3878
3879 cops = NULL;
3880
3881 /* reset fdb offset to 0 for rest of the interfaces */
3882 cb->args[2] = 0;
3883 fidx = 0;
3884 cont:
3885 idx++;
3886 }
3887 }
3888
3889 out:
3890 cb->args[0] = h;
3891 cb->args[1] = idx;
3892 cb->args[2] = fidx;
3893
3894 return skb->len;
3895 }
3896
brport_nla_put_flag(struct sk_buff * skb,u32 flags,u32 mask,unsigned int attrnum,unsigned int flag)3897 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3898 unsigned int attrnum, unsigned int flag)
3899 {
3900 if (mask & flag)
3901 return nla_put_u8(skb, attrnum, !!(flags & flag));
3902 return 0;
3903 }
3904
ndo_dflt_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u16 mode,u32 flags,u32 mask,int nlflags,u32 filter_mask,int (* vlan_fill)(struct sk_buff * skb,struct net_device * dev,u32 filter_mask))3905 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
3906 struct net_device *dev, u16 mode,
3907 u32 flags, u32 mask, int nlflags,
3908 u32 filter_mask,
3909 int (*vlan_fill)(struct sk_buff *skb,
3910 struct net_device *dev,
3911 u32 filter_mask))
3912 {
3913 struct nlmsghdr *nlh;
3914 struct ifinfomsg *ifm;
3915 struct nlattr *br_afspec;
3916 struct nlattr *protinfo;
3917 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3918 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3919 int err = 0;
3920
3921 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3922 if (nlh == NULL)
3923 return -EMSGSIZE;
3924
3925 ifm = nlmsg_data(nlh);
3926 ifm->ifi_family = AF_BRIDGE;
3927 ifm->__ifi_pad = 0;
3928 ifm->ifi_type = dev->type;
3929 ifm->ifi_index = dev->ifindex;
3930 ifm->ifi_flags = dev_get_flags(dev);
3931 ifm->ifi_change = 0;
3932
3933
3934 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3935 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3936 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3937 (br_dev &&
3938 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3939 (dev->addr_len &&
3940 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3941 (dev->ifindex != dev_get_iflink(dev) &&
3942 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3943 goto nla_put_failure;
3944
3945 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3946 if (!br_afspec)
3947 goto nla_put_failure;
3948
3949 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3950 nla_nest_cancel(skb, br_afspec);
3951 goto nla_put_failure;
3952 }
3953
3954 if (mode != BRIDGE_MODE_UNDEF) {
3955 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3956 nla_nest_cancel(skb, br_afspec);
3957 goto nla_put_failure;
3958 }
3959 }
3960 if (vlan_fill) {
3961 err = vlan_fill(skb, dev, filter_mask);
3962 if (err) {
3963 nla_nest_cancel(skb, br_afspec);
3964 goto nla_put_failure;
3965 }
3966 }
3967 nla_nest_end(skb, br_afspec);
3968
3969 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3970 if (!protinfo)
3971 goto nla_put_failure;
3972
3973 if (brport_nla_put_flag(skb, flags, mask,
3974 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3975 brport_nla_put_flag(skb, flags, mask,
3976 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3977 brport_nla_put_flag(skb, flags, mask,
3978 IFLA_BRPORT_FAST_LEAVE,
3979 BR_MULTICAST_FAST_LEAVE) ||
3980 brport_nla_put_flag(skb, flags, mask,
3981 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3982 brport_nla_put_flag(skb, flags, mask,
3983 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3984 brport_nla_put_flag(skb, flags, mask,
3985 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3986 brport_nla_put_flag(skb, flags, mask,
3987 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3988 brport_nla_put_flag(skb, flags, mask,
3989 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3990 nla_nest_cancel(skb, protinfo);
3991 goto nla_put_failure;
3992 }
3993
3994 nla_nest_end(skb, protinfo);
3995
3996 nlmsg_end(skb, nlh);
3997 return 0;
3998 nla_put_failure:
3999 nlmsg_cancel(skb, nlh);
4000 return err ? err : -EMSGSIZE;
4001 }
4002 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4003
rtnl_bridge_getlink(struct sk_buff * skb,struct netlink_callback * cb)4004 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4005 {
4006 struct net *net = sock_net(skb->sk);
4007 struct net_device *dev;
4008 int idx = 0;
4009 u32 portid = NETLINK_CB(cb->skb).portid;
4010 u32 seq = cb->nlh->nlmsg_seq;
4011 u32 filter_mask = 0;
4012 int err;
4013
4014 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
4015 struct nlattr *extfilt;
4016
4017 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
4018 IFLA_EXT_MASK);
4019 if (extfilt) {
4020 if (nla_len(extfilt) < sizeof(filter_mask))
4021 return -EINVAL;
4022
4023 filter_mask = nla_get_u32(extfilt);
4024 }
4025 }
4026
4027 rcu_read_lock();
4028 for_each_netdev_rcu(net, dev) {
4029 const struct net_device_ops *ops = dev->netdev_ops;
4030 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4031
4032 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4033 if (idx >= cb->args[0]) {
4034 err = br_dev->netdev_ops->ndo_bridge_getlink(
4035 skb, portid, seq, dev,
4036 filter_mask, NLM_F_MULTI);
4037 if (err < 0 && err != -EOPNOTSUPP) {
4038 if (likely(skb->len))
4039 break;
4040
4041 goto out_err;
4042 }
4043 }
4044 idx++;
4045 }
4046
4047 if (ops->ndo_bridge_getlink) {
4048 if (idx >= cb->args[0]) {
4049 err = ops->ndo_bridge_getlink(skb, portid,
4050 seq, dev,
4051 filter_mask,
4052 NLM_F_MULTI);
4053 if (err < 0 && err != -EOPNOTSUPP) {
4054 if (likely(skb->len))
4055 break;
4056
4057 goto out_err;
4058 }
4059 }
4060 idx++;
4061 }
4062 }
4063 err = skb->len;
4064 out_err:
4065 rcu_read_unlock();
4066 cb->args[0] = idx;
4067
4068 return err;
4069 }
4070
bridge_nlmsg_size(void)4071 static inline size_t bridge_nlmsg_size(void)
4072 {
4073 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4074 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4075 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4076 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4077 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4078 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4079 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4080 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4081 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4082 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4083 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4084 }
4085
rtnl_bridge_notify(struct net_device * dev)4086 static int rtnl_bridge_notify(struct net_device *dev)
4087 {
4088 struct net *net = dev_net(dev);
4089 struct sk_buff *skb;
4090 int err = -EOPNOTSUPP;
4091
4092 if (!dev->netdev_ops->ndo_bridge_getlink)
4093 return 0;
4094
4095 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4096 if (!skb) {
4097 err = -ENOMEM;
4098 goto errout;
4099 }
4100
4101 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
4102 if (err < 0)
4103 goto errout;
4104
4105 if (!skb->len)
4106 goto errout;
4107
4108 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4109 return 0;
4110 errout:
4111 WARN_ON(err == -EMSGSIZE);
4112 kfree_skb(skb);
4113 if (err)
4114 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4115 return err;
4116 }
4117
rtnl_bridge_setlink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)4118 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4119 struct netlink_ext_ack *extack)
4120 {
4121 struct net *net = sock_net(skb->sk);
4122 struct ifinfomsg *ifm;
4123 struct net_device *dev;
4124 struct nlattr *br_spec, *attr = NULL;
4125 int rem, err = -EOPNOTSUPP;
4126 u16 flags = 0;
4127 bool have_flags = false;
4128
4129 if (nlmsg_len(nlh) < sizeof(*ifm))
4130 return -EINVAL;
4131
4132 ifm = nlmsg_data(nlh);
4133 if (ifm->ifi_family != AF_BRIDGE)
4134 return -EPFNOSUPPORT;
4135
4136 dev = __dev_get_by_index(net, ifm->ifi_index);
4137 if (!dev) {
4138 NL_SET_ERR_MSG(extack, "unknown ifindex");
4139 return -ENODEV;
4140 }
4141
4142 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4143 if (br_spec) {
4144 nla_for_each_nested(attr, br_spec, rem) {
4145 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4146 if (nla_len(attr) < sizeof(flags))
4147 return -EINVAL;
4148
4149 have_flags = true;
4150 flags = nla_get_u16(attr);
4151 break;
4152 }
4153 }
4154 }
4155
4156 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4157 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4158
4159 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
4160 err = -EOPNOTSUPP;
4161 goto out;
4162 }
4163
4164 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
4165 if (err)
4166 goto out;
4167
4168 flags &= ~BRIDGE_FLAGS_MASTER;
4169 }
4170
4171 if ((flags & BRIDGE_FLAGS_SELF)) {
4172 if (!dev->netdev_ops->ndo_bridge_setlink)
4173 err = -EOPNOTSUPP;
4174 else
4175 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4176 flags);
4177 if (!err) {
4178 flags &= ~BRIDGE_FLAGS_SELF;
4179
4180 /* Generate event to notify upper layer of bridge
4181 * change
4182 */
4183 err = rtnl_bridge_notify(dev);
4184 }
4185 }
4186
4187 if (have_flags)
4188 memcpy(nla_data(attr), &flags, sizeof(flags));
4189 out:
4190 return err;
4191 }
4192
rtnl_bridge_dellink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)4193 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4194 struct netlink_ext_ack *extack)
4195 {
4196 struct net *net = sock_net(skb->sk);
4197 struct ifinfomsg *ifm;
4198 struct net_device *dev;
4199 struct nlattr *br_spec, *attr = NULL;
4200 int rem, err = -EOPNOTSUPP;
4201 u16 flags = 0;
4202 bool have_flags = false;
4203
4204 if (nlmsg_len(nlh) < sizeof(*ifm))
4205 return -EINVAL;
4206
4207 ifm = nlmsg_data(nlh);
4208 if (ifm->ifi_family != AF_BRIDGE)
4209 return -EPFNOSUPPORT;
4210
4211 dev = __dev_get_by_index(net, ifm->ifi_index);
4212 if (!dev) {
4213 NL_SET_ERR_MSG(extack, "unknown ifindex");
4214 return -ENODEV;
4215 }
4216
4217 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4218 if (br_spec) {
4219 nla_for_each_nested(attr, br_spec, rem) {
4220 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
4221 if (nla_len(attr) < sizeof(flags))
4222 return -EINVAL;
4223
4224 have_flags = true;
4225 flags = nla_get_u16(attr);
4226 break;
4227 }
4228 }
4229 }
4230
4231 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4232 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4233
4234 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4235 err = -EOPNOTSUPP;
4236 goto out;
4237 }
4238
4239 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
4240 if (err)
4241 goto out;
4242
4243 flags &= ~BRIDGE_FLAGS_MASTER;
4244 }
4245
4246 if ((flags & BRIDGE_FLAGS_SELF)) {
4247 if (!dev->netdev_ops->ndo_bridge_dellink)
4248 err = -EOPNOTSUPP;
4249 else
4250 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4251 flags);
4252
4253 if (!err) {
4254 flags &= ~BRIDGE_FLAGS_SELF;
4255
4256 /* Generate event to notify upper layer of bridge
4257 * change
4258 */
4259 err = rtnl_bridge_notify(dev);
4260 }
4261 }
4262
4263 if (have_flags)
4264 memcpy(nla_data(attr), &flags, sizeof(flags));
4265 out:
4266 return err;
4267 }
4268
stats_attr_valid(unsigned int mask,int attrid,int idxattr)4269 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4270 {
4271 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4272 (!idxattr || idxattr == attrid);
4273 }
4274
4275 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
rtnl_get_offload_stats_attr_size(int attr_id)4276 static int rtnl_get_offload_stats_attr_size(int attr_id)
4277 {
4278 switch (attr_id) {
4279 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4280 return sizeof(struct rtnl_link_stats64);
4281 }
4282
4283 return 0;
4284 }
4285
rtnl_get_offload_stats(struct sk_buff * skb,struct net_device * dev,int * prividx)4286 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4287 int *prividx)
4288 {
4289 struct nlattr *attr = NULL;
4290 int attr_id, size;
4291 void *attr_data;
4292 int err;
4293
4294 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4295 dev->netdev_ops->ndo_get_offload_stats))
4296 return -ENODATA;
4297
4298 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4299 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4300 if (attr_id < *prividx)
4301 continue;
4302
4303 size = rtnl_get_offload_stats_attr_size(attr_id);
4304 if (!size)
4305 continue;
4306
4307 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4308 continue;
4309
4310 attr = nla_reserve_64bit(skb, attr_id, size,
4311 IFLA_OFFLOAD_XSTATS_UNSPEC);
4312 if (!attr)
4313 goto nla_put_failure;
4314
4315 attr_data = nla_data(attr);
4316 memset(attr_data, 0, size);
4317 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4318 attr_data);
4319 if (err)
4320 goto get_offload_stats_failure;
4321 }
4322
4323 if (!attr)
4324 return -ENODATA;
4325
4326 *prividx = 0;
4327 return 0;
4328
4329 nla_put_failure:
4330 err = -EMSGSIZE;
4331 get_offload_stats_failure:
4332 *prividx = attr_id;
4333 return err;
4334 }
4335
rtnl_get_offload_stats_size(const struct net_device * dev)4336 static int rtnl_get_offload_stats_size(const struct net_device *dev)
4337 {
4338 int nla_size = 0;
4339 int attr_id;
4340 int size;
4341
4342 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4343 dev->netdev_ops->ndo_get_offload_stats))
4344 return 0;
4345
4346 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4347 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4348 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
4349 continue;
4350 size = rtnl_get_offload_stats_attr_size(attr_id);
4351 nla_size += nla_total_size_64bit(size);
4352 }
4353
4354 if (nla_size != 0)
4355 nla_size += nla_total_size(0);
4356
4357 return nla_size;
4358 }
4359
rtnl_fill_statsinfo(struct sk_buff * skb,struct net_device * dev,int type,u32 pid,u32 seq,u32 change,unsigned int flags,unsigned int filter_mask,int * idxattr,int * prividx)4360 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4361 int type, u32 pid, u32 seq, u32 change,
4362 unsigned int flags, unsigned int filter_mask,
4363 int *idxattr, int *prividx)
4364 {
4365 struct if_stats_msg *ifsm;
4366 struct nlmsghdr *nlh;
4367 struct nlattr *attr;
4368 int s_prividx = *prividx;
4369 int err;
4370
4371 ASSERT_RTNL();
4372
4373 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4374 if (!nlh)
4375 return -EMSGSIZE;
4376
4377 ifsm = nlmsg_data(nlh);
4378 ifsm->family = PF_UNSPEC;
4379 ifsm->pad1 = 0;
4380 ifsm->pad2 = 0;
4381 ifsm->ifindex = dev->ifindex;
4382 ifsm->filter_mask = filter_mask;
4383
4384 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
4385 struct rtnl_link_stats64 *sp;
4386
4387 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4388 sizeof(struct rtnl_link_stats64),
4389 IFLA_STATS_UNSPEC);
4390 if (!attr)
4391 goto nla_put_failure;
4392
4393 sp = nla_data(attr);
4394 dev_get_stats(dev, sp);
4395 }
4396
4397 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4398 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4399
4400 if (ops && ops->fill_linkxstats) {
4401 *idxattr = IFLA_STATS_LINK_XSTATS;
4402 attr = nla_nest_start(skb,
4403 IFLA_STATS_LINK_XSTATS);
4404 if (!attr)
4405 goto nla_put_failure;
4406
4407 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4408 nla_nest_end(skb, attr);
4409 if (err)
4410 goto nla_put_failure;
4411 *idxattr = 0;
4412 }
4413 }
4414
4415 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4416 *idxattr)) {
4417 const struct rtnl_link_ops *ops = NULL;
4418 const struct net_device *master;
4419
4420 master = netdev_master_upper_dev_get(dev);
4421 if (master)
4422 ops = master->rtnl_link_ops;
4423 if (ops && ops->fill_linkxstats) {
4424 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4425 attr = nla_nest_start(skb,
4426 IFLA_STATS_LINK_XSTATS_SLAVE);
4427 if (!attr)
4428 goto nla_put_failure;
4429
4430 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4431 nla_nest_end(skb, attr);
4432 if (err)
4433 goto nla_put_failure;
4434 *idxattr = 0;
4435 }
4436 }
4437
4438 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4439 *idxattr)) {
4440 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4441 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4442 if (!attr)
4443 goto nla_put_failure;
4444
4445 err = rtnl_get_offload_stats(skb, dev, prividx);
4446 if (err == -ENODATA)
4447 nla_nest_cancel(skb, attr);
4448 else
4449 nla_nest_end(skb, attr);
4450
4451 if (err && err != -ENODATA)
4452 goto nla_put_failure;
4453 *idxattr = 0;
4454 }
4455
4456 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4457 struct rtnl_af_ops *af_ops;
4458
4459 *idxattr = IFLA_STATS_AF_SPEC;
4460 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4461 if (!attr)
4462 goto nla_put_failure;
4463
4464 rcu_read_lock();
4465 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4466 if (af_ops->fill_stats_af) {
4467 struct nlattr *af;
4468 int err;
4469
4470 af = nla_nest_start(skb, af_ops->family);
4471 if (!af) {
4472 rcu_read_unlock();
4473 goto nla_put_failure;
4474 }
4475 err = af_ops->fill_stats_af(skb, dev);
4476
4477 if (err == -ENODATA) {
4478 nla_nest_cancel(skb, af);
4479 } else if (err < 0) {
4480 rcu_read_unlock();
4481 goto nla_put_failure;
4482 }
4483
4484 nla_nest_end(skb, af);
4485 }
4486 }
4487 rcu_read_unlock();
4488
4489 nla_nest_end(skb, attr);
4490
4491 *idxattr = 0;
4492 }
4493
4494 nlmsg_end(skb, nlh);
4495
4496 return 0;
4497
4498 nla_put_failure:
4499 /* not a multi message or no progress mean a real error */
4500 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4501 nlmsg_cancel(skb, nlh);
4502 else
4503 nlmsg_end(skb, nlh);
4504
4505 return -EMSGSIZE;
4506 }
4507
if_nlmsg_stats_size(const struct net_device * dev,u32 filter_mask)4508 static size_t if_nlmsg_stats_size(const struct net_device *dev,
4509 u32 filter_mask)
4510 {
4511 size_t size = 0;
4512
4513 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
4514 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4515
4516 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4517 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4518 int attr = IFLA_STATS_LINK_XSTATS;
4519
4520 if (ops && ops->get_linkxstats_size) {
4521 size += nla_total_size(ops->get_linkxstats_size(dev,
4522 attr));
4523 /* for IFLA_STATS_LINK_XSTATS */
4524 size += nla_total_size(0);
4525 }
4526 }
4527
4528 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4529 struct net_device *_dev = (struct net_device *)dev;
4530 const struct rtnl_link_ops *ops = NULL;
4531 const struct net_device *master;
4532
4533 /* netdev_master_upper_dev_get can't take const */
4534 master = netdev_master_upper_dev_get(_dev);
4535 if (master)
4536 ops = master->rtnl_link_ops;
4537 if (ops && ops->get_linkxstats_size) {
4538 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4539
4540 size += nla_total_size(ops->get_linkxstats_size(dev,
4541 attr));
4542 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4543 size += nla_total_size(0);
4544 }
4545 }
4546
4547 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4548 size += rtnl_get_offload_stats_size(dev);
4549
4550 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4551 struct rtnl_af_ops *af_ops;
4552
4553 /* for IFLA_STATS_AF_SPEC */
4554 size += nla_total_size(0);
4555
4556 rcu_read_lock();
4557 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
4558 if (af_ops->get_stats_af_size) {
4559 size += nla_total_size(
4560 af_ops->get_stats_af_size(dev));
4561
4562 /* for AF_* */
4563 size += nla_total_size(0);
4564 }
4565 }
4566 rcu_read_unlock();
4567 }
4568
4569 return size;
4570 }
4571
rtnl_stats_get(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)4572 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4573 struct netlink_ext_ack *extack)
4574 {
4575 struct net *net = sock_net(skb->sk);
4576 struct net_device *dev = NULL;
4577 int idxattr = 0, prividx = 0;
4578 struct if_stats_msg *ifsm;
4579 struct sk_buff *nskb;
4580 u32 filter_mask;
4581 int err;
4582
4583 if (nlmsg_len(nlh) < sizeof(*ifsm))
4584 return -EINVAL;
4585
4586 ifsm = nlmsg_data(nlh);
4587 if (ifsm->ifindex > 0)
4588 dev = __dev_get_by_index(net, ifsm->ifindex);
4589 else
4590 return -EINVAL;
4591
4592 if (!dev)
4593 return -ENODEV;
4594
4595 filter_mask = ifsm->filter_mask;
4596 if (!filter_mask)
4597 return -EINVAL;
4598
4599 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4600 if (!nskb)
4601 return -ENOBUFS;
4602
4603 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4604 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
4605 0, filter_mask, &idxattr, &prividx);
4606 if (err < 0) {
4607 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4608 WARN_ON(err == -EMSGSIZE);
4609 kfree_skb(nskb);
4610 } else {
4611 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4612 }
4613
4614 return err;
4615 }
4616
rtnl_stats_dump(struct sk_buff * skb,struct netlink_callback * cb)4617 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4618 {
4619 int h, s_h, err, s_idx, s_idxattr, s_prividx;
4620 struct net *net = sock_net(skb->sk);
4621 unsigned int flags = NLM_F_MULTI;
4622 struct if_stats_msg *ifsm;
4623 struct hlist_head *head;
4624 struct net_device *dev;
4625 u32 filter_mask = 0;
4626 int idx = 0;
4627
4628 s_h = cb->args[0];
4629 s_idx = cb->args[1];
4630 s_idxattr = cb->args[2];
4631 s_prividx = cb->args[3];
4632
4633 cb->seq = net->dev_base_seq;
4634
4635 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4636 return -EINVAL;
4637
4638 ifsm = nlmsg_data(cb->nlh);
4639 filter_mask = ifsm->filter_mask;
4640 if (!filter_mask)
4641 return -EINVAL;
4642
4643 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4644 idx = 0;
4645 head = &net->dev_index_head[h];
4646 hlist_for_each_entry(dev, head, index_hlist) {
4647 if (idx < s_idx)
4648 goto cont;
4649 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4650 NETLINK_CB(cb->skb).portid,
4651 cb->nlh->nlmsg_seq, 0,
4652 flags, filter_mask,
4653 &s_idxattr, &s_prividx);
4654 /* If we ran out of room on the first message,
4655 * we're in trouble
4656 */
4657 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4658
4659 if (err < 0)
4660 goto out;
4661 s_prividx = 0;
4662 s_idxattr = 0;
4663 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4664 cont:
4665 idx++;
4666 }
4667 }
4668 out:
4669 cb->args[3] = s_prividx;
4670 cb->args[2] = s_idxattr;
4671 cb->args[1] = idx;
4672 cb->args[0] = h;
4673
4674 return skb->len;
4675 }
4676
4677 /* Process one rtnetlink message. */
4678
rtnetlink_rcv_msg(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)4679 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4680 struct netlink_ext_ack *extack)
4681 {
4682 struct net *net = sock_net(skb->sk);
4683 struct rtnl_link *link;
4684 struct module *owner;
4685 int err = -EOPNOTSUPP;
4686 rtnl_doit_func doit;
4687 unsigned int flags;
4688 int kind;
4689 int family;
4690 int type;
4691
4692 type = nlh->nlmsg_type;
4693 if (type > RTM_MAX)
4694 return -EOPNOTSUPP;
4695
4696 type -= RTM_BASE;
4697
4698 /* All the messages must have at least 1 byte length */
4699 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
4700 return 0;
4701
4702 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
4703 kind = type&3;
4704
4705 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
4706 return -EPERM;
4707
4708 rcu_read_lock();
4709 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
4710 struct sock *rtnl;
4711 rtnl_dumpit_func dumpit;
4712 u16 min_dump_alloc = 0;
4713
4714 link = rtnl_get_link(family, type);
4715 if (!link || !link->dumpit) {
4716 family = PF_UNSPEC;
4717 link = rtnl_get_link(family, type);
4718 if (!link || !link->dumpit)
4719 goto err_unlock;
4720 }
4721 owner = link->owner;
4722 dumpit = link->dumpit;
4723
4724 if (type == RTM_GETLINK - RTM_BASE)
4725 min_dump_alloc = rtnl_calcit(skb, nlh);
4726
4727 err = 0;
4728 /* need to do this before rcu_read_unlock() */
4729 if (!try_module_get(owner))
4730 err = -EPROTONOSUPPORT;
4731
4732 rcu_read_unlock();
4733
4734 rtnl = net->rtnl;
4735 if (err == 0) {
4736 struct netlink_dump_control c = {
4737 .dump = dumpit,
4738 .min_dump_alloc = min_dump_alloc,
4739 .module = owner,
4740 };
4741 err = netlink_dump_start(rtnl, skb, nlh, &c);
4742 /* netlink_dump_start() will keep a reference on
4743 * module if dump is still in progress.
4744 */
4745 module_put(owner);
4746 }
4747 return err;
4748 }
4749
4750 link = rtnl_get_link(family, type);
4751 if (!link || !link->doit) {
4752 family = PF_UNSPEC;
4753 link = rtnl_get_link(PF_UNSPEC, type);
4754 if (!link || !link->doit)
4755 goto out_unlock;
4756 }
4757
4758 owner = link->owner;
4759 if (!try_module_get(owner)) {
4760 err = -EPROTONOSUPPORT;
4761 goto out_unlock;
4762 }
4763
4764 flags = link->flags;
4765 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
4766 doit = link->doit;
4767 rcu_read_unlock();
4768 if (doit)
4769 err = doit(skb, nlh, extack);
4770 module_put(owner);
4771 return err;
4772 }
4773 rcu_read_unlock();
4774
4775 rtnl_lock();
4776 link = rtnl_get_link(family, type);
4777 if (link && link->doit)
4778 err = link->doit(skb, nlh, extack);
4779 rtnl_unlock();
4780
4781 module_put(owner);
4782
4783 return err;
4784
4785 out_unlock:
4786 rcu_read_unlock();
4787 return err;
4788
4789 err_unlock:
4790 rcu_read_unlock();
4791 return -EOPNOTSUPP;
4792 }
4793
rtnetlink_rcv(struct sk_buff * skb)4794 static void rtnetlink_rcv(struct sk_buff *skb)
4795 {
4796 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
4797 }
4798
rtnetlink_bind(struct net * net,int group)4799 static int rtnetlink_bind(struct net *net, int group)
4800 {
4801 switch (group) {
4802 case RTNLGRP_IPV4_MROUTE_R:
4803 case RTNLGRP_IPV6_MROUTE_R:
4804 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4805 return -EPERM;
4806 break;
4807 }
4808 return 0;
4809 }
4810
rtnetlink_event(struct notifier_block * this,unsigned long event,void * ptr)4811 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4812 {
4813 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4814
4815 switch (event) {
4816 case NETDEV_REBOOT:
4817 case NETDEV_CHANGEMTU:
4818 case NETDEV_CHANGEADDR:
4819 case NETDEV_CHANGENAME:
4820 case NETDEV_FEAT_CHANGE:
4821 case NETDEV_BONDING_FAILOVER:
4822 case NETDEV_POST_TYPE_CHANGE:
4823 case NETDEV_NOTIFY_PEERS:
4824 case NETDEV_CHANGEUPPER:
4825 case NETDEV_RESEND_IGMP:
4826 case NETDEV_CHANGEINFODATA:
4827 case NETDEV_CHANGELOWERSTATE:
4828 case NETDEV_CHANGE_TX_QUEUE_LEN:
4829 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
4830 GFP_KERNEL, NULL, 0);
4831 break;
4832 default:
4833 break;
4834 }
4835 return NOTIFY_DONE;
4836 }
4837
4838 static struct notifier_block rtnetlink_dev_notifier = {
4839 .notifier_call = rtnetlink_event,
4840 };
4841
4842
rtnetlink_net_init(struct net * net)4843 static int __net_init rtnetlink_net_init(struct net *net)
4844 {
4845 struct sock *sk;
4846 struct netlink_kernel_cfg cfg = {
4847 .groups = RTNLGRP_MAX,
4848 .input = rtnetlink_rcv,
4849 .cb_mutex = &rtnl_mutex,
4850 .flags = NL_CFG_F_NONROOT_RECV,
4851 .bind = rtnetlink_bind,
4852 };
4853
4854 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
4855 if (!sk)
4856 return -ENOMEM;
4857 net->rtnl = sk;
4858 return 0;
4859 }
4860
rtnetlink_net_exit(struct net * net)4861 static void __net_exit rtnetlink_net_exit(struct net *net)
4862 {
4863 netlink_kernel_release(net->rtnl);
4864 net->rtnl = NULL;
4865 }
4866
4867 static struct pernet_operations rtnetlink_net_ops = {
4868 .init = rtnetlink_net_init,
4869 .exit = rtnetlink_net_exit,
4870 };
4871
rtnetlink_init(void)4872 void __init rtnetlink_init(void)
4873 {
4874 if (register_pernet_subsys(&rtnetlink_net_ops))
4875 panic("rtnetlink_init: cannot initialize rtnetlink\n");
4876
4877 register_netdevice_notifier(&rtnetlink_dev_notifier);
4878
4879 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
4880 rtnl_dump_ifinfo, 0);
4881 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4882 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4883 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
4884
4885 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4886 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4887 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
4888
4889 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4890 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4891 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
4892
4893 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4894 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4895 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
4896
4897 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
4898 0);
4899 }
4900