xref: /openbsd-src/usr.sbin/rarpd/arptab.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: arptab.c,v 1.22 2013/11/12 19:49:42 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/param.h>
41 #include <sys/file.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
44 
45 #include <net/if.h>
46 #include <net/if_dl.h>
47 #include <net/if_types.h>
48 #include <net/route.h>
49 
50 #include <netinet/in.h>
51 #include <netinet/if_ether.h>
52 
53 #include <arpa/inet.h>
54 
55 #include <netdb.h>
56 #include <errno.h>
57 #include <stdio.h>
58 #include <unistd.h>
59 #include <stdlib.h>
60 #include <paths.h>
61 #include <syslog.h>
62 #include <string.h>
63 #include <err.h>
64 
65 static pid_t pid;
66 static int s = -1;
67 
68 static void
69 getsocket(void)
70 {
71 	s = socket(PF_ROUTE, SOCK_RAW, 0);
72 	if (s < 0)
73 		err(1, "arp: socket");
74 }
75 
76 struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
77 struct	sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
78 struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
79 time_t	expire_time;
80 int	flags, export_only, doing_proxy;
81 
82 struct	{
83 	struct	rt_msghdr m_rtm;
84 	char	m_space[512];
85 } m_rtmsg;
86 
87 int	arptab_set(u_char *, u_int32_t);
88 int	rtmsg(int);
89 
90 /*
91  * Set an individual arp entry
92  */
93 int
94 arptab_set(u_char *eaddr, u_int32_t host)
95 {
96 	struct sockaddr_inarp *sin = &sin_m;
97 	struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
98 	struct sockaddr_dl *sdl;
99 	struct timeval now;
100 	int rt;
101 
102 	getsocket();
103 	pid = getpid();
104 
105 	sdl_m = blank_sdl;
106 	sin_m = blank_sin;
107 	sin->sin_addr.s_addr = host;
108 	memcpy((u_char *)LLADDR(&sdl_m), (char *)eaddr, 6);
109 	sdl_m.sdl_alen = 6;
110 	expire_time = 0;
111 	doing_proxy = flags = export_only = 0;
112 	gettimeofday(&now, 0);
113 	expire_time = now.tv_sec + 20 * 60;
114 
115 tryagain:
116 	if (rtmsg(RTM_GET) < 0) {
117 		syslog(LOG_ERR,"%s: %m", inet_ntoa(sin->sin_addr));
118 		close(s);
119 		s = -1;
120 		return (1);
121 	}
122 	sin = (struct sockaddr_inarp *)((char *)rtm + rtm->rtm_hdrlen);
123 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
124 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
125 		if (sdl->sdl_family == AF_LINK &&
126 		    (rtm->rtm_flags & RTF_LLINFO) &&
127 		    !(rtm->rtm_flags & RTF_GATEWAY))
128 			switch (sdl->sdl_type) {
129 			case IFT_ETHER:
130 			case IFT_FDDI:
131 			case IFT_ISO88023:
132 			case IFT_ISO88024:
133 			case IFT_ISO88025:
134 				goto overwrite;
135 			default:
136 				break;
137 		}
138 		if (doing_proxy == 0) {
139 			syslog(LOG_ERR, "arptab_set: can only proxy for %s",
140 			    inet_ntoa(sin->sin_addr));
141 			close(s);
142 			s = -1;
143 			return (1);
144 		}
145 		if (sin_m.sin_other & SIN_PROXY) {
146 			syslog(LOG_ERR,
147 			    "arptab_set: proxy entry exists for non 802 device");
148 			close(s);
149 			s = -1;
150 			return(1);
151 		}
152 		sin_m.sin_other = SIN_PROXY;
153 		export_only = 1;
154 		goto tryagain;
155 	}
156 overwrite:
157 	if (sdl->sdl_family != AF_LINK) {
158 		syslog(LOG_ERR,
159 		    "arptab_set: cannot intuit interface index and type for %s",
160 		    inet_ntoa(sin->sin_addr));
161 		close(s);
162 		s = -1;
163 		return (1);
164 	}
165 	sdl_m.sdl_type = sdl->sdl_type;
166 	sdl_m.sdl_index = sdl->sdl_index;
167 	rt = rtmsg(RTM_ADD);
168 	close(s);
169 	s = -1;
170 	return (rt);
171 }
172 
173 int
174 rtmsg(int cmd)
175 {
176 	static int seq;
177 	struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
178 	char *cp = m_rtmsg.m_space;
179 	int l;
180 
181 	errno = 0;
182 	if (cmd == RTM_DELETE)
183 		goto doit;
184 	memset((char *)&m_rtmsg, 0, sizeof(m_rtmsg));
185 	rtm->rtm_flags = flags;
186 	rtm->rtm_version = RTM_VERSION;
187 
188 	switch (cmd) {
189 	default:
190 		syslog(LOG_ERR, "arptab_set: internal wrong cmd");
191 		exit(1);
192 	case RTM_ADD:
193 		rtm->rtm_addrs |= RTA_GATEWAY;
194 		rtm->rtm_rmx.rmx_expire = expire_time;
195 		rtm->rtm_inits = RTV_EXPIRE;
196 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
197 		sin_m.sin_other = 0;
198 		if (doing_proxy) {
199 			if (export_only)
200 				sin_m.sin_other = SIN_PROXY;
201 			else {
202 				rtm->rtm_addrs |= RTA_NETMASK;
203 				rtm->rtm_flags &= ~RTF_HOST;
204 			}
205 		}
206 		/* FALLTHROUGH */
207 	case RTM_GET:
208 		rtm->rtm_addrs |= RTA_DST;
209 	}
210 #define NEXTADDR(w, s) \
211 	if (rtm->rtm_addrs & (w)) { \
212 		memcpy(cp, (char *)&s, sizeof(s)); \
213 		cp += sizeof(s); \
214 	}
215 
216 	NEXTADDR(RTA_DST, sin_m);
217 	NEXTADDR(RTA_GATEWAY, sdl_m);
218 	NEXTADDR(RTA_NETMASK, so_mask);
219 
220 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
221 doit:
222 	l = rtm->rtm_msglen;
223 	rtm->rtm_seq = ++seq;
224 	rtm->rtm_type = cmd;
225 	if (write(s, (char *)&m_rtmsg, l) < 0) {
226 		if (errno != ESRCH && errno != EEXIST) {
227 			syslog(LOG_ERR, "writing to routing socket: %m");
228 			return (-1);
229 		}
230 	}
231 	do {
232 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
233 	} while (l > 0 && (rtm->rtm_version != RTM_VERSION ||
234 	    rtm->rtm_seq != seq || rtm->rtm_pid != pid));
235 	if (l < 0)
236 		syslog(LOG_ERR, "arptab_set: read from routing socket: %m");
237 	return (0);
238 }
239