1 | /* |
---|
2 | * dhcpcd - DHCP client daemon |
---|
3 | * Copyright (c) 2006-2014 Roy Marples <roy@marples.name> |
---|
4 | * All rights reserved |
---|
5 | |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
25 | * SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #include <sys/ioctl.h> |
---|
29 | #include <sys/param.h> |
---|
30 | #include <sys/socket.h> |
---|
31 | #include <sys/sysctl.h> |
---|
32 | #include <sys/utsname.h> |
---|
33 | |
---|
34 | #include <net/if.h> |
---|
35 | #ifdef __FreeBSD__ /* Needed so that including netinet6/in6_var.h works */ |
---|
36 | # include <net/if_var.h> |
---|
37 | #endif |
---|
38 | #include <netinet/in.h> |
---|
39 | #include <netinet6/in6_var.h> |
---|
40 | |
---|
41 | #include <errno.h> |
---|
42 | #include <stdlib.h> |
---|
43 | #include <string.h> |
---|
44 | #include <syslog.h> |
---|
45 | #include <unistd.h> |
---|
46 | |
---|
47 | #include "common.h" |
---|
48 | #include "dhcpcd.h" |
---|
49 | #include "if-options.h" |
---|
50 | #include "platform.h" |
---|
51 | |
---|
52 | #ifndef SYS_NMLN /* OSX */ |
---|
53 | # define SYS_NMLN 256 |
---|
54 | #endif |
---|
55 | |
---|
56 | static char march[SYS_NMLN]; |
---|
57 | |
---|
58 | char * |
---|
59 | hardware_platform(void) |
---|
60 | { |
---|
61 | int mib[2] = { CTL_HW, HW_MACHINE_ARCH }; |
---|
62 | size_t len = sizeof(march); |
---|
63 | |
---|
64 | if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
---|
65 | march, &len, NULL, 0) != 0) |
---|
66 | return NULL; |
---|
67 | return march; |
---|
68 | } |
---|
69 | |
---|
70 | #ifdef INET6 |
---|
71 | #define get_inet6_sysctl(code) inet6_sysctl(code, 0, 0) |
---|
72 | #define set_inet6_sysctl(code, val) inet6_sysctl(code, val, 1) |
---|
73 | static int |
---|
74 | inet6_sysctl(int code, int val, int action) |
---|
75 | { |
---|
76 | int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 }; |
---|
77 | size_t size; |
---|
78 | |
---|
79 | mib[3] = code; |
---|
80 | size = sizeof(val); |
---|
81 | if (action) { |
---|
82 | if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), |
---|
83 | NULL, 0, &val, size) == -1) |
---|
84 | return -1; |
---|
85 | return 0; |
---|
86 | } |
---|
87 | if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &val, &size, NULL, 0) == -1) |
---|
88 | return -1; |
---|
89 | return val; |
---|
90 | } |
---|
91 | |
---|
92 | static void |
---|
93 | restore_kernel_ra(void) |
---|
94 | { |
---|
95 | |
---|
96 | if (options & DHCPCD_FORKED) |
---|
97 | return; |
---|
98 | syslog(LOG_INFO, "restoring Kernel IPv6 RA support"); |
---|
99 | if (set_inet6_sysctl(IPV6CTL_ACCEPT_RTADV, 1) == -1) |
---|
100 | syslog(LOG_ERR, "IPV6CTL_ACCEPT_RTADV: %m"); |
---|
101 | } |
---|
102 | |
---|
103 | static int |
---|
104 | ipv6_ra_flush(void) |
---|
105 | { |
---|
106 | int s; |
---|
107 | char dummy[IFNAMSIZ + 8]; |
---|
108 | |
---|
109 | s = socket(AF_INET6, SOCK_DGRAM, 0); |
---|
110 | if (s == -1) |
---|
111 | return -1; |
---|
112 | strcpy(dummy, "lo0"); |
---|
113 | if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummy) == -1) |
---|
114 | syslog(LOG_ERR, "SIOSRTRFLUSH_IN6: %m"); |
---|
115 | if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummy) == -1) |
---|
116 | syslog(LOG_ERR, "SIOSPFXFLUSH_IN6: %m"); |
---|
117 | close(s); |
---|
118 | return 0; |
---|
119 | } |
---|
120 | |
---|
121 | int |
---|
122 | check_ipv6(const char *ifname, int own) |
---|
123 | { |
---|
124 | static int set_restore = 0, global_ra = 0; |
---|
125 | int ra; |
---|
126 | |
---|
127 | /* BSD doesn't support these values per iface, so just return |
---|
128 | * the global ra setting */ |
---|
129 | if (ifname) |
---|
130 | return global_ra; |
---|
131 | |
---|
132 | ra = get_inet6_sysctl(IPV6CTL_ACCEPT_RTADV); |
---|
133 | if (ra == -1) |
---|
134 | /* The sysctl probably doesn't exist, but this isn't an |
---|
135 | * error as such so just log it and continue */ |
---|
136 | syslog(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, |
---|
137 | "IPV6CTL_ACCEPT_RTADV: %m"); |
---|
138 | else if (ra != 0 && own) { |
---|
139 | syslog(LOG_INFO, "disabling Kernel IPv6 RA support"); |
---|
140 | if (set_inet6_sysctl(IPV6CTL_ACCEPT_RTADV, 0) == -1) { |
---|
141 | syslog(LOG_ERR, "IPV6CTL_ACCEPT_RTADV: %m"); |
---|
142 | return ra; |
---|
143 | } |
---|
144 | if (!set_restore) { |
---|
145 | set_restore = 1; |
---|
146 | atexit(restore_kernel_ra); |
---|
147 | } |
---|
148 | ra = 0; |
---|
149 | |
---|
150 | /* Flush the kernel knowledge of advertised routers |
---|
151 | * and prefixes so the kernel does not expire prefixes |
---|
152 | * and default routes we are trying to own. */ |
---|
153 | ipv6_ra_flush(); |
---|
154 | } |
---|
155 | if (ifname == NULL) |
---|
156 | global_ra = ra; |
---|
157 | |
---|
158 | return ra; |
---|
159 | } |
---|
160 | |
---|
161 | int |
---|
162 | ipv6_dadtransmits(__unused const char *ifname) |
---|
163 | { |
---|
164 | int r; |
---|
165 | |
---|
166 | r = get_inet6_sysctl(IPV6CTL_DAD_COUNT); |
---|
167 | return r < 0 ? 0 : r; |
---|
168 | } |
---|
169 | #endif |
---|