datapath: Restructure datapath.c and flow.c
[sliver-openvswitch.git] / datapath / flow.c
1 /*
2  * Copyright (c) 2007-2013 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #include "flow.h"
20 #include "datapath.h"
21 #include <linux/uaccess.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <net/llc_pdu.h>
27 #include <linux/kernel.h>
28 #include <linux/jhash.h>
29 #include <linux/jiffies.h>
30 #include <linux/llc.h>
31 #include <linux/module.h>
32 #include <linux/in.h>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.h>
35 #include <linux/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/sctp.h>
38 #include <linux/tcp.h>
39 #include <linux/udp.h>
40 #include <linux/icmp.h>
41 #include <linux/icmpv6.h>
42 #include <linux/rculist.h>
43 #include <net/ip.h>
44 #include <net/ipv6.h>
45 #include <net/ndisc.h>
46
47 #include "vlan.h"
48
49 u64 ovs_flow_used_time(unsigned long flow_jiffies)
50 {
51         struct timespec cur_ts;
52         u64 cur_ms, idle_ms;
53
54         ktime_get_ts(&cur_ts);
55         idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
56         cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
57                  cur_ts.tv_nsec / NSEC_PER_MSEC;
58
59         return cur_ms - idle_ms;
60 }
61
62 #define TCP_FLAGS_OFFSET 13
63 #define TCP_FLAG_MASK 0x3f
64
65 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
66 {
67         u8 tcp_flags = 0;
68
69         if ((flow->key.eth.type == htons(ETH_P_IP) ||
70              flow->key.eth.type == htons(ETH_P_IPV6)) &&
71             flow->key.ip.proto == IPPROTO_TCP &&
72             likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
73                 u8 *tcp = (u8 *)tcp_hdr(skb);
74                 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
75         }
76
77         spin_lock(&flow->lock);
78         flow->used = jiffies;
79         flow->packet_count++;
80         flow->byte_count += skb->len;
81         flow->tcp_flags |= tcp_flags;
82         spin_unlock(&flow->lock);
83 }
84
85 static int check_header(struct sk_buff *skb, int len)
86 {
87         if (unlikely(skb->len < len))
88                 return -EINVAL;
89         if (unlikely(!pskb_may_pull(skb, len)))
90                 return -ENOMEM;
91         return 0;
92 }
93
94 static bool arphdr_ok(struct sk_buff *skb)
95 {
96         return pskb_may_pull(skb, skb_network_offset(skb) +
97                                   sizeof(struct arp_eth_header));
98 }
99
100 static int check_iphdr(struct sk_buff *skb)
101 {
102         unsigned int nh_ofs = skb_network_offset(skb);
103         unsigned int ip_len;
104         int err;
105
106         err = check_header(skb, nh_ofs + sizeof(struct iphdr));
107         if (unlikely(err))
108                 return err;
109
110         ip_len = ip_hdrlen(skb);
111         if (unlikely(ip_len < sizeof(struct iphdr) ||
112                      skb->len < nh_ofs + ip_len))
113                 return -EINVAL;
114
115         skb_set_transport_header(skb, nh_ofs + ip_len);
116         return 0;
117 }
118
119 static bool tcphdr_ok(struct sk_buff *skb)
120 {
121         int th_ofs = skb_transport_offset(skb);
122         int tcp_len;
123
124         if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
125                 return false;
126
127         tcp_len = tcp_hdrlen(skb);
128         if (unlikely(tcp_len < sizeof(struct tcphdr) ||
129                      skb->len < th_ofs + tcp_len))
130                 return false;
131
132         return true;
133 }
134
135 static bool udphdr_ok(struct sk_buff *skb)
136 {
137         return pskb_may_pull(skb, skb_transport_offset(skb) +
138                                   sizeof(struct udphdr));
139 }
140
141 static bool sctphdr_ok(struct sk_buff *skb)
142 {
143         return pskb_may_pull(skb, skb_transport_offset(skb) +
144                                   sizeof(struct sctphdr));
145 }
146
147 static bool icmphdr_ok(struct sk_buff *skb)
148 {
149         return pskb_may_pull(skb, skb_transport_offset(skb) +
150                                   sizeof(struct icmphdr));
151 }
152
153 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
154 {
155         unsigned int nh_ofs = skb_network_offset(skb);
156         unsigned int nh_len;
157         int payload_ofs;
158         struct ipv6hdr *nh;
159         uint8_t nexthdr;
160         __be16 frag_off;
161         int err;
162
163         err = check_header(skb, nh_ofs + sizeof(*nh));
164         if (unlikely(err))
165                 return err;
166
167         nh = ipv6_hdr(skb);
168         nexthdr = nh->nexthdr;
169         payload_ofs = (u8 *)(nh + 1) - skb->data;
170
171         key->ip.proto = NEXTHDR_NONE;
172         key->ip.tos = ipv6_get_dsfield(nh);
173         key->ip.ttl = nh->hop_limit;
174         key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
175         key->ipv6.addr.src = nh->saddr;
176         key->ipv6.addr.dst = nh->daddr;
177
178         payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
179         if (unlikely(payload_ofs < 0))
180                 return -EINVAL;
181
182         if (frag_off) {
183                 if (frag_off & htons(~0x7))
184                         key->ip.frag = OVS_FRAG_TYPE_LATER;
185                 else
186                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
187         }
188
189         nh_len = payload_ofs - nh_ofs;
190         skb_set_transport_header(skb, nh_ofs + nh_len);
191         key->ip.proto = nexthdr;
192         return nh_len;
193 }
194
195 static bool icmp6hdr_ok(struct sk_buff *skb)
196 {
197         return pskb_may_pull(skb, skb_transport_offset(skb) +
198                                   sizeof(struct icmp6hdr));
199 }
200
201 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
202 {
203         struct qtag_prefix {
204                 __be16 eth_type; /* ETH_P_8021Q */
205                 __be16 tci;
206         };
207         struct qtag_prefix *qp;
208
209         if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
210                 return 0;
211
212         if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
213                                          sizeof(__be16))))
214                 return -ENOMEM;
215
216         qp = (struct qtag_prefix *) skb->data;
217         key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
218         __skb_pull(skb, sizeof(struct qtag_prefix));
219
220         return 0;
221 }
222
223 static __be16 parse_ethertype(struct sk_buff *skb)
224 {
225         struct llc_snap_hdr {
226                 u8  dsap;  /* Always 0xAA */
227                 u8  ssap;  /* Always 0xAA */
228                 u8  ctrl;
229                 u8  oui[3];
230                 __be16 ethertype;
231         };
232         struct llc_snap_hdr *llc;
233         __be16 proto;
234
235         proto = *(__be16 *) skb->data;
236         __skb_pull(skb, sizeof(__be16));
237
238         if (ntohs(proto) >= ETH_P_802_3_MIN)
239                 return proto;
240
241         if (skb->len < sizeof(struct llc_snap_hdr))
242                 return htons(ETH_P_802_2);
243
244         if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
245                 return htons(0);
246
247         llc = (struct llc_snap_hdr *) skb->data;
248         if (llc->dsap != LLC_SAP_SNAP ||
249             llc->ssap != LLC_SAP_SNAP ||
250             (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
251                 return htons(ETH_P_802_2);
252
253         __skb_pull(skb, sizeof(struct llc_snap_hdr));
254
255         if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
256                 return llc->ethertype;
257
258         return htons(ETH_P_802_2);
259 }
260
261 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
262                         int nh_len)
263 {
264         struct icmp6hdr *icmp = icmp6_hdr(skb);
265
266         /* The ICMPv6 type and code fields use the 16-bit transport port
267          * fields, so we need to store them in 16-bit network byte order.
268          */
269         key->ipv6.tp.src = htons(icmp->icmp6_type);
270         key->ipv6.tp.dst = htons(icmp->icmp6_code);
271
272         if (icmp->icmp6_code == 0 &&
273             (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
274              icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
275                 int icmp_len = skb->len - skb_transport_offset(skb);
276                 struct nd_msg *nd;
277                 int offset;
278
279                 /* In order to process neighbor discovery options, we need the
280                  * entire packet.
281                  */
282                 if (unlikely(icmp_len < sizeof(*nd)))
283                         return 0;
284
285                 if (unlikely(skb_linearize(skb)))
286                         return -ENOMEM;
287
288                 nd = (struct nd_msg *)skb_transport_header(skb);
289                 key->ipv6.nd.target = nd->target;
290
291                 icmp_len -= sizeof(*nd);
292                 offset = 0;
293                 while (icmp_len >= 8) {
294                         struct nd_opt_hdr *nd_opt =
295                                  (struct nd_opt_hdr *)(nd->opt + offset);
296                         int opt_len = nd_opt->nd_opt_len * 8;
297
298                         if (unlikely(!opt_len || opt_len > icmp_len))
299                                 return 0;
300
301                         /* Store the link layer address if the appropriate
302                          * option is provided.  It is considered an error if
303                          * the same link layer option is specified twice.
304                          */
305                         if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
306                             && opt_len == 8) {
307                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
308                                         goto invalid;
309                                 memcpy(key->ipv6.nd.sll,
310                                     &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
311                         } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
312                                    && opt_len == 8) {
313                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
314                                         goto invalid;
315                                 memcpy(key->ipv6.nd.tll,
316                                     &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
317                         }
318
319                         icmp_len -= opt_len;
320                         offset += opt_len;
321                 }
322         }
323
324         return 0;
325
326 invalid:
327         memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
328         memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
329         memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
330
331         return 0;
332 }
333
334 /**
335  * ovs_flow_extract - extracts a flow key from an Ethernet frame.
336  * @skb: sk_buff that contains the frame, with skb->data pointing to the
337  * Ethernet header
338  * @in_port: port number on which @skb was received.
339  * @key: output flow key
340  *
341  * The caller must ensure that skb->len >= ETH_HLEN.
342  *
343  * Returns 0 if successful, otherwise a negative errno value.
344  *
345  * Initializes @skb header pointers as follows:
346  *
347  *    - skb->mac_header: the Ethernet header.
348  *
349  *    - skb->network_header: just past the Ethernet header, or just past the
350  *      VLAN header, to the first byte of the Ethernet payload.
351  *
352  *    - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
353  *      on output, then just past the IP header, if one is present and
354  *      of a correct length, otherwise the same as skb->network_header.
355  *      For other key->eth.type values it is left untouched.
356  */
357 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
358 {
359         int error;
360         struct ethhdr *eth;
361
362         memset(key, 0, sizeof(*key));
363
364         key->phy.priority = skb->priority;
365         if (OVS_CB(skb)->tun_key)
366                 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
367         key->phy.in_port = in_port;
368         key->phy.skb_mark = skb->mark;
369
370         skb_reset_mac_header(skb);
371
372         /* Link layer.  We are guaranteed to have at least the 14 byte Ethernet
373          * header in the linear data area.
374          */
375         eth = eth_hdr(skb);
376         memcpy(key->eth.src, eth->h_source, ETH_ALEN);
377         memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
378
379         __skb_pull(skb, 2 * ETH_ALEN);
380         /* We are going to push all headers that we pull, so no need to
381          * update skb->csum here. */
382
383         if (vlan_tx_tag_present(skb))
384                 key->eth.tci = htons(vlan_get_tci(skb));
385         else if (eth->h_proto == htons(ETH_P_8021Q))
386                 if (unlikely(parse_vlan(skb, key)))
387                         return -ENOMEM;
388
389         key->eth.type = parse_ethertype(skb);
390         if (unlikely(key->eth.type == htons(0)))
391                 return -ENOMEM;
392
393         skb_reset_network_header(skb);
394         __skb_push(skb, skb->data - skb_mac_header(skb));
395
396         /* Network layer. */
397         if (key->eth.type == htons(ETH_P_IP)) {
398                 struct iphdr *nh;
399                 __be16 offset;
400
401                 error = check_iphdr(skb);
402                 if (unlikely(error)) {
403                         if (error == -EINVAL) {
404                                 skb->transport_header = skb->network_header;
405                                 error = 0;
406                         }
407                         return error;
408                 }
409
410                 nh = ip_hdr(skb);
411                 key->ipv4.addr.src = nh->saddr;
412                 key->ipv4.addr.dst = nh->daddr;
413
414                 key->ip.proto = nh->protocol;
415                 key->ip.tos = nh->tos;
416                 key->ip.ttl = nh->ttl;
417
418                 offset = nh->frag_off & htons(IP_OFFSET);
419                 if (offset) {
420                         key->ip.frag = OVS_FRAG_TYPE_LATER;
421                         return 0;
422                 }
423                 if (nh->frag_off & htons(IP_MF) ||
424                          skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
425                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
426
427                 /* Transport layer. */
428                 if (key->ip.proto == IPPROTO_TCP) {
429                         if (tcphdr_ok(skb)) {
430                                 struct tcphdr *tcp = tcp_hdr(skb);
431                                 key->ipv4.tp.src = tcp->source;
432                                 key->ipv4.tp.dst = tcp->dest;
433                         }
434                 } else if (key->ip.proto == IPPROTO_UDP) {
435                         if (udphdr_ok(skb)) {
436                                 struct udphdr *udp = udp_hdr(skb);
437                                 key->ipv4.tp.src = udp->source;
438                                 key->ipv4.tp.dst = udp->dest;
439                         }
440                 } else if (key->ip.proto == IPPROTO_SCTP) {
441                         if (sctphdr_ok(skb)) {
442                                 struct sctphdr *sctp = sctp_hdr(skb);
443                                 key->ipv4.tp.src = sctp->source;
444                                 key->ipv4.tp.dst = sctp->dest;
445                         }
446                 } else if (key->ip.proto == IPPROTO_ICMP) {
447                         if (icmphdr_ok(skb)) {
448                                 struct icmphdr *icmp = icmp_hdr(skb);
449                                 /* The ICMP type and code fields use the 16-bit
450                                  * transport port fields, so we need to store
451                                  * them in 16-bit network byte order. */
452                                 key->ipv4.tp.src = htons(icmp->type);
453                                 key->ipv4.tp.dst = htons(icmp->code);
454                         }
455                 }
456
457         } else if ((key->eth.type == htons(ETH_P_ARP) ||
458                    key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
459                 struct arp_eth_header *arp;
460
461                 arp = (struct arp_eth_header *)skb_network_header(skb);
462
463                 if (arp->ar_hrd == htons(ARPHRD_ETHER)
464                                 && arp->ar_pro == htons(ETH_P_IP)
465                                 && arp->ar_hln == ETH_ALEN
466                                 && arp->ar_pln == 4) {
467
468                         /* We only match on the lower 8 bits of the opcode. */
469                         if (ntohs(arp->ar_op) <= 0xff)
470                                 key->ip.proto = ntohs(arp->ar_op);
471                         memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
472                         memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
473                         memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
474                         memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
475                 }
476         } else if (key->eth.type == htons(ETH_P_IPV6)) {
477                 int nh_len;             /* IPv6 Header + Extensions */
478
479                 nh_len = parse_ipv6hdr(skb, key);
480                 if (unlikely(nh_len < 0)) {
481                         if (nh_len == -EINVAL) {
482                                 skb->transport_header = skb->network_header;
483                                 error = 0;
484                         } else {
485                                 error = nh_len;
486                         }
487                         return error;
488                 }
489
490                 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
491                         return 0;
492                 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
493                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
494
495                 /* Transport layer. */
496                 if (key->ip.proto == NEXTHDR_TCP) {
497                         if (tcphdr_ok(skb)) {
498                                 struct tcphdr *tcp = tcp_hdr(skb);
499                                 key->ipv6.tp.src = tcp->source;
500                                 key->ipv6.tp.dst = tcp->dest;
501                         }
502                 } else if (key->ip.proto == NEXTHDR_UDP) {
503                         if (udphdr_ok(skb)) {
504                                 struct udphdr *udp = udp_hdr(skb);
505                                 key->ipv6.tp.src = udp->source;
506                                 key->ipv6.tp.dst = udp->dest;
507                         }
508                 } else if (key->ip.proto == NEXTHDR_SCTP) {
509                         if (sctphdr_ok(skb)) {
510                                 struct sctphdr *sctp = sctp_hdr(skb);
511                                 key->ipv6.tp.src = sctp->source;
512                                 key->ipv6.tp.dst = sctp->dest;
513                         }
514                 } else if (key->ip.proto == NEXTHDR_ICMP) {
515                         if (icmp6hdr_ok(skb)) {
516                                 error = parse_icmpv6(skb, key, nh_len);
517                                 if (error)
518                                         return error;
519                         }
520                 }
521         }
522
523         return 0;
524 }