]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_atm.c
tc: Remove pointless assignments in batch()
[mirror_iproute2.git] / tc / q_atm.c
CommitLineData
6054c1eb 1/* SPDX-License-Identifier: GPL-2.0 */
aba5acdf
SH
2/*
3 * q_atm.c ATM.
4 *
5 * Hacked 1998-2000 by Werner Almesberger, EPFL ICA
6 *
7 */
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <unistd.h>
12#include <ctype.h>
aba5acdf
SH
13#include <fcntl.h>
14#include <sys/socket.h>
15#include <sys/ioctl.h>
16#include <netinet/in.h>
17#include <arpa/inet.h>
18#include <string.h>
19#include <atm.h>
20#include <linux/atmdev.h>
21#include <linux/atmarp.h>
22
23#include "utils.h"
24#include "tc_util.h"
25
26
27#define MAX_HDR_LEN 64
28
aba5acdf 29
859af0a5
SH
30static int atm_parse_opt(struct qdisc_util *qu, int argc, char **argv,
31 struct nlmsghdr *n, const char *dev)
aba5acdf
SH
32{
33 if (argc) {
32a121cb 34 fprintf(stderr, "Usage: atm\n");
aba5acdf
SH
35 return -1;
36 }
37 return 0;
38}
39
40
41static void explain(void)
42{
32a121cb
SH
43 fprintf(stderr, "Usage: ... atm ( pvc ADDR | svc ADDR [ sap SAP ] ) [ qos QOS ] [ sndbuf BYTES ]\n");
44 fprintf(stderr, " [ hdr HEX... ] [ excess ( CLASSID | clp ) ] [ clip ]\n");
aba5acdf
SH
45}
46
47
48static int atm_parse_class_opt(struct qdisc_util *qu, int argc, char **argv,
927e3cfb 49 struct nlmsghdr *n, const char *dev)
aba5acdf 50{
d17b136f 51 struct sockaddr_atmsvc addr = {};
aba5acdf
SH
52 struct atm_qos qos;
53 struct atm_sap sap;
54 unsigned char hdr[MAX_HDR_LEN];
55 __u32 excess = 0;
56 struct rtattr *tail;
57 int sndbuf = 0;
58 int hdr_len = -1;
59 int set_clip = 0;
60 int s;
61
32a121cb
SH
62 (void) text2qos("aal5,ubr:sdu=9180,rx:none", &qos, 0);
63 (void) text2sap("blli:l2=iso8802", &sap, 0);
aba5acdf 64 while (argc > 0) {
32a121cb 65 if (!strcmp(*argv, "pvc")) {
aba5acdf 66 NEXT_ARG();
32a121cb
SH
67 if (text2atm(*argv, (struct sockaddr *) &addr,
68 sizeof(addr), T2A_PVC | T2A_NAME) < 0) {
aba5acdf
SH
69 explain();
70 return -1;
71 }
32a121cb 72 } else if (!strcmp(*argv,"svc")) {
aba5acdf 73 NEXT_ARG();
32a121cb
SH
74 if (text2atm(*argv, (struct sockaddr *) &addr,
75 sizeof(addr), T2A_SVC | T2A_NAME) < 0) {
aba5acdf
SH
76 explain();
77 return -1;
78 }
32a121cb 79 } else if (!strcmp(*argv,"qos")) {
aba5acdf 80 NEXT_ARG();
32a121cb 81 if (text2qos(*argv, &qos, 0) < 0) {
aba5acdf
SH
82 explain();
83 return -1;
84 }
32a121cb 85 } else if (!strcmp(*argv,"sndbuf")) {
aba5acdf
SH
86 char *end;
87
88 NEXT_ARG();
32a121cb 89 sndbuf = strtol(*argv, &end, 0);
aba5acdf
SH
90 if (*end) {
91 explain();
92 return -1;
93 }
32a121cb 94 } else if (!strcmp(*argv,"sap")) {
aba5acdf
SH
95 NEXT_ARG();
96 if (addr.sas_family != AF_ATMSVC ||
32a121cb 97 text2sap(*argv, &sap, T2A_NAME) < 0) {
aba5acdf
SH
98 explain();
99 return -1;
100 }
32a121cb 101 } else if (!strcmp(*argv,"hdr")) {
aba5acdf
SH
102 unsigned char *ptr;
103 char *walk;
104
105 NEXT_ARG();
106 ptr = hdr;
107 for (walk = *argv; *walk; walk++) {
108 int tmp;
109
110 if (ptr == hdr+MAX_HDR_LEN) {
32a121cb 111 fprintf(stderr, "header is too long\n");
aba5acdf
SH
112 return -1;
113 }
114 if (*walk == '.') continue;
115 if (!isxdigit(walk[0]) || !walk[1] ||
116 !isxdigit(walk[1])) {
117 explain();
118 return -1;
119 }
32a121cb 120 sscanf(walk, "%2x", &tmp);
aba5acdf
SH
121 *ptr++ = tmp;
122 walk++;
123 }
124 hdr_len = ptr-hdr;
32a121cb 125 } else if (!strcmp(*argv,"excess")) {
aba5acdf 126 NEXT_ARG();
32a121cb
SH
127 if (!strcmp(*argv, "clp")) excess = 0;
128 else if (get_tc_classid(&excess, *argv)) {
aba5acdf
SH
129 explain();
130 return -1;
131 }
32a121cb 132 } else if (!strcmp(*argv,"clip")) {
aba5acdf 133 set_clip = 1;
32a121cb 134 } else {
aba5acdf
SH
135 explain();
136 return 1;
137 }
138 argc--;
139 argv++;
140 }
32a121cb 141 s = socket(addr.sas_family, SOCK_DGRAM, 0);
aba5acdf
SH
142 if (s < 0) {
143 perror("socket");
144 return -1;
145 }
32a121cb 146 if (setsockopt(s, SOL_ATM, SO_ATMQOS, &qos, sizeof(qos)) < 0) {
aba5acdf
SH
147 perror("SO_ATMQOS");
148 return -1;
149 }
150 if (sndbuf)
32a121cb 151 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)) < 0) {
aba5acdf
SH
152 perror("SO_SNDBUF");
153 return -1;
154 }
32a121cb
SH
155 if (addr.sas_family == AF_ATMSVC && setsockopt(s, SOL_ATM, SO_ATMSAP,
156 &sap, sizeof(sap)) < 0) {
aba5acdf
SH
157 perror("SO_ATMSAP");
158 return -1;
159 }
32a121cb 160 if (connect(s, (struct sockaddr *) &addr, addr.sas_family == AF_ATMPVC ?
aba5acdf
SH
161 sizeof(struct sockaddr_atmpvc) : sizeof(addr)) < 0) {
162 perror("connect");
163 return -1;
164 }
165 if (set_clip)
32a121cb 166 if (ioctl(s, ATMARP_MKIP, 0) < 0) {
aba5acdf
SH
167 perror("ioctl ATMARP_MKIP");
168 return -1;
169 }
c14f9d92 170 tail = addattr_nest(n, 1024, TCA_OPTIONS);
32a121cb 171 addattr_l(n, 1024, TCA_ATM_FD, &s, sizeof(s));
c14f9d92
SP
172 if (excess)
173 addattr_l(n, 1024, TCA_ATM_EXCESS, &excess, sizeof(excess));
174 if (hdr_len != -1)
175 addattr_l(n, 1024, TCA_ATM_HDR, hdr, hdr_len);
176 addattr_nest_end(n, tail);
aba5acdf
SH
177 return 0;
178}
179
180
181
182static int atm_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
183{
184 struct rtattr *tb[TCA_ATM_MAX+1];
185 char buffer[MAX_ATM_ADDR_LEN+1];
186
3b3ecd31 187 if (opt == NULL)
188 return 0;
189
190 parse_rtattr_nested(tb, TCA_ATM_MAX, opt);
aba5acdf
SH
191 if (tb[TCA_ATM_ADDR]) {
192 if (RTA_PAYLOAD(tb[TCA_ATM_ADDR]) <
193 sizeof(struct sockaddr_atmpvc))
32a121cb 194 fprintf(stderr, "ATM: address too short\n");
aba5acdf 195 else {
32a121cb
SH
196 if (atm2text(buffer, MAX_ATM_ADDR_LEN,
197 RTA_DATA(tb[TCA_ATM_ADDR]), A2T_PRETTY | A2T_NAME) <
198 0) fprintf(stderr, "atm2text error\n");
199 fprintf(f, "pvc %s ", buffer);
aba5acdf
SH
200 }
201 }
202 if (tb[TCA_ATM_HDR]) {
203 int i;
ff24746c 204 const __u8 *hdr = RTA_DATA(tb[TCA_ATM_HDR]);
aba5acdf 205
32a121cb 206 fprintf(f, "hdr");
aba5acdf 207 for (i = 0; i < RTA_PAYLOAD(tb[TCA_ATM_HDR]); i++)
32a121cb
SH
208 fprintf(f, "%c%02x", i ? '.' : ' ', hdr[i]);
209 if (!i) fprintf(f, " .");
210 fprintf(f, " ");
aba5acdf
SH
211 }
212 if (tb[TCA_ATM_EXCESS]) {
213 __u32 excess;
214
215 if (RTA_PAYLOAD(tb[TCA_ATM_EXCESS]) < sizeof(excess))
32a121cb 216 fprintf(stderr, "ATM: excess class ID too short\n");
aba5acdf 217 else {
ff24746c 218 excess = rta_getattr_u32(tb[TCA_ATM_EXCESS]);
32a121cb 219 if (!excess) fprintf(f, "excess clp ");
aba5acdf
SH
220 else {
221 char buf[64];
222
32a121cb
SH
223 print_tc_classid(buf, sizeof(buf), excess);
224 fprintf(f, "excess %s ", buf);
aba5acdf
SH
225 }
226 }
227 }
228 if (tb[TCA_ATM_STATE]) {
229 static const char *map[] = { ATM_VS2TXT_MAP };
230 int state;
231
232 if (RTA_PAYLOAD(tb[TCA_ATM_STATE]) < sizeof(state))
32a121cb 233 fprintf(stderr, "ATM: state field too short\n");
aba5acdf 234 else {
a59b6162 235 state = rta_getattr_u32(tb[TCA_ATM_STATE]);
32a121cb 236 fprintf(f, "%s ", map[state]);
aba5acdf
SH
237 }
238 }
239 return 0;
240}
241
242
95812b56 243struct qdisc_util atm_qdisc_util = {
32a121cb 244 .id = "atm",
f2f99e2e
SH
245 .parse_qopt = atm_parse_opt,
246 .print_qopt = atm_print_opt,
247 .parse_copt = atm_parse_class_opt,
248 .print_copt = atm_print_opt,
aba5acdf 249};