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