ctdb: Remove unused ctdb_set_process_name
[ambi/samba-autobuild/.git] / ctdb / common / system_linux.c
index 82a6575bab333198a2fbe042cfd2bb1b73045757..23c83d494ba7bd370c626a28421baf38dd8f266a 100644 (file)
    along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "includes.h"
+#include "replace.h"
 #include "system/network.h"
 #include "system/filesys.h"
 #include "system/wait.h"
-#include "../include/ctdb_private.h"
-#include "lib/events/events.h"
+
+#include "lib/util/debug.h"
+
+#include "ctdb_private.h"
+
 #include <netinet/if_ether.h>
+#include <netinet/ip6.h>
+#include <netinet/icmp6.h>
 #include <net/if_arp.h>
+#include <netpacket/packet.h>
+#include <sys/prctl.h>
 
+#include "common/logging.h"
+#include "common/system.h"
 
+#ifndef ETHERTYPE_IP6
+#define ETHERTYPE_IP6 0x86dd
+#endif
+
+/*
+  calculate the tcp checksum for tcp over ipv6
+*/
+static uint16_t tcp_checksum6(uint16_t *data, size_t n, struct ip6_hdr *ip6)
+{
+       uint32_t phdr[2];
+       uint32_t sum = 0;
+       uint16_t sum2;
+
+       sum += uint16_checksum((uint16_t *)(void *)&ip6->ip6_src, 16);
+       sum += uint16_checksum((uint16_t *)(void *)&ip6->ip6_dst, 16);
+
+       phdr[0] = htonl(n);
+       phdr[1] = htonl(ip6->ip6_nxt);
+       sum += uint16_checksum((uint16_t *)phdr, 8);
+
+       sum += uint16_checksum(data, n);
+
+       sum = (sum & 0xFFFF) + (sum >> 16);
+       sum = (sum & 0xFFFF) + (sum >> 16);
+       sum2 = htons(sum);
+       sum2 = ~sum2;
+       if (sum2 == 0) {
+               return 0xFFFF;
+       }
+       return sum2;
+}
 
 /*
   send gratuitous arp reply after we have taken over an ip address
   saddr is the address we are trying to claim
   iface is the interface name we will be using to claim the address
  */
