Never free an skb that has been passed to genlmsg_reply().
[sliver-openvswitch.git] / secchan / discovery.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  *
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  *
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include <config.h>
35 #include "discovery.h"
36 #include <inttypes.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include "dhcp-client.h"
40 #include "dhcp.h"
41 #include "netdev.h"
42 #include "openflow/openflow.h"
43 #include "packets.h"
44 #include "port-watcher.h"
45 #include "secchan.h"
46 #include "status.h"
47
48 #define THIS_MODULE VLM_discovery
49 #include "vlog.h"
50
51 struct discovery
52 {
53     const struct settings *s;
54     struct dhclient *dhcp;
55     int n_changes;
56 };
57
58 static void modify_dhcp_request(struct dhcp_msg *, void *aux);
59 static bool validate_dhcp_offer(const struct dhcp_msg *, void *aux);
60
61 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
62
63 static void
64 discovery_status_cb(struct status_reply *sr, void *d_)
65 {
66     struct discovery *d = d_;
67
68     status_reply_put(sr, "accept-remote=%s", d->s->accept_controller_re);
69     status_reply_put(sr, "n-changes=%d", d->n_changes);
70     if (d->dhcp) {
71         status_reply_put(sr, "state=%s", dhclient_get_state(d->dhcp));
72         status_reply_put(sr, "state-elapsed=%u",
73                          dhclient_get_state_elapsed(d->dhcp)); 
74         if (dhclient_is_bound(d->dhcp)) {
75             uint32_t ip = dhclient_get_ip(d->dhcp);
76             uint32_t netmask = dhclient_get_netmask(d->dhcp);
77             uint32_t router = dhclient_get_router(d->dhcp);
78
79             const struct dhcp_msg *cfg = dhclient_get_config(d->dhcp);
80             uint32_t dns_server;
81             char *domain_name;
82             int i;
83
84             status_reply_put(sr, "ip="IP_FMT, IP_ARGS(&ip));
85             status_reply_put(sr, "netmask="IP_FMT, IP_ARGS(&netmask));
86             if (router) {
87                 status_reply_put(sr, "router="IP_FMT, IP_ARGS(&router));
88             }
89
90             for (i = 0; dhcp_msg_get_ip(cfg, DHCP_CODE_DNS_SERVER, i,
91                                         &dns_server);
92                  i++) {
93                 status_reply_put(sr, "dns%d="IP_FMT, i, IP_ARGS(&dns_server));
94             }
95
96             domain_name = dhcp_msg_get_string(cfg, DHCP_CODE_DOMAIN_NAME);
97             if (domain_name) {
98                 status_reply_put(sr, "domain=%s", domain_name);
99                 free(domain_name);
100             }
101
102             status_reply_put(sr, "lease-remaining=%u",
103                              dhclient_get_lease_remaining(d->dhcp));
104         }
105     }
106 }
107
108 static void
109 discovery_local_port_cb(const struct ofp_phy_port *port, void *d_) 
110 {
111     struct discovery *d = d_;
112     if (port) {
113         char name[OFP_MAX_PORT_NAME_LEN + 1];
114         struct netdev *netdev;
115         int retval;
116
117         /* Check that this was really a change. */
118         get_port_name(port, name, sizeof name);
119         if (d->dhcp && !strcmp(netdev_get_name(dhclient_get_netdev(d->dhcp)),
120                                name)) {
121             return;
122         }
123
124         /* Destroy current DHCP client. */
125         dhclient_destroy(d->dhcp);
126         d->dhcp = NULL;
127
128         /* Bring local network device up. */
129         retval = netdev_open(name, NETDEV_ETH_TYPE_NONE, &netdev);
130         if (retval) {
131             VLOG_ERR("Could not open %s device, discovery disabled: %s",
132                      name, strerror(retval));
133             return;
134         }
135         retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
136         if (retval) {
137             VLOG_ERR("Could not bring %s device up, discovery disabled: %s",
138                      name, strerror(retval));
139             return;
140         }
141         netdev_close(netdev);
142
143         /* Initialize DHCP client. */
144         retval = dhclient_create(name, modify_dhcp_request,
145                                  validate_dhcp_offer, (void *) d->s, &d->dhcp);
146         if (retval) {
147             VLOG_ERR("Failed to initialize DHCP client, "
148                      "discovery disabled: %s", strerror(retval));
149             return;
150         }
151         dhclient_set_max_timeout(d->dhcp, 3);
152         dhclient_init(d->dhcp, 0);
153     } else {
154         dhclient_destroy(d->dhcp);
155         d->dhcp = NULL;
156     }
157 }
158
159
160 struct discovery *
161 discovery_init(const struct settings *s, struct port_watcher *pw,
162                struct switch_status *ss)
163 {
164     struct discovery *d;
165
166     d = xmalloc(sizeof *d);
167     d->s = s;
168     d->dhcp = NULL;
169     d->n_changes = 0;
170
171     switch_status_register_category(ss, "discovery", discovery_status_cb, d);
172     port_watcher_register_local_port_callback(pw, discovery_local_port_cb, d);
173
174     return d;
175 }
176
177 void
178 discovery_question_connectivity(struct discovery *d)
179 {
180     if (d->dhcp) {
181         dhclient_force_renew(d->dhcp, 15); 
182     }
183 }
184
185 bool
186 discovery_run(struct discovery *d, char **controller_name)
187 {
188     if (!d->dhcp) {
189         *controller_name = NULL;
190         return true;
191     }
192
193     dhclient_run(d->dhcp);
194     if (!dhclient_changed(d->dhcp)) {
195         return false;
196     }
197
198     dhclient_configure_netdev(d->dhcp);
199     if (d->s->update_resolv_conf) {
200         dhclient_update_resolv_conf(d->dhcp);
201     }
202
203     if (dhclient_is_bound(d->dhcp)) {
204         *controller_name = dhcp_msg_get_string(dhclient_get_config(d->dhcp),
205                                                DHCP_CODE_OFP_CONTROLLER_VCONN);
206         VLOG_WARN("%s: discovered controller", *controller_name);
207         d->n_changes++;
208     } else {
209         *controller_name = NULL;
210         if (d->n_changes) {
211             VLOG_WARN("discovered controller no longer available");
212             d->n_changes++;
213         }
214     }
215     return true;
216 }
217
218 void
219 discovery_wait(struct discovery *d)
220 {
221     if (d->dhcp) {
222         dhclient_wait(d->dhcp); 
223     }
224 }
225
226 static void
227 modify_dhcp_request(struct dhcp_msg *msg, void *aux)
228 {
229     dhcp_msg_put_string(msg, DHCP_CODE_VENDOR_CLASS, "OpenFlow");
230 }
231
232 static bool
233 validate_dhcp_offer(const struct dhcp_msg *msg, void *s_)
234 {
235     const struct settings *s = s_;
236     char *vconn_name;
237     bool accept;
238
239     vconn_name = dhcp_msg_get_string(msg, DHCP_CODE_OFP_CONTROLLER_VCONN);
240     if (!vconn_name) {
241         VLOG_WARN_RL(&rl, "rejecting DHCP offer missing controller vconn");
242         return false;
243     }
244     accept = !regexec(&s->accept_controller_regex, vconn_name, 0, NULL, 0);
245     if (!accept) {
246         VLOG_WARN_RL(&rl, "rejecting controller vconn that fails to match %s",
247                      s->accept_controller_re);
248     }
249     free(vconn_name);
250     return accept;
251 }