ofpbuf: Fix use-after-free in bundle parse.
[cascardo/ovs.git] / lib / bundle.c
1 /* Copyright (c) 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17
18 #include "bundle.h"
19
20 #include <arpa/inet.h>
21 #include <inttypes.h>
22
23 #include "dynamic-string.h"
24 #include "multipath.h"
25 #include "meta-flow.h"
26 #include "nx-match.h"
27 #include "ofpbuf.h"
28 #include "ofp-actions.h"
29 #include "ofp-errors.h"
30 #include "ofp-util.h"
31 #include "openflow/nicira-ext.h"
32 #include "openvswitch/vlog.h"
33
34 VLOG_DEFINE_THIS_MODULE(bundle);
35
36 static ofp_port_t
37 execute_ab(const struct ofpact_bundle *bundle,
38            bool (*slave_enabled)(ofp_port_t ofp_port, void *aux), void *aux)
39 {
40     size_t i;
41
42     for (i = 0; i < bundle->n_slaves; i++) {
43         ofp_port_t slave = bundle->slaves[i];
44         if (slave_enabled(slave, aux)) {
45             return slave;
46         }
47     }
48
49     return OFPP_NONE;
50 }
51
52 static ofp_port_t
53 execute_hrw(const struct ofpact_bundle *bundle,
54             const struct flow *flow, struct flow_wildcards *wc,
55             bool (*slave_enabled)(ofp_port_t ofp_port, void *aux), void *aux)
56 {
57     uint32_t flow_hash, best_hash;
58     int best, i;
59
60     if (bundle->n_slaves > 1) {
61         flow_mask_hash_fields(flow, wc, bundle->fields);
62     }
63
64     flow_hash = flow_hash_fields(flow, bundle->fields, bundle->basis);
65     best = -1;
66     best_hash = 0;
67
68     for (i = 0; i < bundle->n_slaves; i++) {
69         if (slave_enabled(bundle->slaves[i], aux)) {
70             uint32_t hash = hash_2words(i, flow_hash);
71
72             if (best < 0 || hash > best_hash) {
73                 best_hash = hash;
74                 best = i;
75             }
76         }
77     }
78
79     return best >= 0 ? bundle->slaves[best] : OFPP_NONE;
80 }
81
82 /* Executes 'bundle' on 'flow'.  Sets fields in 'wc' that were used to
83  * calculate the result.  Uses 'slave_enabled' to determine if the slave
84  * designated by 'ofp_port' is up.  Returns the chosen slave, or
85  * OFPP_NONE if none of the slaves are acceptable. */
86 ofp_port_t
87 bundle_execute(const struct ofpact_bundle *bundle,
88                const struct flow *flow, struct flow_wildcards *wc,
89                bool (*slave_enabled)(ofp_port_t ofp_port, void *aux),
90                void *aux)
91 {
92     switch (bundle->algorithm) {
93     case NX_BD_ALG_HRW:
94         return execute_hrw(bundle, flow, wc, slave_enabled, aux);
95
96     case NX_BD_ALG_ACTIVE_BACKUP:
97         return execute_ab(bundle, slave_enabled, aux);
98
99     default:
100         OVS_NOT_REACHED();
101     }
102 }
103
104 enum ofperr
105 bundle_check(const struct ofpact_bundle *bundle, ofp_port_t max_ports,
106              const struct flow *flow)
107 {
108     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
109     size_t i;
110
111     if (bundle->dst.field) {
112         enum ofperr error = mf_check_dst(&bundle->dst, flow);
113         if (error) {
114             return error;
115         }
116     }
117
118     for (i = 0; i < bundle->n_slaves; i++) {
119         ofp_port_t ofp_port = bundle->slaves[i];
120
121         if (ofp_port != OFPP_NONE) {
122             enum ofperr error = ofpact_check_output_port(ofp_port, max_ports);
123             if (error) {
124                 VLOG_WARN_RL(&rl, "invalid slave %"PRIu16, ofp_port);
125                 return error;
126             }
127         }
128         /* Controller slaves are unsupported due to the lack of a max_len
129          * argument. This may or may not change in the future.  There doesn't
130          * seem to be a real-world use-case for supporting it. */
131         if (ofp_port == OFPP_CONTROLLER) {
132             VLOG_WARN_RL(&rl, "unsupported controller slave");
133             return OFPERR_OFPBAC_BAD_OUT_PORT;
134         }
135     }
136
137     return 0;
138 }
139
140
141 /* Helper for bundle_parse and bundle_parse_load.
142  *
143  * Returns NULL if successful, otherwise a malloc()'d string describing the
144  * error.  The caller is responsible for freeing the returned string.*/
145 static char * OVS_WARN_UNUSED_RESULT
146 bundle_parse__(const char *s, char **save_ptr,
147                const char *fields, const char *basis, const char *algorithm,
148                const char *slave_type, const char *dst,
149                const char *slave_delim, struct ofpbuf *ofpacts)
150 {
151     struct ofpact_bundle *bundle;
152
153     if (!slave_delim) {
154         return xasprintf("%s: not enough arguments to bundle action", s);
155     }
156
157     if (strcasecmp(slave_delim, "slaves")) {
158         return xasprintf("%s: missing slave delimiter, expected `slaves' "
159                          "got `%s'", s, slave_delim);
160     }
161
162     bundle = ofpact_put_BUNDLE(ofpacts);
163
164     for (;;) {
165         ofp_port_t slave_port;
166         char *slave;
167
168         slave = strtok_r(NULL, ", []", save_ptr);
169         if (!slave || bundle->n_slaves >= BUNDLE_MAX_SLAVES) {
170             break;
171         }
172
173         if (!ofputil_port_from_string(slave, &slave_port)) {
174             return xasprintf("%s: bad port number", slave);
175         }
176         ofpbuf_put(ofpacts, &slave_port, sizeof slave_port);
177
178         bundle = ofpacts->header;
179         bundle->n_slaves++;
180     }
181     ofpact_finish(ofpacts, &bundle->ofpact);
182
183     bundle = ofpacts->header;
184     bundle->basis = atoi(basis);
185
186     if (!strcasecmp(fields, "eth_src")) {
187         bundle->fields = NX_HASH_FIELDS_ETH_SRC;
188     } else if (!strcasecmp(fields, "symmetric_l4")) {
189         bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L4;
190     } else if (!strcasecmp(fields, "symmetric_l3l4")) {
191         bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3L4;
192     } else if (!strcasecmp(fields, "symmetric_l3l4+udp")) {
193         bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP;
194     } else {
195         return xasprintf("%s: unknown fields `%s'", s, fields);
196     }
197
198     if (!strcasecmp(algorithm, "active_backup")) {
199         bundle->algorithm = NX_BD_ALG_ACTIVE_BACKUP;
200     } else if (!strcasecmp(algorithm, "hrw")) {
201         bundle->algorithm = NX_BD_ALG_HRW;
202     } else {
203         return xasprintf("%s: unknown algorithm `%s'", s, algorithm);
204     }
205
206     if (strcasecmp(slave_type, "ofport")) {
207         return xasprintf("%s: unknown slave_type `%s'", s, slave_type);
208     }
209
210     if (dst) {
211         char *error = mf_parse_subfield(&bundle->dst, dst);
212         if (error) {
213             return error;
214         }
215
216         if (!mf_nxm_header(bundle->dst.field->id)) {
217             return xasprintf("%s: experimenter OXM field '%s' not supported",
218                              s, dst);
219         }
220     }
221
222     return NULL;
223 }
224
225 /* Converts a bundle action string contained in 's' to an nx_action_bundle and
226  * stores it in 'b'.  Sets 'b''s l2 pointer to NULL.
227  *
228  * Returns NULL if successful, otherwise a malloc()'d string describing the
229  * error.  The caller is responsible for freeing the returned string. */
230 char * OVS_WARN_UNUSED_RESULT
231 bundle_parse(const char *s, struct ofpbuf *ofpacts)
232 {
233     char *fields, *basis, *algorithm, *slave_type, *slave_delim;
234     char *tokstr, *save_ptr;
235     char *error;
236
237     save_ptr = NULL;
238     tokstr = xstrdup(s);
239     fields = strtok_r(tokstr, ", ", &save_ptr);
240     basis = strtok_r(NULL, ", ", &save_ptr);
241     algorithm = strtok_r(NULL, ", ", &save_ptr);
242     slave_type = strtok_r(NULL, ", ", &save_ptr);
243     slave_delim = strtok_r(NULL, ": ", &save_ptr);
244
245     error = bundle_parse__(s, &save_ptr, fields, basis, algorithm, slave_type,
246                            NULL, slave_delim, ofpacts);
247     free(tokstr);
248
249     return error;
250 }
251
252 /* Converts a bundle_load action string contained in 's' to an nx_action_bundle
253  * and stores it in 'b'.  Sets 'b''s l2 pointer to NULL.
254  *
255  * Returns NULL if successful, otherwise a malloc()'d string describing the
256  * error.  The caller is responsible for freeing the returned string.*/
257 char * OVS_WARN_UNUSED_RESULT
258 bundle_parse_load(const char *s, struct ofpbuf *ofpacts)
259 {
260     char *fields, *basis, *algorithm, *slave_type, *dst, *slave_delim;
261     char *tokstr, *save_ptr;
262     char *error;
263
264     save_ptr = NULL;
265     tokstr = xstrdup(s);
266     fields = strtok_r(tokstr, ", ", &save_ptr);
267     basis = strtok_r(NULL, ", ", &save_ptr);
268     algorithm = strtok_r(NULL, ", ", &save_ptr);
269     slave_type = strtok_r(NULL, ", ", &save_ptr);
270     dst = strtok_r(NULL, ", ", &save_ptr);
271     slave_delim = strtok_r(NULL, ": ", &save_ptr);
272
273     error = bundle_parse__(s, &save_ptr, fields, basis, algorithm, slave_type,
274                            dst, slave_delim, ofpacts);
275
276     free(tokstr);
277
278     return error;
279 }
280
281 /* Appends a human-readable representation of 'nab' to 's'. */
282 void
283 bundle_format(const struct ofpact_bundle *bundle, struct ds *s)
284 {
285     const char *action, *fields, *algorithm;
286     size_t i;
287
288     fields = flow_hash_fields_to_str(bundle->fields);
289
290     switch (bundle->algorithm) {
291     case NX_BD_ALG_HRW:
292         algorithm = "hrw";
293         break;
294     case NX_BD_ALG_ACTIVE_BACKUP:
295         algorithm = "active_backup";
296         break;
297     default:
298         algorithm = "<unknown>";
299     }
300
301     action = bundle->dst.field ? "bundle_load" : "bundle";
302
303     ds_put_format(s, "%s(%s,%"PRIu16",%s,%s,", action, fields,
304                   bundle->basis, algorithm, "ofport");
305
306     if (bundle->dst.field) {
307         mf_format_subfield(&bundle->dst, s);
308         ds_put_cstr(s, ",");
309     }
310
311     ds_put_cstr(s, "slaves:");
312     for (i = 0; i < bundle->n_slaves; i++) {
313         if (i) {
314             ds_put_cstr(s, ",");
315         }
316
317         ofputil_format_port(bundle->slaves[i], s);
318     }
319
320     ds_put_cstr(s, ")");
321 }