Don't process timers of disabled peers.
[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 /* process bgp timers only */
837 void bgp_process_peers_timers()
838 {
839 int i;
840
841 if (!bgp_configured)
842 return;
843
844 for (i = 0; i < BGP_NUM_PEERS; i++)
845 {
846 struct bgp_peer *peer = &bgp_peers[i];
847
848 if (peer->state == Disabled)
849 continue;
850
851 bgp_process_timers(peer);
852 }
853 }
854
855 static void bgp_process_timers(struct bgp_peer *peer)
856 {
857 if (peer->state == Established)
858 {
859 if (time_now > peer->expire_time)
860 {
861 LOG(1, 0, 0, "No message from BGP peer %s in %ds\n",
862 peer->name, peer->hold);
863
864 bgp_send_notification(peer, BGP_ERR_HOLD_TIMER_EXP, 0);
865 return;
866 }
867
868 if (time_now > peer->keepalive_time && !peer->outbuf->packet.header.len)
869 bgp_send_keepalive(peer);
870 }
871 else if (peer->state == Idle)
872 {
873 if (time_now > peer->retry_time)
874 bgp_connect(peer);
875 }
876 else if (time_now > peer->state_time + BGP_STATE_TIME)
877 {
878 LOG(1, 0, 0, "%s timer expired for BGP peer %s\n",
879 bgp_state_str(peer->state), peer->name);
880
881 bgp_restart(peer);
882 }
883 }
884
885 static void bgp_free_routes(struct bgp_route_list *routes)
886 {
887 struct bgp_route_list *tmp;
888
889 while ((tmp = routes))
890 {
891 routes = tmp->next;
892 free(tmp);
893 }
894 }
895
896 static void bgp_free_routes6(struct bgp_route6_list *routes)
897 {
898 struct bgp_route6_list *tmp;
899
900 while ((tmp = routes))
901 {
902 routes = tmp->next;
903 free(tmp);
904 }
905 }
906
907 char const *bgp_state_str(enum bgp_state state)
908 {
909 switch (state)
910 {
911 case Disabled: return "Disabled";
912 case Idle: return "Idle";
913 case Connect: return "Connect";
914 case Active: return "Active";
915 case OpenSent: return "OpenSent";
916 case OpenConfirm: return "OpenConfirm";
917 case Established: return "Established";
918 }
919
920 return "?";
921 }
922
923 static char const *bgp_msg_type_str(uint8_t type)
924 {
925 switch (type)
926 {
927 case BGP_MSG_OPEN: return "OPEN";
928 case BGP_MSG_UPDATE: return "UPDATE";
929 case BGP_MSG_NOTIFICATION: return "NOTIFICATION";
930 case BGP_MSG_KEEPALIVE: return "KEEPALIVE";
931 }
932
933 return "?";
934 }
935
936 /* attempt to connect to peer */
937 static int bgp_connect(struct bgp_peer *peer)
938 {
939 static int bgp_port = 0;
940 struct sockaddr_in addr;
941 struct sockaddr_in source_addr;
942 struct epoll_event ev;
943
944 if (!bgp_port)
945 {
946 struct servent *serv;
947 if (!(serv = getservbyname("bgp", "tcp")))
948 {
949 LOG(0, 0, 0, "Can't get bgp service (%s)\n", strerror(errno));
950 return 0;
951 }
952
953 bgp_port = serv->s_port;
954 }
955
956 if ((peer->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
957 {
958 LOG(0, 0, 0, "Can't create a socket for BGP peer %s (%s)\n",
959 peer->name, strerror(errno));
960
961 peer->state = peer->next_state = Disabled;
962 return 0;
963 }
964
965 /* add to poll set */
966 ev.events = peer->events = EPOLLOUT;
967 ev.data.ptr = &peer->edata;
968 epoll_ctl(epollfd, EPOLL_CTL_ADD, peer->sock, &ev);
969
970 /* set to non-blocking */
971 fcntl(peer->sock, F_SETFL, fcntl(peer->sock, F_GETFL, 0) | O_NONBLOCK);
972
973 /* set source address */
974 memset(&source_addr, 0, sizeof(source_addr));
975 source_addr.sin_family = AF_INET;
976 source_addr.sin_addr.s_addr = peer->source_addr; /* defaults to INADDR_ANY */
977 if (bind(peer->sock, (struct sockaddr *) &source_addr, sizeof(source_addr)) < 0)
978 {
979 LOG(1, 0, 0, "Can't set source address to %s: %s\n",
980 inet_ntoa(source_addr.sin_addr), strerror(errno));
981
982 bgp_set_retry(peer);
983 return 0;
984 }
985
986 /* try connect */
987 memset(&addr, 0, sizeof(addr));
988 addr.sin_family = AF_INET;
989 addr.sin_port = bgp_port;
990 addr.sin_addr.s_addr = peer->addr;
991
992 while (connect(peer->sock, (struct sockaddr *) &addr, sizeof(addr)) == -1)
993 {
994 if (errno == EINTR) /* SIGALARM handler */
995 continue;
996
997 if (errno != EINPROGRESS)
998 {
999 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n",
1000 inet_ntoa(addr.sin_addr), strerror(errno));
1001
1002 bgp_set_retry(peer);
1003 return 0;
1004 }
1005
1006 peer->state = Connect;
1007 peer->state_time = time_now;
1008
1009 LOG(4, 0, 0, "BGP peer %s: state Connect\n", peer->name);
1010 return 1;
1011 }
1012
1013 peer->state = Active;
1014 peer->state_time = time_now;
1015 peer->retry_time = peer->retry_count = 0;
1016
1017 LOG(4, 0, 0, "BGP peer %s: state Active\n", inet_ntoa(addr.sin_addr));
1018
1019 return bgp_send_open(peer);
1020 }
1021
1022 /* complete partial connection (state = Connect) */
1023 static int bgp_handle_connect(struct bgp_peer *peer)
1024 {
1025 int err = 0;
1026 socklen_t len = sizeof(int);
1027 getsockopt(peer->sock, SOL_SOCKET, SO_ERROR, &err, &len);
1028 if (err)
1029 {
1030 LOG(1, 0, 0, "Can't connect to BGP peer %s (%s)\n", peer->name,
1031 strerror(err));
1032
1033 bgp_set_retry(peer);
1034 return 0;
1035 }
1036
1037 peer->state = Active;
1038 peer->state_time = time_now;
1039
1040 LOG(4, 0, 0, "BGP peer %s: state Active\n", peer->name);
1041
1042 return bgp_send_open(peer);
1043 }
1044
1045 /* initiate a write */
1046 static int bgp_write(struct bgp_peer *peer)
1047 {
1048 int len = htons(peer->outbuf->packet.header.len);
1049 int r;
1050
1051 while ((r = write(peer->sock, &peer->outbuf->packet + peer->outbuf->done,
1052 len - peer->outbuf->done)) == -1)
1053 {
1054 if (errno == EINTR)
1055 continue;
1056
1057 if (errno == EAGAIN)
1058 return 1;
1059
1060 if (errno == EPIPE)
1061 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer->name);
1062 else
1063 LOG(1, 0, 0, "Can't write to BGP peer %s (%s)\n", peer->name,
1064 strerror(errno));
1065
1066 bgp_set_retry(peer);
1067 return 0;
1068 }
1069
1070 if (r < len)
1071 {
1072 peer->outbuf->done += r;
1073 return 1;
1074 }
1075
1076 LOG(4, 0, 0, "Sent %s to BGP peer %s\n",
1077 bgp_msg_type_str(peer->outbuf->packet.header.type), peer->name);
1078
1079 peer->outbuf->packet.header.len = 0;
1080 peer->outbuf->done = 0;
1081
1082 if (peer->state == Established)
1083 peer->keepalive_time = time_now + peer->keepalive;
1084
1085 if (peer->state != peer->next_state)
1086 {
1087 if (peer->next_state == Disabled || peer->next_state == Idle)
1088 {
1089 bgp_clear(peer);
1090 return 0;
1091 }
1092
1093 peer->state = peer->next_state;
1094 peer->state_time = time_now;
1095
1096 LOG(4, 0, 0, "BGP peer %s: state %s\n", peer->name,
1097 bgp_state_str(peer->state));
1098 }
1099
1100 return 1;
1101 }
1102
1103 /* initiate a read */
1104 static int bgp_read(struct bgp_peer *peer)
1105 {
1106 int r;
1107
1108 while ((r = read(peer->sock, &peer->inbuf->packet + peer->inbuf->done,
1109 BGP_MAX_PACKET_SIZE - peer->inbuf->done)) < 1)
1110 {
1111 if (!r)
1112 {
1113 LOG(1, 0, 0, "Connection to BGP peer %s closed\n", peer->name);
1114 }
1115 else
1116 {
1117 if (errno == EINTR)
1118 continue;
1119
1120 if (errno == EAGAIN)
1121 return 1;
1122
1123 LOG(1, 0, 0, "Can't read from BGP peer %s (%s)\n", peer->name,
1124 strerror(errno));
1125 }
1126
1127 bgp_set_retry(peer);
1128 return 0;
1129 }
1130
1131 peer->inbuf->done += r;
1132 return 1;
1133 }
1134
1135 /* process buffered packets */
1136 static int bgp_handle_input(struct bgp_peer *peer)
1137 {
1138 struct bgp_packet *p = &peer->inbuf->packet;
1139 int len = ntohs(p->header.len);
1140
1141 if (len > BGP_MAX_PACKET_SIZE)
1142 {
1143 LOG(1, 0, 0, "Bad header length from BGP %s\n", peer->name);
1144 bgp_send_notification(peer, BGP_ERR_HEADER, BGP_ERR_HDR_BAD_LEN);
1145 return 0;
1146 }
1147
1148 if (peer->inbuf->done < len)
1149 return 0;
1150
1151 LOG(4, 0, 0, "Received %s from BGP peer %s\n",
1152 bgp_msg_type_str(p->header.type), peer->name);
1153
1154 switch (p->header.type)
1155 {
1156 case BGP_MSG_OPEN:
1157 {
1158 struct bgp_data_open data;
1159 int hold;
1160 int i;
1161 off_t param_offset, capability_offset;
1162 struct bgp_opt_param *param;
1163 uint8_t capabilities_len;
1164 char *capabilities = NULL;
1165 struct bgp_capability *capability;
1166 struct bgp_mp_cap_param *mp_cap;
1167
1168 for (i = 0; i < sizeof(p->header.marker); i++)
1169 {
1170 if ((unsigned char) p->header.marker[i] != 0xff)
1171 {
1172 LOG(1, 0, 0, "Invalid marker from BGP peer %s\n",
1173 peer->name);
1174
1175 bgp_send_notification(peer, BGP_ERR_HEADER,
1176 BGP_ERR_HDR_NOT_SYNC);
1177
1178 return 0;
1179 }
1180 }
1181
1182 if (peer->state != OpenSent)
1183 {
1184 LOG(1, 0, 0, "OPEN from BGP peer %s in %s state\n",
1185 peer->name, bgp_state_str(peer->state));
1186
1187 bgp_send_notification(peer, BGP_ERR_FSM, 0);
1188 return 0;
1189 }
1190
1191 memcpy(&data, p->data, len - sizeof(p->header));
1192
1193 if (data.version != BGP_VERSION)
1194 {
1195 LOG(1, 0, 0, "Bad version (%d) sent by BGP peer %s\n",
1196 (int) data.version, peer->name);
1197
1198 bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_OPN_VERSION);
1199 return 0;
1200 }
1201
1202 if (ntohs(data.as) != peer->as)
1203 {
1204 LOG(1, 0, 0, "Bad AS sent by BGP peer %s (got %d, "
1205 "expected %d)\n", peer->name, (int) htons(data.as),
1206 (int) peer->as);
1207
1208 bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_OPN_BAD_AS);
1209 return 0;
1210 }
1211
1212 if ((hold = ntohs(data.hold_time)) < 3)
1213 {
1214 LOG(1, 0, 0, "Bad hold time (%d) from BGP peer %s\n",
1215 hold, peer->name);
1216
1217 bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_OPN_HOLD_TIME);
1218 return 0;
1219 }
1220
1221 /* pick lowest hold time */
1222 if (hold < peer->hold)
1223 peer->hold = hold;
1224
1225 /* adjust our keepalive based on negotiated hold value */
1226 if (peer->keepalive * 3 > peer->hold)
1227 peer->keepalive = peer->hold / 3;
1228
1229 /* check for optional parameters */
1230 /* 2 is for the size of type + len (both uint8_t) */
1231 for (param_offset = 0;
1232 param_offset < data.opt_len;
1233 param_offset += 2 + param->len)
1234 {
1235 param = (struct bgp_opt_param *)((char *)&data.opt_params + param_offset);
1236
1237 /* sensible check */
1238 if (data.opt_len - param_offset < 2
1239 || param->len > data.opt_len - param_offset - 2)
1240 {
1241 LOG(1, 0, 0, "Malformed Optional Parameter list from BGP peer %s\n",
1242 peer->name);
1243
1244 bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_UNSPEC);
1245 return 0;
1246 }
1247
1248 /* we know only one parameter type */
1249 if (param->type != BGP_PARAM_TYPE_CAPABILITY)
1250 {
1251 LOG(1, 0, 0, "Unsupported Optional Parameter type %d from BGP peer %s\n",
1252 param->type, peer->name);
1253
1254 bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_OPN_UNSUP_PARAM);
1255 return 0;
1256 }
1257
1258 capabilities_len = param->len;
1259 capabilities = (char *)&param->value;
1260
1261 /* look for BGP multiprotocol capability */
1262 for (capability_offset = 0;
1263 capability_offset < capabilities_len;
1264 capability_offset += 2 + capability->len)
1265 {
1266 capability = (struct bgp_capability *)(capabilities + capability_offset);
1267
1268 /* sensible check */
1269 if (capabilities_len - capability_offset < 2
1270 || capability->len > capabilities_len - capability_offset - 2)
1271 {
1272 LOG(1, 0, 0, "Malformed Capabilities list from BGP peer %s\n",
1273 peer->name);
1274
1275 bgp_send_notification(peer, BGP_ERR_OPEN, BGP_ERR_UNSPEC);
1276 return 0;
1277 }
1278
1279 /* we only know one capability code */
1280 if (capability->code != BGP_CAP_CODE_MP
1281 && capability->len != sizeof(struct bgp_mp_cap_param))
1282 {
1283 LOG(4, 0, 0, "Unsupported Capability code %d from BGP peer %s\n",
1284 capability->code, peer->name);
1285
1286 /* we don't terminate, still; we just jump to the next one */
1287 continue;
1288 }
1289
1290 mp_cap = (struct bgp_mp_cap_param *)&capability->value;
1291 /* the only <AFI, SAFI> tuple we support */
1292 if (ntohs(mp_cap->afi) != BGP_MP_AFI_IPv6 && mp_cap->safi != BGP_MP_SAFI_UNICAST)
1293 {
1294 LOG(4, 0, 0, "Unsupported multiprotocol AFI %d and SAFI %d from BGP peer %s\n",
1295 mp_cap->afi, mp_cap->safi, peer->name);
1296
1297 /* we don't terminate, still; we just jump to the next one */
1298 continue;
1299 }
1300
1301 /* yes it can! */
1302 peer->mp_handling = HandleIPv6Routes;
1303 }
1304 }
1305
1306 if (peer->mp_handling != HandleIPv6Routes)
1307 {
1308 peer->mp_handling = DoesntHandleIPv6Routes;
1309 if (config->ipv6_prefix.s6_addr[0])
1310 LOG(1, 0, 0, "Warning: BGP peer %s doesn't handle IPv6 prefixes updates\n",
1311 peer->name);
1312 }
1313
1314 /* next transition requires an exchange of keepalives */
1315 bgp_send_keepalive(peer);
1316 }
1317
1318 break;
1319
1320 case BGP_MSG_KEEPALIVE:
1321 if (peer->state == OpenConfirm)
1322 {
1323 peer->state = peer->next_state = Established;
1324 peer->state_time = time_now;
1325 peer->keepalive_time = time_now + peer->keepalive;
1326 peer->update_routes = 1;
1327 peer->retry_count = 0;
1328 peer->retry_time = 0;
1329
1330 LOG(4, 0, 0, "BGP peer %s: state Established\n", peer->name);
1331 }
1332
1333 break;
1334
1335 case BGP_MSG_NOTIFICATION:
1336 if (len > sizeof(p->header))
1337 {
1338 struct bgp_data_notification *notification =
1339 (struct bgp_data_notification *) p->data;
1340
1341 if (notification->error_code == BGP_ERR_CEASE)
1342 {
1343 LOG(4, 0, 0, "BGP peer %s sent CEASE\n", peer->name);
1344 bgp_set_retry(peer);
1345 return 0;
1346 }
1347
1348 if (notification->error_code == BGP_ERR_OPEN
1349 && notification->error_subcode == BGP_ERR_OPN_UNSUP_PARAM)
1350 {
1351 LOG(4, 0, 0, "BGP peer %s doesn't support BGP Capabilities\n", peer->name);
1352 peer->mp_handling = DoesntHandleIPv6Routes;
1353 bgp_set_retry(peer);
1354 return 0;
1355 }
1356
1357 if (notification->error_code == BGP_ERR_OPEN
1358 && notification->error_subcode == BGP_ERR_OPN_UNSUP_CAP)
1359 {
1360 /* the only capability we advertise is this one, so upon receiving
1361 an "unsupported capability" message, we disable IPv6 routes for
1362 this peer */
1363 LOG(4, 0, 0, "BGP peer %s doesn't support IPv6 routes advertisement\n", peer->name);
1364 peer->mp_handling = DoesntHandleIPv6Routes;
1365 break;
1366 }
1367
1368 /* FIXME: should handle more notifications */
1369 LOG(4, 0, 0, "BGP peer %s sent unhandled NOTIFICATION %d\n",
1370 peer->name, (int) notification->error_code);
1371 }
1372
1373 break;
1374 }
1375
1376 /* reset timer */
1377 peer->expire_time = time_now + peer->hold;
1378
1379 /* see if there's another message in the same packet/buffer */
1380 if (peer->inbuf->done > len)
1381 {
1382 peer->inbuf->done -= len;
1383 memmove(p, (char *) p + len, peer->inbuf->done);
1384 }
1385 else
1386 {
1387 peer->inbuf->packet.header.len = 0;
1388 peer->inbuf->done = 0;
1389 }
1390
1391 return peer->inbuf->done;
1392 }
1393
1394 /* send/buffer OPEN message */
1395 static int bgp_send_open(struct bgp_peer *peer)
1396 {
1397 struct bgp_data_open data;
1398 struct bgp_mp_cap_param mp_ipv6 = { htons(BGP_MP_AFI_IPv6), 0, BGP_MP_SAFI_UNICAST };
1399 struct bgp_capability cap_mp_ipv6;
1400 struct bgp_opt_param param_cap_mp_ipv6;
1401 uint16_t len = sizeof(peer->outbuf->packet.header);
1402
1403 memset(peer->outbuf->packet.header.marker, 0xff,
1404 sizeof(peer->outbuf->packet.header.marker));
1405
1406 peer->outbuf->packet.header.type = BGP_MSG_OPEN;
1407
1408 data.version = BGP_VERSION;
1409 data.as = htons(our_as);
1410 data.hold_time = htons(peer->hold);
1411 /* use the source IP we use as identifier, if available */
1412 if (peer->source_addr != INADDR_ANY)
1413 data.identifier = peer->source_addr;
1414 else
1415 data.identifier = my_address;
1416
1417 /* if we know peer doesn't support MP (mp_handling == DoesntHandleIPv6Routes)
1418 then don't add this parameter */
1419 if (config->ipv6_prefix.s6_addr[0]
1420 && (peer->mp_handling == HandlingUnknown
1421 || peer->mp_handling == HandleIPv6Routes))
1422 {
1423 /* construct the param and capability */
1424 cap_mp_ipv6.code = BGP_CAP_CODE_MP;
1425 cap_mp_ipv6.len = sizeof(mp_ipv6);
1426 memcpy(&cap_mp_ipv6.value, &mp_ipv6, cap_mp_ipv6.len);
1427
1428 param_cap_mp_ipv6.type = BGP_PARAM_TYPE_CAPABILITY;
1429 param_cap_mp_ipv6.len = 2 + sizeof(mp_ipv6);
1430 memcpy(&param_cap_mp_ipv6.value, &cap_mp_ipv6, param_cap_mp_ipv6.len);
1431
1432 data.opt_len = 2 + param_cap_mp_ipv6.len;
1433 memcpy(&data.opt_params, &param_cap_mp_ipv6, data.opt_len);
1434 }
1435 else
1436 data.opt_len = 0;
1437
1438 memcpy(peer->outbuf->packet.data, &data, BGP_DATA_OPEN_SIZE + data.opt_len);
1439 len += BGP_DATA_OPEN_SIZE + data.opt_len;
1440
1441 peer->outbuf->packet.header.len = htons(len);
1442 peer->outbuf->done = 0;
1443 peer->next_state = OpenSent;
1444
1445 return bgp_write(peer);
1446 }
1447
1448 /* send/buffer KEEPALIVE message */
1449 static int bgp_send_keepalive(struct bgp_peer *peer)
1450 {
1451 memset(peer->outbuf->packet.header.marker, 0xff,
1452 sizeof(peer->outbuf->packet.header.marker));
1453
1454 peer->outbuf->packet.header.type = BGP_MSG_KEEPALIVE;
1455 peer->outbuf->packet.header.len =
1456 htons(sizeof(peer->outbuf->packet.header));
1457
1458 peer->outbuf->done = 0;
1459 peer->next_state = (peer->state == OpenSent) ? OpenConfirm : peer->state;
1460
1461 return bgp_write(peer);
1462 }
1463
1464 /* send/buffer UPDATE message */
1465 static int bgp_send_update(struct bgp_peer *peer)
1466 {
1467 uint16_t unf_len = 0;
1468 uint16_t attr_len;
1469 uint16_t len = sizeof(peer->outbuf->packet.header);
1470 struct bgp_route_list *have = peer->routes;
1471 struct bgp_route_list *want = peer->routing ? bgp_routes : 0;
1472 struct bgp_route_list *e = 0;
1473 struct bgp_route_list *add = 0;
1474 int s;
1475
1476 char *data = (char *) &peer->outbuf->packet.data;
1477
1478 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1479 char *max = (char *) &peer->outbuf->packet.data
1480 + sizeof(peer->outbuf->packet.data)
1481 - sizeof(attr_len) - peer->path_attr_len - sizeof(struct bgp_ip_prefix);
1482
1483 /* skip over unf_len */
1484 data += sizeof(unf_len);
1485 len += sizeof(unf_len);
1486
1487 memset(peer->outbuf->packet.header.marker, 0xff,
1488 sizeof(peer->outbuf->packet.header.marker));
1489
1490 peer->outbuf->packet.header.type = BGP_MSG_UPDATE;
1491
1492 peer->update_routes = 0; /* tentatively clear */
1493
1494 /* find differences */
1495 while ((have || want) && data < (max - sizeof(struct bgp_ip_prefix)))
1496 {
1497 if (have)
1498 s = want
1499 ? memcmp(&have->dest, &want->dest, sizeof(have->dest))
1500 : -1;
1501 else
1502 s = 1;
1503
1504 if (s < 0) /* found one to delete */
1505 {
1506 struct bgp_route_list *tmp = have;
1507 have = have->next;
1508
1509 s = BGP_IP_PREFIX_SIZE(tmp->dest);
1510 memcpy(data, &tmp->dest, s);
1511 data += s;
1512 unf_len += s;
1513 len += s;
1514
1515 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1516 fmtaddr(tmp->dest.prefix, 0), tmp->dest.len, peer->name);
1517
1518 free(tmp);
1519
1520 if (e)
1521 e->next = have;
1522 else
1523 peer->routes = have;
1524 }
1525 else
1526 {
1527 if (!s) /* same */
1528 {
1529 e = have; /* stash the last found to relink above */
1530 have = have->next;
1531 want = want->next;
1532 }
1533 else if (s > 0) /* addition reqd. */
1534 {
1535 if (add)
1536 {
1537 peer->update_routes = 1; /* only one add per packet */
1538 if (!have)
1539 break;
1540 }
1541 else
1542 add = want;
1543
1544 if (want)
1545 want = want->next;
1546 }
1547 }
1548 }
1549
1550 if (have || want)
1551 peer->update_routes = 1; /* more to do */
1552
1553 /* anything changed? */
1554 if (!(unf_len || add))
1555 return 1;
1556
1557 /* go back and insert unf_len */
1558 unf_len = htons(unf_len);
1559 memcpy(&peer->outbuf->packet.data, &unf_len, sizeof(unf_len));
1560
1561 if (add)
1562 {
1563 if (!(e = malloc(sizeof(*e))))
1564 {
1565 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1566 fmtaddr(add->dest.prefix, 0), add->dest.len, strerror(errno));
1567
1568 return 0;
1569 }
1570
1571 memcpy(e, add, sizeof(*e));
1572 e->next = 0;
1573 peer->routes = bgp_insert_route(peer->routes, e);
1574
1575 attr_len = htons(peer->path_attr_len);
1576 memcpy(data, &attr_len, sizeof(attr_len));
1577 data += sizeof(attr_len);
1578 len += sizeof(attr_len);
1579
1580 memcpy(data, peer->path_attrs, peer->path_attr_len);
1581 data += peer->path_attr_len;
1582 len += peer->path_attr_len;
1583
1584 s = BGP_IP_PREFIX_SIZE(add->dest);
1585 memcpy(data, &add->dest, s);
1586 data += s;
1587 len += s;
1588
1589 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1590 fmtaddr(add->dest.prefix, 0), add->dest.len, peer->name);
1591 }
1592 else
1593 {
1594 attr_len = 0;
1595 memcpy(data, &attr_len, sizeof(attr_len));
1596 data += sizeof(attr_len);
1597 len += sizeof(attr_len);
1598 }
1599
1600 peer->outbuf->packet.header.len = htons(len);
1601 peer->outbuf->done = 0;
1602
1603 return bgp_write(peer);
1604 }
1605
1606 /* send/buffer UPDATE message for IPv6 routes */
1607 static int bgp_send_update6(struct bgp_peer *peer)
1608 {
1609 uint16_t attr_len;
1610 uint16_t unreach_len = 0;
1611 char *unreach_len_pos;
1612 uint8_t reach_len;
1613 uint16_t len = sizeof(peer->outbuf->packet.header);
1614 struct bgp_route6_list *have = peer->routes6;
1615 struct bgp_route6_list *want = peer->routing ? bgp_routes6 : 0;
1616 struct bgp_route6_list *e = 0;
1617 struct bgp_route6_list *add = 0;
1618 int s;
1619 char ipv6addr[INET6_ADDRSTRLEN];
1620
1621 char *data = (char *) &peer->outbuf->packet.data;
1622
1623 /* need leave room for attr_len, bgp_path_attrs and one prefix */
1624 char *max = (char *) &peer->outbuf->packet.data
1625 + sizeof(peer->outbuf->packet.data)
1626 - sizeof(attr_len) - peer->path_attr_len_without_nexthop
1627 - BGP_PATH_ATTR_MP_REACH_NLRI_PARTIAL_SIZE - sizeof(struct bgp_ip6_prefix);
1628
1629 memset(peer->outbuf->packet.header.marker, 0xff,
1630 sizeof(peer->outbuf->packet.header.marker));
1631
1632 peer->outbuf->packet.header.type = BGP_MSG_UPDATE;
1633
1634 /* insert non-MP unfeasible routes length */
1635 memcpy(data, &unreach_len, sizeof(unreach_len));
1636 /* skip over it and attr_len too; it will be filled when known */
1637 data += sizeof(unreach_len) + sizeof(attr_len);
1638 len += sizeof(unreach_len) + sizeof(attr_len);
1639
1640 /* copy usual attributes */
1641 memcpy(data, peer->path_attrs, peer->path_attr_len_without_nexthop);
1642 data += peer->path_attr_len_without_nexthop;
1643 attr_len = peer->path_attr_len_without_nexthop;
1644
1645 /* copy MP unreachable NLRI heading */
1646 memcpy(data, peer->mp_unreach_nlri_partial,
1647 BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE);
1648 /* remember where to update this attr len */
1649 unreach_len_pos = data + 2;
1650 data += BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE;
1651 attr_len += BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE;
1652
1653 peer->update_routes6 = 0; /* tentatively clear */
1654
1655 /* find differences */
1656 while ((have || want) && data < (max - sizeof(struct bgp_ip6_prefix)))
1657 {
1658 if (have)
1659 s = want
1660 ? memcmp(&have->dest, &want->dest, sizeof(have->dest))
1661 : -1;
1662 else
1663 s = 1;
1664
1665 if (s < 0) /* found one to delete */
1666 {
1667 struct bgp_route6_list *tmp = have;
1668 have = have->next;
1669
1670 s = BGP_IP_PREFIX_SIZE(tmp->dest);
1671 memcpy(data, &tmp->dest, s);
1672 data += s;
1673 unreach_len += s;
1674 attr_len += s;
1675
1676 LOG(5, 0, 0, "Withdrawing route %s/%d from BGP peer %s\n",
1677 inet_ntop(AF_INET6, &tmp->dest.prefix, ipv6addr, INET6_ADDRSTRLEN),
1678 tmp->dest.len, peer->name);
1679
1680 free(tmp);
1681
1682 if (e)
1683 e->next = have;
1684 else
1685 peer->routes6 = have;
1686 }
1687 else
1688 {
1689 if (!s) /* same */
1690 {
1691 e = have; /* stash the last found to relink above */
1692 have = have->next;
1693 want = want->next;
1694 }
1695 else if (s > 0) /* addition reqd. */
1696 {
1697 if (add)
1698 {
1699 peer->update_routes6 = 1; /* only one add per packet */
1700 if (!have)
1701 break;
1702 }
1703 else
1704 add = want;
1705
1706 if (want)
1707 want = want->next;
1708 }
1709 }
1710 }
1711
1712 if (have || want)
1713 peer->update_routes6 = 1; /* more to do */
1714
1715 /* anything changed? */
1716 if (!(unreach_len || add))
1717 return 1;
1718
1719 if (unreach_len)
1720 {
1721 /* go back and insert MP unreach_len */
1722 unreach_len += sizeof(struct bgp_attr_mp_unreach_nlri_partial);
1723 unreach_len = htons(unreach_len);
1724 memcpy(unreach_len_pos, &unreach_len, sizeof(unreach_len));
1725 }
1726 else
1727 {
1728 /* we can remove this attribute, then */
1729 data -= BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE;
1730 attr_len -= BGP_PATH_ATTR_MP_UNREACH_NLRI_PARTIAL_SIZE;
1731 }
1732
1733 if (add)
1734 {
1735 if (!(e = malloc(sizeof(*e))))
1736 {
1737 LOG(0, 0, 0, "Can't allocate route for %s/%d (%s)\n",
1738 inet_ntop(AF_INET6, &add->dest.prefix, ipv6addr, INET6_ADDRSTRLEN),
1739 add->dest.len, strerror(errno));
1740
1741 return 0;
1742 }
1743
1744 memcpy(e, add, sizeof(*e));
1745 e->next = 0;
1746 peer->routes6 = bgp_insert_route6(peer->routes6, e);
1747
1748 /* copy MP reachable NLRI heading */
1749 memcpy(data, peer->mp_reach_nlri_partial,
1750 BGP_PATH_ATTR_MP_REACH_NLRI_PARTIAL_SIZE);
1751 /* with proper len */
1752 reach_len = BGP_IP_PREFIX_SIZE(add->dest);
1753 data[2] = sizeof(struct bgp_attr_mp_reach_nlri_partial) + reach_len;
1754 data += BGP_PATH_ATTR_MP_REACH_NLRI_PARTIAL_SIZE;
1755 attr_len += BGP_PATH_ATTR_MP_REACH_NLRI_PARTIAL_SIZE;
1756
1757 memcpy(data, &add->dest, reach_len);
1758 data += reach_len;
1759 attr_len += reach_len;
1760
1761 LOG(5, 0, 0, "Advertising route %s/%d to BGP peer %s\n",
1762 inet_ntop(AF_INET6, &add->dest.prefix, ipv6addr, INET6_ADDRSTRLEN),
1763 add->dest.len, peer->name);
1764 }
1765
1766 /* update len with attributes we added */
1767 len += attr_len;
1768
1769 /* go back and insert attr_len */
1770 attr_len = htons(attr_len);
1771 memcpy((char *)&peer->outbuf->packet.data + 2, &attr_len, sizeof(attr_len));
1772
1773 peer->outbuf->packet.header.len = htons(len);
1774 peer->outbuf->done = 0;
1775
1776 return bgp_write(peer);
1777 }
1778
1779 /* send/buffer NOTIFICATION message */
1780 static int bgp_send_notification(struct bgp_peer *peer, uint8_t code,
1781 uint8_t subcode)
1782 {
1783 struct bgp_data_notification data;
1784 uint16_t len = 0;
1785
1786 data.error_code = code;
1787 len += sizeof(data.error_code);
1788
1789 data.error_subcode = subcode;
1790 len += sizeof(data.error_code);
1791
1792 memset(peer->outbuf->packet.header.marker, 0xff,
1793 sizeof(peer->outbuf->packet.header.marker));
1794
1795 peer->outbuf->packet.header.type = BGP_MSG_NOTIFICATION;
1796 peer->outbuf->packet.header.len =
1797 htons(sizeof(peer->outbuf->packet.header) + len);
1798
1799 memcpy(peer->outbuf->packet.data, &data, len);
1800
1801 peer->outbuf->done = 0;
1802 peer->next_state = code == BGP_ERR_CEASE ? Disabled : Idle;
1803
1804 /* we're dying; ignore any pending input */
1805 peer->inbuf->packet.header.len = 0;
1806 peer->inbuf->done = 0;
1807
1808 return bgp_write(peer);
1809 }