addicmp.c revision 145519
1176998Sphk/* $FreeBSD: head/contrib/ipfilter/lib/addicmp.c 145519 2005-04-25 18:20:15Z darrenr $ */ 2330449Seadler 3330449Seadler/* 4176998Sphk * Copyright (C) 1993-2001 by Darren Reed. 5176998Sphk * 6176998Sphk * See the IPFILTER.LICENCE file for details on licencing. 7176998Sphk * 8176998Sphk * Id: addicmp.c,v 1.10.2.1 2004/12/09 19:41:16 darrenr Exp 9176998Sphk */ 10176998Sphk 11176998Sphk#include <ctype.h> 12176998Sphk 13176998Sphk#include "ipf.h" 14176998Sphk 15176998Sphk 16176998Sphkchar *icmptypes[MAX_ICMPTYPE + 1] = { 17176998Sphk "echorep", (char *)NULL, (char *)NULL, "unreach", "squench", 18176998Sphk "redir", (char *)NULL, (char *)NULL, "echo", "routerad", 19176998Sphk "routersol", "timex", "paramprob", "timest", "timestrep", 20176998Sphk "inforeq", "inforep", "maskreq", "maskrep", "END" 21176998Sphk}; 22176998Sphk 23176998Sphk/* 24176998Sphk * set the icmp field to the correct type if "icmp" word is found 25176998Sphk */ 26176998Sphkint addicmp(cp, fp, linenum) 27176998Sphkchar ***cp; 28176998Sphkstruct frentry *fp; 29176998Sphkint linenum; 30176998Sphk{ 31176998Sphk char **t; 32176998Sphk int i; 33176998Sphk 34176998Sphk (*cp)++; 35176998Sphk if (!**cp) 36176998Sphk return -1; 37176998Sphk if (!fp->fr_proto) /* to catch lusers */ 38176998Sphk fp->fr_proto = IPPROTO_ICMP; 39176998Sphk if (ISDIGIT(***cp)) { 40176998Sphk if (!ratoi(**cp, &i, 0, 255)) { 41176998Sphk fprintf(stderr, 42176998Sphk "%d: Invalid icmp-type (%s) specified\n", 43176998Sphk linenum, **cp); 44176998Sphk return -1; 45176998Sphk } 46176998Sphk } else { 47176998Sphk for (t = icmptypes, i = 0; ; t++, i++) { 48176998Sphk if (!*t) 49176998Sphk continue; 50176998Sphk if (!strcasecmp("END", *t)) { 51176998Sphk i = -1; 52176998Sphk break; 53219027Sphk } 54176998Sphk if (!strcasecmp(*t, **cp)) 55176998Sphk break; 56176998Sphk } 57176998Sphk if (i == -1) { 58176998Sphk fprintf(stderr, 59176998Sphk "%d: Unknown icmp-type (%s) specified\n", 60176998Sphk linenum, **cp); 61219097Sphk return -1; 62176998Sphk } 63176998Sphk } 64176998Sphk fp->fr_icmp = (u_short)(i << 8); 65176998Sphk fp->fr_icmpm = (u_short)0xff00; 66176998Sphk (*cp)++; 67176998Sphk if (!**cp) 68176998Sphk return 0; 69176998Sphk 70176998Sphk if (**cp && strcasecmp("code", **cp)) 71176998Sphk return 0; 72176998Sphk (*cp)++; 73176998Sphk if (ISDIGIT(***cp)) { 74176998Sphk if (!ratoi(**cp, &i, 0, 255)) { 75219094Sphk fprintf(stderr, 76219027Sphk "%d: Invalid icmp code (%s) specified\n", 77176998Sphk linenum, **cp); 78176998Sphk return -1; 79176998Sphk } 80176998Sphk } else { 81176998Sphk i = icmpcode(**cp); 82176998Sphk if (i == -1) { 83176998Sphk fprintf(stderr, 84176998Sphk "%d: Unknown icmp code (%s) specified\n", 85176998Sphk linenum, **cp); 86176998Sphk return -1; 87176998Sphk } 88176998Sphk } 89176998Sphk i &= 0xff; 90176998Sphk fp->fr_icmp |= (u_short)i; 91176998Sphk fp->fr_icmpm = (u_short)0xffff; 92176998Sphk (*cp)++; 93176998Sphk return 0; 94176998Sphk} 95176998Sphk