actions.c 29 KB

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