2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003-2004 Benno Rice <benno@eloquent.com.au>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
39 #include <netgraph/ng_message.h>
40 #include <netgraph/netgraph.h>
41 #include <netgraph/ng_atmllc.h>
44 #include <net/ethernet.h> /* for M_HASFCS and ETHER_HDR_LEN */
46 #define NG_ATMLLC_HEADER "\252\252\3\0\200\302"
47 #define NG_ATMLLC_HEADER_LEN (sizeof(struct atmllc))
48 #define NG_ATMLLC_TYPE_ETHERNET_FCS 0x0001
49 #define NG_ATMLLC_TYPE_FDDI_FCS 0x0004
50 #define NG_ATMLLC_TYPE_ETHERNET_NOFCS 0x0007
51 #define NG_ATMLLC_TYPE_FDDI_NOFCS 0x000A
53 struct ng_atmllc_priv {
60 uint8_t llchdr[6]; /* aa.aa.03.00.00.00 */
61 uint8_t type[2]; /* "ethernet" type */
64 /* ATM_LLC macros: note type code in host byte order */
65 #define ATM_LLC_TYPE(X) (((X)->type[0] << 8) | ((X)->type[1]))
66 #define ATM_LLC_SETTYPE(X, V) do { \
67 (X)->type[0] = ((V) >> 8) & 0xff; \
68 (X)->type[1] = ((V) & 0xff); \
71 /* Netgraph methods. */
72 static ng_constructor_t ng_atmllc_constructor;
73 static ng_shutdown_t ng_atmllc_shutdown;
74 static ng_rcvmsg_t ng_atmllc_rcvmsg;
75 static ng_newhook_t ng_atmllc_newhook;
76 static ng_rcvdata_t ng_atmllc_rcvdata;
77 static ng_disconnect_t ng_atmllc_disconnect;
79 static struct ng_type ng_atmllc_typestruct = {
80 .version = NG_ABI_VERSION,
81 .name = NG_ATMLLC_NODE_TYPE,
82 .constructor = ng_atmllc_constructor,
83 .rcvmsg = ng_atmllc_rcvmsg,
84 .shutdown = ng_atmllc_shutdown,
85 .newhook = ng_atmllc_newhook,
86 .rcvdata = ng_atmllc_rcvdata,
87 .disconnect = ng_atmllc_disconnect,
89 NETGRAPH_INIT(atmllc, &ng_atmllc_typestruct);
92 ng_atmllc_constructor(node_p node)
94 struct ng_atmllc_priv *priv;
96 priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
97 NG_NODE_SET_PRIVATE(node, priv);
103 ng_atmllc_rcvmsg(node_p node, item_p item, hook_p lasthook)
109 NGI_GET_MSG(item, msg);
110 msg->header.flags |= NGF_RESP;
111 NG_RESPOND_MSG(error, node, item, msg);
116 ng_atmllc_shutdown(node_p node)
118 struct ng_atmllc_priv *priv;
120 priv = NG_NODE_PRIVATE(node);
122 free(priv, M_NETGRAPH);
130 ng_atmllc_newhook(node_p node, hook_p hook, const char *name)
132 struct ng_atmllc_priv *priv;
134 priv = NG_NODE_PRIVATE(node);
136 if (strcmp(name, NG_ATMLLC_HOOK_ATM) == 0) {
137 if (priv->atm != NULL) {
141 } else if (strcmp(name, NG_ATMLLC_HOOK_ETHER) == 0) {
142 if (priv->ether != NULL) {
146 } else if (strcmp(name, NG_ATMLLC_HOOK_FDDI) == 0) {
147 if (priv->fddi != NULL) {
159 ng_atmllc_rcvdata(hook_p hook, item_p item)
161 struct ng_atmllc_priv *priv;
168 priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
173 if (hook == priv->atm) {
174 /* Ditch the pseudoheader. */
175 hdr = mtod(m, struct atmllc *);
176 /* m_adj(m, sizeof(struct atm_pseudohdr)); */
179 * Make sure we have the LLC and ethernet headers.
180 * The ethernet header size is slightly larger than the FDDI
181 * header, which is convenient.
183 if (m->m_len < sizeof(struct atmllc) + ETHER_HDR_LEN) {
184 m = m_pullup(m, sizeof(struct atmllc) + ETHER_HDR_LEN);
191 /* Decode the LLC header. */
192 hdr = mtod(m, struct atmllc *);
193 if (ATM_LLC_TYPE(hdr) == NG_ATMLLC_TYPE_ETHERNET_NOFCS) {
194 m->m_flags &= ~M_HASFCS;
195 outhook = priv->ether;
197 } else if (ATM_LLC_TYPE(hdr) == NG_ATMLLC_TYPE_ETHERNET_FCS) {
198 m->m_flags |= M_HASFCS;
199 outhook = priv->ether;
201 } else if (ATM_LLC_TYPE(hdr) == NG_ATMLLC_TYPE_FDDI_NOFCS) {
202 m->m_flags &= ~M_HASFCS;
203 outhook = priv->fddi;
205 } else if (ATM_LLC_TYPE(hdr) == NG_ATMLLC_TYPE_FDDI_FCS) {
206 m->m_flags |= M_HASFCS;
207 outhook = priv->fddi;
210 printf("ng_atmllc: unknown type: %x\n",
214 /* Remove the LLC header and any padding*/
215 m_adj(m, sizeof(struct atmllc) + padding);
216 } else if (hook == priv->ether) {
217 /* Add the LLC header */
218 M_PREPEND(m, NG_ATMLLC_HEADER_LEN + 2, M_NOWAIT);
220 printf("ng_atmllc: M_PREPEND failed\n");
224 hdr = mtod(m, struct atmllc *);
225 bzero((void *)hdr, sizeof(struct atmllc) + 2);
226 bcopy(NG_ATMLLC_HEADER, hdr->llchdr, 6);
227 if ((m->m_flags & M_HASFCS) != 0) {
228 ATM_LLC_SETTYPE(hdr, NG_ATMLLC_TYPE_ETHERNET_FCS);
230 ATM_LLC_SETTYPE(hdr, NG_ATMLLC_TYPE_ETHERNET_NOFCS);
233 } else if (hook == priv->fddi) {
234 /* Add the LLC header */
235 M_PREPEND(m, NG_ATMLLC_HEADER_LEN + 3, M_NOWAIT);
237 printf("ng_atmllc: M_PREPEND failed\n");
241 hdr = mtod(m, struct atmllc *);
242 bzero((void *)hdr, sizeof(struct atmllc) + 3);
243 bcopy(NG_ATMLLC_HEADER, hdr->llchdr, 6);
244 if ((m->m_flags & M_HASFCS) != 0) {
245 ATM_LLC_SETTYPE(hdr, NG_ATMLLC_TYPE_FDDI_FCS);
247 ATM_LLC_SETTYPE(hdr, NG_ATMLLC_TYPE_FDDI_NOFCS);
252 if (outhook == NULL) {
258 NG_FWD_NEW_DATA(error, item, outhook, m);
263 ng_atmllc_disconnect(hook_p hook)
266 struct ng_atmllc_priv *priv;
268 node = NG_HOOK_NODE(hook);
269 priv = NG_NODE_PRIVATE(node);
271 if (hook == priv->atm) {
273 } else if (hook == priv->ether) {
275 } else if (hook == priv->fddi) {
279 if (NG_NODE_NUMHOOKS(node) == 0 && NG_NODE_IS_VALID(node)) {
280 ng_rmnode_self(node);