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