2 * BIRD -- BGP Packet Processing
4 * (c) 2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
11 #include "nest/bird.h"
12 #include "nest/iface.h"
13 #include "nest/protocol.h"
14 #include "nest/route.h"
15 #include "nest/attrs.h"
16 #include "proto/mrt/mrt.h"
17 #include "conf/conf.h"
18 #include "lib/unaligned.h"
19 #include "lib/socket.h"
26 #define BGP_RR_REQUEST 0
27 #define BGP_RR_BEGIN 1
31 static struct tbf rl_rcv_update
= TBF_DEFAULT_LOG_LIMITS
;
32 static struct tbf rl_snd_update
= TBF_DEFAULT_LOG_LIMITS
;
34 /* Table for state -> RFC 6608 FSM error subcodes */
35 static byte fsm_err_subcode
[BS_MAX
] = {
42 init_mrt_bgp_data(struct bgp_conn
*conn
, struct mrt_bgp_data
*d
)
44 struct bgp_proto
*p
= conn
->bgp
;
45 int p_ok
= conn
->state
>= BS_OPENCONFIRM
;
47 memset(d
, 0, sizeof(struct mrt_bgp_data
));
48 d
->peer_as
= p
->remote_as
;
49 d
->local_as
= p
->local_as
;
50 d
->index
= (p
->neigh
&& p
->neigh
->iface
) ? p
->neigh
->iface
->index
: 0;
52 d
->peer_ip
= conn
->sk
? conn
->sk
->daddr
: IPA_NONE
;
53 d
->local_ip
= conn
->sk
? conn
->sk
->saddr
: IPA_NONE
;
54 d
->as4
= p_ok
? p
->as4_session
: 0;
55 d
->add_path
= p_ok
? p
->add_path_rx
: 0;
59 bgp_dump_message(struct bgp_conn
*conn
, byte
*pkt
, uint len
)
61 struct mrt_bgp_data d
;
62 init_mrt_bgp_data(conn
, &d
);
67 mrt_dump_bgp_message(&d
);
71 bgp_dump_state_change(struct bgp_conn
*conn
, uint old
, uint
new)
73 struct mrt_bgp_data d
;
74 init_mrt_bgp_data(conn
, &d
);
79 mrt_dump_bgp_state_change(&d
);
83 bgp_create_notification(struct bgp_conn
*conn
, byte
*buf
)
85 struct bgp_proto
*p
= conn
->bgp
;
87 BGP_TRACE(D_PACKETS
, "Sending NOTIFICATION(code=%d.%d)", conn
->notify_code
, conn
->notify_subcode
);
88 buf
[0] = conn
->notify_code
;
89 buf
[1] = conn
->notify_subcode
;
90 memcpy(buf
+2, conn
->notify_data
, conn
->notify_size
);
91 return buf
+ 2 + conn
->notify_size
;
96 bgp_put_cap_ipv6(struct bgp_proto
*p UNUSED
, byte
*buf
)
98 *buf
++ = 1; /* Capability 1: Multiprotocol extensions */
99 *buf
++ = 4; /* Capability data length */
100 *buf
++ = 0; /* We support AF IPv6 */
101 *buf
++ = BGP_AF_IPV6
;
102 *buf
++ = 0; /* RFU */
103 *buf
++ = 1; /* and SAFI 1 */
110 bgp_put_cap_ipv4(struct bgp_proto
*p UNUSED
, byte
*buf
)
112 *buf
++ = 1; /* Capability 1: Multiprotocol extensions */
113 *buf
++ = 4; /* Capability data length */
114 *buf
++ = 0; /* We support AF IPv4 */
115 *buf
++ = BGP_AF_IPV4
;
116 *buf
++ = 0; /* RFU */
117 *buf
++ = 1; /* and SAFI 1 */
123 bgp_put_cap_rr(struct bgp_proto
*p UNUSED
, byte
*buf
)
125 *buf
++ = 2; /* Capability 2: Support for route refresh */
126 *buf
++ = 0; /* Capability data length */
131 bgp_put_cap_ext_msg(struct bgp_proto
*p UNUSED
, byte
*buf
)
133 *buf
++ = 6; /* Capability 6: Support for extended messages */
134 *buf
++ = 0; /* Capability data length */
139 bgp_put_cap_gr1(struct bgp_proto
*p
, byte
*buf
)
141 *buf
++ = 64; /* Capability 64: Support for graceful restart */
142 *buf
++ = 6; /* Capability data length */
144 put_u16(buf
, p
->cf
->gr_time
);
145 if (p
->p
.gr_recovery
)
146 buf
[0] |= BGP_GRF_RESTART
;
149 *buf
++ = 0; /* Appropriate AF */
151 *buf
++ = 1; /* and SAFI 1 */
152 *buf
++ = p
->p
.gr_recovery
? BGP_GRF_FORWARDING
: 0;
158 bgp_put_cap_gr2(struct bgp_proto
*p UNUSED
, byte
*buf
)
160 *buf
++ = 64; /* Capability 64: Support for graceful restart */
161 *buf
++ = 2; /* Capability data length */
167 bgp_put_cap_as4(struct bgp_proto
*p
, byte
*buf
)
169 *buf
++ = 65; /* Capability 65: Support for 4-octet AS number */
170 *buf
++ = 4; /* Capability data length */
171 put_u32(buf
, p
->local_as
);
176 bgp_put_cap_add_path(struct bgp_proto
*p
, byte
*buf
)
178 *buf
++ = 69; /* Capability 69: Support for ADD-PATH */
179 *buf
++ = 4; /* Capability data length */
181 *buf
++ = 0; /* Appropriate AF */
183 *buf
++ = 1; /* SAFI 1 */
185 *buf
++ = p
->cf
->add_path
;
191 bgp_put_cap_err(struct bgp_proto
*p UNUSED
, byte
*buf
)
193 *buf
++ = 70; /* Capability 70: Support for enhanced route refresh */
194 *buf
++ = 0; /* Capability data length */
199 bgp_put_cap_llgr1(struct bgp_proto
*p
, byte
*buf
)
201 *buf
++ = 71; /* Capability 71: Support for long-lived graceful restart */
202 *buf
++ = 7; /* Capability data length */
204 *buf
++ = 0; /* Appropriate AF */
206 *buf
++ = 1; /* and SAFI 1 */
208 /* Next is 8bit flags and 24bit time */
209 put_u32(buf
, p
->cf
->llgr_time
);
210 buf
[0] = p
->p
.gr_recovery
? BGP_LLGRF_FORWARDING
: 0;
217 bgp_put_cap_llgr2(struct bgp_proto
*p UNUSED
, byte
*buf
)
219 *buf
++ = 71; /* Capability 71: Support for long-lived graceful restart */
220 *buf
++ = 0; /* Capability data length */
226 bgp_create_open(struct bgp_conn
*conn
, byte
*buf
)
228 struct bgp_proto
*p
= conn
->bgp
;
232 BGP_TRACE(D_PACKETS
, "Sending OPEN(ver=%d,as=%d,hold=%d,id=%08x)",
233 BGP_VERSION
, p
->local_as
, p
->cf
->hold_time
, p
->local_id
);
234 buf
[0] = BGP_VERSION
;
235 put_u16(buf
+1, (p
->local_as
< 0xFFFF) ? p
->local_as
: AS_TRANS
);
236 put_u16(buf
+3, p
->cf
->hold_time
);
237 put_u32(buf
+5, p
->local_id
);
239 if (conn
->start_state
== BSS_CONNECT_NOCAP
)
241 BGP_TRACE(D_PACKETS
, "Skipping capabilities");
246 /* Skipped 3 B for length field and Capabilities parameter header */
250 if (p
->cf
->advertise_ipv4
)
251 cap
= bgp_put_cap_ipv4(p
, cap
);
255 cap
= bgp_put_cap_ipv6(p
, cap
);
258 if (p
->cf
->enable_refresh
)
259 cap
= bgp_put_cap_rr(p
, cap
);
261 if (p
->cf
->gr_mode
== BGP_GR_ABLE
)
262 cap
= bgp_put_cap_gr1(p
, cap
);
263 else if (p
->cf
->gr_mode
== BGP_GR_AWARE
)
264 cap
= bgp_put_cap_gr2(p
, cap
);
266 if (p
->cf
->enable_as4
)
267 cap
= bgp_put_cap_as4(p
, cap
);
270 cap
= bgp_put_cap_add_path(p
, cap
);
272 if (p
->cf
->enable_refresh
)
273 cap
= bgp_put_cap_err(p
, cap
);
275 if (p
->cf
->enable_extended_messages
)
276 cap
= bgp_put_cap_ext_msg(p
, cap
);
278 if (p
->cf
->llgr_mode
== BGP_LLGR_ABLE
)
279 cap
= bgp_put_cap_llgr1(p
, cap
);
280 else if (p
->cf
->llgr_mode
== BGP_LLGR_AWARE
)
281 cap
= bgp_put_cap_llgr2(p
, cap
);
283 cap_len
= cap
- buf
- 12;
286 buf
[9] = cap_len
+ 2; /* Optional params len */
287 buf
[10] = 2; /* Option: Capability list */
288 buf
[11] = cap_len
; /* Option length */
293 buf
[9] = 0; /* No optional parameters */
299 bgp_encode_prefixes(struct bgp_proto
*p
, byte
*w
, struct bgp_bucket
*buck
, uint remains
)
305 while (!EMPTY_LIST(buck
->prefixes
) && (remains
>= (5+sizeof(ip_addr
))))
307 struct bgp_prefix
*px
= SKIP_BACK(struct bgp_prefix
, bucket_node
, HEAD(buck
->prefixes
));
308 DBG("\tDequeued route %I/%d\n", px
->n
.prefix
, px
->n
.pxlen
);
312 put_u32(w
, px
->path_id
);
318 bytes
= (px
->n
.pxlen
+ 7) / 8;
321 memcpy(w
, &a
, bytes
);
323 remains
-= bytes
+ 1;
324 rem_node(&px
->bucket_node
);
325 bgp_free_prefix(p
, px
);
326 // fib_delete(&p->prefix_fib, px);
332 bgp_flush_prefixes(struct bgp_proto
*p
, struct bgp_bucket
*buck
)
334 while (!EMPTY_LIST(buck
->prefixes
))
336 struct bgp_prefix
*px
= SKIP_BACK(struct bgp_prefix
, bucket_node
, HEAD(buck
->prefixes
));
337 log(L_ERR
"%s: - route %I/%d skipped", p
->p
.name
, px
->n
.prefix
, px
->n
.pxlen
);
338 rem_node(&px
->bucket_node
);
339 bgp_free_prefix(p
, px
);
340 // fib_delete(&p->prefix_fib, px);
344 #ifndef IPV6 /* IPv4 version */
347 bgp_create_update(struct bgp_conn
*conn
, byte
*buf
)
349 struct bgp_proto
*p
= conn
->bgp
;
350 struct bgp_bucket
*buck
;
351 int remains
= bgp_max_packet_length(p
) - BGP_HEADER_LENGTH
- 4;
358 if ((buck
= p
->withdraw_bucket
) && !EMPTY_LIST(buck
->prefixes
))
360 DBG("Withdrawn routes:\n");
361 wd_size
= bgp_encode_prefixes(p
, w
, buck
, remains
);
365 put_u16(buf
, wd_size
);
369 while ((buck
= (struct bgp_bucket
*) HEAD(p
->bucket_queue
))->send_node
.next
)
371 if (EMPTY_LIST(buck
->prefixes
))
373 DBG("Deleting empty bucket %p\n", buck
);
374 rem_node(&buck
->send_node
);
375 bgp_free_bucket(p
, buck
);
379 DBG("Processing bucket %p\n", buck
);
380 a_size
= bgp_encode_attrs(p
, w
+2, buck
->eattrs
, remains
- 1024);
384 log(L_ERR
"%s: Attribute list too long, skipping corresponding routes", p
->p
.name
);
385 bgp_flush_prefixes(p
, buck
);
386 rem_node(&buck
->send_node
);
387 bgp_free_bucket(p
, buck
);
393 r_size
= bgp_encode_prefixes(p
, w
, buck
, remains
- a_size
);
398 if (!a_size
) /* Attributes not already encoded */
403 if (wd_size
|| r_size
)
405 BGP_TRACE_RL(&rl_snd_update
, D_PACKETS
, "Sending UPDATE");
413 bgp_create_end_mark(struct bgp_conn
*conn
, byte
*buf
)
415 struct bgp_proto
*p
= conn
->bgp
;
416 BGP_TRACE(D_PACKETS
, "Sending END-OF-RIB");
422 #else /* IPv6 version */
425 same_iface(struct bgp_proto
*p
, ip_addr
*ip
)
427 neighbor
*n
= neigh_find(&p
->p
, ip
, 0);
428 return n
&& p
->neigh
&& n
->iface
== p
->neigh
->iface
;
432 bgp_create_update(struct bgp_conn
*conn
, byte
*buf
)
434 struct bgp_proto
*p
= conn
->bgp
;
435 struct bgp_bucket
*buck
;
436 int size
, second
, rem_stored
;
437 int remains
= bgp_max_packet_length(p
) - BGP_HEADER_LENGTH
- 4;
438 byte
*w
, *w_stored
, *tmp
, *tstart
;
439 ip_addr
*ipp
, ip
, ip_ll
;
446 if ((buck
= p
->withdraw_bucket
) && !EMPTY_LIST(buck
->prefixes
))
448 DBG("Withdrawn routes:\n");
449 tmp
= bgp_attach_attr_wa(&ea
, bgp_linpool
, BA_MP_UNREACH_NLRI
, remains
-8);
451 *tmp
++ = BGP_AF_IPV6
;
453 ea
->attrs
[0].u
.ptr
->length
= 3 + bgp_encode_prefixes(p
, tmp
, buck
, remains
-11);
454 size
= bgp_encode_attrs(p
, w
, ea
, remains
);
461 while ((buck
= (struct bgp_bucket
*) HEAD(p
->bucket_queue
))->send_node
.next
)
463 if (EMPTY_LIST(buck
->prefixes
))
465 DBG("Deleting empty bucket %p\n", buck
);
466 rem_node(&buck
->send_node
);
467 bgp_free_bucket(p
, buck
);
471 DBG("Processing bucket %p\n", buck
);
472 rem_stored
= remains
;
475 size
= bgp_encode_attrs(p
, w
, buck
->eattrs
, remains
- 1024);
478 log(L_ERR
"%s: Attribute list too long, skipping corresponding routes", p
->p
.name
);
479 bgp_flush_prefixes(p
, buck
);
480 rem_node(&buck
->send_node
);
481 bgp_free_bucket(p
, buck
);
487 /* We have two addresses here in NEXT_HOP eattr. Really.
488 Unless NEXT_HOP was modified by filter */
489 nh
= ea_find(buck
->eattrs
, EA_CODE(EAP_BGP
, BA_NEXT_HOP
));
491 second
= (nh
->u
.ptr
->length
== NEXT_HOP_LENGTH
);
492 ipp
= (ip_addr
*) nh
->u
.ptr
->data
;
496 if (ipa_equal(ip
, p
->source_addr
))
497 ip_ll
= p
->local_link
;
500 /* If we send a route with 'third party' next hop destinated
501 * in the same interface, we should also send a link local
502 * next hop address. We use the received one (stored in the
503 * other part of BA_NEXT_HOP eattr). If we didn't received
504 * it (for example it is a static route), we can't use
505 * 'third party' next hop and we have to use local IP address
506 * as next hop. Sending original next hop address without
507 * link local address seems to be a natural way to solve that
508 * problem, but it is contrary to RFC 2545 and Quagga does not
509 * accept such routes.
511 * There are two cases, either we have global IP, or
512 * IPA_NONE if the neighbor is link-local. For IPA_NONE,
513 * we suppose it is on the same iface, see bgp_update_attrs().
516 if (ipa_zero(ip
) || same_iface(p
, &ip
))
518 if (second
&& ipa_nonzero(ipp
[1]))
522 switch (p
->cf
->missing_lladdr
)
526 ip_ll
= p
->local_link
;
529 log(L_ERR
"%s: Missing link-local next hop address, skipping corresponding routes", p
->p
.name
);
531 remains
= rem_stored
;
532 bgp_flush_prefixes(p
, buck
);
533 rem_node(&buck
->send_node
);
534 bgp_free_bucket(p
, buck
);
543 tstart
= tmp
= bgp_attach_attr_wa(&ea
, bgp_linpool
, BA_MP_REACH_NLRI
, remains
-8);
545 *tmp
++ = BGP_AF_IPV6
;
548 if (ipa_is_link_local(ip
))
551 if (ipa_nonzero(ip_ll
))
555 memcpy(tmp
, &ip
, 16);
557 memcpy(tmp
+16, &ip_ll
, 16);
564 memcpy(tmp
, &ip
, 16);
568 *tmp
++ = 0; /* No SNPA information */
569 tmp
+= bgp_encode_prefixes(p
, tmp
, buck
, remains
- (8+3+32+1));
570 ea
->attrs
[0].u
.ptr
->length
= tmp
- tstart
;
571 size
= bgp_encode_attrs(p
, w
, ea
, remains
);
579 put_u16(buf
+2, size
);
580 lp_flush(bgp_linpool
);
583 BGP_TRACE_RL(&rl_snd_update
, D_PACKETS
, "Sending UPDATE");
591 bgp_create_end_mark(struct bgp_conn
*conn
, byte
*buf
)
593 struct bgp_proto
*p
= conn
->bgp
;
594 BGP_TRACE(D_PACKETS
, "Sending END-OF-RIB");
597 put_u16(buf
+2, 6); /* length 4-9 */
600 /* Empty MP_UNREACH_NLRI atribute */
601 *buf
++ = BAF_OPTIONAL
;
602 *buf
++ = BA_MP_UNREACH_NLRI
;
603 *buf
++ = 3; /* Length 7-9 */
604 *buf
++ = 0; /* AFI */
605 *buf
++ = BGP_AF_IPV6
;
606 *buf
++ = 1; /* SAFI */
613 bgp_create_route_refresh(struct bgp_conn
*conn
, byte
*buf
)
615 struct bgp_proto
*p
= conn
->bgp
;
616 BGP_TRACE(D_PACKETS
, "Sending ROUTE-REFRESH");
618 /* Original original route refresh request, RFC 2918 */
621 *buf
++ = BGP_RR_REQUEST
;
622 *buf
++ = 1; /* SAFI */
627 bgp_create_begin_refresh(struct bgp_conn
*conn
, byte
*buf
)
629 struct bgp_proto
*p
= conn
->bgp
;
630 BGP_TRACE(D_PACKETS
, "Sending BEGIN-OF-RR");
632 /* Demarcation of beginning of route refresh (BoRR), RFC 7313 */
635 *buf
++ = BGP_RR_BEGIN
;
636 *buf
++ = 1; /* SAFI */
641 bgp_create_end_refresh(struct bgp_conn
*conn
, byte
*buf
)
643 struct bgp_proto
*p
= conn
->bgp
;
644 BGP_TRACE(D_PACKETS
, "Sending END-OF-RR");
646 /* Demarcation of ending of route refresh (EoRR), RFC 7313 */
650 *buf
++ = 1; /* SAFI */
656 bgp_create_header(byte
*buf
, uint len
, uint type
)
658 memset(buf
, 0xff, 16); /* Marker */
659 put_u16(buf
+16, len
);
664 * bgp_fire_tx - transmit packets
667 * Whenever the transmit buffers of the underlying TCP connection
668 * are free and we have any packets queued for sending, the socket functions
669 * call bgp_fire_tx() which takes care of selecting the highest priority packet
670 * queued (Notification > Keepalive > Open > Update), assembling its header
671 * and body and sending it to the connection.
674 bgp_fire_tx(struct bgp_conn
*conn
)
676 struct bgp_proto
*p
= conn
->bgp
;
677 uint s
= conn
->packets_to_send
;
679 byte
*buf
, *pkt
, *end
;
684 conn
->packets_to_send
= 0;
688 pkt
= buf
+ BGP_HEADER_LENGTH
;
690 if (s
& (1 << PKT_SCHEDULE_CLOSE
))
692 /* We can finally close connection and enter idle state */
693 bgp_conn_enter_idle_state(conn
);
696 if (s
& (1 << PKT_NOTIFICATION
))
698 s
= 1 << PKT_SCHEDULE_CLOSE
;
699 type
= PKT_NOTIFICATION
;
700 end
= bgp_create_notification(conn
, pkt
);
702 else if (s
& (1 << PKT_KEEPALIVE
))
704 s
&= ~(1 << PKT_KEEPALIVE
);
705 type
= PKT_KEEPALIVE
;
706 end
= pkt
; /* Keepalives carry no data */
707 BGP_TRACE(D_PACKETS
, "Sending KEEPALIVE");
708 bgp_start_timer(conn
->keepalive_timer
, conn
->keepalive_time
);
710 else if (s
& (1 << PKT_OPEN
))
712 s
&= ~(1 << PKT_OPEN
);
714 end
= bgp_create_open(conn
, pkt
);
716 else if (s
& (1 << PKT_ROUTE_REFRESH
))
718 s
&= ~(1 << PKT_ROUTE_REFRESH
);
719 type
= PKT_ROUTE_REFRESH
;
720 end
= bgp_create_route_refresh(conn
, pkt
);
722 else if (s
& (1 << PKT_BEGIN_REFRESH
))
724 s
&= ~(1 << PKT_BEGIN_REFRESH
);
725 type
= PKT_ROUTE_REFRESH
; /* BoRR is a subtype of RR */
726 end
= bgp_create_begin_refresh(conn
, pkt
);
728 else if (s
& (1 << PKT_UPDATE
))
731 end
= bgp_create_update(conn
, pkt
);
735 /* No update to send, perhaps we need to send End-of-RIB or EoRR */
737 conn
->packets_to_send
= 0;
739 if (p
->feed_state
== BFS_LOADED
)
742 end
= bgp_create_end_mark(conn
, pkt
);
745 else if (p
->feed_state
== BFS_REFRESHED
)
747 type
= PKT_ROUTE_REFRESH
;
748 end
= bgp_create_end_refresh(conn
, pkt
);
751 else /* Really nothing to send */
754 p
->feed_state
= BFS_NONE
;
760 conn
->packets_to_send
= s
;
761 bgp_create_header(buf
, end
- buf
, type
);
762 return sk_send(sk
, end
- buf
);
766 * bgp_schedule_packet - schedule a packet for transmission
770 * Schedule a packet of type @type to be sent as soon as possible.
773 bgp_schedule_packet(struct bgp_conn
*conn
, int type
)
775 DBG("BGP: Scheduling packet type %d\n", type
);
776 conn
->packets_to_send
|= 1 << type
;
777 if (conn
->sk
&& conn
->sk
->tpos
== conn
->sk
->tbuf
&& !ev_active(conn
->tx_ev
))
778 ev_schedule(conn
->tx_ev
);
782 bgp_kick_tx(void *vconn
)
784 struct bgp_conn
*conn
= vconn
;
786 DBG("BGP: kicking TX\n");
787 while (bgp_fire_tx(conn
) > 0)
794 struct bgp_conn
*conn
= sk
->data
;
796 DBG("BGP: TX hook\n");
797 while (bgp_fire_tx(conn
) > 0)
801 /* Capatibility negotiation as per RFC 2842 */
804 bgp_parse_capabilities(struct bgp_conn
*conn
, byte
*opt
, int len
)
806 // struct bgp_proto *p = conn->bgp;
811 if (len
< 2 || len
< 2 + opt
[1])
818 case 2: /* Route refresh capability, RFC 2918 */
821 conn
->peer_refresh_support
= 1;
824 case 6: /* Extended message length capability, draft */
827 conn
->peer_ext_messages_support
= 1;
830 case 64: /* Graceful restart capability, RFC 4724 */
833 conn
->peer_gr_aware
= 1;
834 conn
->peer_gr_able
= 0;
835 conn
->peer_gr_time
= get_u16(opt
+ 2) & 0x0fff;
836 conn
->peer_gr_flags
= opt
[2] & 0xf0;
837 conn
->peer_gr_aflags
= 0;
838 for (i
= 2; i
< cl
; i
+= 4)
839 if (opt
[2+i
+0] == 0 && opt
[2+i
+1] == BGP_AF
&& opt
[2+i
+2] == 1) /* Match AFI/SAFI */
841 conn
->peer_gr_able
= 1;
842 conn
->peer_gr_aflags
= opt
[2+i
+3];
846 case 65: /* AS4 capability, RFC 4893 */
849 conn
->peer_as4_support
= 1;
850 if (conn
->bgp
->cf
->enable_as4
)
851 conn
->advertised_as
= get_u32(opt
+ 2);
854 case 69: /* ADD-PATH capability, RFC 7911 */
857 for (i
= 0; i
< cl
; i
+= 4)
858 if (opt
[2+i
+0] == 0 && opt
[2+i
+1] == BGP_AF
&& opt
[2+i
+2] == 1) /* Match AFI/SAFI */
859 conn
->peer_add_path
= opt
[2+i
+3];
860 if (conn
->peer_add_path
> ADD_PATH_FULL
)
864 case 70: /* Enhanced route refresh capability, RFC 7313 */
867 conn
->peer_enhanced_refresh_support
= 1;
870 case 71: /* Long-lived graceful restart capability, RFC draft */
873 conn
->peer_llgr_aware
= 1;
874 conn
->peer_llgr_able
= 0;
875 conn
->peer_llgr_time
= 0;
876 conn
->peer_llgr_aflags
= 0;
877 for (i
= 0; i
< cl
; i
+= 7)
878 if (opt
[2+i
+0] == 0 && opt
[2+i
+1] == BGP_AF
&& opt
[2+i
+2] == 1) /* Match AFI/SAFI */
880 conn
->peer_llgr_able
= 1;
881 conn
->peer_llgr_time
= get_u32(opt
+ 2+i
+3) & 0xffffff;
882 conn
->peer_llgr_aflags
= opt
[2+i
+3];
886 /* We can safely ignore all other capabilities */
892 /* The LLGR capability must be advertised together with the GR capability,
893 otherwise it must be disregarded */
894 if (!conn
->peer_gr_aware
&& conn
->peer_llgr_aware
)
896 conn
->peer_llgr_aware
= 0;
897 conn
->peer_llgr_able
= 0;
898 conn
->peer_llgr_time
= 0;
899 conn
->peer_llgr_aflags
= 0;
905 bgp_error(conn
, 2, 0, NULL
, 0);
910 bgp_parse_options(struct bgp_conn
*conn
, byte
*opt
, int len
)
912 struct bgp_proto
*p
= conn
->bgp
;
917 if (len
< 2 || len
< 2 + opt
[1])
918 { bgp_error(conn
, 2, 0, NULL
, 0); return 0; }
922 DBG("\tOption %02x:", opt
[0]);
923 for(i
=0; i
<opt
[1]; i
++)
924 DBG(" %02x", opt
[2+i
]);
933 if (conn
->start_state
== BSS_CONNECT_NOCAP
)
934 BGP_TRACE(D_PACKETS
, "Ignoring received capabilities");
936 bgp_parse_capabilities(conn
, opt
+ 2, ol
);
941 * BGP specs don't tell us to send which option
942 * we didn't recognize, but it's common practice
943 * to do so. Also, capability negotiation with
944 * Cisco routers doesn't work without that.
946 bgp_error(conn
, 2, 4, opt
, ol
);
956 bgp_rx_open(struct bgp_conn
*conn
, byte
*pkt
, uint len
)
958 struct bgp_conn
*other
;
959 struct bgp_proto
*p
= conn
->bgp
;
965 if (conn
->state
!= BS_OPENSENT
)
966 { bgp_error(conn
, 5, fsm_err_subcode
[conn
->state
], NULL
, 0); return; }
968 /* Check message contents */
969 if (len
< 29 || len
!= 29U + pkt
[28])
970 { bgp_error(conn
, 1, 2, pkt
+16, 2); return; }
971 if (pkt
[19] != BGP_VERSION
)
972 { bgp_error(conn
, 2, 1, pkt
+19, 1); return; } /* RFC 1771 says 16 bits, draft-09 tells to use 8 */
973 conn
->advertised_as
= base_as
= get_u16(pkt
+20);
974 hold
= get_u16(pkt
+22);
975 id
= get_u32(pkt
+24);
976 BGP_TRACE(D_PACKETS
, "Got OPEN(as=%d,hold=%d,id=%08x)", conn
->advertised_as
, hold
, id
);
978 if (bgp_parse_options(conn
, pkt
+29, pkt
[28]))
981 if (hold
> 0 && hold
< 3)
982 { bgp_error(conn
, 2, 6, pkt
+22, 2); return; }
984 /* RFC 6286 2.2 - router ID is nonzero and AS-wide unique */
985 if (!id
|| (p
->is_internal
&& id
== p
->local_id
))
986 { bgp_error(conn
, 2, 3, pkt
+24, -4); return; }
988 if ((conn
->advertised_as
!= base_as
) && (base_as
!= AS_TRANS
))
989 log(L_WARN
"%s: Peer advertised inconsistent AS numbers", p
->p
.name
);
991 if (conn
->advertised_as
!= p
->remote_as
)
993 if (conn
->peer_as4_support
)
995 u32 val
= htonl(conn
->advertised_as
);
996 bgp_error(conn
, 2, 2, (byte
*) &val
, 4);
999 bgp_error(conn
, 2, 2, pkt
+20, 2);
1004 /* Check the other connection */
1005 other
= (conn
== &p
->outgoing_conn
) ? &p
->incoming_conn
: &p
->outgoing_conn
;
1006 switch (other
->state
)
1010 /* Stop outgoing connection attempts */
1011 bgp_conn_enter_idle_state(other
);
1019 case BS_OPENCONFIRM
:
1021 * Description of collision detection rules in RFC 4271 is confusing and
1022 * contradictory, but it is essentially:
1024 * 1. Router with higher ID is dominant
1025 * 2. If both have the same ID, router with higher ASN is dominant [RFC6286]
1026 * 3. When both connections are in OpenConfirm state, one initiated by
1027 * the dominant router is kept.
1029 * The first line in the expression below evaluates whether the neighbor
1030 * is dominant, the second line whether the new connection was initiated
1031 * by the neighbor. If both are true (or both are false), we keep the new
1032 * connection, otherwise we keep the old one.
1034 if (((p
->local_id
< id
) || ((p
->local_id
== id
) && (p
->local_as
< p
->remote_as
)))
1035 == (conn
== &p
->incoming_conn
))
1037 /* Should close the other connection */
1038 BGP_TRACE(D_EVENTS
, "Connection collision, giving up the other connection");
1039 bgp_error(other
, 6, 7, NULL
, 0);
1043 case BS_ESTABLISHED
:
1044 /* Should close this connection */
1045 BGP_TRACE(D_EVENTS
, "Connection collision, giving up this connection");
1046 bgp_error(conn
, 6, 7, NULL
, 0);
1049 bug("bgp_rx_open: Unknown state");
1052 /* Update our local variables */
1053 conn
->hold_time
= MIN(hold
, p
->cf
->hold_time
);
1054 conn
->keepalive_time
= p
->cf
->keepalive_time
? : conn
->hold_time
/ 3;
1056 p
->as4_session
= p
->cf
->enable_as4
&& conn
->peer_as4_support
;
1057 p
->add_path_rx
= (p
->cf
->add_path
& ADD_PATH_RX
) && (conn
->peer_add_path
& ADD_PATH_TX
);
1058 p
->add_path_tx
= (p
->cf
->add_path
& ADD_PATH_TX
) && (conn
->peer_add_path
& ADD_PATH_RX
);
1059 p
->gr_ready
= (p
->cf
->gr_mode
&& conn
->peer_gr_able
) ||
1060 (p
->cf
->llgr_mode
&& conn
->peer_llgr_able
);
1061 p
->ext_messages
= p
->cf
->enable_extended_messages
&& conn
->peer_ext_messages_support
;
1063 /* Update RA mode */
1065 p
->p
.accept_ra_types
= RA_ANY
;
1066 else if (p
->cf
->secondary
)
1067 p
->p
.accept_ra_types
= RA_ACCEPTED
;
1069 p
->p
.accept_ra_types
= RA_OPTIMAL
;
1071 DBG("BGP: Hold timer set to %d, keepalive to %d, AS to %d, ID to %x, AS4 session to %d\n", conn
->hold_time
, conn
->keepalive_time
, p
->remote_as
, p
->remote_id
, p
->as4_session
);
1073 bgp_schedule_packet(conn
, PKT_KEEPALIVE
);
1074 bgp_start_timer(conn
->hold_timer
, conn
->hold_time
);
1075 bgp_conn_enter_openconfirm_state(conn
);
1080 bgp_rx_end_mark(struct bgp_proto
*p
)
1082 BGP_TRACE(D_PACKETS
, "Got END-OF-RIB");
1084 if (p
->load_state
== BFS_LOADING
)
1085 p
->load_state
= BFS_NONE
;
1087 if (p
->p
.gr_recovery
)
1088 proto_graceful_restart_unlock(&p
->p
);
1091 bgp_graceful_restart_done(p
);
1095 #define DECODE_PREFIX(pp, ll) do { \
1096 if (p->add_path_rx) \
1098 if (ll < 5) { err=1; goto done; } \
1099 path_id = get_u32(pp); \
1106 if (b > BITS_PER_IP_ADDRESS) { err=10; goto done; } \
1108 if (ll < q) { err=1; goto done; } \
1109 memcpy(&prefix, pp, q); \
1113 prefix = ipa_and(prefix, ipa_mkmask(b)); \
1119 bgp_rte_update(struct bgp_proto
*p
, ip_addr prefix
, int pxlen
,
1120 u32 path_id
, u32
*last_id
, struct rte_src
**src
,
1123 if (path_id
!= *last_id
)
1125 *src
= rt_get_source(&p
->p
, path_id
);
1135 /* Prepare cached route attributes */
1140 /* Workaround for rta_lookup() breaking eattrs */
1141 ea_list
*ea
= a0
->eattrs
;
1142 *a
= rta_lookup(a0
);
1146 net
*n
= net_get(p
->p
.table
, prefix
, pxlen
);
1147 rte
*e
= rte_get_temp(rta_clone(*a
));
1150 e
->u
.bgp
.suppressed
= 0;
1151 e
->u
.bgp
.stale
= -1;
1152 rte_update2(p
->p
.main_ahook
, n
, e
, *src
);
1156 bgp_rte_withdraw(struct bgp_proto
*p
, ip_addr prefix
, int pxlen
,
1157 u32 path_id
, u32
*last_id
, struct rte_src
**src
)
1159 if (path_id
!= *last_id
)
1161 *src
= rt_find_source(&p
->p
, path_id
);
1165 net
*n
= net_find(p
->p
.table
, prefix
, pxlen
);
1166 rte_update2( p
->p
.main_ahook
, n
, NULL
, *src
);
1170 bgp_set_next_hop(struct bgp_proto
*p
, rta
*a
)
1172 struct eattr
*nh
= ea_find(a
->eattrs
, EA_CODE(EAP_BGP
, BA_NEXT_HOP
));
1173 ip_addr
*nexthop
= (ip_addr
*) nh
->u
.ptr
->data
;
1176 int second
= (nh
->u
.ptr
->length
== NEXT_HOP_LENGTH
) && ipa_nonzero(nexthop
[1]);
1178 /* First address should not be link-local, but may be zero in direct mode */
1179 if (ipa_is_link_local(*nexthop
))
1180 *nexthop
= IPA_NONE
;
1185 if (p
->cf
->gw_mode
== GW_DIRECT
)
1187 neighbor
*ng
= NULL
;
1189 if (ipa_nonzero(*nexthop
))
1190 ng
= neigh_find(&p
->p
, nexthop
, 0);
1191 else if (second
) /* GW_DIRECT -> single_hop -> p->neigh != NULL */
1192 ng
= neigh_find2(&p
->p
, nexthop
+ 1, p
->neigh
->iface
, 0);
1198 if (ng
->scope
== SCOPE_HOST
)
1201 a
->dest
= RTD_ROUTER
;
1203 a
->iface
= ng
->iface
;
1204 a
->hostentry
= NULL
;
1207 else /* GW_RECURSIVE */
1209 if (ipa_zero(*nexthop
))
1212 rta_set_recursive_next_hop(p
->p
.table
, a
, p
->igp_table
, nexthop
, nexthop
+ second
);
1218 #ifndef IPV6 /* IPv4 version */
1221 bgp_do_rx_update(struct bgp_conn
*conn
,
1222 byte
*withdrawn
, int withdrawn_len
,
1223 byte
*nlri
, int nlri_len
,
1224 byte
*attrs
, int attr_len
)
1226 struct bgp_proto
*p
= conn
->bgp
;
1227 struct rte_src
*src
= p
->p
.main_source
;
1234 /* Check for End-of-RIB marker */
1235 if (!withdrawn_len
&& !attr_len
&& !nlri_len
)
1241 /* Withdraw routes */
1242 while (withdrawn_len
)
1244 DECODE_PREFIX(withdrawn
, withdrawn_len
);
1245 DBG("Withdraw %I/%d\n", prefix
, pxlen
);
1247 bgp_rte_withdraw(p
, prefix
, pxlen
, path_id
, &last_id
, &src
);
1250 if (!attr_len
&& !nlri_len
) /* shortcut */
1253 a0
= bgp_decode_attrs(conn
, attrs
, attr_len
, bgp_linpool
, nlri_len
);
1255 if (conn
->state
!= BS_ESTABLISHED
) /* fatal error during decoding */
1258 if (a0
&& nlri_len
&& !bgp_set_next_hop(p
, a0
))
1262 src
= p
->p
.main_source
;
1266 DECODE_PREFIX(nlri
, nlri_len
);
1267 DBG("Add %I/%d\n", prefix
, pxlen
);
1270 bgp_rte_update(p
, prefix
, pxlen
, path_id
, &last_id
, &src
, a0
, &a
);
1271 else /* Forced withdraw as a result of soft error */
1272 bgp_rte_withdraw(p
, prefix
, pxlen
, path_id
, &last_id
, &src
);
1280 bgp_error(conn
, 3, err
, NULL
, 0);
1285 #else /* IPv6 version */
1287 #define DO_NLRI(name) \
1288 x = p->name##_start; \
1289 len = len0 = p->name##_len; \
1292 if (len < 3) { err=9; goto done; } \
1296 DBG("\tNLRI AF=%d sub=%d len=%d\n", af, x[-1], len);\
1300 if (af == BGP_AF_IPV6)
1303 bgp_attach_next_hop(rta
*a0
, byte
*x
)
1305 ip_addr
*nh
= (ip_addr
*) bgp_attach_attr_wa(&a0
->eattrs
, bgp_linpool
, BA_NEXT_HOP
, NEXT_HOP_LENGTH
);
1306 memcpy(nh
, x
+1, 16);
1309 /* We store received link local address in the other part of BA_NEXT_HOP eattr. */
1312 memcpy(nh
+1, x
+17, 16);
1321 bgp_do_rx_update(struct bgp_conn
*conn
,
1322 byte
*withdrawn UNUSED
, int withdrawn_len
,
1323 byte
*nlri UNUSED
, int nlri_len
,
1324 byte
*attrs
, int attr_len
)
1326 struct bgp_proto
*p
= conn
->bgp
;
1327 struct rte_src
*src
= p
->p
.main_source
;
1337 p
->mp_reach_len
= 0;
1338 p
->mp_unreach_len
= 0;
1339 a0
= bgp_decode_attrs(conn
, attrs
, attr_len
, bgp_linpool
, 0);
1341 if (conn
->state
!= BS_ESTABLISHED
) /* fatal error during decoding */
1344 /* Check for End-of-RIB marker */
1345 if ((attr_len
< 8) && !withdrawn_len
&& !nlri_len
&& !p
->mp_reach_len
&&
1346 (p
->mp_unreach_len
== 3) && (get_u16(p
->mp_unreach_start
) == BGP_AF_IPV6
))
1356 DECODE_PREFIX(x
, len
);
1357 DBG("Withdraw %I/%d\n", prefix
, pxlen
);
1358 bgp_rte_withdraw(p
, prefix
, pxlen
, path_id
, &last_id
, &src
);
1364 /* Create fake NEXT_HOP attribute */
1365 if (len
< 1 || (*x
!= 16 && *x
!= 32) || len
< *x
+ 2)
1366 { err
= 9; goto done
; }
1369 bgp_attach_next_hop(a0
, x
);
1371 /* Also ignore one reserved byte */
1375 if (a0
&& ! bgp_set_next_hop(p
, a0
))
1379 src
= p
->p
.main_source
;
1383 DECODE_PREFIX(x
, len
);
1384 DBG("Add %I/%d\n", prefix
, pxlen
);
1387 bgp_rte_update(p
, prefix
, pxlen
, path_id
, &last_id
, &src
, a0
, &a
);
1388 else /* Forced withdraw as a result of soft error */
1389 bgp_rte_withdraw(p
, prefix
, pxlen
, path_id
, &last_id
, &src
);
1397 if (err
) /* Use subcode 9, not err */
1398 bgp_error(conn
, 3, 9, NULL
, 0);
1406 bgp_rx_update(struct bgp_conn
*conn
, byte
*pkt
, uint len
)
1408 struct bgp_proto
*p
= conn
->bgp
;
1409 byte
*withdrawn
, *attrs
, *nlri
;
1410 uint withdrawn_len
, attr_len
, nlri_len
;
1412 BGP_TRACE_RL(&rl_rcv_update
, D_PACKETS
, "Got UPDATE");
1414 /* Workaround for some BGP implementations that skip initial KEEPALIVE */
1415 if (conn
->state
== BS_OPENCONFIRM
)
1416 bgp_conn_enter_established_state(conn
);
1418 if (conn
->state
!= BS_ESTABLISHED
)
1419 { bgp_error(conn
, 5, fsm_err_subcode
[conn
->state
], NULL
, 0); return; }
1420 bgp_start_timer(conn
->hold_timer
, conn
->hold_time
);
1422 /* Find parts of the packet and check sizes */
1425 bgp_error(conn
, 1, 2, pkt
+16, 2);
1428 withdrawn
= pkt
+ 21;
1429 withdrawn_len
= get_u16(pkt
+ 19);
1430 if (withdrawn_len
+ 23 > len
)
1432 attrs
= withdrawn
+ withdrawn_len
+ 2;
1433 attr_len
= get_u16(attrs
- 2);
1434 if (withdrawn_len
+ attr_len
+ 23 > len
)
1436 nlri
= attrs
+ attr_len
;
1437 nlri_len
= len
- withdrawn_len
- attr_len
- 23;
1438 if (!attr_len
&& nlri_len
)
1440 DBG("Sizes: withdrawn=%d, attrs=%d, NLRI=%d\n", withdrawn_len
, attr_len
, nlri_len
);
1442 lp_flush(bgp_linpool
);
1444 bgp_do_rx_update(conn
, withdrawn
, withdrawn_len
, nlri
, nlri_len
, attrs
, attr_len
);
1448 bgp_error(conn
, 3, 1, NULL
, 0);
1454 } bgp_msg_table
[] = {
1455 { 1, 0, "Invalid message header" },
1456 { 1, 1, "Connection not synchronized" },
1457 { 1, 2, "Bad message length" },
1458 { 1, 3, "Bad message type" },
1459 { 2, 0, "Invalid OPEN message" },
1460 { 2, 1, "Unsupported version number" },
1461 { 2, 2, "Bad peer AS" },
1462 { 2, 3, "Bad BGP identifier" },
1463 { 2, 4, "Unsupported optional parameter" },
1464 { 2, 5, "Authentication failure" },
1465 { 2, 6, "Unacceptable hold time" },
1466 { 2, 7, "Required capability missing" }, /* [RFC5492] */
1467 { 2, 8, "No supported AFI/SAFI" }, /* This error msg is nonstandard */
1468 { 3, 0, "Invalid UPDATE message" },
1469 { 3, 1, "Malformed attribute list" },
1470 { 3, 2, "Unrecognized well-known attribute" },
1471 { 3, 3, "Missing mandatory attribute" },
1472 { 3, 4, "Invalid attribute flags" },
1473 { 3, 5, "Invalid attribute length" },
1474 { 3, 6, "Invalid ORIGIN attribute" },
1475 { 3, 7, "AS routing loop" }, /* Deprecated */
1476 { 3, 8, "Invalid NEXT_HOP attribute" },
1477 { 3, 9, "Optional attribute error" },
1478 { 3, 10, "Invalid network field" },
1479 { 3, 11, "Malformed AS_PATH" },
1480 { 4, 0, "Hold timer expired" },
1481 { 5, 0, "Finite state machine error" }, /* Subcodes are according to [RFC6608] */
1482 { 5, 1, "Unexpected message in OpenSent state" },
1483 { 5, 2, "Unexpected message in OpenConfirm state" },
1484 { 5, 3, "Unexpected message in Established state" },
1485 { 6, 0, "Cease" }, /* Subcodes are according to [RFC4486] */
1486 { 6, 1, "Maximum number of prefixes reached" },
1487 { 6, 2, "Administrative shutdown" },
1488 { 6, 3, "Peer de-configured" },
1489 { 6, 4, "Administrative reset" },
1490 { 6, 5, "Connection rejected" },
1491 { 6, 6, "Other configuration change" },
1492 { 6, 7, "Connection collision resolution" },
1493 { 6, 8, "Out of Resources" },
1494 { 7, 0, "Invalid ROUTE-REFRESH message" }, /* [RFC7313] */
1495 { 7, 1, "Invalid ROUTE-REFRESH message length" } /* [RFC7313] */
1499 * bgp_error_dsc - return BGP error description
1500 * @code: BGP error code
1501 * @subcode: BGP error subcode
1503 * bgp_error_dsc() returns error description for BGP errors
1504 * which might be static string or given temporary buffer.
1507 bgp_error_dsc(unsigned code
, unsigned subcode
)
1509 static char buff
[32];
1511 for (i
=0; i
< ARRAY_SIZE(bgp_msg_table
); i
++)
1512 if (bgp_msg_table
[i
].major
== code
&& bgp_msg_table
[i
].minor
== subcode
)
1514 return bgp_msg_table
[i
].msg
;
1517 bsprintf(buff
, "Unknown error %d.%d", code
, subcode
);
1521 /* RFC 8203 - shutdown communication message */
1523 bgp_handle_message(struct bgp_proto
*p
, byte
*data
, uint len
, byte
**bp
)
1525 byte
*msg
= data
+ 1;
1526 uint msg_len
= data
[0];
1529 /* Handle zero length message */
1533 /* Handle proper message */
1534 if ((msg_len
> 128) && (msg_len
+ 1 > len
))
1537 /* Some elementary cleanup */
1538 for (i
= 0; i
< msg_len
; i
++)
1542 proto_set_message(&p
->p
, msg
, msg_len
);
1543 *bp
+= bsprintf(*bp
, ": \"%s\"", p
->p
.message
);
1548 bgp_log_error(struct bgp_proto
*p
, u8
class, char *msg
, unsigned code
, unsigned subcode
, byte
*data
, unsigned len
)
1550 byte argbuf
[256], *t
= argbuf
;
1553 /* Don't report Cease messages generated by myself */
1554 if (code
== 6 && class == BE_BGP_TX
)
1557 /* Reset shutdown message */
1558 if ((code
== 6) && ((subcode
== 2) || (subcode
== 4)))
1559 proto_set_message(&p
->p
, NULL
, 0);
1563 /* Bad peer AS - we would like to print the AS */
1564 if ((code
== 2) && (subcode
== 2) && ((len
== 2) || (len
== 4)))
1566 t
+= bsprintf(t
, ": %u", (len
== 2) ? get_u16(data
) : get_u32(data
));
1570 /* RFC 8203 - shutdown communication */
1571 if (((code
== 6) && ((subcode
== 2) || (subcode
== 4))))
1572 if (bgp_handle_message(p
, data
, len
, &t
))
1579 for (i
=0; i
<len
; i
++)
1580 t
+= bsprintf(t
, "%02x", data
[i
]);
1585 const byte
*dsc
= bgp_error_dsc(code
, subcode
);
1586 log(L_REMOTE
"%s: %s: %s%s", p
->p
.name
, msg
, dsc
, argbuf
);
1590 bgp_rx_notification(struct bgp_conn
*conn
, byte
*pkt
, uint len
)
1592 struct bgp_proto
*p
= conn
->bgp
;
1595 bgp_error(conn
, 1, 2, pkt
+16, 2);
1599 unsigned code
= pkt
[19];
1600 unsigned subcode
= pkt
[20];
1601 int err
= (code
!= 6);
1603 bgp_log_error(p
, BE_BGP_RX
, "Received", code
, subcode
, pkt
+21, len
-21);
1604 bgp_store_error(p
, conn
, BE_BGP_RX
, (code
<< 16) | subcode
);
1607 if ((code
== 2) && ((subcode
== 4) || (subcode
== 7))
1608 /* Error related to capability:
1609 * 4 - Peer does not support capabilities at all.
1610 * 7 - Peer request some capability. Strange unless it is IPv6 only peer.
1612 && (p
->cf
->capabilities
== 2)
1613 /* Capabilities are not explicitly enabled or disabled, therefore heuristic is used */
1614 && (conn
->start_state
== BSS_CONNECT
)
1615 /* Failed connection attempt have used capabilities */
1616 && (p
->cf
->remote_as
<= 0xFFFF))
1617 /* Not possible with disabled capabilities */
1619 /* We try connect without capabilities */
1620 log(L_WARN
"%s: Capability related error received, retry with capabilities disabled", p
->p
.name
);
1621 p
->start_state
= BSS_CONNECT_NOCAP
;
1626 bgp_conn_enter_close_state(conn
);
1627 bgp_schedule_packet(conn
, PKT_SCHEDULE_CLOSE
);
1631 bgp_update_startup_delay(p
);
1632 bgp_stop(p
, 0, NULL
, 0);
1636 uint subcode_bit
= 1 << ((subcode
<= 8) ? subcode
: 0);
1637 if (p
->cf
->disable_after_cease
& subcode_bit
)
1639 log(L_INFO
"%s: Disabled after Cease notification", p
->p
.name
);
1640 p
->startup_delay
= 0;
1647 bgp_rx_keepalive(struct bgp_conn
*conn
)
1649 struct bgp_proto
*p
= conn
->bgp
;
1651 BGP_TRACE(D_PACKETS
, "Got KEEPALIVE");
1652 bgp_start_timer(conn
->hold_timer
, conn
->hold_time
);
1653 switch (conn
->state
)
1655 case BS_OPENCONFIRM
:
1656 bgp_conn_enter_established_state(conn
);
1658 case BS_ESTABLISHED
:
1661 bgp_error(conn
, 5, fsm_err_subcode
[conn
->state
], NULL
, 0);
1666 bgp_rx_route_refresh(struct bgp_conn
*conn
, byte
*pkt
, uint len
)
1668 struct bgp_proto
*p
= conn
->bgp
;
1670 if (conn
->state
!= BS_ESTABLISHED
)
1671 { bgp_error(conn
, 5, fsm_err_subcode
[conn
->state
], NULL
, 0); return; }
1673 if (!p
->cf
->enable_refresh
)
1674 { bgp_error(conn
, 1, 3, pkt
+18, 1); return; }
1676 if (len
< (BGP_HEADER_LENGTH
+ 4))
1677 { bgp_error(conn
, 1, 2, pkt
+16, 2); return; }
1679 if (len
> (BGP_HEADER_LENGTH
+ 4))
1680 { bgp_error(conn
, 7, 1, pkt
, MIN(len
, 2048)); return; }
1682 /* FIXME - we ignore AFI/SAFI values, as we support
1683 just one value and even an error code for an invalid
1684 request is not defined */
1686 /* RFC 7313 redefined reserved field as RR message subtype */
1687 uint subtype
= conn
->peer_enhanced_refresh_support
? pkt
[21] : BGP_RR_REQUEST
;
1691 case BGP_RR_REQUEST
:
1692 BGP_TRACE(D_PACKETS
, "Got ROUTE-REFRESH");
1693 proto_request_feeding(&p
->p
);
1697 BGP_TRACE(D_PACKETS
, "Got BEGIN-OF-RR");
1698 bgp_refresh_begin(p
);
1702 BGP_TRACE(D_PACKETS
, "Got END-OF-RR");
1707 log(L_WARN
"%s: Got ROUTE-REFRESH message with unknown subtype %u, ignoring",
1708 p
->p
.name
, subtype
);
1715 * bgp_rx_packet - handle a received packet
1716 * @conn: BGP connection
1717 * @pkt: start of the packet
1720 * bgp_rx_packet() takes a newly received packet and calls the corresponding
1721 * packet handler according to the packet type.
1724 bgp_rx_packet(struct bgp_conn
*conn
, byte
*pkt
, unsigned len
)
1726 byte type
= pkt
[18];
1728 DBG("BGP: Got packet %02x (%d bytes)\n", type
, len
);
1730 if (conn
->bgp
->p
.mrtdump
& MD_MESSAGES
)
1731 bgp_dump_message(conn
, pkt
, len
);
1735 case PKT_OPEN
: return bgp_rx_open(conn
, pkt
, len
);
1736 case PKT_UPDATE
: return bgp_rx_update(conn
, pkt
, len
);
1737 case PKT_NOTIFICATION
: return bgp_rx_notification(conn
, pkt
, len
);
1738 case PKT_KEEPALIVE
: return bgp_rx_keepalive(conn
);
1739 case PKT_ROUTE_REFRESH
: return bgp_rx_route_refresh(conn
, pkt
, len
);
1740 default: bgp_error(conn
, 1, 3, pkt
+18, 1);
1745 * bgp_rx - handle received data
1747 * @size: amount of data received
1749 * bgp_rx() is called by the socket layer whenever new data arrive from
1750 * the underlying TCP connection. It assembles the data fragments to packets,
1751 * checks their headers and framing and passes complete packets to
1755 bgp_rx(sock
*sk
, uint size
)
1757 struct bgp_conn
*conn
= sk
->data
;
1758 struct bgp_proto
*p
= conn
->bgp
;
1759 byte
*pkt_start
= sk
->rbuf
;
1760 byte
*end
= pkt_start
+ size
;
1763 DBG("BGP: RX hook: Got %d bytes\n", size
);
1764 while (end
>= pkt_start
+ BGP_HEADER_LENGTH
)
1766 if ((conn
->state
== BS_CLOSE
) || (conn
->sk
!= sk
))
1769 if (pkt_start
[i
] != 0xff)
1771 bgp_error(conn
, 1, 1, NULL
, 0);
1774 len
= get_u16(pkt_start
+16);
1775 if (len
< BGP_HEADER_LENGTH
|| len
> bgp_max_packet_length(p
))
1777 bgp_error(conn
, 1, 2, pkt_start
+16, 2);
1780 if (end
< pkt_start
+ len
)
1782 bgp_rx_packet(conn
, pkt_start
, len
);
1785 if (pkt_start
!= sk
->rbuf
)
1787 memmove(sk
->rbuf
, pkt_start
, end
- pkt_start
);
1788 sk
->rpos
= sk
->rbuf
+ (end
- pkt_start
);