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