]> The Tcpdump Group git mirrors - libpcap/commitdiff
Factor assert_ss7() out.
authorDenis Ovsienko <[email protected]>
Fri, 21 Mar 2025 15:42:51 +0000 (15:42 +0000)
committerDenis Ovsienko <[email protected]>
Sat, 22 Mar 2025 17:20:20 +0000 (17:20 +0000)
Add another helper function for SS7 keywords, replace 10 hard-coded
error messages with one parametrised message and converge the check
conditions.

gencode.c
testprogs/TESTrun

index e568488cfca615e45a7482ebce0cd617eba6f75c..759305706215ff7723b6813c674d5b5a3cd12127 100644 (file)
--- a/gencode.c
+++ b/gencode.c
@@ -977,6 +977,29 @@ atmkw(const unsigned id)
        return qual2kw("ATM keyword", id, tokens, sizeof(tokens) / sizeof(tokens[0]));
 }
 
+// SS7 keywords
+static const char *
+ss7kw(const unsigned id)
+{
+       const char * tokens[] = {
+               [M_FISU] = "fisu",
+               [M_LSSU] = "lssu",
+               [M_MSU] = "msu",
+               [MH_FISU] = "hfisu",
+               [MH_LSSU] = "hlssu",
+               [MH_MSU] = "hmsu",
+               [M_SIO] = "sio",
+               [M_OPC] = "opc",
+               [M_DPC] = "dpc",
+               [M_SLS] = "sls",
+               [MH_SIO] = "hsio",
+               [MH_OPC] = "hopc",
+               [MH_DPC] = "hdpc",
+               [MH_SLS] = "hsls",
+       };
+       return qual2kw("MTP keyword", id, tokens, sizeof(tokens) / sizeof(tokens[0]));
+}
+
 static PCAP_NORETURN_DEF void
 fail_kw_on_dlt(compiler_state_t *cstate, const char *keyword)
 {
@@ -1007,6 +1030,23 @@ assert_atm(compiler_state_t *cstate, const char *kw)
                bpf_error(cstate, "'%s' supported only on SUNATM", kw);
 }
 
+static void
+assert_ss7(compiler_state_t *cstate, const char *kw)
+{
+       switch (cstate->linktype) {
+       case DLT_MTP2:
+       case DLT_ERF:
+       case DLT_MTP2_WITH_PHDR:
+               // Belt and braces, same as in assert_atm().
+               if (cstate->off_sio != OFFSET_NOT_SET &&
+                   cstate->off_opc != OFFSET_NOT_SET &&
+                   cstate->off_dpc != OFFSET_NOT_SET &&
+                   cstate->off_sls != OFFSET_NOT_SET)
+                       return;
+       }
+       bpf_error(cstate, "'%s' supported only on SS7", kw);
+}
+
 #define ERRSTR_802_11_ONLY_KW "'%s' is valid for 802.11 syntax only"
 #define ERRSTR_INVALID_QUAL "'%s' is not a valid qualifier for '%s'"
 
@@ -10006,23 +10046,17 @@ gen_mtp2type_abbrev(compiler_state_t *cstate, int type)
        if (setjmp(cstate->top_ctx))
                return (NULL);
 
+       assert_ss7(cstate, ss7kw(type));
+
        switch (type) {
 
        case M_FISU:
-               if ( (cstate->linktype != DLT_MTP2) &&
-                    (cstate->linktype != DLT_ERF) &&
-                    (cstate->linktype != DLT_MTP2_WITH_PHDR) )
-                       bpf_error(cstate, "'fisu' supported only on MTP2");
                /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
                b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
                    0x3fU, BPF_JEQ, 0, 0U);
                break;
 
        case M_LSSU:
-               if ( (cstate->linktype != DLT_MTP2) &&
-                    (cstate->linktype != DLT_ERF) &&
-                    (cstate->linktype != DLT_MTP2_WITH_PHDR) )
-                       bpf_error(cstate, "'lssu' supported only on MTP2");
                b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
                    0x3fU, BPF_JGT, 1, 2U);
                b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
@@ -10031,29 +10065,17 @@ gen_mtp2type_abbrev(compiler_state_t *cstate, int type)
                break;
 
        case M_MSU:
-               if ( (cstate->linktype != DLT_MTP2) &&
-                    (cstate->linktype != DLT_ERF) &&
-                    (cstate->linktype != DLT_MTP2_WITH_PHDR) )
-                       bpf_error(cstate, "'msu' supported only on MTP2");
                b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
                    0x3fU, BPF_JGT, 0, 2U);
                break;
 
        case MH_FISU:
