Fix handling of IP but non-TCP, non-UDP packets in kernel flow_extract().
[sliver-openvswitch.git] / datapath / flow.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008 The Board of Trustees of The Leland 
4  * Stanford Junior University
5  */
6
7 #include "flow.h"
8 #include <linux/netdevice.h>
9 #include <linux/etherdevice.h>
10 #include <linux/if_ether.h>
11 #include <linux/if_vlan.h>
12 #include <net/llc_pdu.h>
13 #include <linux/ip.h>
14 #include <linux/jiffies.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/tcp.h>
18 #include <linux/udp.h>
19 #include <linux/in.h>
20 #include <linux/rcupdate.h>
21 #include <net/ip.h>
22
23 #include "openflow.h"
24 #include "compat.h"
25 #include "snap.h"
26
27 struct kmem_cache *flow_cache;
28
29 /* Internal function used to compare fields in flow. */
30 static inline
31 int flow_fields_match(const struct sw_flow_key *a, const struct sw_flow_key *b,
32                 uint16_t w)
33 {
34         return ((w & OFPFW_IN_PORT || a->in_port == b->in_port)
35                 && (w & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
36                 && (w & OFPFW_DL_SRC || !memcmp(a->dl_src, b->dl_src, ETH_ALEN))
37                 && (w & OFPFW_DL_DST || !memcmp(a->dl_dst, b->dl_dst, ETH_ALEN))
38                 && (w & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
39                 && (w & OFPFW_NW_SRC || a->nw_src == b->nw_src)
40                 && (w & OFPFW_NW_DST || a->nw_dst == b->nw_dst)
41                 && (w & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
42                 && (w & OFPFW_TP_SRC || a->tp_src == b->tp_src)
43                 && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst));
44 }
45
46 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
47  * modulo wildcards, zero otherwise. */
48 int flow_matches(const struct sw_flow_key *a, const struct sw_flow_key *b)
49 {
50         return flow_fields_match(a, b, (a->wildcards | b->wildcards));
51 }
52 EXPORT_SYMBOL(flow_matches);
53
54 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key 
55  * describing the deletion) match, that is, if their fields are 
56  * equal modulo wildcards, zero otherwise.  If 'strict' is nonzero, the
57  * wildcards must match in both 't_key' and 'd_key'.  Note that the
58  * table's wildcards are ignored unless 'strict' is set. */
59 int flow_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, int strict)
60 {
61         if (strict && (t->wildcards != d->wildcards))
62                 return 0;
63
64         return flow_fields_match(t, d, d->wildcards);
65 }
66 EXPORT_SYMBOL(flow_del_matches);
67
68 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
69 {
70         to->wildcards = ntohs(from->wildcards) & OFPFW_ALL;
71         memset(to->pad, '\0', sizeof(to->pad));
72         to->in_port = from->in_port;
73         to->dl_vlan = from->dl_vlan;
74         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
75         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
76         to->dl_type = from->dl_type;
77
78         to->nw_src = to->nw_dst = to->nw_proto = 0;
79         to->tp_src = to->tp_dst = 0;
80
81 #define OFPFW_TP (OFPFW_TP_SRC | OFPFW_TP_DST)
82 #define OFPFW_NW (OFPFW_NW_SRC | OFPFW_NW_DST | OFPFW_NW_PROTO)
83         if (to->wildcards & OFPFW_DL_TYPE) {
84                 /* Can't sensibly match on network or transport headers if the
85                  * data link type is unknown. */
86                 to->wildcards |= OFPFW_NW | OFPFW_TP;
87         } else if (from->dl_type == htons(ETH_P_IP)) {
88                 to->nw_src   = from->nw_src;
89                 to->nw_dst   = from->nw_dst;
90                 to->nw_proto = from->nw_proto;
91
92                 if (to->wildcards & OFPFW_NW_PROTO) {
93                         /* Can't sensibly match on transport headers if the
94                          * network protocol is unknown. */
95                         to->wildcards |= OFPFW_TP;
96                 } else if (from->nw_proto == IPPROTO_TCP
97                            || from->nw_proto == IPPROTO_UDP) {
98                         to->tp_src = from->tp_src;
99                         to->tp_dst = from->tp_dst;
100                 } else {
101                         /* Transport layer fields are undefined.  Mark them as
102                          * exact-match to allow such flows to reside in
103                          * table-hash, instead of falling into table-linear. */
104                         to->wildcards &= ~OFPFW_TP;
105                 }
106         } else {
107                 /* Network and transport layer fields are undefined.  Mark them
108                  * as exact-match to allow such flows to reside in table-hash,
109                  * instead of falling into table-linear. */
110                 to->wildcards &= ~(OFPFW_NW | OFPFW_TP);
111         }
112 }
113
114 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
115 {
116         to->wildcards = htons(from->wildcards);
117         to->in_port   = from->in_port;
118         to->dl_vlan   = from->dl_vlan;
119         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
120         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
121         to->dl_type   = from->dl_type;
122         to->nw_src        = from->nw_src;
123         to->nw_dst        = from->nw_dst;
124         to->nw_proto  = from->nw_proto;
125         to->tp_src        = from->tp_src;
126         to->tp_dst        = from->tp_dst;
127         memset(to->pad, '\0', sizeof(to->pad));
128 }
129
130 int flow_timeout(struct sw_flow *flow)
131 {
132         if (flow->idle_timeout != OFP_FLOW_PERMANENT
133             && time_after(jiffies, flow->used + flow->idle_timeout * HZ))
134                 return OFPER_IDLE_TIMEOUT;
135         else if (flow->hard_timeout != OFP_FLOW_PERMANENT
136                  && time_after(jiffies,
137                                flow->init_time + flow->hard_timeout * HZ))
138                 return OFPER_HARD_TIMEOUT;
139         else
140                 return -1;
141 }
142 EXPORT_SYMBOL(flow_timeout);
143
144 /* Allocates and returns a new flow with 'n_actions' action, using allocation
145  * flags 'flags'.  Returns the new flow or a null pointer on failure. */
146 struct sw_flow *flow_alloc(int n_actions, gfp_t flags)
147 {
148         struct sw_flow *flow = kmem_cache_alloc(flow_cache, flags);
149         if (unlikely(!flow))
150                 return NULL;
151
152         flow->n_actions = n_actions;
153         flow->actions = kmalloc(n_actions * sizeof *flow->actions,
154                                 flags);
155         if (unlikely(!flow->actions) && n_actions > 0) {
156                 kmem_cache_free(flow_cache, flow);
157                 return NULL;
158         }
159         return flow;
160 }
161
162 /* Frees 'flow' immediately. */
163 void flow_free(struct sw_flow *flow)
164 {
165         if (unlikely(!flow))
166                 return;
167         kfree(flow->actions);
168         kmem_cache_free(flow_cache, flow);
169 }
170 EXPORT_SYMBOL(flow_free);
171
172 /* RCU callback used by flow_deferred_free. */
173 static void rcu_callback(struct rcu_head *rcu)
174 {
175         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
176         flow_free(flow);
177 }
178
179 /* Schedules 'flow' to be freed after the next RCU grace period.
180  * The caller must hold rcu_read_lock for this to be sensible. */
181 void flow_deferred_free(struct sw_flow *flow)
182 {
183         call_rcu(&flow->rcu, rcu_callback);
184 }
185 EXPORT_SYMBOL(flow_deferred_free);
186
187 /* Prints a representation of 'key' to the kernel log. */
188 void print_flow(const struct sw_flow_key *key)
189 {
190         printk("wild%04x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
191                         "->%02x:%02x:%02x:%02x:%02x:%02x "
192                         "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
193                         key->wildcards, ntohs(key->in_port), ntohs(key->dl_vlan),
194                         key->dl_src[0], key->dl_src[1], key->dl_src[2],
195                         key->dl_src[3], key->dl_src[4], key->dl_src[5],
196                         key->dl_dst[0], key->dl_dst[1], key->dl_dst[2],
197                         key->dl_dst[3], key->dl_dst[4], key->dl_dst[5],
198                         ntohs(key->dl_type),
199                         ((unsigned char *)&key->nw_src)[0],
200                         ((unsigned char *)&key->nw_src)[1],
201                         ((unsigned char *)&key->nw_src)[2],
202                         ((unsigned char *)&key->nw_src)[3],
203                         ((unsigned char *)&key->nw_dst)[0],
204                         ((unsigned char *)&key->nw_dst)[1],
205                         ((unsigned char *)&key->nw_dst)[2],
206                         ((unsigned char *)&key->nw_dst)[3],
207                         ntohs(key->tp_src), ntohs(key->tp_dst));
208 }
209 EXPORT_SYMBOL(print_flow);
210
211 static int tcphdr_ok(struct sk_buff *skb)
212 {
213         int th_ofs = skb_transport_offset(skb);
214         if (skb->len >= th_ofs + sizeof(struct tcphdr)) {
215                 int tcp_len = tcp_hdrlen(skb);
216                 return (tcp_len >= sizeof(struct tcphdr)
217                         && skb->len >= th_ofs + tcp_len);
218         }
219         return 0;
220 }
221
222 static int udphdr_ok(struct sk_buff *skb)
223 {
224         int th_ofs = skb_transport_offset(skb);
225         return skb->len >= th_ofs + sizeof(struct udphdr);
226 }
227
228 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
229  * and initializes 'key' to match.  Returns 1 if 'skb' contains an IP
230  * fragment, 0 otherwise. */
231 int flow_extract(struct sk_buff *skb, uint16_t in_port,
232                  struct sw_flow_key *key)
233 {
234         struct ethhdr *mac;
235         int nh_ofs, th_ofs;
236         int retval = 0;
237
238         key->in_port = htons(in_port);
239         key->wildcards = 0;
240         memset(key->pad, '\0', sizeof(key->pad));
241
242         /* This code doesn't check that skb->len is long enough to contain the
243          * MAC or network header.  With a 46-byte minimum length frame this
244          * assumption is always correct. */
245
246         /* Doesn't verify checksums.  Should it? */
247
248         /* Data link layer.  We only support Ethernet. */
249         mac = eth_hdr(skb);
250         nh_ofs = sizeof(struct ethhdr);
251         if (likely(ntohs(mac->h_proto) >= OFP_DL_TYPE_ETH2_CUTOFF)) {
252                 /* This is an Ethernet II frame */
253                 key->dl_type = mac->h_proto;
254         } else {
255                 /* This is an 802.2 frame */
256                 if (snap_get_ethertype(skb, &key->dl_type) != -EINVAL) {
257                         nh_ofs += sizeof(struct snap_hdr);
258                 } else {
259                         key->dl_type = OFP_DL_TYPE_NOT_ETH_TYPE;
260                         nh_ofs += sizeof(struct llc_pdu_un);
261                 }
262         }
263
264         /* Check for a VLAN tag */
265         if (likely(key->dl_type != htons(ETH_P_8021Q))) {
266                 key->dl_vlan = htons(OFP_VLAN_NONE);
267         } else {
268                 struct vlan_hdr *vh = (struct vlan_hdr *)(skb_mac_header(skb) + nh_ofs);
269                 key->dl_type = vh->h_vlan_encapsulated_proto;
270                 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
271                 nh_ofs += sizeof(*vh);
272         }
273         memcpy(key->dl_src, mac->h_source, ETH_ALEN);
274         memcpy(key->dl_dst, mac->h_dest, ETH_ALEN);
275         skb_set_network_header(skb, nh_ofs);
276
277         /* Network layer. */
278         if (likely(key->dl_type == htons(ETH_P_IP))) {
279                 struct iphdr *nh = ip_hdr(skb);
280                 key->nw_src = nh->saddr;
281                 key->nw_dst = nh->daddr;
282                 key->nw_proto = nh->protocol;
283                 th_ofs = nh_ofs + nh->ihl * 4;
284                 skb_set_transport_header(skb, th_ofs);
285
286                 /* Transport layer. */
287                 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET))) {
288                         if (key->nw_proto == IPPROTO_TCP) {
289                                 if (tcphdr_ok(skb)) {
290                                         struct tcphdr *tcp = tcp_hdr(skb);
291                                         key->tp_src = tcp->source;
292                                         key->tp_dst = tcp->dest;
293                                 } else {
294                                         /* Avoid tricking other code into
295                                          * thinking that this packet has an L4
296                                          * header. */
297                                         goto no_proto;
298                                 }
299                         } else if (key->nw_proto == IPPROTO_UDP) {
300                                 if (udphdr_ok(skb)) {
301                                         struct udphdr *udp = udp_hdr(skb);
302                                         key->tp_src = udp->source;
303                                         key->tp_dst = udp->dest;
304                                 } else {
305                                         /* Avoid tricking other code into
306                                          * thinking that this packet has an L4
307                                          * header. */
308                                         goto no_proto;
309                                 }
310                         } else {
311                                 goto no_th;
312                         }
313                 } else {
314                         retval = 1;
315                         goto no_th;
316                 }
317
318                 return 0;
319         }
320
321         key->nw_src = 0;
322         key->nw_dst = 0;
323
324 no_proto:
325         key->nw_proto = 0;
326
327 no_th:
328         key->tp_src = 0;
329         key->tp_dst = 0;
330         return retval;
331 }
332
333 /* Initializes the flow module.
334  * Returns zero if successful or a negative error code. */
335 int flow_init(void)
336 {
337         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
338                                         0, NULL);
339         if (flow_cache == NULL)
340                 return -ENOMEM;
341
342         return 0;
343 }
344
345 /* Uninitializes the flow module. */
346 void flow_exit(void)
347 {
348         kmem_cache_destroy(flow_cache);
349 }
350