* made delete_all_inet_gws() static since it is not used elsewhere
authorBernd Petrovitsch <bernd@firmix.at>
Mon, 17 Sep 2007 22:55:40 +0000 (22:55 +0000)
committerBernd Petrovitsch <bernd@firmix.at>
Mon, 17 Sep 2007 22:55:40 +0000 (22:55 +0000)
* localized the netlink version restoring the former logging
* killed a superflous close()
No functional change, just moving code around.

src/kernel_routes.h
src/linux/kernel_routes.c

index c5884fd..2091530 100644 (file)
  * to the project. For more information see the website or contact
  * the copyright holders.
  *
- * $Id: kernel_routes.h,v 1.9 2007/09/05 16:11:10 bernd67 Exp $
+ * $Id: kernel_routes.h,v 1.10 2007/09/17 22:55:40 bernd67 Exp $
  */
 
-
-
 #ifndef _OLSR_KERNEL_RT
 #define _OLSR_KERNEL_RT
 
 #include "defs.h"
 #include "routing_table.h"
 
-
 int
 olsr_ioctl_add_route(struct rt_entry *);
 
@@ -61,9 +57,4 @@ olsr_ioctl_del_route(struct rt_entry *);
 int
 olsr_ioctl_del_route6(struct rt_entry *);
 
-
-int
-delete_all_inet_gws(void);
-
-
 #endif
index 910e299..c8a1803 100644 (file)
  * to the project. For more information see the website or contact
  * the copyright holders.
  *
- * $Id: kernel_routes.c,v 1.26 2007/09/13 16:08:13 bernd67 Exp $
+ * $Id: kernel_routes.c,v 1.27 2007/09/17 22:55:40 bernd67 Exp $
  */
 
-
-
 #include "kernel_routes.h"
-#include "link_set.h"
-#include "olsr.h"
-#include "log.h"
-#include <net/if.h>
-#include <sys/ioctl.h>
-#include <unistd.h>
 
 #if !LINUX_POLICY_ROUTING
