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 <stdio.h> 66 #include <unistd.h> 67 #include <stdlib.h> 68 #include <paths.h> 69 #include <syslog.h> 70 #include <string.h> 71 #include <err.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 err(1, "arp: socket"); 82 } 83 84 struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}}; 85 struct sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m; 86 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m; 87 int expire_time, 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 time; 107 int rt; 108 109 getsocket(); 110 pid = getpid(); 111 112 sdl_m = blank_sdl; 113 sin_m = blank_sin; 114 sin->sin_addr.s_addr = host; 115 memcpy((u_char *)LLADDR(&sdl_m), (char *)eaddr, 6); 116 sdl_m.sdl_alen = 6; 117 doing_proxy = flags = export_only = expire_time = 0; 118 gettimeofday(&time, 0); 119 expire_time = time.tv_sec + 20 * 60; 120 121 tryagain: 122 if (rtmsg(RTM_GET) < 0) { 123 syslog(LOG_ERR,"%s: %m", inet_ntoa(sin->sin_addr)); 124 close(s); 125 s = -1; 126 return (1); 127 } 128 sin = (struct sockaddr_inarp *)((char *)rtm + rtm->rtm_hdrlen); 129 sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin); 130 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) { 131 if (sdl->sdl_family == AF_LINK && 132 (rtm->rtm_flags & RTF_LLINFO) && 133 !(rtm->rtm_flags & RTF_GATEWAY)) 134 switch (sdl->sdl_type) { 135 case IFT_ETHER: 136 case IFT_FDDI: 137 case IFT_ISO88023: 138 case IFT_ISO88024: 139 case IFT_ISO88025: 140 goto overwrite; 141 default: 142 break; 143 } 144 if (doing_proxy == 0) { 145 syslog(LOG_ERR, "arptab_set: can only proxy for %s", 146 inet_ntoa(sin->sin_addr)); 147 close(s); 148 s = -1; 149 return (1); 150 } 151 if (sin_m.sin_other & SIN_PROXY) { 152 syslog(LOG_ERR, 153 "arptab_set: proxy entry exists for non 802 device"); 154 close(s); 155 s = -1; 156 return(1); 157 } 158 sin_m.sin_other = SIN_PROXY; 159 export_only = 1; 160 goto tryagain; 161 } 162 overwrite: 163 if (sdl->sdl_family != AF_LINK) { 164 syslog(LOG_ERR, 165 "arptab_set: cannot intuit interface index and type for %s", 166 inet_ntoa(sin->sin_addr)); 167 close(s); 168 s = -1; 169 return (1); 170 } 171 sdl_m.sdl_type = sdl->sdl_type; 172 sdl_m.sdl_index = sdl->sdl_index; 173 rt = rtmsg(RTM_ADD); 174 close(s); 175 s = -1; 176 return (rt); 177 } 178 179 int 180 rtmsg(int cmd) 181 { 182 static int seq; 183 struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 184 char *cp = m_rtmsg.m_space; 185 int l; 186 187 errno = 0; 188 if (cmd == RTM_DELETE) 189 goto doit; 190 memset((char *)&m_rtmsg, 0, sizeof(m_rtmsg)); 191 rtm->rtm_flags = flags; 192 rtm->rtm_version = RTM_VERSION; 193 194 switch (cmd) { 195 default: 196 syslog(LOG_ERR, "arptab_set: internal wrong cmd"); 197 exit(1); 198 case RTM_ADD: 199 rtm->rtm_addrs |= RTA_GATEWAY; 200 rtm->rtm_rmx.rmx_expire = expire_time; 201 rtm->rtm_inits = RTV_EXPIRE; 202 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 203 sin_m.sin_other = 0; 204 if (doing_proxy) { 205 if (export_only) 206 sin_m.sin_other = SIN_PROXY; 207 else { 208 rtm->rtm_addrs |= RTA_NETMASK; 209 rtm->rtm_flags &= ~RTF_HOST; 210 } 211 } 212 /* FALLTHROUGH */ 213 case RTM_GET: 214 rtm->rtm_addrs |= RTA_DST; 215 } 216 #define NEXTADDR(w, s) \ 217 if (rtm->rtm_addrs & (w)) { \ 218 memcpy(cp, (char *)&s, sizeof(s)); \ 219 cp += sizeof(s); \ 220 } 221 222 NEXTADDR(RTA_DST, sin_m); 223 NEXTADDR(RTA_GATEWAY, sdl_m); 224 NEXTADDR(RTA_NETMASK, so_mask); 225 226 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 227 doit: 228 l = rtm->rtm_msglen; 229 rtm->rtm_seq = ++seq; 230 rtm->rtm_type = cmd; 231 if (write(s, (char *)&m_rtmsg, l) < 0) { 232 if (errno != ESRCH && errno != EEXIST) { 233 syslog(LOG_ERR, "writing to routing socket: %m"); 234 return (-1); 235 } 236 } 237 do { 238 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 239 } while (l > 0 && (rtm->rtm_version != RTM_VERSION || 240 rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 241 if (l < 0) 242 syslog(LOG_ERR, "arptab_set: read from routing socket: %m"); 243 return (0); 244 } 245