flow.c 18 KB

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