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