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 /* 34 * set arp table entries 35 */ 36 37 38 #include <sys/param.h> 39 #include <sys/file.h> 40 #include <sys/socket.h> 41 #include <sys/sysctl.h> 42 43 #include <net/if.h> 44 #include <net/if_dl.h> 45 #include <net/if_types.h> 46 #include <net/route.h> 47 48 #include <netinet/in.h> 49 #include <netinet/if_ether.h> 50 51 #include <arpa/inet.h> 52 53 #include <netdb.h> 54 #include <errno.h> 55 #include <stdio.h> 56 #include <unistd.h> 57 #include <stdlib.h> 58 #include <paths.h> 59 #include <syslog.h> 60 #include <string.h> 61 #include <err.h> 62 63 static pid_t pid; 64 static int s = -1; 65 66 void 67 getsocket(void) 68 { 69 s = socket(PF_ROUTE, SOCK_RAW, 0); 70 if (s < 0) 71 err(1, "arp: socket"); 72 } 73 74 struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}}; 75 struct sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m; 76 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m; 77 int expire_time, flags, export_only, doing_proxy; 78 79 struct { 80 struct rt_msghdr m_rtm; 81 char m_space[512]; 82 } m_rtmsg; 83 84 int arptab_set(u_char *, u_int32_t); 85 int rtmsg(int); 86 87 /* 88 * Set an individual arp entry 89 */ 90 int 91 arptab_set(u_char *eaddr, u_int32_t host) 92 { 93 struct sockaddr_inarp *sin = &sin_m; 94 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); 95 struct sockaddr_dl *sdl; 96 struct timeval time; 97 int rt; 98 99 getsocket(); 100 pid = getpid(); 101 102 sdl_m = blank_sdl; 103 sin_m = blank_sin; 104 sin->sin_addr.s_addr = host; 105 memcpy((u_char *)LLADDR(&sdl_m), (char *)eaddr, 6); 106 sdl_m.sdl_alen = 6; 107 doing_proxy = flags = export_only = expire_time = 0; 108 gettimeofday(&time, 0); 109 expire_time = time.tv_sec + 20 * 60; 110 111 tryagain: 112 if (rtmsg(RTM_GET) < 0) { 113 syslog(LOG_ERR,"%s: %m", inet_ntoa(sin->sin_addr)); 114 close(s); 115 s = -1; 116 return (1); 117 } 118 sin = (struct sockaddr_inarp *)((char *)rtm + rtm->rtm_hdrlen); 119 sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin); 120 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) { 121 if (sdl->sdl_family == AF_LINK && 122 (rtm->rtm_flags & RTF_LLINFO) && 123 !(rtm->rtm_flags & RTF_GATEWAY)) 124 switch (sdl->sdl_type) { 125 case IFT_ETHER: 126 case IFT_FDDI: 127 case IFT_ISO88023: 128 case IFT_ISO88024: 129 case IFT_ISO88025: 130 goto overwrite; 131 default: 132 break; 133 } 134 if (doing_proxy == 0) { 135 syslog(LOG_ERR, "arptab_set: can only proxy for %s", 136 inet_ntoa(sin->sin_addr)); 137 close(s); 138 s = -1; 139 return (1); 140 } 141 if (sin_m.sin_other & SIN_PROXY) { 142 syslog(LOG_ERR, 143 "arptab_set: proxy entry exists for non 802 device"); 144 close(s); 145 s = -1; 146 return(1); 147 } 148 sin_m.sin_other = SIN_PROXY; 149 export_only = 1; 150 goto tryagain; 151 } 152 overwrite: 153 if (sdl->sdl_family != AF_LINK) { 154 syslog(LOG_ERR, 155 "arptab_set: cannot intuit interface index and type for %s", 156 inet_ntoa(sin->sin_addr)); 157 close(s); 158 s = -1; 159 return (1); 160 } 161 sdl_m.sdl_type = sdl->sdl_type; 162 sdl_m.sdl_index = sdl->sdl_index; 163 rt = rtmsg(RTM_ADD); 164 close(s); 165 s = -1; 166 return (rt); 167 } 168 169 int 170 rtmsg(int cmd) 171 { 172 static int seq; 173 struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 174 char *cp = m_rtmsg.m_space; 175 int l; 176 177 errno = 0; 178 if (cmd == RTM_DELETE) 179 goto doit; 180 memset((char *)&m_rtmsg, 0, sizeof(m_rtmsg)); 181 rtm->rtm_flags = flags; 182 rtm->rtm_version = RTM_VERSION; 183 184 switch (cmd) { 185 default: 186 syslog(LOG_ERR, "arptab_set: internal wrong cmd"); 187 exit(1); 188 case RTM_ADD: 189 rtm->rtm_addrs |= RTA_GATEWAY; 190 rtm->rtm_rmx.rmx_expire = expire_time; 191 rtm->rtm_inits = RTV_EXPIRE; 192 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 193 sin_m.sin_other = 0; 194 if (doing_proxy) { 195 if (export_only) 196 sin_m.sin_other = SIN_PROXY; 197 else { 198 rtm->rtm_addrs |= RTA_NETMASK; 199 rtm->rtm_flags &= ~RTF_HOST; 200 } 201 } 202 /* FALLTHROUGH */ 203 case RTM_GET: 204 rtm->rtm_addrs |= RTA_DST; 205 } 206 #define NEXTADDR(w, s) \ 207 if (rtm->rtm_addrs & (w)) { \ 208 memcpy(cp, (char *)&s, sizeof(s)); \ 209 cp += sizeof(s); \ 210 } 211 212 NEXTADDR(RTA_DST, sin_m); 213 NEXTADDR(RTA_GATEWAY, sdl_m); 214 NEXTADDR(RTA_NETMASK, so_mask); 215 216 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 217 doit: 218 l = rtm->rtm_msglen; 219 rtm->rtm_seq = ++seq; 220 rtm->rtm_type = cmd; 221 if (write(s, (char *)&m_rtmsg, l) < 0) { 222 if (errno != ESRCH && errno != EEXIST) { 223 syslog(LOG_ERR, "writing to routing socket: %m"); 224 return (-1); 225 } 226 } 227 do { 228 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 229 } while (l > 0 && (rtm->rtm_version != RTM_VERSION || 230 rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 231 if (l < 0) 232 syslog(LOG_ERR, "arptab_set: read from routing socket: %m"); 233 return (0); 234 } 235