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"
48 #include "discovery.h"
50 #include "fail-open.h"
55 #include "openflow/openflow.h"
57 #include "port-watcher.h"
58 #include "poll-loop.h"
59 #include "ratelimit.h"
64 #include "stp-secchan.h"
68 #include "vconn-ssl.h"
70 #include "vlog-socket.h"
73 #define THIS_MODULE VLM_secchan
76 const struct hook_class *class;
82 size_t n_hooks, allocated_hooks;
85 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
87 static void parse_options(int argc, char *argv[], struct settings *);
88 static void usage(void) NO_RETURN;
90 static char *vconn_name_without_subscription(const char *);
91 static struct pvconn *open_passive_vconn(const char *name);
92 static struct vconn *accept_vconn(struct pvconn *pvconn);
94 static struct relay *relay_create(struct rconn *async,
95 struct rconn *local, struct rconn *remote);
96 static struct relay *relay_accept(const struct settings *, struct pvconn *);
97 static void relay_run(struct relay *, struct secchan *);
98 static void relay_wait(struct relay *);
99 static void relay_destroy(struct relay *);
102 main(int argc, char *argv[])
106 struct list relays = LIST_INITIALIZER(&relays);
108 struct secchan secchan;
110 struct pvconn *monitor;
112 struct pvconn *listeners[MAX_MGMT];
115 char *local_rconn_name;
116 struct rconn *async_rconn, *local_rconn, *remote_rconn;
117 struct relay *controller_relay;
118 struct discovery *discovery;
119 struct switch_status *switch_status;
120 struct port_watcher *pw;
124 set_program_name(argv[0]);
125 register_fault_handlers();
128 parse_options(argc, argv, &s);
129 signal(SIGPIPE, SIG_IGN);
131 secchan.hooks = NULL;
133 secchan.allocated_hooks = 0;
135 /* Start listening for management and monitoring connections. */
137 for (i = 0; i < s.n_listeners; i++) {
138 listeners[n_listeners++] = open_passive_vconn(s.listener_names[i]);
140 monitor = s.monitor_name ? open_passive_vconn(s.monitor_name) : NULL;
142 /* Initialize switch status hook. */
143 switch_status_start(&secchan, &s, &switch_status);
145 die_if_already_running();
148 /* Start listening for vlogconf requests. */
149 retval = vlog_server_listen(NULL, NULL);
151 ofp_fatal(retval, "Could not listen for vlog connections");
154 VLOG_WARN("OpenFlow reference implementation version %s", VERSION BUILDNR);
155 VLOG_WARN("OpenFlow protocol version 0x%02x", OFP_VERSION);
157 /* Check datapath name, to try to catch command-line invocation errors. */
158 if (strncmp(s.dp_name, "nl:", 3) && strncmp(s.dp_name, "unix:", 5)
159 && !s.controller_name) {
160 VLOG_WARN("Controller not specified and datapath is not nl: or "
161 "unix:. (Did you forget to specify the datapath?)");
164 /* Connect to datapath with a subscription for asynchronous events. */
165 async_rconn = rconn_create(0, s.max_backoff);
166 rconn_connect(async_rconn, s.dp_name);
167 switch_status_register_category(switch_status, "async",
168 rconn_status_cb, async_rconn);
170 /* Connect to datapath without a subscription, for requests and replies. */
171 local_rconn_name = vconn_name_without_subscription(s.dp_name);
172 local_rconn = rconn_create(0, s.max_backoff);
173 rconn_connect(local_rconn, local_rconn_name);
174 free(local_rconn_name);
175 switch_status_register_category(switch_status, "local",
176 rconn_status_cb, local_rconn);
178 /* Connect to controller. */
179 remote_rconn = rconn_create(s.probe_interval, s.max_backoff);
180 if (s.controller_name) {
181 retval = rconn_connect(remote_rconn, s.controller_name);
182 if (retval == EAFNOSUPPORT) {
183 ofp_fatal(0, "No support for %s vconn", s.controller_name);
186 switch_status_register_category(switch_status, "remote",
187 rconn_status_cb, remote_rconn);
189 /* Start relaying. */
190 controller_relay = relay_create(async_rconn, local_rconn, remote_rconn);
191 list_push_back(&relays, &controller_relay->node);
194 port_watcher_start(&secchan, local_rconn, remote_rconn, &pw);
195 discovery = s.discovery ? discovery_init(&s, pw, switch_status) : NULL;
197 snat_start(&secchan, pw);
200 stp_start(&secchan, &s, pw, local_rconn, remote_rconn);
203 in_band_start(&secchan, &s, switch_status, pw, remote_rconn);
205 if (s.fail_mode == FAIL_OPEN) {
206 fail_open_start(&secchan, &s, switch_status,
207 local_rconn, remote_rconn);
210 rate_limit_start(&secchan, &s, switch_status, remote_rconn);
212 if (s.command_acl[0]) {
213 executer_start(&secchan, &s);
221 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
222 relay_run(r, &secchan);
224 for (i = 0; i < n_listeners; i++) {
226 struct relay *r = relay_accept(&s, listeners[i]);
230 list_push_back(&relays, &r->node);
234 struct vconn *new = accept_vconn(monitor);
236 /* XXX should monitor async_rconn too but rconn_add_monitor()
237 * takes ownership of the vconn passed in. */
238 rconn_add_monitor(local_rconn, new);
241 for (i = 0; i < secchan.n_hooks; i++) {
242 if (secchan.hooks[i].class->periodic_cb) {
243 secchan.hooks[i].class->periodic_cb(secchan.hooks[i].aux);
247 char *controller_name;
248 if (rconn_is_connectivity_questionable(remote_rconn)) {
249 discovery_question_connectivity(discovery);
251 if (discovery_run(discovery, &controller_name)) {
252 if (controller_name) {
253 rconn_connect(remote_rconn, controller_name);
255 rconn_disconnect(remote_rconn);
260 /* Wait for something to happen. */
261 LIST_FOR_EACH (r, struct relay, node, &relays) {
264 for (i = 0; i < n_listeners; i++) {
265 pvconn_wait(listeners[i]);
268 pvconn_wait(monitor);
270 for (i = 0; i < secchan.n_hooks; i++) {
271 if (secchan.hooks[i].class->wait_cb) {
272 secchan.hooks[i].class->wait_cb(secchan.hooks[i].aux);
276 discovery_wait(discovery);
284 static struct pvconn *
285 open_passive_vconn(const char *name)
287 struct pvconn *pvconn;
290 retval = pvconn_open(name, &pvconn);
291 if (retval && retval != EAGAIN) {
292 ofp_fatal(retval, "opening %s", name);
297 static struct vconn *
298 accept_vconn(struct pvconn *pvconn)
303 retval = pvconn_accept(pvconn, OFP_VERSION, &new);
304 if (retval && retval != EAGAIN) {
305 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
311 add_hook(struct secchan *secchan, const struct hook_class *class, void *aux)
315 if (secchan->n_hooks >= secchan->allocated_hooks) {
316 secchan->allocated_hooks = secchan->allocated_hooks * 2 + 1;
317 secchan->hooks = xrealloc(secchan->hooks,
318 (sizeof *secchan->hooks
319 * secchan->allocated_hooks));
321 hook = &secchan->hooks[secchan->n_hooks++];
326 struct ofp_packet_in *
327 get_ofp_packet_in(struct relay *r)
329 struct ofpbuf *msg = r->halves[HALF_LOCAL].rxbuf;
330 struct ofp_header *oh = msg->data;
331 if (oh->type == OFPT_PACKET_IN) {
332 if (msg->size >= offsetof (struct ofp_packet_in, data)) {
335 VLOG_WARN("packet too short (%zu bytes) for packet_in",
343 get_ofp_packet_eth_header(struct relay *r, struct ofp_packet_in **opip,
344 struct eth_header **ethp)
346 const int min_len = offsetof(struct ofp_packet_in, data) + ETH_HEADER_LEN;
347 struct ofp_packet_in *opi = get_ofp_packet_in(r);
348 if (opi && ntohs(opi->header.length) >= min_len) {
350 *ethp = (void *) opi->data;
356 /* OpenFlow message relaying. */
358 /* Returns a malloc'd string containing a copy of 'vconn_name' modified not to
359 * subscribe to asynchronous messages such as 'ofp_packet_in' events (if
362 vconn_name_without_subscription(const char *vconn_name)
365 if (sscanf(vconn_name, "nl:%d", &nl_index) == 1) {
366 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123.
367 * nl:123:0 opens a netlink connection to local datapath 123 without
368 * obtaining a subscription for ofp_packet_in or ofp_flow_expired
370 return xasprintf("nl:%d:0", nl_index);
372 /* We don't have a way to specify not to subscribe to those messages
373 * for other transports. (That's a defect: really this should be in
374 * the OpenFlow protocol, not the Netlink transport). */
375 VLOG_WARN_RL(&rl, "new management connection will receive "
376 "asynchronous messages");
377 return xstrdup(vconn_name);
381 static struct relay *
382 relay_accept(const struct settings *s, struct pvconn *pvconn)
384 struct vconn *new_remote, *new_local;
385 struct rconn *r1, *r2;
389 new_remote = accept_vconn(pvconn);
394 vconn_name = vconn_name_without_subscription(s->dp_name);
395 retval = vconn_open(vconn_name, OFP_VERSION, &new_local);
397 VLOG_ERR_RL(&rl, "could not connect to %s (%s)",
398 vconn_name, strerror(retval));
399 vconn_close(new_remote);
404 /* Create and return relay. */
405 r1 = rconn_create(0, 0);
406 rconn_connect_unreliably(r1, vconn_name, new_local);
409 r2 = rconn_create(0, 0);
410 rconn_connect_unreliably(r2, "passive", new_remote);
412 return relay_create(NULL, r1, r2);
415 static struct relay *
416 relay_create(struct rconn *async, struct rconn *local, struct rconn *remote)
418 struct relay *r = xcalloc(1, sizeof *r);
419 r->halves[HALF_LOCAL].rconn = local;
420 r->halves[HALF_REMOTE].rconn = remote;
421 r->is_mgmt_conn = async == NULL;
422 r->async_rconn = async;
427 call_local_packet_cbs(struct secchan *secchan, struct relay *r)
429 const struct hook *h;
430 for (h = secchan->hooks; h < &secchan->hooks[secchan->n_hooks]; h++) {
431 bool (*cb)(struct relay *, void *aux) = h->class->local_packet_cb;
432 if (cb && (cb)(r, h->aux)) {
440 call_remote_packet_cbs(struct secchan *secchan, struct relay *r)
442 const struct hook *h;
443 for (h = secchan->hooks; h < &secchan->hooks[secchan->n_hooks]; h++) {
444 bool (*cb)(struct relay *, void *aux) = h->class->remote_packet_cb;
445 if (cb && (cb)(r, h->aux)) {
453 relay_run(struct relay *r, struct secchan *secchan)
458 if (r->async_rconn) {
459 rconn_run(r->async_rconn);
461 for (i = 0; i < 2; i++) {
462 rconn_run(r->halves[i].rconn);
465 /* Limit the number of iterations to prevent other tasks from starving. */
466 for (iteration = 0; iteration < 50; iteration++) {
467 bool progress = false;
468 for (i = 0; i < 2; i++) {
469 struct half *this = &r->halves[i];
470 struct half *peer = &r->halves[!i];
473 this->rxbuf = rconn_recv(this->rconn);
474 if (!this->rxbuf && i == HALF_LOCAL && r->async_rconn) {
475 this->rxbuf = rconn_recv(r->async_rconn);
477 if (this->rxbuf && (i == HALF_REMOTE || !r->is_mgmt_conn)) {
479 ? call_local_packet_cbs(secchan, r)
480 : call_remote_packet_cbs(secchan, r))
482 ofpbuf_delete(this->rxbuf);
490 if (this->rxbuf && !this->n_txq) {
491 int retval = rconn_send(peer->rconn, this->rxbuf,
493 if (retval != EAGAIN) {
497 ofpbuf_delete(this->rxbuf);
508 if (r->is_mgmt_conn) {
509 for (i = 0; i < 2; i++) {
510 struct half *this = &r->halves[i];
511 if (!rconn_is_alive(this->rconn)) {
520 relay_wait(struct relay *r)
524 if (r->async_rconn) {
525 rconn_run_wait(r->async_rconn);
527 for (i = 0; i < 2; i++) {
528 struct half *this = &r->halves[i];
530 rconn_run_wait(this->rconn);
532 rconn_recv_wait(this->rconn);
533 if (i == HALF_LOCAL && r->async_rconn) {
534 rconn_recv_wait(r->async_rconn);
541 relay_destroy(struct relay *r)
545 list_remove(&r->node);
546 rconn_destroy(r->async_rconn);
547 for (i = 0; i < 2; i++) {
548 struct half *this = &r->halves[i];
549 rconn_destroy(this->rconn);
550 ofpbuf_delete(this->rxbuf);
555 /* User interface. */
558 parse_options(int argc, char *argv[], struct settings *s)
561 OPT_ACCEPT_VCONN = UCHAR_MAX + 1,
563 OPT_INACTIVITY_PROBE,
568 OPT_BOOTSTRAP_CA_CERT,
577 static struct option long_options[] = {
578 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
579 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
580 {"fail", required_argument, 0, 'F'},
581 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
582 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
583 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
584 {"listen", required_argument, 0, 'l'},
585 {"monitor", required_argument, 0, 'm'},
586 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
587 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
588 {"stp", no_argument, 0, OPT_STP},
589 {"no-stp", no_argument, 0, OPT_NO_STP},
590 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
591 {"in-band", no_argument, 0, OPT_IN_BAND},
592 {"command-acl", required_argument, 0, OPT_COMMAND_ACL},
593 {"command-dir", required_argument, 0, OPT_COMMAND_DIR},
594 {"verbose", optional_argument, 0, 'v'},
595 {"help", no_argument, 0, 'h'},
596 {"version", no_argument, 0, 'V'},
600 VCONN_SSL_LONG_OPTIONS
601 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
605 char *short_options = long_options_to_short_options(long_options);
606 char *accept_re = NULL;
609 /* Set defaults that we can figure out before parsing options. */
611 s->monitor_name = NULL;
612 s->fail_mode = FAIL_OPEN;
614 s->probe_interval = 15;
616 s->update_resolv_conf = true;
619 s->enable_stp = false;
622 s->command_dir = xasprintf("%s/commands", ofp_pkgdatadir);
626 c = getopt_long(argc, argv, short_options, long_options, NULL);
632 case OPT_ACCEPT_VCONN:
633 accept_re = optarg[0] == '^' ? optarg : xasprintf("^%s", optarg);
636 case OPT_NO_RESOLV_CONF:
637 s->update_resolv_conf = false;
641 if (!strcmp(optarg, "open")) {
642 s->fail_mode = FAIL_OPEN;
643 } else if (!strcmp(optarg, "closed")) {
644 s->fail_mode = FAIL_CLOSED;
646 ofp_fatal(0, "-f or --fail argument must be \"open\" "
651 case OPT_INACTIVITY_PROBE:
652 s->probe_interval = atoi(optarg);
653 if (s->probe_interval < 5) {
654 ofp_fatal(0, "--inactivity-probe argument must be at least 5");
659 if (!strcmp(optarg, "permanent")) {
660 s->max_idle = OFP_FLOW_PERMANENT;
662 s->max_idle = atoi(optarg);
663 if (s->max_idle < 1 || s->max_idle > 65535) {
664 ofp_fatal(0, "--max-idle argument must be between 1 and "
665 "65535 or the word 'permanent'");
670 case OPT_MAX_BACKOFF:
671 s->max_backoff = atoi(optarg);
672 if (s->max_backoff < 1) {
673 ofp_fatal(0, "--max-backoff argument must be at least 1");
674 } else if (s->max_backoff > 3600) {
675 s->max_backoff = 3600;
681 s->rate_limit = atoi(optarg);
682 if (s->rate_limit < 1) {
683 ofp_fatal(0, "--rate-limit argument must be at least 1");
686 s->rate_limit = 1000;
690 case OPT_BURST_LIMIT:
691 s->burst_limit = atoi(optarg);
692 if (s->burst_limit < 1) {
693 ofp_fatal(0, "--burst-limit argument must be at least 1");
698 s->enable_stp = true;
702 s->enable_stp = false;
705 case OPT_OUT_OF_BAND:
713 case OPT_COMMAND_ACL:
714 s->command_acl = (s->command_acl[0]
715 ? xasprintf("%s,%s", s->command_acl, optarg)
719 case OPT_COMMAND_DIR:
720 s->command_dir = optarg;
724 if (s->n_listeners >= MAX_MGMT) {
726 "-l or --listen may be specified at most %d times",
729 s->listener_names[s->n_listeners++] = optarg;
733 if (s->monitor_name) {
734 ofp_fatal(0, "-m or --monitor may only be specified once");
736 s->monitor_name = optarg;
743 printf("%s %s compiled "__DATE__" "__TIME__"\n",
744 program_name, VERSION BUILDNR);
747 DAEMON_OPTION_HANDLERS
752 VCONN_SSL_OPTION_HANDLERS
754 case OPT_BOOTSTRAP_CA_CERT:
755 vconn_ssl_set_ca_cert_file(optarg, true);
770 if (argc < 1 || argc > 2) {
771 ofp_fatal(0, "need one or two non-option arguments; "
772 "use --help for usage");
775 /* Local and remote vconns. */
776 s->dp_name = argv[0];
777 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
779 /* Set accept_controller_regex. */
781 accept_re = vconn_ssl_is_configured() ? "^ssl:.*" : ".*";
783 retval = regcomp(&s->accept_controller_regex, accept_re,
784 REG_NOSUB | REG_EXTENDED);
786 size_t length = regerror(retval, &s->accept_controller_regex, NULL, 0);
787 char *buffer = xmalloc(length);
788 regerror(retval, &s->accept_controller_regex, buffer, length);
789 ofp_fatal(0, "%s: %s", accept_re, buffer);
791 s->accept_controller_re = accept_re;
793 /* Mode of operation. */
794 s->discovery = s->controller_name == NULL;
795 if (s->discovery && !s->in_band) {
796 ofp_fatal(0, "Cannot perform discovery with out-of-band control");
801 if (s->rate_limit < 100) {
802 VLOG_WARN("Rate limit set to unusually low value %d",
805 if (!s->burst_limit) {
806 s->burst_limit = s->rate_limit / 4;
808 s->burst_limit = MAX(s->burst_limit, 1);
809 s->burst_limit = MIN(s->burst_limit, INT_MAX / 1000);
816 printf("%s: secure channel, a relay for OpenFlow messages.\n"
817 "usage: %s [OPTIONS] nl:DP_IDX [CONTROLLER]\n"
818 "where nl:DP_IDX is a datapath that has been added with dpctl.\n"
819 "CONTROLLER is an active OpenFlow connection method; if it is\n"
820 "omitted, then secchan performs controller discovery.\n",
821 program_name, program_name);
822 vconn_usage(true, true, true);
823 printf("\nController discovery options:\n"
824 " --accept-vconn=REGEX accept matching discovered controllers\n"
825 " --no-resolv-conf do not update /etc/resolv.conf\n"
826 "\nNetworking options:\n"
827 " -F, --fail=open|closed when controller connection fails:\n"
828 " closed: drop all packets\n"
829 " open (default): act as learning switch\n"
830 " --inactivity-probe=SECS time between inactivity probes\n"
831 " --max-idle=SECS max idle for flows set up by secchan\n"
832 " --max-backoff=SECS max time between controller connection\n"
833 " attempts (default: 15 seconds)\n"
834 " -l, --listen=METHOD allow management connections on METHOD\n"
835 " (a passive OpenFlow connection method)\n"
836 " -m, --monitor=METHOD copy traffic to/from kernel to METHOD\n"
837 " (a passive OpenFlow connection method)\n"
838 " --out-of-band controller connection is out-of-band\n"
839 " --stp enable 802.1D Spanning Tree Protocol\n"
840 " --no-stp disable 802.1D Spanning Tree Protocol\n"
841 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
842 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
843 " --burst-limit=BURST limit on packet credit for idle time\n"
844 "\nRemote command execution options:\n"
845 " --command-acl=[!]GLOB[,[!]GLOB...] set allowed/denied commands\n"
846 " --command-dir=DIR set command dir (default: %s/commands)\n",
850 printf("\nOther options:\n"
851 " -h, --help display this help message\n"
852 " -V, --version display version information\n");