flow.c 19 KB

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