1 2 /* 3 * ng_rfc1490.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$ 40 * $Whistle: ng_rfc1490.c,v 1.22 1999/11/01 09:24:52 julian Exp $ 41 */ 42 43 /* 44 * This node does RFC 1490 multiplexing. 45 * 46 * NOTE: RFC 1490 is updated by RFC 2427. 47 */ 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/errno.h> 52 #include <sys/kernel.h> 53 #include <sys/malloc.h> 54 #include <sys/mbuf.h> 55 #include <sys/errno.h> 56 #include <sys/socket.h> 57 58 #include <net/if.h> 59 #include <netinet/in.h> 60 #include <netinet/if_ether.h> 61 62 #include <netgraph/ng_message.h> 63 #include <netgraph/netgraph.h> 64 #include <netgraph/ng_rfc1490.h> 65 66 /* 67 * DEFINITIONS 68 */ 69 70 /* Q.922 stuff -- see RFC 1490 */ 71 #define HDLC_UI 0x03 72 73 #define NLPID_IP 0xCC 74 #define NLPID_PPP 0xCF 75 #define NLPID_SNAP 0x80 76 #define NLPID_Q933 0x08 77 #define NLPID_CLNP 0x81 78 #define NLPID_ESIS 0x82 79 #define NLPID_ISIS 0x83 80 81 /* Node private data */ 82 struct ng_rfc1490_private { 83 hook_p downlink; 84 hook_p ppp; 85 hook_p inet; 86 }; 87 typedef struct ng_rfc1490_private *priv_p; 88 89 /* Netgraph node methods */ 90 static ng_constructor_t ng_rfc1490_constructor; 91 static ng_rcvmsg_t ng_rfc1490_rcvmsg; 92 static ng_shutdown_t ng_rfc1490_shutdown; 93 static ng_newhook_t ng_rfc1490_newhook; 94 static ng_rcvdata_t ng_rfc1490_rcvdata; 95 static ng_disconnect_t ng_rfc1490_disconnect; 96 97 /* Node type descriptor */ 98 static struct ng_type typestruct = { 99 NG_ABI_VERSION, 100 NG_RFC1490_NODE_TYPE, 101 NULL, 102 ng_rfc1490_constructor, 103 ng_rfc1490_rcvmsg, 104 ng_rfc1490_shutdown, 105 ng_rfc1490_newhook, 106 NULL, 107 NULL, 108 ng_rfc1490_rcvdata, 109 ng_rfc1490_disconnect, 110 NULL 111 }; 112 NETGRAPH_INIT(rfc1490, &typestruct); 113 114 /************************************************************************ 115 NETGRAPH NODE STUFF 116 ************************************************************************/ 117 118 /* 119 * Node constructor 120 */ 121 static int 122 ng_rfc1490_constructor(node_p node) 123 { 124 priv_p priv; 125 126 /* Allocate private structure */ 127 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 128 if (priv == NULL) 129 return (ENOMEM); 130 131 node->private = priv; 132 133 /* Done */ 134 return (0); 135 } 136 137 /* 138 * Give our ok for a hook to be added 139 */ 140 static int 141 ng_rfc1490_newhook(node_p node, hook_p hook, const char *name) 142 { 143 const priv_p priv = node->private; 144 145 if (!strcmp(name, NG_RFC1490_HOOK_DOWNSTREAM)) { 146 if (priv->downlink) 147 return (EISCONN); 148 priv->downlink = hook; 149 } else if (!strcmp(name, NG_RFC1490_HOOK_PPP)) { 150 if (priv->ppp) 151 return (EISCONN); 152 priv->ppp = hook; 153 } else if (!strcmp(name, NG_RFC1490_HOOK_INET)) { 154 if (priv->inet) 155 return (EISCONN); 156 priv->inet = hook; 157 } else 158 return (EINVAL); 159 return (0); 160 } 161 162 /* 163 * Receive a control message. We don't support any special ones. 164 */ 165 static int 166 ng_rfc1490_rcvmsg(node_p node, item_p item, hook_p lasthook) 167 { 168 NG_FREE_ITEM(item); 169 return (EINVAL); 170 } 171 172 /* 173 * Receive data on a hook and encapsulate according to RFC 1490. 174 * Only those nodes marked (*) are supported by this routine so far. 175 * 176 * Q.922 control 177 * | 178 * | 179 * -------------------------------------------- 180 * | 0x03 | 181 * UI I Frame 182 * | | 183 * --------------------------------- -------------- 184 * | 0x08 | 0x81 |0xCC |0xCF | 0x00 |..01.... |..10.... 185 * | | | | | 0x80 | | 186 * Q.933 CLNP IP(*) PPP(*) SNAP ISO 8208 ISO 8208 187 * | (rfc1973) | Modulo 8 Modulo 128 188 * | | 189 * -------------------- OUI 190 * | | | 191 * L2 ID L3 ID ------------------------- 192 * | User |00-80-C2 |00-00-00 193 * | specified | | 194 * | 0x70 PID Ethertype 195 * | | | 196 * ------------------- --------------... ---------- 197 * |0x51 |0x4E | |0x4C |0x1 |0xB | |0x806 | 198 * | | | | | | | | | 199 * 7776 Q.922 Others 802.2 802.3 802.6 Others ARP(*) Others 200 * 201 * 202 */ 203 204 #define MAX_ENCAPS_HDR 8 205 #define ERROUT(x) do { error = (x); goto done; } while (0) 206 #define OUICMP(P,A,B,C) ((P)[0]==(A) && (P)[1]==(B) && (P)[2]==(C)) 207 208 static int 209 ng_rfc1490_rcvdata(hook_p hook, item_p item) 210 { 211 const node_p node = hook->node; 212 const priv_p priv = node->private; 213 int error = 0; 214 struct mbuf *m; 215 216 NGI_GET_M(item, m); 217 if (hook == priv->downlink) { 218 u_char *start, *ptr; 219 220 if (!m || (m->m_len < MAX_ENCAPS_HDR 221 && !(m = m_pullup(m, MAX_ENCAPS_HDR)))) 222 ERROUT(ENOBUFS); 223 ptr = start = mtod(m, u_char *); 224 225 /* Must be UI frame */ 226 if (*ptr++ != HDLC_UI) 227 ERROUT(0); 228 229 /* Eat optional zero pad byte */ 230 if (*ptr == 0x00) 231 ptr++; 232 233 /* Multiplex on NLPID */ 234 switch (*ptr++) { 235 case NLPID_SNAP: 236 if (OUICMP(ptr, 0, 0, 0)) { /* It's an ethertype */ 237 u_int16_t etype; 238 239 ptr += 3; 240 etype = ntohs(*((u_int16_t *) ptr)); 241 ptr += 2; 242 m_adj(m, ptr - start); 243 switch (etype) { 244 case ETHERTYPE_IP: 245 NG_FWD_NEW_DATA(error, item, 246 priv->inet, m); 247 break; 248 case ETHERTYPE_ARP: 249 case ETHERTYPE_REVARP: 250 default: 251 ERROUT(0); 252 } 253 } else if (OUICMP(ptr, 0x00, 0x80, 0xc2)) /* 802.1 bridging */ 254 ERROUT(0); 255 else /* Other weird stuff... */ 256 ERROUT(0); 257 break; 258 case NLPID_IP: 259 m_adj(m, ptr - start); 260 NG_FWD_NEW_DATA(error, item, priv->inet, m); 261 break; 262 case NLPID_PPP: 263 m_adj(m, ptr - start); 264 NG_FWD_NEW_DATA(error, item, priv->ppp, m); 265 break; 266 case NLPID_Q933: 267 case NLPID_CLNP: 268 case NLPID_ESIS: 269 case NLPID_ISIS: 270 ERROUT(0); 271 default: /* Try PPP (see RFC 1973) */ 272 ptr--; /* NLPID becomes PPP proto */ 273 if ((*ptr & 0x01) == 0x01) 274 ERROUT(0); 275 m_adj(m, ptr - start); 276 NG_FWD_NEW_DATA(error, item, priv->ppp, m); 277 break; 278 } 279 } else if (hook == priv->ppp) { 280 M_PREPEND(m, 2, M_DONTWAIT); /* Prepend PPP NLPID */ 281 if (!m) 282 ERROUT(ENOBUFS); 283 mtod(m, u_char *)[0] = HDLC_UI; 284 mtod(m, u_char *)[1] = NLPID_PPP; 285 NG_FWD_NEW_DATA(error, item, priv->downlink, m); 286 } else if (hook == priv->inet) { 287 M_PREPEND(m, 2, M_DONTWAIT); /* Prepend IP NLPID */ 288 if (!m) 289 ERROUT(ENOBUFS); 290 mtod(m, u_char *)[0] = HDLC_UI; 291 mtod(m, u_char *)[1] = NLPID_IP; 292 NG_FWD_NEW_DATA(error, item, priv->downlink, m); 293 } else 294 panic(__FUNCTION__); 295 296 done: 297 if (item) 298 NG_FREE_ITEM(item); 299 NG_FREE_M(m); 300 return (error); 301 } 302 303 /* 304 * Nuke node 305 */ 306 static int 307 ng_rfc1490_shutdown(node_p node) 308 { 309 const priv_p priv = node->private; 310 311 /* Take down netgraph node */ 312 node->flags |= NG_INVALID; 313 bzero(priv, sizeof(*priv)); 314 node->private = NULL; 315 ng_unref(node); /* let the node escape */ 316 return (0); 317 } 318 319 /* 320 * Hook disconnection 321 */ 322 static int 323 ng_rfc1490_disconnect(hook_p hook) 324 { 325 const priv_p priv = hook->node->private; 326 327 if ((hook->node->numhooks == 0) 328 && ((hook->node->flags & NG_INVALID) == 0)) 329 ng_rmnode_self(hook->node); 330 else if (hook == priv->downlink) 331 priv->downlink = NULL; 332 else if (hook == priv->inet) 333 priv->inet = NULL; 334 else if (hook == priv->ppp) 335 priv->ppp = NULL; 336 else 337 panic(__FUNCTION__); 338 return (0); 339 } 340 341