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