| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* em_cmp.c Simple comparison Ematch |
| 4 |
* |
| 5 |
* Authors: Thomas Graf <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <sys/socket.h> |
| 13 |
#include <netinet/in.h> |
| 14 |
#include <arpa/inet.h> |
| 15 |
#include <string.h> |
| 16 |
#include <errno.h> |
| 17 |
|
| 18 |
#include "m_ematch.h" |
| 19 |
#include <linux/tc_ematch/tc_em_cmp.h> |
| 20 |
|
| 21 |
extern struct ematch_util cmp_ematch_util; |
| 22 |
|
| 23 |
static void cmp_print_usage(FILE *fd) |
| 24 |
{ |
| 25 |
fprintf(fd, |
| 26 |
"Usage: cmp(ALIGN at OFFSET [ ATTRS ] { eq | lt | gt } VALUE)\n" \ |
| 27 |
"where: ALIGN := { u8 | u16 | u32 }\n" \ |
| 28 |
" ATTRS := [ layer LAYER ] [ mask MASK ] [ trans ]\n" \ |
| 29 |
" LAYER := { link | network | transport | 0..%d }\n" \ |
| 30 |
"\n" \ |
| 31 |
"Example: cmp(u16 at 3 layer 2 mask 0xff00 gt 20)\n", |
| 32 |
TCF_LAYER_MAX); |
| 33 |
} |
| 34 |
|
| 35 |
static int cmp_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr, |
| 36 |
struct bstr *args) |
| 37 |
{ |
| 38 |
struct bstr *a; |
| 39 |
int align, opnd = 0; |
| 40 |
unsigned long offset = 0, layer = TCF_LAYER_NETWORK, mask = 0, value = 0; |
| 41 |
int offset_present = 0, value_present = 0; |
| 42 |
struct tcf_em_cmp cmp = {}; |
| 43 |
|
| 44 |
#define PARSE_ERR(CARG, FMT, ARGS...) \ |
| 45 |
em_parse_error(EINVAL, args, CARG, &cmp_ematch_util, FMT, ##ARGS) |
| 46 |
|
| 47 |
if (args == NULL) |
| 48 |
return PARSE_ERR(args, "cmp: missing arguments"); |
| 49 |
|
| 50 |
if (!bstrcmp(args, "u8")) |
| 51 |
align = TCF_EM_ALIGN_U8; |
| 52 |
else if (!bstrcmp(args, "u16")) |
| 53 |
align = TCF_EM_ALIGN_U16; |
| 54 |
else if (!bstrcmp(args, "u32")) |
| 55 |
align = TCF_EM_ALIGN_U32; |
| 56 |
else |
| 57 |
return PARSE_ERR(args, "cmp: invalid alignment"); |
| 58 |
|
| 59 |
for (a = bstr_next(args); a; a = bstr_next(a)) { |
| 60 |
if (!bstrcmp(a, "at")) { |
| 61 |
if (a->next == NULL) |
| 62 |
return PARSE_ERR(a, "cmp: missing argument"); |
| 63 |
a = bstr_next(a); |
| 64 |
|
| 65 |
offset = bstrtoul(a); |
| 66 |
if (offset == ULONG_MAX) |
| 67 |
return PARSE_ERR(a, "cmp: invalid offset, " \ |
| 68 |
"must be numeric"); |
| 69 |
|
| 70 |
offset_present = 1; |
| 71 |
} else if (!bstrcmp(a, "layer")) { |
| 72 |
if (a->next == NULL) |
| 73 |
return PARSE_ERR(a, "cmp: missing argument"); |
| 74 |
a = bstr_next(a); |
| 75 |
|
| 76 |
layer = parse_layer(a); |
| 77 |
if (layer == INT_MAX) { |
| 78 |
layer = bstrtoul(a); |
| 79 |
if (layer == ULONG_MAX) |
| 80 |
return PARSE_ERR(a, "cmp: invalid " \ |
| 81 |
"layer"); |
| 82 |
} |
| 83 |
|
| 84 |
if (layer > TCF_LAYER_MAX) |
| 85 |
return PARSE_ERR(a, "cmp: illegal layer, " \ |
| 86 |
"must be in 0..%d", TCF_LAYER_MAX); |
| 87 |
} else if (!bstrcmp(a, "mask")) { |
| 88 |
if (a->next == NULL) |
| 89 |
return PARSE_ERR(a, "cmp: missing argument"); |
| 90 |
a = bstr_next(a); |
| 91 |
|
| 92 |
mask = bstrtoul(a); |
| 93 |
if (mask == ULONG_MAX) |
| 94 |
return PARSE_ERR(a, "cmp: invalid mask"); |
| 95 |
} else if (!bstrcmp(a, "trans")) { |
| 96 |
cmp.flags |= TCF_EM_CMP_TRANS; |
| 97 |
} else if (!bstrcmp(a, "eq") || !bstrcmp(a, "gt") || |
| 98 |
!bstrcmp(a, "lt")) { |
| 99 |
|
| 100 |
if (!bstrcmp(a, "eq")) |
| 101 |
opnd = TCF_EM_OPND_EQ; |
| 102 |
else if (!bstrcmp(a, "gt")) |
| 103 |
opnd = TCF_EM_OPND_GT; |
| 104 |
else if (!bstrcmp(a, "lt")) |
| 105 |
opnd = TCF_EM_OPND_LT; |
| 106 |
|
| 107 |
if (a->next == NULL) |
| 108 |
return PARSE_ERR(a, "cmp: missing argument"); |
| 109 |
a = bstr_next(a); |
| 110 |
|
| 111 |
value = bstrtoul(a); |
| 112 |
if (value == ULONG_MAX) |
| 113 |
return PARSE_ERR(a, "cmp: invalid value"); |
| 114 |
|
| 115 |
value_present = 1; |
| 116 |
} else |
| 117 |
return PARSE_ERR(a, "nbyte: unknown parameter"); |
| 118 |
} |
| 119 |
|
| 120 |
if (offset_present == 0 || value_present == 0) |
| 121 |
return PARSE_ERR(a, "cmp: offset and value required"); |
| 122 |
|
| 123 |
cmp.val = (__u32) value; |
| 124 |
cmp.mask = (__u32) mask; |
| 125 |
cmp.off = (__u16) offset; |
| 126 |
cmp.align = (__u8) align; |
| 127 |
cmp.layer = (__u8) layer; |
| 128 |
cmp.opnd = (__u8) opnd; |
| 129 |
|
| 130 |
addraw_l(n, MAX_MSG, hdr, sizeof(*hdr)); |
| 131 |
addraw_l(n, MAX_MSG, &cmp, sizeof(cmp)); |
| 132 |
|
| 133 |
#undef PARSE_ERR |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
static int cmp_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data, |
| 138 |
int data_len) |
| 139 |
{ |
| 140 |
struct tcf_em_cmp *cmp = data; |
| 141 |
|
| 142 |
if (data_len < sizeof(*cmp)) { |
| 143 |
fprintf(stderr, "CMP header size mismatch\n"); |
| 144 |
return -1; |
| 145 |
} |
| 146 |
|
| 147 |
if (cmp->align == TCF_EM_ALIGN_U8) |
| 148 |
fprintf(fd, "u8 "); |
| 149 |
else if (cmp->align == TCF_EM_ALIGN_U16) |
| 150 |
fprintf(fd, "u16 "); |
| 151 |
else if (cmp->align == TCF_EM_ALIGN_U32) |
| 152 |
fprintf(fd, "u32 "); |
| 153 |
|
| 154 |
fprintf(fd, "at %d layer %d ", cmp->off, cmp->layer); |
| 155 |
|
| 156 |
if (cmp->mask) |
| 157 |
fprintf(fd, "mask 0x%x ", cmp->mask); |
| 158 |
|
| 159 |
if (cmp->flags & TCF_EM_CMP_TRANS) |
| 160 |
fprintf(fd, "trans "); |
| 161 |
|
| 162 |
if (cmp->opnd == TCF_EM_OPND_EQ) |
| 163 |
fprintf(fd, "eq "); |
| 164 |
else if (cmp->opnd == TCF_EM_OPND_LT) |
| 165 |
fprintf(fd, "lt "); |
| 166 |
else if (cmp->opnd == TCF_EM_OPND_GT) |
| 167 |
fprintf(fd, "gt "); |
| 168 |
|
| 169 |
fprintf(fd, "%d", cmp->val); |
| 170 |
|
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
struct ematch_util cmp_ematch_util = { |
| 175 |
.kind = "cmp", |
| 176 |
.kind_num = TCF_EM_CMP, |
| 177 |
.parse_eopt = cmp_parse_eopt, |
| 178 |
.print_eopt = cmp_print_eopt, |
| 179 |
.print_usage = cmp_print_usage |
| 180 |
}; |