LCOV - code coverage report
Current view: top level - libbpfilter/cgen - stub.c (source / functions) Coverage Total Hit
Test: coverage.lcov Lines: 95.0 % 181 172
Test Date: 2026-04-17 15:45:04 Functions: 100.0 % 9 9
Branches: 46.2 % 312 144

             Branch data     Line data    Source code
       1                 :             : /* SPDX-License-Identifier: GPL-2.0-only */
       2                 :             : /*
       3                 :             :  * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
       4                 :             :  */
       5                 :             : 
       6                 :             : #include "cgen/stub.h"
       7                 :             : 
       8                 :             : #include <linux/bpf.h>
       9                 :             : #include <linux/bpf_common.h>
      10                 :             : #include <linux/icmp.h>
      11                 :             : #include <linux/icmpv6.h>
      12                 :             : #include <linux/if_ether.h>
      13                 :             : #include <linux/in.h> // NOLINT
      14                 :             : #include <linux/in6.h>
      15                 :             : #include <linux/ip.h>
      16                 :             : #include <linux/ipv6.h>
      17                 :             : #include <linux/tcp.h>
      18                 :             : #include <linux/udp.h>
      19                 :             : 
      20                 :             : #include <endian.h>
      21                 :             : #include <stddef.h>
      22                 :             : 
      23                 :             : #include <bpfilter/ctx.h>
      24                 :             : #include <bpfilter/elfstub.h>
      25                 :             : #include <bpfilter/flavor.h>
      26                 :             : #include <bpfilter/helper.h>
      27                 :             : #include <bpfilter/matcher.h>
      28                 :             : #include <bpfilter/verdict.h>
      29                 :             : 
      30                 :             : #include "cgen/jmp.h"
      31                 :             : #include "cgen/printer.h"
      32                 :             : #include "cgen/program.h"
      33                 :             : #include "cgen/swich.h"
      34                 :             : #include "filter.h"
      35                 :             : 
      36                 :             : #define _BF_LOW_EH_BITMASK 0x1801800000000801ULL
      37                 :             : 
      38                 :             : /**
      39                 :             :  * Generate stub to create a dynptr.
      40                 :             :  *
      41                 :             :  * @param program Program to generate the stub for. Must not be NULL.
      42                 :             :  * @param arg_reg Register where the first argument to the dynptr creation
      43                 :             :  *        function is located (SKB or xdp_md structure).
      44                 :             :  * @param kfunc Name of the kfunc to use to create the dynamic pointer.
      45                 :             :  * @return 0 on success, or negative errno value on error.
      46                 :             :  */
      47                 :        1140 : static int _bf_stub_make_ctx_dynptr(struct bf_program *program, int arg_reg,
      48                 :             :                                     const char *kfunc)
      49                 :             : {
      50                 :             :     int ret_code;
      51                 :             :     int r;
      52                 :             : 
      53                 :             :     assert(program);
      54                 :             :     assert(kfunc);
      55                 :             : 
      56                 :             :     // Call bpf_dynptr_from_xxx()
      57         [ -  + ]:        1140 :     if (arg_reg != BPF_REG_1)
      58         [ #  # ]:           0 :         EMIT(program, BPF_MOV64_IMM(BPF_REG_1, arg_reg));
      59         [ +  - ]:        1140 :     EMIT(program, BPF_MOV64_IMM(BPF_REG_2, 0));
      60         [ +  - ]:        1140 :     EMIT(program, BPF_MOV64_REG(BPF_REG_3, BPF_REG_10));
      61         [ +  - ]:        1140 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, BF_PROG_CTX_OFF(dynptr)));
      62         [ +  - ]:        1140 :     EMIT_KFUNC_CALL(program, kfunc);
      63                 :             : 
      64                 :             :     // If the function call failed, quit the program
      65                 :             :     {
      66                 :        2280 :         _clean_bf_jmpctx_ struct bf_jmpctx _ =
      67         [ +  - ]:        1140 :             bf_jmpctx_get(program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 0));
      68                 :             : 
      69                 :             :         // Update the error counter
      70         [ +  - ]:        1140 :         EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
      71         [ +  - ]:        1140 :         EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(arg)));
      72   [ +  -  +  - ]:        2280 :         EMIT_LOAD_COUNTERS_FD_FIXUP(program, BPF_REG_2);
      73         [ +  - ]:        1140 :         EMIT(program,
      74                 :             :              BPF_MOV32_IMM(BPF_REG_3, bf_program_error_counter_idx(program)));
      75         [ +  - ]:        1140 :         EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_UPDATE_COUNTERS);
      76                 :             : 
      77         [ -  + ]:        1140 :         if (bf_ctx_is_verbose(BF_VERBOSE_BPF))
      78   [ #  #  #  #  :           0 :             EMIT_PRINT(program, "failed to create a new dynamic pointer");
             #  #  #  # ]
      79                 :             : 
      80                 :        1140 :         r = program->runtime.ops->get_verdict(BF_VERDICT_ACCEPT, &ret_code);
      81         [ +  - ]:        1140 :         if (r)
      82                 :             :             return r;
      83                 :             : 
      84         [ +  - ]:        1140 :         EMIT(program, BPF_MOV64_IMM(BPF_REG_0, ret_code));
      85         [ +  - ]:        1140 :         EMIT(program, BPF_EXIT_INSN());
      86                 :             :     }
      87                 :             : 
      88                 :        1140 :     return 0;
      89                 :             : }
      90                 :             : 
      91                 :         162 : int bf_stub_make_ctx_xdp_dynptr(struct bf_program *program, int md_reg)
      92                 :             : {
      93                 :             :     assert(program);
      94                 :             : 
      95                 :         162 :     return _bf_stub_make_ctx_dynptr(program, md_reg, "bpf_dynptr_from_xdp");
      96                 :             : }
      97                 :             : 
      98                 :         978 : int bf_stub_make_ctx_skb_dynptr(struct bf_program *program, int skb_reg)
      99                 :             : {
     100                 :             :     assert(program);
     101                 :             : 
     102                 :         978 :     return _bf_stub_make_ctx_dynptr(program, skb_reg, "bpf_dynptr_from_skb");
     103                 :             : }
     104                 :             : 
     105                 :         406 : int bf_stub_parse_l2_ethhdr(struct bf_program *program)
     106                 :             : {
     107                 :             :     int ret_code;
     108                 :             :     int r;
     109                 :             : 
     110                 :             :     assert(program);
     111                 :             : 
     112                 :             :     // Call bpf_dynptr_slice()
     113         [ +  - ]:         406 :     EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     114         [ +  - ]:         406 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(dynptr)));
     115         [ +  - ]:         406 :     EMIT(program, BPF_MOV64_IMM(BPF_REG_2, 0));
     116         [ +  - ]:         406 :     EMIT(program, BPF_MOV64_REG(BPF_REG_3, BPF_REG_10));
     117         [ +  - ]:         406 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, BF_PROG_CTX_OFF(l2)));
     118         [ +  - ]:         406 :     EMIT(program, BPF_MOV64_IMM(BPF_REG_4, sizeof(struct ethhdr)));
     119                 :             : 
     120         [ +  - ]:         406 :     EMIT(program,
     121                 :             :          BPF_STX_MEM(BPF_B, BPF_REG_10, BPF_REG_4, BF_PROG_CTX_OFF(l2_size)));
     122                 :             : 
     123         [ +  - ]:         406 :     EMIT_KFUNC_CALL(program, "bpf_dynptr_slice");
     124                 :             : 
     125                 :             :     // If the function call failed, quit the program
     126                 :             :     {
     127                 :         812 :         _clean_bf_jmpctx_ struct bf_jmpctx _ =
     128         [ +  - ]:         406 :             bf_jmpctx_get(program, BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 0));
     129                 :             : 
     130                 :             :         // Update the error counter
     131         [ +  - ]:         406 :         EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     132         [ +  - ]:         406 :         EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(arg)));
     133   [ +  -  +  - ]:         812 :         EMIT_LOAD_COUNTERS_FD_FIXUP(program, BPF_REG_2);
     134         [ +  - ]:         406 :         EMIT(program,
     135                 :             :              BPF_MOV32_IMM(BPF_REG_3, bf_program_error_counter_idx(program)));
     136         [ +  - ]:         406 :         EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_UPDATE_COUNTERS);
     137                 :             : 
     138         [ -  + ]:         406 :         if (bf_ctx_is_verbose(BF_VERBOSE_BPF))
     139   [ #  #  #  #  :           0 :             EMIT_PRINT(program, "failed to create L2 dynamic pointer slice");
             #  #  #  # ]
     140                 :             : 
     141                 :         406 :         r = program->runtime.ops->get_verdict(BF_VERDICT_ACCEPT, &ret_code);
     142         [ +  - ]:         406 :         if (r)
     143                 :             :             return r;
     144                 :             : 
     145         [ +  - ]:         406 :         EMIT(program, BPF_MOV64_IMM(BPF_REG_0, ret_code));
     146         [ +  - ]:         406 :         EMIT(program, BPF_EXIT_INSN());
     147                 :             :     }
     148                 :             : 
     149                 :             :     // Store the L2 header address into the runtime context
     150         [ +  - ]:         406 :     EMIT(program,
     151                 :             :          BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, BF_PROG_CTX_OFF(l2_hdr)));
     152                 :             : 
     153                 :             :     // Store the L3 protocol ID in r7
     154         [ +  - ]:         406 :     EMIT(program, BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_0,
     155                 :             :                               offsetof(struct ethhdr, h_proto)));
     156                 :             : 
     157                 :             :     // Set bf_runtime.l3_offset
     158                 :         406 :     EMIT(program, BPF_ST_MEM(BPF_W, BPF_REG_10, BF_PROG_CTX_OFF(l3_offset),
     159                 :             :                              sizeof(struct ethhdr)));
     160                 :             : 
     161                 :             :     return 0;
     162                 :             : }
     163                 :             : 
     164                 :        1140 : int bf_stub_parse_l3_hdr(struct bf_program *program)
     165                 :             : {
     166                 :        1140 :     _clean_bf_jmpctx_ struct bf_jmpctx _ = bf_jmpctx_default();
     167                 :             :     int ret_code;
     168                 :             :     int r;
     169                 :             : 
     170                 :             :     assert(program);
     171                 :             : 
     172                 :             :     /* Store the size of the L3 protocol header in r4, depending on the protocol
     173                 :             :      * ID stored in r7. If the protocol is not supported, we store 0 into r7
     174                 :             :      * and we skip the instructions below. */
     175                 :             :     {
     176                 :        1140 :         _clean_bf_swich_ struct bf_swich swich =
     177         [ -  + ]:        1140 :             bf_swich_get(program, BPF_REG_7);
     178                 :             : 
     179         [ -  + ]:        1140 :         EMIT_SWICH_OPTION(&swich, htobe16(ETH_P_IP),
     180                 :             :                           BPF_MOV64_IMM(BPF_REG_4, sizeof(struct iphdr)));
     181         [ -  + ]:        1140 :         EMIT_SWICH_OPTION(&swich, htobe16(ETH_P_IPV6),
     182                 :             :                           BPF_MOV64_IMM(BPF_REG_4, sizeof(struct ipv6hdr)));
     183         [ -  + ]:        1140 :         EMIT_SWICH_DEFAULT(&swich, BPF_MOV64_IMM(BPF_REG_7, 0));
     184                 :             : 
     185                 :        1140 :         r = bf_swich_generate(&swich);
     186         [ +  - ]:        1140 :         if (r)
     187                 :             :             return r;
     188                 :             :     }
     189         [ +  - ]:        1140 :     _ = bf_jmpctx_get(program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_7, 0, 0));
     190                 :             : 
     191         [ +  - ]:        1140 :     EMIT(program,
     192                 :             :          BPF_STX_MEM(BPF_B, BPF_REG_10, BPF_REG_4, BF_PROG_CTX_OFF(l3_size)));
     193                 :             : 
     194                 :             :     // Call bpf_dynptr_slice()
     195         [ +  - ]:        1140 :     EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     196         [ +  - ]:        1140 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(dynptr)));
     197         [ +  - ]:        1140 :     EMIT(program,
     198                 :             :          BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_10, BF_PROG_CTX_OFF(l3_offset)));
     199         [ +  - ]:        1140 :     EMIT(program, BPF_MOV64_REG(BPF_REG_3, BPF_REG_10));
     200         [ +  - ]:        1140 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, BF_PROG_CTX_OFF(l2)));
     201         [ +  - ]:        1140 :     EMIT_KFUNC_CALL(program, "bpf_dynptr_slice");
     202                 :             : 
     203                 :             :     // If the function call failed, quit the program
     204                 :             :     {
     205                 :        2280 :         _clean_bf_jmpctx_ struct bf_jmpctx _ =
     206         [ +  - ]:        1140 :             bf_jmpctx_get(program, BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 0));
     207                 :             : 
     208         [ +  - ]:        1140 :         EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     209         [ +  - ]:        1140 :         EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(arg)));
     210   [ +  -  +  - ]:        2280 :         EMIT_LOAD_COUNTERS_FD_FIXUP(program, BPF_REG_2);
     211         [ +  - ]:        1140 :         EMIT(program,
     212                 :             :              BPF_MOV32_IMM(BPF_REG_3, bf_program_error_counter_idx(program)));
     213         [ +  - ]:        1140 :         EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_UPDATE_COUNTERS);
     214                 :             : 
     215         [ -  + ]:        1140 :         if (bf_ctx_is_verbose(BF_VERBOSE_BPF))
     216   [ #  #  #  #  :           0 :             EMIT_PRINT(program, "failed to create L3 dynamic pointer slice");
             #  #  #  # ]
     217                 :             : 
     218                 :        1140 :         r = program->runtime.ops->get_verdict(BF_VERDICT_ACCEPT, &ret_code);
     219         [ +  - ]:        1140 :         if (r)
     220                 :             :             return r;
     221                 :             : 
     222         [ +  - ]:        1140 :         EMIT(program, BPF_MOV64_IMM(BPF_REG_0, ret_code));
     223         [ +  - ]:        1140 :         EMIT(program, BPF_EXIT_INSN());
     224                 :             :     }
     225                 :             : 
     226                 :             :     // Store the L3 header address into the runtime context
     227         [ +  - ]:        1140 :     EMIT(program,
     228                 :             :          BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, BF_PROG_CTX_OFF(l3_hdr)));
     229                 :             : 
     230                 :             :     /* Unsupported L3 protocols have been filtered out at the beginning of this
     231                 :             :      * function and would jump over the block below, so there is no need to
     232                 :             :      * worry about them here. */
     233                 :             :     {
     234                 :             :         // IPv4
     235         [ +  - ]:        2280 :         _clean_bf_jmpctx_ struct bf_jmpctx _ = bf_jmpctx_get(
     236                 :             :             program, BPF_JMP_IMM(BPF_JNE, BPF_REG_7, htobe16(ETH_P_IP), 0));
     237                 :             : 
     238         [ +  - ]:        1140 :         EMIT(program, BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0));
     239         [ +  - ]:        1140 :         EMIT(program, BPF_ALU64_IMM(BPF_AND, BPF_REG_1, 0x0f));
     240         [ +  - ]:        1140 :         EMIT(program, BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 2));
     241         [ +  - ]:        1140 :         EMIT(program, BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_10,
     242                 :             :                                   BF_PROG_CTX_OFF(l3_offset)));
     243         [ +  - ]:        1140 :         EMIT(program, BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_2));
     244         [ +  - ]:        1140 :         EMIT(program, BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_1,
     245                 :             :                                   BF_PROG_CTX_OFF(l4_offset)));
     246         [ +  - ]:        1140 :         EMIT(program, BPF_LDX_MEM(BPF_B, BPF_REG_8, BPF_REG_0,
     247                 :             :                                   offsetof(struct iphdr, protocol)));
     248                 :             :     }
     249                 :             : 
     250                 :             :     {
     251                 :             :         // IPv6
     252                 :             :         struct bf_jmpctx tcpjmp, udpjmp, noehjmp, ehjmp;
     253                 :             :         struct bpf_insn ld64[2] = {BPF_LD_IMM64(BPF_REG_2, _BF_LOW_EH_BITMASK)};
     254         [ +  - ]:        2280 :         _clean_bf_jmpctx_ struct bf_jmpctx _ = bf_jmpctx_get(
     255                 :             :             program, BPF_JMP_IMM(BPF_JNE, BPF_REG_7, htobe16(ETH_P_IPV6), 0));
     256                 :             : 
     257         [ +  - ]:        1140 :         EMIT(program, BPF_LDX_MEM(BPF_B, BPF_REG_8, BPF_REG_0,
     258                 :             :                                   offsetof(struct ipv6hdr, nexthdr)));
     259                 :             : 
     260                 :             :         /* Fast path for TCP and UDP: quickly recognize the most used protocol
     261                 :             :          * to process them as fast as possible. */
     262         [ +  - ]:        1140 :         tcpjmp = bf_jmpctx_get(program,
     263                 :             :                                BPF_JMP_IMM(BPF_JEQ, BPF_REG_8, IPPROTO_TCP, 0));
     264         [ +  - ]:        1140 :         udpjmp = bf_jmpctx_get(program,
     265                 :             :                                BPF_JMP_IMM(BPF_JEQ, BPF_REG_8, IPPROTO_UDP, 0));
     266                 :             : 
     267                 :             :         /* For all the EH protocol numbers <64, use a bitmask:
     268                 :             :          * mask = (1<<0) | (1<<43) | (1<<44) | (1<<50) | (1<<51) | (1<<60)
     269                 :             :          *
     270                 :             :          * Pseudo-code:
     271                 :             :          * - r3 = 1 << r8 (nexthdr)
     272                 :             :          * - r3 = r3 & mask
     273                 :             :          * - if r3 != 0: go to slow path (EH present) */
     274         [ +  - ]:        1140 :         EMIT(program, ld64[0]);
     275         [ +  - ]:        1140 :         EMIT(program, ld64[1]);
     276         [ +  - ]:        1140 :         EMIT(program, BPF_JMP_IMM(BPF_JGE, BPF_REG_8, 64, 4));
     277         [ +  - ]:        1140 :         EMIT(program, BPF_MOV64_IMM(BPF_REG_3, 1));
     278         [ +  - ]:        1140 :         EMIT(program, BPF_ALU64_REG(BPF_LSH, BPF_REG_3, BPF_REG_8));
     279         [ +  - ]:        1140 :         EMIT(program, BPF_ALU64_REG(BPF_AND, BPF_REG_3, BPF_REG_2));
     280         [ +  - ]:        1140 :         EMIT(program, BPF_JMP_IMM(BPF_JNE, BPF_REG_3, 0, 4));
     281                 :             : 
     282                 :             :         // EH with protocol numbers >64 are processed individually
     283         [ +  - ]:        1140 :         EMIT(program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_8, 135, 3));
     284         [ +  - ]:        1140 :         EMIT(program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_8, 139, 2));
     285         [ +  - ]:        1140 :         EMIT(program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_8, 140, 1));
     286                 :             : 
     287                 :             :         // If no EH matched, nexthdr is L4, skip EH processing
     288         [ +  - ]:        1140 :         noehjmp = bf_jmpctx_get(program, BPF_JMP_A(0));
     289                 :             : 
     290                 :             :         // Process EH
     291         [ +  - ]:        1140 :         EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     292         [ +  - ]:        1140 :         EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(arg)));
     293                 :             :         // If any rule filters on ipv6.nexthdr, store the EH in the runtime context
     294                 :             :         // during process, so we won't have to process the EH again.
     295         [ +  + ]:        1140 :         if (program->runtime.chain->flags & BF_FLAG(BF_CHAIN_STORE_NEXTHDR))
     296         [ +  - ]:          30 :             EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_PARSE_IPV6_NH);
     297                 :             :         else
     298         [ +  - ]:        1110 :             EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_PARSE_IPV6_EH);
     299         [ +  - ]:        1140 :         EMIT(program, BPF_MOV64_REG(BPF_REG_8, BPF_REG_0));
     300                 :             : 
     301         [ +  - ]:        1140 :         ehjmp = bf_jmpctx_get(program, BPF_JMP_A(0));
     302                 :             : 
     303                 :             :         // If no EH found, all the jmp will end up here
     304                 :        1140 :         bf_jmpctx_cleanup(&tcpjmp);
     305                 :        1140 :         bf_jmpctx_cleanup(&udpjmp);
     306                 :        1140 :         bf_jmpctx_cleanup(&noehjmp);
     307                 :             : 
     308                 :             :         // Process IPv6 header, no EH (BPF_REG_8 already contains nexthdr)
     309         [ +  - ]:        1140 :         EMIT(program, BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_10,
     310                 :             :                                   BF_PROG_CTX_OFF(l3_offset)));
     311         [ +  - ]:        1140 :         EMIT(program,
     312                 :             :              BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, sizeof(struct ipv6hdr)));
     313         [ +  - ]:        1140 :         EMIT(program, BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2,
     314                 :             :                                   BF_PROG_CTX_OFF(l4_offset)));
     315                 :             : 
     316                 :        1140 :         bf_jmpctx_cleanup(&ehjmp);
     317                 :             :     }
     318                 :             : 
     319                 :        1140 :     return 0;
     320                 :             : }
     321                 :             : 
     322                 :        1140 : int bf_stub_parse_l4_hdr(struct bf_program *program)
     323                 :             : {
     324                 :        1140 :     _clean_bf_jmpctx_ struct bf_jmpctx _ = bf_jmpctx_default();
     325                 :             :     int ret_code;
     326                 :             :     int r;
     327                 :             : 
     328                 :             :     assert(program);
     329                 :             : 
     330                 :             :     /* Parse the L4 protocol and handle unuspported protocol, similarly to
     331                 :             :      * bf_stub_parse_l3_hdr() above. */
     332                 :             :     {
     333                 :        1140 :         _clean_bf_swich_ struct bf_swich swich =
     334         [ -  + ]:        1140 :             bf_swich_get(program, BPF_REG_8);
     335                 :             : 
     336         [ -  + ]:        1140 :         EMIT_SWICH_OPTION(&swich, IPPROTO_TCP,
     337                 :             :                           BPF_MOV64_IMM(BPF_REG_4, sizeof(struct tcphdr)));
     338         [ -  + ]:        1140 :         EMIT_SWICH_OPTION(&swich, IPPROTO_UDP,
     339                 :             :                           BPF_MOV64_IMM(BPF_REG_4, sizeof(struct udphdr)));
     340         [ -  + ]:        1140 :         EMIT_SWICH_OPTION(&swich, IPPROTO_ICMP,
     341                 :             :                           BPF_MOV64_IMM(BPF_REG_4, sizeof(struct icmphdr)));
     342         [ -  + ]:        1140 :         EMIT_SWICH_OPTION(&swich, IPPROTO_ICMPV6,
     343                 :             :                           BPF_MOV64_IMM(BPF_REG_4, sizeof(struct icmp6hdr)));
     344         [ -  + ]:        1140 :         EMIT_SWICH_DEFAULT(&swich, BPF_MOV64_IMM(BPF_REG_8, 0));
     345                 :             : 
     346                 :        1140 :         r = bf_swich_generate(&swich);
     347         [ +  - ]:        1140 :         if (r)
     348                 :             :             return r;
     349                 :             :     }
     350         [ +  - ]:        1140 :     _ = bf_jmpctx_get(program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_8, 0, 0));
     351                 :             : 
     352         [ +  - ]:        1140 :     EMIT(program,
     353                 :             :          BPF_STX_MEM(BPF_B, BPF_REG_10, BPF_REG_4, BF_PROG_CTX_OFF(l4_size)));
     354                 :             : 
     355                 :             :     // Call bpf_dynptr_slice()
     356         [ +  - ]:        1140 :     EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     357         [ +  - ]:        1140 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(dynptr)));
     358         [ +  - ]:        1140 :     EMIT(program,
     359                 :             :          BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_10, BF_PROG_CTX_OFF(l4_offset)));
     360         [ +  - ]:        1140 :     EMIT(program, BPF_MOV64_REG(BPF_REG_3, BPF_REG_10));
     361         [ +  - ]:        1140 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, BF_PROG_CTX_OFF(l4)));
     362         [ +  - ]:        1140 :     EMIT_KFUNC_CALL(program, "bpf_dynptr_slice");
     363                 :             : 
     364                 :             :     // If the function call failed, quit the program
     365                 :             :     {
     366                 :        2280 :         _clean_bf_jmpctx_ struct bf_jmpctx _ =
     367         [ +  - ]:        1140 :             bf_jmpctx_get(program, BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 0));
     368                 :             : 
     369         [ +  - ]:        1140 :         EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     370         [ +  - ]:        1140 :         EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(arg)));
     371   [ +  -  +  - ]:        2280 :         EMIT_LOAD_COUNTERS_FD_FIXUP(program, BPF_REG_2);
     372         [ +  - ]:        1140 :         EMIT(program,
     373                 :             :              BPF_MOV32_IMM(BPF_REG_3, bf_program_error_counter_idx(program)));
     374         [ +  - ]:        1140 :         EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_UPDATE_COUNTERS);
     375                 :             : 
     376         [ -  + ]:        1140 :         if (bf_ctx_is_verbose(BF_VERBOSE_BPF))
     377   [ #  #  #  #  :           0 :             EMIT_PRINT(program, "failed to create L4 dynamic pointer slice");
             #  #  #  # ]
     378                 :             : 
     379                 :        1140 :         r = program->runtime.ops->get_verdict(BF_VERDICT_ACCEPT, &ret_code);
     380         [ +  - ]:        1140 :         if (r)
     381                 :             :             return r;
     382                 :             : 
     383         [ +  - ]:        1140 :         EMIT(program, BPF_MOV64_IMM(BPF_REG_0, ret_code));
     384         [ +  - ]:        1140 :         EMIT(program, BPF_EXIT_INSN());
     385                 :             :     }
     386                 :             : 
     387                 :             :     // Store the L4 header address into the runtime context
     388                 :        1140 :     EMIT(program,
     389                 :             :          BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, BF_PROG_CTX_OFF(l4_hdr)));
     390                 :             : 
     391                 :             :     return 0;
     392                 :             : }
     393                 :             : 
     394                 :        1228 : int bf_stub_rule_check_protocol(struct bf_program *program,
     395                 :             :                                 const struct bf_matcher_meta *meta)
     396                 :             : {
     397                 :             :     assert(program);
     398                 :             :     assert(meta);
     399                 :             : 
     400      [ +  +  - ]:        1228 :     switch (meta->layer) {
     401                 :         784 :     case BF_MATCHER_LAYER_3:
     402         [ -  + ]:         784 :         EMIT_FIXUP_JMP_NEXT_RULE(
     403                 :             :             program, BPF_JMP_IMM(BPF_JNE, BPF_REG_7,
     404                 :             :                                  htobe16((uint16_t)meta->hdr_id), 0));
     405                 :         784 :         break;
     406                 :         444 :     case BF_MATCHER_LAYER_4:
     407         [ -  + ]:         444 :         EMIT_FIXUP_JMP_NEXT_RULE(
     408                 :             :             program, BPF_JMP_IMM(BPF_JNE, BPF_REG_8, (uint8_t)meta->hdr_id, 0));
     409                 :         444 :         break;
     410                 :           0 :     default:
     411         [ #  # ]:           0 :         return bf_err_r(-EINVAL, "rule can't check for layer ID %d",
     412                 :             :                         meta->layer);
     413                 :             :     }
     414                 :             : 
     415                 :             :     return 0;
     416                 :             : }
     417                 :             : 
     418                 :        1901 : int bf_stub_load_header(struct bf_program *program,
     419                 :             :                         const struct bf_matcher_meta *meta, int reg)
     420                 :             : {
     421                 :             :     assert(program);
     422                 :             :     assert(meta);
     423                 :             : 
     424      [ +  +  - ]:        1901 :     switch (meta->layer) {
     425                 :         915 :     case BF_MATCHER_LAYER_3:
     426         [ +  - ]:         915 :         EMIT(program,
     427                 :             :              BPF_LDX_MEM(BPF_DW, reg, BPF_REG_10, BF_PROG_CTX_OFF(l3_hdr)));
     428                 :             :         break;
     429                 :         986 :     case BF_MATCHER_LAYER_4:
     430         [ +  - ]:         986 :         EMIT(program,
     431                 :             :              BPF_LDX_MEM(BPF_DW, reg, BPF_REG_10, BF_PROG_CTX_OFF(l4_hdr)));
     432                 :             :         break;
     433                 :           0 :     default:
     434         [ #  # ]:           0 :         return bf_err_r(-EINVAL,
     435                 :             :                         "layer ID %d is not a valid layer to load header for",
     436                 :             :                         meta->layer);
     437                 :             :     }
     438                 :             : 
     439                 :             :     return 0;
     440                 :             : }
     441                 :             : 
     442                 :         244 : int bf_stub_stx_payload(struct bf_program *program,
     443                 :             :                         const struct bf_matcher_meta *meta, size_t offset)
     444                 :             : {
     445                 :             :     assert(program);
     446                 :             :     assert(meta);
     447                 :             : 
     448                 :         244 :     size_t remaining_size = meta->hdr_payload_size;
     449                 :             :     size_t src_offset = 0;
     450                 :             :     size_t dst_offset = offset;
     451                 :             : 
     452         [ +  + ]:         508 :     while (remaining_size) {
     453                 :             :         int bpf_size = BPF_B;
     454                 :             :         size_t copy_bytes = 1;
     455                 :             : 
     456   [ +  +  +  + ]:         264 :         if (BF_ALIGNED_64(offset) && remaining_size >= 8) {
     457                 :             :             bpf_size = BPF_DW;
     458                 :             :             copy_bytes = 8;
     459   [ +  -  +  + ]:         224 :         } else if (BF_ALIGNED_32(offset) && remaining_size >= 4) {
     460                 :             :             bpf_size = BPF_W;
     461                 :             :             copy_bytes = 4;
     462   [ -  +  +  + ]:         162 :         } else if (BF_ALIGNED_16(offset) && remaining_size >= 2) {
     463                 :             :             bpf_size = BPF_H;
     464                 :             :             copy_bytes = 2;
     465                 :             :         }
     466                 :             : 
     467         [ +  - ]:         264 :         EMIT(program, BPF_LDX_MEM(bpf_size, BPF_REG_1, BPF_REG_6,
     468                 :             :                                   meta->hdr_payload_offset + src_offset));
     469         [ +  - ]:         264 :         EMIT(program, BPF_STX_MEM(bpf_size, BPF_REG_10, BPF_REG_1,
     470                 :             :                                   BF_PROG_SCR_OFF(dst_offset)));
     471                 :             : 
     472                 :         264 :         remaining_size -= copy_bytes;
     473                 :         264 :         src_offset += copy_bytes;
     474                 :         264 :         dst_offset += copy_bytes;
     475                 :             :     }
     476                 :             : 
     477                 :             :     return 0;
     478                 :             : }
        

Generated by: LCOV version 2.0-1