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);
97 static struct relay *relay_accept(const struct settings *, struct pvconn *);
98 static void relay_run(struct relay *, struct secchan *);
99 static void relay_wait(struct relay *);
100 static void relay_destroy(struct relay *);
103 main(int argc, char *argv[])
107 struct list relays = LIST_INITIALIZER(&relays);
109 struct secchan secchan;
111 struct pvconn *monitor;
113 struct pvconn *listeners[MAX_MGMT];
116 char *local_rconn_name;
117 struct rconn *async_rconn, *local_rconn, *remote_rconn;
118 struct relay *controller_relay;
119 struct discovery *discovery;
120 struct switch_status *switch_status;
121 struct port_watcher *pw;
125 set_program_name(argv[0]);
126 register_fault_handlers();
129 parse_options(argc, argv, &s);
130 signal(SIGPIPE, SIG_IGN);
132 secchan.hooks = NULL;
134 secchan.allocated_hooks = 0;
136 /* Start listening for management and monitoring connections. */
138 for (i = 0; i < s.n_listeners; i++) {
139 listeners[n_listeners++] = open_passive_vconn(s.listener_names[i]);
141 monitor = s.monitor_name ? open_passive_vconn(s.monitor_name) : NULL;
143 /* Initialize switch status hook. */
144 switch_status_start(&secchan, &s, &switch_status);
146 die_if_already_running();
149 /* Start listening for vlogconf requests. */
150 retval = vlog_server_listen(NULL, NULL);
152 ofp_fatal(retval, "Could not listen for vlog connections");
155 VLOG_WARN("OpenFlow reference implementation version %s", VERSION BUILDNR);
156 VLOG_WARN("OpenFlow protocol version 0x%02x", OFP_VERSION);
158 /* Check datapath name, to try to catch command-line invocation errors. */
159 if (strncmp(s.dp_name, "nl:", 3) && strncmp(s.dp_name, "unix:", 5)
160 && !s.controller_name) {
161 VLOG_WARN("Controller not specified and datapath is not nl: or "
162 "unix:. (Did you forget to specify the datapath?)");
165 /* Connect to datapath with a subscription for asynchronous events. */
166 async_rconn = rconn_create(0, s.max_backoff);
167 rconn_connect(async_rconn, s.dp_name);
168 switch_status_register_category(switch_status, "async",
169 rconn_status_cb, async_rconn);
171 /* Connect to datapath without a subscription, for requests and replies. */
172 local_rconn_name = vconn_name_without_subscription(s.dp_name);
173 local_rconn = rconn_create(0, s.max_backoff);
174 rconn_connect(local_rconn, local_rconn_name);
175 free(local_rconn_name);
176 switch_status_register_category(switch_status, "local",
177 rconn_status_cb, local_rconn);
179 /* Connect to controller. */
180 remote_rconn = rconn_create(s.probe_interval, s.max_backoff);
181 if (s.controller_name) {
182 retval = rconn_connect(remote_rconn, s.controller_name);
183 if (retval == EAFNOSUPPORT) {
184 ofp_fatal(0, "No support for %s vconn", s.controller_name);
187 switch_status_register_category(switch_status, "remote",
188 rconn_status_cb, remote_rconn);
190 /* Start relaying. */
191 controller_relay = relay_create(async_rconn, local_rconn, remote_rconn);
192 list_push_back(&relays, &controller_relay->node);
195 port_watcher_start(&secchan, local_rconn, remote_rconn, &pw);
196 discovery = s.discovery ? discovery_init(&s, pw, switch_status) : NULL;
198 snat_start(&secchan, pw);
200 flow_end_start(&secchan, s.netflow_dst, local_rconn, remote_rconn);
202 stp_start(&secchan, &s, pw, local_rconn, remote_rconn);
205 in_band_start(&secchan, &s, switch_status, pw, remote_rconn);
207 if (s.fail_mode == FAIL_OPEN) {
208 fail_open_start(&secchan, &s, switch_status,
209 local_rconn, remote_rconn);
212 rate_limit_start(&secchan, &s, switch_status, remote_rconn);
214 if (s.command_acl[0]) {
215 executer_start(&secchan, &s);
223 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
224 relay_run(r, &secchan);
226 for (i = 0; i < n_listeners; i++) {
228 struct relay *r = relay_accept(&s, listeners[i]);
232 list_push_back(&relays, &r->node);
236 struct vconn *new = accept_vconn(monitor);
238 /* XXX should monitor async_rconn too but rconn_add_monitor()
239 * takes ownership of the vconn passed in. */
240 rconn_add_monitor(local_rconn, new);
243 for (i = 0; i < secchan.n_hooks; i++) {
244 if (secchan.hooks[i].class->periodic_cb) {
245 secchan.hooks[i].class->periodic_cb(secchan.hooks[i].aux);
249 char *controller_name;
250 if (rconn_is_connectivity_questionable(remote_rconn)) {
251 discovery_question_connectivity(discovery);
253 if (discovery_run(discovery, &controller_name)) {
254 if (controller_name) {
255 rconn_connect(remote_rconn, controller_name);
257 rconn_disconnect(remote_rconn);
262 /* Wait for something to happen. */
263 LIST_FOR_EACH (r, struct relay, node, &relays) {
266 for (i = 0; i < n_listeners; i++) {
267 pvconn_wait(listeners[i]);
270 pvconn_wait(monitor);
272 for (i = 0; i < secchan.n_hooks; i++) {
273 if (secchan.hooks[i].class->wait_cb) {
274 secchan.hooks[i].class->wait_cb(secchan.hooks[i].aux);
278 discovery_wait(discovery);
286 static struct pvconn *
287 open_passive_vconn(const char *name)
289 struct pvconn *pvconn;
292 retval = pvconn_open(name, &pvconn);
293 if (retval && retval != EAGAIN) {
294 ofp_fatal(retval, "opening %s", name);
299 static struct vconn *
300 accept_vconn(struct pvconn *pvconn)
305 retval = pvconn_accept(pvconn, OFP_VERSION, &new);
306 if (retval && retval != EAGAIN) {
307 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
313 add_hook(struct secchan *secchan, const struct hook_class *class, void *aux)
317 if (secchan->n_hooks >= secchan->allocated_hooks) {
318 secchan->allocated_hooks = secchan->allocated_hooks * 2 + 1;
319 secchan->hooks = xrealloc(secchan->hooks,
320 (sizeof *secchan->hooks
321 * secchan->allocated_hooks));
323 hook = &secchan->hooks[secchan->n_hooks++];
328 struct ofp_packet_in *
329 get_ofp_packet_in(struct relay *r)
331 struct ofpbuf *msg = r->halves[HALF_LOCAL].rxbuf;
332 struct ofp_header *oh = msg->data;
333 if (oh->type == OFPT_PACKET_IN) {
334 if (msg->size >= offsetof (struct ofp_packet_in, data)) {
337 VLOG_WARN("packet too short (%zu bytes) for packet_in",
345 get_ofp_packet_eth_header(struct relay *r, struct ofp_packet_in **opip,
346 struct eth_header **ethp)
348 const int min_len = offsetof(struct ofp_packet_in, data) + ETH_HEADER_LEN;
349 struct ofp_packet_in *opi = get_ofp_packet_in(r);
350 if (opi && ntohs(opi->header.length) >= min_len) {
352 *ethp = (void *) opi->data;
358 /* OpenFlow message relaying. */
360 /* Returns a malloc'd string containing a copy of 'vconn_name' modified not to
361 * subscribe to asynchronous messages such as 'ofp_packet_in' events (if
364 vconn_name_without_subscription(const char *vconn_name)
367 if (sscanf(vconn_name, "nl:%d", &nl_index) == 1) {
368 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123.
369 * nl:123:0 opens a netlink connection to local datapath 123 without
370 * obtaining a subscription for ofp_packet_in or ofp_flow_expired
372 return xasprintf("nl:%d:0", nl_index);
374 /* We don't have a way to specify not to subscribe to those messages
375 * for other transports. (That's a defect: really this should be in
376 * the OpenFlow protocol, not the Netlink transport). */
377 VLOG_WARN_RL(&rl, "new management connection will receive "
378 "asynchronous messages");
379 return xstrdup(vconn_name);
383 static struct relay *
384 relay_accept(const struct settings *s, struct pvconn *pvconn)
386 struct vconn *new_remote, *new_local;
387 struct rconn *r1, *r2;
391 new_remote = accept_vconn(pvconn);
396 vconn_name = vconn_name_without_subscription(s->dp_name);
397 retval = vconn_open(vconn_name, OFP_VERSION, &new_local);
399 VLOG_ERR_RL(&rl, "could not connect to %s (%s)",
400 vconn_name, strerror(retval));
401 vconn_close(new_remote);
406 /* Create and return relay. */
407 r1 = rconn_create(0, 0);
408 rconn_connect_unreliably(r1, vconn_name, new_local);
411 r2 = rconn_create(0, 0);
412 rconn_connect_unreliably(r2, "passive", new_remote);
414 return relay_create(NULL, r1, r2);
417 static struct relay *
418 relay_create(struct rconn *async, struct rconn *local, struct rconn *remote)
420 struct relay *r = xcalloc(1, sizeof *r);
421 r->halves[HALF_LOCAL].rconn = local;
422 r->halves[HALF_REMOTE].rconn = remote;
423 r->is_mgmt_conn = async == NULL;
424 r->async_rconn = async;
429 call_local_packet_cbs(struct secchan *secchan, struct relay *r)
431 const struct hook *h;
432 for (h = secchan->hooks; h < &secchan->hooks[secchan->n_hooks]; h++) {
433 bool (*cb)(struct relay *, void *aux) = h->class->local_packet_cb;
434 if (cb && (cb)(r, h->aux)) {
442 call_remote_packet_cbs(struct secchan *secchan, struct relay *r)
444 const struct hook *h;
445 for (h = secchan->hooks; h < &secchan->hooks[secchan->n_hooks]; h++) {
446 bool (*cb)(struct relay *, void *aux) = h->class->remote_packet_cb;
447 if (cb && (cb)(r, h->aux)) {
455 relay_run(struct relay *r, struct secchan *secchan)
460 if (r->async_rconn) {
461 rconn_run(r->async_rconn);
463 for (i = 0; i < 2; i++) {
464 rconn_run(r->halves[i].rconn);
467 /* Limit the number of iterations to prevent other tasks from starving. */
468 for (iteration = 0; iteration < 50; iteration++) {
469 bool progress = false;
470 for (i = 0; i < 2; i++) {
471 struct half *this = &r->halves[i];
472 struct half *peer = &r->halves[!i];
475 this->rxbuf = rconn_recv(this->rconn);
476 if (!this->rxbuf && i == HALF_LOCAL && r->async_rconn) {
477 this->rxbuf = rconn_recv(r->async_rconn);
479 if (this->rxbuf && (i == HALF_REMOTE || !r->is_mgmt_conn)) {
481 ? call_local_packet_cbs(secchan, r)
482 : call_remote_packet_cbs(secchan, r))
484 ofpbuf_delete(this->rxbuf);
492 if (this->rxbuf && !this->n_txq) {
493 int retval = rconn_send(peer->rconn, this->rxbuf,
495 if (retval != EAGAIN) {
499 ofpbuf_delete(this->rxbuf);
510 if (r->is_mgmt_conn) {
511 for (i = 0; i < 2; i++) {
512 struct half *this = &r->halves[i];
513 if (!rconn_is_alive(this->rconn)) {
522 relay_wait(struct relay *r)
526 if (r->async_rconn) {
527 rconn_run_wait(r->async_rconn);
529 for (i = 0; i < 2; i++) {
530 struct half *this = &r->halves[i];
532 rconn_run_wait(this->rconn);
534 rconn_recv_wait(this->rconn);
535 if (i == HALF_LOCAL && r->async_rconn) {
536 rconn_recv_wait(r->async_rconn);
543 relay_destroy(struct relay *r)
547 list_remove(&r->node);
548 rconn_destroy(r->async_rconn);
549 for (i = 0; i < 2; i++) {
550 struct half *this = &r->halves[i];
551 rconn_destroy(this->rconn);
552 ofpbuf_delete(this->rxbuf);
557 /* User interface. */
560 parse_options(int argc, char *argv[], struct settings *s)
563 OPT_ACCEPT_VCONN = UCHAR_MAX + 1,
565 OPT_INACTIVITY_PROBE,
570 OPT_BOOTSTRAP_CA_CERT,
580 static struct option long_options[] = {
581 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
582 {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
583 {"fail", required_argument, 0, 'F'},
584 {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
585 {"max-idle", required_argument, 0, OPT_MAX_IDLE},
586 {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
587 {"listen", required_argument, 0, 'l'},
588 {"monitor", required_argument, 0, 'm'},
589 {"rate-limit", optional_argument, 0, OPT_RATE_LIMIT},
590 {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
591 {"stp", no_argument, 0, OPT_STP},
592 {"no-stp", no_argument, 0, OPT_NO_STP},
593 {"out-of-band", no_argument, 0, OPT_OUT_OF_BAND},
594 {"in-band", no_argument, 0, OPT_IN_BAND},
595 {"command-acl", required_argument, 0, OPT_COMMAND_ACL},
596 {"command-dir", required_argument, 0, OPT_COMMAND_DIR},
597 {"netflow", required_argument, 0, OPT_NETFLOW},
598 {"verbose", optional_argument, 0, 'v'},
599 {"help", no_argument, 0, 'h'},
600 {"version", no_argument, 0, 'V'},
604 VCONN_SSL_LONG_OPTIONS
605 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
609 char *short_options = long_options_to_short_options(long_options);
610 char *accept_re = NULL;
613 /* Set defaults that we can figure out before parsing options. */
615 s->monitor_name = NULL;
616 s->fail_mode = FAIL_OPEN;
618 s->probe_interval = 15;
620 s->update_resolv_conf = true;
623 s->enable_stp = false;
626 s->command_dir = xasprintf("%s/commands", ofp_pkgdatadir);
627 s->netflow_dst = NULL;
631 c = getopt_long(argc, argv, short_options, long_options, NULL);
637 case OPT_ACCEPT_VCONN:
638 accept_re = optarg[0] == '^' ? optarg : xasprintf("^%s", optarg);
641 case OPT_NO_RESOLV_CONF:
642 s->update_resolv_conf = false;
646 if (!strcmp(optarg, "open")) {
647 s->fail_mode = FAIL_OPEN;
648 } else if (!strcmp(optarg, "closed")) {
649 s->fail_mode = FAIL_CLOSED;
651 ofp_fatal(0, "-f or --fail argument must be \"open\" "
656 case OPT_INACTIVITY_PROBE:
657 s->probe_interval = atoi(optarg);
658 if (s->probe_interval < 5) {
659 ofp_fatal(0, "--inactivity-probe argument must be at least 5");
664 if (!strcmp(optarg, "permanent")) {
665 s->max_idle = OFP_FLOW_PERMANENT;
667 s->max_idle = atoi(optarg);
668 if (s->max_idle < 1 || s->max_idle > 65535) {
669 ofp_fatal(0, "--max-idle argument must be between 1 and "
670 "65535 or the word 'permanent'");
675 case OPT_MAX_BACKOFF:
676 s->max_backoff = atoi(optarg);
677 if (s->max_backoff < 1) {
678 ofp_fatal(0, "--max-backoff argument must be at least 1");
679 } else if (s->max_backoff > 3600) {
680 s->max_backoff = 3600;
686 s->rate_limit = atoi(optarg);
687 if (s->rate_limit < 1) {
688 ofp_fatal(0, "--rate-limit argument must be at least 1");
691 s->rate_limit = 1000;
695 case OPT_BURST_LIMIT:
696 s->burst_limit = atoi(optarg);
697 if (s->burst_limit < 1) {
698 ofp_fatal(0, "--burst-limit argument must be at least 1");
703 s->enable_stp = true;
707 s->enable_stp = false;
710 case OPT_OUT_OF_BAND:
718 case OPT_COMMAND_ACL:
719 s->command_acl = (s->command_acl[0]
720 ? xasprintf("%s,%s", s->command_acl, optarg)
724 case OPT_COMMAND_DIR:
725 s->command_dir = optarg;
729 if (s->netflow_dst) {
730 ofp_fatal(0, "--netflow may only be specified once");
732 s->netflow_dst = optarg;
736 if (s->n_listeners >= MAX_MGMT) {
738 "-l or --listen may be specified at most %d times",
741 s->listener_names[s->n_listeners++] = optarg;
745 if (s->monitor_name) {
746 ofp_fatal(0, "-m or --monitor may only be specified once");
748 s->monitor_name = optarg;
755 printf("%s %s compiled "__DATE__" "__TIME__"\n",
756 program_name, VERSION BUILDNR);
759 DAEMON_OPTION_HANDLERS
764 VCONN_SSL_OPTION_HANDLERS
766 case OPT_BOOTSTRAP_CA_CERT:
767 vconn_ssl_set_ca_cert_file(optarg, true);
782 if (argc < 1 || argc > 2) {
783 ofp_fatal(0, "need one or two non-option arguments; "
784 "use --help for usage");
787 /* Local and remote vconns. */
788 s->dp_name = argv[0];
789 s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
791 /* Set accept_controller_regex. */
793 accept_re = vconn_ssl_is_configured() ? "^ssl:.*" : ".*";
795 retval = regcomp(&s->accept_controller_regex, accept_re,
796 REG_NOSUB | REG_EXTENDED);
798 size_t length = regerror(retval, &s->accept_controller_regex, NULL, 0);
799 char *buffer = xmalloc(length);
800 regerror(retval, &s->accept_controller_regex, buffer, length);
801 ofp_fatal(0, "%s: %s", accept_re, buffer);
803 s->accept_controller_re = accept_re;
805 /* Mode of operation. */
806 s->discovery = s->controller_name == NULL;
807 if (s->discovery && !s->in_band) {
808 ofp_fatal(0, "Cannot perform discovery with out-of-band control");
813 if (s->rate_limit < 100) {
814 VLOG_WARN("Rate limit set to unusually low value %d",
817 if (!s->burst_limit) {
818 s->burst_limit = s->rate_limit / 4;
820 s->burst_limit = MAX(s->burst_limit, 1);
821 s->burst_limit = MIN(s->burst_limit, INT_MAX / 1000);
828 printf("%s: secure channel, a relay for OpenFlow messages.\n"
829 "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
830 "DATAPATH is an active connection method to a local datapath.\n"
831 "CONTROLLER is an active OpenFlow connection method; if it is\n"
832 "omitted, then secchan performs controller discovery.\n",
833 program_name, program_name);
834 vconn_usage(true, true, true);
835 printf("\nController discovery options:\n"
836 " --accept-vconn=REGEX accept matching discovered controllers\n"
837 " --no-resolv-conf do not update /etc/resolv.conf\n"
838 "\nNetworking options:\n"
839 " -F, --fail=open|closed when controller connection fails:\n"
840 " closed: drop all packets\n"
841 " open (default): act as learning switch\n"
842 " --inactivity-probe=SECS time between inactivity probes\n"
843 " --max-idle=SECS max idle for flows set up by secchan\n"
844 " --max-backoff=SECS max time between controller connection\n"
845 " attempts (default: 15 seconds)\n"
846 " -l, --listen=METHOD allow management connections on METHOD\n"
847 " (a passive OpenFlow connection method)\n"
848 " -m, --monitor=METHOD copy traffic to/from kernel to METHOD\n"
849 " (a passive OpenFlow connection method)\n"
850 " --out-of-band controller connection is out-of-band\n"
851 " --stp enable 802.1D Spanning Tree Protocol\n"
852 " --no-stp disable 802.1D Spanning Tree Protocol\n"
853 " --netflow=HOST:PORT send NetFlow v5 messages when flows end\n"
854 "\nRate-limiting of \"packet-in\" messages to the controller:\n"
855 " --rate-limit[=PACKETS] max rate, in packets/s (default: 1000)\n"
856 " --burst-limit=BURST limit on packet credit for idle time\n"
857 "\nRemote command execution options:\n"
858 " --command-acl=[!]GLOB[,[!]GLOB...] set allowed/denied commands\n"
859 " --command-dir=DIR set command dir (default: %s/commands)\n",
863 printf("\nOther options:\n"
864 " -h, --help display this help message\n"
865 " -V, --version display version information\n");