ng_split.c revision 72909
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: head/sys/netgraph/ng_split.c 72909 2001-02-22 17:14:36Z julian $ 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_rcvmsg_t ng_split_rcvmsg; 50static ng_shutdown_t ng_split_rmnode; 51static ng_newhook_t ng_split_newhook; 52static ng_rcvdata_t ng_split_rcvdata; 53static ng_connect_t ng_split_connect; 54static ng_disconnect_t ng_split_disconnect; 55 56/* Node type descriptor */ 57static struct ng_type typestruct = { 58 NG_ABI_VERSION, 59 NG_SPLIT_NODE_TYPE, 60 NULL, 61 ng_split_constructor, 62 ng_split_rcvmsg, 63 ng_split_rmnode, 64 ng_split_newhook, 65 NULL, 66 ng_split_connect, 67 ng_split_rcvdata, 68 ng_split_disconnect, 69 NULL 70}; 71NETGRAPH_INIT(ng_split, &typestruct); 72 73/* Node private data */ 74struct ng_split_private { 75 hook_p outhook; 76 hook_p inhook; 77 hook_p mixed; 78 node_p node; /* Our netgraph node */ 79}; 80typedef struct ng_split_private *priv_p; 81 82/************************************************************************ 83 NETGRAPH NODE STUFF 84 ************************************************************************/ 85 86/* 87 * Constructor for a node 88 */ 89static int 90ng_split_constructor(node_p node) 91{ 92 priv_p priv; 93 94 /* Allocate node */ 95 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_ZERO | M_NOWAIT); 96 if (priv == NULL) 97 return (ENOMEM); 98 bzero(priv, sizeof(*priv)); 99 100 /* Link together node and private info */ 101 NG_NODE_SET_PRIVATE(node, priv); 102 priv->node = node; 103 104 /* Done */ 105 return (0); 106} 107 108/* 109 * Give our ok for a hook to be added 110 */ 111static int 112ng_split_newhook(node_p node, hook_p hook, const char *name) 113{ 114 priv_p priv = NG_NODE_PRIVATE(node); 115 116 if (strcmp(name, NG_SPLIT_HOOK_MIXED)) { 117 if (strcmp(name, NG_SPLIT_HOOK_INHOOK)) { 118 if (strcmp(name, NG_SPLIT_HOOK_OUTHOOK)) 119 return (EPFNOSUPPORT); 120 else { 121 if (priv->outhook != NULL) 122 return (EISCONN); 123 priv->outhook = hook; 124 NG_HOOK_SET_PRIVATE(hook, &(priv->outhook)); 125 } 126 } else { 127 if (priv->inhook != NULL) 128 return (EISCONN); 129 priv->inhook = hook; 130 NG_HOOK_SET_PRIVATE(hook, &(priv->inhook)); 131 } 132 } else { 133 if (priv->mixed != NULL) 134 return (EISCONN); 135 priv->mixed = hook; 136 NG_HOOK_SET_PRIVATE(hook, &(priv->mixed)); 137 } 138 139 return (0); 140} 141 142/* 143 * Receive a control message 144 */ 145static int 146ng_split_rcvmsg(node_p node, item_p item, hook_p lasthook) 147{ 148 NG_FREE_ITEM(item); 149 return (EINVAL); 150} 151 152/* 153 * Recive data from a hook. 154 */ 155static int 156ng_split_rcvdata(hook_p hook, item_p item) 157{ 158 meta_p meta; 159 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 160 int error = 0; 161 162 if (hook == priv->outhook) { 163 printf("ng_split: got packet from outhook!\n"); 164 NG_FREE_ITEM(item); 165 return (EINVAL); 166 } 167#if 0 /* should never happen */ 168 if (NGI_M(item) == NULL) { 169 printf("ng_split: mbuf is null.\n"); 170 NG_FREE_ITEM(item); 171 return (EINVAL); 172 } 173#endif 174 /* 175 * XXX Really here we should just remove metadata we understand. 176 */ 177 NGI_GET_META(item, meta); 178 NG_FREE_META(meta); 179 if ((hook == priv->inhook) && (priv->mixed)) { 180 NG_FWD_ITEM_HOOK(error, item, priv->mixed); 181 } else if ((hook == priv->mixed) && (priv->outhook)) { 182 NG_FWD_ITEM_HOOK(error, item, priv->outhook); 183 } 184 return (error); 185} 186 187static int 188ng_split_rmnode(node_p node) 189{ 190 const priv_p priv = NG_NODE_PRIVATE(node); 191 192 NG_NODE_SET_PRIVATE(node, NULL); 193 NG_NODE_UNREF(node); 194 FREE(priv, M_NETGRAPH); 195 196 return (0); 197} 198 199 200/* 201 * This is called once we've already connected a new hook to the other node. 202 * It gives us a chance to balk at the last minute. 203 */ 204static int 205ng_split_connect(hook_p hook) 206{ 207 /* be really amiable and just say "YUP that's OK by me! " */ 208 return (0); 209} 210 211/* 212 * Hook disconnection 213 */ 214static int 215ng_split_disconnect(hook_p hook) 216{ 217 if (NG_HOOK_PRIVATE(hook)) { 218 *((hook_p *)NG_HOOK_PRIVATE(hook)) = (hook_p)0; 219 } 220 221 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 222 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) { 223 ng_rmnode_self(NG_HOOK_NODE(hook)); 224 } 225 226 return (0); 227} 228