2016-08-25 17:48:53 +02:00
|
|
|
/*
|
|
|
|
* \brief A net interface in form of a signal-driven NIC-packet handler
|
|
|
|
* \author Martin Stein
|
|
|
|
* \date 2016-08-24
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2016-2017 Genode Labs GmbH
|
2016-08-25 17:48:53 +02:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2016-08-25 17:48:53 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Genode includes */
|
2016-09-12 12:55:12 +02:00
|
|
|
#include <net/tcp.h>
|
2016-08-25 17:48:53 +02:00
|
|
|
#include <net/udp.h>
|
2016-09-12 12:55:12 +02:00
|
|
|
#include <net/arp.h>
|
2016-08-25 17:48:53 +02:00
|
|
|
|
|
|
|
/* local includes */
|
|
|
|
#include <interface.h>
|
2016-09-12 12:55:12 +02:00
|
|
|
#include <configuration.h>
|
2017-09-14 15:10:32 +02:00
|
|
|
#include <l3_protocol.h>
|
nic_router: DHCP client functionality
If the attribute 'interface' is not set in a 'domain' tag, the router tries to
dynamically receive and maintain an IP configuration for that domain by using
DHCP in the client role at all interfaces that connect to the domain. In the
DHCP discover phase, the router simply chooses the first DHCP offer that
arrives. So, no comparison of different DHCP offers is done. In the DHCP
request phase, the server is expected to provide an IP address, a gateway, a
subnet mask, and an IP lease time to the router. If anything substantial goes
wrong during a DHCP exchange, the router discards the outcome of the exchange
and goes back to the DHCP discover phase. At any time where there is no valid
IP configuration present at a domain, the domain does only act as DHCP client
and all other router functionality is disabled for the domain. A domain cannot
act as DHCP client and DHCP server at once. So, a 'domain' tag must either
have an 'interface' attribute or must not contain a 'dhcp-server' tag.
Ref #2534
2017-10-16 11:31:43 +02:00
|
|
|
#include <size_guard.h>
|
2016-08-25 17:48:53 +02:00
|
|
|
|
|
|
|
using namespace Net;
|
|
|
|
using namespace Genode;
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
/***************
|
|
|
|
** Utilities **
|
|
|
|
***************/
|
|
|
|
|
|
|
|
template <typename LINK_TYPE>
|
|
|
|
static void _destroy_closed_links(Link_list &closed_links,
|
|
|
|
Deallocator &dealloc)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
while (Link *link = closed_links.first()) {
|
|
|
|
closed_links.remove(link);
|
|
|
|
destroy(dealloc, static_cast<LINK_TYPE *>(link));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename LINK_TYPE>
|
|
|
|
static void _destroy_links(Link_side_tree &links,
|
|
|
|
Link_list &closed_links,
|
|
|
|
Deallocator &dealloc)
|
|
|
|
{
|
|
|
|
_destroy_closed_links<LINK_TYPE>(closed_links, dealloc);
|
|
|
|
while (Link_side *link_side = links.first()) {
|
|
|
|
Link &link = link_side->link();
|
|
|
|
link.dissolve();
|
|
|
|
destroy(dealloc, static_cast<LINK_TYPE *>(&link));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
static void _link_packet(L3_protocol const prot,
|
|
|
|
void *const prot_base,
|
|
|
|
Link &link,
|
|
|
|
bool const client)
|
2016-09-12 12:55:12 +02:00
|
|
|
{
|
|
|
|
switch (prot) {
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::TCP:
|
2016-09-12 12:55:12 +02:00
|
|
|
if (client) {
|
|
|
|
static_cast<Tcp_link *>(&link)->client_packet(*(Tcp_packet *)(prot_base));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
static_cast<Tcp_link *>(&link)->server_packet(*(Tcp_packet *)(prot_base));
|
|
|
|
return;
|
|
|
|
}
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::UDP:
|
2016-09-12 12:55:12 +02:00
|
|
|
static_cast<Udp_link *>(&link)->packet();
|
2016-08-25 17:48:53 +02:00
|
|
|
return;
|
2016-09-12 12:55:12 +02:00
|
|
|
default: throw Interface::Bad_transport_protocol(); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
static void _update_checksum(L3_protocol const prot,
|
2016-09-12 12:55:12 +02:00
|
|
|
void *const prot_base,
|
|
|
|
size_t const prot_size,
|
|
|
|
Ipv4_address const src,
|
|
|
|
Ipv4_address const dst)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
switch (prot) {
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::TCP:
|
2016-09-12 12:55:12 +02:00
|
|
|
((Tcp_packet *)prot_base)->update_checksum(src, dst, prot_size);
|
|
|
|
return;
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::UDP:
|
2016-09-12 12:55:12 +02:00
|
|
|
((Udp_packet *)prot_base)->update_checksum(src, dst);
|
|
|
|
return;
|
|
|
|
default: throw Interface::Bad_transport_protocol(); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
static Port _dst_port(L3_protocol const prot, void *const prot_base)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
switch (prot) {
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::TCP: return (*(Tcp_packet *)prot_base).dst_port();
|
|
|
|
case L3_protocol::UDP: return (*(Udp_packet *)prot_base).dst_port();
|
2016-09-12 12:55:12 +02:00
|
|
|
default: throw Interface::Bad_transport_protocol(); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
static void _dst_port(L3_protocol const prot,
|
|
|
|
void *const prot_base,
|
|
|
|
Port const port)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
switch (prot) {
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::TCP: (*(Tcp_packet *)prot_base).dst_port(port); return;
|
|
|
|
case L3_protocol::UDP: (*(Udp_packet *)prot_base).dst_port(port); return;
|
2016-09-12 12:55:12 +02:00
|
|
|
default: throw Interface::Bad_transport_protocol(); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
static Port _src_port(L3_protocol const prot, void *const prot_base)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
switch (prot) {
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::TCP: return (*(Tcp_packet *)prot_base).src_port();
|
|
|
|
case L3_protocol::UDP: return (*(Udp_packet *)prot_base).src_port();
|
2016-09-12 12:55:12 +02:00
|
|
|
default: throw Interface::Bad_transport_protocol(); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
static void _src_port(L3_protocol const prot,
|
|
|
|
void *const prot_base,
|
|
|
|
Port const port)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
switch (prot) {
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::TCP: ((Tcp_packet *)prot_base)->src_port(port); return;
|
|
|
|
case L3_protocol::UDP: ((Udp_packet *)prot_base)->src_port(port); return;
|
2016-09-12 12:55:12 +02:00
|
|
|
default: throw Interface::Bad_transport_protocol(); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
static void *_prot_base(L3_protocol const prot,
|
|
|
|
size_t const prot_size,
|
|
|
|
Ipv4_packet &ip)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
switch (prot) {
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::TCP: return new (ip.data<void>()) Tcp_packet(prot_size);
|
|
|
|
case L3_protocol::UDP: return new (ip.data<void>()) Udp_packet(prot_size);
|
2016-09-12 12:55:12 +02:00
|
|
|
default: throw Interface::Bad_transport_protocol(); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
/***************
|
|
|
|
** Interface **
|
|
|
|
***************/
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
void Interface::_pass_prot(Ethernet_frame ð,
|
|
|
|
size_t const eth_size,
|
|
|
|
Ipv4_packet &ip,
|
|
|
|
L3_protocol const prot,
|
|
|
|
void *const prot_base,
|
|
|
|
size_t const prot_size)
|
2017-09-21 20:00:37 +02:00
|
|
|
{
|
|
|
|
_update_checksum(prot, prot_base, prot_size, ip.src(), ip.dst());
|
|
|
|
_pass_ip(eth, eth_size, ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
void Interface::_pass_ip(Ethernet_frame ð,
|
|
|
|
size_t const eth_size,
|
2017-09-21 20:00:37 +02:00
|
|
|
Ipv4_packet &ip)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
ip.checksum(Ipv4_packet::calculate_checksum(ip));
|
nic_router: DHCP client functionality
If the attribute 'interface' is not set in a 'domain' tag, the router tries to
dynamically receive and maintain an IP configuration for that domain by using
DHCP in the client role at all interfaces that connect to the domain. In the
DHCP discover phase, the router simply chooses the first DHCP offer that
arrives. So, no comparison of different DHCP offers is done. In the DHCP
request phase, the server is expected to provide an IP address, a gateway, a
subnet mask, and an IP lease time to the router. If anything substantial goes
wrong during a DHCP exchange, the router discards the outcome of the exchange
and goes back to the DHCP discover phase. At any time where there is no valid
IP configuration present at a domain, the domain does only act as DHCP client
and all other router functionality is disabled for the domain. A domain cannot
act as DHCP client and DHCP server at once. So, a 'domain' tag must either
have an 'interface' attribute or must not contain a 'dhcp-server' tag.
Ref #2534
2017-10-16 11:31:43 +02:00
|
|
|
send(eth, eth_size);
|
2016-09-12 12:55:12 +02:00
|
|
|
}
|
2016-08-25 17:48:53 +02:00
|
|
|
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
Forward_rule_tree &
|
|
|
|
Interface::_forward_rules(L3_protocol const prot) const
|
2016-09-12 12:55:12 +02:00
|
|
|
{
|
|
|
|
switch (prot) {
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::TCP: return _domain.tcp_forward_rules();
|
|
|
|
case L3_protocol::UDP: return _domain.udp_forward_rules();
|
2016-09-12 12:55:12 +02:00
|
|
|
default: throw Bad_transport_protocol(); }
|
|
|
|
}
|
2016-08-25 17:48:53 +02:00
|
|
|
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
Transport_rule_list &
|
|
|
|
Interface::_transport_rules(L3_protocol const prot) const
|
2016-09-12 12:55:12 +02:00
|
|
|
{
|
|
|
|
switch (prot) {
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::TCP: return _domain.tcp_rules();
|
|
|
|
case L3_protocol::UDP: return _domain.udp_rules();
|
2016-09-12 12:55:12 +02:00
|
|
|
default: throw Bad_transport_protocol(); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
void
|
2017-09-14 15:10:32 +02:00
|
|
|
Interface::_new_link(L3_protocol const protocol,
|
2016-09-12 12:55:12 +02:00
|
|
|
Link_side_id const &local,
|
|
|
|
Pointer<Port_allocator_guard> const remote_port_alloc,
|
|
|
|
Interface &remote_interface,
|
|
|
|
Link_side_id const &remote)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
switch (protocol) {
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::TCP:
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
Tcp_link &link = *new (_alloc)
|
|
|
|
Tcp_link(*this, local, remote_port_alloc, remote_interface,
|
|
|
|
remote, _timer, _config(), protocol);
|
|
|
|
_tcp_links.insert(&link.client());
|
|
|
|
remote_interface._tcp_links.insert(&link.server());
|
|
|
|
if (_config().verbose()) {
|
|
|
|
log("New TCP client link: ", link.client(), " at ", *this);
|
|
|
|
log("New TCP server link: ", link.server(),
|
|
|
|
" at ", remote_interface._domain);
|
|
|
|
}
|
|
|
|
return;
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::UDP:
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
Udp_link &link = *new (_alloc)
|
|
|
|
Udp_link(*this, local, remote_port_alloc, remote_interface,
|
|
|
|
remote, _timer, _config(), protocol);
|
|
|
|
_udp_links.insert(&link.client());
|
|
|
|
remote_interface._udp_links.insert(&link.server());
|
|
|
|
if (_config().verbose()) {
|
|
|
|
log("New UDP client link: ", link.client(), " at ", *this);
|
|
|
|
log("New UDP server link: ", link.server(),
|
|
|
|
" at ", remote_interface._domain);
|
|
|
|
}
|
|
|
|
return;
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
2016-09-12 12:55:12 +02:00
|
|
|
default: throw Bad_transport_protocol(); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
Link_side_tree &Interface::_links(L3_protocol const protocol)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
switch (protocol) {
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::TCP: return _tcp_links;
|
|
|
|
case L3_protocol::UDP: return _udp_links;
|
2016-09-12 12:55:12 +02:00
|
|
|
default: throw Bad_transport_protocol(); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
void Interface::link_closed(Link &link, L3_protocol const prot)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
_closed_links(prot).insert(&link);
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-06 13:00:05 +02:00
|
|
|
void Interface::ip_allocation_expired(Ip_allocation &allocation)
|
|
|
|
{
|
|
|
|
_release_ip_allocation(allocation);
|
|
|
|
_released_ip_allocations.insert(&allocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
void Interface::dissolve_link(Link_side &link_side, L3_protocol const prot)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
_links(prot).remove(&link_side);
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
Link_list &Interface::_closed_links(L3_protocol const protocol)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
switch (protocol) {
|
2017-09-14 15:10:32 +02:00
|
|
|
case L3_protocol::TCP: return _closed_tcp_links;
|
|
|
|
case L3_protocol::UDP: return _closed_udp_links;
|
2016-09-12 12:55:12 +02:00
|
|
|
default: throw Bad_transport_protocol(); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
void Interface::_adapt_eth(Ethernet_frame ð,
|
|
|
|
size_t const eth_size,
|
|
|
|
Ipv4_address const &ip,
|
|
|
|
Packet_descriptor const &pkt,
|
|
|
|
Interface &interface)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
Ipv4_address const &hop_ip = interface._domain.next_hop(ip);
|
|
|
|
try { eth.dst(interface._arp_cache.find_by_ip(hop_ip).mac()); }
|
|
|
|
catch (Arp_cache::No_match) {
|
|
|
|
interface._broadcast_arp_request(hop_ip);
|
|
|
|
new (_alloc) Arp_waiter(*this, interface, hop_ip, pkt);
|
|
|
|
throw Packet_postponed();
|
|
|
|
}
|
|
|
|
eth.src(_router_mac);
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-14 15:10:32 +02:00
|
|
|
void Interface::_nat_link_and_pass(Ethernet_frame ð,
|
|
|
|
size_t const eth_size,
|
|
|
|
Ipv4_packet &ip,
|
|
|
|
L3_protocol const prot,
|
|
|
|
void *const prot_base,
|
|
|
|
size_t const prot_size,
|
|
|
|
Link_side_id const &local,
|
|
|
|
Interface &interface)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
Pointer<Port_allocator_guard> remote_port_alloc;
|
|
|
|
try {
|
|
|
|
Nat_rule &nat = interface._domain.nat_rules().find_by_domain(_domain);
|
|
|
|
if(_config().verbose()) {
|
|
|
|
log("Using NAT rule: ", nat); }
|
2016-08-25 17:48:53 +02:00
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
_src_port(prot, prot_base, nat.port_alloc(prot).alloc());
|
|
|
|
ip.src(interface._router_ip());
|
|
|
|
remote_port_alloc.set(nat.port_alloc(prot));
|
|
|
|
}
|
|
|
|
catch (Nat_rule_tree::No_match) { }
|
|
|
|
Link_side_id const remote = { ip.dst(), _dst_port(prot, prot_base),
|
|
|
|
ip.src(), _src_port(prot, prot_base) };
|
|
|
|
_new_link(prot, local, remote_port_alloc, interface, remote);
|
2017-09-21 20:00:37 +02:00
|
|
|
interface._pass_prot(eth, eth_size, ip, prot, prot_base, prot_size);
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-06 13:00:05 +02:00
|
|
|
void Interface::_send_dhcp_reply(Dhcp_server const &dhcp_srv,
|
|
|
|
Mac_address const &client_mac,
|
|
|
|
Ipv4_address const &client_ip,
|
|
|
|
Dhcp_packet::Message_type msg_type,
|
|
|
|
uint32_t xid)
|
|
|
|
{
|
|
|
|
/* allocate buffer for the reply */
|
|
|
|
enum { BUF_SIZE = 512 };
|
nic_router: DHCP client functionality
If the attribute 'interface' is not set in a 'domain' tag, the router tries to
dynamically receive and maintain an IP configuration for that domain by using
DHCP in the client role at all interfaces that connect to the domain. In the
DHCP discover phase, the router simply chooses the first DHCP offer that
arrives. So, no comparison of different DHCP offers is done. In the DHCP
request phase, the server is expected to provide an IP address, a gateway, a
subnet mask, and an IP lease time to the router. If anything substantial goes
wrong during a DHCP exchange, the router discards the outcome of the exchange
and goes back to the DHCP discover phase. At any time where there is no valid
IP configuration present at a domain, the domain does only act as DHCP client
and all other router functionality is disabled for the domain. A domain cannot
act as DHCP client and DHCP server at once. So, a 'domain' tag must either
have an 'interface' attribute or must not contain a 'dhcp-server' tag.
Ref #2534
2017-10-16 11:31:43 +02:00
|
|
|
using Size_guard = Size_guard_tpl<BUF_SIZE, Dhcp_msg_buffer_too_small>;
|
2017-10-06 13:00:05 +02:00
|
|
|
void *buf;
|
|
|
|
try { _alloc.alloc(BUF_SIZE, &buf); }
|
nic_router: DHCP client functionality
If the attribute 'interface' is not set in a 'domain' tag, the router tries to
dynamically receive and maintain an IP configuration for that domain by using
DHCP in the client role at all interfaces that connect to the domain. In the
DHCP discover phase, the router simply chooses the first DHCP offer that
arrives. So, no comparison of different DHCP offers is done. In the DHCP
request phase, the server is expected to provide an IP address, a gateway, a
subnet mask, and an IP lease time to the router. If anything substantial goes
wrong during a DHCP exchange, the router discards the outcome of the exchange
and goes back to the DHCP discover phase. At any time where there is no valid
IP configuration present at a domain, the domain does only act as DHCP client
and all other router functionality is disabled for the domain. A domain cannot
act as DHCP client and DHCP server at once. So, a 'domain' tag must either
have an 'interface' attribute or must not contain a 'dhcp-server' tag.
Ref #2534
2017-10-16 11:31:43 +02:00
|
|
|
catch (...) { throw Alloc_dhcp_msg_buffer_failed(); }
|
2017-10-06 13:00:05 +02:00
|
|
|
|
|
|
|
/* create ETH header of the reply */
|
2017-10-16 12:42:06 +02:00
|
|
|
Size_guard size;
|
|
|
|
size.add(sizeof(Ethernet_frame));
|
|
|
|
Ethernet_frame ð = *reinterpret_cast<Ethernet_frame *>(buf);
|
|
|
|
eth.dst(client_mac);
|
|
|
|
eth.src(_router_mac);
|
|
|
|
eth.type(Ethernet_frame::Type::IPV4);
|
2017-10-06 13:00:05 +02:00
|
|
|
|
|
|
|
/* create IP header of the reply */
|
|
|
|
enum { IPV4_TIME_TO_LIVE = 64 };
|
2017-10-16 12:42:06 +02:00
|
|
|
size_t const ip_off = size.curr();
|
|
|
|
size.add(sizeof(Ipv4_packet));
|
|
|
|
Ipv4_packet &ip = *eth.data<Ipv4_packet>();
|
|
|
|
ip.header_length(sizeof(Ipv4_packet) / 4);
|
|
|
|
ip.version(4);
|
|
|
|
ip.diff_service(0);
|
|
|
|
ip.identification(0);
|
|
|
|
ip.flags(0);
|
|
|
|
ip.fragment_offset(0);
|
|
|
|
ip.time_to_live(IPV4_TIME_TO_LIVE);
|
|
|
|
ip.protocol(Ipv4_packet::Protocol::UDP);
|
|
|
|
ip.src(_router_ip());
|
|
|
|
ip.dst(client_ip);
|
2017-10-06 13:00:05 +02:00
|
|
|
|
|
|
|
/* create UDP header of the reply */
|
2017-10-16 12:42:06 +02:00
|
|
|
size_t const udp_off = size.curr();
|
|
|
|
size.add(sizeof(Udp_packet));
|
|
|
|
Udp_packet &udp = *ip.data<Udp_packet>();
|
|
|
|
udp.src_port(Port(Dhcp_packet::BOOTPS));
|
|
|
|
udp.dst_port(Port(Dhcp_packet::BOOTPC));
|
2017-10-06 13:00:05 +02:00
|
|
|
|
|
|
|
/* create mandatory DHCP fields of the reply */
|
2017-10-16 12:42:06 +02:00
|
|
|
size.add(sizeof(Dhcp_packet));
|
|
|
|
Dhcp_packet &dhcp = *udp.data<Dhcp_packet>();
|
|
|
|
dhcp.op(Dhcp_packet::REPLY);
|
|
|
|
dhcp.htype(Dhcp_packet::Htype::ETH);
|
|
|
|
dhcp.hlen(sizeof(Mac_address));
|
|
|
|
dhcp.hops(0);
|
|
|
|
dhcp.xid(xid);
|
|
|
|
dhcp.secs(0);
|
|
|
|
dhcp.flags(0);
|
|
|
|
dhcp.ciaddr(msg_type == Dhcp_packet::Message_type::INFORM ? client_ip : Ipv4_address());
|
|
|
|
dhcp.yiaddr(msg_type == Dhcp_packet::Message_type::INFORM ? Ipv4_address() : client_ip);
|
|
|
|
dhcp.siaddr(_router_ip());
|
|
|
|
dhcp.giaddr(Ipv4_address());
|
|
|
|
dhcp.client_mac(client_mac);
|
|
|
|
dhcp.zero_fill_sname();
|
|
|
|
dhcp.zero_fill_file();
|
|
|
|
dhcp.default_magic_cookie();
|
2017-10-06 13:00:05 +02:00
|
|
|
|
|
|
|
/* append DHCP option fields to the reply */
|
2017-10-16 12:42:06 +02:00
|
|
|
Dhcp_packet::Options_aggregator<Size_guard> dhcp_opts(dhcp, size);
|
|
|
|
dhcp_opts.append_option<Dhcp_packet::Message_type_option>(msg_type);
|
|
|
|
dhcp_opts.append_option<Dhcp_packet::Server_ipv4>(_router_ip());
|
|
|
|
dhcp_opts.append_option<Dhcp_packet::Ip_lease_time>(dhcp_srv.ip_lease_time().value / 1000 / 1000);
|
|
|
|
dhcp_opts.append_option<Dhcp_packet::Subnet_mask>(_ip_config().interface.subnet_mask());
|
|
|
|
dhcp_opts.append_option<Dhcp_packet::Router_ipv4>(_router_ip());
|
2017-10-06 13:00:05 +02:00
|
|
|
if (dhcp_srv.dns_server().valid()) {
|
2017-10-16 12:42:06 +02:00
|
|
|
dhcp_opts.append_option<Dhcp_packet::Dns_server_ipv4>(dhcp_srv.dns_server()); }
|
|
|
|
dhcp_opts.append_option<Dhcp_packet::Broadcast_addr>(_ip_config().interface.broadcast_address());
|
|
|
|
dhcp_opts.append_option<Dhcp_packet::Options_end>();
|
2017-10-06 13:00:05 +02:00
|
|
|
|
|
|
|
/* fill in header values that need the packet to be complete already */
|
2017-10-16 12:42:06 +02:00
|
|
|
udp.length(size.curr() - udp_off);
|
|
|
|
udp.update_checksum(ip.src(), ip.dst());
|
|
|
|
ip.total_length(size.curr() - ip_off);
|
|
|
|
ip.checksum(Ipv4_packet::calculate_checksum(ip));
|
2017-10-06 13:00:05 +02:00
|
|
|
|
|
|
|
/* send reply to sender of request and free reply buffer */
|
2017-10-16 12:42:06 +02:00
|
|
|
send(eth, size.curr());
|
2017-10-06 13:00:05 +02:00
|
|
|
_alloc.free(buf, BUF_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Interface::_release_ip_allocation(Ip_allocation &allocation)
|
|
|
|
{
|
|
|
|
if (_config().verbose()) {
|
|
|
|
log("Release IP allocation: ", allocation, " at ", *this);
|
|
|
|
}
|
|
|
|
_ip_allocations.remove(&allocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Interface::_handle_dhcp_request(Ethernet_frame ð,
|
|
|
|
Genode::size_t eth_size,
|
|
|
|
Dhcp_packet &dhcp)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
/* try to get the DHCP server config of this interface */
|
|
|
|
Dhcp_server &dhcp_srv = _domain.dhcp_server();
|
|
|
|
|
|
|
|
/* determine type of DHCP request */
|
|
|
|
Dhcp_packet::Message_type const msg_type =
|
|
|
|
dhcp.option<Dhcp_packet::Message_type_option>().value();
|
|
|
|
|
|
|
|
try {
|
|
|
|
/* look up existing DHCP configuration for client */
|
|
|
|
Ip_allocation &allocation =
|
|
|
|
_ip_allocations.find_by_mac(dhcp.client_mac());
|
|
|
|
|
|
|
|
switch (msg_type) {
|
|
|
|
case Dhcp_packet::Message_type::DISCOVER:
|
|
|
|
|
|
|
|
if (allocation.bound()) {
|
|
|
|
throw Bad_dhcp_request();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
allocation.lifetime(_config().rtt());
|
|
|
|
_send_dhcp_reply(dhcp_srv, eth.src(),
|
|
|
|
allocation.ip(),
|
|
|
|
Dhcp_packet::Message_type::OFFER,
|
|
|
|
dhcp.xid());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case Dhcp_packet::Message_type::REQUEST:
|
|
|
|
|
|
|
|
if (allocation.bound()) {
|
|
|
|
allocation.lifetime(dhcp_srv.ip_lease_time());
|
|
|
|
_send_dhcp_reply(dhcp_srv, eth.src(),
|
|
|
|
allocation.ip(),
|
|
|
|
Dhcp_packet::Message_type::ACK,
|
|
|
|
dhcp.xid());
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Dhcp_packet::Server_ipv4 &dhcp_srv_ip =
|
|
|
|
dhcp.option<Dhcp_packet::Server_ipv4>();
|
|
|
|
|
|
|
|
if (dhcp_srv_ip.value() == _router_ip()) {
|
|
|
|
|
|
|
|
allocation.set_bound();
|
|
|
|
allocation.lifetime(dhcp_srv.ip_lease_time());
|
|
|
|
if (_config().verbose()) {
|
|
|
|
log("Bind IP allocation: ", allocation,
|
|
|
|
" at ", *this);
|
|
|
|
}
|
|
|
|
_send_dhcp_reply(dhcp_srv, eth.src(),
|
|
|
|
allocation.ip(),
|
|
|
|
Dhcp_packet::Message_type::ACK,
|
|
|
|
dhcp.xid());
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
_release_ip_allocation(allocation);
|
|
|
|
_destroy_ip_allocation(allocation);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case Dhcp_packet::Message_type::INFORM:
|
|
|
|
|
|
|
|
_send_dhcp_reply(dhcp_srv, eth.src(),
|
|
|
|
allocation.ip(),
|
|
|
|
Dhcp_packet::Message_type::ACK,
|
|
|
|
dhcp.xid());
|
|
|
|
return;
|
|
|
|
|
|
|
|
case Dhcp_packet::Message_type::DECLINE:
|
|
|
|
case Dhcp_packet::Message_type::RELEASE:
|
|
|
|
|
|
|
|
_release_ip_allocation(allocation);
|
|
|
|
_destroy_ip_allocation(allocation);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case Dhcp_packet::Message_type::NAK:
|
|
|
|
case Dhcp_packet::Message_type::OFFER:
|
|
|
|
case Dhcp_packet::Message_type::ACK:
|
|
|
|
default: throw Bad_dhcp_request();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Ip_allocation_tree::No_match) {
|
|
|
|
|
|
|
|
switch (msg_type) {
|
|
|
|
case Dhcp_packet::Message_type::DISCOVER:
|
|
|
|
{
|
|
|
|
Ip_allocation &allocation = *new (_alloc)
|
|
|
|
Ip_allocation(*this, _config(),
|
|
|
|
dhcp_srv.alloc_ip(),
|
|
|
|
dhcp.client_mac(), _timer,
|
|
|
|
_config().rtt());
|
|
|
|
|
|
|
|
_ip_allocations.insert(&allocation);
|
|
|
|
if (_config().verbose()) {
|
|
|
|
log("Offer IP allocation: ", allocation,
|
|
|
|
" at ", *this);
|
|
|
|
}
|
|
|
|
_send_dhcp_reply(dhcp_srv, eth.src(),
|
|
|
|
allocation.ip(),
|
|
|
|
Dhcp_packet::Message_type::OFFER,
|
|
|
|
dhcp.xid());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case Dhcp_packet::Message_type::REQUEST:
|
|
|
|
case Dhcp_packet::Message_type::DECLINE:
|
|
|
|
case Dhcp_packet::Message_type::RELEASE:
|
|
|
|
case Dhcp_packet::Message_type::NAK:
|
|
|
|
case Dhcp_packet::Message_type::OFFER:
|
|
|
|
case Dhcp_packet::Message_type::ACK:
|
|
|
|
default: throw Bad_dhcp_request();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Dhcp_packet::Option_not_found) {
|
|
|
|
throw Bad_dhcp_request();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
void Interface::_handle_ip(Ethernet_frame ð,
|
|
|
|
Genode::size_t const eth_size,
|
|
|
|
Packet_descriptor const &pkt)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
/* read packet information */
|
|
|
|
Ipv4_packet &ip = *new (eth.data<void>())
|
|
|
|
Ipv4_packet(eth_size - sizeof(Ethernet_frame));
|
2016-08-25 17:48:53 +02:00
|
|
|
|
2017-09-21 20:00:37 +02:00
|
|
|
/* try to route via transport layer rules */
|
2016-08-25 17:48:53 +02:00
|
|
|
try {
|
2017-10-16 12:42:06 +02:00
|
|
|
L3_protocol const prot = ip.protocol();
|
|
|
|
size_t const prot_size = ip.total_length() - ip.header_length() * 4;
|
|
|
|
void *const prot_base = _prot_base(prot, prot_size, ip);
|
2017-10-06 13:00:05 +02:00
|
|
|
|
|
|
|
/* try handling DHCP requests before trying any routing */
|
|
|
|
if (prot == L3_protocol::UDP) {
|
|
|
|
Udp_packet &udp = *new (ip.data<void>())
|
|
|
|
Udp_packet(eth_size - sizeof(Ipv4_packet));
|
|
|
|
|
|
|
|
if (Dhcp_packet::is_dhcp(&udp)) {
|
|
|
|
|
|
|
|
/* get DHCP packet */
|
|
|
|
Dhcp_packet &dhcp = *new (udp.data<void>())
|
|
|
|
Dhcp_packet(eth_size - sizeof(Ipv4_packet)
|
|
|
|
- sizeof(Udp_packet));
|
|
|
|
|
|
|
|
if (dhcp.op() == Dhcp_packet::REQUEST) {
|
|
|
|
try {
|
|
|
|
_handle_dhcp_request(eth, eth_size, dhcp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (Pointer<Dhcp_server>::Invalid) { }
|
nic_router: DHCP client functionality
If the attribute 'interface' is not set in a 'domain' tag, the router tries to
dynamically receive and maintain an IP configuration for that domain by using
DHCP in the client role at all interfaces that connect to the domain. In the
DHCP discover phase, the router simply chooses the first DHCP offer that
arrives. So, no comparison of different DHCP offers is done. In the DHCP
request phase, the server is expected to provide an IP address, a gateway, a
subnet mask, and an IP lease time to the router. If anything substantial goes
wrong during a DHCP exchange, the router discards the outcome of the exchange
and goes back to the DHCP discover phase. At any time where there is no valid
IP configuration present at a domain, the domain does only act as DHCP client
and all other router functionality is disabled for the domain. A domain cannot
act as DHCP client and DHCP server at once. So, a 'domain' tag must either
have an 'interface' attribute or must not contain a 'dhcp-server' tag.
Ref #2534
2017-10-16 11:31:43 +02:00
|
|
|
} else {
|
|
|
|
_dhcp_client.handle_ip(eth, eth_size);
|
|
|
|
return;
|
2017-10-06 13:00:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-16 12:42:06 +02:00
|
|
|
Link_side_id const local = { ip.src(), _src_port(prot, prot_base),
|
|
|
|
ip.dst(), _dst_port(prot, prot_base) };
|
2016-09-12 12:55:12 +02:00
|
|
|
|
2017-09-21 20:00:37 +02:00
|
|
|
/* try to route via existing UDP/TCP links */
|
|
|
|
try {
|
|
|
|
Link_side const &local_side = _links(prot).find_by_id(local);
|
|
|
|
Link &link = local_side.link();
|
|
|
|
bool const client = local_side.is_client();
|
|
|
|
Link_side &remote_side = client ? link.server() : link.client();
|
|
|
|
Interface &interface = remote_side.interface();
|
2017-10-06 13:00:05 +02:00
|
|
|
if (_config().verbose()) {
|
2017-09-14 15:10:32 +02:00
|
|
|
log("Using ", l3_protocol_name(prot), " link: ", link); }
|
2016-09-12 12:55:12 +02:00
|
|
|
|
2017-09-21 20:00:37 +02:00
|
|
|
_adapt_eth(eth, eth_size, remote_side.src_ip(), pkt, interface);
|
|
|
|
ip.src(remote_side.dst_ip());
|
|
|
|
ip.dst(remote_side.src_ip());
|
|
|
|
_src_port(prot, prot_base, remote_side.dst_port());
|
|
|
|
_dst_port(prot, prot_base, remote_side.src_port());
|
2016-08-25 17:48:53 +02:00
|
|
|
|
2017-09-21 20:00:37 +02:00
|
|
|
interface._pass_prot(eth, eth_size, ip, prot, prot_base, prot_size);
|
|
|
|
_link_packet(prot, prot_base, link, client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (Link_side_tree::No_match) { }
|
|
|
|
|
|
|
|
/* try to route via forward rules */
|
|
|
|
if (local.dst_ip == _router_ip()) {
|
|
|
|
try {
|
|
|
|
Forward_rule const &rule =
|
|
|
|
_forward_rules(prot).find_by_port(local.dst_port);
|
|
|
|
|
|
|
|
Interface &interface = rule.domain().interface().deref();
|
|
|
|
if(_config().verbose()) {
|
2017-09-14 15:10:32 +02:00
|
|
|
log("Using forward rule: ", l3_protocol_name(prot), " ", rule); }
|
2017-09-21 20:00:37 +02:00
|
|
|
|
|
|
|
_adapt_eth(eth, eth_size, rule.to(), pkt, interface);
|
|
|
|
ip.dst(rule.to());
|
|
|
|
_nat_link_and_pass(eth, eth_size, ip, prot, prot_base, prot_size,
|
|
|
|
local, interface);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (Forward_rule_tree::No_match) { }
|
|
|
|
}
|
|
|
|
/* try to route via transport and permit rules */
|
2016-09-12 12:55:12 +02:00
|
|
|
try {
|
2017-09-21 20:00:37 +02:00
|
|
|
Transport_rule const &transport_rule =
|
|
|
|
_transport_rules(prot).longest_prefix_match(local.dst_ip);
|
2016-08-25 17:48:53 +02:00
|
|
|
|
2017-09-21 20:00:37 +02:00
|
|
|
Permit_rule const &permit_rule =
|
|
|
|
transport_rule.permit_rule(local.dst_port);
|
|
|
|
|
|
|
|
Interface &interface = permit_rule.domain().interface().deref();
|
2016-09-12 12:55:12 +02:00
|
|
|
if(_config().verbose()) {
|
2017-09-14 15:10:32 +02:00
|
|
|
log("Using ", l3_protocol_name(prot), " rule: ", transport_rule,
|
2017-09-21 20:00:37 +02:00
|
|
|
" ", permit_rule); }
|
2016-08-25 17:48:53 +02:00
|
|
|
|
2017-09-21 20:00:37 +02:00
|
|
|
_adapt_eth(eth, eth_size, local.dst_ip, pkt, interface);
|
2016-09-12 12:55:12 +02:00
|
|
|
_nat_link_and_pass(eth, eth_size, ip, prot, prot_base, prot_size,
|
|
|
|
local, interface);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-21 20:00:37 +02:00
|
|
|
catch (Transport_rule_list::No_match) { }
|
|
|
|
catch (Permit_single_rule_tree::No_match) { }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
2017-09-21 20:00:37 +02:00
|
|
|
catch (Interface::Bad_transport_protocol) { }
|
2016-08-25 17:48:53 +02:00
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
/* try to route via IP rules */
|
|
|
|
try {
|
|
|
|
Ip_rule const &rule =
|
2017-09-21 20:00:37 +02:00
|
|
|
_domain.ip_rules().longest_prefix_match(ip.dst());
|
2016-08-25 17:48:53 +02:00
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
Interface &interface = rule.domain().interface().deref();
|
|
|
|
if(_config().verbose()) {
|
|
|
|
log("Using IP rule: ", rule); }
|
2016-08-25 17:48:53 +02:00
|
|
|
|
2017-09-21 20:00:37 +02:00
|
|
|
_adapt_eth(eth, eth_size, ip.dst(), pkt, interface);
|
|
|
|
interface._pass_ip(eth, eth_size, ip);
|
2016-09-12 12:55:12 +02:00
|
|
|
return;
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
2016-09-12 12:55:12 +02:00
|
|
|
catch (Ip_rule_list::No_match) { }
|
2016-08-25 17:48:53 +02:00
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
/* give up and drop packet */
|
|
|
|
if (_config().verbose()) {
|
|
|
|
log("Unroutable packet"); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
void Interface::_broadcast_arp_request(Ipv4_address const &ip)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
|
|
|
using Ethernet_arp = Ethernet_frame_sized<sizeof(Arp_packet)>;
|
2017-09-13 13:10:39 +02:00
|
|
|
Ethernet_arp eth_arp(Mac_address(0xff), _router_mac, Ethernet_frame::Type::ARP);
|
2016-09-12 12:55:12 +02:00
|
|
|
void *const eth_data = eth_arp.data<void>();
|
2016-08-25 17:48:53 +02:00
|
|
|
size_t const arp_size = sizeof(eth_arp) - sizeof(Ethernet_frame);
|
2016-09-12 12:55:12 +02:00
|
|
|
Arp_packet &arp = *new (eth_data) Arp_packet(arp_size);
|
|
|
|
arp.hardware_address_type(Arp_packet::ETHERNET);
|
|
|
|
arp.protocol_address_type(Arp_packet::IPV4);
|
|
|
|
arp.hardware_address_size(sizeof(Mac_address));
|
|
|
|
arp.protocol_address_size(sizeof(Ipv4_address));
|
|
|
|
arp.opcode(Arp_packet::REQUEST);
|
|
|
|
arp.src_mac(_router_mac);
|
|
|
|
arp.src_ip(_router_ip());
|
|
|
|
arp.dst_mac(Mac_address(0xff));
|
|
|
|
arp.dst_ip(ip);
|
nic_router: DHCP client functionality
If the attribute 'interface' is not set in a 'domain' tag, the router tries to
dynamically receive and maintain an IP configuration for that domain by using
DHCP in the client role at all interfaces that connect to the domain. In the
DHCP discover phase, the router simply chooses the first DHCP offer that
arrives. So, no comparison of different DHCP offers is done. In the DHCP
request phase, the server is expected to provide an IP address, a gateway, a
subnet mask, and an IP lease time to the router. If anything substantial goes
wrong during a DHCP exchange, the router discards the outcome of the exchange
and goes back to the DHCP discover phase. At any time where there is no valid
IP configuration present at a domain, the domain does only act as DHCP client
and all other router functionality is disabled for the domain. A domain cannot
act as DHCP client and DHCP server at once. So, a 'domain' tag must either
have an 'interface' attribute or must not contain a 'dhcp-server' tag.
Ref #2534
2017-10-16 11:31:43 +02:00
|
|
|
send(eth_arp, sizeof(eth_arp));
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
void Interface::_handle_arp_reply(Arp_packet &arp)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
/* do nothing if ARP info already exists */
|
2016-08-25 17:48:53 +02:00
|
|
|
try {
|
2016-09-12 12:55:12 +02:00
|
|
|
_arp_cache.find_by_ip(arp.src_ip());
|
|
|
|
if (_config().verbose()) {
|
2016-08-25 17:48:53 +02:00
|
|
|
log("ARP entry already exists"); }
|
|
|
|
|
|
|
|
return;
|
2016-09-12 12:55:12 +02:00
|
|
|
}
|
|
|
|
/* create cache entry and continue handling of matching packets */
|
|
|
|
catch (Arp_cache::No_match) {
|
|
|
|
Ipv4_address const ip = arp.src_ip();
|
|
|
|
_arp_cache.new_entry(ip, arp.src_mac());
|
|
|
|
for (Arp_waiter_list_element *waiter_le = _foreign_arp_waiters.first();
|
|
|
|
waiter_le; )
|
|
|
|
{
|
|
|
|
Arp_waiter &waiter = *waiter_le->object();
|
|
|
|
waiter_le = waiter_le->next();
|
|
|
|
if (ip != waiter.ip()) { continue; }
|
|
|
|
waiter.src()._continue_handle_eth(waiter.packet());
|
|
|
|
destroy(waiter.src()._alloc, &waiter);
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
Ipv4_address const &Interface::_router_ip() const
|
|
|
|
{
|
2017-10-13 16:22:05 +02:00
|
|
|
return _ip_config().interface.address;
|
2016-09-12 12:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Interface::_handle_arp_request(Ethernet_frame ð,
|
|
|
|
size_t const eth_size,
|
|
|
|
Arp_packet &arp)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2017-09-25 17:40:35 +02:00
|
|
|
/*
|
|
|
|
* We handle ARP only if it asks for the routers IP or if the router
|
|
|
|
* shall forward an ARP to a foreign address as gateway. The second
|
|
|
|
* is the case if no gateway attribute is specified (so we're the
|
|
|
|
* gateway) and the address is not of the same subnet like the interface
|
|
|
|
* attribute.
|
|
|
|
*/
|
|
|
|
if (arp.dst_ip() != _router_ip() &&
|
2017-10-13 16:22:05 +02:00
|
|
|
(_ip_config().gateway_valid ||
|
|
|
|
_ip_config().interface.prefix_matches(arp.dst_ip())))
|
2017-09-25 17:40:35 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
if (_config().verbose()) {
|
2017-09-25 17:40:35 +02:00
|
|
|
log("Ignore ARP request"); }
|
2016-08-25 17:48:53 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* interchange source and destination MAC and IP addresses */
|
2017-09-25 17:40:35 +02:00
|
|
|
Ipv4_address dst_ip = arp.dst_ip();
|
2016-09-12 12:55:12 +02:00
|
|
|
arp.dst_ip(arp.src_ip());
|
|
|
|
arp.dst_mac(arp.src_mac());
|
|
|
|
eth.dst(eth.src());
|
2017-09-25 17:40:35 +02:00
|
|
|
arp.src_ip(dst_ip);
|
2016-09-12 12:55:12 +02:00
|
|
|
arp.src_mac(_router_mac);
|
|
|
|
eth.src(_router_mac);
|
2016-08-25 17:48:53 +02:00
|
|
|
|
|
|
|
/* mark packet as reply and send it back to its sender */
|
2016-09-12 12:55:12 +02:00
|
|
|
arp.opcode(Arp_packet::REPLY);
|
nic_router: DHCP client functionality
If the attribute 'interface' is not set in a 'domain' tag, the router tries to
dynamically receive and maintain an IP configuration for that domain by using
DHCP in the client role at all interfaces that connect to the domain. In the
DHCP discover phase, the router simply chooses the first DHCP offer that
arrives. So, no comparison of different DHCP offers is done. In the DHCP
request phase, the server is expected to provide an IP address, a gateway, a
subnet mask, and an IP lease time to the router. If anything substantial goes
wrong during a DHCP exchange, the router discards the outcome of the exchange
and goes back to the DHCP discover phase. At any time where there is no valid
IP configuration present at a domain, the domain does only act as DHCP client
and all other router functionality is disabled for the domain. A domain cannot
act as DHCP client and DHCP server at once. So, a 'domain' tag must either
have an 'interface' attribute or must not contain a 'dhcp-server' tag.
Ref #2534
2017-10-16 11:31:43 +02:00
|
|
|
send(eth, eth_size);
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
void Interface::_handle_arp(Ethernet_frame ð, size_t const eth_size)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
|
|
|
/* ignore ARP regarding protocols other than IPv4 via ethernet */
|
2016-09-12 12:55:12 +02:00
|
|
|
size_t const arp_size = eth_size - sizeof(Ethernet_frame);
|
|
|
|
Arp_packet &arp = *new (eth.data<void>()) Arp_packet(arp_size);
|
|
|
|
if (!arp.ethernet_ipv4()) {
|
|
|
|
error("ARP for unknown protocol"); }
|
|
|
|
|
|
|
|
switch (arp.opcode()) {
|
2016-08-25 17:48:53 +02:00
|
|
|
case Arp_packet::REPLY: _handle_arp_reply(arp); break;
|
|
|
|
case Arp_packet::REQUEST: _handle_arp_request(eth, eth_size, arp); break;
|
2016-09-12 12:55:12 +02:00
|
|
|
default: error("unknown ARP operation"); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
void Interface::_ready_to_submit()
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
while (_sink().packet_avail()) {
|
2016-08-25 17:48:53 +02:00
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
Packet_descriptor const pkt = _sink().get_packet();
|
|
|
|
if (!pkt.size()) {
|
2016-08-25 17:48:53 +02:00
|
|
|
continue; }
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
try { _handle_eth(_sink().packet_content(pkt), pkt.size(), pkt); }
|
|
|
|
catch (Packet_postponed) { continue; }
|
|
|
|
_ack_packet(pkt);
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
void Interface::_continue_handle_eth(Packet_descriptor const &pkt)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
try { _handle_eth(_sink().packet_content(pkt), pkt.size(), pkt); }
|
|
|
|
catch (Packet_postponed) { error("failed twice to handle packet"); }
|
|
|
|
_ack_packet(pkt);
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
void Interface::_ready_to_ack()
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
while (_source().ack_avail()) {
|
|
|
|
_source().release_packet(_source().get_acked_packet()); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-06 13:00:05 +02:00
|
|
|
void Interface::_destroy_ip_allocation(Ip_allocation &allocation)
|
|
|
|
{
|
|
|
|
_domain.dhcp_server().free_ip(allocation.ip());
|
|
|
|
destroy(_alloc, &allocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Interface::_destroy_released_ip_allocations()
|
|
|
|
{
|
|
|
|
while (Ip_allocation *allocation = _released_ip_allocations.first()) {
|
|
|
|
_released_ip_allocations.remove(allocation);
|
|
|
|
_destroy_ip_allocation(*allocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
void Interface::_handle_eth(void *const eth_base,
|
|
|
|
size_t const eth_size,
|
|
|
|
Packet_descriptor const &pkt)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2017-10-06 13:00:05 +02:00
|
|
|
/* do garbage collection over transport-layer links and IP allocations */
|
2017-09-25 17:43:00 +02:00
|
|
|
_destroy_closed_links<Udp_link>(_closed_udp_links, _alloc);
|
|
|
|
_destroy_closed_links<Tcp_link>(_closed_tcp_links, _alloc);
|
2017-10-06 13:00:05 +02:00
|
|
|
_destroy_released_ip_allocations();
|
2017-09-25 17:43:00 +02:00
|
|
|
|
|
|
|
/* inspect and handle ethernet frame */
|
2016-08-25 17:48:53 +02:00
|
|
|
try {
|
2016-09-12 12:55:12 +02:00
|
|
|
Ethernet_frame * const eth = new (eth_base) Ethernet_frame(eth_size);
|
|
|
|
if (_config().verbose()) {
|
2017-09-25 16:54:11 +02:00
|
|
|
log("\033[33m(router <- ", _domain, ")\033[0m ", *eth); }
|
2016-09-12 12:55:12 +02:00
|
|
|
|
nic_router: DHCP client functionality
If the attribute 'interface' is not set in a 'domain' tag, the router tries to
dynamically receive and maintain an IP configuration for that domain by using
DHCP in the client role at all interfaces that connect to the domain. In the
DHCP discover phase, the router simply chooses the first DHCP offer that
arrives. So, no comparison of different DHCP offers is done. In the DHCP
request phase, the server is expected to provide an IP address, a gateway, a
subnet mask, and an IP lease time to the router. If anything substantial goes
wrong during a DHCP exchange, the router discards the outcome of the exchange
and goes back to the DHCP discover phase. At any time where there is no valid
IP configuration present at a domain, the domain does only act as DHCP client
and all other router functionality is disabled for the domain. A domain cannot
act as DHCP client and DHCP server at once. So, a 'domain' tag must either
have an 'interface' attribute or must not contain a 'dhcp-server' tag.
Ref #2534
2017-10-16 11:31:43 +02:00
|
|
|
if (_domain.ip_config().valid) {
|
|
|
|
|
|
|
|
switch (eth->type()) {
|
|
|
|
case Ethernet_frame::Type::ARP: _handle_arp(*eth, eth_size); break;
|
|
|
|
case Ethernet_frame::Type::IPV4: _handle_ip(*eth, eth_size, pkt); break;
|
|
|
|
default: throw Bad_network_protocol(); }
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
switch (eth->type()) {
|
|
|
|
case Ethernet_frame::Type::IPV4: _dhcp_client.handle_ip(*eth, eth_size); break;
|
|
|
|
default: throw Bad_network_protocol(); }
|
|
|
|
}
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
catch (Ethernet_frame::No_ethernet_frame) {
|
2016-09-12 12:55:12 +02:00
|
|
|
error("invalid ethernet frame"); }
|
|
|
|
|
|
|
|
catch (Interface::Bad_network_protocol) {
|
2017-09-29 12:08:40 +02:00
|
|
|
if (_config().verbose()) {
|
|
|
|
log("unknown network layer protocol");
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 12:55:12 +02:00
|
|
|
catch (Ipv4_packet::No_ip_packet) {
|
|
|
|
error("invalid IP packet"); }
|
|
|
|
|
|
|
|
catch (Port_allocator_guard::Out_of_indices) {
|
|
|
|
error("no available NAT ports"); }
|
2016-08-25 17:48:53 +02:00
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
catch (Domain::No_next_hop) {
|
|
|
|
error("can not find next hop"); }
|
2016-12-06 12:51:58 +01:00
|
|
|
|
|
|
|
catch (Pointer<Interface>::Invalid) {
|
|
|
|
error("no interface connected to domain"); }
|
2017-10-06 13:00:05 +02:00
|
|
|
|
|
|
|
catch (Bad_dhcp_request) {
|
|
|
|
error("bad DHCP request"); }
|
|
|
|
|
nic_router: DHCP client functionality
If the attribute 'interface' is not set in a 'domain' tag, the router tries to
dynamically receive and maintain an IP configuration for that domain by using
DHCP in the client role at all interfaces that connect to the domain. In the
DHCP discover phase, the router simply chooses the first DHCP offer that
arrives. So, no comparison of different DHCP offers is done. In the DHCP
request phase, the server is expected to provide an IP address, a gateway, a
subnet mask, and an IP lease time to the router. If anything substantial goes
wrong during a DHCP exchange, the router discards the outcome of the exchange
and goes back to the DHCP discover phase. At any time where there is no valid
IP configuration present at a domain, the domain does only act as DHCP client
and all other router functionality is disabled for the domain. A domain cannot
act as DHCP client and DHCP server at once. So, a 'domain' tag must either
have an 'interface' attribute or must not contain a 'dhcp-server' tag.
Ref #2534
2017-10-16 11:31:43 +02:00
|
|
|
catch (Alloc_dhcp_msg_buffer_failed) {
|
2017-10-06 13:00:05 +02:00
|
|
|
error("failed to allocate buffer for DHCP reply"); }
|
|
|
|
|
nic_router: DHCP client functionality
If the attribute 'interface' is not set in a 'domain' tag, the router tries to
dynamically receive and maintain an IP configuration for that domain by using
DHCP in the client role at all interfaces that connect to the domain. In the
DHCP discover phase, the router simply chooses the first DHCP offer that
arrives. So, no comparison of different DHCP offers is done. In the DHCP
request phase, the server is expected to provide an IP address, a gateway, a
subnet mask, and an IP lease time to the router. If anything substantial goes
wrong during a DHCP exchange, the router discards the outcome of the exchange
and goes back to the DHCP discover phase. At any time where there is no valid
IP configuration present at a domain, the domain does only act as DHCP client
and all other router functionality is disabled for the domain. A domain cannot
act as DHCP client and DHCP server at once. So, a 'domain' tag must either
have an 'interface' attribute or must not contain a 'dhcp-server' tag.
Ref #2534
2017-10-16 11:31:43 +02:00
|
|
|
catch (Dhcp_msg_buffer_too_small) {
|
2017-10-06 13:00:05 +02:00
|
|
|
error("DHCP reply buffer too small"); }
|
|
|
|
|
|
|
|
catch (Dhcp_server::Alloc_ip_failed) {
|
|
|
|
error("failed to allocate IP for DHCP client"); }
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
nic_router: DHCP client functionality
If the attribute 'interface' is not set in a 'domain' tag, the router tries to
dynamically receive and maintain an IP configuration for that domain by using
DHCP in the client role at all interfaces that connect to the domain. In the
DHCP discover phase, the router simply chooses the first DHCP offer that
arrives. So, no comparison of different DHCP offers is done. In the DHCP
request phase, the server is expected to provide an IP address, a gateway, a
subnet mask, and an IP lease time to the router. If anything substantial goes
wrong during a DHCP exchange, the router discards the outcome of the exchange
and goes back to the DHCP discover phase. At any time where there is no valid
IP configuration present at a domain, the domain does only act as DHCP client
and all other router functionality is disabled for the domain. A domain cannot
act as DHCP client and DHCP server at once. So, a 'domain' tag must either
have an 'interface' attribute or must not contain a 'dhcp-server' tag.
Ref #2534
2017-10-16 11:31:43 +02:00
|
|
|
void Interface::send(Ethernet_frame ð, Genode::size_t const size)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
if (_config().verbose()) {
|
2017-09-25 16:54:11 +02:00
|
|
|
log("\033[33m(", _domain, " <- router)\033[0m ", eth); }
|
2016-08-25 17:48:53 +02:00
|
|
|
try {
|
|
|
|
/* copy and submit packet */
|
2016-09-12 12:55:12 +02:00
|
|
|
Packet_descriptor const pkt = _source().alloc_packet(size);
|
|
|
|
char *content = _source().packet_content(pkt);
|
|
|
|
Genode::memcpy((void *)content, (void *)ð, size);
|
|
|
|
_source().submit_packet(pkt);
|
|
|
|
}
|
|
|
|
catch (Packet_stream_source::Packet_alloc_failed) {
|
|
|
|
if (_config().verbose()) {
|
2016-08-25 17:48:53 +02:00
|
|
|
log("Failed to allocate packet"); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
Interface::Interface(Entrypoint &ep,
|
os/timer: interpolate time via timestamps
Previously, the Genode::Timer::curr_time always used the
Timer_session::elapsed_ms RPC as back end. Now, Genode::Timer reads
this remote time only in a periodic fashion independently from the calls
to Genode::Timer::curr_time. If now one calls Genode::Timer::curr_time,
the function takes the last read remote time value and adapts it using
the timestamp difference since the remote-time read. The conversion
factor from timestamps to time is estimated on every remote-time read
using the last read remote-time value and the timestamp difference since
the last remote time read.
This commit also re-works the timeout test. The test now has two stages.
In the first stage, it tests fast polling of the
Genode::Timer::curr_time. This stage checks the error between locally
interpolated and timer-driver time as well as wether the locally
interpolated time is monotone and sufficiently homogeneous. In the
second stage several periodic and one-shot timeouts are scheduled at
once. This stage checks if the timeouts trigger sufficiently precise.
This commit adds the new Kernel::time syscall to base-hw. The syscall is
solely used by the Genode::Timer on base-hw as substitute for the
timestamp. This is because on ARM, the timestamp function uses the ARM
performance counter that stops counting when the WFI (wait for
interrupt) instruction is active. This instruction, however is used by
the base-hw idle contexts that get active when no user thread needs to
be scheduled. Thus, the ARM performance counter is not a good choice for
time interpolation and we use the kernel internal time instead.
With this commit, the timeout library becomes a basic library. That means
that it is linked against the LDSO which then provides it to the program it
serves. Furthermore, you can't use the timeout library anymore without the
LDSO because through the kernel-dependent LDSO make-files we can achieve a
kernel-dependent timeout implementation.
This commit introduces a structured Duration type that shall successively
replace the use of Microseconds, Milliseconds, and integer types for duration
values.
Open issues:
* The timeout test fails on Raspberry PI because of precision errors in the
first stage. However, this does not render the framework unusable in general
on the RPI but merely is an issue when speaking of microseconds precision.
* If we run on ARM with another Kernel than HW the timestamp speed may
continuously vary from almost 0 up to CPU speed. The Timer, however,
only uses interpolation if the timestamp speed remained stable (12.5%
tolerance) for at least 3 observation periods. Currently, one period is
100ms, so its 300ms. As long as this is not the case,
Timer_session::elapsed_ms is called instead.
Anyway, it might happen that the CPU load was stable for some time so
interpolation becomes active and now the timestamp speed drops. In the
worst case, we would now have 100ms of slowed down time. The bad thing
about it would be, that this also affects the timeout of the period.
Thus, it might "freeze" the local time for more than 100ms.
On the other hand, if the timestamp speed suddenly raises after some
stable time, interpolated time can get too fast. This would shorten the
period but nonetheless may result in drifting away into the far future.
Now we would have the problem that we can't deliver the real time
anymore until it has caught up because the output of Timer::curr_time
shall be monotone. So, effectively local time might "freeze" again for
more than 100ms.
It would be a solution to not use the Trace::timestamp on ARM w/o HW but
a function whose return value causes the Timer to never use
interpolation because of its stability policy.
Fixes #2400
2017-04-22 00:52:23 +02:00
|
|
|
Timer::Connection &timer,
|
2016-09-12 12:55:12 +02:00
|
|
|
Mac_address const router_mac,
|
|
|
|
Genode::Allocator &alloc,
|
|
|
|
Mac_address const mac,
|
|
|
|
Domain &domain)
|
2016-08-25 17:48:53 +02:00
|
|
|
:
|
|
|
|
_sink_ack(ep, *this, &Interface::_ack_avail),
|
|
|
|
_sink_submit(ep, *this, &Interface::_ready_to_submit),
|
|
|
|
_source_ack(ep, *this, &Interface::_ready_to_ack),
|
2016-09-12 12:55:12 +02:00
|
|
|
_source_submit(ep, *this, &Interface::_packet_avail),
|
|
|
|
_router_mac(router_mac), _mac(mac), _timer(timer), _alloc(alloc),
|
|
|
|
_domain(domain)
|
|
|
|
{
|
|
|
|
if (_config().verbose()) {
|
|
|
|
log("Interface connected ", *this);
|
2016-08-25 17:48:53 +02:00
|
|
|
log(" MAC ", _mac);
|
2016-09-12 12:55:12 +02:00
|
|
|
log(" Router identity: MAC ", _router_mac, " IP ",
|
2017-10-13 16:22:05 +02:00
|
|
|
_router_ip(), "/", _ip_config().interface.prefix);
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
2016-09-12 12:55:12 +02:00
|
|
|
_domain.interface().set(*this);
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
nic_router: DHCP client functionality
If the attribute 'interface' is not set in a 'domain' tag, the router tries to
dynamically receive and maintain an IP configuration for that domain by using
DHCP in the client role at all interfaces that connect to the domain. In the
DHCP discover phase, the router simply chooses the first DHCP offer that
arrives. So, no comparison of different DHCP offers is done. In the DHCP
request phase, the server is expected to provide an IP address, a gateway, a
subnet mask, and an IP lease time to the router. If anything substantial goes
wrong during a DHCP exchange, the router discards the outcome of the exchange
and goes back to the DHCP discover phase. At any time where there is no valid
IP configuration present at a domain, the domain does only act as DHCP client
and all other router functionality is disabled for the domain. A domain cannot
act as DHCP client and DHCP server at once. So, a 'domain' tag must either
have an 'interface' attribute or must not contain a 'dhcp-server' tag.
Ref #2534
2017-10-16 11:31:43 +02:00
|
|
|
void Interface::_init()
|
|
|
|
{
|
|
|
|
if (!_domain.ip_config().valid) {
|
|
|
|
_dhcp_client.discover();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
void Interface::_ack_packet(Packet_descriptor const &pkt)
|
2016-08-25 17:48:53 +02:00
|
|
|
{
|
2016-09-12 12:55:12 +02:00
|
|
|
if (!_sink().ready_to_ack()) {
|
|
|
|
error("ack state FULL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_sink().acknowledge_packet(pkt);
|
|
|
|
}
|
2016-08-25 17:48:53 +02:00
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
void Interface::_cancel_arp_waiting(Arp_waiter &waiter)
|
|
|
|
{
|
|
|
|
warning("waiting for ARP cancelled");
|
|
|
|
_ack_packet(waiter.packet());
|
|
|
|
destroy(_alloc, &waiter);
|
|
|
|
}
|
2016-08-25 17:48:53 +02:00
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
Interface::~Interface()
|
|
|
|
{
|
|
|
|
_domain.interface().unset();
|
|
|
|
if (_config().verbose()) {
|
|
|
|
log("Interface disconnected ", *this); }
|
2016-08-25 17:48:53 +02:00
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
/* destroy ARP waiters */
|
|
|
|
while (_own_arp_waiters.first()) {
|
|
|
|
_cancel_arp_waiting(*_foreign_arp_waiters.first()->object()); }
|
2016-08-25 17:48:53 +02:00
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
while (_foreign_arp_waiters.first()) {
|
|
|
|
Arp_waiter &waiter = *_foreign_arp_waiters.first()->object();
|
|
|
|
waiter.src()._cancel_arp_waiting(waiter); }
|
2016-08-25 17:48:53 +02:00
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
/* destroy links */
|
|
|
|
_destroy_links<Tcp_link>(_tcp_links, _closed_tcp_links, _alloc);
|
|
|
|
_destroy_links<Udp_link>(_udp_links, _closed_udp_links, _alloc);
|
2017-10-06 13:00:05 +02:00
|
|
|
|
|
|
|
/* destroy IP allocations */
|
|
|
|
_destroy_released_ip_allocations();
|
|
|
|
while (Ip_allocation *allocation = _ip_allocations.first()) {
|
|
|
|
_ip_allocations.remove(allocation);
|
|
|
|
_destroy_ip_allocation(*allocation);
|
|
|
|
}
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
Configuration &Interface::_config() const { return _domain.config(); }
|
2016-08-25 17:48:53 +02:00
|
|
|
|
|
|
|
|
2017-10-13 16:22:05 +02:00
|
|
|
Ipv4_config const &Interface::_ip_config() const { return _domain.ip_config(); }
|
|
|
|
|
|
|
|
|
2016-09-12 12:55:12 +02:00
|
|
|
void Interface::print(Output &output) const
|
|
|
|
{
|
|
|
|
Genode::print(output, "\"", _domain.name(), "\"");
|
2016-08-25 17:48:53 +02:00
|
|
|
}
|
2017-10-06 13:00:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*******************
|
|
|
|
** Ip_allocation **
|
|
|
|
*******************/
|
|
|
|
|
|
|
|
Ip_allocation::Ip_allocation(Interface &interface,
|
|
|
|
Configuration &config,
|
|
|
|
Ipv4_address const &ip,
|
|
|
|
Mac_address const &mac,
|
|
|
|
Timer::Connection &timer,
|
|
|
|
Microseconds lifetime)
|
|
|
|
:
|
|
|
|
_interface(interface),
|
|
|
|
_config(config),
|
|
|
|
_ip(ip),
|
|
|
|
_mac(mac),
|
|
|
|
_release_timeout(timer, *this, &Ip_allocation::_handle_release_timeout)
|
|
|
|
{
|
|
|
|
_release_timeout.schedule(lifetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Ip_allocation::lifetime(Microseconds lifetime)
|
|
|
|
{
|
|
|
|
_release_timeout.schedule(lifetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Ip_allocation::_higher(Mac_address const &mac) const
|
|
|
|
{
|
|
|
|
return memcmp(mac.addr, _mac.addr, sizeof(_mac.addr)) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ip_allocation &Ip_allocation::find_by_mac(Mac_address const &mac)
|
|
|
|
{
|
|
|
|
if (mac == _mac) {
|
|
|
|
return *this; }
|
|
|
|
|
|
|
|
Ip_allocation *const allocation = child(_higher(mac));
|
|
|
|
if (!allocation) {
|
|
|
|
throw Ip_allocation_tree::No_match(); }
|
|
|
|
|
|
|
|
return allocation->find_by_mac(mac);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Ip_allocation::print(Output &output) const
|
|
|
|
{
|
|
|
|
Genode::print(output, "MAC ", _mac, " IP ", _ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Ip_allocation::_handle_release_timeout(Duration)
|
|
|
|
{
|
|
|
|
_interface.ip_allocation_expired(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/************************
|
|
|
|
** Ip_allocation_tree **
|
|
|
|
************************/
|
|
|
|
|
|
|
|
Ip_allocation &
|
|
|
|
Ip_allocation_tree::find_by_mac(Mac_address const &mac) const
|
|
|
|
{
|
|
|
|
if (!first()) {
|
|
|
|
throw No_match(); }
|
|
|
|
|
|
|
|
return first()->find_by_mac(mac);
|
|
|
|
}
|