nicira-ext: Support matching IPv6 traffic.
[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     int payload_len;
140     ovs_be32 tc_flow;
141     int nexthdr;
142
143     if (packet->size < sizeof *nh) {
144         return -EINVAL;
145     }
146
147     nh = packet->data;
148     nexthdr = nh->ip6_nxt;
149     payload_len = ntohs(nh->ip6_plen);
150
151     flow->ipv6_src = nh->ip6_src;
152     flow->ipv6_dst = nh->ip6_dst;
153
154     tc_flow = get_unaligned_be32(&nh->ip6_flow);
155     flow->nw_tos = (ntohl(tc_flow) >> 4) & IP_DSCP_MASK;
156     flow->nw_proto = IPPROTO_NONE;
157
158     /* We don't process jumbograms. */
159     if (!payload_len) {
160         return -EINVAL;
161     }
162
163     if (packet->size < sizeof *nh + payload_len) {
164         return -EINVAL;
165     }
166
167     while (1) {
168         if ((nexthdr != IPPROTO_HOPOPTS)
169                 && (nexthdr != IPPROTO_ROUTING)
170                 && (nexthdr != IPPROTO_DSTOPTS)
171                 && (nexthdr != IPPROTO_AH)
172                 && (nexthdr != IPPROTO_FRAGMENT)) {
173             /* It's either a terminal header (e.g., TCP, UDP) or one we
174              * don't understand.  In either case, we're done with the
175              * packet, so use it to fill in 'nw_proto'. */
176             break;
177         }
178
179         /* We only verify that at least 8 bytes of the next header are
180          * available, but many of these headers are longer.  Ensure that
181          * accesses within the extension header are within those first 8
182          * bytes. */
183         if (packet->size < nh_len + 8) {
184             return -EINVAL;
185         }
186
187         if ((nexthdr == IPPROTO_HOPOPTS)
188                 || (nexthdr == IPPROTO_ROUTING)
189                 || (nexthdr == IPPROTO_DSTOPTS)) {
190             /* These headers, while different, have the fields we care about
191              * in the same location and with the same interpretation. */
192             struct ip6_ext *ext_hdr;
193
194             ext_hdr = (struct ip6_ext *)((char *)packet->data + nh_len);
195             nexthdr = ext_hdr->ip6e_nxt;
196             nh_len += (ext_hdr->ip6e_len + 1) * 8;
197         } else if (nexthdr == IPPROTO_AH) {
198             /* A standard AH definition isn't available, but the fields
199              * we care about are in the same location as the generic
200              * option header--only the header length is calculated
201              * differently. */
202             struct ip6_ext *ext_hdr;
203
204             ext_hdr = (struct ip6_ext *)((char *)packet->data + nh_len);
205             nexthdr = ext_hdr->ip6e_nxt;
206             nh_len += (ext_hdr->ip6e_len + 2) * 4;
207         } else if (nexthdr == IPPROTO_FRAGMENT) {
208             struct ip6_frag *frag_hdr;
209
210             frag_hdr = (struct ip6_frag *)((char *)packet->data + nh_len);
211
212             nexthdr = frag_hdr->ip6f_nxt;
213             nh_len += sizeof *frag_hdr;
214
215             /* We only process the first fragment. */
216             if ((frag_hdr->ip6f_offlg & IP6F_OFF_MASK) != htons(0)) {
217                 nexthdr = IPPROTO_FRAGMENT;
218                 break;
219             }
220         }
221     }
222
223     /* The payload length claims to be smaller than the size of the
224      * headers we've already processed. */
225     if (payload_len < nh_len - sizeof *nh) {
226         return -EINVAL;
227     }
228
229     flow->nw_proto = nexthdr;
230     return nh_len;
231 }
232
233 /* Initializes 'flow' members from 'packet', 'tun_id', and 'in_port.
234  * Initializes 'packet' header pointers as follows:
235  *
236  *    - packet->l2 to the start of the Ethernet header.
237  *
238  *    - packet->l3 to just past the Ethernet header, or just past the
239  *      vlan_header if one is present, to the first byte of the payload of the
240  *      Ethernet frame.
241  *
242  *    - packet->l4 to just past the IPv4 header, if one is present and has a
243  *      correct length, and otherwise NULL.
244  *
245  *    - packet->l7 to just past the TCP or UDP or ICMP header, if one is
246  *      present and has a correct length, and otherwise NULL.
247  */
248 int
249 flow_extract(struct ofpbuf *packet, ovs_be64 tun_id, uint16_t in_port,
250              struct flow *flow)
251 {
252     struct ofpbuf b = *packet;
253     struct eth_header *eth;
254     int retval = 0;
255
256     COVERAGE_INC(flow_extract);
257
258     memset(flow, 0, sizeof *flow);
259     flow->tun_id = tun_id;
260     flow->in_port = in_port;
261
262     packet->l2 = b.data;
263     packet->l3 = NULL;
264     packet->l4 = NULL;
265     packet->l7 = NULL;
266
267     if (b.size < sizeof *eth) {
268         return 0;
269     }
270
271     /* Link layer. */
272     eth = b.data;
273     memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
274     memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
275
276     /* dl_type, vlan_tci. */
277     ofpbuf_pull(&b, ETH_ADDR_LEN * 2);
278     if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
279         parse_vlan(&b, flow);
280     }
281     flow->dl_type = parse_ethertype(&b);
282
283     /* Network layer. */
284     packet->l3 = b.data;
285     if (flow->dl_type == htons(ETH_TYPE_IP)) {
286         const struct ip_header *nh = pull_ip(&b);
287         if (nh) {
288             flow->nw_src = get_unaligned_be32(&nh->ip_src);
289             flow->nw_dst = get_unaligned_be32(&nh->ip_dst);
290             flow->nw_tos = nh->ip_tos & IP_DSCP_MASK;
291             flow->nw_proto = nh->ip_proto;
292             packet->l4 = b.data;
293             if (!IP_IS_FRAGMENT(nh->ip_frag_off)) {
294                 if (flow->nw_proto == IPPROTO_TCP) {
295                     const struct tcp_header *tcp = pull_tcp(&b);
296                     if (tcp) {
297                         flow->tp_src = tcp->tcp_src;
298                         flow->tp_dst = tcp->tcp_dst;
299                         packet->l7 = b.data;
300                     }
301                 } else if (flow->nw_proto == IPPROTO_UDP) {
302                     const struct udp_header *udp = pull_udp(&b);
303                     if (udp) {
304                         flow->tp_src = udp->udp_src;
305                         flow->tp_dst = udp->udp_dst;
306                         packet->l7 = b.data;
307                     }
308                 } else if (flow->nw_proto == IPPROTO_ICMP) {
309                     const struct icmp_header *icmp = pull_icmp(&b);
310                     if (icmp) {
311                         flow->icmp_type = htons(icmp->icmp_type);
312                         flow->icmp_code = htons(icmp->icmp_code);
313                         packet->l7 = b.data;
314                     }
315                 }
316             } else {
317                 retval = 1;
318             }
319         }
320     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
321         int nh_len;
322         const struct ip6_hdr *nh;
323
324         nh_len = parse_ipv6(&b, flow);
325         if (nh_len < 0) {
326             return 0;
327         }
328
329         nh = ofpbuf_pull(&b, nh_len);
330         if (nh) {
331             packet->l4 = b.data;
332             if (flow->nw_proto == IPPROTO_TCP) {
333                 const struct tcp_header *tcp = pull_tcp(&b);
334                 if (tcp) {
335                     flow->tp_src = tcp->tcp_src;
336                     flow->tp_dst = tcp->tcp_dst;
337                     packet->l7 = b.data;
338                 }
339             } else if (flow->nw_proto == IPPROTO_UDP) {
340                 const struct udp_header *udp = pull_udp(&b);
341                 if (udp) {
342                     flow->tp_src = udp->udp_src;
343                     flow->tp_dst = udp->udp_dst;
344                     packet->l7 = b.data;
345                 }
346             } else if (flow->nw_proto == IPPROTO_ICMPV6) {
347                 const struct icmp6_hdr *icmp = pull_icmpv6(&b);
348                 if (icmp) {
349                     flow->icmp_type = htons(icmp->icmp6_type);
350                     flow->icmp_code = htons(icmp->icmp6_code);
351                     packet->l7 = b.data;
352                 }
353             }
354         }
355     } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
356         const struct arp_eth_header *arp = pull_arp(&b);
357         if (arp && arp->ar_hrd == htons(1)
358             && arp->ar_pro == htons(ETH_TYPE_IP)
359             && arp->ar_hln == ETH_ADDR_LEN
360             && arp->ar_pln == 4) {
361             /* We only match on the lower 8 bits of the opcode. */
362             if (ntohs(arp->ar_op) <= 0xff) {
363                 flow->nw_proto = ntohs(arp->ar_op);
364             }
365
366             if ((flow->nw_proto == ARP_OP_REQUEST)
367                 || (flow->nw_proto == ARP_OP_REPLY)) {
368                 flow->nw_src = arp->ar_spa;
369                 flow->nw_dst = arp->ar_tpa;
370                 memcpy(flow->arp_sha, arp->ar_sha, ETH_ADDR_LEN);
371                 memcpy(flow->arp_tha, arp->ar_tha, ETH_ADDR_LEN);
372             }
373         }
374     }
375
376     return retval;
377 }
378
379 /* Extracts the flow stats for a packet.  The 'flow' and 'packet'
380  * arguments must have been initialized through a call to flow_extract().
381  */
382 void
383 flow_extract_stats(const struct flow *flow, struct ofpbuf *packet,
384                    struct dpif_flow_stats *stats)
385 {
386     memset(stats, 0, sizeof(*stats));
387
388     if ((flow->dl_type == htons(ETH_TYPE_IP)) && packet->l4) {
389         if ((flow->nw_proto == IPPROTO_TCP) && packet->l7) {
390             struct tcp_header *tcp = packet->l4;
391             stats->tcp_flags = TCP_FLAGS(tcp->tcp_ctl);
392         }
393     }
394
395     stats->n_bytes = packet->size;
396     stats->n_packets = 1;
397 }
398
399 char *
400 flow_to_string(const struct flow *flow)
401 {
402     struct ds ds = DS_EMPTY_INITIALIZER;
403     flow_format(&ds, flow);
404     return ds_cstr(&ds);
405 }
406
407 void
408 flow_format(struct ds *ds, const struct flow *flow)
409 {
410     ds_put_format(ds, "tunnel%#"PRIx64":in_port%04"PRIx16":tci(",
411                   flow->tun_id, flow->in_port);
412     if (flow->vlan_tci) {
413         ds_put_format(ds, "vlan%"PRIu16",pcp%d",
414                       vlan_tci_to_vid(flow->vlan_tci),
415                       vlan_tci_to_pcp(flow->vlan_tci));
416     } else {
417         ds_put_char(ds, '0');
418     }
419     ds_put_format(ds, ") mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT
420                       " type%04"PRIx16,
421                   ETH_ADDR_ARGS(flow->dl_src),
422                   ETH_ADDR_ARGS(flow->dl_dst),
423                   ntohs(flow->dl_type));
424
425     if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
426         ds_put_format(ds, " proto%"PRIu8" tos%"PRIu8" ipv6",
427                       flow->nw_proto, flow->nw_tos);
428         print_ipv6_addr(ds, &flow->ipv6_src);
429         ds_put_cstr(ds, "->");
430         print_ipv6_addr(ds, &flow->ipv6_dst);
431        
432     } else {
433         ds_put_format(ds, " proto%"PRIu8
434                           " tos%"PRIu8
435                           " ip"IP_FMT"->"IP_FMT,
436                       flow->nw_proto,
437                       flow->nw_tos,
438                       IP_ARGS(&flow->nw_src),
439                       IP_ARGS(&flow->nw_dst));
440     }
441     if (flow->tp_src || flow->tp_dst) {
442         ds_put_format(ds, " port%"PRIu16"->%"PRIu16,
443                 ntohs(flow->tp_src), ntohs(flow->tp_dst));
444     }
445     if (!eth_addr_is_zero(flow->arp_sha) || !eth_addr_is_zero(flow->arp_tha)) {
446         ds_put_format(ds, " arp_ha"ETH_ADDR_FMT"->"ETH_ADDR_FMT,
447                 ETH_ADDR_ARGS(flow->arp_sha),
448                 ETH_ADDR_ARGS(flow->arp_tha));
449     }
450 }
451
452 void
453 flow_print(FILE *stream, const struct flow *flow)
454 {
455     char *s = flow_to_string(flow);
456     fputs(s, stream);
457     free(s);
458 }
459 \f
460 /* flow_wildcards functions. */
461
462 /* Initializes 'wc' as a set of wildcards that matches every packet. */
463 void
464 flow_wildcards_init_catchall(struct flow_wildcards *wc)
465 {
466     wc->wildcards = FWW_ALL;
467     wc->tun_id_mask = htonll(0);
468     wc->nw_src_mask = htonl(0);
469     wc->nw_dst_mask = htonl(0);
470     wc->ipv6_src_mask = in6addr_any;
471     wc->ipv6_dst_mask = in6addr_any;
472     memset(wc->reg_masks, 0, sizeof wc->reg_masks);
473     wc->vlan_tci_mask = htons(0);
474     wc->zero = 0;
475 }
476
477 /* Initializes 'wc' as an exact-match set of wildcards; that is, 'wc' does not
478  * wildcard any bits or fields. */
479 void
480 flow_wildcards_init_exact(struct flow_wildcards *wc)
481 {
482     wc->wildcards = 0;
483     wc->tun_id_mask = htonll(UINT64_MAX);
484     wc->nw_src_mask = htonl(UINT32_MAX);
485     wc->nw_dst_mask = htonl(UINT32_MAX);
486     wc->ipv6_src_mask = in6addr_exact;
487     wc->ipv6_dst_mask = in6addr_exact;
488     memset(wc->reg_masks, 0xff, sizeof wc->reg_masks);
489     wc->vlan_tci_mask = htons(UINT16_MAX);
490     wc->zero = 0;
491 }
492
493 /* Returns true if 'wc' is exact-match, false if 'wc' wildcards any bits or
494  * fields. */
495 bool
496 flow_wildcards_is_exact(const struct flow_wildcards *wc)
497 {
498     int i;
499
500     if (wc->wildcards
501         || wc->tun_id_mask != htonll(UINT64_MAX)
502         || wc->nw_src_mask != htonl(UINT32_MAX)
503         || wc->nw_dst_mask != htonl(UINT32_MAX)
504         || wc->vlan_tci_mask != htons(UINT16_MAX)
505         || !ipv6_mask_is_exact(&wc->ipv6_src_mask)
506         || !ipv6_mask_is_exact(&wc->ipv6_dst_mask)) {
507         return false;
508     }
509
510     for (i = 0; i < FLOW_N_REGS; i++) {
511         if (wc->reg_masks[i] != htonl(UINT32_MAX)) {
512             return false;
513         }
514     }
515
516     return true;
517 }
518
519 /* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'.
520  * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in
521  * 'src1' or 'src2' or both.  */
522 void
523 flow_wildcards_combine(struct flow_wildcards *dst,
524                        const struct flow_wildcards *src1,
525                        const struct flow_wildcards *src2)
526 {
527     int i;
528
529     dst->wildcards = src1->wildcards | src2->wildcards;
530     dst->tun_id_mask = src1->tun_id_mask & src2->tun_id_mask;
531     dst->nw_src_mask = src1->nw_src_mask & src2->nw_src_mask;
532     dst->nw_dst_mask = src1->nw_dst_mask & src2->nw_dst_mask;
533     dst->ipv6_src_mask = ipv6_addr_bitand(&src1->ipv6_src_mask,
534                                         &src2->ipv6_src_mask);
535     dst->ipv6_dst_mask = ipv6_addr_bitand(&src1->ipv6_dst_mask,
536                                         &src2->ipv6_dst_mask);
537     for (i = 0; i < FLOW_N_REGS; i++) {
538         dst->reg_masks[i] = src1->reg_masks[i] & src2->reg_masks[i];
539     }
540     dst->vlan_tci_mask = src1->vlan_tci_mask & src2->vlan_tci_mask;
541 }
542
543 /* Returns a hash of the wildcards in 'wc'. */
544 uint32_t
545 flow_wildcards_hash(const struct flow_wildcards *wc)
546 {
547     /* If you change struct flow_wildcards and thereby trigger this
548      * assertion, please check that the new struct flow_wildcards has no holes
549      * in it before you update the assertion. */
550     BUILD_ASSERT_DECL(sizeof *wc == 56 + FLOW_N_REGS * 4);
551     return hash_bytes(wc, sizeof *wc, 0);
552 }
553
554 /* Returns true if 'a' and 'b' represent the same wildcards, false if they are
555  * different. */
556 bool
557 flow_wildcards_equal(const struct flow_wildcards *a,
558                      const struct flow_wildcards *b)
559 {
560     int i;
561
562     if (a->wildcards != b->wildcards
563         || a->tun_id_mask != b->tun_id_mask
564         || a->nw_src_mask != b->nw_src_mask
565         || a->nw_dst_mask != b->nw_dst_mask
566         || a->vlan_tci_mask != b->vlan_tci_mask 
567         || !ipv6_addr_equals(&a->ipv6_src_mask, &b->ipv6_src_mask)
568         || !ipv6_addr_equals(&a->ipv6_dst_mask, &b->ipv6_dst_mask)) {
569         return false;
570     }
571
572     for (i = 0; i < FLOW_N_REGS; i++) {
573         if (a->reg_masks[i] != b->reg_masks[i]) {
574             return false;
575         }
576     }
577
578     return true;
579 }
580
581 /* Returns true if at least one bit or field is wildcarded in 'a' but not in
582  * 'b', false otherwise. */
583 bool
584 flow_wildcards_has_extra(const struct flow_wildcards *a,
585                          const struct flow_wildcards *b)
586 {
587     int i;
588     struct in6_addr ipv6_masked;
589
590     for (i = 0; i < FLOW_N_REGS; i++) {
591         if ((a->reg_masks[i] & b->reg_masks[i]) != b->reg_masks[i]) {
592             return true;
593         }
594     }
595
596     ipv6_masked = ipv6_addr_bitand(&a->ipv6_src_mask, &b->ipv6_src_mask);
597     if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_src_mask)) {
598         return true;
599     }
600
601     ipv6_masked = ipv6_addr_bitand(&a->ipv6_dst_mask, &b->ipv6_dst_mask);
602     if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_dst_mask)) {
603         return true;
604     }
605
606     return (a->wildcards & ~b->wildcards
607             || (a->tun_id_mask & b->tun_id_mask) != b->tun_id_mask
608             || (a->nw_src_mask & b->nw_src_mask) != b->nw_src_mask
609             || (a->nw_dst_mask & b->nw_dst_mask) != b->nw_dst_mask
610             || (a->vlan_tci_mask & b->vlan_tci_mask) != b->vlan_tci_mask);
611 }
612
613 static bool
614 set_nw_mask(ovs_be32 *maskp, ovs_be32 mask)
615 {
616     if (ip_is_cidr(mask)) {
617         *maskp = mask;
618         return true;
619     } else {
620         return false;
621     }
622 }
623
624 /* Sets the IP (or ARP) source wildcard mask to CIDR 'mask' (consisting of N
625  * high-order 1-bit and 32-N low-order 0-bits).  Returns true if successful,
626  * false if 'mask' is not a CIDR mask.  */
627 bool
628 flow_wildcards_set_nw_src_mask(struct flow_wildcards *wc, ovs_be32 mask)
629 {
630     return set_nw_mask(&wc->nw_src_mask, mask);
631 }
632
633 /* Sets the IP (or ARP) destination wildcard mask to CIDR 'mask' (consisting of
634  * N high-order 1-bit and 32-N low-order 0-bits).  Returns true if successful,
635  * false if 'mask' is not a CIDR mask.  */
636 bool
637 flow_wildcards_set_nw_dst_mask(struct flow_wildcards *wc, ovs_be32 mask)
638 {
639     return set_nw_mask(&wc->nw_dst_mask, mask);
640 }
641
642 static bool
643 set_ipv6_mask(struct in6_addr *maskp, const struct in6_addr *mask)
644 {
645     if (ipv6_is_cidr(mask)) {
646         *maskp = *mask;
647         return true;
648     } else {
649         return false;
650     }
651 }
652
653 /* Sets the IPv6 source wildcard mask to CIDR 'mask' (consisting of N
654  * high-order 1-bit and 128-N low-order 0-bits).  Returns true if successful,
655  * false if 'mask' is not a CIDR mask.  */
656 bool
657 flow_wildcards_set_ipv6_src_mask(struct flow_wildcards *wc,
658                                  const struct in6_addr *mask)
659 {
660     return set_ipv6_mask(&wc->ipv6_src_mask, mask);
661 }
662
663 /* Sets the IPv6 destination wildcard mask to CIDR 'mask' (consisting of
664  * N high-order 1-bit and 128-N low-order 0-bits).  Returns true if
665  * successful, false if 'mask' is not a CIDR mask.  */
666 bool
667 flow_wildcards_set_ipv6_dst_mask(struct flow_wildcards *wc,
668                                  const struct in6_addr *mask)
669 {
670     return set_ipv6_mask(&wc->ipv6_dst_mask, mask);
671 }
672
673 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
674  * (A 0-bit indicates a wildcard bit.) */
675 void
676 flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
677 {
678     wc->reg_masks[idx] = mask;
679 }
680
681 /* Hashes 'flow' based on its L2 through L4 protocol information. */
682 uint32_t
683 flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis)
684 {
685     struct {
686         union {
687             ovs_be32 ipv4_addr;
688             struct in6_addr ipv6_addr;
689         };
690         ovs_be16 eth_type;
691         ovs_be16 vlan_tci;
692         ovs_be16 tp_addr;
693         uint8_t eth_addr[ETH_ADDR_LEN];
694         uint8_t ip_proto;
695     } fields;
696
697     int i;
698
699     memset(&fields, 0, sizeof fields);
700     for (i = 0; i < ETH_ADDR_LEN; i++) {
701         fields.eth_addr[i] = flow->dl_src[i] ^ flow->dl_dst[i];
702     }
703     fields.vlan_tci = flow->vlan_tci & htons(VLAN_VID_MASK);
704     fields.eth_type = flow->dl_type;
705     if (fields.eth_type == htons(ETH_TYPE_IP)) {
706         fields.ipv4_addr = flow->nw_src ^ flow->nw_dst;
707         fields.ip_proto = flow->nw_proto;
708         if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_UDP) {
709             fields.tp_addr = flow->tp_src ^ flow->tp_dst;
710         }
711     } else if (fields.eth_type == htons(ETH_TYPE_IPV6)) {
712         const uint8_t *a = &flow->ipv6_src.s6_addr[0];
713         const uint8_t *b = &flow->ipv6_dst.s6_addr[0];
714         uint8_t *ipv6_addr = &fields.ipv6_addr.s6_addr[0];
715
716         for (i=0; i<16; i++) {
717             ipv6_addr[i] = a[i] ^ b[i];
718         }
719         fields.ip_proto = flow->nw_proto;
720         if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_UDP) {
721             fields.tp_addr = flow->tp_src ^ flow->tp_dst;
722         }
723     }
724     return hash_bytes(&fields, sizeof fields, basis);
725 }