]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: MIT |
ca10883e DS |
2 | /* |
3 | Copyright (c) 2007, 2008 by Juliusz Chroboczek | |
ca10883e DS |
4 | */ |
5 | ||
6 | #include <zebra.h> | |
7 | #include "if.h" | |
8 | ||
9 | #include "babeld.h" | |
10 | #include "util.h" | |
11 | #include "net.h" | |
12 | #include "babel_interface.h" | |
13 | #include "source.h" | |
14 | #include "neighbour.h" | |
15 | #include "route.h" | |
16 | #include "xroute.h" | |
17 | #include "resend.h" | |
18 | #include "message.h" | |
19 | #include "kernel.h" | |
20 | #include "babel_main.h" | |
e33b116c | 21 | #include "babel_errors.h" |
ca10883e DS |
22 | |
23 | static unsigned char packet_header[4] = {42, 2}; | |
24 | ||
25 | int split_horizon = 1; | |
26 | ||
27 | unsigned short myseqno = 0; | |
28 | ||
29 | #define UNICAST_BUFSIZE 1024 | |
30 | static int unicast_buffered = 0; | |
31 | static unsigned char *unicast_buffer = NULL; | |
32 | struct neighbour *unicast_neighbour = NULL; | |
33 | struct timeval unicast_flush_timeout = {0, 0}; | |
34 | ||
35 | /* Minimum TLV _body_ length for TLVs of particular types (0 = no limit). */ | |
36 | static const unsigned char tlv_min_length[MESSAGE_MAX + 1] = | |
37 | { | |
38 | [ MESSAGE_PAD1 ] = 0, | |
39 | [ MESSAGE_PADN ] = 0, | |
40 | [ MESSAGE_ACK_REQ ] = 6, | |
41 | [ MESSAGE_ACK ] = 2, | |
42 | [ MESSAGE_HELLO ] = 6, | |
43 | [ MESSAGE_IHU ] = 6, | |
44 | [ MESSAGE_ROUTER_ID ] = 10, | |
45 | [ MESSAGE_NH ] = 2, | |
46 | [ MESSAGE_UPDATE ] = 10, | |
47 | [ MESSAGE_REQUEST ] = 2, | |
48 | [ MESSAGE_MH_REQUEST ] = 14, | |
49 | }; | |
50 | ||
51 | /* Parse a network prefix, encoded in the somewhat baroque compressed | |
52 | representation used by Babel. Return the number of bytes parsed. */ | |
53 | static int | |
54 | network_prefix(int ae, int plen, unsigned int omitted, | |
55 | const unsigned char *p, const unsigned char *dp, | |
56 | unsigned int len, unsigned char *p_r) | |
57 | { | |
58 | unsigned pb; | |
59 | unsigned char prefix[16]; | |
60 | int ret = -1; | |
61 | ||
62 | if(plen >= 0) | |
63 | pb = (plen + 7) / 8; | |
64 | else if(ae == 1) | |
65 | pb = 4; | |
66 | else | |
67 | pb = 16; | |
68 | ||
69 | if(pb > 16) | |
70 | return -1; | |
71 | ||
72 | memset(prefix, 0, 16); | |
73 | ||
74 | switch(ae) { | |
75 | case 0: | |
76 | ret = 0; | |
77 | break; | |
78 | case 1: | |
79 | if(omitted > 4 || pb > 4 || (pb > omitted && len < pb - omitted)) | |
80 | return -1; | |
81 | memcpy(prefix, v4prefix, 12); | |
82 | if(omitted) { | |
83 | if (dp == NULL || !v4mapped(dp)) return -1; | |
84 | memcpy(prefix, dp, 12 + omitted); | |
85 | } | |
86 | if(pb > omitted) memcpy(prefix + 12 + omitted, p, pb - omitted); | |
87 | ret = pb - omitted; | |
88 | break; | |
89 | case 2: | |
90 | if(omitted > 16 || (pb > omitted && len < pb - omitted)) return -1; | |
91 | if(omitted) { | |
92 | if (dp == NULL || v4mapped(dp)) return -1; | |
93 | memcpy(prefix, dp, omitted); | |
94 | } | |
95 | if(pb > omitted) memcpy(prefix + omitted, p, pb - omitted); | |
96 | ret = pb - omitted; | |
97 | break; | |
98 | case 3: | |
99 | if(pb > 8 && len < pb - 8) return -1; | |
100 | prefix[0] = 0xfe; | |
101 | prefix[1] = 0x80; | |
102 | if(pb > 8) memcpy(prefix + 8, p, pb - 8); | |
103 | ret = pb - 8; | |
104 | break; | |
105 | default: | |
106 | return -1; | |
107 | } | |
108 | ||
109 | mask_prefix(p_r, prefix, plen < 0 ? 128 : ae == 1 ? plen + 96 : plen); | |
110 | return ret; | |
111 | } | |
112 | ||
a76cf7e4 DS |
113 | static bool parse_update_subtlv(const unsigned char *a, int alen, |
114 | unsigned char *channels) | |
ca10883e DS |
115 | { |
116 | int type, len, i = 0; | |
117 | ||
118 | while(i < alen) { | |
119 | type = a[i]; | |
120 | if(type == SUBTLV_PAD1) { | |
121 | i++; | |
122 | continue; | |
123 | } | |
124 | ||
c3793352 | 125 | if(i + 1 >= alen) { |
5b003f31 | 126 | flog_err(EC_BABEL_PACKET, "Received truncated attributes."); |
a76cf7e4 DS |
127 | return false; |
128 | } | |
ca10883e | 129 | len = a[i + 1]; |
c3793352 | 130 | if(i + len + 2 > alen) { |
5b003f31 | 131 | flog_err(EC_BABEL_PACKET, "Received truncated attributes."); |
a76cf7e4 DS |
132 | return false; |
133 | } | |
ca10883e | 134 | |
a76cf7e4 DS |
135 | if (type & SUBTLV_MANDATORY) { |
136 | /* | |
137 | * RFC 8966 - 4.4 | |
138 | * If the mandatory bit is set, then the whole enclosing | |
139 | * TLV MUST be silently ignored (except for updating the | |
140 | * parser state by a Router-Id, Next Hop, or Update TLV, | |
141 | * as described in the next section). | |
142 | */ | |
143 | debugf(BABEL_DEBUG_COMMON, | |
144 | "Received Mandatory bit set but this FRR version is not prepared to handle it at this point"); | |
145 | return true; | |
146 | } else if (type == SUBTLV_PADN) { | |
147 | /* Nothing. */ | |
148 | } else if (type == SUBTLV_DIVERSITY) { | |
149 | if (len > DIVERSITY_HOPS) { | |
150 | flog_err( | |
151 | EC_BABEL_PACKET, | |
152 | "Received overlong channel information (%d > %d).n", | |
153 | len, DIVERSITY_HOPS); | |
154 | len = DIVERSITY_HOPS; | |
155 | } | |
156 | if (memchr(a + i + 2, 0, len) != NULL) { | |
157 | /* 0 is reserved. */ | |
158 | flog_err(EC_BABEL_PACKET, | |
159 | "Channel information contains 0!"); | |
160 | return false; | |
161 | } | |
162 | memset(channels, 0, DIVERSITY_HOPS); | |
163 | memcpy(channels, a + i + 2, len); | |
164 | } else { | |
165 | debugf(BABEL_DEBUG_COMMON, | |
166 | "Received unknown route attribute %d.", type); | |
167 | } | |
ca10883e | 168 | |
a76cf7e4 | 169 | i += len + 2; |
ca10883e | 170 | } |
a76cf7e4 | 171 | return false; |
ca10883e DS |
172 | } |
173 | ||
174 | static int | |
175 | parse_hello_subtlv(const unsigned char *a, int alen, | |
176 | unsigned int *hello_send_us) | |
177 | { | |
178 | int type, len, i = 0, ret = 0; | |
179 | ||
180 | while(i < alen) { | |
c3793352 | 181 | type = a[i]; |
ca10883e DS |
182 | if(type == SUBTLV_PAD1) { |
183 | i++; | |
184 | continue; | |
185 | } | |
186 | ||
c3793352 | 187 | if(i + 1 >= alen) { |
5b003f31 | 188 | flog_err(EC_BABEL_PACKET, |
e33b116c | 189 | "Received truncated sub-TLV on Hello message."); |
ca10883e DS |
190 | return -1; |
191 | } | |
192 | len = a[i + 1]; | |
c3793352 | 193 | if(i + len + 2 > alen) { |
5b003f31 | 194 | flog_err(EC_BABEL_PACKET, |
e33b116c | 195 | "Received truncated sub-TLV on Hello message."); |
ca10883e DS |
196 | return -1; |
197 | } | |
198 | ||
a76cf7e4 DS |
199 | if (type & SUBTLV_MANDATORY) { |
200 | /* | |
201 | * RFC 8966 4.4 | |
202 | * If the mandatory bit is set, then the whole enclosing | |
203 | * TLV MUST be silently ignored (except for updating the | |
204 | * parser state by a Router-Id, Next Hop, or Update TLV, as | |
205 | * described in the next section). | |
206 | */ | |
207 | debugf(BABEL_DEBUG_COMMON, | |
208 | "Received subtlv with Mandatory bit, this version of FRR is not prepared to handle this currently"); | |
209 | return -2; | |
210 | } else if (type == SUBTLV_PADN) { | |
211 | /* Nothing to do. */ | |
212 | } else if (type == SUBTLV_TIMESTAMP) { | |
213 | if (len >= 4) { | |
214 | DO_NTOHL(*hello_send_us, a + i + 2); | |
215 | ret = 1; | |
216 | } else { | |
217 | flog_err( | |
218 | EC_BABEL_PACKET, | |
219 | "Received incorrect RTT sub-TLV on Hello message."); | |
220 | } | |
221 | } else { | |
222 | debugf(BABEL_DEBUG_COMMON, | |
223 | "Received unknown Hello sub-TLV type %d.", type); | |
224 | } | |
ca10883e | 225 | |
a76cf7e4 | 226 | i += len + 2; |
ca10883e DS |
227 | } |
228 | return ret; | |
229 | } | |
230 | ||
231 | static int | |
232 | parse_ihu_subtlv(const unsigned char *a, int alen, | |
233 | unsigned int *hello_send_us, | |
234 | unsigned int *hello_rtt_receive_time) | |
235 | { | |
236 | int type, len, i = 0, ret = 0; | |
237 | ||
238 | while(i < alen) { | |
c3793352 | 239 | type = a[i]; |
ca10883e DS |
240 | if(type == SUBTLV_PAD1) { |
241 | i++; | |
242 | continue; | |
243 | } | |
244 | ||
c3793352 | 245 | if(i + 1 >= alen) { |
5b003f31 | 246 | flog_err(EC_BABEL_PACKET, |
e33b116c | 247 | "Received truncated sub-TLV on IHU message."); |
ca10883e DS |
248 | return -1; |
249 | } | |
250 | len = a[i + 1]; | |
c3793352 | 251 | if(i + len + 2 > alen) { |
5b003f31 | 252 | flog_err(EC_BABEL_PACKET, |
e33b116c | 253 | "Received truncated sub-TLV on IHU message."); |
ca10883e DS |
254 | return -1; |
255 | } | |
256 | ||
257 | if(type == SUBTLV_PADN) { | |
258 | /* Nothing to do. */ | |
259 | } else if(type == SUBTLV_TIMESTAMP) { | |
260 | if(len >= 8) { | |
261 | DO_NTOHL(*hello_send_us, a + i + 2); | |
262 | DO_NTOHL(*hello_rtt_receive_time, a + i + 6); | |
263 | ret = 1; | |
264 | } | |
265 | else { | |
5b003f31 | 266 | flog_err(EC_BABEL_PACKET, |
e33b116c | 267 | "Received incorrect RTT sub-TLV on IHU message."); |
ca10883e DS |
268 | } |
269 | } else { | |
270 | debugf(BABEL_DEBUG_COMMON, | |
271 | "Received unknown IHU sub-TLV type %d.", type); | |
272 | } | |
273 | ||
274 | i += len + 2; | |
275 | } | |
276 | return ret; | |
277 | } | |
278 | ||
279 | static int | |
280 | network_address(int ae, const unsigned char *a, unsigned int len, | |
281 | unsigned char *a_r) | |
282 | { | |
283 | return network_prefix(ae, -1, 0, a, NULL, len, a_r); | |
284 | } | |
285 | ||
286 | static int | |
287 | channels_len(unsigned char *channels) | |
288 | { | |
289 | unsigned char *p = memchr(channels, 0, DIVERSITY_HOPS); | |
290 | return p ? (p - channels) : DIVERSITY_HOPS; | |
291 | } | |
292 | ||
293 | /* Check, that the provided frame consists of a valid Babel packet header | |
294 | followed by a sequence of TLVs. TLVs of known types are also checked to meet | |
295 | minimum length constraints defined for each. Return 0 for no errors. */ | |
296 | static int | |
8128153b | 297 | babel_packet_examin(const unsigned char *packet, int packetlen, int *blength) |
ca10883e | 298 | { |
50044ec7 | 299 | int i = 0, bodylen; |
ca10883e DS |
300 | const unsigned char *message; |
301 | unsigned char type, len; | |
302 | ||
303 | if(packetlen < 4 || packet[0] != 42 || packet[1] != 2) | |
304 | return 1; | |
305 | DO_NTOHS(bodylen, packet + 2); | |
50044ec7 | 306 | if(bodylen + 4 > packetlen) { |
307 | debugf(BABEL_DEBUG_COMMON, "Received truncated packet (%d + 4 > %d).", | |
308 | bodylen, packetlen); | |
309 | return 1; | |
310 | } | |
ca10883e DS |
311 | while (i < bodylen){ |
312 | message = packet + 4 + i; | |
313 | type = message[0]; | |
314 | if(type == MESSAGE_PAD1) { | |
315 | i++; | |
316 | continue; | |
317 | } | |
c3793352 | 318 | if(i + 2 > bodylen) { |
ca10883e DS |
319 | debugf(BABEL_DEBUG_COMMON,"Received truncated message."); |
320 | return 1; | |
321 | } | |
322 | len = message[1]; | |
c3793352 | 323 | if(i + len + 2 > bodylen) { |
ca10883e DS |
324 | debugf(BABEL_DEBUG_COMMON,"Received truncated message."); |
325 | return 1; | |
326 | } | |
327 | /* not Pad1 */ | |
328 | if(type <= MESSAGE_MAX && tlv_min_length[type] && len < tlv_min_length[type]) { | |
329 | debugf(BABEL_DEBUG_COMMON,"Undersized %u TLV", type); | |
330 | return 1; | |
331 | } | |
332 | i += len + 2; | |
333 | } | |
8128153b DS |
334 | |
335 | *blength = bodylen; | |
ca10883e DS |
336 | return 0; |
337 | } | |
338 | ||
339 | void | |
340 | parse_packet(const unsigned char *from, struct interface *ifp, | |
341 | const unsigned char *packet, int packetlen) | |
342 | { | |
343 | int i; | |
344 | const unsigned char *message; | |
345 | unsigned char type, len; | |
346 | int bodylen; | |
347 | struct neighbour *neigh; | |
348 | int have_router_id = 0, have_v4_prefix = 0, have_v6_prefix = 0, | |
349 | have_v4_nh = 0, have_v6_nh = 0; | |
350 | unsigned char router_id[8], v4_prefix[16], v6_prefix[16], | |
351 | v4_nh[16], v6_nh[16]; | |
352 | int have_hello_rtt = 0; | |
353 | /* Content of the RTT sub-TLV on IHU messages. */ | |
354 | unsigned int hello_send_us = 0, hello_rtt_receive_time = 0; | |
355 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
356 | ||
357 | if(babel_ifp->flags & BABEL_IF_TIMESTAMPS) { | |
358 | /* We want to track exactly when we received this packet. */ | |
359 | gettime(&babel_now); | |
360 | } | |
361 | ||
362 | if(!linklocal(from)) { | |
5b003f31 | 363 | flog_err(EC_BABEL_PACKET, |
e33b116c DS |
364 | "Received packet from non-local address %s.", |
365 | format_address(from)); | |
ca10883e DS |
366 | return; |
367 | } | |
368 | ||
8128153b | 369 | if (babel_packet_examin (packet, packetlen, &bodylen)) { |
5b003f31 | 370 | flog_err(EC_BABEL_PACKET, |
e33b116c DS |
371 | "Received malformed packet on %s from %s.", |
372 | ifp->name, format_address(from)); | |
ca10883e DS |
373 | return; |
374 | } | |
375 | ||
376 | neigh = find_neighbour(from, ifp); | |
377 | if(neigh == NULL) { | |
5b003f31 | 378 | flog_err(EC_BABEL_PACKET, "Couldn't allocate neighbour."); |
ca10883e DS |
379 | return; |
380 | } | |
381 | ||
ca10883e DS |
382 | i = 0; |
383 | while(i < bodylen) { | |
384 | message = packet + 4 + i; | |
385 | type = message[0]; | |
386 | if(type == MESSAGE_PAD1) { | |
387 | debugf(BABEL_DEBUG_COMMON,"Received pad1 from %s on %s.", | |
388 | format_address(from), ifp->name); | |
389 | i++; | |
390 | continue; | |
391 | } | |
392 | len = message[1]; | |
393 | ||
394 | if(type == MESSAGE_PADN) { | |
395 | debugf(BABEL_DEBUG_COMMON,"Received pad%d from %s on %s.", | |
396 | len, format_address(from), ifp->name); | |
397 | } else if(type == MESSAGE_ACK_REQ) { | |
398 | unsigned short nonce, interval; | |
399 | DO_NTOHS(nonce, message + 4); | |
400 | DO_NTOHS(interval, message + 6); | |
401 | debugf(BABEL_DEBUG_COMMON,"Received ack-req (%04X %d) from %s on %s.", | |
402 | nonce, interval, format_address(from), ifp->name); | |
403 | send_ack(neigh, nonce, interval); | |
404 | } else if(type == MESSAGE_ACK) { | |
405 | debugf(BABEL_DEBUG_COMMON,"Received ack from %s on %s.", | |
406 | format_address(from), ifp->name); | |
407 | /* Nothing right now */ | |
408 | } else if(type == MESSAGE_HELLO) { | |
54a3e60b DS |
409 | unsigned short seqno, interval, flags; |
410 | int changed; | |
411 | unsigned int timestamp = 0; | |
412 | ||
413 | #define BABEL_UNICAST_HELLO 0x8000 | |
414 | DO_NTOHS(flags, message + 2); | |
415 | ||
54a3e60b DS |
416 | /* |
417 | * RFC 8966 Appendix F | |
418 | * TL;DR -> Please ignore Unicast hellos until FRR's | |
419 | * BABEL is brought up to date | |
420 | */ | |
421 | if (CHECK_FLAG(flags, BABEL_UNICAST_HELLO)) { | |
422 | debugf(BABEL_DEBUG_COMMON, | |
423 | "Received Unicast Hello from %s on %s that FRR is not prepared to understand yet", | |
424 | format_address(from), ifp->name); | |
ae1e0e1f | 425 | goto done; |
54a3e60b DS |
426 | } |
427 | ||
428 | DO_NTOHS(seqno, message + 4); | |
429 | DO_NTOHS(interval, message + 6); | |
430 | debugf(BABEL_DEBUG_COMMON, | |
431 | "Received hello %d (%d) from %s on %s.", seqno, interval, | |
432 | format_address(from), ifp->name); | |
433 | ||
434 | /* | |
435 | * RFC 8966 Appendix F | |
436 | * TL;DR -> Please ignore any Hello packets with the interval | |
437 | * field set to 0 | |
438 | */ | |
439 | if (interval == 0) { | |
440 | debugf(BABEL_DEBUG_COMMON, | |
441 | "Received hello from %s on %s should be ignored as that this version of FRR does not know how to properly handle interval == 0", | |
442 | format_address(from), ifp->name); | |
ae1e0e1f | 443 | goto done; |
54a3e60b DS |
444 | } |
445 | ||
446 | changed = update_neighbour(neigh, seqno, interval); | |
447 | update_neighbour_metric(neigh, changed); | |
448 | if (interval > 0) | |
449 | /* Multiply by 3/2 to allow hellos to expire. */ | |
450 | schedule_neighbours_check(interval * 15, 0); | |
451 | /* Sub-TLV handling. */ | |
452 | if (len > 8) { | |
453 | if (parse_hello_subtlv(message + 8, len - 6, | |
454 | ×tamp) > 0) { | |
455 | neigh->hello_send_us = timestamp; | |
456 | neigh->hello_rtt_receive_time = babel_now; | |
457 | have_hello_rtt = 1; | |
458 | } | |
459 | } | |
ca10883e DS |
460 | } else if(type == MESSAGE_IHU) { |
461 | unsigned short txcost, interval; | |
462 | unsigned char address[16]; | |
463 | int rc; | |
464 | DO_NTOHS(txcost, message + 4); | |
465 | DO_NTOHS(interval, message + 6); | |
466 | rc = network_address(message[2], message + 8, len - 6, address); | |
467 | if(rc < 0) goto fail; | |
468 | debugf(BABEL_DEBUG_COMMON,"Received ihu %d (%d) from %s on %s for %s.", | |
469 | txcost, interval, | |
470 | format_address(from), ifp->name, | |
471 | format_address(address)); | |
472 | if(message[2] == 0 || is_interface_ll_address(ifp, address)) { | |
473 | int changed = txcost != neigh->txcost; | |
474 | neigh->txcost = txcost; | |
475 | neigh->ihu_time = babel_now; | |
476 | neigh->ihu_interval = interval; | |
477 | update_neighbour_metric(neigh, changed); | |
478 | if(interval > 0) | |
479 | /* Multiply by 3/2 to allow neighbours to expire. */ | |
480 | schedule_neighbours_check(interval * 45, 0); | |
481 | /* RTT sub-TLV. */ | |
482 | if(len > 10 + rc) | |
483 | parse_ihu_subtlv(message + 8 + rc, len - 6 - rc, | |
484 | &hello_send_us, &hello_rtt_receive_time); | |
485 | } | |
486 | } else if(type == MESSAGE_ROUTER_ID) { | |
487 | memcpy(router_id, message + 4, 8); | |
488 | have_router_id = 1; | |
489 | debugf(BABEL_DEBUG_COMMON,"Received router-id %s from %s on %s.", | |
490 | format_eui64(router_id), format_address(from), ifp->name); | |
491 | } else if(type == MESSAGE_NH) { | |
492 | unsigned char nh[16]; | |
493 | int rc; | |
494 | rc = network_address(message[2], message + 4, len - 2, | |
495 | nh); | |
496 | if(rc < 0) { | |
497 | have_v4_nh = 0; | |
498 | have_v6_nh = 0; | |
499 | goto fail; | |
500 | } | |
501 | debugf(BABEL_DEBUG_COMMON,"Received nh %s (%d) from %s on %s.", | |
502 | format_address(nh), message[2], | |
503 | format_address(from), ifp->name); | |
504 | if(message[2] == 1) { | |
505 | memcpy(v4_nh, nh, 16); | |
506 | have_v4_nh = 1; | |
507 | } else { | |
508 | memcpy(v6_nh, nh, 16); | |
509 | have_v6_nh = 1; | |
510 | } | |
511 | } else if(type == MESSAGE_UPDATE) { | |
512 | unsigned char prefix[16], *nh; | |
513 | unsigned char plen; | |
514 | unsigned char channels[DIVERSITY_HOPS]; | |
515 | unsigned short interval, seqno, metric; | |
516 | int rc, parsed_len; | |
a76cf7e4 DS |
517 | bool ignore_update = false; |
518 | ||
519 | DO_NTOHS(interval, message + 6); | |
ca10883e DS |
520 | DO_NTOHS(seqno, message + 8); |
521 | DO_NTOHS(metric, message + 10); | |
522 | if(message[5] == 0 || | |
523 | (message[2] == 1 ? have_v4_prefix : have_v6_prefix)) | |
524 | rc = network_prefix(message[2], message[4], message[5], | |
525 | message + 12, | |
526 | message[2] == 1 ? v4_prefix : v6_prefix, | |
527 | len - 10, prefix); | |
528 | else | |
529 | rc = -1; | |
530 | if(rc < 0) { | |
531 | if(message[3] & 0x80) | |
532 | have_v4_prefix = have_v6_prefix = 0; | |
533 | goto fail; | |
534 | } | |
535 | parsed_len = 10 + rc; | |
536 | ||
537 | plen = message[4] + (message[2] == 1 ? 96 : 0); | |
538 | ||
539 | if(message[3] & 0x80) { | |
540 | if(message[2] == 1) { | |
541 | memcpy(v4_prefix, prefix, 16); | |
542 | have_v4_prefix = 1; | |
543 | } else { | |
544 | memcpy(v6_prefix, prefix, 16); | |
545 | have_v6_prefix = 1; | |
546 | } | |
547 | } | |
548 | if(message[3] & 0x40) { | |
549 | if(message[2] == 1) { | |
550 | memset(router_id, 0, 4); | |
551 | memcpy(router_id + 4, prefix + 12, 4); | |
552 | } else { | |
553 | memcpy(router_id, prefix + 8, 8); | |
554 | } | |
555 | have_router_id = 1; | |
556 | } | |
557 | if(!have_router_id && message[2] != 0) { | |
5b003f31 | 558 | flog_err(EC_BABEL_PACKET, |
e33b116c | 559 | "Received prefix with no router id."); |
ca10883e DS |
560 | goto fail; |
561 | } | |
562 | debugf(BABEL_DEBUG_COMMON,"Received update%s%s for %s from %s on %s.", | |
563 | (message[3] & 0x80) ? "/prefix" : "", | |
564 | (message[3] & 0x40) ? "/id" : "", | |
565 | format_prefix(prefix, plen), | |
566 | format_address(from), ifp->name); | |
567 | ||
568 | if(message[2] == 0) { | |
569 | if(metric < 0xFFFF) { | |
5b003f31 | 570 | flog_err(EC_BABEL_PACKET, |
e33b116c | 571 | "Received wildcard update with finite metric."); |
ca10883e DS |
572 | goto done; |
573 | } | |
574 | retract_neighbour_routes(neigh); | |
575 | goto done; | |
576 | } else if(message[2] == 1) { | |
577 | if(!have_v4_nh) | |
578 | goto fail; | |
579 | nh = v4_nh; | |
580 | } else if(have_v6_nh) { | |
581 | nh = v6_nh; | |
582 | } else { | |
583 | nh = neigh->address; | |
584 | } | |
585 | ||
586 | if(message[2] == 1) { | |
587 | if(!babel_get_if_nfo(ifp)->ipv4) | |
588 | goto done; | |
589 | } | |
590 | ||
591 | if((babel_get_if_nfo(ifp)->flags & BABEL_IF_FARAWAY)) { | |
592 | channels[0] = 0; | |
593 | } else { | |
594 | /* This will be overwritten by parse_update_subtlv below. */ | |
595 | if(metric < 256) { | |
596 | /* Assume non-interfering (wired) link. */ | |
597 | channels[0] = 0; | |
598 | } else { | |
599 | /* Assume interfering. */ | |
600 | channels[0] = BABEL_IF_CHANNEL_INTERFERING; | |
601 | channels[1] = 0; | |
602 | } | |
603 | ||
604 | if(parsed_len < len) | |
a76cf7e4 DS |
605 | ignore_update = |
606 | parse_update_subtlv(message + 2 + parsed_len, | |
607 | len - parsed_len, channels); | |
608 | } | |
609 | ||
d5260dc1 | 610 | if (!ignore_update) |
a76cf7e4 DS |
611 | update_route(router_id, prefix, plen, seqno, metric, |
612 | interval, neigh, nh, channels, | |
613 | channels_len(channels)); | |
614 | } else if(type == MESSAGE_REQUEST) { | |
ca10883e DS |
615 | unsigned char prefix[16], plen; |
616 | int rc; | |
617 | rc = network_prefix(message[2], message[3], 0, | |
618 | message + 4, NULL, len - 2, prefix); | |
619 | if(rc < 0) goto fail; | |
620 | plen = message[3] + (message[2] == 1 ? 96 : 0); | |
621 | debugf(BABEL_DEBUG_COMMON,"Received request for %s from %s on %s.", | |
622 | message[2] == 0 ? "any" : format_prefix(prefix, plen), | |
623 | format_address(from), ifp->name); | |
624 | if(message[2] == 0) { | |
625 | struct babel_interface *neigh_ifp =babel_get_if_nfo(neigh->ifp); | |
626 | /* If a neighbour is requesting a full route dump from us, | |
627 | we might as well send it an IHU. */ | |
628 | send_ihu(neigh, NULL); | |
629 | /* Since nodes send wildcard requests on boot, booting | |
630 | a large number of nodes at the same time may cause an | |
631 | update storm. Ignore a wildcard request that happens | |
632 | shortly after we sent a full update. */ | |
633 | if(neigh_ifp->last_update_time < | |
634 | (time_t)(babel_now.tv_sec - | |
635 | MAX(neigh_ifp->hello_interval / 100, 1))) | |
636 | send_update(neigh->ifp, 0, NULL, 0); | |
637 | } else { | |
638 | send_update(neigh->ifp, 0, prefix, plen); | |
639 | } | |
640 | } else if(type == MESSAGE_MH_REQUEST) { | |
641 | unsigned char prefix[16], plen; | |
642 | unsigned short seqno; | |
643 | int rc; | |
644 | DO_NTOHS(seqno, message + 4); | |
645 | rc = network_prefix(message[2], message[3], 0, | |
646 | message + 16, NULL, len - 14, prefix); | |
647 | if(rc < 0) goto fail; | |
648 | plen = message[3] + (message[2] == 1 ? 96 : 0); | |
649 | debugf(BABEL_DEBUG_COMMON,"Received request (%d) for %s from %s on %s (%s, %d).", | |
650 | message[6], | |
651 | format_prefix(prefix, plen), | |
652 | format_address(from), ifp->name, | |
653 | format_eui64(message + 8), seqno); | |
654 | handle_request(neigh, prefix, plen, message[6], | |
655 | seqno, message + 8); | |
656 | } else { | |
657 | debugf(BABEL_DEBUG_COMMON,"Received unknown packet type %d from %s on %s.", | |
658 | type, format_address(from), ifp->name); | |
659 | } | |
660 | done: | |
661 | i += len + 2; | |
662 | continue; | |
663 | ||
664 | fail: | |
5b003f31 | 665 | flog_err(EC_BABEL_PACKET, |
e33b116c DS |
666 | "Couldn't parse packet (%d, %d) from %s on %s.", |
667 | message[0], message[1], format_address(from), ifp->name); | |
ca10883e DS |
668 | goto done; |
669 | } | |
670 | ||
671 | /* We can calculate the RTT to this neighbour. */ | |
672 | if(have_hello_rtt && hello_send_us && hello_rtt_receive_time) { | |
673 | int remote_waiting_us, local_waiting_us; | |
674 | unsigned int rtt, smoothed_rtt; | |
675 | unsigned int old_rttcost; | |
676 | int changed = 0; | |
677 | remote_waiting_us = neigh->hello_send_us - hello_rtt_receive_time; | |
678 | local_waiting_us = time_us(neigh->hello_rtt_receive_time) - | |
679 | hello_send_us; | |
680 | ||
681 | /* Sanity checks (validity window of 10 minutes). */ | |
682 | if(remote_waiting_us < 0 || local_waiting_us < 0 || | |
683 | remote_waiting_us > 600000000 || local_waiting_us > 600000000) | |
684 | return; | |
685 | ||
686 | rtt = MAX(0, local_waiting_us - remote_waiting_us); | |
1d5453d6 | 687 | debugf(BABEL_DEBUG_COMMON, "RTT to %s on %s sample result: %d us.", |
ca10883e DS |
688 | format_address(from), ifp->name, rtt); |
689 | ||
690 | old_rttcost = neighbour_rttcost(neigh); | |
691 | if (valid_rtt(neigh)) { | |
692 | /* Running exponential average. */ | |
693 | smoothed_rtt = (babel_ifp->rtt_decay * rtt + | |
694 | (256 - babel_ifp->rtt_decay) * neigh->rtt); | |
695 | /* Rounding (up or down) to get closer to the sample. */ | |
696 | neigh->rtt = (neigh->rtt >= rtt) ? smoothed_rtt / 256 : | |
697 | (smoothed_rtt + 255) / 256; | |
698 | } else { | |
699 | /* We prefer to be conservative with new neighbours | |
700 | (higher RTT) */ | |
701 | assert(rtt <= 0x7FFFFFFF); | |
702 | neigh->rtt = 2*rtt; | |
703 | } | |
704 | changed = (neighbour_rttcost(neigh) == old_rttcost ? 0 : 1); | |
705 | update_neighbour_metric(neigh, changed); | |
706 | neigh->rtt_time = babel_now; | |
707 | } | |
708 | return; | |
709 | } | |
710 | ||
711 | /* Under normal circumstances, there are enough moderation mechanisms | |
712 | elsewhere in the protocol to make sure that this last-ditch check | |
713 | should never trigger. But I'm superstitious. */ | |
714 | ||
715 | static int | |
716 | check_bucket(struct interface *ifp) | |
717 | { | |
718 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
d11c6941 | 719 | if(babel_ifp->bucket == 0) { |
ca10883e DS |
720 | int seconds = babel_now.tv_sec - babel_ifp->bucket_time; |
721 | if(seconds > 0) { | |
722 | babel_ifp->bucket = MIN(BUCKET_TOKENS_MAX, | |
723 | seconds * BUCKET_TOKENS_PER_SEC); | |
724 | } | |
725 | /* Reset bucket time unconditionally, in case clock is stepped. */ | |
726 | babel_ifp->bucket_time = babel_now.tv_sec; | |
727 | } | |
728 | ||
729 | if(babel_ifp->bucket > 0) { | |
730 | babel_ifp->bucket--; | |
731 | return 1; | |
732 | } else { | |
733 | return 0; | |
734 | } | |
735 | } | |
736 | ||
737 | static int | |
738 | fill_rtt_message(struct interface *ifp) | |
739 | { | |
740 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
741 | if((babel_ifp->flags & BABEL_IF_TIMESTAMPS) && | |
742 | (babel_ifp->buffered_hello >= 0)) { | |
743 | if(babel_ifp->sendbuf[babel_ifp->buffered_hello + 8] == SUBTLV_PADN && | |
744 | babel_ifp->sendbuf[babel_ifp->buffered_hello + 9] == 4) { | |
745 | unsigned int time; | |
746 | /* Change the type of sub-TLV. */ | |
747 | babel_ifp->sendbuf[babel_ifp->buffered_hello + 8] = | |
748 | SUBTLV_TIMESTAMP; | |
749 | gettime(&babel_now); | |
750 | time = time_us(babel_now); | |
751 | DO_HTONL(babel_ifp->sendbuf + babel_ifp->buffered_hello + 10, time); | |
752 | return 1; | |
753 | } else { | |
3efd0893 | 754 | flog_err(EC_BABEL_PACKET, "No space left for timestamp sub-TLV (this shouldn't happen)"); |
ca10883e DS |
755 | return -1; |
756 | } | |
757 | } | |
758 | return 0; | |
759 | } | |
760 | ||
761 | void | |
762 | flushbuf(struct interface *ifp) | |
763 | { | |
764 | int rc; | |
765 | struct sockaddr_in6 sin6; | |
766 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
767 | ||
768 | assert(babel_ifp->buffered <= babel_ifp->bufsize); | |
769 | ||
770 | flushupdates(ifp); | |
771 | ||
772 | if(babel_ifp->buffered > 0) { | |
773 | debugf(BABEL_DEBUG_COMMON," (flushing %d buffered bytes on %s)", | |
774 | babel_ifp->buffered, ifp->name); | |
775 | if(check_bucket(ifp)) { | |
776 | memset(&sin6, 0, sizeof(sin6)); | |
777 | sin6.sin6_family = AF_INET6; | |
778 | memcpy(&sin6.sin6_addr, protocol_group, 16); | |
779 | sin6.sin6_port = htons(protocol_port); | |
780 | sin6.sin6_scope_id = ifp->ifindex; | |
781 | DO_HTONS(packet_header + 2, babel_ifp->buffered); | |
782 | fill_rtt_message(ifp); | |
783 | rc = babel_send(protocol_socket, | |
784 | packet_header, sizeof(packet_header), | |
785 | babel_ifp->sendbuf, babel_ifp->buffered, | |
786 | (struct sockaddr*)&sin6, sizeof(sin6)); | |
787 | if(rc < 0) | |
5b003f31 | 788 | flog_err(EC_BABEL_PACKET, "send: %s", safe_strerror(errno)); |
ca10883e | 789 | } else { |
5c997d29 DS |
790 | flog_err(EC_BABEL_PACKET, "Bucket full, dropping packet to %s.", |
791 | ifp->name); | |
792 | } | |
ca10883e DS |
793 | } |
794 | VALGRIND_MAKE_MEM_UNDEFINED(babel_ifp->sendbuf, babel_ifp->bufsize); | |
795 | babel_ifp->buffered = 0; | |
796 | babel_ifp->buffered_hello = -1; | |
797 | babel_ifp->have_buffered_id = 0; | |
798 | babel_ifp->have_buffered_nh = 0; | |
799 | babel_ifp->have_buffered_prefix = 0; | |
800 | babel_ifp->flush_timeout.tv_sec = 0; | |
801 | babel_ifp->flush_timeout.tv_usec = 0; | |
802 | } | |
803 | ||
804 | static void | |
805 | schedule_flush(struct interface *ifp) | |
806 | { | |
807 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
808 | unsigned msecs = jitter(babel_ifp, 0); | |
809 | if(babel_ifp->flush_timeout.tv_sec != 0 && | |
810 | timeval_minus_msec(&babel_ifp->flush_timeout, &babel_now) < msecs) | |
811 | return; | |
812 | set_timeout(&babel_ifp->flush_timeout, msecs); | |
813 | } | |
814 | ||
815 | static void | |
816 | schedule_flush_now(struct interface *ifp) | |
817 | { | |
818 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
819 | /* Almost now */ | |
820 | unsigned msecs = roughly(10); | |
821 | if(babel_ifp->flush_timeout.tv_sec != 0 && | |
822 | timeval_minus_msec(&babel_ifp->flush_timeout, &babel_now) < msecs) | |
823 | return; | |
824 | set_timeout(&babel_ifp->flush_timeout, msecs); | |
825 | } | |
826 | ||
827 | static void | |
828 | schedule_unicast_flush(unsigned msecs) | |
829 | { | |
830 | if(!unicast_neighbour) | |
831 | return; | |
832 | if(unicast_flush_timeout.tv_sec != 0 && | |
833 | timeval_minus_msec(&unicast_flush_timeout, &babel_now) < msecs) | |
834 | return; | |
835 | unicast_flush_timeout.tv_usec = (babel_now.tv_usec + msecs * 1000) %1000000; | |
836 | unicast_flush_timeout.tv_sec = | |
837 | babel_now.tv_sec + (babel_now.tv_usec / 1000 + msecs) / 1000; | |
838 | } | |
839 | ||
840 | static void | |
841 | ensure_space(struct interface *ifp, int space) | |
842 | { | |
843 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
844 | if(babel_ifp->bufsize - babel_ifp->buffered < space) | |
845 | flushbuf(ifp); | |
846 | } | |
847 | ||
848 | static void | |
849 | start_message(struct interface *ifp, int type, int len) | |
850 | { | |
851 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
852 | if(babel_ifp->bufsize - babel_ifp->buffered < len + 2) | |
853 | flushbuf(ifp); | |
854 | babel_ifp->sendbuf[babel_ifp->buffered++] = type; | |
855 | babel_ifp->sendbuf[babel_ifp->buffered++] = len; | |
856 | } | |
857 | ||
858 | static void | |
859 | end_message(struct interface *ifp, int type, int bytes) | |
860 | { | |
861 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
862 | assert(babel_ifp->buffered >= bytes + 2 && | |
863 | babel_ifp->sendbuf[babel_ifp->buffered - bytes - 2] == type && | |
864 | babel_ifp->sendbuf[babel_ifp->buffered - bytes - 1] == bytes); | |
865 | schedule_flush(ifp); | |
866 | } | |
867 | ||
868 | static void | |
869 | accumulate_byte(struct interface *ifp, unsigned char value) | |
870 | { | |
871 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
872 | babel_ifp->sendbuf[babel_ifp->buffered++] = value; | |
873 | } | |
874 | ||
875 | static void | |
876 | accumulate_short(struct interface *ifp, unsigned short value) | |
877 | { | |
878 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
879 | DO_HTONS(babel_ifp->sendbuf + babel_ifp->buffered, value); | |
880 | babel_ifp->buffered += 2; | |
881 | } | |
882 | ||
883 | static void | |
884 | accumulate_int(struct interface *ifp, unsigned int value) | |
885 | { | |
886 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
887 | DO_HTONL(babel_ifp->sendbuf + babel_ifp->buffered, value); | |
888 | babel_ifp->buffered += 4; | |
889 | } | |
890 | ||
891 | static void | |
892 | accumulate_bytes(struct interface *ifp, | |
893 | const unsigned char *value, unsigned len) | |
894 | { | |
895 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
896 | memcpy(babel_ifp->sendbuf + babel_ifp->buffered, value, len); | |
897 | babel_ifp->buffered += len; | |
898 | } | |
899 | ||
900 | static int | |
901 | start_unicast_message(struct neighbour *neigh, int type, int len) | |
902 | { | |
903 | if(unicast_neighbour) { | |
904 | if(neigh != unicast_neighbour || | |
905 | unicast_buffered + len + 2 >= | |
906 | MIN(UNICAST_BUFSIZE, babel_get_if_nfo(neigh->ifp)->bufsize)) | |
907 | flush_unicast(0); | |
908 | } | |
909 | if(!unicast_buffer) | |
910 | unicast_buffer = malloc(UNICAST_BUFSIZE); | |
911 | if(!unicast_buffer) { | |
5b003f31 | 912 | flog_err(EC_BABEL_MEMORY, "malloc(unicast_buffer): %s", |
e33b116c | 913 | safe_strerror(errno)); |
ca10883e DS |
914 | return -1; |
915 | } | |
916 | ||
917 | unicast_neighbour = neigh; | |
918 | ||
919 | unicast_buffer[unicast_buffered++] = type; | |
920 | unicast_buffer[unicast_buffered++] = len; | |
921 | return 1; | |
922 | } | |
923 | ||
924 | static void | |
925 | end_unicast_message(struct neighbour *neigh, int type, int bytes) | |
926 | { | |
927 | assert(unicast_neighbour == neigh && unicast_buffered >= bytes + 2 && | |
928 | unicast_buffer[unicast_buffered - bytes - 2] == type && | |
929 | unicast_buffer[unicast_buffered - bytes - 1] == bytes); | |
930 | schedule_unicast_flush(jitter(babel_get_if_nfo(neigh->ifp), 0)); | |
931 | } | |
932 | ||
933 | static void | |
934 | accumulate_unicast_byte(struct neighbour *neigh, unsigned char value) | |
935 | { | |
936 | unicast_buffer[unicast_buffered++] = value; | |
937 | } | |
938 | ||
939 | static void | |
940 | accumulate_unicast_short(struct neighbour *neigh, unsigned short value) | |
941 | { | |
942 | DO_HTONS(unicast_buffer + unicast_buffered, value); | |
943 | unicast_buffered += 2; | |
944 | } | |
945 | ||
946 | static void | |
947 | accumulate_unicast_int(struct neighbour *neigh, unsigned int value) | |
948 | { | |
949 | DO_HTONL(unicast_buffer + unicast_buffered, value); | |
950 | unicast_buffered += 4; | |
951 | } | |
952 | ||
953 | static void | |
954 | accumulate_unicast_bytes(struct neighbour *neigh, | |
955 | const unsigned char *value, unsigned len) | |
956 | { | |
957 | memcpy(unicast_buffer + unicast_buffered, value, len); | |
958 | unicast_buffered += len; | |
959 | } | |
960 | ||
961 | void | |
962 | send_ack(struct neighbour *neigh, unsigned short nonce, unsigned short interval) | |
963 | { | |
964 | int rc; | |
965 | debugf(BABEL_DEBUG_COMMON,"Sending ack (%04x) to %s on %s.", | |
966 | nonce, format_address(neigh->address), neigh->ifp->name); | |
967 | rc = start_unicast_message(neigh, MESSAGE_ACK, 2); if(rc < 0) return; | |
968 | accumulate_unicast_short(neigh, nonce); | |
969 | end_unicast_message(neigh, MESSAGE_ACK, 2); | |
970 | /* Roughly yields a value no larger than 3/2, so this meets the deadline */ | |
971 | schedule_unicast_flush(roughly(interval * 6)); | |
972 | } | |
973 | ||
974 | void | |
975 | send_hello_noupdate(struct interface *ifp, unsigned interval) | |
976 | { | |
977 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
978 | /* This avoids sending multiple hellos in a single packet, which breaks | |
979 | link quality estimation. */ | |
980 | if(babel_ifp->buffered_hello >= 0) | |
981 | flushbuf(ifp); | |
982 | ||
983 | babel_ifp->hello_seqno = seqno_plus(babel_ifp->hello_seqno, 1); | |
984 | set_timeout(&babel_ifp->hello_timeout, babel_ifp->hello_interval); | |
985 | ||
986 | if(!if_up(ifp)) | |
987 | return; | |
988 | ||
989 | debugf(BABEL_DEBUG_COMMON,"Sending hello %d (%d) to %s.", | |
990 | babel_ifp->hello_seqno, interval, ifp->name); | |
991 | ||
992 | start_message(ifp, MESSAGE_HELLO, | |
993 | (babel_ifp->flags & BABEL_IF_TIMESTAMPS) ? 12 : 6); | |
994 | babel_ifp->buffered_hello = babel_ifp->buffered - 2; | |
995 | accumulate_short(ifp, 0); | |
996 | accumulate_short(ifp, babel_ifp->hello_seqno); | |
997 | accumulate_short(ifp, interval > 0xFFFF ? 0xFFFF : interval); | |
998 | if(babel_ifp->flags & BABEL_IF_TIMESTAMPS) { | |
999 | /* Sub-TLV containing the local time of emission. We use a | |
1000 | Pad4 sub-TLV, which we'll fill just before sending. */ | |
1001 | accumulate_byte(ifp, SUBTLV_PADN); | |
1002 | accumulate_byte(ifp, 4); | |
1003 | accumulate_int(ifp, 0); | |
1004 | } | |
1005 | end_message(ifp, MESSAGE_HELLO, | |
1006 | (babel_ifp->flags & BABEL_IF_TIMESTAMPS) ? 12 : 6); | |
1007 | } | |
1008 | ||
1009 | void | |
1010 | send_hello(struct interface *ifp) | |
1011 | { | |
1012 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
1013 | send_hello_noupdate(ifp, (babel_ifp->hello_interval + 9) / 10); | |
1014 | /* Send full IHU every 3 hellos, and marginal IHU each time */ | |
1015 | if(babel_ifp->hello_seqno % 3 == 0) | |
1016 | send_ihu(NULL, ifp); | |
1017 | else | |
1018 | send_marginal_ihu(ifp); | |
1019 | } | |
1020 | ||
1021 | void | |
1022 | flush_unicast(int dofree) | |
1023 | { | |
1024 | struct sockaddr_in6 sin6; | |
1025 | int rc; | |
1026 | ||
1027 | if(unicast_buffered == 0) | |
1028 | goto done; | |
1029 | ||
1030 | if(!if_up(unicast_neighbour->ifp)) | |
1031 | goto done; | |
1032 | ||
1033 | /* Preserve ordering of messages */ | |
1034 | flushbuf(unicast_neighbour->ifp); | |
1035 | ||
1036 | if(check_bucket(unicast_neighbour->ifp)) { | |
1037 | memset(&sin6, 0, sizeof(sin6)); | |
1038 | sin6.sin6_family = AF_INET6; | |
1039 | memcpy(&sin6.sin6_addr, unicast_neighbour->address, 16); | |
1040 | sin6.sin6_port = htons(protocol_port); | |
1041 | sin6.sin6_scope_id = unicast_neighbour->ifp->ifindex; | |
1042 | DO_HTONS(packet_header + 2, unicast_buffered); | |
1043 | fill_rtt_message(unicast_neighbour->ifp); | |
1044 | rc = babel_send(protocol_socket, | |
1045 | packet_header, sizeof(packet_header), | |
1046 | unicast_buffer, unicast_buffered, | |
1047 | (struct sockaddr*)&sin6, sizeof(sin6)); | |
1048 | if(rc < 0) | |
5b003f31 | 1049 | flog_err(EC_BABEL_PACKET, "send(unicast): %s", |
e33b116c | 1050 | safe_strerror(errno)); |
ca10883e | 1051 | } else { |
5c997d29 DS |
1052 | flog_err(EC_BABEL_PACKET, |
1053 | "Bucket full, dropping unicast packet to %s if %s.", | |
1054 | format_address(unicast_neighbour->address), | |
1055 | unicast_neighbour->ifp->name); | |
ca10883e DS |
1056 | } |
1057 | ||
1058 | done: | |
1059 | VALGRIND_MAKE_MEM_UNDEFINED(unicast_buffer, UNICAST_BUFSIZE); | |
1060 | unicast_buffered = 0; | |
1061 | if(dofree && unicast_buffer) { | |
1062 | free(unicast_buffer); | |
1063 | unicast_buffer = NULL; | |
1064 | } | |
1065 | unicast_neighbour = NULL; | |
1066 | unicast_flush_timeout.tv_sec = 0; | |
1067 | unicast_flush_timeout.tv_usec = 0; | |
1068 | } | |
1069 | ||
1070 | static void | |
1071 | really_send_update(struct interface *ifp, | |
1072 | const unsigned char *id, | |
1073 | const unsigned char *prefix, unsigned char plen, | |
1074 | unsigned short seqno, unsigned short metric, | |
1075 | unsigned char *channels, int channels_len) | |
1076 | { | |
1077 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
1078 | int add_metric, v4, real_plen, omit = 0; | |
1079 | const unsigned char *real_prefix; | |
1080 | unsigned short flags = 0; | |
1081 | int channels_size; | |
1082 | ||
1083 | if(diversity_kind != DIVERSITY_CHANNEL) | |
1084 | channels_len = -1; | |
1085 | ||
1086 | channels_size = channels_len >= 0 ? channels_len + 2 : 0; | |
1087 | ||
1088 | if(!if_up(ifp)) | |
1089 | return; | |
1090 | ||
1091 | add_metric = output_filter(id, prefix, plen, ifp->ifindex); | |
1092 | if(add_metric >= INFINITY) | |
1093 | return; | |
1094 | ||
1095 | metric = MIN(metric + add_metric, INFINITY); | |
1096 | /* Worst case */ | |
1097 | ensure_space(ifp, 20 + 12 + 28); | |
1098 | ||
1099 | v4 = plen >= 96 && v4mapped(prefix); | |
1100 | ||
1101 | if(v4) { | |
1102 | if(!babel_ifp->ipv4) | |
1103 | return; | |
1104 | if(!babel_ifp->have_buffered_nh || | |
1105 | memcmp(babel_ifp->buffered_nh, babel_ifp->ipv4, 4) != 0) { | |
1106 | start_message(ifp, MESSAGE_NH, 6); | |
1107 | accumulate_byte(ifp, 1); | |
1108 | accumulate_byte(ifp, 0); | |
1109 | accumulate_bytes(ifp, babel_ifp->ipv4, 4); | |
1110 | end_message(ifp, MESSAGE_NH, 6); | |
1111 | memcpy(babel_ifp->buffered_nh, babel_ifp->ipv4, 4); | |
1112 | babel_ifp->have_buffered_nh = 1; | |
1113 | } | |
1114 | ||
1115 | real_prefix = prefix + 12; | |
1116 | real_plen = plen - 96; | |
1117 | } else { | |
1118 | if(babel_ifp->have_buffered_prefix) { | |
1119 | while(omit < plen / 8 && | |
1120 | babel_ifp->buffered_prefix[omit] == prefix[omit]) | |
1121 | omit++; | |
1122 | } | |
1123 | if(!babel_ifp->have_buffered_prefix || plen >= 48) | |
1124 | flags |= 0x80; | |
1125 | real_prefix = prefix; | |
1126 | real_plen = plen; | |
1127 | } | |
1128 | ||
1129 | if(!babel_ifp->have_buffered_id | |
1130 | || memcmp(id, babel_ifp->buffered_id, 8) != 0) { | |
1131 | if(real_plen == 128 && memcmp(real_prefix + 8, id, 8) == 0) { | |
1132 | flags |= 0x40; | |
1133 | } else { | |
1134 | start_message(ifp, MESSAGE_ROUTER_ID, 10); | |
1135 | accumulate_short(ifp, 0); | |
1136 | accumulate_bytes(ifp, id, 8); | |
1137 | end_message(ifp, MESSAGE_ROUTER_ID, 10); | |
1138 | } | |
01b08f09 | 1139 | memcpy(babel_ifp->buffered_id, id, sizeof(babel_ifp->buffered_id)); |
ca10883e DS |
1140 | babel_ifp->have_buffered_id = 1; |
1141 | } | |
1142 | ||
1143 | start_message(ifp, MESSAGE_UPDATE, 10 + (real_plen + 7) / 8 - omit + | |
1144 | channels_size); | |
1145 | accumulate_byte(ifp, v4 ? 1 : 2); | |
1146 | accumulate_byte(ifp, flags); | |
1147 | accumulate_byte(ifp, real_plen); | |
1148 | accumulate_byte(ifp, omit); | |
1149 | accumulate_short(ifp, (babel_ifp->update_interval + 5) / 10); | |
1150 | accumulate_short(ifp, seqno); | |
1151 | accumulate_short(ifp, metric); | |
1152 | accumulate_bytes(ifp, real_prefix + omit, (real_plen + 7) / 8 - omit); | |
1153 | /* Note that an empty channels TLV is different from no such TLV. */ | |
1154 | if(channels_len >= 0) { | |
1155 | accumulate_byte(ifp, 2); | |
1156 | accumulate_byte(ifp, channels_len); | |
fa3bf3a2 MS |
1157 | |
1158 | if (channels && channels_len > 0) | |
1159 | accumulate_bytes(ifp, channels, channels_len); | |
ca10883e DS |
1160 | } |
1161 | end_message(ifp, MESSAGE_UPDATE, 10 + (real_plen + 7) / 8 - omit + | |
1162 | channels_size); | |
1163 | ||
1164 | if(flags & 0x80) { | |
1165 | memcpy(babel_ifp->buffered_prefix, prefix, 16); | |
1166 | babel_ifp->have_buffered_prefix = 1; | |
1167 | } | |
1168 | } | |
1169 | ||
1170 | static int | |
1171 | compare_buffered_updates(const void *av, const void *bv) | |
1172 | { | |
1173 | const struct buffered_update *a = av, *b = bv; | |
1174 | int rc, v4a, v4b, ma, mb; | |
1175 | ||
1176 | rc = memcmp(a->id, b->id, 8); | |
1177 | if(rc != 0) | |
1178 | return rc; | |
1179 | ||
1180 | v4a = (a->plen >= 96 && v4mapped(a->prefix)); | |
1181 | v4b = (b->plen >= 96 && v4mapped(b->prefix)); | |
1182 | ||
1183 | if(v4a > v4b) | |
1184 | return 1; | |
1185 | else if(v4a < v4b) | |
1186 | return -1; | |
1187 | ||
1188 | ma = (!v4a && a->plen == 128 && memcmp(a->prefix + 8, a->id, 8) == 0); | |
1189 | mb = (!v4b && b->plen == 128 && memcmp(b->prefix + 8, b->id, 8) == 0); | |
1190 | ||
1191 | if(ma > mb) | |
1192 | return -1; | |
1193 | else if(mb > ma) | |
1194 | return 1; | |
1195 | ||
1196 | if(a->plen < b->plen) | |
1197 | return 1; | |
1198 | else if(a->plen > b->plen) | |
1199 | return -1; | |
1200 | ||
1201 | return memcmp(a->prefix, b->prefix, 16); | |
1202 | } | |
1203 | ||
1204 | void | |
1205 | flushupdates(struct interface *ifp) | |
1206 | { | |
1207 | babel_interface_nfo *babel_ifp = NULL; | |
1208 | struct xroute *xroute; | |
1209 | struct babel_route *route; | |
1210 | const unsigned char *last_prefix = NULL; | |
1211 | unsigned char last_plen = 0xFF; | |
1212 | int i; | |
1213 | ||
1214 | if(ifp == NULL) { | |
f4e14fdb RW |
1215 | struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT); |
1216 | struct interface *ifp_aux; | |
1217 | FOR_ALL_INTERFACES(vrf, ifp_aux) | |
ca10883e DS |
1218 | flushupdates(ifp_aux); |
1219 | return; | |
1220 | } | |
1221 | ||
1222 | babel_ifp = babel_get_if_nfo(ifp); | |
1223 | if(babel_ifp->num_buffered_updates > 0) { | |
1224 | struct buffered_update *b = babel_ifp->buffered_updates; | |
1225 | int n = babel_ifp->num_buffered_updates; | |
1226 | ||
1227 | babel_ifp->buffered_updates = NULL; | |
1228 | babel_ifp->update_bufsize = 0; | |
1229 | babel_ifp->num_buffered_updates = 0; | |
1230 | ||
1231 | if(!if_up(ifp)) | |
1232 | goto done; | |
1233 | ||
1234 | debugf(BABEL_DEBUG_COMMON," (flushing %d buffered updates on %s (%d))", | |
1235 | n, ifp->name, ifp->ifindex); | |
1236 | ||
1237 | /* In order to send fewer update messages, we want to send updates | |
1238 | with the same router-id together, with IPv6 going out before IPv4. */ | |
1239 | ||
1240 | for(i = 0; i < n; i++) { | |
1241 | route = find_installed_route(b[i].prefix, b[i].plen); | |
1242 | if(route) | |
1243 | memcpy(b[i].id, route->src->id, 8); | |
1244 | else | |
1245 | memcpy(b[i].id, myid, 8); | |
1246 | } | |
1247 | ||
1248 | qsort(b, n, sizeof(struct buffered_update), compare_buffered_updates); | |
1249 | ||
1250 | for(i = 0; i < n; i++) { | |
1251 | /* The same update may be scheduled multiple times before it is | |
1252 | sent out. Since our buffer is now sorted, it is enough to | |
1253 | compare with the previous update. */ | |
1254 | ||
1255 | if(last_prefix) { | |
1256 | if(b[i].plen == last_plen && | |
1257 | memcmp(b[i].prefix, last_prefix, 16) == 0) | |
1258 | continue; | |
1259 | } | |
1260 | ||
1261 | xroute = find_xroute(b[i].prefix, b[i].plen); | |
1262 | route = find_installed_route(b[i].prefix, b[i].plen); | |
1263 | ||
1264 | if(xroute && (!route || xroute->metric <= kernel_metric)) { | |
1265 | really_send_update(ifp, myid, | |
1266 | xroute->prefix, xroute->plen, | |
1267 | myseqno, xroute->metric, | |
1268 | NULL, 0); | |
1269 | last_prefix = xroute->prefix; | |
1270 | last_plen = xroute->plen; | |
1271 | } else if(route) { | |
1272 | unsigned char channels[DIVERSITY_HOPS]; | |
1273 | int chlen; | |
1274 | struct interface *route_ifp = route->neigh->ifp; | |
1275 | struct babel_interface *babel_route_ifp = NULL; | |
1276 | unsigned short metric; | |
1277 | unsigned short seqno; | |
1278 | ||
1279 | seqno = route->seqno; | |
1280 | metric = | |
1281 | route_interferes(route, ifp) ? | |
1282 | route_metric(route) : | |
1283 | route_metric_noninterfering(route); | |
1284 | ||
1285 | if(metric < INFINITY) | |
1286 | satisfy_request(route->src->prefix, route->src->plen, | |
1287 | seqno, route->src->id, ifp); | |
1288 | if((babel_ifp->flags & BABEL_IF_SPLIT_HORIZON) && | |
1289 | route->neigh->ifp == ifp) | |
1290 | continue; | |
1291 | ||
1292 | babel_route_ifp = babel_get_if_nfo(route_ifp); | |
1293 | if(babel_route_ifp->channel ==BABEL_IF_CHANNEL_NONINTERFERING) { | |
1294 | memcpy(channels, route->channels, DIVERSITY_HOPS); | |
1295 | } else { | |
1296 | if(babel_route_ifp->channel == BABEL_IF_CHANNEL_UNKNOWN) | |
1297 | channels[0] = BABEL_IF_CHANNEL_INTERFERING; | |
1298 | else { | |
1299 | assert(babel_route_ifp->channel > 0 && | |
1300 | babel_route_ifp->channel <= 255); | |
1301 | channels[0] = babel_route_ifp->channel; | |
1302 | } | |
1303 | memcpy(channels + 1, route->channels, DIVERSITY_HOPS - 1); | |
1304 | } | |
1305 | ||
1306 | chlen = channels_len(channels); | |
1307 | really_send_update(ifp, route->src->id, | |
1308 | route->src->prefix, | |
1309 | route->src->plen, | |
1310 | seqno, metric, | |
1311 | channels, chlen); | |
1312 | update_source(route->src, seqno, metric); | |
1313 | last_prefix = route->src->prefix; | |
1314 | last_plen = route->src->plen; | |
1315 | } else { | |
1316 | /* There's no route for this prefix. This can happen shortly | |
1317 | after an xroute has been retracted, so send a retraction. */ | |
1318 | really_send_update(ifp, myid, b[i].prefix, b[i].plen, | |
1319 | myseqno, INFINITY, NULL, -1); | |
1320 | } | |
1321 | } | |
1322 | schedule_flush_now(ifp); | |
1323 | done: | |
1324 | free(b); | |
1325 | } | |
1326 | babel_ifp->update_flush_timeout.tv_sec = 0; | |
1327 | babel_ifp->update_flush_timeout.tv_usec = 0; | |
1328 | } | |
1329 | ||
1330 | static void | |
1331 | schedule_update_flush(struct interface *ifp, int urgent) | |
1332 | { | |
1333 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
1334 | unsigned msecs; | |
1335 | msecs = update_jitter(babel_ifp, urgent); | |
1336 | if(babel_ifp->update_flush_timeout.tv_sec != 0 && | |
1337 | timeval_minus_msec(&babel_ifp->update_flush_timeout, &babel_now) < msecs) | |
1338 | return; | |
1339 | set_timeout(&babel_ifp->update_flush_timeout, msecs); | |
1340 | } | |
1341 | ||
1342 | static void | |
1343 | buffer_update(struct interface *ifp, | |
1344 | const unsigned char *prefix, unsigned char plen) | |
1345 | { | |
1346 | babel_interface_nfo *babel_ifp = babel_get_if_nfo(ifp); | |
1347 | if(babel_ifp->num_buffered_updates > 0 && | |
1348 | babel_ifp->num_buffered_updates >= babel_ifp->update_bufsize) | |
1349 | flushupdates(ifp); | |
1350 | ||
1351 | if(babel_ifp->update_bufsize == 0) { | |
1352 | int n; | |
1353 | assert(babel_ifp->buffered_updates == NULL); | |
1354 | /* Allocate enough space to hold a full update. Since the | |
1355 | number of installed routes will grow over time, make sure we | |
1356 | have enough space to send a full-ish frame. */ | |
1357 | n = installed_routes_estimate() + xroutes_estimate() + 4; | |
1358 | n = MAX(n, babel_ifp->bufsize / 16); | |
1359 | again: | |
1360 | babel_ifp->buffered_updates = malloc(n *sizeof(struct buffered_update)); | |
1361 | if(babel_ifp->buffered_updates == NULL) { | |
5b003f31 | 1362 | flog_err(EC_BABEL_MEMORY, "malloc(buffered_updates): %s", |
e33b116c | 1363 | safe_strerror(errno)); |
ca10883e DS |
1364 | if(n > 4) { |
1365 | /* Try again with a tiny buffer. */ | |
1366 | n = 4; | |
1367 | goto again; | |
1368 | } | |
1369 | return; | |
1370 | } | |
1371 | babel_ifp->update_bufsize = n; | |
1372 | babel_ifp->num_buffered_updates = 0; | |
1373 | } | |
1374 | ||
1375 | memcpy(babel_ifp->buffered_updates[babel_ifp->num_buffered_updates].prefix, | |
1376 | prefix, 16); | |
1377 | babel_ifp->buffered_updates[babel_ifp->num_buffered_updates].plen = plen; | |
1378 | babel_ifp->num_buffered_updates++; | |
1379 | } | |
1380 | ||
1381 | void | |
1382 | send_update(struct interface *ifp, int urgent, | |
1383 | const unsigned char *prefix, unsigned char plen) | |
1384 | { | |
1385 | babel_interface_nfo *babel_ifp = NULL; | |
1386 | ||
1387 | if(ifp == NULL) { | |
f4e14fdb RW |
1388 | struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT); |
1389 | struct interface *ifp_aux; | |
ca10883e | 1390 | struct babel_route *route; |
f4e14fdb | 1391 | FOR_ALL_INTERFACES(vrf, ifp_aux) |
ca10883e DS |
1392 | send_update(ifp_aux, urgent, prefix, plen); |
1393 | if(prefix) { | |
1394 | /* Since flushupdates only deals with non-wildcard interfaces, we | |
1395 | need to do this now. */ | |
1396 | route = find_installed_route(prefix, plen); | |
1397 | if(route && route_metric(route) < INFINITY) | |
1398 | satisfy_request(prefix, plen, route->src->seqno, route->src->id, | |
1399 | NULL); | |
1400 | } | |
1401 | return; | |
1402 | } | |
1403 | ||
1404 | if(!if_up(ifp)) | |
1405 | return; | |
1406 | ||
1407 | babel_ifp = babel_get_if_nfo(ifp); | |
1408 | if(prefix) { | |
1409 | debugf(BABEL_DEBUG_COMMON,"Sending update to %s for %s.", | |
1410 | ifp->name, format_prefix(prefix, plen)); | |
1411 | buffer_update(ifp, prefix, plen); | |
1412 | } else { | |
1413 | struct route_stream *routes = NULL; | |
1414 | send_self_update(ifp); | |
1415 | debugf(BABEL_DEBUG_COMMON,"Sending update to %s for any.", ifp->name); | |
1416 | routes = route_stream(1); | |
1417 | if(routes) { | |
1418 | while(1) { | |
1419 | struct babel_route *route = route_stream_next(routes); | |
1420 | if(route == NULL) | |
1421 | break; | |
1422 | buffer_update(ifp, route->src->prefix, route->src->plen); | |
1423 | } | |
1424 | route_stream_done(routes); | |
1425 | } else { | |
5b003f31 | 1426 | flog_err(EC_BABEL_MEMORY, "Couldn't allocate route stream."); |
ca10883e DS |
1427 | } |
1428 | set_timeout(&babel_ifp->update_timeout, babel_ifp->update_interval); | |
1429 | babel_ifp->last_update_time = babel_now.tv_sec; | |
1430 | } | |
1431 | schedule_update_flush(ifp, urgent); | |
1432 | } | |
1433 | ||
1434 | void | |
1435 | send_update_resend(struct interface *ifp, | |
1436 | const unsigned char *prefix, unsigned char plen) | |
1437 | { | |
1438 | assert(prefix != NULL); | |
1439 | ||
1440 | send_update(ifp, 1, prefix, plen); | |
1441 | record_resend(RESEND_UPDATE, prefix, plen, 0, NULL, NULL, resend_delay); | |
1442 | } | |
1443 | ||
1444 | void | |
1445 | send_wildcard_retraction(struct interface *ifp) | |
1446 | { | |
1447 | babel_interface_nfo *babel_ifp = NULL; | |
1448 | if(ifp == NULL) { | |
f4e14fdb RW |
1449 | struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT); |
1450 | struct interface *ifp_aux; | |
1451 | FOR_ALL_INTERFACES(vrf, ifp_aux) | |
ca10883e DS |
1452 | send_wildcard_retraction(ifp_aux); |
1453 | return; | |
1454 | } | |
1455 | ||
1456 | if(!if_up(ifp)) | |
1457 | return; | |
1458 | ||
1459 | babel_ifp = babel_get_if_nfo(ifp); | |
1460 | start_message(ifp, MESSAGE_UPDATE, 10); | |
1461 | accumulate_byte(ifp, 0); | |
1462 | accumulate_byte(ifp, 0x40); | |
1463 | accumulate_byte(ifp, 0); | |
1464 | accumulate_byte(ifp, 0); | |
1465 | accumulate_short(ifp, 0xFFFF); | |
1466 | accumulate_short(ifp, myseqno); | |
1467 | accumulate_short(ifp, 0xFFFF); | |
1468 | end_message(ifp, MESSAGE_UPDATE, 10); | |
1469 | ||
1470 | babel_ifp->have_buffered_id = 0; | |
1471 | } | |
1472 | ||
1473 | void | |
4d762f26 | 1474 | update_myseqno(void) |
ca10883e DS |
1475 | { |
1476 | myseqno = seqno_plus(myseqno, 1); | |
1477 | } | |
1478 | ||
1479 | void | |
1480 | send_self_update(struct interface *ifp) | |
1481 | { | |
1482 | struct xroute_stream *xroutes; | |
1483 | if(ifp == NULL) { | |
f4e14fdb RW |
1484 | struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT); |
1485 | struct interface *ifp_aux; | |
1486 | FOR_ALL_INTERFACES(vrf, ifp_aux) { | |
ca10883e DS |
1487 | if(!if_up(ifp_aux)) |
1488 | continue; | |
1489 | send_self_update(ifp_aux); | |
1490 | } | |
1491 | return; | |
1492 | } | |
1493 | ||
1494 | debugf(BABEL_DEBUG_COMMON,"Sending self update to %s.", ifp->name); | |
1495 | xroutes = xroute_stream(); | |
1496 | if(xroutes) { | |
1497 | while(1) { | |
1498 | struct xroute *xroute = xroute_stream_next(xroutes); | |
1499 | if(xroute == NULL) break; | |
1500 | send_update(ifp, 0, xroute->prefix, xroute->plen); | |
1501 | } | |
1502 | xroute_stream_done(xroutes); | |
1503 | } else { | |
5b003f31 | 1504 | flog_err(EC_BABEL_MEMORY, "Couldn't allocate xroute stream."); |
ca10883e DS |
1505 | } |
1506 | } | |
1507 | ||
1508 | void | |
1509 | send_ihu(struct neighbour *neigh, struct interface *ifp) | |
1510 | { | |
1511 | babel_interface_nfo *babel_ifp = NULL; | |
1512 | int rxcost, interval; | |
1513 | int ll; | |
1514 | int send_rtt_data; | |
1515 | int msglen; | |
1516 | ||
1517 | if(neigh == NULL && ifp == NULL) { | |
f4e14fdb | 1518 | struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT); |
ca10883e | 1519 | struct interface *ifp_aux; |
f4e14fdb | 1520 | FOR_ALL_INTERFACES(vrf, ifp_aux) { |
ca10883e DS |
1521 | if(if_up(ifp_aux)) |
1522 | continue; | |
1523 | send_ihu(NULL, ifp_aux); | |
1524 | } | |
1525 | return; | |
1526 | } | |
1527 | ||
1528 | if(neigh == NULL) { | |
1529 | struct neighbour *ngh; | |
1530 | FOR_ALL_NEIGHBOURS(ngh) { | |
1531 | if(ngh->ifp == ifp) | |
1532 | send_ihu(ngh, ifp); | |
1533 | } | |
1534 | return; | |
1535 | } | |
1536 | ||
1537 | ||
1538 | if(ifp && neigh->ifp != ifp) | |
1539 | return; | |
1540 | ||
1541 | ifp = neigh->ifp; | |
1542 | babel_ifp = babel_get_if_nfo(ifp); | |
1543 | if(!if_up(ifp)) | |
1544 | return; | |
1545 | ||
1546 | rxcost = neighbour_rxcost(neigh); | |
1547 | interval = (babel_ifp->hello_interval * 3 + 9) / 10; | |
1548 | ||
1549 | /* Conceptually, an IHU is a unicast message. We usually send them as | |
1550 | multicast, since this allows aggregation into a single packet and | |
1551 | avoids an ARP exchange. If we already have a unicast message queued | |
1552 | for this neighbour, however, we might as well piggyback the IHU. */ | |
1553 | debugf(BABEL_DEBUG_COMMON,"Sending %sihu %d on %s to %s.", | |
1554 | unicast_neighbour == neigh ? "unicast " : "", | |
1555 | rxcost, | |
1556 | neigh->ifp->name, | |
1557 | format_address(neigh->address)); | |
1558 | ||
1559 | ll = linklocal(neigh->address); | |
1560 | ||
1561 | if((babel_ifp->flags & BABEL_IF_TIMESTAMPS) && neigh->hello_send_us | |
1562 | /* Checks whether the RTT data is not too old to be sent. */ | |
1563 | && timeval_minus_msec(&babel_now, | |
1564 | &neigh->hello_rtt_receive_time) < 1000000) { | |
1565 | send_rtt_data = 1; | |
1566 | } else { | |
1567 | neigh->hello_send_us = 0; | |
1568 | send_rtt_data = 0; | |
1569 | } | |
1570 | ||
1571 | /* The length depends on the format of the address, and then an | |
1572 | optional 10-bytes sub-TLV for timestamps (used to compute a RTT). */ | |
1573 | msglen = (ll ? 14 : 22) + (send_rtt_data ? 10 : 0); | |
1574 | ||
1575 | if(unicast_neighbour != neigh) { | |
1576 | start_message(ifp, MESSAGE_IHU, msglen); | |
1577 | accumulate_byte(ifp, ll ? 3 : 2); | |
1578 | accumulate_byte(ifp, 0); | |
1579 | accumulate_short(ifp, rxcost); | |
1580 | accumulate_short(ifp, interval); | |
1581 | if(ll) | |
1582 | accumulate_bytes(ifp, neigh->address + 8, 8); | |
1583 | else | |
1584 | accumulate_bytes(ifp, neigh->address, 16); | |
1585 | if (send_rtt_data) { | |
1586 | accumulate_byte(ifp, SUBTLV_TIMESTAMP); | |
1587 | accumulate_byte(ifp, 8); | |
1588 | accumulate_int(ifp, neigh->hello_send_us); | |
1589 | accumulate_int(ifp, time_us(neigh->hello_rtt_receive_time)); | |
1590 | } | |
1591 | end_message(ifp, MESSAGE_IHU, msglen); | |
1592 | } else { | |
1593 | int rc; | |
1594 | rc = start_unicast_message(neigh, MESSAGE_IHU, msglen); | |
1595 | if(rc < 0) return; | |
1596 | accumulate_unicast_byte(neigh, ll ? 3 : 2); | |
1597 | accumulate_unicast_byte(neigh, 0); | |
1598 | accumulate_unicast_short(neigh, rxcost); | |
1599 | accumulate_unicast_short(neigh, interval); | |
1600 | if(ll) | |
1601 | accumulate_unicast_bytes(neigh, neigh->address + 8, 8); | |
1602 | else | |
1603 | accumulate_unicast_bytes(neigh, neigh->address, 16); | |
1604 | if (send_rtt_data) { | |
1605 | accumulate_unicast_byte(neigh, SUBTLV_TIMESTAMP); | |
1606 | accumulate_unicast_byte(neigh, 8); | |
1607 | accumulate_unicast_int(neigh, neigh->hello_send_us); | |
1608 | accumulate_unicast_int(neigh, | |
1609 | time_us(neigh->hello_rtt_receive_time)); | |
1610 | } | |
1611 | end_unicast_message(neigh, MESSAGE_IHU, msglen); | |
1612 | } | |
1613 | } | |
1614 | ||
1615 | /* Send IHUs to all marginal neighbours */ | |
1616 | void | |
1617 | send_marginal_ihu(struct interface *ifp) | |
1618 | { | |
1619 | struct neighbour *neigh; | |
1620 | FOR_ALL_NEIGHBOURS(neigh) { | |
1621 | if(ifp && neigh->ifp != ifp) | |
1622 | continue; | |
1623 | if(neigh->txcost >= 384 || (neigh->reach & 0xF000) != 0xF000) | |
1624 | send_ihu(neigh, ifp); | |
1625 | } | |
1626 | } | |
1627 | ||
1628 | void | |
1629 | send_request(struct interface *ifp, | |
1630 | const unsigned char *prefix, unsigned char plen) | |
1631 | { | |
1632 | int v4, pb, len; | |
1633 | ||
1634 | if(ifp == NULL) { | |
f4e14fdb RW |
1635 | struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT); |
1636 | struct interface *ifp_aux; | |
1637 | FOR_ALL_INTERFACES(vrf, ifp_aux) { | |
ca10883e DS |
1638 | if(if_up(ifp_aux)) |
1639 | continue; | |
1640 | send_request(ifp_aux, prefix, plen); | |
1641 | } | |
1642 | return; | |
1643 | } | |
1644 | ||
1645 | /* make sure any buffered updates go out before this request. */ | |
1646 | flushupdates(ifp); | |
1647 | ||
1648 | if(!if_up(ifp)) | |
1649 | return; | |
1650 | ||
1651 | debugf(BABEL_DEBUG_COMMON,"sending request to %s for %s.", | |
1652 | ifp->name, prefix ? format_prefix(prefix, plen) : "any"); | |
1653 | v4 = plen >= 96 && v4mapped(prefix); | |
1654 | pb = v4 ? ((plen - 96) + 7) / 8 : (plen + 7) / 8; | |
1655 | len = !prefix ? 2 : 2 + pb; | |
1656 | ||
1657 | start_message(ifp, MESSAGE_REQUEST, len); | |
1658 | accumulate_byte(ifp, !prefix ? 0 : v4 ? 1 : 2); | |
1659 | accumulate_byte(ifp, !prefix ? 0 : v4 ? plen - 96 : plen); | |
1660 | if(prefix) { | |
1661 | if(v4) | |
1662 | accumulate_bytes(ifp, prefix + 12, pb); | |
1663 | else | |
1664 | accumulate_bytes(ifp, prefix, pb); | |
1665 | } | |
1666 | end_message(ifp, MESSAGE_REQUEST, len); | |
1667 | } | |
1668 | ||
1669 | void | |
1670 | send_unicast_request(struct neighbour *neigh, | |
1671 | const unsigned char *prefix, unsigned char plen) | |
1672 | { | |
1673 | int rc, v4, pb, len; | |
1674 | ||
1675 | /* make sure any buffered updates go out before this request. */ | |
1676 | flushupdates(neigh->ifp); | |
1677 | ||
1678 | debugf(BABEL_DEBUG_COMMON,"sending unicast request to %s for %s.", | |
1679 | format_address(neigh->address), | |
1680 | prefix ? format_prefix(prefix, plen) : "any"); | |
1681 | v4 = plen >= 96 && v4mapped(prefix); | |
1682 | pb = v4 ? ((plen - 96) + 7) / 8 : (plen + 7) / 8; | |
1683 | len = !prefix ? 2 : 2 + pb; | |
1684 | ||
1685 | rc = start_unicast_message(neigh, MESSAGE_REQUEST, len); | |
1686 | if(rc < 0) return; | |
1687 | accumulate_unicast_byte(neigh, !prefix ? 0 : v4 ? 1 : 2); | |
1688 | accumulate_unicast_byte(neigh, !prefix ? 0 : v4 ? plen - 96 : plen); | |
1689 | if(prefix) { | |
1690 | if(v4) | |
1691 | accumulate_unicast_bytes(neigh, prefix + 12, pb); | |
1692 | else | |
1693 | accumulate_unicast_bytes(neigh, prefix, pb); | |
1694 | } | |
1695 | end_unicast_message(neigh, MESSAGE_REQUEST, len); | |
1696 | } | |
1697 | ||
1698 | void | |
1699 | send_multihop_request(struct interface *ifp, | |
1700 | const unsigned char *prefix, unsigned char plen, | |
1701 | unsigned short seqno, const unsigned char *id, | |
1702 | unsigned short hop_count) | |
1703 | { | |
1704 | int v4, pb, len; | |
1705 | ||
1706 | /* Make sure any buffered updates go out before this request. */ | |
1707 | flushupdates(ifp); | |
1708 | ||
1709 | if(ifp == NULL) { | |
f4e14fdb RW |
1710 | struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT); |
1711 | struct interface *ifp_aux; | |
1712 | FOR_ALL_INTERFACES(vrf, ifp_aux) { | |
ca10883e DS |
1713 | if(!if_up(ifp_aux)) |
1714 | continue; | |
1715 | send_multihop_request(ifp_aux, prefix, plen, seqno, id, hop_count); | |
1716 | } | |
1717 | return; | |
1718 | } | |
1719 | ||
1720 | if(!if_up(ifp)) | |
1721 | return; | |
1722 | ||
1723 | debugf(BABEL_DEBUG_COMMON,"Sending request (%d) on %s for %s.", | |
1724 | hop_count, ifp->name, format_prefix(prefix, plen)); | |
1725 | v4 = plen >= 96 && v4mapped(prefix); | |
1726 | pb = v4 ? ((plen - 96) + 7) / 8 : (plen + 7) / 8; | |
1727 | len = 6 + 8 + pb; | |
1728 | ||
1729 | start_message(ifp, MESSAGE_MH_REQUEST, len); | |
1730 | accumulate_byte(ifp, v4 ? 1 : 2); | |
1731 | accumulate_byte(ifp, v4 ? plen - 96 : plen); | |
1732 | accumulate_short(ifp, seqno); | |
1733 | accumulate_byte(ifp, hop_count); | |
1734 | accumulate_byte(ifp, 0); | |
1735 | accumulate_bytes(ifp, id, 8); | |
1736 | if(prefix) { | |
1737 | if(v4) | |
1738 | accumulate_bytes(ifp, prefix + 12, pb); | |
1739 | else | |
1740 | accumulate_bytes(ifp, prefix, pb); | |
1741 | } | |
1742 | end_message(ifp, MESSAGE_MH_REQUEST, len); | |
1743 | } | |
1744 | ||
1745 | void | |
1746 | send_unicast_multihop_request(struct neighbour *neigh, | |
1747 | const unsigned char *prefix, unsigned char plen, | |
1748 | unsigned short seqno, const unsigned char *id, | |
1749 | unsigned short hop_count) | |
1750 | { | |
1751 | int rc, v4, pb, len; | |
1752 | ||
1753 | /* Make sure any buffered updates go out before this request. */ | |
1754 | flushupdates(neigh->ifp); | |
1755 | ||
1756 | debugf(BABEL_DEBUG_COMMON,"Sending multi-hop request to %s for %s (%d hops).", | |
1757 | format_address(neigh->address), | |
1758 | format_prefix(prefix, plen), hop_count); | |
1759 | v4 = plen >= 96 && v4mapped(prefix); | |
1760 | pb = v4 ? ((plen - 96) + 7) / 8 : (plen + 7) / 8; | |
1761 | len = 6 + 8 + pb; | |
1762 | ||
1763 | rc = start_unicast_message(neigh, MESSAGE_MH_REQUEST, len); | |
1764 | if(rc < 0) return; | |
1765 | accumulate_unicast_byte(neigh, v4 ? 1 : 2); | |
1766 | accumulate_unicast_byte(neigh, v4 ? plen - 96 : plen); | |
1767 | accumulate_unicast_short(neigh, seqno); | |
1768 | accumulate_unicast_byte(neigh, hop_count); | |
1769 | accumulate_unicast_byte(neigh, 0); | |
1770 | accumulate_unicast_bytes(neigh, id, 8); | |
1771 | if(prefix) { | |
1772 | if(v4) | |
1773 | accumulate_unicast_bytes(neigh, prefix + 12, pb); | |
1774 | else | |
1775 | accumulate_unicast_bytes(neigh, prefix, pb); | |
1776 | } | |
1777 | end_unicast_message(neigh, MESSAGE_MH_REQUEST, len); | |
1778 | } | |
1779 | ||
1780 | void | |
1781 | send_request_resend(struct neighbour *neigh, | |
1782 | const unsigned char *prefix, unsigned char plen, | |
1783 | unsigned short seqno, unsigned char *id) | |
1784 | { | |
1785 | if(neigh) | |
1786 | send_unicast_multihop_request(neigh, prefix, plen, seqno, id, 127); | |
1787 | else | |
1788 | send_multihop_request(NULL, prefix, plen, seqno, id, 127); | |
1789 | ||
1790 | record_resend(RESEND_REQUEST, prefix, plen, seqno, id, | |
1791 | neigh ? neigh->ifp : NULL, resend_delay); | |
1792 | } | |
1793 | ||
1794 | void | |
1795 | handle_request(struct neighbour *neigh, const unsigned char *prefix, | |
1796 | unsigned char plen, unsigned char hop_count, | |
1797 | unsigned short seqno, const unsigned char *id) | |
1798 | { | |
1799 | struct xroute *xroute; | |
1800 | struct babel_route *route; | |
1801 | struct neighbour *successor = NULL; | |
1802 | ||
1803 | xroute = find_xroute(prefix, plen); | |
1804 | route = find_installed_route(prefix, plen); | |
1805 | ||
1806 | if(xroute && (!route || xroute->metric <= kernel_metric)) { | |
1807 | if(hop_count > 0 && memcmp(id, myid, 8) == 0) { | |
1808 | if(seqno_compare(seqno, myseqno) > 0) { | |
1809 | if(seqno_minus(seqno, myseqno) > 100) { | |
1810 | /* Hopelessly out-of-date request */ | |
1811 | return; | |
1812 | } | |
1813 | update_myseqno(); | |
1814 | } | |
1815 | } | |
1816 | send_update(neigh->ifp, 1, prefix, plen); | |
1817 | return; | |
1818 | } | |
1819 | ||
1820 | if(route && | |
1821 | (memcmp(id, route->src->id, 8) != 0 || | |
1822 | seqno_compare(seqno, route->seqno) <= 0)) { | |
1823 | send_update(neigh->ifp, 1, prefix, plen); | |
1824 | return; | |
1825 | } | |
1826 | ||
1827 | if(hop_count <= 1) | |
1828 | return; | |
1829 | ||
1830 | if(route && memcmp(id, route->src->id, 8) == 0 && | |
1831 | seqno_minus(seqno, route->seqno) > 100) { | |
1832 | /* Hopelessly out-of-date */ | |
1833 | return; | |
1834 | } | |
1835 | ||
1836 | if(request_redundant(neigh->ifp, prefix, plen, seqno, id)) | |
1837 | return; | |
1838 | ||
1839 | /* Let's try to forward this request. */ | |
1840 | if(route && route_metric(route) < INFINITY) | |
1841 | successor = route->neigh; | |
1842 | ||
1843 | if(!successor || successor == neigh) { | |
1844 | /* We were about to forward a request to its requestor. Try to | |
1845 | find a different neighbour to forward the request to. */ | |
1846 | struct babel_route *other_route; | |
1847 | ||
1848 | other_route = find_best_route(prefix, plen, 0, neigh); | |
1849 | if(other_route && route_metric(other_route) < INFINITY) | |
1850 | successor = other_route->neigh; | |
1851 | } | |
1852 | ||
1853 | if(!successor || successor == neigh) | |
1854 | /* Give up */ | |
1855 | return; | |
1856 | ||
1857 | send_unicast_multihop_request(successor, prefix, plen, seqno, id, | |
1858 | hop_count - 1); | |
1859 | record_resend(RESEND_REQUEST, prefix, plen, seqno, id, | |
1860 | neigh->ifp, 0); | |
1861 | } |