~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/core/rtnetlink.c

Version: ~ [ linux-5.14-rc3 ] ~ [ linux-5.13.5 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.53 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.135 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.198 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.240 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.276 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.276 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 68 void rtnl_lock(void)
 69 {
 70         mutex_lock(&rtnl_mutex);
 71 }
 72 EXPORT_SYMBOL(rtnl_lock);
 73 
 74 void __rtnl_unlock(void)
 75 {
 76         mutex_unlock(&rtnl_mutex);
 77 }
 78 
 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 
 86 int rtnl_trylock(void)
 87 {
 88         return mutex_trylock(&rtnl_mutex);
 89 }
 90 EXPORT_SYMBOL(rtnl_trylock);
 91 
 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
 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 
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 
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 
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 
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  */
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  */
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  */
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  */
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 
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  */
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  */
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 
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  */
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  */
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  */
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 
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 
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 
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  */
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  */
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  */
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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         a->rx_nohandler = b->rx_nohandler;
809 }
810 
811 static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
812 {
813         memcpy(v, b, sizeof(*b));
814 }
815 
816 /* All VF info */
817 static inline int rtnl_vfinfo_size(const struct net_device *dev,
818                                    u32 ext_filter_mask)
819 {
820         if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
821             (ext_filter_mask & RTEXT_FILTER_VF)) {
822                 int num_vfs = dev_num_vf(dev->dev.parent);
823                 size_t size = nla_total_size(sizeof(struct nlattr));
824                 size += nla_total_size(num_vfs * sizeof(struct nlattr));
825                 size += num_vfs *
826                         (nla_total_size(sizeof(struct ifla_vf_mac)) +
827                          nla_total_size(sizeof(struct ifla_vf_vlan)) +
828                          nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
829                          nla_total_size(sizeof(struct ifla_vf_rate)) +
830                          nla_total_size(sizeof(struct ifla_vf_link_state)) +
831                          nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
832                          /* IFLA_VF_STATS_RX_PACKETS */
833                          nla_total_size(sizeof(__u64)) +
834                          /* IFLA_VF_STATS_TX_PACKETS */
835                          nla_total_size(sizeof(__u64)) +
836                          /* IFLA_VF_STATS_RX_BYTES */
837                          nla_total_size(sizeof(__u64)) +
838                          /* IFLA_VF_STATS_TX_BYTES */
839                          nla_total_size(sizeof(__u64)) +
840                          /* IFLA_VF_STATS_BROADCAST */
841                          nla_total_size(sizeof(__u64)) +
842                          /* IFLA_VF_STATS_MULTICAST */
843                          nla_total_size(sizeof(__u64)) +
844                          nla_total_size(sizeof(struct ifla_vf_trust)));
845                 return size;
846         } else
847                 return 0;
848 }
849 
850 static size_t rtnl_port_size(const struct net_device *dev,
851                              u32 ext_filter_mask)
852 {
853         size_t port_size = nla_total_size(4)            /* PORT_VF */
854                 + nla_total_size(PORT_PROFILE_MAX)      /* PORT_PROFILE */
855                 + nla_total_size(sizeof(struct ifla_port_vsi))
856                                                         /* PORT_VSI_TYPE */
857                 + nla_total_size(PORT_UUID_MAX)         /* PORT_INSTANCE_UUID */
858                 + nla_total_size(PORT_UUID_MAX)         /* PORT_HOST_UUID */
859                 + nla_total_size(1)                     /* PROT_VDP_REQUEST */
860                 + nla_total_size(2);                    /* PORT_VDP_RESPONSE */
861         size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
862         size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
863                 + port_size;
864         size_t port_self_size = nla_total_size(sizeof(struct nlattr))
865                 + port_size;
866 
867         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
868             !(ext_filter_mask & RTEXT_FILTER_VF))
869                 return 0;
870         if (dev_num_vf(dev->dev.parent))
871                 return port_self_size + vf_ports_size +
872                         vf_port_size * dev_num_vf(dev->dev.parent);
873         else
874                 return port_self_size;
875 }
876 
877 static noinline size_t if_nlmsg_size(const struct net_device *dev,
878                                      u32 ext_filter_mask)
879 {
880         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
881                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
882                + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
883                + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
884                + nla_total_size(sizeof(struct rtnl_link_ifmap))
885                + nla_total_size(sizeof(struct rtnl_link_stats))
886                + nla_total_size(sizeof(struct rtnl_link_stats64))
887                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
888                + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
889                + nla_total_size(4) /* IFLA_TXQLEN */
890                + nla_total_size(4) /* IFLA_WEIGHT */
891                + nla_total_size(4) /* IFLA_MTU */
892                + nla_total_size(4) /* IFLA_LINK */
893                + nla_total_size(4) /* IFLA_MASTER */
894                + nla_total_size(1) /* IFLA_CARRIER */
895                + nla_total_size(4) /* IFLA_PROMISCUITY */
896                + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
897                + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
898                + nla_total_size(4) /* IFLA_MAX_GSO_SEGS */
899                + nla_total_size(4) /* IFLA_MAX_GSO_SIZE */
900                + nla_total_size(1) /* IFLA_OPERSTATE */
901                + nla_total_size(1) /* IFLA_LINKMODE */
902                + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
903                + nla_total_size(4) /* IFLA_LINK_NETNSID */
904                + nla_total_size(ext_filter_mask
905                                 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
906                + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
907                + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
908                + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
909                + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
910                + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
911                + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
912                + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
913                + nla_total_size(1); /* IFLA_PROTO_DOWN */
914 
915 }
916 
917 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
918 {
919         struct nlattr *vf_ports;
920         struct nlattr *vf_port;
921         int vf;
922         int err;
923 
924         vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
925         if (!vf_ports)
926                 return -EMSGSIZE;
927 
928         for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
929                 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
930                 if (!vf_port)
931                         goto nla_put_failure;
932                 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
933                         goto nla_put_failure;
934                 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
935                 if (err == -EMSGSIZE)
936                         goto nla_put_failure;
937                 if (err) {
938                         nla_nest_cancel(skb, vf_port);
939                         continue;
940                 }
941                 nla_nest_end(skb, vf_port);
942         }
943 
944         nla_nest_end(skb, vf_ports);
945 
946         return 0;
947 
948 nla_put_failure:
949         nla_nest_cancel(skb, vf_ports);
950         return -EMSGSIZE;
951 }
952 
953 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
954 {
955         struct nlattr *port_self;
956         int err;
957 
958         port_self = nla_nest_start(skb, IFLA_PORT_SELF);
959         if (!port_self)
960                 return -EMSGSIZE;
961 
962         err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
963         if (err) {
964                 nla_nest_cancel(skb, port_self);
965                 return (err == -EMSGSIZE) ? err : 0;
966         }
967 
968         nla_nest_end(skb, port_self);
969 
970         return 0;
971 }
972 
973 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
974                           u32 ext_filter_mask)
975 {
976         int err;
977 
978         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
979             !(ext_filter_mask & RTEXT_FILTER_VF))
980                 return 0;
981 
982         err = rtnl_port_self_fill(skb, dev);
983         if (err)
984                 return err;
985 
986         if (dev_num_vf(dev->dev.parent)) {
987                 err = rtnl_vf_ports_fill(skb, dev);
988                 if (err)
989                         return err;
990         }
991 
992         return 0;
993 }
994 
995 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
996 {
997         int err;
998         struct netdev_phys_item_id ppid;
999 
1000         err = dev_get_phys_port_id(dev, &ppid);
1001         if (err) {
1002                 if (err == -EOPNOTSUPP)
1003                         return 0;
1004                 return err;
1005         }
1006 
1007         if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1008                 return -EMSGSIZE;
1009 
1010         return 0;
1011 }
1012 
1013 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1014 {
1015         char name[IFNAMSIZ];
1016         int err;
1017 
1018         err = dev_get_phys_port_name(dev, name, sizeof(name));
1019         if (err) {
1020                 if (err == -EOPNOTSUPP)
1021                         return 0;
1022                 return err;
1023         }
1024 
1025         if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
1026                 return -EMSGSIZE;
1027 
1028         return 0;
1029 }
1030 
1031 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1032 {
1033         int err;
1034         struct switchdev_attr attr = {
1035                 .orig_dev = dev,
1036                 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1037                 .flags = SWITCHDEV_F_NO_RECURSE,
1038         };
1039 
1040         err = switchdev_port_attr_get(dev, &attr);
1041         if (err) {
1042                 if (err == -EOPNOTSUPP)
1043                         return 0;
1044                 return err;
1045         }
1046 
1047         if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1048                     attr.u.ppid.id))
1049                 return -EMSGSIZE;
1050 
1051         return 0;
1052 }
1053 
1054 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1055                                               struct net_device *dev)
1056 {
1057         const struct rtnl_link_stats64 *stats;
1058         struct rtnl_link_stats64 temp;
1059         struct nlattr *attr;
1060 
1061         stats = dev_get_stats(dev, &temp);
1062 
1063         attr = nla_reserve(skb, IFLA_STATS,
1064                            sizeof(struct rtnl_link_stats));
1065         if (!attr)
1066                 return -EMSGSIZE;
1067 
1068         copy_rtnl_link_stats(nla_data(attr), stats);
1069 
1070         attr = nla_reserve(skb, IFLA_STATS64,
1071                            sizeof(struct rtnl_link_stats64));
1072         if (!attr)
1073                 return -EMSGSIZE;
1074 
1075         copy_rtnl_link_stats64(nla_data(attr), stats);
1076 
1077         return 0;
1078 }
1079 
1080 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1081                                                struct net_device *dev,
1082                                                int vfs_num,
1083                                                struct nlattr *vfinfo)
1084 {
1085         struct ifla_vf_rss_query_en vf_rss_query_en;
1086         struct ifla_vf_link_state vf_linkstate;
1087         struct ifla_vf_spoofchk vf_spoofchk;
1088         struct ifla_vf_tx_rate vf_tx_rate;
1089         struct ifla_vf_stats vf_stats;
1090         struct ifla_vf_trust vf_trust;
1091         struct ifla_vf_vlan vf_vlan;
1092         struct ifla_vf_rate vf_rate;
1093         struct nlattr *vf, *vfstats;
1094         struct ifla_vf_mac vf_mac;
1095         struct ifla_vf_info ivi;
1096 
1097         /* Not all SR-IOV capable drivers support the
1098          * spoofcheck and "RSS query enable" query.  Preset to
1099          * -1 so the user space tool can detect that the driver
1100          * didn't report anything.
1101          */
1102         ivi.spoofchk = -1;
1103         ivi.rss_query_en = -1;
1104         ivi.trusted = -1;
1105         memset(ivi.mac, 0, sizeof(ivi.mac));
1106         /* The default value for VF link state is "auto"
1107          * IFLA_VF_LINK_STATE_AUTO which equals zero
1108          */
1109         ivi.linkstate = 0;
1110         if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1111                 return 0;
1112 
1113         vf_mac.vf =
1114                 vf_vlan.vf =
1115                 vf_rate.vf =
1116                 vf_tx_rate.vf =
1117                 vf_spoofchk.vf =
1118                 vf_linkstate.vf =
1119                 vf_rss_query_en.vf =
1120                 vf_trust.vf = ivi.vf;
1121 
1122         memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1123         vf_vlan.vlan = ivi.vlan;
1124         vf_vlan.qos = ivi.qos;
1125         vf_tx_rate.rate = ivi.max_tx_rate;
1126         vf_rate.min_tx_rate = ivi.min_tx_rate;
1127         vf_rate.max_tx_rate = ivi.max_tx_rate;
1128         vf_spoofchk.setting = ivi.spoofchk;
1129         vf_linkstate.link_state = ivi.linkstate;
1130         vf_rss_query_en.setting = ivi.rss_query_en;
1131         vf_trust.setting = ivi.trusted;
1132         vf = nla_nest_start(skb, IFLA_VF_INFO);
1133         if (!vf) {
1134                 nla_nest_cancel(skb, vfinfo);
1135                 return -EMSGSIZE;
1136         }
1137         if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1138             nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1139             nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1140                     &vf_rate) ||
1141             nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1142                     &vf_tx_rate) ||
1143             nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1144                     &vf_spoofchk) ||
1145             nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1146                     &vf_linkstate) ||
1147             nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1148                     sizeof(vf_rss_query_en),
1149                     &vf_rss_query_en) ||
1150             nla_put(skb, IFLA_VF_TRUST,
1151                     sizeof(vf_trust), &vf_trust))
1152                 return -EMSGSIZE;
1153         memset(&vf_stats, 0, sizeof(vf_stats));
1154         if (dev->netdev_ops->ndo_get_vf_stats)
1155                 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1156                                                 &vf_stats);
1157         vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1158         if (!vfstats) {
1159                 nla_nest_cancel(skb, vf);
1160                 nla_nest_cancel(skb, vfinfo);
1161                 return -EMSGSIZE;
1162         }
1163         if (nla_put_u64(skb, IFLA_VF_STATS_RX_PACKETS,
1164                         vf_stats.rx_packets) ||
1165             nla_put_u64(skb, IFLA_VF_STATS_TX_PACKETS,
1166                         vf_stats.tx_packets) ||
1167             nla_put_u64(skb, IFLA_VF_STATS_RX_BYTES,
1168                         vf_stats.rx_bytes) ||
1169             nla_put_u64(skb, IFLA_VF_STATS_TX_BYTES,
1170                         vf_stats.tx_bytes) ||
1171             nla_put_u64(skb, IFLA_VF_STATS_BROADCAST,
1172                         vf_stats.broadcast) ||
1173             nla_put_u64(skb, IFLA_VF_STATS_MULTICAST,
1174                         vf_stats.multicast))
1175                 return -EMSGSIZE;
1176         nla_nest_end(skb, vfstats);
1177         nla_nest_end(skb, vf);
1178         return 0;
1179 }
1180 
1181 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1182 {
1183         struct rtnl_link_ifmap map;
1184 
1185         memset(&map, 0, sizeof(map));
1186         map.mem_start   = dev->mem_start;
1187         map.mem_end     = dev->mem_end;
1188         map.base_addr   = dev->base_addr;
1189         map.irq         = dev->irq;
1190         map.dma         = dev->dma;
1191         map.port        = dev->if_port;
1192 
1193         if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1194                 return -EMSGSIZE;
1195 
1196         return 0;
1197 }
1198 
1199 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1200                             int type, u32 pid, u32 seq, u32 change,
1201                             unsigned int flags, u32 ext_filter_mask)
1202 {
1203         struct ifinfomsg *ifm;
1204         struct nlmsghdr *nlh;
1205         struct nlattr *af_spec;
1206         struct rtnl_af_ops *af_ops;
1207         struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1208 
1209         ASSERT_RTNL();
1210         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1211         if (nlh == NULL)
1212                 return -EMSGSIZE;
1213 
1214         ifm = nlmsg_data(nlh);
1215         ifm->ifi_family = AF_UNSPEC;
1216         ifm->__ifi_pad = 0;
1217         ifm->ifi_type = dev->type;
1218         ifm->ifi_index = dev->ifindex;
1219         ifm->ifi_flags = dev_get_flags(dev);
1220         ifm->ifi_change = change;
1221 
1222         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1223             nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1224             nla_put_u8(skb, IFLA_OPERSTATE,
1225                        netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1226             nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1227             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1228             nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1229             nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1230             nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1231             nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1232             nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1233 #ifdef CONFIG_RPS
1234             nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1235 #endif
1236             (dev->ifindex != dev_get_iflink(dev) &&
1237              nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
1238             (upper_dev &&
1239              nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
1240             nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1241             (dev->qdisc &&
1242              nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1243             (dev->ifalias &&
1244              nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1245             nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1246                         atomic_read(&dev->carrier_changes)) ||
1247             nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1248                 goto nla_put_failure;
1249 
1250         if (rtnl_fill_link_ifmap(skb, dev))
1251                 goto nla_put_failure;
1252 
1253         if (dev->addr_len) {
1254                 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1255                     nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1256                         goto nla_put_failure;
1257         }
1258 
1259         if (rtnl_phys_port_id_fill(skb, dev))
1260                 goto nla_put_failure;
1261 
1262         if (rtnl_phys_port_name_fill(skb, dev))
1263                 goto nla_put_failure;
1264 
1265         if (rtnl_phys_switch_id_fill(skb, dev))
1266                 goto nla_put_failure;
1267 
1268         if (rtnl_fill_stats(skb, dev))
1269                 goto nla_put_failure;
1270 
1271         if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1272             nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1273                 goto nla_put_failure;
1274 
1275         if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1276             ext_filter_mask & RTEXT_FILTER_VF) {
1277                 int i;
1278                 struct nlattr *vfinfo;
1279                 int num_vfs = dev_num_vf(dev->dev.parent);
1280 
1281                 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1282                 if (!vfinfo)
1283                         goto nla_put_failure;
1284                 for (i = 0; i < num_vfs; i++) {
1285                         if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1286                                 goto nla_put_failure;
1287                 }
1288 
1289                 nla_nest_end(skb, vfinfo);
1290         }
1291 
1292         if (rtnl_port_fill(skb, dev, ext_filter_mask))
1293                 goto nla_put_failure;
1294 
1295         if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1296                 if (rtnl_link_fill(skb, dev) < 0)
1297                         goto nla_put_failure;
1298         }
1299 
1300         if (dev->rtnl_link_ops &&
1301             dev->rtnl_link_ops->get_link_net) {
1302                 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1303 
1304                 if (!net_eq(dev_net(dev), link_net)) {
1305                         int id = peernet2id_alloc(dev_net(dev), link_net);
1306 
1307                         if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1308                                 goto nla_put_failure;
1309                 }
1310         }
1311 
1312         if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1313                 goto nla_put_failure;
1314 
1315         list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1316                 if (af_ops->fill_link_af) {
1317                         struct nlattr *af;
1318                         int err;
1319 
1320                         if (!(af = nla_nest_start(skb, af_ops->family)))
1321                                 goto nla_put_failure;
1322 
1323                         err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1324 
1325                         /*
1326                          * Caller may return ENODATA to indicate that there
1327                          * was no data to be dumped. This is not an error, it
1328                          * means we should trim the attribute header and
1329                          * continue.
1330                          */
1331                         if (err == -ENODATA)
1332                                 nla_nest_cancel(skb, af);
1333                         else if (err < 0)
1334                                 goto nla_put_failure;
1335 
1336                         nla_nest_end(skb, af);
1337                 }
1338         }
1339 
1340         nla_nest_end(skb, af_spec);
1341 
1342         nlmsg_end(skb, nlh);
1343         return 0;
1344 
1345 nla_put_failure:
1346         nlmsg_cancel(skb, nlh);
1347         return -EMSGSIZE;
1348 }
1349 
1350 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1351         [IFLA_IFNAME]           = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1352         [IFLA_ADDRESS]          = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1353         [IFLA_BROADCAST]        = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1354         [IFLA_MAP]              = { .len = sizeof(struct rtnl_link_ifmap) },
1355         [IFLA_MTU]              = { .type = NLA_U32 },
1356         [IFLA_LINK]             = { .type = NLA_U32 },
1357         [IFLA_MASTER]           = { .type = NLA_U32 },
1358         [IFLA_CARRIER]          = { .type = NLA_U8 },
1359         [IFLA_TXQLEN]           = { .type = NLA_U32 },
1360         [IFLA_WEIGHT]           = { .type = NLA_U32 },
1361         [IFLA_OPERSTATE]        = { .type = NLA_U8 },
1362         [IFLA_LINKMODE]         = { .type = NLA_U8 },
1363         [IFLA_LINKINFO]         = { .type = NLA_NESTED },
1364         [IFLA_NET_NS_PID]       = { .type = NLA_U32 },
1365         [IFLA_NET_NS_FD]        = { .type = NLA_U32 },
1366         [IFLA_IFALIAS]          = { .type = NLA_STRING, .len = IFALIASZ-1 },
1367         [IFLA_VFINFO_LIST]      = {. type = NLA_NESTED },
1368         [IFLA_VF_PORTS]         = { .type = NLA_NESTED },
1369         [IFLA_PORT_SELF]        = { .type = NLA_NESTED },
1370         [IFLA_AF_SPEC]          = { .type = NLA_NESTED },
1371         [IFLA_EXT_MASK]         = { .type = NLA_U32 },
1372         [IFLA_PROMISCUITY]      = { .type = NLA_U32 },
1373         [IFLA_NUM_TX_QUEUES]    = { .type = NLA_U32 },
1374         [IFLA_NUM_RX_QUEUES]    = { .type = NLA_U32 },
1375         [IFLA_PHYS_PORT_ID]     = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1376         [IFLA_CARRIER_CHANGES]  = { .type = NLA_U32 },  /* ignored */
1377         [IFLA_PHYS_SWITCH_ID]   = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1378         [IFLA_LINK_NETNSID]     = { .type = NLA_S32 },
1379         [IFLA_PROTO_DOWN]       = { .type = NLA_U8 },
1380 };
1381 
1382 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1383         [IFLA_INFO_KIND]        = { .type = NLA_STRING },
1384         [IFLA_INFO_DATA]        = { .type = NLA_NESTED },
1385         [IFLA_INFO_SLAVE_KIND]  = { .type = NLA_STRING },
1386         [IFLA_INFO_SLAVE_DATA]  = { .type = NLA_NESTED },
1387 };
1388 
1389 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1390         [IFLA_VF_MAC]           = { .len = sizeof(struct ifla_vf_mac) },
1391         [IFLA_VF_VLAN]          = { .len = sizeof(struct ifla_vf_vlan) },
1392         [IFLA_VF_TX_RATE]       = { .len = sizeof(struct ifla_vf_tx_rate) },
1393         [IFLA_VF_SPOOFCHK]      = { .len = sizeof(struct ifla_vf_spoofchk) },
1394         [IFLA_VF_RATE]          = { .len = sizeof(struct ifla_vf_rate) },
1395         [IFLA_VF_LINK_STATE]    = { .len = sizeof(struct ifla_vf_link_state) },
1396         [IFLA_VF_RSS_QUERY_EN]  = { .len = sizeof(struct ifla_vf_rss_query_en) },
1397         [IFLA_VF_STATS]         = { .type = NLA_NESTED },
1398         [IFLA_VF_TRUST]         = { .len = sizeof(struct ifla_vf_trust) },
1399         [IFLA_VF_IB_NODE_GUID]  = { .len = sizeof(struct ifla_vf_guid) },
1400         [IFLA_VF_IB_PORT_GUID]  = { .len = sizeof(struct ifla_vf_guid) },
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 
1417 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1418 {
1419         const struct rtnl_link_ops *ops = NULL;
1420         struct nlattr *linfo[IFLA_INFO_MAX + 1];
1421 
1422         if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, ifla_info_policy) < 0)
1423                 return NULL;
1424 
1425         if (linfo[IFLA_INFO_KIND]) {
1426                 char kind[MODULE_NAME_LEN];
1427 
1428                 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1429                 ops = rtnl_link_ops_get(kind);
1430         }
1431 
1432         return ops;
1433 }
1434 
1435 static bool link_master_filtered(struct net_device *dev, int master_idx)
1436 {
1437         struct net_device *master;
1438 
1439         if (!master_idx)
1440                 return false;
1441 
1442         master = netdev_master_upper_dev_get(dev);
1443         if (!master || master->ifindex != master_idx)
1444                 return true;
1445 
1446         return false;
1447 }
1448 
1449 static bool link_kind_filtered(const struct net_device *dev,
1450                                const struct rtnl_link_ops *kind_ops)
1451 {
1452         if (kind_ops && dev->rtnl_link_ops != kind_ops)
1453                 return true;
1454 
1455         return false;
1456 }
1457 
1458 static bool link_dump_filtered(struct net_device *dev,
1459                                int master_idx,
1460                                const struct rtnl_link_ops *kind_ops)
1461 {
1462         if (link_master_filtered(dev, master_idx) ||
1463             link_kind_filtered(dev, kind_ops))
1464                 return true;
1465 
1466         return false;
1467 }
1468 
1469 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1470 {
1471         struct net *net = sock_net(skb->sk);
1472         int h, s_h;
1473         int idx = 0, s_idx;
1474         struct net_device *dev;
1475         struct hlist_head *head;
1476         struct nlattr *tb[IFLA_MAX+1];
1477         u32 ext_filter_mask = 0;
1478         const struct rtnl_link_ops *kind_ops = NULL;
1479         unsigned int flags = NLM_F_MULTI;
1480         int master_idx = 0;
1481         int err;
1482         int hdrlen;
1483 
1484         s_h = cb->args[0];
1485         s_idx = cb->args[1];
1486 
1487         cb->seq = net->dev_base_seq;
1488 
1489         /* A hack to preserve kernel<->userspace interface.
1490          * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1491          * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1492          * what iproute2 < v3.9.0 used.
1493          * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1494          * attribute, its netlink message is shorter than struct ifinfomsg.
1495          */
1496         hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1497                  sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1498 
1499         if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1500 
1501                 if (tb[IFLA_EXT_MASK])
1502                         ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1503 
1504                 if (tb[IFLA_MASTER])
1505                         master_idx = nla_get_u32(tb[IFLA_MASTER]);
1506 
1507                 if (tb[IFLA_LINKINFO])
1508                         kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1509 
1510                 if (master_idx || kind_ops)
1511                         flags |= NLM_F_DUMP_FILTERED;
1512         }
1513 
1514         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1515                 idx = 0;
1516                 head = &net->dev_index_head[h];
1517                 hlist_for_each_entry(dev, head, index_hlist) {
1518                         if (link_dump_filtered(dev, master_idx, kind_ops))
1519                                 continue;
1520                         if (idx < s_idx)
1521                                 goto cont;
1522                         err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1523                                                NETLINK_CB(cb->skb).portid,
1524                                                cb->nlh->nlmsg_seq, 0,
1525                                                flags,
1526                                                ext_filter_mask);
1527                         /* If we ran out of room on the first message,
1528                          * we're in trouble
1529                          */
1530                         WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1531 
1532                         if (err < 0)
1533                                 goto out;
1534 
1535                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1536 cont:
1537                         idx++;
1538                 }
1539         }
1540 out:
1541         cb->args[1] = idx;
1542         cb->args[0] = h;
1543 
1544         return skb->len;
1545 }
1546 
1547 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1548 {
1549         return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1550 }
1551 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1552 
1553 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1554 {
1555         struct net *net;
1556         /* Examine the link attributes and figure out which
1557          * network namespace we are talking about.
1558          */
1559         if (tb[IFLA_NET_NS_PID])
1560                 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1561         else if (tb[IFLA_NET_NS_FD])
1562                 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1563         else
1564                 net = get_net(src_net);
1565         return net;
1566 }
1567 EXPORT_SYMBOL(rtnl_link_get_net);
1568 
1569 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1570 {
1571         if (dev) {
1572                 if (tb[IFLA_ADDRESS] &&
1573                     nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1574                         return -EINVAL;
1575 
1576                 if (tb[IFLA_BROADCAST] &&
1577                     nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1578                         return -EINVAL;
1579         }
1580 
1581         if (tb[IFLA_AF_SPEC]) {
1582                 struct nlattr *af;
1583                 int rem, err;
1584 
1585                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1586                         const struct rtnl_af_ops *af_ops;
1587 
1588                         if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1589                                 return -EAFNOSUPPORT;
1590 
1591                         if (!af_ops->set_link_af)
1592                                 return -EOPNOTSUPP;
1593 
1594                         if (af_ops->validate_link_af) {
1595                                 err = af_ops->validate_link_af(dev, af);
1596                                 if (err < 0)
1597                                         return err;
1598                         }
1599                 }
1600         }
1601 
1602         return 0;
1603 }
1604 
1605 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1606                                   int guid_type)
1607 {
1608         const struct net_device_ops *ops = dev->netdev_ops;
1609 
1610         return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1611 }
1612 
1613 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1614 {
1615         if (dev->type != ARPHRD_INFINIBAND)
1616                 return -EOPNOTSUPP;
1617 
1618         return handle_infiniband_guid(dev, ivt, guid_type);
1619 }
1620 
1621 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
1622 {
1623         const struct net_device_ops *ops = dev->netdev_ops;
1624         int err = -EINVAL;
1625 
1626         if (tb[IFLA_VF_MAC]) {
1627                 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
1628 
1629                 err = -EOPNOTSUPP;
1630                 if (ops->ndo_set_vf_mac)
1631                         err = ops->ndo_set_vf_mac(dev, ivm->vf,
1632                                                   ivm->mac);
1633                 if (err < 0)
1634                         return err;
1635         }
1636 
1637         if (tb[IFLA_VF_VLAN]) {
1638                 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1639 
1640                 err = -EOPNOTSUPP;
1641                 if (ops->ndo_set_vf_vlan)
1642                         err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1643                                                    ivv->qos);
1644                 if (err < 0)
1645                         return err;
1646         }
1647 
1648         if (tb[IFLA_VF_TX_RATE]) {
1649                 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1650                 struct ifla_vf_info ivf;
1651 
1652                 err = -EOPNOTSUPP;
1653                 if (ops->ndo_get_vf_config)
1654                         err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1655                 if (err < 0)
1656                         return err;
1657 
1658                 err = -EOPNOTSUPP;
1659                 if (ops->ndo_set_vf_rate)
1660                         err = ops->ndo_set_vf_rate(dev, ivt->vf,
1661                                                    ivf.min_tx_rate,
1662                                                    ivt->rate);
1663                 if (err < 0)
1664                         return err;
1665         }
1666 
1667         if (tb[IFLA_VF_RATE]) {
1668                 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1669 
1670                 err = -EOPNOTSUPP;
1671                 if (ops->ndo_set_vf_rate)
1672                         err = ops->ndo_set_vf_rate(dev, ivt->vf,
1673                                                    ivt->min_tx_rate,
1674                                                    ivt->max_tx_rate);
1675                 if (err < 0)
1676                         return err;
1677         }
1678 
1679         if (tb[IFLA_VF_SPOOFCHK]) {
1680                 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1681 
1682                 err = -EOPNOTSUPP;
1683                 if (ops->ndo_set_vf_spoofchk)
1684                         err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1685                                                        ivs->setting);
1686                 if (err < 0)
1687                         return err;
1688         }
1689 
1690         if (tb[IFLA_VF_LINK_STATE]) {
1691                 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1692 
1693                 err = -EOPNOTSUPP;
1694                 if (ops->ndo_set_vf_link_state)
1695                         err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1696                                                          ivl->link_state);
1697                 if (err < 0)
1698                         return err;
1699         }
1700 
1701         if (tb[IFLA_VF_RSS_QUERY_EN]) {
1702                 struct ifla_vf_rss_query_en *ivrssq_en;
1703 
1704                 err = -EOPNOTSUPP;
1705                 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1706                 if (ops->ndo_set_vf_rss_query_en)
1707                         err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1708                                                            ivrssq_en->setting);
1709                 if (err < 0)
1710                         return err;
1711         }
1712 
1713         if (tb[IFLA_VF_TRUST]) {
1714                 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1715 
1716                 err = -EOPNOTSUPP;
1717                 if (ops->ndo_set_vf_trust)
1718                         err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1719                 if (err < 0)
1720                         return err;
1721         }
1722 
1723         if (tb[IFLA_VF_IB_NODE_GUID]) {
1724                 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1725 
1726                 if (!ops->ndo_set_vf_guid)
1727                         return -EOPNOTSUPP;
1728 
1729                 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1730         }
1731 
1732         if (tb[IFLA_VF_IB_PORT_GUID]) {
1733                 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1734 
1735                 if (!ops->ndo_set_vf_guid)
1736                         return -EOPNOTSUPP;
1737 
1738                 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1739         }
1740 
1741         return err;
1742 }
1743 
1744 static int do_set_master(struct net_device *dev, int ifindex)
1745 {
1746         struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1747         const struct net_device_ops *ops;
1748         int err;
1749 
1750         if (upper_dev) {
1751                 if (upper_dev->ifindex == ifindex)
1752                         return 0;
1753                 ops = upper_dev->netdev_ops;
1754                 if (ops->ndo_del_slave) {
1755                         err = ops->ndo_del_slave(upper_dev, dev);
1756                         if (err)
1757                                 return err;
1758                 } else {
1759                         return -EOPNOTSUPP;
1760                 }
1761         }
1762 
1763         if (ifindex) {
1764                 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1765                 if (!upper_dev)
1766                         return -EINVAL;
1767                 ops = upper_dev->netdev_ops;
1768                 if (ops->ndo_add_slave) {
1769                         err = ops->ndo_add_slave(upper_dev, dev);
1770                         if (err)
1771                                 return err;
1772                 } else {
1773                         return -EOPNOTSUPP;
1774                 }
1775         }
1776         return 0;
1777 }
1778 
1779 #define DO_SETLINK_MODIFIED     0x01
1780 /* notify flag means notify + modified. */
1781 #define DO_SETLINK_NOTIFY       0x03
1782 static int do_setlink(const struct sk_buff *skb,
1783                       struct net_device *dev, struct ifinfomsg *ifm,
1784                       struct nlattr **tb, char *ifname, int status)
1785 {
1786         const struct net_device_ops *ops = dev->netdev_ops;
1787         int err;
1788 
1789         if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1790                 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1791                 if (IS_ERR(net)) {
1792                         err = PTR_ERR(net);
1793                         goto errout;
1794                 }
1795                 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1796                         put_net(net);
1797                         err = -EPERM;
1798                         goto errout;
1799                 }
1800                 err = dev_change_net_namespace(dev, net, ifname);
1801                 put_net(net);
1802                 if (err)
1803                         goto errout;
1804                 status |= DO_SETLINK_MODIFIED;
1805         }
1806 
1807         if (tb[IFLA_MAP]) {
1808                 struct rtnl_link_ifmap *u_map;
1809                 struct ifmap k_map;
1810 
1811                 if (!ops->ndo_set_config) {
1812                         err = -EOPNOTSUPP;
1813                         goto errout;
1814                 }
1815 
1816                 if (!netif_device_present(dev)) {
1817                         err = -ENODEV;
1818                         goto errout;
1819                 }
1820 
1821                 u_map = nla_data(tb[IFLA_MAP]);
1822                 k_map.mem_start = (unsigned long) u_map->mem_start;
1823                 k_map.mem_end = (unsigned long) u_map->mem_end;
1824                 k_map.base_addr = (unsigned short) u_map->base_addr;
1825                 k_map.irq = (unsigned char) u_map->irq;
1826                 k_map.dma = (unsigned char) u_map->dma;
1827                 k_map.port = (unsigned char) u_map->port;
1828 
1829                 err = ops->ndo_set_config(dev, &k_map);
1830                 if (err < 0)
1831                         goto errout;
1832 
1833                 status |= DO_SETLINK_NOTIFY;
1834         }
1835 
1836         if (tb[IFLA_ADDRESS]) {
1837                 struct sockaddr *sa;
1838                 int len;
1839 
1840                 len = sizeof(sa_family_t) + dev->addr_len;
1841                 sa = kmalloc(len, GFP_KERNEL);
1842                 if (!sa) {
1843                         err = -ENOMEM;
1844                         goto errout;
1845                 }
1846                 sa->sa_family = dev->type;
1847                 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1848                        dev->addr_len);
1849                 err = dev_set_mac_address(dev, sa);
1850                 kfree(sa);
1851                 if (err)
1852                         goto errout;
1853                 status |= DO_SETLINK_MODIFIED;
1854         }
1855 
1856         if (tb[IFLA_MTU]) {
1857                 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1858                 if (err < 0)
1859                         goto errout;
1860                 status |= DO_SETLINK_MODIFIED;
1861         }
1862 
1863         if (tb[IFLA_GROUP]) {
1864                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1865                 status |= DO_SETLINK_NOTIFY;
1866         }
1867 
1868         /*
1869          * Interface selected by interface index but interface
1870          * name provided implies that a name change has been
1871          * requested.
1872          */
1873         if (ifm->ifi_index > 0 && ifname[0]) {
1874                 err = dev_change_name(dev, ifname);
1875                 if (err < 0)
1876                         goto errout;
1877                 status |= DO_SETLINK_MODIFIED;
1878         }
1879 
1880         if (tb[IFLA_IFALIAS]) {
1881                 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1882                                     nla_len(tb[IFLA_IFALIAS]));
1883                 if (err < 0)
1884                         goto errout;
1885                 status |= DO_SETLINK_NOTIFY;
1886         }
1887 
1888         if (tb[IFLA_BROADCAST]) {
1889                 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1890                 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1891         }
1892 
1893         if (ifm->ifi_flags || ifm->ifi_change) {
1894                 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1895                 if (err < 0)
1896                         goto errout;
1897         }
1898 
1899         if (tb[IFLA_MASTER]) {
1900                 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1901                 if (err)
1902                         goto errout;
1903                 status |= DO_SETLINK_MODIFIED;
1904         }
1905 
1906         if (tb[IFLA_CARRIER]) {
1907                 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1908                 if (err)
1909                         goto errout;
1910                 status |= DO_SETLINK_MODIFIED;
1911         }
1912 
1913         if (tb[IFLA_TXQLEN]) {
1914                 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1915 
1916                 if (dev->tx_queue_len ^ value)
1917                         status |= DO_SETLINK_NOTIFY;
1918 
1919                 dev->tx_queue_len = value;
1920         }
1921 
1922         if (tb[IFLA_OPERSTATE])
1923                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1924 
1925         if (tb[IFLA_LINKMODE]) {
1926                 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1927 
1928                 write_lock_bh(&dev_base_lock);
1929                 if (dev->link_mode ^ value)
1930                         status |= DO_SETLINK_NOTIFY;
1931                 dev->link_mode = value;
1932                 write_unlock_bh(&dev_base_lock);
1933         }
1934 
1935         if (tb[IFLA_VFINFO_LIST]) {
1936                 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
1937                 struct nlattr *attr;
1938                 int rem;
1939 
1940                 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1941                         if (nla_type(attr) != IFLA_VF_INFO ||
1942                             nla_len(attr) < NLA_HDRLEN) {
1943                                 err = -EINVAL;
1944                                 goto errout;
1945                         }
1946                         err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
1947                                                ifla_vf_policy);
1948                         if (err < 0)
1949                                 goto errout;
1950                         err = do_setvfinfo(dev, vfinfo);
1951                         if (err < 0)
1952                                 goto errout;
1953                         status |= DO_SETLINK_NOTIFY;
1954                 }
1955         }
1956         err = 0;
1957 
1958         if (tb[IFLA_VF_PORTS]) {
1959                 struct nlattr *port[IFLA_PORT_MAX+1];
1960                 struct nlattr *attr;
1961                 int vf;
1962                 int rem;
1963 
1964                 err = -EOPNOTSUPP;
1965                 if (!ops->ndo_set_vf_port)
1966                         goto errout;
1967 
1968                 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1969                         if (nla_type(attr) != IFLA_VF_PORT ||
1970                             nla_len(attr) < NLA_HDRLEN) {
1971                                 err = -EINVAL;
1972                                 goto errout;
1973                         }
1974                         err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
1975                                                ifla_port_policy);
1976                         if (err < 0)
1977                                 goto errout;
1978                         if (!port[IFLA_PORT_VF]) {
1979                                 err = -EOPNOTSUPP;
1980                                 goto errout;
1981                         }
1982                         vf = nla_get_u32(port[IFLA_PORT_VF]);
1983                         err = ops->ndo_set_vf_port(dev, vf, port);
1984                         if (err < 0)
1985                                 goto errout;
1986                         status |= DO_SETLINK_NOTIFY;
1987                 }
1988         }
1989         err = 0;
1990 
1991         if (tb[IFLA_PORT_SELF]) {
1992                 struct nlattr *port[IFLA_PORT_MAX+1];
1993 
1994                 err = nla_parse_nested(port, IFLA_PORT_MAX,
1995                         tb[IFLA_PORT_SELF], ifla_port_policy);
1996                 if (err < 0)
1997                         goto errout;
1998 
1999                 err = -EOPNOTSUPP;
2000                 if (ops->ndo_set_vf_port)
2001                         err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2002                 if (err < 0)
2003                         goto errout;
2004                 status |= DO_SETLINK_NOTIFY;
2005         }
2006 
2007         if (tb[IFLA_AF_SPEC]) {
2008                 struct nlattr *af;
2009                 int rem;
2010 
2011                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2012                         const struct rtnl_af_ops *af_ops;
2013 
2014                         if (!(af_ops = rtnl_af_lookup(nla_type(af))))
2015                                 BUG();
2016 
2017                         err = af_ops->set_link_af(dev, af);
2018                         if (err < 0)
2019                                 goto errout;
2020 
2021                         status |= DO_SETLINK_NOTIFY;
2022                 }
2023         }
2024         err = 0;
2025 
2026         if (tb[IFLA_PROTO_DOWN]) {
2027                 err = dev_change_proto_down(dev,
2028                                             nla_get_u8(tb[IFLA_PROTO_DOWN]));
2029                 if (err)
2030                         goto errout;
2031                 status |= DO_SETLINK_NOTIFY;
2032         }
2033 
2034 errout:
2035         if (status & DO_SETLINK_MODIFIED) {
2036                 if (status & DO_SETLINK_NOTIFY)
2037                         netdev_state_change(dev);
2038 
2039                 if (err < 0)
2040                         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",
2041                                              dev->name);
2042         }
2043 
2044         return err;
2045 }
2046 
2047 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2048 {
2049         struct net *net = sock_net(skb->sk);
2050         struct ifinfomsg *ifm;
2051         struct net_device *dev;
2052         int err;
2053         struct nlattr *tb[IFLA_MAX+1];
2054         char ifname[IFNAMSIZ];
2055 
2056         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2057         if (err < 0)
2058                 goto errout;
2059 
2060         if (tb[IFLA_IFNAME])
2061                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2062         else
2063                 ifname[0] = '\0';
2064 
2065         err = -EINVAL;
2066         ifm = nlmsg_data(nlh);
2067         if (ifm->ifi_index > 0)
2068                 dev = __dev_get_by_index(net, ifm->ifi_index);
2069         else if (tb[IFLA_IFNAME])
2070                 dev = __dev_get_by_name(net, ifname);
2071         else
2072                 goto errout;
2073 
2074         if (dev == NULL) {
2075                 err = -ENODEV;
2076                 goto errout;
2077         }
2078 
2079         err = validate_linkmsg(dev, tb);
2080         if (err < 0)
2081                 goto errout;
2082 
2083         err = do_setlink(skb, dev, ifm, tb, ifname, 0);
2084 errout:
2085         return err;
2086 }
2087 
2088 static int rtnl_group_dellink(const struct net *net, int group)
2089 {
2090         struct net_device *dev, *aux;
2091         LIST_HEAD(list_kill);
2092         bool found = false;
2093 
2094         if (!group)
2095                 return -EPERM;
2096 
2097         for_each_netdev(net, dev) {
2098                 if (dev->group == group) {
2099                         const struct rtnl_link_ops *ops;
2100 
2101                         found = true;
2102                         ops = dev->rtnl_link_ops;
2103                         if (!ops || !ops->dellink)
2104                                 return -EOPNOTSUPP;
2105                 }
2106         }
2107 
2108         if (!found)
2109                 return -ENODEV;
2110 
2111         for_each_netdev_safe(net, dev, aux) {
2112                 if (dev->group == group) {
2113                         const struct rtnl_link_ops *ops;
2114 
2115                         ops = dev->rtnl_link_ops;
2116                         ops->dellink(dev, &list_kill);
2117                 }
2118         }
2119         unregister_netdevice_many(&list_kill);
2120 
2121         return 0;
2122 }
2123 
2124 int rtnl_delete_link(struct net_device *dev)
2125 {
2126         const struct rtnl_link_ops *ops;
2127         LIST_HEAD(list_kill);
2128 
2129         ops = dev->rtnl_link_ops;
2130         if (!ops || !ops->dellink)
2131                 return -EOPNOTSUPP;
2132 
2133         ops->dellink(dev, &list_kill);
2134         unregister_netdevice_many(&list_kill);
2135 
2136         return 0;
2137 }
2138 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2139 
2140 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2141 {
2142         struct net *net = sock_net(skb->sk);
2143         struct net_device *dev;
2144         struct ifinfomsg *ifm;
2145         char ifname[IFNAMSIZ];
2146         struct nlattr *tb[IFLA_MAX+1];
2147         int err;
2148 
2149         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2150         if (err < 0)
2151                 return err;
2152 
2153         if (tb[IFLA_IFNAME])
2154                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2155 
2156         ifm = nlmsg_data(nlh);
2157         if (ifm->ifi_index > 0)
2158                 dev = __dev_get_by_index(net, ifm->ifi_index);
2159         else if (tb[IFLA_IFNAME])
2160                 dev = __dev_get_by_name(net, ifname);
2161         else if (tb[IFLA_GROUP])
2162                 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
2163         else
2164                 return -EINVAL;
2165 
2166         if (!dev)
2167                 return -ENODEV;
2168 
2169         return rtnl_delete_link(dev);
2170 }
2171 
2172 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2173 {
2174         unsigned int old_flags;
2175         int err;
2176 
2177         old_flags = dev->flags;
2178         if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2179                 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2180                 if (err < 0)
2181                         return err;
2182         }
2183 
2184         dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2185 
2186         __dev_notify_flags(dev, old_flags, ~0U);
2187         return 0;
2188 }
2189 EXPORT_SYMBOL(rtnl_configure_link);
2190 
2191 struct net_device *rtnl_create_link(struct net *net,
2192         const char *ifname, unsigned char name_assign_type,
2193         const struct rtnl_link_ops *ops, struct nlattr *tb[])
2194 {
2195         int err;
2196         struct net_device *dev;
2197         unsigned int num_tx_queues = 1;
2198         unsigned int num_rx_queues = 1;
2199 
2200         if (tb[IFLA_NUM_TX_QUEUES])
2201                 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2202         else if (ops->get_num_tx_queues)
2203                 num_tx_queues = ops->get_num_tx_queues();
2204 
2205         if (tb[IFLA_NUM_RX_QUEUES])
2206                 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2207         else if (ops->get_num_rx_queues)
2208                 num_rx_queues = ops->get_num_rx_queues();
2209 
2210         err = -ENOMEM;
2211         dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2212                                ops->setup, num_tx_queues, num_rx_queues);
2213         if (!dev)
2214                 goto err;
2215 
2216         dev_net_set(dev, net);
2217         dev->rtnl_link_ops = ops;
2218         dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2219 
2220         if (tb[IFLA_MTU])
2221                 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2222         if (tb[IFLA_ADDRESS]) {
2223                 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2224                                 nla_len(tb[IFLA_ADDRESS]));
2225                 dev->addr_assign_type = NET_ADDR_SET;
2226         }
2227         if (tb[IFLA_BROADCAST])
2228                 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2229                                 nla_len(tb[IFLA_BROADCAST]));
2230         if (tb[IFLA_TXQLEN])
2231                 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2232         if (tb[IFLA_OPERSTATE])
2233                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2234         if (tb[IFLA_LINKMODE])
2235                 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2236         if (tb[IFLA_GROUP])
2237                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2238 
2239         return dev;
2240 
2241 err:
2242         return ERR_PTR(err);
2243 }
2244 EXPORT_SYMBOL(rtnl_create_link);
2245 
2246 static int rtnl_group_changelink(const struct sk_buff *skb,
2247                 struct net *net, int group,
2248                 struct ifinfomsg *ifm,
2249                 struct nlattr **tb)
2250 {
2251         struct net_device *dev, *aux;
2252         int err;
2253 
2254         for_each_netdev_safe(net, dev, aux) {
2255                 if (dev->group == group) {
2256                         err = do_setlink(skb, dev, ifm, tb, NULL, 0);
2257                         if (err < 0)
2258                                 return err;
2259                 }
2260         }
2261 
2262         return 0;
2263 }
2264 
2265 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2266 {
2267         struct net *net = sock_net(skb->sk);
2268         const struct rtnl_link_ops *ops;
2269         const struct rtnl_link_ops *m_ops = NULL;
2270         struct net_device *dev;
2271         struct net_device *master_dev = NULL;
2272         struct ifinfomsg *ifm;
2273         char kind[MODULE_NAME_LEN];
2274         char ifname[IFNAMSIZ];
2275         struct nlattr *tb[IFLA_MAX+1];
2276         struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2277         unsigned char name_assign_type = NET_NAME_USER;
2278         int err;
2279 
2280 #ifdef CONFIG_MODULES
2281 replay:
2282 #endif
2283         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2284         if (err < 0)
2285                 return err;
2286 
2287         if (tb[IFLA_IFNAME])
2288                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2289         else
2290                 ifname[0] = '\0';
2291 
2292         ifm = nlmsg_data(nlh);
2293         if (ifm->ifi_index > 0)
2294                 dev = __dev_get_by_index(net, ifm->ifi_index);
2295         else {
2296                 if (ifname[0])
2297                         dev = __dev_get_by_name(net, ifname);
2298                 else
2299                         dev = NULL;
2300         }
2301 
2302         if (dev) {
2303                 master_dev = netdev_master_upper_dev_get(dev);
2304                 if (master_dev)
2305                         m_ops = master_dev->rtnl_link_ops;
2306         }
2307 
2308         err = validate_linkmsg(dev, tb);
2309         if (err < 0)
2310                 return err;
2311 
2312         if (tb[IFLA_LINKINFO]) {
2313                 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2314                                        tb[IFLA_LINKINFO], ifla_info_policy);
2315                 if (err < 0)
2316                         return err;
2317         } else
2318                 memset(linkinfo, 0, sizeof(linkinfo));
2319 
2320         if (linkinfo[IFLA_INFO_KIND]) {
2321                 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2322                 ops = rtnl_link_ops_get(kind);
2323         } else {
2324                 kind[0] = '\0';
2325                 ops = NULL;
2326         }
2327 
2328         if (1) {
2329                 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2330                 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
2331                 struct nlattr **data = NULL;
2332                 struct nlattr **slave_data = NULL;
2333                 struct net *dest_net, *link_net = NULL;
2334 
2335                 if (ops) {
2336                         if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2337                                 err = nla_parse_nested(attr, ops->maxtype,
2338                                                        linkinfo[IFLA_INFO_DATA],
2339                                                        ops->policy);
2340                                 if (err < 0)
2341                                         return err;
2342                                 data = attr;
2343                         }
2344                         if (ops->validate) {
2345                                 err = ops->validate(tb, data);
2346                                 if (err < 0)
2347                                         return err;
2348                         }
2349                 }
2350 
2351                 if (m_ops) {
2352                         if (m_ops->slave_maxtype &&
2353                             linkinfo[IFLA_INFO_SLAVE_DATA]) {
2354                                 err = nla_parse_nested(slave_attr,
2355                                                        m_ops->slave_maxtype,
2356                                                        linkinfo[IFLA_INFO_SLAVE_DATA],
2357                                                        m_ops->slave_policy);
2358                                 if (err < 0)
2359                                         return err;
2360                                 slave_data = slave_attr;
2361                         }
2362                         if (m_ops->slave_validate) {
2363                                 err = m_ops->slave_validate(tb, slave_data);
2364                                 if (err < 0)
2365                                         return err;
2366                         }
2367                 }
2368 
2369                 if (dev) {
2370                         int status = 0;
2371 
2372                         if (nlh->nlmsg_flags & NLM_F_EXCL)
2373                                 return -EEXIST;
2374                         if (nlh->nlmsg_flags & NLM_F_REPLACE)
2375                                 return -EOPNOTSUPP;
2376 
2377                         if (linkinfo[IFLA_INFO_DATA]) {
2378                                 if (!ops || ops != dev->rtnl_link_ops ||
2379                                     !ops->changelink)
2380                                         return -EOPNOTSUPP;
2381 
2382                                 err = ops->changelink(dev, tb, data);
2383                                 if (err < 0)
2384                                         return err;
2385                                 status |= DO_SETLINK_NOTIFY;
2386                         }
2387 
2388                         if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2389                                 if (!m_ops || !m_ops->slave_changelink)
2390                                         return -EOPNOTSUPP;
2391 
2392                                 err = m_ops->slave_changelink(master_dev, dev,
2393                                                               tb, slave_data);
2394                                 if (err < 0)
2395                                         return err;
2396                                 status |= DO_SETLINK_NOTIFY;
2397                         }
2398 
2399                         return do_setlink(skb, dev, ifm, tb, ifname, status);
2400                 }
2401 
2402                 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2403                         if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
2404                                 return rtnl_group_changelink(skb, net,
2405                                                 nla_get_u32(tb[IFLA_GROUP]),
2406                                                 ifm, tb);
2407                         return -ENODEV;
2408                 }
2409 
2410                 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
2411                         return -EOPNOTSUPP;
2412 
2413                 if (!ops) {
2414 #ifdef CONFIG_MODULES
2415                         if (kind[0]) {
2416                                 __rtnl_unlock();
2417                                 request_module("rtnl-link-%s", kind);
2418                                 rtnl_lock();
2419                                 ops = rtnl_link_ops_get(kind);
2420                                 if (ops)
2421                                         goto replay;
2422                         }
2423 #endif
2424                         return -EOPNOTSUPP;
2425                 }
2426 
2427                 if (!ops->setup)
2428                         return -EOPNOTSUPP;
2429 
2430                 if (!ifname[0]) {
2431                         snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
2432                         name_assign_type = NET_NAME_ENUM;
2433                 }
2434 
2435                 dest_net = rtnl_link_get_net(net, tb);
2436                 if (IS_ERR(dest_net))
2437                         return PTR_ERR(dest_net);
2438 
2439                 err = -EPERM;
2440                 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2441                         goto out;
2442 
2443                 if (tb[IFLA_LINK_NETNSID]) {
2444                         int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2445 
2446                         link_net = get_net_ns_by_id(dest_net, id);
2447                         if (!link_net) {
2448                                 err =  -EINVAL;
2449                                 goto out;
2450                         }
2451                         err = -EPERM;
2452                         if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2453                                 goto out;
2454                 }
2455 
2456                 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2457                                        name_assign_type, ops, tb);
2458                 if (IS_ERR(dev)) {
2459                         err = PTR_ERR(dev);
2460                         goto out;
2461                 }
2462 
2463                 dev->ifindex = ifm->ifi_index;
2464 
2465                 if (ops->newlink) {
2466                         err = ops->newlink(link_net ? : net, dev, tb, data);
2467                         /* Drivers should call free_netdev() in ->destructor
2468                          * and unregister it on failure after registration
2469                          * so that device could be finally freed in rtnl_unlock.
2470                          */
2471                         if (err < 0) {
2472                                 /* If device is not registered at all, free it now */
2473                                 if (dev->reg_state == NETREG_UNINITIALIZED)
2474                                         free_netdev(dev);
2475                                 goto out;
2476                         }
2477                 } else {
2478                         err = register_netdevice(dev);
2479                         if (err < 0) {
2480                                 free_netdev(dev);
2481                                 goto out;
2482                         }
2483                 }
2484                 err = rtnl_configure_link(dev, ifm);
2485                 if (err < 0)
2486                         goto out_unregister;
2487                 if (link_net) {
2488                         err = dev_change_net_namespace(dev, dest_net, ifname);
2489                         if (err < 0)
2490                                 goto out_unregister;
2491                 }
2492 out:
2493                 if (link_net)
2494                         put_net(link_net);
2495                 put_net(dest_net);
2496                 return err;
2497 out_unregister:
2498                 if (ops->newlink) {
2499                         LIST_HEAD(list_kill);
2500 
2501                         ops->dellink(dev, &list_kill);
2502                         unregister_netdevice_many(&list_kill);
2503                 } else {
2504                         unregister_netdevice(dev);
2505                 }
2506                 goto out;
2507         }
2508 }
2509 
2510 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2511 {
2512         struct net *net = sock_net(skb->sk);
2513         struct ifinfomsg *ifm;
2514         char ifname[IFNAMSIZ];
2515         struct nlattr *tb[IFLA_MAX+1];
2516         struct net_device *dev = NULL;
2517         struct sk_buff *nskb;
2518         int err;
2519         u32 ext_filter_mask = 0;
2520 
2521         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2522         if (err < 0)
2523                 return err;
2524 
2525         if (tb[IFLA_IFNAME])
2526                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2527 
2528         if (tb[IFLA_EXT_MASK])
2529                 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2530 
2531         ifm = nlmsg_data(nlh);
2532         if (ifm->ifi_index > 0)
2533                 dev = __dev_get_by_index(net, ifm->ifi_index);
2534         else if (tb[IFLA_IFNAME])
2535                 dev = __dev_get_by_name(net, ifname);
2536         else
2537                 return -EINVAL;
2538 
2539         if (dev == NULL)
2540                 return -ENODEV;
2541 
2542         nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2543         if (nskb == NULL)
2544                 return -ENOBUFS;
2545 
2546         err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2547                                nlh->nlmsg_seq, 0, 0, ext_filter_mask);
2548         if (err < 0) {
2549                 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2550                 WARN_ON(err == -EMSGSIZE);
2551                 kfree_skb(nskb);
2552         } else
2553                 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2554 
2555         return err;
2556 }
2557 
2558 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2559 {
2560         struct net *net = sock_net(skb->sk);
2561         struct net_device *dev;
2562         struct nlattr *tb[IFLA_MAX+1];
2563         u32 ext_filter_mask = 0;
2564         u16 min_ifinfo_dump_size = 0;
2565         int hdrlen;
2566 
2567         /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2568         hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2569                  sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2570 
2571         if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
2572                 if (tb[IFLA_EXT_MASK])
2573                         ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2574         }
2575 
2576         if (!ext_filter_mask)
2577                 return NLMSG_GOODSIZE;
2578         /*
2579          * traverse the list of net devices and compute the minimum
2580          * buffer size based upon the filter mask.
2581          */
2582         list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2583                 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2584                                              if_nlmsg_size(dev,
2585                                                            ext_filter_mask));
2586         }
2587 
2588         return min_ifinfo_dump_size;
2589 }
2590 
2591 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
2592 {
2593         int idx;
2594         int s_idx = cb->family;
2595 
2596         if (s_idx == 0)
2597                 s_idx = 1;
2598         for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
2599                 int type = cb->nlh->nlmsg_type-RTM_BASE;
2600                 if (idx < s_idx || idx == PF_PACKET)
2601                         continue;
2602                 if (rtnl_msg_handlers[idx] == NULL ||
2603                     rtnl_msg_handlers[idx][type].dumpit == NULL)
2604                         continue;
2605                 if (idx > s_idx) {
2606                         memset(&cb->args[0], 0, sizeof(cb->args));
2607                         cb->prev_seq = 0;
2608                         cb->seq = 0;
2609                 }
2610                 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
2611                         break;
2612         }
2613         cb->family = idx;
2614 
2615         return skb->len;
2616 }
2617 
2618 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2619                                        unsigned int change, gfp_t flags)
2620 {
2621         struct net *net = dev_net(dev);
2622         struct sk_buff *skb;
2623         int err = -ENOBUFS;
2624         size_t if_info_size;
2625 
2626         skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
2627         if (skb == NULL)
2628                 goto errout;
2629 
2630         err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
2631         if (err < 0) {
2632                 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2633                 WARN_ON(err == -EMSGSIZE);
2634                 kfree_skb(skb);
2635                 goto errout;
2636         }
2637         return skb;
2638 errout:
2639         if (err < 0)
2640                 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2641         return NULL;
2642 }
2643 
2644 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2645 {
2646         struct net *net = dev_net(dev);
2647 
2648         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2649 }
2650 
2651 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2652                   gfp_t flags)
2653 {
2654         struct sk_buff *skb;
2655 
2656         if (dev->reg_state != NETREG_REGISTERED)
2657                 return;
2658 
2659         skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2660         if (skb)
2661                 rtmsg_ifinfo_send(skb, dev, flags);
2662 }
2663 EXPORT_SYMBOL(rtmsg_ifinfo);
2664 
2665 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2666                                    struct net_device *dev,
2667                                    u8 *addr, u16 vid, u32 pid, u32 seq,
2668                                    int type, unsigned int flags,
2669                                    int nlflags, u16 ndm_state)
2670 {
2671         struct nlmsghdr *nlh;
2672         struct ndmsg *ndm;
2673 
2674         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2675         if (!nlh)
2676                 return -EMSGSIZE;
2677 
2678         ndm = nlmsg_data(nlh);
2679         ndm->ndm_family  = AF_BRIDGE;
2680         ndm->ndm_pad1    = 0;
2681         ndm->ndm_pad2    = 0;
2682         ndm->ndm_flags   = flags;
2683         ndm->ndm_type    = 0;
2684         ndm->ndm_ifindex = dev->ifindex;
2685         ndm->ndm_state   = ndm_state;
2686 
2687         if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2688                 goto nla_put_failure;
2689         if (vid)
2690                 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2691                         goto nla_put_failure;
2692 
2693         nlmsg_end(skb, nlh);
2694         return 0;
2695 
2696 nla_put_failure:
2697         nlmsg_cancel(skb, nlh);
2698         return -EMSGSIZE;
2699 }
2700 
2701 static inline size_t rtnl_fdb_nlmsg_size(void)
2702 {
2703         return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2704 }
2705 
2706 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2707                             u16 ndm_state)
2708 {
2709         struct net *net = dev_net(dev);
2710         struct sk_buff *skb;
2711         int err = -ENOBUFS;
2712 
2713         skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2714         if (!skb)
2715                 goto errout;
2716 
2717         err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2718                                       0, 0, type, NTF_SELF, 0, ndm_state);
2719         if (err < 0) {
2720                 kfree_skb(skb);
2721                 goto errout;
2722         }
2723 
2724         rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2725         return;
2726 errout:
2727         rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2728 }
2729 
2730 /**
2731  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2732  */
2733 int ndo_dflt_fdb_add(struct ndmsg *ndm,
2734                      struct nlattr *tb[],
2735                      struct net_device *dev,
2736                      const unsigned char *addr, u16 vid,
2737                      u16 flags)
2738 {
2739         int err = -EINVAL;
2740 
2741         /* If aging addresses are supported device will need to
2742          * implement its own handler for this.
2743          */
2744         if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2745                 pr_info("%s: FDB only supports static addresses\n", dev->name);
2746                 return err;
2747         }
2748 
2749         if (vid) {
2750                 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2751                 return err;
2752         }
2753 
2754         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2755                 err = dev_uc_add_excl(dev, addr);
2756         else if (is_multicast_ether_addr(addr))
2757                 err = dev_mc_add_excl(dev, addr);
2758 
2759         /* Only return duplicate errors if NLM_F_EXCL is set */
2760         if (err == -EEXIST && !(flags & NLM_F_EXCL))
2761                 err = 0;
2762 
2763         return err;
2764 }
2765 EXPORT_SYMBOL(ndo_dflt_fdb_add);
2766 
2767 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2768 {
2769         u16 vid = 0;
2770 
2771         if (vlan_attr) {
2772                 if (nla_len(vlan_attr) != sizeof(u16)) {
2773                         pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2774                         return -EINVAL;
2775                 }
2776 
2777                 vid = nla_get_u16(vlan_attr);
2778 
2779                 if (!vid || vid >= VLAN_VID_MASK) {
2780                         pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2781                                 vid);
2782                         return -EINVAL;
2783                 }
2784         }
2785         *p_vid = vid;
2786         return 0;
2787 }
2788 
2789 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
2790 {
2791         struct net *net = sock_net(skb->sk);
2792         struct ndmsg *ndm;
2793         struct nlattr *tb[NDA_MAX+1];
2794         struct net_device *dev;
2795         u8 *addr;
2796         u16 vid;
2797         int err;
2798 
2799         err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2800         if (err < 0)
2801                 return err;
2802 
2803         ndm = nlmsg_data(nlh);
2804         if (ndm->ndm_ifindex == 0) {
2805                 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2806                 return -EINVAL;
2807         }
2808 
2809         dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2810         if (dev == NULL) {
2811                 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2812                 return -ENODEV;
2813         }
2814 
2815         if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2816                 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2817                 return -EINVAL;
2818         }
2819 
2820         addr = nla_data(tb[NDA_LLADDR]);
2821 
2822         err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2823         if (err)
2824                 return err;
2825 
2826         err = -EOPNOTSUPP;
2827 
2828         /* Support fdb on master device the net/bridge default case */
2829         if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2830             (dev->priv_flags & IFF_BRIDGE_PORT)) {
2831                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2832                 const struct net_device_ops *ops = br_dev->netdev_ops;
2833 
2834                 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2835                                        nlh->nlmsg_flags);
2836                 if (err)
2837                         goto out;
2838                 else
2839                         ndm->ndm_flags &= ~NTF_MASTER;
2840         }
2841 
2842         /* Embedded bridge, macvlan, and any other device support */
2843         if ((ndm->ndm_flags & NTF_SELF)) {
2844                 if (dev->netdev_ops->ndo_fdb_add)
2845                         err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2846                                                            vid,
2847                                                            nlh->nlmsg_flags);
2848                 else
2849                         err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
2850                                                nlh->nlmsg_flags);
2851 
2852                 if (!err) {
2853                         rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
2854                                         ndm->ndm_state);
2855                         ndm->ndm_flags &= ~NTF_SELF;
2856                 }
2857         }
2858 out:
2859         return err;
2860 }
2861 
2862 /**
2863  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2864  */
2865 int ndo_dflt_fdb_del(struct ndmsg *ndm,
2866                      struct nlattr *tb[],
2867                      struct net_device *dev,
2868                      const unsigned char *addr, u16 vid)
2869 {
2870         int err = -EINVAL;
2871 
2872         /* If aging addresses are supported device will need to
2873          * implement its own handler for this.
2874          */
2875         if (!(ndm->ndm_state & NUD_PERMANENT)) {
2876                 pr_info("%s: FDB only supports static addresses\n", dev->name);
2877                 return err;
2878         }
2879 
2880         if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2881                 err = dev_uc_del(dev, addr);
2882         else if (is_multicast_ether_addr(addr))
2883                 err = dev_mc_del(dev, addr);
2884 
2885         return err;
2886 }
2887 EXPORT_SYMBOL(ndo_dflt_fdb_del);
2888 
2889 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
2890 {
2891         struct net *net = sock_net(skb->sk);
2892         struct ndmsg *ndm;
2893         struct nlattr *tb[NDA_MAX+1];
2894         struct net_device *dev;
2895         int err = -EINVAL;
2896         __u8 *addr;
2897         u16 vid;
2898 
2899         if (!netlink_capable(skb, CAP_NET_ADMIN))
2900                 return -EPERM;
2901 
2902         err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2903         if (err < 0)
2904                 return err;
2905 
2906         ndm = nlmsg_data(nlh);
2907         if (ndm->ndm_ifindex == 0) {
2908                 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2909                 return -EINVAL;
2910         }
2911 
2912         dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2913         if (dev == NULL) {
2914                 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2915                 return -ENODEV;
2916         }
2917 
2918         if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2919                 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
2920                 return -EINVAL;
2921         }
2922 
2923         addr = nla_data(tb[NDA_LLADDR]);
2924 
2925         err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2926         if (err)
2927                 return err;
2928 
2929         err = -EOPNOTSUPP;
2930 
2931         /* Support fdb on master device the net/bridge default case */
2932         if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2933             (dev->priv_flags & IFF_BRIDGE_PORT)) {
2934                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2935                 const struct net_device_ops *ops = br_dev->netdev_ops;
2936 
2937                 if (ops->ndo_fdb_del)
2938                         err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
2939 
2940                 if (err)
2941                         goto out;
2942                 else
2943                         ndm->ndm_flags &= ~NTF_MASTER;
2944         }
2945 
2946         /* Embedded bridge, macvlan, and any other device support */
2947         if (ndm->ndm_flags & NTF_SELF) {
2948                 if (dev->netdev_ops->ndo_fdb_del)
2949                         err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2950                                                            vid);
2951                 else
2952                         err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
2953 
2954                 if (!err) {
2955                         rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
2956                                         ndm->ndm_state);
2957                         ndm->ndm_flags &= ~NTF_SELF;
2958                 }
2959         }
2960 out:
2961         return err;
2962 }
2963 
2964 static int nlmsg_populate_fdb(struct sk_buff *skb,
2965                               struct netlink_callback *cb,
2966                               struct net_device *dev,
2967                               int *idx,
2968                               struct netdev_hw_addr_list *list)
2969 {
2970         struct netdev_hw_addr *ha;
2971         int err;
2972         u32 portid, seq;
2973 
2974         portid = NETLINK_CB(cb->skb).portid;
2975         seq = cb->nlh->nlmsg_seq;
2976 
2977         list_for_each_entry(ha, &list->list, list) {
2978                 if (*idx < cb->args[0])
2979                         goto skip;
2980 
2981                 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
2982                                               portid, seq,
2983                                               RTM_NEWNEIGH, NTF_SELF,
2984                                               NLM_F_MULTI, NUD_PERMANENT);
2985                 if (err < 0)
2986                         return err;
2987 skip:
2988                 *idx += 1;
2989         }
2990         return 0;
2991 }
2992 
2993 /**
2994  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2995  * @nlh: netlink message header
2996  * @dev: netdevice
2997  *
2998  * Default netdevice operation to dump the existing unicast address list.
2999  * Returns number of addresses from list put in skb.
3000  */
3001 int ndo_dflt_fdb_dump(struct sk_buff *skb,
3002                       struct netlink_callback *cb,
3003                       struct net_device *dev,
3004                       struct net_device *filter_dev,
3005                       int idx)
3006 {
3007         int err;
3008 
3009         netif_addr_lock_bh(dev);
3010         err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
3011         if (err)
3012                 goto out;
3013         nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
3014 out:
3015         netif_addr_unlock_bh(dev);
3016         cb->args[1] = err;
3017         return idx;
3018 }
3019 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3020 
3021 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3022 {
3023         struct net_device *dev;
3024         struct nlattr *tb[IFLA_MAX+1];
3025         struct net_device *br_dev = NULL;
3026         const struct net_device_ops *ops = NULL;
3027         const struct net_device_ops *cops = NULL;
3028         struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3029         struct net *net = sock_net(skb->sk);
3030         int brport_idx = 0;
3031         int br_idx = 0;
3032         int idx = 0;
3033 
3034         if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
3035                         ifla_policy) == 0) {
3036                 if (tb[IFLA_MASTER])
3037                         br_idx = nla_get_u32(tb[IFLA_MASTER]);
3038         }
3039 
3040         brport_idx = ifm->ifi_index;
3041 
3042         if (br_idx) {
3043                 br_dev = __dev_get_by_index(net, br_idx);
3044                 if (!br_dev)
3045                         return -ENODEV;
3046 
3047                 ops = br_dev->netdev_ops;
3048         }
3049 
3050         cb->args[1] = 0;
3051         for_each_netdev(net, dev) {
3052                 if (brport_idx && (dev->ifindex != brport_idx))
3053                         continue;
3054 
3055                 if (!br_idx) { /* user did not specify a specific bridge */
3056                         if (dev->priv_flags & IFF_BRIDGE_PORT) {
3057                                 br_dev = netdev_master_upper_dev_get(dev);
3058                                 cops = br_dev->netdev_ops;
3059                         }
3060 
3061                 } else {
3062                         if (dev != br_dev &&
3063                             !(dev->priv_flags & IFF_BRIDGE_PORT))
3064                                 continue;
3065 
3066                         if (br_dev != netdev_master_upper_dev_get(dev) &&
3067                             !(dev->priv_flags & IFF_EBRIDGE))
3068                                 continue;
3069 
3070                         cops = ops;
3071                 }
3072 
3073                 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3074                         if (cops && cops->ndo_fdb_dump)
3075                                 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
3076                                                          idx);
3077                 }
3078                 if (cb->args[1] == -EMSGSIZE)
3079                         break;
3080 
3081                 if (dev->netdev_ops->ndo_fdb_dump)
3082                         idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
3083                                                             idx);
3084                 else
3085                         idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
3086                 if (cb->args[1] == -EMSGSIZE)
3087                         break;
3088 
3089                 cops = NULL;
3090         }
3091 
3092         cb->args[0] = idx;
3093         return skb->len;
3094 }
3095 
3096 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3097                                unsigned int attrnum, unsigned int flag)
3098 {
3099         if (mask & flag)
3100                 return nla_put_u8(skb, attrnum, !!(flags & flag));
3101         return 0;
3102 }
3103 
3104 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
3105                             struct net_device *dev, u16 mode,
3106                             u32 flags, u32 mask, int nlflags,
3107                             u32 filter_mask,
3108                             int (*vlan_fill)(struct sk_buff *skb,
3109                                              struct net_device *dev,
3110                                              u32 filter_mask))
3111 {
3112         struct nlmsghdr *nlh;
3113         struct ifinfomsg *ifm;
3114         struct nlattr *br_afspec;
3115         struct nlattr *protinfo;
3116         u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3117         struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3118         int err = 0;
3119 
3120         nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3121         if (nlh == NULL)
3122                 return -EMSGSIZE;
3123 
3124         ifm = nlmsg_data(nlh);
3125         ifm->ifi_family = AF_BRIDGE;
3126         ifm->__ifi_pad = 0;
3127         ifm->ifi_type = dev->type;
3128         ifm->ifi_index = dev->ifindex;
3129         ifm->ifi_flags = dev_get_flags(dev);
3130         ifm->ifi_change = 0;
3131 
3132 
3133         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3134             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3135             nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3136             (br_dev &&
3137              nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3138             (dev->addr_len &&
3139              nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3140             (dev->ifindex != dev_get_iflink(dev) &&
3141              nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3142                 goto nla_put_failure;
3143 
3144         br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3145         if (!br_afspec)
3146                 goto nla_put_failure;
3147 
3148         if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3149                 nla_nest_cancel(skb, br_afspec);
3150                 goto nla_put_failure;
3151         }
3152 
3153         if (mode != BRIDGE_MODE_UNDEF) {
3154                 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3155                         nla_nest_cancel(skb, br_afspec);
3156                         goto nla_put_failure;
3157                 }
3158         }
3159         if (vlan_fill) {
3160                 err = vlan_fill(skb, dev, filter_mask);
3161                 if (err) {
3162                         nla_nest_cancel(skb, br_afspec);
3163                         goto nla_put_failure;
3164                 }
3165         }
3166         nla_nest_end(skb, br_afspec);
3167 
3168         protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3169         if (!protinfo)
3170                 goto nla_put_failure;
3171 
3172         if (brport_nla_put_flag(skb, flags, mask,
3173                                 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3174             brport_nla_put_flag(skb, flags, mask,
3175                                 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3176             brport_nla_put_flag(skb, flags, mask,
3177                                 IFLA_BRPORT_FAST_LEAVE,
3178                                 BR_MULTICAST_FAST_LEAVE) ||
3179             brport_nla_put_flag(skb, flags, mask,
3180                                 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3181             brport_nla_put_flag(skb, flags, mask,
3182                                 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3183             brport_nla_put_flag(skb, flags, mask,
3184                                 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3185             brport_nla_put_flag(skb, flags, mask,
3186                                 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3187             brport_nla_put_flag(skb, flags, mask,
3188                                 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3189                 nla_nest_cancel(skb, protinfo);
3190                 goto nla_put_failure;
3191         }
3192 
3193         nla_nest_end(skb, protinfo);
3194 
3195         nlmsg_end(skb, nlh);
3196         return 0;
3197 nla_put_failure:
3198         nlmsg_cancel(skb, nlh);
3199         return err ? err : -EMSGSIZE;
3200 }
3201 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
3202 
3203 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3204 {
3205         struct net *net = sock_net(skb->sk);
3206         struct net_device *dev;
3207         int idx = 0;
3208         u32 portid = NETLINK_CB(cb->skb).portid;
3209         u32 seq = cb->nlh->nlmsg_seq;
3210         u32 filter_mask = 0;
3211         int err;
3212 
3213         if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3214                 struct nlattr *extfilt;
3215 
3216                 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3217                                           IFLA_EXT_MASK);
3218                 if (extfilt) {
3219                         if (nla_len(extfilt) < sizeof(filter_mask))
3220                                 return -EINVAL;
3221 
3222                         filter_mask = nla_get_u32(extfilt);
3223                 }
3224         }
3225 
3226         rcu_read_lock();
3227         for_each_netdev_rcu(net, dev) {
3228                 const struct net_device_ops *ops = dev->netdev_ops;
3229                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3230 
3231                 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
3232                         if (idx >= cb->args[0]) {
3233                                 err = br_dev->netdev_ops->ndo_bridge_getlink(
3234                                                 skb, portid, seq, dev,
3235                                                 filter_mask, NLM_F_MULTI);
3236                                 if (err < 0 && err != -EOPNOTSUPP)
3237                                         break;
3238                         }
3239                         idx++;
3240                 }
3241 
3242                 if (ops->ndo_bridge_getlink) {
3243                         if (idx >= cb->args[0]) {
3244                                 err = ops->ndo_bridge_getlink(skb, portid,
3245                                                               seq, dev,
3246                                                               filter_mask,
3247                                                               NLM_F_MULTI);
3248                                 if (err < 0 && err != -EOPNOTSUPP)
3249                                         break;
3250                         }
3251                         idx++;
3252                 }
3253         }
3254         rcu_read_unlock();
3255         cb->args[0] = idx;
3256 
3257         return skb->len;
3258 }
3259 
3260 static inline size_t bridge_nlmsg_size(void)
3261 {
3262         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3263                 + nla_total_size(IFNAMSIZ)      /* IFLA_IFNAME */
3264                 + nla_total_size(MAX_ADDR_LEN)  /* IFLA_ADDRESS */
3265                 + nla_total_size(sizeof(u32))   /* IFLA_MASTER */
3266                 + nla_total_size(sizeof(u32))   /* IFLA_MTU */
3267                 + nla_total_size(sizeof(u32))   /* IFLA_LINK */
3268                 + nla_total_size(sizeof(u32))   /* IFLA_OPERSTATE */
3269                 + nla_total_size(sizeof(u8))    /* IFLA_PROTINFO */
3270                 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3271                 + nla_total_size(sizeof(u16))   /* IFLA_BRIDGE_FLAGS */
3272                 + nla_total_size(sizeof(u16));  /* IFLA_BRIDGE_MODE */
3273 }
3274 
3275 static int rtnl_bridge_notify(struct net_device *dev)
3276 {
3277         struct net *net = dev_net(dev);
3278         struct sk_buff *skb;
3279         int err = -EOPNOTSUPP;
3280 
3281         if (!dev->netdev_ops->ndo_bridge_getlink)
3282                 return 0;
3283 
3284         skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3285         if (!skb) {
3286                 err = -ENOMEM;
3287                 goto errout;
3288         }
3289 
3290         err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
3291         if (err < 0)
3292                 goto errout;
3293 
3294         if (!skb->len)
3295                 goto errout;
3296 
3297         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3298         return 0;
3299 errout:
3300         WARN_ON(err == -EMSGSIZE);
3301         kfree_skb(skb);
3302         if (err)
3303                 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3304         return err;
3305 }
3306 
3307 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
3308 {
3309         struct net *net = sock_net(skb->sk);
3310         struct ifinfomsg *ifm;
3311         struct net_device *dev;
3312         struct nlattr *br_spec, *attr = NULL;
3313         int rem, err = -EOPNOTSUPP;
3314         u16 flags = 0;
3315         bool have_flags = false;
3316 
3317         if (nlmsg_len(nlh) < sizeof(*ifm))
3318                 return -EINVAL;
3319 
3320         ifm = nlmsg_data(nlh);
3321         if (ifm->ifi_family != AF_BRIDGE)
3322                 return -EPFNOSUPPORT;
3323 
3324         dev = __dev_get_by_index(net, ifm->ifi_index);
3325         if (!dev) {
3326                 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3327                 return -ENODEV;
3328         }
3329 
3330         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3331         if (br_spec) {
3332                 nla_for_each_nested(attr, br_spec, rem) {
3333                         if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3334                                 if (nla_len(attr) < sizeof(flags))
3335                                         return -EINVAL;
3336 
3337                                 have_flags = true;
3338                                 flags = nla_get_u16(attr);
3339                                 break;
3340                         }
3341                 }
3342         }
3343 
3344         if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3345                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3346 
3347                 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
3348                         err = -EOPNOTSUPP;
3349                         goto out;
3350                 }
3351 
3352                 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
3353                 if (err)
3354                         goto out;
3355 
3356                 flags &= ~BRIDGE_FLAGS_MASTER;
3357         }
3358 
3359         if ((flags & BRIDGE_FLAGS_SELF)) {
3360                 if (!dev->netdev_ops->ndo_bridge_setlink)
3361                         err = -EOPNOTSUPP;
3362                 else
3363                         err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3364                                                                   flags);
3365                 if (!err) {
3366                         flags &= ~BRIDGE_FLAGS_SELF;
3367 
3368                         /* Generate event to notify upper layer of bridge
3369                          * change
3370                          */
3371                         err = rtnl_bridge_notify(dev);
3372                 }
3373         }
3374 
3375         if (have_flags)
3376                 memcpy(nla_data(attr), &flags, sizeof(flags));
3377 out:
3378         return err;
3379 }
3380 
3381 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
3382 {
3383         struct net *net = sock_net(skb->sk);
3384         struct ifinfomsg *ifm;
3385         struct net_device *dev;
3386         struct nlattr *br_spec, *attr = NULL;
3387         int rem, err = -EOPNOTSUPP;
3388         u16 flags = 0;
3389         bool have_flags = false;
3390 
3391         if (nlmsg_len(nlh) < sizeof(*ifm))
3392                 return -EINVAL;
3393 
3394         ifm = nlmsg_data(nlh);
3395         if (ifm->ifi_family != AF_BRIDGE)
3396                 return -EPFNOSUPPORT;
3397 
3398         dev = __dev_get_by_index(net, ifm->ifi_index);
3399         if (!dev) {
3400                 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3401                 return -ENODEV;
3402         }
3403 
3404         br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3405         if (br_spec) {
3406                 nla_for_each_nested(attr, br_spec, rem) {
3407                         if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3408                                 if (nla_len(attr) < sizeof(flags))
3409                                         return -EINVAL;
3410 
3411                                 have_flags = true;
3412                                 flags = nla_get_u16(attr);
3413                                 break;
3414                         }
3415                 }
3416         }
3417 
3418         if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3419                 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3420 
3421                 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3422                         err = -EOPNOTSUPP;
3423                         goto out;
3424                 }
3425 
3426                 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
3427                 if (err)
3428                         goto out;
3429 
3430                 flags &= ~BRIDGE_FLAGS_MASTER;
3431         }
3432 
3433         if ((flags & BRIDGE_FLAGS_SELF)) {
3434                 if (!dev->netdev_ops->ndo_bridge_dellink)
3435                         err = -EOPNOTSUPP;
3436                 else
3437                         err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3438                                                                   flags);
3439 
3440                 if (!err) {
3441                         flags &= ~BRIDGE_FLAGS_SELF;
3442 
3443                         /* Generate event to notify upper layer of bridge
3444                          * change
3445                          */
3446                         err = rtnl_bridge_notify(dev);
3447                 }
3448         }
3449 
3450         if (have_flags)
3451                 memcpy(nla_data(attr), &flags, sizeof(flags));
3452 out:
3453         return err;
3454 }
3455 
3456 /* Process one rtnetlink message. */
3457 
3458 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
3459 {
3460         struct net *net = sock_net(skb->sk);
3461         rtnl_doit_func doit;
3462         int kind;
3463         int family;
3464         int type;
3465         int err;
3466 
3467         type = nlh->nlmsg_type;
3468         if (type > RTM_MAX)
3469                 return -EOPNOTSUPP;
3470 
3471         type -= RTM_BASE;
3472 
3473         /* All the messages must have at least 1 byte length */
3474         if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
3475                 return 0;
3476 
3477         family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
3478         kind = type&3;
3479 
3480         if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
3481                 return -EPERM;
3482 
3483         if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
3484                 struct sock *rtnl;
3485                 rtnl_dumpit_func dumpit;
3486                 rtnl_calcit_func calcit;
3487                 u16 min_dump_alloc = 0;
3488 
3489                 dumpit = rtnl_get_dumpit(family, type);
3490                 if (dumpit == NULL)
3491                         return -EOPNOTSUPP;
3492                 calcit = rtnl_get_calcit(family, type);
3493                 if (calcit)
3494                         min_dump_alloc = calcit(skb, nlh);
3495 
3496                 __rtnl_unlock();
3497                 rtnl = net->rtnl;
3498                 {
3499                         struct netlink_dump_control c = {
3500                                 .dump           = dumpit,
3501                                 .min_dump_alloc = min_dump_alloc,
3502                         };
3503                         err = netlink_dump_start(rtnl, skb, nlh, &c);
3504                 }
3505                 rtnl_lock();
3506                 return err;
3507         }
3508 
3509         doit = rtnl_get_doit(family, type);
3510         if (doit == NULL)
3511                 return -EOPNOTSUPP;
3512 
3513         return doit(skb, nlh);
3514 }
3515 
3516 static void rtnetlink_rcv(struct sk_buff *skb)
3517 {
3518         rtnl_lock();
3519         netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3520         rtnl_unlock();
3521 }
3522 
3523 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3524 {
3525         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3526 
3527         switch (event) {
3528         case NETDEV_UP:
3529         case NETDEV_DOWN:
3530         case NETDEV_PRE_UP:
3531         case NETDEV_POST_INIT:
3532         case NETDEV_REGISTER:
3533         case NETDEV_CHANGE:
3534         case NETDEV_PRE_TYPE_CHANGE:
3535         case NETDEV_GOING_DOWN:
3536         case NETDEV_UNREGISTER:
3537         case NETDEV_UNREGISTER_FINAL:
3538         case NETDEV_RELEASE:
3539         case NETDEV_JOIN:
3540         case NETDEV_BONDING_INFO:
3541                 break;
3542         default:
3543                 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
3544                 break;
3545         }
3546         return NOTIFY_DONE;
3547 }
3548 
3549 static struct notifier_block rtnetlink_dev_notifier = {
3550         .notifier_call  = rtnetlink_event,
3551 };
3552 
3553 
3554 static int __net_init rtnetlink_net_init(struct net *net)
3555 {
3556         struct sock *sk;
3557         struct netlink_kernel_cfg cfg = {
3558                 .groups         = RTNLGRP_MAX,
3559                 .input          = rtnetlink_rcv,
3560                 .cb_mutex       = &rtnl_mutex,
3561                 .flags          = NL_CFG_F_NONROOT_RECV,
3562         };
3563 
3564         sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
3565         if (!sk)
3566                 return -ENOMEM;
3567         net->rtnl = sk;
3568         return 0;
3569 }
3570 
3571 static void __net_exit rtnetlink_net_exit(struct net *net)
3572 {
3573         netlink_kernel_release(net->rtnl);
3574         net->rtnl = NULL;
3575 }
3576 
3577 static struct pernet_operations rtnetlink_net_ops = {
3578         .init = rtnetlink_net_init,
3579         .exit = rtnetlink_net_exit,
3580 };
3581 
3582 void __init rtnetlink_init(void)
3583 {
3584         if (register_pernet_subsys(&rtnetlink_net_ops))
3585                 panic("rtnetlink_init: cannot initialize rtnetlink\n");
3586 
3587         register_netdevice_notifier(&rtnetlink_dev_notifier);
3588 
3589         rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3590                       rtnl_dump_ifinfo, rtnl_calcit);
3591         rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3592         rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3593         rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
3594 
3595         rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3596         rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
3597 
3598         rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3599         rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3600         rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
3601 
3602         rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
3603         rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
3604         rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
3605 }
3606 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp