flow.c 22 KB

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