]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/ether_inet_pr_module.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / bsd / net / ether_inet_pr_module.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1982, 1989, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 */
61
62
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/malloc.h>
68 #include <sys/mbuf.h>
69 #include <sys/socket.h>
70 #include <sys/sockio.h>
71 #include <sys/sysctl.h>
72 #include <kern/lock.h>
73
74 #include <net/if.h>
75 #include <net/route.h>
76 #include <net/if_llc.h>
77 #include <net/if_dl.h>
78 #include <net/if_types.h>
79 #include <net/kpi_protocol.h>
80
81 #include <netinet/in.h>
82 #include <netinet/in_var.h>
83 #include <netinet/if_ether.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/in_arp.h>
87
88 #include <sys/socketvar.h>
89
90 #include <net/dlil.h>
91
92 #if LLC && CCITT
93 extern struct ifqueue pkintrq;
94 #endif
95
96 #if BRIDGE
97 #include <net/bridge.h>
98 #endif
99
100 /* #include "vlan.h" */
101 #if NVLAN > 0
102 #include <net/if_vlan_var.h>
103 #endif /* NVLAN > 0 */
104
105 /* Local function declerations */
106 int ether_attach_inet(struct ifnet *ifp, u_long proto_family);
107 int ether_detach_inet(struct ifnet *ifp, u_long proto_family);
108
109 extern void * kdp_get_interface(void);
110 extern void ipintr(void);
111 extern void arp_input(struct mbuf* m);
112
113 static void
114 inet_ether_arp_input(
115 struct mbuf *m)
116 {
117 struct ether_arp *ea;
118 struct sockaddr_dl sender_hw;
119 struct sockaddr_in sender_ip;
120 struct sockaddr_in target_ip;
121
122 if (mbuf_len(m) < sizeof(*ea) &&
123 mbuf_pullup(&m, sizeof(*ea)) != 0)
124 return;
125
126 ea = mbuf_data(m);
127
128 /* Verify this is an ethernet/ip arp and address lengths are correct */
129 if (ntohs(ea->arp_hrd) != ARPHRD_ETHER ||
130 ntohs(ea->arp_pro) != ETHERTYPE_IP ||
131 ea->arp_pln != sizeof(struct in_addr) ||
132 ea->arp_hln != ETHER_ADDR_LEN) {
133 mbuf_free(m);
134 return;
135 }
136
137 /* Verify the sender is not broadcast */
138 if (bcmp(ea->arp_sha, etherbroadcastaddr, ETHER_ADDR_LEN) == 0) {
139 mbuf_free(m);
140 return;
141 }
142
143 bzero(&sender_ip, sizeof(sender_ip));
144 sender_ip.sin_len = sizeof(sender_ip);
145 sender_ip.sin_family = AF_INET;
146 sender_ip.sin_addr = *(struct in_addr*)ea->arp_spa;
147 target_ip = sender_ip;
148 target_ip.sin_addr = *(struct in_addr*)ea->arp_tpa;
149
150 bzero(&sender_hw, sizeof(sender_hw));
151 sender_hw.sdl_len = sizeof(sender_hw);
152 sender_hw.sdl_family = AF_LINK;
153 sender_hw.sdl_type = IFT_ETHER;
154 sender_hw.sdl_alen = ETHER_ADDR_LEN;
155 bcopy(ea->arp_sha, LLADDR(&sender_hw), ETHER_ADDR_LEN);
156
157 arp_ip_handle_input(mbuf_pkthdr_rcvif(m), ntohs(ea->arp_op), &sender_hw, &sender_ip, &target_ip);
158 mbuf_free(m);
159 }
160
161 /*
162 * Process a received Ethernet packet;
163 * the packet is in the mbuf chain m without
164 * the ether header, which is provided separately.
165 */
166 static errno_t
167 inet_ether_input(
168 __unused ifnet_t ifp,
169 __unused protocol_family_t protocol_family,
170 mbuf_t m,
171 char *frame_header)
172 {
173 register struct ether_header *eh = (struct ether_header *) frame_header;
174 u_short ether_type;
175
176 ether_type = ntohs(eh->ether_type);
177
178 switch (ether_type) {
179
180 case ETHERTYPE_IP:
181 proto_input(PF_INET, m);
182 break;
183
184 case ETHERTYPE_ARP: {
185 inet_ether_arp_input(m);
186 }
187 break;
188
189 default: {
190 return ENOENT;
191 }
192 }
193
194 return 0;
195 }
196
197 static errno_t
198 inet_ether_pre_output(
199 ifnet_t ifp,
200 __unused protocol_family_t protocol_family,
201 mbuf_t *m0,
202 const struct sockaddr *dst_netaddr,
203 void* route,
204 char *type,
205 char *edst)
206 {
207 register struct mbuf *m = *m0;
208 register struct ether_header *eh;
209 errno_t result = 0;
210
211
212 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
213 return ENETDOWN;
214
215 /*
216 * Tell ether_frameout it's ok to loop packet unless negated below.
217 */
218 m->m_flags |= M_LOOP;
219
220 switch (dst_netaddr->sa_family) {
221
222 case AF_INET: {
223 struct sockaddr_dl ll_dest;
224 result = arp_lookup_ip(ifp, (const struct sockaddr_in*)dst_netaddr,
225 &ll_dest, sizeof(ll_dest), (route_t)route, *m0);
226 if (result == 0) {
227 bcopy(LLADDR(&ll_dest), edst, ETHER_ADDR_LEN);
228 *(u_int16_t*)type = htons(ETHERTYPE_IP);
229 }
230 }
231 break;
232
233 case pseudo_AF_HDRCMPLT:
234 case AF_UNSPEC:
235 m->m_flags &= ~M_LOOP;
236 eh = (struct ether_header *)dst_netaddr->sa_data;
237 (void)memcpy(edst, eh->ether_dhost, 6);
238 *(u_short *)type = eh->ether_type;
239 break;
240
241 default:
242 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
243 dst_netaddr->sa_family);
244
245 result = EAFNOSUPPORT;
246 }
247
248 return result;
249 }
250
251 static errno_t
252 ether_inet_resolve_multi(
253 ifnet_t ifp,
254 const struct sockaddr *proto_addr,
255 struct sockaddr_dl *out_ll,
256 size_t ll_len)
257 {
258 static const size_t minsize = offsetof(struct sockaddr_dl, sdl_data[0]) + ETHER_ADDR_LEN;
259 const struct sockaddr_in *sin = (const struct sockaddr_in*)proto_addr;
260
261 if (proto_addr->sa_family != AF_INET)
262 return EAFNOSUPPORT;
263
264 if (proto_addr->sa_len < sizeof(struct sockaddr_in))
265 return EINVAL;
266
267 if (ll_len < minsize)
268 return EMSGSIZE;
269
270 bzero(out_ll, minsize);
271 out_ll->sdl_len = minsize;
272 out_ll->sdl_family = AF_LINK;
273 out_ll->sdl_index = ifp->if_index;
274 out_ll->sdl_type = IFT_ETHER;
275 out_ll->sdl_nlen = 0;
276 out_ll->sdl_alen = ETHER_ADDR_LEN;
277 out_ll->sdl_slen = 0;
278 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, LLADDR(out_ll));
279
280 return 0;
281 }
282
283
284 static errno_t
285 ether_inet_prmod_ioctl(
286 ifnet_t ifp,
287 __unused protocol_family_t protocol_family,
288 u_int32_t command,
289 void* data)
290 {
291 ifaddr_t ifa = data;
292 struct ifreq *ifr = data;
293 int error = 0;
294
295
296 switch (command) {
297 case SIOCSIFADDR:
298 if ((ifnet_flags(ifp) & IFF_RUNNING) == 0) {
299 ifnet_set_flags(ifp, IFF_UP, IFF_UP);
300 ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
301 }
302
303 switch (ifaddr_address_family(ifa)) {
304
305 case AF_INET:
306
307 inet_arp_init_ifaddr(ifp, ifa);
308 /*
309 * Register new IP and MAC addresses with the kernel debugger
310 * if the interface is the same as was registered by IOKernelDebugger. If
311 * no interface was registered, fall back and just match against en0 interface.
312 */
313 if ((kdp_get_interface() != 0 && kdp_get_interface() == ifp->if_softc)
314 || (kdp_get_interface() == 0 && ifp->if_unit == 0))
315 kdp_set_ip_and_mac_addresses(&(IA_SIN(ifa)->sin_addr), ifnet_lladdr(ifp));
316
317 break;
318
319 default:
320 break;
321 }
322
323 break;
324
325 case SIOCGIFADDR:
326 ifnet_lladdr_copy_bytes(ifp, ifr->ifr_addr.sa_data, ETHER_ADDR_LEN);
327 break;
328
329 case SIOCSIFMTU:
330 /*
331 * IOKit IONetworkFamily will set the right MTU according to the driver
332 */
333
334 return (0);
335
336 default:
337 return EOPNOTSUPP;
338 }
339
340 return (error);
341 }
342
343 static void
344 ether_inet_event(
345 ifnet_t ifp,
346 __unused protocol_family_t protocol,
347 const struct kev_msg *event)
348 {
349 ifaddr_t *addresses;
350
351 if (event->vendor_code != KEV_VENDOR_APPLE ||
352 event->kev_class != KEV_NETWORK_CLASS ||
353 event->kev_subclass != KEV_DL_SUBCLASS ||
354 event->event_code != KEV_DL_LINK_ADDRESS_CHANGED) {
355 return;
356 }
357
358 if (ifnet_get_address_list_family(ifp, &addresses, AF_INET) == 0) {
359 int i;
360
361 for (i = 0; addresses[i] != NULL; i++) {
362 inet_arp_init_ifaddr(ifp, addresses[i]);
363 }
364
365 ifnet_free_address_list(addresses);
366 }
367 }
368
369 static errno_t
370 ether_inet_arp(
371 ifnet_t ifp,
372 u_short arpop,
373 const struct sockaddr_dl* sender_hw,
374 const struct sockaddr* sender_proto,
375 const struct sockaddr_dl* target_hw,
376 const struct sockaddr* target_proto)
377 {
378 mbuf_t m;
379 errno_t result;
380 struct ether_header *eh;
381 struct ether_arp *ea;
382 const struct sockaddr_in* sender_ip = (const struct sockaddr_in*)sender_proto;
383 const struct sockaddr_in* target_ip = (const struct sockaddr_in*)target_proto;
384 char *datap;
385
386 if (target_ip == NULL)
387 return EINVAL;
388
389 if ((sender_ip && sender_ip->sin_family != AF_INET) ||
390 (target_ip && target_ip->sin_family != AF_INET))
391 return EAFNOSUPPORT;
392
393 result = mbuf_gethdr(MBUF_WAITOK, MBUF_TYPE_DATA, &m);
394 if (result != 0)
395 return result;
396
397 mbuf_setlen(m, sizeof(*ea));
398 mbuf_pkthdr_setlen(m, sizeof(*ea));
399
400 /* Move the data pointer in the mbuf to the end, aligned to 4 bytes */
401 datap = mbuf_datastart(m);
402 datap += mbuf_trailingspace(m);
403 datap -= (((u_long)datap) & 0x3);
404 mbuf_setdata(m, datap, sizeof(*ea));
405 ea = mbuf_data(m);
406
407 /* Prepend the ethernet header, we will send the raw frame */
408 mbuf_prepend(&m, sizeof(*eh), MBUF_WAITOK);
409 eh = mbuf_data(m);
410 eh->ether_type = htons(ETHERTYPE_ARP);
411
412 /* Fill out the arp header */
413 ea->arp_pro = htons(ETHERTYPE_IP);
414 ea->arp_hln = sizeof(ea->arp_sha);
415 ea->arp_pln = sizeof(ea->arp_spa);
416 ea->arp_hrd = htons(ARPHRD_ETHER);
417 ea->arp_op = htons(arpop);
418
419 /* Sender Hardware */
420 if (sender_hw != NULL) {
421 bcopy(CONST_LLADDR(sender_hw), ea->arp_sha, sizeof(ea->arp_sha));
422 }
423 else {
424 ifnet_lladdr_copy_bytes(ifp, ea->arp_sha, ETHER_ADDR_LEN);
425 }
426 ifnet_lladdr_copy_bytes(ifp, eh->ether_shost, sizeof(eh->ether_shost));
427
428 /* Sender IP */
429 if (sender_ip != NULL) {
430 bcopy(&sender_ip->sin_addr, ea->arp_spa, sizeof(ea->arp_spa));
431 }
432 else {
433 struct ifaddr *ifa;
434
435 /* Look for an IP address to use as our source */
436 ifnet_lock_shared(ifp);
437 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
438 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET)
439 break;
440 }
441 if (ifa) {
442 bcopy(&((struct sockaddr_in*)ifa->ifa_addr)->sin_addr, ea->arp_spa,
443 sizeof(ea->arp_spa));
444 }
445 ifnet_lock_done(ifp);
446
447 if (ifa == NULL) {
448 mbuf_free(m);
449 return ENXIO;
450 }
451 }
452
453 /* Target Hardware */
454 if (target_hw == 0) {
455 bzero(ea->arp_tha, sizeof(ea->arp_tha));
456 bcopy(etherbroadcastaddr, eh->ether_dhost, sizeof(eh->ether_dhost));
457 }
458 else {
459 bcopy(CONST_LLADDR(target_hw), ea->arp_tha, sizeof(ea->arp_tha));
460 bcopy(CONST_LLADDR(target_hw), eh->ether_dhost, sizeof(eh->ether_dhost));
461 }
462
463 /* Target IP */
464 bcopy(&target_ip->sin_addr, ea->arp_tpa, sizeof(ea->arp_tpa));
465
466 ifnet_output_raw(ifp, PF_INET, m);
467
468 return 0;
469 }
470
471 int
472 ether_attach_inet(
473 struct ifnet *ifp,
474 __unused u_long proto_family)
475 {
476 struct ifnet_attach_proto_param proto;
477 struct ifnet_demux_desc demux[2];
478 u_short en_native=htons(ETHERTYPE_IP);
479 u_short arp_native=htons(ETHERTYPE_ARP);
480 errno_t error;
481
482 bzero(&demux[0], sizeof(demux));
483 demux[0].type = DLIL_DESC_ETYPE2;
484 demux[0].data = &en_native;
485 demux[0].datalen = sizeof(en_native);
486 demux[1].type = DLIL_DESC_ETYPE2;
487 demux[1].data = &arp_native;
488 demux[1].datalen = sizeof(arp_native);
489
490 bzero(&proto, sizeof(proto));
491 proto.demux_list = demux;
492 proto.demux_count = sizeof(demux) / sizeof(demux[0]);
493 proto.input = inet_ether_input;
494 proto.pre_output = inet_ether_pre_output;
495 proto.ioctl = ether_inet_prmod_ioctl;
496 proto.event = ether_inet_event;
497 proto.resolve = ether_inet_resolve_multi;
498 proto.send_arp = ether_inet_arp;
499
500 error = ifnet_attach_protocol(ifp, proto_family, &proto);
501 if (error && error != EEXIST) {
502 printf("WARNING: ether_attach_inet can't attach ip to %s%d\n",
503 ifp->if_name, ifp->if_unit);
504 }
505 return error;
506 }
507
508 int
509 ether_detach_inet(
510 struct ifnet *ifp,
511 u_long proto_family)
512 {
513 int stat;
514
515 stat = dlil_detach_protocol(ifp, proto_family);
516
517 return stat;
518 }
519