flow.c 14 KB

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