xref: /titanic_50/usr/src/cmd/ipf/lib/common/addicmp.c (revision ab25eeb551a4be927a4b6ae2cf8aff7ed17decb4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (C) 1993-2001 by Darren Reed.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate  *
6*ab25eeb5Syz155240  * $Id: addicmp.c,v 1.10.2.1 2004/12/09 19:41:16 darrenr Exp $
77c478bd9Sstevel@tonic-gate  */
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate #include <ctype.h>
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate #include "ipf.h"
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate char	*icmptypes[MAX_ICMPTYPE + 1] = {
157c478bd9Sstevel@tonic-gate 	"echorep", (char *)NULL, (char *)NULL, "unreach", "squench",
167c478bd9Sstevel@tonic-gate 	"redir", (char *)NULL, (char *)NULL, "echo", "routerad",
177c478bd9Sstevel@tonic-gate 	"routersol", "timex", "paramprob", "timest", "timestrep",
187c478bd9Sstevel@tonic-gate 	"inforeq", "inforep", "maskreq", "maskrep", "END"
197c478bd9Sstevel@tonic-gate };
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  * set the icmp field to the correct type if "icmp" word is found
237c478bd9Sstevel@tonic-gate  */
addicmp(cp,fp,linenum)247c478bd9Sstevel@tonic-gate int	addicmp(cp, fp, linenum)
257c478bd9Sstevel@tonic-gate char	***cp;
267c478bd9Sstevel@tonic-gate struct	frentry	*fp;
277c478bd9Sstevel@tonic-gate int     linenum;
287c478bd9Sstevel@tonic-gate {
297c478bd9Sstevel@tonic-gate 	char	**t;
307c478bd9Sstevel@tonic-gate 	int	i;
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate 	(*cp)++;
337c478bd9Sstevel@tonic-gate 	if (!**cp)
347c478bd9Sstevel@tonic-gate 		return -1;
357c478bd9Sstevel@tonic-gate 	if (!fp->fr_proto)	/* to catch lusers */
367c478bd9Sstevel@tonic-gate 		fp->fr_proto = IPPROTO_ICMP;
37*ab25eeb5Syz155240 	if (ISDIGIT(***cp)) {
387c478bd9Sstevel@tonic-gate 		if (!ratoi(**cp, &i, 0, 255)) {
397c478bd9Sstevel@tonic-gate 			fprintf(stderr,
407c478bd9Sstevel@tonic-gate 				"%d: Invalid icmp-type (%s) specified\n",
417c478bd9Sstevel@tonic-gate 				linenum, **cp);
427c478bd9Sstevel@tonic-gate 			return -1;
437c478bd9Sstevel@tonic-gate 		}
447c478bd9Sstevel@tonic-gate 	} else {
457c478bd9Sstevel@tonic-gate 		for (t = icmptypes, i = 0; ; t++, i++) {
467c478bd9Sstevel@tonic-gate 			if (!*t)
477c478bd9Sstevel@tonic-gate 				continue;
487c478bd9Sstevel@tonic-gate 			if (!strcasecmp("END", *t)) {
497c478bd9Sstevel@tonic-gate 				i = -1;
507c478bd9Sstevel@tonic-gate 				break;
517c478bd9Sstevel@tonic-gate 			}
527c478bd9Sstevel@tonic-gate 			if (!strcasecmp(*t, **cp))
537c478bd9Sstevel@tonic-gate 				break;
547c478bd9Sstevel@tonic-gate 		}
557c478bd9Sstevel@tonic-gate 		if (i == -1) {
567c478bd9Sstevel@tonic-gate 			fprintf(stderr,
577c478bd9Sstevel@tonic-gate 				"%d: Unknown icmp-type (%s) specified\n",
587c478bd9Sstevel@tonic-gate 				linenum, **cp);
597c478bd9Sstevel@tonic-gate 			return -1;
607c478bd9Sstevel@tonic-gate 		}
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 	fp->fr_icmp = (u_short)(i << 8);
637c478bd9Sstevel@tonic-gate 	fp->fr_icmpm = (u_short)0xff00;
647c478bd9Sstevel@tonic-gate 	(*cp)++;
657c478bd9Sstevel@tonic-gate 	if (!**cp)
667c478bd9Sstevel@tonic-gate 		return 0;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (**cp && strcasecmp("code", **cp))
697c478bd9Sstevel@tonic-gate 		return 0;
707c478bd9Sstevel@tonic-gate 	(*cp)++;
71*ab25eeb5Syz155240 	if (ISDIGIT(***cp)) {
727c478bd9Sstevel@tonic-gate 		if (!ratoi(**cp, &i, 0, 255)) {
737c478bd9Sstevel@tonic-gate 			fprintf(stderr,
747c478bd9Sstevel@tonic-gate 				"%d: Invalid icmp code (%s) specified\n",
757c478bd9Sstevel@tonic-gate 				linenum, **cp);
767c478bd9Sstevel@tonic-gate 			return -1;
777c478bd9Sstevel@tonic-gate 		}
787c478bd9Sstevel@tonic-gate 	} else {
797c478bd9Sstevel@tonic-gate 		i = icmpcode(**cp);
807c478bd9Sstevel@tonic-gate 		if (i == -1) {
817c478bd9Sstevel@tonic-gate 			fprintf(stderr,
827c478bd9Sstevel@tonic-gate 				"%d: Unknown icmp code (%s) specified\n",
837c478bd9Sstevel@tonic-gate 				linenum, **cp);
847c478bd9Sstevel@tonic-gate 			return -1;
857c478bd9Sstevel@tonic-gate 		}
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 	i &= 0xff;
887c478bd9Sstevel@tonic-gate 	fp->fr_icmp |= (u_short)i;
897c478bd9Sstevel@tonic-gate 	fp->fr_icmpm = (u_short)0xffff;
907c478bd9Sstevel@tonic-gate 	(*cp)++;
917c478bd9Sstevel@tonic-gate 	return 0;
927c478bd9Sstevel@tonic-gate }
93