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.6 2004-11-11 03:07:42 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(u8 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
, u8 code
, u8 subcode
);
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, 0, "Can't allocate buffers for bgp peer (%s)\n",
86 bgp_configured
= 0; /* set by bgp_start */
91 /* start connection with a peer */
92 int bgp_start(struct bgp_peer
*peer
, char *name
, int as
, int keepalive
, int hold
, int enable
)
97 struct bgp_path_attr a
;
101 u32 metric
= htonl(BGP_METRIC
);
102 u32 no_export
= htonl(BGP_COMMUNITY_NO_EXPORT
);
107 if (peer
->state
!= Disabled
)
110 snprintf(peer
->name
, sizeof(peer
->name
), "%s", name
);
112 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
114 LOG(0, 0, 0, 0, "Can't get address for BGP peer %s (%s)\n",
115 name
, h
? "no address" : hstrerror(h_errno
));
120 memcpy(&peer
->addr
, h
->h_addr
, sizeof(peer
->addr
));
121 peer
->as
= as
> 0 ? as
: our_as
;
122 ibgp
= peer
->as
== our_as
;
124 /* set initial timer values */
125 peer
->init_keepalive
= keepalive
== -1 ? BGP_KEEPALIVE_TIME
: keepalive
;
126 peer
->init_hold
= hold
== -1 ? BGP_HOLD_TIME
: hold
;
128 if (peer
->init_hold
< 3)
131 if (peer
->init_keepalive
* 3 > peer
->init_hold
)
132 peer
->init_keepalive
= peer
->init_hold
/ 3;
134 /* clear buffers, go to Idle state */
135 peer
->next_state
= Idle
;
138 /* set initial routing state */
139 peer
->routing
= enable
;
141 /* all our routes use the same attributes, so prepare it in advance */
142 if (peer
->path_attrs
)
143 free(peer
->path_attrs
);
145 peer
->path_attr_len
= 0;
148 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
149 a
.code
= BGP_PATH_ATTR_CODE_ORIGIN
;
151 a
.data
.s
.value
[0] = BGP_PATH_ATTR_CODE_ORIGIN_IGP
;
153 #define ADD_ATTRIBUTE() do { \
154 i = BGP_PATH_ATTR_SIZE(a); \
157 peer->path_attr_len += i; } while (0)
162 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
163 a
.code
= BGP_PATH_ATTR_CODE_AS_PATH
;
177 BGP_PATH_ATTR_CODE_AS_PATH_AS_SEQUENCE
,
182 a
.data
.s
.len
= sizeof(as_path
);
183 memcpy(&a
.data
.s
.value
, &as_path
, sizeof(as_path
));
189 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
190 a
.code
= BGP_PATH_ATTR_CODE_NEXT_HOP
;
191 ip
= my_address
; /* we're it */
192 a
.data
.s
.len
= sizeof(ip
);
193 memcpy(a
.data
.s
.value
, &ip
, sizeof(ip
));
197 /* MULTI_EXIT_DISC */
198 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
;
199 a
.code
= BGP_PATH_ATTR_CODE_MULTI_EXIT_DISC
;
200 a
.data
.s
.len
= sizeof(metric
);
201 memcpy(a
.data
.s
.value
, &metric
, sizeof(metric
));
207 u32 local_pref
= htonl(BGP_LOCAL_PREF
);
210 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
211 a
.code
= BGP_PATH_ATTR_CODE_LOCAL_PREF
;
212 a
.data
.s
.len
= sizeof(local_pref
);
213 memcpy(a
.data
.s
.value
, &local_pref
, sizeof(local_pref
));
219 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
| BGP_PATH_ATTR_FLAG_TRANS
;
220 a
.code
= BGP_PATH_ATTR_CODE_COMMUNITIES
;
221 a
.data
.s
.len
= sizeof(no_export
);
222 memcpy(a
.data
.s
.value
, &no_export
, sizeof(no_export
));
226 if (!(peer
->path_attrs
= malloc(peer
->path_attr_len
)))
228 LOG(0, 0, 0, 0, "Can't allocate path_attrs for %s (%s)\n",
229 name
, strerror(errno
));
234 memcpy(peer
->path_attrs
, path_attrs
, peer
->path_attr_len
);
236 LOG(4, 0, 0, 0, "Initiating BGP connection to %s (routing %s)\n",
237 name
, enable
? "enabled" : "suspended");
239 /* we have at least one peer configured */
243 return bgp_connect(peer
);
246 /* clear counters, timers, routes and buffers; close socket; move to
247 next_state, which may be Disabled or Idle */
248 static void bgp_clear(struct bgp_peer
*peer
)
250 if (peer
->sock
!= -1)
256 peer
->keepalive_time
= 0;
257 peer
->expire_time
= 0;
259 peer
->keepalive
= peer
->init_keepalive
;
260 peer
->hold
= peer
->init_hold
;
262 bgp_free_routes(peer
->routes
);
265 peer
->outbuf
->packet
.header
.len
= 0;
266 peer
->outbuf
->done
= 0;
267 peer
->inbuf
->packet
.header
.len
= 0;
268 peer
->inbuf
->done
= 0;
272 if (peer
->state
!= peer
->next_state
)
274 peer
->state
= peer
->next_state
;
275 peer
->state_time
= time_now
;
277 LOG(4, 0, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
278 bgp_state_str(peer
->next_state
));
282 /* initiate a clean shutdown */
283 void bgp_stop(struct bgp_peer
*peer
)
285 LOG(4, 0, 0, 0, "Terminating BGP connection to %s\n", peer
->name
);
286 bgp_send_notification(peer
, BGP_ERR_CEASE
, 0);
289 /* drop connection (if any) and set state to Disabled */
290 void bgp_halt(struct bgp_peer
*peer
)
292 LOG(4, 0, 0, 0, "Aborting BGP connection to %s\n", peer
->name
);
293 peer
->next_state
= Disabled
;
297 /* drop connection (if any) and set to Idle for connection retry */
298 int bgp_restart(struct bgp_peer
*peer
)
300 peer
->next_state
= Idle
;
304 peer
->retry_time
= time_now
;
305 peer
->retry_count
= 0;
308 return bgp_connect(peer
);
311 static void bgp_set_retry(struct bgp_peer
*peer
)
313 if (peer
->retry_count
++ < BGP_MAX_RETRY
)
315 peer
->retry_time
= time_now
+ (BGP_RETRY_BACKOFF
* peer
->retry_count
);
316 peer
->next_state
= Idle
;
320 bgp_halt(peer
); /* give up */
323 /* convert ip/mask to CIDR notation */
324 static void bgp_cidr(in_addr_t ip
, in_addr_t mask
, struct bgp_ip_prefix
*pfx
)
329 /* convert to prefix notation */
333 if (!mask
) /* bogus */
336 for (i
= 0; i
< 32 && ((b
= ntohl(1 << i
)), !(mask
& b
)); i
++)
343 /* insert route into list; sorted */
344 static struct bgp_route_list
*bgp_insert_route(struct bgp_route_list
*head
,
345 struct bgp_route_list
*new)
347 struct bgp_route_list
*p
= head
;
348 struct bgp_route_list
*e
= 0;
350 while (p
&& memcmp(&p
->dest
, &new->dest
, sizeof(p
->dest
)) < 0)
370 /* add route to list for peers */
372 * Note: this doesn't do route aggregation, nor drop routes if a less
373 * specific match already exists (partly because I'm lazy, but also so
374 * that if that route is later deleted we don't have to be concerned
375 * about adding back the more specific one).
377 int bgp_add_route(in_addr_t ip
, in_addr_t mask
)
379 struct bgp_route_list
*r
= bgp_routes
;
380 struct bgp_route_list add
;
383 bgp_cidr(ip
, mask
, &add
.dest
);
386 /* check for duplicate */
389 i
= memcmp(&r
->dest
, &add
.dest
, sizeof(r
->dest
));
391 return 1; /* already covered */
399 /* insert into route list; sorted */
400 if (!(r
= malloc(sizeof(*r
))))
402 LOG(0, 0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
403 inet_toa(add
.dest
.prefix
), add
.dest
.len
, strerror(errno
));
408 memcpy(r
, &add
, sizeof(*r
));
409 bgp_routes
= bgp_insert_route(bgp_routes
, r
);
411 /* flag established peers for update */
412 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
413 if (bgp_peers
[i
].state
== Established
)
414 bgp_peers
[i
].update_routes
= 1;
416 LOG(4, 0, 0, 0, "Registered BGP route %s/%d\n", inet_toa(add
.dest
.prefix
),
422 /* remove route from list for peers */
423 int bgp_del_route(in_addr_t ip
, in_addr_t mask
)
425 struct bgp_route_list
*r
= bgp_routes
;
426 struct bgp_route_list
*e
= 0;
427 struct bgp_route_list del
;
430 bgp_cidr(ip
, mask
, &del
.dest
);
433 /* find entry in routes list and remove */
436 i
= memcmp(&r
->dest
, &del
.dest
, sizeof(r
->dest
));
442 bgp_routes
= r
->next
;
460 /* flag established peers for update */
461 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
462 if (bgp_peers
[i
].state
== Established
)
463 bgp_peers
[i
].update_routes
= 1;
465 LOG(4, 0, 0, 0, "Removed BGP route %s/%d\n", inet_toa(del
.dest
.prefix
),
471 /* enable or disable routing */
472 void bgp_enable_routing(int enable
)
476 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
478 bgp_peers
[i
].routing
= enable
;
480 /* flag established peers for update */
481 if (bgp_peers
[i
].state
== Established
)
482 bgp_peers
[i
].update_routes
= 1;
485 LOG(4, 0, 0, 0, "%s BGP routing\n", enable
? "Enabled" : "Suspended");
488 /* return a bitmask indicating if the socket should be added to the
489 read set (1) and or write set (2) for select */
490 int bgp_select_state(struct bgp_peer
*peer
)
497 if (peer
->state
== Disabled
|| peer
->state
== Idle
)
500 if (peer
->inbuf
->done
< BGP_MAX_PACKET_SIZE
)
503 if (peer
->state
== Connect
|| /* connection in progress */
504 peer
->update_routes
|| /* routing updates */
505 peer
->outbuf
->packet
.header
.len
) /* pending output */
511 /* process bgp peer */
512 int bgp_process(struct bgp_peer
*peer
, int readable
, int writable
)
517 if (*peer
->name
&& peer
->cli_flag
== BGP_CLI_RESTART
)
518 return bgp_restart(peer
);
520 if (peer
->state
== Disabled
)
525 switch (peer
->cli_flag
)
527 case BGP_CLI_SUSPEND
:
531 if (peer
->state
== Established
)
532 peer
->update_routes
= 1;
541 if (peer
->state
== Established
)
542 peer
->update_routes
= 1;
551 /* handle empty/fill of buffers */
555 if (peer
->state
== Connect
)
556 r
= bgp_handle_connect(peer
);
557 else if (peer
->outbuf
->packet
.header
.len
)
570 /* process input buffer contents */
571 while (peer
->inbuf
->done
>= sizeof(peer
->inbuf
->packet
.header
)
572 && !peer
->outbuf
->packet
.header
.len
) /* may need to queue a response */
574 if (bgp_handle_input(peer
) < 0)
578 /* process pending updates */
579 if (peer
->update_routes
580 && !peer
->outbuf
->packet
.header
.len
) /* ditto */
582 if (!bgp_send_update(peer
))
587 if (peer
->state
== Established
)
589 if (time_now
> peer
->expire_time
)
591 LOG(1, 0, 0, 0, "No message from BGP peer %s in %ds\n",
592 peer
->name
, peer
->hold
);
594 bgp_send_notification(peer
, BGP_ERR_HOLD_TIMER_EXP
, 0);
598 if (time_now
> peer
->keepalive_time
&& !peer
->outbuf
->packet
.header
.len
)
599 bgp_send_keepalive(peer
);
601 else if (peer
->state
== Idle
)
603 if (time_now
> peer
->retry_time
)
604 return bgp_connect(peer
);
606 else if (time_now
> peer
->state_time
+ BGP_STATE_TIME
)
608 LOG(1, 0, 0, 0, "%s timer expired for BGP peer %s\n",
609 bgp_state_str(peer
->state
), peer
->name
);
611 return bgp_restart(peer
);
617 static void bgp_free_routes(struct bgp_route_list
*routes
)
619 struct bgp_route_list
*tmp
;
621 while ((tmp
= routes
))
628 char const *bgp_state_str(enum bgp_state state
)
632 case Disabled
: return "Disabled";
633 case Idle
: return "Idle";
634 case Connect
: return "Connect";
635 case Active
: return "Active";
636 case OpenSent
: return "OpenSent";
637 case OpenConfirm
: return "OpenConfirm";
638 case Established
: return "Established";
644 static char const *bgp_msg_type_str(u8 type
)
648 case BGP_MSG_OPEN
: return "OPEN";
649 case BGP_MSG_UPDATE
: return "UPDATE";
650 case BGP_MSG_NOTIFICATION
: return "NOTIFICATION";
651 case BGP_MSG_KEEPALIVE
: return "KEEPALIVE";
657 /* attempt to connect to peer */
658 static int bgp_connect(struct bgp_peer
*peer
)
660 static int bgp_port
= 0;
661 struct sockaddr_in addr
;
665 struct servent
*serv
;
666 if (!(serv
= getservbyname("bgp", "tcp")))
668 LOG(0, 0, 0, 0, "Can't get bgp service (%s)\n", strerror(errno
));
672 bgp_port
= serv
->s_port
;
675 if ((peer
->sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
677 LOG(0, 0, 0, 0, "Can't create a socket for BGP peer %s (%s)\n",
678 peer
->name
, strerror(errno
));
680 peer
->state
= peer
->next_state
= Disabled
;
684 /* set to non-blocking */
685 fcntl(peer
->sock
, F_SETFL
, fcntl(peer
->sock
, F_GETFL
, 0) | O_NONBLOCK
);
688 memset(&addr
, 0, sizeof(addr
));
689 addr
.sin_family
= AF_INET
;
690 addr
.sin_port
= bgp_port
;
691 addr
.sin_addr
.s_addr
= peer
->addr
;
693 while (connect(peer
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1)
695 if (errno
== EINTR
) /* SIGALARM handler */
698 if (errno
!= EINPROGRESS
)
700 LOG(1, 0, 0, 0, "Can't connect to BGP peer %s (%s)\n",
701 inet_ntoa(addr
.sin_addr
), strerror(errno
));
707 peer
->state
= Connect
;
708 peer
->state_time
= time_now
;
710 LOG(4, 0, 0, 0, "BGP peer %s: state Connect\n", peer
->name
);
714 peer
->state
= Active
;
715 peer
->state_time
= time_now
;
716 peer
->retry_time
= peer
->retry_count
= 0;
718 LOG(4, 0, 0, 0, "BGP peer %s: state Active\n", inet_ntoa(addr
.sin_addr
));
720 return bgp_send_open(peer
);
723 /* complete partial connection (state = Connect) */
724 static int bgp_handle_connect(struct bgp_peer
*peer
)
727 int len
= sizeof(int);
728 getsockopt(peer
->sock
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
731 LOG(1, 0, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer
->name
,
738 peer
->state
= Active
;
739 peer
->state_time
= time_now
;
741 LOG(4, 0, 0, 0, "BGP peer %s: state Active\n", peer
->name
);
743 return bgp_send_open(peer
);
746 /* initiate a write */
747 static int bgp_write(struct bgp_peer
*peer
)
749 int len
= htons(peer
->outbuf
->packet
.header
.len
);
752 while ((r
= write(peer
->sock
, &peer
->outbuf
->packet
+ peer
->outbuf
->done
,
753 len
- peer
->outbuf
->done
)) == -1)
762 LOG(1, 0, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
764 LOG(1, 0, 0, 0, "Can't write to BGP peer %s (%s)\n", peer
->name
,
773 peer
->outbuf
->done
+= r
;
777 LOG(4, 0, 0, 0, "Sent %s to BGP peer %s\n",
778 bgp_msg_type_str(peer
->outbuf
->packet
.header
.type
), peer
->name
);
780 peer
->outbuf
->packet
.header
.len
= 0;
781 peer
->outbuf
->done
= 0;
783 if (peer
->state
== Established
)
784 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
786 if (peer
->state
!= peer
->next_state
)
788 if (peer
->next_state
== Disabled
|| peer
->next_state
== Idle
)
794 peer
->state
= peer
->next_state
;
795 peer
->state_time
= time_now
;
797 LOG(4, 0, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
798 bgp_state_str(peer
->state
));
804 /* initiate a read */
805 static int bgp_read(struct bgp_peer
*peer
)
809 while ((r
= read(peer
->sock
, &peer
->inbuf
->packet
+ peer
->inbuf
->done
,
810 BGP_MAX_PACKET_SIZE
- peer
->inbuf
->done
)) < 1)
814 LOG(1, 0, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
824 LOG(1, 0, 0, 0, "Can't read from BGP peer %s (%s)\n", peer
->name
,
832 peer
->inbuf
->done
+= r
;
836 /* process buffered packets */
837 static int bgp_handle_input(struct bgp_peer
*peer
)
839 struct bgp_packet
*p
= &peer
->inbuf
->packet
;
840 int len
= ntohs(p
->header
.len
);
842 if (len
> BGP_MAX_PACKET_SIZE
)
844 LOG(1, 0, 0, 0, "Bad header length from BGP %s\n", peer
->name
);
845 bgp_send_notification(peer
, BGP_ERR_HEADER
, BGP_ERR_HDR_BAD_LEN
);
849 if (peer
->inbuf
->done
< len
)
852 LOG(4, 0, 0, 0, "Received %s from BGP peer %s\n",
853 bgp_msg_type_str(p
->header
.type
), peer
->name
);
855 switch (p
->header
.type
)
859 struct bgp_data_open data
;
862 for (i
= 0; i
< sizeof(p
->header
.marker
); i
++)
864 if ((unsigned char) p
->header
.marker
[i
] != 0xff)
866 LOG(1, 0, 0, 0, "Invalid marker from BGP peer %s\n",
869 bgp_send_notification(peer
, BGP_ERR_HEADER
,
870 BGP_ERR_HDR_NOT_SYNC
);
876 if (peer
->state
!= OpenSent
)
878 LOG(1, 0, 0, 0, "OPEN from BGP peer %s in %s state\n",
879 peer
->name
, bgp_state_str(peer
->state
));
881 bgp_send_notification(peer
, BGP_ERR_FSM
, 0);
885 memcpy(&data
, p
->data
, len
- sizeof(p
->header
));
887 if (data
.version
!= BGP_VERSION
)
889 LOG(1, 0, 0, 0, "Bad version (%d) sent by BGP peer %s\n",
890 (int) data
.version
, peer
->name
);
892 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_VERSION
);
896 if (ntohs(data
.as
) != peer
->as
)
898 LOG(1, 0, 0, 0, "Bad AS sent by BGP peer %s (got %d, "
899 "expected %d)\n", peer
->name
, (int) htons(data
.as
),
902 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_BAD_AS
);
906 if ((peer
->hold
= ntohs(data
.hold_time
)) < 3)
908 LOG(1, 0, 0, 0, "Bad hold time (%d) from BGP peer %s\n",
909 peer
->hold
, peer
->name
);
911 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_HOLD_TIME
);
915 /* adjust our keepalive based on negotiated hold value */
916 if (peer
->keepalive
* 3 > peer
->hold
)
917 peer
->keepalive
= peer
->hold
/ 3;
919 /* next transition requires an exchange of keepalives */
920 bgp_send_keepalive(peer
);
922 /* FIXME: may need to check for optional params */
927 case BGP_MSG_KEEPALIVE
:
928 if (peer
->state
== OpenConfirm
)
930 peer
->state
= peer
->next_state
= Established
;
931 peer
->state_time
= time_now
;
932 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
933 peer
->update_routes
= 1;
934 peer
->retry_count
= 0;
935 peer
->retry_time
= 0;
937 LOG(4, 0, 0, 0, "BGP peer %s: state Established\n", peer
->name
);
942 case BGP_MSG_NOTIFICATION
:
943 if (len
> sizeof(p
->header
))
945 struct bgp_data_notification
*notification
=
946 (struct bgp_data_notification
*) p
->data
;
948 if (notification
->error_code
== BGP_ERR_CEASE
)
950 LOG(4, 0, 0, 0, "BGP peer %s sent CEASE\n", peer
->name
);
955 /* FIXME: should handle more notifications */
956 LOG(4, 0, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n",
957 peer
->name
, (int) notification
->error_code
);
964 peer
->expire_time
= time_now
+ peer
->hold
;
966 /* see if there's another message in the same packet/buffer */
967 if (peer
->inbuf
->done
> len
)
969 peer
->inbuf
->done
-= len
;
970 memmove(p
, (char *) p
+ len
, peer
->inbuf
->done
);
974 peer
->inbuf
->packet
.header
.len
= 0;
975 peer
->inbuf
->done
= 0;
978 return peer
->inbuf
->done
;
981 /* send/buffer OPEN message */
982 static int bgp_send_open(struct bgp_peer
*peer
)
984 struct bgp_data_open data
;
985 u16 len
= sizeof(peer
->outbuf
->packet
.header
);
987 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
988 sizeof(peer
->outbuf
->packet
.header
.marker
));
990 peer
->outbuf
->packet
.header
.type
= BGP_MSG_OPEN
;
992 data
.version
= BGP_VERSION
;
993 data
.as
= htons(our_as
);
994 data
.hold_time
= htons(peer
->hold
);
995 data
.identifier
= my_address
;
998 memcpy(peer
->outbuf
->packet
.data
, &data
, BGP_DATA_OPEN_SIZE
);
999 len
+= BGP_DATA_OPEN_SIZE
;
1001 peer
->outbuf
->packet
.header
.len
= htons(len
);
1002 peer
->outbuf
->done
= 0;
1003 peer
->next_state
= OpenSent
;
1005 return bgp_write(peer
);
1008 /* send/buffer KEEPALIVE message */
1009 static int bgp_send_keepalive(struct bgp_peer
*peer
)
1011 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1012 sizeof(peer
->outbuf
->packet
.header
.marker
));
1014 peer
->outbuf
->packet
.header
.type
= BGP_MSG_KEEPALIVE
;
1015 peer
->outbuf
->packet
.header
.len
=
1016 htons(sizeof(peer
->outbuf
->packet
.header
));
1018 peer
->outbuf
->done
= 0;
1019 peer
->next_state
= (peer
->state
== OpenSent
) ? OpenConfirm
: peer
->state
;
1021 return bgp_write(peer
);
1024 /* send/buffer UPDATE message */
1025 static int bgp_send_update(struct bgp_peer
*peer
)
1029 u16 len
= sizeof(peer
->outbuf
->packet
.header
);
1030 struct bgp_route_list
*have
= peer
->routes
;
1031 struct bgp_route_list
*want
= peer
->routing
? bgp_routes
: 0;
1032 struct bgp_route_list
*e
= 0;
1033 struct bgp_route_list
*add
= 0;
1036 char *data
= (char *) &peer
->outbuf
->packet
.data
;
1038 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1039 char *max
= (char *) &peer
->outbuf
->packet
.data
1040 + sizeof(peer
->outbuf
->packet
.data
)
1041 - sizeof(attr_len
) - peer
->path_attr_len
- sizeof(struct bgp_ip_prefix
);
1043 /* skip over unf_len */
1044 data
+= sizeof(unf_len
);
1045 len
+= sizeof(unf_len
);
1047 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1048 sizeof(peer
->outbuf
->packet
.header
.marker
));
1050 peer
->outbuf
->packet
.header
.type
= BGP_MSG_UPDATE
;
1052 peer
->update_routes
= 0; /* tentatively clear */
1054 /* find differences */
1055 while ((have
|| want
) && data
< (max
- sizeof(struct bgp_ip_prefix
)))
1059 ? memcmp(&have
->dest
, &want
->dest
, sizeof(have
->dest
))
1064 if (s
< 0) /* found one to delete */
1066 struct bgp_route_list
*tmp
= have
;
1069 s
= BGP_IP_PREFIX_SIZE(tmp
->dest
);
1070 memcpy(data
, &tmp
->dest
, s
);
1075 LOG(5, 0, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1076 inet_toa(tmp
->dest
.prefix
), tmp
->dest
.len
, peer
->name
);
1083 peer
->routes
= have
;
1089 e
= have
; /* stash the last found to relink above */
1093 else if (s
> 0) /* addition reqd. */
1097 peer
->update_routes
= 1; /* only one add per packet */
1111 peer
->update_routes
= 1; /* more to do */
1113 /* anything changed? */
1114 if (!(unf_len
|| add
))
1117 /* go back and insert unf_len */
1118 unf_len
= htons(unf_len
);
1119 memcpy(&peer
->outbuf
->packet
.data
, &unf_len
, sizeof(unf_len
));
1123 if (!(e
= malloc(sizeof(*e
))))
1125 LOG(0, 0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1126 inet_toa(add
->dest
.prefix
), add
->dest
.len
, strerror(errno
));
1131 memcpy(e
, add
, sizeof(*e
));
1133 peer
->routes
= bgp_insert_route(peer
->routes
, e
);
1135 attr_len
= htons(peer
->path_attr_len
);
1136 memcpy(data
, &attr_len
, sizeof(attr_len
));
1137 data
+= sizeof(attr_len
);
1138 len
+= sizeof(attr_len
);
1140 memcpy(data
, peer
->path_attrs
, peer
->path_attr_len
);
1141 data
+= peer
->path_attr_len
;
1142 len
+= peer
->path_attr_len
;
1144 s
= BGP_IP_PREFIX_SIZE(add
->dest
);
1145 memcpy(data
, &add
->dest
, s
);
1149 LOG(5, 0, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1150 inet_toa(add
->dest
.prefix
), add
->dest
.len
, peer
->name
);
1155 memcpy(data
, &attr_len
, sizeof(attr_len
));
1156 data
+= sizeof(attr_len
);
1157 len
+= sizeof(attr_len
);
1160 peer
->outbuf
->packet
.header
.len
= htons(len
);
1161 peer
->outbuf
->done
= 0;
1163 return bgp_write(peer
);
1166 /* send/buffer NOTIFICATION message */
1167 static int bgp_send_notification(struct bgp_peer
*peer
, u8 code
, u8 subcode
)
1169 struct bgp_data_notification data
;
1172 data
.error_code
= code
;
1173 len
+= sizeof(data
.error_code
);
1175 data
.error_subcode
= subcode
;
1176 len
+= sizeof(data
.error_code
);
1178 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1179 sizeof(peer
->outbuf
->packet
.header
.marker
));
1181 peer
->outbuf
->packet
.header
.type
= BGP_MSG_NOTIFICATION
;
1182 peer
->outbuf
->packet
.header
.len
=
1183 htons(sizeof(peer
->outbuf
->packet
.header
) + len
);
1185 memcpy(peer
->outbuf
->packet
.data
, &data
, len
);
1187 peer
->outbuf
->done
= 0;
1188 peer
->next_state
= code
== BGP_ERR_CEASE
? Disabled
: Idle
;
1190 /* we're dying; ignore any pending input */
1191 peer
->inbuf
->packet
.header
.len
= 0;
1192 peer
->inbuf
->done
= 0;
1194 return bgp_write(peer
);