xorp

system_utilities.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-2009 XORP, Inc.
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, Version 2, June
00007 // 1991 as published by the Free Software Foundation. Redistribution
00008 // and/or modification of this program under the terms of any other
00009 // version of the GNU General Public License is not permitted.
00010 // 
00011 // This program is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00014 // see the GNU General Public License, Version 2, a copy of which can be
00015 // found in the XORP LICENSE.gpl file.
00016 // 
00017 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00018 // http://xorp.net
00019 
00020 // $XORP: xorp/fea/data_plane/control_socket/system_utilities.hh,v 1.6 2008/10/02 21:56:54 bms Exp $
00021 
00022 #ifndef __FEA_DATA_PLANE_CONTROL_SOCKET_SYSTEM_UTILITIES_HH__
00023 #define __FEA_DATA_PLANE_CONTROL_SOCKET_SYSTEM_UTILITIES_HH__
00024 
00025 
00026 #include "libxorp/xorp.h"
00027 #include "libxorp/ipvx.hh"
00028 
00029 #include "libproto/packet.hh"
00030 
00031 
00032 //
00033 // Conditionally re-define some of the system macros that are not
00034 // defined properly and might generate alignment-related compilation
00035 // warning on some architectures (e.g, ARM/XScale) if we use
00036 // "-Wcast-align" compilation flag.
00037 //
00038 #ifdef HAVE_BROKEN_MACRO_CMSG_NXTHDR
00039 #if ((! defined(__CMSG_ALIGN)) && (! defined(_ALIGN)))
00040 #error "The system has broken CMSG_NXTHDR, but we don't know how to re-define it. Fix the alignment compilation error of the CMSG_NXTHDR macro in your system header files."
00041 #endif
00042 
00043 #ifndef __CMSG_ALIGN
00044 #define __CMSG_ALIGN(n)     _ALIGN(n)
00045 #endif
00046 
00047 #undef CMSG_NXTHDR
00048 #define CMSG_NXTHDR(mhdr, cmsg) \
00049     (((caddr_t)(cmsg) + __CMSG_ALIGN((cmsg)->cmsg_len) + \
00050                 __CMSG_ALIGN(sizeof(struct cmsghdr)) > \
00051         ((caddr_t)(mhdr)->msg_control) + (mhdr)->msg_controllen) ? \
00052         (struct cmsghdr *)NULL : \
00053         (struct cmsghdr *)(void *)((caddr_t)(cmsg) + __CMSG_ALIGN((cmsg)->cmsg_len)))
00054 #endif // HAVE_BROKEN_MACRO_CMSG_NXTHDR
00055 
00056 //
00057 // XXX: In case of KAME the local interface index (also the link-local
00058 // scope_id) is encoded in the third and fourth octet of an IPv6
00059 // address (for link-local unicast/multicast addresses or
00060 // interface-local multicast addresses only).
00061 // E.g., see the sa6_recoverscope() implementation inside file
00062 // "sys/netinet6/scope6.c" on KAME-derived IPv6 stack.
00063 //
00064 #ifdef HAVE_IPV6
00065 inline IPv6
00066 system_adjust_ipv6_recv(const IPv6& ipv6)
00067 {
00068 #ifdef IPV6_STACK_KAME
00069     in6_addr in6_addr;
00070     ipv6.copy_out(in6_addr);
00071     if (IN6_IS_ADDR_LINKLOCAL(&in6_addr)
00072     || IN6_IS_ADDR_MC_LINKLOCAL(&in6_addr)
00073     || IN6_IS_ADDR_MC_NODELOCAL(&in6_addr)) {
00074     in6_addr.s6_addr[2] = 0;
00075     in6_addr.s6_addr[3] = 0;
00076     return IPv6(in6_addr);
00077     }
00078 #endif // IPV6_STACK_KAME
00079 
00080     return ipv6;
00081 }
00082 #endif // HAVE_IPV6
00083 
00084 inline IPvX
00085 system_adjust_ipvx_recv(const IPvX& ipvx)
00086 {
00087 #ifndef HAVE_IPV6
00088     return ipvx;
00089 #else
00090     if (! ipvx.is_ipv6())
00091     return ipvx;
00092     return (system_adjust_ipv6_recv(ipvx.get_ipv6()));
00093 #endif // HAVE_IPV6
00094 }
00095 
00096 //
00097 // XXX: In case of KAME the local interface index should be set as
00098 // the link-local scope_id ((for link-local unicast/multicast addresses or
00099 // interface-local multicast addresses only).
00100 //
00101 #ifdef HAVE_IPV6
00102 inline void
00103 system_adjust_sockaddr_in6_send(struct sockaddr_in6& sin6, uint16_t zone_id)
00104 {
00105 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
00106 #ifdef IPV6_STACK_KAME
00107     if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)
00108     || IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)
00109     || IN6_IS_ADDR_MC_NODELOCAL(&sin6.sin6_addr)) {
00110     sin6.sin6_scope_id = zone_id;
00111     }
00112 #endif // IPV6_STACK_KAME
00113 #endif // HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
00114 
00115     UNUSED(sin6);
00116     UNUSED(zone_id);
00117 }
00118 #endif // HAVE_IPV6
00119 
00120 //
00121 // XXX: In case of KAME sometimes the local interface index is encoded
00122 // in the third and fourth octet of an IPv6 address (for link-local
00123 // unicast/multicast addresses) when we add an unicast route to the system.
00124 // E.g., see /usr/src/sbin/route/route.c on FreeBSD-6.2 and search for
00125 // the __KAME__ marker.
00126 //
00127 #ifdef HAVE_IPV6
00128 inline void
00129 system_adjust_sockaddr_in6_route(struct sockaddr_in6& sin6, uint16_t iface_id)
00130 {
00131 #ifdef IPV6_STACK_KAME
00132     if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)
00133     || IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) {
00134     embed_16(&sin6.sin6_addr.s6_addr[2], iface_id);
00135 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
00136     sin6.sin6_scope_id = 0;
00137 #endif
00138     }
00139 #endif // IPV6_STACK_KAME
00140 
00141     UNUSED(sin6);
00142     UNUSED(iface_id);
00143 }
00144 #endif // HAVE_IPV6
00145 
00146 #endif // _FEA_DATA_PLANE_CONTROL_SOCKET_SYSTEM_UTILITIES_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations