fix some issues in net/olsrd

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

fix some issues in net/olsrd

Stefan Sperling-5
This fixes issues I have found in net/olsrd so far.

Heed DEBUG flag during the port build (I expect there will be more issues).

My route deletion fix does not work if olsrd's idea of the gateway differs
from the existing route's gateway in the kernel. The simple fix is to not
specify a gateway. (This means olsrd cannot delete MPATH routes but that
is not a concern for now. For MPATH routes it would need to get the gateway
from the kernel before deleting the route.)

Replace calls to net80211-specific ioctls with a simple SIOCGIFMEDIA call.
This is one of several things that stand in the way of pledge(2) support.

Patch up horrid racy signal handlers to fix a crash upon exit (another
reason why stale routes were left behind by the daemon):

#0  0x0000014764e15edf in olsr_add_kernel_route (rt=0x149ff2c4300) at src/process_routes.c:199
199       if (rt->rt_best->rtp_metric.hops > 1) {
(gdb) p rt
$1 = (struct rt_entry *) 0x149ff2c4300
(gdb) p rt->rt_best
$2 = (struct rt_path *) 0xdfdfdfdfdfdfdfdf
(gdb) up
#1  0x0000014764e160b9 in olsr_chg_kernel_routes (head_node=0x1476505fa90) at src/process_routes.c:274
274         olsr_add_kernel_route(rt);
(gdb) p *rt
$3 = {rt_dst = {prefix = {v4 = {s_addr = 3755991007}, v6 = {__u6_addr = {__u6_addr8 = ' <repeats 16 times>, __u6_addr16 = {
            57311, 57311, 57311, 57311, 57311, 57311, 57311, 57311}, __u6_addr32 = {3755991007, 3755991007, 3755991007,
            3755991007}}}}, prefix_len = 223 '}, rt_tree_node = {parent = 0xdfdfdfdfdfdfdfdf, left = 0xdfdfdfdfdfdfdfdf,
    right = 0xdfdfdfdfdfdfdfdf, next = 0xdfdfdfdfdfdfdfdf, prev = 0xdfdfdfdfdfdfdfdf, key = 0xdfdfdfdfdfdfdfdf,
    balance = -33 ', leader = 223 '}, rt_best = 0xdfdfdfdfdfdfdfdf, rt_nexthop = {gateway = {v4 = {s_addr = 3755991007},
      v6 = {__u6_addr = {__u6_addr8 = ' <repeats 16 times>, __u6_addr16 = {57311, 57311, 57311, 57311, 57311, 57311, 57311,
            57311}, __u6_addr32 = {3755991007, 3755991007, 3755991007, 3755991007}}}}, iif_index = -538976289}, rt_metric = {
    cost = 3755991007, hops = 3755991007}, rt_path_tree = {root = 0xdfdfdfdfdfdfdfdf, first = 0xdfdfdfdfdfdfdfdf,
    last = 0xdfdfdfdfdfdfdfdf, count = 3755991007, comp = 0xdfdfdfdfdfdfdfdf}, rt_change_node = {next = 0xdfdfdfdfdfdfdfdf,
    prev = 0xdfdfdfdfdfdfdfdf}}
