datapath: Provide compatibility for kernels up to 3.17
[cascardo/ovs.git] / datapath / linux / compat / include / net / udp.h
1 #ifndef __NET_UDP_WRAPPER_H
2 #define __NET_UDP_WRAPPER_H  1
3
4 #include_next <net/udp.h>
5
6 #ifndef HAVE_UDP_FLOW_SRC_PORT
7 static inline __be16 udp_flow_src_port(struct net *net, struct sk_buff *skb,
8                                        int min, int max, bool use_eth)
9 {
10         u32 hash;
11
12         if (min >= max) {
13                 /* Use default range */
14                 inet_get_local_port_range(net, &min, &max);
15         }
16
17         hash = skb_get_hash(skb);
18         if (unlikely(!hash) && use_eth) {
19                 /* Can't find a normal hash, caller has indicated an Ethernet
20                  * packet so use that to compute a hash.
21                  */
22                 hash = jhash(skb->data, 2 * ETH_ALEN,
23                              (__force u32) skb->protocol);
24         }
25
26         /* Since this is being sent on the wire obfuscate hash a bit
27          * to minimize possbility that any useful information to an
28          * attacker is leaked. Only upper 16 bits are relevant in the
29          * computation for 16 bit port value.
30          */
31         hash ^= hash << 16;
32
33         return htons((((u64) hash * (max - min)) >> 32) + min);
34 }
35 #endif
36
37 #endif