flow.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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 short frag_off;
  224. unsigned int payload_ofs = 0;
  225. unsigned int nh_ofs = skb_network_offset(skb);
  226. unsigned int nh_len;
  227. struct ipv6hdr *nh;
  228. int err, nexthdr, flags = 0;
  229. err = check_header(skb, nh_ofs + sizeof(*nh));
  230. if (unlikely(err))
  231. return err;
  232. nh = ipv6_hdr(skb);
  233. key->ip.proto = NEXTHDR_NONE;
  234. key->ip.tos = ipv6_get_dsfield(nh);
  235. key->ip.ttl = nh->hop_limit;
  236. key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
  237. key->ipv6.addr.src = nh->saddr;
  238. key->ipv6.addr.dst = nh->daddr;
  239. nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags);
  240. if (flags & IP6_FH_F_FRAG) {
  241. if (frag_off)
  242. key->ip.frag = OVS_FRAG_TYPE_LATER;
  243. else
  244. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  245. } else {
  246. key->ip.frag = OVS_FRAG_TYPE_NONE;
  247. }
  248. /* Delayed handling of error in ipv6_find_hdr() as it
  249. * always sets flags and frag_off to a valid value which may be
  250. * used to set key->ip.frag above.
  251. */
  252. if (unlikely(nexthdr < 0))
  253. return -EPROTO;
  254. nh_len = payload_ofs - nh_ofs;
  255. skb_set_transport_header(skb, nh_ofs + nh_len);
  256. key->ip.proto = nexthdr;
  257. return nh_len;
  258. }
  259. static bool icmp6hdr_ok(struct sk_buff *skb)
  260. {
  261. return pskb_may_pull(skb, skb_transport_offset(skb) +
  262. sizeof(struct icmp6hdr));
  263. }
  264. /**
  265. * Parse vlan tag from vlan header.
  266. * Returns ERROR on memory error.
  267. * Returns 0 if it encounters a non-vlan or incomplete packet.
  268. * Returns 1 after successfully parsing vlan tag.
  269. */
  270. static int parse_vlan_tag(struct sk_buff *skb, struct vlan_head *key_vh,
  271. bool untag_vlan)
  272. {
  273. struct vlan_head *vh = (struct vlan_head *)skb->data;
  274. if (likely(!eth_type_vlan(vh->tpid)))
  275. return 0;
  276. if (unlikely(skb->len < sizeof(struct vlan_head) + sizeof(__be16)))
  277. return 0;
  278. if (unlikely(!pskb_may_pull(skb, sizeof(struct vlan_head) +
  279. sizeof(__be16))))
  280. return -ENOMEM;
  281. vh = (struct vlan_head *)skb->data;
  282. key_vh->tci = vh->tci | htons(VLAN_TAG_PRESENT);
  283. key_vh->tpid = vh->tpid;
  284. if (unlikely(untag_vlan)) {
  285. int offset = skb->data - skb_mac_header(skb);
  286. u16 tci;
  287. int err;
  288. __skb_push(skb, offset);
  289. err = __skb_vlan_pop(skb, &tci);
  290. __skb_pull(skb, offset);
  291. if (err)
  292. return err;
  293. __vlan_hwaccel_put_tag(skb, key_vh->tpid, tci);
  294. } else {
  295. __skb_pull(skb, sizeof(struct vlan_head));
  296. }
  297. return 1;
  298. }
  299. static void clear_vlan(struct sw_flow_key *key)
  300. {
  301. key->eth.vlan.tci = 0;
  302. key->eth.vlan.tpid = 0;
  303. key->eth.cvlan.tci = 0;
  304. key->eth.cvlan.tpid = 0;
  305. }
  306. static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  307. {
  308. int res;
  309. if (skb_vlan_tag_present(skb)) {
  310. key->eth.vlan.tci = htons(skb->vlan_tci);
  311. key->eth.vlan.tpid = skb->vlan_proto;
  312. } else {
  313. /* Parse outer vlan tag in the non-accelerated case. */
  314. res = parse_vlan_tag(skb, &key->eth.vlan, true);
  315. if (res <= 0)
  316. return res;
  317. }
  318. /* Parse inner vlan tag. */
  319. res = parse_vlan_tag(skb, &key->eth.cvlan, false);
  320. if (res <= 0)
  321. return res;
  322. return 0;
  323. }
  324. static __be16 parse_ethertype(struct sk_buff *skb)
  325. {
  326. struct llc_snap_hdr {
  327. u8 dsap; /* Always 0xAA */
  328. u8 ssap; /* Always 0xAA */
  329. u8 ctrl;
  330. u8 oui[3];
  331. __be16 ethertype;
  332. };
  333. struct llc_snap_hdr *llc;
  334. __be16 proto;
  335. proto = *(__be16 *) skb->data;
  336. __skb_pull(skb, sizeof(__be16));
  337. if (eth_proto_is_802_3(proto))
  338. return proto;
  339. if (skb->len < sizeof(struct llc_snap_hdr))
  340. return htons(ETH_P_802_2);
  341. if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
  342. return htons(0);
  343. llc = (struct llc_snap_hdr *) skb->data;
  344. if (llc->dsap != LLC_SAP_SNAP ||
  345. llc->ssap != LLC_SAP_SNAP ||
  346. (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
  347. return htons(ETH_P_802_2);
  348. __skb_pull(skb, sizeof(struct llc_snap_hdr));
  349. if (eth_proto_is_802_3(llc->ethertype))
  350. return llc->ethertype;
  351. return htons(ETH_P_802_2);
  352. }
  353. static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
  354. int nh_len)
  355. {
  356. struct icmp6hdr *icmp = icmp6_hdr(skb);
  357. /* The ICMPv6 type and code fields use the 16-bit transport port
  358. * fields, so we need to store them in 16-bit network byte order.
  359. */
  360. key->tp.src = htons(icmp->icmp6_type);
  361. key->tp.dst = htons(icmp->icmp6_code);
  362. memset(&key->ipv6.nd, 0, sizeof(key->ipv6.nd));
  363. if (icmp->icmp6_code == 0 &&
  364. (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  365. icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  366. int icmp_len = skb->len - skb_transport_offset(skb);
  367. struct nd_msg *nd;
  368. int offset;
  369. /* In order to process neighbor discovery options, we need the
  370. * entire packet.
  371. */
  372. if (unlikely(icmp_len < sizeof(*nd)))
  373. return 0;
  374. if (unlikely(skb_linearize(skb)))
  375. return -ENOMEM;
  376. nd = (struct nd_msg *)skb_transport_header(skb);
  377. key->ipv6.nd.target = nd->target;
  378. icmp_len -= sizeof(*nd);
  379. offset = 0;
  380. while (icmp_len >= 8) {
  381. struct nd_opt_hdr *nd_opt =
  382. (struct nd_opt_hdr *)(nd->opt + offset);
  383. int opt_len = nd_opt->nd_opt_len * 8;
  384. if (unlikely(!opt_len || opt_len > icmp_len))
  385. return 0;
  386. /* Store the link layer address if the appropriate
  387. * option is provided. It is considered an error if
  388. * the same link layer option is specified twice.
  389. */
  390. if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
  391. && opt_len == 8) {
  392. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
  393. goto invalid;
  394. ether_addr_copy(key->ipv6.nd.sll,
  395. &nd->opt[offset+sizeof(*nd_opt)]);
  396. } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
  397. && opt_len == 8) {
  398. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
  399. goto invalid;
  400. ether_addr_copy(key->ipv6.nd.tll,
  401. &nd->opt[offset+sizeof(*nd_opt)]);
  402. }
  403. icmp_len -= opt_len;
  404. offset += opt_len;
  405. }
  406. }
  407. return 0;
  408. invalid:
  409. memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
  410. memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
  411. memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
  412. return 0;
  413. }
  414. static int parse_nsh(struct sk_buff *skb, struct sw_flow_key *key)
  415. {
  416. struct nshhdr *nh;
  417. unsigned int nh_ofs = skb_network_offset(skb);
  418. u8 version, length;
  419. int err;
  420. err = check_header(skb, nh_ofs + NSH_BASE_HDR_LEN);
  421. if (unlikely(err))
  422. return err;
  423. nh = nsh_hdr(skb);
  424. version = nsh_get_ver(nh);
  425. length = nsh_hdr_len(nh);
  426. if (version != 0)
  427. return -EINVAL;
  428. err = check_header(skb, nh_ofs + length);
  429. if (unlikely(err))
  430. return err;
  431. nh = nsh_hdr(skb);
  432. key->nsh.base.flags = nsh_get_flags(nh);
  433. key->nsh.base.ttl = nsh_get_ttl(nh);
  434. key->nsh.base.mdtype = nh->mdtype;
  435. key->nsh.base.np = nh->np;
  436. key->nsh.base.path_hdr = nh->path_hdr;
  437. switch (key->nsh.base.mdtype) {
  438. case NSH_M_TYPE1:
  439. if (length != NSH_M_TYPE1_LEN)
  440. return -EINVAL;
  441. memcpy(key->nsh.context, nh->md1.context,
  442. sizeof(nh->md1));
  443. break;
  444. case NSH_M_TYPE2:
  445. memset(key->nsh.context, 0,
  446. sizeof(nh->md1));
  447. break;
  448. default:
  449. return -EINVAL;
  450. }
  451. return 0;
  452. }
  453. /**
  454. * key_extract - extracts a flow key from an Ethernet frame.
  455. * @skb: sk_buff that contains the frame, with skb->data pointing to the
  456. * Ethernet header
  457. * @key: output flow key
  458. *
  459. * The caller must ensure that skb->len >= ETH_HLEN.
  460. *
  461. * Returns 0 if successful, otherwise a negative errno value.
  462. *
  463. * Initializes @skb header fields as follows:
  464. *
  465. * - skb->mac_header: the L2 header.
  466. *
  467. * - skb->network_header: just past the L2 header, or just past the
  468. * VLAN header, to the first byte of the L2 payload.
  469. *
  470. * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
  471. * on output, then just past the IP header, if one is present and
  472. * of a correct length, otherwise the same as skb->network_header.
  473. * For other key->eth.type values it is left untouched.
  474. *
  475. * - skb->protocol: the type of the data starting at skb->network_header.
  476. * Equals to key->eth.type.
  477. */
  478. static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
  479. {
  480. int error;
  481. struct ethhdr *eth;
  482. /* Flags are always used as part of stats */
  483. key->tp.flags = 0;
  484. skb_reset_mac_header(skb);
  485. /* Link layer. */
  486. clear_vlan(key);
  487. if (ovs_key_mac_proto(key) == MAC_PROTO_NONE) {
  488. if (unlikely(eth_type_vlan(skb->protocol)))
  489. return -EINVAL;
  490. skb_reset_network_header(skb);
  491. key->eth.type = skb->protocol;
  492. } else {
  493. eth = eth_hdr(skb);
  494. ether_addr_copy(key->eth.src, eth->h_source);
  495. ether_addr_copy(key->eth.dst, eth->h_dest);
  496. __skb_pull(skb, 2 * ETH_ALEN);
  497. /* We are going to push all headers that we pull, so no need to
  498. * update skb->csum here.
  499. */
  500. if (unlikely(parse_vlan(skb, key)))
  501. return -ENOMEM;
  502. key->eth.type = parse_ethertype(skb);
  503. if (unlikely(key->eth.type == htons(0)))
  504. return -ENOMEM;
  505. /* Multiple tagged packets need to retain TPID to satisfy
  506. * skb_vlan_pop(), which will later shift the ethertype into
  507. * skb->protocol.
  508. */
  509. if (key->eth.cvlan.tci & htons(VLAN_TAG_PRESENT))
  510. skb->protocol = key->eth.cvlan.tpid;
  511. else
  512. skb->protocol = key->eth.type;
  513. skb_reset_network_header(skb);
  514. __skb_push(skb, skb->data - skb_mac_header(skb));
  515. }
  516. skb_reset_mac_len(skb);
  517. /* Network layer. */
  518. if (key->eth.type == htons(ETH_P_IP)) {
  519. struct iphdr *nh;
  520. __be16 offset;
  521. error = check_iphdr(skb);
  522. if (unlikely(error)) {
  523. memset(&key->ip, 0, sizeof(key->ip));
  524. memset(&key->ipv4, 0, sizeof(key->ipv4));
  525. if (error == -EINVAL) {
  526. skb->transport_header = skb->network_header;
  527. error = 0;
  528. }
  529. return error;
  530. }
  531. nh = ip_hdr(skb);
  532. key->ipv4.addr.src = nh->saddr;
  533. key->ipv4.addr.dst = nh->daddr;
  534. key->ip.proto = nh->protocol;
  535. key->ip.tos = nh->tos;
  536. key->ip.ttl = nh->ttl;
  537. offset = nh->frag_off & htons(IP_OFFSET);
  538. if (offset) {
  539. key->ip.frag = OVS_FRAG_TYPE_LATER;
  540. return 0;
  541. }
  542. if (nh->frag_off & htons(IP_MF) ||
  543. skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  544. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  545. else
  546. key->ip.frag = OVS_FRAG_TYPE_NONE;
  547. /* Transport layer. */
  548. if (key->ip.proto == IPPROTO_TCP) {
  549. if (tcphdr_ok(skb)) {
  550. struct tcphdr *tcp = tcp_hdr(skb);
  551. key->tp.src = tcp->source;
  552. key->tp.dst = tcp->dest;
  553. key->tp.flags = TCP_FLAGS_BE16(tcp);
  554. } else {
  555. memset(&key->tp, 0, sizeof(key->tp));
  556. }
  557. } else if (key->ip.proto == IPPROTO_UDP) {
  558. if (udphdr_ok(skb)) {
  559. struct udphdr *udp = udp_hdr(skb);
  560. key->tp.src = udp->source;
  561. key->tp.dst = udp->dest;
  562. } else {
  563. memset(&key->tp, 0, sizeof(key->tp));
  564. }
  565. } else if (key->ip.proto == IPPROTO_SCTP) {
  566. if (sctphdr_ok(skb)) {
  567. struct sctphdr *sctp = sctp_hdr(skb);
  568. key->tp.src = sctp->source;
  569. key->tp.dst = sctp->dest;
  570. } else {
  571. memset(&key->tp, 0, sizeof(key->tp));
  572. }
  573. } else if (key->ip.proto == IPPROTO_ICMP) {
  574. if (icmphdr_ok(skb)) {
  575. struct icmphdr *icmp = icmp_hdr(skb);
  576. /* The ICMP type and code fields use the 16-bit
  577. * transport port fields, so we need to store
  578. * them in 16-bit network byte order. */
  579. key->tp.src = htons(icmp->type);
  580. key->tp.dst = htons(icmp->code);
  581. } else {
  582. memset(&key->tp, 0, sizeof(key->tp));
  583. }
  584. }
  585. } else if (key->eth.type == htons(ETH_P_ARP) ||
  586. key->eth.type == htons(ETH_P_RARP)) {
  587. struct arp_eth_header *arp;
  588. bool arp_available = arphdr_ok(skb);
  589. arp = (struct arp_eth_header *)skb_network_header(skb);
  590. if (arp_available &&
  591. arp->ar_hrd == htons(ARPHRD_ETHER) &&
  592. arp->ar_pro == htons(ETH_P_IP) &&
  593. arp->ar_hln == ETH_ALEN &&
  594. arp->ar_pln == 4) {
  595. /* We only match on the lower 8 bits of the opcode. */
  596. if (ntohs(arp->ar_op) <= 0xff)
  597. key->ip.proto = ntohs(arp->ar_op);
  598. else
  599. key->ip.proto = 0;
  600. memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
  601. memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
  602. ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
  603. ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
  604. } else {
  605. memset(&key->ip, 0, sizeof(key->ip));
  606. memset(&key->ipv4, 0, sizeof(key->ipv4));
  607. }
  608. } else if (eth_p_mpls(key->eth.type)) {
  609. size_t stack_len = MPLS_HLEN;
  610. skb_set_inner_network_header(skb, skb->mac_len);
  611. while (1) {
  612. __be32 lse;
  613. error = check_header(skb, skb->mac_len + stack_len);
  614. if (unlikely(error))
  615. return 0;
  616. memcpy(&lse, skb_inner_network_header(skb), MPLS_HLEN);
  617. if (stack_len == MPLS_HLEN)
  618. memcpy(&key->mpls.top_lse, &lse, MPLS_HLEN);
  619. skb_set_inner_network_header(skb, skb->mac_len + stack_len);
  620. if (lse & htonl(MPLS_LS_S_MASK))
  621. break;
  622. stack_len += MPLS_HLEN;
  623. }
  624. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  625. int nh_len; /* IPv6 Header + Extensions */
  626. nh_len = parse_ipv6hdr(skb, key);
  627. if (unlikely(nh_len < 0)) {
  628. switch (nh_len) {
  629. case -EINVAL:
  630. memset(&key->ip, 0, sizeof(key->ip));
  631. memset(&key->ipv6.addr, 0, sizeof(key->ipv6.addr));
  632. /* fall-through */
  633. case -EPROTO:
  634. skb->transport_header = skb->network_header;
  635. error = 0;
  636. break;
  637. default:
  638. error = nh_len;
  639. }
  640. return error;
  641. }
  642. if (key->ip.frag == OVS_FRAG_TYPE_LATER)
  643. return 0;
  644. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  645. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  646. /* Transport layer. */
  647. if (key->ip.proto == NEXTHDR_TCP) {
  648. if (tcphdr_ok(skb)) {
  649. struct tcphdr *tcp = tcp_hdr(skb);
  650. key->tp.src = tcp->source;
  651. key->tp.dst = tcp->dest;
  652. key->tp.flags = TCP_FLAGS_BE16(tcp);
  653. } else {
  654. memset(&key->tp, 0, sizeof(key->tp));
  655. }
  656. } else if (key->ip.proto == NEXTHDR_UDP) {
  657. if (udphdr_ok(skb)) {
  658. struct udphdr *udp = udp_hdr(skb);
  659. key->tp.src = udp->source;
  660. key->tp.dst = udp->dest;
  661. } else {
  662. memset(&key->tp, 0, sizeof(key->tp));
  663. }
  664. } else if (key->ip.proto == NEXTHDR_SCTP) {
  665. if (sctphdr_ok(skb)) {
  666. struct sctphdr *sctp = sctp_hdr(skb);
  667. key->tp.src = sctp->source;
  668. key->tp.dst = sctp->dest;
  669. } else {
  670. memset(&key->tp, 0, sizeof(key->tp));
  671. }
  672. } else if (key->ip.proto == NEXTHDR_ICMP) {
  673. if (icmp6hdr_ok(skb)) {
  674. error = parse_icmpv6(skb, key, nh_len);
  675. if (error)
  676. return error;
  677. } else {
  678. memset(&key->tp, 0, sizeof(key->tp));
  679. }
  680. }
  681. } else if (key->eth.type == htons(ETH_P_NSH)) {
  682. error = parse_nsh(skb, key);
  683. if (error)
  684. return error;
  685. }
  686. return 0;
  687. }
  688. int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
  689. {
  690. int res;
  691. res = key_extract(skb, key);
  692. if (!res)
  693. key->mac_proto &= ~SW_FLOW_KEY_INVALID;
  694. return res;
  695. }
  696. static int key_extract_mac_proto(struct sk_buff *skb)
  697. {
  698. switch (skb->dev->type) {
  699. case ARPHRD_ETHER:
  700. return MAC_PROTO_ETHERNET;
  701. case ARPHRD_NONE:
  702. if (skb->protocol == htons(ETH_P_TEB))
  703. return MAC_PROTO_ETHERNET;
  704. return MAC_PROTO_NONE;
  705. }
  706. WARN_ON_ONCE(1);
  707. return -EINVAL;
  708. }
  709. int ovs_flow_key_extract(const struct ip_tunnel_info *tun_info,
  710. struct sk_buff *skb, struct sw_flow_key *key)
  711. {
  712. int res, err;
  713. /* Extract metadata from packet. */
  714. if (tun_info) {
  715. key->tun_proto = ip_tunnel_info_af(tun_info);
  716. memcpy(&key->tun_key, &tun_info->key, sizeof(key->tun_key));
  717. if (tun_info->options_len) {
  718. BUILD_BUG_ON((1 << (sizeof(tun_info->options_len) *
  719. 8)) - 1
  720. > sizeof(key->tun_opts));
  721. ip_tunnel_info_opts_get(TUN_METADATA_OPTS(key, tun_info->options_len),
  722. tun_info);
  723. key->tun_opts_len = tun_info->options_len;
  724. } else {
  725. key->tun_opts_len = 0;
  726. }
  727. } else {
  728. key->tun_proto = 0;
  729. key->tun_opts_len = 0;
  730. memset(&key->tun_key, 0, sizeof(key->tun_key));
  731. }
  732. key->phy.priority = skb->priority;
  733. key->phy.in_port = OVS_CB(skb)->input_vport->port_no;
  734. key->phy.skb_mark = skb->mark;
  735. key->ovs_flow_hash = 0;
  736. res = key_extract_mac_proto(skb);
  737. if (res < 0)
  738. return res;
  739. key->mac_proto = res;
  740. key->recirc_id = 0;
  741. err = key_extract(skb, key);
  742. if (!err)
  743. ovs_ct_fill_key(skb, key); /* Must be after key_extract(). */
  744. return err;
  745. }
  746. int ovs_flow_key_extract_userspace(struct net *net, const struct nlattr *attr,
  747. struct sk_buff *skb,
  748. struct sw_flow_key *key, bool log)
  749. {
  750. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  751. u64 attrs = 0;
  752. int err;
  753. err = parse_flow_nlattrs(attr, a, &attrs, log);
  754. if (err)
  755. return -EINVAL;
  756. /* Extract metadata from netlink attributes. */
  757. err = ovs_nla_get_flow_metadata(net, a, attrs, key, log);
  758. if (err)
  759. return err;
  760. /* key_extract assumes that skb->protocol is set-up for
  761. * layer 3 packets which is the case for other callers,
  762. * in particular packets received from the network stack.
  763. * Here the correct value can be set from the metadata
  764. * extracted above.
  765. * For L2 packet key eth type would be zero. skb protocol
  766. * would be set to correct value later during key-extact.
  767. */
  768. skb->protocol = key->eth.type;
  769. err = key_extract(skb, key);
  770. if (err)
  771. return err;
  772. /* Check that we have conntrack original direction tuple metadata only
  773. * for packets for which it makes sense. Otherwise the key may be
  774. * corrupted due to overlapping key fields.
  775. */
  776. if (attrs & (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4) &&
  777. key->eth.type != htons(ETH_P_IP))
  778. return -EINVAL;
  779. if (attrs & (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6) &&
  780. (key->eth.type != htons(ETH_P_IPV6) ||
  781. sw_flow_key_is_nd(key)))
  782. return -EINVAL;
  783. return 0;
  784. }