| 1 |
#include <stdio.h> |
| 2 |
#include "protoRouteMgr.h" |
| 3 |
#include "protoDebug.h" |
| 4 |
#include "unix/zebraRouteMgr.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 |
class LinuxRouteMgr : public ProtoRouteMgr |
| 23 |
{ |
| 24 |
public: |
| 25 |
LinuxRouteMgr(); |
| 26 |
~LinuxRouteMgr(); |
| 27 |
|
| 28 |
virtual bool Open(const void* userData = NULL); |
| 29 |
virtual bool IsOpen() const {return (descriptor >= 0);} |
| 30 |
virtual void Close(); |
| 31 |
|
| 32 |
virtual bool GetAllRoutes(ProtoAddress::Type addrType, |
| 33 |
ProtoRouteTable& routeTable); |
| 34 |
|
| 35 |
virtual bool GetRoute(const ProtoAddress& dst, |
| 36 |
unsigned int prefixLen, |
| 37 |
ProtoAddress& gw, |
| 38 |
unsigned int& ifIndex, |
| 39 |
int& metric); |
| 40 |
|
| 41 |
virtual bool SetRoute(const ProtoAddress& dst, |
| 42 |
unsigned int prefixLen, |
| 43 |
const ProtoAddress& gw, |
| 44 |
unsigned int ifIndex = 0, |
| 45 |
int metric = -1); |
| 46 |
|
| 47 |
virtual bool DeleteRoute(const ProtoAddress& dst, |
| 48 |
unsigned int prefixLen, |
| 49 |
const ProtoAddress& gw, |
| 50 |
unsigned int ifIndex = 0); |
| 51 |
|
| 52 |
virtual bool SetForwarding(bool state); |
| 53 |
|
| 54 |
virtual unsigned int GetInterfaceIndex(const char* interfaceName) |
| 55 |
{return ProtoSocket::GetInterfaceIndex(interfaceName);} |
| 56 |
|
| 57 |
virtual bool GetInterfaceAddressList(unsigned int ifIndex, |
| 58 |
ProtoAddress::Type addrType, |
| 59 |
ProtoAddressList& addrList); |
| 60 |
|
| 61 |
virtual bool GetInterfaceName(unsigned int interfaceIndex, |
| 62 |
char* buffer, |
| 63 |
unsigned int buflen) |
| 64 |
{return ProtoSocket::GetInterfaceName(interfaceIndex, buffer, buflen);} |
| 65 |
|
| 66 |
private: |
| 67 |
static bool NetlinkAddAttr(struct nlmsghdr* msg, |
| 68 |
unsigned int maxLen, |
| 69 |
int type, |
| 70 |
const void* data, |
| 71 |
int len); |
| 72 |
bool NetlinkCheckResponse(UINT32 seq); |
| 73 |
|
| 74 |
int descriptor; |
| 75 |
UINT32 port_id; // netlink port id |
| 76 |
UINT32 sequence; // netlink request/response sequence number |
| 77 |
|
| 78 |
}; // end class LinuxRouteMgr |
| 79 |
|
| 80 |
ProtoRouteMgr* ProtoRouteMgr::Create(Type theType) |
| 81 |
{ |
| 82 |
// TBD - support alternative route mgr "types" (e.g. ZEBRA) |
| 83 |
ProtoRouteMgr* returnMgr = NULL; |
| 84 |
switch(theType) |
| 85 |
{ |
| 86 |
case ZEBRA: |
| 87 |
returnMgr = (ProtoRouteMgr*)(new ZebraRouteMgr); |
| 88 |
break; |
| 89 |
case SYSTEM: |
| 90 |
returnMgr = (ProtoRouteMgr*)(new LinuxRouteMgr); |
| 91 |
break; |
| 92 |
default: |
| 93 |
return NULL; |
| 94 |
} |
| 95 |
return returnMgr; |
| 96 |
} // end ProtoRouteMgr::Create() |
| 97 |
|
| 98 |
LinuxRouteMgr::LinuxRouteMgr() |
| 99 |
: descriptor(-1), port_id(0), sequence(0) |
| 100 |
{ |
| 101 |
} |
| 102 |
|
| 103 |
LinuxRouteMgr::~LinuxRouteMgr() |
| 104 |
{ |
| 105 |
Close(); |
| 106 |
} |
| 107 |
|
| 108 |
bool LinuxRouteMgr::SetForwarding(bool state) |
| 109 |
{ |
| 110 |
int fd = open("/proc/sys/net/ipv4/ip_forward", O_WRONLY); |
| 111 |
if (fd < 0) |
| 112 |
{ |
| 113 |
PLOG(PL_ERROR, "LinuxRouteMgr::SetForwarding() open(/proc/sys) error: %s\n", |
| 114 |
strerror(errno)); |
| 115 |
return false; |
| 116 |
} |
| 117 |
char value = state ? '1' : '0'; |
| 118 |
if (1 == write(fd, &value, 1)) |
| 119 |
{ |
| 120 |
return true; |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
PLOG(PL_ERROR, "LinuxRouteMgr::SetForwarding() write(/proc/sys) error: %s\n", |
| 125 |
strerror(errno)); |
| 126 |
return false; |
| 127 |
} |
| 128 |
} // end LinuxRouteMgr::SetForwarding() |
| 129 |
|
| 130 |
bool LinuxRouteMgr::Open(const void* /*userData*/) |
| 131 |
{ |
| 132 |
if (IsOpen()) Close(); |
| 133 |
// (TBD) We may need to dictate NETLINK_ROUTE6 for IPv6 ops ??? |
| 134 |
if ((descriptor = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) |
| 135 |
{ |
| 136 |
PLOG(PL_ERROR, "LinuxRouteMgr::Open() socket(NETLINK_ROUTE) error: %s\n", |
| 137 |
strerror(errno)); |
| 138 |
return false; |
| 139 |
} |
| 140 |
else |
| 141 |
{ |
| 142 |
// Here we use "bind()" to get a unique netlink port id (pid) from the kernel |
| 143 |
// (with localAddr.nl_pid passed into bind() set as '0', kernel assigns us one |
| 144 |
struct sockaddr_nl localAddr; |
| 145 |
memset(&localAddr, 0, sizeof(localAddr)); |
| 146 |
localAddr.nl_family = AF_NETLINK; |
| 147 |
if (0 > bind(descriptor, (struct sockaddr*) &localAddr, sizeof(localAddr))) |
| 148 |
{ |
| 149 |
PLOG(PL_ERROR, "ProtoNetlink::Open() bind() error: %s\n", GetErrorString()); |
| 150 |
Close(); |
| 151 |
return false; |
| 152 |
} |
| 153 |
// Get socket name so we know our port number (i.e. netlink pid) |
| 154 |
socklen_t addrLen = sizeof(localAddr); |
| 155 |
if (getsockname(descriptor, (struct sockaddr*)&localAddr, &addrLen) < 0) |
| 156 |
{ |
| 157 |
PLOG(PL_ERROR, "ProtoNetlink::Open() getsockname() error: %s\n", GetErrorString()); |
| 158 |
Close(); |
| 159 |
return false; |
| 160 |
} |
| 161 |
port_id = localAddr.nl_pid; |
| 162 |
return true; |
| 163 |
} |
| 164 |
} // end LinuxRouteMgr::Open() |
| 165 |
|
| 166 |
void LinuxRouteMgr::Close() |
| 167 |
{ |
| 168 |
if (IsOpen()) |
| 169 |
{ |
| 170 |
close(descriptor); |
| 171 |
descriptor = -1; |
| 172 |
} |
| 173 |
} // end LinuxRouteMgr::Close() |
| 174 |
|
| 175 |
// Add an "attribute" (field) to netlink msg payload |
| 176 |
bool LinuxRouteMgr::NetlinkAddAttr(struct nlmsghdr* msg, |
| 177 |
unsigned int maxLen, |
| 178 |
int type, |
| 179 |
const void* data, |
| 180 |
int len) |
| 181 |
{ |
| 182 |
int rtaLen = RTA_LENGTH(len); |
| 183 |
if ((NLMSG_ALIGN(msg->nlmsg_len) + rtaLen) > maxLen) return false; |
| 184 |
struct rtattr* rta = (struct rtattr*)(((char*)msg) + NLMSG_ALIGN(msg->nlmsg_len)); |
| 185 |
rta->rta_type = type; |
| 186 |
rta->rta_len = rtaLen; |
| 187 |
memcpy(RTA_DATA(rta), data, len); |
| 188 |
msg->nlmsg_len = NLMSG_ALIGN(msg->nlmsg_len) + rtaLen; |
| 189 |
return true; |
| 190 |
} // end LinuxRouteMgr::NetlinkAddAttr() |
| 191 |
|
| 192 |
bool LinuxRouteMgr::NetlinkCheckResponse(UINT32 seq) |
| 193 |
{ |
| 194 |
while (1) |
| 195 |
{ |
| 196 |
char buffer[1024]; |
| 197 |
int msgLen = recv(descriptor, buffer, 1024, 0); |
| 198 |
if (msgLen < 0) |
| 199 |
{ |
| 200 |
if (errno == EINTR) |
| 201 |
{ |
| 202 |
continue; |
| 203 |
} |
| 204 |
else |
| 205 |
{ |
| 206 |
PLOG(PL_ERROR, "LinuxRouteMgr::NetlinkCheckResponse() recv() error: %s\n", strerror(errno)); |
| 207 |
return false; |
| 208 |
} |
| 209 |
} |
| 210 |
struct nlmsghdr* msg = (struct nlmsghdr*)buffer; |
| 211 |
for (; 0 != NLMSG_OK(msg, (unsigned int)msgLen); msg = NLMSG_NEXT(msg, msgLen)) |
| 212 |
{ |
| 213 |
// Justin Below is a semi-hack to get SMF to function correctly in core namespaces. |
| 214 |
// If when the msg->nlmsg_pid gets fixed this ifdef can be removed |
| 215 |
#ifndef CORE_NAMESPACES |
| 216 |
if ((msg->nlmsg_pid == port_id) && (msg->nlmsg_seq == seq)) |
| 217 |
#else |
| 218 |
//DMSG(0,"J Namspaces comment in NetlinkCheckResponse\n"); |
| 219 |
if (msg->nlmsg_seq == seq) |
| 220 |
#endif // if/else !CORE_NAMESPACES |
| 221 |
{ |
| 222 |
switch(msg->nlmsg_type) |
| 223 |
{ |
| 224 |
case NLMSG_ERROR: |
| 225 |
{ |
| 226 |
struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg); |
| 227 |
if (0 != errorMsg->error) |
| 228 |
{ |
| 229 |
PLOG(PL_DEBUG, "LinuxRouteMgr::NetlinkCheckResponse() recvd NLMSG_ERROR " |
| 230 |
"error seq:%d code:%d...\n", msg->nlmsg_seq, errorMsg->error); |
| 231 |
return false; |
| 232 |
} |
| 233 |
else |
| 234 |
{ |
| 235 |
return true; |
| 236 |
} |
| 237 |
break; |
| 238 |
} |
| 239 |
default: |
| 240 |
PLOG(PL_ERROR, "LinuxRouteMgr::NetlinkCheckResponse() recvd unexpected " |
| 241 |
"matching message type:%d\n", msg->nlmsg_type); |
| 242 |
// Assume success ??? |
| 243 |
return true; |
| 244 |
break; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
} // end while (1) |
| 249 |
} // end LinuxRouteMgr::NetlinkCheckResponse() |
| 250 |
|
| 251 |
bool LinuxRouteMgr::SetRoute(const ProtoAddress& dst, |
| 252 |
unsigned int prefixLen, |
| 253 |
const ProtoAddress& gw, |
| 254 |
unsigned int ifIndex, |
| 255 |
int metric) |
| 256 |
{ |
| 257 |
// First, delete any pre-existing route(s) to this destination |
| 258 |
// (TBD) try to do a "make before break" routing change |
| 259 |
PLOG(PL_DEBUG, "LinuxRouteMgr::SetRoute() setting route to %s/%d via ", |
| 260 |
dst.GetHostString(), prefixLen); |
| 261 |
if (gw.IsValid()) |
| 262 |
PLOG(PL_DEBUG, "gateway:%s\n", gw.GetHostString()); |
| 263 |
else |
| 264 |
PLOG(PL_DEBUG, "direct if:%d\n", ifIndex); |
| 265 |
if (!DeleteRoute(dst, prefixLen, gw, ifIndex)) |
| 266 |
{ |
| 267 |
// Limited to PL_DEBUG since route may _not_ pre-exist. TBD - improve this by checking first??? |
| 268 |
PLOG(PL_DEBUG, "LinuxRouteMgr::SetRoute() error deleting _possible_ pre-existing route to %s/%d\n", |
| 269 |
dst.GetHostString(), prefixLen); |
| 270 |
} |
| 271 |
|
| 272 |
struct |
| 273 |
{ |
| 274 |
struct nlmsghdr msg; |
| 275 |
struct rtmsg rt; |
| 276 |
char buf[1024]; |
| 277 |
} req; |
| 278 |
memset(&req, 0, sizeof(req)); |
| 279 |
|
| 280 |
// netlink message header |
| 281 |
req.msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
| 282 |
req.msg.nlmsg_type = RTM_NEWROUTE; |
| 283 |
req.msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE | NLM_F_ACK; |
| 284 |
UINT32 seq = sequence++; |
| 285 |
req.msg.nlmsg_seq = seq; |
| 286 |
req.msg.nlmsg_pid = port_id; |
| 287 |
|
| 288 |
// route add request |
| 289 |
switch (dst.GetType()) |
| 290 |
{ |
| 291 |
case ProtoAddress::IPv4: |
| 292 |
req.rt.rtm_family = AF_INET; |
| 293 |
break; |
| 294 |
#ifdef HAVE_IPV6 |
| 295 |
case ProtoAddress::IPv6: |
| 296 |
req.rt.rtm_family = AF_INET6; |
| 297 |
break; |
| 298 |
#endif // HAVE_IPV6 |
| 299 |
default: |
| 300 |
PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() invalid destination address!\n"); |
| 301 |
return false; |
| 302 |
break; |
| 303 |
} |
| 304 |
|
| 305 |
unsigned int addrBits = dst.GetLength() << 3; |
| 306 |
req.rt.rtm_dst_len = addrBits; |
| 307 |
req.rt.rtm_src_len = 0; |
| 308 |
req.rt.rtm_table = RT_TABLE_MAIN; |
| 309 |
req.rt.rtm_protocol = RTPROT_BOOT; |
| 310 |
if (gw.IsValid()) |
| 311 |
req.rt.rtm_scope = RT_SCOPE_UNIVERSE; |
| 312 |
else |
| 313 |
req.rt.rtm_scope = RT_SCOPE_LINK; |
| 314 |
if (dst.IsMulticast()) |
| 315 |
req.rt.rtm_type = RTN_MULTICAST; |
| 316 |
else |
| 317 |
req.rt.rtm_type = RTN_UNICAST; |
| 318 |
|
| 319 |
req.rt.rtm_flags = 0; |
| 320 |
|
| 321 |
// Set destination rtattr |
| 322 |
if (dst.IsValid()) |
| 323 |
{ |
| 324 |
|
| 325 |
if (prefixLen < addrBits) |
| 326 |
{ |
| 327 |
// address bits past mask _must_ be ZERO, so we test for that here |
| 328 |
unsigned int index = prefixLen >> 3; |
| 329 |
const char* bytes = dst.GetRawHostAddress(); |
| 330 |
if (0 != ((0x00ff >> (prefixLen & 0x07)) & bytes[index])) |
| 331 |
{ |
| 332 |
PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() invalid address for given mask\n"); |
| 333 |
return false; |
| 334 |
} |
| 335 |
while (++index < dst.GetLength()) |
| 336 |
{ |
| 337 |
if (0 != bytes[index] ) |
| 338 |
{ |
| 339 |
PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() invalid address for given mask\n"); |
| 340 |
return false; |
| 341 |
} |
| 342 |
} |
| 343 |
req.rt.rtm_dst_len = prefixLen; |
| 344 |
} |
| 345 |
else if (prefixLen > addrBits) |
| 346 |
{ |
| 347 |
PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() invalid mask.\n"); |
| 348 |
return false; |
| 349 |
} |
| 350 |
if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_DST, dst.GetRawHostAddress(), dst.GetLength())) |
| 351 |
{ |
| 352 |
PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() error setting RTA_DST attr.\n"); |
| 353 |
return false; |
| 354 |
} |
| 355 |
} |
| 356 |
else |
| 357 |
{ |
| 358 |
PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() error: invalid destination address\n"); |
| 359 |
return false; |
| 360 |
} |
| 361 |
|
| 362 |
// Set gateway rtattr |
| 363 |
if (gw.IsValid()) |
| 364 |
{ |
| 365 |
if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_GATEWAY, gw.GetRawHostAddress(), gw.GetLength())) |
| 366 |
{ |
| 367 |
PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() error adding RTA_GATEWAY attr.\n"); |
| 368 |
return false; |
| 369 |
} |
| 370 |
} |
| 371 |
else if (ifIndex == 0) |
| 372 |
{ |
| 373 |
PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() error: invalid gateway address\n"); |
| 374 |
return false; |
| 375 |
} |
| 376 |
|
| 377 |
if (ifIndex != 0) |
| 378 |
{ |
| 379 |
UINT32 value = ifIndex; |
| 380 |
if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_OIF, &value, sizeof(UINT32))) |
| 381 |
{ |
| 382 |
PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() error adding RTA_OIF attr.\n"); |
| 383 |
return false; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
// Set default route metric values |
| 388 |
if (metric < 0) |
| 389 |
{ |
| 390 |
if (gw.IsValid()) |
| 391 |
metric = 2; |
| 392 |
else |
| 393 |
metric = 1; |
| 394 |
} |
| 395 |
|
| 396 |
if (metric >= 0) |
| 397 |
{ |
| 398 |
UINT32 value = metric; |
| 399 |
if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_PRIORITY, &value, sizeof(UINT32))) |
| 400 |
{ |
| 401 |
PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() error adding RTA_PRIORITY attr.\n"); |
| 402 |
return false; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
// Send request to netlink socket |
| 407 |
int result = send(descriptor, &req, req.msg.nlmsg_len, 0); |
| 408 |
if ((int)req.msg.nlmsg_len != result) |
| 409 |
{ |
| 410 |
PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() send() error: %s\n", |
| 411 |
strerror(errno)); |
| 412 |
return false; |
| 413 |
} |
| 414 |
|
| 415 |
// Check response for error code |
| 416 |
if (NetlinkCheckResponse(seq)) |
| 417 |
{ |
| 418 |
return true; |
| 419 |
} |
| 420 |
else |
| 421 |
{ |
| 422 |
PLOG(PL_ERROR, "LinuxRouteMgr::SetRoute() error setting route\n"); |
| 423 |
return false; |
| 424 |
} |
| 425 |
} // end LinuxRouteMgr::SetRoute() |
| 426 |
|
| 427 |
bool LinuxRouteMgr::DeleteRoute(const ProtoAddress& dst, |
| 428 |
unsigned int prefixLen, |
| 429 |
const ProtoAddress& /*gateway*/, |
| 430 |
unsigned int ifIndex) |
| 431 |
{ |
| 432 |
ProtoAddress gw; |
| 433 |
unsigned int debugCount = 1; |
| 434 |
PLOG(PL_DEBUG, "LinuxRouteMgr::DeleteRoute() %u) getting route(s) to dst>%s/%d ", |
| 435 |
debugCount++, dst.GetHostString(), prefixLen); |
| 436 |
int metric; |
| 437 |
while (GetRoute(dst, prefixLen, gw, ifIndex, metric)) |
| 438 |
{ |
| 439 |
PLOG(PL_DEBUG, "LinuxRouteMgr::DeleteRoute() %u) deleting dst>%s/%d ", |
| 440 |
debugCount++, dst.GetHostString(), prefixLen); |
| 441 |
if (gw.IsValid()) |
| 442 |
PLOG(PL_DEBUG, "gw>%s (idx>%d)\n", gw.GetHostString(), ifIndex); |
| 443 |
else |
| 444 |
PLOG(PL_DEBUG, "direct idx>%d\n", ifIndex); |
| 445 |
|
| 446 |
struct |
| 447 |
{ |
| 448 |
struct nlmsghdr msg; |
| 449 |
struct rtmsg rt; |
| 450 |
char buf[1024]; |
| 451 |
} req; |
| 452 |
memset(&req, 0, sizeof(req)); |
| 453 |
|
| 454 |
// netlink message header |
| 455 |
req.msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
| 456 |
req.msg.nlmsg_type = RTM_DELROUTE; |
| 457 |
req.msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_MATCH; |
| 458 |
UINT32 seq = sequence++; |
| 459 |
req.msg.nlmsg_seq = seq; |
| 460 |
req.msg.nlmsg_pid = port_id; |
| 461 |
|
| 462 |
// route delete request |
| 463 |
switch (dst.GetType()) |
| 464 |
{ |
| 465 |
case ProtoAddress::IPv4: |
| 466 |
req.rt.rtm_family = AF_INET; |
| 467 |
break; |
| 468 |
#ifdef HAVE_IPV6 |
| 469 |
case ProtoAddress::IPv6: |
| 470 |
req.rt.rtm_family = AF_INET6; |
| 471 |
break; |
| 472 |
#endif // HAVE_IPV6 |
| 473 |
default: |
| 474 |
PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() invalid destination address!\n"); |
| 475 |
return false; |
| 476 |
break; |
| 477 |
} |
| 478 |
|
| 479 |
req.rt.rtm_dst_len = prefixLen; |
| 480 |
req.rt.rtm_src_len = 0; |
| 481 |
req.rt.rtm_table = RT_TABLE_UNSPEC; |
| 482 |
req.rt.rtm_protocol = RTPROT_BOOT; |
| 483 |
|
| 484 |
if (gw.IsValid()) |
| 485 |
req.rt.rtm_scope = RT_SCOPE_UNIVERSE; |
| 486 |
else |
| 487 |
req.rt.rtm_scope = RT_SCOPE_LINK; |
| 488 |
if (dst.IsMulticast()) |
| 489 |
req.rt.rtm_type = RTN_MULTICAST; |
| 490 |
else |
| 491 |
req.rt.rtm_type = RTN_UNICAST; |
| 492 |
|
| 493 |
req.rt.rtm_flags = 0; |
| 494 |
|
| 495 |
// Set destination rtattr |
| 496 |
if (dst.IsValid()) |
| 497 |
{ |
| 498 |
unsigned int addrBits = dst.GetLength() << 3; |
| 499 |
if (prefixLen < addrBits) |
| 500 |
{ |
| 501 |
// address bits past mask _must_ be ZERO, we test for that here |
| 502 |
unsigned int index = prefixLen >> 3; |
| 503 |
const char* bytes = dst.GetRawHostAddress(); |
| 504 |
if (0 != ((0x00ff >> (prefixLen & 0x07)) & bytes[index])) |
| 505 |
{ |
| 506 |
PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() invalid address for given mask\n"); |
| 507 |
return false; |
| 508 |
} |
| 509 |
while (++index < dst.GetLength()) |
| 510 |
{ |
| 511 |
if (0 != bytes[index] ) |
| 512 |
{ |
| 513 |
PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() invalid address for given mask\n"); |
| 514 |
return false; |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
else if (prefixLen > addrBits) |
| 519 |
{ |
| 520 |
PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() invalid mask.\n"); |
| 521 |
return false; |
| 522 |
} |
| 523 |
if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_DST, dst.GetRawHostAddress(), dst.GetLength())) |
| 524 |
{ |
| 525 |
PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() error setting RTA_DST attr.\n"); |
| 526 |
return false; |
| 527 |
} |
| 528 |
} |
| 529 |
else |
| 530 |
{ |
| 531 |
PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() no valid destination given\n"); |
| 532 |
return false; |
| 533 |
} |
| 534 |
|
| 535 |
if (gw.IsValid()) |
| 536 |
{ |
| 537 |
if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_GATEWAY, gw.GetRawHostAddress(), gw.GetLength())) |
| 538 |
{ |
| 539 |
PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() error setting RTA_GATEWAY attr.\n"); |
| 540 |
return false; |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
if (0 != ifIndex) |
| 545 |
{ |
| 546 |
UINT32 index = ifIndex; |
| 547 |
if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_OIF, &index, sizeof(UINT32))) |
| 548 |
{ |
| 549 |
PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() error adding RTA_GATEWAY attr.\n"); |
| 550 |
return false; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
// Send request to netlink socket |
| 555 |
int result = send(descriptor, &req, req.msg.nlmsg_len, 0); |
| 556 |
if ((int)req.msg.nlmsg_len != result) |
| 557 |
{ |
| 558 |
PLOG(PL_ERROR, "LinuxRouteMgr::DeleteRoute() send() error: %s\n", |
| 559 |
strerror(errno)); |
| 560 |
return false; |
| 561 |
} |
| 562 |
|
| 563 |
// Check response for error code |
| 564 |
if (!NetlinkCheckResponse(seq)) |
| 565 |
{ |
| 566 |
PLOG(PL_DEBUG, "LinuxRouteMgr::DeleteRoute() error deleting route\n"); |
| 567 |
return false; |
| 568 |
} |
| 569 |
} |
| 570 |
return true; |
| 571 |
} // end LinuxRouteMgr::DeleteRoute() |
| 572 |
|
| 573 |
|
| 574 |
bool LinuxRouteMgr::GetRoute(const ProtoAddress& dst, |
| 575 |
unsigned int prefixLen, |
| 576 |
ProtoAddress& gw, |
| 577 |
unsigned int& ifIndex, |
| 578 |
int& metric) |
| 579 |
{ |
| 580 |
bool routeFound = false; |
| 581 |
bool complete = false; |
| 582 |
int bufferSize = 4096; |
| 583 |
do |
| 584 |
{ |
| 585 |
// Init return values |
| 586 |
gw.Invalidate(); |
| 587 |
ifIndex = 0; |
| 588 |
char* buffer = new char[bufferSize]; |
| 589 |
if (!buffer) |
| 590 |
{ |
| 591 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() new buffer error: %s\n", strerror(errno)); |
| 592 |
return false; |
| 593 |
} |
| 594 |
|
| 595 |
// Construct request for route |
| 596 |
struct |
| 597 |
{ |
| 598 |
struct nlmsghdr msg; |
| 599 |
struct rtmsg rt; |
| 600 |
char buf[512]; |
| 601 |
} req; |
| 602 |
memset(&req, 0, sizeof(req)); |
| 603 |
|
| 604 |
// netlink message header |
| 605 |
req.msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
| 606 |
req.msg.nlmsg_type = RTM_GETROUTE; |
| 607 |
req.msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH; |
| 608 |
UINT32 seq = sequence++; |
| 609 |
req.msg.nlmsg_seq = seq; |
| 610 |
req.msg.nlmsg_pid = port_id; |
| 611 |
|
| 612 |
// route dump request |
| 613 |
switch (dst.GetType()) |
| 614 |
{ |
| 615 |
case ProtoAddress::IPv4: |
| 616 |
req.rt.rtm_family = AF_INET; |
| 617 |
break; |
| 618 |
#ifdef HAVE_IPV6 |
| 619 |
case ProtoAddress::IPv6: |
| 620 |
req.rt.rtm_family = AF_INET6; |
| 621 |
break; |
| 622 |
#endif // HAVE_IPV6 |
| 623 |
default: |
| 624 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() invalid destination address!\n"); |
| 625 |
delete[] buffer; |
| 626 |
return false; |
| 627 |
break; |
| 628 |
} |
| 629 |
req.rt.rtm_dst_len = prefixLen; |
| 630 |
req.rt.rtm_src_len = 0; |
| 631 |
req.rt.rtm_table = RT_TABLE_UNSPEC; |
| 632 |
req.rt.rtm_protocol = RTPROT_UNSPEC; |
| 633 |
req.rt.rtm_scope = RT_SCOPE_UNIVERSE; |
| 634 |
req.rt.rtm_type = RTN_UNSPEC; |
| 635 |
|
| 636 |
req.rt.rtm_flags = 0; |
| 637 |
|
| 638 |
// Set destination rtattr |
| 639 |
if (dst.IsValid()) |
| 640 |
{ |
| 641 |
unsigned int addrBits = dst.GetLength() << 3; |
| 642 |
if (prefixLen < addrBits) |
| 643 |
{ |
| 644 |
// address bits past mask _must_ be ZERO, we test for that here |
| 645 |
unsigned int index = prefixLen >> 3; |
| 646 |
const char* bytes = dst.GetRawHostAddress(); |
| 647 |
if (0 != ((0x00ff >> (prefixLen & 0x07)) & bytes[index])) |
| 648 |
{ |
| 649 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() invalid address for given mask\n"); |
| 650 |
delete[] buffer; |
| 651 |
return false; |
| 652 |
} |
| 653 |
while (++index < dst.GetLength()) |
| 654 |
{ |
| 655 |
if (0 != bytes[index] ) |
| 656 |
{ |
| 657 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() invalid address for given mask\n"); |
| 658 |
delete[] buffer; |
| 659 |
return false; |
| 660 |
} |
| 661 |
} |
| 662 |
} |
| 663 |
else if (prefixLen > addrBits) |
| 664 |
{ |
| 665 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() invalid mask.\n"); |
| 666 |
delete[] buffer; |
| 667 |
return false; |
| 668 |
} |
| 669 |
if (!NetlinkAddAttr(&req.msg, sizeof(req), RTA_DST, dst.GetRawHostAddress(), dst.GetLength())) |
| 670 |
{ |
| 671 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() error setting RTA_DST attr.\n"); |
| 672 |
delete[] buffer; |
| 673 |
return false; |
| 674 |
} |
| 675 |
} |
| 676 |
else |
| 677 |
{ |
| 678 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() no valid destination given\n"); |
| 679 |
delete[] buffer; |
| 680 |
return false; |
| 681 |
} |
| 682 |
|
| 683 |
// write message to our netlink socket |
| 684 |
int result = send(descriptor, &req, req.msg.nlmsg_len, 0); |
| 685 |
if (result < 0) |
| 686 |
{ |
| 687 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() send() error: %s\n", |
| 688 |
strerror(errno)); |
| 689 |
delete[] buffer; |
| 690 |
return false; |
| 691 |
} |
| 692 |
|
| 693 |
// read response(s) until NLMSG_DONE |
| 694 |
bool done = false; |
| 695 |
bool truncated = false; |
| 696 |
while(!done) |
| 697 |
{ |
| 698 |
int msgLen = recv(descriptor, buffer, bufferSize, 0); |
| 699 |
if (msgLen < 0) |
| 700 |
{ |
| 701 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetRoute() recv() error: %s\n", strerror(errno)); |
| 702 |
delete[] buffer; |
| 703 |
return false; |
| 704 |
} |
| 705 |
else if (msgLen == bufferSize) |
| 706 |
{ |
| 707 |
bufferSize *= 2; |
| 708 |
truncated = true; |
| 709 |
// TBD - should use struct msghdr.msg_flags MSG_TRUNC flag to detect this isntead |
| 710 |
} |
| 711 |
|
| 712 |
struct nlmsghdr* msg = (struct nlmsghdr*)buffer; |
| 713 |
for (; 0 != NLMSG_OK(msg, (unsigned int)msgLen); msg = NLMSG_NEXT(msg, msgLen)) |
| 714 |
{ |
| 715 |
//Justin comment oout pid part below if pid issues occur. This can happen with some virtualized systems. ie core namespaces |
| 716 |
//Justin Below is a semi-hack to get SMF to function correctly in core namespaces. |
| 717 |
//If when the msg->nlmsg_pid gets fixed this ifdef can be removed |
| 718 |
#ifndef CORE_NAMESPACES |
| 719 |
if ((msg->nlmsg_pid == port_id) && (msg->nlmsg_seq == seq)) |
| 720 |
#else |
| 721 |
if (msg->nlmsg_seq == seq) |
| 722 |
#endif // if/else !CORE_NAMESPACES |
| 723 |
{ |
| 724 |
if (!truncated) complete = true; // matching response within our buffer bounds |
| 725 |
switch (msg->nlmsg_type) |
| 726 |
{ |
| 727 |
case NLMSG_NOOP: |
| 728 |
TRACE("recvd NLMSG_NOOP ...\n"); |
| 729 |
break; |
| 730 |
case NLMSG_ERROR: |
| 731 |
{ |
| 732 |
struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg); |
| 733 |
PLOG(PL_DEBUG, "LinuxRouteMgr::GetRoute() recvd matching NLMSG_ERROR error seq:%d code:%d...\n", |
| 734 |
msg->nlmsg_seq, errorMsg->error); |
| 735 |
delete[] buffer; |
| 736 |
return false; |
| 737 |
break; |
| 738 |
} |
| 739 |
case NLMSG_DONE: |
| 740 |
//TRACE("recvd NLMSG_DONE ...\n"); |
| 741 |
done = true; |
| 742 |
break; |
| 743 |
case NLMSG_OVERRUN: |
| 744 |
TRACE("recvd NLMSG_OVERRUN ...\n"); |
| 745 |
break; |
| 746 |
case RTM_GETROUTE: |
| 747 |
{ |
| 748 |
TRACE("recvd RTM_GETROUTE ... \n"); |
| 749 |
break; |
| 750 |
} |
| 751 |
case RTM_NEWROUTE: |
| 752 |
{ |
| 753 |
if (truncated) break; // ignore "truncated" responses |
| 754 |
ProtoAddress destination; |
| 755 |
destination.Reset(dst.GetType()); |
| 756 |
unsigned int prefixLen = 0; |
| 757 |
//bool havePrefSource = false; |
| 758 |
ProtoAddress gateway; |
| 759 |
gateway.Invalidate(); |
| 760 |
unsigned int interfaceIndex = 0; |
| 761 |
|
| 762 |
struct rtmsg* rtm = (struct rtmsg*)NLMSG_DATA(msg); |
| 763 |
struct rtattr* rta = RTM_RTA(rtm); |
| 764 |
int rtaLen = msg->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg)); |
| 765 |
for (; RTA_OK(rta, rtaLen); |
| 766 |
rta = RTA_NEXT(rta, rtaLen)) |
| 767 |
{ |
| 768 |
switch (rta->rta_type) |
| 769 |
{ |
| 770 |
case RTA_DST: |
| 771 |
{ |
| 772 |
prefixLen = rtm->rtm_dst_len; |
| 773 |
destination.SetRawHostAddress(dst.GetType(), (char*)RTA_DATA(rta), |
| 774 |
(prefixLen+7)>>3); |
| 775 |
//TRACE("got dst: \"%s\"\n", destination.GetHostString()); |
| 776 |
break; |
| 777 |
} |
| 778 |
case RTA_GATEWAY: |
| 779 |
{ |
| 780 |
gateway.SetRawHostAddress(dst.GetType(), (char*)RTA_DATA(rta), dst.GetLength()); |
| 781 |
//TRACE("got gw: \"%s\"\n", gw.GetHostString()); |
| 782 |
break; |
| 783 |
} |
| 784 |
|
| 785 |
case RTA_PREFSRC: |
| 786 |
{ |
| 787 |
//ProtoAddress addr; |
| 788 |
//addr.SetRawHostAddress(dst.GetType(), (char*)RTA_DATA(rta), dst.GetLength()); |
| 789 |
//havePrefSource = true; |
| 790 |
break; |
| 791 |
} |
| 792 |
case RTA_OIF: |
| 793 |
{ |
| 794 |
ASSERT(4 == RTA_PAYLOAD(rta)); |
| 795 |
memcpy(&interfaceIndex, RTA_DATA(rta), 4); |
| 796 |
//TRACE("got ifIndex: %d\n", ifIndex); |
| 797 |
break; |
| 798 |
} |
| 799 |
case RTA_PRIORITY: |
| 800 |
{ |
| 801 |
UINT32 priority; |
| 802 |
memcpy(&priority, RTA_DATA(rta), 4); |
| 803 |
metric = priority; |
| 804 |
//TRACE("RTA_PRIORITY value: %d\n", metric); |
| 805 |
break; |
| 806 |
} |
| 807 |
default: |
| 808 |
//TRACE("LinuxRouteMgr::GetRoute() unhandled rtattr type:%d len:%d\n", |
| 809 |
// rta->rta_type, RTA_PAYLOAD(rta)); |
| 810 |
break; |
| 811 |
|
| 812 |
} // end switch(rta_type) |
| 813 |
} // end for(RTA_NEXT()) |
| 814 |
|
| 815 |
if ((RTN_BROADCAST != rtm->rtm_type) && |
| 816 |
(RTN_UNREACHABLE != rtm->rtm_type) && |
| 817 |
//destination.IsValid() && |
| 818 |
destination.HostIsEqual(dst) && |
| 819 |
(RTN_LOCAL != rtm->rtm_type) && |
| 820 |
(0 == (RTM_F_CLONED & rtm->rtm_flags))) |
| 821 |
{ |
| 822 |
if (!routeFound) |
| 823 |
{ |
| 824 |
if (gateway.IsValid() && |
| 825 |
!gateway.HostIsEqual(dst) && |
| 826 |
!gateway.IsUnspecified()) |
| 827 |
gw = gateway; |
| 828 |
if (gw.IsValid() || (interfaceIndex > 0)) |
| 829 |
{ |
| 830 |
ifIndex = interfaceIndex; |
| 831 |
// (TBD) fill in real metric |
| 832 |
metric = -1; |
| 833 |
routeFound = true; |
| 834 |
//delete[] buffer; return true; |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
break; |
| 839 |
} |
| 840 |
default: |
| 841 |
TRACE("matching reply type:%d len:%d bytes\n", |
| 842 |
msg->nlmsg_type, msg->nlmsg_len); |
| 843 |
break; |
| 844 |
} // end switch(nlmsg_type) |
| 845 |
} |
| 846 |
else |
| 847 |
{ |
| 848 |
if (NLMSG_ERROR == msg->nlmsg_type) |
| 849 |
{ |
| 850 |
struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg); |
| 851 |
PLOG(PL_DEBUG, "LinuxRouteMgr::GetRoute() recvd non-matching NLMSG_ERROR seq:%d code:%d\n", |
| 852 |
msg->nlmsg_seq, errorMsg->error); |
| 853 |
} |
| 854 |
} // end if/else (matching pid && seq) |
| 855 |
} // end for(NLMSG_NEXT()) |
| 856 |
} // end while (!done) |
| 857 |
delete[] buffer; |
| 858 |
buffer = NULL; |
| 859 |
} while (!complete); |
| 860 |
return routeFound; |
| 861 |
} // end LinuxRouteMgr::GetRoute() |
| 862 |
|
| 863 |
|
| 864 |
bool LinuxRouteMgr::GetAllRoutes(ProtoAddress::Type addrType, |
| 865 |
ProtoRouteTable& routeTable) |
| 866 |
{ |
| 867 |
// The "do" loop is needed to make sure we get all of the routes |
| 868 |
// Routing tables can be big (particularly IPv6) and since the |
| 869 |
// Netlink does _not_ do multi-part messages properly (It only |
| 870 |
// gives you part of the routing table if your receive buffer |
| 871 |
// isn't big enough), we repeat the request, increasing the buffer |
| 872 |
// size until it works |
| 873 |
|
| 874 |
bool complete = false; |
| 875 |
|
| 876 |
int bufferSize = 4096; |
| 877 |
int failSafeCount = 0; |
| 878 |
int failSafeMax = 10000; |
| 879 |
do |
| 880 |
{ |
| 881 |
if(failSafeCount++>failSafeMax) |
| 882 |
{ |
| 883 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() didn't find any routes!\n"); |
| 884 |
return false; |
| 885 |
} |
| 886 |
char* buffer = new char[bufferSize]; |
| 887 |
if (!buffer) |
| 888 |
{ |
| 889 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() new buffer error: %s\n", strerror(errno)); |
| 890 |
return false; |
| 891 |
} |
| 892 |
// Construct request for all routes |
| 893 |
struct |
| 894 |
{ |
| 895 |
struct nlmsghdr msg; |
| 896 |
struct rtmsg rt; |
| 897 |
} req; |
| 898 |
memset(&req, 0, sizeof(req)); |
| 899 |
|
| 900 |
// netlink message header |
| 901 |
req.msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
| 902 |
req.msg.nlmsg_type = RTM_GETROUTE; |
| 903 |
req.msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP | NLM_F_MULTI; |
| 904 |
UINT32 seq = sequence++; |
| 905 |
req.msg.nlmsg_seq = seq; |
| 906 |
req.msg.nlmsg_pid = port_id; |
| 907 |
|
| 908 |
// route dump request |
| 909 |
switch (addrType) |
| 910 |
{ |
| 911 |
case ProtoAddress::IPv4: |
| 912 |
req.rt.rtm_family = AF_INET; |
| 913 |
break; |
| 914 |
#ifdef HAVE_IPV6 |
| 915 |
case ProtoAddress::IPv6: |
| 916 |
req.rt.rtm_family = AF_INET6; |
| 917 |
break; |
| 918 |
#endif // HAVE_IPV6 |
| 919 |
default: |
| 920 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() invalid address type\n"); |
| 921 |
delete[] buffer; |
| 922 |
return false; |
| 923 |
} |
| 924 |
|
| 925 |
req.rt.rtm_dst_len = 0; |
| 926 |
req.rt.rtm_src_len = 0; |
| 927 |
req.rt.rtm_table = RT_TABLE_UNSPEC; |
| 928 |
req.rt.rtm_protocol = RTPROT_UNSPEC; |
| 929 |
req.rt.rtm_scope = RT_SCOPE_UNIVERSE; |
| 930 |
req.rt.rtm_type = RTN_UNSPEC; |
| 931 |
req.rt.rtm_flags = 0; |
| 932 |
|
| 933 |
// write message to our netlink socket |
| 934 |
int result = send(descriptor, &req, req.msg.nlmsg_len, 0); |
| 935 |
if (result < 0) |
| 936 |
{ |
| 937 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() send() error: %s\n", strerror(errno)); |
| 938 |
delete[] buffer; |
| 939 |
return false; |
| 940 |
} |
| 941 |
|
| 942 |
// read response(s) until NLMSG_DONE |
| 943 |
// (if response is truncated, rescale "bufferSize" and try again |
| 944 |
bool done = false; |
| 945 |
bool truncated = false; |
| 946 |
while(!done) |
| 947 |
{ |
| 948 |
//fprintf(stderr,"tiger time %d is buffer size\n",bufferSize); |
| 949 |
int msgLen = recv(descriptor, buffer, bufferSize, 0); |
| 950 |
//fprintf(stderr,"tiger time sdfsdfi\n"); |
| 951 |
if (msgLen < 0) |
| 952 |
{ |
| 953 |
//fprintf(stderr,"tiger time 2\n"); |
| 954 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() recv() error: %s\n", |
| 955 |
strerror(errno)); |
| 956 |
delete[] buffer; |
| 957 |
return false; |
| 958 |
} |
| 959 |
else if (msgLen == bufferSize) |
| 960 |
{ |
| 961 |
//fprintf(stderr,"tiger time 3\n"); |
| 962 |
// assume our receive buffer wasn't big enough |
| 963 |
// mark response as "truncated" |
| 964 |
bufferSize *= 2; |
| 965 |
truncated = true; |
| 966 |
} |
| 967 |
|
| 968 |
struct nlmsghdr* msg = (struct nlmsghdr*)buffer; |
| 969 |
for (; 0 != NLMSG_OK(msg, (unsigned int)msgLen); msg = NLMSG_NEXT(msg, msgLen)) |
| 970 |
{ |
| 971 |
// Justin Below is a semi-hack to get SMF to function correctly in core namespaces. |
| 972 |
// If when the msg->nlmsg_pid gets fixed this #ifdef can be removed |
| 973 |
#ifndef CORE_NAMESPACES |
| 974 |
if ((msg->nlmsg_pid == port_id) && (msg->nlmsg_seq == seq)) |
| 975 |
#else |
| 976 |
if (msg->nlmsg_seq == seq) |
| 977 |
#endif // if/else !CORE_NAMESPACES |
| 978 |
{ |
| 979 |
if (!truncated) complete = true; // we got a matching response _within_ our buffer bounds |
| 980 |
// if (0 != (NLM_F_MULTI & msg->nlmsg_flags)) TRACE("multi part message ...\n"); |
| 981 |
switch (msg->nlmsg_type) |
| 982 |
{ |
| 983 |
case NLMSG_NOOP: |
| 984 |
TRACE("recvd NLMSG_NOOP ...\n"); |
| 985 |
break; |
| 986 |
case NLMSG_ERROR: |
| 987 |
{ |
| 988 |
struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg); |
| 989 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() recvd NLMSG_ERROR error seq:%d code:%d...\n", |
| 990 |
msg->nlmsg_seq, errorMsg->error); |
| 991 |
break; |
| 992 |
} |
| 993 |
case NLMSG_DONE: |
| 994 |
done = true; |
| 995 |
break; |
| 996 |
case NLMSG_OVERRUN: |
| 997 |
TRACE("recvd NLMSG_OVERRUN ...\n"); |
| 998 |
break; |
| 999 |
case RTM_GETROUTE: |
| 1000 |
{ |
| 1001 |
TRACE("recvd RTM_GETROUTE ...\n"); |
| 1002 |
break; |
| 1003 |
} |
| 1004 |
case RTM_NEWROUTE: |
| 1005 |
{ |
| 1006 |
if (truncated) break; // ignore truncated responses |
| 1007 |
//TRACE("matching RTM_NEWROUTE reply. len:%d bytes\n", msg->nlmsg_len); |
| 1008 |
ProtoAddress gateway; |
| 1009 |
gateway.Invalidate(); |
| 1010 |
ProtoAddress destination; |
| 1011 |
destination.Invalidate(); |
| 1012 |
unsigned int prefixLen = 0; |
| 1013 |
unsigned int ifIndex = 0; |
| 1014 |
int metric = -1; |
| 1015 |
struct rtmsg* rtm = (struct rtmsg*)NLMSG_DATA(msg); |
| 1016 |
struct rtattr* rta = RTM_RTA(rtm); |
| 1017 |
int rtaLen = msg->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg)); |
| 1018 |
ProtoAddress::Type addrType = ProtoAddress::INVALID; |
| 1019 |
int addrLen = 0; |
| 1020 |
switch (rtm->rtm_family) |
| 1021 |
{ |
| 1022 |
case AF_INET: |
| 1023 |
addrType = ProtoAddress::IPv4; |
| 1024 |
addrLen = 4; |
| 1025 |
break; |
| 1026 |
#ifdef HAVE_IPV6 |
| 1027 |
case AF_INET6: |
| 1028 |
addrType = ProtoAddress::IPv6; |
| 1029 |
addrLen = 16; |
| 1030 |
break; |
| 1031 |
#endif // HAVE_IPV6 |
| 1032 |
default: |
| 1033 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() recvd invalid addr family\n"); |
| 1034 |
delete[] buffer; |
| 1035 |
return false; |
| 1036 |
break; |
| 1037 |
} |
| 1038 |
for (; RTA_OK(rta, rtaLen); |
| 1039 |
rta = RTA_NEXT(rta, rtaLen)) |
| 1040 |
{ |
| 1041 |
switch (rta->rta_type) |
| 1042 |
{ |
| 1043 |
case RTA_DST: |
| 1044 |
{ |
| 1045 |
prefixLen = rtm->rtm_dst_len; |
| 1046 |
destination.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), (prefixLen+7)>>3); |
| 1047 |
//TRACE("RTA_DST: %s/%d\n", destination.GetHostString(), |
| 1048 |
// rtm->rtm_dst_len); |
| 1049 |
break; |
| 1050 |
} |
| 1051 |
case RTA_GATEWAY: |
| 1052 |
{ |
| 1053 |
gateway.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLen); |
| 1054 |
//TRACE("RTA_GWY: %s\n", gateway.GetHostString()); |
| 1055 |
break; |
| 1056 |
} |
| 1057 |
|
| 1058 |
case RTA_PREFSRC: |
| 1059 |
{ |
| 1060 |
//ProtoAddress addr; |
| 1061 |
//addr.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLen); |
| 1062 |
//TRACE("RTA_SRC: %s\n", addr.GetHostString()); |
| 1063 |
break; |
| 1064 |
} |
| 1065 |
case RTA_OIF: |
| 1066 |
{ |
| 1067 |
memcpy(&ifIndex, RTA_DATA(rta), RTA_PAYLOAD(rta)); |
| 1068 |
//TRACE("RTA_OIF: %d\n", ifIndex); |
| 1069 |
break; |
| 1070 |
} |
| 1071 |
case RTA_PRIORITY: |
| 1072 |
{ |
| 1073 |
UINT32 priority; |
| 1074 |
memcpy(&priority, RTA_DATA(rta), 4); |
| 1075 |
metric = priority; |
| 1076 |
//TRACE("LinuxRouteMgr::GetAllRoutes() RTA_PRIORITY value: %d\n", metric); |
| 1077 |
break; |
| 1078 |
} |
| 1079 |
case RTA_METRICS: |
| 1080 |
{ |
| 1081 |
//TRACE("get all routes got RTA_METRIC ...\n"); |
| 1082 |
break; |
| 1083 |
} |
| 1084 |
|
| 1085 |
default: |
| 1086 |
//TRACE("LinuxRouteMgr::GetAllRoutes() unhandled rtattr type:%d len:%d\n", |
| 1087 |
// rta->rta_type, RTA_PAYLOAD(rta)); |
| 1088 |
break; |
| 1089 |
|
| 1090 |
} |
| 1091 |
} // end for(RTA_NEXT()) |
| 1092 |
|
| 1093 |
if ((RTN_BROADCAST != rtm->rtm_type) && |
| 1094 |
(RTN_UNREACHABLE != rtm->rtm_type) && |
| 1095 |
(RTN_LOCAL != rtm->rtm_type) && |
| 1096 |
(0 == (RTM_F_CLONED & rtm->rtm_flags))) |
| 1097 |
{ |
| 1098 |
if (!destination.IsValid()) |
| 1099 |
{ |
| 1100 |
destination.Reset(addrType); |
| 1101 |
prefixLen = 0; |
| 1102 |
} |
| 1103 |
|
| 1104 |
if (gateway.IsValid() && (gateway.IsUnspecified() || gateway.HostIsEqual(destination))) |
| 1105 |
{ |
| 1106 |
//TRACE("found UNSPECIFIED gateway\n"); |
| 1107 |
gateway.Invalidate(); |
| 1108 |
} |
| 1109 |
|
| 1110 |
if (!routeTable.SetRoute(destination, prefixLen, gateway, ifIndex, metric)) |
| 1111 |
{ |
| 1112 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() error creating table entry\n"); |
| 1113 |
delete[] buffer; |
| 1114 |
return false; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
break; |
| 1118 |
} |
| 1119 |
default: |
| 1120 |
TRACE("matching reply type:%d len:%d bytes\n", |
| 1121 |
msg->nlmsg_type, msg->nlmsg_len); |
| 1122 |
break; |
| 1123 |
} // end switch() |
| 1124 |
} |
| 1125 |
else |
| 1126 |
{ |
| 1127 |
//fprintf(stderr,"NLMSG_OK if false\n"); |
| 1128 |
//fprintf(stderr,"%d %d is msg type\n",msg->nlmsg_type,NLMSG_ERROR); |
| 1129 |
if (NLMSG_ERROR == msg->nlmsg_type) |
| 1130 |
{ |
| 1131 |
//fprintf(stderr,"before error message\n"); |
| 1132 |
struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg); |
| 1133 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetAllRoutes() recvd NLMSG_ERROR seq:%d code:%d\n", |
| 1134 |
msg->nlmsg_seq, errorMsg->error); |
| 1135 |
} |
| 1136 |
//fprintf(stderr," bunny time 1\n"); |
| 1137 |
} |
| 1138 |
//fprintf(stderr," bunny time 2\n"); |
| 1139 |
} // end for(NLMSG_NEXT()) |
| 1140 |
//fprintf(stderr," bunny time 3\n"); |
| 1141 |
} // end while (!done) |
| 1142 |
//fprintf(stderr," bunny time 4\n"); |
| 1143 |
delete[] buffer; |
| 1144 |
buffer = NULL; |
| 1145 |
//fprintf(stderr," bunny time 5\n"); |
| 1146 |
} while (!complete); |
| 1147 |
return true; |
| 1148 |
} // end LinuxRouteMgr::GetAllRoutes() |
| 1149 |
|
| 1150 |
bool LinuxRouteMgr::GetInterfaceAddressList(unsigned int ifIndex, |
| 1151 |
ProtoAddress::Type addrType, |
| 1152 |
ProtoAddressList& addrList) |
| 1153 |
{ |
| 1154 |
ProtoAddressList localAddrList; // keep link/site local addresses separate and add to "addrList" at end |
| 1155 |
// Construct request for interface address |
| 1156 |
struct |
| 1157 |
{ |
| 1158 |
struct nlmsghdr msg; |
| 1159 |
struct ifaddrmsg ifa; |
| 1160 |
} req; |
| 1161 |
memset(&req, 0, sizeof(req)); |
| 1162 |
|
| 1163 |
// DMSG(0,"Justin comment in getinterfaceaddress\n"); |
| 1164 |
// netlink message header |
| 1165 |
req.msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
| 1166 |
req.msg.nlmsg_type = RTM_GETADDR; |
| 1167 |
req.msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH ; |
| 1168 |
UINT32 seq = sequence++; |
| 1169 |
req.msg.nlmsg_seq = seq; |
| 1170 |
req.msg.nlmsg_pid = port_id; |
| 1171 |
|
| 1172 |
// route dump request |
| 1173 |
unsigned int addrLength = 0; |
| 1174 |
switch (addrType) |
| 1175 |
{ |
| 1176 |
case ProtoAddress::IPv4: |
| 1177 |
// DMSG(0,"Justin comment in getinterfaceaddress is ipv4\n"); |
| 1178 |
req.ifa.ifa_family = AF_INET; |
| 1179 |
req.ifa.ifa_prefixlen = 32; |
| 1180 |
addrLength = 4; |
| 1181 |
break; |
| 1182 |
#ifdef HAVE_IPV6 |
| 1183 |
case ProtoAddress::IPv6: |
| 1184 |
// DMSG(0,"Justin comment in getinterfaceaddress is ipv6\n"); |
| 1185 |
req.ifa.ifa_family = AF_INET6; |
| 1186 |
req.ifa.ifa_prefixlen = 128; |
| 1187 |
addrLength = 16; |
| 1188 |
break; |
| 1189 |
#endif // HAVE_IPV6 |
| 1190 |
default: |
| 1191 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() invalid destination address!\n"); |
| 1192 |
return false; |
| 1193 |
break; |
| 1194 |
} |
| 1195 |
req.ifa.ifa_flags = 0;//IFA_F_SECONDARY;//0;//IFA_F_PERMANENT; |
| 1196 |
req.ifa.ifa_scope = RT_SCOPE_UNIVERSE; |
| 1197 |
req.ifa.ifa_index = ifIndex; |
| 1198 |
|
| 1199 |
// write message to our netlink socket |
| 1200 |
// DMSG(0,"Justin before send comment in getinterfaceaddress is ipv4\n"); |
| 1201 |
int result = send(descriptor, &req, req.msg.nlmsg_len, 0); |
| 1202 |
// DMSG(0,"Justin comment after send in getinterfaceaddress is ipv4\n"); |
| 1203 |
if (result < 0) |
| 1204 |
{ |
| 1205 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() send() error: %s\n", |
| 1206 |
strerror(errno)); |
| 1207 |
return false; |
| 1208 |
} |
| 1209 |
|
| 1210 |
// read response(s) |
| 1211 |
bool done = false; |
| 1212 |
while(!done) |
| 1213 |
{ |
| 1214 |
// DMSG(0,"Justin in while comment in getinterfaceaddress is ipv4\n"); |
| 1215 |
char buffer[4096]; |
| 1216 |
int msgLen = recv(descriptor, buffer, 4096, 0); |
| 1217 |
if (msgLen < 0) |
| 1218 |
{ |
| 1219 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() recv() error: %s\n", strerror(errno)); |
| 1220 |
return false; |
| 1221 |
} |
| 1222 |
struct nlmsghdr* msg = (struct nlmsghdr*)buffer; |
| 1223 |
for (; 0 != NLMSG_OK(msg, (unsigned int)msgLen); msg = NLMSG_NEXT(msg, msgLen)) |
| 1224 |
{ |
| 1225 |
//Justin Below is a semi-hack to get SMF to function correctly in core namespaces. |
| 1226 |
//If when the msg->nlmsg_pid gets fixed this #if can be removed |
| 1227 |
#ifndef CORE_NAMESPACES |
| 1228 |
if ((msg->nlmsg_pid == port_id) && (msg->nlmsg_seq == seq)) |
| 1229 |
#else |
| 1230 |
if (msg->nlmsg_seq == seq) |
| 1231 |
#endif // if/else !CORE_NAMESPACES |
| 1232 |
{ |
| 1233 |
switch (msg->nlmsg_type) |
| 1234 |
{ |
| 1235 |
case NLMSG_NOOP: |
| 1236 |
//TRACE("recvd NLMSG_NOOP ...\n"); |
| 1237 |
break; |
| 1238 |
case NLMSG_ERROR: |
| 1239 |
{ |
| 1240 |
struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg); |
| 1241 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() recvd NLMSG_ERROR error seq:%d code:%d...\n", |
| 1242 |
msg->nlmsg_seq, errorMsg->error); |
| 1243 |
return false; |
| 1244 |
break; |
| 1245 |
} |
| 1246 |
case NLMSG_DONE: |
| 1247 |
//TRACE("recvd NLMSG_DONE ...\n"); |
| 1248 |
done = true; |
| 1249 |
break; |
| 1250 |
case RTM_NEWADDR: |
| 1251 |
{ |
| 1252 |
//TRACE("recvd RTM_NEWADDR ... \n"); |
| 1253 |
struct ifaddrmsg* ifa = (struct ifaddrmsg*)NLMSG_DATA(msg); |
| 1254 |
struct rtattr* rta = IFA_RTA(ifa); |
| 1255 |
int rtaLen = msg->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
| 1256 |
for (; RTA_OK(rta, rtaLen); |
| 1257 |
rta = RTA_NEXT(rta, rtaLen)) |
| 1258 |
{ |
| 1259 |
switch (rta->rta_type) |
| 1260 |
{ |
| 1261 |
case IFA_ADDRESS: |
| 1262 |
case IFA_LOCAL: |
| 1263 |
{ |
| 1264 |
//ProtoAddress addr; |
| 1265 |
//addr.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLength); |
| 1266 |
//TRACE("%s: %s (index:%d scope:%d)\n", (IFA_ADDRESS == rta->rta_type) ? |
| 1267 |
// "IFA_ADDRESS" : "IFA_LOCAL", addr.GetHostString(), |
| 1268 |
// ifa->ifa_index, ifa->ifa_scope); |
| 1269 |
if (ifa->ifa_index == ifIndex) |
| 1270 |
{ |
| 1271 |
switch (ifa->ifa_scope) |
| 1272 |
{ |
| 1273 |
case RT_SCOPE_UNIVERSE: |
| 1274 |
{ |
| 1275 |
ProtoAddress theAddress; |
| 1276 |
theAddress.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLength); |
| 1277 |
if (theAddress.IsValid()) |
| 1278 |
{ |
| 1279 |
if (!addrList.Insert(theAddress)) |
| 1280 |
{ |
| 1281 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() error: couldn't add to addrList\n"); |
| 1282 |
done = true; |
| 1283 |
} |
| 1284 |
} |
| 1285 |
break; |
| 1286 |
} |
| 1287 |
case RT_SCOPE_SITE: |
| 1288 |
case RT_SCOPE_LINK: |
| 1289 |
{ |
| 1290 |
// Keep site-local addresses in separate list and add at end. |
| 1291 |
ProtoAddress theAddress; |
| 1292 |
theAddress.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLength); |
| 1293 |
if (theAddress.IsValid() && !localAddrList.Insert(theAddress)) |
| 1294 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() error: couldn't add to localAddrList\n"); |
| 1295 |
break; |
| 1296 |
} |
| 1297 |
default: |
| 1298 |
// ignore other address types for now |
| 1299 |
break; |
| 1300 |
} |
| 1301 |
} |
| 1302 |
break; |
| 1303 |
} |
| 1304 |
case IFA_BROADCAST: |
| 1305 |
{ |
| 1306 |
//ProtoAddress addr; |
| 1307 |
//addr.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLength); |
| 1308 |
//TRACE("IFA_BROADCAST: %s\n", addr.GetHostString()); |
| 1309 |
break; |
| 1310 |
} |
| 1311 |
default: |
| 1312 |
//TRACE("LinuxRouteMgr::GetInterfaceAddressList() unhandled rtattr type:%d len:%d\n", |
| 1313 |
// rta->rta_type, RTA_PAYLOAD(rta)); |
| 1314 |
break; |
| 1315 |
|
| 1316 |
} // end switch(rta_type) |
| 1317 |
} // end for(RTA_NEXT()) |
| 1318 |
break; |
| 1319 |
} |
| 1320 |
default: |
| 1321 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() matching reply type:%d len:%d bytes\n", |
| 1322 |
msg->nlmsg_type, msg->nlmsg_len); |
| 1323 |
break; |
| 1324 |
} // end switch(nlmsg_type) |
| 1325 |
} |
| 1326 |
else |
| 1327 |
{ |
| 1328 |
if (NLMSG_ERROR == msg->nlmsg_type) |
| 1329 |
{ |
| 1330 |
struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg); |
| 1331 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() recvd NLMSG_ERROR seq:%d code:%d\n", |
| 1332 |
msg->nlmsg_seq, errorMsg->error); |
| 1333 |
} |
| 1334 |
} // end if/else (matching pid && seq) |
| 1335 |
} // end for(NLMSG_NEXT()) |
| 1336 |
} // end while (!done) |
| 1337 |
|
| 1338 |
ProtoAddressList::Iterator iterator(localAddrList); |
| 1339 |
ProtoAddress localAddr; |
| 1340 |
while (iterator.GetNextAddress(localAddr)) |
| 1341 |
{ |
| 1342 |
if (!addrList.Insert(localAddr)) |
| 1343 |
{ |
| 1344 |
PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() error: couldn't add localAddr to addrList\n"); |
| 1345 |
break; |
| 1346 |
} |
| 1347 |
} |
| 1348 |
if (addrList.IsEmpty()) |
| 1349 |
PLOG(PL_WARN, "LinuxRouteMgr::GetInterfaceAddressList() warning: no addresses found\n"); |
| 1350 |
return true; |
| 1351 |
} // end LinuxRouteMgr::GetInterfaceAddressList() |