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