summaryrefslogtreecommitdiffstats
path: root/src/config.y
blob: be30ab030e9841f6c5aa9fff81c079200c2b618a (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
/*
  Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


%define api.pure
%define api.push-pull push
%name-prefix "fastd_config_"
%locations
%parse-param {fastd_context *ctx}
%parse-param {fastd_config *conf}
%parse-param {const char *filename}
%parse-param {int depth}

%code requires {
	#define _GNU_SOURCE

	#include <fastd.h>
	#include <arpa/inet.h>
	#include <unistd.h>
}

%union {
	int num;
	char* str;
	bool boolean;
	struct in_addr addr;
	struct in6_addr addr6;
}

%token START_CONFIG
%token START_PEER_CONFIG

%token <num> TOK_INTEGER
%token <str> TOK_STRING

%token TOK_INTERFACE
%token TOK_BIND
%token TOK_MTU
%token TOK_MODE
%token TOK_PROTOCOL
%token TOK_PEER
%token TOK_ADDRESS
%token TOK_SECRET
%token TOK_KEY
%token TOK_INCLUDE
%token TOK_AS
%token TOK_ANY
%token TOK_TAP
%token TOK_TUN
%token TOK_ON
%token TOK_UP
%token TOK_PEERS
%token TOK_FROM
%token TOK_LOG
%token TOK_LEVEL
%token TOK_FATAL
%token TOK_ERROR
%token TOK_WARN
%token TOK_INFO
%token TOK_VERBOSE
%token TOK_DEBUG
%token TOK_PEER_TO_PEER
%token TOK_YES
%token TOK_NO

%token <addr> TOK_ADDR
%token <addr6> TOK_ADDR6


%code {
	#include <config.h>
	#include <stdint.h>
	#include <peer.h>

	void fastd_config_error(YYLTYPE *loc, fastd_context *ctx, fastd_config *conf, const char *filename, int depth, char *s);

	extern fastd_protocol fastd_protocol_null;

	#ifdef WITH_PROTOCOL_ECFXP
	extern fastd_protocol fastd_protocol_ec25519_fhmqvc_xsalsa20_poly1305;
	#endif
}


%type <str> maybe_string

%type <num> port
%type <boolean> boolean
%type <num> maybe_port
%type <str> maybe_as

%%
start:		START_CONFIG config
	|	START_PEER_CONFIG peer_conf
	;

config:		config statement
	|
	;

statement:	TOK_LOG log ';'
	| 	TOK_INTERFACE interface ';'
	| 	TOK_BIND bind ';'
	|	TOK_MTU mtu ';'
	|	TOK_MODE mode ';'
	|	TOK_PROTOCOL protocol ';'
	|	TOK_SECRET secret ';'
	|	TOK_ON TOK_UP on_up ';'
	|	TOK_PEER peer '{' peer_conf '}'
	|	TOK_PEER_TO_PEER peer_to_peer ';'
	|	TOK_INCLUDE include ';'
	;

log:		TOK_LEVEL log_level
	;

log_level:	TOK_FATAL	{ conf->loglevel = LOG_FATAL; }
	|	TOK_ERROR	{ conf->loglevel = LOG_ERROR; }
	|	TOK_WARN	{ conf->loglevel = LOG_WARN; }
	|	TOK_INFO	{ conf->loglevel = LOG_INFO; }
	|	TOK_VERBOSE	{ conf->loglevel = LOG_VERBOSE; }
	|	TOK_DEBUG	{ conf->loglevel = LOG_DEBUG; }
	;

interface:	TOK_STRING	{ free(conf->ifname); conf->ifname = $1; }
	;

bind:		TOK_ADDR maybe_port {
			conf->bind_addr_in.sin_family = AF_INET;
			conf->bind_addr_in.sin_addr = $1;
			conf->bind_addr_in.sin_port = $2;
		}
	|	TOK_ADDR6 maybe_port {
			conf->bind_addr_in6.sin6_family = AF_INET6;
			conf->bind_addr_in6.sin6_addr = $1;
			conf->bind_addr_in6.sin6_port = $2;
		}
	|	TOK_ANY maybe_port {
			conf->bind_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
			conf->bind_addr_in.sin_port = $2;
			conf->bind_addr_in6.sin6_addr = in6addr_any;
			conf->bind_addr_in6.sin6_port = $2;
		}
	;

mtu:		TOK_INTEGER	{ conf->mtu = $1; }
	;

mode:		TOK_TAP		{ conf->mode = MODE_TAP; }
	|	TOK_TUN		{ conf->mode = MODE_TUN; }
	;

protocol:	TOK_STRING {
			if (!strcmp($1, "null"))
				conf->protocol = &fastd_protocol_null;
#ifdef WITH_PROTOCOL_ECFXP
			else if (!strcmp($1, "ecfxp"))
				conf->protocol = &fastd_protocol_ec25519_fhmqvc_xsalsa20_poly1305;
#endif
			else
				exit_error(ctx, "config error: invalid protocol `%s'", $1);

			free($1);
}
	;

secret:		TOK_STRING	{ free(conf->secret); conf->secret = $1; }
	;

on_up:		TOK_STRING	{
			free(conf->on_up);
			free(conf->on_up_dir);

			conf->on_up = $1;
			conf->on_up_dir = get_current_dir_name();
		}
	;

peer:		maybe_string {
			fastd_peer_config_new(ctx, conf);
			conf->peers->name = $1;
		}
	;

peer_conf:	peer_conf peer_statement
	|
	;

peer_statement: TOK_ADDRESS peer_address ';'
	|	TOK_KEY peer_key ';'
	|	TOK_INCLUDE peer_include  ';'
	;

peer_address:	TOK_ADDR ':' port {
			conf->peers->address.in.sin_family = AF_INET;
			conf->peers->address.in.sin_addr = $1;
			conf->peers->address.in.sin_port = $3;
		}
	|	TOK_ADDR6 ':' port {
			conf->peers->address.in6.sin6_family = AF_INET6;
			conf->peers->address.in6.sin6_addr = $1;
			conf->peers->address.in6.sin6_port = $3;
		}
	;

peer_key:	TOK_STRING	{ free(conf->peers->key); conf->peers->key = $1; }
	;

peer_include:	TOK_STRING	{ fastd_read_config(ctx, conf, $1, true, depth); free($1); }
	;


peer_to_peer:	boolean		{ conf->peer_to_peer = $1; }
	;


include:	TOK_PEER TOK_STRING maybe_as {
			fastd_peer_config_new(ctx, conf);
			conf->peers->name = $3;

			fastd_read_config(ctx, conf, $2, true, depth);
			free($2);
		}
	|	TOK_PEERS TOK_FROM TOK_STRING {
			fastd_read_config_dir(ctx, conf, $3, depth);
			free($3);
		}
	|	TOK_STRING	{ fastd_read_config(ctx, conf, $1, false, depth); free($1); }
	;


maybe_string:	TOK_STRING
	|			{ $$ = NULL; }
	;

maybe_port:	':' port	{ $$ = $2; }
	|			{ $$ = 0; }
	;

maybe_as:	TOK_AS TOK_STRING { $$ = $2; }
	|			{ $$ = NULL; }
	;

boolean:	TOK_YES		{ $$ = true; }
	|	TOK_NO		{ $$ = false; }
	;

port:		TOK_INTEGER {
			if ($1 < 0 || $1 > 65635)
				exit_error(ctx, "invalid port %i", $1);
			$$ = htons($1);
		}
	;
%%
void fastd_config_error(YYLTYPE *loc, fastd_context *ctx, fastd_config *conf, const char *filename, int depth, char *s) {
	exit_error(ctx, "config error: %s at %s:%i:%i", s, filename, loc->first_line, loc->first_column);
}