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.
37 #include <netinet/in.h>
45 #include "command-line.h"
50 #include "mac-learning.h"
54 #include "poll-loop.h"
57 #include "vconn-ssl.h"
59 #include "vlog-socket.h"
62 #define THIS_MODULE VLM_secchan
64 #include "ofp-print.h"
66 static const char *listen_vconn_name;
78 struct half halves[2];
81 static struct list relays = LIST_INITIALIZER(&relays);
83 /* Enable the local port? */
84 static int local_port;
86 /* MAC address of local port. */
87 static uint8_t local_mac[ETH_ADDR_LEN];
89 /* MAC learning table for local port. */
90 static struct mac_learning *local_ml;
92 static void parse_options(int argc, char *argv[]);
93 static void usage(void) NO_RETURN;
95 static void new_management_connection(const char *nl_name, struct vconn *new_remote);
96 static void relay_create(struct rconn *local, struct rconn *remote);
97 static void relay_run(struct relay *);
98 static void relay_wait(struct relay *);
99 static void relay_destroy(struct relay *);
101 static bool local_hook(struct relay *r);
104 main(int argc, char *argv[])
106 struct vconn *listen_vconn;
107 struct netdev *of_device;
112 set_program_name(argv[0]);
113 register_fault_handlers();
115 parse_options(argc, argv);
117 if (argc - optind != 2) {
119 "need exactly two non-option arguments; use --help for usage");
121 nl_name = argv[optind];
122 if (strncmp(nl_name, "nl:", 3)
123 || strlen(nl_name) < 4
124 || nl_name[strspn(nl_name + 3, "0123456789") + 3]) {
125 fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", nl_name);
128 if (listen_vconn_name) {
129 retval = vconn_open(listen_vconn_name, &listen_vconn);
130 if (retval && retval != EAGAIN) {
131 fatal(retval, "opening %s", listen_vconn_name);
133 if (!vconn_is_passive(listen_vconn)) {
134 fatal(0, "%s is not a passive vconn", listen_vconn_name);
141 snprintf(of_name, sizeof of_name, "of%s", nl_name + 3);
142 retval = netdev_open(of_name, &of_device);
144 enum netdev_flags flags;
145 retval = netdev_get_flags(of_device, &flags);
147 if (flags & NETDEV_UP) {
151 memcpy(local_mac, netdev_get_etheraddr(of_device),
153 if (netdev_get_in6(of_device, &in6)) {
154 VLOG_WARN("Ignoring IPv6 address on %s device: "
155 "IPv6 not supported", of_name);
157 local_ml = mac_learning_create();
160 error(retval, "Could not get flags for %s device", of_name);
162 netdev_close(of_device);
164 error(retval, "Could not open %s device", of_name);
167 retval = vlog_server_listen(NULL, NULL);
169 fatal(retval, "Could not listen for vlog connections");
172 relay_create(rconn_new(argv[optind], 1), rconn_new(argv[optind + 1], 1));
177 LIST_FOR_EACH_SAFE (r, n, struct relay, node, &relays) {
182 struct vconn *new_remote;
183 retval = vconn_accept(listen_vconn, &new_remote);
185 if (retval != EAGAIN) {
186 VLOG_WARN("accept failed (%s)", strerror(retval));
190 new_management_connection(nl_name, new_remote);
194 /* Wait for something to happen. */
195 LIST_FOR_EACH (r, struct relay, node, &relays) {
199 vconn_accept_wait(listen_vconn);
208 new_management_connection(const char *nl_name, struct vconn *new_remote)
210 char *nl_name_without_subscription;
211 struct vconn *new_local;
212 struct rconn *r1, *r2;
215 /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123. We
216 * only accept the former syntax in main().
218 * nl:123:0 opens a netlink connection to local datapath 123 without
219 * obtaining a subscription for ofp_packet_in or ofp_flow_expired
221 nl_name_without_subscription = xasprintf("%s:0", nl_name);
222 retval = vconn_open(nl_name_without_subscription, &new_local);
224 VLOG_ERR("could not connect to %s (%s)",
225 nl_name_without_subscription, strerror(retval));
226 vconn_close(new_remote);
229 free(nl_name_without_subscription);
231 /* Add it to the relay list. */
232 r1 = rconn_new_from_vconn(nl_name_without_subscription, 1, new_local);
233 r2 = rconn_new_from_vconn("passive", 1, new_remote);
234 relay_create(r1, r2);
238 relay_create(struct rconn *local, struct rconn *remote)
243 r = xmalloc(sizeof *r);
244 r->halves[HALF_LOCAL].rconn = local;
245 r->halves[HALF_REMOTE].rconn = remote;
246 for (i = 0; i < 2; i++) {
247 r->halves[i].rxbuf = NULL;
249 list_push_back(&relays, &r->node);
253 relay_run(struct relay *r)
258 for (i = 0; i < 2; i++) {
259 rconn_run(r->halves[i].rconn);
262 /* Limit the number of iterations to prevent other tasks from starving. */
263 for (iteration = 0; iteration < 50; iteration++) {
264 bool progress = false;
265 for (i = 0; i < 2; i++) {
266 struct half *this = &r->halves[i];
267 struct half *peer = &r->halves[!i];
270 this->rxbuf = rconn_recv(this->rconn);
271 if (this->rxbuf && i == HALF_LOCAL && local_hook(r)) {
272 buffer_delete(this->rxbuf);
278 int retval = rconn_send(peer->rconn, this->rxbuf);
279 if (retval != EAGAIN) {
283 buffer_delete(this->rxbuf);
294 for (i = 0; i < 2; i++) {
295 struct half *this = &r->halves[i];
296 if (!rconn_is_alive(this->rconn)) {
304 relay_wait(struct relay *r)
308 for (i = 0; i < 2; i++) {
309 struct half *this = &r->halves[i];
311 rconn_run_wait(this->rconn);
313 rconn_recv_wait(this->rconn);
319 relay_destroy(struct relay *r)
323 list_remove(&r->node);
324 for (i = 0; i < 2; i++) {
325 struct half *this = &r->halves[i];
326 rconn_destroy(this->rconn);
327 buffer_delete(this->rxbuf);
333 local_hook(struct relay *r)
335 struct rconn *rc = r->halves[HALF_LOCAL].rconn;
336 struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
337 struct ofp_packet_in *opi;
338 struct ofp_header *oh;
339 size_t pkt_ofs, pkt_len;
340 struct buffer pkt, *b;
342 uint16_t in_port, out_port;
349 if (oh->type != OFPT_PACKET_IN) {
352 if (msg->size < offsetof (struct ofp_packet_in, data)) {
353 VLOG_WARN("packet too short (%zu bytes) for packet_in", msg->size);
357 /* Extract flow data from 'opi' into 'flow'. */
359 in_port = ntohs(opi->in_port);
360 pkt_ofs = offsetof(struct ofp_packet_in, data);
361 pkt_len = ntohs(opi->header.length) - pkt_ofs;
362 pkt.data = opi->data;
364 flow_extract(&pkt, in_port, &flow);
366 /* Deal with local stuff. */
367 if (!rconn_is_connected(r->halves[HALF_REMOTE].rconn)
368 && eth_addr_is_broadcast(flow.dl_dst)) {
369 out_port = OFPP_FLOOD;
370 } else if (in_port == OFPP_LOCAL) {
371 out_port = mac_learning_lookup(local_ml, flow.dl_dst);
372 } else if (eth_addr_equals(flow.dl_dst, local_mac)) {
373 out_port = OFPP_LOCAL;
374 if (mac_learning_learn(local_ml, flow.dl_src, in_port)) {
375 VLOG_DBG("learned that "ETH_ADDR_FMT" is on port %"PRIu16,
376 ETH_ADDR_ARGS(flow.dl_src), in_port);
383 if (out_port != OFPP_FLOOD) {
384 b = make_add_simple_flow(&flow, ntohl(opi->buffer_id), out_port);
385 if (rconn_force_send(rc, b)) {
390 /* If the switch didn't buffer the packet, we need to send a copy. */
391 if (out_port == OFPP_FLOOD || ntohl(opi->buffer_id) == UINT32_MAX) {
392 b = make_unbuffered_packet_out(&pkt, in_port, out_port);
393 if (rconn_force_send(rc, b)) {
401 parse_options(int argc, char *argv[])
403 static struct option long_options[] = {
404 {"listen", required_argument, 0, 'l'},
405 {"verbose", optional_argument, 0, 'v'},
406 {"help", no_argument, 0, 'h'},
407 {"version", no_argument, 0, 'V'},
408 VCONN_SSL_LONG_OPTIONS
411 char *short_options = long_options_to_short_options(long_options);
416 c = getopt_long(argc, argv, short_options, long_options, NULL);
423 if (listen_vconn_name) {
424 fatal(0, "-l or --listen may be only specified once");
426 listen_vconn_name = optarg;
433 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
437 vlog_set_verbosity(optarg);
440 VCONN_SSL_OPTION_HANDLERS
455 printf("%s: Secure Channel, a relay for OpenFlow messages.\n"
456 "usage: %s [OPTIONS] LOCAL REMOTE\n"
457 "where LOCAL and REMOTE are active OpenFlow connection methods.\n",
458 program_name, program_name);
459 vconn_usage(true, true);
460 printf("\nNetworking options:\n"
461 " -l, --listen=METHOD allow management connections on METHOD\n"
462 " (a passive OpenFlow connection method)\n"
464 " -v, --verbose set maximum verbosity level\n"
465 " -h, --help display this help message\n"
466 " -V, --version display version information\n");