Loading...
1#include <linux/skbuff.h>
2#include <linux/export.h>
3#include <linux/ip.h>
4#include <linux/ipv6.h>
5#include <linux/if_vlan.h>
6#include <net/ip.h>
7#include <net/ipv6.h>
8#include <linux/igmp.h>
9#include <linux/icmp.h>
10#include <linux/sctp.h>
11#include <linux/dccp.h>
12#include <linux/if_tunnel.h>
13#include <linux/if_pppox.h>
14#include <linux/ppp_defs.h>
15#include <net/flow_keys.h>
16
17/* copy saddr & daddr, possibly using 64bit load/store
18 * Equivalent to : flow->src = iph->saddr;
19 * flow->dst = iph->daddr;
20 */
21static void iph_to_flow_copy_addrs(struct flow_keys *flow, const struct iphdr *iph)
22{
23 BUILD_BUG_ON(offsetof(typeof(*flow), dst) !=
24 offsetof(typeof(*flow), src) + sizeof(flow->src));
25 memcpy(&flow->src, &iph->saddr, sizeof(flow->src) + sizeof(flow->dst));
26}
27
28/**
29 * skb_flow_get_ports - extract the upper layer ports and return them
30 * @skb: buffer to extract the ports from
31 * @thoff: transport header offset
32 * @ip_proto: protocol for which to get port offset
33 *
34 * The function will try to retrieve the ports at offset thoff + poff where poff
35 * is the protocol port offset returned from proto_ports_offset
36 */
37__be32 skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto)
38{
39 int poff = proto_ports_offset(ip_proto);
40
41 if (poff >= 0) {
42 __be32 *ports, _ports;
43
44 ports = skb_header_pointer(skb, thoff + poff,
45 sizeof(_ports), &_ports);
46 if (ports)
47 return *ports;
48 }
49
50 return 0;
51}
52EXPORT_SYMBOL(skb_flow_get_ports);
53
54bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow)
55{
56 int nhoff = skb_network_offset(skb);
57 u8 ip_proto;
58 __be16 proto = skb->protocol;
59
60 memset(flow, 0, sizeof(*flow));
61
62again:
63 switch (proto) {
64 case htons(ETH_P_IP): {
65 const struct iphdr *iph;
66 struct iphdr _iph;
67ip:
68 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
69 if (!iph || iph->ihl < 5)
70 return false;
71 nhoff += iph->ihl * 4;
72
73 ip_proto = iph->protocol;
74 if (ip_is_fragment(iph))
75 ip_proto = 0;
76
77 iph_to_flow_copy_addrs(flow, iph);
78 break;
79 }
80 case htons(ETH_P_IPV6): {
81 const struct ipv6hdr *iph;
82 struct ipv6hdr _iph;
83ipv6:
84 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
85 if (!iph)
86 return false;
87
88 ip_proto = iph->nexthdr;
89 flow->src = (__force __be32)ipv6_addr_hash(&iph->saddr);
90 flow->dst = (__force __be32)ipv6_addr_hash(&iph->daddr);
91 nhoff += sizeof(struct ipv6hdr);
92 break;
93 }
94 case htons(ETH_P_8021AD):
95 case htons(ETH_P_8021Q): {
96 const struct vlan_hdr *vlan;
97 struct vlan_hdr _vlan;
98
99 vlan = skb_header_pointer(skb, nhoff, sizeof(_vlan), &_vlan);
100 if (!vlan)
101 return false;
102
103 proto = vlan->h_vlan_encapsulated_proto;
104 nhoff += sizeof(*vlan);
105 goto again;
106 }
107 case htons(ETH_P_PPP_SES): {
108 struct {
109 struct pppoe_hdr hdr;
110 __be16 proto;
111 } *hdr, _hdr;
112 hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
113 if (!hdr)
114 return false;
115 proto = hdr->proto;
116 nhoff += PPPOE_SES_HLEN;
117 switch (proto) {
118 case htons(PPP_IP):
119 goto ip;
120 case htons(PPP_IPV6):
121 goto ipv6;
122 default:
123 return false;
124 }
125 }
126 default:
127 return false;
128 }
129
130 switch (ip_proto) {
131 case IPPROTO_GRE: {
132 struct gre_hdr {
133 __be16 flags;
134 __be16 proto;
135 } *hdr, _hdr;
136
137 hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
138 if (!hdr)
139 return false;
140 /*
141 * Only look inside GRE if version zero and no
142 * routing
143 */
144 if (!(hdr->flags & (GRE_VERSION|GRE_ROUTING))) {
145 proto = hdr->proto;
146 nhoff += 4;
147 if (hdr->flags & GRE_CSUM)
148 nhoff += 4;
149 if (hdr->flags & GRE_KEY)
150 nhoff += 4;
151 if (hdr->flags & GRE_SEQ)
152 nhoff += 4;
153 if (proto == htons(ETH_P_TEB)) {
154 const struct ethhdr *eth;
155 struct ethhdr _eth;
156
157 eth = skb_header_pointer(skb, nhoff,
158 sizeof(_eth), &_eth);
159 if (!eth)
160 return false;
161 proto = eth->h_proto;
162 nhoff += sizeof(*eth);
163 }
164 goto again;
165 }
166 break;
167 }
168 case IPPROTO_IPIP:
169 proto = htons(ETH_P_IP);
170 goto ip;
171 case IPPROTO_IPV6:
172 proto = htons(ETH_P_IPV6);
173 goto ipv6;
174 default:
175 break;
176 }
177
178 flow->ip_proto = ip_proto;
179 flow->ports = skb_flow_get_ports(skb, nhoff, ip_proto);
180 flow->thoff = (u16) nhoff;
181
182 return true;
183}
184EXPORT_SYMBOL(skb_flow_dissect);
185
186static u32 hashrnd __read_mostly;
187static __always_inline void __flow_hash_secret_init(void)
188{
189 net_get_random_once(&hashrnd, sizeof(hashrnd));
190}
191
192static __always_inline u32 __flow_hash_3words(u32 a, u32 b, u32 c)
193{
194 __flow_hash_secret_init();
195 return jhash_3words(a, b, c, hashrnd);
196}
197
198static __always_inline u32 __flow_hash_1word(u32 a)
199{
200 __flow_hash_secret_init();
201 return jhash_1word(a, hashrnd);
202}
203
204/*
205 * __skb_get_hash: calculate a flow hash based on src/dst addresses
206 * and src/dst port numbers. Sets hash in skb to non-zero hash value
207 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
208 * if hash is a canonical 4-tuple hash over transport ports.
209 */
210void __skb_get_hash(struct sk_buff *skb)
211{
212 struct flow_keys keys;
213 u32 hash;
214
215 if (!skb_flow_dissect(skb, &keys))
216 return;
217
218 if (keys.ports)
219 skb->l4_hash = 1;
220
221 /* get a consistent hash (same value on both flow directions) */
222 if (((__force u32)keys.dst < (__force u32)keys.src) ||
223 (((__force u32)keys.dst == (__force u32)keys.src) &&
224 ((__force u16)keys.port16[1] < (__force u16)keys.port16[0]))) {
225 swap(keys.dst, keys.src);
226 swap(keys.port16[0], keys.port16[1]);
227 }
228
229 hash = __flow_hash_3words((__force u32)keys.dst,
230 (__force u32)keys.src,
231 (__force u32)keys.ports);
232 if (!hash)
233 hash = 1;
234
235 skb->hash = hash;
236}
237EXPORT_SYMBOL(__skb_get_hash);
238
239/*
240 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
241 * to be used as a distribution range.
242 */
243u16 __skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb,
244 unsigned int num_tx_queues)
245{
246 u32 hash;
247 u16 qoffset = 0;
248 u16 qcount = num_tx_queues;
249
250 if (skb_rx_queue_recorded(skb)) {
251 hash = skb_get_rx_queue(skb);
252 while (unlikely(hash >= num_tx_queues))
253 hash -= num_tx_queues;
254 return hash;
255 }
256
257 if (dev->num_tc) {
258 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
259 qoffset = dev->tc_to_txq[tc].offset;
260 qcount = dev->tc_to_txq[tc].count;
261 }
262
263 if (skb->sk && skb->sk->sk_hash)
264 hash = skb->sk->sk_hash;
265 else
266 hash = (__force u16) skb->protocol;
267 hash = __flow_hash_1word(hash);
268
269 return (u16) (((u64) hash * qcount) >> 32) + qoffset;
270}
271EXPORT_SYMBOL(__skb_tx_hash);
272
273/* __skb_get_poff() returns the offset to the payload as far as it could
274 * be dissected. The main user is currently BPF, so that we can dynamically
275 * truncate packets without needing to push actual payload to the user
276 * space and can analyze headers only, instead.
277 */
278u32 __skb_get_poff(const struct sk_buff *skb)
279{
280 struct flow_keys keys;
281 u32 poff = 0;
282
283 if (!skb_flow_dissect(skb, &keys))
284 return 0;
285
286 poff += keys.thoff;
287 switch (keys.ip_proto) {
288 case IPPROTO_TCP: {
289 const struct tcphdr *tcph;
290 struct tcphdr _tcph;
291
292 tcph = skb_header_pointer(skb, poff, sizeof(_tcph), &_tcph);
293 if (!tcph)
294 return poff;
295
296 poff += max_t(u32, sizeof(struct tcphdr), tcph->doff * 4);
297 break;
298 }
299 case IPPROTO_UDP:
300 case IPPROTO_UDPLITE:
301 poff += sizeof(struct udphdr);
302 break;
303 /* For the rest, we do not really care about header
304 * extensions at this point for now.
305 */
306 case IPPROTO_ICMP:
307 poff += sizeof(struct icmphdr);
308 break;
309 case IPPROTO_ICMPV6:
310 poff += sizeof(struct icmp6hdr);
311 break;
312 case IPPROTO_IGMP:
313 poff += sizeof(struct igmphdr);
314 break;
315 case IPPROTO_DCCP:
316 poff += sizeof(struct dccp_hdr);
317 break;
318 case IPPROTO_SCTP:
319 poff += sizeof(struct sctphdr);
320 break;
321 }
322
323 return poff;
324}
325
326static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
327{
328#ifdef CONFIG_XPS
329 struct xps_dev_maps *dev_maps;
330 struct xps_map *map;
331 int queue_index = -1;
332
333 rcu_read_lock();
334 dev_maps = rcu_dereference(dev->xps_maps);
335 if (dev_maps) {
336 map = rcu_dereference(
337 dev_maps->cpu_map[raw_smp_processor_id()]);
338 if (map) {
339 if (map->len == 1)
340 queue_index = map->queues[0];
341 else {
342 u32 hash;
343 if (skb->sk && skb->sk->sk_hash)
344 hash = skb->sk->sk_hash;
345 else
346 hash = (__force u16) skb->protocol ^
347 skb->hash;
348 hash = __flow_hash_1word(hash);
349 queue_index = map->queues[
350 ((u64)hash * map->len) >> 32];
351 }
352 if (unlikely(queue_index >= dev->real_num_tx_queues))
353 queue_index = -1;
354 }
355 }
356 rcu_read_unlock();
357
358 return queue_index;
359#else
360 return -1;
361#endif
362}
363
364static u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
365{
366 struct sock *sk = skb->sk;
367 int queue_index = sk_tx_queue_get(sk);
368
369 if (queue_index < 0 || skb->ooo_okay ||
370 queue_index >= dev->real_num_tx_queues) {
371 int new_index = get_xps_queue(dev, skb);
372 if (new_index < 0)
373 new_index = skb_tx_hash(dev, skb);
374
375 if (queue_index != new_index && sk &&
376 rcu_access_pointer(sk->sk_dst_cache))
377 sk_tx_queue_set(sk, new_index);
378
379 queue_index = new_index;
380 }
381
382 return queue_index;
383}
384
385struct netdev_queue *netdev_pick_tx(struct net_device *dev,
386 struct sk_buff *skb,
387 void *accel_priv)
388{
389 int queue_index = 0;
390
391 if (dev->real_num_tx_queues != 1) {
392 const struct net_device_ops *ops = dev->netdev_ops;
393 if (ops->ndo_select_queue)
394 queue_index = ops->ndo_select_queue(dev, skb, accel_priv,
395 __netdev_pick_tx);
396 else
397 queue_index = __netdev_pick_tx(dev, skb);
398
399 if (!accel_priv)
400 queue_index = netdev_cap_txqueue(dev, queue_index);
401 }
402
403 skb_set_queue_mapping(skb, queue_index);
404 return netdev_get_tx_queue(dev, queue_index);
405}
1// SPDX-License-Identifier: GPL-2.0-only
2#include <linux/kernel.h>
3#include <linux/skbuff.h>
4#include <linux/export.h>
5#include <linux/ip.h>
6#include <linux/ipv6.h>
7#include <linux/if_vlan.h>
8#include <net/dsa.h>
9#include <net/dst_metadata.h>
10#include <net/ip.h>
11#include <net/ipv6.h>
12#include <net/gre.h>
13#include <net/pptp.h>
14#include <net/tipc.h>
15#include <linux/igmp.h>
16#include <linux/icmp.h>
17#include <linux/sctp.h>
18#include <linux/dccp.h>
19#include <linux/if_tunnel.h>
20#include <linux/if_pppox.h>
21#include <linux/ppp_defs.h>
22#include <linux/stddef.h>
23#include <linux/if_ether.h>
24#include <linux/mpls.h>
25#include <linux/tcp.h>
26#include <net/flow_dissector.h>
27#include <scsi/fc/fc_fcoe.h>
28#include <uapi/linux/batadv_packet.h>
29#include <linux/bpf.h>
30#if IS_ENABLED(CONFIG_NF_CONNTRACK)
31#include <net/netfilter/nf_conntrack_core.h>
32#include <net/netfilter/nf_conntrack_labels.h>
33#endif
34#include <linux/bpf-netns.h>
35
36static void dissector_set_key(struct flow_dissector *flow_dissector,
37 enum flow_dissector_key_id key_id)
38{
39 flow_dissector->used_keys |= (1 << key_id);
40}
41
42void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
43 const struct flow_dissector_key *key,
44 unsigned int key_count)
45{
46 unsigned int i;
47
48 memset(flow_dissector, 0, sizeof(*flow_dissector));
49
50 for (i = 0; i < key_count; i++, key++) {
51 /* User should make sure that every key target offset is withing
52 * boundaries of unsigned short.
53 */
54 BUG_ON(key->offset > USHRT_MAX);
55 BUG_ON(dissector_uses_key(flow_dissector,
56 key->key_id));
57
58 dissector_set_key(flow_dissector, key->key_id);
59 flow_dissector->offset[key->key_id] = key->offset;
60 }
61
62 /* Ensure that the dissector always includes control and basic key.
63 * That way we are able to avoid handling lack of these in fast path.
64 */
65 BUG_ON(!dissector_uses_key(flow_dissector,
66 FLOW_DISSECTOR_KEY_CONTROL));
67 BUG_ON(!dissector_uses_key(flow_dissector,
68 FLOW_DISSECTOR_KEY_BASIC));
69}
70EXPORT_SYMBOL(skb_flow_dissector_init);
71
72#ifdef CONFIG_BPF_SYSCALL
73int flow_dissector_bpf_prog_attach_check(struct net *net,
74 struct bpf_prog *prog)
75{
76 enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR;
77
78 if (net == &init_net) {
79 /* BPF flow dissector in the root namespace overrides
80 * any per-net-namespace one. When attaching to root,
81 * make sure we don't have any BPF program attached
82 * to the non-root namespaces.
83 */
84 struct net *ns;
85
86 for_each_net(ns) {
87 if (ns == &init_net)
88 continue;
89 if (rcu_access_pointer(ns->bpf.run_array[type]))
90 return -EEXIST;
91 }
92 } else {
93 /* Make sure root flow dissector is not attached
94 * when attaching to the non-root namespace.
95 */
96 if (rcu_access_pointer(init_net.bpf.run_array[type]))
97 return -EEXIST;
98 }
99
100 return 0;
101}
102#endif /* CONFIG_BPF_SYSCALL */
103
104/**
105 * __skb_flow_get_ports - extract the upper layer ports and return them
106 * @skb: sk_buff to extract the ports from
107 * @thoff: transport header offset
108 * @ip_proto: protocol for which to get port offset
109 * @data: raw buffer pointer to the packet, if NULL use skb->data
110 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
111 *
112 * The function will try to retrieve the ports at offset thoff + poff where poff
113 * is the protocol port offset returned from proto_ports_offset
114 */
115__be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
116 void *data, int hlen)
117{
118 int poff = proto_ports_offset(ip_proto);
119
120 if (!data) {
121 data = skb->data;
122 hlen = skb_headlen(skb);
123 }
124
125 if (poff >= 0) {
126 __be32 *ports, _ports;
127
128 ports = __skb_header_pointer(skb, thoff + poff,
129 sizeof(_ports), data, hlen, &_ports);
130 if (ports)
131 return *ports;
132 }
133
134 return 0;
135}
136EXPORT_SYMBOL(__skb_flow_get_ports);
137
138static bool icmp_has_id(u8 type)
139{
140 switch (type) {
141 case ICMP_ECHO:
142 case ICMP_ECHOREPLY:
143 case ICMP_TIMESTAMP:
144 case ICMP_TIMESTAMPREPLY:
145 case ICMPV6_ECHO_REQUEST:
146 case ICMPV6_ECHO_REPLY:
147 return true;
148 }
149
150 return false;
151}
152
153/**
154 * skb_flow_get_icmp_tci - extract ICMP(6) Type, Code and Identifier fields
155 * @skb: sk_buff to extract from
156 * @key_icmp: struct flow_dissector_key_icmp to fill
157 * @data: raw buffer pointer to the packet
158 * @thoff: offset to extract at
159 * @hlen: packet header length
160 */
161void skb_flow_get_icmp_tci(const struct sk_buff *skb,
162 struct flow_dissector_key_icmp *key_icmp,
163 void *data, int thoff, int hlen)
164{
165 struct icmphdr *ih, _ih;
166
167 ih = __skb_header_pointer(skb, thoff, sizeof(_ih), data, hlen, &_ih);
168 if (!ih)
169 return;
170
171 key_icmp->type = ih->type;
172 key_icmp->code = ih->code;
173
174 /* As we use 0 to signal that the Id field is not present,
175 * avoid confusion with packets without such field
176 */
177 if (icmp_has_id(ih->type))
178 key_icmp->id = ih->un.echo.id ? : 1;
179 else
180 key_icmp->id = 0;
181}
182EXPORT_SYMBOL(skb_flow_get_icmp_tci);
183
184/* If FLOW_DISSECTOR_KEY_ICMP is set, dissect an ICMP packet
185 * using skb_flow_get_icmp_tci().
186 */
187static void __skb_flow_dissect_icmp(const struct sk_buff *skb,
188 struct flow_dissector *flow_dissector,
189 void *target_container,
190 void *data, int thoff, int hlen)
191{
192 struct flow_dissector_key_icmp *key_icmp;
193
194 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ICMP))
195 return;
196
197 key_icmp = skb_flow_dissector_target(flow_dissector,
198 FLOW_DISSECTOR_KEY_ICMP,
199 target_container);
200
201 skb_flow_get_icmp_tci(skb, key_icmp, data, thoff, hlen);
202}
203
204void skb_flow_dissect_meta(const struct sk_buff *skb,
205 struct flow_dissector *flow_dissector,
206 void *target_container)
207{
208 struct flow_dissector_key_meta *meta;
209
210 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_META))
211 return;
212
213 meta = skb_flow_dissector_target(flow_dissector,
214 FLOW_DISSECTOR_KEY_META,
215 target_container);
216 meta->ingress_ifindex = skb->skb_iif;
217}
218EXPORT_SYMBOL(skb_flow_dissect_meta);
219
220static void
221skb_flow_dissect_set_enc_addr_type(enum flow_dissector_key_id type,
222 struct flow_dissector *flow_dissector,
223 void *target_container)
224{
225 struct flow_dissector_key_control *ctrl;
226
227 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL))
228 return;
229
230 ctrl = skb_flow_dissector_target(flow_dissector,
231 FLOW_DISSECTOR_KEY_ENC_CONTROL,
232 target_container);
233 ctrl->addr_type = type;
234}
235
236void
237skb_flow_dissect_ct(const struct sk_buff *skb,
238 struct flow_dissector *flow_dissector,
239 void *target_container,
240 u16 *ctinfo_map,
241 size_t mapsize)
242{
243#if IS_ENABLED(CONFIG_NF_CONNTRACK)
244 struct flow_dissector_key_ct *key;
245 enum ip_conntrack_info ctinfo;
246 struct nf_conn_labels *cl;
247 struct nf_conn *ct;
248
249 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_CT))
250 return;
251
252 ct = nf_ct_get(skb, &ctinfo);
253 if (!ct)
254 return;
255
256 key = skb_flow_dissector_target(flow_dissector,
257 FLOW_DISSECTOR_KEY_CT,
258 target_container);
259
260 if (ctinfo < mapsize)
261 key->ct_state = ctinfo_map[ctinfo];
262#if IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)
263 key->ct_zone = ct->zone.id;
264#endif
265#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
266 key->ct_mark = ct->mark;
267#endif
268
269 cl = nf_ct_labels_find(ct);
270 if (cl)
271 memcpy(key->ct_labels, cl->bits, sizeof(key->ct_labels));
272#endif /* CONFIG_NF_CONNTRACK */
273}
274EXPORT_SYMBOL(skb_flow_dissect_ct);
275
276void
277skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
278 struct flow_dissector *flow_dissector,
279 void *target_container)
280{
281 struct ip_tunnel_info *info;
282 struct ip_tunnel_key *key;
283
284 /* A quick check to see if there might be something to do. */
285 if (!dissector_uses_key(flow_dissector,
286 FLOW_DISSECTOR_KEY_ENC_KEYID) &&
287 !dissector_uses_key(flow_dissector,
288 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) &&
289 !dissector_uses_key(flow_dissector,
290 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) &&
291 !dissector_uses_key(flow_dissector,
292 FLOW_DISSECTOR_KEY_ENC_CONTROL) &&
293 !dissector_uses_key(flow_dissector,
294 FLOW_DISSECTOR_KEY_ENC_PORTS) &&
295 !dissector_uses_key(flow_dissector,
296 FLOW_DISSECTOR_KEY_ENC_IP) &&
297 !dissector_uses_key(flow_dissector,
298 FLOW_DISSECTOR_KEY_ENC_OPTS))
299 return;
300
301 info = skb_tunnel_info(skb);
302 if (!info)
303 return;
304
305 key = &info->key;
306
307 switch (ip_tunnel_info_af(info)) {
308 case AF_INET:
309 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV4_ADDRS,
310 flow_dissector,
311 target_container);
312 if (dissector_uses_key(flow_dissector,
313 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
314 struct flow_dissector_key_ipv4_addrs *ipv4;
315
316 ipv4 = skb_flow_dissector_target(flow_dissector,
317 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
318 target_container);
319 ipv4->src = key->u.ipv4.src;
320 ipv4->dst = key->u.ipv4.dst;
321 }
322 break;
323 case AF_INET6:
324 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV6_ADDRS,
325 flow_dissector,
326 target_container);
327 if (dissector_uses_key(flow_dissector,
328 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) {
329 struct flow_dissector_key_ipv6_addrs *ipv6;
330
331 ipv6 = skb_flow_dissector_target(flow_dissector,
332 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
333 target_container);
334 ipv6->src = key->u.ipv6.src;
335 ipv6->dst = key->u.ipv6.dst;
336 }
337 break;
338 }
339
340 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
341 struct flow_dissector_key_keyid *keyid;
342
343 keyid = skb_flow_dissector_target(flow_dissector,
344 FLOW_DISSECTOR_KEY_ENC_KEYID,
345 target_container);
346 keyid->keyid = tunnel_id_to_key32(key->tun_id);
347 }
348
349 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
350 struct flow_dissector_key_ports *tp;
351
352 tp = skb_flow_dissector_target(flow_dissector,
353 FLOW_DISSECTOR_KEY_ENC_PORTS,
354 target_container);
355 tp->src = key->tp_src;
356 tp->dst = key->tp_dst;
357 }
358
359 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_IP)) {
360 struct flow_dissector_key_ip *ip;
361
362 ip = skb_flow_dissector_target(flow_dissector,
363 FLOW_DISSECTOR_KEY_ENC_IP,
364 target_container);
365 ip->tos = key->tos;
366 ip->ttl = key->ttl;
367 }
368
369 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
370 struct flow_dissector_key_enc_opts *enc_opt;
371
372 enc_opt = skb_flow_dissector_target(flow_dissector,
373 FLOW_DISSECTOR_KEY_ENC_OPTS,
374 target_container);
375
376 if (info->options_len) {
377 enc_opt->len = info->options_len;
378 ip_tunnel_info_opts_get(enc_opt->data, info);
379 enc_opt->dst_opt_type = info->key.tun_flags &
380 TUNNEL_OPTIONS_PRESENT;
381 }
382 }
383}
384EXPORT_SYMBOL(skb_flow_dissect_tunnel_info);
385
386void skb_flow_dissect_hash(const struct sk_buff *skb,
387 struct flow_dissector *flow_dissector,
388 void *target_container)
389{
390 struct flow_dissector_key_hash *key;
391
392 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_HASH))
393 return;
394
395 key = skb_flow_dissector_target(flow_dissector,
396 FLOW_DISSECTOR_KEY_HASH,
397 target_container);
398
399 key->hash = skb_get_hash_raw(skb);
400}
401EXPORT_SYMBOL(skb_flow_dissect_hash);
402
403static enum flow_dissect_ret
404__skb_flow_dissect_mpls(const struct sk_buff *skb,
405 struct flow_dissector *flow_dissector,
406 void *target_container, void *data, int nhoff, int hlen,
407 int lse_index, bool *entropy_label)
408{
409 struct mpls_label *hdr, _hdr;
410 u32 entry, label, bos;
411
412 if (!dissector_uses_key(flow_dissector,
413 FLOW_DISSECTOR_KEY_MPLS_ENTROPY) &&
414 !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS))
415 return FLOW_DISSECT_RET_OUT_GOOD;
416
417 if (lse_index >= FLOW_DIS_MPLS_MAX)
418 return FLOW_DISSECT_RET_OUT_GOOD;
419
420 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
421 hlen, &_hdr);
422 if (!hdr)
423 return FLOW_DISSECT_RET_OUT_BAD;
424
425 entry = ntohl(hdr->entry);
426 label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
427 bos = (entry & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT;
428
429 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) {
430 struct flow_dissector_key_mpls *key_mpls;
431 struct flow_dissector_mpls_lse *lse;
432
433 key_mpls = skb_flow_dissector_target(flow_dissector,
434 FLOW_DISSECTOR_KEY_MPLS,
435 target_container);
436 lse = &key_mpls->ls[lse_index];
437
438 lse->mpls_ttl = (entry & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
439 lse->mpls_bos = bos;
440 lse->mpls_tc = (entry & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT;
441 lse->mpls_label = label;
442 dissector_set_mpls_lse(key_mpls, lse_index);
443 }
444
445 if (*entropy_label &&
446 dissector_uses_key(flow_dissector,
447 FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) {
448 struct flow_dissector_key_keyid *key_keyid;
449
450 key_keyid = skb_flow_dissector_target(flow_dissector,
451 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
452 target_container);
453 key_keyid->keyid = cpu_to_be32(label);
454 }
455
456 *entropy_label = label == MPLS_LABEL_ENTROPY;
457
458 return bos ? FLOW_DISSECT_RET_OUT_GOOD : FLOW_DISSECT_RET_PROTO_AGAIN;
459}
460
461static enum flow_dissect_ret
462__skb_flow_dissect_arp(const struct sk_buff *skb,
463 struct flow_dissector *flow_dissector,
464 void *target_container, void *data, int nhoff, int hlen)
465{
466 struct flow_dissector_key_arp *key_arp;
467 struct {
468 unsigned char ar_sha[ETH_ALEN];
469 unsigned char ar_sip[4];
470 unsigned char ar_tha[ETH_ALEN];
471 unsigned char ar_tip[4];
472 } *arp_eth, _arp_eth;
473 const struct arphdr *arp;
474 struct arphdr _arp;
475
476 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP))
477 return FLOW_DISSECT_RET_OUT_GOOD;
478
479 arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data,
480 hlen, &_arp);
481 if (!arp)
482 return FLOW_DISSECT_RET_OUT_BAD;
483
484 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
485 arp->ar_pro != htons(ETH_P_IP) ||
486 arp->ar_hln != ETH_ALEN ||
487 arp->ar_pln != 4 ||
488 (arp->ar_op != htons(ARPOP_REPLY) &&
489 arp->ar_op != htons(ARPOP_REQUEST)))
490 return FLOW_DISSECT_RET_OUT_BAD;
491
492 arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp),
493 sizeof(_arp_eth), data,
494 hlen, &_arp_eth);
495 if (!arp_eth)
496 return FLOW_DISSECT_RET_OUT_BAD;
497
498 key_arp = skb_flow_dissector_target(flow_dissector,
499 FLOW_DISSECTOR_KEY_ARP,
500 target_container);
501
502 memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip));
503 memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip));
504
505 /* Only store the lower byte of the opcode;
506 * this covers ARPOP_REPLY and ARPOP_REQUEST.
507 */
508 key_arp->op = ntohs(arp->ar_op) & 0xff;
509
510 ether_addr_copy(key_arp->sha, arp_eth->ar_sha);
511 ether_addr_copy(key_arp->tha, arp_eth->ar_tha);
512
513 return FLOW_DISSECT_RET_OUT_GOOD;
514}
515
516static enum flow_dissect_ret
517__skb_flow_dissect_gre(const struct sk_buff *skb,
518 struct flow_dissector_key_control *key_control,
519 struct flow_dissector *flow_dissector,
520 void *target_container, void *data,
521 __be16 *p_proto, int *p_nhoff, int *p_hlen,
522 unsigned int flags)
523{
524 struct flow_dissector_key_keyid *key_keyid;
525 struct gre_base_hdr *hdr, _hdr;
526 int offset = 0;
527 u16 gre_ver;
528
529 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr),
530 data, *p_hlen, &_hdr);
531 if (!hdr)
532 return FLOW_DISSECT_RET_OUT_BAD;
533
534 /* Only look inside GRE without routing */
535 if (hdr->flags & GRE_ROUTING)
536 return FLOW_DISSECT_RET_OUT_GOOD;
537
538 /* Only look inside GRE for version 0 and 1 */
539 gre_ver = ntohs(hdr->flags & GRE_VERSION);
540 if (gre_ver > 1)
541 return FLOW_DISSECT_RET_OUT_GOOD;
542
543 *p_proto = hdr->protocol;
544 if (gre_ver) {
545 /* Version1 must be PPTP, and check the flags */
546 if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
547 return FLOW_DISSECT_RET_OUT_GOOD;
548 }
549
550 offset += sizeof(struct gre_base_hdr);
551
552 if (hdr->flags & GRE_CSUM)
553 offset += sizeof_field(struct gre_full_hdr, csum) +
554 sizeof_field(struct gre_full_hdr, reserved1);
555
556 if (hdr->flags & GRE_KEY) {
557 const __be32 *keyid;
558 __be32 _keyid;
559
560 keyid = __skb_header_pointer(skb, *p_nhoff + offset,
561 sizeof(_keyid),
562 data, *p_hlen, &_keyid);
563 if (!keyid)
564 return FLOW_DISSECT_RET_OUT_BAD;
565
566 if (dissector_uses_key(flow_dissector,
567 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
568 key_keyid = skb_flow_dissector_target(flow_dissector,
569 FLOW_DISSECTOR_KEY_GRE_KEYID,
570 target_container);
571 if (gre_ver == 0)
572 key_keyid->keyid = *keyid;
573 else
574 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
575 }
576 offset += sizeof_field(struct gre_full_hdr, key);
577 }
578
579 if (hdr->flags & GRE_SEQ)
580 offset += sizeof_field(struct pptp_gre_header, seq);
581
582 if (gre_ver == 0) {
583 if (*p_proto == htons(ETH_P_TEB)) {
584 const struct ethhdr *eth;
585 struct ethhdr _eth;
586
587 eth = __skb_header_pointer(skb, *p_nhoff + offset,
588 sizeof(_eth),
589 data, *p_hlen, &_eth);
590 if (!eth)
591 return FLOW_DISSECT_RET_OUT_BAD;
592 *p_proto = eth->h_proto;
593 offset += sizeof(*eth);
594
595 /* Cap headers that we access via pointers at the
596 * end of the Ethernet header as our maximum alignment
597 * at that point is only 2 bytes.
598 */
599 if (NET_IP_ALIGN)
600 *p_hlen = *p_nhoff + offset;
601 }
602 } else { /* version 1, must be PPTP */
603 u8 _ppp_hdr[PPP_HDRLEN];
604 u8 *ppp_hdr;
605
606 if (hdr->flags & GRE_ACK)
607 offset += sizeof_field(struct pptp_gre_header, ack);
608
609 ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset,
610 sizeof(_ppp_hdr),
611 data, *p_hlen, _ppp_hdr);
612 if (!ppp_hdr)
613 return FLOW_DISSECT_RET_OUT_BAD;
614
615 switch (PPP_PROTOCOL(ppp_hdr)) {
616 case PPP_IP:
617 *p_proto = htons(ETH_P_IP);
618 break;
619 case PPP_IPV6:
620 *p_proto = htons(ETH_P_IPV6);
621 break;
622 default:
623 /* Could probably catch some more like MPLS */
624 break;
625 }
626
627 offset += PPP_HDRLEN;
628 }
629
630 *p_nhoff += offset;
631 key_control->flags |= FLOW_DIS_ENCAPSULATION;
632 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
633 return FLOW_DISSECT_RET_OUT_GOOD;
634
635 return FLOW_DISSECT_RET_PROTO_AGAIN;
636}
637
638/**
639 * __skb_flow_dissect_batadv() - dissect batman-adv header
640 * @skb: sk_buff to with the batman-adv header
641 * @key_control: flow dissectors control key
642 * @data: raw buffer pointer to the packet, if NULL use skb->data
643 * @p_proto: pointer used to update the protocol to process next
644 * @p_nhoff: pointer used to update inner network header offset
645 * @hlen: packet header length
646 * @flags: any combination of FLOW_DISSECTOR_F_*
647 *
648 * ETH_P_BATMAN packets are tried to be dissected. Only
649 * &struct batadv_unicast packets are actually processed because they contain an
650 * inner ethernet header and are usually followed by actual network header. This
651 * allows the flow dissector to continue processing the packet.
652 *
653 * Return: FLOW_DISSECT_RET_PROTO_AGAIN when &struct batadv_unicast was found,
654 * FLOW_DISSECT_RET_OUT_GOOD when dissector should stop after encapsulation,
655 * otherwise FLOW_DISSECT_RET_OUT_BAD
656 */
657static enum flow_dissect_ret
658__skb_flow_dissect_batadv(const struct sk_buff *skb,
659 struct flow_dissector_key_control *key_control,
660 void *data, __be16 *p_proto, int *p_nhoff, int hlen,
661 unsigned int flags)
662{
663 struct {
664 struct batadv_unicast_packet batadv_unicast;
665 struct ethhdr eth;
666 } *hdr, _hdr;
667
668 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), data, hlen,
669 &_hdr);
670 if (!hdr)
671 return FLOW_DISSECT_RET_OUT_BAD;
672
673 if (hdr->batadv_unicast.version != BATADV_COMPAT_VERSION)
674 return FLOW_DISSECT_RET_OUT_BAD;
675
676 if (hdr->batadv_unicast.packet_type != BATADV_UNICAST)
677 return FLOW_DISSECT_RET_OUT_BAD;
678
679 *p_proto = hdr->eth.h_proto;
680 *p_nhoff += sizeof(*hdr);
681
682 key_control->flags |= FLOW_DIS_ENCAPSULATION;
683 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
684 return FLOW_DISSECT_RET_OUT_GOOD;
685
686 return FLOW_DISSECT_RET_PROTO_AGAIN;
687}
688
689static void
690__skb_flow_dissect_tcp(const struct sk_buff *skb,
691 struct flow_dissector *flow_dissector,
692 void *target_container, void *data, int thoff, int hlen)
693{
694 struct flow_dissector_key_tcp *key_tcp;
695 struct tcphdr *th, _th;
696
697 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP))
698 return;
699
700 th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th);
701 if (!th)
702 return;
703
704 if (unlikely(__tcp_hdrlen(th) < sizeof(_th)))
705 return;
706
707 key_tcp = skb_flow_dissector_target(flow_dissector,
708 FLOW_DISSECTOR_KEY_TCP,
709 target_container);
710 key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF));
711}
712
713static void
714__skb_flow_dissect_ports(const struct sk_buff *skb,
715 struct flow_dissector *flow_dissector,
716 void *target_container, void *data, int nhoff,
717 u8 ip_proto, int hlen)
718{
719 enum flow_dissector_key_id dissector_ports = FLOW_DISSECTOR_KEY_MAX;
720 struct flow_dissector_key_ports *key_ports;
721
722 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS))
723 dissector_ports = FLOW_DISSECTOR_KEY_PORTS;
724 else if (dissector_uses_key(flow_dissector,
725 FLOW_DISSECTOR_KEY_PORTS_RANGE))
726 dissector_ports = FLOW_DISSECTOR_KEY_PORTS_RANGE;
727
728 if (dissector_ports == FLOW_DISSECTOR_KEY_MAX)
729 return;
730
731 key_ports = skb_flow_dissector_target(flow_dissector,
732 dissector_ports,
733 target_container);
734 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
735 data, hlen);
736}
737
738static void
739__skb_flow_dissect_ipv4(const struct sk_buff *skb,
740 struct flow_dissector *flow_dissector,
741 void *target_container, void *data, const struct iphdr *iph)
742{
743 struct flow_dissector_key_ip *key_ip;
744
745 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
746 return;
747
748 key_ip = skb_flow_dissector_target(flow_dissector,
749 FLOW_DISSECTOR_KEY_IP,
750 target_container);
751 key_ip->tos = iph->tos;
752 key_ip->ttl = iph->ttl;
753}
754
755static void
756__skb_flow_dissect_ipv6(const struct sk_buff *skb,
757 struct flow_dissector *flow_dissector,
758 void *target_container, void *data, const struct ipv6hdr *iph)
759{
760 struct flow_dissector_key_ip *key_ip;
761
762 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
763 return;
764
765 key_ip = skb_flow_dissector_target(flow_dissector,
766 FLOW_DISSECTOR_KEY_IP,
767 target_container);
768 key_ip->tos = ipv6_get_dsfield(iph);
769 key_ip->ttl = iph->hop_limit;
770}
771
772/* Maximum number of protocol headers that can be parsed in
773 * __skb_flow_dissect
774 */
775#define MAX_FLOW_DISSECT_HDRS 15
776
777static bool skb_flow_dissect_allowed(int *num_hdrs)
778{
779 ++*num_hdrs;
780
781 return (*num_hdrs <= MAX_FLOW_DISSECT_HDRS);
782}
783
784static void __skb_flow_bpf_to_target(const struct bpf_flow_keys *flow_keys,
785 struct flow_dissector *flow_dissector,
786 void *target_container)
787{
788 struct flow_dissector_key_ports *key_ports = NULL;
789 struct flow_dissector_key_control *key_control;
790 struct flow_dissector_key_basic *key_basic;
791 struct flow_dissector_key_addrs *key_addrs;
792 struct flow_dissector_key_tags *key_tags;
793
794 key_control = skb_flow_dissector_target(flow_dissector,
795 FLOW_DISSECTOR_KEY_CONTROL,
796 target_container);
797 key_control->thoff = flow_keys->thoff;
798 if (flow_keys->is_frag)
799 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
800 if (flow_keys->is_first_frag)
801 key_control->flags |= FLOW_DIS_FIRST_FRAG;
802 if (flow_keys->is_encap)
803 key_control->flags |= FLOW_DIS_ENCAPSULATION;
804
805 key_basic = skb_flow_dissector_target(flow_dissector,
806 FLOW_DISSECTOR_KEY_BASIC,
807 target_container);
808 key_basic->n_proto = flow_keys->n_proto;
809 key_basic->ip_proto = flow_keys->ip_proto;
810
811 if (flow_keys->addr_proto == ETH_P_IP &&
812 dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
813 key_addrs = skb_flow_dissector_target(flow_dissector,
814 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
815 target_container);
816 key_addrs->v4addrs.src = flow_keys->ipv4_src;
817 key_addrs->v4addrs.dst = flow_keys->ipv4_dst;
818 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
819 } else if (flow_keys->addr_proto == ETH_P_IPV6 &&
820 dissector_uses_key(flow_dissector,
821 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
822 key_addrs = skb_flow_dissector_target(flow_dissector,
823 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
824 target_container);
825 memcpy(&key_addrs->v6addrs, &flow_keys->ipv6_src,
826 sizeof(key_addrs->v6addrs));
827 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
828 }
829
830 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS))
831 key_ports = skb_flow_dissector_target(flow_dissector,
832 FLOW_DISSECTOR_KEY_PORTS,
833 target_container);
834 else if (dissector_uses_key(flow_dissector,
835 FLOW_DISSECTOR_KEY_PORTS_RANGE))
836 key_ports = skb_flow_dissector_target(flow_dissector,
837 FLOW_DISSECTOR_KEY_PORTS_RANGE,
838 target_container);
839
840 if (key_ports) {
841 key_ports->src = flow_keys->sport;
842 key_ports->dst = flow_keys->dport;
843 }
844
845 if (dissector_uses_key(flow_dissector,
846 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
847 key_tags = skb_flow_dissector_target(flow_dissector,
848 FLOW_DISSECTOR_KEY_FLOW_LABEL,
849 target_container);
850 key_tags->flow_label = ntohl(flow_keys->flow_label);
851 }
852}
853
854bool bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx,
855 __be16 proto, int nhoff, int hlen, unsigned int flags)
856{
857 struct bpf_flow_keys *flow_keys = ctx->flow_keys;
858 u32 result;
859
860 /* Pass parameters to the BPF program */
861 memset(flow_keys, 0, sizeof(*flow_keys));
862 flow_keys->n_proto = proto;
863 flow_keys->nhoff = nhoff;
864 flow_keys->thoff = flow_keys->nhoff;
865
866 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG !=
867 (int)FLOW_DISSECTOR_F_PARSE_1ST_FRAG);
868 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL !=
869 (int)FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
870 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP !=
871 (int)FLOW_DISSECTOR_F_STOP_AT_ENCAP);
872 flow_keys->flags = flags;
873
874 result = bpf_prog_run_pin_on_cpu(prog, ctx);
875
876 flow_keys->nhoff = clamp_t(u16, flow_keys->nhoff, nhoff, hlen);
877 flow_keys->thoff = clamp_t(u16, flow_keys->thoff,
878 flow_keys->nhoff, hlen);
879
880 return result == BPF_OK;
881}
882
883/**
884 * __skb_flow_dissect - extract the flow_keys struct and return it
885 * @net: associated network namespace, derived from @skb if NULL
886 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
887 * @flow_dissector: list of keys to dissect
888 * @target_container: target structure to put dissected values into
889 * @data: raw buffer pointer to the packet, if NULL use skb->data
890 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
891 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
892 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
893 * @flags: flags that control the dissection process, e.g.
894 * FLOW_DISSECTOR_F_STOP_AT_ENCAP.
895 *
896 * The function will try to retrieve individual keys into target specified
897 * by flow_dissector from either the skbuff or a raw buffer specified by the
898 * rest parameters.
899 *
900 * Caller must take care of zeroing target container memory.
901 */
902bool __skb_flow_dissect(const struct net *net,
903 const struct sk_buff *skb,
904 struct flow_dissector *flow_dissector,
905 void *target_container,
906 void *data, __be16 proto, int nhoff, int hlen,
907 unsigned int flags)
908{
909 struct flow_dissector_key_control *key_control;
910 struct flow_dissector_key_basic *key_basic;
911 struct flow_dissector_key_addrs *key_addrs;
912 struct flow_dissector_key_tags *key_tags;
913 struct flow_dissector_key_vlan *key_vlan;
914 enum flow_dissect_ret fdret;
915 enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
916 bool mpls_el = false;
917 int mpls_lse = 0;
918 int num_hdrs = 0;
919 u8 ip_proto = 0;
920 bool ret;
921
922 if (!data) {
923 data = skb->data;
924 proto = skb_vlan_tag_present(skb) ?
925 skb->vlan_proto : skb->protocol;
926 nhoff = skb_network_offset(skb);
927 hlen = skb_headlen(skb);
928#if IS_ENABLED(CONFIG_NET_DSA)
929 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev) &&
930 proto == htons(ETH_P_XDSA))) {
931 const struct dsa_device_ops *ops;
932 int offset = 0;
933
934 ops = skb->dev->dsa_ptr->tag_ops;
935 if (ops->flow_dissect &&
936 !ops->flow_dissect(skb, &proto, &offset)) {
937 hlen -= offset;
938 nhoff += offset;
939 }
940 }
941#endif
942 }
943
944 /* It is ensured by skb_flow_dissector_init() that control key will
945 * be always present.
946 */
947 key_control = skb_flow_dissector_target(flow_dissector,
948 FLOW_DISSECTOR_KEY_CONTROL,
949 target_container);
950
951 /* It is ensured by skb_flow_dissector_init() that basic key will
952 * be always present.
953 */
954 key_basic = skb_flow_dissector_target(flow_dissector,
955 FLOW_DISSECTOR_KEY_BASIC,
956 target_container);
957
958 if (skb) {
959 if (!net) {
960 if (skb->dev)
961 net = dev_net(skb->dev);
962 else if (skb->sk)
963 net = sock_net(skb->sk);
964 }
965 }
966
967 WARN_ON_ONCE(!net);
968 if (net) {
969 enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR;
970 struct bpf_prog_array *run_array;
971
972 rcu_read_lock();
973 run_array = rcu_dereference(init_net.bpf.run_array[type]);
974 if (!run_array)
975 run_array = rcu_dereference(net->bpf.run_array[type]);
976
977 if (run_array) {
978 struct bpf_flow_keys flow_keys;
979 struct bpf_flow_dissector ctx = {
980 .flow_keys = &flow_keys,
981 .data = data,
982 .data_end = data + hlen,
983 };
984 __be16 n_proto = proto;
985 struct bpf_prog *prog;
986
987 if (skb) {
988 ctx.skb = skb;
989 /* we can't use 'proto' in the skb case
990 * because it might be set to skb->vlan_proto
991 * which has been pulled from the data
992 */
993 n_proto = skb->protocol;
994 }
995
996 prog = READ_ONCE(run_array->items[0].prog);
997 ret = bpf_flow_dissect(prog, &ctx, n_proto, nhoff,
998 hlen, flags);
999 __skb_flow_bpf_to_target(&flow_keys, flow_dissector,
1000 target_container);
1001 rcu_read_unlock();
1002 return ret;
1003 }
1004 rcu_read_unlock();
1005 }
1006
1007 if (dissector_uses_key(flow_dissector,
1008 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1009 struct ethhdr *eth = eth_hdr(skb);
1010 struct flow_dissector_key_eth_addrs *key_eth_addrs;
1011
1012 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
1013 FLOW_DISSECTOR_KEY_ETH_ADDRS,
1014 target_container);
1015 memcpy(key_eth_addrs, ð->h_dest, sizeof(*key_eth_addrs));
1016 }
1017
1018proto_again:
1019 fdret = FLOW_DISSECT_RET_CONTINUE;
1020
1021 switch (proto) {
1022 case htons(ETH_P_IP): {
1023 const struct iphdr *iph;
1024 struct iphdr _iph;
1025
1026 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1027 if (!iph || iph->ihl < 5) {
1028 fdret = FLOW_DISSECT_RET_OUT_BAD;
1029 break;
1030 }
1031
1032 nhoff += iph->ihl * 4;
1033
1034 ip_proto = iph->protocol;
1035
1036 if (dissector_uses_key(flow_dissector,
1037 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
1038 key_addrs = skb_flow_dissector_target(flow_dissector,
1039 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1040 target_container);
1041
1042 memcpy(&key_addrs->v4addrs, &iph->saddr,
1043 sizeof(key_addrs->v4addrs));
1044 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1045 }
1046
1047 if (ip_is_fragment(iph)) {
1048 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1049
1050 if (iph->frag_off & htons(IP_OFFSET)) {
1051 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1052 break;
1053 } else {
1054 key_control->flags |= FLOW_DIS_FIRST_FRAG;
1055 if (!(flags &
1056 FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) {
1057 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1058 break;
1059 }
1060 }
1061 }
1062
1063 __skb_flow_dissect_ipv4(skb, flow_dissector,
1064 target_container, data, iph);
1065
1066 break;
1067 }
1068 case htons(ETH_P_IPV6): {
1069 const struct ipv6hdr *iph;
1070 struct ipv6hdr _iph;
1071
1072 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1073 if (!iph) {
1074 fdret = FLOW_DISSECT_RET_OUT_BAD;
1075 break;
1076 }
1077
1078 ip_proto = iph->nexthdr;
1079 nhoff += sizeof(struct ipv6hdr);
1080
1081 if (dissector_uses_key(flow_dissector,
1082 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
1083 key_addrs = skb_flow_dissector_target(flow_dissector,
1084 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1085 target_container);
1086
1087 memcpy(&key_addrs->v6addrs, &iph->saddr,
1088 sizeof(key_addrs->v6addrs));
1089 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1090 }
1091
1092 if ((dissector_uses_key(flow_dissector,
1093 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
1094 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
1095 ip6_flowlabel(iph)) {
1096 __be32 flow_label = ip6_flowlabel(iph);
1097
1098 if (dissector_uses_key(flow_dissector,
1099 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
1100 key_tags = skb_flow_dissector_target(flow_dissector,
1101 FLOW_DISSECTOR_KEY_FLOW_LABEL,
1102 target_container);
1103 key_tags->flow_label = ntohl(flow_label);
1104 }
1105 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) {
1106 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1107 break;
1108 }
1109 }
1110
1111 __skb_flow_dissect_ipv6(skb, flow_dissector,
1112 target_container, data, iph);
1113
1114 break;
1115 }
1116 case htons(ETH_P_8021AD):
1117 case htons(ETH_P_8021Q): {
1118 const struct vlan_hdr *vlan = NULL;
1119 struct vlan_hdr _vlan;
1120 __be16 saved_vlan_tpid = proto;
1121
1122 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX &&
1123 skb && skb_vlan_tag_present(skb)) {
1124 proto = skb->protocol;
1125 } else {
1126 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
1127 data, hlen, &_vlan);
1128 if (!vlan) {
1129 fdret = FLOW_DISSECT_RET_OUT_BAD;
1130 break;
1131 }
1132
1133 proto = vlan->h_vlan_encapsulated_proto;
1134 nhoff += sizeof(*vlan);
1135 }
1136
1137 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX) {
1138 dissector_vlan = FLOW_DISSECTOR_KEY_VLAN;
1139 } else if (dissector_vlan == FLOW_DISSECTOR_KEY_VLAN) {
1140 dissector_vlan = FLOW_DISSECTOR_KEY_CVLAN;
1141 } else {
1142 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1143 break;
1144 }
1145
1146 if (dissector_uses_key(flow_dissector, dissector_vlan)) {
1147 key_vlan = skb_flow_dissector_target(flow_dissector,
1148 dissector_vlan,
1149 target_container);
1150
1151 if (!vlan) {
1152 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
1153 key_vlan->vlan_priority = skb_vlan_tag_get_prio(skb);
1154 } else {
1155 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
1156 VLAN_VID_MASK;
1157 key_vlan->vlan_priority =
1158 (ntohs(vlan->h_vlan_TCI) &
1159 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
1160 }
1161 key_vlan->vlan_tpid = saved_vlan_tpid;
1162 }
1163
1164 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1165 break;
1166 }
1167 case htons(ETH_P_PPP_SES): {
1168 struct {
1169 struct pppoe_hdr hdr;
1170 __be16 proto;
1171 } *hdr, _hdr;
1172 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
1173 if (!hdr) {
1174 fdret = FLOW_DISSECT_RET_OUT_BAD;
1175 break;
1176 }
1177
1178 proto = hdr->proto;
1179 nhoff += PPPOE_SES_HLEN;
1180 switch (proto) {
1181 case htons(PPP_IP):
1182 proto = htons(ETH_P_IP);
1183 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1184 break;
1185 case htons(PPP_IPV6):
1186 proto = htons(ETH_P_IPV6);
1187 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1188 break;
1189 default:
1190 fdret = FLOW_DISSECT_RET_OUT_BAD;
1191 break;
1192 }
1193 break;
1194 }
1195 case htons(ETH_P_TIPC): {
1196 struct tipc_basic_hdr *hdr, _hdr;
1197
1198 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr),
1199 data, hlen, &_hdr);
1200 if (!hdr) {
1201 fdret = FLOW_DISSECT_RET_OUT_BAD;
1202 break;
1203 }
1204
1205 if (dissector_uses_key(flow_dissector,
1206 FLOW_DISSECTOR_KEY_TIPC)) {
1207 key_addrs = skb_flow_dissector_target(flow_dissector,
1208 FLOW_DISSECTOR_KEY_TIPC,
1209 target_container);
1210 key_addrs->tipckey.key = tipc_hdr_rps_key(hdr);
1211 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC;
1212 }
1213 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1214 break;
1215 }
1216
1217 case htons(ETH_P_MPLS_UC):
1218 case htons(ETH_P_MPLS_MC):
1219 fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
1220 target_container, data,
1221 nhoff, hlen, mpls_lse,
1222 &mpls_el);
1223 nhoff += sizeof(struct mpls_label);
1224 mpls_lse++;
1225 break;
1226 case htons(ETH_P_FCOE):
1227 if ((hlen - nhoff) < FCOE_HEADER_LEN) {
1228 fdret = FLOW_DISSECT_RET_OUT_BAD;
1229 break;
1230 }
1231
1232 nhoff += FCOE_HEADER_LEN;
1233 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1234 break;
1235
1236 case htons(ETH_P_ARP):
1237 case htons(ETH_P_RARP):
1238 fdret = __skb_flow_dissect_arp(skb, flow_dissector,
1239 target_container, data,
1240 nhoff, hlen);
1241 break;
1242
1243 case htons(ETH_P_BATMAN):
1244 fdret = __skb_flow_dissect_batadv(skb, key_control, data,
1245 &proto, &nhoff, hlen, flags);
1246 break;
1247
1248 default:
1249 fdret = FLOW_DISSECT_RET_OUT_BAD;
1250 break;
1251 }
1252
1253 /* Process result of proto processing */
1254 switch (fdret) {
1255 case FLOW_DISSECT_RET_OUT_GOOD:
1256 goto out_good;
1257 case FLOW_DISSECT_RET_PROTO_AGAIN:
1258 if (skb_flow_dissect_allowed(&num_hdrs))
1259 goto proto_again;
1260 goto out_good;
1261 case FLOW_DISSECT_RET_CONTINUE:
1262 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1263 break;
1264 case FLOW_DISSECT_RET_OUT_BAD:
1265 default:
1266 goto out_bad;
1267 }
1268
1269ip_proto_again:
1270 fdret = FLOW_DISSECT_RET_CONTINUE;
1271
1272 switch (ip_proto) {
1273 case IPPROTO_GRE:
1274 fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
1275 target_container, data,
1276 &proto, &nhoff, &hlen, flags);
1277 break;
1278
1279 case NEXTHDR_HOP:
1280 case NEXTHDR_ROUTING:
1281 case NEXTHDR_DEST: {
1282 u8 _opthdr[2], *opthdr;
1283
1284 if (proto != htons(ETH_P_IPV6))
1285 break;
1286
1287 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
1288 data, hlen, &_opthdr);
1289 if (!opthdr) {
1290 fdret = FLOW_DISSECT_RET_OUT_BAD;
1291 break;
1292 }
1293
1294 ip_proto = opthdr[0];
1295 nhoff += (opthdr[1] + 1) << 3;
1296
1297 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1298 break;
1299 }
1300 case NEXTHDR_FRAGMENT: {
1301 struct frag_hdr _fh, *fh;
1302
1303 if (proto != htons(ETH_P_IPV6))
1304 break;
1305
1306 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
1307 data, hlen, &_fh);
1308
1309 if (!fh) {
1310 fdret = FLOW_DISSECT_RET_OUT_BAD;
1311 break;
1312 }
1313
1314 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1315
1316 nhoff += sizeof(_fh);
1317 ip_proto = fh->nexthdr;
1318
1319 if (!(fh->frag_off & htons(IP6_OFFSET))) {
1320 key_control->flags |= FLOW_DIS_FIRST_FRAG;
1321 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
1322 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1323 break;
1324 }
1325 }
1326
1327 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1328 break;
1329 }
1330 case IPPROTO_IPIP:
1331 proto = htons(ETH_P_IP);
1332
1333 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1334 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1335 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1336 break;
1337 }
1338
1339 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1340 break;
1341
1342 case IPPROTO_IPV6:
1343 proto = htons(ETH_P_IPV6);
1344
1345 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1346 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1347 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1348 break;
1349 }
1350
1351 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1352 break;
1353
1354
1355 case IPPROTO_MPLS:
1356 proto = htons(ETH_P_MPLS_UC);
1357 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1358 break;
1359
1360 case IPPROTO_TCP:
1361 __skb_flow_dissect_tcp(skb, flow_dissector, target_container,
1362 data, nhoff, hlen);
1363 break;
1364
1365 case IPPROTO_ICMP:
1366 case IPPROTO_ICMPV6:
1367 __skb_flow_dissect_icmp(skb, flow_dissector, target_container,
1368 data, nhoff, hlen);
1369 break;
1370
1371 default:
1372 break;
1373 }
1374
1375 if (!(key_control->flags & FLOW_DIS_IS_FRAGMENT))
1376 __skb_flow_dissect_ports(skb, flow_dissector, target_container,
1377 data, nhoff, ip_proto, hlen);
1378
1379 /* Process result of IP proto processing */
1380 switch (fdret) {
1381 case FLOW_DISSECT_RET_PROTO_AGAIN:
1382 if (skb_flow_dissect_allowed(&num_hdrs))
1383 goto proto_again;
1384 break;
1385 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1386 if (skb_flow_dissect_allowed(&num_hdrs))
1387 goto ip_proto_again;
1388 break;
1389 case FLOW_DISSECT_RET_OUT_GOOD:
1390 case FLOW_DISSECT_RET_CONTINUE:
1391 break;
1392 case FLOW_DISSECT_RET_OUT_BAD:
1393 default:
1394 goto out_bad;
1395 }
1396
1397out_good:
1398 ret = true;
1399
1400out:
1401 key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
1402 key_basic->n_proto = proto;
1403 key_basic->ip_proto = ip_proto;
1404
1405 return ret;
1406
1407out_bad:
1408 ret = false;
1409 goto out;
1410}
1411EXPORT_SYMBOL(__skb_flow_dissect);
1412
1413static siphash_key_t hashrnd __read_mostly;
1414static __always_inline void __flow_hash_secret_init(void)
1415{
1416 net_get_random_once(&hashrnd, sizeof(hashrnd));
1417}
1418
1419static const void *flow_keys_hash_start(const struct flow_keys *flow)
1420{
1421 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT);
1422 return &flow->FLOW_KEYS_HASH_START_FIELD;
1423}
1424
1425static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
1426{
1427 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
1428
1429 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
1430
1431 switch (flow->control.addr_type) {
1432 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1433 diff -= sizeof(flow->addrs.v4addrs);
1434 break;
1435 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1436 diff -= sizeof(flow->addrs.v6addrs);
1437 break;
1438 case FLOW_DISSECTOR_KEY_TIPC:
1439 diff -= sizeof(flow->addrs.tipckey);
1440 break;
1441 }
1442 return sizeof(*flow) - diff;
1443}
1444
1445__be32 flow_get_u32_src(const struct flow_keys *flow)
1446{
1447 switch (flow->control.addr_type) {
1448 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1449 return flow->addrs.v4addrs.src;
1450 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1451 return (__force __be32)ipv6_addr_hash(
1452 &flow->addrs.v6addrs.src);
1453 case FLOW_DISSECTOR_KEY_TIPC:
1454 return flow->addrs.tipckey.key;
1455 default:
1456 return 0;
1457 }
1458}
1459EXPORT_SYMBOL(flow_get_u32_src);
1460
1461__be32 flow_get_u32_dst(const struct flow_keys *flow)
1462{
1463 switch (flow->control.addr_type) {
1464 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1465 return flow->addrs.v4addrs.dst;
1466 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1467 return (__force __be32)ipv6_addr_hash(
1468 &flow->addrs.v6addrs.dst);
1469 default:
1470 return 0;
1471 }
1472}
1473EXPORT_SYMBOL(flow_get_u32_dst);
1474
1475/* Sort the source and destination IP (and the ports if the IP are the same),
1476 * to have consistent hash within the two directions
1477 */
1478static inline void __flow_hash_consistentify(struct flow_keys *keys)
1479{
1480 int addr_diff, i;
1481
1482 switch (keys->control.addr_type) {
1483 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1484 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
1485 (__force u32)keys->addrs.v4addrs.src;
1486 if ((addr_diff < 0) ||
1487 (addr_diff == 0 &&
1488 ((__force u16)keys->ports.dst <
1489 (__force u16)keys->ports.src))) {
1490 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
1491 swap(keys->ports.src, keys->ports.dst);
1492 }
1493 break;
1494 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1495 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
1496 &keys->addrs.v6addrs.src,
1497 sizeof(keys->addrs.v6addrs.dst));
1498 if ((addr_diff < 0) ||
1499 (addr_diff == 0 &&
1500 ((__force u16)keys->ports.dst <
1501 (__force u16)keys->ports.src))) {
1502 for (i = 0; i < 4; i++)
1503 swap(keys->addrs.v6addrs.src.s6_addr32[i],
1504 keys->addrs.v6addrs.dst.s6_addr32[i]);
1505 swap(keys->ports.src, keys->ports.dst);
1506 }
1507 break;
1508 }
1509}
1510
1511static inline u32 __flow_hash_from_keys(struct flow_keys *keys,
1512 const siphash_key_t *keyval)
1513{
1514 u32 hash;
1515
1516 __flow_hash_consistentify(keys);
1517
1518 hash = siphash(flow_keys_hash_start(keys),
1519 flow_keys_hash_length(keys), keyval);
1520 if (!hash)
1521 hash = 1;
1522
1523 return hash;
1524}
1525
1526u32 flow_hash_from_keys(struct flow_keys *keys)
1527{
1528 __flow_hash_secret_init();
1529 return __flow_hash_from_keys(keys, &hashrnd);
1530}
1531EXPORT_SYMBOL(flow_hash_from_keys);
1532
1533static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1534 struct flow_keys *keys,
1535 const siphash_key_t *keyval)
1536{
1537 skb_flow_dissect_flow_keys(skb, keys,
1538 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1539
1540 return __flow_hash_from_keys(keys, keyval);
1541}
1542
1543struct _flow_keys_digest_data {
1544 __be16 n_proto;
1545 u8 ip_proto;
1546 u8 padding;
1547 __be32 ports;
1548 __be32 src;
1549 __be32 dst;
1550};
1551
1552void make_flow_keys_digest(struct flow_keys_digest *digest,
1553 const struct flow_keys *flow)
1554{
1555 struct _flow_keys_digest_data *data =
1556 (struct _flow_keys_digest_data *)digest;
1557
1558 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1559
1560 memset(digest, 0, sizeof(*digest));
1561
1562 data->n_proto = flow->basic.n_proto;
1563 data->ip_proto = flow->basic.ip_proto;
1564 data->ports = flow->ports.ports;
1565 data->src = flow->addrs.v4addrs.src;
1566 data->dst = flow->addrs.v4addrs.dst;
1567}
1568EXPORT_SYMBOL(make_flow_keys_digest);
1569
1570static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1571
1572u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
1573{
1574 struct flow_keys keys;
1575
1576 __flow_hash_secret_init();
1577
1578 memset(&keys, 0, sizeof(keys));
1579 __skb_flow_dissect(NULL, skb, &flow_keys_dissector_symmetric,
1580 &keys, NULL, 0, 0, 0,
1581 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1582
1583 return __flow_hash_from_keys(&keys, &hashrnd);
1584}
1585EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
1586
1587/**
1588 * __skb_get_hash: calculate a flow hash
1589 * @skb: sk_buff to calculate flow hash from
1590 *
1591 * This function calculates a flow hash based on src/dst addresses
1592 * and src/dst port numbers. Sets hash in skb to non-zero hash value
1593 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
1594 * if hash is a canonical 4-tuple hash over transport ports.
1595 */
1596void __skb_get_hash(struct sk_buff *skb)
1597{
1598 struct flow_keys keys;
1599 u32 hash;
1600
1601 __flow_hash_secret_init();
1602
1603 hash = ___skb_get_hash(skb, &keys, &hashrnd);
1604
1605 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1606}
1607EXPORT_SYMBOL(__skb_get_hash);
1608
1609__u32 skb_get_hash_perturb(const struct sk_buff *skb,
1610 const siphash_key_t *perturb)
1611{
1612 struct flow_keys keys;
1613
1614 return ___skb_get_hash(skb, &keys, perturb);
1615}
1616EXPORT_SYMBOL(skb_get_hash_perturb);
1617
1618u32 __skb_get_poff(const struct sk_buff *skb, void *data,
1619 const struct flow_keys_basic *keys, int hlen)
1620{
1621 u32 poff = keys->control.thoff;
1622
1623 /* skip L4 headers for fragments after the first */
1624 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1625 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1626 return poff;
1627
1628 switch (keys->basic.ip_proto) {
1629 case IPPROTO_TCP: {
1630 /* access doff as u8 to avoid unaligned access */
1631 const u8 *doff;
1632 u8 _doff;
1633
1634 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1635 data, hlen, &_doff);
1636 if (!doff)
1637 return poff;
1638
1639 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1640 break;
1641 }
1642 case IPPROTO_UDP:
1643 case IPPROTO_UDPLITE:
1644 poff += sizeof(struct udphdr);
1645 break;
1646 /* For the rest, we do not really care about header
1647 * extensions at this point for now.
1648 */
1649 case IPPROTO_ICMP:
1650 poff += sizeof(struct icmphdr);
1651 break;
1652 case IPPROTO_ICMPV6:
1653 poff += sizeof(struct icmp6hdr);
1654 break;
1655 case IPPROTO_IGMP:
1656 poff += sizeof(struct igmphdr);
1657 break;
1658 case IPPROTO_DCCP:
1659 poff += sizeof(struct dccp_hdr);
1660 break;
1661 case IPPROTO_SCTP:
1662 poff += sizeof(struct sctphdr);
1663 break;
1664 }
1665
1666 return poff;
1667}
1668
1669/**
1670 * skb_get_poff - get the offset to the payload
1671 * @skb: sk_buff to get the payload offset from
1672 *
1673 * The function will get the offset to the payload as far as it could
1674 * be dissected. The main user is currently BPF, so that we can dynamically
1675 * truncate packets without needing to push actual payload to the user
1676 * space and can analyze headers only, instead.
1677 */
1678u32 skb_get_poff(const struct sk_buff *skb)
1679{
1680 struct flow_keys_basic keys;
1681
1682 if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
1683 NULL, 0, 0, 0, 0))
1684 return 0;
1685
1686 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1687}
1688
1689__u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1690{
1691 memset(keys, 0, sizeof(*keys));
1692
1693 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1694 sizeof(keys->addrs.v6addrs.src));
1695 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1696 sizeof(keys->addrs.v6addrs.dst));
1697 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1698 keys->ports.src = fl6->fl6_sport;
1699 keys->ports.dst = fl6->fl6_dport;
1700 keys->keyid.keyid = fl6->fl6_gre_key;
1701 keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
1702 keys->basic.ip_proto = fl6->flowi6_proto;
1703
1704 return flow_hash_from_keys(keys);
1705}
1706EXPORT_SYMBOL(__get_hash_from_flowi6);
1707
1708static const struct flow_dissector_key flow_keys_dissector_keys[] = {
1709 {
1710 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1711 .offset = offsetof(struct flow_keys, control),
1712 },
1713 {
1714 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1715 .offset = offsetof(struct flow_keys, basic),
1716 },
1717 {
1718 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1719 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1720 },
1721 {
1722 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1723 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1724 },
1725 {
1726 .key_id = FLOW_DISSECTOR_KEY_TIPC,
1727 .offset = offsetof(struct flow_keys, addrs.tipckey),
1728 },
1729 {
1730 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1731 .offset = offsetof(struct flow_keys, ports),
1732 },
1733 {
1734 .key_id = FLOW_DISSECTOR_KEY_VLAN,
1735 .offset = offsetof(struct flow_keys, vlan),
1736 },
1737 {
1738 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1739 .offset = offsetof(struct flow_keys, tags),
1740 },
1741 {
1742 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1743 .offset = offsetof(struct flow_keys, keyid),
1744 },
1745};
1746
1747static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1748 {
1749 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1750 .offset = offsetof(struct flow_keys, control),
1751 },
1752 {
1753 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1754 .offset = offsetof(struct flow_keys, basic),
1755 },
1756 {
1757 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1758 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1759 },
1760 {
1761 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1762 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1763 },
1764 {
1765 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1766 .offset = offsetof(struct flow_keys, ports),
1767 },
1768};
1769
1770static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = {
1771 {
1772 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1773 .offset = offsetof(struct flow_keys, control),
1774 },
1775 {
1776 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1777 .offset = offsetof(struct flow_keys, basic),
1778 },
1779};
1780
1781struct flow_dissector flow_keys_dissector __read_mostly;
1782EXPORT_SYMBOL(flow_keys_dissector);
1783
1784struct flow_dissector flow_keys_basic_dissector __read_mostly;
1785EXPORT_SYMBOL(flow_keys_basic_dissector);
1786
1787static int __init init_default_flow_dissectors(void)
1788{
1789 skb_flow_dissector_init(&flow_keys_dissector,
1790 flow_keys_dissector_keys,
1791 ARRAY_SIZE(flow_keys_dissector_keys));
1792 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1793 flow_keys_dissector_symmetric_keys,
1794 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1795 skb_flow_dissector_init(&flow_keys_basic_dissector,
1796 flow_keys_basic_dissector_keys,
1797 ARRAY_SIZE(flow_keys_basic_dissector_keys));
1798 return 0;
1799}
1800core_initcall(init_default_flow_dissectors);