Home | History | Annotate | Line # | Download | only in rarpd
mkarp.c revision 1.10
      1 /*	$NetBSD: mkarp.c,v 1.10 2014/12/03 03:44:45 christos 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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 __COPYRIGHT("@(#) Copyright (c) 1984, 1993\
     38  The Regents of the University of California.  All rights reserved.");
     39 #endif /* not lint */
     40 
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "@(#)arp.c	8.3 (Berkeley) 4/28/95";
     44 #else
     45 __RCSID("$NetBSD: mkarp.c,v 1.10 2014/12/03 03:44:45 christos Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 /*
     50  * mkarp - set an arp table entry
     51  */
     52 
     53 #include <sys/param.h>
     54 #include <sys/file.h>
     55 #include <sys/socket.h>
     56 #include <sys/sysctl.h>
     57 
     58 #include <net/if.h>
     59 #include <net/if_dl.h>
     60 #include <net/if_ether.h>
     61 #include <net/if_types.h>
     62 #include <net/route.h>
     63 #include <netinet/in.h>
     64 #include <netinet/if_inarp.h>
     65 #include <arpa/inet.h>
     66 
     67 #include <err.h>
     68 #include <errno.h>
     69 #include <netdb.h>
     70 #include <nlist.h>
     71 #include <paths.h>
     72 #include <stdio.h>
     73 #include <stdlib.h>
     74 #include <string.h>
     75 #include <unistd.h>
     76 
     77 #include "mkarp.h"
     78 
     79 /* Roundup the same way rt_xaddrs does */
     80 #define ROUNDUP(a) \
     81        ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
     82 
     83 int	rtmsg(int, int, struct rt_msghdr *, struct sockaddr_inarp *,
     84 	      struct sockaddr_dl *);
     85 static struct {
     86 	struct	rt_msghdr m_rtm;
     87 	char	m_space[512];
     88 }	m_rtmsg;
     89 
     90 /*
     91  * Set an individual arp entry
     92  */
     93 int
     94 mkarp(u_char *haddr, u_int32_t ipaddr)
     95 {
     96 	static struct sockaddr_inarp blank_sin = {
     97 		.sin_len = sizeof(blank_sin),
     98 		.sin_family = AF_INET,
     99 	};
    100 	static struct sockaddr_dl blank_sdl = {
    101 		.sdl_len = sizeof(blank_sdl),
    102 		.sdl_family = AF_LINK,
    103 	};
    104 
    105 	struct sockaddr_inarp *sin;
    106 	struct sockaddr_dl *sdl;
    107 	struct rt_msghdr *rtm;
    108 	u_int8_t *p, *endp;
    109 	int result;
    110 	int s;
    111 
    112 	struct sockaddr_inarp sin_m;
    113 	struct sockaddr_dl sdl_m;
    114 
    115 	sin = &sin_m;
    116 	rtm = &(m_rtmsg.m_rtm);
    117 
    118 	sdl_m = blank_sdl;		/* struct copy */
    119 	sin_m = blank_sin;		/* struct copy */
    120 
    121 	sin->sin_addr.s_addr = ipaddr;
    122 
    123 	p = LLADDR(&sdl_m);
    124 	endp = ((caddr_t)&sdl_m) + sdl_m.sdl_len;
    125 	if (endp > (p + ETHER_ADDR_LEN))
    126 		endp = p + ETHER_ADDR_LEN;
    127 
    128 	while (p < endp) {
    129 		*p++ = *haddr++;
    130 	}
    131 	sdl_m.sdl_alen = ETHER_ADDR_LEN;
    132 
    133 	/*
    134 	 * We need to close and open the socket to prevent routing updates
    135 	 * building up such that when we send our message we never see our
    136 	 * reply (and hang)
    137 	 */
    138 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    139 	if (s < 0)
    140 		err(1, "socket");
    141 
    142 	rtm->rtm_flags = 0;
    143 
    144 	if (rtmsg(RTM_GET, s, rtm, &sin_m, &sdl_m) < 0) {
    145 #if 0
    146 		warn("%s", host);
    147 #endif
    148 		close(s);
    149 		return (1);
    150 	}
    151 	sin = (struct sockaddr_inarp *)(rtm + 1);
    152 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
    153 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    154 		if (sdl->sdl_family == AF_LINK &&
    155 		    (rtm->rtm_flags & RTF_LLINFO) &&
    156 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    157 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    158 		case IFT_ISO88024: case IFT_ISO88025: case IFT_ARCNET:
    159 			goto overwrite;
    160 		}
    161 #if 0
    162 		(void)printf("set: can only proxy for %s\n", host);
    163 #endif
    164 		close(s);
    165 		return (1);
    166 	}
    167 overwrite:
    168 	if (sdl->sdl_family != AF_LINK) {
    169 #if 0
    170 		(void)printf("cannot intuit interface index and type for %s\n",
    171 		    host);
    172 #endif
    173 		close(s);
    174 		return (1);
    175 	}
    176 	sdl_m.sdl_type = sdl->sdl_type;
    177 	sdl_m.sdl_index = sdl->sdl_index;
    178 	result = rtmsg(RTM_ADD, s, rtm, &sin_m, &sdl_m);
    179 	close(s);
    180 	return (result);
    181 }
    182 
    183 int
    184 rtmsg(int cmd, int s, struct rt_msghdr *rtm, struct sockaddr_inarp *sin_m,
    185       struct sockaddr_dl *sdl_m)
    186 {
    187 	static int seq;
    188 	int rlen;
    189 	char *cp;
    190 	int l;
    191 	pid_t pid;
    192 	struct timeval tv;
    193 
    194 	rtm = &m_rtmsg.m_rtm;
    195 	cp = m_rtmsg.m_space;
    196 	errno = 0;
    197 
    198 	pid = getpid();
    199 
    200 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    201 	rtm->rtm_version = RTM_VERSION;
    202 
    203 	switch (cmd) {
    204 	default:
    205 		errx(1, "internal wrong cmd");
    206 		/*NOTREACHED*/
    207 	case RTM_ADD:
    208 		rtm->rtm_addrs |= RTA_GATEWAY;
    209 		(void)gettimeofday(&tv, 0);
    210 		rtm->rtm_rmx.rmx_expire = tv.tv_sec + 20 * 60;
    211 		rtm->rtm_inits = RTV_EXPIRE;
    212 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
    213 		sin_m->sin_other = 0;
    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 		(void)memcpy(cp, s, ((struct sockaddr *)s)->sa_len); \
    222                 cp += ROUNDUP(((struct sockaddr *)s)->sa_len);}
    223 
    224 	NEXTADDR(RTA_DST, sin_m);
    225 	NEXTADDR(RTA_GATEWAY, sdl_m);
    226 
    227 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
    228 
    229 	l = rtm->rtm_msglen;
    230 	rtm->rtm_seq = ++seq;
    231 	rtm->rtm_type = cmd;
    232 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
    233 		if (errno != ESRCH && errno != EEXIST) {
    234 			warn("writing to routing socket");
    235 			return (-1);
    236 		}
    237 	}
    238 	do {
    239 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
    240 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    241 	if (l < 0)
    242 		warn("read from routing socket");
    243 	return (0);
    244 }
    245