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