Merge branch 'rfc4760' into fdn-mods
[l2tpns.git] / bgp.c
1 /*
2 * BGPv4
3 * Used to advertise routes for upstream (l2tp port, rather than gratiutious
4 * arp) and downstream--allowing routers to load-balance both.
5 *
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).
11 */
12
13 char const *cvs_id_bgp = "$Id: bgp.c,v 1.12 2005/09/02 23:39:36 bodea Exp $";
14
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <time.h>
19 #include <errno.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <netdb.h>
24 #include <fcntl.h>
25
26 #include "l2tpns.h"
27 #include "bgp.h"
28 #include "util.h"
29
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);
37
38 static void bgp_free_routes(struct bgp_route_list *routes);
39 static void bgp_free_routes6(struct bgp_route6_list *routes);
40 static char const *bgp_msg_type_str(uint8_t type);
41 static int bgp_connect(struct bgp_peer *peer);
42 static int bgp_handle_connect(struct bgp_peer *peer);
43 static int bgp_write(struct bgp_peer *peer);
44 static int bgp_read(struct bgp_peer *peer);
45 static int bgp_handle_input(struct bgp_peer *peer);
46 static int bgp_send_open(struct bgp_peer *peer);
47 static int bgp_send_keepalive(struct bgp_peer *peer);
48 static int bgp_send_update(struct bgp_peer *peer);
49 static int bgp_send_update6(struct bgp_peer *peer);
50 static int bgp_send_notification(struct bgp_peer *peer, uint8_t code,
51 uint8_t subcode);
52
53 static uint16_t our_as;
54 static struct bgp_route_list *bgp_routes = 0;
55 static struct bgp_route6_list *bgp_routes6 = 0;
56
57 int bgp_configured = 0;
58 struct bgp_peer *bgp_peers = 0;
59
60 /* prepare peer structure, globals */
61 int bgp_setup(int as)
62 {
63 int i;
64 struct bgp_peer *peer;
65
66 for (i = 0; i < BGP_NUM_PEERS; i++)
67 {
68 peer = &bgp_peers[i];
69 memset(peer, 0, sizeof(*peer));
70
71 peer->addr = INADDR_NONE;
72 peer->sock = -1;
73 peer->state = peer->next_state = Disabled;
74
75 if (!((peer->outbuf = malloc(sizeof(*peer->outbuf)))
76 && (peer->inbuf = malloc(sizeof(*peer->inbuf)))))
77 {
78 LOG(0, 0, 0, "Can't allocate buffers for bgp peer (%s)\n",
79 strerror(errno));
80
81 return 0;
82 }
83
84 peer->edata.type = FD_TYPE_BGP;
85 peer->edata.index = i;
86 peer->events = 0;
87 }
88
89 if (as < 1)
90 as = 0;
91
92 if ((our_as = as))
93 return 0;
94
95 bgp_routes = 0;
96 bgp_routes6 = 0;
97 bgp_configured = 0; /* set by bgp_start */
98
99 return 1;
100 }
101
102 /* start connection with a peer */
103 int bgp_start(struct bgp_peer *peer, char *name, int as, int keepalive,
104 int hold, struct in_addr update_source, int enable)
105 {
106 struct hostent *h;
107 int ibgp;
108 int i;
109 struct bgp_path_attr a;
110 char path_attrs[64];
111 char *p = path_attrs;
112 in_addr_t ip;
113 uint32_t metric = htonl(BGP_METRIC);
114 uint32_t no_export = htonl(BGP_COMMUNITY_NO_EXPORT);
115
116 if (!our_as)
117 return 0;
118
119 if (peer->state != Disabled)
120 bgp_halt(peer);
121
122 snprintf(peer->name, sizeof(peer->name), "%s", name);
123
124 if (!(h = gethostbyname(name)) || h->h_addrtype != AF_INET)
125 {
126 LOG(0, 0, 0, "Can't get address for BGP peer %s (%s)\n",
127 name, h ? "no address" : hstrerror(h_errno));
128
129 return 0;
130 }
131
132 memcpy(&peer->addr, h->h_addr, sizeof(peer->addr));
133 peer->source_addr = update_source.s_addr;
134 peer->as = as > 0 ? as : our_as;
135 ibgp = peer->as == our_as;
136
137 /* set initial timer values */
138 peer->init_keepalive = keepalive == -1 ? BGP_KEEPALIVE_TIME : keepalive;
139 peer->init_hold = hold == -1 ? BGP_HOLD_TIME : hold;
140
141 if (peer->init_hold < 3)
142 peer->init_hold = 3;
143
144 if (peer->init_keepalive * 3 > peer->init_hold)
145 peer->init_keepalive = peer->init_hold / 3;
146
147 /* clear buffers, go to Idle state */
148 peer->next_state = Idle;
149 bgp_clear(peer);
150
151 /* set initial routing state */
152 peer->routing = enable;
153
154 /* all our routes use the same attributes, so prepare it in advance */
155 if (peer->path_attrs)
156 free(peer->path_attrs);
157
158 peer->path_attr_len = 0;
159
160 /* ORIGIN */
161 a.flags = BGP_PATH_ATTR_FLAG_TRANS;
162 a.code = BGP_PATH_ATTR_CODE_ORIGIN;
163 a.data.s.len = 1;
164 a.data.s.value[0] = BGP_PATH_ATTR_CODE_ORIGIN_IGP;
165
166 #define ADD_ATTRIBUTE() do { \
167 i = BGP_PATH_ATTR_SIZE(a); \
168 memcpy(p, &a, i); \
169 p += i; \
170 peer->path_attr_len += i; } while (0)
171
172 ADD_ATTRIBUTE();
173
174 /* AS_PATH */
175 a.flags = BGP_PATH_ATTR_FLAG_TRANS;
176 a.code = BGP_PATH_ATTR_CODE_AS_PATH;
177 if (ibgp)
178 {
179 /* empty path */
180 a.data.s.len = 0;
181 }
182 else
183 {
184 /* just our AS */
185 struct {
186 uint8_t type;
187 uint8_t len;
188 uint16_t value;
189 } as_path = {
190 BGP_PATH_ATTR_CODE_AS_PATH_AS_SEQUENCE,
191 1,
192 htons(our_as),
193 };
194
195 a.data.s.len = sizeof(as_path);
196 memcpy(&a.data.s.value, &as_path, sizeof(as_path));
197 }
198
199 ADD_ATTRIBUTE();
200
201 /* MULTI_EXIT_DISC */
202 a.flags = BGP_PATH_ATTR_FLAG_OPTIONAL;
203 a.code = BGP_PATH_ATTR_CODE_MULTI_EXIT_DISC;
204 a.data.s.len = sizeof(metric);
205 memcpy(a.data.s.value, &metric, sizeof(metric));
206
207 ADD_ATTRIBUTE();
208
209 if (ibgp)
210 {
211 uint32_t local_pref = htonl(BGP_LOCAL_PREF);
212
213 /* LOCAL_PREF */
214 a.flags = BGP_PATH_ATTR_FLAG_TRANS;
215 a.code = BGP_PATH_ATTR_CODE_LOCAL_PREF;
216 a.data.s.len = sizeof(local_pref);
217 memcpy(a.data.s.value, &local_pref, sizeof(local_pref));
218
219 ADD_ATTRIBUTE();
220 }
221
222 /* COMMUNITIES */
223 a.flags = BGP_PATH_ATTR_FLAG_OPTIONAL | BGP_PATH_ATTR_FLAG_TRANS;
224 a.code = BGP_PATH_ATTR_CODE_COMMUNITIES;
225 a.data.s.len = sizeof(no_export);
226 memcpy(a.data.s.value, &no_export, sizeof(no_export));
227
228 ADD_ATTRIBUTE();
229
230 /* remember the len before adding NEXT_HOP */
231 peer->path_attr_len_without_nexthop = peer->path_attr_len;
232
233 /* NEXT_HOP */
234 a.flags = BGP_PATH_ATTR_FLAG_TRANS;
235 a.code = BGP_PATH_ATTR_CODE_NEXT_HOP;
236 if (config->nexthop_address)
237 {
238 ip = config->nexthop_address;
239 }
240 else
241 {
242 ip = my_address; /* we're it */
243 }
244 a.data.s.len = sizeof(ip);
245 memcpy(a.data.s.value, &ip, sizeof(ip));
246
247 ADD_ATTRIBUTE();
248
249 if (!(peer->path_attrs = malloc(peer->path_attr_len)))
250 {
251 LOG(0, 0, 0, "Can't allocate path_attrs for %s (%s)\n",
252 name, strerror(errno));
253
254 return 0;
255 }
256
257 memcpy(peer->path_attrs, path_attrs, peer->path_attr_len);
258
259 /* multiprotocol attributes initialization */
260 if (config->ipv6_prefix.s6_addr[0])
261 {
262 struct bgp_attr_mp_reach_nlri_partial mp_reach_nlri_partial;
263 struct bgp_attr_mp_unreach_nlri_partial mp_unreach_nlri_partial;
264
265 a.flags = BGP_PATH_ATTR_FLAG_OPTIONAL;
266 a.code = BGP_PATH_ATTR_CODE_MP_REACH_NLRI;
267 a.data.s.len = 0; /* will be set on UPDATE */
268
269 mp_reach_nlri_partial.afi = htons(BGP_MP_AFI_IPv6);
270 mp_reach_nlri_partial.safi = BGP_MP_SAFI_UNICAST;
271 mp_reach_nlri_partial.reserved = 0;
272 mp_reach_nlri_partial.next_hop_len = 16;
273
274 /* use the defined nexthop6, or our address in ipv6_prefix */
275 if (config->nexthop6_address.s6_addr[0])
276 memcpy(&mp_reach_nlri_partial.next_hop,
277 &config->nexthop6_address.s6_addr, 16);
278 else
279 {
280 /* our address is ipv6prefix::1 */
281 memcpy(&mp_reach_nlri_partial.next_hop,
282 &config->ipv6_prefix.s6_addr, 16);
283 mp_reach_nlri_partial.next_hop[15] = 1;
284 }
285
286 memcpy(&a.data.s.value, &mp_reach_nlri_partial,
287 sizeof(struct bgp_attr_mp_reach_nlri_partial));
288 memcpy(&peer->mp_reach_nlri_partial, &a,
289 BGP_PATH_ATTR_MP_REACH_NLRI_PARTIAL_SIZE);
290
291 a.flags = BGP_PATH_ATTR_FLAG_OPTIONAL | BGP_PATH_ATTR_FLAG_EXTLEN;
292 a.code = BGP_PATH_ATTR_CODE_MP_UNREACH_NLRI;
293 a.data.e.len = 0; /* will be set on UPDATE */
294
295 mp_unreach_nlri_partial.afi = htons(BGP_MP_AFI_IPv6);
296 mp_unreach_nlri_partial.safi = BGP_MP_SAFI_UNICAST;
297
298 memcpy(&a.data.e.value, &mp_unreach_nlri_partial,
299 sizeof(struct bgp_attr_mp_unreach_nlri_partial));
300 memcpy(&peer->mp_unreach_nlri_partial, &a,
301 BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE);
302 }
303
304 peer->mp_handling = HandlingUnknown;
305
306 LOG(4, 0, 0, "Initiating BGP connection to %s (routing %s)\n",
307 name, enable ? "enabled" : "suspended");
308
309 /* we have at least one peer configured */
310 bgp_configured = 1;
311
312 /* connect */
313 return bgp_connect(peer);
314 }
315
316 /* clear counters, timers, routes and buffers; close socket; move to
317 next_state, which may be Disabled or Idle */
318 static void bgp_clear(struct bgp_peer *peer)
319 {
320 if (peer->sock != -1)
321 {
322 close(peer->sock);
323 peer->sock = -1;
324 }
325
326 peer->keepalive_time = 0;
327 peer->expire_time = 0;
328
329 peer->keepalive = peer->init_keepalive;
330 peer->hold = peer->init_hold;
331
332 bgp_free_routes(peer->routes);
333 peer->routes = 0;
334 bgp_free_routes6(peer->routes6);
335 peer->routes6 = 0;
336
337 peer->outbuf->packet.header.len = 0;
338 peer->outbuf->done = 0;
339 peer->inbuf->packet.header.len = 0;
340 peer->inbuf->done = 0;
341
342 peer->cli_flag = 0;
343 peer->events = 0;
344
345 if (peer->state != peer->next_state)
346 {
347 peer->state = peer->next_state;
348 peer->state_time = time_now;
349
350 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer->name,
351 bgp_state_str(peer->next_state));
352 }
353 }
354
355 /* initiate a clean shutdown */
356 void bgp_stop(struct bgp_peer *peer)
357 {
358 LOG(4, 0, 0, "Terminating BGP connection to %s\n", peer->name);
359 bgp_send_notification(peer, BGP_ERR_CEASE, 0);
360 }
361
362 /* drop connection (if any) and set state to Disabled */
363 void bgp_halt(struct bgp_peer *peer)
364 {
365 LOG(4, 0, 0, "Aborting BGP connection to %s\n", peer->name);
366 peer->next_state = Disabled;
367 bgp_clear(peer);
368 }
369
370 /* drop connection (if any) and set to Idle for connection retry */
371 int bgp_restart(struct bgp_peer *peer)
372 {
373 peer->next_state = Idle;
374 bgp_clear(peer);
375
376 /* restart now */
377 peer->retry_time = time_now;
378 peer->retry_count = 0;
379
380 /* connect */
381 return bgp_connect(peer);
382 }
383
384 static void bgp_set_retry(struct bgp_peer *peer)
385 {
386 if (peer->retry_count++ < BGP_MAX_RETRY)
387 {
388 peer->retry_time = time_now + (BGP_RETRY_BACKOFF * peer->retry_count);
389 peer->next_state = Idle;
390 bgp_clear(peer);
391 }
392 else
393 bgp_halt(peer); /* give up */
394 }
395
396 /* convert ip/mask to CIDR notation */
397 static void bgp_cidr(in_addr_t ip, in_addr_t mask, struct bgp_ip_prefix *pfx)
398 {
399 int i;
400 uint32_t b;
401
402 /* convert to prefix notation */
403 pfx->len = 32;
404 pfx->prefix = ip;
405
406 if (!mask) /* bogus */
407 mask = 0xffffffff;
408
409 for (i = 0; i < 32 && ((b = ntohl(1 << i)), !(mask & b)); i++)
410 {
411 pfx->len--;
412 pfx->prefix &= ~b;
413 }
414 }
415
416 /* insert route into list; sorted */
417 static struct bgp_route_list *bgp_insert_route(struct bgp_route_list *head,
418 struct bgp_route_list *new)
419 {
420 struct bgp_route_list *p = head;
421 struct bgp_route_list *e = 0;
422
423 while (p && memcmp(&p->dest, &new->dest, sizeof(p->dest)) < 0)
424 {
425 e = p;
426 p = p->next;
427 }
428
429 if (e)
430 {
431 new->next = e->next;
432 e->next = new;
433 }
434 else
435 {
436 new->next = head;
437 head = new;
438 }
439
440 return head;
441 }
442
443 /* insert route6 into list; sorted */
444 static struct bgp_route6_list *bgp_insert_route6(struct bgp_route6_list *head,
445 struct bgp_route6_list *new)
446 {
447 struct bgp_route6_list *p = head;
448 struct bgp_route6_list *e = 0;
449
450 while (p && memcmp(&p->dest, &new->dest, sizeof(p->dest)) < 0)
451 {
452 e = p;
453 p = p->next;
454 }
455
456 if (e)
457 {
458 new->next = e->next;
459 e->next = new;
460 }
461 else
462 {
463 new->next = head;
464 head = new;
465 }
466
467 return head;
468 }
469
470 /* add route to list for peers */
471 /*
472 * Note: this doesn't do route aggregation, nor drop routes if a less
473 * specific match already exists (partly because I'm lazy, but also so
474 * that if that route is later deleted we don't have to be concerned
475 * about adding back the more specific one).
476 */
477 int bgp_add_route(in_addr_t ip, in_addr_t mask)
478 {
479 struct bgp_route_list *r = bgp_routes;
480 struct bgp_route_list add;
481 int i;
482
483 bgp_cidr(ip, mask, &add.dest);
484 add.next = 0;
485
486 /* check for duplicate */
487 while (r)
488 {
489 i = memcmp(&r->dest, &add.dest, sizeof(r->dest));
490 if (!i)
491 return 1; /* already covered */
492
493 if (i > 0)
494 break;
495
496 r = r->next;
497 }
498
499 /* insert into route list; sorted */
500 if (!(r = malloc(sizeof(*r))))
501 {
502 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
503 fmtaddr(add.dest.prefix, 0), add.dest.len, strerror(errno));
504
505 return 0;
506 }
507
508 memcpy(r, &add, sizeof(*r));
509 bgp_routes = bgp_insert_route(bgp_routes, r);
510
511 /* flag established peers for update */
512 for (i = 0; i < BGP_NUM_PEERS; i++)
513 if (bgp_peers[i].state == Established)
514 bgp_peers[i].update_routes = 1;
515
516 LOG(4, 0, 0, "Registered BGP route %s/%d\n",
517 fmtaddr(add.dest.prefix, 0), add.dest.len);
518
519 return 1;
520 }
521
522 /* add route to list for peers */
523 /*
524 * Note: same provisions as above
525 */
526 int bgp_add_route6(struct in6_addr ip, int prefixlen)
527 {
528 struct bgp_route6_list *r = bgp_routes6;
529 struct bgp_route6_list add;
530 int i;
531 char ipv6addr[INET6_ADDRSTRLEN];
532
533 memcpy(&add.dest.prefix, &ip.s6_addr, 16);
534 add.dest.len = prefixlen;
535 add.next = 0;
536
537 /* check for duplicate */
538 while (r)
539 {
540 i = memcmp(&r->dest, &add.dest, sizeof(r->dest));
541 if (!i)
542 return 1; /* already covered */
543
544 if (i > 0)
545 break;
546
547 r = r->next;
548 }
549
550 /* insert into route list; sorted */
551 if (!(r = malloc(sizeof(*r))))
552 {
553 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
554 inet_ntop(AF_INET6, &ip, ipv6addr, INET6_ADDRSTRLEN), add.dest.len,
555 strerror(errno));
556
557 return 0;
558 }
559
560 memcpy(r, &add, sizeof(*r));
561 bgp_routes6 = bgp_insert_route6(bgp_routes6, r);
562
563 /* flag established peers for update */
564 for (i = 0; i < BGP_NUM_PEERS; i++)
565 if (bgp_peers[i].state == Established
566 && bgp_peers[i].mp_handling == HandleIPv6Routes)
567 bgp_peers[i].update_routes6 = 1;
568
569 LOG(4, 0, 0, "Registered BGP route %s/%d\n",
570 inet_ntop(AF_INET6, &ip, ipv6addr, INET6_ADDRSTRLEN), add.dest.len);
571
572 return 1;
573 }
574
575 /* remove route from list for peers */
576 int bgp_del_route(in_addr_t ip, in_addr_t mask)
577 {
578 struct bgp_route_list *r = bgp_routes;
579 struct bgp_route_list *e = 0;
580 struct bgp_route_list del;
581 int i;
582
583 bgp_cidr(ip, mask, &del.dest);
584 del.next = 0;
585
586 /* find entry in routes list and remove */
587 while (r)
588 {
589 i = memcmp(&r->dest, &del.dest, sizeof(r->dest));
590 if (!i)
591 {
592 if (e)
593 e->next = r->next;
594 else
595 bgp_routes = r->next;
596
597 free(r);
598 break;
599 }
600
601 e = r;
602
603 if (i > 0)
604 r = 0; /* stop */
605 else
606 r = r->next;
607 }
608
609 /* not found */
610 if (!r)
611 return 1;
612
613 /* flag established peers for update */
614 for (i = 0; i < BGP_NUM_PEERS; i++)
615 if (bgp_peers[i].state == Established)
616 bgp_peers[i].update_routes = 1;
617
618 LOG(4, 0, 0, "Removed BGP route %s/%d\n",
619 fmtaddr(del.dest.prefix, 0), del.dest.len);
620
621 return 1;
622 }
623
624 /* remove route from list for peers */
625 int bgp_del_route6(struct in6_addr ip, int prefixlen)
626 {
627 struct bgp_route6_list *r = bgp_routes6;
628 struct bgp_route6_list *e = 0;
629 struct bgp_route6_list del;
630 int i;
631 char ipv6addr[INET6_ADDRSTRLEN];
632
633 memcpy(&del.dest.prefix, &ip.s6_addr, 16);
634 del.dest.len = prefixlen;
635 del.next = 0;
636
637 /* find entry in routes list and remove */
638 while (r)
639 {
640 i = memcmp(&r->dest, &del.dest, sizeof(r->dest));
641 if (!i)
642 {
643 if (e)
644 e->next = r->next;
645 else
646 bgp_routes6 = r->next;
647
648 free(r);
649 break;
650 }
651
652 e = r;
653
654 if (i > 0)
655 r = 0; /* stop */
656 else
657 r = r->next;
658 }
659
660 /* not found */
661 if (!r)
662 return 1;
663
664 /* flag established peers for update */
665 for (i = 0; i < BGP_NUM_PEERS; i++)
666 if (bgp_peers[i].state == Established
667 && bgp_peers[i].mp_handling == HandleIPv6Routes)
668 bgp_peers[i].update_routes6 = 1;
669
670 LOG(4, 0, 0, "Removed BGP route %s/%d\n",
671 inet_ntop(AF_INET6, &ip, ipv6addr, INET6_ADDRSTRLEN), del.dest.len);
672
673 return 1;
674 }
675
676 /* enable or disable routing */
677 void bgp_enable_routing(int enable)
678 {
679 int i;
680
681 for (i = 0; i < BGP_NUM_PEERS; i++)
682 {
683 bgp_peers[i].routing = enable;
684
685 /* flag established peers for update */
686 if (bgp_peers[i].state == Established)
687 bgp_peers[i].update_routes = 1;
688 }
689
690 LOG(4, 0, 0, "%s BGP routing\n", enable ? "Enabled" : "Suspended");
691 }
692
693 #ifdef HAVE_EPOLL
694 # include <sys/epoll.h>
695 #else
696 # include "fake_epoll.h"
697 #endif
698
699 /* return a bitmask of the events required to poll this peer's fd */
700 int bgp_set_poll()
701 {
702 int i;
703
704 if (!bgp_configured)
705 return 0;
706
707 for (i = 0; i < BGP_NUM_PEERS; i++)
708 {
709 struct bgp_peer *peer = &bgp_peers[i];
710 int events = 0;
711
712 if (peer->state == Disabled || peer->state == Idle)
713 continue;
714
715 if (peer->inbuf->done < BGP_MAX_PACKET_SIZE)
716 events |= EPOLLIN;
717
718 if (peer->state == Connect || /* connection in progress */
719 peer->update_routes || /* routing updates */
720 peer->outbuf->packet.header.len) /* pending output */
721 events |= EPOLLOUT;
722
723 if (peer->events != events)
724 {
725 struct epoll_event ev;
726
727 ev.events = peer->events = events;
728 ev.data.ptr = &peer->edata;
729 epoll_ctl(epollfd, EPOLL_CTL_MOD, peer->sock, &ev);
730 }
731 }
732
733 return 1;
734 }
735
736 /* process bgp events/timers */
737 int bgp_process(uint32_t events[])
738 {
739 int i;
740
741 if (!bgp_configured)
742 return 0;
743
744 for (i = 0; i < BGP_NUM_PEERS; i++)
745 {
746 struct bgp_peer *peer = &bgp_peers[i];
747
748 if (*peer->name && peer->cli_flag == BGP_CLI_RESTART)
749 {
750 bgp_restart(peer);
751 continue;
752 }
753
754 if (peer->state == Disabled)
755 continue;
756
757 if (peer->cli_flag)
758 {
759 switch (peer->cli_flag)
760 {
761 case BGP_CLI_SUSPEND:
762 if (peer->routing)
763 {
764 peer->routing = 0;
765 if (peer->state == Established)
766 peer->update_routes = 1;
767 }
768
769 break;
770
771 case BGP_CLI_ENABLE:
772 if (!peer->routing)
773 {
774 peer->routing = 1;
775 if (peer->state == Established)
776 peer->update_routes = 1;
777 }
778
779 break;
780 }
781
782 peer->cli_flag = 0;
783 }
784
785 /* handle empty/fill of buffers */
786 if (events[i] & EPOLLOUT)
787 {
788 int r = 1;
789 if (peer->state == Connect)
790 r = bgp_handle_connect(peer);
791 else if (peer->outbuf->packet.header.len)
792 r = bgp_write(peer);
793
794 if (!r)
795 continue;
796 }
797
798 if (events[i] & (EPOLLIN|EPOLLHUP))
799 {
800 if (!bgp_read(peer))
801 continue;
802 }
803
804 /* process input buffer contents */
805 while (peer->inbuf->done >= sizeof(peer->inbuf->packet.header)
806 && !peer->outbuf->packet.header.len) /* may need to queue a response */
807 {
808 if (bgp_handle_input(peer) < 0)
809 continue;
810 }
811
812 /* process pending updates */
813 if (peer->update_routes
814 && !peer->outbuf->packet.header.len) /* ditto */
815 {
816 if (!bgp_send_update(peer))
817 continue;
818 }
819
820 /* process pending IPv6 updates */
821 if (peer->update_routes6
822 && !peer->outbuf->packet.header.len) /* ditto */
823 {
824 if (!bgp_send_update6(peer))
825 continue;
826 }
827
828 /* process timers */
829 if (peer->state == Established)
830 {
831 if (time_now > peer->expire_time)
832 {
833 LOG(1, 0, 0, "No message from BGP peer %s in %ds\n",
834 peer->name, peer->hold);
835
836 bgp_send_notification(peer, BGP_ERR_HOLD_TIMER_EXP, 0);
837 continue;
838 }
839
840 if (time_now > peer->keepalive_time && !peer->outbuf->packet.header.len)
841 bgp_send_keepalive(peer);
842 }
843 else if (peer->state == Idle)
844 {
845 if (time_now > peer->retry_time)
846 bgp_connect(peer);
847 }
848 else if (time_now > peer->state_time + BGP_STATE_TIME)
849 {
850 LOG(1, 0, 0, "%s timer expired for BGP peer %s\n",
851 bgp_state_str(peer->state), peer->name);
852
853 bgp_restart(peer);
854 }
855 }
856
857 return 1;
858 }
859
860 static void bgp_free_routes(struct bgp_route_list *routes)
861 {
862 struct bgp_route_list *tmp;
863
864 while ((tmp = routes))
865 {
866 routes = tmp->next;
867 free(tmp);
868 }
869 }
870
871 static void bgp_free_routes6(struct bgp_route6_list *routes)
872 {
873 struct bgp_route6_list *tmp;
874
875 while ((tmp = routes))
876 {
877 routes = tmp->next;
878 free(tmp);
879 }
880 }
881
882 char const *bgp_state_str(enum bgp_state state)
883 {
884 switch (state)
885 {
886 case Disabled: return "Disabled";
887 case Idle: return "Idle";
888 case Connect: return "Connect";
889 case Active: return "Active";
890 case OpenSent: return "OpenSent";
891 case OpenConfirm: return "OpenConfirm";
892 case Established: return "Established";
893 }
894
895 return "?";
896 }
897
898 static char const *bgp_msg_type_str(uint8_t type)
899 {
900 switch (type)
901 {
902 case BGP_MSG_OPEN: return "OPEN";
903 case BGP_MSG_UPDATE: return "UPDATE";
904 case BGP_MSG_NOTIFICATION: return "NOTIFICATION";
905 case BGP_MSG_KEEPALIVE: return "KEEPALIVE";
906 }
907
908 return "?";
909 }
910
911 /* attempt to connect to peer */
912 static int bgp_connect(struct bgp_peer *peer)
913 {
914 static int bgp_port = 0;
915 struct sockaddr_in addr;
916 struct sockaddr_in source_addr;
917 struct epoll_event ev;
918
919 if (!bgp_port)
920 {
921 struct servent *serv;
922 if (!(serv = getservbyname("bgp", "tcp")))
923 {
924 LOG(0, 0, 0, "Can't get bgp service (%s)\n", strerror(errno));
925 return 0;
926 }
927
928 bgp_port = serv->s_port;
929 }
930
931 if ((peer->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
932 {
933 LOG(0, 0, 0, "Can't create a socket for BGP peer %s (%s)\n",
934 peer->name, strerror(errno));
935
936 peer->state = peer->next_state = Disabled;
937 return 0;
938 }
939
940 /* add to poll set */
941 ev.events = peer->events = EPOLLOUT;
942 ev.data.ptr = &peer->edata;
943 epoll_ctl(epollfd, EPOLL_CTL_ADD, peer->sock, &ev);
944
945 /* set to non-blocking */
946 fcntl(peer->sock, F_SETFL, fcntl(peer->sock, F_GETFL, 0) | O_NONBLOCK);
947
948 /* set source address */
949 memset(&source_addr, 0, sizeof(source_addr));
950 source_addr.sin_family = AF_INET;
951 source_addr.sin_addr.s_addr = peer->source_addr; /* defaults to INADDR_ANY */
952 if (bind(peer->sock, (struct sockaddr *) &source_addr, sizeof(source_addr)) < 0)
953 {
954 LOG(1, 0, 0, "Can't set source address to %s: %s\n",
955 inet_ntoa(source_addr.sin_addr), strerror(errno));
956
957 bgp_set_retry(peer);
958 return 0;
959 }
960
961 /* try connect */
962 memset(&addr, 0, sizeof(addr));
963 addr.sin_family = AF_INET;
964 addr.sin_port = bgp_port;
965 addr.sin_addr.s_addr = peer->addr;
966
967 while (connect(peer->sock, (struct sockaddr *) &addr, sizeof(addr)) == -1)
968 {
969 if (errno == EINTR) /* SIGALARM handler */
970 continue;
971
972 if (errno != EINPROGRESS)
973 {
974 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n",
975 inet_ntoa(addr.sin_addr), strerror(errno));
976
977 bgp_set_retry(peer);
978 return 0;
979 }
980
981 peer->state = Connect;
982 peer->state_time = time_now;
983
984 LOG(4, 0, 0, "BGP peer %s: state Connect\n", peer->name);
985 return 1;
986 }
987
988 peer->state = Active;
989 peer->state_time = time_now;
990 peer->retry_time = peer->retry_count = 0;
991
992 LOG(4, 0, 0, "BGP peer %s: state Active\n", inet_ntoa(addr.sin_addr));
993
994 return bgp_send_open(peer);
995 }
996
997 /* complete partial connection (state = Connect) */
998 static int bgp_handle_connect(struct bgp_peer *peer)
999 {
1000 int err = 0;
1001 socklen_t len = sizeof(int);
1002 getsockopt(peer->sock, SOL_SOCKET, SO_ERROR, &err, &len);
1003 if (err)
1004 {
1005 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer->name,
1006 strerror(err));
1007
1008 bgp_set_retry(peer);
1009 return 0;
1010 }
1011
1012 peer->state = Active;
1013 peer->state_time = time_now;
1014
1015 LOG(4, 0, 0, "BGP peer %s: state Active\n", peer->name);
1016
1017 return bgp_send_open(peer);
1018 }
1019
1020 /* initiate a write */
1021 static int bgp_write(struct bgp_peer *peer)
1022 {
1023 int len = htons(peer->outbuf->packet.header.len);
1024 int r;
1025
1026 while ((r = write(peer->sock, &peer->outbuf->packet + peer->outbuf->done,
1027 len - peer->outbuf->done)) == -1)
1028 {
1029 if (errno == EINTR)
1030 continue;
1031
1032 if (errno == EAGAIN)
1033 return 1;
1034
1035 if (errno == EPIPE)
1036 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer->name);
1037 else
1038 LOG(1, 0, 0, "Can't write to BGP peer %s (%s)\n", peer->name,
1039 strerror(errno));
1040
1041 bgp_set_retry(peer);
1042 return 0;
1043 }
1044
1045 if (r < len)
1046 {
1047 peer->outbuf->done += r;
1048 return 1;
1049 }
1050
1051 LOG(4, 0, 0, "Sent %s to BGP peer %s\n",
1052 bgp_msg_type_str(peer->outbuf->packet.header.type), peer->name);
1053
1054 peer->outbuf->packet.header.len = 0;
1055 peer->outbuf->done = 0;
1056
1057 if (peer->state == Established)
1058 peer->keepalive_time = time_now + peer->keepalive;
1059
1060 if (peer->state != peer->next_state)
1061 {
1062 if (peer->next_state == Disabled || peer->next_state == Idle)
1063 {
1064 bgp_clear(peer);
1065 return 0;
1066 }
1067
1068 peer->state = peer->next_state;
1069 peer->state_time = time_now;
1070
1071 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer->name,
1072 bgp_state_str(peer->state));
1073 }
1074
1075 return 1;
1076 }
1077
1078 /* initiate a read */
1079 static int bgp_read(struct bgp_peer *peer)
1080 {
1081 int r;
1082
1083 while ((r = read(peer->sock, &peer->inbuf->packet + peer->inbuf->done,
1084 BGP_MAX_PACKET_SIZE - peer->inbuf->done)) < 1)
1085 {
1086 if (!r)
1087 {
1088 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer->name);
1089 }
1090 else
1091 {
1092 if (errno == EINTR)
1093 continue;
1094
1095 if (errno == EAGAIN)
1096 return 1;
1097
1098 LOG(1, 0, 0, "Can't read from BGP peer %s (%s)\n", peer->name,
1099 strerror(errno));
1100 }
1101
1102 bgp_set_retry(peer);
1103 return 0;
1104 }
1105
1106 peer->inbuf->done += r;
1107 return 1;
1108 }
1109
1110 /* process buffered packets */
1111 static int bgp_handle_input(struct bgp_peer *peer)
1112 {
1113 struct bgp_packet *p = &peer->inbuf->packet;
1114 int len = ntohs(p->header.len);
1115
1116 if (len > BGP_MAX_PACKET_SIZE)
1117 {
1118 LOG(1, 0, 0, "Bad header length from BGP %s\n", peer->name);
1119 bgp_send_notification(peer, BGP_ERR_HEADER, BGP_ERR_HDR_BAD_LEN);
1120 return 0;
1121 }
1122
1123 if (peer->inbuf->done < len)
1124 return 0;
1125
1126 LOG(4, 0, 0, "Received %s from BGP peer %s\n",
1127 bgp_msg_type_str(p->header.type), peer->name);
1128
1129 switch (p->header.type)
1130 {
1131 case BGP_MSG_OPEN:
1132 {
1133 struct bgp_data_open data;
1134 int hold;
1135 int i;
1136 off_t param_offset, capability_offset;
1137 struct bgp_opt_param *param;
1138 uint8_t capabilities_len;
1139 char *capabilities = NULL;
1140 struct bgp_capability *capability;
1141 struct bgp_mp_cap_param *mp_cap;
1142
1143 for (i = 0; i < sizeof(p->header.marker); i++)
1144 {
1145 if ((unsigned char) p->header.marker[i] != 0xff)
1146 {
1147 LOG(1, 0, 0, "Invalid marker from BGP peer %s\n",
1148 peer->name);
1149
1150 bgp_send_notification(peer, BGP_ERR_HEADER,
1151 BGP_ERR_HDR_NOT_SYNC);
1152
1153 return 0;
1154 }
1155 }
1156
1157 if (peer->state != OpenSent)
1158 {
1159 LOG(1, 0, 0, "OPEN from BGP peer %s in %s state\n",
1160 peer->name, bgp_state_str(peer->state));
1161
1162 bgp_send_notification(peer, BGP_ERR_FSM, 0);
1163 return 0;
1164 }
1165
1166 memcpy(&data, p->data, len - sizeof(p->header));
1167
1168 if (data.version != BGP_VERSION)
1169 {
1170 LOG(1, 0, 0, "Bad version (%d) sent by BGP peer %s\n",
1171 (int) data.version, peer->name);
1172
1173 bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_OPN_VERSION);
1174 return 0;
1175 }
1176
1177 if (ntohs(data.as) != peer->as)
1178 {
1179 LOG(1, 0, 0, "Bad AS sent by BGP peer %s (got %d, "
1180 "expected %d)\n", peer->name, (int) htons(data.as),
1181 (int) peer->as);
1182
1183 bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_OPN_BAD_AS);
1184 return 0;
1185 }
1186
1187 if ((hold = ntohs(data.hold_time)) < 3)
1188 {
1189 LOG(1, 0, 0, "Bad hold time (%d) from BGP peer %s\n",
1190 hold, peer->name);
1191
1192 bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_OPN_HOLD_TIME);
1193 return 0;
1194 }
1195
1196 /* pick lowest hold time */
1197 if (hold < peer->hold)
1198 peer->hold = hold;
1199
1200 /* adjust our keepalive based on negotiated hold value */
1201 if (peer->keepalive * 3 > peer->hold)
1202 peer->keepalive = peer->hold / 3;
1203
1204 /* check for optional parameters */
1205 /* 2 is for the size of type + len (both uint8_t) */
1206 for (param_offset = 0;
1207 param_offset < data.opt_len;
1208 param_offset += 2 + param->len)
1209 {
1210 param = (struct bgp_opt_param *)((char *)&data.opt_params + param_offset);
1211
1212 /* sensible check */
1213 if (data.opt_len - param_offset < 2
1214 || param->len > data.opt_len - param_offset - 2)
1215 {
1216 LOG(1, 0, 0, "Malformed Optional Parameter list from BGP peer %s\n",
1217 peer->name);
1218
1219 bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_UNSPEC);
1220 return 0;
1221 }
1222
1223 /* we know only one parameter type */
1224 if (param->type != BGP_PARAM_TYPE_CAPABILITY)
1225 {
1226 LOG(1, 0, 0, "Unsupported Optional Parameter type %d from BGP peer %s\n",
1227 param->type, peer->name);
1228
1229 bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_OPN_UNSUP_PARAM);
1230 return 0;
1231 }
1232
1233 capabilities_len = param->len;
1234 capabilities = (char *)&param->value;
1235
1236 /* look for BGP multiprotocol capability */
1237 for (capability_offset = 0;
1238 capability_offset < capabilities_len;
1239 capability_offset += 2 + capability->len)
1240 {
1241 capability = (struct bgp_capability *)(capabilities + capability_offset);
1242
1243 /* sensible check */
1244 if (capabilities_len - capability_offset < 2
1245 || capability->len > capabilities_len - capability_offset - 2)
1246 {
1247 LOG(1, 0, 0, "Malformed Capabilities list from BGP peer %s\n",
1248 peer->name);
1249
1250 bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_UNSPEC);
1251 return 0;
1252 }
1253
1254 /* we only know one capability code */
1255 if (capability->code != BGP_CAP_CODE_MP
1256 && capability->len != sizeof(struct bgp_mp_cap_param))
1257 {
1258 LOG(4, 0, 0, "Unsupported Capability code %d from BGP peer %s\n",
1259 capability->code, peer->name);
1260
1261 /* we don't terminate, still; we just jump to the next one */
1262 continue;
1263 }
1264
1265 mp_cap = (struct bgp_mp_cap_param *)&capability->value;
1266 /* the only <AFI, SAFI> tuple we support */
1267 if (ntohs(mp_cap->afi) != BGP_MP_AFI_IPv6 && mp_cap->safi != BGP_MP_SAFI_UNICAST)
1268 {
1269 LOG(4, 0, 0, "Unsupported multiprotocol AFI %d and SAFI %d from BGP peer %s\n",
1270 mp_cap->afi, mp_cap->safi, peer->name);
1271
1272 /* we don't terminate, still; we just jump to the next one */
1273 continue;
1274 }
1275
1276 /* yes it can! */
1277 peer->mp_handling = HandleIPv6Routes;
1278 }
1279 }
1280
1281 if (peer->mp_handling != HandleIPv6Routes)
1282 {
1283 peer->mp_handling = DoesntHandleIPv6Routes;
1284 if (config->ipv6_prefix.s6_addr[0])
1285 LOG(1, 0, 0, "Warning: BGP peer %s doesn't handle IPv6 prefixes updates\n",
1286 peer->name);
1287 }
1288
1289 /* next transition requires an exchange of keepalives */
1290 bgp_send_keepalive(peer);
1291 }
1292
1293 break;
1294
1295 case BGP_MSG_KEEPALIVE:
1296 if (peer->state == OpenConfirm)
1297 {
1298 peer->state = peer->next_state = Established;
1299 peer->state_time = time_now;
1300 peer->keepalive_time = time_now + peer->keepalive;
1301 peer->update_routes = 1;
1302 peer->retry_count = 0;
1303 peer->retry_time = 0;
1304
1305 LOG(4, 0, 0, "BGP peer %s: state Established\n", peer->name);
1306 }
1307
1308 break;
1309
1310 case BGP_MSG_NOTIFICATION:
1311 if (len > sizeof(p->header))
1312 {
1313 struct bgp_data_notification *notification =
1314 (struct bgp_data_notification *) p->data;
1315
1316 if (notification->error_code == BGP_ERR_CEASE)
1317 {
1318 LOG(4, 0, 0, "BGP peer %s sent CEASE\n", peer->name);
1319 bgp_set_retry(peer);
1320 return 0;
1321 }
1322
1323 if (notification->error_code == BGP_ERR_OPEN
1324 && notification->error_subcode == BGP_ERR_OPN_UNSUP_PARAM)
1325 {
1326 LOG(4, 0, 0, "BGP peer %s doesn't support BGP Capabilities\n", peer->name);
1327 peer->mp_handling = DoesntHandleIPv6Routes;
1328 bgp_set_retry(peer);
1329 return 0;
1330 }
1331
1332 if (notification->error_code == BGP_ERR_OPEN
1333 && notification->error_subcode == BGP_ERR_OPN_UNSUP_CAP)
1334 {
1335 /* the only capability we advertise is this one, so upon receiving
1336 an "unsupported capability" message, we disable IPv6 routes for
1337 this peer */
1338 LOG(4, 0, 0, "BGP peer %s doesn't support IPv6 routes advertisement\n", peer->name);
1339 peer->mp_handling = DoesntHandleIPv6Routes;
1340 break;
1341 }
1342
1343 /* FIXME: should handle more notifications */
1344 LOG(4, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n",
1345 peer->name, (int) notification->error_code);
1346 }
1347
1348 break;
1349 }
1350
1351 /* reset timer */
1352 peer->expire_time = time_now + peer->hold;
1353
1354 /* see if there's another message in the same packet/buffer */
1355 if (peer->inbuf->done > len)
1356 {
1357 peer->inbuf->done -= len;
1358 memmove(p, (char *) p + len, peer->inbuf->done);
1359 }
1360 else
1361 {
1362 peer->inbuf->packet.header.len = 0;
1363 peer->inbuf->done = 0;
1364 }
1365
1366 return peer->inbuf->done;
1367 }
1368
1369 /* send/buffer OPEN message */
1370 static int bgp_send_open(struct bgp_peer *peer)
1371 {
1372 struct bgp_data_open data;
1373 struct bgp_mp_cap_param mp_ipv6 = { htons(BGP_MP_AFI_IPv6), 0, BGP_MP_SAFI_UNICAST };
1374 struct bgp_capability cap_mp_ipv6;
1375 struct bgp_opt_param param_cap_mp_ipv6;
1376 uint16_t len = sizeof(peer->outbuf->packet.header);
1377
1378 memset(peer->outbuf->packet.header.marker, 0xff,
1379 sizeof(peer->outbuf->packet.header.marker));
1380
1381 peer->outbuf->packet.header.type = BGP_MSG_OPEN;
1382
1383 data.version = BGP_VERSION;
1384 data.as = htons(our_as);
1385 data.hold_time = htons(peer->hold);
1386 /* use the source IP we use as identifier, if available */
1387 if (peer->source_addr != INADDR_ANY)
1388 data.identifier = peer->source_addr;
1389 else
1390 data.identifier = my_address;
1391
1392 /* if we know peer doesn't support MP (mp_handling == DoesntHandleIPv6Routes)
1393 then don't add this parameter */
1394 if (config->ipv6_prefix.s6_addr[0]
1395 && (peer->mp_handling == HandlingUnknown
1396 || peer->mp_handling == HandleIPv6Routes))
1397 {
1398 /* construct the param and capability */
1399 cap_mp_ipv6.code = BGP_CAP_CODE_MP;
1400 cap_mp_ipv6.len = sizeof(mp_ipv6);
1401 memcpy(&cap_mp_ipv6.value, &mp_ipv6, cap_mp_ipv6.len);
1402
1403 param_cap_mp_ipv6.type = BGP_PARAM_TYPE_CAPABILITY;
1404 param_cap_mp_ipv6.len = 2 + sizeof(mp_ipv6);
1405 memcpy(&param_cap_mp_ipv6.value, &cap_mp_ipv6, param_cap_mp_ipv6.len);
1406
1407 data.opt_len = 2 + param_cap_mp_ipv6.len;
1408 memcpy(&data.opt_params, &param_cap_mp_ipv6, data.opt_len);
1409 }
1410 else
1411 data.opt_len = 0;
1412
1413 memcpy(peer->outbuf->packet.data, &data, BGP_DATA_OPEN_SIZE + data.opt_len);
1414 len += BGP_DATA_OPEN_SIZE + data.opt_len;
1415
1416 peer->outbuf->packet.header.len = htons(len);
1417 peer->outbuf->done = 0;
1418 peer->next_state = OpenSent;
1419
1420 return bgp_write(peer);
1421 }
1422
1423 /* send/buffer KEEPALIVE message */
1424 static int bgp_send_keepalive(struct bgp_peer *peer)
1425 {
1426 memset(peer->outbuf->packet.header.marker, 0xff,
1427 sizeof(peer->outbuf->packet.header.marker));
1428
1429 peer->outbuf->packet.header.type = BGP_MSG_KEEPALIVE;
1430 peer->outbuf->packet.header.len =
1431 htons(sizeof(peer->outbuf->packet.header));
1432
1433 peer->outbuf->done = 0;
1434 peer->next_state = (peer->state == OpenSent) ? OpenConfirm : peer->state;
1435
1436 return bgp_write(peer);
1437 }
1438
1439 /* send/buffer UPDATE message */
1440 static int bgp_send_update(struct bgp_peer *peer)
1441 {
1442 uint16_t unf_len = 0;
1443 uint16_t attr_len;
1444 uint16_t len = sizeof(peer->outbuf->packet.header);
1445 struct bgp_route_list *have = peer->routes;
1446 struct bgp_route_list *want = peer->routing ? bgp_routes : 0;
1447 struct bgp_route_list *e = 0;
1448 struct bgp_route_list *add = 0;
1449 int s;
1450
1451 char *data = (char *) &peer->outbuf->packet.data;
1452
1453 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1454 char *max = (char *) &peer->outbuf->packet.data
1455 + sizeof(peer->outbuf->packet.data)
1456 - sizeof(attr_len) - peer->path_attr_len - sizeof(struct bgp_ip_prefix);
1457
1458 /* skip over unf_len */
1459 data += sizeof(unf_len);
1460 len += sizeof(unf_len);
1461
1462 memset(peer->outbuf->packet.header.marker, 0xff,
1463 sizeof(peer->outbuf->packet.header.marker));
1464
1465 peer->outbuf->packet.header.type = BGP_MSG_UPDATE;
1466
1467 peer->update_routes = 0; /* tentatively clear */
1468
1469 /* find differences */
1470 while ((have || want) && data < (max - sizeof(struct bgp_ip_prefix)))
1471 {
1472 if (have)
1473 s = want
1474 ? memcmp(&have->dest, &want->dest, sizeof(have->dest))
1475 : -1;
1476 else
1477 s = 1;
1478
1479 if (s < 0) /* found one to delete */
1480 {
1481 struct bgp_route_list *tmp = have;
1482 have = have->next;
1483
1484 s = BGP_IP_PREFIX_SIZE(tmp->dest);
1485 memcpy(data, &tmp->dest, s);
1486 data += s;
1487 unf_len += s;
1488 len += s;
1489
1490 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1491 fmtaddr(tmp->dest.prefix, 0), tmp->dest.len, peer->name);
1492
1493 free(tmp);
1494
1495 if (e)
1496 e->next = have;
1497 else
1498 peer->routes = have;
1499 }
1500 else
1501 {
1502 if (!s) /* same */
1503 {
1504 e = have; /* stash the last found to relink above */
1505 have = have->next;
1506 want = want->next;
1507 }
1508 else if (s > 0) /* addition reqd. */
1509 {
1510 if (add)
1511 {
1512 peer->update_routes = 1; /* only one add per packet */
1513 if (!have)
1514 break;
1515 }
1516 else
1517 add = want;
1518
1519 if (want)
1520 want = want->next;
1521 }
1522 }
1523 }
1524
1525 if (have || want)
1526 peer->update_routes = 1; /* more to do */
1527
1528 /* anything changed? */
1529 if (!(unf_len || add))
1530 return 1;
1531
1532 /* go back and insert unf_len */
1533 unf_len = htons(unf_len);
1534 memcpy(&peer->outbuf->packet.data, &unf_len, sizeof(unf_len));
1535
1536 if (add)
1537 {
1538 if (!(e = malloc(sizeof(*e))))
1539 {
1540 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1541 fmtaddr(add->dest.prefix, 0), add->dest.len, strerror(errno));
1542
1543 return 0;
1544 }
1545
1546 memcpy(e, add, sizeof(*e));
1547 e->next = 0;
1548 peer->routes = bgp_insert_route(peer->routes, e);
1549
1550 attr_len = htons(peer->path_attr_len);
1551 memcpy(data, &attr_len, sizeof(attr_len));
1552 data += sizeof(attr_len);
1553 len += sizeof(attr_len);
1554
1555 memcpy(data, peer->path_attrs, peer->path_attr_len);
1556 data += peer->path_attr_len;
1557 len += peer->path_attr_len;
1558
1559 s = BGP_IP_PREFIX_SIZE(add->dest);
1560 memcpy(data, &add->dest, s);
1561 data += s;
1562 len += s;
1563
1564 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1565 fmtaddr(add->dest.prefix, 0), add->dest.len, peer->name);
1566 }
1567 else
1568 {
1569 attr_len = 0;
1570 memcpy(data, &attr_len, sizeof(attr_len));
1571 data += sizeof(attr_len);
1572 len += sizeof(attr_len);
1573 }
1574
1575 peer->outbuf->packet.header.len = htons(len);
1576 peer->outbuf->done = 0;
1577
1578 return bgp_write(peer);
1579 }
1580
1581 /* send/buffer UPDATE message for IPv6 routes */
1582 static int bgp_send_update6(struct bgp_peer *peer)
1583 {
1584 uint16_t attr_len;
1585 uint16_t unreach_len = 0;
1586 char *unreach_len_pos;
1587 uint8_t reach_len;
1588 uint16_t len = sizeof(peer->outbuf->packet.header);
1589 struct bgp_route6_list *have = peer->routes6;
1590 struct bgp_route6_list *want = peer->routing ? bgp_routes6 : 0;
1591 struct bgp_route6_list *e = 0;
1592 struct bgp_route6_list *add = 0;
1593 int s;
1594 char ipv6addr[INET6_ADDRSTRLEN];
1595
1596 char *data = (char *) &peer->outbuf->packet.data;
1597
1598 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1599 char *max = (char *) &peer->outbuf->packet.data
1600 + sizeof(peer->outbuf->packet.data)
1601 - sizeof(attr_len) - peer->path_attr_len_without_nexthop
1602 - BGP_PATH_ATTR_MP_REACH_NLRI_PARTIAL_SIZE - sizeof(struct bgp_ip6_prefix);
1603
1604 memset(peer->outbuf->packet.header.marker, 0xff,
1605 sizeof(peer->outbuf->packet.header.marker));
1606
1607 peer->outbuf->packet.header.type = BGP_MSG_UPDATE;
1608
1609 /* insert non-MP unfeasible routes length */
1610 memcpy(data, &unreach_len, sizeof(unreach_len));
1611 /* skip over it and attr_len too; it will be filled when known */
1612 data += sizeof(unreach_len) + sizeof(attr_len);
1613 len += sizeof(unreach_len) + sizeof(attr_len);
1614
1615 /* copy usual attributes */
1616 memcpy(data, peer->path_attrs, peer->path_attr_len_without_nexthop);
1617 data += peer->path_attr_len_without_nexthop;
1618 attr_len = peer->path_attr_len_without_nexthop;
1619
1620 /* copy MP unreachable NLRI heading */
1621 memcpy(data, peer->mp_unreach_nlri_partial,
1622 BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE);
1623 /* remember where to update this attr len */
1624 unreach_len_pos = data + 2;
1625 data += BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE;
1626 attr_len += BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE;
1627
1628 peer->update_routes6 = 0; /* tentatively clear */
1629
1630 /* find differences */
1631 while ((have || want) && data < (max - sizeof(struct bgp_ip6_prefix)))
1632 {
1633 if (have)
1634 s = want
1635 ? memcmp(&have->dest, &want->dest, sizeof(have->dest))
1636 : -1;
1637 else
1638 s = 1;
1639
1640 if (s < 0) /* found one to delete */
1641 {
1642 struct bgp_route6_list *tmp = have;
1643 have = have->next;
1644
1645 s = BGP_IP_PREFIX_SIZE(tmp->dest);
1646 memcpy(data, &tmp->dest, s);
1647 data += s;
1648 unreach_len += s;
1649 attr_len += s;
1650
1651 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1652 inet_ntop(AF_INET6, &tmp->dest.prefix, ipv6addr, INET6_ADDRSTRLEN),
1653 tmp->dest.len, peer->name);
1654
1655 free(tmp);
1656
1657 if (e)
1658 e->next = have;
1659 else
1660 peer->routes6 = have;
1661 }
1662 else
1663 {
1664 if (!s) /* same */
1665 {
1666 e = have; /* stash the last found to relink above */
1667 have = have->next;
1668 want = want->next;
1669 }
1670 else if (s > 0) /* addition reqd. */
1671 {
1672 if (add)
1673 {
1674 peer->update_routes6 = 1; /* only one add per packet */
1675 if (!have)
1676 break;
1677 }
1678 else
1679 add = want;
1680
1681 if (want)
1682 want = want->next;
1683 }
1684 }
1685 }
1686
1687 if (have || want)
1688 peer->update_routes6 = 1; /* more to do */
1689
1690 /* anything changed? */
1691 if (!(unreach_len || add))
1692 return 1;
1693
1694 if (unreach_len)
1695 {
1696 /* go back and insert MP unreach_len */
1697 unreach_len += sizeof(struct bgp_attr_mp_unreach_nlri_partial);
1698 unreach_len = htons(unreach_len);
1699 memcpy(unreach_len_pos, &unreach_len, sizeof(unreach_len));
1700 }
1701 else
1702 {
1703 /* we can remove this attribute, then */
1704 data -= BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE;
1705 attr_len -= BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE;
1706 }
1707
1708 if (add)
1709 {
1710 if (!(e = malloc(sizeof(*e))))
1711 {
1712 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1713 inet_ntop(AF_INET6, &add->dest.prefix, ipv6addr, INET6_ADDRSTRLEN),
1714 add->dest.len, strerror(errno));
1715
1716 return 0;
1717 }
1718
1719 memcpy(e, add, sizeof(*e));
1720 e->next = 0;
1721 peer->routes6 = bgp_insert_route6(peer->routes6, e);
1722
1723 /* copy MP reachable NLRI heading */
1724 memcpy(data, peer->mp_reach_nlri_partial,
1725 BGP_PATH_ATTR_MP_REACH_NLRI_PARTIAL_SIZE);
1726 /* with proper len */
1727 reach_len = BGP_IP_PREFIX_SIZE(add->dest);
1728 data[2] = sizeof(struct bgp_attr_mp_reach_nlri_partial) + reach_len;
1729 data += BGP_PATH_ATTR_MP_REACH_NLRI_PARTIAL_SIZE;
1730 attr_len += BGP_PATH_ATTR_MP_REACH_NLRI_PARTIAL_SIZE;
1731
1732 memcpy(data, &add->dest, reach_len);
1733 data += reach_len;
1734 attr_len += reach_len;
1735
1736 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1737 inet_ntop(AF_INET6, &add->dest.prefix, ipv6addr, INET6_ADDRSTRLEN),
1738 add->dest.len, peer->name);
1739 }
1740
1741 /* update len with attributes we added */
1742 len += attr_len;
1743
1744 /* go back and insert attr_len */
1745 attr_len = htons(attr_len);
1746 memcpy((char *)&peer->outbuf->packet.data + 2, &attr_len, sizeof(attr_len));
1747
1748 peer->outbuf->packet.header.len = htons(len);
1749 peer->outbuf->done = 0;
1750
1751 return bgp_write(peer);
1752 }
1753
1754 /* send/buffer NOTIFICATION message */
1755 static int bgp_send_notification(struct bgp_peer *peer, uint8_t code,
1756 uint8_t subcode)
1757 {
1758 struct bgp_data_notification data;
1759 uint16_t len = 0;
1760
1761 data.error_code = code;
1762 len += sizeof(data.error_code);
1763
1764 data.error_subcode = subcode;
1765 len += sizeof(data.error_code);
1766
1767 memset(peer->outbuf->packet.header.marker, 0xff,
1768 sizeof(peer->outbuf->packet.header.marker));
1769
1770 peer->outbuf->packet.header.type = BGP_MSG_NOTIFICATION;
1771 peer->outbuf->packet.header.len =
1772 htons(sizeof(peer->outbuf->packet.header) + len);
1773
1774 memcpy(peer->outbuf->packet.data, &data, len);
1775
1776 peer->outbuf->done = 0;
1777 peer->next_state = code == BGP_ERR_CEASE ? Disabled : Idle;
1778
1779 /* we're dying; ignore any pending input */
1780 peer->inbuf->packet.header.len = 0;
1781 peer->inbuf->done = 0;
1782
1783 return bgp_write(peer);
1784 }