ofp-util: Fix null pointer dereference in ofputil_pull_buckets().
[cascardo/ovs.git] / lib / odp-execute.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3  * Copyright (c) 2013 Simon Horman
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <config.h>
19 #include "odp-execute.h"
20 #include <linux/openvswitch.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "dpif.h"
25 #include "netlink.h"
26 #include "ofpbuf.h"
27 #include "odp-util.h"
28 #include "packets.h"
29 #include "flow.h"
30 #include "unaligned.h"
31 #include "util.h"
32
33 static void
34 odp_eth_set_addrs(struct ofpbuf *packet,
35                   const struct ovs_key_ethernet *eth_key)
36 {
37     struct eth_header *eh = ofpbuf_l2(packet);
38
39     if (eh) {
40         memcpy(eh->eth_src, eth_key->eth_src, sizeof eh->eth_src);
41         memcpy(eh->eth_dst, eth_key->eth_dst, sizeof eh->eth_dst);
42     }
43 }
44
45 static void
46 odp_set_tunnel_action(const struct nlattr *a, struct flow_tnl *tun_key)
47 {
48     enum odp_key_fitness fitness;
49
50     fitness = odp_tun_key_from_attr(a, tun_key);
51     ovs_assert(fitness != ODP_FIT_ERROR);
52 }
53
54 static void
55 set_arp(struct ofpbuf *packet, const struct ovs_key_arp *arp_key)
56 {
57     struct arp_eth_header *arp = ofpbuf_l3(packet);
58
59     arp->ar_op = arp_key->arp_op;
60     memcpy(arp->ar_sha, arp_key->arp_sha, ETH_ADDR_LEN);
61     put_16aligned_be32(&arp->ar_spa, arp_key->arp_sip);
62     memcpy(arp->ar_tha, arp_key->arp_tha, ETH_ADDR_LEN);
63     put_16aligned_be32(&arp->ar_tpa, arp_key->arp_tip);
64 }
65
66 static void
67 odp_execute_set_action(struct ofpbuf *packet, const struct nlattr *a,
68                        struct pkt_metadata *md)
69 {
70     enum ovs_key_attr type = nl_attr_type(a);
71     const struct ovs_key_ipv4 *ipv4_key;
72     const struct ovs_key_ipv6 *ipv6_key;
73     const struct ovs_key_tcp *tcp_key;
74     const struct ovs_key_udp *udp_key;
75     const struct ovs_key_sctp *sctp_key;
76
77     switch (type) {
78     case OVS_KEY_ATTR_PRIORITY:
79         md->skb_priority = nl_attr_get_u32(a);
80         break;
81
82     case OVS_KEY_ATTR_TUNNEL:
83         odp_set_tunnel_action(a, &md->tunnel);
84         break;
85
86     case OVS_KEY_ATTR_SKB_MARK:
87         md->pkt_mark = nl_attr_get_u32(a);
88         break;
89
90     case OVS_KEY_ATTR_ETHERNET:
91         odp_eth_set_addrs(packet,
92                           nl_attr_get_unspec(a, sizeof(struct ovs_key_ethernet)));
93         break;
94
95     case OVS_KEY_ATTR_IPV4:
96         ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
97         packet_set_ipv4(packet, ipv4_key->ipv4_src, ipv4_key->ipv4_dst,
98                         ipv4_key->ipv4_tos, ipv4_key->ipv4_ttl);
99         break;
100
101     case OVS_KEY_ATTR_IPV6:
102         ipv6_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv6));
103         packet_set_ipv6(packet, ipv6_key->ipv6_proto, ipv6_key->ipv6_src,
104                         ipv6_key->ipv6_dst, ipv6_key->ipv6_tclass,
105                         ipv6_key->ipv6_label, ipv6_key->ipv6_hlimit);
106         break;
107
108     case OVS_KEY_ATTR_TCP:
109         tcp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
110         packet_set_tcp_port(packet, tcp_key->tcp_src, tcp_key->tcp_dst);
111         break;
112
113     case OVS_KEY_ATTR_UDP:
114         udp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
115         packet_set_udp_port(packet, udp_key->udp_src, udp_key->udp_dst);
116         break;
117
118     case OVS_KEY_ATTR_SCTP:
119         sctp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_sctp));
120         packet_set_sctp_port(packet, sctp_key->sctp_src, sctp_key->sctp_dst);
121         break;
122
123     case OVS_KEY_ATTR_MPLS:
124          set_mpls_lse(packet, nl_attr_get_be32(a));
125          break;
126
127     case OVS_KEY_ATTR_ARP:
128         set_arp(packet, nl_attr_get_unspec(a, sizeof(struct ovs_key_arp)));
129         break;
130
131     case OVS_KEY_ATTR_DP_HASH:
132         md->dp_hash = nl_attr_get_u32(a);
133         break;
134
135     case OVS_KEY_ATTR_RECIRC_ID:
136         md->recirc_id = nl_attr_get_u32(a);
137         break;
138
139     case OVS_KEY_ATTR_UNSPEC:
140     case OVS_KEY_ATTR_ENCAP:
141     case OVS_KEY_ATTR_ETHERTYPE:
142     case OVS_KEY_ATTR_IN_PORT:
143     case OVS_KEY_ATTR_VLAN:
144     case OVS_KEY_ATTR_ICMP:
145     case OVS_KEY_ATTR_ICMPV6:
146     case OVS_KEY_ATTR_ND:
147     case OVS_KEY_ATTR_TCP_FLAGS:
148     case __OVS_KEY_ATTR_MAX:
149     default:
150         OVS_NOT_REACHED();
151     }
152 }
153
154 static void
155 odp_execute_actions__(void *dp, struct ofpbuf *packet, bool steal,
156                       struct pkt_metadata *,
157                       const struct nlattr *actions, size_t actions_len,
158                       odp_execute_cb dp_execute_action, bool more_actions);
159
160 static void
161 odp_execute_sample(void *dp, struct ofpbuf *packet, bool steal,
162                    struct pkt_metadata *md, const struct nlattr *action,
163                    odp_execute_cb dp_execute_action, bool more_actions)
164 {
165     const struct nlattr *subactions = NULL;
166     const struct nlattr *a;
167     size_t left;
168
169     NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
170         int type = nl_attr_type(a);
171
172         switch ((enum ovs_sample_attr) type) {
173         case OVS_SAMPLE_ATTR_PROBABILITY:
174             if (random_uint32() >= nl_attr_get_u32(a)) {
175                 return;
176             }
177             break;
178
179         case OVS_SAMPLE_ATTR_ACTIONS:
180             subactions = a;
181             break;
182
183         case OVS_SAMPLE_ATTR_UNSPEC:
184         case __OVS_SAMPLE_ATTR_MAX:
185         default:
186             OVS_NOT_REACHED();
187         }
188     }
189
190     odp_execute_actions__(dp, packet, steal, md, nl_attr_get(subactions),
191                           nl_attr_get_size(subactions), dp_execute_action,
192                           more_actions);
193 }
194
195 static void
196 odp_execute_actions__(void *dp, struct ofpbuf *packet, bool steal,
197                       struct pkt_metadata *md,
198                       const struct nlattr *actions, size_t actions_len,
199                       odp_execute_cb dp_execute_action, bool more_actions)
200 {
201     const struct nlattr *a;
202     unsigned int left;
203
204     NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
205         int type = nl_attr_type(a);
206
207         switch ((enum ovs_action_attr) type) {
208             /* These only make sense in the context of a datapath. */
209         case OVS_ACTION_ATTR_OUTPUT:
210         case OVS_ACTION_ATTR_USERSPACE:
211         case OVS_ACTION_ATTR_RECIRC:
212             if (dp_execute_action) {
213                 /* Allow 'dp_execute_action' to steal the packet data if we do
214                  * not need it any more. */
215                 bool may_steal = steal && (!more_actions
216                                            && left <= NLA_ALIGN(a->nla_len)
217                                            && type != OVS_ACTION_ATTR_RECIRC);
218                 dp_execute_action(dp, packet, md, a, may_steal);
219             }
220             break;
221
222         case OVS_ACTION_ATTR_HASH: {
223             const struct ovs_action_hash *hash_act = nl_attr_get(a);
224
225             /* Calculate a hash value directly.  This might not match the
226              * value computed by the datapath, but it is much less expensive,
227              * and the current use case (bonding) does not require a strict
228              * match to work properly. */
229             if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
230                 struct flow flow;
231                 uint32_t hash;
232
233                 flow_extract(packet, md, &flow);
234                 hash = flow_hash_5tuple(&flow, hash_act->hash_basis);
235                 md->dp_hash = hash ? hash : 1;
236             } else {
237                 /* Assert on unknown hash algorithm.  */
238                 OVS_NOT_REACHED();
239             }
240             break;
241         }
242
243         case OVS_ACTION_ATTR_PUSH_VLAN: {
244             const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
245             eth_push_vlan(packet, htons(ETH_TYPE_VLAN), vlan->vlan_tci);
246             break;
247         }
248
249         case OVS_ACTION_ATTR_POP_VLAN:
250             eth_pop_vlan(packet);
251             break;
252
253         case OVS_ACTION_ATTR_PUSH_MPLS: {
254             const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
255             push_mpls(packet, mpls->mpls_ethertype, mpls->mpls_lse);
256             break;
257          }
258
259         case OVS_ACTION_ATTR_POP_MPLS:
260             pop_mpls(packet, nl_attr_get_be16(a));
261             break;
262
263         case OVS_ACTION_ATTR_SET:
264             odp_execute_set_action(packet, nl_attr_get(a), md);
265             break;
266
267         case OVS_ACTION_ATTR_SAMPLE:
268             odp_execute_sample(dp, packet, steal, md, a, dp_execute_action,
269                                more_actions || left > NLA_ALIGN(a->nla_len));
270             break;
271
272         case OVS_ACTION_ATTR_UNSPEC:
273         case __OVS_ACTION_ATTR_MAX:
274             OVS_NOT_REACHED();
275         }
276     }
277 }
278
279 void
280 odp_execute_actions(void *dp, struct ofpbuf *packet, bool steal,
281                     struct pkt_metadata *md,
282                     const struct nlattr *actions, size_t actions_len,
283                     odp_execute_cb dp_execute_action)
284 {
285     odp_execute_actions__(dp, packet, steal, md, actions, actions_len,
286                           dp_execute_action, false);
287
288     if (!actions_len && steal) {
289         /* Drop action. */
290         ofpbuf_delete(packet);
291     }
292 }