]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
exthdr: add exthdr_desc_id enum and use it
authorPablo Neira Ayuso <pablo@netfilter.org>
Tue, 17 Dec 2019 17:16:53 +0000 (18:16 +0100)
committerFlorian Westphal <fw@strlen.de>
Tue, 17 Dec 2019 22:10:46 +0000 (23:10 +0100)
This allows to identify the exthdr protocol from the userdata area.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
include/exthdr.h
src/exthdr.c

index 3959a65c7713f0cd304b90806add20780f7e37d7..c9a3c211b8c4a810e3ba38bedc75eea9a014b912 100644 (file)
@@ -5,6 +5,20 @@
 #include <tcpopt.h>
 #include <ipopt.h>
 
+enum exthdr_desc_id {
+       EXTHDR_DESC_UNKNOWN     = 0,
+       EXTHDR_DESC_HBH,
+       EXTHDR_DESC_RT,
+       EXTHDR_DESC_RT0,
+       EXTHDR_DESC_RT2,
+       EXTHDR_DESC_SRH,
+       EXTHDR_DESC_FRAG,
+       EXTHDR_DESC_DST,
+       EXTHDR_DESC_MH,
+       __EXTHDR_DESC_MAX
+};
+#define EXTHDR_DESC_MAX        (__EXTHDR_DESC_MAX - 1)
+
 /**
  * struct exthdr_desc - extension header description
  *
@@ -14,6 +28,7 @@
  */
 struct exthdr_desc {
        const char                      *name;
+       enum exthdr_desc_id             id;
        uint8_t                         type;
        int                             proto_key;
        struct proto_hdr_template       templates[10];
index e1ec6f3dd52bcc8d531aebdbe2c7e4435cddb779..925b523290030afb445fc731fce2bda8e8f96301 100644 (file)
 #include <expression.h>
 #include <statement.h>
 
+static const struct exthdr_desc *exthdr_definitions[PROTO_DESC_MAX + 1] = {
+       [EXTHDR_DESC_HBH]       = &exthdr_hbh,
+       [EXTHDR_DESC_RT]        = &exthdr_rt,
+       [EXTHDR_DESC_RT0]       = &exthdr_rt0,
+       [EXTHDR_DESC_RT2]       = &exthdr_rt2,
+       [EXTHDR_DESC_SRH]       = &exthdr_rt4,
+       [EXTHDR_DESC_FRAG]      = &exthdr_frag,
+       [EXTHDR_DESC_DST]       = &exthdr_dst,
+       [EXTHDR_DESC_MH]        = &exthdr_mh,
+};
+
+static const struct exthdr_desc *exthdr_find_desc(enum exthdr_desc_id desc_id)
+{
+       if (desc_id >= EXTHDR_DESC_UNKNOWN &&
+           desc_id <= EXTHDR_DESC_MAX)
+               return exthdr_definitions[desc_id];
+
+       return NULL;
+}
+
 static void exthdr_expr_print(const struct expr *expr, struct output_ctx *octx)
 {
        if (expr->exthdr.op == NFT_EXTHDR_OP_TCPOPT) {
@@ -281,6 +301,7 @@ bool exthdr_find_template(struct expr *expr, const struct expr *mask, unsigned i
 
 const struct exthdr_desc exthdr_hbh = {
        .name           = "hbh",
+       .id             = EXTHDR_DESC_HBH,
        .type           = IPPROTO_HOPOPTS,
        .templates      = {
                [HBHHDR_NEXTHDR]        = HBH_FIELD("nexthdr", ip6h_nxt, &inet_protocol_type),
@@ -294,6 +315,7 @@ const struct exthdr_desc exthdr_hbh = {
 
 const struct exthdr_desc exthdr_rt2 = {
        .name           = "rt2",
+       .id             = EXTHDR_DESC_RT2,
        .type           = IPPROTO_ROUTING,
        .proto_key      = 2,
        .templates      = {
@@ -307,6 +329,7 @@ const struct exthdr_desc exthdr_rt2 = {
 
 const struct exthdr_desc exthdr_rt0 = {
        .name           = "rt0",
+       .id             = EXTHDR_DESC_RT0,
        .type           = IPPROTO_ROUTING,
        .proto_key      = 0,
        .templates      = {
@@ -322,6 +345,7 @@ const struct exthdr_desc exthdr_rt0 = {
 
 const struct exthdr_desc exthdr_rt4 = {
        .name           = "srh",
+       .id             = EXTHDR_DESC_SRH,
        .type           = IPPROTO_ROUTING,
        .proto_key      = 4,
        .templates      = {
@@ -340,6 +364,7 @@ const struct exthdr_desc exthdr_rt4 = {
 
 const struct exthdr_desc exthdr_rt = {
        .name           = "rt",
+       .id             = EXTHDR_DESC_RT,
        .type           = IPPROTO_ROUTING,
        .proto_key      = -1,
 #if 0
@@ -366,6 +391,7 @@ const struct exthdr_desc exthdr_rt = {
 
 const struct exthdr_desc exthdr_frag = {
        .name           = "frag",
+       .id             = EXTHDR_DESC_FRAG,
        .type           = IPPROTO_FRAGMENT,
        .templates      = {
                [FRAGHDR_NEXTHDR]       = FRAG_FIELD("nexthdr", ip6f_nxt, &inet_protocol_type),
@@ -392,6 +418,7 @@ const struct exthdr_desc exthdr_frag = {
 
 const struct exthdr_desc exthdr_dst = {
        .name           = "dst",
+       .id             = EXTHDR_DESC_DST,
        .type           = IPPROTO_DSTOPTS,
        .templates      = {
                [DSTHDR_NEXTHDR]        = DST_FIELD("nexthdr", ip6d_nxt, &inet_protocol_type),
@@ -438,6 +465,7 @@ const struct datatype mh_type_type = {
 
 const struct exthdr_desc exthdr_mh = {
        .name           = "mh",
+       .id             = EXTHDR_DESC_MH,
        .type           = IPPROTO_MH,
        .templates      = {
                [MHHDR_NEXTHDR]         = MH_FIELD("nexthdr", ip6mh_proto, &inet_protocol_type),