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
));
197 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
198 a
.code
= BGP_PATH_ATTR_CODE_NEXT_HOP
;
199 ip
= my_address
; /* we're it */
200 a
.data
.s
.len
= sizeof(ip
);
201 memcpy(a
.data
.s
.value
, &ip
, sizeof(ip
));
205 /* MULTI_EXIT_DISC */
206 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
;
207 a
.code
= BGP_PATH_ATTR_CODE_MULTI_EXIT_DISC
;
208 a
.data
.s
.len
= sizeof(metric
);
209 memcpy(a
.data
.s
.value
, &metric
, sizeof(metric
));
215 uint32_t local_pref
= htonl(BGP_LOCAL_PREF
);
218 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
219 a
.code
= BGP_PATH_ATTR_CODE_LOCAL_PREF
;
220 a
.data
.s
.len
= sizeof(local_pref
);
221 memcpy(a
.data
.s
.value
, &local_pref
, sizeof(local_pref
));
227 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
| BGP_PATH_ATTR_FLAG_TRANS
;
228 a
.code
= BGP_PATH_ATTR_CODE_COMMUNITIES
;
229 a
.data
.s
.len
= sizeof(no_export
);
230 memcpy(a
.data
.s
.value
, &no_export
, sizeof(no_export
));
234 if (!(peer
->path_attrs
= malloc(peer
->path_attr_len
)))
236 LOG(0, 0, 0, "Can't allocate path_attrs for %s (%s)\n",
237 name
, strerror(errno
));
242 memcpy(peer
->path_attrs
, path_attrs
, peer
->path_attr_len
);
244 LOG(4, 0, 0, "Initiating BGP connection to %s (routing %s)\n",
245 name
, enable
? "enabled" : "suspended");
247 /* we have at least one peer configured */
251 return bgp_connect(peer
);
254 /* clear counters, timers, routes and buffers; close socket; move to
255 next_state, which may be Disabled or Idle */
256 static void bgp_clear(struct bgp_peer
*peer
)
258 if (peer
->sock
!= -1)
264 peer
->keepalive_time
= 0;
265 peer
->expire_time
= 0;
267 peer
->keepalive
= peer
->init_keepalive
;
268 peer
->hold
= peer
->init_hold
;
270 bgp_free_routes(peer
->routes
);
273 peer
->outbuf
->packet
.header
.len
= 0;
274 peer
->outbuf
->done
= 0;
275 peer
->inbuf
->packet
.header
.len
= 0;
276 peer
->inbuf
->done
= 0;
281 if (peer
->state
!= peer
->next_state
)
283 peer
->state
= peer
->next_state
;
284 peer
->state_time
= time_now
;
286 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
287 bgp_state_str(peer
->next_state
));
291 /* initiate a clean shutdown */
292 void bgp_stop(struct bgp_peer
*peer
)
294 LOG(4, 0, 0, "Terminating BGP connection to %s\n", peer
->name
);
295 bgp_send_notification(peer
, BGP_ERR_CEASE
, 0);
298 /* drop connection (if any) and set state to Disabled */
299 void bgp_halt(struct bgp_peer
*peer
)
301 LOG(4, 0, 0, "Aborting BGP connection to %s\n", peer
->name
);
302 peer
->next_state
= Disabled
;
306 /* drop connection (if any) and set to Idle for connection retry */
307 int bgp_restart(struct bgp_peer
*peer
)
309 peer
->next_state
= Idle
;
313 peer
->retry_time
= time_now
;
314 peer
->retry_count
= 0;
317 return bgp_connect(peer
);
320 static void bgp_set_retry(struct bgp_peer
*peer
)
322 if (peer
->retry_count
++ < BGP_MAX_RETRY
)
324 peer
->retry_time
= time_now
+ (BGP_RETRY_BACKOFF
* peer
->retry_count
);
325 peer
->next_state
= Idle
;
329 bgp_halt(peer
); /* give up */
332 /* convert ip/mask to CIDR notation */
333 static void bgp_cidr(in_addr_t ip
, in_addr_t mask
, struct bgp_ip_prefix
*pfx
)
338 /* convert to prefix notation */
342 if (!mask
) /* bogus */
345 for (i
= 0; i
< 32 && ((b
= ntohl(1 << i
)), !(mask
& b
)); i
++)
352 /* insert route into list; sorted */
353 static struct bgp_route_list
*bgp_insert_route(struct bgp_route_list
*head
,
354 struct bgp_route_list
*new)
356 struct bgp_route_list
*p
= head
;
357 struct bgp_route_list
*e
= 0;
359 while (p
&& memcmp(&p
->dest
, &new->dest
, sizeof(p
->dest
)) < 0)
379 /* add route to list for peers */
381 * Note: this doesn't do route aggregation, nor drop routes if a less
382 * specific match already exists (partly because I'm lazy, but also so
383 * that if that route is later deleted we don't have to be concerned
384 * about adding back the more specific one).
386 int bgp_add_route(in_addr_t ip
, in_addr_t mask
)
388 struct bgp_route_list
*r
= bgp_routes
;
389 struct bgp_route_list add
;
392 bgp_cidr(ip
, mask
, &add
.dest
);
395 /* check for duplicate */
398 i
= memcmp(&r
->dest
, &add
.dest
, sizeof(r
->dest
));
400 return 1; /* already covered */
408 /* insert into route list; sorted */
409 if (!(r
= malloc(sizeof(*r
))))
411 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
412 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
, strerror(errno
));
417 memcpy(r
, &add
, sizeof(*r
));
418 bgp_routes
= bgp_insert_route(bgp_routes
, r
);
420 /* flag established peers for update */
421 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
422 if (bgp_peers
[i
].state
== Established
)
423 bgp_peers
[i
].update_routes
= 1;
425 LOG(4, 0, 0, "Registered BGP route %s/%d\n",
426 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
);
431 /* remove route from list for peers */
432 int bgp_del_route(in_addr_t ip
, in_addr_t mask
)
434 struct bgp_route_list
*r
= bgp_routes
;
435 struct bgp_route_list
*e
= 0;
436 struct bgp_route_list del
;
439 bgp_cidr(ip
, mask
, &del
.dest
);
442 /* find entry in routes list and remove */
445 i
= memcmp(&r
->dest
, &del
.dest
, sizeof(r
->dest
));
451 bgp_routes
= r
->next
;
469 /* flag established peers for update */
470 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
471 if (bgp_peers
[i
].state
== Established
)
472 bgp_peers
[i
].update_routes
= 1;
474 LOG(4, 0, 0, "Removed BGP route %s/%d\n",
475 fmtaddr(del
.dest
.prefix
, 0), del
.dest
.len
);
480 /* enable or disable routing */
481 void bgp_enable_routing(int enable
)
485 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
487 bgp_peers
[i
].routing
= enable
;
489 /* flag established peers for update */
490 if (bgp_peers
[i
].state
== Established
)
491 bgp_peers
[i
].update_routes
= 1;
494 LOG(4, 0, 0, "%s BGP routing\n", enable
? "Enabled" : "Suspended");
498 # include <sys/epoll.h>
500 # include "fake_epoll.h"
503 /* return a bitmask of the events required to poll this peer's fd */
511 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
513 struct bgp_peer
*peer
= &bgp_peers
[i
];
516 if (peer
->state
== Disabled
|| peer
->state
== Idle
)
519 if (peer
->inbuf
->done
< BGP_MAX_PACKET_SIZE
)
522 if (peer
->state
== Connect
|| /* connection in progress */
523 peer
->update_routes
|| /* routing updates */
524 peer
->outbuf
->packet
.header
.len
) /* pending output */
527 if (peer
->events
!= events
)
529 struct epoll_event ev
;
531 ev
.events
= peer
->events
= events
;
532 ev
.data
.ptr
= &peer
->edata
;
533 epoll_ctl(epollfd
, EPOLL_CTL_MOD
, peer
->sock
, &ev
);
540 /* process bgp events/timers */
541 int bgp_process(uint32_t events
[])
548 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
550 struct bgp_peer
*peer
= &bgp_peers
[i
];
552 if (*peer
->name
&& peer
->cli_flag
== BGP_CLI_RESTART
)
558 if (peer
->state
== Disabled
)
563 switch (peer
->cli_flag
)
565 case BGP_CLI_SUSPEND
:
569 if (peer
->state
== Established
)
570 peer
->update_routes
= 1;
579 if (peer
->state
== Established
)
580 peer
->update_routes
= 1;
589 /* handle empty/fill of buffers */
590 if (events
[i
] & EPOLLOUT
)
593 if (peer
->state
== Connect
)
594 r
= bgp_handle_connect(peer
);
595 else if (peer
->outbuf
->packet
.header
.len
)
602 if (events
[i
] & (EPOLLIN
|EPOLLHUP
))
608 /* process input buffer contents */
609 while (peer
->inbuf
->done
>= sizeof(peer
->inbuf
->packet
.header
)
610 && !peer
->outbuf
->packet
.header
.len
) /* may need to queue a response */
612 if (bgp_handle_input(peer
) < 0)
616 /* process pending updates */
617 if (peer
->update_routes
618 && !peer
->outbuf
->packet
.header
.len
) /* ditto */
620 if (!bgp_send_update(peer
))
625 if (peer
->state
== Established
)
627 if (time_now
> peer
->expire_time
)
629 LOG(1, 0, 0, "No message from BGP peer %s in %ds\n",
630 peer
->name
, peer
->hold
);
632 bgp_send_notification(peer
, BGP_ERR_HOLD_TIMER_EXP
, 0);
636 if (time_now
> peer
->keepalive_time
&& !peer
->outbuf
->packet
.header
.len
)
637 bgp_send_keepalive(peer
);
639 else if (peer
->state
== Idle
)
641 if (time_now
> peer
->retry_time
)
644 else if (time_now
> peer
->state_time
+ BGP_STATE_TIME
)
646 LOG(1, 0, 0, "%s timer expired for BGP peer %s\n",
647 bgp_state_str(peer
->state
), peer
->name
);
656 static void bgp_free_routes(struct bgp_route_list
*routes
)
658 struct bgp_route_list
*tmp
;
660 while ((tmp
= routes
))
667 char const *bgp_state_str(enum bgp_state state
)
671 case Disabled
: return "Disabled";
672 case Idle
: return "Idle";
673 case Connect
: return "Connect";
674 case Active
: return "Active";
675 case OpenSent
: return "OpenSent";
676 case OpenConfirm
: return "OpenConfirm";
677 case Established
: return "Established";
683 static char const *bgp_msg_type_str(uint8_t type
)
687 case BGP_MSG_OPEN
: return "OPEN";
688 case BGP_MSG_UPDATE
: return "UPDATE";
689 case BGP_MSG_NOTIFICATION
: return "NOTIFICATION";
690 case BGP_MSG_KEEPALIVE
: return "KEEPALIVE";
696 /* attempt to connect to peer */
697 static int bgp_connect(struct bgp_peer
*peer
)
699 static int bgp_port
= 0;
700 struct sockaddr_in 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
);
733 memset(&addr
, 0, sizeof(addr
));
734 addr
.sin_family
= AF_INET
;
735 addr
.sin_port
= bgp_port
;
736 addr
.sin_addr
.s_addr
= peer
->addr
;
738 while (connect(peer
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1)
740 if (errno
== EINTR
) /* SIGALARM handler */
743 if (errno
!= EINPROGRESS
)
745 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n",
746 inet_ntoa(addr
.sin_addr
), strerror(errno
));
752 peer
->state
= Connect
;
753 peer
->state_time
= time_now
;
755 LOG(4, 0, 0, "BGP peer %s: state Connect\n", peer
->name
);
759 peer
->state
= Active
;
760 peer
->state_time
= time_now
;
761 peer
->retry_time
= peer
->retry_count
= 0;
763 LOG(4, 0, 0, "BGP peer %s: state Active\n", inet_ntoa(addr
.sin_addr
));
765 peer
->handle_ipv6_routes
= 0;
767 return bgp_send_open(peer
);
770 /* complete partial connection (state = Connect) */
771 static int bgp_handle_connect(struct bgp_peer
*peer
)
774 socklen_t len
= sizeof(int);
775 getsockopt(peer
->sock
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
778 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer
->name
,
785 peer
->state
= Active
;
786 peer
->state_time
= time_now
;
788 LOG(4, 0, 0, "BGP peer %s: state Active\n", peer
->name
);
790 peer
->handle_ipv6_routes
= 0;
792 return bgp_send_open(peer
);
795 /* initiate a write */
796 static int bgp_write(struct bgp_peer
*peer
)
798 int len
= htons(peer
->outbuf
->packet
.header
.len
);
801 while ((r
= write(peer
->sock
, &peer
->outbuf
->packet
+ peer
->outbuf
->done
,
802 len
- peer
->outbuf
->done
)) == -1)
811 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
813 LOG(1, 0, 0, "Can't write to BGP peer %s (%s)\n", peer
->name
,
822 peer
->outbuf
->done
+= r
;
826 LOG(4, 0, 0, "Sent %s to BGP peer %s\n",
827 bgp_msg_type_str(peer
->outbuf
->packet
.header
.type
), peer
->name
);
829 peer
->outbuf
->packet
.header
.len
= 0;
830 peer
->outbuf
->done
= 0;
832 if (peer
->state
== Established
)
833 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
835 if (peer
->state
!= peer
->next_state
)
837 if (peer
->next_state
== Disabled
|| peer
->next_state
== Idle
)
843 peer
->state
= peer
->next_state
;
844 peer
->state_time
= time_now
;
846 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
847 bgp_state_str(peer
->state
));
853 /* initiate a read */
854 static int bgp_read(struct bgp_peer
*peer
)
858 while ((r
= read(peer
->sock
, &peer
->inbuf
->packet
+ peer
->inbuf
->done
,
859 BGP_MAX_PACKET_SIZE
- peer
->inbuf
->done
)) < 1)
863 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
873 LOG(1, 0, 0, "Can't read from BGP peer %s (%s)\n", peer
->name
,
881 peer
->inbuf
->done
+= r
;
885 /* process buffered packets */
886 static int bgp_handle_input(struct bgp_peer
*peer
)
888 struct bgp_packet
*p
= &peer
->inbuf
->packet
;
889 int len
= ntohs(p
->header
.len
);
891 if (len
> BGP_MAX_PACKET_SIZE
)
893 LOG(1, 0, 0, "Bad header length from BGP %s\n", peer
->name
);
894 bgp_send_notification(peer
, BGP_ERR_HEADER
, BGP_ERR_HDR_BAD_LEN
);
898 if (peer
->inbuf
->done
< len
)
901 LOG(4, 0, 0, "Received %s from BGP peer %s\n",
902 bgp_msg_type_str(p
->header
.type
), peer
->name
);
904 switch (p
->header
.type
)
908 struct bgp_data_open data
;
911 off_t param_offset
, capability_offset
;
912 struct bgp_opt_param
*param
;
913 uint8_t capabilities_len
;
914 char *capabilities
= NULL
;
915 struct bgp_capability
*capability
;
916 struct bgp_mp_cap_param
*mp_cap
;
918 for (i
= 0; i
< sizeof(p
->header
.marker
); i
++)
920 if ((unsigned char) p
->header
.marker
[i
] != 0xff)
922 LOG(1, 0, 0, "Invalid marker from BGP peer %s\n",
925 bgp_send_notification(peer
, BGP_ERR_HEADER
,
926 BGP_ERR_HDR_NOT_SYNC
);
932 if (peer
->state
!= OpenSent
)
934 LOG(1, 0, 0, "OPEN from BGP peer %s in %s state\n",
935 peer
->name
, bgp_state_str(peer
->state
));
937 bgp_send_notification(peer
, BGP_ERR_FSM
, 0);
941 memcpy(&data
, p
->data
, len
- sizeof(p
->header
));
943 if (data
.version
!= BGP_VERSION
)
945 LOG(1, 0, 0, "Bad version (%d) sent by BGP peer %s\n",
946 (int) data
.version
, peer
->name
);
948 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_VERSION
);
952 if (ntohs(data
.as
) != peer
->as
)
954 LOG(1, 0, 0, "Bad AS sent by BGP peer %s (got %d, "
955 "expected %d)\n", peer
->name
, (int) htons(data
.as
),
958 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_BAD_AS
);
962 if ((hold
= ntohs(data
.hold_time
)) < 3)
964 LOG(1, 0, 0, "Bad hold time (%d) from BGP peer %s\n",
967 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_HOLD_TIME
);
971 /* pick lowest hold time */
972 if (hold
< peer
->hold
)
975 /* adjust our keepalive based on negotiated hold value */
976 if (peer
->keepalive
* 3 > peer
->hold
)
977 peer
->keepalive
= peer
->hold
/ 3;
979 /* check for optional parameters */
980 /* 2 is for the size of type + len (both uint8_t) */
981 for (param_offset
= 0;
982 param_offset
< data
.opt_len
;
983 param_offset
+= 2 + param
->len
)
985 param
= (struct bgp_opt_param
*)(&data
.opt_params
+ param_offset
);
988 if (data
.opt_len
- param_offset
< 2
989 || param
->len
> data
.opt_len
- param_offset
- 2)
991 LOG(1, 0, 0, "Malformed Optional Parameter list from BGP peer %s\n",
994 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_UNSPEC
);
998 /* we know only one parameter type */
999 if (param
->type
!= BGP_CAPABILITY_PARAM_TYPE
)
1001 LOG(1, 0, 0, "Unsupported Optional Parameter type %d from BGP peer %s\n",
1002 param
->type
, peer
->name
);
1004 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_UNSUP_PARAM
);
1008 capabilities_len
= param
->len
;
1009 capabilities
= (char *)¶m
->value
;
1012 /* look for BGP multiprotocol capability */
1015 for (capability_offset
= 0;
1016 capability_offset
< capabilities_len
;
1017 capability_offset
+= 2 + capability
->len
)
1019 capability
= (struct bgp_capability
*)(capabilities
+ capability_offset
);
1021 /* sensible check */
1022 if (capabilities_len
- capability_offset
< 2
1023 || capability
->len
> capabilities_len
- capability_offset
- 2)
1025 LOG(1, 0, 0, "Malformed Capabilities list from BGP peer %s\n",
1028 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_UNSPEC
);
1032 /* we only know one capability code */
1033 if (capability
->code
!= BGP_CAP_CODE_MP
1034 && capability
->len
!= sizeof(struct bgp_mp_cap_param
))
1036 LOG(4, 0, 0, "Unsupported Capability code %d from BGP peer %s\n",
1037 capability
->code
, peer
->name
);
1039 bgp_send_notification_full(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_UNSUP_CAP
,
1040 (char *)capability
, 2 + capability
->len
);
1041 /* we don't terminate, still; we just jump to the next one */
1045 mp_cap
= (struct bgp_mp_cap_param
*)&capability
->value
;
1046 /* the only <AFI, SAFI> tuple we support */
1047 if (mp_cap
->afi
!= AF_INET6
&& mp_cap
->safi
!= BGP_MP_SAFI_UNICAST
)
1049 LOG(4, 0, 0, "Unsupported multiprotocol AFI %d and SAFI %d from BGP peer %s\n",
1050 mp_cap
->afi
, mp_cap
->safi
, peer
->name
);
1052 bgp_send_notification_full(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_UNSUP_CAP
,
1053 (char *)capability
, 2 + capability
->len
);
1054 /* we don't terminate, still; we just jump to the next one */
1058 peer
->handle_ipv6_routes
= 1;
1062 /* next transition requires an exchange of keepalives */
1063 bgp_send_keepalive(peer
);
1068 case BGP_MSG_KEEPALIVE
:
1069 if (peer
->state
== OpenConfirm
)
1071 peer
->state
= peer
->next_state
= Established
;
1072 peer
->state_time
= time_now
;
1073 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
1074 peer
->update_routes
= 1;
1075 peer
->retry_count
= 0;
1076 peer
->retry_time
= 0;
1078 LOG(4, 0, 0, "BGP peer %s: state Established\n", peer
->name
);
1083 case BGP_MSG_NOTIFICATION
:
1084 if (len
> sizeof(p
->header
))
1086 struct bgp_data_notification
*notification
=
1087 (struct bgp_data_notification
*) p
->data
;
1089 if (notification
->error_code
== BGP_ERR_CEASE
)
1091 LOG(4, 0, 0, "BGP peer %s sent CEASE\n", peer
->name
);
1096 /* FIXME: should handle more notifications */
1097 LOG(4, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n",
1098 peer
->name
, (int) notification
->error_code
);
1105 peer
->expire_time
= time_now
+ peer
->hold
;
1107 /* see if there's another message in the same packet/buffer */
1108 if (peer
->inbuf
->done
> len
)
1110 peer
->inbuf
->done
-= len
;
1111 memmove(p
, (char *) p
+ len
, peer
->inbuf
->done
);
1115 peer
->inbuf
->packet
.header
.len
= 0;
1116 peer
->inbuf
->done
= 0;
1119 return peer
->inbuf
->done
;
1122 /* send/buffer OPEN message */
1123 static int bgp_send_open(struct bgp_peer
*peer
)
1125 struct bgp_data_open data
;
1126 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1128 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1129 sizeof(peer
->outbuf
->packet
.header
.marker
));
1131 peer
->outbuf
->packet
.header
.type
= BGP_MSG_OPEN
;
1133 data
.version
= BGP_VERSION
;
1134 data
.as
= htons(our_as
);
1135 data
.hold_time
= htons(peer
->hold
);
1136 data
.identifier
= my_address
;
1139 memcpy(peer
->outbuf
->packet
.data
, &data
, BGP_DATA_OPEN_SIZE
);
1140 len
+= BGP_DATA_OPEN_SIZE
;
1142 peer
->outbuf
->packet
.header
.len
= htons(len
);
1143 peer
->outbuf
->done
= 0;
1144 peer
->next_state
= OpenSent
;
1146 return bgp_write(peer
);
1149 /* send/buffer KEEPALIVE message */
1150 static int bgp_send_keepalive(struct bgp_peer
*peer
)
1152 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1153 sizeof(peer
->outbuf
->packet
.header
.marker
));
1155 peer
->outbuf
->packet
.header
.type
= BGP_MSG_KEEPALIVE
;
1156 peer
->outbuf
->packet
.header
.len
=
1157 htons(sizeof(peer
->outbuf
->packet
.header
));
1159 peer
->outbuf
->done
= 0;
1160 peer
->next_state
= (peer
->state
== OpenSent
) ? OpenConfirm
: peer
->state
;
1162 return bgp_write(peer
);
1165 /* send/buffer UPDATE message */
1166 static int bgp_send_update(struct bgp_peer
*peer
)
1168 uint16_t unf_len
= 0;
1170 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1171 struct bgp_route_list
*have
= peer
->routes
;
1172 struct bgp_route_list
*want
= peer
->routing
? bgp_routes
: 0;
1173 struct bgp_route_list
*e
= 0;
1174 struct bgp_route_list
*add
= 0;
1177 char *data
= (char *) &peer
->outbuf
->packet
.data
;
1179 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1180 char *max
= (char *) &peer
->outbuf
->packet
.data
1181 + sizeof(peer
->outbuf
->packet
.data
)
1182 - sizeof(attr_len
) - peer
->path_attr_len
- sizeof(struct bgp_ip_prefix
);
1184 /* skip over unf_len */
1185 data
+= sizeof(unf_len
);
1186 len
+= sizeof(unf_len
);
1188 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1189 sizeof(peer
->outbuf
->packet
.header
.marker
));
1191 peer
->outbuf
->packet
.header
.type
= BGP_MSG_UPDATE
;
1193 peer
->update_routes
= 0; /* tentatively clear */
1195 /* find differences */
1196 while ((have
|| want
) && data
< (max
- sizeof(struct bgp_ip_prefix
)))
1200 ? memcmp(&have
->dest
, &want
->dest
, sizeof(have
->dest
))
1205 if (s
< 0) /* found one to delete */
1207 struct bgp_route_list
*tmp
= have
;
1210 s
= BGP_IP_PREFIX_SIZE(tmp
->dest
);
1211 memcpy(data
, &tmp
->dest
, s
);
1216 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1217 fmtaddr(tmp
->dest
.prefix
, 0), tmp
->dest
.len
, peer
->name
);
1224 peer
->routes
= have
;
1230 e
= have
; /* stash the last found to relink above */
1234 else if (s
> 0) /* addition reqd. */
1238 peer
->update_routes
= 1; /* only one add per packet */
1252 peer
->update_routes
= 1; /* more to do */
1254 /* anything changed? */
1255 if (!(unf_len
|| add
))
1258 /* go back and insert unf_len */
1259 unf_len
= htons(unf_len
);
1260 memcpy(&peer
->outbuf
->packet
.data
, &unf_len
, sizeof(unf_len
));
1264 if (!(e
= malloc(sizeof(*e
))))
1266 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1267 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, strerror(errno
));
1272 memcpy(e
, add
, sizeof(*e
));
1274 peer
->routes
= bgp_insert_route(peer
->routes
, e
);
1276 attr_len
= htons(peer
->path_attr_len
);
1277 memcpy(data
, &attr_len
, sizeof(attr_len
));
1278 data
+= sizeof(attr_len
);
1279 len
+= sizeof(attr_len
);
1281 memcpy(data
, peer
->path_attrs
, peer
->path_attr_len
);
1282 data
+= peer
->path_attr_len
;
1283 len
+= peer
->path_attr_len
;
1285 s
= BGP_IP_PREFIX_SIZE(add
->dest
);
1286 memcpy(data
, &add
->dest
, s
);
1290 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1291 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, peer
->name
);
1296 memcpy(data
, &attr_len
, sizeof(attr_len
));
1297 data
+= sizeof(attr_len
);
1298 len
+= sizeof(attr_len
);
1301 peer
->outbuf
->packet
.header
.len
= htons(len
);
1302 peer
->outbuf
->done
= 0;
1304 return bgp_write(peer
);
1307 /* send/buffer NOTIFICATION message */
1308 static int bgp_send_notification(struct bgp_peer
*peer
, uint8_t code
,
1311 return bgp_send_notification_full(peer
, code
, subcode
, NULL
, 0);
1314 static int bgp_send_notification_full(struct bgp_peer
*peer
, uint8_t code
,
1315 uint8_t subcode
, char *notification_data
, uint16_t data_len
)
1317 struct bgp_data_notification data
;
1320 data
.error_code
= code
;
1321 len
+= sizeof(data
.error_code
);
1323 data
.error_subcode
= subcode
;
1324 len
+= sizeof(data
.error_code
);
1326 memcpy(data
.data
, notification_data
, data_len
);
1329 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1330 sizeof(peer
->outbuf
->packet
.header
.marker
));
1332 peer
->outbuf
->packet
.header
.type
= BGP_MSG_NOTIFICATION
;
1333 peer
->outbuf
->packet
.header
.len
=
1334 htons(sizeof(peer
->outbuf
->packet
.header
) + len
);
1336 memcpy(peer
->outbuf
->packet
.data
, &data
, len
);
1338 peer
->outbuf
->done
= 0;
1339 peer
->next_state
= code
== BGP_ERR_CEASE
? Disabled
: Idle
;
1341 /* we're dying; ignore any pending input */
1342 peer
->inbuf
->packet
.header
.len
= 0;
1343 peer
->inbuf
->done
= 0;
1345 return bgp_write(peer
);