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