Debian Codegraph @

ifconfig/changeif.c

raw
1 /* changeif.c -- change the configuration of a network interface
2 Copyright (C) 2001-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 /* Written by Marcus Brinkmann. */
20
21 #include <config.h>
22
23 #include <stdio.h>
24 #include <errno.h>
25 #include <string.h>
26
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/ioctl.h>
30 #include <net/if.h>
31 #include <netinet/in.h>
32 #ifdef HAVE_NETINET_ETHER_H
33 # include <netinet/ether.h>
34 #endif
35 #include <arpa/inet.h>
36 #include <netdb.h>
37 #include "ifconfig.h"
38
39 /* Necessary for BSD systems. */
40 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
41 # define SET_SIN_LEN sin->sin_len = sizeof (struct sockaddr_in);
42 #else /* !HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
43 # define SET_SIN_LEN
44 #endif
45
46 #define SIOCSIF(type, addr) \
47 int err = 0; \
48 struct sockaddr_in *sin = (struct sockaddr_in *) &ifr->ifr_addr; \
49 \
50 sin->sin_family = AF_INET; \
51 err = inet_aton (addr, &sin->sin_addr); \
52 if (!err) \
53 { \
54 error (0, 0, "`%s' is not a valid address", addr); \
55 return -1; \
56 } \
57 SET_SIN_LEN \
58 err = ioctl (sfd, SIOCSIF##type, ifr); \
59 if (err < 0) \
60 { \
61 error (0, errno, "%s failed", "SIOCSIF" #type); \
62 return -1; \
63 }
64
65 #if !HAVE_DECL_GETADDRINFO
66 extern void herror (const char *pfx);
67 #endif
68
69 /* Set address of interface in IFR. Destroys union in IFR, but leaves
70 ifr_name intact. ADDRESS may be an IP number or a hostname that
71 can be resolved. */
72 int
73 set_address (int sfd, struct ifreq *ifr, char *address)
74 {
75 #ifndef SIOCSIFADDR
76 error (0, 0, "don't know how to set an interface address on this system");
77 return -1;
78 #else
79 # if HAVE_DECL_GETADDRINFO
80 int rc;
81 char addr[INET_ADDRSTRLEN];
82 struct addrinfo hints, *ai, *res;
83
84 memset (&hints, 0, sizeof (hints));
85 hints.ai_family = AF_INET;
86
87 rc = getaddrinfo (address, NULL, &hints, &res);
88 if (rc)
89 {
90 error (0, 0, "cannot resolve `%s': %s", address, gai_strerror (rc));
91 return -1;
92 }
93 for (ai = res; ai; ai = ai->ai_next)
94 if (ai->ai_family == AF_INET)
95 break;
96
97 if (ai == NULL)
98 {
99 error (0, 0, "`%s' refers to an unknown address type", address);
100 freeaddrinfo (res);
101 return -1;
102 }
103
104 rc = getnameinfo (ai->ai_addr, ai->ai_addrlen,
105 addr, sizeof (addr), NULL, 0, NI_NUMERICHOST);
106 freeaddrinfo (res);
107 if (rc)
108 {
109 error (0, 0, "cannot resolve `%s': %s", address, gai_strerror (rc));
110 return -1;
111 }
112 # else/* !HAVE_DECL_GETADDRINFO */
113 char *addr;
114 struct hostent *host = gethostbyname (address);
115
116 if (!host)
117 {
118 error (0, 0, "cannot resolve `%s': %s", address, hstrerror (h_errno));
119 return -1;
120 }
121 if (host->h_addrtype != AF_INET)
122 {
123 error (0, 0, "`%s' refers to an unknown address type", address);
124 return -1;
125 }
126
127 addr = inet_ntoa (*((struct in_addr *) host->h_addr));
128 # endif/* !HAVE_DECL_GETADDRINFO */
129
130 {
131 SIOCSIF (ADDR, addr) if (verbose)
132 printf ("Set interface address of `%s' to %s.\n",
133 ifr->ifr_name, inet_ntoa (sin->sin_addr));
134 }
135 return 0;
136 #endif
137 }
138
139 int
140 set_netmask (int sfd, struct ifreq *ifr, char *netmask)
141 {
142 #ifndef SIOCSIFNETMASK
143 error (0, 0, "don't know how to set an interface netmask on this system");
144 return -1;
145 #else
146
147 SIOCSIF (NETMASK, netmask) if (verbose)
148 printf ("Set interface netmask of `%s' to %s.\n",
149 ifr->ifr_name, inet_ntoa (sin->sin_addr));
150 return 0;
151 #endif
152 }
153
154 int
155 set_dstaddr (int sfd, struct ifreq *ifr, char *dstaddr)
156 {
157 #ifndef SIOCSIFDSTADDR
158 error (0, 0,
159 "don't know how to set an interface peer address on this system");
160 return -1;
161 #else /* !SIOCSIFDSTADDR */
162 # if HAVE_DECL_GETADDRINFO
163 int rc;
164 char addr[INET_ADDRSTRLEN];
165 struct addrinfo hints, *ai, *res;
166
167 memset (&hints, 0, sizeof (hints));
168 hints.ai_family = AF_INET;
169
170 rc = getaddrinfo (dstaddr, NULL, &hints, &res);
171 if (rc)
172 {
173 error (0, 0, "cannot resolve `%s': %s", dstaddr, gai_strerror (rc));
174 return -1;
175 }
176 for (ai = res; ai; ai = ai->ai_next)
177 if (ai->ai_family == AF_INET)
178 break;
179
180 if (ai == NULL)
181 {
182 error (0, 0, "`%s' refers to an unknown address type", dstaddr);
183 freeaddrinfo (res);
184 return -1;
185 }
186
187 rc = getnameinfo (ai->ai_addr, ai->ai_addrlen,
188 addr, sizeof (addr), NULL, 0, NI_NUMERICHOST);
189 freeaddrinfo (res);
190 if (rc)
191 {
192 error (0, 0, "cannot resolve `%s': %s", dstaddr, gai_strerror (rc));
193 return -1;
194 }
195 # else/* !HAVE_DECL_GETADDRINFO */
196 char *addr;
197 struct hostent *host = gethostbyname (dstaddr);
198
199 if (!host)
200 {
201 error (0, 0, "cannot resolve `%s': %s", dstaddr, hstrerror (h_errno));
202 return -1;
203 }
204 if (host->h_addrtype != AF_INET)
205 {
206 error (0, 0, "`%s' refers to an unknown address type", dstaddr);
207 return -1;
208 }
209
210 addr = inet_ntoa (*((struct in_addr *) host->h_addr));
211 # endif/* !HAVE_DECL_GETADDRINFO */
212
213 SIOCSIF (DSTADDR, addr) if (verbose)
214 printf ("Set interface peer address of `%s' to %s.\n",
215 ifr->ifr_name, inet_ntoa (sin->sin_addr));
216 return 0;
217 #endif /* SIOCSIFDSTADDR */
218 }
219
220 int
221 set_brdaddr (int sfd, struct ifreq *ifr, char *brdaddr)
222 {
223 #ifndef SIOCSIFBRDADDR
224 error (0, 0,
225 "don't know how to set an interface broadcast address on this system");
226 return -1;
227 #else
228 SIOCSIF (BRDADDR, brdaddr) if (verbose)
229 printf ("Set interface broadcast address of `%s' to %s.\n",
230 ifr->ifr_name, inet_ntoa (sin->sin_addr));
231 return 0;
232 #endif
233 }
234
235 int
236 set_hwaddr (int sfd, struct ifreq *ifr, char *hwaddr)
237 {
238 #ifndef SIOCSIFHWADDR
239 error (0, 0, "don't know how to set a hardware address on this system");
240 return -1;
241 #else
242 # ifndef ifr_hwaddr
243 # ifdef ifr_enaddr
244 # define ifr_hwaddr ifr_enaddr
245 # endif
246 /* ifr_en_addr */
247 # endif/* ifr_hwaddr */
248 int err;
249 struct ether_addr *ether;
250 struct sockaddr *sa = (struct sockaddr *) &ifr->ifr_hwaddr;
251 struct ether_addr addr;
252
253 ether = ether_aton (hwaddr);
254 if (!ether)
255 {
256 err = ether_hostton (hwaddr, &addr);
257 if (err)
258 {
259 error (0, 0, "`%s' is not a valid hardware address", hwaddr);
260 return -1;
261 }
262
263 ether = &addr;
264 }
265
266 /* Reading the present hardware address is a simple
267 * way of initializing the structure!
268 */
269 (void) ioctl (sfd, SIOCGIFHWADDR, ifr);
270
271 memcpy (&sa->sa_data, ether, sizeof (*ether));
272 err = ioctl (sfd, SIOCSIFHWADDR, ifr);
273 if (err < 0)
274 {
275 error (0, errno, "%s failed", "SIOCSIFHWADDR");
276 return -1;
277 }
278
279 if (verbose)
280 printf ("Set interface hardware address of `%s' to %s.\n",
281 ifr->ifr_name, ether_ntoa (ether));
282 return 0;
283 #endif /* SIOCSIFHWADDR */
284 }
285
286 int
287 set_mtu (int sfd, struct ifreq *ifr, int mtu)
288 {
289 #ifndef SIOCSIFMTU
290 error (0, 0, "don't know how to set the interface mtu on this system");
291 return -1;
292 #else
293 int err = 0;
294
295 ifr->ifr_mtu = mtu;
296 err = ioctl (sfd, SIOCSIFMTU, ifr);
297 if (err < 0)
298 {
299 error (0, errno, "SIOCSIFMTU failed");
300 return -1;
301 }
302 if (verbose)
303 printf ("Set mtu value of `%s' to `%i'.\n", ifr->ifr_name, ifr->ifr_mtu);
304 return 0;
305 #endif
306 }
307
308 int
309 set_metric (int sfd, struct ifreq *ifr, int metric)
310 {
311 #ifndef SIOCSIFMETRIC
312 error (0, 0, "don't know how to set the interface metric on this system");
313 return -1;
314 #else
315 int err = 0;
316
317 ifr->ifr_metric = metric;
318 err = ioctl (sfd, SIOCSIFMETRIC, ifr);
319 if (err < 0)
320 {
321 error (0, errno, "SIOCSIFMETRIC failed");
322 return -1;
323 }
324 if (verbose)
325 printf ("Set metric value of `%s' to `%i'.\n",
326 ifr->ifr_name, ifr->ifr_metric);
327 return 0;
328 #endif
329 }
330
331 int
332 set_flags (int sfd, struct ifreq *ifr, int setflags, int clrflags)
333 {
334 #if !defined SIOCGIFFLAGS || !defined SIOCSIFFLAGS
335 error (0, 0, "don't know how to set the interface flags on this system");
336 return -1;
337 #else
338 struct ifreq tifr = *ifr;
339
340 if (ioctl (sfd, SIOCGIFFLAGS, &tifr) < 0)
341 {
342 error (0, errno, "SIOCGIFFLAGS failed");
343 return -1;
344 }
345 /* Some systems, notably FreeBSD, use two short integers. */
346 ifr->ifr_flags = (tifr.ifr_flags | setflags) & ~clrflags & 0xffff;
347
348 # ifdef ifr_flagshigh
349 ifr->ifr_flagshigh = (tifr.ifr_flagshigh | (setflags >> 16))
350 & ~(clrflags >> 16);
351 # endif/* ifr_flagshigh */
352
353 if (ioctl (sfd, SIOCSIFFLAGS, ifr) < 0)
354 {
355 error (0, errno, "SIOCSIFFLAGS failed");
356 return -1;
357 }
358
359 if (verbose)
360 {
361 printf ("Setting %sflags", setflags ? "" : "no ");
362
363 if (setflags)
364 {
365 printf (" `");
366 print_if_flags (setflags, NULL, ',');
367 putchar ('\'');
368 }
369
370 printf (" of `%s'", ifr->ifr_name);
371
372 if (clrflags)
373 {
374 printf (", clearing `");
375 print_if_flags (clrflags, NULL, ',');
376 putchar ('\'');
377 }
378
379 printf (".\n");
380 }
381
382 return 0;
383 #endif
384 }
385
386 int
387 configure_if (int sfd, struct ifconfig *ifp)
388 {
389 int err = 0;
390 struct ifreq ifr;
391
392 memset (&ifr, 0, sizeof (ifr));
393 strncpy (ifr.ifr_name, ifp->name, IFNAMSIZ);
394 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
395
396 err = system_preconfigure (sfd, &ifr);
397 if (!err && ifp->valid & IF_VALID_ADDR)
398 err = set_address (sfd, &ifr, ifp->address);
399 if (!err && ifp->valid & IF_VALID_NETMASK)
400 err = set_netmask (sfd, &ifr, ifp->netmask);
401 if (!err && ifp->valid & IF_VALID_DSTADDR)
402 err = set_dstaddr (sfd, &ifr, ifp->dstaddr);
403 if (!err && ifp->valid & IF_VALID_BRDADDR)
404 err = set_brdaddr (sfd, &ifr, ifp->brdaddr);
405 if (!err && ifp->valid & IF_VALID_HWADDR)
406 err = set_hwaddr (sfd, &ifr, ifp->hwaddr);
407 if (!err && ifp->valid & IF_VALID_MTU)
408 err = set_mtu (sfd, &ifr, ifp->mtu);
409 if (!err && ifp->valid & IF_VALID_METRIC)
410 err = set_metric (sfd, &ifr, ifp->metric);
411 if (!err && ifp->valid & IF_VALID_SYSTEM)
412 err = system_configure (sfd, &ifr, ifp->system);
413 if (!err && (ifp->setflags || ifp->clrflags))
414 err = set_flags (sfd, &ifr, ifp->setflags, ifp->clrflags);
415 if (!err && ifp->valid & IF_VALID_FORMAT)
416 print_interface (sfd, ifp->name, &ifr, ifp->format);
417 return err;
418 }