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