OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [core/] [rtnetlink.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1278 phoenix
/*
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/config.h>
20
#include <linux/errno.h>
21
#include <linux/types.h>
22
#include <linux/socket.h>
23
#include <linux/kernel.h>
24
#include <linux/major.h>
25
#include <linux/sched.h>
26
#include <linux/timer.h>
27
#include <linux/string.h>
28
#include <linux/sockios.h>
29
#include <linux/net.h>
30
#include <linux/fcntl.h>
31
#include <linux/mm.h>
32
#include <linux/slab.h>
33
#include <linux/interrupt.h>
34
#include <linux/capability.h>
35
#include <linux/skbuff.h>
36
#include <linux/init.h>
37
 
38
#include <asm/uaccess.h>
39
#include <asm/system.h>
40
#include <asm/string.h>
41
 
42
#include <linux/inet.h>
43
#include <linux/netdevice.h>
44
#include <net/ip.h>
45
#include <net/protocol.h>
46
#include <net/arp.h>
47
#include <net/route.h>
48
#include <net/udp.h>
49
#include <net/sock.h>
50
#include <net/pkt_sched.h>
51
 
52
DECLARE_MUTEX(rtnl_sem);
53
 
54
void rtnl_lock(void)
55
{
56
        rtnl_shlock();
57
        rtnl_exlock();
58
}
59
 
60
void rtnl_unlock(void)
61
{
62
        rtnl_exunlock();
63
        rtnl_shunlock();
64
}
65
 
66
int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
67
{
68
        memset(tb, 0, sizeof(struct rtattr*)*maxattr);
69
 
70
        while (RTA_OK(rta, len)) {
71
                unsigned flavor = rta->rta_type;
72
                if (flavor && flavor <= maxattr)
73
                        tb[flavor-1] = rta;
74
                rta = RTA_NEXT(rta, len);
75
        }
76
        return 0;
77
}
78
 
79
struct sock *rtnl;
80
 
81
struct rtnetlink_link * rtnetlink_links[NPROTO];
82
 
83
static const int rtm_min[(RTM_MAX+1-RTM_BASE)/4] =
84
{
85
        NLMSG_LENGTH(sizeof(struct ifinfomsg)),
86
        NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
87
        NLMSG_LENGTH(sizeof(struct rtmsg)),
88
        NLMSG_LENGTH(sizeof(struct ndmsg)),
89
        NLMSG_LENGTH(sizeof(struct rtmsg)),
90
        NLMSG_LENGTH(sizeof(struct tcmsg)),
91
        NLMSG_LENGTH(sizeof(struct tcmsg)),
92
        NLMSG_LENGTH(sizeof(struct tcmsg))
93
};
94
 
95
static const int rta_max[(RTM_MAX+1-RTM_BASE)/4] =
96
{
97
        IFLA_MAX,
98
        IFA_MAX,
99
        RTA_MAX,
100
        NDA_MAX,
101
        RTA_MAX,
102
        TCA_MAX,
103
        TCA_MAX,
104
        TCA_MAX
105
};
106
 
107
void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
108
{
109
        struct rtattr *rta;
110
        int size = RTA_LENGTH(attrlen);
111
 
112
        rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
113
        rta->rta_type = attrtype;
114
        rta->rta_len = size;
115
        memcpy(RTA_DATA(rta), data, attrlen);
116
}
117
 
118
int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
119
{
120
        int err = 0;
121
 
122
        NETLINK_CB(skb).dst_groups = group;
123
        if (echo)
124
                atomic_inc(&skb->users);
125
        netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
126
        if (echo)
127
                err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
128
        return err;
129
}
130
 
131
int rtnetlink_put_metrics(struct sk_buff *skb, unsigned *metrics)
132
{
133
        struct rtattr *mx = (struct rtattr*)skb->tail;
134
        int i;
135
 
136
        RTA_PUT(skb, RTA_METRICS, 0, NULL);
137
        for (i=0; i<RTAX_MAX; i++) {
138
                if (metrics[i])
139
                        RTA_PUT(skb, i+1, sizeof(unsigned), metrics+i);
140
        }
141
        mx->rta_len = skb->tail - (u8*)mx;
142
        if (mx->rta_len == RTA_LENGTH(0))
143
                skb_trim(skb, (u8*)mx - skb->data);
144
        return 0;
145
 
146
rtattr_failure:
147
        skb_trim(skb, (u8*)mx - skb->data);
148
        return -1;
149
}
150
 
151
 
152
static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
153
                                 int type, u32 pid, u32 seq, u32 change)
154
{
155
        struct ifinfomsg *r;
156
        struct nlmsghdr  *nlh;
157
        unsigned char    *b = skb->tail;
158
 
159
        nlh = NLMSG_PUT(skb, pid, seq, type, sizeof(*r));
160
        if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
161
        r = NLMSG_DATA(nlh);
162
        r->ifi_family = AF_UNSPEC;
163
        r->ifi_type = dev->type;
164
        r->ifi_index = dev->ifindex;
165
        r->ifi_flags = dev->flags;
166
        r->ifi_change = change;
167
 
168
        if (!netif_running(dev) || !netif_carrier_ok(dev))
169
                r->ifi_flags &= ~IFF_RUNNING;
170
        else
171
                r->ifi_flags |= IFF_RUNNING;
172
 
173
        RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
174
        if (dev->addr_len) {
175
                RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
176
                RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
177
        }
178
        if (1) {
179
                unsigned mtu = dev->mtu;
180
                RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
181
        }
182
        if (dev->ifindex != dev->iflink)
183
                RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
184
        if (dev->qdisc_sleeping)
185
                RTA_PUT(skb, IFLA_QDISC,
186
                        strlen(dev->qdisc_sleeping->ops->id) + 1,
187
                        dev->qdisc_sleeping->ops->id);
188
        if (dev->master)
189
                RTA_PUT(skb, IFLA_MASTER, sizeof(int), &dev->master->ifindex);
190
        if (dev->get_stats) {
191
                struct net_device_stats *stats = dev->get_stats(dev);
192
                if (stats)
193
                        RTA_PUT(skb, IFLA_STATS, sizeof(*stats), stats);
194
        }
195
        nlh->nlmsg_len = skb->tail - b;
196
        return skb->len;
197
 
198
nlmsg_failure:
199
rtattr_failure:
200
        skb_trim(skb, b - skb->data);
201
        return -1;
202
}
203
 
204
int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
205
{
206
        int idx;
207
        int s_idx = cb->args[0];
208
        struct net_device *dev;
209
 
210
        read_lock(&dev_base_lock);
211
        for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
212
                if (idx < s_idx)
213
                        continue;
214
                if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, 0) <= 0)
215
                        break;
216
        }
217
        read_unlock(&dev_base_lock);
218
        cb->args[0] = idx;
219
 
220
        return skb->len;
221
}
222
 
223
int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
224
{
225
        int idx;
226
        int s_idx = cb->family;
227
 
228
        if (s_idx == 0)
229
                s_idx = 1;
230
        for (idx=1; idx<NPROTO; idx++) {
231
                int type = cb->nlh->nlmsg_type-RTM_BASE;
232
                if (idx < s_idx || idx == PF_PACKET)
233
                        continue;
234
                if (rtnetlink_links[idx] == NULL ||
235
                    rtnetlink_links[idx][type].dumpit == NULL)
236
                        continue;
237
                if (idx > s_idx)
238
                        memset(&cb->args[0], 0, sizeof(cb->args));
239
                if (rtnetlink_links[idx][type].dumpit(skb, cb))
240
                        break;
241
        }
242
        cb->family = idx;
243
 
244
        return skb->len;
245
}
246
 
247
void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
248
{
249
        struct sk_buff *skb;
250
        int size = NLMSG_GOODSIZE;
251
 
252
        skb = alloc_skb(size, GFP_KERNEL);
253
        if (!skb)
254
                return;
255
 
256
        if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change) < 0) {
257
                kfree_skb(skb);
258
                return;
259
        }
260
        NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
261
        netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_KERNEL);
262
}
263
 
264
static int rtnetlink_done(struct netlink_callback *cb)
265
{
266
        return 0;
267
}
268
 
269
/* Process one rtnetlink message. */
270
 
271
static __inline__ int
272
rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
273
{
274
        struct rtnetlink_link *link;
275
        struct rtnetlink_link *link_tab;
276
        struct rtattr   *rta[RTATTR_MAX];
277
 
278
        int exclusive = 0;
279
        int sz_idx, kind;
280
        int min_len;
281
        int family;
282
        int type;
283
        int err;
284
 
285
        /* Only requests are handled by kernel now */
286
        if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
287
                return 0;
288
 
289
        type = nlh->nlmsg_type;
290
 
291
        /* A control message: ignore them */
292
        if (type < RTM_BASE)
293
                return 0;
294
 
295
        /* Unknown message: reply with EINVAL */
296
        if (type > RTM_MAX)
297
                goto err_inval;
298
 
299
        type -= RTM_BASE;
300
 
301
        /* All the messages must have at least 1 byte length */
302
        if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
303
                return 0;
304
 
305
        family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
306
        if (family > NPROTO) {
307
                *errp = -EAFNOSUPPORT;
308
                return -1;
309
        }
310
 
311
        link_tab = rtnetlink_links[family];
312
        if (link_tab == NULL)
313
                link_tab = rtnetlink_links[PF_UNSPEC];
314
        link = &link_tab[type];
315
 
316
        sz_idx = type>>2;
317
        kind = type&3;
318
 
319
        if (kind != 2 && !cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN)) {
320
                *errp = -EPERM;
321
                return -1;
322
        }
323
 
324
        if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
325
                u32 rlen;
326
 
327
                if (link->dumpit == NULL)
328
                        link = &(rtnetlink_links[PF_UNSPEC][type]);
329
 
330
                if (link->dumpit == NULL)
331
                        goto err_inval;
332
 
333
                if ((*errp = netlink_dump_start(rtnl, skb, nlh,
334
                                                link->dumpit,
335
                                                rtnetlink_done)) != 0) {
336
                        return -1;
337
                }
338
                rlen = NLMSG_ALIGN(nlh->nlmsg_len);
339
                if (rlen > skb->len)
340
                        rlen = skb->len;
341
                skb_pull(skb, rlen);
342
                return -1;
343
        }
