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