+#include "log.h"
+
+static int delete_all_inet_gws(void);
+
+#else /* !LINUX_POLICY_ROUTING */
+
+#include <assert.h>
+#include <linux/rtnetlink.h>
+
+struct olsr_rtreq
+{
+       struct nlmsghdr         n;
+       struct rtmsg            r;
+       char                    buf[512];
+};
+
+static void olsr_netlink_addreq(struct olsr_rtreq *req, int type, void *data, int len)
+{
+       struct rtattr *rta = (struct rtattr*)(((char*)req) + NLMSG_ALIGN(req->n.nlmsg_len));
+       req->n.nlmsg_len = NLMSG_ALIGN(req->n.nlmsg_len) + RTA_LENGTH(len);
+       assert(req->n.nlmsg_len < sizeof(struct olsr_rtreq));
+       rta->rta_type = type;
+       rta->rta_len = RTA_LENGTH(len);
+       memcpy(RTA_DATA(rta), data, len);
+}
+
+static int olsr_netlink_route(struct rt_entry *rt, olsr_u8_t family, olsr_u8_t rttable, __u16 cmd)
+{
+       int ret = 0;
+       struct olsr_rtreq req;
+       struct iovec iov;
+       struct sockaddr_nl nladdr;
+       struct msghdr msg =
+       {
+               &nladdr,
+               sizeof(nladdr),
+               &iov,
+               1,
+               NULL,
+               0,
+               0
+       };
+       olsr_u32_t metric = 1;
+       struct rt_nexthop* nexthop = (RTM_NEWROUTE == cmd) ?
+               &rt->rt_best->rtp_nexthop : &rt->rt_nexthop;
 
+       memset(&req, 0, sizeof(req));
+       req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
+       req.n.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_EXCL|NLM_F_ACK;
+       req.n.nlmsg_type = cmd;
+       req.r.rtm_family = family;
+       req.r.rtm_table = rttable;
+       req.r.rtm_protocol = RTPROT_BOOT;
+       req.r.rtm_scope = RT_SCOPE_LINK;
+       req.r.rtm_type = RTN_UNICAST;
+       req.r.rtm_dst_len = rt->rt_dst.prefix_len;
+
+       if (AF_INET == family)
+       {
+               if (rt->rt_dst.prefix.v4 != nexthop->gateway.v4)
+               {
+                       olsr_netlink_addreq(&req, RTA_GATEWAY, &nexthop->gateway.v4, sizeof(nexthop->gateway.v4));
+                       req.r.rtm_scope = RT_SCOPE_UNIVERSE;
+                       metric = RT_METRIC_DEFAULT;
+               }
+               olsr_netlink_addreq(&req, RTA_DST, &rt->rt_dst.prefix.v4, sizeof(rt->rt_dst.prefix.v4));
+       }
+       else
+       {
+               if (0 != memcmp(&rt->rt_dst.prefix.v6, &nexthop->gateway.v6, sizeof(nexthop->gateway.v6)))
+               {
+                       olsr_netlink_addreq(&req, RTA_GATEWAY, &nexthop->gateway.v6, sizeof(nexthop->gateway.v6));
+                       req.r.rtm_scope = RT_SCOPE_UNIVERSE;
+                       metric = RT_METRIC_DEFAULT;
+               }
+               olsr_netlink_addreq(&req, RTA_DST, &rt->rt_dst.prefix.v6, sizeof(rt->rt_dst.prefix.v6));
+       }
+       //!!!olsr_netlink_addreq(&req, RTA_PRIORITY, &rt->rt_best->rtp_metric.hops, sizeof(rt->rt_best->rtp_metric.hops));
+       olsr_netlink_addreq(&req, RTA_PRIORITY, &metric, sizeof(metric));
+       olsr_netlink_addreq(&req, RTA_OIF, &nexthop->iif_index, sizeof(nexthop->iif_index));
+       iov.iov_base = &req.n;
+       iov.iov_len = req.n.nlmsg_len;
+       memset(&nladdr, 0, sizeof(nladdr));
+       nladdr.nl_family = AF_NETLINK;
+       if (0 <= (ret = sendmsg(olsr_cnf->rtnl_s, &msg, 0)))
+       {
+               iov.iov_base = req.buf;
+               iov.iov_len = sizeof(req.buf);
+               if (0 < (ret = recvmsg(olsr_cnf->rtnl_s, &msg, 0)))
+               {
+                       struct nlmsghdr* h = (struct nlmsghdr*)req.buf;
+                       while (NLMSG_OK(h, (unsigned int)ret)) {
+                               if (NLMSG_DONE == h->nlmsg_type) break;
+                               if (NLMSG_ERROR == h->nlmsg_type)
+                               {
+                                       if (NLMSG_LENGTH(sizeof(struct nlmsgerr) <= h->nlmsg_len))
+                                       {
+                                               struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
+                                               errno = -l_err->error;
+                                               if (0 != errno) ret = -1;
+                                       }
+                                       break;
+                               }
+                               h = NLMSG_NEXT(h, ret);
+                       }
+               }
+               if (0 <= ret && olsr_cnf->open_ipc)
+               {
+                       ipc_route_send_rtentry(
+                               &rt->rt_dst.prefix,
+                               &nexthop->gateway,
+                               metric,
+                               RTM_NEWROUTE == cmd,
+                               if_ifwithindex_name(nexthop->iif_index));
+               }
+       }
+       return ret;
+}
+#endif /* LINUX_POLICY_ROUTING */
 
 /**
  * Insert a route in the kernel routing table
 int
 olsr_ioctl_add_route(struct rt_entry *rt)
 {
+#if !LINUX_POLICY_ROUTING
   struct rtentry kernel_route;
   union olsr_ip_addr mask;
   int rslt;
+#endif /* LINUX_POLICY_ROUTING */
 
   OLSR_PRINTF(2, "KERN: Adding %s\n", olsr_rtp_to_string(rt->rt_best));
 
