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