diff options
-rw-r--r-- | usr.sbin/bgpd/Makefile | 4 | ||||
-rw-r--r-- | usr.sbin/bgpd/bgpd.h | 11 | ||||
-rw-r--r-- | usr.sbin/bgpd/log.c | 48 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde.h | 8 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde_attr.c | 148 | ||||
-rw-r--r-- | usr.sbin/bgpd/session.h | 3 | ||||
-rw-r--r-- | usr.sbin/bgpd/util.c | 221 |
7 files changed, 238 insertions, 205 deletions
diff --git a/usr.sbin/bgpd/Makefile b/usr.sbin/bgpd/Makefile index e29cea78830..c74c6e999be 100644 --- a/usr.sbin/bgpd/Makefile +++ b/usr.sbin/bgpd/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.23 2005/06/13 21:16:18 henning Exp $ +# $OpenBSD: Makefile,v 1.24 2006/01/03 22:19:59 claudio Exp $ .PATH: ${.CURDIR}/.. @@ -6,7 +6,7 @@ PROG= bgpd SRCS= bgpd.c buffer.c session.c log.c parse.y config.c imsg.c \ rde.c rde_rib.c rde_decide.c rde_prefix.c mrt.c kroute.c \ control.c pfkey.c rde_update.c rde_attr.c printconf.c \ - rde_filter.c pftable.c name2id.c + rde_filter.c pftable.c name2id.c util.c CFLAGS+= -Wall -I${.CURDIR} CFLAGS+= -Wstrict-prototypes -Wmissing-prototypes CFLAGS+= -Wmissing-declarations diff --git a/usr.sbin/bgpd/bgpd.h b/usr.sbin/bgpd/bgpd.h index e8cd1e3e5a1..931bec7d860 100644 --- a/usr.sbin/bgpd/bgpd.h +++ b/usr.sbin/bgpd/bgpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bgpd.h,v 1.181 2005/11/02 15:34:43 claudio Exp $ */ +/* $OpenBSD: bgpd.h,v 1.182 2006/01/03 22:19:59 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -672,8 +672,6 @@ void log_debug(const char *, ...); void fatal(const char *); void fatalx(const char *); void fatal_ensure(const char *, int, const char *); -const char *log_addr(const struct bgpd_addr *); -const char *log_in6addr(const struct in6_addr *); /* parse.y */ int cmdline_symset(char *); @@ -743,5 +741,12 @@ void pftable_ref(u_int16_t); void filterset_free(struct filter_set_head *); int filterset_cmp(struct filter_set *, struct filter_set *); +/* util.c */ +const char *log_addr(const struct bgpd_addr *); +const char *log_in6addr(const struct in6_addr *); +const char * log_sockaddr(struct sockaddr *); +int aspath_snprint(char *, size_t, void *, u_int16_t); +int aspath_asprint(char **, void *, u_int16_t); +size_t aspath_strlen(void *, u_int16_t); #endif /* __BGPD_H__ */ diff --git a/usr.sbin/bgpd/log.c b/usr.sbin/bgpd/log.c index 3769e5ba633..1cea36f0598 100644 --- a/usr.sbin/bgpd/log.c +++ b/usr.sbin/bgpd/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.46 2005/06/05 23:54:05 henning Exp $ */ +/* $OpenBSD: log.c,v 1.47 2006/01/03 22:19:59 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -331,49 +331,3 @@ log_conn_attempt(const struct peer *peer, struct sockaddr *sa) free(p); } } - -const char * -log_addr(const struct bgpd_addr *addr) -{ - static char buf[48]; - - if (inet_ntop(addr->af, &addr->ba, buf, sizeof(buf)) == NULL) - return ("?"); - else - return (buf); -} - -const char * -log_in6addr(const struct in6_addr *addr) -{ - struct sockaddr_in6 sa_in6; - u_int16_t tmp16; - - bzero(&sa_in6, sizeof(sa_in6)); - sa_in6.sin6_len = sizeof(sa_in6); - sa_in6.sin6_family = AF_INET6; - memcpy(&sa_in6.sin6_addr, addr, sizeof(sa_in6.sin6_addr)); - - /* XXX thanks, KAME, for this ugliness... adopted from route/show.c */ - if (IN6_IS_ADDR_LINKLOCAL(&sa_in6.sin6_addr) || - IN6_IS_ADDR_MC_LINKLOCAL(&sa_in6.sin6_addr)) { - memcpy(&tmp16, &sa_in6.sin6_addr.s6_addr[2], sizeof(tmp16)); - sa_in6.sin6_scope_id = ntohs(tmp16); - sa_in6.sin6_addr.s6_addr[2] = 0; - sa_in6.sin6_addr.s6_addr[3] = 0; - } - - return (log_sockaddr((struct sockaddr *)&sa_in6)); -} - -const char * -log_sockaddr(struct sockaddr *sa) -{ - static char buf[NI_MAXHOST]; - - if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, - NI_NUMERICHOST)) - return ("(unknown)"); - else - return (buf); -} diff --git a/usr.sbin/bgpd/rde.h b/usr.sbin/bgpd/rde.h index e33842a4b67..2dd90e7e40f 100644 --- a/usr.sbin/bgpd/rde.h +++ b/usr.sbin/bgpd/rde.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rde.h,v 1.77 2005/12/30 17:20:21 claudio Exp $ */ +/* $OpenBSD: rde.h,v 1.78 2006/01/03 22:19:59 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org> and @@ -273,9 +273,6 @@ u_int16_t aspath_neighbor(struct aspath *); int aspath_loopfree(struct aspath *, u_int16_t); int aspath_compare(struct aspath *, struct aspath *); struct aspath *aspath_prepend(struct aspath *, u_int16_t, int); -int aspath_snprint(char *, size_t, void *, u_int16_t); -int aspath_asprint(char **, void *, u_int16_t); -size_t aspath_strlen(void *, u_int16_t); int aspath_match(struct aspath *, enum as_spec, u_int16_t); int community_match(void *, u_int16_t, int, int); int community_set(struct attr *, int, int); @@ -359,4 +356,7 @@ int rde_filter_community(struct rde_aspath *, int, int); int rde_filter_equal(struct filter_head *, struct filter_head *, enum directions); +/* util.c */ +u_int16_t aspath_extract(const void *, int); + #endif /* __RDE_H__ */ diff --git a/usr.sbin/bgpd/rde_attr.c b/usr.sbin/bgpd/rde_attr.c index a697aea7457..f7cd6a86638 100644 --- a/usr.sbin/bgpd/rde_attr.c +++ b/usr.sbin/bgpd/rde_attr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_attr.c,v 1.52 2005/12/30 16:40:15 claudio Exp $ */ +/* $OpenBSD: rde_attr.c,v 1.53 2006/01/03 22:19:59 claudio Exp $ */ /* * Copyright (c) 2004 Claudio Jeker <claudio@openbsd.org> @@ -142,7 +142,6 @@ attr_optfree(struct rde_aspath *asp) /* aspath specific functions */ -u_int16_t aspath_extract(const void *, int); struct aspath *aspath_lookup(const void *, u_int16_t); struct aspath_table { @@ -351,24 +350,6 @@ aspath_compare(struct aspath *a1, struct aspath *a2) return (0); } -/* - * Extract the asnum out of the as segment at the specified position. - * Direct access is not possible because of non-aligned reads. - * ATTENTION: no bounds check are done. - */ -u_int16_t -aspath_extract(const void *seg, int pos) -{ - const u_char *ptr = seg; - u_int16_t as = 0; - - ptr += 2 + 2 * pos; - as = *ptr++; - as <<= 8; - as |= *ptr; - return (as); -} - struct aspath * aspath_lookup(const void *data, u_int16_t len) { @@ -457,133 +438,6 @@ aspath_prepend(struct aspath *asp, u_int16_t as, int quantum) return (asp); } -int -aspath_snprint(char *buf, size_t size, void *data, u_int16_t len) -{ -#define UPDATE() \ - do { \ - if (r == -1) \ - return (-1); \ - total_size += r; \ - if ((unsigned int)r < size) { \ - size -= r; \ - buf += r; \ - } else { \ - buf += size; \ - size = 0; \ - } \ - } while (0) - u_int8_t *seg; - int r, total_size; - u_int16_t seg_size; - u_int8_t i, seg_type, seg_len; - - total_size = 0; - seg = data; - for (; len > 0; len -= seg_size, seg += seg_size) { - seg_type = seg[0]; - seg_len = seg[1]; - seg_size = 2 + 2 * seg_len; - - if (seg_type == AS_SET) { - if (total_size != 0) - r = snprintf(buf, size, " { "); - else - r = snprintf(buf, size, "{ "); - UPDATE(); - } else if (total_size != 0) { - r = snprintf(buf, size, " "); - UPDATE(); - } - - for (i = 0; i < seg_len; i++) { - r = snprintf(buf, size, "%hu", aspath_extract(seg, i)); - UPDATE(); - if (i + 1 < seg_len) { - r = snprintf(buf, size, " "); - UPDATE(); - } - } - if (seg_type == AS_SET) { - r = snprintf(buf, size, " }"); - UPDATE(); - } - } - /* ensure that we have a valid C-string especially for emtpy as path */ - if (size > 0) - *buf = '\0'; - - return (total_size); -#undef UPDATE -} - -int -aspath_asprint(char **ret, void *data, u_int16_t len) -{ - size_t slen; - int plen; - - slen = aspath_strlen(data, len) + 1; - *ret = malloc(slen); - if (*ret == NULL) - return (-1); - - plen = aspath_snprint(*ret, slen, data, len); - if (plen == -1) { - free(*ret); - *ret = NULL; - return (-1); - } - - return (0); -} - -size_t -aspath_strlen(void *data, u_int16_t len) -{ - u_int8_t *seg; - int total_size; - u_int16_t as, seg_size; - u_int8_t i, seg_type, seg_len; - - total_size = 0; - seg = data; - for (; len > 0; len -= seg_size, seg += seg_size) { - seg_type = seg[0]; - seg_len = seg[1]; - seg_size = 2 + 2 * seg_len; - - if (seg_type == AS_SET) - if (total_size != 0) - total_size += 3; - else - total_size += 2; - else if (total_size != 0) - total_size += 1; - - for (i = 0; i < seg_len; i++) { - as = aspath_extract(seg, i); - if (as >= 10000) - total_size += 5; - else if (as >= 1000) - total_size += 4; - else if (as >= 100) - total_size += 3; - else if (as >= 10) - total_size += 2; - else - total_size += 1; - - if (i + 1 < seg_len) - total_size += 1; - } - - if (seg_type == AS_SET) - total_size += 2; - } - return (total_size); -} - /* we need to be able to search more than one as */ int aspath_match(struct aspath *a, enum as_spec type, u_int16_t as) diff --git a/usr.sbin/bgpd/session.h b/usr.sbin/bgpd/session.h index 8b3b1aff8cf..a310266573a 100644 --- a/usr.sbin/bgpd/session.h +++ b/usr.sbin/bgpd/session.h @@ -1,4 +1,4 @@ -/* $OpenBSD: session.h,v 1.79 2005/10/19 10:26:21 henning Exp $ */ +/* $OpenBSD: session.h,v 1.80 2006/01/03 22:19:59 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -199,7 +199,6 @@ void log_statechange(struct peer *, enum session_state, void log_notification(const struct peer *, u_int8_t, u_int8_t, u_char *, u_int16_t); void log_conn_attempt(const struct peer *, struct sockaddr *); -const char * log_sockaddr(struct sockaddr *); /* parse.y */ int parse_config(char *, struct bgpd_config *, struct mrt_head *, diff --git a/usr.sbin/bgpd/util.c b/usr.sbin/bgpd/util.c new file mode 100644 index 00000000000..d301dce878d --- /dev/null +++ b/usr.sbin/bgpd/util.c @@ -0,0 +1,221 @@ +/* $OpenBSD: util.c,v 1.1 2006/01/03 22:19:59 claudio Exp $ */ + +/* + * Copyright (c) 2006 Claudio Jeker <claudio@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 <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "bgpd.h" +#include "rde.h" + +const char * +log_addr(const struct bgpd_addr *addr) +{ + static char buf[48]; + + if (inet_ntop(addr->af, &addr->ba, buf, sizeof(buf)) == NULL) + return ("?"); + else + return (buf); +} + +const char * +log_in6addr(const struct in6_addr *addr) +{ + struct sockaddr_in6 sa_in6; + u_int16_t tmp16; + + bzero(&sa_in6, sizeof(sa_in6)); + sa_in6.sin6_len = sizeof(sa_in6); + sa_in6.sin6_family = AF_INET6; + memcpy(&sa_in6.sin6_addr, addr, sizeof(sa_in6.sin6_addr)); + + /* XXX thanks, KAME, for this ugliness... adopted from route/show.c */ + if (IN6_IS_ADDR_LINKLOCAL(&sa_in6.sin6_addr) || + IN6_IS_ADDR_MC_LINKLOCAL(&sa_in6.sin6_addr)) { + memcpy(&tmp16, &sa_in6.sin6_addr.s6_addr[2], sizeof(tmp16)); + sa_in6.sin6_scope_id = ntohs(tmp16); + sa_in6.sin6_addr.s6_addr[2] = 0; + sa_in6.sin6_addr.s6_addr[3] = 0; + } + + return (log_sockaddr((struct sockaddr *)&sa_in6)); +} + +const char * +log_sockaddr(struct sockaddr *sa) +{ + static char buf[NI_MAXHOST]; + + if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, + NI_NUMERICHOST)) + return ("(unknown)"); + else + return (buf); +} + +int +aspath_snprint(char *buf, size_t size, void *data, u_int16_t len) +{ +#define UPDATE() \ + do { \ + if (r == -1) \ + return (-1); \ + total_size += r; \ + if ((unsigned int)r < size) { \ + size -= r; \ + buf += r; \ + } else { \ + buf += size; \ + size = 0; \ + } \ + } while (0) + u_int8_t *seg; + int r, total_size; + u_int16_t seg_size; + u_int8_t i, seg_type, seg_len; + + total_size = 0; + seg = data; + for (; len > 0; len -= seg_size, seg += seg_size) { + seg_type = seg[0]; + seg_len = seg[1]; + seg_size = 2 + 2 * seg_len; + + if (seg_type == AS_SET) { + if (total_size != 0) + r = snprintf(buf, size, " { "); + else + r = snprintf(buf, size, "{ "); + UPDATE(); + } else if (total_size != 0) { + r = snprintf(buf, size, " "); + UPDATE(); + } + + for (i = 0; i < seg_len; i++) { + r = snprintf(buf, size, "%hu", aspath_extract(seg, i)); + UPDATE(); + if (i + 1 < seg_len) { + r = snprintf(buf, size, " "); + UPDATE(); + } + } + if (seg_type == AS_SET) { + r = snprintf(buf, size, " }"); + UPDATE(); + } + } + /* ensure that we have a valid C-string especially for emtpy as path */ + if (size > 0) + *buf = '\0'; + + return (total_size); +#undef UPDATE +} + +int +aspath_asprint(char **ret, void *data, u_int16_t len) +{ + size_t slen; + int plen; + + slen = aspath_strlen(data, len) + 1; + *ret = malloc(slen); + if (*ret == NULL) + return (-1); + + plen = aspath_snprint(*ret, slen, data, len); + if (plen == -1) { + free(*ret); + *ret = NULL; + return (-1); + } + + return (0); +} + +size_t +aspath_strlen(void *data, u_int16_t len) +{ + u_int8_t *seg; + int total_size; + u_int16_t as, seg_size; + u_int8_t i, seg_type, seg_len; + + total_size = 0; + seg = data; + for (; len > 0; len -= seg_size, seg += seg_size) { + seg_type = seg[0]; + seg_len = seg[1]; + seg_size = 2 + 2 * seg_len; + + if (seg_type == AS_SET) + if (total_size != 0) + total_size += 3; + else + total_size += 2; + else if (total_size != 0) + total_size += 1; + + for (i = 0; i < seg_len; i++) { + as = aspath_extract(seg, i); + if (as >= 10000) + total_size += 5; + else if (as >= 1000) + total_size += 4; + else if (as >= 100) + total_size += 3; + else if (as >= 10) + total_size += 2; + else + total_size += 1; + + if (i + 1 < seg_len) + total_size += 1; + } + + if (seg_type == AS_SET) + total_size += 2; + } + return (total_size); +} + +/* + * Extract the asnum out of the as segment at the specified position. + * Direct access is not possible because of non-aligned reads. + * ATTENTION: no bounds check are done. + */ +u_int16_t +aspath_extract(const void *seg, int pos) +{ + const u_char *ptr = seg; + u_int16_t as = 0; + + ptr += 2 + 2 * pos; + as = *ptr++; + as <<= 8; + as |= *ptr; + return (as); +} + |