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>
|
1994-11-16 10:17:11 +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>
|
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
1994-10-28 15:09:49 +00:00
|
|
|
#include <netinet/ip_fw.h>
|
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
#ifdef IPFIREWALL
|
1994-11-08 12:47:29 +00:00
|
|
|
struct ip_fw *ip_fw_fwd_chain;
|
|
|
|
struct ip_fw *ip_fw_blk_chain;
|
1994-11-28 12:35:14 +00:00
|
|
|
u_short ip_fw_policy=0;
|
1994-11-16 10:17:11 +00:00
|
|
|
#endif
|
|
|
|
#ifdef IPACCT
|
|
|
|
struct ip_fw *ip_acct_chain;
|
|
|
|
#endif
|
1994-10-28 15:09:49 +00:00
|
|
|
|
|
|
|
|
1994-11-28 12:35:14 +00:00
|
|
|
#ifdef IPFIREWALL_DEBUG
|
|
|
|
#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
|
|
|
|
#define dprintf1(a)
|
|
|
|
#define dprintf2(a1,a2)
|
|
|
|
#define dprintf3(a1,a2,a3)
|
|
|
|
#define dprintf4(a1,a2,a3,a4)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define print_ip(a) printf("%d.%d.%d.%d",(ntohl(a.s_addr)>>24)&0xFF,\
|
|
|
|
(ntohl(a.s_addr)>>16)&0xFF,\
|
|
|
|
(ntohl(a.s_addr)>>8)&0xFF,\
|
|
|
|
(ntohl(a.s_addr))&0xFF);
|
|
|
|
|
|
|
|
#ifdef IPFIREWALL_DEBUG
|
|
|
|
#define dprint_ip(a) print_ip(a)
|
|
|
|
#else
|
|
|
|
#define dprint_ip(a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
1994-10-28 15:09:49 +00:00
|
|
|
inline
|
|
|
|
void
|
|
|
|
print_ip(xaddr)
|
|
|
|
struct in_addr xaddr;
|
|
|
|
{
|
|
|
|
u_long addr = ntohl(xaddr.s_addr);
|
|
|
|
printf("%d.%d.%d.%d",(addr>>24) & 0xff,
|
|
|
|
(addr>>16)&0xff,
|
|
|
|
(addr>>8)&0xff,
|
|
|
|
addr&0xFF);
|
|
|
|
}
|
1994-11-28 12:35:14 +00:00
|
|
|
*/
|
1994-10-28 15:09:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns 1 if the port is matched by the vector, 0 otherwise
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
return 1;
|
1994-11-28 12:35:14 +00:00
|
|
|
if (range_flag) {
|
|
|
|
if (portptr[0]<=port && port<=portptr[1]) {
|
|
|
|
return 1;
|
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) {
|
|
|
|
return 1;
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
1994-11-28 12:35:14 +00:00
|
|
|
return 0;
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1994-11-08 12:47:29 +00:00
|
|
|
* Returns 0 if packet should be dropped, 1 or more if it should be accepted
|
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
|
|
|
|
* allow any packet-this is equal
|
|
|
|
* to disabling firewall.
|
|
|
|
*/
|
1994-11-28 12:35:14 +00:00
|
|
|
if (!chain)
|
1994-12-12 17:20:55 +00:00
|
|
|
return(1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
return(1);
|
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
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
for (f=chain;f;f=f->next)
|
|
|
|
if ((src.s_addr&f->src_mask.s_addr)==f->src.s_addr
|
|
|
|
&& (dst.s_addr&f->dst_mask.s_addr)==f->dst.s_addr) {
|
1994-12-12 17:20:55 +00:00
|
|
|
if (f->via.s_addr && rif) {
|
|
|
|
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)
|
|
|
|
/*
|
|
|
|
* Next interface adress.
|
|
|
|
* This is continue for
|
|
|
|
* local "for"
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
ia_i.s_addr=(((struct sockaddr_in *)\
|
|
|
|
(ia_p->ifa_addr))->sin_addr.s_addr);
|
|
|
|
if (ia_i.s_addr==f->via.s_addr)
|
|
|
|
goto via_match;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Next interface adress.
|
|
|
|
* This is continue for
|
|
|
|
* local "for"
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* No special "via" adress set
|
|
|
|
* or interface from which packet
|
|
|
|
* came unknown so match anyway
|
|
|
|
*/
|
|
|
|
goto via_match;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Skip to next firewall entry - via
|
|
|
|
* address did not matched.
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
via_match:
|
|
|
|
f_prt=f->flags&IP_FW_F_KIND;
|
|
|
|
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
|
|
|
/*
|
1994-11-28 12:35:14 +00:00
|
|
|
* This is actually buggy as if you set SYN flag
|
|
|
|
* on UDp or ICMP firewall it will never work,but
|
|
|
|
* actually it is a concern of software which sets
|
|
|
|
* firewall entries.
|
1994-11-16 10:17:11 +00:00
|
|
|
*/
|
1994-11-28 12:35:14 +00:00
|
|
|
if (f->flags&IP_FW_F_TCPSYN && notcpsyn)
|
|
|
|
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-11-16 10:17:11 +00:00
|
|
|
(port_match(&f->ports[0],f->n_src_p,src_port,
|
|
|
|
f->flags&IP_FW_F_SRNG) &&
|
|
|
|
port_match(&f->ports[f->n_src_p],f->n_dst_p,dst_port,
|
|
|
|
f->flags&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
|
|
|
|
goto good_packet;
|
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-11-16 10:17:11 +00:00
|
|
|
if (f->flags&IP_FW_F_PRN) {
|
1994-11-28 12:35:14 +00:00
|
|
|
if (f->flags&IP_FW_F_ACCEPT)
|
|
|
|
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);
|
1994-11-28 12:35:14 +00:00
|
|
|
if (ip->ip_p==IPPROTO_TCP || ip->ip_p==IPPROTO_UDP)
|
|
|
|
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-11-28 12:35:14 +00:00
|
|
|
if (f->flags&IP_FW_F_ACCEPT)
|
|
|
|
goto good_packet;
|
|
|
|
#ifdef noneed
|
|
|
|
else
|
|
|
|
goto bad_packet;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bad_packet:
|
|
|
|
if (f) {
|
|
|
|
/*
|
|
|
|
* Do not ICMP reply to icmp
|
|
|
|
* packets....:)
|
|
|
|
*/
|
1994-12-12 17:20:55 +00:00
|
|
|
if (f_prt==IP_FW_F_ICMP)
|
1994-11-28 12:35:14 +00:00
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
* Reply to packets rejected
|
|
|
|
* by entry with this flag
|
|
|
|
* set only.
|
|
|
|
*/
|
|
|
|
if (!(f->flags&IP_FW_F_ICMPRPL))
|
|
|
|
return 0;
|
|
|
|
m = m_get(M_DONTWAIT, MT_SOOPTS);
|
|
|
|
/*
|
|
|
|
* We never retry,we don't want to
|
|
|
|
* waste time-it is not so critical
|
|
|
|
* if ICMP unsent.
|
|
|
|
*/
|
|
|
|
if (!m)
|
|
|
|
return 0;
|
|
|
|
m->m_len = sizeof(struct ip)+64;
|
|
|
|
bcopy((caddr_t)ip,mtod(m, caddr_t),(unsigned)m->m_len);
|
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);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If global icmp flag set we will do
|
|
|
|
* something here...later..
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
good_packet:
|
|
|
|
return 1;
|
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
|
|
|
|
1994-11-16 10:17:11 +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-11-16 10:17:11 +00:00
|
|
|
for (f=chain;f;f=f->next) {
|
|
|
|
if ((src.s_addr&f->src_mask.s_addr)==f->src.s_addr
|
|
|
|
&& (dst.s_addr&f->dst_mask.s_addr)==f->dst.s_addr) {
|
|
|
|
rev=0;
|
|
|
|
goto addr_match;
|
|
|
|
}
|
|
|
|
if ((f->flags&IP_FW_F_BIDIR) &&
|
|
|
|
((src.s_addr&f->src_mask.s_addr)==f->dst.s_addr
|
|
|
|
&& (dst.s_addr&f->dst_mask.s_addr)==f->src.s_addr)) {
|
|
|
|
rev=1;
|
|
|
|
goto addr_match;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
addr_match:
|
1994-12-12 17:20:55 +00:00
|
|
|
if (f->via.s_addr && rif) {
|
|
|
|
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->via.s_addr)
|
|
|
|
goto via_match;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
goto via_match;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
via_match:
|
|
|
|
f_prt=f->flags&IP_FW_F_KIND;
|
|
|
|
if (f_prt==IP_FW_F_ALL) {
|
1994-11-16 10:17:11 +00:00
|
|
|
/* Universal frwl - we've got a match! */
|
|
|
|
|
|
|
|
f->p_cnt++; /* Rise packet count */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rise byte count,
|
|
|
|
* if need to convert from
|
|
|
|
* host to network byte
|
|
|
|
* order,do it.
|
|
|
|
*/
|
|
|
|
if (nh_conv)
|
|
|
|
f->b_cnt+=ntohs(ip->ip_len);
|
|
|
|
else
|
|
|
|
f->b_cnt+=ip->ip_len;
|
|
|
|
} 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-11-16 10:17:11 +00:00
|
|
|
(port_match(&f->ports[0],f->n_src_p,src_port,
|
|
|
|
f->flags&IP_FW_F_SRNG) &&
|
|
|
|
port_match(&f->ports[f->n_src_p],f->n_dst_p,dst_port,
|
|
|
|
f->flags&IP_FW_F_DRNG)))
|
|
|
|
|| ((rev)
|
|
|
|
&& (port_match(&f->ports[0],f->n_src_p,dst_port,
|
|
|
|
f->flags&IP_FW_F_SRNG)
|
|
|
|
&& port_match(&f->ports[f->n_src_p],f->n_dst_p,src_port,
|
|
|
|
f->flags&IP_FW_F_DRNG))))
|
|
|
|
{
|
|
|
|
f->p_cnt++; /* Rise packet count */
|
|
|
|
/*
|
|
|
|
* Rise byte count,
|
|
|
|
* if need to convert from
|
|
|
|
* host to network byte
|
|
|
|
* order,do it.
|
|
|
|
*/
|
|
|
|
if (nh_conv)
|
|
|
|
f->b_cnt+=ntohs(ip->ip_len);
|
|
|
|
else
|
|
|
|
f->b_cnt+=ip->ip_len;
|
|
|
|
} /* 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) {
|
|
|
|
ctmp->p_cnt=0l;
|
|
|
|
ctmp->b_cnt=0l;
|
|
|
|
ctmp=ctmp->next;
|
|
|
|
}
|
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;
|
|
|
|
*chainptr = ftmp->next;
|
|
|
|
free(ftmp,M_SOOPTS);
|
|
|
|
}
|
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;
|
1994-11-07 10:01:32 +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
|
|
|
|
1994-11-28 12:35:14 +00:00
|
|
|
ftmp = malloc(sizeof(struct ip_fw),M_SOOPTS,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-11-16 10:17:11 +00:00
|
|
|
ftmp->p_cnt=0L;
|
|
|
|
ftmp->b_cnt=0L;
|
|
|
|
|
1994-10-28 15:09:49 +00:00
|
|
|
ftmp->next = NULL;
|
|
|
|
|
|
|
|
if (*chainptr==NULL)
|
|
|
|
{
|
|
|
|
*chainptr=ftmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1994-11-07 10:01:32 +00:00
|
|
|
chtmp_prev=NULL;
|
|
|
|
for (chtmp=*chainptr;chtmp!=NULL;chtmp=chtmp->next) {
|
1994-10-31 23:58:04 +00:00
|
|
|
|
|
|
|
addb4=0;
|
|
|
|
|
1994-11-07 10:01:32 +00:00
|
|
|
newkind=ftmp->flags & IP_FW_F_KIND;
|
|
|
|
oldkind=chtmp->flags & IP_FW_F_KIND;
|
1994-10-31 23:58:04 +00:00
|
|
|
|
1994-11-07 10:01:32 +00:00
|
|
|
if (newkind!=IP_FW_F_ALL
|
|
|
|
&& 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....
|
|
|
|
*/
|
|
|
|
n_sa=ntohl(ftmp->src.s_addr);
|
|
|
|
n_da=ntohl(ftmp->dst.s_addr);
|
|
|
|
n_sm=ntohl(ftmp->src_mask.s_addr);
|
|
|
|
n_dm=ntohl(ftmp->dst_mask.s_addr);
|
|
|
|
|
1994-11-07 10:01:32 +00:00
|
|
|
o_sa=ntohl(chtmp->src.s_addr);
|
|
|
|
o_da=ntohl(chtmp->dst.s_addr);
|
|
|
|
o_sm=ntohl(chtmp->src_mask.s_addr);
|
|
|
|
o_dm=ntohl(chtmp->dst_mask.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)) {
|
|
|
|
if (n_sm > o_sm)
|
|
|
|
addb4++;
|
|
|
|
if (n_sm < o_sm)
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ftmp->flags & IP_FW_F_SRNG)
|
|
|
|
n_sr=ftmp->ports[1]-ftmp->ports[0];
|
|
|
|
else
|
|
|
|
n_sr=(ftmp->n_src_p)?
|
|
|
|
ftmp->n_src_p : USHRT_MAX;
|
|
|
|
|
|
|
|
if (chtmp->flags & IP_FW_F_SRNG)
|
|
|
|
o_sr=chtmp->ports[1]-chtmp->ports[0];
|
|
|
|
else
|
|
|
|
o_sr=(chtmp->n_src_p)?
|
|
|
|
chtmp->n_src_p : USHRT_MAX;
|
|
|
|
|
|
|
|
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--;
|
|
|
|
|
|
|
|
n_n=ftmp->n_src_p;
|
|
|
|
n_o=chtmp->n_src_p;
|
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;
|
|
|
|
|
|
|
|
if (ftmp->flags & IP_FW_F_DRNG)
|
|
|
|
n_dr=ftmp->ports[n_n+1]-ftmp->ports[n_n];
|
|
|
|
else
|
|
|
|
n_dr=(ftmp->n_dst_p)?
|
|
|
|
ftmp->n_dst_p : USHRT_MAX;
|
|
|
|
|
|
|
|
if (chtmp->flags & IP_FW_F_DRNG)
|
1994-11-16 10:17:11 +00:00
|
|
|
o_dr=chtmp->ports[n_o+1]-chtmp->ports[n_o];
|
1994-11-07 10:01:32 +00:00
|
|
|
else
|
|
|
|
o_dr=(chtmp->n_dst_p)?
|
|
|
|
chtmp->n_dst_p : USHRT_MAX;
|
|
|
|
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) {
|
|
|
|
chtmp_prev->next=ftmp;
|
|
|
|
ftmp->next=chtmp;
|
1994-10-31 23:58:04 +00:00
|
|
|
} else {
|
|
|
|
*chainptr=ftmp;
|
1994-11-07 10:01:32 +00:00
|
|
|
ftmp->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)
|
|
|
|
chtmp_prev->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;
|
1994-12-12 18:10:41 +00:00
|
|
|
if (ftmp->src.s_addr!=frwl->src.s_addr
|
|
|
|
|| ftmp->dst.s_addr!=frwl->dst.s_addr
|
|
|
|
|| ftmp->src_mask.s_addr!=frwl->src_mask.s_addr
|
|
|
|
|| ftmp->dst_mask.s_addr!=frwl->dst_mask.s_addr
|
|
|
|
|| ftmp->via.s_addr!=frwl->via.s_addr
|
|
|
|
|| ftmp->flags!=frwl->flags)
|
1994-10-28 15:09:49 +00:00
|
|
|
matches=0;
|
1994-11-08 12:47:29 +00:00
|
|
|
tport1=ftmp->n_src_p+ftmp->n_dst_p;
|
|
|
|
tport2=frwl->n_src_p+frwl->n_dst_p;
|
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++)
|
|
|
|
if (ftmp->ports[tmpnum]!=frwl->ports[tmpnum])
|
1994-10-28 15:09:49 +00:00
|
|
|
matches=0;
|
|
|
|
}
|
|
|
|
if(matches)
|
|
|
|
{
|
|
|
|
was_found=1;
|
|
|
|
if (ltmp)
|
|
|
|
{
|
|
|
|
ltmp->next=ftmp->next;
|
|
|
|
free(ftmp,M_SOOPTS);
|
|
|
|
ftmp=ltmp->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*chainptr=ftmp->next;
|
|
|
|
free(ftmp,M_SOOPTS);
|
|
|
|
ftmp=*chainptr;
|
|
|
|
}
|
|
|
|
|
1994-11-28 12:35:14 +00:00
|
|
|
}
|
1994-10-28 15:09:49 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ltmp = ftmp;
|
|
|
|
ftmp = ftmp->next;
|
|
|
|
}
|
|
|
|
}
|
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-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*);
|
|
|
|
|
|
|
|
if ( (frwl->flags & ~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-11-16 10:17:11 +00:00
|
|
|
frwl->flags);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (frwl->flags & IP_FW_F_SRNG) && frwl->n_src_p < 2 ) {
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf2("ip_fw_ctl: src range set but n_src_p=%d\n",
|
1994-11-16 10:17:11 +00:00
|
|
|
frwl->n_src_p);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (frwl->flags & IP_FW_F_DRNG) && frwl->n_dst_p < 2 ) {
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf2("ip_fw_ctl: dst range set but n_dst_p=%d\n",
|
1994-11-16 10:17:11 +00:00
|
|
|
frwl->n_dst_p);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( frwl->n_src_p + frwl->n_dst_p > IP_FW_MAX_PORTS ) {
|
1994-11-28 12:35:14 +00:00
|
|
|
dprintf3("ip_fw_ctl: too many ports (%d+%d)\n",
|
1994-11-16 10:17:11 +00:00
|
|
|
frwl->n_src_p,frwl->n_dst_p);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if ( (frwl->flags & 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);
|
|
|
|
}
|
|
|
|
if ( stage == IP_ACCT_ZERO )
|
|
|
|
{
|
|
|
|
zero_fw_chain(ip_acct_chain);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
if ( stage == IP_ACCT_ADD
|
|
|
|
|| stage == IP_ACCT_DEL
|
|
|
|
) {
|
|
|
|
|
|
|
|
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-11-16 10:17:11 +00:00
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* Should be panic but...
|
|
|
|
*/
|
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-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)
|
1994-10-28 15:09:49 +00:00
|
|
|
int stage;
|
|
|
|
struct mbuf *m;
|
|
|
|
{
|
|
|
|
if ( stage == IP_FW_FLUSH )
|
|
|
|
{
|
1994-11-08 12:47:29 +00:00
|
|
|
free_fw_chain(&ip_fw_blk_chain);
|
|
|
|
free_fw_chain(&ip_fw_fwd_chain);
|
1994-10-28 15:09:49 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
if ( m == 0 )
|
|
|
|
{
|
|
|
|
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
|
|
|
|
* to blocking/forwarding chains or deleting'em
|
|
|
|
*/
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
if ( stage == IP_FW_ADD_BLK
|
|
|
|
|| stage == IP_FW_ADD_FWD
|
|
|
|
|| stage == IP_FW_DEL_BLK
|
|
|
|
|| stage == IP_FW_DEL_FWD
|
|
|
|
) {
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
struct ip_fw *frwl;
|
|
|
|
|
|
|
|
frwl=check_ipfw_struct(m);
|
|
|
|
if (frwl==NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
#ifdef nenado
|
|
|
|
if (!(frwl=check_ipfw_struct(m)))
|
|
|
|
return (EINVAL);
|
1994-11-08 12:47:29 +00:00
|
|
|
#endif
|
1994-10-28 15:09:49 +00:00
|
|
|
|
1994-11-16 10:17:11 +00:00
|
|
|
switch (stage) {
|
|
|
|
case IP_FW_ADD_BLK:
|
1994-12-12 18:10:41 +00:00
|
|
|
return(add_entry(&ip_fw_blk_chain,frwl));
|
1994-11-16 10:17:11 +00:00
|
|
|
case IP_FW_ADD_FWD:
|
1994-12-12 18:10:41 +00:00
|
|
|
return(add_entry(&ip_fw_fwd_chain,frwl));
|
1994-11-16 10:17:11 +00:00
|
|
|
case IP_FW_DEL_BLK:
|
1994-12-12 18:10:41 +00:00
|
|
|
return(del_entry(&ip_fw_blk_chain,frwl));
|
1994-11-16 10:17:11 +00:00
|
|
|
case IP_FW_DEL_FWD:
|
1994-12-12 18:10:41 +00:00
|
|
|
return(del_entry(&ip_fw_fwd_chain,frwl));
|
1994-11-16 10:17:11 +00:00
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* Should be panic but...
|
|
|
|
*/
|
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);
|
1994-10-28 15:09:49 +00:00
|
|
|
}
|
1994-11-16 10:17:11 +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 */
|