ovsdb: Remove 'managers' column from 'Open vSwitch' table.
[openvswitch] / datapath / actions.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 /* Functions for executing flow actions. */
10
11 #include <linux/skbuff.h>
12 #include <linux/in.h>
13 #include <linux/ip.h>
14 #include <linux/tcp.h>
15 #include <linux/udp.h>
16 #include <linux/in6.h>
17 #include <linux/if_arp.h>
18 #include <linux/if_vlan.h>
19 #include <net/inet_ecn.h>
20 #include <net/ip.h>
21 #include <net/checksum.h>
22
23 #include "actions.h"
24 #include "checksum.h"
25 #include "datapath.h"
26 #include "openvswitch/datapath-protocol.h"
27 #include "vlan.h"
28 #include "vport.h"
29
30 static int do_execute_actions(struct datapath *, struct sk_buff *,
31                               const struct sw_flow_key *,
32                               const struct nlattr *actions, u32 actions_len);
33
34 static struct sk_buff *make_writable(struct sk_buff *skb, unsigned min_headroom)
35 {
36         if (skb_cloned(skb)) {
37                 struct sk_buff *nskb;
38                 unsigned headroom = max(min_headroom, skb_headroom(skb));
39
40                 nskb = skb_copy_expand(skb, headroom, skb_tailroom(skb), GFP_ATOMIC);
41                 if (nskb) {
42                         set_skb_csum_bits(skb, nskb);
43                         kfree_skb(skb);
44                         return nskb;
45                 }
46         } else {
47                 unsigned int hdr_len = (skb_transport_offset(skb)
48                                         + sizeof(struct tcphdr));
49                 if (pskb_may_pull(skb, min(hdr_len, skb->len)))
50                         return skb;
51         }
52         kfree_skb(skb);
53         return NULL;
54 }
55
56 static struct sk_buff *strip_vlan(struct sk_buff *skb)
57 {
58         struct ethhdr *eh;
59
60         if (vlan_tx_tag_present(skb)) {
61                 vlan_set_tci(skb, 0);
62                 return skb;
63         }
64
65         if (unlikely(vlan_eth_hdr(skb)->h_vlan_proto != htons(ETH_P_8021Q) ||
66             skb->len < VLAN_ETH_HLEN))
67                 return skb;
68
69         skb = make_writable(skb, 0);
70         if (unlikely(!skb))
71                 return NULL;
72
73         if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
74                 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
75                                         + ETH_HLEN, VLAN_HLEN, 0));
76
77         memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
78
79         eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
80
81         skb->protocol = eh->h_proto;
82         skb->mac_header += VLAN_HLEN;
83
84         return skb;
85 }
86
87 static struct sk_buff *modify_vlan_tci(struct datapath *dp, struct sk_buff *skb,
88                                        const struct sw_flow_key *key,
89                                        const struct nlattr *a, u32 actions_len)
90 {
91         __be16 tci = nla_get_be16(a);
92         struct vlan_ethhdr *vh;
93         __be16 old_tci;
94
95         if (vlan_tx_tag_present(skb) || skb->protocol != htons(ETH_P_8021Q))
96                 return __vlan_hwaccel_put_tag(skb, ntohs(tci));
97
98         skb = make_writable(skb, 0);
99         if (unlikely(!skb))
100                 return NULL;
101
102         if (unlikely(skb->len < VLAN_ETH_HLEN))
103                 return skb;
104
105         vh = vlan_eth_hdr(skb);
106
107         old_tci = vh->h_vlan_TCI;
108         vh->h_vlan_TCI = tci;
109
110         if (get_ip_summed(skb) == OVS_CSUM_COMPLETE) {
111                 __be16 diff[] = { ~old_tci, vh->h_vlan_TCI };
112                 skb->csum = ~csum_partial((char *)diff, sizeof(diff), ~skb->csum);
113         }
114
115         return skb;
116 }
117
118 static bool is_ip(struct sk_buff *skb, const struct sw_flow_key *key)
119 {
120         return (key->dl_type == htons(ETH_P_IP) &&
121                 skb->transport_header > skb->network_header);
122 }
123
124 static __sum16 *get_l4_checksum(struct sk_buff *skb, const struct sw_flow_key *key)
125 {
126         int transport_len = skb->len - skb_transport_offset(skb);
127         if (key->nw_proto == IPPROTO_TCP) {
128                 if (likely(transport_len >= sizeof(struct tcphdr)))
129                         return &tcp_hdr(skb)->check;
130         } else if (key->nw_proto == IPPROTO_UDP) {
131                 if (likely(transport_len >= sizeof(struct udphdr)))
132                         return &udp_hdr(skb)->check;
133         }
134         return NULL;
135 }
136
137 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
138                                    const struct sw_flow_key *key,
139                                    const struct nlattr *a)
140 {
141         __be32 new_nwaddr = nla_get_be32(a);
142         struct iphdr *nh;
143         __sum16 *check;
144         __be32 *nwaddr;
145
146         if (unlikely(!is_ip(skb, key)))
147                 return skb;
148
149         skb = make_writable(skb, 0);
150         if (unlikely(!skb))
151                 return NULL;
152
153         nh = ip_hdr(skb);
154         nwaddr = nla_type(a) == ODP_ACTION_ATTR_SET_NW_SRC ? &nh->saddr : &nh->daddr;
155
156         check = get_l4_checksum(skb, key);
157         if (likely(check))
158                 inet_proto_csum_replace4(check, skb, *nwaddr, new_nwaddr, 1);
159         csum_replace4(&nh->check, *nwaddr, new_nwaddr);
160
161         skb_clear_rxhash(skb);
162
163         *nwaddr = new_nwaddr;
164
165         return skb;
166 }
167
168 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
169                                   const struct sw_flow_key *key,
170                                   u8 nw_tos)
171 {
172         if (unlikely(!is_ip(skb, key)))
173                 return skb;
174
175         skb = make_writable(skb, 0);
176         if (skb) {
177                 struct iphdr *nh = ip_hdr(skb);
178                 u8 *f = &nh->tos;
179                 u8 old = *f;
180                 u8 new;
181
182                 /* Set the DSCP bits and preserve the ECN bits. */
183                 new = nw_tos | (nh->tos & INET_ECN_MASK);
184                 csum_replace4(&nh->check, (__force __be32)old,
185                                           (__force __be32)new);
186                 *f = new;
187         }
188         return skb;
189 }
190
191 static struct sk_buff *set_tp_port(struct sk_buff *skb,
192                                    const struct sw_flow_key *key,
193                                    const struct nlattr *a)
194 {
195         struct udphdr *th;
196         __sum16 *check;
197         __be16 *port;
198
199         if (unlikely(!is_ip(skb, key)))
200                 return skb;
201
202         skb = make_writable(skb, 0);
203         if (unlikely(!skb))
204                 return NULL;
205
206         /* Must follow make_writable() since that can move the skb data. */
207         check = get_l4_checksum(skb, key);
208         if (unlikely(!check))
209                 return skb;
210
211         /*
212          * Update port and checksum.
213          *
214          * This is OK because source and destination port numbers are at the
215          * same offsets in both UDP and TCP headers, and get_l4_checksum() only
216          * supports those protocols.
217          */
218         th = udp_hdr(skb);
219         port = nla_type(a) == ODP_ACTION_ATTR_SET_TP_SRC ? &th->source : &th->dest;
220         inet_proto_csum_replace2(check, skb, *port, nla_get_be16(a), 0);
221         *port = nla_get_be16(a);
222         skb_clear_rxhash(skb);
223
224         return skb;
225 }
226
227 /**
228  * is_spoofed_arp - check for invalid ARP packet
229  *
230  * @skb: skbuff containing an Ethernet packet, with network header pointing
231  * just past the Ethernet and optional 802.1Q header.
232  * @key: flow key extracted from @skb by flow_extract()
233  *
234  * Returns true if @skb is an invalid Ethernet+IPv4 ARP packet: one with screwy
235  * or truncated header fields or one whose inner and outer Ethernet address
236  * differ.
237  */
238 static bool is_spoofed_arp(struct sk_buff *skb, const struct sw_flow_key *key)
239 {
240         struct arp_eth_header *arp;
241
242         if (key->dl_type != htons(ETH_P_ARP))
243                 return false;
244
245         if (skb_network_offset(skb) + sizeof(struct arp_eth_header) > skb->len)
246                 return true;
247
248         arp = (struct arp_eth_header *)skb_network_header(skb);
249         return (arp->ar_hrd != htons(ARPHRD_ETHER) ||
250                 arp->ar_pro != htons(ETH_P_IP) ||
251                 arp->ar_hln != ETH_ALEN ||
252                 arp->ar_pln != 4 ||
253                 compare_ether_addr(arp->ar_sha, eth_hdr(skb)->h_source));
254 }
255
256 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
257 {
258         struct vport *p;
259
260         if (!skb)
261                 goto error;
262
263         p = rcu_dereference(dp->ports[out_port]);
264         if (!p)
265                 goto error;
266
267         vport_send(p, skb);
268         return;
269
270 error:
271         kfree_skb(skb);
272 }
273
274 static int output_control(struct datapath *dp, struct sk_buff *skb, u64 arg,
275                           const struct sw_flow_key *key)
276 {
277         struct dp_upcall_info upcall;
278
279         skb = skb_clone(skb, GFP_ATOMIC);
280         if (!skb)
281                 return -ENOMEM;
282
283         upcall.cmd = ODP_PACKET_CMD_ACTION;
284         upcall.key = key;
285         upcall.userdata = arg;
286         upcall.sample_pool = 0;
287         upcall.actions = NULL;
288         upcall.actions_len = 0;
289         return dp_upcall(dp, skb, &upcall);
290 }
291
292 /* Execute a list of actions against 'skb'. */
293 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
294                               const struct sw_flow_key *key,
295                               const struct nlattr *actions, u32 actions_len)
296 {
297         /* Every output action needs a separate clone of 'skb', but the common
298          * case is just a single output action, so that doing a clone and
299          * then freeing the original skbuff is wasteful.  So the following code
300          * is slightly obscure just to avoid that. */
301         int prev_port = -1;
302         u32 priority = skb->priority;
303         const struct nlattr *a;
304         int rem, err;
305
306         for (a = actions, rem = actions_len; rem > 0; a = nla_next(a, &rem)) {
307                 if (prev_port != -1) {
308                         do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
309                         prev_port = -1;
310                 }
311
312                 switch (nla_type(a)) {
313                 case ODP_ACTION_ATTR_OUTPUT:
314                         prev_port = nla_get_u32(a);
315                         break;
316
317                 case ODP_ACTION_ATTR_CONTROLLER:
318                         err = output_control(dp, skb, nla_get_u64(a), key);
319                         if (err) {
320                                 kfree_skb(skb);
321                                 return err;
322                         }
323                         break;
324
325                 case ODP_ACTION_ATTR_SET_TUNNEL:
326                         OVS_CB(skb)->tun_id = nla_get_be64(a);
327                         break;
328
329                 case ODP_ACTION_ATTR_SET_DL_TCI:
330                         skb = modify_vlan_tci(dp, skb, key, a, rem);
331                         break;
332
333                 case ODP_ACTION_ATTR_STRIP_VLAN:
334                         skb = strip_vlan(skb);
335                         break;
336
337                 case ODP_ACTION_ATTR_SET_DL_SRC:
338                         skb = make_writable(skb, 0);
339                         if (!skb)
340                                 return -ENOMEM;
341                         memcpy(eth_hdr(skb)->h_source, nla_data(a), ETH_ALEN);
342                         break;
343
344                 case ODP_ACTION_ATTR_SET_DL_DST:
345                         skb = make_writable(skb, 0);
346                         if (!skb)
347                                 return -ENOMEM;
348                         memcpy(eth_hdr(skb)->h_dest, nla_data(a), ETH_ALEN);
349                         break;
350
351                 case ODP_ACTION_ATTR_SET_NW_SRC:
352                 case ODP_ACTION_ATTR_SET_NW_DST:
353                         skb = set_nw_addr(skb, key, a);
354                         break;
355
356                 case ODP_ACTION_ATTR_SET_NW_TOS:
357                         skb = set_nw_tos(skb, key, nla_get_u8(a));
358                         break;
359
360                 case ODP_ACTION_ATTR_SET_TP_SRC:
361                 case ODP_ACTION_ATTR_SET_TP_DST:
362                         skb = set_tp_port(skb, key, a);
363                         break;
364
365                 case ODP_ACTION_ATTR_SET_PRIORITY:
366                         skb->priority = nla_get_u32(a);
367                         break;
368
369                 case ODP_ACTION_ATTR_POP_PRIORITY:
370                         skb->priority = priority;
371                         break;
372
373                 case ODP_ACTION_ATTR_DROP_SPOOFED_ARP:
374                         if (unlikely(is_spoofed_arp(skb, key)))
375                                 goto exit;
376                         break;
377                 }
378                 if (!skb)
379                         return -ENOMEM;
380         }
381 exit:
382         if (prev_port != -1)
383                 do_output(dp, skb, prev_port);
384         else
385                 kfree_skb(skb);
386         return 0;
387 }
388
389 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
390                          const struct sw_flow_key *key,
391                          const struct nlattr *a, u32 actions_len)
392 {
393         struct sk_buff *nskb;
394         struct vport *p = OVS_CB(skb)->vport;
395         struct dp_upcall_info upcall;
396
397         if (unlikely(!p))
398                 return;
399
400         atomic_inc(&p->sflow_pool);
401         if (net_random() >= dp->sflow_probability)
402                 return;
403
404         nskb = skb_clone(skb, GFP_ATOMIC);
405         if (unlikely(!nskb))
406                 return;
407
408         upcall.cmd = ODP_PACKET_CMD_SAMPLE;
409         upcall.key = key;
410         upcall.userdata = 0;
411         upcall.sample_pool = atomic_read(&p->sflow_pool);
412         upcall.actions = a;
413         upcall.actions_len = actions_len;
414         dp_upcall(dp, nskb, &upcall);
415 }
416
417 /* Execute a list of actions against 'skb'. */
418 int execute_actions(struct datapath *dp, struct sk_buff *skb,
419                     const struct sw_flow_key *key,
420                     const struct nlattr *actions, u32 actions_len)
421 {
422         if (dp->sflow_probability)
423                 sflow_sample(dp, skb, key, actions, actions_len);
424
425         OVS_CB(skb)->tun_id = 0;
426
427         return do_execute_actions(dp, skb, key, actions, actions_len);
428 }