]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_csum.c
update kernel headers from 4.10 net-next
[mirror_iproute2.git] / tc / m_csum.c
CommitLineData
3822cc98
GB
1/*
2 * m_csum.c checksum updating action
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: Gregoire Baron <baronchon@n7mm.org>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <unistd.h>
16
17#include <linux/tc_act/tc_csum.h>
18
19#include "utils.h"
20#include "tc_util.h"
21
22static void
23explain(void)
24{
25 fprintf(stderr, "Usage: ... csum <UPDATE>\n"
26 "Where: UPDATE := <TARGET> [<UPDATE>]\n"
32a121cb 27 " TARGET := { ip4h | icmp | igmp | tcp | udp | udplite | <SWEETS> }\n"
3822cc98
GB
28 " SWEETS := { and | or | \'+\' }\n");
29}
30
31static void
32usage(void)
33{
34 explain();
35 exit(-1);
36}
37
38static int
39parse_csum_args(int *argc_p, char ***argv_p, struct tc_csum *sel)
40{
41 int argc = *argc_p;
42 char **argv = *argv_p;
43
44 if (argc <= 0)
45 return -1;
46
32a121cb 47 while (argc > 0) {
3822cc98
GB
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;
52
53 else if (matches(*argv, "icmp") == 0)
54 sel->update_flags |= TCA_CSUM_UPDATE_FLAG_ICMP;
55
56 else if (matches(*argv, "igmp") == 0)
57 sel->update_flags |= TCA_CSUM_UPDATE_FLAG_IGMP;
58
59 else if (matches(*argv, "tcp") == 0)
60 sel->update_flags |= TCA_CSUM_UPDATE_FLAG_TCP;
61
62 else if (matches(*argv, "udp") == 0)
63 sel->update_flags |= TCA_CSUM_UPDATE_FLAG_UDP;
64
65 else if (matches(*argv, "udplite") == 0)
66 sel->update_flags |= TCA_CSUM_UPDATE_FLAG_UDPLITE;
67
68 else if ((matches(*argv, "and") == 0) ||
69 (matches(*argv, "or") == 0) ||
70 (matches(*argv, "+") == 0))
71 ; /* just ignore: ... csum iph and tcp or udp */
72 else
73 break;
74 argc--;
75 argv++;
76 }
77
78 *argc_p = argc;
79 *argv_p = argv;
80
81 return 0;
82}
83
84static int
85parse_csum(struct action_util *a, int *argc_p,
86 char ***argv_p, int tca_id, struct nlmsghdr *n)
87{
d17b136f 88 struct tc_csum sel = {};
3822cc98
GB
89
90 int argc = *argc_p;
91 char **argv = *argv_p;
92 int ok = 0;
93 struct rtattr *tail;
94
3822cc98
GB
95 while (argc > 0) {
96 if (matches(*argv, "csum") == 0) {
97 NEXT_ARG();
98 if (parse_csum_args(&argc, &argv, &sel)) {
99 fprintf(stderr, "Illegal csum construct (%s)\n",
100 *argv);
101 explain();
102 return -1;
103 }
104 ok++;
105 continue;
106 } else if (matches(*argv, "help") == 0) {
107 usage();
32a121cb 108 } else {
3822cc98
GB
109 break;
110 }
111 }
112
113 if (!ok) {
114 explain();
115 return -1;
116 }
117
118 if (sel.update_flags == 0) {
119 fprintf(stderr, "Illegal csum construct, empty <UPDATE> list\n");
120 return -1;
121 }
122
69f5aff6
PS
123 if (argc && !action_a2n(*argv, &sel.action, false))
124 NEXT_ARG_FWD();
3822cc98
GB
125
126 if (argc) {
127 if (matches(*argv, "index") == 0) {
128 NEXT_ARG();
129 if (get_u32(&sel.index, *argv, 10)) {
130 fprintf(stderr, "Illegal \"index\" (%s) <csum>\n",
131 *argv);
132 return -1;
133 }
134 argc--;
135 argv++;
136 }
137 }
138
139 tail = NLMSG_TAIL(n);
140 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
141 addattr_l(n, MAX_MSG, TCA_CSUM_PARMS, &sel, sizeof(sel));
142 tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail;
143
144 *argc_p = argc;
145 *argv_p = argv;
146
147 return 0;
148}
149
150static int
32a121cb 151print_csum(struct action_util *au, FILE *f, struct rtattr *arg)
3822cc98
GB
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 = "";
32a121cb 163
3822cc98
GB
164 int uflag_count = 0;
165
166 if (arg == NULL)
167 return -1;
168
169 parse_rtattr_nested(tb, TCA_CSUM_MAX, arg);
170
171 if (tb[TCA_CSUM_PARMS] == NULL) {
172 fprintf(f, "[NULL csum parameters]");
173 return -1;
174 }
175 sel = RTA_DATA(tb[TCA_CSUM_PARMS]);
176
177 if (sel->update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
178 uflag_1 = "iph";
179 uflag_count++;
180 }
181 #define CSUM_UFLAG_BUFFER(flag_buffer, flag_value, flag_string) \
182 do { \
183 if (sel->update_flags & flag_value) { \
184 flag_buffer = uflag_count > 0 ? \
185 ", " flag_string : flag_string; \
186 uflag_count++; \
187 } \
32a121cb 188 } while (0)
3822cc98
GB
189 CSUM_UFLAG_BUFFER(uflag_2, TCA_CSUM_UPDATE_FLAG_ICMP, "icmp");
190 CSUM_UFLAG_BUFFER(uflag_3, TCA_CSUM_UPDATE_FLAG_IGMP, "igmp");
90d98edf 191 CSUM_UFLAG_BUFFER(uflag_4, TCA_CSUM_UPDATE_FLAG_TCP, "tcp");
3822cc98
GB
192 CSUM_UFLAG_BUFFER(uflag_5, TCA_CSUM_UPDATE_FLAG_UDP, "udp");
193 CSUM_UFLAG_BUFFER(uflag_6, TCA_CSUM_UPDATE_FLAG_UDPLITE, "udplite");
194 if (!uflag_count) {
195 uflag_1 = "?empty";
196 }
197
198 fprintf(f, "csum (%s%s%s%s%s%s) action %s\n",
199 uflag_1, uflag_2, uflag_3,
200 uflag_4, uflag_5, uflag_6,
70932006 201 action_n2a(sel->action));
53075318
RM
202 fprintf(f, "\tindex %u ref %d bind %d", sel->index, sel->refcnt,
203 sel->bindcnt);
3822cc98
GB
204
205 if (show_stats) {
206 if (tb[TCA_CSUM_TM]) {
207 struct tcf_t *tm = RTA_DATA(tb[TCA_CSUM_TM]);
32a121cb
SH
208
209 print_tm(f, tm);
3822cc98
GB
210 }
211 }
212 fprintf(f, "\n");
213
214 return 0;
215}
216
217struct action_util csum_action_util = {
218 .id = "csum",
219 .parse_aopt = parse_csum,
220 .print_aopt = print_csum,
221};