• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * q_dsmark.c		Differentiated Services field marking.
3  *
4  * Hacked 1998,1999 by Werner Almesberger, EPFL ICA
5  *
6  */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <syslog.h>
12 #include <fcntl.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <string.h>
17 
18 #include "utils.h"
19 #include "tc_util.h"
20 
21 
explain(void)22 static void explain(void)
23 {
24 	fprintf(stderr,"Usage: dsmark indices INDICES [ default_index DEFAULT_INDEX ] [ set_tc_index ]\n");
25 }
26 
27 
dsmark_parse_opt(struct qdisc_util * qu,int argc,char ** argv,struct nlmsghdr * n)28 static int dsmark_parse_opt(struct qdisc_util *qu, int argc, char **argv,
29     struct nlmsghdr *n)
30 {
31 	struct rtattr *tail;
32 	__u16 ind;
33 	char *end;
34 	int dflt, set_tc_index;
35 
36 	ind = set_tc_index = 0;
37 	dflt = -1;
38 	while (argc > 0) {
39 		if (!strcmp(*argv, "indices")) {
40 			NEXT_ARG();
41 			ind = strtoul(*argv, &end, 0);
42 			if (*end) {
43 				explain();
44 				return -1;
45 			}
46 		} else if (!strcmp(*argv,"default_index") || !strcmp(*argv,
47 		    "default")) {
48 			NEXT_ARG();
49 			dflt = strtoul(*argv, &end, 0);
50 			if (*end) {
51 				explain();
52 				return -1;
53 			}
54 		} else if (!strcmp(*argv,"set_tc_index")) {
55 			set_tc_index = 1;
56 		} else {
57 			explain();
58 			return -1;
59 		}
60 		argc--;
61 		argv++;
62 	}
63 	if (!ind) {
64 		explain();
65 		return -1;
66 	}
67 	tail = NLMSG_TAIL(n);
68 	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
69 	addattr_l(n, 1024, TCA_DSMARK_INDICES, &ind, sizeof(ind));
70 	if (dflt != -1) {
71 	    __u16 tmp = dflt;
72 
73 	    addattr_l(n, 1024, TCA_DSMARK_DEFAULT_INDEX, &tmp, sizeof(tmp));
74 	}
75 	if (set_tc_index) addattr_l(n, 1024, TCA_DSMARK_SET_TC_INDEX, NULL, 0);
76 	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
77 	return 0;
78 }
79 
80 
explain_class(void)81 static void explain_class(void)
82 {
83 	fprintf(stderr, "Usage: ... dsmark [ mask MASK ] [ value VALUE ]\n");
84 }
85 
86 
dsmark_parse_class_opt(struct qdisc_util * qu,int argc,char ** argv,struct nlmsghdr * n)87 static int dsmark_parse_class_opt(struct qdisc_util *qu, int argc, char **argv,
88    struct nlmsghdr *n)
89 {
90 	struct rtattr *tail;
91 	__u8 tmp;
92 	char *end;
93 
94 	tail = NLMSG_TAIL(n);
95 	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
96 	while (argc > 0) {
97 		if (!strcmp(*argv, "mask")) {
98 			NEXT_ARG();
99 			tmp = strtoul(*argv, &end, 0);
100 			if (*end) {
101 				explain_class();
102 				return -1;
103 			}
104 			addattr_l(n, 1024, TCA_DSMARK_MASK, &tmp, 1);
105 		} else if (!strcmp(*argv,"value")) {
106 			NEXT_ARG();
107 			tmp = strtoul(*argv, &end, 0);
108 			if (*end) {
109 				explain_class();
110 				return -1;
111 			}
112 			addattr_l(n, 1024, TCA_DSMARK_VALUE, &tmp, 1);
113 		} else {
114 			explain_class();
115 			return -1;
116 		}
117 		argc--;
118 		argv++;
119 	}
120 	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
121 	return 0;
122 }
123 
124 
125 
dsmark_print_opt(struct qdisc_util * qu,FILE * f,struct rtattr * opt)126 static int dsmark_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
127 {
128 	struct rtattr *tb[TCA_DSMARK_MAX+1];
129 
130 	if (!opt) return 0;
131 	parse_rtattr(tb, TCA_DSMARK_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt));
132 	if (tb[TCA_DSMARK_MASK]) {
133 		if (!RTA_PAYLOAD(tb[TCA_DSMARK_MASK]))
134 			fprintf(stderr, "dsmark: empty mask\n");
135 		else fprintf(f, "mask 0x%02x ",
136 			    rta_getattr_u8(tb[TCA_DSMARK_MASK]));
137 	}
138 	if (tb[TCA_DSMARK_VALUE]) {
139 		if (!RTA_PAYLOAD(tb[TCA_DSMARK_VALUE]))
140 			fprintf(stderr, "dsmark: empty value\n");
141 		else fprintf(f, "value 0x%02x ",
142 			    rta_getattr_u8(tb[TCA_DSMARK_VALUE]));
143 	}
144 	if (tb[TCA_DSMARK_INDICES]) {
145 		if (RTA_PAYLOAD(tb[TCA_DSMARK_INDICES]) < sizeof(__u16))
146 			fprintf(stderr, "dsmark: indices too short\n");
147 		else fprintf(f, "indices 0x%04x ",
148 			    rta_getattr_u16(tb[TCA_DSMARK_INDICES]));
149 	}
150 	if (tb[TCA_DSMARK_DEFAULT_INDEX]) {
151 		if (RTA_PAYLOAD(tb[TCA_DSMARK_DEFAULT_INDEX]) < sizeof(__u16))
152 			fprintf(stderr, "dsmark: default_index too short\n");
153 		else fprintf(f, "default_index 0x%04x ",
154 			    rta_getattr_u16(tb[TCA_DSMARK_DEFAULT_INDEX]));
155 	}
156 	if (tb[TCA_DSMARK_SET_TC_INDEX]) fprintf(f, "set_tc_index ");
157 	return 0;
158 }
159 
160 
161 struct qdisc_util dsmark_qdisc_util = {
162 	.id		= "dsmark",
163 	.parse_qopt	= dsmark_parse_opt,
164 	.print_qopt	= dsmark_print_opt,
165 	.parse_copt	= dsmark_parse_class_opt,
166 	.print_copt	= dsmark_print_opt,
167 };
168