| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | #pragma once |
| 3 | |
| 4 | #include <functional> |
| 5 | #include "NetlinkChannel.h" |
| 6 | #include "Route.h" |
| 7 | #include "Operation.h" |
| 8 | |
| 9 | struct RouteMessage |
| 10 | { |
| 11 | rtmsg route; |
| 12 | utils::IntegerAttribute tableId; |
| 13 | utils::IntegerAttribute dev; |
| 14 | } __attribute__((packed)); |
| 15 | |
| 16 | // The below is a means to ensure that messages have a common set of fields. It just means |
| 17 | // that a type T must inherit from RouteMessage, and any functions that reference |
| 18 | // DerivedRouteMessage can be assured that they can safely access the fields in RouteMessage. |
| 19 | template <typename TMessage> |
| 20 | concept DerivedRouteMessage = std::is_base_of<RouteMessage, TMessage>::value; |
| 21 | |
| 22 | class RoutingTable |
| 23 | { |
| 24 | public: |
| 25 | RoutingTable(int table); |
| 26 | RoutingTable(const RoutingTable&) = delete; |
| 27 | RoutingTable(RoutingTable&&) = delete; |
| 28 | |
| 29 | const RoutingTable& operator=(const RoutingTable&) const = delete; |
| 30 | const RoutingTable& operator=(RoutingTable&&) const = delete; |
| 31 | |
| 32 | void ChangeTableId(int newTableId); |
| 33 | |
| 34 | void ModifyRoute(const Route& route, Operation action); |
| 35 | |
| 36 | std::vector<Route> ListRoutes(int family); |
| 37 | |
| 38 | void RemoveAll(int addressFamily); |
| 39 | |
| 40 | private: |
| 41 | /* |
| 42 | Implement netlink equivalent of |
| 43 | "ip route <operation> table <id> <destination> via <gateway> src <source> dev <interface> onlink". |
| 44 | |
| 45 | Note: the preferred source is set equal to destination. See comments in method implementation for more |
| 46 | details. |
| 47 | */ |
| 48 | template <typename TAddr> |
| 49 | void ModifyLoopbackRouteImpl(const Route& route, int operation, int flags); |
| 50 | |
| 51 | template <typename TAddr> |
| 52 | void ModifyDefaultRouteImpl(const Route& route, int operation, int flags); |
| 53 | |
| 54 | template <typename TAddr> |
| 55 | void ModifyDefaultLinkLocalRouteImpl(const Route& route, int operation, int flags); |
| 56 | |
| 57 | template <typename TAddr> |
| 58 | void ModifyLinkLocalRouteImpl(const Route& route, int operation, int flags); |
| 59 | |
| 60 | /* |
| 61 | By "offlink route", we mean a route with a specific destination prefix and a specific next hop. |
| 62 | |
| 63 | Contrast this to a default route which has no specific destination prefix and a link local |
| 64 | route which has no specific next hop. |
| 65 | */ |
| 66 | template <typename TAddr> |
| 67 | void ModifyOfflinkRouteImpl(const Route& route, int operation, int flags); |
| 68 | |
| 69 | template <typename TAddr> |
| 70 | void ModifyRouteImpl(const Route& route, Operation action); |
| 71 | |
| 72 | /* |
| 73 | Creates the message using the routine, then sends the message via netlink. |
| 74 | */ |
| 75 | template <DerivedRouteMessage TMessage> |
| 76 | void SendMessage(const Route& route, int operation, int flags, const std::function<void(TMessage&)>& routine = [](auto&) {}); |
| 77 | |
| 78 | NetlinkChannel m_channel; |
| 79 | int m_table; |
| 80 | }; |