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