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