1141351Sglebius/*- 2141351Sglebius * Copyright 2005, Gleb Smirnoff <glebius@FreeBSD.org> 3141351Sglebius * All rights reserved. 4141351Sglebius * 5141351Sglebius * Redistribution and use in source and binary forms, with or without 6141351Sglebius * modification, are permitted provided that the following conditions 7141351Sglebius * are met: 8141351Sglebius * 1. Redistributions of source code must retain the above copyright 9141351Sglebius * notice, this list of conditions and the following disclaimer. 10141351Sglebius * 2. Redistributions in binary form must reproduce the above copyright 11141351Sglebius * notice, this list of conditions and the following disclaimer in the 12141351Sglebius * documentation and/or other materials provided with the distribution. 13141351Sglebius * 14141351Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15141351Sglebius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16141351Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17141351Sglebius * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18141351Sglebius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19141351Sglebius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20141351Sglebius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21141351Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22141351Sglebius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23141351Sglebius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24141351Sglebius * SUCH DAMAGE. 25141351Sglebius * 26141351Sglebius * $FreeBSD$ 27141351Sglebius */ 28141351Sglebius 29225586Sae#include "opt_inet.h" 30225586Sae#include "opt_inet6.h" 31225586Sae 32141351Sglebius#include <sys/param.h> 33141351Sglebius#include <sys/systm.h> 34141351Sglebius#include <sys/kernel.h> 35185895Szec#include <sys/lock.h> 36141351Sglebius#include <sys/mbuf.h> 37141351Sglebius#include <sys/malloc.h> 38141351Sglebius#include <sys/ctype.h> 39141351Sglebius#include <sys/errno.h> 40185895Szec#include <sys/rwlock.h> 41141351Sglebius#include <sys/socket.h> 42141351Sglebius#include <sys/syslog.h> 43141351Sglebius 44141351Sglebius#include <net/if.h> 45141351Sglebius 46141351Sglebius#include <netinet/in.h> 47141351Sglebius#include <netinet/in_systm.h> 48141351Sglebius#include <netinet/in_var.h> 49201748Sluigi#include <netinet/ip_var.h> 50141351Sglebius#include <netinet/ip_fw.h> 51141351Sglebius#include <netinet/ip.h> 52225586Sae#include <netinet/ip6.h> 53225586Sae#include <netinet6/ip6_var.h> 54141351Sglebius 55240494Sglebius#include <netpfil/ipfw/ip_fw_private.h> 56240494Sglebius 57141351Sglebius#include <netgraph/ng_message.h> 58141351Sglebius#include <netgraph/ng_parse.h> 59141351Sglebius#include <netgraph/ng_ipfw.h> 60141351Sglebius#include <netgraph/netgraph.h> 61141351Sglebius 62141351Sglebiusstatic int ng_ipfw_mod_event(module_t mod, int event, void *data); 63141351Sglebiusstatic ng_constructor_t ng_ipfw_constructor; 64141351Sglebiusstatic ng_shutdown_t ng_ipfw_shutdown; 65141351Sglebiusstatic ng_newhook_t ng_ipfw_newhook; 66141351Sglebiusstatic ng_connect_t ng_ipfw_connect; 67141351Sglebiusstatic ng_findhook_t ng_ipfw_findhook; 68141351Sglebiusstatic ng_rcvdata_t ng_ipfw_rcvdata; 69141351Sglebiusstatic ng_disconnect_t ng_ipfw_disconnect; 70141351Sglebius 71141351Sglebiusstatic hook_p ng_ipfw_findhook1(node_p, u_int16_t ); 72141351Sglebiusstatic int ng_ipfw_input(struct mbuf **, int, struct ip_fw_args *, 73141351Sglebius int); 74141351Sglebius 75141351Sglebius/* We have only one node */ 76141351Sglebiusstatic node_p fw_node; 77141351Sglebius 78141351Sglebius/* Netgraph node type descriptor */ 79141351Sglebiusstatic struct ng_type ng_ipfw_typestruct = { 80141351Sglebius .version = NG_ABI_VERSION, 81141351Sglebius .name = NG_IPFW_NODE_TYPE, 82141351Sglebius .mod_event = ng_ipfw_mod_event, 83141351Sglebius .constructor = ng_ipfw_constructor, 84141351Sglebius .shutdown = ng_ipfw_shutdown, 85141351Sglebius .newhook = ng_ipfw_newhook, 86141351Sglebius .connect = ng_ipfw_connect, 87141351Sglebius .findhook = ng_ipfw_findhook, 88141351Sglebius .rcvdata = ng_ipfw_rcvdata, 89141351Sglebius .disconnect = ng_ipfw_disconnect, 90141351Sglebius}; 91141351SglebiusNETGRAPH_INIT(ipfw, &ng_ipfw_typestruct); 92141351SglebiusMODULE_DEPEND(ng_ipfw, ipfw, 2, 2, 2); 93141351Sglebius 94141351Sglebius/* Information we store for each hook */ 95141351Sglebiusstruct ng_ipfw_hook_priv { 96141351Sglebius hook_p hook; 97141351Sglebius u_int16_t rulenum; 98141351Sglebius}; 99141351Sglebiustypedef struct ng_ipfw_hook_priv *hpriv_p; 100141351Sglebius 101141351Sglebiusstatic int 102141351Sglebiusng_ipfw_mod_event(module_t mod, int event, void *data) 103141351Sglebius{ 104141351Sglebius int error = 0; 105141351Sglebius 106141351Sglebius switch (event) { 107141351Sglebius case MOD_LOAD: 108141351Sglebius 109141351Sglebius if (ng_ipfw_input_p != NULL) { 110141351Sglebius error = EEXIST; 111141351Sglebius break; 112141351Sglebius } 113141351Sglebius 114141351Sglebius /* Setup node without any private data */ 115141351Sglebius if ((error = ng_make_node_common(&ng_ipfw_typestruct, &fw_node)) 116141351Sglebius != 0) { 117141351Sglebius log(LOG_ERR, "%s: can't create ng_ipfw node", __func__); 118141351Sglebius break; 119141351Sglebius }; 120141351Sglebius 121141351Sglebius /* Try to name node */ 122141351Sglebius if (ng_name_node(fw_node, "ipfw") != 0) 123141351Sglebius log(LOG_WARNING, "%s: failed to name node \"ipfw\"", 124141351Sglebius __func__); 125141351Sglebius 126141351Sglebius /* Register hook */ 127141351Sglebius ng_ipfw_input_p = ng_ipfw_input; 128141351Sglebius break; 129141351Sglebius 130141351Sglebius case MOD_UNLOAD: 131141351Sglebius /* 132141351Sglebius * This won't happen if a node exists. 133141351Sglebius * ng_ipfw_input_p is already cleared. 134141351Sglebius */ 135141351Sglebius break; 136141351Sglebius 137141351Sglebius default: 138141351Sglebius error = EOPNOTSUPP; 139141351Sglebius break; 140141351Sglebius } 141141351Sglebius 142141351Sglebius return (error); 143141351Sglebius} 144141351Sglebius 145141351Sglebiusstatic int 146141351Sglebiusng_ipfw_constructor(node_p node) 147141351Sglebius{ 148141351Sglebius return (EINVAL); /* Only one node */ 149141351Sglebius} 150141351Sglebius 151141351Sglebiusstatic int 152141351Sglebiusng_ipfw_newhook(node_p node, hook_p hook, const char *name) 153141351Sglebius{ 154141351Sglebius hpriv_p hpriv; 155141351Sglebius u_int16_t rulenum; 156141351Sglebius const char *cp; 157141451Sglebius char *endptr; 158141351Sglebius 159146745Sglebius /* Protect from leading zero */ 160146745Sglebius if (name[0] == '0' && name[1] != '\0') 161146745Sglebius return (EINVAL); 162146745Sglebius 163141351Sglebius /* Check that name contains only digits */ 164141451Sglebius for (cp = name; *cp != '\0'; cp++) 165146745Sglebius if (!isdigit(*cp)) 166141351Sglebius return (EINVAL); 167141351Sglebius 168141351Sglebius /* Convert it to integer */ 169141451Sglebius rulenum = (u_int16_t)strtol(name, &endptr, 10); 170141451Sglebius if (*endptr != '\0') 171141351Sglebius return (EINVAL); 172141351Sglebius 173141351Sglebius /* Allocate memory for this hook's private data */ 174184205Sdes hpriv = malloc(sizeof(*hpriv), M_NETGRAPH, M_NOWAIT | M_ZERO); 175141351Sglebius if (hpriv== NULL) 176141351Sglebius return (ENOMEM); 177141351Sglebius 178141351Sglebius hpriv->hook = hook; 179141351Sglebius hpriv->rulenum = rulenum; 180141351Sglebius 181141351Sglebius NG_HOOK_SET_PRIVATE(hook, hpriv); 182141351Sglebius 183141351Sglebius return(0); 184141351Sglebius} 185141351Sglebius 186141351Sglebius/* 187141351Sglebius * Set hooks into queueing mode, to avoid recursion between 188141351Sglebius * netgraph layer and ip_{input,output}. 189141351Sglebius */ 190141351Sglebiusstatic int 191141351Sglebiusng_ipfw_connect(hook_p hook) 192141351Sglebius{ 193141351Sglebius NG_HOOK_FORCE_QUEUE(hook); 194141351Sglebius return (0); 195141351Sglebius} 196141351Sglebius 197141351Sglebius/* Look up hook by name */ 198230214Sglebiusstatic hook_p 199141351Sglebiusng_ipfw_findhook(node_p node, const char *name) 200141351Sglebius{ 201141351Sglebius u_int16_t n; /* numeric representation of hook */ 202141451Sglebius char *endptr; 203141351Sglebius 204141451Sglebius n = (u_int16_t)strtol(name, &endptr, 10); 205141451Sglebius if (*endptr != '\0') 206141351Sglebius return NULL; 207141351Sglebius return ng_ipfw_findhook1(node, n); 208141351Sglebius} 209141351Sglebius 210141351Sglebius/* Look up hook by rule number */ 211141351Sglebiusstatic hook_p 212141351Sglebiusng_ipfw_findhook1(node_p node, u_int16_t rulenum) 213141351Sglebius{ 214141351Sglebius hook_p hook; 215141351Sglebius hpriv_p hpriv; 216141351Sglebius 217141351Sglebius LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { 218141351Sglebius hpriv = NG_HOOK_PRIVATE(hook); 219141351Sglebius if (NG_HOOK_IS_VALID(hook) && (hpriv->rulenum == rulenum)) 220141351Sglebius return (hook); 221141351Sglebius } 222141351Sglebius 223141351Sglebius return (NULL); 224141351Sglebius} 225141351Sglebius 226141351Sglebius 227141351Sglebiusstatic int 228141351Sglebiusng_ipfw_rcvdata(hook_p hook, item_p item) 229141351Sglebius{ 230209633Sglebius struct m_tag *tag; 231209633Sglebius struct ipfw_rule_ref *r; 232141351Sglebius struct mbuf *m; 233225586Sae struct ip *ip; 234141351Sglebius 235141351Sglebius NGI_GET_M(item, m); 236141351Sglebius NG_FREE_ITEM(item); 237141351Sglebius 238209633Sglebius tag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL); 239201527Sluigi if (tag == NULL) { 240141351Sglebius NG_FREE_M(m); 241141351Sglebius return (EINVAL); /* XXX: find smth better */ 242141351Sglebius }; 243141351Sglebius 244225586Sae if (m->m_len < sizeof(struct ip) && 245225586Sae (m = m_pullup(m, sizeof(struct ip))) == NULL) 246226186Smelifaro return (ENOBUFS); 247225586Sae 248225586Sae ip = mtod(m, struct ip *); 249225586Sae 250209633Sglebius r = (struct ipfw_rule_ref *)(tag + 1); 251209633Sglebius if (r->info & IPFW_INFO_IN) { 252225586Sae switch (ip->ip_v) { 253225586Sae#ifdef INET 254225586Sae case IPVERSION: 255225586Sae ip_input(m); 256226186Smelifaro return (0); 257225586Sae#endif 258225586Sae#ifdef INET6 259225586Sae case IPV6_VERSION >> 4: 260225586Sae ip6_input(m); 261226186Smelifaro return (0); 262225586Sae#endif 263225586Sae } 264201527Sluigi } else { 265225586Sae switch (ip->ip_v) { 266225586Sae#ifdef INET 267225586Sae case IPVERSION: 268225586Sae return (ip_output(m, NULL, NULL, IP_FORWARDING, 269225586Sae NULL, NULL)); 270225586Sae#endif 271225586Sae#ifdef INET6 272225586Sae case IPV6_VERSION >> 4: 273225586Sae return (ip6_output(m, NULL, NULL, 0, NULL, 274225586Sae NULL, NULL)); 275225586Sae#endif 276225586Sae } 277225586Sae } 278226186Smelifaro 279226186Smelifaro /* unknown IP protocol version */ 280226186Smelifaro NG_FREE_M(m); 281226186Smelifaro return (EPROTONOSUPPORT); 282141351Sglebius} 283141351Sglebius 284141351Sglebiusstatic int 285141351Sglebiusng_ipfw_input(struct mbuf **m0, int dir, struct ip_fw_args *fwa, int tee) 286141351Sglebius{ 287141351Sglebius struct mbuf *m; 288141699Sglebius struct ip *ip; 289141351Sglebius hook_p hook; 290141351Sglebius int error = 0; 291141351Sglebius 292141351Sglebius /* 293141351Sglebius * Node must be loaded and corresponding hook must be present. 294141351Sglebius */ 295141351Sglebius if (fw_node == NULL || 296209722Sglebius (hook = ng_ipfw_findhook1(fw_node, fwa->rule.info)) == NULL) 297141351Sglebius return (ESRCH); /* no hook associated with this rule */ 298141351Sglebius 299141351Sglebius /* 300141351Sglebius * We have two modes: in normal mode we add a tag to packet, which is 301141351Sglebius * important to return packet back to IP stack. In tee mode we make 302141351Sglebius * a copy of a packet and forward it into netgraph without a tag. 303141351Sglebius */ 304141351Sglebius if (tee == 0) { 305201527Sluigi struct m_tag *tag; 306201527Sluigi struct ipfw_rule_ref *r; 307141351Sglebius m = *m0; 308141351Sglebius *m0 = NULL; /* it belongs now to netgraph */ 309141351Sglebius 310201527Sluigi tag = m_tag_alloc(MTAG_IPFW_RULE, 0, sizeof(*r), 311201527Sluigi M_NOWAIT|M_ZERO); 312201527Sluigi if (tag == NULL) { 313141351Sglebius m_freem(m); 314141351Sglebius return (ENOMEM); 315141351Sglebius } 316201527Sluigi r = (struct ipfw_rule_ref *)(tag + 1); 317201527Sluigi *r = fwa->rule; 318210537Sglebius r->info &= IPFW_ONEPASS; /* keep this info */ 319210537Sglebius r->info |= dir ? IPFW_INFO_IN : IPFW_INFO_OUT; 320201527Sluigi m_tag_prepend(m, tag); 321141351Sglebius 322141351Sglebius } else 323243882Sglebius if ((m = m_dup(*m0, M_NOWAIT)) == NULL) 324141351Sglebius return (ENOMEM); /* which is ignored */ 325141351Sglebius 326141705Sglebius if (m->m_len < sizeof(struct ip) && 327141706Sglebius (m = m_pullup(m, sizeof(struct ip))) == NULL) 328141706Sglebius return (EINVAL); 329141705Sglebius 330141699Sglebius ip = mtod(m, struct ip *); 331141699Sglebius 332141351Sglebius NG_SEND_DATA_ONLY(error, hook, m); 333141351Sglebius 334141351Sglebius return (error); 335141351Sglebius} 336141351Sglebius 337141351Sglebiusstatic int 338141351Sglebiusng_ipfw_shutdown(node_p node) 339141351Sglebius{ 340141351Sglebius 341141351Sglebius /* 342141351Sglebius * After our single node has been removed, 343141351Sglebius * the only thing that can be done is 344141351Sglebius * 'kldunload ng_ipfw.ko' 345141351Sglebius */ 346141351Sglebius ng_ipfw_input_p = NULL; 347141351Sglebius NG_NODE_UNREF(node); 348141351Sglebius return (0); 349141351Sglebius} 350141351Sglebius 351141351Sglebiusstatic int 352141351Sglebiusng_ipfw_disconnect(hook_p hook) 353141351Sglebius{ 354141351Sglebius const hpriv_p hpriv = NG_HOOK_PRIVATE(hook); 355141351Sglebius 356184205Sdes free(hpriv, M_NETGRAPH); 357141351Sglebius NG_HOOK_SET_PRIVATE(hook, NULL); 358141351Sglebius 359141351Sglebius return (0); 360141351Sglebius} 361