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