The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/ip6_fw.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $FreeBSD: releng/5.0/sys/netinet6/ip6_fw.c 102397 2002-08-25 03:50:29Z cjc $    */
    2 /*      $KAME: ip6_fw.c,v 1.21 2001/01/24 01:25:32 itojun Exp $ */
    3 
    4 /*
    5  * Copyright (C) 1998, 1999, 2000 and 2001 WIDE Project.
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of the project nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 /*
   34  * Copyright (c) 1993 Daniel Boulet
   35  * Copyright (c) 1994 Ugen J.S.Antsilevich
   36  * Copyright (c) 1996 Alex Nash
   37  *
   38  * Redistribution and use in source forms, with and without modification,
   39  * are permitted provided that this entire comment appears intact.
   40  *
   41  * Redistribution in binary form may occur without any restrictions.
   42  * Obviously, it would be nice if you gave credit where credit is due
   43  * but requiring it would be too onerous.
   44  *
   45  * This software is provided ``AS IS'' without any warranties of any kind.
   46  */
   47 
   48 /*
   49  * Implement IPv6 packet firewall
   50  */
   51 
   52 #if !defined(KLD_MODULE)
   53 #include "opt_ip6fw.h"
   54 #include "opt_inet.h"
   55 #include "opt_inet6.h"
   56 #endif
   57 
   58 #ifdef IP6DIVERT
   59 #error "NOT SUPPORTED IPV6 DIVERT"
   60 #endif
   61 #ifdef IP6FW_DIVERT_RESTART
   62 #error "NOT SUPPORTED IPV6 DIVERT"
   63 #endif
   64 
   65 #include <sys/param.h>
   66 #include <sys/systm.h>
   67 #include <sys/malloc.h>
   68 #include <sys/mbuf.h>
   69 #include <sys/queue.h>
   70 #include <sys/kernel.h>
   71 #include <sys/socket.h>
   72 #include <sys/socketvar.h>
   73 #include <sys/syslog.h>
   74 #include <sys/time.h>
   75 #include <net/if.h>
   76 #include <net/route.h>
   77 #include <netinet/in_systm.h>
   78 #include <netinet/in.h>
   79 #include <netinet/ip.h>
   80 
   81 #include <netinet/ip6.h>
   82 #include <netinet6/ip6_var.h>
   83 #include <netinet6/in6_var.h>
   84 #include <netinet/icmp6.h>
   85 
   86 #include <netinet/in_pcb.h>
   87 
   88 #include <netinet6/ip6_fw.h>
   89 #include <netinet/ip_var.h>
   90 #include <netinet/tcp.h>
   91 #include <netinet/tcp_seq.h>
   92 #include <netinet/tcp_timer.h>
   93 #include <netinet/tcp_var.h>
   94 #include <netinet/udp.h>
   95 
   96 #include <sys/sysctl.h>
   97 
   98 #include <net/net_osdep.h>
   99 
  100 MALLOC_DEFINE(M_IP6FW, "Ip6Fw/Ip6Acct", "Ip6Fw/Ip6Acct chain's");
  101 
  102 static int fw6_debug = 1;
  103 #ifdef IPV6FIREWALL_VERBOSE
  104 static int fw6_verbose = 1;
  105 #else
  106 static int fw6_verbose = 0;
  107 #endif
  108 #ifdef IPV6FIREWALL_VERBOSE_LIMIT
  109 static int fw6_verbose_limit = IPV6FIREWALL_VERBOSE_LIMIT;
  110 #else
  111 static int fw6_verbose_limit = 0;
  112 #endif
  113 
  114 static LIST_HEAD (ip6_fw_head, ip6_fw_chain) ip6_fw_chain;
  115 
  116 #ifdef SYSCTL_NODE
  117 SYSCTL_DECL(_net_inet6_ip6);
  118 SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW | CTLFLAG_SECURE,
  119     0, "Firewall");
  120 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, enable, CTLFLAG_RW | CTLFLAG_SECURE,
  121         &ip6_fw_enable, 0, "Enable ip6fw");
  122 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, debug, CTLFLAG_RW, &fw6_debug, 0, "");
  123 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, verbose, CTLFLAG_RW | CTLFLAG_SECURE,
  124     &fw6_verbose, 0, "");
  125 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, verbose_limit, CTLFLAG_RW, &fw6_verbose_limit, 0, "");
  126 #endif
  127 
  128 #define dprintf(a)      do {                                            \
  129                                 if (fw6_debug)                          \
  130                                         printf a;                       \
  131                         } while (0)
  132 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
  133 
  134 static int      add_entry6 __P((struct ip6_fw_head *chainptr, struct ip6_fw *frwl));
  135 static int      del_entry6 __P((struct ip6_fw_head *chainptr, u_short number));
  136 static int      zero_entry6 __P((struct mbuf *m));
  137 static struct ip6_fw *check_ip6fw_struct __P((struct ip6_fw *m));
  138 static struct ip6_fw *check_ip6fw_mbuf __P((struct mbuf *fw));
  139 static int      ip6opts_match __P((struct ip6_hdr **ip6, struct ip6_fw *f,
  140                                    struct mbuf **m,
  141                                    int *off, int *nxt, u_short *offset));
  142 static int      port_match6 __P((u_short *portptr, int nports, u_short port,
  143                                 int range_flag));
  144 static int      tcp6flg_match __P((struct tcphdr *tcp6, struct ip6_fw *f));
  145 static int      icmp6type_match __P((struct icmp6_hdr *  icmp, struct ip6_fw * f));
  146 static void     ip6fw_report __P((struct ip6_fw *f, struct ip6_hdr *ip6,
  147                                 struct ifnet *rif, struct ifnet *oif, int off, int nxt));
  148 
  149 static int      ip6_fw_chk __P((struct ip6_hdr **pip6,
  150                         struct ifnet *oif, u_int16_t *cookie, struct mbuf **m));
  151 static int      ip6_fw_ctl __P((int stage, struct mbuf **mm));
  152 
  153 static char err_prefix[] = "ip6_fw_ctl:";
  154 
  155 /*
  156  * Returns 1 if the port is matched by the vector, 0 otherwise
  157  */
  158 static
  159 __inline int
  160 port_match6(u_short *portptr, int nports, u_short port, int range_flag)
  161 {
  162         if (!nports)
  163                 return 1;
  164         if (range_flag) {
  165                 if (portptr[0] <= port && port <= portptr[1]) {
  166                         return 1;
  167                 }
  168                 nports -= 2;
  169                 portptr += 2;
  170         }
  171         while (nports-- > 0) {
  172                 if (*portptr++ == port) {
  173                         return 1;
  174                 }
  175         }
  176         return 0;
  177 }
  178 
  179 static int
  180 tcp6flg_match(struct tcphdr *tcp6, struct ip6_fw *f)
  181 {
  182         u_char          flg_set, flg_clr;
  183         
  184         /*
  185          * If an established connection is required, reject packets that
  186          * have only SYN of RST|ACK|SYN set.  Otherwise, fall through to
  187          * other flag requirements.
  188          */
  189         if ((f->fw_ipflg & IPV6_FW_IF_TCPEST) &&
  190             ((tcp6->th_flags & (IPV6_FW_TCPF_RST | IPV6_FW_TCPF_ACK |
  191             IPV6_FW_TCPF_SYN)) == IPV6_FW_TCPF_SYN))
  192                 return 0;
  193 
  194         flg_set = tcp6->th_flags & f->fw_tcpf;
  195         flg_clr = tcp6->th_flags & f->fw_tcpnf;
  196 
  197         if (flg_set != f->fw_tcpf)
  198                 return 0;
  199         if (flg_clr)
  200                 return 0;
  201 
  202         return 1;
  203 }
  204 
  205 static int
  206 icmp6type_match(struct icmp6_hdr *icmp6, struct ip6_fw *f)
  207 {
  208         int type;
  209 
  210         if (!(f->fw_flg & IPV6_FW_F_ICMPBIT))
  211                 return(1);
  212 
  213         type = icmp6->icmp6_type;
  214 
  215         /* check for matching type in the bitmap */
  216         if (type < IPV6_FW_ICMPTYPES_DIM * sizeof(unsigned) * 8 &&
  217                 (f->fw_icmp6types[type / (sizeof(unsigned) * 8)] &
  218                 (1U << (type % (8 * sizeof(unsigned))))))
  219                 return(1);
  220 
  221         return(0); /* no match */
  222 }
  223 
  224 static int
  225 is_icmp6_query(struct ip6_hdr *ip6, int off)
  226 {
  227         const struct icmp6_hdr *icmp6;
  228         int icmp6_type;
  229 
  230         icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
  231         icmp6_type = icmp6->icmp6_type;
  232 
  233         if (icmp6_type == ICMP6_ECHO_REQUEST ||
  234             icmp6_type == ICMP6_MEMBERSHIP_QUERY ||
  235             icmp6_type == ICMP6_WRUREQUEST ||
  236             icmp6_type == ICMP6_FQDN_QUERY ||
  237             icmp6_type == ICMP6_NI_QUERY)
  238                 return(1);
  239 
  240         return(0);
  241 }
  242 
  243 static int
  244 ip6opts_match(struct ip6_hdr **pip6, struct ip6_fw *f, struct mbuf **m,
  245               int *off, int *nxt, u_short *offset)
  246 {
  247         int len;
  248         struct ip6_hdr *ip6 = *pip6;
  249         struct ip6_ext *ip6e;
  250         u_char  opts, nopts, nopts_sve;
  251 
  252         opts = f->fw_ip6opt;
  253         nopts = nopts_sve = f->fw_ip6nopt;
  254 
  255         *nxt = ip6->ip6_nxt;
  256         *off = sizeof(struct ip6_hdr);
  257         len = ntohs(ip6->ip6_plen) + sizeof(struct ip6_hdr);
  258         while (*off < len) {
  259                 ip6e = (struct ip6_ext *)((caddr_t) ip6 + *off);
  260                 if ((*m)->m_len < *off + sizeof(*ip6e))
  261                         goto opts_check;        /* XXX */
  262 
  263                 switch(*nxt) {
  264                 case IPPROTO_FRAGMENT:
  265                         if ((*m)->m_len >= *off + sizeof(struct ip6_frag)) {
  266                                 struct ip6_frag *ip6f;
  267 
  268                                 ip6f = (struct ip6_frag *) ((caddr_t)ip6 + *off);
  269                                 *offset = ip6f->ip6f_offlg & IP6F_OFF_MASK;
  270                         }
  271                         opts &= ~IPV6_FW_IP6OPT_FRAG;
  272                         nopts &= ~IPV6_FW_IP6OPT_FRAG;
  273                         *off += sizeof(struct ip6_frag);
  274                         break;
  275                 case IPPROTO_AH:
  276                         opts &= ~IPV6_FW_IP6OPT_AH;
  277                         nopts &= ~IPV6_FW_IP6OPT_AH;
  278                         *off += (ip6e->ip6e_len + 2) << 2;
  279                         break;
  280                 default:
  281                         switch (*nxt) {
  282                         case IPPROTO_HOPOPTS:
  283                                 opts &= ~IPV6_FW_IP6OPT_HOPOPT;
  284                                 nopts &= ~IPV6_FW_IP6OPT_HOPOPT;
  285                                 break;
  286                         case IPPROTO_ROUTING:
  287                                 opts &= ~IPV6_FW_IP6OPT_ROUTE;
  288                                 nopts &= ~IPV6_FW_IP6OPT_ROUTE;
  289                                 break;
  290                         case IPPROTO_ESP:
  291                                 opts &= ~IPV6_FW_IP6OPT_ESP;
  292                                 nopts &= ~IPV6_FW_IP6OPT_ESP;
  293                                 break;
  294                         case IPPROTO_NONE:
  295                                 opts &= ~IPV6_FW_IP6OPT_NONXT;
  296                                 nopts &= ~IPV6_FW_IP6OPT_NONXT;
  297                                 goto opts_check;
  298                                 break;
  299                         case IPPROTO_DSTOPTS:
  300                                 opts &= ~IPV6_FW_IP6OPT_OPTS;
  301                                 nopts &= ~IPV6_FW_IP6OPT_OPTS;
  302                                 break;
  303                         default:
  304                                 goto opts_check;
  305                                 break;
  306                         }
  307                         *off += (ip6e->ip6e_len + 1) << 3;
  308                         break;
  309                 }
  310                 *nxt = ip6e->ip6e_nxt;
  311 
  312         }
  313  opts_check:
  314         if (f->fw_ip6opt == f->fw_ip6nopt)      /* XXX */
  315                 return 1;
  316 
  317         if (opts == 0 && nopts == nopts_sve)
  318                 return 1;
  319         else
  320                 return 0;
  321 }
  322 
  323 static
  324 __inline int
  325 iface_match(struct ifnet *ifp, union ip6_fw_if *ifu, int byname)
  326 {
  327         /* Check by name or by IP address */
  328         if (byname) {
  329                 /* Check unit number (-1 is wildcard) */
  330                 if (ifu->fu_via_if.unit != -1
  331                     && ifp->if_unit != ifu->fu_via_if.unit)
  332                         return(0);
  333                 /* Check name */
  334                 if (strncmp(ifp->if_name, ifu->fu_via_if.name, IP6FW_IFNLEN))
  335                         return(0);
  336                 return(1);
  337         } else if (!IN6_IS_ADDR_UNSPECIFIED(&ifu->fu_via_ip6)) {        /* Zero == wildcard */
  338                 struct ifaddr *ia;
  339 
  340                 for (ia = ifp->if_addrlist.tqh_first; ia; ia = ia->ifa_list.tqe_next)
  341                 {
  342 
  343                         if (ia->ifa_addr == NULL)
  344                                 continue;
  345                         if (ia->ifa_addr->sa_family != AF_INET6)
  346                                 continue;
  347                         if (!IN6_ARE_ADDR_EQUAL(&ifu->fu_via_ip6,
  348                             &(((struct sockaddr_in6 *)
  349                             (ia->ifa_addr))->sin6_addr)))
  350                                 continue;
  351                         return(1);
  352                 }
  353                 return(0);
  354         }
  355         return(1);
  356 }
  357 
  358 static void
  359 ip6fw_report(struct ip6_fw *f, struct ip6_hdr *ip6,
  360         struct ifnet *rif, struct ifnet *oif, int off, int nxt)
  361 {
  362         static int counter;
  363         struct tcphdr *const tcp6 = (struct tcphdr *) ((caddr_t) ip6+ off);
  364         struct udphdr *const udp = (struct udphdr *) ((caddr_t) ip6+ off);
  365         struct icmp6_hdr *const icmp6 = (struct icmp6_hdr *) ((caddr_t) ip6+ off);
  366         int count;
  367         char *action;
  368         char action2[32], proto[102], name[18];
  369         int len;
  370 
  371         count = f ? f->fw_pcnt : ++counter;
  372         if (fw6_verbose_limit != 0 && count > fw6_verbose_limit)
  373                 return;
  374 
  375         /* Print command name */
  376         snprintf(SNPARGS(name, 0), "ip6fw: %d", f ? f->fw_number : -1);
  377 
  378         action = action2;
  379         if (!f)
  380                 action = "Refuse";
  381         else {
  382                 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
  383                 case IPV6_FW_F_DENY:
  384                         action = "Deny";
  385                         break;
  386                 case IPV6_FW_F_REJECT:
  387                         if (f->fw_reject_code == IPV6_FW_REJECT_RST)
  388                                 action = "Reset";
  389                         else
  390                                 action = "Unreach";
  391                         break;
  392                 case IPV6_FW_F_ACCEPT:
  393                         action = "Accept";
  394                         break;
  395                 case IPV6_FW_F_COUNT:
  396                         action = "Count";
  397                         break;
  398                 case IPV6_FW_F_DIVERT:
  399                         snprintf(SNPARGS(action2, 0), "Divert %d",
  400                             f->fw_divert_port);
  401                         break;
  402                 case IPV6_FW_F_TEE:
  403                         snprintf(SNPARGS(action2, 0), "Tee %d",
  404                             f->fw_divert_port);
  405                         break;
  406                 case IPV6_FW_F_SKIPTO:
  407                         snprintf(SNPARGS(action2, 0), "SkipTo %d",
  408                             f->fw_skipto_rule);
  409                         break;
  410                 default:        
  411                         action = "UNKNOWN";
  412                         break;
  413                 }
  414         }
  415 
  416         switch (nxt) {
  417         case IPPROTO_TCP:
  418                 len = snprintf(SNPARGS(proto, 0), "TCP [%s]",
  419                     ip6_sprintf(&ip6->ip6_src));
  420                 if (off > 0)
  421                         len += snprintf(SNPARGS(proto, len), ":%d ",
  422                             ntohs(tcp6->th_sport));
  423                 else
  424                         len += snprintf(SNPARGS(proto, len), " ");
  425                 len += snprintf(SNPARGS(proto, len), "[%s]",
  426                     ip6_sprintf(&ip6->ip6_dst));
  427                 if (off > 0)
  428                         snprintf(SNPARGS(proto, len), ":%d",
  429                             ntohs(tcp6->th_dport));
  430                 break;
  431         case IPPROTO_UDP:
  432                 len = snprintf(SNPARGS(proto, 0), "UDP [%s]",
  433                     ip6_sprintf(&ip6->ip6_src));
  434                 if (off > 0)
  435                         len += snprintf(SNPARGS(proto, len), ":%d ",
  436                             ntohs(udp->uh_sport));
  437                 else
  438                     len += snprintf(SNPARGS(proto, len), " ");
  439                 len += snprintf(SNPARGS(proto, len), "[%s]",
  440                     ip6_sprintf(&ip6->ip6_dst));
  441                 if (off > 0)
  442                         snprintf(SNPARGS(proto, len), ":%d",
  443                             ntohs(udp->uh_dport));
  444                 break;
  445         case IPPROTO_ICMPV6:
  446                 if (off > 0)
  447                         len = snprintf(SNPARGS(proto, 0), "IPV6-ICMP:%u.%u ",
  448                             icmp6->icmp6_type, icmp6->icmp6_code);
  449                 else
  450                         len = snprintf(SNPARGS(proto, 0), "IPV6-ICMP ");
  451                 len += snprintf(SNPARGS(proto, len), "[%s]",
  452                     ip6_sprintf(&ip6->ip6_src));
  453                 snprintf(SNPARGS(proto, len), " [%s]",
  454                     ip6_sprintf(&ip6->ip6_dst));
  455                 break;
  456         default:
  457                 len = snprintf(SNPARGS(proto, 0), "P:%d [%s]", nxt,
  458                     ip6_sprintf(&ip6->ip6_src));
  459                 snprintf(SNPARGS(proto, len), " [%s]",
  460                     ip6_sprintf(&ip6->ip6_dst));
  461                 break;
  462         }
  463 
  464         if (oif)
  465                 log(LOG_SECURITY | LOG_INFO, "%s %s %s out via %s\n",
  466                     name, action, proto, if_name(oif));
  467         else if (rif)
  468                 log(LOG_SECURITY | LOG_INFO, "%s %s %s in via %s\n",
  469                     name, action, proto, if_name(rif));
  470         else
  471                 log(LOG_SECURITY | LOG_INFO, "%s %s %s",
  472                     name, action, proto);
  473         if (fw6_verbose_limit != 0 && count == fw6_verbose_limit)
  474             log(LOG_SECURITY | LOG_INFO, "ip6fw: limit reached on entry %d\n",
  475                 f ? f->fw_number : -1);
  476 }
  477 
  478 /*
  479  * Parameters:
  480  *
  481  *      ip      Pointer to packet header (struct ip6_hdr *)
  482  *      hlen    Packet header length
  483  *      oif     Outgoing interface, or NULL if packet is incoming
  484  * #ifndef IP6FW_DIVERT_RESTART
  485  *      *cookie Ignore all divert/tee rules to this port (if non-zero)
  486  * #else
  487  *      *cookie Skip up to the first rule past this rule number;
  488  * #endif
  489  *      *m      The packet; we set to NULL when/if we nuke it.
  490  *
  491  * Return value:
  492  *
  493  *      0       The packet is to be accepted and routed normally OR
  494  *              the packet was denied/rejected and has been dropped;
  495  *              in the latter case, *m is equal to NULL upon return.
  496  *      port    Divert the packet to port.
  497  */
  498 
  499 static int
  500 ip6_fw_chk(struct ip6_hdr **pip6,
  501         struct ifnet *oif, u_int16_t *cookie, struct mbuf **m)
  502 {
  503         struct ip6_fw_chain *chain;
  504         struct ip6_fw *rule = NULL;
  505         struct ip6_hdr *ip6 = *pip6;
  506         struct ifnet *const rif = (*m)->m_pkthdr.rcvif;
  507         u_short offset = 0;
  508         int off = sizeof(struct ip6_hdr), nxt = ip6->ip6_nxt;
  509         u_short src_port, dst_port;
  510 #ifdef  IP6FW_DIVERT_RESTART
  511         u_int16_t skipto = *cookie;
  512 #else
  513         u_int16_t ignport = ntohs(*cookie);
  514 #endif
  515 
  516         *cookie = 0;
  517         /*
  518          * Go down the chain, looking for enlightment
  519          * #ifdef IP6FW_DIVERT_RESTART
  520          * If we've been asked to start at a given rule immediatly, do so.
  521          * #endif
  522          */
  523         chain = LIST_FIRST(&ip6_fw_chain);
  524 #ifdef IP6FW_DIVERT_RESTART
  525         if (skipto) {
  526                 if (skipto >= 65535)
  527                         goto dropit;
  528                 while (chain && (chain->rule->fw_number <= skipto)) {
  529                         chain = LIST_NEXT(chain, chain);
  530                 }
  531                 if (! chain) goto dropit;
  532         }
  533 #endif /* IP6FW_DIVERT_RESTART */
  534         for (; chain; chain = LIST_NEXT(chain, chain)) {
  535                 struct ip6_fw *const f = chain->rule;
  536 
  537                 if (oif) {
  538                         /* Check direction outbound */
  539                         if (!(f->fw_flg & IPV6_FW_F_OUT))
  540                                 continue;
  541                 } else {
  542                         /* Check direction inbound */
  543                         if (!(f->fw_flg & IPV6_FW_F_IN))
  544                                 continue;
  545                 }
  546 
  547 #define IN6_ARE_ADDR_MASKEQUAL(x,y,z) (\
  548         (((x)->s6_addr32[0] & (y)->s6_addr32[0]) == (z)->s6_addr32[0]) && \
  549         (((x)->s6_addr32[1] & (y)->s6_addr32[1]) == (z)->s6_addr32[1]) && \
  550         (((x)->s6_addr32[2] & (y)->s6_addr32[2]) == (z)->s6_addr32[2]) && \
  551         (((x)->s6_addr32[3] & (y)->s6_addr32[3]) == (z)->s6_addr32[3]))
  552 
  553                 /* If src-addr doesn't match, not this rule. */
  554                 if (((f->fw_flg & IPV6_FW_F_INVSRC) != 0) ^
  555                         (!IN6_ARE_ADDR_MASKEQUAL(&ip6->ip6_src,&f->fw_smsk,&f->fw_src)))
  556                         continue;
  557 
  558                 /* If dest-addr doesn't match, not this rule. */
  559                 if (((f->fw_flg & IPV6_FW_F_INVDST) != 0) ^
  560                         (!IN6_ARE_ADDR_MASKEQUAL(&ip6->ip6_dst,&f->fw_dmsk,&f->fw_dst)))
  561                         continue;
  562 
  563 #undef IN6_ARE_ADDR_MASKEQUAL
  564                 /* Interface check */
  565                 if ((f->fw_flg & IF6_FW_F_VIAHACK) == IF6_FW_F_VIAHACK) {
  566                         struct ifnet *const iface = oif ? oif : rif;
  567 
  568                         /* Backwards compatibility hack for "via" */
  569                         if (!iface || !iface_match(iface,
  570                             &f->fw_in_if, f->fw_flg & IPV6_FW_F_OIFNAME))
  571                                 continue;
  572                 } else {
  573                         /* Check receive interface */
  574                         if ((f->fw_flg & IPV6_FW_F_IIFACE)
  575                             && (!rif || !iface_match(rif,
  576                               &f->fw_in_if, f->fw_flg & IPV6_FW_F_IIFNAME)))
  577                                 continue;
  578                         /* Check outgoing interface */
  579                         if ((f->fw_flg & IPV6_FW_F_OIFACE)
  580                             && (!oif || !iface_match(oif,
  581                               &f->fw_out_if, f->fw_flg & IPV6_FW_F_OIFNAME)))
  582                                 continue;
  583                 }
  584 
  585                 /* Check IP options */
  586                 if (!ip6opts_match(&ip6, f, m, &off, &nxt, &offset))
  587                         continue;
  588 
  589                 /* Fragments */
  590                 if ((f->fw_flg & IPV6_FW_F_FRAG) && !offset)
  591                         continue;
  592 
  593                 /* Check protocol; if wildcard, match */
  594                 if (f->fw_prot == IPPROTO_IPV6)
  595                         goto got_match;
  596 
  597                 /* If different, don't match */
  598                 if (nxt != f->fw_prot)
  599                         continue;
  600 
  601 #define PULLUP_TO(len)  do {                                            \
  602                             if ((*m)->m_len < (len)                     \
  603                                 && (*m = m_pullup(*m, (len))) == 0) {   \
  604                                     goto dropit;                        \
  605                             }                                           \
  606                             *pip6 = ip6 = mtod(*m, struct ip6_hdr *);   \
  607                         } while (0)
  608 
  609                 /* Protocol specific checks */
  610                 switch (nxt) {
  611                 case IPPROTO_TCP:
  612                     {
  613                         struct tcphdr *tcp6;
  614 
  615                         if (offset == 1) {      /* cf. RFC 1858 */
  616                                 PULLUP_TO(off + 4); /* XXX ? */
  617                                 goto bogusfrag;
  618                         }
  619                         if (offset != 0) {
  620                                 /*
  621                                  * TCP flags and ports aren't available in this
  622                                  * packet -- if this rule specified either one,
  623                                  * we consider the rule a non-match.
  624                                  */
  625                                 if (f->fw_nports != 0 ||
  626                                     f->fw_tcpf != f->fw_tcpnf)
  627                                         continue;
  628 
  629                                 break;
  630                         }
  631                         PULLUP_TO(off + 14);
  632                         tcp6 = (struct tcphdr *) ((caddr_t)ip6 + off);
  633                         if (((f->fw_tcpf != f->fw_tcpnf) ||
  634                            (f->fw_ipflg & IPV6_FW_IF_TCPEST))  &&
  635                            !tcp6flg_match(tcp6, f))
  636                                 continue;
  637                         src_port = ntohs(tcp6->th_sport);
  638                         dst_port = ntohs(tcp6->th_dport);
  639                         goto check_ports;
  640                     }
  641 
  642                 case IPPROTO_UDP:
  643                     {
  644                         struct udphdr *udp;
  645 
  646                         if (offset != 0) {
  647                                 /*
  648                                  * Port specification is unavailable -- if this
  649                                  * rule specifies a port, we consider the rule
  650                                  * a non-match.
  651                                  */
  652                                 if (f->fw_nports != 0)
  653                                         continue;
  654 
  655                                 break;
  656                         }
  657                         PULLUP_TO(off + 4);
  658                         udp = (struct udphdr *) ((caddr_t)ip6 + off);
  659                         src_port = ntohs(udp->uh_sport);
  660                         dst_port = ntohs(udp->uh_dport);
  661 check_ports:
  662                         if (!port_match6(&f->fw_pts[0],
  663                             IPV6_FW_GETNSRCP(f), src_port,
  664                             f->fw_flg & IPV6_FW_F_SRNG))
  665                                 continue;
  666                         if (!port_match6(&f->fw_pts[IPV6_FW_GETNSRCP(f)],
  667                             IPV6_FW_GETNDSTP(f), dst_port,
  668                             f->fw_flg & IPV6_FW_F_DRNG))
  669                                 continue;
  670                         break;
  671                     }
  672 
  673                 case IPPROTO_ICMPV6:
  674                     {
  675                         struct icmp6_hdr *icmp;
  676 
  677                         if (offset != 0)        /* Type isn't valid */
  678                                 break;
  679                         PULLUP_TO(off + 2);
  680                         icmp = (struct icmp6_hdr *) ((caddr_t)ip6 + off);
  681                         if (!icmp6type_match(icmp, f))
  682                                 continue;
  683                         break;
  684                     }
  685 #undef PULLUP_TO
  686 
  687 bogusfrag:
  688                         if (fw6_verbose)
  689                                 ip6fw_report(NULL, ip6, rif, oif, off, nxt);
  690                         goto dropit;
  691                 }
  692 
  693 got_match:
  694 #ifndef IP6FW_DIVERT_RESTART
  695                 /* Ignore divert/tee rule if socket port is "ignport" */
  696                 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
  697                 case IPV6_FW_F_DIVERT:
  698                 case IPV6_FW_F_TEE:
  699                         if (f->fw_divert_port == ignport)
  700                                 continue;       /* ignore this rule */
  701                         break;
  702                 }
  703 
  704 #endif /* IP6FW_DIVERT_RESTART */
  705                 /* Update statistics */
  706                 f->fw_pcnt += 1;
  707                 f->fw_bcnt += ntohs(ip6->ip6_plen);
  708                 f->timestamp = time_second;
  709 
  710                 /* Log to console if desired */
  711                 if ((f->fw_flg & IPV6_FW_F_PRN) && fw6_verbose)
  712                         ip6fw_report(f, ip6, rif, oif, off, nxt);
  713 
  714                 /* Take appropriate action */
  715                 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
  716                 case IPV6_FW_F_ACCEPT:
  717                         return(0);
  718                 case IPV6_FW_F_COUNT:
  719                         continue;
  720                 case IPV6_FW_F_DIVERT:
  721 #ifdef IP6FW_DIVERT_RESTART
  722                         *cookie = f->fw_number;
  723 #else
  724                         *cookie = htons(f->fw_divert_port);
  725 #endif /* IP6FW_DIVERT_RESTART */
  726                         return(f->fw_divert_port);
  727                 case IPV6_FW_F_TEE:
  728                         /*
  729                          * XXX someday tee packet here, but beware that you
  730                          * can't use m_copym() or m_copypacket() because
  731                          * the divert input routine modifies the mbuf
  732                          * (and these routines only increment reference
  733                          * counts in the case of mbuf clusters), so need
  734                          * to write custom routine.
  735                          */
  736                         continue;
  737                 case IPV6_FW_F_SKIPTO:
  738 #ifdef DIAGNOSTIC
  739                         while (chain->chain.le_next
  740                             && chain->chain.le_next->rule->fw_number
  741                                 < f->fw_skipto_rule)
  742 #else
  743                         while (chain->chain.le_next->rule->fw_number
  744                             < f->fw_skipto_rule)
  745 #endif
  746                                 chain = chain->chain.le_next;
  747                         continue;
  748                 }
  749 
  750                 /* Deny/reject this packet using this rule */
  751                 rule = f;
  752                 break;
  753         }
  754 
  755 #ifdef DIAGNOSTIC
  756         /* Rule 65535 should always be there and should always match */
  757         if (!chain)
  758                 panic("ip6_fw: chain");
  759 #endif
  760 
  761         /*
  762          * At this point, we're going to drop the packet.
  763          * Send a reject notice if all of the following are true:
  764          *
  765          * - The packet matched a reject rule
  766          * - The packet is not an ICMP packet, or is an ICMP query packet
  767          * - The packet is not a multicast or broadcast packet
  768          */
  769         if ((rule->fw_flg & IPV6_FW_F_COMMAND) == IPV6_FW_F_REJECT
  770             && (nxt != IPPROTO_ICMPV6 || is_icmp6_query(ip6, off))
  771             && !((*m)->m_flags & (M_BCAST|M_MCAST))
  772             && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  773                 switch (rule->fw_reject_code) {
  774                 case IPV6_FW_REJECT_RST:
  775                   {
  776                         struct tcphdr *const tcp =
  777                                 (struct tcphdr *) ((caddr_t)ip6 + off);
  778                         struct {
  779                                 struct ip6_hdr ip6;
  780                                 struct tcphdr th;
  781                         } ti;
  782                         tcp_seq ack, seq;
  783                         int flags;
  784 
  785                         if (offset != 0 || (tcp->th_flags & TH_RST))
  786                                 break;
  787 
  788                         ti.ip6 = *ip6;
  789                         ti.th = *tcp;
  790                         ti.th.th_seq = ntohl(ti.th.th_seq);
  791                         ti.th.th_ack = ntohl(ti.th.th_ack);
  792                         ti.ip6.ip6_nxt = IPPROTO_TCP;
  793                         if (ti.th.th_flags & TH_ACK) {
  794                                 ack = 0;
  795                                 seq = ti.th.th_ack;
  796                                 flags = TH_RST;
  797                         } else {
  798                                 ack = ti.th.th_seq;
  799                                 if (((*m)->m_flags & M_PKTHDR) != 0) {
  800                                         ack += (*m)->m_pkthdr.len - off
  801                                                 - (ti.th.th_off << 2);
  802                                 } else if (ip6->ip6_plen) {
  803                                         ack += ntohs(ip6->ip6_plen) + sizeof(*ip6)
  804                                                 - off - (ti.th.th_off << 2);
  805                                 } else {
  806                                         m_freem(*m);
  807                                         *m = 0;
  808                                         break;
  809                                 }
  810                                 seq = 0;
  811                                 flags = TH_RST|TH_ACK;
  812                         }
  813                         bcopy(&ti, ip6, sizeof(ti));
  814                         tcp_respond(NULL, ip6, (struct tcphdr *)(ip6 + 1),
  815                                 *m, ack, seq, flags);
  816                         *m = NULL;
  817                         break;
  818                   }
  819                 default:        /* Send an ICMP unreachable using code */
  820                         if (oif)
  821                                 (*m)->m_pkthdr.rcvif = oif;
  822                         icmp6_error(*m, ICMP6_DST_UNREACH,
  823                             rule->fw_reject_code, 0);
  824                         *m = NULL;
  825                         break;
  826                 }
  827         }
  828 
  829 dropit:
  830         /*
  831          * Finally, drop the packet.
  832          */
  833         if (*m) {
  834                 m_freem(*m);
  835                 *m = NULL;
  836         }
  837         return(0);
  838 }
  839 
  840 static int
  841 add_entry6(struct ip6_fw_head *chainptr, struct ip6_fw *frwl)
  842 {
  843         struct ip6_fw *ftmp = 0;
  844         struct ip6_fw_chain *fwc = 0, *fcp, *fcpl = 0;
  845         u_short nbr = 0;
  846         int s;
  847 
  848         fwc = malloc(sizeof *fwc, M_IP6FW, M_DONTWAIT);
  849         ftmp = malloc(sizeof *ftmp, M_IP6FW, M_DONTWAIT);
  850         if (!fwc || !ftmp) {
  851                 dprintf(("%s malloc said no\n", err_prefix));
  852                 if (fwc)  free(fwc, M_IP6FW);
  853                 if (ftmp) free(ftmp, M_IP6FW);
  854                 return (ENOSPC);
  855         }
  856 
  857         bcopy(frwl, ftmp, sizeof(struct ip6_fw));
  858         ftmp->fw_in_if.fu_via_if.name[IP6FW_IFNLEN - 1] = '\0';
  859         ftmp->fw_pcnt = 0L;
  860         ftmp->fw_bcnt = 0L;
  861         fwc->rule = ftmp;
  862         
  863         s = splnet();
  864 
  865         if (!chainptr->lh_first) {
  866                 LIST_INSERT_HEAD(chainptr, fwc, chain);
  867                 splx(s);
  868                 return(0);
  869         } else if (ftmp->fw_number == (u_short)-1) {
  870                 if (fwc)  free(fwc, M_IP6FW);
  871                 if (ftmp) free(ftmp, M_IP6FW);
  872                 splx(s);
  873                 dprintf(("%s bad rule number\n", err_prefix));
  874                 return (EINVAL);
  875         }
  876 
  877         /* If entry number is 0, find highest numbered rule and add 100 */
  878         if (ftmp->fw_number == 0) {
  879                 for (fcp = chainptr->lh_first; fcp; fcp = fcp->chain.le_next) {
  880                         if (fcp->rule->fw_number != (u_short)-1)
  881                                 nbr = fcp->rule->fw_number;
  882                         else
  883                                 break;
  884                 }
  885                 if (nbr < (u_short)-1 - 100)
  886                         nbr += 100;
  887                 ftmp->fw_number = nbr;
  888         }
  889 
  890         /* Got a valid number; now insert it, keeping the list ordered */
  891         for (fcp = chainptr->lh_first; fcp; fcp = fcp->chain.le_next) {
  892                 if (fcp->rule->fw_number > ftmp->fw_number) {
  893                         if (fcpl) {
  894                                 LIST_INSERT_AFTER(fcpl, fwc, chain);
  895                         } else {
  896                                 LIST_INSERT_HEAD(chainptr, fwc, chain);
  897                         }
  898                         break;
  899                 } else {
  900                         fcpl = fcp;
  901                 }
  902         }
  903 
  904         splx(s);
  905         return (0);
  906 }
  907 
  908 static int
  909 del_entry6(struct ip6_fw_head *chainptr, u_short number)
  910 {
  911         struct ip6_fw_chain *fcp;
  912         int s;
  913 
  914         s = splnet();
  915 
  916         fcp = chainptr->lh_first;
  917         if (number != (u_short)-1) {
  918                 for (; fcp; fcp = fcp->chain.le_next) {
  919                         if (fcp->rule->fw_number == number) {
  920                                 LIST_REMOVE(fcp, chain);
  921                                 splx(s);
  922                                 free(fcp->rule, M_IP6FW);
  923                                 free(fcp, M_IP6FW);
  924                                 return 0;
  925                         }
  926                 }
  927         }
  928 
  929         splx(s);
  930         return (EINVAL);
  931 }
  932 
  933 static int
  934 zero_entry6(struct mbuf *m)
  935 {
  936         struct ip6_fw *frwl;
  937         struct ip6_fw_chain *fcp;
  938         int s;
  939 
  940         if (m && m->m_len != 0) {
  941                 if (m->m_len != sizeof(struct ip6_fw))
  942                         return(EINVAL);
  943                 frwl = mtod(m, struct ip6_fw *);
  944         }
  945         else
  946                 frwl = NULL;
  947 
  948         /*
  949          *      It's possible to insert multiple chain entries with the
  950          *      same number, so we don't stop after finding the first
  951          *      match if zeroing a specific entry.
  952          */
  953         s = splnet();
  954         for (fcp = ip6_fw_chain.lh_first; fcp; fcp = fcp->chain.le_next)
  955                 if (!frwl || frwl->fw_number == fcp->rule->fw_number) {
  956                         fcp->rule->fw_bcnt = fcp->rule->fw_pcnt = 0;
  957                         fcp->rule->timestamp = 0;
  958                 }
  959         splx(s);
  960 
  961         if (fw6_verbose) {
  962                 if (frwl)
  963                         log(LOG_SECURITY | LOG_NOTICE,
  964                             "ip6fw: Entry %d cleared.\n", frwl->fw_number);
  965                 else
  966                         log(LOG_SECURITY | LOG_NOTICE,
  967                             "ip6fw: Accounting cleared.\n");
  968         }
  969 
  970         return(0);
  971 }
  972 
  973 static struct ip6_fw *
  974 check_ip6fw_mbuf(struct mbuf *m)
  975 {
  976         /* Check length */
  977         if (m->m_len != sizeof(struct ip6_fw)) {
  978                 dprintf(("%s len=%d, want %d\n", err_prefix, m->m_len,
  979                     sizeof(struct ip6_fw)));
  980                 return (NULL);
  981         }
  982         return(check_ip6fw_struct(mtod(m, struct ip6_fw *)));
  983 }
  984 
  985 static struct ip6_fw *
  986 check_ip6fw_struct(struct ip6_fw *frwl)
  987 {
  988         /* Check for invalid flag bits */
  989         if ((frwl->fw_flg & ~IPV6_FW_F_MASK) != 0) {
  990                 dprintf(("%s undefined flag bits set (flags=%x)\n",
  991                     err_prefix, frwl->fw_flg));
  992                 return (NULL);
  993         }
  994         /* Must apply to incoming or outgoing (or both) */
  995         if (!(frwl->fw_flg & (IPV6_FW_F_IN | IPV6_FW_F_OUT))) {
  996                 dprintf(("%s neither in nor out\n", err_prefix));
  997                 return (NULL);
  998         }
  999         /* Empty interface name is no good */
 1000         if (((frwl->fw_flg & IPV6_FW_F_IIFNAME)
 1001               && !*frwl->fw_in_if.fu_via_if.name)
 1002             || ((frwl->fw_flg & IPV6_FW_F_OIFNAME)
 1003               && !*frwl->fw_out_if.fu_via_if.name)) {
 1004                 dprintf(("%s empty interface name\n", err_prefix));
 1005                 return (NULL);
 1006         }
 1007         /* Sanity check interface matching */
 1008         if ((frwl->fw_flg & IF6_FW_F_VIAHACK) == IF6_FW_F_VIAHACK) {
 1009                 ;               /* allow "via" backwards compatibility */
 1010         } else if ((frwl->fw_flg & IPV6_FW_F_IN)
 1011             && (frwl->fw_flg & IPV6_FW_F_OIFACE)) {
 1012                 dprintf(("%s outgoing interface check on incoming\n",
 1013                     err_prefix));
 1014                 return (NULL);
 1015         }
 1016         /* Sanity check port ranges */
 1017         if ((frwl->fw_flg & IPV6_FW_F_SRNG) && IPV6_FW_GETNSRCP(frwl) < 2) {
 1018                 dprintf(("%s src range set but n_src_p=%d\n",
 1019                     err_prefix, IPV6_FW_GETNSRCP(frwl)));
 1020                 return (NULL);
 1021         }
 1022         if ((frwl->fw_flg & IPV6_FW_F_DRNG) && IPV6_FW_GETNDSTP(frwl) < 2) {
 1023                 dprintf(("%s dst range set but n_dst_p=%d\n",
 1024                     err_prefix, IPV6_FW_GETNDSTP(frwl)));
 1025                 return (NULL);
 1026         }
 1027         if (IPV6_FW_GETNSRCP(frwl) + IPV6_FW_GETNDSTP(frwl) > IPV6_FW_MAX_PORTS) {
 1028                 dprintf(("%s too many ports (%d+%d)\n",
 1029                     err_prefix, IPV6_FW_GETNSRCP(frwl), IPV6_FW_GETNDSTP(frwl)));
 1030                 return (NULL);
 1031         }
 1032         /*
 1033          *      Protocols other than TCP/UDP don't use port range
 1034          */
 1035         if ((frwl->fw_prot != IPPROTO_TCP) &&
 1036             (frwl->fw_prot != IPPROTO_UDP) &&
 1037             (IPV6_FW_GETNSRCP(frwl) || IPV6_FW_GETNDSTP(frwl))) {
 1038                 dprintf(("%s port(s) specified for non TCP/UDP rule\n",
 1039                     err_prefix));
 1040                 return(NULL);
 1041         }
 1042 
 1043         /*
 1044          *      Rather than modify the entry to make such entries work,
 1045          *      we reject this rule and require user level utilities
 1046          *      to enforce whatever policy they deem appropriate.
 1047          */
 1048         if ((frwl->fw_src.s6_addr32[0] & (~frwl->fw_smsk.s6_addr32[0])) ||
 1049                 (frwl->fw_src.s6_addr32[1] & (~frwl->fw_smsk.s6_addr32[1])) ||
 1050                 (frwl->fw_src.s6_addr32[2] & (~frwl->fw_smsk.s6_addr32[2])) ||
 1051                 (frwl->fw_src.s6_addr32[3] & (~frwl->fw_smsk.s6_addr32[3])) ||
 1052                 (frwl->fw_dst.s6_addr32[0] & (~frwl->fw_dmsk.s6_addr32[0])) ||
 1053                 (frwl->fw_dst.s6_addr32[1] & (~frwl->fw_dmsk.s6_addr32[1])) ||
 1054                 (frwl->fw_dst.s6_addr32[2] & (~frwl->fw_dmsk.s6_addr32[2])) ||
 1055                 (frwl->fw_dst.s6_addr32[3] & (~frwl->fw_dmsk.s6_addr32[3]))) {
 1056                 dprintf(("%s rule never matches\n", err_prefix));
 1057                 return(NULL);
 1058         }
 1059 
 1060         if ((frwl->fw_flg & IPV6_FW_F_FRAG) &&
 1061                 (frwl->fw_prot == IPPROTO_UDP || frwl->fw_prot == IPPROTO_TCP)) {
 1062                 if (frwl->fw_nports) {
 1063                         dprintf(("%s cannot mix 'frag' and ports\n", err_prefix));
 1064                         return(NULL);
 1065                 }
 1066                 if (frwl->fw_prot == IPPROTO_TCP &&
 1067                         frwl->fw_tcpf != frwl->fw_tcpnf) {
 1068                         dprintf(("%s cannot mix 'frag' with TCP flags\n", err_prefix));
 1069                         return(NULL);
 1070                 }
 1071         }
 1072 
 1073         /* Check command specific stuff */
 1074         switch (frwl->fw_flg & IPV6_FW_F_COMMAND)
 1075         {
 1076         case IPV6_FW_F_REJECT:
 1077                 if (frwl->fw_reject_code >= 0x100
 1078                     && !(frwl->fw_prot == IPPROTO_TCP
 1079                       && frwl->fw_reject_code == IPV6_FW_REJECT_RST)) {
 1080                         dprintf(("%s unknown reject code\n", err_prefix));
 1081                         return(NULL);
 1082                 }
 1083                 break;
 1084         case IPV6_FW_F_DIVERT:          /* Diverting to port zero is invalid */
 1085         case IPV6_FW_F_TEE:
 1086                 if (frwl->fw_divert_port == 0) {
 1087                         dprintf(("%s can't divert to port 0\n", err_prefix));
 1088                         return (NULL);
 1089                 }
 1090                 break;
 1091         case IPV6_FW_F_DENY:
 1092         case IPV6_FW_F_ACCEPT:
 1093         case IPV6_FW_F_COUNT:
 1094         case IPV6_FW_F_SKIPTO:
 1095                 break;
 1096         default:
 1097                 dprintf(("%s invalid command\n", err_prefix));
 1098                 return(NULL);
 1099         }
 1100 
 1101         return frwl;
 1102 }
 1103 
 1104 static int
 1105 ip6_fw_ctl(int stage, struct mbuf **mm)
 1106 {
 1107         int error;
 1108         struct mbuf *m;
 1109 
 1110         if (stage == IPV6_FW_GET) {
 1111                 struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
 1112                 *mm = m = m_get(M_TRYWAIT, MT_DATA); /* XXX */
 1113                 if (!m)
 1114                         return(ENOBUFS);
 1115                 if (sizeof *(fcp->rule) > MLEN) {
 1116                         MCLGET(m, M_TRYWAIT);
 1117                         if ((m->m_flags & M_EXT) == 0) {
 1118                                 m_free(m);
 1119                                 return(ENOBUFS);
 1120                         }
 1121                 }
 1122                 for (; fcp; fcp = fcp->chain.le_next) {
 1123                         bcopy(fcp->rule, m->m_data, sizeof *(fcp->rule));
 1124                         m->m_len = sizeof *(fcp->rule);
 1125                         m->m_next = m_get(M_TRYWAIT, MT_DATA); /* XXX */
 1126                         if (!m->m_next) {
 1127                                 m_freem(*mm);
 1128                                 return(ENOBUFS);
 1129                         }
 1130                         m = m->m_next;
 1131                         if (sizeof *(fcp->rule) > MLEN) {
 1132                                 MCLGET(m, M_TRYWAIT);
 1133                                 if ((m->m_flags & M_EXT) == 0) {
 1134                                         m_freem(*mm);
 1135                                         return(ENOBUFS);
 1136                                 }
 1137                         }
 1138                         m->m_len = 0;
 1139                 }
 1140                 return (0);
 1141         }
 1142         m = *mm;
 1143         /* only allow get calls if secure mode > 2 */
 1144         if (securelevel > 2) {
 1145                 if (m) {
 1146                         (void)m_freem(m);
 1147                         *mm = 0;
 1148                 }
 1149                 return(EPERM);
 1150         }
 1151         if (stage == IPV6_FW_FLUSH) {
 1152                 while (ip6_fw_chain.lh_first != NULL &&
 1153                     ip6_fw_chain.lh_first->rule->fw_number != (u_short)-1) {
 1154                         struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
 1155                         int s = splnet();
 1156                         LIST_REMOVE(ip6_fw_chain.lh_first, chain);
 1157                         splx(s);
 1158                         free(fcp->rule, M_IP6FW);
 1159                         free(fcp, M_IP6FW);
 1160                 }
 1161                 if (m) {
 1162                         (void)m_freem(m);
 1163                         *mm = 0;
 1164                 }
 1165                 return (0);
 1166         }
 1167         if (stage == IPV6_FW_ZERO) {
 1168                 error = zero_entry6(m);
 1169                 if (m) {
 1170                         (void)m_freem(m);
 1171                         *mm = 0;
 1172                 }
 1173                 return (error);
 1174         }
 1175         if (m == NULL) {
 1176                 printf("%s NULL mbuf ptr\n", err_prefix);
 1177                 return (EINVAL);
 1178         }
 1179 
 1180         if (stage == IPV6_FW_ADD) {
 1181                 struct ip6_fw *frwl = check_ip6fw_mbuf(m);
 1182 
 1183                 if (!frwl)
 1184                         error = EINVAL;
 1185                 else
 1186                         error = add_entry6(&ip6_fw_chain, frwl);
 1187                 if (m) {
 1188                         (void)m_freem(m);
 1189                         *mm = 0;
 1190                 }
 1191                 return error;
 1192         }
 1193         if (stage == IPV6_FW_DEL) {
 1194                 if (m->m_len != sizeof(struct ip6_fw)) {
 1195                         dprintf(("%s len=%d, want %d\n", err_prefix, m->m_len,
 1196                             sizeof(struct ip6_fw)));
 1197                         error = EINVAL;
 1198                 } else if (mtod(m, struct ip6_fw *)->fw_number == (u_short)-1) {
 1199                         dprintf(("%s can't delete rule 65535\n", err_prefix));
 1200                         error = EINVAL;
 1201                 } else
 1202                         error = del_entry6(&ip6_fw_chain,
 1203                             mtod(m, struct ip6_fw *)->fw_number);
 1204                 if (m) {
 1205                         (void)m_freem(m);
 1206                         *mm = 0;
 1207                 }
 1208                 return error;
 1209         }
 1210 
 1211         dprintf(("%s unknown request %d\n", err_prefix, stage));
 1212         if (m) {
 1213                 (void)m_freem(m);
 1214                 *mm = 0;
 1215         }
 1216         return (EINVAL);
 1217 }
 1218 
 1219 void
 1220 ip6_fw_init(void)
 1221 {
 1222         struct ip6_fw default_rule;
 1223 
 1224         ip6_fw_chk_ptr = ip6_fw_chk;
 1225         ip6_fw_ctl_ptr = ip6_fw_ctl;
 1226         LIST_INIT(&ip6_fw_chain);
 1227 
 1228         bzero(&default_rule, sizeof default_rule);
 1229         default_rule.fw_prot = IPPROTO_IPV6;
 1230         default_rule.fw_number = (u_short)-1;
 1231 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
 1232         default_rule.fw_flg |= IPV6_FW_F_ACCEPT;
 1233 #else
 1234         default_rule.fw_flg |= IPV6_FW_F_DENY;
 1235 #endif
 1236         default_rule.fw_flg |= IPV6_FW_F_IN | IPV6_FW_F_OUT;
 1237         if (check_ip6fw_struct(&default_rule) == NULL ||
 1238                 add_entry6(&ip6_fw_chain, &default_rule))
 1239                 panic(__func__);
 1240 
 1241         printf("IPv6 packet filtering initialized, ");
 1242 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
 1243         printf("default to accept, ");
 1244 #endif
 1245 #ifndef IPV6FIREWALL_VERBOSE
 1246         printf("logging disabled\n");
 1247 #else
 1248         if (fw6_verbose_limit == 0)
 1249                 printf("unlimited logging\n");
 1250         else
 1251                 printf("logging limited to %d packets/entry\n",
 1252                     fw6_verbose_limit);
 1253 #endif
 1254 }
 1255 
 1256 static ip6_fw_chk_t *old_chk_ptr;
 1257 static ip6_fw_ctl_t *old_ctl_ptr;
 1258 
 1259 static int
 1260 ip6fw_modevent(module_t mod, int type, void *unused)
 1261 {
 1262         int s;
 1263 
 1264         switch (type) {
 1265         case MOD_LOAD:
 1266                 s = splnet();
 1267 
 1268                 old_chk_ptr = ip6_fw_chk_ptr;
 1269                 old_ctl_ptr = ip6_fw_ctl_ptr;
 1270 
 1271                 ip6_fw_init();
 1272                 splx(s);
 1273                 return 0;
 1274         case MOD_UNLOAD:
 1275                 s = splnet();
 1276                 ip6_fw_chk_ptr =  old_chk_ptr;
 1277                 ip6_fw_ctl_ptr =  old_ctl_ptr;
 1278                 while (LIST_FIRST(&ip6_fw_chain) != NULL) {
 1279                         struct ip6_fw_chain *fcp = LIST_FIRST(&ip6_fw_chain);
 1280                         LIST_REMOVE(LIST_FIRST(&ip6_fw_chain), chain);
 1281                         free(fcp->rule, M_IP6FW);
 1282                         free(fcp, M_IP6FW);
 1283                 }
 1284 
 1285                 splx(s);
 1286                 printf("IPv6 firewall unloaded\n");
 1287                 return 0;
 1288         default:
 1289                 break;
 1290         }
 1291         return 0;
 1292 }
 1293 
 1294 static moduledata_t ip6fwmod = {
 1295         "ip6fw",
 1296         ip6fw_modevent,
 1297         0
 1298 };
 1299 DECLARE_MODULE(ip6fw, ip6fwmod, SI_SUB_PSEUDO, SI_ORDER_ANY);

Cache object: 7e3002795e5f05caf1cca37c6e9c0a74


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.