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