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