1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <inttypes.h>
5 #include <stdbool.h>
6 
7 #include "sd-netlink.h"
8 
9 #include "conf-parser.h"
10 #include "in-addr-util.h"
11 #include "networkd-link.h"
12 #include "networkd-util.h"
13 
14 typedef struct Manager Manager;
15 typedef struct Network Network;
16 typedef struct Request Request;
17 typedef struct Route Route;
18 typedef int (*route_netlink_handler_t)(
19                 sd_netlink *rtnl,
20                 sd_netlink_message *m,
21                 Request *req,
22                 Link *link,
23                 Route *route);
24 
25 struct Route {
26         Link *link;
27         Manager *manager;
28         Network *network;
29         ConfigSection *section;
30         NetworkConfigSource source;
31         NetworkConfigState state;
32         union in_addr_union provider; /* DHCP server or router address */
33 
34         int family;
35         int gw_family;
36         uint32_t gw_weight;
37         int quickack;
38         int fast_open_no_cookie;
39         int ttl_propagate;
40 
41         unsigned char dst_prefixlen;
42         unsigned char src_prefixlen;
43         unsigned char scope;
44         unsigned char protocol;  /* RTPROT_* */
45         unsigned char type; /* RTN_* */
46         unsigned char tos;
47         uint32_t priority; /* note that ip(8) calls this 'metric' */
48         uint32_t table;
49         uint32_t mtu;
50         uint32_t initcwnd;
51         uint32_t initrwnd;
52         uint32_t advmss;
53         unsigned char pref;
54         unsigned flags;
55         int gateway_onlink; /* Only used in conf parser and route_section_verify(). */
56         uint32_t nexthop_id;
57 
58         bool scope_set:1;
59         bool table_set:1;
60         bool priority_set:1;
61         bool protocol_set:1;
62         bool pref_set:1;
63         bool gateway_from_dhcp_or_ra:1;
64 
65         union in_addr_union gw;
66         union in_addr_union dst;
67         union in_addr_union src;
68         union in_addr_union prefsrc;
69         OrderedSet *multipath_routes;
70 
71         /* This is an absolute point in time, and NOT a timespan/duration.
72          * Must be specified with clock_boottime_or_monotonic(). */
73         usec_t lifetime_usec;
74         /* Used when kernel does not support RTA_EXPIRES attribute. */
75         sd_event_source *expire;
76 };
77 
78 extern const struct hash_ops route_hash_ops;
79 
80 int route_new(Route **ret);
81 Route *route_free(Route *route);
82 DEFINE_SECTION_CLEANUP_FUNCTIONS(Route, route_free);
83 int route_dup(const Route *src, Route **ret);
84 
85 int route_configure_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg);
86 int route_remove(Route *route);
87 
88 int route_get(Manager *manager, Link *link, const Route *in, Route **ret);
89 
90 int link_drop_managed_routes(Link *link);
91 int link_drop_foreign_routes(Link *link);
92 void link_foreignize_routes(Link *link);
93 
94 void route_cancel_request(Route *route, Link *link);
95 int link_request_route(
96                 Link *link,
97                 Route *route,
98                 bool consume_object,
99                 unsigned *message_counter,
100                 route_netlink_handler_t netlink_handler,
101                 Request **ret);
102 int link_request_static_routes(Link *link, bool only_ipv4);
103 
104 int manager_rtnl_process_route(sd_netlink *rtnl, sd_netlink_message *message, Manager *m);
105 
106 int network_add_ipv4ll_route(Network *network);
107 int network_add_default_route_on_device(Network *network);
108 void network_drop_invalid_routes(Network *network);
109 
110 DEFINE_NETWORK_CONFIG_STATE_FUNCTIONS(Route, route);
111 void link_mark_routes(Link *link, NetworkConfigSource source, const struct in6_addr *router);
112 
113 CONFIG_PARSER_PROTOTYPE(config_parse_gateway);
114 CONFIG_PARSER_PROTOTYPE(config_parse_preferred_src);
115 CONFIG_PARSER_PROTOTYPE(config_parse_destination);
116 CONFIG_PARSER_PROTOTYPE(config_parse_route_priority);
117 CONFIG_PARSER_PROTOTYPE(config_parse_route_scope);
118 CONFIG_PARSER_PROTOTYPE(config_parse_route_table);
119 CONFIG_PARSER_PROTOTYPE(config_parse_route_boolean);
120 CONFIG_PARSER_PROTOTYPE(config_parse_ipv6_route_preference);
121 CONFIG_PARSER_PROTOTYPE(config_parse_route_protocol);
122 CONFIG_PARSER_PROTOTYPE(config_parse_route_type);
123 CONFIG_PARSER_PROTOTYPE(config_parse_tcp_window);
124 CONFIG_PARSER_PROTOTYPE(config_parse_route_mtu);
125 CONFIG_PARSER_PROTOTYPE(config_parse_multipath_route);
126 CONFIG_PARSER_PROTOTYPE(config_parse_tcp_advmss);
127 CONFIG_PARSER_PROTOTYPE(config_parse_route_nexthop);
128