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