xref: /onnv-gate/usr/src/cmd/ipf/lib/common/addicmp.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (C) 1993-2001 by Darren Reed.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  * $Id: addicmp.c,v 1.8 2002/01/28 06:50:45 darrenr Exp $
7*0Sstevel@tonic-gate  */
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate #include <ctype.h>
10*0Sstevel@tonic-gate 
11*0Sstevel@tonic-gate #include "ipf.h"
12*0Sstevel@tonic-gate 
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate char	*icmptypes[MAX_ICMPTYPE + 1] = {
15*0Sstevel@tonic-gate 	"echorep", (char *)NULL, (char *)NULL, "unreach", "squench",
16*0Sstevel@tonic-gate 	"redir", (char *)NULL, (char *)NULL, "echo", "routerad",
17*0Sstevel@tonic-gate 	"routersol", "timex", "paramprob", "timest", "timestrep",
18*0Sstevel@tonic-gate 	"inforeq", "inforep", "maskreq", "maskrep", "END"
19*0Sstevel@tonic-gate };
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate /*
22*0Sstevel@tonic-gate  * set the icmp field to the correct type if "icmp" word is found
23*0Sstevel@tonic-gate  */
24*0Sstevel@tonic-gate int	addicmp(cp, fp, linenum)
25*0Sstevel@tonic-gate char	***cp;
26*0Sstevel@tonic-gate struct	frentry	*fp;
27*0Sstevel@tonic-gate int     linenum;
28*0Sstevel@tonic-gate {
29*0Sstevel@tonic-gate 	char	**t;
30*0Sstevel@tonic-gate 	int	i;
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate 	(*cp)++;
33*0Sstevel@tonic-gate 	if (!**cp)
34*0Sstevel@tonic-gate 		return -1;
35*0Sstevel@tonic-gate 	if (!fp->fr_proto)	/* to catch lusers */
36*0Sstevel@tonic-gate 		fp->fr_proto = IPPROTO_ICMP;
37*0Sstevel@tonic-gate 	if (isdigit(***cp)) {
38*0Sstevel@tonic-gate 		if (!ratoi(**cp, &i, 0, 255)) {
39*0Sstevel@tonic-gate 			fprintf(stderr,
40*0Sstevel@tonic-gate 				"%d: Invalid icmp-type (%s) specified\n",
41*0Sstevel@tonic-gate 				linenum, **cp);
42*0Sstevel@tonic-gate 			return -1;
43*0Sstevel@tonic-gate 		}
44*0Sstevel@tonic-gate 	} else {
45*0Sstevel@tonic-gate 		for (t = icmptypes, i = 0; ; t++, i++) {
46*0Sstevel@tonic-gate 			if (!*t)
47*0Sstevel@tonic-gate 				continue;
48*0Sstevel@tonic-gate 			if (!strcasecmp("END", *t)) {
49*0Sstevel@tonic-gate 				i = -1;
50*0Sstevel@tonic-gate 				break;
51*0Sstevel@tonic-gate 			}
52*0Sstevel@tonic-gate 			if (!strcasecmp(*t, **cp))
53*0Sstevel@tonic-gate 				break;
54*0Sstevel@tonic-gate 		}
55*0Sstevel@tonic-gate 		if (i == -1) {
56*0Sstevel@tonic-gate 			fprintf(stderr,
57*0Sstevel@tonic-gate 				"%d: Unknown icmp-type (%s) specified\n",
58*0Sstevel@tonic-gate 				linenum, **cp);
59*0Sstevel@tonic-gate 			return -1;
60*0Sstevel@tonic-gate 		}
61*0Sstevel@tonic-gate 	}
62*0Sstevel@tonic-gate 	fp->fr_icmp = (u_short)(i << 8);
63*0Sstevel@tonic-gate 	fp->fr_icmpm = (u_short)0xff00;
64*0Sstevel@tonic-gate 	(*cp)++;
65*0Sstevel@tonic-gate 	if (!**cp)
66*0Sstevel@tonic-gate 		return 0;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	if (**cp && strcasecmp("code", **cp))
69*0Sstevel@tonic-gate 		return 0;
70*0Sstevel@tonic-gate 	(*cp)++;
71*0Sstevel@tonic-gate 	if (isdigit(***cp)) {
72*0Sstevel@tonic-gate 		if (!ratoi(**cp, &i, 0, 255)) {
73*0Sstevel@tonic-gate 			fprintf(stderr,
74*0Sstevel@tonic-gate 				"%d: Invalid icmp code (%s) specified\n",
75*0Sstevel@tonic-gate 				linenum, **cp);
76*0Sstevel@tonic-gate 			return -1;
77*0Sstevel@tonic-gate 		}
78*0Sstevel@tonic-gate 	} else {
79*0Sstevel@tonic-gate 		i = icmpcode(**cp);
80*0Sstevel@tonic-gate 		if (i == -1) {
81*0Sstevel@tonic-gate 			fprintf(stderr,
82*0Sstevel@tonic-gate 				"%d: Unknown icmp code (%s) specified\n",
83*0Sstevel@tonic-gate 				linenum, **cp);
84*0Sstevel@tonic-gate 			return -1;
85*0Sstevel@tonic-gate 		}
86*0Sstevel@tonic-gate 	}
87*0Sstevel@tonic-gate 	i &= 0xff;
88*0Sstevel@tonic-gate 	fp->fr_icmp |= (u_short)i;
89*0Sstevel@tonic-gate 	fp->fr_icmpm = (u_short)0xffff;
90*0Sstevel@tonic-gate 	(*cp)++;
91*0Sstevel@tonic-gate 	return 0;
92*0Sstevel@tonic-gate }
93