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