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