2 * Copyright (C) 1993-1998 by Darren Reed.
4 * Redistribution and use in source and binary forms are permitted
5 * provided that this notice is preserved and due credit is given
6 * to the original author and the contributors.
8 * @(#)ip_fil.h 1.35 6/5/96
9 * $Id: ip_fil.h,v 2.3.2.4 1999/10/15 13:42:37 darrenr Exp $
17 * Pathnames for various IP Filter control devices. Used by LKM
18 * and userland, so defined here.
20 #define IPNAT_NAME "/dev/ipnat"
21 #define IPSTATE_NAME "/dev/ipstate"
22 #define IPAUTH_NAME "/dev/ipauth"
25 # define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
28 #if defined(KERNEL) && !defined(_KERNEL)
40 #if defined(__STDC__) || defined(__GNUC__)
41 # define SIOCADAFR _IOW('r', 60, struct frentry)
42 # define SIOCRMAFR _IOW('r', 61, struct frentry)
43 # define SIOCSETFF _IOW('r', 62, u_int)
44 # define SIOCGETFF _IOR('r', 63, u_int)
45 # define SIOCGETFS _IOR('r', 64, struct friostat)
46 # define SIOCIPFFL _IOWR('r', 65, int)
47 # define SIOCIPFFB _IOR('r', 66, int)
48 # define SIOCADIFR _IOW('r', 67, struct frentry)
49 # define SIOCRMIFR _IOW('r', 68, struct frentry)
50 # define SIOCSWAPA _IOR('r', 69, u_int)
51 # define SIOCINAFR _IOW('r', 70, struct frentry)
52 # define SIOCINIFR _IOW('r', 71, struct frentry)
53 # define SIOCFRENB _IOW('r', 72, u_int)
54 # define SIOCFRSYN _IOW('r', 73, u_int)
55 # define SIOCFRZST _IOWR('r', 74, struct friostat)
56 # define SIOCZRLST _IOWR('r', 75, struct frentry)
57 # define SIOCAUTHW _IOWR('r', 76, struct fr_info)
58 # define SIOCAUTHR _IOWR('r', 77, struct fr_info)
59 # define SIOCATHST _IOWR('r', 78, struct fr_authstat)
61 # define SIOCADAFR _IOW(r, 60, struct frentry)
62 # define SIOCRMAFR _IOW(r, 61, struct frentry)
63 # define SIOCSETFF _IOW(r, 62, u_int)
64 # define SIOCGETFF _IOR(r, 63, u_int)
65 # define SIOCGETFS _IOR(r, 64, struct friostat)
66 # define SIOCIPFFL _IOWR(r, 65, int)
67 # define SIOCIPFFB _IOR(r, 66, int)
68 # define SIOCADIFR _IOW(r, 67, struct frentry)
69 # define SIOCRMIFR _IOW(r, 68, struct frentry)
70 # define SIOCSWAPA _IOR(r, 69, u_int)
71 # define SIOCINAFR _IOW(r, 70, struct frentry)
72 # define SIOCINIFR _IOW(r, 71, struct frentry)
73 # define SIOCFRENB _IOW(r, 72, u_int)
74 # define SIOCFRSYN _IOW(r, 73, u_int)
75 # define SIOCFRZST _IOWR(r, 74, struct friostat)
76 # define SIOCZRLST _IOWR(r, 75, struct frentry)
77 # define SIOCAUTHW _IOWR(r, 76, struct fr_info)
78 # define SIOCAUTHR _IOWR(r, 77, struct fr_info)
79 # define SIOCATHST _IOWR(r, 78, struct fr_authstat)
81 #define SIOCADDFR SIOCADAFR
82 #define SIOCDELFR SIOCRMAFR
83 #define SIOCINSFR SIOCINAFR
85 typedef struct fr_ip {
86 u_char fi_v:4; /* IP version */
87 u_char fi_fl:4; /* packet flags */
88 u_char fi_tos; /* IP packet TOS */
89 u_char fi_ttl; /* IP packet TTL */
90 u_char fi_p; /* IP packet protocol */
91 struct in_addr fi_src; /* source address from packet */
92 struct in_addr fi_dst; /* destination address from packet */
93 u_32_t fi_optmsk; /* bitmask composed from IP options */
94 u_short fi_secmsk; /* bitmask composed from IP security options */
95 u_short fi_auth; /* authentication code from IP sec. options */
98 #define FI_OPTIONS (FF_OPTIONS >> 24)
99 #define FI_TCPUDP (FF_TCPUDP >> 24) /* TCP/UCP implied comparison*/
100 #define FI_FRAG (FF_FRAG >> 24)
101 #define FI_SHORT (FF_SHORT >> 24)
102 #define FI_CMP (FI_OPTIONS|FI_TCPUDP|FI_SHORT)
105 * These are both used by the state and NAT code to indicate that one port or
106 * the other should be treated as a wildcard.
108 #define FI_W_SPORT 0x00000100
109 #define FI_W_DPORT 0x00000200
111 typedef struct fr_info {
112 void *fin_ifp; /* interface packet is `on' */
113 struct fr_ip fin_fi; /* IP Packet summary */
114 u_short fin_data[2]; /* TCP/UDP ports, ICMP code/type */
115 u_char fin_out; /* in or out ? 1 == out, 0 == in */
116 u_char fin_rev; /* state only: 1 = reverse */
117 u_short fin_hlen; /* length of IP header in bytes */
118 u_char fin_tcpf; /* TCP header flags (SYN, ACK, etc) */
119 /* From here on is packet specific */
120 u_char fin_icode; /* ICMP error to return */
121 u_short fin_rule; /* rule # last matched */
122 u_short fin_group; /* group number, -1 for none */
123 struct frentry *fin_fr; /* last matching rule */
124 char *fin_dp; /* start of data past IP header */
125 u_short fin_dlen; /* length of data portion of packet */
126 u_short fin_id; /* IP packet id field */
127 void *fin_mp; /* pointer to pointer to mbuf */
128 #if SOLARIS && defined(_KERNEL)
129 void *fin_qfm; /* pointer to mblk where pkt starts */
135 * Size for compares on fr_info structures
137 #define FI_CSIZE offsetof(fr_info_t, fin_icode)
140 * Size for copying cache fr_info structure
142 #define FI_COPYSIZE offsetof(fr_info_t, fin_dp)
144 typedef struct frdest {
146 struct in_addr fd_ip;
147 char fd_ifname[IFNAMSIZ];
150 typedef struct frentry {
151 struct frentry *fr_next;
152 u_short fr_group; /* group to which this rule belongs */
153 u_short fr_grhead; /* group # which this rule starts */
154 struct frentry *fr_grp;
155 int fr_ref; /* reference count - for grouping */
161 * These are only incremented when a packet matches this rule and
162 * it is the last match
167 * Fields after this may not change whilst in the kernel.
170 struct fr_ip fr_mip; /* mask structure */
172 u_char fr_tcpfm; /* tcp flags mask */
173 u_char fr_tcpf; /* tcp flags */
175 u_short fr_icmpm; /* data for ICMP packets (mask) */
178 u_char fr_scmp; /* data for port comparisons */
182 u_short fr_stop; /* top port for <> and >< */
183 u_short fr_dtop; /* top port for <> and >< */
184 u_32_t fr_flags; /* per-rule flags && options (see below) */
185 u_short fr_skip; /* # of rules to skip */
186 u_short fr_loglevel; /* syslog log facility + priority */
187 int (*fr_func) __P((int, ip_t *, fr_info_t *)); /* call this function */
188 char fr_icode; /* return ICMP code */
189 char fr_ifname[IFNAMSIZ];
191 char fr_oifname[IFNAMSIZ];
193 struct frdest fr_tif; /* "to" interface */
194 struct frdest fr_dif; /* duplicate packet interfaces */
197 #define fr_proto fr_ip.fi_p
198 #define fr_ttl fr_ip.fi_ttl
199 #define fr_tos fr_ip.fi_tos
200 #define fr_dst fr_ip.fi_dst
201 #define fr_src fr_ip.fi_src
202 #define fr_dmsk fr_mip.fi_dst
203 #define fr_smsk fr_mip.fi_src
206 #define offsetof(t,m) (int)((&((t *)0L)->m))
208 #define FR_CMPSIZ (sizeof(struct frentry) - offsetof(frentry_t, fr_ip))
213 #define FR_BLOCK 0x00001 /* do not allow packet to pass */
214 #define FR_PASS 0x00002 /* allow packet to pass */
215 #define FR_OUTQUE 0x00004 /* outgoing packets */
216 #define FR_INQUE 0x00008 /* ingoing packets */
217 #define FR_LOG 0x00010 /* Log */
218 #define FR_LOGB 0x00011 /* Log-fail */
219 #define FR_LOGP 0x00012 /* Log-pass */
220 #define FR_LOGBODY 0x00020 /* Log the body */
221 #define FR_LOGFIRST 0x00040 /* Log the first byte if state held */
222 #define FR_RETRST 0x00080 /* Return TCP RST packet - reset connection */
223 #define FR_RETICMP 0x00100 /* Return ICMP unreachable packet */
224 #define FR_FAKEICMP 0x00180 /* Return ICMP unreachable with fake source */
225 #define FR_NOMATCH 0x00200 /* no match occured */
226 #define FR_ACCOUNT 0x00400 /* count packet bytes */
227 #define FR_KEEPFRAG 0x00800 /* keep fragment information */
228 #define FR_KEEPSTATE 0x01000 /* keep `connection' state information */
229 #define FR_INACTIVE 0x02000
230 #define FR_QUICK 0x04000 /* match & stop processing list */
231 #define FR_FASTROUTE 0x08000 /* bypass normal routing */
232 #define FR_CALLNOW 0x10000 /* call another function (fr_func) if matches */
233 #define FR_DUP 0x20000 /* duplicate packet */
234 #define FR_LOGORBLOCK 0x40000 /* block the packet if it can't be logged */
235 #define FR_NOTSRCIP 0x80000 /* not the src IP# */
236 #define FR_NOTDSTIP 0x100000 /* not the dst IP# */
237 #define FR_AUTH 0x200000 /* use authentication */
238 #define FR_PREAUTH 0x400000 /* require preauthentication */
239 #define FR_DONTCACHE 0x800000 /* don't cache the result */
241 #define FR_LOGMASK (FR_LOG|FR_LOGP|FR_LOGB)
242 #define FR_RETMASK (FR_RETICMP|FR_RETRST|FR_FAKEICMP)
245 * These correspond to #define's for FI_* and are stored in fr_flags
247 #define FF_OPTIONS 0x01000000
248 #define FF_TCPUDP 0x02000000
249 #define FF_FRAG 0x04000000
250 #define FF_SHORT 0x08000000
252 * recognized flags for SIOCGETFF and SIOCSETFF, and get put in fr_flags
254 #define FF_LOGPASS 0x10000000
255 #define FF_LOGBLOCK 0x20000000
256 #define FF_LOGNOMATCH 0x40000000
257 #define FF_LOGGING (FF_LOGPASS|FF_LOGBLOCK|FF_LOGNOMATCH)
258 #define FF_BLOCKNONIP 0x80000000 /* Solaris2 Only */
264 #define FR_GREATERT 4
266 #define FR_GREATERTE 6
267 #define FR_OUTRANGE 7
270 typedef struct filterstats {
271 u_long fr_pass; /* packets allowed */
272 u_long fr_block; /* packets denied */
273 u_long fr_nom; /* packets which don't match any rule */
274 u_long fr_ppkl; /* packets allowed and logged */
275 u_long fr_bpkl; /* packets denied and logged */
276 u_long fr_npkl; /* packets unmatched and logged */
277 u_long fr_pkl; /* packets logged */
278 u_long fr_skip; /* packets to be logged but buffer full */
279 u_long fr_ret; /* packets for which a return is sent */
280 u_long fr_acct; /* packets for which counting was performed */
281 u_long fr_bnfr; /* bad attempts to allocate fragment state */
282 u_long fr_nfr; /* new fragment state kept */
283 u_long fr_cfr; /* add new fragment state but complete pkt */
284 u_long fr_bads; /* bad attempts to allocate packet state */
285 u_long fr_ads; /* new packet state kept */
286 u_long fr_chit; /* cached hit */
287 u_long fr_tcpbad; /* TCP checksum check failures */
288 u_long fr_pull[2]; /* good and bad pullup attempts */
290 u_long fr_notdata; /* PROTO/PCPROTO that have no data */
291 u_long fr_nodata; /* mblks that have no data */
292 u_long fr_bad; /* bad IP packets to the filter */
293 u_long fr_notip; /* packets passed through no on ip queue */
294 u_long fr_drop; /* packets dropped - no info for them! */
301 typedef struct friostat {
302 struct filterstats f_st[2];
303 struct frentry *f_fin[2];
304 struct frentry *f_fout[2];
305 struct frentry *f_acctin[2];
306 struct frentry *f_acctout[2];
307 struct frentry *f_auth;
308 struct frgroup *f_groups[3][2];
310 int f_active; /* 1 or 0 - active rule set */
311 int f_defpass; /* default pass - from fr_pass */
312 int f_running; /* 1 if running, else 0 */
313 int f_logging; /* 1 if enabled, else 0 */
314 char f_version[32]; /* version string */
317 typedef struct optlist {
324 * Group list structure.
326 typedef struct frgroup {
328 struct frgroup *fg_next;
329 struct frentry *fg_head;
330 struct frentry **fg_start;
335 * Log structure. Each packet header logged is prepended by one of these.
336 * Following this in the log records read from the device will be an ipflog
337 * structure which is then followed by any packet data.
339 typedef struct iplog {
345 struct iplog *ipl_next;
348 #define IPL_MAGIC 0x49504c4d /* 'IPLM' */
350 typedef struct ipflog {
351 #if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
352 (defined(OpenBSD) && (OpenBSD >= 199603))
353 u_char fl_ifname[IFNAMSIZ];
358 u_char fl_plen; /* extra data after hlen */
359 u_char fl_hlen; /* length of IP headers saved */
360 u_short fl_rule; /* assume never more than 64k rules, total */
362 u_short fl_loglevel; /* syslog log level */
368 #ifndef ICMP_UNREACH_FILTER
369 # define ICMP_UNREACH_FILTER 13
373 # define IPF_LOGGING 0
375 #ifndef IPF_DEFAULT_PASS
376 # define IPF_DEFAULT_PASS FR_PASS
379 #define IPMINLEN(i, h) ((i)->ip_len >= ((i)->ip_hl * 4 + sizeof(struct h)))
380 #define IPLLOGSIZE 8192
383 * Device filenames for reading log information. Use ipf on Solaris2 because
384 * ipl is already a name used by something else.
388 # define IPL_NAME "/dev/ipf"
390 # define IPL_NAME "/dev/ipl"
393 #define IPL_NAT IPNAT_NAME
394 #define IPL_STATE IPSTATE_NAME
395 #define IPL_AUTH IPAUTH_NAME
397 #define IPL_LOGIPF 0 /* Minor device #'s for accessing logs */
399 #define IPL_LOGSTATE 2
400 #define IPL_LOGAUTH 3
403 #if !defined(CDEV_MAJOR) && defined (__FreeBSD_version) && \
404 (__FreeBSD_version >= 220000)
405 # define CDEV_MAJOR 79
409 * Post NetBSD 1.2 has the PFIL interface for packet filters. This turns
410 * on those hooks. We don't need any special mods in non-IP Filter code
413 #if (defined(NetBSD) && (NetBSD > 199609) && (NetBSD <= 1991011)) || \
414 (defined(NetBSD1_2) && NetBSD1_2 > 1)
415 # if (NetBSD >= 199905)
425 extern int fr_check __P((ip_t *, int, void *, int, mb_t **));
426 extern int (*fr_checkp) __P((ip_t *, int, void *, int, mb_t **));
427 extern int send_reset __P((ip_t *, struct ifnet *));
428 extern int icmp_error __P((ip_t *, struct ifnet *));
429 extern int ipf_log __P((void));
430 extern int ipfr_fastroute __P((ip_t *, fr_info_t *, frdest_t *));
431 extern struct ifnet *get_unit __P((char *));
432 # if defined(__NetBSD__) || defined(__OpenBSD__) || \
433 (_BSDI_VERSION >= 199701) || (__FreeBSD_version >= 300000)
434 extern int iplioctl __P((dev_t, u_long, caddr_t, int));
436 extern int iplioctl __P((dev_t, int, caddr_t, int));
438 extern int iplopen __P((dev_t, int));
439 extern int iplclose __P((dev_t, int));
440 #else /* #ifndef _KERNEL */
441 # if defined(__NetBSD__) && defined(PFIL_HOOKS)
442 extern void ipfilterattach __P((int));
444 extern int iplattach __P((void));
445 extern int ipl_enable __P((void));
446 extern int ipl_disable __P((void));
447 extern void ipflog_init __P((void));
448 extern int ipflog_clear __P((minor_t));
449 extern int ipflog_read __P((minor_t, struct uio *));
450 extern int ipflog __P((u_int, ip_t *, fr_info_t *, mb_t *));
451 extern int ipllog __P((int, fr_info_t *, void **, size_t *, int *, int));
453 extern int fr_check __P((ip_t *, int, void *, int, qif_t *, mb_t **));
454 extern int (*fr_checkp) __P((ip_t *, int, void *,
455 int, qif_t *, mb_t **));
456 extern int icmp_error __P((ip_t *, int, int, qif_t *, struct in_addr));
458 extern int iplioctl __P((dev_t, int, intptr_t, int, cred_t *, int *));
460 extern int iplioctl __P((dev_t, int, int *, int, cred_t *, int *));
462 extern int iplopen __P((dev_t *, int, int, cred_t *));
463 extern int iplclose __P((dev_t, int, int, cred_t *));
464 extern int ipfsync __P((void));
465 extern int send_reset __P((fr_info_t *, ip_t *, qif_t *));
466 extern int ipfr_fastroute __P((qif_t *, ip_t *, mblk_t *, mblk_t **,
467 fr_info_t *, frdest_t *));
468 extern void copyin_mblk __P((mblk_t *, size_t, size_t, char *));
469 extern void copyout_mblk __P((mblk_t *, size_t, size_t, char *));
470 extern int fr_qin __P((queue_t *, mblk_t *));
471 extern int fr_qout __P((queue_t *, mblk_t *));
473 extern int iplread __P((dev_t, struct uio *, cred_t *));
476 extern int fr_check __P((ip_t *, int, void *, int, mb_t **));
477 extern int (*fr_checkp) __P((ip_t *, int, void *, int, mb_t **));
479 extern int send_reset __P((tcpiphdr_t *, struct ifnet *));
481 extern int send_reset __P((fr_info_t *, struct ip *));
482 extern int send_icmp_err __P((ip_t *, int, int, void *, struct in_addr));
484 extern int ipfr_fastroute __P((mb_t *, fr_info_t *, frdest_t *));
485 extern size_t mbufchainlen __P((mb_t *));
487 # include <sys/cred.h>
488 extern int iplioctl __P((dev_t, int, caddr_t, int, cred_t *, int *));
489 extern int iplopen __P((dev_t *, int, int, cred_t *));
490 extern int iplclose __P((dev_t, int, int, cred_t *));
491 extern int iplread __P((dev_t, struct uio *, cred_t *));
492 extern int ipfsync __P((void));
493 extern int ipfilter_sgi_attach __P((void));
494 extern void ipfilter_sgi_detach __P((void));
495 extern void ipfilter_sgi_intfsync __P((void));
498 extern int iplidentify __P((char *));
500 # if (_BSDI_VERSION >= 199510) || (__FreeBSD_version >= 220000) || \
501 (NetBSD >= 199511) || defined(__OpenBSD__)
502 # if defined(__NetBSD__) || (_BSDI_VERSION >= 199701) || \
503 defined(__OpenBSD__) || (__FreeBSD_version >= 300000)
504 extern int iplioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
506 extern int iplioctl __P((dev_t, int, caddr_t, int, struct proc *));
508 extern int iplopen __P((dev_t, int, int, struct proc *));
509 extern int iplclose __P((dev_t, int, int, struct proc *));
512 extern int iplopen __P((dev_t, int));
513 extern int iplclose __P((dev_t, int));
514 extern int iplioctl __P((dev_t, int, caddr_t, int));
516 extern int iplioctl(struct inode *, struct file *, u_int, u_long);
517 extern int iplopen __P((struct inode *, struct file *));
518 extern void iplclose __P((struct inode *, struct file *));
520 # endif /* (_BSDI_VERSION >= 199510) */
522 extern int iplread __P((dev_t, struct uio *, int));
525 extern int iplread __P((dev_t, struct uio *));
527 extern int iplread(struct inode *, struct file *, char *, int);
529 # endif /* BSD >= 199306 */
531 # endif /* SOLARIS */
532 #endif /* #ifndef _KERNEL */
534 extern void fixskip __P((frentry_t **, frentry_t *, int));
535 extern int countbits __P((u_32_t));
536 extern int ipldetach __P((void));
537 extern u_short fr_tcpsum __P((mb_t *, ip_t *, tcphdr_t *));
538 extern int fr_scanlist __P((u_32_t, ip_t *, fr_info_t *, void *));
539 extern u_short ipf_cksum __P((u_short *, int));
540 extern int fr_copytolog __P((int, char *, int));
541 extern void fr_forgetifp __P((void *));
542 extern int frflush __P((minor_t, int));
543 extern void frsync __P((void));
544 extern frgroup_t *fr_addgroup __P((u_int, frentry_t *, minor_t, int));
545 extern frgroup_t *fr_findgroup __P((u_int, u_32_t, minor_t, int, frgroup_t ***));
546 extern void fr_delgroup __P((u_int, u_32_t, minor_t, int));
547 extern void fr_makefrip __P((int, ip_t *, fr_info_t *));
548 extern int fr_ifpaddr __P((void *, struct in_addr *));
549 extern char *memstr __P((char *, char *, int, int));
550 extern int ipl_unreach;
551 extern int ipl_inited;
552 extern u_long ipl_frouteok[2];
555 extern int fr_active;
556 extern fr_info_t frcache[2];
557 extern char ipfilter_version[];
559 extern iplog_t **iplh[IPL_LOGMAX+1], *iplt[IPL_LOGMAX+1];
560 extern size_t iplused[IPL_LOGMAX + 1];
562 extern struct frentry *ipfilter[2][2], *ipacct[2][2];
563 extern struct frgroup *ipfgroups[3][2];
564 extern struct filterstats frstats[];
566 #endif /* __IP_FIL_H__ */