flow.c 28 KB

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