void DoDeleteIpForwardEntry(char* pszDest) { DWORD dwStatus, dwDelStatus, i; PMIB_IPFORWARDTABLE pIpRouteTab = NULL; // Ip routing table MIB_IPFORWARDROW routeEntry; // Ip routing table row entry DWORD dwForwardDest = 0; bool fDeleted = FALSE; memset(&routeEntry, 0, sizeof(MIB_IPFORWARDROW)); dwForwardDest = inet_addr(pszDest); // convert dotted ip addr. to ip addr. if (dwForwardDest == INADDR_NONE) { printf("IpRoute: Bad Destination %s\n", pszDest); return; } if ( (dwStatus = MyGetIpForwardTable(pIpRouteTab, TRUE)) == NO_ERROR) { for (i = 0; i < pIpRouteTab->dwNumEntries; i++) { if (dwForwardDest == pIpRouteTab->table[i].dwForwardDest) { memcpy(&routeEntry, &(pIpRouteTab->table[i]), sizeof(MIB_IPFORWARDROW)); dwDelStatus = DeleteIpForwardEntry(&routeEntry); if (dwDelStatus != NO_ERROR) { printf("IpRoute: couldn't delete (%s), dwStatus = %lu.\n", pszDest, dwDelStatus); return; } else fDeleted = TRUE; } } free(pIpRouteTab); if (! fDeleted) printf("IpRoute: The route specified was not found.\n"); return; } else if ( dwStatus == ERROR_NO_DATA) { printf("IpRoute: No entries in route table.\n"); if (pIpRouteTab) free (pIpRouteTab); return; } else { if (pIpRouteTab) free (pIpRouteTab); printf("IpRoute returned 0x%x\n", dwStatus); return; } }
/* * Remove a route from the kernel * @param destination the route to remove * @return negative on error */ int os_route_del_rtentry(const struct rt_entry *rt, int ip_version) { MIB_IPFORWARDROW Row; union olsr_ip_addr mask; unsigned long Res; struct interface *iface = rt->rt_nexthop.interface; if (AF_INET != ip_version) { /* * Not implemented */ return -1; } OLSR_DEBUG(LOG_NETWORKING, "KERN: Deleting %s\n", olsr_rt_to_string(rt)); memset(&Row, 0, sizeof(Row)); Row.dwForwardDest = rt->rt_dst.prefix.v4.s_addr; if (!olsr_prefix_to_netmask(&mask, rt->rt_dst.prefix_len)) { return -1; } Row.dwForwardMask = mask.v4.s_addr; Row.dwForwardPolicy = 0; Row.dwForwardNextHop = rt->rt_nexthop.gateway.v4.s_addr; Row.dwForwardIfIndex = iface->if_index; // MIB_IPROUTE_TYPE_DIRECT and MIB_IPROUTE_TYPE_INDIRECT Row.dwForwardType = (rt->rt_dst.prefix.v4.s_addr == rt->rt_nexthop.gateway.v4.s_addr) ? 3 : 4; Row.dwForwardProto = 3; // MIB_IPPROTO_NETMGMT Row.dwForwardAge = INFINITE; Row.dwForwardNextHopAS = 0; Row.dwForwardMetric1 = olsr_fib_metric(&rt->rt_metric); Row.dwForwardMetric2 = -1; Row.dwForwardMetric3 = -1; Row.dwForwardMetric4 = -1; Row.dwForwardMetric5 = -1; Res = DeleteIpForwardEntry(&Row); if (Res != NO_ERROR) { OLSR_WARN(LOG_NETWORKING, "DeleteIpForwardEntry() = %08lx, %s", Res, win32_strerror(Res)); // XXX - report error in a different way errno = Res; return -1; } return 0; }
STDMETHODIMP CNetwork::RouteDelete(LONG InterfaceIndex, LONG Network, LONG Netmask, LONG Gateway) { HRESULT hr; try { MIB_IPFORWARDROW fr; set_ipforwardraw(&fr, InterfaceIndex, Network, Netmask, Gateway); if ((hr = HRESULT_FROM_WIN32(DeleteIpForwardEntry (&fr))) != S_OK) { throw new _com_error(hr); } return S_OK; } catch(const _com_error &e) { return Error(e.ErrorMessage(), __uuidof(INetwork), e.Error()); } }
DWORD DSStaticRefreshParams( PipeSendFunc Send, COMM_DHCP_REQ *Req ) { NTSTATUS Status; COMM_DHCP_REPLY Reply; PDHCP_ADAPTER Adapter; struct protocol* proto; ApiLock(); Adapter = AdapterFindIndex( Req->AdapterIndex ); Reply.Reply = Adapter ? 1 : 0; if( Adapter ) { if (Adapter->NteContext) { DeleteIPAddress( Adapter->NteContext ); Adapter->NteContext = 0; } if (Adapter->RouterMib.dwForwardNextHop) { DeleteIpForwardEntry( &Adapter->RouterMib ); Adapter->RouterMib.dwForwardNextHop = 0; } Adapter->DhclientState.state = S_STATIC; proto = find_protocol_by_adapter( &Adapter->DhclientInfo ); if (proto) remove_protocol(proto); Status = AddIPAddress( Req->Body.StaticRefreshParams.IPAddress, Req->Body.StaticRefreshParams.Netmask, Req->AdapterIndex, &Adapter->NteContext, &Adapter->NteInstance ); Reply.Reply = NT_SUCCESS(Status); if (AdapterStateChangedEvent != NULL) SetEvent(AdapterStateChangedEvent); } ApiUnlock(); return Send( &Reply ); }
/* * Adds or removes a route from the supplied request */ DWORD add_remove_route(Packet *packet, BOOLEAN add) { MIB_IPFORWARDROW route; DWORD (WINAPI *LocalGetBestInterface)(IPAddr, LPDWORD) = NULL; LPCSTR subnet; LPCSTR netmask; LPCSTR gateway; subnet = packet_get_tlv_value_string(packet, TLV_TYPE_SUBNET_STRING); netmask = packet_get_tlv_value_string(packet, TLV_TYPE_NETMASK_STRING); gateway = packet_get_tlv_value_string(packet, TLV_TYPE_GATEWAY_STRING); memset(&route, 0, sizeof(route)); route.dwForwardDest = inet_addr(subnet); route.dwForwardMask = inet_addr(netmask); route.dwForwardNextHop = inet_addr(gateway); route.dwForwardType = 4; // Assume next hop. route.dwForwardProto = 3; route.dwForwardAge = -1; if ((LocalGetBestInterface = (DWORD (WINAPI *)(IPAddr, LPDWORD))GetProcAddress( GetModuleHandle("iphlpapi"), "GetBestInterface"))) { DWORD result = LocalGetBestInterface(route.dwForwardDest, &route.dwForwardIfIndex); if (result != ERROR_SUCCESS) return result; } // I'm lazy. Need manual lookup of ifindex based on gateway for NT. else return ERROR_NOT_SUPPORTED; if (add) return CreateIpForwardEntry(&route); else return DeleteIpForwardEntry(&route); }
DWORD DSReleaseIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ) { COMM_DHCP_REPLY Reply; PDHCP_ADAPTER Adapter; struct protocol* proto; ApiLock(); Adapter = AdapterFindIndex( Req->AdapterIndex ); Reply.Reply = Adapter ? 1 : 0; if( Adapter ) { if (Adapter->NteContext) { DeleteIPAddress( Adapter->NteContext ); Adapter->NteContext = 0; } if (Adapter->RouterMib.dwForwardNextHop) { DeleteIpForwardEntry( &Adapter->RouterMib ); Adapter->RouterMib.dwForwardNextHop = 0; } proto = find_protocol_by_adapter( &Adapter->DhclientInfo ); if (proto) remove_protocol(proto); Adapter->DhclientInfo.client->active = NULL; Adapter->DhclientInfo.client->state = S_INIT; if (AdapterStateChangedEvent != NULL) SetEvent(AdapterStateChangedEvent); } ApiUnlock(); return Send( &Reply ); }
int route_delete(route_t *route, const struct route_entry *entry) { MIB_IPFORWARDROW ipfrow; DWORD mask; if (entry->route_dst.addr_type != ADDR_TYPE_IP || GetBestRoute(entry->route_dst.addr_ip, IP_ADDR_ANY, &ipfrow) != NO_ERROR) return (-1); addr_btom(entry->route_dst.addr_bits, &mask, IP_ADDR_LEN); if (ipfrow.dwForwardDest != entry->route_dst.addr_ip || ipfrow.dwForwardMask != mask) { errno = ENXIO; SetLastError(ERROR_NO_DATA); return (-1); } if (DeleteIpForwardEntry(&ipfrow) != NO_ERROR) return (-1); return (0); }
static Boolean ssh_ip_route_remove_ipv4(SshIpAddr prefix, SshIpAddr nexthop) { Boolean ok = FALSE; MIB_IPFORWARDTABLE *ift = NULL; MIB_IPFORWARDROW *ifr, the_ifr; DWORD error; unsigned int i; /* Get route table. */ if (!(ift = ssh_ip_route_get_ipforwardtable())) goto end; /* Set up route destination, mask and next hop. */ memset(&the_ifr, 0, sizeof the_ifr); SSH_IP4_ENCODE(prefix, &the_ifr.dwForwardDest); if (prefix->mask_len >= 32) the_ifr.dwForwardMask = 0xFFFFFFFFU; else SSH_PUT_32BIT(&the_ifr.dwForwardMask, ~(0xFFFFFFFFU >> prefix->mask_len)); SSH_IP4_ENCODE(nexthop, &the_ifr.dwForwardNextHop); /* Find the route. Set route interface and type. */ for (i = 0; i < (int)ift->dwNumEntries; i++) { ifr = &ift->table[i]; if (ifr->dwForwardDest == the_ifr.dwForwardDest && ifr->dwForwardMask == the_ifr.dwForwardMask && ifr->dwForwardNextHop == the_ifr.dwForwardNextHop) { the_ifr.dwForwardIfIndex = ifr->dwForwardIfIndex; the_ifr.dwForwardType = ifr->dwForwardType; break; } } if (i >= (int)ift->dwNumEntries) { SSH_DEBUG(SSH_D_FAIL, ("Route not found")); goto end; } /* Delete row. */ error = DeleteIpForwardEntry(&the_ifr); if (error != NO_ERROR) { SSH_DEBUG(SSH_D_FAIL, ("DeleteIpForwardEntry: error 0x%08X", (unsigned)error)); goto end; } ok = TRUE; end: if (ift) ssh_free(ift); return ok; }