-int ctdb_sys_send_arp(const struct sockaddr_in *saddr, const char *iface)
+int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface)
 {
        int s, ret;
-       struct sockaddr sa;
+       struct sockaddr_ll sall;
        struct ether_header *eh;
        struct arphdr *ah;
+       struct ip6_hdr *ip6;
+       struct nd_neighbor_advert *nd_na;
+       struct nd_opt_hdr *nd_oh;
        struct ifreq if_hwaddr;
-       unsigned char buffer[64]; /*minimum eth frame size */
+       /* Size of IPv6 neighbor advertisement (with option) */
+       unsigned char buffer[sizeof(struct ether_header) +
+                            sizeof(struct ip6_hdr) +
+                            sizeof(struct nd_neighbor_advert) +
+                            sizeof(struct nd_opt_hdr) + ETH_ALEN];
        char *ptr;
+       char bdcast[] = {0xff,0xff,0xff,0xff,0xff,0xff};
+       struct ifreq ifr;
+
+       ZERO_STRUCT(sall);
+       ZERO_STRUCT(ifr);
+       ZERO_STRUCT(if_hwaddr);
+
+       switch (addr->ip.sin_family) {
+       case AF_INET:
+               s = socket(PF_PACKET, SOCK_RAW, htons(ETHERTYPE_ARP));
+               if (s == -1){
+                       DEBUG(DEBUG_CRIT,(__location__ " failed to open raw socket\n"));
+                       return -1;
+               }
 
-       ZERO_STRUCT(sa);
+               DEBUG(DEBUG_DEBUG, (__location__ " Created SOCKET FD:%d for sending arp\n", s));
+               strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
+               if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
+                       DEBUG(DEBUG_CRIT,(__location__ " interface '%s' not found\n", iface));
+                       close(s);
+                       return -1;
+               }
 
-       /* for now, we only handle AF_INET addresses */
-       if (saddr->sin_family != AF_INET) {
-               DEBUG(0,(__location__ " not an ipv4 address (family is %u)\n", saddr->sin_family));
-               return -1;
-       }
+               /* get the mac address */
+               strncpy(if_hwaddr.ifr_name, iface, sizeof(if_hwaddr.ifr_name)-1);
+               ret = ioctl(s, SIOCGIFHWADDR, &if_hwaddr);
+               if ( ret < 0 ) {
+                       close(s);
+                       DEBUG(DEBUG_CRIT,(__location__ " ioctl failed\n"));
+                       return -1;
+               }
+               if (ARPHRD_LOOPBACK == if_hwaddr.ifr_hwaddr.sa_family) {
+                       DEBUG(DEBUG_DEBUG,("Ignoring loopback arp request\n"));
+                       close(s);
+                       return 0;
+               }
+               if (if_hwaddr.ifr_hwaddr.sa_family != AF_LOCAL) {
+                       close(s);
+                       errno = EINVAL;
+                       DEBUG(DEBUG_CRIT,(__location__ " not an ethernet address family (0x%x)\n",
+                                if_hwaddr.ifr_hwaddr.sa_family));
+                       return -1;
+               }
 
-       s = socket(AF_INET, SOCK_PACKET, htons(ETHERTYPE_ARP));
-       if (s == -1){
-               DEBUG(0,(__location__ " failed to open raw socket\n"));
-               return -1;
-       }
 
-       /* get the mac address */
-       strcpy(if_hwaddr.ifr_name, iface);
-       ret = ioctl(s, SIOCGIFHWADDR, &if_hwaddr);
-       if ( ret < 0 ) {
-               close(s);
-               DEBUG(0,(__location__ " ioctl failed\n"));
-               return -1;
-       }
-       if (ARPHRD_LOOPBACK == if_hwaddr.ifr_hwaddr.sa_family) {
-               DEBUG(3,("Ignoring loopback arp request\n"));
-               close(s);
-               return 0;
-       }
-       if (if_hwaddr.ifr_hwaddr.sa_family != AF_LOCAL) {
+               memset(buffer, 0 , 64);
+               eh = (struct ether_header *)buffer;
+               memset(eh->ether_dhost, 0xff, ETH_ALEN);
+               memcpy(eh->ether_shost, if_hwaddr.ifr_hwaddr.sa_data, ETH_ALEN);
+               eh->ether_type = htons(ETHERTYPE_ARP);
+       
+               ah = (struct arphdr *)&buffer[sizeof(struct ether_header)];
+               ah->ar_hrd = htons(ARPHRD_ETHER);
+               ah->ar_pro = htons(ETH_P_IP);
+               ah->ar_hln = ETH_ALEN;
+               ah->ar_pln = 4;
+
+               /* send a gratious arp */
+               ah->ar_op  = htons(ARPOP_REQUEST);
+               ptr = (char *)&ah[1];
+               memcpy(ptr, if_hwaddr.ifr_hwaddr.sa_data, ETH_ALEN);
+               ptr+=ETH_ALEN;
+               memcpy(ptr, &addr->ip.sin_addr, 4);       
+               ptr+=4;
+               memset(ptr, 0, ETH_ALEN); 
+               ptr+=ETH_ALEN;
+               memcpy(ptr, &addr->ip.sin_addr, 4);       
+               ptr+=4;
+       
+               sall.sll_family = AF_PACKET;
+               sall.sll_halen = 6;
+               memcpy(&sall.sll_addr[0], bdcast, sall.sll_halen);
+               sall.sll_protocol = htons(ETH_P_ALL);
+               sall.sll_ifindex = ifr.ifr_ifindex;
+               ret = sendto(s, buffer, 64, 0, (struct sockaddr *)&sall, sizeof(sall));
+               if (ret < 0 ){
+                       close(s);
+                       DEBUG(DEBUG_CRIT,(__location__ " failed sendto\n"));
+                       return -1;
+               }       
+
+               /* send unsolicited arp reply broadcast */
+               ah->ar_op  = htons(ARPOP_REPLY);
+               ptr = (char *)&ah[1];
+               memcpy(ptr, if_hwaddr.ifr_hwaddr.sa_data, ETH_ALEN);
+               ptr+=ETH_ALEN;
+               memcpy(ptr, &addr->ip.sin_addr, 4);       
+               ptr+=4;
+               memcpy(ptr, if_hwaddr.ifr_hwaddr.sa_data, ETH_ALEN);
+               ptr+=ETH_ALEN;
+               memcpy(ptr, &addr->ip.sin_addr, 4);       
+               ptr+=4;
+
+               ret = sendto(s, buffer, 64, 0, (struct sockaddr *)&sall, sizeof(sall));
+               if (ret < 0 ){
+                       DEBUG(DEBUG_CRIT,(__location__ " failed sendto\n"));
+                       close(s);
+                       return -1;
+               }
+
                close(s);
-               errno = EINVAL;
-               DEBUG(0,(__location__ " not an ethernet address family (0x%x)\n",
-                        if_hwaddr.ifr_hwaddr.sa_family));
-               return -1;
-       }
+               break;
+       case AF_INET6:
+               s = socket(PF_PACKET, SOCK_RAW, htons(ETHERTYPE_ARP));
+               if (s == -1){
+                       DEBUG(DEBUG_CRIT,(__location__ " failed to open raw socket\n"));
+                       return -1;
+               }
 
+               DEBUG(DEBUG_DEBUG, (__location__ " Created SOCKET FD:%d for sending arp\n", s));
+               strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
+               if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
+                       DEBUG(DEBUG_CRIT,(__location__ " interface '%s' not found\n", iface));
+                       close(s);
+                       return -1;
+               }
 
