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

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

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

~ [ 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