]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 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 | * |
e5568f75 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, | |
e5568f75 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> | |
91447636 | 66 | #include <kern/lock.h> |
1c79356b A |
67 | |
68 | #include <net/if.h> | |
1c79356b A |
69 | #include <net/route.h> |
70 | #include <net/if_llc.h> | |
71 | #include <net/if_dl.h> | |
72 | #include <net/if_types.h> | |
91447636 | 73 | #include <net/kpi_protocol.h> |
1c79356b A |
74 | |
75 | #include <netinet/in.h> | |
76 | #include <netinet/in_var.h> | |
77 | #include <netinet/if_ether.h> | |
78 | #include <netinet/in_systm.h> | |
79 | #include <netinet/ip.h> | |
91447636 | 80 | #include <netinet/in_arp.h> |
1c79356b A |
81 | |
82 | #include <sys/socketvar.h> | |
83 | ||
84 | #include <net/dlil.h> | |
85 | ||
86 | #if LLC && CCITT | |
87 | extern struct ifqueue pkintrq; | |
88 | #endif | |
89 | ||
1c79356b A |
90 | #if BRIDGE |
91 | #include <net/bridge.h> | |
92 | #endif | |
93 | ||
94 | /* #include "vlan.h" */ | |
95 | #if NVLAN > 0 | |
96 | #include <net/if_vlan_var.h> | |
97 | #endif /* NVLAN > 0 */ | |
98 | ||
91447636 A |
99 | /* Local function declerations */ |
100 | int ether_attach_inet(struct ifnet *ifp, u_long proto_family); | |
101 | int ether_detach_inet(struct ifnet *ifp, u_long proto_family); | |
1c79356b | 102 | |
91447636 A |
103 | extern void * kdp_get_interface(void); |
104 | extern void ipintr(void); | |
105 | extern void arp_input(struct mbuf* m); | |
1c79356b | 106 | |
91447636 A |
107 | static void |
108 | inet_ether_arp_input( | |
109 | struct mbuf *m) | |
110 | { | |
111 | struct ether_arp *ea; | |
112 | struct sockaddr_dl sender_hw; | |
113 | struct sockaddr_in sender_ip; | |
114 | struct sockaddr_in target_ip; | |
115 | ||
116 | if (mbuf_len(m) < sizeof(*ea) && | |
117 | mbuf_pullup(&m, sizeof(*ea)) != 0) | |
118 | return; | |
119 | ||
120 | ea = mbuf_data(m); | |
121 | ||
122 | /* Verify this is an ethernet/ip arp and address lengths are correct */ | |
123 | if (ntohs(ea->arp_hrd) != ARPHRD_ETHER || | |
124 | ntohs(ea->arp_pro) != ETHERTYPE_IP || | |
125 | ea->arp_pln != sizeof(struct in_addr) || | |
126 | ea->arp_hln != ETHER_ADDR_LEN) { | |
127 | mbuf_free(m); | |
128 | return; | |
129 | } | |
130 | ||
131 | /* Verify the sender is not broadcast or multicast */ | |
132 | if ((ea->arp_sha[0] & 0x01) != 0) { | |
133 | mbuf_free(m); | |
134 | return; | |
135 | } | |
136 | ||
137 | bzero(&sender_ip, sizeof(sender_ip)); | |
138 | sender_ip.sin_len = sizeof(sender_ip); | |
139 | sender_ip.sin_family = AF_INET; | |
140 | sender_ip.sin_addr = *(struct in_addr*)ea->arp_spa; | |
141 | target_ip = sender_ip; | |
142 | target_ip.sin_addr = *(struct in_addr*)ea->arp_tpa; | |
143 | ||
144 | bzero(&sender_hw, sizeof(sender_hw)); | |
145 | sender_hw.sdl_len = sizeof(sender_hw); | |
146 | sender_hw.sdl_family = AF_LINK; | |
147 | sender_hw.sdl_type = IFT_ETHER; | |
148 | sender_hw.sdl_alen = ETHER_ADDR_LEN; | |
149 | bcopy(ea->arp_sha, LLADDR(&sender_hw), ETHER_ADDR_LEN); | |
150 | ||
151 | arp_ip_handle_input(mbuf_pkthdr_rcvif(m), ntohs(ea->arp_op), &sender_hw, &sender_ip, &target_ip); | |
152 | mbuf_free(m); | |
153 | } | |
1c79356b A |
154 | |
155 | /* | |
156 | * Process a received Ethernet packet; | |
157 | * the packet is in the mbuf chain m without | |
158 | * the ether header, which is provided separately. | |
159 | */ | |
91447636 A |
160 | static errno_t |
161 | inet_ether_input( | |
162 | __unused ifnet_t ifp, | |
163 | __unused protocol_family_t protocol_family, | |
164 | mbuf_t m, | |
165 | char *frame_header) | |
1c79356b A |
166 | { |
167 | register struct ether_header *eh = (struct ether_header *) frame_header; | |
1c79356b | 168 | u_short ether_type; |
1c79356b A |
169 | |
170 | ether_type = ntohs(eh->ether_type); | |
171 | ||
1c79356b A |
172 | switch (ether_type) { |
173 | ||
91447636 A |
174 | case ETHERTYPE_IP: |
175 | proto_input(PF_INET, m); | |
176 | break; | |
177 | ||
178 | case ETHERTYPE_ARP: { | |
179 | inet_ether_arp_input(m); | |
180 | } | |
181 | break; | |
182 | ||
183 | default: { | |
184 | return ENOENT; | |
185 | } | |
1c79356b | 186 | } |
91447636 | 187 | |
1c79356b A |
188 | return 0; |
189 | } | |
190 | ||
91447636 A |
191 | static errno_t |
192 | inet_ether_pre_output( | |
193 | ifnet_t ifp, | |
194 | __unused protocol_family_t protocol_family, | |
195 | mbuf_t *m0, | |
196 | const struct sockaddr *dst_netaddr, | |
197 | void* route, | |
198 | char *type, | |
199 | char *edst) | |
1c79356b | 200 | { |
1c79356b | 201 | register struct mbuf *m = *m0; |
1c79356b | 202 | register struct ether_header *eh; |
91447636 | 203 | errno_t result = 0; |
1c79356b A |
204 | |
205 | ||
206 | if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) | |
91447636 | 207 | return ENETDOWN; |
1c79356b | 208 | |
1c79356b A |
209 | /* |
210 | * Tell ether_frameout it's ok to loop packet unless negated below. | |
211 | */ | |
212 | m->m_flags |= M_LOOP; | |
213 | ||
214 | switch (dst_netaddr->sa_family) { | |
91447636 A |
215 | |
216 | case AF_INET: { | |
217 | struct sockaddr_dl ll_dest; | |
218 | result = arp_lookup_ip(ifp, (const struct sockaddr_in*)dst_netaddr, | |
219 | &ll_dest, sizeof(ll_dest), (route_t)route, *m0); | |
220 | if (result == 0) { | |
221 | bcopy(LLADDR(&ll_dest), edst, ETHER_ADDR_LEN); | |
222 | *(u_int16_t*)type = htons(ETHERTYPE_IP); | |
223 | } | |
224 | } | |
225 | break; | |
226 | ||
227 | case pseudo_AF_HDRCMPLT: | |
228 | case AF_UNSPEC: | |
229 | m->m_flags &= ~M_LOOP; | |
230 | eh = (struct ether_header *)dst_netaddr->sa_data; | |
231 | (void)memcpy(edst, eh->ether_dhost, 6); | |
232 | *(u_short *)type = eh->ether_type; | |
233 | break; | |
234 | ||
235 | default: | |
236 | printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, | |
237 | dst_netaddr->sa_family); | |
238 | ||
239 | result = EAFNOSUPPORT; | |
240 | } | |
1c79356b | 241 | |
91447636 A |
242 | return result; |
243 | } | |
1c79356b | 244 | |
91447636 A |
245 | static errno_t |
246 | ether_inet_resolve_multi( | |
247 | ifnet_t ifp, | |
248 | const struct sockaddr *proto_addr, | |
249 | struct sockaddr_dl *out_ll, | |
250 | size_t ll_len) | |
251 | { | |
252 | static const size_t minsize = offsetof(struct sockaddr_dl, sdl_data[0]) + ETHER_ADDR_LEN; | |
253 | const struct sockaddr_in *sin = (const struct sockaddr_in*)proto_addr; | |
254 | ||
255 | if (proto_addr->sa_family != AF_INET) | |
256 | return EAFNOSUPPORT; | |
257 | ||
258 | if (proto_addr->sa_len < sizeof(struct sockaddr_in)) | |
259 | return EINVAL; | |
1c79356b | 260 | |
91447636 A |
261 | if (ll_len < minsize) |
262 | return EMSGSIZE; | |
263 | ||
264 | bzero(out_ll, minsize); | |
265 | out_ll->sdl_len = minsize; | |
266 | out_ll->sdl_family = AF_LINK; | |
267 | out_ll->sdl_index = ifp->if_index; | |
268 | out_ll->sdl_type = IFT_ETHER; | |
269 | out_ll->sdl_nlen = 0; | |
270 | out_ll->sdl_alen = ETHER_ADDR_LEN; | |
271 | out_ll->sdl_slen = 0; | |
272 | ETHER_MAP_IP_MULTICAST(&sin->sin_addr, LLADDR(out_ll)); | |
273 | ||
274 | return 0; | |
1c79356b A |
275 | } |
276 | ||
277 | ||
91447636 A |
278 | static errno_t |
279 | ether_inet_prmod_ioctl( | |
280 | ifnet_t ifp, | |
281 | __unused protocol_family_t protocol_family, | |
282 | u_int32_t command, | |
283 | void* data) | |
1c79356b | 284 | { |
91447636 A |
285 | ifaddr_t ifa = data; |
286 | struct ifreq *ifr = data; | |
1c79356b | 287 | int error = 0; |
1c79356b A |
288 | |
289 | ||
1c79356b | 290 | switch (command) { |
1c79356b | 291 | case SIOCSIFADDR: |
91447636 A |
292 | if ((ifnet_flags(ifp) & IFF_RUNNING) == 0) { |
293 | ifnet_set_flags(ifp, IFF_UP, IFF_UP); | |
294 | ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL); | |
295 | } | |
1c79356b | 296 | |
91447636 | 297 | switch (ifaddr_address_family(ifa)) { |
1c79356b A |
298 | |
299 | case AF_INET: | |
300 | ||
91447636 | 301 | inet_arp_init_ifaddr(ifp, ifa); |
9bccf70c A |
302 | /* |
303 | * Register new IP and MAC addresses with the kernel debugger | |
4a249263 A |
304 | * if the interface is the same as was registered by IOKernelDebugger. If |
305 | * no interface was registered, fall back and just match against en0 interface. | |
9bccf70c | 306 | */ |
91447636 | 307 | if ((kdp_get_interface() != 0 && kdp_get_interface() == ifp->if_softc) |
4a249263 | 308 | || (kdp_get_interface() == 0 && ifp->if_unit == 0)) |
91447636 | 309 | kdp_set_ip_and_mac_addresses(&(IA_SIN(ifa)->sin_addr), ifnet_lladdr(ifp)); |
9bccf70c | 310 | |
1c79356b A |
311 | break; |
312 | ||
313 | default: | |
314 | break; | |
315 | } | |
316 | ||
317 | break; | |
318 | ||
319 | case SIOCGIFADDR: | |
91447636 | 320 | ifnet_lladdr_copy_bytes(ifp, ifr->ifr_addr.sa_data, ETHER_ADDR_LEN); |
1c79356b A |
321 | break; |
322 | ||
323 | case SIOCSIFMTU: | |
324 | /* | |
9bccf70c | 325 | * IOKit IONetworkFamily will set the right MTU according to the driver |
1c79356b | 326 | */ |
9bccf70c A |
327 | |
328 | return (0); | |
1c79356b A |
329 | |
330 | default: | |
331 | return EOPNOTSUPP; | |
332 | } | |
333 | ||
1c79356b A |
334 | return (error); |
335 | } | |
336 | ||
91447636 A |
337 | static void |
338 | ether_inet_event( | |
339 | ifnet_t ifp, | |
340 | __unused protocol_family_t protocol, | |
341 | const struct kev_msg *event) | |
342 | { | |
343 | ifaddr_t *addresses; | |
344 | ||
345 | if (event->vendor_code != KEV_VENDOR_APPLE || | |
346 | event->kev_class != KEV_NETWORK_CLASS || | |
347 | event->kev_subclass != KEV_DL_SUBCLASS || | |
348 | event->event_code != KEV_DL_LINK_ADDRESS_CHANGED) { | |
349 | return; | |
350 | } | |
351 | ||
352 | if (ifnet_get_address_list_family(ifp, &addresses, AF_INET) == 0) { | |
353 | int i; | |
354 | ||
355 | for (i = 0; addresses[i] != NULL; i++) { | |
356 | inet_arp_init_ifaddr(ifp, addresses[i]); | |
357 | } | |
358 | ||
359 | ifnet_free_address_list(addresses); | |
360 | } | |
361 | } | |
1c79356b | 362 | |
91447636 A |
363 | static errno_t |
364 | ether_inet_arp( | |
365 | ifnet_t ifp, | |
366 | u_short arpop, | |
367 | const struct sockaddr_dl* sender_hw, | |
368 | const struct sockaddr* sender_proto, | |
369 | const struct sockaddr_dl* target_hw, | |
370 | const struct sockaddr* target_proto) | |
371 | { | |
372 | mbuf_t m; | |
373 | errno_t result; | |
374 | struct ether_header *eh; | |
375 | struct ether_arp *ea; | |
376 | const struct sockaddr_in* sender_ip = (const struct sockaddr_in*)sender_proto; | |
377 | const struct sockaddr_in* target_ip = (const struct sockaddr_in*)target_proto; | |
378 | char *datap; | |
379 | ||
380 | if (target_ip == NULL) | |
381 | return EINVAL; | |
382 | ||
383 | if ((sender_ip && sender_ip->sin_family != AF_INET) || | |
384 | (target_ip && target_ip->sin_family != AF_INET)) | |
385 | return EAFNOSUPPORT; | |
386 | ||
387 | result = mbuf_gethdr(MBUF_WAITOK, MBUF_TYPE_DATA, &m); | |
388 | if (result != 0) | |
389 | return result; | |
390 | ||
391 | mbuf_setlen(m, sizeof(*ea)); | |
392 | mbuf_pkthdr_setlen(m, sizeof(*ea)); | |
393 | ||
394 | /* Move the data pointer in the mbuf to the end, aligned to 4 bytes */ | |
395 | datap = mbuf_datastart(m); | |
396 | datap += mbuf_trailingspace(m); | |
397 | datap -= (((u_long)datap) & 0x3); | |
398 | mbuf_setdata(m, datap, sizeof(*ea)); | |
399 | ea = mbuf_data(m); | |
400 | ||
401 | /* Prepend the ethernet header, we will send the raw frame */ | |
402 | mbuf_prepend(&m, sizeof(*eh), MBUF_WAITOK); | |
403 | eh = mbuf_data(m); | |
404 | eh->ether_type = htons(ETHERTYPE_ARP); | |
405 | ||
406 | /* Fill out the arp header */ | |
407 | ea->arp_pro = htons(ETHERTYPE_IP); | |
408 | ea->arp_hln = sizeof(ea->arp_sha); | |
409 | ea->arp_pln = sizeof(ea->arp_spa); | |
410 | ea->arp_hrd = htons(ARPHRD_ETHER); | |
411 | ea->arp_op = htons(arpop); | |
412 | ||
413 | /* Sender Hardware */ | |
414 | if (sender_hw != NULL) { | |
415 | bcopy(CONST_LLADDR(sender_hw), ea->arp_sha, sizeof(ea->arp_sha)); | |
416 | } | |
417 | else { | |
418 | ifnet_lladdr_copy_bytes(ifp, ea->arp_sha, ETHER_ADDR_LEN); | |
419 | } | |
420 | ifnet_lladdr_copy_bytes(ifp, eh->ether_shost, sizeof(eh->ether_shost)); | |
421 | ||
422 | /* Sender IP */ | |
423 | if (sender_ip != NULL) { | |
424 | bcopy(&sender_ip->sin_addr, ea->arp_spa, sizeof(ea->arp_spa)); | |
425 | } | |
426 | else { | |
427 | struct ifaddr *ifa; | |
428 | ||
429 | /* Look for an IP address to use as our source */ | |
430 | ifnet_lock_shared(ifp); | |
431 | TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { | |
432 | if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) | |
433 | break; | |
434 | } | |
435 | if (ifa) { | |
436 | bcopy(&((struct sockaddr_in*)ifa->ifa_addr)->sin_addr, ea->arp_spa, | |
437 | sizeof(ea->arp_spa)); | |
438 | } | |
439 | ifnet_lock_done(ifp); | |
440 | ||
441 | if (ifa == NULL) { | |
442 | mbuf_free(m); | |
443 | return ENXIO; | |
444 | } | |
445 | } | |
446 | ||
447 | /* Target Hardware */ | |
448 | if (target_hw == 0) { | |
449 | bzero(ea->arp_tha, sizeof(ea->arp_tha)); | |
450 | bcopy(etherbroadcastaddr, eh->ether_dhost, sizeof(eh->ether_dhost)); | |
451 | } | |
452 | else { | |
453 | bcopy(CONST_LLADDR(target_hw), ea->arp_tha, sizeof(ea->arp_tha)); | |
454 | bcopy(CONST_LLADDR(target_hw), eh->ether_dhost, sizeof(eh->ether_dhost)); | |
455 | } | |
456 | ||
457 | /* Target IP */ | |
458 | bcopy(&target_ip->sin_addr, ea->arp_tpa, sizeof(ea->arp_tpa)); | |
459 | ||
460 | ifnet_output_raw(ifp, PF_INET, m); | |
461 | ||
462 | return 0; | |
463 | } | |
1c79356b | 464 | |
55e303ae | 465 | int |
91447636 A |
466 | ether_attach_inet( |
467 | struct ifnet *ifp, | |
468 | __unused u_long proto_family) | |
1c79356b | 469 | { |
91447636 A |
470 | struct ifnet_attach_proto_param proto; |
471 | struct ifnet_demux_desc demux[2]; | |
472 | u_short en_native=htons(ETHERTYPE_IP); | |
473 | u_short arp_native=htons(ETHERTYPE_ARP); | |
474 | errno_t error; | |
475 | ||
476 | bzero(&demux[0], sizeof(demux)); | |
477 | demux[0].type = DLIL_DESC_ETYPE2; | |
478 | demux[0].data = &en_native; | |
479 | demux[0].datalen = sizeof(en_native); | |
480 | demux[1].type = DLIL_DESC_ETYPE2; | |
481 | demux[1].data = &arp_native; | |
482 | demux[1].datalen = sizeof(arp_native); | |
483 | ||
484 | bzero(&proto, sizeof(proto)); | |
485 | proto.demux_list = demux; | |
486 | proto.demux_count = sizeof(demux) / sizeof(demux[0]); | |
487 | proto.input = inet_ether_input; | |
488 | proto.pre_output = inet_ether_pre_output; | |
489 | proto.ioctl = ether_inet_prmod_ioctl; | |
490 | proto.event = ether_inet_event; | |
491 | proto.resolve = ether_inet_resolve_multi; | |
492 | proto.send_arp = ether_inet_arp; | |
493 | ||
494 | error = ifnet_attach_protocol(ifp, proto_family, &proto); | |
495 | if (error && error != EEXIST) { | |
496 | printf("WARNING: ether_attach_inet can't attach ip to %s%d\n", | |
497 | ifp->if_name, ifp->if_unit); | |
498 | } | |
499 | return error; | |
1c79356b | 500 | } |
0b4e3aa0 | 501 | |
91447636 A |
502 | int |
503 | ether_detach_inet( | |
504 | struct ifnet *ifp, | |
505 | u_long proto_family) | |
0b4e3aa0 | 506 | { |
0b4e3aa0 A |
507 | int stat; |
508 | ||
91447636 A |
509 | stat = dlil_detach_protocol(ifp, proto_family); |
510 | ||
0b4e3aa0 A |
511 | return stat; |
512 | } | |
513 |