actions.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright (c) 2007-2012 Nicira Networks.
  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/tcp.h>
  24. #include <linux/udp.h>
  25. #include <linux/in6.h>
  26. #include <linux/if_arp.h>
  27. #include <linux/if_vlan.h>
  28. #include <net/ip.h>
  29. #include <net/checksum.h>
  30. #include <net/dsfield.h>
  31. #include "datapath.h"
  32. #include "vport.h"
  33. static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
  34. const struct nlattr *attr, int len, bool keep_skb);
  35. static int make_writable(struct sk_buff *skb, int write_len)
  36. {
  37. if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
  38. return 0;
  39. return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  40. }
  41. /* remove VLAN header from packet and update csum accrodingly. */
  42. static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
  43. {
  44. struct vlan_hdr *vhdr;
  45. int err;
  46. err = make_writable(skb, VLAN_ETH_HLEN);
  47. if (unlikely(err))
  48. return err;
  49. if (skb->ip_summed == CHECKSUM_COMPLETE)
  50. skb->csum = csum_sub(skb->csum, csum_partial(skb->data
  51. + ETH_HLEN, VLAN_HLEN, 0));
  52. vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
  53. *current_tci = vhdr->h_vlan_TCI;
  54. memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
  55. __skb_pull(skb, VLAN_HLEN);
  56. vlan_set_encap_proto(skb, vhdr);
  57. skb->mac_header += VLAN_HLEN;
  58. skb_reset_mac_len(skb);
  59. return 0;
  60. }
  61. static int pop_vlan(struct sk_buff *skb)
  62. {
  63. __be16 tci;
  64. int err;
  65. if (likely(vlan_tx_tag_present(skb))) {
  66. skb->vlan_tci = 0;
  67. } else {
  68. if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
  69. skb->len < VLAN_ETH_HLEN))
  70. return 0;
  71. err = __pop_vlan_tci(skb, &tci);
  72. if (err)
  73. return err;
  74. }
  75. /* move next vlan tag to hw accel tag */
  76. if (likely(skb->protocol != htons(ETH_P_8021Q) ||
  77. skb->len < VLAN_ETH_HLEN))
  78. return 0;
  79. err = __pop_vlan_tci(skb, &tci);
  80. if (unlikely(err))
  81. return err;
  82. __vlan_hwaccel_put_tag(skb, ntohs(tci));
  83. return 0;
  84. }
  85. static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
  86. {
  87. if (unlikely(vlan_tx_tag_present(skb))) {
  88. u16 current_tag;
  89. /* push down current VLAN tag */
  90. current_tag = vlan_tx_tag_get(skb);
  91. if (!__vlan_put_tag(skb, current_tag))
  92. return -ENOMEM;
  93. if (skb->ip_summed == CHECKSUM_COMPLETE)
  94. skb->csum = csum_add(skb->csum, csum_partial(skb->data
  95. + ETH_HLEN, VLAN_HLEN, 0));
  96. }
  97. __vlan_hwaccel_put_tag(skb, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
  98. return 0;
  99. }
  100. static int set_eth_addr(struct sk_buff *skb,
  101. const struct ovs_key_ethernet *eth_key)
  102. {
  103. int err;
  104. err = make_writable(skb, ETH_HLEN);
  105. if (unlikely(err))
  106. return err;
  107. memcpy(eth_hdr(skb)->h_source, eth_key->eth_src, ETH_ALEN);
  108. memcpy(eth_hdr(skb)->h_dest, eth_key->eth_dst, ETH_ALEN);
  109. return 0;
  110. }
  111. static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
  112. __be32 *addr, __be32 new_addr)
  113. {
  114. int transport_len = skb->len - skb_transport_offset(skb);
  115. if (nh->protocol == IPPROTO_TCP) {
  116. if (likely(transport_len >= sizeof(struct tcphdr)))
  117. inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
  118. *addr, new_addr, 1);
  119. } else if (nh->protocol == IPPROTO_UDP) {
  120. if (likely(transport_len >= sizeof(struct udphdr))) {
  121. struct udphdr *uh = udp_hdr(skb);
  122. if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  123. inet_proto_csum_replace4(&uh->check, skb,
  124. *addr, new_addr, 1);
  125. if (!uh->check)
  126. uh->check = CSUM_MANGLED_0;
  127. }
  128. }
  129. }
  130. csum_replace4(&nh->check, *addr, new_addr);
  131. skb->rxhash = 0;
  132. *addr = new_addr;
  133. }
  134. static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
  135. {
  136. csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
  137. nh->ttl = new_ttl;
  138. }
  139. static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
  140. {
  141. struct iphdr *nh;
  142. int err;
  143. err = make_writable(skb, skb_network_offset(skb) +
  144. sizeof(struct iphdr));
  145. if (unlikely(err))
  146. return err;
  147. nh = ip_hdr(skb);
  148. if (ipv4_key->ipv4_src != nh->saddr)
  149. set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
  150. if (ipv4_key->ipv4_dst != nh->daddr)
  151. set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
  152. if (ipv4_key->ipv4_tos != nh->tos)
  153. ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
  154. if (ipv4_key->ipv4_ttl != nh->ttl)
  155. set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
  156. return 0;
  157. }
  158. /* Must follow make_writable() since that can move the skb data. */
  159. static void set_tp_port(struct sk_buff *skb, __be16 *port,
  160. __be16 new_port, __sum16 *check)
  161. {
  162. inet_proto_csum_replace2(check, skb, *port, new_port, 0);
  163. *port = new_port;
  164. skb->rxhash = 0;
  165. }
  166. static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
  167. {
  168. struct udphdr *uh = udp_hdr(skb);
  169. if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
  170. set_tp_port(skb, port, new_port, &uh->check);
  171. if (!uh->check)
  172. uh->check = CSUM_MANGLED_0;
  173. } else {
  174. *port = new_port;
  175. skb->rxhash = 0;
  176. }
  177. }
  178. static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
  179. {
  180. struct udphdr *uh;
  181. int err;
  182. err = make_writable(skb, skb_transport_offset(skb) +
  183. sizeof(struct udphdr));
  184. if (unlikely(err))
  185. return err;
  186. uh = udp_hdr(skb);
  187. if (udp_port_key->udp_src != uh->source)
  188. set_udp_port(skb, &uh->source, udp_port_key->udp_src);
  189. if (udp_port_key->udp_dst != uh->dest)
  190. set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
  191. return 0;
  192. }
  193. static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
  194. {
  195. struct tcphdr *th;
  196. int err;
  197. err = make_writable(skb, skb_transport_offset(skb) +
  198. sizeof(struct tcphdr));
  199. if (unlikely(err))
  200. return err;
  201. th = tcp_hdr(skb);
  202. if (tcp_port_key->tcp_src != th->source)
  203. set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
  204. if (tcp_port_key->tcp_dst != th->dest)
  205. set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
  206. return 0;
  207. }
  208. static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
  209. {
  210. struct vport *vport;
  211. if (unlikely(!skb))
  212. return -ENOMEM;
  213. vport = rcu_dereference(dp->ports[out_port]);
  214. if (unlikely(!vport)) {
  215. kfree_skb(skb);
  216. return -ENODEV;
  217. }
  218. ovs_vport_send(vport, skb);
  219. return 0;
  220. }
  221. static int output_userspace(struct datapath *dp, struct sk_buff *skb,
  222. const struct nlattr *attr)
  223. {
  224. struct dp_upcall_info upcall;
  225. const struct nlattr *a;
  226. int rem;
  227. upcall.cmd = OVS_PACKET_CMD_ACTION;
  228. upcall.key = &OVS_CB(skb)->flow->key;
  229. upcall.userdata = NULL;
  230. upcall.pid = 0;
  231. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  232. a = nla_next(a, &rem)) {
  233. switch (nla_type(a)) {
  234. case OVS_USERSPACE_ATTR_USERDATA:
  235. upcall.userdata = a;
  236. break;
  237. case OVS_USERSPACE_ATTR_PID:
  238. upcall.pid = nla_get_u32(a);
  239. break;
  240. }
  241. }
  242. return ovs_dp_upcall(dp, skb, &upcall);
  243. }
  244. static int sample(struct datapath *dp, struct sk_buff *skb,
  245. const struct nlattr *attr)
  246. {
  247. const struct nlattr *acts_list = NULL;
  248. const struct nlattr *a;
  249. int rem;
  250. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  251. a = nla_next(a, &rem)) {
  252. switch (nla_type(a)) {
  253. case OVS_SAMPLE_ATTR_PROBABILITY:
  254. if (net_random() >= nla_get_u32(a))
  255. return 0;
  256. break;
  257. case OVS_SAMPLE_ATTR_ACTIONS:
  258. acts_list = a;
  259. break;
  260. }
  261. }
  262. return do_execute_actions(dp, skb, nla_data(acts_list),
  263. nla_len(acts_list), true);
  264. }
  265. static int execute_set_action(struct sk_buff *skb,
  266. const struct nlattr *nested_attr)
  267. {
  268. int err = 0;
  269. switch (nla_type(nested_attr)) {
  270. case OVS_KEY_ATTR_PRIORITY:
  271. skb->priority = nla_get_u32(nested_attr);
  272. break;
  273. case OVS_KEY_ATTR_ETHERNET:
  274. err = set_eth_addr(skb, nla_data(nested_attr));
  275. break;
  276. case OVS_KEY_ATTR_IPV4:
  277. err = set_ipv4(skb, nla_data(nested_attr));
  278. break;
  279. case OVS_KEY_ATTR_TCP:
  280. err = set_tcp(skb, nla_data(nested_attr));
  281. break;
  282. case OVS_KEY_ATTR_UDP:
  283. err = set_udp(skb, nla_data(nested_attr));
  284. break;
  285. }
  286. return err;
  287. }
  288. /* Execute a list of actions against 'skb'. */
  289. static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
  290. const struct nlattr *attr, int len, bool keep_skb)
  291. {
  292. /* Every output action needs a separate clone of 'skb', but the common
  293. * case is just a single output action, so that doing a clone and
  294. * then freeing the original skbuff is wasteful. So the following code
  295. * is slightly obscure just to avoid that. */
  296. int prev_port = -1;
  297. const struct nlattr *a;
  298. int rem;
  299. for (a = attr, rem = len; rem > 0;
  300. a = nla_next(a, &rem)) {
  301. int err = 0;
  302. if (prev_port != -1) {
  303. do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
  304. prev_port = -1;
  305. }
  306. switch (nla_type(a)) {
  307. case OVS_ACTION_ATTR_OUTPUT:
  308. prev_port = nla_get_u32(a);
  309. break;
  310. case OVS_ACTION_ATTR_USERSPACE:
  311. output_userspace(dp, skb, a);
  312. break;
  313. case OVS_ACTION_ATTR_PUSH_VLAN:
  314. err = push_vlan(skb, nla_data(a));
  315. if (unlikely(err)) /* skb already freed. */
  316. return err;
  317. break;
  318. case OVS_ACTION_ATTR_POP_VLAN:
  319. err = pop_vlan(skb);
  320. break;
  321. case OVS_ACTION_ATTR_SET:
  322. err = execute_set_action(skb, nla_data(a));
  323. break;
  324. case OVS_ACTION_ATTR_SAMPLE:
  325. err = sample(dp, skb, a);
  326. break;
  327. }
  328. if (unlikely(err)) {
  329. kfree_skb(skb);
  330. return err;
  331. }
  332. }
  333. if (prev_port != -1) {
  334. if (keep_skb)
  335. skb = skb_clone(skb, GFP_ATOMIC);
  336. do_output(dp, skb, prev_port);
  337. } else if (!keep_skb)
  338. consume_skb(skb);
  339. return 0;
  340. }
  341. /* Execute a list of actions against 'skb'. */
  342. int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb)
  343. {
  344. struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
  345. return do_execute_actions(dp, skb, acts->actions,
  346. acts->actions_len, false);
  347. }