summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorClaudio Jeker <claudio@cvs.openbsd.org>2019-02-27 04:31:57 +0000
committerClaudio Jeker <claudio@cvs.openbsd.org>2019-02-27 04:31:57 +0000
commitb0016370aedbf1688b11a355bc911f738b5e3fb5 (patch)
treeb56b2c75aa5025c220911255d58a6cde0ce0e577 /usr.sbin
parentac4bacbe5d31e50d7482d22da6f268ab71b28439 (diff)
Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies some code. sa2addr now takes an optional pointer to return the port of the sockaddr. OK benno@
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/bgpd/bgpd.h4
-rw-r--r--usr.sbin/bgpd/config.c4
-rw-r--r--usr.sbin/bgpd/mrt.c32
-rw-r--r--usr.sbin/bgpd/rde.c6
-rw-r--r--usr.sbin/bgpd/session.c25
-rw-r--r--usr.sbin/bgpd/session.h8
-rw-r--r--usr.sbin/bgpd/util.c8
7 files changed, 44 insertions, 43 deletions
diff --git a/usr.sbin/bgpd/bgpd.h b/usr.sbin/bgpd/bgpd.h
index d3a868d0ccf..b26f0c62248 100644
--- a/usr.sbin/bgpd/bgpd.h
+++ b/usr.sbin/bgpd/bgpd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: bgpd.h,v 1.375 2019/02/26 10:49:15 claudio Exp $ */
+/* $OpenBSD: bgpd.h,v 1.376 2019/02/27 04:31:56 claudio Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -1303,7 +1303,7 @@ 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, socklen_t *);
-void sa2addr(struct sockaddr *, struct bgpd_addr *);
+void sa2addr(struct sockaddr *, struct bgpd_addr *, u_int16_t *);
const char * get_baudrate(unsigned long long, char *);
static const char * const log_procnames[] = {
diff --git a/usr.sbin/bgpd/config.c b/usr.sbin/bgpd/config.c
index 0b3783ae5a0..25deaac272e 100644
--- a/usr.sbin/bgpd/config.c
+++ b/usr.sbin/bgpd/config.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: config.c,v 1.83 2019/02/18 09:58:19 claudio Exp $ */
+/* $OpenBSD: config.c,v 1.84 2019/02/27 04:31:56 claudio Exp $ */
/*
* Copyright (c) 2003, 2004, 2005 Henning Brauer <henning@openbsd.org>
@@ -367,7 +367,7 @@ host_ip(const char *s, struct bgpd_addr *h, u_int8_t *len)
hints.ai_flags = AI_NUMERICHOST;
if (getaddrinfo(s, NULL, &hints, &res) == 0) {
*len = res->ai_family == AF_INET6 ? 128 : 32;
- sa2addr(res->ai_addr, h);
+ sa2addr(res->ai_addr, h, NULL);
freeaddrinfo(res);
} else { /* ie. for 10/8 parsing */
if ((bits = inet_net_pton(AF_INET, s, &h->v4, sizeof(h->v4))) == -1)
diff --git a/usr.sbin/bgpd/mrt.c b/usr.sbin/bgpd/mrt.c
index 94834b0fac3..ae7018eb8ef 100644
--- a/usr.sbin/bgpd/mrt.c
+++ b/usr.sbin/bgpd/mrt.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: mrt.c,v 1.92 2019/02/14 13:13:33 claudio Exp $ */
+/* $OpenBSD: mrt.c,v 1.93 2019/02/27 04:31:56 claudio Exp $ */
/*
* Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org>
@@ -712,15 +712,15 @@ mrt_dump_hdr_se(struct ibuf ** bp, struct peer *peer, u_int16_t type,
DUMP_SHORT(*bp, type);
DUMP_SHORT(*bp, subtype);
- switch (peer->sa_local.ss_family) {
- case AF_INET:
+ switch (peer->local.aid) {
+ case AID_INET:
if (subtype == BGP4MP_STATE_CHANGE_AS4 ||
subtype == BGP4MP_MESSAGE_AS4)
len += MRT_BGP4MP_ET_AS4_IPv4_HEADER_SIZE;
else
len += MRT_BGP4MP_ET_IPv4_HEADER_SIZE;
break;
- case AF_INET6:
+ case AID_INET6:
if (subtype == BGP4MP_STATE_CHANGE_AS4 ||
subtype == BGP4MP_MESSAGE_AS4)
len += MRT_BGP4MP_ET_AS4_IPv6_HEADER_SIZE;
@@ -755,36 +755,30 @@ mrt_dump_hdr_se(struct ibuf ** bp, struct peer *peer, u_int16_t type,
DUMP_SHORT(*bp, /* ifindex */ 0);
- switch (peer->sa_local.ss_family) {
- case AF_INET:
+ switch (peer->local.aid) {
+ case AID_INET:
DUMP_SHORT(*bp, AFI_IPv4);
if (!swap)
- DUMP_NLONG(*bp, ((struct sockaddr_in *)
- &peer->sa_local)->sin_addr.s_addr);
- DUMP_NLONG(*bp,
- ((struct sockaddr_in *)&peer->sa_remote)->sin_addr.s_addr);
+ DUMP_NLONG(*bp, peer->local.v4.s_addr);
+ DUMP_NLONG(*bp, peer->remote.v4.s_addr);
if (swap)
- DUMP_NLONG(*bp, ((struct sockaddr_in *)
- &peer->sa_local)->sin_addr.s_addr);
+ DUMP_NLONG(*bp, peer->local.v4.s_addr);
break;
- case AF_INET6:
+ case AID_INET6:
DUMP_SHORT(*bp, AFI_IPv6);
if (!swap)
- if (ibuf_add(*bp, &((struct sockaddr_in6 *)
- &peer->sa_local)->sin6_addr,
+ if (ibuf_add(*bp, &peer->local.v6,
sizeof(struct in6_addr)) == -1) {
log_warn("mrt_dump_hdr_se: ibuf_add error");
goto fail;
}
- if (ibuf_add(*bp,
- &((struct sockaddr_in6 *)&peer->sa_remote)->sin6_addr,
+ if (ibuf_add(*bp, &peer->remote.v6,
sizeof(struct in6_addr)) == -1) {
log_warn("mrt_dump_hdr_se: ibuf_add error");
goto fail;
}
if (swap)
- if (ibuf_add(*bp, &((struct sockaddr_in6 *)
- &peer->sa_local)->sin6_addr,
+ if (ibuf_add(*bp, &peer->local.v6,
sizeof(struct in6_addr)) == -1) {
log_warn("mrt_dump_hdr_se: ibuf_add error");
goto fail;
diff --git a/usr.sbin/bgpd/rde.c b/usr.sbin/bgpd/rde.c
index 00ec75364b7..06b64e44f02 100644
--- a/usr.sbin/bgpd/rde.c
+++ b/usr.sbin/bgpd/rde.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rde.c,v 1.463 2019/02/14 10:34:54 claudio Exp $ */
+/* $OpenBSD: rde.c,v 1.464 2019/02/27 04:31:56 claudio Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -3366,7 +3366,7 @@ peer_localaddrs(struct rde_peer *peer, struct bgpd_addr *laddr)
if (ifa->ifa_addr->sa_family ==
match->ifa_addr->sa_family)
ifa = match;
- sa2addr(ifa->ifa_addr, &peer->local_v4_addr);
+ sa2addr(ifa->ifa_addr, &peer->local_v4_addr, NULL);
break;
}
}
@@ -3387,7 +3387,7 @@ peer_localaddrs(struct rde_peer *peer, struct bgpd_addr *laddr)
&((struct sockaddr_in6 *)ifa->
ifa_addr)->sin6_addr))
continue;
- sa2addr(ifa->ifa_addr, &peer->local_v6_addr);
+ sa2addr(ifa->ifa_addr, &peer->local_v6_addr, NULL);
break;
}
}
diff --git a/usr.sbin/bgpd/session.c b/usr.sbin/bgpd/session.c
index c7fe51b7e06..f9e442f5967 100644
--- a/usr.sbin/bgpd/session.c
+++ b/usr.sbin/bgpd/session.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: session.c,v 1.373 2019/02/18 09:43:57 claudio Exp $ */
+/* $OpenBSD: session.c,v 1.374 2019/02/27 04:31:56 claudio Exp $ */
/*
* Copyright (c) 2003, 2004, 2005 Henning Brauer <henning@openbsd.org>
@@ -1263,16 +1263,17 @@ session_setup_socket(struct peer *p)
void
session_tcp_established(struct peer *peer)
{
- socklen_t len;
+ struct sockaddr_storage ss;
+ socklen_t len;
- len = sizeof(peer->sa_local);
- if (getsockname(peer->fd, (struct sockaddr *)&peer->sa_local,
- &len) == -1)
+ len = sizeof(ss);
+ if (getsockname(peer->fd, (struct sockaddr *)&ss, &len) == -1)
log_warn("getsockname");
- len = sizeof(peer->sa_remote);
- if (getpeername(peer->fd, (struct sockaddr *)&peer->sa_remote,
- &len) == -1)
+ sa2addr((struct sockaddr *)&ss, &peer->local, &peer->local_port);
+ len = sizeof(ss);
+ if (getpeername(peer->fd, (struct sockaddr *)&ss, &len) == -1)
log_warn("getpeername");
+ sa2addr((struct sockaddr *)&ss, &peer->remote, &peer->remote_port);
}
void
@@ -3068,7 +3069,7 @@ getpeerbyip(struct sockaddr *ip)
struct peer *p, *newpeer, *loose = NULL;
u_int32_t id;
- sa2addr(ip, &addr);
+ sa2addr(ip, &addr, NULL);
/* we might want a more effective way to find peers by IP */
for (p = peers; p != NULL; p = p->next)
@@ -3150,7 +3151,7 @@ session_template_clone(struct peer *p, struct sockaddr *ip, u_int32_t id,
struct bgpd_addr remote_addr;
if (ip)
- sa2addr(ip, &remote_addr);
+ sa2addr(ip, &remote_addr, NULL);
else
memcpy(&remote_addr, &p->conf.remote_addr, sizeof(remote_addr));
@@ -3226,8 +3227,8 @@ session_up(struct peer *p)
&p->conf, sizeof(p->conf)) == -1)
fatalx("imsg_compose error");
- sa2addr((struct sockaddr *)&p->sa_local, &sup.local_addr);
- sa2addr((struct sockaddr *)&p->sa_remote, &sup.remote_addr);
+ sup.local_addr = p->local;
+ sup.remote_addr = p->remote;
sup.remote_bgpid = p->remote_bgpid;
sup.short_as = p->short_as;
diff --git a/usr.sbin/bgpd/session.h b/usr.sbin/bgpd/session.h
index f414fbf6e49..bac15e885d1 100644
--- a/usr.sbin/bgpd/session.h
+++ b/usr.sbin/bgpd/session.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: session.h,v 1.132 2019/02/19 09:13:23 claudio Exp $ */
+/* $OpenBSD: session.h,v 1.133 2019/02/27 04:31:56 claudio Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -208,8 +208,8 @@ struct peer {
enum auth_method method;
u_int8_t established;
} auth;
- struct sockaddr_storage sa_local;
- struct sockaddr_storage sa_remote;
+ struct bgpd_addr local;
+ struct bgpd_addr remote;
struct peer_timer_head timers;
struct msgbuf wbuf;
struct ibuf_read *rbuf;
@@ -224,6 +224,8 @@ struct peer {
enum session_state prev_state;
u_int16_t short_as;
u_int16_t holdtime;
+ u_int16_t local_port;
+ u_int16_t remote_port;
u_int8_t depend_ok;
u_int8_t demoted;
u_int8_t passive;
diff --git a/usr.sbin/bgpd/util.c b/usr.sbin/bgpd/util.c
index c97c9b7f216..7dc79b12306 100644
--- a/usr.sbin/bgpd/util.c
+++ b/usr.sbin/bgpd/util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.47 2019/02/26 10:49:15 claudio Exp $ */
+/* $OpenBSD: util.c,v 1.48 2019/02/27 04:31:56 claudio Exp $ */
/*
* Copyright (c) 2006 Claudio Jeker <claudio@openbsd.org>
@@ -866,7 +866,7 @@ addr2sa(struct bgpd_addr *addr, u_int16_t port, socklen_t *len)
}
void
-sa2addr(struct sockaddr *sa, struct bgpd_addr *addr)
+sa2addr(struct sockaddr *sa, struct bgpd_addr *addr, u_int16_t *port)
{
struct sockaddr_in *sa_in = (struct sockaddr_in *)sa;
struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)sa;
@@ -876,11 +876,15 @@ sa2addr(struct sockaddr *sa, struct bgpd_addr *addr)
case AF_INET:
addr->aid = AID_INET;
memcpy(&addr->v4, &sa_in->sin_addr, sizeof(addr->v4));
+ if (port)
+ *port = ntohs(sa_in->sin_port);
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 */
+ if (port)
+ *port = ntohs(sa_in6->sin6_port);
break;
}
}