datapath: Allow jumbograms through IPv6 parsing.
[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 <errno.h>
20 #include <inttypes.h>
21 #include <netinet/in.h>
22 #include <netinet/icmp6.h>
23 #include <netinet/ip6.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "byte-order.h"
27 #include "coverage.h"
28 #include "dpif.h"
29 #include "dynamic-string.h"
30 #include "hash.h"
31 #include "ofpbuf.h"
32 #include "openflow/openflow.h"
33 #include "openvswitch/datapath-protocol.h"
34 #include "packets.h"
35 #include "unaligned.h"
36 #include "vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(flow);
39
40 COVERAGE_DEFINE(flow_extract);
41
42 static struct arp_eth_header *
43 pull_arp(struct ofpbuf *packet)
44 {
45     return ofpbuf_try_pull(packet, ARP_ETH_HEADER_LEN);
46 }
47
48 static struct ip_header *
49 pull_ip(struct ofpbuf *packet)
50 {
51     if (packet->size >= IP_HEADER_LEN) {
52         struct ip_header *ip = packet->data;
53         int ip_len = IP_IHL(ip->ip_ihl_ver) * 4;
54         if (ip_len >= IP_HEADER_LEN && packet->size >= ip_len) {
55             return ofpbuf_pull(packet, ip_len);
56         }
57     }
58     return NULL;
59 }
60
61 static struct tcp_header *
62 pull_tcp(struct ofpbuf *packet)
63 {
64     if (packet->size >= TCP_HEADER_LEN) {
65         struct tcp_header *tcp = packet->data;
66         int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
67         if (tcp_len >= TCP_HEADER_LEN && packet->size >= tcp_len) {
68             return ofpbuf_pull(packet, tcp_len);
69         }
70     }
71     return NULL;
72 }
73
74 static struct udp_header *
75 pull_udp(struct ofpbuf *packet)
76 {
77     return ofpbuf_try_pull(packet, UDP_HEADER_LEN);
78 }
79
80 static struct icmp_header *
81 pull_icmp(struct ofpbuf *packet)
82 {
83     return ofpbuf_try_pull(packet, ICMP_HEADER_LEN);
84 }
85
86 static struct icmp6_hdr *
87 pull_icmpv6(struct ofpbuf *packet)
88 {
89     return ofpbuf_try_pull(packet, sizeof(struct icmp6_hdr));
90 }
91
92 static void
93 parse_vlan(struct ofpbuf *b, struct flow *flow)
94 {
95     struct qtag_prefix {
96         ovs_be16 eth_type;      /* ETH_TYPE_VLAN */
97         ovs_be16 tci;
98     };
99
100     if (b->size >= sizeof(struct qtag_prefix) + sizeof(ovs_be16)) {
101         struct qtag_prefix *qp = ofpbuf_pull(b, sizeof *qp);
102         flow->vlan_tci = qp->tci | htons(VLAN_CFI);
103     }
104 }
105
106 static ovs_be16
107 parse_ethertype(struct ofpbuf *b)
108 {
109     struct llc_snap_header *llc;
110     ovs_be16 proto;
111
112     proto = *(ovs_be16 *) ofpbuf_pull(b, sizeof proto);
113     if (ntohs(proto) >= ETH_TYPE_MIN) {
114         return proto;
115     }
116
117     if (b->size < sizeof *llc) {
118         return htons(FLOW_DL_TYPE_NONE);
119     }
120
121     llc = b->data;
122     if (llc->llc.llc_dsap != LLC_DSAP_SNAP
123         || llc->llc.llc_ssap != LLC_SSAP_SNAP
124         || llc->llc.llc_cntl != LLC_CNTL_SNAP
125         || memcmp(llc->snap.snap_org, SNAP_ORG_ETHERNET,
126                   sizeof llc->snap.snap_org)) {
127         return htons(FLOW_DL_TYPE_NONE);
128     }
129
130     ofpbuf_pull(b, sizeof *llc);
131     return llc->snap.snap_type;
132 }
133
134 static int
135 parse_ipv6(struct ofpbuf *packet, struct flow *flow)
136 {
137     struct ip6_hdr *nh;
138     int nh_len = sizeof(struct ip6_hdr);
139     ovs_be32 tc_flow;
140     int nexthdr;
141
142     if (packet->size < sizeof *nh) {
143         return -EINVAL;
144     }
145
146     nh = packet->data;
147     nexthdr = nh->ip6_nxt;
148
149     flow->ipv6_src = nh->ip6_src;
150     flow->ipv6_dst = nh->ip6_dst;
151
152     tc_flow = get_unaligned_be32(&nh->ip6_flow);
153     flow->nw_tos = (ntohl(tc_flow) >> 4) & IP_DSCP_MASK;
154     flow->nw_proto = IPPROTO_NONE;
155
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
171          * bytes. */
172         if (packet->size < nh_len + 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             struct ip6_ext *ext_hdr;
182
183             ext_hdr = (struct ip6_ext *)((char *)packet->data + nh_len);
184             nexthdr = ext_hdr->ip6e_nxt;
185             nh_len += (ext_hdr->ip6e_len + 1) * 8;
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             struct ip6_ext *ext_hdr;
192
193             ext_hdr = (struct ip6_ext *)((char *)packet->data + nh_len);
194             nexthdr = ext_hdr->ip6e_nxt;
195             nh_len += (ext_hdr->ip6e_len + 2) * 4;
196         } else if (nexthdr == IPPROTO_FRAGMENT) {
197             struct ip6_frag *frag_hdr;
198
199             frag_hdr = (struct ip6_frag *)((char *)packet->data + nh_len);
200
201             nexthdr = frag_hdr->ip6f_nxt;
202             nh_len += sizeof *frag_hdr;
203
204             /* We only process the first fragment. */
205             if ((frag_hdr->ip6f_offlg & IP6F_OFF_MASK) != htons(0)) {
206                 nexthdr = IPPROTO_FRAGMENT;
207                 break;
208             }
209         }
210     }
211
212     flow->nw_proto = nexthdr;
213     return nh_len;
214 }
215
216 /* Neighbor Discovery Solicitation and Advertisement messages are
217  * identical in structure, so we'll just use one of them.  To be safe,
218  * we'll assert that they're still identical. */
219 BUILD_ASSERT_DECL(sizeof(struct nd_neighbor_solicit) 
220         == sizeof(struct nd_neighbor_advert));
221
222 static bool
223 parse_icmpv6(struct ofpbuf *b, struct flow *flow, int icmp_len)
224 {
225     const struct icmp6_hdr *icmp = pull_icmpv6(b);
226
227     if (!icmp) {
228         return false;
229     }
230
231     /* The ICMPv6 type and code fields use the 16-bit transport port
232      * fields, so we need to store them in 16-bit network byte order. */
233     flow->icmp_type = htons(icmp->icmp6_type);
234     flow->icmp_code = htons(icmp->icmp6_code);
235
236     if (!icmp->icmp6_code
237             && ((icmp->icmp6_type == ND_NEIGHBOR_SOLICIT)
238              || (icmp->icmp6_type == ND_NEIGHBOR_ADVERT))) {
239         struct nd_neighbor_solicit *nd_ns;  /* Identical to ND advert */
240
241         /* In order to process neighbor discovery options, we need the
242          * entire packet. */
243         if ((icmp_len < sizeof *nd_ns)
244                 || (!ofpbuf_try_pull(b, sizeof *nd_ns - sizeof *icmp))) {
245             return false;
246         }
247         nd_ns = (struct nd_neighbor_solicit *)icmp;
248         flow->nd_target = nd_ns->nd_ns_target;
249
250         icmp_len -= sizeof(*nd_ns);
251         while (icmp_len >= 8) {
252             struct nd_opt_hdr *nd_opt;
253             int opt_len;
254             const uint8_t *data;
255
256             /* The minimum size of an option is 8 bytes, which also is
257              * the size of Ethernet link-layer options. */
258             nd_opt = ofpbuf_pull(b, 8);
259             if (!nd_opt->nd_opt_len || nd_opt->nd_opt_len * 8 > icmp_len) {
260                 goto invalid;
261             }
262             opt_len = nd_opt->nd_opt_len * 8;
263             data = (const uint8_t *)(nd_opt + 1);
264
265             /* Store the link layer address if the appropriate option is
266              * provided.  It is considered an error if the same link
267              * layer option is specified twice. */
268             if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR
269                     && opt_len == 8) {
270                 if (eth_addr_is_zero(flow->arp_sha)) {
271                     memcpy(flow->arp_sha, data, ETH_ADDR_LEN);
272                 } else {
273                     goto invalid;
274                 }
275             } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR
276                     && opt_len == 8) {
277                 if (eth_addr_is_zero(flow->arp_tha)) {
278                     memcpy(flow->arp_tha, data, ETH_ADDR_LEN);
279                 } else {
280                     goto invalid;
281                 }
282             }
283
284             /* Pull the rest of this option. */
285             if (!ofpbuf_try_pull(b, opt_len - 8)) {
286                 goto invalid;
287             }
288
289             icmp_len -= opt_len;
290         }
291     }
292
293     return true;
294
295 invalid:
296     memset(&flow->nd_target, '\0', sizeof(flow->nd_target));
297     memset(flow->arp_sha, '\0', sizeof(flow->arp_sha));
298     memset(flow->arp_tha, '\0', sizeof(flow->arp_tha));
299
300     return false;
301
302 }
303
304 /* Initializes 'flow' members from 'packet', 'tun_id', and 'in_port.
305  * Initializes 'packet' header pointers as follows:
306  *
307  *    - packet->l2 to the start of the Ethernet header.
308  *
309  *    - packet->l3 to just past the Ethernet header, or just past the
310  *      vlan_header if one is present, to the first byte of the payload of the
311  *      Ethernet frame.
312  *
313  *    - packet->l4 to just past the IPv4 header, if one is present and has a
314  *      correct length, and otherwise NULL.
315  *
316  *    - packet->l7 to just past the TCP or UDP or ICMP header, if one is
317  *      present and has a correct length, and otherwise NULL.
318  */
319 int
320 flow_extract(struct ofpbuf *packet, ovs_be64 tun_id, uint16_t in_port,
321              struct flow *flow)
322 {
323     struct ofpbuf b = *packet;
324     struct eth_header *eth;
325     int retval = 0;
326
327     COVERAGE_INC(flow_extract);
328
329     memset(flow, 0, sizeof *flow);
330     flow->tun_id = tun_id;
331     flow->in_port = in_port;
332
333     packet->l2 = b.data;
334     packet->l3 = NULL;
335     packet->l4 = NULL;
336     packet->l7 = NULL;
337
338     if (b.size < sizeof *eth) {
339         return 0;
340     }
341
342     /* Link layer. */
343     eth = b.data;
344     memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
345     memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
346
347     /* dl_type, vlan_tci. */
348     ofpbuf_pull(&b, ETH_ADDR_LEN * 2);
349     if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
350         parse_vlan(&b, flow);
351     }
352     flow->dl_type = parse_ethertype(&b);
353
354     /* Network layer. */
355     packet->l3 = b.data;
356     if (flow->dl_type == htons(ETH_TYPE_IP)) {
357         const struct ip_header *nh = pull_ip(&b);
358         if (nh) {
359             flow->nw_src = get_unaligned_be32(&nh->ip_src);
360             flow->nw_dst = get_unaligned_be32(&nh->ip_dst);
361             flow->nw_tos = nh->ip_tos & IP_DSCP_MASK;
362             flow->nw_proto = nh->ip_proto;
363             packet->l4 = b.data;
364             if (!IP_IS_FRAGMENT(nh->ip_frag_off)) {
365                 if (flow->nw_proto == IPPROTO_TCP) {
366                     const struct tcp_header *tcp = pull_tcp(&b);
367                     if (tcp) {
368                         flow->tp_src = tcp->tcp_src;
369                         flow->tp_dst = tcp->tcp_dst;
370                         packet->l7 = b.data;
371                     }
372                 } else if (flow->nw_proto == IPPROTO_UDP) {
373                     const struct udp_header *udp = pull_udp(&b);
374                     if (udp) {
375                         flow->tp_src = udp->udp_src;
376                         flow->tp_dst = udp->udp_dst;
377                         packet->l7 = b.data;
378                     }
379                 } else if (flow->nw_proto == IPPROTO_ICMP) {
380                     const struct icmp_header *icmp = pull_icmp(&b);
381                     if (icmp) {
382                         flow->icmp_type = htons(icmp->icmp_type);
383                         flow->icmp_code = htons(icmp->icmp_code);
384                         packet->l7 = b.data;
385                     }
386                 }
387             } else {
388                 retval = 1;
389             }
390         }
391     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
392         int nh_len;
393         const struct ip6_hdr *nh;
394
395         nh_len = parse_ipv6(&b, flow);
396         if (nh_len < 0) {
397             return 0;
398         }
399
400         nh = ofpbuf_try_pull(&b, nh_len);
401         if (nh) {
402             packet->l4 = b.data;
403             if (flow->nw_proto == IPPROTO_TCP) {
404                 const struct tcp_header *tcp = pull_tcp(&b);
405                 if (tcp) {
406                     flow->tp_src = tcp->tcp_src;
407                     flow->tp_dst = tcp->tcp_dst;
408                     packet->l7 = b.data;
409                 }
410             } else if (flow->nw_proto == IPPROTO_UDP) {
411                 const struct udp_header *udp = pull_udp(&b);
412                 if (udp) {
413                     flow->tp_src = udp->udp_src;
414                     flow->tp_dst = udp->udp_dst;
415                     packet->l7 = b.data;
416                 }
417             } else if (flow->nw_proto == IPPROTO_ICMPV6) {
418                 if (parse_icmpv6(&b, flow, b.size)) {
419                     packet->l7 = b.data;
420                 }
421             }
422         }
423     } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
424         const struct arp_eth_header *arp = pull_arp(&b);
425         if (arp && arp->ar_hrd == htons(1)
426             && arp->ar_pro == htons(ETH_TYPE_IP)
427             && arp->ar_hln == ETH_ADDR_LEN
428             && arp->ar_pln == 4) {
429             /* We only match on the lower 8 bits of the opcode. */
430             if (ntohs(arp->ar_op) <= 0xff) {
431                 flow->nw_proto = ntohs(arp->ar_op);
432             }
433
434             if ((flow->nw_proto == ARP_OP_REQUEST)
435                 || (flow->nw_proto == ARP_OP_REPLY)) {
436                 flow->nw_src = arp->ar_spa;
437                 flow->nw_dst = arp->ar_tpa;
438                 memcpy(flow->arp_sha, arp->ar_sha, ETH_ADDR_LEN);
439                 memcpy(flow->arp_tha, arp->ar_tha, ETH_ADDR_LEN);
440             }
441         }
442     }
443
444     return retval;
445 }
446
447 /* Extracts the flow stats for a packet.  The 'flow' and 'packet'
448  * arguments must have been initialized through a call to flow_extract().
449  */
450 void
451 flow_extract_stats(const struct flow *flow, struct ofpbuf *packet,
452                    struct dpif_flow_stats *stats)
453 {
454     memset(stats, 0, sizeof(*stats));
455
456     if ((flow->dl_type == htons(ETH_TYPE_IP)) && packet->l4) {
457         if ((flow->nw_proto == IPPROTO_TCP) && packet->l7) {
458             struct tcp_header *tcp = packet->l4;
459             stats->tcp_flags = TCP_FLAGS(tcp->tcp_ctl);
460         }
461     }
462
463     stats->n_bytes = packet->size;
464     stats->n_packets = 1;
465 }
466
467 char *
468 flow_to_string(const struct flow *flow)
469 {
470     struct ds ds = DS_EMPTY_INITIALIZER;
471     flow_format(&ds, flow);
472     return ds_cstr(&ds);
473 }
474
475 void
476 flow_format(struct ds *ds, const struct flow *flow)
477 {
478     ds_put_format(ds, "tunnel%#"PRIx64":in_port%04"PRIx16":tci(",
479                   flow->tun_id, flow->in_port);
480     if (flow->vlan_tci) {
481         ds_put_format(ds, "vlan%"PRIu16",pcp%d",
482                       vlan_tci_to_vid(flow->vlan_tci),
483                       vlan_tci_to_pcp(flow->vlan_tci));
484     } else {
485         ds_put_char(ds, '0');
486     }
487     ds_put_format(ds, ") mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT
488                       " type%04"PRIx16,
489                   ETH_ADDR_ARGS(flow->dl_src),
490                   ETH_ADDR_ARGS(flow->dl_dst),
491                   ntohs(flow->dl_type));
492
493     if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
494         ds_put_format(ds, " proto%"PRIu8" tos%"PRIu8" ipv6",
495                       flow->nw_proto, flow->nw_tos);
496         print_ipv6_addr(ds, &flow->ipv6_src);
497         ds_put_cstr(ds, "->");
498         print_ipv6_addr(ds, &flow->ipv6_dst);
499        
500     } else {
501         ds_put_format(ds, " proto%"PRIu8
502                           " tos%"PRIu8
503                           " ip"IP_FMT"->"IP_FMT,
504                       flow->nw_proto,
505                       flow->nw_tos,
506                       IP_ARGS(&flow->nw_src),
507                       IP_ARGS(&flow->nw_dst));
508     }
509     if (flow->tp_src || flow->tp_dst) {
510         ds_put_format(ds, " port%"PRIu16"->%"PRIu16,
511                 ntohs(flow->tp_src), ntohs(flow->tp_dst));
512     }
513     if (!eth_addr_is_zero(flow->arp_sha) || !eth_addr_is_zero(flow->arp_tha)) {
514         ds_put_format(ds, " arp_ha"ETH_ADDR_FMT"->"ETH_ADDR_FMT,
515                 ETH_ADDR_ARGS(flow->arp_sha),
516                 ETH_ADDR_ARGS(flow->arp_tha));
517     }
518 }
519
520 void
521 flow_print(FILE *stream, const struct flow *flow)
522 {
523     char *s = flow_to_string(flow);
524     fputs(s, stream);
525     free(s);
526 }
527 \f
528 /* flow_wildcards functions. */
529
530 /* Initializes 'wc' as a set of wildcards that matches every packet. */
531 void
532 flow_wildcards_init_catchall(struct flow_wildcards *wc)
533 {
534     wc->wildcards = FWW_ALL;
535     wc->tun_id_mask = htonll(0);
536     wc->nw_src_mask = htonl(0);
537     wc->nw_dst_mask = htonl(0);
538     wc->ipv6_src_mask = in6addr_any;
539     wc->ipv6_dst_mask = in6addr_any;
540     memset(wc->reg_masks, 0, sizeof wc->reg_masks);
541     wc->vlan_tci_mask = htons(0);
542     wc->zero = 0;
543 }
544
545 /* Initializes 'wc' as an exact-match set of wildcards; that is, 'wc' does not
546  * wildcard any bits or fields. */
547 void
548 flow_wildcards_init_exact(struct flow_wildcards *wc)
549 {
550     wc->wildcards = 0;
551     wc->tun_id_mask = htonll(UINT64_MAX);
552     wc->nw_src_mask = htonl(UINT32_MAX);
553     wc->nw_dst_mask = htonl(UINT32_MAX);
554     wc->ipv6_src_mask = in6addr_exact;
555     wc->ipv6_dst_mask = in6addr_exact;
556     memset(wc->reg_masks, 0xff, sizeof wc->reg_masks);
557     wc->vlan_tci_mask = htons(UINT16_MAX);
558     wc->zero = 0;
559 }
560
561 /* Returns true if 'wc' is exact-match, false if 'wc' wildcards any bits or
562  * fields. */
563 bool
564 flow_wildcards_is_exact(const struct flow_wildcards *wc)
565 {
566     int i;
567
568     if (wc->wildcards
569         || wc->tun_id_mask != htonll(UINT64_MAX)
570         || wc->nw_src_mask != htonl(UINT32_MAX)
571         || wc->nw_dst_mask != htonl(UINT32_MAX)
572         || wc->vlan_tci_mask != htons(UINT16_MAX)
573         || !ipv6_mask_is_exact(&wc->ipv6_src_mask)
574         || !ipv6_mask_is_exact(&wc->ipv6_dst_mask)) {
575         return false;
576     }
577
578     for (i = 0; i < FLOW_N_REGS; i++) {
579         if (wc->reg_masks[i] != htonl(UINT32_MAX)) {
580             return false;
581         }
582     }
583
584     return true;
585 }
586
587 /* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'.
588  * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in
589  * 'src1' or 'src2' or both.  */
590 void
591 flow_wildcards_combine(struct flow_wildcards *dst,
592                        const struct flow_wildcards *src1,
593                        const struct flow_wildcards *src2)
594 {
595     int i;
596
597     dst->wildcards = src1->wildcards | src2->wildcards;
598     dst->tun_id_mask = src1->tun_id_mask & src2->tun_id_mask;
599     dst->nw_src_mask = src1->nw_src_mask & src2->nw_src_mask;
600     dst->nw_dst_mask = src1->nw_dst_mask & src2->nw_dst_mask;
601     dst->ipv6_src_mask = ipv6_addr_bitand(&src1->ipv6_src_mask,
602                                         &src2->ipv6_src_mask);
603     dst->ipv6_dst_mask = ipv6_addr_bitand(&src1->ipv6_dst_mask,
604                                         &src2->ipv6_dst_mask);
605     for (i = 0; i < FLOW_N_REGS; i++) {
606         dst->reg_masks[i] = src1->reg_masks[i] & src2->reg_masks[i];
607     }
608     dst->vlan_tci_mask = src1->vlan_tci_mask & src2->vlan_tci_mask;
609 }
610
611 /* Returns a hash of the wildcards in 'wc'. */
612 uint32_t
613 flow_wildcards_hash(const struct flow_wildcards *wc)
614 {
615     /* If you change struct flow_wildcards and thereby trigger this
616      * assertion, please check that the new struct flow_wildcards has no holes
617      * in it before you update the assertion. */
618     BUILD_ASSERT_DECL(sizeof *wc == 56 + FLOW_N_REGS * 4);
619     return hash_bytes(wc, sizeof *wc, 0);
620 }
621
622 /* Returns true if 'a' and 'b' represent the same wildcards, false if they are
623  * different. */
624 bool
625 flow_wildcards_equal(const struct flow_wildcards *a,
626                      const struct flow_wildcards *b)
627 {
628     int i;
629
630     if (a->wildcards != b->wildcards
631         || a->tun_id_mask != b->tun_id_mask
632         || a->nw_src_mask != b->nw_src_mask
633         || a->nw_dst_mask != b->nw_dst_mask
634         || a->vlan_tci_mask != b->vlan_tci_mask 
635         || !ipv6_addr_equals(&a->ipv6_src_mask, &b->ipv6_src_mask)
636         || !ipv6_addr_equals(&a->ipv6_dst_mask, &b->ipv6_dst_mask)) {
637         return false;
638     }
639
640     for (i = 0; i < FLOW_N_REGS; i++) {
641         if (a->reg_masks[i] != b->reg_masks[i]) {
642             return false;
643         }
644     }
645
646     return true;
647 }
648
649 /* Returns true if at least one bit or field is wildcarded in 'a' but not in
650  * 'b', false otherwise. */
651 bool
652 flow_wildcards_has_extra(const struct flow_wildcards *a,
653                          const struct flow_wildcards *b)
654 {
655     int i;
656     struct in6_addr ipv6_masked;
657
658     for (i = 0; i < FLOW_N_REGS; i++) {
659         if ((a->reg_masks[i] & b->reg_masks[i]) != b->reg_masks[i]) {
660             return true;
661         }
662     }
663
664     ipv6_masked = ipv6_addr_bitand(&a->ipv6_src_mask, &b->ipv6_src_mask);
665     if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_src_mask)) {
666         return true;
667     }
668
669     ipv6_masked = ipv6_addr_bitand(&a->ipv6_dst_mask, &b->ipv6_dst_mask);
670     if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_dst_mask)) {
671         return true;
672     }
673
674     return (a->wildcards & ~b->wildcards
675             || (a->tun_id_mask & b->tun_id_mask) != b->tun_id_mask
676             || (a->nw_src_mask & b->nw_src_mask) != b->nw_src_mask
677             || (a->nw_dst_mask & b->nw_dst_mask) != b->nw_dst_mask
678             || (a->vlan_tci_mask & b->vlan_tci_mask) != b->vlan_tci_mask);
679 }
680
681 static bool
682 set_nw_mask(ovs_be32 *maskp, ovs_be32 mask)
683 {
684     if (ip_is_cidr(mask)) {
685         *maskp = mask;
686         return true;
687     } else {
688         return false;
689     }
690 }
691
692 /* Sets the IP (or ARP) source wildcard mask to CIDR 'mask' (consisting of N
693  * high-order 1-bit and 32-N low-order 0-bits).  Returns true if successful,
694  * false if 'mask' is not a CIDR mask.  */
695 bool
696 flow_wildcards_set_nw_src_mask(struct flow_wildcards *wc, ovs_be32 mask)
697 {
698     return set_nw_mask(&wc->nw_src_mask, mask);
699 }
700
701 /* Sets the IP (or ARP) destination wildcard mask to CIDR 'mask' (consisting of
702  * N high-order 1-bit and 32-N low-order 0-bits).  Returns true if successful,
703  * false if 'mask' is not a CIDR mask.  */
704 bool
705 flow_wildcards_set_nw_dst_mask(struct flow_wildcards *wc, ovs_be32 mask)
706 {
707     return set_nw_mask(&wc->nw_dst_mask, mask);
708 }
709
710 static bool
711 set_ipv6_mask(struct in6_addr *maskp, const struct in6_addr *mask)
712 {
713     if (ipv6_is_cidr(mask)) {
714         *maskp = *mask;
715         return true;
716     } else {
717         return false;
718     }
719 }
720
721 /* Sets the IPv6 source wildcard mask to CIDR 'mask' (consisting of N
722  * high-order 1-bit and 128-N low-order 0-bits).  Returns true if successful,
723  * false if 'mask' is not a CIDR mask.  */
724 bool
725 flow_wildcards_set_ipv6_src_mask(struct flow_wildcards *wc,
726                                  const struct in6_addr *mask)
727 {
728     return set_ipv6_mask(&wc->ipv6_src_mask, mask);
729 }
730
731 /* Sets the IPv6 destination wildcard mask to CIDR 'mask' (consisting of
732  * N high-order 1-bit and 128-N low-order 0-bits).  Returns true if
733  * successful, false if 'mask' is not a CIDR mask.  */
734 bool
735 flow_wildcards_set_ipv6_dst_mask(struct flow_wildcards *wc,
736                                  const struct in6_addr *mask)
737 {
738     return set_ipv6_mask(&wc->ipv6_dst_mask, mask);
739 }
740
741 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
742  * (A 0-bit indicates a wildcard bit.) */
743 void
744 flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
745 {
746     wc->reg_masks[idx] = mask;
747 }
748
749 /* Hashes 'flow' based on its L2 through L4 protocol information. */
750 uint32_t
751 flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis)
752 {
753     struct {
754         union {
755             ovs_be32 ipv4_addr;
756             struct in6_addr ipv6_addr;
757         };
758         ovs_be16 eth_type;
759         ovs_be16 vlan_tci;
760         ovs_be16 tp_addr;
761         uint8_t eth_addr[ETH_ADDR_LEN];
762         uint8_t ip_proto;
763     } fields;
764
765     int i;
766
767     memset(&fields, 0, sizeof fields);
768     for (i = 0; i < ETH_ADDR_LEN; i++) {
769         fields.eth_addr[i] = flow->dl_src[i] ^ flow->dl_dst[i];
770     }
771     fields.vlan_tci = flow->vlan_tci & htons(VLAN_VID_MASK);
772     fields.eth_type = flow->dl_type;
773     if (fields.eth_type == htons(ETH_TYPE_IP)) {
774         fields.ipv4_addr = flow->nw_src ^ flow->nw_dst;
775         fields.ip_proto = flow->nw_proto;
776         if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_UDP) {
777             fields.tp_addr = flow->tp_src ^ flow->tp_dst;
778         }
779     } else if (fields.eth_type == htons(ETH_TYPE_IPV6)) {
780         const uint8_t *a = &flow->ipv6_src.s6_addr[0];
781         const uint8_t *b = &flow->ipv6_dst.s6_addr[0];
782         uint8_t *ipv6_addr = &fields.ipv6_addr.s6_addr[0];
783
784         for (i=0; i<16; i++) {
785             ipv6_addr[i] = a[i] ^ b[i];
786         }
787         fields.ip_proto = flow->nw_proto;
788         if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_UDP) {
789             fields.tp_addr = flow->tp_src ^ flow->tp_dst;
790         }
791     }
792     return hash_bytes(&fields, sizeof fields, basis);
793 }