(gdb) bt
#0  0x0000014764e15edf in olsr_add_kernel_route (rt=0x149ff2c4300) at src/process_routes.c:199
#1  0x0000014764e160b9 in olsr_chg_kernel_routes (head_node=0x1476505fa90) at src/process_routes.c:274
#2  0x0000014764e16492 in olsr_update_kernel_routes () at src/process_routes.c:420
#3  0x0000014764e15c63 in olsr_delete_all_kernel_routes () at src/process_routes.c:125
#4  0x0000014764e1c2cd in olsr_shutdown (signo=2) at src/main.c:825
#5  <signal handler called>
#6  0x0000014990b80848 in _thread_sys_read () at {standard input}:5
#7  0x0000014990b407d7 in *_libc_read_cancel (fd=Variable "fd" is not available.
) at /usr/src/lib/libc/sys/w_read.c:27
#8  0x0000014764e2608c in add_del_route (rt=0x149ff2c4300, add=1) at src/bsd/kernel_routes.c:259
#9  0x0000014764e2651f in olsr_ioctl_add_route (rt=0x149ff2c4300) at src/bsd/kernel_routes.c:328
#10 0x0000014764e15f87 in olsr_add_kernel_route (rt=0x149ff2c4300) at src/process_routes.c:207
#11 0x0000014764e160b9 in olsr_chg_kernel_routes (head_node=0x1476505fa90) at src/process_routes.c:274
#12 0x0000014764e16492 in olsr_update_kernel_routes () at src/process_routes.c:420
#13 0x0000014764e0d189 in olsr_calculate_routing_table (force=false) at src/olsr_spf.c:511
#14 0x0000014764e040eb in olsr_process_changes () at src/olsr.c:212
#15 0x0000014764e0faae in olsr_scheduler () at src/scheduler.c:487
#16 0x0000014764e1c007 in main (argc=3, argv=0x7f7fffff9188) at src/main.c:711
(gdb)

Index: Makefile
===================================================================
RCS file: /cvs/ports/net/olsrd/Makefile,v
retrieving revision 1.28
diff -u -p -r1.28 Makefile
--- Makefile 13 May 2017 22:27:20 -0000 1.28
+++ Makefile 15 May 2017 10:38:49 -0000
@@ -8,6 +8,7 @@ DISTNAME= olsrd-${VERSION}
 PKGNAME-main= olsrd-${VERSION}
 PKGNAME-gui= olsrd-gui-${VERSION}
 PKGNAME-plugins= olsrd-plugins-${VERSION}
+REVISION= 0
 
 CATEGORIES= net
 
@@ -41,7 +42,10 @@ NO_TEST= Yes
 MODULES= devel/gettext
 
 ALL_TARGET= build_all
-MAKE_FLAGS= DEBUG=0 CC="${CC}" OS=obsd LIBNET=1
+MAKE_FLAGS= CC="${CC}" OS=obsd LIBNET=1
+.ifdef DEBUG
+MAKE_FLAGS+= DEBUG=1
+.endif
 CPPFLAGS+= "-I${PREFIX}/libnet-1.1"
 FAKE_FLAGS= SBINDIR=${DESTDIR}/${PREFIX}/sbin \
  ETCDIR=${DESTDIR}/${PREFIX}/share/examples/olsrd \
Index: patches/patch-src_bsd_kernel_routes_c
===================================================================
RCS file: /cvs/ports/net/olsrd/patches/patch-src_bsd_kernel_routes_c,v
retrieving revision 1.1
diff -u -p -r1.1 patch-src_bsd_kernel_routes_c
--- patches/patch-src_bsd_kernel_routes_c 13 May 2017 22:27:20 -0000 1.1
+++ patches/patch-src_bsd_kernel_routes_c 15 May 2017 10:31:23 -0000
@@ -13,7 +13,7 @@ Index: src/bsd/kernel_routes.c
 
    /* walk to the end of the header */
    walker = buff + sizeof(struct rt_msghdr);
-@@ -222,14 +225,31 @@ add_del_route(const struct rt_entry *rt, int add)
+@@ -222,14 +225,24 @@ add_del_route(const struct rt_entry *rt, int add)
      drtm->rtm_version = RTM_VERSION;
      drtm->rtm_type = RTM_DELETE;
      drtm->rtm_index = 0;
@@ -29,13 +29,6 @@ Index: src/bsd/kernel_routes.c
      memcpy(walker, &sin4, sizeof(sin4));
      walker += sin_size;
      drtm->rtm_addrs = RTA_DST;
