nicira-ext: Support matching ARP source and target hardware addresses.
[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 <inttypes.h>
20 #include <netinet/in.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "byte-order.h"
24 #include "coverage.h"
25 #include "dpif.h"
26 #include "dynamic-string.h"
27 #include "hash.h"
28 #include "ofpbuf.h"
29 #include "openflow/openflow.h"
30 #include "openvswitch/datapath-protocol.h"
31 #include "packets.h"
32 #include "unaligned.h"
33 #include "vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(flow);
36
37 COVERAGE_DEFINE(flow_extract);
38
39 static struct arp_eth_header *
40 pull_arp(struct ofpbuf *packet)
41 {
42     return ofpbuf_try_pull(packet, ARP_ETH_HEADER_LEN);
43 }
44
45 static struct ip_header *
46 pull_ip(struct ofpbuf *packet)
47 {
48     if (packet->size >= IP_HEADER_LEN) {
49         struct ip_header *ip = packet->data;
50         int ip_len = IP_IHL(ip->ip_ihl_ver) * 4;
51         if (ip_len >= IP_HEADER_LEN && packet->size >= ip_len) {
52             return ofpbuf_pull(packet, ip_len);
53         }
54     }
55     return NULL;
56 }
57
58 static struct tcp_header *
59 pull_tcp(struct ofpbuf *packet)
60 {
61     if (packet->size >= TCP_HEADER_LEN) {
62         struct tcp_header *tcp = packet->data;
63         int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
64         if (tcp_len >= TCP_HEADER_LEN && packet->size >= tcp_len) {
65             return ofpbuf_pull(packet, tcp_len);
66         }
67     }
68     return NULL;
69 }
70
71 static struct udp_header *
72 pull_udp(struct ofpbuf *packet)
73 {
74     return ofpbuf_try_pull(packet, UDP_HEADER_LEN);
75 }
76
77 static struct icmp_header *
78 pull_icmp(struct ofpbuf *packet)
79 {
80     return ofpbuf_try_pull(packet, ICMP_HEADER_LEN);
81 }
82
83 static void
84 parse_vlan(struct ofpbuf *b, struct flow *flow)
85 {
86     struct qtag_prefix {
87         ovs_be16 eth_type;      /* ETH_TYPE_VLAN */
88         ovs_be16 tci;
89     };
90
91     if (b->size >= sizeof(struct qtag_prefix) + sizeof(ovs_be16)) {
92         struct qtag_prefix *qp = ofpbuf_pull(b, sizeof *qp);
93         flow->vlan_tci = qp->tci | htons(VLAN_CFI);
94     }
95 }
96
97 static ovs_be16
98 parse_ethertype(struct ofpbuf *b)
99 {
100     struct llc_snap_header *llc;
101     ovs_be16 proto;
102
103     proto = *(ovs_be16 *) ofpbuf_pull(b, sizeof proto);
104     if (ntohs(proto) >= ETH_TYPE_MIN) {
105         return proto;
106     }
107
108     if (b->size < sizeof *llc) {
109         return htons(FLOW_DL_TYPE_NONE);
110     }
111
112     llc = b->data;
113     if (llc->llc.llc_dsap != LLC_DSAP_SNAP
114         || llc->llc.llc_ssap != LLC_SSAP_SNAP
115         || llc->llc.llc_cntl != LLC_CNTL_SNAP
116         || memcmp(llc->snap.snap_org, SNAP_ORG_ETHERNET,
117                   sizeof llc->snap.snap_org)) {
118         return htons(FLOW_DL_TYPE_NONE);
119     }
120
121     ofpbuf_pull(b, sizeof *llc);
122     return llc->snap.snap_type;
123 }
124
125 /* Initializes 'flow' members from 'packet', 'tun_id', and 'in_port.
126  * Initializes 'packet' header pointers as follows:
127  *
128  *    - packet->l2 to the start of the Ethernet header.
129  *
130  *    - packet->l3 to just past the Ethernet header, or just past the
131  *      vlan_header if one is present, to the first byte of the payload of the
132  *      Ethernet frame.
133  *
134  *    - packet->l4 to just past the IPv4 header, if one is present and has a
135  *      correct length, and otherwise NULL.
136  *
137  *    - packet->l7 to just past the TCP or UDP or ICMP header, if one is
138  *      present and has a correct length, and otherwise NULL.
139  */
140 int
141 flow_extract(struct ofpbuf *packet, ovs_be64 tun_id, uint16_t in_port,
142              struct flow *flow)
143 {
144     struct ofpbuf b = *packet;
145     struct eth_header *eth;
146     int retval = 0;
147
148     COVERAGE_INC(flow_extract);
149
150     memset(flow, 0, sizeof *flow);
151     flow->tun_id = tun_id;
152     flow->in_port = in_port;
153
154     packet->l2 = b.data;
155     packet->l3 = NULL;
156     packet->l4 = NULL;
157     packet->l7 = NULL;
158
159     if (b.size < sizeof *eth) {
160         return 0;
161     }
162
163     /* Link layer. */
164     eth = b.data;
165     memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
166     memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
167
168     /* dl_type, vlan_tci. */
169     ofpbuf_pull(&b, ETH_ADDR_LEN * 2);
170     if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
171         parse_vlan(&b, flow);
172     }
173     flow->dl_type = parse_ethertype(&b);
174
175     /* Network layer. */
176     packet->l3 = b.data;
177     if (flow->dl_type == htons(ETH_TYPE_IP)) {
178         const struct ip_header *nh = pull_ip(&b);
179         if (nh) {
180             flow->nw_src = get_unaligned_be32(&nh->ip_src);
181             flow->nw_dst = get_unaligned_be32(&nh->ip_dst);
182             flow->nw_tos = nh->ip_tos & IP_DSCP_MASK;
183             flow->nw_proto = nh->ip_proto;
184             packet->l4 = b.data;
185             if (!IP_IS_FRAGMENT(nh->ip_frag_off)) {
186                 if (flow->nw_proto == IPPROTO_TCP) {
187                     const struct tcp_header *tcp = pull_tcp(&b);
188                     if (tcp) {
189                         flow->tp_src = tcp->tcp_src;
190                         flow->tp_dst = tcp->tcp_dst;
191                         packet->l7 = b.data;
192                     }
193                 } else if (flow->nw_proto == IPPROTO_UDP) {
194                     const struct udp_header *udp = pull_udp(&b);
195                     if (udp) {
196                         flow->tp_src = udp->udp_src;
197                         flow->tp_dst = udp->udp_dst;
198                         packet->l7 = b.data;
199                     }
200                 } else if (flow->nw_proto == IPPROTO_ICMP) {
201                     const struct icmp_header *icmp = pull_icmp(&b);
202                     if (icmp) {
203                         flow->icmp_type = htons(icmp->icmp_type);
204                         flow->icmp_code = htons(icmp->icmp_code);
205                         packet->l7 = b.data;
206                     }
207                 }
208             } else {
209                 retval = 1;
210             }
211         }
212     } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
213         const struct arp_eth_header *arp = pull_arp(&b);
214         if (arp && arp->ar_hrd == htons(1)
215             && arp->ar_pro == htons(ETH_TYPE_IP)
216             && arp->ar_hln == ETH_ADDR_LEN
217             && arp->ar_pln == 4) {
218             /* We only match on the lower 8 bits of the opcode. */
219             if (ntohs(arp->ar_op) <= 0xff) {
220                 flow->nw_proto = ntohs(arp->ar_op);
221             }
222
223             if ((flow->nw_proto == ARP_OP_REQUEST)
224                 || (flow->nw_proto == ARP_OP_REPLY)) {
225                 flow->nw_src = arp->ar_spa;
226                 flow->nw_dst = arp->ar_tpa;
227                 memcpy(flow->arp_sha, arp->ar_sha, ETH_ADDR_LEN);
228                 memcpy(flow->arp_tha, arp->ar_tha, ETH_ADDR_LEN);
229             }
230         }
231     }
232     return retval;
233 }
234
235 /* Extracts the flow stats for a packet.  The 'flow' and 'packet'
236  * arguments must have been initialized through a call to flow_extract().
237  */
238 void
239 flow_extract_stats(const struct flow *flow, struct ofpbuf *packet,
240                    struct dpif_flow_stats *stats)
241 {
242     memset(stats, 0, sizeof(*stats));
243
244     if ((flow->dl_type == htons(ETH_TYPE_IP)) && packet->l4) {
245         if ((flow->nw_proto == IPPROTO_TCP) && packet->l7) {
246             struct tcp_header *tcp = packet->l4;
247             stats->tcp_flags = TCP_FLAGS(tcp->tcp_ctl);
248         }
249     }
250
251     stats->n_bytes = packet->size;
252     stats->n_packets = 1;
253 }
254
255 char *
256 flow_to_string(const struct flow *flow)
257 {
258     struct ds ds = DS_EMPTY_INITIALIZER;
259     flow_format(&ds, flow);
260     return ds_cstr(&ds);
261 }
262
263 void
264 flow_format(struct ds *ds, const struct flow *flow)
265 {
266     ds_put_format(ds, "tunnel%#"PRIx64":in_port%04"PRIx16":tci(",
267                   flow->tun_id, flow->in_port);
268     if (flow->vlan_tci) {
269         ds_put_format(ds, "vlan%"PRIu16",pcp%d",
270                       vlan_tci_to_vid(flow->vlan_tci),
271                       vlan_tci_to_pcp(flow->vlan_tci));
272     } else {
273         ds_put_char(ds, '0');
274     }
275     ds_put_format(ds, ") mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT
276                       " type%04"PRIx16
277                       " proto%"PRIu8
278                       " tos%"PRIu8
279                       " ip"IP_FMT"->"IP_FMT,
280                   ETH_ADDR_ARGS(flow->dl_src),
281                   ETH_ADDR_ARGS(flow->dl_dst),
282                   ntohs(flow->dl_type),
283                   flow->nw_proto,
284                   flow->nw_tos,
285                   IP_ARGS(&flow->nw_src),
286                   IP_ARGS(&flow->nw_dst));
287     if (flow->tp_src || flow->tp_dst) {
288         ds_put_format(ds, " port%"PRIu16"->%"PRIu16,
289                 ntohs(flow->tp_src), ntohs(flow->tp_dst));
290     }
291     if (!eth_addr_is_zero(flow->arp_sha) || !eth_addr_is_zero(flow->arp_tha)) {
292         ds_put_format(ds, " arp_ha"ETH_ADDR_FMT"->"ETH_ADDR_FMT,
293                 ETH_ADDR_ARGS(flow->arp_sha),
294                 ETH_ADDR_ARGS(flow->arp_tha));
295     }
296 }
297
298 void
299 flow_print(FILE *stream, const struct flow *flow)
300 {
301     char *s = flow_to_string(flow);
302     fputs(s, stream);
303     free(s);
304 }
305 \f
306 /* flow_wildcards functions. */
307
308 /* Initializes 'wc' as a set of wildcards that matches every packet. */
309 void
310 flow_wildcards_init_catchall(struct flow_wildcards *wc)
311 {
312     wc->wildcards = FWW_ALL;
313     wc->tun_id_mask = htonll(0);
314     wc->nw_src_mask = htonl(0);
315     wc->nw_dst_mask = htonl(0);
316     memset(wc->reg_masks, 0, sizeof wc->reg_masks);
317     wc->vlan_tci_mask = htons(0);
318     wc->zero = 0;
319 }
320
321 /* Initializes 'wc' as an exact-match set of wildcards; that is, 'wc' does not
322  * wildcard any bits or fields. */
323 void
324 flow_wildcards_init_exact(struct flow_wildcards *wc)
325 {
326     wc->wildcards = 0;
327     wc->tun_id_mask = htonll(UINT64_MAX);
328     wc->nw_src_mask = htonl(UINT32_MAX);
329     wc->nw_dst_mask = htonl(UINT32_MAX);
330     memset(wc->reg_masks, 0xff, sizeof wc->reg_masks);
331     wc->vlan_tci_mask = htons(UINT16_MAX);
332     wc->zero = 0;
333 }
334
335 /* Returns true if 'wc' is exact-match, false if 'wc' wildcards any bits or
336  * fields. */
337 bool
338 flow_wildcards_is_exact(const struct flow_wildcards *wc)
339 {
340     int i;
341
342     if (wc->wildcards
343         || wc->tun_id_mask != htonll(UINT64_MAX)
344         || wc->nw_src_mask != htonl(UINT32_MAX)
345         || wc->nw_dst_mask != htonl(UINT32_MAX)
346         || wc->vlan_tci_mask != htons(UINT16_MAX)) {
347         return false;
348     }
349
350     for (i = 0; i < FLOW_N_REGS; i++) {
351         if (wc->reg_masks[i] != htonl(UINT32_MAX)) {
352             return false;
353         }
354     }
355
356     return true;
357 }
358
359 /* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'.
360  * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in
361  * 'src1' or 'src2' or both.  */
362 void
363 flow_wildcards_combine(struct flow_wildcards *dst,
364                        const struct flow_wildcards *src1,
365                        const struct flow_wildcards *src2)
366 {
367     int i;
368
369     dst->wildcards = src1->wildcards | src2->wildcards;
370     dst->tun_id_mask = src1->tun_id_mask & src2->tun_id_mask;
371     dst->nw_src_mask = src1->nw_src_mask & src2->nw_src_mask;
372     dst->nw_dst_mask = src1->nw_dst_mask & src2->nw_dst_mask;
373     for (i = 0; i < FLOW_N_REGS; i++) {
374         dst->reg_masks[i] = src1->reg_masks[i] & src2->reg_masks[i];
375     }
376     dst->vlan_tci_mask = src1->vlan_tci_mask & src2->vlan_tci_mask;
377 }
378
379 /* Returns a hash of the wildcards in 'wc'. */
380 uint32_t
381 flow_wildcards_hash(const struct flow_wildcards *wc)
382 {
383     /* If you change struct flow_wildcards and thereby trigger this
384      * assertion, please check that the new struct flow_wildcards has no holes
385      * in it before you update the assertion. */
386     BUILD_ASSERT_DECL(sizeof *wc == 24 + FLOW_N_REGS * 4);
387     return hash_bytes(wc, sizeof *wc, 0);
388 }
389
390 /* Returns true if 'a' and 'b' represent the same wildcards, false if they are
391  * different. */
392 bool
393 flow_wildcards_equal(const struct flow_wildcards *a,
394                      const struct flow_wildcards *b)
395 {
396     int i;
397
398     if (a->wildcards != b->wildcards
399         || a->tun_id_mask != b->tun_id_mask
400         || a->nw_src_mask != b->nw_src_mask
401         || a->nw_dst_mask != b->nw_dst_mask
402         || a->vlan_tci_mask != b->vlan_tci_mask) {
403         return false;
404     }
405
406     for (i = 0; i < FLOW_N_REGS; i++) {
407         if (a->reg_masks[i] != b->reg_masks[i]) {
408             return false;
409         }
410     }
411
412     return true;
413 }
414
415 /* Returns true if at least one bit or field is wildcarded in 'a' but not in
416  * 'b', false otherwise. */
417 bool
418 flow_wildcards_has_extra(const struct flow_wildcards *a,
419                          const struct flow_wildcards *b)
420 {
421     int i;
422
423     for (i = 0; i < FLOW_N_REGS; i++) {
424         if ((a->reg_masks[i] & b->reg_masks[i]) != b->reg_masks[i]) {
425             return true;
426         }
427     }
428
429     return (a->wildcards & ~b->wildcards
430             || (a->tun_id_mask & b->tun_id_mask) != b->tun_id_mask
431             || (a->nw_src_mask & b->nw_src_mask) != b->nw_src_mask
432             || (a->nw_dst_mask & b->nw_dst_mask) != b->nw_dst_mask
433             || (a->vlan_tci_mask & b->vlan_tci_mask) != b->vlan_tci_mask);
434 }
435
436 static bool
437 set_nw_mask(ovs_be32 *maskp, ovs_be32 mask)
438 {
439     if (ip_is_cidr(mask)) {
440         *maskp = mask;
441         return true;
442     } else {
443         return false;
444     }
445 }
446
447 /* Sets the IP (or ARP) source wildcard mask to CIDR 'mask' (consisting of N
448  * high-order 1-bit and 32-N low-order 0-bits).  Returns true if successful,
449  * false if 'mask' is not a CIDR mask.  */
450 bool
451 flow_wildcards_set_nw_src_mask(struct flow_wildcards *wc, ovs_be32 mask)
452 {
453     return set_nw_mask(&wc->nw_src_mask, mask);
454 }
455
456 /* Sets the IP (or ARP) destination wildcard mask to CIDR 'mask' (consisting of
457  * N high-order 1-bit and 32-N low-order 0-bits).  Returns true if successful,
458  * false if 'mask' is not a CIDR mask.  */
459 bool
460 flow_wildcards_set_nw_dst_mask(struct flow_wildcards *wc, ovs_be32 mask)
461 {
462     return set_nw_mask(&wc->nw_dst_mask, mask);
463 }
464
465 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
466  * (A 0-bit indicates a wildcard bit.) */
467 void
468 flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
469 {
470     wc->reg_masks[idx] = mask;
471 }
472
473 /* Hashes 'flow' based on its L2 through L4 protocol information. */
474 uint32_t
475 flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis)
476 {
477     struct {
478         ovs_be32 ip_addr;
479         ovs_be16 eth_type;
480         ovs_be16 vlan_tci;
481         ovs_be16 tp_addr;
482         uint8_t eth_addr[ETH_ADDR_LEN];
483         uint8_t ip_proto;
484     } fields;
485
486     int i;
487
488     memset(&fields, 0, sizeof fields);
489     for (i = 0; i < ETH_ADDR_LEN; i++) {
490         fields.eth_addr[i] = flow->dl_src[i] ^ flow->dl_dst[i];
491     }
492     fields.vlan_tci = flow->vlan_tci & htons(VLAN_VID_MASK);
493     fields.eth_type = flow->dl_type;
494     if (fields.eth_type == htons(ETH_TYPE_IP)) {
495         fields.ip_addr = flow->nw_src ^ flow->nw_dst;
496         fields.ip_proto = flow->nw_proto;
497         if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_UDP) {
498             fields.tp_addr = flow->tp_src ^ flow->tp_dst;
499         } else {
500             fields.tp_addr = htons(0);
501         }
502     } else {
503         fields.ip_addr = htonl(0);
504         fields.ip_proto = 0;
505         fields.tp_addr = htons(0);
506     }
507     return hash_bytes(&fields, sizeof fields, basis);
508 }