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