-+    if (0 != (drtm->rtm_flags & RTF_GATEWAY)) {
-+      nexthop = &rt->rt_nexthop;
-+      sin4.sin_addr = nexthop->gateway.v4;
-+      memcpy(walker, &sin4, sizeof(sin4));
-+      walker += sin_size;
-+      drtm->rtm_addrs |= RTA_GATEWAY;
-+    }
 +    if (0 == (drtm->rtm_flags & RTF_HOST)) {
 +      olsr_prefix_to_netmask(&mask, rt->rt_dst.prefix_len);
 +      sin4.sin_addr = mask.v4;
@@ -46,7 +39,7 @@ Index: src/bsd/kernel_routes.c
      drtm->rtm_msglen = (unsigned short)(walker - dbuff);
      len = write(olsr_cnf->rts, dbuff, drtm->rtm_msglen);
      if (len < 0) {
-@@ -298,6 +318,9 @@ add_del_route6(const struct rt_entry *rt, int add)
+@@ -298,6 +311,9 @@ add_del_route6(const struct rt_entry *rt, int add)
    rtm->rtm_flags = olsr_rt_flags(rt, add);
    rtm->rtm_pid = OLSR_PID;
    rtm->rtm_seq = ++seq;
@@ -56,7 +49,7 @@ Index: src/bsd/kernel_routes.c
 
    /* walk to the end of the header */
    walker = buff + sizeof(struct rt_msghdr);
-@@ -385,6 +408,9 @@ add_del_route6(const struct rt_entry *rt, int add)
+@@ -385,6 +401,9 @@ add_del_route6(const struct rt_entry *rt, int add)
      drtm->rtm_index = 0;
      drtm->rtm_flags = olsr_rt_flags(rt, add);
      drtm->rtm_seq = ++seq;
Index: patches/patch-src_bsd_net_c
===================================================================
RCS file: /cvs/ports/net/olsrd/patches/patch-src_bsd_net_c,v
retrieving revision 1.3
diff -u -p -r1.3 patch-src_bsd_net_c
--- patches/patch-src_bsd_net_c 13 May 2017 22:27:20 -0000 1.3
+++ patches/patch-src_bsd_net_c 14 May 2017 14:59:48 -0000
@@ -11,3 +11,84 @@ Index: src/bsd/net.c
  #include "../olsr_cfg.h"
  #include "../olsr.h"
 
+@@ -60,6 +61,7 @@
+ #include <string.h>
+ #include <unistd.h>
+ #include <net/if.h>
++#include <net/if_media.h>
+ #include <sys/ioctl.h>
+ #include <fcntl.h>
+ #include <syslog.h>
+@@ -82,7 +84,6 @@
+ #ifdef __NetBSD__
+ #include <net/if_ether.h>
+ #include <netinet6/in6_var.h>   /* For struct in6_ifreq */
+-#include <net80211/ieee80211_ioctl.h>
+ #include <ifaddrs.h>
+ #endif /* __NetBSD__ */
+
+@@ -96,8 +97,6 @@
+ #include <netinet6/in6_var.h>   /* For struct in6_ifreq */
+ #include <ifaddrs.h>
+ #include <sys/uio.h>
+-#include <net80211/ieee80211.h>
+-#include <net80211/ieee80211_ioctl.h>
+ #endif /* __OpenBSD__ */
+
+ #if defined __FreeBSD__ || defined __FreeBSD_kernel__
+@@ -105,10 +104,6 @@
+ #include <net/ethernet.h>
+ #include <netinet/in_var.h>
+ #include <ifaddrs.h>
+-#ifndef FBSD_NO_80211
+-#include <net80211/ieee80211.h>
+-#include <net80211/ieee80211_ioctl.h>
+-#endif /* FBSD_NO_80211 */
+ #endif /* defined __FreeBSD__ || defined __FreeBSD_kernel__ */
+
+ #ifdef __APPLE__
+@@ -743,39 +738,12 @@ olsr_select(int nfds, fd_set * readfds, fd_set * write
+ int
+ check_wireless_interface(char *ifname)
+ {
+-#if (defined __FreeBSD__ || defined __FreeBSD_kernel__ ) &&  !defined FBSD_NO_80211
+-
+-/* From FreeBSD ifconfig/ifieee80211.c ieee80211_status() */
+-  struct ieee80211req ireq;
+-  u_int8_t data[32];
+-
+-  memset(&ireq, 0, sizeof(ireq));
+-  strscpy(ireq.i_name, ifname, sizeof(ireq.i_name));
+-  ireq.i_data = &data;
+-  ireq.i_type = IEEE80211_IOC_SSID;
+-  ireq.i_val = -1;
+-  return (ioctl(olsr_cnf->ioctl_s, SIOCG80211, &ireq) >= 0) ? 1 : 0;
+-#elif defined __OpenBSD__
+-  struct ieee80211_nodereq nr;
+-  bzero(&nr, sizeof(nr));
+-  strscpy(nr.nr_ifname, ifname, sizeof(nr.nr_ifname));
+-  return (ioctl(olsr_cnf->ioctl_s, SIOCG80211FLAGS, &nr) >= 0) ? 1 : 0;
+-#elif defined __NetBSD__
+-  struct ifreq ireq;
+-  struct ieee80211_nwid data;
++  struct ifmediareq ifmr;
+   int ret;
+-
+-  memset(&ireq, 0, sizeof(ireq));
+-  strscpy(ireq.ifr_name, ifname, sizeof(ireq.ifr_name));
+-  ireq.ifr_data = &data;
+-  ret = ioctl(olsr_cnf->ioctl_s, SIOCG80211NWID, &ireq);
+-  if(ret == 0)
+-  return 1;
+-  return 0;
+-#else /* defined __NetBSD__ */
+-  ifname = NULL;                /* squelsh compiler warning */
+-  return 0;
+-#endif /* defined __NetBSD__ */
++  bzero(&ifmr, sizeof(ifmr));
++  strscpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
++  ret = ioctl(olsr_cnf->ioctl_s, SIOCGIFMEDIA, (caddr_t)&ifmr);
++  return (ret == 0 && IFM_TYPE(ifmr.ifm_current) == IFM_IEEE80211);
+ }
+
+ #include <sys/sockio.h>
Index: patches/patch-src_main_c
===================================================================
RCS file: patches/patch-src_main_c
diff -N patches/patch-src_main_c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ patches/patch-src_main_c 15 May 2017 10:28:39 -0000
@@ -0,0 +1,74 @@
+$OpenBSD$
+
+Index: src/main.c
+--- src/main.c.orig
++++ src/main.c
+@@ -83,9 +83,14 @@ void DisableIcmpRedirects(void);
+ bool olsr_win32_end_request = false;
+ bool olsr_win32_end_flag = false;
+ #else /* _WIN32 */
+-static void olsr_shutdown(int) __attribute__ ((noreturn));
++volatile sig_atomic_t olsr_do_shutdown = 0;
++static void olsr_shutdown_signal(int);
++volatile sig_atomic_t olsr_do_reconfigure = 0;
++static void olsr_reconfigure_signal(int);
+ #endif /* _WIN32 */
+
++
++
+ #if defined __ANDROID__
+ #define DEFAULT_LOCKFILE_PREFIX "/data/local/olsrd"
+ #elif defined linux || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
+@@ -692,13 +697,13 @@ int main(int argc, char *argv[]) {
+   SetConsoleCtrlHandler(SignalHandler, true);
+ #endif /* WINCE */
+ #else /* _WIN32 */
+-  signal(SIGHUP, olsr_reconfigure);
+-  signal(SIGINT, olsr_shutdown);
+-  signal(SIGQUIT, olsr_shutdown);
+-  signal(SIGILL, olsr_shutdown);
+-  signal(SIGABRT, olsr_shutdown);
+-  //  signal(SIGSEGV, olsr_shutdown);
+-  signal(SIGTERM, olsr_shutdown);
++  signal(SIGHUP, olsr_reconfigure_signal);
++  signal(SIGINT, olsr_shutdown_signal);
++  signal(SIGQUIT, olsr_shutdown_signal);
++  signal(SIGILL, olsr_shutdown_signal);
++  signal(SIGABRT, olsr_shutdown_signal);
++  //  signal(SIGSEGV, olsr_shutdown_signal);
++  signal(SIGTERM, olsr_shutdown_signal);
+   signal(SIGPIPE, SIG_IGN);
+   // Ignoring SIGUSR1 and SIGUSR1 by default to be able to use them in plugins
+   signal(SIGUSR1, SIG_IGN);
+@@ -720,7 +725,14 @@ int main(int argc, char *argv[]) {
+  *
+  *@param signo the signal that triggered this callback
+  */
+-void olsr_reconfigure(int signo __attribute__ ((unused))) {
++void olsr_reconfigure_signal(int signo)
++{
++  if (olsr_do_reconfigure == 0)
++    olsr_do_reconfigure = signo;
++  /* scheduler will soon call olsr_reconfigure() */
++}
++
++void olsr_reconfigure(int signo) {
+ #ifndef _WIN32
+   int errNr = errno;
+ #endif
+@@ -786,7 +798,14 @@ static void olsr_shutdown_messages(void) {
+ int __stdcall
+ SignalHandler(unsigned long signo)
+ #else /* _WIN32 */
+-static void olsr_shutdown(int signo __attribute__ ((unused)))
++static void olsr_shutdown_signal(int signo)
++{
++  if (olsr_do_shutdown == 0)
++   olsr_do_shutdown = signo;
++  /* scheduler will soon call olsr_shutdown() */
++}
++
++void olsr_shutdown(int signo)
+ #endif /* _WIN32 */
+ {
+ #ifndef _WIN32
Index: patches/patch-src_olsr_h
===================================================================
RCS file: patches/patch-src_olsr_h
diff -N patches/patch-src_olsr_h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ patches/patch-src_olsr_h 15 May 2017 10:26:29 -0000
@@ -0,0 +1,15 @@
+$OpenBSD$
+
+Index: src/olsr.h
+--- src/olsr.h.orig
++++ src/olsr.h
+@@ -91,6 +91,9 @@ int olsr_printf(int, const char *, ...) __attribute__
+
+ void olsr_trigger_forced_update(void *);
+
++void olsr_shutdown(int);
++void olsr_reconfigure(int);
++
+ #endif /* _OLSR_FUNCTIONS */
+
+ /*
Index: patches/patch-src_scheduler_c
===================================================================
RCS file: patches/patch-src_scheduler_c
diff -N patches/patch-src_scheduler_c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ patches/patch-src_scheduler_c 15 May 2017 10:26:29 -0000
@@ -0,0 +1,38 @@
+$OpenBSD$
+
+Index: src/scheduler.c
+--- src/scheduler.c.orig
++++ src/scheduler.c
+@@ -52,11 +52,16 @@
+
+ #include <unistd.h>
+ #include <assert.h>
++#include <signal.h>
+
+ #ifdef _WIN32
+ #define close(x) closesocket(x)
+ #endif /* _WIN32 */
+
++/* from main.c */
++extern volatile sig_atomic_t olsr_do_shutdown;
++extern volatile sig_atomic_t olsr_do_reconfigure;
++
+ /* Timer data, global. Externed in scheduler.h */
+ uint32_t now_times;                    /* relative time compared to startup (in milliseconds */
+ struct timeval first_tv;               /* timevalue during startup */
+@@ -500,6 +505,15 @@ olsr_scheduler(void)
+     if (olsr_win32_end_request) {
+       olsr_win32_end_flag = true;
+     }
++#else
++  if (olsr_do_shutdown != 0) {
++    olsr_shutdown(olsr_do_shutdown);
++    olsr_do_shutdown = 0;
++    break;
++  } else if (olsr_do_reconfigure != 0) {
++    olsr_reconfigure(olsr_do_reconfigure);
++    olsr_do_reconfigure = 0;
++  }
+ #endif /* _WIN32 */
+   }
+ }