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