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