344
 
345
        if (kind != 2) {
346
                if (rtnl_exlock_nowait()) {
347
                        *errp = 0;
348
                        return -1;
349
                }
350
                exclusive = 1;
351
        }
352
 
353
        memset(&rta, 0, sizeof(rta));
354
 
355
        min_len = rtm_min[sz_idx];
356
        if (nlh->nlmsg_len < min_len)
357
                goto err_inval;
358
 
359
        if (nlh->nlmsg_len > min_len) {
360
                int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
361
                struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
362
 
363
                while (RTA_OK(attr, attrlen)) {
364
                        unsigned flavor = attr->rta_type;
365
                        if (flavor) {
366
                                if (flavor > rta_max[sz_idx])
367
                                        goto err_inval;
368
                                rta[flavor-1] = attr;
369
                        }
370
                        attr = RTA_NEXT(attr, attrlen);
371
                }
372
        }
373
 
374
        if (link->doit == NULL)
375
                link = &(rtnetlink_links[PF_UNSPEC][type]);
376
        if (link->doit == NULL)
377
                goto err_inval;
378
        err = link->doit(skb, nlh, (void *)&rta);
379
 
380
        if (exclusive)
381
                rtnl_exunlock();
382
        *errp = err;
383
        return err;
384
 
385
err_inval:
386
        if (exclusive)
