actions.c 38 KB

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