f927bde80ecd7628fdea89920e3c53d6b9967467
[dragonfly.git] / sys / netinet / if_ether.c
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
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. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *      @(#)if_ether.c  8.1 (Berkeley) 6/10/93
62  * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
63  */
64
65 /*
66  * Ethernet address resolution protocol.
67  * TODO:
68  *      add "inuse/lock" bit (or ref. count) along with valid bit
69  */
70
71 #include "opt_inet.h"
72 #include "opt_carp.h"
73
74 #include <sys/param.h>
75 #include <sys/kernel.h>
76 #include <sys/queue.h>
77 #include <sys/sysctl.h>
78 #include <sys/systm.h>
79 #include <sys/mbuf.h>
80 #include <sys/malloc.h>
81 #include <sys/socket.h>
82 #include <sys/syslog.h>
83 #include <sys/lock.h>
84
85 #include <net/if.h>
86 #include <net/if_dl.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
89 #include <net/netisr.h>
90 #include <net/if_llc.h>
91
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <netinet/if_ether.h>
95
96 #include <sys/thread2.h>
97 #include <sys/msgport2.h>
98 #include <net/netmsg2.h>
99 #include <net/netisr2.h>
100 #include <sys/mplock2.h>
101
102 #ifdef CARP
103 #include <netinet/ip_carp.h>
104 #endif
105
106 #define SIN(s) ((struct sockaddr_in *)s)
107 #define SDL(s) ((struct sockaddr_dl *)s)
108
109 MALLOC_DEFINE(M_ARP, "arp", "ARP");
110
111 SYSCTL_DECL(_net_link_ether);
112 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
113
114 /* timer values */
115 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
116 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
117 static int arpt_down = 20;      /* once declared down, don't send for 20 sec */
118
119 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
120            &arpt_prune, 0, "");
121 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
122            &arpt_keep, 0, "");
123 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
124            &arpt_down, 0, "");
125
126 #define rt_expire       rt_rmx.rmx_expire
127
128 struct llinfo_arp {
129         LIST_ENTRY(llinfo_arp) la_le;
130         struct  rtentry *la_rt;
131         struct  mbuf *la_hold;  /* last packet until resolved/timeout */
132         u_short la_preempt;     /* countdown for pre-expiry arps */
133         u_short la_asked;       /* #times we QUERIED following expiration */
134 };
135
136 static int      arp_maxtries = 5;
137 static int      useloopback = 1; /* use loopback interface for local traffic */
138 static int      arp_proxyall = 0;
139 static int      arp_refresh = 60; /* refresh arp cache ~60 (not impl yet) */
140 static int      arp_restricted_match = 0;
141 static int      arp_ignore_probes = 1;
142
143 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
144            &arp_maxtries, 0, "ARP resolution attempts before returning error");
145 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
146            &useloopback, 0, "Use the loopback interface for local traffic");
147 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
148            &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
149 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, restricted_match, CTLFLAG_RW,
150            &arp_restricted_match, 0, "Only match against the sender");
151 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, refresh, CTLFLAG_RW,
152            &arp_refresh, 0, "Preemptively refresh the ARP");
153 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, ignore_probes, CTLFLAG_RW,
154            &arp_ignore_probes, 0, "Ignore ARP probes");
155
156 static void     arp_rtrequest(int, struct rtentry *);
157 static void     arprequest(struct ifnet *, const struct in_addr *,
158                            const struct in_addr *, const u_char *);
159 static void     arprequest_async(struct ifnet *, const struct in_addr *,
160                                  const struct in_addr *, const u_char *);
161 static void     arpintr(netmsg_t msg);
162 static void     arptfree(struct llinfo_arp *);
163 static void     arptimer(void *);
164 static struct llinfo_arp *
165                 arplookup(in_addr_t, boolean_t, boolean_t);
166 #ifdef INET
167 static void     in_arpinput(struct mbuf *);
168 static void     in_arpreply(struct mbuf *m, in_addr_t, in_addr_t);
169 static void     arp_update_msghandler(netmsg_t);
170 static void     arp_reply_msghandler(netmsg_t);
171 #endif
172
173 struct arp_pcpu_data {
174         LIST_HEAD(, llinfo_arp) llinfo_list;
175         struct callout          timer_ch;
176         struct netmsg_base      timer_nmsg;
177 };
178
179 static struct arp_pcpu_data     *arp_data[MAXCPU];
180
181 /*
182  * Timeout routine.  Age arp_tab entries periodically.
183  */
184 static void
185 arptimer_dispatch(netmsg_t nmsg)
186 {
187         struct arp_pcpu_data *ad = nmsg->lmsg.u.ms_resultp;
188         struct llinfo_arp *la, *nla;
189
190         ASSERT_NETISR_NCPUS(mycpuid);
191
192         /* Reply ASAP */
193         crit_enter();
194         netisr_replymsg(&nmsg->base, 0);
195         crit_exit();
196
197         LIST_FOREACH_MUTABLE(la, &ad->llinfo_list, la_le, nla) {
198                 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_uptime)
199                         arptfree(la);
200         }
201         callout_reset(&ad->timer_ch, arpt_prune * hz, arptimer, &ad->timer_nmsg);
202 }
203
204 static void
205 arptimer(void *xnm)
206 {
207         struct netmsg_base *nm = xnm;
208
209         KKASSERT(mycpuid < netisr_ncpus);
210
211         crit_enter();
212         if (nm->lmsg.ms_flags & MSGF_DONE)
213                 netisr_sendmsg_oncpu(nm);
214         crit_exit();
215 }
216
217 /*
218  * Parallel to llc_rtrequest.
219  *
220  * Called after a route is successfully added to the tree to fix-up the
221  * route and initiate arp operations if required.
222  */
223 static void
224 arp_rtrequest(int req, struct rtentry *rt)
225 {
226         struct sockaddr *gate = rt->rt_gateway;
227         struct llinfo_arp *la = rt->rt_llinfo;
228
229         struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
230
231         if (rt->rt_flags & RTF_GATEWAY)
232                 return;
233
234         switch (req) {
235         case RTM_ADD:
236                 /*
237                  * XXX: If this is a manually added route to interface
238                  * such as older version of routed or gated might provide,
239                  * restore cloning bit.
240                  */
241                 if (!(rt->rt_flags & RTF_HOST) &&
242                     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
243                         rt->rt_flags |= RTF_CLONING;
244                 if (rt->rt_flags & RTF_CLONING) {
245                         /*
246                          * Case 1: This route should come from a route to iface.
247                          */
248                         rt_setgate(rt, rt_key(rt),
249                                    (struct sockaddr *)&null_sdl);
250                         gate = rt->rt_gateway;
251                         SDL(gate)->sdl_type = rt->rt_ifp->if_type;
252                         SDL(gate)->sdl_index = rt->rt_ifp->if_index;
253                         rt->rt_expire = time_uptime;
254                         break;
255                 }
256                 /*
257                  * Announce a new entry if requested, and only announce it
258                  * once on cpu0.
259                  */
260                 if ((rt->rt_flags & RTF_ANNOUNCE) && mycpuid == 0) {
261                         arprequest(rt->rt_ifp,
262                             &SIN(rt_key(rt))->sin_addr,
263                             &SIN(rt_key(rt))->sin_addr,
264                             LLADDR(SDL(gate)));
265                 }
266                 /*FALLTHROUGH*/
267         case RTM_RESOLVE:
268                 if (gate->sa_family != AF_LINK ||
269                     gate->sa_len < sizeof(struct sockaddr_dl)) {
270                         log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
271                         break;
272                 }
273                 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
274                 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
275                 if (la != NULL)
276                         break; /* This happens on a route change */
277                 /*
278                  * Case 2:  This route may come from cloning, or a manual route
279                  * add with a LL address.
280                  */
281                 R_Malloc(la, struct llinfo_arp *, sizeof *la);
282                 rt->rt_llinfo = la;
283                 if (la == NULL) {
284                         log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
285                         break;
286                 }
287                 bzero(la, sizeof *la);
288                 la->la_rt = rt;
289                 rt->rt_flags |= RTF_LLINFO;
290                 LIST_INSERT_HEAD(&arp_data[mycpuid]->llinfo_list, la, la_le);
291
292 #ifdef INET
293                 /*
294                  * This keeps the multicast addresses from showing up
295                  * in `arp -a' listings as unresolved.  It's not actually
296                  * functional.  Then the same for broadcast.
297                  */
298                 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
299                         ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
300                                                LLADDR(SDL(gate)));
301                         SDL(gate)->sdl_alen = 6;
302                         rt->rt_expire = 0;
303                 }
304                 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
305                         memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
306                                rt->rt_ifp->if_addrlen);
307                         SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
308                         rt->rt_expire = 0;
309                 }
310 #endif
311
312                 /*
313                  * This fixes up the routing interface for local addresses.
314                  * The route is adjusted to point at lo0 and the expiration
315                  * timer is disabled.
316                  *
317                  * NOTE: This prevents locally targetted traffic from going
318                  *       out the hardware interface, which is inefficient
319                  *       and might not work if the hardware cannot listen
320                  *       to its own transmitted packets.   Setting
321                  *       net.link.ether.inet.useloopback to 0 will force
322                  *       packets for local addresses out the hardware (and
323                  *       it is expected to receive its own packet).
324                  *
325                  * XXX We should just be able to test RTF_LOCAL here instead
326                  *     of having to compare IPs.
327                  */
328                 if (SIN(rt_key(rt))->sin_addr.s_addr ==
329                     (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
330                         rt->rt_expire = 0;
331                         bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
332                               SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
333                         if (useloopback)
334                                 rt->rt_ifp = loif;
335                 }
336                 break;
337
338         case RTM_DELETE:
339                 if (la == NULL)
340                         break;
341                 LIST_REMOVE(la, la_le);
342                 rt->rt_llinfo = NULL;
343                 rt->rt_flags &= ~RTF_LLINFO;
344                 if (la->la_hold != NULL)
345                         m_freem(la->la_hold);
346                 Free(la);
347                 break;
348         }
349 }
350
351 static struct mbuf *
352 arpreq_alloc(struct ifnet *ifp, const struct in_addr *sip,
353              const struct in_addr *tip, const u_char *enaddr)
354 {
355         struct mbuf *m;
356         struct arphdr *ah;
357         u_short ar_hrd;
358
359         if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
360                 return NULL;
361         m->m_pkthdr.rcvif = NULL;
362
363         switch (ifp->if_type) {
364         case IFT_ETHER:
365                 /*
366                  * This may not be correct for types not explicitly
367                  * listed, but this is our best guess
368                  */
369         default:
370                 ar_hrd = htons(ARPHRD_ETHER);
371
372                 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
373                 m->m_pkthdr.len = m->m_len;
374                 MH_ALIGN(m, m->m_len);
375
376                 ah = mtod(m, struct arphdr *);
377                 break;
378         }
379
380         ah->ar_hrd = ar_hrd;
381         ah->ar_pro = htons(ETHERTYPE_IP);
382         ah->ar_hln = ifp->if_addrlen;           /* hardware address length */
383         ah->ar_pln = sizeof(struct in_addr);    /* protocol address length */
384         ah->ar_op = htons(ARPOP_REQUEST);
385         memcpy(ar_sha(ah), enaddr, ah->ar_hln);
386         memset(ar_tha(ah), 0, ah->ar_hln);
387         memcpy(ar_spa(ah), sip, ah->ar_pln);
388         memcpy(ar_tpa(ah), tip, ah->ar_pln);
389
390         return m;
391 }
392
393 static void
394 arpreq_send(struct ifnet *ifp, struct mbuf *m)
395 {
396         struct sockaddr sa;
397         struct ether_header *eh;
398
399         ASSERT_NETISR_NCPUS(mycpuid);
400
401         switch (ifp->if_type) {
402         case IFT_ETHER:
403                 /*
404                  * This may not be correct for types not explicitly
405                  * listed, but this is our best guess
406                  */
407         default:
408                 eh = (struct ether_header *)sa.sa_data;
409                 /* if_output() will not swap */
410                 eh->ether_type = htons(ETHERTYPE_ARP);
411                 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
412                 break;
413         }
414
415         sa.sa_family = AF_UNSPEC;
416         sa.sa_len = sizeof(sa);
417         ifp->if_output(ifp, m, &sa, NULL);
418 }
419
420 static void
421 arpreq_send_handler(netmsg_t msg)
422 {
423         struct mbuf *m = msg->packet.nm_packet;
424         struct ifnet *ifp = msg->lmsg.u.ms_resultp;
425
426         arpreq_send(ifp, m);
427         /* nmsg was embedded in the mbuf, do not reply! */
428 }
429
430 /*
431  * Broadcast an ARP request. Caller specifies:
432  *      - arp header source ip address
433  *      - arp header target ip address
434  *      - arp header source ethernet address
435  *
436  * NOTE: Caller MUST NOT hold ifp's serializer
437  */
438 static void
439 arprequest(struct ifnet *ifp, const struct in_addr *sip,
440            const struct in_addr *tip, const u_char *enaddr)
441 {
442         struct mbuf *m;
443
444         ASSERT_NETISR_NCPUS(mycpuid);
445
446         if (enaddr == NULL) {
447                 if (ifp->if_bridge) {
448                         enaddr = IF_LLADDR(ether_bridge_interface(ifp));
449                 } else {
450                         enaddr = IF_LLADDR(ifp);
451                 }
452         }
453
454         m = arpreq_alloc(ifp, sip, tip, enaddr);
455         if (m == NULL)
456                 return;
457         arpreq_send(ifp, m);
458 }
459
460 /*
461  * Same as arprequest(), except:
462  * - Caller is allowed to hold ifp's serializer
463  * - Network output is done in protocol thead
464  */
465 static void
466 arprequest_async(struct ifnet *ifp, const struct in_addr *sip,
467                  const struct in_addr *tip, const u_char *enaddr)
468 {
469         struct mbuf *m;
470         struct netmsg_packet *pmsg;
471         int cpu;
472
473         if (enaddr == NULL) {
474                 if (ifp->if_bridge) {
475                         enaddr = IF_LLADDR(ether_bridge_interface(ifp));
476                 } else {
477                         enaddr = IF_LLADDR(ifp);
478                 }
479         }
480         m = arpreq_alloc(ifp, sip, tip, enaddr);
481         if (m == NULL)
482                 return;
483
484         pmsg = &m->m_hdr.mh_netmsg;
485         netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
486                     0, arpreq_send_handler);
487         pmsg->nm_packet = m;
488         pmsg->base.lmsg.u.ms_resultp = ifp;
489
490         if (mycpuid < netisr_ncpus)
491                 cpu = mycpuid;
492         else
493                 cpu = 0;
494         lwkt_sendmsg(netisr_cpuport(cpu), &pmsg->base.lmsg);
495 }
496
497 /*
498  * Resolve an IP address into an ethernet address.  If success,
499  * desten is filled in.  If there is no entry in arptab,
500  * set one up and broadcast a request for the IP address.
501  * Hold onto this mbuf and resend it once the address
502  * is finally resolved.  A return value of 1 indicates
503  * that desten has been filled in and the packet should be sent
504  * normally; a 0 return indicates that the packet has been
505  * taken over here, either now or for later transmission.
506  */
507 int
508 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
509            struct sockaddr *dst, u_char *desten)
510 {
511         struct rtentry *rt = NULL;
512         struct llinfo_arp *la = NULL;
513         struct sockaddr_dl *sdl;
514
515         if (m->m_flags & M_BCAST) {     /* broadcast */
516                 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
517                 return (1);
518         }
519         if (m->m_flags & M_MCAST) {/* multicast */
520                 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
521                 return (1);
522         }
523         if (rt0 != NULL) {
524                 if (rt_llroute(dst, rt0, &rt) != 0) {
525                         m_freem(m);
526                         return 0;
527                 }
528                 la = rt->rt_llinfo;
529         }
530         if (la == NULL) {
531                 la = arplookup(SIN(dst)->sin_addr.s_addr, TRUE, FALSE);
532                 if (la != NULL)
533                         rt = la->la_rt;
534         }
535         if (la == NULL || rt == NULL) {
536                 char addr[INET_ADDRSTRLEN];
537
538                 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
539                     kinet_ntoa(SIN(dst)->sin_addr, addr), la ? "la" : " ",
540                     rt ? "rt" : "");
541                 m_freem(m);
542                 return (0);
543         }
544         sdl = SDL(rt->rt_gateway);
545         /*
546          * Check the address family and length is valid, the address
547          * is resolved; otherwise, try to resolve.
548          */
549         if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
550             sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
551                 /*
552                  * If entry has an expiry time and it is approaching,
553                  * see if we need to send an ARP request within this
554                  * arpt_down interval.
555                  */
556                 if ((rt->rt_expire != 0) &&
557                     (time_uptime + la->la_preempt > rt->rt_expire)) {
558                         arprequest(ifp,
559                                    &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
560                                    &SIN(dst)->sin_addr,
561                                    NULL);
562                         la->la_preempt--;
563                 }
564
565                 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
566                 return 1;
567         }
568         /*
569          * If ARP is disabled or static on this interface, stop.
570          * XXX
571          * Probably should not allocate empty llinfo struct if we are
572          * not going to be sending out an arp request.
573          */
574         if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
575                 m_freem(m);
576                 return (0);
577         }
578         /*
579          * There is an arptab entry, but no ethernet address
580          * response yet.  Replace the held mbuf with this
581          * latest one.
582          */
583         if (la->la_hold != NULL)
584                 m_freem(la->la_hold);
585         la->la_hold = m;
586         if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
587                 rt->rt_flags &= ~RTF_REJECT;
588                 if (la->la_asked == 0 || rt->rt_expire != time_uptime) {
589                         rt->rt_expire = time_uptime;
590                         arprequest(ifp,
591                                    &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
592                                    &SIN(dst)->sin_addr,
593                                    NULL);
594                         if (la->la_asked++ >= arp_maxtries) {
595                                 rt->rt_expire += arpt_down;
596                                 la->la_preempt = arp_maxtries;
597                                 rt_rtmsg(RTM_MISS, rt, rt->rt_ifp, 0);
598                         }
599                 }
600         }
601         return (0);
602 }
603
604 /*
605  * Common length and type checks are done here,
606  * then the protocol-specific routine is called.
607  */
608 static void
609 arpintr(netmsg_t msg)
610 {
611         struct mbuf *m = msg->packet.nm_packet;
612         struct arphdr *ar;
613         u_short ar_hrd;
614         char hexstr[6];
615
616         if (m->m_len < sizeof(struct arphdr) &&
617             (m = m_pullup(m, sizeof(struct arphdr))) == NULL) {
618                 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
619                 return;
620         }
621         ar = mtod(m, struct arphdr *);
622
623         ar_hrd = ntohs(ar->ar_hrd);
624         if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) {
625                 hexncpy((unsigned char *)&ar->ar_hrd, 2, hexstr, 5, NULL);
626                 log(LOG_ERR, "arp: unknown hardware address format (0x%s)\n",
627                     hexstr);
628                 m_freem(m);
629                 return;
630         }
631
632         if (m->m_pkthdr.len < arphdr_len(ar)) {
633                 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
634                         log(LOG_ERR, "arp: runt packet\n");
635                         return;
636                 }
637                 ar = mtod(m, struct arphdr *);
638         }
639
640         switch (ntohs(ar->ar_pro)) {
641 #ifdef INET
642         case ETHERTYPE_IP:
643                 in_arpinput(m);
644                 return;
645 #endif
646         }
647         m_freem(m);
648         /* msg was embedded in the mbuf, do not reply! */
649 }
650
651 #ifdef INET
652 /*
653  * ARP for Internet protocols on 10 Mb/s Ethernet.
654  * Algorithm is that given in RFC 826.
655  * In addition, a sanity check is performed on the sender
656  * protocol address, to catch impersonators.
657  * We no longer handle negotiations for use of trailer protocol:
658  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
659  * along with IP replies if we wanted trailers sent to us,
660  * and also sent them in response to IP replies.
661  * This allowed either end to announce the desire to receive
662  * trailer packets.
663  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
664  * but formerly didn't normally send requests.
665  */
666
667 static int      log_arp_wrong_iface = 1;
668 static int      log_arp_movements = 1;
669 static int      log_arp_permanent_modify = 1;
670 static int      log_arp_creation_failure = 1;
671
672 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
673            &log_arp_wrong_iface, 0,
674            "Log arp packets arriving on the wrong interface");
675 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
676            &log_arp_movements, 0,
677            "Log arp replies from MACs different than the one in the cache");
678 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
679            &log_arp_permanent_modify, 0,
680            "Log arp replies from MACs different than the one "
681            "in the permanent arp entry");
682 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_creation_failure, CTLFLAG_RW,
683            &log_arp_creation_failure, 0, "Log arp creation failure");
684
685 /*
686  * Returns non-zero if the routine updated anything.
687  */
688 static int
689 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
690                  boolean_t dologging)
691 {
692         struct arphdr *ah = mtod(m, struct arphdr *);
693         struct ifnet *ifp = m->m_pkthdr.rcvif;
694         struct llinfo_arp *la;
695         struct sockaddr_dl *sdl;
696         struct rtentry *rt;
697         char hexstr[2][64];
698         char sbuf[INET_ADDRSTRLEN];
699         int changed = create;
700
701         KASSERT(curthread->td_type == TD_TYPE_NETISR,
702             ("arp update not in netisr"));
703
704         la = arplookup(saddr, create, FALSE);
705         if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
706                 struct in_addr isaddr = { saddr };
707                 int rt_cmd = sdl->sdl_alen == 0 ? RTM_ADD : RTM_CHANGE;
708                 bool do_rtmsg = false;
709
710                 /*
711                  * Normally arps coming in on the wrong interface are ignored,
712                  * but if we are bridging and the two interfaces belong to
713                  * the same bridge, or one is a member of the bridge which
714                  * is the other, then it isn't an error.
715                  */
716                 if (rt->rt_ifp != ifp) {
717                         /*
718                          * (1) ifp and rt_ifp both members of same bridge
719                          * (2) rt_ifp member of bridge ifp
720                          * (3) ifp member of bridge rt_ifp
721                          *
722                          * Always replace rt_ifp with the bridge ifc.
723                          */
724                         struct ifnet *nifp;
725
726                         if (ifp->if_bridge &&
727                             rt->rt_ifp->if_bridge == ifp->if_bridge) {
728                                 nifp = ether_bridge_interface(ifp);
729                         } else if (rt->rt_ifp->if_bridge &&
730                                    ether_bridge_interface(rt->rt_ifp) == ifp) {
731                                 nifp = ifp;
732                         } else if (ifp->if_bridge &&
733                                    ether_bridge_interface(ifp) == rt->rt_ifp) {
734                                 nifp = rt->rt_ifp;
735                         } else {
736                                 nifp = NULL;
737                         }
738
739                         if ((log_arp_wrong_iface == 1 && nifp == NULL) ||
740                             log_arp_wrong_iface == 2) {
741                                 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
742                                     hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
743                                 log(LOG_ERR,
744                                     "arp: %s is on %s "
745                                     "but got reply from %s on %s\n",
746                                     kinet_ntoa(isaddr, sbuf),
747                                     rt->rt_ifp->if_xname, hexstr[0],
748                                     ifp->if_xname);
749                         }
750                         if (nifp == NULL)
751                                 return 0;
752
753                         /*
754                          * nifp is our man!  Replace rt_ifp and adjust
755                          * the sdl.
756                          */
757                         ifp = rt->rt_ifp = nifp;
758                         if (sdl->sdl_type != ifp->if_type) {
759                                 sdl->sdl_type = ifp->if_type;
760                                 changed = 1;
761                                 do_rtmsg = true;
762                         }
763                         if (sdl->sdl_index != ifp->if_index) {
764                                 sdl->sdl_index = ifp->if_index;
765                                 changed = 1;
766                                 do_rtmsg = true;
767                         }
768                 }
769                 if (sdl->sdl_alen &&
770                     bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
771                         changed = 1;
772                         if (rt->rt_expire != 0) {
773                                 if (dologging && log_arp_movements) {
774                                         hexncpy((u_char *)LLADDR(sdl), ifp->if_addrlen,
775                                             hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
776                                         hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
777                                             hexstr[1], HEX_NCPYLEN(ifp->if_addrlen), ":");
778                                         log(LOG_INFO,
779                                             "arp: %s moved from %s to %s on %s\n",
780                                             kinet_ntoa(isaddr, sbuf), hexstr[0], hexstr[1],
781                                             ifp->if_xname);
782                                 }
783                         } else {
784                                 if (dologging && log_arp_permanent_modify) {
785                                         hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
786                                             hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
787                                         log(LOG_ERR,
788                                         "arp: %s attempts to modify "
789                                         "permanent entry for %s on %s\n",
790                                         hexstr[0], kinet_ntoa(isaddr, sbuf), ifp->if_xname);
791                                 }
792                                 return changed;
793                         }
794                         do_rtmsg = true;
795                 }
796                 /*
797                  * sanity check for the address length.
798                  * XXX this does not work for protocols with variable address
799                  * length. -is
800                  */
801                 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
802                         hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
803                             hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
804                         log(LOG_WARNING,
805                             "arp from %s: new addr len %d, was %d",
806                             hexstr[0], ah->ar_hln, sdl->sdl_alen);
807                 }
808                 if (ifp->if_addrlen != ah->ar_hln) {
809                         if (dologging) {
810                                 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
811                                     hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
812                                 log(LOG_WARNING,
813                                 "arp from %s: addr len: new %d, i/f %d "
814                                 "(ignored)", hexstr[0],
815                                 ah->ar_hln, ifp->if_addrlen);
816                         }
817                         return changed;
818                 }
819                 if (sdl->sdl_alen == 0)
820                         do_rtmsg = true;
821                 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
822                 if (rt->rt_expire != 0) {
823                         if (rt->rt_expire != time_uptime + arpt_keep &&
824                             rt->rt_expire != time_uptime + arpt_keep - 1) {
825                                 rt->rt_expire = time_uptime + arpt_keep;
826                                 changed = 1;
827                         }
828                 }
829                 if (rt->rt_flags & RTF_REJECT) {
830                         rt->rt_flags &= ~RTF_REJECT;
831                         changed = 1;
832                 }
833                 if (la->la_asked != 0) {
834                         la->la_asked = 0;
835                         changed = 1;
836                 }
837                 if (la->la_preempt != arp_maxtries) {
838                         la->la_preempt = arp_maxtries;
839                         changed = 1;
840                 }
841
842                 /*
843                  * This particular cpu might have been holding an mbuf
844                  * pending ARP resolution.  If so, transmit the mbuf now.
845                  */
846                 if (la->la_hold != NULL) {
847                         struct mbuf *m = la->la_hold;
848
849                         la->la_hold = NULL;
850                         m_adj(m, sizeof(struct ether_header));
851                         ifp->if_output(ifp, m, rt_key(rt), rt);
852                         changed = 1;
853                 }
854
855                 if (do_rtmsg && mycpuid == 0)
856                         rt_rtmsg(rt_cmd, rt, rt->rt_ifp, 0);
857         }
858         return changed;
859 }
860
861 /*
862  * Called from arpintr() - this routine is run from a single cpu.
863  */
864 static void
865 in_arpinput(struct mbuf *m)
866 {
867         struct arphdr *ah;
868         struct ifnet *ifp = m->m_pkthdr.rcvif;
869         struct ifaddr_container *ifac;
870         struct in_ifaddr_container *iac;
871         struct in_ifaddr *ia = NULL;
872         struct in_addr isaddr, itaddr, myaddr;
873         uint8_t *enaddr = NULL;
874         int req_len;
875         int changed;
876         char hexstr[64], sbuf[INET_ADDRSTRLEN];
877
878         req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
879         if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
880                 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
881                 return;
882         }
883
884         ah = mtod(m, struct arphdr *);
885         memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
886         memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
887
888         /*
889          * Check both target and sender IP addresses:
890          *
891          * If we receive the packet on the interface owning the address,
892          * then accept the address.
893          *
894          * For a bridge, we accept the address if the receive interface and
895          * the interface owning the address are on the same bridge, and
896          * use the bridge MAC as the is-at response.  The bridge will be
897          * responsible for handling the packet.
898          *
899          * (0) Check target IP against CARP IPs
900          */
901 #ifdef CARP
902         LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
903                 int is_match = 0, is_parent = 0;
904
905                 ia = iac->ia;
906
907                 /* Skip all ia's which don't match */
908                 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
909                         continue;
910
911                 if (ia->ia_ifp->if_type != IFT_CARP)
912                         continue;
913
914                 if (carp_parent(ia->ia_ifp) == ifp)
915                         is_parent = 1;
916                 if (is_parent || ia->ia_ifp == ifp)
917                         is_match = carp_iamatch(ia);
918
919                 if (is_match) {
920                         if (is_parent) {
921                                 /*
922                                  * The parent interface will also receive
923                                  * the ethernet broadcast packets, e.g. ARP
924                                  * REQUEST, so if we could find a CARP
925                                  * interface of the parent that could match
926                                  * the target IP address, we then drop the
927                                  * packets, which is delieverd to us through
928                                  * the parent interface.
929                                  */
930                                 m_freem(m);
931                                 return;
932                         }
933                         goto match;
934                 }
935         }
936 #endif  /* CARP */
937
938         /*
939          * (1) Check target IP against our local IPs
940          */
941         LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
942                 ia = iac->ia;
943
944                 /* Skip all ia's which don't match */
945                 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
946                         continue;
947
948 #ifdef CARP
949                 /* CARP interfaces are checked in (0) */
950                 if (ia->ia_ifp->if_type == IFT_CARP)
951                         continue;
952 #endif
953
954                 if (ifp->if_bridge && ia->ia_ifp &&
955                     ifp->if_bridge == ia->ia_ifp->if_bridge) {
956                         ifp = ether_bridge_interface(ifp);
957                         goto match;
958                 }
959                 if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
960                     ether_bridge_interface(ia->ia_ifp) == ifp) {
961                         goto match;
962                 }
963                 if (ifp->if_bridge && ether_bridge_interface(ifp) ==
964                     ia->ia_ifp) {
965                         goto match;
966                 }
967                 if (ia->ia_ifp == ifp) {
968                         goto match;
969                 }
970         }
971
972         /*
973          * (2) Check sender IP against our local IPs
974          */
975         LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) {
976                 ia = iac->ia;
977
978                 /* Skip all ia's which don't match */
979                 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
980                         continue;
981
982                 if (ifp->if_bridge && ia->ia_ifp &&
983                     ifp->if_bridge == ia->ia_ifp->if_bridge) {
984                         ifp = ether_bridge_interface(ifp);
985                         goto match;
986                 }
987                 if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
988                     ether_bridge_interface(ia->ia_ifp) == ifp) {
989                         goto match;
990                 }
991                 if (ifp->if_bridge && ether_bridge_interface(ifp) ==
992                     ia->ia_ifp) {
993                         goto match;
994                 }
995
996                 if (ia->ia_ifp == ifp)
997                         goto match;
998         }
999
1000         /*
1001          * No match, use the first inet address on the receive interface
1002          * as a dummy address for the rest of the function.
1003          */
1004         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1005                 struct ifaddr *ifa = ifac->ifa;
1006
1007                 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
1008                         ia = ifatoia(ifa);
1009                         goto match;
1010                 }
1011         }
1012
1013         /*
1014          * If we got here, we didn't find any suitable interface,
1015          * so drop the packet.
1016          */
1017         m_freem(m);
1018         return;
1019
1020 match:
1021         if (!enaddr)
1022                 enaddr = (uint8_t *)IF_LLADDR(ifp);
1023         myaddr = ia->ia_addr.sin_addr;
1024         if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
1025                 m_freem(m);     /* it's from me, ignore it. */
1026                 return;
1027         }
1028         if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
1029                 log(LOG_ERR,
1030                     "arp: link address is broadcast for IP address %s!\n",
1031                     kinet_ntoa(isaddr, sbuf));
1032                 m_freem(m);
1033                 return;
1034         }
1035         if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
1036                 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
1037                     hexstr, HEX_NCPYLEN(ifp->if_addrlen), ":");
1038                 log(LOG_ERR,
1039                    "arp: %s is using my IP address %s!\n",
1040                     hexstr, kinet_ntoa(isaddr, sbuf));
1041                 itaddr = myaddr;
1042                 goto reply;
1043         }
1044         if (ifp->if_flags & IFF_STATICARP)
1045                 goto reply;
1046
1047         /*
1048          * When arp_restricted_match is true and the ARP response is not
1049          * specifically targetted to me, ignore it.  Otherwise the entry
1050          * timeout may be updated for an old MAC.
1051          */
1052         if (arp_restricted_match && itaddr.s_addr != myaddr.s_addr) {
1053                 m_freem(m);
1054                 return;
1055         }
1056
1057         /*
1058          * Update all CPU's routing tables with this ARP packet.
1059          *
1060          * However, we only need to generate rtmsg on CPU0.
1061          */
1062         ASSERT_NETISR0;
1063         changed = arp_update_oncpu(m, isaddr.s_addr,
1064                                    itaddr.s_addr == myaddr.s_addr,
1065                                    TRUE);
1066
1067         if (netisr_ncpus > 1 && changed) {
1068                 struct netmsg_inarp *msg = &m->m_hdr.mh_arpmsg;
1069
1070                 netmsg_init(&msg->base, NULL, &netisr_apanic_rport,
1071                             0, arp_update_msghandler);
1072                 msg->m = m;
1073                 msg->saddr = isaddr.s_addr;
1074                 msg->taddr = itaddr.s_addr;
1075                 msg->myaddr = myaddr.s_addr;
1076                 lwkt_sendmsg(netisr_cpuport(1), &msg->base.lmsg);
1077         } else {
1078                 goto reply;
1079         }
1080
1081         /*
1082          * Just return here; after all CPUs's routing tables are
1083          * properly updated by this ARP packet, an ARP reply will
1084          * be generated if appropriate.
1085          */
1086         return;
1087 reply:
1088         in_arpreply(m, itaddr.s_addr, myaddr.s_addr);
1089 }
1090
1091 static void
1092 arp_reply_msghandler(netmsg_t msg)
1093 {
1094         struct netmsg_inarp *rmsg = (struct netmsg_inarp *)msg;
1095
1096         in_arpreply(rmsg->m, rmsg->taddr, rmsg->myaddr);
1097         /* Don't reply this netmsg; netmsg_inarp is embedded in mbuf */
1098 }
1099
1100 static void
1101 arp_update_msghandler(netmsg_t msg)
1102 {
1103         struct netmsg_inarp *rmsg = (struct netmsg_inarp *)msg;
1104         int nextcpu;
1105
1106         ASSERT_NETISR_NCPUS(mycpuid);
1107
1108         /*
1109          * This message handler will be called on all of the APs;
1110          * no need to generate rtmsg on them.
1111          */
1112         KASSERT(mycpuid > 0, ("arp update msg on cpu%d", mycpuid));
1113         arp_update_oncpu(rmsg->m, rmsg->saddr,
1114                          rmsg->taddr == rmsg->myaddr,
1115                          FALSE);
1116
1117         nextcpu = mycpuid + 1;
1118         if (nextcpu < netisr_ncpus) {
1119                 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
1120         } else {
1121                 struct mbuf *m = rmsg->m;
1122                 in_addr_t saddr = rmsg->saddr;
1123                 in_addr_t taddr = rmsg->taddr;
1124                 in_addr_t myaddr = rmsg->myaddr;
1125
1126                 /*
1127                  * Dispatch this mbuf to netisr0 to perform ARP reply,
1128                  * if appropriate.
1129                  * NOTE: netmsg_inarp is embedded in this mbuf.
1130                  */
1131                 netmsg_init(&rmsg->base, NULL, &netisr_apanic_rport,
1132                     0, arp_reply_msghandler);
1133                 rmsg->m = m;
1134                 rmsg->saddr = saddr;
1135                 rmsg->taddr = taddr;
1136                 rmsg->myaddr = myaddr;
1137                 lwkt_sendmsg(netisr_cpuport(0), &rmsg->base.lmsg);
1138         }
1139 }
1140
1141 /*
1142  * Reply to an arp request
1143  */
1144 static void
1145 in_arpreply(struct mbuf *m, in_addr_t taddr, in_addr_t myaddr)
1146 {
1147         struct ifnet *ifp = m->m_pkthdr.rcvif;
1148         const uint8_t *enaddr;
1149         struct arphdr *ah;
1150         struct sockaddr sa;
1151         struct ether_header *eh;
1152
1153         ASSERT_NETISR0;
1154
1155         ah = mtod(m, struct arphdr *);
1156         if (ntohs(ah->ar_op) != ARPOP_REQUEST) {
1157                 m_freem(m);
1158                 return;
1159         }
1160
1161         enaddr = (const uint8_t *)IF_LLADDR(ifp);
1162         if (taddr == myaddr) {
1163                 /* I am the target */
1164                 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1165                 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1166         } else {
1167                 struct llinfo_arp *la;
1168                 struct rtentry *rt;
1169
1170                 la = arplookup(taddr, FALSE, SIN_PROXY);
1171                 if (la == NULL) {
1172                         struct sockaddr_in sin;
1173 #ifdef DEBUG_PROXY
1174                         char tbuf[INET_ADDRSTRLEN];
1175 #endif
1176
1177                         if (!arp_proxyall) {
1178                                 m_freem(m);
1179                                 return;
1180                         }
1181
1182                         bzero(&sin, sizeof sin);
1183                         sin.sin_family = AF_INET;
1184                         sin.sin_len = sizeof sin;
1185                         sin.sin_addr.s_addr = taddr;
1186
1187                         rt = rtpurelookup((struct sockaddr *)&sin);
1188                         if (rt == NULL) {
1189                                 m_freem(m);
1190                                 return;
1191                         }
1192                         --rt->rt_refcnt;
1193
1194                         /*
1195                          * Don't send proxies for nodes on the same interface
1196                          * as this one came out of, or we'll get into a fight
1197                          * over who claims what Ether address.
1198                          *
1199                          * If the rt entry is associated with a bridge, we
1200                          * count it as the 'same' interface if ifp is
1201                          * associated with the bridge.
1202                          */
1203                         if (rt->rt_ifp == ifp || rt->rt_ifp == ifp->if_bridge) {
1204                                 m_freem(m);
1205                                 return;
1206                         }
1207                         memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1208                         memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1209 #ifdef DEBUG_PROXY
1210                         kprintf("arp: proxying for %s\n",
1211                             kinet_ntoa(itaddr, tbuf));
1212 #endif
1213                 } else {
1214                         struct sockaddr_dl *sdl;
1215
1216                         rt = la->la_rt;
1217                         memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1218                         sdl = SDL(rt->rt_gateway);
1219                         memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
1220                 }
1221         }
1222
1223         memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1224         memcpy(ar_spa(ah), &taddr, ah->ar_pln);
1225         ah->ar_op = htons(ARPOP_REPLY);
1226         ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1227         switch (ifp->if_type) {
1228         case IFT_ETHER:
1229                 /*
1230                  * May not be correct for types not explictly
1231                  * listed, but it is our best guess.
1232                  */
1233         default:
1234                 eh = (struct ether_header *)sa.sa_data;
1235                 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
1236                 eh->ether_type = htons(ETHERTYPE_ARP);
1237                 break;
1238         }
1239         sa.sa_family = AF_UNSPEC;
1240         sa.sa_len = sizeof sa;
1241         ifp->if_output(ifp, m, &sa, NULL);
1242 }
1243
1244 #endif  /* INET */
1245
1246 /*
1247  * Free an arp entry.  If the arp entry is actively referenced or represents
1248  * a static entry we only clear it back to an unresolved state, otherwise
1249  * we destroy the entry entirely.
1250  *
1251  * Note that static entries are created when route add ... -interface is used
1252  * to create an interface route to a (direct) destination.
1253  */
1254 static void
1255 arptfree(struct llinfo_arp *la)
1256 {
1257         struct rtentry *rt = la->la_rt, *nrt;
1258         struct sockaddr_dl *sdl;
1259         int error;
1260
1261         if (rt == NULL)
1262                 panic("arptfree");
1263         sdl = SDL(rt->rt_gateway);
1264         if (sdl != NULL &&
1265             ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
1266              (rt->rt_flags & RTF_STATIC))) {
1267                 sdl->sdl_alen = 0;
1268                 la->la_preempt = la->la_asked = 0;
1269                 rt->rt_flags &= ~RTF_REJECT;
1270                 return;
1271         }
1272         error = rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, &nrt);
1273         if (error == 0 && nrt != NULL) {
1274                 rt_rtmsg(RTM_DELETE, nrt, nrt->rt_ifp, 0);
1275                 rtfree(nrt);
1276         }
1277 }
1278
1279 /*
1280  * Lookup or enter a new address in arptab.
1281  */
1282 static struct llinfo_arp *
1283 arplookup(in_addr_t addr, boolean_t create,
1284           boolean_t proxy)
1285 {
1286         struct rtentry *rt;
1287         struct sockaddr_inarp sin = { sizeof sin, AF_INET };
1288         const char *why = NULL;
1289
1290         /* Check ARP probes, e.g. from Cisco switches. */
1291         if (addr == INADDR_ANY && arp_ignore_probes)
1292                 return (NULL);
1293
1294         sin.sin_addr.s_addr = addr;
1295         sin.sin_other = proxy ? SIN_PROXY : 0;
1296         if (create) {
1297                 rt = rtlookup((struct sockaddr *)&sin);
1298         } else {
1299                 rt = rtpurelookup((struct sockaddr *)&sin);
1300         }
1301         if (rt == NULL)
1302                 return (NULL);
1303         rt->rt_refcnt--;
1304
1305         if (rt->rt_flags & RTF_GATEWAY)
1306                 why = "host is not on local network";
1307         else if (!(rt->rt_flags & RTF_LLINFO))
1308                 why = "could not allocate llinfo";
1309         else if (rt->rt_gateway->sa_family != AF_LINK)
1310                 why = "gateway route is not ours";
1311
1312         if (why) {
1313                 if (create && log_arp_creation_failure) {
1314                         char abuf[INET_ADDRSTRLEN];
1315
1316                         log(LOG_DEBUG, "arplookup %s failed: %s\n",
1317                             kinet_ntoa(sin.sin_addr, abuf), why);
1318                 }
1319                 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1320                         /* No references to this route.  Purge it. */
1321                         rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1322                                   rt_mask(rt), rt->rt_flags, NULL);
1323                 }
1324                 return (NULL);
1325         }
1326         return (rt->rt_llinfo);
1327 }
1328
1329 void
1330 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1331 {
1332         ifa->ifa_rtrequest = arp_rtrequest;
1333         ifa->ifa_flags |= RTF_CLONING;
1334 }
1335
1336 void
1337 arp_gratuitous(struct ifnet *ifp, struct ifaddr *ifa)
1338 {
1339         if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) {
1340                 if (IN_NETISR_NCPUS(mycpuid)) {
1341                         arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1342                             &IA_SIN(ifa)->sin_addr, NULL);
1343                 } else {
1344                         arprequest_async(ifp, &IA_SIN(ifa)->sin_addr,
1345                             &IA_SIN(ifa)->sin_addr, NULL);
1346                 }
1347         }
1348 }
1349
1350 static void
1351 arp_ifaddr(void *arg __unused, struct ifnet *ifp,
1352     enum ifaddr_event event, struct ifaddr *ifa)
1353 {
1354         if (ifa->ifa_rtrequest != arp_rtrequest) /* XXX need a generic way */
1355                 return;
1356         if (ifa->ifa_addr->sa_family != AF_INET)
1357                 return;
1358         if (event == IFADDR_EVENT_DELETE)
1359                 return;
1360
1361         /*
1362          * - CARP interfaces will take care of gratuitous ARP themselves.
1363          * - If we are the CARP interface's parent, don't send gratuitous
1364          *   ARP to avoid unnecessary confusion.
1365          */
1366 #ifdef CARP
1367         if (ifp->if_type != IFT_CARP && ifp->if_carp == NULL)
1368 #endif
1369         {
1370                 arp_gratuitous(ifp, ifa);
1371         }
1372 }
1373
1374 static void
1375 arp_init_dispatch(netmsg_t nm)
1376 {
1377         struct arp_pcpu_data *ad;
1378
1379         ASSERT_NETISR_NCPUS(mycpuid);
1380
1381         ad = kmalloc(sizeof(*ad), M_ARP, M_WAITOK | M_ZERO);
1382
1383         LIST_INIT(&ad->llinfo_list);
1384         callout_init_mp(&ad->timer_ch);
1385         netmsg_init(&ad->timer_nmsg, NULL, &netisr_adone_rport,
1386             MSGF_PRIORITY, arptimer_dispatch);
1387         ad->timer_nmsg.lmsg.u.ms_resultp = ad;
1388
1389         arp_data[mycpuid] = ad;
1390
1391         callout_reset(&ad->timer_ch, hz, arptimer, &ad->timer_nmsg);
1392
1393         netisr_forwardmsg(&nm->base, mycpuid + 1);
1394 }
1395
1396 static void
1397 arp_init(void)
1398 {
1399         struct netmsg_base nm;
1400
1401         netmsg_init(&nm, NULL, &curthread->td_msgport, 0, arp_init_dispatch);
1402         netisr_domsg_global(&nm);
1403
1404         netisr_register(NETISR_ARP, arpintr, NULL);
1405
1406         EVENTHANDLER_REGISTER(ifaddr_event, arp_ifaddr, NULL,
1407             EVENTHANDLER_PRI_LAST);
1408 }
1409 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);