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