PUD: nwif: make setting ttl on tx socket work for IPv6
[olsrd.git] / lib / pud / src / networkInterfaces.c
index 9c91dc9..0fdbdce 100644 (file)
@@ -6,17 +6,10 @@
 #include "netTools.h"
 
 /* OLSRD includes */
-#include "olsr_cfg.h"
 #include "olsr.h"
-#include "interfaces.h"
 
 /* System includes */
-#include <assert.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <errno.h>
 #include <unistd.h>
-#include <stdlib.h>
 #include <fcntl.h>
 #include <ifaddrs.h>
 
 /** the MAC address of the main IP address */
 static unsigned char mac[PUD_NODEIDTYPE_MAC_BYTES] = { 0 };
 
+/** true when the MAC address of the main IP address has been retrieved */
+static bool macSet = false;
+
 /**
  * @return
  * the MAC address of the main IP address
  */
 unsigned char * getMainIpMacAddress(void) {
+       if (!macSet) {
+               struct ifreq ifr;
+               unsigned char * macInIfr;
+
+               struct interface *mainInterface = if_ifwithaddr(&olsr_cnf->main_addr);
+               if (!mainInterface) {
+                       pudError(true, "Could not get the main interface");
+                       return NULL;
+               }
+               macInIfr = getHardwareAddress(mainInterface->int_name, olsr_cnf->ip_version, &ifr);
+               if (!macInIfr) {
+                       pudError(true, "Could not get the MAC address of the main interface");
+                       return NULL;
+               }
+               memcpy(&mac[0], &macInIfr[0], PUD_NODEIDTYPE_MAC_BYTES);
+               macSet = true;
+       }
+
        return &mac[0];
 }
 
