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
,
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
->as
= as
> 0 ? as
: our_as
;
128 ibgp
= peer
->as
== our_as
;
130 /* set initial timer values */
131 peer
->init_keepalive
= keepalive
== -1 ? BGP_KEEPALIVE_TIME
: keepalive
;
132 peer
->init_hold
= hold
== -1 ? BGP_HOLD_TIME
: hold
;
134 if (peer
->init_hold
< 3)
137 if (peer
->init_keepalive
* 3 > peer
->init_hold
)
138 peer
->init_keepalive
= peer
->init_hold
/ 3;
140 /* clear buffers, go to Idle state */
141 peer
->next_state
= Idle
;
144 /* set initial routing state */
145 peer
->routing
= enable
;
147 /* all our routes use the same attributes, so prepare it in advance */
148 if (peer
->path_attrs
)
149 free(peer
->path_attrs
);
151 peer
->path_attr_len
= 0;
154 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
155 a
.code
= BGP_PATH_ATTR_CODE_ORIGIN
;
157 a
.data
.s
.value
[0] = BGP_PATH_ATTR_CODE_ORIGIN_IGP
;
159 #define ADD_ATTRIBUTE() do { \
160 i = BGP_PATH_ATTR_SIZE(a); \
163 peer->path_attr_len += i; } while (0)
168 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
169 a
.code
= BGP_PATH_ATTR_CODE_AS_PATH
;
183 BGP_PATH_ATTR_CODE_AS_PATH_AS_SEQUENCE
,
188 a
.data
.s
.len
= sizeof(as_path
);
189 memcpy(&a
.data
.s
.value
, &as_path
, sizeof(as_path
));
195 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
196 a
.code
= BGP_PATH_ATTR_CODE_NEXT_HOP
;
197 ip
= my_address
; /* we're it */
198 a
.data
.s
.len
= sizeof(ip
);
199 memcpy(a
.data
.s
.value
, &ip
, sizeof(ip
));
203 /* MULTI_EXIT_DISC */
204 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
;
205 a
.code
= BGP_PATH_ATTR_CODE_MULTI_EXIT_DISC
;
206 a
.data
.s
.len
= sizeof(metric
);
207 memcpy(a
.data
.s
.value
, &metric
, sizeof(metric
));
213 uint32_t local_pref
= htonl(BGP_LOCAL_PREF
);
216 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
217 a
.code
= BGP_PATH_ATTR_CODE_LOCAL_PREF
;
218 a
.data
.s
.len
= sizeof(local_pref
);
219 memcpy(a
.data
.s
.value
, &local_pref
, sizeof(local_pref
));
225 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
| BGP_PATH_ATTR_FLAG_TRANS
;
226 a
.code
= BGP_PATH_ATTR_CODE_COMMUNITIES
;
227 a
.data
.s
.len
= sizeof(no_export
);
228 memcpy(a
.data
.s
.value
, &no_export
, sizeof(no_export
));
232 if (!(peer
->path_attrs
= malloc(peer
->path_attr_len
)))
234 LOG(0, 0, 0, "Can't allocate path_attrs for %s (%s)\n",
235 name
, strerror(errno
));
240 memcpy(peer
->path_attrs
, path_attrs
, peer
->path_attr_len
);
242 LOG(4, 0, 0, "Initiating BGP connection to %s (routing %s)\n",
243 name
, enable
? "enabled" : "suspended");
245 /* we have at least one peer configured */
249 return bgp_connect(peer
);
252 /* clear counters, timers, routes and buffers; close socket; move to
253 next_state, which may be Disabled or Idle */
254 static void bgp_clear(struct bgp_peer
*peer
)
256 if (peer
->sock
!= -1)
262 peer
->keepalive_time
= 0;
263 peer
->expire_time
= 0;
265 peer
->keepalive
= peer
->init_keepalive
;
266 peer
->hold
= peer
->init_hold
;
268 bgp_free_routes(peer
->routes
);
271 peer
->outbuf
->packet
.header
.len
= 0;
272 peer
->outbuf
->done
= 0;
273 peer
->inbuf
->packet
.header
.len
= 0;
274 peer
->inbuf
->done
= 0;
279 if (peer
->state
!= peer
->next_state
)
281 peer
->state
= peer
->next_state
;
282 peer
->state_time
= time_now
;
284 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
285 bgp_state_str(peer
->next_state
));
289 /* initiate a clean shutdown */
290 void bgp_stop(struct bgp_peer
*peer
)
292 LOG(4, 0, 0, "Terminating BGP connection to %s\n", peer
->name
);
293 bgp_send_notification(peer
, BGP_ERR_CEASE
, 0);
296 /* drop connection (if any) and set state to Disabled */
297 void bgp_halt(struct bgp_peer
*peer
)
299 LOG(4, 0, 0, "Aborting BGP connection to %s\n", peer
->name
);
300 peer
->next_state
= Disabled
;
304 /* drop connection (if any) and set to Idle for connection retry */
305 int bgp_restart(struct bgp_peer
*peer
)
307 peer
->next_state
= Idle
;
311 peer
->retry_time
= time_now
;
312 peer
->retry_count
= 0;
315 return bgp_connect(peer
);
318 static void bgp_set_retry(struct bgp_peer
*peer
)
320 if (peer
->retry_count
++ < BGP_MAX_RETRY
)
322 peer
->retry_time
= time_now
+ (BGP_RETRY_BACKOFF
* peer
->retry_count
);
323 peer
->next_state
= Idle
;
327 bgp_halt(peer
); /* give up */
330 /* convert ip/mask to CIDR notation */
331 static void bgp_cidr(in_addr_t ip
, in_addr_t mask
, struct bgp_ip_prefix
*pfx
)
336 /* convert to prefix notation */
340 if (!mask
) /* bogus */
343 for (i
= 0; i
< 32 && ((b
= ntohl(1 << i
)), !(mask
& b
)); i
++)
350 /* insert route into list; sorted */
351 static struct bgp_route_list
*bgp_insert_route(struct bgp_route_list
*head
,
352 struct bgp_route_list
*new)
354 struct bgp_route_list
*p
= head
;
355 struct bgp_route_list
*e
= 0;
357 while (p
&& memcmp(&p
->dest
, &new->dest
, sizeof(p
->dest
)) < 0)
377 /* add route to list for peers */
379 * Note: this doesn't do route aggregation, nor drop routes if a less
380 * specific match already exists (partly because I'm lazy, but also so
381 * that if that route is later deleted we don't have to be concerned
382 * about adding back the more specific one).
384 int bgp_add_route(in_addr_t ip
, in_addr_t mask
)
386 struct bgp_route_list
*r
= bgp_routes
;
387 struct bgp_route_list add
;
390 bgp_cidr(ip
, mask
, &add
.dest
);
393 /* check for duplicate */
396 i
= memcmp(&r
->dest
, &add
.dest
, sizeof(r
->dest
));
398 return 1; /* already covered */
406 /* insert into route list; sorted */
407 if (!(r
= malloc(sizeof(*r
))))
409 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
410 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
, strerror(errno
));
415 memcpy(r
, &add
, sizeof(*r
));
416 bgp_routes
= bgp_insert_route(bgp_routes
, r
);
418 /* flag established peers for update */
419 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
420 if (bgp_peers
[i
].state
== Established
)
421 bgp_peers
[i
].update_routes
= 1;
423 LOG(4, 0, 0, "Registered BGP route %s/%d\n",
424 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
);
429 /* remove route from list for peers */
430 int bgp_del_route(in_addr_t ip
, in_addr_t mask
)
432 struct bgp_route_list
*r
= bgp_routes
;
433 struct bgp_route_list
*e
= 0;
434 struct bgp_route_list del
;
437 bgp_cidr(ip
, mask
, &del
.dest
);
440 /* find entry in routes list and remove */
443 i
= memcmp(&r
->dest
, &del
.dest
, sizeof(r
->dest
));
449 bgp_routes
= r
->next
;
467 /* flag established peers for update */
468 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
469 if (bgp_peers
[i
].state
== Established
)
470 bgp_peers
[i
].update_routes
= 1;
472 LOG(4, 0, 0, "Removed BGP route %s/%d\n",
473 fmtaddr(del
.dest
.prefix
, 0), del
.dest
.len
);
478 /* enable or disable routing */
479 void bgp_enable_routing(int enable
)
483 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
485 bgp_peers
[i
].routing
= enable
;
487 /* flag established peers for update */
488 if (bgp_peers
[i
].state
== Established
)
489 bgp_peers
[i
].update_routes
= 1;
492 LOG(4, 0, 0, "%s BGP routing\n", enable
? "Enabled" : "Suspended");
496 # include <sys/epoll.h>
498 # include "fake_epoll.h"
501 /* return a bitmask of the events required to poll this peer's fd */
509 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
511 struct bgp_peer
*peer
= &bgp_peers
[i
];
514 if (peer
->state
== Disabled
|| peer
->state
== Idle
)
517 if (peer
->inbuf
->done
< BGP_MAX_PACKET_SIZE
)
520 if (peer
->state
== Connect
|| /* connection in progress */
521 peer
->update_routes
|| /* routing updates */
522 peer
->outbuf
->packet
.header
.len
) /* pending output */
525 if (peer
->events
!= events
)
527 struct epoll_event ev
;
529 ev
.events
= peer
->events
= events
;
530 ev
.data
.ptr
= &peer
->edata
;
531 epoll_ctl(epollfd
, EPOLL_CTL_MOD
, peer
->sock
, &ev
);
538 /* process bgp events/timers */
539 int bgp_process(uint32_t events
[])
546 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
548 struct bgp_peer
*peer
= &bgp_peers
[i
];
550 if (*peer
->name
&& peer
->cli_flag
== BGP_CLI_RESTART
)
556 if (peer
->state
== Disabled
)
561 switch (peer
->cli_flag
)
563 case BGP_CLI_SUSPEND
:
567 if (peer
->state
== Established
)
568 peer
->update_routes
= 1;
577 if (peer
->state
== Established
)
578 peer
->update_routes
= 1;
587 /* handle empty/fill of buffers */
588 if (events
[i
] & EPOLLOUT
)
591 if (peer
->state
== Connect
)
592 r
= bgp_handle_connect(peer
);
593 else if (peer
->outbuf
->packet
.header
.len
)
600 if (events
[i
] & (EPOLLIN
|EPOLLHUP
))
606 /* process input buffer contents */
607 while (peer
->inbuf
->done
>= sizeof(peer
->inbuf
->packet
.header
)
608 && !peer
->outbuf
->packet
.header
.len
) /* may need to queue a response */
610 if (bgp_handle_input(peer
) < 0)
614 /* process pending updates */
615 if (peer
->update_routes
616 && !peer
->outbuf
->packet
.header
.len
) /* ditto */
618 if (!bgp_send_update(peer
))
623 if (peer
->state
== Established
)
625 if (time_now
> peer
->expire_time
)
627 LOG(1, 0, 0, "No message from BGP peer %s in %ds\n",
628 peer
->name
, peer
->hold
);
630 bgp_send_notification(peer
, BGP_ERR_HOLD_TIMER_EXP
, 0);
634 if (time_now
> peer
->keepalive_time
&& !peer
->outbuf
->packet
.header
.len
)
635 bgp_send_keepalive(peer
);
637 else if (peer
->state
== Idle
)
639 if (time_now
> peer
->retry_time
)
642 else if (time_now
> peer
->state_time
+ BGP_STATE_TIME
)
644 LOG(1, 0, 0, "%s timer expired for BGP peer %s\n",
645 bgp_state_str(peer
->state
), peer
->name
);
654 static void bgp_free_routes(struct bgp_route_list
*routes
)
656 struct bgp_route_list
*tmp
;
658 while ((tmp
= routes
))
665 char const *bgp_state_str(enum bgp_state state
)
669 case Disabled
: return "Disabled";
670 case Idle
: return "Idle";
671 case Connect
: return "Connect";
672 case Active
: return "Active";
673 case OpenSent
: return "OpenSent";
674 case OpenConfirm
: return "OpenConfirm";
675 case Established
: return "Established";
681 static char const *bgp_msg_type_str(uint8_t type
)
685 case BGP_MSG_OPEN
: return "OPEN";
686 case BGP_MSG_UPDATE
: return "UPDATE";
687 case BGP_MSG_NOTIFICATION
: return "NOTIFICATION";
688 case BGP_MSG_KEEPALIVE
: return "KEEPALIVE";
694 /* attempt to connect to peer */
695 static int bgp_connect(struct bgp_peer
*peer
)
697 static int bgp_port
= 0;
698 struct sockaddr_in addr
;
699 struct epoll_event ev
;
703 struct servent
*serv
;
704 if (!(serv
= getservbyname("bgp", "tcp")))
706 LOG(0, 0, 0, "Can't get bgp service (%s)\n", strerror(errno
));
710 bgp_port
= serv
->s_port
;
713 if ((peer
->sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
715 LOG(0, 0, 0, "Can't create a socket for BGP peer %s (%s)\n",
716 peer
->name
, strerror(errno
));
718 peer
->state
= peer
->next_state
= Disabled
;
722 /* add to poll set */
723 ev
.events
= peer
->events
= EPOLLOUT
;
724 ev
.data
.ptr
= &peer
->edata
;
725 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, peer
->sock
, &ev
);
727 /* set to non-blocking */
728 fcntl(peer
->sock
, F_SETFL
, fcntl(peer
->sock
, F_GETFL
, 0) | O_NONBLOCK
);
731 memset(&addr
, 0, sizeof(addr
));
732 addr
.sin_family
= AF_INET
;
733 addr
.sin_port
= bgp_port
;
734 addr
.sin_addr
.s_addr
= peer
->addr
;
736 while (connect(peer
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1)
738 if (errno
== EINTR
) /* SIGALARM handler */
741 if (errno
!= EINPROGRESS
)
743 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n",
744 inet_ntoa(addr
.sin_addr
), strerror(errno
));
750 peer
->state
= Connect
;
751 peer
->state_time
= time_now
;
753 LOG(4, 0, 0, "BGP peer %s: state Connect\n", peer
->name
);
757 peer
->state
= Active
;
758 peer
->state_time
= time_now
;
759 peer
->retry_time
= peer
->retry_count
= 0;
761 LOG(4, 0, 0, "BGP peer %s: state Active\n", inet_ntoa(addr
.sin_addr
));
763 return bgp_send_open(peer
);
766 /* complete partial connection (state = Connect) */
767 static int bgp_handle_connect(struct bgp_peer
*peer
)
770 socklen_t len
= sizeof(int);
771 getsockopt(peer
->sock
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
774 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer
->name
,
781 peer
->state
= Active
;
782 peer
->state_time
= time_now
;
784 LOG(4, 0, 0, "BGP peer %s: state Active\n", peer
->name
);
786 return bgp_send_open(peer
);
789 /* initiate a write */
790 static int bgp_write(struct bgp_peer
*peer
)
792 int len
= htons(peer
->outbuf
->packet
.header
.len
);
795 while ((r
= write(peer
->sock
, &peer
->outbuf
->packet
+ peer
->outbuf
->done
,
796 len
- peer
->outbuf
->done
)) == -1)
805 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
807 LOG(1, 0, 0, "Can't write to BGP peer %s (%s)\n", peer
->name
,
816 peer
->outbuf
->done
+= r
;
820 LOG(4, 0, 0, "Sent %s to BGP peer %s\n",
821 bgp_msg_type_str(peer
->outbuf
->packet
.header
.type
), peer
->name
);
823 peer
->outbuf
->packet
.header
.len
= 0;
824 peer
->outbuf
->done
= 0;
826 if (peer
->state
== Established
)
827 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
829 if (peer
->state
!= peer
->next_state
)
831 if (peer
->next_state
== Disabled
|| peer
->next_state
== Idle
)
837 peer
->state
= peer
->next_state
;
838 peer
->state_time
= time_now
;
840 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
841 bgp_state_str(peer
->state
));
847 /* initiate a read */
848 static int bgp_read(struct bgp_peer
*peer
)
852 while ((r
= read(peer
->sock
, &peer
->inbuf
->packet
+ peer
->inbuf
->done
,
853 BGP_MAX_PACKET_SIZE
- peer
->inbuf
->done
)) < 1)
857 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
867 LOG(1, 0, 0, "Can't read from BGP peer %s (%s)\n", peer
->name
,
875 peer
->inbuf
->done
+= r
;
879 /* process buffered packets */
880 static int bgp_handle_input(struct bgp_peer
*peer
)
882 struct bgp_packet
*p
= &peer
->inbuf
->packet
;
883 int len
= ntohs(p
->header
.len
);
885 if (len
> BGP_MAX_PACKET_SIZE
)
887 LOG(1, 0, 0, "Bad header length from BGP %s\n", peer
->name
);
888 bgp_send_notification(peer
, BGP_ERR_HEADER
, BGP_ERR_HDR_BAD_LEN
);
892 if (peer
->inbuf
->done
< len
)
895 LOG(4, 0, 0, "Received %s from BGP peer %s\n",
896 bgp_msg_type_str(p
->header
.type
), peer
->name
);
898 switch (p
->header
.type
)
902 struct bgp_data_open data
;
905 off_t param_offset
, capability_offset
;
906 struct bgp_opt_param
*param
;
907 uint8_t capabilities_len
;
908 char *capabilities
= NULL
;
909 struct bgp_capability
*capability
;
911 for (i
= 0; i
< sizeof(p
->header
.marker
); i
++)
913 if ((unsigned char) p
->header
.marker
[i
] != 0xff)
915 LOG(1, 0, 0, "Invalid marker from BGP peer %s\n",
918 bgp_send_notification(peer
, BGP_ERR_HEADER
,
919 BGP_ERR_HDR_NOT_SYNC
);
925 if (peer
->state
!= OpenSent
)
927 LOG(1, 0, 0, "OPEN from BGP peer %s in %s state\n",
928 peer
->name
, bgp_state_str(peer
->state
));
930 bgp_send_notification(peer
, BGP_ERR_FSM
, 0);
934 memcpy(&data
, p
->data
, len
- sizeof(p
->header
));
936 if (data
.version
!= BGP_VERSION
)
938 LOG(1, 0, 0, "Bad version (%d) sent by BGP peer %s\n",
939 (int) data
.version
, peer
->name
);
941 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_VERSION
);
945 if (ntohs(data
.as
) != peer
->as
)
947 LOG(1, 0, 0, "Bad AS sent by BGP peer %s (got %d, "
948 "expected %d)\n", peer
->name
, (int) htons(data
.as
),
951 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_BAD_AS
);
955 if ((hold
= ntohs(data
.hold_time
)) < 3)
957 LOG(1, 0, 0, "Bad hold time (%d) from BGP peer %s\n",
960 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_HOLD_TIME
);
964 /* pick lowest hold time */
965 if (hold
< peer
->hold
)
968 /* adjust our keepalive based on negotiated hold value */
969 if (peer
->keepalive
* 3 > peer
->hold
)
970 peer
->keepalive
= peer
->hold
/ 3;
972 /* check for optional parameters */
973 /* 2 is for the size of type + len (both uint8_t) */
974 for (param_offset
= 0;
975 param_offset
< data
.opt_len
;
976 param_offset
+= 2 + param
->len
)
978 param
= (struct bgp_opt_param
*)(&data
.opt_params
+ param_offset
);
981 if (data
.opt_len
- param_offset
< 2
982 || param
->len
> data
.opt_len
- param_offset
- 2) {
983 LOG(1, 0, 0, "Malformed Optional Parameter list from BGP peer %s\n",
986 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_UNSPEC
);
990 /* we know only one parameter type */
991 if (param
->type
!= BGP_CAPABILITY_PARAM_TYPE
) {
992 LOG(1, 0, 0, "Unsupported Optional Parameter type %d from BGP peer %s\n",
993 param
->type
, peer
->name
);
995 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_UNSUP_PARAM
);
999 capabilities_len
= param
->len
;
1000 capabilities
= (char *)¶m
->value
;
1003 /* look for BGP multiprotocol capability */
1006 for (capability_offset
= 0;
1007 capability_offset
< capabilities_len
;
1008 capability_offset
+= 2 + capability
->len
)
1010 capability
= (struct bgp_capability
*)(capabilities
+ capability_offset
);
1012 /* sensible check */
1013 if (capabilities_len
- capability_offset
< 2
1014 || capability
->len
> capabilities_len
- capability_offset
- 2) {
1015 LOG(1, 0, 0, "Malformed Capabilities list from BGP peer %s\n",
1018 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_UNSPEC
);
1022 /* we only know one capability code */
1023 if (capability
->code
!= XXX
) {
1024 LOG(4, 0, 0, "Unsupported Capability code %d from BGP peer %s\n",
1025 capability
->code
, peer
->name
);
1027 /* TODO: send _which_ capability is unsupported */
1028 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_UNSUP_CAP
);
1029 /* we don't terminate, still; we just jump to the next one */
1034 /* next transition requires an exchange of keepalives */
1035 bgp_send_keepalive(peer
);
1040 case BGP_MSG_KEEPALIVE
:
1041 if (peer
->state
== OpenConfirm
)
1043 peer
->state
= peer
->next_state
= Established
;
1044 peer
->state_time
= time_now
;
1045 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
1046 peer
->update_routes
= 1;
1047 peer
->retry_count
= 0;
1048 peer
->retry_time
= 0;
1050 LOG(4, 0, 0, "BGP peer %s: state Established\n", peer
->name
);
1055 case BGP_MSG_NOTIFICATION
:
1056 if (len
> sizeof(p
->header
))
1058 struct bgp_data_notification
*notification
=
1059 (struct bgp_data_notification
*) p
->data
;
1061 if (notification
->error_code
== BGP_ERR_CEASE
)
1063 LOG(4, 0, 0, "BGP peer %s sent CEASE\n", peer
->name
);
1068 /* FIXME: should handle more notifications */
1069 LOG(4, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n",
1070 peer
->name
, (int) notification
->error_code
);
1077 peer
->expire_time
= time_now
+ peer
->hold
;
1079 /* see if there's another message in the same packet/buffer */
1080 if (peer
->inbuf
->done
> len
)
1082 peer
->inbuf
->done
-= len
;
1083 memmove(p
, (char *) p
+ len
, peer
->inbuf
->done
);
1087 peer
->inbuf
->packet
.header
.len
= 0;
1088 peer
->inbuf
->done
= 0;
1091 return peer
->inbuf
->done
;
1094 /* send/buffer OPEN message */
1095 static int bgp_send_open(struct bgp_peer
*peer
)
1097 struct bgp_data_open data
;
1098 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1100 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1101 sizeof(peer
->outbuf
->packet
.header
.marker
));
1103 peer
->outbuf
->packet
.header
.type
= BGP_MSG_OPEN
;
1105 data
.version
= BGP_VERSION
;
1106 data
.as
= htons(our_as
);
1107 data
.hold_time
= htons(peer
->hold
);
1108 data
.identifier
= my_address
;
1111 memcpy(peer
->outbuf
->packet
.data
, &data
, BGP_DATA_OPEN_SIZE
);
1112 len
+= BGP_DATA_OPEN_SIZE
;
1114 peer
->outbuf
->packet
.header
.len
= htons(len
);
1115 peer
->outbuf
->done
= 0;
1116 peer
->next_state
= OpenSent
;
1118 return bgp_write(peer
);
1121 /* send/buffer KEEPALIVE message */
1122 static int bgp_send_keepalive(struct bgp_peer
*peer
)
1124 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1125 sizeof(peer
->outbuf
->packet
.header
.marker
));
1127 peer
->outbuf
->packet
.header
.type
= BGP_MSG_KEEPALIVE
;
1128 peer
->outbuf
->packet
.header
.len
=
1129 htons(sizeof(peer
->outbuf
->packet
.header
));
1131 peer
->outbuf
->done
= 0;
1132 peer
->next_state
= (peer
->state
== OpenSent
) ? OpenConfirm
: peer
->state
;
1134 return bgp_write(peer
);
1137 /* send/buffer UPDATE message */
1138 static int bgp_send_update(struct bgp_peer
*peer
)
1140 uint16_t unf_len
= 0;
1142 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1143 struct bgp_route_list
*have
= peer
->routes
;
1144 struct bgp_route_list
*want
= peer
->routing
? bgp_routes
: 0;
1145 struct bgp_route_list
*e
= 0;
1146 struct bgp_route_list
*add
= 0;
1149 char *data
= (char *) &peer
->outbuf
->packet
.data
;
1151 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1152 char *max
= (char *) &peer
->outbuf
->packet
.data
1153 + sizeof(peer
->outbuf
->packet
.data
)
1154 - sizeof(attr_len
) - peer
->path_attr_len
- sizeof(struct bgp_ip_prefix
);
1156 /* skip over unf_len */
1157 data
+= sizeof(unf_len
);
1158 len
+= sizeof(unf_len
);
1160 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1161 sizeof(peer
->outbuf
->packet
.header
.marker
));
1163 peer
->outbuf
->packet
.header
.type
= BGP_MSG_UPDATE
;
1165 peer
->update_routes
= 0; /* tentatively clear */
1167 /* find differences */
1168 while ((have
|| want
) && data
< (max
- sizeof(struct bgp_ip_prefix
)))
1172 ? memcmp(&have
->dest
, &want
->dest
, sizeof(have
->dest
))
1177 if (s
< 0) /* found one to delete */
1179 struct bgp_route_list
*tmp
= have
;
1182 s
= BGP_IP_PREFIX_SIZE(tmp
->dest
);
1183 memcpy(data
, &tmp
->dest
, s
);
1188 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1189 fmtaddr(tmp
->dest
.prefix
, 0), tmp
->dest
.len
, peer
->name
);
1196 peer
->routes
= have
;
1202 e
= have
; /* stash the last found to relink above */
1206 else if (s
> 0) /* addition reqd. */
1210 peer
->update_routes
= 1; /* only one add per packet */
1224 peer
->update_routes
= 1; /* more to do */
1226 /* anything changed? */
1227 if (!(unf_len
|| add
))
1230 /* go back and insert unf_len */
1231 unf_len
= htons(unf_len
);
1232 memcpy(&peer
->outbuf
->packet
.data
, &unf_len
, sizeof(unf_len
));
1236 if (!(e
= malloc(sizeof(*e
))))
1238 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1239 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, strerror(errno
));
1244 memcpy(e
, add
, sizeof(*e
));
1246 peer
->routes
= bgp_insert_route(peer
->routes
, e
);
1248 attr_len
= htons(peer
->path_attr_len
);
1249 memcpy(data
, &attr_len
, sizeof(attr_len
));
1250 data
+= sizeof(attr_len
);
1251 len
+= sizeof(attr_len
);
1253 memcpy(data
, peer
->path_attrs
, peer
->path_attr_len
);
1254 data
+= peer
->path_attr_len
;
1255 len
+= peer
->path_attr_len
;
1257 s
= BGP_IP_PREFIX_SIZE(add
->dest
);
1258 memcpy(data
, &add
->dest
, s
);
1262 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1263 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, peer
->name
);
1268 memcpy(data
, &attr_len
, sizeof(attr_len
));
1269 data
+= sizeof(attr_len
);
1270 len
+= sizeof(attr_len
);
1273 peer
->outbuf
->packet
.header
.len
= htons(len
);
1274 peer
->outbuf
->done
= 0;
1276 return bgp_write(peer
);
1279 /* send/buffer NOTIFICATION message */
1280 static int bgp_send_notification(struct bgp_peer
*peer
, uint8_t code
,
1283 struct bgp_data_notification data
;
1286 data
.error_code
= code
;
1287 len
+= sizeof(data
.error_code
);
1289 data
.error_subcode
= subcode
;
1290 len
+= sizeof(data
.error_code
);
1292 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1293 sizeof(peer
->outbuf
->packet
.header
.marker
));
1295 peer
->outbuf
->packet
.header
.type
= BGP_MSG_NOTIFICATION
;
1296 peer
->outbuf
->packet
.header
.len
=
1297 htons(sizeof(peer
->outbuf
->packet
.header
) + len
);
1299 memcpy(peer
->outbuf
->packet
.data
, &data
, len
);
1301 peer
->outbuf
->done
= 0;
1302 peer
->next_state
= code
== BGP_ERR_CEASE
? Disabled
: Idle
;
1304 /* we're dying; ignore any pending input */
1305 peer
->inbuf
->packet
.header
.len
= 0;
1306 peer
->inbuf
->done
= 0;
1308 return bgp_write(peer
);