1*fcede00eSroy /* $NetBSD: mkarp.c,v 1.12 2017/04/12 16:57:14 roy Exp $ */
2d4fc5fabSis
3d4fc5fabSis /*
4d4fc5fabSis * Copyright (c) 1984, 1993
5d4fc5fabSis * The Regents of the University of California. All rights reserved.
6d4fc5fabSis *
7d4fc5fabSis * This code is derived from software contributed to Berkeley by
8d4fc5fabSis * Sun Microsystems, Inc.
9d4fc5fabSis *
10d4fc5fabSis * Redistribution and use in source and binary forms, with or without
11d4fc5fabSis * modification, are permitted provided that the following conditions
12d4fc5fabSis * are met:
13d4fc5fabSis * 1. Redistributions of source code must retain the above copyright
14d4fc5fabSis * notice, this list of conditions and the following disclaimer.
15d4fc5fabSis * 2. Redistributions in binary form must reproduce the above copyright
16d4fc5fabSis * notice, this list of conditions and the following disclaimer in the
17d4fc5fabSis * documentation and/or other materials provided with the distribution.
18326b2259Sagc * 3. Neither the name of the University nor the names of its contributors
19d4fc5fabSis * may be used to endorse or promote products derived from this software
20d4fc5fabSis * without specific prior written permission.
21d4fc5fabSis *
22d4fc5fabSis * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23d4fc5fabSis * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24d4fc5fabSis * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25d4fc5fabSis * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26d4fc5fabSis * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27d4fc5fabSis * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28d4fc5fabSis * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29d4fc5fabSis * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30d4fc5fabSis * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31d4fc5fabSis * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32d4fc5fabSis * SUCH DAMAGE.
33d4fc5fabSis */
34d4fc5fabSis
35d4fc5fabSis #include <sys/cdefs.h>
36d4fc5fabSis #ifndef lint
379c194566Slukem __COPYRIGHT("@(#) Copyright (c) 1984, 1993\
389c194566Slukem The Regents of the University of California. All rights reserved.");
39d4fc5fabSis #endif /* not lint */
40d4fc5fabSis
41d4fc5fabSis #ifndef lint
42d4fc5fabSis #if 0
43d4fc5fabSis static char sccsid[] = "@(#)arp.c 8.3 (Berkeley) 4/28/95";
44d4fc5fabSis #else
45*fcede00eSroy __RCSID("$NetBSD: mkarp.c,v 1.12 2017/04/12 16:57:14 roy Exp $");
46d4fc5fabSis #endif
47d4fc5fabSis #endif /* not lint */
48d4fc5fabSis
49d4fc5fabSis /*
50d4fc5fabSis * mkarp - set an arp table entry
51d4fc5fabSis */
52d4fc5fabSis
53d4fc5fabSis #include <sys/param.h>
54d4fc5fabSis #include <sys/file.h>
55d4fc5fabSis #include <sys/socket.h>
56d4fc5fabSis #include <sys/sysctl.h>
57d4fc5fabSis
58d4fc5fabSis #include <net/if.h>
59d4fc5fabSis #include <net/if_dl.h>
60d4fc5fabSis #include <net/if_ether.h>
61d4fc5fabSis #include <net/if_types.h>
62d4fc5fabSis #include <net/route.h>
63d4fc5fabSis #include <netinet/in.h>
64d4fc5fabSis #include <netinet/if_inarp.h>
65d4fc5fabSis #include <arpa/inet.h>
66d4fc5fabSis
67d4fc5fabSis #include <err.h>
68d4fc5fabSis #include <errno.h>
69d4fc5fabSis #include <netdb.h>
70d4fc5fabSis #include <nlist.h>
71d4fc5fabSis #include <paths.h>
72d4fc5fabSis #include <stdio.h>
73d4fc5fabSis #include <stdlib.h>
74d4fc5fabSis #include <string.h>
75d4fc5fabSis #include <unistd.h>
76d4fc5fabSis
77d4fc5fabSis #include "mkarp.h"
78d4fc5fabSis
79cc924725Spetrov /* Roundup the same way rt_xaddrs does */
80cc924725Spetrov #define ROUNDUP(a) \
81cc924725Spetrov ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
82cc924725Spetrov
8384dd8f31Swiz int rtmsg(int, int, struct rt_msghdr *, struct sockaddr_inarp *,
8484dd8f31Swiz struct sockaddr_dl *);
8576d8a55bSjoerg static struct {
86d4fc5fabSis struct rt_msghdr m_rtm;
87d4fc5fabSis char m_space[512];
88d4fc5fabSis } m_rtmsg;
89d4fc5fabSis
9009973b35Sozaki-r static int
is_llinfo(const struct sockaddr_dl * sdl,int rtflags)9109973b35Sozaki-r is_llinfo(const struct sockaddr_dl *sdl, int rtflags)
9209973b35Sozaki-r {
9309973b35Sozaki-r if (sdl->sdl_family != AF_LINK ||
9409973b35Sozaki-r (rtflags & (RTF_LLDATA|RTF_GATEWAY)) != RTF_LLDATA)
9509973b35Sozaki-r return 0;
9609973b35Sozaki-r
9709973b35Sozaki-r switch (sdl->sdl_type) {
9809973b35Sozaki-r case IFT_ETHER:
9909973b35Sozaki-r case IFT_FDDI:
10009973b35Sozaki-r case IFT_ISO88023:
10109973b35Sozaki-r case IFT_ISO88024:
10209973b35Sozaki-r case IFT_ISO88025:
10309973b35Sozaki-r case IFT_ARCNET:
10409973b35Sozaki-r return 1;
10509973b35Sozaki-r default:
10609973b35Sozaki-r return 0;
10709973b35Sozaki-r }
10809973b35Sozaki-r }
10909973b35Sozaki-r
110d4fc5fabSis /*
111d4fc5fabSis * Set an individual arp entry
112d4fc5fabSis */
113d4fc5fabSis int
mkarp(u_char * haddr,u_int32_t ipaddr)11484dd8f31Swiz mkarp(u_char *haddr, u_int32_t ipaddr)
115d4fc5fabSis {
11609ae6325Slukem static struct sockaddr_inarp blank_sin = {
1176994331dSchristos .sin_len = sizeof(blank_sin),
1186994331dSchristos .sin_family = AF_INET,
1196994331dSchristos };
12009ae6325Slukem static struct sockaddr_dl blank_sdl = {
1216994331dSchristos .sdl_len = sizeof(blank_sdl),
1226994331dSchristos .sdl_family = AF_LINK,
1236994331dSchristos };
124d4fc5fabSis
125d4fc5fabSis struct sockaddr_inarp *sin;
126d4fc5fabSis struct sockaddr_dl *sdl;
127d4fc5fabSis struct rt_msghdr *rtm;
128d4fc5fabSis u_int8_t *p, *endp;
1297d14d66bSabs int result;
1307d14d66bSabs int s;
131d4fc5fabSis
132d4fc5fabSis struct sockaddr_inarp sin_m;
133d4fc5fabSis struct sockaddr_dl sdl_m;
134d4fc5fabSis
135*fcede00eSroy #ifdef RO_MSGFILTER
136*fcede00eSroy unsigned char msgfilter[] = { RTM_GET, RTM_ADD };
137*fcede00eSroy #endif
138*fcede00eSroy
139d4fc5fabSis sin = &sin_m;
140d4fc5fabSis rtm = &(m_rtmsg.m_rtm);
141d4fc5fabSis
142d4fc5fabSis sdl_m = blank_sdl; /* struct copy */
143d4fc5fabSis sin_m = blank_sin; /* struct copy */
144d4fc5fabSis
145d4fc5fabSis sin->sin_addr.s_addr = ipaddr;
146d4fc5fabSis
147d4fc5fabSis p = LLADDR(&sdl_m);
148d4fc5fabSis endp = ((caddr_t)&sdl_m) + sdl_m.sdl_len;
14902f88f35Schristos if (endp > (p + ETHER_ADDR_LEN))
15002f88f35Schristos endp = p + ETHER_ADDR_LEN;
151d4fc5fabSis
152d4fc5fabSis while (p < endp) {
153d4fc5fabSis *p++ = *haddr++;
154d4fc5fabSis }
15502f88f35Schristos sdl_m.sdl_alen = ETHER_ADDR_LEN;
156d4fc5fabSis
1577d14d66bSabs /*
1587d14d66bSabs * We need to close and open the socket to prevent routing updates
1597d14d66bSabs * building up such that when we send our message we never see our
1607d14d66bSabs * reply (and hang)
1617d14d66bSabs */
1627d14d66bSabs s = socket(PF_ROUTE, SOCK_RAW, 0);
1637d14d66bSabs if (s < 0)
1647d14d66bSabs err(1, "socket");
165*fcede00eSroy #ifdef RO_MSGFILTER
166*fcede00eSroy if (setsockopt(s, PF_ROUTE, RO_MSGFILTER,
167*fcede00eSroy &msgfilter, sizeof(msgfilter)) < 0)
168*fcede00eSroy warn("RO_MSGFILTER");
169*fcede00eSroy #endif
1707d14d66bSabs
171d4fc5fabSis rtm->rtm_flags = 0;
172d4fc5fabSis
1737d14d66bSabs if (rtmsg(RTM_GET, s, rtm, &sin_m, &sdl_m) < 0) {
174d4fc5fabSis #if 0
175d4fc5fabSis warn("%s", host);
176d4fc5fabSis #endif
1777d14d66bSabs close(s);
178d4fc5fabSis return (1);
179d4fc5fabSis }
180d4fc5fabSis sin = (struct sockaddr_inarp *)(rtm + 1);
181d4fc5fabSis sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
182d4fc5fabSis if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
18309973b35Sozaki-r if (is_llinfo(sdl, rtm->rtm_flags))
184d4fc5fabSis goto overwrite;
185d4fc5fabSis #if 0
186d4fc5fabSis (void)printf("set: can only proxy for %s\n", host);
187d4fc5fabSis #endif
1887d14d66bSabs close(s);
189d4fc5fabSis return (1);
190d4fc5fabSis }
191d4fc5fabSis overwrite:
192d4fc5fabSis if (sdl->sdl_family != AF_LINK) {
193d4fc5fabSis #if 0
194d4fc5fabSis (void)printf("cannot intuit interface index and type for %s\n",
195d4fc5fabSis host);
196d4fc5fabSis #endif
1977d14d66bSabs close(s);
198d4fc5fabSis return (1);
199d4fc5fabSis }
200d4fc5fabSis sdl_m.sdl_type = sdl->sdl_type;
201d4fc5fabSis sdl_m.sdl_index = sdl->sdl_index;
2027d14d66bSabs result = rtmsg(RTM_ADD, s, rtm, &sin_m, &sdl_m);
2037d14d66bSabs close(s);
2047d14d66bSabs return (result);
205d4fc5fabSis }
206d4fc5fabSis
207d4fc5fabSis int
rtmsg(int cmd,int s,struct rt_msghdr * rtm,struct sockaddr_inarp * sin_m,struct sockaddr_dl * sdl_m)20884dd8f31Swiz rtmsg(int cmd, int s, struct rt_msghdr *rtm, struct sockaddr_inarp *sin_m,
20984dd8f31Swiz struct sockaddr_dl *sdl_m)
210d4fc5fabSis {
211d4fc5fabSis static int seq;
212d4fc5fabSis int rlen;
213d4fc5fabSis char *cp;
214d4fc5fabSis int l;
215d4fc5fabSis pid_t pid;
21609ae6325Slukem struct timeval tv;
217d4fc5fabSis
218d4fc5fabSis rtm = &m_rtmsg.m_rtm;
219d4fc5fabSis cp = m_rtmsg.m_space;
220d4fc5fabSis errno = 0;
221d4fc5fabSis
222d4fc5fabSis pid = getpid();
223d4fc5fabSis
224d4fc5fabSis (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
225d4fc5fabSis rtm->rtm_version = RTM_VERSION;
226d4fc5fabSis
227d4fc5fabSis switch (cmd) {
228d4fc5fabSis default:
229d4fc5fabSis errx(1, "internal wrong cmd");
230d4fc5fabSis /*NOTREACHED*/
231d4fc5fabSis case RTM_ADD:
232d4fc5fabSis rtm->rtm_addrs |= RTA_GATEWAY;
23309ae6325Slukem (void)gettimeofday(&tv, 0);
23409ae6325Slukem rtm->rtm_rmx.rmx_expire = tv.tv_sec + 20 * 60;
235d4fc5fabSis rtm->rtm_inits = RTV_EXPIRE;
23609973b35Sozaki-r rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
237d4fc5fabSis sin_m->sin_other = 0;
238d4fc5fabSis
239d4fc5fabSis /* FALLTHROUGH */
240d4fc5fabSis case RTM_GET:
241d4fc5fabSis rtm->rtm_addrs |= RTA_DST;
242d4fc5fabSis }
243d4fc5fabSis #define NEXTADDR(w, s) \
244d4fc5fabSis if (rtm->rtm_addrs & (w)) { \
245d4fc5fabSis (void)memcpy(cp, s, ((struct sockaddr *)s)->sa_len); \
246cc924725Spetrov cp += ROUNDUP(((struct sockaddr *)s)->sa_len);}
247d4fc5fabSis
248d4fc5fabSis NEXTADDR(RTA_DST, sin_m);
249d4fc5fabSis NEXTADDR(RTA_GATEWAY, sdl_m);
250d4fc5fabSis
251d4fc5fabSis rtm->rtm_msglen = cp - (char *)&m_rtmsg;
252d4fc5fabSis
253d4fc5fabSis l = rtm->rtm_msglen;
254d4fc5fabSis rtm->rtm_seq = ++seq;
255d4fc5fabSis rtm->rtm_type = cmd;
256d4fc5fabSis if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
257d4fc5fabSis if (errno != ESRCH && errno != EEXIST) {
258d4fc5fabSis warn("writing to routing socket");
259d4fc5fabSis return (-1);
260d4fc5fabSis }
261d4fc5fabSis }
262d4fc5fabSis do {
263d4fc5fabSis l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
264d4fc5fabSis } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
265d4fc5fabSis if (l < 0)
266d4fc5fabSis warn("read from routing socket");
267d4fc5fabSis return (0);
268d4fc5fabSis }
269