3 * Used to advertise routes for upstream (l2tp port, rather than gratiutious
4 * arp) and downstream--allowing routers to load-balance both.
6 * Implementation limitations:
7 * - We never listen for incoming connections (session always initiated by us).
8 * - Any routes advertised by the peer are accepted, but ignored.
9 * - No password support; neither RFC1771 (which no-one seems to do anyway)
10 * nor RFC2385 (which requires a kernel patch on 2.4 kernels).
13 char const *cvs_id_bgp
= "$Id: bgp.c,v 1.12 2005/09/02 23:39:36 bodea Exp $";
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
30 static void bgp_clear(struct bgp_peer
*peer
);
31 static void bgp_set_retry(struct bgp_peer
*peer
);
32 static void bgp_cidr(in_addr_t ip
, in_addr_t mask
, struct bgp_ip_prefix
*pfx
);
33 static struct bgp_route_list
*bgp_insert_route(struct bgp_route_list
*head
,
34 struct bgp_route_list
*new);
36 static void bgp_free_routes(struct bgp_route_list
*routes
);
37 static char const *bgp_msg_type_str(uint8_t type
);
38 static int bgp_connect(struct bgp_peer
*peer
);
39 static int bgp_handle_connect(struct bgp_peer
*peer
);
40 static int bgp_write(struct bgp_peer
*peer
);
41 static int bgp_read(struct bgp_peer
*peer
);
42 static int bgp_handle_input(struct bgp_peer
*peer
);
43 static int bgp_send_open(struct bgp_peer
*peer
);
44 static int bgp_send_keepalive(struct bgp_peer
*peer
);
45 static int bgp_send_update(struct bgp_peer
*peer
);
46 static int bgp_send_notification(struct bgp_peer
*peer
, uint8_t code
,
48 static int bgp_send_notification_full(struct bgp_peer
*peer
, uint8_t code
,
49 uint8_t subcode
, char *notification_data
, uint16_t data_len
);
51 static uint16_t our_as
;
52 static struct bgp_route_list
*bgp_routes
= 0;
54 int bgp_configured
= 0;
55 struct bgp_peer
*bgp_peers
= 0;
57 /* prepare peer structure, globals */
61 struct bgp_peer
*peer
;
63 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
66 memset(peer
, 0, sizeof(*peer
));
68 peer
->addr
= INADDR_NONE
;
70 peer
->state
= peer
->next_state
= Disabled
;
72 if (!((peer
->outbuf
= malloc(sizeof(*peer
->outbuf
)))
73 && (peer
->inbuf
= malloc(sizeof(*peer
->inbuf
)))))
75 LOG(0, 0, 0, "Can't allocate buffers for bgp peer (%s)\n",
81 peer
->edata
.type
= FD_TYPE_BGP
;
82 peer
->edata
.index
= i
;
93 bgp_configured
= 0; /* set by bgp_start */
98 /* start connection with a peer */
99 int bgp_start(struct bgp_peer
*peer
, char *name
, int as
, int keepalive
,
100 int hold
, int enable
)
105 struct bgp_path_attr a
;
107 char *p
= path_attrs
;
109 uint32_t metric
= htonl(BGP_METRIC
);
110 uint32_t no_export
= htonl(BGP_COMMUNITY_NO_EXPORT
);
115 if (peer
->state
!= Disabled
)
118 snprintf(peer
->name
, sizeof(peer
->name
), "%s", name
);
120 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
122 LOG(0, 0, 0, "Can't get address for BGP peer %s (%s)\n",
123 name
, h
? "no address" : hstrerror(h_errno
));
128 memcpy(&peer
->addr
, h
->h_addr
, sizeof(peer
->addr
));
129 peer
->as
= as
> 0 ? as
: our_as
;
130 ibgp
= peer
->as
== our_as
;
132 /* set initial timer values */
133 peer
->init_keepalive
= keepalive
== -1 ? BGP_KEEPALIVE_TIME
: keepalive
;
134 peer
->init_hold
= hold
== -1 ? BGP_HOLD_TIME
: hold
;
136 if (peer
->init_hold
< 3)
139 if (peer
->init_keepalive
* 3 > peer
->init_hold
)
140 peer
->init_keepalive
= peer
->init_hold
/ 3;
142 /* clear buffers, go to Idle state */
143 peer
->next_state
= Idle
;
146 /* set initial routing state */
147 peer
->routing
= enable
;
149 /* all our routes use the same attributes, so prepare it in advance */
150 if (peer
->path_attrs
)
151 free(peer
->path_attrs
);
153 peer
->path_attr_len
= 0;
156 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
157 a
.code
= BGP_PATH_ATTR_CODE_ORIGIN
;
159 a
.data
.s
.value
[0] = BGP_PATH_ATTR_CODE_ORIGIN_IGP
;
161 #define ADD_ATTRIBUTE() do { \
162 i = BGP_PATH_ATTR_SIZE(a); \
165 peer->path_attr_len += i; } while (0)
170 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
171 a
.code
= BGP_PATH_ATTR_CODE_AS_PATH
;
185 BGP_PATH_ATTR_CODE_AS_PATH_AS_SEQUENCE
,
190 a
.data
.s
.len
= sizeof(as_path
);
191 memcpy(&a
.data
.s
.value
, &as_path
, sizeof(as_path
));
196 /* MULTI_EXIT_DISC */
197 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
;
198 a
.code
= BGP_PATH_ATTR_CODE_MULTI_EXIT_DISC
;
199 a
.data
.s
.len
= sizeof(metric
);
200 memcpy(a
.data
.s
.value
, &metric
, sizeof(metric
));
206 uint32_t local_pref
= htonl(BGP_LOCAL_PREF
);
209 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
210 a
.code
= BGP_PATH_ATTR_CODE_LOCAL_PREF
;
211 a
.data
.s
.len
= sizeof(local_pref
);
212 memcpy(a
.data
.s
.value
, &local_pref
, sizeof(local_pref
));
218 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
| BGP_PATH_ATTR_FLAG_TRANS
;
219 a
.code
= BGP_PATH_ATTR_CODE_COMMUNITIES
;
220 a
.data
.s
.len
= sizeof(no_export
);
221 memcpy(a
.data
.s
.value
, &no_export
, sizeof(no_export
));
225 /* remember the len before adding NEXT_HOP */
226 peer
->path_attr_len_without_nexthop
= peer
->path_attr_len
;
229 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
230 a
.code
= BGP_PATH_ATTR_CODE_NEXT_HOP
;
231 ip
= my_address
; /* we're it */
232 a
.data
.s
.len
= sizeof(ip
);
233 memcpy(a
.data
.s
.value
, &ip
, sizeof(ip
));
237 if (!(peer
->path_attrs
= malloc(peer
->path_attr_len
)))
239 LOG(0, 0, 0, "Can't allocate path_attrs for %s (%s)\n",
240 name
, strerror(errno
));
245 memcpy(peer
->path_attrs
, path_attrs
, peer
->path_attr_len
);
247 LOG(4, 0, 0, "Initiating BGP connection to %s (routing %s)\n",
248 name
, enable
? "enabled" : "suspended");
250 /* we have at least one peer configured */
254 return bgp_connect(peer
);
257 /* clear counters, timers, routes and buffers; close socket; move to
258 next_state, which may be Disabled or Idle */
259 static void bgp_clear(struct bgp_peer
*peer
)
261 if (peer
->sock
!= -1)
267 peer
->keepalive_time
= 0;
268 peer
->expire_time
= 0;
270 peer
->keepalive
= peer
->init_keepalive
;
271 peer
->hold
= peer
->init_hold
;
273 bgp_free_routes(peer
->routes
);
276 peer
->outbuf
->packet
.header
.len
= 0;
277 peer
->outbuf
->done
= 0;
278 peer
->inbuf
->packet
.header
.len
= 0;
279 peer
->inbuf
->done
= 0;
284 if (peer
->state
!= peer
->next_state
)
286 peer
->state
= peer
->next_state
;
287 peer
->state_time
= time_now
;
289 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
290 bgp_state_str(peer
->next_state
));
294 /* initiate a clean shutdown */
295 void bgp_stop(struct bgp_peer
*peer
)
297 LOG(4, 0, 0, "Terminating BGP connection to %s\n", peer
->name
);
298 bgp_send_notification(peer
, BGP_ERR_CEASE
, 0);
301 /* drop connection (if any) and set state to Disabled */
302 void bgp_halt(struct bgp_peer
*peer
)
304 LOG(4, 0, 0, "Aborting BGP connection to %s\n", peer
->name
);
305 peer
->next_state
= Disabled
;
309 /* drop connection (if any) and set to Idle for connection retry */
310 int bgp_restart(struct bgp_peer
*peer
)
312 peer
->next_state
= Idle
;
316 peer
->retry_time
= time_now
;
317 peer
->retry_count
= 0;
320 return bgp_connect(peer
);
323 static void bgp_set_retry(struct bgp_peer
*peer
)
325 if (peer
->retry_count
++ < BGP_MAX_RETRY
)
327 peer
->retry_time
= time_now
+ (BGP_RETRY_BACKOFF
* peer
->retry_count
);
328 peer
->next_state
= Idle
;
332 bgp_halt(peer
); /* give up */
335 /* convert ip/mask to CIDR notation */
336 static void bgp_cidr(in_addr_t ip
, in_addr_t mask
, struct bgp_ip_prefix
*pfx
)
341 /* convert to prefix notation */
345 if (!mask
) /* bogus */
348 for (i
= 0; i
< 32 && ((b
= ntohl(1 << i
)), !(mask
& b
)); i
++)
355 /* insert route into list; sorted */
356 static struct bgp_route_list
*bgp_insert_route(struct bgp_route_list
*head
,
357 struct bgp_route_list
*new)
359 struct bgp_route_list
*p
= head
;
360 struct bgp_route_list
*e
= 0;
362 while (p
&& memcmp(&p
->dest
, &new->dest
, sizeof(p
->dest
)) < 0)
382 /* add route to list for peers */
384 * Note: this doesn't do route aggregation, nor drop routes if a less
385 * specific match already exists (partly because I'm lazy, but also so
386 * that if that route is later deleted we don't have to be concerned
387 * about adding back the more specific one).
389 int bgp_add_route(in_addr_t ip
, in_addr_t mask
)
391 struct bgp_route_list
*r
= bgp_routes
;
392 struct bgp_route_list add
;
395 bgp_cidr(ip
, mask
, &add
.dest
);
398 /* check for duplicate */
401 i
= memcmp(&r
->dest
, &add
.dest
, sizeof(r
->dest
));
403 return 1; /* already covered */
411 /* insert into route list; sorted */
412 if (!(r
= malloc(sizeof(*r
))))
414 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
415 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
, strerror(errno
));
420 memcpy(r
, &add
, sizeof(*r
));
421 bgp_routes
= bgp_insert_route(bgp_routes
, r
);
423 /* flag established peers for update */
424 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
425 if (bgp_peers
[i
].state
== Established
)
426 bgp_peers
[i
].update_routes
= 1;
428 LOG(4, 0, 0, "Registered BGP route %s/%d\n",
429 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
);
434 /* remove route from list for peers */
435 int bgp_del_route(in_addr_t ip
, in_addr_t mask
)
437 struct bgp_route_list
*r
= bgp_routes
;
438 struct bgp_route_list
*e
= 0;
439 struct bgp_route_list del
;
442 bgp_cidr(ip
, mask
, &del
.dest
);
445 /* find entry in routes list and remove */
448 i
= memcmp(&r
->dest
, &del
.dest
, sizeof(r
->dest
));
454 bgp_routes
= r
->next
;
472 /* flag established peers for update */
473 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
474 if (bgp_peers
[i
].state
== Established
)
475 bgp_peers
[i
].update_routes
= 1;
477 LOG(4, 0, 0, "Removed BGP route %s/%d\n",
478 fmtaddr(del
.dest
.prefix
, 0), del
.dest
.len
);
483 /* enable or disable routing */
484 void bgp_enable_routing(int enable
)
488 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
490 bgp_peers
[i
].routing
= enable
;
492 /* flag established peers for update */
493 if (bgp_peers
[i
].state
== Established
)
494 bgp_peers
[i
].update_routes
= 1;
497 LOG(4, 0, 0, "%s BGP routing\n", enable
? "Enabled" : "Suspended");
501 # include <sys/epoll.h>
503 # include "fake_epoll.h"
506 /* return a bitmask of the events required to poll this peer's fd */
514 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
516 struct bgp_peer
*peer
= &bgp_peers
[i
];
519 if (peer
->state
== Disabled
|| peer
->state
== Idle
)
522 if (peer
->inbuf
->done
< BGP_MAX_PACKET_SIZE
)
525 if (peer
->state
== Connect
|| /* connection in progress */
526 peer
->update_routes
|| /* routing updates */
527 peer
->outbuf
->packet
.header
.len
) /* pending output */
530 if (peer
->events
!= events
)
532 struct epoll_event ev
;
534 ev
.events
= peer
->events
= events
;
535 ev
.data
.ptr
= &peer
->edata
;
536 epoll_ctl(epollfd
, EPOLL_CTL_MOD
, peer
->sock
, &ev
);
543 /* process bgp events/timers */
544 int bgp_process(uint32_t events
[])
551 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
553 struct bgp_peer
*peer
= &bgp_peers
[i
];
555 if (*peer
->name
&& peer
->cli_flag
== BGP_CLI_RESTART
)
561 if (peer
->state
== Disabled
)
566 switch (peer
->cli_flag
)
568 case BGP_CLI_SUSPEND
:
572 if (peer
->state
== Established
)
573 peer
->update_routes
= 1;
582 if (peer
->state
== Established
)
583 peer
->update_routes
= 1;
592 /* handle empty/fill of buffers */
593 if (events
[i
] & EPOLLOUT
)
596 if (peer
->state
== Connect
)
597 r
= bgp_handle_connect(peer
);
598 else if (peer
->outbuf
->packet
.header
.len
)
605 if (events
[i
] & (EPOLLIN
|EPOLLHUP
))
611 /* process input buffer contents */
612 while (peer
->inbuf
->done
>= sizeof(peer
->inbuf
->packet
.header
)
613 && !peer
->outbuf
->packet
.header
.len
) /* may need to queue a response */
615 if (bgp_handle_input(peer
) < 0)
619 /* process pending updates */
620 if (peer
->update_routes
621 && !peer
->outbuf
->packet
.header
.len
) /* ditto */
623 if (!bgp_send_update(peer
))
628 if (peer
->state
== Established
)
630 if (time_now
> peer
->expire_time
)
632 LOG(1, 0, 0, "No message from BGP peer %s in %ds\n",
633 peer
->name
, peer
->hold
);
635 bgp_send_notification(peer
, BGP_ERR_HOLD_TIMER_EXP
, 0);
639 if (time_now
> peer
->keepalive_time
&& !peer
->outbuf
->packet
.header
.len
)
640 bgp_send_keepalive(peer
);
642 else if (peer
->state
== Idle
)
644 if (time_now
> peer
->retry_time
)
647 else if (time_now
> peer
->state_time
+ BGP_STATE_TIME
)
649 LOG(1, 0, 0, "%s timer expired for BGP peer %s\n",
650 bgp_state_str(peer
->state
), peer
->name
);
659 static void bgp_free_routes(struct bgp_route_list
*routes
)
661 struct bgp_route_list
*tmp
;
663 while ((tmp
= routes
))
670 char const *bgp_state_str(enum bgp_state state
)
674 case Disabled
: return "Disabled";
675 case Idle
: return "Idle";
676 case Connect
: return "Connect";
677 case Active
: return "Active";
678 case OpenSent
: return "OpenSent";
679 case OpenConfirm
: return "OpenConfirm";
680 case Established
: return "Established";
686 static char const *bgp_msg_type_str(uint8_t type
)
690 case BGP_MSG_OPEN
: return "OPEN";
691 case BGP_MSG_UPDATE
: return "UPDATE";
692 case BGP_MSG_NOTIFICATION
: return "NOTIFICATION";
693 case BGP_MSG_KEEPALIVE
: return "KEEPALIVE";
699 /* attempt to connect to peer */
700 static int bgp_connect(struct bgp_peer
*peer
)
702 static int bgp_port
= 0;
703 struct sockaddr_in addr
;
704 struct epoll_event ev
;
708 struct servent
*serv
;
709 if (!(serv
= getservbyname("bgp", "tcp")))
711 LOG(0, 0, 0, "Can't get bgp service (%s)\n", strerror(errno
));
715 bgp_port
= serv
->s_port
;
718 if ((peer
->sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
720 LOG(0, 0, 0, "Can't create a socket for BGP peer %s (%s)\n",
721 peer
->name
, strerror(errno
));
723 peer
->state
= peer
->next_state
= Disabled
;
727 /* add to poll set */
728 ev
.events
= peer
->events
= EPOLLOUT
;
729 ev
.data
.ptr
= &peer
->edata
;
730 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, peer
->sock
, &ev
);
732 /* set to non-blocking */
733 fcntl(peer
->sock
, F_SETFL
, fcntl(peer
->sock
, F_GETFL
, 0) | O_NONBLOCK
);
736 memset(&addr
, 0, sizeof(addr
));
737 addr
.sin_family
= AF_INET
;
738 addr
.sin_port
= bgp_port
;
739 addr
.sin_addr
.s_addr
= peer
->addr
;
741 while (connect(peer
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1)
743 if (errno
== EINTR
) /* SIGALARM handler */
746 if (errno
!= EINPROGRESS
)
748 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n",
749 inet_ntoa(addr
.sin_addr
), strerror(errno
));
755 peer
->state
= Connect
;
756 peer
->state_time
= time_now
;
758 LOG(4, 0, 0, "BGP peer %s: state Connect\n", peer
->name
);
762 peer
->state
= Active
;
763 peer
->state_time
= time_now
;
764 peer
->retry_time
= peer
->retry_count
= 0;
766 LOG(4, 0, 0, "BGP peer %s: state Active\n", inet_ntoa(addr
.sin_addr
));
768 peer
->handle_ipv6_routes
= 0;
770 return bgp_send_open(peer
);
773 /* complete partial connection (state = Connect) */
774 static int bgp_handle_connect(struct bgp_peer
*peer
)
777 socklen_t len
= sizeof(int);
778 getsockopt(peer
->sock
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
781 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer
->name
,
788 peer
->state
= Active
;
789 peer
->state_time
= time_now
;
791 LOG(4, 0, 0, "BGP peer %s: state Active\n", peer
->name
);
793 peer
->handle_ipv6_routes
= 0;
795 return bgp_send_open(peer
);
798 /* initiate a write */
799 static int bgp_write(struct bgp_peer
*peer
)
801 int len
= htons(peer
->outbuf
->packet
.header
.len
);
804 while ((r
= write(peer
->sock
, &peer
->outbuf
->packet
+ peer
->outbuf
->done
,
805 len
- peer
->outbuf
->done
)) == -1)
814 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
816 LOG(1, 0, 0, "Can't write to BGP peer %s (%s)\n", peer
->name
,
825 peer
->outbuf
->done
+= r
;
829 LOG(4, 0, 0, "Sent %s to BGP peer %s\n",
830 bgp_msg_type_str(peer
->outbuf
->packet
.header
.type
), peer
->name
);
832 peer
->outbuf
->packet
.header
.len
= 0;
833 peer
->outbuf
->done
= 0;
835 if (peer
->state
== Established
)
836 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
838 if (peer
->state
!= peer
->next_state
)
840 if (peer
->next_state
== Disabled
|| peer
->next_state
== Idle
)
846 peer
->state
= peer
->next_state
;
847 peer
->state_time
= time_now
;
849 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
850 bgp_state_str(peer
->state
));
856 /* initiate a read */
857 static int bgp_read(struct bgp_peer
*peer
)
861 while ((r
= read(peer
->sock
, &peer
->inbuf
->packet
+ peer
->inbuf
->done
,
862 BGP_MAX_PACKET_SIZE
- peer
->inbuf
->done
)) < 1)
866 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
876 LOG(1, 0, 0, "Can't read from BGP peer %s (%s)\n", peer
->name
,
884 peer
->inbuf
->done
+= r
;
888 /* process buffered packets */
889 static int bgp_handle_input(struct bgp_peer
*peer
)
891 struct bgp_packet
*p
= &peer
->inbuf
->packet
;
892 int len
= ntohs(p
->header
.len
);
894 if (len
> BGP_MAX_PACKET_SIZE
)
896 LOG(1, 0, 0, "Bad header length from BGP %s\n", peer
->name
);
897 bgp_send_notification(peer
, BGP_ERR_HEADER
, BGP_ERR_HDR_BAD_LEN
);
901 if (peer
->inbuf
->done
< len
)
904 LOG(4, 0, 0, "Received %s from BGP peer %s\n",
905 bgp_msg_type_str(p
->header
.type
), peer
->name
);
907 switch (p
->header
.type
)
911 struct bgp_data_open data
;
914 off_t param_offset
, capability_offset
;
915 struct bgp_opt_param
*param
;
916 uint8_t capabilities_len
;
917 char *capabilities
= NULL
;
918 struct bgp_capability
*capability
;
919 struct bgp_mp_cap_param
*mp_cap
;
921 for (i
= 0; i
< sizeof(p
->header
.marker
); i
++)
923 if ((unsigned char) p
->header
.marker
[i
] != 0xff)
925 LOG(1, 0, 0, "Invalid marker from BGP peer %s\n",
928 bgp_send_notification(peer
, BGP_ERR_HEADER
,
929 BGP_ERR_HDR_NOT_SYNC
);
935 if (peer
->state
!= OpenSent
)
937 LOG(1, 0, 0, "OPEN from BGP peer %s in %s state\n",
938 peer
->name
, bgp_state_str(peer
->state
));
940 bgp_send_notification(peer
, BGP_ERR_FSM
, 0);
944 memcpy(&data
, p
->data
, len
- sizeof(p
->header
));
946 if (data
.version
!= BGP_VERSION
)
948 LOG(1, 0, 0, "Bad version (%d) sent by BGP peer %s\n",
949 (int) data
.version
, peer
->name
);
951 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_VERSION
);
955 if (ntohs(data
.as
) != peer
->as
)
957 LOG(1, 0, 0, "Bad AS sent by BGP peer %s (got %d, "
958 "expected %d)\n", peer
->name
, (int) htons(data
.as
),
961 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_BAD_AS
);
965 if ((hold
= ntohs(data
.hold_time
)) < 3)
967 LOG(1, 0, 0, "Bad hold time (%d) from BGP peer %s\n",
970 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_HOLD_TIME
);
974 /* pick lowest hold time */
975 if (hold
< peer
->hold
)
978 /* adjust our keepalive based on negotiated hold value */
979 if (peer
->keepalive
* 3 > peer
->hold
)
980 peer
->keepalive
= peer
->hold
/ 3;
982 /* check for optional parameters */
983 /* 2 is for the size of type + len (both uint8_t) */
984 for (param_offset
= 0;
985 param_offset
< data
.opt_len
;
986 param_offset
+= 2 + param
->len
)
988 param
= (struct bgp_opt_param
*)(&data
.opt_params
+ param_offset
);
991 if (data
.opt_len
- param_offset
< 2
992 || param
->len
> data
.opt_len
- param_offset
- 2)
994 LOG(1, 0, 0, "Malformed Optional Parameter list from BGP peer %s\n",
997 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_UNSPEC
);
1001 /* we know only one parameter type */
1002 if (param
->type
!= BGP_PARAM_TYPE_CAPABILITY
)
1004 LOG(1, 0, 0, "Unsupported Optional Parameter type %d from BGP peer %s\n",
1005 param
->type
, peer
->name
);
1007 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_UNSUP_PARAM
);
1011 capabilities_len
= param
->len
;
1012 capabilities
= (char *)¶m
->value
;
1015 /* look for BGP multiprotocol capability */
1018 for (capability_offset
= 0;
1019 capability_offset
< capabilities_len
;
1020 capability_offset
+= 2 + capability
->len
)
1022 capability
= (struct bgp_capability
*)(capabilities
+ capability_offset
);
1024 /* sensible check */
1025 if (capabilities_len
- capability_offset
< 2
1026 || capability
->len
> capabilities_len
- capability_offset
- 2)
1028 LOG(1, 0, 0, "Malformed Capabilities list from BGP peer %s\n",
1031 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_UNSPEC
);
1035 /* we only know one capability code */
1036 if (capability
->code
!= BGP_CAP_CODE_MP
1037 && capability
->len
!= sizeof(struct bgp_mp_cap_param
))
1039 LOG(4, 0, 0, "Unsupported Capability code %d from BGP peer %s\n",
1040 capability
->code
, peer
->name
);
1042 bgp_send_notification_full(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_UNSUP_CAP
,
1043 (char *)capability
, 2 + capability
->len
);
1044 /* we don't terminate, still; we just jump to the next one */
1048 mp_cap
= (struct bgp_mp_cap_param
*)&capability
->value
;
1049 /* the only <AFI, SAFI> tuple we support */
1050 if (ntohs(mp_cap
->afi
) != AF_INET6
&& mp_cap
->safi
!= BGP_MP_SAFI_UNICAST
)
1052 LOG(4, 0, 0, "Unsupported multiprotocol AFI %d and SAFI %d from BGP peer %s\n",
1053 mp_cap
->afi
, mp_cap
->safi
, peer
->name
);
1055 bgp_send_notification_full(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_UNSUP_CAP
,
1056 (char *)capability
, 2 + capability
->len
);
1057 /* we don't terminate, still; we just jump to the next one */
1061 peer
->handle_ipv6_routes
= 1;
1065 /* next transition requires an exchange of keepalives */
1066 bgp_send_keepalive(peer
);
1071 case BGP_MSG_KEEPALIVE
:
1072 if (peer
->state
== OpenConfirm
)
1074 peer
->state
= peer
->next_state
= Established
;
1075 peer
->state_time
= time_now
;
1076 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
1077 peer
->update_routes
= 1;
1078 peer
->retry_count
= 0;
1079 peer
->retry_time
= 0;
1081 LOG(4, 0, 0, "BGP peer %s: state Established\n", peer
->name
);
1086 case BGP_MSG_NOTIFICATION
:
1087 if (len
> sizeof(p
->header
))
1089 struct bgp_data_notification
*notification
=
1090 (struct bgp_data_notification
*) p
->data
;
1092 if (notification
->error_code
== BGP_ERR_CEASE
)
1094 LOG(4, 0, 0, "BGP peer %s sent CEASE\n", peer
->name
);
1099 if (notification
->error_code
== BGP_ERR_OPEN
1100 && notification
->error_subcode
== BGP_ERR_OPN_UNSUP_CAP
)
1102 /* the only capability we advertise is this one, so upon receiving
1103 an "unsupported capability" message, we disable IPv6 routes for
1105 LOG(4, 0, 0, "BGP peer %s doesn't support IPv6 routes advertisement\n", peer
->name
);
1106 peer
->handle_ipv6_routes
= 0;
1110 /* FIXME: should handle more notifications */
1111 LOG(4, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n",
1112 peer
->name
, (int) notification
->error_code
);
1119 peer
->expire_time
= time_now
+ peer
->hold
;
1121 /* see if there's another message in the same packet/buffer */
1122 if (peer
->inbuf
->done
> len
)
1124 peer
->inbuf
->done
-= len
;
1125 memmove(p
, (char *) p
+ len
, peer
->inbuf
->done
);
1129 peer
->inbuf
->packet
.header
.len
= 0;
1130 peer
->inbuf
->done
= 0;
1133 return peer
->inbuf
->done
;
1136 /* send/buffer OPEN message */
1137 static int bgp_send_open(struct bgp_peer
*peer
)
1139 struct bgp_data_open data
;
1140 struct bgp_mp_cap_param mp_ipv6
= { htons(AF_INET6
), 0, BGP_MP_SAFI_UNICAST
};
1141 struct bgp_capability cap_mp_ipv6
;
1142 struct bgp_opt_param param_cap_mp_ipv6
;
1143 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1145 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1146 sizeof(peer
->outbuf
->packet
.header
.marker
));
1148 peer
->outbuf
->packet
.header
.type
= BGP_MSG_OPEN
;
1150 data
.version
= BGP_VERSION
;
1151 data
.as
= htons(our_as
);
1152 data
.hold_time
= htons(peer
->hold
);
1153 data
.identifier
= my_address
;
1155 /* construct the param and capability */
1156 cap_mp_ipv6
.code
= BGP_CAP_CODE_MP
;
1157 cap_mp_ipv6
.len
= sizeof(mp_ipv6
);
1158 memcpy(&cap_mp_ipv6
.value
, &mp_ipv6
, cap_mp_ipv6
.len
);
1160 param_cap_mp_ipv6
.type
= BGP_PARAM_TYPE_CAPABILITY
;
1161 param_cap_mp_ipv6
.len
= 2 + sizeof(mp_ipv6
);
1162 memcpy(¶m_cap_mp_ipv6
.value
, &cap_mp_ipv6
, param_cap_mp_ipv6
.len
);
1164 data
.opt_len
= 2 + param_cap_mp_ipv6
.len
;
1165 memcpy(&data
.opt_params
, ¶m_cap_mp_ipv6
, data
.opt_len
);
1167 memcpy(peer
->outbuf
->packet
.data
, &data
, BGP_DATA_OPEN_SIZE
);
1168 len
+= BGP_DATA_OPEN_SIZE
;
1170 peer
->outbuf
->packet
.header
.len
= htons(len
);
1171 peer
->outbuf
->done
= 0;
1172 peer
->next_state
= OpenSent
;
1174 return bgp_write(peer
);
1177 /* send/buffer KEEPALIVE message */
1178 static int bgp_send_keepalive(struct bgp_peer
*peer
)
1180 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1181 sizeof(peer
->outbuf
->packet
.header
.marker
));
1183 peer
->outbuf
->packet
.header
.type
= BGP_MSG_KEEPALIVE
;
1184 peer
->outbuf
->packet
.header
.len
=
1185 htons(sizeof(peer
->outbuf
->packet
.header
));
1187 peer
->outbuf
->done
= 0;
1188 peer
->next_state
= (peer
->state
== OpenSent
) ? OpenConfirm
: peer
->state
;
1190 return bgp_write(peer
);
1193 /* send/buffer UPDATE message */
1194 static int bgp_send_update(struct bgp_peer
*peer
)
1196 uint16_t unf_len
= 0;
1198 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1199 struct bgp_route_list
*have
= peer
->routes
;
1200 struct bgp_route_list
*want
= peer
->routing
? bgp_routes
: 0;
1201 struct bgp_route_list
*e
= 0;
1202 struct bgp_route_list
*add
= 0;
1205 char *data
= (char *) &peer
->outbuf
->packet
.data
;
1207 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1208 char *max
= (char *) &peer
->outbuf
->packet
.data
1209 + sizeof(peer
->outbuf
->packet
.data
)
1210 - sizeof(attr_len
) - peer
->path_attr_len
- sizeof(struct bgp_ip_prefix
);
1212 /* skip over unf_len */
1213 data
+= sizeof(unf_len
);
1214 len
+= sizeof(unf_len
);
1216 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1217 sizeof(peer
->outbuf
->packet
.header
.marker
));
1219 peer
->outbuf
->packet
.header
.type
= BGP_MSG_UPDATE
;
1221 peer
->update_routes
= 0; /* tentatively clear */
1223 /* find differences */
1224 while ((have
|| want
) && data
< (max
- sizeof(struct bgp_ip_prefix
)))
1228 ? memcmp(&have
->dest
, &want
->dest
, sizeof(have
->dest
))
1233 if (s
< 0) /* found one to delete */
1235 struct bgp_route_list
*tmp
= have
;
1238 s
= BGP_IP_PREFIX_SIZE(tmp
->dest
);
1239 memcpy(data
, &tmp
->dest
, s
);
1244 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1245 fmtaddr(tmp
->dest
.prefix
, 0), tmp
->dest
.len
, peer
->name
);
1252 peer
->routes
= have
;
1258 e
= have
; /* stash the last found to relink above */
1262 else if (s
> 0) /* addition reqd. */
1266 peer
->update_routes
= 1; /* only one add per packet */
1280 peer
->update_routes
= 1; /* more to do */
1282 /* anything changed? */
1283 if (!(unf_len
|| add
))
1286 /* go back and insert unf_len */
1287 unf_len
= htons(unf_len
);
1288 memcpy(&peer
->outbuf
->packet
.data
, &unf_len
, sizeof(unf_len
));
1292 if (!(e
= malloc(sizeof(*e
))))
1294 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1295 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, strerror(errno
));
1300 memcpy(e
, add
, sizeof(*e
));
1302 peer
->routes
= bgp_insert_route(peer
->routes
, e
);
1304 attr_len
= htons(peer
->path_attr_len
);
1305 memcpy(data
, &attr_len
, sizeof(attr_len
));
1306 data
+= sizeof(attr_len
);
1307 len
+= sizeof(attr_len
);
1309 memcpy(data
, peer
->path_attrs
, peer
->path_attr_len
);
1310 data
+= peer
->path_attr_len
;
1311 len
+= peer
->path_attr_len
;
1313 s
= BGP_IP_PREFIX_SIZE(add
->dest
);
1314 memcpy(data
, &add
->dest
, s
);
1318 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1319 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, peer
->name
);
1324 memcpy(data
, &attr_len
, sizeof(attr_len
));
1325 data
+= sizeof(attr_len
);
1326 len
+= sizeof(attr_len
);
1329 peer
->outbuf
->packet
.header
.len
= htons(len
);
1330 peer
->outbuf
->done
= 0;
1332 return bgp_write(peer
);
1335 /* send/buffer NOTIFICATION message */
1336 static int bgp_send_notification(struct bgp_peer
*peer
, uint8_t code
,
1339 return bgp_send_notification_full(peer
, code
, subcode
, NULL
, 0);
1342 static int bgp_send_notification_full(struct bgp_peer
*peer
, uint8_t code
,
1343 uint8_t subcode
, char *notification_data
, uint16_t data_len
)
1345 struct bgp_data_notification data
;
1348 data
.error_code
= code
;
1349 len
+= sizeof(data
.error_code
);
1351 data
.error_subcode
= subcode
;
1352 len
+= sizeof(data
.error_code
);
1354 memcpy(data
.data
, notification_data
, data_len
);
1357 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1358 sizeof(peer
->outbuf
->packet
.header
.marker
));
1360 peer
->outbuf
->packet
.header
.type
= BGP_MSG_NOTIFICATION
;
1361 peer
->outbuf
->packet
.header
.len
=
1362 htons(sizeof(peer
->outbuf
->packet
.header
) + len
);
1364 memcpy(peer
->outbuf
->packet
.data
, &data
, len
);
1366 peer
->outbuf
->done
= 0;
1367 peer
->next_state
= code
== BGP_ERR_CEASE
? Disabled
: Idle
;
1369 /* we're dying; ignore any pending input */
1370 peer
->inbuf
->packet
.header
.len
= 0;
1371 peer
->inbuf
->done
= 0;
1373 return bgp_write(peer
);