]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e23775f2 | 2 | * Copyright (c) 2007-2015 Nicira, Inc. |
a14bc59f | 3 | * |
a9a29d22 JG |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of version 2 of the GNU General Public | |
6 | * License as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
16 | * 02110-1301, USA | |
064af421 BP |
17 | */ |
18 | ||
6455100f | 19 | #include <linux/uaccess.h> |
064af421 BP |
20 | #include <linux/netdevice.h> |
21 | #include <linux/etherdevice.h> | |
22 | #include <linux/if_ether.h> | |
23 | #include <linux/if_vlan.h> | |
24 | #include <net/llc_pdu.h> | |
25 | #include <linux/kernel.h> | |
8d5ebd83 | 26 | #include <linux/jhash.h> |
064af421 BP |
27 | #include <linux/jiffies.h> |
28 | #include <linux/llc.h> | |
29 | #include <linux/module.h> | |
30 | #include <linux/in.h> | |
31 | #include <linux/rcupdate.h> | |
f7dc6d34 | 32 | #include <linux/cpumask.h> |
a26ef517 | 33 | #include <linux/if_arp.h> |
064af421 | 34 | #include <linux/ip.h> |
d31f1109 | 35 | #include <linux/ipv6.h> |
2baf0e0c | 36 | #include <linux/mpls.h> |
10f72e3d | 37 | #include <linux/sctp.h> |
b0b906cc | 38 | #include <linux/smp.h> |
064af421 BP |
39 | #include <linux/tcp.h> |
40 | #include <linux/udp.h> | |
41 | #include <linux/icmp.h> | |
d31f1109 | 42 | #include <linux/icmpv6.h> |
3544358a | 43 | #include <linux/rculist.h> |
064af421 | 44 | #include <net/ip.h> |
d31f1109 | 45 | #include <net/ipv6.h> |
2baf0e0c | 46 | #include <net/mpls.h> |
685a51a5 | 47 | #include <net/ndisc.h> |
064af421 | 48 | |
c135bba1 | 49 | #include "datapath.h" |
a94ebc39 | 50 | #include "conntrack.h" |
c135bba1 PS |
51 | #include "flow.h" |
52 | #include "flow_netlink.h" | |
e23775f2 | 53 | #include "vport.h" |
6ce39213 | 54 | |
a097c0b2 | 55 | u64 ovs_flow_used_time(unsigned long flow_jiffies) |
a1c564be | 56 | { |
a097c0b2 PS |
57 | struct timespec cur_ts; |
58 | u64 cur_ms, idle_ms; | |
a1c564be | 59 | |
a097c0b2 PS |
60 | ktime_get_ts(&cur_ts); |
61 | idle_ms = jiffies_to_msecs(jiffies - flow_jiffies); | |
62 | cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC + | |
63 | cur_ts.tv_nsec / NSEC_PER_MSEC; | |
a1c564be | 64 | |
a097c0b2 | 65 | return cur_ms - idle_ms; |
c2dd5e99 AZ |
66 | } |
67 | ||
a66733a8 | 68 | #define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF)) |
a1c564be | 69 | |
a5b8d49b | 70 | void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags, |
f1f60b85 | 71 | const struct sk_buff *skb) |
a1c564be | 72 | { |
b0f3a2fe | 73 | struct flow_stats *stats; |
9ac56358 | 74 | int node = numa_node_id(); |
f7dc6d34 | 75 | int cpu = smp_processor_id(); |
efd8a18e | 76 | int len = skb->len + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0); |
a1c564be | 77 | |
f7dc6d34 | 78 | stats = rcu_dereference(flow->stats[cpu]); |
b0f3a2fe | 79 | |
f7dc6d34 | 80 | /* Check if already have CPU-specific stats. */ |
9ac56358 JR |
81 | if (likely(stats)) { |
82 | spin_lock(&stats->lock); | |
83 | /* Mark if we write on the pre-allocated stats. */ | |
f7dc6d34 TLSC |
84 | if (cpu == 0 && unlikely(flow->stats_last_writer != cpu)) |
85 | flow->stats_last_writer = cpu; | |
9ac56358 JR |
86 | } else { |
87 | stats = rcu_dereference(flow->stats[0]); /* Pre-allocated. */ | |
88 | spin_lock(&stats->lock); | |
89 | ||
f7dc6d34 | 90 | /* If the current CPU is the only writer on the |
9ac56358 JR |
91 | * pre-allocated stats keep using them. |
92 | */ | |
f7dc6d34 | 93 | if (unlikely(flow->stats_last_writer != cpu)) { |
9ac56358 | 94 | /* A previous locker may have already allocated the |
f7dc6d34 | 95 | * stats, so we need to check again. If CPU-specific |
9ac56358 JR |
96 | * stats were already allocated, we update the pre- |
97 | * allocated stats as we have already locked them. | |
98 | */ | |
f7dc6d34 TLSC |
99 | if (likely(flow->stats_last_writer != -1) && |
100 | likely(!rcu_access_pointer(flow->stats[cpu]))) { | |
101 | /* Try to allocate CPU-specific stats. */ | |
9ac56358 JR |
102 | struct flow_stats *new_stats; |
103 | ||
104 | new_stats = | |
105 | kmem_cache_alloc_node(flow_stats_cache, | |
c0cddcec JS |
106 | GFP_NOWAIT | |
107 | __GFP_THISNODE | | |
108 | __GFP_NOWARN | | |
9ac56358 JR |
109 | __GFP_NOMEMALLOC, |
110 | node); | |
111 | if (likely(new_stats)) { | |
112 | new_stats->used = jiffies; | |
113 | new_stats->packet_count = 1; | |
9e917416 | 114 | new_stats->byte_count = len; |
9ac56358 JR |
115 | new_stats->tcp_flags = tcp_flags; |
116 | spin_lock_init(&new_stats->lock); | |
117 | ||
f7dc6d34 | 118 | rcu_assign_pointer(flow->stats[cpu], |
9ac56358 JR |
119 | new_stats); |
120 | goto unlock; | |
121 | } | |
122 | } | |
f7dc6d34 | 123 | flow->stats_last_writer = cpu; |
9ac56358 JR |
124 | } |
125 | } | |
126 | ||
b0b906cc PS |
127 | stats->used = jiffies; |
128 | stats->packet_count++; | |
9e917416 | 129 | stats->byte_count += len; |
b0b906cc | 130 | stats->tcp_flags |= tcp_flags; |
9ac56358 | 131 | unlock: |
b0b906cc PS |
132 | spin_unlock(&stats->lock); |
133 | } | |
134 | ||
4bb90bea JR |
135 | /* Must be called with rcu_read_lock or ovs_mutex. */ |
136 | void ovs_flow_stats_get(const struct sw_flow *flow, | |
137 | struct ovs_flow_stats *ovs_stats, | |
b0f3a2fe PS |
138 | unsigned long *used, __be16 *tcp_flags) |
139 | { | |
f7dc6d34 | 140 | int cpu; |
b0b906cc | 141 | |
b0f3a2fe PS |
142 | *used = 0; |
143 | *tcp_flags = 0; | |
144 | memset(ovs_stats, 0, sizeof(*ovs_stats)); | |
b0b906cc | 145 | |
f7dc6d34 TLSC |
146 | /* We open code this to make sure cpu 0 is always considered */ |
147 | for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpu_possible_mask)) { | |
148 | struct flow_stats *stats = rcu_dereference_ovsl(flow->stats[cpu]); | |
9ac56358 JR |
149 | |
150 | if (stats) { | |
151 | /* Local CPU may write on non-local stats, so we must | |
152 | * block bottom-halves here. | |
153 | */ | |
154 | spin_lock_bh(&stats->lock); | |
4029c043 | 155 | if (!*used || time_after(stats->used, *used)) |
9ac56358 JR |
156 | *used = stats->used; |
157 | *tcp_flags |= stats->tcp_flags; | |
158 | ovs_stats->n_packets += stats->packet_count; | |
159 | ovs_stats->n_bytes += stats->byte_count; | |
160 | spin_unlock_bh(&stats->lock); | |
161 | } | |
b0b906cc | 162 | } |
b0b906cc PS |
163 | } |
164 | ||
4bb90bea | 165 | /* Called with ovs_mutex. */ |
b0b906cc PS |
166 | void ovs_flow_stats_clear(struct sw_flow *flow) |
167 | { | |
f7dc6d34 | 168 | int cpu; |
9ac56358 | 169 | |
f7dc6d34 TLSC |
170 | /* We open code this to make sure cpu 0 is always considered */ |
171 | for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpu_possible_mask)) { | |
172 | struct flow_stats *stats = ovsl_dereference(flow->stats[cpu]); | |
9ac56358 JR |
173 | |
174 | if (stats) { | |
175 | spin_lock_bh(&stats->lock); | |
176 | stats->used = 0; | |
177 | stats->packet_count = 0; | |
178 | stats->byte_count = 0; | |
179 | stats->tcp_flags = 0; | |
180 | spin_unlock_bh(&stats->lock); | |
181 | } | |
b0b906cc | 182 | } |
a1c564be AZ |
183 | } |
184 | ||
2db65bf7 JG |
185 | static int check_header(struct sk_buff *skb, int len) |
186 | { | |
187 | if (unlikely(skb->len < len)) | |
188 | return -EINVAL; | |
189 | if (unlikely(!pskb_may_pull(skb, len))) | |
190 | return -ENOMEM; | |
191 | return 0; | |
192 | } | |
193 | ||
6455100f | 194 | static bool arphdr_ok(struct sk_buff *skb) |
a26ef517 | 195 | { |
2db65bf7 JG |
196 | return pskb_may_pull(skb, skb_network_offset(skb) + |
197 | sizeof(struct arp_eth_header)); | |
a26ef517 JP |
198 | } |
199 | ||
6455100f | 200 | static int check_iphdr(struct sk_buff *skb) |
064af421 | 201 | { |
4c1ad233 BP |
202 | unsigned int nh_ofs = skb_network_offset(skb); |
203 | unsigned int ip_len; | |
2db65bf7 | 204 | int err; |
4c1ad233 | 205 | |
2db65bf7 JG |
206 | err = check_header(skb, nh_ofs + sizeof(struct iphdr)); |
207 | if (unlikely(err)) | |
208 | return err; | |
4c1ad233 BP |
209 | |
210 | ip_len = ip_hdrlen(skb); | |
2db65bf7 JG |
211 | if (unlikely(ip_len < sizeof(struct iphdr) || |
212 | skb->len < nh_ofs + ip_len)) | |
4c1ad233 BP |
213 | return -EINVAL; |
214 | ||
4c1ad233 BP |
215 | skb_set_transport_header(skb, nh_ofs + ip_len); |
216 | return 0; | |
064af421 BP |
217 | } |
218 | ||
6455100f | 219 | static bool tcphdr_ok(struct sk_buff *skb) |
064af421 BP |
220 | { |
221 | int th_ofs = skb_transport_offset(skb); | |
2db65bf7 JG |
222 | int tcp_len; |
223 | ||
224 | if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr)))) | |
225 | return false; | |
226 | ||
227 | tcp_len = tcp_hdrlen(skb); | |
228 | if (unlikely(tcp_len < sizeof(struct tcphdr) || | |
229 | skb->len < th_ofs + tcp_len)) | |
230 | return false; | |
231 | ||
232 | return true; | |
064af421 BP |
233 | } |
234 | ||
6455100f | 235 | static bool udphdr_ok(struct sk_buff *skb) |
064af421 | 236 | { |
2db65bf7 JG |
237 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
238 | sizeof(struct udphdr)); | |
064af421 BP |
239 | } |
240 | ||
10f72e3d JS |
241 | static bool sctphdr_ok(struct sk_buff *skb) |
242 | { | |
243 | return pskb_may_pull(skb, skb_transport_offset(skb) + | |
244 | sizeof(struct sctphdr)); | |
245 | } | |
246 | ||
6455100f | 247 | static bool icmphdr_ok(struct sk_buff *skb) |
064af421 | 248 | { |
2db65bf7 JG |
249 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
250 | sizeof(struct icmphdr)); | |
064af421 BP |
251 | } |
252 | ||
a1c564be | 253 | static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key) |
d31f1109 JP |
254 | { |
255 | unsigned int nh_ofs = skb_network_offset(skb); | |
256 | unsigned int nh_len; | |
257 | int payload_ofs; | |
d31f1109 JP |
258 | struct ipv6hdr *nh; |
259 | uint8_t nexthdr; | |
0fd0d083 | 260 | __be16 frag_off; |
2db65bf7 | 261 | int err; |
d31f1109 | 262 | |
2db65bf7 JG |
263 | err = check_header(skb, nh_ofs + sizeof(*nh)); |
264 | if (unlikely(err)) | |
265 | return err; | |
d31f1109 JP |
266 | |
267 | nh = ipv6_hdr(skb); | |
268 | nexthdr = nh->nexthdr; | |
269 | payload_ofs = (u8 *)(nh + 1) - skb->data; | |
d31f1109 | 270 | |
28bad473 | 271 | key->ip.proto = NEXTHDR_NONE; |
530180fd | 272 | key->ip.tos = ipv6_get_dsfield(nh); |
a61680c6 | 273 | key->ip.ttl = nh->hop_limit; |
fa8223b7 | 274 | key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL); |
858d1026 JG |
275 | key->ipv6.addr.src = nh->saddr; |
276 | key->ipv6.addr.dst = nh->daddr; | |
d31f1109 | 277 | |
0fd0d083 | 278 | payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off); |
bfef4717 | 279 | |
0fd0d083 JG |
280 | if (frag_off) { |
281 | if (frag_off & htons(~0x7)) | |
282 | key->ip.frag = OVS_FRAG_TYPE_LATER; | |
283 | else | |
284 | key->ip.frag = OVS_FRAG_TYPE_FIRST; | |
9cef26ac JG |
285 | } else { |
286 | key->ip.frag = OVS_FRAG_TYPE_NONE; | |
0fd0d083 JG |
287 | } |
288 | ||
b17b330b PS |
289 | /* Delayed handling of error in ipv6_skip_exthdr() as it |
290 | * always sets frag_off to a valid value which may be | |
291 | * used to set key->ip.frag above. | |
292 | */ | |
293 | if (unlikely(payload_ofs < 0)) | |
294 | return -EPROTO; | |
295 | ||
d31f1109 | 296 | nh_len = payload_ofs - nh_ofs; |
d31f1109 | 297 | skb_set_transport_header(skb, nh_ofs + nh_len); |
28bad473 | 298 | key->ip.proto = nexthdr; |
d31f1109 JP |
299 | return nh_len; |
300 | } | |
301 | ||
302 | static bool icmp6hdr_ok(struct sk_buff *skb) | |
303 | { | |
2db65bf7 JG |
304 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
305 | sizeof(struct icmp6hdr)); | |
d31f1109 | 306 | } |
ec58547a | 307 | |
d7efce7b YY |
308 | /** |
309 | * Parse vlan tag from vlan header. | |
310 | * Returns ERROR on memory error. | |
311 | * Returns 0 if it encounters a non-vlan or incomplete packet. | |
312 | * Returns 1 after successfully parsing vlan tag. | |
313 | */ | |
e9bcd25f YY |
314 | static int parse_vlan_tag(struct sk_buff *skb, struct vlan_head *key_vh, |
315 | bool untag_vlan) | |
064af421 | 316 | { |
d7efce7b | 317 | struct vlan_head *vh = (struct vlan_head *)skb->data; |
50f06e16 | 318 | |
d7efce7b | 319 | if (likely(!eth_type_vlan(vh->tpid))) |
8ddc056d BP |
320 | return 0; |
321 | ||
d7efce7b YY |
322 | if (unlikely(skb->len < sizeof(struct vlan_head) + sizeof(__be16))) |
323 | return 0; | |
324 | ||
325 | if (unlikely(!pskb_may_pull(skb, sizeof(struct vlan_head) + | |
326 | sizeof(__be16)))) | |
2db65bf7 | 327 | return -ENOMEM; |
50f06e16 | 328 | |
d7efce7b YY |
329 | vh = (struct vlan_head *)skb->data; |
330 | key_vh->tci = vh->tci | htons(VLAN_TAG_PRESENT); | |
331 | key_vh->tpid = vh->tpid; | |
332 | ||
e9bcd25f YY |
333 | if (unlikely(untag_vlan)) { |
334 | int offset = skb->data - skb_mac_header(skb); | |
335 | u16 tci; | |
336 | int err; | |
337 | ||
338 | __skb_push(skb, offset); | |
339 | err = __skb_vlan_pop(skb, &tci); | |
340 | __skb_pull(skb, offset); | |
341 | if (err) | |
342 | return err; | |
343 | __vlan_hwaccel_put_tag(skb, key_vh->tpid, tci); | |
344 | } else { | |
345 | __skb_pull(skb, sizeof(struct vlan_head)); | |
346 | } | |
d7efce7b YY |
347 | return 1; |
348 | } | |
349 | ||
a27c454e YY |
350 | static void clear_vlan(struct sw_flow_key *key) |
351 | { | |
352 | key->eth.vlan.tci = 0; | |
353 | key->eth.vlan.tpid = 0; | |
354 | key->eth.cvlan.tci = 0; | |
355 | key->eth.cvlan.tpid = 0; | |
356 | } | |
357 | ||
d7efce7b YY |
358 | static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key) |
359 | { | |
360 | int res; | |
361 | ||
362 | key->eth.vlan.tci = 0; | |
363 | key->eth.vlan.tpid = 0; | |
364 | key->eth.cvlan.tci = 0; | |
365 | key->eth.cvlan.tpid = 0; | |
366 | ||
367 | if (skb_vlan_tag_present(skb)) { | |
368 | key->eth.vlan.tci = htons(skb->vlan_tci); | |
369 | key->eth.vlan.tpid = skb->vlan_proto; | |
370 | } else { | |
371 | /* Parse outer vlan tag in the non-accelerated case. */ | |
e9bcd25f | 372 | res = parse_vlan_tag(skb, &key->eth.vlan, true); |
d7efce7b YY |
373 | if (res <= 0) |
374 | return res; | |
375 | } | |
376 | ||
377 | /* Parse inner vlan tag. */ | |
e9bcd25f | 378 | res = parse_vlan_tag(skb, &key->eth.cvlan, false); |
d7efce7b YY |
379 | if (res <= 0) |
380 | return res; | |
2db65bf7 JG |
381 | |
382 | return 0; | |
50f06e16 BP |
383 | } |
384 | ||
385 | static __be16 parse_ethertype(struct sk_buff *skb) | |
064af421 | 386 | { |
50f06e16 BP |
387 | struct llc_snap_hdr { |
388 | u8 dsap; /* Always 0xAA */ | |
389 | u8 ssap; /* Always 0xAA */ | |
390 | u8 ctrl; | |
391 | u8 oui[3]; | |
8dda8c9b | 392 | __be16 ethertype; |
50f06e16 BP |
393 | }; |
394 | struct llc_snap_hdr *llc; | |
395 | __be16 proto; | |
396 | ||
397 | proto = *(__be16 *) skb->data; | |
398 | __skb_pull(skb, sizeof(__be16)); | |
399 | ||
935fc582 | 400 | if (eth_proto_is_802_3(proto)) |
50f06e16 BP |
401 | return proto; |
402 | ||
2db65bf7 | 403 | if (skb->len < sizeof(struct llc_snap_hdr)) |
36956a7d | 404 | return htons(ETH_P_802_2); |
50f06e16 | 405 | |
2db65bf7 JG |
406 | if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr)))) |
407 | return htons(0); | |
408 | ||
50f06e16 BP |
409 | llc = (struct llc_snap_hdr *) skb->data; |
410 | if (llc->dsap != LLC_SAP_SNAP || | |
411 | llc->ssap != LLC_SAP_SNAP || | |
412 | (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0) | |
36956a7d | 413 | return htons(ETH_P_802_2); |
50f06e16 BP |
414 | |
415 | __skb_pull(skb, sizeof(struct llc_snap_hdr)); | |
9e69bc5f | 416 | |
935fc582 | 417 | if (eth_proto_is_802_3(llc->ethertype)) |
9e69bc5f RL |
418 | return llc->ethertype; |
419 | ||
420 | return htons(ETH_P_802_2); | |
064af421 BP |
421 | } |
422 | ||
685a51a5 | 423 | static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key, |
a1c564be | 424 | int nh_len) |
685a51a5 | 425 | { |
685a51a5 JP |
426 | struct icmp6hdr *icmp = icmp6_hdr(skb); |
427 | ||
428 | /* The ICMPv6 type and code fields use the 16-bit transport port | |
bfef4717 JG |
429 | * fields, so we need to store them in 16-bit network byte order. |
430 | */ | |
708fb4c5 JR |
431 | key->tp.src = htons(icmp->icmp6_type); |
432 | key->tp.dst = htons(icmp->icmp6_code); | |
9cef26ac | 433 | memset(&key->ipv6.nd, 0, sizeof(key->ipv6.nd)); |
685a51a5 | 434 | |
bfef4717 JG |
435 | if (icmp->icmp6_code == 0 && |
436 | (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION || | |
437 | icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) { | |
e977ba19 | 438 | int icmp_len = skb->len - skb_transport_offset(skb); |
685a51a5 JP |
439 | struct nd_msg *nd; |
440 | int offset; | |
441 | ||
442 | /* In order to process neighbor discovery options, we need the | |
bfef4717 JG |
443 | * entire packet. |
444 | */ | |
445 | if (unlikely(icmp_len < sizeof(*nd))) | |
a1c564be AZ |
446 | return 0; |
447 | ||
448 | if (unlikely(skb_linearize(skb))) | |
449 | return -ENOMEM; | |
685a51a5 JP |
450 | |
451 | nd = (struct nd_msg *)skb_transport_header(skb); | |
858d1026 | 452 | key->ipv6.nd.target = nd->target; |
685a51a5 JP |
453 | |
454 | icmp_len -= sizeof(*nd); | |
455 | offset = 0; | |
456 | while (icmp_len >= 8) { | |
6455100f PS |
457 | struct nd_opt_hdr *nd_opt = |
458 | (struct nd_opt_hdr *)(nd->opt + offset); | |
685a51a5 JP |
459 | int opt_len = nd_opt->nd_opt_len * 8; |
460 | ||
bfef4717 | 461 | if (unlikely(!opt_len || opt_len > icmp_len)) |
a1c564be | 462 | return 0; |
685a51a5 | 463 | |
bfef4717 JG |
464 | /* Store the link layer address if the appropriate |
465 | * option is provided. It is considered an error if | |
466 | * the same link layer option is specified twice. | |
467 | */ | |
685a51a5 | 468 | if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR |
bfef4717 | 469 | && opt_len == 8) { |
76abe283 | 470 | if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll))) |
685a51a5 | 471 | goto invalid; |
982a47ec | 472 | ether_addr_copy(key->ipv6.nd.sll, |
7d16c847 | 473 | &nd->opt[offset+sizeof(*nd_opt)]); |
685a51a5 | 474 | } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR |
bfef4717 | 475 | && opt_len == 8) { |
76abe283 | 476 | if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll))) |
685a51a5 | 477 | goto invalid; |
982a47ec | 478 | ether_addr_copy(key->ipv6.nd.tll, |
7d16c847 | 479 | &nd->opt[offset+sizeof(*nd_opt)]); |
685a51a5 JP |
480 | } |
481 | ||
482 | icmp_len -= opt_len; | |
483 | offset += opt_len; | |
484 | } | |
485 | } | |
486 | ||
a1c564be | 487 | return 0; |
685a51a5 JP |
488 | |
489 | invalid: | |
76abe283 AE |
490 | memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target)); |
491 | memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll)); | |
492 | memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll)); | |
685a51a5 | 493 | |
a1c564be | 494 | return 0; |
685a51a5 JP |
495 | } |
496 | ||
a31e0e31 | 497 | /** |
c135bba1 | 498 | * key_extract - extracts a flow key from an Ethernet frame. |
a31e0e31 BP |
499 | * @skb: sk_buff that contains the frame, with skb->data pointing to the |
500 | * Ethernet header | |
a31e0e31 BP |
501 | * @key: output flow key |
502 | * | |
503 | * The caller must ensure that skb->len >= ETH_HLEN. | |
504 | * | |
4c1ad233 BP |
505 | * Returns 0 if successful, otherwise a negative errno value. |
506 | * | |
a27c454e | 507 | * Initializes @skb header fields as follows: |
59a18f80 | 508 | * |
a27c454e | 509 | * - skb->mac_header: the L2 header. |
59a18f80 | 510 | * |
a27c454e YY |
511 | * - skb->network_header: just past the L2 header, or just past the |
512 | * VLAN header, to the first byte of the L2 payload. | |
59a18f80 | 513 | * |
ae9020cf | 514 | * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6 |
d31f1109 JP |
515 | * on output, then just past the IP header, if one is present and |
516 | * of a correct length, otherwise the same as skb->network_header. | |
ae9020cf | 517 | * For other key->eth.type values it is left untouched. |
a27c454e YY |
518 | * |
519 | * - skb->protocol: the type of the data starting at skb->network_header. | |
520 | * Equals to key->eth.type. | |
a31e0e31 | 521 | */ |
c135bba1 | 522 | static int key_extract(struct sk_buff *skb, struct sw_flow_key *key) |
064af421 | 523 | { |
a1c564be | 524 | int error; |
064af421 | 525 | struct ethhdr *eth; |
064af421 | 526 | |
7d16c847 | 527 | /* Flags are always used as part of stats */ |
9cef26ac | 528 | key->tp.flags = 0; |
064af421 | 529 | |
064af421 | 530 | skb_reset_mac_header(skb); |
064af421 | 531 | |
a27c454e YY |
532 | /* Link layer. */ |
533 | clear_vlan(key); | |
8bc50076 | 534 | if (ovs_key_mac_proto(key) == MAC_PROTO_NONE) { |
a27c454e YY |
535 | if (unlikely(eth_type_vlan(skb->protocol))) |
536 | return -EINVAL; | |
76abe283 | 537 | |
a27c454e YY |
538 | skb_reset_network_header(skb); |
539 | } else { | |
540 | eth = eth_hdr(skb); | |
541 | ether_addr_copy(key->eth.src, eth->h_source); | |
542 | ether_addr_copy(key->eth.dst, eth->h_dest); | |
6ce39213 | 543 | |
a27c454e YY |
544 | __skb_pull(skb, 2 * ETH_ALEN); |
545 | /* We are going to push all headers that we pull, so no need to | |
546 | * update skb->csum here. | |
547 | */ | |
6ce39213 | 548 | |
a27c454e YY |
549 | if (unlikely(parse_vlan(skb, key))) |
550 | return -ENOMEM; | |
551 | ||
552 | skb->protocol = parse_ethertype(skb); | |
553 | if (unlikely(skb->protocol == htons(0))) | |
554 | return -ENOMEM; | |
555 | ||
556 | skb_reset_network_header(skb); | |
557 | __skb_push(skb, skb->data - skb_mac_header(skb)); | |
558 | } | |
2db65bf7 | 559 | |
ccf43786 | 560 | skb_reset_mac_len(skb); |
a27c454e | 561 | key->eth.type = skb->protocol; |
064af421 BP |
562 | |
563 | /* Network layer. */ | |
76abe283 | 564 | if (key->eth.type == htons(ETH_P_IP)) { |
4c1ad233 | 565 | struct iphdr *nh; |
7257b535 | 566 | __be16 offset; |
76abe283 | 567 | |
4c1ad233 BP |
568 | error = check_iphdr(skb); |
569 | if (unlikely(error)) { | |
9cef26ac JG |
570 | memset(&key->ip, 0, sizeof(key->ip)); |
571 | memset(&key->ipv4, 0, sizeof(key->ipv4)); | |
4c1ad233 BP |
572 | if (error == -EINVAL) { |
573 | skb->transport_header = skb->network_header; | |
76abe283 | 574 | error = 0; |
4c1ad233 | 575 | } |
a1c564be | 576 | return error; |
4c1ad233 BP |
577 | } |
578 | ||
579 | nh = ip_hdr(skb); | |
76abe283 AE |
580 | key->ipv4.addr.src = nh->saddr; |
581 | key->ipv4.addr.dst = nh->daddr; | |
7257b535 | 582 | |
28bad473 | 583 | key->ip.proto = nh->protocol; |
530180fd | 584 | key->ip.tos = nh->tos; |
a61680c6 | 585 | key->ip.ttl = nh->ttl; |
064af421 | 586 | |
7257b535 BP |
587 | offset = nh->frag_off & htons(IP_OFFSET); |
588 | if (offset) { | |
9e44d715 | 589 | key->ip.frag = OVS_FRAG_TYPE_LATER; |
a1c564be | 590 | return 0; |
7257b535 BP |
591 | } |
592 | if (nh->frag_off & htons(IP_MF) || | |
7d16c847 | 593 | skb_shinfo(skb)->gso_type & SKB_GSO_UDP) |
9e44d715 | 594 | key->ip.frag = OVS_FRAG_TYPE_FIRST; |
9cef26ac JG |
595 | else |
596 | key->ip.frag = OVS_FRAG_TYPE_NONE; | |
b7a31ec1 | 597 | |
7257b535 | 598 | /* Transport layer. */ |
28bad473 | 599 | if (key->ip.proto == IPPROTO_TCP) { |
7257b535 | 600 | if (tcphdr_ok(skb)) { |
5f4d087c | 601 | struct tcphdr *tcp = tcp_hdr(skb); |
708fb4c5 JR |
602 | key->tp.src = tcp->source; |
603 | key->tp.dst = tcp->dest; | |
604 | key->tp.flags = TCP_FLAGS_BE16(tcp); | |
9cef26ac JG |
605 | } else { |
606 | memset(&key->tp, 0, sizeof(key->tp)); | |
5f4d087c | 607 | } |
7d16c847 | 608 | |
28bad473 | 609 | } else if (key->ip.proto == IPPROTO_UDP) { |
7257b535 | 610 | if (udphdr_ok(skb)) { |
5f4d087c | 611 | struct udphdr *udp = udp_hdr(skb); |
708fb4c5 JR |
612 | key->tp.src = udp->source; |
613 | key->tp.dst = udp->dest; | |
9cef26ac JG |
614 | } else { |
615 | memset(&key->tp, 0, sizeof(key->tp)); | |
5f4d087c | 616 | } |
10f72e3d JS |
617 | } else if (key->ip.proto == IPPROTO_SCTP) { |
618 | if (sctphdr_ok(skb)) { | |
619 | struct sctphdr *sctp = sctp_hdr(skb); | |
708fb4c5 JR |
620 | key->tp.src = sctp->source; |
621 | key->tp.dst = sctp->dest; | |
9cef26ac JG |
622 | } else { |
623 | memset(&key->tp, 0, sizeof(key->tp)); | |
10f72e3d | 624 | } |
28bad473 | 625 | } else if (key->ip.proto == IPPROTO_ICMP) { |
7257b535 | 626 | if (icmphdr_ok(skb)) { |
5f4d087c JG |
627 | struct icmphdr *icmp = icmp_hdr(skb); |
628 | /* The ICMP type and code fields use the 16-bit | |
6455100f | 629 | * transport port fields, so we need to store |
af465b67 PS |
630 | * them in 16-bit network byte order. |
631 | */ | |
708fb4c5 JR |
632 | key->tp.src = htons(icmp->type); |
633 | key->tp.dst = htons(icmp->code); | |
9cef26ac JG |
634 | } else { |
635 | memset(&key->tp, 0, sizeof(key->tp)); | |
5f4d087c JG |
636 | } |
637 | } | |
638 | ||
9cef26ac JG |
639 | } else if (key->eth.type == htons(ETH_P_ARP) || |
640 | key->eth.type == htons(ETH_P_RARP)) { | |
a26ef517 | 641 | struct arp_eth_header *arp; |
10173cea | 642 | bool arp_available = arphdr_ok(skb); |
a26ef517 JP |
643 | |
644 | arp = (struct arp_eth_header *)skb_network_header(skb); | |
645 | ||
10173cea LR |
646 | if (arp_available && |
647 | arp->ar_hrd == htons(ARPHRD_ETHER) && | |
648 | arp->ar_pro == htons(ETH_P_IP) && | |
649 | arp->ar_hln == ETH_ALEN && | |
650 | arp->ar_pln == 4) { | |
de3f65ea JP |
651 | |
652 | /* We only match on the lower 8 bits of the opcode. */ | |
b7a31ec1 | 653 | if (ntohs(arp->ar_op) <= 0xff) |
28bad473 | 654 | key->ip.proto = ntohs(arp->ar_op); |
9cef26ac JG |
655 | else |
656 | key->ip.proto = 0; | |
657 | ||
a3d3ad0c MM |
658 | memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src)); |
659 | memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst)); | |
982a47ec JP |
660 | ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha); |
661 | ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha); | |
9cef26ac JG |
662 | } else { |
663 | memset(&key->ip, 0, sizeof(key->ip)); | |
664 | memset(&key->ipv4, 0, sizeof(key->ipv4)); | |
de3f65ea | 665 | } |
ccf43786 SH |
666 | } else if (eth_p_mpls(key->eth.type)) { |
667 | size_t stack_len = MPLS_HLEN; | |
668 | ||
176e06dd | 669 | skb_set_inner_network_header(skb, skb->mac_len); |
ccf43786 SH |
670 | while (1) { |
671 | __be32 lse; | |
672 | ||
673 | error = check_header(skb, skb->mac_len + stack_len); | |
674 | if (unlikely(error)) | |
675 | return 0; | |
676 | ||
176e06dd | 677 | memcpy(&lse, skb_inner_network_header(skb), MPLS_HLEN); |
ccf43786 SH |
678 | |
679 | if (stack_len == MPLS_HLEN) | |
680 | memcpy(&key->mpls.top_lse, &lse, MPLS_HLEN); | |
681 | ||
176e06dd | 682 | skb_set_inner_network_header(skb, skb->mac_len + stack_len); |
2baf0e0c | 683 | if (lse & htonl(MPLS_LS_S_MASK)) |
ccf43786 SH |
684 | break; |
685 | ||
686 | stack_len += MPLS_HLEN; | |
687 | } | |
76abe283 | 688 | } else if (key->eth.type == htons(ETH_P_IPV6)) { |
d31f1109 JP |
689 | int nh_len; /* IPv6 Header + Extensions */ |
690 | ||
a1c564be | 691 | nh_len = parse_ipv6hdr(skb, key); |
d31f1109 | 692 | if (unlikely(nh_len < 0)) { |
b17b330b PS |
693 | switch (nh_len) { |
694 | case -EINVAL: | |
695 | memset(&key->ip, 0, sizeof(key->ip)); | |
696 | memset(&key->ipv6.addr, 0, sizeof(key->ipv6.addr)); | |
697 | /* fall-through */ | |
698 | case -EPROTO: | |
d31f1109 | 699 | skb->transport_header = skb->network_header; |
a1c564be | 700 | error = 0; |
b17b330b PS |
701 | break; |
702 | default: | |
76abe283 | 703 | error = nh_len; |
a1c564be AZ |
704 | } |
705 | return error; | |
d31f1109 JP |
706 | } |
707 | ||
9e44d715 | 708 | if (key->ip.frag == OVS_FRAG_TYPE_LATER) |
a1c564be | 709 | return 0; |
7257b535 | 710 | if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) |
9e44d715 | 711 | key->ip.frag = OVS_FRAG_TYPE_FIRST; |
7257b535 | 712 | |
d31f1109 | 713 | /* Transport layer. */ |
28bad473 | 714 | if (key->ip.proto == NEXTHDR_TCP) { |
d31f1109 JP |
715 | if (tcphdr_ok(skb)) { |
716 | struct tcphdr *tcp = tcp_hdr(skb); | |
708fb4c5 JR |
717 | key->tp.src = tcp->source; |
718 | key->tp.dst = tcp->dest; | |
719 | key->tp.flags = TCP_FLAGS_BE16(tcp); | |
9cef26ac JG |
720 | } else { |
721 | memset(&key->tp, 0, sizeof(key->tp)); | |
d31f1109 | 722 | } |
28bad473 | 723 | } else if (key->ip.proto == NEXTHDR_UDP) { |
d31f1109 JP |
724 | if (udphdr_ok(skb)) { |
725 | struct udphdr *udp = udp_hdr(skb); | |
708fb4c5 JR |
726 | key->tp.src = udp->source; |
727 | key->tp.dst = udp->dest; | |
9cef26ac JG |
728 | } else { |
729 | memset(&key->tp, 0, sizeof(key->tp)); | |
d31f1109 | 730 | } |
10f72e3d JS |
731 | } else if (key->ip.proto == NEXTHDR_SCTP) { |
732 | if (sctphdr_ok(skb)) { | |
733 | struct sctphdr *sctp = sctp_hdr(skb); | |
708fb4c5 JR |
734 | key->tp.src = sctp->source; |
735 | key->tp.dst = sctp->dest; | |
9cef26ac JG |
736 | } else { |
737 | memset(&key->tp, 0, sizeof(key->tp)); | |
10f72e3d | 738 | } |
28bad473 | 739 | } else if (key->ip.proto == NEXTHDR_ICMP) { |
d31f1109 | 740 | if (icmp6hdr_ok(skb)) { |
a1c564be AZ |
741 | error = parse_icmpv6(skb, key, nh_len); |
742 | if (error) | |
743 | return error; | |
9cef26ac JG |
744 | } else { |
745 | memset(&key->tp, 0, sizeof(key->tp)); | |
d31f1109 JP |
746 | } |
747 | } | |
064af421 | 748 | } |
a1c564be | 749 | return 0; |
064af421 | 750 | } |
c135bba1 | 751 | |
e0a42ae2 AZ |
752 | int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key) |
753 | { | |
8bc50076 YHW |
754 | int res; |
755 | ||
756 | res = key_extract(skb, key); | |
757 | if (!res) | |
758 | key->mac_proto &= ~SW_FLOW_KEY_INVALID; | |
759 | ||
760 | return res; | |
e0a42ae2 AZ |
761 | } |
762 | ||
a27c454e YY |
763 | static int key_extract_mac_proto(struct sk_buff *skb) |
764 | { | |
765 | switch (skb->dev->type) { | |
766 | case ARPHRD_ETHER: | |
767 | return MAC_PROTO_ETHERNET; | |
768 | case ARPHRD_NONE: | |
769 | if (skb->protocol == htons(ETH_P_TEB)) | |
770 | return MAC_PROTO_ETHERNET; | |
771 | return MAC_PROTO_NONE; | |
772 | } | |
773 | WARN_ON_ONCE(1); | |
774 | return -EINVAL; | |
775 | } | |
776 | ||
e23775f2 | 777 | int ovs_flow_key_extract(const struct ip_tunnel_info *tun_info, |
7d16c847 | 778 | struct sk_buff *skb, struct sw_flow_key *key) |
c135bba1 | 779 | { |
c30b4cea | 780 | int res, err; |
a27c454e | 781 | |
c135bba1 | 782 | /* Extract metadata from packet. */ |
fb66fbd1 | 783 | if (tun_info) { |
8a2d4905 | 784 | key->tun_proto = ip_tunnel_info_af(tun_info); |
e23775f2 | 785 | memcpy(&key->tun_key, &tun_info->key, sizeof(key->tun_key)); |
c135bba1 PS |
786 | BUILD_BUG_ON(((1 << (sizeof(tun_info->options_len) * 8)) - 1) > |
787 | sizeof(key->tun_opts)); | |
788 | ||
e23775f2 PS |
789 | if (tun_info->options_len) { |
790 | ip_tunnel_info_opts_get(TUN_METADATA_OPTS(key, tun_info->options_len), | |
791 | tun_info); | |
c135bba1 PS |
792 | key->tun_opts_len = tun_info->options_len; |
793 | } else { | |
794 | key->tun_opts_len = 0; | |
795 | } | |
8a2d4905 PS |
796 | } else { |
797 | key->tun_proto = 0; | |
c135bba1 PS |
798 | key->tun_opts_len = 0; |
799 | memset(&key->tun_key, 0, sizeof(key->tun_key)); | |
800 | } | |
801 | ||
802 | key->phy.priority = skb->priority; | |
803 | key->phy.in_port = OVS_CB(skb)->input_vport->port_no; | |
804 | key->phy.skb_mark = skb->mark; | |
805 | key->ovs_flow_hash = 0; | |
a27c454e YY |
806 | res = key_extract_mac_proto(skb); |
807 | if (res < 0) | |
808 | return res; | |
809 | key->mac_proto = res; | |
c135bba1 PS |
810 | key->recirc_id = 0; |
811 | ||
c30b4cea JR |
812 | err = key_extract(skb, key); |
813 | if (!err) | |
814 | ovs_ct_fill_key(skb, key); /* Must be after key_extract(). */ | |
815 | return err; | |
c135bba1 PS |
816 | } |
817 | ||
038e34ab | 818 | int ovs_flow_key_extract_userspace(struct net *net, const struct nlattr *attr, |
c135bba1 | 819 | struct sk_buff *skb, |
9233cef7 | 820 | struct sw_flow_key *key, bool log) |
c135bba1 | 821 | { |
c30b4cea JR |
822 | const struct nlattr *a[OVS_KEY_ATTR_MAX + 1]; |
823 | u64 attrs = 0; | |
c135bba1 PS |
824 | int err; |
825 | ||
c30b4cea JR |
826 | err = parse_flow_nlattrs(attr, a, &attrs, log); |
827 | if (err) | |
828 | return -EINVAL; | |
829 | ||
c135bba1 | 830 | /* Extract metadata from netlink attributes. */ |
c30b4cea | 831 | err = ovs_nla_get_flow_metadata(net, a, attrs, key, log); |
c135bba1 PS |
832 | if (err) |
833 | return err; | |
834 | ||
e9bcd25f YY |
835 | /* key_extract assumes that skb->protocol is set-up for |
836 | * layer 3 packets which is the case for other callers, | |
837 | * in particular packets received from the network stack. | |
838 | * Here the correct value can be set from the metadata | |
839 | * extracted above. | |
840 | * For L2 packet key eth type would be zero. skb protocol | |
841 | * would be set to correct value later during key-extact. | |
842 | */ | |
a27c454e | 843 | |
e9bcd25f | 844 | skb->protocol = key->eth.type; |
c30b4cea JR |
845 | err = key_extract(skb, key); |
846 | if (err) | |
847 | return err; | |
848 | ||
849 | /* Check that we have conntrack original direction tuple metadata only | |
850 | * for packets for which it makes sense. Otherwise the key may be | |
851 | * corrupted due to overlapping key fields. | |
852 | */ | |
853 | if (attrs & (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4) && | |
854 | key->eth.type != htons(ETH_P_IP)) | |
855 | return -EINVAL; | |
856 | if (attrs & (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6) && | |
857 | (key->eth.type != htons(ETH_P_IPV6) || | |
858 | sw_flow_key_is_nd(key))) | |
859 | return -EINVAL; | |
860 | ||
861 | return 0; | |
c135bba1 | 862 | } |