155dfa9eef98b33399733ce47b43bd971aefbf8a
[sliver-openvswitch.git] / secchan / secchan.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 <errno.h>
35 #include <getopt.h>
36 #include <poll.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <time.h>
40 #include <unistd.h>
41
42 #include "buffer.h"
43 #include "command-line.h"
44 #include "compiler.h"
45 #include "fault.h"
46 #include "list.h"
47 #include "util.h"
48 #include "rconn.h"
49 #include "vconn-ssl.h"
50 #include "vlog-socket.h"
51 #include "openflow.h"
52 #include "poll-loop.h"
53 #include "vconn.h"
54
55 #include "vlog.h"
56 #define THIS_MODULE VLM_secchan
57
58 static void parse_options(int argc, char *argv[]);
59 static void usage(void) NO_RETURN;
60
61 static struct vconn *listen_vconn = NULL;
62
63 struct half {
64     struct rconn *rconn;
65     struct buffer *rxbuf;
66 };
67
68 struct relay {
69     struct list node;
70     struct half halves[2];
71 };
72
73 static struct list relays = LIST_INITIALIZER(&relays);
74
75 static void new_management_connection(const char *nl_name, struct vconn *new_remote);
76 static void relay_create(struct rconn *, struct rconn *);
77 static void relay_run(struct relay *);
78 static void relay_wait(struct relay *);
79 static void relay_destroy(struct relay *);
80
81 int
82 main(int argc, char *argv[])
83 {
84     const char *nl_name;
85     int retval;
86
87     set_program_name(argv[0]);
88     register_fault_handlers();
89     vlog_init();
90     parse_options(argc, argv);
91
92     if (argc - optind != 2) {
93         fatal(0,
94               "need exactly two non-option arguments; use --help for usage");
95     }
96     nl_name = argv[optind];
97     if (strncmp(nl_name, "nl:", 3)
98         || strlen(nl_name) < 4
99         || nl_name[strspn(nl_name + 3, "0123456789") + 3]) {
100         fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", nl_name);
101     }
102
103     retval = vlog_server_listen(NULL, NULL);
104     if (retval) {
105         fatal(retval, "Could not listen for vlog connections");
106     }
107
108     relay_create(rconn_new(argv[optind], 1), rconn_new(argv[optind + 1], 1));
109     for (;;) {
110         struct relay *r, *n;
111
112         /* Do work. */
113         LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
114             relay_run(r);
115         }
116         if (listen_vconn) {
117             struct vconn *new_remote;
118             for (;;) {
119                 retval = vconn_accept(listen_vconn, &new_remote);
120                 if (retval) {
121                     if (retval != EAGAIN) {
122                         VLOG_WARN("accept failed (%s)", strerror(retval));
123                     }
124                     break;
125                 }
126
127                 new_management_connection(nl_name, new_remote);
128             }
129         }
130
131         /* Wait for something to happen. */
132         LIST_FOR_EACH (r, struct relay, node, &relays) {
133             relay_wait(r);
134         }
135         if (listen_vconn) {
136             vconn_accept_wait(listen_vconn);
137         }
138         poll_block();
139     }
140
141     return 0;
142 }
143
144 static void
145 new_management_connection(const char *nl_name, struct vconn *new_remote)
146 {
147     char *nl_name_without_subscription;
148     struct vconn *new_local;
149     struct rconn *r1, *r2;
150     int retval;
151
152     /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123.  We
153      * only accept the former syntax in main().
154      *
155      * nl:123:0 opens a netlink connection to local datapath 123 without
156      * obtaining a subscription for ofp_packet_in or ofp_flow_expired
157      * messages.*/
158     nl_name_without_subscription = xasprintf("%s:0", nl_name);
159     retval = vconn_open(nl_name_without_subscription, &new_local);
160     if (retval) {
161         VLOG_ERR("could not connect to %s (%s)",
162                  nl_name_without_subscription, strerror(retval));
163         vconn_close(new_remote);
164         return;
165     }
166     free(nl_name_without_subscription);
167
168     /* Add it to the relay list. */
169     r1 = rconn_new_from_vconn(nl_name_without_subscription, 1, new_local);
170     r2 = rconn_new_from_vconn("passive", 1, new_remote);
171     relay_create(r1, r2);
172 }
173
174 static void
175 relay_create(struct rconn *a, struct rconn *b)
176 {
177     struct relay *r;
178     int i;
179
180     r = xmalloc(sizeof *r);
181     for (i = 0; i < 2; i++) {
182         r->halves[i].rconn = i ? b : a;
183         r->halves[i].rxbuf = NULL;
184     }
185     list_push_back(&relays, &r->node);
186 }
187
188 static void
189 relay_run(struct relay *r)
190 {
191     int iteration;
192     int i;
193
194     for (i = 0; i < 2; i++) {
195         rconn_run(r->halves[i].rconn);
196     }
197
198     /* Limit the number of iterations to prevent other tasks from starving. */
199     for (iteration = 0; iteration < 50; iteration++) {
200         bool progress = false;
201         for (i = 0; i < 2; i++) {
202             struct half *this = &r->halves[i];
203             struct half *peer = &r->halves[!i];
204
205             if (!this->rxbuf) {
206                 this->rxbuf = rconn_recv(this->rconn);
207             }
208
209             if (this->rxbuf) {
210                 int retval = rconn_send(peer->rconn, this->rxbuf);
211                 if (retval != EAGAIN) {
212                     this->rxbuf = NULL;
213                     if (!retval) {
214                         progress = true;
215                     }
216                 }
217             }
218         }
219         if (!progress) {
220             break;
221         }
222     }
223
224     for (i = 0; i < 2; i++) {
225         struct half *this = &r->halves[i];
226         if (!rconn_is_alive(this->rconn)) {
227             relay_destroy(r);
228             return;
229         }
230     }
231 }
232
233 static void
234 relay_wait(struct relay *r)
235 {
236     int i;
237
238     for (i = 0; i < 2; i++) {
239         struct half *this = &r->halves[i];
240
241         rconn_run_wait(this->rconn);
242         if (!this->rxbuf) {
243             rconn_recv_wait(this->rconn);
244         }
245     }
246 }
247
248 static void
249 relay_destroy(struct relay *r)
250 {
251     int i;
252
253     list_remove(&r->node);
254     for (i = 0; i < 2; i++) {
255         struct half *this = &r->halves[i];
256         rconn_destroy(this->rconn);
257         buffer_delete(this->rxbuf);
258     }
259     free(r);
260 }
261
262 static void
263 parse_options(int argc, char *argv[]) 
264 {
265     static struct option long_options[] = {
266         {"listen",      required_argument, 0, 'l'},
267         {"verbose",     optional_argument, 0, 'v'},
268         {"help",        no_argument, 0, 'h'},
269         {"version",     no_argument, 0, 'V'},
270 #ifdef HAVE_OPENSSL
271         {"private-key", required_argument, 0, 'p'},
272         {"certificate", required_argument, 0, 'c'},
273         {"ca-cert",     required_argument, 0, 'C'},
274 #endif
275         {0, 0, 0, 0},
276     };
277     char *short_options = long_options_to_short_options(long_options);
278     
279     for (;;) {
280         int retval;
281         int c;
282
283         c = getopt_long(argc, argv, short_options, long_options, NULL);
284         if (c == -1) {
285             break;
286         }
287
288         switch (c) {
289         case 'l':
290             if (listen_vconn) {
291                 fatal(0, "-l or --listen may be only specified once");
292             }
293             retval = vconn_open(optarg, &listen_vconn);
294             if (retval && retval != EAGAIN) {
295                 fatal(retval, "opening %s", optarg);
296             }
297             if (!vconn_is_passive(listen_vconn)) {
298                 fatal(0, "%s is not a passive vconn", optarg);
299             }
300             break;
301
302         case 'h':
303             usage();
304
305         case 'V':
306             printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
307             exit(EXIT_SUCCESS);
308
309         case 'v':
310             vlog_set_verbosity(optarg);
311             break;
312
313 #ifdef HAVE_OPENSSL
314         case 'p':
315             vconn_ssl_set_private_key_file(optarg);
316             break;
317
318         case 'c':
319             vconn_ssl_set_certificate_file(optarg);
320             break;
321
322         case 'C':
323             vconn_ssl_set_ca_cert_file(optarg);
324             break;
325 #endif
326
327         case '?':
328             exit(EXIT_FAILURE);
329
330         default:
331             abort();
332         }
333     }
334     free(short_options);
335 }
336
337 static void
338 usage(void)
339 {
340     printf("%s: Secure Channel, a relay for OpenFlow messages.\n"
341            "usage: %s [OPTIONS] LOCAL REMOTE\n"
342            "where LOCAL and REMOTE are active OpenFlow connection methods.\n",
343            program_name, program_name);
344     vconn_usage(true, true);
345     printf("\nNetworking options:\n"
346            "  -l, --listen=METHOD     allow management connections on METHOD\n"
347            "                          (a passive OpenFlow connection method)\n"
348            "\nOther options:\n"
349            "  -v, --verbose           set maximum verbosity level\n"
350            "  -h, --help              display this help message\n"
351            "  -V, --version           display version information\n");
352     exit(EXIT_SUCCESS);
353 }