xref: /onnv-gate/usr/src/cmd/ipf/lib/common/addicmp.c (revision 2393:76e0289ce525)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (C) 1993-2001 by Darren Reed.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
50Sstevel@tonic-gate  *
6*2393Syz155240  * $Id: addicmp.c,v 1.10.2.1 2004/12/09 19:41:16 darrenr Exp $
70Sstevel@tonic-gate  */
80Sstevel@tonic-gate 
90Sstevel@tonic-gate #include <ctype.h>
100Sstevel@tonic-gate 
110Sstevel@tonic-gate #include "ipf.h"
120Sstevel@tonic-gate 
130Sstevel@tonic-gate 
140Sstevel@tonic-gate char	*icmptypes[MAX_ICMPTYPE + 1] = {
150Sstevel@tonic-gate 	"echorep", (char *)NULL, (char *)NULL, "unreach", "squench",
160Sstevel@tonic-gate 	"redir", (char *)NULL, (char *)NULL, "echo", "routerad",
170Sstevel@tonic-gate 	"routersol", "timex", "paramprob", "timest", "timestrep",
180Sstevel@tonic-gate 	"inforeq", "inforep", "maskreq", "maskrep", "END"
190Sstevel@tonic-gate };
200Sstevel@tonic-gate 
210Sstevel@tonic-gate /*
220Sstevel@tonic-gate  * set the icmp field to the correct type if "icmp" word is found
230Sstevel@tonic-gate  */
addicmp(cp,fp,linenum)240Sstevel@tonic-gate int	addicmp(cp, fp, linenum)
250Sstevel@tonic-gate char	***cp;
260Sstevel@tonic-gate struct	frentry	*fp;
270Sstevel@tonic-gate int     linenum;
280Sstevel@tonic-gate {
290Sstevel@tonic-gate 	char	**t;
300Sstevel@tonic-gate 	int	i;
310Sstevel@tonic-gate 
320Sstevel@tonic-gate 	(*cp)++;
330Sstevel@tonic-gate 	if (!**cp)
340Sstevel@tonic-gate 		return -1;
350Sstevel@tonic-gate 	if (!fp->fr_proto)	/* to catch lusers */
360Sstevel@tonic-gate 		fp->fr_proto = IPPROTO_ICMP;
37*2393Syz155240 	if (ISDIGIT(***cp)) {
380Sstevel@tonic-gate 		if (!ratoi(**cp, &i, 0, 255)) {
390Sstevel@tonic-gate 			fprintf(stderr,
400Sstevel@tonic-gate 				"%d: Invalid icmp-type (%s) specified\n",
410Sstevel@tonic-gate 				linenum, **cp);
420Sstevel@tonic-gate 			return -1;
430Sstevel@tonic-gate 		}
440Sstevel@tonic-gate 	} else {
450Sstevel@tonic-gate 		for (t = icmptypes, i = 0; ; t++, i++) {
460Sstevel@tonic-gate 			if (!*t)
470Sstevel@tonic-gate 				continue;
480Sstevel@tonic-gate 			if (!strcasecmp("END", *t)) {
490Sstevel@tonic-gate 				i = -1;
500Sstevel@tonic-gate 				break;
510Sstevel@tonic-gate 			}
520Sstevel@tonic-gate 			if (!strcasecmp(*t, **cp))
530Sstevel@tonic-gate 				break;
540Sstevel@tonic-gate 		}
550Sstevel@tonic-gate 		if (i == -1) {
560Sstevel@tonic-gate 			fprintf(stderr,
570Sstevel@tonic-gate 				"%d: Unknown icmp-type (%s) specified\n",
580Sstevel@tonic-gate 				linenum, **cp);
590Sstevel@tonic-gate 			return -1;
600Sstevel@tonic-gate 		}
610Sstevel@tonic-gate 	}
620Sstevel@tonic-gate 	fp->fr_icmp = (u_short)(i << 8);
630Sstevel@tonic-gate 	fp->fr_icmpm = (u_short)0xff00;
640Sstevel@tonic-gate 	(*cp)++;
650Sstevel@tonic-gate 	if (!**cp)
660Sstevel@tonic-gate 		return 0;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	if (**cp && strcasecmp("code", **cp))
690Sstevel@tonic-gate 		return 0;
700Sstevel@tonic-gate 	(*cp)++;
71*2393Syz155240 	if (ISDIGIT(***cp)) {
720Sstevel@tonic-gate 		if (!ratoi(**cp, &i, 0, 255)) {
73*2393Syz155240 			fprintf(stderr,
740Sstevel@tonic-gate 				"%d: Invalid icmp code (%s) specified\n",
750Sstevel@tonic-gate 				linenum, **cp);
760Sstevel@tonic-gate 			return -1;
770Sstevel@tonic-gate 		}
780Sstevel@tonic-gate 	} else {
790Sstevel@tonic-gate 		i = icmpcode(**cp);
800Sstevel@tonic-gate 		if (i == -1) {
81*2393Syz155240 			fprintf(stderr,
820Sstevel@tonic-gate 				"%d: Unknown icmp code (%s) specified\n",
830Sstevel@tonic-gate 				linenum, **cp);
840Sstevel@tonic-gate 			return -1;
850Sstevel@tonic-gate 		}
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 	i &= 0xff;
880Sstevel@tonic-gate 	fp->fr_icmp |= (u_short)i;
890Sstevel@tonic-gate 	fp->fr_icmpm = (u_short)0xffff;
900Sstevel@tonic-gate 	(*cp)++;
910Sstevel@tonic-gate 	return 0;
920Sstevel@tonic-gate }
93