xref: /openbsd/usr.sbin/rarpd/arptab.c (revision df69c215)
1 /*	$OpenBSD: arptab.c,v 1.31 2019/06/28 13:32:50 deraadt 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 /*
36  * set arp table entries
37  */
38 
39 
40 #include <sys/socket.h>
41 #include <sys/time.h>
42 
43 #include <net/if.h>
44 #include <net/if_dl.h>
45 #include <net/if_types.h>
46 #include <net/route.h>
47 
48 #include <netinet/in.h>
49 #include <netinet/if_ether.h>
50 
51 #include <arpa/inet.h>
52 
53 #include <netdb.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <stdio.h>
57 #include <unistd.h>
58 #include <stdlib.h>
59 #include <syslog.h>
60 #include <string.h>
61 #include <err.h>
62 
63 /* ROUNDUP() is nasty, but it is identical to what's in the kernel. */
64 #define ROUNDUP(a)					\
65 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
66 
67 static pid_t pid;
68 static int s = -1;
69 
70 int rtget(struct sockaddr_inarp **, struct sockaddr_dl **);
71 
72 void
arptab_init(void)73 arptab_init(void)
74 {
75 	s = socket(AF_ROUTE, SOCK_RAW, 0);
76 	if (s == -1)
77 		err(1, "arp: socket");
78 }
79 
80 struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
81 struct	sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
82 struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
83 struct	sockaddr_dl ifp_m = {sizeof(&ifp_m), AF_LINK};
84 time_t	expire_time;
85 int	flags, export_only, doing_proxy;
86 
87 struct	{
88 	struct	rt_msghdr m_rtm;
89 	char	m_space[512];
90 } m_rtmsg;
91 
92 int	arptab_set(u_char *, u_int32_t);
93 int	rtmsg(int);
94 
95 /*
96  * Set an individual arp entry
97  */
98 int
arptab_set(u_char * eaddr,u_int32_t host)99 arptab_set(u_char *eaddr, u_int32_t host)
100 {
101 	struct sockaddr_inarp *sin = &sin_m;
102 	struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
103 	struct sockaddr_dl *sdl;
104 	struct timeval now;
105 	int rt;
106 
107 	pid = getpid();
108 
109 	sdl_m = blank_sdl;
110 	sin_m = blank_sin;
111 	sin->sin_addr.s_addr = host;
112 	memcpy((u_char *)LLADDR(&sdl_m), (char *)eaddr, 6);
113 	sdl_m.sdl_alen = 6;
114 	expire_time = 0;
115 	doing_proxy = flags = export_only = 0;
116 	gettimeofday(&now, 0);
117 	expire_time = now.tv_sec + 20 * 60;
118 
119 tryagain:
120 	if (rtget(&sin, &sdl)) {
121 		syslog(LOG_ERR,"%s: %m", inet_ntoa(sin->sin_addr));
122 		return (1);
123 	}
124 
125 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
126 		if (sdl->sdl_family == AF_LINK &&
127 		    (rtm->rtm_flags & RTF_LLINFO) &&
128 		    !(rtm->rtm_flags & RTF_GATEWAY))
129 			switch (sdl->sdl_type) {
130 			case IFT_ETHER:
131 			case IFT_FDDI:
132 			case IFT_ISO88023:
133 			case IFT_ISO88024:
134 			case IFT_ISO88025:
135 				goto overwrite;
136 			default:
137 				break;
138 		}
139 		if (doing_proxy == 0) {
140 			syslog(LOG_ERR, "arptab_set: can only proxy for %s",
141 			    inet_ntoa(sin->sin_addr));
142 			return (1);
143 		}
144 		if (sin_m.sin_other & SIN_PROXY) {
145 			syslog(LOG_ERR,
146 			    "arptab_set: proxy entry exists for non 802 device");
147 			return(1);
148 		}
149 		sin_m.sin_other = SIN_PROXY;
150 		export_only = 1;
151 		goto tryagain;
152 	}
153 overwrite:
154 	if (sdl->sdl_family != AF_LINK) {
155 		syslog(LOG_ERR,
156 		    "arptab_set: cannot intuit interface index and type for %s",
157 		    inet_ntoa(sin->sin_addr));
158 		return (1);
159 	}
160 	sdl_m.sdl_type = sdl->sdl_type;
161 	sdl_m.sdl_index = sdl->sdl_index;
162 	rt = rtmsg(RTM_ADD);
163 	return (rt);
164 }
165 
166 int
rtmsg(int cmd)167 rtmsg(int cmd)
168 {
169 	static int seq;
170 	struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
171 	char *cp = m_rtmsg.m_space;
172 	int l;
173 
174 retry:
175 	errno = 0;
176 	if (cmd == RTM_DELETE)
177 		goto doit;
178 	memset((char *)&m_rtmsg, 0, sizeof(m_rtmsg));
179 	rtm->rtm_flags = flags;
180 	rtm->rtm_version = RTM_VERSION;
181 
182 	switch (cmd) {
183 	default:
184 		syslog(LOG_ERR, "arptab_set: internal wrong cmd");
185 		exit(1);
186 	case RTM_ADD:
187 		rtm->rtm_addrs |= RTA_GATEWAY;
188 		rtm->rtm_rmx.rmx_expire = expire_time;
189 		rtm->rtm_inits = RTV_EXPIRE;
190 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
191 		sin_m.sin_other = 0;
192 		if (doing_proxy) {
193 			if (export_only)
194 				sin_m.sin_other = SIN_PROXY;
195 			else {
196 				rtm->rtm_addrs |= RTA_NETMASK;
197 				rtm->rtm_flags &= ~RTF_HOST;
198 			}
199 		}
200 		/* FALLTHROUGH */
201 	case RTM_GET:
202 		rtm->rtm_addrs |= (RTA_DST | RTA_IFP);
203 	}
204 #define NEXTADDR(w, s) \
205 	if (rtm->rtm_addrs & (w)) { \
206 		memcpy(cp, (char *)&s, sizeof(s)); \
207 		cp += sizeof(s); \
208 	}
209 
210 	NEXTADDR(RTA_DST, sin_m);
211 	NEXTADDR(RTA_GATEWAY, sdl_m);
212 	NEXTADDR(RTA_NETMASK, so_mask);
213 	NEXTADDR(RTA_IFP, ifp_m);
214 
215 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
216 doit:
217 	l = rtm->rtm_msglen;
218 	rtm->rtm_seq = ++seq;
219 	rtm->rtm_type = cmd;
220 	if (write(s, (char *)&m_rtmsg, l) == -1) {
221 		if (errno != ESRCH && errno != EEXIST) {
222 			syslog(LOG_ERR, "writing to routing socket: %m");
223 			return (-1);
224 		}
225 	}
226 	do {
227 		l = recv(s, (char *)&m_rtmsg, sizeof(m_rtmsg), MSG_DONTWAIT);
228 	} while (l > 0 && (rtm->rtm_version != RTM_VERSION ||
229 	    rtm->rtm_seq != seq || rtm->rtm_pid != pid));
230 	if (l == -1) {
231 		if (errno == EAGAIN || errno == EINTR)
232 			goto retry;
233 		syslog(LOG_ERR, "arptab_set: read from routing socket: %m");
234 	}
235 	return (0);
236 }
237 
238 int
rtget(struct sockaddr_inarp ** sinp,struct sockaddr_dl ** sdlp)239 rtget(struct sockaddr_inarp **sinp, struct sockaddr_dl **sdlp)
240 {
241 	struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
242 	struct sockaddr_inarp *sin = NULL;
243 	struct sockaddr_dl *sdl = NULL;
244 	struct sockaddr *sa;
245 	char *cp;
246 	unsigned int i;
247 
248 	if (rtmsg(RTM_GET) < 0)
249 		return (1);
250 
251 	if (rtm->rtm_addrs) {
252 		cp = ((char *)rtm + rtm->rtm_hdrlen);
253 		for (i = 1; i; i <<= 1) {
254 			if (i & rtm->rtm_addrs) {
255 				sa = (struct sockaddr *)cp;
256 				switch (i) {
257 				case RTA_DST:
258 					sin = (struct sockaddr_inarp *)sa;
259 					break;
260 				case RTA_IFP:
261 					sdl = (struct sockaddr_dl *)sa;
262 					break;
263 				default:
264 					break;
265 				}
266 				cp += ROUNDUP(sa->sa_len);
267 			}
268 		}
269 	}
270 
271 	if (sin == NULL || sdl == NULL)
272 		return (1);
273 
274 	*sinp = sin;
275 	*sdlp = sdl;
276 
277 	return (0);
278 }
279