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