diff options
author | Rob Landley <rob@landley.net> | 2005-05-03 03:28:55 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2005-05-03 03:28:55 +0000 |
commit | e3752e56a43a823f4aaaa9a4374ccc367417b942 (patch) | |
tree | b954cd7801fd3a30f8a5231ee0106c8db11da568 | |
parent | 8445a9ff99f04cad8845a1aed6ab17737f2d985a (diff) | |
download | busybox-w32-e3752e56a43a823f4aaaa9a4374ccc367417b942.tar.gz busybox-w32-e3752e56a43a823f4aaaa9a4374ccc367417b942.tar.bz2 busybox-w32-e3752e56a43a823f4aaaa9a4374ccc367417b942.zip |
Revert Tito's patch to zcip. My bad, David Brownell had objected and I missed
it...
-rw-r--r-- | include/libbb.h | 7 | ||||
-rw-r--r-- | libbb/setup_environment.c | 2 | ||||
-rw-r--r-- | networking/zcip.c | 295 |
3 files changed, 190 insertions, 114 deletions
diff --git a/include/libbb.h b/include/libbb.h index f2401374b..6ae5c4d64 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -43,7 +43,7 @@ | |||
43 | 43 | ||
44 | #include "config.h" | 44 | #include "config.h" |
45 | #ifdef CONFIG_SELINUX | 45 | #ifdef CONFIG_SELINUX |
46 | #include <proc_secure.h> | 46 | #include <selinux/selinux.h> |
47 | #endif | 47 | #endif |
48 | 48 | ||
49 | #include "pwd_.h" | 49 | #include "pwd_.h" |
@@ -425,12 +425,11 @@ extern void change_identity ( const struct passwd *pw ); | |||
425 | extern const char *change_identity_e2str ( const struct passwd *pw ); | 425 | extern const char *change_identity_e2str ( const struct passwd *pw ); |
426 | extern void run_shell ( const char *shell, int loginshell, const char *command, const char **additional_args | 426 | extern void run_shell ( const char *shell, int loginshell, const char *command, const char **additional_args |
427 | #ifdef CONFIG_SELINUX | 427 | #ifdef CONFIG_SELINUX |
428 | , security_id_t sid | 428 | , security_context_t sid |
429 | #endif | 429 | #endif |
430 | ); | 430 | ); |
431 | extern int run_parts(char **args, const unsigned char test_mode, char **env); | 431 | extern int run_parts(char **args, const unsigned char test_mode, char **env); |
432 | extern int restricted_shell ( const char *shell ); | 432 | extern int restricted_shell ( const char *shell ); |
433 | extern void xsetenv ( const char *key, const char *value ); | ||
434 | extern void setup_environment ( const char *shell, int loginshell, int changeenv, const struct passwd *pw ); | 433 | extern void setup_environment ( const char *shell, int loginshell, int changeenv, const struct passwd *pw ); |
435 | extern int correct_password ( const struct passwd *pw ); | 434 | extern int correct_password ( const struct passwd *pw ); |
436 | extern char *pw_encrypt(const char *clear, const char *salt); | 435 | extern char *pw_encrypt(const char *clear, const char *salt); |
@@ -461,7 +460,7 @@ typedef struct { | |||
461 | 460 | ||
462 | extern procps_status_t * procps_scan(int save_user_arg0 | 461 | extern procps_status_t * procps_scan(int save_user_arg0 |
463 | #ifdef CONFIG_SELINUX | 462 | #ifdef CONFIG_SELINUX |
464 | , int use_selinux, security_id_t *sid | 463 | , int use_selinux, security_context_t *sid |
465 | #endif | 464 | #endif |
466 | ); | 465 | ); |
467 | extern unsigned short compare_string_array(const char *string_array[], const char *key); | 466 | extern unsigned short compare_string_array(const char *string_array[], const char *key); |
diff --git a/libbb/setup_environment.c b/libbb/setup_environment.c index 046ecea34..aeb285a53 100644 --- a/libbb/setup_environment.c +++ b/libbb/setup_environment.c | |||
@@ -42,7 +42,7 @@ | |||
42 | #define DEFAULT_LOGIN_PATH "/bin:/usr/bin" | 42 | #define DEFAULT_LOGIN_PATH "/bin:/usr/bin" |
43 | #define DEFAULT_ROOT_LOGIN_PATH "/usr/sbin:/bin:/usr/bin:/sbin" | 43 | #define DEFAULT_ROOT_LOGIN_PATH "/usr/sbin:/bin:/usr/bin:/sbin" |
44 | 44 | ||
45 | void xsetenv ( const char *key, const char *value ) | 45 | static void xsetenv ( const char *key, const char *value ) |
46 | { | 46 | { |
47 | if ( setenv ( key, value, 1 )) | 47 | if ( setenv ( key, value, 1 )) |
48 | bb_error_msg_and_die (bb_msg_memory_exhausted); | 48 | bb_error_msg_and_die (bb_msg_memory_exhausted); |
diff --git a/networking/zcip.c b/networking/zcip.c index de9ea7675..ccf399bb7 100644 --- a/networking/zcip.c +++ b/networking/zcip.c | |||
@@ -31,12 +31,14 @@ | |||
31 | * certainly be used. Its naming is built over multicast DNS. | 31 | * certainly be used. Its naming is built over multicast DNS. |
32 | */ | 32 | */ |
33 | 33 | ||
34 | /* TODO: | 34 | // #define DEBUG |
35 | - more real-world usage/testing, especially daemon mode | 35 | |
36 | - kernel packet filters to reduce scheduling noise | 36 | // TODO: |
37 | - avoid silent script failures, especially under load... | 37 | // - more real-world usage/testing, especially daemon mode |
38 | - link status monitoring (restart on link-up; stop on link-down) | 38 | // - kernel packet filters to reduce scheduling noise |
39 | */ | 39 | // - avoid silent script failures, especially under load... |
40 | // - link status monitoring (restart on link-up; stop on link-down) | ||
41 | |||
40 | #include <errno.h> | 42 | #include <errno.h> |
41 | #include <stdlib.h> | 43 | #include <stdlib.h> |
42 | #include <stdio.h> | 44 | #include <stdio.h> |
@@ -61,12 +63,11 @@ | |||
61 | 63 | ||
62 | #include <linux/if_packet.h> | 64 | #include <linux/if_packet.h> |
63 | #include <linux/sockios.h> | 65 | #include <linux/sockios.h> |
64 | #include "busybox.h" | 66 | |
65 | #include "libbb.h" | ||
66 | 67 | ||
67 | struct arp_packet { | 68 | struct arp_packet { |
68 | struct ether_header hdr; | 69 | struct ether_header hdr; |
69 | /* FIXME this part is netinet/if_ether.h "struct ether_arp" */ | 70 | // FIXME this part is netinet/if_ether.h "struct ether_arp" |
70 | struct arphdr arp; | 71 | struct arphdr arp; |
71 | struct ether_addr source_addr; | 72 | struct ether_addr source_addr; |
72 | struct in_addr source_ip; | 73 | struct in_addr source_ip; |
@@ -89,13 +90,29 @@ static const unsigned ANNOUNCE_NUM = 2; | |||
89 | static const unsigned ANNOUNCE_INTERVAL = 2; | 90 | static const unsigned ANNOUNCE_INTERVAL = 2; |
90 | static const time_t DEFEND_INTERVAL = 10; | 91 | static const time_t DEFEND_INTERVAL = 10; |
91 | 92 | ||
92 | #define ZCIP_VERSION "0.75 (18 April 2005)" | 93 | static const unsigned char ZCIP_VERSION[] = "0.75 (18 April 2005)"; |
94 | static char *prog; | ||
93 | 95 | ||
94 | static const struct in_addr null_ip = { 0 }; | 96 | static const struct in_addr null_ip = { 0 }; |
95 | static const struct ether_addr null_addr = { {0, 0, 0, 0, 0, 0} }; | 97 | static const struct ether_addr null_addr = { {0, 0, 0, 0, 0, 0} }; |
96 | 98 | ||
99 | static int verbose = 0; | ||
97 | 100 | ||
98 | /* | 101 | #ifdef DEBUG |
102 | |||
103 | #define DBG(fmt,args...) \ | ||
104 | fprintf(stderr, "%s: " fmt , prog , ## args) | ||
105 | #define VDBG(fmt,args...) do { \ | ||
106 | if (verbose) fprintf(stderr, "%s: " fmt , prog ,## args); \ | ||
107 | } while (0) | ||
108 | #else | ||
109 | |||
110 | #define DBG(fmt,args...) \ | ||
111 | do { } while (0) | ||
112 | #define VDBG DBG | ||
113 | #endif /* DEBUG */ | ||
114 | |||
115 | /** | ||
99 | * Pick a random link local IP address on 169.254/16, except that | 116 | * Pick a random link local IP address on 169.254/16, except that |
100 | * the first and last 256 addresses are reserved. | 117 | * the first and last 256 addresses are reserved. |
101 | */ | 118 | */ |
@@ -111,7 +128,7 @@ pick(struct in_addr *ip) | |||
111 | ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp); | 128 | ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp); |
112 | } | 129 | } |
113 | 130 | ||
114 | /* | 131 | /** |
115 | * Broadcast an ARP packet. | 132 | * Broadcast an ARP packet. |
116 | */ | 133 | */ |
117 | static int | 134 | static int |
@@ -121,12 +138,12 @@ arp(int fd, struct sockaddr *saddr, int op, | |||
121 | { | 138 | { |
122 | struct arp_packet p; | 139 | struct arp_packet p; |
123 | 140 | ||
124 | /* ether header */ | 141 | // ether header |
125 | p.hdr.ether_type = htons(ETHERTYPE_ARP); | 142 | p.hdr.ether_type = htons(ETHERTYPE_ARP); |
126 | memcpy(p.hdr.ether_shost, source_addr, ETH_ALEN); | 143 | memcpy(p.hdr.ether_shost, source_addr, ETH_ALEN); |
127 | memset(p.hdr.ether_dhost, 0xff, ETH_ALEN); | 144 | memset(p.hdr.ether_dhost, 0xff, ETH_ALEN); |
128 | 145 | ||
129 | /* arp request */ | 146 | // arp request |
130 | p.arp.ar_hrd = htons(ARPHRD_ETHER); | 147 | p.arp.ar_hrd = htons(ARPHRD_ETHER); |
131 | p.arp.ar_pro = htons(ETHERTYPE_IP); | 148 | p.arp.ar_pro = htons(ETHERTYPE_IP); |
132 | p.arp.ar_hln = ETH_ALEN; | 149 | p.arp.ar_hln = ETH_ALEN; |
@@ -137,15 +154,15 @@ arp(int fd, struct sockaddr *saddr, int op, | |||
137 | memcpy(&p.target_addr, target_addr, ETH_ALEN); | 154 | memcpy(&p.target_addr, target_addr, ETH_ALEN); |
138 | memcpy(&p.target_ip, &target_ip, sizeof (p.target_ip)); | 155 | memcpy(&p.target_ip, &target_ip, sizeof (p.target_ip)); |
139 | 156 | ||
140 | /* send it */ | 157 | // send it |
141 | if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) { | 158 | if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) { |
142 | bb_perror_msg("sendto"); | 159 | perror("sendto"); |
143 | return -errno; | 160 | return -errno; |
144 | } | 161 | } |
145 | return 0; | 162 | return 0; |
146 | } | 163 | } |
147 | 164 | ||
148 | /* | 165 | /** |
149 | * Run a script. | 166 | * Run a script. |
150 | */ | 167 | */ |
151 | static int | 168 | static int |
@@ -155,19 +172,21 @@ run(char *script, char *arg, char *intf, struct in_addr *ip) | |||
155 | char *why; | 172 | char *why; |
156 | 173 | ||
157 | if (script != NULL) { | 174 | if (script != NULL) { |
175 | VDBG("%s run %s %s\n", intf, script, arg); | ||
158 | if (ip != NULL) { | 176 | if (ip != NULL) { |
159 | char *addr = inet_ntoa(*ip); | 177 | char *addr = inet_ntoa(*ip); |
160 | xsetenv("ip", addr); | 178 | setenv("ip", addr, 1); |
161 | syslog(LOG_INFO, "%s %s %s", arg, intf, addr); | 179 | syslog(LOG_INFO, "%s %s %s", arg, intf, addr); |
162 | } | 180 | } |
163 | 181 | ||
164 | pid = vfork(); | 182 | pid = vfork(); |
165 | if (pid < 0) { /* error */ | 183 | if (pid < 0) { // error |
166 | why = "vfork"; | 184 | why = "vfork"; |
167 | goto bad; | 185 | goto bad; |
168 | } else if (pid == 0) { /* child */ | 186 | } else if (pid == 0) { // child |
169 | execl(script, script, arg, NULL); | 187 | execl(script, script, arg, NULL); |
170 | bb_perror_msg_and_die("execl"); | 188 | perror("execl"); |
189 | _exit(EXIT_FAILURE); | ||
171 | } | 190 | } |
172 | 191 | ||
173 | if (waitpid(pid, &status, 0) <= 0) { | 192 | if (waitpid(pid, &status, 0) <= 0) { |
@@ -175,7 +194,8 @@ run(char *script, char *arg, char *intf, struct in_addr *ip) | |||
175 | goto bad; | 194 | goto bad; |
176 | } | 195 | } |
177 | if (WEXITSTATUS(status) != 0) { | 196 | if (WEXITSTATUS(status) != 0) { |
178 | bb_perror_msg("script %s failed, exit=%d", script, WEXITSTATUS(status)); | 197 | fprintf(stderr, "%s: script %s failed, exit=%d\n", |
198 | prog, script, WEXITSTATUS(status)); | ||
179 | return -errno; | 199 | return -errno; |
180 | } | 200 | } |
181 | } | 201 | } |
@@ -187,7 +207,31 @@ bad: | |||
187 | return status; | 207 | return status; |
188 | } | 208 | } |
189 | 209 | ||
190 | /* | 210 | #ifndef NO_BUSYBOX |
211 | #include "busybox.h" | ||
212 | #endif | ||
213 | |||
214 | /** | ||
215 | * Print usage information. | ||
216 | */ | ||
217 | static void __attribute__ ((noreturn)) | ||
218 | usage(const char *msg) | ||
219 | { | ||
220 | fprintf(stderr, "%s: %s\n", prog, msg); | ||
221 | #ifdef NO_BUSYBOX | ||
222 | fprintf(stderr, "Usage: %s [OPTIONS] ifname script\n" | ||
223 | "\t-f foreground mode (implied by -v)\n" | ||
224 | "\t-q quit after address (no daemon)\n" | ||
225 | "\t-r 169.254.x.x request this address first\n" | ||
226 | "\t-v verbose; show version\n", | ||
227 | prog); | ||
228 | exit(0); | ||
229 | #else | ||
230 | bb_show_usage(); | ||
231 | #endif | ||
232 | } | ||
233 | |||
234 | /** | ||
191 | * Return milliseconds of random delay, up to "secs" seconds. | 235 | * Return milliseconds of random delay, up to "secs" seconds. |
192 | */ | 236 | */ |
193 | static inline unsigned | 237 | static inline unsigned |
@@ -196,72 +240,85 @@ ms_rdelay(unsigned secs) | |||
196 | return lrand48() % (secs * 1000); | 240 | return lrand48() % (secs * 1000); |
197 | } | 241 | } |
198 | 242 | ||
199 | /* | 243 | /** |
200 | * main program | 244 | * main program |
201 | */ | 245 | */ |
202 | 246 | int | |
203 | #define FOREGROUND 1 | 247 | main(int argc, char *argv[]) |
204 | #define QUIT 2 | 248 | __attribute__ ((weak, alias ("zcip_main"))); |
205 | #define REQUEST 4 | ||
206 | #define VERBOSE 8 | ||
207 | 249 | ||
208 | int zcip_main(int argc, char *argv[]) | 250 | int zcip_main(int argc, char *argv[]) |
209 | { | 251 | { |
210 | char *intf = NULL; | 252 | char *intf = NULL; |
211 | char *script = NULL; | 253 | char *script = NULL; |
254 | int quit = 0; | ||
255 | int foreground = 0; | ||
256 | |||
212 | char *why; | 257 | char *why; |
213 | struct sockaddr saddr; | 258 | struct sockaddr saddr; |
214 | struct ether_addr addr; | 259 | struct ether_addr addr; |
215 | struct in_addr ip = { 0 }; | 260 | struct in_addr ip = { 0 }; |
216 | int fd; | 261 | int fd; |
217 | int ready = 0; | 262 | int ready = 0; |
218 | suseconds_t timeout = 0; /* milliseconds */ | 263 | suseconds_t timeout = 0; // milliseconds |
219 | time_t defend = 0; | 264 | time_t defend = 0; |
220 | unsigned conflicts = 0; | 265 | unsigned conflicts = 0; |
221 | unsigned nprobes = 0; | 266 | unsigned nprobes = 0; |
222 | unsigned nclaims = 0; | 267 | unsigned nclaims = 0; |
223 | unsigned long t; | 268 | int t; |
224 | 269 | ||
225 | bb_opt_complementaly = "vf"; | 270 | // parse commandline: prog [options] ifname script |
226 | /* parse commandline: prog [options] ifname script */ | 271 | prog = argv[0]; |
227 | t = bb_getopt_ulflags(argc, argv, "fqr:v", &why); /* reuse char* why */ | 272 | while ((t = getopt(argc, argv, "fqr:v")) != EOF) { |
228 | 273 | switch (t) { | |
229 | argc -= optind; | 274 | case 'f': |
230 | argv += optind; | 275 | foreground = 1; |
231 | 276 | continue; | |
232 | if ((t & 0x80000000UL) || (argc < 1) || (argc > 2)) { | 277 | case 'q': |
233 | bb_show_usage(); | 278 | quit = 1; |
234 | } | 279 | continue; |
235 | 280 | case 'r': | |
236 | if (t & VERBOSE) { | 281 | if (inet_aton(optarg, &ip) == 0 |
237 | bb_printf("%s: version %s\n", bb_applet_name, ZCIP_VERSION); | 282 | || (ntohl(ip.s_addr) & IN_CLASSB_NET) |
283 | != LINKLOCAL_ADDR) { | ||
284 | usage("invalid link address"); | ||
285 | } | ||
286 | continue; | ||
287 | case 'v': | ||
288 | if (!verbose) | ||
289 | printf("%s: version %s\n", prog, ZCIP_VERSION); | ||
290 | verbose++; | ||
291 | foreground = 1; | ||
292 | continue; | ||
293 | default: | ||
294 | usage("bad option"); | ||
295 | } | ||
238 | } | 296 | } |
239 | if ((t & REQUEST) && (inet_aton(why, &ip) == 0 || (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR)) { | 297 | if (optind < argc - 1) { |
240 | bb_perror_msg_and_die("invalid link address"); | 298 | intf = argv[optind++]; |
299 | setenv("interface", intf, 1); | ||
300 | script = argv[optind++]; | ||
241 | } | 301 | } |
242 | 302 | if (optind != argc || !intf) | |
243 | intf = argv[0]; | 303 | usage("wrong number of arguments"); |
244 | xsetenv("interface", intf); | 304 | openlog(prog, 0, LOG_DAEMON); |
245 | script = argv[1]; /* Could be NULL ? */ | 305 | |
246 | 306 | // initialize the interface (modprobe, ifup, etc) | |
247 | openlog(bb_applet_name, 0, LOG_DAEMON); | ||
248 | |||
249 | /* initialize the interface (modprobe, ifup, etc) */ | ||
250 | if (run(script, "init", intf, NULL) < 0) | 307 | if (run(script, "init", intf, NULL) < 0) |
251 | return EXIT_FAILURE; | 308 | return EXIT_FAILURE; |
252 | 309 | ||
253 | /* initialize saddr */ | 310 | // initialize saddr |
254 | memset(&saddr, 0, sizeof (saddr)); | 311 | memset(&saddr, 0, sizeof (saddr)); |
255 | strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data)); | 312 | strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data)); |
256 | 313 | ||
257 | /* open an ARP socket */ | 314 | // open an ARP socket |
258 | if ((fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) < 0) { | 315 | if ((fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) < 0) { |
259 | why = "open"; | 316 | why = "open"; |
260 | fail: | 317 | fail: |
261 | t |= FOREGROUND; | 318 | foreground = 1; |
262 | goto bad; | 319 | goto bad; |
263 | } | 320 | } |
264 | /* bind to the interface's ARP socket */ | 321 | // bind to the interface's ARP socket |
265 | if (bind(fd, &saddr, sizeof (saddr)) < 0) { | 322 | if (bind(fd, &saddr, sizeof (saddr)) < 0) { |
266 | why = "bind"; | 323 | why = "bind"; |
267 | goto fail; | 324 | goto fail; |
@@ -269,7 +326,7 @@ fail: | |||
269 | struct ifreq ifr; | 326 | struct ifreq ifr; |
270 | short seed[3]; | 327 | short seed[3]; |
271 | 328 | ||
272 | /* get the interface's ethernet address */ | 329 | // get the interface's ethernet address |
273 | memset(&ifr, 0, sizeof (ifr)); | 330 | memset(&ifr, 0, sizeof (ifr)); |
274 | strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name)); | 331 | strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name)); |
275 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { | 332 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { |
@@ -278,37 +335,37 @@ fail: | |||
278 | } | 335 | } |
279 | memcpy(&addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); | 336 | memcpy(&addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
280 | 337 | ||
281 | /* start with some stable ip address, either a function of | 338 | // start with some stable ip address, either a function of |
282 | the hardware address or else the last address we used. | 339 | // the hardware address or else the last address we used. |
283 | NOTE: the sequence of addresses we try changes only | 340 | // NOTE: the sequence of addresses we try changes only |
284 | depending on when we detect conflicts. */ | 341 | // depending on when we detect conflicts. |
285 | memcpy(seed, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); | 342 | memcpy(seed, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
286 | seed48(seed); | 343 | seed48(seed); |
287 | if (ip.s_addr == 0) | 344 | if (ip.s_addr == 0) |
288 | pick(&ip); | 345 | pick(&ip); |
289 | } | 346 | } |
290 | 347 | ||
291 | /* FIXME cases to handle: | 348 | // FIXME cases to handle: |
292 | - zcip already running! | 349 | // - zcip already running! |
293 | - link already has local address... just defend/update */ | 350 | // - link already has local address... just defend/update |
294 | 351 | ||
295 | /* daemonize now; don't delay system startup */ | 352 | // daemonize now; don't delay system startup |
296 | if (!(t & FOREGROUND)) { | 353 | if (!foreground) { |
297 | if (daemon(0, (t & VERBOSE)) < 0) { | 354 | if (daemon(0, verbose) < 0) { |
298 | why = "daemon"; | 355 | why = "daemon"; |
299 | goto bad; | 356 | goto bad; |
300 | } | 357 | } |
301 | syslog(LOG_INFO, "start, interface %s", intf); | 358 | syslog(LOG_INFO, "start, interface %s", intf); |
302 | } | 359 | } |
303 | 360 | ||
304 | /* run the dynamic address negotiation protocol, | 361 | // run the dynamic address negotiation protocol, |
305 | restarting after address conflicts: | 362 | // restarting after address conflicts: |
306 | - start with some address we want to try | 363 | // - start with some address we want to try |
307 | - short random delay | 364 | // - short random delay |
308 | - arp probes to see if another host else uses it | 365 | // - arp probes to see if another host else uses it |
309 | - arp announcements that we're claiming it | 366 | // - arp announcements that we're claiming it |
310 | - use it | 367 | // - use it |
311 | - defend it, within limits */ | 368 | // - defend it, within limits |
312 | while (1) { | 369 | while (1) { |
313 | struct pollfd fds[1]; | 370 | struct pollfd fds[1]; |
314 | struct timeval tv1; | 371 | struct timeval tv1; |
@@ -318,7 +375,7 @@ fail: | |||
318 | fds[0].events = POLLIN; | 375 | fds[0].events = POLLIN; |
319 | fds[0].revents = 0; | 376 | fds[0].revents = 0; |
320 | 377 | ||
321 | /* poll, being ready to adjust current timeout */ | 378 | // poll, being ready to adjust current timeout |
322 | if (timeout > 0) { | 379 | if (timeout > 0) { |
323 | gettimeofday(&tv1, NULL); | 380 | gettimeofday(&tv1, NULL); |
324 | tv1.tv_usec += (timeout % 1000) * 1000; | 381 | tv1.tv_usec += (timeout % 1000) * 1000; |
@@ -329,17 +386,21 @@ fail: | |||
329 | tv1.tv_sec += timeout / 1000; | 386 | tv1.tv_sec += timeout / 1000; |
330 | } else if (timeout == 0) { | 387 | } else if (timeout == 0) { |
331 | timeout = ms_rdelay(PROBE_WAIT); | 388 | timeout = ms_rdelay(PROBE_WAIT); |
332 | /* FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to | 389 | // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to |
333 | make the kernel filter out all packets except | 390 | // make the kernel filter out all packets except |
334 | ones we'd care about. */ | 391 | // ones we'd care about. |
335 | } | 392 | } |
393 | VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n", | ||
394 | timeout, intf, nprobes, nclaims); | ||
336 | switch (poll(fds, 1, timeout)) { | 395 | switch (poll(fds, 1, timeout)) { |
337 | 396 | ||
338 | /* timeouts trigger protocol transitions */ | 397 | // timeouts trigger protocol transitions |
339 | case 0: | 398 | case 0: |
340 | /* probes */ | 399 | // probes |
341 | if (nprobes < PROBE_NUM) { | 400 | if (nprobes < PROBE_NUM) { |
342 | nprobes++; | 401 | nprobes++; |
402 | VDBG("probe/%d %s@%s\n", | ||
403 | nprobes, intf, inet_ntoa(ip)); | ||
343 | (void)arp(fd, &saddr, ARPOP_REQUEST, | 404 | (void)arp(fd, &saddr, ARPOP_REQUEST, |
344 | &addr, null_ip, | 405 | &addr, null_ip, |
345 | &null_addr, ip); | 406 | &null_addr, ip); |
@@ -350,36 +411,38 @@ fail: | |||
350 | } else | 411 | } else |
351 | timeout = ANNOUNCE_WAIT * 1000; | 412 | timeout = ANNOUNCE_WAIT * 1000; |
352 | } | 413 | } |
353 | /* then announcements */ | 414 | // then announcements |
354 | else if (nclaims < ANNOUNCE_NUM) { | 415 | else if (nclaims < ANNOUNCE_NUM) { |
355 | nclaims++; | 416 | nclaims++; |
417 | VDBG("announce/%d %s@%s\n", | ||
418 | nclaims, intf, inet_ntoa(ip)); | ||
356 | (void)arp(fd, &saddr, ARPOP_REQUEST, | 419 | (void)arp(fd, &saddr, ARPOP_REQUEST, |
357 | &addr, ip, | 420 | &addr, ip, |
358 | &addr, ip); | 421 | &addr, ip); |
359 | if (nclaims < ANNOUNCE_NUM) { | 422 | if (nclaims < ANNOUNCE_NUM) { |
360 | timeout = ANNOUNCE_INTERVAL * 1000; | 423 | timeout = ANNOUNCE_INTERVAL * 1000; |
361 | } else { | 424 | } else { |
362 | /* link is ok to use earlier */ | 425 | // link is ok to use earlier |
363 | run(script, "config", intf, &ip); | 426 | run(script, "config", intf, &ip); |
364 | ready = 1; | 427 | ready = 1; |
365 | conflicts = 0; | 428 | conflicts = 0; |
366 | timeout = -1; | 429 | timeout = -1; |
367 | 430 | ||
368 | /* NOTE: all other exit paths | 431 | // NOTE: all other exit paths |
369 | should deconfig ... */ | 432 | // should deconfig ... |
370 | if (t & QUIT) | 433 | if (quit) |
371 | return EXIT_SUCCESS; | 434 | return EXIT_SUCCESS; |
372 | /* FIXME update filters */ | 435 | // FIXME update filters |
373 | } | 436 | } |
374 | } | 437 | } |
375 | break; | 438 | break; |
376 | 439 | ||
377 | /* packets arriving */ | 440 | // packets arriving |
378 | case 1: | 441 | case 1: |
379 | /* maybe adjust timeout */ | 442 | // maybe adjust timeout |
380 | if (timeout > 0) { | 443 | if (timeout > 0) { |
381 | struct timeval tv2; | 444 | struct timeval tv2; |
382 | 445 | ||
383 | gettimeofday(&tv2, NULL); | 446 | gettimeofday(&tv2, NULL); |
384 | if (timercmp(&tv1, &tv2, <)) { | 447 | if (timercmp(&tv1, &tv2, <)) { |
385 | timeout = -1; | 448 | timeout = -1; |
@@ -391,33 +454,46 @@ fail: | |||
391 | } | 454 | } |
392 | if ((fds[0].revents & POLLIN) == 0) { | 455 | if ((fds[0].revents & POLLIN) == 0) { |
393 | if (fds[0].revents & POLLERR) { | 456 | if (fds[0].revents & POLLERR) { |
394 | /* FIXME: links routinely go down; | 457 | // FIXME: links routinely go down; |
395 | this shouldn't necessarily exit. */ | 458 | // this shouldn't necessarily exit. |
396 | bb_perror_msg("%s: poll error", intf); | 459 | fprintf(stderr, "%s %s: poll error\n", |
460 | prog, intf); | ||
397 | if (ready) { | 461 | if (ready) { |
398 | run(script, "deconfig", intf, &ip); | 462 | run(script, "deconfig", |
463 | intf, &ip); | ||
399 | } | 464 | } |
400 | return EXIT_FAILURE; | 465 | return EXIT_FAILURE; |
401 | } | 466 | } |
402 | continue; | 467 | continue; |
403 | } | 468 | } |
404 | /* read ARP packet */ | 469 | // read ARP packet |
405 | if (recv(fd, &p, sizeof (p), 0) < 0) { | 470 | if (recv(fd, &p, sizeof (p), 0) < 0) { |
406 | why = "recv"; | 471 | why = "recv"; |
407 | goto bad; | 472 | goto bad; |
408 | } | 473 | } |
409 | if (p.hdr.ether_type != htons(ETHERTYPE_ARP)) | 474 | if (p.hdr.ether_type != htons(ETHERTYPE_ARP)) |
410 | continue; | 475 | continue; |
411 | 476 | ||
477 | VDBG("%s recv arp type=%d, op=%d,\n", | ||
478 | intf, ntohs(p.hdr.ether_type), | ||
479 | ntohs(p.arp.ar_op)); | ||
480 | VDBG("\tsource=%s %s\n", | ||
481 | ether_ntoa(&p.source_addr), | ||
482 | inet_ntoa(p.source_ip)); | ||
483 | VDBG("\ttarget=%s %s\n", | ||
484 | ether_ntoa(&p.target_addr), | ||
485 | inet_ntoa(p.target_ip)); | ||
412 | if (p.arp.ar_op != htons(ARPOP_REQUEST) | 486 | if (p.arp.ar_op != htons(ARPOP_REQUEST) |
413 | && p.arp.ar_op != htons(ARPOP_REPLY)) | 487 | && p.arp.ar_op != htons(ARPOP_REPLY)) |
414 | continue; | 488 | continue; |
415 | 489 | ||
416 | /* some cases are always conflicts */ | 490 | // some cases are always conflicts |
417 | if ((p.source_ip.s_addr == ip.s_addr) | 491 | if ((p.source_ip.s_addr == ip.s_addr) |
418 | && (memcmp(&addr, &p.source_addr, | 492 | && (memcmp(&addr, &p.source_addr, |
419 | ETH_ALEN) != 0)) { | 493 | ETH_ALEN) != 0)) { |
420 | collision: | 494 | collision: |
495 | VDBG("%s ARP conflict from %s\n", intf, | ||
496 | ether_ntoa(&p.source_addr)); | ||
421 | if (ready) { | 497 | if (ready) { |
422 | time_t now = time(0); | 498 | time_t now = time(0); |
423 | 499 | ||
@@ -428,26 +504,28 @@ collision: | |||
428 | ARPOP_REQUEST, | 504 | ARPOP_REQUEST, |
429 | &addr, ip, | 505 | &addr, ip, |
430 | &addr, ip); | 506 | &addr, ip); |
507 | VDBG("%s defend\n", intf); | ||
431 | timeout = -1; | 508 | timeout = -1; |
432 | continue; | 509 | continue; |
433 | } | 510 | } |
434 | defend = now; | 511 | defend = now; |
435 | ready = 0; | 512 | ready = 0; |
436 | run(script, "deconfig", intf, &ip); | 513 | run(script, "deconfig", intf, &ip); |
437 | /* FIXME rm filters: setsockopt(fd, | 514 | // FIXME rm filters: setsockopt(fd, |
438 | SO_DETACH_FILTER, ...) */ | 515 | // SO_DETACH_FILTER, ...) |
439 | } | 516 | } |
440 | conflicts++; | 517 | conflicts++; |
441 | if (conflicts >= MAX_CONFLICTS) { | 518 | if (conflicts >= MAX_CONFLICTS) { |
519 | VDBG("%s ratelimit\n", intf); | ||
442 | sleep(RATE_LIMIT_INTERVAL); | 520 | sleep(RATE_LIMIT_INTERVAL); |
443 | } | 521 | } |
444 | /* restart the whole protocol */ | 522 | // restart the whole protocol |
445 | pick(&ip); | 523 | pick(&ip); |
446 | timeout = 0; | 524 | timeout = 0; |
447 | nprobes = 0; | 525 | nprobes = 0; |
448 | nclaims = 0; | 526 | nclaims = 0; |
449 | } | 527 | } |
450 | /* two hosts probing one address is a collision too */ | 528 | // two hosts probing one address is a collision too |
451 | else if (p.target_ip.s_addr == ip.s_addr | 529 | else if (p.target_ip.s_addr == ip.s_addr |
452 | && nclaims == 0 | 530 | && nclaims == 0 |
453 | && p.arp.ar_op == htons(ARPOP_REQUEST) | 531 | && p.arp.ar_op == htons(ARPOP_REQUEST) |
@@ -463,11 +541,10 @@ collision: | |||
463 | } | 541 | } |
464 | } | 542 | } |
465 | bad: | 543 | bad: |
466 | if ( t & FOREGROUND) { | 544 | if (foreground) |
467 | bb_perror_msg(why); | 545 | perror(why); |
468 | } else { | 546 | else |
469 | syslog(LOG_ERR, "%s %s, %s error: %s", | 547 | syslog(LOG_ERR, "%s %s, %s error: %s", |
470 | bb_applet_name, intf, why, strerror(errno)); | 548 | prog, intf, why, strerror(errno)); |
471 | } | ||
472 | return EXIT_FAILURE; | 549 | return EXIT_FAILURE; |
473 | } | 550 | } |