]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/ether_at_pr_module.c
4dc1f3c4341e8f71d5e8e47d13a44b154c48492e
[apple/xnu.git] / bsd / net / ether_at_pr_module.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1982, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 *
55 */
56
57
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/malloc.h>
63 #include <sys/mbuf.h>
64 #include <sys/socket.h>
65 #include <sys/sockio.h>
66 #include <sys/sysctl.h>
67
68 #include <net/if.h>
69 #include <net/route.h>
70 #include <net/if_llc.h>
71 #include <net/if_dl.h>
72 #include <net/if_types.h>
73 #include <netinet/if_ether.h>
74
75 #include <sys/socketvar.h>
76
77 #include <net/dlil.h>
78 #include <netat/at_pat.h>
79 #if NETAT
80 extern struct ifqueue atalkintrq;
81 #endif
82
83
84 #if BRIDGE
85 #include <net/bridge.h>
86 #endif
87
88 /* #include "vlan.h" */
89 #if NVLAN > 0
90 #include <net/if_vlan_var.h>
91 #endif /* NVLAN > 0 */
92
93 struct dl_es_at_entry
94 {
95 struct ifnet *ifp;
96 int ref_count;
97 };
98
99 /* Local fuction declerations */
100 int at_ether_input(struct mbuf *m, char *frame_header, struct ifnet *ifp,
101 u_long protocol_family, int sync_ok);
102 int ether_pre_output(struct ifnet *ifp, u_long protocol_family, struct mbuf **m0,
103 const struct sockaddr *dst_netaddr, caddr_t route, char *type, char *edst);
104 int ether_prmod_ioctl(u_long protocol_family, struct ifnet *ifp, u_long command,
105 caddr_t data);
106 int ether_attach_at(struct ifnet *ifp);
107 void ether_detach_at(struct ifnet *ifp);
108
109
110 /*
111 * Temp static for protocol registration XXX
112 */
113
114 #define MAX_EN_COUNT 30
115
116 static struct dl_es_at_entry en_at_array[MAX_EN_COUNT];
117
118 /*
119 * Process a received Ethernet packet;
120 * the packet is in the mbuf chain m without
121 * the ether header, which is provided separately.
122 */
123 int
124 at_ether_input(
125 struct mbuf *m,
126 __unused char *frame_header,
127 __unused struct ifnet *ifp,
128 __unused u_long protocol_family,
129 __unused int sync_ok)
130
131 {
132 /*
133 * note: for AppleTalk we need to pass the enet header of the
134 * packet up stack. To do so, we made sure in that the FULL packet
135 * is copied in the mbuf by the mace driver, and only the m_data and
136 * length have been shifted to make IP and the other guys happy.
137 */
138
139 m->m_data -= sizeof(struct ether_header);
140 m->m_len += sizeof(struct ether_header);
141 m->m_pkthdr.len += sizeof(struct ether_header);
142 proto_input(PF_APPLETALK, m);
143
144 return 0;
145 }
146
147
148
149 int
150 ether_pre_output(
151 struct ifnet *ifp,
152 __unused u_long protocol_family,
153 struct mbuf **m0,
154 const struct sockaddr *dst_netaddr,
155 __unused caddr_t route,
156 char *type,
157 char *edst)
158 {
159 register struct mbuf *m = *m0;
160 register struct ether_header *eh;
161 int hlen; /* link layer header lenght */
162
163
164
165 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
166 return ENETDOWN;
167
168 hlen = ETHER_HDR_LEN;
169
170 /*
171 * Tell ether_frameout it's ok to loop packet unless negated below.
172 */
173 m->m_flags |= M_LOOP;
174
175 switch (dst_netaddr->sa_family) {
176 case AF_UNSPEC:
177 m->m_flags &= ~M_LOOP;
178 eh = (struct ether_header *)dst_netaddr->sa_data;
179 (void)memcpy(edst, eh->ether_dhost, 6);
180 *(u_short *)type = eh->ether_type;
181 break;
182
183
184 case AF_APPLETALK:
185 {
186 eh = (struct ether_header *)dst_netaddr->sa_data;
187 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, 6);
188
189 *(u_short *)type = m->m_pkthdr.len;
190 }
191 break;
192
193
194 default:
195 kprintf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
196 dst_netaddr->sa_family);
197
198 return EAFNOSUPPORT;
199 }
200
201 return (0);
202 }
203
204
205
206
207
208 int
209 ether_prmod_ioctl(
210 __unused u_long protocol_family,
211 struct ifnet *ifp,
212 u_long command,
213 caddr_t data)
214 {
215 struct ifreq *ifr = (struct ifreq *) data;
216 int error = 0;
217
218 switch (command) {
219
220 case SIOCSIFADDR:
221 if ((ifp->if_flags & IFF_RUNNING) == 0) {
222 ifnet_set_flags(ifp, IFF_UP, IFF_UP);
223 dlil_ioctl(0, ifp, SIOCSIFFLAGS, (caddr_t) 0);
224 }
225
226 break;
227
228 case SIOCGIFADDR:
229 ifnet_lladdr_copy_bytes(ifp, ifr->ifr_addr.sa_data, ETHER_ADDR_LEN);
230 break;
231
232 case SIOCSIFMTU:
233 /*
234 * Set the interface MTU.
235 */
236 if (ifr->ifr_mtu > ETHERMTU) {
237 error = EINVAL;
238 } else {
239 ifp->if_mtu = ifr->ifr_mtu;
240 }
241 break;
242
243 default:
244 return EOPNOTSUPP;
245 }
246
247
248 return (error);
249 }
250
251
252
253 int
254 ether_attach_at(
255 struct ifnet *ifp)
256 {
257 struct dlil_proto_reg_str reg;
258 struct dlil_demux_desc desc;
259 struct dlil_demux_desc desc2;
260 int stat;
261 int first_empty;
262 int i;
263 u_int8_t atalk_snap[5] = {0x08, 0x00, 0x07, 0x80, 0x9b};
264 u_int8_t aarp_snap[5] = {0x00, 0x00, 0x00, 0x80, 0xf3};
265
266 first_empty = MAX_EN_COUNT;
267 for (i=0; i < MAX_EN_COUNT; i++) {
268 if (en_at_array[i].ifp == 0)
269 first_empty = i;
270
271 if (en_at_array[i].ifp == ifp) {
272 en_at_array[i].ref_count++;
273 return 0;
274 }
275 }
276
277 if (first_empty == MAX_EN_COUNT)
278 return ENOMEM;
279
280 bzero(&reg, sizeof(reg));
281 bzero(&desc, sizeof(desc));
282 bzero(&desc2, sizeof(desc2));
283
284 TAILQ_INIT(&reg.demux_desc_head);
285 reg.interface_family = ifp->if_family;
286 reg.unit_number = ifp->if_unit;
287 reg.input = at_ether_input;
288 reg.pre_output = ether_pre_output;
289 reg.ioctl = ether_prmod_ioctl;
290 reg.protocol_family = PF_APPLETALK;
291
292 desc.type = DLIL_DESC_SNAP;
293 desc.native_type = atalk_snap;
294 desc.variants.native_type_length = sizeof(atalk_snap);
295 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc, next);
296
297 desc2.type = DLIL_DESC_SNAP;
298 desc2.native_type = aarp_snap;
299 desc2.variants.native_type_length = sizeof(aarp_snap);
300 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc2, next);
301
302 stat = dlil_attach_protocol(&reg);
303 if (stat) {
304 printf("WARNING: ether_attach_at can't attach at to interface\n");
305 return stat;
306 }
307
308 en_at_array[first_empty].ifp = ifp;
309 en_at_array[first_empty].ref_count = 1;
310
311 return 0;
312 } /* ether_attach_at */
313
314
315 void
316 ether_detach_at(struct ifnet *ifp)
317 {
318 int i;
319
320 for (i=0; i < MAX_EN_COUNT; i++) {
321 if (en_at_array[i].ifp == ifp)
322 break;
323 }
324
325 if (i < MAX_EN_COUNT) {
326 if (en_at_array[i].ref_count > 1)
327 en_at_array[i].ref_count--;
328 else {
329 if (en_at_array[i].ref_count == 1) {
330 dlil_detach_protocol(ifp, PF_APPLETALK);
331 en_at_array[i].ifp = 0;
332 }
333 }
334 }
335 }