2 * m_csum.c checksum updating action
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.
9 * Authors: Gregoire Baron <baronchon@n7mm.org>
17 #include <linux/tc_act/tc_csum.h>
25 fprintf(stderr, "Usage: ... csum <UPDATE>\n"
26 "Where: UPDATE := <TARGET> [<UPDATE>]\n"
27 " TARGET := { ip4h | icmp | igmp | tcp | udp | udplite | sctp | <SWEETS> }\n"
28 " SWEETS := { and | or | \'+\' }\n");
39 parse_csum_args(int *argc_p, char ***argv_p, struct tc_csum *sel)
42 char **argv = *argv_p;
48 if ((matches(*argv, "iph") == 0) ||
49 (matches(*argv, "ip4h") == 0) ||
50 (matches(*argv, "ipv4h") == 0))
51 sel->update_flags |= TCA_CSUM_UPDATE_FLAG_IPV4HDR;
53 else if (matches(*argv, "icmp") == 0)
54 sel->update_flags |= TCA_CSUM_UPDATE_FLAG_ICMP;
56 else if (matches(*argv, "igmp") == 0)
57 sel->update_flags |= TCA_CSUM_UPDATE_FLAG_IGMP;
59 else if (matches(*argv, "tcp") == 0)
60 sel->update_flags |= TCA_CSUM_UPDATE_FLAG_TCP;
62 else if (matches(*argv, "udp") == 0)
63 sel->update_flags |= TCA_CSUM_UPDATE_FLAG_UDP;
65 else if (matches(*argv, "udplite") == 0)
66 sel->update_flags |= TCA_CSUM_UPDATE_FLAG_UDPLITE;
68 else if (matches(*argv, "sctp") == 0)
69 sel->update_flags |= TCA_CSUM_UPDATE_FLAG_SCTP;
71 else if ((matches(*argv, "and") == 0) ||
72 (matches(*argv, "or") == 0) ||
73 (matches(*argv, "+") == 0))
74 ; /* just ignore: ... csum iph and tcp or udp */
88 parse_csum(struct action_util *a, int *argc_p,
89 char ***argv_p, int tca_id, struct nlmsghdr *n)
91 struct tc_csum sel = {};
94 char **argv = *argv_p;
99 if (matches(*argv, "csum") == 0) {
101 if (parse_csum_args(&argc, &argv, &sel)) {
102 fprintf(stderr, "Illegal csum construct (%s)\n",
109 } else if (matches(*argv, "help") == 0) {
121 if (sel.update_flags == 0) {
122 fprintf(stderr, "Illegal csum construct, empty <UPDATE> list\n");
126 parse_action_control_dflt(&argc, &argv, &sel.action, false, TC_ACT_OK);
129 if (matches(*argv, "index") == 0) {
131 if (get_u32(&sel.index, *argv, 10)) {
132 fprintf(stderr, "Illegal \"index\" (%s) <csum>\n",
141 tail = addattr_nest(n, MAX_MSG, tca_id);
142 addattr_l(n, MAX_MSG, TCA_CSUM_PARMS, &sel, sizeof(sel));
143 addattr_nest_end(n, tail);
152 print_csum(struct action_util *au, FILE *f, struct rtattr *arg)
156 struct rtattr *tb[TCA_CSUM_MAX + 1];
172 parse_rtattr_nested(tb, TCA_CSUM_MAX, arg);
174 if (tb[TCA_CSUM_PARMS] == NULL) {
175 fprintf(stderr, "Missing csum parameters\n");
178 sel = RTA_DATA(tb[TCA_CSUM_PARMS]);
180 if (sel->update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
184 #define CSUM_UFLAG_BUFFER(flag_buffer, flag_value, flag_string) \
186 if (sel->update_flags & flag_value) { \
187 flag_buffer = uflag_count > 0 ? \
188 ", " flag_string : flag_string; \
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");
202 print_string(PRINT_ANY, "kind", "%s ", "csum");
203 snprintf(buf, sizeof(buf), "%s%s%s%s%s%s%s",
204 uflag_1, uflag_2, uflag_3,
205 uflag_4, uflag_5, uflag_6, uflag_7);
206 print_string(PRINT_ANY, "csum", "(%s) ", buf);
208 print_action_control(f, "action ", sel->action, "\n");
209 print_uint(PRINT_ANY, "index", "\tindex %u", sel->index);
210 print_int(PRINT_ANY, "ref", " ref %d", sel->refcnt);
211 print_int(PRINT_ANY, "bind", " bind %d", sel->bindcnt);
214 if (tb[TCA_CSUM_TM]) {
215 struct tcf_t *tm = RTA_DATA(tb[TCA_CSUM_TM]);
220 print_string(PRINT_FP, NULL, "%s", "\n");
225 struct action_util csum_action_util = {
227 .parse_aopt = parse_csum,
228 .print_aopt = print_csum,