SatCat5
ip_table.h
Go to the documentation of this file.
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
41 
42 #pragma once
43 
44 #include <satcat5/eth_header.h>
45 #include <satcat5/ip_core.h>
46 
47 // Set size of the static routing table, not including the default route.
48 // (To override, add gcc argument "-DSATCAT5_ROUTING_TABLE=32" etc.)
49 #ifndef SATCAT5_ROUTING_TABLE
50 #define SATCAT5_ROUTING_TABLE 8
51 #endif
52 
53 namespace satcat5 {
54  namespace ip {
57  struct Route {
58  // Contents of this table entry:
62  u8 port;
63  u8 flags;
64 
69  static constexpr u8 FLAG_PROXY_ARP = 0x01;
70 
74  static constexpr u8 FLAG_MAC_FIXED = 0x02;
75 
76  // Explicitly declare default constructor and copy methods.
77  Route() = default;
78  Route(const Route& t) = default;
79  Route& operator=(const Route& t) = default;
80 
82  inline bool has_dstmac() const
83  { return (dstmac != satcat5::eth::MACADDR_NONE); }
85  inline bool has_gateway() const
86  { return (gateway != satcat5::ip::ADDR_NONE); }
88  inline bool is_deliverable() const
89  { return has_dstmac() || has_gateway(); }
91  inline bool is_multicast() const
92  { return dstmac.is_multicast() || gateway.is_multicast(); }
94  inline bool is_proxy_arp() const
95  { return (flags & FLAG_PROXY_ARP) != 0; }
97  inline bool is_unicast() const
98  { return dstmac.is_unicast() || gateway.is_unicast(); }
99 
101  void log_to(satcat5::log::LogBuffer& wr) const;
102  };
103 
111  class RouteArray {
112  protected:
114  RouteArray();
115 
119  inline const satcat5::ip::Route& route_rddef() const
120  { return m_route_default; }
121  virtual bool route_wrdef(const satcat5::ip::Route& route);
123 
127  inline const satcat5::ip::Route& route_read(unsigned idx) const
128  { return m_route_table[idx]; }
129  virtual bool route_write(unsigned idx, const satcat5::ip::Route& route);
131 
132  private:
133  // Members are private to prevent accidental changes.
134  satcat5::ip::Route m_route_default;
135  satcat5::ip::Route m_route_table[SATCAT5_ROUTING_TABLE];
136  };
137 
144  class Table : protected satcat5::ip::RouteArray {
145  public:
148  Table();
149 
154  void log_to(satcat5::log::LogBuffer& wr) const;
155 
159  void route_clear(bool lockdown = true);
160 
167  void route_flush();
168 
170  bool route_default(
171  const satcat5::ip::Addr& gateway,
172  const satcat5::eth::MacAddr& dstmac = satcat5::eth::MACADDR_NONE,
173  u8 port = 0, u8 flags = 0);
174 
178  bool route_simple(
179  const satcat5::ip::Addr& gateway,
180  const satcat5::ip::Mask& subnet = satcat5::ip::MASK_24);
181 
184  bool route_static(
185  const satcat5::ip::Subnet& subnet,
186  const satcat5::ip::Addr& gateway,
187  const satcat5::eth::MacAddr& dstmac = satcat5::eth::MACADDR_NONE,
188  u8 port = 0, u8 flags = 0);
189 
193  const satcat5::ip::Subnet& subnet,
194  u8 port = 0, u8 flags = 0)
195  {
196  return route_static(subnet,
197  satcat5::ip::ADDR_BROADCAST,
198  satcat5::eth::MACADDR_NONE,
199  port, flags);
200  }
201 
206  bool route_cache(
207  const satcat5::ip::Addr& gateway,
208  const satcat5::eth::MacAddr& dstmac);
209 
212  bool route_remove(const satcat5::ip::Subnet& subnet);
213  inline bool route_remove(const satcat5::ip::Addr& addr)
214  { return route_remove({addr, ip::MASK_32}); }
215 
218  const satcat5::ip::Addr& dstaddr) const;
219 
220  protected:
221  // Routing table state (in addition to RouteArray):
222  // First N routes are static; the rest are ephemeral.
223  unsigned m_wridx_static;
224  unsigned m_wridx_ephemeral;
225  };
226  }
227 }
An array of routes with read and write accessors.
Definition: ip_table.h:111
RouteArray()
Create an empty table.
Definition: ip_table.cc:222
virtual bool route_wrdef(const satcat5::ip::Route &route)
Internal methods used to access "m_route_default".
Definition: ip_table.cc:229
virtual bool route_write(unsigned idx, const satcat5::ip::Route &route)
Internal methods used to access "m_route_table".
Definition: ip_table.cc:234
const satcat5::ip::Route & route_rddef() const
Internal methods used to access "m_route_default".
Definition: ip_table.h:119
const satcat5::ip::Route & route_read(unsigned idx) const
Internal methods used to access "m_route_table".
Definition: ip_table.h:127
IPv4 forwarding table.
Definition: ip_table.h:144
Table()
Construct an empty table with a local default route.
Definition: ip_table.cc:52
bool route_default(const satcat5::ip::Addr &gateway, const satcat5::eth::MacAddr &dstmac=satcat5::eth::MACADDR_NONE, u8 port=0, u8 flags=0)
Set the default behavior when no other routes match.
Definition: ip_table.cc:97
satcat5::ip::Route route_lookup(const satcat5::ip::Addr &dstaddr) const
Next-hop routing lookup for the given destination address.
Definition: ip_table.cc:199
void route_clear(bool lockdown=true)
Clear all routes, including the default.
Definition: ip_table.cc:73
bool route_static(const satcat5::ip::Subnet &subnet, const satcat5::ip::Addr &gateway, const satcat5::eth::MacAddr &dstmac=satcat5::eth::MACADDR_NONE, u8 port=0, u8 flags=0)
Create or update a single static route.
Definition: ip_table.cc:111
void log_to(satcat5::log::LogBuffer &wr) const
Create a log entry with the full contents of this table.
Definition: ip_table.cc:59
bool route_local(const satcat5::ip::Subnet &subnet, u8 port=0, u8 flags=0)
Create or update a local static route.
Definition: ip_table.h:192
bool route_cache(const satcat5::ip::Addr &gateway, const satcat5::eth::MacAddr &dstmac)
Update matching MAC address cache entries.
Definition: ip_table.cc:138
bool route_remove(const satcat5::ip::Subnet &subnet)
Remove a single static route.
Definition: ip_table.cc:178
bool route_simple(const satcat5::ip::Addr &gateway, const satcat5::ip::Mask &subnet=satcat5::ip::MASK_24)
Simplified one-step setup for a typical home network.
Definition: ip_table.cc:105
void route_flush()
Flush cached MAC-addresses.
Definition: ip_table.cc:81
Internal buffer used by the Log class.
Definition: log.h:134
Type definitions for Ethernet frames and protocol handlers.
An Ethernet MAC address (with serializable interface).
Definition: eth_header.h:29
bool is_multicast() const
Broadcast, L2 multicast, or L3 multicast.
Definition: eth_header.cc:57
bool is_unicast() const
Any normal single-destination address.
Definition: eth_header.cc:74
IPv4 address is a 32-bit unsigned integer.
Definition: ip_core.h:15
bool is_multicast() const
IP multicast (224.*.*.*)
Definition: ip_core.cc:41
bool is_unicast() const
Any normal single-destination address.
Definition: ip_core.cc:59
IPv4 subnet masks share functionality with a basic address, but are constructed differently to match ...
Definition: ip_core.h:66
A single entry in the static routing table.
Definition: ip_table.h:57
bool is_multicast() const
Is the next-hop address a multicast address?
Definition: ip_table.h:91
satcat5::eth::MacAddr dstmac
Next-hop MAC address.
Definition: ip_table.h:61
bool has_gateway() const
Does this route have a known next-hop IPv4 address?
Definition: ip_table.h:85
u8 flags
Additional flags.
Definition: ip_table.h:63
void log_to(satcat5::log::LogBuffer &wr) const
Format a one-line log entry containing all route parameters.
Definition: ip_table.cc:30
static constexpr u8 FLAG_MAC_FIXED
Fixed MAC-address for this route? If this flag is set, then the user specified the MAC address.
Definition: ip_table.h:74
bool has_dstmac() const
Does this route have a known next-hop MAC address?
Definition: ip_table.h:82
static constexpr u8 FLAG_PROXY_ARP
Enable proxy-ARP for this route? If proxy-ARP is enabled, then the router should respond to ARP queri...
Definition: ip_table.h:69
bool is_deliverable() const
Does this route have a valid next-hop address of any kind?
Definition: ip_table.h:88
satcat5::ip::Subnet subnet
Subnet address + mask.
Definition: ip_table.h:59
u8 port
Next-hop port number.
Definition: ip_table.h:62
bool is_proxy_arp() const
Is proxy-ARP enabled for this route?
Definition: ip_table.h:94
bool is_unicast() const
Is the next-hop address an ordinary unicast address?
Definition: ip_table.h:97
satcat5::ip::Addr gateway
Next-hop IPv4 address.
Definition: ip_table.h:60
An IPv4 subnet consists of a base address and a subnet mask.
Definition: ip_core.h:87