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