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