flow.c 19 KB

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