actions.c 29 KB

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