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