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