]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/f_fw.c
man: tc-csum.8: Fix inconsistency in example description
[mirror_iproute2.git] / tc / f_fw.c
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>
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/if.h> /* IFNAMSIZ */
22 #include "utils.h"
23 #include "tc_util.h"
24
25 static void explain(void)
26 {
27 fprintf(stderr,
28 "Usage: ... fw [ classid CLASSID ] [ indev DEV ] [ action ACTION_SPEC ]\n");
29 fprintf(stderr,
30 " CLASSID := Push matching packets to the class identified by CLASSID with format X:Y\n");
31 fprintf(stderr,
32 " CLASSID is parsed as hexadecimal input.\n");
33 fprintf(stderr,
34 " DEV := specify device for incoming device classification.\n");
35 fprintf(stderr,
36 " ACTION_SPEC := Apply an action on matching packets.\n");
37 fprintf(stderr,
38 " NOTE: handle is represented as HANDLE[/FWMASK].\n");
39 fprintf(stderr, " FWMASK is 0xffffffff by default.\n");
40 }
41
42 static int fw_parse_opt(struct filter_util *qu, char *handle, int argc, char **argv, struct nlmsghdr *n)
43 {
44 struct tcmsg *t = NLMSG_DATA(n);
45 struct rtattr *tail;
46 __u32 mask = 0;
47 int mask_set = 0;
48
49 if (handle) {
50 char *slash;
51
52 if ((slash = strchr(handle, '/')) != NULL)
53 *slash = '\0';
54 if (get_u32(&t->tcm_handle, handle, 0)) {
55 fprintf(stderr, "Illegal \"handle\"\n");
56 return -1;
57 }
58 if (slash) {
59 if (get_u32(&mask, slash+1, 0)) {
60 fprintf(stderr, "Illegal \"handle\" mask\n");
61 return -1;
62 }
63 mask_set = 1;
64 }
65 }
66
67 if (argc == 0)
68 return 0;
69
70 tail = NLMSG_TAIL(n);
71 addattr_l(n, 4096, TCA_OPTIONS, NULL, 0);
72
73 if (mask_set)
74 addattr32(n, MAX_MSG, TCA_FW_MASK, mask);
75
76 while (argc > 0) {
77 if (matches(*argv, "classid") == 0 ||
78 matches(*argv, "flowid") == 0) {
79 unsigned int handle;
80
81 NEXT_ARG();
82 if (get_tc_classid(&handle, *argv)) {
83 fprintf(stderr, "Illegal \"classid\"\n");
84 return -1;
85 }
86 addattr_l(n, 4096, TCA_FW_CLASSID, &handle, 4);
87 } else if (matches(*argv, "police") == 0) {
88 NEXT_ARG();
89 if (parse_police(&argc, &argv, TCA_FW_POLICE, n)) {
90 fprintf(stderr, "Illegal \"police\"\n");
91 return -1;
92 }
93 continue;
94 } else if (matches(*argv, "action") == 0) {
95 NEXT_ARG();
96 if (parse_action(&argc, &argv, TCA_FW_ACT, n)) {
97 fprintf(stderr, "Illegal fw \"action\"\n");
98 return -1;
99 }
100 continue;
101 } else if (strcmp(*argv, "indev") == 0) {
102 char d[IFNAMSIZ+1] = {};
103
104 argc--;
105 argv++;
106 if (argc < 1) {
107 fprintf(stderr, "Illegal indev\n");
108 return -1;
109 }
110 strncpy(d, *argv, sizeof(d) - 1);
111 addattr_l(n, MAX_MSG, TCA_FW_INDEV, d, strlen(d) + 1);
112 } else if (strcmp(*argv, "help") == 0) {
113 explain();
114 return -1;
115 } else {
116 fprintf(stderr, "What is \"%s\"?\n", *argv);
117 explain();
118 return -1;
119 }
120 argc--; argv++;
121 }
122 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
123 return 0;
124 }
125
126 static int fw_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle)
127 {
128 struct rtattr *tb[TCA_FW_MAX+1];
129
130 if (opt == NULL)
131 return 0;
132
133 parse_rtattr_nested(tb, TCA_FW_MAX, opt);
134
135 if (handle || tb[TCA_FW_MASK]) {
136 __u32 mark = 0, mask = 0;
137
138 if (handle)
139 mark = handle;
140 if (tb[TCA_FW_MASK] &&
141 (mask = rta_getattr_u32(tb[TCA_FW_MASK])) != 0xFFFFFFFF)
142 fprintf(f, "handle 0x%x/0x%x ", mark, mask);
143 else
144 fprintf(f, "handle 0x%x ", handle);
145 }
146
147 if (tb[TCA_FW_CLASSID]) {
148 SPRINT_BUF(b1);
149 fprintf(f, "classid %s ", sprint_tc_classid(rta_getattr_u32(tb[TCA_FW_CLASSID]), b1));
150 }
151
152 if (tb[TCA_FW_POLICE])
153 tc_print_police(f, tb[TCA_FW_POLICE]);
154 if (tb[TCA_FW_INDEV]) {
155 struct rtattr *idev = tb[TCA_FW_INDEV];
156
157 fprintf(f, "input dev %s ", rta_getattr_str(idev));
158 }
159
160 if (tb[TCA_FW_ACT]) {
161 fprintf(f, "\n");
162 tc_print_action(f, tb[TCA_FW_ACT], 0);
163 }
164 return 0;
165 }
166
167 struct filter_util fw_filter_util = {
168 .id = "fw",
169 .parse_fopt = fw_parse_opt,
170 .print_fopt = fw_print_opt,
171 };