From a3e24bb9ae2491ed3d8b80f9e2966ab6dfb77ac6 Mon Sep 17 00:00:00 2001 From: bluhm Date: Fri, 10 Nov 2023 20:05:22 +0000 Subject: [PATCH] rtable_match() takes constant destination. For implementing MP safe route lookup, it helps to know which function parameters are constant. Add some const declarations, so that the compiler guarantees that sockaddr dst parameter of rtable_match() does not change. OK dlg@ --- sys/net/art.c | 16 ++++++++-------- sys/net/art.h | 12 ++++++------ sys/net/route.c | 12 ++++++------ sys/net/route.h | 4 ++-- sys/net/rtable.c | 23 ++++++++++++----------- sys/net/rtable.h | 5 +++-- sys/netinet/in.h | 8 +++++++- sys/netinet6/in6.h | 8 +++++++- 8 files changed, 51 insertions(+), 37 deletions(-) diff --git a/sys/net/art.c b/sys/net/art.c index 404b10cc80c..900de04846c 100644 --- a/sys/net/art.c +++ b/sys/net/art.c @@ -1,4 +1,4 @@ -/* $OpenBSD: art.c,v 1.29 2020/11/12 15:25:28 mpi Exp $ */ +/* $OpenBSD: art.c,v 1.30 2023/11/10 20:05:22 bluhm Exp $ */ /* * Copyright (c) 2015 Martin Pieuchot @@ -37,7 +37,7 @@ #include -int art_bindex(struct art_table *, uint8_t *, int); +int art_bindex(struct art_table *, const uint8_t *, int); void art_allot(struct art_table *at, int, struct art_node *, struct art_node *); struct art_table *art_table_get(struct art_root *, struct art_table *, @@ -149,7 +149,7 @@ art_check_duplicate(struct art_root *ar, struct art_node *old, * prefix length is > 24. */ int -art_bindex(struct art_table *at, uint8_t *addr, int plen) +art_bindex(struct art_table *at, const uint8_t *addr, int plen) { uint8_t boff, bend; uint32_t k; @@ -204,7 +204,7 @@ art_bindex(struct art_table *at, uint8_t *addr, int plen) * corresponding to the range covered by the table ``at''. */ static inline int -art_findex(struct art_table *at, uint8_t *addr) +art_findex(struct art_table *at, const uint8_t *addr) { return art_bindex(at, addr, (at->at_offset + at->at_bits)); } @@ -215,7 +215,7 @@ art_findex(struct art_table *at, uint8_t *addr) * Return the best existing match for a destination. */ struct art_node * -art_match(struct art_root *ar, void *addr, struct srp_ref *nsr) +art_match(struct art_root *ar, const void *addr, struct srp_ref *nsr) { struct srp_ref dsr, ndsr; void *entry; @@ -278,7 +278,7 @@ done: * it does not exist. */ struct art_node * -art_lookup(struct art_root *ar, void *addr, int plen, struct srp_ref *nsr) +art_lookup(struct art_root *ar, const void *addr, int plen, struct srp_ref *nsr) { void *entry; struct art_table *at; @@ -336,7 +336,7 @@ done: * same destination/mask pair is already present. */ struct art_node * -art_insert(struct art_root *ar, struct art_node *an, void *addr, int plen) +art_insert(struct art_root *ar, struct art_node *an, const void *addr, int plen) { struct art_table *at, *child; struct art_node *node; @@ -440,7 +440,7 @@ art_table_insert(struct art_root *ar, struct art_table *at, int i, * Deletion API function. */ struct art_node * -art_delete(struct art_root *ar, struct art_node *an, void *addr, int plen) +art_delete(struct art_root *ar, struct art_node *an, const void *addr, int plen) { struct art_table *at; struct art_node *node; diff --git a/sys/net/art.h b/sys/net/art.h index 33462c465de..e03d0f61498 100644 --- a/sys/net/art.h +++ b/sys/net/art.h @@ -1,4 +1,4 @@ -/* $OpenBSD: art.h,v 1.23 2023/04/19 17:42:47 bluhm Exp $ */ +/* $OpenBSD: art.h,v 1.24 2023/11/10 20:05:22 bluhm Exp $ */ /* * Copyright (c) 2015 Martin Pieuchot @@ -95,13 +95,13 @@ struct art_node { #define an_gc an_pointer.an__gc void art_init(void); -struct art_root *art_alloc(unsigned int, unsigned int, unsigned int); -struct art_node *art_insert(struct art_root *, struct art_node *, void *, +struct art_root *art_alloc(unsigned int, unsigned int, unsigned int); +struct art_node *art_insert(struct art_root *, struct art_node *, const void *, int); -struct art_node *art_delete(struct art_root *, struct art_node *, void *, +struct art_node *art_delete(struct art_root *, struct art_node *, const void *, int); -struct art_node *art_match(struct art_root *, void *, struct srp_ref *); -struct art_node *art_lookup(struct art_root *, void *, int, +struct art_node *art_match(struct art_root *, const void *, struct srp_ref *); +struct art_node *art_lookup(struct art_root *, const void *, int, struct srp_ref *); int art_walk(struct art_root *, int (*)(struct art_node *, void *), void *); diff --git a/sys/net/route.c b/sys/net/route.c index e7c5442c3bd..35909c1abf7 100644 --- a/sys/net/route.c +++ b/sys/net/route.c @@ -1,4 +1,4 @@ -/* $OpenBSD: route.c,v 1.422 2023/04/28 20:03:14 mvs Exp $ */ +/* $OpenBSD: route.c,v 1.423 2023/11/10 20:05:22 bluhm Exp $ */ /* $NetBSD: route.c,v 1.14 1996/02/13 22:00:46 christos Exp $ */ /* @@ -294,7 +294,7 @@ rt_clone(struct rtentry **rtp, struct sockaddr *dst, unsigned int rtableid) } while (0) int -rt_hash(struct rtentry *rt, struct sockaddr *dst, uint32_t *src) +rt_hash(struct rtentry *rt, const struct sockaddr *dst, uint32_t *src) { uint32_t a, b, c; @@ -307,12 +307,12 @@ rt_hash(struct rtentry *rt, struct sockaddr *dst, uint32_t *src) switch (dst->sa_family) { case AF_INET: { - struct sockaddr_in *sin; + const struct sockaddr_in *sin; if (!ipmultipath) return (-1); - sin = satosin(dst); + sin = satosin_const(dst); a += sin->sin_addr.s_addr; b += src[0]; mix(a, b, c); @@ -321,12 +321,12 @@ rt_hash(struct rtentry *rt, struct sockaddr *dst, uint32_t *src) #ifdef INET6 case AF_INET6: { - struct sockaddr_in6 *sin6; + const struct sockaddr_in6 *sin6; if (!ip6_multipath) return (-1); - sin6 = satosin6(dst); + sin6 = satosin6_const(dst); a += sin6->sin6_addr.s6_addr32[0]; b += sin6->sin6_addr.s6_addr32[2]; c += src[0]; diff --git a/sys/net/route.h b/sys/net/route.h index e74d623fc0b..6c41d872b28 100644 --- a/sys/net/route.h +++ b/sys/net/route.h @@ -1,4 +1,4 @@ -/* $OpenBSD: route.h,v 1.199 2023/04/26 19:54:35 mvs Exp $ */ +/* $OpenBSD: route.h,v 1.200 2023/11/10 20:05:22 bluhm Exp $ */ /* $NetBSD: route.h,v 1.9 1996/02/13 22:00:49 christos Exp $ */ /* @@ -467,7 +467,7 @@ int rt_mpls_set(struct rtentry *, struct sockaddr *, uint8_t); void rt_mpls_clear(struct rtentry *); int rtisvalid(struct rtentry *); -int rt_hash(struct rtentry *, struct sockaddr *, uint32_t *); +int rt_hash(struct rtentry *, const struct sockaddr *, uint32_t *); struct rtentry *rtalloc_mpath(struct sockaddr *, uint32_t *, u_int); struct rtentry *rtalloc(struct sockaddr *, int, unsigned int); void rtref(struct rtentry *); diff --git a/sys/net/rtable.c b/sys/net/rtable.c index 6f7b96f9f50..676563b6e3d 100644 --- a/sys/net/rtable.c +++ b/sys/net/rtable.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rtable.c,v 1.82 2023/04/19 17:42:47 bluhm Exp $ */ +/* $OpenBSD: rtable.c,v 1.83 2023/11/10 20:05:22 bluhm Exp $ */ /* * Copyright (c) 2014-2016 Martin Pieuchot @@ -349,7 +349,8 @@ rtable_l2set(unsigned int rtableid, unsigned int rdomain, unsigned int loifidx) } -static inline uint8_t *satoaddr(struct art_root *, struct sockaddr *); +static inline const uint8_t *satoaddr(struct art_root *, + const struct sockaddr *); int an_match(struct art_node *, struct sockaddr *, int); void rtentry_ref(void *, void *); @@ -421,7 +422,7 @@ rtable_lookup(unsigned int rtableid, struct sockaddr *dst, struct art_node *an; struct rtentry *rt = NULL; struct srp_ref sr, nsr; - uint8_t *addr; + const uint8_t *addr; int plen; ar = rtable_get(rtableid, dst->sa_family); @@ -470,13 +471,13 @@ out: } struct rtentry * -rtable_match(unsigned int rtableid, struct sockaddr *dst, uint32_t *src) +rtable_match(unsigned int rtableid, const struct sockaddr *dst, uint32_t *src) { struct art_root *ar; struct art_node *an; struct rtentry *rt = NULL; struct srp_ref sr, nsr; - uint8_t *addr; + const uint8_t *addr; int hash; ar = rtable_get(rtableid, dst->sa_family); @@ -551,7 +552,7 @@ rtable_insert(unsigned int rtableid, struct sockaddr *dst, struct srp_ref sr; struct art_root *ar; struct art_node *an, *prev; - uint8_t *addr; + const uint8_t *addr; int plen; unsigned int rt_flags; int error = 0; @@ -655,7 +656,7 @@ rtable_delete(unsigned int rtableid, struct sockaddr *dst, struct art_root *ar; struct art_node *an; struct srp_ref sr; - uint8_t *addr; + const uint8_t *addr; int plen; struct rtentry *mrt; int npaths = 0; @@ -795,7 +796,7 @@ rtable_mpath_reprio(unsigned int rtableid, struct sockaddr *dst, struct art_root *ar; struct art_node *an; struct srp_ref sr; - uint8_t *addr; + const uint8_t *addr; int error = 0; ar = rtable_get(rtableid, dst->sa_family); @@ -901,10 +902,10 @@ rtentry_unref(void *null, void *xrt) * BSD radix tree needed to skip the non-address fields from the flavor * of "struct sockaddr" used by this routing table. */ -static inline uint8_t * -satoaddr(struct art_root *at, struct sockaddr *sa) +static inline const uint8_t * +satoaddr(struct art_root *at, const struct sockaddr *sa) { - return (((uint8_t *)sa) + at->ar_off); + return (((const uint8_t *)sa) + at->ar_off); } /* diff --git a/sys/net/rtable.h b/sys/net/rtable.h index e9dc137918e..a849183a4c3 100644 --- a/sys/net/rtable.h +++ b/sys/net/rtable.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rtable.h,v 1.26 2020/11/07 09:51:40 denis Exp $ */ +/* $OpenBSD: rtable.h,v 1.27 2023/11/10 20:05:22 bluhm Exp $ */ /* * Copyright (c) 2014-2016 Martin Pieuchot @@ -44,7 +44,8 @@ struct sockaddr *rtable_getsource(unsigned int, int); void rtable_clearsource(unsigned int, struct sockaddr *); struct rtentry *rtable_lookup(unsigned int, struct sockaddr *, struct sockaddr *, struct sockaddr *, uint8_t); -struct rtentry *rtable_match(unsigned int, struct sockaddr *, uint32_t *); +struct rtentry *rtable_match(unsigned int, const struct sockaddr *, + uint32_t *); struct rtentry *rtable_iterate(struct rtentry *); int rtable_insert(unsigned int, struct sockaddr *, struct sockaddr *, struct sockaddr *, uint8_t, diff --git a/sys/netinet/in.h b/sys/netinet/in.h index 359be65f650..9e7b473fd3d 100644 --- a/sys/netinet/in.h +++ b/sys/netinet/in.h @@ -1,4 +1,4 @@ -/* $OpenBSD: in.h,v 1.144 2023/05/13 13:35:17 bluhm Exp $ */ +/* $OpenBSD: in.h,v 1.145 2023/11/10 20:05:22 bluhm Exp $ */ /* $NetBSD: in.h,v 1.20 1996/02/13 23:41:47 christos Exp $ */ /* @@ -810,6 +810,12 @@ satosin(struct sockaddr *sa) return ((struct sockaddr_in *)(sa)); } +static inline const struct sockaddr_in * +satosin_const(const struct sockaddr *sa) +{ + return ((const struct sockaddr_in *)(sa)); +} + static inline struct sockaddr * sintosa(struct sockaddr_in *sin) { diff --git a/sys/netinet6/in6.h b/sys/netinet6/in6.h index c7ce800c8a6..1a022f4c2e4 100644 --- a/sys/netinet6/in6.h +++ b/sys/netinet6/in6.h @@ -1,4 +1,4 @@ -/* $OpenBSD: in6.h,v 1.109 2021/06/02 00:20:50 dlg Exp $ */ +/* $OpenBSD: in6.h,v 1.110 2023/11/10 20:05:23 bluhm Exp $ */ /* $KAME: in6.h,v 1.83 2001/03/29 02:55:07 jinmei Exp $ */ /* @@ -446,6 +446,12 @@ satosin6(struct sockaddr *sa) return ((struct sockaddr_in6 *)(sa)); } +static inline const struct sockaddr_in6 * +satosin6_const(const struct sockaddr *sa) +{ + return ((const struct sockaddr_in6 *)(sa)); +} + static inline struct sockaddr * sin6tosa(struct sockaddr_in6 *sin6) { -- 2.20.1