summaryrefslogtreecommitdiff
path: root/sys/net
diff options
context:
space:
mode:
authordenis <denis@cvs.openbsd.org>2020-11-07 09:51:41 +0000
committerdenis <denis@cvs.openbsd.org>2020-11-07 09:51:41 +0000
commitf8d1ac4bca340e6afd3ff9ae454d9d2c703857c5 (patch)
tree24aee78e26365e871b7e433e12e0d38096b221ee /sys/net
parent9c38e7ef4d59da85e7f4c72891712c88050ce9a9 (diff)
Rework source IP address setting.
- Move most of the processing out of rtable.c (reasonnable tb@, ok bluhm@) - Remove memory allocation, store pointer to existing ifaddr - Fix tunnel interface handling looks fine mpi@
Diffstat (limited to 'sys/net')
-rw-r--r--sys/net/rtable.c48
-rw-r--r--sys/net/rtable.h4
-rw-r--r--sys/net/rtsock.c47
3 files changed, 52 insertions, 47 deletions
diff --git a/sys/net/rtable.c b/sys/net/rtable.c
index dd8f33af576..9a9f2a65c31 100644
--- a/sys/net/rtable.c
+++ b/sys/net/rtable.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rtable.c,v 1.71 2020/11/05 10:46:13 denis Exp $ */
+/* $OpenBSD: rtable.c,v 1.72 2020/11/07 09:51:40 denis Exp $ */
/*
* Copyright (c) 2014-2016 Martin Pieuchot
@@ -31,10 +31,6 @@
#include <net/rtable.h>
#include <net/route.h>
-#include <net/if.h>
-#include <net/if_dl.h>
-#include <net/if_var.h>
-
/*
* Structures used by rtable_get() to retrieve the corresponding
* routing table for a given pair of ``af'' and ``rtableid''.
@@ -370,46 +366,14 @@ rtable_alloc(unsigned int rtableid, unsigned int alen, unsigned int off)
}
int
-rtable_setsource(unsigned int rtableid, struct sockaddr *src)
+rtable_setsource(unsigned int rtableid, int af, struct sockaddr *src)
{
struct art_root *ar;
- if ((ar = rtable_get(rtableid, src->sa_family)) == NULL)
+ if ((ar = rtable_get(rtableid, af)) == NULL)
return (EAFNOSUPPORT);
- /*
- * Check if source address is assigned to an interface in the
- * same rdomain
- */
- if (ifa_ifwithaddr(src, rtableid) == NULL) {
- /*
- * If source address is 0.0.0.0 or ::
- * use automatic source selection
- */
- switch(src->sa_family) {
- case AF_INET:
- if(satosin(src)->sin_addr.s_addr == INADDR_ANY)
- return (EINVAL);
- break;
- case AF_INET6:
- if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr))
- return (EINVAL);
- break;
- default:
- return (EAFNOSUPPORT);
- }
- src = NULL;
- }
-
- if (ar->source) {
- free(ar->source, M_IFADDR, ar->source->sa_len);
- ar->source = NULL;
- }
-
- if (src) {
- ar->source = malloc(src->sa_len, M_IFADDR, M_WAITOK|M_ZERO);
- memcpy(ar->source, src, src->sa_len);
- }
+ ar->source = src;
return (0);
}
@@ -434,9 +398,7 @@ rtable_clearsource(unsigned int rtableid, struct sockaddr *src)
addr = rtable_getsource(rtableid, src->sa_family);
if (addr && (addr->sa_len == src->sa_len)) {
if (memcmp(src, addr, addr->sa_len) == 0) {
- memset(addr->sa_data, 0, addr->sa_len-
- sizeof(addr->sa_len)-sizeof(addr->sa_family));
- rtable_setsource(rtableid, addr);
+ rtable_setsource(rtableid, src->sa_family, NULL);
}
}
}
diff --git a/sys/net/rtable.h b/sys/net/rtable.h
index 0686d0c75a2..e9dc137918e 100644
--- a/sys/net/rtable.h
+++ b/sys/net/rtable.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: rtable.h,v 1.25 2020/10/29 21:15:27 denis Exp $ */
+/* $OpenBSD: rtable.h,v 1.26 2020/11/07 09:51:40 denis Exp $ */
/*
* Copyright (c) 2014-2016 Martin Pieuchot
@@ -39,7 +39,7 @@ unsigned int rtable_l2(unsigned int);
unsigned int rtable_loindex(unsigned int);
void rtable_l2set(unsigned int, unsigned int, unsigned int);
-int rtable_setsource(unsigned int, struct sockaddr *);
+int rtable_setsource(unsigned int, int, struct sockaddr *);
struct sockaddr *rtable_getsource(unsigned int, int);
void rtable_clearsource(unsigned int, struct sockaddr *);
struct rtentry *rtable_lookup(unsigned int, struct sockaddr *,
diff --git a/sys/net/rtsock.c b/sys/net/rtsock.c
index bed2efff028..65214a9dd1d 100644
--- a/sys/net/rtsock.c
+++ b/sys/net/rtsock.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rtsock.c,v 1.303 2020/10/29 21:15:27 denis Exp $ */
+/* $OpenBSD: rtsock.c,v 1.304 2020/11/07 09:51:40 denis Exp $ */
/* $NetBSD: rtsock.c,v 1.18 1996/03/29 00:32:10 cgd Exp $ */
/*
@@ -138,6 +138,8 @@ int sysctl_iflist(int, struct walkarg *);
int sysctl_ifnames(struct walkarg *);
int sysctl_rtable_rtstat(void *, size_t *, void *);
+int rt_setsource(unsigned int, struct sockaddr *);
+
/*
* Locks used to protect struct members
* I immutable after creation
@@ -860,7 +862,7 @@ route_output(struct mbuf *m, struct socket *so, struct sockaddr *dstaddr,
goto fail;
}
if ((error =
- rtable_setsource(tableid, info.rti_info[RTAX_IFA])) != 0)
+ rt_setsource(tableid, info.rti_info[RTAX_IFA])) != 0)
goto fail;
} else {
error = rtm_output(rtm, &rt, &info, prio, tableid);
@@ -2076,9 +2078,13 @@ sysctl_source(int af, u_int tableid, struct walkarg *w)
case AF_INET:
size = sizeof(struct sockaddr_in);
break;
+#ifdef INET6
case AF_INET6:
size = sizeof(struct sockaddr_in6);
break;
+#endif
+ default:
+ return (0);
}
w->w_needed += size;
if (w->w_where && w->w_needed <= 0) {
@@ -2307,6 +2313,43 @@ rtm_validate_proposal(struct rt_addrinfo *info)
return 0;
}
+int
+rt_setsource(unsigned int rtableid, struct sockaddr *src)
+{
+ struct ifaddr *ifa;
+ /*
+ * If source address is 0.0.0.0 or ::
+ * use automatic source selection
+ */
+ switch(src->sa_family) {
+ case AF_INET:
+ if(satosin(src)->sin_addr.s_addr == INADDR_ANY) {
+ rtable_setsource(rtableid, AF_INET, NULL);
+ return (0);
+ }
+ break;
+#ifdef INET6
+ case AF_INET6:
+ if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr)) {
+ rtable_setsource(rtableid, AF_INET6, NULL);
+ return (0);
+ }
+ break;
+#endif
+ default:
+ return (EAFNOSUPPORT);
+ }
+
+ /*
+ * Check if source address is assigned to an interface in the
+ * same rdomain
+ */
+ if ((ifa = ifa_ifwithaddr(src, rtableid)) == NULL)
+ return (EINVAL);
+
+ return (rtable_setsource(rtableid, src->sa_family, ifa->ifa_addr));
+}
+
/*
* Definitions of protocols supported in the ROUTE domain.
*/