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
, int prefixlen
, 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 /* insert route into list; sorted */
331 static struct bgp_route_list
*bgp_insert_route(struct bgp_route_list
*head
,
332 struct bgp_route_list
*new)
334 struct bgp_route_list
*p
= head
;
335 struct bgp_route_list
*e
= 0;
337 while (p
&& memcmp(&p
->dest
, &new->dest
, sizeof(p
->dest
)) < 0)
357 /* add route to list for peers */
359 * Note: this doesn't do route aggregation, nor drop routes if a less
360 * specific match already exists (partly because I'm lazy, but also so
361 * that if that route is later deleted we don't have to be concerned
362 * about adding back the more specific one).
364 int bgp_add_route(in_addr_t ip
, int prefixlen
)
366 struct bgp_route_list
*r
= bgp_routes
;
367 struct bgp_route_list add
;
370 add
.dest
.prefix
= ip
;
371 add
.dest
.len
= prefixlen
;
374 /* check for duplicate */
377 i
= memcmp(&r
->dest
, &add
.dest
, sizeof(r
->dest
));
379 return 1; /* already covered */
387 /* insert into route list; sorted */
388 if (!(r
= malloc(sizeof(*r
))))
390 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
391 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
, strerror(errno
));
396 memcpy(r
, &add
, sizeof(*r
));
397 bgp_routes
= bgp_insert_route(bgp_routes
, r
);
399 /* flag established peers for update */
400 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
401 if (bgp_peers
[i
].state
== Established
)
402 bgp_peers
[i
].update_routes
= 1;
404 LOG(4, 0, 0, "Registered BGP route %s/%d\n",
405 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
);
410 /* remove route from list for peers */
411 int bgp_del_route(in_addr_t ip
, int prefixlen
)
413 struct bgp_route_list
*r
= bgp_routes
;
414 struct bgp_route_list
*e
= 0;
415 struct bgp_route_list del
;
418 del
.dest
.prefix
= ip
;
419 del
.dest
.len
= prefixlen
;
422 /* find entry in routes list and remove */
425 i
= memcmp(&r
->dest
, &del
.dest
, sizeof(r
->dest
));
431 bgp_routes
= r
->next
;
449 /* flag established peers for update */
450 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
451 if (bgp_peers
[i
].state
== Established
)
452 bgp_peers
[i
].update_routes
= 1;
454 LOG(4, 0, 0, "Removed BGP route %s/%d\n",
455 fmtaddr(del
.dest
.prefix
, 0), del
.dest
.len
);
460 /* enable or disable routing */
461 void bgp_enable_routing(int enable
)
465 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
467 bgp_peers
[i
].routing
= enable
;
469 /* flag established peers for update */
470 if (bgp_peers
[i
].state
== Established
)
471 bgp_peers
[i
].update_routes
= 1;
474 LOG(4, 0, 0, "%s BGP routing\n", enable
? "Enabled" : "Suspended");
478 # include <sys/epoll.h>
480 # include "fake_epoll.h"
483 /* return a bitmask of the events required to poll this peer's fd */
491 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
493 struct bgp_peer
*peer
= &bgp_peers
[i
];
496 if (peer
->state
== Disabled
|| peer
->state
== Idle
)
499 if (peer
->inbuf
->done
< BGP_MAX_PACKET_SIZE
)
502 if (peer
->state
== Connect
|| /* connection in progress */
503 peer
->update_routes
|| /* routing updates */
504 peer
->outbuf
->packet
.header
.len
) /* pending output */
507 if (peer
->events
!= events
)
509 struct epoll_event ev
;
511 ev
.events
= peer
->events
= events
;
512 ev
.data
.ptr
= &peer
->edata
;
513 epoll_ctl(epollfd
, EPOLL_CTL_MOD
, peer
->sock
, &ev
);
520 /* process bgp events/timers */
521 int bgp_process(uint32_t events
[])
528 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
530 struct bgp_peer
*peer
= &bgp_peers
[i
];
532 if (*peer
->name
&& peer
->cli_flag
== BGP_CLI_RESTART
)
538 if (peer
->state
== Disabled
)
543 switch (peer
->cli_flag
)
545 case BGP_CLI_SUSPEND
:
549 if (peer
->state
== Established
)
550 peer
->update_routes
= 1;
559 if (peer
->state
== Established
)
560 peer
->update_routes
= 1;
569 /* handle empty/fill of buffers */
570 if (events
[i
] & EPOLLOUT
)
573 if (peer
->state
== Connect
)
574 r
= bgp_handle_connect(peer
);
575 else if (peer
->outbuf
->packet
.header
.len
)
582 if (events
[i
] & (EPOLLIN
|EPOLLHUP
))
588 /* process input buffer contents */
589 while (peer
->inbuf
->done
>= sizeof(peer
->inbuf
->packet
.header
)
590 && !peer
->outbuf
->packet
.header
.len
) /* may need to queue a response */
592 if (bgp_handle_input(peer
) < 0)
596 /* process pending updates */
597 if (peer
->update_routes
598 && !peer
->outbuf
->packet
.header
.len
) /* ditto */
600 if (!bgp_send_update(peer
))
605 if (peer
->state
== Established
)
607 if (time_now
> peer
->expire_time
)
609 LOG(1, 0, 0, "No message from BGP peer %s in %ds\n",
610 peer
->name
, peer
->hold
);
612 bgp_send_notification(peer
, BGP_ERR_HOLD_TIMER_EXP
, 0);
616 if (time_now
> peer
->keepalive_time
&& !peer
->outbuf
->packet
.header
.len
)
617 bgp_send_keepalive(peer
);
619 else if (peer
->state
== Idle
)
621 if (time_now
> peer
->retry_time
)
624 else if (time_now
> peer
->state_time
+ BGP_STATE_TIME
)
626 LOG(1, 0, 0, "%s timer expired for BGP peer %s\n",
627 bgp_state_str(peer
->state
), peer
->name
);
636 static void bgp_free_routes(struct bgp_route_list
*routes
)
638 struct bgp_route_list
*tmp
;
640 while ((tmp
= routes
))
647 char const *bgp_state_str(enum bgp_state state
)
651 case Disabled
: return "Disabled";
652 case Idle
: return "Idle";
653 case Connect
: return "Connect";
654 case Active
: return "Active";
655 case OpenSent
: return "OpenSent";
656 case OpenConfirm
: return "OpenConfirm";
657 case Established
: return "Established";
663 static char const *bgp_msg_type_str(uint8_t type
)
667 case BGP_MSG_OPEN
: return "OPEN";
668 case BGP_MSG_UPDATE
: return "UPDATE";
669 case BGP_MSG_NOTIFICATION
: return "NOTIFICATION";
670 case BGP_MSG_KEEPALIVE
: return "KEEPALIVE";
676 /* attempt to connect to peer */
677 static int bgp_connect(struct bgp_peer
*peer
)
679 static int bgp_port
= 0;
680 struct sockaddr_in addr
;
681 struct epoll_event ev
;
685 struct servent
*serv
;
686 if (!(serv
= getservbyname("bgp", "tcp")))
688 LOG(0, 0, 0, "Can't get bgp service (%s)\n", strerror(errno
));
692 bgp_port
= serv
->s_port
;
695 if ((peer
->sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
697 LOG(0, 0, 0, "Can't create a socket for BGP peer %s (%s)\n",
698 peer
->name
, strerror(errno
));
700 peer
->state
= peer
->next_state
= Disabled
;
704 /* add to poll set */
705 ev
.events
= peer
->events
= EPOLLOUT
;
706 ev
.data
.ptr
= &peer
->edata
;
707 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, peer
->sock
, &ev
);
709 /* set to non-blocking */
710 fcntl(peer
->sock
, F_SETFL
, fcntl(peer
->sock
, F_GETFL
, 0) | O_NONBLOCK
);
713 memset(&addr
, 0, sizeof(addr
));
714 addr
.sin_family
= AF_INET
;
715 addr
.sin_port
= bgp_port
;
716 addr
.sin_addr
.s_addr
= peer
->addr
;
718 while (connect(peer
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1)
720 if (errno
== EINTR
) /* SIGALARM handler */
723 if (errno
!= EINPROGRESS
)
725 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n",
726 inet_ntoa(addr
.sin_addr
), strerror(errno
));
732 peer
->state
= Connect
;
733 peer
->state_time
= time_now
;
735 LOG(4, 0, 0, "BGP peer %s: state Connect\n", peer
->name
);
739 peer
->state
= Active
;
740 peer
->state_time
= time_now
;
741 peer
->retry_time
= peer
->retry_count
= 0;
743 LOG(4, 0, 0, "BGP peer %s: state Active\n", inet_ntoa(addr
.sin_addr
));
745 return bgp_send_open(peer
);
748 /* complete partial connection (state = Connect) */
749 static int bgp_handle_connect(struct bgp_peer
*peer
)
752 socklen_t len
= sizeof(int);
753 getsockopt(peer
->sock
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
756 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer
->name
,
763 peer
->state
= Active
;
764 peer
->state_time
= time_now
;
766 LOG(4, 0, 0, "BGP peer %s: state Active\n", peer
->name
);
768 return bgp_send_open(peer
);
771 /* initiate a write */
772 static int bgp_write(struct bgp_peer
*peer
)
774 int len
= htons(peer
->outbuf
->packet
.header
.len
);
777 while ((r
= write(peer
->sock
, &peer
->outbuf
->packet
+ peer
->outbuf
->done
,
778 len
- peer
->outbuf
->done
)) == -1)
787 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
789 LOG(1, 0, 0, "Can't write to BGP peer %s (%s)\n", peer
->name
,
798 peer
->outbuf
->done
+= r
;
802 LOG(4, 0, 0, "Sent %s to BGP peer %s\n",
803 bgp_msg_type_str(peer
->outbuf
->packet
.header
.type
), peer
->name
);
805 peer
->outbuf
->packet
.header
.len
= 0;
806 peer
->outbuf
->done
= 0;
808 if (peer
->state
== Established
)
809 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
811 if (peer
->state
!= peer
->next_state
)
813 if (peer
->next_state
== Disabled
|| peer
->next_state
== Idle
)
819 peer
->state
= peer
->next_state
;
820 peer
->state_time
= time_now
;
822 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
823 bgp_state_str(peer
->state
));
829 /* initiate a read */
830 static int bgp_read(struct bgp_peer
*peer
)
834 while ((r
= read(peer
->sock
, &peer
->inbuf
->packet
+ peer
->inbuf
->done
,
835 BGP_MAX_PACKET_SIZE
- peer
->inbuf
->done
)) < 1)
839 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
849 LOG(1, 0, 0, "Can't read from BGP peer %s (%s)\n", peer
->name
,
857 peer
->inbuf
->done
+= r
;
861 /* process buffered packets */
862 static int bgp_handle_input(struct bgp_peer
*peer
)
864 struct bgp_packet
*p
= &peer
->inbuf
->packet
;
865 int len
= ntohs(p
->header
.len
);
867 if (len
> BGP_MAX_PACKET_SIZE
)
869 LOG(1, 0, 0, "Bad header length from BGP %s\n", peer
->name
);
870 bgp_send_notification(peer
, BGP_ERR_HEADER
, BGP_ERR_HDR_BAD_LEN
);
874 if (peer
->inbuf
->done
< len
)
877 LOG(4, 0, 0, "Received %s from BGP peer %s\n",
878 bgp_msg_type_str(p
->header
.type
), peer
->name
);
880 switch (p
->header
.type
)
884 struct bgp_data_open data
;
888 for (i
= 0; i
< sizeof(p
->header
.marker
); i
++)
890 if ((unsigned char) p
->header
.marker
[i
] != 0xff)
892 LOG(1, 0, 0, "Invalid marker from BGP peer %s\n",
895 bgp_send_notification(peer
, BGP_ERR_HEADER
,
896 BGP_ERR_HDR_NOT_SYNC
);
902 if (peer
->state
!= OpenSent
)
904 LOG(1, 0, 0, "OPEN from BGP peer %s in %s state\n",
905 peer
->name
, bgp_state_str(peer
->state
));
907 bgp_send_notification(peer
, BGP_ERR_FSM
, 0);
911 memcpy(&data
, p
->data
, len
- sizeof(p
->header
));
913 if (data
.version
!= BGP_VERSION
)
915 LOG(1, 0, 0, "Bad version (%d) sent by BGP peer %s\n",
916 (int) data
.version
, peer
->name
);
918 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_VERSION
);
922 if (ntohs(data
.as
) != peer
->as
)
924 LOG(1, 0, 0, "Bad AS sent by BGP peer %s (got %d, "
925 "expected %d)\n", peer
->name
, (int) htons(data
.as
),
928 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_BAD_AS
);
932 if ((hold
= ntohs(data
.hold_time
)) < 3)
934 LOG(1, 0, 0, "Bad hold time (%d) from BGP peer %s\n",
937 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_HOLD_TIME
);
941 /* pick lowest hold time */
942 if (hold
< peer
->hold
)
945 /* adjust our keepalive based on negotiated hold value */
946 if (peer
->keepalive
* 3 > peer
->hold
)
947 peer
->keepalive
= peer
->hold
/ 3;
949 /* next transition requires an exchange of keepalives */
950 bgp_send_keepalive(peer
);
952 /* FIXME: may need to check for optional params */
957 case BGP_MSG_KEEPALIVE
:
958 if (peer
->state
== OpenConfirm
)
960 peer
->state
= peer
->next_state
= Established
;
961 peer
->state_time
= time_now
;
962 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
963 peer
->update_routes
= 1;
964 peer
->retry_count
= 0;
965 peer
->retry_time
= 0;
967 LOG(4, 0, 0, "BGP peer %s: state Established\n", peer
->name
);
972 case BGP_MSG_NOTIFICATION
:
973 if (len
> sizeof(p
->header
))
975 struct bgp_data_notification
*notification
=
976 (struct bgp_data_notification
*) p
->data
;
978 if (notification
->error_code
== BGP_ERR_CEASE
)
980 LOG(4, 0, 0, "BGP peer %s sent CEASE\n", peer
->name
);
985 /* FIXME: should handle more notifications */
986 LOG(4, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n",
987 peer
->name
, (int) notification
->error_code
);
994 peer
->expire_time
= time_now
+ peer
->hold
;
996 /* see if there's another message in the same packet/buffer */
997 if (peer
->inbuf
->done
> len
)
999 peer
->inbuf
->done
-= len
;
1000 memmove(p
, (char *) p
+ len
, peer
->inbuf
->done
);
1004 peer
->inbuf
->packet
.header
.len
= 0;
1005 peer
->inbuf
->done
= 0;
1008 return peer
->inbuf
->done
;
1011 /* send/buffer OPEN message */
1012 static int bgp_send_open(struct bgp_peer
*peer
)
1014 struct bgp_data_open data
;
1015 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1017 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1018 sizeof(peer
->outbuf
->packet
.header
.marker
));
1020 peer
->outbuf
->packet
.header
.type
= BGP_MSG_OPEN
;
1022 data
.version
= BGP_VERSION
;
1023 data
.as
= htons(our_as
);
1024 data
.hold_time
= htons(peer
->hold
);
1025 data
.identifier
= my_address
;
1028 memcpy(peer
->outbuf
->packet
.data
, &data
, BGP_DATA_OPEN_SIZE
);
1029 len
+= BGP_DATA_OPEN_SIZE
;
1031 peer
->outbuf
->packet
.header
.len
= htons(len
);
1032 peer
->outbuf
->done
= 0;
1033 peer
->next_state
= OpenSent
;
1035 return bgp_write(peer
);
1038 /* send/buffer KEEPALIVE message */
1039 static int bgp_send_keepalive(struct bgp_peer
*peer
)
1041 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1042 sizeof(peer
->outbuf
->packet
.header
.marker
));
1044 peer
->outbuf
->packet
.header
.type
= BGP_MSG_KEEPALIVE
;
1045 peer
->outbuf
->packet
.header
.len
=
1046 htons(sizeof(peer
->outbuf
->packet
.header
));
1048 peer
->outbuf
->done
= 0;
1049 peer
->next_state
= (peer
->state
== OpenSent
) ? OpenConfirm
: peer
->state
;
1051 return bgp_write(peer
);
1054 /* send/buffer UPDATE message */
1055 static int bgp_send_update(struct bgp_peer
*peer
)
1057 uint16_t unf_len
= 0;
1059 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1060 struct bgp_route_list
*have
= peer
->routes
;
1061 struct bgp_route_list
*want
= peer
->routing
? bgp_routes
: 0;
1062 struct bgp_route_list
*e
= 0;
1063 struct bgp_route_list
*add
= 0;
1066 char *data
= (char *) &peer
->outbuf
->packet
.data
;
1068 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1069 char *max
= (char *) &peer
->outbuf
->packet
.data
1070 + sizeof(peer
->outbuf
->packet
.data
)
1071 - sizeof(attr_len
) - peer
->path_attr_len
- sizeof(struct bgp_ip_prefix
);
1073 /* skip over unf_len */
1074 data
+= sizeof(unf_len
);
1075 len
+= sizeof(unf_len
);
1077 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1078 sizeof(peer
->outbuf
->packet
.header
.marker
));
1080 peer
->outbuf
->packet
.header
.type
= BGP_MSG_UPDATE
;
1082 peer
->update_routes
= 0; /* tentatively clear */
1084 /* find differences */
1085 while ((have
|| want
) && data
< (max
- sizeof(struct bgp_ip_prefix
)))
1089 ? memcmp(&have
->dest
, &want
->dest
, sizeof(have
->dest
))
1094 if (s
< 0) /* found one to delete */
1096 struct bgp_route_list
*tmp
= have
;
1099 s
= BGP_IP_PREFIX_SIZE(tmp
->dest
);
1100 memcpy(data
, &tmp
->dest
, s
);
1105 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1106 fmtaddr(tmp
->dest
.prefix
, 0), tmp
->dest
.len
, peer
->name
);
1113 peer
->routes
= have
;
1119 e
= have
; /* stash the last found to relink above */
1123 else if (s
> 0) /* addition reqd. */
1127 peer
->update_routes
= 1; /* only one add per packet */
1141 peer
->update_routes
= 1; /* more to do */
1143 /* anything changed? */
1144 if (!(unf_len
|| add
))
1147 /* go back and insert unf_len */
1148 unf_len
= htons(unf_len
);
1149 memcpy(&peer
->outbuf
->packet
.data
, &unf_len
, sizeof(unf_len
));
1153 if (!(e
= malloc(sizeof(*e
))))
1155 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1156 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, strerror(errno
));
1161 memcpy(e
, add
, sizeof(*e
));
1163 peer
->routes
= bgp_insert_route(peer
->routes
, e
);
1165 attr_len
= htons(peer
->path_attr_len
);
1166 memcpy(data
, &attr_len
, sizeof(attr_len
));
1167 data
+= sizeof(attr_len
);
1168 len
+= sizeof(attr_len
);
1170 memcpy(data
, peer
->path_attrs
, peer
->path_attr_len
);
1171 data
+= peer
->path_attr_len
;
1172 len
+= peer
->path_attr_len
;
1174 s
= BGP_IP_PREFIX_SIZE(add
->dest
);
1175 memcpy(data
, &add
->dest
, s
);
1179 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1180 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, peer
->name
);
1185 memcpy(data
, &attr_len
, sizeof(attr_len
));
1186 data
+= sizeof(attr_len
);
1187 len
+= sizeof(attr_len
);
1190 peer
->outbuf
->packet
.header
.len
= htons(len
);
1191 peer
->outbuf
->done
= 0;
1193 return bgp_write(peer
);
1196 /* send/buffer NOTIFICATION message */
1197 static int bgp_send_notification(struct bgp_peer
*peer
, uint8_t code
,
1200 struct bgp_data_notification data
;
1203 data
.error_code
= code
;
1204 len
+= sizeof(data
.error_code
);
1206 data
.error_subcode
= subcode
;
1207 len
+= sizeof(data
.error_code
);
1209 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1210 sizeof(peer
->outbuf
->packet
.header
.marker
));
1212 peer
->outbuf
->packet
.header
.type
= BGP_MSG_NOTIFICATION
;
1213 peer
->outbuf
->packet
.header
.len
=
1214 htons(sizeof(peer
->outbuf
->packet
.header
) + len
);
1216 memcpy(peer
->outbuf
->packet
.data
, &data
, len
);
1218 peer
->outbuf
->done
= 0;
1219 peer
->next_state
= code
== BGP_ERR_CEASE
? Disabled
: Idle
;
1221 /* we're dying; ignore any pending input */
1222 peer
->inbuf
->packet
.header
.len
= 0;
1223 peer
->inbuf
->done
= 0;
1225 return bgp_write(peer
);