387
                rtnl_exunlock();
388
        *errp = -EINVAL;
389
        return -1;
390
}
391
 
392
/*
393
 * Process one packet of messages.
394
 * Malformed skbs with wrong lengths of messages are discarded silently.
395
 */
396
 
397
static inline int rtnetlink_rcv_skb(struct sk_buff *skb)
398
{
399
        int err;
400
        struct nlmsghdr * nlh;
401
 
402
        while (skb->len >= NLMSG_SPACE(0)) {
403
                u32 rlen;
404
 
405
                nlh = (struct nlmsghdr *)skb->data;
406
                if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
407
                        return 0;
408
                rlen = NLMSG_ALIGN(nlh->nlmsg_len);
409
                if (rlen > skb->len)
410
                        rlen = skb->len;
411
                if (rtnetlink_rcv_msg(skb, nlh, &err)) {
412
                        /* Not error, but we must interrupt processing here:
413
                         *   Note, that in this case we do not pull message
414
                         *   from skb, it will be processed later.
415
                         */
416
                        if (err == 0)
417
                                return -1;
418
                        netlink_ack(skb, nlh, err);
419
                } else if (nlh->nlmsg_flags&NLM_F_ACK)
420
                        netlink_ack(skb, nlh, 0);
421
                skb_pull(skb, rlen);
422
        }
423
 
424
        return 0;
425
}
426
 
