Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/libexec/bootpd/rtmsg.c
34821 views
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/param.h>
43
/*
44
* Verify that we are at least 4.4 BSD
45
*/
46
#if defined(BSD)
47
#if BSD >= 199306
48
49
#include <sys/socket.h>
50
#include <sys/filio.h>
51
#include <sys/time.h>
52
53
#include <net/if.h>
54
#include <net/if_dl.h>
55
#include <net/if_types.h>
56
#include <net/route.h>
57
58
#include <netinet/in.h>
59
#include <netinet/if_ether.h>
60
61
#include <arpa/inet.h>
62
63
#include <errno.h>
64
#include <stdio.h>
65
#include <stdlib.h>
66
#include <string.h>
67
#include <syslog.h>
68
#include <unistd.h>
69
70
#include "report.h"
71
72
73
static int rtmsg(int);
74
75
static int s = -1; /* routing socket */
76
77
78
/*
79
* Open the routing socket
80
*/
81
static 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
} else {
89
/*
90
* Drain the socket of any unwanted routing messages.
91
*/
92
int n;
93
char buf[512];
94
95
ioctl(s, FIONREAD, &n);
96
while (n > 0) {
97
read(s, buf, sizeof buf);
98
ioctl(s, FIONREAD, &n);
99
}
100
}
101
}
102
103
static struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
104
static struct sockaddr_in blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
105
static struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
106
static int expire_time, flags, doing_proxy;
107
static struct {
108
struct rt_msghdr m_rtm;
109
char m_space[512];
110
} m_rtmsg;
111
112
/*
113
* Set an individual arp entry
114
*/
115
int
116
bsd_arp_set(struct in_addr *ia, char *eaddr, int len)
117
{
118
struct sockaddr_in *sin = &sin_m;
119
struct sockaddr_dl *sdl;
120
struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
121
u_char *ea;
122
struct timespec tp;
123
int op = RTM_ADD;
124
125
getsocket();
126
sdl_m = blank_sdl;
127
sin_m = blank_sin;
128
sin->sin_addr = *ia;
129
130
ea = (u_char *)LLADDR(&sdl_m);
131
bcopy(eaddr, ea, len);
132
sdl_m.sdl_alen = len;
133
doing_proxy = flags = expire_time = 0;
134
135
/* make arp entry temporary */
136
clock_gettime(CLOCK_MONOTONIC, &tp);
137
expire_time = tp.tv_sec + 20 * 60;
138
139
tryagain:
140
if (rtmsg(RTM_GET) < 0) {
141
report(LOG_WARNING, "rtmget: %s", strerror(errno));
142
return (1);
143
}
144
sin = (struct sockaddr_in *)(rtm + 1);
145
sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
146
if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
147
if (sdl->sdl_family == AF_LINK &&
148
!(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
149
case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
150
case IFT_ISO88024: case IFT_ISO88025:
151
op = RTM_CHANGE;
152
goto overwrite;
153
}
154
if (doing_proxy == 0) {
155
report(LOG_WARNING, "set: can only proxy for %s\n",
156
inet_ntoa(sin->sin_addr));
157
return (1);
158
}
159
goto tryagain;
160
}
161
overwrite:
162
if (sdl->sdl_family != AF_LINK) {
163
report(LOG_WARNING,
164
"cannot intuit interface index and type for %s\n",
165
inet_ntoa(sin->sin_addr));
166
return (1);
167
}
168
sdl_m.sdl_type = sdl->sdl_type;
169
sdl_m.sdl_index = sdl->sdl_index;
170
return (rtmsg(op));
171
}
172
173
174
static int
175
rtmsg(int cmd)
176
{
177
static int seq;
178
int rlen;
179
struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
180
char *cp = m_rtmsg.m_space;
181
int l;
182
183
errno = 0;
184
bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
185
rtm->rtm_flags = flags;
186
rtm->rtm_version = RTM_VERSION;
187
188
switch (cmd) {
189
default:
190
report(LOG_ERR, "set_arp: internal wrong cmd - exiting");
191
exit(1);
192
case RTM_ADD:
193
case RTM_CHANGE:
194
rtm->rtm_addrs |= RTA_GATEWAY;
195
rtm->rtm_rmx.rmx_expire = expire_time;
196
rtm->rtm_inits = RTV_EXPIRE;
197
rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
198
if (doing_proxy) {
199
rtm->rtm_addrs |= RTA_NETMASK;
200
rtm->rtm_flags &= ~RTF_HOST;
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_type != cmd || 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
238