flow.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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 timespec cur_ts;
  57. u64 cur_ms, idle_ms;
  58. ktime_get_ts(&cur_ts);
  59. idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
  60. cur_ms = (u64)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. } else {
  495. eth = eth_hdr(skb);
  496. ether_addr_copy(key->eth.src, eth->h_source);
  497. ether_addr_copy(key->eth.dst, eth->h_dest);
  498. __skb_pull(skb, 2 * ETH_ALEN);
  499. /* We are going to push all headers that we pull, so no need to
  500. * update skb->csum here.
  501. */
  502. if (unlikely(parse_vlan(skb, key)))
  503. return -ENOMEM;
  504. skb->protocol = parse_ethertype(skb);
  505. if (unlikely(skb->protocol == htons(0)))
  506. return -ENOMEM;
  507. skb_reset_network_header(skb);
  508. __skb_push(skb, skb->data - skb_mac_header(skb));
  509. }
  510. skb_reset_mac_len(skb);
  511. key->eth.type = skb->protocol;
  512. /* Network layer. */
  513. if (key->eth.type == htons(ETH_P_IP)) {
  514. struct iphdr *nh;
  515. __be16 offset;
  516. error = check_iphdr(skb);
  517. if (unlikely(error)) {
  518. memset(&key->ip, 0, sizeof(key->ip));
  519. memset(&key->ipv4, 0, sizeof(key->ipv4));
  520. if (error == -EINVAL) {
  521. skb->transport_header = skb->network_header;
  522. error = 0;
  523. }
  524. return error;
  525. }
  526. nh = ip_hdr(skb);
  527. key->ipv4.addr.src = nh->saddr;
  528. key->ipv4.addr.dst = nh->daddr;
  529. key->ip.proto = nh->protocol;
  530. key->ip.tos = nh->tos;
  531. key->ip.ttl = nh->ttl;
  532. offset = nh->frag_off & htons(IP_OFFSET);
  533. if (offset) {
  534. key->ip.frag = OVS_FRAG_TYPE_LATER;
  535. return 0;
  536. }
  537. if (nh->frag_off & htons(IP_MF) ||
  538. skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  539. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  540. else
  541. key->ip.frag = OVS_FRAG_TYPE_NONE;
  542. /* Transport layer. */
  543. if (key->ip.proto == IPPROTO_TCP) {
  544. if (tcphdr_ok(skb)) {
  545. struct tcphdr *tcp = tcp_hdr(skb);
  546. key->tp.src = tcp->source;
  547. key->tp.dst = tcp->dest;
  548. key->tp.flags = TCP_FLAGS_BE16(tcp);
  549. } else {
  550. memset(&key->tp, 0, sizeof(key->tp));
  551. }
  552. } else if (key->ip.proto == IPPROTO_UDP) {
  553. if (udphdr_ok(skb)) {
  554. struct udphdr *udp = udp_hdr(skb);
  555. key->tp.src = udp->source;
  556. key->tp.dst = udp->dest;
  557. } else {
  558. memset(&key->tp, 0, sizeof(key->tp));
  559. }
  560. } else if (key->ip.proto == IPPROTO_SCTP) {
  561. if (sctphdr_ok(skb)) {
  562. struct sctphdr *sctp = sctp_hdr(skb);
  563. key->tp.src = sctp->source;
  564. key->tp.dst = sctp->dest;
  565. } else {
  566. memset(&key->tp, 0, sizeof(key->tp));
  567. }
  568. } else if (key->ip.proto == IPPROTO_ICMP) {
  569. if (icmphdr_ok(skb)) {
  570. struct icmphdr *icmp = icmp_hdr(skb);
  571. /* The ICMP type and code fields use the 16-bit
  572. * transport port fields, so we need to store
  573. * them in 16-bit network byte order. */
  574. key->tp.src = htons(icmp->type);
  575. key->tp.dst = htons(icmp->code);
  576. } else {
  577. memset(&key->tp, 0, sizeof(key->tp));
  578. }
  579. }
  580. } else if (key->eth.type == htons(ETH_P_ARP) ||
  581. key->eth.type == htons(ETH_P_RARP)) {
  582. struct arp_eth_header *arp;
  583. bool arp_available = arphdr_ok(skb);
  584. arp = (struct arp_eth_header *)skb_network_header(skb);
  585. if (arp_available &&
  586. arp->ar_hrd == htons(ARPHRD_ETHER) &&
  587. arp->ar_pro == htons(ETH_P_IP) &&
  588. arp->ar_hln == ETH_ALEN &&
  589. arp->ar_pln == 4) {
  590. /* We only match on the lower 8 bits of the opcode. */
  591. if (ntohs(arp->ar_op) <= 0xff)
  592. key->ip.proto = ntohs(arp->ar_op);
  593. else
  594. key->ip.proto = 0;
  595. memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
  596. memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
  597. ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
  598. ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
  599. } else {
  600. memset(&key->ip, 0, sizeof(key->ip));
  601. memset(&key->ipv4, 0, sizeof(key->ipv4));
  602. }
  603. } else if (eth_p_mpls(key->eth.type)) {
  604. size_t stack_len = MPLS_HLEN;
  605. skb_set_inner_network_header(skb, skb->mac_len);
  606. while (1) {
  607. __be32 lse;
  608. error = check_header(skb, skb->mac_len + stack_len);
  609. if (unlikely(error))
  610. return 0;
  611. memcpy(&lse, skb_inner_network_header(skb), MPLS_HLEN);
  612. if (stack_len == MPLS_HLEN)
  613. memcpy(&key->mpls.top_lse, &lse, MPLS_HLEN);
  614. skb_set_inner_network_header(skb, skb->mac_len + stack_len);
  615. if (lse & htonl(MPLS_LS_S_MASK))
  616. break;
  617. stack_len += MPLS_HLEN;
  618. }
  619. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  620. int nh_len; /* IPv6 Header + Extensions */
  621. nh_len = parse_ipv6hdr(skb, key);
  622. if (unlikely(nh_len < 0)) {
  623. switch (nh_len) {
  624. case -EINVAL:
  625. memset(&key->ip, 0, sizeof(key->ip));
  626. memset(&key->ipv6.addr, 0, sizeof(key->ipv6.addr));
  627. /* fall-through */
  628. case -EPROTO:
  629. skb->transport_header = skb->network_header;
  630. error = 0;
  631. break;
  632. default:
  633. error = nh_len;
  634. }
  635. return error;
  636. }
  637. if (key->ip.frag == OVS_FRAG_TYPE_LATER)
  638. return 0;
  639. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  640. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  641. /* Transport layer. */
  642. if (key->ip.proto == NEXTHDR_TCP) {
  643. if (tcphdr_ok(skb)) {
  644. struct tcphdr *tcp = tcp_hdr(skb);
  645. key->tp.src = tcp->source;
  646. key->tp.dst = tcp->dest;
  647. key->tp.flags = TCP_FLAGS_BE16(tcp);
  648. } else {
  649. memset(&key->tp, 0, sizeof(key->tp));
  650. }
  651. } else if (key->ip.proto == NEXTHDR_UDP) {
  652. if (udphdr_ok(skb)) {
  653. struct udphdr *udp = udp_hdr(skb);
  654. key->tp.src = udp->source;
  655. key->tp.dst = udp->dest;
  656. } else {
  657. memset(&key->tp, 0, sizeof(key->tp));
  658. }
  659. } else if (key->ip.proto == NEXTHDR_SCTP) {
  660. if (sctphdr_ok(skb)) {
  661. struct sctphdr *sctp = sctp_hdr(skb);
  662. key->tp.src = sctp->source;
  663. key->tp.dst = sctp->dest;
  664. } else {
  665. memset(&key->tp, 0, sizeof(key->tp));
  666. }
  667. } else if (key->ip.proto == NEXTHDR_ICMP) {
  668. if (icmp6hdr_ok(skb)) {
  669. error = parse_icmpv6(skb, key, nh_len);
  670. if (error)
  671. return error;
  672. } else {
  673. memset(&key->tp, 0, sizeof(key->tp));
  674. }
  675. }
  676. } else if (key->eth.type == htons(ETH_P_NSH)) {
  677. error = parse_nsh(skb, key);
  678. if (error)
  679. return error;
  680. }
  681. return 0;
  682. }
  683. int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
  684. {
  685. int res;
  686. res = key_extract(skb, key);
  687. if (!res)
  688. key->mac_proto &= ~SW_FLOW_KEY_INVALID;
  689. return res;
  690. }
  691. static int key_extract_mac_proto(struct sk_buff *skb)
  692. {
  693. switch (skb->dev->type) {
  694. case ARPHRD_ETHER:
  695. return MAC_PROTO_ETHERNET;
  696. case ARPHRD_NONE:
  697. if (skb->protocol == htons(ETH_P_TEB))
  698. return MAC_PROTO_ETHERNET;
  699. return MAC_PROTO_NONE;
  700. }
  701. WARN_ON_ONCE(1);
  702. return -EINVAL;
  703. }
  704. int ovs_flow_key_extract(const struct ip_tunnel_info *tun_info,
  705. struct sk_buff *skb, struct sw_flow_key *key)
  706. {
  707. int res, err;
  708. /* Extract metadata from packet. */
  709. if (tun_info) {
  710. key->tun_proto = ip_tunnel_info_af(tun_info);
  711. memcpy(&key->tun_key, &tun_info->key, sizeof(key->tun_key));
  712. if (tun_info->options_len) {
  713. BUILD_BUG_ON((1 << (sizeof(tun_info->options_len) *
  714. 8)) - 1
  715. > sizeof(key->tun_opts));
  716. ip_tunnel_info_opts_get(TUN_METADATA_OPTS(key, tun_info->options_len),
  717. tun_info);
  718. key->tun_opts_len = tun_info->options_len;
  719. } else {
  720. key->tun_opts_len = 0;
  721. }
  722. } else {
  723. key->tun_proto = 0;
  724. key->tun_opts_len = 0;
  725. memset(&key->tun_key, 0, sizeof(key->tun_key));
  726. }
  727. key->phy.priority = skb->priority;
  728. key->phy.in_port = OVS_CB(skb)->input_vport->port_no;
  729. key->phy.skb_mark = skb->mark;
  730. key->ovs_flow_hash = 0;
  731. res = key_extract_mac_proto(skb);
  732. if (res < 0)
  733. return res;
  734. key->mac_proto = res;
  735. key->recirc_id = 0;
  736. err = key_extract(skb, key);
  737. if (!err)
  738. ovs_ct_fill_key(skb, key); /* Must be after key_extract(). */
  739. return err;
  740. }
  741. int ovs_flow_key_extract_userspace(struct net *net, const struct nlattr *attr,
  742. struct sk_buff *skb,
  743. struct sw_flow_key *key, bool log)
  744. {
  745. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  746. u64 attrs = 0;
  747. int err;
  748. err = parse_flow_nlattrs(attr, a, &attrs, log);
  749. if (err)
  750. return -EINVAL;
  751. /* Extract metadata from netlink attributes. */
  752. err = ovs_nla_get_flow_metadata(net, a, attrs, key, log);
  753. if (err)
  754. return err;
  755. /* key_extract assumes that skb->protocol is set-up for
  756. * layer 3 packets which is the case for other callers,
  757. * in particular packets received from the network stack.
  758. * Here the correct value can be set from the metadata
  759. * extracted above.
  760. * For L2 packet key eth type would be zero. skb protocol
  761. * would be set to correct value later during key-extact.
  762. */
  763. skb->protocol = key->eth.type;
  764. err = key_extract(skb, key);
  765. if (err)
  766. return err;
  767. /* Check that we have conntrack original direction tuple metadata only
  768. * for packets for which it makes sense. Otherwise the key may be
  769. * corrupted due to overlapping key fields.
  770. */
  771. if (attrs & (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4) &&
  772. key->eth.type != htons(ETH_P_IP))
  773. return -EINVAL;
  774. if (attrs & (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6) &&
  775. (key->eth.type != htons(ETH_P_IPV6) ||
  776. sw_flow_key_is_nd(key)))
  777. return -EINVAL;
  778. return 0;
  779. }