]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/f_fw.c
Merge branch 'master' of git://git.kernel.org/pub/scm/network/iproute2/iproute2-next
[mirror_iproute2.git] / tc / f_fw.c
CommitLineData
aba5acdf
SH
1/*
2 * f_fw.c FW filter.
3 *
4 * This program is free software; you can redistribute 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: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
aba5acdf
SH
16#include <fcntl.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20#include <string.h>
fa3a9930 21#include <linux/if.h> /* IFNAMSIZ */
aba5acdf
SH
22#include "utils.h"
23#include "tc_util.h"
24
25static void explain(void)
26{
4b5451c4 27 fprintf(stderr,
8589eb4e
MC
28 "Usage: ... fw [ classid CLASSID ] [ indev DEV ] [ action ACTION_SPEC ]\n"
29 " CLASSID := Push matching packets to the class identified by CLASSID with format X:Y\n"
30 " CLASSID is parsed as hexadecimal input.\n"
31 " DEV := specify device for incoming device classification.\n"
32 " ACTION_SPEC := Apply an action on matching packets.\n"
33 " NOTE: handle is represented as HANDLE[/FWMASK].\n"
34 " FWMASK is 0xffffffff by default.\n");
aba5acdf
SH
35}
36
aba5acdf
SH
37static int fw_parse_opt(struct filter_util *qu, char *handle, int argc, char **argv, struct nlmsghdr *n)
38{
aba5acdf
SH
39 struct tcmsg *t = NLMSG_DATA(n);
40 struct rtattr *tail;
c90308ff
PM
41 __u32 mask = 0;
42 int mask_set = 0;
aba5acdf 43
aba5acdf 44 if (handle) {
e22b42a2 45 char *slash;
32a121cb 46
e22b42a2
FD
47 if ((slash = strchr(handle, '/')) != NULL)
48 *slash = '\0';
aba5acdf
SH
49 if (get_u32(&t->tcm_handle, handle, 0)) {
50 fprintf(stderr, "Illegal \"handle\"\n");
51 return -1;
52 }
e22b42a2
FD
53 if (slash) {
54 if (get_u32(&mask, slash+1, 0)) {
55 fprintf(stderr, "Illegal \"handle\" mask\n");
56 return -1;
57 }
c90308ff 58 mask_set = 1;
e22b42a2 59 }
aba5acdf
SH
60 }
61
62 if (argc == 0)
63 return 0;
64
c14f9d92 65 tail = addattr_nest(n, 4096, TCA_OPTIONS);
c90308ff
PM
66
67 if (mask_set)
68 addattr32(n, MAX_MSG, TCA_FW_MASK, mask);
69
aba5acdf
SH
70 while (argc > 0) {
71 if (matches(*argv, "classid") == 0 ||
72 matches(*argv, "flowid") == 0) {
32a121cb
SH
73 unsigned int handle;
74
aba5acdf
SH
75 NEXT_ARG();
76 if (get_tc_classid(&handle, *argv)) {
77 fprintf(stderr, "Illegal \"classid\"\n");
78 return -1;
79 }
80 addattr_l(n, 4096, TCA_FW_CLASSID, &handle, 4);
81 } else if (matches(*argv, "police") == 0) {
82 NEXT_ARG();
83 if (parse_police(&argc, &argv, TCA_FW_POLICE, n)) {
84 fprintf(stderr, "Illegal \"police\"\n");
85 return -1;
86 }
87 continue;
fa3a9930
SH
88 } else if (matches(*argv, "action") == 0) {
89 NEXT_ARG();
90 if (parse_action(&argc, &argv, TCA_FW_ACT, n)) {
91 fprintf(stderr, "Illegal fw \"action\"\n");
92 return -1;
93 }
94 continue;
95 } else if (strcmp(*argv, "indev") == 0) {
d17b136f 96 char d[IFNAMSIZ+1] = {};
32a121cb 97
fa3a9930
SH
98 argc--;
99 argv++;
100 if (argc < 1) {
101 fprintf(stderr, "Illegal indev\n");
102 return -1;
103 }
32a121cb 104 strncpy(d, *argv, sizeof(d) - 1);
fa3a9930 105 addattr_l(n, MAX_MSG, TCA_FW_INDEV, d, strlen(d) + 1);
aba5acdf
SH
106 } else if (strcmp(*argv, "help") == 0) {
107 explain();
108 return -1;
109 } else {
110 fprintf(stderr, "What is \"%s\"?\n", *argv);
111 explain();
112 return -1;
113 }
114 argc--; argv++;
115 }
c14f9d92 116 addattr_nest_end(n, tail);
aba5acdf
SH
117 return 0;
118}
119
120static int fw_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle)
121{
122 struct rtattr *tb[TCA_FW_MAX+1];
123
124 if (opt == NULL)
125 return 0;
126
021ed13f 127 parse_rtattr_nested(tb, TCA_FW_MAX, opt);
aba5acdf 128
e22b42a2
FD
129 if (handle || tb[TCA_FW_MASK]) {
130 __u32 mark = 0, mask = 0;
32a121cb
SH
131
132 if (handle)
e22b42a2 133 mark = handle;
32a121cb 134 if (tb[TCA_FW_MASK] &&
ff24746c 135 (mask = rta_getattr_u32(tb[TCA_FW_MASK])) != 0xFFFFFFFF)
e22b42a2
FD
136 fprintf(f, "handle 0x%x/0x%x ", mark, mask);
137 else
138 fprintf(f, "handle 0x%x ", handle);
139 }
aba5acdf
SH
140
141 if (tb[TCA_FW_CLASSID]) {
142 SPRINT_BUF(b1);
ff24746c 143 fprintf(f, "classid %s ", sprint_tc_classid(rta_getattr_u32(tb[TCA_FW_CLASSID]), b1));
aba5acdf
SH
144 }
145
146 if (tb[TCA_FW_POLICE])
147 tc_print_police(f, tb[TCA_FW_POLICE]);
fa3a9930
SH
148 if (tb[TCA_FW_INDEV]) {
149 struct rtattr *idev = tb[TCA_FW_INDEV];
32a121cb
SH
150
151 fprintf(f, "input dev %s ", rta_getattr_str(idev));
fa3a9930 152 }
ae665a52 153
fa3a9930
SH
154 if (tb[TCA_FW_ACT]) {
155 fprintf(f, "\n");
9e713525 156 tc_print_action(f, tb[TCA_FW_ACT], 0);
fa3a9930 157 }
aba5acdf
SH
158 return 0;
159}
160
6b7dff17
SH
161struct filter_util fw_filter_util = {
162 .id = "fw",
163 .parse_fopt = fw_parse_opt,
164 .print_fopt = fw_print_opt,
aba5acdf 165};