1 #include <linux/kernel.h>
2 #include <linux/skbuff.h>
3 #include <linux/export.h>
5 #include <linux/ipv6.h>
6 #include <linux/if_vlan.h>
9 #include <linux/igmp.h>
10 #include <linux/icmp.h>
11 #include <linux/sctp.h>
12 #include <linux/dccp.h>
13 #include <linux/if_tunnel.h>
14 #include <linux/if_pppox.h>
15 #include <linux/ppp_defs.h>
16 #include <net/flow_dissector.h>
17 #include <scsi/fc/fc_fcoe.h>
19 static bool skb_flow_dissector_uses_key(struct flow_dissector *flow_dissector,
20 enum flow_dissector_key_id key_id)
22 return flow_dissector->used_keys & (1 << key_id);
25 static void skb_flow_dissector_set_key(struct flow_dissector *flow_dissector,
26 enum flow_dissector_key_id key_id)
28 flow_dissector->used_keys |= (1 << key_id);
31 static void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
32 enum flow_dissector_key_id key_id,
33 void *target_container)
35 return ((char *) target_container) + flow_dissector->offset[key_id];
38 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
39 const struct flow_dissector_key *key,
40 unsigned int key_count)
44 memset(flow_dissector, 0, sizeof(*flow_dissector));
46 for (i = 0; i < key_count; i++, key++) {
47 /* User should make sure that every key target offset is withing
48 * boundaries of unsigned short.
50 BUG_ON(key->offset > USHRT_MAX);
51 BUG_ON(skb_flow_dissector_uses_key(flow_dissector,
54 skb_flow_dissector_set_key(flow_dissector, key->key_id);
55 flow_dissector->offset[key->key_id] = key->offset;
58 /* Ensure that the dissector always includes basic key. That way
59 * we are able to avoid handling lack of it in fast path.
61 BUG_ON(!skb_flow_dissector_uses_key(flow_dissector,
62 FLOW_DISSECTOR_KEY_BASIC));
64 EXPORT_SYMBOL(skb_flow_dissector_init);
66 /* copy saddr & daddr, possibly using 64bit load/store
67 * Equivalent to : flow->src = iph->saddr;
68 * flow->dst = iph->daddr;
70 static void iph_to_flow_copy_addrs(struct flow_keys *flow, const struct iphdr *iph)
72 BUILD_BUG_ON(offsetof(typeof(*flow), dst) !=
73 offsetof(typeof(*flow), src) + sizeof(flow->src));
74 memcpy(&flow->src, &iph->saddr, sizeof(flow->src) + sizeof(flow->dst));
78 * __skb_flow_get_ports - extract the upper layer ports and return them
79 * @skb: sk_buff to extract the ports from
80 * @thoff: transport header offset
81 * @ip_proto: protocol for which to get port offset
82 * @data: raw buffer pointer to the packet, if NULL use skb->data
83 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
85 * The function will try to retrieve the ports at offset thoff + poff where poff
86 * is the protocol port offset returned from proto_ports_offset
88 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
91 int poff = proto_ports_offset(ip_proto);
95 hlen = skb_headlen(skb);
99 __be32 *ports, _ports;
101 ports = __skb_header_pointer(skb, thoff + poff,
102 sizeof(_ports), data, hlen, &_ports);
109 EXPORT_SYMBOL(__skb_flow_get_ports);
112 * __skb_flow_dissect - extract the flow_keys struct and return it
113 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
114 * @data: raw buffer pointer to the packet, if NULL use skb->data
115 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
116 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
117 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
119 * The function will try to retrieve the struct flow_keys from either the skbuff
120 * or a raw buffer specified by the rest parameters
122 bool __skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow,
123 void *data, __be16 proto, int nhoff, int hlen)
129 proto = skb->protocol;
130 nhoff = skb_network_offset(skb);
131 hlen = skb_headlen(skb);
134 memset(flow, 0, sizeof(*flow));
138 case htons(ETH_P_IP): {
139 const struct iphdr *iph;
142 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
143 if (!iph || iph->ihl < 5)
145 nhoff += iph->ihl * 4;
147 ip_proto = iph->protocol;
148 if (ip_is_fragment(iph))
151 /* skip the address processing if skb is NULL. The assumption
152 * here is that if there is no skb we are not looking for flow
153 * info but lengths and protocols.
158 iph_to_flow_copy_addrs(flow, iph);
161 case htons(ETH_P_IPV6): {
162 const struct ipv6hdr *iph;
167 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
171 ip_proto = iph->nexthdr;
172 nhoff += sizeof(struct ipv6hdr);
174 /* see comment above in IPv4 section */
178 flow->src = (__force __be32)ipv6_addr_hash(&iph->saddr);
179 flow->dst = (__force __be32)ipv6_addr_hash(&iph->daddr);
181 flow_label = ip6_flowlabel(iph);
183 /* Awesome, IPv6 packet has a flow label so we can
184 * use that to represent the ports without any
185 * further dissection.
187 flow->n_proto = proto;
188 flow->ip_proto = ip_proto;
189 flow->ports = flow_label;
190 flow->thoff = (u16)nhoff;
197 case htons(ETH_P_8021AD):
198 case htons(ETH_P_8021Q): {
199 const struct vlan_hdr *vlan;
200 struct vlan_hdr _vlan;
202 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan);
206 proto = vlan->h_vlan_encapsulated_proto;
207 nhoff += sizeof(*vlan);
210 case htons(ETH_P_PPP_SES): {
212 struct pppoe_hdr hdr;
215 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
219 nhoff += PPPOE_SES_HLEN;
223 case htons(PPP_IPV6):
229 case htons(ETH_P_TIPC): {
234 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
237 flow->src = hdr->srcnode;
239 flow->n_proto = proto;
240 flow->thoff = (u16)nhoff;
243 case htons(ETH_P_FCOE):
244 flow->thoff = (u16)(nhoff + FCOE_HEADER_LEN);
257 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
261 * Only look inside GRE if version zero and no
264 if (!(hdr->flags & (GRE_VERSION|GRE_ROUTING))) {
267 if (hdr->flags & GRE_CSUM)
269 if (hdr->flags & GRE_KEY)
271 if (hdr->flags & GRE_SEQ)
273 if (proto == htons(ETH_P_TEB)) {
274 const struct ethhdr *eth;
277 eth = __skb_header_pointer(skb, nhoff,
282 proto = eth->h_proto;
283 nhoff += sizeof(*eth);
290 proto = htons(ETH_P_IP);
293 proto = htons(ETH_P_IPV6);
299 flow->n_proto = proto;
300 flow->ip_proto = ip_proto;
301 flow->thoff = (u16) nhoff;
303 /* unless skb is set we don't need to record port info */
305 flow->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
310 EXPORT_SYMBOL(__skb_flow_dissect);
312 static u32 hashrnd __read_mostly;
313 static __always_inline void __flow_hash_secret_init(void)
315 net_get_random_once(&hashrnd, sizeof(hashrnd));
318 static __always_inline u32 __flow_hash_3words(u32 a, u32 b, u32 c, u32 keyval)
320 return jhash_3words(a, b, c, keyval);
323 static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
327 /* get a consistent hash (same value on both flow directions) */
328 if (((__force u32)keys->dst < (__force u32)keys->src) ||
329 (((__force u32)keys->dst == (__force u32)keys->src) &&
330 ((__force u16)keys->port16[1] < (__force u16)keys->port16[0]))) {
331 swap(keys->dst, keys->src);
332 swap(keys->port16[0], keys->port16[1]);
335 hash = __flow_hash_3words((__force u32)keys->dst,
336 (__force u32)keys->src,
337 (__force u32)keys->ports,
345 u32 flow_hash_from_keys(struct flow_keys *keys)
347 __flow_hash_secret_init();
348 return __flow_hash_from_keys(keys, hashrnd);
350 EXPORT_SYMBOL(flow_hash_from_keys);
352 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
353 struct flow_keys *keys, u32 keyval)
355 if (!skb_flow_dissect(skb, keys))
358 return __flow_hash_from_keys(keys, keyval);
361 struct _flow_keys_digest_data {
370 void make_flow_keys_digest(struct flow_keys_digest *digest,
371 const struct flow_keys *flow)
373 struct _flow_keys_digest_data *data =
374 (struct _flow_keys_digest_data *)digest;
376 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
378 memset(digest, 0, sizeof(*digest));
380 data->n_proto = flow->n_proto;
381 data->ip_proto = flow->ip_proto;
382 data->ports = flow->ports;
383 data->src = flow->src;
384 data->dst = flow->dst;
386 EXPORT_SYMBOL(make_flow_keys_digest);
389 * __skb_get_hash: calculate a flow hash
390 * @skb: sk_buff to calculate flow hash from
392 * This function calculates a flow hash based on src/dst addresses
393 * and src/dst port numbers. Sets hash in skb to non-zero hash value
394 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
395 * if hash is a canonical 4-tuple hash over transport ports.
397 void __skb_get_hash(struct sk_buff *skb)
399 struct flow_keys keys;
402 __flow_hash_secret_init();
404 hash = ___skb_get_hash(skb, &keys, hashrnd);
412 EXPORT_SYMBOL(__skb_get_hash);
414 __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
416 struct flow_keys keys;
418 return ___skb_get_hash(skb, &keys, perturb);
420 EXPORT_SYMBOL(skb_get_hash_perturb);
422 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
423 const struct flow_keys *keys, int hlen)
425 u32 poff = keys->thoff;
427 switch (keys->ip_proto) {
429 /* access doff as u8 to avoid unaligned access */
433 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
438 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
442 case IPPROTO_UDPLITE:
443 poff += sizeof(struct udphdr);
445 /* For the rest, we do not really care about header
446 * extensions at this point for now.
449 poff += sizeof(struct icmphdr);
452 poff += sizeof(struct icmp6hdr);
455 poff += sizeof(struct igmphdr);
458 poff += sizeof(struct dccp_hdr);
461 poff += sizeof(struct sctphdr);
469 * skb_get_poff - get the offset to the payload
470 * @skb: sk_buff to get the payload offset from
472 * The function will get the offset to the payload as far as it could
473 * be dissected. The main user is currently BPF, so that we can dynamically
474 * truncate packets without needing to push actual payload to the user
475 * space and can analyze headers only, instead.
477 u32 skb_get_poff(const struct sk_buff *skb)
479 struct flow_keys keys;
481 if (!skb_flow_dissect(skb, &keys))
484 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));