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.10 2005/06/04 15:42:35 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 int 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
;
906 for (i
= 0; i
< sizeof(p
->header
.marker
); i
++)
908 if ((unsigned char) p
->header
.marker
[i
] != 0xff)
910 LOG(1, 0, 0, "Invalid marker from BGP peer %s\n",
913 bgp_send_notification(peer
, BGP_ERR_HEADER
,
914 BGP_ERR_HDR_NOT_SYNC
);
920 if (peer
->state
!= OpenSent
)
922 LOG(1, 0, 0, "OPEN from BGP peer %s in %s state\n",
923 peer
->name
, bgp_state_str(peer
->state
));
925 bgp_send_notification(peer
, BGP_ERR_FSM
, 0);
929 memcpy(&data
, p
->data
, len
- sizeof(p
->header
));
931 if (data
.version
!= BGP_VERSION
)
933 LOG(1, 0, 0, "Bad version (%d) sent by BGP peer %s\n",
934 (int) data
.version
, peer
->name
);
936 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_VERSION
);
940 if (ntohs(data
.as
) != peer
->as
)
942 LOG(1, 0, 0, "Bad AS sent by BGP peer %s (got %d, "
943 "expected %d)\n", peer
->name
, (int) htons(data
.as
),
946 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_BAD_AS
);
950 if ((hold
= ntohs(data
.hold_time
)) < 3)
952 LOG(1, 0, 0, "Bad hold time (%d) from BGP peer %s\n",
955 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_HOLD_TIME
);
959 /* pick lowest hold time */
960 if (hold
< peer
->hold
)
963 /* adjust our keepalive based on negotiated hold value */
964 if (peer
->keepalive
* 3 > peer
->hold
)
965 peer
->keepalive
= peer
->hold
/ 3;
967 /* next transition requires an exchange of keepalives */
968 bgp_send_keepalive(peer
);
970 /* FIXME: may need to check for optional params */
975 case BGP_MSG_KEEPALIVE
:
976 if (peer
->state
== OpenConfirm
)
978 peer
->state
= peer
->next_state
= Established
;
979 peer
->state_time
= time_now
;
980 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
981 peer
->update_routes
= 1;
982 peer
->retry_count
= 0;
983 peer
->retry_time
= 0;
985 LOG(4, 0, 0, "BGP peer %s: state Established\n", peer
->name
);
990 case BGP_MSG_NOTIFICATION
:
991 if (len
> sizeof(p
->header
))
993 struct bgp_data_notification
*notification
=
994 (struct bgp_data_notification
*) p
->data
;
996 if (notification
->error_code
== BGP_ERR_CEASE
)
998 LOG(4, 0, 0, "BGP peer %s sent CEASE\n", peer
->name
);
1003 /* FIXME: should handle more notifications */
1004 LOG(4, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n",
1005 peer
->name
, (int) notification
->error_code
);
1012 peer
->expire_time
= time_now
+ peer
->hold
;
1014 /* see if there's another message in the same packet/buffer */
1015 if (peer
->inbuf
->done
> len
)
1017 peer
->inbuf
->done
-= len
;
1018 memmove(p
, (char *) p
+ len
, peer
->inbuf
->done
);
1022 peer
->inbuf
->packet
.header
.len
= 0;
1023 peer
->inbuf
->done
= 0;
1026 return peer
->inbuf
->done
;
1029 /* send/buffer OPEN message */
1030 static int bgp_send_open(struct bgp_peer
*peer
)
1032 struct bgp_data_open data
;
1033 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1035 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1036 sizeof(peer
->outbuf
->packet
.header
.marker
));
1038 peer
->outbuf
->packet
.header
.type
= BGP_MSG_OPEN
;
1040 data
.version
= BGP_VERSION
;
1041 data
.as
= htons(our_as
);
1042 data
.hold_time
= htons(peer
->hold
);
1043 data
.identifier
= my_address
;
1046 memcpy(peer
->outbuf
->packet
.data
, &data
, BGP_DATA_OPEN_SIZE
);
1047 len
+= BGP_DATA_OPEN_SIZE
;
1049 peer
->outbuf
->packet
.header
.len
= htons(len
);
1050 peer
->outbuf
->done
= 0;
1051 peer
->next_state
= OpenSent
;
1053 return bgp_write(peer
);
1056 /* send/buffer KEEPALIVE message */
1057 static int bgp_send_keepalive(struct bgp_peer
*peer
)
1059 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1060 sizeof(peer
->outbuf
->packet
.header
.marker
));
1062 peer
->outbuf
->packet
.header
.type
= BGP_MSG_KEEPALIVE
;
1063 peer
->outbuf
->packet
.header
.len
=
1064 htons(sizeof(peer
->outbuf
->packet
.header
));
1066 peer
->outbuf
->done
= 0;
1067 peer
->next_state
= (peer
->state
== OpenSent
) ? OpenConfirm
: peer
->state
;
1069 return bgp_write(peer
);
1072 /* send/buffer UPDATE message */
1073 static int bgp_send_update(struct bgp_peer
*peer
)
1075 uint16_t unf_len
= 0;
1077 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1078 struct bgp_route_list
*have
= peer
->routes
;
1079 struct bgp_route_list
*want
= peer
->routing
? bgp_routes
: 0;
1080 struct bgp_route_list
*e
= 0;
1081 struct bgp_route_list
*add
= 0;
1084 char *data
= (char *) &peer
->outbuf
->packet
.data
;
1086 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1087 char *max
= (char *) &peer
->outbuf
->packet
.data
1088 + sizeof(peer
->outbuf
->packet
.data
)
1089 - sizeof(attr_len
) - peer
->path_attr_len
- sizeof(struct bgp_ip_prefix
);
1091 /* skip over unf_len */
1092 data
+= sizeof(unf_len
);
1093 len
+= sizeof(unf_len
);
1095 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1096 sizeof(peer
->outbuf
->packet
.header
.marker
));
1098 peer
->outbuf
->packet
.header
.type
= BGP_MSG_UPDATE
;
1100 peer
->update_routes
= 0; /* tentatively clear */
1102 /* find differences */
1103 while ((have
|| want
) && data
< (max
- sizeof(struct bgp_ip_prefix
)))
1107 ? memcmp(&have
->dest
, &want
->dest
, sizeof(have
->dest
))
1112 if (s
< 0) /* found one to delete */
1114 struct bgp_route_list
*tmp
= have
;
1117 s
= BGP_IP_PREFIX_SIZE(tmp
->dest
);
1118 memcpy(data
, &tmp
->dest
, s
);
1123 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1124 fmtaddr(tmp
->dest
.prefix
, 0), tmp
->dest
.len
, peer
->name
);
1131 peer
->routes
= have
;
1137 e
= have
; /* stash the last found to relink above */
1141 else if (s
> 0) /* addition reqd. */
1145 peer
->update_routes
= 1; /* only one add per packet */
1159 peer
->update_routes
= 1; /* more to do */
1161 /* anything changed? */
1162 if (!(unf_len
|| add
))
1165 /* go back and insert unf_len */
1166 unf_len
= htons(unf_len
);
1167 memcpy(&peer
->outbuf
->packet
.data
, &unf_len
, sizeof(unf_len
));
1171 if (!(e
= malloc(sizeof(*e
))))
1173 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1174 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, strerror(errno
));
1179 memcpy(e
, add
, sizeof(*e
));
1181 peer
->routes
= bgp_insert_route(peer
->routes
, e
);
1183 attr_len
= htons(peer
->path_attr_len
);
1184 memcpy(data
, &attr_len
, sizeof(attr_len
));
1185 data
+= sizeof(attr_len
);
1186 len
+= sizeof(attr_len
);
1188 memcpy(data
, peer
->path_attrs
, peer
->path_attr_len
);
1189 data
+= peer
->path_attr_len
;
1190 len
+= peer
->path_attr_len
;
1192 s
= BGP_IP_PREFIX_SIZE(add
->dest
);
1193 memcpy(data
, &add
->dest
, s
);
1197 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1198 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, peer
->name
);
1203 memcpy(data
, &attr_len
, sizeof(attr_len
));
1204 data
+= sizeof(attr_len
);
1205 len
+= sizeof(attr_len
);
1208 peer
->outbuf
->packet
.header
.len
= htons(len
);
1209 peer
->outbuf
->done
= 0;
1211 return bgp_write(peer
);
1214 /* send/buffer NOTIFICATION message */
1215 static int bgp_send_notification(struct bgp_peer
*peer
, uint8_t code
,
1218 struct bgp_data_notification data
;
1221 data
.error_code
= code
;
1222 len
+= sizeof(data
.error_code
);
1224 data
.error_subcode
= subcode
;
1225 len
+= sizeof(data
.error_code
);
1227 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1228 sizeof(peer
->outbuf
->packet
.header
.marker
));
1230 peer
->outbuf
->packet
.header
.type
= BGP_MSG_NOTIFICATION
;
1231 peer
->outbuf
->packet
.header
.len
=
1232 htons(sizeof(peer
->outbuf
->packet
.header
) + len
);
1234 memcpy(peer
->outbuf
->packet
.data
, &data
, len
);
1236 peer
->outbuf
->done
= 0;
1237 peer
->next_state
= code
== BGP_ERR_CEASE
? Disabled
: Idle
;
1239 /* we're dying; ignore any pending input */
1240 peer
->inbuf
->packet
.header
.len
= 0;
1241 peer
->inbuf
->done
= 0;
1243 return bgp_write(peer
);