actions.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  1. /*
  2. * Copyright (c) 2007-2017 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. #include "flow_netlink.h"
  43. struct deferred_action {
  44. struct sk_buff *skb;
  45. const struct nlattr *actions;
  46. int actions_len;
  47. /* Store pkt_key clone when creating deferred action. */
  48. struct sw_flow_key pkt_key;
  49. };
  50. #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
  51. struct ovs_frag_data {
  52. unsigned long dst;
  53. struct vport *vport;
  54. struct ovs_skb_cb cb;
  55. __be16 inner_protocol;
  56. u16 network_offset; /* valid only for MPLS */
  57. u16 vlan_tci;
  58. __be16 vlan_proto;
  59. unsigned int l2_len;
  60. u8 mac_proto;
  61. u8 l2_data[MAX_L2_LEN];
  62. };
  63. static DEFINE_PER_CPU(struct ovs_frag_data, ovs_frag_data_storage);
  64. #define DEFERRED_ACTION_FIFO_SIZE 10
  65. #define OVS_RECURSION_LIMIT 5
  66. #define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2)
  67. struct action_fifo {
  68. int head;
  69. int tail;
  70. /* Deferred action fifo queue storage. */
  71. struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
  72. };
  73. struct action_flow_keys {
  74. struct sw_flow_key key[OVS_DEFERRED_ACTION_THRESHOLD];
  75. };
  76. static struct action_fifo __percpu *action_fifos;
  77. static struct action_flow_keys __percpu *flow_keys;
  78. static DEFINE_PER_CPU(int, exec_actions_level);
  79. /* Make a clone of the 'key', using the pre-allocated percpu 'flow_keys'
  80. * space. Return NULL if out of key spaces.
  81. */
  82. static struct sw_flow_key *clone_key(const struct sw_flow_key *key_)
  83. {
  84. struct action_flow_keys *keys = this_cpu_ptr(flow_keys);
  85. int level = this_cpu_read(exec_actions_level);
  86. struct sw_flow_key *key = NULL;
  87. if (level <= OVS_DEFERRED_ACTION_THRESHOLD) {
  88. key = &keys->key[level - 1];
  89. *key = *key_;
  90. }
  91. return key;
  92. }
  93. static void action_fifo_init(struct action_fifo *fifo)
  94. {
  95. fifo->head = 0;
  96. fifo->tail = 0;
  97. }
  98. static bool action_fifo_is_empty(const struct action_fifo *fifo)
  99. {
  100. return (fifo->head == fifo->tail);
  101. }
  102. static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
  103. {
  104. if (action_fifo_is_empty(fifo))
  105. return NULL;
  106. return &fifo->fifo[fifo->tail++];
  107. }
  108. static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
  109. {
  110. if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
  111. return NULL;
  112. return &fifo->fifo[fifo->head++];
  113. }
  114. /* Return true if fifo is not full */
  115. static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
  116. const struct sw_flow_key *key,
  117. const struct nlattr *actions,
  118. const int actions_len)
  119. {
  120. struct action_fifo *fifo;
  121. struct deferred_action *da;
  122. fifo = this_cpu_ptr(action_fifos);
  123. da = action_fifo_put(fifo);
  124. if (da) {
  125. da->skb = skb;
  126. da->actions = actions;
  127. da->actions_len = actions_len;
  128. da->pkt_key = *key;
  129. }
  130. return da;
  131. }
  132. static void invalidate_flow_key(struct sw_flow_key *key)
  133. {
  134. key->mac_proto |= SW_FLOW_KEY_INVALID;
  135. }
  136. static bool is_flow_key_valid(const struct sw_flow_key *key)
  137. {
  138. return !(key->mac_proto & SW_FLOW_KEY_INVALID);
  139. }
  140. static int clone_execute(struct datapath *dp, struct sk_buff *skb,
  141. struct sw_flow_key *key,
  142. u32 recirc_id,
  143. const struct nlattr *actions, int len,
  144. bool last, bool clone_flow_key);
  145. static void update_ethertype(struct sk_buff *skb, struct ethhdr *hdr,
  146. __be16 ethertype)
  147. {
  148. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  149. __be16 diff[] = { ~(hdr->h_proto), ethertype };
  150. skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
  151. }
  152. hdr->h_proto = ethertype;
  153. }
  154. static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
  155. const struct ovs_action_push_mpls *mpls)
  156. {
  157. struct mpls_shim_hdr *new_mpls_lse;
  158. /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
  159. if (skb->encapsulation)
  160. return -ENOTSUPP;
  161. if (skb_cow_head(skb, MPLS_HLEN) < 0)
  162. return -ENOMEM;
  163. if (!skb->inner_protocol) {
  164. skb_set_inner_network_header(skb, skb->mac_len);
  165. skb_set_inner_protocol(skb, skb->protocol);
  166. }
  167. skb_push(skb, MPLS_HLEN);
  168. memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
  169. skb->mac_len);
  170. skb_reset_mac_header(skb);
  171. skb_set_network_header(skb, skb->mac_len);
  172. new_mpls_lse = mpls_hdr(skb);
  173. new_mpls_lse->label_stack_entry = mpls->mpls_lse;
  174. skb_postpush_rcsum(skb, new_mpls_lse, MPLS_HLEN);
  175. if (ovs_key_mac_proto(key) == MAC_PROTO_ETHERNET)
  176. update_ethertype(skb, eth_hdr(skb), mpls->mpls_ethertype);
  177. skb->protocol = mpls->mpls_ethertype;
  178. invalidate_flow_key(key);
  179. return 0;
  180. }
  181. static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
  182. const __be16 ethertype)
  183. {
  184. int err;
  185. err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
  186. if (unlikely(err))
  187. return err;
  188. skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
  189. memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
  190. skb->mac_len);
  191. __skb_pull(skb, MPLS_HLEN);
  192. skb_reset_mac_header(skb);
  193. skb_set_network_header(skb, skb->mac_len);
  194. if (ovs_key_mac_proto(key) == MAC_PROTO_ETHERNET) {
  195. struct ethhdr *hdr;
  196. /* mpls_hdr() is used to locate the ethertype field correctly in the
  197. * presence of VLAN tags.
  198. */
  199. hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
  200. update_ethertype(skb, hdr, ethertype);
  201. }
  202. if (eth_p_mpls(skb->protocol))
  203. skb->protocol = ethertype;
  204. invalidate_flow_key(key);
  205. return 0;
  206. }
  207. static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
  208. const __be32 *mpls_lse, const __be32 *mask)
  209. {
  210. struct mpls_shim_hdr *stack;
  211. __be32 lse;
  212. int err;
  213. err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
  214. if (unlikely(err))
  215. return err;
  216. stack = mpls_hdr(skb);
  217. lse = OVS_MASKED(stack->label_stack_entry, *mpls_lse, *mask);
  218. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  219. __be32 diff[] = { ~(stack->label_stack_entry), lse };
  220. skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
  221. }
  222. stack->label_stack_entry = lse;
  223. flow_key->mpls.top_lse = lse;
  224. return 0;
  225. }
  226. static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  227. {
  228. int err;
  229. err = skb_vlan_pop(skb);
  230. if (skb_vlan_tag_present(skb)) {
  231. invalidate_flow_key(key);
  232. } else {
  233. key->eth.vlan.tci = 0;
  234. key->eth.vlan.tpid = 0;
  235. }
  236. return err;
  237. }
  238. static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
  239. const struct ovs_action_push_vlan *vlan)
  240. {
  241. if (skb_vlan_tag_present(skb)) {
  242. invalidate_flow_key(key);
  243. } else {
  244. key->eth.vlan.tci = vlan->vlan_tci;
  245. key->eth.vlan.tpid = vlan->vlan_tpid;
  246. }
  247. return skb_vlan_push(skb, vlan->vlan_tpid,
  248. ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
  249. }
  250. /* 'src' is already properly masked. */
  251. static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
  252. {
  253. u16 *dst = (u16 *)dst_;
  254. const u16 *src = (const u16 *)src_;
  255. const u16 *mask = (const u16 *)mask_;
  256. OVS_SET_MASKED(dst[0], src[0], mask[0]);
  257. OVS_SET_MASKED(dst[1], src[1], mask[1]);
  258. OVS_SET_MASKED(dst[2], src[2], mask[2]);
  259. }
  260. static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
  261. const struct ovs_key_ethernet *key,
  262. const struct ovs_key_ethernet *mask)
  263. {
  264. int err;
  265. err = skb_ensure_writable(skb, ETH_HLEN);
  266. if (unlikely(err))
  267. return err;
  268. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
  269. ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
  270. mask->eth_src);
  271. ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
  272. mask->eth_dst);
  273. skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
  274. ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
  275. ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
  276. return 0;
  277. }
  278. /* pop_eth does not support VLAN packets as this action is never called
  279. * for them.
  280. */
  281. static int pop_eth(struct sk_buff *skb, struct sw_flow_key *key)
  282. {
  283. skb_pull_rcsum(skb, ETH_HLEN);
  284. skb_reset_mac_header(skb);
  285. skb_reset_mac_len(skb);
  286. /* safe right before invalidate_flow_key */
  287. key->mac_proto = MAC_PROTO_NONE;
  288. invalidate_flow_key(key);
  289. return 0;
  290. }
  291. static int push_eth(struct sk_buff *skb, struct sw_flow_key *key,
  292. const struct ovs_action_push_eth *ethh)
  293. {
  294. struct ethhdr *hdr;
  295. /* Add the new Ethernet header */
  296. if (skb_cow_head(skb, ETH_HLEN) < 0)
  297. return -ENOMEM;
  298. skb_push(skb, ETH_HLEN);
  299. skb_reset_mac_header(skb);
  300. skb_reset_mac_len(skb);
  301. hdr = eth_hdr(skb);
  302. ether_addr_copy(hdr->h_source, ethh->addresses.eth_src);
  303. ether_addr_copy(hdr->h_dest, ethh->addresses.eth_dst);
  304. hdr->h_proto = skb->protocol;
  305. skb_postpush_rcsum(skb, hdr, ETH_HLEN);
  306. /* safe right before invalidate_flow_key */
  307. key->mac_proto = MAC_PROTO_ETHERNET;
  308. invalidate_flow_key(key);
  309. return 0;
  310. }
  311. static int push_nsh(struct sk_buff *skb, struct sw_flow_key *key,
  312. const struct nshhdr *nh)
  313. {
  314. int err;
  315. err = nsh_push(skb, nh);
  316. if (err)
  317. return err;
  318. /* safe right before invalidate_flow_key */
  319. key->mac_proto = MAC_PROTO_NONE;
  320. invalidate_flow_key(key);
  321. return 0;
  322. }
  323. static int pop_nsh(struct sk_buff *skb, struct sw_flow_key *key)
  324. {
  325. int err;
  326. err = nsh_pop(skb);
  327. if (err)
  328. return err;
  329. /* safe right before invalidate_flow_key */
  330. if (skb->protocol == htons(ETH_P_TEB))
  331. key->mac_proto = MAC_PROTO_ETHERNET;
  332. else
  333. key->mac_proto = MAC_PROTO_NONE;
  334. invalidate_flow_key(key);
  335. return 0;
  336. }
  337. static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
  338. __be32 addr, __be32 new_addr)
  339. {
  340. int transport_len = skb->len - skb_transport_offset(skb);
  341. if (nh->frag_off & htons(IP_OFFSET))
  342. return;
  343. if (nh->protocol == IPPROTO_TCP) {
  344. if (likely(transport_len >= sizeof(struct tcphdr)))
  345. inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
  346. addr, new_addr, true);
  347. } else if (nh->protocol == IPPROTO_UDP) {
  348. if (likely(transport_len >= sizeof(struct udphdr))) {
  349. struct udphdr *uh = udp_hdr(skb);
  350. if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  351. inet_proto_csum_replace4(&uh->check, skb,
  352. addr, new_addr, true);
  353. if (!uh->check)
  354. uh->check = CSUM_MANGLED_0;
  355. }
  356. }
  357. }
  358. }
  359. static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
  360. __be32 *addr, __be32 new_addr)
  361. {
  362. update_ip_l4_checksum(skb, nh, *addr, new_addr);
  363. csum_replace4(&nh->check, *addr, new_addr);
  364. skb_clear_hash(skb);
  365. *addr = new_addr;
  366. }
  367. static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
  368. __be32 addr[4], const __be32 new_addr[4])
  369. {
  370. int transport_len = skb->len - skb_transport_offset(skb);
  371. if (l4_proto == NEXTHDR_TCP) {
  372. if (likely(transport_len >= sizeof(struct tcphdr)))
  373. inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
  374. addr, new_addr, true);
  375. } else if (l4_proto == NEXTHDR_UDP) {
  376. if (likely(transport_len >= sizeof(struct udphdr))) {
  377. struct udphdr *uh = udp_hdr(skb);
  378. if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  379. inet_proto_csum_replace16(&uh->check, skb,
  380. addr, new_addr, true);
  381. if (!uh->check)
  382. uh->check = CSUM_MANGLED_0;
  383. }
  384. }
  385. } else if (l4_proto == NEXTHDR_ICMP) {
  386. if (likely(transport_len >= sizeof(struct icmp6hdr)))
  387. inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
  388. skb, addr, new_addr, true);
  389. }
  390. }
  391. static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
  392. const __be32 mask[4], __be32 masked[4])
  393. {
  394. masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
  395. masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
  396. masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
  397. masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
  398. }
  399. static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
  400. __be32 addr[4], const __be32 new_addr[4],
  401. bool recalculate_csum)
  402. {
  403. if (recalculate_csum)
  404. update_ipv6_checksum(skb, l4_proto, addr, new_addr);
  405. skb_clear_hash(skb);
  406. memcpy(addr, new_addr, sizeof(__be32[4]));
  407. }
  408. static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
  409. {
  410. /* Bits 21-24 are always unmasked, so this retains their values. */
  411. OVS_SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
  412. OVS_SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
  413. OVS_SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
  414. }
  415. static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
  416. u8 mask)
  417. {
  418. new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask);
  419. csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
  420. nh->ttl = new_ttl;
  421. }
  422. static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
  423. const struct ovs_key_ipv4 *key,
  424. const struct ovs_key_ipv4 *mask)
  425. {
  426. struct iphdr *nh;
  427. __be32 new_addr;
  428. int err;
  429. err = skb_ensure_writable(skb, skb_network_offset(skb) +
  430. sizeof(struct iphdr));
  431. if (unlikely(err))
  432. return err;
  433. nh = ip_hdr(skb);
  434. /* Setting an IP addresses is typically only a side effect of
  435. * matching on them in the current userspace implementation, so it
  436. * makes sense to check if the value actually changed.
  437. */
  438. if (mask->ipv4_src) {
  439. new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
  440. if (unlikely(new_addr != nh->saddr)) {
  441. set_ip_addr(skb, nh, &nh->saddr, new_addr);
  442. flow_key->ipv4.addr.src = new_addr;
  443. }
  444. }
  445. if (mask->ipv4_dst) {
  446. new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
  447. if (unlikely(new_addr != nh->daddr)) {
  448. set_ip_addr(skb, nh, &nh->daddr, new_addr);
  449. flow_key->ipv4.addr.dst = new_addr;
  450. }
  451. }
  452. if (mask->ipv4_tos) {
  453. ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
  454. flow_key->ip.tos = nh->tos;
  455. }
  456. if (mask->ipv4_ttl) {
  457. set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
  458. flow_key->ip.ttl = nh->ttl;
  459. }
  460. return 0;
  461. }
  462. static bool is_ipv6_mask_nonzero(const __be32 addr[4])
  463. {
  464. return !!(addr[0] | addr[1] | addr[2] | addr[3]);
  465. }
  466. static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
  467. const struct ovs_key_ipv6 *key,
  468. const struct ovs_key_ipv6 *mask)
  469. {
  470. struct ipv6hdr *nh;
  471. int err;
  472. err = skb_ensure_writable(skb, skb_network_offset(skb) +
  473. sizeof(struct ipv6hdr));
  474. if (unlikely(err))
  475. return err;
  476. nh = ipv6_hdr(skb);
  477. /* Setting an IP addresses is typically only a side effect of
  478. * matching on them in the current userspace implementation, so it
  479. * makes sense to check if the value actually changed.
  480. */
  481. if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
  482. __be32 *saddr = (__be32 *)&nh->saddr;
  483. __be32 masked[4];
  484. mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
  485. if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
  486. set_ipv6_addr(skb, flow_key->ip.proto, saddr, masked,
  487. true);
  488. memcpy(&flow_key->ipv6.addr.src, masked,
  489. sizeof(flow_key->ipv6.addr.src));
  490. }
  491. }
  492. if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
  493. unsigned int offset = 0;
  494. int flags = IP6_FH_F_SKIP_RH;
  495. bool recalc_csum = true;
  496. __be32 *daddr = (__be32 *)&nh->daddr;
  497. __be32 masked[4];
  498. mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
  499. if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
  500. if (ipv6_ext_hdr(nh->nexthdr))
  501. recalc_csum = (ipv6_find_hdr(skb, &offset,
  502. NEXTHDR_ROUTING,
  503. NULL, &flags)
  504. != NEXTHDR_ROUTING);
  505. set_ipv6_addr(skb, flow_key->ip.proto, daddr, masked,
  506. recalc_csum);
  507. memcpy(&flow_key->ipv6.addr.dst, masked,
  508. sizeof(flow_key->ipv6.addr.dst));
  509. }
  510. }
  511. if (mask->ipv6_tclass) {
  512. ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
  513. flow_key->ip.tos = ipv6_get_dsfield(nh);
  514. }
  515. if (mask->ipv6_label) {
  516. set_ipv6_fl(nh, ntohl(key->ipv6_label),
  517. ntohl(mask->ipv6_label));
  518. flow_key->ipv6.label =
  519. *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
  520. }
  521. if (mask->ipv6_hlimit) {
  522. OVS_SET_MASKED(nh->hop_limit, key->ipv6_hlimit,
  523. mask->ipv6_hlimit);
  524. flow_key->ip.ttl = nh->hop_limit;
  525. }
  526. return 0;
  527. }
  528. static int set_nsh(struct sk_buff *skb, struct sw_flow_key *flow_key,
  529. const struct nlattr *a)
  530. {
  531. struct nshhdr *nh;
  532. size_t length;
  533. int err;
  534. u8 flags;
  535. u8 ttl;
  536. int i;
  537. struct ovs_key_nsh key;
  538. struct ovs_key_nsh mask;
  539. err = nsh_key_from_nlattr(a, &key, &mask);
  540. if (err)
  541. return err;
  542. /* Make sure the NSH base header is there */
  543. if (!pskb_may_pull(skb, skb_network_offset(skb) + NSH_BASE_HDR_LEN))
  544. return -ENOMEM;
  545. nh = nsh_hdr(skb);
  546. length = nsh_hdr_len(nh);
  547. /* Make sure the whole NSH header is there */
  548. err = skb_ensure_writable(skb, skb_network_offset(skb) +
  549. length);
  550. if (unlikely(err))
  551. return err;
  552. nh = nsh_hdr(skb);
  553. skb_postpull_rcsum(skb, nh, length);
  554. flags = nsh_get_flags(nh);
  555. flags = OVS_MASKED(flags, key.base.flags, mask.base.flags);
  556. flow_key->nsh.base.flags = flags;
  557. ttl = nsh_get_ttl(nh);
  558. ttl = OVS_MASKED(ttl, key.base.ttl, mask.base.ttl);
  559. flow_key->nsh.base.ttl = ttl;
  560. nsh_set_flags_and_ttl(nh, flags, ttl);
  561. nh->path_hdr = OVS_MASKED(nh->path_hdr, key.base.path_hdr,
  562. mask.base.path_hdr);
  563. flow_key->nsh.base.path_hdr = nh->path_hdr;
  564. switch (nh->mdtype) {
  565. case NSH_M_TYPE1:
  566. for (i = 0; i < NSH_MD1_CONTEXT_SIZE; i++) {
  567. nh->md1.context[i] =
  568. OVS_MASKED(nh->md1.context[i], key.context[i],
  569. mask.context[i]);
  570. }
  571. memcpy(flow_key->nsh.context, nh->md1.context,
  572. sizeof(nh->md1.context));
  573. break;
  574. case NSH_M_TYPE2:
  575. memset(flow_key->nsh.context, 0,
  576. sizeof(flow_key->nsh.context));
  577. break;
  578. default:
  579. return -EINVAL;
  580. }
  581. skb_postpush_rcsum(skb, nh, length);
  582. return 0;
  583. }
  584. /* Must follow skb_ensure_writable() since that can move the skb data. */
  585. static void set_tp_port(struct sk_buff *skb, __be16 *port,
  586. __be16 new_port, __sum16 *check)
  587. {
  588. inet_proto_csum_replace2(check, skb, *port, new_port, false);
  589. *port = new_port;
  590. }
  591. static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  592. const struct ovs_key_udp *key,
  593. const struct ovs_key_udp *mask)
  594. {
  595. struct udphdr *uh;
  596. __be16 src, dst;
  597. int err;
  598. err = skb_ensure_writable(skb, skb_transport_offset(skb) +
  599. sizeof(struct udphdr));
  600. if (unlikely(err))
  601. return err;
  602. uh = udp_hdr(skb);
  603. /* Either of the masks is non-zero, so do not bother checking them. */
  604. src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src);
  605. dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst);
  606. if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
  607. if (likely(src != uh->source)) {
  608. set_tp_port(skb, &uh->source, src, &uh->check);
  609. flow_key->tp.src = src;
  610. }
  611. if (likely(dst != uh->dest)) {
  612. set_tp_port(skb, &uh->dest, dst, &uh->check);
  613. flow_key->tp.dst = dst;
  614. }
  615. if (unlikely(!uh->check))
  616. uh->check = CSUM_MANGLED_0;
  617. } else {
  618. uh->source = src;
  619. uh->dest = dst;
  620. flow_key->tp.src = src;
  621. flow_key->tp.dst = dst;
  622. }
  623. skb_clear_hash(skb);
  624. return 0;
  625. }
  626. static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  627. const struct ovs_key_tcp *key,
  628. const struct ovs_key_tcp *mask)
  629. {
  630. struct tcphdr *th;
  631. __be16 src, dst;
  632. int err;
  633. err = skb_ensure_writable(skb, skb_transport_offset(skb) +
  634. sizeof(struct tcphdr));
  635. if (unlikely(err))
  636. return err;
  637. th = tcp_hdr(skb);
  638. src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
  639. if (likely(src != th->source)) {
  640. set_tp_port(skb, &th->source, src, &th->check);
  641. flow_key->tp.src = src;
  642. }
  643. dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
  644. if (likely(dst != th->dest)) {
  645. set_tp_port(skb, &th->dest, dst, &th->check);
  646. flow_key->tp.dst = dst;
  647. }
  648. skb_clear_hash(skb);
  649. return 0;
  650. }
  651. static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  652. const struct ovs_key_sctp *key,
  653. const struct ovs_key_sctp *mask)
  654. {
  655. unsigned int sctphoff = skb_transport_offset(skb);
  656. struct sctphdr *sh;
  657. __le32 old_correct_csum, new_csum, old_csum;
  658. int err;
  659. err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
  660. if (unlikely(err))
  661. return err;
  662. sh = sctp_hdr(skb);
  663. old_csum = sh->checksum;
  664. old_correct_csum = sctp_compute_cksum(skb, sctphoff);
  665. sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
  666. sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
  667. new_csum = sctp_compute_cksum(skb, sctphoff);
  668. /* Carry any checksum errors through. */
  669. sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
  670. skb_clear_hash(skb);
  671. flow_key->tp.src = sh->source;
  672. flow_key->tp.dst = sh->dest;
  673. return 0;
  674. }
  675. static int ovs_vport_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  676. {
  677. struct ovs_frag_data *data = this_cpu_ptr(&ovs_frag_data_storage);
  678. struct vport *vport = data->vport;
  679. if (skb_cow_head(skb, data->l2_len) < 0) {
  680. kfree_skb(skb);
  681. return -ENOMEM;
  682. }
  683. __skb_dst_copy(skb, data->dst);
  684. *OVS_CB(skb) = data->cb;
  685. skb->inner_protocol = data->inner_protocol;
  686. skb->vlan_tci = data->vlan_tci;
  687. skb->vlan_proto = data->vlan_proto;
  688. /* Reconstruct the MAC header. */
  689. skb_push(skb, data->l2_len);
  690. memcpy(skb->data, &data->l2_data, data->l2_len);
  691. skb_postpush_rcsum(skb, skb->data, data->l2_len);
  692. skb_reset_mac_header(skb);
  693. if (eth_p_mpls(skb->protocol)) {
  694. skb->inner_network_header = skb->network_header;
  695. skb_set_network_header(skb, data->network_offset);
  696. skb_reset_mac_len(skb);
  697. }
  698. ovs_vport_send(vport, skb, data->mac_proto);
  699. return 0;
  700. }
  701. static unsigned int
  702. ovs_dst_get_mtu(const struct dst_entry *dst)
  703. {
  704. return dst->dev->mtu;
  705. }
  706. static struct dst_ops ovs_dst_ops = {
  707. .family = AF_UNSPEC,
  708. .mtu = ovs_dst_get_mtu,
  709. };
  710. /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
  711. * ovs_vport_output(), which is called once per fragmented packet.
  712. */
  713. static void prepare_frag(struct vport *vport, struct sk_buff *skb,
  714. u16 orig_network_offset, u8 mac_proto)
  715. {
  716. unsigned int hlen = skb_network_offset(skb);
  717. struct ovs_frag_data *data;
  718. data = this_cpu_ptr(&ovs_frag_data_storage);
  719. data->dst = skb->_skb_refdst;
  720. data->vport = vport;
  721. data->cb = *OVS_CB(skb);
  722. data->inner_protocol = skb->inner_protocol;
  723. data->network_offset = orig_network_offset;
  724. data->vlan_tci = skb->vlan_tci;
  725. data->vlan_proto = skb->vlan_proto;
  726. data->mac_proto = mac_proto;
  727. data->l2_len = hlen;
  728. memcpy(&data->l2_data, skb->data, hlen);
  729. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  730. skb_pull(skb, hlen);
  731. }
  732. static void ovs_fragment(struct net *net, struct vport *vport,
  733. struct sk_buff *skb, u16 mru,
  734. struct sw_flow_key *key)
  735. {
  736. u16 orig_network_offset = 0;
  737. if (eth_p_mpls(skb->protocol)) {
  738. orig_network_offset = skb_network_offset(skb);
  739. skb->network_header = skb->inner_network_header;
  740. }
  741. if (skb_network_offset(skb) > MAX_L2_LEN) {
  742. OVS_NLERR(1, "L2 header too long to fragment");
  743. goto err;
  744. }
  745. if (key->eth.type == htons(ETH_P_IP)) {
  746. struct rtable ovs_rt = { 0 };
  747. unsigned long orig_dst;
  748. prepare_frag(vport, skb, orig_network_offset,
  749. ovs_key_mac_proto(key));
  750. dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
  751. DST_OBSOLETE_NONE, DST_NOCOUNT);
  752. ovs_rt.dst.dev = vport->dev;
  753. orig_dst = skb->_skb_refdst;
  754. skb_dst_set_noref(skb, &ovs_rt.dst);
  755. IPCB(skb)->frag_max_size = mru;
  756. ip_do_fragment(net, skb->sk, skb, ovs_vport_output);
  757. refdst_drop(orig_dst);
  758. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  759. const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
  760. unsigned long orig_dst;
  761. struct rt6_info ovs_rt;
  762. if (!v6ops)
  763. goto err;
  764. prepare_frag(vport, skb, orig_network_offset,
  765. ovs_key_mac_proto(key));
  766. memset(&ovs_rt, 0, sizeof(ovs_rt));
  767. dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
  768. DST_OBSOLETE_NONE, DST_NOCOUNT);
  769. ovs_rt.dst.dev = vport->dev;
  770. orig_dst = skb->_skb_refdst;
  771. skb_dst_set_noref(skb, &ovs_rt.dst);
  772. IP6CB(skb)->frag_max_size = mru;
  773. v6ops->fragment(net, skb->sk, skb, ovs_vport_output);
  774. refdst_drop(orig_dst);
  775. } else {
  776. WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
  777. ovs_vport_name(vport), ntohs(key->eth.type), mru,
  778. vport->dev->mtu);
  779. goto err;
  780. }
  781. return;
  782. err:
  783. kfree_skb(skb);
  784. }
  785. static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
  786. struct sw_flow_key *key)
  787. {
  788. struct vport *vport = ovs_vport_rcu(dp, out_port);
  789. if (likely(vport)) {
  790. u16 mru = OVS_CB(skb)->mru;
  791. u32 cutlen = OVS_CB(skb)->cutlen;
  792. if (unlikely(cutlen > 0)) {
  793. if (skb->len - cutlen > ovs_mac_header_len(key))
  794. pskb_trim(skb, skb->len - cutlen);
  795. else
  796. pskb_trim(skb, ovs_mac_header_len(key));
  797. }
  798. if (likely(!mru ||
  799. (skb->len <= mru + vport->dev->hard_header_len))) {
  800. ovs_vport_send(vport, skb, ovs_key_mac_proto(key));
  801. } else if (mru <= vport->dev->mtu) {
  802. struct net *net = read_pnet(&dp->net);
  803. ovs_fragment(net, vport, skb, mru, key);
  804. } else {
  805. kfree_skb(skb);
  806. }
  807. } else {
  808. kfree_skb(skb);
  809. }
  810. }
  811. static int output_userspace(struct datapath *dp, struct sk_buff *skb,
  812. struct sw_flow_key *key, const struct nlattr *attr,
  813. const struct nlattr *actions, int actions_len,
  814. uint32_t cutlen)
  815. {
  816. struct dp_upcall_info upcall;
  817. const struct nlattr *a;
  818. int rem;
  819. memset(&upcall, 0, sizeof(upcall));
  820. upcall.cmd = OVS_PACKET_CMD_ACTION;
  821. upcall.mru = OVS_CB(skb)->mru;
  822. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  823. a = nla_next(a, &rem)) {
  824. switch (nla_type(a)) {
  825. case OVS_USERSPACE_ATTR_USERDATA:
  826. upcall.userdata = a;
  827. break;
  828. case OVS_USERSPACE_ATTR_PID:
  829. upcall.portid = nla_get_u32(a);
  830. break;
  831. case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
  832. /* Get out tunnel info. */
  833. struct vport *vport;
  834. vport = ovs_vport_rcu(dp, nla_get_u32(a));
  835. if (vport) {
  836. int err;
  837. err = dev_fill_metadata_dst(vport->dev, skb);
  838. if (!err)
  839. upcall.egress_tun_info = skb_tunnel_info(skb);
  840. }
  841. break;
  842. }
  843. case OVS_USERSPACE_ATTR_ACTIONS: {
  844. /* Include actions. */
  845. upcall.actions = actions;
  846. upcall.actions_len = actions_len;
  847. break;
  848. }
  849. } /* End of switch. */
  850. }
  851. return ovs_dp_upcall(dp, skb, key, &upcall, cutlen);
  852. }
  853. /* When 'last' is true, sample() should always consume the 'skb'.
  854. * Otherwise, sample() should keep 'skb' intact regardless what
  855. * actions are executed within sample().
  856. */
  857. static int sample(struct datapath *dp, struct sk_buff *skb,
  858. struct sw_flow_key *key, const struct nlattr *attr,
  859. bool last)
  860. {
  861. struct nlattr *actions;
  862. struct nlattr *sample_arg;
  863. int rem = nla_len(attr);
  864. const struct sample_arg *arg;
  865. bool clone_flow_key;
  866. /* The first action is always 'OVS_SAMPLE_ATTR_ARG'. */
  867. sample_arg = nla_data(attr);
  868. arg = nla_data(sample_arg);
  869. actions = nla_next(sample_arg, &rem);
  870. if ((arg->probability != U32_MAX) &&
  871. (!arg->probability || prandom_u32() > arg->probability)) {
  872. if (last)
  873. consume_skb(skb);
  874. return 0;
  875. }
  876. clone_flow_key = !arg->exec;
  877. return clone_execute(dp, skb, key, 0, actions, rem, last,
  878. clone_flow_key);
  879. }
  880. /* When 'last' is true, clone() should always consume the 'skb'.
  881. * Otherwise, clone() should keep 'skb' intact regardless what
  882. * actions are executed within clone().
  883. */
  884. static int clone(struct datapath *dp, struct sk_buff *skb,
  885. struct sw_flow_key *key, const struct nlattr *attr,
  886. bool last)
  887. {
  888. struct nlattr *actions;
  889. struct nlattr *clone_arg;
  890. int rem = nla_len(attr);
  891. bool dont_clone_flow_key;
  892. /* The first action is always 'OVS_CLONE_ATTR_ARG'. */
  893. clone_arg = nla_data(attr);
  894. dont_clone_flow_key = nla_get_u32(clone_arg);
  895. actions = nla_next(clone_arg, &rem);
  896. return clone_execute(dp, skb, key, 0, actions, rem, last,
  897. !dont_clone_flow_key);
  898. }
  899. static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
  900. const struct nlattr *attr)
  901. {
  902. struct ovs_action_hash *hash_act = nla_data(attr);
  903. u32 hash = 0;
  904. /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
  905. hash = skb_get_hash(skb);
  906. hash = jhash_1word(hash, hash_act->hash_basis);
  907. if (!hash)
  908. hash = 0x1;
  909. key->ovs_flow_hash = hash;
  910. }
  911. static int execute_set_action(struct sk_buff *skb,
  912. struct sw_flow_key *flow_key,
  913. const struct nlattr *a)
  914. {
  915. /* Only tunnel set execution is supported without a mask. */
  916. if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
  917. struct ovs_tunnel_info *tun = nla_data(a);
  918. skb_dst_drop(skb);
  919. dst_hold((struct dst_entry *)tun->tun_dst);
  920. skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
  921. return 0;
  922. }
  923. return -EINVAL;
  924. }
  925. /* Mask is at the midpoint of the data. */
  926. #define get_mask(a, type) ((const type)nla_data(a) + 1)
  927. static int execute_masked_set_action(struct sk_buff *skb,
  928. struct sw_flow_key *flow_key,
  929. const struct nlattr *a)
  930. {
  931. int err = 0;
  932. switch (nla_type(a)) {
  933. case OVS_KEY_ATTR_PRIORITY:
  934. OVS_SET_MASKED(skb->priority, nla_get_u32(a),
  935. *get_mask(a, u32 *));
  936. flow_key->phy.priority = skb->priority;
  937. break;
  938. case OVS_KEY_ATTR_SKB_MARK:
  939. OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
  940. flow_key->phy.skb_mark = skb->mark;
  941. break;
  942. case OVS_KEY_ATTR_TUNNEL_INFO:
  943. /* Masked data not supported for tunnel. */
  944. err = -EINVAL;
  945. break;
  946. case OVS_KEY_ATTR_ETHERNET:
  947. err = set_eth_addr(skb, flow_key, nla_data(a),
  948. get_mask(a, struct ovs_key_ethernet *));
  949. break;
  950. case OVS_KEY_ATTR_NSH:
  951. err = set_nsh(skb, flow_key, a);
  952. break;
  953. case OVS_KEY_ATTR_IPV4:
  954. err = set_ipv4(skb, flow_key, nla_data(a),
  955. get_mask(a, struct ovs_key_ipv4 *));
  956. break;
  957. case OVS_KEY_ATTR_IPV6:
  958. err = set_ipv6(skb, flow_key, nla_data(a),
  959. get_mask(a, struct ovs_key_ipv6 *));
  960. break;
  961. case OVS_KEY_ATTR_TCP:
  962. err = set_tcp(skb, flow_key, nla_data(a),
  963. get_mask(a, struct ovs_key_tcp *));
  964. break;
  965. case OVS_KEY_ATTR_UDP:
  966. err = set_udp(skb, flow_key, nla_data(a),
  967. get_mask(a, struct ovs_key_udp *));
  968. break;
  969. case OVS_KEY_ATTR_SCTP:
  970. err = set_sctp(skb, flow_key, nla_data(a),
  971. get_mask(a, struct ovs_key_sctp *));
  972. break;
  973. case OVS_KEY_ATTR_MPLS:
  974. err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
  975. __be32 *));
  976. break;
  977. case OVS_KEY_ATTR_CT_STATE:
  978. case OVS_KEY_ATTR_CT_ZONE:
  979. case OVS_KEY_ATTR_CT_MARK:
  980. case OVS_KEY_ATTR_CT_LABELS:
  981. case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
  982. case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
  983. err = -EINVAL;
  984. break;
  985. }
  986. return err;
  987. }
  988. static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
  989. struct sw_flow_key *key,
  990. const struct nlattr *a, bool last)
  991. {
  992. u32 recirc_id;
  993. if (!is_flow_key_valid(key)) {
  994. int err;
  995. err = ovs_flow_key_update(skb, key);
  996. if (err)
  997. return err;
  998. }
  999. BUG_ON(!is_flow_key_valid(key));
  1000. recirc_id = nla_get_u32(a);
  1001. return clone_execute(dp, skb, key, recirc_id, NULL, 0, last, true);
  1002. }
  1003. /* Execute a list of actions against 'skb'. */
  1004. static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
  1005. struct sw_flow_key *key,
  1006. const struct nlattr *attr, int len)
  1007. {
  1008. const struct nlattr *a;
  1009. int rem;
  1010. for (a = attr, rem = len; rem > 0;
  1011. a = nla_next(a, &rem)) {
  1012. int err = 0;
  1013. switch (nla_type(a)) {
  1014. case OVS_ACTION_ATTR_OUTPUT: {
  1015. int port = nla_get_u32(a);
  1016. struct sk_buff *clone;
  1017. /* Every output action needs a separate clone
  1018. * of 'skb', In case the output action is the
  1019. * last action, cloning can be avoided.
  1020. */
  1021. if (nla_is_last(a, rem)) {
  1022. do_output(dp, skb, port, key);
  1023. /* 'skb' has been used for output.
  1024. */
  1025. return 0;
  1026. }
  1027. clone = skb_clone(skb, GFP_ATOMIC);
  1028. if (clone)
  1029. do_output(dp, clone, port, key);
  1030. OVS_CB(skb)->cutlen = 0;
  1031. break;
  1032. }
  1033. case OVS_ACTION_ATTR_TRUNC: {
  1034. struct ovs_action_trunc *trunc = nla_data(a);
  1035. if (skb->len > trunc->max_len)
  1036. OVS_CB(skb)->cutlen = skb->len - trunc->max_len;
  1037. break;
  1038. }
  1039. case OVS_ACTION_ATTR_USERSPACE:
  1040. output_userspace(dp, skb, key, a, attr,
  1041. len, OVS_CB(skb)->cutlen);
  1042. OVS_CB(skb)->cutlen = 0;
  1043. break;
  1044. case OVS_ACTION_ATTR_HASH:
  1045. execute_hash(skb, key, a);
  1046. break;
  1047. case OVS_ACTION_ATTR_PUSH_MPLS:
  1048. err = push_mpls(skb, key, nla_data(a));
  1049. break;
  1050. case OVS_ACTION_ATTR_POP_MPLS:
  1051. err = pop_mpls(skb, key, nla_get_be16(a));
  1052. break;
  1053. case OVS_ACTION_ATTR_PUSH_VLAN:
  1054. err = push_vlan(skb, key, nla_data(a));
  1055. break;
  1056. case OVS_ACTION_ATTR_POP_VLAN:
  1057. err = pop_vlan(skb, key);
  1058. break;
  1059. case OVS_ACTION_ATTR_RECIRC: {
  1060. bool last = nla_is_last(a, rem);
  1061. err = execute_recirc(dp, skb, key, a, last);
  1062. if (last) {
  1063. /* If this is the last action, the skb has
  1064. * been consumed or freed.
  1065. * Return immediately.
  1066. */
  1067. return err;
  1068. }
  1069. break;
  1070. }
  1071. case OVS_ACTION_ATTR_SET:
  1072. err = execute_set_action(skb, key, nla_data(a));
  1073. break;
  1074. case OVS_ACTION_ATTR_SET_MASKED:
  1075. case OVS_ACTION_ATTR_SET_TO_MASKED:
  1076. err = execute_masked_set_action(skb, key, nla_data(a));
  1077. break;
  1078. case OVS_ACTION_ATTR_SAMPLE: {
  1079. bool last = nla_is_last(a, rem);
  1080. err = sample(dp, skb, key, a, last);
  1081. if (last)
  1082. return err;
  1083. break;
  1084. }
  1085. case OVS_ACTION_ATTR_CT:
  1086. if (!is_flow_key_valid(key)) {
  1087. err = ovs_flow_key_update(skb, key);
  1088. if (err)
  1089. return err;
  1090. }
  1091. err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
  1092. nla_data(a));
  1093. /* Hide stolen IP fragments from user space. */
  1094. if (err)
  1095. return err == -EINPROGRESS ? 0 : err;
  1096. break;
  1097. case OVS_ACTION_ATTR_CT_CLEAR:
  1098. err = ovs_ct_clear(skb, key);
  1099. break;
  1100. case OVS_ACTION_ATTR_PUSH_ETH:
  1101. err = push_eth(skb, key, nla_data(a));
  1102. break;
  1103. case OVS_ACTION_ATTR_POP_ETH:
  1104. err = pop_eth(skb, key);
  1105. break;
  1106. case OVS_ACTION_ATTR_PUSH_NSH: {
  1107. u8 buffer[NSH_HDR_MAX_LEN];
  1108. struct nshhdr *nh = (struct nshhdr *)buffer;
  1109. err = nsh_hdr_from_nlattr(nla_data(a), nh,
  1110. NSH_HDR_MAX_LEN);
  1111. if (unlikely(err))
  1112. break;
  1113. err = push_nsh(skb, key, nh);
  1114. break;
  1115. }
  1116. case OVS_ACTION_ATTR_POP_NSH:
  1117. err = pop_nsh(skb, key);
  1118. break;
  1119. case OVS_ACTION_ATTR_METER:
  1120. if (ovs_meter_execute(dp, skb, key, nla_get_u32(a))) {
  1121. consume_skb(skb);
  1122. return 0;
  1123. }
  1124. break;
  1125. case OVS_ACTION_ATTR_CLONE: {
  1126. bool last = nla_is_last(a, rem);
  1127. err = clone(dp, skb, key, a, last);
  1128. if (last)
  1129. return err;
  1130. break;
  1131. }
  1132. }
  1133. if (unlikely(err)) {
  1134. kfree_skb(skb);
  1135. return err;
  1136. }
  1137. }
  1138. consume_skb(skb);
  1139. return 0;
  1140. }
  1141. /* Execute the actions on the clone of the packet. The effect of the
  1142. * execution does not affect the original 'skb' nor the original 'key'.
  1143. *
  1144. * The execution may be deferred in case the actions can not be executed
  1145. * immediately.
  1146. */
  1147. static int clone_execute(struct datapath *dp, struct sk_buff *skb,
  1148. struct sw_flow_key *key, u32 recirc_id,
  1149. const struct nlattr *actions, int len,
  1150. bool last, bool clone_flow_key)
  1151. {
  1152. struct deferred_action *da;
  1153. struct sw_flow_key *clone;
  1154. skb = last ? skb : skb_clone(skb, GFP_ATOMIC);
  1155. if (!skb) {
  1156. /* Out of memory, skip this action.
  1157. */
  1158. return 0;
  1159. }
  1160. /* When clone_flow_key is false, the 'key' will not be change
  1161. * by the actions, then the 'key' can be used directly.
  1162. * Otherwise, try to clone key from the next recursion level of
  1163. * 'flow_keys'. If clone is successful, execute the actions
  1164. * without deferring.
  1165. */
  1166. clone = clone_flow_key ? clone_key(key) : key;
  1167. if (clone) {
  1168. int err = 0;
  1169. if (actions) { /* Sample action */
  1170. if (clone_flow_key)
  1171. __this_cpu_inc(exec_actions_level);
  1172. err = do_execute_actions(dp, skb, clone,
  1173. actions, len);
  1174. if (clone_flow_key)
  1175. __this_cpu_dec(exec_actions_level);
  1176. } else { /* Recirc action */
  1177. clone->recirc_id = recirc_id;
  1178. ovs_dp_process_packet(skb, clone);
  1179. }
  1180. return err;
  1181. }
  1182. /* Out of 'flow_keys' space. Defer actions */
  1183. da = add_deferred_actions(skb, key, actions, len);
  1184. if (da) {
  1185. if (!actions) { /* Recirc action */
  1186. key = &da->pkt_key;
  1187. key->recirc_id = recirc_id;
  1188. }
  1189. } else {
  1190. /* Out of per CPU action FIFO space. Drop the 'skb' and
  1191. * log an error.
  1192. */
  1193. kfree_skb(skb);
  1194. if (net_ratelimit()) {
  1195. if (actions) { /* Sample action */
  1196. pr_warn("%s: deferred action limit reached, drop sample action\n",
  1197. ovs_dp_name(dp));
  1198. } else { /* Recirc action */
  1199. pr_warn("%s: deferred action limit reached, drop recirc action\n",
  1200. ovs_dp_name(dp));
  1201. }
  1202. }
  1203. }
  1204. return 0;
  1205. }
  1206. static void process_deferred_actions(struct datapath *dp)
  1207. {
  1208. struct action_fifo *fifo = this_cpu_ptr(action_fifos);
  1209. /* Do not touch the FIFO in case there is no deferred actions. */
  1210. if (action_fifo_is_empty(fifo))
  1211. return;
  1212. /* Finishing executing all deferred actions. */
  1213. do {
  1214. struct deferred_action *da = action_fifo_get(fifo);
  1215. struct sk_buff *skb = da->skb;
  1216. struct sw_flow_key *key = &da->pkt_key;
  1217. const struct nlattr *actions = da->actions;
  1218. int actions_len = da->actions_len;
  1219. if (actions)
  1220. do_execute_actions(dp, skb, key, actions, actions_len);
  1221. else
  1222. ovs_dp_process_packet(skb, key);
  1223. } while (!action_fifo_is_empty(fifo));
  1224. /* Reset FIFO for the next packet. */
  1225. action_fifo_init(fifo);
  1226. }
  1227. /* Execute a list of actions against 'skb'. */
  1228. int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
  1229. const struct sw_flow_actions *acts,
  1230. struct sw_flow_key *key)
  1231. {
  1232. int err, level;
  1233. level = __this_cpu_inc_return(exec_actions_level);
  1234. if (unlikely(level > OVS_RECURSION_LIMIT)) {
  1235. net_crit_ratelimited("ovs: recursion limit reached on datapath %s, probable configuration error\n",
  1236. ovs_dp_name(dp));
  1237. kfree_skb(skb);
  1238. err = -ENETDOWN;
  1239. goto out;
  1240. }
  1241. OVS_CB(skb)->acts_origlen = acts->orig_len;
  1242. err = do_execute_actions(dp, skb, key,
  1243. acts->actions, acts->actions_len);
  1244. if (level == 1)
  1245. process_deferred_actions(dp);
  1246. out:
  1247. __this_cpu_dec(exec_actions_level);
  1248. return err;
  1249. }
  1250. int action_fifos_init(void)
  1251. {
  1252. action_fifos = alloc_percpu(struct action_fifo);
  1253. if (!action_fifos)
  1254. return -ENOMEM;
  1255. flow_keys = alloc_percpu(struct action_flow_keys);
  1256. if (!flow_keys) {
  1257. free_percpu(action_fifos);
  1258. return -ENOMEM;
  1259. }
  1260. return 0;
  1261. }
  1262. void action_fifos_exit(void)
  1263. {
  1264. free_percpu(action_fifos);
  1265. free_percpu(flow_keys);
  1266. }