]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netgraph/ng_atmllc.c
MFV r326007: less v529.
[FreeBSD/FreeBSD.git] / sys / netgraph / ng_atmllc.c
1 /*-
2  * Copyright (c) 2003-2004 Benno Rice <benno@eloquent.com.au>
3  * All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/mbuf.h>
34 #include <sys/socket.h>
35 #include <sys/sockio.h>
36
37 #include <netgraph/ng_message.h>
38 #include <netgraph/netgraph.h>
39 #include <netgraph/ng_atmllc.h>
40
41 #include <net/if.h>
42 #include <net/ethernet.h>       /* for M_HASFCS and ETHER_HDR_LEN */
43
44 #define NG_ATMLLC_HEADER                "\252\252\3\0\200\302"
45 #define NG_ATMLLC_HEADER_LEN            (sizeof(struct atmllc))
46 #define NG_ATMLLC_TYPE_ETHERNET_FCS     0x0001
47 #define NG_ATMLLC_TYPE_FDDI_FCS         0x0004
48 #define NG_ATMLLC_TYPE_ETHERNET_NOFCS   0x0007
49 #define NG_ATMLLC_TYPE_FDDI_NOFCS       0x000A
50
51 struct ng_atmllc_priv {
52         hook_p          atm;
53         hook_p          ether;
54         hook_p          fddi;
55 };
56
57 struct atmllc {
58         uint8_t         llchdr[6];      /* aa.aa.03.00.00.00 */
59         uint8_t         type[2];        /* "ethernet" type */
60 };
61
62 /* ATM_LLC macros: note type code in host byte order */
63 #define ATM_LLC_TYPE(X) (((X)->type[0] << 8) | ((X)->type[1]))
64 #define ATM_LLC_SETTYPE(X, V) do {              \
65         (X)->type[0] = ((V) >> 8) & 0xff;       \
66         (X)->type[1] = ((V) & 0xff);            \
67     } while (0)
68
69 /* Netgraph methods. */
70 static ng_constructor_t         ng_atmllc_constructor;
71 static ng_shutdown_t            ng_atmllc_shutdown;
72 static ng_rcvmsg_t              ng_atmllc_rcvmsg;
73 static ng_newhook_t             ng_atmllc_newhook;
74 static ng_rcvdata_t             ng_atmllc_rcvdata;
75 static ng_disconnect_t          ng_atmllc_disconnect;
76
77 static struct ng_type ng_atmllc_typestruct = {
78         .version =      NG_ABI_VERSION, 
79         .name =         NG_ATMLLC_NODE_TYPE,
80         .constructor =  ng_atmllc_constructor,
81         .rcvmsg =       ng_atmllc_rcvmsg,
82         .shutdown =     ng_atmllc_shutdown,
83         .newhook =      ng_atmllc_newhook,
84         .rcvdata =      ng_atmllc_rcvdata,
85         .disconnect =   ng_atmllc_disconnect,
86 };
87 NETGRAPH_INIT(atmllc, &ng_atmllc_typestruct);
88
89 static int
90 ng_atmllc_constructor(node_p node)
91 {
92         struct  ng_atmllc_priv *priv;
93
94         priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
95         NG_NODE_SET_PRIVATE(node, priv);
96
97         return (0);
98 }
99
100 static int
101 ng_atmllc_rcvmsg(node_p node, item_p item, hook_p lasthook)
102 {
103         struct  ng_mesg *msg;
104         int     error;
105
106         error = 0;
107         NGI_GET_MSG(item, msg);
108         msg->header.flags |= NGF_RESP;
109         NG_RESPOND_MSG(error, node, item, msg);
110         return (error);
111 }
112
113 static int
114 ng_atmllc_shutdown(node_p node)
115 {
116         struct  ng_atmllc_priv *priv;
117
118         priv = NG_NODE_PRIVATE(node);
119
120         free(priv, M_NETGRAPH);
121
122         NG_NODE_UNREF(node);
123
124         return (0);
125 }
126
127 static int
128 ng_atmllc_newhook(node_p node, hook_p hook, const char *name)
129 {
130         struct  ng_atmllc_priv *priv;
131
132         priv = NG_NODE_PRIVATE(node);
133
134         if (strcmp(name, NG_ATMLLC_HOOK_ATM) == 0) {
135                 if (priv->atm != NULL) {
136                         return (EISCONN);
137                 }
138                 priv->atm = hook;
139         } else if (strcmp(name, NG_ATMLLC_HOOK_ETHER) == 0) {
140                 if (priv->ether != NULL) {
141                         return (EISCONN);
142                 }
143                 priv->ether = hook;
144         } else if (strcmp(name, NG_ATMLLC_HOOK_FDDI) == 0) {
145                 if (priv->fddi != NULL) {
146                         return (EISCONN);
147                 }
148                 priv->fddi = hook;
149         } else {
150                 return (EINVAL);
151         }
152
153         return (0);
154 }
155
156 static int
157 ng_atmllc_rcvdata(hook_p hook, item_p item)
158 {
159         struct  ng_atmllc_priv *priv;
160         struct  mbuf *m;
161         struct  atmllc *hdr;
162         hook_p  outhook;
163         u_int   padding;
164         int     error;
165
166         priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
167         NGI_GET_M(item, m);
168         outhook = NULL;
169         padding = 0;
170
171         if (hook == priv->atm) {
172                 /* Ditch the psuedoheader. */
173                 hdr = mtod(m, struct atmllc *);
174                 /* m_adj(m, sizeof(struct atm_pseudohdr)); */
175
176                 /*
177                  * Make sure we have the LLC and ethernet headers.
178                  * The ethernet header size is slightly larger than the FDDI
179                  * header, which is convenient.
180                  */
181                 if (m->m_len < sizeof(struct atmllc) + ETHER_HDR_LEN) {
182                         m = m_pullup(m, sizeof(struct atmllc) + ETHER_HDR_LEN);
183                         if (m == NULL) {
184                                 NG_FREE_ITEM(item);
185                                 return (ENOMEM);
186                         }
187                 }
188
189                 /* Decode the LLC header. */
190                 hdr = mtod(m, struct atmllc *);
191                 if (ATM_LLC_TYPE(hdr) == NG_ATMLLC_TYPE_ETHERNET_NOFCS) {
192                         m->m_flags &= ~M_HASFCS;
193                         outhook = priv->ether;
194                         padding = 2;
195                 } else if (ATM_LLC_TYPE(hdr) == NG_ATMLLC_TYPE_ETHERNET_FCS) {
196                         m->m_flags |= M_HASFCS;
197                         outhook = priv->ether;
198                         padding = 2;
199                 } else if (ATM_LLC_TYPE(hdr) == NG_ATMLLC_TYPE_FDDI_NOFCS) {
200                         m->m_flags &= ~M_HASFCS;
201                         outhook = priv->fddi;
202                         padding = 3;
203                 } else if (ATM_LLC_TYPE(hdr) == NG_ATMLLC_TYPE_FDDI_FCS) {
204                         m->m_flags |= M_HASFCS;
205                         outhook = priv->fddi;
206                         padding = 3;
207                 } else {
208                         printf("ng_atmllc: unknown type: %x\n",
209                             ATM_LLC_TYPE(hdr));
210                 }
211
212                 /* Remove the LLC header and any padding*/
213                 m_adj(m, sizeof(struct atmllc) + padding);
214         } else if (hook == priv->ether) {
215                 /* Add the LLC header */
216                 M_PREPEND(m, NG_ATMLLC_HEADER_LEN + 2, M_NOWAIT);
217                 if (m == NULL) {
218                         printf("ng_atmllc: M_PREPEND failed\n");
219                         NG_FREE_ITEM(item);
220                         return (ENOMEM);
221                 }
222                 hdr = mtod(m, struct atmllc *);
223                 bzero((void *)hdr, sizeof(struct atmllc) + 2);
224                 bcopy(NG_ATMLLC_HEADER, hdr->llchdr, 6);
225                 if ((m->m_flags & M_HASFCS) != 0) {
226                         ATM_LLC_SETTYPE(hdr, NG_ATMLLC_TYPE_ETHERNET_FCS);
227                 } else {
228                         ATM_LLC_SETTYPE(hdr, NG_ATMLLC_TYPE_ETHERNET_NOFCS);
229                 }
230                 outhook = priv->atm;
231         } else if (hook == priv->fddi) {
232                 /* Add the LLC header */
233                 M_PREPEND(m, NG_ATMLLC_HEADER_LEN + 3, M_NOWAIT);
234                 if (m == NULL) {
235                         printf("ng_atmllc: M_PREPEND failed\n");
236                         NG_FREE_ITEM(item);
237                         return (ENOMEM);
238                 }
239                 hdr = mtod(m, struct atmllc *);
240                 bzero((void *)hdr, sizeof(struct atmllc) + 3);
241                 bcopy(NG_ATMLLC_HEADER, hdr->llchdr, 6);
242                 if ((m->m_flags & M_HASFCS) != 0) {
243                         ATM_LLC_SETTYPE(hdr, NG_ATMLLC_TYPE_FDDI_FCS);
244                 } else {
245                         ATM_LLC_SETTYPE(hdr, NG_ATMLLC_TYPE_FDDI_NOFCS);
246                 }
247                 outhook = priv->atm;
248         }
249
250         if (outhook == NULL) {
251                 NG_FREE_M(m);
252                 NG_FREE_ITEM(item);
253                 return (0);
254         }
255
256         NG_FWD_NEW_DATA(error, item, outhook, m);
257         return (error);
258 }
259
260 static int
261 ng_atmllc_disconnect(hook_p hook)
262 {
263         node_p  node;
264         struct  ng_atmllc_priv *priv;
265
266         node = NG_HOOK_NODE(hook);
267         priv = NG_NODE_PRIVATE(node);
268
269         if (hook == priv->atm) {
270                 priv->atm = NULL;
271         } else if (hook == priv->ether) {
272                 priv->ether = NULL;
273         } else if (hook == priv->fddi) {
274                 priv->fddi = NULL;
275         }
276
277         if (NG_NODE_NUMHOOKS(node) == 0 && NG_NODE_IS_VALID(node)) {
278                 ng_rmnode_self(node);
279         }
280
281         return (0);
282 }