2 * Copyright (c) 1984, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Geoffrey M. Rehmet, All rights reserved.
7 * This code is derived from software which forms part of the 4.4-Lite
8 * Berkeley software distribution, which was in derived from software
9 * contributed to Berkeley by Sun Microsystems, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from arp.c 8.2 (Berkeley) 1/2/94
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
45 * Verify that we are at least 4.4 BSD
50 #include <sys/socket.h>
51 #include <sys/filio.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/if_ether.h>
62 #include <arpa/inet.h>
74 static int rtmsg(int);
76 static int s = -1; /* routing socket */
80 * Open the routing socket
82 static void getsocket () {
84 s = socket(PF_ROUTE, SOCK_RAW, 0);
86 report(LOG_ERR, "socket %s", strerror(errno));
91 * Drain the socket of any unwanted routing messages.
96 ioctl(s, FIONREAD, &n);
98 read(s, buf, sizeof buf);
99 ioctl(s, FIONREAD, &n);
104 static struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
105 static struct sockaddr_in blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
106 static struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
107 static int expire_time, flags, doing_proxy;
109 struct rt_msghdr m_rtm;
114 * Set an individual arp entry
116 int bsd_arp_set(ia, eaddr, len)
121 register struct sockaddr_in *sin = &sin_m;
122 register struct sockaddr_dl *sdl;
123 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
133 ea = (u_char *)LLADDR(&sdl_m);
134 bcopy(eaddr, ea, len);
135 sdl_m.sdl_alen = len;
136 doing_proxy = flags = expire_time = 0;
138 /* make arp entry temporary */
139 clock_gettime(CLOCK_MONOTONIC, &tp);
140 expire_time = tp.tv_sec + 20 * 60;
143 if (rtmsg(RTM_GET) < 0) {
144 report(LOG_WARNING, "rtmget: %s", strerror(errno));
147 sin = (struct sockaddr_in *)(rtm + 1);
148 sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
149 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
150 if (sdl->sdl_family == AF_LINK &&
151 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
152 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
153 case IFT_ISO88024: case IFT_ISO88025:
157 if (doing_proxy == 0) {
158 report(LOG_WARNING, "set: can only proxy for %s\n",
159 inet_ntoa(sin->sin_addr));
165 if (sdl->sdl_family != AF_LINK) {
167 "cannot intuit interface index and type for %s\n",
168 inet_ntoa(sin->sin_addr));
171 sdl_m.sdl_type = sdl->sdl_type;
172 sdl_m.sdl_index = sdl->sdl_index;
177 static int rtmsg(cmd)
182 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
183 register char *cp = m_rtmsg.m_space;
187 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
188 rtm->rtm_flags = flags;
189 rtm->rtm_version = RTM_VERSION;
193 report(LOG_ERR, "set_arp: internal wrong cmd - exiting");
197 rtm->rtm_addrs |= RTA_GATEWAY;
198 rtm->rtm_rmx.rmx_expire = expire_time;
199 rtm->rtm_inits = RTV_EXPIRE;
200 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
202 rtm->rtm_addrs |= RTA_NETMASK;
203 rtm->rtm_flags &= ~RTF_HOST;
207 rtm->rtm_addrs |= RTA_DST;
209 #define NEXTADDR(w, s) \
210 if (rtm->rtm_addrs & (w)) { \
211 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
213 NEXTADDR(RTA_DST, sin_m);
214 NEXTADDR(RTA_GATEWAY, sdl_m);
215 NEXTADDR(RTA_NETMASK, so_mask);
217 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
220 rtm->rtm_seq = ++seq;
222 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
223 if ((errno != ESRCH) && !(errno == EEXIST && cmd == RTM_ADD)){
224 report(LOG_WARNING, "writing to routing socket: %s",
230 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
231 } while (l > 0 && (rtm->rtm_type != cmd || rtm->rtm_seq != seq || rtm->rtm_pid != getpid()));
233 report(LOG_WARNING, "arp: read from routing socket: %s\n",
239 #endif /* BSD >= 199306 */