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/ip_tunnels.h> 45 #include <net/ipv6.h> 46 #include <net/ndisc.h> 47 48 u64 ovs_flow_used_time(unsigned long flow_jiffies) 49 { 50 struct timespec cur_ts; 51 u64 cur_ms, idle_ms; 52 53 ktime_get_ts(&cur_ts); 54 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies); 55 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC + 56 cur_ts.tv_nsec / NSEC_PER_MSEC; 57 58 return cur_ms - idle_ms; 59 } 60 61 #define TCP_FLAGS_OFFSET 13 62 #define TCP_FLAG_MASK 0x3f 63 64 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb) 65 { 66 u8 tcp_flags = 0; 67 68 if ((flow->key.eth.type == htons(ETH_P_IP) || 69 flow->key.eth.type == htons(ETH_P_IPV6)) && 70 flow->key.ip.proto == IPPROTO_TCP && 71 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) { 72 u8 *tcp = (u8 *)tcp_hdr(skb); 73 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK; 74 } 75 76 spin_lock(&flow->lock); 77 flow->used = jiffies; 78 flow->packet_count++; 79 flow->byte_count += skb->len; 80 flow->tcp_flags |= tcp_flags; 81 spin_unlock(&flow->lock); 82 } 83 84 static int check_header(struct sk_buff *skb, int len) 85 { 86 if (unlikely(skb->len < len)) 87 return -EINVAL; 88 if (unlikely(!pskb_may_pull(skb, len))) 89 return -ENOMEM; 90 return 0; 91 } 92 93 static bool arphdr_ok(struct sk_buff *skb) 94 { 95 return pskb_may_pull(skb, skb_network_offset(skb) + 96 sizeof(struct arp_eth_header)); 97 } 98 99 static int check_iphdr(struct sk_buff *skb) 100 { 101 unsigned int nh_ofs = skb_network_offset(skb); 102 unsigned int ip_len; 103 int err; 104 105 err = check_header(skb, nh_ofs + sizeof(struct iphdr)); 106 if (unlikely(err)) 107 return err; 108 109 ip_len = ip_hdrlen(skb); 110 if (unlikely(ip_len < sizeof(struct iphdr) || 111 skb->len < nh_ofs + ip_len)) 112 return -EINVAL; 113 114 skb_set_transport_header(skb, nh_ofs + ip_len); 115 return 0; 116 } 117 118 static bool tcphdr_ok(struct sk_buff *skb) 119 { 120 int th_ofs = skb_transport_offset(skb); 121 int tcp_len; 122 123 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr)))) 124 return false; 125 126 tcp_len = tcp_hdrlen(skb); 127 if (unlikely(tcp_len < sizeof(struct tcphdr) || 128 skb->len < th_ofs + tcp_len)) 129 return false; 130 131 return true; 132 } 133 134 static bool udphdr_ok(struct sk_buff *skb) 135 { 136 return pskb_may_pull(skb, skb_transport_offset(skb) + 137 sizeof(struct udphdr)); 138 } 139 140 static bool sctphdr_ok(struct sk_buff *skb) 141 { 142 return pskb_may_pull(skb, skb_transport_offset(skb) + 143 sizeof(struct sctphdr)); 144 } 145 146 static bool icmphdr_ok(struct sk_buff *skb) 147 { 148 return pskb_may_pull(skb, skb_transport_offset(skb) + 149 sizeof(struct icmphdr)); 150 } 151 152 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key) 153 { 154 unsigned int nh_ofs = skb_network_offset(skb); 155 unsigned int nh_len; 156 int payload_ofs; 157 struct ipv6hdr *nh; 158 uint8_t nexthdr; 159 __be16 frag_off; 160 int err; 161 162 err = check_header(skb, nh_ofs + sizeof(*nh)); 163 if (unlikely(err)) 164 return err; 165 166 nh = ipv6_hdr(skb); 167 nexthdr = nh->nexthdr; 168 payload_ofs = (u8 *)(nh + 1) - skb->data; 169 170 key->ip.proto = NEXTHDR_NONE; 171 key->ip.tos = ipv6_get_dsfield(nh); 172 key->ip.ttl = nh->hop_limit; 173 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL); 174 key->ipv6.addr.src = nh->saddr; 175 key->ipv6.addr.dst = nh->daddr; 176 177 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off); 178 if (unlikely(payload_ofs < 0)) 179 return -EINVAL; 180 181 if (frag_off) { 182 if (frag_off & htons(~0x7)) 183 key->ip.frag = OVS_FRAG_TYPE_LATER; 184 else 185 key->ip.frag = OVS_FRAG_TYPE_FIRST; 186 } 187 188 nh_len = payload_ofs - nh_ofs; 189 skb_set_transport_header(skb, nh_ofs + nh_len); 190 key->ip.proto = nexthdr; 191 return nh_len; 192 } 193 194 static bool icmp6hdr_ok(struct sk_buff *skb) 195 { 196 return pskb_may_pull(skb, skb_transport_offset(skb) + 197 sizeof(struct icmp6hdr)); 198 } 199 200 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key) 201 { 202 struct qtag_prefix { 203 __be16 eth_type; /* ETH_P_8021Q */ 204 __be16 tci; 205 }; 206 struct qtag_prefix *qp; 207 208 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16))) 209 return 0; 210 211 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) + 212 sizeof(__be16)))) 213 return -ENOMEM; 214 215 qp = (struct qtag_prefix *) skb->data; 216 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT); 217 __skb_pull(skb, sizeof(struct qtag_prefix)); 218 219 return 0; 220 } 221 222 static __be16 parse_ethertype(struct sk_buff *skb) 223 { 224 struct llc_snap_hdr { 225 u8 dsap; /* Always 0xAA */ 226 u8 ssap; /* Always 0xAA */ 227 u8 ctrl; 228 u8 oui[3]; 229 __be16 ethertype; 230 }; 231 struct llc_snap_hdr *llc; 232 __be16 proto; 233 234 proto = *(__be16 *) skb->data; 235 __skb_pull(skb, sizeof(__be16)); 236 237 if (ntohs(proto) >= ETH_P_802_3_MIN) 238 return proto; 239 240 if (skb->len < sizeof(struct llc_snap_hdr)) 241 return htons(ETH_P_802_2); 242 243 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr)))) 244 return htons(0); 245 246 llc = (struct llc_snap_hdr *) skb->data; 247 if (llc->dsap != LLC_SAP_SNAP || 248 llc->ssap != LLC_SAP_SNAP || 249 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0) 250 return htons(ETH_P_802_2); 251 252 __skb_pull(skb, sizeof(struct llc_snap_hdr)); 253 254 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN) 255 return llc->ethertype; 256 257 return htons(ETH_P_802_2); 258 } 259 260 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key, 261 int nh_len) 262 { 263 struct icmp6hdr *icmp = icmp6_hdr(skb); 264 265 /* The ICMPv6 type and code fields use the 16-bit transport port 266 * fields, so we need to store them in 16-bit network byte order. 267 */ 268 key->ipv6.tp.src = htons(icmp->icmp6_type); 269 key->ipv6.tp.dst = htons(icmp->icmp6_code); 270 271 if (icmp->icmp6_code == 0 && 272 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION || 273 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) { 274 int icmp_len = skb->len - skb_transport_offset(skb); 275 struct nd_msg *nd; 276 int offset; 277 278 /* In order to process neighbor discovery options, we need the 279 * entire packet. 280 */ 281 if (unlikely(icmp_len < sizeof(*nd))) 282 return 0; 283 284 if (unlikely(skb_linearize(skb))) 285 return -ENOMEM; 286 287 nd = (struct nd_msg *)skb_transport_header(skb); 288 key->ipv6.nd.target = nd->target; 289 290 icmp_len -= sizeof(*nd); 291 offset = 0; 292 while (icmp_len >= 8) { 293 struct nd_opt_hdr *nd_opt = 294 (struct nd_opt_hdr *)(nd->opt + offset); 295 int opt_len = nd_opt->nd_opt_len * 8; 296 297 if (unlikely(!opt_len || opt_len > icmp_len)) 298 return 0; 299 300 /* Store the link layer address if the appropriate 301 * option is provided. It is considered an error if 302 * the same link layer option is specified twice. 303 */ 304 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR 305 && opt_len == 8) { 306 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll))) 307 goto invalid; 308 memcpy(key->ipv6.nd.sll, 309 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN); 310 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR 311 && opt_len == 8) { 312 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll))) 313 goto invalid; 314 memcpy(key->ipv6.nd.tll, 315 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN); 316 } 317 318 icmp_len -= opt_len; 319 offset += opt_len; 320 } 321 } 322 323 return 0; 324 325 invalid: 326 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target)); 327 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll)); 328 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll)); 329 330 return 0; 331 } 332 333 /** 334 * ovs_flow_extract - extracts a flow key from an Ethernet frame. 335 * @skb: sk_buff that contains the frame, with skb->data pointing to the 336 * Ethernet header 337 * @in_port: port number on which @skb was received. 338 * @key: output flow key 339 * 340 * The caller must ensure that skb->len >= ETH_HLEN. 341 * 342 * Returns 0 if successful, otherwise a negative errno value. 343 * 344 * Initializes @skb header pointers as follows: 345 * 346 * - skb->mac_header: the Ethernet header. 347 * 348 * - skb->network_header: just past the Ethernet header, or just past the 349 * VLAN header, to the first byte of the Ethernet payload. 350 * 351 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6 352 * on output, then just past the IP header, if one is present and 353 * of a correct length, otherwise the same as skb->network_header. 354 * For other key->eth.type values it is left untouched. 355 */ 356 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key) 357 { 358 int error; 359 struct ethhdr *eth; 360 361 memset(key, 0, sizeof(*key)); 362 363 key->phy.priority = skb->priority; 364 if (OVS_CB(skb)->tun_key) 365 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key)); 366 key->phy.in_port = in_port; 367 key->phy.skb_mark = skb->mark; 368 369 skb_reset_mac_header(skb); 370 371 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet 372 * header in the linear data area. 373 */ 374 eth = eth_hdr(skb); 375 memcpy(key->eth.src, eth->h_source, ETH_ALEN); 376 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN); 377 378 __skb_pull(skb, 2 * ETH_ALEN); 379 /* We are going to push all headers that we pull, so no need to 380 * update skb->csum here. 381 */ 382 383 if (vlan_tx_tag_present(skb)) 384 key->eth.tci = htons(skb->vlan_tci); 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 } 525