Fix build break in 64bit architectures
[platform/upstream/iproute2.git] / tc / q_dsmark.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * q_dsmark.c           Differentiated Services field marking.
4  *
5  * Hacked 1998,1999 by Werner Almesberger, EPFL ICA
6  *
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <string.h>
17
18 #include "utils.h"
19 #include "tc_util.h"
20
21
22 static void explain(void)
23 {
24         fprintf(stderr,"Usage: dsmark indices INDICES [ default_index DEFAULT_INDEX ] [ set_tc_index ]\n");
25 }
26
27
28 static int dsmark_parse_opt(struct qdisc_util *qu, int argc, char **argv,
29         struct nlmsghdr *n, const char *dev)
30 {
31         struct rtattr *tail;
32         __u16 ind;
33         char *end;
34         int dflt, set_tc_index;
35
36         ind = set_tc_index = 0;
37         dflt = -1;
38         while (argc > 0) {
39                 if (!strcmp(*argv, "indices")) {
40                         NEXT_ARG();
41                         ind = strtoul(*argv, &end, 0);
42                         if (*end) {
43                                 explain();
44                                 return -1;
45                         }
46                 } else if (!strcmp(*argv,"default_index") || !strcmp(*argv,
47                     "default")) {
48                         NEXT_ARG();
49                         dflt = strtoul(*argv, &end, 0);
50                         if (*end) {
51                                 explain();
52                                 return -1;
53                         }
54                 } else if (!strcmp(*argv,"set_tc_index")) {
55                         set_tc_index = 1;
56                 } else {
57                         explain();
58                         return -1;
59                 }
60                 argc--;
61                 argv++;
62         }
63         if (!ind) {
64                 explain();
65                 return -1;
66         }
67         tail = addattr_nest(n, 1024, TCA_OPTIONS);
68         addattr_l(n, 1024, TCA_DSMARK_INDICES, &ind, sizeof(ind));
69         if (dflt != -1) {
70             __u16 tmp = dflt;
71
72             addattr_l(n, 1024, TCA_DSMARK_DEFAULT_INDEX, &tmp, sizeof(tmp));
73         }
74         if (set_tc_index)
75                 addattr_l(n, 1024, TCA_DSMARK_SET_TC_INDEX, NULL, 0);
76         addattr_nest_end(n, tail);
77         return 0;
78 }
79
80
81 static void explain_class(void)
82 {
83         fprintf(stderr, "Usage: ... dsmark [ mask MASK ] [ value VALUE ]\n");
84 }
85
86
87 static int dsmark_parse_class_opt(struct qdisc_util *qu, int argc, char **argv,
88         struct nlmsghdr *n, const char *dev)
89 {
90         struct rtattr *tail;
91         __u8 tmp;
92         char *end;
93
94         tail = addattr_nest(n, 1024, TCA_OPTIONS);
95         while (argc > 0) {
96                 if (!strcmp(*argv, "mask")) {
97                         NEXT_ARG();
98                         tmp = strtoul(*argv, &end, 0);
99                         if (*end) {
100                                 explain_class();
101                                 return -1;
102                         }
103                         addattr_l(n, 1024, TCA_DSMARK_MASK, &tmp, 1);
104                 } else if (!strcmp(*argv,"value")) {
105                         NEXT_ARG();
106                         tmp = strtoul(*argv, &end, 0);
107                         if (*end) {
108                                 explain_class();
109                                 return -1;
110                         }
111                         addattr_l(n, 1024, TCA_DSMARK_VALUE, &tmp, 1);
112                 } else {
113                         explain_class();
114                         return -1;
115                 }
116                 argc--;
117                 argv++;
118         }
119         addattr_nest_end(n, tail);
120         return 0;
121 }
122
123
124
125 static int dsmark_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
126 {
127         struct rtattr *tb[TCA_DSMARK_MAX+1];
128
129         if (!opt) return 0;
130         parse_rtattr(tb, TCA_DSMARK_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt));
131         if (tb[TCA_DSMARK_MASK]) {
132                 if (!RTA_PAYLOAD(tb[TCA_DSMARK_MASK]))
133                         fprintf(stderr, "dsmark: empty mask\n");
134                 else fprintf(f, "mask 0x%02x ",
135                             rta_getattr_u8(tb[TCA_DSMARK_MASK]));
136         }
137         if (tb[TCA_DSMARK_VALUE]) {
138                 if (!RTA_PAYLOAD(tb[TCA_DSMARK_VALUE]))
139                         fprintf(stderr, "dsmark: empty value\n");
140                 else fprintf(f, "value 0x%02x ",
141                             rta_getattr_u8(tb[TCA_DSMARK_VALUE]));
142         }
143         if (tb[TCA_DSMARK_INDICES]) {
144                 if (RTA_PAYLOAD(tb[TCA_DSMARK_INDICES]) < sizeof(__u16))
145                         fprintf(stderr, "dsmark: indices too short\n");
146                 else fprintf(f, "indices 0x%04x ",
147                             rta_getattr_u16(tb[TCA_DSMARK_INDICES]));
148         }
149         if (tb[TCA_DSMARK_DEFAULT_INDEX]) {
150                 if (RTA_PAYLOAD(tb[TCA_DSMARK_DEFAULT_INDEX]) < sizeof(__u16))
151                         fprintf(stderr, "dsmark: default_index too short\n");
152                 else fprintf(f, "default_index 0x%04x ",
153                             rta_getattr_u16(tb[TCA_DSMARK_DEFAULT_INDEX]));
154         }
155         if (tb[TCA_DSMARK_SET_TC_INDEX]) fprintf(f, "set_tc_index ");
156         return 0;
157 }
158
159
160 struct qdisc_util dsmark_qdisc_util = {
161         .id             = "dsmark",
162         .parse_qopt     = dsmark_parse_opt,
163         .print_qopt     = dsmark_print_opt,
164         .parse_copt     = dsmark_parse_class_opt,
165         .print_copt     = dsmark_print_opt,
166 };