osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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> |
| 12 | #include <syslog.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 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 29 | |
| 30 | static int atm_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n) |
| 31 | { |
| 32 | if (argc) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 33 | fprintf(stderr, "Usage: atm\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 34 | return -1; |
| 35 | } |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | static void explain(void) |
| 41 | { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 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"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
| 47 | static int atm_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, |
| 48 | struct nlmsghdr *n) |
| 49 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 50 | struct sockaddr_atmsvc addr = {}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 61 | (void) text2qos("aal5,ubr:sdu=9180,rx:none", &qos, 0); |
| 62 | (void) text2sap("blli:l2=iso8802", &sap, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 63 | while (argc > 0) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 64 | if (!strcmp(*argv, "pvc")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 65 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 66 | if (text2atm(*argv, (struct sockaddr *) &addr, |
| 67 | sizeof(addr), T2A_PVC | T2A_NAME) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 68 | explain(); |
| 69 | return -1; |
| 70 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 71 | } else if (!strcmp(*argv,"svc")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 72 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 73 | if (text2atm(*argv, (struct sockaddr *) &addr, |
| 74 | sizeof(addr), T2A_SVC | T2A_NAME) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 75 | explain(); |
| 76 | return -1; |
| 77 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 78 | } else if (!strcmp(*argv,"qos")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 79 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 80 | if (text2qos(*argv, &qos, 0) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 81 | explain(); |
| 82 | return -1; |
| 83 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 84 | } else if (!strcmp(*argv,"sndbuf")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 85 | char *end; |
| 86 | |
| 87 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 88 | sndbuf = strtol(*argv, &end, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 89 | if (*end) { |
| 90 | explain(); |
| 91 | return -1; |
| 92 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 93 | } else if (!strcmp(*argv,"sap")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 94 | NEXT_ARG(); |
| 95 | if (addr.sas_family != AF_ATMSVC || |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 96 | text2sap(*argv, &sap, T2A_NAME) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 97 | explain(); |
| 98 | return -1; |
| 99 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 100 | } else if (!strcmp(*argv,"hdr")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 110 | fprintf(stderr, "header is too long\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 119 | sscanf(walk, "%2x", &tmp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 120 | *ptr++ = tmp; |
| 121 | walk++; |
| 122 | } |
| 123 | hdr_len = ptr-hdr; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 124 | } else if (!strcmp(*argv,"excess")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 125 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 126 | if (!strcmp(*argv, "clp")) excess = 0; |
| 127 | else if (get_tc_classid(&excess, *argv)) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 128 | explain(); |
| 129 | return -1; |
| 130 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 131 | } else if (!strcmp(*argv,"clip")) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 132 | set_clip = 1; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 133 | } else { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 134 | explain(); |
| 135 | return 1; |
| 136 | } |
| 137 | argc--; |
| 138 | argv++; |
| 139 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 140 | s = socket(addr.sas_family, SOCK_DGRAM, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 141 | if (s < 0) { |
| 142 | perror("socket"); |
| 143 | return -1; |
| 144 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 145 | if (setsockopt(s, SOL_ATM, SO_ATMQOS, &qos, sizeof(qos)) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 146 | perror("SO_ATMQOS"); |
| 147 | return -1; |
| 148 | } |
| 149 | if (sndbuf) |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 150 | if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 151 | perror("SO_SNDBUF"); |
| 152 | return -1; |
| 153 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 154 | if (addr.sas_family == AF_ATMSVC && setsockopt(s, SOL_ATM, SO_ATMSAP, |
| 155 | &sap, sizeof(sap)) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 156 | perror("SO_ATMSAP"); |
| 157 | return -1; |
| 158 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 159 | if (connect(s, (struct sockaddr *) &addr, addr.sas_family == AF_ATMPVC ? |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 160 | sizeof(struct sockaddr_atmpvc) : sizeof(addr)) < 0) { |
| 161 | perror("connect"); |
| 162 | return -1; |
| 163 | } |
| 164 | if (set_clip) |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 165 | if (ioctl(s, ATMARP_MKIP, 0) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 166 | perror("ioctl ATMARP_MKIP"); |
| 167 | return -1; |
| 168 | } |
4!tgraf | 228569c | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 169 | tail = NLMSG_TAIL(n); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 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); |
4!tgraf | 228569c | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 174 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | static 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 | |
4!tgraf | 3b3ecd3 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 185 | if (opt == NULL) |
| 186 | return 0; |
| 187 | |
| 188 | parse_rtattr_nested(tb, TCA_ATM_MAX, opt); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 189 | if (tb[TCA_ATM_ADDR]) { |
| 190 | if (RTA_PAYLOAD(tb[TCA_ATM_ADDR]) < |
| 191 | sizeof(struct sockaddr_atmpvc)) |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 192 | fprintf(stderr, "ATM: address too short\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 193 | else { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 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); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | if (tb[TCA_ATM_HDR]) { |
| 201 | int i; |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 202 | const __u8 *hdr = RTA_DATA(tb[TCA_ATM_HDR]); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 203 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 204 | fprintf(f, "hdr"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 205 | for (i = 0; i < RTA_PAYLOAD(tb[TCA_ATM_HDR]); i++) |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 206 | fprintf(f, "%c%02x", i ? '.' : ' ', hdr[i]); |
| 207 | if (!i) fprintf(f, " ."); |
| 208 | fprintf(f, " "); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 209 | } |
| 210 | if (tb[TCA_ATM_EXCESS]) { |
| 211 | __u32 excess; |
| 212 | |
| 213 | if (RTA_PAYLOAD(tb[TCA_ATM_EXCESS]) < sizeof(excess)) |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 214 | fprintf(stderr, "ATM: excess class ID too short\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 215 | else { |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 216 | excess = rta_getattr_u32(tb[TCA_ATM_EXCESS]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 217 | if (!excess) fprintf(f, "excess clp "); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 218 | else { |
| 219 | char buf[64]; |
| 220 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 221 | print_tc_classid(buf, sizeof(buf), excess); |
| 222 | fprintf(f, "excess %s ", buf); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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)) |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 231 | fprintf(stderr, "ATM: state field too short\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 232 | else { |
| 233 | state = *(int *) RTA_DATA(tb[TCA_ATM_STATE]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 234 | fprintf(f, "%s ", map[state]); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | |
net[shemminger]!kaber | 95812b5 | 2004-09-28 18:35:49 +0000 | [diff] [blame] | 241 | struct qdisc_util atm_qdisc_util = { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 242 | .id = "atm", |
osdl.net!shemminger | f2f99e2 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 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, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 247 | }; |