flow.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2007-2014 Nicira, Inc.
  4. */
  5. #include <linux/uaccess.h>
  6. #include <linux/netdevice.h>
  7. #include <linux/etherdevice.h>
  8. #include <linux/if_ether.h>
  9. #include <linux/if_vlan.h>
  10. #include <net/llc_pdu.h>
  11. #include <linux/kernel.h>
  12. #include <linux/jhash.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/llc.h>
  15. #include <linux/module.h>
  16. #include <linux/in.h>
  17. #include <linux/rcupdate.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/ip.h>
  21. #include <linux/ipv6.h>
  22. #include <linux/mpls.h>
  23. #include <linux/sctp.h>
  24. #include <linux/smp.h>
  25. #include <linux/tcp.h>
  26. #include <linux/udp.h>
  27. #include <linux/icmp.h>
  28. #include <linux/icmpv6.h>
  29. #include <linux/rculist.h>
  30. #include <net/ip.h>
  31. #include <net/ip_tunnels.h>
  32. #include <net/ipv6.h>
  33. #include <net/mpls.h>
  34. #include <net/ndisc.h>
  35. #include <net/nsh.h>
  36. #include <net/pkt_cls.h>
  37. #include <net/netfilter/nf_conntrack_zones.h>
  38. #include "conntrack.h"
  39. #include "datapath.h"
  40. #include "flow.h"
  41. #include "flow_netlink.h"
  42. #include "vport.h"
  43. u64 ovs_flow_used_time(unsigned long flow_jiffies)
  44. {
  45. struct timespec64 cur_ts;
  46. u64 cur_ms, idle_ms;
  47. ktime_get_ts64(&cur_ts);
  48. idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
  49. cur_ms = (u64)(u32)cur_ts.tv_sec * MSEC_PER_SEC +
  50. cur_ts.tv_nsec / NSEC_PER_MSEC;
  51. return cur_ms - idle_ms;
  52. }
  53. #define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
  54. void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
  55. const struct sk_buff *skb)
  56. {
  57. struct sw_flow_stats *stats;
  58. unsigned int cpu = smp_processor_id();
  59. int len = skb->len + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
  60. stats = rcu_dereference(flow->stats[cpu]);
  61. /* Check if already have CPU-specific stats. */
  62. if (likely(stats)) {
  63. spin_lock(&stats->lock);
  64. /* Mark if we write on the pre-allocated stats. */
  65. if (cpu == 0 && unlikely(flow->stats_last_writer != cpu))
  66. flow->stats_last_writer = cpu;
  67. } else {
  68. stats = rcu_dereference(flow->stats[0]); /* Pre-allocated. */
  69. spin_lock(&stats->lock);
  70. /* If the current CPU is the only writer on the
  71. * pre-allocated stats keep using them.
  72. */
  73. if (unlikely(flow->stats_last_writer != cpu)) {
  74. /* A previous locker may have already allocated the
  75. * stats, so we need to check again. If CPU-specific
  76. * stats were already allocated, we update the pre-
  77. * allocated stats as we have already locked them.
  78. */
  79. if (likely(flow->stats_last_writer != -1) &&
  80. likely(!rcu_access_pointer(flow->stats[cpu]))) {
  81. /* Try to allocate CPU-specific stats. */
  82. struct sw_flow_stats *new_stats;
  83. new_stats =
  84. kmem_cache_alloc_node(flow_stats_cache,
  85. GFP_NOWAIT |
  86. __GFP_THISNODE |
  87. __GFP_NOWARN |
  88. __GFP_NOMEMALLOC,
  89. numa_node_id());
  90. if (likely(new_stats)) {
  91. new_stats->used = jiffies;
  92. new_stats->packet_count = 1;
  93. new_stats->byte_count = len;
  94. new_stats->tcp_flags = tcp_flags;
  95. spin_lock_init(&new_stats->lock);
  96. rcu_assign_pointer(flow->stats[cpu],
  97. new_stats);
  98. cpumask_set_cpu(cpu,
  99. flow->cpu_used_mask);
  100. goto unlock;
  101. }
  102. }
  103. flow->stats_last_writer = cpu;
  104. }
  105. }
  106. stats->used = jiffies;
  107. stats->packet_count++;
  108. stats->byte_count += len;
  109. stats->tcp_flags |= tcp_flags;
  110. unlock:
  111. spin_unlock(&stats->lock);
  112. }
  113. /* Must be called with rcu_read_lock or ovs_mutex. */
  114. void ovs_flow_stats_get(const struct sw_flow *flow,
  115. struct ovs_flow_stats *ovs_stats,
  116. unsigned long *used, __be16 *tcp_flags)
  117. {
  118. unsigned int cpu;
  119. *used = 0;
  120. *tcp_flags = 0;
  121. memset(ovs_stats, 0, sizeof(*ovs_stats));
  122. for_each_cpu(cpu, flow->cpu_used_mask) {
  123. struct sw_flow_stats *stats = rcu_dereference_ovsl(flow->stats[cpu]);
  124. if (stats) {
  125. /* Local CPU may write on non-local stats, so we must
  126. * block bottom-halves here.
  127. */
  128. spin_lock_bh(&stats->lock);
  129. if (!*used || time_after(stats->used, *used))
  130. *used = stats->used;
  131. *tcp_flags |= stats->tcp_flags;
  132. ovs_stats->n_packets += stats->packet_count;
  133. ovs_stats->n_bytes += stats->byte_count;
  134. spin_unlock_bh(&stats->lock);
  135. }
  136. }
  137. }
  138. /* Called with ovs_mutex. */
  139. void ovs_flow_stats_clear(struct sw_flow *flow)
  140. {
  141. unsigned int cpu;
  142. for_each_cpu(cpu, flow->cpu_used_mask) {
  143. struct sw_flow_stats *stats = ovsl_dereference(flow->stats[cpu]);
  144. if (stats) {
  145. spin_lock_bh(&stats->lock);
  146. stats->used = 0;
  147. stats->packet_count = 0;
  148. stats->byte_count = 0;
  149. stats->tcp_flags = 0;
  150. spin_unlock_bh(&stats->lock);
  151. }
  152. }
  153. }
  154. static int check_header(struct sk_buff *skb, int len)
  155. {
  156. if (unlikely(skb->len < len))
  157. return -EINVAL;
  158. if (unlikely(!pskb_may_pull(skb, len)))
  159. return -ENOMEM;
  160. return 0;
  161. }
  162. static bool arphdr_ok(struct sk_buff *skb)
  163. {
  164. return pskb_may_pull(skb, skb_network_offset(skb) +
  165. sizeof(struct arp_eth_header));
  166. }
  167. static int check_iphdr(struct sk_buff *skb)
  168. {
  169. unsigned int nh_ofs = skb_network_offset(skb);
  170. unsigned int ip_len;
  171. int err;
  172. err = check_header(skb, nh_ofs + sizeof(struct iphdr));
  173. if (unlikely(err))
  174. return err;
  175. ip_len = ip_hdrlen(skb);
  176. if (unlikely(ip_len < sizeof(struct iphdr) ||
  177. skb->len < nh_ofs + ip_len))
  178. return -EINVAL;
  179. skb_set_transport_header(skb, nh_ofs + ip_len);
  180. return 0;
  181. }
  182. static bool tcphdr_ok(struct sk_buff *skb)
  183. {
  184. int th_ofs = skb_transport_offset(skb);
  185. int tcp_len;
  186. if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
  187. return false;
  188. tcp_len = tcp_hdrlen(skb);
  189. if (unlikely(tcp_len < sizeof(struct tcphdr) ||
  190. skb->len < th_ofs + tcp_len))
  191. return false;
  192. return true;
  193. }
  194. static bool udphdr_ok(struct sk_buff *skb)
  195. {
  196. return pskb_may_pull(skb, skb_transport_offset(skb) +
  197. sizeof(struct udphdr));
  198. }
  199. static bool sctphdr_ok(struct sk_buff *skb)
  200. {
  201. return pskb_may_pull(skb, skb_transport_offset(skb) +
  202. sizeof(struct sctphdr));
  203. }
  204. static bool icmphdr_ok(struct sk_buff *skb)
  205. {
  206. return pskb_may_pull(skb, skb_transport_offset(skb) +
  207. sizeof(struct icmphdr));
  208. }
  209. /**
  210. * get_ipv6_ext_hdrs() - Parses packet and sets IPv6 extension header flags.
  211. *
  212. * @skb: buffer where extension header data starts in packet
  213. * @nh: ipv6 header
  214. * @ext_hdrs: flags are stored here
  215. *
  216. * OFPIEH12_UNREP is set if more than one of a given IPv6 extension header
  217. * is unexpectedly encountered. (Two destination options headers may be
  218. * expected and would not cause this bit to be set.)
  219. *
  220. * OFPIEH12_UNSEQ is set if IPv6 extension headers were not in the order
  221. * preferred (but not required) by RFC 2460:
  222. *
  223. * When more than one extension header is used in the same packet, it is
  224. * recommended that those headers appear in the following order:
  225. * IPv6 header
  226. * Hop-by-Hop Options header
  227. * Destination Options header
  228. * Routing header
  229. * Fragment header
  230. * Authentication header
  231. * Encapsulating Security Payload header
  232. * Destination Options header
  233. * upper-layer header
  234. */
  235. static void get_ipv6_ext_hdrs(struct sk_buff *skb, struct ipv6hdr *nh,
  236. u16 *ext_hdrs)
  237. {
  238. u8 next_type = nh->nexthdr;
  239. unsigned int start = skb_network_offset(skb) + sizeof(struct ipv6hdr);
  240. int dest_options_header_count = 0;
  241. *ext_hdrs = 0;
  242. while (ipv6_ext_hdr(next_type)) {
  243. struct ipv6_opt_hdr _hdr, *hp;
  244. switch (next_type) {
  245. case IPPROTO_NONE:
  246. *ext_hdrs |= OFPIEH12_NONEXT;
  247. /* stop parsing */
  248. return;
  249. case IPPROTO_ESP:
  250. if (*ext_hdrs & OFPIEH12_ESP)
  251. *ext_hdrs |= OFPIEH12_UNREP;
  252. if ((*ext_hdrs & ~(OFPIEH12_HOP | OFPIEH12_DEST |
  253. OFPIEH12_ROUTER | IPPROTO_FRAGMENT |
  254. OFPIEH12_AUTH | OFPIEH12_UNREP)) ||
  255. dest_options_header_count >= 2) {
  256. *ext_hdrs |= OFPIEH12_UNSEQ;
  257. }
  258. *ext_hdrs |= OFPIEH12_ESP;
  259. break;
  260. case IPPROTO_AH:
  261. if (*ext_hdrs & OFPIEH12_AUTH)
  262. *ext_hdrs |= OFPIEH12_UNREP;
  263. if ((*ext_hdrs &
  264. ~(OFPIEH12_HOP | OFPIEH12_DEST | OFPIEH12_ROUTER |
  265. IPPROTO_FRAGMENT | OFPIEH12_UNREP)) ||
  266. dest_options_header_count >= 2) {
  267. *ext_hdrs |= OFPIEH12_UNSEQ;
  268. }
  269. *ext_hdrs |= OFPIEH12_AUTH;
  270. break;
  271. case IPPROTO_DSTOPTS:
  272. if (dest_options_header_count == 0) {
  273. if (*ext_hdrs &
  274. ~(OFPIEH12_HOP | OFPIEH12_UNREP))
  275. *ext_hdrs |= OFPIEH12_UNSEQ;
  276. *ext_hdrs |= OFPIEH12_DEST;
  277. } else if (dest_options_header_count == 1) {
  278. if (*ext_hdrs &
  279. ~(OFPIEH12_HOP | OFPIEH12_DEST |
  280. OFPIEH12_ROUTER | OFPIEH12_FRAG |
  281. OFPIEH12_AUTH | OFPIEH12_ESP |
  282. OFPIEH12_UNREP)) {
  283. *ext_hdrs |= OFPIEH12_UNSEQ;
  284. }
  285. } else {
  286. *ext_hdrs |= OFPIEH12_UNREP;
  287. }
  288. dest_options_header_count++;
  289. break;
  290. case IPPROTO_FRAGMENT:
  291. if (*ext_hdrs & OFPIEH12_FRAG)
  292. *ext_hdrs |= OFPIEH12_UNREP;
  293. if ((*ext_hdrs & ~(OFPIEH12_HOP |
  294. OFPIEH12_DEST |
  295. OFPIEH12_ROUTER |
  296. OFPIEH12_UNREP)) ||
  297. dest_options_header_count >= 2) {
  298. *ext_hdrs |= OFPIEH12_UNSEQ;
  299. }
  300. *ext_hdrs |= OFPIEH12_FRAG;
  301. break;
  302. case IPPROTO_ROUTING:
  303. if (*ext_hdrs & OFPIEH12_ROUTER)
  304. *ext_hdrs |= OFPIEH12_UNREP;
  305. if ((*ext_hdrs & ~(OFPIEH12_HOP |
  306. OFPIEH12_DEST |
  307. OFPIEH12_UNREP)) ||
  308. dest_options_header_count >= 2) {
  309. *ext_hdrs |= OFPIEH12_UNSEQ;
  310. }
  311. *ext_hdrs |= OFPIEH12_ROUTER;
  312. break;
  313. case IPPROTO_HOPOPTS:
  314. if (*ext_hdrs & OFPIEH12_HOP)
  315. *ext_hdrs |= OFPIEH12_UNREP;
  316. /* OFPIEH12_HOP is set to 1 if a hop-by-hop IPv6
  317. * extension header is present as the first
  318. * extension header in the packet.
  319. */
  320. if (*ext_hdrs == 0)
  321. *ext_hdrs |= OFPIEH12_HOP;
  322. else
  323. *ext_hdrs |= OFPIEH12_UNSEQ;
  324. break;
  325. default:
  326. return;
  327. }
  328. hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
  329. if (!hp)
  330. break;
  331. next_type = hp->nexthdr;
  332. start += ipv6_optlen(hp);
  333. }
  334. }
  335. static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
  336. {
  337. unsigned short frag_off;
  338. unsigned int payload_ofs = 0;
  339. unsigned int nh_ofs = skb_network_offset(skb);
  340. unsigned int nh_len;
  341. struct ipv6hdr *nh;
  342. int err, nexthdr, flags = 0;
  343. err = check_header(skb, nh_ofs + sizeof(*nh));
  344. if (unlikely(err))
  345. return err;
  346. nh = ipv6_hdr(skb);
  347. get_ipv6_ext_hdrs(skb, nh, &key->ipv6.exthdrs);
  348. key->ip.proto = NEXTHDR_NONE;
  349. key->ip.tos = ipv6_get_dsfield(nh);
  350. key->ip.ttl = nh->hop_limit;
  351. key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
  352. key->ipv6.addr.src = nh->saddr;
  353. key->ipv6.addr.dst = nh->daddr;
  354. nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags);
  355. if (flags & IP6_FH_F_FRAG) {
  356. if (frag_off) {
  357. key->ip.frag = OVS_FRAG_TYPE_LATER;
  358. key->ip.proto = NEXTHDR_FRAGMENT;
  359. return 0;
  360. }
  361. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  362. } else {
  363. key->ip.frag = OVS_FRAG_TYPE_NONE;
  364. }
  365. /* Delayed handling of error in ipv6_find_hdr() as it
  366. * always sets flags and frag_off to a valid value which may be
  367. * used to set key->ip.frag above.
  368. */
  369. if (unlikely(nexthdr < 0))
  370. return -EPROTO;
  371. nh_len = payload_ofs - nh_ofs;
  372. skb_set_transport_header(skb, nh_ofs + nh_len);
  373. key->ip.proto = nexthdr;
  374. return nh_len;
  375. }
  376. static bool icmp6hdr_ok(struct sk_buff *skb)
  377. {
  378. return pskb_may_pull(skb, skb_transport_offset(skb) +
  379. sizeof(struct icmp6hdr));
  380. }
  381. /**
  382. * parse_vlan_tag - Parse vlan tag from vlan header.
  383. * @skb: skb containing frame to parse
  384. * @key_vh: pointer to parsed vlan tag
  385. * @untag_vlan: should the vlan header be removed from the frame
  386. *
  387. * Return: ERROR on memory error.
  388. * %0 if it encounters a non-vlan or incomplete packet.
  389. * %1 after successfully parsing vlan tag.
  390. */
  391. static int parse_vlan_tag(struct sk_buff *skb, struct vlan_head *key_vh,
  392. bool untag_vlan)
  393. {
  394. struct vlan_head *vh = (struct vlan_head *)skb->data;
  395. if (likely(!eth_type_vlan(vh->tpid)))
  396. return 0;
  397. if (unlikely(skb->len < sizeof(struct vlan_head) + sizeof(__be16)))
  398. return 0;
  399. if (unlikely(!pskb_may_pull(skb, sizeof(struct vlan_head) +
  400. sizeof(__be16))))
  401. return -ENOMEM;
  402. vh = (struct vlan_head *)skb->data;
  403. key_vh->tci = vh->tci | htons(VLAN_CFI_MASK);
  404. key_vh->tpid = vh->tpid;
  405. if (unlikely(untag_vlan)) {
  406. int offset = skb->data - skb_mac_header(skb);
  407. u16 tci;
  408. int err;
  409. __skb_push(skb, offset);
  410. err = __skb_vlan_pop(skb, &tci);
  411. __skb_pull(skb, offset);
  412. if (err)
  413. return err;
  414. __vlan_hwaccel_put_tag(skb, key_vh->tpid, tci);
  415. } else {
  416. __skb_pull(skb, sizeof(struct vlan_head));
  417. }
  418. return 1;
  419. }
  420. static void clear_vlan(struct sw_flow_key *key)
  421. {
  422. key->eth.vlan.tci = 0;
  423. key->eth.vlan.tpid = 0;
  424. key->eth.cvlan.tci = 0;
  425. key->eth.cvlan.tpid = 0;
  426. }
  427. static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  428. {
  429. int res;
  430. if (skb_vlan_tag_present(skb)) {
  431. key->eth.vlan.tci = htons(skb->vlan_tci) | htons(VLAN_CFI_MASK);
  432. key->eth.vlan.tpid = skb->vlan_proto;
  433. } else {
  434. /* Parse outer vlan tag in the non-accelerated case. */
  435. res = parse_vlan_tag(skb, &key->eth.vlan, true);
  436. if (res <= 0)
  437. return res;
  438. }
  439. /* Parse inner vlan tag. */
  440. res = parse_vlan_tag(skb, &key->eth.cvlan, false);
  441. if (res <= 0)
  442. return res;
  443. return 0;
  444. }
  445. static __be16 parse_ethertype(struct sk_buff *skb)
  446. {
  447. struct llc_snap_hdr {
  448. u8 dsap; /* Always 0xAA */
  449. u8 ssap; /* Always 0xAA */
  450. u8 ctrl;
  451. u8 oui[3];
  452. __be16 ethertype;
  453. };
  454. struct llc_snap_hdr *llc;
  455. __be16 proto;
  456. proto = *(__be16 *) skb->data;
  457. __skb_pull(skb, sizeof(__be16));
  458. if (eth_proto_is_802_3(proto))
  459. return proto;
  460. if (skb->len < sizeof(struct llc_snap_hdr))
  461. return htons(ETH_P_802_2);
  462. if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
  463. return htons(0);
  464. llc = (struct llc_snap_hdr *) skb->data;
  465. if (llc->dsap != LLC_SAP_SNAP ||
  466. llc->ssap != LLC_SAP_SNAP ||
  467. (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
  468. return htons(ETH_P_802_2);
  469. __skb_pull(skb, sizeof(struct llc_snap_hdr));
  470. if (eth_proto_is_802_3(llc->ethertype))
  471. return llc->ethertype;
  472. return htons(ETH_P_802_2);
  473. }
  474. static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
  475. int nh_len)
  476. {
  477. struct icmp6hdr *icmp = icmp6_hdr(skb);
  478. /* The ICMPv6 type and code fields use the 16-bit transport port
  479. * fields, so we need to store them in 16-bit network byte order.
  480. */
  481. key->tp.src = htons(icmp->icmp6_type);
  482. key->tp.dst = htons(icmp->icmp6_code);
  483. if (icmp->icmp6_code == 0 &&
  484. (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  485. icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  486. int icmp_len = skb->len - skb_transport_offset(skb);
  487. struct nd_msg *nd;
  488. int offset;
  489. memset(&key->ipv6.nd, 0, sizeof(key->ipv6.nd));
  490. /* In order to process neighbor discovery options, we need the
  491. * entire packet.
  492. */
  493. if (unlikely(icmp_len < sizeof(*nd)))
  494. return 0;
  495. if (unlikely(skb_linearize(skb)))
  496. return -ENOMEM;
  497. nd = (struct nd_msg *)skb_transport_header(skb);
  498. key->ipv6.nd.target = nd->target;
  499. icmp_len -= sizeof(*nd);
  500. offset = 0;
  501. while (icmp_len >= 8) {
  502. struct nd_opt_hdr *nd_opt =
  503. (struct nd_opt_hdr *)(nd->opt + offset);
  504. int opt_len = nd_opt->nd_opt_len * 8;
  505. if (unlikely(!opt_len || opt_len > icmp_len))
  506. return 0;
  507. /* Store the link layer address if the appropriate
  508. * option is provided. It is considered an error if
  509. * the same link layer option is specified twice.
  510. */
  511. if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
  512. && opt_len == 8) {
  513. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
  514. goto invalid;
  515. ether_addr_copy(key->ipv6.nd.sll,
  516. &nd->opt[offset+sizeof(*nd_opt)]);
  517. } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
  518. && opt_len == 8) {
  519. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
  520. goto invalid;
  521. ether_addr_copy(key->ipv6.nd.tll,
  522. &nd->opt[offset+sizeof(*nd_opt)]);
  523. }
  524. icmp_len -= opt_len;
  525. offset += opt_len;
  526. }
  527. }
  528. return 0;
  529. invalid:
  530. memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
  531. memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
  532. memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
  533. return 0;
  534. }
  535. static int parse_nsh(struct sk_buff *skb, struct sw_flow_key *key)
  536. {
  537. struct nshhdr *nh;
  538. unsigned int nh_ofs = skb_network_offset(skb);
  539. u8 version, length;
  540. int err;
  541. err = check_header(skb, nh_ofs + NSH_BASE_HDR_LEN);
  542. if (unlikely(err))
  543. return err;
  544. nh = nsh_hdr(skb);
  545. version = nsh_get_ver(nh);
  546. length = nsh_hdr_len(nh);
  547. if (version != 0)
  548. return -EINVAL;
  549. err = check_header(skb, nh_ofs + length);
  550. if (unlikely(err))
  551. return err;
  552. nh = nsh_hdr(skb);
  553. key->nsh.base.flags = nsh_get_flags(nh);
  554. key->nsh.base.ttl = nsh_get_ttl(nh);
  555. key->nsh.base.mdtype = nh->mdtype;
  556. key->nsh.base.np = nh->np;
  557. key->nsh.base.path_hdr = nh->path_hdr;
  558. switch (key->nsh.base.mdtype) {
  559. case NSH_M_TYPE1:
  560. if (length != NSH_M_TYPE1_LEN)
  561. return -EINVAL;
  562. memcpy(key->nsh.context, nh->md1.context,
  563. sizeof(nh->md1));
  564. break;
  565. case NSH_M_TYPE2:
  566. memset(key->nsh.context, 0,
  567. sizeof(nh->md1));
  568. break;
  569. default:
  570. return -EINVAL;
  571. }
  572. return 0;
  573. }
  574. /**
  575. * key_extract_l3l4 - extracts L3/L4 header information.
  576. * @skb: sk_buff that contains the frame, with skb->data pointing to the
  577. * L3 header
  578. * @key: output flow key
  579. *
  580. * Return: %0 if successful, otherwise a negative errno value.
  581. */
  582. static int key_extract_l3l4(struct sk_buff *skb, struct sw_flow_key *key)
  583. {
  584. int error;
  585. /* Network layer. */
  586. if (key->eth.type == htons(ETH_P_IP)) {
  587. struct iphdr *nh;
  588. __be16 offset;
  589. error = check_iphdr(skb);
  590. if (unlikely(error)) {
  591. memset(&key->ip, 0, sizeof(key->ip));
  592. memset(&key->ipv4, 0, sizeof(key->ipv4));
  593. if (error == -EINVAL) {
  594. skb->transport_header = skb->network_header;
  595. error = 0;
  596. }
  597. return error;
  598. }
  599. nh = ip_hdr(skb);
  600. key->ipv4.addr.src = nh->saddr;
  601. key->ipv4.addr.dst = nh->daddr;
  602. key->ip.proto = nh->protocol;
  603. key->ip.tos = nh->tos;
  604. key->ip.ttl = nh->ttl;
  605. offset = nh->frag_off & htons(IP_OFFSET);
  606. if (offset) {
  607. key->ip.frag = OVS_FRAG_TYPE_LATER;
  608. memset(&key->tp, 0, sizeof(key->tp));
  609. return 0;
  610. }
  611. if (nh->frag_off & htons(IP_MF) ||
  612. skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  613. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  614. else
  615. key->ip.frag = OVS_FRAG_TYPE_NONE;
  616. /* Transport layer. */
  617. if (key->ip.proto == IPPROTO_TCP) {
  618. if (tcphdr_ok(skb)) {
  619. struct tcphdr *tcp = tcp_hdr(skb);
  620. key->tp.src = tcp->source;
  621. key->tp.dst = tcp->dest;
  622. key->tp.flags = TCP_FLAGS_BE16(tcp);
  623. } else {
  624. memset(&key->tp, 0, sizeof(key->tp));
  625. }
  626. } else if (key->ip.proto == IPPROTO_UDP) {
  627. if (udphdr_ok(skb)) {
  628. struct udphdr *udp = udp_hdr(skb);
  629. key->tp.src = udp->source;
  630. key->tp.dst = udp->dest;
  631. } else {
  632. memset(&key->tp, 0, sizeof(key->tp));
  633. }
  634. } else if (key->ip.proto == IPPROTO_SCTP) {
  635. if (sctphdr_ok(skb)) {
  636. struct sctphdr *sctp = sctp_hdr(skb);
  637. key->tp.src = sctp->source;
  638. key->tp.dst = sctp->dest;
  639. } else {
  640. memset(&key->tp, 0, sizeof(key->tp));
  641. }
  642. } else if (key->ip.proto == IPPROTO_ICMP) {
  643. if (icmphdr_ok(skb)) {
  644. struct icmphdr *icmp = icmp_hdr(skb);
  645. /* The ICMP type and code fields use the 16-bit
  646. * transport port fields, so we need to store
  647. * them in 16-bit network byte order. */
  648. key->tp.src = htons(icmp->type);
  649. key->tp.dst = htons(icmp->code);
  650. } else {
  651. memset(&key->tp, 0, sizeof(key->tp));
  652. }
  653. }
  654. } else if (key->eth.type == htons(ETH_P_ARP) ||
  655. key->eth.type == htons(ETH_P_RARP)) {
  656. struct arp_eth_header *arp;
  657. bool arp_available = arphdr_ok(skb);
  658. arp = (struct arp_eth_header *)skb_network_header(skb);
  659. if (arp_available &&
  660. arp->ar_hrd == htons(ARPHRD_ETHER) &&
  661. arp->ar_pro == htons(ETH_P_IP) &&
  662. arp->ar_hln == ETH_ALEN &&
  663. arp->ar_pln == 4) {
  664. /* We only match on the lower 8 bits of the opcode. */
  665. if (ntohs(arp->ar_op) <= 0xff)
  666. key->ip.proto = ntohs(arp->ar_op);
  667. else
  668. key->ip.proto = 0;
  669. memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
  670. memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
  671. ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
  672. ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
  673. } else {
  674. memset(&key->ip, 0, sizeof(key->ip));
  675. memset(&key->ipv4, 0, sizeof(key->ipv4));
  676. }
  677. } else if (eth_p_mpls(key->eth.type)) {
  678. size_t label_count = 1;
  679. memset(&key->mpls, 0, sizeof(key->mpls));
  680. skb_set_inner_network_header(skb, skb->mac_len);
  681. while (1) {
  682. __be32 lse;
  683. error = check_header(skb, skb->mac_len +
  684. label_count * MPLS_HLEN);
  685. if (unlikely(error))
  686. return 0;
  687. memcpy(&lse, skb_inner_network_header(skb), MPLS_HLEN);
  688. if (label_count <= MPLS_LABEL_DEPTH)
  689. memcpy(&key->mpls.lse[label_count - 1], &lse,
  690. MPLS_HLEN);
  691. skb_set_inner_network_header(skb, skb->mac_len +
  692. label_count * MPLS_HLEN);
  693. if (lse & htonl(MPLS_LS_S_MASK))
  694. break;
  695. label_count++;
  696. }
  697. if (label_count > MPLS_LABEL_DEPTH)
  698. label_count = MPLS_LABEL_DEPTH;
  699. key->mpls.num_labels_mask = GENMASK(label_count - 1, 0);
  700. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  701. int nh_len; /* IPv6 Header + Extensions */
  702. nh_len = parse_ipv6hdr(skb, key);
  703. if (unlikely(nh_len < 0)) {
  704. switch (nh_len) {
  705. case -EINVAL:
  706. memset(&key->ip, 0, sizeof(key->ip));
  707. memset(&key->ipv6.addr, 0, sizeof(key->ipv6.addr));
  708. fallthrough;
  709. case -EPROTO:
  710. skb->transport_header = skb->network_header;
  711. error = 0;
  712. break;
  713. default:
  714. error = nh_len;
  715. }
  716. return error;
  717. }
  718. if (key->ip.frag == OVS_FRAG_TYPE_LATER) {
  719. memset(&key->tp, 0, sizeof(key->tp));
  720. return 0;
  721. }
  722. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  723. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  724. /* Transport layer. */
  725. if (key->ip.proto == NEXTHDR_TCP) {
  726. if (tcphdr_ok(skb)) {
  727. struct tcphdr *tcp = tcp_hdr(skb);
  728. key->tp.src = tcp->source;
  729. key->tp.dst = tcp->dest;
  730. key->tp.flags = TCP_FLAGS_BE16(tcp);
  731. } else {
  732. memset(&key->tp, 0, sizeof(key->tp));
  733. }
  734. } else if (key->ip.proto == NEXTHDR_UDP) {
  735. if (udphdr_ok(skb)) {
  736. struct udphdr *udp = udp_hdr(skb);
  737. key->tp.src = udp->source;
  738. key->tp.dst = udp->dest;
  739. } else {
  740. memset(&key->tp, 0, sizeof(key->tp));
  741. }
  742. } else if (key->ip.proto == NEXTHDR_SCTP) {
  743. if (sctphdr_ok(skb)) {
  744. struct sctphdr *sctp = sctp_hdr(skb);
  745. key->tp.src = sctp->source;
  746. key->tp.dst = sctp->dest;
  747. } else {
  748. memset(&key->tp, 0, sizeof(key->tp));
  749. }
  750. } else if (key->ip.proto == NEXTHDR_ICMP) {
  751. if (icmp6hdr_ok(skb)) {
  752. error = parse_icmpv6(skb, key, nh_len);
  753. if (error)
  754. return error;
  755. } else {
  756. memset(&key->tp, 0, sizeof(key->tp));
  757. }
  758. }
  759. } else if (key->eth.type == htons(ETH_P_NSH)) {
  760. error = parse_nsh(skb, key);
  761. if (error)
  762. return error;
  763. }
  764. return 0;
  765. }
  766. /**
  767. * key_extract - extracts a flow key from an Ethernet frame.
  768. * @skb: sk_buff that contains the frame, with skb->data pointing to the
  769. * Ethernet header
  770. * @key: output flow key
  771. *
  772. * The caller must ensure that skb->len >= ETH_HLEN.
  773. *
  774. * Initializes @skb header fields as follows:
  775. *
  776. * - skb->mac_header: the L2 header.
  777. *
  778. * - skb->network_header: just past the L2 header, or just past the
  779. * VLAN header, to the first byte of the L2 payload.
  780. *
  781. * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
  782. * on output, then just past the IP header, if one is present and
  783. * of a correct length, otherwise the same as skb->network_header.
  784. * For other key->eth.type values it is left untouched.
  785. *
  786. * - skb->protocol: the type of the data starting at skb->network_header.
  787. * Equals to key->eth.type.
  788. *
  789. * Return: %0 if successful, otherwise a negative errno value.
  790. */
  791. static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
  792. {
  793. struct ethhdr *eth;
  794. /* Flags are always used as part of stats */
  795. key->tp.flags = 0;
  796. skb_reset_mac_header(skb);
  797. /* Link layer. */
  798. clear_vlan(key);
  799. if (ovs_key_mac_proto(key) == MAC_PROTO_NONE) {
  800. if (unlikely(eth_type_vlan(skb->protocol)))
  801. return -EINVAL;
  802. skb_reset_network_header(skb);
  803. key->eth.type = skb->protocol;
  804. } else {
  805. eth = eth_hdr(skb);
  806. ether_addr_copy(key->eth.src, eth->h_source);
  807. ether_addr_copy(key->eth.dst, eth->h_dest);
  808. __skb_pull(skb, 2 * ETH_ALEN);
  809. /* We are going to push all headers that we pull, so no need to
  810. * update skb->csum here.
  811. */
  812. if (unlikely(parse_vlan(skb, key)))
  813. return -ENOMEM;
  814. key->eth.type = parse_ethertype(skb);
  815. if (unlikely(key->eth.type == htons(0)))
  816. return -ENOMEM;
  817. /* Multiple tagged packets need to retain TPID to satisfy
  818. * skb_vlan_pop(), which will later shift the ethertype into
  819. * skb->protocol.
  820. */
  821. if (key->eth.cvlan.tci & htons(VLAN_CFI_MASK))
  822. skb->protocol = key->eth.cvlan.tpid;
  823. else
  824. skb->protocol = key->eth.type;
  825. skb_reset_network_header(skb);
  826. __skb_push(skb, skb->data - skb_mac_header(skb));
  827. }
  828. skb_reset_mac_len(skb);
  829. /* Fill out L3/L4 key info, if any */
  830. return key_extract_l3l4(skb, key);
  831. }
  832. /* In the case of conntrack fragment handling it expects L3 headers,
  833. * add a helper.
  834. */
  835. int ovs_flow_key_update_l3l4(struct sk_buff *skb, struct sw_flow_key *key)
  836. {
  837. return key_extract_l3l4(skb, key);
  838. }
  839. int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
  840. {
  841. int res;
  842. res = key_extract(skb, key);
  843. if (!res)
  844. key->mac_proto &= ~SW_FLOW_KEY_INVALID;
  845. return res;
  846. }
  847. static int key_extract_mac_proto(struct sk_buff *skb)
  848. {
  849. switch (skb->dev->type) {
  850. case ARPHRD_ETHER:
  851. return MAC_PROTO_ETHERNET;
  852. case ARPHRD_NONE:
  853. if (skb->protocol == htons(ETH_P_TEB))
  854. return MAC_PROTO_ETHERNET;
  855. return MAC_PROTO_NONE;
  856. }
  857. WARN_ON_ONCE(1);
  858. return -EINVAL;
  859. }
  860. int ovs_flow_key_extract(const struct ip_tunnel_info *tun_info,
  861. struct sk_buff *skb, struct sw_flow_key *key)
  862. {
  863. #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
  864. struct tc_skb_ext *tc_ext;
  865. #endif
  866. bool post_ct = false, post_ct_snat = false, post_ct_dnat = false;
  867. int res, err;
  868. u16 zone = 0;
  869. /* Extract metadata from packet. */
  870. if (tun_info) {
  871. key->tun_proto = ip_tunnel_info_af(tun_info);
  872. memcpy(&key->tun_key, &tun_info->key, sizeof(key->tun_key));
  873. if (tun_info->options_len) {
  874. BUILD_BUG_ON((1 << (sizeof(tun_info->options_len) *
  875. 8)) - 1
  876. > sizeof(key->tun_opts));
  877. ip_tunnel_info_opts_get(TUN_METADATA_OPTS(key, tun_info->options_len),
  878. tun_info);
  879. key->tun_opts_len = tun_info->options_len;
  880. } else {
  881. key->tun_opts_len = 0;
  882. }
  883. } else {
  884. key->tun_proto = 0;
  885. key->tun_opts_len = 0;
  886. memset(&key->tun_key, 0, sizeof(key->tun_key));
  887. }
  888. key->phy.priority = skb->priority;
  889. key->phy.in_port = OVS_CB(skb)->input_vport->port_no;
  890. key->phy.skb_mark = skb->mark;
  891. key->ovs_flow_hash = 0;
  892. res = key_extract_mac_proto(skb);
  893. if (res < 0)
  894. return res;
  895. key->mac_proto = res;
  896. #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
  897. if (tc_skb_ext_tc_enabled()) {
  898. tc_ext = skb_ext_find(skb, TC_SKB_EXT);
  899. key->recirc_id = tc_ext && !tc_ext->act_miss ?
  900. tc_ext->chain : 0;
  901. OVS_CB(skb)->mru = tc_ext ? tc_ext->mru : 0;
  902. post_ct = tc_ext ? tc_ext->post_ct : false;
  903. post_ct_snat = post_ct ? tc_ext->post_ct_snat : false;
  904. post_ct_dnat = post_ct ? tc_ext->post_ct_dnat : false;
  905. zone = post_ct ? tc_ext->zone : 0;
  906. } else {
  907. key->recirc_id = 0;
  908. }
  909. #else
  910. key->recirc_id = 0;
  911. #endif
  912. err = key_extract(skb, key);
  913. if (!err) {
  914. ovs_ct_fill_key(skb, key, post_ct); /* Must be after key_extract(). */
  915. if (post_ct) {
  916. if (!skb_get_nfct(skb)) {
  917. key->ct_zone = zone;
  918. } else {
  919. if (!post_ct_dnat)
  920. key->ct_state &= ~OVS_CS_F_DST_NAT;
  921. if (!post_ct_snat)
  922. key->ct_state &= ~OVS_CS_F_SRC_NAT;
  923. }
  924. }
  925. }
  926. return err;
  927. }
  928. int ovs_flow_key_extract_userspace(struct net *net, const struct nlattr *attr,
  929. struct sk_buff *skb,
  930. struct sw_flow_key *key, bool log)
  931. {
  932. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  933. u64 attrs = 0;
  934. int err;
  935. err = parse_flow_nlattrs(attr, a, &attrs, log);
  936. if (err)
  937. return -EINVAL;
  938. /* Extract metadata from netlink attributes. */
  939. err = ovs_nla_get_flow_metadata(net, a, attrs, key, log);
  940. if (err)
  941. return err;
  942. /* key_extract assumes that skb->protocol is set-up for
  943. * layer 3 packets which is the case for other callers,
  944. * in particular packets received from the network stack.
  945. * Here the correct value can be set from the metadata
  946. * extracted above.
  947. * For L2 packet key eth type would be zero. skb protocol
  948. * would be set to correct value later during key-extact.
  949. */
  950. skb->protocol = key->eth.type;
  951. err = key_extract(skb, key);
  952. if (err)
  953. return err;
  954. /* Check that we have conntrack original direction tuple metadata only
  955. * for packets for which it makes sense. Otherwise the key may be
  956. * corrupted due to overlapping key fields.
  957. */
  958. if (attrs & (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4) &&
  959. key->eth.type != htons(ETH_P_IP))
  960. return -EINVAL;
  961. if (attrs & (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6) &&
  962. (key->eth.type != htons(ETH_P_IPV6) ||
  963. sw_flow_key_is_nd(key)))
  964. return -EINVAL;
  965. return 0;
  966. }