+#if !LINUX_POLICY_ROUTING
   memset(&kernel_route, 0, sizeof(struct rtentry));
 
   ((struct sockaddr_in*)&kernel_route.rt_dst)->sin_family = AF_INET;
@@ -115,6 +228,18 @@ olsr_ioctl_add_route(struct rt_entry *rt)
   }
 
   return rslt;
+#else /* !LINUX_POLICY_ROUTING */
+       if (0 == rt->rt_dst.prefix_len && 253 > olsr_cnf->rttable)
+       {
+               /*
+                * Users start whining about not having internet with policy
+                * routing activated and no static default route in table 254.
+                * We maintain a fallback defroute in the default=253 table.
+                */
+               olsr_netlink_route(rt, AF_INET, 253, RTM_NEWROUTE);
+       }
+       return olsr_netlink_route(rt, AF_INET, olsr_cnf->rttable, RTM_NEWROUTE);
+#endif /* LINUX_POLICY_ROUTING */
 }
 
 
@@ -128,7 +253,7 @@ olsr_ioctl_add_route(struct rt_entry *rt)
 int
 olsr_ioctl_add_route6(struct rt_entry *rt)
 {
-
+#if !LINUX_POLICY_ROUTING
   struct in6_rtmsg kernel_route;
   int rslt;
 
@@ -162,6 +287,9 @@ olsr_ioctl_add_route6(struct rt_entry *rt)
   }
 
   return rslt;
+#else /* !LINUX_POLICY_ROUTING */
+       return olsr_netlink_route(rt, AF_INET6, olsr_cnf->rttable, RTM_NEWROUTE);
+#endif /* LINUX_POLICY_ROUTING */
 }
 
 
@@ -175,12 +303,15 @@ olsr_ioctl_add_route6(struct rt_entry *rt)
 int
 olsr_ioctl_del_route(struct rt_entry *rt)
 {
+#if !LINUX_POLICY_ROUTING
   struct rtentry kernel_route;
   union olsr_ip_addr mask;
   int rslt;
+#endif /* LINUX_POLICY_ROUTING */
 
   OLSR_PRINTF(2, "KERN: Deleting %s\n", olsr_rt_to_string(rt));
 
+#if !LINUX_POLICY_ROUTING
   memset(&kernel_route,0,sizeof(struct rtentry));
 
   ((struct sockaddr_in*)&kernel_route.rt_dst)->sin_family = AF_INET;
@@ -218,6 +349,16 @@ olsr_ioctl_del_route(struct rt_entry *rt)
   }
 
   return rslt;
+#else /* !LINUX_POLICY_ROUTING */
+       if (0 == rt->rt_dst.prefix_len && 253 > olsr_cnf->rttable)
+       {
+               /*
+                * Also remove the fallback default route
+                */
+               olsr_netlink_route(rt, AF_INET, 253, RTM_DELROUTE);
+       }
+       return olsr_netlink_route(rt, AF_INET, olsr_cnf->rttable, RTM_DELROUTE);
+#endif /* LINUX_POLICY_ROUTING */
 }
 
 
@@ -231,15 +372,16 @@ olsr_ioctl_del_route(struct rt_entry *rt)
 int
 olsr_ioctl_del_route6(struct rt_entry *rt)
 {
-
+#if !LINUX_POLICY_ROUTING
   struct in6_rtmsg kernel_route;
   int rslt;
+#endif /* LINUX_POLICY_ROUTING */
 
   OLSR_PRINTF(2, "KERN: Deleting %s\n", olsr_rt_to_string(rt));
 
+#if !LINUX_POLICY_ROUTING
   memset(&kernel_route,0,sizeof(struct in6_rtmsg));
 
-
   COPY_IP(&kernel_route.rtmsg_dst, &rt->rt_dst.prefix);
   kernel_route.rtmsg_dst_len = rt->rt_dst.prefix_len;
 
@@ -257,12 +399,13 @@ olsr_ioctl_del_route6(struct rt_entry *rt)
   }
 
   return rslt;
