Constantify lookup key in ifa_ifwith* functions.

Some places in our network stack already have const
arguments (like if_output() routines and LLE functions).

Code using ifa_ifwith (and similar functins) along with
LLE/_output functions is currently bound to use tricks
like __DECONST(). Provide a cleaner way by making sockaddr
lookup key really constant.

MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D3464
This commit is contained in:
melifaro 2015-09-05 05:33:20 +00:00
parent 3b40d98a5d
commit bbab608243
5 changed files with 30 additions and 28 deletions

View File

@ -1624,18 +1624,18 @@ ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *sa, int fib)
*/
#define sa_dl_equal(a1, a2) \
((((struct sockaddr_dl *)(a1))->sdl_len == \
((struct sockaddr_dl *)(a2))->sdl_len) && \
(bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
LLADDR((struct sockaddr_dl *)(a2)), \
((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
((((const struct sockaddr_dl *)(a1))->sdl_len == \
((const struct sockaddr_dl *)(a2))->sdl_len) && \
(bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
CLLADDR((const struct sockaddr_dl *)(a2)), \
((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
/*
* Locate an interface based on a complete address.
*/
/*ARGSUSED*/
static struct ifaddr *
ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref)
{
struct ifnet *ifp;
struct ifaddr *ifa;
@ -1672,14 +1672,14 @@ ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
}
struct ifaddr *
ifa_ifwithaddr(struct sockaddr *addr)
ifa_ifwithaddr(const struct sockaddr *addr)
{
return (ifa_ifwithaddr_internal(addr, 1));
}
int
ifa_ifwithaddr_check(struct sockaddr *addr)
ifa_ifwithaddr_check(const struct sockaddr *addr)
{
return (ifa_ifwithaddr_internal(addr, 0) != NULL);
@ -1690,7 +1690,7 @@ ifa_ifwithaddr_check(struct sockaddr *addr)
*/
/* ARGSUSED */
struct ifaddr *
ifa_ifwithbroadaddr(struct sockaddr *addr, int fibnum)
ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
{
struct ifnet *ifp;
struct ifaddr *ifa;
@ -1725,7 +1725,7 @@ ifa_ifwithbroadaddr(struct sockaddr *addr, int fibnum)
*/
/*ARGSUSED*/
struct ifaddr *
ifa_ifwithdstaddr(struct sockaddr *addr, int fibnum)
ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
{
struct ifnet *ifp;
struct ifaddr *ifa;
@ -1760,20 +1760,20 @@ ifa_ifwithdstaddr(struct sockaddr *addr, int fibnum)
* is most specific found.
*/
struct ifaddr *
ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp, int fibnum)
ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
{
struct ifnet *ifp;
struct ifaddr *ifa;
struct ifaddr *ifa_maybe = NULL;
u_int af = addr->sa_family;
char *addr_data = addr->sa_data, *cplim;
const char *addr_data = addr->sa_data, *cplim;
/*
* AF_LINK addresses can be looked up directly by their index number,
* so do that if we can.
*/
if (af == AF_LINK) {
struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
return (ifaddr_byindex(sdl->sdl_index));
}
@ -1790,7 +1790,7 @@ ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp, int fibnum)
continue;
IF_ADDR_RLOCK(ifp);
TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
char *cp, *cp2, *cp3;
const char *cp, *cp2, *cp3;
if (ifa->ifa_addr->sa_family != af)
next: continue;
@ -1863,10 +1863,10 @@ next: continue;
* a given address.
*/
struct ifaddr *
ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
{
struct ifaddr *ifa;
char *cp, *cp2, *cp3;
const char *cp, *cp2, *cp3;
char *cplim;
struct ifaddr *ifa_maybe = NULL;
u_int af = addr->sa_family;
@ -2924,7 +2924,7 @@ if_allmulti(struct ifnet *ifp, int onswitch)
}
struct ifmultiaddr *
if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
{
struct ifmultiaddr *ifma;

View File

@ -67,6 +67,7 @@ struct sockaddr_dl {
};
#define LLADDR(s) ((caddr_t)((s)->sdl_data + (s)->sdl_nlen))
#define CLLADDR(s) ((c_caddr_t)((s)->sdl_data + (s)->sdl_nlen))
#define LLINDEX(s) ((s)->sdl_index)

View File

@ -487,7 +487,7 @@ void if_purgeaddrs(struct ifnet *);
void if_delallmulti(struct ifnet *);
void if_down(struct ifnet *);
struct ifmultiaddr *
if_findmulti(struct ifnet *, struct sockaddr *);
if_findmulti(struct ifnet *, const struct sockaddr *);
void if_free(struct ifnet *);
void if_initname(struct ifnet *, const char *, int);
void if_link_state_change(struct ifnet *, int);
@ -505,13 +505,14 @@ int ifa_add_loopback_route(struct ifaddr *, struct sockaddr *);
int ifa_del_loopback_route(struct ifaddr *, struct sockaddr *);
int ifa_switch_loopback_route(struct ifaddr *, struct sockaddr *, int fib);
struct ifaddr *ifa_ifwithaddr(struct sockaddr *);
int ifa_ifwithaddr_check(struct sockaddr *);
struct ifaddr *ifa_ifwithbroadaddr(struct sockaddr *, int);
struct ifaddr *ifa_ifwithdstaddr(struct sockaddr *, int);
struct ifaddr *ifa_ifwithnet(struct sockaddr *, int, int);
struct ifaddr *ifa_ifwithroute(int, struct sockaddr *, struct sockaddr *, u_int);
struct ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
struct ifaddr *ifa_ifwithaddr(const struct sockaddr *);
int ifa_ifwithaddr_check(const struct sockaddr *);
struct ifaddr *ifa_ifwithbroadaddr(const struct sockaddr *, int);
struct ifaddr *ifa_ifwithdstaddr(const struct sockaddr *, int);
struct ifaddr *ifa_ifwithnet(const struct sockaddr *, int, int);
struct ifaddr *ifa_ifwithroute(int, const struct sockaddr *, struct sockaddr *,
u_int);
struct ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *);
int ifa_preferred(struct ifaddr *, struct ifaddr *);
int if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen);

View File

@ -704,7 +704,7 @@ rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
}
struct ifaddr *
ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway,
ifa_ifwithroute(int flags, const struct sockaddr *dst, struct sockaddr *gateway,
u_int fibnum)
{
struct ifaddr *ifa;

View File

@ -282,8 +282,8 @@ struct rt_addrinfo {
1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) )
#define sa_equal(a, b) ( \
(((struct sockaddr *)(a))->sa_len == ((struct sockaddr *)(b))->sa_len) && \
(bcmp((a), (b), ((struct sockaddr *)(b))->sa_len) == 0))
(((const struct sockaddr *)(a))->sa_len == ((const struct sockaddr *)(b))->sa_len) && \
(bcmp((a), (b), ((const struct sockaddr *)(b))->sa_len) == 0))
#ifdef _KERNEL