ng_UI.c revision 69922
190261Simp 290261Simp/* 390261Simp * ng_UI.c 490261Simp * 590261Simp * Copyright (c) 1996-1999 Whistle Communications, Inc. 690261Simp * All rights reserved. 790261Simp * 890261Simp * Subject to the following obligations and disclaimer of warranty, use and 990261Simp * redistribution of this software, in source or object code forms, with or 1090261Simp * without modifications are expressly permitted by Whistle Communications; 1190261Simp * provided, however, that: 1290261Simp * 1. Any and all reproductions of the source or object code must include the 1390261Simp * copyright notice above and the following disclaimer of warranties; and 1490261Simp * 2. No rights are granted, in any manner or form, to use Whistle 1590261Simp * Communications, Inc. trademarks, including the mark "WHISTLE 1690261Simp * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 1790261Simp * such appears in the above copyright notice or in the software. 1890261Simp * 1990261Simp * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 2090261Simp * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 2190261Simp * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 2290261Simp * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 2390261Simp * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 2490261Simp * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 2590261Simp * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 2690261Simp * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 2790261Simp * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28241777Sed * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29241777Sed * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30241777Sed * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 3190261Simp * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32112998Sjmallett * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3390261Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3490262Simp * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 3590261Simp * OF SUCH DAMAGE. 3690261Simp * 3790261Simp * Author: Julian Elischer <julian@freebsd.org> 3890261Simp * 39112998Sjmallett * $FreeBSD: head/sys/netgraph/ng_UI.c 69922 2000-12-12 18:52:14Z julian $ 4090261Simp * $Whistle: ng_UI.c,v 1.14 1999/11/01 09:24:51 julian Exp $ 4190261Simp */ 4290261Simp 4390261Simp#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/errno.h> 46#include <sys/kernel.h> 47#include <sys/malloc.h> 48#include <sys/mbuf.h> 49#include <sys/errno.h> 50 51 52#include <netgraph/ng_message.h> 53#include <netgraph/netgraph.h> 54#include <netgraph/ng_UI.h> 55 56/* 57 * DEFINITIONS 58 */ 59 60/* Everything, starting with sdlc on has defined UI as 0x03 */ 61#define HDLC_UI 0x03 62 63/* Node private data */ 64struct ng_UI_private { 65 hook_p downlink; 66 hook_p uplink; 67}; 68typedef struct ng_UI_private *priv_p; 69 70/* Netgraph node methods */ 71static ng_constructor_t ng_UI_constructor; 72static ng_rcvmsg_t ng_UI_rcvmsg; 73static ng_shutdown_t ng_UI_rmnode; 74static ng_newhook_t ng_UI_newhook; 75static ng_rcvdata_t ng_UI_rcvdata; 76static ng_disconnect_t ng_UI_disconnect; 77 78/* Node type descriptor */ 79static struct ng_type typestruct = { 80 NG_VERSION, 81 NG_UI_NODE_TYPE, 82 NULL, 83 ng_UI_constructor, 84 ng_UI_rcvmsg, 85 ng_UI_rmnode, 86 ng_UI_newhook, 87 NULL, 88 NULL, 89 ng_UI_rcvdata, 90 ng_UI_disconnect, 91 NULL 92}; 93NETGRAPH_INIT(UI, &typestruct); 94 95/************************************************************************ 96 NETGRAPH NODE STUFF 97 ************************************************************************/ 98 99/* 100 * Create a newborn node. We start with an implicit reference. 101 */ 102 103static int 104ng_UI_constructor(node_p *nodep) 105{ 106 priv_p priv; 107 int error; 108 109 /* Allocate private structure */ 110 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 111 if (priv == NULL) 112 return (ENOMEM); 113 114 /* Call generic node constructor */ 115 if ((error = ng_make_node_common(&typestruct, nodep))) { 116 FREE(priv, M_NETGRAPH); 117 return (error); 118 } 119 (*nodep)->private = priv; 120 121 /* Done */ 122 return (0); 123} 124 125/* 126 * Give our ok for a hook to be added 127 */ 128static int 129ng_UI_newhook(node_p node, hook_p hook, const char *name) 130{ 131 const priv_p priv = node->private; 132 133 if (!strcmp(name, NG_UI_HOOK_DOWNSTREAM)) { 134 if (priv->downlink) 135 return (EISCONN); 136 priv->downlink = hook; 137 } else if (!strcmp(name, NG_UI_HOOK_UPSTREAM)) { 138 if (priv->uplink) 139 return (EISCONN); 140 priv->uplink = hook; 141 } else 142 return (EINVAL); 143 return (0); 144} 145 146/* 147 * Receive a control message 148 */ 149static int 150ng_UI_rcvmsg(node_p node, struct ng_mesg *msg, 151 const char *raddr, struct ng_mesg **rp, hook_p lasthook) 152{ 153 const priv_p priv = node->private; 154 155 if ((msg->header.typecookie == NGM_FLOW_COOKIE) && lasthook) { 156 if (lasthook == priv->downlink) { 157 if (priv->uplink) { 158 return (ng_send_msg(node, msg, NULL, 159 priv->uplink, raddr, rp)); 160 } 161 } else { 162 if (priv->downlink) { 163 return (ng_send_msg(node, msg, NULL, 164 priv->downlink, raddr, rp)); 165 } 166 } 167 } 168 169 FREE(msg, M_NETGRAPH); 170 return (EINVAL); 171} 172 173#define MAX_ENCAPS_HDR 1 174#define ERROUT(x) do { error = (x); goto done; } while (0) 175 176/* 177 * Receive a data frame 178 */ 179static int 180ng_UI_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 181 struct mbuf **ret_m, meta_p *ret_meta, struct ng_mesg **resp) 182{ 183 const node_p node = hook->node; 184 const priv_p priv = node->private; 185 int error = 0; 186 187 if (hook == priv->downlink) { 188 u_char *start, *ptr; 189 190 if (!m || (m->m_len < MAX_ENCAPS_HDR 191 && !(m = m_pullup(m, MAX_ENCAPS_HDR)))) 192 ERROUT(ENOBUFS); 193 ptr = start = mtod(m, u_char *); 194 195 /* Must be UI frame */ 196 if (*ptr++ != HDLC_UI) 197 ERROUT(0); 198 199 m_adj(m, ptr - start); 200 NG_SEND_DATA(error, priv->uplink, m, meta); /* m -> NULL */ 201 } else if (hook == priv->uplink) { 202 M_PREPEND(m, 1, M_DONTWAIT); /* Prepend IP NLPID */ 203 if (!m) 204 ERROUT(ENOBUFS); 205 mtod(m, u_char *)[0] = HDLC_UI; 206 NG_SEND_DATA(error, priv->downlink, m, meta); /* m -> NULL */ 207 } else 208 panic(__FUNCTION__); 209 210done: 211 NG_FREE_DATA(m, meta); /* does nothing if m == NULL */ 212 return (error); 213} 214 215/* 216 * Shutdown node 217 */ 218static int 219ng_UI_rmnode(node_p node) 220{ 221 const priv_p priv = node->private; 222 223 /* Take down netgraph node */ 224 node->flags |= NG_INVALID; 225 ng_cutlinks(node); 226 ng_unname(node); 227 bzero(priv, sizeof(*priv)); 228 FREE(priv, M_NETGRAPH); 229 node->private = NULL; 230 ng_unref(node); 231 return (0); 232} 233 234/* 235 * Hook disconnection 236 */ 237static int 238ng_UI_disconnect(hook_p hook) 239{ 240 const priv_p priv = hook->node->private; 241 242 if (hook->node->numhooks == 0) 243 ng_rmnode(hook->node); 244 else if (hook == priv->downlink) 245 priv->downlink = NULL; 246 else if (hook == priv->uplink) 247 priv->uplink = NULL; 248 else 249 panic(__FUNCTION__); 250 return (0); 251} 252 253