actions.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/skbuff.h>
  20. #include <linux/in.h>
  21. #include <linux/ip.h>
  22. #include <linux/openvswitch.h>
  23. #include <linux/netfilter_ipv6.h>
  24. #include <linux/sctp.h>
  25. #include <linux/tcp.h>
  26. #include <linux/udp.h>
  27. #include <linux/in6.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/if_vlan.h>
  30. #include <net/dst.h>
  31. #include <net/ip.h>
  32. #include <net/ipv6.h>
  33. #include <net/ip6_fib.h>
  34. #include <net/checksum.h>
  35. #include <net/dsfield.h>
  36. #include <net/mpls.h>
  37. #include <net/sctp/checksum.h>
  38. #include "datapath.h"
  39. #include "flow.h"
  40. #include "conntrack.h"
  41. #include "vport.h"
  42. static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
  43. struct sw_flow_key *key,
  44. const struct nlattr *attr, int len);
  45. struct deferred_action {
  46. struct sk_buff *skb;
  47. const struct nlattr *actions;
  48. /* Store pkt_key clone when creating deferred action. */
  49. struct sw_flow_key pkt_key;
  50. };
  51. #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
  52. struct ovs_frag_data {
  53. unsigned long dst;
  54. struct vport *vport;
  55. struct ovs_skb_cb cb;
  56. __be16 inner_protocol;
  57. __u16 vlan_tci;
  58. __be16 vlan_proto;
  59. unsigned int l2_len;
  60. u8 l2_data[MAX_L2_LEN];
  61. };
  62. static DEFINE_PER_CPU(struct ovs_frag_data, ovs_frag_data_storage);
  63. #define DEFERRED_ACTION_FIFO_SIZE 10
  64. struct action_fifo {
  65. int head;
  66. int tail;
  67. /* Deferred action fifo queue storage. */
  68. struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
  69. };
  70. static struct action_fifo __percpu *action_fifos;
  71. static DEFINE_PER_CPU(int, exec_actions_level);
  72. static void action_fifo_init(struct action_fifo *fifo)
  73. {
  74. fifo->head = 0;
  75. fifo->tail = 0;
  76. }
  77. static bool action_fifo_is_empty(const struct action_fifo *fifo)
  78. {
  79. return (fifo->head == fifo->tail);
  80. }
  81. static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
  82. {
  83. if (action_fifo_is_empty(fifo))
  84. return NULL;
  85. return &fifo->fifo[fifo->tail++];
  86. }
  87. static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
  88. {
  89. if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
  90. return NULL;
  91. return &fifo->fifo[fifo->head++];
  92. }
  93. /* Return true if fifo is not full */
  94. static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
  95. const struct sw_flow_key *key,
  96. const struct nlattr *attr)
  97. {
  98. struct action_fifo *fifo;
  99. struct deferred_action *da;
  100. fifo = this_cpu_ptr(action_fifos);
  101. da = action_fifo_put(fifo);
  102. if (da) {
  103. da->skb = skb;
  104. da->actions = attr;
  105. da->pkt_key = *key;
  106. }
  107. return da;
  108. }
  109. static void invalidate_flow_key(struct sw_flow_key *key)
  110. {
  111. key->eth.type = htons(0);
  112. }
  113. static bool is_flow_key_valid(const struct sw_flow_key *key)
  114. {
  115. return !!key->eth.type;
  116. }
  117. static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
  118. const struct ovs_action_push_mpls *mpls)
  119. {
  120. __be32 *new_mpls_lse;
  121. struct ethhdr *hdr;
  122. /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
  123. if (skb->encapsulation)
  124. return -ENOTSUPP;
  125. if (skb_cow_head(skb, MPLS_HLEN) < 0)
  126. return -ENOMEM;
  127. skb_push(skb, MPLS_HLEN);
  128. memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
  129. skb->mac_len);
  130. skb_reset_mac_header(skb);
  131. new_mpls_lse = (__be32 *)skb_mpls_header(skb);
  132. *new_mpls_lse = mpls->mpls_lse;
  133. skb_postpush_rcsum(skb, new_mpls_lse, MPLS_HLEN);
  134. hdr = eth_hdr(skb);
  135. hdr->h_proto = mpls->mpls_ethertype;
  136. if (!skb->inner_protocol)
  137. skb_set_inner_protocol(skb, skb->protocol);
  138. skb->protocol = mpls->mpls_ethertype;
  139. invalidate_flow_key(key);
  140. return 0;
  141. }
  142. static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
  143. const __be16 ethertype)
  144. {
  145. struct ethhdr *hdr;
  146. int err;
  147. err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
  148. if (unlikely(err))
  149. return err;
  150. skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
  151. memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
  152. skb->mac_len);
  153. __skb_pull(skb, MPLS_HLEN);
  154. skb_reset_mac_header(skb);
  155. /* skb_mpls_header() is used to locate the ethertype
  156. * field correctly in the presence of VLAN tags.
  157. */
  158. hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
  159. hdr->h_proto = ethertype;
  160. if (eth_p_mpls(skb->protocol))
  161. skb->protocol = ethertype;
  162. invalidate_flow_key(key);
  163. return 0;
  164. }
  165. static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
  166. const __be32 *mpls_lse, const __be32 *mask)
  167. {
  168. __be32 *stack;
  169. __be32 lse;
  170. int err;
  171. err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
  172. if (unlikely(err))
  173. return err;
  174. stack = (__be32 *)skb_mpls_header(skb);
  175. lse = OVS_MASKED(*stack, *mpls_lse, *mask);
  176. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  177. __be32 diff[] = { ~(*stack), lse };
  178. skb->csum = ~csum_partial((char *)diff, sizeof(diff),
  179. ~skb->csum);
  180. }
  181. *stack = lse;
  182. flow_key->mpls.top_lse = lse;
  183. return 0;
  184. }
  185. static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  186. {
  187. int err;
  188. err = skb_vlan_pop(skb);
  189. if (skb_vlan_tag_present(skb))
  190. invalidate_flow_key(key);
  191. else
  192. key->eth.tci = 0;
  193. return err;
  194. }
  195. static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
  196. const struct ovs_action_push_vlan *vlan)
  197. {
  198. if (skb_vlan_tag_present(skb))
  199. invalidate_flow_key(key);
  200. else
  201. key->eth.tci = vlan->vlan_tci;
  202. return skb_vlan_push(skb, vlan->vlan_tpid,
  203. ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
  204. }
  205. /* 'src' is already properly masked. */
  206. static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
  207. {
  208. u16 *dst = (u16 *)dst_;
  209. const u16 *src = (const u16 *)src_;
  210. const u16 *mask = (const u16 *)mask_;
  211. OVS_SET_MASKED(dst[0], src[0], mask[0]);
  212. OVS_SET_MASKED(dst[1], src[1], mask[1]);
  213. OVS_SET_MASKED(dst[2], src[2], mask[2]);
  214. }
  215. static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
  216. const struct ovs_key_ethernet *key,
  217. const struct ovs_key_ethernet *mask)
  218. {
  219. int err;
  220. err = skb_ensure_writable(skb, ETH_HLEN);
  221. if (unlikely(err))
  222. return err;
  223. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
  224. ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
  225. mask->eth_src);
  226. ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
  227. mask->eth_dst);
  228. skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
  229. ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
  230. ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
  231. return 0;
  232. }
  233. static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
  234. __be32 addr, __be32 new_addr)
  235. {
  236. int transport_len = skb->len - skb_transport_offset(skb);
  237. if (nh->frag_off & htons(IP_OFFSET))
  238. return;
  239. if (nh->protocol == IPPROTO_TCP) {
  240. if (likely(transport_len >= sizeof(struct tcphdr)))
  241. inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
  242. addr, new_addr, true);
  243. } else if (nh->protocol == IPPROTO_UDP) {
  244. if (likely(transport_len >= sizeof(struct udphdr))) {
  245. struct udphdr *uh = udp_hdr(skb);
  246. if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  247. inet_proto_csum_replace4(&uh->check, skb,
  248. addr, new_addr, true);
  249. if (!uh->check)
  250. uh->check = CSUM_MANGLED_0;
  251. }
  252. }
  253. }
  254. }
  255. static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
  256. __be32 *addr, __be32 new_addr)
  257. {
  258. update_ip_l4_checksum(skb, nh, *addr, new_addr);
  259. csum_replace4(&nh->check, *addr, new_addr);
  260. skb_clear_hash(skb);
  261. *addr = new_addr;
  262. }
  263. static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
  264. __be32 addr[4], const __be32 new_addr[4])
  265. {
  266. int transport_len = skb->len - skb_transport_offset(skb);
  267. if (l4_proto == NEXTHDR_TCP) {
  268. if (likely(transport_len >= sizeof(struct tcphdr)))
  269. inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
  270. addr, new_addr, true);
  271. } else if (l4_proto == NEXTHDR_UDP) {
  272. if (likely(transport_len >= sizeof(struct udphdr))) {
  273. struct udphdr *uh = udp_hdr(skb);
  274. if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  275. inet_proto_csum_replace16(&uh->check, skb,
  276. addr, new_addr, true);
  277. if (!uh->check)
  278. uh->check = CSUM_MANGLED_0;
  279. }
  280. }
  281. } else if (l4_proto == NEXTHDR_ICMP) {
  282. if (likely(transport_len >= sizeof(struct icmp6hdr)))
  283. inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
  284. skb, addr, new_addr, true);
  285. }
  286. }
  287. static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
  288. const __be32 mask[4], __be32 masked[4])
  289. {
  290. masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
  291. masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
  292. masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
  293. masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
  294. }
  295. static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
  296. __be32 addr[4], const __be32 new_addr[4],
  297. bool recalculate_csum)
  298. {
  299. if (recalculate_csum)
  300. update_ipv6_checksum(skb, l4_proto, addr, new_addr);
  301. skb_clear_hash(skb);
  302. memcpy(addr, new_addr, sizeof(__be32[4]));
  303. }
  304. static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
  305. {
  306. /* Bits 21-24 are always unmasked, so this retains their values. */
  307. OVS_SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
  308. OVS_SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
  309. OVS_SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
  310. }
  311. static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
  312. u8 mask)
  313. {
  314. new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask);
  315. csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
  316. nh->ttl = new_ttl;
  317. }
  318. static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
  319. const struct ovs_key_ipv4 *key,
  320. const struct ovs_key_ipv4 *mask)
  321. {
  322. struct iphdr *nh;
  323. __be32 new_addr;
  324. int err;
  325. err = skb_ensure_writable(skb, skb_network_offset(skb) +
  326. sizeof(struct iphdr));
  327. if (unlikely(err))
  328. return err;
  329. nh = ip_hdr(skb);
  330. /* Setting an IP addresses is typically only a side effect of
  331. * matching on them in the current userspace implementation, so it
  332. * makes sense to check if the value actually changed.
  333. */
  334. if (mask->ipv4_src) {
  335. new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
  336. if (unlikely(new_addr != nh->saddr)) {
  337. set_ip_addr(skb, nh, &nh->saddr, new_addr);
  338. flow_key->ipv4.addr.src = new_addr;
  339. }
  340. }
  341. if (mask->ipv4_dst) {
  342. new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
  343. if (unlikely(new_addr != nh->daddr)) {
  344. set_ip_addr(skb, nh, &nh->daddr, new_addr);
  345. flow_key->ipv4.addr.dst = new_addr;
  346. }
  347. }
  348. if (mask->ipv4_tos) {
  349. ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
  350. flow_key->ip.tos = nh->tos;
  351. }
  352. if (mask->ipv4_ttl) {
  353. set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
  354. flow_key->ip.ttl = nh->ttl;
  355. }
  356. return 0;
  357. }
  358. static bool is_ipv6_mask_nonzero(const __be32 addr[4])
  359. {
  360. return !!(addr[0] | addr[1] | addr[2] | addr[3]);
  361. }
  362. static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
  363. const struct ovs_key_ipv6 *key,
  364. const struct ovs_key_ipv6 *mask)
  365. {
  366. struct ipv6hdr *nh;
  367. int err;
  368. err = skb_ensure_writable(skb, skb_network_offset(skb) +
  369. sizeof(struct ipv6hdr));
  370. if (unlikely(err))
  371. return err;
  372. nh = ipv6_hdr(skb);
  373. /* Setting an IP addresses is typically only a side effect of
  374. * matching on them in the current userspace implementation, so it
  375. * makes sense to check if the value actually changed.
  376. */
  377. if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
  378. __be32 *saddr = (__be32 *)&nh->saddr;
  379. __be32 masked[4];
  380. mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
  381. if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
  382. set_ipv6_addr(skb, flow_key->ip.proto, saddr, masked,
  383. true);
  384. memcpy(&flow_key->ipv6.addr.src, masked,
  385. sizeof(flow_key->ipv6.addr.src));
  386. }
  387. }
  388. if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
  389. unsigned int offset = 0;
  390. int flags = IP6_FH_F_SKIP_RH;
  391. bool recalc_csum = true;
  392. __be32 *daddr = (__be32 *)&nh->daddr;
  393. __be32 masked[4];
  394. mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
  395. if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
  396. if (ipv6_ext_hdr(nh->nexthdr))
  397. recalc_csum = (ipv6_find_hdr(skb, &offset,
  398. NEXTHDR_ROUTING,
  399. NULL, &flags)
  400. != NEXTHDR_ROUTING);
  401. set_ipv6_addr(skb, flow_key->ip.proto, daddr, masked,
  402. recalc_csum);
  403. memcpy(&flow_key->ipv6.addr.dst, masked,
  404. sizeof(flow_key->ipv6.addr.dst));
  405. }
  406. }
  407. if (mask->ipv6_tclass) {
  408. ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
  409. flow_key->ip.tos = ipv6_get_dsfield(nh);
  410. }
  411. if (mask->ipv6_label) {
  412. set_ipv6_fl(nh, ntohl(key->ipv6_label),
  413. ntohl(mask->ipv6_label));
  414. flow_key->ipv6.label =
  415. *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
  416. }
  417. if (mask->ipv6_hlimit) {
  418. OVS_SET_MASKED(nh->hop_limit, key->ipv6_hlimit,
  419. mask->ipv6_hlimit);
  420. flow_key->ip.ttl = nh->hop_limit;
  421. }
  422. return 0;
  423. }
  424. /* Must follow skb_ensure_writable() since that can move the skb data. */
  425. static void set_tp_port(struct sk_buff *skb, __be16 *port,
  426. __be16 new_port, __sum16 *check)
  427. {
  428. inet_proto_csum_replace2(check, skb, *port, new_port, false);
  429. *port = new_port;
  430. }
  431. static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  432. const struct ovs_key_udp *key,
  433. const struct ovs_key_udp *mask)
  434. {
  435. struct udphdr *uh;
  436. __be16 src, dst;
  437. int err;
  438. err = skb_ensure_writable(skb, skb_transport_offset(skb) +
  439. sizeof(struct udphdr));
  440. if (unlikely(err))
  441. return err;
  442. uh = udp_hdr(skb);
  443. /* Either of the masks is non-zero, so do not bother checking them. */
  444. src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src);
  445. dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst);
  446. if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
  447. if (likely(src != uh->source)) {
  448. set_tp_port(skb, &uh->source, src, &uh->check);
  449. flow_key->tp.src = src;
  450. }
  451. if (likely(dst != uh->dest)) {
  452. set_tp_port(skb, &uh->dest, dst, &uh->check);
  453. flow_key->tp.dst = dst;
  454. }
  455. if (unlikely(!uh->check))
  456. uh->check = CSUM_MANGLED_0;
  457. } else {
  458. uh->source = src;
  459. uh->dest = dst;
  460. flow_key->tp.src = src;
  461. flow_key->tp.dst = dst;
  462. }
  463. skb_clear_hash(skb);
  464. return 0;
  465. }
  466. static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  467. const struct ovs_key_tcp *key,
  468. const struct ovs_key_tcp *mask)
  469. {
  470. struct tcphdr *th;
  471. __be16 src, dst;
  472. int err;
  473. err = skb_ensure_writable(skb, skb_transport_offset(skb) +
  474. sizeof(struct tcphdr));
  475. if (unlikely(err))
  476. return err;
  477. th = tcp_hdr(skb);
  478. src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
  479. if (likely(src != th->source)) {
  480. set_tp_port(skb, &th->source, src, &th->check);
  481. flow_key->tp.src = src;
  482. }
  483. dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
  484. if (likely(dst != th->dest)) {
  485. set_tp_port(skb, &th->dest, dst, &th->check);
  486. flow_key->tp.dst = dst;
  487. }
  488. skb_clear_hash(skb);
  489. return 0;
  490. }
  491. static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  492. const struct ovs_key_sctp *key,
  493. const struct ovs_key_sctp *mask)
  494. {
  495. unsigned int sctphoff = skb_transport_offset(skb);
  496. struct sctphdr *sh;
  497. __le32 old_correct_csum, new_csum, old_csum;
  498. int err;
  499. err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
  500. if (unlikely(err))
  501. return err;
  502. sh = sctp_hdr(skb);
  503. old_csum = sh->checksum;
  504. old_correct_csum = sctp_compute_cksum(skb, sctphoff);
  505. sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
  506. sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
  507. new_csum = sctp_compute_cksum(skb, sctphoff);
  508. /* Carry any checksum errors through. */
  509. sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
  510. skb_clear_hash(skb);
  511. flow_key->tp.src = sh->source;
  512. flow_key->tp.dst = sh->dest;
  513. return 0;
  514. }
  515. static int ovs_vport_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  516. {
  517. struct ovs_frag_data *data = this_cpu_ptr(&ovs_frag_data_storage);
  518. struct vport *vport = data->vport;
  519. if (skb_cow_head(skb, data->l2_len) < 0) {
  520. kfree_skb(skb);
  521. return -ENOMEM;
  522. }
  523. __skb_dst_copy(skb, data->dst);
  524. *OVS_CB(skb) = data->cb;
  525. skb->inner_protocol = data->inner_protocol;
  526. skb->vlan_tci = data->vlan_tci;
  527. skb->vlan_proto = data->vlan_proto;
  528. /* Reconstruct the MAC header. */
  529. skb_push(skb, data->l2_len);
  530. memcpy(skb->data, &data->l2_data, data->l2_len);
  531. skb_postpush_rcsum(skb, skb->data, data->l2_len);
  532. skb_reset_mac_header(skb);
  533. ovs_vport_send(vport, skb);
  534. return 0;
  535. }
  536. static unsigned int
  537. ovs_dst_get_mtu(const struct dst_entry *dst)
  538. {
  539. return dst->dev->mtu;
  540. }
  541. static struct dst_ops ovs_dst_ops = {
  542. .family = AF_UNSPEC,
  543. .mtu = ovs_dst_get_mtu,
  544. };
  545. /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
  546. * ovs_vport_output(), which is called once per fragmented packet.
  547. */
  548. static void prepare_frag(struct vport *vport, struct sk_buff *skb)
  549. {
  550. unsigned int hlen = skb_network_offset(skb);
  551. struct ovs_frag_data *data;
  552. data = this_cpu_ptr(&ovs_frag_data_storage);
  553. data->dst = skb->_skb_refdst;
  554. data->vport = vport;
  555. data->cb = *OVS_CB(skb);
  556. data->inner_protocol = skb->inner_protocol;
  557. data->vlan_tci = skb->vlan_tci;
  558. data->vlan_proto = skb->vlan_proto;
  559. data->l2_len = hlen;
  560. memcpy(&data->l2_data, skb->data, hlen);
  561. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  562. skb_pull(skb, hlen);
  563. }
  564. static void ovs_fragment(struct net *net, struct vport *vport,
  565. struct sk_buff *skb, u16 mru, __be16 ethertype)
  566. {
  567. if (skb_network_offset(skb) > MAX_L2_LEN) {
  568. OVS_NLERR(1, "L2 header too long to fragment");
  569. goto err;
  570. }
  571. if (ethertype == htons(ETH_P_IP)) {
  572. struct dst_entry ovs_dst;
  573. unsigned long orig_dst;
  574. prepare_frag(vport, skb);
  575. dst_init(&ovs_dst, &ovs_dst_ops, NULL, 1,
  576. DST_OBSOLETE_NONE, DST_NOCOUNT);
  577. ovs_dst.dev = vport->dev;
  578. orig_dst = skb->_skb_refdst;
  579. skb_dst_set_noref(skb, &ovs_dst);
  580. IPCB(skb)->frag_max_size = mru;
  581. ip_do_fragment(net, skb->sk, skb, ovs_vport_output);
  582. refdst_drop(orig_dst);
  583. } else if (ethertype == htons(ETH_P_IPV6)) {
  584. const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
  585. unsigned long orig_dst;
  586. struct rt6_info ovs_rt;
  587. if (!v6ops) {
  588. goto err;
  589. }
  590. prepare_frag(vport, skb);
  591. memset(&ovs_rt, 0, sizeof(ovs_rt));
  592. dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
  593. DST_OBSOLETE_NONE, DST_NOCOUNT);
  594. ovs_rt.dst.dev = vport->dev;
  595. orig_dst = skb->_skb_refdst;
  596. skb_dst_set_noref(skb, &ovs_rt.dst);
  597. IP6CB(skb)->frag_max_size = mru;
  598. v6ops->fragment(net, skb->sk, skb, ovs_vport_output);
  599. refdst_drop(orig_dst);
  600. } else {
  601. WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
  602. ovs_vport_name(vport), ntohs(ethertype), mru,
  603. vport->dev->mtu);
  604. goto err;
  605. }
  606. return;
  607. err:
  608. kfree_skb(skb);
  609. }
  610. static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
  611. struct sw_flow_key *key)
  612. {
  613. struct vport *vport = ovs_vport_rcu(dp, out_port);
  614. if (likely(vport)) {
  615. u16 mru = OVS_CB(skb)->mru;
  616. if (likely(!mru || (skb->len <= mru + ETH_HLEN))) {
  617. ovs_vport_send(vport, skb);
  618. } else if (mru <= vport->dev->mtu) {
  619. struct net *net = read_pnet(&dp->net);
  620. __be16 ethertype = key->eth.type;
  621. if (!is_flow_key_valid(key)) {
  622. if (eth_p_mpls(skb->protocol))
  623. ethertype = skb->inner_protocol;
  624. else
  625. ethertype = vlan_get_protocol(skb);
  626. }
  627. ovs_fragment(net, vport, skb, mru, ethertype);
  628. } else {
  629. kfree_skb(skb);
  630. }
  631. } else {
  632. kfree_skb(skb);
  633. }
  634. }
  635. static int output_userspace(struct datapath *dp, struct sk_buff *skb,
  636. struct sw_flow_key *key, const struct nlattr *attr,
  637. const struct nlattr *actions, int actions_len)
  638. {
  639. struct dp_upcall_info upcall;
  640. const struct nlattr *a;
  641. int rem;
  642. memset(&upcall, 0, sizeof(upcall));
  643. upcall.cmd = OVS_PACKET_CMD_ACTION;
  644. upcall.mru = OVS_CB(skb)->mru;
  645. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  646. a = nla_next(a, &rem)) {
  647. switch (nla_type(a)) {
  648. case OVS_USERSPACE_ATTR_USERDATA:
  649. upcall.userdata = a;
  650. break;
  651. case OVS_USERSPACE_ATTR_PID:
  652. upcall.portid = nla_get_u32(a);
  653. break;
  654. case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
  655. /* Get out tunnel info. */
  656. struct vport *vport;
  657. vport = ovs_vport_rcu(dp, nla_get_u32(a));
  658. if (vport) {
  659. int err;
  660. err = dev_fill_metadata_dst(vport->dev, skb);
  661. if (!err)
  662. upcall.egress_tun_info = skb_tunnel_info(skb);
  663. }
  664. break;
  665. }
  666. case OVS_USERSPACE_ATTR_ACTIONS: {
  667. /* Include actions. */
  668. upcall.actions = actions;
  669. upcall.actions_len = actions_len;
  670. break;
  671. }
  672. } /* End of switch. */
  673. }
  674. return ovs_dp_upcall(dp, skb, key, &upcall);
  675. }
  676. static int sample(struct datapath *dp, struct sk_buff *skb,
  677. struct sw_flow_key *key, const struct nlattr *attr,
  678. const struct nlattr *actions, int actions_len)
  679. {
  680. const struct nlattr *acts_list = NULL;
  681. const struct nlattr *a;
  682. int rem;
  683. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  684. a = nla_next(a, &rem)) {
  685. u32 probability;
  686. switch (nla_type(a)) {
  687. case OVS_SAMPLE_ATTR_PROBABILITY:
  688. probability = nla_get_u32(a);
  689. if (!probability || prandom_u32() > probability)
  690. return 0;
  691. break;
  692. case OVS_SAMPLE_ATTR_ACTIONS:
  693. acts_list = a;
  694. break;
  695. }
  696. }
  697. rem = nla_len(acts_list);
  698. a = nla_data(acts_list);
  699. /* Actions list is empty, do nothing */
  700. if (unlikely(!rem))
  701. return 0;
  702. /* The only known usage of sample action is having a single user-space
  703. * action. Treat this usage as a special case.
  704. * The output_userspace() should clone the skb to be sent to the
  705. * user space. This skb will be consumed by its caller.
  706. */
  707. if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
  708. nla_is_last(a, rem)))
  709. return output_userspace(dp, skb, key, a, actions, actions_len);
  710. skb = skb_clone(skb, GFP_ATOMIC);
  711. if (!skb)
  712. /* Skip the sample action when out of memory. */
  713. return 0;
  714. if (!add_deferred_actions(skb, key, a)) {
  715. if (net_ratelimit())
  716. pr_warn("%s: deferred actions limit reached, dropping sample action\n",
  717. ovs_dp_name(dp));
  718. kfree_skb(skb);
  719. }
  720. return 0;
  721. }
  722. static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
  723. const struct nlattr *attr)
  724. {
  725. struct ovs_action_hash *hash_act = nla_data(attr);
  726. u32 hash = 0;
  727. /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
  728. hash = skb_get_hash(skb);
  729. hash = jhash_1word(hash, hash_act->hash_basis);
  730. if (!hash)
  731. hash = 0x1;
  732. key->ovs_flow_hash = hash;
  733. }
  734. static int execute_set_action(struct sk_buff *skb,
  735. struct sw_flow_key *flow_key,
  736. const struct nlattr *a)
  737. {
  738. /* Only tunnel set execution is supported without a mask. */
  739. if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
  740. struct ovs_tunnel_info *tun = nla_data(a);
  741. skb_dst_drop(skb);
  742. dst_hold((struct dst_entry *)tun->tun_dst);
  743. skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
  744. return 0;
  745. }
  746. return -EINVAL;
  747. }
  748. /* Mask is at the midpoint of the data. */
  749. #define get_mask(a, type) ((const type)nla_data(a) + 1)
  750. static int execute_masked_set_action(struct sk_buff *skb,
  751. struct sw_flow_key *flow_key,
  752. const struct nlattr *a)
  753. {
  754. int err = 0;
  755. switch (nla_type(a)) {
  756. case OVS_KEY_ATTR_PRIORITY:
  757. OVS_SET_MASKED(skb->priority, nla_get_u32(a),
  758. *get_mask(a, u32 *));
  759. flow_key->phy.priority = skb->priority;
  760. break;
  761. case OVS_KEY_ATTR_SKB_MARK:
  762. OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
  763. flow_key->phy.skb_mark = skb->mark;
  764. break;
  765. case OVS_KEY_ATTR_TUNNEL_INFO:
  766. /* Masked data not supported for tunnel. */
  767. err = -EINVAL;
  768. break;
  769. case OVS_KEY_ATTR_ETHERNET:
  770. err = set_eth_addr(skb, flow_key, nla_data(a),
  771. get_mask(a, struct ovs_key_ethernet *));
  772. break;
  773. case OVS_KEY_ATTR_IPV4:
  774. err = set_ipv4(skb, flow_key, nla_data(a),
  775. get_mask(a, struct ovs_key_ipv4 *));
  776. break;
  777. case OVS_KEY_ATTR_IPV6:
  778. err = set_ipv6(skb, flow_key, nla_data(a),
  779. get_mask(a, struct ovs_key_ipv6 *));
  780. break;
  781. case OVS_KEY_ATTR_TCP:
  782. err = set_tcp(skb, flow_key, nla_data(a),
  783. get_mask(a, struct ovs_key_tcp *));
  784. break;
  785. case OVS_KEY_ATTR_UDP:
  786. err = set_udp(skb, flow_key, nla_data(a),
  787. get_mask(a, struct ovs_key_udp *));
  788. break;
  789. case OVS_KEY_ATTR_SCTP:
  790. err = set_sctp(skb, flow_key, nla_data(a),
  791. get_mask(a, struct ovs_key_sctp *));
  792. break;
  793. case OVS_KEY_ATTR_MPLS:
  794. err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
  795. __be32 *));
  796. break;
  797. case OVS_KEY_ATTR_CT_STATE:
  798. case OVS_KEY_ATTR_CT_ZONE:
  799. case OVS_KEY_ATTR_CT_MARK:
  800. case OVS_KEY_ATTR_CT_LABELS:
  801. err = -EINVAL;
  802. break;
  803. }
  804. return err;
  805. }
  806. static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
  807. struct sw_flow_key *key,
  808. const struct nlattr *a, int rem)
  809. {
  810. struct deferred_action *da;
  811. if (!is_flow_key_valid(key)) {
  812. int err;
  813. err = ovs_flow_key_update(skb, key);
  814. if (err)
  815. return err;
  816. }
  817. BUG_ON(!is_flow_key_valid(key));
  818. if (!nla_is_last(a, rem)) {
  819. /* Recirc action is the not the last action
  820. * of the action list, need to clone the skb.
  821. */
  822. skb = skb_clone(skb, GFP_ATOMIC);
  823. /* Skip the recirc action when out of memory, but
  824. * continue on with the rest of the action list.
  825. */
  826. if (!skb)
  827. return 0;
  828. }
  829. da = add_deferred_actions(skb, key, NULL);
  830. if (da) {
  831. da->pkt_key.recirc_id = nla_get_u32(a);
  832. } else {
  833. kfree_skb(skb);
  834. if (net_ratelimit())
  835. pr_warn("%s: deferred action limit reached, drop recirc action\n",
  836. ovs_dp_name(dp));
  837. }
  838. return 0;
  839. }
  840. /* Execute a list of actions against 'skb'. */
  841. static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
  842. struct sw_flow_key *key,
  843. const struct nlattr *attr, int len)
  844. {
  845. /* Every output action needs a separate clone of 'skb', but the common
  846. * case is just a single output action, so that doing a clone and
  847. * then freeing the original skbuff is wasteful. So the following code
  848. * is slightly obscure just to avoid that.
  849. */
  850. int prev_port = -1;
  851. const struct nlattr *a;
  852. int rem;
  853. for (a = attr, rem = len; rem > 0;
  854. a = nla_next(a, &rem)) {
  855. int err = 0;
  856. if (unlikely(prev_port != -1)) {
  857. struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
  858. if (out_skb)
  859. do_output(dp, out_skb, prev_port, key);
  860. prev_port = -1;
  861. }
  862. switch (nla_type(a)) {
  863. case OVS_ACTION_ATTR_OUTPUT:
  864. prev_port = nla_get_u32(a);
  865. break;
  866. case OVS_ACTION_ATTR_USERSPACE:
  867. output_userspace(dp, skb, key, a, attr, len);
  868. break;
  869. case OVS_ACTION_ATTR_HASH:
  870. execute_hash(skb, key, a);
  871. break;
  872. case OVS_ACTION_ATTR_PUSH_MPLS:
  873. err = push_mpls(skb, key, nla_data(a));
  874. break;
  875. case OVS_ACTION_ATTR_POP_MPLS:
  876. err = pop_mpls(skb, key, nla_get_be16(a));
  877. break;
  878. case OVS_ACTION_ATTR_PUSH_VLAN:
  879. err = push_vlan(skb, key, nla_data(a));
  880. break;
  881. case OVS_ACTION_ATTR_POP_VLAN:
  882. err = pop_vlan(skb, key);
  883. break;
  884. case OVS_ACTION_ATTR_RECIRC:
  885. err = execute_recirc(dp, skb, key, a, rem);
  886. if (nla_is_last(a, rem)) {
  887. /* If this is the last action, the skb has
  888. * been consumed or freed.
  889. * Return immediately.
  890. */
  891. return err;
  892. }
  893. break;
  894. case OVS_ACTION_ATTR_SET:
  895. err = execute_set_action(skb, key, nla_data(a));
  896. break;
  897. case OVS_ACTION_ATTR_SET_MASKED:
  898. case OVS_ACTION_ATTR_SET_TO_MASKED:
  899. err = execute_masked_set_action(skb, key, nla_data(a));
  900. break;
  901. case OVS_ACTION_ATTR_SAMPLE:
  902. err = sample(dp, skb, key, a, attr, len);
  903. break;
  904. case OVS_ACTION_ATTR_CT:
  905. if (!is_flow_key_valid(key)) {
  906. err = ovs_flow_key_update(skb, key);
  907. if (err)
  908. return err;
  909. }
  910. err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
  911. nla_data(a));
  912. /* Hide stolen IP fragments from user space. */
  913. if (err)
  914. return err == -EINPROGRESS ? 0 : err;
  915. break;
  916. }
  917. if (unlikely(err)) {
  918. kfree_skb(skb);
  919. return err;
  920. }
  921. }
  922. if (prev_port != -1)
  923. do_output(dp, skb, prev_port, key);
  924. else
  925. consume_skb(skb);
  926. return 0;
  927. }
  928. static void process_deferred_actions(struct datapath *dp)
  929. {
  930. struct action_fifo *fifo = this_cpu_ptr(action_fifos);
  931. /* Do not touch the FIFO in case there is no deferred actions. */
  932. if (action_fifo_is_empty(fifo))
  933. return;
  934. /* Finishing executing all deferred actions. */
  935. do {
  936. struct deferred_action *da = action_fifo_get(fifo);
  937. struct sk_buff *skb = da->skb;
  938. struct sw_flow_key *key = &da->pkt_key;
  939. const struct nlattr *actions = da->actions;
  940. if (actions)
  941. do_execute_actions(dp, skb, key, actions,
  942. nla_len(actions));
  943. else
  944. ovs_dp_process_packet(skb, key);
  945. } while (!action_fifo_is_empty(fifo));
  946. /* Reset FIFO for the next packet. */
  947. action_fifo_init(fifo);
  948. }
  949. /* Execute a list of actions against 'skb'. */
  950. int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
  951. const struct sw_flow_actions *acts,
  952. struct sw_flow_key *key)
  953. {
  954. int level = this_cpu_read(exec_actions_level);
  955. int err;
  956. this_cpu_inc(exec_actions_level);
  957. err = do_execute_actions(dp, skb, key,
  958. acts->actions, acts->actions_len);
  959. if (!level)
  960. process_deferred_actions(dp);
  961. this_cpu_dec(exec_actions_level);
  962. return err;
  963. }
  964. int action_fifos_init(void)
  965. {
  966. action_fifos = alloc_percpu(struct action_fifo);
  967. if (!action_fifos)
  968. return -ENOMEM;
  969. return 0;
  970. }
  971. void action_fifos_exit(void)
  972. {
  973. free_percpu(action_fifos);
  974. }