-
Notifications
You must be signed in to change notification settings - Fork 59
hook: accept interface names in ifindex= option #508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| #include "bpfilter/dump.h" | ||
| #include "bpfilter/flavor.h" | ||
| #include "bpfilter/helper.h" | ||
| #include "bpfilter/if.h" | ||
| #include "bpfilter/logger.h" | ||
| #include "bpfilter/pack.h" | ||
|
|
||
|
|
@@ -203,9 +204,17 @@ static int _bf_hookopts_ifindex_parse(struct bf_hookopts *hookopts, | |
| assert(hookopts); | ||
| assert(raw_opt); | ||
|
|
||
| if (hookopts->used_opts & BF_FLAG(BF_HOOKOPTS_IFACE)) { | ||
| return bf_err_r(-EEXIST, | ||
| "ifindex= can't be set when iface= is already defined"); | ||
| } | ||
|
|
||
| if (hookopts->used_opts & BF_FLAG(BF_HOOKOPTS_IFINDEX)) | ||
| return bf_err_r(-EEXIST, "ifindex= is defined multiple times"); | ||
|
|
||
| bf_warn( | ||
| "hook option 'ifindex=' is deprecated, use 'iface=' instead; 'iface=' accepts either an interface name or a numeric index"); | ||
|
|
||
| errno = 0; | ||
| ifindex = strtoul(raw_opt, NULL, 0); | ||
| if (errno != 0) { | ||
|
|
@@ -222,12 +231,60 @@ static int _bf_hookopts_ifindex_parse(struct bf_hookopts *hookopts, | |
| return 0; | ||
| } | ||
|
|
||
| static int _bf_hookopts_iface_parse(struct bf_hookopts *hookopts, | ||
| const char *raw_opt) | ||
| { | ||
| uint32_t ifindex; | ||
| int r; | ||
|
|
||
| assert(hookopts); | ||
| assert(raw_opt); | ||
|
|
||
| if (hookopts->used_opts & BF_FLAG(BF_HOOKOPTS_IFACE)) | ||
| return bf_err_r(-EEXIST, "iface= is defined multiple times"); | ||
|
|
||
| if (hookopts->used_opts & BF_FLAG(BF_HOOKOPTS_IFINDEX)) { | ||
| return bf_err_r(-EEXIST, | ||
| "iface= can't be set when ifindex= is already defined"); | ||
| } | ||
|
|
||
| r = bf_if_index_from_str(raw_opt, &ifindex); | ||
| if (r) | ||
| return bf_err_r(r, "failed to parse iface from '%s'", raw_opt); | ||
|
|
||
| if (ifindex > INT_MAX) | ||
| return bf_err_r(-E2BIG, "iface resolves to ifindex too big: %u", | ||
| ifindex); | ||
|
|
||
| hookopts->ifindex = (int)ifindex; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Claude: suggestion: Missing if (ifindex > INT_MAX)
return bf_err_r(-E2BIG, ...);Consider adding the same check here for consistency. |
||
| hookopts->used_opts |= BF_FLAG(BF_HOOKOPTS_IFACE); | ||
| hookopts->used_opts |= BF_FLAG(BF_HOOKOPTS_IFINDEX); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Claude: suggestion: Setting both
Consider updating |
||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static void _bf_hookopts_iface_dump(const struct bf_hookopts *hookopts, | ||
| prefix_t *prefix) | ||
| { | ||
| assert(hookopts); | ||
| assert(prefix); | ||
|
|
||
| DUMP(prefix, "iface: %d", hookopts->ifindex); | ||
| } | ||
|
|
||
| static void _bf_hookopts_ifindex_dump(const struct bf_hookopts *hookopts, | ||
| prefix_t *prefix) | ||
| { | ||
| assert(hookopts); | ||
| assert(prefix); | ||
|
|
||
| /* iface= sets both BF_HOOKOPTS_IFACE and BF_HOOKOPTS_IFINDEX so the | ||
| * XDP/TC ifindex requirement in bf_hookopts_validate is satisfied. In | ||
| * that case _bf_hookopts_iface_dump has already emitted the value; | ||
| * skip the ifindex line to avoid a duplicate dump entry. */ | ||
| if (bf_hookopts_is_used(hookopts, BF_HOOKOPTS_IFACE)) | ||
| return; | ||
|
|
||
| DUMP(prefix, "ifindex: %d", hookopts->ifindex); | ||
| } | ||
|
|
||
|
|
@@ -366,6 +423,12 @@ static struct bf_hookopts_ops | |
| int (*parse)(struct bf_hookopts *, const char *); | ||
| void (*dump)(const struct bf_hookopts *, prefix_t *); | ||
| } _bf_hookopts_ops[] = { | ||
| [BF_HOOKOPTS_IFACE] = {.name = "iface", | ||
| .type = BF_HOOKOPTS_IFACE, | ||
| .required_by = 0, | ||
| .supported_by = BF_FLAGS(BF_FLAVOR_XDP, BF_FLAVOR_TC), | ||
| .parse = _bf_hookopts_iface_parse, | ||
| .dump = _bf_hookopts_iface_dump}, | ||
| [BF_HOOKOPTS_IFINDEX] = {.name = "ifindex", | ||
| .type = BF_HOOKOPTS_IFINDEX, | ||
| .required_by = | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,9 @@ | |
|
|
||
| # Disallow duplicated hook options | ||
| (! ${BFCLI} ruleset set --dry-run --from-str "chain ifindex BF_HOOK_XDP{ifindex=2,ifindex=3} ACCEPT") | ||
| (! ${BFCLI} ruleset set --dry-run --from-str "chain iface BF_HOOK_XDP{iface=lo,iface=lo} ACCEPT") | ||
| (! ${BFCLI} ruleset set --dry-run --from-str "chain iface_ifindex BF_HOOK_XDP{ifindex=2,iface=lo} ACCEPT") | ||
| (! ${BFCLI} ruleset set --dry-run --from-str "chain ifindex_iface BF_HOOK_XDP{iface=lo,ifindex=2} ACCEPT") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Claude: nit: The file lacks a trailing newline (pre-existing, visible as |
||
| (! ${BFCLI} ruleset set --dry-run --from-str "chain cgpath BF_HOOK_CGROUP_SKB_INGRESS{cgpath=/sys/fs/cgroup,cgpath=/sys/fs/cgroup} ACCEPT") | ||
| (! ${BFCLI} ruleset set --dry-run --from-str "chain family BF_HOOK_NF_LOCAL_IN{family=inet4,family=inet6} ACCEPT") | ||
| (! ${BFCLI} ruleset set --dry-run --from-str "chain priorities BF_HOOK_NF_LOCAL_IN{priorities=1-2,priorities=3-4} ACCEPT") | ||
| (! ${BFCLI} ruleset set --dry-run --from-str "chain priorities BF_HOOK_NF_LOCAL_IN{priorities=1-2,priorities=3-4} ACCEPT") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude: must-fix: The "Required by" and "Supported by" columns are swapped for both
iface=andifindex=.In the code,
iface=has.required_by = 0and.supported_by = BF_FLAGS(BF_FLAVOR_XDP, BF_FLAVOR_TC), but this row listsBF_HOOK_XDP, BF_HOOK_TCunder "Required by" (unchanged context lines 385-386 inherited from the oldifindex=row).Conversely, the new
ifindex=entry (line 389-390) shows "Required by: N/A" and "Supported by: BF_HOOK_XDP, BF_HOOK_TC", but the code retains.required_by = BF_FLAGS(BF_FLAVOR_XDP, BF_FLAVOR_TC)and.supported_by = 0.Fix:
iface=should show "Required by: N/A" / "Supported by:BF_HOOK_XDP,BF_HOOK_TC".ifindex=should show "Required by:BF_HOOK_XDP,BF_HOOK_TC" / "Supported by: N/A".