rtmsg.c revision 110395
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 */
43
44#include <sys/cdefs.h>
45__FBSDID("$FreeBSD: head/libexec/bootpd/rtmsg.c 110395 2003-02-05 13:45:25Z charnier $");
46
47#include <sys/param.h>
48/*
49 * Verify that we are at least 4.4 BSD
50 */
51#if defined(BSD)
52#if BSD >= 199306
53
54#include <sys/socket.h>
55#include <sys/filio.h>
56#include <sys/time.h>
57
58#include <net/if.h>
59#include <net/if_dl.h>
60#include <net/if_types.h>
61#include <net/route.h>
62
63#include <netinet/in.h>
64#include <netinet/if_ether.h>
65
66#include <arpa/inet.h>
67
68#include <errno.h>
69#include <stdio.h>
70#include <stdlib.h>
71#include <string.h>
72#include <syslog.h>
73#include <unistd.h>
74
75#include "report.h"
76
77
78static int rtmsg(int);
79
80static int s = -1; 	/* routing socket */
81
82
83/*
84 * Open the routing socket
85 */
86static void getsocket () {
87	if (s < 0) {
88		s = socket(PF_ROUTE, SOCK_RAW, 0);
89		if (s < 0) {
90			report(LOG_ERR, "socket %s", strerror(errno));
91			exit(1);
92		}
93	} else {
94		/*
95		 * Drain the socket of any unwanted routing messages.
96		 */
97		int n;
98		char buf[512];
99
100		ioctl(s, FIONREAD, &n);
101		while (n > 0) {
102			read(s, buf, sizeof buf);
103			ioctl(s, FIONREAD, &n);
104		}
105	}
106}
107
108static struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
109static struct	sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
110static struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
111static int	expire_time, flags, export_only, doing_proxy;
112static struct	{
113	struct	rt_msghdr m_rtm;
114	char	m_space[512];
115}	m_rtmsg;
116
117/*
118 * Set an individual arp entry
119 */
120int bsd_arp_set(ia, eaddr, len)
121	struct in_addr *ia;
122	char *eaddr;
123	int len;
124{
125	register struct sockaddr_inarp *sin = &sin_m;
126	register struct sockaddr_dl *sdl;
127	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
128	u_char *ea;
129	struct timeval time;
130	int op = RTM_ADD;
131
132	getsocket();
133	sdl_m = blank_sdl;
134	sin_m = blank_sin;
135	sin->sin_addr = *ia;
136
137	ea = (u_char *)LLADDR(&sdl_m);
138	bcopy(eaddr, ea, len);
139	sdl_m.sdl_alen = len;
140	doing_proxy = flags = export_only = expire_time = 0;
141
142	/* make arp entry temporary */
143	gettimeofday(&time, 0);
144	expire_time = time.tv_sec + 20 * 60;
145
146tryagain:
147	if (rtmsg(RTM_GET) < 0) {
148		report(LOG_WARNING, "rtmget: %s", strerror(errno));
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:
159			op = RTM_CHANGE;
160			goto overwrite;
161		}
162		if (doing_proxy == 0) {
163			report(LOG_WARNING, "set: can only proxy for %s\n",
164				inet_ntoa(sin->sin_addr));
165			return (1);
166		}
167		if (sin_m.sin_other & SIN_PROXY) {
168			report(LOG_WARNING,
169				"set: proxy entry exists for non 802 device\n");
170			return(1);
171		}
172		sin_m.sin_other = SIN_PROXY;
173		export_only = 1;
174		goto tryagain;
175	}
176overwrite:
177	if (sdl->sdl_family != AF_LINK) {
178		report(LOG_WARNING,
179			"cannot intuit interface index and type for %s\n",
180			inet_ntoa(sin->sin_addr));
181		return (1);
182	}
183	sdl_m.sdl_type = sdl->sdl_type;
184	sdl_m.sdl_index = sdl->sdl_index;
185	return (rtmsg(op));
186}
187
188
189static int rtmsg(cmd)
190	int cmd;
191{
192	static int seq;
193	int rlen;
194	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
195	register char *cp = m_rtmsg.m_space;
196	register int l;
197
198	errno = 0;
199	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
200	rtm->rtm_flags = flags;
201	rtm->rtm_version = RTM_VERSION;
202
203	switch (cmd) {
204	default:
205		report(LOG_ERR, "set_arp: internal wrong cmd - exiting");
206		exit(1);
207	case RTM_ADD:
208	case RTM_CHANGE:
209		rtm->rtm_addrs |= RTA_GATEWAY;
210		rtm->rtm_rmx.rmx_expire = expire_time;
211		rtm->rtm_inits = RTV_EXPIRE;
212		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
213		sin_m.sin_other = 0;
214		if (doing_proxy) {
215			if (export_only)
216				sin_m.sin_other = SIN_PROXY;
217			else {
218				rtm->rtm_addrs |= RTA_NETMASK;
219				rtm->rtm_flags &= ~RTF_HOST;
220			}
221		}
222		/* FALLTHROUGH */
223	case RTM_GET:
224		rtm->rtm_addrs |= RTA_DST;
225	}
226#define NEXTADDR(w, s) \
227	if (rtm->rtm_addrs & (w)) { \
228		bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
229
230	NEXTADDR(RTA_DST, sin_m);
231	NEXTADDR(RTA_GATEWAY, sdl_m);
232	NEXTADDR(RTA_NETMASK, so_mask);
233
234	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
235
236	l = rtm->rtm_msglen;
237	rtm->rtm_seq = ++seq;
238	rtm->rtm_type = cmd;
239	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
240		if ((errno != ESRCH) && !(errno == EEXIST && cmd == RTM_ADD)){
241			report(LOG_WARNING, "writing to routing socket: %s",
242				strerror(errno));
243			return (-1);
244		}
245	}
246	do {
247		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
248	} while (l > 0 && (rtm->rtm_type != cmd || rtm->rtm_seq != seq || rtm->rtm_pid != getpid()));
249	if (l < 0)
250		report(LOG_WARNING, "arp: read from routing socket: %s\n",
251		    strerror(errno));
252	return (0);
253}
254
255#endif /* BSD */
256#endif /* BSD >= 199306 */
257