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>
77 #include <netinet/in.h> /* For M_LOOP */
81 #include <netinet/in.h>
82 #include <netinet/in_var.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
89 #include <sys/socketvar.h>
95 extern struct ifqueue pkintrq
;
98 /* General stuff from if_ethersubr.c - may not need some of it */
100 #include <netat/at_pat.h>
102 extern struct ifqueue atalkintrq
;
107 #include <net/bridge.h>
110 /* #include "vlan.h" */
112 #include <net/if_vlan_var.h>
113 #endif /* NVLAN > 0 */
115 static u_long lo_dlt
= 0;
117 #define IFP2AC(IFP) ((struct arpcom *)IFP)
120 u_int16_t type
; /* Type of protocol stored in data */
121 struct if_proto
*proto
; /* Protocol structure */
122 u_long data
[2]; /* Protocol data */
125 #define ETHER_DESC_BLK_SIZE (10)
126 #define MAX_INTERFACES 50
129 * Statics for demux module
132 struct ether_desc_blk_str
{
135 struct en_desc
*block_ptr
;
139 static struct ether_desc_blk_str ether_desc_blk
[MAX_INTERFACES
];
142 /* from if_ethersubr.c */
143 int ether_resolvemulti
__P((struct ifnet
*, struct sockaddr
**,
147 * Release all descriptor entries owned by this dl_tag (there may be several).
148 * Setting the type to 0 releases the entry. Eventually we should compact-out
149 * the unused entries.
152 int ether_del_proto(struct if_proto
*proto
, u_long dl_tag
)
154 struct en_desc
* ed
= ether_desc_blk
[proto
->ifp
->family_cookie
].block_ptr
;
158 for (current
= ether_desc_blk
[proto
->ifp
->family_cookie
].n_max_used
;
159 current
> 0; current
--) {
160 if (ed
[current
- 1].proto
== proto
) {
162 ed
[current
- 1].type
= 0;
164 if (current
== ether_desc_blk
[proto
->ifp
->family_cookie
].n_max_used
) {
165 ether_desc_blk
[proto
->ifp
->family_cookie
].n_max_used
--;
176 ether_add_proto(struct ddesc_head_str
*desc_head
, struct if_proto
*proto
, u_long dl_tag
)
179 struct dlil_demux_desc
*desc
;
181 struct en_desc
*last
;
190 TAILQ_FOREACH(desc
, desc_head
, next
) {
191 switch (desc
->type
) {
192 /* These types are supported */
193 /* Top three are preferred */
194 case DLIL_DESC_ETYPE2
:
195 if (desc
->variants
.native_type_length
!= 2)
200 if (desc
->variants
.native_type_length
!= 3)
205 if (desc
->variants
.native_type_length
!= 5)
209 case DLIL_DESC_802_2
:
210 case DLIL_DESC_802_2_SNAP
:
214 if (desc
->variants
.bitmask
.proto_id_length
== 0)
216 /* else fall through, bitmask variant not supported */
219 ether_del_proto(proto
, dl_tag
);
224 ed
= ether_desc_blk
[proto
->ifp
->family_cookie
].block_ptr
;
226 /* Find a free entry */
227 for (i
= 0; i
< ether_desc_blk
[proto
->ifp
->family_cookie
].n_count
; i
++) {
228 if (ed
[i
].type
== 0) {
233 if (i
>= ether_desc_blk
[proto
->ifp
->family_cookie
].n_count
) {
234 u_long new_count
= ETHER_DESC_BLK_SIZE
+
235 ether_desc_blk
[proto
->ifp
->family_cookie
].n_count
;
236 tmp
= _MALLOC((new_count
* (sizeof(*ed
))), M_IFADDR
, M_WAITOK
);
239 * Remove any previous descriptors set in the call.
241 ether_del_proto(proto
, dl_tag
);
245 bzero(tmp
, new_count
* sizeof(*ed
));
246 bcopy(ether_desc_blk
[proto
->ifp
->family_cookie
].block_ptr
,
247 tmp
, ether_desc_blk
[proto
->ifp
->family_cookie
].n_count
* sizeof(*ed
));
248 FREE(ether_desc_blk
[proto
->ifp
->family_cookie
].block_ptr
, M_IFADDR
);
249 ether_desc_blk
[proto
->ifp
->family_cookie
].n_count
= new_count
;
250 ether_desc_blk
[proto
->ifp
->family_cookie
].block_ptr
= (struct en_desc
*)tmp
;
253 /* Bump n_max_used if appropriate */
254 if (i
+ 1 > ether_desc_blk
[proto
->ifp
->family_cookie
].n_max_used
) {
255 ether_desc_blk
[proto
->ifp
->family_cookie
].n_max_used
= i
+ 1;
262 switch (desc
->type
) {
264 /* 2 byte ethernet raw protocol type is at native_type */
265 /* protocol is not in network byte order */
266 ed
[i
].type
= DLIL_DESC_ETYPE2
;
267 ed
[i
].data
[0] = htons(*(u_int16_t
*)desc
->native_type
);
270 case DLIL_DESC_ETYPE2
:
271 /* 2 byte ethernet raw protocol type is at native_type */
272 /* prtocol must be in network byte order */
273 ed
[i
].type
= DLIL_DESC_ETYPE2
;
274 ed
[i
].data
[0] = *(u_int16_t
*)desc
->native_type
;
277 case DLIL_DESC_802_2
:
278 ed
[i
].type
= DLIL_DESC_SAP
;
279 ed
[i
].data
[0] = *(u_int32_t
*)&desc
->variants
.desc_802_2
;
280 ed
[i
].data
[0] &= htonl(0xFFFFFF00);
284 ed
[i
].type
= DLIL_DESC_SAP
;
285 bcopy(desc
->native_type
, &ed
[i
].data
[0], 3);
288 case DLIL_DESC_802_2_SNAP
:
289 ed
[i
].type
= DLIL_DESC_SNAP
;
290 desc
->variants
.desc_802_2_SNAP
.protocol_type
=
291 htons(desc
->variants
.desc_802_2_SNAP
.protocol_type
);
292 bcopy(&desc
->variants
.desc_802_2_SNAP
, &ed
[i
].data
[0], 8);
293 ed
[i
].data
[0] &= htonl(0x000000FF);
294 desc
->variants
.desc_802_2_SNAP
.protocol_type
=
295 ntohs(desc
->variants
.desc_802_2_SNAP
.protocol_type
);
298 case DLIL_DESC_SNAP
: {
299 u_int8_t
* pDest
= ((u_int8_t
*)&ed
[i
].data
[0]) + 3;
300 ed
[i
].type
= DLIL_DESC_SNAP
;
301 bcopy(desc
->native_type
, pDest
, 5);
318 int ether_demux(ifp
, m
, frame_header
, proto
)
322 struct if_proto
**proto
;
325 register struct ether_header
*eh
= (struct ether_header
*)frame_header
;
326 u_short ether_type
= eh
->ether_type
;
330 u_long max
= ether_desc_blk
[ifp
->family_cookie
].n_max_used
;
331 struct en_desc
*ed
= ether_desc_blk
[ifp
->family_cookie
].block_ptr
;
332 u_int32_t extProto1
= 0;
333 u_int32_t extProto2
= 0;
335 if (eh
->ether_dhost
[0] & 1) {
336 /* Check for broadcast */
337 if (*(u_int32_t
*)eh
->ether_dhost
== 0xFFFFFFFF &&
338 *(u_int16_t
*)(eh
->ether_dhost
+ sizeof(u_int32_t
)) == 0xFFFF)
339 m
->m_flags
|= M_BCAST
;
341 m
->m_flags
|= M_MCAST
;
344 * When the driver is put into promiscuous mode we may receive unicast
345 * frames that are not intended for our interfaces. They are filtered
346 * here to keep them from traveling further up the stack to code that
347 * is not expecting them or prepared to deal with them. In the near
348 * future, the filtering done here will be moved even further down the
349 * stack into the IONetworkingFamily, preventing even interface
350 * filter NKE's from receiving promiscuous packets. Please use BPF.
352 #define ETHER_CMP(x, y) ( ((u_int16_t *) x)[0] != ((u_int16_t *) y)[0] || \
353 ((u_int16_t *) x)[1] != ((u_int16_t *) y)[1] || \
354 ((u_int16_t *) x)[2] != ((u_int16_t *) y)[2] )
356 if (ETHER_CMP(eh
->ether_dhost
, ((struct arpcom
*) ifp
)->ac_enaddr
)) {
362 data
= mtod(m
, u_int8_t
*);
365 * Determine the packet's protocol type and stuff the protocol into
366 * longs for quick compares.
369 if (ntohs(ether_type
) <= 1500) {
370 extProto1
= *(u_int32_t
*)data
;
373 if ((extProto1
& htonl(0xFFFFFF00)) == htonl(0xAAAA0300)) {
375 type
= DLIL_DESC_SNAP
;
376 extProto2
= *(u_int32_t
*)(data
+ sizeof(u_int32_t
));
377 extProto1
&= htonl(0x000000FF);
379 type
= DLIL_DESC_SAP
;
380 extProto1
&= htonl(0xFFFFFF00);
383 type
= DLIL_DESC_ETYPE2
;
387 * Search through the connected protocols for a match.
391 case DLIL_DESC_ETYPE2
:
392 for (i
= 0; i
< max
; i
++) {
393 if ((ed
[i
].type
== type
) && (ed
[i
].data
[0] == ether_type
)) {
394 *proto
= ed
[i
].proto
;
401 for (i
= 0; i
< max
; i
++) {
402 if ((ed
[i
].type
== type
) && (ed
[i
].data
[0] == extProto1
)) {
403 *proto
= ed
[i
].proto
;
410 for (i
= 0; i
< max
; i
++) {
411 if ((ed
[i
].type
== type
) && (ed
[i
].data
[0] == extProto1
) &&
412 (ed
[i
].data
[1] == extProto2
)) {
413 *proto
= ed
[i
].proto
;
426 * Ethernet output routine.
427 * Encapsulate a packet of type family for the local net.
428 * Use trailer local net encapsulation if enough data in first
429 * packet leaves a multiple of 512 bytes of data in remainder.
430 * Assumes that ifp is actually pointer to arpcom structure.
433 ether_frameout(ifp
, m
, ndest
, edst
, ether_type
)
434 register struct ifnet
*ifp
;
436 struct sockaddr
*ndest
;
440 register struct ether_header
*eh
;
441 int hlen
; /* link layer header lenght */
442 struct arpcom
*ac
= IFP2AC(ifp
);
445 hlen
= ETHER_HDR_LEN
;
448 * If a simplex interface, and the packet is being sent to our
449 * Ethernet address or a broadcast address, loopback a copy.
450 * XXX To make a simplex device behave exactly like a duplex
451 * device, we should copy in the case of sending to our own
452 * ethernet address (thus letting the original actually appear
453 * on the wire). However, we don't do that here for security
454 * reasons and compatibility with the original behavior.
456 if ((ifp
->if_flags
& IFF_SIMPLEX
) &&
457 ((*m
)->m_flags
& M_LOOP
)) {
459 dlil_find_dltag(APPLE_IF_FAM_LOOPBACK
, 0, PF_INET
, &lo_dlt
);
462 if ((*m
)->m_flags
& M_BCAST
) {
463 struct mbuf
*n
= m_copy(*m
, 0, (int)M_COPYALL
);
465 dlil_output(lo_dlt
, n
, 0, ndest
, 0);
469 if (bcmp(edst
, ac
->ac_enaddr
, ETHER_ADDR_LEN
) == 0) {
470 dlil_output(lo_dlt
, *m
, 0, ndest
, 0);
479 * Add local net header. If no space in first mbuf,
482 M_PREPEND(*m
, sizeof (struct ether_header
), M_DONTWAIT
);
484 return (EJUSTRETURN
);
488 eh
= mtod(*m
, struct ether_header
*);
489 (void)memcpy(&eh
->ether_type
, ether_type
,
490 sizeof(eh
->ether_type
));
491 (void)memcpy(eh
->ether_dhost
, edst
, 6);
492 (void)memcpy(eh
->ether_shost
, ac
->ac_enaddr
,
493 sizeof(eh
->ether_shost
));
500 int ether_add_if(struct ifnet
*ifp
)
504 ifp
->if_framer
= ether_frameout
;
505 ifp
->if_demux
= ether_demux
;
507 ifp
->if_resolvemulti
= ether_resolvemulti
;
509 for (i
=0; i
< MAX_INTERFACES
; i
++)
510 if (ether_desc_blk
[i
].n_count
== 0)
513 if (i
== MAX_INTERFACES
)
516 ether_desc_blk
[i
].block_ptr
= _MALLOC(ETHER_DESC_BLK_SIZE
* sizeof(struct en_desc
),
518 if (ether_desc_blk
[i
].block_ptr
== 0)
521 ether_desc_blk
[i
].n_count
= ETHER_DESC_BLK_SIZE
;
522 bzero(ether_desc_blk
[i
].block_ptr
, ETHER_DESC_BLK_SIZE
* sizeof(struct en_desc
));
524 ifp
->family_cookie
= i
;
530 int ether_del_if(struct ifnet
*ifp
)
532 if ((ifp
->family_cookie
< MAX_INTERFACES
) &&
533 (ether_desc_blk
[ifp
->family_cookie
].n_count
))
535 FREE(ether_desc_blk
[ifp
->family_cookie
].block_ptr
, M_IFADDR
);
536 ether_desc_blk
[ifp
->family_cookie
].block_ptr
= NULL
;
537 ether_desc_blk
[ifp
->family_cookie
].n_count
= 0;
538 ether_desc_blk
[ifp
->family_cookie
].n_max_used
= 0;
546 int ether_init_if(struct ifnet
*ifp
)
548 register struct ifaddr
*ifa
;
549 register struct sockaddr_dl
*sdl
;
551 ifa
= ifnet_addrs
[ifp
->if_index
- 1];
553 printf("ether_ifattach: no lladdr!\n");
556 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
557 sdl
->sdl_type
= IFT_ETHER
;
558 sdl
->sdl_alen
= ifp
->if_addrlen
;
559 bcopy((IFP2AC(ifp
))->ac_enaddr
, LLADDR(sdl
), ifp
->if_addrlen
);
566 ether_ifmod_ioctl(ifp
, command
, data
)
571 struct rslvmulti_req
*rsreq
= (struct rslvmulti_req
*) data
;
573 struct sockaddr_dl
*sdl
;
574 struct sockaddr_in
*sin
;
580 switch(rsreq
->sa
->sa_family
) {
582 /* AppleTalk uses AF_UNSPEC for multicast registration.
583 * No mapping needed. Just check that it's a valid MC address.
585 e_addr
= &rsreq
->sa
->sa_data
[0];
586 if ((e_addr
[0] & 1) != 1)
587 return EADDRNOTAVAIL
;
594 * No mapping needed. Just check that it's a valid MC address.
596 sdl
= (struct sockaddr_dl
*)rsreq
->sa
;
597 e_addr
= LLADDR(sdl
);
598 if ((e_addr
[0] & 1) != 1)
599 return EADDRNOTAVAIL
;
613 extern int ether_attach_inet(struct ifnet
*ifp
, u_long
*dl_tag
);
614 extern int ether_detach_inet(struct ifnet
*ifp
, u_long dl_tag
);
615 extern int ether_attach_inet6(struct ifnet
*ifp
, u_long
*dl_tag
);
616 extern int ether_detach_inet6(struct ifnet
*ifp
, u_long dl_tag
);
617 int ether_family_init()
620 struct dlil_ifmod_reg_str ifmod_reg
;
621 struct dlil_protomod_reg_str enet_protoreg
;
623 /* ethernet family is built-in, called from bsd_init */
624 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
626 bzero(&ifmod_reg
, sizeof(ifmod_reg
));
627 ifmod_reg
.add_if
= ether_add_if
;
628 ifmod_reg
.del_if
= ether_del_if
;
629 ifmod_reg
.init_if
= ether_init_if
;
630 ifmod_reg
.add_proto
= ether_add_proto
;
631 ifmod_reg
.del_proto
= ether_del_proto
;
632 ifmod_reg
.ifmod_ioctl
= ether_ifmod_ioctl
;
633 ifmod_reg
.shutdown
= ether_shutdown
;
635 if (dlil_reg_if_modules(APPLE_IF_FAM_ETHERNET
, &ifmod_reg
)) {
636 printf("WARNING: ether_family_init -- Can't register if family modules\n");
640 for (i
=0; i
< MAX_INTERFACES
; i
++)
641 ether_desc_blk
[i
].n_count
= 0;
643 /* Register protocol registration functions */
645 bzero(&enet_protoreg
, sizeof(enet_protoreg
));
646 enet_protoreg
.attach_proto
= ether_attach_inet
;
647 enet_protoreg
.detach_proto
= ether_detach_inet
;
649 if ( error
= dlil_reg_proto_module(PF_INET
, APPLE_IF_FAM_ETHERNET
, &enet_protoreg
) != 0)
650 kprintf("dlil_reg_proto_module failed for AF_INET6 error=%d\n", error
);
653 enet_protoreg
.attach_proto
= ether_attach_inet6
;
654 enet_protoreg
.detach_proto
= ether_detach_inet6
;
656 if ( error
= dlil_reg_proto_module(PF_INET6
, APPLE_IF_FAM_ETHERNET
, &enet_protoreg
) != 0)
657 kprintf("dlil_reg_proto_module failed for AF_INET6 error=%d\n", error
);
659 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);