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"
65 #include "stp-secchan.h"
69 #include "vconn-ssl.h"
71 #include "vlog-socket.h"
74 #define THIS_MODULE VLM_secchan
77 const struct hook_class *class;
83 size_t n_hooks, allocated_hooks;
86 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
88 static void parse_options(int argc, char *argv[], struct settings *);
89 static void usage(void) NO_RETURN;
91 static char *vconn_name_without_subscription(const char *);
92 static struct pvconn *open_passive_vconn(const char *name);
93 static struct vconn *accept_vconn(struct pvconn *pvconn);
95 static struct relay *relay_create(struct rconn *async,
96 struct rconn *local, struct rconn *remote,
98 static struct relay *relay_accept(const struct settings *, struct pvconn *);
99 static void relay_run(struct relay *, struct secchan *);
100 static void relay_wait(struct relay *);
101 static void relay_destroy(struct relay *);
104 main(int argc, char *argv[])
108 struct list relays = LIST_INITIALIZER(&relays);
110 struct secchan secchan;
112 struct pvconn *monitor;
114 struct pvconn *listeners[MAX_MGMT];
117 char *local_rconn_name;
118 struct rconn *async_rconn, *local_rconn, *remote_rconn;
119 struct relay *controller_relay;
120 struct discovery *discovery;
121 struct switch_status *switch_status;
122 struct port_watcher *pw;
126 set_program_name(argv[0]);
127 register_fault_handlers();
130 parse_options(argc, argv, &s);
131 signal(SIGPIPE, SIG_IGN);
133 secchan.hooks = NULL;
135 secchan.allocated_hooks = 0;
137 /* Start listening for management and monitoring connections. */
139 for (i = 0; i < s.n_listeners; i++) {
140 listeners[n_listeners++] = open_passive_vconn(s.listener_names[i]);
142 monitor = s.monitor_name ? open_passive_vconn(s.monitor_name) : NULL;
144 /* Initialize switch status hook. */
145 switch_status_start(&secchan, &s, &switch_status);
147 die_if_already_running();
150 /* Start listening for vlogconf requests. */
151 retval = vlog_server_listen(NULL, NULL);
153 ofp_fatal(retval, "Could not listen for vlog connections");
156 VLOG_WARN("OpenFlow reference implementation version %s", VERSION BUILDNR);
157 VLOG_WARN("OpenFlow protocol version 0x%02x", OFP_VERSION);
159 /* Check datapath name, to try to catch command-line invocation errors. */
160 if (strncmp(s.dp_name, "nl:", 3) && strncmp(s.dp_name, "unix:", 5)
161 && !s.controller_name) {
162 VLOG_WARN("Controller not specified and datapath is not nl: or "
163 "unix:. (Did you forget to specify the datapath?)");
166 if (!strncmp(s.dp_name, "nl:", 3)) {
167 /* Connect to datapath with a subscription for asynchronous events. By
168 * separating the connection for asynchronous events from that for
169 * request and replies we prevent the socket receive buffer from being
170 * filled up by received packet data, which in turn would prevent
171 * getting replies to any Netlink messages we send to the kernel. */
172 async_rconn = rconn_create(0, s.max_backoff);
173 rconn_connect(async_rconn, s.dp_name);
174 switch_status_register_category(switch_status, "async",
175 rconn_status_cb, async_rconn);
177 /* No need for a separate asynchronous connection: we must be connected
178 * to the user datapath, which is smart enough to discard packet events
179 * instead of message replies. In fact, having a second connection
180 * would work against us since we'd get double copies of asynchronous
181 * event messages (the user datapath provides no way to turn off
182 * asynchronous events). */
186 /* Connect to datapath without a subscription, for requests and replies. */
187 local_rconn_name = vconn_name_without_subscription(s.dp_name);
188 local_rconn = rconn_create(0, s.max_backoff);
189 rconn_connect(local_rconn, local_rconn_name);
190 free(local_rconn_name);
191 switch_status_register_category(switch_status, "local",
192 rconn_status_cb, local_rconn);
194 /* Connect to controller. */
195 remote_rconn = rconn_create(s.probe_interval, s.max_backoff);
196 if (s.controller_name) {
197 retval = rconn_connect(remote_rconn, s.controller_name);
198 if (retval == EAFNOSUPPORT) {
199 ofp_fatal(0, "No support for %s vconn", s.controller_name);
202 switch_status_register_category(switch_status, "remote",
203 rconn_status_cb, remote_rconn);
205 /* Start relaying. */
206 controller_relay = relay_create(async_rconn, local_rconn, remote_rconn,
208 list_push_back(&relays, &controller_relay->node);
211 port_watcher_start(&secchan, local_rconn, remote_rconn, &pw);
212 discovery = s.discovery ? discovery_init(&s, pw, switch_status) : NULL;
214 snat_start(&secchan, pw);
216 flow_end_start(&secchan, s.netflow_dst, local_rconn, remote_rconn);
218 stp_start(&secchan, &s, pw, local_rconn, remote_rconn);
221 in_band_start(&secchan, &s, switch_status, pw, remote_rconn);
223 if (s.fail_mode == FAIL_OPEN) {
224 fail_open_start(&secchan, &s, switch_status,
225 local_rconn, remote_rconn);
228 rate_limit_start(&secchan, &s, switch_status, remote_rconn);
230 if (s.command_acl[0]) {
231 executer_start(&secchan, &s);
239 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
240 relay_run(r, &secchan);
242 for (i = 0; i < n_listeners; i++) {
244 struct relay *r = relay_accept(&s, listeners[i]);
248 list_push_back(&relays, &r->node);
252 struct vconn *new = accept_vconn(monitor);
254 /* XXX should monitor async_rconn too but rconn_add_monitor()
255 * takes ownership of the vconn passed in. */
256 rconn_add_monitor(local_rconn, new);
259 for (i = 0; i < secchan.n_hooks; i++) {
260 if (secchan.hooks[i].class->periodic_cb) {
261 secchan.hooks[i].class->periodic_cb(secchan.hooks[i].aux);
265 char *controller_name;
266 if (rconn_is_connectivity_questionable(remote_rconn)) {
267 discovery_question_connectivity(discovery);
269 if (discovery_run(discovery, &controller_name)) {
270 if (controller_name) {
271 rconn_connect(remote_rconn, controller_name);
273 rconn_disconnect(remote_rconn);
278 /* Wait for something to happen. */
279 LIST_FOR_EACH (r, struct relay, node, &relays) {
282 for (i = 0; i < n_listeners; i++) {
283 pvconn_wait(listeners[i]);
286 pvconn_wait(monitor);
288 for (i = 0; i < secchan.n_hooks; i++) {
289 if (secchan.hooks[i].class->wait_cb) {
290 secchan.hooks[i].class->wait_cb(secchan.hooks[i].aux);
294 discovery_wait(discovery);
302 static struct pvconn *
303 open_passive_vconn(const char *name)
305 struct pvconn *pvconn;
308 retval = pvconn_open(name, &pvconn);
309 if (retval && retval != EAGAIN) {
310 ofp_fatal(retval, "opening %s", name);
315 static struct vconn *
316 accept_vconn(struct pvconn *pvconn)
321 retval = pvconn_accept(pvconn, OFP_VERSION, &new);
322 if (retval && retval != EAGAIN) {
323 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
329 add_hook(struct secchan *secchan, const struct hook_class *class, void *aux)
333 if (secchan->n_hooks >= secchan->allocated_hooks) {
334 secchan->allocated_hooks = secchan->allocated_hooks * 2 + 1;
335 secchan->hooks = xrealloc(secchan->hooks,
336 (sizeof *secchan->hooks
337 * secchan->allocated_hooks));
339 hook = &secchan->hooks[secchan->n_hooks++];
344 struct ofp_packet_in *
345 get_ofp_packet_in(struct relay *r)
347 struct ofpbuf *msg = r->halves[HALF_LOCAL].rxbuf;
348 struct ofp_header *oh = msg->data;
349 if (oh->type == OFPT_PACKET_IN) {
350 if (msg->size >= offsetof (struct ofp_packet_in, data)) {
353 VLOG_WARN("packet too short (%zu bytes) for packet_in",
361 get_ofp_packet_eth_header(struct relay *r, struct ofp_packet_in **opip,
362 struct eth_header **ethp)
364 const int min_len = offsetof(struct ofp_packet_in, data) + ETH_HEADER_LEN;
365 struct ofp_packet_in *opi = get_ofp_packet_in(r);
366 if (opi && ntohs(opi->header.length) >= min_len) {
368 *ethp = (void *) opi->data;
374 /* OpenFlow message relaying. */
376 /* Returns a malloc'd string containing a copy of 'vconn_name' modified not to
377 * subscribe to asynchronous messages such as 'ofp_packet_in' events (if
380 vconn_name_without_subscription(const char *vconn_name)
383 if (sscanf(vconn_name, "nl:%d", &nl_index) == 1) {
384 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123.
385 * nl:123:0 opens a netlink connection to local datapath 123 without
386 * obtaining a subscription for ofp_packet_in or ofp_flow_expired
388 return xasprintf("nl:%d:0", nl_index);
390 /* We don't have a way to specify not to subscribe to those messages
391 * for other transports. (That's a defect: really this should be in
392 * the OpenFlow protocol, not the Netlink transport). */
393 VLOG_WARN_RL(&rl, "new management connection will receive "
394 "asynchronous messages");
395 return xstrdup(vconn_name);
399 static struct relay *
400 relay_accept(const struct settings *s, struct pvconn *pvconn)
402 struct vconn *new_remote, *new_local;
403 struct rconn *r1, *r2;
407 new_remote = accept_vconn(pvconn);
412 vconn_name = vconn_name_without_subscription(s->dp_name);
413 retval = vconn_open(vconn_name, OFP_VERSION, &new_local);
415 VLOG_ERR_RL(&rl, "could not connect to %s (%s)",
416 vconn_name, strerror(retval));
417 vconn_close(new_remote);
422 /* Create and return relay. */
423 r1 = rconn_create(0, 0);
424 rconn_connect_unreliably(r1, vconn_name, new_local);
427 r2 = rconn_create(0, 0);
428 rconn_connect_unreliably(r2, "passive", new_remote);
430 return relay_create(NULL, r1, r2, true);
433 static struct relay *
434 relay_create(struct rconn *async, struct rconn *local, struct rconn *remote,
437 struct relay *r = xcalloc(1, sizeof *r);
438 r->halves[HALF_LOCAL].rconn = local;
439 r->halves[HALF_REMOTE].rconn = remote;
440 r->is_mgmt_conn = is_mgmt_conn;
441 r->async_rconn = async;
446 call_local_packet_cbs(struct secchan *secchan, struct relay *r)
448 const struct hook *h;
449 for (h = secchan->hooks; h < &secchan->hooks[secchan->n_hooks]; h++) {
450 bool (*cb)(struct relay *, void *aux) = h->class->local_packet_cb;
451 if (cb && (cb)(r, h->aux)) {
459 call_remote_packet_cbs(struct secchan *secchan, struct relay *r)
461 const struct hook *h;
462 for (h = secchan->hooks; h < &secchan->hooks[secchan->n_hooks]; h++) {
463 bool (*cb)(struct relay *, void *aux) = h->class->remote_packet_cb;
464 if (cb && (cb)(r, h->aux)) {
472 relay_run(struct relay *r, struct secchan *secchan)
477 if (r->async_rconn) {
478 rconn_run(r->async_rconn);
480 for (i = 0; i < 2; i++) {
481 rconn_run(r->halves[i].rconn);
484 /* Limit the number of iterations to prevent other tasks from starving. */
485 for (iteration = 0; iteration < 50; iteration++) {
486 bool progress = false;
487 for (i = 0; i < 2; i++) {
488 struct half *this = &r->halves[i];
489 struct half *peer = &r->halves[!i];
492 this->rxbuf = rconn_recv(this->rconn);
493 if (!this->rxbuf && i == HALF_LOCAL && r->async_rconn) {
494 this->rxbuf = rconn_recv(r->async_rconn);
496 if (this->rxbuf && (i == HALF_REMOTE || !r->is_mgmt_conn)) {
498 ? call_local_packet_cbs(secchan, r)
499 : call_remote_packet_cbs(secchan, r))
501 ofpbuf_delete(this->rxbuf);
509 if (this->rxbuf && !this->n_txq) {
510 int retval = rconn_send(peer->rconn, this->rxbuf,
512 if (retval != EAGAIN) {
516 ofpbuf_delete(this->rxbuf);
527 if (r->is_mgmt_conn) {
528 for (i = 0; i < 2; i++) {
529 struct half *this = &r->halves[i];
530 if (!rconn_is_alive(this->rconn)) {
539 relay_wait(struct relay *r)
543 if (r->async_rconn) {
544 rconn_run_wait(r->async_rconn);
546 for (i = 0; i < 2; i++) {
547 struct half *this = &r->halves[i];
549 rconn_run_wait(this->rconn);
551 rconn_recv_wait(this->rconn);
552 if (i == HALF_LOCAL && r->async_rconn) {
553 rconn_recv_wait(r->async_rconn);
560 relay_destroy(struct relay *r)
564 list_remove(&r->node);
565 rconn_destroy(r->async_rconn);
566 for (i = 0; i < 2; i++) {
567 struct half *this = &r->halves[i];
568 rconn_destroy(this->rconn);
569 ofpbuf_delete(this->rxbuf);
574 /* User interface. */
577 parse_options(int argc, char *argv[], struct settings *s)
580 OPT_ACCEPT_VCONN = UCHAR_MAX + 1,
582 OPT_INACTIVITY_PROBE,
587 OPT_BOOTSTRAP_CA_CERT,
597 static struct option long_options[] = {
598 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
599 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
600 {"fail", required_argument, 0, 'F'},
601 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
602 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
603 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
604 {"listen", required_argument, 0, 'l'},
605 {"monitor", required_argument, 0, 'm'},
606 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
607 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
608 {"stp", no_argument, 0, OPT_STP},
609 {"no-stp", no_argument, 0, OPT_NO_STP},
610 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
611 {"in-band", no_argument, 0, OPT_IN_BAND},
612 {"command-acl", required_argument, 0, OPT_COMMAND_ACL},
613 {"command-dir", required_argument, 0, OPT_COMMAND_DIR},
614 {"netflow", required_argument, 0, OPT_NETFLOW},
615 {"verbose", optional_argument, 0, 'v'},
616 {"help", no_argument, 0, 'h'},
617 {"version", no_argument, 0, 'V'},
621 VCONN_SSL_LONG_OPTIONS
622 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
626 char *short_options = long_options_to_short_options(long_options);
627 char *accept_re = NULL;
630 /* Set defaults that we can figure out before parsing options. */
632 s->monitor_name = NULL;
633 s->fail_mode = FAIL_OPEN;
635 s->probe_interval = 15;
637 s->update_resolv_conf = true;
640 s->enable_stp = false;
643 s->command_dir = xasprintf("%s/commands", ofp_pkgdatadir);
644 s->netflow_dst = NULL;
648 c = getopt_long(argc, argv, short_options, long_options, NULL);
654 case OPT_ACCEPT_VCONN:
655 accept_re = optarg[0] == '^' ? optarg : xasprintf("^%s", optarg);
658 case OPT_NO_RESOLV_CONF:
659 s->update_resolv_conf = false;
663 if (!strcmp(optarg, "open")) {
664 s->fail_mode = FAIL_OPEN;
665 } else if (!strcmp(optarg, "closed")) {
666 s->fail_mode = FAIL_CLOSED;
668 ofp_fatal(0, "-f or --fail argument must be \"open\" "
673 case OPT_INACTIVITY_PROBE:
674 s->probe_interval = atoi(optarg);
675 if (s->probe_interval < 5) {
676 ofp_fatal(0, "--inactivity-probe argument must be at least 5");
681 if (!strcmp(optarg, "permanent")) {
682 s->max_idle = OFP_FLOW_PERMANENT;
684 s->max_idle = atoi(optarg);
685 if (s->max_idle < 1 || s->max_idle > 65535) {
686 ofp_fatal(0, "--max-idle argument must be between 1 and "
687 "65535 or the word 'permanent'");
692 case OPT_MAX_BACKOFF:
693 s->max_backoff = atoi(optarg);
694 if (s->max_backoff < 1) {
695 ofp_fatal(0, "--max-backoff argument must be at least 1");
696 } else if (s->max_backoff > 3600) {
697 s->max_backoff = 3600;
703 s->rate_limit = atoi(optarg);
704 if (s->rate_limit < 1) {
705 ofp_fatal(0, "--rate-limit argument must be at least 1");
708 s->rate_limit = 1000;
712 case OPT_BURST_LIMIT:
713 s->burst_limit = atoi(optarg);
714 if (s->burst_limit < 1) {
715 ofp_fatal(0, "--burst-limit argument must be at least 1");
720 s->enable_stp = true;
724 s->enable_stp = false;
727 case OPT_OUT_OF_BAND:
735 case OPT_COMMAND_ACL:
736 s->command_acl = (s->command_acl[0]
737 ? xasprintf("%s,%s", s->command_acl, optarg)
741 case OPT_COMMAND_DIR:
742 s->command_dir = optarg;
746 if (s->netflow_dst) {
747 ofp_fatal(0, "--netflow may only be specified once");
749 s->netflow_dst = optarg;
753 if (s->n_listeners >= MAX_MGMT) {
755 "-l or --listen may be specified at most %d times",
758 s->listener_names[s->n_listeners++] = optarg;
762 if (s->monitor_name) {
763 ofp_fatal(0, "-m or --monitor may only be specified once");
765 s->monitor_name = optarg;
772 printf("%s %s compiled "__DATE__" "__TIME__"\n",
773 program_name, VERSION BUILDNR);
776 DAEMON_OPTION_HANDLERS
781 VCONN_SSL_OPTION_HANDLERS
783 case OPT_BOOTSTRAP_CA_CERT:
784 vconn_ssl_set_ca_cert_file(optarg, true);
799 if (argc < 1 || argc > 2) {
800 ofp_fatal(0, "need one or two non-option arguments; "
801 "use --help for usage");
804 /* Local and remote vconns. */
805 s->dp_name = argv[0];
806 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
808 /* Set accept_controller_regex. */
810 accept_re = vconn_ssl_is_configured() ? "^ssl:.*" : ".*";
812 retval = regcomp(&s->accept_controller_regex, accept_re,
813 REG_NOSUB | REG_EXTENDED);
815 size_t length = regerror(retval, &s->accept_controller_regex, NULL, 0);
816 char *buffer = xmalloc(length);
817 regerror(retval, &s->accept_controller_regex, buffer, length);
818 ofp_fatal(0, "%s: %s", accept_re, buffer);
820 s->accept_controller_re = accept_re;
822 /* Mode of operation. */
823 s->discovery = s->controller_name == NULL;
824 if (s->discovery && !s->in_band) {
825 ofp_fatal(0, "Cannot perform discovery with out-of-band control");
830 if (s->rate_limit < 100) {
831 VLOG_WARN("Rate limit set to unusually low value %d",
834 if (!s->burst_limit) {
835 s->burst_limit = s->rate_limit / 4;
837 s->burst_limit = MAX(s->burst_limit, 1);
838 s->burst_limit = MIN(s->burst_limit, INT_MAX / 1000);
845 printf("%s: secure channel, a relay for OpenFlow messages.\n"
846 "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
847 "DATAPATH is an active connection method to a local datapath.\n"
848 "CONTROLLER is an active OpenFlow connection method; if it is\n"
849 "omitted, then secchan performs controller discovery.\n",
850 program_name, program_name);
851 vconn_usage(true, true, true);
852 printf("\nController discovery options:\n"
853 " --accept-vconn=REGEX accept matching discovered controllers\n"
854 " --no-resolv-conf do not update /etc/resolv.conf\n"
855 "\nNetworking options:\n"
856 " -F, --fail=open|closed when controller connection fails:\n"
857 " closed: drop all packets\n"
858 " open (default): act as learning switch\n"
859 " --inactivity-probe=SECS time between inactivity probes\n"
860 " --max-idle=SECS max idle for flows set up by secchan\n"
861 " --max-backoff=SECS max time between controller connection\n"
862 " attempts (default: 15 seconds)\n"
863 " -l, --listen=METHOD allow management connections on METHOD\n"
864 " (a passive OpenFlow connection method)\n"
865 " -m, --monitor=METHOD copy traffic to/from kernel to METHOD\n"
866 " (a passive OpenFlow connection method)\n"
867 " --out-of-band controller connection is out-of-band\n"
868 " --stp enable 802.1D Spanning Tree Protocol\n"
869 " --no-stp disable 802.1D Spanning Tree Protocol\n"
870 " --netflow=HOST:PORT send NetFlow v5 messages when flows end\n"
871 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
872 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
873 " --burst-limit=BURST limit on packet credit for idle time\n"
874 "\nRemote command execution options:\n"
875 " --command-acl=[!]GLOB[,[!]GLOB...] set allowed/denied commands\n"
876 " --command-dir=DIR set command dir (default: %s/commands)\n",
880 printf("\nOther options:\n"
881 " -h, --help display this help message\n"
882 " -V, --version display version information\n");