flow.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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/smp.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. u64 ovs_flow_used_time(unsigned long flow_jiffies)
  48. {
  49. struct timespec cur_ts;
  50. u64 cur_ms, idle_ms;
  51. ktime_get_ts(&cur_ts);
  52. idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
  53. cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
  54. cur_ts.tv_nsec / NSEC_PER_MSEC;
  55. return cur_ms - idle_ms;
  56. }
  57. #define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
  58. void ovs_flow_stats_update(struct sw_flow *flow, struct sk_buff *skb)
  59. {
  60. struct flow_stats *stats;
  61. __be16 tcp_flags = 0;
  62. if (!flow->stats.is_percpu)
  63. stats = flow->stats.stat;
  64. else
  65. stats = this_cpu_ptr(flow->stats.cpu_stats);
  66. if ((flow->key.eth.type == htons(ETH_P_IP) ||
  67. flow->key.eth.type == htons(ETH_P_IPV6)) &&
  68. flow->key.ip.frag != OVS_FRAG_TYPE_LATER &&
  69. flow->key.ip.proto == IPPROTO_TCP &&
  70. likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
  71. tcp_flags = TCP_FLAGS_BE16(tcp_hdr(skb));
  72. }
  73. spin_lock(&stats->lock);
  74. stats->used = jiffies;
  75. stats->packet_count++;
  76. stats->byte_count += skb->len;
  77. stats->tcp_flags |= tcp_flags;
  78. spin_unlock(&stats->lock);
  79. }
  80. static void stats_read(struct flow_stats *stats,
  81. struct ovs_flow_stats *ovs_stats,
  82. unsigned long *used, __be16 *tcp_flags)
  83. {
  84. spin_lock(&stats->lock);
  85. if (!*used || time_after(stats->used, *used))
  86. *used = stats->used;
  87. *tcp_flags |= stats->tcp_flags;
  88. ovs_stats->n_packets += stats->packet_count;
  89. ovs_stats->n_bytes += stats->byte_count;
  90. spin_unlock(&stats->lock);
  91. }
  92. void ovs_flow_stats_get(struct sw_flow *flow, struct ovs_flow_stats *ovs_stats,
  93. unsigned long *used, __be16 *tcp_flags)
  94. {
  95. int cpu;
  96. *used = 0;
  97. *tcp_flags = 0;
  98. memset(ovs_stats, 0, sizeof(*ovs_stats));
  99. local_bh_disable();
  100. if (!flow->stats.is_percpu) {
  101. stats_read(flow->stats.stat, ovs_stats, used, tcp_flags);
  102. } else {
  103. for_each_possible_cpu(cpu) {
  104. struct flow_stats *stats;
  105. stats = per_cpu_ptr(flow->stats.cpu_stats, cpu);
  106. stats_read(stats, ovs_stats, used, tcp_flags);
  107. }
  108. }
  109. local_bh_enable();
  110. }
  111. static void stats_reset(struct flow_stats *stats)
  112. {
  113. spin_lock(&stats->lock);
  114. stats->used = 0;
  115. stats->packet_count = 0;
  116. stats->byte_count = 0;
  117. stats->tcp_flags = 0;
  118. spin_unlock(&stats->lock);
  119. }
  120. void ovs_flow_stats_clear(struct sw_flow *flow)
  121. {
  122. int cpu;
  123. local_bh_disable();
  124. if (!flow->stats.is_percpu) {
  125. stats_reset(flow->stats.stat);
  126. } else {
  127. for_each_possible_cpu(cpu) {
  128. stats_reset(per_cpu_ptr(flow->stats.cpu_stats, cpu));
  129. }
  130. }
  131. local_bh_enable();
  132. }
  133. static int check_header(struct sk_buff *skb, int len)
  134. {
  135. if (unlikely(skb->len < len))
  136. return -EINVAL;
  137. if (unlikely(!pskb_may_pull(skb, len)))
  138. return -ENOMEM;
  139. return 0;
  140. }
  141. static bool arphdr_ok(struct sk_buff *skb)
  142. {
  143. return pskb_may_pull(skb, skb_network_offset(skb) +
  144. sizeof(struct arp_eth_header));
  145. }
  146. static int check_iphdr(struct sk_buff *skb)
  147. {
  148. unsigned int nh_ofs = skb_network_offset(skb);
  149. unsigned int ip_len;
  150. int err;
  151. err = check_header(skb, nh_ofs + sizeof(struct iphdr));
  152. if (unlikely(err))
  153. return err;
  154. ip_len = ip_hdrlen(skb);
  155. if (unlikely(ip_len < sizeof(struct iphdr) ||
  156. skb->len < nh_ofs + ip_len))
  157. return -EINVAL;
  158. skb_set_transport_header(skb, nh_ofs + ip_len);
  159. return 0;
  160. }
  161. static bool tcphdr_ok(struct sk_buff *skb)
  162. {
  163. int th_ofs = skb_transport_offset(skb);
  164. int tcp_len;
  165. if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
  166. return false;
  167. tcp_len = tcp_hdrlen(skb);
  168. if (unlikely(tcp_len < sizeof(struct tcphdr) ||
  169. skb->len < th_ofs + tcp_len))
  170. return false;
  171. return true;
  172. }
  173. static bool udphdr_ok(struct sk_buff *skb)
  174. {
  175. return pskb_may_pull(skb, skb_transport_offset(skb) +
  176. sizeof(struct udphdr));
  177. }
  178. static bool sctphdr_ok(struct sk_buff *skb)
  179. {
  180. return pskb_may_pull(skb, skb_transport_offset(skb) +
  181. sizeof(struct sctphdr));
  182. }
  183. static bool icmphdr_ok(struct sk_buff *skb)
  184. {
  185. return pskb_may_pull(skb, skb_transport_offset(skb) +
  186. sizeof(struct icmphdr));
  187. }
  188. static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
  189. {
  190. unsigned int nh_ofs = skb_network_offset(skb);
  191. unsigned int nh_len;
  192. int payload_ofs;
  193. struct ipv6hdr *nh;
  194. uint8_t nexthdr;
  195. __be16 frag_off;
  196. int err;
  197. err = check_header(skb, nh_ofs + sizeof(*nh));
  198. if (unlikely(err))
  199. return err;
  200. nh = ipv6_hdr(skb);
  201. nexthdr = nh->nexthdr;
  202. payload_ofs = (u8 *)(nh + 1) - skb->data;
  203. key->ip.proto = NEXTHDR_NONE;
  204. key->ip.tos = ipv6_get_dsfield(nh);
  205. key->ip.ttl = nh->hop_limit;
  206. key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
  207. key->ipv6.addr.src = nh->saddr;
  208. key->ipv6.addr.dst = nh->daddr;
  209. payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
  210. if (unlikely(payload_ofs < 0))
  211. return -EINVAL;
  212. if (frag_off) {
  213. if (frag_off & htons(~0x7))
  214. key->ip.frag = OVS_FRAG_TYPE_LATER;
  215. else
  216. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  217. }
  218. nh_len = payload_ofs - nh_ofs;
  219. skb_set_transport_header(skb, nh_ofs + nh_len);
  220. key->ip.proto = nexthdr;
  221. return nh_len;
  222. }
  223. static bool icmp6hdr_ok(struct sk_buff *skb)
  224. {
  225. return pskb_may_pull(skb, skb_transport_offset(skb) +
  226. sizeof(struct icmp6hdr));
  227. }
  228. static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  229. {
  230. struct qtag_prefix {
  231. __be16 eth_type; /* ETH_P_8021Q */
  232. __be16 tci;
  233. };
  234. struct qtag_prefix *qp;
  235. if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
  236. return 0;
  237. if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
  238. sizeof(__be16))))
  239. return -ENOMEM;
  240. qp = (struct qtag_prefix *) skb->data;
  241. key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
  242. __skb_pull(skb, sizeof(struct qtag_prefix));
  243. return 0;
  244. }
  245. static __be16 parse_ethertype(struct sk_buff *skb)
  246. {
  247. struct llc_snap_hdr {
  248. u8 dsap; /* Always 0xAA */
  249. u8 ssap; /* Always 0xAA */
  250. u8 ctrl;
  251. u8 oui[3];
  252. __be16 ethertype;
  253. };
  254. struct llc_snap_hdr *llc;
  255. __be16 proto;
  256. proto = *(__be16 *) skb->data;
  257. __skb_pull(skb, sizeof(__be16));
  258. if (ntohs(proto) >= ETH_P_802_3_MIN)
  259. return proto;
  260. if (skb->len < sizeof(struct llc_snap_hdr))
  261. return htons(ETH_P_802_2);
  262. if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
  263. return htons(0);
  264. llc = (struct llc_snap_hdr *) skb->data;
  265. if (llc->dsap != LLC_SAP_SNAP ||
  266. llc->ssap != LLC_SAP_SNAP ||
  267. (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
  268. return htons(ETH_P_802_2);
  269. __skb_pull(skb, sizeof(struct llc_snap_hdr));
  270. if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
  271. return llc->ethertype;
  272. return htons(ETH_P_802_2);
  273. }
  274. static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
  275. int nh_len)
  276. {
  277. struct icmp6hdr *icmp = icmp6_hdr(skb);
  278. /* The ICMPv6 type and code fields use the 16-bit transport port
  279. * fields, so we need to store them in 16-bit network byte order.
  280. */
  281. key->ipv6.tp.src = htons(icmp->icmp6_type);
  282. key->ipv6.tp.dst = htons(icmp->icmp6_code);
  283. if (icmp->icmp6_code == 0 &&
  284. (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  285. icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  286. int icmp_len = skb->len - skb_transport_offset(skb);
  287. struct nd_msg *nd;
  288. int offset;
  289. /* In order to process neighbor discovery options, we need the
  290. * entire packet.
  291. */
  292. if (unlikely(icmp_len < sizeof(*nd)))
  293. return 0;
  294. if (unlikely(skb_linearize(skb)))
  295. return -ENOMEM;
  296. nd = (struct nd_msg *)skb_transport_header(skb);
  297. key->ipv6.nd.target = nd->target;
  298. icmp_len -= sizeof(*nd);
  299. offset = 0;
  300. while (icmp_len >= 8) {
  301. struct nd_opt_hdr *nd_opt =
  302. (struct nd_opt_hdr *)(nd->opt + offset);
  303. int opt_len = nd_opt->nd_opt_len * 8;
  304. if (unlikely(!opt_len || opt_len > icmp_len))
  305. return 0;
  306. /* Store the link layer address if the appropriate
  307. * option is provided. It is considered an error if
  308. * the same link layer option is specified twice.
  309. */
  310. if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
  311. && opt_len == 8) {
  312. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
  313. goto invalid;
  314. memcpy(key->ipv6.nd.sll,
  315. &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
  316. } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
  317. && opt_len == 8) {
  318. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
  319. goto invalid;
  320. memcpy(key->ipv6.nd.tll,
  321. &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
  322. }
  323. icmp_len -= opt_len;
  324. offset += opt_len;
  325. }
  326. }
  327. return 0;
  328. invalid:
  329. memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
  330. memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
  331. memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
  332. return 0;
  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. memset(key, 0, sizeof(*key));
  362. key->phy.priority = skb->priority;
  363. if (OVS_CB(skb)->tun_key)
  364. memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
  365. key->phy.in_port = in_port;
  366. key->phy.skb_mark = skb->mark;
  367. skb_reset_mac_header(skb);
  368. /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
  369. * header in the linear data area.
  370. */
  371. eth = eth_hdr(skb);
  372. memcpy(key->eth.src, eth->h_source, ETH_ALEN);
  373. memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
  374. __skb_pull(skb, 2 * ETH_ALEN);
  375. /* We are going to push all headers that we pull, so no need to
  376. * update skb->csum here.
  377. */
  378. if (vlan_tx_tag_present(skb))
  379. key->eth.tci = htons(skb->vlan_tci);
  380. else if (eth->h_proto == htons(ETH_P_8021Q))
  381. if (unlikely(parse_vlan(skb, key)))
  382. return -ENOMEM;
  383. key->eth.type = parse_ethertype(skb);
  384. if (unlikely(key->eth.type == htons(0)))
  385. return -ENOMEM;
  386. skb_reset_network_header(skb);
  387. __skb_push(skb, skb->data - skb_mac_header(skb));
  388. /* Network layer. */
  389. if (key->eth.type == htons(ETH_P_IP)) {
  390. struct iphdr *nh;
  391. __be16 offset;
  392. error = check_iphdr(skb);
  393. if (unlikely(error)) {
  394. if (error == -EINVAL) {
  395. skb->transport_header = skb->network_header;
  396. error = 0;
  397. }
  398. return error;
  399. }
  400. nh = ip_hdr(skb);
  401. key->ipv4.addr.src = nh->saddr;
  402. key->ipv4.addr.dst = nh->daddr;
  403. key->ip.proto = nh->protocol;
  404. key->ip.tos = nh->tos;
  405. key->ip.ttl = nh->ttl;
  406. offset = nh->frag_off & htons(IP_OFFSET);
  407. if (offset) {
  408. key->ip.frag = OVS_FRAG_TYPE_LATER;
  409. return 0;
  410. }
  411. if (nh->frag_off & htons(IP_MF) ||
  412. skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  413. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  414. /* Transport layer. */
  415. if (key->ip.proto == IPPROTO_TCP) {
  416. if (tcphdr_ok(skb)) {
  417. struct tcphdr *tcp = tcp_hdr(skb);
  418. key->ipv4.tp.src = tcp->source;
  419. key->ipv4.tp.dst = tcp->dest;
  420. key->ipv4.tp.flags = TCP_FLAGS_BE16(tcp);
  421. }
  422. } else if (key->ip.proto == IPPROTO_UDP) {
  423. if (udphdr_ok(skb)) {
  424. struct udphdr *udp = udp_hdr(skb);
  425. key->ipv4.tp.src = udp->source;
  426. key->ipv4.tp.dst = udp->dest;
  427. }
  428. } else if (key->ip.proto == IPPROTO_SCTP) {
  429. if (sctphdr_ok(skb)) {
  430. struct sctphdr *sctp = sctp_hdr(skb);
  431. key->ipv4.tp.src = sctp->source;
  432. key->ipv4.tp.dst = sctp->dest;
  433. }
  434. } else if (key->ip.proto == IPPROTO_ICMP) {
  435. if (icmphdr_ok(skb)) {
  436. struct icmphdr *icmp = icmp_hdr(skb);
  437. /* The ICMP type and code fields use the 16-bit
  438. * transport port fields, so we need to store
  439. * them in 16-bit network byte order. */
  440. key->ipv4.tp.src = htons(icmp->type);
  441. key->ipv4.tp.dst = htons(icmp->code);
  442. }
  443. }
  444. } else if ((key->eth.type == htons(ETH_P_ARP) ||
  445. key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
  446. struct arp_eth_header *arp;
  447. arp = (struct arp_eth_header *)skb_network_header(skb);
  448. if (arp->ar_hrd == htons(ARPHRD_ETHER)
  449. && arp->ar_pro == htons(ETH_P_IP)
  450. && arp->ar_hln == ETH_ALEN
  451. && arp->ar_pln == 4) {
  452. /* We only match on the lower 8 bits of the opcode. */
  453. if (ntohs(arp->ar_op) <= 0xff)
  454. key->ip.proto = ntohs(arp->ar_op);
  455. memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
  456. memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
  457. memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
  458. memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
  459. }
  460. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  461. int nh_len; /* IPv6 Header + Extensions */
  462. nh_len = parse_ipv6hdr(skb, key);
  463. if (unlikely(nh_len < 0)) {
  464. if (nh_len == -EINVAL) {
  465. skb->transport_header = skb->network_header;
  466. error = 0;
  467. } else {
  468. error = nh_len;
  469. }
  470. return error;
  471. }
  472. if (key->ip.frag == OVS_FRAG_TYPE_LATER)
  473. return 0;
  474. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  475. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  476. /* Transport layer. */
  477. if (key->ip.proto == NEXTHDR_TCP) {
  478. if (tcphdr_ok(skb)) {
  479. struct tcphdr *tcp = tcp_hdr(skb);
  480. key->ipv6.tp.src = tcp->source;
  481. key->ipv6.tp.dst = tcp->dest;
  482. key->ipv6.tp.flags = TCP_FLAGS_BE16(tcp);
  483. }
  484. } else if (key->ip.proto == NEXTHDR_UDP) {
  485. if (udphdr_ok(skb)) {
  486. struct udphdr *udp = udp_hdr(skb);
  487. key->ipv6.tp.src = udp->source;
  488. key->ipv6.tp.dst = udp->dest;
  489. }
  490. } else if (key->ip.proto == NEXTHDR_SCTP) {
  491. if (sctphdr_ok(skb)) {
  492. struct sctphdr *sctp = sctp_hdr(skb);
  493. key->ipv6.tp.src = sctp->source;
  494. key->ipv6.tp.dst = sctp->dest;
  495. }
  496. } else if (key->ip.proto == NEXTHDR_ICMP) {
  497. if (icmp6hdr_ok(skb)) {
  498. error = parse_icmpv6(skb, key, nh_len);
  499. if (error)
  500. return error;
  501. }
  502. }
  503. }
  504. return 0;
  505. }