-       memset(buffer, 0 , 64);
-       eh = (struct ether_header *)buffer;
-       memset(eh->ether_dhost, 0xff, ETH_ALEN);
-       memcpy(eh->ether_shost, if_hwaddr.ifr_hwaddr.sa_data, ETH_ALEN);
-       eh->ether_type = htons(ETHERTYPE_ARP);
-
-       ah = (struct arphdr *)&buffer[sizeof(struct ether_header)];
-       ah->ar_hrd = htons(ARPHRD_ETHER);
-       ah->ar_pro = htons(ETH_P_IP);
-       ah->ar_hln = ETH_ALEN;
-       ah->ar_pln = 4;
-
-       /* send a gratious arp */
-       ah->ar_op  = htons(ARPOP_REQUEST);
-       ptr = (char *)&ah[1];
-       memcpy(ptr, if_hwaddr.ifr_hwaddr.sa_data, ETH_ALEN);
-       ptr+=ETH_ALEN;
-       memcpy(ptr, &saddr->sin_addr, 4);         
-       ptr+=4;
-       memset(ptr, 0, ETH_ALEN); 
-       ptr+=ETH_ALEN;
-       memcpy(ptr, &saddr->sin_addr, 4);         
-       ptr+=4;
-
-       strncpy(sa.sa_data, iface, sizeof(sa.sa_data));
-       ret = sendto(s, buffer, 64, 0, &sa, sizeof(sa));
-       if (ret < 0 ){
-               close(s);
-               DEBUG(0,(__location__ " failed sendto\n"));
-               return -1;
-       }
+               /* get the mac address */
+               strncpy(if_hwaddr.ifr_name, iface, sizeof(if_hwaddr.ifr_name)-1);
+               ret = ioctl(s, SIOCGIFHWADDR, &if_hwaddr);
+               if ( ret < 0 ) {
+                       close(s);
+                       DEBUG(DEBUG_CRIT,(__location__ " ioctl failed\n"));
+                       return -1;
+               }
+               if (ARPHRD_LOOPBACK == if_hwaddr.ifr_hwaddr.sa_family) {
+                       DEBUG(DEBUG_DEBUG,("Ignoring loopback arp request\n"));
+                       close(s);
+                       return 0;
+               }
+               if (if_hwaddr.ifr_hwaddr.sa_family != AF_LOCAL) {
+                       close(s);
+                       errno = EINVAL;
+                       DEBUG(DEBUG_CRIT,(__location__ " not an ethernet address family (0x%x)\n",
+                                if_hwaddr.ifr_hwaddr.sa_family));
+                       return -1;
+               }
+
+               memset(buffer, 0 , sizeof(buffer));
+               eh = (struct ether_header *)buffer;
+               /* Ethernet multicast: 33:33:00:00:00:01 (see RFC2464,
+                * section 7) - note zeroes above! */
+               eh->ether_dhost[0] = eh->ether_dhost[1] = 0x33;
+               eh->ether_dhost[5] = 0x01;
+               memcpy(eh->ether_shost, if_hwaddr.ifr_hwaddr.sa_data, ETH_ALEN);
+               eh->ether_type = htons(ETHERTYPE_IP6);
+
+               ip6 = (struct ip6_hdr *)(eh+1);
+               ip6->ip6_vfc  = 0x60;
+               ip6->ip6_plen = htons(sizeof(*nd_na) +
+                                     sizeof(struct nd_opt_hdr) +
+                                     ETH_ALEN);
+               ip6->ip6_nxt  = IPPROTO_ICMPV6;
+               ip6->ip6_hlim = 255;
+               ip6->ip6_src  = addr->ip6.sin6_addr;
+               /* all-nodes multicast */
+               inet_pton(AF_INET6, "ff02::1", &ip6->ip6_dst);
+
+               nd_na = (struct nd_neighbor_advert *)(ip6+1);
+               nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
+               nd_na->nd_na_code = 0;
+               nd_na->nd_na_flags_reserved = ND_NA_FLAG_OVERRIDE;
+               nd_na->nd_na_target = addr->ip6.sin6_addr;
+               /* Option: Target link-layer address */
+               nd_oh = (struct nd_opt_hdr *)(nd_na+1);
+               nd_oh->nd_opt_type = ND_OPT_TARGET_LINKADDR;
+               nd_oh->nd_opt_len = 1;
+               memcpy(&(nd_oh+1)[0], if_hwaddr.ifr_hwaddr.sa_data, ETH_ALEN);
+
+               nd_na->nd_na_cksum = tcp_checksum6((uint16_t *)nd_na,
+                                                  ntohs(ip6->ip6_plen), ip6);
+
+               sall.sll_family = AF_PACKET;
+               sall.sll_halen = 6;
+               memcpy(&sall.sll_addr[0], &eh->ether_dhost[0], sall.sll_halen);
+               sall.sll_protocol = htons(ETH_P_ALL);
+               sall.sll_ifindex = ifr.ifr_ifindex;
+               ret = sendto(s, buffer, sizeof(buffer),
+                            0, (struct sockaddr *)&sall, sizeof(sall));
+               if (ret < 0 ){
+                       close(s);
+                       DEBUG(DEBUG_CRIT,(__location__ " failed sendto\n"));
+                       return -1;
+               }       
 
