2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * I hate legaleese, don't you ?
9 static const char sccsid[] = "%W% %G% (C) 1993-2000 Darren Reed";
10 static const char rcsid[] = "@(#)$Id: ip_sfil.c,v 2.23.2.16 2002/04/05 08:43:25 darrenr Exp $";
13 #include <sys/types.h>
14 #include <sys/errno.h>
15 #include <sys/param.h>
16 #include <sys/cpuvar.h>
18 #include <sys/ioctl.h>
19 #include <sys/filio.h>
20 #include <sys/systm.h>
23 #include <sys/sunddi.h>
24 #include <sys/ksynch.h>
26 #include <sys/mkdev.h>
27 #include <sys/protosw.h>
28 #include <sys/socket.h>
29 #include <sys/dditypes.h>
30 #include <sys/cmn_err.h>
33 #include <net/route.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <netinet/ip_var.h>
38 #include <netinet/tcp.h>
39 #include <netinet/udp.h>
40 #include <netinet/tcpip.h>
41 #include <netinet/ip_icmp.h>
42 #include "ip_compat.h"
44 # include <netinet/icmp6.h>
52 #include <inet/ip_ire.h>
54 #define MIN(a,b) (((a)<(b))?(a):(b))
58 extern fr_flags, fr_active;
61 int ipl_unreach = ICMP_UNREACH_HOST;
62 u_long ipl_frouteok[2] = {0, 0};
63 static int frzerostats __P((caddr_t));
64 static u_long *ip_ttl_ptr;
66 static int frrequest __P((minor_t, int, caddr_t, int));
67 static int send_ip __P((fr_info_t *fin, mblk_t *m));
68 kmutex_t ipl_mutex, ipf_authmx, ipf_rw;
69 KRWLOCK_T ipf_mutex, ipfs_mutex, ipf_solaris;
70 KRWLOCK_T ipf_frag, ipf_state, ipf_nat, ipf_natfrag, ipf_auth;
71 kcondvar_t iplwait, ipfauthwait;
79 cmn_err(CE_CONT, "ipldetach()\n");
82 for (i = IPL_LOGMAX; i >= 0; i--)
85 i = FR_INQUE|FR_OUTQUE;
86 (void) frflush(IPL_LOGIPF, FR_INQUE|FR_OUTQUE);
91 cv_destroy(&ipfauthwait);
92 mutex_destroy(&ipf_authmx);
93 mutex_destroy(&ipl_mutex);
94 mutex_destroy(&ipf_rw);
95 RW_DESTROY(&ipf_mutex);
96 RW_DESTROY(&ipf_frag);
97 RW_DESTROY(&ipf_state);
98 RW_DESTROY(&ipf_natfrag);
100 RW_DESTROY(&ipf_auth);
101 RW_DESTROY(&ipfs_mutex);
102 /* NOTE: This lock is acquired in ipf_detach */
103 RWLOCK_EXIT(&ipf_solaris);
104 RW_DESTROY(&ipf_solaris);
109 int iplattach __P((void))
114 cmn_err(CE_CONT, "iplattach()\n");
116 bzero((char *)frcache, sizeof(frcache));
117 mutex_init(&ipf_rw, "ipf rw mutex", MUTEX_DRIVER, NULL);
118 mutex_init(&ipl_mutex, "ipf log mutex", MUTEX_DRIVER, NULL);
119 mutex_init(&ipf_authmx, "ipf auth log mutex", MUTEX_DRIVER, NULL);
120 RWLOCK_INIT(&ipf_solaris, "ipf filter load/unload mutex", NULL);
121 RWLOCK_INIT(&ipf_mutex, "ipf filter rwlock", NULL);
122 RWLOCK_INIT(&ipfs_mutex, "ipf solaris mutex", NULL);
123 RWLOCK_INIT(&ipf_frag, "ipf fragment rwlock", NULL);
124 RWLOCK_INIT(&ipf_state, "ipf IP state rwlock", NULL);
125 RWLOCK_INIT(&ipf_nat, "ipf IP NAT rwlock", NULL);
126 RWLOCK_INIT(&ipf_natfrag, "ipf IP NAT-Frag rwlock", NULL);
127 RWLOCK_INIT(&ipf_auth, "ipf IP User-Auth rwlock", NULL);
128 cv_init(&iplwait, "ipl condvar", CV_DRIVER, NULL);
129 cv_init(&ipfauthwait, "ipf auth condvar", CV_DRIVER, NULL);
133 if (nat_init() == -1)
135 if (fr_stateinit() == -1)
137 if (appr_init() == -1)
142 * XXX - There is no terminator for this array, so it is not possible
143 * to tell if what we are looking for is missing and go off the end
147 if (!strcmp(ip_param_arr[i].ip_param_name, "ip_def_ttl")) {
148 ip_ttl_ptr = &ip_param_arr[i].ip_param_value;
156 static int frzerostats(data)
163 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
167 bzero((char *)frstats, sizeof(*frstats) * 2);
174 * Filter ioctl interface.
176 int iplioctl(dev, cmd, data, mode, cp, rp)
192 cmn_err(CE_CONT, "iplioctl(%x,%x,%x,%d,%x,%d)\n",
193 dev, cmd, data, mode, cp, rp);
195 unit = getminor(dev);
196 if (IPL_LOGMAX < unit)
202 READ_ENTER(&ipf_solaris);
203 if (unit == IPL_LOGNAT) {
204 error = nat_ioctl((caddr_t)data, cmd, mode);
205 RWLOCK_EXIT(&ipf_solaris);
208 if (unit == IPL_LOGSTATE) {
209 error = fr_state_ioctl((caddr_t)data, cmd, mode);
210 RWLOCK_EXIT(&ipf_solaris);
213 if (unit == IPL_LOGAUTH) {
214 error = fr_auth_ioctl((caddr_t)data, mode, cmd, NULL, NULL);
215 RWLOCK_EXIT(&ipf_solaris);
224 if (!(mode & FWRITE))
227 error = IRCOPY((caddr_t)data, (caddr_t)&enable,
232 if (!(mode & FWRITE))
235 WRITE_ENTER(&ipf_mutex);
236 error = IRCOPY((caddr_t)data, (caddr_t)&fr_flags,
238 RWLOCK_EXIT(&ipf_mutex);
242 error = IWCOPY((caddr_t)&fr_flags, (caddr_t)data,
251 if (!(mode & FWRITE))
254 error = frrequest(unit, cmd, (caddr_t)data, fr_active);
259 if (!(mode & FWRITE))
262 error = frrequest(unit, cmd, (caddr_t)data,
266 if (!(mode & FWRITE))
269 WRITE_ENTER(&ipf_mutex);
270 bzero((char *)frcache, sizeof(frcache[0]) * 2);
271 error = IWCOPY((caddr_t)&fr_active, (caddr_t)data,
275 fr_active = 1 - fr_active;
276 RWLOCK_EXIT(&ipf_mutex);
283 READ_ENTER(&ipf_mutex);
285 RWLOCK_EXIT(&ipf_mutex);
286 error = IWCOPYPTR((caddr_t)&fio, (caddr_t)data, sizeof(fio));
292 if (!(mode & FWRITE))
295 error = frzerostats((caddr_t)data);
298 if (!(mode & FWRITE))
301 error = IRCOPY((caddr_t)data, (caddr_t)&tmp,
304 tmp = frflush(unit, tmp);
305 error = IWCOPY((caddr_t)&tmp, (caddr_t)data,
313 error = IRCOPY((caddr_t)data, (caddr_t)&tmp, sizeof(tmp));
324 if (!(mode & FWRITE))
327 tmp = ipflog_clear(unit);
328 error = IWCOPY((caddr_t)&tmp, (caddr_t)data,
334 #endif /* IPFILTER_LOG */
336 if (!(mode & FWRITE))
342 error = IWCOPYPTR((caddr_t)ipfr_fragstats(), (caddr_t)data,
348 int copy = (int)iplused[IPL_LOGIPF];
350 error = IWCOPY((caddr_t)©, (caddr_t)data, sizeof(copy));
360 RWLOCK_EXIT(&ipf_solaris);
365 ill_t *get_unit(name, v)
369 size_t len = strlen(name) + 1; /* includes \0 */
379 for (il = ill_g_head; il; il = il->ill_next)
380 if ((len == il->ill_name_length) && (il->ill_sap == sap) &&
381 !strncmp(il->ill_name, name, len))
387 static int frrequest(unit, req, data, set)
392 register frentry_t *fp, *f, **fprev;
393 register frentry_t **ftail;
394 frgroup_t *fg = NULL;
395 int error = 0, in, i;
405 error = IRCOPYPTR(data, (caddr_t)fp, sizeof(*fp));
411 fp->fr_sap = IP_DL_SAP;
412 else if (fp->fr_v == 6)
413 fp->fr_sap = IP6_DL_SAP;
420 WRITE_ENTER(&ipf_mutex);
422 * Check that the group number does exist and that if a head group
423 * has been specified, doesn't exist.
425 if ((req != SIOCZRLST) && fp->fr_grhead &&
426 fr_findgroup(fp->fr_grhead, fp->fr_flags, unit, set, NULL)) {
430 if ((req != SIOCZRLST) && fp->fr_group &&
431 !fr_findgroup(fp->fr_group, fp->fr_flags, unit, set, NULL)) {
436 in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
438 if (unit == IPL_LOGAUTH)
439 ftail = fprev = &ipauth;
440 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 4))
441 ftail = fprev = &ipacct[in][set];
442 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 4))
443 ftail = fprev = &ipfilter[in][set];
445 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 6))
446 ftail = fprev = &ipacct6[in][set];
447 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 6))
448 ftail = fprev = &ipfilter6[in][set];
455 group = fp->fr_group;
457 fg = fr_findgroup(group, fp->fr_flags, unit, set, NULL);
462 ftail = fprev = fg->fg_start;
465 bzero((char *)frcache, sizeof(frcache[0]) * 2);
467 for (i = 0; i < 4; i++) {
468 if ((fp->fr_ifnames[i][1] == '\0') &&
469 ((fp->fr_ifnames[i][0] == '-') ||
470 (fp->fr_ifnames[i][0] == '*'))) {
471 fp->fr_ifas[i] = NULL;
472 } else if (*fp->fr_ifnames[i]) {
473 fp->fr_ifas[i] = GETUNIT(fp->fr_ifnames[i], fp->fr_v);
475 fp->fr_ifas[i] = (void *)-1;
481 fp->fr_flags &= ~FR_DUP;
482 if (*fdp->fd_ifname) {
483 ill = get_unit(fdp->fd_ifname, (int)fp->fr_v);
486 else if ((ipif = ill->ill_ipif) && (fp->fr_v == 4)) {
488 ire = ire_ctable_lookup(ipif->ipif_local_addr, 0,
489 IRE_LOCAL, NULL, NULL,
492 ire = ire_lookup_myaddr(ipif->ipif_local_addr);
497 fp->fr_flags |= FR_DUP;
500 else if ((ipif = ill->ill_ipif) && (fp->fr_v == 6)) {
501 ire = ire_ctable_lookup_v6(&ipif->ipif_v6lcl_addr, 0,
502 IRE_LOCAL, NULL, NULL,
507 fp->fr_flags |= FR_DUP;
510 fdp->fd_ifp = (struct ifnet *)ire;
515 if (*fdp->fd_ifname) {
516 ill = get_unit(fdp->fd_ifname, (int)fp->fr_v);
519 else if ((ipif = ill->ill_ipif) && (fp->fr_v == 4)) {
521 ire = ire_ctable_lookup(ipif->ipif_local_addr, 0,
522 IRE_LOCAL, NULL, NULL,
525 ire = ire_lookup_myaddr(ipif->ipif_local_addr);
531 else if ((ipif = ill->ill_ipif) && (fp->fr_v == 6)) {
532 ire = ire_ctable_lookup_v6(&ipif->ipif_v6lcl_addr, 0,
533 IRE_LOCAL, NULL, NULL,
539 fdp->fd_ifp = (struct ifnet *)ire;
543 * Look for a matching filter rule, but don't include the next or
544 * interface pointer in the comparison (fr_next, fr_ifa).
546 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_ip, pp = &fp->fr_cksum;
550 for (; (f = *ftail); ftail = &f->fr_next)
551 if ((fp->fr_cksum == f->fr_cksum) &&
552 !bcmp((char *)&f->fr_ip, (char *)&fp->fr_ip, FR_CMPSIZ))
556 * If zero'ing statistics, copy current to caller and zero.
558 if (req == SIOCZRLST) {
563 MUTEX_DOWNGRADE(&ipf_mutex);
564 error = IWCOPYPTR((caddr_t)f, data, sizeof(*f));
573 if (req != SIOCINAFR && req != SIOCINIFR)
579 while (--fp->fr_hits && (f = *ftail))
586 if (req == SIOCRMAFR || req == SIOCRMIFR) {
591 * Only return EBUSY if there is a group list, else
592 * it's probably just state information referencing
595 if ((f->fr_ref > 1) && f->fr_grp) {
599 if (fg && fg->fg_head)
600 fg->fg_head->fr_ref--;
602 fr_delgroup(f->fr_grhead, fp->fr_flags,
604 fixskip(fprev, f, -1);
615 KMALLOC(f, frentry_t *);
617 if (fg && fg->fg_head)
618 fg->fg_head->fr_ref++;
619 bcopy((char *)fp, (char *)f, sizeof(*f));
624 if (req == SIOCINIFR || req == SIOCINAFR)
625 fixskip(fprev, f, 1);
627 group = f->fr_grhead;
629 fg = fr_addgroup(group, f, unit, set);
635 RWLOCK_EXIT(&ipf_mutex);
641 * routines below for saving IP headers to buffer
643 int iplopen(devp, flags, otype, cred)
648 minor_t min = getminor(*devp);
651 cmn_err(CE_CONT, "iplopen(%x,%x,%x,%x)\n", devp, flags, otype, cred);
653 if ((fr_running <= 0) || !(otype & OTYP_CHR))
655 min = (IPL_LOGMAX < min) ? ENXIO : 0;
660 int iplclose(dev, flags, otype, cred)
665 minor_t min = getminor(dev);
668 cmn_err(CE_CONT, "iplclose(%x,%x,%x,%x)\n", dev, flags, otype, cred);
670 min = (IPL_LOGMAX < min) ? ENXIO : 0;
677 * both of these must operate with at least splnet() lest they be
678 * called during packet processing and cause an inconsistancy to appear in
681 int iplread(dev, uio, cp)
683 register struct uio *uio;
687 cmn_err(CE_CONT, "iplread(%x,%x,%x)\n", dev, uio, cp);
689 return ipflog_read(getminor(dev), uio);
691 #endif /* IPFILTER_LOG */
695 * send_reset - this could conceivably be a call to tcp_respond(), but that
696 * requires a large amount of setting up and isn't any more efficient.
698 int send_reset(oip, fin)
702 tcphdr_t *tcp, *tcp2;
706 ip6_t *ip6, *oip6 = (ip6_t *)oip;
710 tcp = (struct tcphdr *)fin->fin_dp;
711 if (tcp->th_flags & TH_RST)
713 tlen = (tcp->th_flags & (TH_SYN|TH_FIN)) ? 1 : 0;
716 hlen = sizeof(ip6_t);
720 hlen += sizeof(*tcp2);
721 if ((m = (mblk_t *)allocb(hlen + 16, BPRI_HI)) == NULL)
726 m->b_wptr = m->b_rptr + hlen;
727 bzero((char *)m->b_rptr, hlen);
728 tcp2 = (struct tcphdr *)(m->b_rptr + hlen - sizeof(*tcp2));
729 tcp2->th_dport = tcp->th_sport;
730 tcp2->th_sport = tcp->th_dport;
731 if (tcp->th_flags & TH_ACK) {
732 tcp2->th_seq = tcp->th_ack;
733 tcp2->th_flags = TH_RST|TH_ACK;
735 tcp2->th_ack = ntohl(tcp->th_seq);
736 tcp2->th_ack += tlen;
737 tcp2->th_ack = htonl(tcp2->th_ack);
738 tcp2->th_flags = TH_RST;
740 tcp2->th_off = sizeof(struct tcphdr) >> 2;
741 tcp2->th_flags = TH_RST|TH_ACK;
744 * This is to get around a bug in the Solaris 2.4/2.5 TCP checksum
745 * computation that is done by their put routine.
747 tcp2->th_sum = htons(0x14);
749 if (fin->fin_v == 6) {
750 ip6 = (ip6_t *)m->b_rptr;
751 ip6->ip6_src = oip6->ip6_dst;
752 ip6->ip6_dst = oip6->ip6_src;
753 ip6->ip6_plen = htons(sizeof(*tcp));
754 ip6->ip6_nxt = IPPROTO_TCP;
758 ip = (ip_t *)m->b_rptr;
759 ip->ip_src.s_addr = oip->ip_dst.s_addr;
760 ip->ip_dst.s_addr = oip->ip_src.s_addr;
761 ip->ip_hl = sizeof(*ip) >> 2;
762 ip->ip_p = IPPROTO_TCP;
763 ip->ip_len = htons(sizeof(*ip) + sizeof(*tcp));
764 ip->ip_tos = oip->ip_tos;
766 return send_ip(fin, m);
770 int static send_ip(fin, m)
774 RWLOCK_EXIT(&ipfs_mutex);
775 RWLOCK_EXIT(&ipf_solaris);
777 if (fin->fin_v == 6) {
778 extern void ip_wput_v6 __P((queue_t *, mblk_t *));
781 ip6 = (ip6_t *)m->b_rptr;
785 ip_wput_v6(((qif_t *)fin->fin_qif)->qf_ill->ill_wq, m);
791 ip = (ip_t *)m->b_rptr;
792 ip->ip_v = IPVERSION;
793 ip->ip_ttl = (u_char)(*ip_ttl_ptr);
794 ip_wput(((qif_t *)fin->fin_qif)->qf_ill->ill_wq, m);
796 READ_ENTER(&ipf_solaris);
797 READ_ENTER(&ipfs_mutex);
802 int send_icmp_err(oip, type, fin, dst)
820 if ((type < 0) || (type > ICMP_MAXTYPE))
823 code = fin->fin_icode;
825 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
833 if (oip->ip_v == 6) {
836 sz += MIN(m->b_wptr - m->b_rptr, 512);
837 hlen = sizeof(ip6_t);
838 type = icmptoicmp6types[type];
839 if (type == ICMP6_DST_UNREACH)
840 code = icmptoicmp6unreach[code];
844 if ((oip->ip_p == IPPROTO_ICMP) &&
845 !(fin->fin_fi.fi_fl & FI_SHORT))
846 switch (ntohs(fin->fin_data[0]) >> 8)
857 sz = sizeof(ip_t) * 2;
858 sz += 8; /* 64 bits of data */
862 sz += offsetof(struct icmp, icmp_ip);
863 if ((mb = (mblk_t *)allocb((size_t)sz + 16, BPRI_HI)) == NULL)
867 mb->b_wptr = mb->b_rptr + sz;
868 bzero((char *)mb->b_rptr, (size_t)sz);
869 icmp = (struct icmp *)(mb->b_rptr + sizeof(*ip));
870 icmp->icmp_type = type;
871 icmp->icmp_code = code;
872 icmp->icmp_cksum = 0;
874 if (type == ICMP_UNREACH && (il = qif->qf_ill) &&
875 fin->fin_icode == ICMP_UNREACH_NEEDFRAG)
876 icmp->icmp_nextmtu = htons(il->ill_max_frag);
880 if (oip->ip_v == 6) {
881 struct in6_addr dst6;
885 if (fr_ifpaddr(6, ((qif_t *)fin->fin_qif)->qf_ill,
886 (struct in_addr *)&dst6) == -1)
889 dst6 = oip6->ip6_dst;
893 ip6 = (ip6_t *)mb->b_rptr;
897 ip6->ip6_plen = htons(sz);
898 ip6->ip6_nxt = IPPROTO_ICMPV6;
900 ip6->ip6_dst = oip6->ip6_src;
901 sz -= offsetof(struct icmp, icmp_ip);
902 bcopy((char *)m->b_rptr, (char *)&icmp->icmp_ip, sz);
903 icmp->icmp_cksum = csz - sizeof(ip6_t);
907 ip = (ip_t *)mb->b_rptr;
908 ip->ip_v = IPVERSION;
909 ip->ip_hl = (sizeof(*ip) >> 2);
910 ip->ip_p = IPPROTO_ICMP;
911 ip->ip_id = oip->ip_id;
913 ip->ip_ttl = (u_char)(*ip_ttl_ptr);
914 ip->ip_tos = oip->ip_tos;
915 ip->ip_len = (u_short)htons(sz);
917 if (fr_ifpaddr(4, ((qif_t *)fin->fin_qif)->qf_ill,
923 ip->ip_dst = oip->ip_src;
924 bcopy((char *)oip, (char *)&icmp->icmp_ip, sizeof(*oip));
925 bcopy((char *)oip + (oip->ip_hl << 2),
926 (char *)&icmp->icmp_ip + sizeof(*oip), 8);
927 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
932 * Need to exit out of these so we don't recursively call rw_enter
935 return send_ip(fin, mb);