iof-bird-daemon / proto / babel / packets.c @ 3b3b0910
History | View | Annotate | Download (33.8 KB)
1 |
/*
|
---|---|
2 |
* BIRD -- The Babel protocol
|
3 |
*
|
4 |
* Copyright (c) 2015--2016 Toke Hoiland-Jorgensen
|
5 |
* (c) 2016--2017 Ondrej Zajicek <santiago@crfreenet.org>
|
6 |
* (c) 2016--2017 CZ.NIC z.s.p.o.
|
7 |
*
|
8 |
* Can be freely distributed and used under the terms of the GNU GPL.
|
9 |
*
|
10 |
* This file contains the packet and TLV handling code for the protocol.
|
11 |
*/
|
12 |
|
13 |
#include "babel.h" |
14 |
|
15 |
|
16 |
struct babel_pkt_header {
|
17 |
u8 magic; |
18 |
u8 version; |
19 |
u16 length; |
20 |
} PACKED; |
21 |
|
22 |
struct babel_tlv {
|
23 |
u8 type; |
24 |
u8 length; |
25 |
u8 value[0];
|
26 |
} PACKED; |
27 |
|
28 |
struct babel_tlv_ack_req {
|
29 |
u8 type; |
30 |
u8 length; |
31 |
u16 reserved; |
32 |
u16 nonce; |
33 |
u16 interval; |
34 |
} PACKED; |
35 |
|
36 |
struct babel_tlv_ack {
|
37 |
u8 type; |
38 |
u8 length; |
39 |
u16 nonce; |
40 |
} PACKED; |
41 |
|
42 |
struct babel_tlv_hello {
|
43 |
u8 type; |
44 |
u8 length; |
45 |
u16 reserved; |
46 |
u16 seqno; |
47 |
u16 interval; |
48 |
} PACKED; |
49 |
|
50 |
struct babel_tlv_ihu {
|
51 |
u8 type; |
52 |
u8 length; |
53 |
u8 ae; |
54 |
u8 reserved; |
55 |
u16 rxcost; |
56 |
u16 interval; |
57 |
u8 addr[0];
|
58 |
} PACKED; |
59 |
|
60 |
struct babel_tlv_router_id {
|
61 |
u8 type; |
62 |
u8 length; |
63 |
u16 reserved; |
64 |
u64 router_id; |
65 |
} PACKED; |
66 |
|
67 |
struct babel_tlv_next_hop {
|
68 |
u8 type; |
69 |
u8 length; |
70 |
u8 ae; |
71 |
u8 reserved; |
72 |
u8 addr[0];
|
73 |
} PACKED; |
74 |
|
75 |
struct babel_tlv_update {
|
76 |
u8 type; |
77 |
u8 length; |
78 |
u8 ae; |
79 |
u8 flags; |
80 |
u8 plen; |
81 |
u8 omitted; |
82 |
u16 interval; |
83 |
u16 seqno; |
84 |
u16 metric; |
85 |
u8 addr[0];
|
86 |
} PACKED; |
87 |
|
88 |
struct babel_tlv_route_request {
|
89 |
u8 type; |
90 |
u8 length; |
91 |
u8 ae; |
92 |
u8 plen; |
93 |
u8 addr[0];
|
94 |
} PACKED; |
95 |
|
96 |
struct babel_tlv_seqno_request {
|
97 |
u8 type; |
98 |
u8 length; |
99 |
u8 ae; |
100 |
u8 plen; |
101 |
u16 seqno; |
102 |
u8 hop_count; |
103 |
u8 reserved; |
104 |
u64 router_id; |
105 |
u8 addr[0];
|
106 |
} PACKED; |
107 |
|
108 |
|
109 |
#define BABEL_FLAG_DEF_PREFIX 0x80 |
110 |
#define BABEL_FLAG_ROUTER_ID 0x40 |
111 |
|
112 |
|
113 |
struct babel_parse_state {
|
114 |
struct babel_proto *proto;
|
115 |
struct babel_iface *ifa;
|
116 |
ip_addr saddr; |
117 |
ip_addr next_hop_ip4; |
118 |
ip_addr next_hop_ip6; |
119 |
u64 router_id; /* Router ID used in subsequent updates */
|
120 |
u8 def_ip6_prefix[16]; /* Implicit IPv6 prefix in network order */ |
121 |
u8 def_ip4_prefix[4]; /* Implicit IPv4 prefix in network order */ |
122 |
u8 router_id_seen; /* router_id field is valid */
|
123 |
u8 def_ip6_prefix_seen; /* def_ip6_prefix is valid */
|
124 |
u8 def_ip4_prefix_seen; /* def_ip4_prefix is valid */
|
125 |
u8 current_tlv_endpos; /* End of self-terminating TLVs (offset from start) */
|
126 |
}; |
127 |
|
128 |
enum parse_result {
|
129 |
PARSE_SUCCESS, |
130 |
PARSE_ERROR, |
131 |
PARSE_IGNORE, |
132 |
}; |
133 |
|
134 |
struct babel_write_state {
|
135 |
u64 router_id; |
136 |
u8 router_id_seen; |
137 |
ip_addr next_hop_ip4; |
138 |
ip_addr next_hop_ip6; |
139 |
u8 def_ip6_prefix[16]; /* Implicit IPv6 prefix in network order */ |
140 |
u8 def_ip6_pxlen; |
141 |
}; |
142 |
|
143 |
|
144 |
#define DROP(DSC,VAL) do { err_dsc = DSC; err_val = VAL; goto drop; } while(0) |
145 |
#define DROP1(DSC) do { err_dsc = DSC; goto drop; } while(0) |
146 |
#define LOG_PKT(msg, args...) \
|
147 |
log_rl(&p->log_pkt_tbf, L_REMOTE "%s: " msg, p->p.name, args)
|
148 |
|
149 |
#define FIRST_TLV(p) ((struct babel_tlv *) (((struct babel_pkt_header *) p) + 1)) |
150 |
#define NEXT_TLV(t) ((struct babel_tlv *) (((byte *) t) + TLV_LENGTH(t))) |
151 |
#define TLV_LENGTH(t) (t->type == BABEL_TLV_PAD1 ? 1 : t->length + sizeof(struct babel_tlv)) |
152 |
#define TLV_OPT_LENGTH(t) (t->length + sizeof(struct babel_tlv) - sizeof(*t)) |
153 |
#define TLV_HDR(tlv,t,l) ({ tlv->type = t; tlv->length = l - sizeof(struct babel_tlv); }) |
154 |
#define TLV_HDR0(tlv,t) TLV_HDR(tlv, t, tlv_data[t].min_length)
|
155 |
|
156 |
#define NET_SIZE(n) BYTES(net_pxlen(n))
|
157 |
|
158 |
static inline uint |
159 |
bytes_equal(u8 *b1, u8 *b2, uint maxlen) |
160 |
{ |
161 |
uint i; |
162 |
for (i = 0; (i < maxlen) && (*b1 == *b2); i++, b1++, b2++) |
163 |
; |
164 |
return i;
|
165 |
} |
166 |
|
167 |
static inline uint |
168 |
get_time16(const void *p) |
169 |
{ |
170 |
uint v = get_u16(p) * BABEL_TIME_UNITS; |
171 |
return MAX(BABEL_MIN_INTERVAL, v);
|
172 |
} |
173 |
|
174 |
static inline void |
175 |
put_time16(void *p, uint v)
|
176 |
{ |
177 |
put_u16(p, v / BABEL_TIME_UNITS); |
178 |
} |
179 |
|
180 |
static inline void |
181 |
read_ip4_px(net_addr *n, const void *p, uint plen) |
182 |
{ |
183 |
ip4_addr addr = {0};
|
184 |
memcpy(&addr, p, BYTES(plen)); |
185 |
net_fill_ip4(n, ip4_ntoh(addr), plen); |
186 |
} |
187 |
|
188 |
static inline void |
189 |
put_ip4_px(void *p, net_addr *n)
|
190 |
{ |
191 |
ip4_addr addr = ip4_hton(net4_prefix(n)); |
192 |
memcpy(p, &addr, NET_SIZE(n)); |
193 |
} |
194 |
|
195 |
static inline void |
196 |
read_ip6_px(net_addr *n, const void *p, uint plen) |
197 |
{ |
198 |
ip6_addr addr = IPA_NONE; |
199 |
memcpy(&addr, p, BYTES(plen)); |
200 |
net_fill_ip6(n, ip6_ntoh(addr), plen); |
201 |
} |
202 |
|
203 |
static inline void |
204 |
put_ip6_px(void *p, net_addr *n)
|
205 |
{ |
206 |
ip6_addr addr = ip6_hton(net6_prefix(n)); |
207 |
memcpy(p, &addr, NET_SIZE(n)); |
208 |
} |
209 |
|
210 |
static inline ip6_addr |
211 |
get_ip6_ll(const void *p) |
212 |
{ |
213 |
return ip6_build(0xfe800000, 0, get_u32(p+0), get_u32(p+4)); |
214 |
} |
215 |
|
216 |
static inline void |
217 |
put_ip6_ll(void *p, ip6_addr addr)
|
218 |
{ |
219 |
put_u32(p+0, _I2(addr));
|
220 |
put_u32(p+4, _I3(addr));
|
221 |
} |
222 |
|
223 |
|
224 |
/*
|
225 |
* TLV read/write functions
|
226 |
*/
|
227 |
|
228 |
static int babel_read_ack_req(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state); |
229 |
static int babel_read_hello(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state); |
230 |
static int babel_read_ihu(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state); |
231 |
static int babel_read_router_id(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state); |
232 |
static int babel_read_next_hop(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state); |
233 |
static int babel_read_update(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state); |
234 |
static int babel_read_route_request(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state); |
235 |
static int babel_read_seqno_request(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state); |
236 |
|
237 |
static uint babel_write_ack(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len); |
238 |
static uint babel_write_hello(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len); |
239 |
static uint babel_write_ihu(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len); |
240 |
static uint babel_write_update(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len); |
241 |
static uint babel_write_route_request(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len); |
242 |
static uint babel_write_seqno_request(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len); |
243 |
|
244 |
struct babel_tlv_data {
|
245 |
u8 min_length; |
246 |
int (*read_tlv)(struct babel_tlv *hdr, union babel_msg *m, struct babel_parse_state *state); |
247 |
uint (*write_tlv)(struct babel_tlv *hdr, union babel_msg *m, struct babel_write_state *state, uint max_len); |
248 |
void (*handle_tlv)(union babel_msg *m, struct babel_iface *ifa); |
249 |
}; |
250 |
|
251 |
static const struct babel_tlv_data tlv_data[BABEL_TLV_MAX] = { |
252 |
[BABEL_TLV_ACK_REQ] = { |
253 |
sizeof(struct babel_tlv_ack_req), |
254 |
babel_read_ack_req, |
255 |
NULL,
|
256 |
babel_handle_ack_req |
257 |
}, |
258 |
[BABEL_TLV_ACK] = { |
259 |
sizeof(struct babel_tlv_ack), |
260 |
NULL,
|
261 |
babel_write_ack, |
262 |
NULL
|
263 |
}, |
264 |
[BABEL_TLV_HELLO] = { |
265 |
sizeof(struct babel_tlv_hello), |
266 |
babel_read_hello, |
267 |
babel_write_hello, |
268 |
babel_handle_hello |
269 |
}, |
270 |
[BABEL_TLV_IHU] = { |
271 |
sizeof(struct babel_tlv_ihu), |
272 |
babel_read_ihu, |
273 |
babel_write_ihu, |
274 |
babel_handle_ihu |
275 |
}, |
276 |
[BABEL_TLV_ROUTER_ID] = { |
277 |
sizeof(struct babel_tlv_router_id), |
278 |
babel_read_router_id, |
279 |
NULL,
|
280 |
NULL
|
281 |
}, |
282 |
[BABEL_TLV_NEXT_HOP] = { |
283 |
sizeof(struct babel_tlv_next_hop), |
284 |
babel_read_next_hop, |
285 |
NULL,
|
286 |
NULL
|
287 |
}, |
288 |
[BABEL_TLV_UPDATE] = { |
289 |
sizeof(struct babel_tlv_update), |
290 |
babel_read_update, |
291 |
babel_write_update, |
292 |
babel_handle_update |
293 |
}, |
294 |
[BABEL_TLV_ROUTE_REQUEST] = { |
295 |
sizeof(struct babel_tlv_route_request), |
296 |
babel_read_route_request, |
297 |
babel_write_route_request, |
298 |
babel_handle_route_request |
299 |
}, |
300 |
[BABEL_TLV_SEQNO_REQUEST] = { |
301 |
sizeof(struct babel_tlv_seqno_request), |
302 |
babel_read_seqno_request, |
303 |
babel_write_seqno_request, |
304 |
babel_handle_seqno_request |
305 |
}, |
306 |
}; |
307 |
|
308 |
static int |
309 |
babel_read_ack_req(struct babel_tlv *hdr, union babel_msg *m, |
310 |
struct babel_parse_state *state)
|
311 |
{ |
312 |
struct babel_tlv_ack_req *tlv = (void *) hdr; |
313 |
struct babel_msg_ack_req *msg = &m->ack_req;
|
314 |
|
315 |
msg->type = BABEL_TLV_ACK_REQ; |
316 |
msg->nonce = get_u16(&tlv->nonce); |
317 |
msg->interval = get_time16(&tlv->interval); |
318 |
msg->sender = state->saddr; |
319 |
|
320 |
if (!msg->interval)
|
321 |
return PARSE_ERROR;
|
322 |
|
323 |
return PARSE_SUCCESS;
|
324 |
} |
325 |
|
326 |
static uint
|
327 |
babel_write_ack(struct babel_tlv *hdr, union babel_msg *m, |
328 |
struct babel_write_state *state UNUSED, uint max_len UNUSED)
|
329 |
{ |
330 |
struct babel_tlv_ack *tlv = (void *) hdr; |
331 |
struct babel_msg_ack *msg = &m->ack;
|
332 |
|
333 |
TLV_HDR0(tlv, BABEL_TLV_ACK); |
334 |
put_u16(&tlv->nonce, msg->nonce); |
335 |
|
336 |
return sizeof(struct babel_tlv_ack); |
337 |
} |
338 |
|
339 |
static int |
340 |
babel_read_hello(struct babel_tlv *hdr, union babel_msg *m, |
341 |
struct babel_parse_state *state)
|
342 |
{ |
343 |
struct babel_tlv_hello *tlv = (void *) hdr; |
344 |
struct babel_msg_hello *msg = &m->hello;
|
345 |
|
346 |
msg->type = BABEL_TLV_HELLO; |
347 |
msg->seqno = get_u16(&tlv->seqno); |
348 |
msg->interval = get_time16(&tlv->interval); |
349 |
msg->sender = state->saddr; |
350 |
|
351 |
return PARSE_SUCCESS;
|
352 |
} |
353 |
|
354 |
static uint
|
355 |
babel_write_hello(struct babel_tlv *hdr, union babel_msg *m, |
356 |
struct babel_write_state *state UNUSED, uint max_len UNUSED)
|
357 |
{ |
358 |
struct babel_tlv_hello *tlv = (void *) hdr; |
359 |
struct babel_msg_hello *msg = &m->hello;
|
360 |
|
361 |
TLV_HDR0(tlv, BABEL_TLV_HELLO); |
362 |
put_u16(&tlv->seqno, msg->seqno); |
363 |
put_time16(&tlv->interval, msg->interval); |
364 |
|
365 |
return sizeof(struct babel_tlv_hello); |
366 |
} |
367 |
|
368 |
static int |
369 |
babel_read_ihu(struct babel_tlv *hdr, union babel_msg *m, |
370 |
struct babel_parse_state *state)
|
371 |
{ |
372 |
struct babel_tlv_ihu *tlv = (void *) hdr; |
373 |
struct babel_msg_ihu *msg = &m->ihu;
|
374 |
|
375 |
msg->type = BABEL_TLV_IHU; |
376 |
msg->ae = tlv->ae; |
377 |
msg->rxcost = get_u16(&tlv->rxcost); |
378 |
msg->interval = get_time16(&tlv->interval); |
379 |
msg->addr = IPA_NONE; |
380 |
msg->sender = state->saddr; |
381 |
|
382 |
if (msg->ae >= BABEL_AE_MAX)
|
383 |
return PARSE_IGNORE;
|
384 |
|
385 |
/*
|
386 |
* We only actually read link-local IPs. In every other case, the addr field
|
387 |
* will be 0 but validation will succeed. The handler takes care of these
|
388 |
* cases. We handle them here anyway because we need the length for parsing
|
389 |
* subtlvs.
|
390 |
*/
|
391 |
switch (msg->ae)
|
392 |
{ |
393 |
case BABEL_AE_IP4:
|
394 |
if (TLV_OPT_LENGTH(tlv) < 4) |
395 |
return PARSE_ERROR;
|
396 |
state->current_tlv_endpos += 4;
|
397 |
break;
|
398 |
|
399 |
case BABEL_AE_IP6:
|
400 |
if (TLV_OPT_LENGTH(tlv) < 16) |
401 |
return PARSE_ERROR;
|
402 |
state->current_tlv_endpos += 16;
|
403 |
break;
|
404 |
|
405 |
case BABEL_AE_IP6_LL:
|
406 |
if (TLV_OPT_LENGTH(tlv) < 8) |
407 |
return PARSE_ERROR;
|
408 |
|
409 |
msg->addr = ipa_from_ip6(get_ip6_ll(&tlv->addr)); |
410 |
state->current_tlv_endpos += 8;
|
411 |
break;
|
412 |
} |
413 |
|
414 |
return PARSE_SUCCESS;
|
415 |
} |
416 |
|
417 |
static uint
|
418 |
babel_write_ihu(struct babel_tlv *hdr, union babel_msg *m, |
419 |
struct babel_write_state *state UNUSED, uint max_len)
|
420 |
{ |
421 |
struct babel_tlv_ihu *tlv = (void *) hdr; |
422 |
struct babel_msg_ihu *msg = &m->ihu;
|
423 |
|
424 |
if (ipa_is_link_local(msg->addr) && max_len < sizeof(struct babel_tlv_ihu) + 8) |
425 |
return 0; |
426 |
|
427 |
TLV_HDR0(tlv, BABEL_TLV_IHU); |
428 |
put_u16(&tlv->rxcost, msg->rxcost); |
429 |
put_time16(&tlv->interval, msg->interval); |
430 |
|
431 |
if (!ipa_is_link_local(msg->addr))
|
432 |
{ |
433 |
tlv->ae = BABEL_AE_WILDCARD; |
434 |
return sizeof(struct babel_tlv_ihu); |
435 |
} |
436 |
put_ip6_ll(&tlv->addr, msg->addr); |
437 |
tlv->ae = BABEL_AE_IP6_LL; |
438 |
hdr->length += 8;
|
439 |
return sizeof(struct babel_tlv_ihu) + 8; |
440 |
} |
441 |
|
442 |
static int |
443 |
babel_read_router_id(struct babel_tlv *hdr, union babel_msg *m UNUSED, |
444 |
struct babel_parse_state *state)
|
445 |
{ |
446 |
struct babel_tlv_router_id *tlv = (void *) hdr; |
447 |
|
448 |
state->router_id = get_u64(&tlv->router_id); |
449 |
state->router_id_seen = 1;
|
450 |
|
451 |
return PARSE_IGNORE;
|
452 |
} |
453 |
|
454 |
/* This is called directly from babel_write_update() */
|
455 |
static uint
|
456 |
babel_write_router_id(struct babel_tlv *hdr, u64 router_id,
|
457 |
struct babel_write_state *state, uint max_len UNUSED)
|
458 |
{ |
459 |
struct babel_tlv_router_id *tlv = (void *) hdr; |
460 |
|
461 |
/* We still assume that first min_length bytes are available and zeroed */
|
462 |
|
463 |
TLV_HDR0(tlv, BABEL_TLV_ROUTER_ID); |
464 |
put_u64(&tlv->router_id, router_id); |
465 |
|
466 |
state->router_id = router_id; |
467 |
state->router_id_seen = 1;
|
468 |
|
469 |
return sizeof(struct babel_tlv_router_id); |
470 |
} |
471 |
|
472 |
static int |
473 |
babel_read_next_hop(struct babel_tlv *hdr, union babel_msg *m UNUSED, |
474 |
struct babel_parse_state *state)
|
475 |
{ |
476 |
struct babel_tlv_next_hop *tlv = (void *) hdr; |
477 |
|
478 |
switch (tlv->ae)
|
479 |
{ |
480 |
case BABEL_AE_WILDCARD:
|
481 |
return PARSE_ERROR;
|
482 |
|
483 |
case BABEL_AE_IP4:
|
484 |
if (TLV_OPT_LENGTH(tlv) < sizeof(ip4_addr)) |
485 |
return PARSE_ERROR;
|
486 |
|
487 |
state->next_hop_ip4 = ipa_from_ip4(get_ip4(&tlv->addr)); |
488 |
state->current_tlv_endpos += sizeof(ip4_addr);
|
489 |
return PARSE_IGNORE;
|
490 |
|
491 |
case BABEL_AE_IP6:
|
492 |
if (TLV_OPT_LENGTH(tlv) < sizeof(ip6_addr)) |
493 |
return PARSE_ERROR;
|
494 |
|
495 |
state->next_hop_ip6 = ipa_from_ip6(get_ip6(&tlv->addr)); |
496 |
state->current_tlv_endpos += sizeof(ip6_addr);
|
497 |
return PARSE_IGNORE;
|
498 |
|
499 |
case BABEL_AE_IP6_LL:
|
500 |
if (TLV_OPT_LENGTH(tlv) < 8) |
501 |
return PARSE_ERROR;
|
502 |
|
503 |
state->next_hop_ip6 = ipa_from_ip6(get_ip6_ll(&tlv->addr)); |
504 |
state->current_tlv_endpos += 8;
|
505 |
return PARSE_IGNORE;
|
506 |
|
507 |
default:
|
508 |
return PARSE_IGNORE;
|
509 |
} |
510 |
|
511 |
return PARSE_IGNORE;
|
512 |
} |
513 |
|
514 |
/* This is called directly from babel_write_update() and returns -1 if a next
|
515 |
hop should be written but there is not enough space. */
|
516 |
static int |
517 |
babel_write_next_hop(struct babel_tlv *hdr, ip_addr addr,
|
518 |
struct babel_write_state *state, uint max_len)
|
519 |
{ |
520 |
struct babel_tlv_next_hop *tlv = (void *) hdr; |
521 |
|
522 |
if (ipa_zero(addr))
|
523 |
{ |
524 |
/* Should not happen */
|
525 |
return 0; |
526 |
} |
527 |
else if (ipa_is_ip4(addr) && !ipa_equal(addr, state->next_hop_ip4)) |
528 |
{ |
529 |
uint len = sizeof(struct babel_tlv_next_hop) + sizeof(ip4_addr); |
530 |
if (len > max_len)
|
531 |
return -1; |
532 |
|
533 |
TLV_HDR(tlv, BABEL_TLV_NEXT_HOP, len); |
534 |
|
535 |
tlv->ae = BABEL_AE_IP4; |
536 |
put_ip4(&tlv->addr, ipa_to_ip4(addr)); |
537 |
state->next_hop_ip4 = addr; |
538 |
|
539 |
return len;
|
540 |
} |
541 |
else if (ipa_is_ip6(addr) && !ipa_equal(addr, state->next_hop_ip6)) |
542 |
{ |
543 |
uint len = sizeof(struct babel_tlv_next_hop) + sizeof(ip6_addr); |
544 |
if (len > max_len)
|
545 |
return -1; |
546 |
|
547 |
TLV_HDR(tlv, BABEL_TLV_NEXT_HOP, len); |
548 |
|
549 |
tlv->ae = BABEL_AE_IP6; |
550 |
put_ip6(&tlv->addr, ipa_to_ip6(addr)); |
551 |
state->next_hop_ip6 = addr; |
552 |
|
553 |
return len;
|
554 |
} |
555 |
|
556 |
return 0; |
557 |
} |
558 |
|
559 |
static int |
560 |
babel_read_update(struct babel_tlv *hdr, union babel_msg *m, |
561 |
struct babel_parse_state *state)
|
562 |
{ |
563 |
struct babel_tlv_update *tlv = (void *) hdr; |
564 |
struct babel_msg_update *msg = &m->update;
|
565 |
|
566 |
msg->type = BABEL_TLV_UPDATE; |
567 |
msg->interval = get_time16(&tlv->interval); |
568 |
msg->seqno = get_u16(&tlv->seqno); |
569 |
msg->metric = get_u16(&tlv->metric); |
570 |
|
571 |
/* Length of received prefix data without omitted part */
|
572 |
int len = BYTES(tlv->plen) - (int) tlv->omitted; |
573 |
u8 buf[16] = {};
|
574 |
|
575 |
if ((len < 0) || ((uint) len > TLV_OPT_LENGTH(tlv))) |
576 |
return PARSE_ERROR;
|
577 |
|
578 |
switch (tlv->ae)
|
579 |
{ |
580 |
case BABEL_AE_WILDCARD:
|
581 |
if (tlv->plen > 0) |
582 |
return PARSE_ERROR;
|
583 |
|
584 |
if (msg->metric != 65535) |
585 |
return PARSE_ERROR;
|
586 |
|
587 |
msg->wildcard = 1;
|
588 |
break;
|
589 |
|
590 |
case BABEL_AE_IP4:
|
591 |
if (tlv->plen > IP4_MAX_PREFIX_LENGTH)
|
592 |
return PARSE_ERROR;
|
593 |
|
594 |
/* Cannot omit data if there is no saved prefix */
|
595 |
if (tlv->omitted && !state->def_ip4_prefix_seen)
|
596 |
return PARSE_ERROR;
|
597 |
|
598 |
/* Update must have next hop, unless it is retraction */
|
599 |
if (ipa_zero(state->next_hop_ip4) && (msg->metric != BABEL_INFINITY))
|
600 |
return PARSE_ERROR;
|
601 |
|
602 |
/* Merge saved prefix and received prefix parts */
|
603 |
memcpy(buf, state->def_ip4_prefix, tlv->omitted); |
604 |
memcpy(buf + tlv->omitted, tlv->addr, len); |
605 |
|
606 |
ip4_addr prefix4 = get_ip4(buf); |
607 |
net_fill_ip4(&msg->net, prefix4, tlv->plen); |
608 |
|
609 |
if (tlv->flags & BABEL_FLAG_DEF_PREFIX)
|
610 |
{ |
611 |
put_ip4(state->def_ip4_prefix, prefix4); |
612 |
state->def_ip4_prefix_seen = 1;
|
613 |
} |
614 |
|
615 |
msg->next_hop = state->next_hop_ip4; |
616 |
|
617 |
break;
|
618 |
|
619 |
case BABEL_AE_IP6:
|
620 |
if (tlv->plen > IP6_MAX_PREFIX_LENGTH)
|
621 |
return PARSE_ERROR;
|
622 |
|
623 |
/* Cannot omit data if there is no saved prefix */
|
624 |
if (tlv->omitted && !state->def_ip6_prefix_seen)
|
625 |
return PARSE_ERROR;
|
626 |
|
627 |
/* Merge saved prefix and received prefix parts */
|
628 |
memcpy(buf, state->def_ip6_prefix, tlv->omitted); |
629 |
memcpy(buf + tlv->omitted, tlv->addr, len); |
630 |
|
631 |
ip6_addr prefix6 = get_ip6(buf); |
632 |
net_fill_ip6(&msg->net, prefix6, tlv->plen); |
633 |
|
634 |
if (tlv->flags & BABEL_FLAG_DEF_PREFIX)
|
635 |
{ |
636 |
put_ip6(state->def_ip6_prefix, prefix6); |
637 |
state->def_ip6_prefix_seen = 1;
|
638 |
} |
639 |
|
640 |
if (tlv->flags & BABEL_FLAG_ROUTER_ID)
|
641 |
{ |
642 |
state->router_id = ((u64) _I2(prefix6)) << 32 | _I3(prefix6);
|
643 |
state->router_id_seen = 1;
|
644 |
} |
645 |
|
646 |
msg->next_hop = state->next_hop_ip6; |
647 |
|
648 |
break;
|
649 |
|
650 |
case BABEL_AE_IP6_LL:
|
651 |
/* ??? */
|
652 |
return PARSE_IGNORE;
|
653 |
|
654 |
default:
|
655 |
return PARSE_IGNORE;
|
656 |
} |
657 |
|
658 |
/* Update must have Router ID, unless it is retraction */
|
659 |
if (!state->router_id_seen && (msg->metric != BABEL_INFINITY))
|
660 |
{ |
661 |
DBG("Babel: No router ID seen before update\n");
|
662 |
return PARSE_ERROR;
|
663 |
} |
664 |
|
665 |
msg->router_id = state->router_id; |
666 |
msg->sender = state->saddr; |
667 |
state->current_tlv_endpos += len; |
668 |
|
669 |
return PARSE_SUCCESS;
|
670 |
} |
671 |
|
672 |
static uint
|
673 |
babel_write_update(struct babel_tlv *hdr, union babel_msg *m, |
674 |
struct babel_write_state *state, uint max_len)
|
675 |
{ |
676 |
struct babel_msg_update *msg = &m->update;
|
677 |
uint len0 = 0;
|
678 |
|
679 |
/*
|
680 |
* When needed, we write Router-ID TLV before Update TLV and return size of
|
681 |
* both of them. There is enough space for the Router-ID TLV, because
|
682 |
* sizeof(struct babel_tlv_router_id) == sizeof(struct babel_tlv_update).
|
683 |
*
|
684 |
* Router ID is not used for retractions, so do not use it in such case.
|
685 |
*/
|
686 |
if ((msg->metric < BABEL_INFINITY) &&
|
687 |
(!state->router_id_seen || (msg->router_id != state->router_id))) |
688 |
{ |
689 |
len0 = babel_write_router_id(hdr, msg->router_id, state, max_len); |
690 |
hdr = NEXT_TLV(hdr); |
691 |
} |
692 |
|
693 |
/*
|
694 |
* We also may add Next Hop TLV for regular updates. It may fail for not
|
695 |
* enough space or it may be unnecessary as the next hop is the same as the
|
696 |
* last one already announced. So we handle all three cases.
|
697 |
*/
|
698 |
if (msg->metric < BABEL_INFINITY)
|
699 |
{ |
700 |
int l = babel_write_next_hop(hdr, msg->next_hop, state, max_len - len0);
|
701 |
if (l < 0) |
702 |
return 0; |
703 |
|
704 |
if (l)
|
705 |
{ |
706 |
len0 += l; |
707 |
hdr = NEXT_TLV(hdr); |
708 |
} |
709 |
} |
710 |
|
711 |
struct babel_tlv_update *tlv = (void *) hdr; |
712 |
uint len = sizeof(struct babel_tlv_update) + NET_SIZE(&msg->net); |
713 |
|
714 |
if (len0 + len > max_len)
|
715 |
return 0; |
716 |
|
717 |
memset(tlv, 0, sizeof(struct babel_tlv_update)); |
718 |
TLV_HDR(tlv, BABEL_TLV_UPDATE, len); |
719 |
|
720 |
if (msg->wildcard)
|
721 |
{ |
722 |
tlv->ae = BABEL_AE_WILDCARD; |
723 |
tlv->plen = 0;
|
724 |
} |
725 |
else if (msg->net.type == NET_IP4) |
726 |
{ |
727 |
tlv->ae = BABEL_AE_IP4; |
728 |
tlv->plen = net4_pxlen(&msg->net); |
729 |
put_ip4_px(tlv->addr, &msg->net); |
730 |
} |
731 |
else
|
732 |
{ |
733 |
tlv->ae = BABEL_AE_IP6; |
734 |
tlv->plen = net6_pxlen(&msg->net); |
735 |
|
736 |
/* Address compression - omit initial matching bytes */
|
737 |
u8 buf[16], omit;
|
738 |
put_ip6(buf, net6_prefix(&msg->net)); |
739 |
omit = bytes_equal(buf, state->def_ip6_prefix, |
740 |
MIN(tlv->plen, state->def_ip6_pxlen) / 8);
|
741 |
|
742 |
if (omit > 0) |
743 |
{ |
744 |
memcpy(tlv->addr, buf + omit, NET_SIZE(&msg->net) - omit); |
745 |
|
746 |
tlv->omitted = omit; |
747 |
tlv->length -= omit; |
748 |
len -= omit; |
749 |
} |
750 |
else
|
751 |
{ |
752 |
put_ip6_px(tlv->addr, &msg->net); |
753 |
tlv->flags |= BABEL_FLAG_DEF_PREFIX; |
754 |
|
755 |
put_ip6(state->def_ip6_prefix, net6_prefix(&msg->net)); |
756 |
state->def_ip6_pxlen = tlv->plen; |
757 |
} |
758 |
} |
759 |
|
760 |
put_time16(&tlv->interval, msg->interval); |
761 |
put_u16(&tlv->seqno, msg->seqno); |
762 |
put_u16(&tlv->metric, msg->metric); |
763 |
|
764 |
return len0 + len;
|
765 |
} |
766 |
|
767 |
static int |
768 |
babel_read_route_request(struct babel_tlv *hdr, union babel_msg *m, |
769 |
struct babel_parse_state *state UNUSED)
|
770 |
{ |
771 |
struct babel_tlv_route_request *tlv = (void *) hdr; |
772 |
struct babel_msg_route_request *msg = &m->route_request;
|
773 |
|
774 |
msg->type = BABEL_TLV_ROUTE_REQUEST; |
775 |
|
776 |
switch (tlv->ae)
|
777 |
{ |
778 |
case BABEL_AE_WILDCARD:
|
779 |
/* Wildcard requests must have plen 0 */
|
780 |
if (tlv->plen > 0) |
781 |
return PARSE_ERROR;
|
782 |
|
783 |
msg->full = 1;
|
784 |
return PARSE_SUCCESS;
|
785 |
|
786 |
case BABEL_AE_IP4:
|
787 |
if (tlv->plen > IP4_MAX_PREFIX_LENGTH)
|
788 |
return PARSE_ERROR;
|
789 |
|
790 |
if (TLV_OPT_LENGTH(tlv) < BYTES(tlv->plen))
|
791 |
return PARSE_ERROR;
|
792 |
|
793 |
read_ip4_px(&msg->net, tlv->addr, tlv->plen); |
794 |
state->current_tlv_endpos += BYTES(tlv->plen); |
795 |
return PARSE_SUCCESS;
|
796 |
|
797 |
case BABEL_AE_IP6:
|
798 |
if (tlv->plen > IP6_MAX_PREFIX_LENGTH)
|
799 |
return PARSE_ERROR;
|
800 |
|
801 |
if (TLV_OPT_LENGTH(tlv) < BYTES(tlv->plen))
|
802 |
return PARSE_ERROR;
|
803 |
|
804 |
read_ip6_px(&msg->net, tlv->addr, tlv->plen); |
805 |
state->current_tlv_endpos += BYTES(tlv->plen); |
806 |
return PARSE_SUCCESS;
|
807 |
|
808 |
case BABEL_AE_IP6_LL:
|
809 |
return PARSE_ERROR;
|
810 |
|
811 |
default:
|
812 |
return PARSE_IGNORE;
|
813 |
} |
814 |
|
815 |
return PARSE_IGNORE;
|
816 |
} |
817 |
|
818 |
static uint
|
819 |
babel_write_route_request(struct babel_tlv *hdr, union babel_msg *m, |
820 |
struct babel_write_state *state UNUSED, uint max_len)
|
821 |
{ |
822 |
struct babel_tlv_route_request *tlv = (void *) hdr; |
823 |
struct babel_msg_route_request *msg = &m->route_request;
|
824 |
|
825 |
uint len = sizeof(struct babel_tlv_route_request) + NET_SIZE(&msg->net); |
826 |
|
827 |
if (len > max_len)
|
828 |
return 0; |
829 |
|
830 |
TLV_HDR(tlv, BABEL_TLV_ROUTE_REQUEST, len); |
831 |
|
832 |
if (msg->full)
|
833 |
{ |
834 |
tlv->ae = BABEL_AE_WILDCARD; |
835 |
tlv->plen = 0;
|
836 |
} |
837 |
else if (msg->net.type == NET_IP4) |
838 |
{ |
839 |
tlv->ae = BABEL_AE_IP4; |
840 |
tlv->plen = net4_pxlen(&msg->net); |
841 |
put_ip4_px(tlv->addr, &msg->net); |
842 |
} |
843 |
else
|
844 |
{ |
845 |
tlv->ae = BABEL_AE_IP6; |
846 |
tlv->plen = net6_pxlen(&msg->net); |
847 |
put_ip6_px(tlv->addr, &msg->net); |
848 |
} |
849 |
|
850 |
return len;
|
851 |
} |
852 |
|
853 |
static int |
854 |
babel_read_seqno_request(struct babel_tlv *hdr, union babel_msg *m, |
855 |
struct babel_parse_state *state)
|
856 |
{ |
857 |
struct babel_tlv_seqno_request *tlv = (void *) hdr; |
858 |
struct babel_msg_seqno_request *msg = &m->seqno_request;
|
859 |
|
860 |
msg->type = BABEL_TLV_SEQNO_REQUEST; |
861 |
msg->seqno = get_u16(&tlv->seqno); |
862 |
msg->hop_count = tlv->hop_count; |
863 |
msg->router_id = get_u64(&tlv->router_id); |
864 |
msg->sender = state->saddr; |
865 |
|
866 |
if (tlv->hop_count == 0) |
867 |
return PARSE_ERROR;
|
868 |
|
869 |
switch (tlv->ae)
|
870 |
{ |
871 |
case BABEL_AE_WILDCARD:
|
872 |
return PARSE_ERROR;
|
873 |
|
874 |
case BABEL_AE_IP4:
|
875 |
if (tlv->plen > IP4_MAX_PREFIX_LENGTH)
|
876 |
return PARSE_ERROR;
|
877 |
|
878 |
if (TLV_OPT_LENGTH(tlv) < BYTES(tlv->plen))
|
879 |
return PARSE_ERROR;
|
880 |
|
881 |
read_ip4_px(&msg->net, tlv->addr, tlv->plen); |
882 |
state->current_tlv_endpos += BYTES(tlv->plen); |
883 |
return PARSE_SUCCESS;
|
884 |
|
885 |
case BABEL_AE_IP6:
|
886 |
if (tlv->plen > IP6_MAX_PREFIX_LENGTH)
|
887 |
return PARSE_ERROR;
|
888 |
|
889 |
if (TLV_OPT_LENGTH(tlv) < BYTES(tlv->plen))
|
890 |
return PARSE_ERROR;
|
891 |
|
892 |
read_ip6_px(&msg->net, tlv->addr, tlv->plen); |
893 |
state->current_tlv_endpos += BYTES(tlv->plen); |
894 |
return PARSE_SUCCESS;
|
895 |
|
896 |
case BABEL_AE_IP6_LL:
|
897 |
return PARSE_ERROR;
|
898 |
|
899 |
default:
|
900 |
return PARSE_IGNORE;
|
901 |
} |
902 |
|
903 |
return PARSE_IGNORE;
|
904 |
} |
905 |
|
906 |
static uint
|
907 |
babel_write_seqno_request(struct babel_tlv *hdr, union babel_msg *m, |
908 |
struct babel_write_state *state UNUSED, uint max_len)
|
909 |
{ |
910 |
struct babel_tlv_seqno_request *tlv = (void *) hdr; |
911 |
struct babel_msg_seqno_request *msg = &m->seqno_request;
|
912 |
|
913 |
uint len = sizeof(struct babel_tlv_seqno_request) + NET_SIZE(&msg->net); |
914 |
|
915 |
if (len > max_len)
|
916 |
return 0; |
917 |
|
918 |
TLV_HDR(tlv, BABEL_TLV_SEQNO_REQUEST, len); |
919 |
|
920 |
if (msg->net.type == NET_IP4)
|
921 |
{ |
922 |
tlv->ae = BABEL_AE_IP4; |
923 |
tlv->plen = net4_pxlen(&msg->net); |
924 |
put_ip4_px(tlv->addr, &msg->net); |
925 |
} |
926 |
else
|
927 |
{ |
928 |
tlv->ae = BABEL_AE_IP6; |
929 |
tlv->plen = net6_pxlen(&msg->net); |
930 |
put_ip6_px(tlv->addr, &msg->net); |
931 |
} |
932 |
|
933 |
put_u16(&tlv->seqno, msg->seqno); |
934 |
tlv->hop_count = msg->hop_count; |
935 |
put_u64(&tlv->router_id, msg->router_id); |
936 |
|
937 |
return len;
|
938 |
} |
939 |
|
940 |
static inline int |
941 |
babel_read_subtlvs(struct babel_tlv *hdr,
|
942 |
union babel_msg *msg UNUSED,
|
943 |
struct babel_parse_state *state)
|
944 |
{ |
945 |
struct babel_tlv *tlv;
|
946 |
|
947 |
for (tlv = (void *) hdr + state->current_tlv_endpos; |
948 |
(void *) tlv < (void *) hdr + TLV_LENGTH(hdr); |
949 |
tlv = NEXT_TLV(tlv)) |
950 |
{ |
951 |
/*
|
952 |
* The subtlv type space is non-contiguous (due to the mandatory bit), so
|
953 |
* use a switch for dispatch instead of the mapping array we use for TLVs
|
954 |
*/
|
955 |
switch (tlv->type)
|
956 |
{ |
957 |
case BABEL_SUBTLV_PAD1:
|
958 |
case BABEL_SUBTLV_PADN:
|
959 |
/* FIXME: Framing errors in PADN are silently ignored, see babel_process_packet() */
|
960 |
break;
|
961 |
|
962 |
default:
|
963 |
/* Unknown mandatory subtlv; PARSE_IGNORE ignores the whole TLV */
|
964 |
if (tlv->type > 128) |
965 |
{ |
966 |
DBG("Babel: Mandatory subtlv %d found; skipping TLV\n", tlv->type);
|
967 |
return PARSE_IGNORE;
|
968 |
} |
969 |
break;
|
970 |
} |
971 |
} |
972 |
|
973 |
return PARSE_SUCCESS;
|
974 |
} |
975 |
|
976 |
static inline int |
977 |
babel_read_tlv(struct babel_tlv *hdr,
|
978 |
union babel_msg *msg,
|
979 |
struct babel_parse_state *state)
|
980 |
{ |
981 |
if ((hdr->type <= BABEL_TLV_PADN) ||
|
982 |
(hdr->type >= BABEL_TLV_MAX) || |
983 |
!tlv_data[hdr->type].read_tlv) |
984 |
return PARSE_IGNORE;
|
985 |
|
986 |
if (TLV_LENGTH(hdr) < tlv_data[hdr->type].min_length)
|
987 |
return PARSE_ERROR;
|
988 |
|
989 |
state->current_tlv_endpos = tlv_data[hdr->type].min_length; |
990 |
memset(msg, 0, sizeof(*msg)); |
991 |
|
992 |
int res = tlv_data[hdr->type].read_tlv(hdr, msg, state);
|
993 |
if (res != PARSE_SUCCESS)
|
994 |
return res;
|
995 |
|
996 |
return babel_read_subtlvs(hdr, msg, state);
|
997 |
} |
998 |
|
999 |
static uint
|
1000 |
babel_write_tlv(struct babel_tlv *hdr,
|
1001 |
union babel_msg *msg,
|
1002 |
struct babel_write_state *state,
|
1003 |
uint max_len) |
1004 |
{ |
1005 |
if ((msg->type <= BABEL_TLV_PADN) ||
|
1006 |
(msg->type >= BABEL_TLV_MAX) || |
1007 |
!tlv_data[msg->type].write_tlv) |
1008 |
return 0; |
1009 |
|
1010 |
if (tlv_data[msg->type].min_length > max_len)
|
1011 |
return 0; |
1012 |
|
1013 |
memset(hdr, 0, tlv_data[msg->type].min_length);
|
1014 |
return tlv_data[msg->type].write_tlv(hdr, msg, state, max_len);
|
1015 |
} |
1016 |
|
1017 |
|
1018 |
/*
|
1019 |
* Packet RX/TX functions
|
1020 |
*/
|
1021 |
|
1022 |
static int |
1023 |
babel_send_to(struct babel_iface *ifa, ip_addr dest)
|
1024 |
{ |
1025 |
sock *sk = ifa->sk; |
1026 |
struct babel_pkt_header *hdr = (void *) sk->tbuf; |
1027 |
int len = get_u16(&hdr->length) + sizeof(struct babel_pkt_header); |
1028 |
|
1029 |
DBG("Babel: Sending %d bytes to %I\n", len, dest);
|
1030 |
return sk_send_to(sk, len, dest, 0); |
1031 |
} |
1032 |
|
1033 |
/**
|
1034 |
* babel_write_queue - Write a TLV queue to a transmission buffer
|
1035 |
* @ifa: Interface holding the transmission buffer
|
1036 |
* @queue: TLV queue to write (containing internal-format TLVs)
|
1037 |
*
|
1038 |
* This function writes a packet to the interface transmission buffer with as
|
1039 |
* many TLVs from the &queue as will fit in the buffer. It returns the number of
|
1040 |
* bytes written (NOT counting the packet header). The function is called by
|
1041 |
* babel_send_queue() and babel_send_unicast() to construct packets for
|
1042 |
* transmission, and uses per-TLV helper functions to convert the
|
1043 |
* internal-format TLVs to their wire representations.
|
1044 |
*
|
1045 |
* The TLVs in the queue are freed after they are written to the buffer.
|
1046 |
*/
|
1047 |
static uint
|
1048 |
babel_write_queue(struct babel_iface *ifa, list *queue)
|
1049 |
{ |
1050 |
struct babel_proto *p = ifa->proto;
|
1051 |
struct babel_write_state state = { .next_hop_ip6 = ifa->addr };
|
1052 |
|
1053 |
if (EMPTY_LIST(*queue))
|
1054 |
return 0; |
1055 |
|
1056 |
byte *pos = ifa->sk->tbuf; |
1057 |
byte *end = pos + ifa->tx_length; |
1058 |
|
1059 |
struct babel_pkt_header *pkt = (void *) pos; |
1060 |
pkt->magic = BABEL_MAGIC; |
1061 |
pkt->version = BABEL_VERSION; |
1062 |
pkt->length = 0;
|
1063 |
pos += sizeof(struct babel_pkt_header); |
1064 |
|
1065 |
struct babel_msg_node *msg;
|
1066 |
WALK_LIST_FIRST(msg, *queue) |
1067 |
{ |
1068 |
if (pos >= end)
|
1069 |
break;
|
1070 |
|
1071 |
int len = babel_write_tlv((struct babel_tlv *) pos, &msg->msg, &state, end - pos); |
1072 |
|
1073 |
if (!len)
|
1074 |
break;
|
1075 |
|
1076 |
pos += len; |
1077 |
rem_node(NODE msg); |
1078 |
sl_free(p->msg_slab, msg); |
1079 |
} |
1080 |
|
1081 |
uint plen = pos - (byte *) pkt; |
1082 |
put_u16(&pkt->length, plen - sizeof(struct babel_pkt_header)); |
1083 |
|
1084 |
return plen;
|
1085 |
} |
1086 |
|
1087 |
void
|
1088 |
babel_send_queue(void *arg)
|
1089 |
{ |
1090 |
struct babel_iface *ifa = arg;
|
1091 |
while ((babel_write_queue(ifa, &ifa->msg_queue) > 0) && |
1092 |
(babel_send_to(ifa, IP6_BABEL_ROUTERS) > 0));
|
1093 |
} |
1094 |
|
1095 |
static inline void |
1096 |
babel_kick_queue(struct babel_iface *ifa)
|
1097 |
{ |
1098 |
/*
|
1099 |
* Only schedule send event if there is not already data in the socket buffer.
|
1100 |
* Otherwise we may overwrite the data already in the buffer.
|
1101 |
*/
|
1102 |
|
1103 |
if ((ifa->sk->tpos == ifa->sk->tbuf) && !ev_active(ifa->send_event))
|
1104 |
ev_schedule(ifa->send_event); |
1105 |
} |
1106 |
|
1107 |
/**
|
1108 |
* babel_send_unicast - send a single TLV via unicast to a destination
|
1109 |
* @msg: TLV to send
|
1110 |
* @ifa: Interface to send via
|
1111 |
* @dest: Destination of the TLV
|
1112 |
*
|
1113 |
* This function is used to send a single TLV via unicast to a designated
|
1114 |
* receiver. This is used for replying to certain incoming requests, and for
|
1115 |
* sending unicast requests to refresh routes before they expire.
|
1116 |
*/
|
1117 |
void
|
1118 |
babel_send_unicast(union babel_msg *msg, struct babel_iface *ifa, ip_addr dest) |
1119 |
{ |
1120 |
struct babel_proto *p = ifa->proto;
|
1121 |
struct babel_msg_node *msgn = sl_alloc(p->msg_slab);
|
1122 |
list queue; |
1123 |
|
1124 |
msgn->msg = *msg; |
1125 |
init_list(&queue); |
1126 |
add_tail(&queue, NODE msgn); |
1127 |
babel_write_queue(ifa, &queue); |
1128 |
babel_send_to(ifa, dest); |
1129 |
|
1130 |
/* We could overwrite waiting packet here, we may have to kick TX queue */
|
1131 |
if (!EMPTY_LIST(ifa->msg_queue))
|
1132 |
babel_kick_queue(ifa); |
1133 |
} |
1134 |
|
1135 |
/**
|
1136 |
* babel_enqueue - enqueue a TLV for transmission on an interface
|
1137 |
* @msg: TLV to enqueue (in internal TLV format)
|
1138 |
* @ifa: Interface to enqueue to
|
1139 |
*
|
1140 |
* This function is called to enqueue a TLV for subsequent transmission on an
|
1141 |
* interface. The transmission event is triggered whenever a TLV is enqueued;
|
1142 |
* this ensures that TLVs will be transmitted in a timely manner, but that TLVs
|
1143 |
* which are enqueued in rapid succession can be transmitted together in one
|
1144 |
* packet.
|
1145 |
*/
|
1146 |
void
|
1147 |
babel_enqueue(union babel_msg *msg, struct babel_iface *ifa) |
1148 |
{ |
1149 |
struct babel_proto *p = ifa->proto;
|
1150 |
struct babel_msg_node *msgn = sl_alloc(p->msg_slab);
|
1151 |
msgn->msg = *msg; |
1152 |
add_tail(&ifa->msg_queue, NODE msgn); |
1153 |
babel_kick_queue(ifa); |
1154 |
} |
1155 |
|
1156 |
/**
|
1157 |
* babel_process_packet - process incoming data packet
|
1158 |
* @pkt: Pointer to the packet data
|
1159 |
* @len: Length of received packet
|
1160 |
* @saddr: Address of packet sender
|
1161 |
* @ifa: Interface packet was received on.
|
1162 |
*
|
1163 |
* This function is the main processing hook of incoming Babel packets. It
|
1164 |
* checks that the packet header is well-formed, then processes the TLVs
|
1165 |
* contained in the packet. This is done in two passes: First all TLVs are
|
1166 |
* parsed into the internal TLV format. If a TLV parser fails, processing of the
|
1167 |
* rest of the packet is aborted.
|
1168 |
*
|
1169 |
* After the parsing step, the TLV handlers are called for each parsed TLV in
|
1170 |
* order.
|
1171 |
*/
|
1172 |
static void |
1173 |
babel_process_packet(struct babel_pkt_header *pkt, int len, |
1174 |
ip_addr saddr, struct babel_iface *ifa)
|
1175 |
{ |
1176 |
struct babel_proto *p = ifa->proto;
|
1177 |
struct babel_tlv *tlv;
|
1178 |
struct babel_msg_node *msg;
|
1179 |
list msgs; |
1180 |
int res;
|
1181 |
|
1182 |
int plen = sizeof(struct babel_pkt_header) + get_u16(&pkt->length); |
1183 |
byte *pos; |
1184 |
byte *end = (byte *)pkt + plen; |
1185 |
|
1186 |
struct babel_parse_state state = {
|
1187 |
.proto = p, |
1188 |
.ifa = ifa, |
1189 |
.saddr = saddr, |
1190 |
.next_hop_ip6 = saddr, |
1191 |
}; |
1192 |
|
1193 |
if ((pkt->magic != BABEL_MAGIC) || (pkt->version != BABEL_VERSION))
|
1194 |
{ |
1195 |
TRACE(D_PACKETS, "Strange packet from %I via %s - magic %d version %d",
|
1196 |
saddr, ifa->iface->name, pkt->magic, pkt->version); |
1197 |
return;
|
1198 |
} |
1199 |
|
1200 |
if (plen > len)
|
1201 |
{ |
1202 |
LOG_PKT("Bad packet from %I via %s - %s (%u)",
|
1203 |
saddr, ifa->iface->name, "length mismatch", plen);
|
1204 |
return;
|
1205 |
} |
1206 |
|
1207 |
TRACE(D_PACKETS, "Packet received from %I via %s",
|
1208 |
saddr, ifa->iface->name); |
1209 |
|
1210 |
init_list(&msgs); |
1211 |
|
1212 |
/* First pass through the packet TLV by TLV, parsing each into internal data
|
1213 |
structures. */
|
1214 |
for (tlv = FIRST_TLV(pkt);
|
1215 |
(byte *)tlv < end; |
1216 |
tlv = NEXT_TLV(tlv)) |
1217 |
{ |
1218 |
/* Ugly special case */
|
1219 |
if (tlv->type == BABEL_TLV_PAD1)
|
1220 |
continue;
|
1221 |
|
1222 |
/* The end of the common TLV header */
|
1223 |
pos = (byte *)tlv + sizeof(struct babel_tlv); |
1224 |
if ((pos > end) || (pos + tlv->length > end))
|
1225 |
{ |
1226 |
LOG_PKT("Bad TLV from %I via %s type %d pos %d - framing error",
|
1227 |
saddr, ifa->iface->name, tlv->type, (byte *)tlv - (byte *)pkt); |
1228 |
break;
|
1229 |
} |
1230 |
|
1231 |
msg = sl_alloc(p->msg_slab); |
1232 |
res = babel_read_tlv(tlv, &msg->msg, &state); |
1233 |
if (res == PARSE_SUCCESS)
|
1234 |
{ |
1235 |
add_tail(&msgs, NODE msg); |
1236 |
} |
1237 |
else if (res == PARSE_IGNORE) |
1238 |
{ |
1239 |
DBG("Babel: Ignoring TLV of type %d\n", tlv->type);
|
1240 |
sl_free(p->msg_slab, msg); |
1241 |
} |
1242 |
else /* PARSE_ERROR */ |
1243 |
{ |
1244 |
LOG_PKT("Bad TLV from %I via %s type %d pos %d - parse error",
|
1245 |
saddr, ifa->iface->name, tlv->type, (byte *)tlv - (byte *)pkt); |
1246 |
sl_free(p->msg_slab, msg); |
1247 |
break;
|
1248 |
} |
1249 |
} |
1250 |
|
1251 |
/* Parsing done, handle all parsed TLVs */
|
1252 |
WALK_LIST_FIRST(msg, msgs) |
1253 |
{ |
1254 |
if (tlv_data[msg->msg.type].handle_tlv)
|
1255 |
tlv_data[msg->msg.type].handle_tlv(&msg->msg, ifa); |
1256 |
rem_node(NODE msg); |
1257 |
sl_free(p->msg_slab, msg); |
1258 |
} |
1259 |
} |
1260 |
|
1261 |
static void |
1262 |
babel_err_hook(sock *sk, int err)
|
1263 |
{ |
1264 |
struct babel_iface *ifa = sk->data;
|
1265 |
struct babel_proto *p = ifa->proto;
|
1266 |
|
1267 |
log(L_ERR "%s: Socket error on %s: %M", p->p.name, ifa->iface->name, err);
|
1268 |
/* FIXME: Drop queued TLVs here? */
|
1269 |
} |
1270 |
|
1271 |
|
1272 |
static void |
1273 |
babel_tx_hook(sock *sk) |
1274 |
{ |
1275 |
struct babel_iface *ifa = sk->data;
|
1276 |
|
1277 |
DBG("Babel: TX hook called (iface %s, src %I, dst %I)\n",
|
1278 |
sk->iface->name, sk->saddr, sk->daddr); |
1279 |
|
1280 |
babel_send_queue(ifa); |
1281 |
} |
1282 |
|
1283 |
|
1284 |
static int |
1285 |
babel_rx_hook(sock *sk, uint len) |
1286 |
{ |
1287 |
struct babel_iface *ifa = sk->data;
|
1288 |
struct babel_proto *p = ifa->proto;
|
1289 |
const char *err_dsc = NULL; |
1290 |
uint err_val = 0;
|
1291 |
|
1292 |
if (sk->lifindex != ifa->iface->index)
|
1293 |
return 1; |
1294 |
|
1295 |
DBG("Babel: RX hook called (iface %s, src %I, dst %I)\n",
|
1296 |
sk->iface->name, sk->faddr, sk->laddr); |
1297 |
|
1298 |
/* Silently ignore my own packets */
|
1299 |
if (ipa_equal(sk->faddr, sk->saddr))
|
1300 |
return 1; |
1301 |
|
1302 |
if (!ipa_is_link_local(sk->faddr))
|
1303 |
DROP1("wrong src address");
|
1304 |
|
1305 |
if (sk->fport != ifa->cf->port)
|
1306 |
DROP("wrong src port", sk->fport);
|
1307 |
|
1308 |
if (len < sizeof(struct babel_pkt_header)) |
1309 |
DROP("too short", len);
|
1310 |
|
1311 |
if (sk->flags & SKF_TRUNCATED)
|
1312 |
DROP("truncated", len);
|
1313 |
|
1314 |
babel_process_packet((struct babel_pkt_header *) sk->rbuf, len, sk->faddr, ifa);
|
1315 |
return 1; |
1316 |
|
1317 |
drop:
|
1318 |
LOG_PKT("Bad packet from %I via %s - %s (%u)",
|
1319 |
sk->faddr, sk->iface->name, err_dsc, err_val); |
1320 |
return 1; |
1321 |
} |
1322 |
|
1323 |
int
|
1324 |
babel_open_socket(struct babel_iface *ifa)
|
1325 |
{ |
1326 |
struct babel_proto *p = ifa->proto;
|
1327 |
|
1328 |
sock *sk; |
1329 |
sk = sk_new(ifa->pool); |
1330 |
sk->type = SK_UDP; |
1331 |
sk->sport = ifa->cf->port; |
1332 |
sk->dport = ifa->cf->port; |
1333 |
sk->iface = ifa->iface; |
1334 |
sk->saddr = ifa->addr; |
1335 |
|
1336 |
sk->rx_hook = babel_rx_hook; |
1337 |
sk->tx_hook = babel_tx_hook; |
1338 |
sk->err_hook = babel_err_hook; |
1339 |
sk->data = ifa; |
1340 |
|
1341 |
sk->tos = ifa->cf->tx_tos; |
1342 |
sk->priority = ifa->cf->tx_priority; |
1343 |
sk->ttl = 1;
|
1344 |
sk->flags = SKF_LADDR_RX; |
1345 |
|
1346 |
if (sk_open(sk) < 0) |
1347 |
goto err;
|
1348 |
|
1349 |
if (sk_setup_multicast(sk) < 0) |
1350 |
goto err;
|
1351 |
|
1352 |
if (sk_join_group(sk, IP6_BABEL_ROUTERS) < 0) |
1353 |
goto err;
|
1354 |
|
1355 |
ifa->sk = sk; |
1356 |
return 1; |
1357 |
|
1358 |
err:
|
1359 |
sk_log_error(sk, p->p.name); |
1360 |
rfree(sk); |
1361 |
return 0; |
1362 |
} |