]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network/dhcp4: create prefix route and route to gateway in the specified table with...
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 26 Jan 2025 20:17:44 +0000 (05:17 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 26 Jan 2025 23:38:14 +0000 (08:38 +0900)
Previously, the following setting
====
[Route]
Gateway=_dhcp4
Table=100
====
only configured the route in the specified table. But it was mostly
useless. This makes prefix route and route to the gateway are also
configured in the specified table.

Before:
====
$ ip route show table 100
default via 192.168.0.1 dev eth0 proto dhcp metric 1024
====

After:
====
$ ip route show table 100
default via 192.168.0.1 dev eth0 proto dhcp metric 1024
192.168.0.0/24 dev eth0 proto dhcp src 192.168.0.100 metric 1024
192.168.0.1 dev eth0 proto dhcp scope link src 192.168.0.100 metric 1024
====

src/network/networkd-dhcp4.c

index 8790b61ee222e1ae8b779cec364a19571306a966..949f08cf53f36a5bad050d2fea12a3d39be27041 100644 (file)
@@ -410,14 +410,14 @@ static bool prefixroute_by_kernel(Link *link) {
                 link->network->dhcp_route_table == RT_TABLE_MAIN;
 }
 
-static int dhcp4_request_prefix_route(Link *link) {
+static int dhcp4_request_prefix_route(Link *link, Route *rt) {
         _cleanup_(route_unrefp) Route *route = NULL;
         int r;
 
         assert(link);
         assert(link->dhcp_lease);
 
-        if (prefixroute_by_kernel(link))
+        if (prefixroute_by_kernel(link) && (!rt || !rt->table_set || rt->table == RT_TABLE_MAIN))
                 /* The prefix route in the main table will be created by the kernel. See dhcp4_update_address(). */
                 return 0;
 
@@ -426,6 +426,10 @@ static int dhcp4_request_prefix_route(Link *link) {
                 return r;
 
         route->scope = RT_SCOPE_LINK;
+        if (rt) {
+                route->table_set = rt->table_set;
+                route->table = rt->table;
+        }
 
         r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &route->dst.in, &route->dst_prefixlen);
         if (r < 0)
@@ -464,6 +468,8 @@ static int dhcp4_request_route_to_gateway(Link *link, const Route *rt) {
         route->dst_prefixlen = 32;
         route->prefsrc.in = address;
         route->scope = RT_SCOPE_LINK;
+        route->table = rt->table;
+        route->table_set = rt->table_set;
 
         return dhcp4_request_route(route, link);
 }
@@ -675,6 +681,10 @@ static int dhcp4_request_semi_static_routes(Link *link) {
 
                 route->nexthop.gw.in = gw;
 
+                r = dhcp4_request_prefix_route(link, route);
+                if (r < 0)
+                        return r;
+
                 r = dhcp4_request_route_to_gateway(link, route);
                 if (r < 0)
                         return r;
@@ -778,7 +788,7 @@ static int dhcp4_request_routes(Link *link) {
         assert(link);
         assert(link->dhcp_lease);
 
-        r = dhcp4_request_prefix_route(link);
+        r = dhcp4_request_prefix_route(link, /* rt = */ NULL);
         if (r < 0)
                 return log_link_error_errno(link, r, "DHCP error: Could not request prefix route: %m");