2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1984, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Geoffrey M. Rehmet, All rights reserved.
9 * This code is derived from software which forms part of the 4.4-Lite
10 * Berkeley software distribution, which was in derived from software
11 * contributed to Berkeley by Sun Microsystems, Inc.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * from arp.c 8.2 (Berkeley) 1/2/94
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
47 * Verify that we are at least 4.4 BSD
52 #include <sys/socket.h>
53 #include <sys/filio.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/if_ether.h>
64 #include <arpa/inet.h>
76 static int rtmsg(int);
78 static int s = -1; /* routing socket */
82 * Open the routing socket
84 static void getsocket () {
86 s = socket(PF_ROUTE, SOCK_RAW, 0);
88 report(LOG_ERR, "socket %s", strerror(errno));
93 * Drain the socket of any unwanted routing messages.
98 ioctl(s, FIONREAD, &n);
100 read(s, buf, sizeof buf);
101 ioctl(s, FIONREAD, &n);
106 static struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
107 static struct sockaddr_in blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
108 static struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
109 static int expire_time, flags, doing_proxy;
111 struct rt_msghdr m_rtm;
116 * Set an individual arp entry
118 int bsd_arp_set(ia, eaddr, len)
123 struct sockaddr_in *sin = &sin_m;
124 struct sockaddr_dl *sdl;
125 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
135 ea = (u_char *)LLADDR(&sdl_m);
136 bcopy(eaddr, ea, len);
137 sdl_m.sdl_alen = len;
138 doing_proxy = flags = expire_time = 0;
140 /* make arp entry temporary */
141 clock_gettime(CLOCK_MONOTONIC, &tp);
142 expire_time = tp.tv_sec + 20 * 60;
145 if (rtmsg(RTM_GET) < 0) {
146 report(LOG_WARNING, "rtmget: %s", strerror(errno));
149 sin = (struct sockaddr_in *)(rtm + 1);
150 sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
151 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
152 if (sdl->sdl_family == AF_LINK &&
153 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
154 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
155 case IFT_ISO88024: case IFT_ISO88025:
159 if (doing_proxy == 0) {
160 report(LOG_WARNING, "set: can only proxy for %s\n",
161 inet_ntoa(sin->sin_addr));
167 if (sdl->sdl_family != AF_LINK) {
169 "cannot intuit interface index and type for %s\n",
170 inet_ntoa(sin->sin_addr));
173 sdl_m.sdl_type = sdl->sdl_type;
174 sdl_m.sdl_index = sdl->sdl_index;
179 static int rtmsg(cmd)
184 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
185 char *cp = m_rtmsg.m_space;
189 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
190 rtm->rtm_flags = flags;
191 rtm->rtm_version = RTM_VERSION;
195 report(LOG_ERR, "set_arp: internal wrong cmd - exiting");
199 rtm->rtm_addrs |= RTA_GATEWAY;
200 rtm->rtm_rmx.rmx_expire = expire_time;
201 rtm->rtm_inits = RTV_EXPIRE;
202 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
204 rtm->rtm_addrs |= RTA_NETMASK;
205 rtm->rtm_flags &= ~RTF_HOST;
209 rtm->rtm_addrs |= RTA_DST;
211 #define NEXTADDR(w, s) \
212 if (rtm->rtm_addrs & (w)) { \
213 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
215 NEXTADDR(RTA_DST, sin_m);
216 NEXTADDR(RTA_GATEWAY, sdl_m);
217 NEXTADDR(RTA_NETMASK, so_mask);
219 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
222 rtm->rtm_seq = ++seq;
224 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
225 if ((errno != ESRCH) && !(errno == EEXIST && cmd == RTM_ADD)){
226 report(LOG_WARNING, "writing to routing socket: %s",
232 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
233 } while (l > 0 && (rtm->rtm_type != cmd || rtm->rtm_seq != seq || rtm->rtm_pid != getpid()));
235 report(LOG_WARNING, "arp: read from routing socket: %s\n",
241 #endif /* BSD >= 199306 */