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