## modules/rpsl/default.y

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. yyparse
2. defaulterror

       1 %{
       2 /*
       3   filename: default.y
       4
       5   description:
       6     Defines the grammar for an RPSL default attribute.  It was mostly
       7     stolen from the IRRToolSet, simplified by removing ability to parse
       8     things defined by a dictionary (we use XML for extensibility rather
       9     than a dictionary).
      10
      11   notes:
      12     Defines tokens for the associated lexer, default.l.
      13 */
      14 %}
      15
      16 %union {
      17     char *sval;
      18 }
      19
      20 %token OP_OR OP_AND OP_NOT OP_MS OP_EQUAL OP_APPEND OP_COMPARE
      21 %token KEYW_ANY KEYW_PEERAS
      22 %token ASPATH_POSTFIX
      23 %token TKN_FLTRNAME TKN_ASNO TKN_RSNAME TKN_ASNAME TKN_PRFXV4 TKN_PRFXV4RNG
      24 %token TKN_IPV4 TKN_DNS TKN_RTRSNAME TKN_PRNGNAME
      25 %token KEYW_TO KEYW_ACTION KEYW_NETWORKS KEYW_EXCEPT
      26 %token TKN_PREF TKN_MED TKN_DPA TKN_ASPATH TKN_COMMUNITY TKN_NEXT_HOP TKN_COST
      27 %token TKN_COMM_NO
      28 %token KEYW_IGP_COST KEYW_SELF KEYW_PREPEND
      29 %token KEYW_APPEND KEYW_DELETE KEYW_CONTAINS KEYW_AT
      30 %token KEYW_INTERNET KEYW_NO_EXPORT KEYW_NO_ADVERTISE
      31 %token <sval> TKN_INT TKN_DNS
      32 %type <sval> domain_name
      33
      34 %{
      35 #include <stdio.h>
      36 #include <stdarg.h>
      37 #include <stdlib.h>
      38
      39 int yyerror(const char *s);
      40 void syntax_error(char *fmt, ...);
      41
      42 %}
      43
      44 %%
         /* [<][>][^][v][top][bottom][index][help] */
      45
      46 default_rule: KEYW_TO peering opt_action opt_default_filter
      47 ;
      48
      49 peering: as_expr opt_router_expr opt_router_expr_with_at
      50 | TKN_PRNGNAME
      51 ;
      52
      53 opt_action:
      54 | KEYW_ACTION action
      55 ;
      56
      57 opt_default_filter:
      58 | KEYW_NETWORKS filter
      59 ;
      60
      61 as_expr: as_expr OP_OR as_expr_term
      62 | as_expr_term
      63 ;
      64
      65 as_expr_term: as_expr_term OP_AND as_expr_factor
      66 | as_expr_term KEYW_EXCEPT as_expr_factor
      67 | as_expr_factor
      68 ;
      69
      70 as_expr_factor: '(' as_expr ')'
      71 | as_expr_operand
      72 ;
      73
      74 as_expr_operand: TKN_ASNO
      75 | TKN_ASNAME
      76 ;
      77
      78 opt_router_expr:
      79 | router_expr
      80 ;
      81
      82 opt_router_expr_with_at:
      83 | KEYW_AT router_expr
      84 ;
      85
      86 router_expr: router_expr OP_OR router_expr_term
      87 | router_expr_term
      88 ;
      89
      90 router_expr_term: router_expr_term OP_AND router_expr_factor
      91 | router_expr_term KEYW_EXCEPT router_expr_factor
      92 | router_expr_factor
      93 ;
      94
      95 router_expr_factor: '(' router_expr ')'
      96 | router_expr_operand
      97 ;
      98
      99 router_expr_operand: TKN_IPV4
     100 | domain_name {
     101     if (strlen($1) > 255) {
     102         syntax_error("Domain name \"%s\" is longer than 255 characters", $1);
     103     }
     104 }
     105 | TKN_RTRSNAME
     106 ;
     107
     108 domain_name: TKN_DNS
     109 | domain_name '.' TKN_DNS
     110 ;
     111
     112 action: rp_attribute ';'
     113 | action rp_attribute ';'
     114 ;
     115
     116 rp_attribute: pref
     117 | med
     118 | dpa
     119 | aspath
     120 | community
     121 | next_hop
     122 | cost
     123 ;
     124
     125 pref: TKN_PREF OP_EQUAL TKN_INT {
     126       long int val;
     127       char *s, *p;
     128       p = $3;
     129       val = strtol(p, &s, 10);
     130       if ((val < 0) || (val > 65535)) {
     131           syntax_error("pref value \"%s\" is not between 0 and 65535", p);
     132       }
     133 }
     134 ;
     135
     136 med: TKN_MED OP_EQUAL TKN_INT {
     137       long int val;
     138       char *s, *p;
     139       p = $3;
     140       val = strtol(p, &s, 10);
     141       if ((val < 0) || (val > 65535)) {
     142           syntax_error("med value \"%s\" is not between 0 and 65535", p);
     143       }
     144 }
     145 | TKN_MED OP_EQUAL KEYW_IGP_COST
     146 ;
     147
     148 dpa: TKN_DPA OP_EQUAL TKN_INT {
     149       long int val;
     150       char *s, *p;
     151       p = $3;
     152       val = strtol(p, &s, 10);
     153       if ((val < 0) || (val > 65535)) {
     154           syntax_error("dpa value \"%s\" is not between 0 and 65535", p);
     155       }
     156 }
     157 ;
     158
     159 aspath: TKN_ASPATH '.' KEYW_PREPEND '(' asno_list ')'
     160 ;
     161
     162 asno_list: TKN_ASNO
     163 | asno_list ',' TKN_ASNO
     164 ;
     165
     166 community: TKN_COMMUNITY OP_EQUAL community_list
     167 | TKN_COMMUNITY OP_APPEND community_list
     168 | TKN_COMMUNITY '.' KEYW_APPEND '(' community_elm_list ')'
     169 | TKN_COMMUNITY '.' KEYW_DELETE '(' community_elm_list ')'
     170 | TKN_COMMUNITY '.' KEYW_CONTAINS '(' community_elm_list ')'
     171 | TKN_COMMUNITY '(' community_elm_list ')'
     172 | TKN_COMMUNITY OP_COMPARE community_list
     173 ;
     174
     175 community_list: '{' community_elm_list '}'
     176 ;
     177
     178 community_elm_list: community_elm
     179 | community_elm_list ',' community_elm
     180 ;
     181
     182 community_elm: KEYW_INTERNET
     183 | KEYW_NO_EXPORT
     184 | KEYW_NO_ADVERTISE
     185 | TKN_INT {
     186       unsigned long int val;
     187       char *s, *p;
     188       p = $1;
     189       val = strtoul(p, &s, 10);
     190       if ((val < 1) || (val > 4294967295UL) || (*s != '\0')) {
     191           syntax_error("community element \"%s\" is not between 1 and 4294967295",
     192                        p);
     193       }
     194 }
     195 | TKN_COMM_NO
     196 ;
     197
     198 next_hop: TKN_NEXT_HOP OP_EQUAL TKN_IPV4
     199 | TKN_NEXT_HOP OP_EQUAL KEYW_SELF
     200 ;
     201
     202 cost: TKN_COST OP_EQUAL TKN_INT {
     203       long int val;
     204       char *s, *p;
     205       p = $3;
     206       val = strtol(p, &s, 10);
     207       if ((val < 0) || (val > 65535)) {
     208           syntax_error("cost value \"%s\" is not between 0 and 65535", p);
     209       }
     210 }
     211 ;
     212
     213 filter: filter OP_OR filter_term
     214 | filter filter_term %prec OP_OR
     215 | filter_term
     216 ;
     217
     218 filter_term : filter_term OP_AND filter_factor
     219 | filter_factor
     220 ;
     221
     222 filter_factor :  OP_NOT filter_factor
     223 | '(' filter ')'
     224 | filter_operand
     225 ;
     226
     227 filter_operand: KEYW_ANY
     228 | '<' filter_aspath '>'
     229 | rp_attribute
     230 | TKN_FLTRNAME
     231 | filter_prefix
     232 ;
     233
     234 filter_prefix: filter_prefix_operand OP_MS
     235 |  filter_prefix_operand
     236 ;
     237
     238 filter_prefix_operand: TKN_ASNO
     239 | KEYW_PEERAS
     240 | TKN_ASNAME
     241 | TKN_RSNAME
     242 | '{' opt_filter_prefix_list '}'
     243 ;
     244
     245 opt_filter_prefix_list:
     246 | filter_prefix_list
     247 ;
     248
     249 filter_prefix_list: filter_prefix_list_prefix
     250 | filter_prefix_list ',' filter_prefix_list_prefix
     251 ;
     252
     253 filter_prefix_list_prefix: TKN_PRFXV4
     254 | TKN_PRFXV4RNG
     255 ;
     256
     257 filter_aspath: filter_aspath '|' filter_aspath_term
     258 | filter_aspath_term
     259 ;
     260
     261 filter_aspath_term: filter_aspath_term filter_aspath_closure
     262 | filter_aspath_closure
     263 ;
     264
     265 filter_aspath_closure: filter_aspath_closure '*'
     266 | filter_aspath_closure '?'
     267 | filter_aspath_closure '+'
     268 | filter_aspath_closure ASPATH_POSTFIX
     269 | filter_aspath_factor
     270 ;
     271
     272 filter_aspath_factor: '^'
     273 | '$'
     274 | '(' filter_aspath ')'
     275 | filter_aspath_no
     276 ;
     277
     278 filter_aspath_no: TKN_ASNO
     279 | KEYW_PEERAS
     280 | TKN_ASNAME
     281 | '.'
     282 | '[' filter_aspath_range ']'
     283 | '[' '^' filter_aspath_range ']'
     284 ;
     285
     286 filter_aspath_range:
     287 | filter_aspath_range TKN_ASNO
     288 | filter_aspath_range KEYW_PEERAS
     289 | filter_aspath_range '.'
     290 | filter_aspath_range TKN_ASNO '-' TKN_ASNO
     291 | filter_aspath_range TKN_ASNAME
     292 ;
     293
     294 %%
     295
     296 #undef defaulterror
     297 #undef yyerror
     298
     299 int
     300 defaulterror (const char *s)
         /* [<][>][^][v][top][bottom][index][help] */
     301 {
     302     yyerror(s);
     303     return 0;
     304 }
     305

/* [<][>][^][v][top][bottom][index][help] */