| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* m_csum.c checksum updating action |
| 4 |
* |
| 5 |
* Authors: Gregoire Baron <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include <unistd.h> |
| 12 |
|
| 13 |
#include <linux/tc_act/tc_csum.h> |
| 14 |
|
| 15 |
#include "utils.h" |
| 16 |
#include "tc_util.h" |
| 17 |
|
| 18 |
static void |
| 19 |
explain(void) |
| 20 |
{ |
| 21 |
fprintf(stderr, "Usage: ... csum <UPDATE>\n" |
| 22 |
"Where: UPDATE := <TARGET> [<UPDATE>]\n" |
| 23 |
" TARGET := { ip4h | icmp | igmp | tcp | udp | udplite | sctp | <SWEETS> }\n" |
| 24 |
" SWEETS := { and | or | \'+\' }\n"); |
| 25 |
} |
| 26 |
|
| 27 |
static void |
| 28 |
usage(void) |
| 29 |
{ |
| 30 |
explain(); |
| 31 |
exit(-1); |
| 32 |
} |
| 33 |
|
| 34 |
static int |
| 35 |
parse_csum_args(int *argc_p, char ***argv_p, struct tc_csum *sel) |
| 36 |
{ |
| 37 |
int argc = *argc_p; |
| 38 |
char **argv = *argv_p; |
| 39 |
|
| 40 |
if (argc <= 0) |
| 41 |
return -1; |
| 42 |
|
| 43 |
while (argc > 0) { |
| 44 |
if ((matches(*argv, "iph") == 0) || |
| 45 |
(matches(*argv, "ip4h") == 0) || |
| 46 |
(matches(*argv, "ipv4h") == 0)) |
| 47 |
sel->update_flags |= TCA_CSUM_UPDATE_FLAG_IPV4HDR; |
| 48 |
|
| 49 |
else if (matches(*argv, "icmp") == 0) |
| 50 |
sel->update_flags |= TCA_CSUM_UPDATE_FLAG_ICMP; |
| 51 |
|
| 52 |
else if (matches(*argv, "igmp") == 0) |
| 53 |
sel->update_flags |= TCA_CSUM_UPDATE_FLAG_IGMP; |
| 54 |
|
| 55 |
else if (matches(*argv, "tcp") == 0) |
| 56 |
sel->update_flags |= TCA_CSUM_UPDATE_FLAG_TCP; |
| 57 |
|
| 58 |
else if (matches(*argv, "udp") == 0) |
| 59 |
sel->update_flags |= TCA_CSUM_UPDATE_FLAG_UDP; |
| 60 |
|
| 61 |
else if (matches(*argv, "udplite") == 0) |
| 62 |
sel->update_flags |= TCA_CSUM_UPDATE_FLAG_UDPLITE; |
| 63 |
|
| 64 |
else if (matches(*argv, "sctp") == 0) |
| 65 |
sel->update_flags |= TCA_CSUM_UPDATE_FLAG_SCTP; |
| 66 |
|
| 67 |
else if ((matches(*argv, "and") == 0) || |
| 68 |
(matches(*argv, "or") == 0) || |
| 69 |
(matches(*argv, "+") == 0)) |
| 70 |
; /* just ignore: ... csum iph and tcp or udp */ |
| 71 |
else |
| 72 |
break; |
| 73 |
argc--; |
| 74 |
argv++; |
| 75 |
} |
| 76 |
|
| 77 |
*argc_p = argc; |
| 78 |
*argv_p = argv; |
| 79 |
|
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
static int |
| 84 |
parse_csum(const struct action_util *a, int *argc_p, |
| 85 |
char ***argv_p, int tca_id, struct nlmsghdr *n) |
| 86 |
{ |
| 87 |
struct tc_csum sel = {}; |
| 88 |
|
| 89 |
int argc = *argc_p; |
| 90 |
char **argv = *argv_p; |
| 91 |
int ok = 0; |
| 92 |
struct rtattr *tail; |
| 93 |
|
| 94 |
while (argc > 0) { |
| 95 |
if (matches(*argv, "csum") == 0) { |
| 96 |
NEXT_ARG(); |
| 97 |
if (strcmp(*argv, "index") == 0) { |
| 98 |
goto skip_args; |
| 99 |
} else if (parse_csum_args(&argc, &argv, &sel)) { |
| 100 |
fprintf(stderr, "Illegal csum construct (%s)\n", |
| 101 |
*argv); |
| 102 |
explain(); |
| 103 |
return -1; |
| 104 |
} |
| 105 |
ok++; |
| 106 |
continue; |
| 107 |
} else if (matches(*argv, "help") == 0) { |
| 108 |
usage(); |
| 109 |
} else { |
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if (!ok) { |
| 115 |
explain(); |
| 116 |
return -1; |
| 117 |
} |
| 118 |
|
| 119 |
if (sel.update_flags == 0) { |
| 120 |
fprintf(stderr, "Illegal csum construct, empty <UPDATE> list\n"); |
| 121 |
return -1; |
| 122 |
} |
| 123 |
|
| 124 |
parse_action_control_dflt(&argc, &argv, &sel.action, false, TC_ACT_OK); |
| 125 |
|
| 126 |
if (argc) { |
| 127 |
if (matches(*argv, "index") == 0) { |
| 128 |
skip_args: |
| 129 |
NEXT_ARG(); |
| 130 |
if (get_u32(&sel.index, *argv, 10)) { |
| 131 |
fprintf(stderr, "Illegal \"index\" (%s) <csum>\n", |
| 132 |
*argv); |
| 133 |
return -1; |
| 134 |
} |
| 135 |
argc--; |
| 136 |
argv++; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
tail = addattr_nest(n, MAX_MSG, tca_id); |
| 141 |
addattr_l(n, MAX_MSG, TCA_CSUM_PARMS, &sel, sizeof(sel)); |
| 142 |
addattr_nest_end(n, tail); |
| 143 |
|
| 144 |
*argc_p = argc; |
| 145 |
*argv_p = argv; |
| 146 |
|
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
static int |
| 151 |
print_csum(const struct action_util *au, FILE *f, struct rtattr *arg) |
| 152 |
{ |
| 153 |
struct tc_csum *sel; |
| 154 |
|
| 155 |
struct rtattr *tb[TCA_CSUM_MAX + 1]; |
| 156 |
|
| 157 |
char *uflag_1 = ""; |
| 158 |
char *uflag_2 = ""; |
| 159 |
char *uflag_3 = ""; |
| 160 |
char *uflag_4 = ""; |
| 161 |
char *uflag_5 = ""; |
| 162 |
char *uflag_6 = ""; |
| 163 |
char *uflag_7 = ""; |
| 164 |
SPRINT_BUF(buf); |
| 165 |
|
| 166 |
int uflag_count = 0; |
| 167 |
|
| 168 |
print_string(PRINT_ANY, "kind", "%s ", "csum"); |
| 169 |
if (arg == NULL) |
| 170 |
return 0; |
| 171 |
|
| 172 |
parse_rtattr_nested(tb, TCA_CSUM_MAX, arg); |
| 173 |
|
| 174 |
if (tb[TCA_CSUM_PARMS] == NULL) { |
| 175 |
fprintf(stderr, "Missing csum parameters\n"); |
| 176 |
return -1; |
| 177 |
} |
| 178 |
sel = RTA_DATA(tb[TCA_CSUM_PARMS]); |
| 179 |
|
| 180 |
if (sel->update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) { |
| 181 |
uflag_1 = "iph"; |
| 182 |
uflag_count++; |
| 183 |
} |
| 184 |
#define CSUM_UFLAG_BUFFER(flag_buffer, flag_value, flag_string) \ |
| 185 |
do { \ |
| 186 |
if (sel->update_flags & flag_value) { \ |
| 187 |
flag_buffer = uflag_count > 0 ? \ |
| 188 |
", " flag_string : flag_string; \ |
| 189 |
uflag_count++; \ |
| 190 |
} \ |
| 191 |
} while (0) |
| 192 |
CSUM_UFLAG_BUFFER(uflag_2, TCA_CSUM_UPDATE_FLAG_ICMP, "icmp"); |
| 193 |
CSUM_UFLAG_BUFFER(uflag_3, TCA_CSUM_UPDATE_FLAG_IGMP, "igmp"); |
| 194 |
CSUM_UFLAG_BUFFER(uflag_4, TCA_CSUM_UPDATE_FLAG_TCP, "tcp"); |
| 195 |
CSUM_UFLAG_BUFFER(uflag_5, TCA_CSUM_UPDATE_FLAG_UDP, "udp"); |
| 196 |
CSUM_UFLAG_BUFFER(uflag_6, TCA_CSUM_UPDATE_FLAG_UDPLITE, "udplite"); |
| 197 |
CSUM_UFLAG_BUFFER(uflag_7, TCA_CSUM_UPDATE_FLAG_SCTP, "sctp"); |
| 198 |
if (!uflag_count) { |
| 199 |
uflag_1 = "?empty"; |
| 200 |
} |
| 201 |
|
| 202 |
snprintf(buf, sizeof(buf), "%s%s%s%s%s%s%s", |
| 203 |
uflag_1, uflag_2, uflag_3, |
| 204 |
uflag_4, uflag_5, uflag_6, uflag_7); |
| 205 |
print_string(PRINT_ANY, "csum", "(%s) ", buf); |
| 206 |
|
| 207 |
print_action_control("action ", sel->action, _SL_); |
| 208 |
print_uint(PRINT_ANY, "index", "\tindex %u", sel->index); |
| 209 |
print_int(PRINT_ANY, "ref", " ref %d", sel->refcnt); |
| 210 |
print_int(PRINT_ANY, "bind", " bind %d", sel->bindcnt); |
| 211 |
|
| 212 |
if (show_stats) { |
| 213 |
if (tb[TCA_CSUM_TM]) { |
| 214 |
struct tcf_t *tm = RTA_DATA(tb[TCA_CSUM_TM]); |
| 215 |
|
| 216 |
print_tm(tm); |
| 217 |
} |
| 218 |
} |
| 219 |
print_nl(); |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
struct action_util csum_action_util = { |
| 225 |
.id = "csum", |
| 226 |
.parse_aopt = parse_csum, |
| 227 |
.print_aopt = print_csum, |
| 228 |
}; |