xorp

netlink_socket_utilities.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-2012 XORP, Inc and Others
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 
00021 #ifndef __FEA_DATA_PLANE_CONTROL_SOCKET_NETLINK_SOCKET_UTILITIES_HH__
00022 #define __FEA_DATA_PLANE_CONTROL_SOCKET_NETLINK_SOCKET_UTILITIES_HH__
00023 
00024 #include <xorp_config.h>
00025 #ifdef HAVE_NETLINK_SOCKETS
00026 
00027 #include "libxorp/xorp.h"
00028 
00029 #ifdef HAVE_LINUX_TYPES_H
00030 #include <linux/types.h>
00031 #endif
00032 #ifdef HAVE_LINUX_RTNETLINK_H
00033 #include <linux/rtnetlink.h>
00034 #endif
00035 
00036 #include "fea/fte.hh"
00037 #include "fea/iftree.hh"
00038 
00039 
00040 //
00041 // Conditionally re-define some of the netlink-related macros that are not
00042 // defined properly and might generate alignment-related compilation
00043 // warning on some architectures (e.g, ARM/XScale) if we use
00044 // "-Wcast-align" compilation flag.
00045 //
00046 #ifdef HAVE_BROKEN_MACRO_NLMSG_NEXT
00047 #undef NLMSG_NEXT
00048 #define NLMSG_NEXT(nlh, len)    ((len) -= NLMSG_ALIGN((nlh)->nlmsg_len), \
00049                   (struct nlmsghdr*)(void*)(((char*)(nlh)) + NLMSG_ALIGN((nlh)->nlmsg_len)))
00050 #endif
00051 
00052 #ifdef HAVE_BROKEN_MACRO_RTA_NEXT
00053 #undef RTA_NEXT
00054 #define RTA_NEXT(rta, attrlen)  ((attrlen) -= RTA_ALIGN((rta)->rta_len), \
00055                     (struct rtattr*)(void*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
00056 #endif
00057 
00058 #ifdef HAVE_BROKEN_MACRO_IFA_RTA
00059 #undef IFA_RTA
00060 #define IFA_RTA(r)      ((struct rtattr*)(void*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
00061 #endif
00062 
00063 #ifdef HAVE_BROKEN_MACRO_IFLA_RTA
00064 #undef IFLA_RTA
00065 #define IFLA_RTA(r)     ((struct rtattr*)(void*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))))
00066 #endif
00067 
00068 #ifdef HAVE_BROKEN_MACRO_RTM_RTA
00069 #undef RTM_RTA
00070 #define RTM_RTA(r)      ((struct rtattr*)(void *)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct rtmsg))))
00071 #endif
00072 
00073 
00074 //
00075 // TODO: XXX: a temporary definition of RTPROT_XORP (e.g., in case of Linux)
00076 // that is used to mark the routes installed by XORP.
00077 // XXX: RTPROT_XORP must be unique
00078 // (see <linux/rtnetlink.h> for definition of all RTPROT_* protocols).
00079 //
00080 #if defined(RTPROT_UNSPEC) && !defined(RTPROT_XORP)
00081 #define RTPROT_XORP 14
00082 #endif
00083 
00084 class NetlinkSocket;
00085 class NetlinkSocketReader;
00086 class FibConfig;
00087 
00091 class NlmUtils {
00092 public:
00100     static string nlm_msg_type(uint32_t m);
00101 
00102     static string nlm_print_msg(const vector<uint8_t>& message);
00103 
00113     static void get_rtattr(const struct rtattr* rtattr, int rta_len,
00114                const struct rtattr* rta_array[],
00115                size_t rta_array_n);
00116     
00127     static int  nlm_get_to_fte_cfg(const IfTree& iftree, FteX& fte,
00128                    const struct nlmsghdr* nlh,
00129                    const struct rtmsg* rtmsg, int rta_len,
00130                    const FibConfig& fibconfig, string& err_msg);
00131 
00142     static int check_netlink_request(NetlinkSocketReader& ns_reader,
00143                      NetlinkSocket& ns,
00144                      uint32_t seqno,
00145                      int& last_errno,
00146                      string& error_msg);
00147 
00148     static int nlm_decode_ipvx_address(int family, const struct rtattr* rtattr,
00149                        IPvX& ipvx_addr, bool& is_set, string& error_msg);
00150 
00151 
00152     static int nlm_decode_ipvx_interface_address(const struct ifinfomsg* ifinfomsg,
00153                          const struct rtattr* rtattr,
00154                          IPvX& ipvx_addr, bool& is_set,
00155                          string& error_msg);
00156 
00157     static void nlm_cond_newlink_to_fea_cfg(const IfTree& user_cfg, IfTree& iftree,
00158                         const struct ifinfomsg* ifinfomsg,
00159                         int rta_len, bool& modified);
00160 
00161 
00162     static void nlm_dellink_to_fea_cfg(IfTree& iftree, const struct ifinfomsg* ifinfomsg,
00163                        int rta_len, bool& modified);
00164 
00165 
00166     static void nlm_cond_newdeladdr_to_fea_cfg(const IfTree& user_config, IfTree& iftree,
00167                            const struct ifaddrmsg* ifaddrmsg,
00168                            int rta_len, bool is_deleted, bool& modified);
00169 
00170 };
00171 
00172 #endif
00173 #endif // __FEA_DATA_PLANE_CONTROL_SOCKET_NETLINK_SOCKET_UTILITIES_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations