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