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
,
49 static uint16_t our_as
;
50 static struct bgp_route_list
*bgp_routes
= 0;
52 int bgp_configured
= 0;
53 struct bgp_peer
*bgp_peers
= 0;
55 /* prepare peer structure, globals */
59 struct bgp_peer
*peer
;
61 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
64 memset(peer
, 0, sizeof(*peer
));
66 peer
->addr
= INADDR_NONE
;
68 peer
->state
= peer
->next_state
= Disabled
;
70 if (!((peer
->outbuf
= malloc(sizeof(*peer
->outbuf
)))
71 && (peer
->inbuf
= malloc(sizeof(*peer
->inbuf
)))))
73 LOG(0, 0, 0, "Can't allocate buffers for bgp peer (%s)\n",
79 peer
->edata
.type
= FD_TYPE_BGP
;
80 peer
->edata
.index
= i
;
91 bgp_configured
= 0; /* set by bgp_start */
96 /* start connection with a peer */
97 int bgp_start(struct bgp_peer
*peer
, char *name
, int as
, int keepalive
,
98 int hold
, struct in_addr update_source
, int enable
)
103 struct bgp_path_attr a
;
105 char *p
= path_attrs
;
107 uint32_t metric
= htonl(BGP_METRIC
);
108 uint32_t no_export
= htonl(BGP_COMMUNITY_NO_EXPORT
);
113 if (peer
->state
!= Disabled
)
116 snprintf(peer
->name
, sizeof(peer
->name
), "%s", name
);
118 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
120 LOG(0, 0, 0, "Can't get address for BGP peer %s (%s)\n",
121 name
, h
? "no address" : hstrerror(h_errno
));
126 memcpy(&peer
->addr
, h
->h_addr
, sizeof(peer
->addr
));
127 peer
->source_addr
= update_source
.s_addr
;
128 peer
->as
= as
> 0 ? as
: our_as
;
129 ibgp
= peer
->as
== our_as
;
131 /* set initial timer values */
132 peer
->init_keepalive
= keepalive
== -1 ? BGP_KEEPALIVE_TIME
: keepalive
;
133 peer
->init_hold
= hold
== -1 ? BGP_HOLD_TIME
: hold
;
135 if (peer
->init_hold
< 3)
138 if (peer
->init_keepalive
* 3 > peer
->init_hold
)
139 peer
->init_keepalive
= peer
->init_hold
/ 3;
141 /* clear buffers, go to Idle state */
142 peer
->next_state
= Idle
;
145 /* set initial routing state */
146 peer
->routing
= enable
;
148 /* all our routes use the same attributes, so prepare it in advance */
149 if (peer
->path_attrs
)
150 free(peer
->path_attrs
);
152 peer
->path_attr_len
= 0;
155 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
156 a
.code
= BGP_PATH_ATTR_CODE_ORIGIN
;
158 a
.data
.s
.value
[0] = BGP_PATH_ATTR_CODE_ORIGIN_IGP
;
160 #define ADD_ATTRIBUTE() do { \
161 i = BGP_PATH_ATTR_SIZE(a); \
164 peer->path_attr_len += i; } while (0)
169 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
170 a
.code
= BGP_PATH_ATTR_CODE_AS_PATH
;
184 BGP_PATH_ATTR_CODE_AS_PATH_AS_SEQUENCE
,
189 a
.data
.s
.len
= sizeof(as_path
);
190 memcpy(&a
.data
.s
.value
, &as_path
, sizeof(as_path
));
196 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
197 a
.code
= BGP_PATH_ATTR_CODE_NEXT_HOP
;
198 ip
= my_address
; /* we're it */
199 a
.data
.s
.len
= sizeof(ip
);
200 memcpy(a
.data
.s
.value
, &ip
, sizeof(ip
));
204 /* MULTI_EXIT_DISC */
205 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
;
206 a
.code
= BGP_PATH_ATTR_CODE_MULTI_EXIT_DISC
;
207 a
.data
.s
.len
= sizeof(metric
);
208 memcpy(a
.data
.s
.value
, &metric
, sizeof(metric
));
214 uint32_t local_pref
= htonl(BGP_LOCAL_PREF
);
217 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
218 a
.code
= BGP_PATH_ATTR_CODE_LOCAL_PREF
;
219 a
.data
.s
.len
= sizeof(local_pref
);
220 memcpy(a
.data
.s
.value
, &local_pref
, sizeof(local_pref
));
226 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
| BGP_PATH_ATTR_FLAG_TRANS
;
227 a
.code
= BGP_PATH_ATTR_CODE_COMMUNITIES
;
228 a
.data
.s
.len
= sizeof(no_export
);
229 memcpy(a
.data
.s
.value
, &no_export
, sizeof(no_export
));
233 if (!(peer
->path_attrs
= malloc(peer
->path_attr_len
)))
235 LOG(0, 0, 0, "Can't allocate path_attrs for %s (%s)\n",
236 name
, strerror(errno
));
241 memcpy(peer
->path_attrs
, path_attrs
, peer
->path_attr_len
);
243 LOG(4, 0, 0, "Initiating BGP connection to %s (routing %s)\n",
244 name
, enable
? "enabled" : "suspended");
246 /* we have at least one peer configured */
250 return bgp_connect(peer
);
253 /* clear counters, timers, routes and buffers; close socket; move to
254 next_state, which may be Disabled or Idle */
255 static void bgp_clear(struct bgp_peer
*peer
)
257 if (peer
->sock
!= -1)
263 peer
->keepalive_time
= 0;
264 peer
->expire_time
= 0;
266 peer
->keepalive
= peer
->init_keepalive
;
267 peer
->hold
= peer
->init_hold
;
269 bgp_free_routes(peer
->routes
);
272 peer
->outbuf
->packet
.header
.len
= 0;
273 peer
->outbuf
->done
= 0;
274 peer
->inbuf
->packet
.header
.len
= 0;
275 peer
->inbuf
->done
= 0;
280 if (peer
->state
!= peer
->next_state
)
282 peer
->state
= peer
->next_state
;
283 peer
->state_time
= time_now
;
285 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
286 bgp_state_str(peer
->next_state
));
290 /* initiate a clean shutdown */
291 void bgp_stop(struct bgp_peer
*peer
)
293 LOG(4, 0, 0, "Terminating BGP connection to %s\n", peer
->name
);
294 bgp_send_notification(peer
, BGP_ERR_CEASE
, 0);
297 /* drop connection (if any) and set state to Disabled */
298 void bgp_halt(struct bgp_peer
*peer
)
300 LOG(4, 0, 0, "Aborting BGP connection to %s\n", peer
->name
);
301 peer
->next_state
= Disabled
;
305 /* drop connection (if any) and set to Idle for connection retry */
306 int bgp_restart(struct bgp_peer
*peer
)
308 peer
->next_state
= Idle
;
312 peer
->retry_time
= time_now
;
313 peer
->retry_count
= 0;
316 return bgp_connect(peer
);
319 static void bgp_set_retry(struct bgp_peer
*peer
)
321 if (peer
->retry_count
++ < BGP_MAX_RETRY
)
323 peer
->retry_time
= time_now
+ (BGP_RETRY_BACKOFF
* peer
->retry_count
);
324 peer
->next_state
= Idle
;
328 bgp_halt(peer
); /* give up */
331 /* convert ip/mask to CIDR notation */
332 static void bgp_cidr(in_addr_t ip
, in_addr_t mask
, struct bgp_ip_prefix
*pfx
)
337 /* convert to prefix notation */
341 if (!mask
) /* bogus */
344 for (i
= 0; i
< 32 && ((b
= ntohl(1 << i
)), !(mask
& b
)); i
++)
351 /* insert route into list; sorted */
352 static struct bgp_route_list
*bgp_insert_route(struct bgp_route_list
*head
,
353 struct bgp_route_list
*new)
355 struct bgp_route_list
*p
= head
;
356 struct bgp_route_list
*e
= 0;
358 while (p
&& memcmp(&p
->dest
, &new->dest
, sizeof(p
->dest
)) < 0)
378 /* add route to list for peers */
380 * Note: this doesn't do route aggregation, nor drop routes if a less
381 * specific match already exists (partly because I'm lazy, but also so
382 * that if that route is later deleted we don't have to be concerned
383 * about adding back the more specific one).
385 int bgp_add_route(in_addr_t ip
, in_addr_t mask
)
387 struct bgp_route_list
*r
= bgp_routes
;
388 struct bgp_route_list add
;
391 bgp_cidr(ip
, mask
, &add
.dest
);
394 /* check for duplicate */
397 i
= memcmp(&r
->dest
, &add
.dest
, sizeof(r
->dest
));
399 return 1; /* already covered */
407 /* insert into route list; sorted */
408 if (!(r
= malloc(sizeof(*r
))))
410 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
411 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
, strerror(errno
));
416 memcpy(r
, &add
, sizeof(*r
));
417 bgp_routes
= bgp_insert_route(bgp_routes
, r
);
419 /* flag established peers for update */
420 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
421 if (bgp_peers
[i
].state
== Established
)
422 bgp_peers
[i
].update_routes
= 1;
424 LOG(4, 0, 0, "Registered BGP route %s/%d\n",
425 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
);
430 /* remove route from list for peers */
431 int bgp_del_route(in_addr_t ip
, in_addr_t mask
)
433 struct bgp_route_list
*r
= bgp_routes
;
434 struct bgp_route_list
*e
= 0;
435 struct bgp_route_list del
;
438 bgp_cidr(ip
, mask
, &del
.dest
);
441 /* find entry in routes list and remove */
444 i
= memcmp(&r
->dest
, &del
.dest
, sizeof(r
->dest
));
450 bgp_routes
= r
->next
;
468 /* flag established peers for update */
469 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
470 if (bgp_peers
[i
].state
== Established
)
471 bgp_peers
[i
].update_routes
= 1;
473 LOG(4, 0, 0, "Removed BGP route %s/%d\n",
474 fmtaddr(del
.dest
.prefix
, 0), del
.dest
.len
);
479 /* enable or disable routing */
480 void bgp_enable_routing(int enable
)
484 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
486 bgp_peers
[i
].routing
= enable
;
488 /* flag established peers for update */
489 if (bgp_peers
[i
].state
== Established
)
490 bgp_peers
[i
].update_routes
= 1;
493 LOG(4, 0, 0, "%s BGP routing\n", enable
? "Enabled" : "Suspended");
497 # include <sys/epoll.h>
499 # include "fake_epoll.h"
502 /* return a bitmask of the events required to poll this peer's fd */
510 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
512 struct bgp_peer
*peer
= &bgp_peers
[i
];
515 if (peer
->state
== Disabled
|| peer
->state
== Idle
)
518 if (peer
->inbuf
->done
< BGP_MAX_PACKET_SIZE
)
521 if (peer
->state
== Connect
|| /* connection in progress */
522 peer
->update_routes
|| /* routing updates */
523 peer
->outbuf
->packet
.header
.len
) /* pending output */
526 if (peer
->events
!= events
)
528 struct epoll_event ev
;
530 ev
.events
= peer
->events
= events
;
531 ev
.data
.ptr
= &peer
->edata
;
532 epoll_ctl(epollfd
, EPOLL_CTL_MOD
, peer
->sock
, &ev
);
539 /* process bgp events/timers */
540 int bgp_process(uint32_t events
[])
547 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
549 struct bgp_peer
*peer
= &bgp_peers
[i
];
551 if (*peer
->name
&& peer
->cli_flag
== BGP_CLI_RESTART
)
557 if (peer
->state
== Disabled
)
562 switch (peer
->cli_flag
)
564 case BGP_CLI_SUSPEND
:
568 if (peer
->state
== Established
)
569 peer
->update_routes
= 1;
578 if (peer
->state
== Established
)
579 peer
->update_routes
= 1;
588 /* handle empty/fill of buffers */
589 if (events
[i
] & EPOLLOUT
)
592 if (peer
->state
== Connect
)
593 r
= bgp_handle_connect(peer
);
594 else if (peer
->outbuf
->packet
.header
.len
)
601 if (events
[i
] & (EPOLLIN
|EPOLLHUP
))
607 /* process input buffer contents */
608 while (peer
->inbuf
->done
>= sizeof(peer
->inbuf
->packet
.header
)
609 && !peer
->outbuf
->packet
.header
.len
) /* may need to queue a response */
611 if (bgp_handle_input(peer
) < 0)
615 /* process pending updates */
616 if (peer
->update_routes
617 && !peer
->outbuf
->packet
.header
.len
) /* ditto */
619 if (!bgp_send_update(peer
))
624 if (peer
->state
== Established
)
626 if (time_now
> peer
->expire_time
)
628 LOG(1, 0, 0, "No message from BGP peer %s in %ds\n",
629 peer
->name
, peer
->hold
);
631 bgp_send_notification(peer
, BGP_ERR_HOLD_TIMER_EXP
, 0);
635 if (time_now
> peer
->keepalive_time
&& !peer
->outbuf
->packet
.header
.len
)
636 bgp_send_keepalive(peer
);
638 else if (peer
->state
== Idle
)
640 if (time_now
> peer
->retry_time
)
643 else if (time_now
> peer
->state_time
+ BGP_STATE_TIME
)
645 LOG(1, 0, 0, "%s timer expired for BGP peer %s\n",
646 bgp_state_str(peer
->state
), peer
->name
);
655 static void bgp_free_routes(struct bgp_route_list
*routes
)
657 struct bgp_route_list
*tmp
;
659 while ((tmp
= routes
))
666 char const *bgp_state_str(enum bgp_state state
)
670 case Disabled
: return "Disabled";
671 case Idle
: return "Idle";
672 case Connect
: return "Connect";
673 case Active
: return "Active";
674 case OpenSent
: return "OpenSent";
675 case OpenConfirm
: return "OpenConfirm";
676 case Established
: return "Established";
682 static char const *bgp_msg_type_str(uint8_t type
)
686 case BGP_MSG_OPEN
: return "OPEN";
687 case BGP_MSG_UPDATE
: return "UPDATE";
688 case BGP_MSG_NOTIFICATION
: return "NOTIFICATION";
689 case BGP_MSG_KEEPALIVE
: return "KEEPALIVE";
695 /* attempt to connect to peer */
696 static int bgp_connect(struct bgp_peer
*peer
)
698 static int bgp_port
= 0;
699 struct sockaddr_in addr
;
700 struct sockaddr_in source_addr
;
701 struct epoll_event ev
;
705 struct servent
*serv
;
706 if (!(serv
= getservbyname("bgp", "tcp")))
708 LOG(0, 0, 0, "Can't get bgp service (%s)\n", strerror(errno
));
712 bgp_port
= serv
->s_port
;
715 if ((peer
->sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
717 LOG(0, 0, 0, "Can't create a socket for BGP peer %s (%s)\n",
718 peer
->name
, strerror(errno
));
720 peer
->state
= peer
->next_state
= Disabled
;
724 /* add to poll set */
725 ev
.events
= peer
->events
= EPOLLOUT
;
726 ev
.data
.ptr
= &peer
->edata
;
727 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, peer
->sock
, &ev
);
729 /* set to non-blocking */
730 fcntl(peer
->sock
, F_SETFL
, fcntl(peer
->sock
, F_GETFL
, 0) | O_NONBLOCK
);
732 /* set source address */
733 memset(&source_addr
, 0, sizeof(source_addr
));
734 source_addr
.sin_family
= AF_INET
;
735 source_addr
.sin_addr
.s_addr
= peer
->source_addr
; /* defaults to INADDR_ANY */
736 if (bind(peer
->sock
, (struct sockaddr
*) &source_addr
, sizeof(source_addr
)) < 0)
738 LOG(1, 0, 0, "Can't set source address to %s: %s\n",
739 inet_ntoa(source_addr
.sin_addr
), strerror(errno
));
746 memset(&addr
, 0, sizeof(addr
));
747 addr
.sin_family
= AF_INET
;
748 addr
.sin_port
= bgp_port
;
749 addr
.sin_addr
.s_addr
= peer
->addr
;
751 while (connect(peer
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1)
753 if (errno
== EINTR
) /* SIGALARM handler */
756 if (errno
!= EINPROGRESS
)
758 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n",
759 inet_ntoa(addr
.sin_addr
), strerror(errno
));
765 peer
->state
= Connect
;
766 peer
->state_time
= time_now
;
768 LOG(4, 0, 0, "BGP peer %s: state Connect\n", peer
->name
);
772 peer
->state
= Active
;
773 peer
->state_time
= time_now
;
774 peer
->retry_time
= peer
->retry_count
= 0;
776 LOG(4, 0, 0, "BGP peer %s: state Active\n", inet_ntoa(addr
.sin_addr
));
778 return bgp_send_open(peer
);
781 /* complete partial connection (state = Connect) */
782 static int bgp_handle_connect(struct bgp_peer
*peer
)
785 socklen_t len
= sizeof(int);
786 getsockopt(peer
->sock
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
789 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer
->name
,
796 peer
->state
= Active
;
797 peer
->state_time
= time_now
;
799 LOG(4, 0, 0, "BGP peer %s: state Active\n", peer
->name
);
801 return bgp_send_open(peer
);
804 /* initiate a write */
805 static int bgp_write(struct bgp_peer
*peer
)
807 int len
= htons(peer
->outbuf
->packet
.header
.len
);
810 while ((r
= write(peer
->sock
, &peer
->outbuf
->packet
+ peer
->outbuf
->done
,
811 len
- peer
->outbuf
->done
)) == -1)
820 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
822 LOG(1, 0, 0, "Can't write to BGP peer %s (%s)\n", peer
->name
,
831 peer
->outbuf
->done
+= r
;
835 LOG(4, 0, 0, "Sent %s to BGP peer %s\n",
836 bgp_msg_type_str(peer
->outbuf
->packet
.header
.type
), peer
->name
);
838 peer
->outbuf
->packet
.header
.len
= 0;
839 peer
->outbuf
->done
= 0;
841 if (peer
->state
== Established
)
842 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
844 if (peer
->state
!= peer
->next_state
)
846 if (peer
->next_state
== Disabled
|| peer
->next_state
== Idle
)
852 peer
->state
= peer
->next_state
;
853 peer
->state_time
= time_now
;
855 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
856 bgp_state_str(peer
->state
));
862 /* initiate a read */
863 static int bgp_read(struct bgp_peer
*peer
)
867 while ((r
= read(peer
->sock
, &peer
->inbuf
->packet
+ peer
->inbuf
->done
,
868 BGP_MAX_PACKET_SIZE
- peer
->inbuf
->done
)) < 1)
872 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
882 LOG(1, 0, 0, "Can't read from BGP peer %s (%s)\n", peer
->name
,
890 peer
->inbuf
->done
+= r
;
894 /* process buffered packets */
895 static int bgp_handle_input(struct bgp_peer
*peer
)
897 struct bgp_packet
*p
= &peer
->inbuf
->packet
;
898 int len
= ntohs(p
->header
.len
);
900 if (len
> BGP_MAX_PACKET_SIZE
)
902 LOG(1, 0, 0, "Bad header length from BGP %s\n", peer
->name
);
903 bgp_send_notification(peer
, BGP_ERR_HEADER
, BGP_ERR_HDR_BAD_LEN
);
907 if (peer
->inbuf
->done
< len
)
910 LOG(4, 0, 0, "Received %s from BGP peer %s\n",
911 bgp_msg_type_str(p
->header
.type
), peer
->name
);
913 switch (p
->header
.type
)
917 struct bgp_data_open data
;
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 /* next transition requires an exchange of keepalives */
983 bgp_send_keepalive(peer
);
985 /* FIXME: may need to check for optional params */
990 case BGP_MSG_KEEPALIVE
:
991 if (peer
->state
== OpenConfirm
)
993 peer
->state
= peer
->next_state
= Established
;
994 peer
->state_time
= time_now
;
995 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
996 peer
->update_routes
= 1;
997 peer
->retry_count
= 0;
998 peer
->retry_time
= 0;
1000 LOG(4, 0, 0, "BGP peer %s: state Established\n", peer
->name
);
1005 case BGP_MSG_NOTIFICATION
:
1006 if (len
> sizeof(p
->header
))
1008 struct bgp_data_notification
*notification
=
1009 (struct bgp_data_notification
*) p
->data
;
1011 if (notification
->error_code
== BGP_ERR_CEASE
)
1013 LOG(4, 0, 0, "BGP peer %s sent CEASE\n", peer
->name
);
1018 /* FIXME: should handle more notifications */
1019 LOG(4, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n",
1020 peer
->name
, (int) notification
->error_code
);
1027 peer
->expire_time
= time_now
+ peer
->hold
;
1029 /* see if there's another message in the same packet/buffer */
1030 if (peer
->inbuf
->done
> len
)
1032 peer
->inbuf
->done
-= len
;
1033 memmove(p
, (char *) p
+ len
, peer
->inbuf
->done
);
1037 peer
->inbuf
->packet
.header
.len
= 0;
1038 peer
->inbuf
->done
= 0;
1041 return peer
->inbuf
->done
;
1044 /* send/buffer OPEN message */
1045 static int bgp_send_open(struct bgp_peer
*peer
)
1047 struct bgp_data_open data
;
1048 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1050 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1051 sizeof(peer
->outbuf
->packet
.header
.marker
));
1053 peer
->outbuf
->packet
.header
.type
= BGP_MSG_OPEN
;
1055 data
.version
= BGP_VERSION
;
1056 data
.as
= htons(our_as
);
1057 data
.hold_time
= htons(peer
->hold
);
1058 data
.identifier
= my_address
;
1061 memcpy(peer
->outbuf
->packet
.data
, &data
, BGP_DATA_OPEN_SIZE
);
1062 len
+= BGP_DATA_OPEN_SIZE
;
1064 peer
->outbuf
->packet
.header
.len
= htons(len
);
1065 peer
->outbuf
->done
= 0;
1066 peer
->next_state
= OpenSent
;
1068 return bgp_write(peer
);
1071 /* send/buffer KEEPALIVE message */
1072 static int bgp_send_keepalive(struct bgp_peer
*peer
)
1074 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1075 sizeof(peer
->outbuf
->packet
.header
.marker
));
1077 peer
->outbuf
->packet
.header
.type
= BGP_MSG_KEEPALIVE
;
1078 peer
->outbuf
->packet
.header
.len
=
1079 htons(sizeof(peer
->outbuf
->packet
.header
));
1081 peer
->outbuf
->done
= 0;
1082 peer
->next_state
= (peer
->state
== OpenSent
) ? OpenConfirm
: peer
->state
;
1084 return bgp_write(peer
);
1087 /* send/buffer UPDATE message */
1088 static int bgp_send_update(struct bgp_peer
*peer
)
1090 uint16_t unf_len
= 0;
1092 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1093 struct bgp_route_list
*have
= peer
->routes
;
1094 struct bgp_route_list
*want
= peer
->routing
? bgp_routes
: 0;
1095 struct bgp_route_list
*e
= 0;
1096 struct bgp_route_list
*add
= 0;
1099 char *data
= (char *) &peer
->outbuf
->packet
.data
;
1101 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1102 char *max
= (char *) &peer
->outbuf
->packet
.data
1103 + sizeof(peer
->outbuf
->packet
.data
)
1104 - sizeof(attr_len
) - peer
->path_attr_len
- sizeof(struct bgp_ip_prefix
);
1106 /* skip over unf_len */
1107 data
+= sizeof(unf_len
);
1108 len
+= sizeof(unf_len
);
1110 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1111 sizeof(peer
->outbuf
->packet
.header
.marker
));
1113 peer
->outbuf
->packet
.header
.type
= BGP_MSG_UPDATE
;
1115 peer
->update_routes
= 0; /* tentatively clear */
1117 /* find differences */
1118 while ((have
|| want
) && data
< (max
- sizeof(struct bgp_ip_prefix
)))
1122 ? memcmp(&have
->dest
, &want
->dest
, sizeof(have
->dest
))
1127 if (s
< 0) /* found one to delete */
1129 struct bgp_route_list
*tmp
= have
;
1132 s
= BGP_IP_PREFIX_SIZE(tmp
->dest
);
1133 memcpy(data
, &tmp
->dest
, s
);
1138 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1139 fmtaddr(tmp
->dest
.prefix
, 0), tmp
->dest
.len
, peer
->name
);
1146 peer
->routes
= have
;
1152 e
= have
; /* stash the last found to relink above */
1156 else if (s
> 0) /* addition reqd. */
1160 peer
->update_routes
= 1; /* only one add per packet */
1174 peer
->update_routes
= 1; /* more to do */
1176 /* anything changed? */
1177 if (!(unf_len
|| add
))
1180 /* go back and insert unf_len */
1181 unf_len
= htons(unf_len
);
1182 memcpy(&peer
->outbuf
->packet
.data
, &unf_len
, sizeof(unf_len
));
1186 if (!(e
= malloc(sizeof(*e
))))
1188 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1189 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, strerror(errno
));
1194 memcpy(e
, add
, sizeof(*e
));
1196 peer
->routes
= bgp_insert_route(peer
->routes
, e
);
1198 attr_len
= htons(peer
->path_attr_len
);
1199 memcpy(data
, &attr_len
, sizeof(attr_len
));
1200 data
+= sizeof(attr_len
);
1201 len
+= sizeof(attr_len
);
1203 memcpy(data
, peer
->path_attrs
, peer
->path_attr_len
);
1204 data
+= peer
->path_attr_len
;
1205 len
+= peer
->path_attr_len
;
1207 s
= BGP_IP_PREFIX_SIZE(add
->dest
);
1208 memcpy(data
, &add
->dest
, s
);
1212 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1213 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, peer
->name
);
1218 memcpy(data
, &attr_len
, sizeof(attr_len
));
1219 data
+= sizeof(attr_len
);
1220 len
+= sizeof(attr_len
);
1223 peer
->outbuf
->packet
.header
.len
= htons(len
);
1224 peer
->outbuf
->done
= 0;
1226 return bgp_write(peer
);
1229 /* send/buffer NOTIFICATION message */
1230 static int bgp_send_notification(struct bgp_peer
*peer
, uint8_t code
,
1233 struct bgp_data_notification data
;
1236 data
.error_code
= code
;
1237 len
+= sizeof(data
.error_code
);
1239 data
.error_subcode
= subcode
;
1240 len
+= sizeof(data
.error_code
);
1242 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1243 sizeof(peer
->outbuf
->packet
.header
.marker
));
1245 peer
->outbuf
->packet
.header
.type
= BGP_MSG_NOTIFICATION
;
1246 peer
->outbuf
->packet
.header
.len
=
1247 htons(sizeof(peer
->outbuf
->packet
.header
) + len
);
1249 memcpy(peer
->outbuf
->packet
.data
, &data
, len
);
1251 peer
->outbuf
->done
= 0;
1252 peer
->next_state
= code
== BGP_ERR_CEASE
? Disabled
: Idle
;
1254 /* we're dying; ignore any pending input */
1255 peer
->inbuf
->packet
.header
.len
= 0;
1256 peer
->inbuf
->done
= 0;
1258 return bgp_write(peer
);