diff options
Diffstat (limited to 'avahi-autoipd')
-rw-r--r-- | avahi-autoipd/Makefile.am | 12 | ||||
-rwxr-xr-x | avahi-autoipd/avahi-autoipd.action.bsd | 2 | ||||
-rwxr-xr-x | avahi-autoipd/avahi-autoipd.action.linux | 23 | ||||
-rwxr-xr-x | avahi-autoipd/dhclient-enter-hook.in | 2 | ||||
-rwxr-xr-x | avahi-autoipd/dhclient-exit-hook.in | 6 | ||||
-rw-r--r-- | avahi-autoipd/iface-linux.c | 66 | ||||
-rw-r--r-- | avahi-autoipd/iface.h | 8 | ||||
-rw-r--r-- | avahi-autoipd/main.c | 113 | ||||
-rw-r--r-- | avahi-autoipd/main.h | 8 |
9 files changed, 108 insertions, 132 deletions
diff --git a/avahi-autoipd/Makefile.am b/avahi-autoipd/Makefile.am index 2f99592..dc43b54 100644 --- a/avahi-autoipd/Makefile.am +++ b/avahi-autoipd/Makefile.am @@ -1,7 +1,5 @@ -# $Id$ -# # This file is part of avahi. -# +# # avahi is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2 of the @@ -54,10 +52,10 @@ nodist_pkgsysconf_SCRIPTS = avahi-autoipd.action if TARGET_FREEBSD avahi-autoipd.action: avahi-autoipd.action.bsd - cp $< $@ + $(AM_V_GEN)cp $< $@ else avahi-autoipd.action: avahi-autoipd.action.linux - cp $< $@ + $(AM_V_GEN)cp $< $@ endif if TARGET_DEBIAN @@ -65,11 +63,11 @@ if TARGET_DEBIAN noinst_SCRIPTS = dhclient-enter-hook dhclient-exit-hook dhclient-enter-hook: dhclient-enter-hook.in - sed -e 's,@sbindir\@,$(sbindir),g' $< > $@ + $(AM_V_GEN)sed -e 's,@sbindir\@,$(sbindir),g' $< > $@ && \ chmod +x $@ dhclient-exit-hook: dhclient-exit-hook.in - sed -e 's,@sbindir\@,$(sbindir),g' $< > $@ + $(AM_V_GEN)sed -e 's,@sbindir\@,$(sbindir),g' $< > $@ && \ chmod +x $@ BUILD = dhclient-exit-hook dhclient-enter-hook diff --git a/avahi-autoipd/avahi-autoipd.action.bsd b/avahi-autoipd/avahi-autoipd.action.bsd index db39c4e..06084fb 100755 --- a/avahi-autoipd/avahi-autoipd.action.bsd +++ b/avahi-autoipd/avahi-autoipd.action.bsd @@ -1,7 +1,5 @@ #!/bin/sh -# $Id$ -# # This file is part of avahi. # # avahi is free software; you can redistribute it and/or modify it diff --git a/avahi-autoipd/avahi-autoipd.action.linux b/avahi-autoipd/avahi-autoipd.action.linux index 27f00bb..c2db994 100755 --- a/avahi-autoipd/avahi-autoipd.action.linux +++ b/avahi-autoipd/avahi-autoipd.action.linux @@ -1,9 +1,7 @@ #!/bin/sh -# $Id$ -# # This file is part of avahi. -# +# # avahi is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2 of the @@ -30,17 +28,26 @@ set -e # $2 interface name # $3 IP adddress +PATH="$PATH:/usr/bin:/usr/sbin:/bin:/sbin" + +# Use a different metric for each interface, so that we can set +# identical routes to multiple interfaces. + +METRIC=$((1000 + `cat "/sys/class/net/$2/ifindex" 2>/dev/null || echo 0`)) + if [ -x /bin/ip -o -x /sbin/ip ] ; then # We have the Linux ip tool from the iproute package case "$1" in BIND) - ip addr add "$3"/16 brd 169.254.255.255 label "$2:avahi" scope link dev "$2" + ip addr add "$3"/16 brd 169.254.255.255 label "$2:avahi" scope link dev "$2" + ip route add default dev "$2" metric "$METRIC" scope link ||: ;; CONFLICT|UNBIND|STOP) - ip addr del "$3"/16 brd 169.254.255.255 label "$2:avahi" scope link dev "$2" + ip route del default dev "$2" metric "$METRIC" scope link ||: + ip addr del "$3"/16 brd 169.254.255.255 label "$2:avahi" scope link dev "$2" ;; *) @@ -55,11 +62,13 @@ elif [ -x /bin/ifconfig -o -x /sbin/ifconfig ] ; then case "$1" in BIND) - ifconfig "$2:3" inet "$3" netmask 255.255.0.0 broadcast 169.254.255.255 up + ifconfig "$2:avahi" inet "$3" netmask 255.255.0.0 broadcast 169.254.255.255 up + route add default dev "$2:avahi" metric "$METRIC" ||: ;; CONFLICT|STOP|UNBIND) - ifconfig "$2:3" down + route del default dev "$2:avahi" metric "$METRIC" ||: + ifconfig "$2:avahi" down ;; *) diff --git a/avahi-autoipd/dhclient-enter-hook.in b/avahi-autoipd/dhclient-enter-hook.in index 10449f1..a746856 100755 --- a/avahi-autoipd/dhclient-enter-hook.in +++ b/avahi-autoipd/dhclient-enter-hook.in @@ -1,7 +1,5 @@ #!/bin/sh -# $Id$ -# # This file is part of avahi. # # avahi is free software; you can redistribute it and/or modify it diff --git a/avahi-autoipd/dhclient-exit-hook.in b/avahi-autoipd/dhclient-exit-hook.in index 3dcb7df..379cb46 100755 --- a/avahi-autoipd/dhclient-exit-hook.in +++ b/avahi-autoipd/dhclient-exit-hook.in @@ -1,9 +1,7 @@ #!/bin/sh -# $Id$ -# # This file is part of avahi. -# +# # avahi is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2 of the @@ -26,7 +24,7 @@ case "$reason" in PREINIT|BOUND|RENEW|REBIND|REBOOT|STOP|RELEASE) # Stopping avahi-autoipd is left for the enter hook ;; - + EXPIRE|FAIL|TIMEOUT) @sbindir@/avahi-autoipd -wD $interface 2> /dev/null ;; diff --git a/avahi-autoipd/iface-linux.c b/avahi-autoipd/iface-linux.c index 5bc0300..fc27829 100644 --- a/avahi-autoipd/iface-linux.c +++ b/avahi-autoipd/iface-linux.c @@ -1,18 +1,16 @@ -/* $Id$ */ - /*** This file is part of avahi. - + avahi is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. - + avahi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. - + You should have received a copy of the GNU Lesser General Public License along with avahi; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 @@ -67,7 +65,7 @@ AVAHI_LLIST_HEAD(Address, addresses) = NULL; int iface_init(int i) { struct sockaddr_nl addr; int on = 1; - + if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) { daemon_log(LOG_ERR, "socket(PF_NETLINK): %s", strerror(errno)); goto fail; @@ -82,22 +80,22 @@ int iface_init(int i) { daemon_log(LOG_ERR, "bind(): %s", strerror(errno)); goto fail; } - + if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) { daemon_log(LOG_ERR, "SO_PASSCRED: %s", strerror(errno)); goto fail; } ifindex = i; - + return fd; - + fail: if (fd >= 0) { close(fd); fd = -1; } - + return -1; } @@ -112,14 +110,14 @@ static int process_nlmsg(struct nlmsghdr *n) { if (ifi->ifi_family != AF_UNSPEC || (int) ifi->ifi_index != ifindex) return 0; - + if (n->nlmsg_type == RTM_DELLINK) { daemon_log(LOG_ERR, "Interface vanished."); return -1; } assert(n->nlmsg_type == RTM_NEWLINK); - + if ((ifi->ifi_flags & IFF_LOOPBACK) || (ifi->ifi_flags & IFF_NOARP) || ifi->ifi_type != ARPHRD_ETHER) { @@ -136,7 +134,7 @@ static int process_nlmsg(struct nlmsghdr *n) { int l; uint32_t address = 0; Address *i; - + ifa = NLMSG_DATA(n); if (ifa->ifa_family != AF_INET || (int) ifa->ifa_index != ifindex) @@ -154,7 +152,7 @@ static int process_nlmsg(struct nlmsghdr *n) { memcpy(&address, RTA_DATA(a), sizeof(uint32_t)); break; } - + a = RTA_NEXT(a, l); } @@ -180,7 +178,7 @@ static int process_nlmsg(struct nlmsghdr *n) { static int process_response(int wait_for_done, unsigned seq) { assert(fd >= 0); - + do { size_t bytes; ssize_t r; @@ -189,39 +187,39 @@ static int process_response(int wait_for_done, unsigned seq) { struct msghdr msghdr; struct cmsghdr *cmsghdr; struct ucred *ucred; - struct iovec iov; + struct iovec iov; struct nlmsghdr *p = (struct nlmsghdr *) replybuf; - + memset(&iov, 0, sizeof(iov)); - iov.iov_base = replybuf; + iov.iov_base = replybuf; iov.iov_len = sizeof(replybuf); memset(&msghdr, 0, sizeof(msghdr)); msghdr.msg_name = (void*) NULL; - msghdr.msg_namelen = 0; - msghdr.msg_iov = &iov; - msghdr.msg_iovlen = 1; - msghdr.msg_control = cred_msg; - msghdr.msg_controllen = sizeof(cred_msg); + msghdr.msg_namelen = 0; + msghdr.msg_iov = &iov; + msghdr.msg_iovlen = 1; + msghdr.msg_control = cred_msg; + msghdr.msg_controllen = sizeof(cred_msg); msghdr.msg_flags = 0; - + if ((r = recvmsg(fd, &msghdr, 0)) < 0) { daemon_log(LOG_ERR, "recvmsg() failed: %s", strerror(errno)); return -1; } if (!(cmsghdr = CMSG_FIRSTHDR(&msghdr)) || cmsghdr->cmsg_type != SCM_CREDENTIALS) { - daemon_log(LOG_WARNING, "No sender credentials received, ignoring data."); + daemon_log(LOG_WARNING, "No sender credentials received, ignoring data."); return -1; } ucred = (struct ucred*) CMSG_DATA(cmsghdr); - + if (ucred->uid != 0) return -1; bytes = (size_t) r; - + for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) { if (!NLMSG_OK(p, bytes) || bytes < sizeof(struct nlmsghdr) || bytes < p->nlmsg_len) { @@ -258,7 +256,7 @@ int iface_get_initial_state(State *state) { assert(fd >= 0); assert(state); - + memset(&req, 0, sizeof(req)); n = (struct nlmsghdr*) req; n->nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)); @@ -278,7 +276,7 @@ int iface_get_initial_state(State *state) { if (process_response(1, 0) < 0) return -1; - + n->nlmsg_type = RTM_GETADDR; n->nlmsg_len = NLMSG_LENGTH(sizeof(*ifa)); n->nlmsg_seq = ++seq; @@ -286,7 +284,7 @@ int iface_get_initial_state(State *state) { ifa = NLMSG_DATA(n); ifa->ifa_family = AF_INET; ifa->ifa_index = ifindex; - + if (send(fd, n, n->nlmsg_len, 0) < 0) { daemon_log(LOG_ERR, "send(): %s", strerror(errno)); return -1; @@ -296,7 +294,7 @@ int iface_get_initial_state(State *state) { return -1; *state = addresses ? STATE_SLEEPING : STATE_START; - + return 0; } @@ -305,7 +303,7 @@ int iface_process(Event *event) { assert(fd >= 0); b = !!addresses; - + if (process_response(0, 0) < 0) return -1; @@ -313,13 +311,13 @@ int iface_process(Event *event) { *event = EVENT_ROUTABLE_ADDR_UNCONFIGURED; else if (!b && addresses) *event = EVENT_ROUTABLE_ADDR_CONFIGURED; - + return 0; } void iface_done(void) { Address *a; - + if (fd >= 0) { close(fd); fd = -1; diff --git a/avahi-autoipd/iface.h b/avahi-autoipd/iface.h index e3f913e..95c97fd 100644 --- a/avahi-autoipd/iface.h +++ b/avahi-autoipd/iface.h @@ -1,21 +1,19 @@ #ifndef fooavahiifacehfoo #define fooavahiifacehfoo -/* $Id$ */ - /*** This file is part of avahi. - + avahi is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. - + avahi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. - + You should have received a copy of the GNU Lesser General Public License along with avahi; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 diff --git a/avahi-autoipd/main.c b/avahi-autoipd/main.c index eb71c3b..4b35f7c 100644 --- a/avahi-autoipd/main.c +++ b/avahi-autoipd/main.c @@ -1,5 +1,3 @@ -/* $Id$ */ - /*** This file is part of avahi. @@ -566,8 +564,7 @@ fail: return -1; } -static void -close_socket(int fd) { +static void close_socket(int fd) { close(fd); } @@ -582,16 +579,14 @@ static uint8_t __lladdr[ETHER_ADDRLEN]; #define elementsof(array) (sizeof(array)/sizeof(array[0])) #endif -static int -__get_ether_addr(int ifindex, u_char *lladdr) -{ - int mib[6]; - char *buf; - struct if_msghdr *ifm; - char *lim; - char *next; - struct sockaddr_dl *sdl; - size_t len; +static int __get_ether_addr(int ifindex, u_char *lladdr) { + int mib[6]; + char *buf; + struct if_msghdr *ifm; + char *lim; + char *next; + struct sockaddr_dl *sdl; + size_t len; mib[0] = CTL_NET; mib[1] = PF_ROUTE; @@ -603,20 +598,15 @@ __get_ether_addr(int ifindex, u_char *lladdr) if (sysctl(mib, elementsof(mib), NULL, &len, NULL, 0) != 0) { daemon_log(LOG_ERR, "sysctl(NET_RT_IFLIST): %s", strerror(errno)); - return (-1); - } - - buf = malloc(len); - if (buf == NULL) { - daemon_log(LOG_ERR, "malloc(%d): %s", len, strerror(errno)); - return (-1); + return -1; } + buf = avahi_malloc(len); if (sysctl(mib, elementsof(mib), buf, &len, NULL, 0) != 0) { daemon_log(LOG_ERR, "sysctl(NET_RT_IFLIST): %s", strerror(errno)); free(buf); - return (-1); + return -1; } lim = buf + len; @@ -627,34 +617,31 @@ __get_ether_addr(int ifindex, u_char *lladdr) memcpy(lladdr, LLADDR(sdl), ETHER_ADDRLEN); } } - free(buf); + avahi_free(buf); - return (0); + return 0; } #define PCAP_TIMEOUT 500 /* 0.5s */ -static int -open_socket(int iface, uint8_t *hw_address) -{ - struct bpf_program bpf; - char *filter; - char ifname[IFNAMSIZ]; - pcap_t *pp; - int err; - int fd; +static int open_socket(int iface, uint8_t *hw_address) { + struct bpf_program bpf; + char *filter; + char ifname[IFNAMSIZ]; + pcap_t *pp; + int err; + int fd; assert(__pp == NULL); - if (interface_up(iface) < 0) { - return (-1); - } - if (__get_ether_addr(iface, __lladdr) == -1) { - return (-1); - } - if (if_indextoname(iface, ifname) == NULL) { - return (-1); - } + if (interface_up(iface) < 0) + return -1; + + if (__get_ether_addr(iface, __lladdr) == -1) + return -1; + + if (if_indextoname(iface, ifname) == NULL) + return -1; /* * Using a timeout for BPF is fairly portable across BSDs. On most @@ -728,10 +715,7 @@ open_socket(int iface, uint8_t *hw_address) return (fd); } -static void -close_socket(int fd __unused) -{ - +static void close_socket(int fd AVAHI_GCC_UNUSED) { assert(__pp != NULL); pcap_close(__pp); __pp = NULL; @@ -741,10 +725,7 @@ close_socket(int fd __unused) * We trick avahi into allocating sizeof(packet) + sizeof(ether_header), * and prepend the required ethernet header information before sending. */ -static int -send_packet(int fd __unused, int iface __unused, ArpPacket *packet, - size_t packet_len) -{ +static int send_packet(int fd AVAHI_GCC_UNUSED, int iface AVAHI_GCC_UNUSED, ArpPacket *packet, size_t packet_len) { struct ether_header *eh; assert(__pp != NULL); @@ -758,14 +739,12 @@ send_packet(int fd __unused, int iface __unused, ArpPacket *packet, return (pcap_inject(__pp, (void *)eh, packet_len + sizeof(*eh))); } -static int -recv_packet(int fd __unused, ArpPacket **packet, size_t *packet_len) -{ - struct pcap_pkthdr *ph; - u_char *pd; - ArpPacket *ap; - int err; - int retval; +static int recv_packet(int fd AVAHI_GCC_UNUSED, ArpPacket **packet, size_t *packet_len) { + struct pcap_pkthdr *ph; + u_char *pd; + ArpPacket *ap; + int err; + int retval; assert(__pp != NULL); assert(packet != NULL); @@ -791,10 +770,9 @@ recv_packet(int fd __unused, ArpPacket **packet, size_t *packet_len) * didn't match the filter, in which case we'll get 0 packets. */ retval = 0; - } else { + } else daemon_log(LOG_ERR, "pcap_next_ex(%d): %s", err, pcap_geterr(__pp)); - } return (retval); } @@ -843,8 +821,6 @@ static FILE* fork_dispatcher(void) { daemon_retval_done(); - setsid(); - avahi_set_proc_title(argv0, "%s: [%s] callout dispatcher", argv0, interface_name); close(fds[1]); @@ -1097,7 +1073,7 @@ static int loop(int iface, uint32_t addr) { char *address_fn = NULL; const char *p; - daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP,0); + daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0); if (!(dispatcher = fork_dispatcher())) goto fail; @@ -1244,9 +1220,14 @@ static int loop(int iface, uint32_t addr) { int conflict = 0; if (info.sender_ip_address == addr) { - /* Normal conflict */ - conflict = 1; - daemon_log(LOG_INFO, "Received conflicting normal ARP packet."); + + if (memcmp(hw_address, info.sender_hw_address, ETHER_ADDRLEN)) { + /* Normal conflict */ + conflict = 1; + daemon_log(LOG_INFO, "Received conflicting normal ARP packet."); + } else + daemon_log(LOG_DEBUG, "Received ARP packet back on source interface. Ignoring."); + } else if (state == STATE_WAITING_PROBE || state == STATE_PROBING || state == STATE_WAITING_ANNOUNCE) { /* Probe conflict */ conflict = info.target_ip_address == addr && memcmp(hw_address, info.sender_hw_address, ETHER_ADDRLEN); diff --git a/avahi-autoipd/main.h b/avahi-autoipd/main.h index adadc99..cc02df7 100644 --- a/avahi-autoipd/main.h +++ b/avahi-autoipd/main.h @@ -1,21 +1,19 @@ #ifndef fooavahimainhfoo #define fooavahimainhfoo -/* $Id$ */ - /*** This file is part of avahi. - + avahi is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. - + avahi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. - + You should have received a copy of the GNU Lesser General Public License along with avahi; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |