Fix LINT64 build.
[dragonfly.git] / contrib / ipfilter / iplang / iplang_y.y
1 %{
2 /*
3  * Copyright (C) 1997-1998 by Darren Reed.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and due credit is given
7  * to the original author and the contributors.
8  *
9  * $Id: iplang_y.y,v 2.2.2.3 2002/12/06 11:41:14 darrenr Exp $
10  */
11
12 #if defined(__sgi) && (IRIX > 602)
13 # include <sys/ptimers.h>
14 #endif
15 #include <stdio.h>
16 #include <string.h>
17 #include <fcntl.h>
18 #if !defined(__SVR4) && !defined(__svr4__)
19 #include <strings.h>
20 #else
21 #include <sys/byteorder.h>
22 #endif
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/param.h>
26 #include <sys/time.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <stddef.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <netinet/in_systm.h>
33 #include <netinet/ip.h>
34 #include <netinet/ip_icmp.h>
35 #ifndef linux
36 #include <netinet/ip_var.h>
37 #endif
38 #include <netinet/tcp.h>
39 #include <netinet/udp.h>
40 #include <net/if.h>
41 #ifndef linux
42 #include <netinet/if_ether.h>
43 #endif
44 #include <netdb.h>
45 #include <arpa/nameser.h>
46 #include <arpa/inet.h>
47 #include <resolv.h>
48 #include <ctype.h>
49 #include "ipsend.h"
50 #include "ip_compat.h"
51 #include "ipf.h"
52 #include "iplang.h"
53
54 #if !defined(__NetBSD__) && (!defined(__FreeBSD_version) && \
55     __FreeBSD_version < 400020 ) && SOLARIS2 < 10 
56 extern  struct ether_addr *ether_aton __P((char *));
57 #endif
58
59 extern  int     opts;
60 extern  struct ipopt_names ionames[];
61 extern  int     state, state, lineNum, token;
62 extern  int     yylineno;
63 extern  char    yytext[];
64 extern  FILE    *yyin;
65 int     yylex   __P((void));
66 #define YYDEBUG 1
67 #if !defined(ultrix) && !defined(hpux)
68 int     yydebug = 1;
69 #else
70 extern  int     yydebug;
71 #endif
72
73 iface_t *iflist = NULL, **iftail = &iflist;
74 iface_t *cifp = NULL;
75 arp_t *arplist = NULL, **arptail = &arplist, *carp = NULL;
76 struct in_addr defrouter;
77 send_t  sending;
78 char    *sclass = NULL;
79 u_short c_chksum __P((u_short *, u_int, u_long));
80 u_long  p_chksum __P((u_short *, u_int));
81
82 u_long  ipbuffer[67584/sizeof(u_long)];         /* 66K */
83 aniphdr_t       *aniphead = NULL, *canip = NULL, **aniptail = &aniphead;
84 ip_t            *ip = NULL;
85 udphdr_t        *udp = NULL;
86 tcphdr_t        *tcp = NULL;
87 icmphdr_t       *icmp = NULL;
88
89 struct statetoopt {
90         int     sto_st;
91         int     sto_op;
92 };
93
94 struct  in_addr getipv4addr __P((char *arg));
95 u_short getportnum __P((char *, char *));
96 struct  ether_addr *geteaddr __P((char *, struct ether_addr *));
97 void    *new_header __P((int));
98 void    free_aniplist __P((void));
99 void    inc_anipheaders __P((int));
100 void    new_data __P((void));
101 void    set_datalen __P((char **));
102 void    set_datafile __P((char **));
103 void    set_data __P((char **));
104 void    new_packet __P((void));
105 void    set_ipv4proto __P((char **));
106 void    set_ipv4src __P((char **));
107 void    set_ipv4dst __P((char **));
108 void    set_ipv4off __P((char **));
109 void    set_ipv4v __P((char **));
110 void    set_ipv4hl __P((char **));
111 void    set_ipv4ttl __P((char **));
112 void    set_ipv4tos __P((char **));
113 void    set_ipv4id __P((char **));
114 void    set_ipv4sum __P((char **));
115 void    set_ipv4len __P((char **));
116 void    new_tcpheader __P((void));
117 void    set_tcpsport __P((char **));
118 void    set_tcpdport __P((char **));
119 void    set_tcpseq __P((char **));
120 void    set_tcpack __P((char **));
121 void    set_tcpoff __P((char **));
122 void    set_tcpurp __P((char **));
123 void    set_tcpwin __P((char **));
124 void    set_tcpsum __P((char **));
125 void    set_tcpflags __P((char **));
126 void    set_tcpopt __P((int, char **));
127 void    end_tcpopt __P((void));
128 void    new_udpheader __P((void));
129 void    set_udplen __P((char **));
130 void    set_udpsum __P((char **));
131 void    prep_packet __P((void));
132 void    packet_done __P((void));
133 void    new_interface __P((void));
134 void    check_interface __P((void));
135 void    set_ifname __P((char **));
136 void    set_ifmtu __P((int));
137 void    set_ifv4addr __P((char **));
138 void    set_ifeaddr __P((char **));
139 void    new_arp __P((void));
140 void    set_arpeaddr __P((char **));
141 void    set_arpv4addr __P((char **));
142 void    reset_send __P((void));
143 void    set_sendif __P((char **));
144 void    set_sendvia __P((char **));
145 void    set_defaultrouter __P((char **));
146 void    new_icmpheader __P((void));
147 void    set_icmpcode __P((int));
148 void    set_icmptype __P((int));
149 void    set_icmpcodetok __P((char **));
150 void    set_icmptypetok __P((char **));
151 void    set_icmpid __P((int));
152 void    set_icmpseq __P((int));
153 void    set_icmpotime __P((int));
154 void    set_icmprtime __P((int));
155 void    set_icmpttime __P((int));
156 void    set_icmpmtu __P((int));
157 void    set_redir __P((int, char **));
158 void    new_ipv4opt __P((void));
159 void    set_icmppprob __P((int));
160 void    add_ipopt __P((int, void *));
161 void    end_ipopt __P((void));
162 void    set_secclass __P((char **));
163 void    free_anipheader __P((void));
164 void    end_ipv4 __P((void));
165 void    end_icmp __P((void));
166 void    end_udp __P((void));
167 void    end_tcp __P((void));
168 void    end_data __P((void));
169 void    yyerror __P((char *));
170 void    iplang __P((FILE *));
171 int     arp_getipv4 __P((char *, char *));
172 int     yyparse __P((void));
173 %}
174 %union {
175         char    *str;
176         int     num;
177 }
178 %token  <num> IL_NUMBER
179 %type   <num> number digits optnumber
180 %token  <str> IL_TOKEN
181 %type   <str> token optoken
182 %token  IL_HEXDIGIT IL_COLON IL_DOT IL_EOF IL_COMMENT
183 %token  IL_INTERFACE IL_IFNAME IL_MTU IL_EADDR
184 %token  IL_IPV4 IL_V4PROTO IL_V4SRC IL_V4DST IL_V4OFF IL_V4V IL_V4HL IL_V4TTL
185 %token  IL_V4TOS IL_V4SUM IL_V4LEN IL_V4OPT IL_V4ID
186 %token  IL_TCP IL_SPORT IL_DPORT IL_TCPFL IL_TCPSEQ IL_TCPACK IL_TCPOFF
187 %token  IL_TCPWIN IL_TCPSUM IL_TCPURP IL_TCPOPT IL_TCPO_NOP IL_TCPO_EOL
188 %token  IL_TCPO_MSS IL_TCPO_WSCALE IL_TCPO_TS
189 %token  IL_UDP IL_UDPLEN IL_UDPSUM
190 %token  IL_ICMP IL_ICMPTYPE IL_ICMPCODE
191 %token  IL_SEND IL_VIA
192 %token  IL_ARP
193 %token  IL_DEFROUTER
194 %token  IL_SUM IL_OFF IL_LEN IL_V4ADDR IL_OPT
195 %token  IL_DATA IL_DLEN IL_DVALUE IL_DFILE
196 %token  IL_IPO_NOP IL_IPO_RR IL_IPO_ZSU IL_IPO_MTUP IL_IPO_MTUR IL_IPO_EOL
197 %token  IL_IPO_TS IL_IPO_TR IL_IPO_SEC IL_IPO_LSRR IL_IPO_ESEC
198 %token  IL_IPO_SATID IL_IPO_SSRR IL_IPO_ADDEXT IL_IPO_VISA IL_IPO_IMITD
199 %token  IL_IPO_EIP IL_IPO_FINN IL_IPO_SECCLASS IL_IPO_CIPSO IL_IPO_ENCODE
200 %token  <str> IL_IPS_RESERV4 IL_IPS_TOPSECRET IL_IPS_SECRET IL_IPS_RESERV3
201 %token  <str> IL_IPS_CONFID IL_IPS_UNCLASS IL_IPS_RESERV2 IL_IPS_RESERV1
202 %token  IL_ICMP_ECHOREPLY IL_ICMP_UNREACH IL_ICMP_UNREACH_NET
203 %token  IL_ICMP_UNREACH_HOST IL_ICMP_UNREACH_PROTOCOL IL_ICMP_UNREACH_PORT
204 %token  IL_ICMP_UNREACH_NEEDFRAG IL_ICMP_UNREACH_SRCFAIL
205 %token  IL_ICMP_UNREACH_NET_UNKNOWN IL_ICMP_UNREACH_HOST_UNKNOWN
206 %token  IL_ICMP_UNREACH_ISOLATED IL_ICMP_UNREACH_NET_PROHIB
207 %token  IL_ICMP_UNREACH_HOST_PROHIB IL_ICMP_UNREACH_TOSNET
208 %token  IL_ICMP_UNREACH_TOSHOST IL_ICMP_UNREACH_FILTER_PROHIB
209 %token  IL_ICMP_UNREACH_HOST_PRECEDENCE IL_ICMP_UNREACH_PRECEDENCE_CUTOFF
210 %token  IL_ICMP_SOURCEQUENCH IL_ICMP_REDIRECT IL_ICMP_REDIRECT_NET
211 %token  IL_ICMP_REDIRECT_HOST IL_ICMP_REDIRECT_TOSNET
212 %token  IL_ICMP_REDIRECT_TOSHOST IL_ICMP_ECHO IL_ICMP_ROUTERADVERT
213 %token  IL_ICMP_ROUTERSOLICIT IL_ICMP_TIMXCEED IL_ICMP_TIMXCEED_INTRANS
214 %token  IL_ICMP_TIMXCEED_REASS IL_ICMP_PARAMPROB IL_ICMP_PARAMPROB_OPTABSENT
215 %token  IL_ICMP_TSTAMP IL_ICMP_TSTAMPREPLY IL_ICMP_IREQ IL_ICMP_IREQREPLY
216 %token  IL_ICMP_MASKREQ IL_ICMP_MASKREPLY IL_ICMP_SEQ IL_ICMP_ID
217 %token  IL_ICMP_OTIME IL_ICMP_RTIME IL_ICMP_TTIME
218
219 %%
220 file:   line
221         | line file
222         | IL_COMMENT
223         | IL_COMMENT file
224         ;
225
226 line:   iface
227         | arp
228         | send
229         | defrouter
230         | ipline
231         ;
232
233 iface:  ifhdr '{' ifaceopts '}' ';'     { check_interface(); }
234         ;
235
236 ifhdr:  IL_INTERFACE                    { new_interface(); }
237         ;
238
239 ifaceopts:
240         ifaceopt
241         | ifaceopt ifaceopts
242         ;
243
244 ifaceopt:
245         IL_IFNAME token                 { set_ifname(&$2); }
246         | IL_MTU number                 { set_ifmtu($2); }
247         | IL_V4ADDR token               { set_ifv4addr(&$2); }
248         | IL_EADDR token                { set_ifeaddr(&$2); }
249         ;
250
251 send:   sendhdr '{' sendbody '}' ';'    { packet_done(); }
252         | sendhdr ';'                   { packet_done(); }
253         ;
254
255 sendhdr:
256         IL_SEND                         { reset_send(); }
257         ;
258
259 sendbody:
260         sendopt
261         | sendbody sendopt
262         ;
263
264 sendopt:
265         IL_IFNAME token                 { set_sendif(&$2); }
266         | IL_VIA token                  { set_sendvia(&$2); }
267         ;
268
269 arp:    arphdr '{' arpbody '}' ';'
270         ;
271
272 arphdr: IL_ARP                          { new_arp(); }
273         ;
274
275 arpbody:
276         arpopt
277         | arpbody arpopt
278         ;
279
280 arpopt: IL_V4ADDR token                 { set_arpv4addr(&$2); }
281         | IL_EADDR token                { set_arpeaddr(&$2); }
282         ;
283
284 defrouter:
285         IL_DEFROUTER token              { set_defaultrouter(&$2); }
286         ;
287
288 bodyline:
289         ipline
290         | tcp tcpline
291         | udp udpline
292         | icmp icmpline
293         | data dataline
294         ;
295
296 ipline: ipv4 '{' ipv4body '}' ';'       { end_ipv4(); }
297         ;
298
299 ipv4:   IL_IPV4                         { new_packet(); }
300
301 ipv4body:
302         ipv4type
303         | ipv4type ipv4body
304         | bodyline
305         ;
306
307 ipv4type:
308         IL_V4PROTO token                { set_ipv4proto(&$2); }
309         | IL_V4SRC token                { set_ipv4src(&$2); }
310         | IL_V4DST token                { set_ipv4dst(&$2); }
311         | IL_V4OFF token                { set_ipv4off(&$2); }
312         | IL_V4V token                  { set_ipv4v(&$2); }
313         | IL_V4HL token                 { set_ipv4hl(&$2); }
314         | IL_V4ID token                 { set_ipv4id(&$2); }
315         | IL_V4TTL token                { set_ipv4ttl(&$2); }
316         | IL_V4TOS token                { set_ipv4tos(&$2); }
317         | IL_V4SUM token                { set_ipv4sum(&$2); }
318         | IL_V4LEN token                { set_ipv4len(&$2); }
319         | ipv4opt '{' ipv4optlist '}' ';'       { end_ipopt(); }
320         ;
321
322 tcp:    IL_TCP                          { new_tcpheader(); }
323         ;
324
325 tcpline:
326         '{' tcpheader '}' ';'           { end_tcp(); }
327         ;
328
329 tcpheader:
330         tcpbody
331         | tcpbody tcpheader
332         | bodyline
333         ;
334
335 tcpbody:
336         IL_SPORT token                  { set_tcpsport(&$2); }
337         | IL_DPORT token                { set_tcpdport(&$2); }
338         | IL_TCPSEQ token               { set_tcpseq(&$2); }
339         | IL_TCPACK token               { set_tcpack(&$2); }
340         | IL_TCPOFF token               { set_tcpoff(&$2); }
341         | IL_TCPURP token               { set_tcpurp(&$2); }
342         | IL_TCPWIN token               { set_tcpwin(&$2); }
343         | IL_TCPSUM token               { set_tcpsum(&$2); }
344         | IL_TCPFL token                { set_tcpflags(&$2); }
345         | IL_TCPOPT '{' tcpopts '}' ';' { end_tcpopt(); }
346         ;
347
348 tcpopts:
349         | tcpopt tcpopts
350         ;
351
352 tcpopt: IL_TCPO_NOP ';'                 { set_tcpopt(IL_TCPO_NOP, NULL); }
353         | IL_TCPO_EOL ';'               { set_tcpopt(IL_TCPO_EOL, NULL); }
354         | IL_TCPO_MSS optoken           { set_tcpopt(IL_TCPO_MSS,&$2);}
355         | IL_TCPO_WSCALE optoken        { set_tcpopt(IL_TCPO_WSCALE,&$2);}
356         | IL_TCPO_TS optoken            { set_tcpopt(IL_TCPO_TS, &$2);}
357         ;
358
359 udp:    IL_UDP                          { new_udpheader(); }
360         ;
361
362 udpline:
363         '{' udpheader '}' ';'           { end_udp(); }
364         ;
365
366
367 udpheader:
368         udpbody
369         | udpbody udpheader
370         | bodyline
371         ;
372
373 udpbody:
374         IL_SPORT token                  { set_tcpsport(&$2); }
375         | IL_DPORT token                { set_tcpdport(&$2); }
376         | IL_UDPLEN token               { set_udplen(&$2); }
377         | IL_UDPSUM token               { set_udpsum(&$2); }
378         ;
379
380 icmp:   IL_ICMP                         { new_icmpheader(); }
381         ;
382
383 icmpline:
384         '{' icmpbody '}' ';'            { end_icmp(); }
385         ;
386
387 icmpbody:
388         icmpheader
389         | icmpheader bodyline
390         ;
391
392 icmpheader:
393         IL_ICMPTYPE icmptype
394         | IL_ICMPTYPE icmptype icmpcode
395         ;
396
397 icmpcode:
398         IL_ICMPCODE token               { set_icmpcodetok(&$2); }
399         ;
400
401 icmptype:
402         IL_ICMP_ECHOREPLY ';'           { set_icmptype(ICMP_ECHOREPLY); }
403         | IL_ICMP_ECHOREPLY '{' icmpechoopts '}' ';'
404         | unreach
405         | IL_ICMP_SOURCEQUENCH ';'      { set_icmptype(ICMP_SOURCEQUENCH); }
406         | redirect
407         | IL_ICMP_ROUTERADVERT ';'      { set_icmptype(ICMP_ROUTERADVERT); }
408         | IL_ICMP_ROUTERSOLICIT ';'     { set_icmptype(ICMP_ROUTERSOLICIT); }
409         | IL_ICMP_ECHO ';'              { set_icmptype(ICMP_ECHO); }
410         | IL_ICMP_ECHO '{' icmpechoopts '}' ';'
411         | IL_ICMP_TIMXCEED ';'          { set_icmptype(ICMP_TIMXCEED); }
412         | IL_ICMP_TIMXCEED '{' exceed '}' ';'
413         | IL_ICMP_TSTAMP ';'            { set_icmptype(ICMP_TSTAMP); }
414         | IL_ICMP_TSTAMPREPLY ';'       { set_icmptype(ICMP_TSTAMPREPLY); }
415         | IL_ICMP_TSTAMPREPLY '{' icmptsopts '}' ';'
416         | IL_ICMP_IREQ ';'              { set_icmptype(ICMP_IREQ); }
417         | IL_ICMP_IREQREPLY ';'         { set_icmptype(ICMP_IREQREPLY); }
418         | IL_ICMP_IREQREPLY '{' data dataline '}' ';'
419         | IL_ICMP_MASKREQ ';'           { set_icmptype(ICMP_MASKREQ); }
420         | IL_ICMP_MASKREPLY ';'         { set_icmptype(ICMP_MASKREPLY); }
421         | IL_ICMP_MASKREPLY '{' token '}' ';'
422         | IL_ICMP_PARAMPROB ';'         { set_icmptype(ICMP_PARAMPROB); }
423         | IL_ICMP_PARAMPROB '{' paramprob '}' ';'
424         | IL_TOKEN ';'                  { set_icmptypetok(&$1); }
425         ;
426
427 icmpechoopts:
428         | icmpechoopts icmpecho
429         ;
430
431 icmpecho:
432         IL_ICMP_SEQ number              { set_icmpseq($2); }
433         | IL_ICMP_ID number             { set_icmpid($2); }
434         ;
435
436 icmptsopts:
437         | icmptsopts icmpts ';'
438         ;
439
440 icmpts: IL_ICMP_OTIME number            { set_icmpotime($2); }
441         | IL_ICMP_RTIME number          { set_icmprtime($2); }
442         | IL_ICMP_TTIME number          { set_icmpttime($2); }
443         ;
444
445 unreach:
446         IL_ICMP_UNREACH
447         | IL_ICMP_UNREACH '{' unreachopts '}' ';'
448         ;
449
450 unreachopts:
451         IL_ICMP_UNREACH_NET line
452         | IL_ICMP_UNREACH_HOST line
453         | IL_ICMP_UNREACH_PROTOCOL line
454         | IL_ICMP_UNREACH_PORT line
455         | IL_ICMP_UNREACH_NEEDFRAG number ';'   { set_icmpmtu($2); }
456         | IL_ICMP_UNREACH_SRCFAIL line
457         | IL_ICMP_UNREACH_NET_UNKNOWN line
458         | IL_ICMP_UNREACH_HOST_UNKNOWN line
459         | IL_ICMP_UNREACH_ISOLATED line
460         | IL_ICMP_UNREACH_NET_PROHIB line
461         | IL_ICMP_UNREACH_HOST_PROHIB line
462         | IL_ICMP_UNREACH_TOSNET line
463         | IL_ICMP_UNREACH_TOSHOST line
464         | IL_ICMP_UNREACH_FILTER_PROHIB line
465         | IL_ICMP_UNREACH_HOST_PRECEDENCE line
466         | IL_ICMP_UNREACH_PRECEDENCE_CUTOFF line
467         ;
468
469 redirect:
470         IL_ICMP_REDIRECT
471         | IL_ICMP_REDIRECT '{' redirectopts '}' ';'
472         ;
473
474 redirectopts:
475         | IL_ICMP_REDIRECT_NET token            { set_redir(0, &$2); }
476         | IL_ICMP_REDIRECT_HOST token           { set_redir(1, &$2); }
477         | IL_ICMP_REDIRECT_TOSNET token         { set_redir(2, &$2); }
478         | IL_ICMP_REDIRECT_TOSHOST token        { set_redir(3, &$2); }
479         ;
480
481 exceed:
482         IL_ICMP_TIMXCEED_INTRANS line
483         | IL_ICMP_TIMXCEED_REASS line
484         ;
485
486 paramprob:
487         IL_ICMP_PARAMPROB_OPTABSENT
488         | IL_ICMP_PARAMPROB_OPTABSENT paraprobarg
489
490 paraprobarg:
491         '{' number '}' ';'              { set_icmppprob($2); }
492         ;
493
494 ipv4opt:        IL_V4OPT                { new_ipv4opt(); }
495         ;
496
497 ipv4optlist:
498         | ipv4opts ipv4optlist
499         ;
500
501 ipv4opts:
502         IL_IPO_NOP ';'                  { add_ipopt(IL_IPO_NOP, NULL); }
503         | IL_IPO_RR optnumber           { add_ipopt(IL_IPO_RR, &$2); }
504         | IL_IPO_ZSU ';'                { add_ipopt(IL_IPO_ZSU, NULL); }
505         | IL_IPO_MTUP ';'               { add_ipopt(IL_IPO_MTUP, NULL); }
506         | IL_IPO_MTUR ';'               { add_ipopt(IL_IPO_MTUR, NULL); }
507         | IL_IPO_ENCODE ';'             { add_ipopt(IL_IPO_ENCODE, NULL); }
508         | IL_IPO_TS ';'                 { add_ipopt(IL_IPO_TS, NULL); }
509         | IL_IPO_TR ';'                 { add_ipopt(IL_IPO_TR, NULL); }
510         | IL_IPO_SEC ';'                { add_ipopt(IL_IPO_SEC, NULL); }
511         | IL_IPO_SECCLASS secclass      { add_ipopt(IL_IPO_SECCLASS, sclass); }
512         | IL_IPO_LSRR token             { add_ipopt(IL_IPO_LSRR,&$2); }
513         | IL_IPO_ESEC ';'               { add_ipopt(IL_IPO_ESEC, NULL); }
514         | IL_IPO_CIPSO ';'              { add_ipopt(IL_IPO_CIPSO, NULL); }
515         | IL_IPO_SATID optnumber        { add_ipopt(IL_IPO_SATID,&$2);}
516         | IL_IPO_SSRR token             { add_ipopt(IL_IPO_SSRR,&$2); }
517         | IL_IPO_ADDEXT ';'             { add_ipopt(IL_IPO_ADDEXT, NULL); }
518         | IL_IPO_VISA ';'               { add_ipopt(IL_IPO_VISA, NULL); }
519         | IL_IPO_IMITD ';'              { add_ipopt(IL_IPO_IMITD, NULL); }
520         | IL_IPO_EIP ';'                { add_ipopt(IL_IPO_EIP, NULL); }
521         | IL_IPO_FINN ';'               { add_ipopt(IL_IPO_FINN, NULL); }
522         ;
523
524 secclass:
525         IL_IPS_RESERV4 ';'              { set_secclass(&$1); }
526         | IL_IPS_TOPSECRET ';'          { set_secclass(&$1); }
527         | IL_IPS_SECRET ';'             { set_secclass(&$1); }
528         | IL_IPS_RESERV3 ';'            { set_secclass(&$1); }
529         | IL_IPS_CONFID ';'             { set_secclass(&$1); }
530         | IL_IPS_UNCLASS ';'            { set_secclass(&$1); }
531         | IL_IPS_RESERV2 ';'            { set_secclass(&$1); }
532         | IL_IPS_RESERV1 ';'            { set_secclass(&$1); }
533         ;
534
535 data:   IL_DATA                         { new_data(); }
536         ;
537
538 dataline:
539         '{' databody '}' ';'            { end_data(); }
540         ;
541
542 databody: dataopts
543         | dataopts databody
544         ;
545
546 dataopts:
547         IL_DLEN token                   { set_datalen(&$2); }
548         | IL_DVALUE token               { set_data(&$2); }
549         | IL_DFILE token                { set_datafile(&$2); }
550         ;
551
552 token: IL_TOKEN ';'
553         ;
554
555 optoken: ';'                            { $$ = ""; }
556         | token
557         ;
558
559 number: digits ';'
560         ;
561
562 optnumber: ';'                          { $$ = 0; }
563         | number
564         ;
565
566 digits: IL_NUMBER
567         | digits IL_NUMBER
568         ;
569 %%
570
571 struct  statetoopt      toipopts[] = {
572         { IL_IPO_NOP,           IPOPT_NOP },
573         { IL_IPO_RR,            IPOPT_RR },
574         { IL_IPO_ZSU,           IPOPT_ZSU },
575         { IL_IPO_MTUP,          IPOPT_MTUP },
576         { IL_IPO_MTUR,          IPOPT_MTUR },
577         { IL_IPO_ENCODE,        IPOPT_ENCODE },
578         { IL_IPO_TS,            IPOPT_TS },
579         { IL_IPO_TR,            IPOPT_TR },
580         { IL_IPO_SEC,           IPOPT_SECURITY },
581         { IL_IPO_SECCLASS,      IPOPT_SECURITY },
582         { IL_IPO_LSRR,          IPOPT_LSRR },
583         { IL_IPO_ESEC,          IPOPT_E_SEC },
584         { IL_IPO_CIPSO,         IPOPT_CIPSO },
585         { IL_IPO_SATID,         IPOPT_SATID },
586         { IL_IPO_SSRR,          IPOPT_SSRR },
587         { IL_IPO_ADDEXT,        IPOPT_ADDEXT },
588         { IL_IPO_VISA,          IPOPT_VISA },
589         { IL_IPO_IMITD,         IPOPT_IMITD },
590         { IL_IPO_EIP,           IPOPT_EIP },
591         { IL_IPO_FINN,          IPOPT_FINN },
592         { 0, 0 }
593 };
594
595 struct  statetoopt      tosecopts[] = {
596         { IL_IPS_RESERV4,       IPSO_CLASS_RES4 },
597         { IL_IPS_TOPSECRET,     IPSO_CLASS_TOPS },
598         { IL_IPS_SECRET,        IPSO_CLASS_SECR },
599         { IL_IPS_RESERV3,       IPSO_CLASS_RES3 },
600         { IL_IPS_CONFID,        IPSO_CLASS_CONF },
601         { IL_IPS_UNCLASS,       IPSO_CLASS_UNCL },
602         { IL_IPS_RESERV2,       IPSO_CLASS_RES2 },
603         { IL_IPS_RESERV1,       IPSO_CLASS_RES1 },
604         { 0, 0 }
605 };
606
607 #ifdef  bsdi
608 struct ether_addr *
609 ether_aton(s)
610         char *s;   
611 {
612         static struct ether_addr n;
613         u_int i[6];
614
615         if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
616             &i[2], &i[3], &i[4], &i[5]) == 6) {
617                 n.ether_addr_octet[0] = (u_char)i[0];
618                 n.ether_addr_octet[1] = (u_char)i[1];
619                 n.ether_addr_octet[2] = (u_char)i[2];
620                 n.ether_addr_octet[3] = (u_char)i[3];
621                 n.ether_addr_octet[4] = (u_char)i[4];
622                 n.ether_addr_octet[5] = (u_char)i[5];
623                 return &n;
624         }
625         return NULL;
626 }
627 #endif
628
629
630 struct in_addr getipv4addr(arg)
631 char *arg;
632 {
633         struct hostent *hp;
634         struct in_addr in;
635
636         in.s_addr = 0xffffffff;
637
638         if ((hp = gethostbyname(arg)))
639                 bcopy(hp->h_addr, &in.s_addr, sizeof(struct in_addr));
640         else
641                 in.s_addr = inet_addr(arg);
642         return in;
643 }
644
645
646 u_short getportnum(pr, name)
647 char *pr, *name;
648 {
649         struct servent *sp;
650
651         if (!(sp = getservbyname(name, pr)))
652                 return htons(atoi(name));
653         return sp->s_port;
654 }
655
656
657 struct ether_addr *geteaddr(arg, buf)
658 char *arg;
659 struct ether_addr *buf;
660 {
661         struct ether_addr *e;
662
663 #if !defined(hpux) && !defined(linux)
664         e = ether_aton(arg);
665         if (!e)
666                 fprintf(stderr, "Invalid ethernet address: %s\n", arg);
667         else
668 # ifdef __FreeBSD__
669                 bcopy(e->octet, buf->octet, sizeof(e->octet));
670 # else
671                 bcopy(e->ether_addr_octet, buf->ether_addr_octet,
672                       sizeof(e->ether_addr_octet));
673 # endif
674         return e;
675 #else
676         return NULL;
677 #endif
678 }
679
680
681 void *new_header(type)
682 int type;
683 {
684         aniphdr_t *aip, *oip = canip;
685         int     sz = 0;
686
687         aip = (aniphdr_t *)calloc(1, sizeof(*aip));
688         *aniptail = aip;
689         aniptail = &aip->ah_next;
690         aip->ah_p = type;
691         aip->ah_prev = oip;
692         canip = aip;
693
694         if (type == IPPROTO_UDP)
695                 sz = sizeof(udphdr_t);
696         else if (type == IPPROTO_TCP)
697                 sz = sizeof(tcphdr_t);
698         else if (type == IPPROTO_ICMP)
699                 sz = sizeof(icmphdr_t);
700         else if (type == IPPROTO_IP)
701                 sz = sizeof(ip_t);
702
703         if (oip)
704                 canip->ah_data = oip->ah_data + oip->ah_len;
705         else
706                 canip->ah_data = (char *)ipbuffer;
707
708         /*
709          * Increase the size fields in all wrapping headers.
710          */
711         for (aip = aniphead; aip; aip = aip->ah_next) {
712                 aip->ah_len += sz;
713                 if (aip->ah_p == IPPROTO_IP)
714                         aip->ah_ip->ip_len += sz;
715                 else if (aip->ah_p == IPPROTO_UDP)
716                         aip->ah_udp->uh_ulen += sz;
717         }
718         return (void *)canip->ah_data;
719 }
720
721
722 void free_aniplist()
723 {
724         aniphdr_t *aip, **aipp = &aniphead;
725
726         while ((aip = *aipp)) {
727                 *aipp = aip->ah_next;
728                 free(aip);
729         }
730         aniptail = &aniphead;
731 }
732
733
734 void inc_anipheaders(inc)
735 int inc;
736 {
737         aniphdr_t *aip;
738
739         for (aip = aniphead; aip; aip = aip->ah_next) {
740                 aip->ah_len += inc;
741                 if (aip->ah_p == IPPROTO_IP)
742                         aip->ah_ip->ip_len += inc;
743                 else if (aip->ah_p == IPPROTO_UDP)
744                         aip->ah_udp->uh_ulen += inc;
745         }
746 }
747
748
749 void new_data()
750 {
751         (void) new_header(-1);
752         canip->ah_len = 0;
753 }
754
755
756 void set_datalen(arg)
757 char **arg;
758 {
759         int     len;
760
761         len = strtol(*arg, NULL, 0);
762         inc_anipheaders(len);
763         free(*arg);
764         *arg = NULL;
765 }
766
767
768 void set_data(arg)
769 char **arg;
770 {
771         u_char *s = (u_char *)*arg, *t = (u_char *)canip->ah_data, c;
772         int len = 0, todo = 0, quote = 0, val = 0;
773
774         while ((c = *s++)) {
775                 if (todo) {
776                         if (isdigit(c)) {
777                                 todo--;
778                                 if (c > '7') {
779                                         fprintf(stderr, "octal with %c!\n", c);
780                                         break;
781                                 }
782                                 val <<= 3;
783                                 val |= (c - '0');
784                         }
785                         if (!isdigit(c) || !todo) {
786                                 *t++ = (u_char)(val & 0xff);
787                                 todo = 0;
788                         }
789                         if (todo)
790                                 continue;
791                 }
792                 if (quote) {
793                         if (isdigit(c)) {
794                                 todo = 2;
795                                 if (c > '7') {
796                                         fprintf(stderr, "octal with %c!\n", c);
797                                         break;
798                                 }
799                                 val = (c - '0');
800                         } else {
801                                 switch (c)
802                                 {
803                                 case '\"' :
804                                         *t++ = '\"';
805                                         break;
806                                 case '\\' :
807                                         *t++ = '\\';
808                                         break;
809                                 case 'n' :
810                                         *t++ = '\n';
811                                         break;
812                                 case 'r' :
813                                         *t++ = '\r';
814                                         break;
815                                 case 't' :
816                                         *t++ = '\t';
817                                         break;
818                                 }
819                         }
820                         quote = 0;
821                         continue;
822                 }
823
824                 if (c == '\\')
825                         quote = 1;
826                 else
827                         *t++ = c;
828         }
829         if (todo)
830                 *t++ = (u_char)(val & 0xff);
831         if (quote)
832                 *t++ = '\\';
833         len = t - (u_char *)canip->ah_data;
834         inc_anipheaders(len - canip->ah_len);
835         canip->ah_len = len;
836 }
837
838
839 void set_datafile(arg)
840 char **arg;
841 {
842         struct stat sb;
843         char *file = *arg;
844         int fd, len;
845
846         if ((fd = open(file, O_RDONLY)) == -1) {
847                 perror("open");
848                 exit(-1);
849         }
850
851         if (fstat(fd, &sb) == -1) {
852                 perror("fstat");
853                 exit(-1);
854         }
855
856         if ((sb.st_size + aniphead->ah_len ) > 65535) {
857                 fprintf(stderr, "data file %s too big to include.\n", file);
858                 close(fd);
859                 return;
860         }
861         if ((len = read(fd, canip->ah_data, sb.st_size)) == -1) {
862                 perror("read");
863                 close(fd);
864                 return;
865         }
866         inc_anipheaders(len);
867         canip->ah_len += len;
868         close(fd);
869 }
870
871
872 void new_packet()
873 {
874         static  u_short id = 0;
875
876         if (!aniphead)
877                 bzero((char *)ipbuffer, sizeof(ipbuffer));
878
879         ip = (ip_t *)new_header(IPPROTO_IP);
880         ip->ip_v = IPVERSION;
881         ip->ip_hl = sizeof(ip_t) >> 2;
882         ip->ip_len = sizeof(ip_t);
883         ip->ip_ttl = 63;
884         ip->ip_id = htons(id++);
885 }
886
887
888 void set_ipv4proto(arg)
889 char **arg;
890 {
891         struct protoent *pr;
892
893         if ((pr = getprotobyname(*arg)))
894                 ip->ip_p = pr->p_proto;
895         else
896                 if (!(ip->ip_p = atoi(*arg)))
897                         fprintf(stderr, "unknown protocol %s\n", *arg);
898         free(*arg);
899         *arg = NULL;
900 }
901
902
903 void set_ipv4src(arg)
904 char **arg;
905 {
906         ip->ip_src = getipv4addr(*arg);
907         free(*arg);
908         *arg = NULL;
909 }
910
911
912 void set_ipv4dst(arg)
913 char **arg;
914 {
915         ip->ip_dst = getipv4addr(*arg);
916         free(*arg);
917         *arg = NULL;
918 }
919
920
921 void set_ipv4off(arg)
922 char **arg;
923 {
924         ip->ip_off = htons(strtol(*arg, NULL, 0));
925         free(*arg);
926         *arg = NULL;
927 }
928
929
930 void set_ipv4v(arg)
931 char **arg;
932 {
933         ip->ip_v = strtol(*arg, NULL, 0);
934         free(*arg);
935         *arg = NULL;
936 }
937
938
939 void set_ipv4hl(arg)
940 char **arg;
941 {
942         int newhl, inc;
943
944         newhl = strtol(*arg, NULL, 0);
945         inc = (newhl - ip->ip_hl) << 2;
946         ip->ip_len += inc;
947         ip->ip_hl = newhl;
948         canip->ah_len += inc;
949         free(*arg);
950         *arg = NULL;
951 }
952
953
954 void set_ipv4ttl(arg)
955 char **arg;
956 {
957         ip->ip_ttl = strtol(*arg, NULL, 0);
958         free(*arg);
959         *arg = NULL;
960 }
961
962
963 void set_ipv4tos(arg)
964 char **arg;
965 {
966         ip->ip_tos = strtol(*arg, NULL, 0);
967         free(*arg);
968         *arg = NULL;
969 }
970
971
972 void set_ipv4id(arg)
973 char **arg;
974 {
975         ip->ip_id = htons(strtol(*arg, NULL, 0));
976         free(*arg);
977         *arg = NULL;
978 }
979
980
981 void set_ipv4sum(arg)
982 char **arg;
983 {
984         ip->ip_sum = strtol(*arg, NULL, 0);
985         free(*arg);
986         *arg = NULL;
987 }
988
989
990 void set_ipv4len(arg)
991 char **arg;
992 {
993         int len;
994
995         len = strtol(*arg, NULL, 0);
996         inc_anipheaders(len - ip->ip_len);
997         ip->ip_len = len;
998         free(*arg);
999         *arg = NULL;
1000 }
1001
1002
1003 void new_tcpheader()
1004 {
1005
1006         if ((ip->ip_p) && (ip->ip_p != IPPROTO_TCP)) {
1007                 fprintf(stderr, "protocol %d specified with TCP!\n", ip->ip_p);
1008                 return;
1009         }
1010         ip->ip_p = IPPROTO_TCP;
1011
1012         tcp = (tcphdr_t *)new_header(IPPROTO_TCP);
1013         tcp->th_win = htons(4096);
1014         tcp->th_off = sizeof(*tcp) >> 2;
1015 }
1016
1017
1018 void set_tcpsport(arg)
1019 char **arg;
1020 {
1021         u_short *port;
1022         char *pr;
1023
1024         if (ip->ip_p == IPPROTO_UDP) {
1025                 port = &udp->uh_sport;
1026                 pr = "udp";
1027         } else {
1028                 port = &tcp->th_sport;
1029                 pr = "udp";
1030         }
1031
1032         *port = getportnum(pr, *arg);
1033         free(*arg);
1034         *arg = NULL;
1035 }
1036
1037
1038 void set_tcpdport(arg)
1039 char **arg;
1040 {
1041         u_short *port;
1042         char *pr;
1043
1044         if (ip->ip_p == IPPROTO_UDP) {
1045                 port = &udp->uh_dport;
1046                 pr = "udp";
1047         } else {
1048                 port = &tcp->th_dport;
1049                 pr = "udp";
1050         }
1051
1052         *port = getportnum(pr, *arg);
1053         free(*arg);
1054         *arg = NULL;
1055 }
1056
1057
1058 void set_tcpseq(arg)
1059 char **arg;
1060 {
1061         tcp->th_seq = htonl(strtol(*arg, NULL, 0));
1062         free(*arg);
1063         *arg = NULL;
1064 }
1065
1066
1067 void set_tcpack(arg)
1068 char **arg;
1069 {
1070         tcp->th_ack = htonl(strtol(*arg, NULL, 0));
1071         free(*arg);
1072         *arg = NULL;
1073 }
1074
1075
1076 void set_tcpoff(arg)
1077 char **arg;
1078 {
1079         int     off;
1080
1081         off = strtol(*arg, NULL, 0);
1082         inc_anipheaders((off - tcp->th_off) << 2);
1083         tcp->th_off = off;
1084         free(*arg);
1085         *arg = NULL;
1086 }
1087
1088
1089 void set_tcpurp(arg)
1090 char **arg;
1091 {
1092         tcp->th_urp = htons(strtol(*arg, NULL, 0));
1093         free(*arg);
1094         *arg = NULL;
1095 }
1096
1097
1098 void set_tcpwin(arg)
1099 char **arg;
1100 {
1101         tcp->th_win = htons(strtol(*arg, NULL, 0));
1102         free(*arg);
1103         *arg = NULL;
1104 }
1105
1106
1107 void set_tcpsum(arg)
1108 char **arg;
1109 {
1110         tcp->th_sum = strtol(*arg, NULL, 0);
1111         free(*arg);
1112         *arg = NULL;
1113 }
1114
1115
1116 void set_tcpflags(arg)
1117 char **arg;
1118 {
1119         static  char    flags[] = "ASURPF";
1120         static  int     flagv[] = { TH_ACK, TH_SYN, TH_URG, TH_RST, TH_PUSH,
1121                                     TH_FIN } ;
1122         char *s, *t;
1123
1124         for (s = *arg; *s; s++)
1125                 if (!(t = strchr(flags, *s))) {
1126                         if (s - *arg) {
1127                                 fprintf(stderr, "unknown TCP flag %c\n", *s);
1128                                 break;
1129                         }
1130                         tcp->th_flags = strtol(*arg, NULL, 0);
1131                         break;
1132                 } else
1133                         tcp->th_flags |= flagv[t - flags];
1134         free(*arg);
1135         *arg = NULL;
1136 }
1137
1138
1139 void set_tcpopt(state, arg)
1140 int state;
1141 char **arg;
1142 {
1143         u_char *s;
1144         int val, len, val2, pad, optval;
1145
1146         if (arg && *arg)
1147                 val = atoi(*arg);
1148         else
1149                 val = 0;
1150
1151         s = (u_char *)tcp + sizeof(*tcp) + canip->ah_optlen;
1152         switch (state)
1153         {
1154         case IL_TCPO_EOL :
1155                 optval = 0;
1156                 len = 1;
1157                 break;
1158         case IL_TCPO_NOP :
1159                 optval = 1;
1160                 len = 1;
1161                 break;
1162         case IL_TCPO_MSS :
1163                 optval = 2;
1164                 len = 4;
1165                 break;
1166         case IL_TCPO_WSCALE :
1167                 optval = 3;
1168                 len = 3;
1169                 break;
1170         case IL_TCPO_TS :
1171                 optval = 8;
1172                 len = 10;
1173                 break;
1174         default :
1175                 optval = 0;
1176                 len = 0;
1177                 break;
1178         }
1179
1180         if (len > 1) {
1181                 /*
1182                  * prepend padding - if required.
1183                  */
1184                 if (len & 3)
1185                         for (pad = 4 - (len & 3); pad; pad--) {
1186                                 *s++ = 1;
1187                                 canip->ah_optlen++;
1188                         }
1189                 /*
1190                  * build tcp option
1191                  */
1192                 *s++ = (u_char)optval;
1193                 *s++ = (u_char)len;
1194                 if (len > 2) {
1195                         if (len == 3) {         /* 1 byte - char */
1196                                 *s++ = (u_char)val;
1197                         } else if (len == 4) {  /* 2 bytes - short */
1198                                 *s++ = (u_char)((val >> 8) & 0xff);
1199                                 *s++ = (u_char)(val & 0xff);
1200                         } else if (len >= 6) {  /* 4 bytes - long */
1201                                 val2 = htonl(val);
1202                                 bcopy((char *)&val2, s, 4);
1203                         }
1204                         s += (len - 2);
1205                 }
1206         } else
1207                 *s++ = (u_char)optval;
1208
1209         canip->ah_lastopt = optval;
1210         canip->ah_optlen += len;
1211
1212         if (arg && *arg) {
1213                 free(*arg);
1214                 *arg = NULL;
1215         }
1216 }
1217
1218
1219 void end_tcpopt()
1220 {
1221         int pad;
1222         char *s = (char *)tcp;
1223
1224         s += sizeof(*tcp) + canip->ah_optlen;
1225         /*
1226          * pad out so that we have a multiple of 4 bytes in size fo the
1227          * options.  make sure last byte is EOL.
1228          */
1229         if (canip->ah_optlen & 3) {
1230                 if (canip->ah_lastopt != 1) {
1231                         for (pad = 3 - (canip->ah_optlen & 3); pad; pad--) {
1232                                 *s++ = 1;
1233                                 canip->ah_optlen++;
1234                         }
1235                         canip->ah_optlen++;
1236                 } else {
1237                         s -= 1;
1238
1239                         for (pad = 3 - (canip->ah_optlen & 3); pad; pad--) {
1240                                 *s++ = 1;
1241                                 canip->ah_optlen++;
1242                         }
1243                 }
1244                 *s++ = 0;
1245         }
1246         tcp->th_off = (sizeof(*tcp) + canip->ah_optlen) >> 2;
1247         inc_anipheaders(canip->ah_optlen);
1248 }
1249
1250
1251 void new_udpheader()
1252 {
1253         if ((ip->ip_p) && (ip->ip_p != IPPROTO_UDP)) {
1254                 fprintf(stderr, "protocol %d specified with UDP!\n", ip->ip_p);
1255                 return;
1256         }
1257         ip->ip_p = IPPROTO_UDP;
1258
1259         udp = (udphdr_t *)new_header(IPPROTO_UDP);
1260         udp->uh_ulen = sizeof(*udp);
1261 }
1262
1263
1264 void set_udplen(arg)
1265 char **arg;
1266 {
1267         int len;
1268
1269         len = strtol(*arg, NULL, 0);
1270         inc_anipheaders(len - udp->uh_ulen);
1271         udp->uh_ulen = len;
1272         free(*arg);
1273         *arg = NULL;
1274 }
1275
1276
1277 void set_udpsum(arg)
1278 char **arg;
1279 {
1280         udp->uh_sum = strtol(*arg, NULL, 0);
1281         free(*arg);
1282         *arg = NULL;
1283 }
1284
1285
1286 void prep_packet()
1287 {
1288         iface_t *ifp;
1289         struct in_addr gwip;
1290
1291         ifp = sending.snd_if;
1292         if (!ifp) {
1293                 fprintf(stderr, "no interface defined for sending!\n");
1294                 return;
1295         }
1296         if (ifp->if_fd == -1)
1297                 ifp->if_fd = initdevice(ifp->if_name, 0, 5);
1298         gwip = sending.snd_gw;
1299         if (!gwip.s_addr)
1300                 gwip = aniphead->ah_ip->ip_dst;
1301         (void) send_ip(ifp->if_fd, ifp->if_MTU, (ip_t *)ipbuffer, gwip, 2);
1302 }
1303
1304
1305 void packet_done()
1306 {
1307         char    outline[80];
1308         int     i, j, k;
1309         u_char  *s = (u_char *)ipbuffer, *t = (u_char *)outline;
1310
1311         if (opts & OPT_VERBOSE) {
1312                 ip->ip_len = htons(ip->ip_len);
1313                 for (i = ntohs(ip->ip_len), j = 0; i; i--, j++, s++) {
1314                         if (j && !(j & 0xf)) {
1315                                 *t++ = '\n';
1316                                 *t = '\0';
1317                                 fputs(outline, stdout);
1318                                 fflush(stdout);
1319                                 t = (u_char *)outline;
1320                                 *t = '\0';
1321                         }
1322                         sprintf((char *)t, "%02x", *s & 0xff);
1323                         t += 2;
1324                         if (!((j + 1) & 0xf)) {
1325                                 s -= 15;
1326                                 sprintf((char *)t, "    ");
1327                                 t += 8;
1328                                 for (k = 16; k; k--, s++)
1329                                         *t++ = (isprint(*s) ? *s : '.');
1330                                 s--;
1331                         }
1332
1333                         if ((j + 1) & 0xf)
1334                                 *t++ = ' ';;
1335                 }
1336
1337                 if (j & 0xf) {
1338                         for (k = 16 - (j & 0xf); k; k--) {
1339                                 *t++ = ' ';
1340                                 *t++ = ' ';
1341                                 *t++ = ' ';
1342                         }
1343                         sprintf((char *)t, "       ");
1344                         t += 7;
1345                         s -= j & 0xf;
1346                         for (k = j & 0xf; k; k--, s++)
1347                                 *t++ = (isprint(*s) ? *s : '.');
1348                         *t++ = '\n';
1349                         *t = '\0';
1350                 }
1351                 fputs(outline, stdout);
1352                 fflush(stdout);
1353                 ip->ip_len = ntohs(ip->ip_len);
1354         }
1355
1356         prep_packet();
1357         free_aniplist();
1358 }
1359
1360
1361 void new_interface()
1362 {
1363         cifp = (iface_t *)calloc(1, sizeof(iface_t));
1364         *iftail = cifp;
1365         iftail = &cifp->if_next;
1366         cifp->if_fd = -1;
1367 }
1368
1369
1370 void check_interface()
1371 {
1372         if (!cifp->if_name || !*cifp->if_name)
1373                 fprintf(stderr, "No interface name given!\n");
1374         if (!cifp->if_MTU || !*cifp->if_name)
1375                 fprintf(stderr, "Interface %s has an MTU of 0!\n",
1376                         cifp->if_name);
1377 }
1378
1379
1380 void set_ifname(arg)
1381 char **arg;
1382 {
1383         cifp->if_name = *arg;
1384         *arg = NULL;
1385 }
1386
1387
1388 void set_ifmtu(arg)
1389 int arg;
1390 {
1391         cifp->if_MTU = arg;
1392 }
1393
1394
1395 void set_ifv4addr(arg)
1396 char **arg;
1397 {
1398         cifp->if_addr = getipv4addr(*arg);
1399         free(*arg);
1400         *arg = NULL;
1401 }
1402
1403
1404 void set_ifeaddr(arg)
1405 char **arg;
1406 {
1407         (void) geteaddr(*arg, &cifp->if_eaddr);
1408         free(*arg);
1409         *arg = NULL;
1410 }
1411
1412
1413 void new_arp()
1414 {
1415         carp = (arp_t *)calloc(1, sizeof(arp_t));
1416         *arptail = carp;
1417         arptail = &carp->arp_next;
1418 }
1419
1420
1421 void set_arpeaddr(arg)
1422 char **arg;
1423 {
1424         (void) geteaddr(*arg, &carp->arp_eaddr);
1425         free(*arg);
1426         *arg = NULL;
1427 }
1428
1429
1430 void set_arpv4addr(arg)
1431 char **arg;
1432 {
1433         carp->arp_addr = getipv4addr(*arg);
1434         free(*arg);
1435         *arg = NULL;
1436 }
1437
1438
1439 int arp_getipv4(ip, addr)
1440 char *ip;
1441 char *addr;
1442 {
1443         arp_t *a;
1444
1445         for (a = arplist; a; a = a->arp_next)
1446                 if (!bcmp(ip, (char *)&a->arp_addr, 4)) {
1447                         bcopy((char *)&a->arp_eaddr, addr, 6);
1448                         return 0;
1449                 }
1450         return -1;
1451 }
1452
1453
1454 void reset_send()
1455 {
1456         sending.snd_if = iflist;
1457         sending.snd_gw = defrouter;
1458 }
1459
1460
1461 void set_sendif(arg)
1462 char **arg;
1463 {
1464         iface_t *ifp;
1465
1466         for (ifp = iflist; ifp; ifp = ifp->if_next)
1467                 if (ifp->if_name && !strcmp(ifp->if_name, *arg))
1468                         break;
1469         sending.snd_if = ifp;
1470         if (!ifp)
1471                 fprintf(stderr, "couldn't find interface %s\n", *arg);
1472         free(*arg);
1473         *arg = NULL;
1474 }
1475
1476
1477 void set_sendvia(arg)
1478 char **arg;
1479 {
1480         sending.snd_gw = getipv4addr(*arg);
1481         free(*arg);
1482         *arg = NULL;
1483 }
1484
1485
1486 void set_defaultrouter(arg)
1487 char **arg;
1488 {
1489         defrouter = getipv4addr(*arg);
1490         free(*arg);
1491         *arg = NULL;
1492 }
1493
1494
1495 void new_icmpheader()
1496 {
1497         if ((ip->ip_p) && (ip->ip_p != IPPROTO_ICMP)) {
1498                 fprintf(stderr, "protocol %d specified with ICMP!\n",
1499                         ip->ip_p);
1500                 return;
1501         }
1502         ip->ip_p = IPPROTO_ICMP;
1503         icmp = (icmphdr_t *)new_header(IPPROTO_ICMP);
1504 }
1505
1506
1507 void set_icmpcode(code)
1508 int code;
1509 {
1510         icmp->icmp_code = code;
1511 }
1512
1513
1514 void set_icmptype(type)
1515 int type;
1516 {
1517         icmp->icmp_type = type;
1518 }
1519
1520
1521 static  char    *icmpcodes[] = {
1522         "net-unr", "host-unr", "proto-unr", "port-unr", "needfrag", "srcfail",
1523         "net-unk", "host-unk", "isolate", "net-prohib", "host-prohib",
1524         "net-tos", "host-tos", NULL };
1525
1526 void set_icmpcodetok(code)
1527 char **code;
1528 {
1529         char    *s;
1530         int     i;
1531
1532         for (i = 0; (s = icmpcodes[i]); i++)
1533                 if (!strcmp(s, *code)) {
1534                         icmp->icmp_code = i;
1535                         break;
1536                 }
1537         if (!s)
1538                 fprintf(stderr, "unknown ICMP code %s\n", *code);
1539         free(*code);
1540         *code = NULL;
1541 }
1542
1543
1544 static  char    *icmptypes[] = {
1545         "echorep", (char *)NULL, (char *)NULL, "unreach", "squench",
1546         "redir", (char *)NULL, (char *)NULL, "echo", (char *)NULL,
1547         (char *)NULL, "timex", "paramprob", "timest", "timestrep",
1548         "inforeq", "inforep", "maskreq", "maskrep", "END"
1549 };
1550
1551 void set_icmptypetok(type)
1552 char **type;
1553 {
1554         char    *s;
1555         int     i, done = 0;
1556
1557         for (i = 0; !(s = icmptypes[i]) || strcmp(s, "END"); i++)
1558                 if (s && !strcmp(s, *type)) {
1559                         icmp->icmp_type = i;
1560                         done = 1;
1561                         break;
1562                 }
1563         if (!done)
1564                 fprintf(stderr, "unknown ICMP type %s\n", *type);
1565         free(*type);
1566         *type = NULL;
1567 }
1568
1569
1570 void set_icmpid(arg)
1571 int arg;
1572 {
1573         icmp->icmp_id = htons(arg);
1574 }
1575
1576
1577 void set_icmpseq(arg)
1578 int arg;
1579 {
1580         icmp->icmp_seq = htons(arg);
1581 }
1582
1583
1584 void set_icmpotime(arg)
1585 int arg;
1586 {
1587         icmp->icmp_otime = htonl(arg);
1588 }
1589
1590
1591 void set_icmprtime(arg)
1592 int arg;
1593 {
1594         icmp->icmp_rtime = htonl(arg);
1595 }
1596
1597
1598 void set_icmpttime(arg)
1599 int arg;
1600 {
1601         icmp->icmp_ttime = htonl(arg);
1602 }
1603
1604
1605 void set_icmpmtu(arg)
1606 int arg;
1607 {
1608 #if     BSD >= 199306
1609         icmp->icmp_nextmtu = htons(arg);
1610 #endif
1611 }
1612
1613
1614 void set_redir(redir, arg)
1615 int redir;
1616 char **arg;
1617 {
1618         icmp->icmp_code = redir;
1619         icmp->icmp_gwaddr = getipv4addr(*arg);
1620         free(*arg);
1621         *arg = NULL;
1622 }
1623
1624
1625 void set_icmppprob(num)
1626 int num;
1627 {
1628         icmp->icmp_pptr = num;
1629 }
1630
1631
1632 void new_ipv4opt()
1633 {
1634         new_header(-2);
1635 }
1636
1637
1638 void add_ipopt(state, ptr)
1639 int state;
1640 void *ptr;
1641 {
1642         struct ipopt_names *io;
1643         struct statetoopt *sto;
1644         char numbuf[16], *arg, **param = ptr;
1645         int inc, hlen;
1646
1647         if (state == IL_IPO_RR || state == IL_IPO_SATID) {
1648                 if (param)
1649                         sprintf(numbuf, "%d", *(int *)param);
1650                 else
1651                         strcpy(numbuf, "0");
1652                 arg = numbuf;
1653         } else
1654                 arg = param ? *param : NULL;
1655
1656         if (canip->ah_next) {
1657                 fprintf(stderr, "cannot specify options after data body\n");
1658                 return;
1659         }
1660         for (sto = toipopts; sto->sto_st; sto++)
1661                 if (sto->sto_st == state)
1662                         break;
1663         if (!sto || !sto->sto_st) {
1664                 fprintf(stderr, "No mapping for state %d to IP option\n",
1665                         state);
1666                 return;
1667         }
1668
1669         hlen = sizeof(ip_t) + canip->ah_optlen;
1670         for (io = ionames; io->on_name; io++)
1671                 if (io->on_value == sto->sto_op)
1672                         break;
1673         canip->ah_lastopt = io->on_value;
1674
1675         if (io->on_name) {
1676                 inc = addipopt((char *)ip + hlen, io, hlen - sizeof(ip_t),arg);
1677                 if (inc > 0) {
1678                         while (inc & 3) {
1679                                 ((char *)ip)[sizeof(*ip) + inc] = IPOPT_NOP;
1680                                 canip->ah_lastopt = IPOPT_NOP;
1681                                 inc++;
1682                         }
1683                         hlen += inc;
1684                 }
1685         }
1686
1687         canip->ah_optlen = hlen - sizeof(ip_t);
1688
1689         if (state != IL_IPO_RR && state != IL_IPO_SATID)
1690                 if (param && *param) {
1691                         free(*param);
1692                         *param = NULL;
1693                 }
1694         sclass = NULL;
1695 }
1696
1697
1698 void end_ipopt()
1699 {
1700         int pad;
1701         char *s, *buf = (char *)ip;
1702
1703         /*
1704          * pad out so that we have a multiple of 4 bytes in size fo the
1705          * options.  make sure last byte is EOL.
1706          */
1707         if (canip->ah_lastopt == IPOPT_NOP) {
1708                 buf[sizeof(*ip) + canip->ah_optlen - 1] = IPOPT_EOL;
1709         } else if (canip->ah_lastopt != IPOPT_EOL) {
1710                 s = buf + sizeof(*ip) + canip->ah_optlen;
1711
1712                 for (pad = 3 - (canip->ah_optlen & 3); pad; pad--) {
1713                         *s++ = IPOPT_NOP;
1714                         *s = IPOPT_EOL;
1715                         canip->ah_optlen++;
1716                 }
1717                 canip->ah_optlen++;
1718         } else {
1719                 s = buf + sizeof(*ip) + canip->ah_optlen - 1;
1720
1721                 for (pad = 3 - (canip->ah_optlen & 3); pad; pad--) {
1722                         *s++ = IPOPT_NOP;
1723                         *s = IPOPT_EOL;
1724                         canip->ah_optlen++;
1725                 }
1726         }
1727         ip->ip_hl = (sizeof(*ip) + canip->ah_optlen) >> 2;
1728         inc_anipheaders(canip->ah_optlen);
1729         free_anipheader();
1730 }
1731
1732
1733 void set_secclass(arg)
1734 char **arg;
1735 {
1736         sclass = *arg;
1737         *arg = NULL;
1738 }
1739
1740
1741 void free_anipheader()
1742 {
1743         aniphdr_t *aip;
1744
1745         aip = canip;
1746         if ((canip = aip->ah_prev)) {
1747                 canip->ah_next = NULL;
1748                 aniptail = &canip->ah_next;
1749         }
1750
1751         if (canip)
1752                 free(aip);
1753 }
1754
1755
1756 void end_ipv4()
1757 {
1758         aniphdr_t *aip;
1759
1760         ip->ip_sum = 0;
1761         ip->ip_len = htons(ip->ip_len);
1762         ip->ip_sum = chksum((u_short *)ip, ip->ip_hl << 2);
1763         ip->ip_len = ntohs(ip->ip_len);
1764         free_anipheader();
1765         for (aip = aniphead, ip = NULL; aip; aip = aip->ah_next)
1766                 if (aip->ah_p == IPPROTO_IP)
1767                         ip = aip->ah_ip;
1768 }
1769
1770
1771 void end_icmp()
1772 {
1773         aniphdr_t *aip;
1774
1775         icmp->icmp_cksum = 0;
1776         icmp->icmp_cksum = chksum((u_short *)icmp, canip->ah_len);
1777         free_anipheader();
1778         for (aip = aniphead, icmp = NULL; aip; aip = aip->ah_next)
1779                 if (aip->ah_p == IPPROTO_ICMP)
1780                         icmp = aip->ah_icmp;
1781 }
1782
1783
1784 void end_udp()
1785 {
1786         u_long  sum;
1787         aniphdr_t *aip;
1788         ip_t    iptmp;
1789
1790         bzero((char *)&iptmp, sizeof(iptmp));
1791         iptmp.ip_p = ip->ip_p;
1792         iptmp.ip_src = ip->ip_src;
1793         iptmp.ip_dst = ip->ip_dst;
1794         iptmp.ip_len = htons(ip->ip_len - (ip->ip_hl << 2));
1795         sum = p_chksum((u_short *)&iptmp, (u_int)sizeof(iptmp));
1796         udp->uh_ulen = htons(udp->uh_ulen);
1797         udp->uh_sum = c_chksum((u_short *)udp, (u_int)ntohs(iptmp.ip_len), sum);
1798         free_anipheader();
1799         for (aip = aniphead, udp = NULL; aip; aip = aip->ah_next)
1800                 if (aip->ah_p == IPPROTO_UDP)
1801                         udp = aip->ah_udp;
1802 }
1803
1804
1805 void end_tcp()
1806 {
1807         u_long  sum;
1808         aniphdr_t *aip;
1809         ip_t    iptmp;
1810
1811         bzero((char *)&iptmp, sizeof(iptmp));
1812         iptmp.ip_p = ip->ip_p;
1813         iptmp.ip_src = ip->ip_src;
1814         iptmp.ip_dst = ip->ip_dst;
1815         iptmp.ip_len = htons(ip->ip_len - (ip->ip_hl << 2));
1816         sum = p_chksum((u_short *)&iptmp, (u_int)sizeof(iptmp));
1817         tcp->th_sum = 0;
1818         tcp->th_sum = c_chksum((u_short *)tcp, (u_int)ntohs(iptmp.ip_len), sum);
1819         free_anipheader();
1820         for (aip = aniphead, tcp = NULL; aip; aip = aip->ah_next)
1821                 if (aip->ah_p == IPPROTO_TCP)
1822                         tcp = aip->ah_tcp;
1823 }
1824
1825
1826 void end_data()
1827 {
1828         free_anipheader();
1829 }
1830
1831
1832 void iplang(fp)
1833 FILE *fp;
1834 {
1835         yyin = fp;
1836
1837         yydebug = (opts & OPT_DEBUG) ? 1 : 0;
1838
1839         while (!feof(fp))
1840                 yyparse();
1841 }
1842
1843
1844 u_short c_chksum(buf, len, init)
1845 u_short *buf;
1846 u_int   len;
1847 u_long  init;
1848 {
1849         u_long  sum = init;
1850         int     nwords = len >> 1;
1851  
1852         for(; nwords > 0; nwords--)
1853                 sum += *buf++;
1854         sum = (sum>>16) + (sum & 0xffff);
1855         sum += (sum >>16);
1856         return (~sum);
1857 }
1858
1859
1860 u_long  p_chksum(buf,len)
1861 u_short *buf;
1862 u_int   len;
1863 {
1864         u_long  sum = 0;
1865         int     nwords = len >> 1;
1866  
1867         for(; nwords > 0; nwords--)
1868                 sum += *buf++;
1869         return sum;
1870 }