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