2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1989, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
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.
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
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
61 #include <sys/malloc.h>
63 #include <sys/socket.h>
64 #include <sys/sockio.h>
65 #include <sys/sysctl.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>
74 #include <netinet/in.h>
75 #include <netinet/in_var.h>
76 #include <netinet/if_ether.h>
77 #include <netinet/in_systm.h>
78 #include <netinet/ip.h>
80 #include <sys/socketvar.h>
85 extern struct ifqueue pkintrq
;
90 #include <net/bridge.h>
93 /* #include "vlan.h" */
95 #include <net/if_vlan_var.h>
96 #endif /* NVLAN > 0 */
98 static u_long lo_dlt
= 0;
99 static ivedonethis
= 0;
100 static u_char etherbroadcastaddr
[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
102 #define IFP2AC(IFP) ((struct arpcom *)IFP)
106 extern void * kdp_get_interface();
109 * Process a received Ethernet packet;
110 * the packet is in the mbuf chain m without
111 * the ether header, which is provided separately.
114 inet_ether_input(m
, frame_header
, ifp
, dl_tag
, sync_ok
)
122 register struct ether_header
*eh
= (struct ether_header
*) frame_header
;
123 register struct ifqueue
*inq
=0;
126 u_int16_t ptype
= -1;
127 unsigned char buf
[18];
129 #if ISO || LLC || NETAT
130 register struct llc
*l
;
133 if ((ifp
->if_flags
& IFF_UP
) == 0) {
138 ifp
->if_lastchange
= time
;
140 if (eh
->ether_dhost
[0] & 1) {
141 if (bcmp((caddr_t
)etherbroadcastaddr
, (caddr_t
)eh
->ether_dhost
,
142 sizeof(etherbroadcastaddr
)) == 0)
143 m
->m_flags
|= M_BCAST
;
145 m
->m_flags
|= M_MCAST
;
147 if (m
->m_flags
& (M_BCAST
|M_MCAST
))
150 ether_type
= ntohs(eh
->ether_type
);
153 if (ether_type
== vlan_proto
) {
154 if (vlan_input(eh
, m
) < 0)
155 ifp
->if_data
.ifi_noproto
++;
158 #endif /* NVLAN > 0 */
160 switch (ether_type
) {
163 if (ipflow_fastforward(m
))
165 ptype
= mtod(m
, struct ip
*)->ip_p
;
166 if ((sync_ok
== 0) ||
167 (ptype
!= IPPROTO_TCP
&& ptype
!= IPPROTO_UDP
)) {
168 schednetisr(NETISR_IP
);
175 schednetisr(NETISR_ARP
);
198 (ptype
== IPPROTO_TCP
|| ptype
== IPPROTO_UDP
)) {
199 extern void ipintr(void);
213 inet_ether_pre_output(ifp
, m0
, dst_netaddr
, route
, type
, edst
, dl_tag
)
216 struct sockaddr
*dst_netaddr
;
222 struct rtentry
*rt0
= (struct rtentry
*) route
;
224 register struct mbuf
*m
= *m0
;
225 register struct rtentry
*rt
;
226 register struct ether_header
*eh
;
227 int off
, len
= m
->m_pkthdr
.len
;
228 int hlen
; /* link layer header length */
229 struct arpcom
*ac
= IFP2AC(ifp
);
233 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
238 if ((rt
->rt_flags
& RTF_UP
) == 0) {
239 rt0
= rt
= rtalloc1(dst_netaddr
, 1, 0UL);
246 if (rt
->rt_flags
& RTF_GATEWAY
) {
247 if (rt
->rt_gwroute
== 0)
249 if (((rt
= rt
->rt_gwroute
)->rt_flags
& RTF_UP
) == 0) {
250 rtfree(rt
); rt
= rt0
;
251 lookup
: rt
->rt_gwroute
= rtalloc1(rt
->rt_gateway
, 1,
253 if ((rt
= rt
->rt_gwroute
) == 0)
254 return (EHOSTUNREACH
);
259 if (rt
->rt_flags
& RTF_REJECT
)
260 if (rt
->rt_rmx
.rmx_expire
== 0 ||
261 time_second
< rt
->rt_rmx
.rmx_expire
)
262 return (rt
== rt0
? EHOSTDOWN
: EHOSTUNREACH
);
265 hlen
= ETHER_HDR_LEN
;
268 * Tell ether_frameout it's ok to loop packet unless negated below.
270 m
->m_flags
|= M_LOOP
;
272 switch (dst_netaddr
->sa_family
) {
275 if (!arpresolve(ac
, rt
, m
, dst_netaddr
, edst
, rt0
))
276 return (EJUSTRETURN
); /* if not yet resolved */
277 off
= m
->m_pkthdr
.len
- m
->m_len
;
278 *(u_short
*)type
= htons(ETHERTYPE_IP
);
282 m
->m_flags
&= ~M_LOOP
;
283 eh
= (struct ether_header
*)dst_netaddr
->sa_data
;
284 (void)memcpy(edst
, eh
->ether_dhost
, 6);
285 *(u_short
*)type
= eh
->ether_type
;
289 kprintf("%s%d: can't handle af%d\n", ifp
->if_name
, ifp
->if_unit
,
290 dst_netaddr
->sa_family
);
300 ether_inet_prmod_ioctl(dl_tag
, ifp
, command
, data
)
306 struct ifaddr
*ifa
= (struct ifaddr
*) data
;
307 struct ifreq
*ifr
= (struct ifreq
*) data
;
308 struct rslvmulti_req
*rsreq
= (struct rslvmulti_req
*) data
;
310 boolean_t funnel_state
;
311 struct arpcom
*ac
= (struct arpcom
*) ifp
;
312 struct sockaddr_dl
*sdl
;
313 struct sockaddr_in
*sin
;
318 /* No tneeded at soo_ioctlis already funnelled */
319 funnel_state
= thread_funnel_set(network_flock
,TRUE
);
323 case SIOCRSLVMULTI
: {
324 switch(rsreq
->sa
->sa_family
) {
327 sin
= (struct sockaddr_in
*)rsreq
->sa
;
328 if (!IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
329 return EADDRNOTAVAIL
;
330 MALLOC(sdl
, struct sockaddr_dl
*, sizeof *sdl
, M_IFMADDR
,
332 sdl
->sdl_len
= sizeof *sdl
;
333 sdl
->sdl_family
= AF_LINK
;
334 sdl
->sdl_index
= ifp
->if_index
;
335 sdl
->sdl_type
= IFT_ETHER
;
337 sdl
->sdl_alen
= ETHER_ADDR_LEN
;
339 e_addr
= LLADDR(sdl
);
340 ETHER_MAP_IP_MULTICAST(&sin
->sin_addr
, e_addr
);
341 *rsreq
->llsa
= (struct sockaddr
*)sdl
;
346 * Well, the text isn't quite right, but it's the name
354 if ((ifp
->if_flags
& IFF_RUNNING
) == 0) {
355 ifp
->if_flags
|= IFF_UP
;
356 dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
) 0);
359 switch (ifa
->ifa_addr
->sa_family
) {
364 ifp
->if_init(ifp
->if_softc
); /* before arpwhohas */
366 arp_ifinit(IFP2AC(ifp
), ifa
);
368 * Register new IP and MAC addresses with the kernel debugger
369 * if the interface is the same as was registered by IOKernelDebugger. If
370 * no interface was registered, fall back and just match against en0 interface.
372 if ((kdp_get_interface() != 0 && kdp_get_interface() == ifp
->if_private
)
373 || (kdp_get_interface() == 0 && ifp
->if_unit
== 0))
374 kdp_set_ip_and_mac_addresses(&(IA_SIN(ifa
)->sin_addr
), &(IFP2AC(ifp
)->ac_enaddr
));
388 sa
= (struct sockaddr
*) & ifr
->ifr_data
;
389 bcopy(IFP2AC(ifp
)->ac_enaddr
,
390 (caddr_t
) sa
->sa_data
, ETHER_ADDR_LEN
);
396 * IOKit IONetworkFamily will set the right MTU according to the driver
405 //(void) thread_funnel_set(network_flock, FALSE);
415 ether_attach_inet(struct ifnet
*ifp
, u_long
*dl_tag
)
417 struct dlil_proto_reg_str reg
;
418 struct dlil_demux_desc desc
;
419 struct dlil_demux_desc desc2
;
420 u_short en_native
=ETHERTYPE_IP
;
421 u_short arp_native
=ETHERTYPE_ARP
;
426 stat
= dlil_find_dltag(ifp
->if_family
, ifp
->if_unit
, PF_INET
, dl_tag
);
430 TAILQ_INIT(®
.demux_desc_head
);
431 desc
.type
= DLIL_DESC_RAW
;
432 desc
.variants
.bitmask
.proto_id_length
= 0;
433 desc
.variants
.bitmask
.proto_id
= 0;
434 desc
.variants
.bitmask
.proto_id_mask
= 0;
435 desc
.native_type
= (char *) &en_native
;
436 TAILQ_INSERT_TAIL(®
.demux_desc_head
, &desc
, next
);
437 reg
.interface_family
= ifp
->if_family
;
438 reg
.unit_number
= ifp
->if_unit
;
439 reg
.input
= inet_ether_input
;
440 reg
.pre_output
= inet_ether_pre_output
;
443 reg
.ioctl
= ether_inet_prmod_ioctl
;
444 reg
.default_proto
= 1;
445 reg
.protocol_family
= PF_INET
;
448 desc2
.native_type
= (char *) &arp_native
;
449 TAILQ_INSERT_TAIL(®
.demux_desc_head
, &desc2
, next
);
451 stat
= dlil_attach_protocol(®
, dl_tag
);
453 printf("WARNING: ether_attach_inet can't attach ip to interface\n");
459 int ether_detach_inet(struct ifnet
*ifp
, u_long dl_tag
)
463 stat
= dlil_find_dltag(ifp
->if_family
, ifp
->if_unit
, PF_INET
, &dl_tag
);
465 stat
= dlil_detach_protocol(dl_tag
);
467 printf("WARNING: ether_detach_inet can't detach ip from interface\n");