1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993
5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved.
6df8bae1dSRodney W. Grimes *
7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes * are met:
10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes * without specific prior written permission.
18df8bae1dSRodney W. Grimes *
19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes */
31df8bae1dSRodney W. Grimes
32df8bae1dSRodney W. Grimes /*
33df8bae1dSRodney W. Grimes * Ethernet address resolution protocol.
34df8bae1dSRodney W. Grimes * TODO:
35df8bae1dSRodney W. Grimes * add "inuse/lock" bit (or ref. count) along with valid bit
36df8bae1dSRodney W. Grimes */
37df8bae1dSRodney W. Grimes
384b421e2dSMike Silbersack #include <sys/cdefs.h>
391d5e9e22SEivind Eklund #include "opt_inet.h"
401d5e9e22SEivind Eklund
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42e2e050c8SConrad Meyer #include <sys/eventhandler.h>
43df8bae1dSRodney W. Grimes #include <sys/kernel.h>
44cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
45ce02431fSDoug Rabson #include <sys/queue.h>
46885f1aa4SPoul-Henning Kamp #include <sys/sysctl.h>
47885f1aa4SPoul-Henning Kamp #include <sys/systm.h>
48885f1aa4SPoul-Henning Kamp #include <sys/mbuf.h>
49885f1aa4SPoul-Henning Kamp #include <sys/malloc.h>
50de34ad3fSJulian Elischer #include <sys/proc.h>
514458ac71SBruce Evans #include <sys/socket.h>
52885f1aa4SPoul-Henning Kamp #include <sys/syslog.h>
53df8bae1dSRodney W. Grimes
54df8bae1dSRodney W. Grimes #include <net/if.h>
5576039bc8SGleb Smirnoff #include <net/if_var.h>
56df8bae1dSRodney W. Grimes #include <net/if_dl.h>
573d0d5b21SJustin Hibbits #include <net/if_private.h>
58722012ccSJulian Elischer #include <net/if_types.h>
59748e0b0aSGarrett Wollman #include <net/netisr.h>
60c8f8e9c1SJulian Elischer #include <net/ethernet.h>
615736e6fbSBjoern A. Zeeb #include <net/route.h>
626ad7446cSAlexander V. Chernikov #include <net/route/nhop.h>
63530c0060SRobert Watson #include <net/vnet.h>
64df8bae1dSRodney W. Grimes
65df8bae1dSRodney W. Grimes #include <netinet/in.h>
669977be4aSAlexander V. Chernikov #include <netinet/in_fib.h>
67df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
686e6b3f7cSQing Li #include <net/if_llatbl.h>
69df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
7083e521ecSBjoern A. Zeeb #ifdef INET
719963e8a5SWill Andrews #include <netinet/ip_carp.h>
729963e8a5SWill Andrews #endif
73df8bae1dSRodney W. Grimes
74aed55708SRobert Watson #include <security/mac/mac_framework.h>
75aed55708SRobert Watson
7647e8d432SGleb Smirnoff #define SIN(s) ((const struct sockaddr_in *)(s))
77eec33ea0SAlexander V. Chernikov
78eec33ea0SAlexander V. Chernikov static struct timeval arp_lastlog;
79eec33ea0SAlexander V. Chernikov static int arp_curpps;
80eec33ea0SAlexander V. Chernikov static int arp_maxpps = 1;
81df8bae1dSRodney W. Grimes
82f8aee88fSAlexander V. Chernikov /* Simple ARP state machine */
83f8aee88fSAlexander V. Chernikov enum arp_llinfo_state {
84f8aee88fSAlexander V. Chernikov ARP_LLINFO_INCOMPLETE = 0, /* No LLE data */
85f8aee88fSAlexander V. Chernikov ARP_LLINFO_REACHABLE, /* LLE is valid */
86f8aee88fSAlexander V. Chernikov ARP_LLINFO_VERIFY, /* LLE is valid, need refresh */
87f8aee88fSAlexander V. Chernikov ARP_LLINFO_DELETED, /* LLE is deleted */
88f8aee88fSAlexander V. Chernikov };
89f8aee88fSAlexander V. Chernikov
90ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether);
917029da5cSPawel Biernacki static SYSCTL_NODE(_net_link_ether, PF_INET, inet,
927029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
937029da5cSPawel Biernacki "");
947029da5cSPawel Biernacki static SYSCTL_NODE(_net_link_ether, PF_ARP, arp,
957029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
967029da5cSPawel Biernacki "");
97df8bae1dSRodney W. Grimes
98df8bae1dSRodney W. Grimes /* timer values */
995f901c92SAndrew Turner VNET_DEFINE_STATIC(int, arpt_keep) = (20*60); /* once resolved, good for 20
100eddfbb76SRobert Watson * minutes */
1015f901c92SAndrew Turner VNET_DEFINE_STATIC(int, arp_maxtries) = 5;
1025f901c92SAndrew Turner VNET_DEFINE_STATIC(int, arp_proxyall) = 0;
1035f901c92SAndrew Turner VNET_DEFINE_STATIC(int, arpt_down) = 20; /* keep incomplete entries for
10493704ac5SQing Li * 20 seconds */
1055f901c92SAndrew Turner VNET_DEFINE_STATIC(int, arpt_rexmit) = 1; /* retransmit arp entries, sec*/
1065b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct arpstat, arpstat); /* ARP statistics, see if_arp.h */
1075b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(arpstat);
1085b7cb97cSAndrey V. Elsukov
1095b7cb97cSAndrey V. Elsukov #ifdef VIMAGE
1105b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(arpstat);
1115b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */
112e162ea60SGeorge V. Neville-Neil
1130da3f8c9SAlexander V. Chernikov VNET_DEFINE_STATIC(int, arp_maxhold) = 16;
114885f1aa4SPoul-Henning Kamp
1151e77c105SRobert Watson #define V_arpt_keep VNET(arpt_keep)
11693704ac5SQing Li #define V_arpt_down VNET(arpt_down)
117f8aee88fSAlexander V. Chernikov #define V_arpt_rexmit VNET(arpt_rexmit)
1181e77c105SRobert Watson #define V_arp_maxtries VNET(arp_maxtries)
1191e77c105SRobert Watson #define V_arp_proxyall VNET(arp_proxyall)
120e162ea60SGeorge V. Neville-Neil #define V_arp_maxhold VNET(arp_maxhold)
121885f1aa4SPoul-Henning Kamp
1226df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_VNET | CTLFLAG_RW,
123eddfbb76SRobert Watson &VNET_NAME(arpt_keep), 0,
124eddfbb76SRobert Watson "ARP entry lifetime in seconds");
1256df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_VNET | CTLFLAG_RW,
126eddfbb76SRobert Watson &VNET_NAME(arp_maxtries), 0,
1278b615593SMarko Zec "ARP resolution attempts before returning error");
1286df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_VNET | CTLFLAG_RW,
129eddfbb76SRobert Watson &VNET_NAME(arp_proxyall), 0,
1308b615593SMarko Zec "Enable proxy ARP for all suitable requests");
1316df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, wait, CTLFLAG_VNET | CTLFLAG_RW,
132e162ea60SGeorge V. Neville-Neil &VNET_NAME(arpt_down), 0,
133e162ea60SGeorge V. Neville-Neil "Incomplete ARP entry lifetime in seconds");
1345b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_link_ether_arp, OID_AUTO, stats, struct arpstat,
1355b7cb97cSAndrey V. Elsukov arpstat, "ARP statistics (struct arpstat, net/if_arp.h)");
1366df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxhold, CTLFLAG_VNET | CTLFLAG_RW,
137e162ea60SGeorge V. Neville-Neil &VNET_NAME(arp_maxhold), 0,
138e162ea60SGeorge V. Neville-Neil "Number of packets to hold per ARP entry");
139eec33ea0SAlexander V. Chernikov SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_log_per_second,
140eec33ea0SAlexander V. Chernikov CTLFLAG_RW, &arp_maxpps, 0,
141eec33ea0SAlexander V. Chernikov "Maximum number of remotely triggered ARP messages that can be "
142eec33ea0SAlexander V. Chernikov "logged per second");
143eec33ea0SAlexander V. Chernikov
1442d9db0bcSEric van Gyzen /*
1452d9db0bcSEric van Gyzen * Due to the exponential backoff algorithm used for the interval between GARP
1462d9db0bcSEric van Gyzen * retransmissions, the maximum number of retransmissions is limited for
1472d9db0bcSEric van Gyzen * sanity. This limit corresponds to a maximum interval between retransmissions
1482d9db0bcSEric van Gyzen * of 2^16 seconds ~= 18 hours.
1492d9db0bcSEric van Gyzen *
1502d9db0bcSEric van Gyzen * Making this limit more dynamic is more complicated than worthwhile,
1512d9db0bcSEric van Gyzen * especially since sending out GARPs spaced days apart would be of little
1522d9db0bcSEric van Gyzen * use. A maximum dynamic limit would look something like:
1532d9db0bcSEric van Gyzen *
1542d9db0bcSEric van Gyzen * const int max = fls(INT_MAX / hz) - 1;
1552d9db0bcSEric van Gyzen */
1562d9db0bcSEric van Gyzen #define MAX_GARP_RETRANSMITS 16
1572d9db0bcSEric van Gyzen static int sysctl_garp_rexmit(SYSCTL_HANDLER_ARGS);
158013784c9SKristof Provost VNET_DEFINE_STATIC(int, garp_rexmit_count) = 0; /* GARP retransmission setting. */
159013784c9SKristof Provost #define V_garp_rexmit_count VNET(garp_rexmit_count)
1602d9db0bcSEric van Gyzen
1612d9db0bcSEric van Gyzen SYSCTL_PROC(_net_link_ether_inet, OID_AUTO, garp_rexmit_count,
162013784c9SKristof Provost CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_MPSAFE|CTLFLAG_VNET,
163013784c9SKristof Provost &VNET_NAME(garp_rexmit_count), 0, sysctl_garp_rexmit, "I",
1642d9db0bcSEric van Gyzen "Number of times to retransmit GARP packets;"
1652d9db0bcSEric van Gyzen " 0 to disable, maximum of 16");
1662d9db0bcSEric van Gyzen
167b25d74e0SBjoern A. Zeeb VNET_DEFINE_STATIC(int, arp_log_level) = LOG_INFO; /* Min. log(9) level. */
168b25d74e0SBjoern A. Zeeb #define V_arp_log_level VNET(arp_log_level)
169b25d74e0SBjoern A. Zeeb SYSCTL_INT(_net_link_ether_arp, OID_AUTO, log_level, CTLFLAG_VNET | CTLFLAG_RW,
170b25d74e0SBjoern A. Zeeb &VNET_NAME(arp_log_level), 0,
171b25d74e0SBjoern A. Zeeb "Minimum log(9) level for recording rate limited arp log messages. "
172b25d74e0SBjoern A. Zeeb "The higher will be log more (emerg=0, info=6 (default), debug=7).");
173eec33ea0SAlexander V. Chernikov #define ARP_LOG(pri, ...) do { \
174b25d74e0SBjoern A. Zeeb if ((pri) <= V_arp_log_level && \
175b25d74e0SBjoern A. Zeeb ppsratecheck(&arp_lastlog, &arp_curpps, arp_maxpps)) \
176eec33ea0SAlexander V. Chernikov log((pri), "arp: " __VA_ARGS__); \
177eec33ea0SAlexander V. Chernikov } while (0)
178eec33ea0SAlexander V. Chernikov
1791cafed39SJonathan Lemon static void arpintr(struct mbuf *);
1804d77a549SAlfred Perlstein static void arptimer(void *);
1811d5e9e22SEivind Eklund #ifdef INET
1824d77a549SAlfred Perlstein static void in_arpinput(struct mbuf *);
1831d5e9e22SEivind Eklund #endif
18428e82295SGarrett Wollman
185f3bfa7d1SAlexander V. Chernikov static void arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr,
186f3bfa7d1SAlexander V. Chernikov struct ifnet *ifp, int bridged, struct llentry *la);
187c9a5c48aSKonrad Sewiłło-Jopek static void arp_mark_lle_reachable(struct llentry *la, struct ifnet *ifp);
188b13c5b5dSAlexander V. Chernikov static void arp_iflladdr(void *arg __unused, struct ifnet *ifp);
189f3bfa7d1SAlexander V. Chernikov
190b13c5b5dSAlexander V. Chernikov static eventhandler_tag iflladdr_tag;
191f3bfa7d1SAlexander V. Chernikov
192d4b5cae4SRobert Watson static const struct netisr_handler arp_nh = {
193d4b5cae4SRobert Watson .nh_name = "arp",
194d4b5cae4SRobert Watson .nh_handler = arpintr,
195d4b5cae4SRobert Watson .nh_proto = NETISR_ARP,
196d4b5cae4SRobert Watson .nh_policy = NETISR_POLICY_SOURCE,
197d4b5cae4SRobert Watson };
198d4b5cae4SRobert Watson
1996e6b3f7cSQing Li /*
2006e6b3f7cSQing Li * Timeout routine. Age arp_tab entries periodically.
201df8bae1dSRodney W. Grimes */
202df8bae1dSRodney W. Grimes static void
arptimer(void * arg)2031daaa65dSGleb Smirnoff arptimer(void *arg)
204df8bae1dSRodney W. Grimes {
205ea537929SGleb Smirnoff struct llentry *lle = (struct llentry *)arg;
2066e6b3f7cSQing Li struct ifnet *ifp;
207df8bae1dSRodney W. Grimes
208ea537929SGleb Smirnoff if (lle->la_flags & LLE_STATIC) {
2092575fbb8SRandall Stewart return;
2102575fbb8SRandall Stewart }
2112575fbb8SRandall Stewart LLE_WLOCK(lle);
2120447c136SAlexander V. Chernikov if (callout_pending(&lle->lle_timer)) {
2132575fbb8SRandall Stewart /*
2142575fbb8SRandall Stewart * Here we are a bit odd here in the treatment of
2152575fbb8SRandall Stewart * active/pending. If the pending bit is set, it got
2162575fbb8SRandall Stewart * rescheduled before I ran. The active
2172575fbb8SRandall Stewart * bit we ignore, since if it was stopped
2182575fbb8SRandall Stewart * in ll_tablefree() and was currently running
2192575fbb8SRandall Stewart * it would have return 0 so the code would
2202575fbb8SRandall Stewart * not have deleted it since the callout could
2212575fbb8SRandall Stewart * not be stopped so we want to go through
2222575fbb8SRandall Stewart * with the delete here now. If the callout
2232575fbb8SRandall Stewart * was restarted, the pending bit will be back on and
2242575fbb8SRandall Stewart * we just want to bail since the callout_reset would
2252575fbb8SRandall Stewart * return 1 and our reference would have been removed
2262575fbb8SRandall Stewart * by arpresolve() below.
2272575fbb8SRandall Stewart */
228ea537929SGleb Smirnoff LLE_WUNLOCK(lle);
229ea537929SGleb Smirnoff return;
230ea537929SGleb Smirnoff }
2316e6b3f7cSQing Li ifp = lle->lle_tbl->llt_ifp;
23254fc657dSGeorge V. Neville-Neil CURVNET_SET(ifp->if_vnet);
23309fe6320SNavdeep Parhar
234f8aee88fSAlexander V. Chernikov switch (lle->ln_state) {
235f8aee88fSAlexander V. Chernikov case ARP_LLINFO_REACHABLE:
236f8aee88fSAlexander V. Chernikov
237f8aee88fSAlexander V. Chernikov /*
238f8aee88fSAlexander V. Chernikov * Expiration time is approaching.
239f3a3b061SAlexander V. Chernikov * Request usage feedback from the datapath.
240f3a3b061SAlexander V. Chernikov * Change state and re-schedule ourselves.
241f8aee88fSAlexander V. Chernikov */
242f3a3b061SAlexander V. Chernikov llentry_request_feedback(lle);
243f8aee88fSAlexander V. Chernikov lle->ln_state = ARP_LLINFO_VERIFY;
244f8aee88fSAlexander V. Chernikov callout_schedule(&lle->lle_timer, hz * V_arpt_rexmit);
245f8aee88fSAlexander V. Chernikov LLE_WUNLOCK(lle);
246f8aee88fSAlexander V. Chernikov CURVNET_RESTORE();
247f8aee88fSAlexander V. Chernikov return;
248f8aee88fSAlexander V. Chernikov case ARP_LLINFO_VERIFY:
249f3a3b061SAlexander V. Chernikov if (llentry_get_hittime(lle) > 0 && lle->la_preempt > 0) {
250f8aee88fSAlexander V. Chernikov /* Entry was used, issue refresh request */
251b8a6e03fSGleb Smirnoff struct epoch_tracker et;
252f8aee88fSAlexander V. Chernikov struct in_addr dst;
253b8a6e03fSGleb Smirnoff
254f8aee88fSAlexander V. Chernikov dst = lle->r_l3addr.addr4;
255f8aee88fSAlexander V. Chernikov lle->la_preempt--;
256f8aee88fSAlexander V. Chernikov callout_schedule(&lle->lle_timer, hz * V_arpt_rexmit);
257f8aee88fSAlexander V. Chernikov LLE_WUNLOCK(lle);
258b8a6e03fSGleb Smirnoff NET_EPOCH_ENTER(et);
259f8aee88fSAlexander V. Chernikov arprequest(ifp, NULL, &dst, NULL);
260b8a6e03fSGleb Smirnoff NET_EPOCH_EXIT(et);
261f8aee88fSAlexander V. Chernikov CURVNET_RESTORE();
262f8aee88fSAlexander V. Chernikov return;
263f8aee88fSAlexander V. Chernikov }
264f8aee88fSAlexander V. Chernikov /* Nothing happened. Reschedule if not too late */
265f8aee88fSAlexander V. Chernikov if (lle->la_expire > time_uptime) {
266f8aee88fSAlexander V. Chernikov callout_schedule(&lle->lle_timer, hz * V_arpt_rexmit);
267f8aee88fSAlexander V. Chernikov LLE_WUNLOCK(lle);
268f8aee88fSAlexander V. Chernikov CURVNET_RESTORE();
269f8aee88fSAlexander V. Chernikov return;
270f8aee88fSAlexander V. Chernikov }
271f8aee88fSAlexander V. Chernikov break;
272f8aee88fSAlexander V. Chernikov case ARP_LLINFO_INCOMPLETE:
273f8aee88fSAlexander V. Chernikov case ARP_LLINFO_DELETED:
274f8aee88fSAlexander V. Chernikov break;
275f8aee88fSAlexander V. Chernikov }
276f8aee88fSAlexander V. Chernikov
277e364d8c4SNavdeep Parhar if ((lle->la_flags & LLE_DELETED) == 0) {
27809fe6320SNavdeep Parhar int evt;
27909fe6320SNavdeep Parhar
28009fe6320SNavdeep Parhar if (lle->la_flags & LLE_VALID)
28109fe6320SNavdeep Parhar evt = LLENTRY_EXPIRED;
28209fe6320SNavdeep Parhar else
28309fe6320SNavdeep Parhar evt = LLENTRY_TIMEDOUT;
28409fe6320SNavdeep Parhar EVENTHANDLER_INVOKE(lle_event, lle, evt);
28509fe6320SNavdeep Parhar }
28609fe6320SNavdeep Parhar
2870447c136SAlexander V. Chernikov callout_stop(&lle->lle_timer);
288ea537929SGleb Smirnoff
289ea537929SGleb Smirnoff /* XXX: LOR avoidance. We still have ref on lle. */
290ea537929SGleb Smirnoff LLE_WUNLOCK(lle);
291ea537929SGleb Smirnoff IF_AFDATA_LOCK(ifp);
292ea537929SGleb Smirnoff LLE_WLOCK(lle);
293ea537929SGleb Smirnoff
294b1ec2940SGleb Smirnoff /* Guard against race with other llentry_free(). */
295b1ec2940SGleb Smirnoff if (lle->la_flags & LLE_LINKED) {
296ea537929SGleb Smirnoff LLE_REMREF(lle);
297d3cdb716SAlexander V. Chernikov lltable_unlink_entry(lle->lle_tbl, lle);
298d3cdb716SAlexander V. Chernikov }
299b1ec2940SGleb Smirnoff IF_AFDATA_UNLOCK(ifp);
300b1ec2940SGleb Smirnoff
301d3cdb716SAlexander V. Chernikov size_t pkts_dropped = llentry_free(lle);
302d3cdb716SAlexander V. Chernikov
303d3cdb716SAlexander V. Chernikov ARPSTAT_ADD(dropped, pkts_dropped);
30454fc657dSGeorge V. Neville-Neil ARPSTAT_INC(timeouts);
305b1ec2940SGleb Smirnoff
30654fc657dSGeorge V. Neville-Neil CURVNET_RESTORE();
307df8bae1dSRodney W. Grimes }
308df8bae1dSRodney W. Grimes
309df8bae1dSRodney W. Grimes /*
3104fb3a820SAlexander V. Chernikov * Stores link-layer header for @ifp in format suitable for if_output()
3114fb3a820SAlexander V. Chernikov * into buffer @buf. Resulting header length is stored in @bufsize.
3124fb3a820SAlexander V. Chernikov *
3134fb3a820SAlexander V. Chernikov * Returns 0 on success.
3144fb3a820SAlexander V. Chernikov */
3154fb3a820SAlexander V. Chernikov static int
arp_fillheader(struct ifnet * ifp,struct arphdr * ah,int bcast,u_char * buf,size_t * bufsize)3164fb3a820SAlexander V. Chernikov arp_fillheader(struct ifnet *ifp, struct arphdr *ah, int bcast, u_char *buf,
3174fb3a820SAlexander V. Chernikov size_t *bufsize)
3184fb3a820SAlexander V. Chernikov {
3194fb3a820SAlexander V. Chernikov struct if_encap_req ereq;
3204fb3a820SAlexander V. Chernikov int error;
3214fb3a820SAlexander V. Chernikov
3224fb3a820SAlexander V. Chernikov bzero(buf, *bufsize);
3234fb3a820SAlexander V. Chernikov bzero(&ereq, sizeof(ereq));
3244fb3a820SAlexander V. Chernikov ereq.buf = buf;
3254fb3a820SAlexander V. Chernikov ereq.bufsize = *bufsize;
3264fb3a820SAlexander V. Chernikov ereq.rtype = IFENCAP_LL;
3274fb3a820SAlexander V. Chernikov ereq.family = AF_ARP;
3284fb3a820SAlexander V. Chernikov ereq.lladdr = ar_tha(ah);
3294fb3a820SAlexander V. Chernikov ereq.hdata = (u_char *)ah;
3304fb3a820SAlexander V. Chernikov if (bcast)
3314fb3a820SAlexander V. Chernikov ereq.flags = IFENCAP_FLAG_BROADCAST;
3324fb3a820SAlexander V. Chernikov error = ifp->if_requestencap(ifp, &ereq);
3334fb3a820SAlexander V. Chernikov if (error == 0)
3344fb3a820SAlexander V. Chernikov *bufsize = ereq.bufsize;
3354fb3a820SAlexander V. Chernikov
3364fb3a820SAlexander V. Chernikov return (error);
3374fb3a820SAlexander V. Chernikov }
3384fb3a820SAlexander V. Chernikov
3394fb3a820SAlexander V. Chernikov /*
340df8bae1dSRodney W. Grimes * Broadcast an ARP request. Caller specifies:
341df8bae1dSRodney W. Grimes * - arp header source ip address
342df8bae1dSRodney W. Grimes * - arp header target ip address
343df8bae1dSRodney W. Grimes * - arp header source ethernet address
344df8bae1dSRodney W. Grimes */
345a4c69b8bSBjoern A. Zeeb static int
arprequest_internal(struct ifnet * ifp,const struct in_addr * sip,const struct in_addr * tip,u_char * enaddr)346a4c69b8bSBjoern A. Zeeb arprequest_internal(struct ifnet *ifp, const struct in_addr *sip,
34747e8d432SGleb Smirnoff const struct in_addr *tip, u_char *enaddr)
348df8bae1dSRodney W. Grimes {
349e952fa39SMatthew N. Dodd struct mbuf *m;
350e952fa39SMatthew N. Dodd struct arphdr *ah;
351df8bae1dSRodney W. Grimes struct sockaddr sa;
352aa24ae3cSGleb Smirnoff u_char *carpaddr = NULL;
3534fb3a820SAlexander V. Chernikov uint8_t linkhdr[LLE_MAX_LINKHDR];
3544fb3a820SAlexander V. Chernikov size_t linkhdrsize;
3554fb3a820SAlexander V. Chernikov struct route ro;
3564fb3a820SAlexander V. Chernikov int error;
357df8bae1dSRodney W. Grimes
358b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT();
359b8a6e03fSGleb Smirnoff
3606e6b3f7cSQing Li if (sip == NULL) {
3616e6b3f7cSQing Li /*
3626e6b3f7cSQing Li * The caller did not supply a source address, try to find
3636e6b3f7cSQing Li * a compatible one among those assigned to this interface.
3646e6b3f7cSQing Li */
3656e6b3f7cSQing Li struct ifaddr *ifa;
3666e6b3f7cSQing Li
367d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
368aa24ae3cSGleb Smirnoff if (ifa->ifa_addr->sa_family != AF_INET)
3696e6b3f7cSQing Li continue;
370aa24ae3cSGleb Smirnoff
371aa24ae3cSGleb Smirnoff if (ifa->ifa_carp) {
372aa24ae3cSGleb Smirnoff if ((*carp_iamatch_p)(ifa, &carpaddr) == 0)
373aa24ae3cSGleb Smirnoff continue;
374aa24ae3cSGleb Smirnoff sip = &IA_SIN(ifa)->sin_addr;
375aa24ae3cSGleb Smirnoff } else {
376aa24ae3cSGleb Smirnoff carpaddr = NULL;
377aa24ae3cSGleb Smirnoff sip = &IA_SIN(ifa)->sin_addr;
378aa24ae3cSGleb Smirnoff }
379aa24ae3cSGleb Smirnoff
3806e6b3f7cSQing Li if (0 == ((sip->s_addr ^ tip->s_addr) &
381aa24ae3cSGleb Smirnoff IA_MASKSIN(ifa)->sin_addr.s_addr))
3826e6b3f7cSQing Li break; /* found it. */
3836e6b3f7cSQing Li }
3846e6b3f7cSQing Li if (sip == NULL) {
3856e6b3f7cSQing Li printf("%s: cannot find matching address\n", __func__);
386a4c69b8bSBjoern A. Zeeb return (EADDRNOTAVAIL);
3876e6b3f7cSQing Li }
3886e6b3f7cSQing Li }
389aa24ae3cSGleb Smirnoff if (enaddr == NULL)
390aa24ae3cSGleb Smirnoff enaddr = carpaddr ? carpaddr : (u_char *)IF_LLADDR(ifp);
3916e6b3f7cSQing Li
392eb1b1807SGleb Smirnoff if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
393a4c69b8bSBjoern A. Zeeb return (ENOMEM);
39464bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
395546451a2SGleb Smirnoff 2 * ifp->if_addrlen;
39664bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len;
397ed6a66caSRobert Watson M_ALIGN(m, m->m_len);
39864bf80ceSMatthew N. Dodd ah = mtod(m, struct arphdr *);
39964bf80ceSMatthew N. Dodd bzero((caddr_t)ah, m->m_len);
40019527d3eSRobert Watson #ifdef MAC
401b9b0dac3SRobert Watson mac_netinet_arp_send(ifp, m);
40219527d3eSRobert Watson #endif
403322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP);
404322dcb8dSMax Khon ah->ar_hln = ifp->if_addrlen; /* hardware address length */
405322dcb8dSMax Khon ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
406322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REQUEST);
40747e8d432SGleb Smirnoff bcopy(enaddr, ar_sha(ah), ah->ar_hln);
40847e8d432SGleb Smirnoff bcopy(sip, ar_spa(ah), ah->ar_pln);
40947e8d432SGleb Smirnoff bcopy(tip, ar_tpa(ah), ah->ar_pln);
41064bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP;
41164bf80ceSMatthew N. Dodd sa.sa_len = 2;
4124fb3a820SAlexander V. Chernikov
4134fb3a820SAlexander V. Chernikov /* Calculate link header for sending frame */
4144fb3a820SAlexander V. Chernikov bzero(&ro, sizeof(ro));
4154fb3a820SAlexander V. Chernikov linkhdrsize = sizeof(linkhdr);
4164fb3a820SAlexander V. Chernikov error = arp_fillheader(ifp, ah, 1, linkhdr, &linkhdrsize);
4174fb3a820SAlexander V. Chernikov if (error != 0 && error != EAFNOSUPPORT) {
418f5777c12Sorange30 m_freem(m);
4194fb3a820SAlexander V. Chernikov ARP_LOG(LOG_ERR, "Failed to calculate ARP header on %s: %d\n",
4204fb3a820SAlexander V. Chernikov if_name(ifp), error);
421a4c69b8bSBjoern A. Zeeb return (error);
4224fb3a820SAlexander V. Chernikov }
4234fb3a820SAlexander V. Chernikov
4244fb3a820SAlexander V. Chernikov ro.ro_prepend = linkhdr;
4254fb3a820SAlexander V. Chernikov ro.ro_plen = linkhdrsize;
4264fb3a820SAlexander V. Chernikov ro.ro_flags = 0;
4274fb3a820SAlexander V. Chernikov
42864bf80ceSMatthew N. Dodd m->m_flags |= M_BCAST;
42986bd0491SAndre Oppermann m_clrprotoflags(m); /* Avoid confusing lower layers. */
430a4c69b8bSBjoern A. Zeeb error = (*ifp->if_output)(ifp, m, &sa, &ro);
43154fc657dSGeorge V. Neville-Neil ARPSTAT_INC(txrequests);
432b25d74e0SBjoern A. Zeeb if (error) {
433b25d74e0SBjoern A. Zeeb ARPSTAT_INC(txerrors);
434a4c69b8bSBjoern A. Zeeb ARP_LOG(LOG_DEBUG, "Failed to send ARP packet on %s: %d\n",
435a4c69b8bSBjoern A. Zeeb if_name(ifp), error);
436b25d74e0SBjoern A. Zeeb }
437a4c69b8bSBjoern A. Zeeb return (error);
438df8bae1dSRodney W. Grimes }
439df8bae1dSRodney W. Grimes
440a4c69b8bSBjoern A. Zeeb void
arprequest(struct ifnet * ifp,const struct in_addr * sip,const struct in_addr * tip,u_char * enaddr)441a4c69b8bSBjoern A. Zeeb arprequest(struct ifnet *ifp, const struct in_addr *sip,
442a4c69b8bSBjoern A. Zeeb const struct in_addr *tip, u_char *enaddr)
443a4c69b8bSBjoern A. Zeeb {
444a4c69b8bSBjoern A. Zeeb
445a4c69b8bSBjoern A. Zeeb (void) arprequest_internal(ifp, sip, tip, enaddr);
446a4c69b8bSBjoern A. Zeeb }
4474fb3a820SAlexander V. Chernikov
448df8bae1dSRodney W. Grimes /*
449512e30efSAlexander V. Chernikov * Resolve an IP address into an ethernet address - heavy version.
450512e30efSAlexander V. Chernikov * Used internally by arpresolve().
451945aad9cSBjoern A. Zeeb * We have already checked that we can't use an existing lle without
452945aad9cSBjoern A. Zeeb * modification so we have to acquire an LLE_EXCLUSIVE lle lock.
453cd46a114SLuigi Rizzo *
454945aad9cSBjoern A. Zeeb * On success, desten and pflags are filled in and the function returns 0;
455cd46a114SLuigi Rizzo * If the packet must be held pending resolution, we return EWOULDBLOCK
456cd46a114SLuigi Rizzo * On other errors, we return the corresponding error code.
457b6ae6984SJulian Elischer * Note that m_freem() handles NULL.
458df8bae1dSRodney W. Grimes */
459512e30efSAlexander V. Chernikov static int
arpresolve_full(struct ifnet * ifp,int is_gw,int flags,struct mbuf * m,const struct sockaddr * dst,u_char * desten,uint32_t * pflags,struct llentry ** plle)4604fb3a820SAlexander V. Chernikov arpresolve_full(struct ifnet *ifp, int is_gw, int flags, struct mbuf *m,
4616d768226SGeorge V. Neville-Neil const struct sockaddr *dst, u_char *desten, uint32_t *pflags,
4626d768226SGeorge V. Neville-Neil struct llentry **plle)
463df8bae1dSRodney W. Grimes {
4645a255516SAlexander V. Chernikov struct llentry *la = NULL, *la_tmp;
465512e30efSAlexander V. Chernikov int error, renew;
4664fb3a820SAlexander V. Chernikov char *lladdr;
4674fb3a820SAlexander V. Chernikov int ll_len;
468df8bae1dSRodney W. Grimes
469b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT();
470b8a6e03fSGleb Smirnoff
47174860d4fSAlexander V. Chernikov if (pflags != NULL)
47274860d4fSAlexander V. Chernikov *pflags = 0;
4736d768226SGeorge V. Neville-Neil if (plle != NULL)
4746d768226SGeorge V. Neville-Neil *plle = NULL;
47574860d4fSAlexander V. Chernikov
476b8a6e03fSGleb Smirnoff if ((flags & LLE_CREATE) == 0)
477512e30efSAlexander V. Chernikov la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
478512e30efSAlexander V. Chernikov if (la == NULL && (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0) {
4795a255516SAlexander V. Chernikov la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
4801ed7bf1eSGleb Smirnoff if (la == NULL) {
4818144690aSEric van Gyzen char addrbuf[INET_ADDRSTRLEN];
4828144690aSEric van Gyzen
4831ed7bf1eSGleb Smirnoff log(LOG_DEBUG,
484743c072aSAlan Somers "arpresolve: can't allocate llinfo for %s on %s\n",
4858144690aSEric van Gyzen inet_ntoa_r(SIN(dst)->sin_addr, addrbuf),
4868144690aSEric van Gyzen if_name(ifp));
4875a255516SAlexander V. Chernikov m_freem(m);
4885a255516SAlexander V. Chernikov return (EINVAL);
4895a255516SAlexander V. Chernikov }
4905a255516SAlexander V. Chernikov
4915a255516SAlexander V. Chernikov IF_AFDATA_WLOCK(ifp);
4925a255516SAlexander V. Chernikov LLE_WLOCK(la);
4935a255516SAlexander V. Chernikov la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
4945a255516SAlexander V. Chernikov /* Prefer ANY existing lle over newly-created one */
4955a255516SAlexander V. Chernikov if (la_tmp == NULL)
4965a255516SAlexander V. Chernikov lltable_link_entry(LLTABLE(ifp), la);
4975a255516SAlexander V. Chernikov IF_AFDATA_WUNLOCK(ifp);
4985a255516SAlexander V. Chernikov if (la_tmp != NULL) {
4995a255516SAlexander V. Chernikov lltable_free_entry(LLTABLE(ifp), la);
5005a255516SAlexander V. Chernikov la = la_tmp;
5015a255516SAlexander V. Chernikov }
5025a255516SAlexander V. Chernikov }
5035a255516SAlexander V. Chernikov if (la == NULL) {
5041ed7bf1eSGleb Smirnoff m_freem(m);
5056e6b3f7cSQing Li return (EINVAL);
5061ed7bf1eSGleb Smirnoff }
507a20e2538SGleb Smirnoff
5086e6b3f7cSQing Li if ((la->la_flags & LLE_VALID) &&
509a98c06f1SGleb Smirnoff ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
5104fb3a820SAlexander V. Chernikov if (flags & LLE_ADDRONLY) {
5114fb3a820SAlexander V. Chernikov lladdr = la->ll_addr;
5124fb3a820SAlexander V. Chernikov ll_len = ifp->if_addrlen;
5134fb3a820SAlexander V. Chernikov } else {
5144fb3a820SAlexander V. Chernikov lladdr = la->r_linkdata;
5154fb3a820SAlexander V. Chernikov ll_len = la->r_hdrlen;
5164fb3a820SAlexander V. Chernikov }
5174fb3a820SAlexander V. Chernikov bcopy(lladdr, desten, ll_len);
518f8aee88fSAlexander V. Chernikov
5191435dcd9SAlexander V. Chernikov /* Notify LLE code that the entry was used by datapath */
520f3a3b061SAlexander V. Chernikov llentry_provide_feedback(la);
52174860d4fSAlexander V. Chernikov if (pflags != NULL)
522f8aee88fSAlexander V. Chernikov *pflags = la->la_flags & (LLE_VALID|LLE_IFADDR);
5236d768226SGeorge V. Neville-Neil if (plle) {
5246d768226SGeorge V. Neville-Neil LLE_ADDREF(la);
5256d768226SGeorge V. Neville-Neil *plle = la;
5266d768226SGeorge V. Neville-Neil }
527f0516b3cSErmal Luçi LLE_WUNLOCK(la);
528f0516b3cSErmal Luçi return (0);
529df8bae1dSRodney W. Grimes }
5306e6b3f7cSQing Li
531a98c06f1SGleb Smirnoff renew = (la->la_asked == 0 || la->la_expire != time_uptime);
532d18b4becSArseny Smalyuk
53308aadfbbSJonathan Lemon /*
534df8bae1dSRodney W. Grimes * There is an arptab entry, but no ethernet address
535e162ea60SGeorge V. Neville-Neil * response yet. Add the mbuf to the list, dropping
536e162ea60SGeorge V. Neville-Neil * the oldest packet if we have exceeded the system
537e162ea60SGeorge V. Neville-Neil * setting.
538df8bae1dSRodney W. Grimes */
5396e6b3f7cSQing Li if (m != NULL) {
540d18b4becSArseny Smalyuk size_t dropped = lltable_append_entry_queue(la, m, V_arp_maxhold);
541d18b4becSArseny Smalyuk ARPSTAT_ADD(dropped, dropped);
54254fc657dSGeorge V. Neville-Neil }
543d18b4becSArseny Smalyuk
544e1ff74c5SGleb Smirnoff /*
545e1ff74c5SGleb Smirnoff * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
546e1ff74c5SGleb Smirnoff * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
547e1ff74c5SGleb Smirnoff * if we have already sent arp_maxtries ARP requests. Retransmit the
548e1ff74c5SGleb Smirnoff * ARP request, but not faster than one request per second.
549e1ff74c5SGleb Smirnoff */
550603724d3SBjoern A. Zeeb if (la->la_asked < V_arp_maxtries)
551e1ff74c5SGleb Smirnoff error = EWOULDBLOCK; /* First request. */
552e1ff74c5SGleb Smirnoff else
55374860d4fSAlexander V. Chernikov error = is_gw != 0 ? EHOSTUNREACH : EHOSTDOWN;
554e1ff74c5SGleb Smirnoff
5556e6b3f7cSQing Li if (renew) {
556a4c69b8bSBjoern A. Zeeb int canceled, e;
557becba438SBjoern A. Zeeb
5586e6b3f7cSQing Li LLE_ADDREF(la);
559a98c06f1SGleb Smirnoff la->la_expire = time_uptime;
5600447c136SAlexander V. Chernikov canceled = callout_reset(&la->lle_timer, hz * V_arpt_down,
561becba438SBjoern A. Zeeb arptimer, la);
562becba438SBjoern A. Zeeb if (canceled)
563becba438SBjoern A. Zeeb LLE_REMREF(la);
56495ebcabeSMaxim Konovalov la->la_asked++;
5656e6b3f7cSQing Li LLE_WUNLOCK(la);
566a4c69b8bSBjoern A. Zeeb e = arprequest_internal(ifp, NULL, &SIN(dst)->sin_addr, NULL);
567a4c69b8bSBjoern A. Zeeb /*
568a4c69b8bSBjoern A. Zeeb * Only overwrite 'error' in case of error; in case of success
569a4c69b8bSBjoern A. Zeeb * the proper return value was already set above.
570a4c69b8bSBjoern A. Zeeb */
571a4c69b8bSBjoern A. Zeeb if (e != 0)
572a4c69b8bSBjoern A. Zeeb return (e);
5736e6b3f7cSQing Li return (error);
5746e6b3f7cSQing Li }
575512e30efSAlexander V. Chernikov
5766e6b3f7cSQing Li LLE_WUNLOCK(la);
577e1ff74c5SGleb Smirnoff return (error);
578df8bae1dSRodney W. Grimes }
579df8bae1dSRodney W. Grimes
580df8bae1dSRodney W. Grimes /*
5814fb3a820SAlexander V. Chernikov * Lookups link header based on an IP address.
582512e30efSAlexander V. Chernikov * On input:
583512e30efSAlexander V. Chernikov * ifp is the interface we use
584512e30efSAlexander V. Chernikov * is_gw != 0 if @dst represents gateway to some destination
585512e30efSAlexander V. Chernikov * m is the mbuf. May be NULL if we don't have a packet.
586512e30efSAlexander V. Chernikov * dst is the next hop,
5874fb3a820SAlexander V. Chernikov * desten is the storage to put LL header.
588f8aee88fSAlexander V. Chernikov * flags returns subset of lle flags: LLE_VALID | LLE_IFADDR
589512e30efSAlexander V. Chernikov *
5904fb3a820SAlexander V. Chernikov * On success, full/partial link header and flags are filled in and
5914fb3a820SAlexander V. Chernikov * the function returns 0.
592512e30efSAlexander V. Chernikov * If the packet must be held pending resolution, we return EWOULDBLOCK
593512e30efSAlexander V. Chernikov * On other errors, we return the corresponding error code.
594512e30efSAlexander V. Chernikov * Note that m_freem() handles NULL.
595512e30efSAlexander V. Chernikov */
596512e30efSAlexander V. Chernikov int
arpresolve(struct ifnet * ifp,int is_gw,struct mbuf * m,const struct sockaddr * dst,u_char * desten,uint32_t * pflags,struct llentry ** plle)597512e30efSAlexander V. Chernikov arpresolve(struct ifnet *ifp, int is_gw, struct mbuf *m,
5986d768226SGeorge V. Neville-Neil const struct sockaddr *dst, u_char *desten, uint32_t *pflags,
5996d768226SGeorge V. Neville-Neil struct llentry **plle)
600512e30efSAlexander V. Chernikov {
60199d628d5SPedro F. Giffuni struct llentry *la = NULL;
602512e30efSAlexander V. Chernikov
603b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT();
604b8a6e03fSGleb Smirnoff
605512e30efSAlexander V. Chernikov if (pflags != NULL)
606512e30efSAlexander V. Chernikov *pflags = 0;
6076d768226SGeorge V. Neville-Neil if (plle != NULL)
6086d768226SGeorge V. Neville-Neil *plle = NULL;
609512e30efSAlexander V. Chernikov
610512e30efSAlexander V. Chernikov if (m != NULL) {
611512e30efSAlexander V. Chernikov if (m->m_flags & M_BCAST) {
612512e30efSAlexander V. Chernikov /* broadcast */
613512e30efSAlexander V. Chernikov (void)memcpy(desten,
614512e30efSAlexander V. Chernikov ifp->if_broadcastaddr, ifp->if_addrlen);
615512e30efSAlexander V. Chernikov return (0);
616512e30efSAlexander V. Chernikov }
617512e30efSAlexander V. Chernikov if (m->m_flags & M_MCAST) {
618512e30efSAlexander V. Chernikov /* multicast */
619512e30efSAlexander V. Chernikov ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
620512e30efSAlexander V. Chernikov return (0);
621512e30efSAlexander V. Chernikov }
622512e30efSAlexander V. Chernikov }
623512e30efSAlexander V. Chernikov
624ea17754cSMike Karels la = lla_lookup(LLTABLE(ifp), plle ? LLE_EXCLUSIVE : LLE_UNLOCKED, dst);
625f8aee88fSAlexander V. Chernikov if (la != NULL && (la->r_flags & RLLE_VALID) != 0) {
626f8aee88fSAlexander V. Chernikov /* Entry found, let's copy lle info */
6274fb3a820SAlexander V. Chernikov bcopy(la->r_linkdata, desten, la->r_hdrlen);
628512e30efSAlexander V. Chernikov if (pflags != NULL)
629f8aee88fSAlexander V. Chernikov *pflags = LLE_VALID | (la->r_flags & RLLE_IFADDR);
6301435dcd9SAlexander V. Chernikov /* Notify the LLE handling code that the entry was used. */
631f3a3b061SAlexander V. Chernikov llentry_provide_feedback(la);
632ea17754cSMike Karels if (plle) {
633ea17754cSMike Karels LLE_ADDREF(la);
634ea17754cSMike Karels *plle = la;
635ea17754cSMike Karels LLE_WUNLOCK(la);
636ea17754cSMike Karels }
637512e30efSAlexander V. Chernikov return (0);
638512e30efSAlexander V. Chernikov }
639ea17754cSMike Karels if (plle && la)
640ea17754cSMike Karels LLE_WUNLOCK(la);
641512e30efSAlexander V. Chernikov
6424fb3a820SAlexander V. Chernikov return (arpresolve_full(ifp, is_gw, la == NULL ? LLE_CREATE : 0, m, dst,
6436d768226SGeorge V. Neville-Neil desten, pflags, plle));
644512e30efSAlexander V. Chernikov }
645512e30efSAlexander V. Chernikov
646512e30efSAlexander V. Chernikov /*
647df8bae1dSRodney W. Grimes * Common length and type checks are done here,
648df8bae1dSRodney W. Grimes * then the protocol-specific routine is called.
649df8bae1dSRodney W. Grimes */
650885f1aa4SPoul-Henning Kamp static void
arpintr(struct mbuf * m)6511cafed39SJonathan Lemon arpintr(struct mbuf *m)
652df8bae1dSRodney W. Grimes {
6531cafed39SJonathan Lemon struct arphdr *ar;
654eec33ea0SAlexander V. Chernikov struct ifnet *ifp;
655deb6bda6SAlexander V. Chernikov char *layer;
656deb6bda6SAlexander V. Chernikov int hlen;
657df8bae1dSRodney W. Grimes
658eec33ea0SAlexander V. Chernikov ifp = m->m_pkthdr.rcvif;
659eec33ea0SAlexander V. Chernikov
66076ec7b2fSRobert Watson if (m->m_len < sizeof(struct arphdr) &&
66184365e2bSMatthew Dillon ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
662eec33ea0SAlexander V. Chernikov ARP_LOG(LOG_NOTICE, "packet with short header received on %s\n",
663eec33ea0SAlexander V. Chernikov if_name(ifp));
6641cafed39SJonathan Lemon return;
66576ec7b2fSRobert Watson }
66676ec7b2fSRobert Watson ar = mtod(m, struct arphdr *);
66776ec7b2fSRobert Watson
668deb6bda6SAlexander V. Chernikov /* Check if length is sufficient */
66989bc0426SGleb Smirnoff if (m->m_len < arphdr_len(ar)) {
67089bc0426SGleb Smirnoff m = m_pullup(m, arphdr_len(ar));
67189bc0426SGleb Smirnoff if (m == NULL) {
672eec33ea0SAlexander V. Chernikov ARP_LOG(LOG_NOTICE, "short packet received on %s\n",
673eec33ea0SAlexander V. Chernikov if_name(ifp));
674deb6bda6SAlexander V. Chernikov return;
675deb6bda6SAlexander V. Chernikov }
676deb6bda6SAlexander V. Chernikov ar = mtod(m, struct arphdr *);
67789bc0426SGleb Smirnoff }
678deb6bda6SAlexander V. Chernikov
679deb6bda6SAlexander V. Chernikov hlen = 0;
680deb6bda6SAlexander V. Chernikov layer = "";
681deb6bda6SAlexander V. Chernikov switch (ntohs(ar->ar_hrd)) {
682deb6bda6SAlexander V. Chernikov case ARPHRD_ETHER:
683deb6bda6SAlexander V. Chernikov hlen = ETHER_ADDR_LEN; /* RFC 826 */
684deb6bda6SAlexander V. Chernikov layer = "ethernet";
685deb6bda6SAlexander V. Chernikov break;
686fcdf9a19SDenny Page case ARPHRD_IEEE802:
687fcdf9a19SDenny Page hlen = ETHER_ADDR_LEN;
688fcdf9a19SDenny Page layer = "ieee802";
689fcdf9a19SDenny Page break;
690deb6bda6SAlexander V. Chernikov case ARPHRD_INFINIBAND:
691deb6bda6SAlexander V. Chernikov hlen = 20; /* RFC 4391, INFINIBAND_ALEN */
692deb6bda6SAlexander V. Chernikov layer = "infiniband";
693deb6bda6SAlexander V. Chernikov break;
694deb6bda6SAlexander V. Chernikov case ARPHRD_IEEE1394:
695deb6bda6SAlexander V. Chernikov hlen = 0; /* SHALL be 16 */ /* RFC 2734 */
696deb6bda6SAlexander V. Chernikov layer = "firewire";
697deb6bda6SAlexander V. Chernikov
698deb6bda6SAlexander V. Chernikov /*
699ddb13598SKevin Lo * Restrict too long hardware addresses.
700deb6bda6SAlexander V. Chernikov * Currently we are capable of handling 20-byte
701deb6bda6SAlexander V. Chernikov * addresses ( sizeof(lle->ll_addr) )
702deb6bda6SAlexander V. Chernikov */
703deb6bda6SAlexander V. Chernikov if (ar->ar_hln >= 20)
704deb6bda6SAlexander V. Chernikov hlen = 16;
705deb6bda6SAlexander V. Chernikov break;
706deb6bda6SAlexander V. Chernikov default:
707eec33ea0SAlexander V. Chernikov ARP_LOG(LOG_NOTICE,
708ddb13598SKevin Lo "packet with unknown hardware format 0x%02d received on "
709ddb13598SKevin Lo "%s\n", ntohs(ar->ar_hrd), if_name(ifp));
71076ec7b2fSRobert Watson m_freem(m);
7111cafed39SJonathan Lemon return;
71276ec7b2fSRobert Watson }
71376ec7b2fSRobert Watson
714deb6bda6SAlexander V. Chernikov if (hlen != 0 && hlen != ar->ar_hln) {
715eec33ea0SAlexander V. Chernikov ARP_LOG(LOG_NOTICE,
716eec33ea0SAlexander V. Chernikov "packet with invalid %s address length %d received on %s\n",
717eec33ea0SAlexander V. Chernikov layer, ar->ar_hln, if_name(ifp));
71876ec7b2fSRobert Watson m_freem(m);
7191cafed39SJonathan Lemon return;
72076ec7b2fSRobert Watson }
721df8bae1dSRodney W. Grimes
72254fc657dSGeorge V. Neville-Neil ARPSTAT_INC(received);
723df8bae1dSRodney W. Grimes switch (ntohs(ar->ar_pro)) {
7241d5e9e22SEivind Eklund #ifdef INET
725df8bae1dSRodney W. Grimes case ETHERTYPE_IP:
726df8bae1dSRodney W. Grimes in_arpinput(m);
7271cafed39SJonathan Lemon return;
7281d5e9e22SEivind Eklund #endif
729df8bae1dSRodney W. Grimes }
730df8bae1dSRodney W. Grimes m_freem(m);
731df8bae1dSRodney W. Grimes }
732df8bae1dSRodney W. Grimes
7331d5e9e22SEivind Eklund #ifdef INET
734df8bae1dSRodney W. Grimes /*
735df8bae1dSRodney W. Grimes * ARP for Internet protocols on 10 Mb/s Ethernet.
736df8bae1dSRodney W. Grimes * Algorithm is that given in RFC 826.
737df8bae1dSRodney W. Grimes * In addition, a sanity check is performed on the sender
738df8bae1dSRodney W. Grimes * protocol address, to catch impersonators.
739df8bae1dSRodney W. Grimes * We no longer handle negotiations for use of trailer protocol:
740df8bae1dSRodney W. Grimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
741df8bae1dSRodney W. Grimes * along with IP replies if we wanted trailers sent to us,
742df8bae1dSRodney W. Grimes * and also sent them in response to IP replies.
743df8bae1dSRodney W. Grimes * This allowed either end to announce the desire to receive
744df8bae1dSRodney W. Grimes * trailer packets.
745df8bae1dSRodney W. Grimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
746df8bae1dSRodney W. Grimes * but formerly didn't normally send requests.
747df8bae1dSRodney W. Grimes */
7483269187dSAlfred Perlstein static int log_arp_wrong_iface = 1;
749e3d123d6SAlfred Perlstein static int log_arp_movements = 1;
75039393906SGleb Smirnoff static int log_arp_permanent_modify = 1;
751478df1d5SGleb Smirnoff static int allow_multicast = 0;
7523269187dSAlfred Perlstein
7533269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
7543269187dSAlfred Perlstein &log_arp_wrong_iface, 0,
7553269187dSAlfred Perlstein "log arp packets arriving on the wrong interface");
756e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
757e3d123d6SAlfred Perlstein &log_arp_movements, 0,
75875ce3221SAlfred Perlstein "log arp replies from MACs different than the one in the cache");
75939393906SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
76039393906SGleb Smirnoff &log_arp_permanent_modify, 0,
76139393906SGleb Smirnoff "log arp replies from MACs different than the one in the permanent arp entry");
762478df1d5SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, allow_multicast, CTLFLAG_RW,
763478df1d5SGleb Smirnoff &allow_multicast, 0, "accept multicast addresses");
7643269187dSAlfred Perlstein
765df8bae1dSRodney W. Grimes static void
in_arpinput(struct mbuf * m)766f2565d68SRobert Watson in_arpinput(struct mbuf *m)
767df8bae1dSRodney W. Grimes {
768e952fa39SMatthew N. Dodd struct arphdr *ah;
769e952fa39SMatthew N. Dodd struct ifnet *ifp = m->m_pkthdr.rcvif;
7705a255516SAlexander V. Chernikov struct llentry *la = NULL, *la_tmp;
771ca925d9cSJonathan Lemon struct ifaddr *ifa;
772ca925d9cSJonathan Lemon struct in_ifaddr *ia;
773df8bae1dSRodney W. Grimes struct sockaddr sa;
774df8bae1dSRodney W. Grimes struct in_addr isaddr, itaddr, myaddr;
775a9771948SGleb Smirnoff u_int8_t *enaddr = NULL;
776f3bfa7d1SAlexander V. Chernikov int op;
77780b11ee4SPhilip Paeps int bridged = 0, is_bridge = 0;
778f3bfa7d1SAlexander V. Chernikov int carped;
7798e7e854cSKip Macy struct sockaddr_in sin;
7805a255516SAlexander V. Chernikov struct sockaddr *dst;
7816ad7446cSAlexander V. Chernikov struct nhop_object *nh;
7824fb3a820SAlexander V. Chernikov uint8_t linkhdr[LLE_MAX_LINKHDR];
7834fb3a820SAlexander V. Chernikov struct route ro;
7844fb3a820SAlexander V. Chernikov size_t linkhdrsize;
7854fb3a820SAlexander V. Chernikov int lladdr_off;
7864fb3a820SAlexander V. Chernikov int error;
7878144690aSEric van Gyzen char addrbuf[INET_ADDRSTRLEN];
788b8a6e03fSGleb Smirnoff
789b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT();
7909977be4aSAlexander V. Chernikov
7918e7e854cSKip Macy sin.sin_len = sizeof(struct sockaddr_in);
7928e7e854cSKip Macy sin.sin_family = AF_INET;
79329910a5aSKip Macy sin.sin_addr.s_addr = 0;
794df8bae1dSRodney W. Grimes
79574948aa6SAndrew Thompson if (ifp->if_bridge)
7968f867517SAndrew Thompson bridged = 1;
79780b11ee4SPhilip Paeps if (ifp->if_type == IFT_BRIDGE)
79880b11ee4SPhilip Paeps is_bridge = 1;
7998f867517SAndrew Thompson
800eec33ea0SAlexander V. Chernikov /*
801eec33ea0SAlexander V. Chernikov * We already have checked that mbuf contains enough contiguous data
802eec33ea0SAlexander V. Chernikov * to hold entire arp message according to the arp header.
803eec33ea0SAlexander V. Chernikov */
804322dcb8dSMax Khon ah = mtod(m, struct arphdr *);
805eec33ea0SAlexander V. Chernikov
80609d3f895SGeorge V. Neville-Neil /*
80709d3f895SGeorge V. Neville-Neil * ARP is only for IPv4 so we can reject packets with
80809d3f895SGeorge V. Neville-Neil * a protocol length not equal to an IPv4 address.
80909d3f895SGeorge V. Neville-Neil */
81009d3f895SGeorge V. Neville-Neil if (ah->ar_pln != sizeof(struct in_addr)) {
8115d81d095SGleb Smirnoff ARP_LOG(LOG_NOTICE, "requested protocol length != %zu\n",
81209d3f895SGeorge V. Neville-Neil sizeof(struct in_addr));
813414676baSGleb Smirnoff goto drop;
81409d3f895SGeorge V. Neville-Neil }
81509d3f895SGeorge V. Neville-Neil
816478df1d5SGleb Smirnoff if (allow_multicast == 0 && ETHER_IS_MULTICAST(ar_sha(ah))) {
8175d81d095SGleb Smirnoff ARP_LOG(LOG_NOTICE, "%*D is multicast\n",
818c9168718SGleb Smirnoff ifp->if_addrlen, (u_char *)ar_sha(ah), ":");
819414676baSGleb Smirnoff goto drop;
82009d3f895SGeorge V. Neville-Neil }
82109d3f895SGeorge V. Neville-Neil
822322dcb8dSMax Khon op = ntohs(ah->ar_op);
823322dcb8dSMax Khon (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
824322dcb8dSMax Khon (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
82532439868SGleb Smirnoff
82654fc657dSGeorge V. Neville-Neil if (op == ARPOP_REPLY)
82754fc657dSGeorge V. Neville-Neil ARPSTAT_INC(rxreplies);
82854fc657dSGeorge V. Neville-Neil
829ca925d9cSJonathan Lemon /*
830ca925d9cSJonathan Lemon * For a bridge, we want to check the address irrespective
831ca925d9cSJonathan Lemon * of the receive interface. (This will change slightly
832ca925d9cSJonathan Lemon * when we have clusters of interfaces).
833ca925d9cSJonathan Lemon */
834c8ee75f2SGleb Smirnoff CK_LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
83596561547SAndrew Thompson if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
8366e6b3f7cSQing Li ia->ia_ifp == ifp) &&
83708b68b0eSGleb Smirnoff itaddr.s_addr == ia->ia_addr.sin_addr.s_addr &&
83808b68b0eSGleb Smirnoff (ia->ia_ifa.ifa_carp == NULL ||
83908b68b0eSGleb Smirnoff (*carp_iamatch_p)(&ia->ia_ifa, &enaddr))) {
84009d54778SRobert Watson ifa_ref(&ia->ia_ifa);
8412ef4a436SGleb Smirnoff goto match;
8422ef4a436SGleb Smirnoff }
8432ef4a436SGleb Smirnoff }
844c8ee75f2SGleb Smirnoff CK_LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
84596561547SAndrew Thompson if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
8466e6b3f7cSQing Li ia->ia_ifp == ifp) &&
84709d54778SRobert Watson isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
84809d54778SRobert Watson ifa_ref(&ia->ia_ifa);
849ca925d9cSJonathan Lemon goto match;
85009d54778SRobert Watson }
85180b11ee4SPhilip Paeps
85280b11ee4SPhilip Paeps #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \
85380b11ee4SPhilip Paeps (ia->ia_ifp->if_bridge == ifp->if_softc && \
85480b11ee4SPhilip Paeps !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \
85580b11ee4SPhilip Paeps addr == ia->ia_addr.sin_addr.s_addr)
85680b11ee4SPhilip Paeps /*
85780b11ee4SPhilip Paeps * Check the case when bridge shares its MAC address with
85880b11ee4SPhilip Paeps * some of its children, so packets are claimed by bridge
85980b11ee4SPhilip Paeps * itself (bridge_input() does it first), but they are really
86080b11ee4SPhilip Paeps * meant to be destined to the bridge member.
86180b11ee4SPhilip Paeps */
86280b11ee4SPhilip Paeps if (is_bridge) {
863c8ee75f2SGleb Smirnoff CK_LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
86480b11ee4SPhilip Paeps if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
86509d54778SRobert Watson ifa_ref(&ia->ia_ifa);
86680b11ee4SPhilip Paeps ifp = ia->ia_ifp;
86780b11ee4SPhilip Paeps goto match;
86880b11ee4SPhilip Paeps }
86980b11ee4SPhilip Paeps }
87080b11ee4SPhilip Paeps }
87180b11ee4SPhilip Paeps #undef BDG_MEMBER_MATCHES_ARP
87280b11ee4SPhilip Paeps
873ca925d9cSJonathan Lemon /*
874d8b84d9eSJonathan Lemon * No match, use the first inet address on the receive interface
875ca925d9cSJonathan Lemon * as a dummy address for the rest of the function.
876ca925d9cSJonathan Lemon */
877d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
8789de96e89SGleb Smirnoff if (ifa->ifa_addr->sa_family == AF_INET &&
8799de96e89SGleb Smirnoff (ifa->ifa_carp == NULL ||
8809de96e89SGleb Smirnoff (*carp_iamatch_p)(ifa, &enaddr))) {
881ec691a10SJonathan Lemon ia = ifatoia(ifa);
88209d54778SRobert Watson ifa_ref(ifa);
883ec691a10SJonathan Lemon goto match;
884ec691a10SJonathan Lemon }
88509d54778SRobert Watson
886ec691a10SJonathan Lemon /*
887ec691a10SJonathan Lemon * If bridging, fall back to using any inet address.
888ec691a10SJonathan Lemon */
8892144431cSGleb Smirnoff if (!bridged || (ia = CK_STAILQ_FIRST(&V_in_ifaddrhead)) == NULL)
890b2a8ac7cSLuigi Rizzo goto drop;
89109d54778SRobert Watson ifa_ref(&ia->ia_ifa);
892ca925d9cSJonathan Lemon match:
893a9771948SGleb Smirnoff if (!enaddr)
894a9771948SGleb Smirnoff enaddr = (u_int8_t *)IF_LLADDR(ifp);
89508b68b0eSGleb Smirnoff carped = (ia->ia_ifa.ifa_carp != NULL);
896ca925d9cSJonathan Lemon myaddr = ia->ia_addr.sin_addr;
89709d54778SRobert Watson ifa_free(&ia->ia_ifa);
898a9771948SGleb Smirnoff if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
899b2a8ac7cSLuigi Rizzo goto drop; /* it's from me, ignore it. */
900322dcb8dSMax Khon if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
9015d81d095SGleb Smirnoff ARP_LOG(LOG_NOTICE, "link address is broadcast for IP address "
9028144690aSEric van Gyzen "%s!\n", inet_ntoa_r(isaddr, addrbuf));
903b2a8ac7cSLuigi Rizzo goto drop;
904df8bae1dSRodney W. Grimes }
905f3bfa7d1SAlexander V. Chernikov
906f3bfa7d1SAlexander V. Chernikov if (ifp->if_addrlen != ah->ar_hln) {
907f3bfa7d1SAlexander V. Chernikov ARP_LOG(LOG_WARNING, "from %*D: addr len: new %d, "
908f3bfa7d1SAlexander V. Chernikov "i/f %d (ignored)\n", ifp->if_addrlen,
909f3bfa7d1SAlexander V. Chernikov (u_char *) ar_sha(ah), ":", ah->ar_hln,
910f3bfa7d1SAlexander V. Chernikov ifp->if_addrlen);
911f3bfa7d1SAlexander V. Chernikov goto drop;
912f3bfa7d1SAlexander V. Chernikov }
913f3bfa7d1SAlexander V. Chernikov
91400fcf9d1SRobert Watson /*
91500fcf9d1SRobert Watson * Warn if another host is using the same IP address, but only if the
91600fcf9d1SRobert Watson * IP address isn't 0.0.0.0, which is used for DHCP only, in which
91700fcf9d1SRobert Watson * case we suppress the warning to avoid false positive complaints of
91800fcf9d1SRobert Watson * potential misconfiguration.
91900fcf9d1SRobert Watson */
92008b68b0eSGleb Smirnoff if (!bridged && !carped && isaddr.s_addr == myaddr.s_addr &&
92108b68b0eSGleb Smirnoff myaddr.s_addr != 0) {
9225d81d095SGleb Smirnoff ARP_LOG(LOG_ERR, "%*D is using my IP address %s on %s!\n",
923322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
9248144690aSEric van Gyzen inet_ntoa_r(isaddr, addrbuf), ifp->if_xname);
925df8bae1dSRodney W. Grimes itaddr = myaddr;
92654fc657dSGeorge V. Neville-Neil ARPSTAT_INC(dupips);
927df8bae1dSRodney W. Grimes goto reply;
928df8bae1dSRodney W. Grimes }
929deb62e28SRuslan Ermilov if (ifp->if_flags & IFF_STATICARP)
930deb62e28SRuslan Ermilov goto reply;
9318b07e49aSJulian Elischer
9326e6b3f7cSQing Li bzero(&sin, sizeof(sin));
9336e6b3f7cSQing Li sin.sin_len = sizeof(struct sockaddr_in);
9346e6b3f7cSQing Li sin.sin_family = AF_INET;
9356e6b3f7cSQing Li sin.sin_addr = isaddr;
9365a255516SAlexander V. Chernikov dst = (struct sockaddr *)&sin;
9375a255516SAlexander V. Chernikov la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
938f3bfa7d1SAlexander V. Chernikov if (la != NULL)
939f3bfa7d1SAlexander V. Chernikov arp_check_update_lle(ah, isaddr, ifp, bridged, la);
940f3bfa7d1SAlexander V. Chernikov else if (itaddr.s_addr == myaddr.s_addr) {
941e162ea60SGeorge V. Neville-Neil /*
942942e4b4bSAlexander V. Chernikov * Request/reply to our address, but no lle exists yet.
9434fb3a820SAlexander V. Chernikov * Calculate full link prepend to use in lle.
944e162ea60SGeorge V. Neville-Neil */
9454fb3a820SAlexander V. Chernikov linkhdrsize = sizeof(linkhdr);
9464fb3a820SAlexander V. Chernikov if (lltable_calc_llheader(ifp, AF_INET, ar_sha(ah), linkhdr,
9474fb3a820SAlexander V. Chernikov &linkhdrsize, &lladdr_off) != 0)
9484fb3a820SAlexander V. Chernikov goto reply;
9494fb3a820SAlexander V. Chernikov
9504fb3a820SAlexander V. Chernikov /* Allocate new entry */
9515a255516SAlexander V. Chernikov la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
952942e4b4bSAlexander V. Chernikov if (la == NULL) {
953942e4b4bSAlexander V. Chernikov /*
954942e4b4bSAlexander V. Chernikov * lle creation may fail if source address belongs
955942e4b4bSAlexander V. Chernikov * to non-directly connected subnet. However, we
956942e4b4bSAlexander V. Chernikov * will try to answer the request instead of dropping
957942e4b4bSAlexander V. Chernikov * frame.
958942e4b4bSAlexander V. Chernikov */
959942e4b4bSAlexander V. Chernikov goto reply;
960942e4b4bSAlexander V. Chernikov }
9614fb3a820SAlexander V. Chernikov lltable_set_entry_addr(ifp, la, linkhdr, linkhdrsize,
9624fb3a820SAlexander V. Chernikov lladdr_off);
9635a255516SAlexander V. Chernikov
9645a255516SAlexander V. Chernikov IF_AFDATA_WLOCK(ifp);
9655a255516SAlexander V. Chernikov LLE_WLOCK(la);
9665a255516SAlexander V. Chernikov la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
9675a255516SAlexander V. Chernikov
9685a255516SAlexander V. Chernikov /*
9695a255516SAlexander V. Chernikov * Check if lle still does not exists.
9705a255516SAlexander V. Chernikov * If it does, that means that we either
9715a255516SAlexander V. Chernikov * 1) have configured it explicitly, via
9725a255516SAlexander V. Chernikov * 1a) 'arp -s' static entry or
9735a255516SAlexander V. Chernikov * 1b) interface address static record
9745a255516SAlexander V. Chernikov * or
9755a255516SAlexander V. Chernikov * 2) it was the result of sending first packet to-host
9765a255516SAlexander V. Chernikov * or
9775a255516SAlexander V. Chernikov * 3) it was another arp reply packet we handled in
9785a255516SAlexander V. Chernikov * different thread.
9795a255516SAlexander V. Chernikov *
9805a255516SAlexander V. Chernikov * In all cases except 3) we definitely need to prefer
9815a255516SAlexander V. Chernikov * existing lle. For the sake of simplicity, prefer any
9825a255516SAlexander V. Chernikov * existing lle over newly-create one.
9835a255516SAlexander V. Chernikov */
9845a255516SAlexander V. Chernikov if (la_tmp == NULL)
9855a255516SAlexander V. Chernikov lltable_link_entry(LLTABLE(ifp), la);
986f3bfa7d1SAlexander V. Chernikov IF_AFDATA_WUNLOCK(ifp);
9875a255516SAlexander V. Chernikov
9885a255516SAlexander V. Chernikov if (la_tmp == NULL) {
989c9a5c48aSKonrad Sewiłło-Jopek arp_mark_lle_reachable(la, ifp);
990e162ea60SGeorge V. Neville-Neil LLE_WUNLOCK(la);
9915a255516SAlexander V. Chernikov } else {
9925a255516SAlexander V. Chernikov /* Free newly-create entry and handle packet */
9935a255516SAlexander V. Chernikov lltable_free_entry(LLTABLE(ifp), la);
9945a255516SAlexander V. Chernikov la = la_tmp;
9955a255516SAlexander V. Chernikov la_tmp = NULL;
9965a255516SAlexander V. Chernikov arp_check_update_lle(ah, isaddr, ifp, bridged, la);
9975a255516SAlexander V. Chernikov /* arp_check_update_lle() returns @la unlocked */
998f4048639SBjoern A. Zeeb }
9995a255516SAlexander V. Chernikov la = NULL;
1000a4141c63SAlexander V. Chernikov }
10016e6b3f7cSQing Li reply:
1002b2a8ac7cSLuigi Rizzo if (op != ARPOP_REQUEST)
1003b2a8ac7cSLuigi Rizzo goto drop;
100454fc657dSGeorge V. Neville-Neil ARPSTAT_INC(rxrequests);
10056e6b3f7cSQing Li
1006df8bae1dSRodney W. Grimes if (itaddr.s_addr == myaddr.s_addr) {
10078b07e49aSJulian Elischer /* Shortcut.. the receiving interface is the target. */
1008322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1009a9771948SGleb Smirnoff (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1010df8bae1dSRodney W. Grimes } else {
101166bc03d4SAlexander V. Chernikov /*
101266bc03d4SAlexander V. Chernikov * Destination address is not ours. Check if
101366bc03d4SAlexander V. Chernikov * proxyarp entry exists or proxyarp is turned on globally.
101466bc03d4SAlexander V. Chernikov */
101566bc03d4SAlexander V. Chernikov struct llentry *lle;
1016897d75c9SQing Li
1017cd29a779SQing Li sin.sin_addr = itaddr;
1018cd29a779SQing Li lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
1019cd29a779SQing Li
1020cd29a779SQing Li if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
1021cd29a779SQing Li (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
10224fb3a820SAlexander V. Chernikov (void)memcpy(ar_sha(ah), lle->ll_addr, ah->ar_hln);
1023cd29a779SQing Li LLE_RUNLOCK(lle);
1024cd29a779SQing Li } else {
1025cd29a779SQing Li if (lle != NULL)
1026cd29a779SQing Li LLE_RUNLOCK(lle);
1027cd29a779SQing Li
1028603724d3SBjoern A. Zeeb if (!V_arp_proxyall)
1029b2a8ac7cSLuigi Rizzo goto drop;
103028e82295SGarrett Wollman
10316ad7446cSAlexander V. Chernikov NET_EPOCH_ASSERT();
10326ad7446cSAlexander V. Chernikov nh = fib4_lookup(ifp->if_fib, itaddr, 0, 0, 0);
10336ad7446cSAlexander V. Chernikov if (nh == NULL)
1034b2a8ac7cSLuigi Rizzo goto drop;
1035897d75c9SQing Li
103628e82295SGarrett Wollman /*
103728e82295SGarrett Wollman * Don't send proxies for nodes on the same interface
103828e82295SGarrett Wollman * as this one came out of, or we'll get into a fight
103928e82295SGarrett Wollman * over who claims what Ether address.
104028e82295SGarrett Wollman */
10416ad7446cSAlexander V. Chernikov if (nh->nh_ifp == ifp)
1042b2a8ac7cSLuigi Rizzo goto drop;
1043cc728227SDavid Malone
1044897d75c9SQing Li (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1045cd29a779SQing Li (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1046897d75c9SQing Li
1047cc728227SDavid Malone /*
1048cc728227SDavid Malone * Also check that the node which sent the ARP packet
10496bccea7cSRebecca Cran * is on the interface we expect it to be on. This
1050cc728227SDavid Malone * avoids ARP chaos if an interface is connected to the
1051cc728227SDavid Malone * wrong network.
1052cc728227SDavid Malone */
1053cc728227SDavid Malone
10546ad7446cSAlexander V. Chernikov nh = fib4_lookup(ifp->if_fib, isaddr, 0, 0, 0);
10556ad7446cSAlexander V. Chernikov if (nh == NULL)
1056b2a8ac7cSLuigi Rizzo goto drop;
10576ad7446cSAlexander V. Chernikov if (nh->nh_ifp != ifp) {
10585d81d095SGleb Smirnoff ARP_LOG(LOG_INFO, "proxy: ignoring request"
10599977be4aSAlexander V. Chernikov " from %s via %s\n",
10608144690aSEric van Gyzen inet_ntoa_r(isaddr, addrbuf),
10618144690aSEric van Gyzen ifp->if_xname);
1062b2a8ac7cSLuigi Rizzo goto drop;
1063cc728227SDavid Malone }
1064cc728227SDavid Malone
1065ac234f93SGarrett Wollman #ifdef DEBUG_PROXY
10668144690aSEric van Gyzen printf("arp: proxying for %s\n",
10678144690aSEric van Gyzen inet_ntoa_r(itaddr, addrbuf));
1068ac234f93SGarrett Wollman #endif
1069df8bae1dSRodney W. Grimes }
1070cd29a779SQing Li }
1071df8bae1dSRodney W. Grimes
1072d0558157SBruce M Simpson if (itaddr.s_addr == myaddr.s_addr &&
1073d0558157SBruce M Simpson IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
1074d0558157SBruce M Simpson /* RFC 3927 link-local IPv4; always reply by broadcast. */
1075d0558157SBruce M Simpson #ifdef DEBUG_LINKLOCAL
1076d0558157SBruce M Simpson printf("arp: sending reply for link-local addr %s\n",
10778144690aSEric van Gyzen inet_ntoa_r(itaddr, addrbuf));
1078d0558157SBruce M Simpson #endif
1079d0558157SBruce M Simpson m->m_flags |= M_BCAST;
1080d0558157SBruce M Simpson m->m_flags &= ~M_MCAST;
1081d0558157SBruce M Simpson } else {
1082d0558157SBruce M Simpson /* default behaviour; never reply by broadcast. */
1083d0558157SBruce M Simpson m->m_flags &= ~(M_BCAST|M_MCAST);
1084d0558157SBruce M Simpson }
1085322dcb8dSMax Khon (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1086322dcb8dSMax Khon (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
1087322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REPLY);
1088322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
108964bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
109064bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len;
10912303570fSAndrey V. Elsukov m->m_pkthdr.rcvif = NULL;
109264bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP;
109364bf80ceSMatthew N. Dodd sa.sa_len = 2;
10944fb3a820SAlexander V. Chernikov
10954fb3a820SAlexander V. Chernikov /* Calculate link header for sending frame */
10964fb3a820SAlexander V. Chernikov bzero(&ro, sizeof(ro));
10974fb3a820SAlexander V. Chernikov linkhdrsize = sizeof(linkhdr);
10984fb3a820SAlexander V. Chernikov error = arp_fillheader(ifp, ah, 0, linkhdr, &linkhdrsize);
10994fb3a820SAlexander V. Chernikov
11004fb3a820SAlexander V. Chernikov /*
11014fb3a820SAlexander V. Chernikov * arp_fillheader() may fail due to lack of support inside encap request
11024fb3a820SAlexander V. Chernikov * routing. This is not necessary an error, AF_ARP can/should be handled
11034fb3a820SAlexander V. Chernikov * by if_output().
11044fb3a820SAlexander V. Chernikov */
11054fb3a820SAlexander V. Chernikov if (error != 0 && error != EAFNOSUPPORT) {
11064fb3a820SAlexander V. Chernikov ARP_LOG(LOG_ERR, "Failed to calculate ARP header on %s: %d\n",
11074fb3a820SAlexander V. Chernikov if_name(ifp), error);
1108f5777c12Sorange30 goto drop;
11094fb3a820SAlexander V. Chernikov }
11104fb3a820SAlexander V. Chernikov
11114fb3a820SAlexander V. Chernikov ro.ro_prepend = linkhdr;
11124fb3a820SAlexander V. Chernikov ro.ro_plen = linkhdrsize;
11134fb3a820SAlexander V. Chernikov ro.ro_flags = 0;
11144fb3a820SAlexander V. Chernikov
111586bd0491SAndre Oppermann m_clrprotoflags(m); /* Avoid confusing lower layers. */
11164fb3a820SAlexander V. Chernikov (*ifp->if_output)(ifp, m, &sa, &ro);
111754fc657dSGeorge V. Neville-Neil ARPSTAT_INC(txreplies);
1118df8bae1dSRodney W. Grimes return;
1119b2a8ac7cSLuigi Rizzo
1120b2a8ac7cSLuigi Rizzo drop:
1121b2a8ac7cSLuigi Rizzo m_freem(m);
1122df8bae1dSRodney W. Grimes }
11231d5e9e22SEivind Eklund #endif
1124df8bae1dSRodney W. Grimes
11258482aa77SAlexander V. Chernikov static struct mbuf *
arp_grab_holdchain(struct llentry * la)11268482aa77SAlexander V. Chernikov arp_grab_holdchain(struct llentry *la)
11278482aa77SAlexander V. Chernikov {
11288482aa77SAlexander V. Chernikov struct mbuf *chain;
11298482aa77SAlexander V. Chernikov
11308482aa77SAlexander V. Chernikov LLE_WLOCK_ASSERT(la);
11318482aa77SAlexander V. Chernikov
11328482aa77SAlexander V. Chernikov chain = la->la_hold;
11338482aa77SAlexander V. Chernikov la->la_hold = NULL;
11348482aa77SAlexander V. Chernikov la->la_numheld = 0;
11358482aa77SAlexander V. Chernikov
11368482aa77SAlexander V. Chernikov return (chain);
11378482aa77SAlexander V. Chernikov }
11388482aa77SAlexander V. Chernikov
11398482aa77SAlexander V. Chernikov static void
arp_flush_holdchain(struct ifnet * ifp,struct llentry * la,struct mbuf * chain)11408482aa77SAlexander V. Chernikov arp_flush_holdchain(struct ifnet *ifp, struct llentry *la, struct mbuf *chain)
11418482aa77SAlexander V. Chernikov {
11428482aa77SAlexander V. Chernikov struct mbuf *m_hold, *m_hold_next;
11438482aa77SAlexander V. Chernikov struct sockaddr_in sin;
11448482aa77SAlexander V. Chernikov
11458482aa77SAlexander V. Chernikov NET_EPOCH_ASSERT();
11468482aa77SAlexander V. Chernikov
11478482aa77SAlexander V. Chernikov struct route ro = {
11488482aa77SAlexander V. Chernikov .ro_prepend = la->r_linkdata,
11498482aa77SAlexander V. Chernikov .ro_plen = la->r_hdrlen,
11508482aa77SAlexander V. Chernikov };
11518482aa77SAlexander V. Chernikov
11528482aa77SAlexander V. Chernikov lltable_fill_sa_entry(la, (struct sockaddr *)&sin);
11538482aa77SAlexander V. Chernikov
11548482aa77SAlexander V. Chernikov for (m_hold = chain; m_hold != NULL; m_hold = m_hold_next) {
11558482aa77SAlexander V. Chernikov m_hold_next = m_hold->m_nextpkt;
11568482aa77SAlexander V. Chernikov m_hold->m_nextpkt = NULL;
11578482aa77SAlexander V. Chernikov /* Avoid confusing lower layers. */
11588482aa77SAlexander V. Chernikov m_clrprotoflags(m_hold);
11598482aa77SAlexander V. Chernikov (*ifp->if_output)(ifp, m_hold, (struct sockaddr *)&sin, &ro);
11608482aa77SAlexander V. Chernikov }
11618482aa77SAlexander V. Chernikov }
11628482aa77SAlexander V. Chernikov
1163f3bfa7d1SAlexander V. Chernikov /*
1164f3bfa7d1SAlexander V. Chernikov * Checks received arp data against existing @la.
1165f3bfa7d1SAlexander V. Chernikov * Updates lle state/performs notification if necessary.
1166f3bfa7d1SAlexander V. Chernikov */
1167f3bfa7d1SAlexander V. Chernikov static void
arp_check_update_lle(struct arphdr * ah,struct in_addr isaddr,struct ifnet * ifp,int bridged,struct llentry * la)1168f3bfa7d1SAlexander V. Chernikov arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr, struct ifnet *ifp,
1169f3bfa7d1SAlexander V. Chernikov int bridged, struct llentry *la)
1170f3bfa7d1SAlexander V. Chernikov {
11714fb3a820SAlexander V. Chernikov uint8_t linkhdr[LLE_MAX_LINKHDR];
11724fb3a820SAlexander V. Chernikov size_t linkhdrsize;
11734fb3a820SAlexander V. Chernikov int lladdr_off;
11748144690aSEric van Gyzen char addrbuf[INET_ADDRSTRLEN];
1175f3bfa7d1SAlexander V. Chernikov
1176f3bfa7d1SAlexander V. Chernikov LLE_WLOCK_ASSERT(la);
1177f3bfa7d1SAlexander V. Chernikov
1178f3bfa7d1SAlexander V. Chernikov /* the following is not an error when doing bridging */
1179f3bfa7d1SAlexander V. Chernikov if (!bridged && la->lle_tbl->llt_ifp != ifp) {
1180f3bfa7d1SAlexander V. Chernikov if (log_arp_wrong_iface)
1181f3bfa7d1SAlexander V. Chernikov ARP_LOG(LOG_WARNING, "%s is on %s "
1182f3bfa7d1SAlexander V. Chernikov "but got reply from %*D on %s\n",
11838144690aSEric van Gyzen inet_ntoa_r(isaddr, addrbuf),
1184f3bfa7d1SAlexander V. Chernikov la->lle_tbl->llt_ifp->if_xname,
1185f3bfa7d1SAlexander V. Chernikov ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
1186f3bfa7d1SAlexander V. Chernikov ifp->if_xname);
1187f3bfa7d1SAlexander V. Chernikov LLE_WUNLOCK(la);
1188f3bfa7d1SAlexander V. Chernikov return;
1189f3bfa7d1SAlexander V. Chernikov }
1190f3bfa7d1SAlexander V. Chernikov if ((la->la_flags & LLE_VALID) &&
11914fb3a820SAlexander V. Chernikov bcmp(ar_sha(ah), la->ll_addr, ifp->if_addrlen)) {
1192f3bfa7d1SAlexander V. Chernikov if (la->la_flags & LLE_STATIC) {
1193f3bfa7d1SAlexander V. Chernikov LLE_WUNLOCK(la);
1194f3bfa7d1SAlexander V. Chernikov if (log_arp_permanent_modify)
1195f3bfa7d1SAlexander V. Chernikov ARP_LOG(LOG_ERR,
1196f3bfa7d1SAlexander V. Chernikov "%*D attempts to modify "
1197f3bfa7d1SAlexander V. Chernikov "permanent entry for %s on %s\n",
1198f3bfa7d1SAlexander V. Chernikov ifp->if_addrlen,
1199f3bfa7d1SAlexander V. Chernikov (u_char *)ar_sha(ah), ":",
12008144690aSEric van Gyzen inet_ntoa_r(isaddr, addrbuf),
12018144690aSEric van Gyzen ifp->if_xname);
1202f3bfa7d1SAlexander V. Chernikov return;
1203f3bfa7d1SAlexander V. Chernikov }
1204f3bfa7d1SAlexander V. Chernikov if (log_arp_movements) {
1205f3bfa7d1SAlexander V. Chernikov ARP_LOG(LOG_INFO, "%s moved from %*D "
1206f3bfa7d1SAlexander V. Chernikov "to %*D on %s\n",
12078144690aSEric van Gyzen inet_ntoa_r(isaddr, addrbuf),
1208f3bfa7d1SAlexander V. Chernikov ifp->if_addrlen,
120971949810SAndrey V. Elsukov (u_char *)la->ll_addr, ":",
1210f3bfa7d1SAlexander V. Chernikov ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
1211f3bfa7d1SAlexander V. Chernikov ifp->if_xname);
1212f3bfa7d1SAlexander V. Chernikov }
1213f3bfa7d1SAlexander V. Chernikov }
1214f3bfa7d1SAlexander V. Chernikov
12154fb3a820SAlexander V. Chernikov /* Calculate full link prepend to use in lle */
12164fb3a820SAlexander V. Chernikov linkhdrsize = sizeof(linkhdr);
12174fb3a820SAlexander V. Chernikov if (lltable_calc_llheader(ifp, AF_INET, ar_sha(ah), linkhdr,
1218dd91d844SMark Johnston &linkhdrsize, &lladdr_off) != 0) {
1219dd91d844SMark Johnston LLE_WUNLOCK(la);
1220f3bfa7d1SAlexander V. Chernikov return;
1221dd91d844SMark Johnston }
1222f3bfa7d1SAlexander V. Chernikov
12234fb3a820SAlexander V. Chernikov /* Check if something has changed */
12244fb3a820SAlexander V. Chernikov if (memcmp(la->r_linkdata, linkhdr, linkhdrsize) != 0 ||
12254fb3a820SAlexander V. Chernikov (la->la_flags & LLE_VALID) == 0) {
12264fb3a820SAlexander V. Chernikov /* Try to perform LLE update */
12274fb3a820SAlexander V. Chernikov if (lltable_try_set_entry_addr(ifp, la, linkhdr, linkhdrsize,
1228dd91d844SMark Johnston lladdr_off) == 0) {
1229dd91d844SMark Johnston LLE_WUNLOCK(la);
12304fb3a820SAlexander V. Chernikov return;
1231dd91d844SMark Johnston }
1232f8aee88fSAlexander V. Chernikov
1233f8aee88fSAlexander V. Chernikov /* Clear fast path feedback request if set */
1234f3a3b061SAlexander V. Chernikov llentry_mark_used(la);
1235f3bfa7d1SAlexander V. Chernikov }
1236f3bfa7d1SAlexander V. Chernikov
1237c9a5c48aSKonrad Sewiłło-Jopek arp_mark_lle_reachable(la, ifp);
1238f3bfa7d1SAlexander V. Chernikov
1239f3bfa7d1SAlexander V. Chernikov /*
1240f3bfa7d1SAlexander V. Chernikov * The packets are all freed within the call to the output
1241f3bfa7d1SAlexander V. Chernikov * routine.
1242f3bfa7d1SAlexander V. Chernikov *
1243f3bfa7d1SAlexander V. Chernikov * NB: The lock MUST be released before the call to the
1244f3bfa7d1SAlexander V. Chernikov * output routine.
1245f3bfa7d1SAlexander V. Chernikov */
1246f3bfa7d1SAlexander V. Chernikov if (la->la_hold != NULL) {
12478482aa77SAlexander V. Chernikov struct mbuf *chain;
12488482aa77SAlexander V. Chernikov
12498482aa77SAlexander V. Chernikov chain = arp_grab_holdchain(la);
1250f3bfa7d1SAlexander V. Chernikov LLE_WUNLOCK(la);
12518482aa77SAlexander V. Chernikov arp_flush_holdchain(ifp, la, chain);
1252f3bfa7d1SAlexander V. Chernikov } else
1253f3bfa7d1SAlexander V. Chernikov LLE_WUNLOCK(la);
1254f3bfa7d1SAlexander V. Chernikov }
1255f3bfa7d1SAlexander V. Chernikov
1256f3bfa7d1SAlexander V. Chernikov static void
arp_mark_lle_reachable(struct llentry * la,struct ifnet * ifp)1257c9a5c48aSKonrad Sewiłło-Jopek arp_mark_lle_reachable(struct llentry *la, struct ifnet *ifp)
1258f3bfa7d1SAlexander V. Chernikov {
1259f8aee88fSAlexander V. Chernikov int canceled, wtime;
1260f3bfa7d1SAlexander V. Chernikov
1261f3bfa7d1SAlexander V. Chernikov LLE_WLOCK_ASSERT(la);
1262f3bfa7d1SAlexander V. Chernikov
1263f8aee88fSAlexander V. Chernikov la->ln_state = ARP_LLINFO_REACHABLE;
1264f3bfa7d1SAlexander V. Chernikov EVENTHANDLER_INVOKE(lle_event, la, LLENTRY_RESOLVED);
1265f3bfa7d1SAlexander V. Chernikov
1266c9a5c48aSKonrad Sewiłło-Jopek if ((ifp->if_flags & IFF_STICKYARP) != 0)
1267c9a5c48aSKonrad Sewiłło-Jopek la->la_flags |= LLE_STATIC;
1268c9a5c48aSKonrad Sewiłło-Jopek
1269f3bfa7d1SAlexander V. Chernikov if (!(la->la_flags & LLE_STATIC)) {
1270f3bfa7d1SAlexander V. Chernikov LLE_ADDREF(la);
1271f3bfa7d1SAlexander V. Chernikov la->la_expire = time_uptime + V_arpt_keep;
1272f8aee88fSAlexander V. Chernikov wtime = V_arpt_keep - V_arp_maxtries * V_arpt_rexmit;
1273f8aee88fSAlexander V. Chernikov if (wtime < 0)
1274f8aee88fSAlexander V. Chernikov wtime = V_arpt_keep;
1275f3bfa7d1SAlexander V. Chernikov canceled = callout_reset(&la->lle_timer,
1276f8aee88fSAlexander V. Chernikov hz * wtime, arptimer, la);
1277f3bfa7d1SAlexander V. Chernikov if (canceled)
1278f3bfa7d1SAlexander V. Chernikov LLE_REMREF(la);
1279f3bfa7d1SAlexander V. Chernikov }
1280f3bfa7d1SAlexander V. Chernikov la->la_asked = 0;
1281f3bfa7d1SAlexander V. Chernikov la->la_preempt = V_arp_maxtries;
1282f3bfa7d1SAlexander V. Chernikov }
1283f3bfa7d1SAlexander V. Chernikov
12841c302b58SAlexander V. Chernikov /*
1285ff21796dSOleg Bulyzhin * Add permanent link-layer record for given interface address.
12861c302b58SAlexander V. Chernikov */
12871c302b58SAlexander V. Chernikov static __noinline void
arp_add_ifa_lle(struct ifnet * ifp,const struct sockaddr * dst)12881c302b58SAlexander V. Chernikov arp_add_ifa_lle(struct ifnet *ifp, const struct sockaddr *dst)
1289dd2e4102SGarrett Wollman {
12905a255516SAlexander V. Chernikov struct llentry *lle, *lle_tmp;
12915a255516SAlexander V. Chernikov
12926e6b3f7cSQing Li /*
12935a255516SAlexander V. Chernikov * Interface address LLE record is considered static
12945a255516SAlexander V. Chernikov * because kernel code relies on LLE_STATIC flag to check
12955a255516SAlexander V. Chernikov * if these entries can be rewriten by arp updates.
12966e6b3f7cSQing Li */
12975a255516SAlexander V. Chernikov lle = lltable_alloc_entry(LLTABLE(ifp), LLE_IFADDR | LLE_STATIC, dst);
12985a255516SAlexander V. Chernikov if (lle == NULL) {
12996e6b3f7cSQing Li log(LOG_INFO, "arp_ifinit: cannot create arp "
13006e6b3f7cSQing Li "entry for interface address\n");
13015a255516SAlexander V. Chernikov return;
1302ce9122fdSQing Li }
13035a255516SAlexander V. Chernikov
13045a255516SAlexander V. Chernikov IF_AFDATA_WLOCK(ifp);
13055a255516SAlexander V. Chernikov LLE_WLOCK(lle);
13065a255516SAlexander V. Chernikov /* Unlink any entry if exists */
13075a255516SAlexander V. Chernikov lle_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
13085a255516SAlexander V. Chernikov if (lle_tmp != NULL)
13095a255516SAlexander V. Chernikov lltable_unlink_entry(LLTABLE(ifp), lle_tmp);
13105a255516SAlexander V. Chernikov
13115a255516SAlexander V. Chernikov lltable_link_entry(LLTABLE(ifp), lle);
13125a255516SAlexander V. Chernikov IF_AFDATA_WUNLOCK(ifp);
13135a255516SAlexander V. Chernikov
13145a255516SAlexander V. Chernikov if (lle_tmp != NULL)
13155a255516SAlexander V. Chernikov EVENTHANDLER_INVOKE(lle_event, lle_tmp, LLENTRY_EXPIRED);
13165a255516SAlexander V. Chernikov
13175a255516SAlexander V. Chernikov EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_RESOLVED);
13185a255516SAlexander V. Chernikov LLE_WUNLOCK(lle);
13195a255516SAlexander V. Chernikov if (lle_tmp != NULL)
13205a255516SAlexander V. Chernikov lltable_free_entry(LLTABLE(ifp), lle_tmp);
1321dd2e4102SGarrett Wollman }
1322df5e1987SJonathan Lemon
13232d9db0bcSEric van Gyzen /*
13242d9db0bcSEric van Gyzen * Handle the garp_rexmit_count. Like sysctl_handle_int(), but limits the range
13252d9db0bcSEric van Gyzen * of valid values.
13262d9db0bcSEric van Gyzen */
13272d9db0bcSEric van Gyzen static int
sysctl_garp_rexmit(SYSCTL_HANDLER_ARGS)13282d9db0bcSEric van Gyzen sysctl_garp_rexmit(SYSCTL_HANDLER_ARGS)
13292d9db0bcSEric van Gyzen {
13302d9db0bcSEric van Gyzen int error;
13312d9db0bcSEric van Gyzen int rexmit_count = *(int *)arg1;
13322d9db0bcSEric van Gyzen
13332d9db0bcSEric van Gyzen error = sysctl_handle_int(oidp, &rexmit_count, 0, req);
13342d9db0bcSEric van Gyzen
13352d9db0bcSEric van Gyzen /* Enforce limits on any new value that may have been set. */
13362d9db0bcSEric van Gyzen if (!error && req->newptr) {
13372d9db0bcSEric van Gyzen /* A new value was set. */
13382d9db0bcSEric van Gyzen if (rexmit_count < 0) {
13392d9db0bcSEric van Gyzen rexmit_count = 0;
13402d9db0bcSEric van Gyzen } else if (rexmit_count > MAX_GARP_RETRANSMITS) {
13412d9db0bcSEric van Gyzen rexmit_count = MAX_GARP_RETRANSMITS;
13422d9db0bcSEric van Gyzen }
13432d9db0bcSEric van Gyzen *(int *)arg1 = rexmit_count;
13442d9db0bcSEric van Gyzen }
13452d9db0bcSEric van Gyzen
13462d9db0bcSEric van Gyzen return (error);
13472d9db0bcSEric van Gyzen }
13482d9db0bcSEric van Gyzen
13492d9db0bcSEric van Gyzen /*
13502d9db0bcSEric van Gyzen * Retransmit a Gratuitous ARP (GARP) and, if necessary, schedule a callout to
13512d9db0bcSEric van Gyzen * retransmit it again. A pending callout owns a reference to the ifa.
13522d9db0bcSEric van Gyzen */
13532d9db0bcSEric van Gyzen static void
garp_rexmit(void * arg)13542d9db0bcSEric van Gyzen garp_rexmit(void *arg)
13552d9db0bcSEric van Gyzen {
1356*38fdcca0SKristof Provost struct epoch_tracker et;
13572d9db0bcSEric van Gyzen struct in_ifaddr *ia = arg;
13582d9db0bcSEric van Gyzen
13592d9db0bcSEric van Gyzen if (callout_pending(&ia->ia_garp_timer) ||
13602d9db0bcSEric van Gyzen !callout_active(&ia->ia_garp_timer)) {
13612d9db0bcSEric van Gyzen IF_ADDR_WUNLOCK(ia->ia_ifa.ifa_ifp);
13622d9db0bcSEric van Gyzen ifa_free(&ia->ia_ifa);
13632d9db0bcSEric van Gyzen return;
13642d9db0bcSEric van Gyzen }
13652d9db0bcSEric van Gyzen
1366*38fdcca0SKristof Provost NET_EPOCH_ENTER(et);
13673838c6a3SKristof Provost CURVNET_SET(ia->ia_ifa.ifa_ifp->if_vnet);
13683838c6a3SKristof Provost
13692d9db0bcSEric van Gyzen /*
13702d9db0bcSEric van Gyzen * Drop lock while the ARP request is generated.
13712d9db0bcSEric van Gyzen */
13722d9db0bcSEric van Gyzen IF_ADDR_WUNLOCK(ia->ia_ifa.ifa_ifp);
13732d9db0bcSEric van Gyzen
13742d9db0bcSEric van Gyzen arprequest(ia->ia_ifa.ifa_ifp, &IA_SIN(ia)->sin_addr,
13752d9db0bcSEric van Gyzen &IA_SIN(ia)->sin_addr, IF_LLADDR(ia->ia_ifa.ifa_ifp));
13762d9db0bcSEric van Gyzen
13772d9db0bcSEric van Gyzen /*
13782d9db0bcSEric van Gyzen * Increment the count of retransmissions. If the count has reached the
13792d9db0bcSEric van Gyzen * maximum value, stop sending the GARP packets. Otherwise, schedule
13802d9db0bcSEric van Gyzen * the callout to retransmit another GARP packet.
13812d9db0bcSEric van Gyzen */
13822d9db0bcSEric van Gyzen ++ia->ia_garp_count;
1383013784c9SKristof Provost if (ia->ia_garp_count >= V_garp_rexmit_count) {
13842d9db0bcSEric van Gyzen ifa_free(&ia->ia_ifa);
13852d9db0bcSEric van Gyzen } else {
13862d9db0bcSEric van Gyzen int rescheduled;
13872d9db0bcSEric van Gyzen IF_ADDR_WLOCK(ia->ia_ifa.ifa_ifp);
13882d9db0bcSEric van Gyzen rescheduled = callout_reset(&ia->ia_garp_timer,
13892d9db0bcSEric van Gyzen (1 << ia->ia_garp_count) * hz,
13902d9db0bcSEric van Gyzen garp_rexmit, ia);
13912d9db0bcSEric van Gyzen IF_ADDR_WUNLOCK(ia->ia_ifa.ifa_ifp);
13922d9db0bcSEric van Gyzen if (rescheduled) {
13932d9db0bcSEric van Gyzen ifa_free(&ia->ia_ifa);
13942d9db0bcSEric van Gyzen }
13952d9db0bcSEric van Gyzen }
13963838c6a3SKristof Provost
13973838c6a3SKristof Provost CURVNET_RESTORE();
1398*38fdcca0SKristof Provost NET_EPOCH_EXIT(et);
13992d9db0bcSEric van Gyzen }
14002d9db0bcSEric van Gyzen
14012d9db0bcSEric van Gyzen /*
14022d9db0bcSEric van Gyzen * Start the GARP retransmit timer.
14032d9db0bcSEric van Gyzen *
14042d9db0bcSEric van Gyzen * A single GARP is always transmitted when an IPv4 address is added
14052d9db0bcSEric van Gyzen * to an interface and that is usually sufficient. However, in some
14062d9db0bcSEric van Gyzen * circumstances, such as when a shared address is passed between
14072d9db0bcSEric van Gyzen * cluster nodes, this single GARP may occasionally be dropped or
14082d9db0bcSEric van Gyzen * lost. This can lead to neighbors on the network link working with a
14092d9db0bcSEric van Gyzen * stale ARP cache and sending packets destined for that address to
14102d9db0bcSEric van Gyzen * the node that previously owned the address, which may not respond.
14112d9db0bcSEric van Gyzen *
14122d9db0bcSEric van Gyzen * To avoid this situation, GARP retransmits can be enabled by setting
14132d9db0bcSEric van Gyzen * the net.link.ether.inet.garp_rexmit_count sysctl to a value greater
14142d9db0bcSEric van Gyzen * than zero. The setting represents the maximum number of
14152d9db0bcSEric van Gyzen * retransmissions. The interval between retransmissions is calculated
14162d9db0bcSEric van Gyzen * using an exponential backoff algorithm, doubling each time, so the
14172d9db0bcSEric van Gyzen * retransmission intervals are: {1, 2, 4, 8, 16, ...} (seconds).
14182d9db0bcSEric van Gyzen */
14192d9db0bcSEric van Gyzen static void
garp_timer_start(struct ifaddr * ifa)14202d9db0bcSEric van Gyzen garp_timer_start(struct ifaddr *ifa)
14212d9db0bcSEric van Gyzen {
14222d9db0bcSEric van Gyzen struct in_ifaddr *ia = (struct in_ifaddr *) ifa;
14232d9db0bcSEric van Gyzen
14242d9db0bcSEric van Gyzen IF_ADDR_WLOCK(ia->ia_ifa.ifa_ifp);
14252d9db0bcSEric van Gyzen ia->ia_garp_count = 0;
14262d9db0bcSEric van Gyzen if (callout_reset(&ia->ia_garp_timer, (1 << ia->ia_garp_count) * hz,
14272d9db0bcSEric van Gyzen garp_rexmit, ia) == 0) {
14282d9db0bcSEric van Gyzen ifa_ref(ifa);
14292d9db0bcSEric van Gyzen }
14302d9db0bcSEric van Gyzen IF_ADDR_WUNLOCK(ia->ia_ifa.ifa_ifp);
14312d9db0bcSEric van Gyzen }
14322d9db0bcSEric van Gyzen
1433a9771948SGleb Smirnoff void
arp_ifinit(struct ifnet * ifp,struct ifaddr * ifa)14341c302b58SAlexander V. Chernikov arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1435a9771948SGleb Smirnoff {
1436b8a6e03fSGleb Smirnoff struct epoch_tracker et;
14371c302b58SAlexander V. Chernikov const struct sockaddr_in *dst_in;
14381c302b58SAlexander V. Chernikov const struct sockaddr *dst;
14391c302b58SAlexander V. Chernikov
14401c302b58SAlexander V. Chernikov if (ifa->ifa_carp != NULL)
14411c302b58SAlexander V. Chernikov return;
14421c302b58SAlexander V. Chernikov
14431c302b58SAlexander V. Chernikov dst = ifa->ifa_addr;
14441c302b58SAlexander V. Chernikov dst_in = (const struct sockaddr_in *)dst;
14451c302b58SAlexander V. Chernikov
14461c302b58SAlexander V. Chernikov if (ntohl(dst_in->sin_addr.s_addr) == INADDR_ANY)
14471c302b58SAlexander V. Chernikov return;
1448b8a6e03fSGleb Smirnoff NET_EPOCH_ENTER(et);
1449d6e82913SSteven Hartland arp_announce_ifaddr(ifp, dst_in->sin_addr, IF_LLADDR(ifp));
1450b8a6e03fSGleb Smirnoff NET_EPOCH_EXIT(et);
1451013784c9SKristof Provost if (V_garp_rexmit_count > 0) {
14522d9db0bcSEric van Gyzen garp_timer_start(ifa);
14532d9db0bcSEric van Gyzen }
14541c302b58SAlexander V. Chernikov
14551c302b58SAlexander V. Chernikov arp_add_ifa_lle(ifp, dst);
14561c302b58SAlexander V. Chernikov }
14571c302b58SAlexander V. Chernikov
1458d6e82913SSteven Hartland void
arp_announce_ifaddr(struct ifnet * ifp,struct in_addr addr,u_char * enaddr)1459d6e82913SSteven Hartland arp_announce_ifaddr(struct ifnet *ifp, struct in_addr addr, u_char *enaddr)
14601c302b58SAlexander V. Chernikov {
14611c302b58SAlexander V. Chernikov
1462d6e82913SSteven Hartland if (ntohl(addr.s_addr) != INADDR_ANY)
1463d6e82913SSteven Hartland arprequest(ifp, &addr, &addr, enaddr);
1464a9771948SGleb Smirnoff }
1465a9771948SGleb Smirnoff
1466b13c5b5dSAlexander V. Chernikov /*
1467d6e82913SSteven Hartland * Sends gratuitous ARPs for each ifaddr to notify other
1468d6e82913SSteven Hartland * nodes about the address change.
1469b13c5b5dSAlexander V. Chernikov */
1470d6e82913SSteven Hartland static __noinline void
arp_handle_ifllchange(struct ifnet * ifp)1471d6e82913SSteven Hartland arp_handle_ifllchange(struct ifnet *ifp)
1472b13c5b5dSAlexander V. Chernikov {
1473b13c5b5dSAlexander V. Chernikov struct ifaddr *ifa;
1474b13c5b5dSAlexander V. Chernikov
1475d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1476d6e82913SSteven Hartland if (ifa->ifa_addr->sa_family == AF_INET)
1477d6e82913SSteven Hartland arp_ifinit(ifp, ifa);
147852e53e2dSSteven Hartland }
1479b13c5b5dSAlexander V. Chernikov }
1480b13c5b5dSAlexander V. Chernikov
1481b13c5b5dSAlexander V. Chernikov /*
1482d6e82913SSteven Hartland * A handler for interface link layer address change event.
1483b13c5b5dSAlexander V. Chernikov */
14844fb3a820SAlexander V. Chernikov static void
arp_iflladdr(void * arg __unused,struct ifnet * ifp)1485b13c5b5dSAlexander V. Chernikov arp_iflladdr(void *arg __unused, struct ifnet *ifp)
1486b13c5b5dSAlexander V. Chernikov {
1487c139b3c1SKristof Provost /* if_bridge can update its lladdr during if_vmove(), after we've done
1488c139b3c1SKristof Provost * if_detach_internal()/dom_ifdetach(). */
1489c139b3c1SKristof Provost if (ifp->if_afdata[AF_INET] == NULL)
1490c139b3c1SKristof Provost return;
1491b13c5b5dSAlexander V. Chernikov
14924fb3a820SAlexander V. Chernikov lltable_update_ifaddr(LLTABLE(ifp));
14934fb3a820SAlexander V. Chernikov
1494d6e82913SSteven Hartland if ((ifp->if_flags & IFF_UP) != 0)
1495d6e82913SSteven Hartland arp_handle_ifllchange(ifp);
1496b13c5b5dSAlexander V. Chernikov }
1497b13c5b5dSAlexander V. Chernikov
14981ed81b73SMarko Zec static void
vnet_arp_init(void)1499484149deSBjoern A. Zeeb vnet_arp_init(void)
15001ed81b73SMarko Zec {
15011ed81b73SMarko Zec
1502484149deSBjoern A. Zeeb if (IS_DEFAULT_VNET(curvnet)) {
1503d4b5cae4SRobert Watson netisr_register(&arp_nh);
1504b13c5b5dSAlexander V. Chernikov iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
1505b13c5b5dSAlexander V. Chernikov arp_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
1506df5e1987SJonathan Lemon }
1507484149deSBjoern A. Zeeb #ifdef VIMAGE
1508484149deSBjoern A. Zeeb else
1509484149deSBjoern A. Zeeb netisr_register_vnet(&arp_nh);
1510484149deSBjoern A. Zeeb #endif
1511484149deSBjoern A. Zeeb }
1512484149deSBjoern A. Zeeb VNET_SYSINIT(vnet_arp_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_SECOND,
1513ac51711cSZhenlei Huang vnet_arp_init, NULL);
1514484149deSBjoern A. Zeeb
1515484149deSBjoern A. Zeeb #ifdef VIMAGE
1516484149deSBjoern A. Zeeb /*
1517484149deSBjoern A. Zeeb * We have to unregister ARP along with IP otherwise we risk doing INADDR_HASH
1518484149deSBjoern A. Zeeb * lookups after destroying the hash. Ideally this would go on SI_ORDER_3.5.
1519484149deSBjoern A. Zeeb */
1520484149deSBjoern A. Zeeb static void
vnet_arp_destroy(__unused void * arg)1521484149deSBjoern A. Zeeb vnet_arp_destroy(__unused void *arg)
1522484149deSBjoern A. Zeeb {
1523484149deSBjoern A. Zeeb
1524484149deSBjoern A. Zeeb netisr_unregister_vnet(&arp_nh);
1525484149deSBjoern A. Zeeb }
1526484149deSBjoern A. Zeeb VNET_SYSUNINIT(vnet_arp_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
1527484149deSBjoern A. Zeeb vnet_arp_destroy, NULL);
1528484149deSBjoern A. Zeeb #endif
1529