+#else /* !LINUX_POLICY_ROUTING */
+       return olsr_netlink_route(rt, AF_INET6, olsr_cnf->rttable, RTM_DELROUTE);
+#endif /* LINUX_POLICY_ROUTING */
 }
 
-
-
-int
-delete_all_inet_gws(void)
+#if !LINUX_POLICY_ROUTING
+static int delete_all_inet_gws(void)
 {  
   int s;
   char buf[BUFSIZ], *cp, *cplim;
@@ -275,7 +418,6 @@ delete_all_inet_gws(void)
   if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 
     {
       olsr_syslog(OLSR_LOG_ERR, "socket: %m");
-      close(s);
       return -1;
     }
   
@@ -326,162 +468,8 @@ delete_all_inet_gws(void)
          OLSR_PRINTF(1, "YES\n");
     }  
   close(s);
-  return 0;
-       
-}
-
-#else /* LINUX_POLICY_ROUTING */
-
-#include <assert.h>
-#include <linux/types.h>
-#include <linux/rtnetlink.h>
-
-struct olsr_rtreq
-{
-       struct nlmsghdr         n;
-       struct rtmsg            r;
-       char                    buf[512];
-};
-
-static void olsr_netlink_addreq(struct olsr_rtreq *req, int type, void *data, int len)
-{
-       struct rtattr *rta = (struct rtattr*)(((char*)req) + NLMSG_ALIGN(req->n.nlmsg_len));
-       req->n.nlmsg_len = NLMSG_ALIGN(req->n.nlmsg_len) + RTA_LENGTH(len);
-       assert(req->n.nlmsg_len < sizeof(struct olsr_rtreq));
-       rta->rta_type = type;
-       rta->rta_len = RTA_LENGTH(len);
-       memcpy(RTA_DATA(rta), data, len);
-}
-
-static int olsr_netlink_route(struct rt_entry *rt, olsr_u8_t family, olsr_u8_t rttable, __u16 cmd)
-{
-       int ret = 0;
-       struct olsr_rtreq req;
-       struct iovec iov;
-       struct sockaddr_nl nladdr;
-       struct msghdr msg =
-       {
-               (void*)&nladdr,
-               sizeof(nladdr),
-               &iov,
-               1,
-               NULL,
-               0,
-               0
-       };
-       olsr_u32_t metric = 1;
-       struct rt_nexthop* nexthop = (RTM_NEWROUTE == cmd) ?
-               &rt->rt_best->rtp_nexthop : &rt->rt_nexthop;
-
-       memset(&req, 0, sizeof(req));
-       req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
-       req.n.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_EXCL|NLM_F_ACK;
-       req.n.nlmsg_type = cmd;
-       req.r.rtm_family = family;
-       req.r.rtm_table = rttable;
-       req.r.rtm_protocol = RTPROT_BOOT;
-       req.r.rtm_scope = RT_SCOPE_LINK;
-       req.r.rtm_type = RTN_UNICAST;
-       req.r.rtm_dst_len = rt->rt_dst.prefix_len;
-
-       if (AF_INET == family)
-       {
-               if (rt->rt_dst.prefix.v4 != nexthop->gateway.v4)
-               {
-                       olsr_netlink_addreq(&req, RTA_GATEWAY, &nexthop->gateway.v4, sizeof(nexthop->gateway.v4));
-                       req.r.rtm_scope = RT_SCOPE_UNIVERSE;
-                       metric = RT_METRIC_DEFAULT;
-               }
-               olsr_netlink_addreq(&req, RTA_DST, &rt->rt_dst.prefix.v4, sizeof(rt->rt_dst.prefix.v4));
-       }
-       else
-       {
-               if (0 != memcmp(&rt->rt_dst.prefix.v6, &nexthop->gateway.v6, sizeof(nexthop->gateway.v6)))
-               {
-                       olsr_netlink_addreq(&req, RTA_GATEWAY, &nexthop->gateway.v6, sizeof(nexthop->gateway.v6));
-                       req.r.rtm_scope = RT_SCOPE_UNIVERSE;
-                       metric = RT_METRIC_DEFAULT;
-               }
-               olsr_netlink_addreq(&req, RTA_DST, &rt->rt_dst.prefix.v6, sizeof(rt->rt_dst.prefix.v6));
-       }
-       //!!!olsr_netlink_addreq(&req, RTA_PRIORITY, &rt->rt_best->rtp_metric.hops, sizeof(rt->rt_best->rtp_metric.hops));
-       olsr_netlink_addreq(&req, RTA_PRIORITY, &metric, sizeof(metric));
-       olsr_netlink_addreq(&req, RTA_OIF, &nexthop->iif_index, sizeof(nexthop->iif_index));
-       iov.iov_base = (void*)&req.n;
-       iov.iov_len = req.n.nlmsg_len;
-       memset(&nladdr, 0, sizeof(nladdr));
-       nladdr.nl_family = AF_NETLINK;
-       if (0 <= (ret = sendmsg(olsr_cnf->rtnl_s, &msg, 0)))
-       {
-               iov.iov_base = req.buf;
-               iov.iov_len = sizeof(req.buf);
-               if (0 < (ret = recvmsg(olsr_cnf->rtnl_s, &msg, 0)))
-               {
-                       struct nlmsghdr* h = (struct nlmsghdr*)req.buf;
-                       while (NLMSG_OK(h, (unsigned int)ret)) {
-                               if (NLMSG_DONE == h->nlmsg_type) break;
-                               if (NLMSG_ERROR == h->nlmsg_type)
-                               {
-                                       if (NLMSG_LENGTH(sizeof(struct nlmsgerr) <= h->nlmsg_len))
-                                       {
-                                               struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
-                                               errno = -l_err->error;
-                                               if (0 != errno) ret = -1;
-                                       }
-                                       break;
-                               }
-                               h = NLMSG_NEXT(h, ret);
-                       }
-               }
-               if (0 <= ret && olsr_cnf->open_ipc)
-               {
-                       ipc_route_send_rtentry(
-                               &rt->rt_dst.prefix,
-                               &nexthop->gateway,
-                               metric,
-                               RTM_NEWROUTE == cmd,
-                               if_ifwithindex_name(nexthop->iif_index));
-               }
-       }
-       return ret;
-}
-
-int olsr_ioctl_add_route(struct rt_entry *rt)
-{
-       if (0 == rt->rt_dst.prefix_len && 253 > olsr_cnf->rttable)
-       {
-               /*
-                * Users start whining about not having internet with policy
-                * routing activated and no static default route in table 254.
-                * We maintain a fallback defroute in the default=253 table.
-                */
-               olsr_netlink_route(rt, AF_INET, 253, RTM_NEWROUTE);
-       }
-       return olsr_netlink_route(rt, AF_INET, olsr_cnf->rttable, RTM_NEWROUTE);
-}
-
-int olsr_ioctl_del_route(struct rt_entry *rt)
-{
-       if (0 == rt->rt_dst.prefix_len && 253 > olsr_cnf->rttable)
-       {
-               /*
-                * Also remove the fallback default route
-                */
-               olsr_netlink_route(rt, AF_INET, 253, RTM_DELROUTE);
-       }
-       return olsr_netlink_route(rt, AF_INET, olsr_cnf->rttable, RTM_DELROUTE);
+  return 0;       
 }
-
-int olsr_ioctl_add_route6(struct rt_entry *rt)
-{
-       return olsr_netlink_route(rt, AF_INET6, olsr_cnf->rttable, RTM_NEWROUTE);
-}
-
-int olsr_ioctl_del_route6(struct rt_entry *rt)
-{
-       return olsr_netlink_route(rt, AF_INET6, olsr_cnf->rttable, RTM_DELROUTE);
-}
-
 #endif /* LINUX_POLICY_ROUTING */
 
 /*