Revision b982b6db

View differences:

TODO
21 21
- broadcast/multicast echoing suppresion
22 22
- we need ifindex at least for PtP links
23 23

  
24
- kernel: RTM_F_NOTIFY seems to be unimplemented
25
- kernel: RTM_DELROUTE not announced for device routes
24
- kernel: RTM_DELROUTE not announced for device routes [confirmed by Alexey: it's a feature]
26 25

  
27 26
- netlink: import Linux route attributes to our rta's, so that they can be filtered?
28 27

  
......
35 34
- check log calls for trailing newlines
36 35
- check if all protocols set proper packet priorities and TTL's.
37 36
- replace all NUM's by expr's
37
- try compiling with -Wunused
38 38

  
39 39
Various ideas
40 40
~~~~~~~~~~~~~
sysdep/linux/netlink/krt.c
10 10
#include <stdio.h>
11 11
#include <fcntl.h>
12 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) */
13 18

  
14 19
#include "nest/bird.h"
15 20
#include "nest/route.h"
......
18 23
#include "lib/timer.h"
19 24
#include "lib/unix.h"
20 25
#include "lib/krt.h"
26
#include "lib/socket.h"
21 27

  
22 28
/*
23 29
 *	We need to work around namespace conflicts between us and the kernel,
......
35 41
#include <linux/netlink.h>
36 42
#include <linux/rtnetlink.h>
37 43

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

  
40 48
struct proto_config *cf_krt;
41 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

  
42 256
static void
43 257
krt_preconfig(struct protocol *x, struct config *c)
44 258
{
......
56 270
  return &p->p;
57 271
}
58 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

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

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

  
64 302
  return PS_UP;
......
75 313
void
76 314
scan_if_init(void)
77 315
{
78
  /* FIXME: What to do here? */
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();
79 324
}
80 325

  
81 326
struct protocol proto_unix_kernel = {

Also available in: Unified diff