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