]> git.puffer.fish Git - mirror/frr.git/commitdiff
zebra: add validate function for zapi_labels message 6765/head
authorMark Stapp <mjs@voltanet.io>
Mon, 20 Jul 2020 21:19:31 +0000 (17:19 -0400)
committerMark Stapp <mjs@voltanet.io>
Tue, 21 Jul 2020 12:04:00 +0000 (08:04 -0400)
Add a simple validation function for zapi_labels messages; it
checks for and validates backup nexthop indexes currently.

Signed-off-by: Mark Stapp <mjs@voltanet.io>
zebra/zapi_msg.c

index 632ae08f5109c735f4c326664f4b1010127f29b7..73b73c5caaa60d7f177caffae9327d0184bda74b 100644 (file)
@@ -2000,6 +2000,56 @@ static void zread_vrf_unregister(ZAPI_HANDLER_ARGS)
        vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
 }
 
+/*
+ * Validate incoming zapi mpls lsp / labels message
+ */
+static int zapi_labels_validate(const struct zapi_labels *zl)
+{
+       int ret = -1;
+       int i, j, idx;
+       uint32_t bits[8];
+       uint32_t ival;
+       const struct zapi_nexthop *znh;
+
+       /* Validate backup info: no duplicates for a single primary */
+       if (zl->backup_nexthop_num == 0) {
+               ret = 0;
+               goto done;
+       }
+
+       for (j = 0; j < zl->nexthop_num; j++) {
+               znh = &zl->nexthops[j];
+
+               memset(bits, 0, sizeof(bits));
+
+               for (i = 0; i < znh->backup_num; i++) {
+                       idx = znh->backup_idx[i] / 32;
+
+                       ival = 1 << znh->backup_idx[i] % 32;
+
+                       /* Check whether value is already used */
+                       if (ival & bits[idx]) {
+                               /* Fail */
+
+                               if (IS_ZEBRA_DEBUG_RECV)
+                                       zlog_debug("%s: invalid zapi mpls message: duplicate backup nexthop index %d",
+                                                  __func__,
+                                                  znh->backup_idx[i]);
+                               goto done;
+                       }
+
+                       /* Mark index value */
+                       bits[idx] |= ival;
+               }
+       }
+
+       ret = 0;
+
+done:
+
+       return ret;
+}
+
 /*
  * Handle request to create an MPLS LSP.
  *
@@ -2026,6 +2076,10 @@ static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS)
        if (!mpls_enabled)
                return;
 
+       /* Validate; will debug on failure */
+       if (zapi_labels_validate(&zl) < 0)
+               return;
+
        ret = mpls_zapi_labels_process(true, zvrf, &zl);
        if (ret < 0) {
                if (IS_ZEBRA_DEBUG_RECV)
@@ -2107,6 +2161,10 @@ static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS)
        if (!mpls_enabled)
                return;
 
+       /* Validate; will debug on failure */
+       if (zapi_labels_validate(&zl) < 0)
+               return;
+
        /* This removes everything, then re-adds from the client's
         * zapi message. Since the LSP will be processed later, on this
         * this same pthread, all of the changes will 'appear' at once.