rtmsg.c revision 3230
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.2 1994/09/10 15:13:28 csgr 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
114	getsocket();
115	sdl_m = blank_sdl;
116	sin_m = blank_sin;
117	sin->sin_addr = *ia;
118
119	ea = (u_char *)LLADDR(&sdl_m);
120	bcopy(eaddr, ea, len);
121	sdl_m.sdl_alen = len;
122	doing_proxy = flags = export_only = expire_time = 0;
123
124	/* make arp entry temporary */
125	gettimeofday(&time, 0);
126	expire_time = time.tv_sec + 20 * 60;
127
128tryagain:
129	if (rtmsg(RTM_GET) < 0) {
130		report(LOG_WARNING, "rtmget: %s", strerror(errno));
131		return (1);
132	}
133	sin = (struct sockaddr_inarp *)(rtm + 1);
134	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
135	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
136		if (sdl->sdl_family == AF_LINK &&
137		    (rtm->rtm_flags & RTF_LLINFO) &&
138		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
139		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
140		case IFT_ISO88024: case IFT_ISO88025:
141			goto overwrite;
142		}
143		if (doing_proxy == 0) {
144			report(LOG_WARNING, "set: can only proxy for %s\n",
145				inet_ntoa(sin->sin_addr));
146			return (1);
147		}
148		if (sin_m.sin_other & SIN_PROXY) {
149			report(LOG_WARNING,
150				"set: proxy entry exists for non 802 device\n");
151			return(1);
152		}
153		sin_m.sin_other = SIN_PROXY;
154		export_only = 1;
155		goto tryagain;
156	}
157overwrite:
158	if (sdl->sdl_family != AF_LINK) {
159		report(LOG_WARNING,
160			"cannot intuit interface index and type for %s\n",
161			inet_ntoa(sin->sin_addr));
162		return (1);
163	}
164	sdl_m.sdl_type = sdl->sdl_type;
165	sdl_m.sdl_index = sdl->sdl_index;
166	return (rtmsg(RTM_ADD));
167}
168
169
170static int rtmsg(cmd)
171	int cmd;
172{
173	static int seq;
174	int rlen;
175	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
176	register char *cp = m_rtmsg.m_space;
177	register int l;
178
179	errno = 0;
180	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
181	rtm->rtm_flags = flags;
182	rtm->rtm_version = RTM_VERSION;
183
184	switch (cmd) {
185	default:
186		report(LOG_ERR, "set_arp: internal wrong cmd - exiting");
187		exit(1);
188	case RTM_ADD:
189		rtm->rtm_addrs |= RTA_GATEWAY;
190		rtm->rtm_rmx.rmx_expire = expire_time;
191		rtm->rtm_inits = RTV_EXPIRE;
192		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
193		sin_m.sin_other = 0;
194		if (doing_proxy) {
195			if (export_only)
196				sin_m.sin_other = SIN_PROXY;
197			else {
198				rtm->rtm_addrs |= RTA_NETMASK;
199				rtm->rtm_flags &= ~RTF_HOST;
200			}
201		}
202		/* FALLTHROUGH */
203	case RTM_GET:
204		rtm->rtm_addrs |= RTA_DST;
205	}
206#define NEXTADDR(w, s) \
207	if (rtm->rtm_addrs & (w)) { \
208		bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
209
210	NEXTADDR(RTA_DST, sin_m);
211	NEXTADDR(RTA_GATEWAY, sdl_m);
212	NEXTADDR(RTA_NETMASK, so_mask);
213
214	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
215
216	l = rtm->rtm_msglen;
217	rtm->rtm_seq = ++seq;
218	rtm->rtm_type = cmd;
219	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
220		if ((errno != ESRCH) && !(errno == EEXIST && cmd == RTM_ADD)){
221			report(LOG_WARNING, "writing to routing socket: %s",
222				strerror(errno));
223			return (-1);
224		}
225	}
226	do {
227		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
228	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != getpid()));
229	if (l < 0)
230		report(LOG_WARNING, "arp: read from routing socket: %s\n",
231		    strerror(errno));
232	return (0);
233}
234
235#endif /* BSD */
236#endif /* BSD >= 199306 */
237