]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Reimplement RTR version validation
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Mon, 23 Mar 2026 22:26:49 +0000 (16:26 -0600)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Mon, 23 Mar 2026 22:26:49 +0000 (16:26 -0600)
Fixes #171.

src/rtr/pdu_stream.c

index 4c64dce8b61055f0df58b05cd1e46b079d8c1a90..498eec4c09e136e85ffef49d5641fbdd25fa1b5a 100644 (file)
@@ -238,70 +238,27 @@ read_hdr(struct pdu_stream *stream, struct pdu_header *header)
 }
 
 static int
-validate_rtr_version(struct pdu_stream *stream, struct pdu_header *header,
+validate_rtr_version(struct pdu_stream *stream, struct pdu_header *hdr,
     struct rtr_buffer *request)
 {
-       switch (stream->rtr_version) {
-       case RTR_V2:
-               switch (header->version) {
-               case RTR_V2:
+       if (stream->rtr_version == -1) {
+               if (RTR_V0 <= hdr->version && hdr->version <= RTR_V2) {
+                       stream->rtr_version = hdr->version;
                        return 0;
-               case RTR_V1:
-               case RTR_V0:
-                       goto unexpected;
-               default:
-                       goto unsupported;
-               }
-
-       case RTR_V1:
-               switch (header->version) {
-               case RTR_V1:
-                       return 0;
-               case RTR_V0:
-               case RTR_V2:
-                       goto unexpected;
-               default:
-                       goto unsupported;
-               }
-
-       case RTR_V0:
-               switch (header->version) {
-               case RTR_V0:
-                       return 0;
-               case RTR_V1:
-               case RTR_V2:
-                       goto unexpected;
-               default:
-                       goto unsupported;
-               }
-
-       case -1:
-               switch (header->version) {
-               case RTR_V0:
-               case RTR_V1:
-               case RTR_V2:
-                       stream->rtr_version = header->version;
-                       return 0;
-               default:
-                       goto unsupported;
                }
+               return err_pdu_send_unsupported_proto_version(
+                       stream->fd, RTR_V2, request,
+                       "The maximum supported RTR version is 2."
+               );
        }
 
-       pr_crit("Unknown RTR version %u", stream->rtr_version);
-
-unsupported:
-       return err_pdu_send_unsupported_proto_version(
-               stream->fd,
-               (stream->rtr_version != -1) ? stream->rtr_version : RTR_V0,
-               request,
-               "The maximum supported RTR version is 2."
-       );
-
-unexpected:
-       return err_pdu_send_unexpected_proto_version(
-               stream->fd, stream->rtr_version, request,
-               "The RTR version does not match the one we negotiated during the handshake."
-       );
+       if (stream->rtr_version != hdr->version)
+               return err_pdu_send_unexpected_proto_version(
+                       stream->fd, stream->rtr_version, request,
+                       "The RTR version does not match the one we negotiated during the handshake."
+               );
+
+       return 0;
 }
 
 static int