xref: /freebsd/libexec/bootpd/rtmsg.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
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 #include <sys/param.h>
44 /*
45  * Verify that we are at least 4.4 BSD
46  */
47 #if defined(BSD)
48 #if BSD >= 199306
49 
50 #include <sys/socket.h>
51 #include <sys/filio.h>
52 #include <sys/time.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 <stdlib.h>
67 #include <string.h>
68 #include <syslog.h>
69 #include <unistd.h>
70 
71 #include "report.h"
72 
73 
74 static int rtmsg(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_in 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, 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
117 bsd_arp_set(struct in_addr *ia, char *eaddr, int len)
118 {
119 	struct sockaddr_in *sin = &sin_m;
120 	struct sockaddr_dl *sdl;
121 	struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
122 	u_char *ea;
123 	struct timespec tp;
124 	int op = RTM_ADD;
125 
126 	getsocket();
127 	sdl_m = blank_sdl;
128 	sin_m = blank_sin;
129 	sin->sin_addr = *ia;
130 
131 	ea = (u_char *)LLADDR(&sdl_m);
132 	bcopy(eaddr, ea, len);
133 	sdl_m.sdl_alen = len;
134 	doing_proxy = flags = expire_time = 0;
135 
136 	/* make arp entry temporary */
137 	clock_gettime(CLOCK_MONOTONIC, &tp);
138 	expire_time = tp.tv_sec + 20 * 60;
139 
140 tryagain:
141 	if (rtmsg(RTM_GET) < 0) {
142 		report(LOG_WARNING, "rtmget: %s", strerror(errno));
143 		return (1);
144 	}
145 	sin = (struct sockaddr_in *)(rtm + 1);
146 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
147 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
148 		if (sdl->sdl_family == AF_LINK &&
149 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
150 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
151 		case IFT_ISO88024: case IFT_ISO88025:
152 			op = RTM_CHANGE;
153 			goto overwrite;
154 		}
155 		if (doing_proxy == 0) {
156 			report(LOG_WARNING, "set: can only proxy for %s\n",
157 				inet_ntoa(sin->sin_addr));
158 			return (1);
159 		}
160 		goto tryagain;
161 	}
162 overwrite:
163 	if (sdl->sdl_family != AF_LINK) {
164 		report(LOG_WARNING,
165 			"cannot intuit interface index and type for %s\n",
166 			inet_ntoa(sin->sin_addr));
167 		return (1);
168 	}
169 	sdl_m.sdl_type = sdl->sdl_type;
170 	sdl_m.sdl_index = sdl->sdl_index;
171 	return (rtmsg(op));
172 }
173 
174 
175 static int
176 rtmsg(int cmd)
177 {
178 	static int seq;
179 	int rlen;
180 	struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
181 	char *cp = m_rtmsg.m_space;
182 	int l;
183 
184 	errno = 0;
185 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
186 	rtm->rtm_flags = flags;
187 	rtm->rtm_version = RTM_VERSION;
188 
189 	switch (cmd) {
190 	default:
191 		report(LOG_ERR, "set_arp: internal wrong cmd - exiting");
192 		exit(1);
193 	case RTM_ADD:
194 	case RTM_CHANGE:
195 		rtm->rtm_addrs |= RTA_GATEWAY;
196 		rtm->rtm_rmx.rmx_expire = expire_time;
197 		rtm->rtm_inits = RTV_EXPIRE;
198 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
199 		if (doing_proxy) {
200 			rtm->rtm_addrs |= RTA_NETMASK;
201 			rtm->rtm_flags &= ~RTF_HOST;
202 		}
203 		/* FALLTHROUGH */
204 	case RTM_GET:
205 		rtm->rtm_addrs |= RTA_DST;
206 	}
207 #define NEXTADDR(w, s) \
208 	if (rtm->rtm_addrs & (w)) { \
209 		bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
210 
211 	NEXTADDR(RTA_DST, sin_m);
212 	NEXTADDR(RTA_GATEWAY, sdl_m);
213 	NEXTADDR(RTA_NETMASK, so_mask);
214 
215 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
216 
217 	l = rtm->rtm_msglen;
218 	rtm->rtm_seq = ++seq;
219 	rtm->rtm_type = cmd;
220 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
221 		if ((errno != ESRCH) && !(errno == EEXIST && cmd == RTM_ADD)){
222 			report(LOG_WARNING, "writing to routing socket: %s",
223 				strerror(errno));
224 			return (-1);
225 		}
226 	}
227 	do {
228 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
229 	} while (l > 0 && (rtm->rtm_type != cmd || rtm->rtm_seq != seq || rtm->rtm_pid != getpid()));
230 	if (l < 0)
231 		report(LOG_WARNING, "arp: read from routing socket: %s\n",
232 		    strerror(errno));
233 	return (0);
234 }
235 
236 #endif /* BSD */
237 #endif /* BSD >= 199306 */
238