2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1982, 1989, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
66 #include <sys/socket.h>
67 #include <sys/sockio.h>
68 #include <sys/sysctl.h>
71 #include <net/netisr.h>
72 #include <net/route.h>
73 #include <net/if_llc.h>
74 #include <net/if_dl.h>
75 #include <net/if_types.h>
76 #include <netinet/if_ether.h>
78 #include <sys/socketvar.h>
81 #include <netat/at_pat.h>
83 extern struct ifqueue atalkintrq
;
88 #include <net/bridge.h>
91 /* #include "vlan.h" */
93 #include <net/if_vlan_var.h>
94 #endif /* NVLAN > 0 */
97 u_char etherbroadcastaddr
[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
99 #define IFP2AC(IFP) ((struct arpcom *)IFP)
102 struct dl_es_at_entry
111 * Temp static for protocol registration XXX
114 #define MAX_EN_COUNT 30
116 static struct dl_es_at_entry en_at_array
[MAX_EN_COUNT
];
119 * Process a received Ethernet packet;
120 * the packet is in the mbuf chain m without
121 * the ether header, which is provided separately.
124 at_ether_input(m
, frame_header
, ifp
, dl_tag
, sync_ok
)
132 register struct ether_header
*eh
= (struct ether_header
*) frame_header
;
133 register struct ifqueue
*inq
=0;
136 u_int16_t ptype
= -1;
137 unsigned char buf
[18];
140 register struct llc
*l
;
143 if ((ifp
->if_flags
& IFF_UP
) == 0) {
148 ifp
->if_lastchange
= time
;
150 if (eh
->ether_dhost
[0] & 1) {
151 if (bcmp((caddr_t
)etherbroadcastaddr
, (caddr_t
)eh
->ether_dhost
,
152 sizeof(etherbroadcastaddr
)) == 0)
153 m
->m_flags
|= M_BCAST
;
155 m
->m_flags
|= M_MCAST
;
157 if (m
->m_flags
& (M_BCAST
|M_MCAST
))
160 ether_type
= ntohs(eh
->ether_type
);
163 if (ether_type
== vlan_proto
) {
164 if (vlan_input(eh
, m
) < 0)
165 ifp
->if_data
.ifi_noproto
++;
168 #endif /* NVLAN > 0 */
170 if (ether_type
> ETHERMTU
)
174 l
= mtod(m
, struct llc
*);
176 switch (l
->llc_dsap
) {
179 /* Temporary hack: check for AppleTalk and AARP packets */
180 /* WARNING we're checking only on the "ether_type" (the 2 bytes
181 * of the SNAP header. This shouldn't be a big deal,
182 * AppleTalk pat_input is making sure we have the right packets
183 * because it needs to discrimante AARP from EtherTalk packets.
186 if (l
->llc_ssap
== LLC_SNAP_LSAP
&&
187 l
->llc_un
.type_snap
.control
== 0x03) {
189 #ifdef APPLETALK_DEBUG
190 printf("new_ether_input: SNAP Cntrol type=0x%x Src=%s\n",
191 l
->llc_un
.type_snap
.ether_type
,
192 ether_sprintf(buf
, &eh
->ether_shost
));
194 ether_sprintf(buf
, &eh
->ether_dhost
));
195 #endif /* APPLETALK_DEBUG */
197 if ((l
->llc_un
.type_snap
.ether_type
== 0x809B) ||
198 (l
->llc_un
.type_snap
.ether_type
== 0x80F3)) {
202 * note: for AppleTalk we need to pass the enet header of the
203 * packet up stack. To do so, we made sure in that the FULL packet
204 * is copied in the mbuf by the mace driver, and only the m_data and
205 * length have been shifted to make IP and the other guys happy.
208 m
->m_data
-= sizeof(*eh
);
209 m
->m_len
+= sizeof(*eh
);
210 m
->m_pkthdr
.len
+= sizeof(*eh
);
211 #ifdef APPLETALK_DEBUG
212 l
== (struct llc
*)(eh
+1);
213 if (l
->llc_un
.type_snap
.ether_type
== 0x80F3) {
214 kprintf("new_ether_input: RCV AppleTalk type=0x%x Src=%s\n",
215 l
->llc_un
.type_snap
.ether_type
,
216 ether_sprintf(buf
, &eh
->ether_shost
));
218 ether_sprintf(buf
, &eh
->ether_dhost
));
220 #endif /* APPLETALK_DEBUG */
221 schednetisr(NETISR_APPLETALK
);
257 ether_pre_output(ifp
, m0
, dst_netaddr
, route
, type
, edst
, dl_tag
)
260 struct sockaddr
*dst_netaddr
;
267 register struct mbuf
*m
= *m0
;
268 register struct rtentry
*rt
;
269 register struct ether_header
*eh
;
270 int off
, len
= m
->m_pkthdr
.len
;
271 int hlen
; /* link layer header lenght */
272 struct arpcom
*ac
= IFP2AC(ifp
);
276 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
279 hlen
= ETHER_HDR_LEN
;
282 * Tell ether_frameout it's ok to loop packet unless negated below.
284 m
->m_flags
|= M_LOOP
;
286 switch (dst_netaddr
->sa_family
) {
288 m
->m_flags
&= ~M_LOOP
;
289 eh
= (struct ether_header
*)dst_netaddr
->sa_data
;
290 (void)memcpy(edst
, eh
->ether_dhost
, 6);
291 *(u_short
*)type
= eh
->ether_type
;
297 eh
= (struct ether_header
*)dst_netaddr
->sa_data
;
298 bcopy((caddr_t
)eh
->ether_dhost
, (caddr_t
)edst
, 6);
300 *(u_short
*)type
= m
->m_pkthdr
.len
;
306 kprintf("%s%d: can't handle af%d\n", ifp
->if_name
, ifp
->if_unit
,
307 dst_netaddr
->sa_family
);
320 ether_prmod_ioctl(dl_tag
, ifp
, command
, data
)
326 struct ifaddr
*ifa
= (struct ifaddr
*) data
;
327 struct ifreq
*ifr
= (struct ifreq
*) data
;
329 boolean_t funnel_state
;
330 struct arpcom
*ac
= (struct arpcom
*) ifp
;
331 struct sockaddr_dl
*sdl
;
332 struct sockaddr_in
*sin
;
336 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
341 if ((ifp
->if_flags
& IFF_RUNNING
) == 0) {
342 ifp
->if_flags
|= IFF_UP
;
343 dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
) 0);
352 sa
= (struct sockaddr
*) & ifr
->ifr_data
;
353 bcopy(IFP2AC(ifp
)->ac_enaddr
,
354 (caddr_t
) sa
->sa_data
, ETHER_ADDR_LEN
);
360 * Set the interface MTU.
362 if (ifr
->ifr_mtu
> ETHERMTU
) {
365 ifp
->if_mtu
= ifr
->ifr_mtu
;
373 (void) thread_funnel_set(network_flock
, funnel_state
);
381 ether_attach_at(struct ifnet
*ifp
, u_long
*at_dl_tag
, u_long
*aarp_dl_tag
)
383 struct dlil_proto_reg_str reg
;
384 struct dlil_demux_desc desc
;
385 struct dlil_demux_desc desc2
;
386 u_short native
= 0; /* 802.2 frames use a length here */
392 first_empty
= MAX_EN_COUNT
;
393 for (i
=0; i
< MAX_EN_COUNT
; i
++) {
394 if (en_at_array
[i
].ifp
== 0)
397 if (en_at_array
[i
].ifp
== ifp
) {
398 en_at_array
[i
].ref_count
++;
399 *at_dl_tag
= *aarp_dl_tag
= en_at_array
[i
].dl_tag
;
404 if (first_empty
== MAX_EN_COUNT
)
407 TAILQ_INIT(®
.demux_desc_head
);
408 desc
.type
= DLIL_DESC_802_2_SNAP
;
409 desc
.variants
.desc_802_2_SNAP
.dsap
= LLC_SNAP_LSAP
;
410 desc
.variants
.desc_802_2_SNAP
.ssap
= LLC_SNAP_LSAP
;
411 desc
.variants
.desc_802_2_SNAP
.control_code
= 0x03;
412 desc
.variants
.desc_802_2_SNAP
.org
[0] = 0x08;
413 desc
.variants
.desc_802_2_SNAP
.org
[1] = 0x00;
414 desc
.variants
.desc_802_2_SNAP
.org
[2] = 0x07;
415 desc
.variants
.desc_802_2_SNAP
.protocol_type
= 0x809B;
416 desc
.native_type
= NULL
;
417 TAILQ_INSERT_TAIL(®
.demux_desc_head
, &desc
, next
);
418 reg
.interface_family
= ifp
->if_family
;
419 reg
.unit_number
= ifp
->if_unit
;
420 reg
.input
= at_ether_input
;
421 reg
.pre_output
= ether_pre_output
;
424 reg
.ioctl
= ether_prmod_ioctl
;
425 reg
.default_proto
= 0;
426 reg
.protocol_family
= PF_APPLETALK
;
429 desc2
.variants
.desc_802_2_SNAP
.protocol_type
= 0x80F3;
430 desc2
.variants
.desc_802_2_SNAP
.org
[0] = 0;
431 desc2
.variants
.desc_802_2_SNAP
.org
[1] = 0;
432 desc2
.variants
.desc_802_2_SNAP
.org
[2] = 0;
434 TAILQ_INSERT_TAIL(®
.demux_desc_head
, &desc2
, next
);
436 stat
= dlil_attach_protocol(®
, at_dl_tag
);
438 printf("WARNING: ether_attach_at can't attach at to interface\n");
442 *aarp_dl_tag
= *at_dl_tag
;
444 en_at_array
[first_empty
].ifp
= ifp
;
445 en_at_array
[first_empty
].dl_tag
= *at_dl_tag
;
446 en_at_array
[first_empty
].ref_count
= 1;
448 } /* ether_attach_at */
452 ether_detach_at(struct ifnet
*ifp
)
456 for (i
=0; i
< MAX_EN_COUNT
; i
++) {
457 if (en_at_array
[i
].ifp
== ifp
)
461 if (i
< MAX_EN_COUNT
) {
462 if (en_at_array
[i
].ref_count
> 1)
463 en_at_array
[i
].ref_count
--;
465 if (en_at_array
[i
].ref_count
== 1) {
466 dlil_detach_protocol(en_at_array
[i
].dl_tag
);
467 en_at_array
[i
].ifp
= 0;