]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/em_nbyte.c
Introduce TIME_UNITS_PER_SEC to represent internal clock resolution
[mirror_iproute2.git] / tc / em_nbyte.c
1 /*
2 * em_nbyte.c N-Byte Ematch
3 *
4 * This program is free software; you can distribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Thomas Graf <tgraf@suug.ch>
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <syslog.h>
16 #include <fcntl.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <string.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23
24 #include "m_ematch.h"
25 #include <linux/tc_ematch/tc_em_nbyte.h>
26
27 extern struct ematch_util nbyte_ematch_util;
28
29 static void nbyte_print_usage(FILE *fd)
30 {
31 fprintf(fd,
32 "Usage: nbyte(NEEDLE at OFFSET [layer LAYER])\n" \
33 "where: NEEDLE := { string | \"c-escape-sequence\" }\n" \
34 " OFFSET := int\n" \
35 " LAYER := { link | header | next-header | 0..%d }\n" \
36 "\n" \
37 "Example: nbyte(\"ababa\" at 12 layer 1)\n",
38 TCF_LAYER_MAX);
39 }
40
41 static int nbyte_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
42 struct bstr *args)
43 {
44 struct bstr *a;
45 struct bstr *needle = args;
46 unsigned long offset = 0, layer = TCF_LAYER_NETWORK;
47 int offset_present = 0;
48 struct tcf_em_nbyte nb;
49
50 memset(&nb, 0, sizeof(nb));
51
52 #define PARSE_ERR(CARG, FMT, ARGS...) \
53 em_parse_error(EINVAL, args, CARG, &nbyte_ematch_util, FMT ,##ARGS)
54
55 if (args == NULL)
56 return PARSE_ERR(args, "nbyte: missing arguments");
57
58 if (needle->len <= 0)
59 return PARSE_ERR(args, "nbyte: needle length is 0");
60
61 for (a = bstr_next(args); a; a = bstr_next(a)) {
62 if (!bstrcmp(a, "at")) {
63 if (a->next == NULL)
64 return PARSE_ERR(a, "nbyte: missing argument");
65 a = bstr_next(a);
66
67 offset = bstrtoul(a);
68 if (offset == ULONG_MAX)
69 return PARSE_ERR(a, "nbyte: invalid offset, " \
70 "must be numeric");
71
72 offset_present = 1;
73 } else if (!bstrcmp(a, "layer")) {
74 if (a->next == NULL)
75 return PARSE_ERR(a, "nbyte: missing argument");
76 a = bstr_next(a);
77
78 layer = parse_layer(a);
79 if (layer == INT_MAX) {
80 layer = bstrtoul(a);
81 if (layer == ULONG_MAX)
82 return PARSE_ERR(a, "nbyte: invalid " \
83 "layer");
84 }
85
86 if (layer > TCF_LAYER_MAX)
87 return PARSE_ERR(a, "nbyte: illegal layer, " \
88 "must be in 0..%d", TCF_LAYER_MAX);
89 } else
90 return PARSE_ERR(a, "nbyte: unknown parameter");
91 }
92
93 if (offset_present == 0)
94 return PARSE_ERR(a, "nbyte: offset required");
95
96 nb.len = needle->len;
97 nb.layer = (__u8) layer;
98 nb.off = (__u16) offset;
99
100 addraw_l(n, MAX_MSG, hdr, sizeof(*hdr));
101 addraw_l(n, MAX_MSG, &nb, sizeof(nb));
102 addraw_l(n, MAX_MSG, needle->data, needle->len);
103
104 #undef PARSE_ERR
105 return 0;
106 }
107
108 static int nbyte_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
109 int data_len)
110 {
111 int i;
112 struct tcf_em_nbyte *nb = data;
113 __u8 *needle;
114
115 if (data_len < sizeof(*nb)) {
116 fprintf(stderr, "NByte header size mismatch\n");
117 return -1;
118 }
119
120 if (data_len < sizeof(*nb) + nb->len) {
121 fprintf(stderr, "NByte payload size mismatch\n");
122 return -1;
123 }
124
125 needle = data + sizeof(*nb);
126
127 for (i = 0; i < nb->len; i++)
128 fprintf(fd, "%02x ", needle[i]);
129
130 fprintf(fd, "\"");
131 for (i = 0; i < nb->len; i++)
132 fprintf(fd, "%c", isprint(needle[i]) ? needle[i] : '.');
133 fprintf(fd, "\" at %d layer %d", nb->off, nb->layer);
134
135 return 0;
136 }
137
138 struct ematch_util nbyte_ematch_util = {
139 .kind = "nbyte",
140 .kind_num = TCF_EM_NBYTE,
141 .parse_eopt = nbyte_parse_eopt,
142 .print_eopt = nbyte_print_eopt,
143 .print_usage = nbyte_print_usage
144 };