Debian Codegraph @

ifconfig/system/hurd.c

raw
1 /* hurd.c -- Code for ifconfig specific to GNU/Hurd.
2 Copyright (C) 2015-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /* Mostly written by Marcus Brinkmann.
20 Adaptions to GNU/Hurd by Mats Erik Andersson. */
21
22 #include <config.h>
23
24 #include <stdlib.h>
25 #include <sys/ioctl.h>
26 #include <net/if_arp.h>
27 #include <hurd.h>
28 #include <hurd/paths.h>
29 #include <hurd/fsys.h>
30 #include <argz.h>
31 #include "../ifconfig.h"
32
33 #include <attribute.h>
34
35
36 /* Output format stuff. */
37
38 const char *system_default_format = "gnu";
39
40
41 /* Check that pfinet is driving the given interface name. */
42 static int
43 check_driving (const char *name)
44 {
45 file_t node;
46 fsys_t fsys;
47 error_t err;
48
49 char *argz = 0, *new_argz = 0;
50 size_t argz_len = 0;
51 mach_msg_type_number_t argz_len_for_fs_get_options = 0;
52 char *entry = 0;
53 const char *socket = _SERVERS_SOCKET "/2";
54
55 int ret = 0;
56
57 if (strcmp (name, "lo") == 0)
58 /* Always configured. */
59 return 1;
60
61 node = file_name_lookup (socket, 0, 0666);
62 if (node == MACH_PORT_NULL)
63 {
64 error (0, 0, "Interface name %s does not exist", name);
65 return 0;
66 }
67
68 err = file_get_fs_options (node, &argz, &argz_len_for_fs_get_options);
69 if (err)
70 {
71 error (0, err, "Could not get fs options of %s", socket);
72 return 0;
73 }
74
75 argz_len = argz_len_for_fs_get_options;
76
77 for (entry = argz; entry; entry = argz_next (argz, argz_len, entry))
78 {
79 if (strcmp (entry, "-i") == 0)
80 {
81 char *ifname = argz_next (argz, argz_len, entry);
82
83 if (strcmp (ifname, name) == 0)
84 {
85 /* Already there. */
86 ret = 1;
87 goto out;
88 }
89 }
90
91 else if (strncmp (entry, "--interface=", 12) == 0)
92 {
93 if (strcmp (entry + 12, name) == 0)
94 {
95 /* Already there. */
96 ret = 1;
97 goto out;
98 }
99 }
100 }
101
102 /* Not already there. */
103
104 err = file_getcontrol (node, &fsys);
105 if (err)
106 {
107 if (err == EPERM)
108 error (0, err, "Could not make pfinet %s drive %s", socket, name);
109 else
110 error (0, err, "Could not get control of %s", socket);
111 goto out;
112 }
113
114 new_argz = malloc (argz_len);
115 memcpy (new_argz, argz, argz_len);
116
117 err = argz_insert (&new_argz, &argz_len, new_argz, name);
118 if (err)
119 {
120 error (0, err, "Could not prepend name %s to '%s' for %s", name,
121 new_argz, socket);
122 goto out;
123 }
124
125 err = argz_insert (&new_argz, &argz_len, new_argz, "-i");
126 if (err)
127 {
128 argz_stringify (new_argz, argz_len, ' ');
129 error (0, err, "Could not prepend -i to '%s' for %s", new_argz, socket);
130 goto out;
131 }
132
133 err = fsys_set_options (fsys, new_argz, argz_len, 1);
134 if (err)
135 {
136 argz_stringify (new_argz, argz_len, ' ');
137 error (0, err, "Could not make pfinet %s drive %s with '%s'", socket,
138 name, new_argz);
139 goto out;
140 }
141
142 ret = 1;
143
144 out:
145 free (new_argz);
146 vm_deallocate (mach_task_self (), (vm_offset_t) argz, argz_len);
147 mach_port_deallocate (mach_task_self (), node);
148
149 return ret;
150 }
151
152 /* Argument parsing stuff. */
153
154 const char *system_help = "NAME [ADDR]\
155 [broadcast BRDADDR] [netmask MASK]\
156 [mtu N] [up|down] [FLAGS]";
157
158 struct argp_child system_argp_child;
159
160 int
161 system_parse_opt (struct ifconfig **ifp MAYBE_UNUSED,
162 char option MAYBE_UNUSED, char *optarg MAYBE_UNUSED)
163 {
164 return 0;
165 }
166
167 int
168 system_parse_opt_rest (struct ifconfig **ifp, int argc, char *argv[])
169 {
170 int i = 0, mask, rev;
171 enum
172 {
173 EXPECT_NOTHING,
174 EXPECT_AF,
175 EXPECT_BROADCAST,
176 EXPECT_NETMASK,
177 EXPECT_METRIC,
178 EXPECT_MTU
179 } expect = EXPECT_AF;
180
181 *ifp = parse_opt_new_ifs (argv[0]);
182
183 while (++i < argc)
184 {
185 switch (expect)
186 {
187 case EXPECT_BROADCAST:
188 parse_opt_set_brdaddr (*ifp, argv[i]);
189 break;
190
191 case EXPECT_NETMASK:
192 parse_opt_set_netmask (*ifp, argv[i]);
193 break;
194
195 case EXPECT_MTU:
196 parse_opt_set_mtu (*ifp, argv[i]);
197 break;
198
199 /* XXX: 2015-07-18, GNU/Hurd does not yet support
200 ioctl(SIOCSIFMETRIC), but we let the code
201 handle this standard ability anyway!
202 */
203 case EXPECT_METRIC:
204 parse_opt_set_metric (*ifp, argv[i]);
205 break;
206
207 case EXPECT_AF:
208 expect = EXPECT_NOTHING;
209 if (!strcmp (argv[i], "inet"))
210 continue;
211 else if (!strcmp (argv[i], "inet6"))
212 {
213 error (0, 0, "%s is not a supported address family", argv[i]);
214 return 0;
215 }
216 break;
217
218 case EXPECT_NOTHING:
219 break;
220 }
221
222 if (expect != EXPECT_NOTHING)
223 expect = EXPECT_NOTHING;
224 else if (!strcmp (argv[i], "broadcast"))
225 expect = EXPECT_BROADCAST;
226 else if (!strcmp (argv[i], "netmask"))
227 expect = EXPECT_NETMASK;
228 else if (!strcmp (argv[i], "metric"))
229 expect = EXPECT_METRIC;
230 else if (!strcmp (argv[i], "mtu"))
231 expect = EXPECT_MTU;
232 else if (!strcmp (argv[i], "up"))
233 parse_opt_set_flag (*ifp, IFF_UP | IFF_RUNNING, 0);
234 else if (!strcmp (argv[i], "down"))
235 parse_opt_set_flag (*ifp, IFF_UP, 1);
236 else if (((mask = if_nameztoflag (argv[i], &rev))
237 & ~IU_IFF_CANTCHANGE) != 0)
238 parse_opt_set_flag (*ifp, mask, rev);
239 else
240 {
241 if (!((*ifp)->valid & IF_VALID_ADDR))
242 parse_opt_set_address (*ifp, argv[i]);
243 else if (!((*ifp)->valid & IF_VALID_DSTADDR))
244 parse_opt_set_dstaddr (*ifp, argv[i]);
245 }
246 }
247
248 switch (expect)
249 {
250 case EXPECT_BROADCAST:
251 error (0, 0, "option `broadcast' requires an argument");
252 break;
253
254 case EXPECT_NETMASK:
255 error (0, 0, "option `netmask' requires an argument");
256 break;
257
258 case EXPECT_METRIC:
259 error (0, 0, "option `metric' requires an argument");
260 break;
261
262 case EXPECT_MTU:
263 error (0, 0, "option `mtu' requires an argument");
264 break;
265
266 case EXPECT_AF:
267 case EXPECT_NOTHING:
268 return 1;
269 }
270
271 return 0;
272 }
273
274 int
275 system_preconfigure (int sfd MAYBE_UNUSED, struct ifreq *ifr MAYBE_UNUSED)
276 {
277 if (!check_driving (ifr->ifr_name))
278 return -1;
279 return 0;
280 }
281
282 int
283 system_configure (int sfd MAYBE_UNUSED,
284 struct ifreq *ifr MAYBE_UNUSED,
285 struct system_ifconfig *ifs MAYBE_UNUSED)
286 {
287 return 0;
288 }
289
290 struct if_nameindex *(*system_if_nameindex) (void) = if_nameindex;
291
292 static void
293 print_hwaddr_ether (format_data_t form MAYBE_UNUSED, unsigned char *data)
294 {
295 *column += printf ("%02X:%02X:%02X:%02X:%02X:%02X",
296 data[0], data[1], data[2], data[3], data[4], data[5]);
297 had_output = 1;
298 }
299
300 /* GNU/Hurd and Mach are using a mixture of BSD definitions
301 * and GNU/Linux interface headers, which in this situation
302 * means that sa_family_t is an unsigned char, from BSD, while
303 * all ARPHRD_* come from GNU/Linux and are thus 16 bits wide.
304 * We must account for this. The following bitmask will
305 * adapt to any future change!
306 */
307
308 #define _ARP_MASK ((sizeof (sa_family_t) == 1) ? 0xff : 0xffff)
309
310 struct arphrd_symbol
311 {
312 const char *name;
313 const char *title;
314 int value;
315 void (*print_hwaddr) (format_data_t form, unsigned char *data);
316 } arphrd_symbols[] = {
317 #ifdef ARPHRD_ETHER /* Ethernet 10/100Mbps. */
318 {"ETHER", "Ethernet", ARPHRD_ETHER & _ARP_MASK, print_hwaddr_ether},
319 #endif
320 #ifdef ARPHRD_LOOPBACK /* Loopback device. */
321 {"LOOPBACK", "Local Loopback", ARPHRD_LOOPBACK & _ARP_MASK, NULL},
322 #endif
323 {NULL, NULL, 0, NULL}
324 };
325
326 struct arphrd_symbol *
327 arphrd_findvalue (int value)
328 {
329 struct arphrd_symbol *arp = arphrd_symbols;
330 while (arp->name != NULL)
331 {
332 if (arp->value == value)
333 break;
334 arp++;
335 }
336 if (arp->name)
337 return arp;
338 else
339 return NULL;
340 }
341
342 void
343 system_fh_hwaddr_query (format_data_t form, int argc, char *argv[])
344 {
345 #ifdef SIOCGIFHWADDR
346 struct arphrd_symbol *arp;
347
348 if (ioctl (form->sfd, SIOCGIFHWADDR, form->ifr) < 0)
349 select_arg (form, argc, argv, 1);
350
351 arp = arphrd_findvalue (form->ifr->ifr_hwaddr.sa_family);
352 select_arg (form, argc, argv, (arp && arp->print_hwaddr) ? 0 : 1);
353 #else
354 select_arg (form, argc, argv, 1);
355 #endif
356 }
357
358 void
359 system_fh_hwaddr (format_data_t form, int argc MAYBE_UNUSED,
360 MAYBE_UNUSED char *argv[])
361 {
362 #ifdef SIOCGIFHWADDR
363 if (ioctl (form->sfd, SIOCGIFHWADDR, form->ifr) < 0)
364 error (EXIT_FAILURE, errno,
365 "SIOCGIFHWADDR failed for interface `%s'", form->ifr->ifr_name);
366 else
367 {
368 struct arphrd_symbol *arp;
369
370 arp = arphrd_findvalue (form->ifr->ifr_hwaddr.sa_family);
371 if (arp && arp->print_hwaddr)
372 arp->print_hwaddr (form,
373 (unsigned char *) form->ifr->ifr_hwaddr.sa_data);
374 else
375 put_string (form, "(hwaddr unknown)");
376 }
377 #else
378 *column += printf ("(not available)");
379 had_output = 1;
380 #endif
381 }
382
383 void
384 system_fh_hwtype_query (format_data_t form, int argc, char *argv[])
385 {
386 #ifdef SIOCGIFHWADDR
387 if (ioctl (form->sfd, SIOCGIFHWADDR, form->ifr) >= 0)
388 select_arg (form, argc, argv, 0);
389 else
390 #endif
391 select_arg (form, argc, argv, 1);
392 }
393
394 void
395 system_fh_hwtype (format_data_t form, int argc MAYBE_UNUSED,
396 MAYBE_UNUSED char *argv[])
397 {
398 #ifdef SIOCGIFHWADDR
399 if (ioctl (form->sfd, SIOCGIFHWADDR, form->ifr) < 0)
400 error (EXIT_FAILURE, errno,
401 "SIOCGIFHWADDR failed for interface `%s'", form->ifr->ifr_name);
402 else
403 {
404 struct arphrd_symbol *arp;
405
406 arp = arphrd_findvalue (form->ifr->ifr_hwaddr.sa_family);
407 if (arp)
408 put_string (form, arp->title);
409 else
410 put_string (form, "(hwtype unknown)");
411 }
412 #else
413 *column += printf ("(not available)");
414 had_output = 1;
415 #endif
416 }