LCOV - code coverage report
Current view: top level - bpfilter/cgen/prog - link.c (source / functions) Coverage Total Hit
Test: coverage.lcov Lines: 88.2 % 169 149
Test Date: 2026-02-04 15:18:55 Functions: 100.0 % 12 12
Branches: 47.7 % 132 63

             Branch data     Line data    Source code
       1                 :             : // SPDX-License-Identifier: GPL-2.0-only
       2                 :             : /*
       3                 :             :  * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
       4                 :             :  */
       5                 :             : 
       6                 :             : #include "cgen/prog/link.h"
       7                 :             : 
       8                 :             : #include <linux/bpf.h>
       9                 :             : #include <linux/if_link.h>
      10                 :             : 
      11                 :             : #include <errno.h>
      12                 :             : #include <fcntl.h>
      13                 :             : #include <stdio.h>
      14                 :             : #include <stdlib.h>
      15                 :             : #include <string.h>
      16                 :             : #include <sys/socket.h>
      17                 :             : #include <unistd.h>
      18                 :             : 
      19                 :             : #include <bpfilter/bpf.h>
      20                 :             : #include <bpfilter/dump.h>
      21                 :             : #include <bpfilter/flavor.h>
      22                 :             : #include <bpfilter/helper.h>
      23                 :             : #include <bpfilter/hook.h>
      24                 :             : #include <bpfilter/logger.h>
      25                 :             : #include <bpfilter/pack.h>
      26                 :             : 
      27                 :          83 : int bf_link_new(struct bf_link **link, const char *name)
      28                 :             : {
      29                 :          83 :     _free_bf_link_ struct bf_link *_link = NULL;
      30                 :             : 
      31                 :             :     assert(link);
      32                 :             :     assert(name);
      33                 :             :     assert(name[0] != '\0');
      34                 :             : 
      35                 :          83 :     _link = malloc(sizeof(*_link));
      36         [ +  - ]:          83 :     if (!_link)
      37                 :             :         return -ENOMEM;
      38                 :             : 
      39                 :          83 :     bf_strncpy(_link->name, BPF_OBJ_NAME_LEN, name);
      40                 :             : 
      41                 :          83 :     _link->hookopts = NULL;
      42                 :          83 :     _link->fd = -1;
      43                 :          83 :     _link->fd_extra = -1;
      44                 :             : 
      45                 :          83 :     *link = TAKE_PTR(_link);
      46                 :             : 
      47                 :          83 :     return 0;
      48                 :             : }
      49                 :             : 
      50                 :           3 : int bf_link_new_from_pack(struct bf_link **link, int dir_fd,
      51                 :             :                           bf_rpack_node_t node)
      52                 :             : {
      53                 :           3 :     _free_bf_link_ struct bf_link *_link = NULL;
      54                 :           3 :     _cleanup_free_ char *name = NULL;
      55                 :             :     bf_rpack_node_t child;
      56                 :             :     int r;
      57                 :             : 
      58                 :             :     assert(link);
      59                 :             : 
      60                 :           3 :     r = bf_rpack_kv_str(node, "name", &name);
      61         [ -  + ]:           3 :     if (r)
      62         [ #  # ]:           0 :         return bf_rpack_key_err(r, "bf_link.name");
      63                 :             : 
      64                 :           3 :     r = bf_link_new(&_link, name);
      65         [ -  + ]:           3 :     if (r)
      66         [ #  # ]:           0 :         return bf_err_r(r, "failed to create bf_link from pack");
      67                 :             : 
      68                 :           3 :     r = bf_rpack_kv_node(node, "hookopts", &child);
      69         [ -  + ]:           3 :     if (r)
      70         [ #  # ]:           0 :         return bf_rpack_key_err(r, "bf_link.hookopts");
      71         [ +  + ]:           3 :     if (!bf_rpack_is_nil(child)) {
      72                 :           2 :         r = bf_hookopts_new_from_pack(&_link->hookopts, child);
      73         [ +  - ]:           2 :         if (r)
      74                 :             :             return r;
      75                 :             :     }
      76                 :             : 
      77         [ +  - ]:           3 :     if (dir_fd != -1) {
      78                 :           3 :         r = bf_bpf_obj_get("bf_link", dir_fd, &_link->fd);
      79         [ +  + ]:           3 :         if (r)
      80         [ +  - ]:           1 :             return bf_err_r(r, "failed to open pinned BPF link 'bf_link'");
      81                 :             : 
      82                 :           2 :         r = bf_bpf_obj_get("bf_link_extra", dir_fd, &_link->fd_extra);
      83         [ -  + ]:           2 :         if (r && r != -ENOENT) {
      84         [ #  # ]:           0 :             return bf_err_r(
      85                 :             :                 r, "failed to open pinned extra BPF link 'bf_link_extra'");
      86                 :             :         }
      87                 :             :     }
      88                 :             : 
      89                 :           2 :     *link = TAKE_PTR(_link);
      90                 :             : 
      91                 :           2 :     return 0;
      92                 :             : }
      93                 :             : 
      94                 :         169 : void bf_link_free(struct bf_link **link)
      95                 :             : {
      96                 :             :     assert(link);
      97                 :             : 
      98         [ +  + ]:         169 :     if (!*link)
      99                 :             :         return;
     100                 :             : 
     101                 :          83 :     bf_hookopts_free(&(*link)->hookopts);
     102                 :          83 :     closep(&(*link)->fd);
     103                 :          83 :     closep(&(*link)->fd_extra);
     104                 :             :     freep((void *)link);
     105                 :             : }
     106                 :             : 
     107                 :         185 : int bf_link_pack(const struct bf_link *link, bf_wpack_t *pack)
     108                 :             : {
     109                 :             :     assert(link);
     110                 :             :     assert(pack);
     111                 :             : 
     112                 :         185 :     bf_wpack_kv_str(pack, "name", link->name);
     113                 :             : 
     114         [ +  + ]:         185 :     if (link->hookopts) {
     115                 :          67 :         bf_wpack_open_object(pack, "hookopts");
     116                 :          67 :         bf_hookopts_pack(link->hookopts, pack);
     117                 :          67 :         bf_wpack_close_object(pack);
     118                 :             :     } else {
     119                 :             :         bf_wpack_kv_nil(pack, "hookopts");
     120                 :             :     }
     121                 :             : 
     122         [ -  + ]:         185 :     return bf_wpack_is_valid(pack) ? 0 : -EINVAL;
     123                 :             : }
     124                 :             : 
     125                 :           3 : void bf_link_dump(const struct bf_link *link, prefix_t *prefix)
     126                 :             : {
     127                 :             :     assert(link);
     128                 :             :     assert(prefix);
     129                 :             : 
     130         [ +  - ]:           3 :     DUMP(prefix, "struct bf_link at %p", link);
     131                 :             : 
     132                 :           3 :     bf_dump_prefix_push(prefix);
     133         [ +  - ]:           3 :     DUMP(prefix, "name: %s", link->name);
     134                 :             : 
     135         [ +  + ]:           3 :     if (link->hookopts) {
     136         [ +  - ]:           2 :         DUMP(prefix, "hookopts: struct bf_hookopts *");
     137                 :           2 :         bf_dump_prefix_push(prefix);
     138                 :           2 :         bf_hookopts_dump(link->hookopts, prefix);
     139                 :           2 :         bf_dump_prefix_pop(prefix);
     140                 :             :     } else {
     141         [ +  - ]:           1 :         DUMP(prefix, "hookopts: struct bf_hookopts * (NULL)");
     142                 :             :     }
     143                 :             : 
     144         [ +  - ]:           3 :     DUMP(bf_dump_prefix_last(prefix), "fd: %d", link->fd);
     145         [ +  - ]:           3 :     DUMP(bf_dump_prefix_last(prefix), "fd_extra: %d", link->fd_extra);
     146                 :           3 :     bf_dump_prefix_pop(prefix);
     147                 :           3 : }
     148                 :             : 
     149                 :             : /**
     150                 :             :  * @brief Try to attach an XDP program, probing for the best available mode.
     151                 :             :  *
     152                 :             :  * Tries driver mode first, falling back to SKB mode.
     153                 :             :  *
     154                 :             :  * @param prog_fd File descriptor of the BPF program to attach.
     155                 :             :  * @param ifindex Network interface index to attach the program to.
     156                 :             :  * @param hook Hook to attach the program to.
     157                 :             :  * @return File descriptor of the created link on success, negative errno on failure.
     158                 :             :  */
     159                 :          18 : static int _bf_link_try_attach_xdp(int prog_fd, int ifindex, enum bf_hook hook)
     160                 :             : {
     161                 :             :     int r;
     162                 :             : 
     163                 :          18 :     r = bf_bpf_link_create(prog_fd, ifindex, hook, XDP_FLAGS_DRV_MODE, 0, 0);
     164         [ +  + ]:          18 :     if (r >= 0) {
     165         [ +  - ]:          15 :         bf_info("attached XDP program in driver mode");
     166                 :          15 :         return r;
     167                 :             :     }
     168                 :             : 
     169         [ +  - ]:           3 :     if (r != -ENOTSUP)
     170                 :             :         return r;
     171                 :             : 
     172         [ #  # ]:           0 :     bf_dbg_r(r, "driver mode not available");
     173                 :             : 
     174                 :           0 :     r = bf_bpf_link_create(prog_fd, ifindex, hook, XDP_FLAGS_SKB_MODE, 0, 0);
     175         [ #  # ]:           0 :     if (r >= 0)
     176         [ #  # ]:           0 :         bf_info("attached XDP program in SKB mode");
     177                 :             : 
     178                 :             :     return r;
     179                 :             : }
     180                 :             : 
     181                 :          38 : int bf_link_attach(struct bf_link *link, enum bf_hook hook,
     182                 :             :                    struct bf_hookopts **hookopts, int prog_fd)
     183                 :             : {
     184                 :          38 :     _cleanup_close_ int fd = -1;
     185                 :          38 :     _cleanup_close_ int fd_extra = -1;
     186                 :          38 :     _cleanup_close_ int cgroup_fd = -1;
     187                 :          38 :     struct bf_hookopts *_hookopts = *hookopts;
     188                 :             :     int r;
     189                 :             : 
     190                 :             :     assert(link);
     191                 :             :     assert(hookopts);
     192                 :             : 
     193   [ +  +  +  +  :          38 :     switch (bf_hook_to_flavor(hook)) {
                      - ]
     194                 :          18 :     case BF_FLAVOR_XDP:
     195                 :          18 :         r = _bf_link_try_attach_xdp(prog_fd, _hookopts->ifindex, hook);
     196         [ +  + ]:          18 :         if (r < 0)
     197         [ +  - ]:           3 :             return bf_err_r(r, "failed to create XDP BPF link");
     198                 :             : 
     199                 :          15 :         fd = r;
     200                 :          15 :         break;
     201                 :          11 :     case BF_FLAVOR_TC:
     202                 :          11 :         r = bf_bpf_link_create(prog_fd, _hookopts->ifindex, hook, 0, 0, 0);
     203         [ -  + ]:          11 :         if (r < 0)
     204         [ #  # ]:           0 :             return bf_err_r(r, "failed to create TC BPF link");
     205                 :             : 
     206                 :          11 :         fd = r;
     207                 :          11 :         break;
     208                 :           2 :     case BF_FLAVOR_CGROUP:
     209                 :           2 :         cgroup_fd = open(_hookopts->cgpath, O_DIRECTORY | O_RDONLY);
     210         [ -  + ]:           2 :         if (cgroup_fd < 0) {
     211         [ #  # ]:           0 :             return bf_err_r(errno, "failed to open cgroup '%s'",
     212                 :             :                             _hookopts->cgpath);
     213                 :             :         }
     214                 :             : 
     215                 :           2 :         r = bf_bpf_link_create(prog_fd, cgroup_fd, hook, 0, 0, 0);
     216         [ -  + ]:           2 :         if (r < 0)
     217         [ #  # ]:           0 :             return bf_err_r(r, "failed to create cgroup BPF link");
     218                 :             : 
     219                 :           2 :         fd = r;
     220                 :           2 :         break;
     221                 :           7 :     case BF_FLAVOR_NF:
     222                 :           7 :         r = bf_bpf_link_create(prog_fd, 0, hook, 0, PF_INET,
     223                 :           7 :                                _hookopts->priorities[0]);
     224         [ +  + ]:           7 :         if (r < 0)
     225         [ +  - ]:           1 :             return bf_err_r(r, "failed to create nf_inet BPF link");
     226                 :             : 
     227                 :           6 :         fd = r;
     228                 :             : 
     229                 :           6 :         r = bf_bpf_link_create(prog_fd, 0, hook, 0, PF_INET6,
     230                 :           6 :                                _hookopts->priorities[0]);
     231         [ -  + ]:           6 :         if (r < 0)
     232         [ #  # ]:           0 :             return bf_err_r(r, "failed to create nf_inet6 BPF link");
     233                 :             : 
     234                 :           6 :         fd_extra = r;
     235                 :           6 :         break;
     236                 :             :     default:
     237                 :             :         return -ENOTSUP;
     238                 :             :     }
     239                 :             : 
     240                 :          34 :     link->fd = TAKE_FD(fd);
     241                 :          34 :     link->fd_extra = TAKE_FD(fd_extra);
     242                 :          34 :     link->hookopts = TAKE_PTR(*hookopts);
     243                 :             : 
     244                 :          34 :     return 0;
     245                 :             : }
     246                 :             : 
     247                 :           1 : static int _bf_link_update_nf(struct bf_link *link, enum bf_hook hook,
     248                 :             :                               int prog_fd)
     249                 :             : {
     250                 :           1 :     _cleanup_close_ int new_inet_fd = -1;
     251                 :           1 :     _cleanup_close_ int new_inet6_fd = -1;
     252                 :           1 :     struct bf_hookopts opts = *link->hookopts;
     253                 :             :     int r;
     254                 :             : 
     255                 :             :     assert(link);
     256                 :             : 
     257                 :             :     // Attach new program to both inet4 and inet6 using the unused priority
     258                 :             :     // This ensures the network is never left unfiltered
     259                 :           1 :     r = bf_bpf_link_create(prog_fd, 0, hook, 0, PF_INET, opts.priorities[1]);
     260         [ -  + ]:           1 :     if (r < 0)
     261         [ #  # ]:           0 :         return bf_err_r(r, "failed to create nf_inet BPF link");
     262                 :           1 :     new_inet_fd = r;
     263                 :             : 
     264                 :           1 :     r = bf_bpf_link_create(prog_fd, 0, hook, 0, PF_INET6, opts.priorities[1]);
     265         [ -  + ]:           1 :     if (r < 0)
     266         [ #  # ]:           0 :         return bf_err_r(r, "failed to create nf_inet6 BPF link");
     267                 :           1 :     new_inet6_fd = r;
     268                 :             : 
     269                 :             :     // Detach old links - safe now that new ones are active
     270                 :           1 :     closep(&link->fd);
     271                 :           1 :     closep(&link->fd_extra);
     272                 :             : 
     273                 :             :     // Update link with new file descriptors
     274                 :           1 :     link->fd = TAKE_FD(new_inet_fd);
     275                 :           1 :     link->fd_extra = TAKE_FD(new_inet6_fd);
     276                 :             : 
     277                 :             :     // Swap priorities so priorities[0] reflects the currently active priority
     278                 :           1 :     link->hookopts->priorities[0] = opts.priorities[1];
     279                 :           1 :     link->hookopts->priorities[1] = opts.priorities[0];
     280                 :             : 
     281                 :           1 :     return 0;
     282                 :             : }
     283                 :             : 
     284                 :           4 : int bf_link_update(struct bf_link *link, enum bf_hook hook, int prog_fd)
     285                 :             : {
     286                 :             :     assert(link);
     287                 :             : 
     288                 :             :     int r;
     289                 :             : 
     290      [ +  +  - ]:           4 :     switch (bf_hook_to_flavor(hook)) {
     291                 :           3 :     case BF_FLAVOR_XDP:
     292                 :             :     case BF_FLAVOR_TC:
     293                 :             :     case BF_FLAVOR_CGROUP:
     294                 :           3 :         r = bf_bpf_link_update(link->fd, prog_fd);
     295                 :           3 :         break;
     296                 :           1 :     case BF_FLAVOR_NF:
     297                 :           1 :         r = _bf_link_update_nf(link, hook, prog_fd);
     298                 :           1 :         break;
     299                 :             :     default:
     300                 :             :         return -ENOTSUP;
     301                 :             :     }
     302                 :             : 
     303                 :             :     return r;
     304                 :             : }
     305                 :             : 
     306                 :          47 : void bf_link_detach(struct bf_link *link)
     307                 :             : {
     308                 :             :     assert(link);
     309                 :             : 
     310                 :             :     int r;
     311                 :             : 
     312                 :          47 :     r = bf_bpf_link_detach(link->fd);
     313         [ +  + ]:          47 :     if (r) {
     314         [ +  - ]:          20 :         bf_warn_r(
     315                 :             :             r,
     316                 :             :             "call to BPF_LINK_DETACH failed, closing the file descriptor and assuming the link is destroyed");
     317                 :             :     }
     318                 :             : 
     319         [ +  + ]:          47 :     if (link->fd_extra > 0) {
     320                 :           6 :         r = bf_bpf_link_detach(link->fd_extra);
     321         [ -  + ]:           6 :         if (r) {
     322         [ #  # ]:           0 :             bf_warn_r(
     323                 :             :                 r,
     324                 :             :                 "call to BPF_LINK_DETACH for extra link failed, closing the file descriptor and assuming the link is destroyed");
     325                 :             :         }
     326                 :             :     }
     327                 :             : 
     328                 :          47 :     bf_hookopts_free(&link->hookopts);
     329                 :          47 :     closep(&link->fd);
     330                 :          47 :     closep(&link->fd_extra);
     331                 :          47 : }
     332                 :             : 
     333                 :          38 : int bf_link_pin(struct bf_link *link, int dir_fd)
     334                 :             : {
     335                 :             :     int r;
     336                 :             : 
     337                 :             :     assert(link);
     338                 :             : 
     339                 :          38 :     r = bf_bpf_obj_pin("bf_link", link->fd, dir_fd);
     340         [ -  + ]:          38 :     if (r)
     341         [ #  # ]:           0 :         return bf_err_r(r, "failed to pin BPF link");
     342                 :             : 
     343         [ +  + ]:          38 :     if (link->fd_extra > 0) {
     344                 :           7 :         r = bf_bpf_obj_pin("bf_link_extra", link->fd_extra, dir_fd);
     345         [ +  - ]:           7 :         if (r) {
     346                 :           0 :             bf_link_unpin(link, dir_fd);
     347         [ #  # ]:           0 :             return bf_err_r(r, "failed to pin extra BPF link");
     348                 :             :         }
     349                 :             :     }
     350                 :             : 
     351                 :             :     return 0;
     352                 :             : }
     353                 :             : 
     354                 :          51 : void bf_link_unpin(struct bf_link *link, int dir_fd)
     355                 :             : {
     356                 :             :     int r;
     357                 :             : 
     358                 :             :     assert(link);
     359                 :             :     assert(dir_fd > 0);
     360                 :             : 
     361                 :             :     (void)link;
     362                 :             : 
     363                 :          51 :     r = unlinkat(dir_fd, "bf_link", 0);
     364   [ +  +  -  + ]:          51 :     if (r < 0 && errno != ENOENT) {
     365                 :             :         // Do not warn on ENOENT, we want the file to be gone!
     366         [ #  # ]:           0 :         bf_warn_r(errno,
     367                 :             :                   "failed to unlink BPF link, assuming the link is not pinned");
     368                 :             :     }
     369                 :             : 
     370                 :          51 :     r = unlinkat(dir_fd, "bf_link_extra", 0);
     371   [ +  +  -  + ]:          51 :     if (r < 0 && errno != ENOENT) {
     372                 :             :         // Do not warn on ENOENT, we want the file to be gone!
     373         [ #  # ]:           0 :         bf_warn_r(
     374                 :             :             errno,
     375                 :             :             "failed to unlink extra BPF link, assuming the link is not pinned");
     376                 :             :     }
     377                 :          51 : }
        

Generated by: LCOV version 2.0-1