]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_csum.c
tc: m_action: Improve conversion to C99 style initializers
[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{
88 struct tc_csum sel;
89
90 int argc = *argc_p;
91 char **argv = *argv_p;
92 int ok = 0;
93 struct rtattr *tail;
94
95 memset(&sel, 0, sizeof(sel));
96
97 while (argc > 0) {
98 if (matches(*argv, "csum") == 0) {
99 NEXT_ARG();
100 if (parse_csum_args(&argc, &argv, &sel)) {
101 fprintf(stderr, "Illegal csum construct (%s)\n",
102 *argv);
103 explain();
104 return -1;
105 }
106 ok++;
107 continue;
108 } else if (matches(*argv, "help") == 0) {
109 usage();
32a121cb 110 } else {
3822cc98
GB
111 break;
112 }
113 }
114
115 if (!ok) {
116 explain();
117 return -1;
118 }
119
120 if (sel.update_flags == 0) {
121 fprintf(stderr, "Illegal csum construct, empty <UPDATE> list\n");
122 return -1;
123 }
124
125 if (argc) {
126 if (matches(*argv, "reclassify") == 0) {
127 sel.action = TC_ACT_RECLASSIFY;
128 argc--;
129 argv++;
130 } else if (matches(*argv, "pipe") == 0) {
131 sel.action = TC_ACT_PIPE;
132 argc--;
133 argv++;
134 } else if (matches(*argv, "drop") == 0 ||
135 matches(*argv, "shot") == 0) {
136 sel.action = TC_ACT_SHOT;
137 argc--;
138 argv++;
139 } else if (matches(*argv, "continue") == 0) {
140 sel.action = TC_ACT_UNSPEC;
141 argc--;
142 argv++;
43726b75
JHS
143 } else if (matches(*argv, "pass") == 0 ||
144 matches(*argv, "ok") == 0) {
3822cc98
GB
145 sel.action = TC_ACT_OK;
146 argc--;
147 argv++;
148 }
149 }
150
151 if (argc) {
152 if (matches(*argv, "index") == 0) {
153 NEXT_ARG();
154 if (get_u32(&sel.index, *argv, 10)) {
155 fprintf(stderr, "Illegal \"index\" (%s) <csum>\n",
156 *argv);
157 return -1;
158 }
159 argc--;
160 argv++;
161 }
162 }
163
164 tail = NLMSG_TAIL(n);
165 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
166 addattr_l(n, MAX_MSG, TCA_CSUM_PARMS, &sel, sizeof(sel));
167 tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail;
168
169 *argc_p = argc;
170 *argv_p = argv;
171
172 return 0;
173}
174
175static int
32a121cb 176print_csum(struct action_util *au, FILE *f, struct rtattr *arg)
3822cc98
GB
177{
178 struct tc_csum *sel;
179
180 struct rtattr *tb[TCA_CSUM_MAX + 1];
181
182 char *uflag_1 = "";
183 char *uflag_2 = "";
184 char *uflag_3 = "";
185 char *uflag_4 = "";
186 char *uflag_5 = "";
187 char *uflag_6 = "";
32a121cb 188
3822cc98
GB
189 SPRINT_BUF(action_buf);
190
191 int uflag_count = 0;
192
193 if (arg == NULL)
194 return -1;
195
196 parse_rtattr_nested(tb, TCA_CSUM_MAX, arg);
197
198 if (tb[TCA_CSUM_PARMS] == NULL) {
199 fprintf(f, "[NULL csum parameters]");
200 return -1;
201 }
202 sel = RTA_DATA(tb[TCA_CSUM_PARMS]);
203
204 if (sel->update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
205 uflag_1 = "iph";
206 uflag_count++;
207 }
208 #define CSUM_UFLAG_BUFFER(flag_buffer, flag_value, flag_string) \
209 do { \
210 if (sel->update_flags & flag_value) { \
211 flag_buffer = uflag_count > 0 ? \
212 ", " flag_string : flag_string; \
213 uflag_count++; \
214 } \
32a121cb 215 } while (0)
3822cc98
GB
216 CSUM_UFLAG_BUFFER(uflag_2, TCA_CSUM_UPDATE_FLAG_ICMP, "icmp");
217 CSUM_UFLAG_BUFFER(uflag_3, TCA_CSUM_UPDATE_FLAG_IGMP, "igmp");
90d98edf 218 CSUM_UFLAG_BUFFER(uflag_4, TCA_CSUM_UPDATE_FLAG_TCP, "tcp");
3822cc98
GB
219 CSUM_UFLAG_BUFFER(uflag_5, TCA_CSUM_UPDATE_FLAG_UDP, "udp");
220 CSUM_UFLAG_BUFFER(uflag_6, TCA_CSUM_UPDATE_FLAG_UDPLITE, "udplite");
221 if (!uflag_count) {
222 uflag_1 = "?empty";
223 }
224
225 fprintf(f, "csum (%s%s%s%s%s%s) action %s\n",
226 uflag_1, uflag_2, uflag_3,
227 uflag_4, uflag_5, uflag_6,
228 action_n2a(sel->action, action_buf, sizeof(action_buf)));
229 fprintf(f, "\tindex %d ref %d bind %d", sel->index, sel->refcnt, sel->bindcnt);
230
231 if (show_stats) {
232 if (tb[TCA_CSUM_TM]) {
233 struct tcf_t *tm = RTA_DATA(tb[TCA_CSUM_TM]);
32a121cb
SH
234
235 print_tm(f, tm);
3822cc98
GB
236 }
237 }
238 fprintf(f, "\n");
239
240 return 0;
241}
242
243struct action_util csum_action_util = {
244 .id = "csum",
245 .parse_aopt = parse_csum,
246 .print_aopt = print_csum,
247};