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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implement IP packet firewall
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/domain.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/errno.h>
|
1995-05-30 08:16:23 +00:00
|
|
|
#include <sys/time.h>
|
1994-10-28 15:09:49 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
|
1994-10-28 15:09:49 +00:00
|
|
|
#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>
|
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
#ifdef IPFIREWALL_DEBUG
|
1994-11-28 12:35:14 +00:00
|
|
|
#define dprintf1(a) printf(a)
|
|
|
|
#define dprintf2(a1,a2) printf(a1,a2)
|
|
|
|
#define dprintf3(a1,a2,a3) printf(a1,a2,a3)
|
|
|
|
#define dprintf4(a1,a2,a3,a4) printf(a1,a2,a3,a4)
|
|
|
|
#else
|
1995-05-30 08:16:23 +00:00
|
|
|
#define dprintf1(a)
|
1994-11-28 12:35:14 +00:00
|
|
|
#define dprintf2(a1,a2)
|
|
|
|
#define dprintf3(a1,a2,a3)
|
|
|
|
#define dprintf4(a1,a2,a3,a4)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
#ifdef IPFIREWALL_DEBUG
|
|
|
|
#define dprint_ip(a) print_ip(a)
|
|
|
|
#else
|
1995-05-30 08:16:23 +00:00
|
|
|
#define dprint_ip(a)
|
1994-11-28 12:35:14 +00:00
|
|
|
#endif
|
|
|
|
|
1994-10-28 15:09:49 +00:00
|
|
|
|
|
|
|
/*
|
1995-07-04 03:35:20 +00:00
|
|
|
* Returns TRUE if the port is matched by the vector, FALSE otherwise
|
1994-10-28 15:09:49 +00:00
|
|
|
*/
|
|
|
|
inline
|
|
|
|
int port_match(portptr,nports,port,range_flag)
|
|
|
|
u_short *portptr;
|
|
|
|
int nports;
|
|
|
|
u_short port;
|
|
|
|
int range_flag;
|
|
|
|
{
|
1994-11-16 10:17:11 +00:00
|
|
|
if (!nports)
|
1995-07-04 03:35:20 +00:00
|
|
|
return TRUE;
|
1994-11-28 12:35:14 +00:00
|
|
|
if (range_flag) {
|
|
|
|
if (portptr[0]<=port && port<=portptr[1]) {
|
1995-07-04 03:35:20 +00:00
|
|
|
return TRUE;
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
1994-11-28 12:35:14 +00:00
|
|
|
nports-=2;
|
|
|
|
portptr+=2;
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
1994-11-28 12:35:14 +00:00
|
|
|
while (nports-->0) {
|
|
|
|
if (*portptr++==port) {
|
1995-07-04 03:35:20 +00:00
|
|
|
return TRUE;
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
1995-07-04 03:35:20 +00:00
|
|
|
return FALSE;
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1995-07-04 03:35:20 +00:00
|
|
|
* Returns TRUE if it should be accepted, FALSE otherwise.
|
1994-10-28 15:09:49 +00:00
|
|
|
*/
|
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
#ifdef IPFIREWALL
|
1994-12-12 17:20:55 +00:00
|
|
|
int ip_fw_chk(ip,rif,chain)
|
|
|
|
struct ip *ip;
|
|
|
|
struct ifnet *rif;
|
|
|
|
struct ip_fw *chain;
|
1994-10-28 15:09:49 +00:00
|
|
|
{
|
1994-11-28 12:35:14 +00:00
|
|
|
register struct ip_fw *f;
|
|
|
|
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);
|
1994-12-12 17:20:55 +00:00
|
|
|
struct ifaddr *ia=NULL, *ia_p;
|
|
|
|
struct in_addr src, dst, ia_i;
|
|
|
|
struct mbuf *m;
|
|
|
|
u_short src_port=0, dst_port=0;
|
|
|
|
u_short f_prt=0, prt;
|
|
|
|
char notcpsyn=1;
|
1994-11-28 12:35:14 +00:00
|
|
|
|
1994-12-12 17:20:55 +00:00
|
|
|
/*
|
|
|
|
* If the chain is empty
|
1995-05-30 08:16:23 +00:00
|
|
|
* allow any packet-this is equal
|
1994-12-12 17:20:55 +00:00
|
|
|
* to disabling firewall.
|
|
|
|
*/
|
1995-05-30 08:16:23 +00:00
|
|
|
if (!chain)
|
1995-07-04 03:35:20 +00:00
|
|
|
return TRUE;
|
1994-12-12 17:20:55 +00:00
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
/*
|
1994-12-12 17:20:55 +00:00
|
|
|
* This way we handle fragmented packets.
|
|
|
|
* we ignore all fragments but the first one
|
|
|
|
* so the whole packet can't be reassembled.
|
|
|
|
* This way we relay on the full info which
|
|
|
|
* stored only in first packet.
|
|
|
|
*/
|
|
|
|
if (ip->ip_off&IP_OFFMASK)
|
1995-07-04 03:35:20 +00:00
|
|
|
return TRUE;
|
1994-10-28 15:09:49 +00:00
|
|
|
|
|
|
|
src = ip->ip_src;
|
|
|
|
dst = ip->ip_dst;
|
|
|
|
|
1994-12-12 17:20:55 +00:00
|
|
|
/*
|
|
|
|
* If we got interface from
|
|
|
|
* which packet came-store
|
|
|
|
* pointer to it's first adress
|
|
|
|
*/
|
|
|
|
if (rif)
|
|
|
|
ia=rif->if_addrlist;
|
|
|
|
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf1("Packet ");
|
1994-10-28 15:09:49 +00:00
|
|
|
switch(ip->ip_p) {
|
1994-11-28 12:35:14 +00:00
|
|
|
case IPPROTO_TCP:
|
|
|
|
dprintf1("TCP ");
|
|
|
|
src_port=ntohs(tcp->th_sport);
|
|
|
|
dst_port=ntohs(tcp->th_dport);
|
|
|
|
if (tcp->th_flags&TH_SYN)
|
|
|
|
notcpsyn=0; /* We *DO* have SYN ,value FALSE */
|
1994-12-12 17:20:55 +00:00
|
|
|
prt=IP_FW_F_TCP;
|
1994-11-28 12:35:14 +00:00
|
|
|
break;
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
dprintf1("UDP ");
|
|
|
|
src_port=ntohs(udp->uh_sport);
|
|
|
|
dst_port=ntohs(udp->uh_dport);
|
1994-12-12 17:20:55 +00:00
|
|
|
prt=IP_FW_F_UDP;
|
1994-11-28 12:35:14 +00:00
|
|
|
break;
|
|
|
|
case IPPROTO_ICMP:
|
|
|
|
dprintf2("ICMP:%u ",icmp->icmp_type);
|
1994-12-12 17:20:55 +00:00
|
|
|
prt=IP_FW_F_ICMP;
|
1994-11-28 12:35:14 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dprintf2("p=%d ",ip->ip_p);
|
1994-12-12 17:20:55 +00:00
|
|
|
prt=IP_FW_F_ALL;
|
1994-11-28 12:35:14 +00:00
|
|
|
break;
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
1994-11-28 12:35:14 +00:00
|
|
|
dprint_ip(ip->ip_src);
|
1994-11-16 10:17:11 +00:00
|
|
|
if (ip->ip_p==IPPROTO_TCP || ip->ip_p==IPPROTO_UDP) {
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf2(":%d ",src_port);
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
1994-11-28 12:35:14 +00:00
|
|
|
dprint_ip(ip->ip_dst);
|
|
|
|
if (ip->ip_p==IPPROTO_TCP || ip->ip_p==IPPROTO_UDP) {
|
|
|
|
dprintf2(":%d ",dst_port);
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf1("\n");
|
|
|
|
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
for (f=chain;f;f=f->fw_next)
|
1995-02-24 14:33:54 +00:00
|
|
|
if ((src.s_addr&f->fw_smsk.s_addr)==f->fw_src.s_addr
|
|
|
|
&& (dst.s_addr&f->fw_dmsk.s_addr)==f->fw_dst.s_addr) {
|
|
|
|
|
|
|
|
if (!rif)
|
|
|
|
goto via_match;
|
|
|
|
|
|
|
|
if (f->fw_flg&IP_FW_F_IFNAME) {
|
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
if (!f->fw_via_name[0])
|
1995-02-24 14:33:54 +00:00
|
|
|
goto via_match; /* No name/unit set,match any */
|
|
|
|
|
|
|
|
if (rif->if_unit==f->fw_via_unit &&
|
|
|
|
!strncmp(rif->if_name,f->fw_via_name,FW_IFNLEN))
|
|
|
|
goto via_match;
|
|
|
|
} else {
|
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
if (!f->fw_via_ip.s_addr)
|
1995-02-24 14:33:54 +00:00
|
|
|
goto via_match; /* No via ip set,match any */
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1995-02-24 14:33:54 +00:00
|
|
|
for (ia_p=ia;ia_p;ia_p=ia_p->ifa_next) {
|
|
|
|
if (!ia_p->ifa_addr||ia_p->ifa_addr->sa_family!=AF_INET)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ia_i.s_addr=(((struct sockaddr_in *)\
|
|
|
|
(ia_p->ifa_addr))->sin_addr.s_addr);
|
|
|
|
if (ia_i.s_addr==f->fw_via_ip.s_addr)
|
|
|
|
goto via_match;
|
1994-12-12 17:20:55 +00:00
|
|
|
}
|
1995-02-24 14:33:54 +00:00
|
|
|
|
|
|
|
}
|
1995-05-30 08:16:23 +00:00
|
|
|
/*
|
1995-02-24 14:33:54 +00:00
|
|
|
* If we got here,no "via"'s matched,so
|
|
|
|
* we should continue to the next firewall entry.
|
|
|
|
*/
|
1995-05-30 08:16:23 +00:00
|
|
|
continue;
|
1994-12-12 17:20:55 +00:00
|
|
|
via_match:
|
1994-12-13 15:57:34 +00:00
|
|
|
f_prt=f->fw_flg&IP_FW_F_KIND;
|
1994-12-12 17:20:55 +00:00
|
|
|
if (f_prt==IP_FW_F_ALL) {
|
1994-11-16 10:17:11 +00:00
|
|
|
/* Universal frwl - we've got a match! */
|
1994-11-28 12:35:14 +00:00
|
|
|
goto got_match;
|
1994-10-28 15:09:49 +00:00
|
|
|
} else {
|
1994-11-16 10:17:11 +00:00
|
|
|
/*
|
1995-05-30 08:16:23 +00:00
|
|
|
* This is actually buggy as if you set SYN flag
|
|
|
|
* on UDp or ICMP firewall it will never work,but
|
1994-11-28 12:35:14 +00:00
|
|
|
* actually it is a concern of software which sets
|
|
|
|
* firewall entries.
|
1994-11-16 10:17:11 +00:00
|
|
|
*/
|
1994-12-13 15:57:34 +00:00
|
|
|
if (f->fw_flg&IP_FW_F_TCPSYN && notcpsyn)
|
1994-11-28 12:35:14 +00:00
|
|
|
continue;
|
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
/*
|
1994-11-28 12:35:14 +00:00
|
|
|
* Specific firewall - packet's
|
|
|
|
* protocol must match firewall's
|
1994-11-16 10:17:11 +00:00
|
|
|
*/
|
1994-12-12 17:20:55 +00:00
|
|
|
if (prt==f_prt) {
|
1994-11-16 10:17:11 +00:00
|
|
|
|
1994-12-12 17:20:55 +00:00
|
|
|
if (prt==IP_FW_F_ICMP ||
|
1994-12-13 15:57:34 +00:00
|
|
|
(port_match(&f->fw_pts[0],f->fw_nsp,src_port,
|
|
|
|
f->fw_flg&IP_FW_F_SRNG) &&
|
|
|
|
port_match(&f->fw_pts[f->fw_nsp],f->fw_ndp,dst_port,
|
|
|
|
f->fw_flg&IP_FW_F_DRNG))) {
|
1994-11-28 12:35:14 +00:00
|
|
|
goto got_match;
|
1994-11-16 10:17:11 +00:00
|
|
|
} /* Ports match */
|
|
|
|
} /* Proto matches */
|
|
|
|
} /* ALL/Specific */
|
|
|
|
} /* IP addr/mask matches */
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
/*
|
|
|
|
* If we get here then none of the firewalls matched.
|
|
|
|
* So now we relay on policy defined by user-unmatched packet can
|
|
|
|
* be ever accepted or rejected...
|
|
|
|
*/
|
1994-11-28 12:35:14 +00:00
|
|
|
f=(struct ip_fw *)NULL;
|
|
|
|
if (ip_fw_policy&IP_FW_P_DENY)
|
1994-11-16 10:17:11 +00:00
|
|
|
goto bad_packet;
|
1994-11-28 12:35:14 +00:00
|
|
|
else
|
1995-07-04 03:35:20 +00:00
|
|
|
return TRUE;
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1994-11-28 12:35:14 +00:00
|
|
|
got_match:
|
1994-10-31 23:58:04 +00:00
|
|
|
#ifdef IPFIREWALL_VERBOSE
|
1994-11-08 12:47:29 +00:00
|
|
|
/*
|
|
|
|
* VERY ugly piece of code which actually
|
1994-11-16 10:17:11 +00:00
|
|
|
* makes kernel printf for denied packets...
|
1994-11-08 12:47:29 +00:00
|
|
|
*/
|
1994-12-13 15:57:34 +00:00
|
|
|
if (f->fw_flg&IP_FW_F_PRN) {
|
|
|
|
if (f->fw_flg&IP_FW_F_ACCEPT)
|
1994-11-28 12:35:14 +00:00
|
|
|
printf("Accept ");
|
|
|
|
else
|
|
|
|
printf("Deny ");
|
1994-10-31 23:58:04 +00:00
|
|
|
switch(ip->ip_p) {
|
1994-11-16 10:17:11 +00:00
|
|
|
case IPPROTO_TCP:
|
|
|
|
printf("TCP ");
|
|
|
|
break;
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
printf("UDP ");
|
|
|
|
break;
|
|
|
|
case IPPROTO_ICMP:
|
1994-11-28 12:35:14 +00:00
|
|
|
printf("ICMP:%u ",icmp->icmp_type);
|
1994-11-16 10:17:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("p=%d ",ip->ip_p);
|
|
|
|
break;
|
1994-10-31 23:58:04 +00:00
|
|
|
}
|
|
|
|
print_ip(ip->ip_src);
|
1995-05-30 08:16:23 +00:00
|
|
|
if (ip->ip_p==IPPROTO_TCP || ip->ip_p==IPPROTO_UDP)
|
1994-11-28 12:35:14 +00:00
|
|
|
printf(":%d",src_port);
|
|
|
|
printf(" ");
|
1994-10-31 23:58:04 +00:00
|
|
|
print_ip(ip->ip_dst);
|
1994-11-28 12:35:14 +00:00
|
|
|
if (ip->ip_p==IPPROTO_TCP || ip->ip_p==IPPROTO_UDP)
|
|
|
|
printf(":%d",dst_port);
|
1994-10-31 23:58:04 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
1994-10-28 15:09:49 +00:00
|
|
|
#endif
|
1994-12-13 15:57:34 +00:00
|
|
|
if (f->fw_flg&IP_FW_F_ACCEPT)
|
1995-07-04 03:35:20 +00:00
|
|
|
return TRUE;
|
1994-11-28 12:35:14 +00:00
|
|
|
|
|
|
|
bad_packet:
|
1995-07-04 03:35:20 +00:00
|
|
|
m = dtom(ip);
|
|
|
|
if (f != NULL) {
|
|
|
|
/*
|
|
|
|
* 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)) {
|
|
|
|
m_freem(m);
|
|
|
|
return FALSE;
|
|
|
|
}
|
1994-12-12 17:20:55 +00:00
|
|
|
if (f_prt==IP_FW_F_ALL)
|
1994-11-28 12:35:14 +00:00
|
|
|
icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0L, 0);
|
|
|
|
else
|
|
|
|
icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0L, 0);
|
1995-07-04 03:35:20 +00:00
|
|
|
return FALSE;
|
1994-11-28 12:35:14 +00:00
|
|
|
}
|
1995-06-27 17:26:27 +00:00
|
|
|
m_freem(m);
|
1995-07-04 03:35:20 +00:00
|
|
|
return FALSE;
|
1994-11-16 10:17:11 +00:00
|
|
|
}
|
|
|
|
#endif /* IPFIREWALL */
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
#ifdef IPACCT
|
1994-12-12 17:20:55 +00:00
|
|
|
void ip_acct_cnt(ip,rif,chain,nh_conv)
|
|
|
|
struct ip *ip;
|
|
|
|
struct ifnet *rif;
|
|
|
|
struct ip_fw *chain;
|
1994-11-16 10:17:11 +00:00
|
|
|
int nh_conv;
|
|
|
|
{
|
1994-11-28 12:35:14 +00:00
|
|
|
register struct ip_fw *f;
|
|
|
|
struct tcphdr *tcp=(struct tcphdr *)((u_long *)ip+ip->ip_hl);
|
|
|
|
struct udphdr *udp=(struct udphdr *)((u_long *)ip+ip->ip_hl);
|
1994-12-12 17:20:55 +00:00
|
|
|
struct ifaddr *ia=NULL, *ia_p;
|
|
|
|
struct in_addr src, dst, ia_i;
|
|
|
|
u_short src_port=0, dst_port=0;
|
|
|
|
u_short f_prt, prt=0;
|
|
|
|
char rev=0;
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
if (!chain)
|
|
|
|
return;
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1994-12-12 17:20:55 +00:00
|
|
|
if (ip->ip_off&IP_OFFMASK)
|
|
|
|
return;
|
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
src = ip->ip_src;
|
|
|
|
dst = ip->ip_dst;
|
|
|
|
|
1994-12-12 17:20:55 +00:00
|
|
|
if (rif)
|
|
|
|
ia=rif->if_addrlist;
|
|
|
|
|
1994-11-28 12:35:14 +00:00
|
|
|
switch(ip->ip_p) {
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
src_port=ntohs(tcp->th_sport);
|
|
|
|
dst_port=ntohs(tcp->th_dport);
|
1994-12-12 17:20:55 +00:00
|
|
|
prt=IP_FW_F_TCP;
|
1994-11-28 12:35:14 +00:00
|
|
|
break;
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
src_port=ntohs(udp->uh_sport);
|
|
|
|
dst_port=ntohs(udp->uh_dport);
|
1994-12-12 17:20:55 +00:00
|
|
|
prt=IP_FW_F_UDP;
|
1994-11-28 12:35:14 +00:00
|
|
|
break;
|
|
|
|
case IPPROTO_ICMP:
|
1994-12-12 17:20:55 +00:00
|
|
|
prt=IP_FW_F_ICMP;
|
1994-11-28 12:35:14 +00:00
|
|
|
break;
|
|
|
|
default:
|
1994-12-12 17:20:55 +00:00
|
|
|
prt=IP_FW_F_ALL;
|
1994-11-28 12:35:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1994-12-13 15:57:34 +00:00
|
|
|
for (f=chain;f;f=f->fw_next) {
|
1995-02-24 14:33:54 +00:00
|
|
|
if ((src.s_addr&f->fw_smsk.s_addr)==f->fw_src.s_addr
|
|
|
|
&& (dst.s_addr&f->fw_dmsk.s_addr)==f->fw_dst.s_addr) {
|
|
|
|
rev=0;
|
|
|
|
goto addr_match;
|
|
|
|
}
|
|
|
|
if ((f->fw_flg&IP_FW_F_BIDIR) &&
|
|
|
|
((src.s_addr&f->fw_smsk.s_addr)==f->fw_dst.s_addr
|
1995-05-30 08:16:23 +00:00
|
|
|
&& (dst.s_addr&f->fw_dmsk.s_addr)==f->fw_src.s_addr)) {
|
1995-02-24 14:33:54 +00:00
|
|
|
rev=1;
|
|
|
|
goto addr_match;
|
|
|
|
}
|
|
|
|
continue;
|
1994-11-16 10:17:11 +00:00
|
|
|
addr_match:
|
1995-02-24 14:33:54 +00:00
|
|
|
/*
|
|
|
|
* We use here same code for "via" matching
|
1995-05-30 08:16:23 +00:00
|
|
|
* as in firewall.This is wrong and does not do
|
1995-02-24 14:33:54 +00:00
|
|
|
* much use,because in most cases instead of interface
|
|
|
|
* passed NULL pointer.Need to be completely
|
|
|
|
* rewritten.
|
|
|
|
*/
|
|
|
|
if (!rif)
|
|
|
|
goto via_match;
|
|
|
|
|
|
|
|
if (f->fw_flg&IP_FW_F_IFNAME) {
|
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
if (!f->fw_via_name[0])
|
1995-02-24 14:33:54 +00:00
|
|
|
goto via_match; /* No name/unit set,match any */
|
|
|
|
|
|
|
|
if (rif->if_unit==f->fw_via_unit &&
|
|
|
|
!strncmp(rif->if_name,f->fw_via_name,FW_IFNLEN))
|
|
|
|
goto via_match;
|
|
|
|
} else {
|
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
if (!f->fw_via_ip.s_addr)
|
1995-02-24 14:33:54 +00:00
|
|
|
goto via_match; /* No via ip set,match any */
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1995-02-24 14:33:54 +00:00
|
|
|
for (ia_p=ia;ia_p;ia_p=ia_p->ifa_next) {
|
|
|
|
if (!ia_p->ifa_addr||ia_p->ifa_addr->sa_family!=AF_INET)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ia_i.s_addr=(((struct sockaddr_in *)\
|
|
|
|
(ia_p->ifa_addr))->sin_addr.s_addr);
|
|
|
|
if (ia_i.s_addr==f->fw_via_ip.s_addr)
|
|
|
|
goto via_match;
|
1994-12-12 17:20:55 +00:00
|
|
|
}
|
1995-02-24 14:33:54 +00:00
|
|
|
|
|
|
|
}
|
1995-05-30 08:16:23 +00:00
|
|
|
/*
|
1995-02-24 14:33:54 +00:00
|
|
|
* If we got here,no "via"'s matched,so
|
|
|
|
* we should continue to the next firewall entry.
|
|
|
|
*/
|
1995-05-30 08:16:23 +00:00
|
|
|
continue;
|
1994-12-12 17:20:55 +00:00
|
|
|
via_match:
|
1994-12-13 15:57:34 +00:00
|
|
|
f_prt=f->fw_flg&IP_FW_F_KIND;
|
1994-12-12 17:20:55 +00:00
|
|
|
if (f_prt==IP_FW_F_ALL) {
|
1994-11-16 10:17:11 +00:00
|
|
|
/* Universal frwl - we've got a match! */
|
|
|
|
|
1994-12-13 15:57:34 +00:00
|
|
|
f->fw_pcnt++; /* Rise packet count */
|
1994-11-16 10:17:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Rise byte count,
|
|
|
|
* if need to convert from
|
1995-05-30 08:16:23 +00:00
|
|
|
* host to network byte
|
1994-11-16 10:17:11 +00:00
|
|
|
* order,do it.
|
|
|
|
*/
|
1995-05-30 08:16:23 +00:00
|
|
|
if (nh_conv)
|
1994-12-13 15:57:34 +00:00
|
|
|
f->fw_bcnt+=ntohs(ip->ip_len);
|
1994-11-16 10:17:11 +00:00
|
|
|
else
|
1994-12-13 15:57:34 +00:00
|
|
|
f->fw_bcnt+=ip->ip_len;
|
1994-11-16 10:17:11 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Specific firewall - packet's
|
|
|
|
* protocol must match firewall's
|
|
|
|
*/
|
1994-12-12 17:20:55 +00:00
|
|
|
if (prt==f_prt) {
|
1994-11-16 10:17:11 +00:00
|
|
|
|
1994-12-12 17:20:55 +00:00
|
|
|
if ((prt==IP_FW_F_ICMP ||
|
1994-12-13 15:57:34 +00:00
|
|
|
(port_match(&f->fw_pts[0],f->fw_nsp,src_port,
|
|
|
|
f->fw_flg&IP_FW_F_SRNG) &&
|
|
|
|
port_match(&f->fw_pts[f->fw_nsp],f->fw_ndp,dst_port,
|
|
|
|
f->fw_flg&IP_FW_F_DRNG)))
|
1995-05-30 08:16:23 +00:00
|
|
|
|| ((rev)
|
1994-12-13 15:57:34 +00:00
|
|
|
&& (port_match(&f->fw_pts[0],f->fw_nsp,dst_port,
|
|
|
|
f->fw_flg&IP_FW_F_SRNG)
|
|
|
|
&& port_match(&f->fw_pts[f->fw_nsp],f->fw_ndp,src_port,
|
|
|
|
f->fw_flg&IP_FW_F_DRNG))))
|
1994-11-16 10:17:11 +00:00
|
|
|
{
|
1994-12-13 15:57:34 +00:00
|
|
|
f->fw_pcnt++; /* Rise packet count */
|
1994-11-16 10:17:11 +00:00
|
|
|
/*
|
|
|
|
* Rise byte count,
|
|
|
|
* if need to convert from
|
1995-05-30 08:16:23 +00:00
|
|
|
* host to network byte
|
1994-11-16 10:17:11 +00:00
|
|
|
* order,do it.
|
|
|
|
*/
|
1995-05-30 08:16:23 +00:00
|
|
|
if (nh_conv)
|
1994-12-13 15:57:34 +00:00
|
|
|
f->fw_bcnt+=ntohs(ip->ip_len);
|
1994-11-16 10:17:11 +00:00
|
|
|
else
|
1994-12-13 15:57:34 +00:00
|
|
|
f->fw_bcnt+=ip->ip_len;
|
1994-11-16 10:17:11 +00:00
|
|
|
} /* Ports match */
|
|
|
|
} /* Proto matches */
|
|
|
|
} /* ALL/Specific */
|
|
|
|
} /* IP addr/mask matches */
|
|
|
|
} /* End of whole function */
|
|
|
|
#endif /* IPACCT */
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
static
|
|
|
|
void
|
|
|
|
zero_fw_chain(chainptr)
|
|
|
|
struct ip_fw *chainptr;
|
|
|
|
{
|
|
|
|
struct ip_fw *ctmp=chainptr;
|
|
|
|
while(ctmp) {
|
1994-12-13 15:57:34 +00:00
|
|
|
ctmp->fw_pcnt=0l;
|
|
|
|
ctmp->fw_bcnt=0l;
|
|
|
|
ctmp=ctmp->fw_next;
|
1994-11-16 10:17:11 +00:00
|
|
|
}
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
1994-11-08 12:47:29 +00:00
|
|
|
free_fw_chain(chainptr)
|
|
|
|
struct ip_fw **chainptr;
|
1994-10-28 15:09:49 +00:00
|
|
|
{
|
1994-11-07 10:01:32 +00:00
|
|
|
int s=splnet();
|
1994-11-28 12:35:14 +00:00
|
|
|
while (*chainptr) {
|
1994-11-08 12:47:29 +00:00
|
|
|
struct ip_fw *ftmp;
|
1994-10-28 15:09:49 +00:00
|
|
|
ftmp = *chainptr;
|
1994-12-13 15:57:34 +00:00
|
|
|
*chainptr = ftmp->fw_next;
|
1995-03-12 13:28:13 +00:00
|
|
|
free(ftmp,M_IPFW);
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
1994-11-07 10:01:32 +00:00
|
|
|
splx(s);
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
1994-12-12 18:10:41 +00:00
|
|
|
add_entry(chainptr,frwl)
|
1994-11-07 10:01:32 +00:00
|
|
|
struct ip_fw **chainptr;
|
|
|
|
struct ip_fw *frwl;
|
1994-10-28 15:09:49 +00:00
|
|
|
{
|
1994-11-07 10:01:32 +00:00
|
|
|
struct ip_fw *ftmp;
|
|
|
|
struct ip_fw *chtmp=NULL;
|
|
|
|
struct ip_fw *chtmp_prev=NULL;
|
|
|
|
int s=splnet();
|
1994-10-31 23:58:04 +00:00
|
|
|
u_long m_src_mask,m_dst_mask;
|
|
|
|
u_long n_sa,n_da,o_sa,o_da,o_sm,o_dm,n_sm,n_dm;
|
1995-05-30 08:16:23 +00:00
|
|
|
u_short n_sr,n_dr,o_sr,o_dr;
|
1994-10-31 23:58:04 +00:00
|
|
|
u_short oldkind,newkind;
|
|
|
|
int addb4=0;
|
|
|
|
int n_o,n_n;
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1995-03-12 13:28:13 +00:00
|
|
|
ftmp = malloc(sizeof(struct ip_fw),M_IPFW,M_DONTWAIT);
|
1994-10-28 15:09:49 +00:00
|
|
|
if ( ftmp == NULL ) {
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf1("ip_fw_ctl: malloc said no\n");
|
1994-11-07 10:01:32 +00:00
|
|
|
splx(s);
|
1994-11-28 12:35:14 +00:00
|
|
|
return(ENOSPC);
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
|
|
|
|
1994-11-28 12:35:14 +00:00
|
|
|
bcopy(frwl,ftmp,sizeof(struct ip_fw));
|
1994-12-13 15:57:34 +00:00
|
|
|
ftmp->fw_pcnt=0L;
|
|
|
|
ftmp->fw_bcnt=0L;
|
1994-11-16 10:17:11 +00:00
|
|
|
|
1994-12-13 15:57:34 +00:00
|
|
|
ftmp->fw_next = NULL;
|
1994-10-28 15:09:49 +00:00
|
|
|
|
|
|
|
if (*chainptr==NULL)
|
|
|
|
{
|
|
|
|
*chainptr=ftmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1994-11-07 10:01:32 +00:00
|
|
|
chtmp_prev=NULL;
|
1994-12-13 15:57:34 +00:00
|
|
|
for (chtmp=*chainptr;chtmp!=NULL;chtmp=chtmp->fw_next) {
|
1994-10-31 23:58:04 +00:00
|
|
|
|
|
|
|
addb4=0;
|
|
|
|
|
1994-12-13 15:57:34 +00:00
|
|
|
newkind=ftmp->fw_flg & IP_FW_F_KIND;
|
|
|
|
oldkind=chtmp->fw_flg & IP_FW_F_KIND;
|
1994-10-31 23:58:04 +00:00
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
if (newkind!=IP_FW_F_ALL
|
1994-11-07 10:01:32 +00:00
|
|
|
&& oldkind!=IP_FW_F_ALL
|
1994-11-16 10:17:11 +00:00
|
|
|
&& oldkind!=newkind) {
|
|
|
|
chtmp_prev=chtmp;
|
|
|
|
continue;
|
|
|
|
}
|
1994-10-31 23:58:04 +00:00
|
|
|
/*
|
|
|
|
* Very very *UGLY* code...
|
|
|
|
* Sorry,but i had to do this....
|
|
|
|
*/
|
1994-12-13 15:57:34 +00:00
|
|
|
n_sa=ntohl(ftmp->fw_src.s_addr);
|
|
|
|
n_da=ntohl(ftmp->fw_dst.s_addr);
|
|
|
|
n_sm=ntohl(ftmp->fw_smsk.s_addr);
|
|
|
|
n_dm=ntohl(ftmp->fw_dmsk.s_addr);
|
1994-10-31 23:58:04 +00:00
|
|
|
|
1994-12-13 15:57:34 +00:00
|
|
|
o_sa=ntohl(chtmp->fw_src.s_addr);
|
|
|
|
o_da=ntohl(chtmp->fw_dst.s_addr);
|
|
|
|
o_sm=ntohl(chtmp->fw_smsk.s_addr);
|
|
|
|
o_dm=ntohl(chtmp->fw_dmsk.s_addr);
|
1994-10-31 23:58:04 +00:00
|
|
|
|
|
|
|
m_src_mask = o_sm & n_sm;
|
|
|
|
m_dst_mask = o_dm & n_dm;
|
|
|
|
|
|
|
|
if ((o_sa & m_src_mask) == (n_sa & m_src_mask)) {
|
1995-05-30 08:16:23 +00:00
|
|
|
if (n_sm > o_sm)
|
1994-10-31 23:58:04 +00:00
|
|
|
addb4++;
|
1995-05-30 08:16:23 +00:00
|
|
|
if (n_sm < o_sm)
|
1994-10-31 23:58:04 +00:00
|
|
|
addb4--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((o_da & m_dst_mask) == (n_da & m_dst_mask)) {
|
|
|
|
if (n_dm > o_dm)
|
|
|
|
addb4++;
|
|
|
|
if (n_dm < o_dm)
|
|
|
|
addb4--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((o_da & o_dm) == (n_da & n_dm))
|
|
|
|
&&((o_sa & o_sm) == (n_sa & n_sm)))
|
|
|
|
{
|
1994-11-07 10:01:32 +00:00
|
|
|
if (newkind!=IP_FW_F_ALL &&
|
|
|
|
oldkind==IP_FW_F_ALL)
|
1994-10-31 23:58:04 +00:00
|
|
|
addb4++;
|
1994-11-07 10:01:32 +00:00
|
|
|
if (newkind==oldkind && (oldkind==IP_FW_F_TCP
|
|
|
|
|| oldkind==IP_FW_F_UDP)) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here the main ide is to check the size
|
|
|
|
* of port range which the frwl covers
|
|
|
|
* We actually don't check their values but
|
|
|
|
* just the wideness of range they have
|
|
|
|
* so that less wide ranges or single ports
|
|
|
|
* go first and wide ranges go later. No ports
|
|
|
|
* at all treated as a range of maximum number
|
|
|
|
* of ports.
|
|
|
|
*/
|
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
if (ftmp->fw_flg & IP_FW_F_SRNG)
|
1994-12-13 15:57:34 +00:00
|
|
|
n_sr=ftmp->fw_pts[1]-ftmp->fw_pts[0];
|
1995-05-30 08:16:23 +00:00
|
|
|
else
|
1994-12-13 15:57:34 +00:00
|
|
|
n_sr=(ftmp->fw_nsp)?
|
|
|
|
ftmp->fw_nsp : USHRT_MAX;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
|
|
|
if (chtmp->fw_flg & IP_FW_F_SRNG)
|
1994-12-13 15:57:34 +00:00
|
|
|
o_sr=chtmp->fw_pts[1]-chtmp->fw_pts[0];
|
1995-05-30 08:16:23 +00:00
|
|
|
else
|
1994-12-13 15:57:34 +00:00
|
|
|
o_sr=(chtmp->fw_nsp)?
|
|
|
|
chtmp->fw_nsp : USHRT_MAX;
|
1994-11-07 10:01:32 +00:00
|
|
|
|
|
|
|
if (n_sr<o_sr)
|
1994-10-31 23:58:04 +00:00
|
|
|
addb4++;
|
1994-11-07 10:01:32 +00:00
|
|
|
if (n_sr>o_sr)
|
|
|
|
addb4--;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-12-13 15:57:34 +00:00
|
|
|
n_n=ftmp->fw_nsp;
|
|
|
|
n_o=chtmp->fw_nsp;
|
1994-10-31 23:58:04 +00:00
|
|
|
/*
|
1994-11-07 10:01:32 +00:00
|
|
|
* Actually this cannot happen as the frwl control
|
1994-10-31 23:58:04 +00:00
|
|
|
* procedure checks for number of ports in source and
|
|
|
|
* destination range but we will try to be more safe.
|
|
|
|
*/
|
1994-11-07 10:01:32 +00:00
|
|
|
if ((n_n>(IP_FW_MAX_PORTS-2)) ||
|
|
|
|
(n_o>(IP_FW_MAX_PORTS-2)))
|
|
|
|
goto skip_check;
|
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
if (ftmp->fw_flg & IP_FW_F_DRNG)
|
1994-12-13 15:57:34 +00:00
|
|
|
n_dr=ftmp->fw_pts[n_n+1]-ftmp->fw_pts[n_n];
|
1995-05-30 08:16:23 +00:00
|
|
|
else
|
1994-12-13 15:57:34 +00:00
|
|
|
n_dr=(ftmp->fw_ndp)?
|
|
|
|
ftmp->fw_ndp : USHRT_MAX;
|
1994-11-07 10:01:32 +00:00
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
if (chtmp->fw_flg & IP_FW_F_DRNG)
|
1994-12-13 15:57:34 +00:00
|
|
|
o_dr=chtmp->fw_pts[n_o+1]-chtmp->fw_pts[n_o];
|
1995-05-30 08:16:23 +00:00
|
|
|
else
|
1994-12-13 15:57:34 +00:00
|
|
|
o_dr=(chtmp->fw_ndp)?
|
|
|
|
chtmp->fw_ndp : USHRT_MAX;
|
1994-11-07 10:01:32 +00:00
|
|
|
if (n_dr<o_dr)
|
|
|
|
addb4++;
|
|
|
|
if (n_dr>o_dr)
|
|
|
|
addb4--;
|
|
|
|
|
|
|
|
skip_check:
|
1994-10-31 23:58:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (addb4>0) {
|
1994-11-07 10:01:32 +00:00
|
|
|
if (chtmp_prev) {
|
1995-05-30 08:16:23 +00:00
|
|
|
chtmp_prev->fw_next=ftmp;
|
1994-12-13 15:57:34 +00:00
|
|
|
ftmp->fw_next=chtmp;
|
1994-10-31 23:58:04 +00:00
|
|
|
} else {
|
|
|
|
*chainptr=ftmp;
|
1994-12-13 15:57:34 +00:00
|
|
|
ftmp->fw_next=chtmp;
|
1994-10-31 23:58:04 +00:00
|
|
|
}
|
1994-11-07 10:01:32 +00:00
|
|
|
splx(s);
|
1994-10-31 23:58:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1994-11-07 10:01:32 +00:00
|
|
|
chtmp_prev=chtmp;
|
1994-10-31 23:58:04 +00:00
|
|
|
}
|
1994-11-07 10:01:32 +00:00
|
|
|
if (chtmp_prev)
|
1994-12-13 15:57:34 +00:00
|
|
|
chtmp_prev->fw_next=ftmp;
|
1994-10-31 23:58:04 +00:00
|
|
|
else
|
1994-12-12 18:10:41 +00:00
|
|
|
#ifdef DIAGNOSTICS
|
1994-10-31 23:58:04 +00:00
|
|
|
panic("Can't happen");
|
1994-12-12 18:10:41 +00:00
|
|
|
#else
|
|
|
|
*chainptr=ftmp;
|
1994-10-28 15:09:49 +00:00
|
|
|
#endif
|
|
|
|
}
|
1994-11-07 10:01:32 +00:00
|
|
|
splx(s);
|
1994-10-28 15:09:49 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
1994-12-12 18:10:41 +00:00
|
|
|
del_entry(chainptr,frwl)
|
1994-11-08 12:47:29 +00:00
|
|
|
struct ip_fw **chainptr;
|
|
|
|
struct ip_fw *frwl;
|
1994-10-28 15:09:49 +00:00
|
|
|
{
|
1994-11-08 12:47:29 +00:00
|
|
|
struct ip_fw *ftmp,*ltmp;
|
1994-10-28 15:09:49 +00:00
|
|
|
u_short tport1,tport2,tmpnum;
|
|
|
|
char matches,was_found;
|
1994-11-08 12:47:29 +00:00
|
|
|
int s=splnet();
|
1994-10-28 15:09:49 +00:00
|
|
|
|
|
|
|
ftmp=*chainptr;
|
|
|
|
|
1994-11-28 12:35:14 +00:00
|
|
|
if (ftmp == NULL) {
|
|
|
|
dprintf1("ip_fw_ctl: chain is empty\n");
|
1994-11-07 10:01:32 +00:00
|
|
|
splx(s);
|
1994-11-28 12:35:14 +00:00
|
|
|
return(EINVAL);
|
|
|
|
}
|
1994-10-28 15:09:49 +00:00
|
|
|
|
|
|
|
ltmp=NULL;
|
|
|
|
was_found=0;
|
|
|
|
|
1994-11-28 12:35:14 +00:00
|
|
|
while(ftmp)
|
1994-10-28 15:09:49 +00:00
|
|
|
{
|
|
|
|
matches=1;
|
1995-05-30 08:16:23 +00:00
|
|
|
if (ftmp->fw_src.s_addr!=frwl->fw_src.s_addr
|
1994-12-13 15:57:34 +00:00
|
|
|
|| ftmp->fw_dst.s_addr!=frwl->fw_dst.s_addr
|
|
|
|
|| ftmp->fw_smsk.s_addr!=frwl->fw_smsk.s_addr
|
|
|
|
|| ftmp->fw_dmsk.s_addr!=frwl->fw_dmsk.s_addr
|
1995-02-24 14:33:54 +00:00
|
|
|
|| ftmp->fw_via_ip.s_addr!=frwl->fw_via_ip.s_addr
|
1994-12-13 15:57:34 +00:00
|
|
|
|| ftmp->fw_flg!=frwl->fw_flg)
|
1994-10-28 15:09:49 +00:00
|
|
|
matches=0;
|
1994-12-13 15:57:34 +00:00
|
|
|
tport1=ftmp->fw_nsp+ftmp->fw_ndp;
|
|
|
|
tport2=frwl->fw_nsp+frwl->fw_ndp;
|
1994-10-28 15:09:49 +00:00
|
|
|
if (tport1!=tport2)
|
|
|
|
matches=0;
|
|
|
|
else
|
|
|
|
if (tport1!=0)
|
|
|
|
{
|
1994-11-08 12:47:29 +00:00
|
|
|
for (tmpnum=0;tmpnum < tport1 && tmpnum < IP_FW_MAX_PORTS;tmpnum++)
|
1994-12-13 15:57:34 +00:00
|
|
|
if (ftmp->fw_pts[tmpnum]!=frwl->fw_pts[tmpnum])
|
1994-10-28 15:09:49 +00:00
|
|
|
matches=0;
|
|
|
|
}
|
|
|
|
if(matches)
|
|
|
|
{
|
|
|
|
was_found=1;
|
|
|
|
if (ltmp)
|
|
|
|
{
|
1994-12-13 15:57:34 +00:00
|
|
|
ltmp->fw_next=ftmp->fw_next;
|
1995-03-12 13:28:13 +00:00
|
|
|
free(ftmp,M_IPFW);
|
1994-12-13 15:57:34 +00:00
|
|
|
ftmp=ltmp->fw_next;
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1995-05-30 08:16:23 +00:00
|
|
|
*chainptr=ftmp->fw_next;
|
1995-03-12 13:28:13 +00:00
|
|
|
free(ftmp,M_IPFW);
|
1994-10-28 15:09:49 +00:00
|
|
|
ftmp=*chainptr;
|
|
|
|
}
|
1995-05-30 08:16:23 +00:00
|
|
|
|
|
|
|
}
|
1994-10-28 15:09:49 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ltmp = ftmp;
|
1994-12-13 15:57:34 +00:00
|
|
|
ftmp = ftmp->fw_next;
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
1994-11-07 10:01:32 +00:00
|
|
|
splx(s);
|
1994-10-28 15:09:49 +00:00
|
|
|
if (was_found) return 0;
|
|
|
|
else return(EINVAL);
|
|
|
|
}
|
|
|
|
|
1994-12-13 15:57:34 +00:00
|
|
|
static
|
|
|
|
int
|
|
|
|
clr_entry(chainptr,frwl)
|
|
|
|
struct ip_fw **chainptr;
|
|
|
|
struct ip_fw *frwl;
|
|
|
|
{
|
|
|
|
struct ip_fw *ftmp,*ltmp;
|
|
|
|
u_short tport1,tport2,tmpnum;
|
|
|
|
char matches,was_found;
|
|
|
|
|
|
|
|
ftmp=*chainptr;
|
|
|
|
|
|
|
|
if (ftmp == NULL) {
|
|
|
|
dprintf1("ip_fw_ctl: chain is empty\n");
|
|
|
|
return(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
was_found=0;
|
|
|
|
|
|
|
|
while(ftmp)
|
|
|
|
{
|
|
|
|
matches=1;
|
1995-05-30 08:16:23 +00:00
|
|
|
if (ftmp->fw_src.s_addr!=frwl->fw_src.s_addr
|
1994-12-13 15:57:34 +00:00
|
|
|
|| ftmp->fw_dst.s_addr!=frwl->fw_dst.s_addr
|
|
|
|
|| ftmp->fw_smsk.s_addr!=frwl->fw_smsk.s_addr
|
|
|
|
|| ftmp->fw_dmsk.s_addr!=frwl->fw_dmsk.s_addr
|
1995-02-24 14:33:54 +00:00
|
|
|
|| ftmp->fw_via_ip.s_addr!=frwl->fw_via_ip.s_addr
|
1994-12-13 15:57:34 +00:00
|
|
|
|| ftmp->fw_flg!=frwl->fw_flg)
|
|
|
|
matches=0;
|
|
|
|
tport1=ftmp->fw_nsp+ftmp->fw_ndp;
|
|
|
|
tport2=frwl->fw_nsp+frwl->fw_ndp;
|
|
|
|
if (tport1!=tport2)
|
|
|
|
matches=0;
|
|
|
|
else
|
|
|
|
if (tport1!=0)
|
|
|
|
{
|
|
|
|
for (tmpnum=0;tmpnum < tport1 && tmpnum < IP_FW_MAX_PORTS;tmpnum++)
|
|
|
|
if (ftmp->fw_pts[tmpnum]!=frwl->fw_pts[tmpnum])
|
|
|
|
matches=0;
|
|
|
|
}
|
|
|
|
if(matches)
|
|
|
|
{
|
|
|
|
was_found=1;
|
|
|
|
ftmp->fw_pcnt=0L;
|
|
|
|
ftmp->fw_bcnt=0L;
|
|
|
|
}
|
|
|
|
ftmp=ftmp->fw_next;
|
|
|
|
}
|
|
|
|
if (was_found) return 0;
|
|
|
|
else return(EINVAL);
|
|
|
|
}
|
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
struct ip_fw *
|
|
|
|
check_ipfw_struct(m)
|
|
|
|
struct mbuf *m;
|
|
|
|
{
|
|
|
|
struct ip_fw *frwl;
|
|
|
|
|
|
|
|
if ( m->m_len != sizeof(struct ip_fw) ) {
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf3("ip_fw_ctl: len=%d, want %d\n",m->m_len,
|
1994-11-16 10:17:11 +00:00
|
|
|
sizeof(struct ip_fw));
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
frwl = mtod(m,struct ip_fw*);
|
|
|
|
|
1994-12-13 15:57:34 +00:00
|
|
|
if ( (frwl->fw_flg & ~IP_FW_F_MASK) != 0 ) {
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf2("ip_fw_ctl: undefined flag bits set (flags=%x)\n",
|
1994-12-13 15:57:34 +00:00
|
|
|
frwl->fw_flg);
|
1994-11-16 10:17:11 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
1994-12-13 15:57:34 +00:00
|
|
|
if ( (frwl->fw_flg & IP_FW_F_SRNG) && frwl->fw_nsp < 2 ) {
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf2("ip_fw_ctl: src range set but n_src_p=%d\n",
|
1994-12-13 15:57:34 +00:00
|
|
|
frwl->fw_nsp);
|
1994-11-16 10:17:11 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
1994-12-13 15:57:34 +00:00
|
|
|
if ( (frwl->fw_flg & IP_FW_F_DRNG) && frwl->fw_ndp < 2 ) {
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf2("ip_fw_ctl: dst range set but n_dst_p=%d\n",
|
1994-12-13 15:57:34 +00:00
|
|
|
frwl->fw_ndp);
|
1994-11-16 10:17:11 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
1994-12-13 15:57:34 +00:00
|
|
|
if ( frwl->fw_nsp + frwl->fw_ndp > IP_FW_MAX_PORTS ) {
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf3("ip_fw_ctl: too many ports (%d+%d)\n",
|
1994-12-13 15:57:34 +00:00
|
|
|
frwl->fw_nsp,frwl->fw_ndp);
|
1994-11-16 10:17:11 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
1994-12-13 15:57:34 +00:00
|
|
|
if ( (frwl->fw_flg & IP_FW_F_KIND) == IP_FW_F_ICMP ) {
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf1("ip_fw_ctl: request for unsupported ICMP frwling\n");
|
1994-11-16 10:17:11 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return frwl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef IPACCT
|
|
|
|
int
|
|
|
|
ip_acct_ctl(stage,m)
|
|
|
|
int stage;
|
|
|
|
struct mbuf *m;
|
|
|
|
{
|
|
|
|
if ( stage == IP_ACCT_FLUSH )
|
|
|
|
{
|
|
|
|
free_fw_chain(&ip_acct_chain);
|
|
|
|
return(0);
|
1995-05-30 08:16:23 +00:00
|
|
|
}
|
1994-11-16 10:17:11 +00:00
|
|
|
if ( stage == IP_ACCT_ZERO )
|
|
|
|
{
|
|
|
|
zero_fw_chain(ip_acct_chain);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
if ( stage == IP_ACCT_ADD
|
|
|
|
|| stage == IP_ACCT_DEL
|
1994-12-13 15:57:34 +00:00
|
|
|
|| stage == IP_ACCT_CLR
|
1994-11-16 10:17:11 +00:00
|
|
|
) {
|
|
|
|
|
|
|
|
struct ip_fw *frwl;
|
|
|
|
|
|
|
|
if (!(frwl=check_ipfw_struct(m)))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
switch (stage) {
|
|
|
|
case IP_ACCT_ADD:
|
1994-12-12 18:10:41 +00:00
|
|
|
return( add_entry(&ip_acct_chain,frwl));
|
1994-11-16 10:17:11 +00:00
|
|
|
case IP_ACCT_DEL:
|
1994-12-12 18:10:41 +00:00
|
|
|
return( del_entry(&ip_acct_chain,frwl));
|
1994-12-13 15:57:34 +00:00
|
|
|
case IP_ACCT_CLR:
|
|
|
|
return( clr_entry(&ip_acct_chain,frwl));
|
1994-11-16 10:17:11 +00:00
|
|
|
default:
|
1994-12-13 15:57:34 +00:00
|
|
|
#ifdef DIAGNOSTICS
|
|
|
|
panic("Can't happen");
|
|
|
|
#else
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf2("ip_acct_ctl: unknown request %d\n",stage);
|
1994-11-16 10:17:11 +00:00
|
|
|
return(EINVAL);
|
1994-12-13 15:57:34 +00:00
|
|
|
#endif
|
1994-11-16 10:17:11 +00:00
|
|
|
}
|
|
|
|
}
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf2("ip_acct_ctl: unknown request %d\n",stage);
|
1994-11-16 10:17:11 +00:00
|
|
|
return(EINVAL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef IPFIREWALL
|
1994-10-28 15:09:49 +00:00
|
|
|
int
|
1994-11-08 12:47:29 +00:00
|
|
|
ip_fw_ctl(stage,m)
|
1995-05-30 08:16:23 +00:00
|
|
|
int stage;
|
1994-10-28 15:09:49 +00:00
|
|
|
struct mbuf *m;
|
|
|
|
{
|
|
|
|
if ( stage == IP_FW_FLUSH )
|
|
|
|
{
|
1995-01-12 13:06:32 +00:00
|
|
|
free_fw_chain(&ip_fw_chain);
|
1994-10-28 15:09:49 +00:00
|
|
|
return(0);
|
1995-05-30 08:16:23 +00:00
|
|
|
}
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
if ( m == 0 )
|
1994-11-16 10:17:11 +00:00
|
|
|
{
|
|
|
|
printf("ip_fw_ctl: NULL mbuf ptr\n");
|
|
|
|
return(EINVAL);
|
|
|
|
}
|
1994-10-28 15:09:49 +00:00
|
|
|
|
|
|
|
if ( stage == IP_FW_POLICY )
|
|
|
|
{
|
1994-11-28 12:35:14 +00:00
|
|
|
u_short *tmp_policy_ptr;
|
|
|
|
tmp_policy_ptr=mtod(m,u_short *);
|
|
|
|
if ((*tmp_policy_ptr)&~IP_FW_P_MASK)
|
1994-11-16 10:17:11 +00:00
|
|
|
return (EINVAL);
|
1994-10-28 15:09:49 +00:00
|
|
|
ip_fw_policy=*tmp_policy_ptr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
/*
|
|
|
|
* Here we really working hard-adding new elements
|
1995-01-12 13:06:32 +00:00
|
|
|
* to firewall chain or deleting'em
|
1994-11-16 10:17:11 +00:00
|
|
|
*/
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
if ( stage == IP_FW_ADD ||
|
1995-01-12 13:06:32 +00:00
|
|
|
stage == IP_FW_DEL ) {
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
struct ip_fw *frwl;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
if (!(frwl=check_ipfw_struct(m)))
|
|
|
|
return (EINVAL);
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
switch (stage) {
|
1995-01-12 13:06:32 +00:00
|
|
|
case IP_FW_ADD:
|
|
|
|
return(add_entry(&ip_fw_chain,frwl));
|
|
|
|
case IP_FW_DEL:
|
|
|
|
return(del_entry(&ip_fw_chain,frwl));
|
1994-11-16 10:17:11 +00:00
|
|
|
default:
|
1995-01-12 13:06:32 +00:00
|
|
|
#ifdef DIAGNOSTICS
|
|
|
|
panic("Can't happen");
|
|
|
|
#else
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf2("ip_fw_ctl: unknown request %d\n",stage);
|
1994-11-16 10:17:11 +00:00
|
|
|
return(EINVAL);
|
1995-01-12 13:06:32 +00:00
|
|
|
#endif
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
1995-05-30 08:16:23 +00:00
|
|
|
}
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf2("ip_fw_ctl: unknown request %d\n",stage);
|
1994-10-28 15:09:49 +00:00
|
|
|
return(EINVAL);
|
|
|
|
}
|
1994-11-16 10:17:11 +00:00
|
|
|
#endif /* IPFIREWALL */
|