Move platform-specific code to common/system_*
[ctdb.git] / common / system_linux.c
index a771e3ba04f377697bac8b4db93d79b8a9378b49..cb26dcd654bf676693e585455f624c8c6e6d480f 100644 (file)
 #include "system/filesys.h"
 #include "system/wait.h"
 #include "../include/ctdb_private.h"
-#include "lib/events/events.h"
+#include "lib/tevent/tevent.h"
 #include <netinet/if_ether.h>
 #include <netinet/ip6.h>
 #include <netinet/icmp6.h>
 #include <net/if_arp.h>
-
+#include <netpacket/packet.h>
 
 #ifndef ETHERTYPE_IP6
 #define ETHERTYPE_IP6 0x86dd
@@ -71,7 +71,7 @@ static uint16_t tcp_checksum6(uint16_t *data, size_t n, struct ip6_hdr *ip6)
 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;
@@ -79,17 +79,27 @@ int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface)
        struct ifreq if_hwaddr;
        unsigned char buffer[78]; /* ipv6 neigh solicitation size */
        char *ptr;
+       char bdcast[] = {0xff,0xff,0xff,0xff,0xff,0xff};
+       struct ifreq ifr;
 
-       ZERO_STRUCT(sa);
+       ZERO_STRUCT(sall);
 
        switch (addr->ip.sin_family) {
        case AF_INET:
-               s = socket(AF_INET, SOCK_PACKET, htons(ETHERTYPE_ARP));
+               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;
+               }
+
                /* get the mac address */
                strcpy(if_hwaddr.ifr_name, iface);
                ret = ioctl(s, SIOCGIFHWADDR, &if_hwaddr);
@@ -136,8 +146,12 @@ int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface)
                memcpy(ptr, &addr->ip.sin_addr, 4);       
                ptr+=4;
        
-               strncpy(sa.sa_data, iface, sizeof(sa.sa_data));
-               ret = sendto(s, buffer, 64, 0, &sa, sizeof(sa));
+               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"));
@@ -156,22 +170,30 @@ int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface)
                memcpy(ptr, &addr->ip.sin_addr, 4);       
                ptr+=4;
 
-               strncpy(sa.sa_data, iface, sizeof(sa.sa_data));
-               ret = sendto(s, buffer, 64, 0, &sa, sizeof(sa));
+               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);
                break;
        case AF_INET6:
-               s = socket(AF_INET, SOCK_PACKET, htons(ETHERTYPE_IP6));
+               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;
+               }
+
                /* get the mac address */
                strcpy(if_hwaddr.ifr_name, iface);
                ret = ioctl(s, SIOCGIFHWADDR, &if_hwaddr);
@@ -213,8 +235,12 @@ int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface)
 
                icmp6->icmp6_cksum = tcp_checksum6((uint16_t *)icmp6, ntohs(ip6->ip6_plen), ip6);
 
-               strncpy(sa.sa_data, iface, sizeof(sa.sa_data));
-               ret = sendto(s, buffer, 78, 0, &sa, sizeof(sa));
+               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, 78, 0, (struct sockaddr *)&sall, sizeof(sall));
                if (ret < 0 ){
                        close(s);
                        DEBUG(DEBUG_CRIT,(__location__ " failed sendto\n"));
@@ -327,7 +353,9 @@ int ctdb_sys_send_tcp(const ctdb_sock_addr *dest,
                set_nonblocking(s);
                set_close_on_exec(s);
 
-               ret = sendto(s, &ip4pkt, sizeof(ip4pkt), 0, &dest->ip, sizeof(dest->ip));
+               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)));
@@ -369,7 +397,9 @@ int ctdb_sys_send_tcp(const ctdb_sock_addr *dest,
                tmpport = tmpdest->ip6.sin6_port;
 
                tmpdest->ip6.sin6_port = 0;
-               ret = sendto(s, &ip6pkt, sizeof(ip6pkt), 0, &dest->ip6, sizeof(dest->ip6));
+               ret = sendto(s, &ip6pkt, sizeof(ip6pkt), 0,
+                            (const struct sockaddr *)&dest->ip6,
+                            sizeof(dest->ip6));
                tmpdest->ip6.sin6_port = tmpport;
                close(s);
 
@@ -401,6 +431,8 @@ int ctdb_sys_open_capture_socket(const char *iface, void **private_data)
                return -1;
        }
 
+       DEBUG(DEBUG_DEBUG, (__location__ " Created RAW SOCKET FD:%d for tcp tickle\n", s));
+
        set_nonblocking(s);
        set_close_on_exec(s);
 
@@ -509,3 +541,37 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data,
 }
 
 
+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 dont 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));
+       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;
+}
+