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