2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
3 * Copyright (c) 2004, Andreas Tønnesen(andreto@olsr.org)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of olsr.org, olsrd nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * Visit http://www.olsr.org for more information.
35 * If you find this software useful feel free to make a donation
36 * to the project. For more information see the website or contact
37 * the copyright holders.
39 * $Id: ifnet.c,v 1.52 2007/10/20 12:59:08 bernd67 Exp $
43 #if defined __FreeBSD__ || defined __MacOSX__ || defined __NetBSD__ || defined __OpenBSD__
44 #define ifr_netmask ifr_addr
47 #include "interfaces.h"
53 #include "socket_parser.h"
55 #include "scheduler.h"
56 #include "generate_msg.h"
58 #include "lq_packet.h"
62 #include <sys/types.h>
64 #include <net/if_arp.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <arpa/inet.h>
71 int bufspace = 127*1024; /* max. input buffer size to request */
75 set_flag(char *ifname, short flag __attribute__((unused)))
79 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
82 if (ioctl(olsr_cnf->ioctl_s, SIOCGIFFLAGS, &ifr) < 0)
84 fprintf(stderr,"ioctl (get interface flags)");
88 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
90 //printf("Setting flags for if \"%s\"\n", ifr.ifr_name);
92 if(!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)))
95 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
97 if(ioctl(olsr_cnf->ioctl_s, SIOCSIFFLAGS, &ifr) < 0)
99 fprintf(stderr, "ERROR(%s): %s\n", ifr.ifr_name, strerror(errno));
111 check_interface_updates(void *foo __attribute__((unused)))
113 struct olsr_if *tmp_if;
116 OLSR_PRINTF(3, "Checking for updates in the interface set\n");
119 for(tmp_if = olsr_cnf->interfaces; tmp_if != NULL; tmp_if = tmp_if->next)
121 if(tmp_if->host_emul)
124 if(olsr_cnf->host_emul) /* XXX: TEMPORARY! */
127 if(!tmp_if->cnf->autodetect_chg)
130 /* Don't check this interface */
131 OLSR_PRINTF(3, "Not checking interface %s\n", tmp_if->name);
136 if(tmp_if->configured)
138 chk_if_changed(tmp_if);
142 chk_if_up(tmp_if, 3);
150 * Checks if an initialized interface is changed
151 * that is if it has been set down or the address
154 *@param iface the olsr_if struct describing the interface
157 chk_if_changed(struct olsr_if *iface)
159 struct interface *ifp, *tmp_ifp;
161 struct sockaddr_in6 tmp_saddr6;
166 OLSR_PRINTF(3, "Checking if %s is set down or changed\n", iface->name);
176 /* Should not happen */
177 iface->configured = 0;
181 memset(&ifr, 0, sizeof(struct ifreq));
182 strncpy(ifr.ifr_name, iface->name, IFNAMSIZ);
185 /* Get flags (and check if interface exists) */
186 if (ioctl(olsr_cnf->ioctl_s, SIOCGIFFLAGS, &ifr) < 0)
188 OLSR_PRINTF(3, "No such interface: %s\n", iface->name);
189 goto remove_interface;
192 ifp->int_flags = ifr.ifr_flags;
195 * First check if the interface is set DOWN
198 if ((ifp->int_flags & IFF_UP) == 0)
200 OLSR_PRINTF(1, "\tInterface %s not up - removing it...\n", iface->name);
201 goto remove_interface;
205 * We do all the interface type checks over.
206 * This is because the interface might be a PCMCIA card. Therefore
207 * It might not be the same physical interface as we configured earlier.
210 /* Check broadcast */
211 if ((olsr_cnf->ip_version == AF_INET) &&
212 !iface->cnf->ipv4_broadcast.v4 && /* Skip if fixed bcast */
213 (!(ifp->int_flags & IFF_BROADCAST)))
215 OLSR_PRINTF(3, "\tNo broadcast - removing\n");
216 goto remove_interface;
219 if (ifp->int_flags & IFF_LOOPBACK)
221 OLSR_PRINTF(3, "\tThis is a loopback interface - removing it...\n");
222 goto remove_interface;
225 ifp->is_hcif = OLSR_FALSE;
227 /* trying to detect if interface is wireless. */
228 ifp->is_wireless = check_wireless_interface(ifr.ifr_name);
230 /* Set interface metric */
231 if(iface->cnf->weight.fixed)
232 ifp->int_metric = iface->cnf->weight.value;
234 ifp->int_metric = calculate_if_metric(ifr.ifr_name);
237 if (ioctl(olsr_cnf->ioctl_s, SIOCGIFMTU, &ifr) < 0)
241 ifr.ifr_mtu -= (olsr_cnf->ip_version == AF_INET6) ? UDP_IPV6_HDRSIZE : UDP_IPV4_HDRSIZE;
243 if(ifp->int_mtu != ifr.ifr_mtu)
245 ifp->int_mtu = ifr.ifr_mtu;
246 /* Create new outputbuffer */
247 net_remove_buffer(ifp); /* Remove old */
253 /* Get interface index */
254 ifp->if_index = if_nametoindex(ifr.ifr_name);
257 * Now check if the IP has changed
261 if(olsr_cnf->ip_version == AF_INET6)
263 /* Get interface address */
265 if(get_ipv6_address(ifr.ifr_name, &tmp_saddr6, iface->cnf->ipv6_addrtype) <= 0)
267 if(iface->cnf->ipv6_addrtype == IPV6_ADDR_SITELOCAL)
268 OLSR_PRINTF(3, "\tCould not find site-local IPv6 address for %s\n", ifr.ifr_name);
270 OLSR_PRINTF(3, "\tCould not find global IPv6 address for %s\n", ifr.ifr_name);
273 goto remove_interface;
277 OLSR_PRINTF(3, "\tAddress: %s\n", ip6_to_string(&tmp_saddr6.sin6_addr));
280 if(memcmp(&tmp_saddr6.sin6_addr, &ifp->int6_addr.sin6_addr, olsr_cnf->ipsize) != 0)
282 OLSR_PRINTF(1, "New IP address for %s:\n", ifr.ifr_name);
283 OLSR_PRINTF(1, "\tOld: %s\n", ip6_to_string(&ifp->int6_addr.sin6_addr));
284 OLSR_PRINTF(1, "\tNew: %s\n", ip6_to_string(&tmp_saddr6.sin6_addr));
286 /* Check main addr */
287 if(memcmp(&olsr_cnf->main_addr, &tmp_saddr6.sin6_addr, olsr_cnf->ipsize) == 0)
289 /* Update main addr */
290 memcpy(&olsr_cnf->main_addr, &tmp_saddr6.sin6_addr, olsr_cnf->ipsize);
294 memcpy(&ifp->int6_addr.sin6_addr, &tmp_saddr6.sin6_addr, olsr_cnf->ipsize);
295 memcpy(&ifp->ip_addr, &tmp_saddr6.sin6_addr, olsr_cnf->ipsize);
297 run_ifchg_cbs(ifp, IFCHG_IF_UPDATE);
307 /* Check interface address (IPv4)*/
308 if(ioctl(olsr_cnf->ioctl_s, SIOCGIFADDR, &ifr) < 0)
310 OLSR_PRINTF(1, "\tCould not get address of interface - removing it\n");
311 goto remove_interface;
315 OLSR_PRINTF(3, "\tAddress:%s\n", sockaddr_to_string(&ifr.ifr_addr));
318 if(memcmp(&((struct sockaddr_in *)&ifp->int_addr)->sin_addr.s_addr,
319 &((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr,
320 olsr_cnf->ipsize) != 0)
323 OLSR_PRINTF(1, "IPv4 address changed for %s\n", ifr.ifr_name);
324 OLSR_PRINTF(1, "\tOld:%s\n", sockaddr_to_string(&ifp->int_addr));
325 OLSR_PRINTF(1, "\tNew:%s\n", sockaddr_to_string(&ifr.ifr_addr));
327 ifp->int_addr = ifr.ifr_addr;
329 if(memcmp(&olsr_cnf->main_addr,
331 olsr_cnf->ipsize) == 0)
333 OLSR_PRINTF(1, "New main address: %s\n", sockaddr_to_string(&ifr.ifr_addr));
334 olsr_syslog(OLSR_LOG_INFO, "New main address: %s\n", sockaddr_to_string(&ifr.ifr_addr));
335 memcpy(&olsr_cnf->main_addr,
336 &((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr,
340 memcpy(&ifp->ip_addr,
341 &((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr,
348 if (ioctl(olsr_cnf->ioctl_s, SIOCGIFNETMASK, &ifr) < 0)
350 olsr_syslog(OLSR_LOG_ERR, "%s: ioctl (get broadaddr)", ifr.ifr_name);
351 goto remove_interface;
355 OLSR_PRINTF(3, "\tNetmask:%s\n", sockaddr_to_string(&ifr.ifr_netmask));
358 if(memcmp(&((struct sockaddr_in *)&ifp->int_netmask)->sin_addr.s_addr,
359 &((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr,
360 olsr_cnf->ipsize) != 0)
363 OLSR_PRINTF(1, "IPv4 netmask changed for %s\n", ifr.ifr_name);
364 OLSR_PRINTF(1, "\tOld:%s\n", sockaddr_to_string(&ifp->int_netmask));
365 OLSR_PRINTF(1, "\tNew:%s\n", sockaddr_to_string(&ifr.ifr_netmask));
367 ifp->int_netmask = ifr.ifr_netmask;
372 if(!iface->cnf->ipv4_broadcast.v4)
374 /* Check broadcast address */
375 if (ioctl(olsr_cnf->ioctl_s, SIOCGIFBRDADDR, &ifr) < 0)
377 olsr_syslog(OLSR_LOG_ERR, "%s: ioctl (get broadaddr)", ifr.ifr_name);
378 goto remove_interface;
382 OLSR_PRINTF(3, "\tBroadcast address:%s\n", sockaddr_to_string(&ifr.ifr_broadaddr));
385 if(memcmp(&((struct sockaddr_in *)&ifp->int_broadaddr)->sin_addr.s_addr,
386 &((struct sockaddr_in *)&ifr.ifr_broadaddr)->sin_addr.s_addr,
387 olsr_cnf->ipsize) != 0)
391 OLSR_PRINTF(1, "IPv4 broadcast changed for %s\n", ifr.ifr_name);
392 OLSR_PRINTF(1, "\tOld:%s\n", sockaddr_to_string(&ifp->int_broadaddr));
393 OLSR_PRINTF(1, "\tNew:%s\n", sockaddr_to_string(&ifr.ifr_broadaddr));
395 ifp->int_broadaddr = ifr.ifr_broadaddr;
402 run_ifchg_cbs(ifp, IFCHG_IF_UPDATE);
408 OLSR_PRINTF(1, "Removing interface %s\n", iface->name);
409 olsr_syslog(OLSR_LOG_INFO, "Removing interface %s\n", iface->name);
411 del_if_link_entries(&ifp->ip_addr);
414 *Call possible ifchange functions registered by plugins
416 run_ifchg_cbs(ifp, IFCHG_IF_REMOVE);
421 ifnet = ifp->int_next;
426 while(tmp_ifp->int_next != ifp)
428 tmp_ifp = tmp_ifp->int_next;
430 tmp_ifp->int_next = ifp->int_next;
434 /* Remove output buffer */
435 net_remove_buffer(ifp);
437 /* Check main addr */
438 if(COMP_IP(&olsr_cnf->main_addr, &ifp->ip_addr))
442 /* No more interfaces */
443 memset(&olsr_cnf->main_addr, 0, olsr_cnf->ipsize);
444 OLSR_PRINTF(1, "No more interfaces...\n");
448 COPY_IP(&olsr_cnf->main_addr, &ifnet->ip_addr);
449 OLSR_PRINTF(1, "New main address: %s\n", olsr_ip_to_string(&olsr_cnf->main_addr));
450 olsr_syslog(OLSR_LOG_INFO, "New main address: %s\n", olsr_ip_to_string(&olsr_cnf->main_addr));
456 * Deregister scheduled functions
459 if (olsr_cnf->lq_level == 0)
461 olsr_remove_scheduler_event(&generate_hello,
463 iface->cnf->hello_params.emission_interval,
466 olsr_remove_scheduler_event(&generate_tc,
468 iface->cnf->tc_params.emission_interval,
475 olsr_remove_scheduler_event(&olsr_output_lq_hello,
477 iface->cnf->hello_params.emission_interval,
480 olsr_remove_scheduler_event(&olsr_output_lq_tc,
482 iface->cnf->tc_params.emission_interval,
487 olsr_remove_scheduler_event(&generate_mid,
489 iface->cnf->mid_params.emission_interval,
492 olsr_remove_scheduler_event(&generate_hna,
494 iface->cnf->hna_params.emission_interval,
500 iface->configured = 0;
501 iface->interf = NULL;
502 /* Close olsr socket */
503 close(ifp->olsr_socket);
504 remove_olsr_socket(ifp->olsr_socket, &olsr_input);
510 if((ifnet == NULL) && (!olsr_cnf->allow_no_interfaces))
512 OLSR_PRINTF(1, "No more active interfaces - exiting.\n");
513 olsr_syslog(OLSR_LOG_INFO, "No more active interfaces - exiting.\n");
514 olsr_cnf->exit_value = EXIT_FAILURE;
515 kill(getpid(), SIGINT);
523 * Initializes the special interface used in
524 * host-client emulation
527 add_hemu_if(struct olsr_if *iface)
529 struct interface *ifp;
530 union olsr_ip_addr null_addr;
533 if(!iface->host_emul)
536 ifp = olsr_malloc(sizeof (struct interface), "Interface update 2");
538 memset(ifp, 0, sizeof (struct interface));
540 iface->configured = OLSR_TRUE;
543 ifp->is_hcif = OLSR_TRUE;
544 ifp->int_name = olsr_malloc(strlen("hcif01") + 1, "Interface update 3");
547 strcpy(ifp->int_name, "hcif01");
549 OLSR_PRINTF(1, "Adding %s(host emulation):\n", ifp->int_name);
551 OLSR_PRINTF(1, " Address:%s\n", olsr_ip_to_string(&iface->hemu_ip));
553 OLSR_PRINTF(1, " NB! This is a emulated interface\n that does not exist in the kernel!\n");
555 ifp->int_next = ifnet;
558 memset(&null_addr, 0, olsr_cnf->ipsize);
559 if(COMP_IP(&null_addr, &olsr_cnf->main_addr))
561 COPY_IP(&olsr_cnf->main_addr, &iface->hemu_ip);
562 OLSR_PRINTF(1, "New main address: %s\n", olsr_ip_to_string(&olsr_cnf->main_addr));
563 olsr_syslog(OLSR_LOG_INFO, "New main address: %s\n", olsr_ip_to_string(&olsr_cnf->main_addr));
566 ifp->int_mtu = OLSR_DEFAULT_MTU;
568 ifp->int_mtu -= (olsr_cnf->ip_version == AF_INET6) ? UDP_IPV6_HDRSIZE : UDP_IPV4_HDRSIZE;
574 if(olsr_cnf->ip_version == AF_INET)
576 struct sockaddr_in sin;
578 memset(&sin, 0, sizeof(sin));
580 sin.sin_family = AF_INET;
581 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
582 sin.sin_port = htons(10150);
585 ifp->ip_addr.v4 = iface->hemu_ip.v4;
587 memcpy(&((struct sockaddr_in *)&ifp->int_addr)->sin_addr, &iface->hemu_ip, olsr_cnf->ipsize);
590 *We create one socket for each interface and bind
591 *the socket to it. This to ensure that we can control
592 *on what interface the message is transmitted
595 ifp->olsr_socket = gethemusocket(&sin);
597 if (ifp->olsr_socket < 0)
599 fprintf(stderr, "Could not initialize socket... exiting!\n\n");
600 olsr_syslog(OLSR_LOG_ERR, "Could not initialize socket... exiting!\n\n");
601 olsr_cnf->exit_value = EXIT_FAILURE;
602 kill(getpid(), SIGINT);
609 memcpy(&ifp->ip_addr, &iface->hemu_ip, olsr_cnf->ipsize);
613 *We create one socket for each interface and bind
614 *the socket to it. This to ensure that we can control
615 *on what interface the message is transmitted
618 ifp->olsr_socket = gethcsocket6(&addrsock6, bufspace, ifp->int_name);
620 join_mcast(ifp, ifp->olsr_socket);
622 if (ifp->olsr_socket < 0)
624 fprintf(stderr, "Could not initialize socket... exiting!\n\n");
625 olsr_syslog(OLSR_LOG_ERR, "Could not initialize socket... exiting!\n\n");
626 olsr_cnf->exit_value = EXIT_FAILURE;
627 kill(getpid(), SIGINT);
633 /* Send IP as first 4/16 bytes on socket */
634 memcpy(addr, iface->hemu_ip.v6.s6_addr, olsr_cnf->ipsize);
635 addr[0] = htonl(addr[0]);
636 addr[1] = htonl(addr[1]);
637 addr[2] = htonl(addr[2]);
638 addr[3] = htonl(addr[3]);
640 if(send(ifp->olsr_socket, addr , olsr_cnf->ipsize, 0) != (int)olsr_cnf->ipsize)
642 fprintf(stderr, "Error sending IP!");
645 /* Register socket */
646 add_olsr_socket(ifp->olsr_socket, &olsr_input_hostemu);
649 if (olsr_cnf->lq_level == 0)
651 olsr_register_scheduler_event(&generate_hello,
653 iface->cnf->hello_params.emission_interval,
656 olsr_register_scheduler_event(&generate_tc,
658 iface->cnf->tc_params.emission_interval,
665 olsr_register_scheduler_event(&olsr_output_lq_hello,
667 iface->cnf->hello_params.emission_interval,
670 olsr_register_scheduler_event(&olsr_output_lq_tc,
672 iface->cnf->tc_params.emission_interval,
677 olsr_register_scheduler_event(&generate_mid,
679 iface->cnf->mid_params.emission_interval,
682 olsr_register_scheduler_event(&generate_hna,
684 iface->cnf->hna_params.emission_interval,
688 /* Recalculate max jitter */
690 if((olsr_cnf->max_jitter == 0) ||
691 ((iface->cnf->hello_params.emission_interval / 4) < olsr_cnf->max_jitter))
692 olsr_cnf->max_jitter = iface->cnf->hello_params.emission_interval / 4;
694 /* Recalculate max topology hold time */
695 if(olsr_cnf->max_tc_vtime < iface->cnf->tc_params.emission_interval)
696 olsr_cnf->max_tc_vtime = iface->cnf->tc_params.emission_interval;
698 ifp->hello_etime = iface->cnf->hello_params.emission_interval;
699 ifp->valtimes.hello = double_to_me(iface->cnf->hello_params.validity_time);
700 ifp->valtimes.tc = double_to_me(iface->cnf->tc_params.validity_time);
701 ifp->valtimes.mid = double_to_me(iface->cnf->mid_params.validity_time);
702 ifp->valtimes.hna = double_to_me(iface->cnf->hna_params.validity_time);
707 static char basenamestr[32];
708 static const char* if_basename(const char* name);
709 static const char* if_basename(const char* name)
711 char *p = strchr(name, ':');
712 if (NULL == p || p - name >= (int)(sizeof(basenamestr) / sizeof(basenamestr[0]) - 1)) {
715 memcpy(basenamestr, name, p - name);
716 basenamestr[p - name] = 0;
721 * Initializes a interface described by iface,
722 * if it is set up and is of the correct type.
724 *@param iface the olsr_if struct describing the interface
725 *@param so the socket to use for ioctls
729 chk_if_up(struct olsr_if *iface, int debuglvl)
731 struct interface ifs, *ifp;
733 union olsr_ip_addr null_addr;
735 int precedence = IPTOS_PREC(olsr_cnf->tos);
736 int tos_bits = IPTOS_TOS(olsr_cnf->tos);
742 memset(&ifr, 0, sizeof(struct ifreq));
743 strncpy(ifr.ifr_name, iface->name, IFNAMSIZ);
745 OLSR_PRINTF(debuglvl, "Checking %s:\n", ifr.ifr_name);
747 /* Get flags (and check if interface exists) */
748 if (ioctl(olsr_cnf->ioctl_s, SIOCGIFFLAGS, &ifr) < 0)
750 OLSR_PRINTF(debuglvl, "\tNo such interface!\n");
754 memset(&ifs.netbuf, 0, sizeof(struct olsr_netbuf));
755 ifs.int_flags = ifr.ifr_flags;
758 if ((ifs.int_flags & IFF_UP) == 0)
760 OLSR_PRINTF(debuglvl, "\tInterface not up - skipping it...\n");
764 /* Check broadcast */
765 if ((olsr_cnf->ip_version == AF_INET) &&
766 !iface->cnf->ipv4_broadcast.v4 && /* Skip if fixed bcast */
767 (!(ifs.int_flags & IFF_BROADCAST)))
769 OLSR_PRINTF(debuglvl, "\tNo broadcast - skipping\n");
774 if (ifs.int_flags & IFF_LOOPBACK)
776 OLSR_PRINTF(debuglvl, "\tThis is a loopback interface - skipping it...\n");
780 ifs.is_hcif = OLSR_FALSE;
782 /* trying to detect if interface is wireless. */
783 ifs.is_wireless = check_wireless_interface(ifr.ifr_name);
786 OLSR_PRINTF(debuglvl, "\tWireless interface detected\n");
788 OLSR_PRINTF(debuglvl, "\tNot a wireless interface\n");
792 if(olsr_cnf->ip_version == AF_INET6)
794 /* Get interface address */
796 if(get_ipv6_address(ifr.ifr_name, &ifs.int6_addr, iface->cnf->ipv6_addrtype) <= 0)
798 if(iface->cnf->ipv6_addrtype == IPV6_ADDR_SITELOCAL)
799 OLSR_PRINTF(debuglvl, "\tCould not find site-local IPv6 address for %s\n", ifr.ifr_name);
801 OLSR_PRINTF(debuglvl, "\tCould not find global IPv6 address for %s\n", ifr.ifr_name);
806 OLSR_PRINTF(debuglvl, "\tAddress: %s\n", ip6_to_string(&ifs.int6_addr.sin6_addr));
809 ifs.int6_multaddr.sin6_addr = (iface->cnf->ipv6_addrtype == IPV6_ADDR_SITELOCAL) ?
810 iface->cnf->ipv6_multi_site.v6 :
811 iface->cnf->ipv6_multi_glbl.v6;
812 /* Set address family */
813 ifs.int6_multaddr.sin6_family = AF_INET6;
815 ifs.int6_multaddr.sin6_port = htons(OLSRPORT);
818 ifs.int6_multaddr.sin6_scope_id = 0;
821 OLSR_PRINTF(debuglvl, "\tMulticast: %s\n", ip6_to_string(&ifs.int6_multaddr.sin6_addr));
827 /* Get interface address (IPv4)*/
828 if(ioctl(olsr_cnf->ioctl_s, SIOCGIFADDR, &ifr) < 0)
830 OLSR_PRINTF(debuglvl, "\tCould not get address of interface - skipping it\n");
834 ifs.int_addr = ifr.ifr_addr;
838 if (ioctl(olsr_cnf->ioctl_s, SIOCGIFNETMASK, &ifr) < 0)
840 olsr_syslog(OLSR_LOG_ERR, "%s: ioctl (get netmask)", ifr.ifr_name);
844 ifs.int_netmask = ifr.ifr_netmask;
846 /* Find broadcast address */
847 if(iface->cnf->ipv4_broadcast.v4)
849 /* Specified broadcast */
850 memset(&ifs.int_broadaddr, 0, sizeof(struct sockaddr));
851 memcpy(&((struct sockaddr_in *)&ifs.int_broadaddr)->sin_addr.s_addr,
852 &iface->cnf->ipv4_broadcast.v4,
858 if (ioctl(olsr_cnf->ioctl_s, SIOCGIFBRDADDR, &ifr) < 0)
860 olsr_syslog(OLSR_LOG_ERR, "%s: ioctl (get broadaddr)", ifr.ifr_name);
864 ifs.int_broadaddr = ifr.ifr_broadaddr;
867 /* Deactivate IP spoof filter */
868 deactivate_spoof(if_basename(ifr.ifr_name), &ifs, olsr_cnf->ip_version);
870 /* Disable ICMP redirects */
871 disable_redirects(if_basename(ifr.ifr_name), &ifs, olsr_cnf->ip_version);
876 /* Get interface index */
878 ifs.if_index = if_nametoindex(ifr.ifr_name);
880 /* Set interface metric */
881 if(iface->cnf->weight.fixed)
882 ifs.int_metric = iface->cnf->weight.value;
884 ifs.int_metric = calculate_if_metric(ifr.ifr_name);
885 OLSR_PRINTF(1, "\tMetric: %d\n", ifs.int_metric);
888 if (ioctl(olsr_cnf->ioctl_s, SIOCGIFMTU, &ifr) < 0)
889 ifs.int_mtu = OLSR_DEFAULT_MTU;
891 ifs.int_mtu = ifr.ifr_mtu;
893 ifs.int_mtu -= (olsr_cnf->ip_version == AF_INET6) ? UDP_IPV6_HDRSIZE : UDP_IPV4_HDRSIZE;
898 net_add_buffer(&ifs);
900 OLSR_PRINTF(1, "\tMTU - IPhdr: %d\n", ifs.int_mtu);
902 olsr_syslog(OLSR_LOG_INFO, "Adding interface %s\n", iface->name);
903 OLSR_PRINTF(1, "\tIndex %d\n", ifs.if_index);
905 if(olsr_cnf->ip_version == AF_INET)
907 OLSR_PRINTF(1, "\tAddress:%s\n", sockaddr_to_string(&ifs.int_addr));
908 OLSR_PRINTF(1, "\tNetmask:%s\n", sockaddr_to_string(&ifs.int_netmask));
909 OLSR_PRINTF(1, "\tBroadcast address:%s\n", sockaddr_to_string(&ifs.int_broadaddr));
913 OLSR_PRINTF(1, "\tAddress: %s\n", ip6_to_string(&ifs.int6_addr.sin6_addr));
914 OLSR_PRINTF(1, "\tMulticast: %s\n", ip6_to_string(&ifs.int6_multaddr.sin6_addr));
917 ifp = olsr_malloc(sizeof (struct interface), "Interface update 2");
919 iface->configured = 1;
922 memcpy(ifp, &ifs, sizeof(struct interface));
924 ifp->gen_properties = NULL;
925 ifp->int_name = olsr_malloc(strlen(ifr.ifr_name) + 1, "Interface update 3");
927 strcpy(ifp->int_name, if_basename(ifr.ifr_name));
928 /* Segfaults if using strncpy(IFNAMSIZ) why oh why?? */
929 ifp->int_next = ifnet;
932 if(olsr_cnf->ip_version == AF_INET)
935 ifp->ip_addr.v4 = ((struct sockaddr_in *)&ifp->int_addr)->sin_addr.s_addr;
937 *We create one socket for each interface and bind
938 *the socket to it. This to ensure that we can control
939 *on what interface the message is transmitted
942 ifp->olsr_socket = getsocket((struct sockaddr *)&addrsock, bufspace, ifp->int_name);
944 if (ifp->olsr_socket < 0)
946 fprintf(stderr, "Could not initialize socket... exiting!\n\n");
947 olsr_syslog(OLSR_LOG_ERR, "Could not initialize socket... exiting!\n\n");
948 olsr_cnf->exit_value = EXIT_FAILURE;
949 kill(getpid(), SIGINT);
956 memcpy(&ifp->ip_addr, &ifp->int6_addr.sin6_addr, olsr_cnf->ipsize);
960 *We create one socket for each interface and bind
961 *the socket to it. This to ensure that we can control
962 *on what interface the message is transmitted
965 ifp->olsr_socket = getsocket6(&addrsock6, bufspace, ifp->int_name);
967 join_mcast(ifp, ifp->olsr_socket);
969 if (ifp->olsr_socket < 0)
971 fprintf(stderr, "Could not initialize socket... exiting!\n\n");
972 olsr_syslog(OLSR_LOG_ERR, "Could not initialize socket... exiting!\n\n");
973 olsr_cnf->exit_value = EXIT_FAILURE;
974 kill(getpid(), SIGINT);
979 set_buffer_timer(ifp);
981 /* Register socket */
982 add_olsr_socket(ifp->olsr_socket, &olsr_input);
987 if (setsockopt(ifp->olsr_socket, SOL_SOCKET, SO_PRIORITY, (char*)&precedence, sizeof(precedence)) < 0)
989 perror("setsockopt(SO_PRIORITY)");
990 olsr_syslog(OLSR_LOG_ERR, "OLSRD: setsockopt(SO_PRIORITY) error %m");
992 if (setsockopt(ifp->olsr_socket, SOL_IP, IP_TOS, (char*)&tos_bits, sizeof(tos_bits)) < 0)
994 perror("setsockopt(IP_TOS)");
995 olsr_syslog(OLSR_LOG_ERR, "setsockopt(IP_TOS) error %m");
1000 *Initialize sequencenumber as a random 16bit value
1002 ifp->olsr_seqnum = random() & 0xFFFF;
1005 * Set main address if this is the only interface
1007 memset(&null_addr, 0, olsr_cnf->ipsize);
1008 if(COMP_IP(&null_addr, &olsr_cnf->main_addr))
1010 COPY_IP(&olsr_cnf->main_addr, &ifp->ip_addr);
1011 OLSR_PRINTF(1, "New main address: %s\n", olsr_ip_to_string(&olsr_cnf->main_addr));
1012 olsr_syslog(OLSR_LOG_INFO, "New main address: %s\n", olsr_ip_to_string(&olsr_cnf->main_addr));
1016 * Register scheduled functions
1019 if (olsr_cnf->lq_level == 0)
1021 olsr_register_scheduler_event(&generate_hello,
1023 iface->cnf->hello_params.emission_interval,
1026 olsr_register_scheduler_event(&generate_tc,
1028 iface->cnf->tc_params.emission_interval,
1035 olsr_register_scheduler_event(&olsr_output_lq_hello,
1037 iface->cnf->hello_params.emission_interval,
1040 olsr_register_scheduler_event(&olsr_output_lq_tc,
1042 iface->cnf->tc_params.emission_interval,
1047 olsr_register_scheduler_event(&generate_mid,
1049 iface->cnf->mid_params.emission_interval,
1052 olsr_register_scheduler_event(&generate_hna,
1054 iface->cnf->hna_params.emission_interval,
1058 /* Recalculate max jitter */
1060 if((olsr_cnf->max_jitter == 0) ||
1061 ((iface->cnf->hello_params.emission_interval / 4) < olsr_cnf->max_jitter))
1062 olsr_cnf->max_jitter = iface->cnf->hello_params.emission_interval / 4;
1064 /* Recalculate max topology hold time */
1065 if(olsr_cnf->max_tc_vtime < iface->cnf->tc_params.emission_interval)
1066 olsr_cnf->max_tc_vtime = iface->cnf->tc_params.emission_interval;
1068 ifp->hello_etime = iface->cnf->hello_params.emission_interval;
1069 ifp->valtimes.hello = double_to_me(iface->cnf->hello_params.validity_time);
1070 ifp->valtimes.tc = double_to_me(iface->cnf->tc_params.validity_time);
1071 ifp->valtimes.mid = double_to_me(iface->cnf->mid_params.validity_time);
1072 ifp->valtimes.hna = double_to_me(iface->cnf->hna_params.validity_time);
1076 *Call possible ifchange functions registered by plugins
1078 run_ifchg_cbs(ifp, IFCHG_IF_ADD);