fedora core 6 1.2949 + vserver 2.2.0
[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/sched.h>
25 #include <linux/timer.h>
26 #include <linux/string.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/fcntl.h>
30 #include <linux/mm.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/capability.h>
34 #include <linux/skbuff.h>
35 #include <linux/init.h>
36 #include <linux/security.h>
37 #include <linux/mutex.h>
38 #include <linux/if_addr.h>
39 #include <linux/vs_context.h> /* remove with NXF_HIDE_NETIF */
40
41 #include <asm/uaccess.h>
42 #include <asm/system.h>
43 #include <asm/string.h>
44
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <net/ip.h>
48 #include <net/protocol.h>
49 #include <net/arp.h>
50 #include <net/route.h>
51 #include <net/udp.h>
52 #include <net/sock.h>
53 #include <net/pkt_sched.h>
54 #include <net/fib_rules.h>
55 #include <net/netlink.h>
56 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
57 #include <linux/wireless.h>
58 #include <net/iw_handler.h>
59 #endif  /* CONFIG_NET_WIRELESS_RTNETLINK */
60
61 static DEFINE_MUTEX(rtnl_mutex);
62 static struct sock *rtnl;
63
64 void rtnl_lock(void)
65 {
66         mutex_lock(&rtnl_mutex);
67 }
68
69 void __rtnl_unlock(void)
70 {
71         mutex_unlock(&rtnl_mutex);
72 }
73
74 void rtnl_unlock(void)
75 {
76         mutex_unlock(&rtnl_mutex);
77         if (rtnl && rtnl->sk_receive_queue.qlen)
78                 rtnl->sk_data_ready(rtnl, 0);
79         netdev_run_todo();
80 }
81
82 int rtnl_trylock(void)
83 {
84         return mutex_trylock(&rtnl_mutex);
85 }
86
87 int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
88 {
89         memset(tb, 0, sizeof(struct rtattr*)*maxattr);
90
91         while (RTA_OK(rta, len)) {
92                 unsigned flavor = rta->rta_type;
93                 if (flavor && flavor <= maxattr)
94                         tb[flavor-1] = rta;
95                 rta = RTA_NEXT(rta, len);
96         }
97         return 0;
98 }
99
100 struct rtnetlink_link * rtnetlink_links[NPROTO];
101
102 static const int rtm_min[RTM_NR_FAMILIES] =
103 {
104         [RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
105         [RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
106         [RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
107         [RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
108         [RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
109         [RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
110         [RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
111         [RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
112         [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
113         [RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
114 };
115
116 static const int rta_max[RTM_NR_FAMILIES] =
117 {
118         [RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
119         [RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
120         [RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
121         [RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
122         [RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
123         [RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
124         [RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
125         [RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
126 };
127
128 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
129 {
130         struct rtattr *rta;
131         int size = RTA_LENGTH(attrlen);
132
133         rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
134         rta->rta_type = attrtype;
135         rta->rta_len = size;
136         memcpy(RTA_DATA(rta), data, attrlen);
137         memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
138 }
139
140 size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
141 {
142         size_t ret = RTA_PAYLOAD(rta);
143         char *src = RTA_DATA(rta);
144
145         if (ret > 0 && src[ret - 1] == '\0')
146                 ret--;
147         if (size > 0) {
148                 size_t len = (ret >= size) ? size - 1 : ret;
149                 memset(dest, 0, size);
150                 memcpy(dest, src, len);
151         }
152         return ret;
153 }
154
155 int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
156 {
157         int err = 0;
158
159         NETLINK_CB(skb).dst_group = group;
160         if (echo)
161                 atomic_inc(&skb->users);
162         netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
163         if (echo)
164                 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
165         return err;
166 }
167
168 int rtnl_unicast(struct sk_buff *skb, u32 pid)
169 {
170         return nlmsg_unicast(rtnl, skb, pid);
171 }
172
173 int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
174                 struct nlmsghdr *nlh, gfp_t flags)
175 {
176         int report = 0;
177
178         if (nlh)
179                 report = nlmsg_report(nlh);
180
181         return nlmsg_notify(rtnl, skb, pid, group, report, flags);
182 }
183
184 void rtnl_set_sk_err(u32 group, int error)
185 {
186         netlink_set_err(rtnl, 0, group, error);
187 }
188
189 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
190 {
191         struct nlattr *mx;
192         int i, valid = 0;
193
194         mx = nla_nest_start(skb, RTA_METRICS);
195         if (mx == NULL)
196                 return -ENOBUFS;
197
198         for (i = 0; i < RTAX_MAX; i++) {
199                 if (metrics[i]) {
200                         valid++;
201                         NLA_PUT_U32(skb, i+1, metrics[i]);
202                 }
203         }
204
205         if (!valid) {
206                 nla_nest_cancel(skb, mx);
207                 return 0;
208         }
209
210         return nla_nest_end(skb, mx);
211
212 nla_put_failure:
213         return nla_nest_cancel(skb, mx);
214 }
215
216 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
217                        u32 ts, u32 tsage, long expires, u32 error)
218 {
219         struct rta_cacheinfo ci = {
220                 .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
221                 .rta_used = dst->__use,
222                 .rta_clntref = atomic_read(&(dst->__refcnt)),
223                 .rta_error = error,
224                 .rta_id =  id,
225                 .rta_ts = ts,
226                 .rta_tsage = tsage,
227         };
228
229         if (expires)
230                 ci.rta_expires = jiffies_to_clock_t(expires);
231
232         return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
233 }
234
235 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
236
237 static void set_operstate(struct net_device *dev, unsigned char transition)
238 {
239         unsigned char operstate = dev->operstate;
240
241         switch(transition) {
242         case IF_OPER_UP:
243                 if ((operstate == IF_OPER_DORMANT ||
244                      operstate == IF_OPER_UNKNOWN) &&
245                     !netif_dormant(dev))
246                         operstate = IF_OPER_UP;
247                 break;
248
249         case IF_OPER_DORMANT:
250                 if (operstate == IF_OPER_UP ||
251                     operstate == IF_OPER_UNKNOWN)
252                         operstate = IF_OPER_DORMANT;
253                 break;
254         };
255
256         if (dev->operstate != operstate) {
257                 write_lock_bh(&dev_base_lock);
258                 dev->operstate = operstate;
259                 write_unlock_bh(&dev_base_lock);
260                 netdev_state_change(dev);
261         }
262 }
263
264 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
265                                  struct net_device_stats *b)
266 {
267         a->rx_packets = b->rx_packets;
268         a->tx_packets = b->tx_packets;
269         a->rx_bytes = b->rx_bytes;
270         a->tx_bytes = b->tx_bytes;
271         a->rx_errors = b->rx_errors;
272         a->tx_errors = b->tx_errors;
273         a->rx_dropped = b->rx_dropped;
274         a->tx_dropped = b->tx_dropped;
275
276         a->multicast = b->multicast;
277         a->collisions = b->collisions;
278
279         a->rx_length_errors = b->rx_length_errors;
280         a->rx_over_errors = b->rx_over_errors;
281         a->rx_crc_errors = b->rx_crc_errors;
282         a->rx_frame_errors = b->rx_frame_errors;
283         a->rx_fifo_errors = b->rx_fifo_errors;
284         a->rx_missed_errors = b->rx_missed_errors;
285
286         a->tx_aborted_errors = b->tx_aborted_errors;
287         a->tx_carrier_errors = b->tx_carrier_errors;
288         a->tx_fifo_errors = b->tx_fifo_errors;
289         a->tx_heartbeat_errors = b->tx_heartbeat_errors;
290         a->tx_window_errors = b->tx_window_errors;
291
292         a->rx_compressed = b->rx_compressed;
293         a->tx_compressed = b->tx_compressed;
294 };
295
296 static inline size_t if_nlmsg_size(int iwbuflen)
297 {
298         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
299                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
300                + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
301                + nla_total_size(sizeof(struct rtnl_link_ifmap))
302                + nla_total_size(sizeof(struct rtnl_link_stats))
303                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
304                + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
305                + nla_total_size(4) /* IFLA_TXQLEN */
306                + nla_total_size(4) /* IFLA_WEIGHT */
307                + nla_total_size(4) /* IFLA_MTU */
308                + nla_total_size(4) /* IFLA_LINK */
309                + nla_total_size(4) /* IFLA_MASTER */
310                + nla_total_size(1) /* IFLA_OPERSTATE */
311                + nla_total_size(1) /* IFLA_LINKMODE */
312                + nla_total_size(iwbuflen);
313 }
314
315 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
316                             void *iwbuf, int iwbuflen, int type, u32 pid,
317                             u32 seq, u32 change, unsigned int flags)
318 {
319         struct ifinfomsg *ifm;
320         struct nlmsghdr *nlh;
321
322         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
323         if (nlh == NULL)
324                 return -ENOBUFS;
325
326         ifm = nlmsg_data(nlh);
327         ifm->ifi_family = AF_UNSPEC;
328         ifm->__ifi_pad = 0;
329         ifm->ifi_type = dev->type;
330         ifm->ifi_index = dev->ifindex;
331         ifm->ifi_flags = dev_get_flags(dev);
332         ifm->ifi_change = change;
333
334         NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
335         NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
336         NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
337         NLA_PUT_U8(skb, IFLA_OPERSTATE,
338                    netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
339         NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
340         NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
341
342         if (dev->ifindex != dev->iflink)
343                 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
344
345         if (dev->master)
346                 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
347
348         if (dev->qdisc_sleeping)
349                 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
350
351         if (1) {
352                 struct rtnl_link_ifmap map = {
353                         .mem_start   = dev->mem_start,
354                         .mem_end     = dev->mem_end,
355                         .base_addr   = dev->base_addr,
356                         .irq         = dev->irq,
357                         .dma         = dev->dma,
358                         .port        = dev->if_port,
359                 };
360                 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
361         }
362
363         if (dev->addr_len) {
364                 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
365                 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
366         }
367
368         if (dev->get_stats) {
369                 struct net_device_stats *stats = dev->get_stats(dev);
370                 if (stats) {
371                         struct nlattr *attr;
372
373                         attr = nla_reserve(skb, IFLA_STATS,
374                                            sizeof(struct rtnl_link_stats));
375                         if (attr == NULL)
376                                 goto nla_put_failure;
377
378                         copy_rtnl_link_stats(nla_data(attr), stats);
379                 }
380         }
381
382         if (iwbuf)
383                 NLA_PUT(skb, IFLA_WIRELESS, iwbuflen, iwbuf);
384
385         return nlmsg_end(skb, nlh);
386
387 nla_put_failure:
388         return nlmsg_cancel(skb, nlh);
389 }
390
391 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
392 {
393         int idx;
394         int s_idx = cb->args[0];
395         struct net_device *dev;
396
397         read_lock(&dev_base_lock);
398         for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
399                 if (idx < s_idx)
400                         continue;
401                 if (vx_info_flags(skb->sk->sk_vx_info, VXF_HIDE_NETIF, 0) &&
402                         !dev_in_nx_info(dev, skb->sk->sk_nx_info))
403                         continue;
404                 if (rtnl_fill_ifinfo(skb, dev, NULL, 0, RTM_NEWLINK,
405                                      NETLINK_CB(cb->skb).pid,
406                                      cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
407                         break;
408         }
409         read_unlock(&dev_base_lock);
410         cb->args[0] = idx;
411
412         return skb->len;
413 }
414
415 static struct nla_policy ifla_policy[IFLA_MAX+1] __read_mostly = {
416         [IFLA_IFNAME]           = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
417         [IFLA_MAP]              = { .len = sizeof(struct rtnl_link_ifmap) },
418         [IFLA_MTU]              = { .type = NLA_U32 },
419         [IFLA_TXQLEN]           = { .type = NLA_U32 },
420         [IFLA_WEIGHT]           = { .type = NLA_U32 },
421         [IFLA_OPERSTATE]        = { .type = NLA_U8 },
422         [IFLA_LINKMODE]         = { .type = NLA_U8 },
423 };
424
425 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
426 {
427         struct ifinfomsg *ifm;
428         struct net_device *dev;
429         int err, send_addr_notify = 0, modified = 0;
430         struct nlattr *tb[IFLA_MAX+1];
431         char ifname[IFNAMSIZ];
432
433         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
434         if (err < 0)
435                 goto errout;
436
437         if (tb[IFLA_IFNAME])
438                 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
439         else
440                 ifname[0] = '\0';
441
442         err = -EINVAL;
443         ifm = nlmsg_data(nlh);
444         if (ifm->ifi_index >= 0)
445                 dev = dev_get_by_index(ifm->ifi_index);
446         else if (tb[IFLA_IFNAME])
447                 dev = dev_get_by_name(ifname);
448         else
449                 goto errout;
450
451         if (dev == NULL) {
452                 err = -ENODEV;
453                 goto errout;
454         }
455
456         if (tb[IFLA_ADDRESS] &&
457             nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
458                 goto errout_dev;
459
460         if (tb[IFLA_BROADCAST] &&
461             nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
462                 goto errout_dev;
463
464         if (tb[IFLA_MAP]) {
465                 struct rtnl_link_ifmap *u_map;
466                 struct ifmap k_map;
467
468                 if (!dev->set_config) {
469                         err = -EOPNOTSUPP;
470                         goto errout_dev;
471                 }
472
473                 if (!netif_device_present(dev)) {
474                         err = -ENODEV;
475                         goto errout_dev;
476                 }
477
478                 u_map = nla_data(tb[IFLA_MAP]);
479                 k_map.mem_start = (unsigned long) u_map->mem_start;
480                 k_map.mem_end = (unsigned long) u_map->mem_end;
481                 k_map.base_addr = (unsigned short) u_map->base_addr;
482                 k_map.irq = (unsigned char) u_map->irq;
483                 k_map.dma = (unsigned char) u_map->dma;
484                 k_map.port = (unsigned char) u_map->port;
485
486                 err = dev->set_config(dev, &k_map);
487                 if (err < 0)
488                         goto errout_dev;
489
490                 modified = 1;
491         }
492
493         if (tb[IFLA_ADDRESS]) {
494                 struct sockaddr *sa;
495                 int len;
496
497                 if (!dev->set_mac_address) {
498                         err = -EOPNOTSUPP;
499                         goto errout_dev;
500                 }
501
502                 if (!netif_device_present(dev)) {
503                         err = -ENODEV;
504                         goto errout_dev;
505                 }
506
507                 len = sizeof(sa_family_t) + dev->addr_len;
508                 sa = kmalloc(len, GFP_KERNEL);
509                 if (!sa) {
510                         err = -ENOMEM;
511                         goto errout_dev;
512                 }
513                 sa->sa_family = dev->type;
514                 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
515                        dev->addr_len);
516                 err = dev->set_mac_address(dev, sa);
517                 kfree(sa);
518                 if (err)
519                         goto errout_dev;
520                 send_addr_notify = 1;
521                 modified = 1;
522         }
523
524         if (tb[IFLA_MTU]) {
525                 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
526                 if (err < 0)
527                         goto errout_dev;
528                 modified = 1;
529         }
530
531         /*
532          * Interface selected by interface index but interface
533          * name provided implies that a name change has been
534          * requested.
535          */
536         if (ifm->ifi_index >= 0 && ifname[0]) {
537                 err = dev_change_name(dev, ifname);
538                 if (err < 0)
539                         goto errout_dev;
540                 modified = 1;
541         }
542
543 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
544         if (tb[IFLA_WIRELESS]) {
545                 /* Call Wireless Extensions.
546                  * Various stuff checked in there... */
547                 err = wireless_rtnetlink_set(dev, nla_data(tb[IFLA_WIRELESS]),
548                                              nla_len(tb[IFLA_WIRELESS]));
549                 if (err < 0)
550                         goto errout_dev;
551         }
552 #endif  /* CONFIG_NET_WIRELESS_RTNETLINK */
553
554         if (tb[IFLA_BROADCAST]) {
555                 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
556                 send_addr_notify = 1;
557         }
558
559
560         if (ifm->ifi_flags)
561                 dev_change_flags(dev, ifm->ifi_flags);
562
563         if (tb[IFLA_TXQLEN])
564                 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
565
566         if (tb[IFLA_WEIGHT])
567                 dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
568
569         if (tb[IFLA_OPERSTATE])
570                 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
571
572         if (tb[IFLA_LINKMODE]) {
573                 write_lock_bh(&dev_base_lock);
574                 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
575                 write_unlock_bh(&dev_base_lock);
576         }
577
578         err = 0;
579
580 errout_dev:
581         if (err < 0 && modified && net_ratelimit())
582                 printk(KERN_WARNING "A link change request failed with "
583                        "some changes comitted already. Interface %s may "
584                        "have been left with an inconsistent configuration, "
585                        "please check.\n", dev->name);
586
587         if (send_addr_notify)
588                 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
589
590         dev_put(dev);
591 errout:
592         return err;
593 }
594
595 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
596 {
597         struct ifinfomsg *ifm;
598         struct nlattr *tb[IFLA_MAX+1];
599         struct net_device *dev = NULL;
600         struct sk_buff *nskb;
601         char *iw_buf = NULL, *iw = NULL;
602         int iw_buf_len = 0;
603         int err;
604
605         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
606         if (err < 0)
607                 return err;
608
609         ifm = nlmsg_data(nlh);
610         if (ifm->ifi_index >= 0) {
611                 dev = dev_get_by_index(ifm->ifi_index);
612                 if (dev == NULL)
613                         return -ENODEV;
614         } else
615                 return -EINVAL;
616
617
618 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
619         if (tb[IFLA_WIRELESS]) {
620                 /* Call Wireless Extensions. We need to know the size before
621                  * we can alloc. Various stuff checked in there... */
622                 err = wireless_rtnetlink_get(dev, nla_data(tb[IFLA_WIRELESS]),
623                                              nla_len(tb[IFLA_WIRELESS]),
624                                              &iw_buf, &iw_buf_len);
625                 if (err < 0)
626                         goto errout;
627
628                 iw += IW_EV_POINT_OFF;
629         }
630 #endif  /* CONFIG_NET_WIRELESS_RTNETLINK */
631
632         nskb = nlmsg_new(if_nlmsg_size(iw_buf_len), GFP_KERNEL);
633         if (nskb == NULL) {
634                 err = -ENOBUFS;
635                 goto errout;
636         }
637
638         err = rtnl_fill_ifinfo(nskb, dev, iw, iw_buf_len, RTM_NEWLINK,
639                                NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0, 0);
640         /* failure impilies BUG in if_nlmsg_size or wireless_rtnetlink_get */
641         BUG_ON(err < 0);
642
643         err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
644 errout:
645         kfree(iw_buf);
646         dev_put(dev);
647
648         return err;
649 }
650
651 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
652 {
653         int idx;
654         int s_idx = cb->family;
655
656         if (s_idx == 0)
657                 s_idx = 1;
658         for (idx=1; idx<NPROTO; idx++) {
659                 int type = cb->nlh->nlmsg_type-RTM_BASE;
660                 if (idx < s_idx || idx == PF_PACKET)
661                         continue;
662                 if (rtnetlink_links[idx] == NULL ||
663                     rtnetlink_links[idx][type].dumpit == NULL)
664                         continue;
665                 if (idx > s_idx)
666                         memset(&cb->args[0], 0, sizeof(cb->args));
667                 if (rtnetlink_links[idx][type].dumpit(skb, cb))
668                         break;
669         }
670         cb->family = idx;
671
672         return skb->len;
673 }
674
675 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
676 {
677         struct sk_buff *skb;
678         int err = -ENOBUFS;
679
680         skb = nlmsg_new(if_nlmsg_size(0), GFP_KERNEL);
681         if (skb == NULL)
682                 goto errout;
683
684         err = rtnl_fill_ifinfo(skb, dev, NULL, 0, type, 0, 0, change, 0);
685         /* failure implies BUG in if_nlmsg_size() */
686         BUG_ON(err < 0);
687
688         err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
689 errout:
690         if (err < 0)
691                 rtnl_set_sk_err(RTNLGRP_LINK, err);
692 }
693
694 /* Protected by RTNL sempahore.  */
695 static struct rtattr **rta_buf;
696 static int rtattr_max;
697
698 /* Process one rtnetlink message. */
699
700 static __inline__ int
701 rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
702 {
703         struct rtnetlink_link *link;
704         struct rtnetlink_link *link_tab;
705         int sz_idx, kind;
706         int min_len;
707         int family;
708         int type;
709         int err;
710
711         /* Only requests are handled by kernel now */
712         if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
713                 return 0;
714
715         type = nlh->nlmsg_type;
716
717         /* A control message: ignore them */
718         if (type < RTM_BASE)
719                 return 0;
720
721         /* Unknown message: reply with EINVAL */
722         if (type > RTM_MAX)
723                 goto err_inval;
724
725         type -= RTM_BASE;
726
727         /* All the messages must have at least 1 byte length */
728         if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
729                 return 0;
730
731         family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
732         if (family >= NPROTO) {
733                 *errp = -EAFNOSUPPORT;
734                 return -1;
735         }
736
737         link_tab = rtnetlink_links[family];
738         if (link_tab == NULL)
739                 link_tab = rtnetlink_links[PF_UNSPEC];
740         link = &link_tab[type];
741
742         sz_idx = type>>2;
743         kind = type&3;
744
745         if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) {
746                 *errp = -EPERM;
747                 return -1;
748         }
749
750         if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
751                 if (link->dumpit == NULL)
752                         link = &(rtnetlink_links[PF_UNSPEC][type]);
753
754                 if (link->dumpit == NULL)
755                         goto err_inval;
756
757                 if ((*errp = netlink_dump_start(rtnl, skb, nlh,
758                                                 link->dumpit, NULL)) != 0) {
759                         return -1;
760                 }
761
762                 netlink_queue_skip(nlh, skb);
763                 return -1;
764         }
765
766         memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
767
768         min_len = rtm_min[sz_idx];
769         if (nlh->nlmsg_len < min_len)
770                 goto err_inval;
771
772         if (nlh->nlmsg_len > min_len) {
773                 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
774                 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
775
776                 while (RTA_OK(attr, attrlen)) {
777                         unsigned flavor = attr->rta_type;
778                         if (flavor) {
779                                 if (flavor > rta_max[sz_idx])
780                                         goto err_inval;
781                                 rta_buf[flavor-1] = attr;
782                         }
783                         attr = RTA_NEXT(attr, attrlen);
784                 }
785         }
786
787         if (link->doit == NULL)
788                 link = &(rtnetlink_links[PF_UNSPEC][type]);
789         if (link->doit == NULL)
790                 goto err_inval;
791         err = link->doit(skb, nlh, (void *)&rta_buf[0]);
792
793         *errp = err;
794         return err;
795
796 err_inval:
797         *errp = -EINVAL;
798         return -1;
799 }
800
801 static void rtnetlink_rcv(struct sock *sk, int len)
802 {
803         unsigned int qlen = 0;
804
805         do {
806                 mutex_lock(&rtnl_mutex);
807                 netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
808                 mutex_unlock(&rtnl_mutex);
809
810                 netdev_run_todo();
811         } while (qlen);
812 }
813
814 static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
815 {
816         [RTM_GETLINK     - RTM_BASE] = { .doit   = rtnl_getlink,
817                                          .dumpit = rtnl_dump_ifinfo      },
818         [RTM_SETLINK     - RTM_BASE] = { .doit   = rtnl_setlink          },
819         [RTM_GETADDR     - RTM_BASE] = { .dumpit = rtnl_dump_all         },
820         [RTM_GETROUTE    - RTM_BASE] = { .dumpit = rtnl_dump_all         },
821         [RTM_NEWNEIGH    - RTM_BASE] = { .doit   = neigh_add             },
822         [RTM_DELNEIGH    - RTM_BASE] = { .doit   = neigh_delete          },
823         [RTM_GETNEIGH    - RTM_BASE] = { .dumpit = neigh_dump_info       },
824 #ifdef CONFIG_FIB_RULES
825         [RTM_NEWRULE     - RTM_BASE] = { .doit   = fib_nl_newrule        },
826         [RTM_DELRULE     - RTM_BASE] = { .doit   = fib_nl_delrule        },
827 #endif
828         [RTM_GETRULE     - RTM_BASE] = { .dumpit = rtnl_dump_all         },
829         [RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info    },
830         [RTM_SETNEIGHTBL - RTM_BASE] = { .doit   = neightbl_set          },
831 };
832
833 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
834 {
835         struct net_device *dev = ptr;
836         switch (event) {
837         case NETDEV_UNREGISTER:
838                 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
839                 break;
840         case NETDEV_REGISTER:
841                 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
842                 break;
843         case NETDEV_UP:
844         case NETDEV_DOWN:
845                 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
846                 break;
847         case NETDEV_CHANGE:
848         case NETDEV_GOING_DOWN:
849                 break;
850         default:
851                 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
852                 break;
853         }
854         return NOTIFY_DONE;
855 }
856
857 static struct notifier_block rtnetlink_dev_notifier = {
858         .notifier_call  = rtnetlink_event,
859 };
860
861 void __init rtnetlink_init(void)
862 {
863         int i;
864
865         rtattr_max = 0;
866         for (i = 0; i < ARRAY_SIZE(rta_max); i++)
867                 if (rta_max[i] > rtattr_max)
868                         rtattr_max = rta_max[i];
869         rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
870         if (!rta_buf)
871                 panic("rtnetlink_init: cannot allocate rta_buf\n");
872
873         rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
874                                      THIS_MODULE);
875         if (rtnl == NULL)
876                 panic("rtnetlink_init: cannot initialize rtnetlink\n");
877         netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
878         register_netdevice_notifier(&rtnetlink_dev_notifier);
879         rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
880         rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
881 }
882
883 EXPORT_SYMBOL(__rta_fill);
884 EXPORT_SYMBOL(rtattr_strlcpy);
885 EXPORT_SYMBOL(rtattr_parse);
886 EXPORT_SYMBOL(rtnetlink_links);
887 EXPORT_SYMBOL(rtnetlink_put_metrics);
888 EXPORT_SYMBOL(rtnl_lock);
889 EXPORT_SYMBOL(rtnl_trylock);
890 EXPORT_SYMBOL(rtnl_unlock);
891 EXPORT_SYMBOL(rtnl_unicast);
892 EXPORT_SYMBOL(rtnl_notify);
893 EXPORT_SYMBOL(rtnl_set_sk_err);