netdev-dummy: Fix use-after-free error.
[sliver-openvswitch.git] / lib / netdev-dummy.c
1 /*
2  * Copyright (c) 2010, 2011, 2012 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "dummy.h"
20
21 #include <errno.h>
22
23 #include "flow.h"
24 #include "list.h"
25 #include "netdev-provider.h"
26 #include "odp-util.h"
27 #include "ofp-print.h"
28 #include "ofpbuf.h"
29 #include "packets.h"
30 #include "poll-loop.h"
31 #include "shash.h"
32 #include "unixctl.h"
33 #include "vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(netdev_dummy);
36
37 struct netdev_dev_dummy {
38     struct netdev_dev netdev_dev;
39     uint8_t hwaddr[ETH_ADDR_LEN];
40     int mtu;
41     struct netdev_stats stats;
42     enum netdev_flags flags;
43     unsigned int change_seq;
44
45     struct list devs;           /* List of child "netdev_dummy"s. */
46 };
47
48 struct netdev_dummy {
49     struct netdev netdev;
50     struct list node;           /* In netdev_dev_dummy's "devs" list. */
51     struct list recv_queue;
52     bool listening;
53 };
54
55 static struct shash dummy_netdev_devs = SHASH_INITIALIZER(&dummy_netdev_devs);
56
57 static int netdev_dummy_create(const struct netdev_class *, const char *,
58                                struct netdev_dev **);
59 static void netdev_dummy_poll_notify(const struct netdev *);
60
61 static bool
62 is_dummy_class(const struct netdev_class *class)
63 {
64     return class->create == netdev_dummy_create;
65 }
66
67 static struct netdev_dev_dummy *
68 netdev_dev_dummy_cast(const struct netdev_dev *netdev_dev)
69 {
70     assert(is_dummy_class(netdev_dev_get_class(netdev_dev)));
71     return CONTAINER_OF(netdev_dev, struct netdev_dev_dummy, netdev_dev);
72 }
73
74 static struct netdev_dummy *
75 netdev_dummy_cast(const struct netdev *netdev)
76 {
77     struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
78     assert(is_dummy_class(netdev_dev_get_class(netdev_dev)));
79     return CONTAINER_OF(netdev, struct netdev_dummy, netdev);
80 }
81
82 static int
83 netdev_dummy_create(const struct netdev_class *class, const char *name,
84                     struct netdev_dev **netdev_devp)
85 {
86     static unsigned int n = 0xaa550000;
87     struct netdev_dev_dummy *netdev_dev;
88
89     netdev_dev = xzalloc(sizeof *netdev_dev);
90     netdev_dev_init(&netdev_dev->netdev_dev, name, class);
91     netdev_dev->hwaddr[0] = 0xaa;
92     netdev_dev->hwaddr[1] = 0x55;
93     netdev_dev->hwaddr[2] = n >> 24;
94     netdev_dev->hwaddr[3] = n >> 16;
95     netdev_dev->hwaddr[4] = n >> 8;
96     netdev_dev->hwaddr[5] = n;
97     netdev_dev->mtu = 1500;
98     netdev_dev->flags = 0;
99     netdev_dev->change_seq = 1;
100     list_init(&netdev_dev->devs);
101
102     shash_add(&dummy_netdev_devs, name, netdev_dev);
103
104     n++;
105
106     *netdev_devp = &netdev_dev->netdev_dev;
107
108     return 0;
109 }
110
111 static void
112 netdev_dummy_destroy(struct netdev_dev *netdev_dev_)
113 {
114     struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
115
116     shash_find_and_delete(&dummy_netdev_devs,
117                           netdev_dev_get_name(netdev_dev_));
118     free(netdev_dev);
119 }
120
121 static int
122 netdev_dummy_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
123 {
124     struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
125     struct netdev_dummy *netdev;
126
127     netdev = xmalloc(sizeof *netdev);
128     netdev_init(&netdev->netdev, netdev_dev_);
129     list_init(&netdev->recv_queue);
130     netdev->listening = false;
131
132     *netdevp = &netdev->netdev;
133     list_push_back(&netdev_dev->devs, &netdev->node);
134     return 0;
135 }
136
137 static void
138 netdev_dummy_close(struct netdev *netdev_)
139 {
140     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
141     list_remove(&netdev->node);
142     ofpbuf_list_delete(&netdev->recv_queue);
143     free(netdev);
144 }
145
146 static int
147 netdev_dummy_listen(struct netdev *netdev_)
148 {
149     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
150     netdev->listening = true;
151     return 0;
152 }
153
154 static int
155 netdev_dummy_recv(struct netdev *netdev_, void *buffer, size_t size)
156 {
157     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
158     struct ofpbuf *packet;
159     size_t packet_size;
160
161     if (list_is_empty(&netdev->recv_queue)) {
162         return -EAGAIN;
163     }
164
165     packet = ofpbuf_from_list(list_pop_front(&netdev->recv_queue));
166     if (packet->size > size) {
167         return -EMSGSIZE;
168     }
169     packet_size = packet->size;
170
171     memcpy(buffer, packet->data, packet->size);
172     ofpbuf_delete(packet);
173
174     return packet_size;
175 }
176
177 static void
178 netdev_dummy_recv_wait(struct netdev *netdev_)
179 {
180     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
181     if (!list_is_empty(&netdev->recv_queue)) {
182         poll_immediate_wake();
183     }
184 }
185
186 static int
187 netdev_dummy_drain(struct netdev *netdev_)
188 {
189     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
190     ofpbuf_list_delete(&netdev->recv_queue);
191     return 0;
192 }
193
194 static int
195 netdev_dummy_set_etheraddr(struct netdev *netdev,
196                            const uint8_t mac[ETH_ADDR_LEN])
197 {
198     struct netdev_dev_dummy *dev =
199         netdev_dev_dummy_cast(netdev_get_dev(netdev));
200
201     if (!eth_addr_equals(dev->hwaddr, mac)) {
202         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
203         netdev_dummy_poll_notify(netdev);
204     }
205
206     return 0;
207 }
208
209 static int
210 netdev_dummy_get_etheraddr(const struct netdev *netdev,
211                            uint8_t mac[ETH_ADDR_LEN])
212 {
213     const struct netdev_dev_dummy *dev =
214         netdev_dev_dummy_cast(netdev_get_dev(netdev));
215
216     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
217     return 0;
218 }
219
220 static int
221 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
222 {
223     const struct netdev_dev_dummy *dev =
224         netdev_dev_dummy_cast(netdev_get_dev(netdev));
225
226     *mtup = dev->mtu;
227     return 0;
228 }
229
230 static int
231 netdev_dummy_set_mtu(const struct netdev *netdev, int mtu)
232 {
233     struct netdev_dev_dummy *dev =
234         netdev_dev_dummy_cast(netdev_get_dev(netdev));
235
236     dev->mtu = mtu;
237     return 0;
238 }
239
240 static int
241 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
242 {
243     const struct netdev_dev_dummy *dev =
244         netdev_dev_dummy_cast(netdev_get_dev(netdev));
245
246     *stats = dev->stats;
247     return 0;
248 }
249
250 static int
251 netdev_dummy_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
252 {
253     struct netdev_dev_dummy *dev =
254         netdev_dev_dummy_cast(netdev_get_dev(netdev));
255
256     dev->stats = *stats;
257     return 0;
258 }
259
260 static int
261 netdev_dummy_update_flags(struct netdev *netdev,
262                           enum netdev_flags off, enum netdev_flags on,
263                           enum netdev_flags *old_flagsp)
264 {
265     struct netdev_dev_dummy *dev =
266         netdev_dev_dummy_cast(netdev_get_dev(netdev));
267
268     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
269         return EINVAL;
270     }
271
272     *old_flagsp = dev->flags;
273     dev->flags |= on;
274     dev->flags &= ~off;
275     if (*old_flagsp != dev->flags) {
276         netdev_dummy_poll_notify(netdev);
277     }
278     return 0;
279 }
280
281 static unsigned int
282 netdev_dummy_change_seq(const struct netdev *netdev)
283 {
284     return netdev_dev_dummy_cast(netdev_get_dev(netdev))->change_seq;
285 }
286 \f
287 /* Helper functions. */
288
289 static void
290 netdev_dummy_poll_notify(const struct netdev *netdev)
291 {
292     struct netdev_dev_dummy *dev =
293         netdev_dev_dummy_cast(netdev_get_dev(netdev));
294
295     dev->change_seq++;
296     if (!dev->change_seq) {
297         dev->change_seq++;
298     }
299 }
300
301 static const struct netdev_class dummy_class = {
302     "dummy",
303     NULL,                       /* init */
304     NULL,                       /* run */
305     NULL,                       /* wait */
306
307     netdev_dummy_create,
308     netdev_dummy_destroy,
309     NULL,                       /* get_config */
310     NULL,                       /* set_config */
311
312     netdev_dummy_open,
313     netdev_dummy_close,
314
315     netdev_dummy_listen,
316     netdev_dummy_recv,
317     netdev_dummy_recv_wait,
318     netdev_dummy_drain,
319
320     NULL,                       /* send */
321     NULL,                       /* send_wait */
322
323     netdev_dummy_set_etheraddr,
324     netdev_dummy_get_etheraddr,
325     netdev_dummy_get_mtu,
326     netdev_dummy_set_mtu,
327     NULL,                       /* get_ifindex */
328     NULL,                       /* get_carrier */
329     NULL,                       /* get_carrier_resets */
330     NULL,                       /* get_miimon */
331     netdev_dummy_get_stats,
332     netdev_dummy_set_stats,
333
334     NULL,                       /* get_features */
335     NULL,                       /* set_advertisements */
336
337     NULL,                       /* set_policing */
338     NULL,                       /* get_qos_types */
339     NULL,                       /* get_qos_capabilities */
340     NULL,                       /* get_qos */
341     NULL,                       /* set_qos */
342     NULL,                       /* get_queue */
343     NULL,                       /* set_queue */
344     NULL,                       /* delete_queue */
345     NULL,                       /* get_queue_stats */
346     NULL,                       /* dump_queues */
347     NULL,                       /* dump_queue_stats */
348
349     NULL,                       /* get_in4 */
350     NULL,                       /* set_in4 */
351     NULL,                       /* get_in6 */
352     NULL,                       /* add_router */
353     NULL,                       /* get_next_hop */
354     NULL,                       /* get_status */
355     NULL,                       /* arp_lookup */
356
357     netdev_dummy_update_flags,
358
359     netdev_dummy_change_seq
360 };
361
362 static struct ofpbuf *
363 eth_from_packet_or_flow(const char *s)
364 {
365     enum odp_key_fitness fitness;
366     struct ofpbuf *packet;
367     struct ofpbuf odp_key;
368     struct flow flow;
369     int error;
370
371     if (!eth_from_hex(s, &packet)) {
372         return packet;
373     }
374
375     /* Convert string to datapath key.
376      *
377      * It would actually be nicer to parse an OpenFlow-like flow key here, but
378      * the code for that currently calls exit() on parse error.  We have to
379      * settle for parsing a datapath key for now.
380      */
381     ofpbuf_init(&odp_key, 0);
382     error = odp_flow_key_from_string(s, NULL, &odp_key);
383     if (error) {
384         ofpbuf_uninit(&odp_key);
385         return NULL;
386     }
387
388     /* Convert odp_key to flow. */
389     fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
390     if (fitness == ODP_FIT_ERROR) {
391         ofpbuf_uninit(&odp_key);
392         return NULL;
393     }
394
395     packet = ofpbuf_new(0);
396     flow_compose(packet, &flow);
397
398     ofpbuf_uninit(&odp_key);
399     return packet;
400 }
401
402 static void
403 netdev_dummy_receive(struct unixctl_conn *conn,
404                      int argc, const char *argv[], void *aux OVS_UNUSED)
405 {
406     struct netdev_dev_dummy *dummy_dev;
407     int n_listeners;
408     int i;
409
410     dummy_dev = shash_find_data(&dummy_netdev_devs, argv[1]);
411     if (!dummy_dev) {
412         unixctl_command_reply(conn, 501, "no such dummy netdev");
413         return;
414     }
415
416     n_listeners = 0;
417     for (i = 2; i < argc; i++) {
418         struct netdev_dummy *dev;
419         struct ofpbuf *packet;
420
421         packet = eth_from_packet_or_flow(argv[i]);
422         if (!packet) {
423             unixctl_command_reply(conn, 501, "bad packet syntax");
424             return;
425         }
426
427         n_listeners = 0;
428         LIST_FOR_EACH (dev, node, &dummy_dev->devs) {
429             if (dev->listening) {
430                 struct ofpbuf *copy = ofpbuf_clone(packet);
431                 list_push_back(&dev->recv_queue, &copy->list_node);
432                 n_listeners++;
433             }
434         }
435         ofpbuf_delete(packet);
436     }
437
438     if (!n_listeners) {
439         unixctl_command_reply(conn, 202, "packets queued but nobody listened");
440     } else {
441         unixctl_command_reply(conn, 200, "success");
442     }
443 }
444
445 void
446 netdev_dummy_register(void)
447 {
448     netdev_register_provider(&dummy_class);
449     unixctl_command_register("netdev-dummy/receive", "NAME PACKET|FLOW...",
450                              2, INT_MAX, netdev_dummy_receive, NULL);
451 }