Home | History | Annotate | Line # | Download | only in rarpd
mkarp.c revision 1.5
      1 /*	$NetBSD: mkarp.c,v 1.5 2003/04/08 04:04:01 petrov 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.5 2003/04/08 04:04:01 petrov 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 /* Roundup the same way rt_xaddrs does */
     84 #define ROUNDUP(a) \
     85        ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
     86 
     87 int	rtmsg(int, int, struct rt_msghdr *, struct sockaddr_inarp *,
     88 	      struct sockaddr_dl *);
     89 struct	{
     90 	struct	rt_msghdr m_rtm;
     91 	char	m_space[512];
     92 }	m_rtmsg;
     93 
     94 /*
     95  * Set an individual arp entry
     96  */
     97 int
     98 mkarp(u_char *haddr, u_int32_t ipaddr)
     99 {
    100 	static struct sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET };
    101 	static struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK };
    102 
    103 	struct sockaddr_inarp *sin;
    104 	struct sockaddr_dl *sdl;
    105 	struct rt_msghdr *rtm;
    106 	u_int8_t *p, *endp;
    107 	int result;
    108 	int s;
    109 
    110 	struct sockaddr_inarp sin_m;
    111 	struct sockaddr_dl sdl_m;
    112 
    113 	sin = &sin_m;
    114 	rtm = &(m_rtmsg.m_rtm);
    115 
    116 	sdl_m = blank_sdl;		/* struct copy */
    117 	sin_m = blank_sin;		/* struct copy */
    118 
    119 	sin->sin_addr.s_addr = ipaddr;
    120 
    121 	p = LLADDR(&sdl_m);
    122 	endp = ((caddr_t)&sdl_m) + sdl_m.sdl_len;
    123 	if (endp > (p + ETHER_ADDR_LEN))
    124 		endp = p + ETHER_ADDR_LEN;
    125 
    126 	while (p < endp) {
    127 		*p++ = *haddr++;
    128 	}
    129 	sdl_m.sdl_alen = ETHER_ADDR_LEN;
    130 
    131 	/*
    132 	 * We need to close and open the socket to prevent routing updates
    133 	 * building up such that when we send our message we never see our
    134 	 * reply (and hang)
    135 	 */
    136 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    137 	if (s < 0)
    138 		err(1, "socket");
    139 
    140 	rtm->rtm_flags = 0;
    141 
    142 	if (rtmsg(RTM_GET, s, rtm, &sin_m, &sdl_m) < 0) {
    143 #if 0
    144 		warn("%s", host);
    145 #endif
    146 		close(s);
    147 		return (1);
    148 	}
    149 	sin = (struct sockaddr_inarp *)(rtm + 1);
    150 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
    151 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    152 		if (sdl->sdl_family == AF_LINK &&
    153 		    (rtm->rtm_flags & RTF_LLINFO) &&
    154 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    155 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    156 		case IFT_ISO88024: case IFT_ISO88025: case IFT_ARCNET:
    157 			goto overwrite;
    158 		}
    159 #if 0
    160 		(void)printf("set: can only proxy for %s\n", host);
    161 #endif
    162 		close(s);
    163 		return (1);
    164 	}
    165 overwrite:
    166 	if (sdl->sdl_family != AF_LINK) {
    167 #if 0
    168 		(void)printf("cannot intuit interface index and type for %s\n",
    169 		    host);
    170 #endif
    171 		close(s);
    172 		return (1);
    173 	}
    174 	sdl_m.sdl_type = sdl->sdl_type;
    175 	sdl_m.sdl_index = sdl->sdl_index;
    176 	result = rtmsg(RTM_ADD, s, rtm, &sin_m, &sdl_m);
    177 	close(s);
    178 	return (result);
    179 }
    180 
    181 int
    182 rtmsg(int cmd, int s, struct rt_msghdr *rtm, struct sockaddr_inarp *sin_m,
    183       struct sockaddr_dl *sdl_m)
    184 {
    185 	static int seq;
    186 	int rlen;
    187 	char *cp;
    188 	int l;
    189 	pid_t pid;
    190 	struct timeval time;
    191 
    192 	rtm = &m_rtmsg.m_rtm;
    193 	cp = m_rtmsg.m_space;
    194 	errno = 0;
    195 
    196 	pid = getpid();
    197 
    198 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    199 	rtm->rtm_version = RTM_VERSION;
    200 
    201 	switch (cmd) {
    202 	default:
    203 		errx(1, "internal wrong cmd");
    204 		/*NOTREACHED*/
    205 	case RTM_ADD:
    206 		rtm->rtm_addrs |= RTA_GATEWAY;
    207 		(void)gettimeofday(&time, 0);
    208 		rtm->rtm_rmx.rmx_expire = time.tv_sec + 20 * 60;
    209 		rtm->rtm_inits = RTV_EXPIRE;
    210 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
    211 		sin_m->sin_other = 0;
    212 
    213 		/* FALLTHROUGH */
    214 	case RTM_GET:
    215 		rtm->rtm_addrs |= RTA_DST;
    216 	}
    217 #define NEXTADDR(w, s) \
    218 	if (rtm->rtm_addrs & (w)) { \
    219 		(void)memcpy(cp, s, ((struct sockaddr *)s)->sa_len); \
    220                 cp += ROUNDUP(((struct sockaddr *)s)->sa_len);}
    221 
    222 	NEXTADDR(RTA_DST, sin_m);
    223 	NEXTADDR(RTA_GATEWAY, sdl_m);
    224 
    225 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
    226 
    227 	l = rtm->rtm_msglen;
    228 	rtm->rtm_seq = ++seq;
    229 	rtm->rtm_type = cmd;
    230 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
    231 		if (errno != ESRCH && errno != EEXIST) {
    232 			warn("writing to routing socket");
    233 			return (-1);
    234 		}
    235 	}
    236 	do {
    237 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
    238 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    239 	if (l < 0)
    240 		warn("read from routing socket");
    241 	return (0);
    242 }
    243