mkarp.c revision 1.1 1 1.1 is /* $NetBSD: mkarp.c,v 1.1 1997/11/24 18:43:14 is 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.1 is * 3. All advertising materials mentioning features or use of this software
19 1.1 is * must display the following acknowledgement:
20 1.1 is * This product includes software developed by the University of
21 1.1 is * California, Berkeley and its contributors.
22 1.1 is * 4. Neither the name of the University nor the names of its contributors
23 1.1 is * may be used to endorse or promote products derived from this software
24 1.1 is * without specific prior written permission.
25 1.1 is *
26 1.1 is * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 is * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 is * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 is * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 is * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 is * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 is * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 is * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 is * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 is * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 is * SUCH DAMAGE.
37 1.1 is */
38 1.1 is
39 1.1 is #include <sys/cdefs.h>
40 1.1 is #ifndef lint
41 1.1 is __COPYRIGHT("@(#) Copyright (c) 1984, 1993\n\
42 1.1 is The Regents of the University of California. All rights reserved.\n");
43 1.1 is #endif /* not lint */
44 1.1 is
45 1.1 is #ifndef lint
46 1.1 is #if 0
47 1.1 is static char sccsid[] = "@(#)arp.c 8.3 (Berkeley) 4/28/95";
48 1.1 is #else
49 1.1 is __RCSID("$NetBSD: mkarp.c,v 1.1 1997/11/24 18:43:14 is Exp $");
50 1.1 is #endif
51 1.1 is #endif /* not lint */
52 1.1 is
53 1.1 is /*
54 1.1 is * mkarp - set an arp table entry
55 1.1 is */
56 1.1 is
57 1.1 is #include <sys/param.h>
58 1.1 is #include <sys/file.h>
59 1.1 is #include <sys/socket.h>
60 1.1 is #include <sys/sysctl.h>
61 1.1 is
62 1.1 is #include <net/if.h>
63 1.1 is #include <net/if_dl.h>
64 1.1 is #include <net/if_ether.h>
65 1.1 is #include <net/if_types.h>
66 1.1 is #include <net/route.h>
67 1.1 is #include <netinet/in.h>
68 1.1 is #include <netinet/if_inarp.h>
69 1.1 is #include <arpa/inet.h>
70 1.1 is
71 1.1 is #include <err.h>
72 1.1 is #include <errno.h>
73 1.1 is #include <netdb.h>
74 1.1 is #include <nlist.h>
75 1.1 is #include <paths.h>
76 1.1 is #include <stdio.h>
77 1.1 is #include <stdlib.h>
78 1.1 is #include <string.h>
79 1.1 is #include <unistd.h>
80 1.1 is
81 1.1 is #include "mkarp.h"
82 1.1 is
83 1.1 is int rtmsg __P((int, struct rt_msghdr *, struct sockaddr_inarp *,
84 1.1 is struct sockaddr_dl *));
85 1.1 is 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.1 is /*
91 1.1 is * Set an individual arp entry
92 1.1 is */
93 1.1 is int
94 1.1 is mkarp(haddr, ipaddr)
95 1.1 is u_char *haddr;
96 1.1 is u_int32_t ipaddr;
97 1.1 is {
98 1.1 is static struct sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET };
99 1.1 is static struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK };
100 1.1 is
101 1.1 is struct sockaddr_inarp *sin;
102 1.1 is struct sockaddr_dl *sdl;
103 1.1 is struct rt_msghdr *rtm;
104 1.1 is u_int8_t *p, *endp;
105 1.1 is
106 1.1 is struct sockaddr_inarp sin_m;
107 1.1 is struct sockaddr_dl sdl_m;
108 1.1 is
109 1.1 is sin = &sin_m;
110 1.1 is rtm = &(m_rtmsg.m_rtm);
111 1.1 is
112 1.1 is sdl_m = blank_sdl; /* struct copy */
113 1.1 is sin_m = blank_sin; /* struct copy */
114 1.1 is
115 1.1 is sin->sin_addr.s_addr = ipaddr;
116 1.1 is
117 1.1 is p = LLADDR(&sdl_m);
118 1.1 is endp = ((caddr_t)&sdl_m) + sdl_m.sdl_len;
119 1.1 is if (endp > (p + 6))
120 1.1 is endp = p + 6;
121 1.1 is
122 1.1 is while (p < endp) {
123 1.1 is *p++ = *haddr++;
124 1.1 is }
125 1.1 is
126 1.1 is rtm->rtm_flags = 0;
127 1.1 is
128 1.1 is if (rtmsg(RTM_GET, rtm, &sin_m, &sdl_m) < 0) {
129 1.1 is #if 0
130 1.1 is warn("%s", host);
131 1.1 is #endif
132 1.1 is return (1);
133 1.1 is }
134 1.1 is sin = (struct sockaddr_inarp *)(rtm + 1);
135 1.1 is sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
136 1.1 is if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
137 1.1 is if (sdl->sdl_family == AF_LINK &&
138 1.1 is (rtm->rtm_flags & RTF_LLINFO) &&
139 1.1 is !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
140 1.1 is case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
141 1.1 is case IFT_ISO88024: case IFT_ISO88025: case IFT_ARCNET:
142 1.1 is goto overwrite;
143 1.1 is }
144 1.1 is #if 0
145 1.1 is (void)printf("set: can only proxy for %s\n", host);
146 1.1 is #endif
147 1.1 is return (1);
148 1.1 is }
149 1.1 is overwrite:
150 1.1 is if (sdl->sdl_family != AF_LINK) {
151 1.1 is #if 0
152 1.1 is (void)printf("cannot intuit interface index and type for %s\n",
153 1.1 is host);
154 1.1 is #endif
155 1.1 is return (1);
156 1.1 is }
157 1.1 is sdl_m.sdl_type = sdl->sdl_type;
158 1.1 is sdl_m.sdl_index = sdl->sdl_index;
159 1.1 is return (rtmsg(RTM_ADD, rtm, &sin_m, &sdl_m));
160 1.1 is }
161 1.1 is
162 1.1 is int
163 1.1 is rtmsg(cmd, rtm, sin_m, sdl_m)
164 1.1 is int cmd;
165 1.1 is struct rt_msghdr *rtm;
166 1.1 is struct sockaddr_inarp *sin_m;
167 1.1 is struct sockaddr_dl *sdl_m;
168 1.1 is {
169 1.1 is static int seq;
170 1.1 is static int s = -1;
171 1.1 is int rlen;
172 1.1 is char *cp;
173 1.1 is int l;
174 1.1 is pid_t pid;
175 1.1 is struct timeval time;
176 1.1 is
177 1.1 is rtm = &m_rtmsg.m_rtm;
178 1.1 is cp = m_rtmsg.m_space;
179 1.1 is errno = 0;
180 1.1 is
181 1.1 is if (s < 0) {
182 1.1 is s = socket(PF_ROUTE, SOCK_RAW, 0);
183 1.1 is if (s < 0)
184 1.1 is err(1, "socket");
185 1.1 is }
186 1.1 is pid = getpid();
187 1.1 is
188 1.1 is (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
189 1.1 is rtm->rtm_version = RTM_VERSION;
190 1.1 is
191 1.1 is switch (cmd) {
192 1.1 is default:
193 1.1 is errx(1, "internal wrong cmd");
194 1.1 is /*NOTREACHED*/
195 1.1 is case RTM_ADD:
196 1.1 is rtm->rtm_addrs |= RTA_GATEWAY;
197 1.1 is (void)gettimeofday(&time, 0);
198 1.1 is rtm->rtm_rmx.rmx_expire = time.tv_sec + 20 * 60;
199 1.1 is rtm->rtm_inits = RTV_EXPIRE;
200 1.1 is rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
201 1.1 is sin_m->sin_other = 0;
202 1.1 is
203 1.1 is /* FALLTHROUGH */
204 1.1 is case RTM_GET:
205 1.1 is rtm->rtm_addrs |= RTA_DST;
206 1.1 is }
207 1.1 is #define NEXTADDR(w, s) \
208 1.1 is if (rtm->rtm_addrs & (w)) { \
209 1.1 is (void)memcpy(cp, s, ((struct sockaddr *)s)->sa_len); \
210 1.1 is cp += ((struct sockaddr *)s)->sa_len;}
211 1.1 is
212 1.1 is NEXTADDR(RTA_DST, sin_m);
213 1.1 is NEXTADDR(RTA_GATEWAY, sdl_m);
214 1.1 is
215 1.1 is rtm->rtm_msglen = cp - (char *)&m_rtmsg;
216 1.1 is
217 1.1 is l = rtm->rtm_msglen;
218 1.1 is rtm->rtm_seq = ++seq;
219 1.1 is rtm->rtm_type = cmd;
220 1.1 is if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
221 1.1 is if (errno != ESRCH && errno != EEXIST) {
222 1.1 is warn("writing to routing socket");
223 1.1 is return (-1);
224 1.1 is }
225 1.1 is }
226 1.1 is do {
227 1.1 is l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
228 1.1 is } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
229 1.1 is if (l < 0)
230 1.1 is warn("read from routing socket");
231 1.1 is return (0);
232 1.1 is }
233