flow.c 19 KB

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