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 return bgp_send_open(peer
);
768 /* complete partial connection (state = Connect) */
769 static int bgp_handle_connect(struct bgp_peer
*peer
)
772 socklen_t len
= sizeof(int);
773 getsockopt(peer
->sock
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
776 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer
->name
,
783 peer
->state
= Active
;
784 peer
->state_time
= time_now
;
786 LOG(4, 0, 0, "BGP peer %s: state Active\n", peer
->name
);
788 return bgp_send_open(peer
);
791 /* initiate a write */
792 static int bgp_write(struct bgp_peer
*peer
)
794 int len
= htons(peer
->outbuf
->packet
.header
.len
);
797 while ((r
= write(peer
->sock
, &peer
->outbuf
->packet
+ peer
->outbuf
->done
,
798 len
- peer
->outbuf
->done
)) == -1)
807 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
809 LOG(1, 0, 0, "Can't write to BGP peer %s (%s)\n", peer
->name
,
818 peer
->outbuf
->done
+= r
;
822 LOG(4, 0, 0, "Sent %s to BGP peer %s\n",
823 bgp_msg_type_str(peer
->outbuf
->packet
.header
.type
), peer
->name
);
825 peer
->outbuf
->packet
.header
.len
= 0;
826 peer
->outbuf
->done
= 0;
828 if (peer
->state
== Established
)
829 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
831 if (peer
->state
!= peer
->next_state
)
833 if (peer
->next_state
== Disabled
|| peer
->next_state
== Idle
)
839 peer
->state
= peer
->next_state
;
840 peer
->state_time
= time_now
;
842 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
843 bgp_state_str(peer
->state
));
849 /* initiate a read */
850 static int bgp_read(struct bgp_peer
*peer
)
854 while ((r
= read(peer
->sock
, &peer
->inbuf
->packet
+ peer
->inbuf
->done
,
855 BGP_MAX_PACKET_SIZE
- peer
->inbuf
->done
)) < 1)
859 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
869 LOG(1, 0, 0, "Can't read from BGP peer %s (%s)\n", peer
->name
,
877 peer
->inbuf
->done
+= r
;
881 /* process buffered packets */
882 static int bgp_handle_input(struct bgp_peer
*peer
)
884 struct bgp_packet
*p
= &peer
->inbuf
->packet
;
885 int len
= ntohs(p
->header
.len
);
887 if (len
> BGP_MAX_PACKET_SIZE
)
889 LOG(1, 0, 0, "Bad header length from BGP %s\n", peer
->name
);
890 bgp_send_notification(peer
, BGP_ERR_HEADER
, BGP_ERR_HDR_BAD_LEN
);
894 if (peer
->inbuf
->done
< len
)
897 LOG(4, 0, 0, "Received %s from BGP peer %s\n",
898 bgp_msg_type_str(p
->header
.type
), peer
->name
);
900 switch (p
->header
.type
)
904 struct bgp_data_open data
;
907 off_t param_offset
, capability_offset
;
908 struct bgp_opt_param
*param
;
909 uint8_t capabilities_len
;
910 char *capabilities
= NULL
;
911 struct bgp_capability
*capability
;
912 struct bgp_mp_cap_param
*mp_cap
;
914 for (i
= 0; i
< sizeof(p
->header
.marker
); i
++)
916 if ((unsigned char) p
->header
.marker
[i
] != 0xff)
918 LOG(1, 0, 0, "Invalid marker from BGP peer %s\n",
921 bgp_send_notification(peer
, BGP_ERR_HEADER
,
922 BGP_ERR_HDR_NOT_SYNC
);
928 if (peer
->state
!= OpenSent
)
930 LOG(1, 0, 0, "OPEN from BGP peer %s in %s state\n",
931 peer
->name
, bgp_state_str(peer
->state
));
933 bgp_send_notification(peer
, BGP_ERR_FSM
, 0);
937 memcpy(&data
, p
->data
, len
- sizeof(p
->header
));
939 if (data
.version
!= BGP_VERSION
)
941 LOG(1, 0, 0, "Bad version (%d) sent by BGP peer %s\n",
942 (int) data
.version
, peer
->name
);
944 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_VERSION
);
948 if (ntohs(data
.as
) != peer
->as
)
950 LOG(1, 0, 0, "Bad AS sent by BGP peer %s (got %d, "
951 "expected %d)\n", peer
->name
, (int) htons(data
.as
),
954 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_BAD_AS
);
958 if ((hold
= ntohs(data
.hold_time
)) < 3)
960 LOG(1, 0, 0, "Bad hold time (%d) from BGP peer %s\n",
963 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_HOLD_TIME
);
967 /* pick lowest hold time */
968 if (hold
< peer
->hold
)
971 /* adjust our keepalive based on negotiated hold value */
972 if (peer
->keepalive
* 3 > peer
->hold
)
973 peer
->keepalive
= peer
->hold
/ 3;
975 /* check for optional parameters */
976 /* 2 is for the size of type + len (both uint8_t) */
977 for (param_offset
= 0;
978 param_offset
< data
.opt_len
;
979 param_offset
+= 2 + param
->len
)
981 param
= (struct bgp_opt_param
*)(&data
.opt_params
+ param_offset
);
984 if (data
.opt_len
- param_offset
< 2
985 || param
->len
> data
.opt_len
- param_offset
- 2)
987 LOG(1, 0, 0, "Malformed Optional Parameter list from BGP peer %s\n",
990 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_UNSPEC
);
994 /* we know only one parameter type */
995 if (param
->type
!= BGP_CAPABILITY_PARAM_TYPE
)
997 LOG(1, 0, 0, "Unsupported Optional Parameter type %d from BGP peer %s\n",
998 param
->type
, peer
->name
);
1000 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_UNSUP_PARAM
);
1004 capabilities_len
= param
->len
;
1005 capabilities
= (char *)¶m
->value
;
1008 /* look for BGP multiprotocol capability */
1011 for (capability_offset
= 0;
1012 capability_offset
< capabilities_len
;
1013 capability_offset
+= 2 + capability
->len
)
1015 capability
= (struct bgp_capability
*)(capabilities
+ capability_offset
);
1017 /* sensible check */
1018 if (capabilities_len
- capability_offset
< 2
1019 || capability
->len
> capabilities_len
- capability_offset
- 2)
1021 LOG(1, 0, 0, "Malformed Capabilities list from BGP peer %s\n",
1024 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_UNSPEC
);
1028 /* we only know one capability code */
1029 if (capability
->code
!= BGP_CAP_CODE_MP
1030 && capability
->len
!= sizeof(struct bgp_mp_cap_param
))
1032 LOG(4, 0, 0, "Unsupported Capability code %d from BGP peer %s\n",
1033 capability
->code
, peer
->name
);
1035 bgp_send_notification_full(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_UNSUP_CAP
,
1036 capability
, 2 + capability
->len
);
1037 /* we don't terminate, still; we just jump to the next one */
1041 mp_cap
= (struct bgp_mp_cap_param
*)&capability
->value
;
1042 /* the only <AFI, SAFI> tuple we support */
1043 if (mp_cap
->afi
!= AF_INET6
&& mp_cap
->safi
!= BGP_MP_SAFI_UNICAST
)
1045 LOG(4, 0, 0, "Unsupported multiprotocol AFI %d and SAFI %d from BGP peer %s\n",
1046 mp_cap
->afi
, mp_cap
->safi
, peer
->name
);
1048 bgp_send_notification_full(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_UNSUP_CAP
,
1049 capability
, 2 + capability
->len
);
1050 /* we don't terminate, still; we just jump to the next one */
1057 /* next transition requires an exchange of keepalives */
1058 bgp_send_keepalive(peer
);
1063 case BGP_MSG_KEEPALIVE
:
1064 if (peer
->state
== OpenConfirm
)
1066 peer
->state
= peer
->next_state
= Established
;
1067 peer
->state_time
= time_now
;
1068 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
1069 peer
->update_routes
= 1;
1070 peer
->retry_count
= 0;
1071 peer
->retry_time
= 0;
1073 LOG(4, 0, 0, "BGP peer %s: state Established\n", peer
->name
);
1078 case BGP_MSG_NOTIFICATION
:
1079 if (len
> sizeof(p
->header
))
1081 struct bgp_data_notification
*notification
=
1082 (struct bgp_data_notification
*) p
->data
;
1084 if (notification
->error_code
== BGP_ERR_CEASE
)
1086 LOG(4, 0, 0, "BGP peer %s sent CEASE\n", peer
->name
);
1091 /* FIXME: should handle more notifications */
1092 LOG(4, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n",
1093 peer
->name
, (int) notification
->error_code
);
1100 peer
->expire_time
= time_now
+ peer
->hold
;
1102 /* see if there's another message in the same packet/buffer */
1103 if (peer
->inbuf
->done
> len
)
1105 peer
->inbuf
->done
-= len
;
1106 memmove(p
, (char *) p
+ len
, peer
->inbuf
->done
);
1110 peer
->inbuf
->packet
.header
.len
= 0;
1111 peer
->inbuf
->done
= 0;
1114 return peer
->inbuf
->done
;
1117 /* send/buffer OPEN message */
1118 static int bgp_send_open(struct bgp_peer
*peer
)
1120 struct bgp_data_open data
;
1121 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1123 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1124 sizeof(peer
->outbuf
->packet
.header
.marker
));
1126 peer
->outbuf
->packet
.header
.type
= BGP_MSG_OPEN
;
1128 data
.version
= BGP_VERSION
;
1129 data
.as
= htons(our_as
);
1130 data
.hold_time
= htons(peer
->hold
);
1131 data
.identifier
= my_address
;
1134 memcpy(peer
->outbuf
->packet
.data
, &data
, BGP_DATA_OPEN_SIZE
);
1135 len
+= BGP_DATA_OPEN_SIZE
;
1137 peer
->outbuf
->packet
.header
.len
= htons(len
);
1138 peer
->outbuf
->done
= 0;
1139 peer
->next_state
= OpenSent
;
1141 return bgp_write(peer
);
1144 /* send/buffer KEEPALIVE message */
1145 static int bgp_send_keepalive(struct bgp_peer
*peer
)
1147 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1148 sizeof(peer
->outbuf
->packet
.header
.marker
));
1150 peer
->outbuf
->packet
.header
.type
= BGP_MSG_KEEPALIVE
;
1151 peer
->outbuf
->packet
.header
.len
=
1152 htons(sizeof(peer
->outbuf
->packet
.header
));
1154 peer
->outbuf
->done
= 0;
1155 peer
->next_state
= (peer
->state
== OpenSent
) ? OpenConfirm
: peer
->state
;
1157 return bgp_write(peer
);
1160 /* send/buffer UPDATE message */
1161 static int bgp_send_update(struct bgp_peer
*peer
)
1163 uint16_t unf_len
= 0;
1165 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1166 struct bgp_route_list
*have
= peer
->routes
;
1167 struct bgp_route_list
*want
= peer
->routing
? bgp_routes
: 0;
1168 struct bgp_route_list
*e
= 0;
1169 struct bgp_route_list
*add
= 0;
1172 char *data
= (char *) &peer
->outbuf
->packet
.data
;
1174 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1175 char *max
= (char *) &peer
->outbuf
->packet
.data
1176 + sizeof(peer
->outbuf
->packet
.data
)
1177 - sizeof(attr_len
) - peer
->path_attr_len
- sizeof(struct bgp_ip_prefix
);
1179 /* skip over unf_len */
1180 data
+= sizeof(unf_len
);
1181 len
+= sizeof(unf_len
);
1183 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1184 sizeof(peer
->outbuf
->packet
.header
.marker
));
1186 peer
->outbuf
->packet
.header
.type
= BGP_MSG_UPDATE
;
1188 peer
->update_routes
= 0; /* tentatively clear */
1190 /* find differences */
1191 while ((have
|| want
) && data
< (max
- sizeof(struct bgp_ip_prefix
)))
1195 ? memcmp(&have
->dest
, &want
->dest
, sizeof(have
->dest
))
1200 if (s
< 0) /* found one to delete */
1202 struct bgp_route_list
*tmp
= have
;
1205 s
= BGP_IP_PREFIX_SIZE(tmp
->dest
);
1206 memcpy(data
, &tmp
->dest
, s
);
1211 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1212 fmtaddr(tmp
->dest
.prefix
, 0), tmp
->dest
.len
, peer
->name
);
1219 peer
->routes
= have
;
1225 e
= have
; /* stash the last found to relink above */
1229 else if (s
> 0) /* addition reqd. */
1233 peer
->update_routes
= 1; /* only one add per packet */
1247 peer
->update_routes
= 1; /* more to do */
1249 /* anything changed? */
1250 if (!(unf_len
|| add
))
1253 /* go back and insert unf_len */
1254 unf_len
= htons(unf_len
);
1255 memcpy(&peer
->outbuf
->packet
.data
, &unf_len
, sizeof(unf_len
));
1259 if (!(e
= malloc(sizeof(*e
))))
1261 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1262 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, strerror(errno
));
1267 memcpy(e
, add
, sizeof(*e
));
1269 peer
->routes
= bgp_insert_route(peer
->routes
, e
);
1271 attr_len
= htons(peer
->path_attr_len
);
1272 memcpy(data
, &attr_len
, sizeof(attr_len
));
1273 data
+= sizeof(attr_len
);
1274 len
+= sizeof(attr_len
);
1276 memcpy(data
, peer
->path_attrs
, peer
->path_attr_len
);
1277 data
+= peer
->path_attr_len
;
1278 len
+= peer
->path_attr_len
;
1280 s
= BGP_IP_PREFIX_SIZE(add
->dest
);
1281 memcpy(data
, &add
->dest
, s
);
1285 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1286 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, peer
->name
);
1291 memcpy(data
, &attr_len
, sizeof(attr_len
));
1292 data
+= sizeof(attr_len
);
1293 len
+= sizeof(attr_len
);
1296 peer
->outbuf
->packet
.header
.len
= htons(len
);
1297 peer
->outbuf
->done
= 0;
1299 return bgp_write(peer
);
1302 /* send/buffer NOTIFICATION message */
1303 static int bgp_send_notification(struct bgp_peer
*peer
, uint8_t code
,
1306 return bgp_send_notification_full(peer
, code
, subcode
, NULL
, 0);
1309 static int bgp_send_notification_full(struct bgp_peer
*peer
, uint8_t code
,
1310 uint8_t subcode
, char *notification_data
, uint16_t data_len
)
1312 struct bgp_data_notification data
;
1315 data
.error_code
= code
;
1316 len
+= sizeof(data
.error_code
);
1318 data
.error_subcode
= subcode
;
1319 len
+= sizeof(data
.error_code
);
1321 memcpy(data
.data
, notification_data
, data_len
);
1324 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1325 sizeof(peer
->outbuf
->packet
.header
.marker
));
1327 peer
->outbuf
->packet
.header
.type
= BGP_MSG_NOTIFICATION
;
1328 peer
->outbuf
->packet
.header
.len
=
1329 htons(sizeof(peer
->outbuf
->packet
.header
) + len
);
1331 memcpy(peer
->outbuf
->packet
.data
, &data
, len
);
1333 peer
->outbuf
->done
= 0;
1334 peer
->next_state
= code
== BGP_ERR_CEASE
? Disabled
: Idle
;
1336 /* we're dying; ignore any pending input */
1337 peer
->inbuf
->packet
.header
.len
= 0;
1338 peer
->inbuf
->done
= 0;
1340 return bgp_write(peer
);