"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * RFC3927 ZeroConf IPv4 Link-Local addressing |
| 4 | * (see <http://www.zeroconf.org/>) |
| 5 | * |
| 6 | * Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com) |
| 7 | * Copyright (C) 2004 by David Brownell |
| 8 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 9 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | /* |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 13 | * ZCIP just manages the 169.254.*.* addresses. That network is not |
| 14 | * routed at the IP level, though various proxies or bridges can |
| 15 | * certainly be used. Its naming is built over multicast DNS. |
| 16 | */ |
| 17 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 18 | //#define DEBUG |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 19 | |
| 20 | // TODO: |
| 21 | // - more real-world usage/testing, especially daemon mode |
| 22 | // - kernel packet filters to reduce scheduling noise |
| 23 | // - avoid silent script failures, especially under load... |
| 24 | // - link status monitoring (restart on link-up; stop on link-down) |
| 25 | |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 26 | #include "busybox.h" |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 27 | #include <errno.h> |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <syslog.h> |
| 30 | #include <poll.h> |
| 31 | #include <time.h> |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 32 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 33 | #include <sys/wait.h> |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 34 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 35 | #include <netinet/ether.h> |
| 36 | #include <net/ethernet.h> |
| 37 | #include <net/if.h> |
| 38 | #include <net/if_arp.h> |
| 39 | |
| 40 | #include <linux/if_packet.h> |
| 41 | #include <linux/sockios.h> |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 42 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 43 | |
Bernhard Reutner-Fischer | ec351c3 | 2005-12-13 10:28:25 +0000 | [diff] [blame] | 44 | struct arp_packet { |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 45 | struct ether_header hdr; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 46 | struct ether_arp arp; |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 47 | } ATTRIBUTE_PACKED; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 48 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 49 | enum { |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 50 | /* 169.254.0.0 */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 51 | LINKLOCAL_ADDR = 0xa9fe0000, |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 52 | |
| 53 | /* protocol timeout parameters, specified in seconds */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 54 | PROBE_WAIT = 1, |
| 55 | PROBE_MIN = 1, |
| 56 | PROBE_MAX = 2, |
| 57 | PROBE_NUM = 3, |
| 58 | MAX_CONFLICTS = 10, |
| 59 | RATE_LIMIT_INTERVAL = 60, |
| 60 | ANNOUNCE_WAIT = 2, |
| 61 | ANNOUNCE_NUM = 2, |
| 62 | ANNOUNCE_INTERVAL = 2, |
| 63 | DEFEND_INTERVAL = 10 |
| 64 | }; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 65 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 66 | /* States during the configuration process. */ |
| 67 | enum { |
| 68 | PROBE = 0, |
| 69 | RATE_LIMIT_PROBE, |
| 70 | ANNOUNCE, |
| 71 | MONITOR, |
| 72 | DEFEND |
| 73 | }; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 75 | #define VDBG(fmt,args...) \ |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 76 | do { } while (0) |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | a9abecd | 2006-09-03 12:28:32 +0000 | [diff] [blame] | 78 | static unsigned long opts; |
| 79 | #define FOREGROUND (opts & 1) |
| 80 | #define QUIT (opts & 2) |
| 81 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 82 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 83 | * Pick a random link local IP address on 169.254/16, except that |
| 84 | * the first and last 256 addresses are reserved. |
| 85 | */ |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 86 | static void pick(struct in_addr *ip) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 87 | { |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 88 | unsigned tmp; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 89 | |
| 90 | /* use cheaper math than lrand48() mod N */ |
| 91 | do { |
| 92 | tmp = (lrand48() >> 16) & IN_CLASSB_HOST; |
| 93 | } while (tmp > (IN_CLASSB_HOST - 0x0200)); |
| 94 | ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp); |
| 95 | } |
| 96 | |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 97 | /* TODO: we need a flag to direct bb_[p]error_msg output to stderr. */ |
| 98 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 99 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 100 | * Broadcast an ARP packet. |
| 101 | */ |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 102 | static void arp(int fd, struct sockaddr *saddr, int op, |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 103 | const struct ether_addr *source_addr, struct in_addr source_ip, |
| 104 | const struct ether_addr *target_addr, struct in_addr target_ip) |
| 105 | { |
| 106 | struct arp_packet p; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 107 | memset(&p, 0, sizeof(p)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 108 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 109 | // ether header |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 110 | p.hdr.ether_type = htons(ETHERTYPE_ARP); |
| 111 | memcpy(p.hdr.ether_shost, source_addr, ETH_ALEN); |
| 112 | memset(p.hdr.ether_dhost, 0xff, ETH_ALEN); |
| 113 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 114 | // arp request |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 115 | p.arp.arp_hrd = htons(ARPHRD_ETHER); |
| 116 | p.arp.arp_pro = htons(ETHERTYPE_IP); |
| 117 | p.arp.arp_hln = ETH_ALEN; |
| 118 | p.arp.arp_pln = 4; |
| 119 | p.arp.arp_op = htons(op); |
| 120 | memcpy(&p.arp.arp_sha, source_addr, ETH_ALEN); |
| 121 | memcpy(&p.arp.arp_spa, &source_ip, sizeof (p.arp.arp_spa)); |
| 122 | memcpy(&p.arp.arp_tha, target_addr, ETH_ALEN); |
| 123 | memcpy(&p.arp.arp_tpa, &target_ip, sizeof (p.arp.arp_tpa)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 124 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 125 | // send it |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 126 | if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) { |
Denis Vlasenko | a9abecd | 2006-09-03 12:28:32 +0000 | [diff] [blame] | 127 | if (FOREGROUND) |
| 128 | perror("sendto"); |
| 129 | else |
| 130 | syslog(LOG_ERR, "sendto: %s", strerror(errno)); |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 131 | //return -errno; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 132 | } |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 133 | // Currently all callers ignore errors, that's why returns are |
| 134 | // commented out... |
| 135 | //return 0; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 138 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 139 | * Run a script. |
| 140 | */ |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 141 | static int run(char *script, char *arg, char *intf, struct in_addr *ip) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 142 | { |
| 143 | int pid, status; |
| 144 | char *why; |
| 145 | |
Denis Vlasenko | a9abecd | 2006-09-03 12:28:32 +0000 | [diff] [blame] | 146 | if(1) { //always true: if (script != NULL) |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 147 | VDBG("%s run %s %s\n", intf, script, arg); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 148 | if (ip != NULL) { |
| 149 | char *addr = inet_ntoa(*ip); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 150 | setenv("ip", addr, 1); |
Denis Vlasenko | a9abecd | 2006-09-03 12:28:32 +0000 | [diff] [blame] | 151 | if (!FOREGROUND) |
| 152 | syslog(LOG_INFO, "%s %s %s", arg, intf, addr); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | pid = vfork(); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 156 | if (pid < 0) { // error |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 157 | why = "vfork"; |
| 158 | goto bad; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 159 | } else if (pid == 0) { // child |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 160 | execl(script, script, arg, NULL); |
Denis Vlasenko | a9abecd | 2006-09-03 12:28:32 +0000 | [diff] [blame] | 161 | if (FOREGROUND) |
| 162 | perror("execl"); |
| 163 | else |
| 164 | syslog(LOG_ERR, "execl: %s", strerror(errno)); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 165 | _exit(EXIT_FAILURE); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 166 | } |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 167 | |
| 168 | if (waitpid(pid, &status, 0) <= 0) { |
| 169 | why = "waitpid"; |
| 170 | goto bad; |
| 171 | } |
| 172 | if (WEXITSTATUS(status) != 0) { |
Denis Vlasenko | a9abecd | 2006-09-03 12:28:32 +0000 | [diff] [blame] | 173 | if (FOREGROUND) |
Denis Vlasenko | 65113e8 | 2006-09-03 12:31:59 +0000 | [diff] [blame^] | 174 | bb_error_msg("script %s failed, exit=%d", |
Denis Vlasenko | a9abecd | 2006-09-03 12:28:32 +0000 | [diff] [blame] | 175 | script, WEXITSTATUS(status)); |
| 176 | else |
| 177 | syslog(LOG_ERR, "script %s failed, exit=%d", |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 178 | script, WEXITSTATUS(status)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 179 | return -errno; |
| 180 | } |
| 181 | } |
| 182 | return 0; |
| 183 | bad: |
| 184 | status = -errno; |
Denis Vlasenko | a9abecd | 2006-09-03 12:28:32 +0000 | [diff] [blame] | 185 | if (FOREGROUND) |
| 186 | bb_perror_msg("%s %s, %s", |
| 187 | arg, intf, why); |
| 188 | else |
| 189 | syslog(LOG_ERR, "%s %s, %s: %s", |
| 190 | arg, intf, why, strerror(errno)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 191 | return status; |
| 192 | } |
| 193 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 194 | |
| 195 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 196 | * Return milliseconds of random delay, up to "secs" seconds. |
| 197 | */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 198 | static unsigned ATTRIBUTE_ALWAYS_INLINE ms_rdelay(unsigned secs) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 199 | { |
| 200 | return lrand48() % (secs * 1000); |
| 201 | } |
| 202 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 203 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 204 | * main program |
| 205 | */ |
Bernhard Reutner-Fischer | 38d6615 | 2005-10-21 10:43:11 +0000 | [diff] [blame] | 206 | |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 207 | /* Used to be auto variables on main() stack, but |
| 208 | * most of them were zero-inited. Moving them to bss |
| 209 | * is more space-efficient. |
| 210 | */ |
| 211 | static const struct in_addr null_ip; // = { 0 }; |
| 212 | static const struct ether_addr null_addr; // = { {0, 0, 0, 0, 0, 0} }; |
| 213 | |
| 214 | static struct sockaddr saddr; // memset(0); |
| 215 | static struct in_addr ip; // = { 0 }; |
| 216 | static struct ifreq ifr; //memset(0); |
| 217 | |
| 218 | static char *intf; // = NULL; |
| 219 | static char *script; // = NULL; |
| 220 | static suseconds_t timeout; // = 0; // milliseconds |
| 221 | static unsigned conflicts; // = 0; |
| 222 | static unsigned nprobes; // = 0; |
| 223 | static unsigned nclaims; // = 0; |
| 224 | static int ready; // = 0; |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 225 | static int verbose; // = 0; |
| 226 | static int state = PROBE; |
| 227 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 228 | int zcip_main(int argc, char *argv[]) |
| 229 | { |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 230 | struct ether_addr eth_addr; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 231 | char *why; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 232 | int fd; |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame] | 233 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 234 | // parse commandline: prog [options] ifname script |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 235 | char *r_opt; |
Denis Vlasenko | 65113e8 | 2006-09-03 12:31:59 +0000 | [diff] [blame^] | 236 | bb_opt_complementally = "vv:vf"; // -v accumulates and implies -f |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 237 | opts = bb_getopt_ulflags(argc, argv, "fqr:v", &r_opt, &verbose); |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 238 | if (opts & 4) { // -r n.n.n.n |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 239 | if (inet_aton(r_opt, &ip) == 0 |
| 240 | || (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR) { |
| 241 | bb_error_msg_and_die("invalid link address"); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 242 | } |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 243 | } |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 244 | argc -= optind; |
| 245 | argv += optind; |
| 246 | if (argc != 2) |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 247 | bb_show_usage(); |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 248 | intf = argv[0]; |
| 249 | script = argv[1]; |
| 250 | setenv("interface", intf, 1); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 251 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 252 | // initialize the interface (modprobe, ifup, etc) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 253 | if (run(script, "init", intf, NULL) < 0) |
| 254 | return EXIT_FAILURE; |
| 255 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 256 | // initialize saddr |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 257 | //memset(&saddr, 0, sizeof (saddr)); |
Rob Landley | 768945b | 2006-06-25 00:34:52 +0000 | [diff] [blame] | 258 | safe_strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 259 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 260 | // open an ARP socket |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame] | 261 | fd = xsocket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)); |
| 262 | // bind to the interface's ARP socket |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 263 | xbind(fd, &saddr, sizeof (saddr)); |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame] | 264 | |
| 265 | // get the interface's ethernet address |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 266 | //memset(&ifr, 0, sizeof (ifr)); |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame] | 267 | strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name)); |
| 268 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 269 | bb_perror_msg_and_die("get ethernet address"); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 270 | } |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 271 | memcpy(ð_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 272 | |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame] | 273 | // start with some stable ip address, either a function of |
| 274 | // the hardware address or else the last address we used. |
| 275 | // NOTE: the sequence of addresses we try changes only |
| 276 | // depending on when we detect conflicts. |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 277 | // (SVID 3 bogon: who says that "short" is always 16 bits?) |
| 278 | seed48( (unsigned short*)&ifr.ifr_hwaddr.sa_data ); |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame] | 279 | if (ip.s_addr == 0) |
| 280 | pick(&ip); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 281 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 282 | // FIXME cases to handle: |
| 283 | // - zcip already running! |
| 284 | // - link already has local address... just defend/update |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 285 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 286 | // daemonize now; don't delay system startup |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 287 | if (!FOREGROUND) { |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 288 | xdaemon(0, verbose); |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 289 | openlog(bb_applet_name, 0, LOG_DAEMON); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 290 | syslog(LOG_INFO, "start, interface %s", intf); |
| 291 | } |
| 292 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 293 | // run the dynamic address negotiation protocol, |
| 294 | // restarting after address conflicts: |
| 295 | // - start with some address we want to try |
| 296 | // - short random delay |
| 297 | // - arp probes to see if another host else uses it |
| 298 | // - arp announcements that we're claiming it |
| 299 | // - use it |
| 300 | // - defend it, within limits |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 301 | while (1) { |
| 302 | struct pollfd fds[1]; |
| 303 | struct timeval tv1; |
| 304 | struct arp_packet p; |
| 305 | |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 306 | int source_ip_conflict = 0; |
| 307 | int target_ip_conflict = 0; |
| 308 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 309 | fds[0].fd = fd; |
| 310 | fds[0].events = POLLIN; |
| 311 | fds[0].revents = 0; |
| 312 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 313 | // poll, being ready to adjust current timeout |
Rob Landley | 00c051e | 2006-06-30 14:05:19 +0000 | [diff] [blame] | 314 | if (!timeout) { |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 315 | timeout = ms_rdelay(PROBE_WAIT); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 316 | // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to |
| 317 | // make the kernel filter out all packets except |
| 318 | // ones we'd care about. |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 319 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 320 | // set tv1 to the point in time when we timeout |
Rob Landley | 00c051e | 2006-06-30 14:05:19 +0000 | [diff] [blame] | 321 | gettimeofday(&tv1, NULL); |
| 322 | tv1.tv_usec += (timeout % 1000) * 1000; |
| 323 | while (tv1.tv_usec > 1000000) { |
| 324 | tv1.tv_usec -= 1000000; |
| 325 | tv1.tv_sec++; |
| 326 | } |
| 327 | tv1.tv_sec += timeout / 1000; |
| 328 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 329 | VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n", |
| 330 | timeout, intf, nprobes, nclaims); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 331 | switch (poll(fds, 1, timeout)) { |
| 332 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 333 | // timeout |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 334 | case 0: |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 335 | VDBG("state = %d\n", state); |
| 336 | switch (state) { |
| 337 | case PROBE: |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 338 | // timeouts in the PROBE state mean no conflicting ARP packets |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 339 | // have been received, so we can progress through the states |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 340 | if (nprobes < PROBE_NUM) { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 341 | nprobes++; |
| 342 | VDBG("probe/%d %s@%s\n", |
| 343 | nprobes, intf, inet_ntoa(ip)); |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 344 | arp(fd, &saddr, ARPOP_REQUEST, |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 345 | ð_addr, null_ip, |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 346 | &null_addr, ip); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 347 | timeout = PROBE_MIN * 1000; |
| 348 | timeout += ms_rdelay(PROBE_MAX |
| 349 | - PROBE_MIN); |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 350 | } |
| 351 | else { |
| 352 | // Switch to announce state. |
| 353 | state = ANNOUNCE; |
| 354 | nclaims = 0; |
| 355 | VDBG("announce/%d %s@%s\n", |
| 356 | nclaims, intf, inet_ntoa(ip)); |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 357 | arp(fd, &saddr, ARPOP_REQUEST, |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 358 | ð_addr, ip, |
| 359 | ð_addr, ip); |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 360 | timeout = ANNOUNCE_INTERVAL * 1000; |
| 361 | } |
| 362 | break; |
| 363 | case RATE_LIMIT_PROBE: |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 364 | // timeouts in the RATE_LIMIT_PROBE state mean no conflicting ARP packets |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 365 | // have been received, so we can move immediately to the announce state |
| 366 | state = ANNOUNCE; |
| 367 | nclaims = 0; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 368 | VDBG("announce/%d %s@%s\n", |
| 369 | nclaims, intf, inet_ntoa(ip)); |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 370 | arp(fd, &saddr, ARPOP_REQUEST, |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 371 | ð_addr, ip, |
| 372 | ð_addr, ip); |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 373 | timeout = ANNOUNCE_INTERVAL * 1000; |
| 374 | break; |
| 375 | case ANNOUNCE: |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 376 | // timeouts in the ANNOUNCE state mean no conflicting ARP packets |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 377 | // have been received, so we can progress through the states |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 378 | if (nclaims < ANNOUNCE_NUM) { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 379 | nclaims++; |
| 380 | VDBG("announce/%d %s@%s\n", |
| 381 | nclaims, intf, inet_ntoa(ip)); |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 382 | arp(fd, &saddr, ARPOP_REQUEST, |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 383 | ð_addr, ip, |
| 384 | ð_addr, ip); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 385 | timeout = ANNOUNCE_INTERVAL * 1000; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 386 | } |
| 387 | else { |
| 388 | // Switch to monitor state. |
| 389 | state = MONITOR; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 390 | // link is ok to use earlier |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 391 | // FIXME update filters |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 392 | run(script, "config", intf, &ip); |
| 393 | ready = 1; |
| 394 | conflicts = 0; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 395 | timeout = -1; // Never timeout in the monitor state. |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 396 | |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 397 | // NOTE: all other exit paths |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 398 | // should deconfig ... |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 399 | if (QUIT) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 400 | return EXIT_SUCCESS; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 401 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 402 | break; |
| 403 | case DEFEND: |
| 404 | // We won! No ARP replies, so just go back to monitor. |
| 405 | state = MONITOR; |
| 406 | timeout = -1; |
| 407 | conflicts = 0; |
| 408 | break; |
| 409 | default: |
| 410 | // Invalid, should never happen. Restart the whole protocol. |
| 411 | state = PROBE; |
| 412 | pick(&ip); |
| 413 | timeout = 0; |
| 414 | nprobes = 0; |
| 415 | nclaims = 0; |
| 416 | break; |
| 417 | } // switch (state) |
| 418 | break; // case 0 (timeout) |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 419 | // packets arriving |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 420 | case 1: |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 421 | // We need to adjust the timeout in case we didn't receive |
| 422 | // a conflicting packet. |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 423 | if (timeout > 0) { |
| 424 | struct timeval tv2; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 425 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 426 | gettimeofday(&tv2, NULL); |
| 427 | if (timercmp(&tv1, &tv2, <)) { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 428 | // Current time is greater than the expected timeout time. |
| 429 | // Should never happen. |
| 430 | VDBG("missed an expected timeout\n"); |
Paul Fox | ef81ce6 | 2006-03-29 23:01:33 +0000 | [diff] [blame] | 431 | timeout = 0; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 432 | } else { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 433 | VDBG("adjusting timeout\n"); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 434 | timersub(&tv1, &tv2, &tv1); |
| 435 | timeout = 1000 * tv1.tv_sec |
| 436 | + tv1.tv_usec / 1000; |
| 437 | } |
| 438 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 439 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 440 | if ((fds[0].revents & POLLIN) == 0) { |
| 441 | if (fds[0].revents & POLLERR) { |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 442 | // FIXME: links routinely go down; |
| 443 | // this shouldn't necessarily exit. |
Denis Vlasenko | 65113e8 | 2006-09-03 12:31:59 +0000 | [diff] [blame^] | 444 | bb_error_msg("%s: poll error", intf); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 445 | if (ready) { |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 446 | run(script, "deconfig", |
| 447 | intf, &ip); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 448 | } |
| 449 | return EXIT_FAILURE; |
| 450 | } |
| 451 | continue; |
| 452 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 453 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 454 | // read ARP packet |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 455 | if (recv(fd, &p, sizeof (p), 0) < 0) { |
| 456 | why = "recv"; |
| 457 | goto bad; |
| 458 | } |
| 459 | if (p.hdr.ether_type != htons(ETHERTYPE_ARP)) |
| 460 | continue; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 461 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 462 | #ifdef DEBUG |
| 463 | { |
| 464 | struct ether_addr * sha = (struct ether_addr *) p.arp.arp_sha; |
| 465 | struct ether_addr * tha = (struct ether_addr *) p.arp.arp_tha; |
| 466 | struct in_addr * spa = (struct in_addr *) p.arp.arp_spa; |
| 467 | struct in_addr * tpa = (struct in_addr *) p.arp.arp_tpa; |
| 468 | VDBG("%s recv arp type=%d, op=%d,\n", |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 469 | intf, ntohs(p.hdr.ether_type), |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 470 | ntohs(p.arp.arp_op)); |
| 471 | VDBG("\tsource=%s %s\n", |
| 472 | ether_ntoa(sha), |
| 473 | inet_ntoa(*spa)); |
| 474 | VDBG("\ttarget=%s %s\n", |
| 475 | ether_ntoa(tha), |
| 476 | inet_ntoa(*tpa)); |
| 477 | } |
| 478 | #endif |
| 479 | if (p.arp.arp_op != htons(ARPOP_REQUEST) |
| 480 | && p.arp.arp_op != htons(ARPOP_REPLY)) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 481 | continue; |
| 482 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 483 | if (memcmp(p.arp.arp_spa, &ip.s_addr, sizeof(struct in_addr)) == 0 && |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 484 | memcmp(ð_addr, &p.arp.arp_sha, ETH_ALEN) != 0) { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 485 | source_ip_conflict = 1; |
| 486 | } |
| 487 | if (memcmp(p.arp.arp_tpa, &ip.s_addr, sizeof(struct in_addr)) == 0 && |
| 488 | p.arp.arp_op == htons(ARPOP_REQUEST) && |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 489 | memcmp(ð_addr, &p.arp.arp_tha, ETH_ALEN) != 0) { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 490 | target_ip_conflict = 1; |
| 491 | } |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 492 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 493 | VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n", |
| 494 | state, source_ip_conflict, target_ip_conflict); |
| 495 | switch (state) { |
| 496 | case PROBE: |
| 497 | case ANNOUNCE: |
| 498 | // When probing or announcing, check for source IP conflicts |
| 499 | // and other hosts doing ARP probes (target IP conflicts). |
| 500 | if (source_ip_conflict || target_ip_conflict) { |
| 501 | conflicts++; |
| 502 | if (conflicts >= MAX_CONFLICTS) { |
| 503 | VDBG("%s ratelimit\n", intf); |
| 504 | timeout = RATE_LIMIT_INTERVAL * 1000; |
| 505 | state = RATE_LIMIT_PROBE; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 506 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 507 | |
| 508 | // restart the whole protocol |
| 509 | pick(&ip); |
| 510 | timeout = 0; |
| 511 | nprobes = 0; |
| 512 | nclaims = 0; |
| 513 | } |
| 514 | break; |
| 515 | case MONITOR: |
| 516 | // If a conflict, we try to defend with a single ARP probe. |
| 517 | if (source_ip_conflict) { |
| 518 | VDBG("monitor conflict -- defending\n"); |
| 519 | state = DEFEND; |
| 520 | timeout = DEFEND_INTERVAL * 1000; |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 521 | arp(fd, &saddr, |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 522 | ARPOP_REQUEST, |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 523 | ð_addr, ip, |
| 524 | ð_addr, ip); |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 525 | } |
| 526 | break; |
| 527 | case DEFEND: |
| 528 | // Well, we tried. Start over (on conflict). |
| 529 | if (source_ip_conflict) { |
| 530 | state = PROBE; |
| 531 | VDBG("defend conflict -- starting over\n"); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 532 | ready = 0; |
| 533 | run(script, "deconfig", intf, &ip); |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 534 | |
| 535 | // restart the whole protocol |
| 536 | pick(&ip); |
| 537 | timeout = 0; |
| 538 | nprobes = 0; |
| 539 | nclaims = 0; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 540 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 541 | break; |
| 542 | default: |
| 543 | // Invalid, should never happen. Restart the whole protocol. |
| 544 | VDBG("invalid state -- starting over\n"); |
| 545 | state = PROBE; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 546 | pick(&ip); |
| 547 | timeout = 0; |
| 548 | nprobes = 0; |
| 549 | nclaims = 0; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 550 | break; |
| 551 | } // switch state |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 552 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 553 | break; // case 1 (packets arriving) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 554 | default: |
| 555 | why = "poll"; |
| 556 | goto bad; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 557 | } // switch poll |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 558 | } |
| 559 | bad: |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 560 | if (FOREGROUND) |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 561 | perror(why); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 562 | else |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 563 | syslog(LOG_ERR, "%s %s, %s error: %s", |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 564 | bb_applet_name, intf, why, strerror(errno)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 565 | return EXIT_FAILURE; |
| 566 | } |