13229Spst/*
23229Spst * Copyright (c) 1984, 1993
33229Spst *	The Regents of the University of California.  All rights reserved.
43229Spst * Copyright (c) 1994
53229Spst *	Geoffrey M. Rehmet, All rights reserved.
63229Spst *
73229Spst * This code is derived from software which forms part of the 4.4-Lite
83229Spst * Berkeley software distribution, which was in derived from software
93229Spst * contributed to Berkeley by Sun Microsystems, Inc.
103229Spst *
113229Spst * Redistribution and use in source and binary forms, with or without
123229Spst * modification, are permitted provided that the following conditions
133229Spst * are met:
143229Spst * 1. Redistributions of source code must retain the above copyright
153229Spst *    notice, this list of conditions and the following disclaimer.
163229Spst * 2. Redistributions in binary form must reproduce the above copyright
173229Spst *    notice, this list of conditions and the following disclaimer in the
183229Spst *    documentation and/or other materials provided with the distribution.
193229Spst * 3. All advertising materials mentioning features or use of this software
203229Spst *    must display the following acknowledgement:
213229Spst *	This product includes software developed by the University of
223229Spst *	California, Berkeley and its contributors.
233229Spst * 4. Neither the name of the University nor the names of its contributors
243229Spst *    may be used to endorse or promote products derived from this software
253229Spst *    without specific prior written permission.
263229Spst *
273229Spst * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
283229Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
293229Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
303229Spst * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
313229Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
323229Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
333229Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
343229Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
353229Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
363229Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
373229Spst * SUCH DAMAGE.
383229Spst */
393229Spst
403229Spst/*
413229Spst * from arp.c	8.2 (Berkeley) 1/2/94
423229Spst */
433229Spst
44110395Scharnier#include <sys/cdefs.h>
45110395Scharnier__FBSDID("$FreeBSD$");
46110395Scharnier
473229Spst#include <sys/param.h>
483229Spst/*
493229Spst * Verify that we are at least 4.4 BSD
503229Spst */
513229Spst#if defined(BSD)
523229Spst#if BSD >= 199306
533229Spst
543229Spst#include <sys/socket.h>
556034Sdfr#include <sys/filio.h>
5620287Swollman#include <sys/time.h>
573229Spst
583229Spst#include <net/if.h>
593229Spst#include <net/if_dl.h>
603229Spst#include <net/if_types.h>
613229Spst#include <net/route.h>
623229Spst
633229Spst#include <netinet/in.h>
643229Spst#include <netinet/if_ether.h>
653229Spst
663229Spst#include <arpa/inet.h>
673229Spst
683229Spst#include <errno.h>
693229Spst#include <stdio.h>
70110395Scharnier#include <stdlib.h>
713229Spst#include <string.h>
723229Spst#include <syslog.h>
733229Spst#include <unistd.h>
743229Spst
753229Spst#include "report.h"
763229Spst
773229Spst
78104742Salfredstatic int rtmsg(int);
793229Spst
803229Spststatic int s = -1; 	/* routing socket */
813229Spst
823229Spst
833229Spst/*
843229Spst * Open the routing socket
853229Spst */
863229Spststatic void getsocket () {
873229Spst	if (s < 0) {
883229Spst		s = socket(PF_ROUTE, SOCK_RAW, 0);
893229Spst		if (s < 0) {
903229Spst			report(LOG_ERR, "socket %s", strerror(errno));
913229Spst			exit(1);
923229Spst		}
936034Sdfr	} else {
946034Sdfr		/*
956034Sdfr		 * Drain the socket of any unwanted routing messages.
966034Sdfr		 */
976034Sdfr		int n;
986034Sdfr		char buf[512];
996034Sdfr
1006034Sdfr		ioctl(s, FIONREAD, &n);
1016034Sdfr		while (n > 0) {
1026034Sdfr			read(s, buf, sizeof buf);
1036034Sdfr			ioctl(s, FIONREAD, &n);
1046034Sdfr		}
1053229Spst	}
1063229Spst}
1073229Spst
1083229Spststatic struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
109246143Sglebiusstatic struct	sockaddr_in blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
1103229Spststatic struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
111246143Sglebiusstatic int	expire_time, flags, doing_proxy;
1123229Spststatic struct	{
1133229Spst	struct	rt_msghdr m_rtm;
1143229Spst	char	m_space[512];
1153229Spst}	m_rtmsg;
1163229Spst
1173229Spst/*
1188870Srgrimes * Set an individual arp entry
1193229Spst */
1203229Spstint bsd_arp_set(ia, eaddr, len)
1213229Spst	struct in_addr *ia;
1223229Spst	char *eaddr;
1233229Spst	int len;
1243229Spst{
125246143Sglebius	register struct sockaddr_in *sin = &sin_m;
1263229Spst	register struct sockaddr_dl *sdl;
1273229Spst	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
1283229Spst	u_char *ea;
129216226Sglebius	struct timespec tp;
1305661Sdfr	int op = RTM_ADD;
1313229Spst
1323229Spst	getsocket();
1333229Spst	sdl_m = blank_sdl;
1343229Spst	sin_m = blank_sin;
1353229Spst	sin->sin_addr = *ia;
1363229Spst
1373229Spst	ea = (u_char *)LLADDR(&sdl_m);
1383229Spst	bcopy(eaddr, ea, len);
1393229Spst	sdl_m.sdl_alen = len;
140246143Sglebius	doing_proxy = flags = expire_time = 0;
1413229Spst
1423229Spst	/* make arp entry temporary */
143216226Sglebius	clock_gettime(CLOCK_MONOTONIC, &tp);
144216226Sglebius	expire_time = tp.tv_sec + 20 * 60;
1453229Spst
1463229Spsttryagain:
1473229Spst	if (rtmsg(RTM_GET) < 0) {
1483229Spst		report(LOG_WARNING, "rtmget: %s", strerror(errno));
1493229Spst		return (1);
1503229Spst	}
151246143Sglebius	sin = (struct sockaddr_in *)(rtm + 1);
1523229Spst	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
1533229Spst	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
1543229Spst		if (sdl->sdl_family == AF_LINK &&
1553229Spst		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
1563229Spst		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
1573229Spst		case IFT_ISO88024: case IFT_ISO88025:
1585661Sdfr			op = RTM_CHANGE;
1593229Spst			goto overwrite;
1603229Spst		}
1613229Spst		if (doing_proxy == 0) {
1628870Srgrimes			report(LOG_WARNING, "set: can only proxy for %s\n",
1633229Spst				inet_ntoa(sin->sin_addr));
1643229Spst			return (1);
1653229Spst		}
1663229Spst		goto tryagain;
1673229Spst	}
1683229Spstoverwrite:
1693229Spst	if (sdl->sdl_family != AF_LINK) {
1703229Spst		report(LOG_WARNING,
1718870Srgrimes			"cannot intuit interface index and type for %s\n",
1723229Spst			inet_ntoa(sin->sin_addr));
1733229Spst		return (1);
1743229Spst	}
1753229Spst	sdl_m.sdl_type = sdl->sdl_type;
1763229Spst	sdl_m.sdl_index = sdl->sdl_index;
1775661Sdfr	return (rtmsg(op));
1783229Spst}
1793229Spst
1803229Spst
1813229Spststatic int rtmsg(cmd)
1823229Spst	int cmd;
1833229Spst{
1843229Spst	static int seq;
1853229Spst	int rlen;
1863229Spst	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
1873229Spst	register char *cp = m_rtmsg.m_space;
1883229Spst	register int l;
1893229Spst
1903229Spst	errno = 0;
1913229Spst	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
1923229Spst	rtm->rtm_flags = flags;
1933229Spst	rtm->rtm_version = RTM_VERSION;
1943229Spst
1953229Spst	switch (cmd) {
1963229Spst	default:
1973229Spst		report(LOG_ERR, "set_arp: internal wrong cmd - exiting");
1983229Spst		exit(1);
1993229Spst	case RTM_ADD:
2005661Sdfr	case RTM_CHANGE:
2013229Spst		rtm->rtm_addrs |= RTA_GATEWAY;
2023229Spst		rtm->rtm_rmx.rmx_expire = expire_time;
2033229Spst		rtm->rtm_inits = RTV_EXPIRE;
204190601Scognet		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
2053229Spst		if (doing_proxy) {
206246143Sglebius			rtm->rtm_addrs |= RTA_NETMASK;
207246143Sglebius			rtm->rtm_flags &= ~RTF_HOST;
2083229Spst		}
2093229Spst		/* FALLTHROUGH */
2103229Spst	case RTM_GET:
2113229Spst		rtm->rtm_addrs |= RTA_DST;
2123229Spst	}
2133229Spst#define NEXTADDR(w, s) \
2143229Spst	if (rtm->rtm_addrs & (w)) { \
2153229Spst		bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
2163229Spst
2173229Spst	NEXTADDR(RTA_DST, sin_m);
2183229Spst	NEXTADDR(RTA_GATEWAY, sdl_m);
2193229Spst	NEXTADDR(RTA_NETMASK, so_mask);
2203229Spst
2213229Spst	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
2223229Spst
2233229Spst	l = rtm->rtm_msglen;
2243229Spst	rtm->rtm_seq = ++seq;
2253229Spst	rtm->rtm_type = cmd;
2263229Spst	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
2273229Spst		if ((errno != ESRCH) && !(errno == EEXIST && cmd == RTM_ADD)){
2283229Spst			report(LOG_WARNING, "writing to routing socket: %s",
2293229Spst				strerror(errno));
2303229Spst			return (-1);
2313229Spst		}
2323229Spst	}
2333229Spst	do {
2343229Spst		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
2356034Sdfr	} while (l > 0 && (rtm->rtm_type != cmd || rtm->rtm_seq != seq || rtm->rtm_pid != getpid()));
2363229Spst	if (l < 0)
2373229Spst		report(LOG_WARNING, "arp: read from routing socket: %s\n",
2383229Spst		    strerror(errno));
2393229Spst	return (0);
2403229Spst}
2413229Spst
2423229Spst#endif /* BSD */
2433229Spst#endif /* BSD >= 199306 */
244