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

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

Version: ~ [ linux-5.1-rc5 ] ~ [ linux-5.0.7 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.34 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.111 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.168 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.178 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.138 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.65 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
  3  *              operating system.  INET is implemented using the  BSD Socket
  4  *              interface as the means of communication with the user level.
  5  *
  6  *              Routing netlink socket interface: protocol independent part.
  7  *
  8  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9  *
 10  *              This program is free software; you can redistribute it and/or
 11  *              modify it under the terms of the GNU General Public License
 12  *              as published by the Free Software Foundation; either version
 13  *              2 of the License, or (at your option) any later version.
 14  *
 15  *      Fixes:
 16  *      Vitaly E. Lavrov                RTA_OK arithmetics was wrong.
 17  */
 18 
 19 #include <linux/errno.h>
 20 #include <linux/module.h>
 21 #include <linux/types.h>
 22 #include <linux/socket.h>
 23 #include <linux/kernel.h>
 24 #include <linux/timer.h>
 25 #include <linux/string.h>
 26 #include <linux/sockios.h>
 27 #include <linux/net.h>
 28 #include <linux/fcntl.h>
 29 #include <linux/mm.h>
 30 #include <linux/slab.h>
 31 #include <linux/interrupt.h>
 32 #include <linux/capability.h>
 33 #include <linux/skbuff.h>
 34 #include <linux/init.h>
 35 #include <linux/security.h>
 36 #include <linux/mutex.h>
 37 #include <linux/if_addr.h>
 38 #include <linux/pci.h>
 39 
 40 #include <asm/uaccess.h>
 41 #include <asm/system.h>
 42 
 43 #include <linux/inet.h>
 44 #include <linux/netdevice.h>
 45 #include <net/ip.h>
 46 #include <net/protocol.h>
 47 #include <net/arp.h>
 48 #include <net/route.h>
 49 #include <net/udp.h>
 50 #include <net/sock.h>
 51 #include <net/pkt_sched.h>
 52 #include <net/fib_rules.h>
 53 #include <net/rtnetlink.h>
 54 #include <net/net_namespace.h>
 55 
 56 struct rtnl_link {
 57         rtnl_doit_func          doit;
 58         rtnl_dumpit_func        dumpit;
 59         rtnl_calcit_func        calcit;
 60 };
 61 
 62 static DEFINE_MUTEX(rtnl_mutex);
 63 
 64 void rtnl_lock(void)
 65 {
 66         mutex_lock(&rtnl_mutex);
 67 }
 68 EXPORT_SYMBOL(rtnl_lock);
 69 
 70 void __rtnl_unlock(void)
 71 {
 72         mutex_unlock(&rtnl_mutex);
 73 }
 74 
 75 void rtnl_unlock(void)
 76 {
 77         /* This fellow will unlock it for us. */
 78         netdev_run_todo();
 79 }
 80 EXPORT_SYMBOL(rtnl_unlock);
 81 
 82 int rtnl_trylock(void)
 83 {
 84         return mutex_trylock(&rtnl_mutex);
 85 }
 86 EXPORT_SYMBOL(rtnl_trylock);
 87 
 88 int rtnl_is_locked(void)
 89 {
 90         return mutex_is_locked(&rtnl_mutex);
 91 }
 92 EXPORT_SYMBOL(rtnl_is_locked);
 93 
 94 #ifdef CONFIG_PROVE_LOCKING
 95 int lockdep_rtnl_is_held(void)
 96 {
 97         return lockdep_is_held(&rtnl_mutex);
 98 }
 99 EXPORT_SYMBOL(lockdep_rtnl_is_held);
100 #endif /* #ifdef CONFIG_PROVE_LOCKING */
101 
102 static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
103 
104 static inline int rtm_msgindex(int msgtype)
105 {
106         int msgindex = msgtype - RTM_BASE;
107 
108         /*
109          * msgindex < 0 implies someone tried to register a netlink
110          * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
111          * the message type has not been added to linux/rtnetlink.h
112          */
113         BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
114 
115         return msgindex;
116 }
117 
118 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
119 {
120         struct rtnl_link *tab;
121 
122         if (protocol <= RTNL_FAMILY_MAX)
123                 tab = rtnl_msg_handlers[protocol];
124         else
125                 tab = NULL;
126 
127         if (tab == NULL || tab[msgindex].doit == NULL)
128                 tab = rtnl_msg_handlers[PF_UNSPEC];
129 
130         return tab ? tab[msgindex].doit : NULL;
131 }
132 
133 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
134 {
135         struct rtnl_link *tab;
136 
137         if (protocol <= RTNL_FAMILY_MAX)
138                 tab = rtnl_msg_handlers[protocol];
139         else
140                 tab = NULL;
141 
142         if (tab == NULL || tab[msgindex].dumpit == NULL)
143                 tab = rtnl_msg_handlers[PF_UNSPEC];
144 
145         return tab ? tab[msgindex].dumpit : NULL;
146 }
147 
148 static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
149 {
150         struct rtnl_link *tab;
151 
152         if (protocol <= RTNL_FAMILY_MAX)
153                 tab = rtnl_msg_handlers[protocol];
154         else
155                 tab = NULL;
156 
157         if (tab == NULL || tab[msgindex].calcit == NULL)
158                 tab = rtnl_msg_handlers[PF_UNSPEC];
159 
160         return tab ? tab[msgindex].calcit : NULL;
161 }
162 
163 /**
164  * __rtnl_register - Register a rtnetlink message type
165  * @protocol: Protocol family or PF_UNSPEC
166  * @msgtype: rtnetlink message type
167  * @doit: Function pointer called for each request message
168  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
169  * @calcit: Function pointer to calc size of dump message
170  *
171  * Registers the specified function pointers (at least one of them has
172  * to be non-NULL) to be called whenever a request message for the
173  * specified protocol family and message type is received.
174  *
175  * The special protocol family PF_UNSPEC may be used to define fallback
176  * function pointers for the case when no entry for the specific protocol
177  * family exists.
178  *
179  * Returns 0 on success or a negative error code.
180  */
181 int __rtnl_register(int protocol, int msgtype,
182                     rtnl_doit_func doit, rtnl_dumpit_func dumpit,
183                     rtnl_calcit_func calcit)
184 {
185         struct rtnl_link *tab;
186         int msgindex;
187 
188         BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
189         msgindex = rtm_msgindex(msgtype);
190 
191         tab = rtnl_msg_handlers[protocol];
192         if (tab == NULL) {
193                 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
194                 if (tab == NULL)
195                         return -ENOBUFS;
196 
197                 rtnl_msg_handlers[protocol] = tab;
198         }
199 
200         if (doit)
201                 tab[msgindex].doit = doit;
202 
203         if (dumpit)
204                 tab[msgindex].dumpit = dumpit;
205 
206         if (calcit)
207                 tab[msgindex].calcit = calcit;
208 
209         return 0;
210 }
211 EXPORT_SYMBOL_GPL(__rtnl_register);
212 
213 /**
214  * rtnl_register - Register a rtnetlink message type
215  *
216  * Identical to __rtnl_register() but panics on failure. This is useful
217  * as failure of this function is very unlikely, it can only happen due
218  * to lack of memory when allocating the chain to store all message
219  * handlers for a protocol. Meant for use in init functions where lack
220  * of memory implies no sense in continuing.
221  */
222 void rtnl_register(int protocol, int msgtype,
223                    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
224                    rtnl_calcit_func calcit)
225 {
226         if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
227                 panic("Unable to register rtnetlink message handler, "
228                       "protocol = %d, message type = %d\n",
229                       protocol, msgtype);
230 }
231 EXPORT_SYMBOL_GPL(rtnl_register);
232 
233 /**
234  * rtnl_unregister - Unregister a rtnetlink message type
235  * @protocol: Protocol family or PF_UNSPEC
236  * @msgtype: rtnetlink message type
237  *
238  * Returns 0 on success or a negative error code.
239  */
240 int rtnl_unregister(int protocol, int msgtype)
241 {
242         int msgindex;
243 
244         BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
245         msgindex = rtm_msgindex(msgtype);
246 
247         if (rtnl_msg_handlers[protocol] == NULL)
248                 return -ENOENT;
249 
250         rtnl_msg_handlers[protocol][msgindex].doit = NULL;
251         rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
252 
253         return 0;
254 }
255 EXPORT_SYMBOL_GPL(rtnl_unregister);
256 
257 /**
258  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
259  * @protocol : Protocol family or PF_UNSPEC
260  *
261  * Identical to calling rtnl_unregster() for all registered message types
262  * of a certain protocol family.
263  */
264 void rtnl_unregister_all(int protocol)
265 {
266         BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
267 
268         kfree(rtnl_msg_handlers[protocol]);
269         rtnl_msg_handlers[protocol] = NULL;
270 }
271 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
272 
273 static LIST_HEAD(link_ops);
274 
275 /**
276  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
277  * @ops: struct rtnl_link_ops * to register
278  *
279  * The caller must hold the rtnl_mutex. This function should be used
280  * by drivers that create devices during module initialization. It
281  * must be called before registering the devices.
282  *
283  * Returns 0 on success or a negative error code.
284  */
285 int __rtnl_link_register(struct rtnl_link_ops *ops)
286 {
287         if (!ops->dellink)
288                 ops->dellink = unregister_netdevice_queue;
289 
290         list_add_tail(&ops->list, &link_ops);
291         return 0;
292 }
293 EXPORT_SYMBOL_GPL(__rtnl_link_register);
294 
295 /**
296  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
297  * @ops: struct rtnl_link_ops * to register
298  *
299  * Returns 0 on success or a negative error code.
300  */
301 int rtnl_link_register(struct rtnl_link_ops *ops)
302 {
303         int err;
304 
305         rtnl_lock();
306         err = __rtnl_link_register(ops);
307         rtnl_unlock();
308         return err;
309 }
310 EXPORT_SYMBOL_GPL(rtnl_link_register);
311 
312 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
313 {
314         struct net_device *dev;
315         LIST_HEAD(list_kill);
316 
317         for_each_netdev(net, dev) {
318                 if (dev->rtnl_link_ops == ops)
319                         ops->dellink(dev, &list_kill);
320         }
321         unregister_netdevice_many(&list_kill);
322 }
323 
324 /**
325  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
326  * @ops: struct rtnl_link_ops * to unregister
327  *
328  * The caller must hold the rtnl_mutex.
329  */
330 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
331 {
332         struct net *net;
333 
334         for_each_net(net) {
335                 __rtnl_kill_links(net, ops);
336         }
337         list_del(&ops->list);
338 }
339 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
340 
341 /**
342  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
343  * @ops: struct rtnl_link_ops * to unregister
344  */
345 void rtnl_link_unregister(struct rtnl_link_ops *ops)
346 {
347         rtnl_lock();
348         __rtnl_link_unregister(ops);
349         rtnl_unlock();
350 }
351 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
352 
353 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
354 {
355         const struct rtnl_link_ops *ops;
356 
357         list_for_each_entry(ops, &link_ops, list) {
358                 if (!strcmp(ops->kind, kind))
359                         return ops;
360         }
361         return NULL;
362 }
363 
364 static size_t rtnl_link_get_size(const struct net_device *dev)
365 {
366         const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
367         size_t size;
368 
369         if (!ops)
370                 return 0;
371 
372         size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
373                nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
374 
375         if (ops->get_size)
376                 /* IFLA_INFO_DATA + nested data */
377                 size += nla_total_size(sizeof(struct nlattr)) +
378                         ops->get_size(dev);
379 
380         if (ops->get_xstats_size)
381                 /* IFLA_INFO_XSTATS */
382                 size += nla_total_size(ops->get_xstats_size(dev));
383 
384         return size;
385 }
386 
387 static LIST_HEAD(rtnl_af_ops);
388 
389 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
390 {
391         const struct rtnl_af_ops *ops;
392 
393         list_for_each_entry(ops, &rtnl_af_ops, list) {
394                 if (ops->family == family)
395                         return ops;
396         }
397 
398         return NULL;
399 }
400 
401 /**
402  * __rtnl_af_register - Register rtnl_af_ops with rtnetlink.
403  * @ops: struct rtnl_af_ops * to register
404  *
405  * The caller must hold the rtnl_mutex.
406  *
407  * Returns 0 on success or a negative error code.
408  */
409 int __rtnl_af_register(struct rtnl_af_ops *ops)
410 {
411         list_add_tail(&ops->list, &rtnl_af_ops);
412         return 0;
413 }
414 EXPORT_SYMBOL_GPL(__rtnl_af_register);
415 
416 /**
417  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
418  * @ops: struct rtnl_af_ops * to register
419  *
420  * Returns 0 on success or a negative error code.
421  */
422 int rtnl_af_register(struct rtnl_af_ops *ops)
423 {
424         int err;
425 
426         rtnl_lock();
427         err = __rtnl_af_register(ops);
428         rtnl_unlock();
429         return err;
430 }
431 EXPORT_SYMBOL_GPL(rtnl_af_register);
432 
433 /**
434  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
435  * @ops: struct rtnl_af_ops * to unregister
436  *
437  * The caller must hold the rtnl_mutex.
438  */
439 void __rtnl_af_unregister(struct rtnl_af_ops *ops)
440 {
441         list_del(&ops->list);
442 }
443 EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
444 
445 /**
446  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
447  * @ops: struct rtnl_af_ops * to unregister
448  */
449 void rtnl_af_unregister(struct rtnl_af_ops *ops)
450 {
451         rtnl_lock();
452         __rtnl_af_unregister(ops);
453         rtnl_unlock();
454 }
455 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
456 
457 static size_t rtnl_link_get_af_size(const struct net_device *dev)
458 {
459         struct rtnl_af_ops *af_ops;
460         size_t size;
461 
462         /* IFLA_AF_SPEC */
463         size = nla_total_size(sizeof(struct nlattr));
464 
465         list_for_each_entry(af_ops, &rtnl_af_ops, list) {
466                 if (af_ops->get_link_af_size) {
467                         /* AF_* + nested data */
468                         size += nla_total_size(sizeof(struct nlattr)) +
469                                 af_ops->get_link_af_size(dev);
470                 }
471         }
472 
473         return size;
474 }
475 
476 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
477 {
478         const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
479         struct nlattr *linkinfo, *data;
480         int err = -EMSGSIZE;
481 
482         linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
483         if (linkinfo == NULL)
484                 goto out;
485 
486         if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
487                 goto err_cancel_link;
488         if (ops->fill_xstats) {
489                 err = ops->fill_xstats(skb, dev);
490                 if (err < 0)
491                         goto err_cancel_link;
492         }
493         if (ops->fill_info) {
494                 data = nla_nest_start(skb, IFLA_INFO_DATA);
495                 if (data == NULL)
496                         goto err_cancel_link;
497                 err = ops->fill_info(skb, dev);
498                 if (err < 0)
499                         goto err_cancel_data;
500                 nla_nest_end(skb, data);
501         }
502 
503         nla_nest_end(skb, linkinfo);
504         return 0;
505 
506 err_cancel_data:
507         nla_nest_cancel(skb, data);
508 err_cancel_link:
509         nla_nest_cancel(skb, linkinfo);
510 out:
511         return err;
512 }
513 
514 static const int rtm_min[RTM_NR_FAMILIES] =
515 {
516         [RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
517         [RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
518         [RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
519         [RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
520         [RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
521         [RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
522         [RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
523         [RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
524         [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
525         [RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
526 };
527 
528 static const int rta_max[RTM_NR_FAMILIES] =
529 {
530         [RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
531         [RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
532         [RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
533         [RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
534         [RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
535         [RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
536         [RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
537         [RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
538 };
539 
540 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
541 {
542         struct rtattr *rta;
543         int size = RTA_LENGTH(attrlen);
544 
545         rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size));
546         rta->rta_type = attrtype;
547         rta->rta_len = size;
548         memcpy(RTA_DATA(rta), data, attrlen);
549         memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
550 }
551 EXPORT_SYMBOL(__rta_fill);
552 
553 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
554 {
555         struct sock *rtnl = net->rtnl;
556         int err = 0;
557 
558         NETLINK_CB(skb).dst_group = group;
559         if (echo)
560                 atomic_inc(&skb->users);
561         netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
562         if (echo)
563                 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
564         return err;
565 }
566 
567 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
568 {
569         struct sock *rtnl = net->rtnl;
570 
571         return nlmsg_unicast(rtnl, skb, pid);
572 }
573 EXPORT_SYMBOL(rtnl_unicast);
574 
575 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
576                  struct nlmsghdr *nlh, gfp_t flags)
577 {
578         struct sock *rtnl = net->rtnl;
579         int report = 0;
580 
581         if (nlh)
582                 report = nlmsg_report(nlh);
583 
584         nlmsg_notify(rtnl, skb, pid, group, report, flags);
585 }
586 EXPORT_SYMBOL(rtnl_notify);
587 
588 void rtnl_set_sk_err(struct net *net, u32 group, int error)
589 {
590         struct sock *rtnl = net->rtnl;
591 
592         netlink_set_err(rtnl, 0, group, error);
593 }
594 EXPORT_SYMBOL(rtnl_set_sk_err);
595 
596 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
597 {
598         struct nlattr *mx;
599         int i, valid = 0;
600 
601         mx = nla_nest_start(skb, RTA_METRICS);
602         if (mx == NULL)
603                 return -ENOBUFS;
604 
605         for (i = 0; i < RTAX_MAX; i++) {
606                 if (metrics[i]) {
607                         valid++;
608                         NLA_PUT_U32(skb, i+1, metrics[i]);
609                 }
610         }
611 
612         if (!valid) {
613                 nla_nest_cancel(skb, mx);
614                 return 0;
615         }
616 
617         return nla_nest_end(skb, mx);
618 
619 nla_put_failure:
620         nla_nest_cancel(skb, mx);
621         return -EMSGSIZE;
622 }
623 EXPORT_SYMBOL(rtnetlink_put_metrics);
624 
625 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
626                        u32 ts, u32 tsage, long expires, u32 error)
627 {
628         struct rta_cacheinfo ci = {
629                 .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
630                 .rta_used = dst->__use,
631                 .rta_clntref = atomic_read(&(dst->__refcnt)),
632                 .rta_error = error,
633                 .rta_id =  id,
634                 .rta_ts = ts,
635                 .rta_tsage = tsage,
636         };
637 
638         if (expires)
639                 ci.rta_expires = jiffies_to_clock_t(expires);
640 
641         return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
642 }
643 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
644 
645 static void set_operstate(struct net_device *dev, unsigned char transition)
646 {
647         unsigned char operstate = dev->operstate;
648 
649         switch (transition) {
650         case IF_OPER_UP:
651                 if ((operstate == IF_OPER_DORMANT ||
652                      operstate == IF_OPER_UNKNOWN) &&
653                     !netif_dormant(dev))
654                         operstate = IF_OPER_UP;
655                 break;
656 
657         case IF_OPER_DORMANT:
658                 if (operstate == IF_OPER_UP ||
659                     operstate == IF_OPER_UNKNOWN)
660                         operstate = IF_OPER_DORMANT;
661                 break;
662         }
663 
664         if (dev->operstate != operstate) {
665                 write_lock_bh(&dev_base_lock);
666                 dev->operstate = operstate;
667                 write_unlock_bh(&dev_base_lock);
668                 netdev_state_change(dev);
669         }
670 }
671 
672 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
673 {
674         return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
675                (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
676 }
677 
678 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
679                                            const struct ifinfomsg *ifm)
680 {
681         unsigned int flags = ifm->ifi_flags;
682 
683         /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
684         if (ifm->ifi_change)
685                 flags = (flags & ifm->ifi_change) |
686                         (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
687 
688         return flags;
689 }
690 
691 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
692                                  const struct rtnl_link_stats64 *b)
693 {
694         a->rx_packets = b->rx_packets;
695         a->tx_packets = b->tx_packets;
696         a->rx_bytes = b->rx_bytes;
697         a->tx_bytes = b->tx_bytes;
698         a->rx_errors = b->rx_errors;
699         a->tx_errors = b->tx_errors;
700         a->rx_dropped = b->rx_dropped;
701         a->tx_dropped = b->tx_dropped;
702 
703         a->multicast = b->multicast;
704         a->collisions = b->collisions;
705 
706         a->rx_length_errors = b->rx_length_errors;
707         a->rx_over_errors = b->rx_over_errors;
708         a->rx_crc_errors = b->rx_crc_errors;
709         a->rx_frame_errors = b->rx_frame_errors;
710         a->rx_fifo_errors = b->rx_fifo_errors;
711         a->rx_missed_errors = b->rx_missed_errors;
712 
713         a->tx_aborted_errors = b->tx_aborted_errors;
714         a->tx_carrier_errors = b->tx_carrier_errors;
715         a->tx_fifo_errors = b->tx_fifo_errors;
716         a->tx_heartbeat_errors = b->tx_heartbeat_errors;
717         a->tx_window_errors = b->tx_window_errors;
718 
719         a->rx_compressed = b->rx_compressed;
720         a->tx_compressed = b->tx_compressed;
721 }
722 
723 static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
724 {
725         memcpy(v, b, sizeof(*b));
726 }
727 
728 /* All VF info */
729 static inline int rtnl_vfinfo_size(const struct net_device *dev,
730                                    u32 ext_filter_mask)
731 {
732         if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
733             (ext_filter_mask & RTEXT_FILTER_VF)) {
734                 int num_vfs = dev_num_vf(dev->dev.parent);
735                 size_t size = nla_total_size(sizeof(struct nlattr));
736                 size += nla_total_size(num_vfs * sizeof(struct nlattr));
737                 size += num_vfs *
738                         (nla_total_size(sizeof(struct ifla_vf_mac)) +
739                          nla_total_size(sizeof(struct ifla_vf_vlan)) +
740                          nla_total_size(sizeof(struct ifla_vf_tx_rate)));
741                 return size;
742         } else
743                 return 0;
744 }
745 
746 static size_t rtnl_port_size(const struct net_device *dev)
747 {
748         size_t port_size = nla_total_size(4)            /* PORT_VF */
749                 + nla_total_size(PORT_PROFILE_MAX)      /* PORT_PROFILE */
750                 + nla_total_size(sizeof(struct ifla_port_vsi))
751                                                         /* PORT_VSI_TYPE */
752                 + nla_total_size(PORT_UUID_MAX)         /* PORT_INSTANCE_UUID */
753                 + nla_total_size(PORT_UUID_MAX)         /* PORT_HOST_UUID */
754                 + nla_total_size(1)                     /* PROT_VDP_REQUEST */
755                 + nla_total_size(2);                    /* PORT_VDP_RESPONSE */
756         size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
757         size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
758                 + port_size;
759         size_t port_self_size = nla_total_size(sizeof(struct nlattr))
760                 + port_size;
761 
762         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
763                 return 0;
764         if (dev_num_vf(dev->dev.parent))
765                 return port_self_size + vf_ports_size +
766                         vf_port_size * dev_num_vf(dev->dev.parent);
767         else
768                 return port_self_size;
769 }
770 
771 static noinline size_t if_nlmsg_size(const struct net_device *dev,
772                                      u32 ext_filter_mask)
773 {
774         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
775                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
776                + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
777                + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
778                + nla_total_size(sizeof(struct rtnl_link_ifmap))
779                + nla_total_size(sizeof(struct rtnl_link_stats))
780                + nla_total_size(sizeof(struct rtnl_link_stats64))
781                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
782                + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
783                + nla_total_size(4) /* IFLA_TXQLEN */
784                + nla_total_size(4) /* IFLA_WEIGHT */
785                + nla_total_size(4) /* IFLA_MTU */
786                + nla_total_size(4) /* IFLA_LINK */
787                + nla_total_size(4) /* IFLA_MASTER */
788                + nla_total_size(1) /* IFLA_OPERSTATE */
789                + nla_total_size(1) /* IFLA_LINKMODE */
790                + nla_total_size(ext_filter_mask
791                                 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
792                + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
793                + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
794                + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
795                + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
796 }
797 
798 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
799 {
800         struct nlattr *vf_ports;
801         struct nlattr *vf_port;
802         int vf;
803         int err;
804 
805         vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
806         if (!vf_ports)
807                 return -EMSGSIZE;
808 
809         for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
810                 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
811                 if (!vf_port)
812                         goto nla_put_failure;
813                 NLA_PUT_U32(skb, IFLA_PORT_VF, vf);
814                 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
815                 if (err == -EMSGSIZE)
816                         goto nla_put_failure;
817                 if (err) {
818                         nla_nest_cancel(skb, vf_port);
819                         continue;
820                 }
821                 nla_nest_end(skb, vf_port);
822         }
823 
824         nla_nest_end(skb, vf_ports);
825 
826         return 0;
827 
828 nla_put_failure:
829         nla_nest_cancel(skb, vf_ports);
830         return -EMSGSIZE;
831 }
832 
833 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
834 {
835         struct nlattr *port_self;
836         int err;
837 
838         port_self = nla_nest_start(skb, IFLA_PORT_SELF);
839         if (!port_self)
840                 return -EMSGSIZE;
841 
842         err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
843         if (err) {
844                 nla_nest_cancel(skb, port_self);
845                 return (err == -EMSGSIZE) ? err : 0;
846         }
847 
848         nla_nest_end(skb, port_self);
849 
850         return 0;
851 }
852 
853 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
854 {
855         int err;
856 
857         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
858                 return 0;
859 
860         err = rtnl_port_self_fill(skb, dev);
861         if (err)
862                 return err;
863 
864         if (dev_num_vf(dev->dev.parent)) {
865                 err = rtnl_vf_ports_fill(skb, dev);
866                 if (err)
867                         return err;
868         }
869 
870         return 0;
871 }
872 
873 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
874                             int type, u32 pid, u32 seq, u32 change,
875                             unsigned int flags, u32 ext_filter_mask)
876 {
877         struct ifinfomsg *ifm;
878         struct nlmsghdr *nlh;
879         struct rtnl_link_stats64 temp;
880         const struct rtnl_link_stats64 *stats;
881         struct nlattr *attr, *af_spec;
882         struct rtnl_af_ops *af_ops;
883 
884         ASSERT_RTNL();
885         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
886         if (nlh == NULL)
887                 return -EMSGSIZE;
888 
889         ifm = nlmsg_data(nlh);
890         ifm->ifi_family = AF_UNSPEC;
891         ifm->__ifi_pad = 0;
892         ifm->ifi_type = dev->type;
893         ifm->ifi_index = dev->ifindex;
894         ifm->ifi_flags = dev_get_flags(dev);
895         ifm->ifi_change = change;
896 
897         NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
898         NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
899         NLA_PUT_U8(skb, IFLA_OPERSTATE,
900                    netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
901         NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
902         NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
903         NLA_PUT_U32(skb, IFLA_GROUP, dev->group);
904 
905         if (dev->ifindex != dev->iflink)
906                 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
907 
908         if (dev->master)
909                 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
910 
911         if (dev->qdisc)
912                 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id);
913 
914         if (dev->ifalias)
915                 NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
916 
917         if (1) {
918                 struct rtnl_link_ifmap map = {
919                         .mem_start   = dev->mem_start,
920                         .mem_end     = dev->mem_end,
921                         .base_addr   = dev->base_addr,
922                         .irq         = dev->irq,
923                         .dma         = dev->dma,
924                         .port        = dev->if_port,
925                 };
926                 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
927         }
928 
929         if (dev->addr_len) {
930                 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
931                 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
932         }
933 
934         attr = nla_reserve(skb, IFLA_STATS,
935                         sizeof(struct rtnl_link_stats));
936         if (attr == NULL)
937                 goto nla_put_failure;
938 
939         stats = dev_get_stats(dev, &temp);
940         copy_rtnl_link_stats(nla_data(attr), stats);
941 
942         attr = nla_reserve(skb, IFLA_STATS64,
943                         sizeof(struct rtnl_link_stats64));
944         if (attr == NULL)
945                 goto nla_put_failure;
946         copy_rtnl_link_stats64(nla_data(attr), stats);
947 
948         if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF))
949                 NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent));
950 
951         if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
952             && (ext_filter_mask & RTEXT_FILTER_VF)) {
953                 int i;
954 
955                 struct nlattr *vfinfo, *vf;
956                 int num_vfs = dev_num_vf(dev->dev.parent);
957 
958                 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
959                 if (!vfinfo)
960                         goto nla_put_failure;
961                 for (i = 0; i < num_vfs; i++) {
962                         struct ifla_vf_info ivi;
963                         struct ifla_vf_mac vf_mac;
964                         struct ifla_vf_vlan vf_vlan;
965                         struct ifla_vf_tx_rate vf_tx_rate;
966                         memset(ivi.mac, 0, sizeof(ivi.mac));
967                         if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
968                                 break;
969                         vf_mac.vf = vf_vlan.vf = vf_tx_rate.vf = ivi.vf;
970                         memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
971                         vf_vlan.vlan = ivi.vlan;
972                         vf_vlan.qos = ivi.qos;
973                         vf_tx_rate.rate = ivi.tx_rate;
974                         vf = nla_nest_start(skb, IFLA_VF_INFO);
975                         if (!vf) {
976                                 nla_nest_cancel(skb, vfinfo);
977                                 goto nla_put_failure;
978                         }
979                         NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac);
980                         NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan);
981                         NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), &vf_tx_rate);
982                         nla_nest_end(skb, vf);
983                 }
984                 nla_nest_end(skb, vfinfo);
985         }
986 
987         if (rtnl_port_fill(skb, dev))
988                 goto nla_put_failure;
989 
990         if (dev->rtnl_link_ops) {
991                 if (rtnl_link_fill(skb, dev) < 0)
992                         goto nla_put_failure;
993         }
994 
995         if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
996                 goto nla_put_failure;
997 
998         list_for_each_entry(af_ops, &rtnl_af_ops, list) {
999                 if (af_ops->fill_link_af) {
1000                         struct nlattr *af;
1001                         int err;
1002 
1003                         if (!(af = nla_nest_start(skb, af_ops->family)))
1004                                 goto nla_put_failure;
1005 
1006                         err = af_ops->fill_link_af(skb, dev);
1007 
1008                         /*
1009                          * Caller may return ENODATA to indicate that there
1010                          * was no data to be dumped. This is not an error, it
1011                          * means we should trim the attribute header and
1012                          * continue.
1013                          */
1014                         if (err == -ENODATA)
1015                                 nla_nest_cancel(skb, af);
1016                         else if (err < 0)
1017                                 goto nla_put_failure;
1018 
1019                         nla_nest_end(skb, af);
1020                 }
1021         }
1022 
1023         nla_nest_end(skb, af_spec);
1024 
1025         return nlmsg_end(skb, nlh);
1026 
1027 nla_put_failure:
1028         nlmsg_cancel(skb, nlh);
1029         return -EMSGSIZE;
1030 }
1031 
1032 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1033 {
1034         struct net *net = sock_net(skb->sk);
1035         int h, s_h;
1036         int idx = 0, s_idx;
1037         struct net_device *dev;
1038         struct hlist_head *head;
1039         struct hlist_node *node;
1040         struct nlattr *tb[IFLA_MAX+1];
1041         u32 ext_filter_mask = 0;
1042 
1043         s_h = cb->args[0];
1044         s_idx = cb->args[1];
1045 
1046         rcu_read_lock();
1047 
1048         if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
1049                         ifla_policy) >= 0) {
1050 
1051                 if (tb[IFLA_EXT_MASK])
1052                         ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1053         }
1054 
1055         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1056                 idx = 0;
1057                 head = &net->dev_index_head[h];
1058                 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
1059                         if (idx < s_idx)
1060                                 goto cont;
1061                         if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1062                                              NETLINK_CB(cb->skb).pid,
1063                                              cb->nlh->nlmsg_seq, 0,
1064                                              NLM_F_MULTI,
1065                                              ext_filter_mask) <= 0)
1066                                 goto out;
1067 cont:
1068                         idx++;
1069                 }
1070         }
1071 out:
1072         rcu_read_unlock();
1073         cb->args[1] = idx;
1074         cb->args[0] = h;
1075 
1076         return skb->len;
1077 }
1078 
1079 const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1080         [IFLA_IFNAME]           = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1081         [IFLA_ADDRESS]          = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1082         [IFLA_BROADCAST]        = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1083         [IFLA_MAP]              = { .len = sizeof(struct rtnl_link_ifmap) },
1084         [IFLA_MTU]              = { .type = NLA_U32 },
1085         [IFLA_LINK]             = { .type = NLA_U32 },
1086         [IFLA_MASTER]           = { .type = NLA_U32 },
1087         [IFLA_TXQLEN]           = { .type = NLA_U32 },
1088         [IFLA_WEIGHT]           = { .type = NLA_U32 },
1089         [IFLA_OPERSTATE]        = { .type = NLA_U8 },
1090         [IFLA_LINKMODE]         = { .type = NLA_U8 },
1091         [IFLA_LINKINFO]         = { .type = NLA_NESTED },
1092         [IFLA_NET_NS_PID]       = { .type = NLA_U32 },
1093         [IFLA_NET_NS_FD]        = { .type = NLA_U32 },
1094         [IFLA_IFALIAS]          = { .type = NLA_STRING, .len = IFALIASZ-1 },
1095         [IFLA_VFINFO_LIST]      = {. type = NLA_NESTED },
1096         [IFLA_VF_PORTS]         = { .type = NLA_NESTED },
1097         [IFLA_PORT_SELF]        = { .type = NLA_NESTED },
1098         [IFLA_AF_SPEC]          = { .type = NLA_NESTED },
1099         [IFLA_EXT_MASK]         = { .type = NLA_U32 },
1100 };
1101 EXPORT_SYMBOL(ifla_policy);
1102 
1103 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1104         [IFLA_INFO_KIND]        = { .type = NLA_STRING },
1105         [IFLA_INFO_DATA]        = { .type = NLA_NESTED },
1106 };
1107 
1108 static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1109         [IFLA_VF_INFO]          = { .type = NLA_NESTED },
1110 };
1111 
1112 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1113         [IFLA_VF_MAC]           = { .type = NLA_BINARY,
1114                                     .len = sizeof(struct ifla_vf_mac) },
1115         [IFLA_VF_VLAN]          = { .type = NLA_BINARY,
1116                                     .len = sizeof(struct ifla_vf_vlan) },
1117         [IFLA_VF_TX_RATE]       = { .type = NLA_BINARY,
1118                                     .len = sizeof(struct ifla_vf_tx_rate) },
1119 };
1120 
1121 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1122         [IFLA_PORT_VF]          = { .type = NLA_U32 },
1123         [IFLA_PORT_PROFILE]     = { .type = NLA_STRING,
1124                                     .len = PORT_PROFILE_MAX },
1125         [IFLA_PORT_VSI_TYPE]    = { .type = NLA_BINARY,
1126                                     .len = sizeof(struct ifla_port_vsi)},
1127         [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1128                                       .len = PORT_UUID_MAX },
1129         [IFLA_PORT_HOST_UUID]   = { .type = NLA_STRING,
1130                                     .len = PORT_UUID_MAX },
1131         [IFLA_PORT_REQUEST]     = { .type = NLA_U8, },
1132         [IFLA_PORT_RESPONSE]    = { .type = NLA_U16, },
1133 };
1134 
1135 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1136 {
1137         struct net *net;
1138         /* Examine the link attributes and figure out which
1139          * network namespace we are talking about.
1140          */
1141         if (tb[IFLA_NET_NS_PID])
1142                 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1143         else if (tb[IFLA_NET_NS_FD])
1144                 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1145         else
1146                 net = get_net(src_net);
1147         return net;
1148 }
1149 EXPORT_SYMBOL(rtnl_link_get_net);
1150 
1151 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1152 {
1153         if (dev) {
1154                 if (tb[IFLA_ADDRESS] &&
1155                     nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1156                         return -EINVAL;
1157 
1158                 if (tb[IFLA_BROADCAST] &&
1159                     nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1160                         return -EINVAL;
1161         }
1162 
1163         if (tb[IFLA_AF_SPEC]) {
1164                 struct nlattr *af;
1165                 int rem, err;
1166 
1167                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1168                         const struct rtnl_af_ops *af_ops;
1169 
1170                         if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1171                                 return -EAFNOSUPPORT;
1172 
1173                         if (!af_ops->set_link_af)
1174                                 return -EOPNOTSUPP;
1175 
1176                         if (af_ops->validate_link_af) {
1177                                 err = af_ops->validate_link_af(dev, af);
1178                                 if (err < 0)
1179                                         return err;
1180                         }
1181                 }
1182         }
1183 
1184         return 0;
1185 }
1186 
1187 static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1188 {
1189         int rem, err = -EINVAL;
1190         struct nlattr *vf;
1191         const struct net_device_ops *ops = dev->netdev_ops;
1192 
1193         nla_for_each_nested(vf, attr, rem) {
1194                 switch (nla_type(vf)) {
1195                 case IFLA_VF_MAC: {
1196                         struct ifla_vf_mac *ivm;
1197                         ivm = nla_data(vf);
1198                         err = -EOPNOTSUPP;
1199                         if (ops->ndo_set_vf_mac)
1200                                 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1201                                                           ivm->mac);
1202                         break;
1203                 }
1204                 case IFLA_VF_VLAN: {
1205                         struct ifla_vf_vlan *ivv;
1206                         ivv = nla_data(vf);
1207                         err = -EOPNOTSUPP;
1208                         if (ops->ndo_set_vf_vlan)
1209                                 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1210                                                            ivv->vlan,
1211                                                            ivv->qos);
1212                         break;
1213                 }
1214                 case IFLA_VF_TX_RATE: {
1215                         struct ifla_vf_tx_rate *ivt;
1216                         ivt = nla_data(vf);
1217                         err = -EOPNOTSUPP;
1218                         if (ops->ndo_set_vf_tx_rate)
1219                                 err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1220                                                               ivt->rate);
1221                         break;
1222                 }
1223                 default:
1224                         err = -EINVAL;
1225                         break;
1226                 }
1227                 if (err)
1228                         break;
1229         }
1230         return err;
1231 }
1232 
1233 static int do_set_master(struct net_device *dev, int ifindex)
1234 {
1235         struct net_device *master_dev;
1236         const struct net_device_ops *ops;
1237         int err;
1238 
1239         if (dev->master) {
1240                 if (dev->master->ifindex == ifindex)
1241                         return 0;
1242                 ops = dev->master->netdev_ops;
1243                 if (ops->ndo_del_slave) {
1244                         err = ops->ndo_del_slave(dev->master, dev);
1245                         if (err)
1246                                 return err;
1247                 } else {
1248                         return -EOPNOTSUPP;
1249                 }
1250         }
1251 
1252         if (ifindex) {
1253                 master_dev = __dev_get_by_index(dev_net(dev), ifindex);
1254                 if (!master_dev)
1255                         return -EINVAL;
1256                 ops = master_dev->netdev_ops;
1257                 if (ops->ndo_add_slave) {
1258                         err = ops->ndo_add_slave(master_dev, dev);
1259                         if (err)
1260                                 return err;
1261                 } else {
1262                         return -EOPNOTSUPP;
1263                 }
1264         }
1265         return 0;
1266 }
1267 
1268 static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
1269                       struct nlattr **tb, char *ifname, int modified)
1270 {
1271         const struct net_device_ops *ops = dev->netdev_ops;
1272         int send_addr_notify = 0;
1273         int err;
1274 
1275         if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1276                 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1277                 if (IS_ERR(net)) {
1278                         err = PTR_ERR(net);
1279                         goto errout;
1280                 }
1281                 err = dev_change_net_namespace(dev, net, ifname);
1282                 put_net(net);
1283                 if (err)
1284                         goto errout;
1285                 modified = 1;
1286         }
1287 
1288         if (tb[IFLA_MAP]) {
1289                 struct rtnl_link_ifmap *u_map;
1290                 struct ifmap k_map;
1291 
1292                 if (!ops->ndo_set_config) {
1293                         err = -EOPNOTSUPP;
1294                         goto errout;
1295                 }
1296 
1297                 if (!netif_device_present(dev)) {
1298                         err = -ENODEV;
1299                         goto errout;
1300                 }
1301 
1302                 u_map = nla_data(tb[IFLA_MAP]);
1303                 k_map.mem_start = (unsigned long) u_map->mem_start;
1304                 k_map.mem_end = (unsigned long) u_map->mem_end;
1305                 k_map.base_addr = (unsigned short) u_map->base_addr;
1306                 k_map.irq = (unsigned char) u_map->irq;
1307                 k_map.dma = (unsigned char) u_map->dma;
1308                 k_map.port = (unsigned char) u_map->port;
1309 
1310                 err = ops->ndo_set_config(dev, &k_map);
1311                 if (err < 0)
1312                         goto errout;
1313 
1314                 modified = 1;
1315         }
1316 
1317         if (tb[IFLA_ADDRESS]) {
1318                 struct sockaddr *sa;
1319                 int len;
1320 
1321                 if (!ops->ndo_set_mac_address) {
1322                         err = -EOPNOTSUPP;
1323                         goto errout;
1324                 }
1325 
1326                 if (!netif_device_present(dev)) {
1327                         err = -ENODEV;
1328                         goto errout;
1329                 }
1330 
1331                 len = sizeof(sa_family_t) + dev->addr_len;
1332                 sa = kmalloc(len, GFP_KERNEL);
1333                 if (!sa) {
1334                         err = -ENOMEM;
1335                         goto errout;
1336                 }
1337                 sa->sa_family = dev->type;
1338                 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1339                        dev->addr_len);
1340                 err = ops->ndo_set_mac_address(dev, sa);
1341                 kfree(sa);
1342                 if (err)
1343                         goto errout;
1344                 send_addr_notify = 1;
1345                 modified = 1;
1346                 add_device_randomness(dev->dev_addr, dev->addr_len);
1347         }
1348 
1349         if (tb[IFLA_MTU]) {
1350                 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1351                 if (err < 0)
1352                         goto errout;
1353                 modified = 1;
1354         }
1355 
1356         if (tb[IFLA_GROUP]) {
1357                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1358                 modified = 1;
1359         }
1360 
1361         /*
1362          * Interface selected by interface index but interface
1363          * name provided implies that a name change has been
1364          * requested.
1365          */
1366         if (ifm->ifi_index > 0 && ifname[0]) {
1367                 err = dev_change_name(dev, ifname);
1368                 if (err < 0)
1369                         goto errout;
1370                 modified = 1;
1371         }
1372 
1373         if (tb[IFLA_IFALIAS]) {
1374                 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1375                                     nla_len(tb[IFLA_IFALIAS]));
1376                 if (err < 0)
1377                         goto errout;
1378                 modified = 1;
1379         }
1380 
1381         if (tb[IFLA_BROADCAST]) {
1382                 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1383                 send_addr_notify = 1;
1384         }
1385 
1386         if (ifm->ifi_flags || ifm->ifi_change) {
1387                 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1388                 if (err < 0)
1389                         goto errout;
1390         }
1391 
1392         if (tb[IFLA_MASTER]) {
1393                 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1394                 if (err)
1395                         goto errout;
1396                 modified = 1;
1397         }
1398 
1399         if (tb[IFLA_TXQLEN])
1400                 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1401 
1402         if (tb[IFLA_OPERSTATE])
1403                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1404 
1405         if (tb[IFLA_LINKMODE]) {
1406                 write_lock_bh(&dev_base_lock);
1407                 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1408                 write_unlock_bh(&dev_base_lock);
1409         }
1410 
1411         if (tb[IFLA_VFINFO_LIST]) {
1412                 struct nlattr *attr;
1413                 int rem;
1414                 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1415                         if (nla_type(attr) != IFLA_VF_INFO) {
1416                                 err = -EINVAL;
1417                                 goto errout;
1418                         }
1419                         err = do_setvfinfo(dev, attr);
1420                         if (err < 0)
1421                                 goto errout;
1422                         modified = 1;
1423                 }
1424         }
1425         err = 0;
1426 
1427         if (tb[IFLA_VF_PORTS]) {
1428                 struct nlattr *port[IFLA_PORT_MAX+1];
1429                 struct nlattr *attr;
1430                 int vf;
1431                 int rem;
1432 
1433                 err = -EOPNOTSUPP;
1434                 if (!ops->ndo_set_vf_port)
1435                         goto errout;
1436 
1437                 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1438                         if (nla_type(attr) != IFLA_VF_PORT)
1439                                 continue;
1440                         err = nla_parse_nested(port, IFLA_PORT_MAX,
1441                                 attr, ifla_port_policy);
1442                         if (err < 0)
1443                                 goto errout;
1444                         if (!port[IFLA_PORT_VF]) {
1445                                 err = -EOPNOTSUPP;
1446                                 goto errout;
1447                         }
1448                         vf = nla_get_u32(port[IFLA_PORT_VF]);
1449                         err = ops->ndo_set_vf_port(dev, vf, port);
1450                         if (err < 0)
1451                                 goto errout;
1452                         modified = 1;
1453                 }
1454         }
1455         err = 0;
1456 
1457         if (tb[IFLA_PORT_SELF]) {
1458                 struct nlattr *port[IFLA_PORT_MAX+1];
1459 
1460                 err = nla_parse_nested(port, IFLA_PORT_MAX,
1461                         tb[IFLA_PORT_SELF], ifla_port_policy);
1462                 if (err < 0)
1463                         goto errout;
1464 
1465                 err = -EOPNOTSUPP;
1466                 if (ops->ndo_set_vf_port)
1467                         err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1468                 if (err < 0)
1469                         goto errout;
1470                 modified = 1;
1471         }
1472 
1473         if (tb[IFLA_AF_SPEC]) {
1474                 struct nlattr *af;
1475                 int rem;
1476 
1477                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1478                         const struct rtnl_af_ops *af_ops;
1479 
1480                         if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1481                                 BUG();
1482 
1483                         err = af_ops->set_link_af(dev, af);
1484                         if (err < 0)
1485                                 goto errout;
1486 
1487                         modified = 1;
1488                 }
1489         }
1490         err = 0;
1491 
1492 errout:
1493         if (err < 0 && modified && net_ratelimit())
1494                 printk(KERN_WARNING "A link change request failed with "
1495                        "some changes committed already. Interface %s may "
1496                        "have been left with an inconsistent configuration, "
1497                        "please check.\n", dev->name);
1498 
1499         if (send_addr_notify)
1500                 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1501         return err;
1502 }
1503 
1504 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1505 {
1506         struct net *net = sock_net(skb->sk);
1507         struct ifinfomsg *ifm;
1508         struct net_device *dev;
1509         int err;
1510         struct nlattr *tb[IFLA_MAX+1];
1511         char ifname[IFNAMSIZ];
1512 
1513         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1514         if (err < 0)
1515                 goto errout;
1516 
1517         if (tb[IFLA_IFNAME])
1518                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1519         else
1520                 ifname[0] = '\0';
1521 
1522         err = -EINVAL;
1523         ifm = nlmsg_data(nlh);
1524         if (ifm->ifi_index > 0)
1525                 dev = __dev_get_by_index(net, ifm->ifi_index);
1526         else if (tb[IFLA_IFNAME])
1527                 dev = __dev_get_by_name(net, ifname);
1528         else
1529                 goto errout;
1530 
1531         if (dev == NULL) {
1532                 err = -ENODEV;
1533                 goto errout;
1534         }
1535 
1536         err = validate_linkmsg(dev, tb);
1537         if (err < 0)
1538                 goto errout;
1539 
1540         err = do_setlink(dev, ifm, tb, ifname, 0);
1541 errout:
1542         return err;
1543 }
1544 
1545 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1546 {
1547         struct net *net = sock_net(skb->sk);
1548         const struct rtnl_link_ops *ops;
1549         struct net_device *dev;
1550         struct ifinfomsg *ifm;
1551         char ifname[IFNAMSIZ];
1552         struct nlattr *tb[IFLA_MAX+1];
1553         int err;
1554         LIST_HEAD(list_kill);
1555 
1556         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1557         if (err < 0)
1558                 return err;
1559 
1560         if (tb[IFLA_IFNAME])
1561                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1562 
1563         ifm = nlmsg_data(nlh);
1564         if (ifm->ifi_index > 0)
1565                 dev = __dev_get_by_index(net, ifm->ifi_index);
1566         else if (tb[IFLA_IFNAME])
1567                 dev = __dev_get_by_name(net, ifname);
1568         else
1569                 return -EINVAL;
1570 
1571         if (!dev)
1572                 return -ENODEV;
1573 
1574         ops = dev->rtnl_link_ops;
1575         if (!ops)
1576                 return -EOPNOTSUPP;
1577 
1578         ops->dellink(dev, &list_kill);
1579         unregister_netdevice_many(&list_kill);
1580         list_del(&list_kill);
1581         return 0;
1582 }
1583 
1584 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1585 {
1586         unsigned int old_flags;
1587         int err;
1588 
1589         old_flags = dev->flags;
1590         if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1591                 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1592                 if (err < 0)
1593                         return err;
1594         }
1595 
1596         dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
1597         rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1598 
1599         __dev_notify_flags(dev, old_flags);
1600         return 0;
1601 }
1602 EXPORT_SYMBOL(rtnl_configure_link);
1603 
1604 struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
1605         char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1606 {
1607         int err;
1608         struct net_device *dev;
1609         unsigned int num_queues = 1;
1610         unsigned int real_num_queues = 1;
1611 
1612         if (ops->get_tx_queues) {
1613                 err = ops->get_tx_queues(src_net, tb, &num_queues,
1614                                          &real_num_queues);
1615                 if (err)
1616                         goto err;
1617         }
1618         err = -ENOMEM;
1619         dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
1620         if (!dev)
1621                 goto err;
1622 
1623         dev_net_set(dev, net);
1624         dev->rtnl_link_ops = ops;
1625         dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
1626         dev->real_num_tx_queues = real_num_queues;
1627 
1628         if (tb[IFLA_MTU])
1629                 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1630         if (tb[IFLA_ADDRESS])
1631                 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1632                                 nla_len(tb[IFLA_ADDRESS]));
1633         if (tb[IFLA_BROADCAST])
1634                 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1635                                 nla_len(tb[IFLA_BROADCAST]));
1636         if (tb[IFLA_TXQLEN])
1637                 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1638         if (tb[IFLA_OPERSTATE])
1639                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1640         if (tb[IFLA_LINKMODE])
1641                 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1642         if (tb[IFLA_GROUP])
1643                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1644 
1645         return dev;
1646 
1647 err:
1648         return ERR_PTR(err);
1649 }
1650 EXPORT_SYMBOL(rtnl_create_link);
1651 
1652 static int rtnl_group_changelink(struct net *net, int group,
1653                 struct ifinfomsg *ifm,
1654                 struct nlattr **tb)
1655 {
1656         struct net_device *dev;
1657         int err;
1658 
1659         for_each_netdev(net, dev) {
1660                 if (dev->group == group) {
1661                         err = do_setlink(dev, ifm, tb, NULL, 0);
1662                         if (err < 0)
1663                                 return err;
1664                 }
1665         }
1666 
1667         return 0;
1668 }
1669 
1670 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1671 {
1672         struct net *net = sock_net(skb->sk);
1673         const struct rtnl_link_ops *ops;
1674         struct net_device *dev;
1675         struct ifinfomsg *ifm;
1676         char kind[MODULE_NAME_LEN];
1677         char ifname[IFNAMSIZ];
1678         struct nlattr *tb[IFLA_MAX+1];
1679         struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1680         int err;
1681 
1682 #ifdef CONFIG_MODULES
1683 replay:
1684 #endif
1685         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1686         if (err < 0)
1687                 return err;
1688 
1689         if (tb[IFLA_IFNAME])
1690                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1691         else
1692                 ifname[0] = '\0';
1693 
1694         ifm = nlmsg_data(nlh);
1695         if (ifm->ifi_index > 0)
1696                 dev = __dev_get_by_index(net, ifm->ifi_index);
1697         else {
1698                 if (ifname[0])
1699                         dev = __dev_get_by_name(net, ifname);
1700                 else
1701                         dev = NULL;
1702         }
1703 
1704         err = validate_linkmsg(dev, tb);
1705         if (err < 0)
1706                 return err;
1707 
1708         if (tb[IFLA_LINKINFO]) {
1709                 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1710                                        tb[IFLA_LINKINFO], ifla_info_policy);
1711                 if (err < 0)
1712                         return err;
1713         } else
1714                 memset(linkinfo, 0, sizeof(linkinfo));
1715 
1716         if (linkinfo[IFLA_INFO_KIND]) {
1717                 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1718                 ops = rtnl_link_ops_get(kind);
1719         } else {
1720                 kind[0] = '\0';
1721                 ops = NULL;
1722         }
1723 
1724         if (1) {
1725                 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
1726                 struct net *dest_net;
1727 
1728                 if (ops) {
1729                         if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1730                                 err = nla_parse_nested(attr, ops->maxtype,
1731                                                        linkinfo[IFLA_INFO_DATA],
1732                                                        ops->policy);
1733                                 if (err < 0)
1734                                         return err;
1735                                 data = attr;
1736                         }
1737                         if (ops->validate) {
1738                                 err = ops->validate(tb, data);
1739                                 if (err < 0)
1740                                         return err;
1741                         }
1742                 }
1743 
1744                 if (dev) {
1745                         int modified = 0;
1746 
1747                         if (nlh->nlmsg_flags & NLM_F_EXCL)
1748                                 return -EEXIST;
1749                         if (nlh->nlmsg_flags & NLM_F_REPLACE)
1750                                 return -EOPNOTSUPP;
1751 
1752                         if (linkinfo[IFLA_INFO_DATA]) {
1753                                 if (!ops || ops != dev->rtnl_link_ops ||
1754                                     !ops->changelink)
1755                                         return -EOPNOTSUPP;
1756 
1757                                 err = ops->changelink(dev, tb, data);
1758                                 if (err < 0)
1759                                         return err;
1760                                 modified = 1;
1761                         }
1762 
1763                         return do_setlink(dev, ifm, tb, ifname, modified);
1764                 }
1765 
1766                 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1767                         if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1768                                 return rtnl_group_changelink(net,
1769                                                 nla_get_u32(tb[IFLA_GROUP]),
1770                                                 ifm, tb);
1771                         return -ENODEV;
1772                 }
1773 
1774                 if (ifm->ifi_index)
1775                         return -EOPNOTSUPP;
1776                 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
1777                         return -EOPNOTSUPP;
1778 
1779                 if (!ops) {
1780 #ifdef CONFIG_MODULES
1781                         if (kind[0]) {
1782                                 __rtnl_unlock();
1783                                 request_module("rtnl-link-%s", kind);
1784                                 rtnl_lock();
1785                                 ops = rtnl_link_ops_get(kind);
1786                                 if (ops)
1787                                         goto replay;
1788                         }
1789 #endif
1790                         return -EOPNOTSUPP;
1791                 }
1792 
1793                 if (!ifname[0])
1794                         snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
1795 
1796                 dest_net = rtnl_link_get_net(net, tb);
1797                 if (IS_ERR(dest_net))
1798                         return PTR_ERR(dest_net);
1799 
1800                 dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
1801 
1802                 if (IS_ERR(dev))
1803                         err = PTR_ERR(dev);
1804                 else if (ops->newlink)
1805                         err = ops->newlink(net, dev, tb, data);
1806                 else
1807                         err = register_netdevice(dev);
1808 
1809                 if (err < 0 && !IS_ERR(dev))
1810                         free_netdev(dev);
1811                 if (err < 0)
1812                         goto out;
1813 
1814                 err = rtnl_configure_link(dev, ifm);
1815                 if (err < 0)
1816                         unregister_netdevice(dev);
1817 out:
1818                 put_net(dest_net);
1819                 return err;
1820         }
1821 }
1822 
1823 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1824 {
1825         struct net *net = sock_net(skb->sk);
1826         struct ifinfomsg *ifm;
1827         char ifname[IFNAMSIZ];
1828         struct nlattr *tb[IFLA_MAX+1];
1829         struct net_device *dev = NULL;
1830         struct sk_buff *nskb;
1831         int err;
1832         u32 ext_filter_mask = 0;
1833 
1834         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1835         if (err < 0)
1836                 return err;
1837 
1838         if (tb[IFLA_IFNAME])
1839                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1840 
1841         if (tb[IFLA_EXT_MASK])
1842                 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1843 
1844         ifm = nlmsg_data(nlh);
1845         if (ifm->ifi_index > 0)
1846                 dev = __dev_get_by_index(net, ifm->ifi_index);
1847         else if (tb[IFLA_IFNAME])
1848                 dev = __dev_get_by_name(net, ifname);
1849         else
1850                 return -EINVAL;
1851 
1852         if (dev == NULL)
1853                 return -ENODEV;
1854 
1855         nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
1856         if (nskb == NULL)
1857                 return -ENOBUFS;
1858 
1859         err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1860                                nlh->nlmsg_seq, 0, 0, ext_filter_mask);
1861         if (err < 0) {
1862                 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1863                 WARN_ON(err == -EMSGSIZE);
1864                 kfree_skb(nskb);
1865         } else
1866                 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
1867 
1868         return err;
1869 }
1870 
1871 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
1872 {
1873         struct net *net = sock_net(skb->sk);
1874         struct net_device *dev;
1875         struct nlattr *tb[IFLA_MAX+1];
1876         u32 ext_filter_mask = 0;
1877         u16 min_ifinfo_dump_size = 0;
1878 
1879         if (nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
1880                         ifla_policy) >= 0) {
1881                 if (tb[IFLA_EXT_MASK])
1882                         ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1883         }
1884 
1885         if (!ext_filter_mask)
1886                 return NLMSG_GOODSIZE;
1887         /*
1888          * traverse the list of net devices and compute the minimum
1889          * buffer size based upon the filter mask.
1890          */
1891         list_for_each_entry(dev, &net->dev_base_head, dev_list) {
1892                 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
1893                                              if_nlmsg_size(dev,
1894                                                            ext_filter_mask));
1895         }
1896 
1897         return min_ifinfo_dump_size;
1898 }
1899 
1900 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1901 {
1902         int idx;
1903         int s_idx = cb->family;
1904 
1905         if (s_idx == 0)
1906                 s_idx = 1;
1907         for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
1908                 int type = cb->nlh->nlmsg_type-RTM_BASE;
1909                 if (idx < s_idx || idx == PF_PACKET)
1910                         continue;
1911                 if (rtnl_msg_handlers[idx] == NULL ||
1912                     rtnl_msg_handlers[idx][type].dumpit == NULL)
1913                         continue;
1914                 if (idx > s_idx)
1915                         memset(&cb->args[0], 0, sizeof(cb->args));
1916                 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
1917                         break;
1918         }
1919         cb->family = idx;
1920 
1921         return skb->len;
1922 }
1923 
1924 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1925 {
1926         struct net *net = dev_net(dev);
1927         struct sk_buff *skb;
1928         int err = -ENOBUFS;
1929         size_t if_info_size;
1930 
1931         skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL);
1932         if (skb == NULL)
1933                 goto errout;
1934 
1935         err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
1936         if (err < 0) {
1937                 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1938                 WARN_ON(err == -EMSGSIZE);
1939                 kfree_skb(skb);
1940                 goto errout;
1941         }
1942         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1943         return;
1944 errout:
1945         if (err < 0)
1946                 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
1947 }
1948 
1949 /* Protected by RTNL sempahore.  */
1950 static struct rtattr **rta_buf;
1951 static int rtattr_max;
1952 
1953 /* Process one rtnetlink message. */
1954 
1955 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1956 {
1957         struct net *net = sock_net(skb->sk);
1958         rtnl_doit_func doit;
1959         int sz_idx, kind;
1960         int min_len;
1961         int family;
1962         int type;
1963         int err;
1964 
1965         type = nlh->nlmsg_type;
1966         if (type > RTM_MAX)
1967                 return -EOPNOTSUPP;
1968 
1969         type -= RTM_BASE;
1970 
1971         /* All the messages must have at least 1 byte length */
1972         if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
1973                 return 0;
1974 
1975         family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
1976         sz_idx = type>>2;
1977         kind = type&3;
1978 
1979         if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
1980                 return -EPERM;
1981 
1982         if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
1983                 struct sock *rtnl;
1984                 rtnl_dumpit_func dumpit;
1985                 rtnl_calcit_func calcit;
1986                 u16 min_dump_alloc = 0;
1987 
1988                 dumpit = rtnl_get_dumpit(family, type);
1989                 if (dumpit == NULL)
1990                         return -EOPNOTSUPP;
1991                 calcit = rtnl_get_calcit(family, type);
1992                 if (calcit)
1993                         min_dump_alloc = calcit(skb, nlh);
1994 
1995                 __rtnl_unlock();
1996                 rtnl = net->rtnl;
1997                 err = netlink_dump_start(rtnl, skb, nlh, dumpit,
1998                                          NULL, min_dump_alloc);
1999                 rtnl_lock();
2000                 return err;
2001         }
2002 
2003         memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
2004 
2005         min_len = rtm_min[sz_idx];
2006         if (nlh->nlmsg_len < min_len)
2007                 return -EINVAL;
2008 
2009         if (nlh->nlmsg_len > min_len) {
2010                 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
2011                 struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
2012 
2013                 while (RTA_OK(attr, attrlen)) {
2014                         unsigned int flavor = attr->rta_type & NLA_TYPE_MASK;
2015                         if (flavor) {
2016                                 if (flavor > rta_max[sz_idx])
2017                                         return -EINVAL;
2018                                 rta_buf[flavor-1] = attr;
2019                         }
2020                         attr = RTA_NEXT(attr, attrlen);
2021                 }
2022         }
2023 
2024         doit = rtnl_get_doit(family, type);
2025         if (doit == NULL)
2026                 return -EOPNOTSUPP;
2027 
2028         return doit(skb, nlh, (void *)&rta_buf[0]);
2029 }
2030 
2031 static void rtnetlink_rcv(struct sk_buff *skb)
2032 {
2033         rtnl_lock();
2034         netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
2035         rtnl_unlock();
2036 }
2037 
2038 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
2039 {
2040         struct net_device *dev = ptr;
2041 
2042         switch (event) {
2043         case NETDEV_UP:
2044         case NETDEV_DOWN:
2045         case NETDEV_PRE_UP:
2046         case NETDEV_POST_INIT:
2047         case NETDEV_REGISTER:
2048         case NETDEV_CHANGE:
2049         case NETDEV_PRE_TYPE_CHANGE:
2050         case NETDEV_GOING_DOWN:
2051         case NETDEV_UNREGISTER:
2052         case NETDEV_UNREGISTER_BATCH:
2053         case NETDEV_RELEASE:
2054         case NETDEV_JOIN:
2055                 break;
2056         default:
2057                 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
2058                 break;
2059         }
2060         return NOTIFY_DONE;
2061 }
2062 
2063 static struct notifier_block rtnetlink_dev_notifier = {
2064         .notifier_call  = rtnetlink_event,
2065 };
2066 
2067 
2068 static int __net_init rtnetlink_net_init(struct net *net)
2069 {
2070         struct sock *sk;
2071         sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
2072                                    rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
2073         if (!sk)
2074                 return -ENOMEM;
2075         net->rtnl = sk;
2076         return 0;
2077 }
2078 
2079 static void __net_exit rtnetlink_net_exit(struct net *net)
2080 {
2081         netlink_kernel_release(net->rtnl);
2082         net->rtnl = NULL;
2083 }
2084 
2085 static struct pernet_operations rtnetlink_net_ops = {
2086         .init = rtnetlink_net_init,
2087         .exit = rtnetlink_net_exit,
2088 };
2089 
2090 void __init rtnetlink_init(void)
2091 {
2092         int i;
2093 
2094         rtattr_max = 0;
2095         for (i = 0; i < ARRAY_SIZE(rta_max); i++)
2096                 if (rta_max[i] > rtattr_max)
2097                         rtattr_max = rta_max[i];
2098         rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
2099         if (!rta_buf)
2100                 panic("rtnetlink_init: cannot allocate rta_buf\n");
2101 
2102         if (register_pernet_subsys(&rtnetlink_net_ops))
2103                 panic("rtnetlink_init: cannot initialize rtnetlink\n");
2104 
2105         netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
2106         register_netdevice_notifier(&rtnetlink_dev_notifier);
2107 
2108         rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2109                       rtnl_dump_ifinfo, rtnl_calcit);
2110         rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2111         rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2112         rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
2113 
2114         rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2115         rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
2116 }
2117 
2118 

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