-       /* send unsolicited arp reply broadcast */
-       ah->ar_op  = htons(ARPOP_REPLY);
-       ptr = (char *)&ah[1];
-       memcpy(ptr, if_hwaddr.ifr_hwaddr.sa_data, ETH_ALEN);
-       ptr+=ETH_ALEN;
-       memcpy(ptr, &saddr->sin_addr, 4);         
-       ptr+=4;
-       memcpy(ptr, if_hwaddr.ifr_hwaddr.sa_data, ETH_ALEN);
-       ptr+=ETH_ALEN;
-       memcpy(ptr, &saddr->sin_addr, 4);         
-       ptr+=4;
-
-       strncpy(sa.sa_data, iface, sizeof(sa.sa_data));
-       ret = sendto(s, buffer, 64, 0, &sa, sizeof(sa));
-       if (ret < 0 ){
-               DEBUG(0,(__location__ " failed sendto\n"));
+               close(s);
+               break;
+       default:
+               DEBUG(DEBUG_CRIT,(__location__ " not an ipv4/ipv6 address (family is %u)\n", addr->ip.sin_family));
                return -1;
        }
 
-       close(s);
        return 0;
 }
 
 
-/*
-  uint16 checksum for n bytes
- */
-static uint32_t uint16_checksum(uint16_t *data, size_t n)
-{
-       uint32_t sum=0;
-       while (n>=2) {
-               sum += (uint32_t)ntohs(*data);
-               data++;
-               n -= 2;
-       }
-       if (n == 1) {
-               sum += (uint32_t)ntohs(*(uint8_t *)data);
-       }
-       return sum;
-}
-
 /*
   simple TCP checksum - assumes data is multiple of 2 bytes long
  */
@@ -187,156 +320,130 @@ static uint16_t tcp_checksum(uint16_t *data, size_t n, struct iphdr *ip)
   This can also be used to send RST segments (if rst is true) and also
   if correct seq and ack numbers are provided.
  */
