]>
Commit | Line | Data |
---|---|---|
064af421 BP |
1 | /* |
2 | * Distributed under the terms of the GNU GPL version 2. | |
f632c8fc | 3 | * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks. |
a14bc59f BP |
4 | * |
5 | * Significant portions of this file may be copied from parts of the Linux | |
6 | * kernel, by Linus Torvalds and others. | |
064af421 BP |
7 | */ |
8 | ||
9 | #include "flow.h" | |
f5e86186 | 10 | #include "datapath.h" |
36956a7d | 11 | #include <asm/uaccess.h> |
064af421 BP |
12 | #include <linux/netdevice.h> |
13 | #include <linux/etherdevice.h> | |
14 | #include <linux/if_ether.h> | |
15 | #include <linux/if_vlan.h> | |
16 | #include <net/llc_pdu.h> | |
17 | #include <linux/kernel.h> | |
8d5ebd83 | 18 | #include <linux/jhash.h> |
064af421 BP |
19 | #include <linux/jiffies.h> |
20 | #include <linux/llc.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/in.h> | |
23 | #include <linux/rcupdate.h> | |
a26ef517 | 24 | #include <linux/if_arp.h> |
064af421 BP |
25 | #include <linux/if_ether.h> |
26 | #include <linux/ip.h> | |
d31f1109 | 27 | #include <linux/ipv6.h> |
064af421 BP |
28 | #include <linux/tcp.h> |
29 | #include <linux/udp.h> | |
30 | #include <linux/icmp.h> | |
d31f1109 | 31 | #include <linux/icmpv6.h> |
3c5f6de3 | 32 | #include <net/inet_ecn.h> |
064af421 | 33 | #include <net/ip.h> |
d31f1109 | 34 | #include <net/ipv6.h> |
685a51a5 | 35 | #include <net/ndisc.h> |
064af421 | 36 | |
6ce39213 JG |
37 | #include "vlan.h" |
38 | ||
33b38b63 | 39 | static struct kmem_cache *flow_cache; |
83e3e75b | 40 | static unsigned int hash_seed __read_mostly; |
064af421 | 41 | |
e819fb47 | 42 | static inline bool arphdr_ok(struct sk_buff *skb) |
a26ef517 | 43 | { |
7d0ab001 | 44 | return skb->len >= skb_network_offset(skb) + sizeof(struct arp_eth_header); |
a26ef517 JP |
45 | } |
46 | ||
4c1ad233 | 47 | static inline int check_iphdr(struct sk_buff *skb) |
064af421 | 48 | { |
4c1ad233 BP |
49 | unsigned int nh_ofs = skb_network_offset(skb); |
50 | unsigned int ip_len; | |
51 | ||
52 | if (skb->len < nh_ofs + sizeof(struct iphdr)) | |
53 | return -EINVAL; | |
54 | ||
55 | ip_len = ip_hdrlen(skb); | |
56 | if (ip_len < sizeof(struct iphdr) || skb->len < nh_ofs + ip_len) | |
57 | return -EINVAL; | |
58 | ||
59 | /* | |
60 | * Pull enough header bytes to account for the IP header plus the | |
61 | * longest transport header that we parse, currently 20 bytes for TCP. | |
62 | */ | |
63 | if (!pskb_may_pull(skb, min(nh_ofs + ip_len + 20, skb->len))) | |
64 | return -ENOMEM; | |
65 | ||
66 | skb_set_transport_header(skb, nh_ofs + ip_len); | |
67 | return 0; | |
064af421 BP |
68 | } |
69 | ||
e819fb47 | 70 | static inline bool tcphdr_ok(struct sk_buff *skb) |
064af421 BP |
71 | { |
72 | int th_ofs = skb_transport_offset(skb); | |
7d0ab001 | 73 | if (skb->len >= th_ofs + sizeof(struct tcphdr)) { |
064af421 BP |
74 | int tcp_len = tcp_hdrlen(skb); |
75 | return (tcp_len >= sizeof(struct tcphdr) | |
76 | && skb->len >= th_ofs + tcp_len); | |
77 | } | |
e819fb47 | 78 | return false; |
064af421 BP |
79 | } |
80 | ||
e819fb47 | 81 | static inline bool udphdr_ok(struct sk_buff *skb) |
064af421 | 82 | { |
7d0ab001 | 83 | return skb->len >= skb_transport_offset(skb) + sizeof(struct udphdr); |
064af421 BP |
84 | } |
85 | ||
e819fb47 | 86 | static inline bool icmphdr_ok(struct sk_buff *skb) |
064af421 | 87 | { |
7d0ab001 | 88 | return skb->len >= skb_transport_offset(skb) + sizeof(struct icmphdr); |
064af421 BP |
89 | } |
90 | ||
ec58547a JG |
91 | u64 flow_used_time(unsigned long flow_jiffies) |
92 | { | |
93 | struct timespec cur_ts; | |
94 | u64 cur_ms, idle_ms; | |
95 | ||
96 | ktime_get_ts(&cur_ts); | |
97 | idle_ms = jiffies_to_msecs(jiffies - flow_jiffies); | |
98 | cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC + | |
99 | cur_ts.tv_nsec / NSEC_PER_MSEC; | |
100 | ||
101 | return cur_ms - idle_ms; | |
102 | } | |
103 | ||
d31f1109 JP |
104 | static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key) |
105 | { | |
106 | unsigned int nh_ofs = skb_network_offset(skb); | |
107 | unsigned int nh_len; | |
108 | int payload_ofs; | |
d31f1109 JP |
109 | struct ipv6hdr *nh; |
110 | uint8_t nexthdr; | |
111 | ||
112 | if (unlikely(skb->len < nh_ofs + sizeof(*nh))) | |
113 | return -EINVAL; | |
114 | ||
115 | nh = ipv6_hdr(skb); | |
116 | nexthdr = nh->nexthdr; | |
117 | payload_ofs = (u8 *)(nh + 1) - skb->data; | |
d31f1109 | 118 | |
bfef4717 JG |
119 | ipv6_addr_copy(&key->ipv6_src, &nh->saddr); |
120 | ipv6_addr_copy(&key->ipv6_dst, &nh->daddr); | |
d31f1109 JP |
121 | key->nw_tos = ipv6_get_dsfield(nh) & ~INET_ECN_MASK; |
122 | key->nw_proto = NEXTHDR_NONE; | |
123 | ||
d31f1109 | 124 | payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr); |
bfef4717 | 125 | if (unlikely(payload_ofs < 0)) |
d31f1109 | 126 | return -EINVAL; |
bfef4717 | 127 | |
d31f1109 JP |
128 | nh_len = payload_ofs - nh_ofs; |
129 | ||
d31f1109 JP |
130 | /* Pull enough header bytes to account for the IP header plus the |
131 | * longest transport header that we parse, currently 20 bytes for TCP. | |
132 | * To dig deeper than the transport header, transport parsers may need | |
133 | * to pull more header bytes. | |
134 | */ | |
135 | if (unlikely(!pskb_may_pull(skb, min(nh_ofs + nh_len + 20, skb->len)))) | |
136 | return -ENOMEM; | |
137 | ||
138 | skb_set_transport_header(skb, nh_ofs + nh_len); | |
139 | key->nw_proto = nexthdr; | |
140 | return nh_len; | |
141 | } | |
142 | ||
143 | static bool icmp6hdr_ok(struct sk_buff *skb) | |
144 | { | |
145 | return skb->len >= skb_transport_offset(skb) + sizeof(struct icmp6hdr); | |
146 | } | |
ec58547a | 147 | |
064af421 BP |
148 | #define TCP_FLAGS_OFFSET 13 |
149 | #define TCP_FLAG_MASK 0x3f | |
150 | ||
064af421 BP |
151 | void flow_used(struct sw_flow *flow, struct sk_buff *skb) |
152 | { | |
064af421 BP |
153 | u8 tcp_flags = 0; |
154 | ||
abfec865 BP |
155 | if (flow->key.dl_type == htons(ETH_P_IP) && |
156 | flow->key.nw_proto == IPPROTO_TCP) { | |
157 | u8 *tcp = (u8 *)tcp_hdr(skb); | |
158 | tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK; | |
064af421 BP |
159 | } |
160 | ||
f2459fe7 | 161 | spin_lock_bh(&flow->lock); |
6bfafa55 | 162 | flow->used = jiffies; |
064af421 BP |
163 | flow->packet_count++; |
164 | flow->byte_count += skb->len; | |
165 | flow->tcp_flags |= tcp_flags; | |
f2459fe7 | 166 | spin_unlock_bh(&flow->lock); |
064af421 BP |
167 | } |
168 | ||
37a1300c | 169 | struct sw_flow_actions *flow_actions_alloc(const struct nlattr *actions) |
064af421 | 170 | { |
37a1300c | 171 | int actions_len = nla_len(actions); |
064af421 BP |
172 | struct sw_flow_actions *sfa; |
173 | ||
722d19c5 BP |
174 | /* At least DP_MAX_PORTS actions are required to be able to flood a |
175 | * packet to every port. Factor of 2 allows for setting VLAN tags, | |
176 | * etc. */ | |
cdee00fd | 177 | if (actions_len > 2 * DP_MAX_PORTS * nla_total_size(4)) |
064af421 BP |
178 | return ERR_PTR(-EINVAL); |
179 | ||
84c17d98 | 180 | sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL); |
064af421 BP |
181 | if (!sfa) |
182 | return ERR_PTR(-ENOMEM); | |
183 | ||
cdee00fd | 184 | sfa->actions_len = actions_len; |
37a1300c | 185 | memcpy(sfa->actions, nla_data(actions), actions_len); |
064af421 BP |
186 | return sfa; |
187 | } | |
188 | ||
560e8022 JG |
189 | struct sw_flow *flow_alloc(void) |
190 | { | |
191 | struct sw_flow *flow; | |
192 | ||
193 | flow = kmem_cache_alloc(flow_cache, GFP_KERNEL); | |
194 | if (!flow) | |
195 | return ERR_PTR(-ENOMEM); | |
196 | ||
197 | spin_lock_init(&flow->lock); | |
fb8c9347 JG |
198 | atomic_set(&flow->refcnt, 1); |
199 | flow->dead = false; | |
064af421 | 200 | |
560e8022 JG |
201 | return flow; |
202 | } | |
203 | ||
8d5ebd83 JG |
204 | void flow_free_tbl(struct tbl_node *node) |
205 | { | |
206 | struct sw_flow *flow = flow_cast(node); | |
fb8c9347 JG |
207 | |
208 | flow->dead = true; | |
209 | flow_put(flow); | |
8d5ebd83 JG |
210 | } |
211 | ||
064af421 BP |
212 | /* RCU callback used by flow_deferred_free. */ |
213 | static void rcu_free_flow_callback(struct rcu_head *rcu) | |
214 | { | |
215 | struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); | |
fb8c9347 JG |
216 | |
217 | flow->dead = true; | |
218 | flow_put(flow); | |
064af421 BP |
219 | } |
220 | ||
221 | /* Schedules 'flow' to be freed after the next RCU grace period. | |
222 | * The caller must hold rcu_read_lock for this to be sensible. */ | |
223 | void flow_deferred_free(struct sw_flow *flow) | |
224 | { | |
225 | call_rcu(&flow->rcu, rcu_free_flow_callback); | |
226 | } | |
227 | ||
fb8c9347 JG |
228 | void flow_hold(struct sw_flow *flow) |
229 | { | |
230 | atomic_inc(&flow->refcnt); | |
231 | } | |
232 | ||
233 | void flow_put(struct sw_flow *flow) | |
234 | { | |
235 | if (unlikely(!flow)) | |
236 | return; | |
237 | ||
238 | if (atomic_dec_and_test(&flow->refcnt)) { | |
39872c70 | 239 | kfree((struct sf_flow_acts __force *)flow->sf_acts); |
fb8c9347 JG |
240 | kmem_cache_free(flow_cache, flow); |
241 | } | |
242 | } | |
243 | ||
064af421 BP |
244 | /* RCU callback used by flow_deferred_free_acts. */ |
245 | static void rcu_free_acts_callback(struct rcu_head *rcu) | |
246 | { | |
d295e8e9 | 247 | struct sw_flow_actions *sf_acts = container_of(rcu, |
064af421 BP |
248 | struct sw_flow_actions, rcu); |
249 | kfree(sf_acts); | |
250 | } | |
251 | ||
252 | /* Schedules 'sf_acts' to be freed after the next RCU grace period. | |
253 | * The caller must hold rcu_read_lock for this to be sensible. */ | |
254 | void flow_deferred_free_acts(struct sw_flow_actions *sf_acts) | |
255 | { | |
256 | call_rcu(&sf_acts->rcu, rcu_free_acts_callback); | |
257 | } | |
258 | ||
36956a7d | 259 | static void parse_vlan(struct sk_buff *skb, struct sw_flow_key *key) |
064af421 | 260 | { |
50f06e16 BP |
261 | struct qtag_prefix { |
262 | __be16 eth_type; /* ETH_P_8021Q */ | |
263 | __be16 tci; | |
264 | }; | |
265 | struct qtag_prefix *qp; | |
266 | ||
267 | if (skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)) | |
268 | return; | |
269 | ||
270 | qp = (struct qtag_prefix *) skb->data; | |
36956a7d | 271 | key->dl_tci = qp->tci | htons(VLAN_TAG_PRESENT); |
50f06e16 BP |
272 | __skb_pull(skb, sizeof(struct qtag_prefix)); |
273 | } | |
274 | ||
275 | static __be16 parse_ethertype(struct sk_buff *skb) | |
064af421 | 276 | { |
50f06e16 BP |
277 | struct llc_snap_hdr { |
278 | u8 dsap; /* Always 0xAA */ | |
279 | u8 ssap; /* Always 0xAA */ | |
280 | u8 ctrl; | |
281 | u8 oui[3]; | |
8dda8c9b | 282 | __be16 ethertype; |
50f06e16 BP |
283 | }; |
284 | struct llc_snap_hdr *llc; | |
285 | __be16 proto; | |
286 | ||
287 | proto = *(__be16 *) skb->data; | |
288 | __skb_pull(skb, sizeof(__be16)); | |
289 | ||
36956a7d | 290 | if (ntohs(proto) >= 1536) |
50f06e16 BP |
291 | return proto; |
292 | ||
293 | if (unlikely(skb->len < sizeof(struct llc_snap_hdr))) | |
36956a7d | 294 | return htons(ETH_P_802_2); |
50f06e16 BP |
295 | |
296 | llc = (struct llc_snap_hdr *) skb->data; | |
297 | if (llc->dsap != LLC_SAP_SNAP || | |
298 | llc->ssap != LLC_SAP_SNAP || | |
299 | (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0) | |
36956a7d | 300 | return htons(ETH_P_802_2); |
50f06e16 BP |
301 | |
302 | __skb_pull(skb, sizeof(struct llc_snap_hdr)); | |
303 | return llc->ethertype; | |
064af421 BP |
304 | } |
305 | ||
685a51a5 | 306 | static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key, |
bfef4717 | 307 | int nh_len) |
685a51a5 | 308 | { |
685a51a5 JP |
309 | struct icmp6hdr *icmp = icmp6_hdr(skb); |
310 | ||
311 | /* The ICMPv6 type and code fields use the 16-bit transport port | |
bfef4717 JG |
312 | * fields, so we need to store them in 16-bit network byte order. |
313 | */ | |
685a51a5 JP |
314 | key->tp_src = htons(icmp->icmp6_type); |
315 | key->tp_dst = htons(icmp->icmp6_code); | |
316 | ||
bfef4717 JG |
317 | if (icmp->icmp6_code == 0 && |
318 | (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION || | |
319 | icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) { | |
e977ba19 | 320 | int icmp_len = skb->len - skb_transport_offset(skb); |
685a51a5 JP |
321 | struct nd_msg *nd; |
322 | int offset; | |
323 | ||
324 | /* In order to process neighbor discovery options, we need the | |
bfef4717 JG |
325 | * entire packet. |
326 | */ | |
327 | if (unlikely(icmp_len < sizeof(*nd))) | |
328 | return 0; | |
329 | if (unlikely(skb_linearize(skb))) | |
685a51a5 JP |
330 | return -ENOMEM; |
331 | ||
332 | nd = (struct nd_msg *)skb_transport_header(skb); | |
bfef4717 | 333 | ipv6_addr_copy(&key->nd_target, &nd->target); |
685a51a5 JP |
334 | |
335 | icmp_len -= sizeof(*nd); | |
336 | offset = 0; | |
337 | while (icmp_len >= 8) { | |
338 | struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd->opt + offset); | |
339 | int opt_len = nd_opt->nd_opt_len * 8; | |
340 | ||
bfef4717 | 341 | if (unlikely(!opt_len || opt_len > icmp_len)) |
685a51a5 JP |
342 | goto invalid; |
343 | ||
bfef4717 JG |
344 | /* Store the link layer address if the appropriate |
345 | * option is provided. It is considered an error if | |
346 | * the same link layer option is specified twice. | |
347 | */ | |
685a51a5 | 348 | if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR |
bfef4717 JG |
349 | && opt_len == 8) { |
350 | if (unlikely(!is_zero_ether_addr(key->arp_sha))) | |
685a51a5 JP |
351 | goto invalid; |
352 | memcpy(key->arp_sha, | |
bfef4717 | 353 | &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN); |
685a51a5 | 354 | } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR |
bfef4717 JG |
355 | && opt_len == 8) { |
356 | if (unlikely(!is_zero_ether_addr(key->arp_tha))) | |
685a51a5 JP |
357 | goto invalid; |
358 | memcpy(key->arp_tha, | |
bfef4717 | 359 | &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN); |
685a51a5 JP |
360 | } |
361 | ||
362 | icmp_len -= opt_len; | |
363 | offset += opt_len; | |
364 | } | |
365 | } | |
366 | ||
367 | return 0; | |
368 | ||
369 | invalid: | |
bfef4717 | 370 | memset(&key->nd_target, 0, sizeof(key->nd_target)); |
685a51a5 JP |
371 | memset(key->arp_sha, 0, sizeof(key->arp_sha)); |
372 | memset(key->arp_tha, 0, sizeof(key->arp_tha)); | |
373 | ||
374 | return 0; | |
375 | } | |
376 | ||
a31e0e31 BP |
377 | /** |
378 | * flow_extract - extracts a flow key from an Ethernet frame. | |
379 | * @skb: sk_buff that contains the frame, with skb->data pointing to the | |
380 | * Ethernet header | |
381 | * @in_port: port number on which @skb was received. | |
382 | * @key: output flow key | |
26233bb4 BP |
383 | * @is_frag: set to 1 if @skb contains an IPv4 fragment, or to 0 if @skb does |
384 | * not contain an IPv4 packet or if it is not a fragment. | |
a31e0e31 BP |
385 | * |
386 | * The caller must ensure that skb->len >= ETH_HLEN. | |
387 | * | |
4c1ad233 BP |
388 | * Returns 0 if successful, otherwise a negative errno value. |
389 | * | |
59a18f80 BP |
390 | * Initializes @skb header pointers as follows: |
391 | * | |
392 | * - skb->mac_header: the Ethernet header. | |
393 | * | |
394 | * - skb->network_header: just past the Ethernet header, or just past the | |
395 | * VLAN header, to the first byte of the Ethernet payload. | |
396 | * | |
d31f1109 JP |
397 | * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6 |
398 | * on output, then just past the IP header, if one is present and | |
399 | * of a correct length, otherwise the same as skb->network_header. | |
400 | * For other key->dl_type values it is left untouched. | |
a31e0e31 | 401 | */ |
36956a7d | 402 | int flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key, |
b7a31ec1 | 403 | bool *is_frag) |
064af421 BP |
404 | { |
405 | struct ethhdr *eth; | |
064af421 | 406 | |
84c17d98 | 407 | memset(key, 0, sizeof(*key)); |
659586ef | 408 | key->tun_id = OVS_CB(skb)->tun_id; |
064af421 | 409 | key->in_port = in_port; |
b7a31ec1 | 410 | *is_frag = false; |
064af421 | 411 | |
4c1ad233 BP |
412 | /* |
413 | * We would really like to pull as many bytes as we could possibly | |
d31f1109 JP |
414 | * want to parse into the linear data area. Currently, for IPv4, |
415 | * that is: | |
4c1ad233 BP |
416 | * |
417 | * 14 Ethernet header | |
418 | * 4 VLAN header | |
419 | * 60 max IP header with options | |
420 | * 20 max TCP/UDP/ICMP header (don't care about options) | |
421 | * -- | |
422 | * 98 | |
423 | * | |
424 | * But Xen only allocates 64 or 72 bytes for the linear data area in | |
425 | * netback, which means that we would reallocate and copy the skb's | |
426 | * linear data on every packet if we did that. So instead just pull 64 | |
427 | * bytes, which is always sufficient without IP options, and then check | |
428 | * whether we need to pull more later when we look at the IP header. | |
429 | */ | |
d9fce1ca | 430 | if (!pskb_may_pull(skb, min(skb->len, 64u))) |
4c1ad233 | 431 | return -ENOMEM; |
064af421 BP |
432 | |
433 | skb_reset_mac_header(skb); | |
064af421 | 434 | |
50f06e16 BP |
435 | /* Link layer. */ |
436 | eth = eth_hdr(skb); | |
064af421 BP |
437 | memcpy(key->dl_src, eth->h_source, ETH_ALEN); |
438 | memcpy(key->dl_dst, eth->h_dest, ETH_ALEN); | |
50f06e16 BP |
439 | |
440 | /* dl_type, dl_vlan, dl_vlan_pcp. */ | |
441 | __skb_pull(skb, 2 * ETH_ALEN); | |
6ce39213 JG |
442 | |
443 | if (vlan_tx_tag_present(skb)) | |
444 | key->dl_tci = htons(vlan_get_tci(skb)); | |
445 | else if (eth->h_proto == htons(ETH_P_8021Q)) | |
50f06e16 | 446 | parse_vlan(skb, key); |
6ce39213 | 447 | |
50f06e16 BP |
448 | key->dl_type = parse_ethertype(skb); |
449 | skb_reset_network_header(skb); | |
450 | __skb_push(skb, skb->data - (unsigned char *)eth); | |
064af421 BP |
451 | |
452 | /* Network layer. */ | |
4c1ad233 BP |
453 | if (key->dl_type == htons(ETH_P_IP)) { |
454 | struct iphdr *nh; | |
455 | int error; | |
456 | ||
457 | error = check_iphdr(skb); | |
458 | if (unlikely(error)) { | |
459 | if (error == -EINVAL) { | |
460 | skb->transport_header = skb->network_header; | |
461 | return 0; | |
462 | } | |
463 | return error; | |
464 | } | |
465 | ||
466 | nh = ip_hdr(skb); | |
d31f1109 JP |
467 | key->ipv4_src = nh->saddr; |
468 | key->ipv4_dst = nh->daddr; | |
f5e86186 | 469 | key->nw_tos = nh->tos & ~INET_ECN_MASK; |
064af421 | 470 | key->nw_proto = nh->protocol; |
064af421 BP |
471 | |
472 | /* Transport layer. */ | |
875244c7 SH |
473 | if (!(nh->frag_off & htons(IP_MF | IP_OFFSET)) && |
474 | !(skb_shinfo(skb)->gso_type & SKB_GSO_UDP)) { | |
064af421 BP |
475 | if (key->nw_proto == IPPROTO_TCP) { |
476 | if (tcphdr_ok(skb)) { | |
477 | struct tcphdr *tcp = tcp_hdr(skb); | |
478 | key->tp_src = tcp->source; | |
479 | key->tp_dst = tcp->dest; | |
064af421 BP |
480 | } |
481 | } else if (key->nw_proto == IPPROTO_UDP) { | |
482 | if (udphdr_ok(skb)) { | |
483 | struct udphdr *udp = udp_hdr(skb); | |
484 | key->tp_src = udp->source; | |
485 | key->tp_dst = udp->dest; | |
064af421 BP |
486 | } |
487 | } else if (key->nw_proto == IPPROTO_ICMP) { | |
488 | if (icmphdr_ok(skb)) { | |
489 | struct icmphdr *icmp = icmp_hdr(skb); | |
490 | /* The ICMP type and code fields use the 16-bit | |
491 | * transport port fields, so we need to store them | |
492 | * in 16-bit network byte order. */ | |
493 | key->tp_src = htons(icmp->type); | |
494 | key->tp_dst = htons(icmp->code); | |
064af421 BP |
495 | } |
496 | } | |
b7a31ec1 JG |
497 | } else |
498 | *is_frag = true; | |
499 | ||
a26ef517 JP |
500 | } else if (key->dl_type == htons(ETH_P_ARP) && arphdr_ok(skb)) { |
501 | struct arp_eth_header *arp; | |
502 | ||
503 | arp = (struct arp_eth_header *)skb_network_header(skb); | |
504 | ||
f5e86186 | 505 | if (arp->ar_hrd == htons(ARPHRD_ETHER) |
de3f65ea JP |
506 | && arp->ar_pro == htons(ETH_P_IP) |
507 | && arp->ar_hln == ETH_ALEN | |
508 | && arp->ar_pln == 4) { | |
509 | ||
510 | /* We only match on the lower 8 bits of the opcode. */ | |
b7a31ec1 | 511 | if (ntohs(arp->ar_op) <= 0xff) |
de3f65ea | 512 | key->nw_proto = ntohs(arp->ar_op); |
de3f65ea | 513 | |
d295e8e9 | 514 | if (key->nw_proto == ARPOP_REQUEST |
de3f65ea | 515 | || key->nw_proto == ARPOP_REPLY) { |
d31f1109 JP |
516 | memcpy(&key->ipv4_src, arp->ar_sip, sizeof(key->ipv4_src)); |
517 | memcpy(&key->ipv4_dst, arp->ar_tip, sizeof(key->ipv4_dst)); | |
bad68a99 JP |
518 | memcpy(key->arp_sha, arp->ar_sha, ETH_ALEN); |
519 | memcpy(key->arp_tha, arp->ar_tha, ETH_ALEN); | |
de3f65ea JP |
520 | } |
521 | } | |
d31f1109 JP |
522 | } else if (key->dl_type == htons(ETH_P_IPV6)) { |
523 | int nh_len; /* IPv6 Header + Extensions */ | |
524 | ||
525 | nh_len = parse_ipv6hdr(skb, key); | |
526 | if (unlikely(nh_len < 0)) { | |
527 | if (nh_len == -EINVAL) { | |
528 | skb->transport_header = skb->network_header; | |
529 | return 0; | |
530 | } | |
531 | return nh_len; | |
532 | } | |
533 | ||
534 | /* Transport layer. */ | |
535 | if (key->nw_proto == NEXTHDR_TCP) { | |
536 | if (tcphdr_ok(skb)) { | |
537 | struct tcphdr *tcp = tcp_hdr(skb); | |
538 | key->tp_src = tcp->source; | |
539 | key->tp_dst = tcp->dest; | |
540 | } | |
541 | } else if (key->nw_proto == NEXTHDR_UDP) { | |
542 | if (udphdr_ok(skb)) { | |
543 | struct udphdr *udp = udp_hdr(skb); | |
544 | key->tp_src = udp->source; | |
545 | key->tp_dst = udp->dest; | |
546 | } | |
547 | } else if (key->nw_proto == NEXTHDR_ICMP) { | |
548 | if (icmp6hdr_ok(skb)) { | |
685a51a5 JP |
549 | int error = parse_icmpv6(skb, key, nh_len); |
550 | if (error < 0) | |
551 | return error; | |
d31f1109 JP |
552 | } |
553 | } | |
064af421 | 554 | } |
769f8ccd | 555 | return 0; |
064af421 BP |
556 | } |
557 | ||
36956a7d | 558 | u32 flow_hash(const struct sw_flow_key *key) |
8d5ebd83 | 559 | { |
84c17d98 | 560 | return jhash2((u32*)key, sizeof(*key) / sizeof(u32), hash_seed); |
8d5ebd83 JG |
561 | } |
562 | ||
563 | int flow_cmp(const struct tbl_node *node, void *key2_) | |
564 | { | |
36956a7d BP |
565 | const struct sw_flow_key *key1 = &flow_cast(node)->key; |
566 | const struct sw_flow_key *key2 = key2_; | |
8d5ebd83 | 567 | |
36956a7d BP |
568 | return !memcmp(key1, key2, sizeof(struct sw_flow_key)); |
569 | } | |
570 | ||
571 | /** | |
572 | * flow_from_nlattrs - parses Netlink attributes into a flow key. | |
573 | * @swkey: receives the extracted flow key. | |
d6569377 BP |
574 | * @key: Netlink attribute holding nested %ODP_KEY_ATTR_* Netlink attribute |
575 | * sequence. | |
36956a7d BP |
576 | * |
577 | * This state machine accepts the following forms, with [] for optional | |
578 | * elements and | for alternatives: | |
579 | * | |
d31f1109 | 580 | * [tun_id] in_port ethernet [8021q] [ethertype \ |
685a51a5 | 581 | * [IPv4 [TCP|UDP|ICMP] | IPv6 [TCP|UDP|ICMPv6 [ND]] | ARP]] |
36956a7d | 582 | */ |
d6569377 | 583 | int flow_from_nlattrs(struct sw_flow_key *swkey, const struct nlattr *attr) |
36956a7d BP |
584 | { |
585 | const struct nlattr *nla; | |
586 | u16 prev_type; | |
587 | int rem; | |
588 | ||
589 | memset(swkey, 0, sizeof(*swkey)); | |
590 | swkey->dl_type = htons(ETH_P_802_2); | |
591 | ||
592 | prev_type = ODP_KEY_ATTR_UNSPEC; | |
d6569377 | 593 | nla_for_each_nested(nla, attr, rem) { |
36956a7d BP |
594 | static const u32 key_lens[ODP_KEY_ATTR_MAX + 1] = { |
595 | [ODP_KEY_ATTR_TUN_ID] = 8, | |
596 | [ODP_KEY_ATTR_IN_PORT] = 4, | |
597 | [ODP_KEY_ATTR_ETHERNET] = sizeof(struct odp_key_ethernet), | |
598 | [ODP_KEY_ATTR_8021Q] = sizeof(struct odp_key_8021q), | |
599 | [ODP_KEY_ATTR_ETHERTYPE] = 2, | |
600 | [ODP_KEY_ATTR_IPV4] = sizeof(struct odp_key_ipv4), | |
d31f1109 | 601 | [ODP_KEY_ATTR_IPV6] = sizeof(struct odp_key_ipv6), |
36956a7d BP |
602 | [ODP_KEY_ATTR_TCP] = sizeof(struct odp_key_tcp), |
603 | [ODP_KEY_ATTR_UDP] = sizeof(struct odp_key_udp), | |
604 | [ODP_KEY_ATTR_ICMP] = sizeof(struct odp_key_icmp), | |
d31f1109 | 605 | [ODP_KEY_ATTR_ICMPV6] = sizeof(struct odp_key_icmpv6), |
36956a7d | 606 | [ODP_KEY_ATTR_ARP] = sizeof(struct odp_key_arp), |
685a51a5 | 607 | [ODP_KEY_ATTR_ND] = sizeof(struct odp_key_nd), |
36956a7d BP |
608 | }; |
609 | ||
610 | const struct odp_key_ethernet *eth_key; | |
611 | const struct odp_key_8021q *q_key; | |
612 | const struct odp_key_ipv4 *ipv4_key; | |
d31f1109 | 613 | const struct odp_key_ipv6 *ipv6_key; |
36956a7d BP |
614 | const struct odp_key_tcp *tcp_key; |
615 | const struct odp_key_udp *udp_key; | |
616 | const struct odp_key_icmp *icmp_key; | |
d31f1109 | 617 | const struct odp_key_icmpv6 *icmpv6_key; |
36956a7d | 618 | const struct odp_key_arp *arp_key; |
685a51a5 | 619 | const struct odp_key_nd *nd_key; |
36956a7d BP |
620 | |
621 | int type = nla_type(nla); | |
622 | ||
623 | if (type > ODP_KEY_ATTR_MAX || nla_len(nla) != key_lens[type]) | |
624 | return -EINVAL; | |
625 | ||
626 | #define TRANSITION(PREV_TYPE, TYPE) (((PREV_TYPE) << 16) | (TYPE)) | |
627 | switch (TRANSITION(prev_type, type)) { | |
628 | case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_TUN_ID): | |
629 | swkey->tun_id = nla_get_be64(nla); | |
630 | break; | |
631 | ||
632 | case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_IN_PORT): | |
633 | case TRANSITION(ODP_KEY_ATTR_TUN_ID, ODP_KEY_ATTR_IN_PORT): | |
634 | if (nla_get_u32(nla) >= DP_MAX_PORTS) | |
635 | return -EINVAL; | |
636 | swkey->in_port = nla_get_u32(nla); | |
637 | break; | |
638 | ||
639 | case TRANSITION(ODP_KEY_ATTR_IN_PORT, ODP_KEY_ATTR_ETHERNET): | |
640 | eth_key = nla_data(nla); | |
641 | memcpy(swkey->dl_src, eth_key->eth_src, ETH_ALEN); | |
642 | memcpy(swkey->dl_dst, eth_key->eth_dst, ETH_ALEN); | |
643 | break; | |
644 | ||
645 | case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_8021Q): | |
646 | q_key = nla_data(nla); | |
647 | /* Only standard 0x8100 VLANs currently supported. */ | |
648 | if (q_key->q_tpid != htons(ETH_P_8021Q)) | |
649 | return -EINVAL; | |
650 | if (q_key->q_tci & htons(VLAN_TAG_PRESENT)) | |
651 | return -EINVAL; | |
652 | swkey->dl_tci = q_key->q_tci | htons(VLAN_TAG_PRESENT); | |
653 | break; | |
654 | ||
655 | case TRANSITION(ODP_KEY_ATTR_8021Q, ODP_KEY_ATTR_ETHERTYPE): | |
656 | case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_ETHERTYPE): | |
657 | swkey->dl_type = nla_get_be16(nla); | |
658 | if (ntohs(swkey->dl_type) < 1536) | |
659 | return -EINVAL; | |
660 | break; | |
661 | ||
662 | case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_IPV4): | |
663 | if (swkey->dl_type != htons(ETH_P_IP)) | |
664 | return -EINVAL; | |
665 | ipv4_key = nla_data(nla); | |
d31f1109 JP |
666 | swkey->ipv4_src = ipv4_key->ipv4_src; |
667 | swkey->ipv4_dst = ipv4_key->ipv4_dst; | |
36956a7d BP |
668 | swkey->nw_proto = ipv4_key->ipv4_proto; |
669 | swkey->nw_tos = ipv4_key->ipv4_tos; | |
670 | if (swkey->nw_tos & INET_ECN_MASK) | |
671 | return -EINVAL; | |
672 | break; | |
673 | ||
d31f1109 JP |
674 | case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_IPV6): |
675 | if (swkey->dl_type != htons(ETH_P_IPV6)) | |
676 | return -EINVAL; | |
677 | ipv6_key = nla_data(nla); | |
bfef4717 | 678 | memcpy(&swkey->ipv6_src, ipv6_key->ipv6_src, |
d31f1109 | 679 | sizeof(swkey->ipv6_src)); |
bfef4717 | 680 | memcpy(&swkey->ipv6_dst, ipv6_key->ipv6_dst, |
d31f1109 JP |
681 | sizeof(swkey->ipv6_dst)); |
682 | swkey->nw_proto = ipv6_key->ipv6_proto; | |
683 | swkey->nw_tos = ipv6_key->ipv6_tos; | |
684 | if (swkey->nw_tos & INET_ECN_MASK) | |
685 | return -EINVAL; | |
686 | break; | |
687 | ||
36956a7d | 688 | case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_TCP): |
d31f1109 | 689 | case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_TCP): |
36956a7d BP |
690 | if (swkey->nw_proto != IPPROTO_TCP) |
691 | return -EINVAL; | |
692 | tcp_key = nla_data(nla); | |
693 | swkey->tp_src = tcp_key->tcp_src; | |
694 | swkey->tp_dst = tcp_key->tcp_dst; | |
695 | break; | |
696 | ||
697 | case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_UDP): | |
d31f1109 | 698 | case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_UDP): |
36956a7d BP |
699 | if (swkey->nw_proto != IPPROTO_UDP) |
700 | return -EINVAL; | |
701 | udp_key = nla_data(nla); | |
702 | swkey->tp_src = udp_key->udp_src; | |
703 | swkey->tp_dst = udp_key->udp_dst; | |
704 | break; | |
705 | ||
706 | case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_ICMP): | |
707 | if (swkey->nw_proto != IPPROTO_ICMP) | |
708 | return -EINVAL; | |
709 | icmp_key = nla_data(nla); | |
710 | swkey->tp_src = htons(icmp_key->icmp_type); | |
711 | swkey->tp_dst = htons(icmp_key->icmp_code); | |
712 | break; | |
713 | ||
d31f1109 JP |
714 | case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_ICMPV6): |
715 | if (swkey->nw_proto != IPPROTO_ICMPV6) | |
716 | return -EINVAL; | |
717 | icmpv6_key = nla_data(nla); | |
718 | swkey->tp_src = htons(icmpv6_key->icmpv6_type); | |
719 | swkey->tp_dst = htons(icmpv6_key->icmpv6_code); | |
720 | break; | |
721 | ||
36956a7d BP |
722 | case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_ARP): |
723 | if (swkey->dl_type != htons(ETH_P_ARP)) | |
724 | return -EINVAL; | |
725 | arp_key = nla_data(nla); | |
d31f1109 JP |
726 | swkey->ipv4_src = arp_key->arp_sip; |
727 | swkey->ipv4_dst = arp_key->arp_tip; | |
36956a7d BP |
728 | if (arp_key->arp_op & htons(0xff00)) |
729 | return -EINVAL; | |
730 | swkey->nw_proto = ntohs(arp_key->arp_op); | |
bad68a99 JP |
731 | memcpy(swkey->arp_sha, arp_key->arp_sha, ETH_ALEN); |
732 | memcpy(swkey->arp_tha, arp_key->arp_tha, ETH_ALEN); | |
36956a7d BP |
733 | break; |
734 | ||
685a51a5 JP |
735 | case TRANSITION(ODP_KEY_ATTR_ICMPV6, ODP_KEY_ATTR_ND): |
736 | if (swkey->tp_src != htons(NDISC_NEIGHBOUR_SOLICITATION) | |
bfef4717 | 737 | && swkey->tp_src != htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) |
685a51a5 JP |
738 | return -EINVAL; |
739 | nd_key = nla_data(nla); | |
bfef4717 | 740 | memcpy(&swkey->nd_target, nd_key->nd_target, |
685a51a5 JP |
741 | sizeof(swkey->nd_target)); |
742 | memcpy(swkey->arp_sha, nd_key->nd_sll, ETH_ALEN); | |
743 | memcpy(swkey->arp_tha, nd_key->nd_tll, ETH_ALEN); | |
744 | break; | |
745 | ||
36956a7d BP |
746 | default: |
747 | return -EINVAL; | |
748 | } | |
749 | ||
750 | prev_type = type; | |
751 | } | |
752 | if (rem) | |
753 | return -EINVAL; | |
754 | ||
755 | switch (prev_type) { | |
756 | case ODP_KEY_ATTR_UNSPEC: | |
757 | return -EINVAL; | |
758 | ||
759 | case ODP_KEY_ATTR_TUN_ID: | |
760 | case ODP_KEY_ATTR_IN_PORT: | |
761 | return -EINVAL; | |
762 | ||
763 | case ODP_KEY_ATTR_ETHERNET: | |
764 | case ODP_KEY_ATTR_8021Q: | |
765 | return 0; | |
766 | ||
767 | case ODP_KEY_ATTR_ETHERTYPE: | |
768 | if (swkey->dl_type == htons(ETH_P_IP) || | |
769 | swkey->dl_type == htons(ETH_P_ARP)) | |
770 | return -EINVAL; | |
771 | return 0; | |
772 | ||
773 | case ODP_KEY_ATTR_IPV4: | |
774 | if (swkey->nw_proto == IPPROTO_TCP || | |
775 | swkey->nw_proto == IPPROTO_UDP || | |
776 | swkey->nw_proto == IPPROTO_ICMP) | |
777 | return -EINVAL; | |
778 | return 0; | |
779 | ||
d31f1109 JP |
780 | case ODP_KEY_ATTR_IPV6: |
781 | if (swkey->nw_proto == IPPROTO_TCP || | |
782 | swkey->nw_proto == IPPROTO_UDP || | |
783 | swkey->nw_proto == IPPROTO_ICMPV6) | |
784 | return -EINVAL; | |
785 | return 0; | |
786 | ||
685a51a5 JP |
787 | case ODP_KEY_ATTR_ICMPV6: |
788 | if (swkey->tp_src == htons(NDISC_NEIGHBOUR_SOLICITATION) || | |
789 | swkey->tp_src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) | |
790 | return -EINVAL; | |
791 | return 0; | |
792 | ||
36956a7d BP |
793 | case ODP_KEY_ATTR_TCP: |
794 | case ODP_KEY_ATTR_UDP: | |
795 | case ODP_KEY_ATTR_ICMP: | |
796 | case ODP_KEY_ATTR_ARP: | |
685a51a5 | 797 | case ODP_KEY_ATTR_ND: |
36956a7d BP |
798 | return 0; |
799 | } | |
800 | ||
801 | WARN_ON_ONCE(1); | |
802 | return -EINVAL; | |
803 | } | |
804 | ||
d6569377 | 805 | int flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb) |
36956a7d BP |
806 | { |
807 | struct odp_key_ethernet *eth_key; | |
d6569377 | 808 | struct nlattr *nla; |
36956a7d | 809 | |
18c43631 JP |
810 | /* This is an imperfect sanity-check that FLOW_BUFSIZE doesn't need |
811 | * to be updated, but will at least raise awareness when new ODP key | |
812 | * types are added. */ | |
813 | BUILD_BUG_ON(__ODP_KEY_ATTR_MAX != 14); | |
814 | ||
36956a7d | 815 | if (swkey->tun_id != cpu_to_be64(0)) |
d6569377 | 816 | NLA_PUT_BE64(skb, ODP_KEY_ATTR_TUN_ID, swkey->tun_id); |
36956a7d | 817 | |
d6569377 | 818 | NLA_PUT_U32(skb, ODP_KEY_ATTR_IN_PORT, swkey->in_port); |
36956a7d | 819 | |
d6569377 BP |
820 | nla = nla_reserve(skb, ODP_KEY_ATTR_ETHERNET, sizeof(*eth_key)); |
821 | if (!nla) | |
822 | goto nla_put_failure; | |
823 | eth_key = nla_data(nla); | |
36956a7d BP |
824 | memcpy(eth_key->eth_src, swkey->dl_src, ETH_ALEN); |
825 | memcpy(eth_key->eth_dst, swkey->dl_dst, ETH_ALEN); | |
826 | ||
827 | if (swkey->dl_tci != htons(0)) { | |
d6569377 | 828 | struct odp_key_8021q q_key; |
36956a7d | 829 | |
d6569377 BP |
830 | q_key.q_tpid = htons(ETH_P_8021Q); |
831 | q_key.q_tci = swkey->dl_tci & ~htons(VLAN_TAG_PRESENT); | |
832 | NLA_PUT(skb, ODP_KEY_ATTR_8021Q, sizeof(q_key), &q_key); | |
36956a7d BP |
833 | } |
834 | ||
835 | if (swkey->dl_type == htons(ETH_P_802_2)) | |
d6569377 | 836 | return 0; |
36956a7d | 837 | |
d6569377 | 838 | NLA_PUT_BE16(skb, ODP_KEY_ATTR_ETHERTYPE, swkey->dl_type); |
36956a7d BP |
839 | |
840 | if (swkey->dl_type == htons(ETH_P_IP)) { | |
841 | struct odp_key_ipv4 *ipv4_key; | |
842 | ||
d6569377 BP |
843 | nla = nla_reserve(skb, ODP_KEY_ATTR_IPV4, sizeof(*ipv4_key)); |
844 | if (!nla) | |
845 | goto nla_put_failure; | |
846 | ipv4_key = nla_data(nla); | |
535d6987 | 847 | memset(ipv4_key, 0, sizeof(struct odp_key_ipv4)); |
d31f1109 JP |
848 | ipv4_key->ipv4_src = swkey->ipv4_src; |
849 | ipv4_key->ipv4_dst = swkey->ipv4_dst; | |
36956a7d BP |
850 | ipv4_key->ipv4_proto = swkey->nw_proto; |
851 | ipv4_key->ipv4_tos = swkey->nw_tos; | |
d31f1109 JP |
852 | } else if (swkey->dl_type == htons(ETH_P_IPV6)) { |
853 | struct odp_key_ipv6 *ipv6_key; | |
854 | ||
855 | nla = nla_reserve(skb, ODP_KEY_ATTR_IPV6, sizeof(*ipv6_key)); | |
856 | if (!nla) | |
857 | goto nla_put_failure; | |
858 | ipv6_key = nla_data(nla); | |
535d6987 | 859 | memset(ipv6_key, 0, sizeof(struct odp_key_ipv6)); |
bfef4717 | 860 | memcpy(ipv6_key->ipv6_src, &swkey->ipv6_src, |
d31f1109 | 861 | sizeof(ipv6_key->ipv6_src)); |
bfef4717 | 862 | memcpy(ipv6_key->ipv6_dst, &swkey->ipv6_dst, |
d31f1109 JP |
863 | sizeof(ipv6_key->ipv6_dst)); |
864 | ipv6_key->ipv6_proto = swkey->nw_proto; | |
865 | ipv6_key->ipv6_tos = swkey->nw_tos; | |
866 | } else if (swkey->dl_type == htons(ETH_P_ARP)) { | |
867 | struct odp_key_arp *arp_key; | |
868 | ||
869 | nla = nla_reserve(skb, ODP_KEY_ATTR_ARP, sizeof(*arp_key)); | |
870 | if (!nla) | |
871 | goto nla_put_failure; | |
872 | arp_key = nla_data(nla); | |
535d6987 | 873 | memset(arp_key, 0, sizeof(struct odp_key_arp)); |
d31f1109 JP |
874 | arp_key->arp_sip = swkey->ipv4_src; |
875 | arp_key->arp_tip = swkey->ipv4_dst; | |
876 | arp_key->arp_op = htons(swkey->nw_proto); | |
877 | memcpy(arp_key->arp_sha, swkey->arp_sha, ETH_ALEN); | |
878 | memcpy(arp_key->arp_tha, swkey->arp_tha, ETH_ALEN); | |
879 | } | |
880 | ||
bfef4717 JG |
881 | if (swkey->dl_type == htons(ETH_P_IP) || |
882 | swkey->dl_type == htons(ETH_P_IPV6)) { | |
36956a7d BP |
883 | |
884 | if (swkey->nw_proto == IPPROTO_TCP) { | |
885 | struct odp_key_tcp *tcp_key; | |
886 | ||
d6569377 BP |
887 | nla = nla_reserve(skb, ODP_KEY_ATTR_TCP, sizeof(*tcp_key)); |
888 | if (!nla) | |
889 | goto nla_put_failure; | |
890 | tcp_key = nla_data(nla); | |
36956a7d BP |
891 | tcp_key->tcp_src = swkey->tp_src; |
892 | tcp_key->tcp_dst = swkey->tp_dst; | |
893 | } else if (swkey->nw_proto == IPPROTO_UDP) { | |
894 | struct odp_key_udp *udp_key; | |
895 | ||
d6569377 BP |
896 | nla = nla_reserve(skb, ODP_KEY_ATTR_UDP, sizeof(*udp_key)); |
897 | if (!nla) | |
898 | goto nla_put_failure; | |
899 | udp_key = nla_data(nla); | |
36956a7d BP |
900 | udp_key->udp_src = swkey->tp_src; |
901 | udp_key->udp_dst = swkey->tp_dst; | |
bfef4717 JG |
902 | } else if (swkey->dl_type == htons(ETH_P_IP) && |
903 | swkey->nw_proto == IPPROTO_ICMP) { | |
36956a7d BP |
904 | struct odp_key_icmp *icmp_key; |
905 | ||
d6569377 BP |
906 | nla = nla_reserve(skb, ODP_KEY_ATTR_ICMP, sizeof(*icmp_key)); |
907 | if (!nla) | |
908 | goto nla_put_failure; | |
909 | icmp_key = nla_data(nla); | |
36956a7d BP |
910 | icmp_key->icmp_type = ntohs(swkey->tp_src); |
911 | icmp_key->icmp_code = ntohs(swkey->tp_dst); | |
bfef4717 JG |
912 | } else if (swkey->dl_type == htons(ETH_P_IPV6) && |
913 | swkey->nw_proto == IPPROTO_ICMPV6) { | |
d31f1109 | 914 | struct odp_key_icmpv6 *icmpv6_key; |
36956a7d | 915 | |
bfef4717 JG |
916 | nla = nla_reserve(skb, ODP_KEY_ATTR_ICMPV6, |
917 | sizeof(*icmpv6_key)); | |
d31f1109 JP |
918 | if (!nla) |
919 | goto nla_put_failure; | |
920 | icmpv6_key = nla_data(nla); | |
921 | icmpv6_key->icmpv6_type = ntohs(swkey->tp_src); | |
922 | icmpv6_key->icmpv6_code = ntohs(swkey->tp_dst); | |
685a51a5 | 923 | |
bfef4717 JG |
924 | if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION || |
925 | icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) { | |
685a51a5 JP |
926 | struct odp_key_nd *nd_key; |
927 | ||
928 | nla = nla_reserve(skb, ODP_KEY_ATTR_ND, sizeof(*nd_key)); | |
929 | if (!nla) | |
930 | goto nla_put_failure; | |
931 | nd_key = nla_data(nla); | |
bfef4717 | 932 | memcpy(nd_key->nd_target, &swkey->nd_target, |
685a51a5 JP |
933 | sizeof(nd_key->nd_target)); |
934 | memcpy(nd_key->nd_sll, swkey->arp_sha, ETH_ALEN); | |
935 | memcpy(nd_key->nd_tll, swkey->arp_tha, ETH_ALEN); | |
936 | } | |
d31f1109 | 937 | } |
36956a7d BP |
938 | } |
939 | ||
d6569377 | 940 | return 0; |
36956a7d | 941 | |
d6569377 BP |
942 | nla_put_failure: |
943 | return -EMSGSIZE; | |
8d5ebd83 JG |
944 | } |
945 | ||
064af421 BP |
946 | /* Initializes the flow module. |
947 | * Returns zero if successful or a negative error code. */ | |
948 | int flow_init(void) | |
949 | { | |
950 | flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0, | |
951 | 0, NULL); | |
952 | if (flow_cache == NULL) | |
953 | return -ENOMEM; | |
954 | ||
84c17d98 | 955 | get_random_bytes(&hash_seed, sizeof(hash_seed)); |
8d5ebd83 | 956 | |
064af421 BP |
957 | return 0; |
958 | } | |
959 | ||
960 | /* Uninitializes the flow module. */ | |
961 | void flow_exit(void) | |
962 | { | |
963 | kmem_cache_destroy(flow_cache); | |
964 | } |