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.4 2004-09-02 04:18:07 fred_nerk 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_state_str(enum bgp_state state
);
38 static char const *bgp_msg_type_str(u8 type
);
39 static int bgp_connect(struct bgp_peer
*peer
);
40 static int bgp_handle_connect(struct bgp_peer
*peer
);
41 static int bgp_write(struct bgp_peer
*peer
);
42 static int bgp_read(struct bgp_peer
*peer
);
43 static int bgp_handle_input(struct bgp_peer
*peer
);
44 static int bgp_send_open(struct bgp_peer
*peer
);
45 static int bgp_send_keepalive(struct bgp_peer
*peer
);
46 static int bgp_send_update(struct bgp_peer
*peer
);
47 static int bgp_send_notification(struct bgp_peer
*peer
, u8 code
, u8 subcode
);
51 /* prepare peer structure, globals */
55 struct bgp_peer
*peer
;
57 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
60 memset(peer
, 0, sizeof(*peer
));
62 peer
->addr
= INADDR_NONE
;
64 peer
->state
= peer
->next_state
= Disabled
;
66 if (!((peer
->outbuf
= malloc(sizeof(*peer
->outbuf
)))
67 && (peer
->inbuf
= malloc(sizeof(*peer
->inbuf
)))))
69 log(0, 0, 0, 0, "Can't allocate buffers for bgp peer (%s)\n",
83 bgp_configured
= 0; /* set by bgp_start */
88 /* start connection with a peer */
89 int bgp_start(struct bgp_peer
*peer
, char *name
, int as
, int enable
)
94 struct bgp_path_attr a
;
98 u32 metric
= htonl(BGP_METRIC
);
99 u32 no_export
= htonl(BGP_COMMUNITY_NO_EXPORT
);
104 if (peer
->state
!= Disabled
)
107 snprintf(peer
->name
, sizeof(peer
->name
), "%s", name
);
109 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
111 log(0, 0, 0, 0, "Can't get address for BGP peer %s (%s)\n",
112 name
, h
? "no address" : hstrerror(h_errno
));
117 memcpy(&peer
->addr
, h
->h_addr
, sizeof(peer
->addr
));
118 peer
->as
= as
> 0 ? as
: our_as
;
119 ibgp
= peer
->as
== our_as
;
121 /* clear buffers, go to Idle state */
122 peer
->next_state
= Idle
;
125 /* set initial routing state */
126 peer
->routing
= enable
;
128 /* all our routes use the same attributes, so prepare it in advance */
129 if (peer
->path_attrs
)
130 free(peer
->path_attrs
);
132 peer
->path_attr_len
= 0;
135 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
136 a
.code
= BGP_PATH_ATTR_CODE_ORIGIN
;
138 a
.data
.s
.value
[0] = BGP_PATH_ATTR_CODE_ORIGIN_IGP
;
140 #define ADD_ATTRIBUTE() do { \
141 i = BGP_PATH_ATTR_SIZE(a); \
144 peer->path_attr_len += i; } while (0)
149 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
150 a
.code
= BGP_PATH_ATTR_CODE_AS_PATH
;
164 BGP_PATH_ATTR_CODE_AS_PATH_AS_SEQUENCE
,
169 a
.data
.s
.len
= sizeof(as_path
);
170 memcpy(&a
.data
.s
.value
, &as_path
, sizeof(as_path
));
176 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
177 a
.code
= BGP_PATH_ATTR_CODE_NEXT_HOP
;
178 ip
= my_address
; /* we're it */
179 a
.data
.s
.len
= sizeof(ip
);
180 memcpy(a
.data
.s
.value
, &ip
, sizeof(ip
));
184 /* MULTI_EXIT_DISC */
185 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
;
186 a
.code
= BGP_PATH_ATTR_CODE_MULTI_EXIT_DISC
;
187 a
.data
.s
.len
= sizeof(metric
);
188 memcpy(a
.data
.s
.value
, &metric
, sizeof(metric
));
194 u32 local_pref
= htonl(BGP_LOCAL_PREF
);
197 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
198 a
.code
= BGP_PATH_ATTR_CODE_LOCAL_PREF
;
199 a
.data
.s
.len
= sizeof(local_pref
);
200 memcpy(a
.data
.s
.value
, &local_pref
, sizeof(local_pref
));
206 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
| BGP_PATH_ATTR_FLAG_TRANS
;
207 a
.code
= BGP_PATH_ATTR_CODE_COMMUNITIES
;
208 a
.data
.s
.len
= sizeof(no_export
);
209 memcpy(a
.data
.s
.value
, &no_export
, sizeof(no_export
));
213 if (!(peer
->path_attrs
= malloc(peer
->path_attr_len
)))
215 log(0, 0, 0, 0, "Can't allocate path_attrs for %s (%s)\n",
216 name
, strerror(errno
));
221 memcpy(peer
->path_attrs
, path_attrs
, peer
->path_attr_len
);
223 log(4, 0, 0, 0, "Initiating BGP connection to %s (routing %s)\n",
224 name
, enable
? "enabled" : "suspended");
226 /* we have at least one peer configured */
230 return bgp_connect(peer
);
233 /* clear counters, timers, routes and buffers; close socket; move to
234 next_state, which may be Disabled or Idle */
235 static void bgp_clear(struct bgp_peer
*peer
)
237 if (peer
->sock
!= -1)
243 peer
->keepalive_time
= 0;
245 peer
->expire_time
= 0;
247 bgp_free_routes(peer
->routes
);
250 peer
->outbuf
->packet
.header
.len
= 0;
251 peer
->outbuf
->done
= 0;
252 peer
->inbuf
->packet
.header
.len
= 0;
253 peer
->inbuf
->done
= 0;
257 if (peer
->state
!= peer
->next_state
)
259 peer
->state
= peer
->next_state
;
260 peer
->state_time
= time_now
;
262 log(4, 0, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
263 bgp_state_str(peer
->next_state
));
267 /* initiate a clean shutdown */
268 void bgp_stop(struct bgp_peer
*peer
)
270 log(4, 0, 0, 0, "Terminating BGP connection to %s\n", peer
->name
);
271 bgp_send_notification(peer
, BGP_ERR_CEASE
, 0);
274 /* drop connection (if any) and set state to Disabled */
275 void bgp_halt(struct bgp_peer
*peer
)
277 log(4, 0, 0, 0, "Aborting BGP connection to %s\n", peer
->name
);
278 peer
->next_state
= Disabled
;
282 /* drop connection (if any) and set to Idle for connection retry */
283 int bgp_restart(struct bgp_peer
*peer
)
285 peer
->next_state
= Idle
;
289 peer
->retry_time
= time_now
;
290 peer
->retry_count
= 0;
293 return bgp_connect(peer
);
296 static void bgp_set_retry(struct bgp_peer
*peer
)
298 if (peer
->retry_count
++ < BGP_MAX_RETRY
)
300 peer
->retry_time
= time_now
+ (BGP_RETRY_BACKOFF
* peer
->retry_count
);
301 peer
->next_state
= Idle
;
305 bgp_halt(peer
); /* give up */
308 /* convert ip/mask to CIDR notation */
309 static void bgp_cidr(in_addr_t ip
, in_addr_t mask
, struct bgp_ip_prefix
*pfx
)
314 /* convert to prefix notation */
318 if (!mask
) /* bogus */
321 for (i
= 0; i
< 32 && ((b
= ntohl(1 << i
)), !(mask
& b
)); i
++)
328 /* insert route into list; sorted */
329 static struct bgp_route_list
*bgp_insert_route(struct bgp_route_list
*head
,
330 struct bgp_route_list
*new)
332 struct bgp_route_list
*p
= head
;
333 struct bgp_route_list
*e
= 0;
335 while (p
&& memcmp(&p
->dest
, &new->dest
, sizeof(p
->dest
)) < 0)
355 /* add route to list for peers */
357 * Note: this doesn't do route aggregation, nor drop routes if a less
358 * specific match already exists (partly because I'm lazy, but also so
359 * that if that route is later deleted we don't have to be concerned
360 * about adding back the more specific one).
362 int bgp_add_route(in_addr_t ip
, in_addr_t mask
)
364 struct bgp_route_list
*r
= bgp_routes
;
365 struct bgp_route_list add
;
368 bgp_cidr(ip
, mask
, &add
.dest
);
371 /* check for duplicate */
374 i
= memcmp(&r
->dest
, &add
.dest
, sizeof(r
->dest
));
376 return 1; /* already covered */
384 /* insert into route list; sorted */
385 if (!(r
= malloc(sizeof(*r
))))
387 log(0, 0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
388 inet_toa(add
.dest
.prefix
), add
.dest
.len
, strerror(errno
));
393 memcpy(r
, &add
, sizeof(*r
));
394 bgp_routes
= bgp_insert_route(bgp_routes
, r
);
396 /* flag established peers for update */
397 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
398 if (bgp_peers
[i
].state
== Established
)
399 bgp_peers
[i
].update_routes
= 1;
401 log(4, 0, 0, 0, "Registered BGP route %s/%d\n", inet_toa(add
.dest
.prefix
),
407 /* remove route from list for peers */
408 int bgp_del_route(in_addr_t ip
, in_addr_t mask
)
410 struct bgp_route_list
*r
= bgp_routes
;
411 struct bgp_route_list
*e
= 0;
412 struct bgp_route_list del
;
415 bgp_cidr(ip
, mask
, &del
.dest
);
418 /* find entry in routes list and remove */
421 i
= memcmp(&r
->dest
, &del
.dest
, sizeof(r
->dest
));
427 bgp_routes
= r
->next
;
445 /* flag established peers for update */
446 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
447 if (bgp_peers
[i
].state
== Established
)
448 bgp_peers
[i
].update_routes
= 1;
450 log(4, 0, 0, 0, "Removed BGP route %s/%d\n", inet_toa(del
.dest
.prefix
),
456 /* enable or disable routing */
457 void bgp_enable_routing(int enable
)
461 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
463 bgp_peers
[i
].routing
= enable
;
465 /* flag established peers for update */
466 if (bgp_peers
[i
].state
== Established
)
467 bgp_peers
[i
].update_routes
= 1;
470 log(4, 0, 0, 0, "%s BGP routing\n", enable
? "Enabled" : "Suspended");
473 /* return a bitmask indicating if the socket should be added to the
474 read set (1) and or write set (2) for select */
475 int bgp_select_state(struct bgp_peer
*peer
)
482 if (peer
->state
== Disabled
|| peer
->state
== Idle
)
485 if (peer
->inbuf
->done
< BGP_MAX_PACKET_SIZE
)
488 if (peer
->state
== Connect
|| /* connection in progress */
489 peer
->update_routes
|| /* routing updates */
490 peer
->outbuf
->packet
.header
.len
) /* pending output */
496 /* process bgp peer */
497 int bgp_process(struct bgp_peer
*peer
, int readable
, int writable
)
502 if (*peer
->name
&& peer
->cli_flag
== BGP_CLI_RESTART
)
503 return bgp_restart(peer
);
505 if (peer
->state
== Disabled
)
510 switch (peer
->cli_flag
)
512 case BGP_CLI_SUSPEND
:
516 if (peer
->state
== Established
)
517 peer
->update_routes
= 1;
526 if (peer
->state
== Established
)
527 peer
->update_routes
= 1;
536 /* handle empty/fill of buffers */
540 if (peer
->state
== Connect
)
541 r
= bgp_handle_connect(peer
);
542 else if (peer
->outbuf
->packet
.header
.len
)
555 /* process input buffer contents */
556 while (peer
->inbuf
->done
>= sizeof(peer
->inbuf
->packet
.header
)
557 && !peer
->outbuf
->packet
.header
.len
) /* may need to queue a response */
559 if (bgp_handle_input(peer
) < 0)
563 /* process pending updates */
564 if (peer
->update_routes
565 && !peer
->outbuf
->packet
.header
.len
) /* ditto */
567 if (!bgp_send_update(peer
))
572 if (peer
->state
== Established
)
574 if (time_now
> peer
->expire_time
)
576 log(1, 0, 0, 0, "No message from BGP peer %s in %ds\n",
577 peer
->name
, peer
->hold
);
579 bgp_send_notification(peer
, BGP_ERR_HOLD_TIMER_EXP
, 0);
583 if (time_now
> peer
->keepalive_time
&& !peer
->outbuf
->packet
.header
.len
)
584 bgp_send_keepalive(peer
);
586 else if (peer
->state
== Idle
)
588 if (time_now
> peer
->retry_time
)
589 return bgp_connect(peer
);
591 else if (time_now
> peer
->state_time
+ BGP_KEEPALIVE_TIME
)
593 log(1, 0, 0, 0, "%s timer expired for BGP peer %s\n",
594 bgp_state_str(peer
->state
), peer
->name
);
596 return bgp_restart(peer
);
602 static void bgp_free_routes(struct bgp_route_list
*routes
)
604 struct bgp_route_list
*tmp
;
606 while ((tmp
= routes
))
613 static char const *bgp_state_str(enum bgp_state state
)
617 case Disabled
: return "Disabled";
618 case Idle
: return "Idle";
619 case Connect
: return "Connect";
620 case Active
: return "Active";
621 case OpenSent
: return "OpenSent";
622 case OpenConfirm
: return "OpenConfirm";
623 case Established
: return "Established";
629 static char const *bgp_msg_type_str(u8 type
)
633 case BGP_MSG_OPEN
: return "OPEN";
634 case BGP_MSG_UPDATE
: return "UPDATE";
635 case BGP_MSG_NOTIFICATION
: return "NOTIFICATION";
636 case BGP_MSG_KEEPALIVE
: return "KEEPALIVE";
642 /* attempt to connect to peer */
643 static int bgp_connect(struct bgp_peer
*peer
)
645 static int bgp_port
= 0;
646 struct sockaddr_in addr
;
650 struct servent
*serv
;
651 if (!(serv
= getservbyname("bgp", "tcp")))
653 log(0, 0, 0, 0, "Can't get bgp service (%s)\n", strerror(errno
));
657 bgp_port
= serv
->s_port
;
660 if ((peer
->sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
662 log(0, 0, 0, 0, "Can't create a socket for BGP peer %s (%s)\n",
663 peer
->name
, strerror(errno
));
665 peer
->state
= peer
->next_state
= Disabled
;
669 /* set to non-blocking */
670 fcntl(peer
->sock
, F_SETFL
, fcntl(peer
->sock
, F_GETFL
, 0) | O_NONBLOCK
);
673 memset(&addr
, 0, sizeof(addr
));
674 addr
.sin_family
= AF_INET
;
675 addr
.sin_port
= bgp_port
;
676 addr
.sin_addr
.s_addr
= peer
->addr
;
678 while (connect(peer
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1)
680 if (errno
== EINTR
) /* SIGALARM handler */
683 if (errno
!= EINPROGRESS
)
685 log(1, 0, 0, 0, "Can't connect to BGP peer %s (%s)\n",
686 inet_ntoa(addr
.sin_addr
), strerror(errno
));
692 peer
->state
= Connect
;
693 peer
->state_time
= time_now
;
695 log(4, 0, 0, 0, "BGP peer %s: state Connect\n", peer
->name
);
699 peer
->state
= Active
;
700 peer
->state_time
= time_now
;
701 peer
->retry_time
= peer
->retry_count
= 0;
703 log(4, 0, 0, 0, "BGP peer %s: state Active\n", inet_ntoa(addr
.sin_addr
));
705 return bgp_send_open(peer
);
708 /* complete partial connection (state = Connect) */
709 static int bgp_handle_connect(struct bgp_peer
*peer
)
712 int len
= sizeof(int);
713 getsockopt(peer
->sock
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
716 log(1, 0, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer
->name
,
723 peer
->state
= Active
;
724 peer
->state_time
= time_now
;
726 log(4, 0, 0, 0, "BGP peer %s: state Active\n", peer
->name
);
728 return bgp_send_open(peer
);
731 /* initiate a write */
732 static int bgp_write(struct bgp_peer
*peer
)
734 int len
= htons(peer
->outbuf
->packet
.header
.len
);
737 while ((r
= write(peer
->sock
, &peer
->outbuf
->packet
+ peer
->outbuf
->done
,
738 len
- peer
->outbuf
->done
)) == -1)
747 log(1, 0, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
749 log(1, 0, 0, 0, "Can't write to BGP peer %s (%s)\n", peer
->name
,
758 peer
->outbuf
->done
+= r
;
762 log(4, 0, 0, 0, "Sent %s to BGP peer %s\n",
763 bgp_msg_type_str(peer
->outbuf
->packet
.header
.type
), peer
->name
);
765 peer
->outbuf
->packet
.header
.len
= 0;
766 peer
->outbuf
->done
= 0;
768 if (peer
->state
== Established
)
769 peer
->keepalive_time
= time_now
+ BGP_KEEPALIVE_TIME
;
771 if (peer
->state
!= peer
->next_state
)
773 if (peer
->next_state
== Disabled
|| peer
->next_state
== Idle
)
779 peer
->state
= peer
->next_state
;
780 peer
->state_time
= time_now
;
782 log(4, 0, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
783 bgp_state_str(peer
->state
));
789 /* initiate a read */
790 static int bgp_read(struct bgp_peer
*peer
)
794 while ((r
= read(peer
->sock
, &peer
->inbuf
->packet
+ peer
->inbuf
->done
,
795 BGP_MAX_PACKET_SIZE
- peer
->inbuf
->done
)) < 1)
799 log(1, 0, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
809 log(1, 0, 0, 0, "Can't read from BGP peer %s (%s)\n", peer
->name
,
817 peer
->inbuf
->done
+= r
;
821 /* process buffered packets */
822 static int bgp_handle_input(struct bgp_peer
*peer
)
824 struct bgp_packet
*p
= &peer
->inbuf
->packet
;
825 int len
= ntohs(p
->header
.len
);
827 if (len
> BGP_MAX_PACKET_SIZE
)
829 log(1, 0, 0, 0, "Bad header length from BGP %s\n", peer
->name
);
830 bgp_send_notification(peer
, BGP_ERR_HEADER
, BGP_ERR_HDR_BAD_LEN
);
834 if (peer
->inbuf
->done
< len
)
837 log(4, 0, 0, 0, "Received %s from BGP peer %s\n",
838 bgp_msg_type_str(p
->header
.type
), peer
->name
);
840 switch (p
->header
.type
)
844 struct bgp_data_open data
;
847 for (i
= 0; i
< sizeof(p
->header
.marker
); i
++)
849 if ((unsigned char) p
->header
.marker
[i
] != 0xff)
851 log(1, 0, 0, 0, "Invalid marker from BGP peer %s\n",
854 bgp_send_notification(peer
, BGP_ERR_HEADER
,
855 BGP_ERR_HDR_NOT_SYNC
);
861 if (peer
->state
!= OpenSent
)
863 log(1, 0, 0, 0, "OPEN from BGP peer %s in %s state\n",
864 peer
->name
, bgp_state_str(peer
->state
));
866 bgp_send_notification(peer
, BGP_ERR_FSM
, 0);
870 memcpy(&data
, p
->data
, len
- sizeof(p
->header
));
872 if (data
.version
!= BGP_VERSION
)
874 log(1, 0, 0, 0, "Bad version (%d) sent by BGP peer %s\n",
875 (int) data
.version
, peer
->name
);
877 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_VERSION
);
881 if (ntohs(data
.as
) != peer
->as
)
883 log(1, 0, 0, 0, "Bad AS sent by BGP peer %s (got %d, "
884 "expected %d)\n", peer
->name
, (int) htons(data
.as
),
887 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_BAD_AS
);
891 if ((peer
->hold
= ntohs(data
.hold_time
)) < 10)
893 log(1, 0, 0, 0, "Bad hold time (%d) from BGP peer %s\n",
894 peer
->hold
, peer
->name
);
896 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_HOLD_TIME
);
900 /* next transition requires an exchange of keepalives */
901 bgp_send_keepalive(peer
);
903 /* FIXME: may need to check for optional params */
908 case BGP_MSG_KEEPALIVE
:
909 if (peer
->state
== OpenConfirm
)
911 peer
->state
= peer
->next_state
= Established
;
912 peer
->state_time
= time_now
;
913 peer
->keepalive_time
= time_now
+ BGP_KEEPALIVE_TIME
;
914 peer
->update_routes
= 1;
915 peer
->retry_count
= 0;
916 peer
->retry_time
= 0;
918 log(4, 0, 0, 0, "BGP peer %s: state Established\n", peer
->name
);
923 case BGP_MSG_NOTIFICATION
:
924 if (len
> sizeof(p
->header
))
926 struct bgp_data_notification
*notification
=
927 (struct bgp_data_notification
*) p
->data
;
929 if (notification
->error_code
== BGP_ERR_CEASE
)
931 log(4, 0, 0, 0, "BGP peer %s sent CEASE\n", peer
->name
);
936 /* FIXME: should handle more notifications */
937 log(4, 0, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n",
938 peer
->name
, (int) notification
->error_code
);
945 peer
->expire_time
= time_now
+ peer
->hold
;
947 /* see if there's another message in the same packet/buffer */
948 if (peer
->inbuf
->done
> len
)
950 peer
->inbuf
->done
-= len
;
951 memmove(p
, (char *) p
+ len
, peer
->inbuf
->done
);
955 peer
->inbuf
->packet
.header
.len
= 0;
956 peer
->inbuf
->done
= 0;
959 return peer
->inbuf
->done
;
962 /* send/buffer OPEN message */
963 static int bgp_send_open(struct bgp_peer
*peer
)
965 struct bgp_data_open data
;
966 u16 len
= sizeof(peer
->outbuf
->packet
.header
);
968 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
969 sizeof(peer
->outbuf
->packet
.header
.marker
));
971 peer
->outbuf
->packet
.header
.type
= BGP_MSG_OPEN
;
973 data
.version
= BGP_VERSION
;
974 data
.as
= htons(our_as
);
975 data
.hold_time
= htons(BGP_HOLD_TIME
);
976 data
.identifier
= my_address
;
979 memcpy(peer
->outbuf
->packet
.data
, &data
, BGP_DATA_OPEN_SIZE
);
980 len
+= BGP_DATA_OPEN_SIZE
;
982 peer
->outbuf
->packet
.header
.len
= htons(len
);
983 peer
->outbuf
->done
= 0;
984 peer
->next_state
= OpenSent
;
986 return bgp_write(peer
);
989 /* send/buffer KEEPALIVE message */
990 static int bgp_send_keepalive(struct bgp_peer
*peer
)
992 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
993 sizeof(peer
->outbuf
->packet
.header
.marker
));
995 peer
->outbuf
->packet
.header
.type
= BGP_MSG_KEEPALIVE
;
996 peer
->outbuf
->packet
.header
.len
=
997 htons(sizeof(peer
->outbuf
->packet
.header
));
999 peer
->outbuf
->done
= 0;
1000 peer
->next_state
= (peer
->state
== OpenSent
) ? OpenConfirm
: peer
->state
;
1002 return bgp_write(peer
);
1005 /* send/buffer UPDATE message */
1006 static int bgp_send_update(struct bgp_peer
*peer
)
1010 u16 len
= sizeof(peer
->outbuf
->packet
.header
);
1011 struct bgp_route_list
*have
= peer
->routes
;
1012 struct bgp_route_list
*want
= peer
->routing
? bgp_routes
: 0;
1013 struct bgp_route_list
*e
= 0;
1014 struct bgp_route_list
*add
= 0;
1017 char *data
= (char *) &peer
->outbuf
->packet
.data
;
1019 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1020 char *max
= (char *) &peer
->outbuf
->packet
.data
1021 + sizeof(peer
->outbuf
->packet
.data
)
1022 - sizeof(attr_len
) - peer
->path_attr_len
- sizeof(struct bgp_ip_prefix
);
1024 /* skip over unf_len */
1025 data
+= sizeof(unf_len
);
1026 len
+= sizeof(unf_len
);
1028 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1029 sizeof(peer
->outbuf
->packet
.header
.marker
));
1031 peer
->outbuf
->packet
.header
.type
= BGP_MSG_UPDATE
;
1033 peer
->update_routes
= 0; /* tentatively clear */
1035 /* find differences */
1036 while ((have
|| want
) && data
< (max
- sizeof(struct bgp_ip_prefix
)))
1040 ? memcmp(&have
->dest
, &want
->dest
, sizeof(have
->dest
))
1045 if (s
< 0) /* found one to delete */
1047 struct bgp_route_list
*tmp
= have
;
1050 s
= BGP_IP_PREFIX_SIZE(tmp
->dest
);
1051 memcpy(data
, &tmp
->dest
, s
);
1056 log(5, 0, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1057 inet_toa(tmp
->dest
.prefix
), tmp
->dest
.len
, peer
->name
);
1064 peer
->routes
= have
;
1070 e
= have
; /* stash the last found to relink above */
1074 else if (s
> 0) /* addition reqd. */
1078 peer
->update_routes
= 1; /* only one add per packet */
1092 peer
->update_routes
= 1; /* more to do */
1094 /* anything changed? */
1095 if (!(unf_len
|| add
))
1098 /* go back and insert unf_len */
1099 unf_len
= htons(unf_len
);
1100 memcpy(&peer
->outbuf
->packet
.data
, &unf_len
, sizeof(unf_len
));
1104 if (!(e
= malloc(sizeof(*e
))))
1106 log(0, 0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1107 inet_toa(add
->dest
.prefix
), add
->dest
.len
, strerror(errno
));
1112 memcpy(e
, add
, sizeof(*e
));
1114 peer
->routes
= bgp_insert_route(peer
->routes
, e
);
1116 attr_len
= htons(peer
->path_attr_len
);
1117 memcpy(data
, &attr_len
, sizeof(attr_len
));
1118 data
+= sizeof(attr_len
);
1119 len
+= sizeof(attr_len
);
1121 memcpy(data
, peer
->path_attrs
, peer
->path_attr_len
);
1122 data
+= peer
->path_attr_len
;
1123 len
+= peer
->path_attr_len
;
1125 s
= BGP_IP_PREFIX_SIZE(add
->dest
);
1126 memcpy(data
, &add
->dest
, s
);
1130 log(5, 0, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1131 inet_toa(add
->dest
.prefix
), add
->dest
.len
, peer
->name
);
1136 memcpy(data
, &attr_len
, sizeof(attr_len
));
1137 data
+= sizeof(attr_len
);
1138 len
+= sizeof(attr_len
);
1141 peer
->outbuf
->packet
.header
.len
= htons(len
);
1142 peer
->outbuf
->done
= 0;
1144 return bgp_write(peer
);
1147 /* send/buffer NOTIFICATION message */
1148 static int bgp_send_notification(struct bgp_peer
*peer
, u8 code
, u8 subcode
)
1150 struct bgp_data_notification data
;
1153 data
.error_code
= code
;
1154 len
+= sizeof(data
.error_code
);
1156 data
.error_subcode
= subcode
;
1157 len
+= sizeof(data
.error_code
);
1159 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1160 sizeof(peer
->outbuf
->packet
.header
.marker
));
1162 peer
->outbuf
->packet
.header
.type
= BGP_MSG_NOTIFICATION
;
1163 peer
->outbuf
->packet
.header
.len
=
1164 htons(sizeof(peer
->outbuf
->packet
.header
) + len
);
1166 memcpy(peer
->outbuf
->packet
.data
, &data
, len
);
1168 peer
->outbuf
->done
= 0;
1169 peer
->next_state
= code
== BGP_ERR_CEASE
? Disabled
: Idle
;
1171 /* we're dying; ignore any pending input */
1172 peer
->inbuf
->packet
.header
.len
= 0;
1173 peer
->inbuf
->done
= 0;
1175 return bgp_write(peer
);
1182 int cmd_show_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1188 if (!bgp_configured
)
1191 if (CLI_HELP_REQUESTED
)
1192 return cli_arg_help(cli
, 1,
1193 "A.B.C.D", "BGP peer address",
1194 "NAME", "BGP peer name",
1197 cli_print(cli
, "BGPv%d router identifier %s, local AS number %d, "
1198 "hold time %ds", BGP_VERSION
, inet_toa(my_address
), (int) our_as
,
1203 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1205 if (!*bgp_peers
[i
].name
)
1208 addr
= inet_toa(bgp_peers
[i
].addr
);
1209 if (argc
&& strcmp(addr
, argv
[0]) &&
1210 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
1216 cli_print(cli
, "Peer AS Address "
1217 "State Retries Retry in Route Pend");
1218 cli_print(cli
, "------------------ ----- --------------- "
1219 "----------- ------- -------- ----- ----");
1222 cli_print(cli
, "%-18.18s %5d %15s %-11s %7d %7ds %5s %4s",
1226 bgp_state_str(bgp_peers
[i
].state
),
1227 bgp_peers
[i
].retry_count
,
1228 bgp_peers
[i
].retry_time
? bgp_peers
[i
].retry_time
- time_now
: 0,
1229 bgp_peers
[i
].routing
? "yes" : "no",
1230 bgp_peers
[i
].update_routes
? "yes" : "no");
1236 int cmd_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1241 if (!bgp_configured
)
1244 if (CLI_HELP_REQUESTED
)
1245 return cli_arg_help(cli
, 1,
1246 "A.B.C.D", "BGP peer address",
1247 "NAME", "BGP peer name",
1250 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1252 if (bgp_peers
[i
].state
!= Established
)
1255 if (!bgp_peers
[i
].routing
)
1258 addr
= inet_toa(bgp_peers
[i
].addr
);
1259 if (argc
&& strcmp(addr
, argv
[0]) && strcmp(bgp_peers
[i
].name
, argv
[0]))
1262 bgp_peers
[i
].cli_flag
= BGP_CLI_SUSPEND
;
1263 cli_print(cli
, "Suspending peer %s", bgp_peers
[i
].name
);
1269 int cmd_no_suspend_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1274 if (!bgp_configured
)
1277 if (CLI_HELP_REQUESTED
)
1278 return cli_arg_help(cli
, 1,
1279 "A.B.C.D", "BGP peer address",
1280 "NAME", "BGP peer name",
1283 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1285 if (bgp_peers
[i
].state
!= Established
)
1288 if (bgp_peers
[i
].routing
)
1291 addr
= inet_toa(bgp_peers
[i
].addr
);
1292 if (argc
&& strcmp(addr
, argv
[0]) &&
1293 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
1296 bgp_peers
[i
].cli_flag
= BGP_CLI_ENABLE
;
1297 cli_print(cli
, "Un-suspending peer %s", bgp_peers
[i
].name
);
1303 int cmd_restart_bgp(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1308 if (!bgp_configured
)
1311 if (CLI_HELP_REQUESTED
)
1312 return cli_arg_help(cli
, 1,
1313 "A.B.C.D", "BGP peer address",
1314 "NAME", "BGP peer name",
1317 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
1319 if (!*bgp_peers
[i
].name
)
1322 addr
= inet_toa(bgp_peers
[i
].addr
);
1323 if (argc
&& strcmp(addr
, argv
[0]) &&
1324 strncmp(bgp_peers
[i
].name
, argv
[0], strlen(argv
[0])))
1327 bgp_peers
[i
].cli_flag
= BGP_CLI_RESTART
;
1328 cli_print(cli
, "Restarting peer %s", bgp_peers
[i
].name
);