1994-10-28 15:09:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1993 Daniel Boulet
|
|
|
|
* Copyright (c) 1994 Ugen J.S.Antsilevich
|
|
|
|
*
|
|
|
|
* Redistribution and use in source forms, with and without modification,
|
|
|
|
* are permitted provided that this entire comment appears intact.
|
|
|
|
*
|
|
|
|
* Redistribution in binary form may occur without any restrictions.
|
|
|
|
* Obviously, it would be nice if you gave credit where credit is due
|
|
|
|
* but requiring it would be too onerous.
|
|
|
|
*
|
|
|
|
* This software is provided ``AS IS'' without any warranties of any kind.
|
1995-07-23 05:36:31 +00:00
|
|
|
*
|
1996-04-03 13:52:20 +00:00
|
|
|
* $Id: ip_fw.c,v 1.33 1996/02/26 15:28:15 phk Exp $
|
1994-10-28 15:09:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implement IP packet firewall
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
1996-02-23 15:47:58 +00:00
|
|
|
#include <sys/queue.h>
|
|
|
|
#if 0 /* XXX -current, but not -stable */
|
1994-10-28 15:09:49 +00:00
|
|
|
#include <sys/kernel.h>
|
1996-02-23 15:47:58 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
1994-10-28 15:09:49 +00:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
1994-11-28 12:35:14 +00:00
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netinet/udp.h>
|
1994-10-28 15:09:49 +00:00
|
|
|
#include <netinet/ip_icmp.h>
|
|
|
|
#include <netinet/ip_fw.h>
|
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
static int fw_debug = 1;
|
|
|
|
#ifdef IPFIREWALL_VERBOSE
|
|
|
|
static int fw_verbose = 1;
|
1994-11-28 12:35:14 +00:00
|
|
|
#else
|
1996-02-23 15:47:58 +00:00
|
|
|
static int fw_verbose = 0;
|
1994-11-28 12:35:14 +00:00
|
|
|
#endif
|
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
|
|
|
|
LIST_HEAD (ip_fw_head, ip_fw_chain) ip_fw_chain;
|
1995-07-04 05:29:30 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
#ifdef SYSCTL_NODE
|
|
|
|
SYSCTL_NODE(net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
|
|
|
|
SYSCTL_INT(net_inet_ip_fw, OID_AUTO, debug, CTLFLAG_RW, &fw_debug, 0, "");
|
|
|
|
SYSCTL_INT(net_inet_ip_fw, OID_AUTO, verbose, CTLFLAG_RW, &fw_verbose, 0, "");
|
1995-07-04 05:29:30 +00:00
|
|
|
#endif
|
1994-11-28 12:35:14 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
#define dprintf(a) if (!fw_debug); else printf a
|
|
|
|
|
1995-05-11 19:26:53 +00:00
|
|
|
#define print_ip(a) printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
|
|
|
|
(ntohl(a.s_addr)>>16)&0xFF,\
|
|
|
|
(ntohl(a.s_addr)>>8)&0xFF,\
|
|
|
|
(ntohl(a.s_addr))&0xFF);
|
1994-11-28 12:35:14 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
#define dprint_ip(a) if (!fw_debug); else print_ip(a)
|
1994-11-28 12:35:14 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
static int add_entry __P((struct ip_fw_head *chainptr, struct ip_fw *frwl));
|
|
|
|
static int del_entry __P((struct ip_fw_head *chainptr, struct ip_fw *frwl));
|
1995-12-02 19:38:06 +00:00
|
|
|
static struct ip_fw *
|
|
|
|
check_ipfw_struct __P(( struct mbuf *m));
|
|
|
|
static int ipopts_match __P((struct ip *ip, struct ip_fw *f));
|
|
|
|
static int port_match __P((u_short *portptr, int nports, u_short port,
|
|
|
|
int range_flag));
|
|
|
|
static int tcpflg_match __P((struct tcphdr *tcp, struct ip_fw *f));
|
1996-02-24 00:17:35 +00:00
|
|
|
static void ipfw_report __P((char *txt, int rule, struct ip *ip));
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1996-04-03 13:52:20 +00:00
|
|
|
static ip_fw_chk_t *old_chk_ptr;
|
|
|
|
static ip_fw_ctl_t *old_ctl_ptr;
|
1996-02-24 13:38:28 +00:00
|
|
|
|
1994-10-28 15:09:49 +00:00
|
|
|
/*
|
1996-02-23 15:47:58 +00:00
|
|
|
* Returns 1 if the port is matched by the vector, 0 otherwise
|
1994-10-28 15:09:49 +00:00
|
|
|
*/
|
1995-11-14 20:34:56 +00:00
|
|
|
static inline int
|
1995-07-04 05:39:03 +00:00
|
|
|
port_match(portptr, nports, port, range_flag)
|
|
|
|
u_short *portptr;
|
|
|
|
int nports;
|
|
|
|
u_short port;
|
|
|
|
int range_flag;
|
1994-10-28 15:09:49 +00:00
|
|
|
{
|
1995-07-04 05:39:03 +00:00
|
|
|
if (!nports)
|
1996-02-23 15:47:58 +00:00
|
|
|
return 1;
|
1995-07-04 05:39:03 +00:00
|
|
|
if (range_flag) {
|
|
|
|
if (portptr[0] <= port && port <= portptr[1]) {
|
1996-02-23 15:47:58 +00:00
|
|
|
return 1;
|
1995-07-04 05:39:03 +00:00
|
|
|
}
|
|
|
|
nports -= 2;
|
|
|
|
portptr += 2;
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
1995-07-04 05:39:03 +00:00
|
|
|
while (nports-- > 0) {
|
|
|
|
if (*portptr++ == port) {
|
1996-02-23 15:47:58 +00:00
|
|
|
return 1;
|
1995-07-04 05:39:03 +00:00
|
|
|
}
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
1996-02-23 15:47:58 +00:00
|
|
|
return 0;
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
|
|
|
|
1995-11-14 20:34:56 +00:00
|
|
|
static int
|
1995-10-23 03:58:06 +00:00
|
|
|
tcpflg_match(tcp, f)
|
|
|
|
struct tcphdr *tcp;
|
|
|
|
struct ip_fw *f;
|
|
|
|
{
|
|
|
|
u_char flg_set, flg_clr;
|
|
|
|
|
1996-04-03 13:52:20 +00:00
|
|
|
if ((f->fw_tcpf & IP_FW_TCPF_ESTAB) &&
|
|
|
|
(tcp->th_flags & (IP_FW_TCPF_RST | IP_FW_TCPF_ACK)))
|
|
|
|
return 1;
|
|
|
|
|
1995-10-23 03:58:06 +00:00
|
|
|
flg_set = tcp->th_flags & f->fw_tcpf;
|
|
|
|
flg_clr = tcp->th_flags & f->fw_tcpnf;
|
|
|
|
|
|
|
|
if (flg_set != f->fw_tcpf)
|
|
|
|
return 0;
|
1996-02-23 15:47:58 +00:00
|
|
|
if (flg_clr)
|
1995-10-23 03:58:06 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1995-11-14 20:34:56 +00:00
|
|
|
static int
|
1995-10-01 21:52:50 +00:00
|
|
|
ipopts_match(ip, f)
|
|
|
|
struct ip *ip;
|
|
|
|
struct ip_fw *f;
|
|
|
|
{
|
|
|
|
register u_char *cp;
|
|
|
|
int opt, optlen, cnt;
|
|
|
|
u_char opts, nopts, nopts_sve;
|
|
|
|
|
|
|
|
cp = (u_char *)(ip + 1);
|
|
|
|
cnt = (ip->ip_hl << 2) - sizeof (struct ip);
|
|
|
|
opts = f->fw_ipopt;
|
|
|
|
nopts = nopts_sve = f->fw_ipnopt;
|
1995-10-23 03:58:06 +00:00
|
|
|
|
1995-10-01 21:52:50 +00:00
|
|
|
for (; cnt > 0; cnt -= optlen, cp += optlen) {
|
|
|
|
opt = cp[IPOPT_OPTVAL];
|
|
|
|
if (opt == IPOPT_EOL)
|
|
|
|
break;
|
|
|
|
if (opt == IPOPT_NOP)
|
|
|
|
optlen = 1;
|
|
|
|
else {
|
|
|
|
optlen = cp[IPOPT_OLEN];
|
|
|
|
if (optlen <= 0 || optlen > cnt) {
|
1996-02-24 13:38:28 +00:00
|
|
|
return 0; /*XXX*/
|
1995-10-01 21:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (opt) {
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPOPT_LSRR:
|
|
|
|
opts &= ~IP_FW_IPOPT_LSRR;
|
|
|
|
nopts &= ~IP_FW_IPOPT_LSRR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPOPT_SSRR:
|
|
|
|
opts &= ~IP_FW_IPOPT_SSRR;
|
|
|
|
nopts &= ~IP_FW_IPOPT_SSRR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPOPT_RR:
|
|
|
|
opts &= ~IP_FW_IPOPT_RR;
|
|
|
|
nopts &= ~IP_FW_IPOPT_RR;
|
|
|
|
break;
|
|
|
|
case IPOPT_TS:
|
|
|
|
opts &= ~IP_FW_IPOPT_TS;
|
|
|
|
nopts &= ~IP_FW_IPOPT_TS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (opts == nopts)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (opts == 0 && nopts == nopts_sve)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
static void
|
1996-02-24 00:17:35 +00:00
|
|
|
ipfw_report(char *txt, int rule, struct ip *ip)
|
1996-02-23 15:47:58 +00:00
|
|
|
{
|
|
|
|
struct tcphdr *tcp = (struct tcphdr *) ((u_long *) ip + ip->ip_hl);
|
|
|
|
struct udphdr *udp = (struct udphdr *) ((u_long *) ip + ip->ip_hl);
|
|
|
|
struct icmp *icmp = (struct icmp *) ((u_long *) ip + ip->ip_hl);
|
|
|
|
if (!fw_verbose)
|
|
|
|
return;
|
1996-02-24 00:17:35 +00:00
|
|
|
printf("ipfw: %d %s ",rule, txt);
|
1996-02-23 15:47:58 +00:00
|
|
|
switch (ip->ip_p) {
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
printf("TCP ");
|
|
|
|
print_ip(ip->ip_src);
|
|
|
|
printf(":%d ", ntohs(tcp->th_sport));
|
|
|
|
print_ip(ip->ip_dst);
|
|
|
|
printf(":%d", ntohs(tcp->th_dport));
|
|
|
|
break;
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
printf("UDP ");
|
|
|
|
print_ip(ip->ip_src);
|
|
|
|
printf(":%d ", ntohs(udp->uh_sport));
|
|
|
|
print_ip(ip->ip_dst);
|
|
|
|
printf(":%d", ntohs(udp->uh_dport));
|
|
|
|
break;
|
|
|
|
case IPPROTO_ICMP:
|
|
|
|
printf("ICMP:%u ", icmp->icmp_type);
|
|
|
|
print_ip(ip->ip_src);
|
|
|
|
printf(" ");
|
|
|
|
print_ip(ip->ip_dst);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("P:%d ", ip->ip_p);
|
|
|
|
print_ip(ip->ip_src);
|
|
|
|
printf(" ");
|
|
|
|
print_ip(ip->ip_dst);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((ip->ip_off & IP_OFFMASK))
|
|
|
|
printf(" Fragment = %d",ip->ip_off & IP_OFFMASK);
|
|
|
|
printf("\n");
|
|
|
|
}
|
1995-10-01 21:52:50 +00:00
|
|
|
|
1994-10-28 15:09:49 +00:00
|
|
|
/*
|
1996-02-23 15:47:58 +00:00
|
|
|
* Returns 1 if it should be accepted, 0 otherwise.
|
1994-10-28 15:09:49 +00:00
|
|
|
*/
|
|
|
|
|
1995-07-04 05:39:03 +00:00
|
|
|
int
|
1996-04-03 13:52:20 +00:00
|
|
|
ip_fw_chk(pip, hlen, rif, dir, m)
|
|
|
|
struct ip **pip;
|
1995-07-04 05:39:03 +00:00
|
|
|
struct ifnet *rif;
|
1996-04-03 13:52:20 +00:00
|
|
|
int hlen, dir;
|
|
|
|
struct mbuf **m;
|
1994-10-28 15:09:49 +00:00
|
|
|
{
|
1996-02-23 15:47:58 +00:00
|
|
|
struct ip_fw_chain *chain;
|
|
|
|
register struct ip_fw *f = NULL;
|
1996-04-03 13:52:20 +00:00
|
|
|
struct ip *ip = *pip;
|
1995-07-04 05:39:03 +00:00
|
|
|
struct tcphdr *tcp = (struct tcphdr *) ((u_long *) ip + ip->ip_hl);
|
|
|
|
struct udphdr *udp = (struct udphdr *) ((u_long *) ip + ip->ip_hl);
|
|
|
|
struct icmp *icmp = (struct icmp *) ((u_long *) ip + ip->ip_hl);
|
|
|
|
struct ifaddr *ia = NULL, *ia_p;
|
|
|
|
struct in_addr src, dst, ia_i;
|
|
|
|
u_short src_port = 0, dst_port = 0;
|
1996-02-23 15:47:58 +00:00
|
|
|
u_short f_prt = 0, prt, len = 0;
|
1994-11-28 12:35:14 +00:00
|
|
|
|
1995-07-04 05:39:03 +00:00
|
|
|
/*
|
1996-02-23 15:47:58 +00:00
|
|
|
* ... else if non-zero, highly unusual and interesting, but
|
|
|
|
* we're not going to pass it...
|
1995-07-04 05:39:03 +00:00
|
|
|
*/
|
1996-02-24 00:17:35 +00:00
|
|
|
if ((ip->ip_off & IP_OFFMASK) == 1) {
|
|
|
|
ipfw_report("Refuse", -1, ip);
|
1996-04-03 13:52:20 +00:00
|
|
|
m_freem(*m);
|
1996-02-24 00:17:35 +00:00
|
|
|
return 0;
|
1996-02-23 15:47:58 +00:00
|
|
|
}
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1995-07-04 05:39:03 +00:00
|
|
|
src = ip->ip_src;
|
|
|
|
dst = ip->ip_dst;
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1995-07-04 05:39:03 +00:00
|
|
|
/*
|
|
|
|
* If we got interface from which packet came-store pointer to it's
|
|
|
|
* first adress
|
|
|
|
*/
|
|
|
|
if (rif != NULL)
|
|
|
|
ia = rif->if_addrlist;
|
1994-12-12 17:20:55 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
/*
|
|
|
|
* Determine the protocol and extract some useful stuff
|
|
|
|
*/
|
1995-07-04 05:39:03 +00:00
|
|
|
switch (ip->ip_p) {
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
src_port = ntohs(tcp->th_sport);
|
|
|
|
dst_port = ntohs(tcp->th_dport);
|
|
|
|
prt = IP_FW_F_TCP;
|
1996-02-23 15:47:58 +00:00
|
|
|
len = sizeof (*tcp);
|
1995-07-04 05:39:03 +00:00
|
|
|
break;
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
src_port = ntohs(udp->uh_sport);
|
|
|
|
dst_port = ntohs(udp->uh_dport);
|
|
|
|
prt = IP_FW_F_UDP;
|
1996-02-23 15:47:58 +00:00
|
|
|
len = sizeof (*udp);
|
1995-07-04 05:39:03 +00:00
|
|
|
break;
|
|
|
|
case IPPROTO_ICMP:
|
|
|
|
prt = IP_FW_F_ICMP;
|
1996-02-23 15:47:58 +00:00
|
|
|
len = sizeof (*icmp);
|
1995-07-04 05:39:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
prt = IP_FW_F_ALL;
|
|
|
|
break;
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
1995-02-24 14:33:54 +00:00
|
|
|
|
1996-02-24 00:17:35 +00:00
|
|
|
/* XXX Check that we have sufficient header for TCP analysis */
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1995-07-04 05:39:03 +00:00
|
|
|
/*
|
1996-02-23 15:47:58 +00:00
|
|
|
* Go down the chain, looking for enlightment
|
1995-07-04 05:39:03 +00:00
|
|
|
*/
|
1996-02-23 15:47:58 +00:00
|
|
|
for (chain=ip_fw_chain.lh_first; chain; chain = chain->chain.le_next) {
|
|
|
|
f = chain->rule;
|
1994-11-28 12:35:14 +00:00
|
|
|
|
1996-02-24 00:17:35 +00:00
|
|
|
/* Check direction inbound */
|
|
|
|
if (!dir && !(f->fw_flg & IP_FW_F_IN))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Check direction outbound */
|
|
|
|
if (dir && !(f->fw_flg & IP_FW_F_OUT))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Fragments */
|
|
|
|
if ((f->fw_flg & IP_FW_F_FRAG) && !(ip->ip_off & IP_OFFMASK))
|
|
|
|
continue;
|
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
/* If src-addr doesn't match, not this rule. */
|
|
|
|
if ((src.s_addr & f->fw_smsk.s_addr) != f->fw_src.s_addr)
|
|
|
|
continue;
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
/* If dest-addr doesn't match, not this rule. */
|
|
|
|
if ((dst.s_addr & f->fw_dmsk.s_addr) != f->fw_dst.s_addr)
|
|
|
|
continue;
|
1994-12-12 17:20:55 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
/* If a i/f name was specified, and we don't know */
|
|
|
|
if ((f->fw_flg & IP_FW_F_IFNAME) && !rif)
|
|
|
|
continue;
|
1995-07-04 05:39:03 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
/* If a i/f name was specified, check it */
|
|
|
|
if ((f->fw_flg & IP_FW_F_IFNAME) && f->fw_via_name[0]) {
|
1995-07-04 05:39:03 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
/* Not same unit, don't match */
|
|
|
|
if (rif->if_unit != f->fw_via_unit)
|
|
|
|
continue;
|
1994-11-28 12:35:14 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
/* Not same name */
|
|
|
|
if (strncmp(rif->if_name, f->fw_via_name, FW_IFNLEN))
|
|
|
|
continue;
|
1995-07-04 05:39:03 +00:00
|
|
|
}
|
1995-02-24 14:33:54 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
/* If a i/f addr was specified, check it */
|
1996-02-26 15:28:15 +00:00
|
|
|
if (!(f->fw_flg & IP_FW_F_IFNAME) && f->fw_via_ip.s_addr) {
|
1996-02-23 20:11:37 +00:00
|
|
|
int match = 0;
|
1995-07-04 05:39:03 +00:00
|
|
|
|
|
|
|
for (ia_p = ia; ia_p != NULL; ia_p = ia_p->ifa_next) {
|
1996-02-23 15:47:58 +00:00
|
|
|
if ((ia_p->ifa_addr == NULL))
|
|
|
|
continue;
|
|
|
|
if (ia_p->ifa_addr->sa_family != AF_INET)
|
1995-07-04 05:39:03 +00:00
|
|
|
continue;
|
|
|
|
ia_i.s_addr =
|
1996-02-23 15:47:58 +00:00
|
|
|
((struct sockaddr_in *)
|
|
|
|
(ia_p->ifa_addr))->sin_addr.s_addr;
|
|
|
|
if (ia_i.s_addr != f->fw_via_ip.s_addr)
|
|
|
|
continue;
|
1996-02-23 20:11:37 +00:00
|
|
|
match = 1;
|
|
|
|
break;
|
1995-07-04 05:39:03 +00:00
|
|
|
}
|
1996-02-23 20:11:37 +00:00
|
|
|
if (!match)
|
|
|
|
continue;
|
1995-07-04 05:39:03 +00:00
|
|
|
}
|
1996-02-23 15:47:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check IP options
|
|
|
|
*/
|
|
|
|
if (f->fw_ipopt != f->fw_ipnopt)
|
|
|
|
if (!ipopts_match(ip, f))
|
|
|
|
continue;
|
|
|
|
|
1995-07-04 05:39:03 +00:00
|
|
|
/*
|
1996-02-23 15:47:58 +00:00
|
|
|
* Check protocol
|
1995-07-04 05:39:03 +00:00
|
|
|
*/
|
|
|
|
f_prt = f->fw_flg & IP_FW_F_KIND;
|
1996-02-23 15:47:58 +00:00
|
|
|
|
|
|
|
/* If wildcard, match */
|
|
|
|
if (f_prt == IP_FW_F_ALL)
|
|
|
|
goto got_match;
|
1994-11-16 10:17:11 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
/* If different, dont match */
|
|
|
|
if (prt != f_prt)
|
|
|
|
continue;
|
1994-11-16 10:17:11 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
/* ICMP, done */
|
|
|
|
if (prt == IP_FW_F_ICMP)
|
|
|
|
goto got_match;
|
1995-07-04 05:39:03 +00:00
|
|
|
|
1996-02-24 00:17:35 +00:00
|
|
|
/* Fragments can't match past this point */
|
|
|
|
if (ip->ip_off & IP_OFFMASK)
|
|
|
|
continue;
|
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
/* TCP, a little more checking */
|
|
|
|
if (prt == IP_FW_F_TCP &&
|
|
|
|
(f->fw_tcpf != f->fw_tcpnf) &&
|
|
|
|
(!tcpflg_match(tcp, f)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!port_match(&f->fw_pts[0], f->fw_nsp,
|
|
|
|
src_port, f->fw_flg & IP_FW_F_SRNG))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!port_match(&f->fw_pts[f->fw_nsp], f->fw_ndp,
|
|
|
|
dst_port, f->fw_flg & IP_FW_F_DRNG))
|
|
|
|
continue;
|
1995-07-04 05:39:03 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
got_match:
|
1996-02-24 00:17:35 +00:00
|
|
|
f->fw_pcnt++;
|
|
|
|
f->fw_bcnt+=ip->ip_len;
|
|
|
|
if (f->fw_flg & IP_FW_F_PRN) {
|
|
|
|
if (f->fw_flg & IP_FW_F_ACCEPT)
|
|
|
|
ipfw_report("Accept", f->fw_number, ip);
|
|
|
|
else if (f->fw_flg & IP_FW_F_COUNT)
|
|
|
|
ipfw_report("Count", f->fw_number, ip);
|
|
|
|
else
|
|
|
|
ipfw_report("Deny", f->fw_number, ip);
|
|
|
|
}
|
1996-02-23 15:47:58 +00:00
|
|
|
if (f->fw_flg & IP_FW_F_ACCEPT)
|
1996-02-24 00:17:35 +00:00
|
|
|
return 1;
|
|
|
|
if (f->fw_flg & IP_FW_F_COUNT)
|
|
|
|
continue;
|
|
|
|
break;
|
1996-02-23 15:47:58 +00:00
|
|
|
|
1996-02-24 00:17:35 +00:00
|
|
|
}
|
1995-07-04 05:39:03 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
/*
|
|
|
|
* Don't icmp outgoing packets at all
|
|
|
|
*/
|
|
|
|
if (f != NULL && !dir) {
|
|
|
|
/*
|
|
|
|
* Do not ICMP reply to icmp packets....:) or to packets
|
|
|
|
* rejected by entry without the special ICMP reply flag.
|
|
|
|
*/
|
|
|
|
if ((f_prt != IP_FW_F_ICMP) && (f->fw_flg & IP_FW_F_ICMPRPL)) {
|
|
|
|
if (f_prt == IP_FW_F_ALL)
|
1996-04-03 13:52:20 +00:00
|
|
|
icmp_error(*m, ICMP_UNREACH,
|
1996-02-23 15:47:58 +00:00
|
|
|
ICMP_UNREACH_HOST, 0L, 0);
|
|
|
|
else
|
1996-04-03 13:52:20 +00:00
|
|
|
icmp_error(*m, ICMP_UNREACH,
|
1996-02-23 15:47:58 +00:00
|
|
|
ICMP_UNREACH_PORT, 0L, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
1995-07-04 05:39:03 +00:00
|
|
|
}
|
1996-04-03 13:52:20 +00:00
|
|
|
m_freem(*m);
|
1996-02-23 15:47:58 +00:00
|
|
|
return 0;
|
1995-07-04 05:39:03 +00:00
|
|
|
}
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1995-07-04 05:39:03 +00:00
|
|
|
static int
|
|
|
|
add_entry(chainptr, frwl)
|
1996-02-23 15:47:58 +00:00
|
|
|
struct ip_fw_head *chainptr;
|
1995-07-04 05:39:03 +00:00
|
|
|
struct ip_fw *frwl;
|
|
|
|
{
|
1996-02-23 15:47:58 +00:00
|
|
|
struct ip_fw *ftmp = 0;
|
|
|
|
struct ip_fw_chain *fwc = 0, *fcp, *fcpl = 0;
|
|
|
|
u_short nbr = 0;
|
1995-07-04 05:39:03 +00:00
|
|
|
int s;
|
|
|
|
|
|
|
|
s = splnet();
|
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
fwc = malloc(sizeof *fwc, M_IPFW, M_DONTWAIT);
|
|
|
|
ftmp = malloc(sizeof *ftmp, M_IPFW, M_DONTWAIT);
|
|
|
|
if (!fwc || !ftmp) {
|
|
|
|
dprintf(("ip_fw_ctl: malloc said no\n"));
|
|
|
|
if (fwc) free(fwc, M_IPFW);
|
|
|
|
if (ftmp) free(ftmp, M_IPFW);
|
1995-07-04 05:39:03 +00:00
|
|
|
splx(s);
|
|
|
|
return (ENOSPC);
|
|
|
|
}
|
|
|
|
bcopy(frwl, ftmp, sizeof(struct ip_fw));
|
|
|
|
ftmp->fw_pcnt = 0L;
|
|
|
|
ftmp->fw_bcnt = 0L;
|
1996-02-23 15:47:58 +00:00
|
|
|
fwc->rule = ftmp;
|
|
|
|
|
|
|
|
if (!chainptr->lh_first) {
|
|
|
|
LIST_INSERT_HEAD(chainptr, fwc, chain);
|
|
|
|
} else if (ftmp->fw_number == (u_short)-1) {
|
|
|
|
if (fwc) free(fwc, M_IPFW);
|
|
|
|
if (ftmp) free(ftmp, M_IPFW);
|
|
|
|
splx(s);
|
|
|
|
return (EINVAL);
|
|
|
|
} else {
|
|
|
|
nbr=0;
|
|
|
|
for (fcp = chainptr->lh_first; fcp; fcp = fcp->chain.le_next)
|
1996-02-24 00:17:35 +00:00
|
|
|
if (fcp->rule->fw_number == (u_short)-1 ||
|
|
|
|
( ftmp->fw_number &&
|
|
|
|
fcp->rule->fw_number > ftmp->fw_number)) {
|
1996-02-23 15:47:58 +00:00
|
|
|
if (!ftmp->fw_number)
|
|
|
|
ftmp->fw_number = nbr + 100;
|
|
|
|
if (fcpl) {
|
|
|
|
LIST_INSERT_AFTER(fcpl, fwc, chain);
|
1995-07-04 05:39:03 +00:00
|
|
|
} else {
|
1996-02-23 15:47:58 +00:00
|
|
|
LIST_INSERT_HEAD(chainptr, fwc, chain);
|
1995-07-04 05:39:03 +00:00
|
|
|
}
|
1996-02-23 15:47:58 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
nbr=fcp->rule->fw_number;
|
|
|
|
fcpl = fcp;
|
1994-10-31 23:58:04 +00:00
|
|
|
}
|
1995-07-04 05:39:03 +00:00
|
|
|
}
|
1996-02-23 15:47:58 +00:00
|
|
|
|
1995-07-04 05:39:03 +00:00
|
|
|
splx(s);
|
|
|
|
return (0);
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
|
|
|
|
1995-07-04 05:39:03 +00:00
|
|
|
static int
|
|
|
|
del_entry(chainptr, frwl)
|
1996-02-23 15:47:58 +00:00
|
|
|
struct ip_fw_head *chainptr;
|
1995-07-04 05:39:03 +00:00
|
|
|
struct ip_fw *frwl;
|
1994-10-28 15:09:49 +00:00
|
|
|
{
|
1996-02-23 15:47:58 +00:00
|
|
|
struct ip_fw_chain *fcp;
|
1995-07-04 05:39:03 +00:00
|
|
|
int s;
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1995-07-04 05:39:03 +00:00
|
|
|
s = splnet();
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
fcp = chainptr->lh_first;
|
|
|
|
if (fcp->rule->fw_number == (u_short)-1)
|
1995-07-04 05:39:03 +00:00
|
|
|
return (EINVAL);
|
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
for (; fcp; fcp = fcp->chain.le_next) {
|
|
|
|
if (fcp->rule->fw_number == frwl->fw_number) {
|
|
|
|
LIST_REMOVE(fcp, chain);
|
|
|
|
splx(s);
|
|
|
|
free(fcp->rule, M_IPFW);
|
|
|
|
free(fcp, M_IPFW);
|
1996-02-24 00:17:35 +00:00
|
|
|
return 0;
|
1995-07-04 05:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
1994-11-07 10:01:32 +00:00
|
|
|
splx(s);
|
1996-02-23 15:47:58 +00:00
|
|
|
return (EINVAL);
|
1994-12-13 15:57:34 +00:00
|
|
|
}
|
|
|
|
|
1995-11-14 20:34:56 +00:00
|
|
|
static struct ip_fw *
|
1994-11-16 10:17:11 +00:00
|
|
|
check_ipfw_struct(m)
|
1995-07-04 05:39:03 +00:00
|
|
|
struct mbuf *m;
|
1994-11-16 10:17:11 +00:00
|
|
|
{
|
1995-07-04 05:39:03 +00:00
|
|
|
struct ip_fw *frwl;
|
1994-11-16 10:17:11 +00:00
|
|
|
|
1995-07-04 05:39:03 +00:00
|
|
|
if (m->m_len != sizeof(struct ip_fw)) {
|
1996-02-23 15:47:58 +00:00
|
|
|
dprintf(("ip_fw_ctl: len=%d, want %d\n", m->m_len,
|
|
|
|
sizeof(struct ip_fw)));
|
1995-07-04 05:39:03 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
frwl = mtod(m, struct ip_fw *);
|
1994-11-16 10:17:11 +00:00
|
|
|
|
1995-07-04 05:39:03 +00:00
|
|
|
if ((frwl->fw_flg & ~IP_FW_F_MASK) != 0) {
|
1996-02-23 15:47:58 +00:00
|
|
|
dprintf(("ip_fw_ctl: undefined flag bits set (flags=%x)\n",
|
|
|
|
frwl->fw_flg));
|
1995-07-04 05:39:03 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
1996-02-24 00:17:35 +00:00
|
|
|
|
|
|
|
/* If neither In nor Out, then both */
|
|
|
|
if (!(frwl->fw_flg & (IP_FW_F_IN | IP_FW_F_OUT)))
|
|
|
|
frwl->fw_flg |= IP_FW_F_IN | IP_FW_F_OUT;
|
|
|
|
|
1995-07-04 05:39:03 +00:00
|
|
|
if ((frwl->fw_flg & IP_FW_F_SRNG) && frwl->fw_nsp < 2) {
|
1996-02-23 15:47:58 +00:00
|
|
|
dprintf(("ip_fw_ctl: src range set but n_src_p=%d\n",
|
|
|
|
frwl->fw_nsp));
|
1995-07-04 05:39:03 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if ((frwl->fw_flg & IP_FW_F_DRNG) && frwl->fw_ndp < 2) {
|
1996-02-23 15:47:58 +00:00
|
|
|
dprintf(("ip_fw_ctl: dst range set but n_dst_p=%d\n",
|
|
|
|
frwl->fw_ndp));
|
1995-07-04 05:39:03 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (frwl->fw_nsp + frwl->fw_ndp > IP_FW_MAX_PORTS) {
|
1996-02-23 15:47:58 +00:00
|
|
|
dprintf(("ip_fw_ctl: too many ports (%d+%d)\n",
|
|
|
|
frwl->fw_nsp, frwl->fw_ndp));
|
1995-07-04 05:39:03 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
1994-11-16 10:17:11 +00:00
|
|
|
#if 0
|
1995-07-04 05:39:03 +00:00
|
|
|
if ((frwl->fw_flg & IP_FW_F_KIND) == IP_FW_F_ICMP) {
|
1996-02-23 15:47:58 +00:00
|
|
|
dprintf(("ip_fw_ctl: request for unsupported ICMP frwling\n"));
|
1995-07-04 05:39:03 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
1994-11-16 10:17:11 +00:00
|
|
|
#endif
|
1995-07-04 05:39:03 +00:00
|
|
|
return frwl;
|
1994-11-16 10:17:11 +00:00
|
|
|
}
|
|
|
|
|
1994-10-28 15:09:49 +00:00
|
|
|
int
|
1996-02-24 13:38:28 +00:00
|
|
|
ip_fw_ctl(stage, mm)
|
1995-07-04 05:39:03 +00:00
|
|
|
int stage;
|
1996-02-24 13:38:28 +00:00
|
|
|
struct mbuf **mm;
|
1994-10-28 15:09:49 +00:00
|
|
|
{
|
1996-02-24 13:38:28 +00:00
|
|
|
int error;
|
|
|
|
struct mbuf *m;
|
|
|
|
|
|
|
|
if (stage == IP_FW_GET) {
|
|
|
|
struct ip_fw_chain *fcp = ip_fw_chain.lh_first;
|
|
|
|
*mm = m = m_get(M_WAIT, MT_SOOPTS);
|
|
|
|
for (; fcp; fcp = fcp->chain.le_next) {
|
|
|
|
memcpy(m->m_data, fcp->rule, sizeof *(fcp->rule));
|
|
|
|
m->m_len = sizeof *(fcp->rule);
|
|
|
|
m->m_next = m_get(M_WAIT, MT_SOOPTS);
|
|
|
|
m = m->m_next;
|
|
|
|
m->m_len = 0;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
m = *mm;
|
1995-07-04 05:39:03 +00:00
|
|
|
if (stage == IP_FW_FLUSH) {
|
1996-02-23 15:47:58 +00:00
|
|
|
while (ip_fw_chain.lh_first != NULL &&
|
|
|
|
ip_fw_chain.lh_first->rule->fw_number != (u_short)-1) {
|
|
|
|
struct ip_fw_chain *fcp = ip_fw_chain.lh_first;
|
|
|
|
LIST_REMOVE(ip_fw_chain.lh_first, chain);
|
|
|
|
free(fcp->rule, M_IPFW);
|
|
|
|
free(fcp, M_IPFW);
|
|
|
|
}
|
1996-02-24 13:38:28 +00:00
|
|
|
if (m) (void)m_free(m);
|
1996-02-23 15:47:58 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (stage == IP_FW_ZERO) {
|
|
|
|
struct ip_fw_chain *fcp;
|
|
|
|
for (fcp = ip_fw_chain.lh_first; fcp; fcp = fcp->chain.le_next)
|
|
|
|
fcp->rule->fw_bcnt = fcp->rule->fw_pcnt = 0;
|
1996-02-24 13:38:28 +00:00
|
|
|
if (m) (void)m_free(m);
|
1995-07-04 05:39:03 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (m == NULL) {
|
|
|
|
printf("ip_fw_ctl: NULL mbuf ptr\n");
|
1994-11-16 10:17:11 +00:00
|
|
|
return (EINVAL);
|
1995-07-04 05:39:03 +00:00
|
|
|
}
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
if (stage == IP_FW_ADD || stage == IP_FW_DEL) {
|
|
|
|
struct ip_fw *frwl = check_ipfw_struct(m);
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1996-02-24 13:38:28 +00:00
|
|
|
if (!frwl) {
|
|
|
|
if (m) (void)m_free(m);
|
1994-11-16 10:17:11 +00:00
|
|
|
return (EINVAL);
|
1996-02-24 13:38:28 +00:00
|
|
|
}
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1996-02-23 15:47:58 +00:00
|
|
|
if (stage == IP_FW_ADD)
|
1996-02-24 13:38:28 +00:00
|
|
|
error = add_entry(&ip_fw_chain, frwl);
|
1996-02-23 15:47:58 +00:00
|
|
|
else
|
1996-02-24 13:38:28 +00:00
|
|
|
error = del_entry(&ip_fw_chain, frwl);
|
|
|
|
if (m) (void)m_free(m);
|
|
|
|
return error;
|
1995-07-04 05:39:03 +00:00
|
|
|
}
|
1996-02-23 15:47:58 +00:00
|
|
|
dprintf(("ip_fw_ctl: unknown request %d\n", stage));
|
1996-02-24 13:38:28 +00:00
|
|
|
if (m) (void)m_free(m);
|
1995-07-04 05:39:03 +00:00
|
|
|
return (EINVAL);
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
1996-02-23 15:47:58 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
ip_fw_init(void)
|
|
|
|
{
|
|
|
|
struct ip_fw deny;
|
|
|
|
|
1996-02-24 13:38:28 +00:00
|
|
|
ip_fw_chk_ptr = ip_fw_chk;
|
|
|
|
ip_fw_ctl_ptr = ip_fw_ctl;
|
1996-02-23 15:47:58 +00:00
|
|
|
LIST_INIT(&ip_fw_chain);
|
|
|
|
|
|
|
|
bzero(&deny, sizeof deny);
|
|
|
|
deny.fw_flg = IP_FW_F_ALL;
|
|
|
|
deny.fw_number = (u_short)-1;
|
|
|
|
add_entry(&ip_fw_chain, &deny);
|
|
|
|
|
|
|
|
printf("IP firewall initialized\n");
|
|
|
|
}
|
1996-02-24 13:38:28 +00:00
|
|
|
|
|
|
|
#ifdef ACTUALLY_LKM_NOT_KERNEL
|
|
|
|
|
|
|
|
#include <sys/exec.h>
|
|
|
|
#include <sys/sysent.h>
|
|
|
|
#include <sys/lkm.h>
|
|
|
|
|
|
|
|
MOD_MISC(ipfw);
|
|
|
|
|
|
|
|
static int
|
|
|
|
ipfw_load(struct lkm_table *lkmtp, int cmd)
|
|
|
|
{
|
|
|
|
int s=splnet();
|
|
|
|
struct ip_fw deny;
|
|
|
|
|
|
|
|
old_chk_ptr = ip_fw_chk_ptr;
|
|
|
|
old_ctl_ptr = ip_fw_ctl_ptr;
|
|
|
|
ip_fw_chk_ptr = ip_fw_chk;
|
|
|
|
ip_fw_ctl_ptr = ip_fw_ctl;
|
|
|
|
|
|
|
|
LIST_INIT(&ip_fw_chain);
|
|
|
|
bzero(&deny, sizeof deny);
|
|
|
|
deny.fw_flg = IP_FW_F_ALL;
|
|
|
|
deny.fw_number = (u_short)-1;
|
|
|
|
add_entry(&ip_fw_chain, &deny);
|
|
|
|
|
|
|
|
splx(s);
|
|
|
|
printf("IP firewall loaded\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ipfw_unload(struct lkm_table *lkmtp, int cmd)
|
|
|
|
{
|
|
|
|
int s=splnet();
|
|
|
|
|
|
|
|
ip_fw_chk_ptr = old_chk_ptr;
|
|
|
|
ip_fw_ctl_ptr = old_ctl_ptr;
|
|
|
|
|
|
|
|
while (ip_fw_chain.lh_first != NULL) {
|
|
|
|
struct ip_fw_chain *fcp = ip_fw_chain.lh_first;
|
|
|
|
LIST_REMOVE(ip_fw_chain.lh_first, chain);
|
|
|
|
free(fcp->rule, M_IPFW);
|
|
|
|
free(fcp, M_IPFW);
|
|
|
|
}
|
|
|
|
|
|
|
|
splx(s);
|
|
|
|
printf("IP firewall unloaded\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ipfw_mod(struct lkm_table *lkmtp, int cmd, int ver)
|
|
|
|
{
|
|
|
|
DISPATCH(lkmtp, cmd, ver, ipfw_load, ipfw_unload, lkm_nullcmd);
|
|
|
|
}
|
|
|
|
#endif
|