ng_split.c revision 256281
1/*- 2 * 3 * Copyright (c) 1999-2000, Vitaly V Belekhov 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice unmodified, this list of conditions, and the following 11 * disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD: stable/10/sys/netgraph/ng_split.c 220768 2011-04-18 09:12:27Z glebius $ 29 * 30 */ 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/errno.h> 35#include <sys/kernel.h> 36#include <sys/malloc.h> 37#include <sys/mbuf.h> 38#include <sys/errno.h> 39#include <sys/sockio.h> 40#include <sys/socket.h> 41#include <sys/syslog.h> 42 43#include <netgraph/ng_message.h> 44#include <netgraph/netgraph.h> 45#include <netgraph/ng_split.h> 46 47/* Netgraph methods */ 48static ng_constructor_t ng_split_constructor; 49static ng_shutdown_t ng_split_shutdown; 50static ng_newhook_t ng_split_newhook; 51static ng_rcvdata_t ng_split_rcvdata; 52static ng_disconnect_t ng_split_disconnect; 53 54/* Node type descriptor */ 55static struct ng_type typestruct = { 56 .version = NG_ABI_VERSION, 57 .name = NG_SPLIT_NODE_TYPE, 58 .constructor = ng_split_constructor, 59 .shutdown = ng_split_shutdown, 60 .newhook = ng_split_newhook, 61 .rcvdata = ng_split_rcvdata, 62 .disconnect = ng_split_disconnect, 63}; 64NETGRAPH_INIT(ng_split, &typestruct); 65 66/* Node private data */ 67struct ng_split_private { 68 hook_p out; 69 hook_p in; 70 hook_p mixed; 71 node_p node; /* Our netgraph node */ 72}; 73typedef struct ng_split_private *priv_p; 74 75/************************************************************************ 76 NETGRAPH NODE STUFF 77 ************************************************************************/ 78 79/* 80 * Constructor for a node 81 */ 82static int 83ng_split_constructor(node_p node) 84{ 85 priv_p priv; 86 87 /* Allocate node */ 88 priv = malloc(sizeof(*priv), M_NETGRAPH, M_ZERO | M_WAITOK); 89 90 /* Link together node and private info */ 91 NG_NODE_SET_PRIVATE(node, priv); 92 priv->node = node; 93 94 /* Done */ 95 return (0); 96} 97 98/* 99 * Give our ok for a hook to be added 100 */ 101static int 102ng_split_newhook(node_p node, hook_p hook, const char *name) 103{ 104 priv_p priv = NG_NODE_PRIVATE(node); 105 hook_p *localhook; 106 107 if (strcmp(name, NG_SPLIT_HOOK_MIXED) == 0) { 108 localhook = &priv->mixed; 109 } else if (strcmp(name, NG_SPLIT_HOOK_IN) == 0) { 110 localhook = &priv->in; 111 } else if (strcmp(name, NG_SPLIT_HOOK_OUT) == 0) { 112 localhook = &priv->out; 113 } else 114 return (EINVAL); 115 116 if (*localhook != NULL) 117 return (EISCONN); 118 *localhook = hook; 119 NG_HOOK_SET_PRIVATE(hook, localhook); 120 121 return (0); 122} 123 124/* 125 * Recive data from a hook. 126 */ 127static int 128ng_split_rcvdata(hook_p hook, item_p item) 129{ 130 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 131 int error = 0; 132 133 if (hook == priv->out) { 134 printf("ng_split: got packet from out hook!\n"); 135 NG_FREE_ITEM(item); 136 error = EINVAL; 137 } else if ((hook == priv->in) && (priv->mixed != NULL)) { 138 NG_FWD_ITEM_HOOK(error, item, priv->mixed); 139 } else if ((hook == priv->mixed) && (priv->out != NULL)) { 140 NG_FWD_ITEM_HOOK(error, item, priv->out); 141 } 142 143 if (item) 144 NG_FREE_ITEM(item); 145 146 return (error); 147} 148 149static int 150ng_split_shutdown(node_p node) 151{ 152 const priv_p priv = NG_NODE_PRIVATE(node); 153 154 NG_NODE_SET_PRIVATE(node, NULL); 155 NG_NODE_UNREF(node); 156 free(priv, M_NETGRAPH); 157 158 return (0); 159} 160 161/* 162 * Hook disconnection 163 */ 164static int 165ng_split_disconnect(hook_p hook) 166{ 167 hook_p *localhook = NG_HOOK_PRIVATE(hook); 168 169 KASSERT(localhook != NULL, ("%s: null info", __func__)); 170 *localhook = NULL; 171 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 172 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) { 173 ng_rmnode_self(NG_HOOK_NODE(hook)); 174 } 175 176 return (0); 177} 178