2 * Copyright (c) 2007-2013 Nicira, Inc.
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.
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.
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
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>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.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>
45 #include <net/ndisc.h>
49 u64 ovs_flow_used_time(unsigned long flow_jiffies)
51 struct timespec cur_ts;
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;
59 return cur_ms - idle_ms;
62 #define TCP_FLAGS_OFFSET 13
63 #define TCP_FLAG_MASK 0x3f
65 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
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;
77 spin_lock(&flow->lock);
80 flow->byte_count += skb->len;
81 flow->tcp_flags |= tcp_flags;
82 spin_unlock(&flow->lock);
85 static int check_header(struct sk_buff *skb, int len)
87 if (unlikely(skb->len < len))
89 if (unlikely(!pskb_may_pull(skb, len)))
94 static bool arphdr_ok(struct sk_buff *skb)
96 return pskb_may_pull(skb, skb_network_offset(skb) +
97 sizeof(struct arp_eth_header));
100 static int check_iphdr(struct sk_buff *skb)
102 unsigned int nh_ofs = skb_network_offset(skb);
106 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
110 ip_len = ip_hdrlen(skb);
111 if (unlikely(ip_len < sizeof(struct iphdr) ||
112 skb->len < nh_ofs + ip_len))
115 skb_set_transport_header(skb, nh_ofs + ip_len);
119 static bool tcphdr_ok(struct sk_buff *skb)
121 int th_ofs = skb_transport_offset(skb);
124 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
127 tcp_len = tcp_hdrlen(skb);
128 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
129 skb->len < th_ofs + tcp_len))
135 static bool udphdr_ok(struct sk_buff *skb)
137 return pskb_may_pull(skb, skb_transport_offset(skb) +
138 sizeof(struct udphdr));
141 static bool sctphdr_ok(struct sk_buff *skb)
143 return pskb_may_pull(skb, skb_transport_offset(skb) +
144 sizeof(struct sctphdr));
147 static bool icmphdr_ok(struct sk_buff *skb)
149 return pskb_may_pull(skb, skb_transport_offset(skb) +
150 sizeof(struct icmphdr));
153 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
155 unsigned int nh_ofs = skb_network_offset(skb);
163 err = check_header(skb, nh_ofs + sizeof(*nh));
168 nexthdr = nh->nexthdr;
169 payload_ofs = (u8 *)(nh + 1) - skb->data;
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;
178 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
179 if (unlikely(payload_ofs < 0))
183 if (frag_off & htons(~0x7))
184 key->ip.frag = OVS_FRAG_TYPE_LATER;
186 key->ip.frag = OVS_FRAG_TYPE_FIRST;
189 nh_len = payload_ofs - nh_ofs;
190 skb_set_transport_header(skb, nh_ofs + nh_len);
191 key->ip.proto = nexthdr;
195 static bool icmp6hdr_ok(struct sk_buff *skb)
197 return pskb_may_pull(skb, skb_transport_offset(skb) +
198 sizeof(struct icmp6hdr));
201 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
204 __be16 eth_type; /* ETH_P_8021Q */
207 struct qtag_prefix *qp;
209 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
212 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
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));
223 static __be16 parse_ethertype(struct sk_buff *skb)
225 struct llc_snap_hdr {
226 u8 dsap; /* Always 0xAA */
227 u8 ssap; /* Always 0xAA */
232 struct llc_snap_hdr *llc;
235 proto = *(__be16 *) skb->data;
236 __skb_pull(skb, sizeof(__be16));
238 if (ntohs(proto) >= ETH_P_802_3_MIN)
241 if (skb->len < sizeof(struct llc_snap_hdr))
242 return htons(ETH_P_802_2);
244 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
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);
253 __skb_pull(skb, sizeof(struct llc_snap_hdr));
255 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
256 return llc->ethertype;
258 return htons(ETH_P_802_2);
261 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
264 struct icmp6hdr *icmp = icmp6_hdr(skb);
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.
269 key->ipv6.tp.src = htons(icmp->icmp6_type);
270 key->ipv6.tp.dst = htons(icmp->icmp6_code);
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);
279 /* In order to process neighbor discovery options, we need the
282 if (unlikely(icmp_len < sizeof(*nd)))
285 if (unlikely(skb_linearize(skb)))
288 nd = (struct nd_msg *)skb_transport_header(skb);
289 key->ipv6.nd.target = nd->target;
291 icmp_len -= sizeof(*nd);
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;
298 if (unlikely(!opt_len || opt_len > icmp_len))
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.
305 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
307 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
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
313 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
315 memcpy(key->ipv6.nd.tll,
316 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
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));
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
338 * @in_port: port number on which @skb was received.
339 * @key: output flow key
341 * The caller must ensure that skb->len >= ETH_HLEN.
343 * Returns 0 if successful, otherwise a negative errno value.
345 * Initializes @skb header pointers as follows:
347 * - skb->mac_header: the Ethernet header.
349 * - skb->network_header: just past the Ethernet header, or just past the
350 * VLAN header, to the first byte of the Ethernet payload.
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.
357 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
362 memset(key, 0, sizeof(*key));
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;
370 skb_reset_mac_header(skb);
372 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
373 * header in the linear data area.
376 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
377 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
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. */
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)))
389 key->eth.type = parse_ethertype(skb);
390 if (unlikely(key->eth.type == htons(0)))
393 skb_reset_network_header(skb);
394 __skb_push(skb, skb->data - skb_mac_header(skb));
397 if (key->eth.type == htons(ETH_P_IP)) {
401 error = check_iphdr(skb);
402 if (unlikely(error)) {
403 if (error == -EINVAL) {
404 skb->transport_header = skb->network_header;
411 key->ipv4.addr.src = nh->saddr;
412 key->ipv4.addr.dst = nh->daddr;
414 key->ip.proto = nh->protocol;
415 key->ip.tos = nh->tos;
416 key->ip.ttl = nh->ttl;
418 offset = nh->frag_off & htons(IP_OFFSET);
420 key->ip.frag = OVS_FRAG_TYPE_LATER;
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;
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;
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;
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;
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);
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;
461 arp = (struct arp_eth_header *)skb_network_header(skb);
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) {
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);
476 } else if (key->eth.type == htons(ETH_P_IPV6)) {
477 int nh_len; /* IPv6 Header + Extensions */
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;
490 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
492 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
493 key->ip.frag = OVS_FRAG_TYPE_FIRST;
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;
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;
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;
514 } else if (key->ip.proto == NEXTHDR_ICMP) {
515 if (icmp6hdr_ok(skb)) {
516 error = parse_icmpv6(skb, key, nh_len);