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
:
484 msg
->prefix
= IPA_NONE
;
492 if (tlv
->plen
> MAX_PREFIX_LENGTH
)
495 /* Cannot omit data if there is no saved prefix */
496 if (tlv
->omitted
&& !state
->def_ip6_prefix_seen
)
499 /* Merge saved prefix and received prefix parts */
500 memcpy(buf
, state
->def_ip6_prefix
, tlv
->omitted
);
501 memcpy(buf
+ tlv
->omitted
, tlv
->addr
, len
);
503 msg
->plen
= tlv
->plen
;
504 msg
->prefix
= ipa_from_ip6(get_ip6(buf
));
506 if (tlv
->flags
& BABEL_FLAG_DEF_PREFIX
)
508 put_ip6(state
->def_ip6_prefix
, msg
->prefix
);
509 state
->def_ip6_prefix_seen
= 1;
512 if (tlv
->flags
& BABEL_FLAG_ROUTER_ID
)
514 state
->router_id
= ((u64
) _I2(msg
->prefix
)) << 32 | _I3(msg
->prefix
);
515 state
->router_id_seen
= 1;
519 case BABEL_AE_IP6_LL
:
527 /* Update must have Router ID, unless it is retraction */
528 if (!state
->router_id_seen
&& (msg
->metric
!= BABEL_INFINITY
))
530 DBG("Babel: No router ID seen before update\n");
534 msg
->router_id
= state
->router_id
;
535 msg
->next_hop
= state
->next_hop
;
536 msg
->sender
= state
->saddr
;
538 return PARSE_SUCCESS
;
542 babel_write_update(struct babel_tlv
*hdr
, union babel_msg
*m
,
543 struct babel_write_state
*state
, int max_len
)
545 struct babel_tlv_update
*tlv
= (void *) hdr
;
546 struct babel_msg_update
*msg
= &m
->update
;
550 * When needed, we write Router-ID TLV before Update TLV and return size of
551 * both of them. There is enough space for the Router-ID TLV, because
552 * sizeof(struct babel_tlv_router_id) == sizeof(struct babel_tlv_update).
554 if (!state
->router_id_seen
|| (msg
->router_id
!= state
->router_id
))
556 len0
= babel_write_router_id(hdr
, msg
->router_id
, state
, max_len
);
557 tlv
= (struct babel_tlv_update
*) NEXT_TLV(tlv
);
560 int len
= sizeof(struct babel_tlv_update
) + (msg
->plen
+ 7)/8;
562 if (len0
+ len
> max_len
)
565 memset(tlv
, 0, sizeof(struct babel_tlv_update
));
566 TLV_HDR(tlv
, BABEL_TLV_UPDATE
, len
);
567 tlv
->ae
= BABEL_AE_IP6
;
568 tlv
->plen
= msg
->plen
;
569 put_time16(&tlv
->interval
, msg
->interval
);
570 put_u16(&tlv
->seqno
, msg
->seqno
);
571 put_u16(&tlv
->metric
, msg
->metric
);
572 put_ip6_px(tlv
->addr
, msg
->prefix
, msg
->plen
);
578 babel_read_route_request(struct babel_tlv
*hdr
, union babel_msg
*m
,
579 struct babel_parse_state
*state
)
581 struct babel_tlv_route_request
*tlv
= (void *) hdr
;
582 struct babel_msg_route_request
*msg
= &m
->route_request
;
584 msg
->type
= BABEL_TLV_ROUTE_REQUEST
;
588 case BABEL_AE_WILDCARD
:
589 /* Wildcard requests must have plen 0 */
594 return PARSE_SUCCESS
;
601 if (tlv
->plen
> MAX_PREFIX_LENGTH
)
604 if (TLV_OPT_LENGTH(tlv
) < (tlv
->plen
+ 7)/8)
607 msg
->plen
= tlv
->plen
;
608 msg
->prefix
= get_ip6_px(tlv
->addr
, tlv
->plen
);
609 return PARSE_SUCCESS
;
611 case BABEL_AE_IP6_LL
:
622 babel_write_route_request(struct babel_tlv
*hdr
, union babel_msg
*m
,
623 struct babel_write_state
*state
, int max_len
)
625 struct babel_tlv_route_request
*tlv
= (void *) hdr
;
626 struct babel_msg_route_request
*msg
= &m
->route_request
;
628 int len
= sizeof(struct babel_tlv_route_request
) + (msg
->plen
+ 7)/8;
633 TLV_HDR(tlv
, BABEL_TLV_ROUTE_REQUEST
, len
);
637 tlv
->ae
= BABEL_AE_WILDCARD
;
642 tlv
->ae
= BABEL_AE_IP6
;
643 tlv
->plen
= msg
->plen
;
644 put_ip6_px(tlv
->addr
, msg
->prefix
, msg
->plen
);
651 babel_read_seqno_request(struct babel_tlv
*hdr
, union babel_msg
*m
,
652 struct babel_parse_state
*state
)
654 struct babel_tlv_seqno_request
*tlv
= (void *) hdr
;
655 struct babel_msg_seqno_request
*msg
= &m
->seqno_request
;
657 msg
->type
= BABEL_TLV_SEQNO_REQUEST
;
658 msg
->seqno
= get_u16(&tlv
->seqno
);
659 msg
->hop_count
= tlv
->hop_count
;
660 msg
->router_id
= get_u64(&tlv
->router_id
);
661 msg
->sender
= state
->saddr
;
663 if (tlv
->hop_count
== 0)
668 case BABEL_AE_WILDCARD
:
676 if (tlv
->plen
> MAX_PREFIX_LENGTH
)
679 if (TLV_OPT_LENGTH(tlv
) < (tlv
->plen
+ 7)/8)
682 msg
->plen
= tlv
->plen
;
683 msg
->prefix
= get_ip6_px(tlv
->addr
, tlv
->plen
);
684 return PARSE_SUCCESS
;
686 case BABEL_AE_IP6_LL
:
697 babel_write_seqno_request(struct babel_tlv
*hdr
, union babel_msg
*m
,
698 struct babel_write_state
*state
, int max_len
)
700 struct babel_tlv_seqno_request
*tlv
= (void *) hdr
;
701 struct babel_msg_seqno_request
*msg
= &m
->seqno_request
;
703 int len
= sizeof(struct babel_tlv_seqno_request
) + (msg
->plen
+ 7)/8;
708 TLV_HDR(tlv
, BABEL_TLV_SEQNO_REQUEST
, len
);
709 tlv
->ae
= BABEL_AE_IP6
;
710 tlv
->plen
= msg
->plen
;
711 put_u16(&tlv
->seqno
, msg
->seqno
);
712 tlv
->hop_count
= msg
->hop_count
;
713 put_u64(&tlv
->router_id
, msg
->router_id
);
714 put_ip6_px(tlv
->addr
, msg
->prefix
, msg
->plen
);
720 babel_read_tlv(struct babel_tlv
*hdr
,
721 union babel_msg
*msg
,
722 struct babel_parse_state
*state
)
724 if ((hdr
->type
<= BABEL_TLV_PADN
) ||
725 (hdr
->type
>= BABEL_TLV_MAX
) ||
726 !tlv_data
[hdr
->type
].read_tlv
)
729 if (TLV_LENGTH(hdr
) < tlv_data
[hdr
->type
].min_length
)
732 memset(msg
, 0, sizeof(*msg
));
733 return tlv_data
[hdr
->type
].read_tlv(hdr
, msg
, state
);
737 babel_write_tlv(struct babel_tlv
*hdr
,
738 union babel_msg
*msg
,
739 struct babel_write_state
*state
,
742 if ((msg
->type
<= BABEL_TLV_PADN
) ||
743 (msg
->type
>= BABEL_TLV_MAX
) ||
744 !tlv_data
[msg
->type
].write_tlv
)
747 if (tlv_data
[msg
->type
].min_length
> max_len
)
750 memset(hdr
, 0, tlv_data
[msg
->type
].min_length
);
751 return tlv_data
[msg
->type
].write_tlv(hdr
, msg
, state
, max_len
);
756 * Packet RX/TX functions
760 babel_send_to(struct babel_iface
*ifa
, ip_addr dest
)
763 struct babel_pkt_header
*hdr
= (void *) sk
->tbuf
;
764 int len
= get_u16(&hdr
->length
) + sizeof(struct babel_pkt_header
);
766 DBG("Babel: Sending %d bytes to %I\n", len
, dest
);
767 return sk_send_to(sk
, len
, dest
, 0);
771 * babel_write_queue - Write a TLV queue to a transmission buffer
772 * @ifa: Interface holding the transmission buffer
773 * @queue: TLV queue to write (containing internal-format TLVs)
775 * This function writes a packet to the interface transmission buffer with as
776 * many TLVs from the &queue as will fit in the buffer. It returns the number of
777 * bytes written (NOT counting the packet header). The function is called by
778 * babel_send_queue() and babel_send_unicast() to construct packets for
779 * transmission, and uses per-TLV helper functions to convert the
780 * internal-format TLVs to their wire representations.
782 * The TLVs in the queue are freed after they are written to the buffer.
785 babel_write_queue(struct babel_iface
*ifa
, list
*queue
)
787 struct babel_proto
*p
= ifa
->proto
;
788 struct babel_write_state state
= {};
790 if (EMPTY_LIST(*queue
))
793 byte
*pos
= ifa
->sk
->tbuf
;
794 byte
*end
= pos
+ ifa
->tx_length
;
796 struct babel_pkt_header
*pkt
= (void *) pos
;
797 pkt
->magic
= BABEL_MAGIC
;
798 pkt
->version
= BABEL_VERSION
;
800 pos
+= sizeof(struct babel_pkt_header
);
802 struct babel_msg_node
*msg
;
803 WALK_LIST_FIRST(msg
, *queue
)
805 int len
= babel_write_tlv((struct babel_tlv
*) pos
, &msg
->msg
, &state
, end
- pos
);
812 sl_free(p
->msg_slab
, msg
);
815 int plen
= pos
- (byte
*) pkt
;
816 put_u16(&pkt
->length
, plen
- sizeof(struct babel_pkt_header
));
822 babel_send_queue(void *arg
)
824 struct babel_iface
*ifa
= arg
;
825 while ((babel_write_queue(ifa
, &ifa
->msg_queue
) > 0) &&
826 (babel_send_to(ifa
, IP6_BABEL_ROUTERS
) > 0));
830 babel_kick_queue(struct babel_iface
*ifa
)
833 * Only schedule send event if there is not already data in the socket buffer.
834 * Otherwise we may overwrite the data already in the buffer.
837 if ((ifa
->sk
->tpos
== ifa
->sk
->tbuf
) && !ev_active(ifa
->send_event
))
838 ev_schedule(ifa
->send_event
);
842 * babel_send_unicast - send a single TLV via unicast to a destination
844 * @ifa: Interface to send via
845 * @dest: Destination of the TLV
847 * This function is used to send a single TLV via unicast to a designated
848 * receiver. This is used for replying to certain incoming requests, and for
849 * sending unicast requests to refresh routes before they expire.
852 babel_send_unicast(union babel_msg
*msg
, struct babel_iface
*ifa
, ip_addr dest
)
854 struct babel_proto
*p
= ifa
->proto
;
855 struct babel_msg_node
*msgn
= sl_alloc(p
->msg_slab
);
860 add_tail(&queue
, NODE msgn
);
861 babel_write_queue(ifa
, &queue
);
862 babel_send_to(ifa
, dest
);
864 /* We could overwrite waiting packet here, we may have to kick TX queue */
865 if (!EMPTY_LIST(ifa
->msg_queue
))
866 babel_kick_queue(ifa
);
870 * babel_enqueue - enqueue a TLV for transmission on an interface
871 * @msg: TLV to enqueue (in internal TLV format)
872 * @ifa: Interface to enqueue to
874 * This function is called to enqueue a TLV for subsequent transmission on an
875 * interface. The transmission event is triggered whenever a TLV is enqueued;
876 * this ensures that TLVs will be transmitted in a timely manner, but that TLVs
877 * which are enqueued in rapid succession can be transmitted together in one
881 babel_enqueue(union babel_msg
*msg
, struct babel_iface
*ifa
)
883 struct babel_proto
*p
= ifa
->proto
;
884 struct babel_msg_node
*msgn
= sl_alloc(p
->msg_slab
);
886 add_tail(&ifa
->msg_queue
, NODE msgn
);
887 babel_kick_queue(ifa
);
891 * babel_process_packet - process incoming data packet
892 * @pkt: Pointer to the packet data
893 * @len: Length of received packet
894 * @saddr: Address of packet sender
895 * @ifa: Interface packet was received on.
897 * This function is the main processing hook of incoming Babel packets. It
898 * checks that the packet header is well-formed, then processes the TLVs
899 * contained in the packet. This is done in two passes: First all TLVs are
900 * parsed into the internal TLV format. If a TLV parser fails, processing of the
901 * rest of the packet is aborted.
903 * After the parsing step, the TLV handlers are called for each parsed TLV in
907 babel_process_packet(struct babel_pkt_header
*pkt
, int len
,
908 ip_addr saddr
, struct babel_iface
*ifa
)
910 struct babel_proto
*p
= ifa
->proto
;
911 struct babel_tlv
*tlv
;
912 struct babel_msg_node
*msg
;
916 int plen
= sizeof(struct babel_pkt_header
) + get_u16(&pkt
->length
);
918 byte
*end
= (byte
*)pkt
+ plen
;
920 struct babel_parse_state state
= {
927 if ((pkt
->magic
!= BABEL_MAGIC
) || (pkt
->version
!= BABEL_VERSION
))
929 TRACE(D_PACKETS
, "Strange packet from %I via %s - magic %d version %d",
930 saddr
, ifa
->iface
->name
, pkt
->magic
, pkt
->version
);
936 LOG_PKT("Bad packet from %I via %s - %s (%u)",
937 saddr
, ifa
->iface
->name
, "length mismatch", plen
);
941 TRACE(D_PACKETS
, "Packet received from %I via %s",
942 saddr
, ifa
->iface
->name
);
946 /* First pass through the packet TLV by TLV, parsing each into internal data
948 for (tlv
= FIRST_TLV(pkt
);
952 /* Ugly special case */
953 if (tlv
->type
== BABEL_TLV_PAD1
)
956 /* The end of the common TLV header */
957 pos
= (byte
*)tlv
+ sizeof(struct babel_tlv
);
958 if ((pos
> end
) || (pos
+ tlv
->length
> end
))
960 LOG_PKT("Bad TLV from %I via %s type %d pos %d - framing error",
961 saddr
, ifa
->iface
->name
, tlv
->type
, (byte
*)tlv
- (byte
*)pkt
);
965 msg
= sl_alloc(p
->msg_slab
);
966 res
= babel_read_tlv(tlv
, &msg
->msg
, &state
);
967 if (res
== PARSE_SUCCESS
)
969 add_tail(&msgs
, NODE msg
);
971 else if (res
== PARSE_IGNORE
)
973 DBG("Babel: Ignoring TLV of type %d\n", tlv
->type
);
974 sl_free(p
->msg_slab
, msg
);
976 else /* PARSE_ERROR */
978 LOG_PKT("Bad TLV from %I via %s type %d pos %d - parse error",
979 saddr
, ifa
->iface
->name
, tlv
->type
, (byte
*)tlv
- (byte
*)pkt
);
980 sl_free(p
->msg_slab
, msg
);
985 /* Parsing done, handle all parsed TLVs */
986 WALK_LIST_FIRST(msg
, msgs
)
988 if (tlv_data
[msg
->msg
.type
].handle_tlv
)
989 tlv_data
[msg
->msg
.type
].handle_tlv(&msg
->msg
, ifa
);
991 sl_free(p
->msg_slab
, msg
);
996 babel_err_hook(sock
*sk
, int err
)
998 struct babel_iface
*ifa
= sk
->data
;
999 struct babel_proto
*p
= ifa
->proto
;
1001 log(L_ERR
"%s: Socket error on %s: %M", p
->p
.name
, ifa
->iface
->name
, err
);
1002 /* FIXME: Drop queued TLVs here? */
1007 babel_tx_hook(sock
*sk
)
1009 struct babel_iface
*ifa
= sk
->data
;
1011 DBG("Babel: TX hook called (iface %s, src %I, dst %I)\n",
1012 sk
->iface
->name
, sk
->saddr
, sk
->daddr
);
1014 babel_send_queue(ifa
);
1019 babel_rx_hook(sock
*sk
, int len
)
1021 struct babel_iface
*ifa
= sk
->data
;
1022 struct babel_proto
*p
= ifa
->proto
;
1023 const char *err_dsc
= NULL
;
1026 if (sk
->lifindex
!= ifa
->iface
->index
)
1029 DBG("Babel: RX hook called (iface %s, src %I, dst %I)\n",
1030 sk
->iface
->name
, sk
->faddr
, sk
->laddr
);
1032 /* Silently ignore my own packets */
1033 if (ipa_equal(ifa
->iface
->addr
->ip
, sk
->faddr
))
1036 if (!ipa_is_link_local(sk
->faddr
))
1037 DROP1("wrong src address");
1039 if (sk
->fport
!= ifa
->cf
->port
)
1040 DROP("wrong src port", sk
->fport
);
1042 if (len
< sizeof(struct babel_pkt_header
))
1043 DROP("too short", len
);
1045 if (sk
->flags
& SKF_TRUNCATED
)
1046 DROP("truncated", len
);
1048 babel_process_packet((struct babel_pkt_header
*) sk
->rbuf
, len
, sk
->faddr
, ifa
);
1052 LOG_PKT("Bad packet from %I via %s - %s (%u)",
1053 sk
->faddr
, sk
->iface
->name
, err_dsc
, err_val
);
1058 babel_open_socket(struct babel_iface
*ifa
)
1060 struct babel_proto
*p
= ifa
->proto
;
1063 sk
= sk_new(ifa
->pool
);
1065 sk
->sport
= ifa
->cf
->port
;
1066 sk
->dport
= ifa
->cf
->port
;
1067 sk
->iface
= ifa
->iface
;
1069 sk
->rx_hook
= babel_rx_hook
;
1070 sk
->tx_hook
= babel_tx_hook
;
1071 sk
->err_hook
= babel_err_hook
;
1074 sk
->tos
= ifa
->cf
->tx_tos
;
1075 sk
->priority
= ifa
->cf
->tx_priority
;
1077 sk
->flags
= SKF_LADDR_RX
;
1079 if (sk_open(sk
) < 0)
1082 if (sk_setup_multicast(sk
) < 0)
1085 if (sk_join_group(sk
, IP6_BABEL_ROUTERS
) < 0)
1092 sk_log_error(sk
, p
->p
.name
);