confbase.Y 6.45 KB
Newer Older
Martin Mareš's avatar
Martin Mareš committed
1 2 3
/*
 *	BIRD -- Configuration Parser Top
 *
4
 *	(c) 1998--2000 Martin Mares <mj@ucw.cz>
Martin Mareš's avatar
Martin Mareš committed
5 6 7 8 9 10
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

CF_HDR

11 12
#define PARSER 1

Martin Mareš's avatar
Martin Mareš committed
13 14
#include "nest/bird.h"
#include "conf/conf.h"
15 16
#include "lib/resource.h"
#include "lib/socket.h"
17
#include "sysdep/unix/timer.h"
18
#include "lib/string.h"
19
#include "nest/protocol.h"
20
#include "nest/iface.h"
21
#include "nest/route.h"
22
#include "nest/cli.h"
23
#include "filter/filter.h"
Martin Mareš's avatar
Martin Mareš committed
24

25 26
/* FIXME: Turn on YYERROR_VERBOSE and work around lots of bison bugs? */

27 28 29 30 31 32 33 34 35
CF_DEFINES

static void
check_u16(unsigned val)
{
  if (val > 0xFFFF)
    cf_error("Value %d out of range (0-65535)", val);
}

Martin Mareš's avatar
Martin Mareš committed
36 37 38 39
CF_DECLS

%union {
  int i;
40
  u32 i32;
41
  u64 i64;
Martin Mareš's avatar
Martin Mareš committed
42
  ip_addr a;
43 44
  ip4_addr ip4;
  ip6_addr ip6;
45
  net_addr net;
46
  net_addr *net_ptr;
Martin Mareš's avatar
Martin Mareš committed
47 48
  struct symbol *s;
  char *t;
49
  struct rtable_config *r;
50
  struct channel_config *cc;
51 52
  struct f_inst *x;
  struct filter *f;
53
  struct f_tree *e;
54
  struct f_trie *trie;
55
  struct f_val v;
56
  struct f_path_mask *h;
Pavel Machek's avatar
Pavel Machek committed
57
  struct password_item *p;
58
  struct rt_show_data *ra;
59
  struct sym_show_data *sd;
60
  struct lsadb_show_data *ld;
61
  struct iface *iface;
62
  void *g;
63
  bird_clock_t time;
64
  struct f_prefix px;
65
  struct proto_spec ps;
66
  struct channel_limit cl;
67
  struct timeformat *tf;
68
  mpls_label_stack *mls;
Martin Mareš's avatar
Martin Mareš committed
69 70
}

71
%token END CLI_MARKER INVALID_TOKEN ELSECOL DDOT
Pavel Machek's avatar
Pavel Machek committed
72
%token GEQ LEQ NEQ AND OR
73
%token PO PC
Martin Mareš's avatar
Martin Mareš committed
74
%token <i> NUM ENUM
75 76
%token <ip4> IP4
%token <ip6> IP6
77
%token <i64> VPN_RD
Martin Mareš's avatar
Martin Mareš committed
78 79
%token <s> SYM
%token <t> TEXT
80
%type <iface> ipa_scope
Martin Mareš's avatar
Martin Mareš committed
81

82
%type <i> expr bool pxlen4
83
%type <i32> expr_us
84
%type <time> datetime
85
%type <a> ipa
86
%type <net> net_ip4_ net_ip6_ net_ip6 net_ip_ net_ip net_or_ipa
87
%type <net_ptr> net_ net_any net_vpn4_ net_vpn6_ net_vpn_ net_roa4_ net_roa6_ net_roa_
88
%type <mls> label_stack_start label_stack
89 90

%type <t> text opttext
91

92
%nonassoc PREFIX_DUMMY
93
%left AND OR
94
%nonassoc '=' '<' '>' '~' GEQ LEQ NEQ NMA PO PC
95
%left '+' '-'
96
%left '*' '/' '%'
97
%left '!'
98
%nonassoc '.'
99

100
CF_KEYWORDS(DEFINE, ON, OFF, YES, NO, S, MS, US, PORT, VPN)
101

Martin Mareš's avatar
Martin Mareš committed
102 103
CF_GRAMMAR

104 105
/* Basic config file structure */

106
config: conf_entries END { return 0; }
107
 | CLI_MARKER cli_cmd { return 0; }
