flow.c 23 KB

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