| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2010 Thomas Graf <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "nl-default.h" |
| 7 |
|
| 8 |
#include <linux/tc_ematch/tc_em_cmp.h> |
| 9 |
|
| 10 |
#include <netlink/cli/utils.h> |
| 11 |
#include <netlink/route/pktloc.h> |
| 12 |
|
| 13 |
static void print_usage(void) |
| 14 |
{ |
| 15 |
printf( |
| 16 |
"Usage: nl-pktloc-lookup [OPTIONS] <name>\n" |
| 17 |
"\n" |
| 18 |
"OPTIONS\n" |
| 19 |
" -h, --help Show this help text.\n" |
| 20 |
" -v, --version Show versioning information.\n" |
| 21 |
" -l, --list List all packet location definitions.\n" |
| 22 |
" --u32=VALUE Print in iproute2's u32 selector style\n" |
| 23 |
"\n" |
| 24 |
"\n" |
| 25 |
"EXAMPLE\n" |
| 26 |
" $ nl-pktloc-lookup ip.dst\n" |
| 27 |
" $ nl-pktloc-lookup --list\n" |
| 28 |
"\n" |
| 29 |
); |
| 30 |
exit(0); |
| 31 |
} |
| 32 |
|
| 33 |
static const char *align_txt[] = { |
| 34 |
[TCF_EM_ALIGN_U8] = "u8", |
| 35 |
[TCF_EM_ALIGN_U16] = "u16", |
| 36 |
[TCF_EM_ALIGN_U32] = "u32" |
| 37 |
}; |
| 38 |
|
| 39 |
static uint32_t align_mask[] = { |
| 40 |
[TCF_EM_ALIGN_U8] = 0xff, |
| 41 |
[TCF_EM_ALIGN_U16] = 0xffff, |
| 42 |
[TCF_EM_ALIGN_U32] = 0xffffffff, |
| 43 |
}; |
| 44 |
|
| 45 |
static const char *layer_txt[] = { |
| 46 |
[TCF_LAYER_LINK] = "eth", |
| 47 |
[TCF_LAYER_NETWORK] = "ip", |
| 48 |
[TCF_LAYER_TRANSPORT] = "tcp" |
| 49 |
}; |
| 50 |
|
| 51 |
static const char *get_align_txt(struct rtnl_pktloc *loc, char buf[static 16]) |
| 52 |
{ |
| 53 |
if (loc->align < _NL_N_ELEMENTS(align_txt)) |
| 54 |
return align_txt[loc->align]; |
| 55 |
|
| 56 |
snprintf(buf, 16, "%u", loc->align); |
| 57 |
return buf; |
| 58 |
} |
| 59 |
|
| 60 |
static void dump_u32_style(struct rtnl_pktloc *loc, uint32_t value) |
| 61 |
{ |
| 62 |
char buf[16]; |
| 63 |
|
| 64 |
if (loc->align > 4) |
| 65 |
nl_cli_fatal(EINVAL, "u32 only supports alignments u8|u16|u32."); |
| 66 |
|
| 67 |
if (loc->layer == TCF_LAYER_LINK) |
| 68 |
nl_cli_fatal(EINVAL, "u32 does not support link " |
| 69 |
"layer locations."); |
| 70 |
|
| 71 |
if (loc->shift > 0) |
| 72 |
nl_cli_fatal(EINVAL, "u32 does not support shifting."); |
| 73 |
|
| 74 |
printf("%s %x %x at %s%u\n", get_align_txt(loc, buf), value, |
| 75 |
loc->mask ? loc->mask : |
| 76 |
(loc->align < _NL_N_ELEMENTS(align_mask) ? |
| 77 |
align_mask[loc->align] : |
| 78 |
0), |
| 79 |
loc->layer == TCF_LAYER_TRANSPORT ? "nexthdr+" : "", |
| 80 |
loc->offset); |
| 81 |
} |
| 82 |
|
| 83 |
static void dump_loc(struct rtnl_pktloc *loc) |
| 84 |
{ |
| 85 |
char buf[16]; |
| 86 |
|
| 87 |
printf("%s = %s at %s+%u & %#x >> %u\n", loc->name, |
| 88 |
get_align_txt(loc, buf), |
| 89 |
loc->layer < _NL_N_ELEMENTS(layer_txt) ? layer_txt[loc->layer] : |
| 90 |
"???", |
| 91 |
loc->offset, loc->mask, loc->shift); |
| 92 |
} |
| 93 |
|
| 94 |
static void list_cb(struct rtnl_pktloc *loc, void *arg) |
| 95 |
{ |
| 96 |
char buf[16]; |
| 97 |
|
| 98 |
printf("%-26s %-5s %3s+%-4u %#-10x %-8u %u\n", loc->name, |
| 99 |
get_align_txt(loc, buf), |
| 100 |
loc->layer < _NL_N_ELEMENTS(layer_txt) ? layer_txt[loc->layer] : |
| 101 |
"???", |
| 102 |
loc->offset, loc->mask, loc->shift, loc->refcnt); |
| 103 |
} |
| 104 |
|
| 105 |
static void do_list(void) |
| 106 |
{ |
| 107 |
printf( |
| 108 |
"name align offset mask shift refcnt\n"); |
| 109 |
printf("---------------------------------------------------------\n"); |
| 110 |
|
| 111 |
rtnl_pktloc_foreach(&list_cb, NULL); |
| 112 |
} |
| 113 |
|
| 114 |
int main(int argc, char *argv[]) |
| 115 |
{ |
| 116 |
struct rtnl_pktloc *loc; |
| 117 |
int err, ustyle = 0; |
| 118 |
uint32_t uvalue = 0; |
| 119 |
|
| 120 |
for (;;) { |
| 121 |
int c, optidx = 0; |
| 122 |
enum { |
| 123 |
ARG_U32 = 257, |
| 124 |
}; |
| 125 |
static struct option long_opts[] = { |
| 126 |
{ "help", 0, 0, 'h' }, |
| 127 |
{ "version", 0, 0, 'v' }, |
| 128 |
{ "list", 0, 0, 'l' }, |
| 129 |
{ "u32", 1, 0, ARG_U32 }, |
| 130 |
{ 0, 0, 0, 0 } |
| 131 |
}; |
| 132 |
|
| 133 |
c = getopt_long(argc, argv, "hvl", long_opts, &optidx); |
| 134 |
if (c == -1) |
| 135 |
break; |
| 136 |
|
| 137 |
switch (c) { |
| 138 |
case 'h': print_usage(); break; |
| 139 |
case 'v': nl_cli_print_version(); break; |
| 140 |
case 'l': do_list(); exit(0); |
| 141 |
case ARG_U32: |
| 142 |
ustyle = 1; |
| 143 |
uvalue = nl_cli_parse_u32(optarg); |
| 144 |
break; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if (optind >= argc) |
| 149 |
print_usage(); |
| 150 |
|
| 151 |
if ((err = rtnl_pktloc_lookup(argv[optind++], &loc)) < 0) |
| 152 |
nl_cli_fatal(err, "Unable to lookup packet location: %s", |
| 153 |
nl_geterror(err)); |
| 154 |
|
| 155 |
if (ustyle) |
| 156 |
dump_u32_style(loc, uvalue); |
| 157 |
else |
| 158 |
dump_loc(loc); |
| 159 |
|
| 160 |
return 0; |
| 161 |
} |