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