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