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