diff options
author | Ondrej Zajicek (work) <santiago@crfreenet.org> | 2017-12-07 21:54:47 +0100 |
---|---|---|
committer | Ondrej Zajicek (work) <santiago@crfreenet.org> | 2017-12-07 21:54:47 +0100 |
commit | 830ba75e6dd369c3e64d122f0537cc85211e56e6 (patch) | |
tree | 23989d7955618540ab2bad467d6e376229ad922c /proto | |
parent | 46434a3cad99260b5a659e5df874eab4615bcb36 (diff) | |
parent | 1e8721e2aeccfbc3f533e8b8abc07582cee77e9a (diff) |
Merge commit '1e8721e2aeccfbc3f533e8b8abc07582cee77e9a' into int-new
Diffstat (limited to 'proto')
-rw-r--r-- | proto/babel/packets.c | 23 | ||||
-rw-r--r-- | proto/bgp/bgp.c | 44 | ||||
-rw-r--r-- | proto/bgp/bgp.h | 2 | ||||
-rw-r--r-- | proto/bgp/packets.c | 58 | ||||
-rw-r--r-- | proto/radv/config.Y | 49 | ||||
-rw-r--r-- | proto/radv/packets.c | 97 | ||||
-rw-r--r-- | proto/radv/radv.c | 345 | ||||
-rw-r--r-- | proto/radv/radv.h | 48 |
8 files changed, 521 insertions, 145 deletions
diff --git a/proto/babel/packets.c b/proto/babel/packets.c index 5b356fae..dd86222a 100644 --- a/proto/babel/packets.c +++ b/proto/babel/packets.c @@ -42,7 +42,7 @@ struct babel_tlv_ack { struct babel_tlv_hello { u8 type; u8 length; - u16 reserved; + u16 flags; u16 seqno; u16 interval; } PACKED; @@ -106,8 +106,12 @@ struct babel_tlv_seqno_request { } PACKED; -#define BABEL_FLAG_DEF_PREFIX 0x80 -#define BABEL_FLAG_ROUTER_ID 0x40 +/* Hello flags */ +#define BABEL_HF_UNICAST 0x8000 + +/* Update flags */ +#define BABEL_UF_DEF_PREFIX 0x80 +#define BABEL_UF_ROUTER_ID 0x40 struct babel_parse_state { @@ -343,6 +347,11 @@ babel_read_hello(struct babel_tlv *hdr, union babel_msg *m, struct babel_tlv_hello *tlv = (void *) hdr; struct babel_msg_hello *msg = &m->hello; + /* We currently don't support unicast Hello */ + u16 flags = get_u16(&tlv->flags); + if (flags & BABEL_HF_UNICAST) + return PARSE_IGNORE; + msg->type = BABEL_TLV_HELLO; msg->seqno = get_u16(&tlv->seqno); msg->interval = get_time16(&tlv->interval); @@ -606,7 +615,7 @@ babel_read_update(struct babel_tlv *hdr, union babel_msg *m, ip4_addr prefix4 = get_ip4(buf); net_fill_ip4(&msg->net, prefix4, tlv->plen); - if (tlv->flags & BABEL_FLAG_DEF_PREFIX) + if (tlv->flags & BABEL_UF_DEF_PREFIX) { put_ip4(state->def_ip4_prefix, prefix4); state->def_ip4_prefix_seen = 1; @@ -631,13 +640,13 @@ babel_read_update(struct babel_tlv *hdr, union babel_msg *m, ip6_addr prefix6 = get_ip6(buf); net_fill_ip6(&msg->net, prefix6, tlv->plen); - if (tlv->flags & BABEL_FLAG_DEF_PREFIX) + if (tlv->flags & BABEL_UF_DEF_PREFIX) { put_ip6(state->def_ip6_prefix, prefix6); state->def_ip6_prefix_seen = 1; } - if (tlv->flags & BABEL_FLAG_ROUTER_ID) + if (tlv->flags & BABEL_UF_ROUTER_ID) { state->router_id = ((u64) _I2(prefix6)) << 32 | _I3(prefix6); state->router_id_seen = 1; @@ -750,7 +759,7 @@ babel_write_update(struct babel_tlv *hdr, union babel_msg *m, else { put_ip6_px(tlv->addr, &msg->net); - tlv->flags |= BABEL_FLAG_DEF_PREFIX; + tlv->flags |= BABEL_UF_DEF_PREFIX; put_ip6(state->def_ip6_prefix, net6_prefix(&msg->net)); state->def_ip6_pxlen = tlv->plen; diff --git a/proto/bgp/bgp.c b/proto/bgp/bgp.c index b0814791..30fe75ba 100644 --- a/proto/bgp/bgp.c +++ b/proto/bgp/bgp.c @@ -403,7 +403,7 @@ bgp_update_startup_delay(struct bgp_proto *p) } static void -bgp_graceful_close_conn(struct bgp_conn *conn, uint subcode) +bgp_graceful_close_conn(struct bgp_conn *conn, uint subcode, byte *data, uint len) { switch (conn->state) { @@ -419,7 +419,7 @@ bgp_graceful_close_conn(struct bgp_conn *conn, uint subcode) case BS_OPENSENT: case BS_OPENCONFIRM: case BS_ESTABLISHED: - bgp_error(conn, 6, subcode, NULL, 0); + bgp_error(conn, 6, subcode, data, len); return; default: @@ -459,11 +459,11 @@ bgp_decision(void *vp) } void -bgp_stop(struct bgp_proto *p, uint subcode) +bgp_stop(struct bgp_proto *p, uint subcode, byte *data, uint len) { proto_notify_state(&p->p, PS_STOP); - bgp_graceful_close_conn(&p->outgoing_conn, subcode); - bgp_graceful_close_conn(&p->incoming_conn, subcode); + bgp_graceful_close_conn(&p->outgoing_conn, subcode, data, len); + bgp_graceful_close_conn(&p->incoming_conn, subcode, data, len); ev_schedule(p->event); } @@ -606,7 +606,7 @@ bgp_conn_leave_established_state(struct bgp_proto *p) // bgp_free_bucket_table(p); if (p->p.proto_state == PS_UP) - bgp_stop(p, 0); + bgp_stop(p, 0, NULL, 0); } void @@ -725,7 +725,7 @@ bgp_graceful_restart_timeout(timer *t) struct bgp_proto *p = t->data; BGP_TRACE(D_EVENTS, "Neighbor graceful restart timeout"); - bgp_stop(p, 0); + bgp_stop(p, 0, NULL, 0); } @@ -1125,7 +1125,7 @@ bgp_neigh_notify(neighbor *n) BGP_TRACE(D_EVENTS, "Neighbor lost"); bgp_store_error(p, NULL, BE_MISC, BEM_NEIGHBOR_LOST); /* Perhaps also run bgp_update_startup_delay(p)? */ - bgp_stop(p, 0); + bgp_stop(p, 0, NULL, 0); } } else if (p->cf->check_link && !(n->iface->flags & IF_LINK_UP)) @@ -1136,7 +1136,7 @@ bgp_neigh_notify(neighbor *n) bgp_store_error(p, NULL, BE_MISC, BEM_LINK_DOWN); if (ps == PS_UP) bgp_update_startup_delay(p); - bgp_stop(p, 0); + bgp_stop(p, 0, NULL, 0); } } else @@ -1161,7 +1161,7 @@ bgp_bfd_notify(struct bfd_request *req) bgp_store_error(p, NULL, BE_MISC, BEM_BFD_DOWN); if (ps == PS_UP) bgp_update_startup_delay(p); - bgp_stop(p, 0); + bgp_stop(p, 0, NULL, 0); } } @@ -1349,6 +1349,10 @@ bgp_shutdown(struct proto *P) struct bgp_proto *p = (struct bgp_proto *) P; uint subcode = 0; + char *message = NULL; + byte *data = NULL; + uint len = 0; + BGP_TRACE(D_EVENTS, "Shutdown requested"); switch (P->down_code) @@ -1365,10 +1369,12 @@ bgp_shutdown(struct proto *P) case PDC_CMD_DISABLE: case PDC_CMD_SHUTDOWN: subcode = 2; // Errcode 6, 2 - administrative shutdown + message = P->message; break; case PDC_CMD_RESTART: subcode = 4; // Errcode 6, 4 - administrative reset + message = P->message; break; case PDC_RX_LIMIT_HIT: @@ -1393,8 +1399,22 @@ bgp_shutdown(struct proto *P) bgp_store_error(p, NULL, BE_MAN_DOWN, 0); p->startup_delay = 0; + /* RFC 8203 - shutdown communication */ + if (message) + { + uint msg_len = strlen(message); + msg_len = MIN(msg_len, 128); + + /* Buffer will be freed automatically by protocol shutdown */ + data = mb_alloc(p->p.pool, msg_len + 1); + len = msg_len + 1; + + data[0] = msg_len; + memcpy(data+1, message, msg_len); + } + done: - bgp_stop(p, subcode); + bgp_stop(p, subcode, data, len); return p->p.proto_state; } @@ -1785,7 +1805,7 @@ bgp_error(struct bgp_conn *c, uint code, uint subcode, byte *data, int len) if (code != 6) { bgp_update_startup_delay(p); - bgp_stop(p, 0); + bgp_stop(p, 0, NULL, 0); } } diff --git a/proto/bgp/bgp.h b/proto/bgp/bgp.h index 3d940c22..40c4b3f0 100644 --- a/proto/bgp/bgp.h +++ b/proto/bgp/bgp.h @@ -437,7 +437,7 @@ void bgp_graceful_restart_done(struct bgp_channel *c); void bgp_refresh_begin(struct bgp_channel *c); void bgp_refresh_end(struct bgp_channel *c); void bgp_store_error(struct bgp_proto *p, struct bgp_conn *c, u8 class, u32 code); -void bgp_stop(struct bgp_proto *p, unsigned subcode); +void bgp_stop(struct bgp_proto *p, uint subcode, byte *data, uint len); struct rte_source *bgp_find_source(struct bgp_proto *p, u32 path_id); struct rte_source *bgp_get_source(struct bgp_proto *p, u32 path_id); diff --git a/proto/bgp/packets.c b/proto/bgp/packets.c index 0e974746..038e89f9 100644 --- a/proto/bgp/packets.c +++ b/proto/bgp/packets.c @@ -2678,38 +2678,72 @@ bgp_error_dsc(uint code, uint subcode) return buff; } +/* RFC 8203 - shutdown communication message */ +static int +bgp_handle_message(struct bgp_proto *p, byte *data, uint len, byte **bp) +{ + byte *msg = data + 1; + uint msg_len = data[0]; + uint i; + + /* Handle zero length message */ + if (msg_len == 0) + return 1; + + /* Handle proper message */ + if ((msg_len > 128) && (msg_len + 1 > len)) + return 0; + + /* Some elementary cleanup */ + for (i = 0; i < msg_len; i++) + if (msg[i] < ' ') + msg[i] = ' '; + + proto_set_message(&p->p, msg, msg_len); + *bp += bsprintf(*bp, ": \"%s\"", p->p.message); + return 1; +} + void bgp_log_error(struct bgp_proto *p, u8 class, char *msg, uint code, uint subcode, byte *data, uint len) { - const byte *name; - byte *t, argbuf[36]; + byte argbuf[256], *t = argbuf; uint i; /* Don't report Cease messages generated by myself */ if (code == 6 && class == BE_BGP_TX) return; - name = bgp_error_dsc(code, subcode); - t = argbuf; + /* Reset shutdown message */ + if ((code == 6) && ((subcode == 2) || (subcode == 4))) + proto_set_message(&p->p, NULL, 0); + if (len) { - *t++ = ':'; - *t++ = ' '; - + /* Bad peer AS - we would like to print the AS */ if ((code == 2) && (subcode == 2) && ((len == 2) || (len == 4))) { - /* Bad peer AS - we would like to print the AS */ - t += bsprintf(t, "%u", (len == 2) ? get_u16(data) : get_u32(data)); + t += bsprintf(t, ": %u", (len == 2) ? get_u16(data) : get_u32(data)); goto done; } + + /* RFC 8203 - shutdown communication */ + if (((code == 6) && ((subcode == 2) || (subcode == 4)))) + if (bgp_handle_message(p, data, len, &t)) + goto done; + + *t++ = ':'; + *t++ = ' '; if (len > 16) len = 16; for (i=0; i<len; i++) t += bsprintf(t, "%02x", data[i]); } - done: + +done: *t = 0; - log(L_REMOTE "%s: %s: %s%s", p->p.name, msg, name, argbuf); + const byte *dsc = bgp_error_dsc(code, subcode); + log(L_REMOTE "%s: %s: %s%s", p->p.name, msg, dsc, argbuf); } static void @@ -2733,7 +2767,7 @@ bgp_rx_notification(struct bgp_conn *conn, byte *pkt, uint len) if (err) { bgp_update_startup_delay(p); - bgp_stop(p, 0); + bgp_stop(p, 0, NULL, 0); } } diff --git a/proto/radv/config.Y b/proto/radv/config.Y index 0e43c237..37815f0d 100644 --- a/proto/radv/config.Y +++ b/proto/radv/config.Y @@ -30,7 +30,10 @@ CF_KEYWORDS(RADV, PREFIX, INTERFACE, MIN, MAX, RA, DELAY, INTERVAL, MANAGED, OTHER, CONFIG, LINGER, LINK, MTU, REACHABLE, TIME, RETRANS, TIMER, CURRENT, HOP, LIMIT, DEFAULT, VALID, PREFERRED, MULT, LIFETIME, SKIP, ONLINK, AUTONOMOUS, RDNSS, DNSSL, NS, DOMAIN, - LOCAL, TRIGGER, SENSITIVE, PREFERENCE, LOW, MEDIUM, HIGH) + LOCAL, TRIGGER, SENSITIVE, PREFERENCE, LOW, MEDIUM, HIGH, PROPAGATE, + ROUTE, ROUTES, RA_PREFERENCE, RA_LIFETIME) + +CF_ENUM(T_ENUM_RA_PREFERENCE, RA_PREF_, LOW, MEDIUM, HIGH) %type<i> radv_mult radv_sensitive radv_preference @@ -56,6 +59,7 @@ radv_proto_item: | RDNSS { init_list(&radv_dns_list); } radv_rdnss { add_tail_list(&RADV_CFG->rdnss_list, &radv_dns_list); } | DNSSL { init_list(&radv_dns_list); } radv_dnssl { add_tail_list(&RADV_CFG->dnssl_list, &radv_dns_list); } | TRIGGER net_ip6 { RADV_CFG->trigger = $2; } + | PROPAGATE ROUTES bool { RADV_CFG->propagate_routes = $3; } ; radv_proto_opts: @@ -76,14 +80,18 @@ radv_iface_start: init_list(&RADV_IFACE->rdnss_list); init_list(&RADV_IFACE->dnssl_list); - RADV_IFACE->min_ra_int = -1; /* undefined */ + RADV_IFACE->min_ra_int = (u32) -1; /* undefined */ RADV_IFACE->max_ra_int = DEFAULT_MAX_RA_INT; RADV_IFACE->min_delay = DEFAULT_MIN_DELAY; + RADV_IFACE->prefix_linger_time = (u32) -1; + RADV_IFACE->route_linger_time = (u32) -1; RADV_IFACE->current_hop_limit = DEFAULT_CURRENT_HOP_LIMIT; - RADV_IFACE->linger_time = DEFAULT_LINGER_TIME; - RADV_IFACE->default_lifetime = -1; + RADV_IFACE->default_lifetime = (u32) -1; RADV_IFACE->default_lifetime_sensitive = 1; RADV_IFACE->default_preference = RA_PREF_MEDIUM; + RADV_IFACE->route_lifetime = (u32) -1; + RADV_IFACE->route_lifetime_sensitive = 0; + RADV_IFACE->route_preference = RA_PREF_MEDIUM; }; radv_iface_item: @@ -95,14 +103,20 @@ radv_iface_item: | LINK MTU expr { RADV_IFACE->link_mtu = $3; } | REACHABLE TIME expr { RADV_IFACE->reachable_time = $3; if ($3 > 3600000) cf_error("Reachable time must be in range 0-3600000"); } | RETRANS TIMER expr { RADV_IFACE->retrans_timer = $3; } - | LINGER TIME expr { RADV_IFACE->linger_time = $3; if ($3 > 3600) cf_error("Linger time must be in range 0-3600"); } | CURRENT HOP LIMIT expr { RADV_IFACE->current_hop_limit = $4; if ($4 > 255) cf_error("Current hop limit must be in range 0-255"); } | DEFAULT LIFETIME expr radv_sensitive { RADV_IFACE->default_lifetime = $3; if ($3 > 9000) cf_error("Default lifetime must be in range 0-9000"); if ($4 != (uint) -1) RADV_IFACE->default_lifetime_sensitive = $4; } + | ROUTE LIFETIME expr radv_sensitive { + RADV_IFACE->route_lifetime = $3; + if ($4 != (uint) -1) RADV_IFACE->route_lifetime_sensitive = $4; + } | DEFAULT PREFERENCE radv_preference { RADV_IFACE->default_preference = $3; } + | ROUTE PREFERENCE radv_preference { RADV_IFACE->route_preference = $3; } + | PREFIX LINGER TIME expr { RADV_IFACE->prefix_linger_time = $4; } + | ROUTE LINGER TIME expr { RADV_IFACE->route_linger_time = $4; } | PREFIX radv_prefix { add_tail(&RADV_IFACE->pref_list, NODE this_radv_prefix); } | RDNSS { init_list(&radv_dns_list); } radv_rdnss { add_tail_list(&RADV_IFACE->rdnss_list, &radv_dns_list); } | DNSSL { init_list(&radv_dns_list); } radv_dnssl { add_tail_list(&RADV_IFACE->dnssl_list, &radv_dns_list); } @@ -125,12 +139,32 @@ radv_iface_finish: if (ic->default_lifetime == (u32) -1) ic->default_lifetime = 3 * ic->max_ra_int; + if (ic->route_lifetime == (u32) -1) + ic->route_lifetime = 3 * ic->max_ra_int; + + if (ic->prefix_linger_time == (u32) -1) + ic->prefix_linger_time = 3 * ic->max_ra_int; + + if (ic->route_linger_time == (u32) -1) + ic->route_linger_time = 3 * ic->max_ra_int; + if ((ic->min_ra_int > 3) && (ic->min_ra_int > (ic->max_ra_int * 3 / 4))) cf_error("Min RA interval must be at most 3/4 * Max RA interval"); if ((ic->default_lifetime > 0) && (ic->default_lifetime < ic->max_ra_int)) cf_error("Default lifetime must be either 0 or at least Max RA interval"); + + if ((ic->route_lifetime > 0) && (ic->route_lifetime < ic->max_ra_int)) + cf_error("Route lifetime must be either 0 or at least Max RA interval"); + + if ((ic->prefix_linger_time > 0) && (ic->prefix_linger_time < ic->max_ra_int)) + cf_error("Prefix linger time must be either 0 or at least Max RA interval"); + + if ((ic->route_linger_time > 0) && (ic->route_linger_time < ic->max_ra_int)) + cf_error("Route linger time must be either 0 or at least Max RA interval"); + + RADV_CFG->max_linger_time = MAX_(RADV_CFG->max_linger_time, ic->route_linger_time); }; @@ -294,10 +328,13 @@ radv_mult: ; radv_sensitive: - /* empty */ { $$ = -1; } + /* empty */ { $$ = (uint) -1; } | SENSITIVE bool { $$ = $2; } ; +CF_ADDTO(dynamic_attr, RA_PREFERENCE { $$ = f_new_dynamic_attr(EAF_TYPE_INT, T_ENUM_RA_PREFERENCE, EA_RA_PREFERENCE); }) +CF_ADDTO(dynamic_attr, RA_LIFETIME { $$ = f_new_dynamic_attr(EAF_TYPE_INT, T_INT, EA_RA_LIFETIME); }) + CF_CODE CF_END diff --git a/proto/radv/packets.c b/proto/radv/packets.c index 7c148b7d..b12d3a12 100644 --- a/proto/radv/packets.c +++ b/proto/radv/packets.c @@ -26,6 +26,7 @@ struct radv_ra_packet #define OPT_PREFIX 3 #define OPT_MTU 5 +#define OPT_ROUTE 24 #define OPT_RDNSS 25 #define OPT_DNSSL 31 @@ -52,6 +53,15 @@ struct radv_opt_mtu u32 mtu; }; +struct radv_opt_route { + u8 type; + u8 length; + u8 pxlen; + u8 flags; + u32 lifetime; + u8 prefix[]; +}; + struct radv_opt_rdnss { u8 type; @@ -71,6 +81,44 @@ struct radv_opt_dnssl }; static int +radv_prepare_route(struct radv_iface *ifa, struct radv_route *rt, + char **buf, char *bufend) +{ + struct radv_proto *p = ifa->ra; + u8 px_blocks = (net6_pxlen(rt->n.addr) + 63) / 64; + u8 opt_len = 8 * (1 + px_blocks); + + if (*buf + opt_len > bufend) + { + log(L_WARN, "%s: Too many RA options on interface %s", + p->p.name, ifa->iface->name); + return -1; + } + + uint preference = rt->preference_set ? rt->preference : ifa->cf->route_preference; + uint lifetime = rt->lifetime_set ? rt->lifetime : ifa->cf->route_lifetime; + uint valid = rt->valid && p->valid && (p->active || !ifa->cf->route_lifetime_sensitive); + + struct radv_opt_route *opt = (void *) *buf; + *buf += opt_len; + opt->type = OPT_ROUTE; + opt->length = 1 + px_blocks; + opt->pxlen = net6_pxlen(rt->n.addr); + opt->flags = preference; + opt->lifetime = valid ? htonl(lifetime) : 0; + + /* Copy the relevant part of the prefix */ + ip6_addr px_addr = ip6_hton(net6_prefix(rt->n.addr)); + memcpy(opt->prefix, &px_addr, 8 * px_blocks); + + /* Keeping track of first linger timeout */ + if (!rt->valid) + ifa->valid_time = MIN(ifa->valid_time, rt->changed + ifa->cf->route_linger_time S); + + return 0; +} + +static int radv_prepare_rdnss(struct radv_iface *ifa, list *rdnss_list, char **buf, char *bufend) { struct radv_rdnss_config *rcf = HEAD(*rdnss_list); @@ -231,6 +279,10 @@ radv_prepare_prefix(struct radv_iface *ifa, struct radv_prefix *px, op->prefix = ip6_hton(px->prefix.prefix); *buf += sizeof(*op); + /* Keeping track of first linger timeout */ + if (!px->valid) + ifa->valid_time = MIN(ifa->valid_time, px->changed + ifa->cf->prefix_linger_time S); + return 0; } @@ -240,6 +292,7 @@ radv_prepare_ra(struct radv_iface *ifa) struct radv_proto *p = ifa->ra; struct radv_config *cf = (struct radv_config *) (p->p.cf); struct radv_iface_config *ic = ifa->cf; + btime now = current_time(); char *buf = ifa->sk->tbuf; char *bufstart = buf; @@ -250,7 +303,7 @@ radv_prepare_ra(struct radv_iface *ifa) pkt->code = 0; pkt->checksum = 0; pkt->current_hop_limit = ic->current_hop_limit; - pkt->router_lifetime = (p->active || !ic->default_lifetime_sensitive) ? + pkt->router_lifetime = (p->valid && (p->active || !ic->default_lifetime_sensitive)) ? htons(ic->default_lifetime) : 0; pkt->flags = (ic->managed ? OPT_RA_MANAGED : 0) | (ic->other_config ? OPT_RA_OTHER_CFG : 0) | @@ -269,10 +322,17 @@ radv_prepare_ra(struct radv_iface *ifa) buf += sizeof (*om); } - struct radv_prefix *prefix; - WALK_LIST(prefix, ifa->prefixes) + /* Keeping track of first linger timeout */ + ifa->valid_time = TIME_INFINITY; + + struct radv_prefix *px; + WALK_LIST(px, ifa->prefixes) { - if (radv_prepare_prefix(ifa, prefix, &buf, bufend) < 0) + /* Skip invalid prefixes that are past linger timeout but still not pruned */ + if (!px->valid && ((px->changed + ic->prefix_linger_time S) <= now)) + continue; + + if (radv_prepare_prefix(ifa, px, &buf, bufend) < 0) goto done; } @@ -290,13 +350,27 @@ radv_prepare_ra(struct radv_iface *ifa) if (radv_prepare_dnssl(ifa, &ic->dnssl_list, &buf, bufend) < 0) goto done; + if (p->fib_up) + { + FIB_WALK(&p->routes, struct radv_route, rt) + { + /* Skip invalid routes that are past linger timeout but still not pruned */ + if (!rt->valid && ((rt->changed + ic->route_linger_time S) <= now)) + continue; + + if (radv_prepare_route(ifa, rt, &buf, bufend) < 0) + goto done; + } + FIB_WALK_END; + } + done: ifa->plen = buf - bufstart; } void -radv_send_ra(struct radv_iface *ifa, int shutdown) +radv_send_ra(struct radv_iface *ifa) { struct radv_proto *p = ifa->ra; @@ -304,19 +378,6 @@ radv_send_ra(struct radv_iface *ifa, int shutdown) if (!ifa->plen) radv_prepare_ra(ifa); - if (shutdown) - { - /* - * Modify router lifetime to 0, it is not restored because we suppose that - * the iface will be removed. The preference value also has to be zeroed. - * (RFC 4191 2.2: If router lifetime is 0, the preference value must be 0.) - */ - - struct radv_ra_packet *pkt = (void *) ifa->sk->tbuf; - pkt->router_lifetime = 0; - pkt->flags &= ~RA_PREF_MASK; - } - RADV_TRACE(D_PACKETS, "Sending RA via %s", ifa->iface->name); sk_send_to(ifa->sk, ifa->plen, IP6_ALL_NODES, 0); } diff --git a/proto/radv/radv.c b/proto/radv/radv.c index e9140115..0a2a3e78 100644 --- a/proto/radv/radv.c +++ b/proto/radv/radv.c @@ -12,59 +12,63 @@ /** * DOC: Router Advertisements * - * The RAdv protocol is implemented in two files: |radv.c| containing - * the interface with BIRD core and the protocol logic and |packets.c| - * handling low level protocol stuff (RX, TX and packet formats). - * The protocol does not export any routes. + * The RAdv protocol is implemented in two files: |radv.c| containing the + * interface with BIRD core and the protocol logic and |packets.c| handling low + * level protocol stuff (RX, TX and packet formats). The protocol does not + * export any routes. * - * The RAdv is structured in the usual way - for each handled interface - * there is a structure &radv_iface that contains a state related to - * that interface together with its resources (a socket, a timer). - * There is also a prepared RA stored in a TX buffer of the socket - * associated with an iface. These iface structures are created - * and removed according to iface events from BIRD core handled by - * radv_if_notify() callback. + * The RAdv is structured in the usual way - for each handled interface there is + * a structure &radv_iface that contains a state related to that interface + * together with its resources (a socket, a timer). There is also a prepared RA + * stored in a TX buffer of the socket associated with an iface. These iface + * structures are created and removed according to iface events from BIRD core + * handled by radv_if_notify() callback. * - * The main logic of RAdv consists of two functions: - * radv_iface_notify(), which processes asynchronous events (specified - * by RA_EV_* codes), and radv_timer(), which triggers sending RAs and - * computes the next timeout. + * The main logic of RAdv consists of two functions: radv_iface_notify(), which + * processes asynchronous events (specified by RA_EV_* codes), and radv_timer(), + * which triggers sending RAs and computes the next timeout. * - * The RAdv protocol could receive routes (through - * radv_import_control() and radv_rt_notify()), but only the - * configured trigger route is tracked (in &active var). When a radv - * protocol is reconfigured, the connected routing table is examined - * (in radv_check_active()) to have proper &active value in case of - * the specified trigger prefix was changed. + * The RAdv protocol could receive routes (through radv_import_control() and + * radv_rt_notify()), but only the configured trigger route is tracked (in + * &active var). When a radv protocol is reconfigured, the connected routing + * table is examined (in radv_check_active()) to have proper &active value in + * case of the specified trigger prefix was changed. * * Supported standards: * - RFC 4861 - main RA standard + * - RFC 4191 - Default Router Preferences and More-Specific Routes * - RFC 6106 - DNS extensions (RDDNS, DNSSL) - * - RFC 4191 (partial) - Default Router Preference */ +static void radv_prune_prefixes(struct radv_iface *ifa); +static void radv_prune_routes(struct radv_proto *p); + +/* Invalidate cached RA packet */ +static inline void radv_invalidate(struct radv_iface *ifa) +{ ifa->plen = 0; } + static void radv_timer(timer *tm) { struct radv_iface *ifa = tm->data; struct radv_proto *p = ifa->ra; + btime now = current_time(); RADV_TRACE(D_EVENTS, "Timer fired on %s", ifa->iface->name); - /* - * If some dead prefixes expired, regenerate the prefix list and the packet. - * We do so by pretending there was a change on the interface. - * - * This sets the timer, but we replace it just at the end of this function - * (replacing a timer is fine). - */ - if (ifa->prefix_expires && (ifa->prefix_expires <= current_time())) - radv_iface_notify(ifa, RA_EV_GC); + if (ifa->valid_time <= now) + radv_invalidate(ifa); + + if (ifa->prune_time <= now) + radv_prune_prefixes(ifa); - radv_send_ra(ifa, 0); + if (p->prune_time <= now) + radv_prune_routes(p); + + radv_send_ra(ifa); /* Update timer */ - ifa->last = current_time(); + ifa->last = now; btime t = ifa->cf->min_ra_int S; btime r = (ifa->cf->max_ra_int - ifa->cf->min_ra_int) S; t += random() % (r + 1); @@ -115,8 +119,8 @@ static void radv_prepare_prefixes(struct radv_iface *ifa) { struct radv_proto *p = ifa->ra; - struct radv_iface_config *cf = ifa->cf; - struct radv_prefix *pfx; + struct radv_prefix *pfx, *next; + btime now = current_time(); /* First mark all the prefixes as unused */ WALK_LIST(pfx, ifa->prefixes) @@ -159,55 +163,58 @@ radv_prepare_prefixes(struct radv_iface *ifa) * Update the information (it may have changed, or even bring a prefix back * to life). */ - existing->alive = 1; + existing->valid = 1; + existing->changed = now; existing->mark = 1; existing->cf = pc; } - /* - * Garbage-collect the prefixes. If something isn't used, it dies (but isn't - * dropped just yet). If something is dead and rots there for long enough, - * clean it up. - */ - btime now_ = current_time(); - btime expires = now_ + cf->linger_time S; - btime expires_min = 0; - struct radv_prefix *next; WALK_LIST_DELSAFE(pfx, next, ifa->prefixes) { - if (pfx->alive && !pfx->mark) + if (pfx->valid && !pfx->mark) { - RADV_TRACE(D_EVENTS, "Marking prefix %N on %s as dead", + RADV_TRACE(D_EVENTS, "Invalidating prefix %N on %s", pfx->prefix, ifa->iface->name); - pfx->alive = 0; - pfx->expires = expires; + pfx->valid = 0; + pfx->changed = now; pfx->cf = &dead_prefix; } + } +} + +static void +radv_prune_prefixes(struct radv_iface *ifa) +{ + struct radv_proto *p = ifa->ra; + btime now = current_time(); + btime next = TIME_INFINITY; + btime expires = 0; - if (!pfx->alive) + struct radv_prefix *px, *pxn; + WALK_LIST_DELSAFE(px, pxn, ifa->prefixes) + { + if (!px->valid) { - if (pfx->expires <= now_) + expires = px->changed + ifa->cf->prefix_linger_time S; + + if (expires <= now) { RADV_TRACE(D_EVENTS, "Removing prefix %N on %s", - pfx->prefix, ifa->iface->name); + px->prefix, ifa->iface->name); - rem_node(NODE pfx); - mb_free(pfx); + rem_node(NODE px); + mb_free(px); } else - { - /* Find minimum expiration time */ - if (!expires_min || (pfx->expires < expires_min)) - expires_min = pfx->expires; - } + next = MIN(next, expires); } } - ifa->prefix_expires = expires_min; + ifa->prune_time = next; } -static char* ev_name[] = { NULL, "Init", "Change", "RS", "Garbage collect" }; +static char* ev_name[] = { NULL, "Init", "Change", "RS" }; void radv_iface_notify(struct radv_iface *ifa, int event) @@ -222,18 +229,17 @@ radv_iface_notify(struct radv_iface *ifa, int event) switch (event) { case RA_EV_CHANGE: - case RA_EV_GC: - ifa->plen = 0; + radv_invalidate(ifa); case RA_EV_INIT: ifa->initial = MAX_INITIAL_RTR_ADVERTISEMENTS; + radv_prepare_prefixes(ifa); + radv_prune_prefixes(ifa); break; case RA_EV_RS: break; } - radv_prepare_prefixes(ifa); - /* Update timer */ btime t = ifa->last + ifa->cf->min_delay S - current_time(); tm_start(ifa->timer, t); @@ -248,7 +254,6 @@ radv_iface_notify_all(struct radv_proto *p, int event) radv_iface_notify(ifa, event); } - static struct radv_iface * radv_iface_find(struct radv_proto *p, struct iface *what) { @@ -291,6 +296,7 @@ radv_iface_new(struct radv_proto *p, struct iface *iface, struct radv_iface_conf ifa->iface = iface; ifa->addr = iface->llv6; init_list(&ifa->prefixes); + ifa->prune_time = TIME_INFINITY; add_tail(&p->iface_list, NODE ifa); @@ -397,14 +403,19 @@ radv_import_control(struct proto *P, rte **new, ea_list **attrs UNUSED, struct l if (radv_net_match_trigger(cf, (*new)->net)) return RIC_PROCESS; - return RIC_DROP; + if (cf->propagate_routes) + return RIC_PROCESS; + else + return RIC_DROP; } static void -radv_rt_notify(struct proto *P, struct channel *ch UNUSED, net *n, rte *new, rte *old UNUSED, ea_list *attrs UNUSED) +radv_rt_notify(struct proto *P, struct channel *ch UNUSED, net *n, rte *new, rte *old UNUSED, ea_list *attrs) { struct radv_proto *p = (struct radv_proto *) P; struct radv_config *cf = (struct radv_config *) (P->cf); + struct radv_route *rt; + eattr *ea; if (radv_net_match_trigger(cf, n)) { @@ -420,7 +431,123 @@ radv_rt_notify(struct proto *P, struct channel *ch UNUSED, net *n, rte *new, rte RADV_TRACE(D_EVENTS, "Suppressed"); radv_iface_notify_all(p, RA_EV_CHANGE); + return; + } + + if (!cf->propagate_routes) + return; + + /* + * Some other route we want to send (or stop sending). Update the cache, + * with marking a removed one as dead or creating a new one as needed. + * + * And yes, we exclude the trigger route on purpose. + */ + + if (new) + { + /* Update */ + + ea = ea_find(attrs, EA_RA_PREFERENCE); + uint preference = ea ? ea->u.data : RA_PREF_MEDIUM; + uint preference_set = !!ea; + + ea = ea_find(attrs, EA_RA_LIFETIME); + uint lifetime = ea ? ea->u.data : 0; + uint lifetime_set = !!ea; + + if ((preference != RA_PREF_LOW) && + (preference != RA_PREF_MEDIUM) && + (preference != RA_PREF_HIGH)) + { + log(L_WARN "%s: Invalid ra_preference value %u on route %N", + p->p.name, preference, n->n.addr); + preference = RA_PREF_MEDIUM; + preference_set = 1; + lifetime = 0; + lifetime_set = 1; + } + + rt = fib_get(&p->routes, n->n.addr); + + /* Ignore update if nothing changed */ + if (rt->valid && + (rt->preference == preference) && + (rt->preference_set == preference_set) && + (rt->lifetime == lifetime) && + (rt->lifetime_set == lifetime_set)) + return; + + if (p->routes.entries == 18) + log(L_WARN "%s: More than 17 routes exported to RAdv", p->p.name); + + rt->valid = 1; + rt->changed = current_time(); + rt->preference = preference; + rt->preference_set = preference_set; + rt->lifetime = lifetime; + rt->lifetime_set = lifetime_set; + } + else + { + /* Withdraw */ + rt = fib_find(&p->routes, n->n.addr); + + if (!rt || !rt->valid) + return; + + /* Invalidate the route */ + rt->valid = 0; + rt->changed = current_time(); + + /* Invalidated route will be pruned eventually */ + btime expires = rt->changed + cf->max_linger_time S; + p->prune_time = MIN(p->prune_time, expires); } + + radv_iface_notify_all(p, RA_EV_CHANGE); +} + +/* + * Cleans up all the dead routes that expired and schedules itself to be run + * again if there are more routes waiting for expiration. + */ +static void +radv_prune_routes(struct radv_proto *p) +{ + struct radv_config *cf = (struct radv_config *) (p->p.cf); + btime now = current_time(); + btime next = TIME_INFINITY; + btime expires = 0; + + /* Should not happen */ + if (!p->fib_up) + return; + + struct fib_iterator fit; + FIB_ITERATE_INIT(&fit, &p->routes); + +again: + FIB_ITERATE_START(&p->routes, &fit, struct radv_route, rt) + { + if (!rt->valid) + { + expires = rt->changed + cf->max_linger_time S; + + /* Delete expired nodes */ + if (expires <= now) + { + FIB_ITERATE_PUT(&fit); + fib_delete(&p->routes, rt); + goto again; + } + else + next = MIN(next, expires); + } + } + FIB_ITERATE_END; + + p->prune_time = next; } static int @@ -460,6 +587,22 @@ radv_init(struct proto_config *CF) return P; } +static void +radv_set_fib(struct radv_proto *p, int up) +{ + if (up == p->fib_up) + return; + + if (up) + fib_init(&p->routes, p->p.pool, NET_IP6, sizeof(struct radv_route), + OFFSETOF(struct radv_route, n), 4, NULL); + else + fib_free(&p->routes); + + p->fib_up = up; + p->prune_time = TIME_INFINITY; +} + static int radv_start(struct proto *P) { @@ -467,8 +610,13 @@ radv_start(struct proto *P) struct radv_config *cf = (struct radv_config *) (P->cf); init_list(&(p->iface_list)); + p->valid = 1; p->active = !radv_trigger_valid(cf); + p->fib_up = 0; + radv_set_fib(p, cf->propagate_routes); + p->prune_time = TIME_INFINITY; + return PS_UP; } @@ -476,7 +624,10 @@ static inline void radv_iface_shutdown(struct radv_iface *ifa) { if (ifa->sk) - radv_send_ra(ifa, 1); + { + radv_invalidate(ifa); + radv_send_ra(ifa); + } } static int @@ -484,6 +635,8 @@ radv_shutdown(struct proto *P) { struct radv_proto *p = (struct radv_proto *) P; + p->valid = 0; + struct radv_iface *ifa; WALK_LIST(ifa, p->iface_list) radv_iface_shutdown(ifa); @@ -495,23 +648,22 @@ static int radv_reconfigure(struct proto *P, struct proto_config *CF) { struct radv_proto *p = (struct radv_proto *) P; - // struct radv_config *old = (struct radv_config *) (p->cf); + struct radv_config *old = (struct radv_config *) (P->cf); struct radv_config *new = (struct radv_config *) CF; - /* - * The question is why there is a reconfigure function for RAdv if - * it has almost none internal state so restarting the protocol - * would probably suffice. One small reason is that restarting the - * protocol would lead to sending a RA with Router Lifetime 0 - * causing nodes to temporary remove their default routes. - */ - if (!proto_configure_channel(P, &P->main_channel, proto_cf_main_channel(CF))) return 0; P->cf = CF; /* radv_check_active() requires proper P->cf */ p->active = radv_check_active(p); + /* Allocate or free FIB */ + radv_set_fib(p, new->propagate_routes); + + /* We started to accept routes so we need to refeed them */ + if (!old->propagate_routes && new->propagate_routes) + channel_request_feeding(p->p.main_channel); + struct iface *iface; WALK_LIST(iface, iface_list) { @@ -574,9 +726,43 @@ radv_get_status(struct proto *P, byte *buf) strcpy(buf, "Suppressed"); } +static const char * +radv_pref_str(u32 pref) +{ + switch (pref) + { + case RA_PREF_LOW: + return "low"; + case RA_PREF_MEDIUM: + return "medium"; + case RA_PREF_HIGH: + return "high"; + default: + return "??"; + } +} + +/* The buffer has some minimal size */ +static int +radv_get_attr(eattr *a, byte *buf, int buflen UNUSED) +{ + switch (a->id) + { + case EA_RA_PREFERENCE: + bsprintf(buf, "preference: %s", radv_pref_str(a->u.data)); + return GA_FULL; + case EA_RA_LIFETIME: + bsprintf(buf, "lifetime"); + return GA_NAME; + default: + return GA_UNKNOWN; + } +} + struct protocol proto_radv = { .name = "RAdv", .template = "radv%d", + .attr_class = EAP_RADV, .channel_mask = NB_IP6, .proto_size = sizeof(struct radv_proto), .config_size = sizeof(struct radv_config), @@ -586,5 +772,6 @@ struct protocol proto_radv = { .shutdown = radv_shutdown, .reconfigure = radv_reconfigure, .copy_config = radv_copy_config, - .get_status = radv_get_status + .get_status = radv_get_status, + .get_attr = radv_get_attr }; diff --git a/proto/radv/radv.h b/proto/radv/radv.h index 4672e3b2..66f785a7 100644 --- a/proto/radv/radv.h +++ b/proto/radv/radv.h @@ -35,7 +35,6 @@ #define DEFAULT_MAX_RA_INT 600 #define DEFAULT_MIN_DELAY 3 #define DEFAULT_CURRENT_HOP_LIMIT 64 -#define DEFAULT_LINGER_TIME 300 #define DEFAULT_VALID_LIFETIME 86400 #define DEFAULT_PREFERRED_LIFETIME 14400 @@ -52,6 +51,8 @@ struct radv_config list dnssl_list; /* Global list of DNSSL configs (struct radv_dnssl_config) */ net_addr trigger; /* Prefix of a trigger route, if defined */ + u8 propagate_routes; /* Do we propagate more specific routes (RFC 4191)? */ + u32 max_linger_time; /* Maximum of interface route_linger_time */ }; struct radv_iface_config @@ -65,8 +66,8 @@ struct radv_iface_config u32 max_ra_int; u32 min_delay; - u32 linger_time; /* How long a dead prefix should still be advertised with 0 - lifetime */ + u32 prefix_linger_time; /* How long we advertise dead prefixes with lifetime 0 */ + u32 route_linger_time; /* How long we advertise dead routes with lifetime 0 */ u8 rdnss_local; /* Global list is not used for RDNSS */ u8 dnssl_local; /* Global list is not used for DNSSL */ @@ -78,8 +79,11 @@ struct radv_iface_config u32 retrans_timer; u32 current_hop_limit; u32 default_lifetime; + u32 route_lifetime; /* Lifetime for the RFC 4191 routes */ u8 default_lifetime_sensitive; /* Whether default_lifetime depends on trigger */ - u8 default_preference; /* Default Router Preference (RFC 4191) */ + u8 route_lifetime_sensitive; /* Whether route_lifetime depends on trigger */ + u8 default_preference; /* Default Router Preference (RFC 4191) */ + u8 route_preference; /* Specific Route Preference (RFC 4191) */ }; struct radv_prefix_config @@ -114,12 +118,34 @@ struct radv_dnssl_config char *domain; /* Domain for DNS search list, in processed form */ }; +/* + * One more specific route as per RFC 4191. + * + * Note that it does *not* contain the next hop field. The next hop is always + * the router sending the advertisment and the more specific route only allows + * overriding the preference of the route. + */ +struct radv_route +{ + u32 lifetime; /* Lifetime from an attribute */ + u8 lifetime_set; /* Whether lifetime is defined */ + u8 preference; /* Preference of the route, RA_PREF_* */ + u8 preference_set; /* Whether preference is defined */ + u8 valid; /* Whethe route is valid or withdrawn */ + btime changed; /* Last time when the route changed */ + + struct fib_node n; +}; struct radv_proto { struct proto p; list iface_list; /* List of active ifaces */ + u8 valid; /* Router is valid for forwarding, used for shutdown */ u8 active; /* Whether radv is active w.r.t. triggers */ + u8 fib_up; /* FIB table (routes) is initialized */ + struct fib routes; /* FIB table of specific routes (struct radv_route) */ + btime prune_time; /* Next time of route table pruning */ }; struct radv_prefix /* One prefix we advertise */ @@ -127,11 +153,10 @@ struct radv_prefix /* One prefix we advertise */ node n; net_addr_ip6 prefix; - u8 alive; /* Is the prefix alive? If not, we advertise it + u8 valid; /* Is the prefix valid? If not, we advertise it with 0 lifetime, so clients stop using it */ u8 mark; /* A temporary mark for processing */ - btime expires; /* The time when we drop this prefix from - advertising. It is valid only if !alive. */ + btime changed; /* Last time when the prefix changed */ struct radv_prefix_config *cf; /* The config tied to this prefix */ }; @@ -144,7 +169,8 @@ struct radv_iface struct ifa *addr; /* Link-local address of iface */ struct pool *pool; /* A pool for interface-specific things */ list prefixes; /* The prefixes we advertise (struct radv_prefix) */ - btime prefix_expires; /* When the soonest prefix expires (0 = none dead) */ + btime prune_time; /* Next time of prefix list pruning */ + btime valid_time; /* Cached packet is valid until first linger timeout */ timer *timer; struct object_lock *lock; @@ -158,7 +184,6 @@ struct radv_iface #define RA_EV_INIT 1 /* Switch to initial mode */ #define RA_EV_CHANGE 2 /* Change of options or prefixes */ #define RA_EV_RS 3 /* Received RS */ -#define RA_EV_GC 4 /* Internal garbage collection of prefixes */ /* Default Router Preferences (RFC 4191) */ #define RA_PREF_LOW 0x18 @@ -166,6 +191,9 @@ struct radv_iface #define RA_PREF_HIGH 0x08 #define RA_PREF_MASK 0x18 +/* Attributes */ +#define EA_RA_PREFERENCE EA_CODE(EAP_RADV, 0) +#define EA_RA_LIFETIME EA_CODE(EAP_RADV, 1) #ifdef LOCAL_DEBUG #define RADV_FORCE_DEBUG 1 @@ -181,7 +209,7 @@ void radv_iface_notify(struct radv_iface *ifa, int event); /* packets.c */ int radv_process_domain(struct radv_dnssl_config *cf); -void radv_send_ra(struct radv_iface *ifa, int shutdown); +void radv_send_ra(struct radv_iface *ifa); int radv_sk_open(struct radv_iface *ifa); |