]>
Commit | Line | Data |
---|---|---|
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 |
30 | static 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 | ||
41 | static 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 | ||
48 | static 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 | } | |
228569c3 | 170 | tail = NLMSG_TAIL(n); |
32a121cb SH |
171 | addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); |
172 | addattr_l(n, 1024, TCA_ATM_FD, &s, sizeof(s)); | |
173 | if (excess) addattr_l(n, 1024, TCA_ATM_EXCESS, &excess, sizeof(excess)); | |
174 | if (hdr_len != -1) addattr_l(n, 1024, TCA_ATM_HDR, hdr, hdr_len); | |
228569c3 | 175 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
aba5acdf SH |
176 | return 0; |
177 | } | |
178 | ||
179 | ||
180 | ||
181 | static int atm_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) | |
182 | { | |
183 | struct rtattr *tb[TCA_ATM_MAX+1]; | |
184 | char buffer[MAX_ATM_ADDR_LEN+1]; | |
185 | ||
3b3ecd31 | 186 | if (opt == NULL) |
187 | return 0; | |
188 | ||
189 | parse_rtattr_nested(tb, TCA_ATM_MAX, opt); | |
aba5acdf SH |
190 | if (tb[TCA_ATM_ADDR]) { |
191 | if (RTA_PAYLOAD(tb[TCA_ATM_ADDR]) < | |
192 | sizeof(struct sockaddr_atmpvc)) | |
32a121cb | 193 | fprintf(stderr, "ATM: address too short\n"); |
aba5acdf | 194 | else { |
32a121cb SH |
195 | if (atm2text(buffer, MAX_ATM_ADDR_LEN, |
196 | RTA_DATA(tb[TCA_ATM_ADDR]), A2T_PRETTY | A2T_NAME) < | |
197 | 0) fprintf(stderr, "atm2text error\n"); | |
198 | fprintf(f, "pvc %s ", buffer); | |
aba5acdf SH |
199 | } |
200 | } | |
201 | if (tb[TCA_ATM_HDR]) { | |
202 | int i; | |
ff24746c | 203 | const __u8 *hdr = RTA_DATA(tb[TCA_ATM_HDR]); |
aba5acdf | 204 | |
32a121cb | 205 | fprintf(f, "hdr"); |
aba5acdf | 206 | for (i = 0; i < RTA_PAYLOAD(tb[TCA_ATM_HDR]); i++) |
32a121cb SH |
207 | fprintf(f, "%c%02x", i ? '.' : ' ', hdr[i]); |
208 | if (!i) fprintf(f, " ."); | |
209 | fprintf(f, " "); | |
aba5acdf SH |
210 | } |
211 | if (tb[TCA_ATM_EXCESS]) { | |
212 | __u32 excess; | |
213 | ||
214 | if (RTA_PAYLOAD(tb[TCA_ATM_EXCESS]) < sizeof(excess)) | |
32a121cb | 215 | fprintf(stderr, "ATM: excess class ID too short\n"); |
aba5acdf | 216 | else { |
ff24746c | 217 | excess = rta_getattr_u32(tb[TCA_ATM_EXCESS]); |
32a121cb | 218 | if (!excess) fprintf(f, "excess clp "); |
aba5acdf SH |
219 | else { |
220 | char buf[64]; | |
221 | ||
32a121cb SH |
222 | print_tc_classid(buf, sizeof(buf), excess); |
223 | fprintf(f, "excess %s ", buf); | |
aba5acdf SH |
224 | } |
225 | } | |
226 | } | |
227 | if (tb[TCA_ATM_STATE]) { | |
228 | static const char *map[] = { ATM_VS2TXT_MAP }; | |
229 | int state; | |
230 | ||
231 | if (RTA_PAYLOAD(tb[TCA_ATM_STATE]) < sizeof(state)) | |
32a121cb | 232 | fprintf(stderr, "ATM: state field too short\n"); |
aba5acdf | 233 | else { |
a59b6162 | 234 | state = rta_getattr_u32(tb[TCA_ATM_STATE]); |
32a121cb | 235 | fprintf(f, "%s ", map[state]); |
aba5acdf SH |
236 | } |
237 | } | |
238 | return 0; | |
239 | } | |
240 | ||
241 | ||
95812b56 | 242 | struct qdisc_util atm_qdisc_util = { |
32a121cb | 243 | .id = "atm", |
f2f99e2e SH |
244 | .parse_qopt = atm_parse_opt, |
245 | .print_qopt = atm_print_opt, | |
246 | .parse_copt = atm_parse_class_opt, | |
247 | .print_copt = atm_print_opt, | |
aba5acdf | 248 | }; |