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