]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/if_ether.c
Update compiler-rt to 3.7.0 release. This also includes the sanitizer
[FreeBSD/FreeBSD.git] / sys / netinet / if_ether.c
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)if_ether.c  8.1 (Berkeley) 6/10/93
30  */
31
32 /*
33  * Ethernet address resolution protocol.
34  * TODO:
35  *      add "inuse/lock" bit (or ref. count) along with valid bit
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include "opt_inet.h"
42
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/queue.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
49 #include <sys/mbuf.h>
50 #include <sys/malloc.h>
51 #include <sys/proc.h>
52 #include <sys/rmlock.h>
53 #include <sys/socket.h>
54 #include <sys/syslog.h>
55
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/netisr.h>
61 #include <net/if_llc.h>
62 #include <net/ethernet.h>
63 #include <net/route.h>
64 #include <net/vnet.h>
65
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <net/if_llatbl.h>
69 #include <netinet/if_ether.h>
70 #ifdef INET
71 #include <netinet/ip_carp.h>
72 #endif
73
74 #include <net/if_arc.h>
75 #include <net/iso88025.h>
76
77 #include <security/mac/mac_framework.h>
78
79 #define SIN(s) ((const struct sockaddr_in *)(s))
80 #define SDL(s) ((struct sockaddr_dl *)s)
81
82 SYSCTL_DECL(_net_link_ether);
83 static SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
84 static SYSCTL_NODE(_net_link_ether, PF_ARP, arp, CTLFLAG_RW, 0, "");
85
86 /* timer values */
87 static VNET_DEFINE(int, arpt_keep) = (20*60);   /* once resolved, good for 20
88                                                  * minutes */
89 static VNET_DEFINE(int, arp_maxtries) = 5;
90 static VNET_DEFINE(int, arp_proxyall) = 0;
91 static VNET_DEFINE(int, arpt_down) = 20;        /* keep incomplete entries for
92                                                  * 20 seconds */
93 VNET_PCPUSTAT_DEFINE(struct arpstat, arpstat);  /* ARP statistics, see if_arp.h */
94 VNET_PCPUSTAT_SYSINIT(arpstat);
95
96 #ifdef VIMAGE
97 VNET_PCPUSTAT_SYSUNINIT(arpstat);
98 #endif /* VIMAGE */
99
100 static VNET_DEFINE(int, arp_maxhold) = 1;
101
102 #define V_arpt_keep             VNET(arpt_keep)
103 #define V_arpt_down             VNET(arpt_down)
104 #define V_arp_maxtries          VNET(arp_maxtries)
105 #define V_arp_proxyall          VNET(arp_proxyall)
106 #define V_arp_maxhold           VNET(arp_maxhold)
107
108 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_VNET | CTLFLAG_RW,
109         &VNET_NAME(arpt_keep), 0,
110         "ARP entry lifetime in seconds");
111 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_VNET | CTLFLAG_RW,
112         &VNET_NAME(arp_maxtries), 0,
113         "ARP resolution attempts before returning error");
114 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_VNET | CTLFLAG_RW,
115         &VNET_NAME(arp_proxyall), 0,
116         "Enable proxy ARP for all suitable requests");
117 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, wait, CTLFLAG_VNET | CTLFLAG_RW,
118         &VNET_NAME(arpt_down), 0,
119         "Incomplete ARP entry lifetime in seconds");
120 SYSCTL_VNET_PCPUSTAT(_net_link_ether_arp, OID_AUTO, stats, struct arpstat,
121     arpstat, "ARP statistics (struct arpstat, net/if_arp.h)");
122 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxhold, CTLFLAG_VNET | CTLFLAG_RW,
123         &VNET_NAME(arp_maxhold), 0,
124         "Number of packets to hold per ARP entry");
125
126 static void     arp_init(void);
127 static void     arpintr(struct mbuf *);
128 static void     arptimer(void *);
129 #ifdef INET
130 static void     in_arpinput(struct mbuf *);
131 #endif
132
133 static void arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr,
134     struct ifnet *ifp, int bridged, struct llentry *la);
135 static void arp_update_lle(struct arphdr *ah, struct ifnet *ifp,
136     struct llentry *la);
137 static void arp_mark_lle_reachable(struct llentry *la);
138
139
140 static const struct netisr_handler arp_nh = {
141         .nh_name = "arp",
142         .nh_handler = arpintr,
143         .nh_proto = NETISR_ARP,
144         .nh_policy = NETISR_POLICY_SOURCE,
145 };
146
147 #ifdef AF_INET
148 /*
149  * called by in_scrubprefix() to remove entry from the table when
150  * the interface goes away
151  */
152 void
153 arp_ifscrub(struct ifnet *ifp, uint32_t addr)
154 {
155         struct sockaddr_in addr4;
156
157         bzero((void *)&addr4, sizeof(addr4));
158         addr4.sin_len    = sizeof(addr4);
159         addr4.sin_family = AF_INET;
160         addr4.sin_addr.s_addr = addr;
161         IF_AFDATA_WLOCK(ifp);
162         lla_delete(LLTABLE(ifp), LLE_IFADDR, (struct sockaddr *)&addr4);
163         IF_AFDATA_WUNLOCK(ifp);
164 }
165 #endif
166
167 /*
168  * Timeout routine.  Age arp_tab entries periodically.
169  */
170 static void
171 arptimer(void *arg)
172 {
173         struct llentry *lle = (struct llentry *)arg;
174         struct ifnet *ifp;
175
176         if (lle->la_flags & LLE_STATIC) {
177                 return;
178         }
179         LLE_WLOCK(lle);
180         if (callout_pending(&lle->lle_timer)) {
181                 /*
182                  * Here we are a bit odd here in the treatment of 
183                  * active/pending. If the pending bit is set, it got
184                  * rescheduled before I ran. The active
185                  * bit we ignore, since if it was stopped
186                  * in ll_tablefree() and was currently running
187                  * it would have return 0 so the code would
188                  * not have deleted it since the callout could
189                  * not be stopped so we want to go through
190                  * with the delete here now. If the callout
191                  * was restarted, the pending bit will be back on and
192                  * we just want to bail since the callout_reset would
193                  * return 1 and our reference would have been removed
194                  * by arpresolve() below.
195                  */
196                 LLE_WUNLOCK(lle);
197                 return;
198         }
199         ifp = lle->lle_tbl->llt_ifp;
200         CURVNET_SET(ifp->if_vnet);
201
202         if ((lle->la_flags & LLE_DELETED) == 0) {
203                 int evt;
204
205                 if (lle->la_flags & LLE_VALID)
206                         evt = LLENTRY_EXPIRED;
207                 else
208                         evt = LLENTRY_TIMEDOUT;
209                 EVENTHANDLER_INVOKE(lle_event, lle, evt);
210         }
211
212         callout_stop(&lle->lle_timer);
213
214         /* XXX: LOR avoidance. We still have ref on lle. */
215         LLE_WUNLOCK(lle);
216         IF_AFDATA_LOCK(ifp);
217         LLE_WLOCK(lle);
218
219         /* Guard against race with other llentry_free(). */
220         if (lle->la_flags & LLE_LINKED) {
221
222                 size_t pkts_dropped;
223                 LLE_REMREF(lle);
224                 pkts_dropped = llentry_free(lle);
225                 ARPSTAT_ADD(dropped, pkts_dropped);
226         } else
227                 LLE_FREE_LOCKED(lle);
228
229         IF_AFDATA_UNLOCK(ifp);
230
231         ARPSTAT_INC(timeouts);
232
233         CURVNET_RESTORE();
234 }
235
236 /*
237  * Broadcast an ARP request. Caller specifies:
238  *      - arp header source ip address
239  *      - arp header target ip address
240  *      - arp header source ethernet address
241  */
242 void
243 arprequest(struct ifnet *ifp, const struct in_addr *sip,
244     const struct in_addr *tip, u_char *enaddr)
245 {
246         struct mbuf *m;
247         struct arphdr *ah;
248         struct sockaddr sa;
249         u_char *carpaddr = NULL;
250
251         if (sip == NULL) {
252                 /*
253                  * The caller did not supply a source address, try to find
254                  * a compatible one among those assigned to this interface.
255                  */
256                 struct ifaddr *ifa;
257
258                 IF_ADDR_RLOCK(ifp);
259                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
260                         if (ifa->ifa_addr->sa_family != AF_INET)
261                                 continue;
262
263                         if (ifa->ifa_carp) {
264                                 if ((*carp_iamatch_p)(ifa, &carpaddr) == 0)
265                                         continue;
266                                 sip = &IA_SIN(ifa)->sin_addr;
267                         } else {
268                                 carpaddr = NULL;
269                                 sip = &IA_SIN(ifa)->sin_addr;
270                         }
271
272                         if (0 == ((sip->s_addr ^ tip->s_addr) &
273                             IA_MASKSIN(ifa)->sin_addr.s_addr))
274                                 break;  /* found it. */
275                 }
276                 IF_ADDR_RUNLOCK(ifp);
277                 if (sip == NULL) {
278                         printf("%s: cannot find matching address\n", __func__);
279                         return;
280                 }
281         }
282         if (enaddr == NULL)
283                 enaddr = carpaddr ? carpaddr : (u_char *)IF_LLADDR(ifp);
284
285         if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
286                 return;
287         m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
288                 2 * ifp->if_addrlen;
289         m->m_pkthdr.len = m->m_len;
290         M_ALIGN(m, m->m_len);
291         ah = mtod(m, struct arphdr *);
292         bzero((caddr_t)ah, m->m_len);
293 #ifdef MAC
294         mac_netinet_arp_send(ifp, m);
295 #endif
296         ah->ar_pro = htons(ETHERTYPE_IP);
297         ah->ar_hln = ifp->if_addrlen;           /* hardware address length */
298         ah->ar_pln = sizeof(struct in_addr);    /* protocol address length */
299         ah->ar_op = htons(ARPOP_REQUEST);
300         bcopy(enaddr, ar_sha(ah), ah->ar_hln);
301         bcopy(sip, ar_spa(ah), ah->ar_pln);
302         bcopy(tip, ar_tpa(ah), ah->ar_pln);
303         sa.sa_family = AF_ARP;
304         sa.sa_len = 2;
305         m->m_flags |= M_BCAST;
306         m_clrprotoflags(m);     /* Avoid confusing lower layers. */
307         (*ifp->if_output)(ifp, m, &sa, NULL);
308         ARPSTAT_INC(txrequests);
309 }
310
311 /*
312  * Resolve an IP address into an ethernet address - heavy version.
313  * Used internally by arpresolve().
314  * We have already checked than  we can't use existing lle without
315  * modification so we have to acquire LLE_EXCLUSIVE lle lock.
316  *
317  * On success, desten and flags are filled in and the function returns 0;
318  * If the packet must be held pending resolution, we return EWOULDBLOCK
319  * On other errors, we return the corresponding error code.
320  * Note that m_freem() handles NULL.
321  */
322 static int
323 arpresolve_full(struct ifnet *ifp, int is_gw, int create, struct mbuf *m,
324         const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
325 {
326         struct llentry *la = NULL, *la_tmp;
327         struct mbuf *curr = NULL;
328         struct mbuf *next = NULL;
329         int error, renew;
330
331         if (pflags != NULL)
332                 *pflags = 0;
333
334         if (create == 0) {
335                 IF_AFDATA_RLOCK(ifp);
336                 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
337                 IF_AFDATA_RUNLOCK(ifp);
338         }
339         if (la == NULL && (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0) {
340                 la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
341                 if (la == NULL) {
342                         log(LOG_DEBUG,
343                             "arpresolve: can't allocate llinfo for %s on %s\n",
344                             inet_ntoa(SIN(dst)->sin_addr), if_name(ifp));
345                         m_freem(m);
346                         return (EINVAL);
347                 }
348
349                 IF_AFDATA_WLOCK(ifp);
350                 LLE_WLOCK(la);
351                 la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
352                 /* Prefer ANY existing lle over newly-created one */
353                 if (la_tmp == NULL)
354                         lltable_link_entry(LLTABLE(ifp), la);
355                 IF_AFDATA_WUNLOCK(ifp);
356                 if (la_tmp != NULL) {
357                         lltable_free_entry(LLTABLE(ifp), la);
358                         la = la_tmp;
359                 }
360         }
361         if (la == NULL) {
362                 m_freem(m);
363                 return (EINVAL);
364         }
365
366         if ((la->la_flags & LLE_VALID) &&
367             ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
368                 bcopy(&la->ll_addr, desten, ifp->if_addrlen);
369                 renew = 0;
370                 /*
371                  * If entry has an expiry time and it is approaching,
372                  * see if we need to send an ARP request within this
373                  * arpt_down interval.
374                  */
375                 if (!(la->la_flags & LLE_STATIC) &&
376                     time_uptime + la->la_preempt > la->la_expire) {
377                         renew = 1;
378                         la->la_preempt--;
379                 }
380
381                 if (pflags != NULL)
382                         *pflags = la->la_flags;
383
384                 LLE_WUNLOCK(la);
385
386                 if (renew == 1)
387                         arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
388
389                 return (0);
390         }
391
392         renew = (la->la_asked == 0 || la->la_expire != time_uptime);
393         /*
394          * There is an arptab entry, but no ethernet address
395          * response yet.  Add the mbuf to the list, dropping
396          * the oldest packet if we have exceeded the system
397          * setting.
398          */
399         if (m != NULL) {
400                 if (la->la_numheld >= V_arp_maxhold) {
401                         if (la->la_hold != NULL) {
402                                 next = la->la_hold->m_nextpkt;
403                                 m_freem(la->la_hold);
404                                 la->la_hold = next;
405                                 la->la_numheld--;
406                                 ARPSTAT_INC(dropped);
407                         }
408                 }
409                 if (la->la_hold != NULL) {
410                         curr = la->la_hold;
411                         while (curr->m_nextpkt != NULL)
412                                 curr = curr->m_nextpkt;
413                         curr->m_nextpkt = m;
414                 } else
415                         la->la_hold = m;
416                 la->la_numheld++;
417         }
418         /*
419          * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
420          * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
421          * if we have already sent arp_maxtries ARP requests. Retransmit the
422          * ARP request, but not faster than one request per second.
423          */
424         if (la->la_asked < V_arp_maxtries)
425                 error = EWOULDBLOCK;    /* First request. */
426         else
427                 error = is_gw != 0 ? EHOSTUNREACH : EHOSTDOWN;
428
429         if (renew) {
430                 int canceled;
431
432                 LLE_ADDREF(la);
433                 la->la_expire = time_uptime;
434                 canceled = callout_reset(&la->lle_timer, hz * V_arpt_down,
435                     arptimer, la);
436                 if (canceled)
437                         LLE_REMREF(la);
438                 la->la_asked++;
439                 LLE_WUNLOCK(la);
440                 arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
441                 return (error);
442         }
443
444         LLE_WUNLOCK(la);
445         return (error);
446 }
447
448 /*
449  * Resolve an IP address into an ethernet address.
450  * On input:
451  *    ifp is the interface we use
452  *    is_gw != 0 if @dst represents gateway to some destination
453  *    m is the mbuf. May be NULL if we don't have a packet.
454  *    dst is the next hop,
455  *    desten is the storage to put LL address.
456  *    flags returns lle entry flags.
457  *
458  * On success, desten and flags are filled in and the function returns 0;
459  * If the packet must be held pending resolution, we return EWOULDBLOCK
460  * On other errors, we return the corresponding error code.
461  * Note that m_freem() handles NULL.
462  */
463 int
464 arpresolve(struct ifnet *ifp, int is_gw, struct mbuf *m,
465         const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
466 {
467         struct llentry *la = 0;
468         int renew;
469
470         if (pflags != NULL)
471                 *pflags = 0;
472
473         if (m != NULL) {
474                 if (m->m_flags & M_BCAST) {
475                         /* broadcast */
476                         (void)memcpy(desten,
477                             ifp->if_broadcastaddr, ifp->if_addrlen);
478                         return (0);
479                 }
480                 if (m->m_flags & M_MCAST) {
481                         /* multicast */
482                         ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
483                         return (0);
484                 }
485         }
486
487         IF_AFDATA_RLOCK(ifp);
488         la = lla_lookup(LLTABLE(ifp), 0, dst);
489         IF_AFDATA_RUNLOCK(ifp);
490
491         if (la == NULL)
492                 return (arpresolve_full(ifp, is_gw, 1, m, dst, desten, pflags));
493
494         if ((la->la_flags & LLE_VALID) &&
495             ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
496                 bcopy(&la->ll_addr, desten, ifp->if_addrlen);
497                 renew = 0;
498                 /*
499                  * If entry has an expiry time and it is approaching,
500                  * see if we need to send an ARP request within this
501                  * arpt_down interval.
502                  */
503                 if (!(la->la_flags & LLE_STATIC) &&
504                     time_uptime + la->la_preempt > la->la_expire) {
505                         renew = 1;
506                         la->la_preempt--;
507                 }
508
509                 if (pflags != NULL)
510                         *pflags = la->la_flags;
511
512                 LLE_RUNLOCK(la);
513
514                 if (renew == 1)
515                         arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
516
517                 return (0);
518         }
519         LLE_RUNLOCK(la);
520
521         return (arpresolve_full(ifp, is_gw, 0, m, dst, desten, pflags));
522 }
523
524 /*
525  * Common length and type checks are done here,
526  * then the protocol-specific routine is called.
527  */
528 static void
529 arpintr(struct mbuf *m)
530 {
531         struct arphdr *ar;
532
533         if (m->m_len < sizeof(struct arphdr) &&
534             ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
535                 log(LOG_NOTICE, "arp: runt packet -- m_pullup failed\n");
536                 return;
537         }
538         ar = mtod(m, struct arphdr *);
539
540         if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
541             ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
542             ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
543             ntohs(ar->ar_hrd) != ARPHRD_IEEE1394 &&
544             ntohs(ar->ar_hrd) != ARPHRD_INFINIBAND) {
545                 log(LOG_NOTICE, "arp: unknown hardware address format (0x%2D)"
546                     " (from %*D to %*D)\n", (unsigned char *)&ar->ar_hrd, "",
547                     ETHER_ADDR_LEN, (u_char *)ar_sha(ar), ":",
548                     ETHER_ADDR_LEN, (u_char *)ar_tha(ar), ":");
549                 m_freem(m);
550                 return;
551         }
552
553         if (m->m_len < arphdr_len(ar)) {
554                 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
555                         log(LOG_NOTICE, "arp: runt packet\n");
556                         m_freem(m);
557                         return;
558                 }
559                 ar = mtod(m, struct arphdr *);
560         }
561
562         ARPSTAT_INC(received);
563         switch (ntohs(ar->ar_pro)) {
564 #ifdef INET
565         case ETHERTYPE_IP:
566                 in_arpinput(m);
567                 return;
568 #endif
569         }
570         m_freem(m);
571 }
572
573 #ifdef INET
574 /*
575  * ARP for Internet protocols on 10 Mb/s Ethernet.
576  * Algorithm is that given in RFC 826.
577  * In addition, a sanity check is performed on the sender
578  * protocol address, to catch impersonators.
579  * We no longer handle negotiations for use of trailer protocol:
580  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
581  * along with IP replies if we wanted trailers sent to us,
582  * and also sent them in response to IP replies.
583  * This allowed either end to announce the desire to receive
584  * trailer packets.
585  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
586  * but formerly didn't normally send requests.
587  */
588 static int log_arp_wrong_iface = 1;
589 static int log_arp_movements = 1;
590 static int log_arp_permanent_modify = 1;
591 static int allow_multicast = 0;
592 static struct timeval arp_lastlog;
593 static int arp_curpps;
594 static int arp_maxpps = 1;
595
596 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
597         &log_arp_wrong_iface, 0,
598         "log arp packets arriving on the wrong interface");
599 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
600         &log_arp_movements, 0,
601         "log arp replies from MACs different than the one in the cache");
602 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
603         &log_arp_permanent_modify, 0,
604         "log arp replies from MACs different than the one in the permanent arp entry");
605 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, allow_multicast, CTLFLAG_RW,
606         &allow_multicast, 0, "accept multicast addresses");
607 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_log_per_second,
608         CTLFLAG_RW, &arp_maxpps, 0,
609         "Maximum number of remotely triggered ARP messages that can be "
610         "logged per second");
611
612 #define ARP_LOG(pri, ...)       do {                                    \
613         if (ppsratecheck(&arp_lastlog, &arp_curpps, arp_maxpps))        \
614                 log((pri), "arp: " __VA_ARGS__);                        \
615 } while (0)
616
617 static void
618 in_arpinput(struct mbuf *m)
619 {
620         struct rm_priotracker in_ifa_tracker;
621         struct arphdr *ah;
622         struct ifnet *ifp = m->m_pkthdr.rcvif;
623         struct llentry *la = NULL, *la_tmp;
624         struct rtentry *rt;
625         struct ifaddr *ifa;
626         struct in_ifaddr *ia;
627         struct sockaddr sa;
628         struct in_addr isaddr, itaddr, myaddr;
629         u_int8_t *enaddr = NULL;
630         int op;
631         int req_len;
632         int bridged = 0, is_bridge = 0;
633         int carped;
634         struct sockaddr_in sin;
635         struct sockaddr *dst;
636         sin.sin_len = sizeof(struct sockaddr_in);
637         sin.sin_family = AF_INET;
638         sin.sin_addr.s_addr = 0;
639
640         if (ifp->if_bridge)
641                 bridged = 1;
642         if (ifp->if_type == IFT_BRIDGE)
643                 is_bridge = 1;
644
645         req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
646         if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
647                 ARP_LOG(LOG_NOTICE, "runt packet -- m_pullup failed\n");
648                 return;
649         }
650
651         ah = mtod(m, struct arphdr *);
652         /*
653          * ARP is only for IPv4 so we can reject packets with
654          * a protocol length not equal to an IPv4 address.
655          */
656         if (ah->ar_pln != sizeof(struct in_addr)) {
657                 ARP_LOG(LOG_NOTICE, "requested protocol length != %zu\n",
658                     sizeof(struct in_addr));
659                 goto drop;
660         }
661
662         if (allow_multicast == 0 && ETHER_IS_MULTICAST(ar_sha(ah))) {
663                 ARP_LOG(LOG_NOTICE, "%*D is multicast\n",
664                     ifp->if_addrlen, (u_char *)ar_sha(ah), ":");
665                 goto drop;
666         }
667
668         op = ntohs(ah->ar_op);
669         (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
670         (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
671
672         if (op == ARPOP_REPLY)
673                 ARPSTAT_INC(rxreplies);
674
675         /*
676          * For a bridge, we want to check the address irrespective
677          * of the receive interface. (This will change slightly
678          * when we have clusters of interfaces).
679          */
680         IN_IFADDR_RLOCK(&in_ifa_tracker);
681         LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
682                 if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
683                     ia->ia_ifp == ifp) &&
684                     itaddr.s_addr == ia->ia_addr.sin_addr.s_addr &&
685                     (ia->ia_ifa.ifa_carp == NULL ||
686                     (*carp_iamatch_p)(&ia->ia_ifa, &enaddr))) {
687                         ifa_ref(&ia->ia_ifa);
688                         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
689                         goto match;
690                 }
691         }
692         LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
693                 if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
694                     ia->ia_ifp == ifp) &&
695                     isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
696                         ifa_ref(&ia->ia_ifa);
697                         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
698                         goto match;
699                 }
700
701 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia)                           \
702   (ia->ia_ifp->if_bridge == ifp->if_softc &&                            \
703   !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) &&      \
704   addr == ia->ia_addr.sin_addr.s_addr)
705         /*
706          * Check the case when bridge shares its MAC address with
707          * some of its children, so packets are claimed by bridge
708          * itself (bridge_input() does it first), but they are really
709          * meant to be destined to the bridge member.
710          */
711         if (is_bridge) {
712                 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
713                         if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
714                                 ifa_ref(&ia->ia_ifa);
715                                 ifp = ia->ia_ifp;
716                                 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
717                                 goto match;
718                         }
719                 }
720         }
721 #undef BDG_MEMBER_MATCHES_ARP
722         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
723
724         /*
725          * No match, use the first inet address on the receive interface
726          * as a dummy address for the rest of the function.
727          */
728         IF_ADDR_RLOCK(ifp);
729         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
730                 if (ifa->ifa_addr->sa_family == AF_INET &&
731                     (ifa->ifa_carp == NULL ||
732                     (*carp_iamatch_p)(ifa, &enaddr))) {
733                         ia = ifatoia(ifa);
734                         ifa_ref(ifa);
735                         IF_ADDR_RUNLOCK(ifp);
736                         goto match;
737                 }
738         IF_ADDR_RUNLOCK(ifp);
739
740         /*
741          * If bridging, fall back to using any inet address.
742          */
743         IN_IFADDR_RLOCK(&in_ifa_tracker);
744         if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) {
745                 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
746                 goto drop;
747         }
748         ifa_ref(&ia->ia_ifa);
749         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
750 match:
751         if (!enaddr)
752                 enaddr = (u_int8_t *)IF_LLADDR(ifp);
753         carped = (ia->ia_ifa.ifa_carp != NULL);
754         myaddr = ia->ia_addr.sin_addr;
755         ifa_free(&ia->ia_ifa);
756         if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
757                 goto drop;      /* it's from me, ignore it. */
758         if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
759                 ARP_LOG(LOG_NOTICE, "link address is broadcast for IP address "
760                     "%s!\n", inet_ntoa(isaddr));
761                 goto drop;
762         }
763
764         if (ifp->if_addrlen != ah->ar_hln) {
765                 ARP_LOG(LOG_WARNING, "from %*D: addr len: new %d, "
766                     "i/f %d (ignored)\n", ifp->if_addrlen,
767                     (u_char *) ar_sha(ah), ":", ah->ar_hln,
768                     ifp->if_addrlen);
769                 goto drop;
770         }
771
772         /*
773          * Warn if another host is using the same IP address, but only if the
774          * IP address isn't 0.0.0.0, which is used for DHCP only, in which
775          * case we suppress the warning to avoid false positive complaints of
776          * potential misconfiguration.
777          */
778         if (!bridged && !carped && isaddr.s_addr == myaddr.s_addr &&
779             myaddr.s_addr != 0) {
780                 ARP_LOG(LOG_ERR, "%*D is using my IP address %s on %s!\n",
781                    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
782                    inet_ntoa(isaddr), ifp->if_xname);
783                 itaddr = myaddr;
784                 ARPSTAT_INC(dupips);
785                 goto reply;
786         }
787         if (ifp->if_flags & IFF_STATICARP)
788                 goto reply;
789
790         bzero(&sin, sizeof(sin));
791         sin.sin_len = sizeof(struct sockaddr_in);
792         sin.sin_family = AF_INET;
793         sin.sin_addr = isaddr;
794         dst = (struct sockaddr *)&sin;
795         IF_AFDATA_RLOCK(ifp);
796         la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
797         IF_AFDATA_RUNLOCK(ifp);
798         if (la != NULL)
799                 arp_check_update_lle(ah, isaddr, ifp, bridged, la);
800         else if (itaddr.s_addr == myaddr.s_addr) {
801                 /*
802                  * Reply to our address, but no lle exists yet.
803                  * do we really have to create an entry?
804                  */
805                 la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
806                 if (la == NULL)
807                         goto drop;
808                 arp_update_lle(ah, ifp, la);
809
810                 IF_AFDATA_WLOCK(ifp);
811                 LLE_WLOCK(la);
812                 la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
813
814                 /*
815                  * Check if lle still does not exists.
816                  * If it does, that means that we either
817                  * 1) have configured it explicitly, via
818                  * 1a) 'arp -s' static entry or
819                  * 1b) interface address static record
820                  * or
821                  * 2) it was the result of sending first packet to-host
822                  * or
823                  * 3) it was another arp reply packet we handled in
824                  * different thread.
825                  *
826                  * In all cases except 3) we definitely need to prefer
827                  * existing lle. For the sake of simplicity, prefer any
828                  * existing lle over newly-create one.
829                  */
830                 if (la_tmp == NULL)
831                         lltable_link_entry(LLTABLE(ifp), la);
832                 IF_AFDATA_WUNLOCK(ifp);
833
834                 if (la_tmp == NULL) {
835                         arp_mark_lle_reachable(la);
836                         LLE_WUNLOCK(la);
837                 } else {
838                         /* Free newly-create entry and handle packet */
839                         lltable_free_entry(LLTABLE(ifp), la);
840                         la = la_tmp;
841                         la_tmp = NULL;
842                         arp_check_update_lle(ah, isaddr, ifp, bridged, la);
843                         /* arp_check_update_lle() returns @la unlocked */
844                 }
845                 la = NULL;
846         }
847 reply:
848         if (op != ARPOP_REQUEST)
849                 goto drop;
850         ARPSTAT_INC(rxrequests);
851
852         if (itaddr.s_addr == myaddr.s_addr) {
853                 /* Shortcut.. the receiving interface is the target. */
854                 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
855                 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
856         } else {
857                 struct llentry *lle = NULL;
858
859                 sin.sin_addr = itaddr;
860                 IF_AFDATA_RLOCK(ifp);
861                 lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
862                 IF_AFDATA_RUNLOCK(ifp);
863
864                 if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
865                         (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
866                         (void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
867                         LLE_RUNLOCK(lle);
868                 } else {
869
870                         if (lle != NULL)
871                                 LLE_RUNLOCK(lle);
872
873                         if (!V_arp_proxyall)
874                                 goto drop;
875
876                         sin.sin_addr = itaddr;
877                         /* XXX MRT use table 0 for arp reply  */
878                         rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
879                         if (!rt)
880                                 goto drop;
881
882                         /*
883                          * Don't send proxies for nodes on the same interface
884                          * as this one came out of, or we'll get into a fight
885                          * over who claims what Ether address.
886                          */
887                         if (!rt->rt_ifp || rt->rt_ifp == ifp) {
888                                 RTFREE_LOCKED(rt);
889                                 goto drop;
890                         }
891                         RTFREE_LOCKED(rt);
892
893                         (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
894                         (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
895
896                         /*
897                          * Also check that the node which sent the ARP packet
898                          * is on the interface we expect it to be on. This
899                          * avoids ARP chaos if an interface is connected to the
900                          * wrong network.
901                          */
902                         sin.sin_addr = isaddr;
903
904                         /* XXX MRT use table 0 for arp checks */
905                         rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
906                         if (!rt)
907                                 goto drop;
908                         if (rt->rt_ifp != ifp) {
909                                 ARP_LOG(LOG_INFO, "proxy: ignoring request"
910                                     " from %s via %s, expecting %s\n",
911                                     inet_ntoa(isaddr), ifp->if_xname,
912                                     rt->rt_ifp->if_xname);
913                                 RTFREE_LOCKED(rt);
914                                 goto drop;
915                         }
916                         RTFREE_LOCKED(rt);
917
918 #ifdef DEBUG_PROXY
919                         printf("arp: proxying for %s\n", inet_ntoa(itaddr));
920 #endif
921                 }
922         }
923
924         if (itaddr.s_addr == myaddr.s_addr &&
925             IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
926                 /* RFC 3927 link-local IPv4; always reply by broadcast. */
927 #ifdef DEBUG_LINKLOCAL
928                 printf("arp: sending reply for link-local addr %s\n",
929                     inet_ntoa(itaddr));
930 #endif
931                 m->m_flags |= M_BCAST;
932                 m->m_flags &= ~M_MCAST;
933         } else {
934                 /* default behaviour; never reply by broadcast. */
935                 m->m_flags &= ~(M_BCAST|M_MCAST);
936         }
937         (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
938         (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
939         ah->ar_op = htons(ARPOP_REPLY);
940         ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
941         m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
942         m->m_pkthdr.len = m->m_len;
943         m->m_pkthdr.rcvif = NULL;
944         sa.sa_family = AF_ARP;
945         sa.sa_len = 2;
946         m_clrprotoflags(m);     /* Avoid confusing lower layers. */
947         (*ifp->if_output)(ifp, m, &sa, NULL);
948         ARPSTAT_INC(txreplies);
949         return;
950
951 drop:
952         m_freem(m);
953 }
954 #endif
955
956 /*
957  * Checks received arp data against existing @la.
958  * Updates lle state/performs notification if necessary.
959  */
960 static void
961 arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr, struct ifnet *ifp,
962     int bridged, struct llentry *la)
963 {
964         struct sockaddr sa;
965         struct mbuf *m_hold, *m_hold_next;
966
967         LLE_WLOCK_ASSERT(la);
968
969         /* the following is not an error when doing bridging */
970         if (!bridged && la->lle_tbl->llt_ifp != ifp) {
971                 if (log_arp_wrong_iface)
972                         ARP_LOG(LOG_WARNING, "%s is on %s "
973                             "but got reply from %*D on %s\n",
974                             inet_ntoa(isaddr),
975                             la->lle_tbl->llt_ifp->if_xname,
976                             ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
977                             ifp->if_xname);
978                 LLE_WUNLOCK(la);
979                 return;
980         }
981         if ((la->la_flags & LLE_VALID) &&
982             bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
983                 if (la->la_flags & LLE_STATIC) {
984                         LLE_WUNLOCK(la);
985                         if (log_arp_permanent_modify)
986                                 ARP_LOG(LOG_ERR,
987                                     "%*D attempts to modify "
988                                     "permanent entry for %s on %s\n",
989                                     ifp->if_addrlen,
990                                     (u_char *)ar_sha(ah), ":",
991                                     inet_ntoa(isaddr), ifp->if_xname);
992                         return;
993                 }
994                 if (log_arp_movements) {
995                         ARP_LOG(LOG_INFO, "%s moved from %*D "
996                             "to %*D on %s\n",
997                             inet_ntoa(isaddr),
998                             ifp->if_addrlen,
999                             (u_char *)&la->ll_addr, ":",
1000                             ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
1001                             ifp->if_xname);
1002                 }
1003         }
1004
1005         /* Check if something has changed */
1006         if (memcmp(&la->ll_addr, ar_sha(ah), ifp->if_addrlen) != 0 ||
1007             (la->la_flags & LLE_VALID) == 0) {
1008                 /* Perform real LLE update */
1009                 /* use afdata WLOCK to update fields */
1010                 LLE_ADDREF(la);
1011                 LLE_WUNLOCK(la);
1012                 IF_AFDATA_WLOCK(ifp);
1013                 LLE_WLOCK(la);
1014
1015                 /*
1016                  * Since we droppped LLE lock, other thread might have deleted
1017                  * this lle. Check and return
1018                  */
1019                 if ((la->la_flags & LLE_DELETED) != 0) {
1020                         IF_AFDATA_WUNLOCK(ifp);
1021                         LLE_FREE_LOCKED(la);
1022                         return;
1023                 }
1024
1025                 /* Update data */
1026                 arp_update_lle(ah, ifp, la);
1027
1028                 IF_AFDATA_WUNLOCK(ifp);
1029                 LLE_REMREF(la);
1030         }
1031
1032         arp_mark_lle_reachable(la);
1033
1034         /*
1035          * The packets are all freed within the call to the output
1036          * routine.
1037          *
1038          * NB: The lock MUST be released before the call to the
1039          * output routine.
1040          */
1041         if (la->la_hold != NULL) {
1042                 m_hold = la->la_hold;
1043                 la->la_hold = NULL;
1044                 la->la_numheld = 0;
1045                 lltable_fill_sa_entry(la, &sa);
1046                 LLE_WUNLOCK(la);
1047                 for (; m_hold != NULL; m_hold = m_hold_next) {
1048                         m_hold_next = m_hold->m_nextpkt;
1049                         m_hold->m_nextpkt = NULL;
1050                         /* Avoid confusing lower layers. */
1051                         m_clrprotoflags(m_hold);
1052                         (*ifp->if_output)(ifp, m_hold, &sa, NULL);
1053                 }
1054         } else
1055                 LLE_WUNLOCK(la);
1056 }
1057
1058 /*
1059  * Updates @la fields used by fast path code.
1060  */
1061 static void
1062 arp_update_lle(struct arphdr *ah, struct ifnet *ifp, struct llentry *la)
1063 {
1064
1065         memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen);
1066         la->la_flags |= LLE_VALID;
1067 }
1068
1069 static void
1070 arp_mark_lle_reachable(struct llentry *la)
1071 {
1072         int canceled;
1073
1074         LLE_WLOCK_ASSERT(la);
1075
1076         EVENTHANDLER_INVOKE(lle_event, la, LLENTRY_RESOLVED);
1077
1078         if (!(la->la_flags & LLE_STATIC)) {
1079                 LLE_ADDREF(la);
1080                 la->la_expire = time_uptime + V_arpt_keep;
1081                 canceled = callout_reset(&la->lle_timer,
1082                     hz * V_arpt_keep, arptimer, la);
1083                 if (canceled)
1084                         LLE_REMREF(la);
1085         }
1086         la->la_asked = 0;
1087         la->la_preempt = V_arp_maxtries;
1088 }
1089
1090 void
1091 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1092 {
1093         struct llentry *lle, *lle_tmp;
1094         struct sockaddr_in *dst_in;
1095         struct sockaddr *dst;
1096
1097         if (ifa->ifa_carp != NULL)
1098                 return;
1099
1100         ifa->ifa_rtrequest = NULL;
1101
1102         dst_in = IA_SIN(ifa);
1103         dst = (struct sockaddr *)dst_in;
1104
1105         if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) == INADDR_ANY)
1106                 return;
1107
1108         arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1109                         &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
1110
1111         /*
1112          * Interface address LLE record is considered static
1113          * because kernel code relies on LLE_STATIC flag to check
1114          * if these entries can be rewriten by arp updates.
1115          */
1116         lle = lltable_alloc_entry(LLTABLE(ifp), LLE_IFADDR | LLE_STATIC, dst);
1117         if (lle == NULL) {
1118                 log(LOG_INFO, "arp_ifinit: cannot create arp "
1119                     "entry for interface address\n");
1120                 return;
1121         }
1122
1123         IF_AFDATA_WLOCK(ifp);
1124         LLE_WLOCK(lle);
1125         /* Unlink any entry if exists */
1126         lle_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
1127         if (lle_tmp != NULL)
1128                 lltable_unlink_entry(LLTABLE(ifp), lle_tmp);
1129
1130         lltable_link_entry(LLTABLE(ifp), lle);
1131         IF_AFDATA_WUNLOCK(ifp);
1132
1133         if (lle_tmp != NULL)
1134                 EVENTHANDLER_INVOKE(lle_event, lle_tmp, LLENTRY_EXPIRED);
1135
1136         EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_RESOLVED);
1137         LLE_WUNLOCK(lle);
1138         if (lle_tmp != NULL)
1139                 lltable_free_entry(LLTABLE(ifp), lle_tmp);
1140 }
1141
1142 void
1143 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
1144 {
1145         if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
1146                 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1147                                 &IA_SIN(ifa)->sin_addr, enaddr);
1148         ifa->ifa_rtrequest = NULL;
1149 }
1150
1151 static void
1152 arp_init(void)
1153 {
1154
1155         netisr_register(&arp_nh);
1156 }
1157 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);