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