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