kernel: Sync ACPICA with Intel's version 20140424.
[dragonfly.git] / sys / netgraph7 / atmllc / 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: src/sys/netgraph/ng_atmllc.c,v 1.3 2005/01/07 01:45:39 imp Exp $
27  * $DragonFly: src/sys/netgraph7/ng_atmllc.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/mbuf.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
37
38 #include <netgraph7/ng_message.h>
39 #include <netgraph7/netgraph.h>
40 #include "ng_atmllc.h"
41
42 #include <net/if.h>
43 #include <net/ethernet.h>       /* for M_HASFCS and ETHER_HDR_LEN */
44 #include <net/if_atm.h>         /* for struct atmllc */
45
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
52
53 struct ng_atmllc_priv {
54         hook_p          atm;
55         hook_p          ether;
56         hook_p          fddi;
57 };
58
59 /* Netgraph methods. */
60 static ng_constructor_t         ng_atmllc_constructor;
61 static ng_shutdown_t            ng_atmllc_shutdown;
62 static ng_rcvmsg_t              ng_atmllc_rcvmsg;
63 static ng_newhook_t             ng_atmllc_newhook;
64 static ng_rcvdata_t             ng_atmllc_rcvdata;
65 static ng_disconnect_t          ng_atmllc_disconnect;
66
67 static struct ng_type ng_atmllc_typestruct = {
68         .version =      NG_ABI_VERSION, 
69         .name =         NG_ATMLLC_NODE_TYPE,
70         .constructor =  ng_atmllc_constructor,
71         .rcvmsg =       ng_atmllc_rcvmsg,
72         .shutdown =     ng_atmllc_shutdown,
73         .newhook =      ng_atmllc_newhook,
74         .rcvdata =      ng_atmllc_rcvdata,
75         .disconnect =   ng_atmllc_disconnect,
76 };
77 NETGRAPH_INIT(atmllc, &ng_atmllc_typestruct);
78
79 static int
80 ng_atmllc_constructor(node_p node)
81 {
82         struct  ng_atmllc_priv *priv;
83
84         priv = kmalloc(sizeof(*priv), M_NETGRAPH,
85                        M_WAITOK | M_NULLOK | M_ZERO);
86         if (priv == NULL) {
87                 return (ENOMEM);
88         }
89
90         NG_NODE_SET_PRIVATE(node, priv);
91
92         return (0);
93 }
94
95 static int
96 ng_atmllc_rcvmsg(node_p node, item_p item, hook_p lasthook)
97 {
98         struct  ng_mesg *msg;
99         int     error;
100
101         error = 0;
102         NGI_GET_MSG(item, msg);
103         msg->header.flags |= NGF_RESP;
104         NG_RESPOND_MSG(error, node, item, msg);
105         return (error);
106 }
107
108 static int
109 ng_atmllc_shutdown(node_p node)
110 {
111         struct  ng_atmllc_priv *priv;
112
113         priv = NG_NODE_PRIVATE(node);
114
115         kfree(priv, M_NETGRAPH);
116
117         NG_NODE_UNREF(node);
118
119         return (0);
120 }
121
122 static int
123 ng_atmllc_newhook(node_p node, hook_p hook, const char *name)
124 {
125         struct  ng_atmllc_priv *priv;
126
127         priv = NG_NODE_PRIVATE(node);
128
129         if (strcmp(name, NG_ATMLLC_HOOK_ATM) == 0) {
130                 if (priv->atm != NULL) {
131                         return (EISCONN);
132                 }
133                 priv->atm = hook;
134         } else if (strcmp(name, NG_ATMLLC_HOOK_ETHER) == 0) {
135                 if (priv->ether != NULL) {
136                         return (EISCONN);
137                 }
138                 priv->ether = hook;
139         } else if (strcmp(name, NG_ATMLLC_HOOK_FDDI) == 0) {
140                 if (priv->fddi != NULL) {
141                         return (EISCONN);
142                 }
143                 priv->fddi = hook;
144         } else {
145                 return (EINVAL);
146         }
147
148         return (0);
149 }
150
151 static int
152 ng_atmllc_rcvdata(hook_p hook, item_p item)
153 {
154         struct  ng_atmllc_priv *priv;
155         struct  mbuf *m;
156         struct  atmllc *hdr;
157         hook_p  outhook;
158         u_int   padding;
159         int     error;
160
161         priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
162         m = NGI_M(item);
163         outhook = NULL;
164         padding = 0;
165
166         if (hook == priv->atm) {
167                 /* Ditch the psuedoheader. */
168                 hdr = mtod(m, struct atmllc *);
169                 /* m_adj(m, sizeof(struct atm_pseudohdr)); */
170
171                 /*
172                  * Make sure we have the LLC and ethernet headers.
173                  * The ethernet header size is slightly larger than the FDDI
174                  * header, which is convenient.
175                  */
176                 if (m->m_len < sizeof(struct atmllc) + ETHER_HDR_LEN) {
177                         m = m_pullup(m, sizeof(struct atmllc) + ETHER_HDR_LEN);
178                         if (m == NULL) {
179                                 return (ENOMEM);
180                         }
181                 }
182
183                 /* Decode the LLC header. */
184                 hdr = mtod(m, struct atmllc *);
185                 if (ATM_LLC_TYPE(hdr) == NG_ATMLLC_TYPE_ETHERNET_NOFCS) {
186                         outhook = priv->ether;
187                         padding = 2;
188                 } else if (ATM_LLC_TYPE(hdr) == NG_ATMLLC_TYPE_ETHERNET_FCS) {
189                         m_adj(m, -ETHER_CRC_LEN);
190                         outhook = priv->ether;
191                         padding = 2;
192 #if 0
193                 } else if (ATM_LLC_TYPE(hdr) == NG_ATMLLC_TYPE_FDDI_NOFCS) {
194                         m->m_flags &= ~M_HASFCS;
195                         outhook = priv->fddi;
196                         padding = 3;
197                 } else if (ATM_LLC_TYPE(hdr) == NG_ATMLLC_TYPE_FDDI_FCS) {
198                         m->m_flags |= M_HASFCS;
199                         outhook = priv->fddi;
200                         padding = 3;
201 #endif
202                 } else {
203                         printf("ng_atmllc: unknown type: %x\n",
204                             ATM_LLC_TYPE(hdr));
205                 }
206
207                 /* Remove the LLC header and any padding*/
208                 m_adj(m, sizeof(struct atmllc) + padding);
209         } else if (hook == priv->ether) {
210                 /* Add the LLC header */
211                 M_PREPEND(m, NG_ATMLLC_HEADER_LEN + 2, MB_DONTWAIT);
212                 if (m == NULL) {
213                         printf("ng_atmllc: M_PREPEND failed\n");
214                         NG_FREE_ITEM(item);
215                         return (ENOMEM);
216                 }
217                 hdr = mtod(m, struct atmllc *);
218                 bzero((void *)hdr, sizeof(struct atmllc) + 2);
219                 bcopy(NG_ATMLLC_HEADER, hdr->llchdr, 6);
220 #if 0
221                 if ((m->m_flags & M_HASFCS) != 0) {
222                         ATM_LLC_SETTYPE(hdr, NG_ATMLLC_TYPE_ETHERNET_FCS);
223                 } else {
224 #endif
225                         ATM_LLC_SETTYPE(hdr, NG_ATMLLC_TYPE_ETHERNET_NOFCS);
226 #if 0
227                 }
228 #endif
229                 outhook = priv->atm;
230         } else if (hook == priv->fddi) {
231                 /* Add the LLC header */
232                 M_PREPEND(m, NG_ATMLLC_HEADER_LEN + 3, MB_DONTWAIT);
233                 if (m == NULL) {
234                         printf("ng_atmllc: M_PREPEND failed\n");
235                         NG_FREE_ITEM(item);
236                         return (ENOMEM);
237                 }
238                 hdr = mtod(m, struct atmllc *);
239                 bzero((void *)hdr, sizeof(struct atmllc) + 3);
240                 bcopy(NG_ATMLLC_HEADER, hdr->llchdr, 6);
241 #if 0
242                 if ((m->m_flags & M_HASFCS) != 0) {
243                         ATM_LLC_SETTYPE(hdr, NG_ATMLLC_TYPE_FDDI_FCS);
244                 } else {
245 #endif
246                         ATM_LLC_SETTYPE(hdr, NG_ATMLLC_TYPE_FDDI_NOFCS);
247 #if 0
248                 }
249 #endif
250                 outhook = priv->atm;
251         }
252
253         if (outhook == NULL) {
254                 NG_FREE_ITEM(item);
255                 return (0);
256         }
257
258         NG_FWD_NEW_DATA(error, item, outhook, m);
259         return (error);
260 }
261
262 static int
263 ng_atmllc_disconnect(hook_p hook)
264 {
265         node_p  node;
266         struct  ng_atmllc_priv *priv;
267
268         node = NG_HOOK_NODE(hook);
269         priv = NG_NODE_PRIVATE(node);
270
271         if (hook == priv->atm) {
272                 priv->atm = NULL;
273         } else if (hook == priv->ether) {
274                 priv->ether = NULL;
275         } else if (hook == priv->fddi) {
276                 priv->fddi = NULL;
277         }
278
279         if (NG_NODE_NUMHOOKS(node) == 0 && NG_NODE_IS_VALID(node)) {
280                 ng_rmnode_self(node);
281         }
282
283         return (0);
284 }