Martin Mareš's avatar
Martin Mareš committed
108 109 110 111
 ;

conf_entries:
   /* EMPTY */
112
 | conf_entries conf
Martin Mareš's avatar
Martin Mareš committed
113 114
 ;

115
CF_ADDTO(conf, ';')
Martin Mareš's avatar
Martin Mareš committed
116

117

118
/* Constant expressions */
119

120 121 122 123 124 125 126 127 128 129
CF_ADDTO(conf, definition)
definition:
   DEFINE SYM '=' term ';' {
     struct f_val *val = cfg_alloc(sizeof(struct f_val));
     *val = f_eval($4, cfg_mem);
     if (val->type == T_RETURN) cf_error("Runtime error");
     cf_define_symbol($2, SYM_CONSTANT | val->type, val);
   }
 ;

130
expr:
131
   NUM
132
 | '(' term ')' { $$ = f_eval_int($2); }
133 134 135
 | SYM {
     if ($1->class != (SYM_CONSTANT | T_INT)) cf_error("Number expected");
     $$ = SYM_VAL($1).i; }
136 137
 ;

138 139

expr_us:
140 141 142
   expr S  { $$ = (u32) $1 * 1000000; }
 | expr MS { $$ = (u32) $1 * 1000; }
 | expr US { $$ = (u32) $1 * 1; }
143 144
 ;

145 146 147
/* Switches */

bool:
Ondřej Zajíček's avatar
Ondřej Zajíček committed
148
   expr { $$ = !!$1; }
149 150 151 152 153 154 155
 | ON { $$ = 1; }
 | YES { $$ = 1; }
 | OFF { $$ = 0; }
 | NO { $$ = 0; }
 | /* Silence means agreement */ { $$ = 1; }
 ;

156

157
/* Addresses */
158

159
ipa:
160 161
   IP4 { $$ = ipa_from_ip4($1); }
 | IP6 { $$ = ipa_from_ip6($1); }
162
 | SYM {
163
     if ($1->class != (SYM_CONSTANT | T_IP)) cf_error("IP address expected");
164
     $$ = SYM_VAL($1).ip;
165 166
   }
 ;
167

168 169 170 171 172
ipa_scope:
   /* empty */ { $$ = NULL; }
 | '%' SYM { $$ = if_get_by_name($2->name); }
 ;

173

174
/* Networks - internal */
175

176 177 178 179 180 181 182
pxlen4:
   '/' NUM {
     if ($2 < 0 || $2 > IP4_MAX_PREFIX_LENGTH) cf_error("Invalid prefix length %d", $2);
     $$ = $2;
   }
 | ':' IP4 {
     $$ = ip4_masklen($2);
183
     if ($$ == 255) cf_error("Invalid netmask %I4", $2);
184 185
   }
 ;
186

187 188 189 190 191 192
net_ip4_: IP4 pxlen4
{
  net_fill_ip4(&($$), $1, $2);
  if (!net_validate_ip4((net_addr_ip4 *) &($$)))
    cf_error("Invalid IPv4 prefix");
};
193

194 195 196 197 198 199 200 201
net_ip6_: IP6 '/' NUM
{
  net_fill_ip6(&($$), $1, $3);
  if ($3 < 0 || $3 > IP6_MAX_PREFIX_LENGTH)
    cf_error("Invalid prefix length %d", $3);
  if (!net_validate_ip6((net_addr_ip6 *) &($$)))
    cf_error("Invalid IPv6 prefix");
};
202

203 204 205
net_vpn4_: VPN_RD net_ip4_
{
  $$ = cfg_alloc(sizeof(net_addr_vpn4));
Ondřej Zajíček's avatar
Ondřej Zajíček committed
206
  net_fill_vpn4($$, net4_prefix(&$2), net4_pxlen(&$2), $1);
207 208 209 210 211
}

net_vpn6_: VPN_RD net_ip6_
{
  $$ = cfg_alloc(sizeof(net_addr_vpn6));
Ondřej Zajíček's avatar
Ondřej Zajíček committed
212
  net_fill_vpn6($$, net6_prefix(&$2), net6_pxlen(&$2), $1);
213 214
}

