]>
Commit | Line | Data |
---|---|---|
a74aeac6 PR |
1 | /* |
2 | * ifdown.c Find all network interfaces on the system and | |
3 | * shut them down. | |
4 | * | |
5 | * Copyright (C) 1998 Miquel van Smoorenburg. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | * | |
21 | */ | |
22 | char *v_ifdown = "@(#)ifdown.c 1.11 02-Jun-1998 miquels@cistron.nl"; | |
23 | ||
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
26 | #include <unistd.h> | |
27 | #include <time.h> | |
28 | #include <string.h> | |
3b487838 | 29 | #include <errno.h> |
a74aeac6 PR |
30 | |
31 | #include <sys/ioctl.h> | |
32 | #include <sys/socket.h> | |
33 | #include <sys/time.h> | |
a74aeac6 PR |
34 | |
35 | #include <net/if.h> | |
36 | #include <netinet/in.h> | |
37 | ||
38 | #define MAX_IFS 64 | |
39 | ||
47d5242a PR |
40 | /* XXX: Ideally this would get detected at configure time... */ |
41 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ | |
42 | defined(__NetBSD__) || defined(__OpenBSD__) | |
43 | #define HAVE_SOCKADDR_SA_LEN 1 | |
44 | #endif | |
45 | ||
46 | #ifndef _SIZEOF_ADDR_IFREQ | |
47 | #ifdef HAVE_SOCKADDR_SA_LEN | |
48 | #define _SIZEOF_ADDR_IFREQ(ifr) \ | |
49 | ((ifr).ifr_addr.sa_len > sizeof(struct sockaddr) ? \ | |
50 | (sizeof((ifr).ifr_name) + (ifr).ifr_addr.sa_len) : \ | |
51 | sizeof(struct ifreq)) | |
52 | #else | |
53 | #define _SIZEOF_ADDR_IFREQ(ifr) sizeof(struct ifreq) | |
54 | #endif | |
55 | #endif | |
56 | ||
a74aeac6 PR |
57 | /* |
58 | * First, we find all shaper devices and down them. Then we | |
59 | * down all real interfaces. This is because the comment in the | |
60 | * shaper driver says "if you down the shaper device before the | |
61 | * attached inerface your computer will follow". | |
62 | */ | |
63 | int ifdown(void) | |
64 | { | |
47d5242a PR |
65 | char ifr_buf[sizeof(struct ifreq) * MAX_IFS]; |
66 | char *ifr_end; | |
a74aeac6 | 67 | struct ifconf ifc; |
47d5242a | 68 | int fd; |
a74aeac6 PR |
69 | int shaper; |
70 | ||
71 | if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { | |
72 | fprintf(stderr, "ifdown: "); | |
73 | perror("socket"); | |
74 | return -1; | |
75 | } | |
47d5242a PR |
76 | ifc.ifc_len = sizeof(ifr_buf); |
77 | ifc.ifc_buf = ifr_buf; | |
a74aeac6 PR |
78 | |
79 | if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) { | |
80 | fprintf(stderr, "ifdown: "); | |
81 | perror("SIOCGIFCONF"); | |
82 | close(fd); | |
83 | return -1; | |
84 | } | |
47d5242a | 85 | ifr_end = ifr_buf + ifc.ifc_len; |
a74aeac6 PR |
86 | |
87 | for (shaper = 1; shaper >= 0; shaper--) { | |
47d5242a PR |
88 | char *ifr_next = ifr_buf; |
89 | ||
90 | while (ifr_next < ifr_end) { | |
91 | struct ifreq *ifr; | |
92 | int flags; | |
a74aeac6 | 93 | |
47d5242a PR |
94 | ifr = (struct ifreq *)ifr_next; |
95 | ifr_next += _SIZEOF_ADDR_IFREQ(*ifr); | |
96 | ||
97 | if ((strncmp(ifr->ifr_name, "shaper", 6) == 0) | |
a74aeac6 PR |
98 | != shaper) continue; |
99 | ||
47d5242a | 100 | if (strncmp(ifr->ifr_name, "lo", 2) == 0) |
a74aeac6 | 101 | continue; |
47d5242a | 102 | if (strchr(ifr->ifr_name, ':') != NULL) |
a74aeac6 | 103 | continue; |
55f242bf DWF |
104 | |
105 | /* Read interface flags */ | |
47d5242a | 106 | if (ioctl(fd, SIOCGIFFLAGS, ifr) < 0) { |
a74aeac6 | 107 | fprintf(stderr, "ifdown: shutdown "); |
47d5242a | 108 | perror(ifr->ifr_name); |
55f242bf | 109 | continue; |
a74aeac6 | 110 | } |
55f242bf DWF |
111 | /* |
112 | * Expected in <net/if.h> according to | |
113 | * "UNIX Network Programming". | |
114 | */ | |
47d5242a PR |
115 | #ifdef ifr_flagshigh |
116 | flags = (ifr->ifr_flags & 0xffff) | | |
117 | (ifr->ifr_flagshigh << 16); | |
118 | #else | |
119 | flags = ifr->ifr_flags; | |
120 | #endif | |
121 | if (flags & IFF_UP) { | |
122 | flags &= ~(IFF_UP); | |
123 | #ifdef ifr_flagshigh | |
124 | ifr->ifr_flags = flags & 0xffff; | |
125 | ifr->ifr_flagshigh = flags >> 16; | |
126 | #else | |
127 | ifr->ifr_flags = flags; | |
55f242bf | 128 | #endif |
47d5242a | 129 | if (ioctl(fd, SIOCSIFFLAGS, ifr) < 0) { |
55f242bf | 130 | fprintf(stderr, "ifdown: shutdown "); |
47d5242a | 131 | perror(ifr->ifr_name); |
55f242bf DWF |
132 | } |
133 | } | |
a74aeac6 PR |
134 | } |
135 | } | |
136 | close(fd); | |
137 | ||
138 | return 0; | |
139 | } |