2 * BIRD -- The Babel protocol
4 * Copyright (c) 2015--2016 Toke Hoiland-Jorgensen
6 * Can be freely distributed and used under the terms of the GNU GPL.
8 * This file contains the packet and TLV handling code for the protocol.
14 struct babel_pkt_header
{
26 struct babel_tlv_ack_req
{
34 struct babel_tlv_ack
{
40 struct babel_tlv_hello
{
48 struct babel_tlv_ihu
{
58 struct babel_tlv_router_id
{
65 struct babel_tlv_next_hop
{
73 struct babel_tlv_update
{
86 struct babel_tlv_route_request
{
94 struct babel_tlv_seqno_request
{
107 #define BABEL_FLAG_DEF_PREFIX 0x80
108 #define BABEL_FLAG_ROUTER_ID 0x40
111 struct babel_parse_state
{
112 struct babel_proto
*proto
;
113 struct babel_iface
*ifa
;
116 u64 router_id
; /* Router ID used in subsequent updates */
117 u8 def_ip6_prefix
[16]; /* Implicit IPv6 prefix in network order */
118 u8 def_ip4_prefix
[4]; /* Implicit IPv4 prefix in network order */
119 u8 router_id_seen
; /* router_id field is valid */
120 u8 def_ip6_prefix_seen
; /* def_ip6_prefix is valid */
121 u8 def_ip4_prefix_seen
; /* def_ip4_prefix is valid */
130 struct babel_write_state
{
137 #define DROP(DSC,VAL) do { err_dsc = DSC; err_val = VAL; goto drop; } while(0)
138 #define DROP1(DSC) do { err_dsc = DSC; goto drop; } while(0)
139 #define LOG_PKT(msg, args...) \
140 log_rl(&p->log_pkt_tbf, L_REMOTE "%s: " msg, p->p.name, args)
142 #define FIRST_TLV(p) ((struct babel_tlv *) (((struct babel_pkt_header *) p) + 1))
143 #define NEXT_TLV(t) ((struct babel_tlv *) (((byte *) t) + TLV_LENGTH(t)))
144 #define TLV_LENGTH(t) (t->type == BABEL_TLV_PAD1 ? 1 : t->length + sizeof(struct babel_tlv))
145 #define TLV_OPT_LENGTH(t) (t->length + sizeof(struct babel_tlv) - sizeof(*t))
146 #define TLV_HDR(tlv,t,l) ({ tlv->type = t; tlv->length = l - sizeof(struct babel_tlv); })
147 #define TLV_HDR0(tlv,t) TLV_HDR(tlv, t, tlv_data[t].min_length)
151 get_time16(const void *p
)
153 u16 v
= get_u16(p
) / BABEL_TIME_UNITS
;
158 put_time16(void *p
, u16 v
)
160 put_u16(p
, v
* BABEL_TIME_UNITS
);
163 static inline ip6_addr
164 get_ip6_px(const void *p
, int plen
)
166 ip6_addr addr
= IPA_NONE
;
167 memcpy(&addr
, p
, (plen
+ 7) / 8);
168 return ip6_ntoh(addr
);
172 put_ip6_px(void *p
, ip6_addr addr
, int plen
)
174 addr
= ip6_hton(addr
);
175 memcpy(p
, &addr
, (plen
+ 7) / 8);
178 static inline ip6_addr
179 get_ip6_ll(const void *p
)
181 return ip6_build(0xfe800000, 0, get_u32(p
+0), get_u32(p
+4));
185 put_ip6_ll(void *p
, ip6_addr addr
)
187 put_u32(p
+0, _I2(addr
));
188 put_u32(p
+4, _I3(addr
));
193 * TLV read/write functions
196 static int babel_read_ack_req(struct babel_tlv
*hdr
, union babel_msg
*msg
, struct babel_parse_state
*state
);
197 static int babel_read_hello(struct babel_tlv
*hdr
, union babel_msg
*msg
, struct babel_parse_state
*state
);
198 static int babel_read_ihu(struct babel_tlv
*hdr
, union babel_msg
*msg
, struct babel_parse_state
*state
);
199 static int babel_read_router_id(struct babel_tlv
*hdr
, union babel_msg
*msg
, struct babel_parse_state
*state
);
200 static int babel_read_next_hop(struct babel_tlv
*hdr
, union babel_msg
*msg
, struct babel_parse_state
*state
);
201 static int babel_read_update(struct babel_tlv
*hdr
, union babel_msg
*msg
, struct babel_parse_state
*state
);
202 static int babel_read_route_request(struct babel_tlv
*hdr
, union babel_msg
*msg
, struct babel_parse_state
*state
);
203 static int babel_read_seqno_request(struct babel_tlv
*hdr
, union babel_msg
*msg
, struct babel_parse_state
*state
);
205 static int babel_write_ack(struct babel_tlv
*hdr
, union babel_msg
*msg
, struct babel_write_state
*state
, int max_len
);
206 static int babel_write_hello(struct babel_tlv
*hdr
, union babel_msg
*msg
, struct babel_write_state
*state
, int max_len
);
207 static int babel_write_ihu(struct babel_tlv
*hdr
, union babel_msg
*msg
, struct babel_write_state
*state
, int max_len
);
208 static int babel_write_update(struct babel_tlv
*hdr
, union babel_msg
*msg
, struct babel_write_state
*state
, int max_len
);
209 static int babel_write_route_request(struct babel_tlv
*hdr
, union babel_msg
*msg
, struct babel_write_state
*state
, int max_len
);
210 static int babel_write_seqno_request(struct babel_tlv
*hdr
, union babel_msg
*msg
, struct babel_write_state
*state
, int max_len
);
212 struct babel_tlv_data
{
214 int (*read_tlv
)(struct babel_tlv
*hdr
, union babel_msg
*m
, struct babel_parse_state
*state
);
215 int (*write_tlv
)(struct babel_tlv
*hdr
, union babel_msg
*m
, struct babel_write_state
*state
, int max_len
);
216 void (*handle_tlv
)(union babel_msg
*m
, struct babel_iface
*ifa
);
219 const static struct babel_tlv_data tlv_data
[BABEL_TLV_MAX
] = {
220 [BABEL_TLV_ACK_REQ
] = {
221 sizeof(struct babel_tlv_ack_req
),
227 sizeof(struct babel_tlv_ack
),
232 [BABEL_TLV_HELLO
] = {
233 sizeof(struct babel_tlv_hello
),
239 sizeof(struct babel_tlv_ihu
),
244 [BABEL_TLV_ROUTER_ID
] = {
245 sizeof(struct babel_tlv_router_id
),
246 babel_read_router_id
,
250 [BABEL_TLV_NEXT_HOP
] = {
251 sizeof(struct babel_tlv_next_hop
),
256 [BABEL_TLV_UPDATE
] = {
257 sizeof(struct babel_tlv_update
),
262 [BABEL_TLV_ROUTE_REQUEST
] = {
263 sizeof(struct babel_tlv_route_request
),
264 babel_read_route_request
,
265 babel_write_route_request
,
266 babel_handle_route_request
268 [BABEL_TLV_SEQNO_REQUEST
] = {
269 sizeof(struct babel_tlv_seqno_request
),
270 babel_read_seqno_request
,
271 babel_write_seqno_request
,
272 babel_handle_seqno_request
277 babel_read_ack_req(struct babel_tlv
*hdr
, union babel_msg
*m
,
278 struct babel_parse_state
*state
)
280 struct babel_tlv_ack_req
*tlv
= (void *) hdr
;
281 struct babel_msg_ack_req
*msg
= &m
->ack_req
;
283 msg
->type
= BABEL_TLV_ACK_REQ
;
284 msg
->nonce
= get_u16(&tlv
->nonce
);
285 msg
->interval
= get_time16(&tlv
->interval
);
286 msg
->sender
= state
->saddr
;
291 return PARSE_SUCCESS
;
295 babel_write_ack(struct babel_tlv
*hdr
, union babel_msg
*m
,
296 struct babel_write_state
*state
, int max_len
)
298 struct babel_tlv_ack
*tlv
= (void *) hdr
;
299 struct babel_msg_ack
*msg
= &m
->ack
;
301 TLV_HDR0(tlv
, BABEL_TLV_ACK
);
302 put_u16(&tlv
->nonce
, msg
->nonce
);
304 return sizeof(struct babel_tlv_ack
);
308 babel_read_hello(struct babel_tlv
*hdr
, union babel_msg
*m
,
309 struct babel_parse_state
*state
)
311 struct babel_tlv_hello
*tlv
= (void *) hdr
;
312 struct babel_msg_hello
*msg
= &m
->hello
;
314 msg
->type
= BABEL_TLV_HELLO
;
315 msg
->seqno
= get_u16(&tlv
->seqno
);
316 msg
->interval
= get_time16(&tlv
->interval
);
317 msg
->sender
= state
->saddr
;
319 return PARSE_SUCCESS
;
323 babel_write_hello(struct babel_tlv
*hdr
, union babel_msg
*m
,
324 struct babel_write_state
*state
, int max_len
)
326 struct babel_tlv_hello
*tlv
= (void *) hdr
;
327 struct babel_msg_hello
*msg
= &m
->hello
;
329 TLV_HDR0(tlv
, BABEL_TLV_HELLO
);
330 put_u16(&tlv
->seqno
, msg
->seqno
);
331 put_time16(&tlv
->interval
, msg
->interval
);
333 return sizeof(struct babel_tlv_hello
);
337 babel_read_ihu(struct babel_tlv
*hdr
, union babel_msg
*m
,
338 struct babel_parse_state
*state
)
340 struct babel_tlv_ihu
*tlv
= (void *) hdr
;
341 struct babel_msg_ihu
*msg
= &m
->ihu
;
343 msg
->type
= BABEL_TLV_IHU
;
345 msg
->rxcost
= get_u16(&tlv
->rxcost
);
346 msg
->interval
= get_time16(&tlv
->interval
);
347 msg
->addr
= IPA_NONE
;
348 msg
->sender
= state
->saddr
;
350 if (msg
->ae
>= BABEL_AE_MAX
)
353 // We handle link-local IPs. In every other case, the addr field will be 0 but
354 // validation will succeed. The handler takes care of these cases.
355 if (msg
->ae
== BABEL_AE_IP6_LL
)
357 if (TLV_OPT_LENGTH(tlv
) < 8)
360 msg
->addr
= ipa_from_ip6(get_ip6_ll(&tlv
->addr
));
363 return PARSE_SUCCESS
;
367 babel_write_ihu(struct babel_tlv
*hdr
, union babel_msg
*m
,
368 struct babel_write_state
*state
, int max_len
)
370 struct babel_tlv_ihu
*tlv
= (void *) hdr
;
371 struct babel_msg_ihu
*msg
= &m
->ihu
;
373 if (ipa_is_link_local(msg
->addr
) && max_len
< sizeof(struct babel_tlv_ihu
) + 8)
376 TLV_HDR0(tlv
, BABEL_TLV_IHU
);
377 put_u16(&tlv
->rxcost
, msg
->rxcost
);
378 put_time16(&tlv
->interval
, msg
->interval
);
380 if (!ipa_is_link_local(msg
->addr
))
382 tlv
->ae
= BABEL_AE_WILDCARD
;
383 return sizeof(struct babel_tlv_ihu
);
385 put_ip6_ll(&tlv
->addr
, msg
->addr
);
386 tlv
->ae
= BABEL_AE_IP6_LL
;
388 return sizeof(struct babel_tlv_ihu
) + 8;
392 babel_read_router_id(struct babel_tlv
*hdr
, union babel_msg
*m UNUSED
,
393 struct babel_parse_state
*state
)
395 struct babel_tlv_router_id
*tlv
= (void *) hdr
;
397 state
->router_id
= get_u64(&tlv
->router_id
);
398 state
->router_id_seen
= 1;
403 /* This is called directly from babel_write_update() */
405 babel_write_router_id(struct babel_tlv
*hdr
, u64 router_id
,
406 struct babel_write_state
*state
, int max_len UNUSED
)
408 struct babel_tlv_router_id
*tlv
= (void *) hdr
;
410 /* We still assume that first min_length bytes are available and zeroed */
412 TLV_HDR0(tlv
, BABEL_TLV_ROUTER_ID
);
413 put_u64(&tlv
->router_id
, router_id
);
415 state
->router_id
= router_id
;
416 state
->router_id_seen
= 1;
418 return sizeof(struct babel_tlv_router_id
);
422 babel_read_next_hop(struct babel_tlv
*hdr
, union babel_msg
*m UNUSED
,
423 struct babel_parse_state
*state
)
425 struct babel_tlv_next_hop
*tlv
= (void *) hdr
;
429 case BABEL_AE_WILDCARD
:
437 if (TLV_OPT_LENGTH(tlv
) < sizeof(ip6_addr
))
440 state
->next_hop
= ipa_from_ip6(get_ip6(&tlv
->addr
));
443 case BABEL_AE_IP6_LL
:
444 if (TLV_OPT_LENGTH(tlv
) < 8)
447 state
->next_hop
= ipa_from_ip6(get_ip6_ll(&tlv
->addr
));
458 babel_read_update(struct babel_tlv
*hdr
, union babel_msg
*m
,
459 struct babel_parse_state
*state
)
461 struct babel_tlv_update
*tlv
= (void *) hdr
;
462 struct babel_msg_update
*msg
= &m
->update
;
464 msg
->type
= BABEL_TLV_UPDATE
;
466 msg
->interval
= get_time16(&tlv
->interval
);
467 msg
->seqno
= get_u16(&tlv
->seqno
);
468 msg
->metric
= get_u16(&tlv
->metric
);
470 /* Length of received prefix data without omitted part */
471 int len
= (tlv
->plen
+ 7)/8 - (int) tlv
->omitted
;
474 if ((len
< 0) || (len
> TLV_OPT_LENGTH(tlv
)))
479 case BABEL_AE_WILDCARD
:
483 msg
->prefix
= IPA_NONE
;
491 if (tlv
->plen
> MAX_PREFIX_LENGTH
)
494 /* Cannot omit data if there is no saved prefix */
495 if (tlv
->omitted
&& !state
->def_ip6_prefix_seen
)
498 /* Merge saved prefix and received prefix parts */
499 memcpy(buf
, state
->def_ip6_prefix
, tlv
->omitted
);
500 memcpy(buf
+ tlv
->omitted
, tlv
->addr
, len
);
502 msg
->plen
= tlv
->plen
;
503 msg
->prefix
= ipa_from_ip6(get_ip6(buf
));
505 if (tlv
->flags
& BABEL_FLAG_DEF_PREFIX
)
507 put_ip6(state
->def_ip6_prefix
, msg
->prefix
);
508 state
->def_ip6_prefix_seen
= 1;
511 if (tlv
->flags
& BABEL_FLAG_ROUTER_ID
)
513 state
->router_id
= ((u64
) _I2(msg
->prefix
)) << 32 | _I3(msg
->prefix
);
514 state
->router_id_seen
= 1;
518 case BABEL_AE_IP6_LL
:
526 if (!state
->router_id_seen
)
528 DBG("Babel: No router ID seen before update\n");
532 msg
->router_id
= state
->router_id
;
533 msg
->next_hop
= state
->next_hop
;
534 msg
->sender
= state
->saddr
;
536 return PARSE_SUCCESS
;
540 babel_write_update(struct babel_tlv
*hdr
, union babel_msg
*m
,
541 struct babel_write_state
*state
, int max_len
)
543 struct babel_tlv_update
*tlv
= (void *) hdr
;
544 struct babel_msg_update
*msg
= &m
->update
;
548 * When needed, we write Router-ID TLV before Update TLV and return size of
549 * both of them. There is enough space for the Router-ID TLV, because
550 * sizeof(struct babel_tlv_router_id) == sizeof(struct babel_tlv_update).
552 if (!state
->router_id_seen
|| (msg
->router_id
!= state
->router_id
))
554 len0
= babel_write_router_id(hdr
, msg
->router_id
, state
, max_len
);
555 tlv
= (struct babel_tlv_update
*) NEXT_TLV(tlv
);
558 int len
= sizeof(struct babel_tlv_update
) + (msg
->plen
+ 7)/8;
560 if (len0
+ len
> max_len
)
563 memset(tlv
, 0, sizeof(struct babel_tlv_update
));
564 TLV_HDR(tlv
, BABEL_TLV_UPDATE
, len
);
565 tlv
->ae
= BABEL_AE_IP6
;
566 tlv
->plen
= msg
->plen
;
567 put_time16(&tlv
->interval
, msg
->interval
);
568 put_u16(&tlv
->seqno
, msg
->seqno
);
569 put_u16(&tlv
->metric
, msg
->metric
);
570 put_ip6_px(tlv
->addr
, msg
->prefix
, msg
->plen
);
576 babel_read_route_request(struct babel_tlv
*hdr
, union babel_msg
*m
,
577 struct babel_parse_state
*state
)
579 struct babel_tlv_route_request
*tlv
= (void *) hdr
;
580 struct babel_msg_route_request
*msg
= &m
->route_request
;
582 msg
->type
= BABEL_TLV_ROUTE_REQUEST
;
586 case BABEL_AE_WILDCARD
:
587 /* Wildcard requests must have plen 0 */
592 return PARSE_SUCCESS
;
599 if (tlv
->plen
> MAX_PREFIX_LENGTH
)
602 if (TLV_OPT_LENGTH(tlv
) < (tlv
->plen
+ 7)/8)
605 msg
->plen
= tlv
->plen
;
606 msg
->prefix
= get_ip6_px(tlv
->addr
, tlv
->plen
);
607 return PARSE_SUCCESS
;
609 case BABEL_AE_IP6_LL
:
620 babel_write_route_request(struct babel_tlv
*hdr
, union babel_msg
*m
,
621 struct babel_write_state
*state
, int max_len
)
623 struct babel_tlv_route_request
*tlv
= (void *) hdr
;
624 struct babel_msg_route_request
*msg
= &m
->route_request
;
626 int len
= sizeof(struct babel_tlv_route_request
) + (msg
->plen
+ 7)/8;
631 TLV_HDR(tlv
, BABEL_TLV_ROUTE_REQUEST
, len
);
635 tlv
->ae
= BABEL_AE_WILDCARD
;
640 tlv
->ae
= BABEL_AE_IP6
;
641 tlv
->plen
= msg
->plen
;
642 put_ip6_px(tlv
->addr
, msg
->prefix
, msg
->plen
);
649 babel_read_seqno_request(struct babel_tlv
*hdr
, union babel_msg
*m
,
650 struct babel_parse_state
*state
)
652 struct babel_tlv_seqno_request
*tlv
= (void *) hdr
;
653 struct babel_msg_seqno_request
*msg
= &m
->seqno_request
;
655 msg
->type
= BABEL_TLV_SEQNO_REQUEST
;
656 msg
->seqno
= get_u16(&tlv
->seqno
);
657 msg
->hop_count
= tlv
->hop_count
;
658 msg
->router_id
= get_u64(&tlv
->router_id
);
659 msg
->sender
= state
->saddr
;
661 if (tlv
->hop_count
== 0)
666 case BABEL_AE_WILDCARD
:
674 if (tlv
->plen
> MAX_PREFIX_LENGTH
)
677 if (TLV_OPT_LENGTH(tlv
) < (tlv
->plen
+ 7)/8)
680 msg
->plen
= tlv
->plen
;
681 msg
->prefix
= get_ip6_px(tlv
->addr
, tlv
->plen
);
682 return PARSE_SUCCESS
;
684 case BABEL_AE_IP6_LL
:
695 babel_write_seqno_request(struct babel_tlv
*hdr
, union babel_msg
*m
,
696 struct babel_write_state
*state
, int max_len
)
698 struct babel_tlv_seqno_request
*tlv
= (void *) hdr
;
699 struct babel_msg_seqno_request
*msg
= &m
->seqno_request
;
701 int len
= sizeof(struct babel_tlv_seqno_request
) + (msg
->plen
+ 7)/8;
706 TLV_HDR(tlv
, BABEL_TLV_SEQNO_REQUEST
, len
);
707 tlv
->ae
= BABEL_AE_IP6
;
708 tlv
->plen
= msg
->plen
;
709 put_u16(&tlv
->seqno
, msg
->seqno
);
710 tlv
->hop_count
= msg
->hop_count
;
711 put_u64(&tlv
->router_id
, msg
->router_id
);
712 put_ip6_px(tlv
->addr
, msg
->prefix
, msg
->plen
);
718 babel_read_tlv(struct babel_tlv
*hdr
,
719 union babel_msg
*msg
,
720 struct babel_parse_state
*state
)
722 if ((hdr
->type
<= BABEL_TLV_PADN
) ||
723 (hdr
->type
>= BABEL_TLV_MAX
) ||
724 !tlv_data
[hdr
->type
].read_tlv
)
727 if (TLV_LENGTH(hdr
) < tlv_data
[hdr
->type
].min_length
)
730 memset(msg
, 0, sizeof(*msg
));
731 return tlv_data
[hdr
->type
].read_tlv(hdr
, msg
, state
);
735 babel_write_tlv(struct babel_tlv
*hdr
,
736 union babel_msg
*msg
,
737 struct babel_write_state
*state
,
740 if ((msg
->type
<= BABEL_TLV_PADN
) ||
741 (msg
->type
>= BABEL_TLV_MAX
) ||
742 !tlv_data
[msg
->type
].write_tlv
)
745 if (tlv_data
[msg
->type
].min_length
> max_len
)
748 memset(hdr
, 0, tlv_data
[msg
->type
].min_length
);
749 return tlv_data
[msg
->type
].write_tlv(hdr
, msg
, state
, max_len
);
754 * Packet RX/TX functions
758 babel_send_to(struct babel_iface
*ifa
, ip_addr dest
)
761 struct babel_pkt_header
*hdr
= (void *) sk
->tbuf
;
762 int len
= get_u16(&hdr
->length
) + sizeof(struct babel_pkt_header
);
764 DBG("Babel: Sending %d bytes to %I\n", len
, dest
);
765 return sk_send_to(sk
, len
, dest
, 0);
769 * babel_write_queue - Write a TLV queue to a transmission buffer
770 * @ifa: Interface holding the transmission buffer
771 * @queue: TLV queue to write (containing internal-format TLVs)
773 * This function writes a packet to the interface transmission buffer with as
774 * many TLVs from the &queue as will fit in the buffer. It returns the number of
775 * bytes written (NOT counting the packet header). The function is called by
776 * babel_send_queue() and babel_send_unicast() to construct packets for
777 * transmission, and uses per-TLV helper functions to convert the
778 * internal-format TLVs to their wire representations.
780 * The TLVs in the queue are freed after they are written to the buffer.
783 babel_write_queue(struct babel_iface
*ifa
, list
*queue
)
785 struct babel_proto
*p
= ifa
->proto
;
786 struct babel_write_state state
= {};
788 if (EMPTY_LIST(*queue
))
791 byte
*pos
= ifa
->sk
->tbuf
;
792 byte
*end
= pos
+ ifa
->tx_length
;
794 struct babel_pkt_header
*pkt
= (void *) pos
;
795 pkt
->magic
= BABEL_MAGIC
;
796 pkt
->version
= BABEL_VERSION
;
798 pos
+= sizeof(struct babel_pkt_header
);
800 struct babel_msg_node
*msg
;
801 WALK_LIST_FIRST(msg
, *queue
)
803 int len
= babel_write_tlv((struct babel_tlv
*) pos
, &msg
->msg
, &state
, end
- pos
);
810 sl_free(p
->msg_slab
, msg
);
813 int plen
= pos
- (byte
*) pkt
;
814 put_u16(&pkt
->length
, plen
- sizeof(struct babel_pkt_header
));
820 babel_send_queue(void *arg
)
822 struct babel_iface
*ifa
= arg
;
823 while ((babel_write_queue(ifa
, &ifa
->msg_queue
) > 0) &&
824 (babel_send_to(ifa
, IP6_BABEL_ROUTERS
) > 0));
828 babel_kick_queue(struct babel_iface
*ifa
)
831 * Only schedule send event if there is not already data in the socket buffer.
832 * Otherwise we may overwrite the data already in the buffer.
835 if ((ifa
->sk
->tpos
== ifa
->sk
->tbuf
) && !ev_active(ifa
->send_event
))
836 ev_schedule(ifa
->send_event
);
840 * babel_send_unicast - send a single TLV via unicast to a destination
842 * @ifa: Interface to send via
843 * @dest: Destination of the TLV
845 * This function is used to send a single TLV via unicast to a designated
846 * receiver. This is used for replying to certain incoming requests, and for
847 * sending unicast requests to refresh routes before they expire.
850 babel_send_unicast(union babel_msg
*msg
, struct babel_iface
*ifa
, ip_addr dest
)
852 struct babel_proto
*p
= ifa
->proto
;
853 struct babel_msg_node
*msgn
= sl_alloc(p
->msg_slab
);
858 add_tail(&queue
, NODE msgn
);
859 babel_write_queue(ifa
, &queue
);
860 babel_send_to(ifa
, dest
);
862 /* We could overwrite waiting packet here, we may have to kick TX queue */
863 if (!EMPTY_LIST(ifa
->msg_queue
))
864 babel_kick_queue(ifa
);
868 * babel_enqueue - enqueue a TLV for transmission on an interface
869 * @msg: TLV to enqueue (in internal TLV format)
870 * @ifa: Interface to enqueue to
872 * This function is called to enqueue a TLV for subsequent transmission on an
873 * interface. The transmission event is triggered whenever a TLV is enqueued;
874 * this ensures that TLVs will be transmitted in a timely manner, but that TLVs
875 * which are enqueued in rapid succession can be transmitted together in one
879 babel_enqueue(union babel_msg
*msg
, struct babel_iface
*ifa
)
881 struct babel_proto
*p
= ifa
->proto
;
882 struct babel_msg_node
*msgn
= sl_alloc(p
->msg_slab
);
884 add_tail(&ifa
->msg_queue
, NODE msgn
);
885 babel_kick_queue(ifa
);
889 * babel_process_packet - process incoming data packet
890 * @pkt: Pointer to the packet data
891 * @len: Length of received packet
892 * @saddr: Address of packet sender
893 * @ifa: Interface packet was received on.
895 * This function is the main processing hook of incoming Babel packets. It
896 * checks that the packet header is well-formed, then processes the TLVs
897 * contained in the packet. This is done in two passes: First all TLVs are
898 * parsed into the internal TLV format. If a TLV parser fails, processing of the
899 * rest of the packet is aborted.
901 * After the parsing step, the TLV handlers are called for each parsed TLV in
905 babel_process_packet(struct babel_pkt_header
*pkt
, int len
,
906 ip_addr saddr
, struct babel_iface
*ifa
)
908 struct babel_proto
*p
= ifa
->proto
;
909 struct babel_tlv
*tlv
;
910 struct babel_msg_node
*msg
;
914 int plen
= sizeof(struct babel_pkt_header
) + get_u16(&pkt
->length
);
916 byte
*end
= (byte
*)pkt
+ plen
;
918 struct babel_parse_state state
= {
925 if ((pkt
->magic
!= BABEL_MAGIC
) || (pkt
->version
!= BABEL_VERSION
))
927 TRACE(D_PACKETS
, "Strange packet from %I via %s - magic %d version %d",
928 saddr
, ifa
->iface
->name
, pkt
->magic
, pkt
->version
);
934 LOG_PKT("Bad packet from %I via %s - %s (%u)",
935 saddr
, ifa
->iface
->name
, "length mismatch", plen
);
939 TRACE(D_PACKETS
, "Packet received from %I via %s",
940 saddr
, ifa
->iface
->name
);
944 /* First pass through the packet TLV by TLV, parsing each into internal data
946 for (tlv
= FIRST_TLV(pkt
);
950 /* Ugly special case */
951 if (tlv
->type
== BABEL_TLV_PAD1
)
954 /* The end of the common TLV header */
955 pos
= (byte
*)tlv
+ sizeof(struct babel_tlv
);
956 if ((pos
> end
) || (pos
+ tlv
->length
> end
))
958 LOG_PKT("Bad TLV from %I via %s type %d pos %d - framing error",
959 saddr
, ifa
->iface
->name
, tlv
->type
, (byte
*)tlv
- (byte
*)pkt
);
963 msg
= sl_alloc(p
->msg_slab
);
964 res
= babel_read_tlv(tlv
, &msg
->msg
, &state
);
965 if (res
== PARSE_SUCCESS
)
967 add_tail(&msgs
, NODE msg
);
969 else if (res
== PARSE_IGNORE
)
971 DBG("Babel: Ignoring TLV of type %d\n", tlv
->type
);
972 sl_free(p
->msg_slab
, msg
);
974 else /* PARSE_ERROR */
976 LOG_PKT("Bad TLV from %I via %s type %d pos %d - parse error",
977 saddr
, ifa
->iface
->name
, tlv
->type
, (byte
*)tlv
- (byte
*)pkt
);
978 sl_free(p
->msg_slab
, msg
);
983 /* Parsing done, handle all parsed TLVs */
984 WALK_LIST_FIRST(msg
, msgs
)
986 if (tlv_data
[msg
->msg
.type
].handle_tlv
)
987 tlv_data
[msg
->msg
.type
].handle_tlv(&msg
->msg
, ifa
);
989 sl_free(p
->msg_slab
, msg
);
994 babel_err_hook(sock
*sk
, int err
)
996 struct babel_iface
*ifa
= sk
->data
;
997 struct babel_proto
*p
= ifa
->proto
;
999 log(L_ERR
"%s: Socket error on %s: %M", p
->p
.name
, ifa
->iface
->name
, err
);
1000 /* FIXME: Drop queued TLVs here? */
1005 babel_tx_hook(sock
*sk
)
1007 struct babel_iface
*ifa
= sk
->data
;
1009 DBG("Babel: TX hook called (iface %s, src %I, dst %I)\n",
1010 sk
->iface
->name
, sk
->saddr
, sk
->daddr
);
1012 babel_send_queue(ifa
);
1017 babel_rx_hook(sock
*sk
, int len
)
1019 struct babel_iface
*ifa
= sk
->data
;
1020 struct babel_proto
*p
= ifa
->proto
;
1021 const char *err_dsc
= NULL
;
1024 if (sk
->lifindex
!= ifa
->iface
->index
)
1027 DBG("Babel: RX hook called (iface %s, src %I, dst %I)\n",
1028 sk
->iface
->name
, sk
->faddr
, sk
->laddr
);
1030 /* Silently ignore my own packets */
1031 if (ipa_equal(ifa
->iface
->addr
->ip
, sk
->faddr
))
1034 if (!ipa_is_link_local(sk
->faddr
))
1035 DROP1("wrong src address");
1037 if (sk
->fport
!= ifa
->cf
->port
)
1038 DROP("wrong src port", sk
->fport
);
1040 if (len
< sizeof(struct babel_pkt_header
))
1041 DROP("too short", len
);
1043 if (sk
->flags
& SKF_TRUNCATED
)
1044 DROP("truncated", len
);
1046 babel_process_packet((struct babel_pkt_header
*) sk
->rbuf
, len
, sk
->faddr
, ifa
);
1050 LOG_PKT("Bad packet from %I via %s - %s (%u)",
1051 sk
->faddr
, sk
->iface
->name
, err_dsc
, err_val
);
1056 babel_open_socket(struct babel_iface
*ifa
)
1058 struct babel_proto
*p
= ifa
->proto
;
1061 sk
= sk_new(ifa
->pool
);
1063 sk
->sport
= ifa
->cf
->port
;
1064 sk
->dport
= ifa
->cf
->port
;
1065 sk
->iface
= ifa
->iface
;
1067 sk
->rx_hook
= babel_rx_hook
;
1068 sk
->tx_hook
= babel_tx_hook
;
1069 sk
->err_hook
= babel_err_hook
;
1072 sk
->tos
= ifa
->cf
->tx_tos
;
1073 sk
->priority
= ifa
->cf
->tx_priority
;
1075 sk
->flags
= SKF_LADDR_RX
;
1077 if (sk_open(sk
) < 0)
1080 if (sk_setup_multicast(sk
) < 0)
1083 if (sk_join_group(sk
, IP6_BABEL_ROUTERS
) < 0)
1090 sk_log_error(sk
, p
->p
.name
);