]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_atm.c
lib: introduce print_nl
[mirror_iproute2.git] / tc / q_atm.c
1 /* SPDX-License-Identifier: GPL-2.0 */
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>
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
29
30 static int atm_parse_opt(struct qdisc_util *qu, int argc, char **argv,
31 struct nlmsghdr *n, const char *dev)
32 {
33 if (argc) {
34 fprintf(stderr, "Usage: atm\n");
35 return -1;
36 }
37 return 0;
38 }
39
40
41 static void explain(void)
42 {
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");
45 }
46
47
48 static int atm_parse_class_opt(struct qdisc_util *qu, int argc, char **argv,
49 struct nlmsghdr *n, const char *dev)
50 {
51 struct sockaddr_atmsvc addr = {};
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
62 (void) text2qos("aal5,ubr:sdu=9180,rx:none", &qos, 0);
63 (void) text2sap("blli:l2=iso8802", &sap, 0);
64 while (argc > 0) {
65 if (!strcmp(*argv, "pvc")) {
66 NEXT_ARG();
67 if (text2atm(*argv, (struct sockaddr *) &addr,
68 sizeof(addr), T2A_PVC | T2A_NAME) < 0) {
69 explain();
70 return -1;
71 }
72 } else if (!strcmp(*argv,"svc")) {
73 NEXT_ARG();
74 if (text2atm(*argv, (struct sockaddr *) &addr,
75 sizeof(addr), T2A_SVC | T2A_NAME) < 0) {
76 explain();
77 return -1;
78 }
79 } else if (!strcmp(*argv,"qos")) {
80 NEXT_ARG();
81 if (text2qos(*argv, &qos, 0) < 0) {
82 explain();
83 return -1;
84 }
85 } else if (!strcmp(*argv,"sndbuf")) {
86 char *end;
87
88 NEXT_ARG();
89 sndbuf = strtol(*argv, &end, 0);
90 if (*end) {
91 explain();
92 return -1;
93 }
94 } else if (!strcmp(*argv,"sap")) {
95 NEXT_ARG();
96 if (addr.sas_family != AF_ATMSVC ||
97 text2sap(*argv, &sap, T2A_NAME) < 0) {
98 explain();
99 return -1;
100 }
101 } else if (!strcmp(*argv,"hdr")) {
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) {
111 fprintf(stderr, "header is too long\n");
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 }
120 sscanf(walk, "%2x", &tmp);
121 *ptr++ = tmp;
122 walk++;
123 }
124 hdr_len = ptr-hdr;
125 } else if (!strcmp(*argv,"excess")) {
126 NEXT_ARG();
127 if (!strcmp(*argv, "clp")) excess = 0;
128 else if (get_tc_classid(&excess, *argv)) {
129 explain();
130 return -1;
131 }
132 } else if (!strcmp(*argv,"clip")) {
133 set_clip = 1;
134 } else {
135 explain();
136 return 1;
137 }
138 argc--;
139 argv++;
140 }
141 s = socket(addr.sas_family, SOCK_DGRAM, 0);
142 if (s < 0) {
143 perror("socket");
144 return -1;
145 }
146 if (setsockopt(s, SOL_ATM, SO_ATMQOS, &qos, sizeof(qos)) < 0) {
147 perror("SO_ATMQOS");
148 return -1;
149 }
150 if (sndbuf)
151 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)) < 0) {
152 perror("SO_SNDBUF");
153 return -1;
154 }
155 if (addr.sas_family == AF_ATMSVC && setsockopt(s, SOL_ATM, SO_ATMSAP,
156 &sap, sizeof(sap)) < 0) {
157 perror("SO_ATMSAP");
158 return -1;
159 }
160 if (connect(s, (struct sockaddr *) &addr, addr.sas_family == AF_ATMPVC ?
161 sizeof(struct sockaddr_atmpvc) : sizeof(addr)) < 0) {
162 perror("connect");
163 return -1;
164 }
165 if (set_clip)
166 if (ioctl(s, ATMARP_MKIP, 0) < 0) {
167 perror("ioctl ATMARP_MKIP");
168 return -1;
169 }
170 tail = addattr_nest(n, 1024, TCA_OPTIONS);
171 addattr_l(n, 1024, TCA_ATM_FD, &s, sizeof(s));
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);
177 return 0;
178 }
179
180
181
182 static 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
187 if (opt == NULL)
188 return 0;
189
190 parse_rtattr_nested(tb, TCA_ATM_MAX, opt);
191 if (tb[TCA_ATM_ADDR]) {
192 if (RTA_PAYLOAD(tb[TCA_ATM_ADDR]) <
193 sizeof(struct sockaddr_atmpvc))
194 fprintf(stderr, "ATM: address too short\n");
195 else {
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);
200 }
201 }
202 if (tb[TCA_ATM_HDR]) {
203 int i;
204 const __u8 *hdr = RTA_DATA(tb[TCA_ATM_HDR]);
205
206 fprintf(f, "hdr");
207 for (i = 0; i < RTA_PAYLOAD(tb[TCA_ATM_HDR]); i++)
208 fprintf(f, "%c%02x", i ? '.' : ' ', hdr[i]);
209 if (!i) fprintf(f, " .");
210 fprintf(f, " ");
211 }
212 if (tb[TCA_ATM_EXCESS]) {
213 __u32 excess;
214
215 if (RTA_PAYLOAD(tb[TCA_ATM_EXCESS]) < sizeof(excess))
216 fprintf(stderr, "ATM: excess class ID too short\n");
217 else {
218 excess = rta_getattr_u32(tb[TCA_ATM_EXCESS]);
219 if (!excess) fprintf(f, "excess clp ");
220 else {
221 char buf[64];
222
223 print_tc_classid(buf, sizeof(buf), excess);
224 fprintf(f, "excess %s ", buf);
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))
233 fprintf(stderr, "ATM: state field too short\n");
234 else {
235 state = rta_getattr_u32(tb[TCA_ATM_STATE]);
236 fprintf(f, "%s ", map[state]);
237 }
238 }
239 return 0;
240 }
241
242
243 struct qdisc_util atm_qdisc_util = {
244 .id = "atm",
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,
249 };