diff options
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/bgpd/bgpd.c | 10 | ||||
-rw-r--r-- | usr.sbin/bgpd/bgpd.h | 71 | ||||
-rw-r--r-- | usr.sbin/bgpd/config.c | 12 | ||||
-rw-r--r-- | usr.sbin/bgpd/control.c | 8 | ||||
-rw-r--r-- | usr.sbin/bgpd/kroute.c | 87 | ||||
-rw-r--r-- | usr.sbin/bgpd/log.c | 7 | ||||
-rw-r--r-- | usr.sbin/bgpd/mrt.c | 28 | ||||
-rw-r--r-- | usr.sbin/bgpd/parse.y | 81 | ||||
-rw-r--r-- | usr.sbin/bgpd/pfkey.c | 46 | ||||
-rw-r--r-- | usr.sbin/bgpd/pftable.c | 4 | ||||
-rw-r--r-- | usr.sbin/bgpd/printconf.c | 16 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde.c | 68 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde.h | 12 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde_filter.c | 16 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde_prefix.c | 117 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde_rib.c | 53 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde_update.c | 48 | ||||
-rw-r--r-- | usr.sbin/bgpd/session.c | 218 | ||||
-rw-r--r-- | usr.sbin/bgpd/util.c | 108 |
19 files changed, 491 insertions, 519 deletions
diff --git a/usr.sbin/bgpd/bgpd.c b/usr.sbin/bgpd/bgpd.c index b92085737c9..f9ba8a42d91 100644 --- a/usr.sbin/bgpd/bgpd.c +++ b/usr.sbin/bgpd/bgpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bgpd.c,v 1.150 2009/11/02 20:38:15 claudio Exp $ */ +/* $OpenBSD: bgpd.c,v 1.151 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -717,7 +717,7 @@ send_nexthop_update(struct kroute_nexthop *msg) { char *gw = NULL; - if (msg->gateway.af) + if (msg->gateway.aid) if (asprintf(&gw, ": via %s", log_addr(&msg->gateway)) == -1) { log_warn("send_nexthop_update"); @@ -727,7 +727,7 @@ send_nexthop_update(struct kroute_nexthop *msg) log_info("nexthop %s now %s%s%s", log_addr(&msg->nexthop), msg->valid ? "valid" : "invalid", msg->connected ? ": directly connected" : "", - msg->gateway.af ? gw : ""); + msg->gateway.aid ? gw : ""); free(gw); @@ -768,12 +768,12 @@ bgpd_redistribute(int type, struct kroute *kr, struct kroute6 *kr6) fatalx("bgpd_redistribute: unable to redistribute v4 and v6" "together"); if (kr != NULL) { - net.prefix.af = AF_INET; + net.prefix.aid = AID_INET; net.prefix.v4.s_addr = kr->prefix.s_addr; net.prefixlen = kr->prefixlen; } if (kr6 != NULL) { - net.prefix.af = AF_INET6; + net.prefix.aid = AID_INET6; memcpy(&net.prefix.v6, &kr6->prefix, sizeof(struct in6_addr)); net.prefixlen = kr6->prefixlen; } diff --git a/usr.sbin/bgpd/bgpd.h b/usr.sbin/bgpd/bgpd.h index b26da83a39a..9d8d16e330d 100644 --- a/usr.sbin/bgpd/bgpd.h +++ b/usr.sbin/bgpd/bgpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bgpd.h,v 1.247 2009/11/26 13:40:43 henning Exp $ */ +/* $OpenBSD: bgpd.h,v 1.248 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -108,7 +108,7 @@ enum reconf_action { }; struct bgpd_addr { - sa_family_t af; + u_int8_t aid; union { struct in_addr v4; struct in6_addr v6; @@ -598,7 +598,7 @@ struct filter_prefix { struct filter_prefixlen { enum comp_ops op; - sa_family_t af; + u_int8_t aid; u_int8_t len_min; u_int8_t len_max; }; @@ -667,22 +667,6 @@ struct rrefresh { u_int8_t safi; }; -struct rde_memstats { - int64_t path_cnt; - int64_t prefix_cnt; - int64_t rib_cnt; - int64_t pt4_cnt; - int64_t pt6_cnt; - int64_t nexthop_cnt; - int64_t aspath_cnt; - int64_t aspath_size; - int64_t aspath_refs; - int64_t attr_cnt; - int64_t attr_refs; - int64_t attr_data; - int64_t attr_dcnt; -}; - struct rde_rib { SIMPLEQ_ENTRY(rde_rib) entry; char name[PEER_DESCR_LEN]; @@ -693,6 +677,7 @@ SIMPLEQ_HEAD(rib_names, rde_rib); extern struct rib_names ribnames; /* Address Family Numbers as per RFC 1700 */ +#define AFI_UNSPEC 0 #define AFI_IPv4 1 #define AFI_IPv6 2 #define AFI_ALL 0xffff @@ -704,9 +689,51 @@ extern struct rib_names ribnames; #define SAFI_MPLS 0x04 #define SAFI_ALL 0xff +struct aid { + u_int16_t afi; + sa_family_t af; + u_int8_t safi; + char *name; +}; + +extern const struct aid aid_vals[]; + +#define AID_UNSPEC 0 +#define AID_INET 1 +#define AID_INET6 2 +#define AID_MAX 3 + +#define AID_VALS { \ + /* afi, af, safii, name */ \ + { AFI_UNSPEC, AF_UNSPEC, SAFI_NONE, "unspec"}, \ + { AFI_IPv4, AF_INET, SAFI_UNICAST, "IPv4" }, \ + { AFI_IPv6, AF_INET6, SAFI_UNICAST, "IPv6" } \ +} + +#define AID_PTSIZE { \ + 0, \ + sizeof(struct pt_entry4), \ + sizeof(struct pt_entry6) \ +} + /* 4-byte magic AS number */ #define AS_TRANS 23456 +struct rde_memstats { + int64_t path_cnt; + int64_t prefix_cnt; + int64_t rib_cnt; + int64_t pt_cnt[AID_MAX]; + int64_t nexthop_cnt; + int64_t aspath_cnt; + int64_t aspath_size; + int64_t aspath_refs; + int64_t attr_cnt; + int64_t attr_refs; + int64_t attr_data; + int64_t attr_dcnt; +}; + /* prototypes */ /* bgpd.c */ void send_nexthop_update(struct kroute_nexthop *); @@ -793,5 +820,11 @@ size_t aspath_strlen(void *, u_int16_t); in_addr_t prefixlen2mask(u_int8_t); void inet6applymask(struct in6_addr *, const struct in6_addr *, int); +int aid2afi(u_int8_t, u_int16_t *, u_int8_t *); +int afi2aid(u_int16_t, u_int8_t, u_int8_t *); +sa_family_t aid2af(u_int8_t); +int af2aid(sa_family_t, u_int8_t, u_int8_t *); +struct sockaddr *addr2sa(struct bgpd_addr *, u_int16_t); +void sa2addr(struct sockaddr *, struct bgpd_addr *); #endif /* __BGPD_H__ */ diff --git a/usr.sbin/bgpd/config.c b/usr.sbin/bgpd/config.c index e7c7d1f4383..5ac76924b78 100644 --- a/usr.sbin/bgpd/config.c +++ b/usr.sbin/bgpd/config.c @@ -1,4 +1,4 @@ -/* $OpenBSD: config.c,v 1.51 2009/01/26 23:10:02 claudio Exp $ */ +/* $OpenBSD: config.c,v 1.52 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2003, 2004, 2005 Henning Brauer <henning@openbsd.org> @@ -208,7 +208,7 @@ host_v4(const char *s, struct bgpd_addr *h, u_int8_t *len) return (0); } - h->af = AF_INET; + h->aid = AID_INET; h->v4.s_addr = ina.s_addr; *len = bits; @@ -225,13 +225,7 @@ host_v6(const char *s, struct bgpd_addr *h) hints.ai_socktype = SOCK_DGRAM; /*dummy*/ hints.ai_flags = AI_NUMERICHOST; if (getaddrinfo(s, "0", &hints, &res) == 0) { - h->af = AF_INET6; - memcpy(&h->v6, - &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, - sizeof(h->v6)); - h->scope_id = - ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; - + sa2addr(res->ai_addr, h); freeaddrinfo(res); return (1); } diff --git a/usr.sbin/bgpd/control.c b/usr.sbin/bgpd/control.c index ef1378f7788..84d29d95765 100644 --- a/usr.sbin/bgpd/control.c +++ b/usr.sbin/bgpd/control.c @@ -1,4 +1,4 @@ -/* $OpenBSD: control.c,v 1.63 2009/11/02 20:38:15 claudio Exp $ */ +/* $OpenBSD: control.c,v 1.64 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -371,7 +371,7 @@ control_dispatch_msg(struct pollfd *pfd, u_int *ctl_cnt) neighbor->descr[PEER_DESCR_LEN - 1] = 0; ribreq->peerid = 0; p = NULL; - if (neighbor->addr.af) { + if (neighbor->addr.aid) { p = getpeerbyaddr(&neighbor->addr); if (p == NULL) { control_result(c, @@ -398,8 +398,8 @@ control_dispatch_msg(struct pollfd *pfd, u_int *ctl_cnt) break; } if ((imsg.hdr.type == IMSG_CTL_SHOW_RIB_PREFIX) - && (ribreq->prefix.af != AF_INET) - && (ribreq->prefix.af != AF_INET6)) { + && (ribreq->prefix.aid != AID_INET) + && (ribreq->prefix.aid != AID_INET6)) { /* malformed request, must specify af */ control_result(c, CTL_RES_PARSE_ERROR); break; diff --git a/usr.sbin/bgpd/kroute.c b/usr.sbin/bgpd/kroute.c index e90e2775ed0..c7725a18c83 100644 --- a/usr.sbin/bgpd/kroute.c +++ b/usr.sbin/bgpd/kroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kroute.c,v 1.172 2009/07/23 14:53:20 claudio Exp $ */ +/* $OpenBSD: kroute.c,v 1.173 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -463,21 +463,21 @@ kr_nexthop_add(struct bgpd_addr *addr) bzero(&nh, sizeof(nh)); memcpy(&nh.nexthop, addr, sizeof(nh.nexthop)); nh.valid = 1; - if (h->kroute != NULL && addr->af == AF_INET) { + if (h->kroute != NULL && addr->aid == AID_INET) { k = h->kroute; nh.connected = k->r.flags & F_CONNECTED; if (k->r.nexthop.s_addr != 0) { - nh.gateway.af = AF_INET; + nh.gateway.aid = AID_INET; nh.gateway.v4.s_addr = k->r.nexthop.s_addr; } memcpy(&nh.kr.kr4, &k->r, sizeof(nh.kr.kr4)); - } else if (h->kroute != NULL && addr->af == AF_INET6) { + } else if (h->kroute != NULL && addr->aid == AID_INET6) { k6 = h->kroute; nh.connected = k6->r.flags & F_CONNECTED; if (memcmp(&k6->r.nexthop, &in6addr_any, sizeof(struct in6_addr)) != 0) { - nh.gateway.af = AF_INET6; + nh.gateway.aid = AID_INET6; memcpy(&nh.gateway.v6, &k6->r.nexthop, sizeof(struct in6_addr)); } @@ -563,14 +563,14 @@ kr_show_route(struct imsg *imsg) } addr = imsg->data; kr = NULL; - switch (addr->af) { - case AF_INET: + switch (addr->aid) { + case AID_INET: kr = kroute_match(addr->v4.s_addr, 1); if (kr != NULL) send_imsg_session(IMSG_CTL_KROUTE, imsg->hdr.pid, &kr->r, sizeof(kr->r)); break; - case AF_INET6: + case AID_INET6: kr6 = kroute6_match(&addr->v6, 1); if (kr6 != NULL) send_imsg_session(IMSG_CTL_KROUTE6, @@ -583,8 +583,8 @@ kr_show_route(struct imsg *imsg) bzero(&snh, sizeof(snh)); memcpy(&snh.addr, &h->nexthop, sizeof(snh.addr)); if (h->kroute != NULL) { - switch (h->nexthop.af) { - case AF_INET: + switch (h->nexthop.aid) { + case AID_INET: kr = h->kroute; snh.valid = kroute_validate(&kr->r); snh.krvalid = 1; @@ -592,7 +592,7 @@ kr_show_route(struct imsg *imsg) sizeof(snh.kr.kr4)); ifindex = kr->r.ifindex; break; - case AF_INET6: + case AID_INET6: kr6 = h->kroute; snh.valid = kroute6_validate(&kr6->r); snh.krvalid = 1; @@ -854,17 +854,17 @@ knexthop_compare(struct knexthop_node *a, struct knexthop_node *b) { int i; - if (a->nexthop.af != b->nexthop.af) - return (b->nexthop.af - a->nexthop.af); + if (a->nexthop.aid != b->nexthop.aid) + return (b->nexthop.aid - a->nexthop.aid); - switch (a->nexthop.af) { - case AF_INET: + switch (a->nexthop.aid) { + case AID_INET: if (ntohl(a->nexthop.v4.s_addr) < ntohl(b->nexthop.v4.s_addr)) return (-1); if (ntohl(a->nexthop.v4.s_addr) > ntohl(b->nexthop.v4.s_addr)) return (1); break; - case AF_INET6: + case AID_INET6: for (i = 0; i < 16; i++) { if (a->nexthop.v6.s6_addr[i] < b->nexthop.v6.s6_addr[i]) return (-1); @@ -953,7 +953,7 @@ kroute_insert(struct kroute_node *kr) mask = prefixlen2mask(kr->r.prefixlen); ina = ntohl(kr->r.prefix.s_addr); RB_FOREACH(h, knexthop_tree, &knt) - if (h->nexthop.af == AF_INET && + if (h->nexthop.aid == AID_INET && (ntohl(h->nexthop.v4.s_addr) & mask) == ina) knexthop_validate(h); @@ -1099,7 +1099,7 @@ kroute6_insert(struct kroute6_node *kr) if (kr->r.flags & F_KERNEL) { inet6applymask(&ina, &kr->r.prefix, kr->r.prefixlen); RB_FOREACH(h, knexthop_tree, &knt) - if (h->nexthop.af == AF_INET6) { + if (h->nexthop.aid == AID_INET6) { inet6applymask(&inb, &h->nexthop.v6, kr->r.prefixlen); if (memcmp(&ina, &inb, sizeof(ina)) == 0) @@ -1489,17 +1489,17 @@ knexthop_validate(struct knexthop_node *kn) struct kroute_nexthop n; int was_valid = 0; - if (kn->nexthop.af == AF_INET && (kr = kn->kroute) != NULL) + if (kn->nexthop.aid == AID_INET && (kr = kn->kroute) != NULL) was_valid = kroute_validate(&kr->r); - if (kn->nexthop.af == AF_INET6 && (kr6 = kn->kroute) != NULL) + if (kn->nexthop.aid == AID_INET6 && (kr6 = kn->kroute) != NULL) was_valid = kroute6_validate(&kr6->r); bzero(&n, sizeof(n)); memcpy(&n.nexthop, &kn->nexthop, sizeof(n.nexthop)); kroute_detach_nexthop(kn); - switch (kn->nexthop.af) { - case AF_INET: + switch (kn->nexthop.aid) { + case AID_INET: if ((kr = kroute_match(kn->nexthop.v4.s_addr, 0)) == NULL) { if (was_valid) send_nexthop_update(&n); @@ -1509,7 +1509,7 @@ knexthop_validate(struct knexthop_node *kn) n.connected = kr->r.flags & F_CONNECTED; if ((n.gateway.v4.s_addr = kr->r.nexthop.s_addr) != 0) - n.gateway.af = AF_INET; + n.gateway.aid = AID_INET; memcpy(&n.kr.kr4, &kr->r, sizeof(n.kr.kr4)); send_nexthop_update(&n); } else /* down */ @@ -1520,7 +1520,7 @@ knexthop_validate(struct knexthop_node *kn) kr->r.flags |= F_NEXTHOP; } break; - case AF_INET6: + case AID_INET6: if ((kr6 = kroute6_match(&kn->nexthop.v6, 0)) == NULL) { if (was_valid) send_nexthop_update(&n); @@ -1530,7 +1530,7 @@ knexthop_validate(struct knexthop_node *kn) n.connected = kr6->r.flags & F_CONNECTED; if (memcmp(&kr6->r.nexthop, &in6addr_any, sizeof(struct in6_addr)) != 0) { - n.gateway.af = AF_INET6; + n.gateway.aid = AID_INET6; memcpy(&n.gateway.v6, &kr6->r.nexthop, sizeof(struct in6_addr)); } @@ -1560,23 +1560,23 @@ knexthop_track(void *krn) bzero(&n, sizeof(n)); memcpy(&n.nexthop, &kn->nexthop, sizeof(n.nexthop)); - switch (kn->nexthop.af) { - case AF_INET: + switch (kn->nexthop.aid) { + case AID_INET: kr = krn; n.valid = 1; n.connected = kr->r.flags & F_CONNECTED; if ((n.gateway.v4.s_addr = kr->r.nexthop.s_addr) != 0) - n.gateway.af = AF_INET; + n.gateway.aid = AID_INET; memcpy(&n.kr.kr4, &kr->r, sizeof(n.kr.kr4)); break; - case AF_INET6: + case AID_INET6: kr6 = krn; n.valid = 1; n.connected = kr6->r.flags & F_CONNECTED; if (memcmp(&kr6->r.nexthop, &in6addr_any, sizeof(struct in6_addr)) != 0) { - n.gateway.af = AF_INET6; + n.gateway.aid = AID_INET6; memcpy(&n.gateway.v6, &kr6->r.nexthop, sizeof(struct in6_addr)); } @@ -1654,12 +1654,12 @@ kroute_detach_nexthop(struct knexthop_node *kn) ; /* nothing */ if (s == NULL) { - switch (kn->nexthop.af) { - case AF_INET: + switch (kn->nexthop.aid) { + case AID_INET: k = kn->kroute; k->r.flags &= ~F_NEXTHOP; break; - case AF_INET6: + case AID_INET6: k6 = kn->kroute; k6->r.flags &= ~F_NEXTHOP; break; @@ -1858,7 +1858,7 @@ if_change(u_short ifindex, int flags, struct if_data *ifd) nh.connected = 1; if ((nh.gateway.v4.s_addr = kkr->kr->r.nexthop.s_addr) != 0) - nh.gateway.af = AF_INET; + nh.gateway.aid = AID_INET; } memcpy(&nh.kr.kr4, &kkr->kr->r, sizeof(nh.kr.kr4)); @@ -1882,7 +1882,7 @@ if_change(u_short ifindex, int flags, struct if_data *ifd) if (memcmp(&kkr6->kr->r.nexthop, &in6addr_any, sizeof(struct in6_addr))) { - nh.gateway.af = AF_INET6; + nh.gateway.aid = AID_INET6; memcpy(&nh.gateway.v6, &kkr6->kr->r.nexthop, sizeof(struct in6_addr)); @@ -2504,9 +2504,9 @@ dispatch_rtmsg_addr(struct rt_msghdr *rtm, struct sockaddr *rti_info[RTAX_MAX], #endif prio = rtm->rtm_priority; - prefix.af = sa->sa_family; - switch (prefix.af) { + switch (sa->sa_family) { case AF_INET: + prefix.aid = AID_INET; prefix.v4.s_addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr; sa_in = (struct sockaddr_in *)rti_info[RTAX_NETMASK]; if (sa_in != NULL) { @@ -2520,6 +2520,7 @@ dispatch_rtmsg_addr(struct rt_msghdr *rtm, struct sockaddr *rti_info[RTAX_MAX], prefixlen_classful(prefix.v4.s_addr); break; case AF_INET6: + prefix.aid = AID_INET6; memcpy(&prefix.v6, &((struct sockaddr_in6 *)sa)->sin6_addr, sizeof(struct in6_addr)); sa_in6 = (struct sockaddr_in6 *)rti_info[RTAX_NETMASK]; @@ -2546,8 +2547,8 @@ dispatch_rtmsg_addr(struct rt_msghdr *rtm, struct sockaddr *rti_info[RTAX_MAX], } if (rtm->rtm_type == RTM_DELETE) { - switch (prefix.af) { - case AF_INET: + switch (prefix.aid) { + case AID_INET: sa_in = (struct sockaddr_in *)sa; if ((kr = kroute_find(prefix.v4.s_addr, prefixlen, prio)) == NULL) @@ -2566,7 +2567,7 @@ dispatch_rtmsg_addr(struct rt_msghdr *rtm, struct sockaddr *rti_info[RTAX_MAX], if (kroute_remove(kr) == -1) return (-1); break; - case AF_INET6: + case AID_INET6: sa_in6 = (struct sockaddr_in6 *)sa; if ((kr6 = kroute6_find(&prefix.v6, prefixlen, prio)) == NULL) @@ -2599,8 +2600,8 @@ dispatch_rtmsg_addr(struct rt_msghdr *rtm, struct sockaddr *rti_info[RTAX_MAX], return (0); } - switch (prefix.af) { - case AF_INET: + switch (prefix.aid) { + case AID_INET: sa_in = (struct sockaddr_in *)sa; if ((kr = kroute_find(prefix.v4.s_addr, prefixlen, prio)) != NULL) { @@ -2663,7 +2664,7 @@ add4: kroute_insert(kr); } break; - case AF_INET6: + case AID_INET6: sa_in6 = (struct sockaddr_in6 *)sa; if ((kr6 = kroute6_find(&prefix.v6, prefixlen, prio)) != NULL) { if (kr6->r.flags & F_KERNEL) { diff --git a/usr.sbin/bgpd/log.c b/usr.sbin/bgpd/log.c index c2ab54834c7..e2708cbc95d 100644 --- a/usr.sbin/bgpd/log.c +++ b/usr.sbin/bgpd/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.51 2009/11/02 20:38:15 claudio Exp $ */ +/* $OpenBSD: log.c,v 1.52 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -43,8 +43,9 @@ log_fmt_peer(const struct peer_config *peer) char *pfmt, *p; ip = log_addr(&peer->remote_addr); - if ((peer->remote_addr.af == AF_INET && peer->remote_masklen != 32) || - (peer->remote_addr.af == AF_INET6 && peer->remote_masklen != 128)) { + if ((peer->remote_addr.aid == AID_INET && peer->remote_masklen != 32) || + (peer->remote_addr.aid == AID_INET6 && + peer->remote_masklen != 128)) { if (asprintf(&p, "%s/%u", ip, peer->remote_masklen) == -1) fatal(NULL); } else { diff --git a/usr.sbin/bgpd/mrt.c b/usr.sbin/bgpd/mrt.c index da74d7af86f..22de447f335 100644 --- a/usr.sbin/bgpd/mrt.c +++ b/usr.sbin/bgpd/mrt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mrt.c,v 1.65 2009/10/26 09:27:58 claudio Exp $ */ +/* $OpenBSD: mrt.c,v 1.66 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org> @@ -194,7 +194,7 @@ mrt_dump_entry_mp(struct mrt *mrt, struct prefix *p, u_int16_t snum, struct bgpd_addr addr, nexthop, *nh; u_int16_t len; u_int8_t p_len; - sa_family_t af; + u_int8_t aid; if ((buf = buf_dynamic(0, MAX_PKTSIZE)) == NULL) { log_warn("mrt_dump_entry_mp: buf_dynamic"); @@ -219,14 +219,15 @@ mrt_dump_entry_mp(struct mrt *mrt, struct prefix *p, u_int16_t snum, DUMP_SHORT(h2buf, /* ifindex */ 0); /* XXX is this for peer self? */ - af = peer->remote_addr.af == 0 ? p->prefix->af : peer->remote_addr.af; - switch (af) { - case AF_INET: + aid = peer->remote_addr.aid == AID_UNSPEC ? p->prefix->aid : + peer->remote_addr.aid; + switch (aid) { + case AID_INET: DUMP_SHORT(h2buf, AFI_IPv4); DUMP_NLONG(h2buf, peer->local_v4_addr.v4.s_addr); DUMP_NLONG(h2buf, peer->remote_addr.v4.s_addr); break; - case AF_INET6: + case AID_INET6: DUMP_SHORT(h2buf, AFI_IPv6); if (buf_add(h2buf, &peer->local_v6_addr.v6, sizeof(struct in6_addr)) == -1 || @@ -237,7 +238,7 @@ mrt_dump_entry_mp(struct mrt *mrt, struct prefix *p, u_int16_t snum, } break; default: - log_warnx("king bula found new AF %d in mrt_dump_entry_mp", af); + log_warnx("king bula found new AF in mrt_dump_entry_mp"); goto fail; } @@ -247,20 +248,20 @@ mrt_dump_entry_mp(struct mrt *mrt, struct prefix *p, u_int16_t snum, if (p->aspath->nexthop == NULL) { bzero(&nexthop, sizeof(struct bgpd_addr)); - nexthop.af = addr.af; + nexthop.aid = addr.aid; nh = &nexthop; } else nh = &p->aspath->nexthop->exit_nexthop; pt_getaddr(p->prefix, &addr); - switch (addr.af) { - case AF_INET: + switch (addr.aid) { + case AID_INET: DUMP_SHORT(h2buf, AFI_IPv4); /* afi */ DUMP_BYTE(h2buf, SAFI_UNICAST); /* safi */ DUMP_BYTE(h2buf, 4); /* nhlen */ DUMP_NLONG(h2buf, nh->v4.s_addr); /* nexthop */ break; - case AF_INET6: + case AID_INET6: DUMP_SHORT(h2buf, AFI_IPv6); /* afi */ DUMP_BYTE(h2buf, SAFI_UNICAST); /* safi */ DUMP_BYTE(h2buf, 16); /* nhlen */ @@ -314,7 +315,8 @@ mrt_dump_entry(struct mrt *mrt, struct prefix *p, u_int16_t snum, struct bgpd_addr addr, *nh; size_t len; - if (p->prefix->af != AF_INET && peer->remote_addr.af == AF_INET) + if (p->prefix->aid != AID_INET && + peer->remote_addr.aid == AID_INET) /* only able to dump IPv4 */ return (0); @@ -325,7 +327,7 @@ mrt_dump_entry(struct mrt *mrt, struct prefix *p, u_int16_t snum, if (p->aspath->nexthop == NULL) { bzero(&addr, sizeof(struct bgpd_addr)); - addr.af = AF_INET; + addr.aid = AID_INET; nh = &addr; } else nh = &p->aspath->nexthop->exit_nexthop; diff --git a/usr.sbin/bgpd/parse.y b/usr.sbin/bgpd/parse.y index 3c1dd8d95c1..cf5645724ba 100644 --- a/usr.sbin/bgpd/parse.y +++ b/usr.sbin/bgpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.240 2009/11/26 13:40:43 henning Exp $ */ +/* $OpenBSD: parse.y,v 1.241 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -105,7 +105,7 @@ struct filter_match_l { struct filter_match m; struct filter_prefix_l *prefix_l; struct filter_as_l *as_l; - sa_family_t af; + u_int8_t aid; } fmopts; struct peer *alloc_peer(void); @@ -324,7 +324,7 @@ conf_main : AS as4number { conf->short_as = $3; } | ROUTERID address { - if ($2.af != AF_INET) { + if ($2.aid != AID_INET) { yyerror("router-id must be an IPv4 address"); YYERROR; } @@ -348,36 +348,13 @@ conf_main : AS as4number { } | LISTEN ON address { struct listen_addr *la; - struct sockaddr_in *in; - struct sockaddr_in6 *in6; if ((la = calloc(1, sizeof(struct listen_addr))) == NULL) fatal("parse conf_main listen on calloc"); la->fd = -1; - la->sa.ss_family = $3.af; - switch ($3.af) { - case AF_INET: - la->sa.ss_len = sizeof(struct sockaddr_in); - in = (struct sockaddr_in *)&la->sa; - in->sin_addr.s_addr = $3.v4.s_addr; - in->sin_port = htons(BGP_PORT); - break; - case AF_INET6: - la->sa.ss_len = sizeof(struct sockaddr_in6); - in6 = (struct sockaddr_in6 *)&la->sa; - memcpy(&in6->sin6_addr, &$3.v6, - sizeof(in6->sin6_addr)); - in6->sin6_port = htons(BGP_PORT); - break; - default: - free(la); - yyerror("king bula does not like family %u", - $3.af); - YYERROR; - } - + memcpy(addr2sa(&$3, BGP_PORT), &la->sa, sizeof(la->sa)); TAILQ_INSERT_TAIL(listen_addrs, la, entry); } | FIBUPDATE yesno { @@ -642,11 +619,11 @@ address : STRING { } free($1); - if (($$.af == AF_INET && len != 32) || - ($$.af == AF_INET6 && len != 128)) { + if (($$.aid == AID_INET && len != 32) || + ($$.aid == AID_INET6 && len != 128)) { /* unreachable */ yyerror("got prefixlen %u, expected %u", - len, $$.af == AF_INET ? 32 : 128); + len, $$.aid == AID_INET ? 32 : 128); YYERROR; } } @@ -693,7 +670,7 @@ prefix : STRING '/' NUMBER { addrspec : address { memcpy(&$$.prefix, &$1, sizeof(struct bgpd_addr)); - if ($$.prefix.af == AF_INET) + if ($$.prefix.aid == AID_INET) $$.len = 32; else $$.len = 128; @@ -717,17 +694,17 @@ neighbor : { curpeer = new_peer(); } memcpy(&curpeer->conf.remote_addr, &$3.prefix, sizeof(curpeer->conf.remote_addr)); curpeer->conf.remote_masklen = $3.len; - if (($3.prefix.af == AF_INET && $3.len != 32) || - ($3.prefix.af == AF_INET6 && $3.len != 128)) + if (($3.prefix.aid == AID_INET && $3.len != 32) || + ($3.prefix.aid == AID_INET6 && $3.len != 128)) curpeer->conf.template = 1; - switch (curpeer->conf.remote_addr.af) { - case AF_INET: + switch (curpeer->conf.remote_addr.aid) { + case AID_INET: if (curpeer->conf.capabilities.mp_v4 != SAFI_ALL) break; curpeer->conf.capabilities.mp_v4 = SAFI_UNICAST; break; - case AF_INET6: + case AID_INET6: if (curpeer->conf.capabilities.mp_v6 != SAFI_ALL) break; @@ -1115,7 +1092,7 @@ peeropts : REMOTEAS as4number { curpeer->conf.reflector_client = 1; } | REFLECTOR address { - if ($2.af != AF_INET) { + if ($2.aid != AID_INET) { yyerror("route reflector cluster-id must be " "an IPv4 address"); YYERROR; @@ -1368,12 +1345,12 @@ filter_prefix_l : filter_prefix { $$ = $1; } ; filter_prefix : prefix { - if (fmopts.af && fmopts.af != $1.prefix.af) { + if (fmopts.aid && fmopts.aid != $1.prefix.aid) { yyerror("rules with mixed address families " "are not allowed"); YYERROR; } else - fmopts.af = $1.prefix.af; + fmopts.aid = $1.prefix.aid; if (($$ = calloc(1, sizeof(struct filter_prefix_l))) == NULL) fatal(NULL); @@ -1469,18 +1446,18 @@ filter_elm : filter_prefix_h { fmopts.prefix_l = $1; } | PREFIXLEN prefixlenop { - if (fmopts.af == 0) { + if (fmopts.aid == 0) { yyerror("address family needs to be specified " "before \"prefixlen\""); YYERROR; } - if (fmopts.m.prefixlen.af) { + if (fmopts.m.prefixlen.aid) { yyerror("\"prefixlen\" already specified"); YYERROR; } memcpy(&fmopts.m.prefixlen, &$2, sizeof(fmopts.m.prefixlen)); - fmopts.m.prefixlen.af = fmopts.af; + fmopts.m.prefixlen.aid = fmopts.aid; } | filter_as_h { if (fmopts.as_l != NULL) { @@ -1503,18 +1480,18 @@ filter_elm : filter_prefix_h { free($2); } | IPV4 { - if (fmopts.af) { + if (fmopts.aid) { yyerror("address family already specified"); YYERROR; } - fmopts.af = AF_INET; + fmopts.aid = AID_INET; } | IPV6 { - if (fmopts.af) { + if (fmopts.aid) { yyerror("address family already specified"); YYERROR; } - fmopts.af = AF_INET6; + fmopts.aid = AID_INET6; } ; @@ -2771,7 +2748,7 @@ get_id(struct peer *newpeer) struct peer *p; for (p = peer_l_old; p != NULL; p = p->next) - if (newpeer->conf.remote_addr.af) { + if (newpeer->conf.remote_addr.aid) { if (!memcmp(&p->conf.remote_addr, &newpeer->conf.remote_addr, sizeof(p->conf.remote_addr))) { @@ -2913,8 +2890,8 @@ int neighbor_consistent(struct peer *p) { /* local-address and peer's address: same address family */ - if (p->conf.local_addr.af && - p->conf.local_addr.af != p->conf.remote_addr.af) { + if (p->conf.local_addr.aid && + p->conf.local_addr.aid != p->conf.remote_addr.aid) { yyerror("local-address and neighbor address " "must be of the same address family"); return (-1); @@ -2925,7 +2902,7 @@ neighbor_consistent(struct peer *p) p->conf.auth.method == AUTH_IPSEC_IKE_AH || p->conf.auth.method == AUTH_IPSEC_MANUAL_ESP || p->conf.auth.method == AUTH_IPSEC_MANUAL_AH) && - !p->conf.local_addr.af) { + !p->conf.local_addr.aid) { yyerror("neighbors with any form of IPsec configured " "need local-address to be specified"); return (-1); @@ -3012,8 +2989,8 @@ merge_filterset(struct filter_set_head *sh, struct filter_set *s) } break; case ACTION_SET_NEXTHOP: - if (s->action.nexthop.af < - t->action.nexthop.af) { + if (s->action.nexthop.aid < + t->action.nexthop.aid) { TAILQ_INSERT_BEFORE(t, s, entry); return (0); } diff --git a/usr.sbin/bgpd/pfkey.c b/usr.sbin/bgpd/pfkey.c index 80936f4cc62..02b61756370 100644 --- a/usr.sbin/bgpd/pfkey.c +++ b/usr.sbin/bgpd/pfkey.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pfkey.c,v 1.37 2009/04/21 15:25:52 henning Exp $ */ +/* $OpenBSD: pfkey.c,v 1.38 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -81,22 +81,16 @@ pfkey_send(int sd, uint8_t satype, uint8_t mtype, uint8_t dir, /* we need clean sockaddr... no ports set */ bzero(&ssrc, sizeof(ssrc)); bzero(&smask, sizeof(smask)); - switch (src->af) { - case AF_INET: - ((struct sockaddr_in *)&ssrc)->sin_addr = src->v4; - ssrc.ss_len = sizeof(struct sockaddr_in); - ssrc.ss_family = AF_INET; + memcpy(addr2sa(src, 0), &ssrc, sizeof(ssrc)); + switch (src->aid) { + case AID_INET: memset(&((struct sockaddr_in *)&smask)->sin_addr, 0xff, 32/8); break; - case AF_INET6: - memcpy(&((struct sockaddr_in6 *)&ssrc)->sin6_addr, - &src->v6, sizeof(struct in6_addr)); - ssrc.ss_len = sizeof(struct sockaddr_in6); - ssrc.ss_family = AF_INET6; + case AID_INET6: memset(&((struct sockaddr_in6 *)&smask)->sin6_addr, 0xff, 128/8); break; - case 0: + case AID_UNSPEC: ssrc.ss_len = sizeof(struct sockaddr); break; default: @@ -107,22 +101,16 @@ pfkey_send(int sd, uint8_t satype, uint8_t mtype, uint8_t dir, bzero(&sdst, sizeof(sdst)); bzero(&dmask, sizeof(dmask)); - switch (dst->af) { - case AF_INET: - ((struct sockaddr_in *)&sdst)->sin_addr = dst->v4; - sdst.ss_len = sizeof(struct sockaddr_in); - sdst.ss_family = AF_INET; + memcpy(addr2sa(dst, 0), &sdst, sizeof(sdst)); + switch (dst->aid) { + case AID_INET: memset(&((struct sockaddr_in *)&dmask)->sin_addr, 0xff, 32/8); break; - case AF_INET6: - memcpy(&((struct sockaddr_in6 *)&sdst)->sin6_addr, - &dst->v6, sizeof(struct in6_addr)); - sdst.ss_len = sizeof(struct sockaddr_in6); - sdst.ss_family = AF_INET6; + case AID_INET6: memset(&((struct sockaddr_in6 *)&dmask)->sin6_addr, 0xff, 128/8); break; - case 0: + case AID_UNSPEC: sdst.ss_len = sizeof(struct sockaddr); break; default: @@ -220,8 +208,8 @@ pfkey_send(int sd, uint8_t satype, uint8_t mtype, uint8_t dir, sa_dst.sadb_address_exttype = SADB_X_EXT_DST_FLOW; bzero(&smask, sizeof(smask)); - switch (src->af) { - case AF_INET: + switch (src->aid) { + case AID_INET: smask.ss_len = sizeof(struct sockaddr_in); smask.ss_family = AF_INET; memset(&((struct sockaddr_in *)&smask)->sin_addr, @@ -233,7 +221,7 @@ pfkey_send(int sd, uint8_t satype, uint8_t mtype, uint8_t dir, htons(0xffff); } break; - case AF_INET6: + case AID_INET6: smask.ss_len = sizeof(struct sockaddr_in6); smask.ss_family = AF_INET6; memset(&((struct sockaddr_in6 *)&smask)->sin6_addr, @@ -247,8 +235,8 @@ pfkey_send(int sd, uint8_t satype, uint8_t mtype, uint8_t dir, break; } bzero(&dmask, sizeof(dmask)); - switch (dst->af) { - case AF_INET: + switch (dst->aid) { + case AID_INET: dmask.ss_len = sizeof(struct sockaddr_in); dmask.ss_family = AF_INET; memset(&((struct sockaddr_in *)&dmask)->sin_addr, @@ -260,7 +248,7 @@ pfkey_send(int sd, uint8_t satype, uint8_t mtype, uint8_t dir, htons(0xffff); } break; - case AF_INET6: + case AID_INET6: dmask.ss_len = sizeof(struct sockaddr_in6); dmask.ss_family = AF_INET6; memset(&((struct sockaddr_in6 *)&dmask)->sin6_addr, diff --git a/usr.sbin/bgpd/pftable.c b/usr.sbin/bgpd/pftable.c index 13b8c48d252..0a365df3dfe 100644 --- a/usr.sbin/bgpd/pftable.c +++ b/usr.sbin/bgpd/pftable.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pftable.c,v 1.5 2005/07/01 09:19:24 claudio Exp $ */ +/* $OpenBSD: pftable.c,v 1.6 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2004 Damien Miller <djm@openbsd.org> @@ -214,7 +214,7 @@ pftable_add_work(const char *table, struct bgpd_addr *addr, bzero(pfa, sizeof(*pfa)); memcpy(&pfa->pfra_u, &addr->ba, (len + 7U) / 8); - pfa->pfra_af = addr->af; + pfa->pfra_af = aid2af(addr->aid); pfa->pfra_net = len; pft->naddrs++; diff --git a/usr.sbin/bgpd/printconf.c b/usr.sbin/bgpd/printconf.c index d1647aa1a96..6503a51fff1 100644 --- a/usr.sbin/bgpd/printconf.c +++ b/usr.sbin/bgpd/printconf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: printconf.c,v 1.73 2009/11/26 13:40:43 henning Exp $ */ +/* $OpenBSD: printconf.c,v 1.74 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -281,8 +281,8 @@ print_peer(struct peer_config *p, struct bgpd_config *conf, const char *c) char *method; struct in_addr ina; - if ((p->remote_addr.af == AF_INET && p->remote_masklen != 32) || - (p->remote_addr.af == AF_INET6 && p->remote_masklen != 128)) + if ((p->remote_addr.aid == AID_INET && p->remote_masklen != 32) || + (p->remote_addr.aid == AID_INET6 && p->remote_masklen != 128)) printf("%sneighbor %s/%u {\n", c, log_addr(&p->remote_addr), p->remote_masklen); else @@ -299,7 +299,7 @@ print_peer(struct peer_config *p, struct bgpd_config *conf, const char *c) printf("%s\tmultihop %u\n", c, p->distance); if (p->passive) printf("%s\tpassive\n", c); - if (p->local_addr.af) + if (p->local_addr.aid) printf("%s\tlocal-address %s\n", c, log_addr(&p->local_addr)); if (p->max_prefix) { printf("%s\tmax-prefix %u", c, p->max_prefix); @@ -479,14 +479,14 @@ print_rule(struct peer *peer_l, struct filter_rule *r) } else printf("any "); - if (r->match.prefix.addr.af) + if (r->match.prefix.addr.aid) printf("prefix %s/%u ", log_addr(&r->match.prefix.addr), r->match.prefix.len); - if (r->match.prefix.addr.af == 0 && r->match.prefixlen.af) { - if (r->match.prefixlen.af == AF_INET) + if (r->match.prefix.addr.aid == 0 && r->match.prefixlen.aid) { + if (r->match.prefixlen.aid == AID_INET) printf("inet "); - if (r->match.prefixlen.af == AF_INET6) + if (r->match.prefixlen.aid == AID_INET6) printf("inet6 "); } diff --git a/usr.sbin/bgpd/rde.c b/usr.sbin/bgpd/rde.c index 631b184462e..732a7deca43 100644 --- a/usr.sbin/bgpd/rde.c +++ b/usr.sbin/bgpd/rde.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde.c,v 1.274 2009/11/11 13:48:34 claudio Exp $ */ +/* $OpenBSD: rde.c,v 1.275 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -452,13 +452,13 @@ rde_dispatch_imsg_session(struct imsgbuf *ibuf) break; } session_set = NULL; - switch (netconf_s.prefix.af) { - case AF_INET: + switch (netconf_s.prefix.aid) { + case AID_INET: if (netconf_s.prefixlen > 32) goto badnet; network_add(&netconf_s, 0); break; - case AF_INET6: + case AID_INET6: if (netconf_s.prefixlen > 128) goto badnet; network_add(&netconf_s, 0); @@ -1269,7 +1269,7 @@ bad_flags: a->flags |= F_ATTR_NEXTHOP; bzero(&nexthop, sizeof(nexthop)); - nexthop.af = AF_INET; + nexthop.aid = AID_INET; UPD_READ(&nexthop.v4.s_addr, p, plen, 4); /* * Check if the nexthop is a valid IP address. We consider @@ -1525,7 +1525,7 @@ rde_get_mp_nexthop(u_char *data, u_int16_t len, u_int16_t afi, log_warnx("bad multiprotocol nexthop, bad size"); return (-1); } - nexthop.af = AF_INET6; + nexthop.aid = AID_INET6; memcpy(&nexthop.v6.s6_addr, data, 16); asp->nexthop = nexthop_get(&nexthop); /* @@ -1577,7 +1577,7 @@ rde_update_get_prefix(u_char *p, u_int16_t len, struct bgpd_addr *prefix, plen++; } } - prefix->af = AF_INET; + prefix->aid = AID_INET; prefix->v4.s_addr = addr.a32.s_addr; *prefixlen = pfxlen; @@ -1608,7 +1608,7 @@ rde_update_get_prefix6(u_char *p, u_int16_t len, struct bgpd_addr *prefix, plen++; } } - prefix->af = AF_INET6; + prefix->aid = AID_INET6; *prefixlen = pfxlen; return (plen); @@ -1814,8 +1814,8 @@ rde_dump_rib_as(struct prefix *p, struct rde_aspath *asp, pid_t pid, int flags) /* announced network may have a NULL nexthop */ bzero(&rib.true_nexthop, sizeof(rib.true_nexthop)); bzero(&rib.exit_nexthop, sizeof(rib.exit_nexthop)); - rib.true_nexthop.af = p->prefix->af; - rib.exit_nexthop.af = p->prefix->af; + rib.true_nexthop.aid = p->prefix->aid; + rib.exit_nexthop.aid = p->prefix->aid; } pt_getaddr(p->prefix, &rib.prefix); rib.prefixlen = p->prefix->prefixlen; @@ -1938,7 +1938,7 @@ rde_dump_prefix_upcall(struct rib_entry *re, void *ptr) pt = re->prefix; pt_getaddr(pt, &addr); - if (addr.af != ctx->req.prefix.af) + if (addr.aid != ctx->req.prefix.aid) return; if (ctx->req.prefixlen > pt->prefixlen) return; @@ -2078,8 +2078,8 @@ rde_send_kroute(struct prefix *new, struct prefix *old) } pt_getaddr(p->prefix, &addr); - switch (addr.af) { - case AF_INET: + switch (addr.aid) { + case AID_INET: bzero(&kl, sizeof(kl)); kl.kr.prefix.s_addr = addr.v4.s_addr; kl.kr.prefixlen = p->prefix->prefixlen; @@ -2096,7 +2096,7 @@ rde_send_kroute(struct prefix *new, struct prefix *old) sizeof(kl)) == -1) fatal("imsg_compose error"); break; - case AF_INET6: + case AID_INET6: bzero(&kl6, sizeof(kl6)); memcpy(&kl6.kr.prefix, &addr.v6, sizeof(struct in6_addr)); kl6.kr.prefixlen = p->prefix->prefixlen; @@ -2636,16 +2636,9 @@ peer_localaddrs(struct rde_peer *peer, struct bgpd_addr *laddr) if (ifa->ifa_addr->sa_family == match->ifa_addr->sa_family) ifa = match; - peer->local_v4_addr.af = AF_INET; - peer->local_v4_addr.v4.s_addr = - ((struct sockaddr_in *)ifa->ifa_addr)-> - sin_addr.s_addr; + sa2addr(ifa->ifa_addr, &peer->local_v4_addr); break; - } - } - - for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { - if (ifa->ifa_addr->sa_family == AF_INET6 && + } else if (ifa->ifa_addr->sa_family == AF_INET6 && strcmp(ifa->ifa_name, match->ifa_name) == 0) { /* * only accept global scope addresses except explicitly @@ -2661,13 +2654,7 @@ peer_localaddrs(struct rde_peer *peer, struct bgpd_addr *laddr) &((struct sockaddr_in6 *)ifa-> ifa_addr)->sin6_addr)) continue; - peer->local_v6_addr.af = AF_INET6; - memcpy(&peer->local_v6_addr.v6, - &((struct sockaddr_in6 *)ifa->ifa_addr)-> - sin6_addr, sizeof(struct in6_addr)); - peer->local_v6_addr.scope_id = - ((struct sockaddr_in6 *)ifa->ifa_addr)-> - sin6_scope_id; + sa2addr(ifa->ifa_addr, &peer->local_v6_addr); break; } } @@ -2767,9 +2754,7 @@ peer_dump(u_int32_t id, u_int16_t afi, u_int8_t safi) peer_send_eor(peer, AFI_IPv4, SAFI_UNICAST); } if (afi == AFI_ALL || afi == AFI_IPv6) - if ((safi == SAFI_ALL || safi == SAFI_UNICAST) && - peer->capa_announced.mp_v6 != SAFI_NONE && - peer->capa_received.mp_v6 != SAFI_NONE) { + if ((safi == SAFI_ALL || safi == SAFI_UNICAST)) { if (peer->conf.announce_type == ANNOUNCE_DEFAULT_ROUTE) up_generate_default(rules_l, peer, AF_INET6); else @@ -2845,7 +2830,7 @@ network_add(struct network_config *nc, int flagstatic) if (!flagstatic) asp->flags |= F_ANN_DYNAMIC; - rde_apply_set(asp, &nc->attrset, nc->prefix.af, peerself, peerself); + rde_apply_set(asp, &nc->attrset, aid2af(nc->prefix.aid), peerself, peerself); for (i = 1; i < rib_size; i++) path_update(&ribs[i], peerself, asp, &nc->prefix, nc->prefixlen); @@ -2880,9 +2865,10 @@ network_dump_upcall(struct rib_entry *re, void *ptr) LIST_FOREACH(p, &re->prefix_h, rib_l) { if (!(p->aspath->flags & F_PREFIX_ANNOUNCED)) continue; - if (p->prefix->af == AF_INET) { + pt_getaddr(p->prefix, &addr); + switch (addr.aid) { + case AID_INET: bzero(&k, sizeof(k)); - pt_getaddr(p->prefix, &addr); k.prefix.s_addr = addr.v4.s_addr; k.prefixlen = p->prefix->prefixlen; if (p->aspath->peer == peerself) @@ -2891,10 +2877,9 @@ network_dump_upcall(struct rib_entry *re, void *ptr) ctx->req.pid, -1, &k, sizeof(k)) == -1) log_warnx("network_dump_upcall: " "imsg_compose error"); - } - if (p->prefix->af == AF_INET6) { + break; + case AID_INET6: bzero(&k6, sizeof(k6)); - pt_getaddr(p->prefix, &addr); memcpy(&k6.prefix, &addr.v6, sizeof(k6.prefix)); k6.prefixlen = p->prefix->prefixlen; if (p->aspath->peer == peerself) @@ -2903,6 +2888,7 @@ network_dump_upcall(struct rib_entry *re, void *ptr) ctx->req.pid, -1, &k6, sizeof(k6)) == -1) log_warnx("network_dump_upcall: " "imsg_compose error"); + break; } } } @@ -2949,10 +2935,10 @@ sa_cmp(struct bgpd_addr *a, struct sockaddr *b) struct sockaddr_in *in_b; struct sockaddr_in6 *in6_b; - if (a->af != b->sa_family) + if (aid2af(a->aid) != b->sa_family) return (1); - switch (a->af) { + switch (b->sa_family) { case AF_INET: in_b = (struct sockaddr_in *)b; if (a->v4.s_addr != in_b->sin_addr.s_addr) diff --git a/usr.sbin/bgpd/rde.h b/usr.sbin/bgpd/rde.h index bb4a7e929ee..3079802dc8b 100644 --- a/usr.sbin/bgpd/rde.h +++ b/usr.sbin/bgpd/rde.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rde.h,v 1.122 2009/10/05 12:03:45 claudio Exp $ */ +/* $OpenBSD: rde.h,v 1.123 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org> and @@ -221,14 +221,14 @@ struct nexthop { /* generic entry without address specific part */ struct pt_entry { RB_ENTRY(pt_entry) pt_e; - sa_family_t af; + u_int8_t aid; u_int8_t prefixlen; u_int16_t refcnt; }; struct pt_entry4 { RB_ENTRY(pt_entry) pt_e; - sa_family_t af; + u_int8_t aid; u_int8_t prefixlen; u_int16_t refcnt; struct in_addr prefix4; @@ -236,7 +236,7 @@ struct pt_entry4 { struct pt_entry6 { RB_ENTRY(pt_entry) pt_e; - sa_family_t af; + u_int8_t aid; u_int8_t prefixlen; u_int16_t refcnt; struct in6_addr prefix6; @@ -397,7 +397,7 @@ void prefix_network_clean(struct rde_peer *, time_t, u_int32_t); void nexthop_init(u_int32_t); void nexthop_shutdown(void); void nexthop_modify(struct rde_aspath *, struct bgpd_addr *, - enum action_types, sa_family_t); + enum action_types, u_int8_t); void nexthop_link(struct rde_aspath *); void nexthop_unlink(struct rde_aspath *); int nexthop_delete(struct nexthop *); @@ -454,7 +454,7 @@ enum filter_actions rde_filter(u_int16_t, struct rde_aspath **, struct rde_aspath *, struct bgpd_addr *, u_int8_t, struct rde_peer *, enum directions); void rde_apply_set(struct rde_aspath *, struct filter_set_head *, - sa_family_t, struct rde_peer *, struct rde_peer *); + u_int8_t, struct rde_peer *, struct rde_peer *); int rde_filter_community(struct rde_aspath *, int, int); int rde_filter_equal(struct filter_head *, struct filter_head *, struct rde_peer *, enum directions); diff --git a/usr.sbin/bgpd/rde_filter.c b/usr.sbin/bgpd/rde_filter.c index 60ccb313a6d..4d313dcca3f 100644 --- a/usr.sbin/bgpd/rde_filter.c +++ b/usr.sbin/bgpd/rde_filter.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_filter.c,v 1.58 2009/11/26 13:40:43 henning Exp $ */ +/* $OpenBSD: rde_filter.c,v 1.59 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2004 Claudio Jeker <claudio@openbsd.org> @@ -66,7 +66,7 @@ rde_filter(u_int16_t ribid, struct rde_aspath **new, struct filter_head *rules, /* ... and use the copy from now on */ asp = *new; } - rde_apply_set(asp, &f->set, prefix->af, + rde_apply_set(asp, &f->set, prefix->aid, from, peer); } if (f->action != ACTION_NONE) @@ -80,7 +80,7 @@ rde_filter(u_int16_t ribid, struct rde_aspath **new, struct filter_head *rules, void rde_apply_set(struct rde_aspath *asp, struct filter_set_head *sh, - sa_family_t af, struct rde_peer *from, struct rde_peer *peer) + u_int8_t aid, struct rde_peer *from, struct rde_peer *peer) { struct filter_set *set; u_char *np; @@ -174,7 +174,7 @@ rde_apply_set(struct rde_aspath *asp, struct filter_set_head *sh, case ACTION_SET_NEXTHOP_NOMODIFY: case ACTION_SET_NEXTHOP_SELF: nexthop_modify(asp, &set->action.nexthop, set->type, - af); + aid); break; case ACTION_SET_COMMUNITY: switch (set->action.community.as) { @@ -295,8 +295,8 @@ rde_filter_match(struct filter_rule *f, struct rde_aspath *asp, return (0); } - if (f->match.prefix.addr.af != 0) { - if (f->match.prefix.addr.af != prefix->af) + if (f->match.prefix.addr.aid != 0) { + if (f->match.prefix.addr.aid != prefix->aid) /* don't use IPv4 rules for IPv6 and vice versa */ return (0); @@ -332,7 +332,7 @@ rde_filter_match(struct filter_rule *f, struct rde_aspath *asp, } else if (f->match.prefixlen.op != OP_NONE) { /* only prefixlen without a prefix */ - if (f->match.prefixlen.af != prefix->af) + if (f->match.prefixlen.aid != prefix->aid) /* don't use IPv4 rules for IPv6 and vice versa */ return (0); @@ -493,7 +493,7 @@ filterset_cmp(struct filter_set *a, struct filter_set *b) * reject it at the same time. Allow one IPv4 and one IPv6 * per filter set or only one of the other nexthop modifiers. */ - return (a->action.nexthop.af - b->action.nexthop.af); + return (a->action.nexthop.aid - b->action.nexthop.aid); } /* equal */ diff --git a/usr.sbin/bgpd/rde_prefix.c b/usr.sbin/bgpd/rde_prefix.c index 0c8e211f688..019e8123464 100644 --- a/usr.sbin/bgpd/rde_prefix.c +++ b/usr.sbin/bgpd/rde_prefix.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_prefix.c,v 1.29 2009/05/30 18:27:17 claudio Exp $ */ +/* $OpenBSD: rde_prefix.c,v 1.30 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org> @@ -38,15 +38,16 @@ * pt_lookup: lookup a IP in the prefix table. Mainly for "show ip bgp". * pt_empty: returns true if there is no bgp prefix linked to the pt_entry. * pt_init: initialize prefix table. - * pt_alloc?: allocate a AF specific pt_entry. Internal function. + * pt_alloc: allocate a AF specific pt_entry. Internal function. * pt_free: free a pt_entry. Internal function. */ /* internal prototypes */ -static struct pt_entry4 *pt_alloc4(void); -static struct pt_entry6 *pt_alloc6(void); +static struct pt_entry *pt_alloc(struct pt_entry *); static void pt_free(struct pt_entry *); +size_t pt_sizes[AID_MAX] = AID_PTSIZE; + RB_HEAD(pt_tree, pt_entry); RB_PROTOTYPE(pt_tree, pt_entry, pt_e, pt_prefix_cmp); RB_GENERATE(pt_tree, pt_entry, pt_e, pt_prefix_cmp); @@ -70,13 +71,12 @@ void pt_getaddr(struct pt_entry *pte, struct bgpd_addr *addr) { bzero(addr, sizeof(struct bgpd_addr)); - switch (pte->af) { - case AF_INET: - addr->af = pte->af; + addr->aid = pte->aid; + switch (addr->aid) { + case AID_INET: addr->v4 = ((struct pt_entry4 *)pte)->prefix4; break; - case AF_INET6: - addr->af = pte->af; + case AID_INET6: memcpy(&addr->v6, &((struct pt_entry6 *)pte)->prefix6, sizeof(addr->v6)); /* XXX scope_id ??? */ @@ -93,27 +93,29 @@ pt_fill(struct bgpd_addr *prefix, int prefixlen) static struct pt_entry6 pte6; in_addr_t addr_hbo; - switch (prefix->af) { - case AF_INET: + switch (prefix->aid) { + case AID_INET: bzero(&pte4, sizeof(pte4)); + if (af2aid(AF_INET, 0, &pte4.aid)) + fatalx("pt_fill: unknown aid"); if (prefixlen > 32) - fatalx("pt_get: bad IPv4 prefixlen"); - pte4.af = AF_INET; + fatalx("pt_fill: bad IPv4 prefixlen"); addr_hbo = ntohl(prefix->v4.s_addr); pte4.prefix4.s_addr = htonl(addr_hbo & prefixlen2mask(prefixlen)); pte4.prefixlen = prefixlen; return ((struct pt_entry *)&pte4); - case AF_INET6: + case AID_INET6: bzero(&pte6, sizeof(pte6)); + if (af2aid(AF_INET6, 0, &pte6.aid)) + fatalx("pt_fill: unknown aid"); if (prefixlen > 128) fatalx("pt_get: bad IPv6 prefixlen"); - pte6.af = AF_INET6; pte6.prefixlen = prefixlen; inet6applymask(&pte6.prefix6, &prefix->v6, prefixlen); return ((struct pt_entry *)&pte6); default: - log_warnx("pt_get: unknown af"); + log_warnx("pt_fill: unknown af"); return (NULL); } } @@ -131,34 +133,9 @@ struct pt_entry * pt_add(struct bgpd_addr *prefix, int prefixlen) { struct pt_entry *p = NULL; - struct pt_entry4 *p4; - struct pt_entry6 *p6; - in_addr_t addr_hbo; - switch (prefix->af) { - case AF_INET: - p4 = pt_alloc4(); - if (prefixlen > 32) - fatalx("pt_add: bad IPv4 prefixlen"); - p4->af = AF_INET; - p4->prefixlen = prefixlen; - addr_hbo = ntohl(prefix->v4.s_addr); - p4->prefix4.s_addr = htonl(addr_hbo & - prefixlen2mask(prefixlen)); - p = (struct pt_entry *)p4; - break; - case AF_INET6: - p6 = pt_alloc6(); - if (prefixlen > 128) - fatalx("pt_add: bad IPv6 prefixlen"); - p6->af = AF_INET6; - p6->prefixlen = prefixlen; - inet6applymask(&p6->prefix6, &prefix->v6, prefixlen); - p = (struct pt_entry *)p6; - break; - default: - fatalx("pt_add: unknown af"); - } + p = pt_fill(prefix, prefixlen); + p = pt_alloc(p); if (RB_INSERT(pt_tree, &pttable, p) != NULL) { log_warnx("pt_add: insert failed"); @@ -185,11 +162,11 @@ pt_lookup(struct bgpd_addr *addr) struct pt_entry *p; int i; - switch (addr->af) { - case AF_INET: + switch (addr->aid) { + case AID_INET: i = 32; break; - case AF_INET6: + case AID_INET6: i = 128; break; default: @@ -210,13 +187,13 @@ pt_prefix_cmp(const struct pt_entry *a, const struct pt_entry *b) const struct pt_entry6 *a6, *b6; int i; - if (a->af > b->af) + if (a->aid > b->aid) return (1); - if (a->af < b->af) + if (a->aid < b->aid) return (-1); - switch (a->af) { - case AF_INET: + switch (a->aid) { + case AID_INET: a4 = (const struct pt_entry4 *)a; b4 = (const struct pt_entry4 *)b; if (ntohl(a4->prefix4.s_addr) > ntohl(b4->prefix4.s_addr)) @@ -228,7 +205,7 @@ pt_prefix_cmp(const struct pt_entry *a, const struct pt_entry *b) if (a4->prefixlen < b4->prefixlen) return (-1); return (0); - case AF_INET6: + case AID_INET6: a6 = (const struct pt_entry6 *)a; b6 = (const struct pt_entry6 *)b; @@ -248,43 +225,27 @@ pt_prefix_cmp(const struct pt_entry *a, const struct pt_entry *b) return (-1); } -/* returns a zeroed pt_entry function may not return on fail */ -static struct pt_entry4 * -pt_alloc4(void) +/* + * Returns a pt_entry cloned from the one passed in. + * Function may not return on failure. + */ +static struct pt_entry * +pt_alloc(struct pt_entry *op) { - struct pt_entry4 *p; + struct pt_entry *p; - p = calloc(1, sizeof(*p)); + p = malloc(pt_sizes[op->aid]); if (p == NULL) fatal("pt_alloc"); - rdemem.pt4_cnt++; - return (p); -} - -static struct pt_entry6 * -pt_alloc6(void) -{ - struct pt_entry6 *p; + rdemem.pt_cnt[op->aid]++; + memcpy(p, op, pt_sizes[op->aid]); - p = calloc(1, sizeof(*p)); - if (p == NULL) - fatal("pt_alloc"); - rdemem.pt6_cnt++; return (p); } static void pt_free(struct pt_entry *pte) { - switch (pte->af) { - case AF_INET: - rdemem.pt4_cnt--; - break; - case AF_INET6: - rdemem.pt6_cnt--; - break; - default: - break; - } + rdemem.pt_cnt[pte->aid]--; free(pte); } diff --git a/usr.sbin/bgpd/rde_rib.c b/usr.sbin/bgpd/rde_rib.c index 591d4e60936..1e0d8ddee11 100644 --- a/usr.sbin/bgpd/rde_rib.c +++ b/usr.sbin/bgpd/rde_rib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_rib.c,v 1.117 2009/10/05 12:03:45 claudio Exp $ */ +/* $OpenBSD: rde_rib.c,v 1.118 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org> @@ -173,15 +173,15 @@ rib_lookup(struct rib *rib, struct bgpd_addr *addr) struct rib_entry *re; int i; - switch (addr->af) { - case AF_INET: + switch (addr->aid) { + case AID_INET: for (i = 32; i >= 0; i--) { re = rib_get(rib, addr, i); if (re != NULL) return (re); } break; - case AF_INET6: + case AID_INET6: for (i = 128; i >= 0; i--) { re = rib_get(rib, addr, i); if (re != NULL) @@ -280,7 +280,8 @@ rib_dump_r(struct rib_context *ctx) re = rib_restart(ctx); for (i = 0; re != NULL; re = RB_NEXT(rib_tree, unused, re)) { - if (ctx->ctx_af != AF_UNSPEC && ctx->ctx_af != re->prefix->af) + if (ctx->ctx_af != AF_UNSPEC && + ctx->ctx_af != aid2af(re->prefix->aid)) continue; if (ctx->ctx_count && i++ >= ctx->ctx_count && (re->flags & F_RIB_ENTRYLOCK) == 0) { @@ -632,11 +633,11 @@ prefix_compare(const struct bgpd_addr *a, const struct bgpd_addr *b, int i; u_int8_t m; - if (a->af != b->af) - return (a->af - b->af); + if (a->aid != b->aid) + return (a->aid - b->aid); - switch (a->af) { - case AF_INET: + switch (a->aid) { + case AID_INET: if (prefixlen > 32) fatalx("prefix_cmp: bad IPv4 prefixlen"); mask = htonl(prefixlen2mask(prefixlen)); @@ -645,7 +646,7 @@ prefix_compare(const struct bgpd_addr *a, const struct bgpd_addr *b, if (aa != ba) return (aa - ba); return (0); - case AF_INET6: + case AID_INET6: if (prefixlen > 128) fatalx("prefix_cmp: bad IPv6 prefixlen"); for (i = 0; i < prefixlen / 8; i++) @@ -806,7 +807,7 @@ prefix_write(u_char *buf, int len, struct bgpd_addr *prefix, u_int8_t plen) { int totlen; - if (prefix->af != AF_INET && prefix->af != AF_INET6) + if (prefix->aid != AID_INET && prefix->aid != AID_INET6) return (-1); totlen = PREFIX_SIZE(plen); @@ -1088,15 +1089,15 @@ nexthop_update(struct kroute_nexthop *msg) memcpy(&nh->true_nexthop, &msg->gateway, sizeof(nh->true_nexthop)); - switch (msg->nexthop.af) { - case AF_INET: + switch (msg->nexthop.aid) { + case AID_INET: nh->nexthop_netlen = msg->kr.kr4.prefixlen; - nh->nexthop_net.af = AF_INET; + nh->nexthop_net.aid = AID_INET; nh->nexthop_net.v4.s_addr = msg->kr.kr4.prefix.s_addr; break; - case AF_INET6: + case AID_INET6: nh->nexthop_netlen = msg->kr.kr6.prefixlen; - nh->nexthop_net.af = AF_INET6; + nh->nexthop_net.aid = AID_INET6; memcpy(&nh->nexthop_net.v6, &msg->kr.kr6.prefix, sizeof(struct in6_addr)); break; @@ -1118,7 +1119,7 @@ nexthop_update(struct kroute_nexthop *msg) void nexthop_modify(struct rde_aspath *asp, struct bgpd_addr *nexthop, - enum action_types type, sa_family_t af) + enum action_types type, u_int8_t aid) { struct nexthop *nh; @@ -1138,7 +1139,7 @@ nexthop_modify(struct rde_aspath *asp, struct bgpd_addr *nexthop, asp->flags |= F_NEXTHOP_SELF; return; } - if (af != nexthop->af) + if (aid != nexthop->aid) return; nh = nexthop_get(nexthop); @@ -1233,17 +1234,17 @@ nexthop_compare(struct nexthop *na, struct nexthop *nb) a = &na->exit_nexthop; b = &nb->exit_nexthop; - if (a->af != b->af) - return (a->af - b->af); + if (a->aid != b->aid) + return (a->aid - b->aid); - switch (a->af) { - case AF_INET: + switch (a->aid) { + case AID_INET: if (ntohl(a->v4.s_addr) > ntohl(b->v4.s_addr)) return (1); if (ntohl(a->v4.s_addr) < ntohl(b->v4.s_addr)) return (-1); return (0); - case AF_INET6: + case AID_INET6: return (memcmp(&a->v6, &b->v6, sizeof(struct in6_addr))); default: fatalx("nexthop_cmp: unknown af"); @@ -1269,13 +1270,13 @@ nexthop_hash(struct bgpd_addr *nexthop) { u_int32_t h = 0; - switch (nexthop->af) { - case AF_INET: + switch (nexthop->aid) { + case AID_INET: h = (AF_INET ^ ntohl(nexthop->v4.s_addr) ^ ntohl(nexthop->v4.s_addr) >> 13) & nexthoptable.nexthop_hashmask; break; - case AF_INET6: + case AID_INET6: h = hash32_buf(nexthop->v6.s6_addr, sizeof(struct in6_addr), HASHINIT) & nexthoptable.nexthop_hashmask; break; diff --git a/usr.sbin/bgpd/rde_update.c b/usr.sbin/bgpd/rde_update.c index 49ef1e52322..c4f797b7833 100644 --- a/usr.sbin/bgpd/rde_update.c +++ b/usr.sbin/bgpd/rde_update.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_update.c,v 1.70 2009/10/30 15:11:00 claudio Exp $ */ +/* $OpenBSD: rde_update.c,v 1.71 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2004 Claudio Jeker <claudio@openbsd.org> @@ -27,9 +27,9 @@ in_addr_t up_get_nexthop(struct rde_peer *, struct rde_aspath *); int up_generate_mp_reach(struct rde_peer *, struct update_attr *, - struct rde_aspath *, sa_family_t); + struct rde_aspath *, u_int8_t); int up_generate_attr(struct rde_peer *, struct update_attr *, - struct rde_aspath *, sa_family_t); + struct rde_aspath *, u_int8_t); /* update stuff. */ struct update_prefix { @@ -120,19 +120,19 @@ up_prefix_cmp(struct update_prefix *a, struct update_prefix *b) { int i; - if (a->prefix.af < b->prefix.af) + if (a->prefix.aid < b->prefix.aid) return (-1); - if (a->prefix.af > b->prefix.af) + if (a->prefix.aid > b->prefix.aid) return (1); - switch (a->prefix.af) { - case AF_INET: + switch (a->prefix.aid) { + case AID_INET: if (ntohl(a->prefix.v4.s_addr) < ntohl(b->prefix.v4.s_addr)) return (-1); if (ntohl(a->prefix.v4.s_addr) > ntohl(b->prefix.v4.s_addr)) return (1); break; - case AF_INET6: + case AID_INET6: i = memcmp(&a->prefix.v6, &b->prefix.v6, sizeof(struct in6_addr)); if (i > 0) @@ -174,12 +174,12 @@ up_add(struct rde_peer *peer, struct update_prefix *p, struct update_attr *a) struct uplist_attr *upl = NULL; struct uplist_prefix *wdl = NULL; - switch (p->prefix.af) { - case AF_INET: + switch (p->prefix.aid) { + case AID_INET: upl = &peer->updates; wdl = &peer->withdraws; break; - case AF_INET6: + case AID_INET6: upl = &peer->updates6; wdl = &peer->withdraws6; break; @@ -276,13 +276,14 @@ up_test_update(struct rde_peer *peer, struct prefix *p) fatalx("try to send out a looped path"); pt_getaddr(p->prefix, &addr); - switch (addr.af) { - case AF_INET: + switch (addr.aid) { + case AID_INET: + /* XXX is this correct? */ if (peer->capa_announced.mp_v4 == SAFI_NONE && peer->capa_received.mp_v6 != SAFI_NONE) return (-1); break; - case AF_INET6: + case AID_INET6: if (peer->capa_announced.mp_v6 == SAFI_NONE) return (-1); break; @@ -364,7 +365,7 @@ up_generate(struct rde_peer *peer, struct rde_aspath *asp, if (ua == NULL) fatal("up_generate"); - if (up_generate_attr(peer, ua, asp, addr->af) == -1) { + if (up_generate_attr(peer, ua, asp, addr->aid) == -1) { log_warnx("generation of bgp path attributes failed"); free(ua); return (-1); @@ -473,7 +474,7 @@ up_generate_default(struct filter_head *rules, struct rde_peer *peer, /* filter as usual */ bzero(&addr, sizeof(addr)); - addr.af = af; + af2aid(af, 0, &addr.aid); if (rde_filter(peer->ribid, &fasp, rules, peer, asp, &addr, 0, NULL, DIR_OUT) == ACTION_DENY) { @@ -553,12 +554,12 @@ up_get_nexthop(struct rde_peer *peer, struct rde_aspath *a) int up_generate_mp_reach(struct rde_peer *peer, struct update_attr *upa, - struct rde_aspath *a, sa_family_t af) + struct rde_aspath *a, u_int8_t aid) { u_int16_t tmp; - switch (af) { - case AF_INET6: + switch (aid) { + case AID_INET6: upa->mpattr_len = 21; /* AFI + SAFI + NH LEN + NH + Reserved */ upa->mpattr = malloc(upa->mpattr_len); if (upa->mpattr == NULL) @@ -573,7 +574,7 @@ up_generate_mp_reach(struct rde_peer *peer, struct update_attr *upa, if (peer->conf.ebgp == 0) { /* ibgp */ if (a->nexthop == NULL || - (a->nexthop->exit_nexthop.af == AF_INET6 && + (a->nexthop->exit_nexthop.aid == AID_INET6 && memcmp(&a->nexthop->exit_nexthop.v6, &peer->remote_addr.v6, sizeof(struct in6_addr)))) memcpy(&upa->mpattr[4], &peer->local_v6_addr.v6, @@ -613,7 +614,7 @@ up_generate_mp_reach(struct rde_peer *peer, struct update_attr *upa, int up_generate_attr(struct rde_peer *peer, struct update_attr *upa, - struct rde_aspath *a, sa_family_t af) + struct rde_aspath *a, u_int8_t aid) { struct attr *oa, *newaggr = NULL; u_char *pdata; @@ -645,8 +646,8 @@ up_generate_attr(struct rde_peer *peer, struct update_attr *upa, wlen += r; len -= r; free(pdata); - switch (af) { - case AF_INET: + switch (aid) { + case AID_INET: nexthop = up_get_nexthop(peer, a); if ((r = attr_write(up_attr_buf + wlen, len, ATTR_WELL_KNOWN, ATTR_NEXTHOP, &nexthop, 4)) == -1) @@ -1052,4 +1053,3 @@ up_dump_mp_reach(u_char *buf, u_int16_t *len, struct rde_peer *peer) *len = datalen + 4; return (buf + wpos); } - diff --git a/usr.sbin/bgpd/session.c b/usr.sbin/bgpd/session.c index 3ed0195cfcf..f1f2b7faef9 100644 --- a/usr.sbin/bgpd/session.c +++ b/usr.sbin/bgpd/session.c @@ -1,4 +1,4 @@ -/* $OpenBSD: session.c,v 1.299 2009/10/26 09:27:58 claudio Exp $ */ +/* $OpenBSD: session.c,v 1.300 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2003, 2004, 2005 Henning Brauer <henning@openbsd.org> @@ -87,11 +87,10 @@ void session_up(struct peer *); void session_down(struct peer *); void session_demote(struct peer *, int); -int la_cmp(struct listen_addr *, struct listen_addr *); -struct peer *getpeerbyip(struct sockaddr *); -int session_match_mask(struct peer *, struct sockaddr *); -struct peer *getpeerbyid(u_int32_t); -static struct sockaddr *addr2sa(struct bgpd_addr *, u_int16_t); +int la_cmp(struct listen_addr *, struct listen_addr *); +struct peer *getpeerbyip(struct sockaddr *); +int session_match_mask(struct peer *, struct bgpd_addr *); +struct peer *getpeerbyid(u_int32_t); struct bgpd_config *conf, *nconf = NULL; struct bgpd_sysdep sysdep; @@ -1073,7 +1072,7 @@ session_connect(struct peer *peer) if (peer->fd != -1) return (-1); - if ((peer->fd = socket(peer->conf.remote_addr.af, SOCK_STREAM, + if ((peer->fd = socket(aid2af(peer->conf.remote_addr.aid), SOCK_STREAM, IPPROTO_TCP)) == -1) { log_peer_warn(&peer->conf, "session_connect socket"); bgp_fsm(peer, EVNT_CON_OPENFAIL); @@ -1104,8 +1103,7 @@ session_connect(struct peer *peer) peer->wbuf.fd = peer->fd; /* if update source is set we need to bind() */ - if (peer->conf.local_addr.af) { - sa = addr2sa(&peer->conf.local_addr, 0); + if ((sa = addr2sa(&peer->conf.local_addr, 0)) != NULL) { if (bind(peer->fd, sa, sa->sa_len) == -1) { log_peer_warn(&peer->conf, "session_connect bind"); bgp_fsm(peer, EVNT_CON_OPENFAIL); @@ -1143,42 +1141,50 @@ session_setup_socket(struct peer *p) int nodelay = 1; int bsize; - if (p->conf.ebgp && p->conf.remote_addr.af == AF_INET) { - /* set TTL to foreign router's distance - 1=direct n=multihop - with ttlsec, we always use 255 */ - if (p->conf.ttlsec) { - ttl = 256 - p->conf.distance; - if (setsockopt(p->fd, IPPROTO_IP, IP_MINTTL, &ttl, + switch (p->conf.remote_addr.aid) { + case AID_INET: + /* set precedence, see RFC 1771 appendix 5 */ + if (setsockopt(p->fd, IPPROTO_IP, IP_TOS, &pre, sizeof(pre)) == + -1) { + log_peer_warn(&p->conf, + "session_setup_socket setsockopt TOS"); + return (-1); + } + + if (p->conf.ebgp) { + /* set TTL to foreign router's distance + 1=direct n=multihop with ttlsec, we always use 255 */ + if (p->conf.ttlsec) { + ttl = 256 - p->conf.distance; + if (setsockopt(p->fd, IPPROTO_IP, IP_MINTTL, + &ttl, sizeof(ttl)) == -1) { + log_peer_warn(&p->conf, + "session_setup_socket: " + "setsockopt MINTTL"); + return (-1); + } + ttl = 255; + } + + if (setsockopt(p->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) == -1) { log_peer_warn(&p->conf, - "session_setup_socket setsockopt MINTTL"); + "session_setup_socket setsockopt TTL"); return (-1); } - ttl = 255; } - - if (setsockopt(p->fd, IPPROTO_IP, IP_TTL, &ttl, - sizeof(ttl)) == -1) { - log_peer_warn(&p->conf, - "session_setup_socket setsockopt TTL"); - return (-1); - } - } - - if (p->conf.ebgp && p->conf.remote_addr.af == AF_INET6) - /* set hoplimit to foreign router's distance */ - if (setsockopt(p->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, - sizeof(ttl)) == -1) { - log_peer_warn(&p->conf, - "session_setup_socket setsockopt hoplimit"); - return (-1); + break; + case AID_INET6: + if (p->conf.ebgp) { + /* set hoplimit to foreign router's distance */ + if (setsockopt(p->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, + &ttl, sizeof(ttl)) == -1) { + log_peer_warn(&p->conf, + "session_setup_socket setsockopt hoplimit"); + return (-1); + } } - - /* if ttlsec is in use, set minttl */ - if (p->conf.ttlsec) { - ttl = 256 - p->conf.distance; - setsockopt(p->fd, IPPROTO_IP, IP_MINTTL, &ttl, sizeof(ttl)); - + break; } /* set TCP_NODELAY */ @@ -1189,14 +1195,6 @@ session_setup_socket(struct peer *p) return (-1); } - /* set precedence, see RFC 1771 appendix 5 */ - if (p->conf.remote_addr.af == AF_INET && - setsockopt(p->fd, IPPROTO_IP, IP_TOS, &pre, sizeof(pre)) == -1) { - log_peer_warn(&p->conf, - "session_setup_socket setsockopt TOS"); - return (-1); - } - /* only increase bufsize (and thus window) if md5 or ipsec is in use */ if (p->conf.auth.method != AUTH_NONE) { /* try to increase bufsize. no biggie if it fails */ @@ -2619,29 +2617,23 @@ getpeerbydesc(const char *descr) struct peer * getpeerbyip(struct sockaddr *ip) { + struct bgpd_addr addr; struct peer *p, *newpeer, *loose = NULL; u_int32_t id; + sa2addr(ip, &addr); + /* we might want a more effective way to find peers by IP */ for (p = peers; p != NULL; p = p->next) if (!p->conf.template && - p->conf.remote_addr.af == ip->sa_family) { - if (p->conf.remote_addr.af == AF_INET && - p->conf.remote_addr.v4.s_addr == - ((struct sockaddr_in *)ip)->sin_addr.s_addr) - return (p); - if (p->conf.remote_addr.af == AF_INET6 && - !bcmp(&p->conf.remote_addr.v6, - &((struct sockaddr_in6 *)ip)->sin6_addr, - sizeof(p->conf.remote_addr.v6))) - return (p); - } + !memcmp(&addr, &p->conf.remote_addr, sizeof(addr))) + return (p); /* try template matching */ for (p = peers; p != NULL; p = p->next) if (p->conf.template && - p->conf.remote_addr.af == ip->sa_family && - session_match_mask(p, ip)) + p->conf.remote_addr.aid == addr.aid && + session_match_mask(p, &addr)) if (loose == NULL || loose->conf.remote_masklen < p->conf.remote_masklen) loose = p; @@ -2660,16 +2652,14 @@ getpeerbyip(struct sockaddr *ip) break; } } - if (newpeer->conf.remote_addr.af == AF_INET) { - newpeer->conf.remote_addr.v4.s_addr = - ((struct sockaddr_in *)ip)->sin_addr.s_addr; + sa2addr(ip, &newpeer->conf.remote_addr); + switch (ip->sa_family) { + case AF_INET: newpeer->conf.remote_masklen = 32; - } - if (newpeer->conf.remote_addr.af == AF_INET6) { - memcpy(&newpeer->conf.remote_addr.v6, - &((struct sockaddr_in6 *)ip)->sin6_addr, - sizeof(newpeer->conf.remote_addr.v6)); + break; + case AF_INET6: newpeer->conf.remote_masklen = 128; + break; } newpeer->conf.template = 0; newpeer->conf.cloned = 1; @@ -2687,40 +2677,24 @@ getpeerbyip(struct sockaddr *ip) } int -session_match_mask(struct peer *p, struct sockaddr *ip) +session_match_mask(struct peer *p, struct bgpd_addr *a) { - int i; in_addr_t v4mask; - struct in6_addr *in; - struct in6_addr mask; + struct in6_addr masked; - if (p->conf.remote_addr.af == AF_INET) { + switch (p->conf.remote_addr.aid) { + case AID_INET: v4mask = htonl(prefixlen2mask(p->conf.remote_masklen)); - if (p->conf.remote_addr.v4.s_addr == - ((((struct sockaddr_in *)ip)->sin_addr.s_addr) & v4mask)) + if (p->conf.remote_addr.v4.s_addr == (a->v4.s_addr & v4mask)) return (1); - else - return (0); - } - - if (p->conf.remote_addr.af == AF_INET6) { - bzero(&mask, sizeof(mask)); - for (i = 0; i < p->conf.remote_masklen / 8; i++) - mask.s6_addr[i] = 0xff; - i = p->conf.remote_masklen % 8; - if (i) - mask.s6_addr[p->conf.remote_masklen / 8] = 0xff00 >> i; - - in = &((struct sockaddr_in6 *)ip)->sin6_addr; - - for (i = 0; i < 16; i++) - if ((in->s6_addr[i] & mask.s6_addr[i]) != - p->conf.remote_addr.addr8[i]) - return (0); + return (0); + case AID_INET6: + inet6applymask(&masked, &a->v6, p->conf.remote_masklen); - return (1); + if (!memcmp(&masked, &p->conf.remote_addr.v6, sizeof(masked))) + return (1); + return (0); } - return (0); } @@ -2755,30 +2729,8 @@ session_up(struct peer *p) &p->conf, sizeof(p->conf)) == -1) fatalx("imsg_compose error"); - switch (p->sa_local.ss_family) { - case AF_INET: - sup.local_addr.af = AF_INET; - memcpy(&sup.local_addr.v4, - &((struct sockaddr_in *)&p->sa_local)->sin_addr, - sizeof(sup.local_addr.v4)); - sup.remote_addr.af = AF_INET; - memcpy(&sup.remote_addr.v4, - &((struct sockaddr_in *)&p->sa_remote)->sin_addr, - sizeof(sup.remote_addr.v4)); - break; - case AF_INET6: - sup.local_addr.af = AF_INET6; - memcpy(&sup.local_addr.v6, - &((struct sockaddr_in6 *)&p->sa_local)->sin6_addr, - sizeof(sup.local_addr.v6)); - sup.remote_addr.af = AF_INET6; - memcpy(&sup.remote_addr.v6, - &((struct sockaddr_in6 *)&p->sa_remote)->sin6_addr, - sizeof(sup.remote_addr.v6)); - break; - default: - fatalx("session_up: unsupported address family"); - } + sa2addr((struct sockaddr *)&p->sa_local, &sup.local_addr); + sa2addr((struct sockaddr *)&p->sa_remote, &sup.remote_addr); sup.remote_bgpid = p->remote_bgpid; sup.short_as = p->short_as; @@ -2802,34 +2754,6 @@ imsg_compose_rde(int type, pid_t pid, void *data, u_int16_t datalen) return (imsg_compose(ibuf_rde, type, 0, pid, -1, data, datalen)); } -static struct sockaddr * -addr2sa(struct bgpd_addr *addr, u_int16_t port) -{ - static struct sockaddr_storage ss; - struct sockaddr_in *sa_in = (struct sockaddr_in *)&ss; - struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)&ss; - - bzero(&ss, sizeof(ss)); - switch (addr->af) { - case AF_INET: - sa_in->sin_family = AF_INET; - sa_in->sin_len = sizeof(struct sockaddr_in); - sa_in->sin_addr.s_addr = addr->v4.s_addr; - sa_in->sin_port = htons(port); - break; - case AF_INET6: - sa_in6->sin6_family = AF_INET6; - sa_in6->sin6_len = sizeof(struct sockaddr_in6); - memcpy(&sa_in6->sin6_addr, &addr->v6, - sizeof(sa_in6->sin6_addr)); - sa_in6->sin6_port = htons(port); - sa_in6->sin6_scope_id = addr->scope_id; - break; - } - - return ((struct sockaddr *)&ss); -} - void session_demote(struct peer *p, int level) { diff --git a/usr.sbin/bgpd/util.c b/usr.sbin/bgpd/util.c index b361b8c3a7c..3db16447dbf 100644 --- a/usr.sbin/bgpd/util.c +++ b/usr.sbin/bgpd/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.6 2009/06/12 16:42:53 claudio Exp $ */ +/* $OpenBSD: util.c,v 1.7 2009/12/01 14:28:05 claudio Exp $ */ /* * Copyright (c) 2006 Claudio Jeker <claudio@openbsd.org> @@ -33,7 +33,7 @@ log_addr(const struct bgpd_addr *addr) { static char buf[48]; - if (inet_ntop(addr->af, &addr->ba, buf, sizeof(buf)) == NULL) + if (inet_ntop(aid2af(addr->aid), &addr->ba, buf, sizeof(buf)) == NULL) return ("?"); else return (buf); @@ -276,3 +276,107 @@ inet6applymask(struct in6_addr *dest, const struct in6_addr *src, int prefixlen) for (i = 0; i < 16; i++) dest->s6_addr[i] = src->s6_addr[i] & mask.s6_addr[i]; } + +/* address family translation functions */ +const struct aid aid_vals[AID_MAX] = AID_VALS; + +int +aid2afi(u_int8_t aid, u_int16_t *afi, u_int8_t *safi) +{ + if (aid < AID_MAX) { + *afi = aid_vals[aid].afi; + *safi = aid_vals[aid].safi; + return (0); + } + return (-1); +} + +int +afi2aid(u_int16_t afi, u_int8_t safi, u_int8_t *aid) +{ + u_int8_t i; + + for (i = 0; i < AID_MAX; i++) + if (aid_vals[i].afi == afi && aid_vals[i].safi == safi) { + *aid = i; + return (0); + } + + return (-1); +} + +sa_family_t +aid2af(u_int8_t aid) +{ + if (aid < AID_MAX) + return (aid_vals[aid].af); + return (AF_UNSPEC); +} + +int +af2aid(sa_family_t af, u_int8_t safi, u_int8_t *aid) +{ + u_int8_t i; + + if (safi == 0) /* default to unicast subclass */ + safi = SAFI_UNICAST; + + for (i = 0; i < AID_MAX; i++) + if (aid_vals[i].af == af && aid_vals[i].safi == safi) { + *aid = i; + return (0); + } + + return (-1); +} + +struct sockaddr * +addr2sa(struct bgpd_addr *addr, u_int16_t port) +{ + static struct sockaddr_storage ss; + struct sockaddr_in *sa_in = (struct sockaddr_in *)&ss; + struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)&ss; + + if (addr->aid == AID_UNSPEC) + return (NULL); + + bzero(&ss, sizeof(ss)); + switch (addr->aid) { + case AID_INET: + sa_in->sin_family = AF_INET; + sa_in->sin_len = sizeof(struct sockaddr_in); + sa_in->sin_addr.s_addr = addr->v4.s_addr; + sa_in->sin_port = htons(port); + break; + case AID_INET6: + sa_in6->sin6_family = AF_INET6; + sa_in6->sin6_len = sizeof(struct sockaddr_in6); + memcpy(&sa_in6->sin6_addr, &addr->v6, + sizeof(sa_in6->sin6_addr)); + sa_in6->sin6_port = htons(port); + sa_in6->sin6_scope_id = addr->scope_id; + break; + } + + return ((struct sockaddr *)&ss); +} + +void +sa2addr(struct sockaddr *sa, struct bgpd_addr *addr) +{ + struct sockaddr_in *sa_in = (struct sockaddr_in *)sa; + struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)sa; + + bzero(addr, sizeof(*addr)); + switch (sa->sa_family) { + case AF_INET: + addr->aid = AID_INET; + memcpy(&addr->v4, &sa_in->sin_addr, sizeof(addr->v4)); + break; + case AF_INET6: + addr->aid = AID_INET6; + memcpy(&addr->v6, &sa_in6->sin6_addr, sizeof(addr->v6)); + addr->scope_id = sa_in6->sin6_scope_id; /* I hate v6 */ + break; + } +} |