From: renato Date: Mon, 8 Aug 2016 16:45:51 +0000 (+0000) Subject: Normalize the received prefixes. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=00f5e67face1b66f4db4de03839ed1775951758e;p=openbsd Normalize the received prefixes. We need to use ldp_applymask() to normalize the received prefixes. Example: 10.1.1.0/16 -> 10.1.0.0/16. Additionally, stop using IANA's AF numbers in map->fec.prefix.af and use AF_INET/AF_INET6 instead. This makes the code much simpler, use AF_IPV[46] only when necessary (decoding/encoding prefixes). ok claudio@ --- diff --git a/usr.sbin/ldpd/labelmapping.c b/usr.sbin/ldpd/labelmapping.c index f8261e6d6d8..5ed9800f2d8 100644 --- a/usr.sbin/ldpd/labelmapping.c +++ b/usr.sbin/ldpd/labelmapping.c @@ -1,4 +1,4 @@ -/* $OpenBSD: labelmapping.c,v 1.58 2016/07/16 19:20:16 renato Exp $ */ +/* $OpenBSD: labelmapping.c,v 1.59 2016/08/08 16:45:51 renato Exp $ */ /* * Copyright (c) 2014, 2015 Renato Westphal @@ -356,7 +356,7 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type) switch (me->map.type) { case MAP_TYPE_PREFIX: switch (me->map.fec.prefix.af) { - case AF_IPV4: + case AF_INET: if (label == MPLS_LABEL_IPV6NULL) { session_shutdown(nbr, S_BAD_TLV_VAL, msg.id, msg.type); @@ -365,7 +365,7 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type) if (!nbr->v4_enabled) goto next; break; - case AF_IPV6: + case AF_INET6: if (label == MPLS_LABEL_IPV4NULL) { session_shutdown(nbr, S_BAD_TLV_VAL, msg.id, msg.type); @@ -547,9 +547,18 @@ gen_fec_tlv(struct ibuf *buf, struct map *map) ft.length = htons(sizeof(map->type) + sizeof(family) + sizeof(map->fec.prefix.prefixlen) + len); err |= ibuf_add(buf, &ft, sizeof(ft)); - err |= ibuf_add(buf, &map->type, sizeof(map->type)); - family = htons(map->fec.prefix.af); + switch (map->fec.prefix.af) { + case AF_INET: + family = htons(AF_IPV4); + break; + case AF_INET6: + family = htons(AF_IPV6); + break; + default: + fatalx("gen_fec_tlv: unknown af"); + break; + } err |= ibuf_add(buf, &family, sizeof(family)); err |= ibuf_add(buf, &map->fec.prefix.prefixlen, sizeof(map->fec.prefix.prefixlen)); @@ -628,10 +637,16 @@ tlv_decode_fec_elm(struct nbr *nbr, struct ldp_msg *msg, char *buf, /* Address Family */ memcpy(&map->fec.prefix.af, buf + off, sizeof(map->fec.prefix.af)); - map->fec.prefix.af = ntohs(map->fec.prefix.af); off += sizeof(map->fec.prefix.af); - if (map->fec.prefix.af != AF_IPV4 && - map->fec.prefix.af != AF_IPV6) { + map->fec.prefix.af = ntohs(map->fec.prefix.af); + switch (map->fec.prefix.af) { + case AF_IPV4: + map->fec.prefix.af = AF_INET; + break; + case AF_IPV6: + map->fec.prefix.af = AF_INET6; + break; + default: send_notification_nbr(nbr, S_UNSUP_ADDR, msg->id, msg->type); return (-1); @@ -652,6 +667,10 @@ tlv_decode_fec_elm(struct nbr *nbr, struct ldp_msg *msg, char *buf, memcpy(&map->fec.prefix.prefix, buf + off, PREFIX_SIZE(map->fec.prefix.prefixlen)); + /* Just in case... */ + ldp_applymask(map->fec.prefix.af, &map->fec.prefix.prefix, + &map->fec.prefix.prefix, map->fec.prefix.prefixlen); + return (off + PREFIX_SIZE(map->fec.prefix.prefixlen)); case MAP_TYPE_PWID: if (len < FEC_PWID_ELM_MIN_LEN) { diff --git a/usr.sbin/ldpd/lde.c b/usr.sbin/ldpd/lde.c index a21f11c4bd1..0d77543792f 100644 --- a/usr.sbin/ldpd/lde.c +++ b/usr.sbin/ldpd/lde.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lde.c,v 1.62 2016/07/18 21:10:37 benno Exp $ */ +/* $OpenBSD: lde.c,v 1.63 2016/08/08 16:45:51 renato Exp $ */ /* * Copyright (c) 2013, 2016 Renato Westphal @@ -695,13 +695,13 @@ lde_fec2map(struct fec *fec, struct map *map) switch (fec->type) { case FEC_TYPE_IPV4: map->type = MAP_TYPE_PREFIX; - map->fec.prefix.af = AF_IPV4; + map->fec.prefix.af = AF_INET; map->fec.prefix.prefix.v4 = fec->u.ipv4.prefix; map->fec.prefix.prefixlen = fec->u.ipv4.prefixlen; break; case FEC_TYPE_IPV6: map->type = MAP_TYPE_PREFIX; - map->fec.prefix.af = AF_IPV6; + map->fec.prefix.af = AF_INET6; map->fec.prefix.prefix.v6 = fec->u.ipv6.prefix; map->fec.prefix.prefixlen = fec->u.ipv6.prefixlen; break; @@ -723,12 +723,12 @@ lde_map2fec(struct map *map, struct in_addr lsr_id, struct fec *fec) switch (map->type) { case MAP_TYPE_PREFIX: switch (map->fec.prefix.af) { - case AF_IPV4: + case AF_INET: fec->type = FEC_TYPE_IPV4; fec->u.ipv4.prefix = map->fec.prefix.prefix.v4; fec->u.ipv4.prefixlen = map->fec.prefix.prefixlen; break; - case AF_IPV6: + case AF_INET6: fec->type = FEC_TYPE_IPV6; fec->u.ipv6.prefix = map->fec.prefix.prefix.v6; fec->u.ipv6.prefixlen = map->fec.prefix.prefixlen; diff --git a/usr.sbin/ldpd/log.c b/usr.sbin/ldpd/log.c index c9312c0581e..66fb8068bf9 100644 --- a/usr.sbin/ldpd/log.c +++ b/usr.sbin/ldpd/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.29 2016/07/15 17:09:25 renato Exp $ */ +/* $OpenBSD: log.c,v 1.30 2016/08/08 16:45:51 renato Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -301,7 +301,6 @@ const char * log_map(const struct map *map) { static char buf[64]; - int af; switch (map->type) { case MAP_TYPE_WILDCARD: @@ -309,19 +308,8 @@ log_map(const struct map *map) return ("???"); break; case MAP_TYPE_PREFIX: - switch (map->fec.prefix.af) { - case AF_IPV4: - af = AF_INET; - break; - case AF_IPV6: - af = AF_INET6; - break; - default: - return ("???"); - } - if (snprintf(buf, sizeof(buf), "%s/%u", - log_addr(af, &map->fec.prefix.prefix), + log_addr(map->fec.prefix.af, &map->fec.prefix.prefix), map->fec.prefix.prefixlen) == -1) return ("???"); break;