[Olsr-cvs] olsrd-current/src/linux kernel_routes.c,1.26,1.27

Bernd Petrovitsch (spam-protected)
Tue Sep 18 00:55:42 CEST 2007


Update of /cvsroot/olsrd/olsrd-current/src/linux
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv933/src/linux

Modified Files:
	kernel_routes.c 
Log Message:
* made delete_all_inet_gws() static since it is not used elsewhere
* localized the netlink version restoring the former logging
* killed a superflous close()
No functional change, just moving code around.


Index: kernel_routes.c
===================================================================
RCS file: /cvsroot/olsrd/olsrd-current/src/linux/kernel_routes.c,v
retrieving revision 1.26
retrieving revision 1.27
diff -C2 -d -r1.26 -r1.27
*** kernel_routes.c	13 Sep 2007 16:08:13 -0000	1.26
--- kernel_routes.c	17 Sep 2007 22:55:40 -0000	1.27
***************
*** 41,56 ****
   */
  
- 
- 
  #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
  
  
  /**
--- 41,166 ----
   */
  
  #include "kernel_routes.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 */
  
  /**
***************
*** 64,73 ****
--- 174,186 ----
  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));
  
***************
*** 116,119 ****
--- 229,244 ----
  
    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 */
  }
  
***************
*** 129,133 ****
  olsr_ioctl_add_route6(struct rt_entry *rt)
  {
! 
    struct in6_rtmsg kernel_route;
    int rslt;
--- 254,258 ----
  olsr_ioctl_add_route6(struct rt_entry *rt)
  {
! #if !LINUX_POLICY_ROUTING
    struct in6_rtmsg kernel_route;
    int rslt;
***************
*** 163,166 ****
--- 288,294 ----
  
    return rslt;
+ #else /* !LINUX_POLICY_ROUTING */
+ 	return olsr_netlink_route(rt, AF_INET6, olsr_cnf->rttable, RTM_NEWROUTE);
+ #endif /* LINUX_POLICY_ROUTING */
  }
  
***************
*** 176,185 ****
--- 304,316 ----
  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));
  
***************
*** 219,222 ****
--- 350,363 ----
  
    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 */
  }
  
***************
*** 232,244 ****
  olsr_ioctl_del_route6(struct rt_entry *rt)
  {
! 
    struct in6_rtmsg kernel_route;
    int rslt;
  
    OLSR_PRINTF(2, "KERN: Deleting %s\n", olsr_rt_to_string(rt));
  
    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;
--- 373,386 ----
  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;
***************
*** 258,267 ****
  
    return rslt;
  }
  
! 
! 
! int
! delete_all_inet_gws(void)
  {  
    int s;
--- 400,410 ----
  
    return rslt;
+ #else /* !LINUX_POLICY_ROUTING */
+ 	return olsr_netlink_route(rt, AF_INET6, olsr_cnf->rttable, RTM_DELROUTE);
+ #endif /* LINUX_POLICY_ROUTING */
  }
  
! #if !LINUX_POLICY_ROUTING
! static int delete_all_inet_gws(void)
  {  
    int s;
***************
*** 276,280 ****
      {
        olsr_syslog(OLSR_LOG_ERR, "socket: %m");
-       close(s);
        return -1;
      }
--- 419,422 ----
***************
*** 327,486 ****
      }  
    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);
! }
! 
! 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 */
  
--- 469,474 ----
      }  
    close(s);
!   return 0;       
  }
  #endif /* LINUX_POLICY_ROUTING */
  





More information about the Olsr-cvs mailing list