flow.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /*
  2. * Copyright (c) 2007-2014 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 <linux/uaccess.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/if_ether.h>
  22. #include <linux/if_vlan.h>
  23. #include <net/llc_pdu.h>
  24. #include <linux/kernel.h>
  25. #include <linux/jhash.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/llc.h>
  28. #include <linux/module.h>
  29. #include <linux/in.h>
  30. #include <linux/rcupdate.h>
  31. #include <linux/cpumask.h>
  32. #include <linux/if_arp.h>
  33. #include <linux/ip.h>
  34. #include <linux/ipv6.h>
  35. #include <linux/mpls.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/mpls.h>
  47. #include <net/ndisc.h>
  48. #include "conntrack.h"
  49. #include "datapath.h"
  50. #include "flow.h"
  51. #include "flow_netlink.h"
  52. #include "vport.h"
  53. u64 ovs_flow_used_time(unsigned long flow_jiffies)
  54. {
  55. struct timespec cur_ts;
  56. u64 cur_ms, idle_ms;
  57. ktime_get_ts(&cur_ts);
  58. idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
  59. cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
  60. cur_ts.tv_nsec / NSEC_PER_MSEC;
  61. return cur_ms - idle_ms;
  62. }
  63. #define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
  64. void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
  65. const struct sk_buff *skb)
  66. {
  67. struct flow_stats *stats;
  68. int node = numa_node_id();
  69. int cpu = smp_processor_id();
  70. int len = skb->len + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
  71. stats = rcu_dereference(flow->stats[cpu]);
  72. /* Check if already have CPU-specific stats. */
  73. if (likely(stats)) {
  74. spin_lock(&stats->lock);
  75. /* Mark if we write on the pre-allocated stats. */
  76. if (cpu == 0 && unlikely(flow->stats_last_writer != cpu))
  77. flow->stats_last_writer = cpu;
  78. } else {
  79. stats = rcu_dereference(flow->stats[0]); /* Pre-allocated. */
  80. spin_lock(&stats->lock);
  81. /* If the current CPU is the only writer on the
  82. * pre-allocated stats keep using them.
  83. */
  84. if (unlikely(flow->stats_last_writer != cpu)) {
  85. /* A previous locker may have already allocated the
  86. * stats, so we need to check again. If CPU-specific
  87. * stats were already allocated, we update the pre-
  88. * allocated stats as we have already locked them.
  89. */
  90. if (likely(flow->stats_last_writer != -1) &&
  91. likely(!rcu_access_pointer(flow->stats[cpu]))) {
  92. /* Try to allocate CPU-specific stats. */
  93. struct flow_stats *new_stats;
  94. new_stats =
  95. kmem_cache_alloc_node(flow_stats_cache,
  96. GFP_NOWAIT |
  97. __GFP_THISNODE |
  98. __GFP_NOWARN |
  99. __GFP_NOMEMALLOC,
  100. node);
  101. if (likely(new_stats)) {
  102. new_stats->used = jiffies;
  103. new_stats->packet_count = 1;
  104. new_stats->byte_count = len;
  105. new_stats->tcp_flags = tcp_flags;
  106. spin_lock_init(&new_stats->lock);
  107. rcu_assign_pointer(flow->stats[cpu],
  108. new_stats);
  109. goto unlock;
  110. }
  111. }
  112. flow->stats_last_writer = cpu;
  113. }
  114. }
  115. stats->used = jiffies;
  116. stats->packet_count++;
  117. stats->byte_count += len;
  118. stats->tcp_flags |= tcp_flags;
  119. unlock:
  120. spin_unlock(&stats->lock);
  121. }
  122. /* Must be called with rcu_read_lock or ovs_mutex. */
  123. void ovs_flow_stats_get(const struct sw_flow *flow,
  124. struct ovs_flow_stats *ovs_stats,
  125. unsigned long *used, __be16 *tcp_flags)
  126. {
  127. int cpu;
  128. *used = 0;
  129. *tcp_flags = 0;
  130. memset(ovs_stats, 0, sizeof(*ovs_stats));
  131. /* We open code this to make sure cpu 0 is always considered */
  132. for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpu_possible_mask)) {
  133. struct flow_stats *stats = rcu_dereference_ovsl(flow->stats[cpu]);
  134. if (stats) {
  135. /* Local CPU may write on non-local stats, so we must
  136. * block bottom-halves here.
  137. */
  138. spin_lock_bh(&stats->lock);
  139. if (!*used || time_after(stats->used, *used))
  140. *used = stats->used;
  141. *tcp_flags |= stats->tcp_flags;
  142. ovs_stats->n_packets += stats->packet_count;
  143. ovs_stats->n_bytes += stats->byte_count;
  144. spin_unlock_bh(&stats->lock);
  145. }
  146. }
  147. }
  148. /* Called with ovs_mutex. */
  149. void ovs_flow_stats_clear(struct sw_flow *flow)
  150. {
  151. int cpu;
  152. /* We open code this to make sure cpu 0 is always considered */
  153. for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpu_possible_mask)) {
  154. struct flow_stats *stats = ovsl_dereference(flow->stats[cpu]);
  155. if (stats) {
  156. spin_lock_bh(&stats->lock);
  157. stats->used = 0;
  158. stats->packet_count = 0;
  159. stats->byte_count = 0;
  160. stats->tcp_flags = 0;
  161. spin_unlock_bh(&stats->lock);
  162. }
  163. }
  164. }
  165. static int check_header(struct sk_buff *skb, int len)
  166. {
  167. if (unlikely(skb->len < len))
  168. return -EINVAL;
  169. if (unlikely(!pskb_may_pull(skb, len)))
  170. return -ENOMEM;
  171. return 0;
  172. }
  173. static bool arphdr_ok(struct sk_buff *skb)
  174. {
  175. return pskb_may_pull(skb, skb_network_offset(skb) +
  176. sizeof(struct arp_eth_header));
  177. }
  178. static int check_iphdr(struct sk_buff *skb)
  179. {
  180. unsigned int nh_ofs = skb_network_offset(skb);
  181. unsigned int ip_len;
  182. int err;
  183. err = check_header(skb, nh_ofs + sizeof(struct iphdr));
  184. if (unlikely(err))
  185. return err;
  186. ip_len = ip_hdrlen(skb);
  187. if (unlikely(ip_len < sizeof(struct iphdr) ||
  188. skb->len < nh_ofs + ip_len))
  189. return -EINVAL;
  190. skb_set_transport_header(skb, nh_ofs + ip_len);
  191. return 0;
  192. }
  193. static bool tcphdr_ok(struct sk_buff *skb)
  194. {
  195. int th_ofs = skb_transport_offset(skb);
  196. int tcp_len;
  197. if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
  198. return false;
  199. tcp_len = tcp_hdrlen(skb);
  200. if (unlikely(tcp_len < sizeof(struct tcphdr) ||
  201. skb->len < th_ofs + tcp_len))
  202. return false;
  203. return true;
  204. }
  205. static bool udphdr_ok(struct sk_buff *skb)
  206. {
  207. return pskb_may_pull(skb, skb_transport_offset(skb) +
  208. sizeof(struct udphdr));
  209. }
  210. static bool sctphdr_ok(struct sk_buff *skb)
  211. {
  212. return pskb_may_pull(skb, skb_transport_offset(skb) +
  213. sizeof(struct sctphdr));
  214. }
  215. static bool icmphdr_ok(struct sk_buff *skb)
  216. {
  217. return pskb_may_pull(skb, skb_transport_offset(skb) +
  218. sizeof(struct icmphdr));
  219. }
  220. static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
  221. {
  222. unsigned int nh_ofs = skb_network_offset(skb);
  223. unsigned int nh_len;
  224. int payload_ofs;
  225. struct ipv6hdr *nh;
  226. uint8_t nexthdr;
  227. __be16 frag_off;
  228. int err;
  229. err = check_header(skb, nh_ofs + sizeof(*nh));
  230. if (unlikely(err))
  231. return err;
  232. nh = ipv6_hdr(skb);
  233. nexthdr = nh->nexthdr;
  234. payload_ofs = (u8 *)(nh + 1) - skb->data;
  235. key->ip.proto = NEXTHDR_NONE;
  236. key->ip.tos = ipv6_get_dsfield(nh);
  237. key->ip.ttl = nh->hop_limit;
  238. key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
  239. key->ipv6.addr.src = nh->saddr;
  240. key->ipv6.addr.dst = nh->daddr;
  241. payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
  242. if (frag_off) {
  243. if (frag_off & htons(~0x7))
  244. key->ip.frag = OVS_FRAG_TYPE_LATER;
  245. else
  246. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  247. } else {
  248. key->ip.frag = OVS_FRAG_TYPE_NONE;
  249. }
  250. /* Delayed handling of error in ipv6_skip_exthdr() as it
  251. * always sets frag_off to a valid value which may be
  252. * used to set key->ip.frag above.
  253. */
  254. if (unlikely(payload_ofs < 0))
  255. return -EPROTO;
  256. nh_len = payload_ofs - nh_ofs;
  257. skb_set_transport_header(skb, nh_ofs + nh_len);
  258. key->ip.proto = nexthdr;
  259. return nh_len;
  260. }
  261. static bool icmp6hdr_ok(struct sk_buff *skb)
  262. {
  263. return pskb_may_pull(skb, skb_transport_offset(skb) +
  264. sizeof(struct icmp6hdr));
  265. }
  266. /**
  267. * Parse vlan tag from vlan header.
  268. * Returns ERROR on memory error.
  269. * Returns 0 if it encounters a non-vlan or incomplete packet.
  270. * Returns 1 after successfully parsing vlan tag.
  271. */
  272. static int parse_vlan_tag(struct sk_buff *skb, struct vlan_head *key_vh)
  273. {
  274. struct vlan_head *vh = (struct vlan_head *)skb->data;
  275. if (likely(!eth_type_vlan(vh->tpid)))
  276. return 0;
  277. if (unlikely(skb->len < sizeof(struct vlan_head) + sizeof(__be16)))
  278. return 0;
  279. if (unlikely(!pskb_may_pull(skb, sizeof(struct vlan_head) +
  280. sizeof(__be16))))
  281. return -ENOMEM;
  282. vh = (struct vlan_head *)skb->data;
  283. key_vh->tci = vh->tci | htons(VLAN_TAG_PRESENT);
  284. key_vh->tpid = vh->tpid;
  285. __skb_pull(skb, sizeof(struct vlan_head));
  286. return 1;
  287. }
  288. static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  289. {
  290. int res;
  291. key->eth.vlan.tci = 0;
  292. key->eth.vlan.tpid = 0;
  293. key->eth.cvlan.tci = 0;
  294. key->eth.cvlan.tpid = 0;
  295. if (skb_vlan_tag_present(skb)) {
  296. key->eth.vlan.tci = htons(skb->vlan_tci);
  297. key->eth.vlan.tpid = skb->vlan_proto;
  298. } else {
  299. /* Parse outer vlan tag in the non-accelerated case. */
  300. res = parse_vlan_tag(skb, &key->eth.vlan);
  301. if (res <= 0)
  302. return res;
  303. }
  304. /* Parse inner vlan tag. */
  305. res = parse_vlan_tag(skb, &key->eth.cvlan);
  306. if (res <= 0)
  307. return res;
  308. return 0;
  309. }
  310. static __be16 parse_ethertype(struct sk_buff *skb)
  311. {
  312. struct llc_snap_hdr {
  313. u8 dsap; /* Always 0xAA */
  314. u8 ssap; /* Always 0xAA */
  315. u8 ctrl;
  316. u8 oui[3];
  317. __be16 ethertype;
  318. };
  319. struct llc_snap_hdr *llc;
  320. __be16 proto;
  321. proto = *(__be16 *) skb->data;
  322. __skb_pull(skb, sizeof(__be16));
  323. if (eth_proto_is_802_3(proto))
  324. return proto;
  325. if (skb->len < sizeof(struct llc_snap_hdr))
  326. return htons(ETH_P_802_2);
  327. if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
  328. return htons(0);
  329. llc = (struct llc_snap_hdr *) skb->data;
  330. if (llc->dsap != LLC_SAP_SNAP ||
  331. llc->ssap != LLC_SAP_SNAP ||
  332. (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
  333. return htons(ETH_P_802_2);
  334. __skb_pull(skb, sizeof(struct llc_snap_hdr));
  335. if (eth_proto_is_802_3(llc->ethertype))
  336. return llc->ethertype;
  337. return htons(ETH_P_802_2);
  338. }
  339. static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
  340. int nh_len)
  341. {
  342. struct icmp6hdr *icmp = icmp6_hdr(skb);
  343. /* The ICMPv6 type and code fields use the 16-bit transport port
  344. * fields, so we need to store them in 16-bit network byte order.
  345. */
  346. key->tp.src = htons(icmp->icmp6_type);
  347. key->tp.dst = htons(icmp->icmp6_code);
  348. memset(&key->ipv6.nd, 0, sizeof(key->ipv6.nd));
  349. if (icmp->icmp6_code == 0 &&
  350. (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  351. icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  352. int icmp_len = skb->len - skb_transport_offset(skb);
  353. struct nd_msg *nd;
  354. int offset;
  355. /* In order to process neighbor discovery options, we need the
  356. * entire packet.
  357. */
  358. if (unlikely(icmp_len < sizeof(*nd)))
  359. return 0;
  360. if (unlikely(skb_linearize(skb)))
  361. return -ENOMEM;
  362. nd = (struct nd_msg *)skb_transport_header(skb);
  363. key->ipv6.nd.target = nd->target;
  364. icmp_len -= sizeof(*nd);
  365. offset = 0;
  366. while (icmp_len >= 8) {
  367. struct nd_opt_hdr *nd_opt =
  368. (struct nd_opt_hdr *)(nd->opt + offset);
  369. int opt_len = nd_opt->nd_opt_len * 8;
  370. if (unlikely(!opt_len || opt_len > icmp_len))
  371. return 0;
  372. /* Store the link layer address if the appropriate
  373. * option is provided. It is considered an error if
  374. * the same link layer option is specified twice.
  375. */
  376. if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
  377. && opt_len == 8) {
  378. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
  379. goto invalid;
  380. ether_addr_copy(key->ipv6.nd.sll,
  381. &nd->opt[offset+sizeof(*nd_opt)]);
  382. } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
  383. && opt_len == 8) {
  384. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
  385. goto invalid;
  386. ether_addr_copy(key->ipv6.nd.tll,
  387. &nd->opt[offset+sizeof(*nd_opt)]);
  388. }
  389. icmp_len -= opt_len;
  390. offset += opt_len;
  391. }
  392. }
  393. return 0;
  394. invalid:
  395. memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
  396. memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
  397. memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
  398. return 0;
  399. }
  400. /**
  401. * key_extract - extracts a flow key from an Ethernet frame.
  402. * @skb: sk_buff that contains the frame, with skb->data pointing to the
  403. * Ethernet header
  404. * @key: output flow key
  405. *
  406. * The caller must ensure that skb->len >= ETH_HLEN.
  407. *
  408. * Returns 0 if successful, otherwise a negative errno value.
  409. *
  410. * Initializes @skb header pointers as follows:
  411. *
  412. * - skb->mac_header: the Ethernet header.
  413. *
  414. * - skb->network_header: just past the Ethernet header, or just past the
  415. * VLAN header, to the first byte of the Ethernet payload.
  416. *
  417. * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
  418. * on output, then just past the IP header, if one is present and
  419. * of a correct length, otherwise the same as skb->network_header.
  420. * For other key->eth.type values it is left untouched.
  421. */
  422. static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
  423. {
  424. int error;
  425. struct ethhdr *eth;
  426. /* Flags are always used as part of stats */
  427. key->tp.flags = 0;
  428. skb_reset_mac_header(skb);
  429. /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
  430. * header in the linear data area.
  431. */
  432. eth = eth_hdr(skb);
  433. ether_addr_copy(key->eth.src, eth->h_source);
  434. ether_addr_copy(key->eth.dst, eth->h_dest);
  435. __skb_pull(skb, 2 * ETH_ALEN);
  436. /* We are going to push all headers that we pull, so no need to
  437. * update skb->csum here.
  438. */
  439. if (unlikely(parse_vlan(skb, key)))
  440. return -ENOMEM;
  441. key->eth.type = parse_ethertype(skb);
  442. if (unlikely(key->eth.type == htons(0)))
  443. return -ENOMEM;
  444. skb_reset_network_header(skb);
  445. skb_reset_mac_len(skb);
  446. __skb_push(skb, skb->data - skb_mac_header(skb));
  447. /* Network layer. */
  448. if (key->eth.type == htons(ETH_P_IP)) {
  449. struct iphdr *nh;
  450. __be16 offset;
  451. error = check_iphdr(skb);
  452. if (unlikely(error)) {
  453. memset(&key->ip, 0, sizeof(key->ip));
  454. memset(&key->ipv4, 0, sizeof(key->ipv4));
  455. if (error == -EINVAL) {
  456. skb->transport_header = skb->network_header;
  457. error = 0;
  458. }
  459. return error;
  460. }
  461. nh = ip_hdr(skb);
  462. key->ipv4.addr.src = nh->saddr;
  463. key->ipv4.addr.dst = nh->daddr;
  464. key->ip.proto = nh->protocol;
  465. key->ip.tos = nh->tos;
  466. key->ip.ttl = nh->ttl;
  467. offset = nh->frag_off & htons(IP_OFFSET);
  468. if (offset) {
  469. key->ip.frag = OVS_FRAG_TYPE_LATER;
  470. return 0;
  471. }
  472. if (nh->frag_off & htons(IP_MF) ||
  473. skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  474. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  475. else
  476. key->ip.frag = OVS_FRAG_TYPE_NONE;
  477. /* Transport layer. */
  478. if (key->ip.proto == IPPROTO_TCP) {
  479. if (tcphdr_ok(skb)) {
  480. struct tcphdr *tcp = tcp_hdr(skb);
  481. key->tp.src = tcp->source;
  482. key->tp.dst = tcp->dest;
  483. key->tp.flags = TCP_FLAGS_BE16(tcp);
  484. } else {
  485. memset(&key->tp, 0, sizeof(key->tp));
  486. }
  487. } else if (key->ip.proto == IPPROTO_UDP) {
  488. if (udphdr_ok(skb)) {
  489. struct udphdr *udp = udp_hdr(skb);
  490. key->tp.src = udp->source;
  491. key->tp.dst = udp->dest;
  492. } else {
  493. memset(&key->tp, 0, sizeof(key->tp));
  494. }
  495. } else if (key->ip.proto == IPPROTO_SCTP) {
  496. if (sctphdr_ok(skb)) {
  497. struct sctphdr *sctp = sctp_hdr(skb);
  498. key->tp.src = sctp->source;
  499. key->tp.dst = sctp->dest;
  500. } else {
  501. memset(&key->tp, 0, sizeof(key->tp));
  502. }
  503. } else if (key->ip.proto == IPPROTO_ICMP) {
  504. if (icmphdr_ok(skb)) {
  505. struct icmphdr *icmp = icmp_hdr(skb);
  506. /* The ICMP type and code fields use the 16-bit
  507. * transport port fields, so we need to store
  508. * them in 16-bit network byte order. */
  509. key->tp.src = htons(icmp->type);
  510. key->tp.dst = htons(icmp->code);
  511. } else {
  512. memset(&key->tp, 0, sizeof(key->tp));
  513. }
  514. }
  515. } else if (key->eth.type == htons(ETH_P_ARP) ||
  516. key->eth.type == htons(ETH_P_RARP)) {
  517. struct arp_eth_header *arp;
  518. bool arp_available = arphdr_ok(skb);
  519. arp = (struct arp_eth_header *)skb_network_header(skb);
  520. if (arp_available &&
  521. arp->ar_hrd == htons(ARPHRD_ETHER) &&
  522. arp->ar_pro == htons(ETH_P_IP) &&
  523. arp->ar_hln == ETH_ALEN &&
  524. arp->ar_pln == 4) {
  525. /* We only match on the lower 8 bits of the opcode. */
  526. if (ntohs(arp->ar_op) <= 0xff)
  527. key->ip.proto = ntohs(arp->ar_op);
  528. else
  529. key->ip.proto = 0;
  530. memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
  531. memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
  532. ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
  533. ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
  534. } else {
  535. memset(&key->ip, 0, sizeof(key->ip));
  536. memset(&key->ipv4, 0, sizeof(key->ipv4));
  537. }
  538. } else if (eth_p_mpls(key->eth.type)) {
  539. size_t stack_len = MPLS_HLEN;
  540. skb_set_inner_network_header(skb, skb->mac_len);
  541. while (1) {
  542. __be32 lse;
  543. error = check_header(skb, skb->mac_len + stack_len);
  544. if (unlikely(error))
  545. return 0;
  546. memcpy(&lse, skb_inner_network_header(skb), MPLS_HLEN);
  547. if (stack_len == MPLS_HLEN)
  548. memcpy(&key->mpls.top_lse, &lse, MPLS_HLEN);
  549. skb_set_inner_network_header(skb, skb->mac_len + stack_len);
  550. if (lse & htonl(MPLS_LS_S_MASK))
  551. break;
  552. stack_len += MPLS_HLEN;
  553. }
  554. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  555. int nh_len; /* IPv6 Header + Extensions */
  556. nh_len = parse_ipv6hdr(skb, key);
  557. if (unlikely(nh_len < 0)) {
  558. switch (nh_len) {
  559. case -EINVAL:
  560. memset(&key->ip, 0, sizeof(key->ip));
  561. memset(&key->ipv6.addr, 0, sizeof(key->ipv6.addr));
  562. /* fall-through */
  563. case -EPROTO:
  564. skb->transport_header = skb->network_header;
  565. error = 0;
  566. break;
  567. default:
  568. error = nh_len;
  569. }
  570. return error;
  571. }
  572. if (key->ip.frag == OVS_FRAG_TYPE_LATER)
  573. return 0;
  574. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  575. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  576. /* Transport layer. */
  577. if (key->ip.proto == NEXTHDR_TCP) {
  578. if (tcphdr_ok(skb)) {
  579. struct tcphdr *tcp = tcp_hdr(skb);
  580. key->tp.src = tcp->source;
  581. key->tp.dst = tcp->dest;
  582. key->tp.flags = TCP_FLAGS_BE16(tcp);
  583. } else {
  584. memset(&key->tp, 0, sizeof(key->tp));
  585. }
  586. } else if (key->ip.proto == NEXTHDR_UDP) {
  587. if (udphdr_ok(skb)) {
  588. struct udphdr *udp = udp_hdr(skb);
  589. key->tp.src = udp->source;
  590. key->tp.dst = udp->dest;
  591. } else {
  592. memset(&key->tp, 0, sizeof(key->tp));
  593. }
  594. } else if (key->ip.proto == NEXTHDR_SCTP) {
  595. if (sctphdr_ok(skb)) {
  596. struct sctphdr *sctp = sctp_hdr(skb);
  597. key->tp.src = sctp->source;
  598. key->tp.dst = sctp->dest;
  599. } else {
  600. memset(&key->tp, 0, sizeof(key->tp));
  601. }
  602. } else if (key->ip.proto == NEXTHDR_ICMP) {
  603. if (icmp6hdr_ok(skb)) {
  604. error = parse_icmpv6(skb, key, nh_len);
  605. if (error)
  606. return error;
  607. } else {
  608. memset(&key->tp, 0, sizeof(key->tp));
  609. }
  610. }
  611. }
  612. return 0;
  613. }
  614. int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
  615. {
  616. return key_extract(skb, key);
  617. }
  618. int ovs_flow_key_extract(const struct ip_tunnel_info *tun_info,
  619. struct sk_buff *skb, struct sw_flow_key *key)
  620. {
  621. /* Extract metadata from packet. */
  622. if (tun_info) {
  623. key->tun_proto = ip_tunnel_info_af(tun_info);
  624. memcpy(&key->tun_key, &tun_info->key, sizeof(key->tun_key));
  625. if (tun_info->options_len) {
  626. BUILD_BUG_ON((1 << (sizeof(tun_info->options_len) *
  627. 8)) - 1
  628. > sizeof(key->tun_opts));
  629. ip_tunnel_info_opts_get(TUN_METADATA_OPTS(key, tun_info->options_len),
  630. tun_info);
  631. key->tun_opts_len = tun_info->options_len;
  632. } else {
  633. key->tun_opts_len = 0;
  634. }
  635. } else {
  636. key->tun_proto = 0;
  637. key->tun_opts_len = 0;
  638. memset(&key->tun_key, 0, sizeof(key->tun_key));
  639. }
  640. key->phy.priority = skb->priority;
  641. key->phy.in_port = OVS_CB(skb)->input_vport->port_no;
  642. key->phy.skb_mark = skb->mark;
  643. ovs_ct_fill_key(skb, key);
  644. key->ovs_flow_hash = 0;
  645. key->recirc_id = 0;
  646. return key_extract(skb, key);
  647. }
  648. int ovs_flow_key_extract_userspace(struct net *net, const struct nlattr *attr,
  649. struct sk_buff *skb,
  650. struct sw_flow_key *key, bool log)
  651. {
  652. int err;
  653. /* Extract metadata from netlink attributes. */
  654. err = ovs_nla_get_flow_metadata(net, attr, key, log);
  655. if (err)
  656. return err;
  657. return key_extract(skb, key);
  658. }