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