예제 #1
0
void TunManager::addRemoveSourceRouteRule(
    RouterID rid, folly::IPAddress addr, bool add) {
  struct {
    struct nlmsghdr n;
    struct rtmsg r;
    char buf[256];
  } req;

  memset(&req, 0, sizeof(req));
  req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  req.n.nlmsg_flags = NLM_F_REQUEST;
  if (add) {
    req.n.nlmsg_type = RTM_NEWRULE;
    req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_REPLACE;
  } else {
    req.n.nlmsg_type = RTM_DELRULE;
  }
  req.r.rtm_family = addr.family();
  req.r.rtm_protocol = RTPROT_FBOSS;
  req.r.rtm_scope = RT_SCOPE_UNIVERSE;
  req.r.rtm_type = RTN_UNICAST;
  req.r.rtm_flags = 0;
  // from addr
  addattr_l(&req.n, sizeof(req), FRA_SRC, addr.bytes(), addr.byteCount());
  req.r.rtm_src_len = addr.bitCount(); // match the exact address
  // table rid
  req.r.rtm_table = getTableId(rid);
  auto ret = rtnl_talk(&rth_, &req.n, 0, 0, nullptr);
  sysCheckError(ret, "Failed to ", add ? "add" : "remove",
                " rule for address ", addr,
                " to lookup table ", getTableId(rid),
                " for router ", rid);
  LOG(INFO) << (add ? "Added" : "Removed") << " rule for address " << addr
            << " to lookup table " << getTableId(rid) << " for router " << rid;
}
예제 #2
0
Interface::Addresses::const_iterator Interface::getAddressToReach(
    const folly::IPAddress& dest) const {
  for (auto iter = getAddresses().begin();
       iter != getAddresses().end();
       iter ++) {
    if (dest.inSubnet(iter->first, iter->second)) {
      return iter;
    }
  }
  return getAddresses().end();
}
예제 #3
0
void TunManager::addRemoveTunAddress(
    const std::string& name, uint32_t ifIndex,
    folly::IPAddress addr, uint8_t mask, bool add) {
  struct {
    struct nlmsghdr n;
    struct ifaddrmsg ifa;
    char buf[256];
  } req;
  memset(&req, 0, sizeof(req));
  req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
  if (add) {
    req.n.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_EXCL;
    req.n.nlmsg_type = RTM_NEWADDR;
  } else {
    req.n.nlmsg_flags = NLM_F_REQUEST;
    req.n.nlmsg_type = RTM_DELADDR;
  }
  if (addr.isV4()) {
    req.ifa.ifa_family = AF_INET;
    addattr_l(&req.n, sizeof(req), IFA_LOCAL, addr.asV4().bytes(),
              folly::IPAddressV4::byteCount());
  } else {
    req.ifa.ifa_family = AF_INET6;
    addattr_l(&req.n, sizeof(req), IFA_LOCAL, addr.asV6().bytes(),
              folly::IPAddressV6::byteCount());
  }
  req.ifa.ifa_prefixlen = mask;
  req.ifa.ifa_index = ifIndex;
  auto ret = rtnl_talk(&rth_, &req.n, 0, 0, nullptr);
  sysCheckError(ret, "Failed to ", add ? "add" : "remove",
                " address ", addr, "/", static_cast<int>(mask),
                " to interface ", name, " @ index ", ifIndex);
  LOG(INFO) << (add ? "Added" : "Removed") << " address " << addr.str() << "/"
            << static_cast<int>(mask) << " on interface " << name
            << " @ index " << ifIndex;
}
예제 #4
0
bool BcmRoute::deleteLpmRoute(int unitNumber,
                              opennsl_vrf_t vrf,
                              const folly::IPAddress& prefix,
                              uint8_t prefixLength) {
  opennsl_l3_route_t rt;
  initL3RouteFromArgs(&rt, vrf, prefix, prefixLength);
  auto rc = opennsl_l3_route_delete(unitNumber, &rt);
  if (OPENNSL_FAILURE(rc)) {
    LOG(ERROR) << "Failed to delete a route entry for " << prefix << "/"
               << static_cast<int>(prefixLength)
               << " Error: " << opennsl_errmsg(rc);
    return false;
  } else {
    VLOG(3) << "deleted a route entry for " << prefix.str() << "/"
            << static_cast<int>(prefixLength);
  }
  return true;
}