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.9 2004/12/16 08:49:52 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",
87 bgp_configured
= 0; /* set by bgp_start */
92 /* start connection with a peer */
93 int bgp_start(struct bgp_peer
*peer
, char *name
, int as
, int keepalive
,
99 struct bgp_path_attr a
;
101 char *p
= path_attrs
;
103 uint32_t metric
= htonl(BGP_METRIC
);
104 uint32_t no_export
= htonl(BGP_COMMUNITY_NO_EXPORT
);
109 if (peer
->state
!= Disabled
)
112 snprintf(peer
->name
, sizeof(peer
->name
), "%s", name
);
114 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
116 LOG(0, 0, 0, "Can't get address for BGP peer %s (%s)\n",
117 name
, h
? "no address" : hstrerror(h_errno
));
122 memcpy(&peer
->addr
, h
->h_addr
, sizeof(peer
->addr
));
123 peer
->as
= as
> 0 ? as
: our_as
;
124 ibgp
= peer
->as
== our_as
;
126 /* set initial timer values */
127 peer
->init_keepalive
= keepalive
== -1 ? BGP_KEEPALIVE_TIME
: keepalive
;
128 peer
->init_hold
= hold
== -1 ? BGP_HOLD_TIME
: hold
;
130 if (peer
->init_hold
< 3)
133 if (peer
->init_keepalive
* 3 > peer
->init_hold
)
134 peer
->init_keepalive
= peer
->init_hold
/ 3;
136 /* clear buffers, go to Idle state */
137 peer
->next_state
= Idle
;
140 /* set initial routing state */
141 peer
->routing
= enable
;
143 /* all our routes use the same attributes, so prepare it in advance */
144 if (peer
->path_attrs
)
145 free(peer
->path_attrs
);
147 peer
->path_attr_len
= 0;
150 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
151 a
.code
= BGP_PATH_ATTR_CODE_ORIGIN
;
153 a
.data
.s
.value
[0] = BGP_PATH_ATTR_CODE_ORIGIN_IGP
;
155 #define ADD_ATTRIBUTE() do { \
156 i = BGP_PATH_ATTR_SIZE(a); \
159 peer->path_attr_len += i; } while (0)
164 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
165 a
.code
= BGP_PATH_ATTR_CODE_AS_PATH
;
179 BGP_PATH_ATTR_CODE_AS_PATH_AS_SEQUENCE
,
184 a
.data
.s
.len
= sizeof(as_path
);
185 memcpy(&a
.data
.s
.value
, &as_path
, sizeof(as_path
));
191 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
192 a
.code
= BGP_PATH_ATTR_CODE_NEXT_HOP
;
193 ip
= my_address
; /* we're it */
194 a
.data
.s
.len
= sizeof(ip
);
195 memcpy(a
.data
.s
.value
, &ip
, sizeof(ip
));
199 /* MULTI_EXIT_DISC */
200 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
;
201 a
.code
= BGP_PATH_ATTR_CODE_MULTI_EXIT_DISC
;
202 a
.data
.s
.len
= sizeof(metric
);
203 memcpy(a
.data
.s
.value
, &metric
, sizeof(metric
));
209 uint32_t local_pref
= htonl(BGP_LOCAL_PREF
);
212 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
213 a
.code
= BGP_PATH_ATTR_CODE_LOCAL_PREF
;
214 a
.data
.s
.len
= sizeof(local_pref
);
215 memcpy(a
.data
.s
.value
, &local_pref
, sizeof(local_pref
));
221 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
| BGP_PATH_ATTR_FLAG_TRANS
;
222 a
.code
= BGP_PATH_ATTR_CODE_COMMUNITIES
;
223 a
.data
.s
.len
= sizeof(no_export
);
224 memcpy(a
.data
.s
.value
, &no_export
, sizeof(no_export
));
228 if (!(peer
->path_attrs
= malloc(peer
->path_attr_len
)))
230 LOG(0, 0, 0, "Can't allocate path_attrs for %s (%s)\n",
231 name
, strerror(errno
));
236 memcpy(peer
->path_attrs
, path_attrs
, peer
->path_attr_len
);
238 LOG(4, 0, 0, "Initiating BGP connection to %s (routing %s)\n",
239 name
, enable
? "enabled" : "suspended");
241 /* we have at least one peer configured */
245 return bgp_connect(peer
);
248 /* clear counters, timers, routes and buffers; close socket; move to
249 next_state, which may be Disabled or Idle */
250 static void bgp_clear(struct bgp_peer
*peer
)
252 if (peer
->sock
!= -1)
258 peer
->keepalive_time
= 0;
259 peer
->expire_time
= 0;
261 peer
->keepalive
= peer
->init_keepalive
;
262 peer
->hold
= peer
->init_hold
;
264 bgp_free_routes(peer
->routes
);
267 peer
->outbuf
->packet
.header
.len
= 0;
268 peer
->outbuf
->done
= 0;
269 peer
->inbuf
->packet
.header
.len
= 0;
270 peer
->inbuf
->done
= 0;
274 if (peer
->state
!= peer
->next_state
)
276 peer
->state
= peer
->next_state
;
277 peer
->state_time
= time_now
;
279 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
280 bgp_state_str(peer
->next_state
));
284 /* initiate a clean shutdown */
285 void bgp_stop(struct bgp_peer
*peer
)
287 LOG(4, 0, 0, "Terminating BGP connection to %s\n", peer
->name
);
288 bgp_send_notification(peer
, BGP_ERR_CEASE
, 0);
291 /* drop connection (if any) and set state to Disabled */
292 void bgp_halt(struct bgp_peer
*peer
)
294 LOG(4, 0, 0, "Aborting BGP connection to %s\n", peer
->name
);
295 peer
->next_state
= Disabled
;
299 /* drop connection (if any) and set to Idle for connection retry */
300 int bgp_restart(struct bgp_peer
*peer
)
302 peer
->next_state
= Idle
;
306 peer
->retry_time
= time_now
;
307 peer
->retry_count
= 0;
310 return bgp_connect(peer
);
313 static void bgp_set_retry(struct bgp_peer
*peer
)
315 if (peer
->retry_count
++ < BGP_MAX_RETRY
)
317 peer
->retry_time
= time_now
+ (BGP_RETRY_BACKOFF
* peer
->retry_count
);
318 peer
->next_state
= Idle
;
322 bgp_halt(peer
); /* give up */
325 /* convert ip/mask to CIDR notation */
326 static void bgp_cidr(in_addr_t ip
, in_addr_t mask
, struct bgp_ip_prefix
*pfx
)
331 /* convert to prefix notation */
335 if (!mask
) /* bogus */
338 for (i
= 0; i
< 32 && ((b
= ntohl(1 << i
)), !(mask
& b
)); i
++)
345 /* insert route into list; sorted */
346 static struct bgp_route_list
*bgp_insert_route(struct bgp_route_list
*head
,
347 struct bgp_route_list
*new)
349 struct bgp_route_list
*p
= head
;
350 struct bgp_route_list
*e
= 0;
352 while (p
&& memcmp(&p
->dest
, &new->dest
, sizeof(p
->dest
)) < 0)
372 /* add route to list for peers */
374 * Note: this doesn't do route aggregation, nor drop routes if a less
375 * specific match already exists (partly because I'm lazy, but also so
376 * that if that route is later deleted we don't have to be concerned
377 * about adding back the more specific one).
379 int bgp_add_route(in_addr_t ip
, in_addr_t mask
)
381 struct bgp_route_list
*r
= bgp_routes
;
382 struct bgp_route_list add
;
385 bgp_cidr(ip
, mask
, &add
.dest
);
388 /* check for duplicate */
391 i
= memcmp(&r
->dest
, &add
.dest
, sizeof(r
->dest
));
393 return 1; /* already covered */
401 /* insert into route list; sorted */
402 if (!(r
= malloc(sizeof(*r
))))
404 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
405 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
, strerror(errno
));
410 memcpy(r
, &add
, sizeof(*r
));
411 bgp_routes
= bgp_insert_route(bgp_routes
, r
);
413 /* flag established peers for update */
414 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
415 if (bgp_peers
[i
].state
== Established
)
416 bgp_peers
[i
].update_routes
= 1;
418 LOG(4, 0, 0, "Registered BGP route %s/%d\n",
419 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
);
424 /* remove route from list for peers */
425 int bgp_del_route(in_addr_t ip
, in_addr_t mask
)
427 struct bgp_route_list
*r
= bgp_routes
;
428 struct bgp_route_list
*e
= 0;
429 struct bgp_route_list del
;
432 bgp_cidr(ip
, mask
, &del
.dest
);
435 /* find entry in routes list and remove */
438 i
= memcmp(&r
->dest
, &del
.dest
, sizeof(r
->dest
));
444 bgp_routes
= r
->next
;
462 /* flag established peers for update */
463 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
464 if (bgp_peers
[i
].state
== Established
)
465 bgp_peers
[i
].update_routes
= 1;
467 LOG(4, 0, 0, "Removed BGP route %s/%d\n",
468 fmtaddr(del
.dest
.prefix
, 0), del
.dest
.len
);
473 /* enable or disable routing */
474 void bgp_enable_routing(int enable
)
478 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
480 bgp_peers
[i
].routing
= enable
;
482 /* flag established peers for update */
483 if (bgp_peers
[i
].state
== Established
)
484 bgp_peers
[i
].update_routes
= 1;
487 LOG(4, 0, 0, "%s BGP routing\n", enable
? "Enabled" : "Suspended");
490 /* return a bitmask indicating if the socket should be added to the
491 read set (1) and or write set (2) for select */
492 int bgp_select_state(struct bgp_peer
*peer
)
499 if (peer
->state
== Disabled
|| peer
->state
== Idle
)
502 if (peer
->inbuf
->done
< BGP_MAX_PACKET_SIZE
)
505 if (peer
->state
== Connect
|| /* connection in progress */
506 peer
->update_routes
|| /* routing updates */
507 peer
->outbuf
->packet
.header
.len
) /* pending output */
513 /* process bgp peer */
514 int bgp_process(struct bgp_peer
*peer
, int readable
, int writable
)
519 if (*peer
->name
&& peer
->cli_flag
== BGP_CLI_RESTART
)
520 return bgp_restart(peer
);
522 if (peer
->state
== Disabled
)
527 switch (peer
->cli_flag
)
529 case BGP_CLI_SUSPEND
:
533 if (peer
->state
== Established
)
534 peer
->update_routes
= 1;
543 if (peer
->state
== Established
)
544 peer
->update_routes
= 1;
553 /* handle empty/fill of buffers */
557 if (peer
->state
== Connect
)
558 r
= bgp_handle_connect(peer
);
559 else if (peer
->outbuf
->packet
.header
.len
)
572 /* process input buffer contents */
573 while (peer
->inbuf
->done
>= sizeof(peer
->inbuf
->packet
.header
)
574 && !peer
->outbuf
->packet
.header
.len
) /* may need to queue a response */
576 if (bgp_handle_input(peer
) < 0)
580 /* process pending updates */
581 if (peer
->update_routes
582 && !peer
->outbuf
->packet
.header
.len
) /* ditto */
584 if (!bgp_send_update(peer
))
589 if (peer
->state
== Established
)
591 if (time_now
> peer
->expire_time
)
593 LOG(1, 0, 0, "No message from BGP peer %s in %ds\n",
594 peer
->name
, peer
->hold
);
596 bgp_send_notification(peer
, BGP_ERR_HOLD_TIMER_EXP
, 0);
600 if (time_now
> peer
->keepalive_time
&& !peer
->outbuf
->packet
.header
.len
)
601 bgp_send_keepalive(peer
);
603 else if (peer
->state
== Idle
)
605 if (time_now
> peer
->retry_time
)
606 return bgp_connect(peer
);
608 else if (time_now
> peer
->state_time
+ BGP_STATE_TIME
)
610 LOG(1, 0, 0, "%s timer expired for BGP peer %s\n",
611 bgp_state_str(peer
->state
), peer
->name
);
613 return bgp_restart(peer
);
619 static void bgp_free_routes(struct bgp_route_list
*routes
)
621 struct bgp_route_list
*tmp
;
623 while ((tmp
= routes
))
630 char const *bgp_state_str(enum bgp_state state
)
634 case Disabled
: return "Disabled";
635 case Idle
: return "Idle";
636 case Connect
: return "Connect";
637 case Active
: return "Active";
638 case OpenSent
: return "OpenSent";
639 case OpenConfirm
: return "OpenConfirm";
640 case Established
: return "Established";
646 static char const *bgp_msg_type_str(uint8_t type
)
650 case BGP_MSG_OPEN
: return "OPEN";
651 case BGP_MSG_UPDATE
: return "UPDATE";
652 case BGP_MSG_NOTIFICATION
: return "NOTIFICATION";
653 case BGP_MSG_KEEPALIVE
: return "KEEPALIVE";
659 /* attempt to connect to peer */
660 static int bgp_connect(struct bgp_peer
*peer
)
662 static int bgp_port
= 0;
663 struct sockaddr_in addr
;
667 struct servent
*serv
;
668 if (!(serv
= getservbyname("bgp", "tcp")))
670 LOG(0, 0, 0, "Can't get bgp service (%s)\n", strerror(errno
));
674 bgp_port
= serv
->s_port
;
677 if ((peer
->sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
679 LOG(0, 0, 0, "Can't create a socket for BGP peer %s (%s)\n",
680 peer
->name
, strerror(errno
));
682 peer
->state
= peer
->next_state
= Disabled
;
686 /* set to non-blocking */
687 fcntl(peer
->sock
, F_SETFL
, fcntl(peer
->sock
, F_GETFL
, 0) | O_NONBLOCK
);
690 memset(&addr
, 0, sizeof(addr
));
691 addr
.sin_family
= AF_INET
;
692 addr
.sin_port
= bgp_port
;
693 addr
.sin_addr
.s_addr
= peer
->addr
;
695 while (connect(peer
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1)
697 if (errno
== EINTR
) /* SIGALARM handler */
700 if (errno
!= EINPROGRESS
)
702 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n",
703 inet_ntoa(addr
.sin_addr
), strerror(errno
));
709 peer
->state
= Connect
;
710 peer
->state_time
= time_now
;
712 LOG(4, 0, 0, "BGP peer %s: state Connect\n", peer
->name
);
716 peer
->state
= Active
;
717 peer
->state_time
= time_now
;
718 peer
->retry_time
= peer
->retry_count
= 0;
720 LOG(4, 0, 0, "BGP peer %s: state Active\n", inet_ntoa(addr
.sin_addr
));
722 return bgp_send_open(peer
);
725 /* complete partial connection (state = Connect) */
726 static int bgp_handle_connect(struct bgp_peer
*peer
)
729 int len
= sizeof(int);
730 getsockopt(peer
->sock
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
733 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer
->name
,
740 peer
->state
= Active
;
741 peer
->state_time
= time_now
;
743 LOG(4, 0, 0, "BGP peer %s: state Active\n", peer
->name
);
745 return bgp_send_open(peer
);
748 /* initiate a write */
749 static int bgp_write(struct bgp_peer
*peer
)
751 int len
= htons(peer
->outbuf
->packet
.header
.len
);
754 while ((r
= write(peer
->sock
, &peer
->outbuf
->packet
+ peer
->outbuf
->done
,
755 len
- peer
->outbuf
->done
)) == -1)
764 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
766 LOG(1, 0, 0, "Can't write to BGP peer %s (%s)\n", peer
->name
,
775 peer
->outbuf
->done
+= r
;
779 LOG(4, 0, 0, "Sent %s to BGP peer %s\n",
780 bgp_msg_type_str(peer
->outbuf
->packet
.header
.type
), peer
->name
);
782 peer
->outbuf
->packet
.header
.len
= 0;
783 peer
->outbuf
->done
= 0;
785 if (peer
->state
== Established
)
786 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
788 if (peer
->state
!= peer
->next_state
)
790 if (peer
->next_state
== Disabled
|| peer
->next_state
== Idle
)
796 peer
->state
= peer
->next_state
;
797 peer
->state_time
= time_now
;
799 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
800 bgp_state_str(peer
->state
));
806 /* initiate a read */
807 static int bgp_read(struct bgp_peer
*peer
)
811 while ((r
= read(peer
->sock
, &peer
->inbuf
->packet
+ peer
->inbuf
->done
,
812 BGP_MAX_PACKET_SIZE
- peer
->inbuf
->done
)) < 1)
816 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
826 LOG(1, 0, 0, "Can't read from BGP peer %s (%s)\n", peer
->name
,
834 peer
->inbuf
->done
+= r
;
838 /* process buffered packets */
839 static int bgp_handle_input(struct bgp_peer
*peer
)
841 struct bgp_packet
*p
= &peer
->inbuf
->packet
;
842 int len
= ntohs(p
->header
.len
);
844 if (len
> BGP_MAX_PACKET_SIZE
)
846 LOG(1, 0, 0, "Bad header length from BGP %s\n", peer
->name
);
847 bgp_send_notification(peer
, BGP_ERR_HEADER
, BGP_ERR_HDR_BAD_LEN
);
851 if (peer
->inbuf
->done
< len
)
854 LOG(4, 0, 0, "Received %s from BGP peer %s\n",
855 bgp_msg_type_str(p
->header
.type
), peer
->name
);
857 switch (p
->header
.type
)
861 struct bgp_data_open data
;
865 for (i
= 0; i
< sizeof(p
->header
.marker
); i
++)
867 if ((unsigned char) p
->header
.marker
[i
] != 0xff)
869 LOG(1, 0, 0, "Invalid marker from BGP peer %s\n",
872 bgp_send_notification(peer
, BGP_ERR_HEADER
,
873 BGP_ERR_HDR_NOT_SYNC
);
879 if (peer
->state
!= OpenSent
)
881 LOG(1, 0, 0, "OPEN from BGP peer %s in %s state\n",
882 peer
->name
, bgp_state_str(peer
->state
));
884 bgp_send_notification(peer
, BGP_ERR_FSM
, 0);
888 memcpy(&data
, p
->data
, len
- sizeof(p
->header
));
890 if (data
.version
!= BGP_VERSION
)
892 LOG(1, 0, 0, "Bad version (%d) sent by BGP peer %s\n",
893 (int) data
.version
, peer
->name
);
895 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_VERSION
);
899 if (ntohs(data
.as
) != peer
->as
)
901 LOG(1, 0, 0, "Bad AS sent by BGP peer %s (got %d, "
902 "expected %d)\n", peer
->name
, (int) htons(data
.as
),
905 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_BAD_AS
);
909 if ((hold
= ntohs(data
.hold_time
)) < 3)
911 LOG(1, 0, 0, "Bad hold time (%d) from BGP peer %s\n",
914 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_HOLD_TIME
);
918 /* pick lowest hold time */
919 if (hold
< peer
->hold
)
922 /* adjust our keepalive based on negotiated hold value */
923 if (peer
->keepalive
* 3 > peer
->hold
)
924 peer
->keepalive
= peer
->hold
/ 3;
926 /* next transition requires an exchange of keepalives */
927 bgp_send_keepalive(peer
);
929 /* FIXME: may need to check for optional params */
934 case BGP_MSG_KEEPALIVE
:
935 if (peer
->state
== OpenConfirm
)
937 peer
->state
= peer
->next_state
= Established
;
938 peer
->state_time
= time_now
;
939 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
940 peer
->update_routes
= 1;
941 peer
->retry_count
= 0;
942 peer
->retry_time
= 0;
944 LOG(4, 0, 0, "BGP peer %s: state Established\n", peer
->name
);
949 case BGP_MSG_NOTIFICATION
:
950 if (len
> sizeof(p
->header
))
952 struct bgp_data_notification
*notification
=
953 (struct bgp_data_notification
*) p
->data
;
955 if (notification
->error_code
== BGP_ERR_CEASE
)
957 LOG(4, 0, 0, "BGP peer %s sent CEASE\n", peer
->name
);
962 /* FIXME: should handle more notifications */
963 LOG(4, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n",
964 peer
->name
, (int) notification
->error_code
);
971 peer
->expire_time
= time_now
+ peer
->hold
;
973 /* see if there's another message in the same packet/buffer */
974 if (peer
->inbuf
->done
> len
)
976 peer
->inbuf
->done
-= len
;
977 memmove(p
, (char *) p
+ len
, peer
->inbuf
->done
);
981 peer
->inbuf
->packet
.header
.len
= 0;
982 peer
->inbuf
->done
= 0;
985 return peer
->inbuf
->done
;
988 /* send/buffer OPEN message */
989 static int bgp_send_open(struct bgp_peer
*peer
)
991 struct bgp_data_open data
;
992 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
994 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
995 sizeof(peer
->outbuf
->packet
.header
.marker
));
997 peer
->outbuf
->packet
.header
.type
= BGP_MSG_OPEN
;
999 data
.version
= BGP_VERSION
;
1000 data
.as
= htons(our_as
);
1001 data
.hold_time
= htons(peer
->hold
);
1002 data
.identifier
= my_address
;
1005 memcpy(peer
->outbuf
->packet
.data
, &data
, BGP_DATA_OPEN_SIZE
);
1006 len
+= BGP_DATA_OPEN_SIZE
;
1008 peer
->outbuf
->packet
.header
.len
= htons(len
);
1009 peer
->outbuf
->done
= 0;
1010 peer
->next_state
= OpenSent
;
1012 return bgp_write(peer
);
1015 /* send/buffer KEEPALIVE message */
1016 static int bgp_send_keepalive(struct bgp_peer
*peer
)
1018 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1019 sizeof(peer
->outbuf
->packet
.header
.marker
));
1021 peer
->outbuf
->packet
.header
.type
= BGP_MSG_KEEPALIVE
;
1022 peer
->outbuf
->packet
.header
.len
=
1023 htons(sizeof(peer
->outbuf
->packet
.header
));
1025 peer
->outbuf
->done
= 0;
1026 peer
->next_state
= (peer
->state
== OpenSent
) ? OpenConfirm
: peer
->state
;
1028 return bgp_write(peer
);
1031 /* send/buffer UPDATE message */
1032 static int bgp_send_update(struct bgp_peer
*peer
)
1034 uint16_t unf_len
= 0;
1036 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1037 struct bgp_route_list
*have
= peer
->routes
;
1038 struct bgp_route_list
*want
= peer
->routing
? bgp_routes
: 0;
1039 struct bgp_route_list
*e
= 0;
1040 struct bgp_route_list
*add
= 0;
1043 char *data
= (char *) &peer
->outbuf
->packet
.data
;
1045 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1046 char *max
= (char *) &peer
->outbuf
->packet
.data
1047 + sizeof(peer
->outbuf
->packet
.data
)
1048 - sizeof(attr_len
) - peer
->path_attr_len
- sizeof(struct bgp_ip_prefix
);
1050 /* skip over unf_len */
1051 data
+= sizeof(unf_len
);
1052 len
+= sizeof(unf_len
);
1054 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1055 sizeof(peer
->outbuf
->packet
.header
.marker
));
1057 peer
->outbuf
->packet
.header
.type
= BGP_MSG_UPDATE
;
1059 peer
->update_routes
= 0; /* tentatively clear */
1061 /* find differences */
1062 while ((have
|| want
) && data
< (max
- sizeof(struct bgp_ip_prefix
)))
1066 ? memcmp(&have
->dest
, &want
->dest
, sizeof(have
->dest
))
1071 if (s
< 0) /* found one to delete */
1073 struct bgp_route_list
*tmp
= have
;
1076 s
= BGP_IP_PREFIX_SIZE(tmp
->dest
);
1077 memcpy(data
, &tmp
->dest
, s
);
1082 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1083 fmtaddr(tmp
->dest
.prefix
, 0), tmp
->dest
.len
, peer
->name
);
1090 peer
->routes
= have
;
1096 e
= have
; /* stash the last found to relink above */
1100 else if (s
> 0) /* addition reqd. */
1104 peer
->update_routes
= 1; /* only one add per packet */
1118 peer
->update_routes
= 1; /* more to do */
1120 /* anything changed? */
1121 if (!(unf_len
|| add
))
1124 /* go back and insert unf_len */
1125 unf_len
= htons(unf_len
);
1126 memcpy(&peer
->outbuf
->packet
.data
, &unf_len
, sizeof(unf_len
));
1130 if (!(e
= malloc(sizeof(*e
))))
1132 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1133 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, strerror(errno
));
1138 memcpy(e
, add
, sizeof(*e
));
1140 peer
->routes
= bgp_insert_route(peer
->routes
, e
);
1142 attr_len
= htons(peer
->path_attr_len
);
1143 memcpy(data
, &attr_len
, sizeof(attr_len
));
1144 data
+= sizeof(attr_len
);
1145 len
+= sizeof(attr_len
);
1147 memcpy(data
, peer
->path_attrs
, peer
->path_attr_len
);
1148 data
+= peer
->path_attr_len
;
1149 len
+= peer
->path_attr_len
;
1151 s
= BGP_IP_PREFIX_SIZE(add
->dest
);
1152 memcpy(data
, &add
->dest
, s
);
1156 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1157 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, peer
->name
);
1162 memcpy(data
, &attr_len
, sizeof(attr_len
));
1163 data
+= sizeof(attr_len
);
1164 len
+= sizeof(attr_len
);
1167 peer
->outbuf
->packet
.header
.len
= htons(len
);
1168 peer
->outbuf
->done
= 0;
1170 return bgp_write(peer
);
1173 /* send/buffer NOTIFICATION message */
1174 static int bgp_send_notification(struct bgp_peer
*peer
, uint8_t code
,
1177 struct bgp_data_notification data
;
1180 data
.error_code
= code
;
1181 len
+= sizeof(data
.error_code
);
1183 data
.error_subcode
= subcode
;
1184 len
+= sizeof(data
.error_code
);
1186 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1187 sizeof(peer
->outbuf
->packet
.header
.marker
));
1189 peer
->outbuf
->packet
.header
.type
= BGP_MSG_NOTIFICATION
;
1190 peer
->outbuf
->packet
.header
.len
=
1191 htons(sizeof(peer
->outbuf
->packet
.header
) + len
);
1193 memcpy(peer
->outbuf
->packet
.data
, &data
, len
);
1195 peer
->outbuf
->done
= 0;
1196 peer
->next_state
= code
== BGP_ERR_CEASE
? Disabled
: Idle
;
1198 /* we're dying; ignore any pending input */
1199 peer
->inbuf
->packet
.header
.len
= 0;
1200 peer
->inbuf
->done
= 0;
1202 return bgp_write(peer
);