]> git.proxmox.com Git - mirror_ovs.git/blame - lib/flow.c
datapath: Properly validate length of OVS_KEY_ATTR_ENCAP attributes.
[mirror_ovs.git] / lib / flow.c
CommitLineData
064af421 1/*
8368c090 2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
064af421 3 *
a14bc59f
BP
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
064af421 7 *
a14bc59f
BP
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
064af421
BP
15 */
16#include <config.h>
17#include <sys/types.h>
18#include "flow.h"
db7f8281 19#include <assert.h>
d31f1109 20#include <errno.h>
064af421
BP
21#include <inttypes.h>
22#include <netinet/in.h>
d31f1109
JP
23#include <netinet/icmp6.h>
24#include <netinet/ip6.h>
064af421
BP
25#include <stdlib.h>
26#include <string.h>
10a24935 27#include "byte-order.h"
064af421
BP
28#include "coverage.h"
29#include "dynamic-string.h"
30#include "hash.h"
31#include "ofpbuf.h"
32#include "openflow/openflow.h"
064af421 33#include "packets.h"
176aaa65 34#include "unaligned.h"
5136ce49 35#include "vlog.h"
064af421 36
d98e6007 37VLOG_DEFINE_THIS_MODULE(flow);
064af421 38
d76f09ea
BP
39COVERAGE_DEFINE(flow_extract);
40
a26ef517
JP
41static struct arp_eth_header *
42pull_arp(struct ofpbuf *packet)
43{
44 return ofpbuf_try_pull(packet, ARP_ETH_HEADER_LEN);
45}
46
064af421
BP
47static struct ip_header *
48pull_ip(struct ofpbuf *packet)
49{
50 if (packet->size >= IP_HEADER_LEN) {
51 struct ip_header *ip = packet->data;
52 int ip_len = IP_IHL(ip->ip_ihl_ver) * 4;
53 if (ip_len >= IP_HEADER_LEN && packet->size >= ip_len) {
54 return ofpbuf_pull(packet, ip_len);
55 }
56 }
57 return NULL;
58}
59
60static struct tcp_header *
d295e8e9 61pull_tcp(struct ofpbuf *packet)
064af421
BP
62{
63 if (packet->size >= TCP_HEADER_LEN) {
64 struct tcp_header *tcp = packet->data;
65 int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
66 if (tcp_len >= TCP_HEADER_LEN && packet->size >= tcp_len) {
67 return ofpbuf_pull(packet, tcp_len);
68 }
69 }
70 return NULL;
71}
72
73static struct udp_header *
d295e8e9 74pull_udp(struct ofpbuf *packet)
064af421
BP
75{
76 return ofpbuf_try_pull(packet, UDP_HEADER_LEN);
77}
78
79static struct icmp_header *
d295e8e9 80pull_icmp(struct ofpbuf *packet)
064af421
BP
81{
82 return ofpbuf_try_pull(packet, ICMP_HEADER_LEN);
83}
84
d31f1109
JP
85static struct icmp6_hdr *
86pull_icmpv6(struct ofpbuf *packet)
87{
88 return ofpbuf_try_pull(packet, sizeof(struct icmp6_hdr));
89}
90
50f06e16 91static void
ae412e7d 92parse_vlan(struct ofpbuf *b, struct flow *flow)
064af421 93{
50f06e16 94 struct qtag_prefix {
0b3e77bb
BP
95 ovs_be16 eth_type; /* ETH_TYPE_VLAN */
96 ovs_be16 tci;
50f06e16
BP
97 };
98
0b3e77bb 99 if (b->size >= sizeof(struct qtag_prefix) + sizeof(ovs_be16)) {
50f06e16 100 struct qtag_prefix *qp = ofpbuf_pull(b, sizeof *qp);
66642cb4 101 flow->vlan_tci = qp->tci | htons(VLAN_CFI);
50f06e16 102 }
064af421
BP
103}
104
0b3e77bb 105static ovs_be16
50f06e16 106parse_ethertype(struct ofpbuf *b)
064af421 107{
50f06e16 108 struct llc_snap_header *llc;
0b3e77bb 109 ovs_be16 proto;
50f06e16 110
0b3e77bb 111 proto = *(ovs_be16 *) ofpbuf_pull(b, sizeof proto);
36956a7d 112 if (ntohs(proto) >= ETH_TYPE_MIN) {
50f06e16
BP
113 return proto;
114 }
115
116 if (b->size < sizeof *llc) {
36956a7d 117 return htons(FLOW_DL_TYPE_NONE);
50f06e16
BP
118 }
119
120 llc = b->data;
121 if (llc->llc.llc_dsap != LLC_DSAP_SNAP
122 || llc->llc.llc_ssap != LLC_SSAP_SNAP
123 || llc->llc.llc_cntl != LLC_CNTL_SNAP
124 || memcmp(llc->snap.snap_org, SNAP_ORG_ETHERNET,
125 sizeof llc->snap.snap_org)) {
36956a7d 126 return htons(FLOW_DL_TYPE_NONE);
50f06e16
BP
127 }
128
129 ofpbuf_pull(b, sizeof *llc);
130 return llc->snap.snap_type;
064af421
BP
131}
132
d31f1109
JP
133static int
134parse_ipv6(struct ofpbuf *packet, struct flow *flow)
135{
88366484 136 const struct ip6_hdr *nh;
d31f1109
JP
137 ovs_be32 tc_flow;
138 int nexthdr;
139
88366484
JG
140 nh = ofpbuf_try_pull(packet, sizeof *nh);
141 if (!nh) {
142 return EINVAL;
d31f1109
JP
143 }
144
d31f1109 145 nexthdr = nh->ip6_nxt;
d31f1109
JP
146
147 flow->ipv6_src = nh->ip6_src;
148 flow->ipv6_dst = nh->ip6_dst;
149
150 tc_flow = get_unaligned_be32(&nh->ip6_flow);
eadef313 151 flow->nw_tos = ntohl(tc_flow) >> 4;
fa8223b7 152 flow->ipv6_label = tc_flow & htonl(IPV6_LABEL_MASK);
a61680c6 153 flow->nw_ttl = nh->ip6_hlim;
d31f1109
JP
154 flow->nw_proto = IPPROTO_NONE;
155
d31f1109
JP
156 while (1) {
157 if ((nexthdr != IPPROTO_HOPOPTS)
158 && (nexthdr != IPPROTO_ROUTING)
159 && (nexthdr != IPPROTO_DSTOPTS)
160 && (nexthdr != IPPROTO_AH)
161 && (nexthdr != IPPROTO_FRAGMENT)) {
162 /* It's either a terminal header (e.g., TCP, UDP) or one we
163 * don't understand. In either case, we're done with the
164 * packet, so use it to fill in 'nw_proto'. */
165 break;
166 }
167
168 /* We only verify that at least 8 bytes of the next header are
169 * available, but many of these headers are longer. Ensure that
170 * accesses within the extension header are within those first 8
88366484 171 * bytes. All extension headers are required to be at least 8
d31f1109 172 * bytes. */
88366484
JG
173 if (packet->size < 8) {
174 return EINVAL;
d31f1109
JP
175 }
176
177 if ((nexthdr == IPPROTO_HOPOPTS)
178 || (nexthdr == IPPROTO_ROUTING)
179 || (nexthdr == IPPROTO_DSTOPTS)) {
180 /* These headers, while different, have the fields we care about
181 * in the same location and with the same interpretation. */
88366484 182 const struct ip6_ext *ext_hdr = (struct ip6_ext *)packet->data;
d31f1109 183 nexthdr = ext_hdr->ip6e_nxt;
88366484
JG
184 if (!ofpbuf_try_pull(packet, (ext_hdr->ip6e_len + 1) * 8)) {
185 return EINVAL;
186 }
d31f1109
JP
187 } else if (nexthdr == IPPROTO_AH) {
188 /* A standard AH definition isn't available, but the fields
189 * we care about are in the same location as the generic
190 * option header--only the header length is calculated
191 * differently. */
88366484 192 const struct ip6_ext *ext_hdr = (struct ip6_ext *)packet->data;
d31f1109 193 nexthdr = ext_hdr->ip6e_nxt;
88366484
JG
194 if (!ofpbuf_try_pull(packet, (ext_hdr->ip6e_len + 2) * 4)) {
195 return EINVAL;
196 }
d31f1109 197 } else if (nexthdr == IPPROTO_FRAGMENT) {
88366484 198 const struct ip6_frag *frag_hdr = (struct ip6_frag *)packet->data;
d31f1109
JP
199
200 nexthdr = frag_hdr->ip6f_nxt;
88366484
JG
201 if (!ofpbuf_try_pull(packet, sizeof *frag_hdr)) {
202 return EINVAL;
203 }
d31f1109
JP
204
205 /* We only process the first fragment. */
eadef313 206 flow->nw_frag = FLOW_NW_FRAG_ANY;
d31f1109 207 if ((frag_hdr->ip6f_offlg & IP6F_OFF_MASK) != htons(0)) {
eadef313 208 flow->nw_frag |= FLOW_NW_FRAG_LATER;
d31f1109
JP
209 nexthdr = IPPROTO_FRAGMENT;
210 break;
211 }
212 }
213 }
214
d31f1109 215 flow->nw_proto = nexthdr;
88366484 216 return 0;
d31f1109
JP
217}
218
88366484
JG
219static void
220parse_tcp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
221{
222 const struct tcp_header *tcp = pull_tcp(b);
223 if (tcp) {
224 flow->tp_src = tcp->tcp_src;
225 flow->tp_dst = tcp->tcp_dst;
226 packet->l7 = b->data;
227 }
228}
229
230static void
231parse_udp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
232{
233 const struct udp_header *udp = pull_udp(b);
234 if (udp) {
235 flow->tp_src = udp->udp_src;
236 flow->tp_dst = udp->udp_dst;
237 packet->l7 = b->data;
238 }
239}
685a51a5
JP
240
241static bool
88366484 242parse_icmpv6(struct ofpbuf *b, struct flow *flow)
685a51a5
JP
243{
244 const struct icmp6_hdr *icmp = pull_icmpv6(b);
245
246 if (!icmp) {
247 return false;
248 }
249
250 /* The ICMPv6 type and code fields use the 16-bit transport port
251 * fields, so we need to store them in 16-bit network byte order. */
3ee8a9f0
BP
252 flow->tp_src = htons(icmp->icmp6_type);
253 flow->tp_dst = htons(icmp->icmp6_code);
685a51a5 254
88366484
JG
255 if (icmp->icmp6_code == 0 &&
256 (icmp->icmp6_type == ND_NEIGHBOR_SOLICIT ||
257 icmp->icmp6_type == ND_NEIGHBOR_ADVERT)) {
258 const struct in6_addr *nd_target;
685a51a5 259
88366484
JG
260 nd_target = ofpbuf_try_pull(b, sizeof *nd_target);
261 if (!nd_target) {
685a51a5
JP
262 return false;
263 }
88366484 264 flow->nd_target = *nd_target;
685a51a5 265
88366484 266 while (b->size >= 8) {
685a51a5
JP
267 /* The minimum size of an option is 8 bytes, which also is
268 * the size of Ethernet link-layer options. */
88366484
JG
269 const struct nd_opt_hdr *nd_opt = b->data;
270 int opt_len = nd_opt->nd_opt_len * 8;
271
272 if (!opt_len || opt_len > b->size) {
685a51a5
JP
273 goto invalid;
274 }
685a51a5
JP
275
276 /* Store the link layer address if the appropriate option is
277 * provided. It is considered an error if the same link
278 * layer option is specified twice. */
279 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR
280 && opt_len == 8) {
281 if (eth_addr_is_zero(flow->arp_sha)) {
88366484 282 memcpy(flow->arp_sha, nd_opt + 1, ETH_ADDR_LEN);
685a51a5
JP
283 } else {
284 goto invalid;
285 }
286 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR
287 && opt_len == 8) {
288 if (eth_addr_is_zero(flow->arp_tha)) {
88366484 289 memcpy(flow->arp_tha, nd_opt + 1, ETH_ADDR_LEN);
685a51a5
JP
290 } else {
291 goto invalid;
292 }
293 }
294
88366484 295 if (!ofpbuf_try_pull(b, opt_len)) {
685a51a5
JP
296 goto invalid;
297 }
685a51a5
JP
298 }
299 }
300
301 return true;
302
303invalid:
88366484
JG
304 memset(&flow->nd_target, 0, sizeof(flow->nd_target));
305 memset(flow->arp_sha, 0, sizeof(flow->arp_sha));
306 memset(flow->arp_tha, 0, sizeof(flow->arp_tha));
685a51a5
JP
307
308 return false;
309
310}
311
abe529af 312/* Initializes 'flow' members from 'packet', 'tun_id', and 'ofp_in_port'.
0b3e77bb 313 * Initializes 'packet' header pointers as follows:
ca78c6b6
BP
314 *
315 * - packet->l2 to the start of the Ethernet header.
316 *
317 * - packet->l3 to just past the Ethernet header, or just past the
318 * vlan_header if one is present, to the first byte of the payload of the
319 * Ethernet frame.
320 *
321 * - packet->l4 to just past the IPv4 header, if one is present and has a
322 * correct length, and otherwise NULL.
323 *
324 * - packet->l7 to just past the TCP or UDP or ICMP header, if one is
325 * present and has a correct length, and otherwise NULL.
326 */
7257b535 327void
abff858b
PS
328flow_extract(struct ofpbuf *packet, uint32_t priority, ovs_be64 tun_id,
329 uint16_t ofp_in_port, struct flow *flow)
064af421
BP
330{
331 struct ofpbuf b = *packet;
332 struct eth_header *eth;
064af421
BP
333
334 COVERAGE_INC(flow_extract);
335
336 memset(flow, 0, sizeof *flow);
659586ef 337 flow->tun_id = tun_id;
abe529af 338 flow->in_port = ofp_in_port;
abff858b 339 flow->priority = priority;
064af421
BP
340
341 packet->l2 = b.data;
342 packet->l3 = NULL;
343 packet->l4 = NULL;
344 packet->l7 = NULL;
345
50f06e16 346 if (b.size < sizeof *eth) {
7257b535 347 return;
50f06e16 348 }
064af421 349
50f06e16
BP
350 /* Link layer. */
351 eth = b.data;
352 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
353 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
354
66642cb4 355 /* dl_type, vlan_tci. */
50f06e16
BP
356 ofpbuf_pull(&b, ETH_ADDR_LEN * 2);
357 if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
358 parse_vlan(&b, flow);
359 }
360 flow->dl_type = parse_ethertype(&b);
361
362 /* Network layer. */
363 packet->l3 = b.data;
364 if (flow->dl_type == htons(ETH_TYPE_IP)) {
365 const struct ip_header *nh = pull_ip(&b);
366 if (nh) {
7257b535
BP
367 packet->l4 = b.data;
368
9ea5d2d5
BP
369 flow->nw_src = get_unaligned_be32(&nh->ip_src);
370 flow->nw_dst = get_unaligned_be32(&nh->ip_dst);
50f06e16 371 flow->nw_proto = nh->ip_proto;
7257b535 372
eadef313 373 flow->nw_tos = nh->ip_tos;
7257b535 374 if (IP_IS_FRAGMENT(nh->ip_frag_off)) {
eadef313 375 flow->nw_frag = FLOW_NW_FRAG_ANY;
7257b535 376 if (nh->ip_frag_off & htons(IP_FRAG_OFF_MASK)) {
eadef313 377 flow->nw_frag |= FLOW_NW_FRAG_LATER;
7257b535
BP
378 }
379 }
a61680c6 380 flow->nw_ttl = nh->ip_ttl;
7257b535
BP
381
382 if (!(nh->ip_frag_off & htons(IP_FRAG_OFF_MASK))) {
6767a2cc 383 if (flow->nw_proto == IPPROTO_TCP) {
88366484 384 parse_tcp(packet, &b, flow);
6767a2cc 385 } else if (flow->nw_proto == IPPROTO_UDP) {
88366484 386 parse_udp(packet, &b, flow);
6767a2cc 387 } else if (flow->nw_proto == IPPROTO_ICMP) {
50f06e16
BP
388 const struct icmp_header *icmp = pull_icmp(&b);
389 if (icmp) {
3ee8a9f0
BP
390 flow->tp_src = htons(icmp->icmp_type);
391 flow->tp_dst = htons(icmp->icmp_code);
50f06e16 392 packet->l7 = b.data;
064af421 393 }
064af421 394 }
50f06e16
BP
395 }
396 }
d31f1109 397 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
7257b535
BP
398 if (parse_ipv6(&b, flow)) {
399 return;
d31f1109
JP
400 }
401
88366484
JG
402 packet->l4 = b.data;
403 if (flow->nw_proto == IPPROTO_TCP) {
404 parse_tcp(packet, &b, flow);
405 } else if (flow->nw_proto == IPPROTO_UDP) {
406 parse_udp(packet, &b, flow);
407 } else if (flow->nw_proto == IPPROTO_ICMPV6) {
408 if (parse_icmpv6(&b, flow)) {
409 packet->l7 = b.data;
d31f1109
JP
410 }
411 }
50f06e16
BP
412 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
413 const struct arp_eth_header *arp = pull_arp(&b);
414 if (arp && arp->ar_hrd == htons(1)
d295e8e9 415 && arp->ar_pro == htons(ETH_TYPE_IP)
50f06e16
BP
416 && arp->ar_hln == ETH_ADDR_LEN
417 && arp->ar_pln == 4) {
418 /* We only match on the lower 8 bits of the opcode. */
419 if (ntohs(arp->ar_op) <= 0xff) {
420 flow->nw_proto = ntohs(arp->ar_op);
064af421 421 }
a26ef517 422
d295e8e9 423 if ((flow->nw_proto == ARP_OP_REQUEST)
50f06e16
BP
424 || (flow->nw_proto == ARP_OP_REPLY)) {
425 flow->nw_src = arp->ar_spa;
426 flow->nw_dst = arp->ar_tpa;
bad68a99
JP
427 memcpy(flow->arp_sha, arp->ar_sha, ETH_ADDR_LEN);
428 memcpy(flow->arp_tha, arp->ar_tha, ETH_ADDR_LEN);
a26ef517 429 }
064af421
BP
430 }
431 }
064af421
BP
432}
433
993410fb
BP
434/* For every bit of a field that is wildcarded in 'wildcards', sets the
435 * corresponding bit in 'flow' to zero. */
436void
437flow_zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
438{
439 const flow_wildcards_t wc = wildcards->wildcards;
440 int i;
441
2486e66a 442 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
993410fb
BP
443
444 for (i = 0; i < FLOW_N_REGS; i++) {
445 flow->regs[i] &= wildcards->reg_masks[i];
446 }
447 flow->tun_id &= wildcards->tun_id_mask;
448 flow->nw_src &= wildcards->nw_src_mask;
449 flow->nw_dst &= wildcards->nw_dst_mask;
450 if (wc & FWW_IN_PORT) {
451 flow->in_port = 0;
452 }
453 flow->vlan_tci &= wildcards->vlan_tci_mask;
454 if (wc & FWW_DL_TYPE) {
cb9457e2 455 flow->dl_type = htons(0);
993410fb
BP
456 }
457 if (wc & FWW_TP_SRC) {
cb9457e2 458 flow->tp_src = htons(0);
993410fb
BP
459 }
460 if (wc & FWW_TP_DST) {
cb9457e2 461 flow->tp_dst = htons(0);
993410fb
BP
462 }
463 if (wc & FWW_DL_SRC) {
464 memset(flow->dl_src, 0, sizeof flow->dl_src);
465 }
466 if (wc & FWW_DL_DST) {
467 flow->dl_dst[0] &= 0x01;
468 memset(&flow->dl_dst[1], 0, 5);
469 }
470 if (wc & FWW_ETH_MCAST) {
471 flow->dl_dst[0] &= 0xfe;
472 }
473 if (wc & FWW_NW_PROTO) {
474 flow->nw_proto = 0;
475 }
fa8223b7
JP
476 if (wc & FWW_IPV6_LABEL) {
477 flow->ipv6_label = htonl(0);
478 }
2486e66a
JP
479 if (wc & FWW_NW_DSCP) {
480 flow->nw_tos &= ~IP_DSCP_MASK;
481 }
482 if (wc & FWW_NW_ECN) {
483 flow->nw_tos &= ~IP_ECN_MASK;
484 }
a61680c6
JP
485 if (wc & FWW_NW_TTL) {
486 flow->nw_ttl = 0;
487 }
eadef313 488 flow->nw_frag &= wildcards->nw_frag_mask;
993410fb
BP
489 if (wc & FWW_ARP_SHA) {
490 memset(flow->arp_sha, 0, sizeof flow->arp_sha);
491 }
492 if (wc & FWW_ARP_THA) {
493 memset(flow->arp_tha, 0, sizeof flow->arp_tha);
494 }
495 flow->ipv6_src = ipv6_addr_bitand(&flow->ipv6_src,
496 &wildcards->ipv6_src_mask);
497 flow->ipv6_dst = ipv6_addr_bitand(&flow->ipv6_dst,
498 &wildcards->ipv6_dst_mask);
499 if (wc & FWW_ND_TARGET) {
500 memset(&flow->nd_target, 0, sizeof flow->nd_target);
501 }
abff858b 502 flow->priority = 0;
993410fb
BP
503}
504
064af421 505char *
ae412e7d 506flow_to_string(const struct flow *flow)
064af421
BP
507{
508 struct ds ds = DS_EMPTY_INITIALIZER;
509 flow_format(&ds, flow);
510 return ds_cstr(&ds);
511}
512
513void
ae412e7d 514flow_format(struct ds *ds, const struct flow *flow)
064af421 515{
abff858b
PS
516 ds_put_format(ds, "priority%"PRIu32
517 ":tunnel%#"PRIx64
518 ":in_port%04"PRIx16,
519 flow->priority,
520 ntohll(flow->tun_id),
521 flow->in_port);
522
523 ds_put_format(ds, ":tci(");
66642cb4
BP
524 if (flow->vlan_tci) {
525 ds_put_format(ds, "vlan%"PRIu16",pcp%d",
526 vlan_tci_to_vid(flow->vlan_tci),
527 vlan_tci_to_pcp(flow->vlan_tci));
528 } else {
529 ds_put_char(ds, '0');
530 }
531 ds_put_format(ds, ") mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT
d31f1109 532 " type%04"PRIx16,
659586ef
JG
533 ETH_ADDR_ARGS(flow->dl_src),
534 ETH_ADDR_ARGS(flow->dl_dst),
d31f1109
JP
535 ntohs(flow->dl_type));
536
537 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
a61680c6
JP
538 ds_put_format(ds, " label%#"PRIx32" proto%"PRIu8" tos%#"PRIx8
539 " ttl%"PRIu8" ipv6",
540 ntohl(flow->ipv6_label), flow->nw_proto,
eadef313 541 flow->nw_tos, flow->nw_ttl);
d31f1109
JP
542 print_ipv6_addr(ds, &flow->ipv6_src);
543 ds_put_cstr(ds, "->");
544 print_ipv6_addr(ds, &flow->ipv6_dst);
b53055f4 545
d31f1109 546 } else {
a61680c6
JP
547 ds_put_format(ds, " proto%"PRIu8" tos%#"PRIx8" ttl%"PRIu8
548 " ip"IP_FMT"->"IP_FMT,
eadef313 549 flow->nw_proto, flow->nw_tos, flow->nw_ttl,
a61680c6 550 IP_ARGS(&flow->nw_src), IP_ARGS(&flow->nw_dst));
d31f1109 551 }
eadef313 552 if (flow->nw_frag) {
7257b535 553 ds_put_format(ds, " frag(%s)",
eadef313
JP
554 flow->nw_frag == FLOW_NW_FRAG_ANY ? "first"
555 : flow->nw_frag == (FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER)
9e44d715 556 ? "later" : "<error>");
7257b535 557 }
bad68a99
JP
558 if (flow->tp_src || flow->tp_dst) {
559 ds_put_format(ds, " port%"PRIu16"->%"PRIu16,
560 ntohs(flow->tp_src), ntohs(flow->tp_dst));
561 }
562 if (!eth_addr_is_zero(flow->arp_sha) || !eth_addr_is_zero(flow->arp_tha)) {
563 ds_put_format(ds, " arp_ha"ETH_ADDR_FMT"->"ETH_ADDR_FMT,
564 ETH_ADDR_ARGS(flow->arp_sha),
565 ETH_ADDR_ARGS(flow->arp_tha));
566 }
064af421
BP
567}
568
569void
ae412e7d 570flow_print(FILE *stream, const struct flow *flow)
064af421
BP
571{
572 char *s = flow_to_string(flow);
573 fputs(s, stream);
574 free(s);
575}
54363004
BP
576\f
577/* flow_wildcards functions. */
578
d8ae4d67 579/* Initializes 'wc' as a set of wildcards that matches every packet. */
54363004 580void
d8ae4d67 581flow_wildcards_init_catchall(struct flow_wildcards *wc)
54363004 582{
2486e66a 583 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
7257b535 584
d8ae4d67 585 wc->wildcards = FWW_ALL;
8368c090 586 wc->tun_id_mask = htonll(0);
d8ae4d67
BP
587 wc->nw_src_mask = htonl(0);
588 wc->nw_dst_mask = htonl(0);
d31f1109
JP
589 wc->ipv6_src_mask = in6addr_any;
590 wc->ipv6_dst_mask = in6addr_any;
b6c9e612 591 memset(wc->reg_masks, 0, sizeof wc->reg_masks);
66642cb4 592 wc->vlan_tci_mask = htons(0);
eadef313 593 wc->nw_frag_mask = 0;
dc56021d 594 memset(wc->zeros, 0, sizeof wc->zeros);
54363004
BP
595}
596
494e43a5
BP
597/* Initializes 'wc' as an exact-match set of wildcards; that is, 'wc' does not
598 * wildcard any bits or fields. */
599void
600flow_wildcards_init_exact(struct flow_wildcards *wc)
601{
2486e66a 602 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
7257b535 603
b6c9e612 604 wc->wildcards = 0;
8368c090 605 wc->tun_id_mask = htonll(UINT64_MAX);
b6c9e612
BP
606 wc->nw_src_mask = htonl(UINT32_MAX);
607 wc->nw_dst_mask = htonl(UINT32_MAX);
d31f1109
JP
608 wc->ipv6_src_mask = in6addr_exact;
609 wc->ipv6_dst_mask = in6addr_exact;
b6c9e612 610 memset(wc->reg_masks, 0xff, sizeof wc->reg_masks);
66642cb4 611 wc->vlan_tci_mask = htons(UINT16_MAX);
eadef313 612 wc->nw_frag_mask = UINT8_MAX;
dc56021d 613 memset(wc->zeros, 0, sizeof wc->zeros);
494e43a5
BP
614}
615
00561f41
BP
616/* Returns true if 'wc' is exact-match, false if 'wc' wildcards any bits or
617 * fields. */
618bool
619flow_wildcards_is_exact(const struct flow_wildcards *wc)
620{
d8ae4d67 621 int i;
00561f41 622
2486e66a 623 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
ecf1e7ac 624
d8ae4d67 625 if (wc->wildcards
8368c090 626 || wc->tun_id_mask != htonll(UINT64_MAX)
d8ae4d67 627 || wc->nw_src_mask != htonl(UINT32_MAX)
66642cb4 628 || wc->nw_dst_mask != htonl(UINT32_MAX)
d31f1109
JP
629 || wc->vlan_tci_mask != htons(UINT16_MAX)
630 || !ipv6_mask_is_exact(&wc->ipv6_src_mask)
7257b535 631 || !ipv6_mask_is_exact(&wc->ipv6_dst_mask)
eadef313 632 || wc->nw_frag_mask != UINT8_MAX) {
d8ae4d67
BP
633 return false;
634 }
635
636 for (i = 0; i < FLOW_N_REGS; i++) {
d84d4b88 637 if (wc->reg_masks[i] != UINT32_MAX) {
d8ae4d67
BP
638 return false;
639 }
640 }
641
642 return true;
b5d97350
BP
643}
644
ecf1e7ac
BP
645/* Returns true if 'wc' matches every packet, false if 'wc' fixes any bits or
646 * fields. */
647bool
648flow_wildcards_is_catchall(const struct flow_wildcards *wc)
649{
650 int i;
651
2486e66a 652 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
ecf1e7ac
BP
653
654 if (wc->wildcards != FWW_ALL
655 || wc->tun_id_mask != htonll(0)
656 || wc->nw_src_mask != htonl(0)
657 || wc->nw_dst_mask != htonl(0)
658 || wc->vlan_tci_mask != htons(0)
659 || !ipv6_mask_is_any(&wc->ipv6_src_mask)
7257b535 660 || !ipv6_mask_is_any(&wc->ipv6_dst_mask)
eadef313 661 || wc->nw_frag_mask != 0) {
ecf1e7ac
BP
662 return false;
663 }
664
665 for (i = 0; i < FLOW_N_REGS; i++) {
666 if (wc->reg_masks[i] != 0) {
667 return false;
668 }
669 }
670
671 return true;
672}
673
b5d97350
BP
674/* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'.
675 * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in
676 * 'src1' or 'src2' or both. */
677void
678flow_wildcards_combine(struct flow_wildcards *dst,
679 const struct flow_wildcards *src1,
680 const struct flow_wildcards *src2)
681{
b6c9e612 682 int i;
b5d97350 683
d8ae4d67 684 dst->wildcards = src1->wildcards | src2->wildcards;
8368c090 685 dst->tun_id_mask = src1->tun_id_mask & src2->tun_id_mask;
b5d97350
BP
686 dst->nw_src_mask = src1->nw_src_mask & src2->nw_src_mask;
687 dst->nw_dst_mask = src1->nw_dst_mask & src2->nw_dst_mask;
d31f1109
JP
688 dst->ipv6_src_mask = ipv6_addr_bitand(&src1->ipv6_src_mask,
689 &src2->ipv6_src_mask);
690 dst->ipv6_dst_mask = ipv6_addr_bitand(&src1->ipv6_dst_mask,
691 &src2->ipv6_dst_mask);
b6c9e612
BP
692 for (i = 0; i < FLOW_N_REGS; i++) {
693 dst->reg_masks[i] = src1->reg_masks[i] & src2->reg_masks[i];
694 }
66642cb4 695 dst->vlan_tci_mask = src1->vlan_tci_mask & src2->vlan_tci_mask;
b5d97350
BP
696}
697
698/* Returns a hash of the wildcards in 'wc'. */
699uint32_t
1006cda6 700flow_wildcards_hash(const struct flow_wildcards *wc, uint32_t basis)
b5d97350 701{
d8ae4d67
BP
702 /* If you change struct flow_wildcards and thereby trigger this
703 * assertion, please check that the new struct flow_wildcards has no holes
704 * in it before you update the assertion. */
dc56021d 705 BUILD_ASSERT_DECL(sizeof *wc == 60 + FLOW_N_REGS * 4);
1006cda6 706 return hash_bytes(wc, sizeof *wc, basis);
b5d97350
BP
707}
708
709/* Returns true if 'a' and 'b' represent the same wildcards, false if they are
710 * different. */
711bool
712flow_wildcards_equal(const struct flow_wildcards *a,
713 const struct flow_wildcards *b)
714{
b6c9e612
BP
715 int i;
716
d8ae4d67 717 if (a->wildcards != b->wildcards
8368c090 718 || a->tun_id_mask != b->tun_id_mask
d8ae4d67 719 || a->nw_src_mask != b->nw_src_mask
66642cb4 720 || a->nw_dst_mask != b->nw_dst_mask
b53055f4 721 || a->vlan_tci_mask != b->vlan_tci_mask
d31f1109
JP
722 || !ipv6_addr_equals(&a->ipv6_src_mask, &b->ipv6_src_mask)
723 || !ipv6_addr_equals(&a->ipv6_dst_mask, &b->ipv6_dst_mask)) {
b6c9e612
BP
724 return false;
725 }
726
727 for (i = 0; i < FLOW_N_REGS; i++) {
728 if (a->reg_masks[i] != b->reg_masks[i]) {
729 return false;
730 }
731 }
732
733 return true;
b5d97350
BP
734}
735
736/* Returns true if at least one bit or field is wildcarded in 'a' but not in
737 * 'b', false otherwise. */
738bool
739flow_wildcards_has_extra(const struct flow_wildcards *a,
740 const struct flow_wildcards *b)
741{
b6c9e612 742 int i;
d31f1109 743 struct in6_addr ipv6_masked;
b6c9e612
BP
744
745 for (i = 0; i < FLOW_N_REGS; i++) {
746 if ((a->reg_masks[i] & b->reg_masks[i]) != b->reg_masks[i]) {
747 return true;
748 }
749 }
750
d31f1109
JP
751 ipv6_masked = ipv6_addr_bitand(&a->ipv6_src_mask, &b->ipv6_src_mask);
752 if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_src_mask)) {
753 return true;
754 }
755
756 ipv6_masked = ipv6_addr_bitand(&a->ipv6_dst_mask, &b->ipv6_dst_mask);
757 if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_dst_mask)) {
758 return true;
759 }
760
d8ae4d67 761 return (a->wildcards & ~b->wildcards
8368c090 762 || (a->tun_id_mask & b->tun_id_mask) != b->tun_id_mask
b5d97350 763 || (a->nw_src_mask & b->nw_src_mask) != b->nw_src_mask
66642cb4
BP
764 || (a->nw_dst_mask & b->nw_dst_mask) != b->nw_dst_mask
765 || (a->vlan_tci_mask & b->vlan_tci_mask) != b->vlan_tci_mask);
b5d97350
BP
766}
767
b6c9e612
BP
768/* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
769 * (A 0-bit indicates a wildcard bit.) */
770void
771flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
772{
d8ae4d67 773 wc->reg_masks[idx] = mask;
b6c9e612 774}
ff55ea1f 775
db7f8281
BP
776/* Returns the wildcard bitmask for the Ethernet destination address
777 * that 'wc' specifies. The bitmask has a 0 in each bit that is wildcarded
778 * and a 1 in each bit that must match. */
779const uint8_t *
780flow_wildcards_to_dl_dst_mask(flow_wildcards_t wc)
781{
782 static const uint8_t no_wild[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
783 static const uint8_t addr_wild[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
784 static const uint8_t mcast_wild[] = {0xfe, 0xff, 0xff, 0xff, 0xff, 0xff};
785 static const uint8_t all_wild[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
786
787 switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
788 case 0: return no_wild;
789 case FWW_DL_DST: return addr_wild;
790 case FWW_ETH_MCAST: return mcast_wild;
791 case FWW_DL_DST | FWW_ETH_MCAST: return all_wild;
792 }
793 NOT_REACHED();
794}
795
796/* Returns true if 'mask' is a valid wildcard bitmask for the Ethernet
797 * destination address. Valid bitmasks are either all-bits-0 or all-bits-1,
798 * except that the multicast bit may differ from the rest of the bits. So,
799 * there are four possible valid bitmasks:
800 *
801 * - 00:00:00:00:00:00
802 * - 01:00:00:00:00:00
803 * - fe:ff:ff:ff:ff:ff
804 * - ff:ff:ff:ff:ff:ff
805 *
806 * All other bitmasks are invalid. */
807bool
808flow_wildcards_is_dl_dst_mask_valid(const uint8_t mask[ETH_ADDR_LEN])
809{
810 switch (mask[0]) {
811 case 0x00:
812 case 0x01:
813 return (mask[1] | mask[2] | mask[3] | mask[4] | mask[5]) == 0x00;
814
815 case 0xfe:
816 case 0xff:
817 return (mask[1] & mask[2] & mask[3] & mask[4] & mask[5]) == 0xff;
818
819 default:
820 return false;
821 }
822}
823
824/* Returns 'wc' with the FWW_DL_DST and FWW_ETH_MCAST bits modified
825 * appropriately to match 'mask'.
826 *
827 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
828 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
829flow_wildcards_t
830flow_wildcards_set_dl_dst_mask(flow_wildcards_t wc,
831 const uint8_t mask[ETH_ADDR_LEN])
832{
833 assert(flow_wildcards_is_dl_dst_mask_valid(mask));
834
835 switch (mask[0]) {
836 case 0x00:
837 return wc | FWW_DL_DST | FWW_ETH_MCAST;
838
839 case 0x01:
840 return (wc | FWW_DL_DST) & ~FWW_ETH_MCAST;
841
842 case 0xfe:
843 return (wc & ~FWW_DL_DST) | FWW_ETH_MCAST;
844
845 case 0xff:
846 return wc & ~(FWW_DL_DST | FWW_ETH_MCAST);
847
848 default:
849 NOT_REACHED();
850 }
851}
852
ff55ea1f
EJ
853/* Hashes 'flow' based on its L2 through L4 protocol information. */
854uint32_t
855flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis)
856{
857 struct {
d31f1109
JP
858 union {
859 ovs_be32 ipv4_addr;
860 struct in6_addr ipv6_addr;
861 };
ff55ea1f
EJ
862 ovs_be16 eth_type;
863 ovs_be16 vlan_tci;
864 ovs_be16 tp_addr;
865 uint8_t eth_addr[ETH_ADDR_LEN];
866 uint8_t ip_proto;
867 } fields;
868
869 int i;
870
871 memset(&fields, 0, sizeof fields);
872 for (i = 0; i < ETH_ADDR_LEN; i++) {
873 fields.eth_addr[i] = flow->dl_src[i] ^ flow->dl_dst[i];
874 }
875 fields.vlan_tci = flow->vlan_tci & htons(VLAN_VID_MASK);
876 fields.eth_type = flow->dl_type;
3e3eda95
EJ
877
878 /* UDP source and destination port are not taken into account because they
879 * will not necessarily be symmetric in a bidirectional flow. */
ff55ea1f 880 if (fields.eth_type == htons(ETH_TYPE_IP)) {
d31f1109
JP
881 fields.ipv4_addr = flow->nw_src ^ flow->nw_dst;
882 fields.ip_proto = flow->nw_proto;
3e3eda95 883 if (fields.ip_proto == IPPROTO_TCP) {
d31f1109
JP
884 fields.tp_addr = flow->tp_src ^ flow->tp_dst;
885 }
886 } else if (fields.eth_type == htons(ETH_TYPE_IPV6)) {
887 const uint8_t *a = &flow->ipv6_src.s6_addr[0];
888 const uint8_t *b = &flow->ipv6_dst.s6_addr[0];
889 uint8_t *ipv6_addr = &fields.ipv6_addr.s6_addr[0];
890
891 for (i=0; i<16; i++) {
892 ipv6_addr[i] = a[i] ^ b[i];
893 }
ff55ea1f 894 fields.ip_proto = flow->nw_proto;
3e3eda95 895 if (fields.ip_proto == IPPROTO_TCP) {
ff55ea1f 896 fields.tp_addr = flow->tp_src ^ flow->tp_dst;
ff55ea1f 897 }
ff55ea1f
EJ
898 }
899 return hash_bytes(&fields, sizeof fields, basis);
900}
520e9a2a
EJ
901
902/* Hashes the portions of 'flow' designated by 'fields'. */
903uint32_t
904flow_hash_fields(const struct flow *flow, enum nx_hash_fields fields,
905 uint16_t basis)
906{
907 switch (fields) {
908
909 case NX_HASH_FIELDS_ETH_SRC:
910 return hash_bytes(flow->dl_src, sizeof flow->dl_src, basis);
911
912 case NX_HASH_FIELDS_SYMMETRIC_L4:
913 return flow_hash_symmetric_l4(flow, basis);
914 }
915
916 NOT_REACHED();
917}
918
919/* Returns a string representation of 'fields'. */
920const char *
921flow_hash_fields_to_str(enum nx_hash_fields fields)
922{
923 switch (fields) {
924 case NX_HASH_FIELDS_ETH_SRC: return "eth_src";
925 case NX_HASH_FIELDS_SYMMETRIC_L4: return "symmetric_l4";
926 default: return "<unknown>";
927 }
928}
929
930/* Returns true if the value of 'fields' is supported. Otherwise false. */
931bool
932flow_hash_fields_valid(enum nx_hash_fields fields)
933{
934 return fields == NX_HASH_FIELDS_ETH_SRC
935 || fields == NX_HASH_FIELDS_SYMMETRIC_L4;
936}
8b3b8dd1
BP
937
938/* Puts into 'b' a packet that flow_extract() would parse as having the given
939 * 'flow'.
940 *
941 * (This is useful only for testing, obviously, and the packet isn't really
942 * valid. It hasn't got any checksums filled in, for one, and lots of fields
943 * are just zeroed.) */
944void
945flow_compose(struct ofpbuf *b, const struct flow *flow)
946{
947 eth_compose(b, flow->dl_dst, flow->dl_src, ntohs(flow->dl_type), 0);
948 if (flow->dl_type == htons(FLOW_DL_TYPE_NONE)) {
949 struct eth_header *eth = b->l2;
950 eth->eth_type = htons(b->size);
951 return;
952 }
953
954 if (flow->vlan_tci & htons(VLAN_CFI)) {
955 eth_push_vlan(b, flow->vlan_tci & ~htons(VLAN_CFI));
956 }
957
958 if (flow->dl_type == htons(ETH_TYPE_IP)) {
959 struct ip_header *ip;
960
961 b->l3 = ip = ofpbuf_put_zeros(b, sizeof *ip);
962 ip->ip_ihl_ver = IP_IHL_VER(5, 4);
eadef313 963 ip->ip_tos = flow->nw_tos;
8b3b8dd1
BP
964 ip->ip_proto = flow->nw_proto;
965 ip->ip_src = flow->nw_src;
966 ip->ip_dst = flow->nw_dst;
967
eadef313 968 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
7257b535 969 ip->ip_frag_off |= htons(IP_MORE_FRAGMENTS);
eadef313 970 if (flow->nw_frag & FLOW_NW_FRAG_LATER) {
7257b535
BP
971 ip->ip_frag_off |= htons(100);
972 }
973 }
eadef313
JP
974 if (!(flow->nw_frag & FLOW_NW_FRAG_ANY)
975 || !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7257b535
BP
976 if (flow->nw_proto == IPPROTO_TCP) {
977 struct tcp_header *tcp;
978
979 b->l4 = tcp = ofpbuf_put_zeros(b, sizeof *tcp);
980 tcp->tcp_src = flow->tp_src;
981 tcp->tcp_dst = flow->tp_dst;
982 } else if (flow->nw_proto == IPPROTO_UDP) {
983 struct udp_header *udp;
984
985 b->l4 = udp = ofpbuf_put_zeros(b, sizeof *udp);
986 udp->udp_src = flow->tp_src;
987 udp->udp_dst = flow->tp_dst;
988 } else if (flow->nw_proto == IPPROTO_ICMP) {
989 struct icmp_header *icmp;
990
991 b->l4 = icmp = ofpbuf_put_zeros(b, sizeof *icmp);
992 icmp->icmp_type = ntohs(flow->tp_src);
993 icmp->icmp_code = ntohs(flow->tp_dst);
994 }
8b3b8dd1
BP
995 }
996 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
997 /* XXX */
998 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
999 struct arp_eth_header *arp;
1000
1001 b->l3 = arp = ofpbuf_put_zeros(b, sizeof *arp);
1002 arp->ar_hrd = htons(1);
1003 arp->ar_pro = htons(ETH_TYPE_IP);
1004 arp->ar_hln = ETH_ADDR_LEN;
1005 arp->ar_pln = 4;
1006 arp->ar_op = htons(flow->nw_proto);
1007
1008 if (flow->nw_proto == ARP_OP_REQUEST ||
1009 flow->nw_proto == ARP_OP_REPLY) {
1010 arp->ar_spa = flow->nw_src;
1011 arp->ar_tpa = flow->nw_dst;
1012 memcpy(arp->ar_sha, flow->arp_sha, ETH_ADDR_LEN);
1013 memcpy(arp->ar_tha, flow->arp_tha, ETH_ADDR_LEN);
1014 }
1015 }
1016}