1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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
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.
39 #include <netinet/in.h>
44 #include "command-line.h"
47 #include "discovery.h"
48 #include "fail-open.h"
55 #include "port-watcher.h"
56 #include "poll-loop.h"
57 #include "ratelimit.h"
62 #include "stp-secchan.h"
66 #include "vconn-ssl.h"
68 #include "vlog-socket.h"
71 #define THIS_MODULE VLM_secchan
74 const struct hook_class *class;
80 size_t n_hooks, allocated_hooks;
83 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
85 static void parse_options(int argc, char *argv[], struct settings *);
86 static void usage(void) NO_RETURN;
88 static struct pvconn *open_passive_vconn(const char *name);
89 static struct vconn *accept_vconn(struct pvconn *pvconn);
91 static struct relay *relay_create(struct rconn *local, struct rconn *remote,
93 static struct relay *relay_accept(const struct settings *, struct pvconn *);
94 static void relay_run(struct relay *, struct secchan *);
95 static void relay_wait(struct relay *);
96 static void relay_destroy(struct relay *);
99 main(int argc, char *argv[])
103 struct list relays = LIST_INITIALIZER(&relays);
105 struct secchan secchan;
107 struct pvconn *monitor;
109 struct pvconn *listeners[MAX_MGMT];
112 struct rconn *local_rconn, *remote_rconn;
113 struct relay *controller_relay;
114 struct discovery *discovery;
115 struct switch_status *switch_status;
116 struct port_watcher *pw;
120 set_program_name(argv[0]);
121 register_fault_handlers();
124 parse_options(argc, argv, &s);
125 signal(SIGPIPE, SIG_IGN);
127 secchan.hooks = NULL;
129 secchan.allocated_hooks = 0;
131 /* Start listening for management and monitoring connections. */
133 for (i = 0; i < s.n_listeners; i++) {
134 listeners[n_listeners++] = open_passive_vconn(s.listener_names[i]);
136 monitor = s.monitor_name ? open_passive_vconn(s.monitor_name) : NULL;
138 /* Initialize switch status hook. */
139 switch_status_start(&secchan, &s, &switch_status);
141 /* Start listening for vlogconf requests. */
142 retval = vlog_server_listen(NULL, NULL);
144 ofp_fatal(retval, "Could not listen for vlog connections");
147 die_if_already_running();
150 VLOG_WARN("OpenFlow reference implementation version %s", VERSION);
151 VLOG_WARN("OpenFlow protocol version 0x%02x", OFP_VERSION);
153 /* Connect to datapath. */
154 local_rconn = rconn_create(0, s.max_backoff);
155 rconn_connect(local_rconn, s.dp_name);
156 switch_status_register_category(switch_status, "local",
157 rconn_status_cb, local_rconn);
159 /* Connect to controller. */
160 remote_rconn = rconn_create(s.probe_interval, s.max_backoff);
161 if (s.controller_name) {
162 retval = rconn_connect(remote_rconn, s.controller_name);
163 if (retval == EAFNOSUPPORT) {
164 ofp_fatal(0, "No support for %s vconn", s.controller_name);
167 switch_status_register_category(switch_status, "remote",
168 rconn_status_cb, remote_rconn);
170 /* Start relaying. */
171 controller_relay = relay_create(local_rconn, remote_rconn, false);
172 list_push_back(&relays, &controller_relay->node);
175 port_watcher_start(&secchan, local_rconn, remote_rconn, &pw);
176 discovery = s.discovery ? discovery_init(&s, pw, switch_status) : NULL;
178 snat_start(&secchan, pw);
181 stp_start(&secchan, &s, pw, local_rconn, remote_rconn);
184 in_band_start(&secchan, &s, switch_status, pw, remote_rconn);
186 if (s.fail_mode == FAIL_OPEN) {
187 fail_open_start(&secchan, &s, switch_status,
188 local_rconn, remote_rconn);
191 rate_limit_start(&secchan, &s, switch_status,
192 local_rconn, remote_rconn);
200 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
201 relay_run(r, &secchan);
203 for (i = 0; i < n_listeners; i++) {
205 struct relay *r = relay_accept(&s, listeners[i]);
209 list_push_back(&relays, &r->node);
213 struct vconn *new = accept_vconn(monitor);
215 rconn_add_monitor(local_rconn, new);
218 for (i = 0; i < secchan.n_hooks; i++) {
219 if (secchan.hooks[i].class->periodic_cb) {
220 secchan.hooks[i].class->periodic_cb(secchan.hooks[i].aux);
224 char *controller_name;
225 if (rconn_is_connectivity_questionable(remote_rconn)) {
226 discovery_question_connectivity(discovery);
228 if (discovery_run(discovery, &controller_name)) {
229 if (controller_name) {
230 rconn_connect(remote_rconn, controller_name);
232 rconn_disconnect(remote_rconn);
237 /* Wait for something to happen. */
238 LIST_FOR_EACH (r, struct relay, node, &relays) {
241 for (i = 0; i < n_listeners; i++) {
242 pvconn_wait(listeners[i]);
245 pvconn_wait(monitor);
247 for (i = 0; i < secchan.n_hooks; i++) {
248 if (secchan.hooks[i].class->wait_cb) {
249 secchan.hooks[i].class->wait_cb(secchan.hooks[i].aux);
253 discovery_wait(discovery);
261 static struct pvconn *
262 open_passive_vconn(const char *name)
264 struct pvconn *pvconn;
267 retval = pvconn_open(name, &pvconn);
268 if (retval && retval != EAGAIN) {
269 ofp_fatal(retval, "opening %s", name);
274 static struct vconn *
275 accept_vconn(struct pvconn *pvconn)
280 retval = pvconn_accept(pvconn, OFP_VERSION, &new);
281 if (retval && retval != EAGAIN) {
282 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
288 add_hook(struct secchan *secchan, const struct hook_class *class, void *aux)
292 if (secchan->n_hooks >= secchan->allocated_hooks) {
293 secchan->allocated_hooks = secchan->allocated_hooks * 2 + 1;
294 secchan->hooks = xrealloc(secchan->hooks,
295 (sizeof *secchan->hooks
296 * secchan->allocated_hooks));
298 hook = &secchan->hooks[secchan->n_hooks++];
303 struct ofp_packet_in *
304 get_ofp_packet_in(struct relay *r)
306 struct ofpbuf *msg = r->halves[HALF_LOCAL].rxbuf;
307 struct ofp_header *oh = msg->data;
308 if (oh->type == OFPT_PACKET_IN) {
309 if (msg->size >= offsetof (struct ofp_packet_in, data)) {
312 VLOG_WARN("packet too short (%zu bytes) for packet_in",
320 get_ofp_packet_eth_header(struct relay *r, struct ofp_packet_in **opip,
321 struct eth_header **ethp)
323 const int min_len = offsetof(struct ofp_packet_in, data) + ETH_HEADER_LEN;
324 struct ofp_packet_in *opi = get_ofp_packet_in(r);
325 if (opi && ntohs(opi->header.length) >= min_len) {
327 *ethp = (void *) opi->data;
333 /* OpenFlow message relaying. */
335 static struct relay *
336 relay_accept(const struct settings *s, struct pvconn *pvconn)
338 struct vconn *new_remote, *new_local;
339 struct rconn *r1, *r2;
344 new_remote = accept_vconn(pvconn);
349 if (sscanf(s->dp_name, "nl:%d", &nl_index) == 1) {
350 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123.
351 * nl:123:0 opens a netlink connection to local datapath 123 without
352 * obtaining a subscription for ofp_packet_in or ofp_flow_expired
353 * messages. That's what we want here; management connections should
354 * not receive those messages, at least by default. */
355 vconn_name = xasprintf("nl:%d:0", nl_index);
357 /* We don't have a way to specify not to subscribe to those messages
358 * for other transports. (That's a defect: really this should be in
359 * the OpenFlow protocol, not the Netlink transport). */
360 VLOG_WARN_RL(&rl, "new management connection will receive "
361 "asynchronous messages");
362 vconn_name = xstrdup(s->dp_name);
365 retval = vconn_open(vconn_name, OFP_VERSION, &new_local);
367 VLOG_ERR_RL(&rl, "could not connect to %s (%s)",
368 vconn_name, strerror(retval));
369 vconn_close(new_remote);
374 /* Create and return relay. */
375 r1 = rconn_create(0, 0);
376 rconn_connect_unreliably(r1, vconn_name, new_local);
379 r2 = rconn_create(0, 0);
380 rconn_connect_unreliably(r2, "passive", new_remote);
382 return relay_create(r1, r2, true);
385 static struct relay *
386 relay_create(struct rconn *local, struct rconn *remote, bool is_mgmt_conn)
388 struct relay *r = xcalloc(1, sizeof *r);
389 r->halves[HALF_LOCAL].rconn = local;
390 r->halves[HALF_REMOTE].rconn = remote;
391 r->is_mgmt_conn = is_mgmt_conn;
396 call_local_packet_cbs(struct secchan *secchan, struct relay *r)
398 const struct hook *h;
399 for (h = secchan->hooks; h < &secchan->hooks[secchan->n_hooks]; h++) {
400 bool (*cb)(struct relay *, void *aux) = h->class->local_packet_cb;
401 if (cb && (cb)(r, h->aux)) {
409 call_remote_packet_cbs(struct secchan *secchan, struct relay *r)
411 const struct hook *h;
412 for (h = secchan->hooks; h < &secchan->hooks[secchan->n_hooks]; h++) {
413 bool (*cb)(struct relay *, void *aux) = h->class->remote_packet_cb;
414 if (cb && (cb)(r, h->aux)) {
422 relay_run(struct relay *r, struct secchan *secchan)
427 for (i = 0; i < 2; i++) {
428 rconn_run(r->halves[i].rconn);
431 /* Limit the number of iterations to prevent other tasks from starving. */
432 for (iteration = 0; iteration < 50; iteration++) {
433 bool progress = false;
434 for (i = 0; i < 2; i++) {
435 struct half *this = &r->halves[i];
436 struct half *peer = &r->halves[!i];
439 this->rxbuf = rconn_recv(this->rconn);
440 if (this->rxbuf && (i == HALF_REMOTE || !r->is_mgmt_conn)) {
442 ? call_local_packet_cbs(secchan, r)
443 : call_remote_packet_cbs(secchan, r))
445 ofpbuf_delete(this->rxbuf);
453 if (this->rxbuf && !this->n_txq) {
454 int retval = rconn_send(peer->rconn, this->rxbuf,
456 if (retval != EAGAIN) {
460 ofpbuf_delete(this->rxbuf);
471 if (r->is_mgmt_conn) {
472 for (i = 0; i < 2; i++) {
473 struct half *this = &r->halves[i];
474 if (!rconn_is_alive(this->rconn)) {
483 relay_wait(struct relay *r)
487 for (i = 0; i < 2; i++) {
488 struct half *this = &r->halves[i];
490 rconn_run_wait(this->rconn);
492 rconn_recv_wait(this->rconn);
498 relay_destroy(struct relay *r)
502 list_remove(&r->node);
503 for (i = 0; i < 2; i++) {
504 struct half *this = &r->halves[i];
505 rconn_destroy(this->rconn);
506 ofpbuf_delete(this->rxbuf);
511 /* User interface. */
514 parse_options(int argc, char *argv[], struct settings *s)
517 OPT_ACCEPT_VCONN = UCHAR_MAX + 1,
519 OPT_INACTIVITY_PROBE,
524 OPT_BOOTSTRAP_CA_CERT,
531 static struct option long_options[] = {
532 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
533 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
534 {"fail", required_argument, 0, 'F'},
535 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
536 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
537 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
538 {"listen", required_argument, 0, 'l'},
539 {"monitor", required_argument, 0, 'm'},
540 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
541 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
542 {"stp", no_argument, 0, OPT_STP},
543 {"no-stp", no_argument, 0, OPT_NO_STP},
544 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
545 {"in-band", no_argument, 0, OPT_IN_BAND},
546 {"verbose", optional_argument, 0, 'v'},
547 {"help", no_argument, 0, 'h'},
548 {"version", no_argument, 0, 'V'},
552 VCONN_SSL_LONG_OPTIONS
553 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
557 char *short_options = long_options_to_short_options(long_options);
558 char *accept_re = NULL;
561 /* Set defaults that we can figure out before parsing options. */
563 s->monitor_name = NULL;
564 s->fail_mode = FAIL_OPEN;
566 s->probe_interval = 15;
568 s->update_resolv_conf = true;
571 s->enable_stp = false;
576 c = getopt_long(argc, argv, short_options, long_options, NULL);
582 case OPT_ACCEPT_VCONN:
583 accept_re = optarg[0] == '^' ? optarg : xasprintf("^%s", optarg);
586 case OPT_NO_RESOLV_CONF:
587 s->update_resolv_conf = false;
591 if (!strcmp(optarg, "open")) {
592 s->fail_mode = FAIL_OPEN;
593 } else if (!strcmp(optarg, "closed")) {
594 s->fail_mode = FAIL_CLOSED;
596 ofp_fatal(0, "-f or --fail argument must be \"open\" "
601 case OPT_INACTIVITY_PROBE:
602 s->probe_interval = atoi(optarg);
603 if (s->probe_interval < 5) {
604 ofp_fatal(0, "--inactivity-probe argument must be at least 5");
609 if (!strcmp(optarg, "permanent")) {
610 s->max_idle = OFP_FLOW_PERMANENT;
612 s->max_idle = atoi(optarg);
613 if (s->max_idle < 1 || s->max_idle > 65535) {
614 ofp_fatal(0, "--max-idle argument must be between 1 and "
615 "65535 or the word 'permanent'");
620 case OPT_MAX_BACKOFF:
621 s->max_backoff = atoi(optarg);
622 if (s->max_backoff < 1) {
623 ofp_fatal(0, "--max-backoff argument must be at least 1");
624 } else if (s->max_backoff > 3600) {
625 s->max_backoff = 3600;
631 s->rate_limit = atoi(optarg);
632 if (s->rate_limit < 1) {
633 ofp_fatal(0, "--rate-limit argument must be at least 1");
636 s->rate_limit = 1000;
640 case OPT_BURST_LIMIT:
641 s->burst_limit = atoi(optarg);
642 if (s->burst_limit < 1) {
643 ofp_fatal(0, "--burst-limit argument must be at least 1");
648 s->enable_stp = true;
652 s->enable_stp = false;
655 case OPT_OUT_OF_BAND:
664 if (s->n_listeners >= MAX_MGMT) {
666 "-l or --listen may be specified at most %d times",
669 s->listener_names[s->n_listeners++] = optarg;
673 if (s->monitor_name) {
674 ofp_fatal(0, "-m or --monitor may only be specified once");
676 s->monitor_name = optarg;
683 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
686 DAEMON_OPTION_HANDLERS
691 VCONN_SSL_OPTION_HANDLERS
693 case OPT_BOOTSTRAP_CA_CERT:
694 vconn_ssl_set_ca_cert_file(optarg, true);
709 if (argc < 1 || argc > 2) {
710 ofp_fatal(0, "need one or two non-option arguments; "
711 "use --help for usage");
714 /* Local and remote vconns. */
715 s->dp_name = argv[0];
716 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
718 /* Set accept_controller_regex. */
720 accept_re = vconn_ssl_is_configured() ? "^ssl:.*" : ".*";
722 retval = regcomp(&s->accept_controller_regex, accept_re,
723 REG_NOSUB | REG_EXTENDED);
725 size_t length = regerror(retval, &s->accept_controller_regex, NULL, 0);
726 char *buffer = xmalloc(length);
727 regerror(retval, &s->accept_controller_regex, buffer, length);
728 ofp_fatal(0, "%s: %s", accept_re, buffer);
730 s->accept_controller_re = accept_re;
732 /* Mode of operation. */
733 s->discovery = s->controller_name == NULL;
734 if (s->discovery && !s->in_band) {
735 ofp_fatal(0, "Cannot perform discovery with out-of-band control");
740 if (s->rate_limit < 100) {
741 VLOG_WARN("Rate limit set to unusually low value %d",
744 if (!s->burst_limit) {
745 s->burst_limit = s->rate_limit / 4;
747 s->burst_limit = MAX(s->burst_limit, 1);
748 s->burst_limit = MIN(s->burst_limit, INT_MAX / 1000);
755 printf("%s: secure channel, a relay for OpenFlow messages.\n"
756 "usage: %s [OPTIONS] nl:DP_IDX [CONTROLLER]\n"
757 "where nl:DP_IDX is a datapath that has been added with dpctl.\n"
758 "CONTROLLER is an active OpenFlow connection method; if it is\n"
759 "omitted, then secchan performs controller discovery.\n",
760 program_name, program_name);
761 vconn_usage(true, true, true);
762 printf("\nController discovery options:\n"
763 " --accept-vconn=REGEX accept matching discovered controllers\n"
764 " --no-resolv-conf do not update /etc/resolv.conf\n"
765 "\nNetworking options:\n"
766 " -F, --fail=open|closed when controller connection fails:\n"
767 " closed: drop all packets\n"
768 " open (default): act as learning switch\n"
769 " --inactivity-probe=SECS time between inactivity probes\n"
770 " --max-idle=SECS max idle for flows set up by secchan\n"
771 " --max-backoff=SECS max time between controller connection\n"
772 " attempts (default: 15 seconds)\n"
773 " -l, --listen=METHOD allow management connections on METHOD\n"
774 " (a passive OpenFlow connection method)\n"
775 " -m, --monitor=METHOD copy traffic to/from kernel to METHOD\n"
776 " (a passive OpenFlow connection method)\n"
777 " --out-of-band controller connection is out-of-band\n"
778 " --stp enable 802.1D Spanning Tree Protocol\n"
779 " --no-stp disable 802.1D Spanning Tree Protocol\n"
780 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
781 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
782 " --burst-limit=BURST limit on packet credit for idle time\n");
785 printf("\nOther options:\n"
786 " -h, --help display this help message\n"
787 " -V, --version display version information\n");