rtmsg.c revision 5661
1/*
2 * Copyright (c) 1984, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 * Copyright (c) 1994
5 *	Geoffrey M. Rehmet, All rights reserved.
6 *
7 * This code is derived from software which forms part of the 4.4-Lite
8 * Berkeley software distribution, which was in derived from software
9 * contributed to Berkeley by Sun Microsystems, Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the University of
22 *	California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40/*
41 * from arp.c	8.2 (Berkeley) 1/2/94
42 * $Id: rtmsg.c,v 1.1.1.1 1994/09/30 05:45:06 pst Exp $
43 */
44
45#include <sys/param.h>
46/*
47 * Verify that we are at least 4.4 BSD
48 */
49#if defined(BSD)
50#if BSD >= 199306
51
52#include <sys/socket.h>
53
54#include <net/if.h>
55#include <net/if_dl.h>
56#include <net/if_types.h>
57#include <net/route.h>
58
59#include <netinet/in.h>
60#include <netinet/if_ether.h>
61
62#include <arpa/inet.h>
63
64#include <errno.h>
65#include <stdio.h>
66#include <string.h>
67#include <syslog.h>
68#include <unistd.h>
69
70#include "report.h"
71
72
73static int rtmsg __P((int));
74
75static int s = -1; 	/* routing socket */
76
77
78/*
79 * Open the routing socket
80 */
81static void getsocket () {
82	if (s < 0) {
83		s = socket(PF_ROUTE, SOCK_RAW, 0);
84		if (s < 0) {
85			report(LOG_ERR, "socket %s", strerror(errno));
86			exit(1);
87		}
88	}
89}
90
91static struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
92static struct	sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
93static struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
94static int	expire_time, flags, export_only, doing_proxy;
95static struct	{
96	struct	rt_msghdr m_rtm;
97	char	m_space[512];
98}	m_rtmsg;
99
100/*
101 * Set an individual arp entry
102 */
103int bsd_arp_set(ia, eaddr, len)
104	struct in_addr *ia;
105	char *eaddr;
106	int len;
107{
108	register struct sockaddr_inarp *sin = &sin_m;
109	register struct sockaddr_dl *sdl;
110	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
111	u_char *ea;
112	struct timeval time;
113	int op = RTM_ADD;
114
115	getsocket();
116	sdl_m = blank_sdl;
117	sin_m = blank_sin;
118	sin->sin_addr = *ia;
119
120	ea = (u_char *)LLADDR(&sdl_m);
121	bcopy(eaddr, ea, len);
122	sdl_m.sdl_alen = len;
123	doing_proxy = flags = export_only = expire_time = 0;
124
125	/* make arp entry temporary */
126	gettimeofday(&time, 0);
127	expire_time = time.tv_sec + 20 * 60;
128
129tryagain:
130	if (rtmsg(RTM_GET) < 0) {
131		report(LOG_WARNING, "rtmget: %s", strerror(errno));
132		return (1);
133	}
134	sin = (struct sockaddr_inarp *)(rtm + 1);
135	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
136	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
137		if (sdl->sdl_family == AF_LINK &&
138		    (rtm->rtm_flags & RTF_LLINFO) &&
139		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
140		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
141		case IFT_ISO88024: case IFT_ISO88025:
142			op = RTM_CHANGE;
143			goto overwrite;
144		}
145		if (doing_proxy == 0) {
146			report(LOG_WARNING, "set: can only proxy for %s\n",
147				inet_ntoa(sin->sin_addr));
148			return (1);
149		}
150		if (sin_m.sin_other & SIN_PROXY) {
151			report(LOG_WARNING,
152				"set: proxy entry exists for non 802 device\n");
153			return(1);
154		}
155		sin_m.sin_other = SIN_PROXY;
156		export_only = 1;
157		goto tryagain;
158	}
159overwrite:
160	if (sdl->sdl_family != AF_LINK) {
161		report(LOG_WARNING,
162			"cannot intuit interface index and type for %s\n",
163			inet_ntoa(sin->sin_addr));
164		return (1);
165	}
166	sdl_m.sdl_type = sdl->sdl_type;
167	sdl_m.sdl_index = sdl->sdl_index;
168	return (rtmsg(op));
169}
170
171
172static int rtmsg(cmd)
173	int cmd;
174{
175	static int seq;
176	int rlen;
177	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
178	register char *cp = m_rtmsg.m_space;
179	register int l;
180
181	errno = 0;
182	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
183	rtm->rtm_flags = flags;
184	rtm->rtm_version = RTM_VERSION;
185
186	switch (cmd) {
187	default:
188		report(LOG_ERR, "set_arp: internal wrong cmd - exiting");
189		exit(1);
190	case RTM_ADD:
191	case RTM_CHANGE:
192		rtm->rtm_addrs |= RTA_GATEWAY;
193		rtm->rtm_rmx.rmx_expire = expire_time;
194		rtm->rtm_inits = RTV_EXPIRE;
195		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
196		sin_m.sin_other = 0;
197		if (doing_proxy) {
198			if (export_only)
199				sin_m.sin_other = SIN_PROXY;
200			else {
201				rtm->rtm_addrs |= RTA_NETMASK;
202				rtm->rtm_flags &= ~RTF_HOST;
203			}
204		}
205		/* FALLTHROUGH */
206	case RTM_GET:
207		rtm->rtm_addrs |= RTA_DST;
208	}
209#define NEXTADDR(w, s) \
210	if (rtm->rtm_addrs & (w)) { \
211		bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
212
213	NEXTADDR(RTA_DST, sin_m);
214	NEXTADDR(RTA_GATEWAY, sdl_m);
215	NEXTADDR(RTA_NETMASK, so_mask);
216
217	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
218
219	l = rtm->rtm_msglen;
220	rtm->rtm_seq = ++seq;
221	rtm->rtm_type = cmd;
222	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
223		if ((errno != ESRCH) && !(errno == EEXIST && cmd == RTM_ADD)){
224			report(LOG_WARNING, "writing to routing socket: %s",
225				strerror(errno));
226			return (-1);
227		}
228	}
229	do {
230		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
231	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != getpid()));
232	if (l < 0)
233		report(LOG_WARNING, "arp: read from routing socket: %s\n",
234		    strerror(errno));
235	return (0);
236}
237
238#endif /* BSD */
239#endif /* BSD >= 199306 */
240