-               if ( (cstate->linktype != DLT_MTP2) &&
-                    (cstate->linktype != DLT_ERF) &&
-                    (cstate->linktype != DLT_MTP2_WITH_PHDR) )
-                       bpf_error(cstate, "'hfisu' supported only on MTP2_HSL");
                /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
                b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
                    0xff80U, BPF_JEQ, 0, 0U);
                break;
 
        case MH_LSSU:
-               if ( (cstate->linktype != DLT_MTP2) &&
-                    (cstate->linktype != DLT_ERF) &&
-                    (cstate->linktype != DLT_MTP2_WITH_PHDR) )
-                       bpf_error(cstate, "'hlssu' supported only on MTP2_HSL");
                b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
                    0xff80U, BPF_JGT, 1, 0x0100U);
                b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
@@ -10062,10 +10084,6 @@ gen_mtp2type_abbrev(compiler_state_t *cstate, int type)
                break;
 
        case MH_MSU:
-               if ( (cstate->linktype != DLT_MTP2) &&
-                    (cstate->linktype != DLT_ERF) &&
-                    (cstate->linktype != DLT_MTP2_WITH_PHDR) )
-                       bpf_error(cstate, "'hmsu' supported only on MTP2_HSL");
                b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
                    0xff80U, BPF_JGT, 0, 0x0100U);
                break;
@@ -10098,6 +10116,9 @@ gen_mtp3field_code_internal(compiler_state_t *cstate, int mtp3field,
        newoff_opc = cstate->off_opc;
        newoff_dpc = cstate->off_dpc;
        newoff_sls = cstate->off_sls;
+
+       assert_ss7(cstate, ss7kw(mtp3field));
+
        switch (mtp3field) {
 
        /*
@@ -10112,8 +10133,6 @@ gen_mtp3field_code_internal(compiler_state_t *cstate, int mtp3field,
                /* FALLTHROUGH */
 
        case M_SIO:
-               if (cstate->off_sio == OFFSET_NOT_SET)
-                       bpf_error(cstate, "'sio' supported only on SS7");
                if(jvalue > MTP2_SIO_MAXVAL)
                        bpf_error(cstate, "sio value %u too big; max value = %u",
                            jvalue, MTP2_SIO_MAXVAL);
@@ -10156,8 +10175,6 @@ gen_mtp3field_code_internal(compiler_state_t *cstate, int mtp3field,
 
                /* FALLTHROUGH */
        case M_OPC:
-               if (cstate->off_opc == OFFSET_NOT_SET)
-                       bpf_error(cstate, "'opc' supported only on SS7");
                if (jvalue > MTP3_PC_MAXVAL)
                        bpf_error(cstate, "opc value %u too big; max value = %u",
                            jvalue, MTP3_PC_MAXVAL);
@@ -10171,8 +10188,6 @@ gen_mtp3field_code_internal(compiler_state_t *cstate, int mtp3field,
                /* FALLTHROUGH */
 
        case M_DPC:
-               if (cstate->off_dpc == OFFSET_NOT_SET)
-                       bpf_error(cstate, "'dpc' supported only on SS7");
                if (jvalue > MTP3_PC_MAXVAL)
                        bpf_error(cstate, "dpc value %u too big; max value = %u",
                            jvalue, MTP3_PC_MAXVAL);
@@ -10186,8 +10201,6 @@ gen_mtp3field_code_internal(compiler_state_t *cstate, int mtp3field,
                /* FALLTHROUGH */
 
        case M_SLS:
-               if (cstate->off_sls == OFFSET_NOT_SET)
-                       bpf_error(cstate, "'sls' supported only on SS7");
                if (jvalue > MTP3_SLS_MAXVAL)
                         bpf_error(cstate, "sls value %u too big; max value = %u",
                             jvalue, MTP3_SLS_MAXVAL);
index c06b491c45d3f18060e57263802205fe1881d717..50b82725c611ae137aa4d672f286d023614bfbf8 100755 (executable)
@@ -10917,7 +10917,7 @@ my @reject_tests = (
                name => 'mtp2_on_mtp3',
                DLT => 'MTP3',
                expr => 'fisu',
-               errstr => 'supported only on MTP2',
+               errstr => '\'fisu\' supported only on SS7',
        },
        {
                name => 'mtp2_sio',