Schedule ipfw_tick() to IPFW_CFGPORT to run, this function is too time
[dragonfly.git] / sys / netinet / if_ether.c
CommitLineData
f3ed2586
JH
1/*
2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Jeffrey M. Hsu.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
984263bc
MD
33/*
34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
66 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
799975d0 67 * $DragonFly: src/sys/netinet/if_ether.c,v 1.52 2008/09/25 14:08:06 sephe Exp $
984263bc
MD
68 */
69
70/*
71 * Ethernet address resolution protocol.
72 * TODO:
73 * add "inuse/lock" bit (or ref. count) along with valid bit
74 */
75
76#include "opt_inet.h"
0d16ba1d 77#include "opt_carp.h"
984263bc
MD
78
79#include <sys/param.h>
80#include <sys/kernel.h>
81#include <sys/queue.h>
82#include <sys/sysctl.h>
83#include <sys/systm.h>
84#include <sys/mbuf.h>
85#include <sys/malloc.h>
86#include <sys/socket.h>
87#include <sys/syslog.h>
88
89#include <net/if.h>
90#include <net/if_dl.h>
91#include <net/if_types.h>
92#include <net/route.h>
93#include <net/netisr.h>
94#include <net/if_llc.h>
984263bc
MD
95
96#include <netinet/in.h>
97#include <netinet/in_var.h>
98#include <netinet/if_ether.h>
99
4599cf19
MD
100#include <sys/thread2.h>
101#include <sys/msgport2.h>
102#include <net/netmsg2.h>
103
0d16ba1d
MD
104#ifdef CARP
105#include <netinet/ip_carp.h>
106#endif
107
984263bc
MD
108#define SIN(s) ((struct sockaddr_in *)s)
109#define SDL(s) ((struct sockaddr_dl *)s)
110
111SYSCTL_DECL(_net_link_ether);
112SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
113
114/* timer values */
115static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
116static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
117static int arpt_down = 20; /* once declared down, don't send for 20 sec */
118
119SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
120 &arpt_prune, 0, "");
f23061d4 121SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
984263bc
MD
122 &arpt_keep, 0, "");
123SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
124 &arpt_down, 0, "");
125
f23061d4 126#define rt_expire rt_rmx.rmx_expire
984263bc
MD
127
128struct llinfo_arp {
129 LIST_ENTRY(llinfo_arp) la_le;
130 struct rtentry *la_rt;
131 struct mbuf *la_hold; /* last packet until resolved/timeout */
a916e847 132 u_short la_preempt; /* countdown for pre-expiry arps */
984263bc 133 u_short la_asked; /* #times we QUERIED following expiration */
984263bc
MD
134};
135
ecdefdda 136static LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU];
984263bc 137
984263bc
MD
138static int arp_maxtries = 5;
139static int useloopback = 1; /* use loopback interface for local traffic */
140static int arp_proxyall = 0;
141
142SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
143 &arp_maxtries, 0, "");
144SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
145 &useloopback, 0, "");
146SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
147 &arp_proxyall, 0, "");
148
799975d0
SZ
149static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
150static void arprequest(struct ifnet *, struct in_addr *, struct in_addr *,
151 u_char *);
4599cf19 152static void arpintr(struct netmsg *);
799975d0
SZ
153static void arptfree(struct llinfo_arp *);
154static void arptimer(void *);
155static struct llinfo_arp *
156 arplookup(in_addr_t, boolean_t, boolean_t);
984263bc 157#ifdef INET
799975d0 158static void in_arpinput(struct mbuf *);
984263bc
MD
159#endif
160
ecdefdda 161static struct callout arptimer_ch[MAXCPU];
ac311540 162
984263bc
MD
163/*
164 * Timeout routine. Age arp_tab entries periodically.
165 */
166/* ARGSUSED */
167static void
f23061d4 168arptimer(void *ignored_arg)
984263bc 169{
2e9572df 170 struct llinfo_arp *la, *nla;
984263bc 171
1cae611f 172 crit_enter();
ecdefdda 173 LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) {
2e9572df 174 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
799975d0 175 arptfree(la);
984263bc 176 }
ecdefdda 177 callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL);
1cae611f 178 crit_exit();
984263bc
MD
179}
180
181/*
182 * Parallel to llc_rtrequest.
183 */
184static void
f23061d4 185arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
984263bc 186{
2256ba69 187 struct sockaddr *gate = rt->rt_gateway;
f23061d4
JH
188 struct llinfo_arp *la = rt->rt_llinfo;
189
5fe66e68 190 struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
ecdefdda 191 static boolean_t arpinit_done[MAXCPU];
984263bc 192
ecdefdda
MD
193 if (!arpinit_done[mycpuid]) {
194 arpinit_done[mycpuid] = TRUE;
195 callout_init(&arptimer_ch[mycpuid]);
196 callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
984263bc
MD
197 }
198 if (rt->rt_flags & RTF_GATEWAY)
199 return;
984263bc 200
f23061d4 201 switch (req) {
984263bc
MD
202 case RTM_ADD:
203 /*
204 * XXX: If this is a manually added route to interface
205 * such as older version of routed or gated might provide,
206 * restore cloning bit.
207 */
f23061d4 208 if (!(rt->rt_flags & RTF_HOST) &&
984263bc
MD
209 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
210 rt->rt_flags |= RTF_CLONING;
211 if (rt->rt_flags & RTF_CLONING) {
212 /*
213 * Case 1: This route should come from a route to iface.
214 */
215 rt_setgate(rt, rt_key(rt),
0c3c561c 216 (struct sockaddr *)&null_sdl);
984263bc
MD
217 gate = rt->rt_gateway;
218 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
219 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
220 rt->rt_expire = time_second;
221 break;
222 }
223 /* Announce a new entry if requested. */
9c94ab97 224 if (rt->rt_flags & RTF_ANNOUNCE) {
984263bc
MD
225 arprequest(rt->rt_ifp,
226 &SIN(rt_key(rt))->sin_addr,
227 &SIN(rt_key(rt))->sin_addr,
f23061d4 228 LLADDR(SDL(gate)));
9c94ab97 229 }
984263bc
MD
230 /*FALLTHROUGH*/
231 case RTM_RESOLVE:
232 if (gate->sa_family != AF_LINK ||
f23061d4 233 gate->sa_len < sizeof(struct sockaddr_dl)) {
984263bc
MD
234 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
235 break;
236 }
237 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
238 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
f23061d4 239 if (la != NULL)
984263bc
MD
240 break; /* This happens on a route change */
241 /*
242 * Case 2: This route may come from cloning, or a manual route
243 * add with a LL address.
244 */
0c3c561c 245 R_Malloc(la, struct llinfo_arp *, sizeof *la);
f23061d4
JH
246 rt->rt_llinfo = la;
247 if (la == NULL) {
984263bc
MD
248 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
249 break;
250 }
f23061d4 251 bzero(la, sizeof *la);
984263bc
MD
252 la->la_rt = rt;
253 rt->rt_flags |= RTF_LLINFO;
ecdefdda 254 LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
984263bc
MD
255
256#ifdef INET
257 /*
258 * This keeps the multicast addresses from showing up
259 * in `arp -a' listings as unresolved. It's not actually
260 * functional. Then the same for broadcast.
261 */
ce6da440 262 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
984263bc
MD
263 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
264 LLADDR(SDL(gate)));
265 SDL(gate)->sdl_alen = 6;
266 rt->rt_expire = 0;
267 }
268 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
c401f0fd 269 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
984263bc
MD
270 rt->rt_ifp->if_addrlen);
271 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
272 rt->rt_expire = 0;
273 }
274#endif
275
276 if (SIN(rt_key(rt))->sin_addr.s_addr ==
277 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
f23061d4
JH
278 /*
279 * This test used to be
280 * if (loif.if_flags & IFF_UP)
281 * It allowed local traffic to be forced
282 * through the hardware by configuring the
283 * loopback down. However, it causes problems
284 * during network configuration for boards
285 * that can't receive packets they send. It
286 * is now necessary to clear "useloopback" and
287 * remove the route to force traffic out to
288 * the hardware.
289 */
984263bc 290 rt->rt_expire = 0;
2e9572df 291 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
984263bc
MD
292 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
293 if (useloopback)
294 rt->rt_ifp = loif;
984263bc
MD
295 }
296 break;
297
298 case RTM_DELETE:
f23061d4 299 if (la == NULL)
984263bc 300 break;
984263bc 301 LIST_REMOVE(la, la_le);
f23061d4 302 rt->rt_llinfo = NULL;
984263bc 303 rt->rt_flags &= ~RTF_LLINFO;
f23061d4 304 if (la->la_hold != NULL)
984263bc 305 m_freem(la->la_hold);
f23061d4 306 Free(la);
799975d0 307 break;
984263bc
MD
308 }
309}
310
311/*
312 * Broadcast an ARP request. Caller specifies:
313 * - arp header source ip address
314 * - arp header target ip address
315 * - arp header source ethernet address
316 */
317static void
f23061d4
JH
318arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip,
319 u_char *enaddr)
984263bc 320{
2256ba69
RG
321 struct mbuf *m;
322 struct ether_header *eh;
2256ba69 323 struct arphdr *ah;
984263bc 324 struct sockaddr sa;
984263bc
MD
325 u_short ar_hrd;
326
74f1caca 327 if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
984263bc 328 return;
f23061d4
JH
329 m->m_pkthdr.rcvif = (struct ifnet *)NULL;
330
984263bc 331 switch (ifp->if_type) {
984263bc
MD
332 case IFT_ETHER:
333 /*
334 * This may not be correct for types not explicitly
335 * listed, but this is our best guess
336 */
337 default:
338 ar_hrd = htons(ARPHRD_ETHER);
339
340 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
341 m->m_pkthdr.len = m->m_len;
342 MH_ALIGN(m, m->m_len);
343
344 eh = (struct ether_header *)sa.sa_data;
f23061d4 345 /* if_output() will not swap */
984263bc 346 eh->ether_type = htons(ETHERTYPE_ARP);
c401f0fd 347 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
984263bc
MD
348
349 ah = mtod(m, struct arphdr *);
350 break;
351 }
352
353 ah->ar_hrd = ar_hrd;
354 ah->ar_pro = htons(ETHERTYPE_IP);
355 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
356 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
357 ah->ar_op = htons(ARPOP_REQUEST);
f23061d4 358 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
984263bc 359 memset(ar_tha(ah), 0, ah->ar_hln);
f23061d4
JH
360 memcpy(ar_spa(ah), sip, ah->ar_pln);
361 memcpy(ar_tpa(ah), tip, ah->ar_pln);
984263bc
MD
362
363 sa.sa_family = AF_UNSPEC;
0c3c561c 364 sa.sa_len = sizeof sa;
799975d0 365 ifp->if_output(ifp, m, &sa, NULL);
984263bc
MD
366}
367
368/*
369 * Resolve an IP address into an ethernet address. If success,
370 * desten is filled in. If there is no entry in arptab,
371 * set one up and broadcast a request for the IP address.
372 * Hold onto this mbuf and resend it once the address
373 * is finally resolved. A return value of 1 indicates
374 * that desten has been filled in and the packet should be sent
375 * normally; a 0 return indicates that the packet has been
376 * taken over here, either now or for later transmission.
377 */
378int
799975d0
SZ
379arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
380 struct sockaddr *dst, u_char *desten)
984263bc 381{
f23061d4
JH
382 struct rtentry *rt;
383 struct llinfo_arp *la = NULL;
984263bc
MD
384 struct sockaddr_dl *sdl;
385
386 if (m->m_flags & M_BCAST) { /* broadcast */
c401f0fd 387 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
984263bc
MD
388 return (1);
389 }
ce6da440 390 if (m->m_flags & M_MCAST) {/* multicast */
984263bc 391 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
f23061d4 392 return (1);
984263bc 393 }
f23061d4
JH
394 if (rt0 != NULL) {
395 if (rt_llroute(dst, rt0, &rt) != 0) {
396 m_freem(m);
397 return 0;
398 }
399 la = rt->rt_llinfo;
400 }
401 if (la == NULL) {
402 la = arplookup(SIN(dst)->sin_addr.s_addr, TRUE, FALSE);
403 if (la != NULL)
984263bc
MD
404 rt = la->la_rt;
405 }
f23061d4 406 if (la == NULL || rt == NULL) {
984263bc 407 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
5fe66e68
JH
408 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
409 rt ? "rt" : "");
984263bc
MD
410 m_freem(m);
411 return (0);
412 }
413 sdl = SDL(rt->rt_gateway);
414 /*
415 * Check the address family and length is valid, the address
416 * is resolved; otherwise, try to resolve.
417 */
418 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
419 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
420 /*
421 * If entry has an expiry time and it is approaching,
422 * see if we need to send an ARP request within this
423 * arpt_down interval.
424 */
425 if ((rt->rt_expire != 0) &&
a916e847 426 (time_second + la->la_preempt > rt->rt_expire)) {
984263bc
MD
427 arprequest(ifp,
428 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
429 &SIN(dst)->sin_addr,
430 IF_LLADDR(ifp));
a916e847 431 la->la_preempt--;
f23061d4 432 }
984263bc
MD
433
434 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
435 return 1;
436 }
437 /*
438 * If ARP is disabled on this interface, stop.
439 * XXX
440 * Probably should not allocate empty llinfo struct if we are
441 * not going to be sending out an arp request.
442 */
443 if (ifp->if_flags & IFF_NOARP) {
444 m_freem(m);
445 return (0);
446 }
447 /*
448 * There is an arptab entry, but no ethernet address
449 * response yet. Replace the held mbuf with this
450 * latest one.
451 */
f23061d4 452 if (la->la_hold != NULL)
984263bc
MD
453 m_freem(la->la_hold);
454 la->la_hold = m;
03f869ef 455 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
984263bc
MD
456 rt->rt_flags &= ~RTF_REJECT;
457 if (la->la_asked == 0 || rt->rt_expire != time_second) {
458 rt->rt_expire = time_second;
459 if (la->la_asked++ < arp_maxtries) {
460 arprequest(ifp,
461 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
462 &SIN(dst)->sin_addr,
463 IF_LLADDR(ifp));
464 } else {
465 rt->rt_flags |= RTF_REJECT;
466 rt->rt_expire += arpt_down;
a916e847
JH
467 la->la_asked = 0;
468 la->la_preempt = arp_maxtries;
984263bc 469 }
984263bc
MD
470 }
471 }
472 return (0);
473}
474
475/*
476 * Common length and type checks are done here,
477 * then the protocol-specific routine is called.
478 */
4599cf19 479static void
9eeaa8a9 480arpintr(struct netmsg *msg)
984263bc 481{
9eeaa8a9 482 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
2256ba69 483 struct arphdr *ar;
8bde602d 484 u_short ar_hrd;
984263bc 485
8bde602d 486 if (m->m_len < sizeof(struct arphdr) &&
db597c8a 487 (m = m_pullup(m, sizeof(struct arphdr))) == NULL) {
8bde602d 488 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
db597c8a 489 return;
8bde602d
JH
490 }
491 ar = mtod(m, struct arphdr *);
492
493 ar_hrd = ntohs(ar->ar_hrd);
db597c8a
SZ
494 if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) {
495 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
8bde602d 496 (unsigned char *)&ar->ar_hrd, "");
db597c8a
SZ
497 m_freem(m);
498 return;
8bde602d 499 }
984263bc 500
db597c8a
SZ
501 if (m->m_pkthdr.len < arphdr_len(ar)) {
502 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
503 log(LOG_ERR, "arp: runt packet\n");
504 return;
505 }
506 ar = mtod(m, struct arphdr *);
8bde602d
JH
507 }
508
509 switch (ntohs(ar->ar_pro)) {
984263bc 510#ifdef INET
db597c8a
SZ
511 case ETHERTYPE_IP:
512 in_arpinput(m);
513 return;
984263bc 514#endif
984263bc 515 }
8bde602d 516 m_freem(m);
6aad077d 517 /* msg was embedded in the mbuf, do not reply! */
984263bc
MD
518}
519
520#ifdef INET
521/*
522 * ARP for Internet protocols on 10 Mb/s Ethernet.
523 * Algorithm is that given in RFC 826.
524 * In addition, a sanity check is performed on the sender
525 * protocol address, to catch impersonators.
526 * We no longer handle negotiations for use of trailer protocol:
527 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
528 * along with IP replies if we wanted trailers sent to us,
529 * and also sent them in response to IP replies.
530 * This allowed either end to announce the desire to receive
531 * trailer packets.
532 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
533 * but formerly didn't normally send requests.
534 */
535static int log_arp_wrong_iface = 1;
984263bc 536SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
799975d0
SZ
537 &log_arp_wrong_iface, 0,
538 "log arp packets arriving on the wrong interface");
984263bc 539
ecdefdda
MD
540static void
541arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
542 boolean_t dologging)
543{
544 struct arphdr *ah = mtod(m, struct arphdr *);
545 struct ifnet *ifp = m->m_pkthdr.rcvif;
546 struct llinfo_arp *la;
547 struct sockaddr_dl *sdl;
548 struct rtentry *rt;
549 int cpu = mycpuid;
550
551 la = arplookup(saddr, create, FALSE);
552 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
553 struct in_addr isaddr = { saddr };
554
555 /* the following is not an error when doing bridging */
a8d45119 556 if (rt->rt_ifp != ifp) {
ecdefdda
MD
557 if (dologging && log_arp_wrong_iface && cpu == 0) {
558 log(LOG_ERR,
559 "arp: %s is on %s "
560 "but got reply from %*D on %s\n",
561 inet_ntoa(isaddr),
562 rt->rt_ifp->if_xname,
563 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
564 ifp->if_xname);
565 }
566 return;
567 }
568 if (sdl->sdl_alen &&
569 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
570 if (rt->rt_expire != 0) {
571 if (dologging && cpu == 0) {
572 log(LOG_INFO,
573 "arp: %s moved from %*D to %*D on %s\n",
574 inet_ntoa(isaddr),
575 ifp->if_addrlen, (u_char *)LLADDR(sdl),
576 ":", ifp->if_addrlen,
577 (u_char *)ar_sha(ah), ":",
578 ifp->if_xname);
579 }
580 } else {
581 if (dologging && cpu == 0) {
582 log(LOG_ERR,
799975d0
SZ
583 "arp: %*D attempts to modify "
584 "permanent entry for %s on %s\n",
585 ifp->if_addrlen, (u_char *)ar_sha(ah),
586 ":", inet_ntoa(isaddr), ifp->if_xname);
ecdefdda
MD
587 }
588 return;
589 }
590 }
591 /*
592 * sanity check for the address length.
593 * XXX this does not work for protocols with variable address
594 * length. -is
595 */
596 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln &&
799975d0 597 cpu == 0) {
ecdefdda
MD
598 log(LOG_WARNING,
599 "arp from %*D: new addr len %d, was %d",
600 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
601 ah->ar_hln, sdl->sdl_alen);
602 }
603 if (ifp->if_addrlen != ah->ar_hln) {
604 if (dologging && cpu == 0) {
605 log(LOG_WARNING,
799975d0
SZ
606 "arp from %*D: addr len: new %d, i/f %d "
607 "(ignored)",
ecdefdda
MD
608 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
609 ah->ar_hln, ifp->if_addrlen);
610 }
611 return;
612 }
613 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
ecdefdda
MD
614 if (rt->rt_expire != 0)
615 rt->rt_expire = time_second + arpt_keep;
616 rt->rt_flags &= ~RTF_REJECT;
617 la->la_asked = 0;
618 la->la_preempt = arp_maxtries;
619
620 /*
621 * This particular cpu might have been holding an mbuf
622 * pending ARP resolution. If so, transmit the mbuf now.
623 */
624 if (la->la_hold != NULL) {
625 m_adj(la->la_hold, sizeof(struct ether_header));
9db4b353 626 ifp->if_output(ifp, la->la_hold, rt_key(rt), rt);
ecdefdda
MD
627 la->la_hold = NULL;
628 }
629 }
630}
631
632#ifdef SMP
633
634struct netmsg_arp_update {
4599cf19 635 struct netmsg netmsg;
ecdefdda
MD
636 struct mbuf *m;
637 in_addr_t saddr;
638 boolean_t create;
639};
640
799975d0 641static void arp_update_msghandler(struct netmsg *);
ecdefdda
MD
642
643#endif
644
645/*
646 * Called from arpintr() - this routine is run from a single cpu.
647 */
984263bc 648static void
f23061d4 649in_arpinput(struct mbuf *m)
984263bc 650{
2256ba69
RG
651 struct arphdr *ah;
652 struct ifnet *ifp = m->m_pkthdr.rcvif;
984263bc 653 struct ether_header *eh;
2256ba69 654 struct rtentry *rt;
b2632176 655 struct ifaddr_container *ifac;
f8983475 656 struct in_ifaddr_container *iac;
984263bc 657 struct in_ifaddr *ia;
984263bc
MD
658 struct sockaddr sa;
659 struct in_addr isaddr, itaddr, myaddr;
ecdefdda
MD
660#ifdef SMP
661 struct netmsg_arp_update msg;
662#endif
0d16ba1d 663 u_int8_t *enaddr = NULL;
ecdefdda 664 int op;
984263bc
MD
665 int req_len;
666
667 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
668 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
669 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
670 return;
671 }
672
673 ah = mtod(m, struct arphdr *);
674 op = ntohs(ah->ar_op);
f23061d4
JH
675 memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
676 memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
984263bc 677 /*
ed5bc924
SS
678 * Check both target and sender IP addresses:
679 *
680 * If we receive the packet on the interface owning the address,
681 * then accept the address.
682 *
683 * For a bridge, we accept the address if the receive interface and
684 * the interface owning the address are on the same bridge.
685 * (This will change slightly when we have clusters of interfaces).
984263bc 686 */
f8983475
SZ
687 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
688 ia = iac->ia;
689
ed5bc924
SS
690 /* Skip all ia's which don't match */
691 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
692 continue;
693
694 if (ia->ia_ifp == ifp)
695 goto match;
696
ac93838f 697 if (ifp->if_bridge && ia->ia_ifp &&
ed5bc924 698 ifp->if_bridge == ia->ia_ifp->if_bridge)
ac93838f 699 goto match;
0d16ba1d
MD
700
701#ifdef CARP
799975d0
SZ
702 /*
703 * If the interface does not match, but the recieving interface
704 * is part of carp, we call carp_iamatch to see if this is a
705 * request for the virtual host ip.
706 * XXX: This is really ugly!
707 */
708 if (ifp->if_carp != NULL &&
0d16ba1d
MD
709 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
710 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
799975d0 711 goto match;
0d16ba1d 712#endif
ac93838f 713 }
f8983475
SZ
714 LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) {
715 ia = iac->ia;
716
ed5bc924
SS
717 /* Skip all ia's which don't match */
718 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
719 continue;
720
721 if (ia->ia_ifp == ifp)
722 goto match;
723
ac93838f 724 if (ifp->if_bridge && ia->ia_ifp &&
ed5bc924 725 ifp->if_bridge == ia->ia_ifp->if_bridge)
ac93838f 726 goto match;
ac93838f 727 }
984263bc
MD
728 /*
729 * No match, use the first inet address on the receive interface
730 * as a dummy address for the rest of the function.
731 */
b2632176
SZ
732 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
733 struct ifaddr *ifa = ifac->ifa;
734
984263bc
MD
735 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
736 ia = ifatoia(ifa);
737 goto match;
738 }
984263bc 739 }
ed5bc924
SS
740 /*
741 * If we got here, we didn't find any suitable interface,
742 * so drop the packet.
743 */
744 m_freem(m);
745 return;
746
984263bc 747match:
0d16ba1d
MD
748 if (!enaddr)
749 enaddr = (u_int8_t *)IF_LLADDR(ifp);
984263bc 750 myaddr = ia->ia_addr.sin_addr;
0d16ba1d 751 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
984263bc
MD
752 m_freem(m); /* it's from me, ignore it. */
753 return;
754 }
c401f0fd 755 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
984263bc
MD
756 log(LOG_ERR,
757 "arp: link address is broadcast for IP address %s!\n",
758 inet_ntoa(isaddr));
759 m_freem(m);
760 return;
761 }
c3749f74 762 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
984263bc
MD
763 log(LOG_ERR,
764 "arp: %*D is using my IP address %s!\n",
765 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
766 inet_ntoa(isaddr));
767 itaddr = myaddr;
768 goto reply;
769 }
ecdefdda 770#ifdef SMP
4599cf19
MD
771 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
772 arp_update_msghandler);
ecdefdda
MD
773 msg.m = m;
774 msg.saddr = isaddr.s_addr;
775 msg.create = (itaddr.s_addr == myaddr.s_addr);
a22c590e 776 lwkt_domsg(rtable_portfn(0), &msg.netmsg.nm_lmsg, 0);
ecdefdda
MD
777#endif
778 arp_update_oncpu(m, isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr),
779 TRUE);
984263bc
MD
780reply:
781 if (op != ARPOP_REQUEST) {
782 m_freem(m);
783 return;
784 }
785 if (itaddr.s_addr == myaddr.s_addr) {
786 /* I am the target */
f23061d4 787 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
0d16ba1d 788 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
984263bc 789 } else {
ecdefdda
MD
790 struct llinfo_arp *la;
791
f23061d4 792 la = arplookup(itaddr.s_addr, FALSE, SIN_PROXY);
984263bc
MD
793 if (la == NULL) {
794 struct sockaddr_in sin;
795
796 if (!arp_proxyall) {
797 m_freem(m);
798 return;
799 }
800
801 bzero(&sin, sizeof sin);
802 sin.sin_family = AF_INET;
803 sin.sin_len = sizeof sin;
804 sin.sin_addr = itaddr;
805
f3ed2586 806 rt = rtpurelookup((struct sockaddr *)&sin);
0c3c561c 807 if (rt == NULL) {
984263bc
MD
808 m_freem(m);
809 return;
810 }
be23faf1 811 --rt->rt_refcnt;
984263bc
MD
812 /*
813 * Don't send proxies for nodes on the same interface
814 * as this one came out of, or we'll get into a fight
815 * over who claims what Ether address.
816 */
817 if (rt->rt_ifp == ifp) {
984263bc
MD
818 m_freem(m);
819 return;
820 }
f23061d4 821 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
0d16ba1d 822 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
984263bc 823#ifdef DEBUG_PROXY
a6ec04bc 824 kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
984263bc
MD
825#endif
826 } else {
ecdefdda
MD
827 struct sockaddr_dl *sdl;
828
984263bc 829 rt = la->la_rt;
f23061d4 830 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
984263bc 831 sdl = SDL(rt->rt_gateway);
f23061d4 832 memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
984263bc
MD
833 }
834 }
835
f23061d4
JH
836 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
837 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
984263bc
MD
838 ah->ar_op = htons(ARPOP_REPLY);
839 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
840 switch (ifp->if_type) {
984263bc 841 case IFT_ETHER:
984263bc
MD
842 /*
843 * May not be correct for types not explictly
844 * listed, but it is our best guess.
845 */
846 default:
847 eh = (struct ether_header *)sa.sa_data;
0c3c561c 848 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
984263bc
MD
849 eh->ether_type = htons(ETHERTYPE_ARP);
850 break;
851 }
852 sa.sa_family = AF_UNSPEC;
0c3c561c 853 sa.sa_len = sizeof sa;
799975d0 854 ifp->if_output(ifp, m, &sa, NULL);
984263bc 855}
ecdefdda
MD
856
857#ifdef SMP
858
799975d0 859static void
4599cf19 860arp_update_msghandler(struct netmsg *netmsg)
ecdefdda 861{
4599cf19 862 struct netmsg_arp_update *msg = (struct netmsg_arp_update *)netmsg;
ecdefdda
MD
863 int nextcpu;
864
865 arp_update_oncpu(msg->m, msg->saddr, msg->create, FALSE);
866
867 nextcpu = mycpuid + 1;
799975d0 868 if (nextcpu < ncpus)
4599cf19 869 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->netmsg.nm_lmsg);
799975d0 870 else
4599cf19 871 lwkt_replymsg(&msg->netmsg.nm_lmsg, 0);
ecdefdda
MD
872}
873
799975d0 874#endif /* SMP */
ecdefdda 875
799975d0 876#endif /* INET */
984263bc
MD
877
878/*
03f869ef
MD
879 * Free an arp entry. If the arp entry is actively referenced or represents
880 * a static entry we only clear it back to an unresolved state, otherwise
881 * we destroy the entry entirely.
882 *
883 * Note that static entries are created when route add ... -interface is used
884 * to create an interface route to a (direct) destination.
984263bc
MD
885 */
886static void
5fe66e68 887arptfree(struct llinfo_arp *la)
984263bc 888{
2256ba69
RG
889 struct rtentry *rt = la->la_rt;
890 struct sockaddr_dl *sdl;
f23061d4 891
03f869ef 892 if (rt == NULL)
984263bc 893 panic("arptfree");
03f869ef 894 sdl = SDL(rt->rt_gateway);
5fe66e68
JH
895 if (sdl != NULL &&
896 ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
897 (rt->rt_flags & RTF_STATIC))) {
984263bc
MD
898 sdl->sdl_alen = 0;
899 la->la_preempt = la->la_asked = 0;
900 rt->rt_flags &= ~RTF_REJECT;
901 return;
902 }
f23061d4 903 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
984263bc 904}
0c3c561c 905
984263bc
MD
906/*
907 * Lookup or enter a new address in arptab.
908 */
909static struct llinfo_arp *
f23061d4 910arplookup(in_addr_t addr, boolean_t create, boolean_t proxy)
984263bc 911{
2256ba69 912 struct rtentry *rt;
f3ed2586 913 struct sockaddr_inarp sin = { sizeof sin, AF_INET };
f23061d4 914 const char *why = NULL;
984263bc
MD
915
916 sin.sin_addr.s_addr = addr;
917 sin.sin_other = proxy ? SIN_PROXY : 0;
f3ed2586
JH
918 if (create)
919 rt = rtlookup((struct sockaddr *)&sin);
920 else
921 rt = rtpurelookup((struct sockaddr *)&sin);
f23061d4
JH
922 if (rt == NULL)
923 return (NULL);
984263bc
MD
924 rt->rt_refcnt--;
925
926 if (rt->rt_flags & RTF_GATEWAY)
927 why = "host is not on local network";
f23061d4 928 else if (!(rt->rt_flags & RTF_LLINFO))
984263bc
MD
929 why = "could not allocate llinfo";
930 else if (rt->rt_gateway->sa_family != AF_LINK)
931 why = "gateway route is not ours";
932
8fd3e627
MD
933 if (why) {
934 if (create) {
935 log(LOG_DEBUG, "arplookup %s failed: %s\n",
936 inet_ntoa(sin.sin_addr), why);
937 }
f23061d4
JH
938 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
939 /* No references to this route. Purge it. */
940 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
be23faf1 941 rt_mask(rt), rt->rt_flags, NULL);
8fd3e627 942 }
f23061d4 943 return (NULL);
984263bc 944 }
f23061d4 945 return (rt->rt_llinfo);
984263bc
MD
946}
947
948void
f23061d4 949arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
984263bc 950{
57dff79c
SZ
951 ASSERT_SERIALIZED(ifp->if_serializer);
952
953 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) {
954 lwkt_serialize_exit(ifp->if_serializer);
f23061d4
JH
955 arprequest(ifp, &IA_SIN(ifa)->sin_addr, &IA_SIN(ifa)->sin_addr,
956 IF_LLADDR(ifp));
57dff79c
SZ
957 lwkt_serialize_enter(ifp->if_serializer);
958 }
984263bc
MD
959 ifa->ifa_rtrequest = arp_rtrequest;
960 ifa->ifa_flags |= RTF_CLONING;
961}
962
0d16ba1d
MD
963void
964arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
965{
57dff79c
SZ
966 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
967
0d16ba1d
MD
968 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY)
969 arprequest(ifp, &IA_SIN(ifa)->sin_addr, &IA_SIN(ifa)->sin_addr,
970 enaddr);
971 ifa->ifa_rtrequest = arp_rtrequest;
972 ifa->ifa_flags |= RTF_CLONING;
973}
974
984263bc
MD
975static void
976arp_init(void)
977{
ecdefdda
MD
978 int cpu;
979
980 for (cpu = 0; cpu < ncpus2; cpu++)
981 LIST_INIT(&llinfo_arp_list[cpu]);
95af0087
SZ
982 netisr_register(NETISR_ARP, cpu0_portfn, arpintr,
983 NETISR_FLAG_NOTMPSAFE);
984263bc
MD
984}
985
986SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);