-int ctdb_sys_send_tcp(int s,
-                     const struct sockaddr_in *dest, 
-                     const struct sockaddr_in *src,
+int ctdb_sys_send_tcp(const ctdb_sock_addr *dest, 
+                     const ctdb_sock_addr *src,
                      uint32_t seq, uint32_t ack, int rst)
 {
+       int s;
        int ret;
+       uint32_t one = 1;
+       uint16_t tmpport;
+       ctdb_sock_addr *tmpdest;
        struct {
                struct iphdr ip;
                struct tcphdr tcp;
-       } pkt;
-
-       /* for now, we only handle AF_INET addresses */
-       if (src->sin_family != AF_INET || dest->sin_family != AF_INET) {
-               DEBUG(0,(__location__ " not an ipv4 address\n"));
-               return -1;
-       }
-
-       ZERO_STRUCT(pkt);
-       pkt.ip.version  = 4;
-       pkt.ip.ihl      = sizeof(pkt.ip)/4;
-       pkt.ip.tot_len  = htons(sizeof(pkt));
-       pkt.ip.ttl      = 255;
-       pkt.ip.protocol = IPPROTO_TCP;
-       pkt.ip.saddr    = src->sin_addr.s_addr;
-       pkt.ip.daddr    = dest->sin_addr.s_addr;
-       pkt.ip.check    = 0;
-
-       pkt.tcp.source   = src->sin_port;
-       pkt.tcp.dest     = dest->sin_port;
-       pkt.tcp.seq      = seq;
-       pkt.tcp.ack_seq  = ack;
-       pkt.tcp.ack      = 1;
-       if (rst) {
-               pkt.tcp.rst      = 1;
-       }
-       pkt.tcp.doff     = sizeof(pkt.tcp)/4;
-       pkt.tcp.window   = htons(1234); /* this makes it easier to spot in a sniffer */
-       pkt.tcp.check    = tcp_checksum((uint16_t *)&pkt.tcp, sizeof(pkt.tcp), &pkt.ip);
-
-       ret = sendto(s, &pkt, sizeof(pkt), 0, dest, sizeof(*dest));
-       if (ret != sizeof(pkt)) {
-               DEBUG(0,(__location__ " failed sendto (%s)\n", strerror(errno)));
-               return -1;
-       }
-
-       return 0;
-}
-
-
-/*
-  see if we currently have an interface with the given IP
-
-  we try to bind to it, and if that fails then we don't have that IP
-  on an interface
-  if is_loopback is specified it will also return whether the ip address
-  is attached to the loopback interface or not
-
-  ifname, if non-NULL, will return the name of the interface this ip is tied to
- */
-bool ctdb_sys_have_ip(struct sockaddr_in ip, bool *is_loopback, TALLOC_CTX *mem_ctx, char **ifname)
-{
-       struct ifreq *ifr = NULL;
-       struct ifconf ifc;
-       int s, i, num_ifs;
-       int ret;
-
-       if (is_loopback) {
-               *is_loopback = false;
-       }
-       if (*ifname) {
-               *ifname = NULL;
-       }
-       
-       ip.sin_port = 0;
-       s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
-       if (s == -1) {
-               return false;
-       }
-       ret = bind(s, (struct sockaddr *)&ip, sizeof(ip));
-       if (ret) {
-               goto finished;
-       }
-
-
-       /* find out how much space we need to store the interface details */
-       ifc.ifc_len = 0;
-       ifc.ifc_req = NULL;
-       ret = ioctl(s, SIOCGIFCONF, &ifc);
-       if (ret) {
-               DEBUG(0,(__location__ " ioctl to read interface list failed\n"));
-               goto finished;
-       }
-
-       ifr = talloc_size(mem_ctx, ifc.ifc_len);
+       } ip4pkt;
+       struct {
+               struct ip6_hdr ip6;
+               struct tcphdr tcp;
+       } ip6pkt;
+
+       switch (src->ip.sin_family) {
+       case AF_INET:
+               ZERO_STRUCT(ip4pkt);
+               ip4pkt.ip.version  = 4;
+               ip4pkt.ip.ihl      = sizeof(ip4pkt.ip)/4;
+               ip4pkt.ip.tot_len  = htons(sizeof(ip4pkt));
+               ip4pkt.ip.ttl      = 255;
+               ip4pkt.ip.protocol = IPPROTO_TCP;
+               ip4pkt.ip.saddr    = src->ip.sin_addr.s_addr;
+               ip4pkt.ip.daddr    = dest->ip.sin_addr.s_addr;
+               ip4pkt.ip.check    = 0;
+
+               ip4pkt.tcp.source   = src->ip.sin_port;
+               ip4pkt.tcp.dest     = dest->ip.sin_port;
+               ip4pkt.tcp.seq      = seq;
+               ip4pkt.tcp.ack_seq  = ack;
+               ip4pkt.tcp.ack      = 1;
+               if (rst) {
+                       ip4pkt.tcp.rst      = 1;
+               }
+               ip4pkt.tcp.doff     = sizeof(ip4pkt.tcp)/4;
+               /* this makes it easier to spot in a sniffer */
+               ip4pkt.tcp.window   = htons(1234);
+               ip4pkt.tcp.check    = tcp_checksum((uint16_t *)&ip4pkt.tcp, sizeof(ip4pkt.tcp), &ip4pkt.ip);
+
+               /* open a raw socket to send this segment from */
+               s = socket(AF_INET, SOCK_RAW, htons(IPPROTO_RAW));
+               if (s == -1) {
+                       DEBUG(DEBUG_CRIT,(__location__ " failed to open raw socket (%s)\n",
+                                strerror(errno)));
+                       return -1;
+               }
 
-       /* get a list of all interface names and addresses */
-       ifc.ifc_req = ifr;
-       ret = ioctl(s, SIOCGIFCONF, &ifc);
-       if (ret) {
-               DEBUG(0,(__location__ " ioctl to read interface list failed\n"));
-               goto finished;
-       }
+               ret = setsockopt(s, SOL_IP, IP_HDRINCL, &one, sizeof(one));
+               if (ret != 0) {
+                       DEBUG(DEBUG_CRIT,(__location__ " failed to setup IP headers (%s)\n",
+                                strerror(errno)));
+                       close(s);
+                       return -1;
+               }
 
-       /* loop over all interfaces and search for the one matching ip */
-       num_ifs = ifc.ifc_len/sizeof(struct ifreq);
-       for (i=0; i<num_ifs;i++) {
-               struct sockaddr_in *sa;
+               set_nonblocking(s);
+               set_close_on_exec(s);
 
-               /* we only care bout ipv4 addresses */
-               sa = (struct sockaddr_in *)&ifr[i].ifr_addr;
-               if (sa->sin_family != AF_INET) {
-                       continue;
+               ret = sendto(s, &ip4pkt, sizeof(ip4pkt), 0,
+                            (const struct sockaddr *)&dest->ip,
+                            sizeof(dest->ip));
+               close(s);
+               if (ret != sizeof(ip4pkt)) {
+                       DEBUG(DEBUG_CRIT,(__location__ " failed sendto (%s)\n", strerror(errno)));
+                       return -1;
                }
-
-               /* this is not the interface you are looking for */
-               if (!ctdb_same_ip(sa, &ip)) {
-                       continue;
+               break;
+       case AF_INET6:
+               ZERO_STRUCT(ip6pkt);
+               ip6pkt.ip6.ip6_vfc  = 0x60;
+               ip6pkt.ip6.ip6_plen = htons(20);
+               ip6pkt.ip6.ip6_nxt  = IPPROTO_TCP;
+               ip6pkt.ip6.ip6_hlim = 64;
+               ip6pkt.ip6.ip6_src  = src->ip6.sin6_addr;
+               ip6pkt.ip6.ip6_dst  = dest->ip6.sin6_addr;
+
+               ip6pkt.tcp.source   = src->ip6.sin6_port;
+               ip6pkt.tcp.dest     = dest->ip6.sin6_port;
+               ip6pkt.tcp.seq      = seq;
+               ip6pkt.tcp.ack_seq  = ack;
+               ip6pkt.tcp.ack      = 1;
+               if (rst) {
+                       ip6pkt.tcp.rst      = 1;
                }
+               ip6pkt.tcp.doff     = sizeof(ip6pkt.tcp)/4;
+               /* this makes it easier to spot in a sniffer */
+               ip6pkt.tcp.window   = htons(1234);
+               ip6pkt.tcp.check    = tcp_checksum6((uint16_t *)&ip6pkt.tcp, sizeof(ip6pkt.tcp), &ip6pkt.ip6);
 
-               /* this is the ifr entry for this interface/address 
-                  read the interface flags so we can tell if it is 
-                  loopback or not
-               */
-               ret = ioctl(s, SIOCGIFFLAGS, &ifr[i]);
-               if (ret) {
-                       DEBUG(0,(__location__ " failed to read interface flags for interface %s\n", ifr[i].ifr_name));
-                       goto finished;
-               }
+               s = socket(PF_INET6, SOCK_RAW, IPPROTO_RAW);
+               if (s == -1) {
+                       DEBUG(DEBUG_CRIT, (__location__ " Failed to open sending socket\n"));
+                       return -1;
 
-               /* was this ip tied to a loopback interface ? */
-               if (ifr[i].ifr_flags & IFF_LOOPBACK) {
-                       if (is_loopback != NULL) {
-                               *is_loopback = true;
-                       }
                }
+               /* sendto() don't like if the port is set and the socket is
+                  in raw mode.
+               */
+               tmpdest = discard_const(dest);
+               tmpport = tmpdest->ip6.sin6_port;
+
+               tmpdest->ip6.sin6_port = 0;
+               ret = sendto(s, &ip6pkt, sizeof(ip6pkt), 0,
+                            (const struct sockaddr *)&dest->ip6,
+                            sizeof(dest->ip6));
+               tmpdest->ip6.sin6_port = tmpport;
+               close(s);
 
-               if (ifname) {
-                       *ifname = talloc_asprintf(mem_ctx, "%s", ifr[i].ifr_name);
+               if (ret != sizeof(ip6pkt)) {
+                       DEBUG(DEBUG_CRIT,(__location__ " failed sendto (%s)\n", strerror(errno)));
+                       return -1;
                }
-
-               /* if we got this far, we have found our interface so we can
-                  exit the loop.
-               */              
                break;
-       }       
 
-finished:
-       talloc_free(ifr);
-       close(s);
-       return ret == 0;
+       default:
+               DEBUG(DEBUG_CRIT,(__location__ " not an ipv4/v6 address\n"));
+               return -1;
+       }
+
+       return 0;
 }
 
 /* 
@@ -349,10 +456,12 @@ int ctdb_sys_open_capture_socket(const char *iface, void **private_data)
        /* Open a socket to capture all traffic */
        s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
        if (s == -1) {
-               DEBUG(0,(__location__ " failed to open raw socket\n"));
+               DEBUG(DEBUG_CRIT,(__location__ " failed to open raw socket\n"));
                return -1;
        }
 
+       DEBUG(DEBUG_DEBUG, (__location__ " Created RAW SOCKET FD:%d for tcp tickle\n", s));
+
        set_nonblocking(s);
        set_close_on_exec(s);
 
@@ -369,40 +478,12 @@ int ctdb_sys_close_capture_socket(void *private_data)
        return 0;
 }
 
-/* 
-   This function is used to open a raw socket to send tickles from
- */
-int ctdb_sys_open_sending_socket(void)
-{
-       int s, ret;
-       uint32_t one = 1;
-
-       s = socket(AF_INET, SOCK_RAW, htons(IPPROTO_RAW));
-       if (s == -1) {
-               DEBUG(0,(__location__ " failed to open raw socket (%s)\n",
-                        strerror(errno)));
-               return -1;
-       }
-
-       ret = setsockopt(s, SOL_IP, IP_HDRINCL, &one, sizeof(one));
-       if (ret != 0) {
-               DEBUG(0,(__location__ " failed to setup IP headers (%s)\n",
-                        strerror(errno)));
-               close(s);
-               return -1;
-       }
-
-       set_nonblocking(s);
-       set_close_on_exec(s);
-
-       return s;
-}
 
 /*
   called when the raw socket becomes readable
  */
 int ctdb_sys_read_tcp_packet(int s, void *private_data, 
-                       struct sockaddr_in *src, struct sockaddr_in *dst,
+                       ctdb_sock_addr *src, ctdb_sock_addr *dst,
                        uint32_t *ack_seq, uint32_t *seq)
 {
        int ret;
@@ -410,6 +491,7 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data,
        char pkt[RCVPKTSIZE];
        struct ether_header *eth;
        struct iphdr *ip;
+       struct ip6_hdr *ip6;
        struct tcphdr *tcp;
 
        ret = recv(s, pkt, RCVPKTSIZE, MSG_TRUNC);
@@ -420,45 +502,104 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data,
        /* Ethernet */
        eth = (struct ether_header *)pkt;
 
-       /* We only want IP packets */
-       if (ntohs(eth->ether_type) != ETHERTYPE_IP) {
-               return -1;
-       }
-       
-       /* IP */
-       ip = (struct iphdr *)(eth+1);
+       /* we want either IPv4 or IPv6 */
+       if (ntohs(eth->ether_type) == ETHERTYPE_IP) {
+               /* IP */
+               ip = (struct iphdr *)(eth+1);
 
-       /* We only want IPv4 packets */
-       if (ip->version != 4) {
-               return -1;
-       }
-       /* Dont look at fragments */
-       if ((ntohs(ip->frag_off)&0x1fff) != 0) {
-               return -1;
-       }
-       /* we only want TCP */
-       if (ip->protocol != IPPROTO_TCP) {
-               return -1;
-       }
+               /* We only want IPv4 packets */
+               if (ip->version != 4) {
+                       return -1;
+               }
+               /* Dont look at fragments */
+               if ((ntohs(ip->frag_off)&0x1fff) != 0) {
+                       return -1;
+               }
+               /* we only want TCP */
+               if (ip->protocol != IPPROTO_TCP) {
+                       return -1;
+               }
 
-       /* make sure its not a short packet */
-       if (offsetof(struct tcphdr, ack_seq) + 4 + 
-           (ip->ihl*4) + sizeof(*eth) > ret) {
-               return -1;
-       }
+               /* make sure its not a short packet */
+               if (offsetof(struct tcphdr, ack_seq) + 4 + 
+                   (ip->ihl*4) + sizeof(*eth) > ret) {
+                       return -1;
+               }
+               /* TCP */
+               tcp = (struct tcphdr *)((ip->ihl*4) + (char *)ip);
+
+               /* tell the caller which one we've found */
+               src->ip.sin_family      = AF_INET;
+               src->ip.sin_addr.s_addr = ip->saddr;
+               src->ip.sin_port        = tcp->source;
+               dst->ip.sin_family      = AF_INET;
+               dst->ip.sin_addr.s_addr = ip->daddr;
+               dst->ip.sin_port        = tcp->dest;
+               *ack_seq                = tcp->ack_seq;
+               *seq                    = tcp->seq;
 
-       /* TCP */
-       tcp = (struct tcphdr *)((ip->ihl*4) + (char *)ip);
+               return 0;
+       } else if (ntohs(eth->ether_type) == ETHERTYPE_IP6) {
+               /* IP6 */
+               ip6 = (struct ip6_hdr *)(eth+1);
 
-       /* tell the caller which one we've found */
-       src->sin_addr.s_addr = ip->saddr;
-       src->sin_port        = tcp->source;
-       dst->sin_addr.s_addr = ip->daddr;
-       dst->sin_port        = tcp->dest;
-       *ack_seq             = tcp->ack_seq;
-       *seq                 = tcp->seq;
+               /* we only want TCP */
+               if (ip6->ip6_nxt != IPPROTO_TCP) {
+                       return -1;
+               }
 
-       return 0;
+               /* TCP */
+               tcp = (struct tcphdr *)(ip6+1);
+
+               /* tell the caller which one we've found */
+               src->ip6.sin6_family = AF_INET6;
+               src->ip6.sin6_port   = tcp->source;
+               src->ip6.sin6_addr   = ip6->ip6_src;
+
+               dst->ip6.sin6_family = AF_INET6;
+               dst->ip6.sin6_port   = tcp->dest;
+               dst->ip6.sin6_addr   = ip6->ip6_dst;
+
+               *ack_seq             = tcp->ack_seq;
+               *seq                 = tcp->seq;
+
+               return 0;
+       }
+
+       return -1;
 }
 
 
+bool ctdb_sys_check_iface_exists(const char *iface)
+{
+       int s;
+       struct ifreq ifr;
+
+       s = socket(PF_PACKET, SOCK_RAW, 0);
+       if (s == -1){
+               /* We don't know if the interface exists, so assume yes */
+               DEBUG(DEBUG_CRIT,(__location__ " failed to open raw socket\n"));
+               return true;
+       }
+
+       strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)-1);
+       if (ioctl(s, SIOCGIFINDEX, &ifr) < 0 && errno == ENODEV) {
+               DEBUG(DEBUG_CRIT,(__location__ " interface '%s' not found\n", iface));
+               close(s);
+               return false;
+       }
+       close(s);
+       
+       return true;
+}
+
+int ctdb_get_peer_pid(const int fd, pid_t *peer_pid)
+{
+       struct ucred cr;
+       socklen_t crl = sizeof(struct ucred);
+       int ret;
+       if ((ret = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &crl) == 0)) {
+               *peer_pid = cr.pid;
+       }
+       return ret;
+}