215 216
net_roa4_: net_ip4_ MAX NUM AS NUM
{
217
  $$ = cfg_alloc(sizeof(net_addr_roa4));
218
  net_fill_roa4($$, net4_prefix(&$1), net4_pxlen(&$1), $3, $5);
219
  if ($3 < (int) net4_pxlen(&$1) || $3 > IP4_MAX_PREFIX_LENGTH)
220 221 222 223 224
    cf_error("Invalid max prefix length %d", $3);
};

net_roa6_: net_ip6_ MAX NUM AS NUM
{
225
  $$ = cfg_alloc(sizeof(net_addr_roa6));
226
  net_fill_roa6($$, net6_prefix(&$1), net6_pxlen(&$1), $3, $5);
227
  if ($3 < (int) net6_pxlen(&$1) || $3 > IP6_MAX_PREFIX_LENGTH)
228 229 230
    cf_error("Invalid max prefix length %d", $3);
};

231
net_ip_: net_ip4_ | net_ip6_ ;
232
net_vpn_: net_vpn4_ | net_vpn6_ ;
Ondřej Zajíček's avatar
Ondřej Zajíček committed
233
net_roa_: net_roa4_ | net_roa6_ ;
234

235 236
net_:
   net_ip_ { $$ = cfg_alloc($1.length); net_copy($$, &($1)); }
237
 | net_vpn_
238
 | net_roa_
239
 | net_flow_
240
 ;
241 242


243 244 245 246 247 248 249 250
/* Networks - regular */

net_ip6:
   net_ip6_
 | SYM {
     if (($1->class != (SYM_CONSTANT | T_NET)) || (SYM_VAL($1).net->type != NET_IP6))
       cf_error("IPv6 network expected");
     $$ = * SYM_VAL($1).net;
251
   }
252 253 254 255 256 257 258 259
 ;

net_ip:
   net_ip_
 | SYM {
     if (($1->class != (SYM_CONSTANT | T_NET)) || !net_is_ip(SYM_VAL($1).net))
       cf_error("IP network expected");
     $$ = * SYM_VAL($1).net;
260 261 262
   }
 ;

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
net_any:
   net_
 | SYM {
     if ($1->class != (SYM_CONSTANT | T_NET))
       cf_error("Network expected");
     $$ = (net_addr *) SYM_VAL($1).net; /* Avoid const warning */
   }
 ;

net_or_ipa:
   net_ip4_
 | net_ip6_
 | IP4 { net_fill_ip4(&($$), $1, IP4_MAX_PREFIX_LENGTH); }
 | IP6 { net_fill_ip6(&($$), $1, IP6_MAX_PREFIX_LENGTH); }
 | SYM {
     if ($1->class == (SYM_CONSTANT | T_IP))
       net_fill_ip_host(&($$), SYM_VAL($1).ip);
     else if (($1->class == (SYM_CONSTANT | T_NET)) && net_is_ip(SYM_VAL($1).net))
       $$ = * SYM_VAL($1).net;
     else
       cf_error("IP address or network expected");
284 285 286
   }
 ;

287 288
label_stack_start: NUM
{
289 290 291
  $$ = cfg_allocz(sizeof(mpls_label_stack));
  $$->len = 1;
  $$->stack[0] = $1;
292 293 294 295 296
};

label_stack:
    label_stack_start
  | label_stack '/' NUM {
297
    if ($1->len >= MPLS_MAX_LABEL_STACK)
Ondřej Zajíček's avatar
Ondřej Zajíček committed
298
      cf_error("Too many labels in stack");
299
    $1->stack[$1->len++] = $3;
300 301 302
    $$ = $1;
  }
;
303

Martin Mareš's avatar
Martin Mareš committed
304
datetime:
305
   TEXT {
306
     $$ = tm_parse_datetime($1);
307
     if (!$$)
308
       cf_error("Invalid date and time");
309
   }
Pavel Machek's avatar
Pavel Machek committed
310 311
 ;

312 313 314 315 316 317 318 319
text:
   TEXT
 | SYM {
     if ($1->class != (SYM_CONSTANT | T_STRING)) cf_error("String expected");
     $$ = SYM_VAL($1).s;
   }
 ;

320 321 322 323 324 325
opttext:
    TEXT
 | /* empty */ { $$ = NULL; }
 ;


Martin Mareš's avatar
Martin Mareš committed
326 327 328
CF_CODE

CF_END