forked from Minki/linux
[IPV4]: INET_MATCH() annotations
INET_MATCH() and friends depend on an interesting set of kludges: * there's a pair of adjacent fields in struct inet_sock - __be16 dport followed by __u16 num. We want to search by pair, so we combine the keys into a single 32bit value and compare with 32bit value read from &...->dport. * on 64bit targets we combine comparisons with pair of adjacent __be32 fields in the same way. Make sure that we don't mix those values with anything else and that pairs we form them from have correct types. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
parent
45d60b9e29
commit
4f765d842f
@ -461,7 +461,7 @@ static inline struct raw6_sock *raw6_sk(const struct sock *sk)
|
||||
|
||||
#define INET6_MATCH(__sk, __hash, __saddr, __daddr, __ports, __dif)\
|
||||
(((__sk)->sk_hash == (__hash)) && \
|
||||
((*((__u32 *)&(inet_sk(__sk)->dport))) == (__ports)) && \
|
||||
((*((__portpair *)&(inet_sk(__sk)->dport))) == (__ports)) && \
|
||||
((__sk)->sk_family == AF_INET6) && \
|
||||
ipv6_addr_equal(&inet6_sk(__sk)->daddr, (__saddr)) && \
|
||||
ipv6_addr_equal(&inet6_sk(__sk)->rcv_saddr, (__daddr)) && \
|
||||
|
@ -283,31 +283,45 @@ static inline struct sock *inet_lookup_listener(struct inet_hashinfo *hashinfo,
|
||||
}
|
||||
|
||||
/* Socket demux engine toys. */
|
||||
/* What happens here is ugly; there's a pair of adjacent fields in
|
||||
struct inet_sock; __be16 dport followed by __u16 num. We want to
|
||||
search by pair, so we combine the keys into a single 32bit value
|
||||
and compare with 32bit value read from &...->dport. Let's at least
|
||||
make sure that it's not mixed with anything else...
|
||||
On 64bit targets we combine comparisons with pair of adjacent __be32
|
||||
fields in the same way.
|
||||
*/
|
||||
typedef __u32 __bitwise __portpair;
|
||||
#ifdef __BIG_ENDIAN
|
||||
#define INET_COMBINED_PORTS(__sport, __dport) \
|
||||
(((__u32)(__sport) << 16) | (__u32)(__dport))
|
||||
((__force __portpair)(((__force __u32)(__be16)(__sport) << 16) | (__u32)(__dport)))
|
||||
#else /* __LITTLE_ENDIAN */
|
||||
#define INET_COMBINED_PORTS(__sport, __dport) \
|
||||
(((__u32)(__dport) << 16) | (__u32)(__sport))
|
||||
((__force __portpair)(((__u32)(__dport) << 16) | (__force __u32)(__be16)(__sport)))
|
||||
#endif
|
||||
|
||||
#if (BITS_PER_LONG == 64)
|
||||
typedef __u64 __bitwise __addrpair;
|
||||
#ifdef __BIG_ENDIAN
|
||||
#define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
|
||||
const __u64 __name = (((__u64)(__saddr)) << 32) | ((__u64)(__daddr));
|
||||
const __addrpair __name = (__force __addrpair) ( \
|
||||
(((__force __u64)(__be32)(__saddr)) << 32) | \
|
||||
((__force __u64)(__be32)(__daddr)));
|
||||
#else /* __LITTLE_ENDIAN */
|
||||
#define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
|
||||
const __u64 __name = (((__u64)(__daddr)) << 32) | ((__u64)(__saddr));
|
||||
const __addrpair __name = (__force __addrpair) ( \
|
||||
(((__force __u64)(__be32)(__daddr)) << 32) | \
|
||||
((__force __u64)(__be32)(__saddr)));
|
||||
#endif /* __BIG_ENDIAN */
|
||||
#define INET_MATCH(__sk, __hash, __cookie, __saddr, __daddr, __ports, __dif)\
|
||||
(((__sk)->sk_hash == (__hash)) && \
|
||||
((*((__u64 *)&(inet_sk(__sk)->daddr))) == (__cookie)) && \
|
||||
((*((__u32 *)&(inet_sk(__sk)->dport))) == (__ports)) && \
|
||||
((*((__addrpair *)&(inet_sk(__sk)->daddr))) == (__cookie)) && \
|
||||
((*((__portpair *)&(inet_sk(__sk)->dport))) == (__ports)) && \
|
||||
(!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
|
||||
#define INET_TW_MATCH(__sk, __hash, __cookie, __saddr, __daddr, __ports, __dif)\
|
||||
(((__sk)->sk_hash == (__hash)) && \
|
||||
((*((__u64 *)&(inet_twsk(__sk)->tw_daddr))) == (__cookie)) && \
|
||||
((*((__u32 *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \
|
||||
((*((__addrpair *)&(inet_twsk(__sk)->tw_daddr))) == (__cookie)) && \
|
||||
((*((__portpair *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \
|
||||
(!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
|
||||
#else /* 32-bit arch */
|
||||
#define INET_ADDR_COOKIE(__name, __saddr, __daddr)
|
||||
@ -315,13 +329,13 @@ static inline struct sock *inet_lookup_listener(struct inet_hashinfo *hashinfo,
|
||||
(((__sk)->sk_hash == (__hash)) && \
|
||||
(inet_sk(__sk)->daddr == (__saddr)) && \
|
||||
(inet_sk(__sk)->rcv_saddr == (__daddr)) && \
|
||||
((*((__u32 *)&(inet_sk(__sk)->dport))) == (__ports)) && \
|
||||
((*((__portpair *)&(inet_sk(__sk)->dport))) == (__ports)) && \
|
||||
(!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
|
||||
#define INET_TW_MATCH(__sk, __hash,__cookie, __saddr, __daddr, __ports, __dif) \
|
||||
(((__sk)->sk_hash == (__hash)) && \
|
||||
(inet_twsk(__sk)->tw_daddr == (__saddr)) && \
|
||||
(inet_twsk(__sk)->tw_rcv_saddr == (__daddr)) && \
|
||||
((*((__u32 *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \
|
||||
((*((__portpair *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \
|
||||
(!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
|
||||
#endif /* 64-bit arch */
|
||||
|
||||
@ -338,7 +352,7 @@ static inline struct sock *
|
||||
const int dif)
|
||||
{
|
||||
INET_ADDR_COOKIE(acookie, saddr, daddr)
|
||||
const __u32 ports = INET_COMBINED_PORTS(sport, hnum);
|
||||
const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
|
||||
struct sock *sk;
|
||||
const struct hlist_node *node;
|
||||
/* Optimize here for direct hit, only listening connections can
|
||||
|
@ -201,7 +201,7 @@ static int __inet_check_established(struct inet_timewait_death_row *death_row,
|
||||
u32 saddr = inet->daddr;
|
||||
int dif = sk->sk_bound_dev_if;
|
||||
INET_ADDR_COOKIE(acookie, saddr, daddr)
|
||||
const __u32 ports = INET_COMBINED_PORTS(inet->dport, lport);
|
||||
const __portpair ports = INET_COMBINED_PORTS(inet->dport, lport);
|
||||
unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport);
|
||||
struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
|
||||
struct sock *sk2;
|
||||
|
@ -64,7 +64,7 @@ struct sock *__inet6_lookup_established(struct inet_hashinfo *hashinfo,
|
||||
{
|
||||
struct sock *sk;
|
||||
const struct hlist_node *node;
|
||||
const __u32 ports = INET_COMBINED_PORTS(sport, hnum);
|
||||
const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
|
||||
/* Optimize here for direct hit, only listening connections can
|
||||
* have wildcards anyways.
|
||||
*/
|
||||
@ -82,7 +82,7 @@ struct sock *__inet6_lookup_established(struct inet_hashinfo *hashinfo,
|
||||
sk_for_each(sk, node, &(head + hashinfo->ehash_size)->chain) {
|
||||
const struct inet_timewait_sock *tw = inet_twsk(sk);
|
||||
|
||||
if(*((__u32 *)&(tw->tw_dport)) == ports &&
|
||||
if(*((__portpair *)&(tw->tw_dport)) == ports &&
|
||||
sk->sk_family == PF_INET6) {
|
||||
const struct inet6_timewait_sock *tw6 = inet6_twsk(sk);
|
||||
|
||||
@ -171,7 +171,7 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row,
|
||||
const struct in6_addr *daddr = &np->rcv_saddr;
|
||||
const struct in6_addr *saddr = &np->daddr;
|
||||
const int dif = sk->sk_bound_dev_if;
|
||||
const u32 ports = INET_COMBINED_PORTS(inet->dport, lport);
|
||||
const __portpair ports = INET_COMBINED_PORTS(inet->dport, lport);
|
||||
const unsigned int hash = inet6_ehashfn(daddr, inet->num, saddr,
|
||||
inet->dport);
|
||||
struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
|
||||
@ -188,7 +188,7 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row,
|
||||
|
||||
tw = inet_twsk(sk2);
|
||||
|
||||
if(*((__u32 *)&(tw->tw_dport)) == ports &&
|
||||
if(*((__portpair *)&(tw->tw_dport)) == ports &&
|
||||
sk2->sk_family == PF_INET6 &&
|
||||
ipv6_addr_equal(&tw6->tw_v6_daddr, saddr) &&
|
||||
ipv6_addr_equal(&tw6->tw_v6_rcv_saddr, daddr) &&
|
||||
|
Loading…
Reference in New Issue
Block a user