flow.c 19 KB

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