| 1 |
#include <stdio.h> |
| 2 |
#include "protoRouteMgr.h" |
| 3 |
#include "protoPipe.h" |
| 4 |
#include "protoDebug.h" |
| 5 |
|
| 6 |
#include <sys/types.h> |
| 7 |
#include <sys/stat.h> |
| 8 |
#include <fcntl.h> |
| 9 |
#include <unistd.h> |
| 10 |
#include <sys/socket.h> |
| 11 |
#include <sys/ioctl.h> |
| 12 |
#include <netinet/in.h> |
| 13 |
#include <arpa/inet.h> |
| 14 |
#include <net/if.h> |
| 15 |
#include <errno.h> |
| 16 |
|
| 17 |
// linux specific includes |
| 18 |
#include <asm/types.h> |
| 19 |
#include <linux/netlink.h> |
| 20 |
#include <linux/rtnetlink.h> |
| 21 |
|
| 22 |
namespace ZebraNameSpace |
| 23 |
{ |
| 24 |
const int ZEBRA_VERSION = 1; |
| 25 |
const int ZEBRA_MAX_PACKET_SIZE = 4096; |
| 26 |
const int ZEBRA_PORT = 2600; |
| 27 |
const int ZEBRA_MODE = ProtoAddress::IPv4; |
| 28 |
const int ZEBRA_HEADER_SIZE = 6; |
| 29 |
|
| 30 |
/* Zebra API message flag. */ |
| 31 |
const int ZAPI_MESSAGE_NEXTHOP = 0x01; |
| 32 |
const int ZAPI_MESSAGE_IFINDEX = 0x02; |
| 33 |
const int ZAPI_MESSAGE_DISTANCE = 0x04; |
| 34 |
const int ZAPI_MESSAGE_METRIC = 0x08; |
| 35 |
|
| 36 |
/* Zebra message types. */ |
| 37 |
const int ZEBRA_INTERFACE_ADD = 1; |
| 38 |
const int ZEBRA_INTERFACE_DELETE = 2; |
| 39 |
const int ZEBRA_INTERFACE_ADDRESS_ADD = 3; |
| 40 |
const int ZEBRA_INTERFACE_ADDRESS_DELETE = 4; |
| 41 |
const int ZEBRA_INTERFACE_UP = 5; |
| 42 |
const int ZEBRA_INTERFACE_DOWN = 6; |
| 43 |
const int ZEBRA_IPV4_ROUTE_ADD = 7; |
| 44 |
const int ZEBRA_IPV4_ROUTE_DELETE = 8; |
| 45 |
const int ZEBRA_IPV6_ROUTE_ADD = 9; |
| 46 |
const int ZEBRA_IPV6_ROUTE_DELETE = 10; |
| 47 |
const int ZEBRA_REDISTRIBUTE_ADD = 11; |
| 48 |
const int ZEBRA_REDISTRIBUTE_DELETE = 12; |
| 49 |
const int ZEBRA_REDISTRIBUTE_DEFAULT_ADD = 13; |
| 50 |
const int ZEBRA_REDISTRIBUTE_DEFAULT_DELETE = 14; |
| 51 |
const int ZEBRA_IPV4_NEXTHOP_LOOKUP = 15; |
| 52 |
const int ZEBRA_IPV6_NEXTHOP_LOOKUP = 16; |
| 53 |
const int ZEBRA_IPV4_IMPORT_LOOKUP = 17; |
| 54 |
const int ZEBRA_IPV6_IMPORT_LOOKUP = 18; |
| 55 |
const int ZEBRA_INTERFACE_RENAME = 19; |
| 56 |
const int ZEBRA_ROUTER_ID_ADD = 20; |
| 57 |
const int ZEBRA_ROUTER_ID_DELETE = 21; |
| 58 |
const int ZEBRA_ROUTER_ID_UPDATE = 22; |
| 59 |
const int ZEBRA_LINKMETRICS_SUBSCRIBE = 23; |
| 60 |
const int ZEBRA_LINKMETRICS_UNSUBSCRIBE = 24; |
| 61 |
const int ZEBRA_LINKMETRICS_METRICS = 25; |
| 62 |
const int ZEBRA_LINKMETRICS_STATUS = 26; |
| 63 |
const int ZEBRA_LINKMETRICS_METRICS_RQST = 27; |
| 64 |
const int ZEBRA_MESSAGE_MAX = 28; |
| 65 |
const int ZEBRA_HEADER_MARKER = 255; |
| 66 |
|
| 67 |
/* Zebra route's types. */ |
| 68 |
const int ZEBRA_ROUTE_SYSTEM = 0; |
| 69 |
const int ZEBRA_ROUTE_KERNEL = 1; |
| 70 |
const int ZEBRA_ROUTE_CONNECT = 2; |
| 71 |
const int ZEBRA_ROUTE_STATIC = 3; |
| 72 |
const int ZEBRA_ROUTE_RIP = 4; |
| 73 |
const int ZEBRA_ROUTE_RIPNG = 5; |
| 74 |
const int ZEBRA_ROUTE_OSPF = 6; |
| 75 |
const int ZEBRA_ROUTE_OSPF6 = 7; |
| 76 |
const int ZEBRA_ROUTE_ISIS = 8; |
| 77 |
const int ZEBRA_ROUTE_BGP = 9; |
| 78 |
const int ZEBRA_ROUTE_HSLS = 10; |
| 79 |
const int ZEBRA_ROUTE_MAX = 11; |
| 80 |
|
| 81 |
/* Zebra's family types. */ |
| 82 |
const int ZEBRA_FAMILY_IPV4 = 1; |
| 83 |
const int ZEBRA_FAMILY_IPV6 = 2; |
| 84 |
const int ZEBRA_FAMILY_MAX = 3; |
| 85 |
|
| 86 |
/* Error codes of zebra. */ |
| 87 |
const int ZEBRA_ERR_NOERROR = 0; |
| 88 |
const int ZEBRA_ERR_RTEXIST = -1; |
| 89 |
const int ZEBRA_ERR_RTUNREACH = -2; |
| 90 |
const int ZEBRA_ERR_EPERM = -3; |
| 91 |
const int ZEBRA_ERR_RTNOEXIST = -4; |
| 92 |
const int ZEBRA_ERR_KERNEL = -5; |
| 93 |
|
| 94 |
/* Zebra message flags */ |
| 95 |
const int ZEBRA_FLAG_INTERNAL = 0x01; |
| 96 |
const int ZEBRA_FLAG_SELFROUTE = 0x02; |
| 97 |
const int ZEBRA_FLAG_BLACKHOLE = 0x04; |
| 98 |
const int ZEBRA_FLAG_IBGP = 0x08; |
| 99 |
const int ZEBRA_FLAG_SELECTED = 0x10; |
| 100 |
const int ZEBRA_FLAG_CHANGED = 0x20; |
| 101 |
const int ZEBRA_FLAG_STATIC = 0x40; |
| 102 |
const int ZEBRA_FLAG_REJECT = 0x80; |
| 103 |
|
| 104 |
/* Zebra nexthop flags. */ |
| 105 |
const int ZEBRA_NEXTHOP_IFINDEX = 1; |
| 106 |
const int ZEBRA_NEXTHOP_IFNAME = 2; |
| 107 |
const int ZEBRA_NEXTHOP_IPV4 = 3; |
| 108 |
const int ZEBRA_NEXTHOP_IPV4_IFINDEX = 4; |
| 109 |
const int ZEBRA_NEXTHOP_IPV4_IFNAME = 5; |
| 110 |
const int ZEBRA_NEXTHOP_IPV6 = 6; |
| 111 |
const int ZEBRA_NEXTHOP_IPV6_IFINDEX = 7; |
| 112 |
const int ZEBRA_NEXTHOP_IPV6_IFNAME = 8; |
| 113 |
const int ZEBRA_NEXTHOP_BLACKHOLE = 9; |
| 114 |
|
| 115 |
#ifndef INADDR_LOOPBACK |
| 116 |
const int INADDR_LOOPBACK = 0x7f000001; /* Internet address 127.0.0.1. */ |
| 117 |
#endif |
| 118 |
|
| 119 |
/* Address family numbers from RFC1700. */ |
| 120 |
const int AFI_IP = 1; |
| 121 |
const int AFI_IP6 = 2; |
| 122 |
const int AFI_MAX = 3; |
| 123 |
|
| 124 |
/* Subsequent Address Family Identifier. */ |
| 125 |
const int SAFI_UNICAST = 1; |
| 126 |
const int SAFI_MULTICAST = 2; |
| 127 |
const int SAFI_UNICAST_MULTICAST = 3; |
| 128 |
const int SAFI_MPLS_VPN = 4; |
| 129 |
const int SAFI_MAX = 5; |
| 130 |
|
| 131 |
/* Filter direction. */ |
| 132 |
const int FILTER_IN = 0; |
| 133 |
const int FILTER_OUT = 1; |
| 134 |
const int FILTER_MAX = 2; |
| 135 |
|
| 136 |
/* Default Administrative Distance of each protocol. */ |
| 137 |
const int ZEBRA_KERNEL_DISTANCE_DEFAULT = 0; |
| 138 |
const int ZEBRA_CONNECT_DISTANCE_DEFAULT = 0; |
| 139 |
const int ZEBRA_STATIC_DISTANCE_DEFAULT = 1; |
| 140 |
const int ZEBRA_RIP_DISTANCE_DEFAULT = 120; |
| 141 |
const int ZEBRA_RIPNG_DISTANCE_DEFAULT = 120; |
| 142 |
const int ZEBRA_OSPF_DISTANCE_DEFAULT = 110; |
| 143 |
const int ZEBRA_OSPF6_DISTANCE_DEFAULT = 110; |
| 144 |
const int ZEBRA_ISIS_DISTANCE_DEFAULT = 115; |
| 145 |
const int ZEBRA_IBGP_DISTANCE_DEFAULT = 200; |
| 146 |
const int ZEBRA_EBGP_DISTANCE_DEFAULT = 20; |
| 147 |
} |
| 148 |
using namespace ZebraNameSpace; |
| 149 |
|
| 150 |
class ZebraRouteMgr : public ProtoRouteMgr |
| 151 |
{ |
| 152 |
public: |
| 153 |
ZebraRouteMgr(); |
| 154 |
~ZebraRouteMgr(); |
| 155 |
|
| 156 |
virtual bool Open(const void* userData = NULL); |
| 157 |
virtual bool IsOpen() const; |
| 158 |
virtual void Close(); |
| 159 |
|
| 160 |
virtual bool GetAllRoutes(ProtoAddress::Type addrType, |
| 161 |
ProtoRouteTable& routeTable); |
| 162 |
|
| 163 |
virtual bool GetRoute(const ProtoAddress& dst, |
| 164 |
unsigned int prefixLen, |
| 165 |
ProtoAddress& gw, |
| 166 |
unsigned int& ifIndex, |
| 167 |
int& metric); |
| 168 |
|
| 169 |
virtual bool SetRoute(const ProtoAddress& dst, |
| 170 |
unsigned int prefixLen, |
| 171 |
const ProtoAddress& gw, |
| 172 |
unsigned int ifIndex = 0, |
| 173 |
int metric = -1); |
| 174 |
|
| 175 |
virtual bool DeleteRoute(const ProtoAddress& dst, |
| 176 |
unsigned int prefixLen, |
| 177 |
const ProtoAddress& gw, |
| 178 |
unsigned int ifIndex = 0); |
| 179 |
|
| 180 |
virtual bool SetForwarding(bool state); |
| 181 |
|
| 182 |
virtual unsigned int GetInterfaceIndex(const char* interfaceName) |
| 183 |
{return ProtoSocket::GetInterfaceIndex(interfaceName);} |
| 184 |
|
| 185 |
virtual bool GetInterfaceAddressList(unsigned int ifIndex, |
| 186 |
ProtoAddress::Type addrType, |
| 187 |
ProtoAddressList& addrList); |
| 188 |
|
| 189 |
virtual bool GetInterfaceName(unsigned int interfaceIndex, |
| 190 |
char* buffer, |
| 191 |
unsigned int buflen) |
| 192 |
{return ProtoSocket::GetInterfaceName(interfaceIndex, buffer, buflen);} |
| 193 |
private: |
| 194 |
|
| 195 |
void OnClientSocketEvent(ProtoSocket& theSocket, |
| 196 |
ProtoSocket::Event theEvent); |
| 197 |
void PrintBuffer(char* buffer,int len,int dlevel); |
| 198 |
ProtoPipe zPipe; |
| 199 |
unsigned char ibuf[ZEBRA_MAX_PACKET_SIZE]; |
| 200 |
unsigned char obuf[ZEBRA_MAX_PACKET_SIZE]; |
| 201 |
|
| 202 |
int descriptor; |
| 203 |
pid_t pid; |
| 204 |
UINT32 sequence; |
| 205 |
|
| 206 |
}; // end class ZebraRouteMgr |