Skip to content

Commit b8f1a55

Browse files
Tom Herbertdavem330
Tom Herbert
authored andcommitted
udp: Add function to make source port for UDP tunnels
This patch adds udp_flow_src_port function which is intended to be a common function that UDP tunnel implementations call to set the source port. The source port is chosen so that a hash over the outer headers (IP addresses and UDP ports) acts as suitable hash for the flow of the encapsulated packet. In this manner, UDP encapsulation works with RSS and ECMP based wrt the inner flow. Signed-off-by: Tom Herbert <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 0e00161 commit b8f1a55

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

include/net/udp.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,35 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
176176
int (*)(const struct sock *, const struct sock *),
177177
unsigned int hash2_nulladdr);
178178

179+
static inline __be16 udp_flow_src_port(struct net *net, struct sk_buff *skb,
180+
int min, int max, bool use_eth)
181+
{
182+
u32 hash;
183+
184+
if (min >= max) {
185+
/* Use default range */
186+
inet_get_local_port_range(net, &min, &max);
187+
}
188+
189+
hash = skb_get_hash(skb);
190+
if (unlikely(!hash) && use_eth) {
191+
/* Can't find a normal hash, caller has indicated an Ethernet
192+
* packet so use that to compute a hash.
193+
*/
194+
hash = jhash(skb->data, 2 * ETH_ALEN,
195+
(__force u32) skb->protocol);
196+
}
197+
198+
/* Since this is being sent on the wire obfuscate hash a bit
199+
* to minimize possbility that any useful information to an
200+
* attacker is leaked. Only upper 16 bits are relevant in the
201+
* computation for 16 bit port value.
202+
*/
203+
hash ^= hash << 16;
204+
205+
return htons((((u64) hash * (max - min)) >> 32) + min);
206+
}
207+
179208
/* net/ipv4/udp.c */
180209
void udp_v4_early_demux(struct sk_buff *skb);
181210
int udp_get_port(struct sock *sk, unsigned short snum,

0 commit comments

Comments
 (0)