]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * The contents of this file constitute Original Code as defined in and | |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
11 | * | |
12 | * This Original Code and all software distributed under the License are | |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* | |
23 | * Copyright (c) 1982, 1989, 1993 | |
24 | * The Regents of the University of California. All rights reserved. | |
25 | * | |
26 | * Redistribution and use in source and binary forms, with or without | |
27 | * modification, are permitted provided that the following conditions | |
28 | * are met: | |
29 | * 1. Redistributions of source code must retain the above copyright | |
30 | * notice, this list of conditions and the following disclaimer. | |
31 | * 2. Redistributions in binary form must reproduce the above copyright | |
32 | * notice, this list of conditions and the following disclaimer in the | |
33 | * documentation and/or other materials provided with the distribution. | |
34 | * 3. All advertising materials mentioning features or use of this software | |
35 | * must display the following acknowledgement: | |
36 | * This product includes software developed by the University of | |
37 | * California, Berkeley and its contributors. | |
38 | * 4. Neither the name of the University nor the names of its contributors | |
39 | * may be used to endorse or promote products derived from this software | |
40 | * without specific prior written permission. | |
41 | * | |
42 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
43 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
44 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
45 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
46 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
48 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
50 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
51 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
52 | * SUCH DAMAGE. | |
53 | * | |
54 | */ | |
55 | ||
56 | ||
57 | ||
58 | #include <sys/param.h> | |
59 | #include <sys/systm.h> | |
60 | #include <sys/kernel.h> | |
61 | #include <sys/malloc.h> | |
62 | #include <sys/mbuf.h> | |
63 | #include <sys/socket.h> | |
64 | #include <sys/sockio.h> | |
65 | #include <sys/sysctl.h> | |
66 | ||
67 | #include <net/if.h> | |
68 | #include <net/netisr.h> | |
69 | #include <net/route.h> | |
70 | #include <net/if_llc.h> | |
71 | #include <net/if_dl.h> | |
72 | #include <net/if_types.h> | |
1c79356b A |
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 | static | |
94 | u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; | |
95 | ||
96 | #define IFP2AC(IFP) ((struct arpcom *)IFP) | |
97 | ||
98 | ||
99 | struct dl_es_at_entry | |
100 | { | |
101 | struct ifnet *ifp; | |
102 | u_long dl_tag; | |
103 | int ref_count; | |
104 | }; | |
105 | ||
106 | ||
107 | /* | |
108 | * Temp static for protocol registration XXX | |
109 | */ | |
110 | ||
111 | #define MAX_EN_COUNT 30 | |
112 | ||
113 | static struct dl_es_at_entry en_at_array[MAX_EN_COUNT]; | |
114 | ||
115 | /* | |
116 | * Process a received Ethernet packet; | |
117 | * the packet is in the mbuf chain m without | |
118 | * the ether header, which is provided separately. | |
119 | */ | |
120 | int | |
121 | at_ether_input(m, frame_header, ifp, dl_tag, sync_ok) | |
122 | struct mbuf *m; | |
123 | char *frame_header; | |
124 | struct ifnet *ifp; | |
125 | u_long dl_tag; | |
126 | int sync_ok; | |
127 | ||
128 | { | |
129 | register struct ether_header *eh = (struct ether_header *) frame_header; | |
130 | register struct ifqueue *inq=0; | |
131 | u_short ether_type; | |
132 | int s; | |
133 | u_int16_t ptype = -1; | |
134 | unsigned char buf[18]; | |
135 | ||
136 | #if NETAT | |
137 | register struct llc *l; | |
138 | #endif | |
139 | ||
140 | if ((ifp->if_flags & IFF_UP) == 0) { | |
141 | m_freem(m); | |
142 | return EJUSTRETURN; | |
143 | } | |
144 | ||
145 | ifp->if_lastchange = time; | |
146 | ||
147 | if (eh->ether_dhost[0] & 1) { | |
148 | if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, | |
149 | sizeof(etherbroadcastaddr)) == 0) | |
150 | m->m_flags |= M_BCAST; | |
151 | else | |
152 | m->m_flags |= M_MCAST; | |
153 | } | |
154 | if (m->m_flags & (M_BCAST|M_MCAST)) | |
155 | ifp->if_imcasts++; | |
156 | ||
157 | ether_type = ntohs(eh->ether_type); | |
158 | ||
159 | #if NVLAN > 0 | |
160 | if (ether_type == vlan_proto) { | |
161 | if (vlan_input(eh, m) < 0) | |
162 | ifp->if_data.ifi_noproto++; | |
163 | return EJUSTRETURN; | |
164 | } | |
165 | #endif /* NVLAN > 0 */ | |
166 | ||
167 | if (ether_type > ETHERMTU) | |
168 | return ENOENT; | |
169 | ||
170 | #if NETAT | |
171 | l = mtod(m, struct llc *); | |
172 | ||
173 | switch (l->llc_dsap) { | |
174 | case LLC_SNAP_LSAP: | |
175 | ||
176 | /* Temporary hack: check for AppleTalk and AARP packets */ | |
177 | /* WARNING we're checking only on the "ether_type" (the 2 bytes | |
178 | * of the SNAP header. This shouldn't be a big deal, | |
179 | * AppleTalk pat_input is making sure we have the right packets | |
180 | * because it needs to discrimante AARP from EtherTalk packets. | |
181 | */ | |
182 | ||
183 | if (l->llc_ssap == LLC_SNAP_LSAP && | |
184 | l->llc_un.type_snap.control == 0x03) { | |
185 | ||
186 | #ifdef APPLETALK_DEBUG | |
187 | printf("new_ether_input: SNAP Cntrol type=0x%x Src=%s\n", | |
188 | l->llc_un.type_snap.ether_type, | |
189 | ether_sprintf(buf, &eh->ether_shost)); | |
190 | printf(" Dst=%s\n", | |
191 | ether_sprintf(buf, &eh->ether_dhost)); | |
192 | #endif /* APPLETALK_DEBUG */ | |
193 | ||
194 | if ((l->llc_un.type_snap.ether_type == 0x809B) || | |
195 | (l->llc_un.type_snap.ether_type == 0x80F3)) { | |
196 | ||
197 | ||
198 | /* | |
199 | * note: for AppleTalk we need to pass the enet header of the | |
200 | * packet up stack. To do so, we made sure in that the FULL packet | |
201 | * is copied in the mbuf by the mace driver, and only the m_data and | |
202 | * length have been shifted to make IP and the other guys happy. | |
203 | */ | |
204 | ||
205 | m->m_data -= sizeof(*eh); | |
206 | m->m_len += sizeof(*eh); | |
207 | m->m_pkthdr.len += sizeof(*eh); | |
208 | #ifdef APPLETALK_DEBUG | |
209 | l == (struct llc *)(eh+1); | |
210 | if (l->llc_un.type_snap.ether_type == 0x80F3) { | |
211 | kprintf("new_ether_input: RCV AppleTalk type=0x%x Src=%s\n", | |
212 | l->llc_un.type_snap.ether_type, | |
213 | ether_sprintf(buf, &eh->ether_shost)); | |
214 | kprintf(" Dst=%s\n", | |
215 | ether_sprintf(buf, &eh->ether_dhost)); | |
216 | } | |
217 | #endif /* APPLETALK_DEBUG */ | |
218 | schednetisr(NETISR_APPLETALK); | |
219 | inq = &atalkintrq ; | |
220 | ||
221 | break; | |
222 | } | |
223 | } | |
224 | ||
225 | break; | |
226 | ||
227 | ||
228 | default: | |
229 | return ENOENT; | |
230 | } | |
231 | ||
232 | ||
233 | if (inq == 0) | |
234 | return ENOENT; | |
235 | ||
236 | s = splimp(); | |
237 | if (IF_QFULL(inq)) { | |
238 | IF_DROP(inq); | |
239 | m_freem(m); | |
240 | splx(s); | |
241 | return EJUSTRETURN; | |
242 | } else | |
243 | IF_ENQUEUE(inq, m); | |
244 | splx(s); | |
245 | return 0; | |
246 | #else | |
247 | return ENOENT; | |
248 | #endif /* NETAT */ | |
249 | } | |
250 | ||
251 | ||
252 | ||
253 | int | |
254 | ether_pre_output(ifp, m0, dst_netaddr, route, type, edst, dl_tag ) | |
255 | struct ifnet *ifp; | |
256 | struct mbuf **m0; | |
257 | struct sockaddr *dst_netaddr; | |
258 | caddr_t route; | |
259 | char *type; | |
260 | char *edst; | |
261 | u_long dl_tag; | |
262 | { | |
263 | int s; | |
264 | register struct mbuf *m = *m0; | |
265 | register struct rtentry *rt; | |
266 | register struct ether_header *eh; | |
267 | int off, len = m->m_pkthdr.len; | |
268 | int hlen; /* link layer header lenght */ | |
269 | struct arpcom *ac = IFP2AC(ifp); | |
270 | ||
271 | ||
272 | ||
273 | if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) | |
274 | return ENETDOWN; | |
275 | ||
276 | hlen = ETHER_HDR_LEN; | |
277 | ||
278 | /* | |
279 | * Tell ether_frameout it's ok to loop packet unless negated below. | |
280 | */ | |
281 | m->m_flags |= M_LOOP; | |
282 | ||
283 | switch (dst_netaddr->sa_family) { | |
284 | case AF_UNSPEC: | |
285 | m->m_flags &= ~M_LOOP; | |
286 | eh = (struct ether_header *)dst_netaddr->sa_data; | |
287 | (void)memcpy(edst, eh->ether_dhost, 6); | |
288 | *(u_short *)type = eh->ether_type; | |
289 | break; | |
290 | ||
291 | ||
292 | case AF_APPLETALK: | |
293 | { | |
294 | eh = (struct ether_header *)dst_netaddr->sa_data; | |
295 | bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, 6); | |
296 | ||
297 | *(u_short *)type = m->m_pkthdr.len; | |
298 | } | |
299 | break; | |
300 | ||
301 | ||
302 | default: | |
303 | kprintf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, | |
304 | dst_netaddr->sa_family); | |
305 | ||
306 | return EAFNOSUPPORT; | |
307 | } | |
308 | ||
309 | return (0); | |
310 | } | |
311 | ||
312 | ||
313 | ||
314 | ||
315 | ||
316 | int | |
317 | ether_prmod_ioctl(dl_tag, ifp, command, data) | |
318 | u_long dl_tag; | |
319 | struct ifnet *ifp; | |
320 | int command; | |
321 | caddr_t data; | |
322 | { | |
323 | struct ifaddr *ifa = (struct ifaddr *) data; | |
324 | struct ifreq *ifr = (struct ifreq *) data; | |
325 | int error = 0; | |
326 | boolean_t funnel_state; | |
327 | struct arpcom *ac = (struct arpcom *) ifp; | |
328 | struct sockaddr_dl *sdl; | |
329 | struct sockaddr_in *sin; | |
330 | u_char *e_addr; | |
331 | ||
332 | ||
333 | funnel_state = thread_funnel_set(network_flock, TRUE); | |
334 | ||
335 | switch (command) { | |
336 | ||
337 | case SIOCSIFADDR: | |
338 | if ((ifp->if_flags & IFF_RUNNING) == 0) { | |
339 | ifp->if_flags |= IFF_UP; | |
340 | dlil_ioctl(0, ifp, SIOCSIFFLAGS, (caddr_t) 0); | |
341 | } | |
342 | ||
343 | break; | |
344 | ||
345 | case SIOCGIFADDR: | |
346 | { | |
347 | struct sockaddr *sa; | |
348 | ||
349 | sa = (struct sockaddr *) & ifr->ifr_data; | |
350 | bcopy(IFP2AC(ifp)->ac_enaddr, | |
351 | (caddr_t) sa->sa_data, ETHER_ADDR_LEN); | |
352 | } | |
353 | break; | |
354 | ||
355 | case SIOCSIFMTU: | |
356 | /* | |
357 | * Set the interface MTU. | |
358 | */ | |
359 | if (ifr->ifr_mtu > ETHERMTU) { | |
360 | error = EINVAL; | |
361 | } else { | |
362 | ifp->if_mtu = ifr->ifr_mtu; | |
363 | } | |
364 | break; | |
365 | ||
366 | default: | |
367 | return EOPNOTSUPP; | |
368 | } | |
369 | ||
370 | (void) thread_funnel_set(network_flock, funnel_state); | |
371 | ||
372 | return (error); | |
373 | } | |
374 | ||
375 | ||
376 | ||
377 | void | |
378 | ether_attach_at(struct ifnet *ifp, u_long *at_dl_tag, u_long *aarp_dl_tag) | |
379 | { | |
380 | struct dlil_proto_reg_str reg; | |
381 | struct dlil_demux_desc desc; | |
382 | struct dlil_demux_desc desc2; | |
383 | u_short native = 0; /* 802.2 frames use a length here */ | |
384 | int stat; | |
385 | int first_empty; | |
386 | int i; | |
387 | ||
388 | ||
389 | first_empty = MAX_EN_COUNT; | |
390 | for (i=0; i < MAX_EN_COUNT; i++) { | |
391 | if (en_at_array[i].ifp == 0) | |
392 | first_empty = i; | |
393 | ||
394 | if (en_at_array[i].ifp == ifp) { | |
395 | en_at_array[i].ref_count++; | |
396 | *at_dl_tag = *aarp_dl_tag = en_at_array[i].dl_tag; | |
397 | return; | |
398 | } | |
399 | } | |
400 | ||
401 | if (first_empty == MAX_EN_COUNT) | |
402 | return; | |
403 | ||
404 | TAILQ_INIT(®.demux_desc_head); | |
405 | desc.type = DLIL_DESC_802_2_SNAP; | |
406 | desc.variants.desc_802_2_SNAP.dsap = LLC_SNAP_LSAP; | |
407 | desc.variants.desc_802_2_SNAP.ssap = LLC_SNAP_LSAP; | |
408 | desc.variants.desc_802_2_SNAP.control_code = 0x03; | |
409 | desc.variants.desc_802_2_SNAP.org[0] = 0x08; | |
410 | desc.variants.desc_802_2_SNAP.org[1] = 0x00; | |
411 | desc.variants.desc_802_2_SNAP.org[2] = 0x07; | |
412 | desc.variants.desc_802_2_SNAP.protocol_type = 0x809B; | |
7b1edb79 | 413 | desc.native_type = NULL; |
1c79356b A |
414 | TAILQ_INSERT_TAIL(®.demux_desc_head, &desc, next); |
415 | reg.interface_family = ifp->if_family; | |
416 | reg.unit_number = ifp->if_unit; | |
417 | reg.input = at_ether_input; | |
418 | reg.pre_output = ether_pre_output; | |
419 | reg.event = 0; | |
420 | reg.offer = 0; | |
421 | reg.ioctl = ether_prmod_ioctl; | |
422 | reg.default_proto = 0; | |
423 | reg.protocol_family = PF_APPLETALK; | |
424 | ||
425 | desc2 = desc; | |
426 | desc2.variants.desc_802_2_SNAP.protocol_type = 0x80F3; | |
427 | desc2.variants.desc_802_2_SNAP.org[0] = 0; | |
428 | desc2.variants.desc_802_2_SNAP.org[1] = 0; | |
429 | desc2.variants.desc_802_2_SNAP.org[2] = 0; | |
430 | ||
431 | TAILQ_INSERT_TAIL(®.demux_desc_head, &desc2, next); | |
432 | ||
433 | stat = dlil_attach_protocol(®, at_dl_tag); | |
434 | if (stat) { | |
435 | printf("WARNING: ether_attach_at can't attach at to interface\n"); | |
436 | return; | |
437 | } | |
438 | ||
439 | *aarp_dl_tag = *at_dl_tag; | |
440 | ||
441 | en_at_array[first_empty].ifp = ifp; | |
442 | en_at_array[first_empty].dl_tag = *at_dl_tag; | |
443 | en_at_array[first_empty].ref_count = 1; | |
444 | ||
445 | } /* ether_attach_at */ | |
446 | ||
447 | ||
448 | void | |
449 | ether_detach_at(struct ifnet *ifp) | |
450 | { | |
451 | int i; | |
452 | ||
453 | for (i=0; i < MAX_EN_COUNT; i++) { | |
454 | if (en_at_array[i].ifp == ifp) | |
455 | break; | |
456 | } | |
457 | ||
458 | if (i < MAX_EN_COUNT) { | |
459 | if (en_at_array[i].ref_count > 1) | |
460 | en_at_array[i].ref_count--; | |
461 | else { | |
462 | if (en_at_array[i].ref_count == 1) { | |
463 | dlil_detach_protocol(en_at_array[i].dl_tag); | |
464 | en_at_array[i].ifp = 0; | |
465 | } | |
466 | } | |
467 | } | |
468 | } |