]>
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, 1986, 1988, 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 | * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 | |
9bccf70c | 55 | * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.11 2001/07/25 17:27:56 jlemon Exp $ |
1c79356b A |
56 | */ |
57 | ||
58 | /* | |
59 | * Ethernet address resolution protocol. | |
60 | * TODO: | |
61 | * add "inuse/lock" bit (or ref. count) along with valid bit | |
62 | */ | |
63 | ||
1c79356b A |
64 | #include <sys/param.h> |
65 | #include <sys/kernel.h> | |
9bccf70c | 66 | #include <sys/queue.h> |
1c79356b | 67 | #include <sys/sysctl.h> |
1c79356b A |
68 | #include <sys/systm.h> |
69 | #include <sys/mbuf.h> | |
70 | #include <sys/malloc.h> | |
71 | #include <sys/socket.h> | |
72 | #include <sys/syslog.h> | |
73 | ||
74 | #include <net/if.h> | |
75 | #include <net/if_dl.h> | |
9bccf70c | 76 | #include <net/if_types.h> |
1c79356b A |
77 | #include <net/route.h> |
78 | #include <net/netisr.h> | |
9bccf70c A |
79 | #include <net/if_llc.h> |
80 | #if BRIDGE | |
81 | #include <net/ethernet.h> | |
82 | #include <net/bridge.h> | |
83 | #endif | |
1c79356b A |
84 | |
85 | #include <netinet/in.h> | |
86 | #include <netinet/in_var.h> | |
87 | #include <netinet/if_ether.h> | |
88 | ||
9bccf70c A |
89 | #include <net/iso88025.h> |
90 | ||
1c79356b A |
91 | #define SIN(s) ((struct sockaddr_in *)s) |
92 | #define SDL(s) ((struct sockaddr_dl *)s) | |
93 | ||
94 | SYSCTL_DECL(_net_link_ether); | |
95 | SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); | |
96 | ||
97 | /* timer values */ | |
98 | static int arpt_prune = (5*60*1); /* walk list every 5 minutes */ | |
99 | static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ | |
100 | static int arpt_down = 20; /* once declared down, don't send for 20 sec */ | |
101 | ||
0b4e3aa0 A |
102 | /* Apple Hardware SUM16 checksuming */ |
103 | int apple_hwcksum_tx = 1; | |
104 | int apple_hwcksum_rx = 1; | |
105 | ||
1c79356b A |
106 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, |
107 | &arpt_prune, 0, ""); | |
108 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, | |
109 | &arpt_keep, 0, ""); | |
110 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, | |
111 | &arpt_down, 0, ""); | |
0b4e3aa0 A |
112 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, apple_hwcksum_tx, CTLFLAG_RW, |
113 | &apple_hwcksum_tx, 0, ""); | |
114 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, apple_hwcksum_rx, CTLFLAG_RW, | |
115 | &apple_hwcksum_rx, 0, ""); | |
1c79356b A |
116 | |
117 | #define rt_expire rt_rmx.rmx_expire | |
118 | ||
119 | struct llinfo_arp { | |
120 | LIST_ENTRY(llinfo_arp) la_le; | |
121 | struct rtentry *la_rt; | |
122 | struct mbuf *la_hold; /* last packet until resolved/timeout */ | |
123 | long la_asked; /* last time we QUERIED for this addr */ | |
124 | #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */ | |
125 | }; | |
126 | ||
127 | static LIST_HEAD(, llinfo_arp) llinfo_arp; | |
128 | ||
129 | struct ifqueue arpintrq = {0, 0, 0, 50}; | |
130 | static int arp_inuse, arp_allocated; | |
131 | ||
132 | static int arp_maxtries = 5; | |
133 | static int useloopback = 1; /* use loopback interface for local traffic */ | |
134 | static int arp_proxyall = 0; | |
135 | static int arp_init_called = 0; | |
136 | ||
137 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, | |
138 | &arp_maxtries, 0, ""); | |
139 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, | |
140 | &useloopback, 0, ""); | |
141 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, | |
142 | &arp_proxyall, 0, ""); | |
143 | ||
144 | void arp_rtrequest __P((int, struct rtentry *, struct sockaddr *)); | |
145 | static void arprequest __P((struct arpcom *, | |
146 | struct in_addr *, struct in_addr *, u_char *)); | |
147 | void arpintr __P((void)); | |
148 | static void arptfree __P((struct llinfo_arp *)); | |
149 | static void arptimer __P((void *)); | |
150 | static u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; | |
151 | static struct llinfo_arp | |
152 | *arplookup __P((u_long, int, int)); | |
153 | #if INET | |
154 | static void in_arpinput __P((struct mbuf *)); | |
155 | #endif | |
156 | ||
157 | /* | |
158 | * Timeout routine. Age arp_tab entries periodically. | |
159 | */ | |
160 | /* ARGSUSED */ | |
161 | static void | |
162 | arptimer(ignored_arg) | |
163 | void *ignored_arg; | |
164 | { | |
9bccf70c A |
165 | #ifdef __APPLE__ |
166 | boolean_t funnel_state = thread_funnel_set(network_flock, TRUE); | |
167 | #endif | |
168 | int s = splnet(); | |
169 | register struct llinfo_arp *la = llinfo_arp.lh_first; | |
1c79356b | 170 | struct llinfo_arp *ola; |
1c79356b | 171 | |
1c79356b A |
172 | timeout(arptimer, (caddr_t)0, arpt_prune * hz); |
173 | while ((ola = la) != 0) { | |
174 | register struct rtentry *rt = la->la_rt; | |
175 | la = la->la_le.le_next; | |
176 | if (rt->rt_expire && rt->rt_expire <= time_second) | |
177 | arptfree(ola); /* timer has expired, clear */ | |
178 | } | |
179 | splx(s); | |
9bccf70c | 180 | #ifdef __APPLE__ |
1c79356b | 181 | (void) thread_funnel_set(network_flock, FALSE); |
9bccf70c | 182 | #endif |
1c79356b A |
183 | } |
184 | ||
185 | /* | |
186 | * Parallel to llc_rtrequest. | |
187 | */ | |
188 | void | |
189 | arp_rtrequest(req, rt, sa) | |
190 | int req; | |
191 | register struct rtentry *rt; | |
192 | struct sockaddr *sa; | |
193 | { | |
194 | register struct sockaddr *gate = rt->rt_gateway; | |
195 | register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; | |
196 | static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; | |
197 | static int arpinit_done; | |
198 | ||
199 | if (!arpinit_done) { | |
200 | arpinit_done = 1; | |
201 | LIST_INIT(&llinfo_arp); | |
202 | timeout(arptimer, (caddr_t)0, hz); | |
9bccf70c A |
203 | #ifndef __APPLE__ |
204 | register_netisr(NETISR_ARP, arpintr); | |
205 | #endif | |
1c79356b A |
206 | } |
207 | if (rt->rt_flags & RTF_GATEWAY) | |
208 | return; | |
209 | switch (req) { | |
210 | ||
211 | case RTM_ADD: | |
212 | /* | |
213 | * XXX: If this is a manually added route to interface | |
214 | * such as older version of routed or gated might provide, | |
215 | * restore cloning bit. | |
216 | */ | |
217 | if ((rt->rt_flags & RTF_HOST) == 0 && | |
218 | SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) | |
219 | rt->rt_flags |= RTF_CLONING; | |
220 | if (rt->rt_flags & RTF_CLONING) { | |
221 | /* | |
222 | * Case 1: This route should come from a route to iface. | |
223 | */ | |
224 | rt_setgate(rt, rt_key(rt), | |
225 | (struct sockaddr *)&null_sdl); | |
226 | gate = rt->rt_gateway; | |
227 | SDL(gate)->sdl_type = rt->rt_ifp->if_type; | |
228 | SDL(gate)->sdl_index = rt->rt_ifp->if_index; | |
229 | rt->rt_expire = time_second; | |
230 | break; | |
231 | } | |
232 | /* Announce a new entry if requested. */ | |
233 | if (rt->rt_flags & RTF_ANNOUNCE) | |
234 | arprequest((struct arpcom *)rt->rt_ifp, | |
235 | &SIN(rt_key(rt))->sin_addr, | |
236 | &SIN(rt_key(rt))->sin_addr, | |
237 | (u_char *)LLADDR(SDL(gate))); | |
238 | /*FALLTHROUGH*/ | |
239 | case RTM_RESOLVE: | |
240 | if (gate->sa_family != AF_LINK || | |
241 | gate->sa_len < sizeof(null_sdl)) { | |
242 | log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); | |
243 | break; | |
244 | } | |
245 | SDL(gate)->sdl_type = rt->rt_ifp->if_type; | |
246 | SDL(gate)->sdl_index = rt->rt_ifp->if_index; | |
247 | if (la != 0) | |
248 | break; /* This happens on a route change */ | |
249 | /* | |
250 | * Case 2: This route may come from cloning, or a manual route | |
251 | * add with a LL address. | |
252 | */ | |
253 | R_Malloc(la, struct llinfo_arp *, sizeof(*la)); | |
254 | rt->rt_llinfo = (caddr_t)la; | |
255 | if (la == 0) { | |
256 | log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); | |
257 | break; | |
258 | } | |
259 | arp_inuse++, arp_allocated++; | |
260 | Bzero(la, sizeof(*la)); | |
261 | la->la_rt = rt; | |
262 | rt->rt_flags |= RTF_LLINFO; | |
263 | LIST_INSERT_HEAD(&llinfo_arp, la, la_le); | |
264 | ||
265 | #if INET | |
266 | /* | |
267 | * This keeps the multicast addresses from showing up | |
268 | * in `arp -a' listings as unresolved. It's not actually | |
269 | * functional. Then the same for broadcast. | |
270 | */ | |
271 | if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) { | |
272 | ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, | |
273 | LLADDR(SDL(gate))); | |
274 | SDL(gate)->sdl_alen = 6; | |
275 | rt->rt_expire = 0; | |
276 | } | |
277 | if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { | |
278 | memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6); | |
279 | SDL(gate)->sdl_alen = 6; | |
9bccf70c | 280 | rt->rt_expire = time_second; |
1c79356b A |
281 | } |
282 | #endif | |
283 | ||
284 | if (SIN(rt_key(rt))->sin_addr.s_addr == | |
285 | (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { | |
286 | /* | |
287 | * This test used to be | |
288 | * if (loif.if_flags & IFF_UP) | |
289 | * It allowed local traffic to be forced | |
290 | * through the hardware by configuring the loopback down. | |
291 | * However, it causes problems during network configuration | |
292 | * for boards that can't receive packets they send. | |
293 | * It is now necessary to clear "useloopback" and remove | |
294 | * the route to force traffic out to the hardware. | |
295 | */ | |
296 | rt->rt_expire = 0; | |
297 | Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr, | |
298 | LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6); | |
299 | if (useloopback) | |
300 | rt->rt_ifp = loif; | |
301 | ||
302 | } | |
303 | break; | |
304 | ||
305 | case RTM_DELETE: | |
306 | if (la == 0) | |
307 | break; | |
308 | arp_inuse--; | |
309 | LIST_REMOVE(la, la_le); | |
310 | rt->rt_llinfo = 0; | |
311 | rt->rt_flags &= ~RTF_LLINFO; | |
312 | if (la->la_hold) | |
313 | m_freem(la->la_hold); | |
314 | Free((caddr_t)la); | |
315 | } | |
316 | } | |
317 | ||
1c79356b A |
318 | /* |
319 | * Broadcast an ARP packet, asking who has addr on interface ac. | |
320 | */ | |
321 | void | |
322 | arpwhohas(ac, addr) | |
9bccf70c A |
323 | struct arpcom *ac; |
324 | struct in_addr *addr; | |
325 | { | |
326 | struct ifnet *ifp = (struct ifnet *)ac; | |
1c79356b A |
327 | struct ifaddr *ifa = TAILQ_FIRST(&ifp->if_addrhead); |
328 | ||
9bccf70c A |
329 | while (ifa) { |
330 | if (ifa->ifa_addr->sa_family == AF_INET) { | |
331 | arprequest(ac, &SIN(ifa->ifa_addr)->sin_addr, addr, ac->ac_enaddr); | |
1c79356b A |
332 | return; |
333 | } | |
9bccf70c | 334 | ifa = TAILQ_NEXT(ifa, ifa_link); |
1c79356b A |
335 | } |
336 | return; /* XXX */ | |
337 | } | |
338 | ||
1c79356b A |
339 | /* |
340 | * Broadcast an ARP request. Caller specifies: | |
341 | * - arp header source ip address | |
342 | * - arp header target ip address | |
343 | * - arp header source ethernet address | |
344 | */ | |
345 | static void | |
346 | arprequest(ac, sip, tip, enaddr) | |
347 | register struct arpcom *ac; | |
348 | register struct in_addr *sip, *tip; | |
349 | register u_char *enaddr; | |
350 | { | |
351 | register struct mbuf *m; | |
352 | register struct ether_header *eh; | |
353 | register struct ether_arp *ea; | |
354 | struct sockaddr sa; | |
9bccf70c A |
355 | static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP, |
356 | LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 }; | |
1c79356b A |
357 | |
358 | if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) | |
359 | return; | |
1c79356b | 360 | m->m_pkthdr.rcvif = (struct ifnet *)0; |
9bccf70c A |
361 | switch (ac->ac_if.if_type) { |
362 | case IFT_ISO88025: | |
363 | m->m_len = sizeof(*ea) + sizeof(llcx); | |
364 | m->m_pkthdr.len = sizeof(*ea) + sizeof(llcx); | |
365 | MH_ALIGN(m, sizeof(*ea) + sizeof(llcx)); | |
366 | (void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx)); | |
367 | (void)memcpy(sa.sa_data, etherbroadcastaddr, 6); | |
368 | (void)memcpy(sa.sa_data + 6, enaddr, 6); | |
369 | sa.sa_data[6] |= TR_RII; | |
370 | sa.sa_data[12] = TR_AC; | |
371 | sa.sa_data[13] = TR_LLC_FRAME; | |
372 | ea = (struct ether_arp *)(mtod(m, char *) + sizeof(llcx)); | |
373 | bzero((caddr_t)ea, sizeof (*ea)); | |
374 | ea->arp_hrd = htons(ARPHRD_IEEE802); | |
375 | break; | |
376 | case IFT_FDDI: | |
377 | case IFT_ETHER: | |
378 | /* | |
379 | * This may not be correct for types not explicitly | |
380 | * listed, but this is our best guess | |
381 | */ | |
382 | default: | |
383 | m->m_len = sizeof(*ea); | |
384 | m->m_pkthdr.len = sizeof(*ea); | |
385 | MH_ALIGN(m, sizeof(*ea)); | |
386 | ea = mtod(m, struct ether_arp *); | |
387 | eh = (struct ether_header *)sa.sa_data; | |
388 | bzero((caddr_t)ea, sizeof (*ea)); | |
389 | /* if_output will not swap */ | |
390 | eh->ether_type = htons(ETHERTYPE_ARP); | |
391 | (void)memcpy(eh->ether_dhost, etherbroadcastaddr, | |
392 | sizeof(eh->ether_dhost)); | |
393 | ea->arp_hrd = htons(ARPHRD_ETHER); | |
394 | break; | |
395 | } | |
1c79356b A |
396 | ea->arp_pro = htons(ETHERTYPE_IP); |
397 | ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ | |
398 | ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ | |
399 | ea->arp_op = htons(ARPOP_REQUEST); | |
400 | (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha)); | |
401 | (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa)); | |
402 | (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa)); | |
403 | sa.sa_family = AF_UNSPEC; | |
404 | sa.sa_len = sizeof(sa); | |
55e303ae | 405 | dlil_output(ifptodlt(((struct ifnet *)ac), PF_INET), m, 0, &sa, 0); |
1c79356b A |
406 | } |
407 | ||
408 | /* | |
409 | * Resolve an IP address into an ethernet address. If success, | |
410 | * desten is filled in. If there is no entry in arptab, | |
411 | * set one up and broadcast a request for the IP address. | |
412 | * Hold onto this mbuf and resend it once the address | |
413 | * is finally resolved. A return value of 1 indicates | |
414 | * that desten has been filled in and the packet should be sent | |
415 | * normally; a 0 return indicates that the packet has been | |
416 | * taken over here, either now or for later transmission. | |
417 | */ | |
418 | int | |
419 | arpresolve(ac, rt, m, dst, desten, rt0) | |
420 | register struct arpcom *ac; | |
421 | register struct rtentry *rt; | |
422 | struct mbuf *m; | |
423 | register struct sockaddr *dst; | |
424 | register u_char *desten; | |
425 | struct rtentry *rt0; | |
426 | { | |
9bccf70c | 427 | struct llinfo_arp *la = 0; |
1c79356b A |
428 | struct sockaddr_dl *sdl; |
429 | ||
430 | if (m->m_flags & M_BCAST) { /* broadcast */ | |
431 | (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr)); | |
432 | return (1); | |
433 | } | |
434 | if (m->m_flags & M_MCAST) { /* multicast */ | |
435 | ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); | |
436 | return(1); | |
437 | } | |
438 | if (rt) | |
439 | la = (struct llinfo_arp *)rt->rt_llinfo; | |
440 | if (la == 0) { | |
441 | la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); | |
442 | if (la) | |
443 | rt = la->la_rt; | |
444 | } | |
445 | if (la == 0 || rt == 0) { | |
446 | log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", | |
447 | inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "", | |
448 | rt ? "rt" : ""); | |
449 | m_freem(m); | |
450 | return (0); | |
451 | } | |
452 | sdl = SDL(rt->rt_gateway); | |
453 | /* | |
454 | * Check the address family and length is valid, the address | |
455 | * is resolved; otherwise, try to resolve. | |
456 | */ | |
457 | if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && | |
458 | sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { | |
459 | bcopy(LLADDR(sdl), desten, sdl->sdl_alen); | |
460 | return 1; | |
461 | } | |
9bccf70c A |
462 | /* |
463 | * If ARP is disabled on this interface, stop. | |
464 | * XXX | |
465 | * Probably should not allocate empty llinfo struct if we are | |
466 | * not going to be sending out an arp request. | |
467 | */ | |
468 | if (ac->ac_if.if_flags & IFF_NOARP) | |
469 | return (0); | |
1c79356b A |
470 | /* |
471 | * There is an arptab entry, but no ethernet address | |
472 | * response yet. Replace the held mbuf with this | |
473 | * latest one. | |
474 | */ | |
475 | if (la->la_hold) | |
476 | m_freem(la->la_hold); | |
477 | la->la_hold = m; | |
478 | if (rt->rt_expire) { | |
479 | rt->rt_flags &= ~RTF_REJECT; | |
480 | if (la->la_asked == 0 || rt->rt_expire != time_second) { | |
481 | rt->rt_expire = time_second; | |
482 | if (la->la_asked++ < arp_maxtries) | |
483 | arprequest(ac, | |
484 | &SIN(rt->rt_ifa->ifa_addr)->sin_addr, | |
485 | &SIN(dst)->sin_addr, ac->ac_enaddr); | |
486 | else { | |
487 | rt->rt_flags |= RTF_REJECT; | |
488 | rt->rt_expire += arpt_down; | |
489 | la->la_asked = 0; | |
490 | } | |
491 | ||
492 | } | |
493 | } | |
494 | return (0); | |
495 | } | |
496 | ||
497 | /* | |
498 | * Common length and type checks are done here, | |
499 | * then the protocol-specific routine is called. | |
500 | */ | |
501 | void | |
502 | arpintr() | |
503 | { | |
504 | register struct mbuf *m; | |
505 | register struct arphdr *ar; | |
506 | int s; | |
507 | ||
508 | while (arpintrq.ifq_head) { | |
509 | s = splimp(); | |
510 | IF_DEQUEUE(&arpintrq, m); | |
511 | splx(s); | |
512 | if (m == 0 || (m->m_flags & M_PKTHDR) == 0) | |
513 | panic("arpintr"); | |
9bccf70c A |
514 | |
515 | if (m->m_len < sizeof(struct arphdr) && | |
516 | ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { | |
517 | log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); | |
518 | continue; | |
519 | } | |
520 | ar = mtod(m, struct arphdr *); | |
521 | ||
522 | if (ntohs(ar->ar_hrd) != ARPHRD_ETHER | |
523 | && ntohs(ar->ar_hrd) != ARPHRD_IEEE802) { | |
524 | log(LOG_ERR, | |
525 | "arp: unknown hardware address format (0x%2D)\n", | |
526 | (unsigned char *)&ar->ar_hrd, ""); | |
527 | m_freem(m); | |
528 | continue; | |
529 | } | |
1c79356b | 530 | |
9bccf70c A |
531 | if (m->m_pkthdr.len < sizeof(struct arphdr) + 2 * ar->ar_hln |
532 | + 2 * ar->ar_pln) { | |
533 | log(LOG_ERR, "arp: runt packet\n"); | |
534 | m_freem(m); | |
535 | continue; | |
536 | } | |
1c79356b | 537 | |
9bccf70c A |
538 | switch (ntohs(ar->ar_pro)) { |
539 | #ifdef INET | |
540 | case ETHERTYPE_IP: | |
541 | in_arpinput(m); | |
542 | continue; | |
1c79356b | 543 | #endif |
9bccf70c | 544 | } |
1c79356b A |
545 | m_freem(m); |
546 | } | |
547 | } | |
548 | ||
1c79356b A |
549 | #if INET |
550 | /* | |
551 | * ARP for Internet protocols on 10 Mb/s Ethernet. | |
552 | * Algorithm is that given in RFC 826. | |
553 | * In addition, a sanity check is performed on the sender | |
554 | * protocol address, to catch impersonators. | |
555 | * We no longer handle negotiations for use of trailer protocol: | |
556 | * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent | |
557 | * along with IP replies if we wanted trailers sent to us, | |
558 | * and also sent them in response to IP replies. | |
559 | * This allowed either end to announce the desire to receive | |
560 | * trailer packets. | |
561 | * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, | |
562 | * but formerly didn't normally send requests. | |
563 | */ | |
9bccf70c A |
564 | static int log_arp_wrong_iface = 0; |
565 | ||
566 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, | |
567 | &log_arp_wrong_iface, 0, | |
568 | "log arp packets arriving on the wrong interface"); | |
569 | ||
1c79356b A |
570 | static void |
571 | in_arpinput(m) | |
572 | struct mbuf *m; | |
573 | { | |
574 | register struct ether_arp *ea; | |
575 | register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif; | |
576 | struct ether_header *eh; | |
9bccf70c | 577 | struct iso88025_header *th = (struct iso88025_header *)0; |
1c79356b A |
578 | register struct llinfo_arp *la = 0; |
579 | register struct rtentry *rt; | |
580 | struct in_ifaddr *ia, *maybe_ia = 0; | |
581 | struct sockaddr_dl *sdl; | |
582 | struct sockaddr sa; | |
583 | struct in_addr isaddr, itaddr, myaddr; | |
9bccf70c | 584 | int op, rif_len; |
1c79356b | 585 | unsigned char buf[18]; |
9bccf70c A |
586 | unsigned char buf2[18]; |
587 | ||
588 | if (m->m_len < sizeof(struct ether_arp) && | |
589 | (m = m_pullup(m, sizeof(struct ether_arp))) == NULL) { | |
590 | log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); | |
591 | return; | |
592 | } | |
1c79356b A |
593 | |
594 | ea = mtod(m, struct ether_arp *); | |
595 | op = ntohs(ea->arp_op); | |
596 | (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr)); | |
597 | (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr)); | |
0b4e3aa0 | 598 | |
9bccf70c | 599 | #if __APPLE__ |
0b4e3aa0 A |
600 | /* Don't respond to requests for 0.0.0.0 */ |
601 | if (itaddr.s_addr == 0 && op == ARPOP_REQUEST) { | |
602 | m_freem(m); | |
603 | return; | |
604 | } | |
9bccf70c | 605 | #endif |
0b4e3aa0 | 606 | |
9bccf70c | 607 | for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next) { |
1c79356b A |
608 | /* |
609 | * For a bridge, we want to check the address irrespective | |
610 | * of the receive interface. (This will change slightly | |
611 | * when we have clusters of interfaces). | |
612 | */ | |
9bccf70c A |
613 | #if BRIDGE |
614 | #define BRIDGE_TEST (do_bridge) | |
1c79356b | 615 | #else |
9bccf70c | 616 | #define BRIDGE_TEST (0) /* cc will optimise the test away */ |
1c79356b | 617 | #endif |
9bccf70c | 618 | if ((BRIDGE_TEST) || (ia->ia_ifp == &ac->ac_if)) { |
1c79356b A |
619 | maybe_ia = ia; |
620 | if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) || | |
9bccf70c | 621 | (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) { |
1c79356b | 622 | break; |
9bccf70c | 623 | } |
1c79356b | 624 | } |
9bccf70c | 625 | } |
1c79356b A |
626 | if (maybe_ia == 0) { |
627 | m_freem(m); | |
628 | return; | |
629 | } | |
630 | myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr; | |
9bccf70c A |
631 | if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, |
632 | sizeof (ea->arp_sha))) { | |
633 | m_freem(m); /* it's from me, ignore it. */ | |
634 | return; | |
635 | } | |
1c79356b A |
636 | if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, |
637 | sizeof (ea->arp_sha))) { | |
638 | log(LOG_ERR, | |
639 | "arp: ether address is broadcast for IP address %s!\n", | |
640 | inet_ntoa(isaddr)); | |
641 | m_freem(m); | |
642 | return; | |
643 | } | |
644 | if (isaddr.s_addr == myaddr.s_addr) { | |
90556fb8 A |
645 | struct kev_msg ev_msg; |
646 | struct kev_in_collision *in_collision; | |
647 | u_char storage[sizeof(struct kev_in_collision) + 6]; | |
648 | in_collision = (struct kev_in_collision*)storage; | |
649 | ||
9bccf70c A |
650 | log(LOG_ERR, |
651 | "duplicate IP address %s sent from ethernet address %s\n", | |
652 | inet_ntoa(isaddr), ether_sprintf(buf, ea->arp_sha)); | |
90556fb8 A |
653 | |
654 | /* Send a kernel event so anyone can learn of the conflict */ | |
655 | in_collision->link_data.if_family = ac->ac_if.if_family; | |
656 | in_collision->link_data.if_unit = ac->ac_if.if_unit; | |
657 | strncpy(&in_collision->link_data.if_name[0], ac->ac_if.if_name, IFNAMSIZ); | |
658 | in_collision->ia_ipaddr = isaddr; | |
659 | in_collision->hw_len = ETHER_ADDR_LEN; | |
660 | bcopy((caddr_t)ea->arp_sha, (caddr_t)in_collision->hw_addr, sizeof(ea->arp_sha)); | |
661 | ev_msg.vendor_code = KEV_VENDOR_APPLE; | |
662 | ev_msg.kev_class = KEV_NETWORK_CLASS; | |
663 | ev_msg.kev_subclass = KEV_INET_SUBCLASS; | |
664 | ev_msg.event_code = KEV_INET_ARPCOLLISION; | |
665 | ev_msg.dv[0].data_ptr = in_collision; | |
666 | ev_msg.dv[0].data_length = sizeof(struct kev_in_collision) + 6; | |
667 | ev_msg.dv[1].data_length = 0; | |
668 | kev_post_msg(&ev_msg); | |
669 | ||
1c79356b A |
670 | itaddr = myaddr; |
671 | goto reply; | |
672 | } | |
673 | la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); | |
674 | if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { | |
9bccf70c A |
675 | /* the following is not an error when doing bridging */ |
676 | if (!BRIDGE_TEST && rt->rt_ifp != &ac->ac_if) { | |
677 | if (log_arp_wrong_iface) | |
678 | log(LOG_ERR, "arp: %s is on %s%d but got reply from %s on %s%d\n", | |
1c79356b A |
679 | inet_ntoa(isaddr), |
680 | rt->rt_ifp->if_name, rt->rt_ifp->if_unit, | |
9bccf70c | 681 | ether_sprintf(buf, ea->arp_sha), |
1c79356b | 682 | ac->ac_if.if_name, ac->ac_if.if_unit); |
9bccf70c | 683 | goto reply; |
1c79356b | 684 | } |
1c79356b | 685 | if (sdl->sdl_alen && |
9bccf70c | 686 | bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) { |
1c79356b | 687 | if (rt->rt_expire) |
9bccf70c A |
688 | log(LOG_INFO, "arp: %s moved from %s to %s on %s%d\n", |
689 | inet_ntoa(isaddr), | |
690 | ether_sprintf(buf, (u_char *)LLADDR(sdl)), | |
691 | ether_sprintf(buf2, ea->arp_sha), | |
1c79356b A |
692 | ac->ac_if.if_name, ac->ac_if.if_unit); |
693 | else { | |
694 | log(LOG_ERR, | |
9bccf70c A |
695 | "arp: %s attempts to modify permanent entry for %s on %s%d", |
696 | ether_sprintf(buf, ea->arp_sha), inet_ntoa(isaddr), | |
1c79356b A |
697 | ac->ac_if.if_name, ac->ac_if.if_unit); |
698 | goto reply; | |
699 | } | |
9bccf70c | 700 | } |
1c79356b A |
701 | (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha)); |
702 | sdl->sdl_alen = sizeof(ea->arp_sha); | |
9bccf70c A |
703 | #ifndef __APPLE__ |
704 | /* TokenRing */ | |
705 | sdl->sdl_rcf = (u_short)0; | |
706 | /* | |
707 | * If we receive an arp from a token-ring station over | |
708 | * a token-ring nic then try to save the source | |
709 | * routing info. | |
710 | */ | |
711 | if (ac->ac_if.if_type == IFT_ISO88025) { | |
712 | th = (struct iso88025_header *)m->m_pkthdr.header; | |
713 | rif_len = TR_RCF_RIFLEN(th->rcf); | |
714 | if ((th->iso88025_shost[0] & TR_RII) && | |
715 | (rif_len > 2)) { | |
716 | sdl->sdl_rcf = th->rcf; | |
717 | sdl->sdl_rcf ^= htons(TR_RCF_DIR); | |
718 | memcpy(sdl->sdl_route, th->rd, rif_len - 2); | |
719 | sdl->sdl_rcf &= ~htons(TR_RCF_BCST_MASK); | |
720 | /* | |
721 | * Set up source routing information for | |
722 | * reply packet (XXX) | |
723 | */ | |
724 | m->m_data -= rif_len; | |
725 | m->m_len += rif_len; | |
726 | m->m_pkthdr.len += rif_len; | |
727 | } else { | |
728 | th->iso88025_shost[0] &= ~TR_RII; | |
729 | } | |
730 | m->m_data -= 8; | |
731 | m->m_len += 8; | |
732 | m->m_pkthdr.len += 8; | |
733 | th->rcf = sdl->sdl_rcf; | |
734 | } | |
735 | #endif | |
1c79356b A |
736 | if (rt->rt_expire) |
737 | rt->rt_expire = time_second + arpt_keep; | |
738 | rt->rt_flags &= ~RTF_REJECT; | |
739 | la->la_asked = 0; | |
740 | if (la->la_hold) { | |
9bccf70c | 741 | dlil_output(((struct ifnet *)ac)->if_data.default_proto, la->la_hold, rt, |
1c79356b A |
742 | rt_key(rt), 0); |
743 | la->la_hold = 0; | |
744 | } | |
745 | } | |
746 | reply: | |
747 | if (op != ARPOP_REQUEST) { | |
748 | m_freem(m); | |
749 | return; | |
750 | } | |
751 | if (itaddr.s_addr == myaddr.s_addr) { | |
752 | /* I am the target */ | |
753 | (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); | |
754 | (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); | |
755 | } else { | |
756 | la = arplookup(itaddr.s_addr, 0, SIN_PROXY); | |
757 | if (la == NULL) { | |
758 | struct sockaddr_in sin; | |
759 | ||
760 | if (!arp_proxyall) { | |
761 | m_freem(m); | |
762 | return; | |
763 | } | |
764 | ||
765 | bzero(&sin, sizeof sin); | |
766 | sin.sin_family = AF_INET; | |
767 | sin.sin_len = sizeof sin; | |
768 | sin.sin_addr = itaddr; | |
769 | ||
770 | rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); | |
771 | if (!rt) { | |
772 | m_freem(m); | |
773 | return; | |
774 | } | |
775 | /* | |
776 | * Don't send proxies for nodes on the same interface | |
777 | * as this one came out of, or we'll get into a fight | |
778 | * over who claims what Ether address. | |
779 | */ | |
780 | if (rt->rt_ifp == &ac->ac_if) { | |
781 | rtfree(rt); | |
782 | m_freem(m); | |
783 | return; | |
784 | } | |
785 | (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); | |
786 | (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); | |
787 | rtfree(rt); | |
788 | #if DEBUG_PROXY | |
789 | printf("arp: proxying for %s\n", | |
790 | inet_ntoa(itaddr)); | |
791 | #endif | |
792 | } else { | |
793 | rt = la->la_rt; | |
794 | (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); | |
795 | sdl = SDL(rt->rt_gateway); | |
796 | (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha)); | |
797 | } | |
798 | } | |
799 | ||
800 | (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa)); | |
801 | (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa)); | |
802 | ea->arp_op = htons(ARPOP_REPLY); | |
803 | ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ | |
9bccf70c A |
804 | switch (ac->ac_if.if_type) { |
805 | case IFT_ISO88025: | |
806 | /* Re-arrange the source/dest address */ | |
807 | memcpy(th->iso88025_dhost, th->iso88025_shost, | |
808 | sizeof(th->iso88025_dhost)); | |
809 | memcpy(th->iso88025_shost, ac->ac_enaddr, | |
810 | sizeof(th->iso88025_shost)); | |
811 | /* Set the source routing bit if neccesary */ | |
812 | if (th->iso88025_dhost[0] & TR_RII) { | |
813 | th->iso88025_dhost[0] &= ~TR_RII; | |
814 | if (TR_RCF_RIFLEN(th->rcf) > 2) | |
815 | th->iso88025_shost[0] |= TR_RII; | |
816 | } | |
817 | /* Copy the addresses, ac and fc into sa_data */ | |
818 | memcpy(sa.sa_data, th->iso88025_dhost, | |
819 | sizeof(th->iso88025_dhost) * 2); | |
820 | sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC; | |
821 | sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME; | |
822 | break; | |
823 | case IFT_ETHER: | |
824 | case IFT_FDDI: | |
825 | /* | |
826 | * May not be correct for types not explictly | |
827 | * listed, but it is our best guess. | |
828 | */ | |
829 | default: | |
830 | eh = (struct ether_header *)sa.sa_data; | |
831 | #ifdef __APPLE__ | |
832 | if (IN_LINKLOCAL(ntohl(*((u_int32_t*)ea->arp_spa)))) | |
833 | (void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost)); | |
834 | else | |
835 | #endif | |
836 | (void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost)); | |
837 | eh->ether_type = htons(ETHERTYPE_ARP); | |
838 | break; | |
839 | } | |
1c79356b A |
840 | sa.sa_family = AF_UNSPEC; |
841 | sa.sa_len = sizeof(sa); | |
9bccf70c | 842 | dlil_output(((struct ifnet *)ac)->if_data.default_proto, m, 0, &sa, 0); |
1c79356b A |
843 | return; |
844 | } | |
845 | #endif | |
846 | ||
847 | /* | |
848 | * Free an arp entry. | |
849 | */ | |
850 | static void | |
851 | arptfree(la) | |
852 | register struct llinfo_arp *la; | |
853 | { | |
854 | register struct rtentry *rt = la->la_rt; | |
855 | register struct sockaddr_dl *sdl; | |
856 | if (rt == 0) | |
857 | panic("arptfree"); | |
858 | if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && | |
859 | sdl->sdl_family == AF_LINK) { | |
860 | sdl->sdl_alen = 0; | |
861 | la->la_asked = 0; | |
862 | rt->rt_flags &= ~RTF_REJECT; | |
863 | return; | |
864 | } | |
865 | rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), | |
866 | 0, (struct rtentry **)0); | |
867 | } | |
868 | /* | |
869 | * Lookup or enter a new address in arptab. | |
870 | */ | |
871 | static struct llinfo_arp * | |
872 | arplookup(addr, create, proxy) | |
873 | u_long addr; | |
874 | int create, proxy; | |
875 | { | |
876 | register struct rtentry *rt; | |
877 | static struct sockaddr_inarp sin = {sizeof(sin), AF_INET }; | |
878 | const char *why = 0; | |
879 | ||
880 | sin.sin_addr.s_addr = addr; | |
881 | sin.sin_other = proxy ? SIN_PROXY : 0; | |
882 | rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); | |
883 | if (rt == 0) | |
884 | return (0); | |
9bccf70c | 885 | rtunref(rt); |
43866e37 A |
886 | |
887 | if (rt->rt_flags & RTF_GATEWAY) { | |
1c79356b | 888 | why = "host is not on local network"; |
43866e37 A |
889 | |
890 | /* If there are no references to this route, purge it */ | |
891 | if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED) != 0) { | |
892 | rtrequest(RTM_DELETE, | |
893 | (struct sockaddr *)rt_key(rt), | |
894 | rt->rt_gateway, rt_mask(rt), | |
895 | rt->rt_flags, 0); | |
896 | } | |
897 | } | |
1c79356b A |
898 | else if ((rt->rt_flags & RTF_LLINFO) == 0) |
899 | why = "could not allocate llinfo"; | |
900 | else if (rt->rt_gateway->sa_family != AF_LINK) | |
901 | why = "gateway route is not ours"; | |
902 | ||
903 | if (why && create) { | |
904 | log(LOG_DEBUG, "arplookup %s failed: %s\n", | |
905 | inet_ntoa(sin.sin_addr), why); | |
906 | return 0; | |
907 | } else if (why) { | |
908 | return 0; | |
909 | } | |
910 | return ((struct llinfo_arp *)rt->rt_llinfo); | |
911 | } | |
912 | ||
913 | void | |
914 | arp_ifinit(ac, ifa) | |
915 | struct arpcom *ac; | |
916 | struct ifaddr *ifa; | |
917 | { | |
918 | if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) | |
919 | arprequest(ac, &IA_SIN(ifa)->sin_addr, | |
920 | &IA_SIN(ifa)->sin_addr, ac->ac_enaddr); | |
921 | ifa->ifa_rtrequest = arp_rtrequest; | |
922 | ifa->ifa_flags |= RTF_CLONING; | |
923 | } |