ng_ipfw.c revision 141699
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: head/sys/netgraph/ng_ipfw.c 141699 2005-02-11 20:53:41Z glebius $ 27141351Sglebius */ 28141351Sglebius 29141351Sglebius#include <sys/param.h> 30141351Sglebius#include <sys/systm.h> 31141351Sglebius#include <sys/kernel.h> 32141351Sglebius#include <sys/mbuf.h> 33141351Sglebius#include <sys/malloc.h> 34141351Sglebius#include <sys/ctype.h> 35141351Sglebius#include <sys/errno.h> 36141351Sglebius#include <sys/socket.h> 37141351Sglebius#include <sys/syslog.h> 38141351Sglebius 39141351Sglebius#include <net/if.h> 40141351Sglebius 41141351Sglebius#include <netinet/in.h> 42141351Sglebius#include <netinet/in_systm.h> 43141351Sglebius#include <netinet/in_var.h> 44141351Sglebius#include <netinet/ip_fw.h> 45141351Sglebius#include <netinet/ip.h> 46141351Sglebius#include <netinet/ip_var.h> 47141351Sglebius 48141351Sglebius#include <netgraph/ng_message.h> 49141351Sglebius#include <netgraph/ng_parse.h> 50141351Sglebius#include <netgraph/ng_ipfw.h> 51141351Sglebius#include <netgraph/netgraph.h> 52141351Sglebius 53141351Sglebiusstatic int ng_ipfw_mod_event(module_t mod, int event, void *data); 54141351Sglebiusstatic ng_constructor_t ng_ipfw_constructor; 55141351Sglebiusstatic ng_shutdown_t ng_ipfw_shutdown; 56141351Sglebiusstatic ng_newhook_t ng_ipfw_newhook; 57141351Sglebiusstatic ng_connect_t ng_ipfw_connect; 58141351Sglebiusstatic ng_findhook_t ng_ipfw_findhook; 59141351Sglebiusstatic ng_rcvdata_t ng_ipfw_rcvdata; 60141351Sglebiusstatic ng_disconnect_t ng_ipfw_disconnect; 61141351Sglebius 62141351Sglebiusstatic hook_p ng_ipfw_findhook1(node_p, u_int16_t ); 63141351Sglebiusstatic int ng_ipfw_input(struct mbuf **, int, struct ip_fw_args *, 64141351Sglebius int); 65141351Sglebius 66141351Sglebius/* We have only one node */ 67141351Sglebiusstatic node_p fw_node; 68141351Sglebius 69141351Sglebius/* Netgraph node type descriptor */ 70141351Sglebiusstatic struct ng_type ng_ipfw_typestruct = { 71141351Sglebius .version = NG_ABI_VERSION, 72141351Sglebius .name = NG_IPFW_NODE_TYPE, 73141351Sglebius .mod_event = ng_ipfw_mod_event, 74141351Sglebius .constructor = ng_ipfw_constructor, 75141351Sglebius .shutdown = ng_ipfw_shutdown, 76141351Sglebius .newhook = ng_ipfw_newhook, 77141351Sglebius .connect = ng_ipfw_connect, 78141351Sglebius .findhook = ng_ipfw_findhook, 79141351Sglebius .rcvdata = ng_ipfw_rcvdata, 80141351Sglebius .disconnect = ng_ipfw_disconnect, 81141351Sglebius}; 82141351SglebiusNETGRAPH_INIT(ipfw, &ng_ipfw_typestruct); 83141351SglebiusMODULE_DEPEND(ng_ipfw, ipfw, 2, 2, 2); 84141351Sglebius 85141351Sglebius/* Information we store for each hook */ 86141351Sglebiusstruct ng_ipfw_hook_priv { 87141351Sglebius hook_p hook; 88141351Sglebius u_int16_t rulenum; 89141351Sglebius}; 90141351Sglebiustypedef struct ng_ipfw_hook_priv *hpriv_p; 91141351Sglebius 92141351Sglebiusstatic int 93141351Sglebiusng_ipfw_mod_event(module_t mod, int event, void *data) 94141351Sglebius{ 95141351Sglebius int error = 0; 96141351Sglebius 97141351Sglebius switch (event) { 98141351Sglebius case MOD_LOAD: 99141351Sglebius 100141351Sglebius if (ng_ipfw_input_p != NULL) { 101141351Sglebius error = EEXIST; 102141351Sglebius break; 103141351Sglebius } 104141351Sglebius 105141351Sglebius /* Setup node without any private data */ 106141351Sglebius if ((error = ng_make_node_common(&ng_ipfw_typestruct, &fw_node)) 107141351Sglebius != 0) { 108141351Sglebius log(LOG_ERR, "%s: can't create ng_ipfw node", __func__); 109141351Sglebius break; 110141351Sglebius }; 111141351Sglebius 112141351Sglebius /* Try to name node */ 113141351Sglebius if (ng_name_node(fw_node, "ipfw") != 0) 114141351Sglebius log(LOG_WARNING, "%s: failed to name node \"ipfw\"", 115141351Sglebius __func__); 116141351Sglebius 117141351Sglebius /* Register hook */ 118141351Sglebius ng_ipfw_input_p = ng_ipfw_input; 119141351Sglebius break; 120141351Sglebius 121141351Sglebius case MOD_UNLOAD: 122141351Sglebius /* 123141351Sglebius * This won't happen if a node exists. 124141351Sglebius * ng_ipfw_input_p is already cleared. 125141351Sglebius */ 126141351Sglebius break; 127141351Sglebius 128141351Sglebius default: 129141351Sglebius error = EOPNOTSUPP; 130141351Sglebius break; 131141351Sglebius } 132141351Sglebius 133141351Sglebius return (error); 134141351Sglebius} 135141351Sglebius 136141351Sglebiusstatic int 137141351Sglebiusng_ipfw_constructor(node_p node) 138141351Sglebius{ 139141351Sglebius return (EINVAL); /* Only one node */ 140141351Sglebius} 141141351Sglebius 142141351Sglebiusstatic int 143141351Sglebiusng_ipfw_newhook(node_p node, hook_p hook, const char *name) 144141351Sglebius{ 145141351Sglebius hpriv_p hpriv; 146141351Sglebius u_int16_t rulenum; 147141351Sglebius const char *cp; 148141451Sglebius char *endptr; 149141351Sglebius 150141351Sglebius /* Check that name contains only digits */ 151141451Sglebius for (cp = name; *cp != '\0'; cp++) 152141451Sglebius if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) 153141351Sglebius return (EINVAL); 154141351Sglebius 155141351Sglebius /* Convert it to integer */ 156141451Sglebius rulenum = (u_int16_t)strtol(name, &endptr, 10); 157141451Sglebius if (*endptr != '\0') 158141351Sglebius return (EINVAL); 159141351Sglebius 160141351Sglebius /* Allocate memory for this hook's private data */ 161141351Sglebius MALLOC(hpriv, hpriv_p, sizeof(*hpriv), M_NETGRAPH, M_NOWAIT | M_ZERO); 162141351Sglebius if (hpriv== NULL) 163141351Sglebius return (ENOMEM); 164141351Sglebius 165141351Sglebius hpriv->hook = hook; 166141351Sglebius hpriv->rulenum = rulenum; 167141351Sglebius 168141351Sglebius NG_HOOK_SET_PRIVATE(hook, hpriv); 169141351Sglebius 170141351Sglebius return(0); 171141351Sglebius} 172141351Sglebius 173141351Sglebius/* 174141351Sglebius * Set hooks into queueing mode, to avoid recursion between 175141351Sglebius * netgraph layer and ip_{input,output}. 176141351Sglebius */ 177141351Sglebiusstatic int 178141351Sglebiusng_ipfw_connect(hook_p hook) 179141351Sglebius{ 180141351Sglebius NG_HOOK_FORCE_QUEUE(hook); 181141351Sglebius return (0); 182141351Sglebius} 183141351Sglebius 184141351Sglebius/* Look up hook by name */ 185141351Sglebiushook_p 186141351Sglebiusng_ipfw_findhook(node_p node, const char *name) 187141351Sglebius{ 188141351Sglebius u_int16_t n; /* numeric representation of hook */ 189141451Sglebius char *endptr; 190141351Sglebius 191141451Sglebius n = (u_int16_t)strtol(name, &endptr, 10); 192141451Sglebius if (*endptr != '\0') 193141351Sglebius return NULL; 194141351Sglebius return ng_ipfw_findhook1(node, n); 195141351Sglebius} 196141351Sglebius 197141351Sglebius/* Look up hook by rule number */ 198141351Sglebiusstatic hook_p 199141351Sglebiusng_ipfw_findhook1(node_p node, u_int16_t rulenum) 200141351Sglebius{ 201141351Sglebius hook_p hook; 202141351Sglebius hpriv_p hpriv; 203141351Sglebius 204141351Sglebius LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { 205141351Sglebius hpriv = NG_HOOK_PRIVATE(hook); 206141351Sglebius if (NG_HOOK_IS_VALID(hook) && (hpriv->rulenum == rulenum)) 207141351Sglebius return (hook); 208141351Sglebius } 209141351Sglebius 210141351Sglebius return (NULL); 211141351Sglebius} 212141351Sglebius 213141351Sglebius 214141351Sglebiusstatic int 215141351Sglebiusng_ipfw_rcvdata(hook_p hook, item_p item) 216141351Sglebius{ 217141351Sglebius struct ng_ipfw_tag *ngit; 218141351Sglebius struct mbuf *m; 219141351Sglebius 220141351Sglebius NGI_GET_M(item, m); 221141351Sglebius NG_FREE_ITEM(item); 222141351Sglebius 223141351Sglebius if ((ngit = (struct ng_ipfw_tag *)m_tag_locate(m, NGM_IPFW_COOKIE, 0, 224141351Sglebius NULL)) == NULL) { 225141351Sglebius NG_FREE_M(m); 226141351Sglebius return (EINVAL); /* XXX: find smth better */ 227141351Sglebius }; 228141351Sglebius 229141351Sglebius switch (ngit->dir) { 230141351Sglebius case NG_IPFW_OUT: 231141699Sglebius { 232141699Sglebius struct ip *ip = mtod(m, struct ip *); 233141699Sglebius 234141699Sglebius ip->ip_len = ntohs(ip->ip_len); 235141699Sglebius ip->ip_off = ntohs(ip->ip_off); 236141699Sglebius 237141351Sglebius return ip_output(m, NULL, NULL, ngit->flags, NULL, NULL); 238141699Sglebius } 239141351Sglebius case NG_IPFW_IN: 240141351Sglebius ip_input(m); 241141351Sglebius return (0); 242141351Sglebius default: 243141351Sglebius panic("ng_ipfw_rcvdata: bad dir %u", ngit->dir); 244141351Sglebius } 245141351Sglebius 246141351Sglebius /* not reached */ 247141351Sglebius return (0); 248141351Sglebius} 249141351Sglebius 250141351Sglebiusstatic int 251141351Sglebiusng_ipfw_input(struct mbuf **m0, int dir, struct ip_fw_args *fwa, int tee) 252141351Sglebius{ 253141351Sglebius struct mbuf *m; 254141351Sglebius struct ng_ipfw_tag *ngit; 255141699Sglebius struct ip *ip; 256141351Sglebius hook_p hook; 257141351Sglebius int error = 0; 258141351Sglebius 259141351Sglebius /* 260141351Sglebius * Node must be loaded and corresponding hook must be present. 261141351Sglebius */ 262141351Sglebius if (fw_node == NULL || 263141351Sglebius (hook = ng_ipfw_findhook1(fw_node, fwa->cookie)) == NULL) { 264141351Sglebius if (tee == 0) 265141351Sglebius m_freem(*m0); 266141351Sglebius return (ESRCH); /* no hook associated with this rule */ 267141351Sglebius } 268141351Sglebius 269141351Sglebius /* 270141351Sglebius * We have two modes: in normal mode we add a tag to packet, which is 271141351Sglebius * important to return packet back to IP stack. In tee mode we make 272141351Sglebius * a copy of a packet and forward it into netgraph without a tag. 273141351Sglebius */ 274141351Sglebius if (tee == 0) { 275141351Sglebius m = *m0; 276141351Sglebius *m0 = NULL; /* it belongs now to netgraph */ 277141351Sglebius 278141351Sglebius if ((ngit = (struct ng_ipfw_tag *)m_tag_alloc(NGM_IPFW_COOKIE, 279141351Sglebius 0, TAGSIZ, M_NOWAIT|M_ZERO)) == NULL) { 280141351Sglebius m_freem(m); 281141351Sglebius return (ENOMEM); 282141351Sglebius } 283141351Sglebius ngit->rule = fwa->rule; 284141351Sglebius ngit->dir = dir; 285141351Sglebius ngit->ifp = fwa->oif; 286141351Sglebius if (dir == NG_IPFW_OUT) 287141351Sglebius ngit->flags = fwa->flags; 288141351Sglebius m_tag_prepend(m, &ngit->mt); 289141351Sglebius 290141351Sglebius } else 291141351Sglebius if ((m = m_dup(*m0, M_DONTWAIT)) == NULL) 292141351Sglebius return (ENOMEM); /* which is ignored */ 293141351Sglebius 294141699Sglebius ip = mtod(m, struct ip *); 295141699Sglebius ip->ip_len = htons(ip->ip_len); 296141699Sglebius ip->ip_off = htons(ip->ip_off); 297141699Sglebius 298141351Sglebius NG_SEND_DATA_ONLY(error, hook, m); 299141351Sglebius 300141351Sglebius return (error); 301141351Sglebius} 302141351Sglebius 303141351Sglebiusstatic int 304141351Sglebiusng_ipfw_shutdown(node_p node) 305141351Sglebius{ 306141351Sglebius 307141351Sglebius /* 308141351Sglebius * After our single node has been removed, 309141351Sglebius * the only thing that can be done is 310141351Sglebius * 'kldunload ng_ipfw.ko' 311141351Sglebius */ 312141351Sglebius ng_ipfw_input_p = NULL; 313141351Sglebius NG_NODE_UNREF(node); 314141351Sglebius return (0); 315141351Sglebius} 316141351Sglebius 317141351Sglebiusstatic int 318141351Sglebiusng_ipfw_disconnect(hook_p hook) 319141351Sglebius{ 320141351Sglebius const hpriv_p hpriv = NG_HOOK_PRIVATE(hook); 321141351Sglebius 322141351Sglebius FREE(hpriv, M_NETGRAPH); 323141351Sglebius NG_HOOK_SET_PRIVATE(hook, NULL); 324141351Sglebius 325141351Sglebius return (0); 326141351Sglebius} 327