1 /*
2  * m_skbmod.c	skb modifier action module
3  *
4  *		This program is free software; you can distribute it and/or
5  *		modify it under the terms of the GNU General Public License
6  *		as published by the Free Software Foundation; either version
7  *		2 of the License, or (at your option) any later version.
8  *
9  * Authors:  J Hadi Salim (jhs@mojatatu.com)
10  *
11  */
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
22 #include <linux/netdevice.h>
23 
24 #include "rt_names.h"
25 #include "utils.h"
26 #include "tc_util.h"
27 #include <linux/tc_act/tc_skbmod.h>
28 
skbmod_explain(void)29 static void skbmod_explain(void)
30 {
31 	fprintf(stderr,
32 		"Usage:... skbmod {[set <SETTABLE>] [swap <SWAPABLE>]} [CONTROL] [index INDEX]\n"
33 		"where SETTABLE is: [dmac DMAC] [smac SMAC] [etype ETYPE]\n"
34 		"where SWAPABLE is: \"mac\" to swap mac addresses\n"
35 		"note: \"swap mac\" is done after any outstanding D/SMAC change\n"
36 		"\tDMAC := 6 byte Destination MAC address\n"
37 		"\tSMAC := optional 6 byte Source MAC address\n"
38 		"\tETYPE := optional 16 bit ethertype\n"
39 		"\tCONTROL := reclassify | pipe | drop | continue | ok |\n"
40 		"\t           goto chain <CHAIN_INDEX>\n"
41 		"\tINDEX := skbmod index value to use\n");
42 }
43 
skbmod_usage(void)44 static void skbmod_usage(void)
45 {
46 	skbmod_explain();
47 	exit(-1);
48 }
49 
parse_skbmod(struct action_util * a,int * argc_p,char *** argv_p,int tca_id,struct nlmsghdr * n)50 static int parse_skbmod(struct action_util *a, int *argc_p, char ***argv_p,
51 			int tca_id, struct nlmsghdr *n)
52 {
53 	int argc = *argc_p;
54 	char **argv = *argv_p;
55 	int ok = 0;
56 	struct tc_skbmod p;
57 	struct rtattr *tail;
58 	char dbuf[ETH_ALEN];
59 	char sbuf[ETH_ALEN];
60 	__u16 skbmod_etype = 0;
61 	char *daddr = NULL;
62 	char *saddr = NULL;
63 
64 	memset(&p, 0, sizeof(p));
65 
66 	if (argc <= 0)
67 		return -1;
68 
69 	while (argc > 0) {
70 		if (matches(*argv, "skbmod") == 0) {
71 			NEXT_ARG();
72 			continue;
73 		} else if (matches(*argv, "swap") == 0) {
74 			NEXT_ARG();
75 			continue;
76 		} else if (matches(*argv, "mac") == 0) {
77 			p.flags |= SKBMOD_F_SWAPMAC;
78 			ok += 1;
79 		} else if (matches(*argv, "set") == 0) {
80 			NEXT_ARG();
81 			continue;
82 		} else if (matches(*argv, "etype") == 0) {
83 			NEXT_ARG();
84 			if (get_u16(&skbmod_etype, *argv, 0))
85 				invarg("ethertype is invalid", *argv);
86 			fprintf(stderr, "skbmod etype 0x%x\n", skbmod_etype);
87 			p.flags |= SKBMOD_F_ETYPE;
88 			ok += 1;
89 		} else if (matches(*argv, "dmac") == 0) {
90 			NEXT_ARG();
91 			daddr = *argv;
92 			if (sscanf(daddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
93 				   dbuf, dbuf + 1, dbuf + 2,
94 				   dbuf + 3, dbuf + 4, dbuf + 5) != 6) {
95 				fprintf(stderr, "Invalid dst mac address %s\n",
96 					daddr);
97 				return -1;
98 			}
99 			p.flags |= SKBMOD_F_DMAC;
100 			fprintf(stderr, "dst MAC address <%s>\n", daddr);
101 			ok += 1;
102 
103 		} else if (matches(*argv, "smac") == 0) {
104 			NEXT_ARG();
105 			saddr = *argv;
106 			if (sscanf(saddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
107 				   sbuf, sbuf + 1, sbuf + 2,
108 				   sbuf + 3, sbuf + 4, sbuf + 5) != 6) {
109 				fprintf(stderr, "Invalid smac address %s\n",
110 					saddr);
111 				return -1;
112 			}
113 			p.flags |= SKBMOD_F_SMAC;
114 			fprintf(stderr, "src MAC address <%s>\n", saddr);
115 			ok += 1;
116 		} else if (matches(*argv, "help") == 0) {
117 			skbmod_usage();
118 		} else {
119 			break;
120 		}
121 
122 		argc--;
123 		argv++;
124 	}
125 
126 	parse_action_control_dflt(&argc, &argv, &p.action, false, TC_ACT_PIPE);
127 
128 	if (argc) {
129 		if (matches(*argv, "index") == 0) {
130 			NEXT_ARG();
131 			if (get_u32(&p.index, *argv, 0)) {
132 				fprintf(stderr, "skbmod: Illegal \"index\"\n");
133 				return -1;
134 			}
135 			ok++;
136 			argc--;
137 			argv++;
138 		}
139 	}
140 
141 	if (!ok) {
142 		fprintf(stderr, "skbmod requires at least one option\n");
143 		skbmod_usage();
144 	}
145 
146 	tail = NLMSG_TAIL(n);
147 	addattr_l(n, MAX_MSG, tca_id, NULL, 0);
148 	addattr_l(n, MAX_MSG, TCA_SKBMOD_PARMS, &p, sizeof(p));
149 
150 	if (daddr)
151 		addattr_l(n, MAX_MSG, TCA_SKBMOD_DMAC, dbuf, ETH_ALEN);
152 	if (skbmod_etype)
153 		addattr16(n, MAX_MSG, TCA_SKBMOD_ETYPE, skbmod_etype);
154 	if (saddr)
155 		addattr_l(n, MAX_MSG, TCA_SKBMOD_SMAC, sbuf, ETH_ALEN);
156 
157 	tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail;
158 
159 	*argc_p = argc;
160 	*argv_p = argv;
161 	return 0;
162 }
163 
print_skbmod(struct action_util * au,FILE * f,struct rtattr * arg)164 static int print_skbmod(struct action_util *au, FILE *f, struct rtattr *arg)
165 {
166 	struct tc_skbmod *p = NULL;
167 	struct rtattr *tb[TCA_SKBMOD_MAX + 1];
168 	__u16 skbmod_etype = 0;
169 	int has_optional = 0;
170 	SPRINT_BUF(b1);
171 	SPRINT_BUF(b2);
172 
173 	if (arg == NULL)
174 		return -1;
175 
176 	parse_rtattr_nested(tb, TCA_SKBMOD_MAX, arg);
177 
178 	if (tb[TCA_SKBMOD_PARMS] == NULL) {
179 		fprintf(f, "[NULL skbmod parameters]");
180 		return -1;
181 	}
182 
183 	p = RTA_DATA(tb[TCA_SKBMOD_PARMS]);
184 
185 	fprintf(f, "skbmod ");
186 	print_action_control(f, "", p->action, " ");
187 
188 	if (tb[TCA_SKBMOD_ETYPE]) {
189 		skbmod_etype = rta_getattr_u16(tb[TCA_SKBMOD_ETYPE]);
190 		has_optional = 1;
191 		fprintf(f, "set etype 0x%X ", skbmod_etype);
192 	}
193 
194 	if (has_optional)
195 		fprintf(f, "\n\t ");
196 
197 	if (tb[TCA_SKBMOD_DMAC]) {
198 		has_optional = 1;
199 		fprintf(f, "set dmac %s ",
200 			ll_addr_n2a(RTA_DATA(tb[TCA_SKBMOD_DMAC]),
201 				    RTA_PAYLOAD(tb[TCA_SKBMOD_DMAC]), 0, b1,
202 				    sizeof(b1)));
203 
204 	}
205 
206 	if (tb[TCA_SKBMOD_SMAC]) {
207 		has_optional = 1;
208 		fprintf(f, "set smac %s ",
209 			ll_addr_n2a(RTA_DATA(tb[TCA_SKBMOD_SMAC]),
210 				    RTA_PAYLOAD(tb[TCA_SKBMOD_SMAC]), 0, b2,
211 				    sizeof(b2)));
212 	}
213 
214 	if (p->flags & SKBMOD_F_SWAPMAC)
215 		fprintf(f, "swap mac ");
216 
217 	fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt,
218 		p->bindcnt);
219 	if (show_stats) {
220 		if (tb[TCA_SKBMOD_TM]) {
221 			struct tcf_t *tm = RTA_DATA(tb[TCA_SKBMOD_TM]);
222 
223 			print_tm(f, tm);
224 		}
225 	}
226 
227 	fprintf(f, "\n");
228 
229 	return 0;
230 }
231 
232 struct action_util skbmod_action_util = {
233 	.id = "skbmod",
234 	.parse_aopt = parse_skbmod,
235 	.print_aopt = print_skbmod,
236 };
237