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