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