1 | /* |
---|
2 | * dhcpcd - DHCP client daemon |
---|
3 | * Copyright (c) 2006-2013 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 <errno.h> |
---|
29 | #include <stdio.h> |
---|
30 | #include <stdlib.h> |
---|
31 | #include <string.h> |
---|
32 | #include <syslog.h> |
---|
33 | |
---|
34 | #include "common.h" |
---|
35 | #include "dhcpcd.h" |
---|
36 | #include "if-options.h" |
---|
37 | #include "platform.h" |
---|
38 | |
---|
39 | static const char *mproc = |
---|
40 | #if defined(__alpha__) |
---|
41 | "system type" |
---|
42 | #elif defined(__arm__) |
---|
43 | "Hardware" |
---|
44 | #elif defined(__avr32__) |
---|
45 | "cpu family" |
---|
46 | #elif defined(__bfin__) |
---|
47 | "BOARD Name" |
---|
48 | #elif defined(__cris__) |
---|
49 | "cpu model" |
---|
50 | #elif defined(__frv__) |
---|
51 | "System" |
---|
52 | #elif defined(__i386__) || defined(__x86_64__) |
---|
53 | "vendor_id" |
---|
54 | #elif defined(__ia64__) |
---|
55 | "vendor" |
---|
56 | #elif defined(__hppa__) |
---|
57 | "model" |
---|
58 | #elif defined(__m68k__) |
---|
59 | "MMU" |
---|
60 | #elif defined(__mips__) |
---|
61 | "system type" |
---|
62 | #elif defined(__powerpc__) || defined(__powerpc64__) |
---|
63 | "machine" |
---|
64 | #elif defined(__s390__) || defined(__s390x__) |
---|
65 | "Manufacturer" |
---|
66 | #elif defined(__sh__) |
---|
67 | "machine" |
---|
68 | #elif defined(sparc) || defined(__sparc__) |
---|
69 | "cpu" |
---|
70 | #elif defined(__vax__) |
---|
71 | "cpu" |
---|
72 | #else |
---|
73 | NULL |
---|
74 | #endif |
---|
75 | ; |
---|
76 | |
---|
77 | #ifdef INET6 |
---|
78 | static char **restore; |
---|
79 | static ssize_t nrestore; |
---|
80 | #endif |
---|
81 | |
---|
82 | char * |
---|
83 | hardware_platform(void) |
---|
84 | { |
---|
85 | FILE *fp; |
---|
86 | char *buf, *p; |
---|
87 | |
---|
88 | if (mproc == NULL) { |
---|
89 | errno = EINVAL; |
---|
90 | return NULL; |
---|
91 | } |
---|
92 | |
---|
93 | fp = fopen("/proc/cpuinfo", "r"); |
---|
94 | if (fp == NULL) |
---|
95 | return NULL; |
---|
96 | |
---|
97 | p = NULL; |
---|
98 | while ((buf = get_line(fp))) { |
---|
99 | if (strncmp(buf, mproc, strlen(mproc)) == 0) { |
---|
100 | p = strchr(buf, ':'); |
---|
101 | if (p != NULL && ++p != NULL) { |
---|
102 | while (*p == ' ') |
---|
103 | p++; |
---|
104 | break; |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | fclose(fp); |
---|
109 | |
---|
110 | if (p == NULL) |
---|
111 | errno = ESRCH; |
---|
112 | return p; |
---|
113 | } |
---|
114 | |
---|
115 | #ifdef INET6 |
---|
116 | static int |
---|
117 | check_proc_int(const char *path) |
---|
118 | { |
---|
119 | FILE *fp; |
---|
120 | char *buf; |
---|
121 | |
---|
122 | fp = fopen(path, "r"); |
---|
123 | if (fp == NULL) |
---|
124 | return -1; |
---|
125 | buf = get_line(fp); |
---|
126 | fclose(fp); |
---|
127 | if (buf == NULL) |
---|
128 | return -1; |
---|
129 | return atoi(buf); |
---|
130 | } |
---|
131 | |
---|
132 | static ssize_t |
---|
133 | write_path(const char *path, const char *val) |
---|
134 | { |
---|
135 | FILE *fp; |
---|
136 | ssize_t r; |
---|
137 | |
---|
138 | fp = fopen(path, "w"); |
---|
139 | if (fp == NULL) |
---|
140 | return -1; |
---|
141 | r = fprintf(fp, "%s\n", val); |
---|
142 | fclose(fp); |
---|
143 | return r; |
---|
144 | } |
---|
145 | |
---|
146 | static const char *prefix = "/proc/sys/net/ipv6/conf"; |
---|
147 | |
---|
148 | static void |
---|
149 | restore_kernel_ra(void) |
---|
150 | { |
---|
151 | char path[256]; |
---|
152 | |
---|
153 | #ifndef DEBUG_MEMORY |
---|
154 | if (options & DHCPCD_FORKED) |
---|
155 | return; |
---|
156 | #endif |
---|
157 | |
---|
158 | for (nrestore--; nrestore >= 0; nrestore--) { |
---|
159 | #ifdef DEBUG_MEMORY |
---|
160 | if (!(options & DHCPCD_FORKED)) { |
---|
161 | #endif |
---|
162 | syslog(LOG_INFO, "%s: restoring Kernel IPv6 RA support", |
---|
163 | restore[nrestore]); |
---|
164 | snprintf(path, sizeof(path), "%s/%s/accept_ra", |
---|
165 | prefix, restore[nrestore]); |
---|
166 | if (write_path(path, "1") == -1 && errno != ENOENT) |
---|
167 | syslog(LOG_ERR, "write_path: %s: %m", path); |
---|
168 | #ifdef DEBUG_MEMORY |
---|
169 | } |
---|
170 | free(restore[nrestore]); |
---|
171 | #endif |
---|
172 | } |
---|
173 | #ifdef DEBUG_MEMORY |
---|
174 | free(restore); |
---|
175 | #endif |
---|
176 | } |
---|
177 | |
---|
178 | int |
---|
179 | check_ipv6(const char *ifname, int own) |
---|
180 | { |
---|
181 | static int ipv6_checked = 0; |
---|
182 | int ra, ex, i; |
---|
183 | char path[256], *p, **nrest; |
---|
184 | |
---|
185 | if (ifname == NULL) { |
---|
186 | if (ipv6_checked) |
---|
187 | return 1; |
---|
188 | ipv6_checked = 1; |
---|
189 | ifname = "all"; |
---|
190 | ex = 1; |
---|
191 | } else |
---|
192 | ex = 0; |
---|
193 | |
---|
194 | snprintf(path, sizeof(path), "%s/%s/autoconf", prefix, ifname); |
---|
195 | i = check_proc_int(path); |
---|
196 | if (i != 1) { |
---|
197 | syslog(LOG_WARNING, "%s: IPv6 kernel autoconf disabled", |
---|
198 | ifname); |
---|
199 | return -1; |
---|
200 | } |
---|
201 | |
---|
202 | snprintf(path, sizeof(path), "%s/%s/accept_ra", prefix, ifname); |
---|
203 | ra = check_proc_int(path); |
---|
204 | if (ra == -1) |
---|
205 | /* The sysctl probably doesn't exist, but this isn't an |
---|
206 | * error as such so just log it and continue */ |
---|
207 | syslog(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, |
---|
208 | "%s: %m", path); |
---|
209 | else if (ra != 0 && own) { |
---|
210 | syslog(LOG_INFO, "%s: disabling Kernel IPv6 RA support", |
---|
211 | ifname); |
---|
212 | if (write_path(path, "0") == -1) { |
---|
213 | syslog(LOG_ERR, "write_path: %s: %m", path); |
---|
214 | return ra; |
---|
215 | } |
---|
216 | for (i = 0; i < nrestore; i++) |
---|
217 | if (strcmp(restore[i], ifname) == 0) |
---|
218 | break; |
---|
219 | if (i == nrestore) { |
---|
220 | p = strdup(ifname); |
---|
221 | if (p == NULL) { |
---|
222 | syslog(LOG_ERR, "%s: %m", __func__); |
---|
223 | return ra; |
---|
224 | } |
---|
225 | nrest = realloc(restore, |
---|
226 | (nrestore + 1) * sizeof(char *)); |
---|
227 | if (nrest == NULL) { |
---|
228 | syslog(LOG_ERR, "%s: %m", __func__); |
---|
229 | return ra; |
---|
230 | } |
---|
231 | restore = nrest; |
---|
232 | restore[nrestore++] = p; |
---|
233 | |
---|
234 | } |
---|
235 | if (ex) |
---|
236 | atexit(restore_kernel_ra); |
---|
237 | } |
---|
238 | |
---|
239 | return ra; |
---|
240 | } |
---|
241 | |
---|
242 | int |
---|
243 | ipv6_dadtransmits(const char *ifname) |
---|
244 | { |
---|
245 | char path[256]; |
---|
246 | int r; |
---|
247 | |
---|
248 | if (ifname == NULL) |
---|
249 | ifname = "default"; |
---|
250 | |
---|
251 | snprintf(path, sizeof(path), "%s/%s/dad_transmits", prefix, ifname); |
---|
252 | r = check_proc_int(path); |
---|
253 | return r < 0 ? 0 : r; |
---|
254 | } |
---|
255 | #endif |
---|