xref: /freebsd/libexec/bootpd/rtmsg.c (revision 0d66206fff44f864ea8a4b220c3a53b4caa959a0)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1984, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1994
7  *	Geoffrey M. Rehmet, All rights reserved.
8  *
9  * This code is derived from software which forms part of the 4.4-Lite
10  * Berkeley software distribution, which was in derived from software
11  * contributed to Berkeley by Sun Microsystems, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 /*
39  * from arp.c	8.2 (Berkeley) 1/2/94
40  */
41 
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
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 #include <sys/time.h>
55 
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/route.h>
60 
61 #include <netinet/in.h>
62 #include <netinet/if_ether.h>
63 
64 #include <arpa/inet.h>
65 
66 #include <errno.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <syslog.h>
71 #include <unistd.h>
72 
73 #include "report.h"
74 
75 
76 static int rtmsg(int);
77 
78 static int s = -1; 	/* routing socket */
79 
80 
81 /*
82  * Open the routing socket
83  */
84 static void getsocket () {
85 	if (s < 0) {
86 		s = socket(PF_ROUTE, SOCK_RAW, 0);
87 		if (s < 0) {
88 			report(LOG_ERR, "socket %s", strerror(errno));
89 			exit(1);
90 		}
91 	} else {
92 		/*
93 		 * Drain the socket of any unwanted routing messages.
94 		 */
95 		int n;
96 		char buf[512];
97 
98 		ioctl(s, FIONREAD, &n);
99 		while (n > 0) {
100 			read(s, buf, sizeof buf);
101 			ioctl(s, FIONREAD, &n);
102 		}
103 	}
104 }
105 
106 static struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
107 static struct	sockaddr_in blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
108 static struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
109 static int	expire_time, flags, doing_proxy;
110 static struct	{
111 	struct	rt_msghdr m_rtm;
112 	char	m_space[512];
113 }	m_rtmsg;
114 
115 /*
116  * Set an individual arp entry
117  */
118 int
119 bsd_arp_set(struct in_addr *ia, char *eaddr, int len)
120 {
121 	struct sockaddr_in *sin = &sin_m;
122 	struct sockaddr_dl *sdl;
123 	struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
124 	u_char *ea;
125 	struct timespec tp;
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 = expire_time = 0;
137 
138 	/* make arp entry temporary */
139 	clock_gettime(CLOCK_MONOTONIC, &tp);
140 	expire_time = tp.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_in *)(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_GATEWAY)) switch (sdl->sdl_type) {
152 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
153 		case IFT_ISO88024: case IFT_ISO88025:
154 			op = RTM_CHANGE;
155 			goto overwrite;
156 		}
157 		if (doing_proxy == 0) {
158 			report(LOG_WARNING, "set: can only proxy for %s\n",
159 				inet_ntoa(sin->sin_addr));
160 			return (1);
161 		}
162 		goto tryagain;
163 	}
164 overwrite:
165 	if (sdl->sdl_family != AF_LINK) {
166 		report(LOG_WARNING,
167 			"cannot intuit interface index and type for %s\n",
168 			inet_ntoa(sin->sin_addr));
169 		return (1);
170 	}
171 	sdl_m.sdl_type = sdl->sdl_type;
172 	sdl_m.sdl_index = sdl->sdl_index;
173 	return (rtmsg(op));
174 }
175 
176 
177 static int
178 rtmsg(int cmd)
179 {
180 	static int seq;
181 	int rlen;
182 	struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
183 	char *cp = m_rtmsg.m_space;
184 	int l;
185 
186 	errno = 0;
187 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
188 	rtm->rtm_flags = flags;
189 	rtm->rtm_version = RTM_VERSION;
190 
191 	switch (cmd) {
192 	default:
193 		report(LOG_ERR, "set_arp: internal wrong cmd - exiting");
194 		exit(1);
195 	case RTM_ADD:
196 	case RTM_CHANGE:
197 		rtm->rtm_addrs |= RTA_GATEWAY;
198 		rtm->rtm_rmx.rmx_expire = expire_time;
199 		rtm->rtm_inits = RTV_EXPIRE;
200 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
201 		if (doing_proxy) {
202 			rtm->rtm_addrs |= RTA_NETMASK;
203 			rtm->rtm_flags &= ~RTF_HOST;
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_type != cmd || 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