iof-bird-daemon / sysdep / linux / netlink.c @ 574b2324
History | View | Annotate | Download (47.7 KB)
1 |
/*
|
---|---|
2 |
* BIRD -- Linux Netlink Interface
|
3 |
*
|
4 |
* (c) 1999--2000 Martin Mares <mj@ucw.cz>
|
5 |
*
|
6 |
* Can be freely distributed and used under the terms of the GNU GPL.
|
7 |
*/
|
8 |
|
9 |
#include <alloca.h> |
10 |
#include <stdio.h> |
11 |
#include <unistd.h> |
12 |
#include <fcntl.h> |
13 |
#include <sys/socket.h> |
14 |
#include <sys/uio.h> |
15 |
#include <errno.h> |
16 |
|
17 |
#undef LOCAL_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/alloca.h" |
24 |
#include "sysdep/unix/timer.h" |
25 |
#include "sysdep/unix/unix.h" |
26 |
#include "sysdep/unix/krt.h" |
27 |
#include "lib/socket.h" |
28 |
#include "lib/string.h" |
29 |
#include "lib/hash.h" |
30 |
#include "conf/conf.h" |
31 |
|
32 |
#include <asm/types.h> |
33 |
#include <linux/if.h> |
34 |
#ifdef HAVE_LWTUNNEL
|
35 |
#include <linux/lwtunnel.h> |
36 |
#else
|
37 |
#include "sysdep/linux/lwtunnel.h" |
38 |
#endif
|
39 |
#include <linux/netlink.h> |
40 |
#include <linux/rtnetlink.h> |
41 |
|
42 |
|
43 |
#ifndef MSG_TRUNC /* Hack: Several versions of glibc miss this one :( */ |
44 |
#define MSG_TRUNC 0x20 |
45 |
#endif
|
46 |
|
47 |
#ifndef IFA_FLAGS
|
48 |
#define IFA_FLAGS 8 |
49 |
#endif
|
50 |
|
51 |
#ifndef IFF_LOWER_UP
|
52 |
#define IFF_LOWER_UP 0x10000 |
53 |
#endif
|
54 |
|
55 |
#ifndef RTA_TABLE
|
56 |
#define RTA_TABLE 15 |
57 |
#endif
|
58 |
|
59 |
#ifndef RTA_VIA
|
60 |
#define RTA_VIA 18 |
61 |
#endif
|
62 |
|
63 |
#ifndef HAVE_STRUCT_RTVIA
|
64 |
struct rtvia {
|
65 |
unsigned short rtvia_family; |
66 |
u8 rtvia_addr[0];
|
67 |
}; |
68 |
#endif
|
69 |
|
70 |
#ifndef RTA_NEWDST
|
71 |
#define RTA_NEWDST 19 |
72 |
#endif
|
73 |
|
74 |
#ifndef RTA_ENCAP_TYPE
|
75 |
#define RTA_ENCAP_TYPE 21 |
76 |
#endif
|
77 |
|
78 |
#ifndef RTA_ENCAP
|
79 |
#define RTA_ENCAP 22 |
80 |
#endif
|
81 |
|
82 |
#define krt_ecmp6(p) ((p)->af == AF_INET6)
|
83 |
|
84 |
/*
|
85 |
* Structure nl_parse_state keeps state of received route processing. Ideally,
|
86 |
* we could just independently parse received Netlink messages and immediately
|
87 |
* propagate received routes to the rest of BIRD, but Linux kernel represents
|
88 |
* and announces IPv6 ECMP routes not as one route with multiple next hops (like
|
89 |
* RTA_MULTIPATH in IPv4 ECMP), but as a set of routes with the same prefix.
|
90 |
*
|
91 |
* Therefore, BIRD keeps currently processed route in nl_parse_state structure
|
92 |
* and postpones its propagation until we expect it to be final; i.e., when
|
93 |
* non-matching route is received or when the scan ends. When another matching
|
94 |
* route is received, it is merged with the already processed route to form an
|
95 |
* ECMP route. Note that merging is done only for IPv6 (merge == 1), but the
|
96 |
* postponing is done in both cases (for simplicity). All IPv4 routes are just
|
97 |
* considered non-matching.
|
98 |
*
|
99 |
* This is ignored for asynchronous notifications (every notification is handled
|
100 |
* as a separate route). It is not an issue for our routes, as we ignore such
|
101 |
* notifications anyways. But importing alien IPv6 ECMP routes does not work
|
102 |
* properly.
|
103 |
*/
|
104 |
|
105 |
struct nl_parse_state
|
106 |
{ |
107 |
struct linpool *pool;
|
108 |
int scan;
|
109 |
int merge;
|
110 |
|
111 |
net *net; |
112 |
rta *attrs; |
113 |
struct krt_proto *proto;
|
114 |
s8 new; |
115 |
s8 krt_src; |
116 |
u8 krt_type; |
117 |
u8 krt_proto; |
118 |
u32 krt_metric; |
119 |
}; |
120 |
|
121 |
/*
|
122 |
* Synchronous Netlink interface
|
123 |
*/
|
124 |
|
125 |
struct nl_sock
|
126 |
{ |
127 |
int fd;
|
128 |
u32 seq; |
129 |
byte *rx_buffer; /* Receive buffer */
|
130 |
struct nlmsghdr *last_hdr; /* Recently received packet */ |
131 |
uint last_size; |
132 |
}; |
133 |
|
134 |
#define NL_RX_SIZE 8192 |
135 |
|
136 |
#define NL_OP_DELETE 0 |
137 |
#define NL_OP_ADD (NLM_F_CREATE|NLM_F_EXCL)
|
138 |
#define NL_OP_REPLACE (NLM_F_CREATE|NLM_F_REPLACE)
|
139 |
#define NL_OP_APPEND (NLM_F_CREATE|NLM_F_APPEND)
|
140 |
|
141 |
static linpool *nl_linpool;
|
142 |
|
143 |
static struct nl_sock nl_scan = {.fd = -1}; /* Netlink socket for synchronous scan */ |
144 |
static struct nl_sock nl_req = {.fd = -1}; /* Netlink socket for requests */ |
145 |
|
146 |
static void |
147 |
nl_open_sock(struct nl_sock *nl)
|
148 |
{ |
149 |
if (nl->fd < 0) |
150 |
{ |
151 |
nl->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
152 |
if (nl->fd < 0) |
153 |
die("Unable to open rtnetlink socket: %m");
|
154 |
nl->seq = (u32) (current_time() TO_S); /* Or perhaps random_u32() ? */
|
155 |
nl->rx_buffer = xmalloc(NL_RX_SIZE); |
156 |
nl->last_hdr = NULL;
|
157 |
nl->last_size = 0;
|
158 |
} |
159 |
} |
160 |
|
161 |
static void |
162 |
nl_open(void)
|
163 |
{ |
164 |
nl_open_sock(&nl_scan); |
165 |
nl_open_sock(&nl_req); |
166 |
} |
167 |
|
168 |
static void |
169 |
nl_send(struct nl_sock *nl, struct nlmsghdr *nh) |
170 |
{ |
171 |
struct sockaddr_nl sa;
|
172 |
|
173 |
memset(&sa, 0, sizeof(sa)); |
174 |
sa.nl_family = AF_NETLINK; |
175 |
nh->nlmsg_pid = 0;
|
176 |
nh->nlmsg_seq = ++(nl->seq); |
177 |
if (sendto(nl->fd, nh, nh->nlmsg_len, 0, (struct sockaddr *)&sa, sizeof(sa)) < 0) |
178 |
die("rtnetlink sendto: %m");
|
179 |
nl->last_hdr = NULL;
|
180 |
} |
181 |
|
182 |
static void |
183 |
nl_request_dump(int af, int cmd) |
184 |
{ |
185 |
struct {
|
186 |
struct nlmsghdr nh;
|
187 |
struct rtgenmsg g;
|
188 |
} req = { |
189 |
.nh.nlmsg_type = cmd, |
190 |
.nh.nlmsg_len = sizeof(req),
|
191 |
.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP, |
192 |
.g.rtgen_family = af |
193 |
}; |
194 |
nl_send(&nl_scan, &req.nh); |
195 |
} |
196 |
|
197 |
static struct nlmsghdr * |
198 |
nl_get_reply(struct nl_sock *nl)
|
199 |
{ |
200 |
for(;;)
|
201 |
{ |
202 |
if (!nl->last_hdr)
|
203 |
{ |
204 |
struct iovec iov = { nl->rx_buffer, NL_RX_SIZE };
|
205 |
struct sockaddr_nl sa;
|
206 |
struct msghdr m = {
|
207 |
.msg_name = &sa, |
208 |
.msg_namelen = sizeof(sa),
|
209 |
.msg_iov = &iov, |
210 |
.msg_iovlen = 1,
|
211 |
}; |
212 |
int x = recvmsg(nl->fd, &m, 0); |
213 |
if (x < 0) |
214 |
die("nl_get_reply: %m");
|
215 |
if (sa.nl_pid) /* It isn't from the kernel */ |
216 |
{ |
217 |
DBG("Non-kernel packet\n");
|
218 |
continue;
|
219 |
} |
220 |
nl->last_size = x; |
221 |
nl->last_hdr = (void *) nl->rx_buffer;
|
222 |
if (m.msg_flags & MSG_TRUNC)
|
223 |
bug("nl_get_reply: got truncated reply which should be impossible");
|
224 |
} |
225 |
if (NLMSG_OK(nl->last_hdr, nl->last_size))
|
226 |
{ |
227 |
struct nlmsghdr *h = nl->last_hdr;
|
228 |
nl->last_hdr = NLMSG_NEXT(h, nl->last_size); |
229 |
if (h->nlmsg_seq != nl->seq)
|
230 |
{ |
231 |
log(L_WARN "nl_get_reply: Ignoring out of sequence netlink packet (%x != %x)",
|
232 |
h->nlmsg_seq, nl->seq); |
233 |
continue;
|
234 |
} |
235 |
return h;
|
236 |
} |
237 |
if (nl->last_size)
|
238 |
log(L_WARN "nl_get_reply: Found packet remnant of size %d", nl->last_size);
|
239 |
nl->last_hdr = NULL;
|
240 |
} |
241 |
} |
242 |
|
243 |
static struct tbf rl_netlink_err = TBF_DEFAULT_LOG_LIMITS; |
244 |
|
245 |
static int |
246 |
nl_error(struct nlmsghdr *h, int ignore_esrch) |
247 |
{ |
248 |
struct nlmsgerr *e;
|
249 |
int ec;
|
250 |
|
251 |
if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) |
252 |
{ |
253 |
log(L_WARN "Netlink: Truncated error message received");
|
254 |
return ENOBUFS;
|
255 |
} |
256 |
e = (struct nlmsgerr *) NLMSG_DATA(h);
|
257 |
ec = -e->error; |
258 |
if (ec && !(ignore_esrch && (ec == ESRCH)))
|
259 |
log_rl(&rl_netlink_err, L_WARN "Netlink: %s", strerror(ec));
|
260 |
return ec;
|
261 |
} |
262 |
|
263 |
static struct nlmsghdr * |
264 |
nl_get_scan(void)
|
265 |
{ |
266 |
struct nlmsghdr *h = nl_get_reply(&nl_scan);
|
267 |
|
268 |
if (h->nlmsg_type == NLMSG_DONE)
|
269 |
return NULL; |
270 |
if (h->nlmsg_type == NLMSG_ERROR)
|
271 |
{ |
272 |
nl_error(h, 0);
|
273 |
return NULL; |
274 |
} |
275 |
return h;
|
276 |
} |
277 |
|
278 |
static int |
279 |
nl_exchange(struct nlmsghdr *pkt, int ignore_esrch) |
280 |
{ |
281 |
struct nlmsghdr *h;
|
282 |
|
283 |
nl_send(&nl_req, pkt); |
284 |
for(;;)
|
285 |
{ |
286 |
h = nl_get_reply(&nl_req); |
287 |
if (h->nlmsg_type == NLMSG_ERROR)
|
288 |
break;
|
289 |
log(L_WARN "nl_exchange: Unexpected reply received");
|
290 |
} |
291 |
return nl_error(h, ignore_esrch) ? -1 : 0; |
292 |
} |
293 |
|
294 |
/*
|
295 |
* Netlink attributes
|
296 |
*/
|
297 |
|
298 |
static int nl_attr_len; |
299 |
|
300 |
static void * |
301 |
nl_checkin(struct nlmsghdr *h, int lsize) |
302 |
{ |
303 |
nl_attr_len = h->nlmsg_len - NLMSG_LENGTH(lsize); |
304 |
if (nl_attr_len < 0) |
305 |
{ |
306 |
log(L_ERR "nl_checkin: underrun by %d bytes", -nl_attr_len);
|
307 |
return NULL; |
308 |
} |
309 |
return NLMSG_DATA(h);
|
310 |
} |
311 |
|
312 |
struct nl_want_attrs {
|
313 |
u8 defined:1;
|
314 |
u8 checksize:1;
|
315 |
u8 size; |
316 |
}; |
317 |
|
318 |
|
319 |
#define BIRD_IFLA_MAX (IFLA_WIRELESS+1) |
320 |
|
321 |
static struct nl_want_attrs ifla_attr_want[BIRD_IFLA_MAX] = { |
322 |
[IFLA_IFNAME] = { 1, 0, 0 }, |
323 |
[IFLA_MTU] = { 1, 1, sizeof(u32) }, |
324 |
[IFLA_WIRELESS] = { 1, 0, 0 }, |
325 |
}; |
326 |
|
327 |
|
328 |
#define BIRD_IFA_MAX (IFA_FLAGS+1) |
329 |
|
330 |
static struct nl_want_attrs ifa_attr_want4[BIRD_IFA_MAX] = { |
331 |
[IFA_ADDRESS] = { 1, 1, sizeof(ip4_addr) }, |
332 |
[IFA_LOCAL] = { 1, 1, sizeof(ip4_addr) }, |
333 |
[IFA_BROADCAST] = { 1, 1, sizeof(ip4_addr) }, |
334 |
[IFA_FLAGS] = { 1, 1, sizeof(u32) }, |
335 |
}; |
336 |
|
337 |
static struct nl_want_attrs ifa_attr_want6[BIRD_IFA_MAX] = { |
338 |
[IFA_ADDRESS] = { 1, 1, sizeof(ip6_addr) }, |
339 |
[IFA_LOCAL] = { 1, 1, sizeof(ip6_addr) }, |
340 |
[IFA_FLAGS] = { 1, 1, sizeof(u32) }, |
341 |
}; |
342 |
|
343 |
|
344 |
#define BIRD_RTA_MAX (RTA_ENCAP+1) |
345 |
|
346 |
static struct nl_want_attrs nexthop_attr_want4[BIRD_RTA_MAX] = { |
347 |
[RTA_GATEWAY] = { 1, 1, sizeof(ip4_addr) }, |
348 |
[RTA_ENCAP_TYPE]= { 1, 1, sizeof(u16) }, |
349 |
[RTA_ENCAP] = { 1, 0, 0 }, |
350 |
}; |
351 |
|
352 |
static struct nl_want_attrs encap_mpls_want[BIRD_RTA_MAX] = { |
353 |
[RTA_DST] = { 1, 0, 0 }, |
354 |
}; |
355 |
|
356 |
static struct nl_want_attrs rtm_attr_want4[BIRD_RTA_MAX] = { |
357 |
[RTA_DST] = { 1, 1, sizeof(ip4_addr) }, |
358 |
[RTA_OIF] = { 1, 1, sizeof(u32) }, |
359 |
[RTA_GATEWAY] = { 1, 1, sizeof(ip4_addr) }, |
360 |
[RTA_PRIORITY] = { 1, 1, sizeof(u32) }, |
361 |
[RTA_PREFSRC] = { 1, 1, sizeof(ip4_addr) }, |
362 |
[RTA_METRICS] = { 1, 0, 0 }, |
363 |
[RTA_MULTIPATH] = { 1, 0, 0 }, |
364 |
[RTA_FLOW] = { 1, 1, sizeof(u32) }, |
365 |
[RTA_TABLE] = { 1, 1, sizeof(u32) }, |
366 |
[RTA_ENCAP_TYPE]= { 1, 1, sizeof(u16) }, |
367 |
[RTA_ENCAP] = { 1, 0, 0 }, |
368 |
}; |
369 |
|
370 |
static struct nl_want_attrs rtm_attr_want6[BIRD_RTA_MAX] = { |
371 |
[RTA_DST] = { 1, 1, sizeof(ip6_addr) }, |
372 |
[RTA_IIF] = { 1, 1, sizeof(u32) }, |
373 |
[RTA_OIF] = { 1, 1, sizeof(u32) }, |
374 |
[RTA_GATEWAY] = { 1, 1, sizeof(ip6_addr) }, |
375 |
[RTA_PRIORITY] = { 1, 1, sizeof(u32) }, |
376 |
[RTA_PREFSRC] = { 1, 1, sizeof(ip6_addr) }, |
377 |
[RTA_METRICS] = { 1, 0, 0 }, |
378 |
[RTA_FLOW] = { 1, 1, sizeof(u32) }, |
379 |
[RTA_TABLE] = { 1, 1, sizeof(u32) }, |
380 |
[RTA_ENCAP_TYPE]= { 1, 1, sizeof(u16) }, |
381 |
[RTA_ENCAP] = { 1, 0, 0 }, |
382 |
}; |
383 |
|
384 |
static struct nl_want_attrs rtm_attr_want_mpls[BIRD_RTA_MAX] = { |
385 |
[RTA_DST] = { 1, 1, sizeof(u32) }, |
386 |
[RTA_IIF] = { 1, 1, sizeof(u32) }, |
387 |
[RTA_OIF] = { 1, 1, sizeof(u32) }, |
388 |
[RTA_PRIORITY] = { 1, 1, sizeof(u32) }, |
389 |
[RTA_METRICS] = { 1, 0, 0 }, |
390 |
[RTA_FLOW] = { 1, 1, sizeof(u32) }, |
391 |
[RTA_TABLE] = { 1, 1, sizeof(u32) }, |
392 |
[RTA_VIA] = { 1, 0, 0 }, |
393 |
[RTA_NEWDST] = { 1, 0, 0 }, |
394 |
}; |
395 |
|
396 |
|
397 |
static int |
398 |
nl_parse_attrs(struct rtattr *a, struct nl_want_attrs *want, struct rtattr **k, int ksize) |
399 |
{ |
400 |
int max = ksize / sizeof(struct rtattr *); |
401 |
bzero(k, ksize); |
402 |
|
403 |
for ( ; RTA_OK(a, nl_attr_len); a = RTA_NEXT(a, nl_attr_len))
|
404 |
{ |
405 |
if ((a->rta_type >= max) || !want[a->rta_type].defined)
|
406 |
continue;
|
407 |
|
408 |
if (want[a->rta_type].checksize && (RTA_PAYLOAD(a) != want[a->rta_type].size))
|
409 |
{ |
410 |
log(L_ERR "nl_parse_attrs: Malformed attribute received");
|
411 |
return 0; |
412 |
} |
413 |
|
414 |
k[a->rta_type] = a; |
415 |
} |
416 |
|
417 |
if (nl_attr_len)
|
418 |
{ |
419 |
log(L_ERR "nl_parse_attrs: remnant of size %d", nl_attr_len);
|
420 |
return 0; |
421 |
} |
422 |
|
423 |
return 1; |
424 |
} |
425 |
|
426 |
static inline u16 rta_get_u16(struct rtattr *a) |
427 |
{ return *(u16 *) RTA_DATA(a); }
|
428 |
|
429 |
static inline u32 rta_get_u32(struct rtattr *a) |
430 |
{ return *(u32 *) RTA_DATA(a); }
|
431 |
|
432 |
static inline ip4_addr rta_get_ip4(struct rtattr *a) |
433 |
{ return ip4_ntoh(*(ip4_addr *) RTA_DATA(a)); }
|
434 |
|
435 |
static inline ip6_addr rta_get_ip6(struct rtattr *a) |
436 |
{ return ip6_ntoh(*(ip6_addr *) RTA_DATA(a)); }
|
437 |
|
438 |
static inline ip_addr rta_get_ipa(struct rtattr *a) |
439 |
{ |
440 |
if (RTA_PAYLOAD(a) == sizeof(ip4_addr)) |
441 |
return ipa_from_ip4(rta_get_ip4(a));
|
442 |
else
|
443 |
return ipa_from_ip6(rta_get_ip6(a));
|
444 |
} |
445 |
|
446 |
static inline ip_addr rta_get_via(struct rtattr *a) |
447 |
{ |
448 |
struct rtvia *v = RTA_DATA(a);
|
449 |
switch(v->rtvia_family) {
|
450 |
case AF_INET: return ipa_from_ip4(ip4_ntoh(*(ip4_addr *) v->rtvia_addr)); |
451 |
case AF_INET6: return ipa_from_ip6(ip6_ntoh(*(ip6_addr *) v->rtvia_addr)); |
452 |
} |
453 |
return IPA_NONE;
|
454 |
} |
455 |
|
456 |
static u32 rta_mpls_stack[MPLS_MAX_LABEL_STACK];
|
457 |
static inline int rta_get_mpls(struct rtattr *a, u32 *stack) |
458 |
{ |
459 |
if (RTA_PAYLOAD(a) % 4) |
460 |
log(L_WARN "KRT: Strange length of received MPLS stack: %u", RTA_PAYLOAD(a));
|
461 |
|
462 |
return mpls_get(RTA_DATA(a), RTA_PAYLOAD(a) & ~0x3, stack); |
463 |
} |
464 |
|
465 |
struct rtattr *
|
466 |
nl_add_attr(struct nlmsghdr *h, uint bufsize, uint code, const void *data, uint dlen) |
467 |
{ |
468 |
uint pos = NLMSG_ALIGN(h->nlmsg_len); |
469 |
uint len = RTA_LENGTH(dlen); |
470 |
|
471 |
if (pos + len > bufsize)
|
472 |
bug("nl_add_attr: packet buffer overflow");
|
473 |
|
474 |
struct rtattr *a = (struct rtattr *)((char *)h + pos); |
475 |
a->rta_type = code; |
476 |
a->rta_len = len; |
477 |
h->nlmsg_len = pos + len; |
478 |
|
479 |
if (dlen > 0) |
480 |
memcpy(RTA_DATA(a), data, dlen); |
481 |
|
482 |
return a;
|
483 |
} |
484 |
|
485 |
static inline struct rtattr * |
486 |
nl_open_attr(struct nlmsghdr *h, uint bufsize, uint code)
|
487 |
{ |
488 |
return nl_add_attr(h, bufsize, code, NULL, 0); |
489 |
} |
490 |
|
491 |
static inline void |
492 |
nl_close_attr(struct nlmsghdr *h, struct rtattr *a) |
493 |
{ |
494 |
a->rta_len = (void *)h + NLMSG_ALIGN(h->nlmsg_len) - (void *)a; |
495 |
} |
496 |
|
497 |
static inline void |
498 |
nl_add_attr_u16(struct nlmsghdr *h, uint bufsize, int code, u16 data) |
499 |
{ |
500 |
nl_add_attr(h, bufsize, code, &data, 2);
|
501 |
} |
502 |
|
503 |
static inline void |
504 |
nl_add_attr_u32(struct nlmsghdr *h, uint bufsize, int code, u32 data) |
505 |
{ |
506 |
nl_add_attr(h, bufsize, code, &data, 4);
|
507 |
} |
508 |
|
509 |
static inline void |
510 |
nl_add_attr_ip4(struct nlmsghdr *h, uint bufsize, int code, ip4_addr ip4) |
511 |
{ |
512 |
ip4 = ip4_hton(ip4); |
513 |
nl_add_attr(h, bufsize, code, &ip4, sizeof(ip4));
|
514 |
} |
515 |
|
516 |
static inline void |
517 |
nl_add_attr_ip6(struct nlmsghdr *h, uint bufsize, int code, ip6_addr ip6) |
518 |
{ |
519 |
ip6 = ip6_hton(ip6); |
520 |
nl_add_attr(h, bufsize, code, &ip6, sizeof(ip6));
|
521 |
} |
522 |
|
523 |
static inline void |
524 |
nl_add_attr_ipa(struct nlmsghdr *h, uint bufsize, int code, ip_addr ipa) |
525 |
{ |
526 |
if (ipa_is_ip4(ipa))
|
527 |
nl_add_attr_ip4(h, bufsize, code, ipa_to_ip4(ipa)); |
528 |
else
|
529 |
nl_add_attr_ip6(h, bufsize, code, ipa_to_ip6(ipa)); |
530 |
} |
531 |
|
532 |
static inline void |
533 |
nl_add_attr_mpls(struct nlmsghdr *h, uint bufsize, int code, int len, u32 *stack) |
534 |
{ |
535 |
char buf[len*4]; |
536 |
mpls_put(buf, len, stack); |
537 |
nl_add_attr(h, bufsize, code, buf, len*4);
|
538 |
} |
539 |
|
540 |
static inline void |
541 |
nl_add_attr_mpls_encap(struct nlmsghdr *h, uint bufsize, int len, u32 *stack) |
542 |
{ |
543 |
nl_add_attr_u16(h, bufsize, RTA_ENCAP_TYPE, LWTUNNEL_ENCAP_MPLS); |
544 |
|
545 |
struct rtattr *nest = nl_open_attr(h, bufsize, RTA_ENCAP);
|
546 |
nl_add_attr_mpls(h, bufsize, RTA_DST, len, stack); |
547 |
nl_close_attr(h, nest); |
548 |
} |
549 |
|
550 |
static inline void |
551 |
nl_add_attr_via(struct nlmsghdr *h, uint bufsize, ip_addr ipa)
|
552 |
{ |
553 |
struct rtattr *nest = nl_open_attr(h, bufsize, RTA_VIA);
|
554 |
struct rtvia *via = RTA_DATA(nest);
|
555 |
|
556 |
h->nlmsg_len += sizeof(*via);
|
557 |
|
558 |
if (ipa_is_ip4(ipa))
|
559 |
{ |
560 |
via->rtvia_family = AF_INET; |
561 |
put_ip4(via->rtvia_addr, ipa_to_ip4(ipa)); |
562 |
h->nlmsg_len += sizeof(ip4_addr);
|
563 |
} |
564 |
else
|
565 |
{ |
566 |
via->rtvia_family = AF_INET6; |
567 |
put_ip6(via->rtvia_addr, ipa_to_ip6(ipa)); |
568 |
h->nlmsg_len += sizeof(ip6_addr);
|
569 |
} |
570 |
|
571 |
nl_close_attr(h, nest); |
572 |
} |
573 |
|
574 |
static inline struct rtnexthop * |
575 |
nl_open_nexthop(struct nlmsghdr *h, uint bufsize)
|
576 |
{ |
577 |
uint pos = NLMSG_ALIGN(h->nlmsg_len); |
578 |
uint len = RTNH_LENGTH(0);
|
579 |
|
580 |
if (pos + len > bufsize)
|
581 |
bug("nl_open_nexthop: packet buffer overflow");
|
582 |
|
583 |
h->nlmsg_len = pos + len; |
584 |
|
585 |
return (void *)h + pos; |
586 |
} |
587 |
|
588 |
static inline void |
589 |
nl_close_nexthop(struct nlmsghdr *h, struct rtnexthop *nh) |
590 |
{ |
591 |
nh->rtnh_len = (void *)h + NLMSG_ALIGN(h->nlmsg_len) - (void *)nh; |
592 |
} |
593 |
|
594 |
static inline void |
595 |
nl_add_nexthop(struct nlmsghdr *h, uint bufsize, struct nexthop *nh, int af) |
596 |
{ |
597 |
if (nh->labels > 0) |
598 |
if (af == AF_MPLS)
|
599 |
nl_add_attr_mpls(h, bufsize, RTA_NEWDST, nh->labels, nh->label); |
600 |
else
|
601 |
nl_add_attr_mpls_encap(h, bufsize, nh->labels, nh->label); |
602 |
|
603 |
if (ipa_nonzero(nh->gw))
|
604 |
if (af == AF_MPLS)
|
605 |
nl_add_attr_via(h, bufsize, nh->gw); |
606 |
else
|
607 |
nl_add_attr_ipa(h, bufsize, RTA_GATEWAY, nh->gw); |
608 |
} |
609 |
|
610 |
static void |
611 |
nl_add_multipath(struct nlmsghdr *h, uint bufsize, struct nexthop *nh, int af) |
612 |
{ |
613 |
struct rtattr *a = nl_open_attr(h, bufsize, RTA_MULTIPATH);
|
614 |
|
615 |
for (; nh; nh = nh->next)
|
616 |
{ |
617 |
struct rtnexthop *rtnh = nl_open_nexthop(h, bufsize);
|
618 |
|
619 |
rtnh->rtnh_flags = 0;
|
620 |
rtnh->rtnh_hops = nh->weight; |
621 |
rtnh->rtnh_ifindex = nh->iface->index; |
622 |
|
623 |
nl_add_nexthop(h, bufsize, nh, af); |
624 |
|
625 |
if (nh->flags & RNF_ONLINK)
|
626 |
rtnh->rtnh_flags |= RTNH_F_ONLINK; |
627 |
|
628 |
nl_close_nexthop(h, rtnh); |
629 |
} |
630 |
|
631 |
nl_close_attr(h, a); |
632 |
} |
633 |
|
634 |
static struct nexthop * |
635 |
nl_parse_multipath(struct krt_proto *p, struct rtattr *ra) |
636 |
{ |
637 |
/* Temporary buffer for multicast nexthops */
|
638 |
static struct nexthop *nh_buffer; |
639 |
static int nh_buf_size; /* in number of structures */ |
640 |
static int nh_buf_used; |
641 |
|
642 |
struct rtattr *a[BIRD_RTA_MAX];
|
643 |
struct rtnexthop *nh = RTA_DATA(ra);
|
644 |
struct nexthop *rv, *first, **last;
|
645 |
unsigned len = RTA_PAYLOAD(ra);
|
646 |
|
647 |
first = NULL;
|
648 |
last = &first; |
649 |
nh_buf_used = 0;
|
650 |
|
651 |
while (len)
|
652 |
{ |
653 |
/* Use RTNH_OK(nh,len) ?? */
|
654 |
if ((len < sizeof(*nh)) || (len < nh->rtnh_len)) |
655 |
return NULL; |
656 |
|
657 |
if (nh_buf_used == nh_buf_size)
|
658 |
{ |
659 |
nh_buf_size = nh_buf_size ? (nh_buf_size * 2) : 4; |
660 |
nh_buffer = xrealloc(nh_buffer, nh_buf_size * NEXTHOP_MAX_SIZE); |
661 |
} |
662 |
*last = rv = nh_buffer + nh_buf_used++; |
663 |
rv->next = NULL;
|
664 |
last = &(rv->next); |
665 |
|
666 |
rv->flags = 0;
|
667 |
rv->weight = nh->rtnh_hops; |
668 |
rv->iface = if_find_by_index(nh->rtnh_ifindex); |
669 |
if (!rv->iface)
|
670 |
return NULL; |
671 |
|
672 |
/* Nonexistent RTNH_PAYLOAD ?? */
|
673 |
nl_attr_len = nh->rtnh_len - RTNH_LENGTH(0);
|
674 |
nl_parse_attrs(RTNH_DATA(nh), nexthop_attr_want4, a, sizeof(a));
|
675 |
if (a[RTA_GATEWAY])
|
676 |
{ |
677 |
rv->gw = rta_get_ipa(a[RTA_GATEWAY]); |
678 |
|
679 |
if (nh->rtnh_flags & RTNH_F_ONLINK)
|
680 |
rv->flags |= RNF_ONLINK; |
681 |
|
682 |
neighbor *nbr; |
683 |
nbr = neigh_find2(&p->p, &rv->gw, rv->iface, |
684 |
(rv->flags & RNF_ONLINK) ? NEF_ONLINK : 0);
|
685 |
if (!nbr || (nbr->scope == SCOPE_HOST))
|
686 |
return NULL; |
687 |
} |
688 |
else
|
689 |
rv->gw = IPA_NONE; |
690 |
|
691 |
if (a[RTA_ENCAP_TYPE])
|
692 |
{ |
693 |
if (rta_get_u16(a[RTA_ENCAP_TYPE]) != LWTUNNEL_ENCAP_MPLS) {
|
694 |
log(L_WARN "KRT: Unknown encapsulation method %d in multipath", rta_get_u16(a[RTA_ENCAP_TYPE]));
|
695 |
return NULL; |
696 |
} |
697 |
|
698 |
struct rtattr *enca[BIRD_RTA_MAX];
|
699 |
nl_attr_len = RTA_PAYLOAD(a[RTA_ENCAP]); |
700 |
nl_parse_attrs(RTA_DATA(a[RTA_ENCAP]), encap_mpls_want, enca, sizeof(enca));
|
701 |
rv->labels = rta_get_mpls(enca[RTA_DST], rv->label); |
702 |
break;
|
703 |
} |
704 |
|
705 |
|
706 |
len -= NLMSG_ALIGN(nh->rtnh_len); |
707 |
nh = RTNH_NEXT(nh); |
708 |
} |
709 |
|
710 |
return first;
|
711 |
} |
712 |
|
713 |
static void |
714 |
nl_add_metrics(struct nlmsghdr *h, uint bufsize, u32 *metrics, int max) |
715 |
{ |
716 |
struct rtattr *a = nl_open_attr(h, bufsize, RTA_METRICS);
|
717 |
int t;
|
718 |
|
719 |
for (t = 1; t < max; t++) |
720 |
if (metrics[0] & (1 << t)) |
721 |
nl_add_attr_u32(h, bufsize, t, metrics[t]); |
722 |
|
723 |
nl_close_attr(h, a); |
724 |
} |
725 |
|
726 |
static int |
727 |
nl_parse_metrics(struct rtattr *hdr, u32 *metrics, int max) |
728 |
{ |
729 |
struct rtattr *a = RTA_DATA(hdr);
|
730 |
int len = RTA_PAYLOAD(hdr);
|
731 |
|
732 |
metrics[0] = 0; |
733 |
for (; RTA_OK(a, len); a = RTA_NEXT(a, len))
|
734 |
{ |
735 |
if (a->rta_type == RTA_UNSPEC)
|
736 |
continue;
|
737 |
|
738 |
if (a->rta_type >= max)
|
739 |
continue;
|
740 |
|
741 |
if (RTA_PAYLOAD(a) != 4) |
742 |
return -1; |
743 |
|
744 |
metrics[0] |= 1 << a->rta_type; |
745 |
metrics[a->rta_type] = rta_get_u32(a); |
746 |
} |
747 |
|
748 |
if (len > 0) |
749 |
return -1; |
750 |
|
751 |
return 0; |
752 |
} |
753 |
|
754 |
|
755 |
/*
|
756 |
* Scanning of interfaces
|
757 |
*/
|
758 |
|
759 |
static void |
760 |
nl_parse_link(struct nlmsghdr *h, int scan) |
761 |
{ |
762 |
struct ifinfomsg *i;
|
763 |
struct rtattr *a[BIRD_IFLA_MAX];
|
764 |
int new = h->nlmsg_type == RTM_NEWLINK;
|
765 |
struct iface f = {};
|
766 |
struct iface *ifi;
|
767 |
char *name;
|
768 |
u32 mtu; |
769 |
uint fl; |
770 |
|
771 |
if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(IFLA_RTA(i), ifla_attr_want, a, sizeof(a))) |
772 |
return;
|
773 |
if (!a[IFLA_IFNAME] || (RTA_PAYLOAD(a[IFLA_IFNAME]) < 2) || !a[IFLA_MTU]) |
774 |
{ |
775 |
/*
|
776 |
* IFLA_IFNAME and IFLA_MTU are required, in fact, but there may also come
|
777 |
* a message with IFLA_WIRELESS set, where (e.g.) no IFLA_IFNAME exists.
|
778 |
* We simply ignore all such messages with IFLA_WIRELESS without notice.
|
779 |
*/
|
780 |
|
781 |
if (a[IFLA_WIRELESS])
|
782 |
return;
|
783 |
|
784 |
log(L_ERR "KIF: Malformed message received");
|
785 |
return;
|
786 |
} |
787 |
|
788 |
name = RTA_DATA(a[IFLA_IFNAME]); |
789 |
mtu = rta_get_u32(a[IFLA_MTU]); |
790 |
|
791 |
ifi = if_find_by_index(i->ifi_index); |
792 |
if (!new)
|
793 |
{ |
794 |
DBG("KIF: IF%d(%s) goes down\n", i->ifi_index, name);
|
795 |
if (!ifi)
|
796 |
return;
|
797 |
|
798 |
if_delete(ifi); |
799 |
} |
800 |
else
|
801 |
{ |
802 |
DBG("KIF: IF%d(%s) goes up (mtu=%d,flg=%x)\n", i->ifi_index, name, mtu, i->ifi_flags);
|
803 |
if (ifi && strncmp(ifi->name, name, sizeof(ifi->name)-1)) |
804 |
if_delete(ifi); |
805 |
|
806 |
strncpy(f.name, name, sizeof(f.name)-1); |
807 |
f.index = i->ifi_index; |
808 |
f.mtu = mtu; |
809 |
|
810 |
fl = i->ifi_flags; |
811 |
if (fl & IFF_UP)
|
812 |
f.flags |= IF_ADMIN_UP; |
813 |
if (fl & IFF_LOWER_UP)
|
814 |
f.flags |= IF_LINK_UP; |
815 |
if (fl & IFF_LOOPBACK) /* Loopback */ |
816 |
f.flags |= IF_MULTIACCESS | IF_LOOPBACK | IF_IGNORE; |
817 |
else if (fl & IFF_POINTOPOINT) /* PtP */ |
818 |
f.flags |= IF_MULTICAST; |
819 |
else if (fl & IFF_BROADCAST) /* Broadcast */ |
820 |
f.flags |= IF_MULTIACCESS | IF_BROADCAST | IF_MULTICAST; |
821 |
else
|
822 |
f.flags |= IF_MULTIACCESS; /* NBMA */
|
823 |
|
824 |
if (fl & IFF_MULTICAST)
|
825 |
f.flags |= IF_MULTICAST; |
826 |
|
827 |
ifi = if_update(&f); |
828 |
|
829 |
if (!scan)
|
830 |
if_end_partial_update(ifi); |
831 |
} |
832 |
} |
833 |
|
834 |
static void |
835 |
nl_parse_addr4(struct ifaddrmsg *i, int scan, int new) |
836 |
{ |
837 |
struct rtattr *a[BIRD_IFA_MAX];
|
838 |
struct iface *ifi;
|
839 |
u32 ifa_flags; |
840 |
int scope;
|
841 |
|
842 |
if (!nl_parse_attrs(IFA_RTA(i), ifa_attr_want4, a, sizeof(a))) |
843 |
return;
|
844 |
|
845 |
if (!a[IFA_LOCAL])
|
846 |
{ |
847 |
log(L_ERR "KIF: Malformed message received (missing IFA_LOCAL)");
|
848 |
return;
|
849 |
} |
850 |
if (!a[IFA_ADDRESS])
|
851 |
{ |
852 |
log(L_ERR "KIF: Malformed message received (missing IFA_ADDRESS)");
|
853 |
return;
|
854 |
} |
855 |
|
856 |
ifi = if_find_by_index(i->ifa_index); |
857 |
if (!ifi)
|
858 |
{ |
859 |
log(L_ERR "KIF: Received address message for unknown interface %d", i->ifa_index);
|
860 |
return;
|
861 |
} |
862 |
|
863 |
if (a[IFA_FLAGS])
|
864 |
ifa_flags = rta_get_u32(a[IFA_FLAGS]); |
865 |
else
|
866 |
ifa_flags = i->ifa_flags; |
867 |
|
868 |
struct ifa ifa;
|
869 |
bzero(&ifa, sizeof(ifa));
|
870 |
ifa.iface = ifi; |
871 |
if (ifa_flags & IFA_F_SECONDARY)
|
872 |
ifa.flags |= IA_SECONDARY; |
873 |
|
874 |
ifa.ip = rta_get_ipa(a[IFA_LOCAL]); |
875 |
|
876 |
if (i->ifa_prefixlen > IP4_MAX_PREFIX_LENGTH)
|
877 |
{ |
878 |
log(L_ERR "KIF: Invalid prefix length for interface %s: %d", ifi->name, i->ifa_prefixlen);
|
879 |
new = 0;
|
880 |
} |
881 |
if (i->ifa_prefixlen == IP4_MAX_PREFIX_LENGTH)
|
882 |
{ |
883 |
ifa.brd = rta_get_ipa(a[IFA_ADDRESS]); |
884 |
net_fill_ip4(&ifa.prefix, rta_get_ip4(a[IFA_ADDRESS]), i->ifa_prefixlen); |
885 |
|
886 |
/* It is either a host address or a peer address */
|
887 |
if (ipa_equal(ifa.ip, ifa.brd))
|
888 |
ifa.flags |= IA_HOST; |
889 |
else
|
890 |
{ |
891 |
ifa.flags |= IA_PEER; |
892 |
ifa.opposite = ifa.brd; |
893 |
} |
894 |
} |
895 |
else
|
896 |
{ |
897 |
net_fill_ip4(&ifa.prefix, ipa_to_ip4(ifa.ip), i->ifa_prefixlen); |
898 |
net_normalize(&ifa.prefix); |
899 |
|
900 |
if (i->ifa_prefixlen == IP4_MAX_PREFIX_LENGTH - 1) |
901 |
ifa.opposite = ipa_opposite_m1(ifa.ip); |
902 |
|
903 |
if (i->ifa_prefixlen == IP4_MAX_PREFIX_LENGTH - 2) |
904 |
ifa.opposite = ipa_opposite_m2(ifa.ip); |
905 |
|
906 |
if ((ifi->flags & IF_BROADCAST) && a[IFA_BROADCAST])
|
907 |
{ |
908 |
ip4_addr xbrd = rta_get_ip4(a[IFA_BROADCAST]); |
909 |
ip4_addr ybrd = ip4_or(ipa_to_ip4(ifa.ip), ip4_not(ip4_mkmask(i->ifa_prefixlen))); |
910 |
|
911 |
if (ip4_equal(xbrd, net4_prefix(&ifa.prefix)) || ip4_equal(xbrd, ybrd))
|
912 |
ifa.brd = ipa_from_ip4(xbrd); |
913 |
else if (ifi->flags & IF_TMP_DOWN) /* Complain only during the first scan */ |
914 |
{ |
915 |
log(L_ERR "KIF: Invalid broadcast address %I4 for %s", xbrd, ifi->name);
|
916 |
ifa.brd = ipa_from_ip4(ybrd); |
917 |
} |
918 |
} |
919 |
} |
920 |
|
921 |
scope = ipa_classify(ifa.ip); |
922 |
if (scope < 0) |
923 |
{ |
924 |
log(L_ERR "KIF: Invalid interface address %I for %s", ifa.ip, ifi->name);
|
925 |
return;
|
926 |
} |
927 |
ifa.scope = scope & IADDR_SCOPE_MASK; |
928 |
|
929 |
DBG("KIF: IF%d(%s): %s IPA %I, flg %x, net %N, brd %I, opp %I\n",
|
930 |
ifi->index, ifi->name, |
931 |
new ? "added" : "removed", |
932 |
ifa.ip, ifa.flags, ifa.prefix, ifa.brd, ifa.opposite); |
933 |
|
934 |
if (new)
|
935 |
ifa_update(&ifa); |
936 |
else
|
937 |
ifa_delete(&ifa); |
938 |
|
939 |
if (!scan)
|
940 |
if_end_partial_update(ifi); |
941 |
} |
942 |
|
943 |
static void |
944 |
nl_parse_addr6(struct ifaddrmsg *i, int scan, int new) |
945 |
{ |
946 |
struct rtattr *a[BIRD_IFA_MAX];
|
947 |
struct iface *ifi;
|
948 |
u32 ifa_flags; |
949 |
int scope;
|
950 |
|
951 |
if (!nl_parse_attrs(IFA_RTA(i), ifa_attr_want6, a, sizeof(a))) |
952 |
return;
|
953 |
|
954 |
if (!a[IFA_ADDRESS])
|
955 |
{ |
956 |
log(L_ERR "KIF: Malformed message received (missing IFA_ADDRESS)");
|
957 |
return;
|
958 |
} |
959 |
|
960 |
ifi = if_find_by_index(i->ifa_index); |
961 |
if (!ifi)
|
962 |
{ |
963 |
log(L_ERR "KIF: Received address message for unknown interface %d", i->ifa_index);
|
964 |
return;
|
965 |
} |
966 |
|
967 |
if (a[IFA_FLAGS])
|
968 |
ifa_flags = rta_get_u32(a[IFA_FLAGS]); |
969 |
else
|
970 |
ifa_flags = i->ifa_flags; |
971 |
|
972 |
struct ifa ifa;
|
973 |
bzero(&ifa, sizeof(ifa));
|
974 |
ifa.iface = ifi; |
975 |
if (ifa_flags & IFA_F_SECONDARY)
|
976 |
ifa.flags |= IA_SECONDARY; |
977 |
|
978 |
/* Ignore tentative addresses silently */
|
979 |
if (ifa_flags & IFA_F_TENTATIVE)
|
980 |
return;
|
981 |
|
982 |
/* IFA_LOCAL can be unset for IPv6 interfaces */
|
983 |
ifa.ip = rta_get_ipa(a[IFA_LOCAL] ? : a[IFA_ADDRESS]); |
984 |
|
985 |
if (i->ifa_prefixlen > IP6_MAX_PREFIX_LENGTH)
|
986 |
{ |
987 |
log(L_ERR "KIF: Invalid prefix length for interface %s: %d", ifi->name, i->ifa_prefixlen);
|
988 |
new = 0;
|
989 |
} |
990 |
if (i->ifa_prefixlen == IP6_MAX_PREFIX_LENGTH)
|
991 |
{ |
992 |
ifa.brd = rta_get_ipa(a[IFA_ADDRESS]); |
993 |
net_fill_ip6(&ifa.prefix, rta_get_ip6(a[IFA_ADDRESS]), i->ifa_prefixlen); |
994 |
|
995 |
/* It is either a host address or a peer address */
|
996 |
if (ipa_equal(ifa.ip, ifa.brd))
|
997 |
ifa.flags |= IA_HOST; |
998 |
else
|
999 |
{ |
1000 |
ifa.flags |= IA_PEER; |
1001 |
ifa.opposite = ifa.brd; |
1002 |
} |
1003 |
} |
1004 |
else
|
1005 |
{ |
1006 |
net_fill_ip6(&ifa.prefix, ipa_to_ip6(ifa.ip), i->ifa_prefixlen); |
1007 |
net_normalize(&ifa.prefix); |
1008 |
|
1009 |
if (i->ifa_prefixlen == IP6_MAX_PREFIX_LENGTH - 1) |
1010 |
ifa.opposite = ipa_opposite_m1(ifa.ip); |
1011 |
} |
1012 |
|
1013 |
scope = ipa_classify(ifa.ip); |
1014 |
if (scope < 0) |
1015 |
{ |
1016 |
log(L_ERR "KIF: Invalid interface address %I for %s", ifa.ip, ifi->name);
|
1017 |
return;
|
1018 |
} |
1019 |
ifa.scope = scope & IADDR_SCOPE_MASK; |
1020 |
|
1021 |
DBG("KIF: IF%d(%s): %s IPA %I, flg %x, net %N, brd %I, opp %I\n",
|
1022 |
ifi->index, ifi->name, |
1023 |
new ? "added" : "removed", |
1024 |
ifa.ip, ifa.flags, ifa.prefix, ifa.brd, ifa.opposite); |
1025 |
|
1026 |
if (new)
|
1027 |
ifa_update(&ifa); |
1028 |
else
|
1029 |
ifa_delete(&ifa); |
1030 |
|
1031 |
if (!scan)
|
1032 |
if_end_partial_update(ifi); |
1033 |
} |
1034 |
|
1035 |
static void |
1036 |
nl_parse_addr(struct nlmsghdr *h, int scan) |
1037 |
{ |
1038 |
struct ifaddrmsg *i;
|
1039 |
|
1040 |
if (!(i = nl_checkin(h, sizeof(*i)))) |
1041 |
return;
|
1042 |
|
1043 |
int new = (h->nlmsg_type == RTM_NEWADDR);
|
1044 |
|
1045 |
switch (i->ifa_family)
|
1046 |
{ |
1047 |
case AF_INET:
|
1048 |
return nl_parse_addr4(i, scan, new);
|
1049 |
|
1050 |
case AF_INET6:
|
1051 |
return nl_parse_addr6(i, scan, new);
|
1052 |
} |
1053 |
} |
1054 |
|
1055 |
void
|
1056 |
kif_do_scan(struct kif_proto *p UNUSED)
|
1057 |
{ |
1058 |
struct nlmsghdr *h;
|
1059 |
|
1060 |
if_start_update(); |
1061 |
|
1062 |
nl_request_dump(AF_UNSPEC, RTM_GETLINK); |
1063 |
while (h = nl_get_scan())
|
1064 |
if (h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)
|
1065 |
nl_parse_link(h, 1);
|
1066 |
else
|
1067 |
log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type);
|
1068 |
|
1069 |
nl_request_dump(AF_INET, RTM_GETADDR); |
1070 |
while (h = nl_get_scan())
|
1071 |
if (h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR)
|
1072 |
nl_parse_addr(h, 1);
|
1073 |
else
|
1074 |
log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type);
|
1075 |
|
1076 |
nl_request_dump(AF_INET6, RTM_GETADDR); |
1077 |
while (h = nl_get_scan())
|
1078 |
if (h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR)
|
1079 |
nl_parse_addr(h, 1);
|
1080 |
else
|
1081 |
log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type);
|
1082 |
|
1083 |
if_end_update(); |
1084 |
} |
1085 |
|
1086 |
/*
|
1087 |
* Routes
|
1088 |
*/
|
1089 |
|
1090 |
static inline u32 |
1091 |
krt_table_id(struct krt_proto *p)
|
1092 |
{ |
1093 |
return KRT_CF->sys.table_id;
|
1094 |
} |
1095 |
|
1096 |
static HASH(struct krt_proto) nl_table_map; |
1097 |
|
1098 |
#define RTH_KEY(p) p->af, krt_table_id(p)
|
1099 |
#define RTH_NEXT(p) p->sys.hash_next
|
1100 |
#define RTH_EQ(a1,i1,a2,i2) a1 == a2 && i1 == i2
|
1101 |
#define RTH_FN(a,i) a ^ u32_hash(i)
|
1102 |
|
1103 |
#define RTH_REHASH rth_rehash
|
1104 |
#define RTH_PARAMS /8, *2, 2, 2, 6, 20 |
1105 |
|
1106 |
HASH_DEFINE_REHASH_FN(RTH, struct krt_proto)
|
1107 |
|
1108 |
int
|
1109 |
krt_capable(rte *e) |
1110 |
{ |
1111 |
rta *a = e->attrs; |
1112 |
|
1113 |
switch (a->dest)
|
1114 |
{ |
1115 |
case RTD_UNICAST:
|
1116 |
case RTD_BLACKHOLE:
|
1117 |
case RTD_UNREACHABLE:
|
1118 |
case RTD_PROHIBIT:
|
1119 |
return 1; |
1120 |
|
1121 |
default:
|
1122 |
return 0; |
1123 |
} |
1124 |
} |
1125 |
|
1126 |
static inline int |
1127 |
nh_bufsize(struct nexthop *nh)
|
1128 |
{ |
1129 |
int rv = 0; |
1130 |
for (; nh != NULL; nh = nh->next) |
1131 |
rv += RTNH_LENGTH(RTA_LENGTH(sizeof(ip_addr)));
|
1132 |
return rv;
|
1133 |
} |
1134 |
|
1135 |
static int |
1136 |
nl_send_route(struct krt_proto *p, rte *e, struct ea_list *eattrs, int op, int dest, struct nexthop *nh) |
1137 |
{ |
1138 |
eattr *ea; |
1139 |
net *net = e->net; |
1140 |
rta *a = e->attrs; |
1141 |
int bufsize = 128 + KRT_METRICS_MAX*8 + nh_bufsize(&(a->nh)); |
1142 |
u32 priority = 0;
|
1143 |
|
1144 |
struct {
|
1145 |
struct nlmsghdr h;
|
1146 |
struct rtmsg r;
|
1147 |
char buf[0]; |
1148 |
} *r; |
1149 |
|
1150 |
int rsize = sizeof(*r) + bufsize; |
1151 |
r = alloca(rsize); |
1152 |
|
1153 |
DBG("nl_send_route(%N,op=%x)\n", net->n.addr, op);
|
1154 |
|
1155 |
bzero(&r->h, sizeof(r->h));
|
1156 |
bzero(&r->r, sizeof(r->r));
|
1157 |
r->h.nlmsg_type = op ? RTM_NEWROUTE : RTM_DELROUTE; |
1158 |
r->h.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
1159 |
r->h.nlmsg_flags = op | NLM_F_REQUEST | NLM_F_ACK; |
1160 |
|
1161 |
r->r.rtm_family = p->af; |
1162 |
r->r.rtm_dst_len = net_pxlen(net->n.addr); |
1163 |
r->r.rtm_protocol = RTPROT_BIRD; |
1164 |
r->r.rtm_scope = RT_SCOPE_NOWHERE; |
1165 |
if (p->af == AF_MPLS)
|
1166 |
{ |
1167 |
u32 label = net_mpls(net->n.addr); |
1168 |
nl_add_attr_mpls(&r->h, rsize, RTA_DST, 1, &label);
|
1169 |
} |
1170 |
else
|
1171 |
nl_add_attr_ipa(&r->h, rsize, RTA_DST, net_prefix(net->n.addr)); |
1172 |
|
1173 |
/*
|
1174 |
* Strange behavior for RTM_DELROUTE:
|
1175 |
* 1) rtm_family is ignored in IPv6, works for IPv4
|
1176 |
* 2) not setting RTA_PRIORITY is different from setting default value (on IPv6)
|
1177 |
* 3) not setting RTA_PRIORITY is equivalent to setting 0, which is wildcard
|
1178 |
*/
|
1179 |
|
1180 |
if (krt_table_id(p) < 256) |
1181 |
r->r.rtm_table = krt_table_id(p); |
1182 |
else
|
1183 |
nl_add_attr_u32(&r->h, rsize, RTA_TABLE, krt_table_id(p)); |
1184 |
|
1185 |
if (a->source == RTS_DUMMY)
|
1186 |
priority = e->u.krt.metric; |
1187 |
else if (KRT_CF->sys.metric) |
1188 |
priority = KRT_CF->sys.metric; |
1189 |
else if ((op != NL_OP_DELETE) && (ea = ea_find(eattrs, EA_KRT_METRIC))) |
1190 |
priority = ea->u.data; |
1191 |
|
1192 |
if (priority)
|
1193 |
nl_add_attr_u32(&r->h, rsize, RTA_PRIORITY, priority); |
1194 |
|
1195 |
/* For route delete, we do not specify remaining route attributes */
|
1196 |
if (op == NL_OP_DELETE)
|
1197 |
goto dest;
|
1198 |
|
1199 |
/* Default scope is LINK for device routes, UNIVERSE otherwise */
|
1200 |
if (ea = ea_find(eattrs, EA_KRT_SCOPE))
|
1201 |
r->r.rtm_scope = ea->u.data; |
1202 |
else
|
1203 |
r->r.rtm_scope = (dest == RTD_UNICAST && ipa_zero(nh->gw)) ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE; |
1204 |
|
1205 |
if (ea = ea_find(eattrs, EA_KRT_PREFSRC))
|
1206 |
nl_add_attr_ipa(&r->h, rsize, RTA_PREFSRC, *(ip_addr *)ea->u.ptr->data); |
1207 |
|
1208 |
if (ea = ea_find(eattrs, EA_KRT_REALM))
|
1209 |
nl_add_attr_u32(&r->h, rsize, RTA_FLOW, ea->u.data); |
1210 |
|
1211 |
|
1212 |
u32 metrics[KRT_METRICS_MAX]; |
1213 |
metrics[0] = 0; |
1214 |
|
1215 |
struct ea_walk_state ews = { .eattrs = eattrs };
|
1216 |
while (ea = ea_walk(&ews, EA_KRT_METRICS, KRT_METRICS_MAX))
|
1217 |
{ |
1218 |
int id = ea->id - EA_KRT_METRICS;
|
1219 |
metrics[0] |= 1 << id; |
1220 |
metrics[id] = ea->u.data; |
1221 |
} |
1222 |
|
1223 |
if (metrics[0]) |
1224 |
nl_add_metrics(&r->h, rsize, metrics, KRT_METRICS_MAX); |
1225 |
|
1226 |
|
1227 |
dest:
|
1228 |
switch (dest)
|
1229 |
{ |
1230 |
case RTD_UNICAST:
|
1231 |
r->r.rtm_type = RTN_UNICAST; |
1232 |
if (nh->next && !krt_ecmp6(p))
|
1233 |
nl_add_multipath(&r->h, rsize, nh, p->af); |
1234 |
else
|
1235 |
{ |
1236 |
nl_add_attr_u32(&r->h, rsize, RTA_OIF, nh->iface->index); |
1237 |
nl_add_nexthop(&r->h, rsize, nh, p->af); |
1238 |
|
1239 |
if (nh->flags & RNF_ONLINK)
|
1240 |
r->r.rtm_flags |= RTNH_F_ONLINK; |
1241 |
} |
1242 |
break;
|
1243 |
case RTD_BLACKHOLE:
|
1244 |
r->r.rtm_type = RTN_BLACKHOLE; |
1245 |
break;
|
1246 |
case RTD_UNREACHABLE:
|
1247 |
r->r.rtm_type = RTN_UNREACHABLE; |
1248 |
break;
|
1249 |
case RTD_PROHIBIT:
|
1250 |
r->r.rtm_type = RTN_PROHIBIT; |
1251 |
break;
|
1252 |
case RTD_NONE:
|
1253 |
break;
|
1254 |
default:
|
1255 |
bug("krt_capable inconsistent with nl_send_route");
|
1256 |
} |
1257 |
|
1258 |
/* Ignore missing for DELETE */
|
1259 |
return nl_exchange(&r->h, (op == NL_OP_DELETE));
|
1260 |
} |
1261 |
|
1262 |
static inline int |
1263 |
nl_add_rte(struct krt_proto *p, rte *e, struct ea_list *eattrs) |
1264 |
{ |
1265 |
rta *a = e->attrs; |
1266 |
int err = 0; |
1267 |
|
1268 |
if (krt_ecmp6(p) && a->nh.next)
|
1269 |
{ |
1270 |
struct nexthop *nh = &(a->nh);
|
1271 |
|
1272 |
err = nl_send_route(p, e, eattrs, NL_OP_ADD, RTD_UNICAST, nh); |
1273 |
if (err < 0) |
1274 |
return err;
|
1275 |
|
1276 |
for (nh = nh->next; nh; nh = nh->next)
|
1277 |
err += nl_send_route(p, e, eattrs, NL_OP_APPEND, RTD_UNICAST, nh); |
1278 |
|
1279 |
return err;
|
1280 |
} |
1281 |
|
1282 |
return nl_send_route(p, e, eattrs, NL_OP_ADD, a->dest, &(a->nh));
|
1283 |
} |
1284 |
|
1285 |
static inline int |
1286 |
nl_delete_rte(struct krt_proto *p, rte *e, struct ea_list *eattrs) |
1287 |
{ |
1288 |
int err = 0; |
1289 |
|
1290 |
/* For IPv6, we just repeatedly request DELETE until we get error */
|
1291 |
do
|
1292 |
err = nl_send_route(p, e, eattrs, NL_OP_DELETE, RTD_NONE, NULL);
|
1293 |
while (krt_ecmp6(p) && !err);
|
1294 |
|
1295 |
return err;
|
1296 |
} |
1297 |
|
1298 |
void
|
1299 |
krt_replace_rte(struct krt_proto *p, net *n, rte *new, rte *old, struct ea_list *eattrs) |
1300 |
{ |
1301 |
int err = 0; |
1302 |
|
1303 |
/*
|
1304 |
* We could use NL_OP_REPLACE, but route replace on Linux has some problems:
|
1305 |
*
|
1306 |
* 1) Does not check for matching rtm_protocol
|
1307 |
* 2) Has broken semantics for IPv6 ECMP
|
1308 |
* 3) Crashes some kernel version when used for IPv6 ECMP
|
1309 |
*
|
1310 |
* So we use NL_OP_DELETE and then NL_OP_ADD. We also do not trust the old
|
1311 |
* route value, so we do not try to optimize IPv6 ECMP reconfigurations.
|
1312 |
*/
|
1313 |
|
1314 |
if (old)
|
1315 |
nl_delete_rte(p, old, eattrs); |
1316 |
|
1317 |
if (new)
|
1318 |
err = nl_add_rte(p, new, eattrs); |
1319 |
|
1320 |
if (err < 0) |
1321 |
n->n.flags |= KRF_SYNC_ERROR; |
1322 |
else
|
1323 |
n->n.flags &= ~KRF_SYNC_ERROR; |
1324 |
} |
1325 |
|
1326 |
|
1327 |
static inline struct nexthop * |
1328 |
nl_alloc_nexthop(struct nl_parse_state *s, ip_addr gw, struct iface *iface, byte weight) |
1329 |
{ |
1330 |
struct nexthop *nh = lp_alloc(s->pool, sizeof(struct nexthop)); |
1331 |
|
1332 |
nh->gw = gw; |
1333 |
nh->iface = iface; |
1334 |
nh->next = NULL;
|
1335 |
nh->weight = weight; |
1336 |
|
1337 |
return nh;
|
1338 |
} |
1339 |
|
1340 |
static int |
1341 |
nl_mergable_route(struct nl_parse_state *s, net *net, struct krt_proto *p, uint priority, uint krt_type) |
1342 |
{ |
1343 |
/* Route merging must be active */
|
1344 |
if (!s->merge)
|
1345 |
return 0; |
1346 |
|
1347 |
/* Saved and new route must have same network, proto/table, and priority */
|
1348 |
if ((s->net != net) || (s->proto != p) || (s->krt_metric != priority))
|
1349 |
return 0; |
1350 |
|
1351 |
/* Both must be regular unicast routes */
|
1352 |
if ((s->krt_type != RTN_UNICAST) || (krt_type != RTN_UNICAST))
|
1353 |
return 0; |
1354 |
|
1355 |
return 1; |
1356 |
} |
1357 |
|
1358 |
static void |
1359 |
nl_announce_route(struct nl_parse_state *s)
|
1360 |
{ |
1361 |
rte *e = rte_get_temp(s->attrs); |
1362 |
e->net = s->net; |
1363 |
e->u.krt.src = s->krt_src; |
1364 |
e->u.krt.proto = s->krt_proto; |
1365 |
e->u.krt.seen = 0;
|
1366 |
e->u.krt.best = 0;
|
1367 |
e->u.krt.metric = s->krt_metric; |
1368 |
|
1369 |
if (s->scan)
|
1370 |
krt_got_route(s->proto, e); |
1371 |
else
|
1372 |
krt_got_route_async(s->proto, e, s->new); |
1373 |
|
1374 |
s->net = NULL;
|
1375 |
s->attrs = NULL;
|
1376 |
s->proto = NULL;
|
1377 |
lp_flush(s->pool); |
1378 |
} |
1379 |
|
1380 |
static inline void |
1381 |
nl_parse_begin(struct nl_parse_state *s, int scan, int merge) |
1382 |
{ |
1383 |
memset(s, 0, sizeof (struct nl_parse_state)); |
1384 |
s->pool = nl_linpool; |
1385 |
s->scan = scan; |
1386 |
s->merge = merge; |
1387 |
} |
1388 |
|
1389 |
static inline void |
1390 |
nl_parse_end(struct nl_parse_state *s)
|
1391 |
{ |
1392 |
if (s->net)
|
1393 |
nl_announce_route(s); |
1394 |
} |
1395 |
|
1396 |
|
1397 |
#define SKIP(ARG...) do { DBG("KRT: Ignoring route - " ARG); return; } while(0) |
1398 |
|
1399 |
static void |
1400 |
nl_parse_route(struct nl_parse_state *s, struct nlmsghdr *h) |
1401 |
{ |
1402 |
struct krt_proto *p;
|
1403 |
struct rtmsg *i;
|
1404 |
struct rtattr *a[BIRD_RTA_MAX];
|
1405 |
int new = h->nlmsg_type == RTM_NEWROUTE;
|
1406 |
|
1407 |
net_addr dst; |
1408 |
u32 oif = ~0;
|
1409 |
u32 table_id; |
1410 |
u32 priority = 0;
|
1411 |
u32 def_scope = RT_SCOPE_UNIVERSE; |
1412 |
int src;
|
1413 |
|
1414 |
if (!(i = nl_checkin(h, sizeof(*i)))) |
1415 |
return;
|
1416 |
|
1417 |
switch (i->rtm_family)
|
1418 |
{ |
1419 |
case AF_INET:
|
1420 |
if (!nl_parse_attrs(RTM_RTA(i), rtm_attr_want4, a, sizeof(a))) |
1421 |
return;
|
1422 |
|
1423 |
if (a[RTA_DST])
|
1424 |
net_fill_ip4(&dst, rta_get_ip4(a[RTA_DST]), i->rtm_dst_len); |
1425 |
else
|
1426 |
net_fill_ip4(&dst, IP4_NONE, 0);
|
1427 |
break;
|
1428 |
|
1429 |
case AF_INET6:
|
1430 |
if (!nl_parse_attrs(RTM_RTA(i), rtm_attr_want6, a, sizeof(a))) |
1431 |
return;
|
1432 |
|
1433 |
if (a[RTA_DST])
|
1434 |
net_fill_ip6(&dst, rta_get_ip6(a[RTA_DST]), i->rtm_dst_len); |
1435 |
else
|
1436 |
net_fill_ip6(&dst, IP6_NONE, 0);
|
1437 |
break;
|
1438 |
|
1439 |
case AF_MPLS:
|
1440 |
if (!nl_parse_attrs(RTM_RTA(i), rtm_attr_want_mpls, a, sizeof(a))) |
1441 |
return;
|
1442 |
|
1443 |
if (!a[RTA_DST])
|
1444 |
SKIP("MPLS route without RTA_DST");
|
1445 |
|
1446 |
if (rta_get_mpls(a[RTA_DST], rta_mpls_stack) != 1) |
1447 |
SKIP("MPLS route with multi-label RTA_DST");
|
1448 |
|
1449 |
net_fill_mpls(&dst, rta_mpls_stack[0]);
|
1450 |
break;
|
1451 |
|
1452 |
default:
|
1453 |
return;
|
1454 |
} |
1455 |
|
1456 |
if (a[RTA_OIF])
|
1457 |
oif = rta_get_u32(a[RTA_OIF]); |
1458 |
|
1459 |
if (a[RTA_TABLE])
|
1460 |
table_id = rta_get_u32(a[RTA_TABLE]); |
1461 |
else
|
1462 |
table_id = i->rtm_table; |
1463 |
|
1464 |
/* Do we know this table? */
|
1465 |
p = HASH_FIND(nl_table_map, RTH, i->rtm_family, table_id); |
1466 |
if (!p)
|
1467 |
SKIP("unknown table %d\n", table);
|
1468 |
|
1469 |
if (a[RTA_IIF])
|
1470 |
SKIP("IIF set\n");
|
1471 |
|
1472 |
if (i->rtm_tos != 0) /* We don't support TOS */ |
1473 |
SKIP("TOS %02x\n", i->rtm_tos);
|
1474 |
|
1475 |
if (s->scan && !new)
|
1476 |
SKIP("RTM_DELROUTE in scan\n");
|
1477 |
|
1478 |
if (a[RTA_PRIORITY])
|
1479 |
priority = rta_get_u32(a[RTA_PRIORITY]); |
1480 |
|
1481 |
int c = net_classify(&dst);
|
1482 |
if ((c < 0) || !(c & IADDR_HOST) || ((c & IADDR_SCOPE_MASK) <= SCOPE_LINK)) |
1483 |
SKIP("strange class/scope\n");
|
1484 |
|
1485 |
switch (i->rtm_protocol)
|
1486 |
{ |
1487 |
case RTPROT_UNSPEC:
|
1488 |
SKIP("proto unspec\n");
|
1489 |
|
1490 |
case RTPROT_REDIRECT:
|
1491 |
src = KRT_SRC_REDIRECT; |
1492 |
break;
|
1493 |
|
1494 |
case RTPROT_KERNEL:
|
1495 |
src = KRT_SRC_KERNEL; |
1496 |
return;
|
1497 |
|
1498 |
case RTPROT_BIRD:
|
1499 |
if (!s->scan)
|
1500 |
SKIP("echo\n");
|
1501 |
src = KRT_SRC_BIRD; |
1502 |
break;
|
1503 |
|
1504 |
case RTPROT_BOOT:
|
1505 |
default:
|
1506 |
src = KRT_SRC_ALIEN; |
1507 |
} |
1508 |
|
1509 |
net *net = net_get(p->p.main_channel->table, &dst); |
1510 |
|
1511 |
if (s->net && !nl_mergable_route(s, net, p, priority, i->rtm_type))
|
1512 |
nl_announce_route(s); |
1513 |
|
1514 |
rta *ra = lp_allocz(s->pool, RTA_MAX_SIZE); |
1515 |
ra->src = p->p.main_source; |
1516 |
ra->source = RTS_INHERIT; |
1517 |
ra->scope = SCOPE_UNIVERSE; |
1518 |
|
1519 |
switch (i->rtm_type)
|
1520 |
{ |
1521 |
case RTN_UNICAST:
|
1522 |
ra->dest = RTD_UNICAST; |
1523 |
|
1524 |
if (a[RTA_MULTIPATH] && (i->rtm_family == AF_INET))
|
1525 |
{ |
1526 |
struct nexthop *nh = nl_parse_multipath(p, a[RTA_MULTIPATH]);
|
1527 |
if (!nh)
|
1528 |
{ |
1529 |
log(L_ERR "KRT: Received strange multipath route %N", net->n.addr);
|
1530 |
return;
|
1531 |
} |
1532 |
|
1533 |
ra->nh = *nh; |
1534 |
break;
|
1535 |
} |
1536 |
|
1537 |
ra->nh.iface = if_find_by_index(oif); |
1538 |
if (!ra->nh.iface)
|
1539 |
{ |
1540 |
log(L_ERR "KRT: Received route %N with unknown ifindex %u", net->n.addr, oif);
|
1541 |
return;
|
1542 |
} |
1543 |
|
1544 |
if ((i->rtm_family != AF_MPLS) && a[RTA_GATEWAY] || (i->rtm_family == AF_MPLS) && a[RTA_VIA])
|
1545 |
{ |
1546 |
if (i->rtm_family == AF_MPLS)
|
1547 |
ra->nh.gw = rta_get_via(a[RTA_VIA]); |
1548 |
else
|
1549 |
ra->nh.gw = rta_get_ipa(a[RTA_GATEWAY]); |
1550 |
|
1551 |
/* Silently skip strange 6to4 routes */
|
1552 |
const net_addr_ip6 sit = NET_ADDR_IP6(IP6_NONE, 96); |
1553 |
if ((i->rtm_family == AF_INET6) && ipa_in_netX(ra->nh.gw, (net_addr *) &sit))
|
1554 |
return;
|
1555 |
|
1556 |
if (i->rtm_flags & RTNH_F_ONLINK)
|
1557 |
ra->nh.flags |= RNF_ONLINK; |
1558 |
|
1559 |
neighbor *nbr; |
1560 |
nbr = neigh_find2(&p->p, &(ra->nh.gw), ra->nh.iface, |
1561 |
(ra->nh.flags & RNF_ONLINK) ? NEF_ONLINK : 0);
|
1562 |
if (!nbr || (nbr->scope == SCOPE_HOST))
|
1563 |
{ |
1564 |
log(L_ERR "KRT: Received route %N with strange next-hop %I", net->n.addr,
|
1565 |
ra->nh.gw); |
1566 |
return;
|
1567 |
} |
1568 |
} |
1569 |
|
1570 |
break;
|
1571 |
case RTN_BLACKHOLE:
|
1572 |
ra->dest = RTD_BLACKHOLE; |
1573 |
break;
|
1574 |
case RTN_UNREACHABLE:
|
1575 |
ra->dest = RTD_UNREACHABLE; |
1576 |
break;
|
1577 |
case RTN_PROHIBIT:
|
1578 |
ra->dest = RTD_PROHIBIT; |
1579 |
break;
|
1580 |
/* FIXME: What about RTN_THROW? */
|
1581 |
default:
|
1582 |
SKIP("type %d\n", i->rtm_type);
|
1583 |
return;
|
1584 |
} |
1585 |
|
1586 |
int labels = 0; |
1587 |
if ((i->rtm_family == AF_MPLS) && a[RTA_NEWDST] && !ra->nh.next)
|
1588 |
labels = rta_get_mpls(a[RTA_NEWDST], ra->nh.label); |
1589 |
|
1590 |
if (a[RTA_ENCAP] && a[RTA_ENCAP_TYPE] && !ra->nh.next)
|
1591 |
{ |
1592 |
switch (rta_get_u16(a[RTA_ENCAP_TYPE]))
|
1593 |
{ |
1594 |
case LWTUNNEL_ENCAP_MPLS:
|
1595 |
{ |
1596 |
struct rtattr *enca[BIRD_RTA_MAX];
|
1597 |
nl_attr_len = RTA_PAYLOAD(a[RTA_ENCAP]); |
1598 |
nl_parse_attrs(RTA_DATA(a[RTA_ENCAP]), encap_mpls_want, enca, sizeof(enca));
|
1599 |
labels = rta_get_mpls(enca[RTA_DST], ra->nh.label); |
1600 |
break;
|
1601 |
} |
1602 |
default:
|
1603 |
SKIP("unknown encapsulation method %d\n", rta_get_u16(a[RTA_ENCAP_TYPE]));
|
1604 |
break;
|
1605 |
} |
1606 |
} |
1607 |
|
1608 |
if (labels < 0) |
1609 |
{ |
1610 |
log(L_WARN "KRT: Too long MPLS stack received, ignoring.");
|
1611 |
ra->nh.labels = 0;
|
1612 |
} |
1613 |
else
|
1614 |
ra->nh.labels = labels; |
1615 |
|
1616 |
rte *e = rte_get_temp(ra); |
1617 |
e->net = net; |
1618 |
e->u.krt.src = src; |
1619 |
e->u.krt.proto = i->rtm_protocol; |
1620 |
e->u.krt.seen = 0;
|
1621 |
e->u.krt.best = 0;
|
1622 |
e->u.krt.metric = 0;
|
1623 |
|
1624 |
if (i->rtm_scope != def_scope)
|
1625 |
{ |
1626 |
ea_list *ea = lp_alloc(s->pool, sizeof(ea_list) + sizeof(eattr)); |
1627 |
ea->next = ra->eattrs; |
1628 |
ra->eattrs = ea; |
1629 |
ea->flags = EALF_SORTED; |
1630 |
ea->count = 1;
|
1631 |
ea->attrs[0].id = EA_KRT_SCOPE;
|
1632 |
ea->attrs[0].flags = 0; |
1633 |
ea->attrs[0].type = EAF_TYPE_INT;
|
1634 |
ea->attrs[0].u.data = i->rtm_scope;
|
1635 |
} |
1636 |
|
1637 |
if (a[RTA_PRIORITY])
|
1638 |
e->u.krt.metric = rta_get_u32(a[RTA_PRIORITY]); |
1639 |
|
1640 |
if (a[RTA_PREFSRC])
|
1641 |
{ |
1642 |
ip_addr ps = rta_get_ipa(a[RTA_PREFSRC]); |
1643 |
|
1644 |
ea_list *ea = lp_alloc(s->pool, sizeof(ea_list) + sizeof(eattr)); |
1645 |
ea->next = ra->eattrs; |
1646 |
ra->eattrs = ea; |
1647 |
ea->flags = EALF_SORTED; |
1648 |
ea->count = 1;
|
1649 |
ea->attrs[0].id = EA_KRT_PREFSRC;
|
1650 |
ea->attrs[0].flags = 0; |
1651 |
ea->attrs[0].type = EAF_TYPE_IP_ADDRESS;
|
1652 |
ea->attrs[0].u.ptr = lp_alloc(s->pool, sizeof(struct adata) + sizeof(ps)); |
1653 |
ea->attrs[0].u.ptr->length = sizeof(ps); |
1654 |
memcpy(ea->attrs[0].u.ptr->data, &ps, sizeof(ps)); |
1655 |
} |
1656 |
|
1657 |
if (a[RTA_FLOW])
|
1658 |
{ |
1659 |
ea_list *ea = lp_alloc(s->pool, sizeof(ea_list) + sizeof(eattr)); |
1660 |
ea->next = ra->eattrs; |
1661 |
ra->eattrs = ea; |
1662 |
ea->flags = EALF_SORTED; |
1663 |
ea->count = 1;
|
1664 |
ea->attrs[0].id = EA_KRT_REALM;
|
1665 |
ea->attrs[0].flags = 0; |
1666 |
ea->attrs[0].type = EAF_TYPE_INT;
|
1667 |
ea->attrs[0].u.data = rta_get_u32(a[RTA_FLOW]);
|
1668 |
} |
1669 |
|
1670 |
if (a[RTA_METRICS])
|
1671 |
{ |
1672 |
u32 metrics[KRT_METRICS_MAX]; |
1673 |
ea_list *ea = lp_alloc(s->pool, sizeof(ea_list) + KRT_METRICS_MAX * sizeof(eattr)); |
1674 |
int t, n = 0; |
1675 |
|
1676 |
if (nl_parse_metrics(a[RTA_METRICS], metrics, ARRAY_SIZE(metrics)) < 0) |
1677 |
{ |
1678 |
log(L_ERR "KRT: Received route %N with strange RTA_METRICS attribute", net->n.addr);
|
1679 |
return;
|
1680 |
} |
1681 |
|
1682 |
for (t = 1; t < KRT_METRICS_MAX; t++) |
1683 |
if (metrics[0] & (1 << t)) |
1684 |
{ |
1685 |
ea->attrs[n].id = EA_CODE(EAP_KRT, KRT_METRICS_OFFSET + t); |
1686 |
ea->attrs[n].flags = 0;
|
1687 |
ea->attrs[n].type = EAF_TYPE_INT; /* FIXME: Some are EAF_TYPE_BITFIELD */
|
1688 |
ea->attrs[n].u.data = metrics[t]; |
1689 |
n++; |
1690 |
} |
1691 |
|
1692 |
if (n > 0) |
1693 |
{ |
1694 |
ea->next = ra->eattrs; |
1695 |
ea->flags = EALF_SORTED; |
1696 |
ea->count = n; |
1697 |
ra->eattrs = ea; |
1698 |
} |
1699 |
} |
1700 |
|
1701 |
/*
|
1702 |
* Ideally, now we would send the received route to the rest of kernel code.
|
1703 |
* But IPv6 ECMP routes are sent as a sequence of routes, so we postpone it
|
1704 |
* and merge next hops until the end of the sequence.
|
1705 |
*/
|
1706 |
|
1707 |
if (!s->net)
|
1708 |
{ |
1709 |
/* Store the new route */
|
1710 |
s->net = net; |
1711 |
s->attrs = ra; |
1712 |
s->proto = p; |
1713 |
s->new = new; |
1714 |
s->krt_src = src; |
1715 |
s->krt_type = i->rtm_type; |
1716 |
s->krt_proto = i->rtm_protocol; |
1717 |
s->krt_metric = priority; |
1718 |
} |
1719 |
else
|
1720 |
{ |
1721 |
/* Merge next hops with the stored route */
|
1722 |
rta *oa = s->attrs; |
1723 |
|
1724 |
struct nexthop *nhs = &oa->nh;
|
1725 |
nexthop_insert(&nhs, &ra->nh); |
1726 |
|
1727 |
/* Perhaps new nexthop is inserted at the first position */
|
1728 |
if (nhs == &ra->nh)
|
1729 |
{ |
1730 |
/* Swap rtas */
|
1731 |
s->attrs = ra; |
1732 |
|
1733 |
/* Keep old eattrs */
|
1734 |
ra->eattrs = oa->eattrs; |
1735 |
} |
1736 |
} |
1737 |
} |
1738 |
|
1739 |
void
|
1740 |
krt_do_scan(struct krt_proto *p UNUSED) /* CONFIG_ALL_TABLES_AT_ONCE => p is NULL */ |
1741 |
{ |
1742 |
struct nlmsghdr *h;
|
1743 |
struct nl_parse_state s;
|
1744 |
|
1745 |
nl_parse_begin(&s, 1, 0); |
1746 |
nl_request_dump(AF_INET, RTM_GETROUTE); |
1747 |
while (h = nl_get_scan())
|
1748 |
if (h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)
|
1749 |
nl_parse_route(&s, h); |
1750 |
else
|
1751 |
log(L_DEBUG "nl_scan_fire: Unknown packet received (type=%d)", h->nlmsg_type);
|
1752 |
nl_parse_end(&s); |
1753 |
|
1754 |
nl_parse_begin(&s, 1, 1); |
1755 |
nl_request_dump(AF_INET6, RTM_GETROUTE); |
1756 |
while (h = nl_get_scan())
|
1757 |
if (h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)
|
1758 |
nl_parse_route(&s, h); |
1759 |
else
|
1760 |
log(L_DEBUG "nl_scan_fire: Unknown packet received (type=%d)", h->nlmsg_type);
|
1761 |
nl_parse_end(&s); |
1762 |
|
1763 |
nl_parse_begin(&s, 1, 1); |
1764 |
nl_request_dump(AF_MPLS, RTM_GETROUTE); |
1765 |
while (h = nl_get_scan())
|
1766 |
if (h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)
|
1767 |
nl_parse_route(&s, h); |
1768 |
else
|
1769 |
log(L_DEBUG "nl_scan_fire: Unknown packet received (type=%d)", h->nlmsg_type);
|
1770 |
nl_parse_end(&s); |
1771 |
} |
1772 |
|
1773 |
/*
|
1774 |
* Asynchronous Netlink interface
|
1775 |
*/
|
1776 |
|
1777 |
static sock *nl_async_sk; /* BIRD socket for asynchronous notifications */ |
1778 |
static byte *nl_async_rx_buffer; /* Receive buffer */ |
1779 |
|
1780 |
static void |
1781 |
nl_async_msg(struct nlmsghdr *h)
|
1782 |
{ |
1783 |
struct nl_parse_state s;
|
1784 |
|
1785 |
switch (h->nlmsg_type)
|
1786 |
{ |
1787 |
case RTM_NEWROUTE:
|
1788 |
case RTM_DELROUTE:
|
1789 |
DBG("KRT: Received async route notification (%d)\n", h->nlmsg_type);
|
1790 |
nl_parse_begin(&s, 0, 0); |
1791 |
nl_parse_route(&s, h); |
1792 |
nl_parse_end(&s); |
1793 |
break;
|
1794 |
case RTM_NEWLINK:
|
1795 |
case RTM_DELLINK:
|
1796 |
DBG("KRT: Received async link notification (%d)\n", h->nlmsg_type);
|
1797 |
if (kif_proto)
|
1798 |
nl_parse_link(h, 0);
|
1799 |
break;
|
1800 |
case RTM_NEWADDR:
|
1801 |
case RTM_DELADDR:
|
1802 |
DBG("KRT: Received async address notification (%d)\n", h->nlmsg_type);
|
1803 |
if (kif_proto)
|
1804 |
nl_parse_addr(h, 0);
|
1805 |
break;
|
1806 |
default:
|
1807 |
DBG("KRT: Received unknown async notification (%d)\n", h->nlmsg_type);
|
1808 |
} |
1809 |
} |
1810 |
|
1811 |
static int |
1812 |
nl_async_hook(sock *sk, uint size UNUSED) |
1813 |
{ |
1814 |
struct iovec iov = { nl_async_rx_buffer, NL_RX_SIZE };
|
1815 |
struct sockaddr_nl sa;
|
1816 |
struct msghdr m = {
|
1817 |
.msg_name = &sa, |
1818 |
.msg_namelen = sizeof(sa),
|
1819 |
.msg_iov = &iov, |
1820 |
.msg_iovlen = 1,
|
1821 |
}; |
1822 |
struct nlmsghdr *h;
|
1823 |
int x;
|
1824 |
uint len; |
1825 |
|
1826 |
x = recvmsg(sk->fd, &m, 0);
|
1827 |
if (x < 0) |
1828 |
{ |
1829 |
if (errno == ENOBUFS)
|
1830 |
{ |
1831 |
/*
|
1832 |
* Netlink reports some packets have been thrown away.
|
1833 |
* One day we might react to it by asking for route table
|
1834 |
* scan in near future.
|
1835 |
*/
|
1836 |
log(L_WARN "Kernel dropped some netlink messages, will resync on next scan.");
|
1837 |
return 1; /* More data are likely to be ready */ |
1838 |
} |
1839 |
else if (errno != EWOULDBLOCK) |
1840 |
log(L_ERR "Netlink recvmsg: %m");
|
1841 |
return 0; |
1842 |
} |
1843 |
if (sa.nl_pid) /* It isn't from the kernel */ |
1844 |
{ |
1845 |
DBG("Non-kernel packet\n");
|
1846 |
return 1; |
1847 |
} |
1848 |
h = (void *) nl_async_rx_buffer;
|
1849 |
len = x; |
1850 |
if (m.msg_flags & MSG_TRUNC)
|
1851 |
{ |
1852 |
log(L_WARN "Netlink got truncated asynchronous message");
|
1853 |
return 1; |
1854 |
} |
1855 |
while (NLMSG_OK(h, len))
|
1856 |
{ |
1857 |
nl_async_msg(h); |
1858 |
h = NLMSG_NEXT(h, len); |
1859 |
} |
1860 |
if (len)
|
1861 |
log(L_WARN "nl_async_hook: Found packet remnant of size %d", len);
|
1862 |
return 1; |
1863 |
} |
1864 |
|
1865 |
static void |
1866 |
nl_async_err_hook(sock *sk, int e UNUSED)
|
1867 |
{ |
1868 |
nl_async_hook(sk, 0);
|
1869 |
} |
1870 |
|
1871 |
static void |
1872 |
nl_open_async(void)
|
1873 |
{ |
1874 |
sock *sk; |
1875 |
struct sockaddr_nl sa;
|
1876 |
int fd;
|
1877 |
|
1878 |
if (nl_async_sk)
|
1879 |
return;
|
1880 |
|
1881 |
DBG("KRT: Opening async netlink socket\n");
|
1882 |
|
1883 |
fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
1884 |
if (fd < 0) |
1885 |
{ |
1886 |
log(L_ERR "Unable to open asynchronous rtnetlink socket: %m");
|
1887 |
return;
|
1888 |
} |
1889 |
|
1890 |
bzero(&sa, sizeof(sa));
|
1891 |
sa.nl_family = AF_NETLINK; |
1892 |
sa.nl_groups = RTMGRP_LINK | |
1893 |
RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE | |
1894 |
RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE; |
1895 |
|
1896 |
if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) |
1897 |
{ |
1898 |
log(L_ERR "Unable to bind asynchronous rtnetlink socket: %m");
|
1899 |
close(fd); |
1900 |
return;
|
1901 |
} |
1902 |
|
1903 |
nl_async_rx_buffer = xmalloc(NL_RX_SIZE); |
1904 |
|
1905 |
sk = nl_async_sk = sk_new(krt_pool); |
1906 |
sk->type = SK_MAGIC; |
1907 |
sk->rx_hook = nl_async_hook; |
1908 |
sk->err_hook = nl_async_err_hook; |
1909 |
sk->fd = fd; |
1910 |
if (sk_open(sk) < 0) |
1911 |
bug("Netlink: sk_open failed");
|
1912 |
} |
1913 |
|
1914 |
|
1915 |
/*
|
1916 |
* Interface to the UNIX krt module
|
1917 |
*/
|
1918 |
|
1919 |
void
|
1920 |
krt_sys_io_init(void)
|
1921 |
{ |
1922 |
nl_linpool = lp_new_default(krt_pool); |
1923 |
HASH_INIT(nl_table_map, krt_pool, 6);
|
1924 |
} |
1925 |
|
1926 |
int
|
1927 |
krt_sys_start(struct krt_proto *p)
|
1928 |
{ |
1929 |
struct krt_proto *old = HASH_FIND(nl_table_map, RTH, p->af, krt_table_id(p));
|
1930 |
|
1931 |
if (old)
|
1932 |
{ |
1933 |
log(L_ERR "%s: Kernel table %u already registered by %s",
|
1934 |
p->p.name, krt_table_id(p), old->p.name); |
1935 |
return 0; |
1936 |
} |
1937 |
|
1938 |
HASH_INSERT2(nl_table_map, RTH, krt_pool, p); |
1939 |
|
1940 |
nl_open(); |
1941 |
nl_open_async(); |
1942 |
|
1943 |
return 1; |
1944 |
} |
1945 |
|
1946 |
void
|
1947 |
krt_sys_shutdown(struct krt_proto *p)
|
1948 |
{ |
1949 |
HASH_REMOVE2(nl_table_map, RTH, krt_pool, p); |
1950 |
} |
1951 |
|
1952 |
int
|
1953 |
krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n, struct krt_config *o) |
1954 |
{ |
1955 |
return (n->sys.table_id == o->sys.table_id) && (n->sys.metric == o->sys.metric);
|
1956 |
} |
1957 |
|
1958 |
void
|
1959 |
krt_sys_init_config(struct krt_config *cf)
|
1960 |
{ |
1961 |
cf->sys.table_id = RT_TABLE_MAIN; |
1962 |
cf->sys.metric = 32;
|
1963 |
} |
1964 |
|
1965 |
void
|
1966 |
krt_sys_copy_config(struct krt_config *d, struct krt_config *s) |
1967 |
{ |
1968 |
d->sys.table_id = s->sys.table_id; |
1969 |
d->sys.metric = s->sys.metric; |
1970 |
} |
1971 |
|
1972 |
static const char *krt_metrics_names[KRT_METRICS_MAX] = { |
1973 |
NULL, "lock", "mtu", "window", "rtt", "rttvar", "sstresh", "cwnd", "advmss", |
1974 |
"reordering", "hoplimit", "initcwnd", "features", "rto_min", "initrwnd", "quickack" |
1975 |
}; |
1976 |
|
1977 |
static const char *krt_features_names[KRT_FEATURES_MAX] = { |
1978 |
"ecn", NULL, NULL, "allfrag" |
1979 |
}; |
1980 |
|
1981 |
int
|
1982 |
krt_sys_get_attr(eattr *a, byte *buf, int buflen UNUSED)
|
1983 |
{ |
1984 |
switch (a->id)
|
1985 |
{ |
1986 |
case EA_KRT_PREFSRC:
|
1987 |
bsprintf(buf, "prefsrc");
|
1988 |
return GA_NAME;
|
1989 |
|
1990 |
case EA_KRT_REALM:
|
1991 |
bsprintf(buf, "realm");
|
1992 |
return GA_NAME;
|
1993 |
|
1994 |
case EA_KRT_SCOPE:
|
1995 |
bsprintf(buf, "scope");
|
1996 |
return GA_NAME;
|
1997 |
|
1998 |
case EA_KRT_LOCK:
|
1999 |
buf += bsprintf(buf, "lock:");
|
2000 |
ea_format_bitfield(a, buf, buflen, krt_metrics_names, 2, KRT_METRICS_MAX);
|
2001 |
return GA_FULL;
|
2002 |
|
2003 |
case EA_KRT_FEATURES:
|
2004 |
buf += bsprintf(buf, "features:");
|
2005 |
ea_format_bitfield(a, buf, buflen, krt_features_names, 0, KRT_FEATURES_MAX);
|
2006 |
return GA_FULL;
|
2007 |
|
2008 |
default:;
|
2009 |
int id = (int)EA_ID(a->id) - KRT_METRICS_OFFSET; |
2010 |
if (id > 0 && id < KRT_METRICS_MAX) |
2011 |
{ |
2012 |
bsprintf(buf, "%s", krt_metrics_names[id]);
|
2013 |
return GA_NAME;
|
2014 |
} |
2015 |
|
2016 |
return GA_UNKNOWN;
|
2017 |
} |
2018 |
} |
2019 |
|
2020 |
|
2021 |
|
2022 |
void
|
2023 |
kif_sys_start(struct kif_proto *p UNUSED)
|
2024 |
{ |
2025 |
nl_open(); |
2026 |
nl_open_async(); |
2027 |
} |
2028 |
|
2029 |
void
|
2030 |
kif_sys_shutdown(struct kif_proto *p UNUSED)
|
2031 |
{ |
2032 |
} |
2033 |
|
2034 |
int
|
2035 |
kif_update_sysdep_addr(struct iface *i UNUSED)
|
2036 |
{ |
2037 |
return 0; |
2038 |
} |