From: tobhe Date: Wed, 23 Jun 2021 12:11:40 +0000 (+0000) Subject: Factor out vroute_addr(). X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=75f6dc0ad1b6a737943ed66c2a8f8456e5095069;p=openbsd Factor out vroute_addr(). ok markus@ --- diff --git a/sbin/iked/iked.h b/sbin/iked/iked.h index 408472443b7..e9aa4541c31 100644 --- a/sbin/iked/iked.h +++ b/sbin/iked/iked.h @@ -1,4 +1,4 @@ -/* $OpenBSD: iked.h,v 1.191 2021/05/13 15:20:48 tobhe Exp $ */ +/* $OpenBSD: iked.h,v 1.192 2021/06/23 12:11:40 tobhe Exp $ */ /* * Copyright (c) 2019 Tobias Heider @@ -972,6 +972,7 @@ ssize_t dsa_verify_final(struct iked_dsa *, void *, size_t); /* vroute.c */ void vroute_init(struct iked *); +int vroute_setaddr(struct iked *, int, struct sockaddr *, int, unsigned int); void vroute_cleanup(struct iked *); int vroute_getaddr(struct iked *, struct imsg *); int vroute_setaddroute(struct iked *, uint8_t, struct sockaddr *, diff --git a/sbin/iked/policy.c b/sbin/iked/policy.c index 0cf3e681a9b..57077c46461 100644 --- a/sbin/iked/policy.c +++ b/sbin/iked/policy.c @@ -1,4 +1,4 @@ -/* $OpenBSD: policy.c,v 1.81 2021/04/09 09:15:04 tobhe Exp $ */ +/* $OpenBSD: policy.c,v 1.82 2021/06/23 12:11:40 tobhe Exp $ */ /* * Copyright (c) 2020-2021 Tobias Heider @@ -674,13 +674,7 @@ int sa_configure_iface(struct iked *env, struct iked_sa *sa, int add) { struct iked_flow *saflow; - struct iovec iov[4]; - int iovcnt; struct sockaddr *caddr; - struct sockaddr_in *addr; - struct sockaddr_in mask; - struct sockaddr_in6 *addr6; - struct sockaddr_in6 mask6; int rdomain; if (sa->sa_policy == NULL || sa->sa_policy->pol_iface == 0) @@ -690,55 +684,15 @@ sa_configure_iface(struct iked *env, struct iked_sa *sa, int add) return (0); if (sa->sa_cp_addr) { - iovcnt = 0; - addr = (struct sockaddr_in *)&sa->sa_cp_addr->addr; - iov[0].iov_base = addr; - iov[0].iov_len = sizeof(*addr); - iovcnt++; - - bzero(&mask, sizeof(mask)); - mask.sin_addr.s_addr = - prefixlen2mask(sa->sa_cp_addr->addr_mask ? - sa->sa_cp_addr->addr_mask : 32); - mask.sin_family = AF_INET; - mask.sin_len = sizeof(mask); - iov[1].iov_base = &mask; - iov[1].iov_len = sizeof(mask); - iovcnt++; - - iov[2].iov_base = &sa->sa_policy->pol_iface; - iov[2].iov_len = sizeof(sa->sa_policy->pol_iface); - iovcnt++; - - if(proc_composev(&env->sc_ps, PROC_PARENT, - add ? IMSG_IF_ADDADDR : IMSG_IF_DELADDR, - iov, iovcnt)) + if (vroute_setaddr(env, add, + (struct sockaddr *)&sa->sa_cp_addr->addr, + sa->sa_cp_addr->addr_mask, sa->sa_policy->pol_iface) != 0) return (-1); } if (sa->sa_cp_addr6) { - iovcnt = 0; - addr6 = (struct sockaddr_in6 *)&sa->sa_cp_addr6->addr; - iov[0].iov_base = addr6; - iov[0].iov_len = sizeof(*addr6); - iovcnt++; - - bzero(&mask6, sizeof(mask6)); - prefixlen2mask6(sa->sa_cp_addr6->addr_mask ? - sa->sa_cp_addr6->addr_mask : 128, - (uint32_t *)&mask6.sin6_addr.s6_addr); - mask6.sin6_family = AF_INET6; - mask6.sin6_len = sizeof(mask6); - iov[1].iov_base = &mask6; - iov[1].iov_len = sizeof(mask6); - iovcnt++; - - iov[2].iov_base = &sa->sa_policy->pol_iface; - iov[2].iov_len = sizeof(sa->sa_policy->pol_iface); - iovcnt++; - - if(proc_composev(&env->sc_ps, PROC_PARENT, - add ? IMSG_IF_ADDADDR : IMSG_IF_DELADDR, - iov, iovcnt)) + if (vroute_setaddr(env, add, + (struct sockaddr *)&sa->sa_cp_addr6->addr, + sa->sa_cp_addr6->addr_mask, sa->sa_policy->pol_iface) != 0) return (-1); } diff --git a/sbin/iked/vroute.c b/sbin/iked/vroute.c index cd2e3990190..82da493cd70 100644 --- a/sbin/iked/vroute.c +++ b/sbin/iked/vroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vroute.c,v 1.10 2021/06/01 20:57:12 tobhe Exp $ */ +/* $OpenBSD: vroute.c,v 1.11 2021/06/23 12:11:40 tobhe Exp $ */ /* * Copyright (c) 2021 Tobias Heider @@ -140,6 +140,53 @@ vroute_cleanup(struct iked *env) } } +int +vroute_setaddr(struct iked *env, int add, struct sockaddr *addr, + int mask, unsigned int ifidx) +{ + struct iovec iov[4]; + int iovcnt; + struct sockaddr_in mask4; + struct sockaddr_in6 mask6; + + iovcnt = 0; + iov[0].iov_base = addr; + iov[0].iov_len = addr->sa_len; + iovcnt++; + + switch(addr->sa_family) { + case AF_INET: + bzero(&mask, sizeof(mask)); + mask4.sin_addr.s_addr = prefixlen2mask(mask ? mask : 32); + mask4.sin_family = AF_INET; + mask4.sin_len = sizeof(mask4); + + iov[1].iov_base = &mask4; + iov[1].iov_len = sizeof(mask4); + iovcnt++; + break; + case AF_INET6: + bzero(&mask6, sizeof(mask6)); + prefixlen2mask6(mask ? mask : 128, + (uint32_t *)&mask6.sin6_addr.s6_addr); + mask6.sin6_family = AF_INET6; + mask6.sin6_len = sizeof(mask6); + iov[1].iov_base = &mask6; + iov[1].iov_len = sizeof(mask6); + iovcnt++; + break; + default: + return -1; + } + + iov[2].iov_base = &ifidx; + iov[2].iov_len = sizeof(ifidx); + iovcnt++; + + return (proc_composev(&env->sc_ps, PROC_PARENT, + add ? IMSG_IF_ADDADDR : IMSG_IF_DELADDR, iov, iovcnt)); +} + int vroute_getaddr(struct iked *env, struct imsg *imsg) {