diff options
author | Alexander Bluhm <bluhm@cvs.openbsd.org> | 2012-10-21 21:30:45 +0000 |
---|---|---|
committer | Alexander Bluhm <bluhm@cvs.openbsd.org> | 2012-10-21 21:30:45 +0000 |
commit | 08d7be14c3ac91912896b04f4e019e2f49c86a79 (patch) | |
tree | 7173e829794991b92c011c5e7f04a4dbbcc89a8e /usr.sbin | |
parent | c365a158c76fa0c425b372788be56fd4fe9b07f5 (diff) |
Move common ospf6d functions for ipv6 address manipulation into
util.c to reuse them in ospf6ctl.
OK claudio@
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/ospf6ctl/Makefile | 4 | ||||
-rw-r--r-- | usr.sbin/ospf6ctl/parser.c | 86 | ||||
-rw-r--r-- | usr.sbin/ospf6d/Makefile | 4 | ||||
-rw-r--r-- | usr.sbin/ospf6d/kroute.c | 134 | ||||
-rw-r--r-- | usr.sbin/ospf6d/util.c | 157 |
5 files changed, 163 insertions, 222 deletions
diff --git a/usr.sbin/ospf6ctl/Makefile b/usr.sbin/ospf6ctl/Makefile index bcf7c48a2bb..44ef6ff5144 100644 --- a/usr.sbin/ospf6ctl/Makefile +++ b/usr.sbin/ospf6ctl/Makefile @@ -1,9 +1,9 @@ -# $OpenBSD: Makefile,v 1.2 2010/05/26 16:44:32 nicm Exp $ +# $OpenBSD: Makefile,v 1.3 2012/10/21 21:30:44 bluhm Exp $ .PATH: ${.CURDIR}/../ospf6d PROG= ospf6ctl -SRCS= log.c ospf6ctl.c parser.c +SRCS= log.c ospf6ctl.c parser.c util.c CFLAGS+= -Wall CFLAGS+= -Wstrict-prototypes -Wmissing-prototypes CFLAGS+= -Wshadow -Wpointer-arith -Wcast-qual diff --git a/usr.sbin/ospf6ctl/parser.c b/usr.sbin/ospf6ctl/parser.c index 67e13a2c19f..8c0ad271260 100644 --- a/usr.sbin/ospf6ctl/parser.c +++ b/usr.sbin/ospf6ctl/parser.c @@ -1,4 +1,4 @@ -/* $OpenBSD: parser.c,v 1.11 2011/03/07 07:43:02 henning Exp $ */ +/* $OpenBSD: parser.c,v 1.12 2012/10/21 21:30:44 bluhm Exp $ */ /* * Copyright (c) 2004 Esben Norby <norby@openbsd.org> @@ -342,87 +342,3 @@ parse_prefix(const char *word, struct in6_addr *addr, u_int8_t *prefixlen) *prefixlen = 128; return (parse_addr(word, addr)); } - -/* XXX prototype defined in ospfd.h and shared with the kroute.c version */ -u_int8_t -mask2prefixlen(struct sockaddr_in6 *sa_in6) -{ - u_int8_t l = 0, *ap, *ep; - - /* - * sin6_len is the size of the sockaddr so substract the offset of - * the possibly truncated sin6_addr struct. - */ - ap = (u_int8_t *)&sa_in6->sin6_addr; - ep = (u_int8_t *)sa_in6 + sa_in6->sin6_len; - for (; ap < ep; ap++) { - /* this "beauty" is adopted from sbin/route/show.c ... */ - switch (*ap) { - case 0xff: - l += 8; - break; - case 0xfe: - l += 7; - return (l); - case 0xfc: - l += 6; - return (l); - case 0xf8: - l += 5; - return (l); - case 0xf0: - l += 4; - return (l); - case 0xe0: - l += 3; - return (l); - case 0xc0: - l += 2; - return (l); - case 0x80: - l += 1; - return (l); - case 0x00: - return (l); - default: - errx(1, "non contiguous inet6 netmask"); - } - } - - return (l); -} - -/* XXX local copy from kroute.c, should go to shared file */ -struct in6_addr * -prefixlen2mask(u_int8_t prefixlen) -{ - static struct in6_addr mask; - int i; - - bzero(&mask, sizeof(mask)); - for (i = 0; i < prefixlen / 8; i++) - mask.s6_addr[i] = 0xff; - i = prefixlen % 8; - if (i) - mask.s6_addr[prefixlen / 8] = 0xff00 >> i; - - return (&mask); -} - -/* XXX local copy from kroute.c, should go to shared file */ -void -inet6applymask(struct in6_addr *dest, const struct in6_addr *src, int prefixlen) -{ - struct in6_addr mask; - int i; - - bzero(&mask, sizeof(mask)); - for (i = 0; i < prefixlen / 8; i++) - mask.s6_addr[i] = 0xff; - i = prefixlen % 8; - if (i) - mask.s6_addr[prefixlen / 8] = 0xff00 >> i; - - for (i = 0; i < 16; i++) - dest->s6_addr[i] = src->s6_addr[i] & mask.s6_addr[i]; -} diff --git a/usr.sbin/ospf6d/Makefile b/usr.sbin/ospf6d/Makefile index 611f78d9140..d54fc1ea158 100644 --- a/usr.sbin/ospf6d/Makefile +++ b/usr.sbin/ospf6d/Makefile @@ -1,10 +1,10 @@ -# $OpenBSD: Makefile,v 1.3 2010/05/26 16:44:32 nicm Exp $ +# $OpenBSD: Makefile,v 1.4 2012/10/21 21:30:44 bluhm Exp $ PROG= ospf6d SRCS= area.c carp.c control.c database.c hello.c \ interface.c iso_cksum.c kroute.c lsack.c \ lsreq.c lsupdate.c log.c neighbor.c ospf6d.c ospfe.c packet.c \ - parse.y printconf.c rde.c rde_lsdb.c rde_spf.c name2id.c + parse.y printconf.c rde.c rde_lsdb.c rde_spf.c util.c name2id.c MAN= ospf6d.8 ospf6d.conf.5 diff --git a/usr.sbin/ospf6d/kroute.c b/usr.sbin/ospf6d/kroute.c index 2a2a9703b3b..b8d812ecf59 100644 --- a/usr.sbin/ospf6d/kroute.c +++ b/usr.sbin/ospf6d/kroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kroute.c,v 1.39 2012/09/20 17:39:08 bluhm Exp $ */ +/* $OpenBSD: kroute.c,v 1.40 2012/10/21 21:30:44 bluhm Exp $ */ /* * Copyright (c) 2004 Esben Norby <norby@openbsd.org> @@ -693,138 +693,6 @@ protect_lo(void) return (0); } -#define IN6_IS_SCOPE_EMBED(a) \ - ((IN6_IS_ADDR_LINKLOCAL(a)) || \ - (IN6_IS_ADDR_MC_LINKLOCAL(a)) || \ - (IN6_IS_ADDR_MC_INTFACELOCAL(a))) - -void -embedscope(struct sockaddr_in6 *sin6) -{ - u_int16_t tmp16; - - if (IN6_IS_SCOPE_EMBED(&sin6->sin6_addr)) { - bcopy(&sin6->sin6_addr.s6_addr[2], &tmp16, sizeof(tmp16)); - if (tmp16 != 0) { - log_warnx("embedscope: address %s already has embeded " - "scope %u", log_sockaddr(sin6), ntohs(tmp16)); - } - tmp16 = htons(sin6->sin6_scope_id); - bcopy(&tmp16, &sin6->sin6_addr.s6_addr[2], sizeof(tmp16)); - sin6->sin6_scope_id = 0; - } -} - -void -recoverscope(struct sockaddr_in6 *sin6) -{ - u_int16_t tmp16; - - if (sin6->sin6_scope_id != 0) { - log_warnx("recoverscope: address %s already has scope id %u", - log_sockaddr(sin6), sin6->sin6_scope_id); - } - - if (IN6_IS_SCOPE_EMBED(&sin6->sin6_addr)) { - bcopy(&sin6->sin6_addr.s6_addr[2], &tmp16, sizeof(tmp16)); - sin6->sin6_scope_id = ntohs(tmp16); - sin6->sin6_addr.s6_addr[2] = 0; - sin6->sin6_addr.s6_addr[3] = 0; - } -} - -void -clearscope(struct in6_addr *in6) -{ - if (IN6_IS_SCOPE_EMBED(in6)) { - in6->s6_addr[2] = 0; - in6->s6_addr[3] = 0; - } -} - -#undef IN6_IS_SCOPE_EMBED - -u_int8_t -mask2prefixlen(struct sockaddr_in6 *sa_in6) -{ - u_int8_t l = 0, *ap, *ep; - - /* - * sin6_len is the size of the sockaddr so substract the offset of - * the possibly truncated sin6_addr struct. - */ - ap = (u_int8_t *)&sa_in6->sin6_addr; - ep = (u_int8_t *)sa_in6 + sa_in6->sin6_len; - for (; ap < ep; ap++) { - /* this "beauty" is adopted from sbin/route/show.c ... */ - switch (*ap) { - case 0xff: - l += 8; - break; - case 0xfe: - l += 7; - return (l); - case 0xfc: - l += 6; - return (l); - case 0xf8: - l += 5; - return (l); - case 0xf0: - l += 4; - return (l); - case 0xe0: - l += 3; - return (l); - case 0xc0: - l += 2; - return (l); - case 0x80: - l += 1; - return (l); - case 0x00: - return (l); - default: - fatalx("non contiguous inet6 netmask"); - } - } - - return (l); -} - -struct in6_addr * -prefixlen2mask(u_int8_t prefixlen) -{ - static struct in6_addr mask; - int i; - - bzero(&mask, sizeof(mask)); - for (i = 0; i < prefixlen / 8; i++) - mask.s6_addr[i] = 0xff; - i = prefixlen % 8; - if (i) - mask.s6_addr[prefixlen / 8] = 0xff00 >> i; - - return (&mask); -} - -void -inet6applymask(struct in6_addr *dest, const struct in6_addr *src, int prefixlen) -{ - struct in6_addr mask; - int i; - - bzero(&mask, sizeof(mask)); - for (i = 0; i < prefixlen / 8; i++) - mask.s6_addr[i] = 0xff; - i = prefixlen % 8; - if (i) - mask.s6_addr[prefixlen / 8] = 0xff00 >> i; - - for (i = 0; i < 16; i++) - dest->s6_addr[i] = src->s6_addr[i] & mask.s6_addr[i]; -} - #define ROUNDUP(a) \ ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) diff --git a/usr.sbin/ospf6d/util.c b/usr.sbin/ospf6d/util.c new file mode 100644 index 00000000000..cfc0a5f980e --- /dev/null +++ b/usr.sbin/ospf6d/util.c @@ -0,0 +1,157 @@ +/* $OpenBSD: util.c,v 1.1 2012/10/21 21:30:44 bluhm Exp $ */ + +/* + * Copyright (c) 2012 Alexander Bluhm <bluhm@openbsd.org> + * Copyright (c) 2004 Esben Norby <norby@openbsd.org> + * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <netinet/in.h> +#include <string.h> + +#include "ospf6d.h" +#include "log.h" + +#define IN6_IS_SCOPE_EMBED(a) \ + ((IN6_IS_ADDR_LINKLOCAL(a)) || \ + (IN6_IS_ADDR_MC_LINKLOCAL(a)) || \ + (IN6_IS_ADDR_MC_INTFACELOCAL(a))) + +void +embedscope(struct sockaddr_in6 *sin6) +{ + u_int16_t tmp16; + + if (IN6_IS_SCOPE_EMBED(&sin6->sin6_addr)) { + bcopy(&sin6->sin6_addr.s6_addr[2], &tmp16, sizeof(tmp16)); + if (tmp16 != 0) { + log_warnx("embedscope: address %s already has embeded " + "scope %u", log_sockaddr(sin6), ntohs(tmp16)); + } + tmp16 = htons(sin6->sin6_scope_id); + bcopy(&tmp16, &sin6->sin6_addr.s6_addr[2], sizeof(tmp16)); + sin6->sin6_scope_id = 0; + } +} + +void +recoverscope(struct sockaddr_in6 *sin6) +{ + u_int16_t tmp16; + + if (sin6->sin6_scope_id != 0) { + log_warnx("recoverscope: address %s already has scope id %u", + log_sockaddr(sin6), sin6->sin6_scope_id); + } + + if (IN6_IS_SCOPE_EMBED(&sin6->sin6_addr)) { + bcopy(&sin6->sin6_addr.s6_addr[2], &tmp16, sizeof(tmp16)); + sin6->sin6_scope_id = ntohs(tmp16); + sin6->sin6_addr.s6_addr[2] = 0; + sin6->sin6_addr.s6_addr[3] = 0; + } +} + +void +clearscope(struct in6_addr *in6) +{ + if (IN6_IS_SCOPE_EMBED(in6)) { + in6->s6_addr[2] = 0; + in6->s6_addr[3] = 0; + } +} + +#undef IN6_IS_SCOPE_EMBED + +u_int8_t +mask2prefixlen(struct sockaddr_in6 *sa_in6) +{ + u_int8_t l = 0, *ap, *ep; + + /* + * sin6_len is the size of the sockaddr so substract the offset of + * the possibly truncated sin6_addr struct. + */ + ap = (u_int8_t *)&sa_in6->sin6_addr; + ep = (u_int8_t *)sa_in6 + sa_in6->sin6_len; + for (; ap < ep; ap++) { + /* this "beauty" is adopted from sbin/route/show.c ... */ + switch (*ap) { + case 0xff: + l += 8; + break; + case 0xfe: + l += 7; + return (l); + case 0xfc: + l += 6; + return (l); + case 0xf8: + l += 5; + return (l); + case 0xf0: + l += 4; + return (l); + case 0xe0: + l += 3; + return (l); + case 0xc0: + l += 2; + return (l); + case 0x80: + l += 1; + return (l); + case 0x00: + return (l); + default: + fatalx("non contiguous inet6 netmask"); + } + } + + return (l); +} + +struct in6_addr * +prefixlen2mask(u_int8_t prefixlen) +{ + static struct in6_addr mask; + int i; + + bzero(&mask, sizeof(mask)); + for (i = 0; i < prefixlen / 8; i++) + mask.s6_addr[i] = 0xff; + i = prefixlen % 8; + if (i) + mask.s6_addr[prefixlen / 8] = 0xff00 >> i; + + return (&mask); +} + +void +inet6applymask(struct in6_addr *dest, const struct in6_addr *src, int prefixlen) +{ + struct in6_addr mask; + int i; + + bzero(&mask, sizeof(mask)); + for (i = 0; i < prefixlen / 8; i++) + mask.s6_addr[i] = 0xff; + i = prefixlen % 8; + if (i) + mask.s6_addr[prefixlen / 8] = 0xff00 >> i; + + for (i = 0; i < 16; i++) + dest->s6_addr[i] = src->s6_addr[i] & mask.s6_addr[i]; +} |