📄 ip_fw.c
字号:
/* * Copyright (c) 1993 Daniel Boulet * Copyright (c) 1994 Ugen J.S.Antsilevich * Copyright (c) 1996 Alex Nash * * 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. * * $Id: ip_fw.c,v 1.103.2.3 1999/04/26 14:59:02 luigi Exp $ *//* * Implement IP packet firewall */#if !defined(KLD_MODULE) && !defined(IPFIREWALL_MODULE)#include "opt_ipfw.h"#include "opt_ipdn.h"#include "opt_ipdivert.h"#include "opt_inet.h"#ifndef INET#error IPFIREWALL requires INET.#endif /* INET */#endif#include <sys/param.h>#include <sys/systm.h>#include <sys/malloc.h>#include <sys/mbuf.h>#include <sys/kernel.h>#include <sys/socket.h>#include <sys/socketvar.h>#include <sys/sysctl.h>#include <net/if.h>#include <netinet/in.h>#include <netinet/in_systm.h>#include <netinet/ip.h>#include <netinet/ip_var.h>#include <netinet/ip_icmp.h>#include <netinet/ip_fw.h>#ifdef DUMMYNET#include <net/route.h>#include <netinet/ip_dummynet.h>#endif#include <netinet/tcp.h>#include <netinet/tcp_timer.h>#include <netinet/tcp_var.h>#include <netinet/tcpip.h>#include <netinet/udp.h>#include <netinet/if_ether.h> /* XXX ethertype_ip */static int fw_debug = 1;#ifdef IPFIREWALL_VERBOSEstatic int fw_verbose = 1;#elsestatic int fw_verbose = 0;#endifstatic int fw_one_pass = 1 ;#ifdef IPFIREWALL_VERBOSE_LIMITstatic int fw_verbose_limit = IPFIREWALL_VERBOSE_LIMIT;#elsestatic int fw_verbose_limit = 0;#endif#define IPFW_DEFAULT_RULE ((u_int)(u_short)~0)LIST_HEAD (ip_fw_head, ip_fw_chain) ip_fw_chain;MALLOC_DEFINE(M_IPFW, "IpFw/IpAcct", "IpFw/IpAcct chain's");#ifdef SYSCTL_NODESYSCTL_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,one_pass,CTLFLAG_RW, &fw_one_pass, 0, "");SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose, CTLFLAG_RW, &fw_verbose, 0, "");SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit, CTLFLAG_RW, &fw_verbose_limit, 0, "");#endif#define dprintf(a) if (!fw_debug); else printf a#define print_ip(a) printf("%d.%d.%d.%d", \ (int)(ntohl(a.s_addr) >> 24) & 0xFF, \ (int)(ntohl(a.s_addr) >> 16) & 0xFF, \ (int)(ntohl(a.s_addr) >> 8) & 0xFF, \ (int)(ntohl(a.s_addr)) & 0xFF);#define dprint_ip(a) if (!fw_debug); else print_ip(a)static int add_entry __P((struct ip_fw_head *chainptr, struct ip_fw *frwl));static int del_entry __P((struct ip_fw_head *chainptr, u_short number));static int zero_entry __P((struct ip_fw *));static int check_ipfw_struct __P((struct ip_fw *m));static __inline int iface_match __P((struct ifnet *ifp, union ip_fw_if *ifu, int byname));static int ipopts_match __P((struct ip *ip, struct ip_fw *f));static __inline 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));static int icmptype_match __P((struct icmp * icmp, struct ip_fw * f));static void ipfw_report __P((struct ip_fw *f, struct ip *ip, struct ifnet *rif, struct ifnet *oif));static void flush_rule_ptrs(void);static int ip_fw_chk __P((struct ip **pip, int hlen, struct ifnet *oif, u_int16_t *cookie, struct mbuf **m, struct ip_fw_chain **flow_id, struct sockaddr_in **next_hop));static int ip_fw_ctl __P((struct sockopt *sopt));static char err_prefix[] = "ip_fw_ctl:";/* * Returns 1 if the port is matched by the vector, 0 otherwise */static __inline int port_match(u_short *portptr, int nports, u_short port, int range_flag){ if (!nports) return 1; if (range_flag) { if (portptr[0] <= port && port <= portptr[1]) { return 1; } nports -= 2; portptr += 2; } while (nports-- > 0) { if (*portptr++ == port) { return 1; } } return 0;}static inttcpflg_match(struct tcphdr *tcp, struct ip_fw *f){ u_char flg_set, flg_clr; if ((f->fw_tcpf & IP_FW_TCPF_ESTAB) && (tcp->th_flags & (IP_FW_TCPF_RST | IP_FW_TCPF_ACK))) return 1; flg_set = tcp->th_flags & f->fw_tcpf; flg_clr = tcp->th_flags & f->fw_tcpnf; if (flg_set != f->fw_tcpf) return 0; if (flg_clr) return 0; return 1;}static inticmptype_match(struct icmp *icmp, struct ip_fw *f){ int type; if (!(f->fw_flg & IP_FW_F_ICMPBIT)) return(1); type = icmp->icmp_type; /* check for matching type in the bitmap */ if (type < IP_FW_ICMPTYPES_MAX && (f->fw_uar.fw_icmptypes[type / (sizeof(unsigned) * 8)] & (1U << (type % (8 * sizeof(unsigned)))))) return(1); return(0); /* no match */}static intis_icmp_query(struct ip *ip){ const struct icmp *icmp; int icmp_type; icmp = (struct icmp *)((u_int32_t *)ip + ip->ip_hl); icmp_type = icmp->icmp_type; if (icmp_type == ICMP_ECHO || icmp_type == ICMP_ROUTERSOLICIT || icmp_type == ICMP_TSTAMP || icmp_type == ICMP_IREQ || icmp_type == ICMP_MASKREQ) return(1); return(0);}static intipopts_match(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; 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) { return 0; /*XXX*/ } } 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;}static __inline intiface_match(struct ifnet *ifp, union ip_fw_if *ifu, int byname){ /* Check by name or by IP address */ if (byname) { /* Check unit number (-1 is wildcard) */ if (ifu->fu_via_if.unit != -1 && ifp->if_unit != ifu->fu_via_if.unit) return(0); /* Check name */ if (strncmp(ifp->if_name, ifu->fu_via_if.name, FW_IFNLEN)) return(0); return(1); } else if (ifu->fu_via_ip.s_addr != 0) { /* Zero == wildcard */ struct ifaddr *ia; for (ia = ifp->if_addrhead.tqh_first; ia != NULL; ia = ia->ifa_link.tqe_next) { if (ia->ifa_addr == NULL) continue; if (ia->ifa_addr->sa_family != AF_INET) continue; if (ifu->fu_via_ip.s_addr != ((struct sockaddr_in *) (ia->ifa_addr))->sin_addr.s_addr) continue; return(1); } return(0); } return(1);}static voidipfw_report(struct ip_fw *f, struct ip *ip, struct ifnet *rif, struct ifnet *oif){ if (ip) { static u_int64_t counter; struct tcphdr *const tcp = (struct tcphdr *) ((u_int32_t *) ip+ ip->ip_hl); struct udphdr *const udp = (struct udphdr *) ((u_int32_t *) ip+ ip->ip_hl); struct icmp *const icmp = (struct icmp *) ((u_int32_t *) ip + ip->ip_hl); int count; count = f ? f->fw_pcnt : ++counter; if (fw_verbose_limit != 0 && count > fw_verbose_limit) return; /* Print command name */ printf("ipfw: %d ", f ? f->fw_number : -1); if (!f) printf("Refuse"); else switch (f->fw_flg & IP_FW_F_COMMAND) { case IP_FW_F_DENY: printf("Deny"); break; case IP_FW_F_REJECT: if (f->fw_reject_code == IP_FW_REJECT_RST) printf("Reset"); else printf("Unreach"); break; case IP_FW_F_ACCEPT: printf("Accept"); break; case IP_FW_F_COUNT: printf("Count"); break; case IP_FW_F_DIVERT: printf("Divert %d", f->fw_divert_port); break; case IP_FW_F_TEE: printf("Tee %d", f->fw_divert_port); break; case IP_FW_F_SKIPTO: printf("SkipTo %d", f->fw_skipto_rule); break;#ifdef DUMMYNET case IP_FW_F_PIPE: printf("Pipe %d", f->fw_skipto_rule); break;#endif#ifdef IPFIREWALL_FORWARD case IP_FW_F_FWD: printf("Forward to "); print_ip(f->fw_fwd_ip.sin_addr); if (f->fw_fwd_ip.sin_port) printf(":%d", f->fw_fwd_ip.sin_port); break;#endif default: printf("UNKNOWN"); break; } printf(" "); switch (ip->ip_p) { case IPPROTO_TCP: printf("TCP "); print_ip(ip->ip_src); if ((ip->ip_off & IP_OFFMASK) == 0) printf(":%d ", ntohs(tcp->th_sport)); else printf(" "); print_ip(ip->ip_dst); if ((ip->ip_off & IP_OFFMASK) == 0) printf(":%d", ntohs(tcp->th_dport)); break; case IPPROTO_UDP: printf("UDP "); print_ip(ip->ip_src); if ((ip->ip_off & IP_OFFMASK) == 0) printf(":%d ", ntohs(udp->uh_sport)); else printf(" "); print_ip(ip->ip_dst); if ((ip->ip_off & IP_OFFMASK) == 0) printf(":%d", ntohs(udp->uh_dport)); break; case IPPROTO_ICMP: if ((ip->ip_off & IP_OFFMASK) == 0) printf("ICMP:%u.%u ", icmp->icmp_type, icmp->icmp_code); else printf("ICMP "); 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 (oif) printf(" out via %s%d", oif->if_name, oif->if_unit); else if (rif) printf(" in via %s%d", rif->if_name, rif->if_unit); if ((ip->ip_off & IP_OFFMASK)) printf(" Fragment = %d",ip->ip_off & IP_OFFMASK); printf("\n"); if (fw_verbose_limit != 0 && count == fw_verbose_limit) printf("ipfw: limit reached on rule #%d\n", f ? f->fw_number : -1); }}/* * given an ip_fw_chain *, lookup_next_rule will return a pointer * of the same type to the next one. This can be either the jump * target (for skipto instructions) or the next one in the chain (in * all other cases including a missing jump target). * Backward jumps are not allowed, so start looking from the next * rule... */ static struct ip_fw_chain * lookup_next_rule(struct ip_fw_chain *me);static struct ip_fw_chain *lookup_next_rule(struct ip_fw_chain *me){ struct ip_fw_chain *chain ; int rule = me->rule->fw_skipto_rule ; /* guess... */ if ( (me->rule->fw_flg & IP_FW_F_COMMAND) == IP_FW_F_SKIPTO ) for (chain = me->chain.le_next; chain ; chain = chain->chain.le_next ) if (chain->rule->fw_number >= rule) return chain ; return me->chain.le_next ; /* failure or not a skipto */}/* * Parameters: * * pip Pointer to packet header (struct ip **) * bridge_ipfw extension: pip = NULL means a complete ethernet packet * including ethernet header in the mbuf. Other fields * are ignored/invalid. * * hlen Packet header length * oif Outgoing interface, or NULL if packet is incoming * *cookie Skip up to the first rule past this rule number; * *m The packet; we set to NULL when/if we nuke it. * *flow_id pointer to the last matching rule (in/out) * *next_hop socket we are forwarding to (in/out). * * Return value: * * 0 The packet is to be accepted and routed normally OR * the packet was denied/rejected and has been dropped; * in the latter case, *m is equal to NULL upon return. * port Divert the packet to port. */static int ip_fw_chk(struct ip **pip, int hlen, struct ifnet *oif, u_int16_t *cookie, struct mbuf **m, struct ip_fw_chain **flow_id, struct sockaddr_in **next_hop){ struct ip_fw_chain *chain; struct ip_fw *rule = NULL; struct ip *ip = NULL ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -