Get rid of ip.h, migrating its users to packets.h.
[sliver-openvswitch.git] / lib / flow.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33 #include <sys/types.h>
34 #include "flow.h"
35 #include <inttypes.h>
36 #include <netinet/in.h>
37 #include <string.h>
38 #include "buffer.h"
39 #include "hash.h"
40 #include "openflow.h"
41 #include "packets.h"
42
43 #include "vlog.h"
44 #define THIS_MODULE VLM_flow
45
46 void
47 flow_extract(struct buffer *packet, uint16_t in_port, struct flow *flow)
48 {
49     struct buffer b = *packet;
50     struct eth_header *eth;
51
52     if (b.size < ETH_TOTAL_MIN) {
53         VLOG_WARN("packet length %d less than minimum size %d",
54                   b.size, ETH_TOTAL_MIN);
55     }
56
57     memset(flow, 0, sizeof *flow);
58     flow->in_port = htons(in_port);
59
60     packet->l2 = b.data;
61     packet->l3 = NULL;
62     packet->l4 = NULL;
63
64     eth = buffer_at(&b, 0, sizeof *eth);
65     if (eth) {
66         buffer_pull(&b, ETH_HEADER_LEN);
67         if (ntohs(eth->eth_type) >= OFP_DL_TYPE_ETH2_CUTOFF) {
68             /* This is an Ethernet II frame */
69             flow->dl_type = eth->eth_type;
70         } else {
71             /* This is an 802.2 frame */
72             struct llc_snap_header *h = buffer_at(&b, 0, sizeof *h);
73             if (h == NULL) {
74                 return;
75             }
76             if (h->llc.llc_dsap == LLC_DSAP_SNAP
77                 && h->llc.llc_ssap == LLC_SSAP_SNAP
78                 && h->llc.llc_cntl == LLC_CNTL_SNAP
79                 && !memcmp(h->snap.snap_org, SNAP_ORG_ETHERNET,
80                            sizeof h->snap.snap_org)) {
81                 flow->dl_type = h->snap.snap_type;
82                 buffer_pull(&b, sizeof *h);
83             } else {
84                 flow->dl_type = OFP_DL_TYPE_NOT_ETH_TYPE;
85                 buffer_pull(&b, sizeof(struct llc_header));
86             }
87         }
88
89         /* Check for a VLAN tag */
90         if (flow->dl_type != htons(ETH_TYPE_VLAN)) {
91             flow->dl_vlan = htons(OFP_VLAN_NONE);
92         } else {
93             struct vlan_header *vh = buffer_at(&b, 0, sizeof *vh);
94             flow->dl_type = vh->vlan_next_type;
95             flow->dl_vlan = vh->vlan_tci & htons(VLAN_VID);
96             buffer_pull(&b, sizeof *vh);
97         }
98         memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
99         memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
100
101         packet->l3 = b.data;
102         if (flow->dl_type == htons(ETH_TYPE_IP)) {
103             const struct ip_header *nh = buffer_at(&b, 0, sizeof *nh);
104             if (nh) {
105                 int ip_len = IP_IHL(nh->ip_ihl_ver) * 4;
106                 if (ip_len < IP_HEADER_LEN) {
107                     return;
108                 }
109
110                 flow->nw_src = nh->ip_src;
111                 flow->nw_dst = nh->ip_dst;
112                 flow->nw_proto = nh->ip_proto;
113                 if (flow->nw_proto == IP_TYPE_TCP
114                     || flow->nw_proto == IP_TYPE_UDP) {
115                     const struct udp_header *th;
116                     th = packet->l4 = buffer_at(&b, ip_len, sizeof *th);
117                     if (th) {
118                         flow->tp_src = th->udp_src;
119                         flow->tp_dst = th->udp_dst;
120                     } else {
121                         /* Avoid tricking other code into thinking that this
122                          * packet has an L4 header. */
123                         flow->nw_proto = 0;
124                     }
125                 }
126             }
127         } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
128             const struct arp_eth_header *ah = buffer_at(&b, 0, sizeof *ah);
129             if (ah && ah->ar_hrd == htons(ARP_HRD_ETHERNET)
130                 && ah->ar_pro == htons(ARP_PRO_IP)
131                 && ah->ar_hln == ETH_ADDR_LEN
132                 && ah->ar_pln == sizeof flow->nw_src)
133             {
134                 /* check if sha/tha match dl_src/dl_dst? */
135                 flow->nw_src = ah->ar_spa;
136                 flow->nw_dst = ah->ar_tpa;
137             }
138         }
139     }
140 }
141
142 void
143 flow_print(FILE *stream, const struct flow *flow) 
144 {
145     fprintf(stream,
146             "port%04x:vlan%04x mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT" "
147             "proto%04x ip"IP_FMT"->"IP_FMT" port%d->%d",
148             ntohs(flow->in_port), ntohs(flow->dl_vlan),
149             ETH_ADDR_ARGS(flow->dl_src), ETH_ADDR_ARGS(flow->dl_dst),
150             ntohs(flow->dl_type),
151             IP_ARGS(&flow->nw_src), IP_ARGS(&flow->nw_dst),
152             ntohs(flow->tp_src), ntohs(flow->tp_dst));
153 }
154
155 int
156 flow_compare(const struct flow *a, const struct flow *b)
157 {
158     return memcmp(a, b, sizeof *a);
159 }
160
161 unsigned long int
162 flow_hash(const struct flow *flow, uint32_t basis) 
163 {
164     return hash_fnv(flow, sizeof *flow, basis);
165 }