]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/ether_inet_pr_module.c
xnu-344.23.tar.gz
[apple/xnu.git] / bsd / net / ether_inet_pr_module.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
de355530
A
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.
1c79356b 11 *
de355530
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
de355530
A
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.
1c79356b
A
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
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>
79
80#include <sys/socketvar.h>
81
82#include <net/dlil.h>
83
84#if LLC && CCITT
85extern struct ifqueue pkintrq;
86#endif
87
88
89#if BRIDGE
90#include <net/bridge.h>
91#endif
92
93/* #include "vlan.h" */
94#if NVLAN > 0
95#include <net/if_vlan_var.h>
96#endif /* NVLAN > 0 */
97
98static u_long lo_dlt = 0;
99static ivedonethis = 0;
100static u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
101
102#define IFP2AC(IFP) ((struct arpcom *)IFP)
103
104
105
106
107/*
108 * Process a received Ethernet packet;
109 * the packet is in the mbuf chain m without
110 * the ether header, which is provided separately.
111 */
112int
113inet_ether_input(m, frame_header, ifp, dl_tag, sync_ok)
114 struct mbuf *m;
115 char *frame_header;
116 struct ifnet *ifp;
117 u_long dl_tag;
118 int sync_ok;
119
120{
121 register struct ether_header *eh = (struct ether_header *) frame_header;
122 register struct ifqueue *inq=0;
123 u_short ether_type;
124 int s;
125 u_int16_t ptype = -1;
126 unsigned char buf[18];
127
128#if ISO || LLC || NETAT
129 register struct llc *l;
130#endif
131
132 if ((ifp->if_flags & IFF_UP) == 0) {
133 m_freem(m);
134 return EJUSTRETURN;
135 }
136
137 ifp->if_lastchange = time;
138
139 if (eh->ether_dhost[0] & 1) {
140 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
141 sizeof(etherbroadcastaddr)) == 0)
142 m->m_flags |= M_BCAST;
143 else
144 m->m_flags |= M_MCAST;
145 }
146 if (m->m_flags & (M_BCAST|M_MCAST))
147 ifp->if_imcasts++;
148
149 ether_type = ntohs(eh->ether_type);
150
151#if NVLAN > 0
152 if (ether_type == vlan_proto) {
153 if (vlan_input(eh, m) < 0)
154 ifp->if_data.ifi_noproto++;
155 return EJUSTRETURN;
156 }
157#endif /* NVLAN > 0 */
158
159 switch (ether_type) {
160
161 case ETHERTYPE_IP:
162 if (ipflow_fastforward(m))
163 return EJUSTRETURN;
164 ptype = mtod(m, struct ip *)->ip_p;
165 if ((sync_ok == 0) ||
166 (ptype != IPPROTO_TCP && ptype != IPPROTO_UDP)) {
167 schednetisr(NETISR_IP);
168 }
169
170 inq = &ipintrq;
171 break;
172
173 case ETHERTYPE_ARP:
174 schednetisr(NETISR_ARP);
175 inq = &arpintrq;
176 break;
177
178 default: {
179 return ENOENT;
180 }
181 }
182
183 if (inq == 0)
184 return ENOENT;
185
186 s = splimp();
187 if (IF_QFULL(inq)) {
188 IF_DROP(inq);
189 m_freem(m);
190 splx(s);
191 return EJUSTRETURN;
192 } else
193 IF_ENQUEUE(inq, m);
194 splx(s);
195
196 if ((sync_ok) &&
197 (ptype == IPPROTO_TCP || ptype == IPPROTO_UDP)) {
198 extern void ipintr(void);
199
200 s = splnet();
201 ipintr();
202 splx(s);
203 }
204
205 return 0;
206}
207
208
209
210
211int
212inet_ether_pre_output(ifp, m0, dst_netaddr, route, type, edst, dl_tag )
213 struct ifnet *ifp;
214 struct mbuf **m0;
215 struct sockaddr *dst_netaddr;
216 caddr_t route;
217 char *type;
218 char *edst;
219 u_long dl_tag;
220{
221 struct rtentry *rt0 = (struct rtentry *) route;
222 int s;
223 register struct mbuf *m = *m0;
224 register struct rtentry *rt;
225 register struct ether_header *eh;
226 int off, len = m->m_pkthdr.len;
227 int hlen; /* link layer header lenght */
228 struct arpcom *ac = IFP2AC(ifp);
229
230
231
232 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
233 return ENETDOWN;
234
235 rt = rt0;
236 if (rt) {
237 if ((rt->rt_flags & RTF_UP) == 0) {
238 rt0 = rt = rtalloc1(dst_netaddr, 1, 0UL);
239 if (rt0)
9bccf70c 240 rtunref(rt);
1c79356b
A
241 else
242 return EHOSTUNREACH;
243 }
244
245 if (rt->rt_flags & RTF_GATEWAY) {
246 if (rt->rt_gwroute == 0)
247 goto lookup;
248 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
249 rtfree(rt); rt = rt0;
250 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
251 0UL);
252 if ((rt = rt->rt_gwroute) == 0)
253 return (EHOSTUNREACH);
254 }
255 }
256
257
258 if (rt->rt_flags & RTF_REJECT)
259 if (rt->rt_rmx.rmx_expire == 0 ||
260 time_second < rt->rt_rmx.rmx_expire)
261 return (rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
262 }
263
264 hlen = ETHER_HDR_LEN;
265
266 /*
267 * Tell ether_frameout it's ok to loop packet unless negated below.
268 */
269 m->m_flags |= M_LOOP;
270
271 switch (dst_netaddr->sa_family) {
272
273 case AF_INET:
274 if (!arpresolve(ac, rt, m, dst_netaddr, edst, rt0))
275 return (EJUSTRETURN); /* if not yet resolved */
276 off = m->m_pkthdr.len - m->m_len;
277 *(u_short *)type = htons(ETHERTYPE_IP);
278 break;
279
280 case AF_UNSPEC:
281 m->m_flags &= ~M_LOOP;
282 eh = (struct ether_header *)dst_netaddr->sa_data;
283 (void)memcpy(edst, eh->ether_dhost, 6);
284 *(u_short *)type = eh->ether_type;
285 break;
286
287 default:
288 kprintf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
289 dst_netaddr->sa_family);
290
291 return EAFNOSUPPORT;
292 }
293
294 return (0);
295}
296
297
298int
299ether_inet_prmod_ioctl(dl_tag, ifp, command, data)
300 u_long dl_tag;
301 struct ifnet *ifp;
302 int command;
303 caddr_t data;
304{
305 struct ifaddr *ifa = (struct ifaddr *) data;
306 struct ifreq *ifr = (struct ifreq *) data;
307 struct rslvmulti_req *rsreq = (struct rslvmulti_req *) data;
308 int error = 0;
309 boolean_t funnel_state;
310 struct arpcom *ac = (struct arpcom *) ifp;
311 struct sockaddr_dl *sdl;
312 struct sockaddr_in *sin;
313 u_char *e_addr;
314
315
316#if 0
317 /* No tneeded at soo_ioctlis already funnelled */
318 funnel_state = thread_funnel_set(network_flock,TRUE);
319#endif
320
321 switch (command) {
322 case SIOCRSLVMULTI: {
323 switch(rsreq->sa->sa_family) {
324
325 case AF_INET:
326 sin = (struct sockaddr_in *)rsreq->sa;
327 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
328 return EADDRNOTAVAIL;
329 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
330 M_WAITOK);
331 sdl->sdl_len = sizeof *sdl;
332 sdl->sdl_family = AF_LINK;
333 sdl->sdl_index = ifp->if_index;
334 sdl->sdl_type = IFT_ETHER;
335 sdl->sdl_nlen = 0;
336 sdl->sdl_alen = ETHER_ADDR_LEN;
337 sdl->sdl_slen = 0;
338 e_addr = LLADDR(sdl);
339 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
340 *rsreq->llsa = (struct sockaddr *)sdl;
341 return EJUSTRETURN;
342
343 default:
344 /*
345 * Well, the text isn't quite right, but it's the name
346 * that counts...
347 */
348 return EAFNOSUPPORT;
349 }
350
351 }
352 case SIOCSIFADDR:
353 if ((ifp->if_flags & IFF_RUNNING) == 0) {
354 ifp->if_flags |= IFF_UP;
355 dlil_ioctl(0, ifp, SIOCSIFFLAGS, (caddr_t) 0);
356 }
357
358 switch (ifa->ifa_addr->sa_family) {
359
360 case AF_INET:
361
362 if (ifp->if_init)
363 ifp->if_init(ifp->if_softc); /* before arpwhohas */
364
365 //
366 // See if another station has *our* IP address.
367 // i.e.: There is an address conflict! If a
368 // conflict exists, a message is sent to the
369 // console.
370 //
371 if (IA_SIN(ifa)->sin_addr.s_addr != 0)
372 {
373 /* don't bother for 0.0.0.0 */
374 ac->ac_ipaddr = IA_SIN(ifa)->sin_addr;
375 arpwhohas(ac, &IA_SIN(ifa)->sin_addr);
376 }
377
378 arp_ifinit(IFP2AC(ifp), ifa);
379
9bccf70c
A
380 /*
381 * Register new IP and MAC addresses with the kernel debugger
382 * for the en0 interface.
383 */
384 if (ifp->if_unit == 0)
385 kdp_set_ip_and_mac_addresses(&(IA_SIN(ifa)->sin_addr), &(IFP2AC(ifp)->ac_enaddr));
386
1c79356b
A
387 break;
388
389 default:
390 break;
391 }
392
393 break;
394
395 case SIOCGIFADDR:
396 {
397 struct sockaddr *sa;
398
399 sa = (struct sockaddr *) & ifr->ifr_data;
400 bcopy(IFP2AC(ifp)->ac_enaddr,
401 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
402 }
403 break;
404
405 case SIOCSIFMTU:
406 /*
9bccf70c 407 * IOKit IONetworkFamily will set the right MTU according to the driver
1c79356b 408 */
9bccf70c
A
409
410 return (0);
1c79356b
A
411
412 default:
413 return EOPNOTSUPP;
414 }
415
416 //(void) thread_funnel_set(network_flock, FALSE);
417
418 return (error);
419}
420
421
422
423
424
425u_long
426ether_attach_inet(struct ifnet *ifp)
427{
428 struct dlil_proto_reg_str reg;
429 struct dlil_demux_desc desc;
430 struct dlil_demux_desc desc2;
431 u_long ip_dl_tag=0;
432 u_short en_native=ETHERTYPE_IP;
433 u_short arp_native=ETHERTYPE_ARP;
434 int stat;
435 int i;
436
437
438 stat = dlil_find_dltag(ifp->if_family, ifp->if_unit, PF_INET, &ip_dl_tag);
439 if (stat == 0)
440 return ip_dl_tag;
441
442 TAILQ_INIT(&reg.demux_desc_head);
443 desc.type = DLIL_DESC_RAW;
444 desc.variants.bitmask.proto_id_length = 0;
445 desc.variants.bitmask.proto_id = 0;
446 desc.variants.bitmask.proto_id_mask = 0;
447 desc.native_type = (char *) &en_native;
448 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc, next);
449 reg.interface_family = ifp->if_family;
450 reg.unit_number = ifp->if_unit;
451 reg.input = inet_ether_input;
452 reg.pre_output = inet_ether_pre_output;
453 reg.event = 0;
454 reg.offer = 0;
455 reg.ioctl = ether_inet_prmod_ioctl;
456 reg.default_proto = 1;
457 reg.protocol_family = PF_INET;
458
459 desc2 = desc;
460 desc2.native_type = (char *) &arp_native;
461 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc2, next);
462
463 stat = dlil_attach_protocol(&reg, &ip_dl_tag);
464 if (stat) {
465 printf("WARNING: ether_attach_inet can't attach ip to interface\n");
466 return stat;
467 }
1c79356b
A
468 return ip_dl_tag;
469}
0b4e3aa0
A
470
471int ether_detach_inet(struct ifnet *ifp)
472{
473 u_long ip_dl_tag = 0;
474 int stat;
475
476 stat = dlil_find_dltag(ifp->if_family, ifp->if_unit, PF_INET, &ip_dl_tag);
477 if (stat == 0) {
478 stat = dlil_detach_protocol(ip_dl_tag);
479 if (stat) {
480 printf("WARNING: ether_detach_inet can't detach ip from interface\n");
481 }
482 }
483 return stat;
484}
485