osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * arpd.c ARP helper daemon. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <syslog.h> |
| 14 | #include <malloc.h> |
| 15 | #include <string.h> |
| 16 | #include <unistd.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <netdb.h> |
osdl.org!shemminger | 16efac5 | 2004-06-09 21:28:46 +0000 | [diff] [blame] | 19 | #include <db_185.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 20 | #include <sys/ioctl.h> |
| 21 | #include <sys/poll.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/uio.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <time.h> |
| 28 | #include <signal.h> |
| 29 | #include <linux/if.h> |
osdl.net!shemminger | 17ce7fd | 2004-07-06 21:01:44 +0000 | [diff] [blame] | 30 | #include <linux/if_ether.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 31 | #include <linux/if_arp.h> |
| 32 | #include <netinet/in.h> |
| 33 | #include <arpa/inet.h> |
| 34 | #include <linux/if_packet.h> |
| 35 | #include <linux/filter.h> |
| 36 | |
| 37 | #include "libnetlink.h" |
| 38 | #include "utils.h" |
| 39 | |
| 40 | int resolve_hosts; |
| 41 | |
| 42 | DB *dbase; |
| 43 | char *dbname = "/var/lib/arpd/arpd.db"; |
| 44 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 45 | int ifnum; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 46 | int *ifvec; |
| 47 | char **ifnames; |
| 48 | |
| 49 | struct dbkey |
| 50 | { |
| 51 | __u32 iface; |
| 52 | __u32 addr; |
| 53 | }; |
| 54 | |
| 55 | #define IS_NEG(x) (((__u8*)(x))[0] == 0xFF) |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 56 | #define NEG_TIME(x) (((x)[2]<<24)|((x)[3]<<16)|((x)[4]<<8)|(x)[5]) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 57 | #define NEG_AGE(x) ((__u32)time(NULL) - NEG_TIME((__u8*)x)) |
| 58 | #define NEG_VALID(x) (NEG_AGE(x) < negative_timeout) |
| 59 | #define NEG_CNT(x) (((__u8*)(x))[1]) |
| 60 | |
| 61 | struct rtnl_handle rth; |
| 62 | |
| 63 | struct pollfd pset[2]; |
| 64 | int udp_sock = -1; |
| 65 | |
| 66 | volatile int do_exit; |
| 67 | volatile int do_sync; |
| 68 | volatile int do_stats; |
| 69 | |
| 70 | struct { |
| 71 | unsigned long arp_new; |
| 72 | unsigned long arp_change; |
| 73 | |
| 74 | unsigned long app_recv; |
| 75 | unsigned long app_success; |
| 76 | unsigned long app_bad; |
| 77 | unsigned long app_neg; |
| 78 | unsigned long app_suppressed; |
| 79 | |
| 80 | unsigned long kern_neg; |
| 81 | unsigned long kern_new; |
| 82 | unsigned long kern_change; |
| 83 | |
| 84 | unsigned long probes_sent; |
| 85 | unsigned long probes_suppressed; |
| 86 | } stats; |
| 87 | |
| 88 | int active_probing; |
| 89 | int negative_timeout = 60; |
| 90 | int no_kernel_broadcasts; |
| 91 | int broadcast_rate = 1000; |
| 92 | int broadcast_burst = 3000; |
Stephen Hemminger | 0df7db3 | 2012-02-17 08:17:09 -0800 | [diff] [blame] | 93 | int poll_timeout = 30000; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 94 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 95 | static void usage(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 96 | { |
| 97 | fprintf(stderr, |
Stephen Hemminger | 0df7db3 | 2012-02-17 08:17:09 -0800 | [diff] [blame] | 98 | "Usage: arpd [ -lkh? ] [ -a N ] [ -b dbase ] [ -B number ]" |
| 99 | " [ -f file ] [ -n time ] [-p interval ] [ -R rate ] [ interfaces ]\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 100 | exit(1); |
| 101 | } |
| 102 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 103 | static int handle_if(int ifindex) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 104 | { |
| 105 | int i; |
| 106 | |
| 107 | if (ifnum == 0) |
| 108 | return 1; |
| 109 | |
| 110 | for (i=0; i<ifnum; i++) |
| 111 | if (ifvec[i] == ifindex) |
| 112 | return 1; |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int sysctl_adjusted; |
| 117 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 118 | static void do_sysctl_adjustments(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 119 | { |
| 120 | int i; |
| 121 | |
| 122 | if (!ifnum) |
| 123 | return; |
| 124 | |
| 125 | for (i=0; i<ifnum; i++) { |
| 126 | char buf[128]; |
| 127 | FILE *fp; |
| 128 | |
| 129 | if (active_probing) { |
| 130 | sprintf(buf, "/proc/sys/net/ipv4/neigh/%s/mcast_solicit", ifnames[i]); |
| 131 | if ((fp = fopen(buf, "w")) != NULL) { |
| 132 | if (no_kernel_broadcasts) |
| 133 | strcpy(buf, "0\n"); |
| 134 | else |
| 135 | sprintf(buf, "%d\n", active_probing>=2 ? 1 : 3-active_probing); |
| 136 | fputs(buf, fp); |
| 137 | fclose(fp); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | sprintf(buf, "/proc/sys/net/ipv4/neigh/%s/app_solicit", ifnames[i]); |
| 142 | if ((fp = fopen(buf, "w")) != NULL) { |
| 143 | sprintf(buf, "%d\n", active_probing<=1 ? 1 : active_probing); |
| 144 | fputs(buf, fp); |
| 145 | fclose(fp); |
| 146 | } |
| 147 | } |
| 148 | sysctl_adjusted = 1; |
| 149 | } |
| 150 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 151 | static void undo_sysctl_adjustments(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 152 | { |
| 153 | int i; |
| 154 | |
| 155 | if (!sysctl_adjusted) |
| 156 | return; |
| 157 | |
| 158 | for (i=0; i<ifnum; i++) { |
| 159 | char buf[128]; |
| 160 | FILE *fp; |
| 161 | |
| 162 | if (active_probing) { |
| 163 | sprintf(buf, "/proc/sys/net/ipv4/neigh/%s/mcast_solicit", ifnames[i]); |
| 164 | if ((fp = fopen(buf, "w")) != NULL) { |
| 165 | strcpy(buf, "3\n"); |
| 166 | fputs(buf, fp); |
| 167 | fclose(fp); |
| 168 | } |
| 169 | } |
| 170 | sprintf(buf, "/proc/sys/net/ipv4/neigh/%s/app_solicit", ifnames[i]); |
| 171 | if ((fp = fopen(buf, "w")) != NULL) { |
| 172 | strcpy(buf, "0\n"); |
| 173 | fputs(buf, fp); |
| 174 | fclose(fp); |
| 175 | } |
| 176 | } |
| 177 | sysctl_adjusted = 0; |
| 178 | } |
| 179 | |
| 180 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 181 | static int send_probe(int ifindex, __u32 addr) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 182 | { |
| 183 | struct ifreq ifr; |
| 184 | struct sockaddr_in dst; |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 185 | socklen_t len; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 186 | unsigned char buf[256]; |
| 187 | struct arphdr *ah = (struct arphdr*)buf; |
| 188 | unsigned char *p = (unsigned char *)(ah+1); |
| 189 | struct sockaddr_ll sll; |
| 190 | |
| 191 | memset(&ifr, 0, sizeof(ifr)); |
| 192 | ifr.ifr_ifindex = ifindex; |
| 193 | if (ioctl(udp_sock, SIOCGIFNAME, &ifr)) |
| 194 | return -1; |
| 195 | if (ioctl(udp_sock, SIOCGIFHWADDR, &ifr)) |
| 196 | return -1; |
| 197 | if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) |
| 198 | return -1; |
| 199 | if (setsockopt(udp_sock, SOL_SOCKET, SO_BINDTODEVICE, ifr.ifr_name, strlen(ifr.ifr_name)+1) < 0) |
| 200 | return -1; |
| 201 | |
| 202 | dst.sin_family = AF_INET; |
| 203 | dst.sin_port = htons(1025); |
| 204 | dst.sin_addr.s_addr = addr; |
| 205 | if (connect(udp_sock, (struct sockaddr*)&dst, sizeof(dst)) < 0) |
| 206 | return -1; |
| 207 | len = sizeof(dst); |
| 208 | if (getsockname(udp_sock, (struct sockaddr*)&dst, &len) < 0) |
| 209 | return -1; |
| 210 | |
| 211 | ah->ar_hrd = htons(ifr.ifr_hwaddr.sa_family); |
| 212 | ah->ar_pro = htons(ETH_P_IP); |
| 213 | ah->ar_hln = 6; |
| 214 | ah->ar_pln = 4; |
| 215 | ah->ar_op = htons(ARPOP_REQUEST); |
| 216 | |
| 217 | memcpy(p, ifr.ifr_hwaddr.sa_data, ah->ar_hln); |
| 218 | p += ah->ar_hln; |
| 219 | |
| 220 | memcpy(p, &dst.sin_addr, 4); |
| 221 | p+=4; |
| 222 | |
| 223 | sll.sll_family = AF_PACKET; |
| 224 | memset(sll.sll_addr, 0xFF, sizeof(sll.sll_addr)); |
| 225 | sll.sll_ifindex = ifindex; |
| 226 | sll.sll_protocol = htons(ETH_P_ARP); |
| 227 | memcpy(p, &sll.sll_addr, ah->ar_hln); |
| 228 | p+=ah->ar_hln; |
| 229 | |
| 230 | memcpy(p, &addr, 4); |
| 231 | p+=4; |
| 232 | |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 233 | if (sendto(pset[0].fd, buf, p-buf, 0, (struct sockaddr*)&sll, sizeof(sll)) < 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 234 | return -1; |
| 235 | stats.probes_sent++; |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | /* Be very tough on sending probes: 1 per second with burst of 3. */ |
| 240 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 241 | static int queue_active_probe(int ifindex, __u32 addr) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 242 | { |
| 243 | static struct timeval prev; |
| 244 | static int buckets; |
| 245 | struct timeval now; |
| 246 | |
| 247 | gettimeofday(&now, NULL); |
| 248 | if (prev.tv_sec) { |
| 249 | int diff = (now.tv_sec-prev.tv_sec)*1000+(now.tv_usec-prev.tv_usec)/1000; |
| 250 | buckets += diff; |
| 251 | } else { |
| 252 | buckets = broadcast_burst; |
| 253 | } |
| 254 | if (buckets > broadcast_burst) |
| 255 | buckets = broadcast_burst; |
| 256 | if (buckets >= broadcast_rate && !send_probe(ifindex, addr)) { |
| 257 | buckets -= broadcast_rate; |
| 258 | prev = now; |
| 259 | return 0; |
| 260 | } |
| 261 | stats.probes_suppressed++; |
| 262 | return -1; |
| 263 | } |
| 264 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 265 | static int respond_to_kernel(int ifindex, __u32 addr, char *lla, int llalen) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 266 | { |
| 267 | struct { |
| 268 | struct nlmsghdr n; |
| 269 | struct ndmsg ndm; |
| 270 | char buf[256]; |
| 271 | } req; |
| 272 | |
| 273 | memset(&req.n, 0, sizeof(req.n)); |
| 274 | memset(&req.ndm, 0, sizeof(req.ndm)); |
| 275 | |
| 276 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); |
| 277 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 278 | req.n.nlmsg_type = RTM_NEWNEIGH; |
| 279 | req.ndm.ndm_family = AF_INET; |
| 280 | req.ndm.ndm_state = NUD_STALE; |
| 281 | req.ndm.ndm_ifindex = ifindex; |
| 282 | req.ndm.ndm_type = RTN_UNICAST; |
| 283 | |
| 284 | addattr_l(&req.n, sizeof(req), NDA_DST, &addr, 4); |
| 285 | addattr_l(&req.n, sizeof(req), NDA_LLADDR, lla, llalen); |
Stephen Hemminger | 6cf8398 | 2011-12-23 10:40:04 -0800 | [diff] [blame] | 286 | return rtnl_send(&rth, &req, req.n.nlmsg_len) <= 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 289 | static void prepare_neg_entry(__u8 *ndata, __u32 stamp) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 290 | { |
| 291 | ndata[0] = 0xFF; |
| 292 | ndata[1] = 0; |
| 293 | ndata[2] = stamp>>24; |
| 294 | ndata[3] = stamp>>16; |
| 295 | ndata[4] = stamp>>8; |
| 296 | ndata[5] = stamp; |
| 297 | } |
| 298 | |
| 299 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 300 | static int do_one_request(struct nlmsghdr *n) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 301 | { |
| 302 | struct ndmsg *ndm = NLMSG_DATA(n); |
| 303 | int len = n->nlmsg_len; |
| 304 | struct rtattr * tb[NDA_MAX+1]; |
| 305 | struct dbkey key; |
| 306 | DBT dbkey, dbdat; |
| 307 | int do_acct = 0; |
| 308 | |
| 309 | if (n->nlmsg_type == NLMSG_DONE) { |
| 310 | dbase->sync(dbase, 0); |
| 311 | |
| 312 | /* Now we have at least mirror of kernel db, so that |
| 313 | * may start real resolution. |
| 314 | */ |
| 315 | do_sysctl_adjustments(); |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | if (n->nlmsg_type != RTM_GETNEIGH && n->nlmsg_type != RTM_NEWNEIGH) |
| 320 | return 0; |
| 321 | |
| 322 | len -= NLMSG_LENGTH(sizeof(*ndm)); |
| 323 | if (len < 0) |
| 324 | return -1; |
| 325 | |
| 326 | if (ndm->ndm_family != AF_INET || |
| 327 | (ifnum && !handle_if(ndm->ndm_ifindex)) || |
| 328 | ndm->ndm_flags || |
| 329 | ndm->ndm_type != RTN_UNICAST || |
| 330 | !(ndm->ndm_state&~NUD_NOARP)) |
| 331 | return 0; |
| 332 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 333 | parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len); |
| 334 | |
| 335 | if (!tb[NDA_DST]) |
| 336 | return 0; |
| 337 | |
| 338 | key.iface = ndm->ndm_ifindex; |
| 339 | memcpy(&key.addr, RTA_DATA(tb[NDA_DST]), 4); |
| 340 | dbkey.data = &key; |
| 341 | dbkey.size = sizeof(key); |
| 342 | |
| 343 | if (dbase->get(dbase, &dbkey, &dbdat, 0) != 0) { |
| 344 | dbdat.data = 0; |
| 345 | dbdat.size = 0; |
| 346 | } |
| 347 | |
| 348 | if (n->nlmsg_type == RTM_GETNEIGH) { |
| 349 | if (!(n->nlmsg_flags&NLM_F_REQUEST)) |
| 350 | return 0; |
| 351 | |
| 352 | if (!(ndm->ndm_state&(NUD_PROBE|NUD_INCOMPLETE))) { |
| 353 | stats.app_bad++; |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | if (ndm->ndm_state&NUD_PROBE) { |
| 358 | /* If we get this, kernel still has some valid |
| 359 | * address, but unicast probing failed and host |
| 360 | * is either dead or changed its mac address. |
| 361 | * Kernel is going to initiate broadcast resolution. |
| 362 | * OK, we invalidate our information as well. |
| 363 | */ |
| 364 | if (dbdat.data && !IS_NEG(dbdat.data)) |
| 365 | stats.app_neg++; |
| 366 | |
| 367 | dbase->del(dbase, &dbkey, 0); |
| 368 | } else { |
| 369 | /* If we get this kernel does not have any information. |
| 370 | * If we have something tell this to kernel. */ |
| 371 | stats.app_recv++; |
| 372 | if (dbdat.data && !IS_NEG(dbdat.data)) { |
| 373 | stats.app_success++; |
| 374 | respond_to_kernel(key.iface, key.addr, dbdat.data, dbdat.size); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | /* Sheeit! We have nothing to tell. */ |
| 379 | /* If we have recent negative entry, be silent. */ |
| 380 | if (dbdat.data && NEG_VALID(dbdat.data)) { |
| 381 | if (NEG_CNT(dbdat.data) >= active_probing) { |
| 382 | stats.app_suppressed++; |
| 383 | return 0; |
| 384 | } |
| 385 | do_acct = 1; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (active_probing && |
| 390 | queue_active_probe(ndm->ndm_ifindex, key.addr) == 0 && |
| 391 | do_acct) { |
| 392 | NEG_CNT(dbdat.data)++; |
| 393 | dbase->put(dbase, &dbkey, &dbdat, 0); |
| 394 | } |
| 395 | } else if (n->nlmsg_type == RTM_NEWNEIGH) { |
| 396 | if (n->nlmsg_flags&NLM_F_REQUEST) |
| 397 | return 0; |
| 398 | |
| 399 | if (ndm->ndm_state&NUD_FAILED) { |
| 400 | /* Kernel was not able to resolve. Host is dead. |
| 401 | * Create negative entry if it is not present |
| 402 | * or renew it if it is too old. */ |
| 403 | if (!dbdat.data || |
| 404 | !IS_NEG(dbdat.data) || |
| 405 | !NEG_VALID(dbdat.data)) { |
| 406 | __u8 ndata[6]; |
| 407 | stats.kern_neg++; |
| 408 | prepare_neg_entry(ndata, time(NULL)); |
| 409 | dbdat.data = ndata; |
| 410 | dbdat.size = sizeof(ndata); |
| 411 | dbase->put(dbase, &dbkey, &dbdat, 0); |
| 412 | } |
| 413 | } else if (tb[NDA_LLADDR]) { |
| 414 | if (dbdat.data && !IS_NEG(dbdat.data)) { |
| 415 | if (memcmp(RTA_DATA(tb[NDA_LLADDR]), dbdat.data, dbdat.size) == 0) |
| 416 | return 0; |
| 417 | stats.kern_change++; |
| 418 | } else { |
| 419 | stats.kern_new++; |
| 420 | } |
| 421 | dbdat.data = RTA_DATA(tb[NDA_LLADDR]); |
| 422 | dbdat.size = RTA_PAYLOAD(tb[NDA_LLADDR]); |
| 423 | dbase->put(dbase, &dbkey, &dbdat, 0); |
| 424 | } |
| 425 | } |
| 426 | return 0; |
| 427 | } |
| 428 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 429 | static void load_initial_table(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 430 | { |
Stephen Hemminger | d2468da | 2013-12-20 08:15:02 -0800 | [diff] [blame^] | 431 | if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETNEIGH) < 0) { |
| 432 | perror("dump request failed"); |
| 433 | exit(1); |
| 434 | } |
| 435 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 438 | static void get_kern_msg(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 439 | { |
| 440 | int status; |
| 441 | struct nlmsghdr *h; |
| 442 | struct sockaddr_nl nladdr; |
| 443 | struct iovec iov; |
| 444 | char buf[8192]; |
| 445 | struct msghdr msg = { |
| 446 | (void*)&nladdr, sizeof(nladdr), |
| 447 | &iov, 1, |
| 448 | NULL, 0, |
| 449 | 0 |
| 450 | }; |
| 451 | |
| 452 | memset(&nladdr, 0, sizeof(nladdr)); |
| 453 | |
| 454 | iov.iov_base = buf; |
| 455 | iov.iov_len = sizeof(buf); |
| 456 | |
| 457 | status = recvmsg(rth.fd, &msg, MSG_DONTWAIT); |
| 458 | |
| 459 | if (status <= 0) |
| 460 | return; |
| 461 | |
| 462 | if (msg.msg_namelen != sizeof(nladdr)) |
| 463 | return; |
| 464 | |
| 465 | if (nladdr.nl_pid) |
| 466 | return; |
| 467 | |
| 468 | for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) { |
| 469 | int len = h->nlmsg_len; |
| 470 | int l = len - sizeof(*h); |
| 471 | |
| 472 | if (l < 0 || len > status) |
| 473 | return; |
| 474 | |
| 475 | if (do_one_request(h) < 0) |
| 476 | return; |
| 477 | |
| 478 | status -= NLMSG_ALIGN(len); |
| 479 | h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /* Receive gratuitous ARP messages and store them, that's all. */ |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 484 | static void get_arp_pkt(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 485 | { |
| 486 | unsigned char buf[1024]; |
| 487 | struct sockaddr_ll sll; |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 488 | socklen_t sll_len = sizeof(sll); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 489 | struct arphdr *a = (struct arphdr*)buf; |
| 490 | struct dbkey key; |
| 491 | DBT dbkey, dbdat; |
| 492 | int n; |
| 493 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 494 | n = recvfrom(pset[0].fd, buf, sizeof(buf), MSG_DONTWAIT, |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 495 | (struct sockaddr*)&sll, &sll_len); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 496 | if (n < 0) { |
| 497 | if (errno != EINTR && errno != EAGAIN) |
| 498 | syslog(LOG_ERR, "recvfrom: %m"); |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | if (ifnum && !handle_if(sll.sll_ifindex)) |
| 503 | return; |
| 504 | |
| 505 | /* Sanity checks */ |
| 506 | |
| 507 | if (n < sizeof(*a) || |
| 508 | (a->ar_op != htons(ARPOP_REQUEST) && |
| 509 | a->ar_op != htons(ARPOP_REPLY)) || |
| 510 | a->ar_pln != 4 || |
| 511 | a->ar_pro != htons(ETH_P_IP) || |
| 512 | a->ar_hln != sll.sll_halen || |
| 513 | sizeof(*a) + 2*4 + 2*a->ar_hln > n) |
| 514 | return; |
| 515 | |
| 516 | key.iface = sll.sll_ifindex; |
| 517 | memcpy(&key.addr, (char*)(a+1) + a->ar_hln, 4); |
| 518 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 519 | /* DAD message, ignore. */ |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 520 | if (key.addr == 0) |
| 521 | return; |
| 522 | |
| 523 | dbkey.data = &key; |
| 524 | dbkey.size = sizeof(key); |
| 525 | |
| 526 | if (dbase->get(dbase, &dbkey, &dbdat, 0) == 0 && !IS_NEG(dbdat.data)) { |
| 527 | if (memcmp(dbdat.data, a+1, dbdat.size) == 0) |
| 528 | return; |
| 529 | stats.arp_change++; |
| 530 | } else { |
| 531 | stats.arp_new++; |
| 532 | } |
| 533 | |
| 534 | dbdat.data = a+1; |
| 535 | dbdat.size = a->ar_hln; |
| 536 | dbase->put(dbase, &dbkey, &dbdat, 0); |
| 537 | } |
| 538 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 539 | static void catch_signal(int sig, void (*handler)(int)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 540 | { |
| 541 | struct sigaction sa; |
| 542 | |
| 543 | memset(&sa, 0, sizeof(sa)); |
| 544 | sa.sa_handler = handler; |
| 545 | #ifdef SA_INTERRUPT |
| 546 | sa.sa_flags = SA_INTERRUPT; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 547 | #endif |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 548 | sigaction(sig, &sa, NULL); |
| 549 | } |
| 550 | |
| 551 | #include <setjmp.h> |
| 552 | sigjmp_buf env; |
| 553 | volatile int in_poll; |
| 554 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 555 | static void sig_exit(int signo) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 556 | { |
| 557 | do_exit = 1; |
| 558 | if (in_poll) |
| 559 | siglongjmp(env, 1); |
| 560 | } |
| 561 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 562 | static void sig_sync(int signo) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 563 | { |
| 564 | do_sync = 1; |
| 565 | if (in_poll) |
| 566 | siglongjmp(env, 1); |
| 567 | } |
| 568 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 569 | static void sig_stats(int signo) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 570 | { |
| 571 | do_sync = 1; |
| 572 | do_stats = 1; |
| 573 | if (in_poll) |
| 574 | siglongjmp(env, 1); |
| 575 | } |
| 576 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 577 | static void send_stats(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 578 | { |
| 579 | syslog(LOG_INFO, "arp_rcv: n%lu c%lu app_rcv: tot %lu hits %lu bad %lu neg %lu sup %lu", |
| 580 | stats.arp_new, stats.arp_change, |
| 581 | |
| 582 | stats.app_recv, stats.app_success, |
| 583 | stats.app_bad, stats.app_neg, stats.app_suppressed |
| 584 | ); |
| 585 | syslog(LOG_INFO, "kern: n%lu c%lu neg %lu arp_send: %lu rlim %lu", |
| 586 | stats.kern_new, stats.kern_change, stats.kern_neg, |
| 587 | |
| 588 | stats.probes_sent, stats.probes_suppressed |
| 589 | ); |
| 590 | do_stats = 0; |
| 591 | } |
| 592 | |
| 593 | |
| 594 | int main(int argc, char **argv) |
| 595 | { |
| 596 | int opt; |
| 597 | int do_list = 0; |
| 598 | char *do_load = NULL; |
| 599 | |
Stephen Hemminger | 0df7db3 | 2012-02-17 08:17:09 -0800 | [diff] [blame] | 600 | while ((opt = getopt(argc, argv, "h?b:lf:a:n:p:kR:B:")) != EOF) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 601 | switch (opt) { |
| 602 | case 'b': |
| 603 | dbname = optarg; |
| 604 | break; |
| 605 | case 'f': |
| 606 | if (do_load) { |
| 607 | fprintf(stderr, "Duplicate option -f\n"); |
| 608 | usage(); |
| 609 | } |
| 610 | do_load = optarg; |
| 611 | break; |
| 612 | case 'l': |
| 613 | do_list = 1; |
| 614 | break; |
| 615 | case 'a': |
| 616 | active_probing = atoi(optarg); |
| 617 | break; |
| 618 | case 'n': |
| 619 | negative_timeout = atoi(optarg); |
| 620 | break; |
| 621 | case 'k': |
| 622 | no_kernel_broadcasts = 1; |
| 623 | break; |
Stephen Hemminger | 0df7db3 | 2012-02-17 08:17:09 -0800 | [diff] [blame] | 624 | case 'p': |
| 625 | if ((poll_timeout = 1000 * strtod(optarg, NULL)) < 100) { |
| 626 | fprintf(stderr,"Invalid poll timeout\n"); |
| 627 | exit(-1); |
| 628 | } |
| 629 | break; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 630 | case 'R': |
| 631 | if ((broadcast_rate = atoi(optarg)) <= 0 || |
| 632 | (broadcast_rate = 1000/broadcast_rate) <= 0) { |
| 633 | fprintf(stderr, "Invalid ARP rate\n"); |
| 634 | exit(-1); |
| 635 | } |
| 636 | break; |
| 637 | case 'B': |
| 638 | if ((broadcast_burst = atoi(optarg)) <= 0 || |
| 639 | (broadcast_burst = 1000*broadcast_burst) <= 0) { |
| 640 | fprintf(stderr, "Invalid ARP burst\n"); |
| 641 | exit(-1); |
| 642 | } |
| 643 | break; |
| 644 | case 'h': |
| 645 | case '?': |
| 646 | default: |
| 647 | usage(); |
| 648 | } |
| 649 | } |
| 650 | argc -= optind; |
| 651 | argv += optind; |
| 652 | |
| 653 | if (argc > 0) { |
| 654 | ifnum = argc; |
| 655 | ifnames = argv; |
| 656 | ifvec = malloc(argc*sizeof(int)); |
| 657 | if (!ifvec) { |
| 658 | perror("malloc"); |
| 659 | exit(-1); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | if ((udp_sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 664 | perror("socket"); |
| 665 | exit(-1); |
| 666 | } |
| 667 | |
| 668 | if (ifnum) { |
| 669 | int i; |
| 670 | struct ifreq ifr; |
| 671 | memset(&ifr, 0, sizeof(ifr)); |
| 672 | for (i=0; i<ifnum; i++) { |
| 673 | strncpy(ifr.ifr_name, ifnames[i], IFNAMSIZ); |
| 674 | if (ioctl(udp_sock, SIOCGIFINDEX, &ifr)) { |
| 675 | perror("ioctl(SIOCGIFINDEX)"); |
| 676 | exit(-1);; |
| 677 | } |
| 678 | ifvec[i] = ifr.ifr_ifindex; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | dbase = dbopen(dbname, O_CREAT|O_RDWR, 0644, DB_HASH, NULL); |
| 683 | if (dbase == NULL) { |
| 684 | perror("db_open"); |
| 685 | exit(-1); |
| 686 | } |
| 687 | |
| 688 | if (do_load) { |
| 689 | char buf[128]; |
| 690 | FILE *fp; |
| 691 | struct dbkey k; |
| 692 | DBT dbkey, dbdat; |
| 693 | |
| 694 | dbkey.data = &k; |
| 695 | dbkey.size = sizeof(k); |
| 696 | |
| 697 | if (strcmp(do_load, "-") == 0 || strcmp(do_load, "--") == 0) { |
| 698 | fp = stdin; |
| 699 | } else if ((fp = fopen(do_load, "r")) == NULL) { |
| 700 | perror("fopen"); |
| 701 | goto do_abort; |
| 702 | } |
| 703 | |
| 704 | buf[sizeof(buf)-1] = 0; |
| 705 | while (fgets(buf, sizeof(buf)-1, fp)) { |
| 706 | __u8 b1[6]; |
| 707 | char ipbuf[128]; |
| 708 | char macbuf[128]; |
| 709 | |
| 710 | if (buf[0] == '#') |
| 711 | continue; |
| 712 | |
| 713 | if (sscanf(buf, "%u%s%s", &k.iface, ipbuf, macbuf) != 3) { |
| 714 | fprintf(stderr, "Wrong format of input file \"%s\"\n", do_load); |
| 715 | goto do_abort; |
| 716 | } |
| 717 | if (strncmp(macbuf, "FAILED:", 7) == 0) |
| 718 | continue; |
| 719 | if (!inet_aton(ipbuf, (struct in_addr*)&k.addr)) { |
| 720 | fprintf(stderr, "Invalid IP address: \"%s\"\n", ipbuf); |
| 721 | goto do_abort; |
| 722 | } |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 723 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 724 | dbdat.data = hexstring_a2n(macbuf, b1, 6); |
| 725 | if (dbdat.data == NULL) |
| 726 | goto do_abort; |
| 727 | dbdat.size = 6; |
| 728 | |
| 729 | if (dbase->put(dbase, &dbkey, &dbdat, 0)) { |
| 730 | perror("hash->put"); |
| 731 | goto do_abort; |
| 732 | } |
| 733 | } |
| 734 | dbase->sync(dbase, 0); |
| 735 | if (fp != stdin) |
| 736 | fclose(fp); |
| 737 | } |
| 738 | |
| 739 | if (do_list) { |
| 740 | DBT dbkey, dbdat; |
| 741 | printf("%-8s %-15s %s\n", "#Ifindex", "IP", "MAC"); |
| 742 | while (dbase->seq(dbase, &dbkey, &dbdat, R_NEXT) == 0) { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 743 | struct dbkey *key = dbkey.data; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 744 | if (handle_if(key->iface)) { |
| 745 | if (!IS_NEG(dbdat.data)) { |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 746 | char b1[18]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 747 | printf("%-8d %-15s %s\n", |
| 748 | key->iface, |
| 749 | inet_ntoa(*(struct in_addr*)&key->addr), |
| 750 | hexstring_n2a(dbdat.data, 6, b1, 18)); |
| 751 | } else { |
| 752 | printf("%-8d %-15s FAILED: %dsec ago\n", |
| 753 | key->iface, |
| 754 | inet_ntoa(*(struct in_addr*)&key->addr), |
| 755 | NEG_AGE(dbdat.data)); |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | if (do_load || do_list) |
| 762 | goto out; |
| 763 | |
| 764 | pset[0].fd = socket(PF_PACKET, SOCK_DGRAM, 0); |
| 765 | if (pset[0].fd < 0) { |
| 766 | perror("socket"); |
| 767 | exit(-1); |
| 768 | } |
| 769 | |
| 770 | if (1) { |
| 771 | struct sockaddr_ll sll; |
| 772 | memset(&sll, 0, sizeof(sll)); |
| 773 | sll.sll_family = AF_PACKET; |
| 774 | sll.sll_protocol = htons(ETH_P_ARP); |
| 775 | sll.sll_ifindex = (ifnum == 1 ? ifvec[0] : 0); |
| 776 | if (bind(pset[0].fd, (struct sockaddr*)&sll, sizeof(sll)) < 0) { |
| 777 | perror("bind"); |
| 778 | goto do_abort; |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | if (rtnl_open(&rth, RTMGRP_NEIGH) < 0) { |
| 783 | perror("rtnl_open"); |
| 784 | goto do_abort; |
| 785 | } |
| 786 | pset[1].fd = rth.fd; |
| 787 | |
| 788 | load_initial_table(); |
| 789 | |
Mike Frysinger | a7a9ddb | 2009-11-06 06:04:39 -0500 | [diff] [blame] | 790 | if (daemon(0, 0)) { |
| 791 | perror("arpd: daemon"); |
| 792 | goto do_abort; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | openlog("arpd", LOG_PID | LOG_CONS, LOG_DAEMON); |
| 796 | catch_signal(SIGINT, sig_exit); |
| 797 | catch_signal(SIGTERM, sig_exit); |
| 798 | catch_signal(SIGHUP, sig_sync); |
| 799 | catch_signal(SIGUSR1, sig_stats); |
| 800 | |
| 801 | #define EVENTS (POLLIN|POLLPRI|POLLERR|POLLHUP) |
| 802 | pset[0].events = EVENTS; |
| 803 | pset[0].revents = 0; |
| 804 | pset[1].events = EVENTS; |
| 805 | pset[1].revents = 0; |
| 806 | |
| 807 | sigsetjmp(env, 1); |
| 808 | |
| 809 | for (;;) { |
| 810 | in_poll = 1; |
| 811 | |
| 812 | if (do_exit) |
| 813 | break; |
| 814 | if (do_sync) { |
| 815 | in_poll = 0; |
| 816 | dbase->sync(dbase, 0); |
| 817 | do_sync = 0; |
| 818 | in_poll = 1; |
| 819 | } |
| 820 | if (do_stats) |
| 821 | send_stats(); |
Stephen Hemminger | 0df7db3 | 2012-02-17 08:17:09 -0800 | [diff] [blame] | 822 | if (poll(pset, 2, poll_timeout) > 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 823 | in_poll = 0; |
| 824 | if (pset[0].revents&EVENTS) |
| 825 | get_arp_pkt(); |
| 826 | if (pset[1].revents&EVENTS) |
| 827 | get_kern_msg(); |
| 828 | } else { |
| 829 | do_sync = 1; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | undo_sysctl_adjustments(); |
| 834 | out: |
| 835 | dbase->close(dbase); |
| 836 | exit(0); |
| 837 | |
| 838 | do_abort: |
| 839 | dbase->close(dbase); |
| 840 | exit(-1); |
| 841 | } |