427
/*
428
 *  rtnetlink input queue processing routine:
429
 *      - try to acquire shared lock. If it is failed, defer processing.
430
 *      - feed skbs to rtnetlink_rcv_skb, until it refuse a message,
431
 *        that will occur, when a dump started and/or acquisition of
432
 *        exclusive lock failed.
433
 */
434
 
435
static void rtnetlink_rcv(struct sock *sk, int len)
436
{
437
        do {
438
                struct sk_buff *skb;
439
 
440
                if (rtnl_shlock_nowait())
441
                        return;
442
 
443
                while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
444
                        if (rtnetlink_rcv_skb(skb)) {
445
                                if (skb->len)
446
                                        skb_queue_head(&sk->receive_queue, skb);
447
                                else
448
                                        kfree_skb(skb);
449
                                break;
450
                        }
451
                        kfree_skb(skb);
452
                }
453
 
454
                up(&rtnl_sem);
455
        } while (rtnl && rtnl->receive_queue.qlen);
456
}
457
 
458
static struct rtnetlink_link link_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
459
{
460
        { NULL,                 NULL,                   },
461
        { NULL,                 NULL,                   },
462
        { NULL,                 rtnetlink_dump_ifinfo,  },
463
        { NULL,                 NULL,                   },
464
 
465
        { NULL,                 NULL,                   },
466
        { NULL,                 NULL,                   },
467
        { NULL,                 rtnetlink_dump_all,     },
468
        { NULL,                 NULL,                   },
469
 
470
        { NULL,                 NULL,                   },
471
        { NULL,                 NULL,                   },
472
        { NULL,                 rtnetlink_dump_all,     },
473
        { NULL,                 NULL,                   },
474
 
475
        { neigh_add,            NULL,                   },
476
        { neigh_delete,         NULL,                   },
477
        { NULL,                 neigh_dump_info,        },
478
        { NULL,                 NULL,                   },
479
 
480
        { NULL,                 NULL,                   },
481
        { NULL,                 NULL,                   },
482
        { NULL,                 NULL,                   },
483
        { NULL,                 NULL,                   },
484
};
485
 
486
 
487
static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
488
{
489
        struct net_device *dev = ptr;
490
        switch (event) {
491
        case NETDEV_UNREGISTER:
492
                rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
493
                break;
494
        case NETDEV_REGISTER:
495
                rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
496
                break;
497
        case NETDEV_UP:
498
        case NETDEV_DOWN:
499
                rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
500
                break;
501
        case NETDEV_CHANGE:
502
        case NETDEV_GOING_DOWN:
503
                break;
504
        default:
505
                rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
506
                break;
507
        }
508
        return NOTIFY_DONE;
509
}
510
 
511
struct notifier_block rtnetlink_dev_notifier = {
512
        rtnetlink_event,
513
        NULL,
514
 
515
};
516
 
517
 
518
void __init rtnetlink_init(void)
519
{
520
#ifdef RTNL_DEBUG
521
        printk("Initializing RT netlink socket\n");
522
#endif
523
        rtnl = netlink_kernel_create(NETLINK_ROUTE, rtnetlink_rcv);
524
        if (rtnl == NULL)
525
                panic("rtnetlink_init: cannot initialize rtnetlink\n");
526
        netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
527
        register_netdevice_notifier(&rtnetlink_dev_notifier);
528
        rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
529
        rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
530
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.