Vanetza
 
Loading...
Searching...
No Matches
router.hpp
1#ifndef ROUTER_HPP_UKYYCAR0
2#define ROUTER_HPP_UKYYCAR0
3
4#include <vanetza/common/byte_order.hpp>
5#include <vanetza/common/hook.hpp>
6#include <vanetza/common/its_aid.hpp>
7#include <vanetza/access/ethertype.hpp>
8#include <vanetza/geonet/beacon_header.hpp>
9#include <vanetza/geonet/cbf_packet_buffer.hpp>
10#include <vanetza/geonet/common_header.hpp>
11#include <vanetza/geonet/extended_pdu.hpp>
12#include <vanetza/geonet/gbc_header.hpp>
13#include <vanetza/geonet/gbc_memory.hpp>
14#include <vanetza/geonet/interface.hpp>
15#include <vanetza/geonet/location_table.hpp>
16#include <vanetza/geonet/mib.hpp>
17#include <vanetza/geonet/packet.hpp>
18#include <vanetza/geonet/packet_buffer.hpp>
19#include <vanetza/geonet/pending_packet.hpp>
20#include <vanetza/geonet/pdu.hpp>
21#include <vanetza/geonet/pdu_variant.hpp>
22#include <vanetza/geonet/repeater.hpp>
23#include <vanetza/geonet/sequence_number.hpp>
24#include <vanetza/geonet/shb_header.hpp>
25#include <vanetza/geonet/timestamp.hpp>
26#include <vanetza/units/length.hpp>
27#include <vanetza/units/time.hpp>
28#include <vanetza/security/security_entity.hpp>
29#include <boost/variant.hpp>
30#include <cstdint>
31#include <memory>
32#include <random>
33#include <map>
34
35namespace vanetza
36{
37
38// forward declarations
39class MacAddress;
40struct PositionFix;
41class Runtime;
42
43namespace dcc
44{
45 struct DataRequest;
46 class RequestInterface;
47} // namespace dcc
48
49namespace geonet
50{
51
52extern const access::EtherType ether_type;
53
54class DccFieldGenerator;
55class IndicationContext;
56class IndicationContextBasic;
57class NextHop;
58class TransportInterface;
59struct ShbDataRequest;
60struct GbcDataRequest;
61struct DataConfirm;
62struct DataIndication;
63struct LinkLayer;
64
65/**
66 * Router is the central entity for GeoNet communication
67 *
68 * Incoming and outgoing GeoNet packets are handled by the router.
69 * It may even dispatch own packets (beacons) if necessary.
70 *
71 * This implementation follows EN 302 636-4-1 v1.3.1
72 */
73class Router
74{
75public:
76 typedef std::unique_ptr<DataRequest> DataRequestPtr;
77 typedef std::unique_ptr<Pdu> PduPtr;
78 typedef std::unique_ptr<DownPacket> DownPacketPtr;
79 typedef std::unique_ptr<UpPacket> UpPacketPtr;
80
82
83 /// Reason for packet drop used by drop hook
85 {
86 Parse_Basic_Header,
87 Parse_Common_Header,
88 Parse_Secured_Header,
89 Parse_Extended_Header,
90 ITS_Protocol_Version,
91 Decap_Unsuccessful_Non_Strict,
92 Decap_Unsuccessful_Strict,
93 Hop_Limit,
94 Payload_Size,
95 Security_Entity_Missing
96 };
97
98 // Reason for stopping packet forwarding
99 enum class ForwardingStopReason
100 {
101 Hop_Limit,
102 Source_PDR,
103 Sender_PDR,
104 Outside_Destination_Area
105 };
106
107 Router(Runtime&, const MIB&);
108 ~Router();
109
110 /**
111 * \brief Request to send payload per single hop broadcast (SHB).
112 * If security is enabled, the message gets encapsulated in a security envelope.
113 * Returns whether data was valid to be sent.
114 *
115 * \param request
116 * \param payload from upper layers
117 * \return result code if packet has been accepted
118 */
119 DataConfirm request(const ShbDataRequest&, DownPacketPtr);
120
121 /**
122 * \brief Request to send payload per GeoBroadcast (GBC).
123 * If security is enabled, the message gets encapsulated in a security envelope.
124 * Returns whether data was valid to be sent.
125 *
126 * \param request
127 * \param payload from upper layers
128 * \return result code if packet has been accepted
129 */
130 DataConfirm request(const GbcDataRequest&, DownPacketPtr);
131
132 // These three requests are not supported yet
133 DataConfirm request(const GucDataRequest&, DownPacketPtr);
134 DataConfirm request(const GacDataRequest&, DownPacketPtr);
135 DataConfirm request(const TsbDataRequest&, DownPacketPtr);
136
137 /**
138 * \brief Handle the received packet on network layer.
139 * Packet handling involves these steps:
140 * - header processing
141 * - packet forwarding
142 * - passing to transport layer
143 * - security decapsulation
144 *
145 * \param packet received packet from access layer
146 * \param sender MAC address of sender
147 * \param destination MAC address of destination (might be broadcast)
148 */
149 void indicate(UpPacketPtr, const MacAddress& sender, const MacAddress& destination);
150
151 /**
152 * \brief When a packet is dropped, this Hook is invoked
153 * \tparam PacketDropReason why Router decided to drop packet
154 */
156
157 /**
158 * \brief When packet forwarding is stopped, this Hook is invoked
159 * \tparam ForwardingStopReason why Router decided not to forward packet
160 */
162
163 /**
164 * \brief Update router's local position vector
165 *
166 * \param fix current position fix
167 */
168 void update_position(const PositionFix&);
169
170 /**
171 * \brief Register a transport protocol handler.
172 *
173 * \param proto register handler for this upper protocol
174 * \param ifc use this interface or disable handling if nullptr
175 */
176 void set_transport_handler(UpperProtocol proto, TransportInterface* ifc);
177
178 /**
179 * \brief Register security entity used when itsGnSecurity is enabled
180 *
181 * \param entity security entity
182 */
184
185 /**
186 * \brief Register access layer interface
187 *
188 * \param ifc interface used for passing packets down to access layer
189 */
191
192 /**
193 * \brief Register generator for DCC-MCO fields
194 *
195 * \param dcc DCC-MCO field generator or nullptr for disabling feature
196 */
198
199 /**
200 * \brief Set Router's own GeoNetworking address
201 *
202 * \param addr
203 */
204 void set_address(const Address&);
205
206 /**
207 * \brief Get Management Information Base (MIB)
208 * \return read-only reference to MIB
209 */
210 const MIB& get_mib() const { return m_mib; }
211
212 /**
213 * \brief Get the Contention-Based-Forwarding buffer
214 *
215 * \return read-only reference to CBF packet buffer
216 */
217 const CbfPacketBuffer& get_cbf_buffer() const { return m_cbf_buffer; }
218
219 /**
220 * \brief Get the LocationTable.
221 * The table holds information about neighbouring ITS-Routers.
222 *
223 * \return read-only reference to LocationTable
224 */
225 const LocationTable& get_location_table() const { return m_location_table; }
226
227 /**
228 * \brief Get the local position vector.
229 * This vector describes the current position of the router.
230 *
231 * \return read-only reference to LongPositionVector
232 */
233 const LongPositionVector& get_local_position_vector() const { return m_local_position_vector; }
234
235 /**
236 * \brief Check if router is outside the sectorial contention area
237 * See TS 102 636-4-1 v1.2.3 section E.4 and figure E.2 for details.
238 *
239 * \param sender
240 * \param forwarder
241 * \return bool true if either sender or forwarder is outside
242 */
243 bool outside_sectorial_contention_area(const MacAddress& sender, const MacAddress& forwarder) const;
244
245 /**
246 * \brief Set seed for internal random number generator (RNG)
247 * RNG is used e.g. for random Beacon jitter
248 *
249 * \param seed reset RNG's state to this seed
250 */
251 void set_random_seed(std::uint_fast32_t seed);
252
253 /**
254 * Forwarding algorithm selection procedure as given by Annex D
255 * \param pdu GeoNetworking PDU
256 * \param payload packet payload
257 * \param ll link-layer control info (unavailable for source operations)
258 * \return routing decision (next hop's address, buffered, or discarded)
259 */
260 NextHop forwarding_algorithm_selection(PendingPacketForwarding&&, const LinkLayer* ll = nullptr);
261
262private:
263 typedef std::map<UpperProtocol, TransportInterface*> transport_map_t;
264
265 /**
266 * \brief Send Beacon packet to all neighbours with updated position vector.
267 * Only to be called when the beacon timer expires.
268 */
270
271 /**
272 * \brief Reschedule timer for next Beacon transmission
273 * Timer will be scheduled according to MIB's Beacon timer settings.
274 */
275 void reset_beacon_timer();
276
277 /**
278 * \brief Reschedule timer for next Beacon transmission
279 * \param next Duration until next transmission
280 */
281 void reset_beacon_timer(Clock::duration next);
282
283 /**
284 * \brief Process BasicHeader at packet indication.
285 * \param ctx Context holding data for further parsing
286 */
288
289 /**
290 * \brief Process CommonHeader at packet indication.
291 * \param ctx Context holding data for further parsing
292 * \param basic Previously decoded BasicHeader
293 */
295
296 /**
297 * \brief Process ExtendedHeader at packet indication.
298 * \param ctx Context holding data for further parsing
299 * \param common Previously decoded CommonHeader
300 */
302
303 /**
304 * \brief Process SecuredMessage at packet indication.
305 * \param ctx Context holding data for further parsing
306 * \param basic Previously decoded BasicHeader
307 */
309
310 /**
311 * \brief Process ExtendedHeader information.
312 * Update router's LocationTable and neighbour relationship.
313 *
314 * \param pdu containing the ExtendedHeader
315 * \param packet received packet
316 * \param ll link-layer control info
317 * \return pass up decision (always false for Beacons)
318 */
319 bool process_extended(const ExtendedPduConstRefs<BeaconHeader>&, const UpPacket&, const LinkLayer& ll);
320
321 /**
322 * \brief Process ExtendedHeader information.
323 * Update router's LocationTable and neighbour relationship.
324 * Pass packet up to transport layer for further processing.
325 *
326 * \param pdu containing the ExtendedHeader
327 * \param packet received packet
328 * \param ll link-layer control info
329 * \return pass up decision (true for all non-duplicate SHBs)
330 */
331 bool process_extended(const ExtendedPduConstRefs<ShbHeader>&, const UpPacket&, const LinkLayer& ll);
332
333 /**
334 * \brief Process ExtendedHeader information.
335 * Update router's LocationTable and neighbour relationship.
336 * Pass packet up to transport layer for further processing.
337 * Forward packets.
338 *
339 * \param pdu containing the ExtendedHeader
340 * \param packet received packet
341 * \param ll link-layer control info
342 * \return pass up decision (depends on addressed area and router position)
343 */
344 bool process_extended(const ExtendedPduConstRefs<GeoBroadcastHeader>&, const UpPacket&, const LinkLayer& ll);
345
346 /**
347 * \brief Send all packets in the broadcast forwarding buffer with expired waiting time.
348 */
350
351 /**
352 * \brief Send all matching packets in the unicast forwarding buffer with expired waiting time.
353 * \param addr unicast packets for this address
354 */
356
357 /**
358 * \brief Executes media specific functionalities
359 * Details are described in TS 102 636-4-2.
360 *
361 * \param profile e.g. ITS-G5
362 */
363 void execute_media_procedures(CommunicationProfile);
364
365 /**
366 * \brief Executes ITS-G5 media specific procedures
367 * Details are described in TS 102 636-4-2.
368 */
370
371 /**
372 * \brief Pass down the packet to the access layer.
373 *
374 * \param addr MAC address of destination
375 * \param pdu header information
376 * \param payload Packet payload
377 */
378 void pass_down(const MacAddress&, PduPtr, DownPacketPtr);
379
380 /**
381 * \brief Send packet using the information in the DataRequest.
382 * The packet is formed using the data in PDU and payload.
383 *
384 * \param request containing transmission parameters
385 * \param pdu header information
386 * \param payload Packet payload
387 */
388 void pass_down(const dcc::DataRequest&, PduPtr, DownPacketPtr);
389
390 /**
391 * \brief Pass packet up to the transport layer.
392 *
393 * \param ind containing network information
394 * \param packet payload to be passed up to the next layer
395 */
396 void pass_up(const DataIndication&, UpPacketPtr);
397
398 /**
399 * \brief Decide if GBC packet shall be passed up to transport layer.
400 *
401 * \param within_destination is router located within destination area
402 * \param gbc GeoBroadcast header
403 *
404 * \return true if packet shall be passed up
405 */
406 bool decide_pass_up(bool within_destination, const GeoBroadcastHeader& gbc);
407
408 /**
409 * \brief Helper method to handle duplicate addresses.
410 * If own address collides with the address of a received packet
411 * Router's address is set to a new random address.
412 * \note Behaviour depends on MIB's itsGnLocalAddrConfMethod.
413 *
414 * \param source address of source (from packet header)
415 * \param sender address of sender (link layer)
416 */
417 void detect_duplicate_address(const Address& source, const MacAddress& sender);
418
419 /**
420 * \brief Detect duplicate packets
421 * See EN 302 636-4-1 v1.3.1 Annex A.2
422 *
423 * \param source source address
424 * \param sn sequence number
425 * \return true if packet is detected as a duplicate
426 */
427 bool detect_duplicate_packet(const Address& source, SequenceNumber sn);
428
429 /**
430 * \brief Determine next hop for greedy forwarding.
431 * See EN 302 636-4-1 v1.3.1 Annex E.2
432 *
433 * \param pdu
434 * \param payload
435 * \return next hop
436 */
437 NextHop greedy_forwarding(PendingPacketForwarding&&);
438
439 /**
440 * \brief Determine next hop for non-area contention-based forwarding
441 * See EN 302 636-4-1 v1.3.1 Annex E.3
442 *
443 * \param pdu
444 * \param payload
445 * \param sender optional sender MAC address (if not first hop)
446 * \return next hop
447 */
448 NextHop non_area_contention_based_forwarding(PendingPacketForwarding&&, const MacAddress* sender);
449
450 /**
451 * \brief Determine next hop for area contention-based forwarding
452 * See EN 302 636-4-1 v1.3.1 Annex F.3
453 *
454 * \param pdu
455 * \param payload
456 * \param sender optional sender MAC address (if not first hop)
457 * \return next hop
458 */
459 NextHop area_contention_based_forwarding(PendingPacketForwarding&&, const MacAddress* sender);
460
461 /**
462 * \brief Determine CBF buffering time for a packet.
463 * Complies to EN 302 636-4-1 v1.3.1 Annex E.3 (non-area CBF, eq. E.1) and F.3 (area CBF, eq. F.1)
464 *
465 * \param dist distance or progress (interpretation depends on non-area vs. area CBF)
466 * \return CBF time-out
467 */
468 units::Duration timeout_cbf(units::Length distance) const;
469
470 /**
471 * \brief Determine (area) CBF buffering time for a packet from a sender
472 *
473 * This is a shortcut for a re-curring pattern in Annex F.3 and F.4:
474 * 1) sender position is looked up in location table
475 * 2) position accuracy of sender is validated (if it is found)
476 * 3) progress is then distance between sender and local router
477 *
478 * \param sender MAC address of sender
479 * \return CBF time-out
480 */
481 units::Duration timeout_cbf(const MacAddress& sender) const;
482
483 /**
484 * \brief Determine next hop for area advanced forwarding
485 * See EN 302 636-4-1 v1.3.1 Annex F.4
486 *
487 * \param pdu
488 * \param payload
489 * \param ll optional link-layer control info (if not source operations)
490 * \return next hop
491 */
492 NextHop area_advanced_forwarding(PendingPacketForwarding&&, const LinkLayer* sender);
493
494 /**
495 * \brief Callback function for dispatching a packet repetition.
496 * Invoked by Repeater when a scheduled repetition is due.
497 *
498 * \param request
499 * \param payload
500 */
501 void dispatch_repetition(const DataRequestVariant&, DownPacketPtr);
502
503 /**
504 * \brief Encaspulate a packet according to security profile
505 *
506 * \param aid ITS-AID
507 * \param pdu PDU
508 * \param packet Packet with payload
509 */
510 DownPacketPtr encap_packet(ItsAid aid, Pdu& pdu, DownPacketPtr packet);
511
512 /**
513 * \brief Create an initialized Single-Hop-Broadcast PDU
514 *
515 * \param request
516 * \return PDU object
517 */
518 std::unique_ptr<ShbPdu> create_shb_pdu(const ShbDataRequest&);
519
520 /**
521 * \brief Create an initialzed Beacon PDU
522 *
523 * \return PDU object
524 */
525 std::unique_ptr<BeaconPdu> create_beacon_pdu();
526
527 /**
528 * \brief Create an initialized GeoBroadcast PDU
529 *
530 * \param request
531 * \return PDU object
532 */
533 std::unique_ptr<GbcPdu> create_gbc_pdu(const GbcDataRequest&);
534
535 const MIB& m_mib;
536 Runtime& m_runtime;
537 dcc::RequestInterface* m_request_interface;
538 DccFieldGenerator* m_dcc_field_generator;
539 security::SecurityEntity* m_security_entity;
540 transport_map_t m_transport_ifcs;
541 LocationTable m_location_table;
542 PacketBuffer m_bc_forward_buffer;
543 PacketBuffer m_uc_forward_buffer;
544 CbfPacketBuffer m_cbf_buffer;
545 LongPositionVector m_local_position_vector;
546 SequenceNumber m_local_sequence_number;
547 Repeater m_repeater;
548 std::mt19937 m_random_gen;
549 GbcMemory m_gbc_memory;
550};
551
552/**
553 * Get string representation of packet drop reason
554 * \param pdr packet drop reason code
555 * \return string representation
556 */
557std::string stringify(Router::PacketDropReason pdr);
558
559} // namespace geonet
560} // namespace vanetza
561
562#endif /* ROUTER_HPP_UKYYCAR0 */
const CbfPacketBuffer & get_cbf_buffer() const
Get the Contention-Based-Forwarding buffer.
Definition: router.hpp:217
NextHop area_advanced_forwarding(PendingPacketForwarding &&, const LinkLayer *sender)
Determine next hop for area advanced forwarding See EN 302 636-4-1 v1.3.1 Annex F....
Definition: router.cpp:892
void flush_broadcast_forwarding_buffer()
Send all packets in the broadcast forwarding buffer with expired waiting time.
Definition: router.cpp:1171
void indicate_extended(IndicationContext &, const CommonHeader &)
Process ExtendedHeader at packet indication.
Definition: router.cpp:543
const MIB & get_mib() const
Get Management Information Base (MIB)
Definition: router.hpp:210
bool detect_duplicate_packet(const Address &source, SequenceNumber sn)
Detect duplicate packets See EN 302 636-4-1 v1.3.1 Annex A.2.
Definition: router.cpp:1199
NextHop greedy_forwarding(PendingPacketForwarding &&)
Determine next hop for greedy forwarding. See EN 302 636-4-1 v1.3.1 Annex E.2.
Definition: router.cpp:766
void pass_up(const DataIndication &, UpPacketPtr)
Pass packet up to the transport layer.
Definition: router.cpp:708
units::Duration timeout_cbf(units::Length distance) const
Determine CBF buffering time for a packet. Complies to EN 302 636-4-1 v1.3.1 Annex E....
Definition: router.cpp:864
void on_beacon_timer_expired()
Send Beacon packet to all neighbours with updated position vector. Only to be called when the beacon ...
Definition: router.cpp:716
void indicate_common(IndicationContext &, const BasicHeader &)
Process CommonHeader at packet indication.
Definition: router.cpp:426
bool decide_pass_up(bool within_destination, const GeoBroadcastHeader &gbc)
Decide if GBC packet shall be passed up to transport layer.
Definition: router.cpp:1158
void set_access_interface(dcc::RequestInterface *ifc)
Register access layer interface.
Definition: router.cpp:197
void set_random_seed(std::uint_fast32_t seed)
Set seed for internal random number generator (RNG) RNG is used e.g. for random Beacon jitter.
Definition: router.cpp:214
NextHop area_contention_based_forwarding(PendingPacketForwarding &&, const MacAddress *sender)
Determine next hop for area contention-based forwarding See EN 302 636-4-1 v1.3.1 Annex F....
Definition: router.cpp:846
std::unique_ptr< ShbPdu > create_shb_pdu(const ShbDataRequest &)
Create an initialized Single-Hop-Broadcast PDU.
Definition: router.cpp:1214
void flush_unicast_forwarding_buffer(const Address &addr)
Send all matching packets in the unicast forwarding buffer with expired waiting time.
Definition: router.cpp:1176
void detect_duplicate_address(const Address &source, const MacAddress &sender)
Helper method to handle duplicate addresses. If own address collides with the address of a received p...
Definition: router.cpp:1182
std::unique_ptr< BeaconPdu > create_beacon_pdu()
Create an initialzed Beacon PDU.
Definition: router.cpp:1225
void indicate_basic(IndicationContextBasic &)
Process BasicHeader at packet indication.
Definition: router.cpp:400
void set_dcc_field_generator(DccFieldGenerator *dcc)
Register generator for DCC-MCO fields.
Definition: router.cpp:203
Hook< PacketDropReason > packet_dropped
When a packet is dropped, this Hook is invoked.
Definition: router.hpp:155
Hook< ForwardingStopReason > forwarding_stopped
When packet forwarding is stopped, this Hook is invoked.
Definition: router.hpp:161
PacketDropReason
Reason for packet drop used by drop hook.
Definition: router.hpp:85
const LocationTable & get_location_table() const
Get the LocationTable. The table holds information about neighbouring ITS-Routers.
Definition: router.hpp:225
DataConfirm request(const ShbDataRequest &, DownPacketPtr)
Request to send payload per single hop broadcast (SHB). If security is enabled, the message gets enca...
Definition: router.cpp:219
void pass_down(const MacAddress &, PduPtr, DownPacketPtr)
Pass down the packet to the access layer.
Definition: router.cpp:694
void dispatch_repetition(const DataRequestVariant &, DownPacketPtr)
Callback function for dispatching a packet repetition. Invoked by Repeater when a scheduled repetitio...
Definition: router.cpp:760
NextHop non_area_contention_based_forwarding(PendingPacketForwarding &&, const MacAddress *sender)
Determine next hop for non-area contention-based forwarding See EN 302 636-4-1 v1....
Definition: router.cpp:805
void indicate(UpPacketPtr, const MacAddress &sender, const MacAddress &destination)
Handle the received packet on network layer. Packet handling involves these steps:
Definition: router.cpp:363
void set_transport_handler(UpperProtocol proto, TransportInterface *ifc)
Register a transport protocol handler.
Definition: router.cpp:187
void reset_beacon_timer()
Reschedule timer for next Beacon transmission Timer will be scheduled according to MIB's Beacon timer...
Definition: router.cpp:739
void update_position(const PositionFix &)
Update router's local position vector.
Definition: router.cpp:167
void execute_media_procedures(CommunicationProfile)
Executes media specific functionalities Details are described in TS 102 636-4-2.
Definition: router.cpp:652
void set_security_entity(security::SecurityEntity *entity)
Register security entity used when itsGnSecurity is enabled.
Definition: router.cpp:192
std::unique_ptr< GbcPdu > create_gbc_pdu(const GbcDataRequest &)
Create an initialized GeoBroadcast PDU.
Definition: router.cpp:1237
DownPacketPtr encap_packet(ItsAid aid, Pdu &pdu, DownPacketPtr packet)
Encaspulate a packet according to security profile.
Definition: router.cpp:1247
void set_address(const Address &)
Set Router's own GeoNetworking address.
Definition: router.cpp:209
NextHop forwarding_algorithm_selection(PendingPacketForwarding &&, const LinkLayer *ll=nullptr)
Definition: router.cpp:604
const LongPositionVector & get_local_position_vector() const
Get the local position vector. This vector describes the current position of the router.
Definition: router.hpp:233
void execute_itsg5_procedures()
Executes ITS-G5 media specific procedures Details are described in TS 102 636-4-2.
Definition: router.cpp:667
bool process_extended(const ExtendedPduConstRefs< BeaconHeader > &, const UpPacket &, const LinkLayer &ll)
Process ExtendedHeader information. Update router's LocationTable and neighbour relationship.
Definition: router.cpp:1015
void indicate_secured(IndicationContextBasic &, const BasicHeader &)
Process SecuredMessage at packet indication.
Definition: router.cpp:469
bool outside_sectorial_contention_area(const MacAddress &sender, const MacAddress &forwarder) const
Check if router is outside the sectorial contention area See TS 102 636-4-1 v1.2.3 section E....
Definition: router.cpp:952
BasicHeader specified in ETSI EN 302 636-4-1 v1.2.1, section 8.6.