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