rtnl: fix info leak on RTM_GETLINK request for VF devices
[pandora-kernel.git] / net / core / rtnetlink.c
1 /*
2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
3  *              operating system.  INET is implemented using the  BSD Socket
4  *              interface as the means of communication with the user level.
5  *
6  *              Routing netlink socket interface: protocol independent part.
7  *
8  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  *
10  *              This program is free software; you can redistribute it and/or
11  *              modify it under the terms of the GNU General Public License
12  *              as published by the Free Software Foundation; either version
13  *              2 of the License, or (at your option) any later version.
14  *
15  *      Fixes:
16  *      Vitaly E. Lavrov                RTA_OK arithmetics was wrong.
17  */
18
19 #include <linux/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                          nla_total_size(sizeof(struct ifla_vf_spoofchk)));
742                 return size;
743         } else
744                 return 0;
745 }
746
747 static size_t rtnl_port_size(const struct net_device *dev)
748 {
749         size_t port_size = nla_total_size(4)            /* PORT_VF */
750                 + nla_total_size(PORT_PROFILE_MAX)      /* PORT_PROFILE */
751                 + nla_total_size(sizeof(struct ifla_port_vsi))
752                                                         /* PORT_VSI_TYPE */
753                 + nla_total_size(PORT_UUID_MAX)         /* PORT_INSTANCE_UUID */
754                 + nla_total_size(PORT_UUID_MAX)         /* PORT_HOST_UUID */
755                 + nla_total_size(1)                     /* PROT_VDP_REQUEST */
756                 + nla_total_size(2);                    /* PORT_VDP_RESPONSE */
757         size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
758         size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
759                 + port_size;
760         size_t port_self_size = nla_total_size(sizeof(struct nlattr))
761                 + port_size;
762
763         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
764                 return 0;
765         if (dev_num_vf(dev->dev.parent))
766                 return port_self_size + vf_ports_size +
767                         vf_port_size * dev_num_vf(dev->dev.parent);
768         else
769                 return port_self_size;
770 }
771
772 static noinline size_t if_nlmsg_size(const struct net_device *dev,
773                                      u32 ext_filter_mask)
774 {
775         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
776                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
777                + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
778                + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
779                + nla_total_size(sizeof(struct rtnl_link_ifmap))
780                + nla_total_size(sizeof(struct rtnl_link_stats))
781                + nla_total_size(sizeof(struct rtnl_link_stats64))
782                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
783                + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
784                + nla_total_size(4) /* IFLA_TXQLEN */
785                + nla_total_size(4) /* IFLA_WEIGHT */
786                + nla_total_size(4) /* IFLA_MTU */
787                + nla_total_size(4) /* IFLA_LINK */
788                + nla_total_size(4) /* IFLA_MASTER */
789                + nla_total_size(1) /* IFLA_OPERSTATE */
790                + nla_total_size(1) /* IFLA_LINKMODE */
791                + nla_total_size(ext_filter_mask
792                                 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
793                + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
794                + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
795                + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
796                + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
797 }
798
799 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
800 {
801         struct nlattr *vf_ports;
802         struct nlattr *vf_port;
803         int vf;
804         int err;
805
806         vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
807         if (!vf_ports)
808                 return -EMSGSIZE;
809
810         for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
811                 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
812                 if (!vf_port)
813                         goto nla_put_failure;
814                 NLA_PUT_U32(skb, IFLA_PORT_VF, vf);
815                 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
816                 if (err == -EMSGSIZE)
817                         goto nla_put_failure;
818                 if (err) {
819                         nla_nest_cancel(skb, vf_port);
820                         continue;
821                 }
822                 nla_nest_end(skb, vf_port);
823         }
824
825         nla_nest_end(skb, vf_ports);
826
827         return 0;
828
829 nla_put_failure:
830         nla_nest_cancel(skb, vf_ports);
831         return -EMSGSIZE;
832 }
833
834 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
835 {
836         struct nlattr *port_self;
837         int err;
838
839         port_self = nla_nest_start(skb, IFLA_PORT_SELF);
840         if (!port_self)
841                 return -EMSGSIZE;
842
843         err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
844         if (err) {
845                 nla_nest_cancel(skb, port_self);
846                 return (err == -EMSGSIZE) ? err : 0;
847         }
848
849         nla_nest_end(skb, port_self);
850
851         return 0;
852 }
853
854 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
855 {
856         int err;
857
858         if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
859                 return 0;
860
861         err = rtnl_port_self_fill(skb, dev);
862         if (err)
863                 return err;
864
865         if (dev_num_vf(dev->dev.parent)) {
866                 err = rtnl_vf_ports_fill(skb, dev);
867                 if (err)
868                         return err;
869         }
870
871         return 0;
872 }
873
874 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
875                             int type, u32 pid, u32 seq, u32 change,
876                             unsigned int flags, u32 ext_filter_mask)
877 {
878         struct ifinfomsg *ifm;
879         struct nlmsghdr *nlh;
880         struct rtnl_link_stats64 temp;
881         const struct rtnl_link_stats64 *stats;
882         struct nlattr *attr, *af_spec;
883         struct rtnl_af_ops *af_ops;
884
885         ASSERT_RTNL();
886         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
887         if (nlh == NULL)
888                 return -EMSGSIZE;
889
890         ifm = nlmsg_data(nlh);
891         ifm->ifi_family = AF_UNSPEC;
892         ifm->__ifi_pad = 0;
893         ifm->ifi_type = dev->type;
894         ifm->ifi_index = dev->ifindex;
895         ifm->ifi_flags = dev_get_flags(dev);
896         ifm->ifi_change = change;
897
898         NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
899         NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
900         NLA_PUT_U8(skb, IFLA_OPERSTATE,
901                    netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
902         NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
903         NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
904         NLA_PUT_U32(skb, IFLA_GROUP, dev->group);
905
906         if (dev->ifindex != dev->iflink)
907                 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
908
909         if (dev->master)
910                 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
911
912         if (dev->qdisc)
913                 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id);
914
915         if (dev->ifalias)
916                 NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
917
918         if (1) {
919                 struct rtnl_link_ifmap map = {
920                         .mem_start   = dev->mem_start,
921                         .mem_end     = dev->mem_end,
922                         .base_addr   = dev->base_addr,
923                         .irq         = dev->irq,
924                         .dma         = dev->dma,
925                         .port        = dev->if_port,
926                 };
927                 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
928         }
929
930         if (dev->addr_len) {
931                 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
932                 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
933         }
934
935         attr = nla_reserve(skb, IFLA_STATS,
936                         sizeof(struct rtnl_link_stats));
937         if (attr == NULL)
938                 goto nla_put_failure;
939
940         stats = dev_get_stats(dev, &temp);
941         copy_rtnl_link_stats(nla_data(attr), stats);
942
943         attr = nla_reserve(skb, IFLA_STATS64,
944                         sizeof(struct rtnl_link_stats64));
945         if (attr == NULL)
946                 goto nla_put_failure;
947         copy_rtnl_link_stats64(nla_data(attr), stats);
948
949         if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF))
950                 NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent));
951
952         if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
953             && (ext_filter_mask & RTEXT_FILTER_VF)) {
954                 int i;
955
956                 struct nlattr *vfinfo, *vf;
957                 int num_vfs = dev_num_vf(dev->dev.parent);
958
959                 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
960                 if (!vfinfo)
961                         goto nla_put_failure;
962                 for (i = 0; i < num_vfs; i++) {
963                         struct ifla_vf_info ivi;
964                         struct ifla_vf_mac vf_mac;
965                         struct ifla_vf_vlan vf_vlan;
966                         struct ifla_vf_tx_rate vf_tx_rate;
967                         struct ifla_vf_spoofchk vf_spoofchk;
968
969                         /*
970                          * Not all SR-IOV capable drivers support the
971                          * spoofcheck query.  Preset to -1 so the user
972                          * space tool can detect that the driver didn't
973                          * report anything.
974                          */
975                         ivi.spoofchk = -1;
976                         memset(ivi.mac, 0, sizeof(ivi.mac));
977                         if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
978                                 break;
979                         vf_mac.vf =
980                                 vf_vlan.vf =
981                                 vf_tx_rate.vf =
982                                 vf_spoofchk.vf = ivi.vf;
983
984                         memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
985                         vf_vlan.vlan = ivi.vlan;
986                         vf_vlan.qos = ivi.qos;
987                         vf_tx_rate.rate = ivi.tx_rate;
988                         vf_spoofchk.setting = ivi.spoofchk;
989                         vf = nla_nest_start(skb, IFLA_VF_INFO);
990                         if (!vf) {
991                                 nla_nest_cancel(skb, vfinfo);
992                                 goto nla_put_failure;
993                         }
994                         NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac);
995                         NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan);
996                         NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
997                                 &vf_tx_rate);
998                         NLA_PUT(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
999                                 &vf_spoofchk);
1000                         nla_nest_end(skb, vf);
1001                 }
1002                 nla_nest_end(skb, vfinfo);
1003         }
1004
1005         if (rtnl_port_fill(skb, dev))
1006                 goto nla_put_failure;
1007
1008         if (dev->rtnl_link_ops) {
1009                 if (rtnl_link_fill(skb, dev) < 0)
1010                         goto nla_put_failure;
1011         }
1012
1013         if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1014                 goto nla_put_failure;
1015
1016         list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1017                 if (af_ops->fill_link_af) {
1018                         struct nlattr *af;
1019                         int err;
1020
1021                         if (!(af = nla_nest_start(skb, af_ops->family)))
1022                                 goto nla_put_failure;
1023
1024                         err = af_ops->fill_link_af(skb, dev);
1025
1026                         /*
1027                          * Caller may return ENODATA to indicate that there
1028                          * was no data to be dumped. This is not an error, it
1029                          * means we should trim the attribute header and
1030                          * continue.
1031                          */
1032                         if (err == -ENODATA)
1033                                 nla_nest_cancel(skb, af);
1034                         else if (err < 0)
1035                                 goto nla_put_failure;
1036
1037                         nla_nest_end(skb, af);
1038                 }
1039         }
1040
1041         nla_nest_end(skb, af_spec);
1042
1043         return nlmsg_end(skb, nlh);
1044
1045 nla_put_failure:
1046         nlmsg_cancel(skb, nlh);
1047         return -EMSGSIZE;
1048 }
1049
1050 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1051 {
1052         struct net *net = sock_net(skb->sk);
1053         int h, s_h;
1054         int idx = 0, s_idx;
1055         struct net_device *dev;
1056         struct hlist_head *head;
1057         struct hlist_node *node;
1058         struct nlattr *tb[IFLA_MAX+1];
1059         u32 ext_filter_mask = 0;
1060
1061         s_h = cb->args[0];
1062         s_idx = cb->args[1];
1063
1064         rcu_read_lock();
1065         cb->seq = net->dev_base_seq;
1066
1067         if (nlmsg_parse(cb->nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
1068                         ifla_policy) >= 0) {
1069
1070                 if (tb[IFLA_EXT_MASK])
1071                         ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1072         }
1073
1074         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1075                 idx = 0;
1076                 head = &net->dev_index_head[h];
1077                 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
1078                         if (idx < s_idx)
1079                                 goto cont;
1080                         if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1081                                              NETLINK_CB(cb->skb).pid,
1082                                              cb->nlh->nlmsg_seq, 0,
1083                                              NLM_F_MULTI,
1084                                              ext_filter_mask) <= 0)
1085                                 goto out;
1086
1087                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1088 cont:
1089                         idx++;
1090                 }
1091         }
1092 out:
1093         rcu_read_unlock();
1094         cb->args[1] = idx;
1095         cb->args[0] = h;
1096
1097         return skb->len;
1098 }
1099
1100 const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1101         [IFLA_IFNAME]           = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1102         [IFLA_ADDRESS]          = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1103         [IFLA_BROADCAST]        = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1104         [IFLA_MAP]              = { .len = sizeof(struct rtnl_link_ifmap) },
1105         [IFLA_MTU]              = { .type = NLA_U32 },
1106         [IFLA_LINK]             = { .type = NLA_U32 },
1107         [IFLA_MASTER]           = { .type = NLA_U32 },
1108         [IFLA_TXQLEN]           = { .type = NLA_U32 },
1109         [IFLA_WEIGHT]           = { .type = NLA_U32 },
1110         [IFLA_OPERSTATE]        = { .type = NLA_U8 },
1111         [IFLA_LINKMODE]         = { .type = NLA_U8 },
1112         [IFLA_LINKINFO]         = { .type = NLA_NESTED },
1113         [IFLA_NET_NS_PID]       = { .type = NLA_U32 },
1114         [IFLA_NET_NS_FD]        = { .type = NLA_U32 },
1115         [IFLA_IFALIAS]          = { .type = NLA_STRING, .len = IFALIASZ-1 },
1116         [IFLA_VFINFO_LIST]      = {. type = NLA_NESTED },
1117         [IFLA_VF_PORTS]         = { .type = NLA_NESTED },
1118         [IFLA_PORT_SELF]        = { .type = NLA_NESTED },
1119         [IFLA_AF_SPEC]          = { .type = NLA_NESTED },
1120         [IFLA_EXT_MASK]         = { .type = NLA_U32 },
1121 };
1122 EXPORT_SYMBOL(ifla_policy);
1123
1124 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1125         [IFLA_INFO_KIND]        = { .type = NLA_STRING },
1126         [IFLA_INFO_DATA]        = { .type = NLA_NESTED },
1127 };
1128
1129 static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1130         [IFLA_VF_INFO]          = { .type = NLA_NESTED },
1131 };
1132
1133 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1134         [IFLA_VF_MAC]           = { .type = NLA_BINARY,
1135                                     .len = sizeof(struct ifla_vf_mac) },
1136         [IFLA_VF_VLAN]          = { .type = NLA_BINARY,
1137                                     .len = sizeof(struct ifla_vf_vlan) },
1138         [IFLA_VF_TX_RATE]       = { .type = NLA_BINARY,
1139                                     .len = sizeof(struct ifla_vf_tx_rate) },
1140         [IFLA_VF_SPOOFCHK]      = { .type = NLA_BINARY,
1141                                     .len = sizeof(struct ifla_vf_spoofchk) },
1142 };
1143
1144 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1145         [IFLA_PORT_VF]          = { .type = NLA_U32 },
1146         [IFLA_PORT_PROFILE]     = { .type = NLA_STRING,
1147                                     .len = PORT_PROFILE_MAX },
1148         [IFLA_PORT_VSI_TYPE]    = { .type = NLA_BINARY,
1149                                     .len = sizeof(struct ifla_port_vsi)},
1150         [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1151                                       .len = PORT_UUID_MAX },
1152         [IFLA_PORT_HOST_UUID]   = { .type = NLA_STRING,
1153                                     .len = PORT_UUID_MAX },
1154         [IFLA_PORT_REQUEST]     = { .type = NLA_U8, },
1155         [IFLA_PORT_RESPONSE]    = { .type = NLA_U16, },
1156 };
1157
1158 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1159 {
1160         struct net *net;
1161         /* Examine the link attributes and figure out which
1162          * network namespace we are talking about.
1163          */
1164         if (tb[IFLA_NET_NS_PID])
1165                 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1166         else if (tb[IFLA_NET_NS_FD])
1167                 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1168         else
1169                 net = get_net(src_net);
1170         return net;
1171 }
1172 EXPORT_SYMBOL(rtnl_link_get_net);
1173
1174 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1175 {
1176         if (dev) {
1177                 if (tb[IFLA_ADDRESS] &&
1178                     nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1179                         return -EINVAL;
1180
1181                 if (tb[IFLA_BROADCAST] &&
1182                     nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1183                         return -EINVAL;
1184         }
1185
1186         if (tb[IFLA_AF_SPEC]) {
1187                 struct nlattr *af;
1188                 int rem, err;
1189
1190                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1191                         const struct rtnl_af_ops *af_ops;
1192
1193                         if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1194                                 return -EAFNOSUPPORT;
1195
1196                         if (!af_ops->set_link_af)
1197                                 return -EOPNOTSUPP;
1198
1199                         if (af_ops->validate_link_af) {
1200                                 err = af_ops->validate_link_af(dev, af);
1201                                 if (err < 0)
1202                                         return err;
1203                         }
1204                 }
1205         }
1206
1207         return 0;
1208 }
1209
1210 static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1211 {
1212         int rem, err = -EINVAL;
1213         struct nlattr *vf;
1214         const struct net_device_ops *ops = dev->netdev_ops;
1215
1216         nla_for_each_nested(vf, attr, rem) {
1217                 switch (nla_type(vf)) {
1218                 case IFLA_VF_MAC: {
1219                         struct ifla_vf_mac *ivm;
1220                         ivm = nla_data(vf);
1221                         err = -EOPNOTSUPP;
1222                         if (ops->ndo_set_vf_mac)
1223                                 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1224                                                           ivm->mac);
1225                         break;
1226                 }
1227                 case IFLA_VF_VLAN: {
1228                         struct ifla_vf_vlan *ivv;
1229                         ivv = nla_data(vf);
1230                         err = -EOPNOTSUPP;
1231                         if (ops->ndo_set_vf_vlan)
1232                                 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1233                                                            ivv->vlan,
1234                                                            ivv->qos);
1235                         break;
1236                 }
1237                 case IFLA_VF_TX_RATE: {
1238                         struct ifla_vf_tx_rate *ivt;
1239                         ivt = nla_data(vf);
1240                         err = -EOPNOTSUPP;
1241                         if (ops->ndo_set_vf_tx_rate)
1242                                 err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1243                                                               ivt->rate);
1244                         break;
1245                 }
1246                 case IFLA_VF_SPOOFCHK: {
1247                         struct ifla_vf_spoofchk *ivs;
1248                         ivs = nla_data(vf);
1249                         err = -EOPNOTSUPP;
1250                         if (ops->ndo_set_vf_spoofchk)
1251                                 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1252                                                                ivs->setting);
1253                         break;
1254                 }
1255                 default:
1256                         err = -EINVAL;
1257                         break;
1258                 }
1259                 if (err)
1260                         break;
1261         }
1262         return err;
1263 }
1264
1265 static int do_set_master(struct net_device *dev, int ifindex)
1266 {
1267         struct net_device *master_dev;
1268         const struct net_device_ops *ops;
1269         int err;
1270
1271         if (dev->master) {
1272                 if (dev->master->ifindex == ifindex)
1273                         return 0;
1274                 ops = dev->master->netdev_ops;
1275                 if (ops->ndo_del_slave) {
1276                         err = ops->ndo_del_slave(dev->master, dev);
1277                         if (err)
1278                                 return err;
1279                 } else {
1280                         return -EOPNOTSUPP;
1281                 }
1282         }
1283
1284         if (ifindex) {
1285                 master_dev = __dev_get_by_index(dev_net(dev), ifindex);
1286                 if (!master_dev)
1287                         return -EINVAL;
1288                 ops = master_dev->netdev_ops;
1289                 if (ops->ndo_add_slave) {
1290                         err = ops->ndo_add_slave(master_dev, dev);
1291                         if (err)
1292                                 return err;
1293                 } else {
1294                         return -EOPNOTSUPP;
1295                 }
1296         }
1297         return 0;
1298 }
1299
1300 static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
1301                       struct nlattr **tb, char *ifname, int modified)
1302 {
1303         const struct net_device_ops *ops = dev->netdev_ops;
1304         int send_addr_notify = 0;
1305         int err;
1306
1307         if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1308                 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1309                 if (IS_ERR(net)) {
1310                         err = PTR_ERR(net);
1311                         goto errout;
1312                 }
1313                 err = dev_change_net_namespace(dev, net, ifname);
1314                 put_net(net);
1315                 if (err)
1316                         goto errout;
1317                 modified = 1;
1318         }
1319
1320         if (tb[IFLA_MAP]) {
1321                 struct rtnl_link_ifmap *u_map;
1322                 struct ifmap k_map;
1323
1324                 if (!ops->ndo_set_config) {
1325                         err = -EOPNOTSUPP;
1326                         goto errout;
1327                 }
1328
1329                 if (!netif_device_present(dev)) {
1330                         err = -ENODEV;
1331                         goto errout;
1332                 }
1333
1334                 u_map = nla_data(tb[IFLA_MAP]);
1335                 k_map.mem_start = (unsigned long) u_map->mem_start;
1336                 k_map.mem_end = (unsigned long) u_map->mem_end;
1337                 k_map.base_addr = (unsigned short) u_map->base_addr;
1338                 k_map.irq = (unsigned char) u_map->irq;
1339                 k_map.dma = (unsigned char) u_map->dma;
1340                 k_map.port = (unsigned char) u_map->port;
1341
1342                 err = ops->ndo_set_config(dev, &k_map);
1343                 if (err < 0)
1344                         goto errout;
1345
1346                 modified = 1;
1347         }
1348
1349         if (tb[IFLA_ADDRESS]) {
1350                 struct sockaddr *sa;
1351                 int len;
1352
1353                 if (!ops->ndo_set_mac_address) {
1354                         err = -EOPNOTSUPP;
1355                         goto errout;
1356                 }
1357
1358                 if (!netif_device_present(dev)) {
1359                         err = -ENODEV;
1360                         goto errout;
1361                 }
1362
1363                 len = sizeof(sa_family_t) + dev->addr_len;
1364                 sa = kmalloc(len, GFP_KERNEL);
1365                 if (!sa) {
1366                         err = -ENOMEM;
1367                         goto errout;
1368                 }
1369                 sa->sa_family = dev->type;
1370                 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1371                        dev->addr_len);
1372                 err = ops->ndo_set_mac_address(dev, sa);
1373                 kfree(sa);
1374                 if (err)
1375                         goto errout;
1376                 send_addr_notify = 1;
1377                 modified = 1;
1378                 add_device_randomness(dev->dev_addr, dev->addr_len);
1379         }
1380
1381         if (tb[IFLA_MTU]) {
1382                 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1383                 if (err < 0)
1384                         goto errout;
1385                 modified = 1;
1386         }
1387
1388         if (tb[IFLA_GROUP]) {
1389                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1390                 modified = 1;
1391         }
1392
1393         /*
1394          * Interface selected by interface index but interface
1395          * name provided implies that a name change has been
1396          * requested.
1397          */
1398         if (ifm->ifi_index > 0 && ifname[0]) {
1399                 err = dev_change_name(dev, ifname);
1400                 if (err < 0)
1401                         goto errout;
1402                 modified = 1;
1403         }
1404
1405         if (tb[IFLA_IFALIAS]) {
1406                 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1407                                     nla_len(tb[IFLA_IFALIAS]));
1408                 if (err < 0)
1409                         goto errout;
1410                 modified = 1;
1411         }
1412
1413         if (tb[IFLA_BROADCAST]) {
1414                 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1415                 send_addr_notify = 1;
1416         }
1417
1418         if (ifm->ifi_flags || ifm->ifi_change) {
1419                 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1420                 if (err < 0)
1421                         goto errout;
1422         }
1423
1424         if (tb[IFLA_MASTER]) {
1425                 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1426                 if (err)
1427                         goto errout;
1428                 modified = 1;
1429         }
1430
1431         if (tb[IFLA_TXQLEN])
1432                 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1433
1434         if (tb[IFLA_OPERSTATE])
1435                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1436
1437         if (tb[IFLA_LINKMODE]) {
1438                 write_lock_bh(&dev_base_lock);
1439                 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1440                 write_unlock_bh(&dev_base_lock);
1441         }
1442
1443         if (tb[IFLA_VFINFO_LIST]) {
1444                 struct nlattr *attr;
1445                 int rem;
1446                 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1447                         if (nla_type(attr) != IFLA_VF_INFO) {
1448                                 err = -EINVAL;
1449                                 goto errout;
1450                         }
1451                         err = do_setvfinfo(dev, attr);
1452                         if (err < 0)
1453                                 goto errout;
1454                         modified = 1;
1455                 }
1456         }
1457         err = 0;
1458
1459         if (tb[IFLA_VF_PORTS]) {
1460                 struct nlattr *port[IFLA_PORT_MAX+1];
1461                 struct nlattr *attr;
1462                 int vf;
1463                 int rem;
1464
1465                 err = -EOPNOTSUPP;
1466                 if (!ops->ndo_set_vf_port)
1467                         goto errout;
1468
1469                 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1470                         if (nla_type(attr) != IFLA_VF_PORT)
1471                                 continue;
1472                         err = nla_parse_nested(port, IFLA_PORT_MAX,
1473                                 attr, ifla_port_policy);
1474                         if (err < 0)
1475                                 goto errout;
1476                         if (!port[IFLA_PORT_VF]) {
1477                                 err = -EOPNOTSUPP;
1478                                 goto errout;
1479                         }
1480                         vf = nla_get_u32(port[IFLA_PORT_VF]);
1481                         err = ops->ndo_set_vf_port(dev, vf, port);
1482                         if (err < 0)
1483                                 goto errout;
1484                         modified = 1;
1485                 }
1486         }
1487         err = 0;
1488
1489         if (tb[IFLA_PORT_SELF]) {
1490                 struct nlattr *port[IFLA_PORT_MAX+1];
1491
1492                 err = nla_parse_nested(port, IFLA_PORT_MAX,
1493                         tb[IFLA_PORT_SELF], ifla_port_policy);
1494                 if (err < 0)
1495                         goto errout;
1496
1497                 err = -EOPNOTSUPP;
1498                 if (ops->ndo_set_vf_port)
1499                         err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1500                 if (err < 0)
1501                         goto errout;
1502                 modified = 1;
1503         }
1504
1505         if (tb[IFLA_AF_SPEC]) {
1506                 struct nlattr *af;
1507                 int rem;
1508
1509                 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1510                         const struct rtnl_af_ops *af_ops;
1511
1512                         if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1513                                 BUG();
1514
1515                         err = af_ops->set_link_af(dev, af);
1516                         if (err < 0)
1517                                 goto errout;
1518
1519                         modified = 1;
1520                 }
1521         }
1522         err = 0;
1523
1524 errout:
1525         if (err < 0 && modified && net_ratelimit())
1526                 printk(KERN_WARNING "A link change request failed with "
1527                        "some changes committed already. Interface %s may "
1528                        "have been left with an inconsistent configuration, "
1529                        "please check.\n", dev->name);
1530
1531         if (send_addr_notify)
1532                 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1533         return err;
1534 }
1535
1536 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1537 {
1538         struct net *net = sock_net(skb->sk);
1539         struct ifinfomsg *ifm;
1540         struct net_device *dev;
1541         int err;
1542         struct nlattr *tb[IFLA_MAX+1];
1543         char ifname[IFNAMSIZ];
1544
1545         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1546         if (err < 0)
1547                 goto errout;
1548
1549         if (tb[IFLA_IFNAME])
1550                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1551         else
1552                 ifname[0] = '\0';
1553
1554         err = -EINVAL;
1555         ifm = nlmsg_data(nlh);
1556         if (ifm->ifi_index > 0)
1557                 dev = __dev_get_by_index(net, ifm->ifi_index);
1558         else if (tb[IFLA_IFNAME])
1559                 dev = __dev_get_by_name(net, ifname);
1560         else
1561                 goto errout;
1562
1563         if (dev == NULL) {
1564                 err = -ENODEV;
1565                 goto errout;
1566         }
1567
1568         err = validate_linkmsg(dev, tb);
1569         if (err < 0)
1570                 goto errout;
1571
1572         err = do_setlink(dev, ifm, tb, ifname, 0);
1573 errout:
1574         return err;
1575 }
1576
1577 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1578 {
1579         struct net *net = sock_net(skb->sk);
1580         const struct rtnl_link_ops *ops;
1581         struct net_device *dev;
1582         struct ifinfomsg *ifm;
1583         char ifname[IFNAMSIZ];
1584         struct nlattr *tb[IFLA_MAX+1];
1585         int err;
1586         LIST_HEAD(list_kill);
1587
1588         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1589         if (err < 0)
1590                 return err;
1591
1592         if (tb[IFLA_IFNAME])
1593                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1594
1595         ifm = nlmsg_data(nlh);
1596         if (ifm->ifi_index > 0)
1597                 dev = __dev_get_by_index(net, ifm->ifi_index);
1598         else if (tb[IFLA_IFNAME])
1599                 dev = __dev_get_by_name(net, ifname);
1600         else
1601                 return -EINVAL;
1602
1603         if (!dev)
1604                 return -ENODEV;
1605
1606         ops = dev->rtnl_link_ops;
1607         if (!ops)
1608                 return -EOPNOTSUPP;
1609
1610         ops->dellink(dev, &list_kill);
1611         unregister_netdevice_many(&list_kill);
1612         list_del(&list_kill);
1613         return 0;
1614 }
1615
1616 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1617 {
1618         unsigned int old_flags;
1619         int err;
1620
1621         old_flags = dev->flags;
1622         if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1623                 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1624                 if (err < 0)
1625                         return err;
1626         }
1627
1628         dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
1629         rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1630
1631         __dev_notify_flags(dev, old_flags);
1632         return 0;
1633 }
1634 EXPORT_SYMBOL(rtnl_configure_link);
1635
1636 struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
1637         char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1638 {
1639         int err;
1640         struct net_device *dev;
1641         unsigned int num_queues = 1;
1642         unsigned int real_num_queues = 1;
1643
1644         if (ops->get_tx_queues) {
1645                 err = ops->get_tx_queues(src_net, tb, &num_queues,
1646                                          &real_num_queues);
1647                 if (err)
1648                         goto err;
1649         }
1650         err = -ENOMEM;
1651         dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
1652         if (!dev)
1653                 goto err;
1654
1655         dev_net_set(dev, net);
1656         dev->rtnl_link_ops = ops;
1657         dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
1658
1659         if (tb[IFLA_MTU])
1660                 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1661         if (tb[IFLA_ADDRESS])
1662                 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1663                                 nla_len(tb[IFLA_ADDRESS]));
1664         if (tb[IFLA_BROADCAST])
1665                 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1666                                 nla_len(tb[IFLA_BROADCAST]));
1667         if (tb[IFLA_TXQLEN])
1668                 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1669         if (tb[IFLA_OPERSTATE])
1670                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1671         if (tb[IFLA_LINKMODE])
1672                 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1673         if (tb[IFLA_GROUP])
1674                 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1675
1676         return dev;
1677
1678 err:
1679         return ERR_PTR(err);
1680 }
1681 EXPORT_SYMBOL(rtnl_create_link);
1682
1683 static int rtnl_group_changelink(struct net *net, int group,
1684                 struct ifinfomsg *ifm,
1685                 struct nlattr **tb)
1686 {
1687         struct net_device *dev;
1688         int err;
1689
1690         for_each_netdev(net, dev) {
1691                 if (dev->group == group) {
1692                         err = do_setlink(dev, ifm, tb, NULL, 0);
1693                         if (err < 0)
1694                                 return err;
1695                 }
1696         }
1697
1698         return 0;
1699 }
1700
1701 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1702 {
1703         struct net *net = sock_net(skb->sk);
1704         const struct rtnl_link_ops *ops;
1705         struct net_device *dev;
1706         struct ifinfomsg *ifm;
1707         char kind[MODULE_NAME_LEN];
1708         char ifname[IFNAMSIZ];
1709         struct nlattr *tb[IFLA_MAX+1];
1710         struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1711         int err;
1712
1713 #ifdef CONFIG_MODULES
1714 replay:
1715 #endif
1716         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1717         if (err < 0)
1718                 return err;
1719
1720         if (tb[IFLA_IFNAME])
1721                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1722         else
1723                 ifname[0] = '\0';
1724
1725         ifm = nlmsg_data(nlh);
1726         if (ifm->ifi_index > 0)
1727                 dev = __dev_get_by_index(net, ifm->ifi_index);
1728         else {
1729                 if (ifname[0])
1730                         dev = __dev_get_by_name(net, ifname);
1731                 else
1732                         dev = NULL;
1733         }
1734
1735         err = validate_linkmsg(dev, tb);
1736         if (err < 0)
1737                 return err;
1738
1739         if (tb[IFLA_LINKINFO]) {
1740                 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1741                                        tb[IFLA_LINKINFO], ifla_info_policy);
1742                 if (err < 0)
1743                         return err;
1744         } else
1745                 memset(linkinfo, 0, sizeof(linkinfo));
1746
1747         if (linkinfo[IFLA_INFO_KIND]) {
1748                 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1749                 ops = rtnl_link_ops_get(kind);
1750         } else {
1751                 kind[0] = '\0';
1752                 ops = NULL;
1753         }
1754
1755         if (1) {
1756                 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
1757                 struct net *dest_net;
1758
1759                 if (ops) {
1760                         if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1761                                 err = nla_parse_nested(attr, ops->maxtype,
1762                                                        linkinfo[IFLA_INFO_DATA],
1763                                                        ops->policy);
1764                                 if (err < 0)
1765                                         return err;
1766                                 data = attr;
1767                         }
1768                         if (ops->validate) {
1769                                 err = ops->validate(tb, data);
1770                                 if (err < 0)
1771                                         return err;
1772                         }
1773                 }
1774
1775                 if (dev) {
1776                         int modified = 0;
1777
1778                         if (nlh->nlmsg_flags & NLM_F_EXCL)
1779                                 return -EEXIST;
1780                         if (nlh->nlmsg_flags & NLM_F_REPLACE)
1781                                 return -EOPNOTSUPP;
1782
1783                         if (linkinfo[IFLA_INFO_DATA]) {
1784                                 if (!ops || ops != dev->rtnl_link_ops ||
1785                                     !ops->changelink)
1786                                         return -EOPNOTSUPP;
1787
1788                                 err = ops->changelink(dev, tb, data);
1789                                 if (err < 0)
1790                                         return err;
1791                                 modified = 1;
1792                         }
1793
1794                         return do_setlink(dev, ifm, tb, ifname, modified);
1795                 }
1796
1797                 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1798                         if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1799                                 return rtnl_group_changelink(net,
1800                                                 nla_get_u32(tb[IFLA_GROUP]),
1801                                                 ifm, tb);
1802                         return -ENODEV;
1803                 }
1804
1805                 if (ifm->ifi_index)
1806                         return -EOPNOTSUPP;
1807                 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
1808                         return -EOPNOTSUPP;
1809
1810                 if (!ops) {
1811 #ifdef CONFIG_MODULES
1812                         if (kind[0]) {
1813                                 __rtnl_unlock();
1814                                 request_module("rtnl-link-%s", kind);
1815                                 rtnl_lock();
1816                                 ops = rtnl_link_ops_get(kind);
1817                                 if (ops)
1818                                         goto replay;
1819                         }
1820 #endif
1821                         return -EOPNOTSUPP;
1822                 }
1823
1824                 if (!ifname[0])
1825                         snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
1826
1827                 dest_net = rtnl_link_get_net(net, tb);
1828                 if (IS_ERR(dest_net))
1829                         return PTR_ERR(dest_net);
1830
1831                 dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
1832
1833                 if (IS_ERR(dev))
1834                         err = PTR_ERR(dev);
1835                 else if (ops->newlink)
1836                         err = ops->newlink(net, dev, tb, data);
1837                 else
1838                         err = register_netdevice(dev);
1839
1840                 if (err < 0 && !IS_ERR(dev))
1841                         free_netdev(dev);
1842                 if (err < 0)
1843                         goto out;
1844
1845                 err = rtnl_configure_link(dev, ifm);
1846                 if (err < 0)
1847                         unregister_netdevice(dev);
1848 out:
1849                 put_net(dest_net);
1850                 return err;
1851         }
1852 }
1853
1854 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1855 {
1856         struct net *net = sock_net(skb->sk);
1857         struct ifinfomsg *ifm;
1858         char ifname[IFNAMSIZ];
1859         struct nlattr *tb[IFLA_MAX+1];
1860         struct net_device *dev = NULL;
1861         struct sk_buff *nskb;
1862         int err;
1863         u32 ext_filter_mask = 0;
1864
1865         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1866         if (err < 0)
1867                 return err;
1868
1869         if (tb[IFLA_IFNAME])
1870                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1871
1872         if (tb[IFLA_EXT_MASK])
1873                 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1874
1875         ifm = nlmsg_data(nlh);
1876         if (ifm->ifi_index > 0)
1877                 dev = __dev_get_by_index(net, ifm->ifi_index);
1878         else if (tb[IFLA_IFNAME])
1879                 dev = __dev_get_by_name(net, ifname);
1880         else
1881                 return -EINVAL;
1882
1883         if (dev == NULL)
1884                 return -ENODEV;
1885
1886         nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
1887         if (nskb == NULL)
1888                 return -ENOBUFS;
1889
1890         err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1891                                nlh->nlmsg_seq, 0, 0, ext_filter_mask);
1892         if (err < 0) {
1893                 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1894                 WARN_ON(err == -EMSGSIZE);
1895                 kfree_skb(nskb);
1896         } else
1897                 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
1898
1899         return err;
1900 }
1901
1902 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
1903 {
1904         struct net *net = sock_net(skb->sk);
1905         struct net_device *dev;
1906         struct nlattr *tb[IFLA_MAX+1];
1907         u32 ext_filter_mask = 0;
1908         u16 min_ifinfo_dump_size = 0;
1909
1910         if (nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
1911                         ifla_policy) >= 0) {
1912                 if (tb[IFLA_EXT_MASK])
1913                         ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1914         }
1915
1916         if (!ext_filter_mask)
1917                 return NLMSG_GOODSIZE;
1918         /*
1919          * traverse the list of net devices and compute the minimum
1920          * buffer size based upon the filter mask.
1921          */
1922         list_for_each_entry(dev, &net->dev_base_head, dev_list) {
1923                 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
1924                                              if_nlmsg_size(dev,
1925                                                            ext_filter_mask));
1926         }
1927
1928         return min_ifinfo_dump_size;
1929 }
1930
1931 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1932 {
1933         int idx;
1934         int s_idx = cb->family;
1935
1936         if (s_idx == 0)
1937                 s_idx = 1;
1938         for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
1939                 int type = cb->nlh->nlmsg_type-RTM_BASE;
1940                 if (idx < s_idx || idx == PF_PACKET)
1941                         continue;
1942                 if (rtnl_msg_handlers[idx] == NULL ||
1943                     rtnl_msg_handlers[idx][type].dumpit == NULL)
1944                         continue;
1945                 if (idx > s_idx)
1946                         memset(&cb->args[0], 0, sizeof(cb->args));
1947                 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
1948                         break;
1949         }
1950         cb->family = idx;
1951
1952         return skb->len;
1953 }
1954
1955 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1956 {
1957         struct net *net = dev_net(dev);
1958         struct sk_buff *skb;
1959         int err = -ENOBUFS;
1960         size_t if_info_size;
1961
1962         skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL);
1963         if (skb == NULL)
1964                 goto errout;
1965
1966         err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
1967         if (err < 0) {
1968                 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1969                 WARN_ON(err == -EMSGSIZE);
1970                 kfree_skb(skb);
1971                 goto errout;
1972         }
1973         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1974         return;
1975 errout:
1976         if (err < 0)
1977                 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
1978 }
1979
1980 /* Protected by RTNL sempahore.  */
1981 static struct rtattr **rta_buf;
1982 static int rtattr_max;
1983
1984 /* Process one rtnetlink message. */
1985
1986 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1987 {
1988         struct net *net = sock_net(skb->sk);
1989         rtnl_doit_func doit;
1990         int sz_idx, kind;
1991         int min_len;
1992         int family;
1993         int type;
1994         int err;
1995
1996         type = nlh->nlmsg_type;
1997         if (type > RTM_MAX)
1998                 return -EOPNOTSUPP;
1999
2000         type -= RTM_BASE;
2001
2002         /* All the messages must have at least 1 byte length */
2003         if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
2004                 return 0;
2005
2006         family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
2007         sz_idx = type>>2;
2008         kind = type&3;
2009
2010         if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
2011                 return -EPERM;
2012
2013         if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
2014                 struct sock *rtnl;
2015                 rtnl_dumpit_func dumpit;
2016                 rtnl_calcit_func calcit;
2017                 u16 min_dump_alloc = 0;
2018
2019                 dumpit = rtnl_get_dumpit(family, type);
2020                 if (dumpit == NULL)
2021                         return -EOPNOTSUPP;
2022                 calcit = rtnl_get_calcit(family, type);
2023                 if (calcit)
2024                         min_dump_alloc = calcit(skb, nlh);
2025
2026                 __rtnl_unlock();
2027                 rtnl = net->rtnl;
2028                 err = netlink_dump_start(rtnl, skb, nlh, dumpit,
2029                                          NULL, min_dump_alloc);
2030                 rtnl_lock();
2031                 return err;
2032         }
2033
2034         memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
2035
2036         min_len = rtm_min[sz_idx];
2037         if (nlh->nlmsg_len < min_len)
2038                 return -EINVAL;
2039
2040         if (nlh->nlmsg_len > min_len) {
2041                 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
2042                 struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
2043
2044                 while (RTA_OK(attr, attrlen)) {
2045                         unsigned flavor = attr->rta_type;
2046                         if (flavor) {
2047                                 if (flavor > rta_max[sz_idx])
2048                                         return -EINVAL;
2049                                 rta_buf[flavor-1] = attr;
2050                         }
2051                         attr = RTA_NEXT(attr, attrlen);
2052                 }
2053         }
2054
2055         doit = rtnl_get_doit(family, type);
2056         if (doit == NULL)
2057                 return -EOPNOTSUPP;
2058
2059         return doit(skb, nlh, (void *)&rta_buf[0]);
2060 }
2061
2062 static void rtnetlink_rcv(struct sk_buff *skb)
2063 {
2064         rtnl_lock();
2065         netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
2066         rtnl_unlock();
2067 }
2068
2069 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
2070 {
2071         struct net_device *dev = ptr;
2072
2073         switch (event) {
2074         case NETDEV_UP:
2075         case NETDEV_DOWN:
2076         case NETDEV_PRE_UP:
2077         case NETDEV_POST_INIT:
2078         case NETDEV_REGISTER:
2079         case NETDEV_CHANGE:
2080         case NETDEV_PRE_TYPE_CHANGE:
2081         case NETDEV_GOING_DOWN:
2082         case NETDEV_UNREGISTER:
2083         case NETDEV_UNREGISTER_BATCH:
2084         case NETDEV_RELEASE:
2085         case NETDEV_JOIN:
2086                 break;
2087         default:
2088                 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
2089                 break;
2090         }
2091         return NOTIFY_DONE;
2092 }
2093
2094 static struct notifier_block rtnetlink_dev_notifier = {
2095         .notifier_call  = rtnetlink_event,
2096 };
2097
2098
2099 static int __net_init rtnetlink_net_init(struct net *net)
2100 {
2101         struct sock *sk;
2102         sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
2103                                    rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
2104         if (!sk)
2105                 return -ENOMEM;
2106         net->rtnl = sk;
2107         return 0;
2108 }
2109
2110 static void __net_exit rtnetlink_net_exit(struct net *net)
2111 {
2112         netlink_kernel_release(net->rtnl);
2113         net->rtnl = NULL;
2114 }
2115
2116 static struct pernet_operations rtnetlink_net_ops = {
2117         .init = rtnetlink_net_init,
2118         .exit = rtnetlink_net_exit,
2119 };
2120
2121 void __init rtnetlink_init(void)
2122 {
2123         int i;
2124
2125         rtattr_max = 0;
2126         for (i = 0; i < ARRAY_SIZE(rta_max); i++)
2127                 if (rta_max[i] > rtattr_max)
2128                         rtattr_max = rta_max[i];
2129         rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
2130         if (!rta_buf)
2131                 panic("rtnetlink_init: cannot allocate rta_buf\n");
2132
2133         if (register_pernet_subsys(&rtnetlink_net_ops))
2134                 panic("rtnetlink_init: cannot initialize rtnetlink\n");
2135
2136         netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
2137         register_netdevice_notifier(&rtnetlink_dev_notifier);
2138
2139         rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2140                       rtnl_dump_ifinfo, rtnl_calcit);
2141         rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2142         rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2143         rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
2144
2145         rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2146         rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
2147 }
2148