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 struct bgp_route_list
*bgp_insert_route(struct bgp_route_list
*head
,
33 struct bgp_route_list
*new);
35 static void bgp_free_routes(struct bgp_route_list
*routes
);
36 static char const *bgp_msg_type_str(uint8_t type
);
37 static int bgp_connect(struct bgp_peer
*peer
);
38 static int bgp_handle_connect(struct bgp_peer
*peer
);
39 static int bgp_write(struct bgp_peer
*peer
);
40 static int bgp_read(struct bgp_peer
*peer
);
41 static int bgp_handle_input(struct bgp_peer
*peer
);
42 static int bgp_send_open(struct bgp_peer
*peer
);
43 static int bgp_send_keepalive(struct bgp_peer
*peer
);
44 static int bgp_send_update(struct bgp_peer
*peer
);
45 static int bgp_send_notification(struct bgp_peer
*peer
, uint8_t code
,
48 static uint16_t our_as
;
49 static struct bgp_route_list
*bgp_routes
= 0;
51 int bgp_configured
= 0;
52 struct bgp_peer
*bgp_peers
= 0;
54 /* prepare peer structure, globals */
58 struct bgp_peer
*peer
;
60 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
63 memset(peer
, 0, sizeof(*peer
));
65 peer
->addr
= INADDR_NONE
;
67 peer
->state
= peer
->next_state
= Disabled
;
69 if (!((peer
->outbuf
= malloc(sizeof(*peer
->outbuf
)))
70 && (peer
->inbuf
= malloc(sizeof(*peer
->inbuf
)))))
72 LOG(0, 0, 0, "Can't allocate buffers for bgp peer (%s)\n",
78 peer
->edata
.type
= FD_TYPE_BGP
;
79 peer
->edata
.index
= i
;
90 bgp_configured
= 0; /* set by bgp_start */
95 /* start connection with a peer */
96 int bgp_start(struct bgp_peer
*peer
, char *name
, int as
, int keepalive
,
102 struct bgp_path_attr a
;
104 char *p
= path_attrs
;
106 uint32_t metric
= htonl(BGP_METRIC
);
107 uint32_t no_export
= htonl(BGP_COMMUNITY_NO_EXPORT
);
112 if (peer
->state
!= Disabled
)
115 snprintf(peer
->name
, sizeof(peer
->name
), "%s", name
);
117 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
119 LOG(0, 0, 0, "Can't get address for BGP peer %s (%s)\n",
120 name
, h
? "no address" : hstrerror(h_errno
));
125 memcpy(&peer
->addr
, h
->h_addr
, sizeof(peer
->addr
));
126 peer
->as
= as
> 0 ? as
: our_as
;
127 ibgp
= peer
->as
== our_as
;
129 /* set initial timer values */
130 peer
->init_keepalive
= keepalive
== -1 ? BGP_KEEPALIVE_TIME
: keepalive
;
131 peer
->init_hold
= hold
== -1 ? BGP_HOLD_TIME
: hold
;
133 if (peer
->init_hold
< 3)
136 if (peer
->init_keepalive
* 3 > peer
->init_hold
)
137 peer
->init_keepalive
= peer
->init_hold
/ 3;
139 /* clear buffers, go to Idle state */
140 peer
->next_state
= Idle
;
143 /* set initial routing state */
144 peer
->routing
= enable
;
146 /* all our routes use the same attributes, so prepare it in advance */
147 if (peer
->path_attrs
)
148 free(peer
->path_attrs
);
150 peer
->path_attr_len
= 0;
153 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
154 a
.code
= BGP_PATH_ATTR_CODE_ORIGIN
;
156 a
.data
.s
.value
[0] = BGP_PATH_ATTR_CODE_ORIGIN_IGP
;
158 #define ADD_ATTRIBUTE() do { \
159 i = BGP_PATH_ATTR_SIZE(a); \
162 peer->path_attr_len += i; } while (0)
167 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
168 a
.code
= BGP_PATH_ATTR_CODE_AS_PATH
;
182 BGP_PATH_ATTR_CODE_AS_PATH_AS_SEQUENCE
,
187 a
.data
.s
.len
= sizeof(as_path
);
188 memcpy(&a
.data
.s
.value
, &as_path
, sizeof(as_path
));
194 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
195 a
.code
= BGP_PATH_ATTR_CODE_NEXT_HOP
;
196 ip
= my_address
; /* we're it */
197 a
.data
.s
.len
= sizeof(ip
);
198 memcpy(a
.data
.s
.value
, &ip
, sizeof(ip
));
202 /* MULTI_EXIT_DISC */
203 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
;
204 a
.code
= BGP_PATH_ATTR_CODE_MULTI_EXIT_DISC
;
205 a
.data
.s
.len
= sizeof(metric
);
206 memcpy(a
.data
.s
.value
, &metric
, sizeof(metric
));
212 uint32_t local_pref
= htonl(BGP_LOCAL_PREF
);
215 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
216 a
.code
= BGP_PATH_ATTR_CODE_LOCAL_PREF
;
217 a
.data
.s
.len
= sizeof(local_pref
);
218 memcpy(a
.data
.s
.value
, &local_pref
, sizeof(local_pref
));
224 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
| BGP_PATH_ATTR_FLAG_TRANS
;
225 a
.code
= BGP_PATH_ATTR_CODE_COMMUNITIES
;
226 a
.data
.s
.len
= sizeof(no_export
);
227 memcpy(a
.data
.s
.value
, &no_export
, sizeof(no_export
));
231 if (!(peer
->path_attrs
= malloc(peer
->path_attr_len
)))
233 LOG(0, 0, 0, "Can't allocate path_attrs for %s (%s)\n",
234 name
, strerror(errno
));
239 memcpy(peer
->path_attrs
, path_attrs
, peer
->path_attr_len
);
241 LOG(4, 0, 0, "Initiating BGP connection to %s (routing %s)\n",
242 name
, enable
? "enabled" : "suspended");
244 /* we have at least one peer configured */
248 return bgp_connect(peer
);
251 /* clear counters, timers, routes and buffers; close socket; move to
252 next_state, which may be Disabled or Idle */
253 static void bgp_clear(struct bgp_peer
*peer
)
255 if (peer
->sock
!= -1)
261 peer
->keepalive_time
= 0;
262 peer
->expire_time
= 0;
264 peer
->keepalive
= peer
->init_keepalive
;
265 peer
->hold
= peer
->init_hold
;
267 bgp_free_routes(peer
->routes
);
270 peer
->outbuf
->packet
.header
.len
= 0;
271 peer
->outbuf
->done
= 0;
272 peer
->inbuf
->packet
.header
.len
= 0;
273 peer
->inbuf
->done
= 0;
278 if (peer
->state
!= peer
->next_state
)
280 peer
->state
= peer
->next_state
;
281 peer
->state_time
= time_now
;
283 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
284 bgp_state_str(peer
->next_state
));
288 /* initiate a clean shutdown */
289 void bgp_stop(struct bgp_peer
*peer
)
291 LOG(4, 0, 0, "Terminating BGP connection to %s\n", peer
->name
);
292 bgp_send_notification(peer
, BGP_ERR_CEASE
, 0);
295 /* drop connection (if any) and set state to Disabled */
296 void bgp_halt(struct bgp_peer
*peer
)
298 LOG(4, 0, 0, "Aborting BGP connection to %s\n", peer
->name
);
299 peer
->next_state
= Disabled
;
303 /* drop connection (if any) and set to Idle for connection retry */
304 int bgp_restart(struct bgp_peer
*peer
)
306 peer
->next_state
= Idle
;
310 peer
->retry_time
= time_now
;
311 peer
->retry_count
= 0;
314 return bgp_connect(peer
);
317 static void bgp_set_retry(struct bgp_peer
*peer
)
319 if (peer
->retry_count
++ < BGP_MAX_RETRY
)
321 peer
->retry_time
= time_now
+ (BGP_RETRY_BACKOFF
* peer
->retry_count
);
322 peer
->next_state
= Idle
;
326 bgp_halt(peer
); /* give up */
329 /* insert route into list; sorted */
330 static struct bgp_route_list
*bgp_insert_route(struct bgp_route_list
*head
,
331 struct bgp_route_list
*new)
333 struct bgp_route_list
*p
= head
;
334 struct bgp_route_list
*e
= 0;
336 while (p
&& memcmp(&p
->dest
, &new->dest
, sizeof(p
->dest
)) < 0)
356 /* add route to list for peers */
358 * Note: this doesn't do route aggregation, nor drop routes if a less
359 * specific match already exists (partly because I'm lazy, but also so
360 * that if that route is later deleted we don't have to be concerned
361 * about adding back the more specific one).
363 int bgp_add_route(in_addr_t ip
, int prefixlen
)
365 struct bgp_route_list
*r
= bgp_routes
;
366 struct bgp_route_list add
;
369 add
.dest
.prefix
= ip
;
370 add
.dest
.len
= prefixlen
;
373 /* check for duplicate */
376 i
= memcmp(&r
->dest
, &add
.dest
, sizeof(r
->dest
));
378 return 1; /* already covered */
386 /* insert into route list; sorted */
387 if (!(r
= malloc(sizeof(*r
))))
389 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
390 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
, strerror(errno
));
395 memcpy(r
, &add
, sizeof(*r
));
396 bgp_routes
= bgp_insert_route(bgp_routes
, r
);
398 /* flag established peers for update */
399 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
400 if (bgp_peers
[i
].state
== Established
)
401 bgp_peers
[i
].update_routes
= 1;
403 LOG(4, 0, 0, "Registered BGP route %s/%d\n",
404 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
);
409 /* remove route from list for peers */
410 int bgp_del_route(in_addr_t ip
, int prefixlen
)
412 struct bgp_route_list
*r
= bgp_routes
;
413 struct bgp_route_list
*e
= 0;
414 struct bgp_route_list del
;
417 del
.dest
.prefix
= ip
;
418 del
.dest
.len
= prefixlen
;
421 /* find entry in routes list and remove */
424 i
= memcmp(&r
->dest
, &del
.dest
, sizeof(r
->dest
));
430 bgp_routes
= r
->next
;
448 /* flag established peers for update */
449 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
450 if (bgp_peers
[i
].state
== Established
)
451 bgp_peers
[i
].update_routes
= 1;
453 LOG(4, 0, 0, "Removed BGP route %s/%d\n",
454 fmtaddr(del
.dest
.prefix
, 0), del
.dest
.len
);
459 /* enable or disable routing */
460 void bgp_enable_routing(int enable
)
464 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
466 bgp_peers
[i
].routing
= enable
;
468 /* flag established peers for update */
469 if (bgp_peers
[i
].state
== Established
)
470 bgp_peers
[i
].update_routes
= 1;
473 LOG(4, 0, 0, "%s BGP routing\n", enable
? "Enabled" : "Suspended");
477 # include <sys/epoll.h>
479 # include "fake_epoll.h"
482 /* return a bitmask of the events required to poll this peer's fd */
490 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
492 struct bgp_peer
*peer
= &bgp_peers
[i
];
495 if (peer
->state
== Disabled
|| peer
->state
== Idle
)
498 if (peer
->inbuf
->done
< BGP_MAX_PACKET_SIZE
)
501 if (peer
->state
== Connect
|| /* connection in progress */
502 peer
->update_routes
|| /* routing updates */
503 peer
->outbuf
->packet
.header
.len
) /* pending output */
506 if (peer
->events
!= events
)
508 struct epoll_event ev
;
510 ev
.events
= peer
->events
= events
;
511 ev
.data
.ptr
= &peer
->edata
;
512 epoll_ctl(epollfd
, EPOLL_CTL_MOD
, peer
->sock
, &ev
);
519 /* process bgp events/timers */
520 int bgp_process(uint32_t events
[])
527 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
529 struct bgp_peer
*peer
= &bgp_peers
[i
];
531 if (*peer
->name
&& peer
->cli_flag
== BGP_CLI_RESTART
)
537 if (peer
->state
== Disabled
)
542 switch (peer
->cli_flag
)
544 case BGP_CLI_SUSPEND
:
548 if (peer
->state
== Established
)
549 peer
->update_routes
= 1;
558 if (peer
->state
== Established
)
559 peer
->update_routes
= 1;
568 /* handle empty/fill of buffers */
569 if (events
[i
] & EPOLLOUT
)
572 if (peer
->state
== Connect
)
573 r
= bgp_handle_connect(peer
);
574 else if (peer
->outbuf
->packet
.header
.len
)
581 if (events
[i
] & (EPOLLIN
|EPOLLHUP
))
587 /* process input buffer contents */
588 while (peer
->inbuf
->done
>= sizeof(peer
->inbuf
->packet
.header
)
589 && !peer
->outbuf
->packet
.header
.len
) /* may need to queue a response */
591 if (bgp_handle_input(peer
) < 0)
595 /* process pending updates */
596 if (peer
->update_routes
597 && !peer
->outbuf
->packet
.header
.len
) /* ditto */
599 if (!bgp_send_update(peer
))
604 if (peer
->state
== Established
)
606 if (time_now
> peer
->expire_time
)
608 LOG(1, 0, 0, "No message from BGP peer %s in %ds\n",
609 peer
->name
, peer
->hold
);
611 bgp_send_notification(peer
, BGP_ERR_HOLD_TIMER_EXP
, 0);
615 if (time_now
> peer
->keepalive_time
&& !peer
->outbuf
->packet
.header
.len
)
616 bgp_send_keepalive(peer
);
618 else if (peer
->state
== Idle
)
620 if (time_now
> peer
->retry_time
)
623 else if (time_now
> peer
->state_time
+ BGP_STATE_TIME
)
625 LOG(1, 0, 0, "%s timer expired for BGP peer %s\n",
626 bgp_state_str(peer
->state
), peer
->name
);
635 static void bgp_free_routes(struct bgp_route_list
*routes
)
637 struct bgp_route_list
*tmp
;
639 while ((tmp
= routes
))
646 char const *bgp_state_str(enum bgp_state state
)
650 case Disabled
: return "Disabled";
651 case Idle
: return "Idle";
652 case Connect
: return "Connect";
653 case Active
: return "Active";
654 case OpenSent
: return "OpenSent";
655 case OpenConfirm
: return "OpenConfirm";
656 case Established
: return "Established";
662 static char const *bgp_msg_type_str(uint8_t type
)
666 case BGP_MSG_OPEN
: return "OPEN";
667 case BGP_MSG_UPDATE
: return "UPDATE";
668 case BGP_MSG_NOTIFICATION
: return "NOTIFICATION";
669 case BGP_MSG_KEEPALIVE
: return "KEEPALIVE";
675 /* attempt to connect to peer */
676 static int bgp_connect(struct bgp_peer
*peer
)
678 static int bgp_port
= 0;
679 struct sockaddr_in addr
;
680 struct epoll_event ev
;
684 struct servent
*serv
;
685 if (!(serv
= getservbyname("bgp", "tcp")))
687 LOG(0, 0, 0, "Can't get bgp service (%s)\n", strerror(errno
));
691 bgp_port
= serv
->s_port
;
694 if ((peer
->sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
696 LOG(0, 0, 0, "Can't create a socket for BGP peer %s (%s)\n",
697 peer
->name
, strerror(errno
));
699 peer
->state
= peer
->next_state
= Disabled
;
703 /* add to poll set */
704 ev
.events
= peer
->events
= EPOLLOUT
;
705 ev
.data
.ptr
= &peer
->edata
;
706 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, peer
->sock
, &ev
);
708 /* set to non-blocking */
709 fcntl(peer
->sock
, F_SETFL
, fcntl(peer
->sock
, F_GETFL
, 0) | O_NONBLOCK
);
712 memset(&addr
, 0, sizeof(addr
));
713 addr
.sin_family
= AF_INET
;
714 addr
.sin_port
= bgp_port
;
715 addr
.sin_addr
.s_addr
= peer
->addr
;
717 while (connect(peer
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1)
719 if (errno
== EINTR
) /* SIGALARM handler */
722 if (errno
!= EINPROGRESS
)
724 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n",
725 inet_ntoa(addr
.sin_addr
), strerror(errno
));
731 peer
->state
= Connect
;
732 peer
->state_time
= time_now
;
734 LOG(4, 0, 0, "BGP peer %s: state Connect\n", peer
->name
);
738 peer
->state
= Active
;
739 peer
->state_time
= time_now
;
740 peer
->retry_time
= peer
->retry_count
= 0;
742 LOG(4, 0, 0, "BGP peer %s: state Active\n", inet_ntoa(addr
.sin_addr
));
744 return bgp_send_open(peer
);
747 /* complete partial connection (state = Connect) */
748 static int bgp_handle_connect(struct bgp_peer
*peer
)
751 socklen_t len
= sizeof(int);
752 getsockopt(peer
->sock
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
755 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer
->name
,
762 peer
->state
= Active
;
763 peer
->state_time
= time_now
;
765 LOG(4, 0, 0, "BGP peer %s: state Active\n", peer
->name
);
767 return bgp_send_open(peer
);
770 /* initiate a write */
771 static int bgp_write(struct bgp_peer
*peer
)
773 int len
= htons(peer
->outbuf
->packet
.header
.len
);
776 while ((r
= write(peer
->sock
, &peer
->outbuf
->packet
+ peer
->outbuf
->done
,
777 len
- peer
->outbuf
->done
)) == -1)
786 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
788 LOG(1, 0, 0, "Can't write to BGP peer %s (%s)\n", peer
->name
,
797 peer
->outbuf
->done
+= r
;
801 LOG(4, 0, 0, "Sent %s to BGP peer %s\n",
802 bgp_msg_type_str(peer
->outbuf
->packet
.header
.type
), peer
->name
);
804 peer
->outbuf
->packet
.header
.len
= 0;
805 peer
->outbuf
->done
= 0;
807 if (peer
->state
== Established
)
808 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
810 if (peer
->state
!= peer
->next_state
)
812 if (peer
->next_state
== Disabled
|| peer
->next_state
== Idle
)
818 peer
->state
= peer
->next_state
;
819 peer
->state_time
= time_now
;
821 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
822 bgp_state_str(peer
->state
));
828 /* initiate a read */
829 static int bgp_read(struct bgp_peer
*peer
)
833 while ((r
= read(peer
->sock
, &peer
->inbuf
->packet
+ peer
->inbuf
->done
,
834 BGP_MAX_PACKET_SIZE
- peer
->inbuf
->done
)) < 1)
838 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
848 LOG(1, 0, 0, "Can't read from BGP peer %s (%s)\n", peer
->name
,
856 peer
->inbuf
->done
+= r
;
860 /* process buffered packets */
861 static int bgp_handle_input(struct bgp_peer
*peer
)
863 struct bgp_packet
*p
= &peer
->inbuf
->packet
;
864 int len
= ntohs(p
->header
.len
);
866 if (len
> BGP_MAX_PACKET_SIZE
)
868 LOG(1, 0, 0, "Bad header length from BGP %s\n", peer
->name
);
869 bgp_send_notification(peer
, BGP_ERR_HEADER
, BGP_ERR_HDR_BAD_LEN
);
873 if (peer
->inbuf
->done
< len
)
876 LOG(4, 0, 0, "Received %s from BGP peer %s\n",
877 bgp_msg_type_str(p
->header
.type
), peer
->name
);
879 switch (p
->header
.type
)
883 struct bgp_data_open data
;
887 for (i
= 0; i
< sizeof(p
->header
.marker
); i
++)
889 if ((unsigned char) p
->header
.marker
[i
] != 0xff)
891 LOG(1, 0, 0, "Invalid marker from BGP peer %s\n",
894 bgp_send_notification(peer
, BGP_ERR_HEADER
,
895 BGP_ERR_HDR_NOT_SYNC
);
901 if (peer
->state
!= OpenSent
)
903 LOG(1, 0, 0, "OPEN from BGP peer %s in %s state\n",
904 peer
->name
, bgp_state_str(peer
->state
));
906 bgp_send_notification(peer
, BGP_ERR_FSM
, 0);
910 memcpy(&data
, p
->data
, len
- sizeof(p
->header
));
912 if (data
.version
!= BGP_VERSION
)
914 LOG(1, 0, 0, "Bad version (%d) sent by BGP peer %s\n",
915 (int) data
.version
, peer
->name
);
917 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_VERSION
);
921 if (ntohs(data
.as
) != peer
->as
)
923 LOG(1, 0, 0, "Bad AS sent by BGP peer %s (got %d, "
924 "expected %d)\n", peer
->name
, (int) htons(data
.as
),
927 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_BAD_AS
);
931 if ((hold
= ntohs(data
.hold_time
)) < 3)
933 LOG(1, 0, 0, "Bad hold time (%d) from BGP peer %s\n",
936 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_HOLD_TIME
);
940 /* pick lowest hold time */
941 if (hold
< peer
->hold
)
944 /* adjust our keepalive based on negotiated hold value */
945 if (peer
->keepalive
* 3 > peer
->hold
)
946 peer
->keepalive
= peer
->hold
/ 3;
948 /* next transition requires an exchange of keepalives */
949 bgp_send_keepalive(peer
);
951 /* FIXME: may need to check for optional params */
956 case BGP_MSG_KEEPALIVE
:
957 if (peer
->state
== OpenConfirm
)
959 peer
->state
= peer
->next_state
= Established
;
960 peer
->state_time
= time_now
;
961 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
962 peer
->update_routes
= 1;
963 peer
->retry_count
= 0;
964 peer
->retry_time
= 0;
966 LOG(4, 0, 0, "BGP peer %s: state Established\n", peer
->name
);
971 case BGP_MSG_NOTIFICATION
:
972 if (len
> sizeof(p
->header
))
974 struct bgp_data_notification
*notification
=
975 (struct bgp_data_notification
*) p
->data
;
977 if (notification
->error_code
== BGP_ERR_CEASE
)
979 LOG(4, 0, 0, "BGP peer %s sent CEASE\n", peer
->name
);
984 /* FIXME: should handle more notifications */
985 LOG(4, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n",
986 peer
->name
, (int) notification
->error_code
);
993 peer
->expire_time
= time_now
+ peer
->hold
;
995 /* see if there's another message in the same packet/buffer */
996 if (peer
->inbuf
->done
> len
)
998 peer
->inbuf
->done
-= len
;
999 memmove(p
, (char *) p
+ len
, peer
->inbuf
->done
);
1003 peer
->inbuf
->packet
.header
.len
= 0;
1004 peer
->inbuf
->done
= 0;
1007 return peer
->inbuf
->done
;
1010 /* send/buffer OPEN message */
1011 static int bgp_send_open(struct bgp_peer
*peer
)
1013 struct bgp_data_open data
;
1014 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1016 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1017 sizeof(peer
->outbuf
->packet
.header
.marker
));
1019 peer
->outbuf
->packet
.header
.type
= BGP_MSG_OPEN
;
1021 data
.version
= BGP_VERSION
;
1022 data
.as
= htons(our_as
);
1023 data
.hold_time
= htons(peer
->hold
);
1024 data
.identifier
= my_address
;
1027 memcpy(peer
->outbuf
->packet
.data
, &data
, BGP_DATA_OPEN_SIZE
);
1028 len
+= BGP_DATA_OPEN_SIZE
;
1030 peer
->outbuf
->packet
.header
.len
= htons(len
);
1031 peer
->outbuf
->done
= 0;
1032 peer
->next_state
= OpenSent
;
1034 return bgp_write(peer
);
1037 /* send/buffer KEEPALIVE message */
1038 static int bgp_send_keepalive(struct bgp_peer
*peer
)
1040 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1041 sizeof(peer
->outbuf
->packet
.header
.marker
));
1043 peer
->outbuf
->packet
.header
.type
= BGP_MSG_KEEPALIVE
;
1044 peer
->outbuf
->packet
.header
.len
=
1045 htons(sizeof(peer
->outbuf
->packet
.header
));
1047 peer
->outbuf
->done
= 0;
1048 peer
->next_state
= (peer
->state
== OpenSent
) ? OpenConfirm
: peer
->state
;
1050 return bgp_write(peer
);
1053 /* send/buffer UPDATE message */
1054 static int bgp_send_update(struct bgp_peer
*peer
)
1056 uint16_t unf_len
= 0;
1058 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1059 struct bgp_route_list
*have
= peer
->routes
;
1060 struct bgp_route_list
*want
= peer
->routing
? bgp_routes
: 0;
1061 struct bgp_route_list
*e
= 0;
1062 struct bgp_route_list
*add
= 0;
1065 char *data
= (char *) &peer
->outbuf
->packet
.data
;
1067 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1068 char *max
= (char *) &peer
->outbuf
->packet
.data
1069 + sizeof(peer
->outbuf
->packet
.data
)
1070 - sizeof(attr_len
) - peer
->path_attr_len
- sizeof(struct bgp_ip_prefix
);
1072 /* skip over unf_len */
1073 data
+= sizeof(unf_len
);
1074 len
+= sizeof(unf_len
);
1076 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1077 sizeof(peer
->outbuf
->packet
.header
.marker
));
1079 peer
->outbuf
->packet
.header
.type
= BGP_MSG_UPDATE
;
1081 peer
->update_routes
= 0; /* tentatively clear */
1083 /* find differences */
1084 while ((have
|| want
) && data
< (max
- sizeof(struct bgp_ip_prefix
)))
1088 ? memcmp(&have
->dest
, &want
->dest
, sizeof(have
->dest
))
1093 if (s
< 0) /* found one to delete */
1095 struct bgp_route_list
*tmp
= have
;
1098 s
= BGP_IP_PREFIX_SIZE(tmp
->dest
);
1099 memcpy(data
, &tmp
->dest
, s
);
1104 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1105 fmtaddr(tmp
->dest
.prefix
, 0), tmp
->dest
.len
, peer
->name
);
1112 peer
->routes
= have
;
1118 e
= have
; /* stash the last found to relink above */
1122 else if (s
> 0) /* addition reqd. */
1126 peer
->update_routes
= 1; /* only one add per packet */
1140 peer
->update_routes
= 1; /* more to do */
1142 /* anything changed? */
1143 if (!(unf_len
|| add
))
1146 /* go back and insert unf_len */
1147 unf_len
= htons(unf_len
);
1148 memcpy(&peer
->outbuf
->packet
.data
, &unf_len
, sizeof(unf_len
));
1152 if (!(e
= malloc(sizeof(*e
))))
1154 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1155 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, strerror(errno
));
1160 memcpy(e
, add
, sizeof(*e
));
1162 peer
->routes
= bgp_insert_route(peer
->routes
, e
);
1164 attr_len
= htons(peer
->path_attr_len
);
1165 memcpy(data
, &attr_len
, sizeof(attr_len
));
1166 data
+= sizeof(attr_len
);
1167 len
+= sizeof(attr_len
);
1169 memcpy(data
, peer
->path_attrs
, peer
->path_attr_len
);
1170 data
+= peer
->path_attr_len
;
1171 len
+= peer
->path_attr_len
;
1173 s
= BGP_IP_PREFIX_SIZE(add
->dest
);
1174 memcpy(data
, &add
->dest
, s
);
1178 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1179 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, peer
->name
);
1184 memcpy(data
, &attr_len
, sizeof(attr_len
));
1185 data
+= sizeof(attr_len
);
1186 len
+= sizeof(attr_len
);
1189 peer
->outbuf
->packet
.header
.len
= htons(len
);
1190 peer
->outbuf
->done
= 0;
1192 return bgp_write(peer
);
1195 /* send/buffer NOTIFICATION message */
1196 static int bgp_send_notification(struct bgp_peer
*peer
, uint8_t code
,
1199 struct bgp_data_notification data
;
1202 data
.error_code
= code
;
1203 len
+= sizeof(data
.error_code
);
1205 data
.error_subcode
= subcode
;
1206 len
+= sizeof(data
.error_code
);
1208 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1209 sizeof(peer
->outbuf
->packet
.header
.marker
));
1211 peer
->outbuf
->packet
.header
.type
= BGP_MSG_NOTIFICATION
;
1212 peer
->outbuf
->packet
.header
.len
=
1213 htons(sizeof(peer
->outbuf
->packet
.header
) + len
);
1215 memcpy(peer
->outbuf
->packet
.data
, &data
, len
);
1217 peer
->outbuf
->done
= 0;
1218 peer
->next_state
= code
== BGP_ERR_CEASE
? Disabled
: Idle
;
1220 /* we're dying; ignore any pending input */
1221 peer
->inbuf
->packet
.header
.len
= 0;
1222 peer
->inbuf
->done
= 0;
1224 return bgp_write(peer
);