LCOV - code coverage report
Current view: top level - libbpfilter/cgen - stub.c (source / functions) Coverage Total Hit
Test: coverage.lcov Lines: 97.2 % 214 208
Test Date: 2026-11-26 17:15:32 Functions: 100.0 % 11 11
Branches: 54.3 % 372 202

             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/codegen.h"
      31                 :             : #include "cgen/jmp.h"
      32                 :             : #include "cgen/printer.h"
      33                 :             : #include "cgen/swich.h"
      34                 :             : #include "filter.h"
      35                 :             : 
      36                 :             : #define _BF_LOW_EH_BITMASK                                                     \
      37                 :             :     (BF_FLAGS(IPPROTO_HOPOPTS, IPPROTO_ROUTING, IPPROTO_FRAGMENT, IPPROTO_ESP, \
      38                 :             :               IPPROTO_AH, IPPROTO_DSTOPTS))
      39                 :             : 
      40                 :             : /**
      41                 :             :  * Generate stub to create a dynptr.
      42                 :             :  *
      43                 :             :  * @param program Program to generate the stub for. Must not be NULL.
      44                 :             :  * @param arg_reg Register where the first argument to the dynptr creation
      45                 :             :  *        function is located (SKB or xdp_md structure).
      46                 :             :  * @param kfunc Name of the kfunc to use to create the dynamic pointer.
      47                 :             :  * @return 0 on success, or negative errno value on error.
      48                 :             :  */
      49                 :        1391 : static int _bf_stub_make_ctx_dynptr(struct bf_codegen *program, int arg_reg,
      50                 :             :                                     const char *kfunc)
      51                 :             : {
      52                 :             :     int ret_code;
      53                 :             :     int r;
      54                 :             : 
      55                 :             :     assert(program);
      56                 :             :     assert(kfunc);
      57                 :             : 
      58                 :             :     // Call bpf_dynptr_from_xxx()
      59         [ -  + ]:        1391 :     if (arg_reg != BPF_REG_1)
      60         [ #  # ]:           0 :         EMIT(program, BPF_MOV64_IMM(BPF_REG_1, arg_reg));
      61         [ +  - ]:        1391 :     EMIT(program, BPF_MOV64_IMM(BPF_REG_2, 0));
      62         [ +  - ]:        1391 :     EMIT(program, BPF_MOV64_REG(BPF_REG_3, BPF_REG_10));
      63         [ +  - ]:        1391 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, BF_PROG_CTX_OFF(dynptr)));
      64         [ +  - ]:        1391 :     EMIT_KFUNC_CALL(program, kfunc);
      65                 :             : 
      66                 :             :     // If the function call failed, quit the program
      67                 :             :     {
      68                 :        2782 :         _clean_bf_jmpctx_ struct bf_jmpctx _ =
      69         [ +  - ]:        1391 :             bf_jmpctx_get(program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 0));
      70                 :             : 
      71                 :             :         // Update the error counter
      72         [ +  - ]:        1391 :         EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
      73         [ +  - ]:        1391 :         EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(arg)));
      74   [ +  -  +  - ]:        2782 :         EMIT_LOAD_COUNTERS_FD_FIXUP(program, BPF_REG_2);
      75         [ +  - ]:        1391 :         EMIT(program,
      76                 :             :              BPF_MOV32_IMM(BPF_REG_3, bf_codegen_error_counter_idx(program)));
      77         [ +  - ]:        1391 :         EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_UPDATE_COUNTERS);
      78                 :             : 
      79         [ +  + ]:        1391 :         if (bf_ctx_is_verbose(BF_VERBOSE_BPF))
      80   [ +  -  +  -  :           6 :             EMIT_PRINT(program, "failed to create a new dynamic pointer");
             +  -  +  - ]
      81                 :             : 
      82                 :        1391 :         r = program->runtime.ops->get_verdict(BF_VERDICT_ACCEPT, &ret_code);
      83         [ +  - ]:        1391 :         if (r)
      84                 :             :             return r;
      85                 :             : 
      86         [ +  - ]:        1391 :         EMIT(program, BPF_MOV64_IMM(BPF_REG_0, ret_code));
      87         [ +  - ]:        1391 :         EMIT(program, BPF_EXIT_INSN());
      88                 :             :     }
      89                 :             : 
      90                 :        1391 :     return 0;
      91                 :             : }
      92                 :             : 
      93                 :         222 : int bf_stub_make_ctx_xdp_dynptr(struct bf_codegen *program, int md_reg)
      94                 :             : {
      95                 :             :     assert(program);
      96                 :             : 
      97                 :         222 :     return _bf_stub_make_ctx_dynptr(program, md_reg, "bpf_dynptr_from_xdp");
      98                 :             : }
      99                 :             : 
     100                 :        1169 : int bf_stub_make_ctx_skb_dynptr(struct bf_codegen *program, int skb_reg)
     101                 :             : {
     102                 :             :     assert(program);
     103                 :             : 
     104                 :        1169 :     return _bf_stub_make_ctx_dynptr(program, skb_reg, "bpf_dynptr_from_skb");
     105                 :             : }
     106                 :             : 
     107                 :         515 : int bf_stub_parse_l2_ethhdr(struct bf_codegen *program)
     108                 :             : {
     109                 :             :     struct bf_jmpctx ipv4;
     110                 :             :     struct bf_jmpctx ipv6;
     111                 :             :     int ret_code;
     112                 :             :     int r;
     113                 :             : 
     114                 :             :     assert(program);
     115                 :             : 
     116                 :             :     // Call bpf_dynptr_slice()
     117         [ +  - ]:         515 :     EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     118         [ +  - ]:         515 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(dynptr)));
     119         [ +  - ]:         515 :     EMIT(program, BPF_MOV64_IMM(BPF_REG_2, 0));
     120         [ +  - ]:         515 :     EMIT(program, BPF_MOV64_REG(BPF_REG_3, BPF_REG_10));
     121         [ +  - ]:         515 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, BF_PROG_CTX_OFF(l2)));
     122         [ +  - ]:         515 :     EMIT(program, BPF_MOV64_IMM(BPF_REG_4, sizeof(struct ethhdr)));
     123                 :             : 
     124         [ +  - ]:         515 :     EMIT(program,
     125                 :             :          BPF_STX_MEM(BPF_B, BPF_REG_10, BPF_REG_4, BF_PROG_CTX_OFF(l2_size)));
     126                 :             : 
     127         [ +  - ]:         515 :     EMIT_KFUNC_CALL(program, "bpf_dynptr_slice");
     128                 :             : 
     129                 :             :     // If the function call failed, quit the program
     130                 :             :     {
     131                 :        1030 :         _clean_bf_jmpctx_ struct bf_jmpctx _ =
     132         [ +  - ]:         515 :             bf_jmpctx_get(program, BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 0));
     133                 :             : 
     134                 :             :         // Update the error counter
     135         [ +  - ]:         515 :         EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     136         [ +  - ]:         515 :         EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(arg)));
     137   [ +  -  +  - ]:        1030 :         EMIT_LOAD_COUNTERS_FD_FIXUP(program, BPF_REG_2);
     138         [ +  - ]:         515 :         EMIT(program,
     139                 :             :              BPF_MOV32_IMM(BPF_REG_3, bf_codegen_error_counter_idx(program)));
     140         [ +  - ]:         515 :         EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_UPDATE_COUNTERS);
     141                 :             : 
     142         [ +  + ]:         515 :         if (bf_ctx_is_verbose(BF_VERBOSE_BPF))
     143   [ +  -  +  -  :           6 :             EMIT_PRINT(program, "failed to create L2 dynamic pointer slice");
             +  -  +  - ]
     144                 :             : 
     145                 :         515 :         r = program->runtime.ops->get_verdict(BF_VERDICT_ACCEPT, &ret_code);
     146         [ +  - ]:         515 :         if (r)
     147                 :             :             return r;
     148                 :             : 
     149         [ +  - ]:         515 :         EMIT(program, BPF_MOV64_IMM(BPF_REG_0, ret_code));
     150         [ +  - ]:         515 :         EMIT(program, BPF_EXIT_INSN());
     151                 :             :     }
     152                 :             : 
     153                 :             :     // Store the L2 header address into the runtime context
     154         [ +  - ]:         515 :     EMIT(program,
     155                 :             :          BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, BF_PROG_CTX_OFF(l2_hdr)));
     156                 :             : 
     157                 :             :     // Store the L3 protocol ID in r7
     158         [ +  - ]:         515 :     EMIT(program, BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_0,
     159                 :             :                               offsetof(struct ethhdr, h_proto)));
     160                 :             : 
     161                 :             :     // Set bf_runtime.l3_offset
     162         [ +  - ]:         515 :     EMIT(program, BPF_ST_MEM(BPF_W, BPF_REG_10, BF_PROG_CTX_OFF(l3_offset),
     163                 :             :                              sizeof(struct ethhdr)));
     164                 :             : 
     165                 :             :     // Send untagged IP directly to L3 parsing.
     166         [ +  - ]:         515 :     ipv4 = bf_jmpctx_get(program,
     167                 :             :                          BPF_JMP_IMM(BPF_JEQ, BPF_REG_7, htobe16(ETH_P_IP), 0));
     168         [ +  - ]:         515 :     ipv6 = bf_jmpctx_get(
     169                 :             :         program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_7, htobe16(ETH_P_IPV6), 0));
     170                 :             : 
     171         [ +  - ]:         515 :     EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     172         [ +  - ]:         515 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(arg)));
     173         [ +  - ]:         515 :     EMIT(program, BPF_MOV64_REG(BPF_REG_2, BPF_REG_7));
     174         [ +  - ]:         515 :     EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_PARSE_L2);
     175                 :             : 
     176                 :             :     {
     177                 :        1030 :         _clean_bf_jmpctx_ struct bf_jmpctx _ =
     178         [ +  - ]:         515 :             bf_jmpctx_get(program, BPF_JMP32_IMM(BPF_JNE, BPF_REG_0, -1, 0));
     179                 :             : 
     180         [ +  - ]:         515 :         EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     181         [ +  - ]:         515 :         EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(arg)));
     182   [ +  -  +  - ]:        1030 :         EMIT_LOAD_COUNTERS_FD_FIXUP(program, BPF_REG_2);
     183         [ +  - ]:         515 :         EMIT(program,
     184                 :             :              BPF_MOV32_IMM(BPF_REG_3,
     185                 :             :                            bf_codegen_parser_drop_counter_idx(program)));
     186         [ +  - ]:         515 :         EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_UPDATE_COUNTERS);
     187                 :             : 
     188                 :         515 :         r = program->runtime.ops->get_verdict(BF_VERDICT_DROP, &ret_code);
     189         [ +  - ]:         515 :         if (r)
     190                 :             :             return r;
     191                 :             : 
     192         [ +  - ]:         515 :         EMIT(program, BPF_MOV64_IMM(BPF_REG_0, ret_code));
     193         [ +  - ]:         515 :         EMIT(program, BPF_EXIT_INSN());
     194                 :             :     }
     195                 :             : 
     196         [ +  - ]:         515 :     EMIT(program, BPF_MOV32_REG(BPF_REG_7, BPF_REG_0));
     197                 :         515 :     bf_jmpctx_cleanup(&ipv4);
     198                 :         515 :     bf_jmpctx_cleanup(&ipv6);
     199                 :             : 
     200                 :         515 :     return 0;
     201                 :             : }
     202                 :             : 
     203                 :        1391 : int bf_stub_parse_l3_hdr(struct bf_codegen *program)
     204                 :             : {
     205                 :        1391 :     _clean_bf_jmpctx_ struct bf_jmpctx _ = bf_jmpctx_default();
     206                 :             :     int ret_code;
     207                 :             :     int r;
     208                 :             : 
     209                 :             :     assert(program);
     210                 :             : 
     211                 :             :     /* Store the size of the L3 protocol header in r4, depending on the protocol
     212                 :             :      * ID stored in r7. If the protocol is not supported, we store 0 into r7
     213                 :             :      * and we skip the instructions below. */
     214                 :             :     {
     215                 :        1391 :         _clean_bf_swich_ struct bf_swich swich =
     216         [ -  + ]:        1391 :             bf_swich_get(program, BPF_REG_7);
     217                 :             : 
     218         [ -  + ]:        1391 :         EMIT_SWICH_OPTION(&swich, htobe16(ETH_P_IP),
     219                 :             :                           BPF_MOV64_IMM(BPF_REG_4, sizeof(struct iphdr)));
     220         [ -  + ]:        1391 :         EMIT_SWICH_OPTION(&swich, htobe16(ETH_P_IPV6),
     221                 :             :                           BPF_MOV64_IMM(BPF_REG_4, sizeof(struct ipv6hdr)));
     222         [ -  + ]:        1391 :         EMIT_SWICH_DEFAULT(&swich, BPF_MOV64_IMM(BPF_REG_7, 0));
     223                 :             : 
     224                 :        1391 :         r = bf_swich_generate(&swich);
     225         [ +  - ]:        1391 :         if (r)
     226                 :             :             return r;
     227                 :             :     }
     228         [ +  - ]:        1391 :     _ = bf_jmpctx_get(program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_7, 0, 0));
     229                 :             : 
     230         [ +  - ]:        1391 :     EMIT(program,
     231                 :             :          BPF_STX_MEM(BPF_B, BPF_REG_10, BPF_REG_4, BF_PROG_CTX_OFF(l3_size)));
     232                 :             : 
     233                 :             :     // Call bpf_dynptr_slice()
     234         [ +  - ]:        1391 :     EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     235         [ +  - ]:        1391 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(dynptr)));
     236         [ +  - ]:        1391 :     EMIT(program,
     237                 :             :          BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_10, BF_PROG_CTX_OFF(l3_offset)));
     238         [ +  - ]:        1391 :     EMIT(program, BPF_MOV64_REG(BPF_REG_3, BPF_REG_10));
     239         [ +  - ]:        1391 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, BF_PROG_CTX_OFF(l2)));
     240         [ +  - ]:        1391 :     EMIT_KFUNC_CALL(program, "bpf_dynptr_slice");
     241                 :             : 
     242                 :             :     // If the function call failed, quit the program
     243                 :             :     {
     244                 :        2782 :         _clean_bf_jmpctx_ struct bf_jmpctx _ =
     245         [ +  - ]:        1391 :             bf_jmpctx_get(program, BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 0));
     246                 :             : 
     247         [ +  - ]:        1391 :         EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     248         [ +  - ]:        1391 :         EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(arg)));
     249   [ +  -  +  - ]:        2782 :         EMIT_LOAD_COUNTERS_FD_FIXUP(program, BPF_REG_2);
     250         [ +  - ]:        1391 :         EMIT(program,
     251                 :             :              BPF_MOV32_IMM(BPF_REG_3, bf_codegen_error_counter_idx(program)));
     252         [ +  - ]:        1391 :         EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_UPDATE_COUNTERS);
     253                 :             : 
     254         [ +  + ]:        1391 :         if (bf_ctx_is_verbose(BF_VERBOSE_BPF))
     255   [ +  -  +  -  :           6 :             EMIT_PRINT(program, "failed to create L3 dynamic pointer slice");
             +  -  +  - ]
     256                 :             : 
     257                 :        1391 :         r = program->runtime.ops->get_verdict(BF_VERDICT_ACCEPT, &ret_code);
     258         [ +  - ]:        1391 :         if (r)
     259                 :             :             return r;
     260                 :             : 
     261         [ +  - ]:        1391 :         EMIT(program, BPF_MOV64_IMM(BPF_REG_0, ret_code));
     262         [ +  - ]:        1391 :         EMIT(program, BPF_EXIT_INSN());
     263                 :             :     }
     264                 :             : 
     265                 :             :     // Store the L3 header address into the runtime context
     266         [ +  - ]:        1391 :     EMIT(program,
     267                 :             :          BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, BF_PROG_CTX_OFF(l3_hdr)));
     268                 :             : 
     269                 :             :     /* Unsupported L3 protocols have been filtered out at the beginning of this
     270                 :             :      * function and would jump over the block below, so there is no need to
     271                 :             :      * worry about them here. */
     272                 :             :     {
     273                 :             :         // IPv4
     274         [ +  - ]:        2782 :         _clean_bf_jmpctx_ struct bf_jmpctx _ = bf_jmpctx_get(
     275                 :             :             program, BPF_JMP_IMM(BPF_JNE, BPF_REG_7, htobe16(ETH_P_IP), 0));
     276                 :             : 
     277         [ +  - ]:        1391 :         EMIT(program, BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0));
     278         [ +  - ]:        1391 :         EMIT(program, BPF_ALU64_IMM(BPF_AND, BPF_REG_1, 0x0f));
     279         [ +  - ]:        1391 :         EMIT(program, BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 2));
     280         [ +  - ]:        1391 :         EMIT(program, BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_10,
     281                 :             :                                   BF_PROG_CTX_OFF(l3_offset)));
     282         [ +  - ]:        1391 :         EMIT(program, BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_2));
     283         [ +  - ]:        1391 :         EMIT(program, BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_1,
     284                 :             :                                   BF_PROG_CTX_OFF(l4_offset)));
     285         [ +  - ]:        1391 :         EMIT(program, BPF_LDX_MEM(BPF_B, BPF_REG_8, BPF_REG_0,
     286                 :             :                                   offsetof(struct iphdr, protocol)));
     287                 :             :     }
     288                 :             : 
     289                 :             :     {
     290                 :             :         // IPv6
     291                 :             :         struct bf_jmpctx tcpjmp, udpjmp, noehjmp, ehjmp;
     292                 :             :         struct bpf_insn ld64[2] = {BPF_LD_IMM64(BPF_REG_2, _BF_LOW_EH_BITMASK)};
     293         [ +  - ]:        2782 :         _clean_bf_jmpctx_ struct bf_jmpctx _ = bf_jmpctx_get(
     294                 :             :             program, BPF_JMP_IMM(BPF_JNE, BPF_REG_7, htobe16(ETH_P_IPV6), 0));
     295                 :             : 
     296         [ +  - ]:        1391 :         EMIT(program, BPF_LDX_MEM(BPF_B, BPF_REG_8, BPF_REG_0,
     297                 :             :                                   offsetof(struct ipv6hdr, nexthdr)));
     298                 :             : 
     299                 :             :         /* Fast path for TCP and UDP: quickly recognize the most used protocol
     300                 :             :          * to process them as fast as possible. */
     301         [ +  - ]:        1391 :         tcpjmp = bf_jmpctx_get(program,
     302                 :             :                                BPF_JMP_IMM(BPF_JEQ, BPF_REG_8, IPPROTO_TCP, 0));
     303         [ +  - ]:        1391 :         udpjmp = bf_jmpctx_get(program,
     304                 :             :                                BPF_JMP_IMM(BPF_JEQ, BPF_REG_8, IPPROTO_UDP, 0));
     305                 :             : 
     306                 :             :         /* For all the EH protocol numbers <64, use a bitmask:
     307                 :             :          *
     308                 :             :          * Pseudo-code:
     309                 :             :          * - r3 = 1 << r8 (nexthdr)
     310                 :             :          * - r3 = r3 & mask
     311                 :             :          * - if r3 != 0: go to slow path (EH present) */
     312         [ +  - ]:        1391 :         EMIT(program, ld64[0]);
     313         [ +  - ]:        1391 :         EMIT(program, ld64[1]);
     314         [ +  - ]:        1391 :         EMIT(program, BPF_JMP_IMM(BPF_JGE, BPF_REG_8, 64, 4));
     315         [ +  - ]:        1391 :         EMIT(program, BPF_MOV64_IMM(BPF_REG_3, 1));
     316         [ +  - ]:        1391 :         EMIT(program, BPF_ALU64_REG(BPF_LSH, BPF_REG_3, BPF_REG_8));
     317         [ +  - ]:        1391 :         EMIT(program, BPF_ALU64_REG(BPF_AND, BPF_REG_3, BPF_REG_2));
     318         [ +  - ]:        1391 :         EMIT(program, BPF_JMP_IMM(BPF_JNE, BPF_REG_3, 0, 4));
     319                 :             : 
     320                 :             :         // EH with protocol numbers >64 are processed individually
     321         [ +  - ]:        1391 :         EMIT(program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_8, 135, 3));
     322         [ +  - ]:        1391 :         EMIT(program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_8, 139, 2));
     323         [ +  - ]:        1391 :         EMIT(program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_8, 140, 1));
     324                 :             : 
     325                 :             :         // If no EH matched, nexthdr is L4, skip EH processing
     326         [ +  - ]:        1391 :         noehjmp = bf_jmpctx_get(program, BPF_JMP_A(0));
     327                 :             : 
     328                 :             :         // Process EH
     329         [ +  - ]:        1391 :         EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     330         [ +  - ]:        1391 :         EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(arg)));
     331                 :             :         // If any rule filters on ipv6.nexthdr, store the EH in the runtime context
     332                 :             :         // during process, so we won't have to process the EH again.
     333         [ +  + ]:        1391 :         if (program->runtime.chain->flags & BF_FLAG(BF_CHAIN_STORE_NEXTHDR))
     334         [ +  - ]:          30 :             EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_PARSE_IPV6_NH);
     335                 :             :         else
     336         [ +  - ]:        1361 :             EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_PARSE_IPV6_EH);
     337         [ +  - ]:        1391 :         EMIT(program, BPF_MOV64_REG(BPF_REG_8, BPF_REG_0));
     338                 :             : 
     339         [ +  - ]:        1391 :         ehjmp = bf_jmpctx_get(program, BPF_JMP_A(0));
     340                 :             : 
     341                 :             :         // If no EH found, all the jmp will end up here
     342                 :        1391 :         bf_jmpctx_cleanup(&tcpjmp);
     343                 :        1391 :         bf_jmpctx_cleanup(&udpjmp);
     344                 :        1391 :         bf_jmpctx_cleanup(&noehjmp);
     345                 :             : 
     346                 :             :         // Process IPv6 header, no EH (BPF_REG_8 already contains nexthdr)
     347         [ +  - ]:        1391 :         EMIT(program, BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_10,
     348                 :             :                                   BF_PROG_CTX_OFF(l3_offset)));
     349         [ +  - ]:        1391 :         EMIT(program,
     350                 :             :              BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, sizeof(struct ipv6hdr)));
     351         [ +  - ]:        1391 :         EMIT(program, BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2,
     352                 :             :                                   BF_PROG_CTX_OFF(l4_offset)));
     353                 :             : 
     354                 :        1391 :         bf_jmpctx_cleanup(&ehjmp);
     355                 :             :     }
     356                 :             : 
     357                 :        1391 :     return 0;
     358                 :             : }
     359                 :             : 
     360                 :        1391 : int bf_stub_parse_l4_hdr(struct bf_codegen *program)
     361                 :             : {
     362                 :        1391 :     _clean_bf_jmpctx_ struct bf_jmpctx _ = bf_jmpctx_default();
     363                 :             :     int r;
     364                 :             : 
     365                 :             :     assert(program);
     366                 :             : 
     367                 :             :     /* Parse the L4 protocol and handle unuspported protocol, similarly to
     368                 :             :      * bf_stub_parse_l3_hdr() above. */
     369                 :             :     {
     370                 :        1391 :         _clean_bf_swich_ struct bf_swich swich =
     371         [ -  + ]:        1391 :             bf_swich_get(program, BPF_REG_8);
     372                 :             : 
     373         [ -  + ]:        1391 :         EMIT_SWICH_OPTION(&swich, IPPROTO_TCP,
     374                 :             :                           BPF_MOV64_IMM(BPF_REG_4, sizeof(struct tcphdr)));
     375         [ -  + ]:        1391 :         EMIT_SWICH_OPTION(&swich, IPPROTO_UDP,
     376                 :             :                           BPF_MOV64_IMM(BPF_REG_4, sizeof(struct udphdr)));
     377         [ -  + ]:        1391 :         EMIT_SWICH_OPTION(&swich, IPPROTO_ICMP,
     378                 :             :                           BPF_MOV64_IMM(BPF_REG_4, sizeof(struct icmphdr)));
     379         [ -  + ]:        1391 :         EMIT_SWICH_OPTION(&swich, IPPROTO_ICMPV6,
     380                 :             :                           BPF_MOV64_IMM(BPF_REG_4, sizeof(struct icmp6hdr)));
     381         [ -  + ]:        1391 :         EMIT_SWICH_DEFAULT(&swich, BPF_MOV64_IMM(BPF_REG_8, 0));
     382                 :             : 
     383                 :        1391 :         r = bf_swich_generate(&swich);
     384         [ +  - ]:        1391 :         if (r)
     385                 :             :             return r;
     386                 :             :     }
     387         [ +  - ]:        1391 :     _ = bf_jmpctx_get(program, BPF_JMP_IMM(BPF_JEQ, BPF_REG_8, 0, 0));
     388                 :             : 
     389         [ +  - ]:        1391 :     EMIT(program,
     390                 :             :          BPF_STX_MEM(BPF_B, BPF_REG_10, BPF_REG_4, BF_PROG_CTX_OFF(l4_size)));
     391                 :             : 
     392                 :             :     // Call bpf_dynptr_slice()
     393         [ +  - ]:        1391 :     EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     394         [ +  - ]:        1391 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(dynptr)));
     395         [ +  - ]:        1391 :     EMIT(program,
     396                 :             :          BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_10, BF_PROG_CTX_OFF(l4_offset)));
     397         [ +  - ]:        1391 :     EMIT(program, BPF_MOV64_REG(BPF_REG_3, BPF_REG_10));
     398         [ +  - ]:        1391 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, BF_PROG_CTX_OFF(l4)));
     399         [ +  - ]:        1391 :     EMIT_KFUNC_CALL(program, "bpf_dynptr_slice");
     400                 :             : 
     401                 :             :     /* If the function call failed (e.g. the packet is too short), continue as
     402                 :             :      * if the L4 protocol was not supported: the rules and the chain policy
     403                 :             :      * still apply. */
     404                 :             :     {
     405                 :        2782 :         _clean_bf_jmpctx_ struct bf_jmpctx _ =
     406         [ +  - ]:        1391 :             bf_jmpctx_get(program, BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 0));
     407                 :             : 
     408         [ +  - ]:        1391 :         EMIT(program, BPF_MOV64_REG(BPF_REG_1, BPF_REG_10));
     409         [ +  - ]:        1391 :         EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, BF_PROG_CTX_OFF(arg)));
     410   [ +  -  +  - ]:        2782 :         EMIT_LOAD_COUNTERS_FD_FIXUP(program, BPF_REG_2);
     411         [ +  - ]:        1391 :         EMIT(program,
     412                 :             :              BPF_MOV32_IMM(BPF_REG_3, bf_codegen_error_counter_idx(program)));
     413         [ +  - ]:        1391 :         EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_UPDATE_COUNTERS);
     414                 :             : 
     415         [ +  + ]:        1391 :         if (bf_ctx_is_verbose(BF_VERBOSE_BPF))
     416   [ +  -  +  -  :           6 :             EMIT_PRINT(program, "failed to create L4 dynamic pointer slice");
             +  -  +  - ]
     417                 :             : 
     418                 :             :         /* No L4 protocol (r8 = 0) and no L4 header (r0 = NULL, the calls
     419                 :             :          * above overwrote r0). */
     420         [ +  - ]:        1391 :         EMIT(program, BPF_MOV64_IMM(BPF_REG_8, 0));
     421         [ +  - ]:        1391 :         EMIT(program, BPF_MOV64_IMM(BPF_REG_0, 0));
     422                 :             :     }
     423                 :             : 
     424                 :             :     // Store the L4 header address (NULL on failure) into the runtime context
     425                 :        1391 :     EMIT(program,
     426                 :             :          BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, BF_PROG_CTX_OFF(l4_hdr)));
     427                 :             : 
     428                 :             :     return 0;
     429                 :             : }
     430                 :             : 
     431                 :        1316 : int bf_stub_rule_check_protocol(struct bf_codegen *program,
     432                 :             :                                 const struct bf_matcher_meta *meta)
     433                 :             : {
     434                 :             :     assert(program);
     435                 :             :     assert(meta);
     436                 :             : 
     437      [ +  +  - ]:        1316 :     switch (meta->layer) {
     438                 :         845 :     case BF_MATCHER_LAYER_3:
     439         [ -  + ]:         845 :         EMIT_FIXUP_JMP_NEXT_RULE(
     440                 :             :             program, BPF_JMP_IMM(BPF_JNE, BPF_REG_7,
     441                 :             :                                  htobe16((uint16_t)meta->hdr_id), 0));
     442                 :         845 :         break;
     443                 :         471 :     case BF_MATCHER_LAYER_4:
     444         [ -  + ]:         471 :         EMIT_FIXUP_JMP_NEXT_RULE(
     445                 :             :             program, BPF_JMP_IMM(BPF_JNE, BPF_REG_8, (uint8_t)meta->hdr_id, 0));
     446                 :         471 :         break;
     447                 :           0 :     default:
     448         [ #  # ]:           0 :         return bf_err_r(-EINVAL, "rule can't check for layer ID %d",
     449                 :             :                         meta->layer);
     450                 :             :     }
     451                 :             : 
     452                 :             :     return 0;
     453                 :             : }
     454                 :             : 
     455                 :        2014 : int bf_stub_load_header(struct bf_codegen *program,
     456                 :             :                         const struct bf_matcher_meta *meta, int reg)
     457                 :             : {
     458                 :             :     assert(program);
     459                 :             :     assert(meta);
     460                 :             : 
     461      [ +  +  - ]:        2014 :     switch (meta->layer) {
     462                 :         964 :     case BF_MATCHER_LAYER_3:
     463                 :         964 :         EMIT(program,
     464                 :             :              BPF_LDX_MEM(BPF_DW, reg, BPF_REG_10, BF_PROG_CTX_OFF(l3_hdr)));
     465                 :             :         break;
     466                 :        1050 :     case BF_MATCHER_LAYER_4:
     467                 :        1050 :         EMIT(program,
     468                 :             :              BPF_LDX_MEM(BPF_DW, reg, BPF_REG_10, BF_PROG_CTX_OFF(l4_hdr)));
     469                 :             :         break;
     470                 :           0 :     default:
     471         [ #  # ]:           0 :         return bf_err_r(-EINVAL,
     472                 :             :                         "layer ID %d is not a valid layer to load header for",
     473                 :             :                         meta->layer);
     474                 :             :     }
     475                 :             : 
     476                 :             :     return 0;
     477                 :             : }
     478                 :             : 
     479                 :         382 : int bf_stub_load(struct bf_codegen *program, size_t src_offset, size_t size,
     480                 :             :                  int dst_offset)
     481                 :             : {
     482                 :             :     size_t src_off = src_offset;
     483                 :             :     int dst_off = dst_offset;
     484                 :             :     size_t remaining_size = size;
     485                 :             : 
     486                 :             :     assert(program);
     487                 :             : 
     488         [ +  + ]:         826 :     while (remaining_size) {
     489                 :             :         int bpf_size = BPF_B;
     490                 :             :         size_t copy_bytes = 1;
     491                 :             : 
     492   [ +  +  +  +  :         444 :         if (BF_ALIGNED_64(src_off) && BF_ALIGNED_64(dst_off) &&
                   +  + ]
     493                 :             :             remaining_size >= 8) {
     494                 :             :             bpf_size = BPF_DW;
     495                 :             :             copy_bytes = 8;
     496   [ +  +  +  -  :         326 :         } else if (BF_ALIGNED_32(src_off) && BF_ALIGNED_32(dst_off) &&
                   +  + ]
     497                 :             :                    remaining_size >= 4) {
     498                 :             :             bpf_size = BPF_W;
     499                 :             :             copy_bytes = 4;
     500   [ +  +  -  +  :         202 :         } else if (BF_ALIGNED_16(src_off) && BF_ALIGNED_16(dst_off) &&
                   +  + ]
     501                 :             :                    remaining_size >= 2) {
     502                 :             :             bpf_size = BPF_H;
     503                 :             :             copy_bytes = 2;
     504                 :             :         }
     505                 :             : 
     506         [ +  - ]:         444 :         EMIT(program, BPF_LDX_MEM(bpf_size, BPF_REG_1, BPF_REG_6, src_off));
     507         [ +  - ]:         444 :         EMIT(program, BPF_STX_MEM(bpf_size, BPF_REG_10, BPF_REG_1, dst_off));
     508                 :             : 
     509                 :         444 :         remaining_size -= copy_bytes;
     510                 :         444 :         src_off += copy_bytes;
     511                 :         444 :         dst_off += (int)copy_bytes;
     512                 :             :     }
     513                 :             : 
     514                 :             :     return 0;
     515                 :             : }
     516                 :             : 
     517                 :           6 : int bf_stub_store(struct bf_codegen *program, int reg, size_t size,
     518                 :             :                   int dst_offset)
     519                 :             : {
     520                 :             :     int dst_off = dst_offset;
     521                 :             :     size_t remaining_size = size;
     522                 :             : 
     523                 :             :     assert(program);
     524                 :             : 
     525         [ -  + ]:           6 :     if (size > 8)
     526         [ #  # ]:           0 :         return bf_err_r(-E2BIG, "bf_stub_store copies at most 8 bytes");
     527                 :             : 
     528         [ +  - ]:           9 :     while (remaining_size) {
     529                 :             :         int bpf_size = BPF_B;
     530                 :             :         size_t copy_bytes = 1;
     531                 :             : 
     532   [ +  +  +  - ]:           9 :         if (BF_ALIGNED_64(dst_off) && remaining_size >= 8) {
     533                 :             :             bpf_size = BPF_DW;
     534                 :             :             copy_bytes = 8;
     535   [ +  +  +  + ]:           9 :         } else if (BF_ALIGNED_32(dst_off) && remaining_size >= 4) {
     536                 :             :             bpf_size = BPF_W;
     537                 :             :             copy_bytes = 4;
     538   [ +  +  +  + ]:           5 :         } else if (BF_ALIGNED_16(dst_off) && remaining_size >= 2) {
     539                 :             :             bpf_size = BPF_H;
     540                 :             :             copy_bytes = 2;
     541                 :             :         }
     542                 :             : 
     543         [ +  - ]:           9 :         EMIT(program, BPF_STX_MEM(bpf_size, BPF_REG_10, reg, dst_off));
     544                 :             : 
     545                 :           9 :         remaining_size -= copy_bytes;
     546                 :           9 :         dst_off += (int)copy_bytes;
     547                 :             : 
     548         [ +  + ]:           9 :         if (remaining_size)
     549         [ +  - ]:           3 :             EMIT(program, BPF_ALU64_IMM(BPF_RSH, reg, copy_bytes * 8));
     550                 :             :     }
     551                 :             : 
     552                 :             :     return 0;
     553                 :             : }
     554                 :             : 
     555                 :         311 : int bf_stub_stx_payload(struct bf_codegen *program,
     556                 :             :                         const struct bf_matcher_meta *meta, size_t offset)
     557                 :             : {
     558                 :             :     assert(program);
     559                 :             :     assert(meta);
     560                 :             : 
     561                 :         622 :     return bf_stub_load(program, meta->hdr_payload_offset,
     562                 :         311 :                         meta->hdr_payload_size, BF_PROG_SCR_OFF(offset));
     563                 :             : }
        

Generated by: LCOV version 2.0-1