165309e5cSBrian Somers /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 31de7b4b8SPedro F. Giffuni * 465309e5cSBrian Somers * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org> 565309e5cSBrian Somers * based on work by Toshiharu OHNO <tony-o@iij.ad.jp> 665309e5cSBrian Somers * Internet Initiative Japan, Inc (IIJ) 765309e5cSBrian Somers * All rights reserved. 8af57ed9fSAtsushi Murai * 965309e5cSBrian Somers * Redistribution and use in source and binary forms, with or without 1065309e5cSBrian Somers * modification, are permitted provided that the following conditions 1165309e5cSBrian Somers * are met: 1265309e5cSBrian Somers * 1. Redistributions of source code must retain the above copyright 1365309e5cSBrian Somers * notice, this list of conditions and the following disclaimer. 1465309e5cSBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 1565309e5cSBrian Somers * notice, this list of conditions and the following disclaimer in the 1665309e5cSBrian Somers * documentation and/or other materials provided with the distribution. 17af57ed9fSAtsushi Murai * 1865309e5cSBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1965309e5cSBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2065309e5cSBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2165309e5cSBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2265309e5cSBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2365309e5cSBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2465309e5cSBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2565309e5cSBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2665309e5cSBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2765309e5cSBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2865309e5cSBrian Somers * SUCH DAMAGE. 29af57ed9fSAtsushi Murai */ 30af57ed9fSAtsushi Murai 31cad7e742SBrian Somers /* Operations - f_srcop, f_dstop */ 32af57ed9fSAtsushi Murai #define OP_NONE 0 33af57ed9fSAtsushi Murai #define OP_EQ 1 34af57ed9fSAtsushi Murai #define OP_GT 2 35cad7e742SBrian Somers #define OP_LT 3 36af57ed9fSAtsushi Murai 371d1fc017SBrian Somers /* srctype or dsttype */ 381d1fc017SBrian Somers #define T_ADDR 0 391d1fc017SBrian Somers #define T_MYADDR 1 4030949fd4SBrian Somers #define T_MYADDR6 2 4130949fd4SBrian Somers #define T_HISADDR 3 4230949fd4SBrian Somers #define T_HISADDR6 4 4330949fd4SBrian Somers #define T_DNS0 5 4430949fd4SBrian Somers #define T_DNS1 6 451d1fc017SBrian Somers 46cad7e742SBrian Somers /* 47cad7e742SBrian Somers * There's a struct filterent for each possible filter rule. The 48cad7e742SBrian Somers * layout is designed to minimise size (there are 4 * MAXFILTERS of 49cad7e742SBrian Somers * them) - which is also conveniently a power of 2 (32 bytes) on 50cad7e742SBrian Somers * architectures where sizeof(int)==4 (this makes indexing faster). 51cad7e742SBrian Somers * 52cad7e742SBrian Somers * Note that there are four free bits in the initial word for future 53cad7e742SBrian Somers * extensions. 54cad7e742SBrian Somers */ 55af57ed9fSAtsushi Murai struct filterent { 5630949fd4SBrian Somers int f_proto; /* Protocol: getprotoby*() */ 57cad7e742SBrian Somers unsigned f_action : 8; /* Filtering action: goto or A_... */ 58cad7e742SBrian Somers unsigned f_srcop : 2; /* Source port operation: OP_... */ 59cad7e742SBrian Somers unsigned f_dstop : 2; /* Destination port operation: OP_... */ 602793563fSBrian Somers unsigned f_srctype : 3; /* T_ value of src */ 612793563fSBrian Somers unsigned f_dsttype : 3; /* T_ value of dst */ 62cad7e742SBrian Somers unsigned f_estab : 1; /* Check TCP ACK bit */ 63cad7e742SBrian Somers unsigned f_syn : 1; /* Check TCP SYN bit */ 64cad7e742SBrian Somers unsigned f_finrst : 1; /* Check TCP FIN/RST bits */ 65cad7e742SBrian Somers unsigned f_invert : 1; /* true to complement match */ 6630949fd4SBrian Somers struct ncprange f_src; /* Source address and mask */ 6730949fd4SBrian Somers struct ncprange f_dst; /* Destination address and mask */ 68cad7e742SBrian Somers u_short f_srcport; /* Source port, compared with f_srcop */ 69cad7e742SBrian Somers u_short f_dstport; /* Destination port, compared with f_dstop */ 700a4b6c5cSBrian Somers unsigned timeout; /* Keep alive value for passed packet */ 71af57ed9fSAtsushi Murai }; 72af57ed9fSAtsushi Murai 73d9949a3aSBrian Somers #define MAXFILTERS 40 /* in each filter set */ 745ca5389aSBrian Somers 75cad7e742SBrian Somers /* f_action values [0..MAXFILTERS) specify the next filter rule, others are: */ 76cad7e742SBrian Somers #define A_NONE (MAXFILTERS) 77cad7e742SBrian Somers #define A_PERMIT (A_NONE+1) 78cad7e742SBrian Somers #define A_DENY (A_PERMIT+1) 79cad7e742SBrian Somers 805ca5389aSBrian Somers struct filter { 815ca5389aSBrian Somers struct filterent rule[MAXFILTERS]; /* incoming packet filter */ 82cdbbb6b5SBrian Somers const char *name; 835ca5389aSBrian Somers unsigned fragok : 1; 845ca5389aSBrian Somers unsigned logok : 1; 855ca5389aSBrian Somers }; 86af57ed9fSAtsushi Murai 87cad7e742SBrian Somers /* Which filter set */ 8884b8a6ebSAtsushi Murai #define FL_IN 0 8984b8a6ebSAtsushi Murai #define FL_OUT 1 9084b8a6ebSAtsushi Murai #define FL_DIAL 2 9184b8a6ebSAtsushi Murai #define FL_KEEP 3 9275240ed1SBrian Somers 932764b86aSBrian Somers struct ipcp; 942764b86aSBrian Somers struct cmdargs; 95af57ed9fSAtsushi Murai 96dd7e2610SBrian Somers extern int filter_Show(struct cmdargs const *); 97dd7e2610SBrian Somers extern int filter_Set(struct cmdargs const *); 98057f1760SBrian Somers extern const char * filter_Action2Nam(unsigned); 99057f1760SBrian Somers extern const char *filter_Op2Nam(unsigned); 10030949fd4SBrian Somers extern void filter_AdjustAddr(struct filter *, struct ncpaddr *, 10130949fd4SBrian Somers struct ncpaddr *, struct in_addr *); 102