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