iprule: resolve ipproto by name
authorIvan Mosin <[email protected]>
Fri, 9 May 2025 22:48:23 +0000 (01:48 +0300)
committerRobert Marko <[email protected]>
Sun, 18 May 2025 16:22:22 +0000 (18:22 +0200)
Handle ipproto as an string. Set protocol in lowercase for musl libc compatibility.

Signed-off-by: Ivan Mosin <[email protected]>
Link: https://github.com/openwrt/netifd/pull/43
Signed-off-by: Robert Marko <[email protected]>
iprule.c
system-dummy.c
system-linux.c
system.h

index 39ce1277dffac245d22cc1e48bfa8c22801ebfb9..f6f9d4ee7ec90d6c36c8dff770a408652997a02a 100644 (file)
--- a/iprule.c
+++ b/iprule.c
@@ -64,7 +64,7 @@ static const struct blobmsg_policy rule_attr[__RULE_MAX] = {
        [RULE_UIDRANGE] = { .name = "uidrange", .type = BLOBMSG_TYPE_STRING },
        [RULE_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
        [RULE_GOTO]   = { .name = "goto", .type = BLOBMSG_TYPE_INT32 },
-       [RULE_IPPROTO]  = { .name = "ipproto", .type = BLOBMSG_TYPE_INT32 },
+       [RULE_IPPROTO]  = { .name = "ipproto", .type = BLOBMSG_TYPE_STRING },
        [RULE_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL },
 };
 
@@ -312,8 +312,8 @@ iprule_add(struct blob_attr *attr, bool v6)
        }
 
        if ((cur = tb[RULE_IPPROTO]) != NULL) {
-               if ((rule->ipproto = blobmsg_get_u32(cur)) > 255) {
-                       D(INTERFACE, "Invalid ipproto value: %u", blobmsg_get_u32(cur));
+               if (!system_resolve_iprule_ipproto(blobmsg_data(cur), &rule->ipproto)) {
+                       D(INTERFACE, "Failed to parse rule ip protocol: %s", (char *) blobmsg_data(cur));
                        goto error;
                }
                rule->flags |= IPRULE_IPPROTO;
index a6e52bff67c34a623bc5960cf1e5d6d57d2bd181..c698361ba6a6b033c99ea4d0e4eb80bf0d64808a 100644 (file)
@@ -323,6 +323,12 @@ int system_flush_iprules(void)
        return 0;
 }
 
+bool system_resolve_iprule_ipproto(const char *name, unsigned int *id)
+{
+       *id = 0;
+       return true;
+}
+
 bool system_resolve_iprule_action(const char *action, unsigned int *id)
 {
        *id = 0;
index 46b5b9b34819e78caea6aebf033eda8ff062961d..5c525ce8f2649e73288a97eb882fc169d3d56a19 100644 (file)
@@ -27,6 +27,7 @@
 #include <net/if_arp.h>
 
 #include <limits.h>
+#include <netdb.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
 #include <netinet/ether.h>
@@ -3746,6 +3747,27 @@ bool system_resolve_iprule_action(const char *action, unsigned int *id)
        return system_rtn_aton(action, id);
 }
 
+bool system_resolve_iprule_ipproto(const char *name, unsigned int *id)
+{
+       char *e;
+       struct protoent *ent;
+       unsigned int n, ipproto = 0;
+
+       if ((n = strtoul(name, &e, 0)) > 0 && *e == '\0')
+               ipproto = n;
+       else {
+               ent = getprotobyname(name);
+
+               if (ent)
+                       ipproto = ent->p_proto;
+               else
+                       return false;
+       }
+
+       *id = ipproto;
+       return true;
+}
+
 time_t system_get_rtime(void)
 {
        struct timespec ts;
index 96bfd070bfecfa17a3f3c7116985a136a9b4343a..084ca969108839cf34253f77eb9991a685b19ee8 100644 (file)
--- a/system.h
+++ b/system.h
@@ -311,6 +311,7 @@ int system_add_iprule(struct iprule *rule);
 int system_del_iprule(struct iprule *rule);
 int system_flush_iprules(void);
 
+bool system_resolve_iprule_ipproto(const char *name, unsigned int *id);
 bool system_resolve_iprule_action(const char *action, unsigned int *id);
 
 time_t system_get_rtime(void);