Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / linux / netlink / krt.c @ b982b6db

History | View | Annotate | Download (7.18 KB)

1
/*
2
 *        BIRD -- Linux Netlink Interface
3
 *
4
 *        (c) 1999 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#include <string.h>
10
#include <stdio.h>
11
#include <fcntl.h>
12
#include <sys/socket.h>
13
#include <sys/uio.h>
14

    
15
#define LOCAL_DEBUG
16

    
17
#undef ASYNC_NETLINK                        /* Define if async notifications should be used (debug) */
18

    
19
#include "nest/bird.h"
20
#include "nest/route.h"
21
#include "nest/protocol.h"
22
#include "nest/iface.h"
23
#include "lib/timer.h"
24
#include "lib/unix.h"
25
#include "lib/krt.h"
26
#include "lib/socket.h"
27

    
28
/*
29
 *        We need to work around namespace conflicts between us and the kernel,
30
 *        but I prefer this way to being forced to rename our configuration symbols.
31
 *        This will disappear as soon as netlink headers become part of the libc.
32
 */
33

    
34
#undef CONFIG_NETLINK
35
#include <linux/config.h>
36
#ifndef CONFIG_NETLINK
37
#error "Kernel not configured to support netlink"
38
#endif
39

    
40
#include <asm/types.h>
41
#include <linux/netlink.h>
42
#include <linux/rtnetlink.h>
43

    
44
#ifndef MSG_TRUNC                        /* FIXME: Hack to circumvent omissions in glibc includes */
45
#define MSG_TRUNC 0x20
46
#endif
47

    
48
struct proto_config *cf_krt;
49

    
50
/*
51
 *        Synchronous Netlink interface
52
 */
53

    
54
static int nl_sync_fd;                        /* Unix socket for synchronous netlink actions */
55
static u32 nl_sync_seq;                        /* Sequence number of last request sent */
56

    
57
static byte *nl_rx_buffer;                /* Receive buffer */
58
static int nl_rx_size = 8192;
59

    
60
static struct nlmsghdr *nl_last_hdr;        /* Recently received packet */
61
static unsigned int nl_last_size;
62

    
63
static void
64
nl_send(void *rq, int size)
65
{
66
  struct nlmsghdr *nh = rq;
67
  struct sockaddr_nl sa;
68

    
69
  memset(&sa, 0, sizeof(sa));
70
  sa.nl_family = AF_NETLINK;
71
  nh->nlmsg_len = size;
72
  nh->nlmsg_pid = 0;
73
  nh->nlmsg_seq = ++nl_sync_seq;
74
  if (sendto(nl_sync_fd, rq, size, 0, (struct sockaddr *)&sa, sizeof(sa)) < 0)
75
    die("rtnetlink sendto: %m");
76
  nl_last_hdr = NULL;
77
}
78

    
79
static void
80
nl_request_dump(int cmd)
81
{
82
  struct {
83
    struct nlmsghdr nh;
84
    struct rtgenmsg g;
85
  } req;
86
  req.nh.nlmsg_type = cmd;
87
  req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
88
  req.g.rtgen_family = PF_INET;
89
  nl_send(&req, sizeof(req));
90
}
91

    
92
static struct nlmsghdr *
93
nl_get_reply(void)
94
{
95
  for(;;)
96
    {
97
      if (!nl_last_hdr)
98
        {
99
          struct iovec iov = { nl_rx_buffer, nl_rx_size };
100
          struct sockaddr_nl sa;
101
          struct msghdr m = { (struct sockaddr *) &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
102
          int x = recvmsg(nl_sync_fd, &m, 0);
103
          if (x < 0)
104
            die("nl_get_reply: %m");
105
          nl_last_size = x;
106
          nl_last_hdr = (void *) nl_rx_buffer;
107
          if (m.msg_flags & MSG_TRUNC)
108
            bug("nl_get_reply: got truncated reply which should be impossible");
109
        }
110
      if (NLMSG_OK(nl_last_hdr, nl_last_size))
111
        {
112
          struct nlmsghdr *h = nl_last_hdr;
113
          if (h->nlmsg_seq != nl_sync_seq)
114
            {
115
              log(L_WARN "nl_get_reply: Ignoring out of sequence netlink packet (%x != %x)",
116
                  h->nlmsg_seq, nl_sync_seq);
117
              continue;
118
            }
119
          nl_last_hdr = NLMSG_NEXT(h, nl_last_size);
120
          return h;
121
        }
122
      if (nl_last_size)
123
        log(L_WARN "nl_get_reply: Found packet remnant of size %d", nl_last_size);
124
      nl_last_hdr = NULL;
125
    }
126
}
127

    
128
static char *
129
nl_error(struct nlmsghdr *h)
130
{
131
  struct nlmsgerr *e = NLMSG_DATA(h);
132
  if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
133
    return "Error message truncated";
134
  else
135
    return strerror(-e->error);
136
}
137

    
138
static struct nlmsghdr *
139
nl_get_scan(void)
140
{
141
  struct nlmsghdr *h = nl_get_reply();
142

    
143
  if (h->nlmsg_type == NLMSG_DONE)
144
    return NULL;
145
  if (h->nlmsg_type == NLMSG_ERROR)
146
    {
147
      log(L_ERR "Netlink error: %s", nl_error(h));
148
      return NULL;
149
    }
150
  return h;
151
}
152

    
153
/*
154
 *        Parsing of Netlink attributes
155
 */
156

    
157
static int nl_attr_len;
158

    
159
static void *
160
nl_checkin(struct nlmsghdr *h, int lsize)
161
{
162
  nl_attr_len = h->nlmsg_len - NLMSG_LENGTH(lsize);
163
  if (nl_attr_len < 0)
164
    {
165
      log(L_ERR "nl_checkin: underrun by %d bytes", -nl_attr_len);
166
      return NULL;
167
    }
168
  return NLMSG_DATA(h);
169
}
170

    
171
static int
172
nl_parse_attrs(struct rtattr *a, struct rtattr **k, int ksize)
173
{
174
  int max = ksize / sizeof(struct rtattr *);
175
  bzero(k, ksize);
176
  while (RTA_OK(a, nl_attr_len))
177
    {
178
      if (a->rta_type < max)
179
        k[a->rta_type] = a;
180
      a = RTA_NEXT(a, nl_attr_len);
181
    }
182
  if (nl_attr_len)
183
    {
184
      log(L_ERR "nl_parse_attrs: remnant of size %d", nl_attr_len);
185
      return 0;
186
    }
187
  else
188
    return 1;
189
}
190

    
191
/*
192
 *        Scanning of interfaces
193
 */
194

    
195
static void
196
nl_parse_link(struct nlmsghdr *h)
197
{
198
  struct ifinfomsg *i;
199
  struct rtattr *a[IFLA_STATS+1];
200

    
201
  if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(IFLA_RTA(i), a, sizeof(a)))
202
    return;
203
  DBG("NEWLINK %d\n", i->ifi_index);
204
}
205

    
206
static void
207
nl_parse_addr(struct nlmsghdr *h)
208
{
209
  struct ifaddrmsg *i;
210
  struct rtattr *a[IFA_ANYCAST+1];
211

    
212
  if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(IFA_RTA(i), a, sizeof(a)))
