summaryrefslogtreecommitdiff
path: root/usr.sbin/switchctl/ofpclient.c
blob: f60e3317431c4ec8b3fee049d4ed3fb2c926530f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*	$OpenBSD: ofpclient.c,v 1.6 2017/01/09 16:42:14 reyk Exp $	*/

/*
 * Copyright (c) 2016 Reyk Floeter <reyk@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/queue.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/tree.h>
#include <sys/un.h>

#include <net/if.h>
#include <net/ofp.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <poll.h>
#include <err.h>
#include <pwd.h>
#include <vis.h>

#include "switchd.h"
#include "parser.h"

void	 ofpclient_read(struct switch_connection *, int);
int	 flowmod(struct switchd *, struct switch_connection *,
	    struct parse_result *);
int	 flowmod_test(struct switchd *, struct switch_connection *);

void
ofpclient(struct parse_result *res, struct passwd *pw)
{
	struct switch_connection con;
	struct switchd		 sc;
	int			 s, timeout;

	memset(&sc, 0, sizeof(sc));
	sc.sc_tap = -1;

	/* If no uri has been specified, try to connect to localhost */
	if (res->uri.swa_addr.ss_family == AF_UNSPEC) {	
		res->uri.swa_type = SWITCH_CONN_TCP;
		if (parsehostport("127.0.0.1",
		    (struct sockaddr *)&res->uri.swa_addr,
		    sizeof(res->uri.swa_addr)) != 0)
			fatal("could not parse address");
	}

	memset(&con, 0, sizeof(con));
	memcpy(&con.con_peer, &res->uri.swa_addr, sizeof(res->uri.swa_addr));
	con.con_sc = &sc;

	/*
	 * Connect and send the request
	 */
	switch (res->uri.swa_type) {
	case SWITCH_CONN_TCP:
		if ((s = socket(res->uri.swa_addr.ss_family, SOCK_STREAM,
		    IPPROTO_TCP)) == -1)
			fatal("socket");

		/* Use the default port if no port has been specified */
		if (socket_getport(&con.con_peer) == 0)
			(void)socket_setport(&con.con_peer, SWITCHD_CTLR_PORT);

		if (connect(s, (struct sockaddr *)&con.con_peer,
		    con.con_peer.ss_len) == -1)
			fatal("connect");

		con.con_fd = s;
		break;
	default:
		fatalx("connect type not supported");
	}

	/* Drop privileges */
	if (pledge("stdio", NULL) == -1)
		err(1, "pledge");

	/* Set a default read timeout */
	timeout = 3 * 1000;

	log_setverbose(res->verbose);

	ofp_send_hello(&sc, &con, OFP_V_1_3);
	ofpclient_read(&con, timeout);

	log_setverbose(res->quiet ? res->verbose : 2);

	switch (res->action) {
	case DUMP_DESC:
		ofp13_desc(&sc, &con);
		break;
	case DUMP_FEATURES:
		ofp_send_featuresrequest(&sc, &con);
		break;
	case DUMP_FLOWS:
		ofp13_flow_stats(&sc, &con, OFP_PORT_ANY, OFP_GROUP_ID_ANY,
		    res->table);
		break;
	case DUMP_TABLES:
		ofp13_table_features(&sc, &con, res->table);
		break;
	case FLOW_ADD:
	case FLOW_DELETE:
	case FLOW_MODIFY:
		timeout = 0;
		flowmod(&sc, &con, res);
		break;
	default:
		fatalx("unsupported action");
	}

	/* XXX */
	ofpclient_read(&con, timeout);
}

int
flowmod(struct switchd *sc, struct switch_connection *con,
    struct parse_result *res)
{
	struct ofp_header	*oh;
	struct ofp_flow_mod	*fm;

	if (oflowmod_iclose(&res->fctx) == -1)
		goto err;
	if (oflowmod_close(&res->fctx) == -1)
		goto err;

	fm = res->fctx.ctx_fm;
	fm->fm_table_id = res->table;
	oh = &fm->fm_oh;

	if (ofp_validate(sc, &con->con_local, &con->con_peer,
	    oh, res->fbuf, oh->oh_version) != 0)
		goto err;

	ofrelay_write(con, res->fbuf);

	return (0);

 err:
	(void)oflowmod_err(&res->fctx, __func__, __LINE__);
	log_warnx("invalid flow");
	return (-1);
}

void
ofpclient_read(struct switch_connection *con, int timeout)
{
	uint8_t			rbuf[0xffff];
	ssize_t			rlen;
	struct ofp_header	*oh;
	struct ibuf		*ibuf;
	struct pollfd		 pfd[1];
	int			 nfds;

	/* Wait for response */
	pfd[0].fd = con->con_fd;
	pfd[0].events = POLLIN;
	nfds = poll(pfd, 1, timeout);
	if (nfds == -1 || (pfd[0].revents & (POLLERR|POLLHUP|POLLNVAL)))
		fatal("poll error");
	if (nfds == 0) {
		if (timeout)
			fatal("time out");
		return;
	}

	if ((rlen = read(con->con_fd, rbuf, sizeof(rbuf))) == -1)
		fatal("read");
	if (rlen == 0)
		fatal("connection closed");

	if ((ibuf = ibuf_new(rbuf, rlen)) == NULL)
		fatal("ibuf_new");

	if ((oh = ibuf_seek(ibuf, 0, sizeof(*oh))) == NULL)
		fatal("short header");

	if (ofp_validate(con->con_sc,
	    &con->con_peer, &con->con_local, oh, ibuf, oh->oh_version) != 0)
		fatal("ofp_validate");

	ibuf_free(ibuf);
}

/*
 * stubs for ofp*.c
 */

void
ofrelay_write(struct switch_connection *con, struct ibuf *buf)
{
	struct msgbuf		msgbuf;

	msgbuf_init(&msgbuf);
	msgbuf.fd = con->con_fd;

	ibuf_close(&msgbuf, buf);
	ibuf_write(&msgbuf);
}

struct switch_control *
switch_add(struct switch_connection *con)
{
	static struct switch_control sw;
	con->con_switch = &sw;
	return (&sw);
}

struct macaddr *
switch_learn(struct switchd *sc, struct switch_control *sw,
    uint8_t *ea, uint32_t port)
{
	return (NULL);
}

struct macaddr *
switch_cached(struct switch_control *sw, uint8_t *ea)
{
	return (NULL);
}

int
ofp_nextstate(struct switchd *sc, struct switch_connection *con,
    enum ofp_state state)
{
	int		rv = 0;

	switch (con->con_state) {
	case OFP_STATE_CLOSED:
		if (state != OFP_STATE_HELLO_WAIT)
			return (-1);
		break;

	case OFP_STATE_HELLO_WAIT:
		if (state != OFP_STATE_FEATURE_WAIT)
			return (-1);

		rv = ofp_send_featuresrequest(sc, con);
		break;

	case OFP_STATE_FEATURE_WAIT:
		if (state != OFP_STATE_ESTABLISHED)
			return (-1);
		break;

	case OFP_STATE_ESTABLISHED:
		if (state != OFP_STATE_CLOSED)
			return (-1);
		break;

	default:
		return (-1);
	}

	/* Set the next state. */
	con->con_state = state;

	return (rv);
}