PUD: nwif: make setting ttl on tx socket work for IPv6
[olsrd.git] / lib / pud / src / networkInterfaces.c
1 #include "networkInterfaces.h"
2
3 /* Plugin includes */
4 #include "pud.h"
5 #include "configuration.h"
6 #include "netTools.h"
7
8 /* OLSRD includes */
9 #include "olsr.h"
10
11 /* System includes */
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <ifaddrs.h>
15
16 /*
17  * Main IP MAC address
18  */
19
20 /** the MAC address of the main IP address */
21 static unsigned char mac[PUD_NODEIDTYPE_MAC_BYTES] = { 0 };
22
23 /** true when the MAC address of the main IP address has been retrieved */
24 static bool macSet = false;
25
26 /**
27  * @return
28  * the MAC address of the main IP address
29  */
30 unsigned char * getMainIpMacAddress(void) {
31         if (!macSet) {
32                 struct ifreq ifr;
33                 unsigned char * macInIfr;
34
35                 struct interface *mainInterface = if_ifwithaddr(&olsr_cnf->main_addr);
36                 if (!mainInterface) {
37                         pudError(true, "Could not get the main interface");
38                         return NULL;
39                 }
40                 macInIfr = getHardwareAddress(mainInterface->int_name, olsr_cnf->ip_version, &ifr);
41                 if (!macInIfr) {
42                         pudError(true, "Could not get the MAC address of the main interface");
43                         return NULL;
44                 }
45                 memcpy(&mac[0], &macInIfr[0], PUD_NODEIDTYPE_MAC_BYTES);
46                 macSet = true;
47         }
48
49         return &mac[0];
50 }
51
52 /*
53  * RX interfaces
54  */
55
56 /** The list of network interface objects, receiving GPS NMEA sentences */
57 static TRxTxNetworkInterface *rxNetworkInterfacesListHead = NULL;
58
59 /** Pointer to the last network interface object, receiving GPS NMEA sentences */
60 static TRxTxNetworkInterface *lastRxNetworkInterface = NULL;
61
62 /**
63  @return
64  The list of network interface objects, receiving GPS NMEA sentences
65  */
66 TRxTxNetworkInterface *getRxNetworkInterfaces(void) {
67         return rxNetworkInterfacesListHead;
68 }
69
70 /**
71  Create a receive socket for a network interface
72
73  @param networkInterface
74  The network interface object. This function expects it to be filled with all
75  information, except for the socket descriptor.
76  @param rxSocketHandlerFunction
77  The function that handles reception of data on the network interface
78  @param rxMcAddr
79  The receive multicast address (must have same IP version as networkInterface address)
80
81  @return
82  - the socket descriptor (>= 0)
83  - -1 if an error occurred
84  */
85 static int createRxSocket(TRxTxNetworkInterface * networkInterface,
86                 socket_handler_func rxSocketHandlerFunction, union olsr_sockaddr * rxMcAddr) {
87         int ipFamilySetting;
88         int ipProtoSetting;
89         int ipMcLoopSetting;
90         int ipAddMembershipSetting;
91
92         union olsr_sockaddr address;
93         struct sockaddr * addr;
94         size_t addrSize;
95
96         int rxSocket = -1;
97
98         int socketReuseFlagValue = 1;
99         int mcLoopValue = 1;
100
101         assert(networkInterface != NULL);
102         assert(rxSocketHandlerFunction != NULL);
103         assert(strncmp((char *) &networkInterface->name[0], "",
104                                         sizeof(networkInterface->name)) != 0);
105         assert(networkInterface->ipAddress.in.sa_family == rxMcAddr->in.sa_family);
106
107         memset(&address, 0, sizeof(address));
108         if (networkInterface->ipAddress.in.sa_family == AF_INET) {
109                 assert(networkInterface->ipAddress.in4.sin_addr.s_addr != INADDR_ANY);
110
111                 ipFamilySetting = AF_INET;
112                 ipProtoSetting = IPPROTO_IP;
113                 ipMcLoopSetting = IP_MULTICAST_LOOP;
114                 ipAddMembershipSetting = IP_ADD_MEMBERSHIP;
115
116                 address.in4.sin_family = ipFamilySetting;
117                 address.in4.sin_addr.s_addr = INADDR_ANY;
118                 address.in4.sin_port = getRxMcPort();
119                 addr = (struct sockaddr *)&address.in4;
120                 addrSize = sizeof(struct sockaddr_in);
121         } else {
122                 assert(networkInterface->ipAddress.in6.sin6_addr.s6_addr != in6addr_any.s6_addr);
123
124                 ipFamilySetting = AF_INET6;
125                 ipProtoSetting = IPPROTO_IPV6;
126                 ipMcLoopSetting = IPV6_MULTICAST_LOOP;
127                 ipAddMembershipSetting = IPV6_ADD_MEMBERSHIP;
128
129                 address.in6.sin6_family = ipFamilySetting;
130                 address.in6.sin6_addr = in6addr_any;
131                 address.in6.sin6_port = getRxMcPort();
132                 addr = (struct sockaddr *)&address.in6;
133                 addrSize = sizeof(struct sockaddr_in6);
134         }
135
136         /* Create a datagram socket on which to receive. */
137         errno = 0;
138         rxSocket = socket(ipFamilySetting, SOCK_DGRAM, 0);
139         if (rxSocket < 0) {
140                 pudError(true, "Could not create a receive socket for interface %s",
141                                 networkInterface->name);
142                 goto bail;
143         }
144
145         /* Enable SO_REUSEADDR to allow multiple applications to receive the same
146          * multicast messages */
147         errno = 0;
148         if (setsockopt(rxSocket, SOL_SOCKET, SO_REUSEADDR, &socketReuseFlagValue,
149                         sizeof(socketReuseFlagValue)) < 0) {
150                 pudError(true, "Could not set the reuse flag on the receive socket for"
151                         " interface %s", networkInterface->name);
152                 goto bail;
153         }
154
155         /* Bind to the proper port number with the IP address INADDR_ANY
156          * (INADDR_ANY is really required here, do not change it) */
157         errno = 0;
158         if (bind(rxSocket, addr, addrSize) < 0) {
159                 pudError(true, "Could not bind the receive socket for interface"
160                         " %s to port %u", networkInterface->name, ntohs(getRxMcPort()));
161                 goto bail;
162         }
163
164         /* Enable multicast local loopback */
165         errno = 0;
166         if (setsockopt(rxSocket, ipProtoSetting, ipMcLoopSetting, &mcLoopValue,
167                         sizeof(mcLoopValue)) < 0) {
168                 pudError(true, "Could not %s multicast loopback on the"
169                         " receive socket for interface %s", mcLoopValue ? "enable"
170                                 : "disable", networkInterface->name);
171                 goto bail;
172         }
173
174         /* Join the multicast group on the local interface. Note that this
175          * ADD_MEMBERSHIP option must be called for each local interface over
176          * which the multicast datagrams are to be received. */
177         if (ipFamilySetting == AF_INET) {
178                 struct ip_mreq mc_settings;
179                 (void) memset(&mc_settings, 0, sizeof(mc_settings));
180                 mc_settings.imr_multiaddr = rxMcAddr->in4.sin_addr;
181                 mc_settings.imr_interface = networkInterface->ipAddress.in4.sin_addr;
182                 errno = 0;
183                 if (setsockopt(rxSocket, ipProtoSetting, ipAddMembershipSetting,
184                                 &mc_settings, sizeof(mc_settings)) < 0) {
185                         pudError(true, "Could not subscribe interface %s to the configured"
186                                 " multicast group", networkInterface->name);
187                         goto bail;
188                 }
189         } else {
190                 struct ipv6_mreq mc6_settings;
191                 (void) memset(&mc6_settings, 0, sizeof(mc6_settings));
192                 mc6_settings.ipv6mr_multiaddr = rxMcAddr->in6.sin6_addr;
193                 mc6_settings.ipv6mr_interface = if_nametoindex((char *)networkInterface->name);
194                 errno = 0;
195                 if (setsockopt(rxSocket, ipProtoSetting, ipAddMembershipSetting,
196                                 &mc6_settings, sizeof(mc6_settings)) < 0) {
197                         pudError(true, "Could not subscribe interface %s to the configured"
198                                 " multicast group", networkInterface->name);
199                         goto bail;
200                 }
201         }
202
203         add_olsr_socket(rxSocket, rxSocketHandlerFunction, NULL, networkInterface,
204                         SP_PR_READ);
205
206         return rxSocket;
207
208         bail: if (rxSocket >= 0) {
209                 close(rxSocket);
210         }
211         return -1;
212
213 }
214
215 /**
216  Create a receive interface and add it to the list of receive network interface
217  objects
218
219  @param ifName
220  the network interface name
221  @param ipAddr
222  the IP address of the interface
223  @param rxSocketHandlerFunction
224  the function that handles reception of data on the network interface
225  @param rxMcAddr
226  The receive multicast address (must have same IP version as ipAddr)
227
228  @return
229  - true on success
230  - false on failure
231  */
232 static bool createRxInterface(const char * ifName, union olsr_sockaddr * ipAddr,
233                 socket_handler_func rxSocketHandlerFunction, union olsr_sockaddr * rxMcAddr) {
234         int socketFd = -1;
235         TRxTxNetworkInterface * networkInterface = NULL;
236
237         if (ifName == NULL) {
238                 goto bail;
239         }
240
241         networkInterface = olsr_malloc(sizeof(TRxTxNetworkInterface),
242                         "TRxTxNetworkInterface (PUD)");
243         if (networkInterface == NULL) {
244                 goto bail;
245         }
246
247         memcpy(networkInterface->name, ifName, sizeof(networkInterface->name));
248         networkInterface->name[IFNAMSIZ] = '\0';
249         networkInterface->ipAddress = *ipAddr;
250         networkInterface->handler = NULL;
251         networkInterface->next = NULL;
252
253         /* networkInterface needs to be filled in when calling createRxSocket */
254         socketFd = createRxSocket(networkInterface, rxSocketHandlerFunction, rxMcAddr);
255         if (socketFd < 0) {
256                 goto bail;
257         }
258         networkInterface->socketFd = socketFd;
259         networkInterface->handler = rxSocketHandlerFunction;
260
261         /* Add new object to the end of the global list. */
262         if (rxNetworkInterfacesListHead == NULL) {
263                 rxNetworkInterfacesListHead = networkInterface;
264                 lastRxNetworkInterface = networkInterface;
265         } else {
266                 lastRxNetworkInterface->next = networkInterface;
267                 lastRxNetworkInterface = networkInterface;
268         }
269
270         return true;
271
272         bail: if (networkInterface != NULL) {
273                 free(networkInterface);
274         }
275         return false;
276
277 }
278
279 /*
280  * TX interfaces
281  */
282
283 /** The list of network interface objects, sending our NMEA sentences */
284 static TRxTxNetworkInterface *txNetworkInterfacesListHead = NULL;
285
286 /** Pointer to the last network interface object, sending our NMEA sentences */
287 static TRxTxNetworkInterface *lastTxNetworkInterface = NULL;
288
289 /**
290  @return
291  The list of network interface objects, sending our NMEA sentences
292  */
293 TRxTxNetworkInterface *getTxNetworkInterfaces(void) {
294         return txNetworkInterfacesListHead;
295 }
296
297 /**
298  Create a transmit socket for a network interface
299
300  @param networkInterface
301  The network interface object. This function expects it to be filled with all
302  information, except for the socket descriptor.
303
304  @return
305  - the socket descriptor (>= 0)
306  - -1 if an error occurred
307  */
308 static int createTxSocket(TRxTxNetworkInterface * networkInterface) {
309         int ipFamilySetting;
310         int ipProtoSetting;
311         int ipMcLoopSetting;
312         int ipMcIfSetting;
313         int ipTtlSetting;
314
315         union olsr_sockaddr address;
316
317         int txSocket = -1;
318
319         int mcLoopValue = 0;
320         int txTtl = getTxTtl();
321
322         assert(networkInterface != NULL);
323         assert(strncmp((char *) &networkInterface->name[0], "",
324                                         sizeof(networkInterface->name)) != 0);
325
326         memset(&address, 0, sizeof(address));
327         if (networkInterface->ipAddress.in.sa_family == AF_INET) {
328                 assert(networkInterface->ipAddress.in4.sin_addr.s_addr != INADDR_ANY);
329
330                 ipFamilySetting = AF_INET;
331                 ipProtoSetting = IPPROTO_IP;
332                 ipMcLoopSetting = IP_MULTICAST_LOOP;
333                 ipMcIfSetting = IP_MULTICAST_IF;
334                 ipTtlSetting = IP_MULTICAST_TTL;
335
336                 address.in4.sin_family = ipFamilySetting;
337                 address.in4.sin_addr = networkInterface->ipAddress.in4.sin_addr;
338                 address.in4.sin_port = getTxMcPort();
339         } else {
340                 assert(networkInterface->ipAddress.in6.sin6_addr.s6_addr != in6addr_any.s6_addr);
341
342                 ipFamilySetting = AF_INET6;
343                 ipProtoSetting = IPPROTO_IPV6;
344                 ipMcLoopSetting = IPV6_MULTICAST_LOOP;
345                 ipMcIfSetting = IPV6_MULTICAST_IF;
346                 ipTtlSetting = IPV6_MULTICAST_HOPS;
347
348                 address.in6.sin6_family = ipFamilySetting;
349                 address.in6.sin6_addr = networkInterface->ipAddress.in6.sin6_addr;
350                 address.in6.sin6_port = getTxMcPort();
351         }
352
353         /*  Create a datagram socket on which to transmit */
354         errno = 0;
355         txSocket = socket(ipFamilySetting, SOCK_DGRAM, 0);
356         if (txSocket < 0) {
357                 pudError(true, "Could not create a transmit socket for interface %s",
358                                 networkInterface->name);
359                 goto bail;
360         }
361
362         /* Bind the socket to the desired interface and port */
363         errno = 0;
364         if (setsockopt(txSocket, ipProtoSetting, ipMcIfSetting, &address,
365                         sizeof(address)) < 0) {
366                 pudError(true, "Could not set the multicast interface on the"
367                         " transmit socket to interface %s", networkInterface->name);
368                 goto bail;
369         }
370
371         /* Disable multicast local loopback */
372         errno = 0;
373         if (setsockopt(txSocket, ipProtoSetting, ipMcLoopSetting, &mcLoopValue,
374                         sizeof(mcLoopValue)) < 0) {
375                 pudError(true, "Could not %s multicast loopback on the"
376                         " transmit socket for interface %s", mcLoopValue ? "enable"
377                                 : "disable", networkInterface->name);
378                 goto bail;
379         }
380
381         /* Set the TTL on the socket */
382         errno = 0;
383         if (setsockopt(txSocket, ipProtoSetting, ipTtlSetting, &txTtl,
384                         sizeof(txTtl)) < 0) {
385                 pudError(true, "Could not set TTL on the transmit socket"
386                         " for interface %s", networkInterface->name);
387                 goto bail;
388         }
389
390         /* Set the no delay option on the socket */
391         errno = 0;
392         if (fcntl(txSocket, F_SETFL, O_NDELAY) < 0) {
393                 pudError(true, "Could not set the no delay option on the"
394                         " transmit socket for interface %s", networkInterface->name);
395                 goto bail;
396         }
397
398         return txSocket;
399
400         bail: if (txSocket >= 0) {
401                 close(txSocket);
402         }
403         return -1;
404 }
405
406 /**
407  Create a transmit interface and add it to the list of transmit network
408  interface objects
409
410  @param ifName
411  the network interface name
412  @param ipAddr
413  the IP address of the interface
414
415  @return
416  - true on success
417  - false on failure
418  */
419 static bool createTxInterface(const char * ifName, union olsr_sockaddr * ipAddr) {
420         int socketFd = -1;
421         TRxTxNetworkInterface * networkInterface = NULL;
422
423         if (ifName == NULL) {
424                 goto bail;
425         }
426
427         networkInterface = olsr_malloc(sizeof(TRxTxNetworkInterface),
428                         "TRxTxNetworkInterface (PUD)");
429         if (networkInterface == NULL) {
430                 goto bail;
431         }
432
433         memcpy(networkInterface->name, ifName, sizeof(networkInterface->name));
434         networkInterface->name[IFNAMSIZ] = '\0';
435         networkInterface->ipAddress = *ipAddr;
436         networkInterface->handler = NULL;
437         networkInterface->next = NULL;
438
439         /* networkInterface needs to be filled in when calling createTxSocket */
440         socketFd = createTxSocket(networkInterface);
441         if (socketFd < 0) {
442                 goto bail;
443         }
444         networkInterface->socketFd = socketFd;
445
446         /* Add new object to the end of the global list. */
447         if (txNetworkInterfacesListHead == NULL) {
448                 txNetworkInterfacesListHead = networkInterface;
449                 lastTxNetworkInterface = networkInterface;
450         } else {
451                 lastTxNetworkInterface->next = networkInterface;
452                 lastTxNetworkInterface = networkInterface;
453         }
454
455         return true;
456
457         bail: if (networkInterface != NULL) {
458                 free(networkInterface);
459         }
460         return false;
461 }
462
463 /*
464  * Downlink interface
465  */
466
467 /** The socket fd, receiving downlinked messages */
468 static int downlinkSocketFd = -1;
469
470 /** the downlink handler function */
471 static socket_handler_func downlinkHandler = NULL;
472
473
474 /**
475  @return
476  The downlink socket fd. -1 when not valid.
477  */
478 int getDownlinkSocketFd(void) {
479         return downlinkSocketFd;
480 }
481
482 /**
483  Create an downlink socket
484
485  @param ipVersion
486  The IP version (AF_INET or AF_INET6) for the socket
487  @param rxSocketHandlerFunction
488  The socket handler function
489
490  @return
491  - the socket descriptor (>= 0)
492  - -1 if an error occurred
493  */
494 static int createDownlinkSocket(int ipVersion, socket_handler_func rxSocketHandlerFunction) {
495         union olsr_sockaddr address;
496         struct sockaddr * addr;
497         size_t addrSize;
498
499         int downlinkSocket = -1;
500
501         int socketReuseFlagValue = 1;
502
503         memset(&address, 0, sizeof(address));
504         if (ipVersion == AF_INET) {
505                 address.in4.sin_family = AF_INET;
506                 address.in4.sin_addr.s_addr = INADDR_ANY;
507                 address.in4.sin_port = getDownlinkPort();
508                 addr = (struct sockaddr *)&address.in4;
509                 addrSize = sizeof(struct sockaddr_in);
510         } else {
511                 address.in6.sin6_family = AF_INET6;
512                 address.in6.sin6_addr = in6addr_any;
513                 address.in6.sin6_port = getDownlinkPort();
514                 addr = (struct sockaddr *)&address.in6;
515                 addrSize = sizeof(struct sockaddr_in6);
516         }
517
518         /*  Create a datagram socket on which to receive */
519         errno = 0;
520         downlinkSocket = socket(ipVersion, SOCK_DGRAM, 0);
521         if (downlinkSocket < 0) {
522                 pudError(true, "Could not create the downlink socket");
523                 goto bail;
524         }
525
526         /* Enable SO_REUSEADDR to allow multiple applications to receive the same
527          * messages */
528         errno = 0;
529         if (setsockopt(downlinkSocket, SOL_SOCKET, SO_REUSEADDR, &socketReuseFlagValue,
530                         sizeof(socketReuseFlagValue)) < 0) {
531                 pudError(true, "Could not set REUSE option on the downlink socket");
532                 goto bail;
533         }
534
535         /* Bind to the proper port number with the IP address INADDR_ANY
536          * (INADDR_ANY is really required here, do not change it) */
537         errno = 0;
538         if (bind(downlinkSocket, addr, addrSize)) {
539                 pudError(true, "Could not bind downlink socket to port %d",
540                                 getDownlinkPort());
541                 goto bail;
542         }
543
544         add_olsr_socket(downlinkSocket, rxSocketHandlerFunction, NULL, NULL,
545                         SP_PR_READ);
546
547         downlinkHandler = rxSocketHandlerFunction;
548
549         return downlinkSocket;
550
551         bail: if (downlinkSocket >= 0) {
552                 close(downlinkSocket);
553         }
554         return -1;
555 }
556
557 /*
558  * OLSR interfaces
559  */
560
561 /** The list of OLSR network interface objects */
562 static TOLSRNetworkInterface *olsrNetworkInterfacesListHead = NULL;
563
564 /** Pointer to the last OLSR network interface object */
565 static TOLSRNetworkInterface *lastOlsrNetworkInterface = NULL;
566
567 /**
568  Get the OLSR interface structure for a certain OLSR interface. Note that
569  pointer comparison is performed to compare the OLSR interfaces.
570
571  @param olsrIntf
572  a pointer to an OLSR interface
573
574  @return
575  - a pointer to the OLSR interface structure
576  - NULL when not found
577  */
578 TOLSRNetworkInterface * getOlsrNetworkInterface(struct interface *olsrIntf) {
579         TOLSRNetworkInterface * retval = olsrNetworkInterfacesListHead;
580
581         while ((retval->olsrIntf != olsrIntf) && (retval != NULL)) {
582                 retval = retval->next;
583         }
584
585         return retval;
586 }
587
588 /**
589  Create an OLSR interface and add it to the list of OLSR network interface
590  objects
591
592  @param olsrIntf
593  a pointer to the OLSR interface
594
595  @return
596  - true on success
597  - false on failure
598  */
599 static int createOlsrInterface(struct interface *olsrIntf) {
600         TOLSRNetworkInterface * networkInterface = NULL;
601
602         networkInterface = olsr_malloc(sizeof(TOLSRNetworkInterface),
603                         "TOLSRNetworkInterface (PUD)");
604         if (networkInterface == NULL) {
605                 goto bail;
606         }
607
608         networkInterface->olsrIntf = olsrIntf;
609         networkInterface->next = NULL;
610
611         /* Add new object to the end of the global list. */
612         if (olsrNetworkInterfacesListHead == NULL) {
613                 olsrNetworkInterfacesListHead = networkInterface;
614                 lastOlsrNetworkInterface = networkInterface;
615         } else {
616                 lastOlsrNetworkInterface->next = networkInterface;
617                 lastOlsrNetworkInterface = networkInterface;
618         }
619
620         return true;
621
622         bail: if (networkInterface != NULL) {
623                 free(networkInterface);
624         }
625         return false;
626 }
627
628 /*
629  * Interface Functions
630  */
631
632 /**
633  Creates receive and transmit sockets and register the receive sockets with
634  the OLSR stack
635
636  @param rxSocketHandlerFunction
637  The function to call upon reception of data on a receive socket
638  @param rxSocketHandlerFunctionDownlink
639  The function to call upon reception of data on a downlink receive socket
640
641  @return
642  - true on success
643  - false on failure
644  */
645 bool createNetworkInterfaces(socket_handler_func rxSocketHandlerFunction,
646                 socket_handler_func rxSocketHandlerFunctionDownlink) {
647         int retval = false;
648         struct ifaddrs *ifAddrs = NULL;
649         struct ifaddrs *ifAddr = NULL;
650         union olsr_sockaddr * rxMcAddr = getRxMcAddr();
651         union olsr_sockaddr * txMcAddr = getTxMcAddr();
652
653         errno = 0;
654         if (getifaddrs(&ifAddrs) != 0) {
655                 pudError(true, "Could not get list of interfaces and their addresses");
656                 return retval;
657         }
658
659         /* loop over all interfaces */
660         for (ifAddr = ifAddrs; ifAddr != NULL; ifAddr = ifAddr->ifa_next) {
661                 union olsr_sockaddr * addr = (union olsr_sockaddr *)ifAddr->ifa_addr;
662                 if ((addr != NULL) && ((addr->in.sa_family == AF_INET) || (addr->in.sa_family == AF_INET6))) {
663                         char * ifName = ifAddr->ifa_name;
664
665                         struct interface * olsrIntf = ((addr->in.sa_family != olsr_cnf->ip_version) ?
666                                         NULL :
667                                         if_ifwithaddr((addr->in.sa_family == AF_INET) ?
668                                                         (union olsr_ip_addr *)&addr->in4.sin_addr :
669                                                         (union olsr_ip_addr *)&addr->in6.sin6_addr));
670                         bool isOlsrIf = (olsrIntf != NULL);
671
672                         /* determine whether the iterated interface is configured as a
673                          * non-OLSR interface in the plugin parameter list */
674                         bool isRxNonOlsrIf = isRxNonOlsrInterface(ifName) && (addr->in.sa_family == rxMcAddr->in.sa_family);
675                         bool isTxNonOlsrIf = isTxNonOlsrInterface(ifName) && (addr->in.sa_family == txMcAddr->in.sa_family);
676
677                         bool isNonOlsrIf = isRxNonOlsrIf || isTxNonOlsrIf;
678
679                         if (!isOlsrIf && !isNonOlsrIf) {
680                                 /* Interface is not an OLSR interface AND interface is not
681                                  * configured as non-OLSR interface: skip */
682                                 continue;
683                         }
684
685                         if (isOlsrIf && !createOlsrInterface(olsrIntf)) {
686                                 /* creating an OLSR interface failed */
687                                 goto end;
688                         }
689
690                         if (!isNonOlsrIf) {
691                                 /* interface is not configured as non-OLSR interface: skip */
692                                 continue;
693                         }
694
695                         if (isRxNonOlsrIf && !createRxInterface(ifName, addr, rxSocketHandlerFunction, rxMcAddr)) {
696                                 /* creating a receive interface failed */
697                                 goto end;
698                         }
699
700                         if (isTxNonOlsrIf && !createTxInterface(ifName, addr)) {
701                                 /* creating a transmit interface failed */
702                                 goto end;
703                         }
704                 }
705         }
706
707         if (isUplinkAddrSet()) {
708                 downlinkSocketFd = createDownlinkSocket(getUplinkAddr()->in.sa_family, rxSocketHandlerFunctionDownlink);
709                 if (downlinkSocketFd == -1) {
710                         goto end;
711                 }
712         } else {
713                 downlinkSocketFd = -1;
714         }
715
716         retval = true;
717
718         end: freeifaddrs(ifAddrs);
719         return retval;
720 }
721
722 /**
723  Cleanup the OLSR network interfaces in the given list
724
725  @param networkInterface
726  the list of network interface to close and clean up
727  */
728 static void cleanupOlsrInterfaces(TOLSRNetworkInterface * networkInterface) {
729         TOLSRNetworkInterface * nextNetworkInterface = networkInterface;
730         while (nextNetworkInterface != NULL) {
731                 TOLSRNetworkInterface * iteratedNetworkInterface = nextNetworkInterface;
732                 nextNetworkInterface = iteratedNetworkInterface->next;
733                 iteratedNetworkInterface->next = NULL;
734                 free(iteratedNetworkInterface);
735         }
736 }
737
738 /**
739  Close and cleanup the network interfaces in the given list
740
741  @param networkInterface
742  the list of network interface to close and clean up
743  */
744 static void closeInterfaces(TRxTxNetworkInterface * networkInterface) {
745         TRxTxNetworkInterface * nextNetworkInterface = networkInterface;
746         while (nextNetworkInterface != NULL) {
747                 TRxTxNetworkInterface * iteratedNetworkInterface = nextNetworkInterface;
748                 if (iteratedNetworkInterface->socketFd >= 0) {
749                         if (iteratedNetworkInterface->handler) {
750                                 remove_olsr_socket(iteratedNetworkInterface->socketFd,
751                                                 iteratedNetworkInterface->handler, NULL);
752                         }
753                         close(iteratedNetworkInterface->socketFd);
754                         iteratedNetworkInterface->socketFd = -1;
755                 }
756                 nextNetworkInterface = iteratedNetworkInterface->next;
757                 iteratedNetworkInterface->next = NULL;
758                 free(iteratedNetworkInterface);
759         }
760 }
761
762 /**
763  Close and cleanup all receive and transmit network interfaces
764  */
765 void closeNetworkInterfaces(void) {
766         if (rxNetworkInterfacesListHead != NULL) {
767                 closeInterfaces(rxNetworkInterfacesListHead);
768                 rxNetworkInterfacesListHead = NULL;
769         }
770
771         if (txNetworkInterfacesListHead != NULL) {
772                 closeInterfaces(txNetworkInterfacesListHead);
773                 txNetworkInterfacesListHead = NULL;
774         }
775
776         if (olsrNetworkInterfacesListHead != NULL) {
777                 cleanupOlsrInterfaces(olsrNetworkInterfacesListHead);
778                 olsrNetworkInterfacesListHead = NULL;
779         }
780
781         if (downlinkSocketFd != -1 ) {
782                 if (downlinkHandler) {
783                         remove_olsr_socket (downlinkSocketFd, downlinkHandler, NULL);
784                         downlinkHandler = NULL;
785                 }
786                 close(downlinkSocketFd);
787                 downlinkSocketFd = -1;
788         }
789 }