213
    return;
214
  DBG("NEWADDR %d\n", i->ifa_index);
215
}
216

    
217
static void
218
nl_scan_ifaces(void)
219
{
220
  struct nlmsghdr *h;
221

    
222
  nl_request_dump(RTM_GETLINK);
223
  while (h = nl_get_scan())
224
    if (h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)
225
      nl_parse_link(h);
226
    else
227
      log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type);
228

    
229
  nl_request_dump(RTM_GETADDR);
230
  while (h = nl_get_scan())
231
    if (h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR)
232
      nl_parse_addr(h);
233
    else
234
      log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type);
235

    
236
  bug("NIIDW");
237
}
238

    
239
/*
240
 *        Asynchronous Netlink interface
241
 */
242

    
243
static sock *nl_async_sk;                /* BIRD socket for asynchronous notifications */
244

    
245
static int
246
nl_async_hook(sock *sk, int size)
247
{
248
  DBG("nl_async_hook\n");
249
  return 0;
250
}
251

    
252
/*
253
 *        Protocol core
254
 */
255

    
256
static void
257
krt_preconfig(struct protocol *x, struct config *c)
258
{
259
  struct krt_config *z = proto_config_new(&proto_unix_kernel, sizeof(struct krt_config));
260

    
261
  cf_krt = &z->c;
262
  z->c.preference = DEF_PREF_UKR;
263
}
264

    
265
static struct proto *
266
krt_init(struct proto_config *c)
267
{
268
  struct krt_proto *p = proto_new(c, sizeof(struct krt_proto));
269

    
270
  return &p->p;
271
}
272

    
273
static void
274
nl_open_async(struct proto *p)
275
{
276
  sock *sk;
277
  struct sockaddr_nl sa;
278

    
279
  sk = nl_async_sk = sk_new(p->pool);
280
  sk->type = SK_MAGIC;
281
  sk->rx_hook = nl_async_hook;
282
  sk->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
283
  if (sk->fd < 0 || sk_open(sk))
284
    die("Unable to open secondary rtnetlink socket: %m");
285

    
286
  bzero(&sa, sizeof(sa));
287
  sa.nl_family = AF_NETLINK;
288
  sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
289
  if (bind(sk->fd, (struct sockaddr *) &sa, sizeof(sa)) < 0)
290
    die("Unable to bind secondary rtnetlink socket: %m");
291
}
292

    
293
static int
294
krt_start(struct proto *p)
295
{
296
#ifdef ASYNC_NETLINK
297
  nl_open_async(p);
298
#endif
299

    
300
  /* FIXME: Filter kernel routing table etc. */
301

    
302
  return PS_UP;
303
}
304

    
305
static int
306
krt_shutdown(struct proto *p)
307
{
308
  /* FIXME: Remove all our routes from the kernel */
309

    
310
  return PS_DOWN;
311
}
312

    
313
void
314
scan_if_init(void)
315
{
316
  nl_sync_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
317
  if (nl_sync_fd < 0)
318
    die("Unable to open rtnetlink socket: %m");
319
  nl_sync_seq = now;
320
  nl_rx_buffer = xmalloc(nl_rx_size);
321
  /* FIXME: Should we fetch our local address and compare it with addresses of all incoming messages? */
322

    
323
  nl_scan_ifaces();
324
}
325

    
326
struct protocol proto_unix_kernel = {
327
  name:                "Kernel",
328
  preconfig:        krt_preconfig,
329
  init:                krt_init,
330
  start:        krt_start,
331
  shutdown:        krt_shutdown
332
};