Debian Codegraph @

tc/em_nbyte.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * em_nbyte.c N-Byte 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_nbyte.h>
20
21 extern struct ematch_util nbyte_ematch_util;
22
23 static void nbyte_print_usage(FILE *fd)
24 {
25 fprintf(fd,
26 "Usage: nbyte(NEEDLE at OFFSET [layer LAYER])\n" \
27 "where: NEEDLE := { string | \"c-escape-sequence\" }\n" \
28 " OFFSET := int\n" \
29 " LAYER := { link | network | transport | 0..%d }\n" \
30 "\n" \
31 "Example: nbyte(\"ababa\" at 12 layer 1)\n",
32 TCF_LAYER_MAX);
33 }
34
35 static int nbyte_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
36 struct bstr *args)
37 {
38 struct bstr *a;
39 struct bstr *needle = args;
40 unsigned long offset = 0, layer = TCF_LAYER_NETWORK;
41 int offset_present = 0;
42 struct tcf_em_nbyte nb = {};
43
44 #define PARSE_ERR(CARG, FMT, ARGS...) \
45 em_parse_error(EINVAL, args, CARG, &nbyte_ematch_util, FMT, ##ARGS)
46
47 if (args == NULL)
48 return PARSE_ERR(args, "nbyte: missing arguments");
49
50 if (needle->len <= 0)
51 return PARSE_ERR(args, "nbyte: needle length is 0");
52
53 for (a = bstr_next(args); a; a = bstr_next(a)) {
54 if (!bstrcmp(a, "at")) {
55 if (a->next == NULL)
56 return PARSE_ERR(a, "nbyte: missing argument");
57 a = bstr_next(a);
58
59 offset = bstrtoul(a);
60 if (offset == ULONG_MAX)
61 return PARSE_ERR(a, "nbyte: invalid offset, " \
62 "must be numeric");
63
64 offset_present = 1;
65 } else if (!bstrcmp(a, "layer")) {
66 if (a->next == NULL)
67 return PARSE_ERR(a, "nbyte: missing argument");
68 a = bstr_next(a);
69
70 layer = parse_layer(a);
71 if (layer == INT_MAX) {
72 layer = bstrtoul(a);
73 if (layer == ULONG_MAX)
74 return PARSE_ERR(a, "nbyte: invalid " \
75 "layer");
76 }
77
78 if (layer > TCF_LAYER_MAX)
79 return PARSE_ERR(a, "nbyte: illegal layer, " \
80 "must be in 0..%d", TCF_LAYER_MAX);
81 } else
82 return PARSE_ERR(a, "nbyte: unknown parameter");
83 }
84
85 if (offset_present == 0)
86 return PARSE_ERR(a, "nbyte: offset required");
87
88 nb.len = needle->len;
89 nb.layer = (__u8) layer;
90 nb.off = (__u16) offset;
91
92 addraw_l(n, MAX_MSG, hdr, sizeof(*hdr));
93 addraw_l(n, MAX_MSG, &nb, sizeof(nb));
94 addraw_l(n, MAX_MSG, needle->data, needle->len);
95
96 #undef PARSE_ERR
97 return 0;
98 }
99
100 static int nbyte_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
101 int data_len)
102 {
103 int i;
104 struct tcf_em_nbyte *nb = data;
105 __u8 *needle;
106
107 if (data_len < sizeof(*nb)) {
108 fprintf(stderr, "NByte header size mismatch\n");
109 return -1;
110 }
111
112 if (data_len < sizeof(*nb) + nb->len) {
113 fprintf(stderr, "NByte payload size mismatch\n");
114 return -1;
115 }
116
117 needle = data + sizeof(*nb);
118
119 for (i = 0; i < nb->len; i++)
120 fprintf(fd, "%02x ", needle[i]);
121
122 fprintf(fd, "\"");
123 for (i = 0; i < nb->len; i++)
124 fprintf(fd, "%c", isprint(needle[i]) ? needle[i] : '.');
125 fprintf(fd, "\" at %d layer %d", nb->off, nb->layer);
126
127 return 0;
128 }
129
130 struct ematch_util nbyte_ematch_util = {
131 .kind = "nbyte",
132 .kind_num = TCF_EM_NBYTE,
133 .parse_eopt = nbyte_parse_eopt,
134 .print_eopt = nbyte_print_eopt,
135 .print_usage = nbyte_print_usage
136 };