Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sbin/dhclient/dispatch.c
39478 views
1
/* $OpenBSD: dispatch.c,v 1.31 2004/09/21 04:07:03 david Exp $ */
2
3
/*-
4
* SPDX-License-Identifier: BSD-3-Clause
5
*
6
* Copyright 2004 Henning Brauer <[email protected]>
7
* Copyright (c) 1995, 1996, 1997, 1998, 1999
8
* The Internet Software Consortium. All rights reserved.
9
*
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
12
* are met:
13
*
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 Internet Software Consortium nor the names
20
* of its contributors may be used to endorse or promote products derived
21
* from this software without specific prior written permission.
22
*
23
* THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
24
* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27
* DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
28
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
31
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35
* SUCH DAMAGE.
36
*
37
* This software has been written for the Internet Software Consortium
38
* by Ted Lemon <[email protected]> in cooperation with Vixie
39
* Enterprises. To learn more about the Internet Software Consortium,
40
* see ``http://www.vix.com/isc''. To learn more about Vixie
41
* Enterprises, see ``http://www.vix.com''.
42
*/
43
44
#include <sys/cdefs.h>
45
#include "dhcpd.h"
46
#include "privsep.h"
47
48
#include <sys/ioctl.h>
49
50
#include <assert.h>
51
#include <net/if_media.h>
52
#include <ifaddrs.h>
53
#include <poll.h>
54
55
/* Assert that pointer p is aligned to at least align bytes */
56
#define assert_aligned(p, align) assert((((uintptr_t)p) & ((align) - 1)) == 0)
57
58
static struct protocol *protocols;
59
static const struct timespec timespec_intmax_ms = {
60
.tv_sec = INT_MAX / 1000,
61
.tv_nsec = (INT_MAX % 1000) * 1000000
62
};
63
static struct timeout *timeouts;
64
static struct timeout *free_timeouts;
65
static int interfaces_invalidated;
66
void (*bootp_packet_handler)(struct interface_info *,
67
struct dhcp_packet *, int, unsigned int,
68
struct iaddr, struct hardware *);
69
70
static int interface_status(struct interface_info *ifinfo);
71
72
/*
73
* Use getifaddrs() to get a list of all the attached interfaces. For
74
* each interface that's of type INET and not the loopback interface,
75
* register that interface with the network I/O software, figure out
76
* what subnet it's on, and add it to the list of interfaces.
77
*/
78
void
79
discover_interfaces(struct interface_info *iface)
80
{
81
struct ifaddrs *ifap, *ifa;
82
struct ifreq *tif;
83
84
if (getifaddrs(&ifap) != 0)
85
error("getifaddrs failed");
86
87
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
88
if ((ifa->ifa_flags & IFF_LOOPBACK) ||
89
(ifa->ifa_flags & IFF_POINTOPOINT) ||
90
(!(ifa->ifa_flags & IFF_UP)))
91
continue;
92
93
if (strcmp(iface->name, ifa->ifa_name))
94
continue;
95
96
/*
97
* If we have the capability, extract link information
98
* and record it in a linked list.
99
*/
100
if (ifa->ifa_addr->sa_family == AF_LINK) {
101
struct sockaddr_dl *foo;
102
103
/*
104
* The implementation of getifaddrs should guarantee
105
* this alignment
106
*/
107
assert_aligned(ifa->ifa_addr,
108
_Alignof(struct sockaddr_dl));
109
#ifdef __clang__
110
#pragma clang diagnostic push
111
#pragma clang diagnostic ignored "-Wcast-align"
112
#endif
113
foo = (struct sockaddr_dl *)ifa->ifa_addr;
114
#ifdef __clang__
115
#pragma clang diagnostic pop
116
#endif
117
118
iface->index = foo->sdl_index;
119
iface->hw_address.hlen = foo->sdl_alen;
120
iface->hw_address.htype = HTYPE_ETHER; /* XXX */
121
memcpy(iface->hw_address.haddr,
122
LLADDR(foo), foo->sdl_alen);
123
}
124
if (!iface->ifp) {
125
if ((tif = calloc(1, sizeof(struct ifreq))) == NULL)
126
error("no space to remember ifp");
127
strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ);
128
iface->ifp = tif;
129
}
130
131
}
132
133
if (!iface->ifp)
134
error("%s: not found", iface->name);
135
136
/* Register the interface... */
137
if_register_receive(iface);
138
if_register_send(iface);
139
add_protocol(iface->name, iface->rfdesc, got_one, iface);
140
freeifaddrs(ifap);
141
}
142
143
void
144
reinitialize_interfaces(void)
145
{
146
interfaces_invalidated = 1;
147
}
148
149
/*
150
* Wait for packets to come in using poll(). When a packet comes in,
151
* call receive_packet to receive the packet and possibly strip hardware
152
* addressing information from it, and then call through the
153
* bootp_packet_handler hook to try to do something with it.
154
*/
155
void
156
dispatch(void)
157
{
158
int count, live_interfaces, i, to_msec, nfds = 0;
159
struct protocol *l;
160
struct pollfd *fds;
161
struct timespec howlong;
162
163
clock_gettime(CLOCK_MONOTONIC, &time_now);
164
165
for (l = protocols; l; l = l->next)
166
nfds++;
167
168
fds = malloc(nfds * sizeof(struct pollfd));
169
if (fds == NULL)
170
error("Can't allocate poll structures.");
171
172
do {
173
/*
174
* Call any expired timeouts, and then if there's still
175
* a timeout registered, time out the select call then.
176
*/
177
another:
178
if (timeouts) {
179
struct timeout *t;
180
181
if (timespeccmp(&timeouts->when, &time_now, <=)) {
182
t = timeouts;
183
timeouts = timeouts->next;
184
(*(t->func))(t->what);
185
t->next = free_timeouts;
186
free_timeouts = t;
187
goto another;
188
}
189
190
/*
191
* Figure timeout in milliseconds, and check for
192
* potential overflow, so we can cram into an
193
* int for poll, while not polling with a
194
* negative timeout and blocking indefinitely.
195
*/
196
timespecsub(&timeouts->when, &time_now, &howlong);
197
if (timespeccmp(&howlong, &timespec_intmax_ms, >))
198
howlong = timespec_intmax_ms;
199
to_msec = howlong.tv_sec * 1000 + howlong.tv_nsec / 1000000;
200
} else
201
to_msec = -1;
202
203
/* Set up the descriptors to be polled. */
204
live_interfaces = 0;
205
for (i = 0, l = protocols; l; l = l->next) {
206
struct interface_info *ip = l->local;
207
208
if (ip == NULL || ip->dead)
209
continue;
210
fds[i].fd = l->fd;
211
fds[i].events = POLLIN;
212
fds[i].revents = 0;
213
i++;
214
if (l->handler == got_one)
215
live_interfaces++;
216
}
217
if (live_interfaces == 0)
218
error("No live interfaces to poll on - exiting.");
219
220
/* Wait for a packet or a timeout... XXX */
221
count = poll(fds, nfds, to_msec);
222
223
/* Not likely to be transitory... */
224
if (count == -1) {
225
if (errno == EAGAIN || errno == EINTR) {
226
clock_gettime(CLOCK_MONOTONIC, &time_now);
227
cur_time = time(NULL);
228
continue;
229
} else
230
error("poll: %m");
231
}
232
233
/* Get the current time... */
234
clock_gettime(CLOCK_MONOTONIC, &time_now);
235
cur_time = time(NULL);
236
237
i = 0;
238
for (l = protocols; l; l = l->next) {
239
struct interface_info *ip;
240
ip = l->local;
241
if ((fds[i].revents & (POLLIN | POLLHUP))) {
242
fds[i].revents = 0;
243
if (ip && (l->handler != got_one ||
244
!ip->dead))
245
(*(l->handler))(l);
246
if (interfaces_invalidated)
247
break;
248
}
249
i++;
250
}
251
interfaces_invalidated = 0;
252
} while (1);
253
}
254
255
256
void
257
got_one(struct protocol *l)
258
{
259
struct sockaddr_in from;
260
struct hardware hfrom;
261
struct iaddr ifrom;
262
ssize_t result;
263
union {
264
/*
265
* Packet input buffer. Must be as large as largest
266
* possible MTU.
267
*/
268
unsigned char packbuf[4095];
269
struct dhcp_packet packet;
270
} u;
271
struct interface_info *ip = l->local;
272
273
if ((result = receive_packet(ip, u.packbuf, sizeof(u), &from,
274
&hfrom)) == -1) {
275
warning("receive_packet failed on %s: %s", ip->name,
276
strerror(errno));
277
ip->errors++;
278
if ((!interface_status(ip)) ||
279
(ip->noifmedia && ip->errors > 20)) {
280
/* our interface has gone away. */
281
warning("Interface %s no longer appears valid.",
282
ip->name);
283
ip->dead = 1;
284
interfaces_invalidated = 1;
285
close(l->fd);
286
remove_protocol(l);
287
free(ip);
288
}
289
return;
290
}
291
if (result == 0)
292
return;
293
294
if (bootp_packet_handler) {
295
ifrom.len = 4;
296
memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
297
298
(*bootp_packet_handler)(ip, &u.packet, result,
299
from.sin_port, ifrom, &hfrom);
300
}
301
}
302
303
int
304
interface_status(struct interface_info *ifinfo)
305
{
306
char *ifname = ifinfo->name;
307
int ifsock = ifinfo->rfdesc;
308
struct ifreq ifr;
309
struct ifmediareq ifmr;
310
311
/* get interface flags */
312
memset(&ifr, 0, sizeof(ifr));
313
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
314
if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
315
cap_syslog(capsyslog, LOG_ERR, "ioctl(SIOCGIFFLAGS) on %s: %m",
316
ifname);
317
goto inactive;
318
}
319
320
/*
321
* if one of UP and RUNNING flags is dropped,
322
* the interface is not active.
323
*/
324
if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
325
goto inactive;
326
327
/* Next, check carrier on the interface, if possible */
328
if (ifinfo->noifmedia)
329
goto active;
330
memset(&ifmr, 0, sizeof(ifmr));
331
strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
332
if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
333
if (errno != EINVAL) {
334
cap_syslog(capsyslog, LOG_DEBUG,
335
"ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
336
ifinfo->noifmedia = 1;
337
goto active;
338
}
339
/*
340
* EINVAL (or ENOTTY) simply means that the interface
341
* does not support the SIOCGIFMEDIA ioctl. We regard it alive.
342
*/
343
ifinfo->noifmedia = 1;
344
goto active;
345
}
346
if (ifmr.ifm_status & IFM_AVALID) {
347
switch (ifmr.ifm_active & IFM_NMASK) {
348
case IFM_ETHER:
349
case IFM_IEEE80211:
350
if (ifmr.ifm_status & IFM_ACTIVE)
351
goto active;
352
else
353
goto inactive;
354
break;
355
default:
356
goto inactive;
357
}
358
}
359
inactive:
360
return (0);
361
active:
362
return (1);
363
}
364
365
void
366
add_timeout(time_t when_s, void (*where)(void *), void *what)
367
{
368
struct timespec when;
369
370
cur_time = time(NULL);
371
clock_gettime(CLOCK_MONOTONIC, &when);
372
when.tv_sec += when_s - cur_time;
373
add_timeout_timespec(when, where, what);
374
}
375
376
void
377
add_timeout_timespec(struct timespec when, void (*where)(void *), void *what)
378
{
379
struct timeout *t, *q;
380
381
/* See if this timeout supersedes an existing timeout. */
382
t = NULL;
383
for (q = timeouts; q; q = q->next) {
384
if (q->func == where && q->what == what) {
385
if (t)
386
t->next = q->next;
387
else
388
timeouts = q->next;
389
break;
390
}
391
t = q;
392
}
393
394
/* If we didn't supersede a timeout, allocate a timeout
395
structure now. */
396
if (!q) {
397
if (free_timeouts) {
398
q = free_timeouts;
399
free_timeouts = q->next;
400
q->func = where;
401
q->what = what;
402
} else {
403
q = malloc(sizeof(struct timeout));
404
if (!q)
405
error("Can't allocate timeout structure!");
406
q->func = where;
407
q->what = what;
408
}
409
}
410
411
q->when = when;
412
413
/* Now sort this timeout into the timeout list. */
414
415
/* Beginning of list? */
416
if (!timeouts || timespeccmp(&timeouts->when, &q->when, >)) {
417
q->next = timeouts;
418
timeouts = q;
419
return;
420
}
421
422
/* Middle of list? */
423
for (t = timeouts; t->next; t = t->next) {
424
if (timespeccmp(&t->next->when, &q->when, >)) {
425
q->next = t->next;
426
t->next = q;
427
return;
428
}
429
}
430
431
/* End of list. */
432
t->next = q;
433
q->next = NULL;
434
}
435
436
void
437
cancel_timeout(void (*where)(void *), void *what)
438
{
439
struct timeout *t, *q;
440
441
/* Look for this timeout on the list, and unlink it if we find it. */
442
t = NULL;
443
for (q = timeouts; q; q = q->next) {
444
if (q->func == where && q->what == what) {
445
if (t)
446
t->next = q->next;
447
else
448
timeouts = q->next;
449
break;
450
}
451
t = q;
452
}
453
454
/* If we found the timeout, put it on the free list. */
455
if (q) {
456
q->next = free_timeouts;
457
free_timeouts = q;
458
}
459
}
460
461
/* Add a protocol to the list of protocols... */
462
void
463
add_protocol(const char *name, int fd, void (*handler)(struct protocol *),
464
void *local)
465
{
466
struct protocol *p;
467
468
p = malloc(sizeof(*p));
469
if (!p)
470
error("can't allocate protocol struct for %s", name);
471
472
p->fd = fd;
473
p->handler = handler;
474
p->local = local;
475
p->next = protocols;
476
protocols = p;
477
}
478
479
void
480
remove_protocol(struct protocol *proto)
481
{
482
struct protocol *p, *prev;
483
484
for (p = protocols, prev = NULL; p != NULL; prev = p, p = p->next) {
485
if (p == proto) {
486
if (prev == NULL)
487
protocols = p->next;
488
else
489
prev->next = p->next;
490
free(p);
491
break;
492
}
493
}
494
}
495
496
int
497
interface_link_status(char *ifname)
498
{
499
struct ifmediareq ifmr;
500
int sock;
501
502
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
503
error("Can't create socket");
504
505
memset(&ifmr, 0, sizeof(ifmr));
506
strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
507
if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
508
/* EINVAL -> link state unknown. treat as active */
509
if (errno != EINVAL)
510
cap_syslog(capsyslog, LOG_DEBUG,
511
"ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
512
close(sock);
513
return (1);
514
}
515
close(sock);
516
517
if (ifmr.ifm_status & IFM_AVALID) {
518
switch (ifmr.ifm_active & IFM_NMASK) {
519
case IFM_ETHER:
520
case IFM_IEEE80211:
521
if (ifmr.ifm_status & IFM_ACTIVE)
522
return (1);
523
else
524
return (0);
525
}
526
}
527
return (1);
528
}
529
530
void
531
interface_set_mtu_unpriv(int privfd, u_int16_t mtu)
532
{
533
struct imsg_hdr hdr;
534
struct buf *buf;
535
int errs = 0;
536
537
hdr.code = IMSG_SET_INTERFACE_MTU;
538
hdr.len = sizeof(hdr) +
539
sizeof(u_int16_t);
540
541
if ((buf = buf_open(hdr.len)) == NULL)
542
error("buf_open: %m");
543
544
errs += buf_add(buf, &hdr, sizeof(hdr));
545
errs += buf_add(buf, &mtu, sizeof(mtu));
546
if (errs)
547
error("buf_add: %m");
548
549
if (buf_close(privfd, buf) == -1)
550
error("buf_close: %m");
551
}
552
553
void
554
interface_set_mtu_priv(char *ifname, u_int16_t mtu)
555
{
556
struct ifreq ifr;
557
int sock;
558
u_int16_t old_mtu;
559
560
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
561
error("Can't create socket");
562
563
memset(&ifr, 0, sizeof(ifr));
564
old_mtu = 0;
565
566
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
567
568
if (ioctl(sock, SIOCGIFMTU, (caddr_t)&ifr) == -1)
569
warning("SIOCGIFMTU failed (%s): %s", ifname,
570
strerror(errno));
571
else
572
old_mtu = ifr.ifr_mtu;
573
574
if (mtu != old_mtu) {
575
ifr.ifr_mtu = mtu;
576
577
if (ioctl(sock, SIOCSIFMTU, &ifr) == -1)
578
warning("SIOCSIFMTU failed (%d): %s", mtu,
579
strerror(errno));
580
}
581
582
close(sock);
583
}
584
585