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