annotate src/dhcp.c @ 5528:274d31419935 draft

privsep: don't leave a BOOTP process hanging around on SIGUSR1 When not in master mode.
author Roy Marples <roy@marples.name>
date Fri, 30 Oct 2020 16:11:44 +0000
parents 8c0e5dc34824
children 16610f4104a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4548
c7df03794de3 Add SPDX identifiers to all dhcpcd source files.
Yegor Yefremov <yegorslists@googlemail.com>
parents: 4536
diff changeset
1 /* SPDX-License-Identifier: BSD-2-Clause */
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2 /*
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3 * dhcpcd - DHCP client daemon
4922
555d7d1a4939 Welcome to 2020!
Roy Marples <roy@marples.name>
parents: 4904
diff changeset
4 * Copyright (c) 2006-2020 Roy Marples <roy@marples.name>
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
5 * All rights reserved
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
6
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
7 * Redistribution and use in source and binary forms, with or without
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
8 * modification, are permitted provided that the following conditions
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
9 * are met:
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
10 * 1. Redistributions of source code must retain the above copyright
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
15 *
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
26 * SUCH DAMAGE.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
27 */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
28
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
29 #include <sys/param.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
30 #include <sys/socket.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
31
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
32 #include <arpa/inet.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
33 #include <net/if.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
34 #include <net/route.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
35 #include <netinet/if_ether.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
36 #include <netinet/in_systm.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
37 #include <netinet/in.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
38 #include <netinet/ip.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
39 #define __FAVOR_BSD /* Nasty glibc hack so we can use BSD semantics for UDP */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
40 #include <netinet/udp.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
41 #undef __FAVOR_BSD
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
42
4784
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
43 #ifdef AF_LINK
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
44 # include <net/if_dl.h>
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
45 #endif
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
46
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
47 #include <assert.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
48 #include <ctype.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
49 #include <errno.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
50 #include <fcntl.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
51 #include <inttypes.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
52 #include <stdbool.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
53 #include <stddef.h>
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
54 #include <stdio.h>
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
55 #include <stdlib.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
56 #include <string.h>
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
57 #include <unistd.h>
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
58 #include <syslog.h>
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
59
5038
1e35e845790a eloop: define eloop queue numbers in common.h
Roy Marples <roy@marples.name>
parents: 5033
diff changeset
60 #define ELOOP_QUEUE ELOOP_DHCP
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
61 #include "config.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
62 #include "arp.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
63 #include "bpf.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
64 #include "common.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
65 #include "dhcp.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
66 #include "dhcpcd.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
67 #include "dhcp-common.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
68 #include "duid.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
69 #include "eloop.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
70 #include "if.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
71 #include "ipv4.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
72 #include "ipv4ll.h"
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
73 #include "logerr.h"
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
74 #include "privsep.h"
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
75 #include "sa.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
76 #include "script.h"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
77
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
78 #define DAD "Duplicate address detected"
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
79 #define DHCP_MIN_LEASE 20
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
80
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
81 #define IPV4A ADDRIPV4 | ARRAY
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
82 #define IPV4R ADDRIPV4 | REQUEST
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
83
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
84 /* We should define a maximum for the NAK exponential backoff */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
85 #define NAKOFF_MAX 60
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
86
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
87 /* Wait N nanoseconds between sending a RELEASE and dropping the address.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
88 * This gives the kernel enough time to actually send it. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
89 #define RELEASE_DELAY_S 0
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
90 #define RELEASE_DELAY_NS 10000000
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
91
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
92 #ifndef IPDEFTTL
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
93 #define IPDEFTTL 64 /* RFC1340 */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
94 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
95
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
96 /* Support older systems with different defines */
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
97 #if !defined(IP_RECVPKTINFO) && defined(IP_PKTINFO)
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
98 #define IP_RECVPKTINFO IP_PKTINFO
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
99 #endif
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
100
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
101 /* Assert the correct structure size for on wire */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
102 __CTASSERT(sizeof(struct ip) == 20);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
103 __CTASSERT(sizeof(struct udphdr) == 8);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
104 __CTASSERT(sizeof(struct bootp) == 300);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
105
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
106 struct dhcp_op {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
107 uint8_t value;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
108 const char *name;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
109 };
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
110
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
111 static const struct dhcp_op dhcp_ops[] = {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
112 { DHCP_DISCOVER, "DISCOVER" },
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
113 { DHCP_OFFER, "OFFER" },
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
114 { DHCP_REQUEST, "REQUEST" },
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
115 { DHCP_DECLINE, "DECLINE" },
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
116 { DHCP_ACK, "ACK" },
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
117 { DHCP_NAK, "NAK" },
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
118 { DHCP_RELEASE, "RELEASE" },
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
119 { DHCP_INFORM, "INFORM" },
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
120 { DHCP_FORCERENEW, "FORCERENEW"},
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
121 { 0, NULL }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
122 };
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
123
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
124 static const char * const dhcp_params[] = {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
125 "ip_address",
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
126 "subnet_cidr",
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
127 "network_number",
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
128 "filename",
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
129 "server_name",
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
130 NULL
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
131 };
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
132
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
133 static int dhcp_openbpf(struct interface *);
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
134 static void dhcp_start1(void *);
4585
745c62847a59 Fix compile on NetBSD with ARP but not ARPING
Roy Marples <roy@marples.name>
parents: 4584
diff changeset
135 #if defined(ARP) && (!defined(KERNEL_RFC5227) || defined(ARPING))
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
136 static void dhcp_arp_found(struct arp_state *, const struct arp_msg *);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
137 #endif
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
138 static void dhcp_handledhcp(struct interface *, struct bootp *, size_t,
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
139 const struct in_addr *);
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
140 static void dhcp_handleifudp(void *);
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
141 static int dhcp_initstate(struct interface *);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
142
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
143 void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
144 dhcp_printoptions(const struct dhcpcd_ctx *ctx,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
145 const struct dhcp_opt *opts, size_t opts_len)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
146 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
147 const char * const *p;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
148 size_t i, j;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
149 const struct dhcp_opt *opt, *opt2;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
150 int cols;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
151
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
152 for (p = dhcp_params; *p; p++)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
153 printf(" %s\n", *p);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
154
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
155 for (i = 0, opt = ctx->dhcp_opts; i < ctx->dhcp_opts_len; i++, opt++) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
156 for (j = 0, opt2 = opts; j < opts_len; j++, opt2++)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
157 if (opt->option == opt2->option)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
158 break;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
159 if (j == opts_len) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
160 cols = printf("%03d %s", opt->option, opt->var);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
161 dhcp_print_option_encoding(opt, cols);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
162 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
163 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
164 for (i = 0, opt = opts; i < opts_len; i++, opt++) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
165 cols = printf("%03d %s", opt->option, opt->var);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
166 dhcp_print_option_encoding(opt, cols);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
167 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
168 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
169
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
170 static const uint8_t *
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
171 get_option(struct dhcpcd_ctx *ctx,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
172 const struct bootp *bootp, size_t bootp_len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
173 unsigned int opt, size_t *opt_len)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
174 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
175 const uint8_t *p, *e;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
176 uint8_t l, o, ol, overl, *bp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
177 const uint8_t *op;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
178 size_t bl;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
179
4904
8a4470e92585 DHCP: Ensure we have a lease to extract options from.
Roy Marples <roy@marples.name>
parents: 4868
diff changeset
180 if (bootp == NULL || bootp_len < DHCP_MIN_LEN) {
8a4470e92585 DHCP: Ensure we have a lease to extract options from.
Roy Marples <roy@marples.name>
parents: 4868
diff changeset
181 errno = EINVAL;
8a4470e92585 DHCP: Ensure we have a lease to extract options from.
Roy Marples <roy@marples.name>
parents: 4868
diff changeset
182 return NULL;
8a4470e92585 DHCP: Ensure we have a lease to extract options from.
Roy Marples <roy@marples.name>
parents: 4868
diff changeset
183 }
8a4470e92585 DHCP: Ensure we have a lease to extract options from.
Roy Marples <roy@marples.name>
parents: 4868
diff changeset
184
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
185 /* Check we have the magic cookie */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
186 if (!IS_DHCP(bootp)) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
187 errno = ENOTSUP;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
188 return NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
189 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
190
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
191 p = bootp->vend + 4; /* options after the 4 byte cookie */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
192 e = (const uint8_t *)bootp + bootp_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
193 ol = o = overl = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
194 bp = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
195 op = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
196 bl = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
197 while (p < e) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
198 o = *p++;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
199 switch (o) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
200 case DHO_PAD:
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
201 /* No length to read */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
202 continue;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
203 case DHO_END:
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
204 if (overl & 1) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
205 /* bit 1 set means parse boot file */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
206 overl = (uint8_t)(overl & ~1);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
207 p = bootp->file;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
208 e = p + sizeof(bootp->file);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
209 } else if (overl & 2) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
210 /* bit 2 set means parse server name */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
211 overl = (uint8_t)(overl & ~2);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
212 p = bootp->sname;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
213 e = p + sizeof(bootp->sname);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
214 } else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
215 goto exit;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
216 /* No length to read */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
217 continue;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
218 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
219
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
220 /* Check we can read the length */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
221 if (p == e) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
222 errno = EINVAL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
223 return NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
224 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
225 l = *p++;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
226
4459
293fe50331c8 DHCP: Fix a potential 1 byte read overflow with DHO_OPTSOVERLOADED
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
227 /* Check we can read the option data, if present */
293fe50331c8 DHCP: Fix a potential 1 byte read overflow with DHO_OPTSOVERLOADED
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
228 if (p + l > e) {
293fe50331c8 DHCP: Fix a potential 1 byte read overflow with DHO_OPTSOVERLOADED
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
229 errno = EINVAL;
293fe50331c8 DHCP: Fix a potential 1 byte read overflow with DHO_OPTSOVERLOADED
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
230 return NULL;
293fe50331c8 DHCP: Fix a potential 1 byte read overflow with DHO_OPTSOVERLOADED
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
231 }
293fe50331c8 DHCP: Fix a potential 1 byte read overflow with DHO_OPTSOVERLOADED
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
232
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
233 if (o == DHO_OPTSOVERLOADED) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
234 /* Ensure we only get this option once by setting
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
235 * the last bit as well as the value.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
236 * This is valid because only the first two bits
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
237 * actually mean anything in RFC2132 Section 9.3 */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
238 if (l == 1 && !overl)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
239 overl = 0x80 | p[0];
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
240 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
241
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
242 if (o == opt) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
243 if (op) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
244 /* We must concatonate the options. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
245 if (bl + l > ctx->opt_buffer_len) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
246 size_t pos;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
247 uint8_t *nb;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
248
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
249 if (bp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
250 pos = (size_t)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
251 (bp - ctx->opt_buffer);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
252 else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
253 pos = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
254 nb = realloc(ctx->opt_buffer, bl + l);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
255 if (nb == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
256 return NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
257 ctx->opt_buffer = nb;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
258 ctx->opt_buffer_len = bl + l;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
259 bp = ctx->opt_buffer + pos;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
260 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
261 if (bp == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
262 bp = ctx->opt_buffer;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
263 memcpy(bp, op, ol);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
264 bp += ol;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
265 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
266 ol = l;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
267 op = p;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
268 bl += ol;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
269 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
270 p += l;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
271 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
272
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
273 exit:
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
274 if (opt_len)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
275 *opt_len = bl;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
276 if (bp) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
277 memcpy(bp, op, ol);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
278 return (const uint8_t *)ctx->opt_buffer;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
279 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
280 if (op)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
281 return op;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
282 errno = ENOENT;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
283 return NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
284 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
285
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
286 static int
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
287 get_option_addr(struct dhcpcd_ctx *ctx,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
288 struct in_addr *a, const struct bootp *bootp, size_t bootp_len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
289 uint8_t option)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
290 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
291 const uint8_t *p;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
292 size_t len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
293
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
294 p = get_option(ctx, bootp, bootp_len, option, &len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
295 if (!p || len < (ssize_t)sizeof(a->s_addr))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
296 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
297 memcpy(&a->s_addr, p, sizeof(a->s_addr));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
298 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
299 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
300
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
301 static int
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
302 get_option_uint32(struct dhcpcd_ctx *ctx,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
303 uint32_t *i, const struct bootp *bootp, size_t bootp_len, uint8_t option)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
304 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
305 const uint8_t *p;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
306 size_t len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
307 uint32_t d;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
308
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
309 p = get_option(ctx, bootp, bootp_len, option, &len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
310 if (!p || len < (ssize_t)sizeof(d))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
311 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
312 memcpy(&d, p, sizeof(d));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
313 if (i)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
314 *i = ntohl(d);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
315 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
316 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
317
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
318 static int
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
319 get_option_uint16(struct dhcpcd_ctx *ctx,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
320 uint16_t *i, const struct bootp *bootp, size_t bootp_len, uint8_t option)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
321 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
322 const uint8_t *p;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
323 size_t len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
324 uint16_t d;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
325
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
326 p = get_option(ctx, bootp, bootp_len, option, &len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
327 if (!p || len < (ssize_t)sizeof(d))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
328 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
329 memcpy(&d, p, sizeof(d));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
330 if (i)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
331 *i = ntohs(d);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
332 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
333 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
334
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
335 static int
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
336 get_option_uint8(struct dhcpcd_ctx *ctx,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
337 uint8_t *i, const struct bootp *bootp, size_t bootp_len, uint8_t option)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
338 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
339 const uint8_t *p;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
340 size_t len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
341
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
342 p = get_option(ctx, bootp, bootp_len, option, &len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
343 if (!p || len < (ssize_t)sizeof(*p))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
344 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
345 if (i)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
346 *i = *(p);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
347 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
348 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
349
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
350 ssize_t
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
351 print_rfc3442(FILE *fp, const uint8_t *data, size_t data_len)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
352 {
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
353 const uint8_t *p = data, *e;
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
354 size_t ocets;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
355 uint8_t cidr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
356 struct in_addr addr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
357
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
358 /* Minimum is 5 -first is CIDR and a router length of 4 */
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
359 if (data_len < 5) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
360 errno = EINVAL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
361 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
362 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
363
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
364 e = p + data_len;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
365 while (p < e) {
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
366 if (p != data) {
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
367 if (fputc(' ', fp) == EOF)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
368 return -1;
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
369 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
370 cidr = *p++;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
371 if (cidr > 32) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
372 errno = EINVAL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
373 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
374 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
375 ocets = (size_t)(cidr + 7) / NBBY;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
376 if (p + 4 + ocets > e) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
377 errno = ERANGE;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
378 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
379 }
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
380 /* If we have ocets then we have a destination and netmask */
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
381 addr.s_addr = 0;
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
382 if (ocets > 0) {
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
383 memcpy(&addr.s_addr, p, ocets);
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
384 p += ocets;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
385 }
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
386 if (fprintf(fp, "%s/%d", inet_ntoa(addr), cidr) == -1)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
387 return -1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
388
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
389 /* Finally, snag the router */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
390 memcpy(&addr.s_addr, p, 4);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
391 p += 4;
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
392 if (fprintf(fp, " %s", inet_ntoa(addr)) == -1)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
393 return -1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
394 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
395
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
396 if (fputc('\0', fp) == EOF)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
397 return -1;
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
398 return 1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
399 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
400
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
401 static int
4379
e9573d5cbf1e Replace route TAILQ macros with rbtree(3) from NetBSD.
Roy Marples <roy@marples.name>
parents: 4377
diff changeset
402 decode_rfc3442_rt(rb_tree_t *routes, struct interface *ifp,
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
403 const uint8_t *data, size_t dl, const struct bootp *bootp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
404 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
405 const uint8_t *p = data;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
406 const uint8_t *e;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
407 uint8_t cidr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
408 size_t ocets;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
409 struct rt *rt = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
410 struct in_addr dest, netmask, gateway;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
411 int n;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
412
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
413 /* Minimum is 5 -first is CIDR and a router length of 4 */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
414 if (dl < 5) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
415 errno = EINVAL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
416 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
417 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
418
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
419 n = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
420 e = p + dl;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
421 while (p < e) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
422 cidr = *p++;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
423 if (cidr > 32) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
424 errno = EINVAL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
425 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
426 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
427
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
428 ocets = (size_t)(cidr + 7) / NBBY;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
429 if (p + 4 + ocets > e) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
430 errno = ERANGE;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
431 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
432 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
433
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
434 if ((rt = rt_new(ifp)) == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
435 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
436
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
437 /* If we have ocets then we have a destination and netmask */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
438 dest.s_addr = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
439 if (ocets > 0) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
440 memcpy(&dest.s_addr, p, ocets);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
441 p += ocets;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
442 netmask.s_addr = htonl(~0U << (32 - cidr));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
443 } else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
444 netmask.s_addr = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
445
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
446 /* Finally, snag the router */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
447 memcpy(&gateway.s_addr, p, 4);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
448 p += 4;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
449
4362
2e028387b21c dhcp: if the netmask is all ones, it's a host route
Roy Marples <roy@marples.name>
parents: 4355
diff changeset
450 /* An on-link host route is normally set by having the
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
451 * gateway match the destination or assigned address */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
452 if (gateway.s_addr == dest.s_addr ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
453 (gateway.s_addr == bootp->yiaddr ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
454 gateway.s_addr == bootp->ciaddr))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
455 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
456 gateway.s_addr = INADDR_ANY;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
457 netmask.s_addr = INADDR_BROADCAST;
4362
2e028387b21c dhcp: if the netmask is all ones, it's a host route
Roy Marples <roy@marples.name>
parents: 4355
diff changeset
458 }
2e028387b21c dhcp: if the netmask is all ones, it's a host route
Roy Marples <roy@marples.name>
parents: 4355
diff changeset
459 if (netmask.s_addr == INADDR_BROADCAST)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
460 rt->rt_flags = RTF_HOST;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
461
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
462 sa_in_init(&rt->rt_dest, &dest);
4077
43b960e65bd5 dhcp: Fix classless static routes
Roy Marples <roy@marples.name>
parents: 4024
diff changeset
463 sa_in_init(&rt->rt_netmask, &netmask);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
464 sa_in_init(&rt->rt_gateway, &gateway);
4522
e2582c7c7e1d route: Use order from message / config
Roy Marples <roy@marples.name>
parents: 4511
diff changeset
465 if (rt_proto_add(routes, rt))
4524
578ac745c627 Fix a gcc warning about signed overflow
Roy Marples <roy@marples.name>
parents: 4522
diff changeset
466 n = 1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
467 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
468 return n;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
469 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
470
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
471 ssize_t
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
472 print_rfc3361(FILE *fp, const uint8_t *data, size_t dl)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
473 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
474 uint8_t enc;
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
475 char sip[NS_MAXDNAME];
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
476 struct in_addr addr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
477
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
478 if (dl < 2) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
479 errno = EINVAL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
480 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
481 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
482
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
483 enc = *data++;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
484 dl--;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
485 switch (enc) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
486 case 0:
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
487 if (decode_rfc1035(sip, sizeof(sip), data, dl) == -1)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
488 return -1;
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
489 if (efprintf(fp, "%s", sip) == -1)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
490 return -1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
491 break;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
492 case 1:
4866
6c1db207d549 DHCP: dl is always >0 at this point, so remove check.
Roy Marples <roy@marples.name>
parents: 4846
diff changeset
493 if (dl % 4 != 0) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
494 errno = EINVAL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
495 break;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
496 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
497 addr.s_addr = INADDR_BROADCAST;
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
498 for (;
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
499 dl != 0;
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
500 data += sizeof(addr.s_addr), dl -= sizeof(addr.s_addr))
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
501 {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
502 memcpy(&addr.s_addr, data, sizeof(addr.s_addr));
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
503 if (fprintf(fp, "%s", inet_ntoa(addr)) == -1)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
504 return -1;
5256
32665ffe1782 Avoid putting an extra space here.
Roy Marples <roy@marples.name>
parents: 5247
diff changeset
505 if (dl != sizeof(addr.s_addr)) {
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
506 if (fputc(' ', fp) == EOF)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
507 return -1;
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
508 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
509 }
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
510 if (fputc('\0', fp) == EOF)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
511 return -1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
512 break;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
513 default:
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
514 errno = EINVAL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
515 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
516 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
517
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
518 return 1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
519 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
520
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
521 static char *
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
522 get_option_string(struct dhcpcd_ctx *ctx,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
523 const struct bootp *bootp, size_t bootp_len, uint8_t option)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
524 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
525 size_t len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
526 const uint8_t *p;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
527 char *s;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
528
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
529 p = get_option(ctx, bootp, bootp_len, option, &len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
530 if (!p || len == 0 || *p == '\0')
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
531 return NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
532
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
533 s = malloc(sizeof(char) * (len + 1));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
534 if (s) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
535 memcpy(s, p, len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
536 s[len] = '\0';
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
537 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
538 return s;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
539 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
540
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
541 /* This calculates the netmask that we should use for static routes.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
542 * This IS different from the calculation used to calculate the netmask
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
543 * for an interface address. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
544 static uint32_t
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
545 route_netmask(uint32_t ip_in)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
546 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
547 /* used to be unsigned long - check if error */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
548 uint32_t p = ntohl(ip_in);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
549 uint32_t t;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
550
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
551 if (IN_CLASSA(p))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
552 t = ~IN_CLASSA_NET;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
553 else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
554 if (IN_CLASSB(p))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
555 t = ~IN_CLASSB_NET;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
556 else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
557 if (IN_CLASSC(p))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
558 t = ~IN_CLASSC_NET;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
559 else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
560 t = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
561 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
562 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
563
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
564 while (t & p)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
565 t >>= 1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
566
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
567 return (htonl(~t));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
568 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
569
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
570 /* We need to obey routing options.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
571 * If we have a CSR then we only use that.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
572 * Otherwise we add static routes and then routers. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
573 static int
4379
e9573d5cbf1e Replace route TAILQ macros with rbtree(3) from NetBSD.
Roy Marples <roy@marples.name>
parents: 4377
diff changeset
574 get_option_routes(rb_tree_t *routes, struct interface *ifp,
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
575 const struct bootp *bootp, size_t bootp_len)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
576 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
577 struct if_options *ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
578 const uint8_t *p;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
579 const uint8_t *e;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
580 struct rt *rt = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
581 struct in_addr dest, netmask, gateway;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
582 size_t len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
583 const char *csr = "";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
584 int n;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
585
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
586 /* If we have CSR's then we MUST use these only */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
587 if (!has_option_mask(ifo->nomask, DHO_CSR))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
588 p = get_option(ifp->ctx, bootp, bootp_len, DHO_CSR, &len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
589 else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
590 p = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
591 /* Check for crappy MS option */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
592 if (!p && !has_option_mask(ifo->nomask, DHO_MSCSR)) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
593 p = get_option(ifp->ctx, bootp, bootp_len, DHO_MSCSR, &len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
594 if (p)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
595 csr = "MS ";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
596 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
597 if (p && (n = decode_rfc3442_rt(routes, ifp, p, len, bootp)) != -1) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
598 const struct dhcp_state *state;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
599
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
600 state = D_CSTATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
601 if (!(ifo->options & DHCPCD_CSR_WARNED) &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
602 !(state->added & STATE_FAKE))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
603 {
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
604 logdebugx("%s: using %sClassless Static Routes",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
605 ifp->name, csr);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
606 ifo->options |= DHCPCD_CSR_WARNED;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
607 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
608 return n;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
609 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
610
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
611 n = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
612 /* OK, get our static routes first. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
613 if (!has_option_mask(ifo->nomask, DHO_STATICROUTE))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
614 p = get_option(ifp->ctx, bootp, bootp_len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
615 DHO_STATICROUTE, &len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
616 else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
617 p = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
618 /* RFC 2131 Section 5.8 states length MUST be in multiples of 8 */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
619 if (p && len % 8 == 0) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
620 e = p + len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
621 while (p < e) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
622 memcpy(&dest.s_addr, p, sizeof(dest.s_addr));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
623 p += 4;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
624 memcpy(&gateway.s_addr, p, sizeof(gateway.s_addr));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
625 p += 4;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
626 /* RFC 2131 Section 5.8 states default route is
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
627 * illegal */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
628 if (gateway.s_addr == INADDR_ANY)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
629 continue;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
630 if ((rt = rt_new(ifp)) == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
631 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
632
4362
2e028387b21c dhcp: if the netmask is all ones, it's a host route
Roy Marples <roy@marples.name>
parents: 4355
diff changeset
633 /* A on-link host route is normally set by having the
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
634 * gateway match the destination or assigned address */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
635 if (gateway.s_addr == dest.s_addr ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
636 (gateway.s_addr == bootp->yiaddr ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
637 gateway.s_addr == bootp->ciaddr))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
638 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
639 gateway.s_addr = INADDR_ANY;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
640 netmask.s_addr = INADDR_BROADCAST;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
641 } else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
642 netmask.s_addr = route_netmask(dest.s_addr);
4362
2e028387b21c dhcp: if the netmask is all ones, it's a host route
Roy Marples <roy@marples.name>
parents: 4355
diff changeset
643 if (netmask.s_addr == INADDR_BROADCAST)
2e028387b21c dhcp: if the netmask is all ones, it's a host route
Roy Marples <roy@marples.name>
parents: 4355
diff changeset
644 rt->rt_flags = RTF_HOST;
2e028387b21c dhcp: if the netmask is all ones, it's a host route
Roy Marples <roy@marples.name>
parents: 4355
diff changeset
645
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
646 sa_in_init(&rt->rt_dest, &dest);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
647 sa_in_init(&rt->rt_netmask, &netmask);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
648 sa_in_init(&rt->rt_gateway, &gateway);
4536
ce9e8fb0e6a3 route: Build list of static routes and routers correctly.
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
649 if (rt_proto_add(routes, rt))
4396
5bdea847921d route: Free route when failing to insert.
Roy Marples <roy@marples.name>
parents: 4379
diff changeset
650 n++;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
651 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
652 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
653
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
654 /* Now grab our routers */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
655 if (!has_option_mask(ifo->nomask, DHO_ROUTER))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
656 p = get_option(ifp->ctx, bootp, bootp_len, DHO_ROUTER, &len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
657 else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
658 p = NULL;
4536
ce9e8fb0e6a3 route: Build list of static routes and routers correctly.
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
659 if (p && len % 4 == 0) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
660 e = p + len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
661 dest.s_addr = INADDR_ANY;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
662 netmask.s_addr = INADDR_ANY;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
663 while (p < e) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
664 if ((rt = rt_new(ifp)) == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
665 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
666 memcpy(&gateway.s_addr, p, sizeof(gateway.s_addr));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
667 p += 4;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
668 sa_in_init(&rt->rt_dest, &dest);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
669 sa_in_init(&rt->rt_netmask, &netmask);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
670 sa_in_init(&rt->rt_gateway, &gateway);
4536
ce9e8fb0e6a3 route: Build list of static routes and routers correctly.
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
671 if (rt_proto_add(routes, rt))
4396
5bdea847921d route: Free route when failing to insert.
Roy Marples <roy@marples.name>
parents: 4379
diff changeset
672 n++;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
673 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
674 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
675
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
676 return n;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
677 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
678
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
679 uint16_t
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
680 dhcp_get_mtu(const struct interface *ifp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
681 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
682 const struct dhcp_state *state;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
683 uint16_t mtu;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
684
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
685 if (ifp->options->mtu)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
686 return (uint16_t)ifp->options->mtu;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
687 mtu = 0; /* bogus gcc warning */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
688 if ((state = D_CSTATE(ifp)) == NULL ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
689 has_option_mask(ifp->options->nomask, DHO_MTU) ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
690 get_option_uint16(ifp->ctx, &mtu,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
691 state->new, state->new_len, DHO_MTU) == -1)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
692 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
693 return mtu;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
694 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
695
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
696 /* Grab our routers from the DHCP message and apply any MTU value
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
697 * the message contains */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
698 int
4379
e9573d5cbf1e Replace route TAILQ macros with rbtree(3) from NetBSD.
Roy Marples <roy@marples.name>
parents: 4377
diff changeset
699 dhcp_get_routes(rb_tree_t *routes, struct interface *ifp)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
700 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
701 const struct dhcp_state *state;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
702
4108
84bdb647122c dhcp: return routes when renewing
Roy Marples <roy@marples.name>
parents: 4104
diff changeset
703 if ((state = D_CSTATE(ifp)) == NULL || !(state->added & STATE_ADDED))
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
704 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
705 return get_option_routes(routes, ifp, state->new, state->new_len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
706 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
707
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
708 /* Assumes DHCP options */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
709 static int
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
710 dhcp_message_add_addr(struct bootp *bootp,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
711 uint8_t type, struct in_addr addr)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
712 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
713 uint8_t *p;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
714 size_t len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
715
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
716 p = bootp->vend;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
717 while (*p != DHO_END) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
718 p++;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
719 p += *p + 1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
720 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
721
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
722 len = (size_t)(p - bootp->vend);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
723 if (len + 6 > sizeof(bootp->vend)) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
724 errno = ENOMEM;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
725 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
726 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
727
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
728 *p++ = type;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
729 *p++ = 4;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
730 memcpy(p, &addr.s_addr, 4);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
731 p += 4;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
732 *p = DHO_END;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
733 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
734 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
735
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
736 static ssize_t
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
737 make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
738 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
739 struct bootp *bootp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
740 uint8_t *lp, *p, *e;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
741 uint8_t *n_params = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
742 uint32_t ul;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
743 uint16_t sz;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
744 size_t len, i;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
745 const struct dhcp_opt *opt;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
746 struct if_options *ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
747 const struct dhcp_state *state = D_CSTATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
748 const struct dhcp_lease *lease = &state->lease;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
749 char hbuf[HOSTNAME_MAX_LEN + 1];
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
750 const char *hostname;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
751 const struct vivco *vivco;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
752 int mtu;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
753 #ifdef AUTH
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
754 uint8_t *auth, auth_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
755 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
756
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
757 if ((mtu = if_getmtu(ifp)) == -1)
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
758 logerr("%s: if_getmtu", ifp->name);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
759 else if (mtu < MTU_MIN) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
760 if (if_setmtu(ifp, MTU_MIN) == -1)
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
761 logerr("%s: if_setmtu", ifp->name);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
762 mtu = MTU_MIN;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
763 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
764
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
765 if (ifo->options & DHCPCD_BOOTP)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
766 bootp = calloc(1, sizeof (*bootp));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
767 else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
768 /* Make the maximal message we could send */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
769 bootp = calloc(1, (size_t)(mtu - IP_UDP_SIZE));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
770
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
771 if (bootp == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
772 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
773 *bootpm = bootp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
774
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
775 if (state->addr != NULL &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
776 (type == DHCP_INFORM || type == DHCP_RELEASE ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
777 (type == DHCP_REQUEST &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
778 state->addr->mask.s_addr == lease->mask.s_addr &&
4188
23ff91710a01 dhcp: Don't set ciaddr or unicast if the address we have is faked.
Roy Marples <roy@marples.name>
parents: 4187
diff changeset
779 (state->new == NULL || IS_DHCP(state->new)) &&
5424
28a650d93729 DHCP: Mark the state as expired on lase lease extension
Roy Marples <roy@marples.name>
parents: 5299
diff changeset
780 !(state->added & (STATE_FAKE | STATE_EXPIRED)))))
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
781 bootp->ciaddr = state->addr->addr.s_addr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
782
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
783 bootp->op = BOOTREQUEST;
5166
a70f6ddefe3c Rename ifp->family -> ifp->hwtype so it's less confusing
Roy Marples <roy@marples.name>
parents: 5160
diff changeset
784 bootp->htype = (uint8_t)ifp->hwtype;
4958
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
785 if (ifp->hwlen != 0 && ifp->hwlen < sizeof(bootp->chaddr)) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
786 bootp->hlen = (uint8_t)ifp->hwlen;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
787 memcpy(&bootp->chaddr, &ifp->hwaddr, ifp->hwlen);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
788 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
789
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
790 if (ifo->options & DHCPCD_BROADCAST &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
791 bootp->ciaddr == 0 &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
792 type != DHCP_DECLINE &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
793 type != DHCP_RELEASE)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
794 bootp->flags = htons(BROADCAST_FLAG);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
795
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
796 if (type != DHCP_DECLINE && type != DHCP_RELEASE) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
797 struct timespec tv;
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
798 unsigned long long secs;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
799
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
800 clock_gettime(CLOCK_MONOTONIC, &tv);
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
801 secs = eloop_timespec_diff(&tv, &state->started, NULL);
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
802 if (secs > UINT16_MAX)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
803 bootp->secs = htons((uint16_t)UINT16_MAX);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
804 else
4924
7c1a365b1e2d eloop: reduce timers rather than calculating expiry
Roy Marples <roy@marples.name>
parents: 4923
diff changeset
805 bootp->secs = htons((uint16_t)secs);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
806 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
807
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
808 bootp->xid = htonl(state->xid);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
809
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
810 if (ifo->options & DHCPCD_BOOTP)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
811 return sizeof(*bootp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
812
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
813 p = bootp->vend;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
814 e = (uint8_t *)bootp + (mtu - IP_UDP_SIZE) - 1; /* -1 for DHO_END */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
815
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
816 ul = htonl(MAGIC_COOKIE);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
817 memcpy(p, &ul, sizeof(ul));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
818 p += sizeof(ul);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
819
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
820 #define AREA_LEFT (size_t)(e - p)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
821 #define AREA_FIT(s) if ((s) > AREA_LEFT) goto toobig
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
822 #define AREA_CHECK(s) if ((s) + 2UL > AREA_LEFT) goto toobig
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
823 #define PUT_ADDR(o, a) do { \
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
824 AREA_CHECK(4); \
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
825 *p++ = (o); \
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
826 *p++ = 4; \
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
827 memcpy(p, &(a)->s_addr, 4); \
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
828 p += 4; \
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
829 } while (0 /* CONSTCOND */)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
830
4958
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
831 /* Options are listed in numerical order as per RFC 7844 Section 3.1
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
832 * XXX: They should be randomised. */
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
833
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
834 bool putip = false;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
835 if (lease->addr.s_addr && lease->cookie == htonl(MAGIC_COOKIE)) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
836 if (type == DHCP_DECLINE ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
837 (type == DHCP_REQUEST &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
838 (state->addr == NULL ||
5426
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
839 state->added & (STATE_FAKE | STATE_EXPIRED) ||
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
840 lease->addr.s_addr != state->addr->addr.s_addr)))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
841 {
4958
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
842 putip = true;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
843 PUT_ADDR(DHO_IPADDRESS, &lease->addr);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
844 }
4958
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
845 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
846
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
847 AREA_CHECK(3);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
848 *p++ = DHO_MESSAGETYPE;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
849 *p++ = 1;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
850 *p++ = type;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
851
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
852 if (lease->addr.s_addr && lease->cookie == htonl(MAGIC_COOKIE)) {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
853 if (type == DHCP_RELEASE || putip) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
854 if (lease->server.s_addr)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
855 PUT_ADDR(DHO_SERVERID, &lease->server);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
856 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
857 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
858
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
859 if (type == DHCP_DECLINE) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
860 len = strlen(DAD);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
861 if (len > AREA_LEFT) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
862 *p++ = DHO_MESSAGE;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
863 *p++ = (uint8_t)len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
864 memcpy(p, DAD, len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
865 p += len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
866 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
867 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
868
4958
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
869 #define DHCP_DIR(type) ((type) == DHCP_DISCOVER || (type) == DHCP_INFORM || \
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
870 (type) == DHCP_REQUEST)
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
871
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
872 if (DHCP_DIR(type)) {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
873 /* vendor is already encoded correctly, so just add it */
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
874 if (ifo->vendor[0]) {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
875 AREA_CHECK(ifo->vendor[0]);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
876 *p++ = DHO_VENDOR;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
877 memcpy(p, ifo->vendor, (size_t)ifo->vendor[0] + 1);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
878 p += ifo->vendor[0] + 1;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
879 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
880 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
881
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
882 if (type == DHCP_DISCOVER && ifo->options & DHCPCD_REQUEST)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
883 PUT_ADDR(DHO_IPADDRESS, &ifo->req_addr);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
884
4958
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
885 if (DHCP_DIR(type)) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
886 if (type != DHCP_INFORM) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
887 if (ifo->leasetime != 0) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
888 AREA_CHECK(4);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
889 *p++ = DHO_LEASETIME;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
890 *p++ = 4;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
891 ul = htonl(ifo->leasetime);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
892 memcpy(p, &ul, 4);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
893 p += 4;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
894 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
895 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
896
4958
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
897 AREA_CHECK(0);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
898 *p++ = DHO_PARAMETERREQUESTLIST;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
899 n_params = p;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
900 *p++ = 0;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
901 for (i = 0, opt = ifp->ctx->dhcp_opts;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
902 i < ifp->ctx->dhcp_opts_len;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
903 i++, opt++)
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
904 {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
905 if (!DHC_REQOPT(opt, ifo->requestmask, ifo->nomask))
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
906 continue;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
907 if (type == DHCP_INFORM &&
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
908 (opt->option == DHO_RENEWALTIME ||
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
909 opt->option == DHO_REBINDTIME))
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
910 continue;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
911 AREA_FIT(1);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
912 *p++ = (uint8_t)opt->option;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
913 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
914 for (i = 0, opt = ifo->dhcp_override;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
915 i < ifo->dhcp_override_len;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
916 i++, opt++)
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
917 {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
918 /* Check if added above */
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
919 for (lp = n_params + 1; lp < p; lp++)
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
920 if (*lp == (uint8_t)opt->option)
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
921 break;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
922 if (lp < p)
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
923 continue;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
924 if (!DHC_REQOPT(opt, ifo->requestmask, ifo->nomask))
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
925 continue;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
926 if (type == DHCP_INFORM &&
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
927 (opt->option == DHO_RENEWALTIME ||
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
928 opt->option == DHO_REBINDTIME))
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
929 continue;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
930 AREA_FIT(1);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
931 *p++ = (uint8_t)opt->option;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
932 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
933 *n_params = (uint8_t)(p - n_params - 1);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
934
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
935 if (mtu != -1 &&
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
936 !(has_option_mask(ifo->nomask, DHO_MAXMESSAGESIZE)))
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
937 {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
938 AREA_CHECK(2);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
939 *p++ = DHO_MAXMESSAGESIZE;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
940 *p++ = 2;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
941 sz = htons((uint16_t)(mtu - IP_UDP_SIZE));
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
942 memcpy(p, &sz, 2);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
943 p += 2;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
944 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
945
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
946 if (ifo->userclass[0] &&
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
947 !has_option_mask(ifo->nomask, DHO_USERCLASS))
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
948 {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
949 AREA_CHECK(ifo->userclass[0]);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
950 *p++ = DHO_USERCLASS;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
951 memcpy(p, ifo->userclass,
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
952 (size_t)ifo->userclass[0] + 1);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
953 p += ifo->userclass[0] + 1;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
954 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
955 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
956
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
957 if (state->clientid) {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
958 AREA_CHECK(state->clientid[0]);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
959 *p++ = DHO_CLIENTID;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
960 memcpy(p, state->clientid, (size_t)state->clientid[0] + 1);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
961 p += state->clientid[0] + 1;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
962 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
963
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
964 if (DHCP_DIR(type) &&
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
965 !has_option_mask(ifo->nomask, DHO_VENDORCLASSID) &&
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
966 ifo->vendorclassid[0])
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
967 {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
968 AREA_CHECK(ifo->vendorclassid[0]);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
969 *p++ = DHO_VENDORCLASSID;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
970 memcpy(p, ifo->vendorclassid, (size_t)ifo->vendorclassid[0]+1);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
971 p += ifo->vendorclassid[0] + 1;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
972 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
973
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
974 if (type == DHCP_DISCOVER &&
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
975 !(ifp->ctx->options & DHCPCD_TEST) &&
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
976 DHC_REQ(ifo->requestmask, ifo->nomask, DHO_RAPIDCOMMIT))
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
977 {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
978 /* RFC 4039 Section 3 */
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
979 AREA_CHECK(0);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
980 *p++ = DHO_RAPIDCOMMIT;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
981 *p++ = 0;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
982 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
983
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
984 if (DHCP_DIR(type)) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
985 hostname = dhcp_get_hostname(hbuf, sizeof(hbuf), ifo);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
986
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
987 /*
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
988 * RFC4702 3.1 States that if we send the Client FQDN option
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
989 * then we MUST NOT also send the Host Name option.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
990 * Technically we could, but that is not RFC conformant and
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
991 * also seems to break some DHCP server implemetations such as
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
992 * Windows. On the other hand, ISC dhcpd is just as non RFC
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
993 * conformant by not accepting a partially qualified FQDN.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
994 */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
995 if (ifo->fqdn != FQDN_DISABLE) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
996 /* IETF DHC-FQDN option (81), RFC4702 */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
997 i = 3;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
998 if (hostname)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
999 i += encode_rfc1035(hostname, NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1000 AREA_CHECK(i);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1001 *p++ = DHO_FQDN;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1002 *p++ = (uint8_t)i;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1003 /*
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1004 * Flags: 0000NEOS
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1005 * S: 1 => Client requests Server to update
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1006 * a RR in DNS as well as PTR
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1007 * O: 1 => Server indicates to client that
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1008 * DNS has been updated
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1009 * E: 1 => Name data is DNS format
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1010 * N: 1 => Client requests Server to not
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1011 * update DNS
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1012 */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1013 if (hostname)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1014 *p++ = (uint8_t)((ifo->fqdn & 0x09) | 0x04);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1015 else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1016 *p++ = (FQDN_NONE & 0x09) | 0x04;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1017 *p++ = 0; /* from server for PTR RR */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1018 *p++ = 0; /* from server for A RR if S=1 */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1019 if (hostname) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1020 i = encode_rfc1035(hostname, p);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1021 p += i;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1022 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1023 } else if (ifo->options & DHCPCD_HOSTNAME && hostname) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1024 len = strlen(hostname);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1025 AREA_CHECK(len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1026 *p++ = DHO_HOSTNAME;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1027 *p++ = (uint8_t)len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1028 memcpy(p, hostname, len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1029 p += len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1030 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1031 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1032
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1033 #ifdef AUTH
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1034 auth = NULL; /* appease GCC */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1035 auth_len = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1036 if (ifo->auth.options & DHCPCD_AUTH_SEND) {
5299
b7e676ac73c1 privsep: Access the RDM monotic file via IPC
Roy Marples <roy@marples.name>
parents: 5294
diff changeset
1037 ssize_t alen = dhcp_auth_encode(ifp->ctx, &ifo->auth,
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1038 state->auth.token,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1039 NULL, 0, 4, type, NULL, 0);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1040 if (alen != -1 && alen > UINT8_MAX) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1041 errno = ERANGE;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1042 alen = -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1043 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1044 if (alen == -1)
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
1045 logerr("%s: dhcp_auth_encode", ifp->name);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1046 else if (alen != 0) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1047 auth_len = (uint8_t)alen;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1048 AREA_CHECK(auth_len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1049 *p++ = DHO_AUTHENTICATION;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1050 *p++ = auth_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1051 auth = p;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1052 p += auth_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1053 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1054 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1055 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1056
4958
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1057 /* RFC 2563 Auto Configure */
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1058 if (type == DHCP_DISCOVER && ifo->options & DHCPCD_IPV4LL &&
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1059 !(has_option_mask(ifo->nomask, DHO_AUTOCONFIGURE)))
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1060 {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1061 AREA_CHECK(1);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1062 *p++ = DHO_AUTOCONFIGURE;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1063 *p++ = 1;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1064 *p++ = 1;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1065 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1066
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1067 if (DHCP_DIR(type)) {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1068 if (ifo->mudurl[0]) {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1069 AREA_CHECK(ifo->mudurl[0]);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1070 *p++ = DHO_MUDURL;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1071 memcpy(p, ifo->mudurl, (size_t)ifo->mudurl[0] + 1);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1072 p += ifo->mudurl[0] + 1;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1073 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1074
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1075 if (ifo->vivco_len &&
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1076 !has_option_mask(ifo->nomask, DHO_VIVCO))
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1077 {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1078 AREA_CHECK(sizeof(ul));
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1079 *p++ = DHO_VIVCO;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1080 lp = p++;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1081 *lp = sizeof(ul);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1082 ul = htonl(ifo->vivco_en);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1083 memcpy(p, &ul, sizeof(ul));
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1084 p += sizeof(ul);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1085 for (i = 0, vivco = ifo->vivco;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1086 i < ifo->vivco_len;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1087 i++, vivco++)
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1088 {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1089 AREA_FIT(vivco->len);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1090 if (vivco->len + 2 + *lp > 255) {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1091 logerrx("%s: VIVCO option too big",
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1092 ifp->name);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1093 free(bootp);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1094 return -1;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1095 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1096 *p++ = (uint8_t)vivco->len;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1097 memcpy(p, vivco->data, vivco->len);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1098 p += vivco->len;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1099 *lp = (uint8_t)(*lp + vivco->len + 1);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1100 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1101 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1102
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1103 #ifdef AUTH
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1104 if ((ifo->auth.options & DHCPCD_AUTH_SENDREQUIRE) !=
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1105 DHCPCD_AUTH_SENDREQUIRE &&
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1106 !has_option_mask(ifo->nomask, DHO_FORCERENEW_NONCE))
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1107 {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1108 /* We support HMAC-MD5 */
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1109 AREA_CHECK(1);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1110 *p++ = DHO_FORCERENEW_NONCE;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1111 *p++ = 1;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1112 *p++ = AUTH_ALG_HMAC_MD5;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1113 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1114 #endif
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1115 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
1116
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1117 *p++ = DHO_END;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1118 len = (size_t)(p - (uint8_t *)bootp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1119
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1120 /* Pad out to the BOOTP message length.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1121 * Even if we send a DHCP packet with a variable length vendor area,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1122 * some servers / relay agents don't like packets smaller than
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1123 * a BOOTP message which is fine because that's stipulated
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1124 * in RFC1542 section 2.1. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1125 while (len < sizeof(*bootp)) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1126 *p++ = DHO_PAD;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1127 len++;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1128 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1129
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1130 #ifdef AUTH
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1131 if (ifo->auth.options & DHCPCD_AUTH_SEND && auth_len != 0)
5299
b7e676ac73c1 privsep: Access the RDM monotic file via IPC
Roy Marples <roy@marples.name>
parents: 5294
diff changeset
1132 dhcp_auth_encode(ifp->ctx, &ifo->auth, state->auth.token,
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1133 (uint8_t *)bootp, len, 4, type, auth, auth_len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1134 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1135
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1136 return (ssize_t)len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1137
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1138 toobig:
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
1139 logerrx("%s: DHCP message too big", ifp->name);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1140 free(bootp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1141 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1142 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1143
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1144 static size_t
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1145 read_lease(struct interface *ifp, struct bootp **bootp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1146 {
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1147 union {
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1148 struct bootp bootp;
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1149 uint8_t buf[FRAMELEN_MAX];
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1150 } buf;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1151 struct dhcp_state *state = D_STATE(ifp);
5208
6e53055c9989 Fix compile warnings with prior.
Roy Marples <roy@marples.name>
parents: 5207
diff changeset
1152 ssize_t sbytes;
6e53055c9989 Fix compile warnings with prior.
Roy Marples <roy@marples.name>
parents: 5207
diff changeset
1153 size_t bytes;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1154 uint8_t type;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1155 #ifdef AUTH
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1156 const uint8_t *auth;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1157 size_t auth_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1158 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1159
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1160 /* Safety */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1161 *bootp = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1162
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1163 if (state->leasefile[0] == '\0') {
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1164 logdebugx("reading standard input");
5208
6e53055c9989 Fix compile warnings with prior.
Roy Marples <roy@marples.name>
parents: 5207
diff changeset
1165 sbytes = read(fileno(stdin), buf.buf, sizeof(buf.buf));
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1166 } else {
5494
0fbde4769bbe Don't log backticks.
Roy Marples <roy@marples.name>
parents: 5489
diff changeset
1167 logdebugx("%s: reading lease: %s",
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1168 ifp->name, state->leasefile);
5208
6e53055c9989 Fix compile warnings with prior.
Roy Marples <roy@marples.name>
parents: 5207
diff changeset
1169 sbytes = dhcp_readfile(ifp->ctx, state->leasefile,
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1170 buf.buf, sizeof(buf.buf));
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1171 }
5208
6e53055c9989 Fix compile warnings with prior.
Roy Marples <roy@marples.name>
parents: 5207
diff changeset
1172 if (sbytes == -1) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1173 if (errno != ENOENT)
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1174 logerr("%s: %s", ifp->name, state->leasefile);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1175 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1176 }
5208
6e53055c9989 Fix compile warnings with prior.
Roy Marples <roy@marples.name>
parents: 5207
diff changeset
1177 bytes = (size_t)sbytes;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1178
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1179 /* Ensure the packet is at lease BOOTP sized
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1180 * with a vendor area of 4 octets
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1181 * (it should be more, and our read packet enforces this so this
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1182 * code should not be needed, but of course people could
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1183 * scribble whatever in the stored lease file. */
5208
6e53055c9989 Fix compile warnings with prior.
Roy Marples <roy@marples.name>
parents: 5207
diff changeset
1184 if (bytes < DHCP_MIN_LEN) {
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
1185 logerrx("%s: %s: truncated lease", ifp->name, __func__);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1186 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1187 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1188
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1189 if (ifp->ctx->options & DHCPCD_DUMPLEASE)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1190 goto out;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1191
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1192 /* We may have found a BOOTP server */
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1193 if (get_option_uint8(ifp->ctx, &type, &buf.bootp, bytes,
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1194 DHO_MESSAGETYPE) == -1)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1195 type = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1196
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1197 #ifdef AUTH
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1198 /* Authenticate the message */
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1199 auth = get_option(ifp->ctx, &buf.bootp, bytes,
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1200 DHO_AUTHENTICATION, &auth_len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1201 if (auth) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1202 if (dhcp_auth_validate(&state->auth, &ifp->options->auth,
5211
cb2fa76d03bc Remove unused variable.
Roy Marples <roy@marples.name>
parents: 5208
diff changeset
1203 &buf.bootp, bytes, 4, type, auth, auth_len) == NULL)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1204 {
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
1205 logerr("%s: authentication failed", ifp->name);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1206 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1207 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1208 if (state->auth.token)
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
1209 logdebugx("%s: validated using 0x%08" PRIu32,
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1210 ifp->name, state->auth.token->secretid);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1211 else
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
1212 logdebugx("%s: accepted reconfigure key", ifp->name);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1213 } else if ((ifp->options->auth.options & DHCPCD_AUTH_SENDREQUIRE) ==
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1214 DHCPCD_AUTH_SENDREQUIRE)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1215 {
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
1216 logerrx("%s: authentication now required", ifp->name);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1217 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1218 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1219 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1220
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1221 out:
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1222 *bootp = malloc(bytes);
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1223 if (*bootp == NULL) {
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1224 logerr(__func__);
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1225 return 0;
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1226 }
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
1227 memcpy(*bootp, buf.buf, bytes);
5208
6e53055c9989 Fix compile warnings with prior.
Roy Marples <roy@marples.name>
parents: 5207
diff changeset
1228 return bytes;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1229 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1230
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1231 static const struct dhcp_opt *
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1232 dhcp_getoverride(const struct if_options *ifo, unsigned int o)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1233 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1234 size_t i;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1235 const struct dhcp_opt *opt;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1236
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1237 for (i = 0, opt = ifo->dhcp_override;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1238 i < ifo->dhcp_override_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1239 i++, opt++)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1240 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1241 if (opt->option == o)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1242 return opt;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1243 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1244 return NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1245 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1246
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1247 static const uint8_t *
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1248 dhcp_getoption(struct dhcpcd_ctx *ctx,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1249 size_t *os, unsigned int *code, size_t *len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1250 const uint8_t *od, size_t ol, struct dhcp_opt **oopt)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1251 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1252 size_t i;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1253 struct dhcp_opt *opt;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1254
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1255 if (od) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1256 if (ol < 2) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1257 errno = EINVAL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1258 return NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1259 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1260 *os = 2; /* code + len */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1261 *code = (unsigned int)*od++;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1262 *len = (size_t)*od++;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1263 if (*len > ol - *os) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1264 errno = ERANGE;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1265 return NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1266 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1267 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1268
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1269 *oopt = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1270 for (i = 0, opt = ctx->dhcp_opts; i < ctx->dhcp_opts_len; i++, opt++) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1271 if (opt->option == *code) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1272 *oopt = opt;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1273 break;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1274 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1275 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1276
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1277 return od;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1278 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1279
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1280 ssize_t
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1281 dhcp_env(FILE *fenv, const char *prefix, const struct interface *ifp,
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1282 const struct bootp *bootp, size_t bootp_len)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1283 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1284 const struct if_options *ifo;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1285 const uint8_t *p;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1286 struct in_addr addr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1287 struct in_addr net;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1288 struct in_addr brd;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1289 struct dhcp_opt *opt, *vo;
4538
8e383f84ebd0 Remove now unused variables.
Roy Marples <roy@marples.name>
parents: 4537
diff changeset
1290 size_t i, pl;
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1291 char safe[(BOOTP_FILE_LEN * 4) + 1];
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1292 uint8_t overl = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1293 uint32_t en;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1294
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1295 ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1296 if (get_option_uint8(ifp->ctx, &overl, bootp, bootp_len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1297 DHO_OPTSOVERLOADED) == -1)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1298 overl = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1299
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1300 if (bootp->yiaddr || bootp->ciaddr) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1301 /* Set some useful variables that we derive from the DHCP
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1302 * message but are not necessarily in the options */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1303 addr.s_addr = bootp->yiaddr ? bootp->yiaddr : bootp->ciaddr;
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1304 if (efprintf(fenv, "%s_ip_address=%s",
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1305 prefix, inet_ntoa(addr)) == -1)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1306 return -1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1307 if (get_option_addr(ifp->ctx, &net,
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1308 bootp, bootp_len, DHO_SUBNETMASK) == -1) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1309 net.s_addr = ipv4_getnetmask(addr.s_addr);
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1310 if (efprintf(fenv, "%s_subnet_mask=%s",
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1311 prefix, inet_ntoa(net)) == -1)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1312 return -1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1313 }
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1314 if (efprintf(fenv, "%s_subnet_cidr=%d",
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1315 prefix, inet_ntocidr(net))== -1)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1316 return -1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1317 if (get_option_addr(ifp->ctx, &brd,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1318 bootp, bootp_len, DHO_BROADCAST) == -1)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1319 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1320 brd.s_addr = addr.s_addr | ~net.s_addr;
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1321 if (efprintf(fenv, "%s_broadcast_address=%s",
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1322 prefix, inet_ntoa(brd)) == -1)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1323 return -1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1324 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1325 addr.s_addr = bootp->yiaddr & net.s_addr;
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1326 if (efprintf(fenv, "%s_network_number=%s",
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1327 prefix, inet_ntoa(addr)) == -1)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1328 return -1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1329 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1330
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1331 if (*bootp->file && !(overl & 1)) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1332 print_string(safe, sizeof(safe), OT_STRING,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1333 bootp->file, sizeof(bootp->file));
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1334 if (efprintf(fenv, "%s_filename=%s", prefix, safe) == -1)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1335 return -1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1336 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1337 if (*bootp->sname && !(overl & 2)) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1338 print_string(safe, sizeof(safe), OT_STRING | OT_DOMAIN,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1339 bootp->sname, sizeof(bootp->sname));
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1340 if (efprintf(fenv, "%s_server_name=%s", prefix, safe) == -1)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1341 return -1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1342 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1343
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1344 /* Zero our indexes */
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1345 for (i = 0, opt = ifp->ctx->dhcp_opts;
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1346 i < ifp->ctx->dhcp_opts_len;
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1347 i++, opt++)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1348 dhcp_zero_index(opt);
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1349 for (i = 0, opt = ifp->options->dhcp_override;
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1350 i < ifp->options->dhcp_override_len;
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1351 i++, opt++)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1352 dhcp_zero_index(opt);
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1353 for (i = 0, opt = ifp->ctx->vivso;
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1354 i < ifp->ctx->vivso_len;
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1355 i++, opt++)
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1356 dhcp_zero_index(opt);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1357
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1358 for (i = 0, opt = ifp->ctx->dhcp_opts;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1359 i < ifp->ctx->dhcp_opts_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1360 i++, opt++)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1361 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1362 if (has_option_mask(ifo->nomask, opt->option))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1363 continue;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1364 if (dhcp_getoverride(ifo, opt->option))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1365 continue;
4166
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1366 p = get_option(ifp->ctx, bootp, bootp_len, opt->option, &pl);
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1367 if (p == NULL)
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1368 continue;
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1369 dhcp_envoption(ifp->ctx, fenv, prefix, ifp->name,
4166
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1370 opt, dhcp_getoption, p, pl);
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1371
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1372 if (opt->option != DHO_VIVSO || pl <= (int)sizeof(uint32_t))
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1373 continue;
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1374 memcpy(&en, p, sizeof(en));
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1375 en = ntohl(en);
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1376 vo = vivso_find(en, ifp);
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1377 if (vo == NULL)
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1378 continue;
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1379 /* Skip over en + total size */
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1380 p += sizeof(en) + 1;
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1381 pl -= sizeof(en) + 1;
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1382 dhcp_envoption(ifp->ctx, fenv, prefix, ifp->name,
4166
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1383 vo, dhcp_getoption, p, pl);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1384 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1385
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1386 for (i = 0, opt = ifo->dhcp_override;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1387 i < ifo->dhcp_override_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1388 i++, opt++)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1389 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1390 if (has_option_mask(ifo->nomask, opt->option))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1391 continue;
4166
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1392 p = get_option(ifp->ctx, bootp, bootp_len, opt->option, &pl);
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1393 if (p == NULL)
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1394 continue;
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1395 dhcp_envoption(ifp->ctx, fenv, prefix, ifp->name,
4166
3d5e01232bf7 Make this more readable.
Roy Marples <roy@marples.name>
parents: 4163
diff changeset
1396 opt, dhcp_getoption, p, pl);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1397 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1398
4535
f2ddefe6c69e script: Write variables to a FILE
Roy Marples <roy@marples.name>
parents: 4524
diff changeset
1399 return 1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1400 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1401
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1402 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1403 get_lease(struct interface *ifp,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1404 struct dhcp_lease *lease, const struct bootp *bootp, size_t len)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1405 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1406 struct dhcpcd_ctx *ctx;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1407
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1408 assert(bootp != NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1409
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1410 memcpy(&lease->cookie, bootp->vend, sizeof(lease->cookie));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1411 /* BOOTP does not set yiaddr for replies when ciaddr is set. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1412 lease->addr.s_addr = bootp->yiaddr ? bootp->yiaddr : bootp->ciaddr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1413 ctx = ifp->ctx;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1414 if (ifp->options->options & (DHCPCD_STATIC | DHCPCD_INFORM)) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1415 if (ifp->options->req_addr.s_addr != INADDR_ANY) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1416 lease->mask = ifp->options->req_mask;
4207
d9c3c7008d20 ipv4: allow configuration of static broadcast address
Roy Marples <roy@marples.name>
parents: 4199
diff changeset
1417 if (ifp->options->req_brd.s_addr != INADDR_ANY)
d9c3c7008d20 ipv4: allow configuration of static broadcast address
Roy Marples <roy@marples.name>
parents: 4199
diff changeset
1418 lease->brd = ifp->options->req_brd;
d9c3c7008d20 ipv4: allow configuration of static broadcast address
Roy Marples <roy@marples.name>
parents: 4199
diff changeset
1419 else
d9c3c7008d20 ipv4: allow configuration of static broadcast address
Roy Marples <roy@marples.name>
parents: 4199
diff changeset
1420 lease->brd.s_addr =
d9c3c7008d20 ipv4: allow configuration of static broadcast address
Roy Marples <roy@marples.name>
parents: 4199
diff changeset
1421 lease->addr.s_addr | ~lease->mask.s_addr;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1422 } else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1423 const struct ipv4_addr *ia;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1424
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1425 ia = ipv4_iffindaddr(ifp, &lease->addr, NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1426 assert(ia != NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1427 lease->mask = ia->mask;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1428 lease->brd = ia->brd;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1429 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1430 } else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1431 if (get_option_addr(ctx, &lease->mask, bootp, len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1432 DHO_SUBNETMASK) == -1)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1433 lease->mask.s_addr =
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1434 ipv4_getnetmask(lease->addr.s_addr);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1435 if (get_option_addr(ctx, &lease->brd, bootp, len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1436 DHO_BROADCAST) == -1)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1437 lease->brd.s_addr =
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1438 lease->addr.s_addr | ~lease->mask.s_addr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1439 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1440 if (get_option_uint32(ctx, &lease->leasetime,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1441 bootp, len, DHO_LEASETIME) != 0)
4555
93b4eb29d297 DHCP: Set address vltime and pltime to the length of the lease
Roy Marples <roy@marples.name>
parents: 4549
diff changeset
1442 lease->leasetime = DHCP_INFINITE_LIFETIME;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1443 if (get_option_uint32(ctx, &lease->renewaltime,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1444 bootp, len, DHO_RENEWALTIME) != 0)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1445 lease->renewaltime = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1446 if (get_option_uint32(ctx, &lease->rebindtime,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1447 bootp, len, DHO_REBINDTIME) != 0)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1448 lease->rebindtime = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1449 if (get_option_addr(ctx, &lease->server, bootp, len, DHO_SERVERID) != 0)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1450 lease->server.s_addr = INADDR_ANY;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1451 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1452
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1453 static const char *
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1454 get_dhcp_op(uint8_t type)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1455 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1456 const struct dhcp_op *d;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1457
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1458 for (d = dhcp_ops; d->name; d++)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1459 if (d->value == type)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1460 return d->name;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1461 return NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1462 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1463
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1464 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1465 dhcp_fallback(void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1466 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1467 struct interface *iface;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1468
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1469 iface = (struct interface *)arg;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1470 dhcpcd_selectprofile(iface, iface->options->fallback);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1471 dhcpcd_startinterface(iface);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1472 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1473
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1474 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1475 dhcp_new_xid(struct interface *ifp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1476 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1477 struct dhcp_state *state;
4177
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1478 const struct interface *ifp1;
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1479 const struct dhcp_state *state1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1480
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1481 state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1482 if (ifp->options->options & DHCPCD_XID_HWADDR &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1483 ifp->hwlen >= sizeof(state->xid))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1484 /* The lower bits are probably more unique on the network */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1485 memcpy(&state->xid,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1486 (ifp->hwaddr + ifp->hwlen) - sizeof(state->xid),
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1487 sizeof(state->xid));
4177
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1488 else {
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1489 again:
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1490 state->xid = arc4random();
4177
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1491 }
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1492
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1493 /* Ensure it's unique */
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1494 TAILQ_FOREACH(ifp1, ifp->ctx->ifaces, next) {
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1495 if (ifp == ifp1)
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1496 continue;
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1497 if ((state1 = D_CSTATE(ifp1)) == NULL)
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1498 continue;
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1499 if (state1->xid == state->xid)
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1500 break;
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1501 }
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1502 if (ifp1 != NULL) {
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1503 if (ifp->options->options & DHCPCD_XID_HWADDR &&
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1504 ifp->hwlen >= sizeof(state->xid))
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1505 {
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1506 logerrx("%s: duplicate xid on %s",
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1507 ifp->name, ifp1->name);
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1508 return;
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1509 }
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1510 goto again;
bce060648873 Ensure that xid is unique across all interfaces.
Roy Marples <roy@marples.name>
parents: 4171
diff changeset
1511 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1512
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1513 /* We can't do this when sharing leases across interfaes */
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1514 #if 0
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1515 /* As the XID changes, re-apply the filter. */
4014
eed6b78ac418 Log bpf_arp and bpf_bootp failures.
Roy Marples <roy@marples.name>
parents: 4010
diff changeset
1516 if (state->bpf_fd != -1) {
eed6b78ac418 Log bpf_arp and bpf_bootp failures.
Roy Marples <roy@marples.name>
parents: 4010
diff changeset
1517 if (bpf_bootp(ifp, state->bpf_fd) == -1)
eed6b78ac418 Log bpf_arp and bpf_bootp failures.
Roy Marples <roy@marples.name>
parents: 4010
diff changeset
1518 logerr(__func__); /* try to continue */
eed6b78ac418 Log bpf_arp and bpf_bootp failures.
Roy Marples <roy@marples.name>
parents: 4010
diff changeset
1519 }
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1520 #endif
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1521 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1522
5528
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1523 static void
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1524 dhcp_closebpf(struct interface *ifp)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1525 {
4846
fa69c463f3ad privsep: Don't close BPF listeners when forked
Roy Marples <roy@marples.name>
parents: 4845
diff changeset
1526 struct dhcpcd_ctx *ctx = ifp->ctx;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1527 struct dhcp_state *state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1528
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
1529 #ifdef PRIVSEP
5528
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1530 if (IN_PRIVSEP_SE(ctx))
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
1531 ps_bpf_closebootp(ifp);
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
1532 #endif
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
1533
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1534 if (state->bpf != NULL) {
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1535 eloop_event_delete(ctx->eloop, state->bpf->bpf_fd);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1536 bpf_close(state->bpf);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1537 state->bpf = NULL;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1538 }
5528
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1539 }
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1540
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1541 static void
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1542 dhcp_closeinet(struct interface *ifp)
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1543 {
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1544 struct dhcpcd_ctx *ctx = ifp->ctx;
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1545 struct dhcp_state *state = D_STATE(ifp);
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1546
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1547 #ifdef PRIVSEP
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1548 if (IN_PRIVSEP_SE(ctx)) {
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1549 if (state->addr != NULL)
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1550 ps_inet_closebootp(state->addr);
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1551 }
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1552 #endif
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1553
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1554 if (state->udp_rfd != -1) {
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1555 eloop_event_delete(ctx->eloop, state->udp_rfd);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1556 close(state->udp_rfd);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1557 state->udp_rfd = -1;
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
1558 }
5528
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1559 }
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1560
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1561 void
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1562 dhcp_close(struct interface *ifp)
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1563 {
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1564 struct dhcp_state *state = D_STATE(ifp);
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1565
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1566 if (state == NULL)
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1567 return;
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1568
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1569 dhcp_closebpf(ifp);
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1570 dhcp_closeinet(ifp);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1571
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1572 state->interval = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1573 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1574
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
1575 int
4829
0609c95174a7 DHCP: Improve API for privsep
Roy Marples <roy@marples.name>
parents: 4825
diff changeset
1576 dhcp_openudp(struct in_addr *ia)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1577 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1578 int s;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1579 struct sockaddr_in sin;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1580 int n;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1581
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1582 if ((s = xsocket(PF_INET, SOCK_DGRAM | SOCK_CXNB, IPPROTO_UDP)) == -1)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1583 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1584
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1585 n = 1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1586 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1)
4829
0609c95174a7 DHCP: Improve API for privsep
Roy Marples <roy@marples.name>
parents: 4825
diff changeset
1587 goto errexit;
4784
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1588 #ifdef IP_RECVIF
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1589 if (setsockopt(s, IPPROTO_IP, IP_RECVIF, &n, sizeof(n)) == -1)
4829
0609c95174a7 DHCP: Improve API for privsep
Roy Marples <roy@marples.name>
parents: 4825
diff changeset
1590 goto errexit;
4784
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1591 #else
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
1592 if (setsockopt(s, IPPROTO_IP, IP_RECVPKTINFO, &n, sizeof(n)) == -1)
4829
0609c95174a7 DHCP: Improve API for privsep
Roy Marples <roy@marples.name>
parents: 4825
diff changeset
1593 goto errexit;
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
1594 #endif
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1595 #ifdef SO_RERROR
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1596 if (setsockopt(s, SOL_SOCKET, SO_RERROR, &n, sizeof(n)) == -1)
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1597 goto errexit;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1598 #endif
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1599
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1600 memset(&sin, 0, sizeof(sin));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1601 sin.sin_family = AF_INET;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1602 sin.sin_port = htons(BOOTPC);
4829
0609c95174a7 DHCP: Improve API for privsep
Roy Marples <roy@marples.name>
parents: 4825
diff changeset
1603 if (ia != NULL)
0609c95174a7 DHCP: Improve API for privsep
Roy Marples <roy@marples.name>
parents: 4825
diff changeset
1604 sin.sin_addr = *ia;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1605 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == -1)
4829
0609c95174a7 DHCP: Improve API for privsep
Roy Marples <roy@marples.name>
parents: 4825
diff changeset
1606 goto errexit;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1607
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1608 return s;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1609
4829
0609c95174a7 DHCP: Improve API for privsep
Roy Marples <roy@marples.name>
parents: 4825
diff changeset
1610 errexit:
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1611 close(s);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1612 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1613 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1614
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1615 static uint16_t
4675
5c59227c6c91 DHCP: Fix in_cksum for big endian
Roy Marples <roy@marples.name>
parents: 4642
diff changeset
1616 in_cksum(const void *data, size_t len, uint32_t *isum)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1617 {
4608
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
1618 const uint16_t *word = data;
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
1619 uint32_t sum = isum != NULL ? *isum : 0;
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
1620
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
1621 for (; len > 1; len -= sizeof(*word))
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
1622 sum += *word++;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1623
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1624 if (len == 1)
4676
d8a9fff079d6 Cast away a conversion warning with prior
Roy Marples <roy@marples.name>
parents: 4675
diff changeset
1625 sum += htons((uint16_t)(*(const uint8_t *)word << 8));
4608
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
1626
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
1627 if (isum != NULL)
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
1628 *isum = sum;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1629
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1630 sum = (sum >> 16) + (sum & 0xffff);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1631 sum += (sum >> 16);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1632
4608
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
1633 return (uint16_t)~sum;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1634 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1635
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1636 static struct bootp_pkt *
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1637 dhcp_makeudppacket(size_t *sz, const uint8_t *data, size_t length,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1638 struct in_addr source, struct in_addr dest)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1639 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1640 struct bootp_pkt *udpp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1641 struct ip *ip;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1642 struct udphdr *udp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1643
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1644 if ((udpp = calloc(1, sizeof(*ip) + sizeof(*udp) + length)) == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1645 return NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1646 ip = &udpp->ip;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1647 udp = &udpp->udp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1648
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1649 /* OK, this is important :)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1650 * We copy the data to our packet and then create a small part of the
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1651 * ip structure and an invalid ip_len (basically udp length).
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1652 * We then fill the udp structure and put the checksum
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1653 * of the whole packet into the udp checksum.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1654 * Finally we complete the ip structure and ip checksum.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1655 * If we don't do the ordering like so then the udp checksum will be
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1656 * broken, so find another way of doing it! */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1657
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1658 memcpy(&udpp->bootp, data, length);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1659
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1660 ip->ip_p = IPPROTO_UDP;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1661 ip->ip_src.s_addr = source.s_addr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1662 if (dest.s_addr == 0)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1663 ip->ip_dst.s_addr = INADDR_BROADCAST;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1664 else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1665 ip->ip_dst.s_addr = dest.s_addr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1666
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1667 udp->uh_sport = htons(BOOTPC);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1668 udp->uh_dport = htons(BOOTPS);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1669 udp->uh_ulen = htons((uint16_t)(sizeof(*udp) + length));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1670 ip->ip_len = udp->uh_ulen;
4608
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
1671 udp->uh_sum = in_cksum(udpp, sizeof(*ip) + sizeof(*udp) + length, NULL);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1672
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1673 ip->ip_v = IPVERSION;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1674 ip->ip_hl = sizeof(*ip) >> 2;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1675 ip->ip_id = (uint16_t)arc4random_uniform(UINT16_MAX);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1676 ip->ip_ttl = IPDEFTTL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1677 ip->ip_len = htons((uint16_t)(sizeof(*ip) + sizeof(*udp) + length));
4608
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
1678 ip->ip_sum = in_cksum(ip, sizeof(*ip), NULL);
4617
fff03a5ca0b8 DHCP: If IP checksum is zero for the message, make it all ones.
Roy Marples <roy@marples.name>
parents: 4610
diff changeset
1679 if (ip->ip_sum == 0)
fff03a5ca0b8 DHCP: If IP checksum is zero for the message, make it all ones.
Roy Marples <roy@marples.name>
parents: 4610
diff changeset
1680 ip->ip_sum = 0xffff; /* RFC 768 */
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1681
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1682 *sz = sizeof(*ip) + sizeof(*udp) + length;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1683 return udpp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1684 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1685
4187
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1686 static ssize_t
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1687 dhcp_sendudp(struct interface *ifp, struct in_addr *to, void *data, size_t len)
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1688 {
4784
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1689 struct sockaddr_in sin = {
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1690 .sin_family = AF_INET,
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1691 .sin_addr = *to,
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1692 .sin_port = htons(BOOTPS),
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1693 #ifdef HAVE_SA_LEN
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1694 .sin_len = sizeof(sin),
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1695 #endif
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1696 };
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1697 struct udphdr udp = {
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1698 .uh_sport = htons(BOOTPC),
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1699 .uh_dport = htons(BOOTPS),
5234
bcd021398c1d Fix compile on Linux
Roy Marples <roy@marples.name>
parents: 5233
diff changeset
1700 .uh_ulen = htons((uint16_t)(sizeof(udp) + len)),
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1701 };
4784
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1702 struct iovec iov[] = {
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1703 { .iov_base = &udp, .iov_len = sizeof(udp), },
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1704 { .iov_base = data, .iov_len = len, },
4784
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1705 };
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1706 struct msghdr msg = {
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1707 .msg_name = (void *)&sin,
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1708 .msg_namelen = sizeof(sin),
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1709 .msg_iov = iov,
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1710 .msg_iovlen = __arraycount(iov),
4784
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1711 };
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1712 struct dhcpcd_ctx *ctx = ifp->ctx;
4784
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1713
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
1714 #ifdef PRIVSEP
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1715 if (ctx->options & DHCPCD_PRIVSEP)
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1716 return ps_inet_sendbootp(ifp, &msg);
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
1717 #endif
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1718 return sendmsg(ctx->udp_wfd, &msg, 0);
4187
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1719 }
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1720
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1721 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1722 send_message(struct interface *ifp, uint8_t type,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1723 void (*callback)(void *))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1724 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1725 struct dhcp_state *state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1726 struct if_options *ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1727 struct bootp *bootp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1728 struct bootp_pkt *udp;
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1729 size_t len, ulen;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1730 ssize_t r;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1731 struct in_addr from, to;
4932
8e1948e75f39 DHCP: Use milliseconds rather than timespec for retranmission
Roy Marples <roy@marples.name>
parents: 4924
diff changeset
1732 unsigned int RT;
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1733
4940
a0d2d9dd08f3 DHCP: Fix a bogus gcc warning
Roy Marples <roy@marples.name>
parents: 4932
diff changeset
1734 if (callback == NULL) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1735 /* No carrier? Don't bother sending the packet. */
5499
6a2da5651841 dhcpcd: Simplify the link handling even more
Roy Marples <roy@marples.name>
parents: 5497
diff changeset
1736 if (!if_is_link_up(ifp))
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1737 return;
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
1738 logdebugx("%s: sending %s with xid 0x%x",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1739 ifp->name,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1740 ifo->options & DHCPCD_BOOTP ? "BOOTP" : get_dhcp_op(type),
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1741 state->xid);
4940
a0d2d9dd08f3 DHCP: Fix a bogus gcc warning
Roy Marples <roy@marples.name>
parents: 4932
diff changeset
1742 RT = 0; /* bogus gcc warning */
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1743 } else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1744 if (state->interval == 0)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1745 state->interval = 4;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1746 else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1747 state->interval *= 2;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1748 if (state->interval > 64)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1749 state->interval = 64;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1750 }
4932
8e1948e75f39 DHCP: Use milliseconds rather than timespec for retranmission
Roy Marples <roy@marples.name>
parents: 4924
diff changeset
1751 RT = (state->interval * MSEC_PER_SEC) +
8e1948e75f39 DHCP: Use milliseconds rather than timespec for retranmission
Roy Marples <roy@marples.name>
parents: 4924
diff changeset
1752 (arc4random_uniform(MSEC_PER_SEC * 2) - MSEC_PER_SEC);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1753 /* No carrier? Don't bother sending the packet.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1754 * However, we do need to advance the timeout. */
5499
6a2da5651841 dhcpcd: Simplify the link handling even more
Roy Marples <roy@marples.name>
parents: 5497
diff changeset
1755 if (!if_is_link_up(ifp))
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1756 goto fail;
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
1757 logdebugx("%s: sending %s (xid 0x%x), next in %0.1f seconds",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1758 ifp->name,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1759 ifo->options & DHCPCD_BOOTP ? "BOOTP" : get_dhcp_op(type),
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1760 state->xid,
4932
8e1948e75f39 DHCP: Use milliseconds rather than timespec for retranmission
Roy Marples <roy@marples.name>
parents: 4924
diff changeset
1761 (float)RT / MSEC_PER_SEC);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1762 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1763
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1764 r = make_message(&bootp, ifp, type);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1765 if (r == -1)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1766 goto fail;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1767 len = (size_t)r;
4719
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1768
5424
28a650d93729 DHCP: Mark the state as expired on lase lease extension
Roy Marples <roy@marples.name>
parents: 5299
diff changeset
1769 if (!(state->added & (STATE_FAKE | STATE_EXPIRED)) &&
4829
0609c95174a7 DHCP: Improve API for privsep
Roy Marples <roy@marples.name>
parents: 4825
diff changeset
1770 state->addr != NULL &&
0609c95174a7 DHCP: Improve API for privsep
Roy Marples <roy@marples.name>
parents: 4825
diff changeset
1771 ipv4_iffindaddr(ifp, &state->lease.addr, NULL) != NULL)
4719
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1772 from.s_addr = state->lease.addr.s_addr;
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1773 else
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1774 from.s_addr = INADDR_ANY;
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1775 if (from.s_addr != INADDR_ANY &&
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1776 state->lease.server.s_addr != INADDR_ANY)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1777 to.s_addr = state->lease.server.s_addr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1778 else
4719
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1779 to.s_addr = INADDR_BROADCAST;
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1780
5528
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1781 logdebugx("from %s", inet_ntoa(from));
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1782 logdebugx("to %s", inet_ntoa(to));
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
1783
4719
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1784 /*
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1785 * If not listening on the unspecified address we can
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1786 * only receive broadcast messages via BPF.
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1787 * Sockets bound to an address cannot receive broadcast messages
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1788 * even if they are setup to send them.
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1789 * Broadcasting from UDP is only an optimisation for rebinding
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1790 * and on BSD, at least, is reliant on the subnet route being
4825
c8d76614bcd0 Spelling fixes
Sascha Wildner <swildner@dragonflybsd.org>
parents: 4810
diff changeset
1791 * correctly configured to receive the unicast reply.
4719
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1792 * As such, we always broadcast and receive the reply to it via BPF.
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1793 * This also guarantees we have a DHCP server attached to the
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1794 * interface we want to configure because we can't dictate the
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1795 * interface via IP_PKTINFO unlike for IPv6.
bb76c20ed3ac DHCP: Improve from/to address selection
Roy Marples <roy@marples.name>
parents: 4718
diff changeset
1796 */
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1797 if (to.s_addr != INADDR_BROADCAST) {
4187
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1798 if (dhcp_sendudp(ifp, &to, bootp, len) != -1)
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1799 goto out;
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1800 logerr("%s: dhcp_sendudp", ifp->name);
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1801 }
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1802
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1803 if (dhcp_openbpf(ifp) == -1)
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1804 goto out;
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1805
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1806 udp = dhcp_makeudppacket(&ulen, (uint8_t *)bootp, len, from, to);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1807 if (udp == NULL) {
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1808 logerr("%s: dhcp_makeudppacket", ifp->name);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1809 r = 0;
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
1810 #ifdef PRIVSEP
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
1811 } else if (ifp->ctx->options & DHCPCD_PRIVSEP) {
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
1812 r = ps_bpf_sendbootp(ifp, udp, ulen);
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
1813 free(udp);
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
1814 #endif
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1815 } else {
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
1816 r = bpf_send(state->bpf, ETHERTYPE_IP, udp, ulen);
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1817 free(udp);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1818 }
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1819 /* If we failed to send a raw packet this normally means
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1820 * we don't have the ability to work beneath the IP layer
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1821 * for this interface.
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1822 * As such we remove it from consideration without actually
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1823 * stopping the interface. */
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1824 if (r == -1) {
4784
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
1825 logerr("%s: bpf_send", ifp->name);
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1826 switch(errno) {
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1827 case ENETDOWN:
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1828 case ENETRESET:
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1829 case ENETUNREACH:
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1830 case ENOBUFS:
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1831 break;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1832 default:
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1833 if (!(ifp->ctx->options & DHCPCD_TEST))
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1834 dhcp_drop(ifp, "FAIL");
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1835 eloop_timeout_delete(ifp->ctx->eloop,
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1836 NULL, ifp);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
1837 callback = NULL;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1838 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1839 }
4187
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1840
73de5e0edf30 dhcp: when unicasting, don't use a L2 broadcast
Roy Marples <roy@marples.name>
parents: 4177
diff changeset
1841 out:
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1842 free(bootp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1843
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1844 fail:
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1845 /* Even if we fail to send a packet we should continue as we are
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1846 * as our failure timeouts will change out codepath when needed. */
4940
a0d2d9dd08f3 DHCP: Fix a bogus gcc warning
Roy Marples <roy@marples.name>
parents: 4932
diff changeset
1847 if (callback != NULL)
4932
8e1948e75f39 DHCP: Use milliseconds rather than timespec for retranmission
Roy Marples <roy@marples.name>
parents: 4924
diff changeset
1848 eloop_timeout_add_msec(ifp->ctx->eloop, RT, callback, ifp);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1849 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1850
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1851 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1852 send_inform(void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1853 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1854
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1855 send_message((struct interface *)arg, DHCP_INFORM, send_inform);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1856 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1857
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1858 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1859 send_discover(void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1860 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1861
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1862 send_message((struct interface *)arg, DHCP_DISCOVER, send_discover);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1863 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1864
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1865 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1866 send_request(void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1867 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1868
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1869 send_message((struct interface *)arg, DHCP_REQUEST, send_request);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1870 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1871
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1872 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1873 send_renew(void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1874 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1875
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1876 send_message((struct interface *)arg, DHCP_REQUEST, send_renew);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1877 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1878
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1879 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1880 send_rebind(void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1881 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1882
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1883 send_message((struct interface *)arg, DHCP_REQUEST, send_rebind);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1884 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1885
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1886 void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1887 dhcp_discover(void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1888 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1889 struct interface *ifp = arg;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1890 struct dhcp_state *state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1891 struct if_options *ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1892
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1893 state->state = DHS_DISCOVER;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1894 dhcp_new_xid(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1895 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
5428
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1896 if (!(state->added & STATE_EXPIRED)) {
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1897 if (ifo->fallback)
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1898 eloop_timeout_add_sec(ifp->ctx->eloop,
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1899 ifo->reboot, dhcp_fallback, ifp);
5449
b8078525aa41 DHCP: Fix compile without IPv4LL
Roy Marples <roy@marples.name>
parents: 5429
diff changeset
1900 #ifdef IPV4LL
5428
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1901 else if (ifo->options & DHCPCD_IPV4LL)
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1902 eloop_timeout_add_sec(ifp->ctx->eloop,
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1903 ifo->reboot, ipv4ll_start, ifp);
5449
b8078525aa41 DHCP: Fix compile without IPv4LL
Roy Marples <roy@marples.name>
parents: 5429
diff changeset
1904 #endif
5428
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1905 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1906 if (ifo->options & DHCPCD_REQUEST)
4024
0984f93d896e To be consitent, do the same to loginfo as we just did for logdebug.
Roy Marples <roy@marples.name>
parents: 4023
diff changeset
1907 loginfox("%s: soliciting a DHCP lease (requesting %s)",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1908 ifp->name, inet_ntoa(ifo->req_addr));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1909 else
4024
0984f93d896e To be consitent, do the same to loginfo as we just did for logdebug.
Roy Marples <roy@marples.name>
parents: 4023
diff changeset
1910 loginfox("%s: soliciting a %s lease",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1911 ifp->name, ifo->options & DHCPCD_BOOTP ? "BOOTP" : "DHCP");
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1912 send_discover(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1913 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1914
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1915 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1916 dhcp_request(void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1917 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1918 struct interface *ifp = arg;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1919 struct dhcp_state *state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1920
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1921 state->state = DHS_REQUEST;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1922 send_request(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1923 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1924
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1925 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1926 dhcp_expire(void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1927 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1928 struct interface *ifp = arg;
5428
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1929 struct dhcp_state *state = D_STATE(ifp);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1930
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
1931 if (ifp->options->options & DHCPCD_LASTLEASE_EXTEND) {
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
1932 logwarnx("%s: DHCP lease expired, extending lease", ifp->name);
5424
28a650d93729 DHCP: Mark the state as expired on lase lease extension
Roy Marples <roy@marples.name>
parents: 5299
diff changeset
1933 state->added |= STATE_EXPIRED;
5428
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1934 } else {
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1935 logerrx("%s: DHCP lease expired", ifp->name);
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1936 dhcp_drop(ifp, "EXPIRE");
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1937 dhcp_unlink(ifp->ctx, state->leasefile);
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
1938 }
5428
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1939 state->interval = 0;
ab53e42710ae DHCP: When extending the lease, re-enter the DISCOVER phase.
Roy Marples <roy@marples.name>
parents: 5426
diff changeset
1940 dhcp_discover(ifp);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1941 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1942
4135
64796240ee75 arp: RFC5227 kernels need to send ARP too
Roy Marples <roy@marples.name>
parents: 4125
diff changeset
1943 #if defined(ARP) || defined(IN_IFF_DUPLICATED)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1944 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1945 dhcp_decline(struct interface *ifp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1946 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1947
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1948 send_message(ifp, DHCP_DECLINE, NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1949 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1950 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1951
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1952 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1953 dhcp_startrenew(void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1954 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1955 struct interface *ifp = arg;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1956 struct dhcp_state *state;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1957 struct dhcp_lease *lease;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1958
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1959 if ((state = D_STATE(ifp)) == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1960 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1961
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1962 /* Only renew in the bound or renew states */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1963 if (state->state != DHS_BOUND &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1964 state->state != DHS_RENEW)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1965 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1966
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1967 /* Remove the timeout as the renew may have been forced. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1968 eloop_timeout_delete(ifp->ctx->eloop, dhcp_startrenew, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1969
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1970 lease = &state->lease;
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
1971 logdebugx("%s: renewing lease of %s", ifp->name,
3960
42a6a3d1596e Remove custom logger and output all diagnostics via syslog(3).
Roy Marples <roy@marples.name>
parents: 3932
diff changeset
1972 inet_ntoa(lease->addr));
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1973 state->state = DHS_RENEW;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1974 dhcp_new_xid(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1975 state->interval = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1976 send_renew(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1977 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1978
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1979 void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1980 dhcp_renew(struct interface *ifp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1981 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1982
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1983 dhcp_startrenew(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1984 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1985
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1986 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1987 dhcp_rebind(void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1988 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1989 struct interface *ifp = arg;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1990 struct dhcp_state *state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1991 struct dhcp_lease *lease = &state->lease;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1992
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
1993 logwarnx("%s: failed to renew DHCP, rebinding", ifp->name);
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
1994 logdebugx("%s: expire in %"PRIu32" seconds",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1995 ifp->name, lease->leasetime - lease->rebindtime);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1996 state->state = DHS_REBIND;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1997 eloop_timeout_delete(ifp->ctx->eloop, send_renew, ifp);
4188
23ff91710a01 dhcp: Don't set ciaddr or unicast if the address we have is faked.
Roy Marples <roy@marples.name>
parents: 4187
diff changeset
1998 state->lease.server.s_addr = INADDR_ANY;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
1999 state->interval = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2000 ifp->options->options &= ~(DHCPCD_CSR_WARNED |
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2001 DHCPCD_ROUTER_HOST_ROUTE_WARNED);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2002 send_rebind(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2003 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2004
4584
978e3fbbec6c Fix compile on Linux without ARPING
Roy Marples <roy@marples.name>
parents: 4581
diff changeset
2005 #if defined(ARP) || defined(IN_IFF_DUPLICATED)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2006 static void
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2007 dhcp_finish_dad(struct interface *ifp, struct in_addr *ia)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2008 {
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2009 struct dhcp_state *state = D_STATE(ifp);
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2010
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2011 if (state->state != DHS_PROBE)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2012 return;
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2013 if (state->offer == NULL || state->offer->yiaddr != ia->s_addr)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2014 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2015
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2016 logdebugx("%s: DAD completed for %s", ifp->name, inet_ntoa(*ia));
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2017 if (!(ifp->options->options & DHCPCD_INFORM))
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2018 dhcp_bind(ifp);
4485
9ba7f650a8f7 Merge branch 'dhcpcd-7'
Roy Marples <roy@marples.name>
parents: 4463 4481
diff changeset
2019 #ifndef IN_IFF_DUPLICATED
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2020 else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2021 struct bootp *bootp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2022 size_t len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2023
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2024 bootp = state->new;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2025 len = state->new_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2026 state->new = state->offer;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2027 state->new_len = state->offer_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2028 get_lease(ifp, &state->lease, state->new, state->new_len);
4457
8e41e487831d Linux: Fix compile
Roy Marples <roy@marples.name>
parents: 4453
diff changeset
2029 ipv4_applyaddr(ifp);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2030 state->new = bootp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2031 state->new_len = len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2032 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2033 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2034
4287
2b519911f230 dhcp: Fix more fallout for builds without IPv4LL
Roy Marples <roy@marples.name>
parents: 4278
diff changeset
2035 #ifdef IPV4LL
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2036 /* Stop IPv4LL now we have a working DHCP address */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2037 ipv4ll_drop(ifp);
4287
2b519911f230 dhcp: Fix more fallout for builds without IPv4LL
Roy Marples <roy@marples.name>
parents: 4278
diff changeset
2038 #endif
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2039
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2040 if (ifp->options->options & DHCPCD_INFORM)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2041 dhcp_inform(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2042 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2043
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2044
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
2045 static bool
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2046 dhcp_addr_duplicated(struct interface *ifp, struct in_addr *ia)
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2047 {
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2048 struct dhcp_state *state = D_STATE(ifp);
4731
f421b59ced72 DHCP: Don't start DHCP if we fail DAD for static or informed addrs
Roy Marples <roy@marples.name>
parents: 4726
diff changeset
2049 unsigned long long opts = ifp->options->options;
f421b59ced72 DHCP: Don't start DHCP if we fail DAD for static or informed addrs
Roy Marples <roy@marples.name>
parents: 4726
diff changeset
2050 struct dhcpcd_ctx *ctx = ifp->ctx;
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
2051 bool deleted = false;
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2052 #ifdef IN_IFF_DUPLICATED
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2053 struct ipv4_addr *iap;
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2054 #endif
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2055
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2056 if ((state->offer == NULL || state->offer->yiaddr != ia->s_addr) &&
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2057 !IN_ARE_ADDR_EQUAL(ia, &state->lease.addr))
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
2058 return deleted;
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2059
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2060 /* RFC 2131 3.1.5, Client-server interaction */
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2061 logerrx("%s: DAD detected %s", ifp->name, inet_ntoa(*ia));
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
2062 dhcp_unlink(ifp->ctx, state->leasefile);
4731
f421b59ced72 DHCP: Don't start DHCP if we fail DAD for static or informed addrs
Roy Marples <roy@marples.name>
parents: 4726
diff changeset
2063 if (!(opts & DHCPCD_STATIC) && !state->lease.frominfo)
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2064 dhcp_decline(ifp);
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2065 #ifdef IN_IFF_DUPLICATED
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
2066 if ((iap = ipv4_iffindaddr(ifp, ia, NULL)) != NULL) {
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2067 ipv4_deladdr(iap, 0);
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
2068 deleted = true;
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
2069 }
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2070 #endif
4731
f421b59ced72 DHCP: Don't start DHCP if we fail DAD for static or informed addrs
Roy Marples <roy@marples.name>
parents: 4726
diff changeset
2071 eloop_timeout_delete(ctx->eloop, NULL, ifp);
f421b59ced72 DHCP: Don't start DHCP if we fail DAD for static or informed addrs
Roy Marples <roy@marples.name>
parents: 4726
diff changeset
2072 if (opts & (DHCPCD_STATIC | DHCPCD_INFORM)) {
f421b59ced72 DHCP: Don't start DHCP if we fail DAD for static or informed addrs
Roy Marples <roy@marples.name>
parents: 4726
diff changeset
2073 state->reason = "EXPIRE";
f421b59ced72 DHCP: Don't start DHCP if we fail DAD for static or informed addrs
Roy Marples <roy@marples.name>
parents: 4726
diff changeset
2074 script_runreason(ifp, state->reason);
4738
f85dad8813de DHCP: Don't exit on static DaD failure if master or using IPv6.
Roy Marples <roy@marples.name>
parents: 4737
diff changeset
2075 #define NOT_ONLY_SELF (DHCPCD_MASTER | DHCPCD_IPV6RS | DHCPCD_DHCP6)
f85dad8813de DHCP: Don't exit on static DaD failure if master or using IPv6.
Roy Marples <roy@marples.name>
parents: 4737
diff changeset
2076 if (!(ctx->options & NOT_ONLY_SELF))
4731
f421b59ced72 DHCP: Don't start DHCP if we fail DAD for static or informed addrs
Roy Marples <roy@marples.name>
parents: 4726
diff changeset
2077 eloop_exit(ifp->ctx->eloop, EXIT_FAILURE);
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
2078 return deleted;
4731
f421b59ced72 DHCP: Don't start DHCP if we fail DAD for static or informed addrs
Roy Marples <roy@marples.name>
parents: 4726
diff changeset
2079 }
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2080 eloop_timeout_add_sec(ifp->ctx->eloop,
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2081 DHCP_RAND_MAX, dhcp_discover, ifp);
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
2082 return deleted;
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2083 }
4584
978e3fbbec6c Fix compile on Linux without ARPING
Roy Marples <roy@marples.name>
parents: 4581
diff changeset
2084 #endif
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2085
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2086 #ifdef ARP
5232
d09c879cc4d5 Fix compile for prior on NetBSD
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
2087 #ifdef KERNEL_RFC5227
d09c879cc4d5 Fix compile for prior on NetBSD
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
2088 static void
d09c879cc4d5 Fix compile for prior on NetBSD
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
2089 dhcp_arp_announced(struct arp_state *state)
d09c879cc4d5 Fix compile for prior on NetBSD
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
2090 {
d09c879cc4d5 Fix compile for prior on NetBSD
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
2091
d09c879cc4d5 Fix compile for prior on NetBSD
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
2092 arp_free(state);
d09c879cc4d5 Fix compile for prior on NetBSD
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
2093 }
d09c879cc4d5 Fix compile for prior on NetBSD
Roy Marples <roy@marples.name>
parents: 5231
diff changeset
2094 #else
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2095 static void
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2096 dhcp_arp_defend_failed(struct arp_state *astate)
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2097 {
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2098 struct interface *ifp = astate->iface;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2099
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2100 dhcp_drop(ifp, "EXPIRED");
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2101 dhcp_start1(ifp);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2102 }
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2103 #endif
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2104
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2105 #if !defined(KERNEL_RFC5227) || defined(ARPING)
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2106 static void dhcp_arp_not_found(struct arp_state *);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2107
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2108 static struct arp_state *
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2109 dhcp_arp_new(struct interface *ifp, struct in_addr *addr)
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2110 {
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2111 struct arp_state *astate;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2112
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2113 astate = arp_new(ifp, addr);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2114 if (astate == NULL)
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2115 return NULL;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2116
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2117 astate->found_cb = dhcp_arp_found;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2118 astate->not_found_cb = dhcp_arp_not_found;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2119 #ifdef KERNEL_RFC5227
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2120 astate->announced_cb = dhcp_arp_announced;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2121 #else
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2122 astate->announced_cb = NULL;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2123 astate->defend_failed_cb = dhcp_arp_defend_failed;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2124 #endif
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2125 return astate;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2126 }
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2127 #endif
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2128
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2129 #ifdef ARPING
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2130 static int
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2131 dhcp_arping(struct interface *ifp)
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2132 {
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2133 struct dhcp_state *state;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2134 struct if_options *ifo;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2135 struct arp_state *astate;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2136 struct in_addr addr;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2137
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2138 state = D_STATE(ifp);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2139 ifo = ifp->options;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2140
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2141 if (ifo->arping_len == 0 || state->arping_index > ifo->arping_len)
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2142 return 0;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2143
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2144 if (state->arping_index + 1 == ifo->arping_len) {
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2145 state->arping_index++;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2146 dhcpcd_startinterface(ifp);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2147 return 1;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2148 }
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2149
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2150 addr.s_addr = ifo->arping[++state->arping_index];
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2151 astate = dhcp_arp_new(ifp, &addr);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2152 if (astate == NULL) {
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2153 logerr(__func__);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2154 return -1;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2155 }
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2156 arp_probe(astate);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2157 return 1;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2158 }
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2159 #endif
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2160
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2161 #if !defined(KERNEL_RFC5227) || defined(ARPING)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2162 static void
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2163 dhcp_arp_not_found(struct arp_state *astate)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2164 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2165 struct interface *ifp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2166
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2167 ifp = astate->iface;
4581
f069d6d7f542 Fix build without ARP and/or ARPING
Roy Marples <roy@marples.name>
parents: 4567
diff changeset
2168 #ifdef ARPING
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2169 if (dhcp_arping(ifp) == 1) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2170 arp_free(astate);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2171 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2172 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2173 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2174
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2175 dhcp_finish_dad(ifp, &astate->addr);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2176 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2177
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2178 static void
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2179 dhcp_arp_found(struct arp_state *astate, const struct arp_msg *amsg)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2180 {
4457
8e41e487831d Linux: Fix compile
Roy Marples <roy@marples.name>
parents: 4453
diff changeset
2181 struct in_addr addr;
4581
f069d6d7f542 Fix build without ARP and/or ARPING
Roy Marples <roy@marples.name>
parents: 4567
diff changeset
2182 struct interface *ifp = astate->iface;
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2183 #ifdef ARPING
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2184 struct dhcp_state *state;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2185 struct if_options *ifo;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2186
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2187 state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2188
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2189 ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2190 if (state->arping_index != -1 &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2191 state->arping_index < ifo->arping_len &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2192 amsg &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2193 amsg->sip.s_addr == ifo->arping[state->arping_index])
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2194 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2195 char buf[HWADDR_LEN * 3];
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2196
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2197 hwaddr_ntoa(amsg->sha, ifp->hwlen, buf, sizeof(buf));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2198 if (dhcpcd_selectprofile(ifp, buf) == -1 &&
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2199 dhcpcd_selectprofile(ifp, inet_ntoa(amsg->sip)) == -1)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2200 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2201 /* We didn't find a profile for this
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2202 * address or hwaddr, so move to the next
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2203 * arping profile */
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2204 dhcp_arp_not_found(astate);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2205 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2206 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2207 arp_free(astate);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2208 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2209 dhcpcd_startinterface(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2210 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2211 }
4581
f069d6d7f542 Fix build without ARP and/or ARPING
Roy Marples <roy@marples.name>
parents: 4567
diff changeset
2212 #else
f069d6d7f542 Fix build without ARP and/or ARPING
Roy Marples <roy@marples.name>
parents: 4567
diff changeset
2213 UNUSED(amsg);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2214 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2215
4457
8e41e487831d Linux: Fix compile
Roy Marples <roy@marples.name>
parents: 4453
diff changeset
2216 addr = astate->addr;
8e41e487831d Linux: Fix compile
Roy Marples <roy@marples.name>
parents: 4453
diff changeset
2217 arp_free(astate);
4567
dda61c0a55f5 DHCP: On ARP duplication, ensure the interface pointer is valid
Roy Marples <roy@marples.name>
parents: 4562
diff changeset
2218 dhcp_addr_duplicated(ifp, &addr);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2219 }
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2220 #endif
4376
579ab74cded9 DHCP: free the ARP state of the address once announced
Roy Marples <roy@marples.name>
parents: 4374
diff changeset
2221
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2222 #endif /* ARP */
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2223
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2224 void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2225 dhcp_bind(struct interface *ifp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2226 {
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
2227 struct dhcpcd_ctx *ctx = ifp->ctx;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2228 struct dhcp_state *state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2229 struct if_options *ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2230 struct dhcp_lease *lease = &state->lease;
4952
e77138b6ec3b DHCP: Spawn a listener in non master mode for a new lease
Roy Marples <roy@marples.name>
parents: 4940
diff changeset
2231 uint8_t old_state;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2232
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2233 state->reason = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2234 /* If we don't have an offer, we are re-binding a lease on preference,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2235 * normally when two interfaces have a lease matching IP addresses. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2236 if (state->offer) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2237 free(state->old);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2238 state->old = state->new;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2239 state->old_len = state->new_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2240 state->new = state->offer;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2241 state->new_len = state->offer_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2242 state->offer = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2243 state->offer_len = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2244 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2245 get_lease(ifp, lease, state->new, state->new_len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2246 if (ifo->options & DHCPCD_STATIC) {
4024
0984f93d896e To be consitent, do the same to loginfo as we just did for logdebug.
Roy Marples <roy@marples.name>
parents: 4023
diff changeset
2247 loginfox("%s: using static address %s/%d",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2248 ifp->name, inet_ntoa(lease->addr),
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2249 inet_ntocidr(lease->mask));
4555
93b4eb29d297 DHCP: Set address vltime and pltime to the length of the lease
Roy Marples <roy@marples.name>
parents: 4549
diff changeset
2250 lease->leasetime = DHCP_INFINITE_LIFETIME;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2251 state->reason = "STATIC";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2252 } else if (ifo->options & DHCPCD_INFORM) {
4024
0984f93d896e To be consitent, do the same to loginfo as we just did for logdebug.
Roy Marples <roy@marples.name>
parents: 4023
diff changeset
2253 loginfox("%s: received approval for %s",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2254 ifp->name, inet_ntoa(lease->addr));
4555
93b4eb29d297 DHCP: Set address vltime and pltime to the length of the lease
Roy Marples <roy@marples.name>
parents: 4549
diff changeset
2255 lease->leasetime = DHCP_INFINITE_LIFETIME;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2256 state->reason = "INFORM";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2257 } else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2258 if (lease->frominfo)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2259 state->reason = "TIMEOUT";
4555
93b4eb29d297 DHCP: Set address vltime and pltime to the length of the lease
Roy Marples <roy@marples.name>
parents: 4549
diff changeset
2260 if (lease->leasetime == DHCP_INFINITE_LIFETIME) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2261 lease->renewaltime =
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2262 lease->rebindtime =
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2263 lease->leasetime;
4024
0984f93d896e To be consitent, do the same to loginfo as we just did for logdebug.
Roy Marples <roy@marples.name>
parents: 4023
diff changeset
2264 loginfox("%s: leased %s for infinity",
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2265 ifp->name, inet_ntoa(lease->addr));
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2266 } else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2267 if (lease->leasetime < DHCP_MIN_LEASE) {
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2268 logwarnx("%s: minimum lease is %d seconds",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2269 ifp->name, DHCP_MIN_LEASE);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2270 lease->leasetime = DHCP_MIN_LEASE;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2271 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2272 if (lease->rebindtime == 0)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2273 lease->rebindtime =
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2274 (uint32_t)(lease->leasetime * T2);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2275 else if (lease->rebindtime >= lease->leasetime) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2276 lease->rebindtime =
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2277 (uint32_t)(lease->leasetime * T2);
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2278 logwarnx("%s: rebind time greater than lease "
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2279 "time, forcing to %"PRIu32" seconds",
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2280 ifp->name, lease->rebindtime);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2281 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2282 if (lease->renewaltime == 0)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2283 lease->renewaltime =
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2284 (uint32_t)(lease->leasetime * T1);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2285 else if (lease->renewaltime > lease->rebindtime) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2286 lease->renewaltime =
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2287 (uint32_t)(lease->leasetime * T1);
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2288 logwarnx("%s: renewal time greater than "
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2289 "rebind time, forcing to %"PRIu32" seconds",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2290 ifp->name, lease->renewaltime);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2291 }
5226
06575a46bbdd DHCP: always log inform when not renewing
Roy Marples <roy@marples.name>
parents: 5211
diff changeset
2292 if (state->state == DHS_RENEW && state->addr &&
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2293 lease->addr.s_addr == state->addr->addr.s_addr &&
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2294 !(state->added & STATE_FAKE))
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
2295 logdebugx("%s: leased %s for %"PRIu32" seconds",
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2296 ifp->name, inet_ntoa(lease->addr),
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2297 lease->leasetime);
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2298 else
4024
0984f93d896e To be consitent, do the same to loginfo as we just did for logdebug.
Roy Marples <roy@marples.name>
parents: 4023
diff changeset
2299 loginfox("%s: leased %s for %"PRIu32" seconds",
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2300 ifp->name, inet_ntoa(lease->addr),
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2301 lease->leasetime);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2302 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2303 }
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
2304 if (ctx->options & DHCPCD_TEST) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2305 state->reason = "TEST";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2306 script_runreason(ifp, state->reason);
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
2307 eloop_exit(ctx->eloop, EXIT_SUCCESS);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2308 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2309 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2310 if (state->reason == NULL) {
5429
4729f8db7842 DHCP: reason is BOUND when recovering from an expired extended lease
Roy Marples <roy@marples.name>
parents: 5428
diff changeset
2311 if (state->old &&
4729f8db7842 DHCP: reason is BOUND when recovering from an expired extended lease
Roy Marples <roy@marples.name>
parents: 5428
diff changeset
2312 !(state->added & (STATE_FAKE | STATE_EXPIRED)))
4729f8db7842 DHCP: reason is BOUND when recovering from an expired extended lease
Roy Marples <roy@marples.name>
parents: 5428
diff changeset
2313 {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2314 if (state->old->yiaddr == state->new->yiaddr &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2315 lease->server.s_addr &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2316 state->state != DHS_REBIND)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2317 state->reason = "RENEW";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2318 else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2319 state->reason = "REBIND";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2320 } else if (state->state == DHS_REBOOT)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2321 state->reason = "REBOOT";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2322 else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2323 state->reason = "BOUND";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2324 }
4555
93b4eb29d297 DHCP: Set address vltime and pltime to the length of the lease
Roy Marples <roy@marples.name>
parents: 4549
diff changeset
2325 if (lease->leasetime == DHCP_INFINITE_LIFETIME)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2326 lease->renewaltime = lease->rebindtime = lease->leasetime;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2327 else {
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
2328 eloop_timeout_add_sec(ctx->eloop,
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
2329 lease->renewaltime, dhcp_startrenew, ifp);
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
2330 eloop_timeout_add_sec(ctx->eloop,
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
2331 lease->rebindtime, dhcp_rebind, ifp);
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
2332 eloop_timeout_add_sec(ctx->eloop,
4923
4fcca755943e eloop: Allow for for timeouts greater than time_t and time wrapping
Roy Marples <roy@marples.name>
parents: 4922
diff changeset
2333 lease->leasetime, dhcp_expire, ifp);
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
2334 logdebugx("%s: renew in %"PRIu32" seconds, rebind in %"PRIu32
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2335 " seconds",
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2336 ifp->name, lease->renewaltime, lease->rebindtime);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2337 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2338 state->state = DHS_BOUND;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2339 if (!state->lease.frominfo &&
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
2340 !(ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC))) {
5494
0fbde4769bbe Don't log backticks.
Roy Marples <roy@marples.name>
parents: 5489
diff changeset
2341 logdebugx("%s: writing lease: %s",
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
2342 ifp->name, state->leasefile);
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
2343 if (dhcp_writefile(ifp->ctx, state->leasefile, 0640,
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
2344 state->new, state->new_len) == -1)
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
2345 logerr("dhcp_writefile: %s", state->leasefile);
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
2346 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2347
5528
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
2348 old_state = state->added;
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
2349
4681
c0b19aa0ee47 DHCP: Don't listen on the unspecified address when not in master mode
Roy Marples <roy@marples.name>
parents: 4677
diff changeset
2350 /* Close the BPF filter as we can now receive DHCP messages
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
2351 * on a UDP socket. */
5528
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
2352 dhcp_closebpf(ifp);
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
2353
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
2354 /* Add the address */
4833
aadc0d25a706 DHCP: Require unspecificed BOOTP port for master mode
Roy Marples <roy@marples.name>
parents: 4829
diff changeset
2355 ipv4_applyaddr(ifp);
4829
0609c95174a7 DHCP: Improve API for privsep
Roy Marples <roy@marples.name>
parents: 4825
diff changeset
2356
4792
ed9e1c73478f DHCP: Rework closing BPF and seeing if we need to open UDP
Roy Marples <roy@marples.name>
parents: 4791
diff changeset
2357 /* If not in master mode, open an address specific socket. */
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
2358 if (ctx->options & DHCPCD_MASTER ||
4952
e77138b6ec3b DHCP: Spawn a listener in non master mode for a new lease
Roy Marples <roy@marples.name>
parents: 4940
diff changeset
2359 (state->old != NULL &&
5528
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
2360 state->old->yiaddr == state->new->yiaddr &&
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
2361 old_state & STATE_ADDED && !(old_state & STATE_FAKE)))
4792
ed9e1c73478f DHCP: Rework closing BPF and seeing if we need to open UDP
Roy Marples <roy@marples.name>
parents: 4791
diff changeset
2362 return;
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
2363
5528
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
2364 dhcp_closeinet(ifp);
274d31419935 privsep: don't leave a BOOTP process hanging around on SIGUSR1
Roy Marples <roy@marples.name>
parents: 5524
diff changeset
2365
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
2366 #ifdef PRIVSEP
4868
119c8986dfc8 privsep: Enable ARP BPF filtering for interesting addresses
Roy Marples <roy@marples.name>
parents: 4866
diff changeset
2367 if (IN_PRIVSEP_SE(ctx)) {
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
2368 if (ps_inet_openbootp(state->addr) == -1)
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
2369 logerr(__func__);
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
2370 return;
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
2371 }
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
2372 #endif
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
2373
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2374 state->udp_rfd = dhcp_openudp(&state->addr->addr);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2375 if (state->udp_rfd == -1) {
4792
ed9e1c73478f DHCP: Rework closing BPF and seeing if we need to open UDP
Roy Marples <roy@marples.name>
parents: 4791
diff changeset
2376 logerr(__func__);
ed9e1c73478f DHCP: Rework closing BPF and seeing if we need to open UDP
Roy Marples <roy@marples.name>
parents: 4791
diff changeset
2377 /* Address sharing without master mode is not supported.
ed9e1c73478f DHCP: Rework closing BPF and seeing if we need to open UDP
Roy Marples <roy@marples.name>
parents: 4791
diff changeset
2378 * It's also possible another DHCP client could be running,
ed9e1c73478f DHCP: Rework closing BPF and seeing if we need to open UDP
Roy Marples <roy@marples.name>
parents: 4791
diff changeset
2379 * which is even worse.
ed9e1c73478f DHCP: Rework closing BPF and seeing if we need to open UDP
Roy Marples <roy@marples.name>
parents: 4791
diff changeset
2380 * We still need to work, so re-open BPF. */
ed9e1c73478f DHCP: Rework closing BPF and seeing if we need to open UDP
Roy Marples <roy@marples.name>
parents: 4791
diff changeset
2381 dhcp_openbpf(ifp);
ed9e1c73478f DHCP: Rework closing BPF and seeing if we need to open UDP
Roy Marples <roy@marples.name>
parents: 4791
diff changeset
2382 return;
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
2383 }
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
2384 eloop_event_add(ctx->eloop, state->udp_rfd, dhcp_handleifudp, ifp);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2385 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2386
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2387 static size_t
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2388 dhcp_message_new(struct bootp **bootp,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2389 const struct in_addr *addr, const struct in_addr *mask)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2390 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2391 uint8_t *p;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2392 uint32_t cookie;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2393
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2394 if ((*bootp = calloc(1, sizeof(**bootp))) == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2395 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2396
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2397 (*bootp)->yiaddr = addr->s_addr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2398 p = (*bootp)->vend;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2399
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2400 cookie = htonl(MAGIC_COOKIE);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2401 memcpy(p, &cookie, sizeof(cookie));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2402 p += sizeof(cookie);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2403
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2404 if (mask->s_addr != INADDR_ANY) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2405 *p++ = DHO_SUBNETMASK;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2406 *p++ = sizeof(mask->s_addr);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2407 memcpy(p, &mask->s_addr, sizeof(mask->s_addr));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2408 p+= sizeof(mask->s_addr);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2409 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2410
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2411 *p = DHO_END;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2412 return sizeof(**bootp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2413 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2414
4587
02c41622154f NetBSD: Allow dhcpcd to be built without ARP support
Roy Marples <roy@marples.name>
parents: 4585
diff changeset
2415 #if defined(ARP) || defined(KERNEL_RFC5227)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2416 static int
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2417 dhcp_arp_address(struct interface *ifp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2418 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2419 struct dhcp_state *state;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2420 struct in_addr addr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2421 struct ipv4_addr *ia;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2422
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2423 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2424
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2425 state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2426 addr.s_addr = state->offer->yiaddr == INADDR_ANY ?
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2427 state->offer->ciaddr : state->offer->yiaddr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2428 /* If the interface already has the address configured
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2429 * then we can't ARP for duplicate detection. */
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2430 ia = ipv4_iffindaddr(ifp, &addr, NULL);
4481
7848309c100c sun: Don't define IN_IFF_TENTATIVE or IN_IFF_DETACHED
Roy Marples <roy@marples.name>
parents: 4459
diff changeset
2431 #ifdef IN_IFF_NOTUSEABLE
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2432 if (ia == NULL || ia->addr_flags & IN_IFF_NOTUSEABLE) {
4191
30f5f070a4aa dhcp: set probe state only when probing.
Roy Marples <roy@marples.name>
parents: 4188
diff changeset
2433 state->state = DHS_PROBE;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2434 if (ia == NULL) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2435 struct dhcp_lease l;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2436
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2437 get_lease(ifp, &l, state->offer, state->offer_len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2438 /* Add the address now, let the kernel handle DAD. */
4555
93b4eb29d297 DHCP: Set address vltime and pltime to the length of the lease
Roy Marples <roy@marples.name>
parents: 4549
diff changeset
2439 ipv4_addaddr(ifp, &l.addr, &l.mask, &l.brd,
4562
559c1ce40f54 Use rebindtime rather than renewaltime.
Roy Marples <roy@marples.name>
parents: 4561
diff changeset
2440 l.leasetime, l.rebindtime);
4731
f421b59ced72 DHCP: Don't start DHCP if we fail DAD for static or informed addrs
Roy Marples <roy@marples.name>
parents: 4726
diff changeset
2441 } else if (ia->addr_flags & IN_IFF_DUPLICATED)
f421b59ced72 DHCP: Don't start DHCP if we fail DAD for static or informed addrs
Roy Marples <roy@marples.name>
parents: 4726
diff changeset
2442 dhcp_addr_duplicated(ifp, &ia->addr);
f421b59ced72 DHCP: Don't start DHCP if we fail DAD for static or informed addrs
Roy Marples <roy@marples.name>
parents: 4726
diff changeset
2443 else
4024
0984f93d896e To be consitent, do the same to loginfo as we just did for logdebug.
Roy Marples <roy@marples.name>
parents: 4023
diff changeset
2444 loginfox("%s: waiting for DAD on %s",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2445 ifp->name, inet_ntoa(addr));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2446 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2447 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2448 #else
4630
fe510f5ee194 ARP: Respect IFF_NOARP interface flag more
Roy Marples <roy@marples.name>
parents: 4617
diff changeset
2449 if (!(ifp->flags & IFF_NOARP) &&
5002
d3925b512c41 DHCP: Defend already configured static addresses
Roy Marples <roy@marples.name>
parents: 4999
diff changeset
2450 ifp->options->options & DHCPCD_ARP)
4630
fe510f5ee194 ARP: Respect IFF_NOARP interface flag more
Roy Marples <roy@marples.name>
parents: 4617
diff changeset
2451 {
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2452 struct arp_state *astate;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2453 struct dhcp_lease l;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2454
5002
d3925b512c41 DHCP: Defend already configured static addresses
Roy Marples <roy@marples.name>
parents: 4999
diff changeset
2455 /* Even if the address exists, we need to defend it. */
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2456 astate = dhcp_arp_new(ifp, &addr);
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2457 if (astate == NULL)
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2458 return -1;
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2459
5002
d3925b512c41 DHCP: Defend already configured static addresses
Roy Marples <roy@marples.name>
parents: 4999
diff changeset
2460 if (ia == NULL) {
d3925b512c41 DHCP: Defend already configured static addresses
Roy Marples <roy@marples.name>
parents: 4999
diff changeset
2461 state->state = DHS_PROBE;
d3925b512c41 DHCP: Defend already configured static addresses
Roy Marples <roy@marples.name>
parents: 4999
diff changeset
2462 get_lease(ifp, &l, state->offer, state->offer_len);
d3925b512c41 DHCP: Defend already configured static addresses
Roy Marples <roy@marples.name>
parents: 4999
diff changeset
2463 loginfox("%s: probing address %s/%d",
d3925b512c41 DHCP: Defend already configured static addresses
Roy Marples <roy@marples.name>
parents: 4999
diff changeset
2464 ifp->name, inet_ntoa(l.addr), inet_ntocidr(l.mask));
d3925b512c41 DHCP: Defend already configured static addresses
Roy Marples <roy@marples.name>
parents: 4999
diff changeset
2465 /* We need to handle DAD. */
d3925b512c41 DHCP: Defend already configured static addresses
Roy Marples <roy@marples.name>
parents: 4999
diff changeset
2466 arp_probe(astate);
d3925b512c41 DHCP: Defend already configured static addresses
Roy Marples <roy@marples.name>
parents: 4999
diff changeset
2467 return 0;
d3925b512c41 DHCP: Defend already configured static addresses
Roy Marples <roy@marples.name>
parents: 4999
diff changeset
2468 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2469 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2470 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2471
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2472 return 1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2473 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2474
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2475 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2476 dhcp_arp_bind(struct interface *ifp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2477 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2478
4425
7cd9fb2bbbe3 DHCP: Dont really add static address in test mode
Roy Marples <roy@marples.name>
parents: 4377
diff changeset
2479 if (ifp->ctx->options & DHCPCD_TEST ||
7cd9fb2bbbe3 DHCP: Dont really add static address in test mode
Roy Marples <roy@marples.name>
parents: 4377
diff changeset
2480 dhcp_arp_address(ifp) == 1)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2481 dhcp_bind(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2482 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2483 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2484
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2485 static void
5426
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2486 dhcp_lastlease(void *arg)
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2487 {
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2488 struct interface *ifp = arg;
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2489 struct dhcp_state *state = D_STATE(ifp);
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2490
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2491 loginfox("%s: timed out contacting a DHCP server, using last lease",
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2492 ifp->name);
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2493 #if defined(ARP) || defined(KERNEL_RFC5227)
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2494 dhcp_arp_bind(ifp);
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2495 #else
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2496 dhcp_bind(ifp);
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2497 #endif
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2498 /* Set expired here because dhcp_bind() -> ipv4_addaddr() will reset
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2499 * state */
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2500 state->added |= STATE_EXPIRED;
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2501 state->interval = 0;
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2502 dhcp_discover(ifp);
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2503 }
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2504
edcbeacf2225 DHCP: Request offered IP when using lastlease
Roy Marples <roy@marples.name>
parents: 5424
diff changeset
2505 static void
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2506 dhcp_static(struct interface *ifp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2507 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2508 struct if_options *ifo;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2509 struct dhcp_state *state;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2510 struct ipv4_addr *ia;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2511
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2512 state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2513 ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2514
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2515 ia = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2516 if (ifo->req_addr.s_addr == INADDR_ANY &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2517 (ia = ipv4_iffindaddr(ifp, NULL, NULL)) == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2518 {
4024
0984f93d896e To be consitent, do the same to loginfo as we just did for logdebug.
Roy Marples <roy@marples.name>
parents: 4023
diff changeset
2519 loginfox("%s: waiting for 3rd party to "
3960
42a6a3d1596e Remove custom logger and output all diagnostics via syslog(3).
Roy Marples <roy@marples.name>
parents: 3932
diff changeset
2520 "configure IP address", ifp->name);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2521 state->reason = "3RDPARTY";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2522 script_runreason(ifp, state->reason);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2523 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2524 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2525
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2526 state->offer_len = dhcp_message_new(&state->offer,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2527 ia ? &ia->addr : &ifo->req_addr,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2528 ia ? &ia->mask : &ifo->req_mask);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2529 if (state->offer_len)
4587
02c41622154f NetBSD: Allow dhcpcd to be built without ARP support
Roy Marples <roy@marples.name>
parents: 4585
diff changeset
2530 #if defined(ARP) || defined(KERNEL_RFC5227)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2531 dhcp_arp_bind(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2532 #else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2533 dhcp_bind(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2534 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2535 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2536
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2537 void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2538 dhcp_inform(struct interface *ifp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2539 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2540 struct dhcp_state *state;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2541 struct if_options *ifo;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2542 struct ipv4_addr *ia;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2543
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2544 state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2545 ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2546
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2547 state->state = DHS_INFORM;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2548 free(state->offer);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2549 state->offer = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2550 state->offer_len = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2551
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2552 if (ifo->req_addr.s_addr == INADDR_ANY) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2553 ia = ipv4_iffindaddr(ifp, NULL, NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2554 if (ia == NULL) {
4024
0984f93d896e To be consitent, do the same to loginfo as we just did for logdebug.
Roy Marples <roy@marples.name>
parents: 4023
diff changeset
2555 loginfox("%s: waiting for 3rd party to "
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2556 "configure IP address",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2557 ifp->name);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2558 if (!(ifp->ctx->options & DHCPCD_TEST)) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2559 state->reason = "3RDPARTY";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2560 script_runreason(ifp, state->reason);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2561 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2562 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2563 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2564 } else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2565 ia = ipv4_iffindaddr(ifp, &ifo->req_addr, &ifo->req_mask);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2566 if (ia == NULL) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2567 if (ifp->ctx->options & DHCPCD_TEST) {
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2568 logerrx("%s: cannot add IP address in test mode",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2569 ifp->name);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2570 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2571 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2572 ia = ipv4_iffindaddr(ifp, &ifo->req_addr, NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2573 if (ia != NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2574 /* Netmask must be different, delete it. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2575 ipv4_deladdr(ia, 1);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2576 state->offer_len = dhcp_message_new(&state->offer,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2577 &ifo->req_addr, &ifo->req_mask);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2578 #ifdef ARP
4829
0609c95174a7 DHCP: Improve API for privsep
Roy Marples <roy@marples.name>
parents: 4825
diff changeset
2579 if (dhcp_arp_address(ifp) != 1)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2580 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2581 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2582 ia = ipv4_iffindaddr(ifp,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2583 &ifo->req_addr, &ifo->req_mask);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2584 assert(ia != NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2585 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2586 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2587
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2588 state->addr = ia;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2589 state->offer_len = dhcp_message_new(&state->offer,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2590 &ia->addr, &ia->mask);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2591 if (state->offer_len) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2592 dhcp_new_xid(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2593 get_lease(ifp, &state->lease, state->offer, state->offer_len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2594 send_inform(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2595 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2596 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2597
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2598 void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2599 dhcp_reboot_newopts(struct interface *ifp, unsigned long long oldopts)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2600 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2601 struct if_options *ifo;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2602 struct dhcp_state *state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2603
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2604 if (state == NULL || state->state == DHS_NONE)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2605 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2606 ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2607 if ((ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC) &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2608 (state->addr == NULL ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2609 state->addr->addr.s_addr != ifo->req_addr.s_addr)) ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2610 (oldopts & (DHCPCD_INFORM | DHCPCD_STATIC) &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2611 !(ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC))))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2612 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2613 dhcp_drop(ifp, "EXPIRE");
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2614 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2615 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2616
4168
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2617 #ifdef ARP
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2618 static int
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2619 dhcp_activeaddr(const struct interface *ifp, const struct in_addr *addr)
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2620 {
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2621 const struct interface *ifp1;
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2622 const struct dhcp_state *state;
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2623
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2624 TAILQ_FOREACH(ifp1, ifp->ctx->ifaces, next) {
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2625 if (ifp1 == ifp)
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2626 continue;
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2627 if ((state = D_CSTATE(ifp1)) == NULL)
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2628 continue;
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2629 switch(state->state) {
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2630 case DHS_REBOOT:
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2631 case DHS_RENEW:
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2632 case DHS_REBIND:
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2633 case DHS_BOUND:
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2634 case DHS_INFORM:
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2635 break;
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2636 default:
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2637 continue;
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2638 }
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2639 if (state->lease.addr.s_addr == addr->s_addr)
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2640 return 1;
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2641 }
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2642 return 0;
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2643 }
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2644 #endif
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2645
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2646 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2647 dhcp_reboot(struct interface *ifp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2648 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2649 struct if_options *ifo;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2650 struct dhcp_state *state = D_STATE(ifp);
4168
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2651 #ifdef ARP
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2652 struct ipv4_addr *ia;
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2653 #endif
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2654
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2655 if (state == NULL || state->state == DHS_NONE)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2656 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2657 ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2658 state->state = DHS_REBOOT;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2659 state->interval = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2660
5499
6a2da5651841 dhcpcd: Simplify the link handling even more
Roy Marples <roy@marples.name>
parents: 5497
diff changeset
2661 if (ifo->options & DHCPCD_LINK && !if_is_link_up(ifp)) {
4024
0984f93d896e To be consitent, do the same to loginfo as we just did for logdebug.
Roy Marples <roy@marples.name>
parents: 4023
diff changeset
2662 loginfox("%s: waiting for carrier", ifp->name);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2663 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2664 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2665 if (ifo->options & DHCPCD_STATIC) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2666 dhcp_static(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2667 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2668 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2669 if (ifo->options & DHCPCD_INFORM) {
4024
0984f93d896e To be consitent, do the same to loginfo as we just did for logdebug.
Roy Marples <roy@marples.name>
parents: 4023
diff changeset
2670 loginfox("%s: informing address of %s",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2671 ifp->name, inet_ntoa(state->lease.addr));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2672 dhcp_inform(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2673 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2674 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2675 if (ifo->reboot == 0 || state->offer == NULL) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2676 dhcp_discover(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2677 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2678 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2679 if (!IS_DHCP(state->offer))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2680 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2681
4024
0984f93d896e To be consitent, do the same to loginfo as we just did for logdebug.
Roy Marples <roy@marples.name>
parents: 4023
diff changeset
2682 loginfox("%s: rebinding lease of %s",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2683 ifp->name, inet_ntoa(state->lease.addr));
4168
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2684
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2685 #ifdef ARP
4810
88ffa4a9431f DHCP: When rebinding ensure we have a DHCP ARP state
Roy Marples <roy@marples.name>
parents: 4792
diff changeset
2686 #ifndef KERNEL_RFC5227
88ffa4a9431f DHCP: When rebinding ensure we have a DHCP ARP state
Roy Marples <roy@marples.name>
parents: 4792
diff changeset
2687 /* Create the DHCP ARP state so we can defend it. */
88ffa4a9431f DHCP: When rebinding ensure we have a DHCP ARP state
Roy Marples <roy@marples.name>
parents: 4792
diff changeset
2688 (void)dhcp_arp_new(ifp, &state->lease.addr);
88ffa4a9431f DHCP: When rebinding ensure we have a DHCP ARP state
Roy Marples <roy@marples.name>
parents: 4792
diff changeset
2689 #endif
88ffa4a9431f DHCP: When rebinding ensure we have a DHCP ARP state
Roy Marples <roy@marples.name>
parents: 4792
diff changeset
2690
4168
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2691 /* If the address exists on the interface and no other interface
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2692 * is currently using it then announce it to ensure this
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2693 * interface gets the reply. */
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2694 ia = ipv4_iffindaddr(ifp, &state->lease.addr, NULL);
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2695 if (ia != NULL &&
4332
8777d91277fc dhcp: Don't ARP announce bound addresses when testing
Roy Marples <roy@marples.name>
parents: 4299
diff changeset
2696 !(ifp->ctx->options & DHCPCD_TEST) &&
4168
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2697 #ifdef IN_IFF_NOTUSEABLE
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2698 !(ia->addr_flags & IN_IFF_NOTUSEABLE) &&
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2699 #endif
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2700 dhcp_activeaddr(ifp, &state->lease.addr) == 0)
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2701 arp_ifannounceaddr(ifp, &state->lease.addr);
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2702 #endif
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
2703
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2704 dhcp_new_xid(ifp);
4188
23ff91710a01 dhcp: Don't set ciaddr or unicast if the address we have is faked.
Roy Marples <roy@marples.name>
parents: 4187
diff changeset
2705 state->lease.server.s_addr = INADDR_ANY;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2706 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2707
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2708 #ifdef IPV4LL
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2709 /* Need to add this before dhcp_expire and friends. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2710 if (!ifo->fallback && ifo->options & DHCPCD_IPV4LL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2711 eloop_timeout_add_sec(ifp->ctx->eloop,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2712 ifo->reboot, ipv4ll_start, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2713 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2714
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2715 if (ifo->options & DHCPCD_LASTLEASE && state->lease.frominfo)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2716 eloop_timeout_add_sec(ifp->ctx->eloop,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2717 ifo->reboot, dhcp_lastlease, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2718 else if (!(ifo->options & DHCPCD_INFORM))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2719 eloop_timeout_add_sec(ifp->ctx->eloop,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2720 ifo->reboot, dhcp_expire, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2721
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2722 /* Don't bother ARP checking as the server could NAK us first.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2723 * Don't call dhcp_request as that would change the state */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2724 send_request(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2725 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2726
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2727 void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2728 dhcp_drop(struct interface *ifp, const char *reason)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2729 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2730 struct dhcp_state *state;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2731 #ifdef RELEASE_SLOW
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2732 struct timespec ts;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2733 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2734
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2735 state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2736 /* dhcp_start may just have been called and we don't yet have a state
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2737 * but we do have a timeout, so punt it. */
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2738 if (state == NULL || state->state == DHS_NONE) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2739 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2740 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2741 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2742
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2743 #ifdef ARP
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2744 if (state->addr != NULL)
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2745 arp_freeaddr(ifp, &state->addr->addr);
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2746 #endif
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2747 #ifdef ARPING
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2748 state->arping_index = -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2749 #endif
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
2750
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2751 if (ifp->options->options & DHCPCD_RELEASE &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2752 !(ifp->options->options & DHCPCD_INFORM))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2753 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2754 /* Failure to send the release may cause this function to
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2755 * re-enter so guard by setting the state. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2756 if (state->state == DHS_RELEASE)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2757 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2758 state->state = DHS_RELEASE;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2759
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
2760 dhcp_unlink(ifp->ctx, state->leasefile);
5499
6a2da5651841 dhcpcd: Simplify the link handling even more
Roy Marples <roy@marples.name>
parents: 5497
diff changeset
2761 if (if_is_link_up(ifp) &&
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2762 state->new != NULL &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2763 state->lease.server.s_addr != INADDR_ANY)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2764 {
4024
0984f93d896e To be consitent, do the same to loginfo as we just did for logdebug.
Roy Marples <roy@marples.name>
parents: 4023
diff changeset
2765 loginfox("%s: releasing lease of %s",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2766 ifp->name, inet_ntoa(state->lease.addr));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2767 dhcp_new_xid(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2768 send_message(ifp, DHCP_RELEASE, NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2769 #ifdef RELEASE_SLOW
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2770 /* Give the packet a chance to go */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2771 ts.tv_sec = RELEASE_DELAY_S;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2772 ts.tv_nsec = RELEASE_DELAY_NS;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2773 nanosleep(&ts, NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2774 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2775 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2776 }
5294
942ddb46cd55 auth: Only accept RECONFIGURE messages from LL hosts
Roy Marples <roy@marples.name>
parents: 5288
diff changeset
2777 #ifdef AUTH
942ddb46cd55 auth: Only accept RECONFIGURE messages from LL hosts
Roy Marples <roy@marples.name>
parents: 5288
diff changeset
2778 else if (state->auth.reconf != NULL) {
942ddb46cd55 auth: Only accept RECONFIGURE messages from LL hosts
Roy Marples <roy@marples.name>
parents: 5288
diff changeset
2779 /*
942ddb46cd55 auth: Only accept RECONFIGURE messages from LL hosts
Roy Marples <roy@marples.name>
parents: 5288
diff changeset
2780 * Drop the lease as the token may only be present
942ddb46cd55 auth: Only accept RECONFIGURE messages from LL hosts
Roy Marples <roy@marples.name>
parents: 5288
diff changeset
2781 * in the initial reply message and not subsequent
942ddb46cd55 auth: Only accept RECONFIGURE messages from LL hosts
Roy Marples <roy@marples.name>
parents: 5288
diff changeset
2782 * renewals.
942ddb46cd55 auth: Only accept RECONFIGURE messages from LL hosts
Roy Marples <roy@marples.name>
parents: 5288
diff changeset
2783 * If dhcpcd is restarted, the token is lost.
942ddb46cd55 auth: Only accept RECONFIGURE messages from LL hosts
Roy Marples <roy@marples.name>
parents: 5288
diff changeset
2784 * XXX persist this in another file?
942ddb46cd55 auth: Only accept RECONFIGURE messages from LL hosts
Roy Marples <roy@marples.name>
parents: 5288
diff changeset
2785 */
942ddb46cd55 auth: Only accept RECONFIGURE messages from LL hosts
Roy Marples <roy@marples.name>
parents: 5288
diff changeset
2786 dhcp_unlink(ifp->ctx, state->leasefile);
942ddb46cd55 auth: Only accept RECONFIGURE messages from LL hosts
Roy Marples <roy@marples.name>
parents: 5288
diff changeset
2787 }
942ddb46cd55 auth: Only accept RECONFIGURE messages from LL hosts
Roy Marples <roy@marples.name>
parents: 5288
diff changeset
2788 #endif
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2789
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2790 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2791 #ifdef AUTH
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2792 dhcp_auth_reset(&state->auth);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2793 #endif
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2794
5176
9f2e260a7369 DHCP: Close sockets on carrier down
Roy Marples <roy@marples.name>
parents: 5166
diff changeset
2795 /* Close DHCP ports so a changed interface family is picked
9f2e260a7369 DHCP: Close sockets on carrier down
Roy Marples <roy@marples.name>
parents: 5166
diff changeset
2796 * up by a new BPF state. */
9f2e260a7369 DHCP: Close sockets on carrier down
Roy Marples <roy@marples.name>
parents: 5166
diff changeset
2797 dhcp_close(ifp);
9f2e260a7369 DHCP: Close sockets on carrier down
Roy Marples <roy@marples.name>
parents: 5166
diff changeset
2798
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2799 state->state = DHS_NONE;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2800 free(state->offer);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2801 state->offer = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2802 state->offer_len = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2803 free(state->old);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2804 state->old = state->new;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2805 state->old_len = state->new_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2806 state->new = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2807 state->new_len = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2808 state->reason = reason;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2809 ipv4_applyaddr(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2810 free(state->old);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2811 state->old = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2812 state->old_len = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2813 state->lease.addr.s_addr = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2814 ifp->options->options &= ~(DHCPCD_CSR_WARNED |
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2815 DHCPCD_ROUTER_HOST_ROUTE_WARNED);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2816 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2817
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2818 static int
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2819 blacklisted_ip(const struct if_options *ifo, in_addr_t addr)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2820 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2821 size_t i;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2822
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2823 for (i = 0; i < ifo->blacklist_len; i += 2)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2824 if (ifo->blacklist[i] == (addr & ifo->blacklist[i + 1]))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2825 return 1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2826 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2827 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2828
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2829 #define WHTLST_NONE 0
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2830 #define WHTLST_MATCH 1
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2831 #define WHTLST_NOMATCH 2
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2832 static unsigned int
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2833 whitelisted_ip(const struct if_options *ifo, in_addr_t addr)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2834 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2835 size_t i;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2836
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2837 if (ifo->whitelist_len == 0)
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2838 return WHTLST_NONE;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2839 for (i = 0; i < ifo->whitelist_len; i += 2)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2840 if (ifo->whitelist[i] == (addr & ifo->whitelist[i + 1]))
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2841 return WHTLST_MATCH;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2842 return WHTLST_NOMATCH;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2843 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2844
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2845 static void
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
2846 log_dhcp(int loglevel, const char *msg,
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2847 const struct interface *ifp, const struct bootp *bootp, size_t bootp_len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2848 const struct in_addr *from, int ad)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2849 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2850 const char *tfrom;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2851 char *a, sname[sizeof(bootp->sname) * 4];
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2852 struct in_addr addr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2853 int r;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2854 uint8_t overl;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2855
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2856 if (strcmp(msg, "NAK:") == 0) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2857 a = get_option_string(ifp->ctx, bootp, bootp_len, DHO_MESSAGE);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2858 if (a) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2859 char *tmp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2860 size_t al, tmpl;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2861
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2862 al = strlen(a);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2863 tmpl = (al * 4) + 1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2864 tmp = malloc(tmpl);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2865 if (tmp == NULL) {
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2866 logerr(__func__);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2867 free(a);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2868 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2869 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2870 print_string(tmp, tmpl, OT_STRING, (uint8_t *)a, al);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2871 free(a);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2872 a = tmp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2873 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2874 } else if (ad && bootp->yiaddr != 0) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2875 addr.s_addr = bootp->yiaddr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2876 a = strdup(inet_ntoa(addr));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2877 if (a == NULL) {
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
2878 logerr(__func__);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2879 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2880 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2881 } else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2882 a = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2883
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2884 tfrom = "from";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2885 r = get_option_addr(ifp->ctx, &addr, bootp, bootp_len, DHO_SERVERID);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2886 if (get_option_uint8(ifp->ctx, &overl, bootp, bootp_len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2887 DHO_OPTSOVERLOADED) == -1)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2888 overl = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2889 if (bootp->sname[0] && r == 0 && !(overl & 2)) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2890 print_string(sname, sizeof(sname), OT_STRING | OT_DOMAIN,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2891 bootp->sname, sizeof(bootp->sname));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2892 if (a == NULL)
5494
0fbde4769bbe Don't log backticks.
Roy Marples <roy@marples.name>
parents: 5489
diff changeset
2893 logmessage(loglevel, "%s: %s %s %s %s",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2894 ifp->name, msg, tfrom, inet_ntoa(addr), sname);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2895 else
5494
0fbde4769bbe Don't log backticks.
Roy Marples <roy@marples.name>
parents: 5489
diff changeset
2896 logmessage(loglevel, "%s: %s %s %s %s %s",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2897 ifp->name, msg, a, tfrom, inet_ntoa(addr), sname);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2898 } else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2899 if (r != 0) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2900 tfrom = "via";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2901 addr = *from;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2902 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2903 if (a == NULL)
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
2904 logmessage(loglevel, "%s: %s %s %s",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2905 ifp->name, msg, tfrom, inet_ntoa(addr));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2906 else
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
2907 logmessage(loglevel, "%s: %s %s %s %s",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2908 ifp->name, msg, a, tfrom, inet_ntoa(addr));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2909 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2910 free(a);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2911 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2912
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2913 /* If we're sharing the same IP address with another interface on the
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2914 * same network, we may receive the DHCP reply on the wrong interface.
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2915 * Try and re-direct it here. */
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2916 static void
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2917 dhcp_redirect_dhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len,
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2918 const struct in_addr *from)
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2919 {
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2920 struct interface *ifn;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2921 const struct dhcp_state *state;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2922 uint32_t xid;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2923
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2924 xid = ntohl(bootp->xid);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2925 TAILQ_FOREACH(ifn, ifp->ctx->ifaces, next) {
5009
0f76dcb791a4 DHCP: redirect message to other interfaces if received for inactive
Roy Marples <roy@marples.name>
parents: 5002
diff changeset
2926 if (ifn == ifp)
0f76dcb791a4 DHCP: redirect message to other interfaces if received for inactive
Roy Marples <roy@marples.name>
parents: 5002
diff changeset
2927 continue;
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2928 state = D_CSTATE(ifn);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2929 if (state == NULL || state->state == DHS_NONE)
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2930 continue;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2931 if (state->xid != xid)
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2932 continue;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2933 if (ifn->hwlen <= sizeof(bootp->chaddr) &&
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2934 memcmp(bootp->chaddr, ifn->hwaddr, ifn->hwlen))
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2935 continue;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2936 logdebugx("%s: redirecting DHCP message to %s",
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2937 ifp->name, ifn->name);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2938 dhcp_handledhcp(ifn, bootp, bootp_len, from);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2939 }
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2940 }
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2941
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2942 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2943 dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2944 const struct in_addr *from)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2945 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2946 struct dhcp_state *state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2947 struct if_options *ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2948 struct dhcp_lease *lease = &state->lease;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2949 uint8_t type, tmp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2950 struct in_addr addr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2951 unsigned int i;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2952 char *msg;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2953 bool bootp_copied;
5524
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
2954 uint32_t v6only_time = 0;
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
2955 bool use_v6only = false;
3989
b05654495fd8 If auth is disabled, then disable FORCERENEW and RECONFIGURE_ACCEPT
Roy Marples <roy@marples.name>
parents: 3970
diff changeset
2956 #ifdef AUTH
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2957 const uint8_t *auth;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2958 size_t auth_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2959 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2960 #ifdef IN_IFF_DUPLICATED
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2961 struct ipv4_addr *ia;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2962 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2963
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2964 #define LOGDHCP0(l, m) \
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2965 log_dhcp((l), (m), ifp, bootp, bootp_len, from, 0)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2966 #define LOGDHCP(l, m) \
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2967 log_dhcp((l), (m), ifp, bootp, bootp_len, from, 1)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2968
4736
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2969 #define IS_STATE_ACTIVE(s) ((s)-state != DHS_NONE && \
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2970 (s)->state != DHS_INIT && (s)->state != DHS_BOUND)
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2971
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2972 if (bootp->op != BOOTREPLY) {
4736
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2973 if (IS_STATE_ACTIVE(state))
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2974 logdebugx("%s: op (%d) is not BOOTREPLY",
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2975 ifp->name, bootp->op);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2976 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2977 }
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2978
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2979 if (state->xid != ntohl(bootp->xid)) {
4736
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2980 if (IS_STATE_ACTIVE(state))
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2981 logdebugx("%s: wrong xid 0x%x (expecting 0x%x) from %s",
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2982 ifp->name, ntohl(bootp->xid), state->xid,
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2983 inet_ntoa(*from));
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2984 dhcp_redirect_dhcp(ifp, bootp, bootp_len, from);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2985 return;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2986 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2987
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2988 if (ifp->hwlen <= sizeof(bootp->chaddr) &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2989 memcmp(bootp->chaddr, ifp->hwaddr, ifp->hwlen))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
2990 {
4736
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2991 if (IS_STATE_ACTIVE(state)) {
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2992 char buf[sizeof(bootp->chaddr) * 3];
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2993
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2994 logdebugx("%s: xid 0x%x is for hwaddr %s",
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2995 ifp->name, ntohl(bootp->xid),
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2996 hwaddr_ntoa(bootp->chaddr, sizeof(bootp->chaddr),
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2997 buf, sizeof(buf)));
4104f563fcc3 DHCP: Don't log diagnostics when state is not running
Roy Marples <roy@marples.name>
parents: 4731
diff changeset
2998 }
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
2999 dhcp_redirect_dhcp(ifp, bootp, bootp_len, from);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3000 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3001 }
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3002
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3003 if (!ifp->active)
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3004 return;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3005
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3006 i = whitelisted_ip(ifp->options, from->s_addr);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3007 switch (i) {
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3008 case WHTLST_NOMATCH:
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3009 logwarnx("%s: non whitelisted DHCP packet from %s",
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3010 ifp->name, inet_ntoa(*from));
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3011 return;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3012 case WHTLST_MATCH:
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3013 break;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3014 case WHTLST_NONE:
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3015 if (blacklisted_ip(ifp->options, from->s_addr) == 1) {
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3016 logwarnx("%s: blacklisted DHCP packet from %s",
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3017 ifp->name, inet_ntoa(*from));
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3018 return;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3019 }
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3020 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3021
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3022 /* We may have found a BOOTP server */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3023 if (get_option_uint8(ifp->ctx, &type,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3024 bootp, bootp_len, DHO_MESSAGETYPE) == -1)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3025 type = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3026 else if (ifo->options & DHCPCD_BOOTP) {
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
3027 logdebugx("%s: ignoring DHCP reply (expecting BOOTP)",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3028 ifp->name);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3029 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3030 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3031
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3032 #ifdef AUTH
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3033 /* Authenticate the message */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3034 auth = get_option(ifp->ctx, bootp, bootp_len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3035 DHO_AUTHENTICATION, &auth_len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3036 if (auth) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3037 if (dhcp_auth_validate(&state->auth, &ifo->auth,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3038 (uint8_t *)bootp, bootp_len, 4, type,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3039 auth, auth_len) == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3040 {
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3041 LOGDHCP0(LOG_ERR, "authentication failed");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3042 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3043 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3044 if (state->auth.token)
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
3045 logdebugx("%s: validated using 0x%08" PRIu32,
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3046 ifp->name, state->auth.token->secretid);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3047 else
4024
0984f93d896e To be consitent, do the same to loginfo as we just did for logdebug.
Roy Marples <roy@marples.name>
parents: 4023
diff changeset
3048 loginfox("%s: accepted reconfigure key", ifp->name);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3049 } else if (ifo->auth.options & DHCPCD_AUTH_SEND) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3050 if (ifo->auth.options & DHCPCD_AUTH_REQUIRE) {
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3051 LOGDHCP0(LOG_ERR, "no authentication");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3052 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3053 }
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3054 LOGDHCP0(LOG_WARNING, "no authentication");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3055 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3056 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3057
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3058 /* RFC 3203 */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3059 if (type == DHCP_FORCERENEW) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3060 if (from->s_addr == INADDR_ANY ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3061 from->s_addr == INADDR_BROADCAST)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3062 {
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3063 LOGDHCP(LOG_ERR, "discarding Force Renew");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3064 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3065 }
3989
b05654495fd8 If auth is disabled, then disable FORCERENEW and RECONFIGURE_ACCEPT
Roy Marples <roy@marples.name>
parents: 3970
diff changeset
3066 #ifdef AUTH
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3067 if (auth == NULL) {
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3068 LOGDHCP(LOG_ERR, "unauthenticated Force Renew");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3069 if (ifo->auth.options & DHCPCD_AUTH_REQUIRE)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3070 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3071 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3072 if (state->state != DHS_BOUND && state->state != DHS_INFORM) {
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3073 LOGDHCP(LOG_DEBUG, "not bound, ignoring Force Renew");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3074 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3075 }
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3076 LOGDHCP(LOG_INFO, "Force Renew from");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3077 /* The rebind and expire timings are still the same, we just
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3078 * enter the renew state early */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3079 if (state->state == DHS_BOUND)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3080 dhcp_renew(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3081 else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3082 eloop_timeout_delete(ifp->ctx->eloop,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3083 send_inform, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3084 dhcp_inform(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3085 }
3989
b05654495fd8 If auth is disabled, then disable FORCERENEW and RECONFIGURE_ACCEPT
Roy Marples <roy@marples.name>
parents: 3970
diff changeset
3086 #else
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3087 LOGDHCP(LOG_ERR, "unauthenticated Force Renew");
3989
b05654495fd8 If auth is disabled, then disable FORCERENEW and RECONFIGURE_ACCEPT
Roy Marples <roy@marples.name>
parents: 3970
diff changeset
3088 #endif
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3089 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3090 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3091
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3092 if (state->state == DHS_BOUND) {
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3093 LOGDHCP(LOG_DEBUG, "bound, ignoring");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3094 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3095 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3096
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3097 if (state->state == DHS_PROBE) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3098 /* Ignore any DHCP messages whilst probing a lease to bind. */
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3099 LOGDHCP(LOG_DEBUG, "probing, ignoring");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3100 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3101 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3102
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3103 /* reset the message counter */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3104 state->interval = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3105
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3106 /* Ensure that no reject options are present */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3107 for (i = 1; i < 255; i++) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3108 if (has_option_mask(ifo->rejectmask, i) &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3109 get_option_uint8(ifp->ctx, &tmp,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3110 bootp, bootp_len, (uint8_t)i) == 0)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3111 {
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3112 LOGDHCP(LOG_WARNING, "reject DHCP");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3113 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3114 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3115 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3116
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3117 if (type == DHCP_NAK) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3118 /* For NAK, only check if we require the ServerID */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3119 if (has_option_mask(ifo->requiremask, DHO_SERVERID) &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3120 get_option_addr(ifp->ctx, &addr,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3121 bootp, bootp_len, DHO_SERVERID) == -1)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3122 {
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3123 LOGDHCP(LOG_WARNING, "reject NAK");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3124 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3125 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3126
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3127 /* We should restart on a NAK */
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3128 LOGDHCP(LOG_WARNING, "NAK:");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3129 if ((msg = get_option_string(ifp->ctx,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3130 bootp, bootp_len, DHO_MESSAGE)))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3131 {
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
3132 logwarnx("%s: message: %s", ifp->name, msg);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3133 free(msg);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3134 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3135 if (state->state == DHS_INFORM) /* INFORM should not be NAKed */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3136 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3137 if (!(ifp->ctx->options & DHCPCD_TEST)) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3138 dhcp_drop(ifp, "NAK");
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
3139 dhcp_unlink(ifp->ctx, state->leasefile);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3140 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3141
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3142 /* If we constantly get NAKS then we should slowly back off */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3143 eloop_timeout_add_sec(ifp->ctx->eloop,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3144 state->nakoff, dhcp_discover, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3145 if (state->nakoff == 0)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3146 state->nakoff = 1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3147 else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3148 state->nakoff *= 2;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3149 if (state->nakoff > NAKOFF_MAX)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3150 state->nakoff = NAKOFF_MAX;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3151 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3152 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3153 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3154
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3155 /* Ensure that all required options are present */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3156 for (i = 1; i < 255; i++) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3157 if (has_option_mask(ifo->requiremask, i) &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3158 get_option_uint8(ifp->ctx, &tmp,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3159 bootp, bootp_len, (uint8_t)i) != 0)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3160 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3161 /* If we are BOOTP, then ignore the need for serverid.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3162 * To ignore BOOTP, require dhcp_message_type.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3163 * However, nothing really stops BOOTP from providing
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3164 * DHCP style options as well so the above isn't
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3165 * always true. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3166 if (type == 0 && i == DHO_SERVERID)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3167 continue;
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3168 LOGDHCP(LOG_WARNING, "reject DHCP");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3169 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3170 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3171 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3172
5524
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3173 if (has_option_mask(ifo->requestmask, DHO_IPV6_PREFERRED_ONLY)) {
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3174 if (get_option_uint32(ifp->ctx, &v6only_time, bootp, bootp_len,
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3175 DHO_IPV6_PREFERRED_ONLY) == 0 &&
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3176 (state->state == DHS_DISCOVER || state->state == DHS_REBOOT))
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3177 {
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3178 char v6msg[128];
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3179
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3180 use_v6only = true;
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3181 if (v6only_time < MIN_V6ONLY_WAIT)
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3182 v6only_time = MIN_V6ONLY_WAIT;
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3183 snprintf(v6msg, sizeof(v6msg),
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3184 "IPv6-Only Preferred received (%u seconds)",
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3185 v6only_time);
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3186 LOGDHCP(LOG_INFO, v6msg);
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3187 }
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3188 }
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3189
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3190 /* DHCP Auto-Configure, RFC 2563 */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3191 if (type == DHCP_OFFER && bootp->yiaddr == 0) {
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3192 LOGDHCP(LOG_WARNING, "no address given");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3193 if ((msg = get_option_string(ifp->ctx,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3194 bootp, bootp_len, DHO_MESSAGE)))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3195 {
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
3196 logwarnx("%s: message: %s", ifp->name, msg);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3197 free(msg);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3198 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3199 #ifdef IPV4LL
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3200 if (state->state == DHS_DISCOVER &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3201 get_option_uint8(ifp->ctx, &tmp, bootp, bootp_len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3202 DHO_AUTOCONFIGURE) == 0)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3203 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3204 switch (tmp) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3205 case 0:
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3206 LOGDHCP(LOG_WARNING, "IPv4LL disabled from");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3207 ipv4ll_drop(ifp);
4135
64796240ee75 arp: RFC5227 kernels need to send ARP too
Roy Marples <roy@marples.name>
parents: 4125
diff changeset
3208 #ifdef ARP
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3209 arp_drop(ifp);
4135
64796240ee75 arp: RFC5227 kernels need to send ARP too
Roy Marples <roy@marples.name>
parents: 4125
diff changeset
3210 #endif
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3211 break;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3212 case 1:
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3213 LOGDHCP(LOG_WARNING, "IPv4LL enabled from");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3214 ipv4ll_start(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3215 break;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3216 default:
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
3217 logerrx("%s: unknown auto configuration "
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
3218 "option %d",
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3219 ifp->name, tmp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3220 break;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3221 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3222 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3223 eloop_timeout_add_sec(ifp->ctx->eloop,
5524
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3224 use_v6only ? v6only_time : DHCP_MAX,
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3225 dhcp_discover, ifp);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3226 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3227 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3228 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3229 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3230
5524
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3231 if (use_v6only) {
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3232 dhcp_drop(ifp, "EXPIRE");
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3233 dhcp_unlink(ifp->ctx, state->leasefile);
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3234 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3235 eloop_timeout_add_sec(ifp->ctx->eloop, v6only_time,
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3236 dhcp_discover, ifp);
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3237 return;
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3238 }
8c0e5dc34824 DHCP: Implement IPv6-Only Preferred option, RFC 8925.
Roy Marples <roy@marples.name>
parents: 5499
diff changeset
3239
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3240 /* Ensure that the address offered is valid */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3241 if ((type == 0 || type == DHCP_OFFER || type == DHCP_ACK) &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3242 (bootp->ciaddr == INADDR_ANY || bootp->ciaddr == INADDR_BROADCAST)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3243 &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3244 (bootp->yiaddr == INADDR_ANY || bootp->yiaddr == INADDR_BROADCAST))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3245 {
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3246 LOGDHCP(LOG_WARNING, "reject invalid address");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3247 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3248 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3249
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3250 #ifdef IN_IFF_DUPLICATED
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3251 ia = ipv4_iffindaddr(ifp, &lease->addr, NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3252 if (ia && ia->addr_flags & IN_IFF_DUPLICATED) {
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3253 LOGDHCP(LOG_WARNING, "declined duplicate address");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3254 if (type)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3255 dhcp_decline(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3256 ipv4_deladdr(ia, 0);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3257 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3258 eloop_timeout_add_sec(ifp->ctx->eloop,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3259 DHCP_RAND_MAX, dhcp_discover, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3260 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3261 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3262 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3263
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3264 bootp_copied = false;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3265 if ((type == 0 || type == DHCP_OFFER) && state->state == DHS_DISCOVER) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3266 lease->frominfo = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3267 lease->addr.s_addr = bootp->yiaddr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3268 memcpy(&lease->cookie, bootp->vend, sizeof(lease->cookie));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3269 if (type == 0 ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3270 get_option_addr(ifp->ctx,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3271 &lease->server, bootp, bootp_len, DHO_SERVERID) != 0)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3272 lease->server.s_addr = INADDR_ANY;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3273
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3274 /* Test for rapid commit in the OFFER */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3275 if (!(ifp->ctx->options & DHCPCD_TEST) &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3276 has_option_mask(ifo->requestmask, DHO_RAPIDCOMMIT) &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3277 get_option(ifp->ctx, bootp, bootp_len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3278 DHO_RAPIDCOMMIT, NULL))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3279 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3280 state->state = DHS_REQUEST;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3281 goto rapidcommit;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3282 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3283
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3284 LOGDHCP(LOG_INFO, "offered");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3285 if (state->offer_len < bootp_len) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3286 free(state->offer);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3287 if ((state->offer = malloc(bootp_len)) == NULL) {
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
3288 logerr(__func__);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3289 state->offer_len = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3290 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3291 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3292 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3293 state->offer_len = bootp_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3294 memcpy(state->offer, bootp, bootp_len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3295 bootp_copied = true;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3296 if (ifp->ctx->options & DHCPCD_TEST) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3297 free(state->old);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3298 state->old = state->new;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3299 state->old_len = state->new_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3300 state->new = state->offer;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3301 state->new_len = state->offer_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3302 state->offer = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3303 state->offer_len = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3304 state->reason = "TEST";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3305 script_runreason(ifp, state->reason);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3306 eloop_exit(ifp->ctx->eloop, EXIT_SUCCESS);
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3307 state->bpf->bpf_flags |= BPF_EOF;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3308 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3309 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3310 eloop_timeout_delete(ifp->ctx->eloop, send_discover, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3311 /* We don't request BOOTP addresses */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3312 if (type) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3313 /* We used to ARP check here, but that seems to be in
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3314 * violation of RFC2131 where it only describes
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3315 * DECLINE after REQUEST.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3316 * It also seems that some MS DHCP servers actually
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3317 * ignore DECLINE if no REQUEST, ie we decline a
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3318 * DISCOVER. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3319 dhcp_request(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3320 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3321 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3322 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3323
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3324 if (type) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3325 if (type == DHCP_OFFER) {
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3326 LOGDHCP(LOG_WARNING, "ignoring offer of");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3327 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3328 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3329
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3330 /* We should only be dealing with acks */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3331 if (type != DHCP_ACK) {
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3332 LOGDHCP(LOG_ERR, "not ACK or OFFER");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3333 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3334 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3335
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3336 if (state->state == DHS_DISCOVER) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3337 /* We only allow ACK of rapid commit DISCOVER. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3338 if (has_option_mask(ifo->requestmask,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3339 DHO_RAPIDCOMMIT) &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3340 get_option(ifp->ctx, bootp, bootp_len,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3341 DHO_RAPIDCOMMIT, NULL))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3342 state->state = DHS_REQUEST;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3343 else {
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3344 LOGDHCP(LOG_DEBUG, "ignoring ack of");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3345 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3346 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3347 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3348
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3349 rapidcommit:
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3350 if (!(ifo->options & DHCPCD_INFORM))
5064
7721231839f5 logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents: 5054
diff changeset
3351 LOGDHCP(LOG_DEBUG, "acknowledged");
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3352 else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3353 ifo->options &= ~DHCPCD_STATIC;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3354 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3355
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3356 /* No NAK, so reset the backoff
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3357 * We don't reset on an OFFER message because the server could
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3358 * potentially NAK the REQUEST. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3359 state->nakoff = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3360
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3361 /* BOOTP could have already assigned this above. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3362 if (!bootp_copied) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3363 if (state->offer_len < bootp_len) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3364 free(state->offer);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3365 if ((state->offer = malloc(bootp_len)) == NULL) {
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
3366 logerr(__func__);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3367 state->offer_len = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3368 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3369 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3370 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3371 state->offer_len = bootp_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3372 memcpy(state->offer, bootp, bootp_len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3373 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3374
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3375 lease->frominfo = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3376 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3377
4587
02c41622154f NetBSD: Allow dhcpcd to be built without ARP support
Roy Marples <roy@marples.name>
parents: 4585
diff changeset
3378 #if defined(ARP) || defined(KERNEL_RFC5227)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3379 dhcp_arp_bind(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3380 #else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3381 dhcp_bind(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3382 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3383 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3384
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3385 static void *
4605
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3386 get_udp_data(void *packet, size_t *len)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3387 {
4605
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3388 const struct ip *ip = packet;
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3389 size_t ip_hl = (size_t)ip->ip_hl * 4;
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3390 char *p = packet;
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3391
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3392 p += ip_hl + sizeof(struct udphdr);
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3393 *len = (size_t)ntohs(ip->ip_len) - sizeof(struct udphdr) - ip_hl;
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3394 return p;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3395 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3396
4757
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3397 static bool
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3398 is_packet_udp_bootp(void *packet, size_t plen)
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3399 {
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3400 struct ip *ip = packet;
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3401 size_t ip_hlen;
4768
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3402 struct udphdr udp;
4757
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3403
4761
6fc5fe895c67 dhcp: Fix incoming verification and checksum routines
Maxime Villard <max@m00nbsd.net>
parents: 4757
diff changeset
3404 if (plen < sizeof(*ip))
4757
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3405 return false;
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3406
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3407 if (ip->ip_v != IPVERSION || ip->ip_p != IPPROTO_UDP)
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3408 return false;
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3409
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3410 /* Sanity. */
4762
d68ac6b143c5 dhcp: Allow for trailing FCS in the packet.
Roy Marples <roy@marples.name>
parents: 4761
diff changeset
3411 if (ntohs(ip->ip_len) > plen)
4757
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3412 return false;
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3413
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3414 ip_hlen = (size_t)ip->ip_hl * 4;
4761
6fc5fe895c67 dhcp: Fix incoming verification and checksum routines
Maxime Villard <max@m00nbsd.net>
parents: 4757
diff changeset
3415 if (ip_hlen < sizeof(*ip))
6fc5fe895c67 dhcp: Fix incoming verification and checksum routines
Maxime Villard <max@m00nbsd.net>
parents: 4757
diff changeset
3416 return false;
6fc5fe895c67 dhcp: Fix incoming verification and checksum routines
Maxime Villard <max@m00nbsd.net>
parents: 4757
diff changeset
3417
4757
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3418 /* Check we have a UDP header and BOOTP. */
4768
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3419 if (ip_hlen + sizeof(udp) + offsetof(struct bootp, vend) > plen)
4757
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3420 return false;
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3421
4761
6fc5fe895c67 dhcp: Fix incoming verification and checksum routines
Maxime Villard <max@m00nbsd.net>
parents: 4757
diff changeset
3422 /* Sanity. */
4768
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3423 memcpy(&udp, (char *)ip + ip_hlen, sizeof(udp));
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3424 if (ntohs(udp.uh_ulen) < sizeof(udp))
4761
6fc5fe895c67 dhcp: Fix incoming verification and checksum routines
Maxime Villard <max@m00nbsd.net>
parents: 4757
diff changeset
3425 return false;
5072
b1ab228ac3b3 Revert "dhcp: Cast away a compile warning"
Roy Marples <roy@marples.name>
parents: 5064
diff changeset
3426 if (ip_hlen + ntohs(udp.uh_ulen) > plen)
4761
6fc5fe895c67 dhcp: Fix incoming verification and checksum routines
Maxime Villard <max@m00nbsd.net>
parents: 4757
diff changeset
3427 return false;
6fc5fe895c67 dhcp: Fix incoming verification and checksum routines
Maxime Villard <max@m00nbsd.net>
parents: 4757
diff changeset
3428
4757
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3429 /* Check it's to and from the right ports. */
4768
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3430 if (udp.uh_dport != htons(BOOTPC) || udp.uh_sport != htons(BOOTPS))
4757
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3431 return false;
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3432
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3433 return true;
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3434 }
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3435
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3436 /* Lengths have already been checked. */
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3437 static bool
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3438 checksums_valid(void *packet,
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3439 struct in_addr *from, unsigned int flags)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3440 {
4605
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3441 struct ip *ip = packet;
4770
e9cb321935ac DHCP: Fix strict aliasing of checksumming the pseudo header
Roy Marples <roy@marples.name>
parents: 4768
diff changeset
3442 union pip {
e9cb321935ac DHCP: Fix strict aliasing of checksumming the pseudo header
Roy Marples <roy@marples.name>
parents: 4768
diff changeset
3443 struct ip ip;
4775
9a29b37bc5cb DHCP: Fix prior to not waste space
Sergei Trofimovich <slyfox@gentoo.org>
parents: 4773
diff changeset
3444 uint16_t w[sizeof(struct ip) / 2];
4770
e9cb321935ac DHCP: Fix strict aliasing of checksumming the pseudo header
Roy Marples <roy@marples.name>
parents: 4768
diff changeset
3445 } pip = {
5272
97e87c65302e Appease older compilers
Roy Marples <roy@marples.name>
parents: 5256
diff changeset
3446 .ip = {
97e87c65302e Appease older compilers
Roy Marples <roy@marples.name>
parents: 5256
diff changeset
3447 .ip_p = IPPROTO_UDP,
97e87c65302e Appease older compilers
Roy Marples <roy@marples.name>
parents: 5256
diff changeset
3448 .ip_src = ip->ip_src,
97e87c65302e Appease older compilers
Roy Marples <roy@marples.name>
parents: 5256
diff changeset
3449 .ip_dst = ip->ip_dst,
97e87c65302e Appease older compilers
Roy Marples <roy@marples.name>
parents: 5256
diff changeset
3450 }
4608
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
3451 };
4605
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3452 size_t ip_hlen;
4768
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3453 struct udphdr udp;
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3454 char *udpp, *uh_sump;
4608
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
3455 uint32_t csum;
4605
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3456
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3457 if (from != NULL)
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3458 from->s_addr = ip->ip_src.s_addr;
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3459
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3460 ip_hlen = (size_t)ip->ip_hl * 4;
4757
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3461 if (in_cksum(ip, ip_hlen, NULL) != 0)
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3462 return false;
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3463
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3464 if (flags & BPF_PARTIALCSUM)
4761
6fc5fe895c67 dhcp: Fix incoming verification and checksum routines
Maxime Villard <max@m00nbsd.net>
parents: 4757
diff changeset
3465 return true;
4757
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3466
4768
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3467 udpp = (char *)ip + ip_hlen;
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3468 memcpy(&udp, udpp, sizeof(udp));
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3469 if (udp.uh_sum == 0)
4761
6fc5fe895c67 dhcp: Fix incoming verification and checksum routines
Maxime Villard <max@m00nbsd.net>
parents: 4757
diff changeset
3470 return true;
4605
5ed975095c4e IP: Work with IP headers with options.
Roy Marples <roy@marples.name>
parents: 4602
diff changeset
3471
4608
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
3472 /* UDP checksum is based on a pseudo IP header alongside
3aef2b50ef03 DHCP: Rework checksuming so that the packet isn't touched.
Roy Marples <roy@marples.name>
parents: 4605
diff changeset
3473 * the UDP header and payload. */
4770
e9cb321935ac DHCP: Fix strict aliasing of checksumming the pseudo header
Roy Marples <roy@marples.name>
parents: 4768
diff changeset
3474 pip.ip.ip_len = udp.uh_ulen;
e9cb321935ac DHCP: Fix strict aliasing of checksumming the pseudo header
Roy Marples <roy@marples.name>
parents: 4768
diff changeset
3475 csum = 0;
4768
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3476
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3477 /* Need to zero the UDP sum in the packet for the checksum to work. */
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3478 uh_sump = udpp + offsetof(struct udphdr, uh_sum);
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3479 memset(uh_sump, 0, sizeof(udp.uh_sum));
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3480
4773
44414d81f0c1 Typo, thanks to Arfrever.
Roy Marples <roy@marples.name>
parents: 4770
diff changeset
3481 /* Checksum pseudo header and then UDP + payload. */
4770
e9cb321935ac DHCP: Fix strict aliasing of checksumming the pseudo header
Roy Marples <roy@marples.name>
parents: 4768
diff changeset
3482 in_cksum(pip.w, sizeof(pip.w), &csum);
e9cb321935ac DHCP: Fix strict aliasing of checksumming the pseudo header
Roy Marples <roy@marples.name>
parents: 4768
diff changeset
3483 csum = in_cksum(udpp, ntohs(udp.uh_ulen), &csum);
e9cb321935ac DHCP: Fix strict aliasing of checksumming the pseudo header
Roy Marples <roy@marples.name>
parents: 4768
diff changeset
3484
e9cb321935ac DHCP: Fix strict aliasing of checksumming the pseudo header
Roy Marples <roy@marples.name>
parents: 4768
diff changeset
3485 #if 0 /* Not needed, just here for completeness. */
4768
55d113e3c287 DHCP: Fix aliasing issues accessing the UDP header
Roy Marples <roy@marples.name>
parents: 4762
diff changeset
3486 /* Put the checksum back. */
4770
e9cb321935ac DHCP: Fix strict aliasing of checksumming the pseudo header
Roy Marples <roy@marples.name>
parents: 4768
diff changeset
3487 memcpy(uh_sump, &udp.uh_sum, sizeof(udp.uh_sum));
e9cb321935ac DHCP: Fix strict aliasing of checksumming the pseudo header
Roy Marples <roy@marples.name>
parents: 4768
diff changeset
3488 #endif
e9cb321935ac DHCP: Fix strict aliasing of checksumming the pseudo header
Roy Marples <roy@marples.name>
parents: 4768
diff changeset
3489
e9cb321935ac DHCP: Fix strict aliasing of checksumming the pseudo header
Roy Marples <roy@marples.name>
parents: 4768
diff changeset
3490 return csum == udp.uh_sum;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3491 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3492
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3493 static void
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3494 dhcp_handlebootp(struct interface *ifp, struct bootp *bootp, size_t len,
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3495 struct in_addr *from)
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3496 {
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3497 size_t v;
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3498
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3499 if (len < offsetof(struct bootp, vend)) {
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3500 logerrx("%s: truncated packet (%zu) from %s",
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3501 ifp->name, len, inet_ntoa(*from));
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3502 return;
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3503 }
4757
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3504
5079
415c98a69db0 DHCP: Move the max frame length check
Roy Marples <roy@marples.name>
parents: 5078
diff changeset
3505 /* Unlikely, but appeases sanitizers. */
415c98a69db0 DHCP: Move the max frame length check
Roy Marples <roy@marples.name>
parents: 5078
diff changeset
3506 if (len > FRAMELEN_MAX) {
415c98a69db0 DHCP: Move the max frame length check
Roy Marples <roy@marples.name>
parents: 5078
diff changeset
3507 logerrx("%s: packet exceeded frame length (%zu) from %s",
415c98a69db0 DHCP: Move the max frame length check
Roy Marples <roy@marples.name>
parents: 5078
diff changeset
3508 ifp->name, len, inet_ntoa(*from));
415c98a69db0 DHCP: Move the max frame length check
Roy Marples <roy@marples.name>
parents: 5078
diff changeset
3509 return;
415c98a69db0 DHCP: Move the max frame length check
Roy Marples <roy@marples.name>
parents: 5078
diff changeset
3510 }
415c98a69db0 DHCP: Move the max frame length check
Roy Marples <roy@marples.name>
parents: 5078
diff changeset
3511
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3512 /* To make our IS_DHCP macro easy, ensure the vendor
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3513 * area has at least 4 octets. */
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3514 v = len - offsetof(struct bootp, vend);
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3515 while (v < 4) {
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3516 bootp->vend[v++] = '\0';
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3517 len++;
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3518 }
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3519
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3520 dhcp_handledhcp(ifp, bootp, len, from);
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3521 }
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3522
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3523 void
5234
bcd021398c1d Fix compile on Linux
Roy Marples <roy@marples.name>
parents: 5233
diff changeset
3524 dhcp_packet(struct interface *ifp, uint8_t *data, size_t len,
bcd021398c1d Fix compile on Linux
Roy Marples <roy@marples.name>
parents: 5233
diff changeset
3525 unsigned int bpf_flags)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3526 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3527 struct bootp *bootp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3528 struct in_addr from;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3529 size_t udp_len;
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3530 size_t fl = bpf_frame_header_len(ifp);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3531 #ifdef PRIVSEP
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3532 const struct dhcp_state *state = D_CSTATE(ifp);
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3533
5489
a3d5af47a4e3 Add a comment to prior incase I do something dumb like removing it
Roy Marples <roy@marples.name>
parents: 5487
diff changeset
3534 /* It's possible that an interface departs and arrives in short
a3d5af47a4e3 Add a comment to prior incase I do something dumb like removing it
Roy Marples <roy@marples.name>
parents: 5487
diff changeset
3535 * order to receive a BPF frame out of order.
a3d5af47a4e3 Add a comment to prior incase I do something dumb like removing it
Roy Marples <roy@marples.name>
parents: 5487
diff changeset
3536 * There is a similar check in ARP, but much lower down the stack.
a3d5af47a4e3 Add a comment to prior incase I do something dumb like removing it
Roy Marples <roy@marples.name>
parents: 5487
diff changeset
3537 * It's not needed for other inet protocols because we send the
a3d5af47a4e3 Add a comment to prior incase I do something dumb like removing it
Roy Marples <roy@marples.name>
parents: 5487
diff changeset
3538 * message as a whole and select the interface off that and then
a3d5af47a4e3 Add a comment to prior incase I do something dumb like removing it
Roy Marples <roy@marples.name>
parents: 5487
diff changeset
3539 * check state. BPF on the other hand is very interface
a3d5af47a4e3 Add a comment to prior incase I do something dumb like removing it
Roy Marples <roy@marples.name>
parents: 5487
diff changeset
3540 * specific and we do need this check. */
5487
23f35ea1a34a privsep: fix crash when interface departs before bpf returns for it
Roy Marples <roy@marples.name>
parents: 5449
diff changeset
3541 if (state == NULL)
23f35ea1a34a privsep: fix crash when interface departs before bpf returns for it
Roy Marples <roy@marples.name>
parents: 5449
diff changeset
3542 return;
23f35ea1a34a privsep: fix crash when interface departs before bpf returns for it
Roy Marples <roy@marples.name>
parents: 5449
diff changeset
3543
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3544 /* Ignore double reads */
4868
119c8986dfc8 privsep: Enable ARP BPF filtering for interesting addresses
Roy Marples <roy@marples.name>
parents: 4866
diff changeset
3545 if (IN_PRIVSEP(ifp->ctx)) {
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3546 switch (state->state) {
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3547 case DHS_BOUND: /* FALLTHROUGH */
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3548 case DHS_RENEW:
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3549 return;
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3550 default:
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3551 break;
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3552 }
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3553 }
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3554 #endif
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3555
5033
d8f63eaa4d2b DHCP: Fix receiving BPF from privsep.
Roy Marples <roy@marples.name>
parents: 5028
diff changeset
3556 /* Trim frame header */
d8f63eaa4d2b DHCP: Fix receiving BPF from privsep.
Roy Marples <roy@marples.name>
parents: 5028
diff changeset
3557 if (fl != 0) {
d8f63eaa4d2b DHCP: Fix receiving BPF from privsep.
Roy Marples <roy@marples.name>
parents: 5028
diff changeset
3558 if (len < fl) {
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3559 logerrx("%s: %s: short frame header %zu",
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3560 __func__, ifp->name, len);
5033
d8f63eaa4d2b DHCP: Fix receiving BPF from privsep.
Roy Marples <roy@marples.name>
parents: 5028
diff changeset
3561 return;
d8f63eaa4d2b DHCP: Fix receiving BPF from privsep.
Roy Marples <roy@marples.name>
parents: 5028
diff changeset
3562 }
d8f63eaa4d2b DHCP: Fix receiving BPF from privsep.
Roy Marples <roy@marples.name>
parents: 5028
diff changeset
3563 len -= fl;
5158
5f18174b5eb2 DHCP: Avoid mis-aligned BOOTP structure
Roy Marples <roy@marples.name>
parents: 5120
diff changeset
3564 /* Move the data to avoid alignment errors. */
5f18174b5eb2 DHCP: Avoid mis-aligned BOOTP structure
Roy Marples <roy@marples.name>
parents: 5120
diff changeset
3565 memmove(data, data + fl, len);
5033
d8f63eaa4d2b DHCP: Fix receiving BPF from privsep.
Roy Marples <roy@marples.name>
parents: 5028
diff changeset
3566 }
d8f63eaa4d2b DHCP: Fix receiving BPF from privsep.
Roy Marples <roy@marples.name>
parents: 5028
diff changeset
3567
4757
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3568 /* Validate filter. */
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3569 if (!is_packet_udp_bootp(data, len)) {
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3570 #ifdef BPF_DEBUG
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3571 logerrx("%s: DHCP BPF validation failure", ifp->name);
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3572 #endif
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3573 return;
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3574 }
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3575
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3576 if (!checksums_valid(data, &from, bpf_flags)) {
4757
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3577 logerrx("%s: checksum failure from %s",
19c3b77a9940 BPF: Move validation logic from BPF to consumers
Roy Marples <roy@marples.name>
parents: 4738
diff changeset
3578 ifp->name, inet_ntoa(from));
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3579 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3580 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3581
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3582 /*
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3583 * DHCP has a variable option area rather than a fixed vendor area.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3584 * Because DHCP uses the BOOTP protocol it should still send BOOTP
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3585 * sized packets to be RFC compliant.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3586 * However some servers send a truncated vendor area.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3587 * dhcpcd can work fine without the vendor area being sent.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3588 */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3589 bootp = get_udp_data(data, &udp_len);
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3590 dhcp_handlebootp(ifp, bootp, udp_len, &from);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3591 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3592
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3593 static void
4717
3cce06e28d99 DHCP: Rename dhcp_readpacket to dhcp_readbpf
Roy Marples <roy@marples.name>
parents: 4715
diff changeset
3594 dhcp_readbpf(void *arg)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3595 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3596 struct interface *ifp = arg;
5028
c06163d3bc14 DHCP: Support jumbo frames
Roy Marples <roy@marples.name>
parents: 5026
diff changeset
3597 uint8_t buf[FRAMELEN_MAX];
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3598 ssize_t bytes;
4169
07bfee79bd7a bpf: store flags in state for a better abort
Roy Marples <roy@marples.name>
parents: 4168
diff changeset
3599 struct dhcp_state *state = D_STATE(ifp);
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3600 struct bpf *bpf = state->bpf;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3601
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3602 bpf->bpf_flags &= ~BPF_EOF;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3603 while (!(bpf->bpf_flags & BPF_EOF)) {
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3604 bytes = bpf_read(bpf, buf, sizeof(buf));
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3605 if (bytes == -1) {
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3606 if (state->state != DHS_NONE) {
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3607 logerr("%s: %s", __func__, ifp->name);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3608 dhcp_close(ifp);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3609 }
4171
4ff80b2bbba1 Fix prior patches by using correct bitmasks for flags.
Roy Marples <roy@marples.name>
parents: 4169
diff changeset
3610 break;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3611 }
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3612 dhcp_packet(ifp, buf, (size_t)bytes, bpf->bpf_flags);
3970
c31023f676be Fix a potential crash where the DHCP state could be freed during processing.
Roy Marples <roy@marples.name>
parents: 3960
diff changeset
3613 /* Check we still have a state after processing. */
4169
07bfee79bd7a bpf: store flags in state for a better abort
Roy Marples <roy@marples.name>
parents: 4168
diff changeset
3614 if ((state = D_STATE(ifp)) == NULL)
3970
c31023f676be Fix a potential crash where the DHCP state could be freed during processing.
Roy Marples <roy@marples.name>
parents: 3960
diff changeset
3615 break;
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3616 if ((bpf = state->bpf) == NULL)
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3617 break;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3618 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3619 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3620
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3621 void
4787
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3622 dhcp_recvmsg(struct dhcpcd_ctx *ctx, struct msghdr *msg)
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3623 {
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3624 struct sockaddr_in *from = (struct sockaddr_in *)msg->msg_name;
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3625 struct iovec *iov = &msg->msg_iov[0];
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3626 struct interface *ifp;
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3627 const struct dhcp_state *state;
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3628
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3629 ifp = if_findifpfromcmsg(ctx, msg, NULL);
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3630 if (ifp == NULL) {
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3631 logerr(__func__);
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3632 return;
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3633 }
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3634 state = D_CSTATE(ifp);
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3635 if (state == NULL) {
5009
0f76dcb791a4 DHCP: redirect message to other interfaces if received for inactive
Roy Marples <roy@marples.name>
parents: 5002
diff changeset
3636 /* Try re-directing it to another interface. */
0f76dcb791a4 DHCP: redirect message to other interfaces if received for inactive
Roy Marples <roy@marples.name>
parents: 5002
diff changeset
3637 dhcp_redirect_dhcp(ifp, (struct bootp *)iov->iov_base,
0f76dcb791a4 DHCP: redirect message to other interfaces if received for inactive
Roy Marples <roy@marples.name>
parents: 5002
diff changeset
3638 iov->iov_len, &from->sin_addr);
4787
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3639 return;
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3640 }
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3641
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3642 if (state->bpf != NULL) {
4787
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3643 /* Avoid a duplicate read if BPF is open for the interface. */
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3644 return;
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3645 }
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3646 #ifdef PRIVSEP
4868
119c8986dfc8 privsep: Enable ARP BPF filtering for interesting addresses
Roy Marples <roy@marples.name>
parents: 4866
diff changeset
3647 if (IN_PRIVSEP(ctx)) {
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3648 switch (state->state) {
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3649 case DHS_BOUND: /* FALLTHROUGH */
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3650 case DHS_RENEW:
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3651 break;
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3652 default:
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3653 /* Any other state we ignore it or will receive
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3654 * via BPF. */
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3655 return;
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3656 }
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3657 }
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3658 #endif
4787
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3659
5193
658eb5d94c0b DHCP: Use correct buffer for receiving UDP
Roy Marples <roy@marples.name>
parents: 5176
diff changeset
3660 dhcp_handlebootp(ifp, iov->iov_base, iov->iov_len,
4787
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3661 &from->sin_addr);
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3662 }
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3663
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3664 static void
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3665 dhcp_readudp(struct dhcpcd_ctx *ctx, struct interface *ifp)
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3666 {
4602
4315e1265f1c DragonFlyBSD: Fix compile
Roy Marples <roy@marples.name>
parents: 4594
diff changeset
3667 const struct dhcp_state *state;
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3668 struct sockaddr_in from;
5193
658eb5d94c0b DHCP: Use correct buffer for receiving UDP
Roy Marples <roy@marples.name>
parents: 5176
diff changeset
3669 union {
658eb5d94c0b DHCP: Use correct buffer for receiving UDP
Roy Marples <roy@marples.name>
parents: 5176
diff changeset
3670 struct bootp bootp;
658eb5d94c0b DHCP: Use correct buffer for receiving UDP
Roy Marples <roy@marples.name>
parents: 5176
diff changeset
3671 uint8_t buf[10 * 1024]; /* Maximum MTU */
658eb5d94c0b DHCP: Use correct buffer for receiving UDP
Roy Marples <roy@marples.name>
parents: 5176
diff changeset
3672 } iovbuf;
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3673 struct iovec iov = {
5193
658eb5d94c0b DHCP: Use correct buffer for receiving UDP
Roy Marples <roy@marples.name>
parents: 5176
diff changeset
3674 .iov_base = iovbuf.buf,
658eb5d94c0b DHCP: Use correct buffer for receiving UDP
Roy Marples <roy@marples.name>
parents: 5176
diff changeset
3675 .iov_len = sizeof(iovbuf.buf),
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3676 };
5160
01d56b8eb743 align CMSG buffer
Roy Marples <roy@marples.name>
parents: 5158
diff changeset
3677 union {
01d56b8eb743 align CMSG buffer
Roy Marples <roy@marples.name>
parents: 5158
diff changeset
3678 struct cmsghdr hdr;
4784
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
3679 #ifdef IP_RECVIF
5160
01d56b8eb743 align CMSG buffer
Roy Marples <roy@marples.name>
parents: 5158
diff changeset
3680 uint8_t buf[CMSG_SPACE(sizeof(struct sockaddr_dl))];
4784
e04f4fb2f967 BSD: Use IP_RECVIF
Roy Marples <roy@marples.name>
parents: 4782
diff changeset
3681 #else
5160
01d56b8eb743 align CMSG buffer
Roy Marples <roy@marples.name>
parents: 5158
diff changeset
3682 uint8_t buf[CMSG_SPACE(sizeof(struct in_pktinfo))];
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3683 #endif
5160
01d56b8eb743 align CMSG buffer
Roy Marples <roy@marples.name>
parents: 5158
diff changeset
3684 } cmsgbuf = { .buf = { 0 } };
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3685 struct msghdr msg = {
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3686 .msg_name = &from, .msg_namelen = sizeof(from),
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3687 .msg_iov = &iov, .msg_iovlen = 1,
5193
658eb5d94c0b DHCP: Use correct buffer for receiving UDP
Roy Marples <roy@marples.name>
parents: 5176
diff changeset
3688 .msg_control = cmsgbuf.buf, .msg_controllen = sizeof(cmsgbuf.buf),
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3689 };
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3690 int s;
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3691 ssize_t bytes;
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3692
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3693 if (ifp != NULL) {
4594
81f346f0e892 DHCP: Avoid duplicate read of packet
Roy Marples <roy@marples.name>
parents: 4587
diff changeset
3694 state = D_CSTATE(ifp);
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3695 s = state->udp_rfd;
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3696 } else
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3697 s = ctx->udp_rfd;
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3698
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3699 bytes = recvmsg(s, &msg, 0);
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3700 if (bytes == -1) {
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3701 logerr(__func__);
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3702 return;
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3703 }
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3704
4787
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3705 iov.iov_len = (size_t)bytes;
d3a3a2ab8ba9 Split *_recv() into *_recv() and *_recvmsg() functions
Roy Marples <roy@marples.name>
parents: 4786
diff changeset
3706 dhcp_recvmsg(ctx, &msg);
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3707 }
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3708
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3709 static void
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3710 dhcp_handleudp(void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3711 {
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3712 struct dhcpcd_ctx *ctx = arg;
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3713
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3714 dhcp_readudp(ctx, NULL);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3715 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3716
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3717 static void
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3718 dhcp_handleifudp(void *arg)
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3719 {
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3720 struct interface *ifp = arg;
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3721
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3722 dhcp_readudp(ifp->ctx, ifp);
4791
3ba8c76fc5af DHCP: Simplify opening the UDP port for all interfaces.
Roy Marples <roy@marples.name>
parents: 4787
diff changeset
3723 }
3ba8c76fc5af DHCP: Simplify opening the UDP port for all interfaces.
Roy Marples <roy@marples.name>
parents: 4787
diff changeset
3724
3ba8c76fc5af DHCP: Simplify opening the UDP port for all interfaces.
Roy Marples <roy@marples.name>
parents: 4787
diff changeset
3725 static int
4168
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
3726 dhcp_openbpf(struct interface *ifp)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3727 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3728 struct dhcp_state *state;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3729
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3730 state = D_STATE(ifp);
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3731
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3732 #ifdef PRIVSEP
4868
119c8986dfc8 privsep: Enable ARP BPF filtering for interesting addresses
Roy Marples <roy@marples.name>
parents: 4866
diff changeset
3733 if (IN_PRIVSEP_SE(ifp->ctx)) {
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3734 if (ps_bpf_openbootp(ifp) == -1) {
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3735 logerr(__func__);
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3736 return -1;
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3737 }
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3738 return 0;
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3739 }
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3740 #endif
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3741
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3742 if (state->bpf != NULL)
4168
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
3743 return 0;
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
3744
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3745 state->bpf = bpf_open(ifp, bpf_bootp, NULL);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3746 if (state->bpf == NULL) {
4168
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
3747 if (errno == ENOENT) {
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
3748 logerrx("%s not found", bpf_name);
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
3749 /* May as well disable IPv4 entirely at
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
3750 * this point as we really need it. */
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
3751 ifp->options->options &= ~DHCPCD_IPV4;
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
3752 } else
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
3753 logerr("%s: %s", __func__, ifp->name);
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
3754 return -1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3755 }
4168
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
3756
e8510a89cdb2 dhcp: arp announce existing address before reboot
Roy Marples <roy@marples.name>
parents: 4167
diff changeset
3757 eloop_event_add(ifp->ctx->eloop,
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3758 state->bpf->bpf_fd, dhcp_readbpf, ifp);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3759 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3760 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3761
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3762 void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3763 dhcp_free(struct interface *ifp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3764 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3765 struct dhcp_state *state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3766 struct dhcpcd_ctx *ctx;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3767
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3768 dhcp_close(ifp);
4135
64796240ee75 arp: RFC5227 kernels need to send ARP too
Roy Marples <roy@marples.name>
parents: 4125
diff changeset
3769 #ifdef ARP
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3770 arp_drop(ifp);
4135
64796240ee75 arp: RFC5227 kernels need to send ARP too
Roy Marples <roy@marples.name>
parents: 4125
diff changeset
3771 #endif
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3772 if (state) {
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3773 state->state = DHS_NONE;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3774 free(state->old);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3775 free(state->new);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3776 free(state->offer);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3777 free(state->clientid);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3778 free(state);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3779 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3780
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3781 ctx = ifp->ctx;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3782 /* If we don't have any more DHCP enabled interfaces,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3783 * close the global socket and release resources */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3784 if (ctx->ifaces) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3785 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3786 state = D_STATE(ifp);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3787 if (state != NULL && state->state != DHS_NONE)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3788 break;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3789 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3790 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3791 if (ifp == NULL) {
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3792 if (ctx->udp_rfd != -1) {
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3793 eloop_event_delete(ctx->eloop, ctx->udp_rfd);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3794 close(ctx->udp_rfd);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3795 ctx->udp_rfd = -1;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3796 }
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3797 if (ctx->udp_wfd != -1) {
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3798 close(ctx->udp_wfd);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3799 ctx->udp_wfd = -1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3800 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3801
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3802 free(ctx->opt_buffer);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3803 ctx->opt_buffer = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3804 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3805 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3806
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3807 static int
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3808 dhcp_initstate(struct interface *ifp)
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3809 {
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3810 struct dhcp_state *state;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3811
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3812 state = D_STATE(ifp);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3813 if (state != NULL)
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3814 return 0;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3815
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3816 ifp->if_data[IF_DATA_DHCP] = calloc(1, sizeof(*state));
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3817 state = D_STATE(ifp);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3818 if (state == NULL)
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3819 return -1;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3820
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3821 state->state = DHS_NONE;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3822 /* 0 is a valid fd, so init to -1 */
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3823 state->udp_rfd = -1;
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3824 #ifdef ARPING
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3825 state->arping_index = -1;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3826 #endif
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3827 return 1;
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3828 }
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3829
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3830 static int
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3831 dhcp_init(struct interface *ifp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3832 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3833 struct dhcp_state *state;
5280
a986083da0ba Fix some clang analyzer issues
Roy Marples <roy@marples.name>
parents: 5272
diff changeset
3834 struct if_options *ifo;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3835 uint8_t len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3836 char buf[(sizeof(ifo->clientid) - 1) * 3];
4400
2d6a2c8c6b56 route: Remove kroutes and make froutes optional
Roy Marples <roy@marples.name>
parents: 4396
diff changeset
3837
2d6a2c8c6b56 route: Remove kroutes and make froutes optional
Roy Marples <roy@marples.name>
parents: 4396
diff changeset
3838 if (dhcp_initstate(ifp) == -1)
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3839 return -1;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3840
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
3841 state = D_STATE(ifp);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3842 state->state = DHS_INIT;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3843 state->reason = "PREINIT";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3844 state->nakoff = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3845 dhcp_set_leasefile(state->leasefile, sizeof(state->leasefile),
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3846 AF_INET, ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3847
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3848 ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3849 /* We need to drop the leasefile so that dhcp_start
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3850 * doesn't load it. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3851 if (ifo->options & DHCPCD_REQUEST)
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
3852 dhcp_unlink(ifp->ctx, state->leasefile);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3853
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3854 free(state->clientid);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3855 state->clientid = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3856
4958
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3857 if (ifo->options & DHCPCD_ANONYMOUS) {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3858 uint8_t duid[DUID_LEN];
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3859 uint8_t duid_len;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3860
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3861 duid_len = (uint8_t)duid_make(duid, ifp, DUID_LL);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3862 if (duid_len != 0) {
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3863 state->clientid = malloc((size_t)duid_len + 6);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3864 if (state->clientid == NULL)
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3865 goto eexit;
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3866 state->clientid[0] =(uint8_t)(duid_len + 5);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3867 state->clientid[1] = 255; /* RFC 4361 */
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3868 memcpy(state->clientid + 2, ifo->iaid, 4);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3869 memset(state->clientid + 2, 0, 4); /* IAID */
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3870 memcpy(state->clientid + 6, duid, duid_len);
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3871 }
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
3872 } else if (*ifo->clientid) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3873 state->clientid = malloc((size_t)(ifo->clientid[0] + 1));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3874 if (state->clientid == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3875 goto eexit;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3876 memcpy(state->clientid, ifo->clientid,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3877 (size_t)(ifo->clientid[0]) + 1);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3878 } else if (ifo->options & DHCPCD_CLIENTID) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3879 if (ifo->options & DHCPCD_DUID) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3880 state->clientid = malloc(ifp->ctx->duid_len + 6);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3881 if (state->clientid == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3882 goto eexit;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3883 state->clientid[0] =(uint8_t)(ifp->ctx->duid_len + 5);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3884 state->clientid[1] = 255; /* RFC 4361 */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3885 memcpy(state->clientid + 2, ifo->iaid, 4);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3886 memcpy(state->clientid + 6, ifp->ctx->duid,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3887 ifp->ctx->duid_len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3888 } else {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3889 len = (uint8_t)(ifp->hwlen + 1);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3890 state->clientid = malloc((size_t)len + 1);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3891 if (state->clientid == NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3892 goto eexit;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3893 state->clientid[0] = len;
5166
a70f6ddefe3c Rename ifp->family -> ifp->hwtype so it's less confusing
Roy Marples <roy@marples.name>
parents: 5160
diff changeset
3894 state->clientid[1] = (uint8_t)ifp->hwtype;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3895 memcpy(state->clientid + 2, ifp->hwaddr,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3896 ifp->hwlen);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3897 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3898 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3899
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3900 if (ifo->options & DHCPCD_DUID)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3901 /* Don't bother logging as DUID and IAID are reported
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3902 * at device start. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3903 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3904
5280
a986083da0ba Fix some clang analyzer issues
Roy Marples <roy@marples.name>
parents: 5272
diff changeset
3905 if (ifo->options & DHCPCD_CLIENTID && state->clientid != NULL)
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
3906 logdebugx("%s: using ClientID %s", ifp->name,
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3907 hwaddr_ntoa(state->clientid + 1, state->clientid[0],
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3908 buf, sizeof(buf)));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3909 else if (ifp->hwlen)
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
3910 logdebugx("%s: using hwaddr %s", ifp->name,
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3911 hwaddr_ntoa(ifp->hwaddr, ifp->hwlen, buf, sizeof(buf)));
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3912 return 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3913
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3914 eexit:
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
3915 logerr(__func__);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3916 return -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3917 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3918
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3919 static void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3920 dhcp_start1(void *arg)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3921 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3922 struct interface *ifp = arg;
4681
c0b19aa0ee47 DHCP: Don't listen on the unspecified address when not in master mode
Roy Marples <roy@marples.name>
parents: 4677
diff changeset
3923 struct dhcpcd_ctx *ctx = ifp->ctx;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3924 struct if_options *ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3925 struct dhcp_state *state;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3926 uint32_t l;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3927 int nolease;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3928
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3929 if (!(ifo->options & DHCPCD_IPV4))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3930 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3931
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3932 /* Listen on *.*.*.*:bootpc so that the kernel never sends an
4681
c0b19aa0ee47 DHCP: Don't listen on the unspecified address when not in master mode
Roy Marples <roy@marples.name>
parents: 4677
diff changeset
3933 * ICMP port unreachable message back to the DHCP server.
c0b19aa0ee47 DHCP: Don't listen on the unspecified address when not in master mode
Roy Marples <roy@marples.name>
parents: 4677
diff changeset
3934 * Only do this in master mode so we don't swallow messages
c0b19aa0ee47 DHCP: Don't listen on the unspecified address when not in master mode
Roy Marples <roy@marples.name>
parents: 4677
diff changeset
3935 * for dhcpcd running on another interface. */
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3936 if ((ctx->options & (DHCPCD_MASTER|DHCPCD_PRIVSEP)) == DHCPCD_MASTER
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3937 && ctx->udp_rfd == -1)
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
3938 {
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3939 ctx->udp_rfd = dhcp_openudp(NULL);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3940 if (ctx->udp_rfd == -1) {
4833
aadc0d25a706 DHCP: Require unspecificed BOOTP port for master mode
Roy Marples <roy@marples.name>
parents: 4829
diff changeset
3941 logerr(__func__);
aadc0d25a706 DHCP: Require unspecificed BOOTP port for master mode
Roy Marples <roy@marples.name>
parents: 4829
diff changeset
3942 return;
4791
3ba8c76fc5af DHCP: Simplify opening the UDP port for all interfaces.
Roy Marples <roy@marples.name>
parents: 4787
diff changeset
3943 }
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3944 eloop_event_add(ctx->eloop, ctx->udp_rfd, dhcp_handleudp, ctx);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3945 }
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3946 if (!IN_PRIVSEP(ctx) && ctx->udp_wfd == -1) {
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3947 ctx->udp_wfd = xsocket(PF_INET, SOCK_RAW|SOCK_CXNB,IPPROTO_UDP);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3948 if (ctx->udp_wfd == -1) {
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3949 logerr(__func__);
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3950 return;
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3951 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3952 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3953
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3954 if (dhcp_init(ifp) == -1) {
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
3955 logerr("%s: dhcp_init", ifp->name);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3956 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3957 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3958
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3959 state = D_STATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3960 clock_gettime(CLOCK_MONOTONIC, &state->started);
4224
595761ecbde0 dhcp: reset interval during init
Roy Marples <roy@marples.name>
parents: 4207
diff changeset
3961 state->interval = 0;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3962 free(state->offer);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3963 state->offer = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3964 state->offer_len = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3965
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3966 #ifdef ARPING
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3967 if (ifo->arping_len && state->arping_index < ifo->arping_len) {
5231
a2c342295221 privsep: Enable Capsicum for all processes.
Roy Marples <roy@marples.name>
parents: 5229
diff changeset
3968 dhcp_arping(ifp);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3969 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3970 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3971 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3972
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3973 if (ifo->options & DHCPCD_STATIC) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3974 dhcp_static(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3975 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3976 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3977
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3978 if (ifo->options & DHCPCD_INFORM) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3979 dhcp_inform(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3980 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3981 }
4373
5ebc4718f728 DHCP: Close the BPF socket once BOUND
Roy Marples <roy@marples.name>
parents: 4365
diff changeset
3982
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3983 /* We don't want to read the old lease if we NAK an old test */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3984 nolease = state->offer && ifp->ctx->options & DHCPCD_TEST;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3985 if (!nolease && ifo->options & DHCPCD_DHCP) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3986 state->offer_len = read_lease(ifp, &state->offer);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3987 /* Check the saved lease matches the type we want */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3988 if (state->offer) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3989 #ifdef IN_IFF_DUPLICATED
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3990 struct in_addr addr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3991 struct ipv4_addr *ia;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3992
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3993 addr.s_addr = state->offer->yiaddr;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3994 ia = ipv4_iffindaddr(ifp, &addr, NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3995 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3996
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3997 if ((!IS_DHCP(state->offer) &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3998 !(ifo->options & DHCPCD_BOOTP)) ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
3999 #ifdef IN_IFF_DUPLICATED
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4000 (ia && ia->addr_flags & IN_IFF_DUPLICATED) ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4001 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4002 (IS_DHCP(state->offer) &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4003 ifo->options & DHCPCD_BOOTP))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4004 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4005 free(state->offer);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4006 state->offer = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4007 state->offer_len = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4008 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4009 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4010 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4011 if (state->offer) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4012 struct ipv4_addr *ia;
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
4013 time_t mtime;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4014
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4015 get_lease(ifp, &state->lease, state->offer, state->offer_len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4016 state->lease.frominfo = 1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4017 if (state->new == NULL &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4018 (ia = ipv4_iffindaddr(ifp,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4019 &state->lease.addr, &state->lease.mask)) != NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4020 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4021 /* We still have the IP address from the last lease.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4022 * Fake add the address and routes from it so the lease
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4023 * can be cleaned up. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4024 state->new = malloc(state->offer_len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4025 if (state->new) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4026 memcpy(state->new,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4027 state->offer, state->offer_len);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4028 state->new_len = state->offer_len;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4029 state->addr = ia;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4030 state->added |= STATE_ADDED | STATE_FAKE;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4031 rt_build(ifp->ctx, AF_INET);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4032 } else
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
4033 logerr(__func__);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4034 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4035 if (!IS_DHCP(state->offer)) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4036 free(state->offer);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4037 state->offer = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4038 state->offer_len = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4039 } else if (!(ifo->options & DHCPCD_LASTLEASE_EXTEND) &&
4555
93b4eb29d297 DHCP: Set address vltime and pltime to the length of the lease
Roy Marples <roy@marples.name>
parents: 4549
diff changeset
4040 state->lease.leasetime != DHCP_INFINITE_LIFETIME &&
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
4041 dhcp_filemtime(ifp->ctx, state->leasefile, &mtime) == 0)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4042 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4043 time_t now;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4044
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4045 /* Offset lease times and check expiry */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4046 now = time(NULL);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4047 if (now == -1 ||
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
4048 (time_t)state->lease.leasetime < now - mtime)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4049 {
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
4050 logdebugx("%s: discarding expired lease",
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents: 4002
diff changeset
4051 ifp->name);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4052 free(state->offer);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4053 state->offer = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4054 state->offer_len = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4055 state->lease.addr.s_addr = 0;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4056 /* Technically we should discard the lease
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4057 * as it's expired, just as DHCPv6 addresses
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4058 * would be by the kernel.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4059 * However, this may violate POLA so
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4060 * we currently leave it be.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4061 * If we get a totally different lease from
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4062 * the DHCP server we'll drop it anyway, as
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4063 * we will on any other event which would
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4064 * trigger a lease drop.
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4065 * This should only happen if dhcpcd stops
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4066 * running and the lease expires before
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4067 * dhcpcd starts again. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4068 #if 0
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4069 if (state->new)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4070 dhcp_drop(ifp, "EXPIRE");
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4071 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4072 } else {
5207
84b63f09c8a4 privsep: Handle all file IO in the Priviledged Actioneer
Roy Marples <roy@marples.name>
parents: 5193
diff changeset
4073 l = (uint32_t)(now - mtime);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4074 state->lease.leasetime -= l;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4075 state->lease.renewaltime -= l;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4076 state->lease.rebindtime -= l;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4077 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4078 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4079 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4080
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4081 #ifdef IPV4LL
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4082 if (!(ifo->options & DHCPCD_DHCP)) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4083 if (ifo->options & DHCPCD_IPV4LL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4084 ipv4ll_start(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4085 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4086 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4087 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4088
4958
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
4089 if (state->offer == NULL ||
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
4090 !IS_DHCP(state->offer) ||
a120f447fe74 Implement Anonymity Profiles for DHCP Clients, RFC 7844
Roy Marples <roy@marples.name>
parents: 4952
diff changeset
4091 ifo->options & DHCPCD_ANONYMOUS)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4092 dhcp_discover(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4093 else
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4094 dhcp_reboot(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4095 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4096
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4097 void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4098 dhcp_start(struct interface *ifp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4099 {
4932
8e1948e75f39 DHCP: Use milliseconds rather than timespec for retranmission
Roy Marples <roy@marples.name>
parents: 4924
diff changeset
4100 unsigned int delay;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4101 #ifdef ARPING
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4102 const struct dhcp_state *state;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4103 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4104
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4105 if (!(ifp->options->options & DHCPCD_IPV4))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4106 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4107
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4108 /* If we haven't been given a netmask for our requested address,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4109 * set it now. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4110 if (ifp->options->req_addr.s_addr != INADDR_ANY &&
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4111 ifp->options->req_mask.s_addr == INADDR_ANY)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4112 ifp->options->req_mask.s_addr =
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4113 ipv4_getnetmask(ifp->options->req_addr.s_addr);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4114
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4115 /* If we haven't specified a ClientID and our hardware address
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4116 * length is greater than BOOTP CHADDR then we enforce a ClientID
5166
a70f6ddefe3c Rename ifp->family -> ifp->hwtype so it's less confusing
Roy Marples <roy@marples.name>
parents: 5160
diff changeset
4117 * of the hardware address type and the hardware address.
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4118 * If there is no hardware address and no ClientID set,
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4119 * force a DUID based ClientID. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4120 if (ifp->hwlen > 16)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4121 ifp->options->options |= DHCPCD_CLIENTID;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4122 else if (ifp->hwlen == 0 && !(ifp->options->options & DHCPCD_CLIENTID))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4123 ifp->options->options |= DHCPCD_CLIENTID | DHCPCD_DUID;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4124
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4125 /* Firewire and InfiniBand interfaces require ClientID and
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4126 * the broadcast option being set. */
5166
a70f6ddefe3c Rename ifp->family -> ifp->hwtype so it's less confusing
Roy Marples <roy@marples.name>
parents: 5160
diff changeset
4127 switch (ifp->hwtype) {
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4128 case ARPHRD_IEEE1394: /* FALLTHROUGH */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4129 case ARPHRD_INFINIBAND:
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4130 ifp->options->options |= DHCPCD_CLIENTID | DHCPCD_BROADCAST;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4131 break;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4132 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4133
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4134 /* If we violate RFC2131 section 3.7 then require ARP
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4135 * to detect if any other client wants our address. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4136 if (ifp->options->options & DHCPCD_LASTLEASE_EXTEND)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4137 ifp->options->options |= DHCPCD_ARP;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4138
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4139 /* No point in delaying a static configuration */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4140 if (ifp->options->options & DHCPCD_STATIC ||
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4141 !(ifp->options->options & DHCPCD_INITIAL_DELAY))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4142 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4143 dhcp_start1(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4144 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4145 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4146
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4147 #ifdef ARPING
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4148 /* If we have arpinged then we have already delayed. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4149 state = D_CSTATE(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4150 if (state != NULL && state->arping_index != -1) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4151 dhcp_start1(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4152 return;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4153 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4154 #endif
4932
8e1948e75f39 DHCP: Use milliseconds rather than timespec for retranmission
Roy Marples <roy@marples.name>
parents: 4924
diff changeset
4155 delay = MSEC_PER_SEC +
8e1948e75f39 DHCP: Use milliseconds rather than timespec for retranmission
Roy Marples <roy@marples.name>
parents: 4924
diff changeset
4156 (arc4random_uniform(MSEC_PER_SEC * 2) - MSEC_PER_SEC);
4023
66f9399ba5c6 Add logdebugx, similar to logerrx and logwarnx.
Roy Marples <roy@marples.name>
parents: 4014
diff changeset
4157 logdebugx("%s: delaying IPv4 for %0.1f seconds",
4932
8e1948e75f39 DHCP: Use milliseconds rather than timespec for retranmission
Roy Marples <roy@marples.name>
parents: 4924
diff changeset
4158 ifp->name, (float)delay / MSEC_PER_SEC);
8e1948e75f39 DHCP: Use milliseconds rather than timespec for retranmission
Roy Marples <roy@marples.name>
parents: 4924
diff changeset
4159
8e1948e75f39 DHCP: Use milliseconds rather than timespec for retranmission
Roy Marples <roy@marples.name>
parents: 4924
diff changeset
4160 eloop_timeout_add_msec(ifp->ctx->eloop, delay, dhcp_start1, ifp);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4161 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4162
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4163 void
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4164 dhcp_abort(struct interface *ifp)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4165 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4166 struct dhcp_state *state;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4167
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4168 state = D_STATE(ifp);
4135
64796240ee75 arp: RFC5227 kernels need to send ARP too
Roy Marples <roy@marples.name>
parents: 4125
diff changeset
4169 #ifdef ARPING
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4170 if (state != NULL)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4171 state->arping_index = -1;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4172 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4173
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4174 eloop_timeout_delete(ifp->ctx->eloop, dhcp_start1, ifp);
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
4175
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
4176 if (state != NULL && state->added) {
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
4177 rt_build(ifp->ctx, AF_INET);
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
4178 #ifdef ARP
5241
989bcc6c8e70 ARP: A few minor fixes for prior
Roy Marples <roy@marples.name>
parents: 5234
diff changeset
4179 if (ifp->options->options & DHCPCD_ARP)
989bcc6c8e70 ARP: A few minor fixes for prior
Roy Marples <roy@marples.name>
parents: 5234
diff changeset
4180 arp_announceaddr(ifp->ctx, &state->addr->addr);
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
4181 #endif
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
4182 }
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4183 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4184
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
4185 struct ipv4_addr *
4232
8b92c1844860 Log the pid of the process deleting an assigned address.
Roy Marples <roy@marples.name>
parents: 4224
diff changeset
4186 dhcp_handleifa(int cmd, struct ipv4_addr *ia, pid_t pid)
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4187 {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4188 struct interface *ifp;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4189 struct dhcp_state *state;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4190 struct if_options *ifo;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4191 uint8_t i;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4192
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4193 ifp = ia->iface;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4194 state = D_STATE(ifp);
4113
d30461d23e91 Persist shared IP address on interfaces.
Roy Marples <roy@marples.name>
parents: 4108
diff changeset
4195 if (state == NULL || state->state == DHS_NONE)
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
4196 return ia;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4197
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4198 if (cmd == RTM_DELADDR) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4199 if (state->addr == ia) {
4232
8b92c1844860 Log the pid of the process deleting an assigned address.
Roy Marples <roy@marples.name>
parents: 4224
diff changeset
4200 loginfox("%s: pid %d deleted IP address %s",
8b92c1844860 Log the pid of the process deleting an assigned address.
Roy Marples <roy@marples.name>
parents: 4224
diff changeset
4201 ifp->name, pid, ia->saddr);
4840
073fcd86db9b privsep: Add support for priviledge separation
Roy Marples <roy@marples.name>
parents: 4833
diff changeset
4202 dhcp_close(ifp);
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4203 state->addr = NULL;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4204 /* Don't clear the added state as we need
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4205 * to drop the lease. */
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4206 dhcp_drop(ifp, "EXPIRE");
4242
9f4dc610f30c Really start dhcp on deletion of address.
Roy Marples <roy@marples.name>
parents: 4240
diff changeset
4207 dhcp_start1(ifp);
4782
8a9ec0631165 INET: Fix a potential memory leak
Roy Marples <roy@marples.name>
parents: 4775
diff changeset
4208 return ia;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4209 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4210 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4211
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4212 if (cmd != RTM_NEWADDR)
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
4213 return ia;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4214
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4215 #ifdef IN_IFF_NOTUSEABLE
4453
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
4216 if (!(ia->addr_flags & IN_IFF_NOTUSEABLE))
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
4217 dhcp_finish_dad(ifp, &ia->addr);
00fb50fd1a45 ARP: Refactor so that ACD is available for all inet addresses
Roy Marples <roy@marples.name>
parents: 4427
diff changeset
4218 else if (ia->addr_flags & IN_IFF_DUPLICATED)
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
4219 return dhcp_addr_duplicated(ifp, &ia->addr) ? NULL : ia;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4220 #endif
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4221
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4222 ifo = ifp->options;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4223 if (ifo->options & DHCPCD_INFORM) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4224 if (state->state != DHS_INFORM)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4225 dhcp_inform(ifp);
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
4226 return ia;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4227 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4228
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4229 if (!(ifo->options & DHCPCD_STATIC))
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
4230 return ia;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4231 if (ifo->req_addr.s_addr != INADDR_ANY)
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
4232 return ia;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4233
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4234 free(state->old);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4235 state->old = state->new;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4236 state->new_len = dhcp_message_new(&state->new, &ia->addr, &ia->mask);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4237 if (state->new == NULL)
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
4238 return ia;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4239 if (ifp->flags & IFF_POINTOPOINT) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4240 for (i = 1; i < 255; i++)
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4241 if (i != DHO_ROUTER && has_option_mask(ifo->dstmask,i))
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4242 dhcp_message_add_addr(state->new, i, ia->brd);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4243 }
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4244 state->reason = "STATIC";
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4245 rt_build(ifp->ctx, AF_INET);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4246 script_runreason(ifp, state->reason);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4247 if (ifo->options & DHCPCD_INFORM) {
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4248 state->state = DHS_INFORM;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4249 dhcp_new_xid(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4250 state->lease.server.s_addr = INADDR_ANY;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4251 state->addr = ia;
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4252 dhcp_inform(ifp);
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4253 }
4737
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
4254
65679ce5c370 inet: Rework prior incase DHCP uses an IPv4LL address
Roy Marples <roy@marples.name>
parents: 4736
diff changeset
4255 return ia;
3932
e802a4235d75 Move the source files along with dev, crypt and comapt into src dir.
Roy Marples <roy@marples.name>
parents:
diff changeset
4256 }
5288
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4257
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4258 #ifndef SMALL
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4259 int
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4260 dhcp_dump(struct interface *ifp)
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4261 {
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4262 struct dhcp_state *state;
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4263
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4264 ifp->if_data[IF_DATA_DHCP] = state = calloc(1, sizeof(*state));
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4265 if (state == NULL) {
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4266 logerr(__func__);
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4267 return -1;
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4268 }
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4269 state->new_len = read_lease(ifp, &state->new);
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4270 if (state->new == NULL) {
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4271 logerr("read_lease");
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4272 return -1;
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4273 }
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4274 state->reason = "DUMP";
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4275 return script_runreason(ifp, state->reason);
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4276 }
30958d539e6c Restore dumping a lease from stdin
Roy Marples <roy@marples.name>
parents: 5280
diff changeset
4277 #endif