actions.c 30 KB

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