xref: /linux/net/openvswitch/actions.c (revision 6eb2fb3170549737207974c2c6ad34bcc2f3025e)
1 /*
2  * Copyright (c) 2007-2012 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 
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 
21 #include <linux/skbuff.h>
22 #include <linux/in.h>
23 #include <linux/ip.h>
24 #include <linux/openvswitch.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/ip.h>
31 #include <net/ipv6.h>
32 #include <net/checksum.h>
33 #include <net/dsfield.h>
34 
35 #include "datapath.h"
36 #include "vport.h"
37 
38 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
39 			const struct nlattr *attr, int len, bool keep_skb);
40 
41 static int make_writable(struct sk_buff *skb, int write_len)
42 {
43 	if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
44 		return 0;
45 
46 	return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
47 }
48 
49 /* remove VLAN header from packet and update csum accordingly. */
50 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
51 {
52 	struct vlan_hdr *vhdr;
53 	int err;
54 
55 	err = make_writable(skb, VLAN_ETH_HLEN);
56 	if (unlikely(err))
57 		return err;
58 
59 	if (skb->ip_summed == CHECKSUM_COMPLETE)
60 		skb->csum = csum_sub(skb->csum, csum_partial(skb->data
61 					+ (2 * ETH_ALEN), VLAN_HLEN, 0));
62 
63 	vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
64 	*current_tci = vhdr->h_vlan_TCI;
65 
66 	memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
67 	__skb_pull(skb, VLAN_HLEN);
68 
69 	vlan_set_encap_proto(skb, vhdr);
70 	skb->mac_header += VLAN_HLEN;
71 	skb_reset_mac_len(skb);
72 
73 	return 0;
74 }
75 
76 static int pop_vlan(struct sk_buff *skb)
77 {
78 	__be16 tci;
79 	int err;
80 
81 	if (likely(vlan_tx_tag_present(skb))) {
82 		skb->vlan_tci = 0;
83 	} else {
84 		if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
85 			     skb->len < VLAN_ETH_HLEN))
86 			return 0;
87 
88 		err = __pop_vlan_tci(skb, &tci);
89 		if (err)
90 			return err;
91 	}
92 	/* move next vlan tag to hw accel tag */
93 	if (likely(skb->protocol != htons(ETH_P_8021Q) ||
94 		   skb->len < VLAN_ETH_HLEN))
95 		return 0;
96 
97 	err = __pop_vlan_tci(skb, &tci);
98 	if (unlikely(err))
99 		return err;
100 
101 	__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(tci));
102 	return 0;
103 }
104 
105 static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
106 {
107 	if (unlikely(vlan_tx_tag_present(skb))) {
108 		u16 current_tag;
109 
110 		/* push down current VLAN tag */
111 		current_tag = vlan_tx_tag_get(skb);
112 
113 		if (!__vlan_put_tag(skb, skb->vlan_proto, current_tag))
114 			return -ENOMEM;
115 
116 		if (skb->ip_summed == CHECKSUM_COMPLETE)
117 			skb->csum = csum_add(skb->csum, csum_partial(skb->data
118 					+ (2 * ETH_ALEN), VLAN_HLEN, 0));
119 
120 	}
121 	__vlan_hwaccel_put_tag(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
122 	return 0;
123 }
124 
125 static int set_eth_addr(struct sk_buff *skb,
126 			const struct ovs_key_ethernet *eth_key)
127 {
128 	int err;
129 	err = make_writable(skb, ETH_HLEN);
130 	if (unlikely(err))
131 		return err;
132 
133 	memcpy(eth_hdr(skb)->h_source, eth_key->eth_src, ETH_ALEN);
134 	memcpy(eth_hdr(skb)->h_dest, eth_key->eth_dst, ETH_ALEN);
135 
136 	return 0;
137 }
138 
139 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
140 				__be32 *addr, __be32 new_addr)
141 {
142 	int transport_len = skb->len - skb_transport_offset(skb);
143 
144 	if (nh->protocol == IPPROTO_TCP) {
145 		if (likely(transport_len >= sizeof(struct tcphdr)))
146 			inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
147 						 *addr, new_addr, 1);
148 	} else if (nh->protocol == IPPROTO_UDP) {
149 		if (likely(transport_len >= sizeof(struct udphdr))) {
150 			struct udphdr *uh = udp_hdr(skb);
151 
152 			if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
153 				inet_proto_csum_replace4(&uh->check, skb,
154 							 *addr, new_addr, 1);
155 				if (!uh->check)
156 					uh->check = CSUM_MANGLED_0;
157 			}
158 		}
159 	}
160 
161 	csum_replace4(&nh->check, *addr, new_addr);
162 	skb->rxhash = 0;
163 	*addr = new_addr;
164 }
165 
166 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
167 				 __be32 addr[4], const __be32 new_addr[4])
168 {
169 	int transport_len = skb->len - skb_transport_offset(skb);
170 
171 	if (l4_proto == IPPROTO_TCP) {
172 		if (likely(transport_len >= sizeof(struct tcphdr)))
173 			inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
174 						  addr, new_addr, 1);
175 	} else if (l4_proto == IPPROTO_UDP) {
176 		if (likely(transport_len >= sizeof(struct udphdr))) {
177 			struct udphdr *uh = udp_hdr(skb);
178 
179 			if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
180 				inet_proto_csum_replace16(&uh->check, skb,
181 							  addr, new_addr, 1);
182 				if (!uh->check)
183 					uh->check = CSUM_MANGLED_0;
184 			}
185 		}
186 	}
187 }
188 
189 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
190 			  __be32 addr[4], const __be32 new_addr[4],
191 			  bool recalculate_csum)
192 {
193 	if (recalculate_csum)
194 		update_ipv6_checksum(skb, l4_proto, addr, new_addr);
195 
196 	skb->rxhash = 0;
197 	memcpy(addr, new_addr, sizeof(__be32[4]));
198 }
199 
200 static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc)
201 {
202 	nh->priority = tc >> 4;
203 	nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4);
204 }
205 
206 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl)
207 {
208 	nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16;
209 	nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8;
210 	nh->flow_lbl[2] = fl & 0x000000FF;
211 }
212 
213 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
214 {
215 	csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
216 	nh->ttl = new_ttl;
217 }
218 
219 static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
220 {
221 	struct iphdr *nh;
222 	int err;
223 
224 	err = make_writable(skb, skb_network_offset(skb) +
225 				 sizeof(struct iphdr));
226 	if (unlikely(err))
227 		return err;
228 
229 	nh = ip_hdr(skb);
230 
231 	if (ipv4_key->ipv4_src != nh->saddr)
232 		set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
233 
234 	if (ipv4_key->ipv4_dst != nh->daddr)
235 		set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
236 
237 	if (ipv4_key->ipv4_tos != nh->tos)
238 		ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
239 
240 	if (ipv4_key->ipv4_ttl != nh->ttl)
241 		set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
242 
243 	return 0;
244 }
245 
246 static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key)
247 {
248 	struct ipv6hdr *nh;
249 	int err;
250 	__be32 *saddr;
251 	__be32 *daddr;
252 
253 	err = make_writable(skb, skb_network_offset(skb) +
254 			    sizeof(struct ipv6hdr));
255 	if (unlikely(err))
256 		return err;
257 
258 	nh = ipv6_hdr(skb);
259 	saddr = (__be32 *)&nh->saddr;
260 	daddr = (__be32 *)&nh->daddr;
261 
262 	if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src)))
263 		set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
264 			      ipv6_key->ipv6_src, true);
265 
266 	if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
267 		unsigned int offset = 0;
268 		int flags = IP6_FH_F_SKIP_RH;
269 		bool recalc_csum = true;
270 
271 		if (ipv6_ext_hdr(nh->nexthdr))
272 			recalc_csum = ipv6_find_hdr(skb, &offset,
273 						    NEXTHDR_ROUTING, NULL,
274 						    &flags) != NEXTHDR_ROUTING;
275 
276 		set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
277 			      ipv6_key->ipv6_dst, recalc_csum);
278 	}
279 
280 	set_ipv6_tc(nh, ipv6_key->ipv6_tclass);
281 	set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label));
282 	nh->hop_limit = ipv6_key->ipv6_hlimit;
283 
284 	return 0;
285 }
286 
287 /* Must follow make_writable() since that can move the skb data. */
288 static void set_tp_port(struct sk_buff *skb, __be16 *port,
289 			 __be16 new_port, __sum16 *check)
290 {
291 	inet_proto_csum_replace2(check, skb, *port, new_port, 0);
292 	*port = new_port;
293 	skb->rxhash = 0;
294 }
295 
296 static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
297 {
298 	struct udphdr *uh = udp_hdr(skb);
299 
300 	if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
301 		set_tp_port(skb, port, new_port, &uh->check);
302 
303 		if (!uh->check)
304 			uh->check = CSUM_MANGLED_0;
305 	} else {
306 		*port = new_port;
307 		skb->rxhash = 0;
308 	}
309 }
310 
311 static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
312 {
313 	struct udphdr *uh;
314 	int err;
315 
316 	err = make_writable(skb, skb_transport_offset(skb) +
317 				 sizeof(struct udphdr));
318 	if (unlikely(err))
319 		return err;
320 
321 	uh = udp_hdr(skb);
322 	if (udp_port_key->udp_src != uh->source)
323 		set_udp_port(skb, &uh->source, udp_port_key->udp_src);
324 
325 	if (udp_port_key->udp_dst != uh->dest)
326 		set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
327 
328 	return 0;
329 }
330 
331 static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
332 {
333 	struct tcphdr *th;
334 	int err;
335 
336 	err = make_writable(skb, skb_transport_offset(skb) +
337 				 sizeof(struct tcphdr));
338 	if (unlikely(err))
339 		return err;
340 
341 	th = tcp_hdr(skb);
342 	if (tcp_port_key->tcp_src != th->source)
343 		set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
344 
345 	if (tcp_port_key->tcp_dst != th->dest)
346 		set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
347 
348 	return 0;
349 }
350 
351 static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
352 {
353 	struct vport *vport;
354 
355 	if (unlikely(!skb))
356 		return -ENOMEM;
357 
358 	vport = ovs_vport_rcu(dp, out_port);
359 	if (unlikely(!vport)) {
360 		kfree_skb(skb);
361 		return -ENODEV;
362 	}
363 
364 	ovs_vport_send(vport, skb);
365 	return 0;
366 }
367 
368 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
369 			    const struct nlattr *attr)
370 {
371 	struct dp_upcall_info upcall;
372 	const struct nlattr *a;
373 	int rem;
374 
375 	upcall.cmd = OVS_PACKET_CMD_ACTION;
376 	upcall.key = &OVS_CB(skb)->flow->key;
377 	upcall.userdata = NULL;
378 	upcall.portid = 0;
379 
380 	for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
381 		 a = nla_next(a, &rem)) {
382 		switch (nla_type(a)) {
383 		case OVS_USERSPACE_ATTR_USERDATA:
384 			upcall.userdata = a;
385 			break;
386 
387 		case OVS_USERSPACE_ATTR_PID:
388 			upcall.portid = nla_get_u32(a);
389 			break;
390 		}
391 	}
392 
393 	return ovs_dp_upcall(dp, skb, &upcall);
394 }
395 
396 static int sample(struct datapath *dp, struct sk_buff *skb,
397 		  const struct nlattr *attr)
398 {
399 	const struct nlattr *acts_list = NULL;
400 	const struct nlattr *a;
401 	int rem;
402 
403 	for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
404 		 a = nla_next(a, &rem)) {
405 		switch (nla_type(a)) {
406 		case OVS_SAMPLE_ATTR_PROBABILITY:
407 			if (net_random() >= nla_get_u32(a))
408 				return 0;
409 			break;
410 
411 		case OVS_SAMPLE_ATTR_ACTIONS:
412 			acts_list = a;
413 			break;
414 		}
415 	}
416 
417 	return do_execute_actions(dp, skb, nla_data(acts_list),
418 						 nla_len(acts_list), true);
419 }
420 
421 static int execute_set_action(struct sk_buff *skb,
422 				 const struct nlattr *nested_attr)
423 {
424 	int err = 0;
425 
426 	switch (nla_type(nested_attr)) {
427 	case OVS_KEY_ATTR_PRIORITY:
428 		skb->priority = nla_get_u32(nested_attr);
429 		break;
430 
431 	case OVS_KEY_ATTR_SKB_MARK:
432 		skb->mark = nla_get_u32(nested_attr);
433 		break;
434 
435 	case OVS_KEY_ATTR_ETHERNET:
436 		err = set_eth_addr(skb, nla_data(nested_attr));
437 		break;
438 
439 	case OVS_KEY_ATTR_IPV4:
440 		err = set_ipv4(skb, nla_data(nested_attr));
441 		break;
442 
443 	case OVS_KEY_ATTR_IPV6:
444 		err = set_ipv6(skb, nla_data(nested_attr));
445 		break;
446 
447 	case OVS_KEY_ATTR_TCP:
448 		err = set_tcp(skb, nla_data(nested_attr));
449 		break;
450 
451 	case OVS_KEY_ATTR_UDP:
452 		err = set_udp(skb, nla_data(nested_attr));
453 		break;
454 	}
455 
456 	return err;
457 }
458 
459 /* Execute a list of actions against 'skb'. */
460 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
461 			const struct nlattr *attr, int len, bool keep_skb)
462 {
463 	/* Every output action needs a separate clone of 'skb', but the common
464 	 * case is just a single output action, so that doing a clone and
465 	 * then freeing the original skbuff is wasteful.  So the following code
466 	 * is slightly obscure just to avoid that. */
467 	int prev_port = -1;
468 	const struct nlattr *a;
469 	int rem;
470 
471 	for (a = attr, rem = len; rem > 0;
472 	     a = nla_next(a, &rem)) {
473 		int err = 0;
474 
475 		if (prev_port != -1) {
476 			do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
477 			prev_port = -1;
478 		}
479 
480 		switch (nla_type(a)) {
481 		case OVS_ACTION_ATTR_OUTPUT:
482 			prev_port = nla_get_u32(a);
483 			break;
484 
485 		case OVS_ACTION_ATTR_USERSPACE:
486 			output_userspace(dp, skb, a);
487 			break;
488 
489 		case OVS_ACTION_ATTR_PUSH_VLAN:
490 			err = push_vlan(skb, nla_data(a));
491 			if (unlikely(err)) /* skb already freed. */
492 				return err;
493 			break;
494 
495 		case OVS_ACTION_ATTR_POP_VLAN:
496 			err = pop_vlan(skb);
497 			break;
498 
499 		case OVS_ACTION_ATTR_SET:
500 			err = execute_set_action(skb, nla_data(a));
501 			break;
502 
503 		case OVS_ACTION_ATTR_SAMPLE:
504 			err = sample(dp, skb, a);
505 			break;
506 		}
507 
508 		if (unlikely(err)) {
509 			kfree_skb(skb);
510 			return err;
511 		}
512 	}
513 
514 	if (prev_port != -1) {
515 		if (keep_skb)
516 			skb = skb_clone(skb, GFP_ATOMIC);
517 
518 		do_output(dp, skb, prev_port);
519 	} else if (!keep_skb)
520 		consume_skb(skb);
521 
522 	return 0;
523 }
524 
525 /* Execute a list of actions against 'skb'. */
526 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb)
527 {
528 	struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
529 
530 	return do_execute_actions(dp, skb, acts->actions,
531 					 acts->actions_len, false);
532 }
533