calloc - Work around gcc-8 bug (2).
[dragonfly.git] / libexec / bootpd / rtmsg.c
blobe2e14ab47286ae017cce72f1b702ac629f2e2fc0
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.
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.
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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
37 * from arp.c 8.2 (Berkeley) 1/2/94
38 * $FreeBSD: src/libexec/bootpd/rtmsg.c,v 1.10 1999/08/28 00:09:19 peter Exp $
39 * $DragonFly: src/libexec/bootpd/rtmsg.c,v 1.4 2007/11/25 01:28:23 swildner Exp $
42 #include <sys/param.h>
44 * Verify that we are at least 4.4 BSD
46 #if defined(BSD)
47 #if BSD >= 199306
49 #include <sys/socket.h>
50 #include <sys/filio.h>
51 #include <sys/time.h>
53 #include <net/if.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/if_ether.h>
61 #include <arpa/inet.h>
63 #include <errno.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <syslog.h>
68 #include <unistd.h>
70 #include "report.h"
73 static int rtmsg (int);
75 static int s = -1; /* routing socket */
79 * Open the routing socket
81 static void
82 getsocket(void)
84 if (s < 0) {
85 s = socket(PF_ROUTE, SOCK_RAW, 0);
86 if (s < 0) {
87 report(LOG_ERR, "socket %s", strerror(errno));
88 exit(1);
90 } else {
92 * Drain the socket of any unwanted routing messages.
94 int n;
95 char buf[512];
97 ioctl(s, FIONREAD, &n);
98 while (n > 0) {
99 read(s, buf, sizeof buf);
100 ioctl(s, FIONREAD, &n);
105 static struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
106 static struct sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
107 static struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
108 static int expire_time, flags, export_only, doing_proxy;
109 static struct {
110 struct rt_msghdr m_rtm;
111 char m_space[512];
112 } m_rtmsg;
115 * Set an individual arp entry
118 bsd_arp_set(struct in_addr *ia, char *eaddr, int len)
120 struct sockaddr_inarp *sin = &sin_m;
121 struct sockaddr_dl *sdl;
122 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
123 u_char *ea;
124 struct timespec tp;
125 int op = RTM_ADD;
127 getsocket();
128 sdl_m = blank_sdl;
129 sin_m = blank_sin;
130 sin->sin_addr = *ia;
132 ea = (u_char *)LLADDR(&sdl_m);
133 bcopy(eaddr, ea, len);
134 sdl_m.sdl_alen = len;
135 doing_proxy = flags = export_only = expire_time = 0;
137 /* make arp entry temporary */
138 clock_gettime(CLOCK_MONOTONIC, &tp);
139 expire_time = tp.tv_sec + 20 * 60;
141 tryagain:
142 if (rtmsg(RTM_GET) < 0) {
143 report(LOG_WARNING, "rtmget: %s", strerror(errno));
144 return (1);
146 sin = (struct sockaddr_inarp *)(rtm + 1);
147 sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
148 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
149 if (sdl->sdl_family == AF_LINK &&
150 (rtm->rtm_flags & RTF_LLINFO) &&
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;
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);
162 if (sin_m.sin_other & SIN_PROXY) {
163 report(LOG_WARNING,
164 "set: proxy entry exists for non 802 device\n");
165 return(1);
167 sin_m.sin_other = SIN_PROXY;
168 export_only = 1;
169 goto tryagain;
171 overwrite:
172 if (sdl->sdl_family != AF_LINK) {
173 report(LOG_WARNING,
174 "cannot intuit interface index and type for %s\n",
175 inet_ntoa(sin->sin_addr));
176 return (1);
178 sdl_m.sdl_type = sdl->sdl_type;
179 sdl_m.sdl_index = sdl->sdl_index;
180 return (rtmsg(op));
184 static int
185 rtmsg(int cmd)
187 static int seq;
188 int rlen;
189 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
190 char *cp = m_rtmsg.m_space;
191 int l;
193 errno = 0;
194 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
195 rtm->rtm_flags = flags;
196 rtm->rtm_version = RTM_VERSION;
198 switch (cmd) {
199 default:
200 report(LOG_ERR, "set_arp: internal wrong cmd - exiting");
201 exit(1);
202 case RTM_ADD:
203 case RTM_CHANGE:
204 rtm->rtm_addrs |= RTA_GATEWAY;
205 rtm->rtm_rmx.rmx_expire = expire_time;
206 rtm->rtm_inits = RTV_EXPIRE;
207 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
208 sin_m.sin_other = 0;
209 if (doing_proxy) {
210 if (export_only)
211 sin_m.sin_other = SIN_PROXY;
212 else {
213 rtm->rtm_addrs |= RTA_NETMASK;
214 rtm->rtm_flags &= ~RTF_HOST;
217 /* FALLTHROUGH */
218 case RTM_GET:
219 rtm->rtm_addrs |= RTA_DST;
221 #define NEXTADDR(w, s) \
222 if (rtm->rtm_addrs & (w)) { \
223 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
225 NEXTADDR(RTA_DST, sin_m);
226 NEXTADDR(RTA_GATEWAY, sdl_m);
227 NEXTADDR(RTA_NETMASK, so_mask);
229 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
231 l = rtm->rtm_msglen;
232 rtm->rtm_seq = ++seq;
233 rtm->rtm_type = cmd;
234 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
235 if ((errno != ESRCH) && !(errno == EEXIST && cmd == RTM_ADD)){
236 report(LOG_WARNING, "writing to routing socket: %s",
237 strerror(errno));
238 return (-1);
241 do {
242 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
243 } while (l > 0 && (rtm->rtm_type != cmd || rtm->rtm_seq != seq || rtm->rtm_pid != getpid()));
244 if (l < 0)
245 report(LOG_WARNING, "arp: read from routing socket: %s\n",
246 strerror(errno));
247 return (0);
250 #endif /* BSD */
251 #endif /* BSD >= 199306 */