]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: make RouteTable= setting can take multiple name:number pairs in a line
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 20 Jan 2021 05:33:21 +0000 (14:33 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 3 Feb 2021 05:32:26 +0000 (14:32 +0900)
Follow-up for c038ce4606f93d9e58147f87703125270fb744e2.

man/networkd.conf.xml
src/network/networkd-route.c

index 12ddcf4502669a398144149e5b6025a7f6171317..dcce2095edd2fb5e377852b263bdba84c7c02567 100644 (file)
 
       <varlistentry>
         <term><varname>RouteTable=</varname></term>
-        <listitem><para>Specifies the route table name. Takes a route name and table number separated with a
-        colon. (<literal><replaceable>name</replaceable>:<replaceable>integer</replaceable></literal>. The
-        route table number must be an integer in the range 1…4294967295. This setting can be specified
-        multiple times. If an empty string is specified, then all options specified earlier are cleared.
-        Defaults to unset.</para></listitem>
+        <listitem><para>Defines the route table name. Takes a whitespace-separated list of the pairs of
+        route table name and number. The route table name and number in each pair are separated with a
+        colon, i.e., <literal><replaceable>name</replaceable>:<replaceable>number</replaceable></literal>.
+        The route table name must not be <literal>default</literal>, <literal>main</literal>, or
+        <literal>local</literal>, as these route table names are predefined with route table number 253,
+        254, and 255, respectively. The route table number must be an integer in the range 1…4294967295.
+        This setting can be specified multiple times. If an empty string is specified, then the list
+        specified earlier are cleared. Defaults to unset.</para></listitem>
       </varlistentry>
 
     </variablelist>
index cfb9befc61459cb069c4f4f36968f5410495414f..ed6a846e15971409321746324508bd1ecc9d7ef6 100644 (file)
@@ -2385,10 +2385,7 @@ int config_parse_route_table_names(
                 void *data,
                 void *userdata) {
 
-        _cleanup_free_ char *name = NULL;
         Hashmap **s = data;
-        uint32_t table;
-        const char *p;
         int r;
 
         assert(filename);
@@ -2401,47 +2398,65 @@ int config_parse_route_table_names(
                 return 0;
         }
 
-        p = rvalue;
-        r = extract_first_word(&p, &name, ":", 0);
-        if (r == -ENOMEM)
-                return log_oom();
-        if (r <= 0 || isempty(p)) {
-                log_syntax(unit, LOG_WARNING, filename, line, r,
-                           "Invalid RouteTable=, ignoring assignment: %s", rvalue);
-                return 0;
-        }
+        for (const char *p = rvalue;;) {
+                _cleanup_free_ char *name = NULL;
+                uint32_t table;
+                char *num;
 
-        if (STR_IN_SET(name, "default", "main","local")) {
-                log_syntax(unit, LOG_WARNING, filename, line, r,
-                           "Route table name %s already preconfigured. Ignoring assignment: %s", name, rvalue);
-                return 0;
-        }
+                r = extract_first_word(&p, &name, NULL, 0);
+                if (r == -ENOMEM)
+                        return log_oom();
+                if (r < 0) {
+                        log_syntax(unit, LOG_WARNING, filename, line, r,
+                                   "Invalid RouteTable=, ignoring assignment: %s", rvalue);
+                        return 0;
+                }
+                if (r == 0)
+                        return 0;
 
-        r = safe_atou32(p, &table);
-        if (r < 0) {
-                log_syntax(unit, LOG_WARNING, filename, line, r,
-                           "Failed to parse RouteTable=, ignoring assignment: %s", p);
-                return 0;
-        }
+                num = strchr(name, ':');
+                if (!num) {
+                        log_syntax(unit, LOG_WARNING, filename, line, 0,
+                                   "Invalid route table name and number pair, ignoring assignment: %s", name);
+                        continue;
+                }
 
-        if (table == 0) {
-                log_syntax(unit, LOG_WARNING, filename, line, 0,
-                           "Invalid RouteTable=, ignoring assignment: %s", p);
-                return 0;
-        }
+                *num++ = '\0';
 
-        r = hashmap_ensure_put(s, &string_hash_ops, name, UINT32_TO_PTR(table));
-        if (r == -ENOMEM)
-                return log_oom();
-        if (r == -EEXIST) {
-                log_syntax(unit, LOG_WARNING, filename, line, r,
-                           "Specified RouteTable= name and value pair conflicts with others, ignoring assignment: %s", rvalue);
-                return 0;
-        }
-        if (r > 0)
-                TAKE_PTR(name);
+                if (STR_IN_SET(name, "default", "main", "local")) {
+                        log_syntax(unit, LOG_WARNING, filename, line, 0,
+                                   "Route table name %s already predefined. Ignoring assignment: %s:%s", name, name, num);
+                        continue;
+                }
 
-        return 0;
+                r = safe_atou32(num, &table);
+                if (r < 0) {
+                        log_syntax(unit, LOG_WARNING, filename, line, r,
+                                   "Failed to parse route table number '%s', ignoring assignment: %s:%s", num, name, num);
+                        continue;
+                }
+                if (table == 0) {
+                        log_syntax(unit, LOG_WARNING, filename, line, 0,
+                                   "Invalid route table number, ignoring assignment: %s:%s", name, num);
+                        continue;
+                }
+
+                r = hashmap_ensure_put(s, &string_hash_ops, name, UINT32_TO_PTR(table));
+                if (r == -ENOMEM)
+                        return log_oom();
+                if (r == -EEXIST) {
+                        log_syntax(unit, LOG_WARNING, filename, line, r,
+                                   "Specified route table name and number pair conflicts with others, ignoring assignment: %s:%s", name, num);
+                        continue;
+                }
+                if (r < 0) {
+                        log_syntax(unit, LOG_WARNING, filename, line, r,
+                                   "Failed to store route table name and number pair, ignoring assignment: %s:%s", name, num);
+                        continue;
+                }
+                if (r > 0)
+                        TAKE_PTR(name);
+        }
 }
 
 static int route_section_verify(Route *route, Network *network) {