@@ -61,29 +75,37 @@ TRxTxNetworkInterface *getRxNetworkInterfaces(void) {
  information, except for the socket descriptor.
  @param rxSocketHandlerFunction
  The function that handles reception of data on the network interface
+ @param rxMcAddr
+ The receive multicast address (must have same IP version as networkInterface address)
 
  @return
  - the socket descriptor (>= 0)
  - -1 if an error occurred
  */
 static int createRxSocket(TRxTxNetworkInterface * networkInterface,
-               socket_handler_func rxSocketHandlerFunction) {
+               socket_handler_func rxSocketHandlerFunction, union olsr_sockaddr * rxMcAddr) {
        int ipFamilySetting;
        int ipProtoSetting;
        int ipMcLoopSetting;
        int ipAddMembershipSetting;
-       int socketReuseFlagValue = 1;
-       int mcLoopValue = 1;
+
        union olsr_sockaddr address;
+       struct sockaddr * addr;
+       size_t addrSize;
+
        int rxSocket = -1;
 
+       int socketReuseFlagValue = 1;
+       int mcLoopValue = 1;
+
        assert(networkInterface != NULL);
        assert(rxSocketHandlerFunction != NULL);
        assert(strncmp((char *) &networkInterface->name[0], "",
                                        sizeof(networkInterface->name)) != 0);
+       assert(networkInterface->ipAddress.in.sa_family == rxMcAddr->in.sa_family);
 
        memset(&address, 0, sizeof(address));
-       if (olsr_cnf->ip_version == AF_INET) {
+       if (networkInterface->ipAddress.in.sa_family == AF_INET) {
                assert(networkInterface->ipAddress.in4.sin_addr.s_addr != INADDR_ANY);
 
                ipFamilySetting = AF_INET;
@@ -94,6 +116,8 @@ static int createRxSocket(TRxTxNetworkInterface * networkInterface,
                address.in4.sin_family = ipFamilySetting;
                address.in4.sin_addr.s_addr = INADDR_ANY;
                address.in4.sin_port = getRxMcPort();
+               addr = (struct sockaddr *)&address.in4;
+               addrSize = sizeof(struct sockaddr_in);
        } else {
                assert(networkInterface->ipAddress.in6.sin6_addr.s6_addr != in6addr_any.s6_addr);
 
@@ -105,6 +129,8 @@ static int createRxSocket(TRxTxNetworkInterface * networkInterface,
                address.in6.sin6_family = ipFamilySetting;
                address.in6.sin6_addr = in6addr_any;
                address.in6.sin6_port = getRxMcPort();
+               addr = (struct sockaddr *)&address.in6;
+               addrSize = sizeof(struct sockaddr_in6);
        }
 
        /* Create a datagram socket on which to receive. */
@@ -129,7 +155,7 @@ static int createRxSocket(TRxTxNetworkInterface * networkInterface,
        /* Bind to the proper port number with the IP address INADDR_ANY
         * (INADDR_ANY is really required here, do not change it) */
        errno = 0;
-       if (bind(rxSocket, (struct sockaddr *) &address, sizeof(address)) < 0) {
+       if (bind(rxSocket, addr, addrSize) < 0) {
                pudError(true, "Could not bind the receive socket for interface"
                        " %s to port %u", networkInterface->name, ntohs(getRxMcPort()));
                goto bail;
@@ -151,7 +177,7 @@ static int createRxSocket(TRxTxNetworkInterface * networkInterface,
        if (ipFamilySetting == AF_INET) {
                struct ip_mreq mc_settings;
                (void) memset(&mc_settings, 0, sizeof(mc_settings));
-               mc_settings.imr_multiaddr = getRxMcAddr()->in4.sin_addr;
+               mc_settings.imr_multiaddr = rxMcAddr->in4.sin_addr;
                mc_settings.imr_interface = networkInterface->ipAddress.in4.sin_addr;
                errno = 0;
                if (setsockopt(rxSocket, ipProtoSetting, ipAddMembershipSetting,
@@ -163,8 +189,8 @@ static int createRxSocket(TRxTxNetworkInterface * networkInterface,
        } else {
                struct ipv6_mreq mc6_settings;
                (void) memset(&mc6_settings, 0, sizeof(mc6_settings));
-               mc6_settings.ipv6mr_multiaddr = getRxMcAddr()->in6.sin6_addr;
-               mc6_settings.ipv6mr_interface = 0;
+               mc6_settings.ipv6mr_multiaddr = rxMcAddr->in6.sin6_addr;
+               mc6_settings.ipv6mr_interface = if_nametoindex((char *)networkInterface->name);
                errno = 0;
                if (setsockopt(rxSocket, ipProtoSetting, ipAddMembershipSetting,
                                &mc6_settings, sizeof(mc6_settings)) < 0) {
@@ -196,13 +222,15 @@ static int createRxSocket(TRxTxNetworkInterface * networkInterface,
  the IP address of the interface
  @param rxSocketHandlerFunction
  the function that handles reception of data on the network interface
+ @param rxMcAddr
+ The receive multicast address (must have same IP version as ipAddr)
 
  @return
  - true on success
  - false on failure
  */
-static bool createRxInterface(const char * ifName, union olsr_sockaddr ipAddr,
-               socket_handler_func rxSocketHandlerFunction) {
+static bool createRxInterface(const char * ifName, union olsr_sockaddr ipAddr,
+               socket_handler_func rxSocketHandlerFunction, union olsr_sockaddr * rxMcAddr) {
        int socketFd = -1;
        TRxTxNetworkInterface * networkInterface = NULL;
 
@@ -218,12 +246,12 @@ static bool createRxInterface(const char * ifName, union olsr_sockaddr ipAddr,
 
        memcpy(networkInterface->name, ifName, sizeof(networkInterface->name));
        networkInterface->name[IFNAMSIZ] = '\0';
-       networkInterface->ipAddress = ipAddr;
+       networkInterface->ipAddress = *ipAddr;
        networkInterface->handler = NULL;
        networkInterface->next = NULL;
 
        /* networkInterface needs to be filled in when calling createRxSocket */
-       socketFd = createRxSocket(networkInterface, rxSocketHandlerFunction);
+       socketFd = createRxSocket(networkInterface, rxSocketHandlerFunction, rxMcAddr);
        if (socketFd < 0) {
                goto bail;
        }
@@ -282,23 +310,28 @@ static int createTxSocket(TRxTxNetworkInterface * networkInterface) {
        int ipProtoSetting;
        int ipMcLoopSetting;
        int ipMcIfSetting;
-       int mcLoopValue = 0;
-       unsigned char txTtl = getTxTtl();
+       int ipTtlSetting;
+
        union olsr_sockaddr address;
+
        int txSocket = -1;
 
+       int mcLoopValue = 0;
+       int txTtl = getTxTtl();
+
        assert(networkInterface != NULL);
        assert(strncmp((char *) &networkInterface->name[0], "",
                                        sizeof(networkInterface->name)) != 0);
 
        memset(&address, 0, sizeof(address));
-       if (olsr_cnf->ip_version == AF_INET) {
+       if (networkInterface->ipAddress.in.sa_family == AF_INET) {
                assert(networkInterface->ipAddress.in4.sin_addr.s_addr != INADDR_ANY);
 
                ipFamilySetting = AF_INET;
                ipProtoSetting = IPPROTO_IP;
                ipMcLoopSetting = IP_MULTICAST_LOOP;
                ipMcIfSetting = IP_MULTICAST_IF;
+               ipTtlSetting = IP_MULTICAST_TTL;
 
                address.in4.sin_family = ipFamilySetting;
                address.in4.sin_addr = networkInterface->ipAddress.in4.sin_addr;
@@ -310,6 +343,7 @@ static int createTxSocket(TRxTxNetworkInterface * networkInterface) {
                ipProtoSetting = IPPROTO_IPV6;
                ipMcLoopSetting = IPV6_MULTICAST_LOOP;
                ipMcIfSetting = IPV6_MULTICAST_IF;
+               ipTtlSetting = IPV6_MULTICAST_HOPS;
 
                address.in6.sin6_family = ipFamilySetting;
                address.in6.sin6_addr = networkInterface->ipAddress.in6.sin6_addr;
@@ -346,7 +380,7 @@ static int createTxSocket(TRxTxNetworkInterface * networkInterface) {
 
        /* Set the TTL on the socket */
        errno = 0;
-       if (setsockopt(txSocket, ipProtoSetting, IP_MULTICAST_TTL, &txTtl,
+       if (setsockopt(txSocket, ipProtoSetting, ipTtlSetting, &txTtl,
                        sizeof(txTtl)) < 0) {
                pudError(true, "Could not set TTL on the transmit socket"
                        " for interface %s", networkInterface->name);
@@ -382,7 +416,7 @@ static int createTxSocket(TRxTxNetworkInterface * networkInterface) {
  - true on success
  - false on failure
  */
-static bool createTxInterface(const char * ifName, union olsr_sockaddr ipAddr) {
+static bool createTxInterface(const char * ifName, union olsr_sockaddr ipAddr) {
        int socketFd = -1;
        TRxTxNetworkInterface * networkInterface = NULL;
 
@@ -398,7 +432,7 @@ static bool createTxInterface(const char * ifName, union olsr_sockaddr ipAddr) {
 
        memcpy(networkInterface->name, ifName, sizeof(networkInterface->name));
        networkInterface->name[IFNAMSIZ] = '\0';
-       networkInterface->ipAddress = ipAddr;
+       networkInterface->ipAddress = *ipAddr;
        networkInterface->handler = NULL;
        networkInterface->next = NULL;
 
@@ -427,78 +461,63 @@ static bool createTxInterface(const char * ifName, union olsr_sockaddr ipAddr) {
 }
 
 /*
- * Uplink interface
+ * Downlink interface
  */
 
-/** The socket fd, uplinking our NMEA sentences */
-static int uplinkSocketFd = -1;
+/** The socket fd, receiving downlinked messages */
+static int downlinkSocketFd = -1;
+
+/** the downlink handler function */
+static socket_handler_func downlinkHandler = NULL;
+
 
 /**
  @return
- The socket fd, uplinking our NMEA sentences. -1 when not valid.
+ The downlink socket fd. -1 when not valid.
  */
-int getUplinkSocketFd(void) {
-       return uplinkSocketFd;
+int getDownlinkSocketFd(void) {
+       return downlinkSocketFd;
 }
 
 /**
- Create an uplink socket
+ Create an downlink socket
+
+ @param ipVersion
+ The IP version (AF_INET or AF_INET6) for the socket
+ @param rxSocketHandlerFunction
+ The socket handler function
 
  @return
  - the socket descriptor (>= 0)
  - -1 if an error occurred
  */
-static int createUplinkSocket(void) {
-       int uplinkSocket = -1;
-
-       /*  Create a datagram socket on which to transmit */
-       errno = 0;
-       uplinkSocket = socket(olsr_cnf->ip_version, SOCK_DGRAM, 0);
-       if (uplinkSocket < 0) {
-               pudError(true, "Could not create the uplink socket");
-               goto bail;
-       }
+static int createDownlinkSocket(int ipVersion, socket_handler_func rxSocketHandlerFunction) {
+       union olsr_sockaddr address;
+       struct sockaddr * addr;
+       size_t addrSize;
 
-       /* Set the no delay option on the socket */
-       errno = 0;
-       if (fcntl(uplinkSocket, F_SETFL, O_NDELAY) < 0) {
-               pudError(true, "Could not set the no delay option on the uplink socket");
-               goto bail;
-       }
+       int downlinkSocket = -1;
 
-       return uplinkSocket;
+       int socketReuseFlagValue = 1;
 
-       bail: if (uplinkSocket >= 0) {
-               close(uplinkSocket);
+       memset(&address, 0, sizeof(address));
+       if (ipVersion == AF_INET) {
+               address.in4.sin_family = AF_INET;
+               address.in4.sin_addr.s_addr = INADDR_ANY;
+               address.in4.sin_port = getDownlinkPort();
+               addr = (struct sockaddr *)&address.in4;
+               addrSize = sizeof(struct sockaddr_in);
+       } else {
+               address.in6.sin6_family = AF_INET6;
+               address.in6.sin6_addr = in6addr_any;
+               address.in6.sin6_port = getDownlinkPort();
+               addr = (struct sockaddr *)&address.in6;
+               addrSize = sizeof(struct sockaddr_in6);
        }
-       return -1;
-}
-
-/*
- * Downlink interface
- */
-
-/** The socket fd, receiving downlinked messages */
-static int downlinkSocketFd = -1;
-
-/** the downlink handler function */
-static socket_handler_func downlinkHandler = NULL;
-
-/**
- Create an downlink socket
-
- @return
- - the socket descriptor (>= 0)
- - -1 if an error occurred
- */
-static int createDownlinkSocket(socket_handler_func rxSocketHandlerFunction) {
-       int downlinkSocket = -1;
-       int reuse = 1;
-       struct sockaddr_in address;
 
        /*  Create a datagram socket on which to receive */
        errno = 0;
-       downlinkSocket = socket(olsr_cnf->ip_version, SOCK_DGRAM, 0);
+       downlinkSocket = socket(ipVersion, SOCK_DGRAM, 0);
        if (downlinkSocket < 0) {
                pudError(true, "Could not create the downlink socket");
                goto bail;
@@ -507,22 +526,18 @@ static int createDownlinkSocket(socket_handler_func rxSocketHandlerFunction) {
        /* Enable SO_REUSEADDR to allow multiple applications to receive the same
         * messages */
        errno = 0;
-       if (setsockopt(downlinkSocket, SOL_SOCKET, SO_REUSEADDR, &reuse,
-                       sizeof(reuse)) < 0) {
+       if (setsockopt(downlinkSocket, SOL_SOCKET, SO_REUSEADDR, &socketReuseFlagValue,
+                       sizeof(socketReuseFlagValue)) < 0) {
                pudError(true, "Could not set REUSE option on the downlink socket");
                goto bail;
        }
 
-       /* Bind to the proper port number with the IP address INADDR_ANY (required) */
-       memset(&address, 0, sizeof(address));
-       address.sin_family = AF_INET;
-       address.sin_addr.s_addr = INADDR_ANY;
-       address.sin_port = getDownlinkPort();
-
+       /* Bind to the proper port number with the IP address INADDR_ANY
+        * (INADDR_ANY is really required here, do not change it) */
        errno = 0;
-       if (bind(downlinkSocket, (struct sockaddr *) &address,
-                       sizeof(address))) {
-               pudError(true, "Could not bind downlink socket to port %d", getDownlinkPort());
+       if (bind(downlinkSocket, addr, addrSize)) {
+               pudError(true, "Could not bind downlink socket to port %d",
+                               getDownlinkPort());
                goto bail;
        }
 
@@ -632,20 +647,8 @@ bool createNetworkInterfaces(socket_handler_func rxSocketHandlerFunction,
        int retval = false;
        struct ifaddrs *ifAddrs = NULL;
        struct ifaddrs *ifAddr = NULL;
-       struct ifreq ifr;
-       unsigned char * macInIfr;
-
-       struct interface *mainInterface = if_ifwithaddr(&olsr_cnf->main_addr);
-       if (!mainInterface) {
-               pudError(true, "Could not get the main interface");
-               return retval;
-       }
-       macInIfr = getHardwareAddress(mainInterface->int_name,olsr_cnf->ip_version,&ifr);
-       if (!macInIfr) {
-               pudError(true, "Could not get the MAC address of the main interface");
-               return retval;
-       }
-       memcpy(&mac[0], &macInIfr[0], PUD_NODEIDTYPE_MAC_BYTES);
+       union olsr_sockaddr * rxMcAddr = getRxMcAddr();
+       union olsr_sockaddr * txMcAddr = getTxMcAddr();
 
        errno = 0;
        if (getifaddrs(&ifAddrs) != 0) {
@@ -655,73 +658,59 @@ bool createNetworkInterfaces(socket_handler_func rxSocketHandlerFunction,
 
        /* loop over all interfaces */
        for (ifAddr = ifAddrs; ifAddr != NULL; ifAddr = ifAddr->ifa_next) {
-               struct sockaddr * addr = ifAddr->ifa_addr;
-               if (addr != NULL) {
-                       int addrFamily = addr->sa_family;
-                       if (addrFamily == olsr_cnf->ip_version) {
-                               char * ifName = ifAddr->ifa_name;
-                               union olsr_sockaddr ipAddr;
-
-                               /* determine whether the iterated interface is an OLSR
-                                * interface: returns NULL when the interface is not an
-                                * OLSR interface */
-                               struct interface *olsrIntf = if_ifwithname(ifName);
-                               bool isOlsrIf = (olsrIntf != NULL);
-
-                               /* determine whether the iterated interface is configured as a
-                                * non-OLSR interface in the plugin parameter list */
-                               bool isRxNonOlsrIf = isRxNonOlsrInterface(ifName);
-                               bool isTxNonOlsrIf = isTxNonOlsrInterface(ifName);
-                               bool isNonOlsrIf = isRxNonOlsrIf || isTxNonOlsrIf;
-
-                               if (!isOlsrIf && !isNonOlsrIf) {
-                                       /* Interface is not an OLSR interface AND interface is not
-                                        * configured as non-OLSR interface: skip */
-                                       continue;
-                               }
-
-                               if (isOlsrIf && !createOlsrInterface(olsrIntf)) {
-                                       /* creating an OLSR interface failed */
-                                       goto end;
-                               }
-
-                               if (!isNonOlsrIf) {
-                                       /* interface is not configured as non-OLSR interface: skip */
-                                       continue;
-                               }
-
-                               if (addrFamily == AF_INET) {
-                                       memcpy(&ipAddr.in4, addr, sizeof(struct sockaddr_in));
-                               } else {
-                                       memcpy(&ipAddr.in6, addr, sizeof(struct sockaddr_in6));
-                               }
-
-                               if (isRxNonOlsrIf && !createRxInterface(ifName, ipAddr,
-                                               rxSocketHandlerFunction)) {
-                                       /* creating a receive interface failed */
-                                       goto end;
-                               }
-
-                               if (isTxNonOlsrIf && !createTxInterface(ifName, ipAddr)) {
-                                       /* creating a transmit interface failed */
-                                       goto end;
-                               }
+               union olsr_sockaddr * addr = (union olsr_sockaddr *)ifAddr->ifa_addr;
+               if ((addr != NULL) && ((addr->in.sa_family == AF_INET) || (addr->in.sa_family == AF_INET6))) {
+                       char * ifName = ifAddr->ifa_name;
+
+                       struct interface * olsrIntf = ((addr->in.sa_family != olsr_cnf->ip_version) ?
+                                       NULL :
+                                       if_ifwithaddr((addr->in.sa_family == AF_INET) ?
+                                                       (union olsr_ip_addr *)&addr->in4.sin_addr :
+                                                       (union olsr_ip_addr *)&addr->in6.sin6_addr));
+                       bool isOlsrIf = (olsrIntf != NULL);
+
+                       /* determine whether the iterated interface is configured as a
+                        * non-OLSR interface in the plugin parameter list */
+                       bool isRxNonOlsrIf = isRxNonOlsrInterface(ifName) && (addr->in.sa_family == rxMcAddr->in.sa_family);
+                       bool isTxNonOlsrIf = isTxNonOlsrInterface(ifName) && (addr->in.sa_family == txMcAddr->in.sa_family);
+
+                       bool isNonOlsrIf = isRxNonOlsrIf || isTxNonOlsrIf;
+
+                       if (!isOlsrIf && !isNonOlsrIf) {
+                               /* Interface is not an OLSR interface AND interface is not
+                                * configured as non-OLSR interface: skip */
+                               continue;
+                       }
+
+                       if (isOlsrIf && !createOlsrInterface(olsrIntf)) {
+                               /* creating an OLSR interface failed */
+                               goto end;
+                       }
+
+                       if (!isNonOlsrIf) {
+                               /* interface is not configured as non-OLSR interface: skip */
+                               continue;
+                       }
+
+                       if (isRxNonOlsrIf && !createRxInterface(ifName, addr, rxSocketHandlerFunction, rxMcAddr)) {
+                               /* creating a receive interface failed */
+                               goto end;
+                       }
+
+                       if (isTxNonOlsrIf && !createTxInterface(ifName, addr)) {
+                               /* creating a transmit interface failed */
+                               goto end;
                        }
                }
        }
 
        if (isUplinkAddrSet()) {
-               uplinkSocketFd = createUplinkSocket();
-               if (uplinkSocketFd == -1) {
-                       goto end;
-               }
-
-               downlinkSocketFd = createDownlinkSocket(rxSocketHandlerFunctionDownlink);
+               downlinkSocketFd = createDownlinkSocket(getUplinkAddr()->in.sa_family, rxSocketHandlerFunctionDownlink);
                if (downlinkSocketFd == -1) {
                        goto end;
                }
        } else {
-               uplinkSocketFd = -1;
+               downlinkSocketFd = -1;
        }
 
        retval = true;
@@ -789,11 +778,6 @@ void closeNetworkInterfaces(void) {
                olsrNetworkInterfacesListHead = NULL;
        }
 
-       if (uplinkSocketFd != -1 ) {
-               close(uplinkSocketFd);
-               uplinkSocketFd = -1;
-       }
-
        if (downlinkSocketFd != -1 ) {
                if (downlinkHandler) {
                        remove_olsr_socket (downlinkSocketFd, downlinkHandler, NULL);