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 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);
35 static struct bgp_route6_list
*bgp_insert_route6(struct bgp_route6_list
*head
,
36 struct bgp_route6_list
*new);
38 static void bgp_process_timers(struct bgp_peer
*peer
);
39 static void bgp_free_routes(struct bgp_route_list
*routes
);
40 static void bgp_free_routes6(struct bgp_route6_list
*routes
);
41 static char const *bgp_msg_type_str(uint8_t type
);
42 static int bgp_connect(struct bgp_peer
*peer
);
43 static int bgp_handle_connect(struct bgp_peer
*peer
);
44 static int bgp_write(struct bgp_peer
*peer
);
45 static int bgp_read(struct bgp_peer
*peer
);
46 static int bgp_handle_input(struct bgp_peer
*peer
);
47 static int bgp_send_open(struct bgp_peer
*peer
);
48 static int bgp_send_keepalive(struct bgp_peer
*peer
);
49 static int bgp_send_update(struct bgp_peer
*peer
);
50 static int bgp_send_update6(struct bgp_peer
*peer
);
51 static int bgp_send_notification(struct bgp_peer
*peer
, uint8_t code
,
54 static uint16_t our_as
;
55 static struct bgp_route_list
*bgp_routes
= 0;
56 static struct bgp_route6_list
*bgp_routes6
= 0;
58 int bgp_configured
= 0;
59 struct bgp_peer
*bgp_peers
= 0;
61 /* prepare peer structure, globals */
65 struct bgp_peer
*peer
;
67 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
70 memset(peer
, 0, sizeof(*peer
));
72 peer
->addr
= INADDR_NONE
;
74 peer
->state
= peer
->next_state
= Disabled
;
76 if (!((peer
->outbuf
= malloc(sizeof(*peer
->outbuf
)))
77 && (peer
->inbuf
= malloc(sizeof(*peer
->inbuf
)))))
79 LOG(0, 0, 0, "Can't allocate buffers for bgp peer (%s)\n",
85 peer
->edata
.type
= FD_TYPE_BGP
;
86 peer
->edata
.index
= i
;
98 bgp_configured
= 0; /* set by bgp_start */
103 /* start connection with a peer */
104 int bgp_start(struct bgp_peer
*peer
, char *name
, int as
, int keepalive
,
105 int hold
, struct in_addr update_source
, int enable
)
110 struct bgp_path_attr a
;
112 char *p
= path_attrs
;
114 uint32_t metric
= htonl(BGP_METRIC
);
115 uint32_t no_export
= htonl(BGP_COMMUNITY_NO_EXPORT
);
120 if (peer
->state
!= Disabled
)
123 snprintf(peer
->name
, sizeof(peer
->name
), "%s", name
);
125 if (!(h
= gethostbyname(name
)) || h
->h_addrtype
!= AF_INET
)
127 LOG(0, 0, 0, "Can't get address for BGP peer %s (%s)\n",
128 name
, h
? "no address" : hstrerror(h_errno
));
133 memcpy(&peer
->addr
, h
->h_addr
, sizeof(peer
->addr
));
134 peer
->source_addr
= update_source
.s_addr
;
135 peer
->as
= as
> 0 ? as
: our_as
;
136 ibgp
= peer
->as
== our_as
;
138 /* set initial timer values */
139 peer
->init_keepalive
= keepalive
== -1 ? BGP_KEEPALIVE_TIME
: keepalive
;
140 peer
->init_hold
= hold
== -1 ? BGP_HOLD_TIME
: hold
;
142 if (peer
->init_hold
< 3)
145 if (peer
->init_keepalive
* 3 > peer
->init_hold
)
146 peer
->init_keepalive
= peer
->init_hold
/ 3;
148 /* clear buffers, go to Idle state */
149 peer
->next_state
= Idle
;
152 /* set initial routing state */
153 peer
->routing
= enable
;
155 /* all our routes use the same attributes, so prepare it in advance */
156 if (peer
->path_attrs
)
157 free(peer
->path_attrs
);
159 peer
->path_attr_len
= 0;
162 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
163 a
.code
= BGP_PATH_ATTR_CODE_ORIGIN
;
165 a
.data
.s
.value
[0] = BGP_PATH_ATTR_CODE_ORIGIN_IGP
;
167 #define ADD_ATTRIBUTE() do { \
168 i = BGP_PATH_ATTR_SIZE(a); \
171 peer->path_attr_len += i; } while (0)
176 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
177 a
.code
= BGP_PATH_ATTR_CODE_AS_PATH
;
191 BGP_PATH_ATTR_CODE_AS_PATH_AS_SEQUENCE
,
196 a
.data
.s
.len
= sizeof(as_path
);
197 memcpy(&a
.data
.s
.value
, &as_path
, sizeof(as_path
));
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 /* remember the len before adding NEXT_HOP */
232 peer
->path_attr_len_without_nexthop
= peer
->path_attr_len
;
235 a
.flags
= BGP_PATH_ATTR_FLAG_TRANS
;
236 a
.code
= BGP_PATH_ATTR_CODE_NEXT_HOP
;
237 if (config
->nexthop_address
)
239 ip
= config
->nexthop_address
;
243 ip
= my_address
; /* we're it */
245 a
.data
.s
.len
= sizeof(ip
);
246 memcpy(a
.data
.s
.value
, &ip
, sizeof(ip
));
250 if (!(peer
->path_attrs
= malloc(peer
->path_attr_len
)))
252 LOG(0, 0, 0, "Can't allocate path_attrs for %s (%s)\n",
253 name
, strerror(errno
));
258 memcpy(peer
->path_attrs
, path_attrs
, peer
->path_attr_len
);
260 /* multiprotocol attributes initialization */
261 if (config
->ipv6_prefix
.s6_addr
[0])
263 struct bgp_attr_mp_reach_nlri_partial mp_reach_nlri_partial
;
264 struct bgp_attr_mp_unreach_nlri_partial mp_unreach_nlri_partial
;
266 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
;
267 a
.code
= BGP_PATH_ATTR_CODE_MP_REACH_NLRI
;
268 a
.data
.s
.len
= 0; /* will be set on UPDATE */
270 mp_reach_nlri_partial
.afi
= htons(BGP_MP_AFI_IPv6
);
271 mp_reach_nlri_partial
.safi
= BGP_MP_SAFI_UNICAST
;
272 mp_reach_nlri_partial
.reserved
= 0;
273 mp_reach_nlri_partial
.next_hop_len
= 16;
275 /* use the defined nexthop6, or our address in ipv6_prefix */
276 if (config
->nexthop6_address
.s6_addr
[0])
277 memcpy(&mp_reach_nlri_partial
.next_hop
,
278 &config
->nexthop6_address
.s6_addr
, 16);
281 /* our address is ipv6prefix::1 */
282 memcpy(&mp_reach_nlri_partial
.next_hop
,
283 &config
->ipv6_prefix
.s6_addr
, 16);
284 mp_reach_nlri_partial
.next_hop
[15] = 1;
287 memcpy(&a
.data
.s
.value
, &mp_reach_nlri_partial
,
288 sizeof(struct bgp_attr_mp_reach_nlri_partial
));
289 memcpy(&peer
->mp_reach_nlri_partial
, &a
,
290 BGP_PATH_ATTR_MP_REACH_NLRI_PARTIAL_SIZE
);
292 a
.flags
= BGP_PATH_ATTR_FLAG_OPTIONAL
| BGP_PATH_ATTR_FLAG_EXTLEN
;
293 a
.code
= BGP_PATH_ATTR_CODE_MP_UNREACH_NLRI
;
294 a
.data
.e
.len
= 0; /* will be set on UPDATE */
296 mp_unreach_nlri_partial
.afi
= htons(BGP_MP_AFI_IPv6
);
297 mp_unreach_nlri_partial
.safi
= BGP_MP_SAFI_UNICAST
;
299 memcpy(&a
.data
.e
.value
, &mp_unreach_nlri_partial
,
300 sizeof(struct bgp_attr_mp_unreach_nlri_partial
));
301 memcpy(&peer
->mp_unreach_nlri_partial
, &a
,
302 BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE
);
305 peer
->mp_handling
= HandlingUnknown
;
307 LOG(4, 0, 0, "Initiating BGP connection to %s (routing %s)\n",
308 name
, enable
? "enabled" : "suspended");
310 /* we have at least one peer configured */
314 return bgp_connect(peer
);
317 /* clear counters, timers, routes and buffers; close socket; move to
318 next_state, which may be Disabled or Idle */
319 static void bgp_clear(struct bgp_peer
*peer
)
321 if (peer
->sock
!= -1)
327 peer
->keepalive_time
= 0;
328 peer
->expire_time
= 0;
330 peer
->keepalive
= peer
->init_keepalive
;
331 peer
->hold
= peer
->init_hold
;
333 bgp_free_routes(peer
->routes
);
335 bgp_free_routes6(peer
->routes6
);
338 peer
->outbuf
->packet
.header
.len
= 0;
339 peer
->outbuf
->done
= 0;
340 peer
->inbuf
->packet
.header
.len
= 0;
341 peer
->inbuf
->done
= 0;
346 if (peer
->state
!= peer
->next_state
)
348 peer
->state
= peer
->next_state
;
349 peer
->state_time
= time_now
;
351 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
352 bgp_state_str(peer
->next_state
));
356 /* initiate a clean shutdown */
357 void bgp_stop(struct bgp_peer
*peer
)
359 LOG(4, 0, 0, "Terminating BGP connection to %s\n", peer
->name
);
360 bgp_send_notification(peer
, BGP_ERR_CEASE
, 0);
363 /* drop connection (if any) and set state to Disabled */
364 void bgp_halt(struct bgp_peer
*peer
)
366 LOG(4, 0, 0, "Aborting BGP connection to %s\n", peer
->name
);
367 peer
->next_state
= Disabled
;
371 /* drop connection (if any) and set to Idle for connection retry */
372 int bgp_restart(struct bgp_peer
*peer
)
374 peer
->next_state
= Idle
;
378 peer
->retry_time
= time_now
;
379 peer
->retry_count
= 0;
382 return bgp_connect(peer
);
385 static void bgp_set_retry(struct bgp_peer
*peer
)
387 if (peer
->retry_count
++ < BGP_MAX_RETRY
)
389 peer
->retry_time
= time_now
+ (BGP_RETRY_BACKOFF
* peer
->retry_count
);
390 peer
->next_state
= Idle
;
394 bgp_halt(peer
); /* give up */
397 /* convert ip/mask to CIDR notation */
398 static void bgp_cidr(in_addr_t ip
, in_addr_t mask
, struct bgp_ip_prefix
*pfx
)
403 /* convert to prefix notation */
407 if (!mask
) /* bogus */
410 for (i
= 0; i
< 32 && ((b
= ntohl(1 << i
)), !(mask
& b
)); i
++)
417 /* insert route into list; sorted */
418 static struct bgp_route_list
*bgp_insert_route(struct bgp_route_list
*head
,
419 struct bgp_route_list
*new)
421 struct bgp_route_list
*p
= head
;
422 struct bgp_route_list
*e
= 0;
424 while (p
&& memcmp(&p
->dest
, &new->dest
, sizeof(p
->dest
)) < 0)
444 /* insert route6 into list; sorted */
445 static struct bgp_route6_list
*bgp_insert_route6(struct bgp_route6_list
*head
,
446 struct bgp_route6_list
*new)
448 struct bgp_route6_list
*p
= head
;
449 struct bgp_route6_list
*e
= 0;
451 while (p
&& memcmp(&p
->dest
, &new->dest
, sizeof(p
->dest
)) < 0)
471 /* add route to list for peers */
473 * Note: this doesn't do route aggregation, nor drop routes if a less
474 * specific match already exists (partly because I'm lazy, but also so
475 * that if that route is later deleted we don't have to be concerned
476 * about adding back the more specific one).
478 int bgp_add_route(in_addr_t ip
, in_addr_t mask
)
480 struct bgp_route_list
*r
= bgp_routes
;
481 struct bgp_route_list add
;
484 bgp_cidr(ip
, mask
, &add
.dest
);
487 /* check for duplicate */
490 i
= memcmp(&r
->dest
, &add
.dest
, sizeof(r
->dest
));
492 return 1; /* already covered */
500 /* insert into route list; sorted */
501 if (!(r
= malloc(sizeof(*r
))))
503 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
504 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
, strerror(errno
));
509 memcpy(r
, &add
, sizeof(*r
));
510 bgp_routes
= bgp_insert_route(bgp_routes
, r
);
512 /* flag established peers for update */
513 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
514 if (bgp_peers
[i
].state
== Established
)
515 bgp_peers
[i
].update_routes
= 1;
517 LOG(4, 0, 0, "Registered BGP route %s/%d\n",
518 fmtaddr(add
.dest
.prefix
, 0), add
.dest
.len
);
523 /* add route to list for peers */
525 * Note: same provisions as above
527 int bgp_add_route6(struct in6_addr ip
, int prefixlen
)
529 struct bgp_route6_list
*r
= bgp_routes6
;
530 struct bgp_route6_list add
;
532 char ipv6addr
[INET6_ADDRSTRLEN
];
534 memcpy(&add
.dest
.prefix
, &ip
.s6_addr
, 16);
535 add
.dest
.len
= prefixlen
;
538 /* check for duplicate */
541 i
= memcmp(&r
->dest
, &add
.dest
, sizeof(r
->dest
));
543 return 1; /* already covered */
551 /* insert into route list; sorted */
552 if (!(r
= malloc(sizeof(*r
))))
554 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
555 inet_ntop(AF_INET6
, &ip
, ipv6addr
, INET6_ADDRSTRLEN
), add
.dest
.len
,
561 memcpy(r
, &add
, sizeof(*r
));
562 bgp_routes6
= bgp_insert_route6(bgp_routes6
, r
);
564 /* flag established peers for update */
565 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
566 if (bgp_peers
[i
].state
== Established
567 && bgp_peers
[i
].mp_handling
== HandleIPv6Routes
)
568 bgp_peers
[i
].update_routes6
= 1;
570 LOG(4, 0, 0, "Registered BGP route %s/%d\n",
571 inet_ntop(AF_INET6
, &ip
, ipv6addr
, INET6_ADDRSTRLEN
), add
.dest
.len
);
576 /* remove route from list for peers */
577 int bgp_del_route(in_addr_t ip
, in_addr_t mask
)
579 struct bgp_route_list
*r
= bgp_routes
;
580 struct bgp_route_list
*e
= 0;
581 struct bgp_route_list del
;
584 bgp_cidr(ip
, mask
, &del
.dest
);
587 /* find entry in routes list and remove */
590 i
= memcmp(&r
->dest
, &del
.dest
, sizeof(r
->dest
));
596 bgp_routes
= r
->next
;
614 /* flag established peers for update */
615 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
616 if (bgp_peers
[i
].state
== Established
)
617 bgp_peers
[i
].update_routes
= 1;
619 LOG(4, 0, 0, "Removed BGP route %s/%d\n",
620 fmtaddr(del
.dest
.prefix
, 0), del
.dest
.len
);
625 /* remove route from list for peers */
626 int bgp_del_route6(struct in6_addr ip
, int prefixlen
)
628 struct bgp_route6_list
*r
= bgp_routes6
;
629 struct bgp_route6_list
*e
= 0;
630 struct bgp_route6_list del
;
632 char ipv6addr
[INET6_ADDRSTRLEN
];
634 memcpy(&del
.dest
.prefix
, &ip
.s6_addr
, 16);
635 del
.dest
.len
= prefixlen
;
638 /* find entry in routes list and remove */
641 i
= memcmp(&r
->dest
, &del
.dest
, sizeof(r
->dest
));
647 bgp_routes6
= r
->next
;
665 /* flag established peers for update */
666 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
667 if (bgp_peers
[i
].state
== Established
668 && bgp_peers
[i
].mp_handling
== HandleIPv6Routes
)
669 bgp_peers
[i
].update_routes6
= 1;
671 LOG(4, 0, 0, "Removed BGP route %s/%d\n",
672 inet_ntop(AF_INET6
, &ip
, ipv6addr
, INET6_ADDRSTRLEN
), del
.dest
.len
);
677 /* enable or disable routing */
678 void bgp_enable_routing(int enable
)
682 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
684 bgp_peers
[i
].routing
= enable
;
686 /* flag established peers for update */
687 if (bgp_peers
[i
].state
== Established
)
688 bgp_peers
[i
].update_routes
= 1;
691 LOG(4, 0, 0, "%s BGP routing\n", enable
? "Enabled" : "Suspended");
695 # include <sys/epoll.h>
697 # include "fake_epoll.h"
700 /* return a bitmask of the events required to poll this peer's fd */
708 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
710 struct bgp_peer
*peer
= &bgp_peers
[i
];
713 if (peer
->state
== Disabled
|| peer
->state
== Idle
)
716 if (peer
->inbuf
->done
< BGP_MAX_PACKET_SIZE
)
719 if (peer
->state
== Connect
|| /* connection in progress */
720 peer
->update_routes
|| /* routing updates */
721 peer
->outbuf
->packet
.header
.len
) /* pending output */
724 if (peer
->events
!= events
)
726 struct epoll_event ev
;
728 ev
.events
= peer
->events
= events
;
729 ev
.data
.ptr
= &peer
->edata
;
730 epoll_ctl(epollfd
, EPOLL_CTL_MOD
, peer
->sock
, &ev
);
737 /* process bgp events/timers */
738 int bgp_process(uint32_t events
[])
745 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
747 struct bgp_peer
*peer
= &bgp_peers
[i
];
749 if (*peer
->name
&& peer
->cli_flag
== BGP_CLI_RESTART
)
755 if (peer
->state
== Disabled
)
760 switch (peer
->cli_flag
)
762 case BGP_CLI_SUSPEND
:
766 if (peer
->state
== Established
)
767 peer
->update_routes
= 1;
776 if (peer
->state
== Established
)
777 peer
->update_routes
= 1;
786 /* handle empty/fill of buffers */
787 if (events
[i
] & EPOLLOUT
)
790 if (peer
->state
== Connect
)
791 r
= bgp_handle_connect(peer
);
792 else if (peer
->outbuf
->packet
.header
.len
)
799 if (events
[i
] & (EPOLLIN
|EPOLLHUP
))
805 /* process input buffer contents */
806 while (peer
->inbuf
->done
>= sizeof(peer
->inbuf
->packet
.header
)
807 && !peer
->outbuf
->packet
.header
.len
) /* may need to queue a response */
809 if (bgp_handle_input(peer
) < 0)
813 /* process pending updates */
814 if (peer
->update_routes
815 && !peer
->outbuf
->packet
.header
.len
) /* ditto */
817 if (!bgp_send_update(peer
))
821 /* process pending IPv6 updates */
822 if (peer
->update_routes6
823 && !peer
->outbuf
->packet
.header
.len
) /* ditto */
825 if (!bgp_send_update6(peer
))
830 bgp_process_timers(peer
);
836 /* process bgp timers only */
837 void bgp_process_peers_timers()
844 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
846 struct bgp_peer
*peer
= &bgp_peers
[i
];
848 if (peer
->state
== Disabled
)
851 bgp_process_timers(peer
);
855 static void bgp_process_timers(struct bgp_peer
*peer
)
857 if (peer
->state
== Established
)
859 if (time_now
> peer
->expire_time
)
861 LOG(1, 0, 0, "No message from BGP peer %s in %ds\n",
862 peer
->name
, peer
->hold
);
864 bgp_send_notification(peer
, BGP_ERR_HOLD_TIMER_EXP
, 0);
868 if (time_now
> peer
->keepalive_time
&& !peer
->outbuf
->packet
.header
.len
)
869 bgp_send_keepalive(peer
);
871 else if (peer
->state
== Idle
)
873 if (time_now
> peer
->retry_time
)
876 else if (time_now
> peer
->state_time
+ BGP_STATE_TIME
)
878 LOG(1, 0, 0, "%s timer expired for BGP peer %s\n",
879 bgp_state_str(peer
->state
), peer
->name
);
885 static void bgp_free_routes(struct bgp_route_list
*routes
)
887 struct bgp_route_list
*tmp
;
889 while ((tmp
= routes
))
896 static void bgp_free_routes6(struct bgp_route6_list
*routes
)
898 struct bgp_route6_list
*tmp
;
900 while ((tmp
= routes
))
907 char const *bgp_state_str(enum bgp_state state
)
911 case Disabled
: return "Disabled";
912 case Idle
: return "Idle";
913 case Connect
: return "Connect";
914 case Active
: return "Active";
915 case OpenSent
: return "OpenSent";
916 case OpenConfirm
: return "OpenConfirm";
917 case Established
: return "Established";
923 static char const *bgp_msg_type_str(uint8_t type
)
927 case BGP_MSG_OPEN
: return "OPEN";
928 case BGP_MSG_UPDATE
: return "UPDATE";
929 case BGP_MSG_NOTIFICATION
: return "NOTIFICATION";
930 case BGP_MSG_KEEPALIVE
: return "KEEPALIVE";
936 /* attempt to connect to peer */
937 static int bgp_connect(struct bgp_peer
*peer
)
939 static int bgp_port
= 0;
940 struct sockaddr_in addr
;
941 struct sockaddr_in source_addr
;
942 struct epoll_event ev
;
946 struct servent
*serv
;
947 if (!(serv
= getservbyname("bgp", "tcp")))
949 LOG(0, 0, 0, "Can't get bgp service (%s)\n", strerror(errno
));
953 bgp_port
= serv
->s_port
;
956 if ((peer
->sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
958 LOG(0, 0, 0, "Can't create a socket for BGP peer %s (%s)\n",
959 peer
->name
, strerror(errno
));
961 peer
->state
= peer
->next_state
= Disabled
;
965 /* add to poll set */
966 ev
.events
= peer
->events
= EPOLLOUT
;
967 ev
.data
.ptr
= &peer
->edata
;
968 epoll_ctl(epollfd
, EPOLL_CTL_ADD
, peer
->sock
, &ev
);
970 /* set to non-blocking */
971 fcntl(peer
->sock
, F_SETFL
, fcntl(peer
->sock
, F_GETFL
, 0) | O_NONBLOCK
);
973 /* set source address */
974 memset(&source_addr
, 0, sizeof(source_addr
));
975 source_addr
.sin_family
= AF_INET
;
976 source_addr
.sin_addr
.s_addr
= peer
->source_addr
; /* defaults to INADDR_ANY */
977 if (bind(peer
->sock
, (struct sockaddr
*) &source_addr
, sizeof(source_addr
)) < 0)
979 LOG(1, 0, 0, "Can't set source address to %s: %s\n",
980 inet_ntoa(source_addr
.sin_addr
), strerror(errno
));
987 memset(&addr
, 0, sizeof(addr
));
988 addr
.sin_family
= AF_INET
;
989 addr
.sin_port
= bgp_port
;
990 addr
.sin_addr
.s_addr
= peer
->addr
;
992 while (connect(peer
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1)
994 if (errno
== EINTR
) /* SIGALARM handler */
997 if (errno
!= EINPROGRESS
)
999 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n",
1000 inet_ntoa(addr
.sin_addr
), strerror(errno
));
1002 bgp_set_retry(peer
);
1006 peer
->state
= Connect
;
1007 peer
->state_time
= time_now
;
1009 LOG(4, 0, 0, "BGP peer %s: state Connect\n", peer
->name
);
1013 peer
->state
= Active
;
1014 peer
->state_time
= time_now
;
1015 peer
->retry_time
= peer
->retry_count
= 0;
1017 LOG(4, 0, 0, "BGP peer %s: state Active\n", inet_ntoa(addr
.sin_addr
));
1019 return bgp_send_open(peer
);
1022 /* complete partial connection (state = Connect) */
1023 static int bgp_handle_connect(struct bgp_peer
*peer
)
1026 socklen_t len
= sizeof(int);
1027 getsockopt(peer
->sock
, SOL_SOCKET
, SO_ERROR
, &err
, &len
);
1030 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer
->name
,
1033 bgp_set_retry(peer
);
1037 peer
->state
= Active
;
1038 peer
->state_time
= time_now
;
1040 LOG(4, 0, 0, "BGP peer %s: state Active\n", peer
->name
);
1042 return bgp_send_open(peer
);
1045 /* initiate a write */
1046 static int bgp_write(struct bgp_peer
*peer
)
1048 int len
= htons(peer
->outbuf
->packet
.header
.len
);
1051 while ((r
= write(peer
->sock
, &peer
->outbuf
->packet
+ peer
->outbuf
->done
,
1052 len
- peer
->outbuf
->done
)) == -1)
1057 if (errno
== EAGAIN
)
1061 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
1063 LOG(1, 0, 0, "Can't write to BGP peer %s (%s)\n", peer
->name
,
1066 bgp_set_retry(peer
);
1072 peer
->outbuf
->done
+= r
;
1076 LOG(4, 0, 0, "Sent %s to BGP peer %s\n",
1077 bgp_msg_type_str(peer
->outbuf
->packet
.header
.type
), peer
->name
);
1079 peer
->outbuf
->packet
.header
.len
= 0;
1080 peer
->outbuf
->done
= 0;
1082 if (peer
->state
== Established
)
1083 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
1085 if (peer
->state
!= peer
->next_state
)
1087 if (peer
->next_state
== Disabled
|| peer
->next_state
== Idle
)
1093 peer
->state
= peer
->next_state
;
1094 peer
->state_time
= time_now
;
1096 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer
->name
,
1097 bgp_state_str(peer
->state
));
1103 /* initiate a read */
1104 static int bgp_read(struct bgp_peer
*peer
)
1108 while ((r
= read(peer
->sock
, &peer
->inbuf
->packet
+ peer
->inbuf
->done
,
1109 BGP_MAX_PACKET_SIZE
- peer
->inbuf
->done
)) < 1)
1113 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer
->name
);
1120 if (errno
== EAGAIN
)
1123 LOG(1, 0, 0, "Can't read from BGP peer %s (%s)\n", peer
->name
,
1127 bgp_set_retry(peer
);
1131 peer
->inbuf
->done
+= r
;
1135 /* process buffered packets */
1136 static int bgp_handle_input(struct bgp_peer
*peer
)
1138 struct bgp_packet
*p
= &peer
->inbuf
->packet
;
1139 int len
= ntohs(p
->header
.len
);
1141 if (len
> BGP_MAX_PACKET_SIZE
)
1143 LOG(1, 0, 0, "Bad header length from BGP %s\n", peer
->name
);
1144 bgp_send_notification(peer
, BGP_ERR_HEADER
, BGP_ERR_HDR_BAD_LEN
);
1148 if (peer
->inbuf
->done
< len
)
1151 LOG(4, 0, 0, "Received %s from BGP peer %s\n",
1152 bgp_msg_type_str(p
->header
.type
), peer
->name
);
1154 switch (p
->header
.type
)
1158 struct bgp_data_open data
;
1161 off_t param_offset
, capability_offset
;
1162 struct bgp_opt_param
*param
;
1163 uint8_t capabilities_len
;
1164 char *capabilities
= NULL
;
1165 struct bgp_capability
*capability
;
1166 struct bgp_mp_cap_param
*mp_cap
;
1168 for (i
= 0; i
< sizeof(p
->header
.marker
); i
++)
1170 if ((unsigned char) p
->header
.marker
[i
] != 0xff)
1172 LOG(1, 0, 0, "Invalid marker from BGP peer %s\n",
1175 bgp_send_notification(peer
, BGP_ERR_HEADER
,
1176 BGP_ERR_HDR_NOT_SYNC
);
1182 if (peer
->state
!= OpenSent
)
1184 LOG(1, 0, 0, "OPEN from BGP peer %s in %s state\n",
1185 peer
->name
, bgp_state_str(peer
->state
));
1187 bgp_send_notification(peer
, BGP_ERR_FSM
, 0);
1191 memcpy(&data
, p
->data
, len
- sizeof(p
->header
));
1193 if (data
.version
!= BGP_VERSION
)
1195 LOG(1, 0, 0, "Bad version (%d) sent by BGP peer %s\n",
1196 (int) data
.version
, peer
->name
);
1198 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_VERSION
);
1202 if (ntohs(data
.as
) != peer
->as
)
1204 LOG(1, 0, 0, "Bad AS sent by BGP peer %s (got %d, "
1205 "expected %d)\n", peer
->name
, (int) htons(data
.as
),
1208 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_BAD_AS
);
1212 if ((hold
= ntohs(data
.hold_time
)) < 3)
1214 LOG(1, 0, 0, "Bad hold time (%d) from BGP peer %s\n",
1217 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_HOLD_TIME
);
1221 /* pick lowest hold time */
1222 if (hold
< peer
->hold
)
1225 /* adjust our keepalive based on negotiated hold value */
1226 if (peer
->keepalive
* 3 > peer
->hold
)
1227 peer
->keepalive
= peer
->hold
/ 3;
1229 /* check for optional parameters */
1230 /* 2 is for the size of type + len (both uint8_t) */
1231 for (param_offset
= 0;
1232 param_offset
< data
.opt_len
;
1233 param_offset
+= 2 + param
->len
)
1235 param
= (struct bgp_opt_param
*)((char *)&data
.opt_params
+ param_offset
);
1237 /* sensible check */
1238 if (data
.opt_len
- param_offset
< 2
1239 || param
->len
> data
.opt_len
- param_offset
- 2)
1241 LOG(1, 0, 0, "Malformed Optional Parameter list from BGP peer %s\n",
1244 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_UNSPEC
);
1248 /* we know only one parameter type */
1249 if (param
->type
!= BGP_PARAM_TYPE_CAPABILITY
)
1251 LOG(1, 0, 0, "Unsupported Optional Parameter type %d from BGP peer %s\n",
1252 param
->type
, peer
->name
);
1254 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_OPN_UNSUP_PARAM
);
1258 capabilities_len
= param
->len
;
1259 capabilities
= (char *)¶m
->value
;
1261 /* look for BGP multiprotocol capability */
1262 for (capability_offset
= 0;
1263 capability_offset
< capabilities_len
;
1264 capability_offset
+= 2 + capability
->len
)
1266 capability
= (struct bgp_capability
*)(capabilities
+ capability_offset
);
1268 /* sensible check */
1269 if (capabilities_len
- capability_offset
< 2
1270 || capability
->len
> capabilities_len
- capability_offset
- 2)
1272 LOG(1, 0, 0, "Malformed Capabilities list from BGP peer %s\n",
1275 bgp_send_notification(peer
, BGP_ERR_OPEN
, BGP_ERR_UNSPEC
);
1279 /* we only know one capability code */
1280 if (capability
->code
!= BGP_CAP_CODE_MP
1281 && capability
->len
!= sizeof(struct bgp_mp_cap_param
))
1283 LOG(4, 0, 0, "Unsupported Capability code %d from BGP peer %s\n",
1284 capability
->code
, peer
->name
);
1286 /* we don't terminate, still; we just jump to the next one */
1290 mp_cap
= (struct bgp_mp_cap_param
*)&capability
->value
;
1291 /* the only <AFI, SAFI> tuple we support */
1292 if (ntohs(mp_cap
->afi
) != BGP_MP_AFI_IPv6
&& mp_cap
->safi
!= BGP_MP_SAFI_UNICAST
)
1294 LOG(4, 0, 0, "Unsupported multiprotocol AFI %d and SAFI %d from BGP peer %s\n",
1295 mp_cap
->afi
, mp_cap
->safi
, peer
->name
);
1297 /* we don't terminate, still; we just jump to the next one */
1302 peer
->mp_handling
= HandleIPv6Routes
;
1306 if (peer
->mp_handling
!= HandleIPv6Routes
)
1308 peer
->mp_handling
= DoesntHandleIPv6Routes
;
1309 if (config
->ipv6_prefix
.s6_addr
[0])
1310 LOG(1, 0, 0, "Warning: BGP peer %s doesn't handle IPv6 prefixes updates\n",
1314 /* next transition requires an exchange of keepalives */
1315 bgp_send_keepalive(peer
);
1320 case BGP_MSG_KEEPALIVE
:
1321 if (peer
->state
== OpenConfirm
)
1323 peer
->state
= peer
->next_state
= Established
;
1324 peer
->state_time
= time_now
;
1325 peer
->keepalive_time
= time_now
+ peer
->keepalive
;
1326 peer
->update_routes
= 1;
1327 peer
->retry_count
= 0;
1328 peer
->retry_time
= 0;
1330 LOG(4, 0, 0, "BGP peer %s: state Established\n", peer
->name
);
1335 case BGP_MSG_NOTIFICATION
:
1336 if (len
> sizeof(p
->header
))
1338 struct bgp_data_notification
*notification
=
1339 (struct bgp_data_notification
*) p
->data
;
1341 if (notification
->error_code
== BGP_ERR_CEASE
)
1343 LOG(4, 0, 0, "BGP peer %s sent CEASE\n", peer
->name
);
1344 bgp_set_retry(peer
);
1348 if (notification
->error_code
== BGP_ERR_OPEN
1349 && notification
->error_subcode
== BGP_ERR_OPN_UNSUP_PARAM
)
1351 LOG(4, 0, 0, "BGP peer %s doesn't support BGP Capabilities\n", peer
->name
);
1352 peer
->mp_handling
= DoesntHandleIPv6Routes
;
1353 bgp_set_retry(peer
);
1357 if (notification
->error_code
== BGP_ERR_OPEN
1358 && notification
->error_subcode
== BGP_ERR_OPN_UNSUP_CAP
)
1360 /* the only capability we advertise is this one, so upon receiving
1361 an "unsupported capability" message, we disable IPv6 routes for
1363 LOG(4, 0, 0, "BGP peer %s doesn't support IPv6 routes advertisement\n", peer
->name
);
1364 peer
->mp_handling
= DoesntHandleIPv6Routes
;
1368 /* FIXME: should handle more notifications */
1369 LOG(4, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n",
1370 peer
->name
, (int) notification
->error_code
);
1377 peer
->expire_time
= time_now
+ peer
->hold
;
1379 /* see if there's another message in the same packet/buffer */
1380 if (peer
->inbuf
->done
> len
)
1382 peer
->inbuf
->done
-= len
;
1383 memmove(p
, (char *) p
+ len
, peer
->inbuf
->done
);
1387 peer
->inbuf
->packet
.header
.len
= 0;
1388 peer
->inbuf
->done
= 0;
1391 return peer
->inbuf
->done
;
1394 /* send/buffer OPEN message */
1395 static int bgp_send_open(struct bgp_peer
*peer
)
1397 struct bgp_data_open data
;
1398 struct bgp_mp_cap_param mp_ipv6
= { htons(BGP_MP_AFI_IPv6
), 0, BGP_MP_SAFI_UNICAST
};
1399 struct bgp_capability cap_mp_ipv6
;
1400 struct bgp_opt_param param_cap_mp_ipv6
;
1401 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1403 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1404 sizeof(peer
->outbuf
->packet
.header
.marker
));
1406 peer
->outbuf
->packet
.header
.type
= BGP_MSG_OPEN
;
1408 data
.version
= BGP_VERSION
;
1409 data
.as
= htons(our_as
);
1410 data
.hold_time
= htons(peer
->hold
);
1411 /* use the source IP we use as identifier, if available */
1412 if (peer
->source_addr
!= INADDR_ANY
)
1413 data
.identifier
= peer
->source_addr
;
1415 data
.identifier
= my_address
;
1417 /* if we know peer doesn't support MP (mp_handling == DoesntHandleIPv6Routes)
1418 then don't add this parameter */
1419 if (config
->ipv6_prefix
.s6_addr
[0]
1420 && (peer
->mp_handling
== HandlingUnknown
1421 || peer
->mp_handling
== HandleIPv6Routes
))
1423 /* construct the param and capability */
1424 cap_mp_ipv6
.code
= BGP_CAP_CODE_MP
;
1425 cap_mp_ipv6
.len
= sizeof(mp_ipv6
);
1426 memcpy(&cap_mp_ipv6
.value
, &mp_ipv6
, cap_mp_ipv6
.len
);
1428 param_cap_mp_ipv6
.type
= BGP_PARAM_TYPE_CAPABILITY
;
1429 param_cap_mp_ipv6
.len
= 2 + sizeof(mp_ipv6
);
1430 memcpy(¶m_cap_mp_ipv6
.value
, &cap_mp_ipv6
, param_cap_mp_ipv6
.len
);
1432 data
.opt_len
= 2 + param_cap_mp_ipv6
.len
;
1433 memcpy(&data
.opt_params
, ¶m_cap_mp_ipv6
, data
.opt_len
);
1438 memcpy(peer
->outbuf
->packet
.data
, &data
, BGP_DATA_OPEN_SIZE
+ data
.opt_len
);
1439 len
+= BGP_DATA_OPEN_SIZE
+ data
.opt_len
;
1441 peer
->outbuf
->packet
.header
.len
= htons(len
);
1442 peer
->outbuf
->done
= 0;
1443 peer
->next_state
= OpenSent
;
1445 return bgp_write(peer
);
1448 /* send/buffer KEEPALIVE message */
1449 static int bgp_send_keepalive(struct bgp_peer
*peer
)
1451 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1452 sizeof(peer
->outbuf
->packet
.header
.marker
));
1454 peer
->outbuf
->packet
.header
.type
= BGP_MSG_KEEPALIVE
;
1455 peer
->outbuf
->packet
.header
.len
=
1456 htons(sizeof(peer
->outbuf
->packet
.header
));
1458 peer
->outbuf
->done
= 0;
1459 peer
->next_state
= (peer
->state
== OpenSent
) ? OpenConfirm
: peer
->state
;
1461 return bgp_write(peer
);
1464 /* send/buffer UPDATE message */
1465 static int bgp_send_update(struct bgp_peer
*peer
)
1467 uint16_t unf_len
= 0;
1469 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1470 struct bgp_route_list
*have
= peer
->routes
;
1471 struct bgp_route_list
*want
= peer
->routing
? bgp_routes
: 0;
1472 struct bgp_route_list
*e
= 0;
1473 struct bgp_route_list
*add
= 0;
1476 char *data
= (char *) &peer
->outbuf
->packet
.data
;
1478 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1479 char *max
= (char *) &peer
->outbuf
->packet
.data
1480 + sizeof(peer
->outbuf
->packet
.data
)
1481 - sizeof(attr_len
) - peer
->path_attr_len
- sizeof(struct bgp_ip_prefix
);
1483 /* skip over unf_len */
1484 data
+= sizeof(unf_len
);
1485 len
+= sizeof(unf_len
);
1487 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1488 sizeof(peer
->outbuf
->packet
.header
.marker
));
1490 peer
->outbuf
->packet
.header
.type
= BGP_MSG_UPDATE
;
1492 peer
->update_routes
= 0; /* tentatively clear */
1494 /* find differences */
1495 while ((have
|| want
) && data
< (max
- sizeof(struct bgp_ip_prefix
)))
1499 ? memcmp(&have
->dest
, &want
->dest
, sizeof(have
->dest
))
1504 if (s
< 0) /* found one to delete */
1506 struct bgp_route_list
*tmp
= have
;
1509 s
= BGP_IP_PREFIX_SIZE(tmp
->dest
);
1510 memcpy(data
, &tmp
->dest
, s
);
1515 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1516 fmtaddr(tmp
->dest
.prefix
, 0), tmp
->dest
.len
, peer
->name
);
1523 peer
->routes
= have
;
1529 e
= have
; /* stash the last found to relink above */
1533 else if (s
> 0) /* addition reqd. */
1537 peer
->update_routes
= 1; /* only one add per packet */
1551 peer
->update_routes
= 1; /* more to do */
1553 /* anything changed? */
1554 if (!(unf_len
|| add
))
1557 /* go back and insert unf_len */
1558 unf_len
= htons(unf_len
);
1559 memcpy(&peer
->outbuf
->packet
.data
, &unf_len
, sizeof(unf_len
));
1563 if (!(e
= malloc(sizeof(*e
))))
1565 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1566 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, strerror(errno
));
1571 memcpy(e
, add
, sizeof(*e
));
1573 peer
->routes
= bgp_insert_route(peer
->routes
, e
);
1575 attr_len
= htons(peer
->path_attr_len
);
1576 memcpy(data
, &attr_len
, sizeof(attr_len
));
1577 data
+= sizeof(attr_len
);
1578 len
+= sizeof(attr_len
);
1580 memcpy(data
, peer
->path_attrs
, peer
->path_attr_len
);
1581 data
+= peer
->path_attr_len
;
1582 len
+= peer
->path_attr_len
;
1584 s
= BGP_IP_PREFIX_SIZE(add
->dest
);
1585 memcpy(data
, &add
->dest
, s
);
1589 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1590 fmtaddr(add
->dest
.prefix
, 0), add
->dest
.len
, peer
->name
);
1595 memcpy(data
, &attr_len
, sizeof(attr_len
));
1596 data
+= sizeof(attr_len
);
1597 len
+= sizeof(attr_len
);
1600 peer
->outbuf
->packet
.header
.len
= htons(len
);
1601 peer
->outbuf
->done
= 0;
1603 return bgp_write(peer
);
1606 /* send/buffer UPDATE message for IPv6 routes */
1607 static int bgp_send_update6(struct bgp_peer
*peer
)
1610 uint16_t unreach_len
= 0;
1611 char *unreach_len_pos
;
1613 uint16_t len
= sizeof(peer
->outbuf
->packet
.header
);
1614 struct bgp_route6_list
*have
= peer
->routes6
;
1615 struct bgp_route6_list
*want
= peer
->routing
? bgp_routes6
: 0;
1616 struct bgp_route6_list
*e
= 0;
1617 struct bgp_route6_list
*add
= 0;
1619 char ipv6addr
[INET6_ADDRSTRLEN
];
1621 char *data
= (char *) &peer
->outbuf
->packet
.data
;
1623 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1624 char *max
= (char *) &peer
->outbuf
->packet
.data
1625 + sizeof(peer
->outbuf
->packet
.data
)
1626 - sizeof(attr_len
) - peer
->path_attr_len_without_nexthop
1627 - BGP_PATH_ATTR_MP_REACH_NLRI_PARTIAL_SIZE
- sizeof(struct bgp_ip6_prefix
);
1629 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1630 sizeof(peer
->outbuf
->packet
.header
.marker
));
1632 peer
->outbuf
->packet
.header
.type
= BGP_MSG_UPDATE
;
1634 /* insert non-MP unfeasible routes length */
1635 memcpy(data
, &unreach_len
, sizeof(unreach_len
));
1636 /* skip over it and attr_len too; it will be filled when known */
1637 data
+= sizeof(unreach_len
) + sizeof(attr_len
);
1638 len
+= sizeof(unreach_len
) + sizeof(attr_len
);
1640 /* copy usual attributes */
1641 memcpy(data
, peer
->path_attrs
, peer
->path_attr_len_without_nexthop
);
1642 data
+= peer
->path_attr_len_without_nexthop
;
1643 attr_len
= peer
->path_attr_len_without_nexthop
;
1645 /* copy MP unreachable NLRI heading */
1646 memcpy(data
, peer
->mp_unreach_nlri_partial
,
1647 BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE
);
1648 /* remember where to update this attr len */
1649 unreach_len_pos
= data
+ 2;
1650 data
+= BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE
;
1651 attr_len
+= BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE
;
1653 peer
->update_routes6
= 0; /* tentatively clear */
1655 /* find differences */
1656 while ((have
|| want
) && data
< (max
- sizeof(struct bgp_ip6_prefix
)))
1660 ? memcmp(&have
->dest
, &want
->dest
, sizeof(have
->dest
))
1665 if (s
< 0) /* found one to delete */
1667 struct bgp_route6_list
*tmp
= have
;
1670 s
= BGP_IP_PREFIX_SIZE(tmp
->dest
);
1671 memcpy(data
, &tmp
->dest
, s
);
1676 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1677 inet_ntop(AF_INET6
, &tmp
->dest
.prefix
, ipv6addr
, INET6_ADDRSTRLEN
),
1678 tmp
->dest
.len
, peer
->name
);
1685 peer
->routes6
= have
;
1691 e
= have
; /* stash the last found to relink above */
1695 else if (s
> 0) /* addition reqd. */
1699 peer
->update_routes6
= 1; /* only one add per packet */
1713 peer
->update_routes6
= 1; /* more to do */
1715 /* anything changed? */
1716 if (!(unreach_len
|| add
))
1721 /* go back and insert MP unreach_len */
1722 unreach_len
+= sizeof(struct bgp_attr_mp_unreach_nlri_partial
);
1723 unreach_len
= htons(unreach_len
);
1724 memcpy(unreach_len_pos
, &unreach_len
, sizeof(unreach_len
));
1728 /* we can remove this attribute, then */
1729 data
-= BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE
;
1730 attr_len
-= BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE
;
1735 if (!(e
= malloc(sizeof(*e
))))
1737 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1738 inet_ntop(AF_INET6
, &add
->dest
.prefix
, ipv6addr
, INET6_ADDRSTRLEN
),
1739 add
->dest
.len
, strerror(errno
));
1744 memcpy(e
, add
, sizeof(*e
));
1746 peer
->routes6
= bgp_insert_route6(peer
->routes6
, e
);
1748 /* copy MP reachable NLRI heading */
1749 memcpy(data
, peer
->mp_reach_nlri_partial
,
1750 BGP_PATH_ATTR_MP_REACH_NLRI_PARTIAL_SIZE
);
1751 /* with proper len */
1752 reach_len
= BGP_IP_PREFIX_SIZE(add
->dest
);
1753 data
[2] = sizeof(struct bgp_attr_mp_reach_nlri_partial
) + reach_len
;
1754 data
+= BGP_PATH_ATTR_MP_REACH_NLRI_PARTIAL_SIZE
;
1755 attr_len
+= BGP_PATH_ATTR_MP_REACH_NLRI_PARTIAL_SIZE
;
1757 memcpy(data
, &add
->dest
, reach_len
);
1759 attr_len
+= reach_len
;
1761 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1762 inet_ntop(AF_INET6
, &add
->dest
.prefix
, ipv6addr
, INET6_ADDRSTRLEN
),
1763 add
->dest
.len
, peer
->name
);
1766 /* update len with attributes we added */
1769 /* go back and insert attr_len */
1770 attr_len
= htons(attr_len
);
1771 memcpy((char *)&peer
->outbuf
->packet
.data
+ 2, &attr_len
, sizeof(attr_len
));
1773 peer
->outbuf
->packet
.header
.len
= htons(len
);
1774 peer
->outbuf
->done
= 0;
1776 return bgp_write(peer
);
1779 /* send/buffer NOTIFICATION message */
1780 static int bgp_send_notification(struct bgp_peer
*peer
, uint8_t code
,
1783 struct bgp_data_notification data
;
1786 data
.error_code
= code
;
1787 len
+= sizeof(data
.error_code
);
1789 data
.error_subcode
= subcode
;
1790 len
+= sizeof(data
.error_code
);
1792 memset(peer
->outbuf
->packet
.header
.marker
, 0xff,
1793 sizeof(peer
->outbuf
->packet
.header
.marker
));
1795 peer
->outbuf
->packet
.header
.type
= BGP_MSG_NOTIFICATION
;
1796 peer
->outbuf
->packet
.header
.len
=
1797 htons(sizeof(peer
->outbuf
->packet
.header
) + len
);
1799 memcpy(peer
->outbuf
->packet
.data
, &data
, len
);
1801 peer
->outbuf
->done
= 0;
1802 peer
->next_state
= code
== BGP_ERR_CEASE
? Disabled
: Idle
;
1804 /* we're dying; ignore any pending input */
1805 peer
->inbuf
->packet
.header
.len
= 0;
1806 peer
->inbuf
->done
= 0;
1808 return bgp_write(peer
);