blob: ff9648c5121328e6898952839ea8fdd11dd7948b [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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 <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>
osdl.net!shemmingerfa3a9932004-08-13 23:54:55 +000022#include <linux/if.h> /* IFNAMSIZ */
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000023#include "utils.h"
24#include "tc_util.h"
25
26static void explain(void)
27{
Jamal Hadi Salim863ecb02014-10-06 07:41:21 -040028 fprintf(stderr, "Usage: ... fw [ classid CLASSID ] [ action ACTION_SPEC ]\n");
29 fprintf(stderr, " ACTION_SPEC := ... look at individual actions\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000030 fprintf(stderr, " CLASSID := X:Y\n");
PJ Waskiewicze9acc242008-02-13 03:49:09 -080031 fprintf(stderr, "\nNOTE: CLASSID is parsed as hexadecimal input.\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000032}
33
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000034static int fw_parse_opt(struct filter_util *qu, char *handle, int argc, char **argv, struct nlmsghdr *n)
35{
36 struct tc_police tp;
37 struct tcmsg *t = NLMSG_DATA(n);
38 struct rtattr *tail;
Patrick McHardyc90308f2009-11-23 12:03:41 +010039 __u32 mask = 0;
40 int mask_set = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000041
42 memset(&tp, 0, sizeof(tp));
43
44 if (handle) {
François Delawardee22b42a2007-11-26 18:13:24 +010045 char *slash;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070046
François Delawardee22b42a2007-11-26 18:13:24 +010047 if ((slash = strchr(handle, '/')) != NULL)
48 *slash = '\0';
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000049 if (get_u32(&t->tcm_handle, handle, 0)) {
50 fprintf(stderr, "Illegal \"handle\"\n");
51 return -1;
52 }
François Delawardee22b42a2007-11-26 18:13:24 +010053 if (slash) {
54 if (get_u32(&mask, slash+1, 0)) {
55 fprintf(stderr, "Illegal \"handle\" mask\n");
56 return -1;
57 }
Patrick McHardyc90308f2009-11-23 12:03:41 +010058 mask_set = 1;
François Delawardee22b42a2007-11-26 18:13:24 +010059 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000060 }
61
62 if (argc == 0)
63 return 0;
64
Patrick McHardyc90308f2009-11-23 12:03:41 +010065 tail = NLMSG_TAIL(n);
66 addattr_l(n, 4096, TCA_OPTIONS, NULL, 0);
67
68 if (mask_set)
69 addattr32(n, MAX_MSG, TCA_FW_MASK, mask);
70
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000071 while (argc > 0) {
72 if (matches(*argv, "classid") == 0 ||
73 matches(*argv, "flowid") == 0) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -070074 unsigned int handle;
75
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000076 NEXT_ARG();
77 if (get_tc_classid(&handle, *argv)) {
78 fprintf(stderr, "Illegal \"classid\"\n");
79 return -1;
80 }
81 addattr_l(n, 4096, TCA_FW_CLASSID, &handle, 4);
82 } else if (matches(*argv, "police") == 0) {
83 NEXT_ARG();
84 if (parse_police(&argc, &argv, TCA_FW_POLICE, n)) {
85 fprintf(stderr, "Illegal \"police\"\n");
86 return -1;
87 }
88 continue;
osdl.net!shemmingerfa3a9932004-08-13 23:54:55 +000089 } else if (matches(*argv, "action") == 0) {
90 NEXT_ARG();
91 if (parse_action(&argc, &argv, TCA_FW_ACT, n)) {
92 fprintf(stderr, "Illegal fw \"action\"\n");
93 return -1;
94 }
95 continue;
96 } else if (strcmp(*argv, "indev") == 0) {
97 char d[IFNAMSIZ+1];
Stephen Hemminger32a121c2016-03-21 11:48:36 -070098
99 memset(d, 0, sizeof(d));
osdl.net!shemmingerfa3a9932004-08-13 23:54:55 +0000100 argc--;
101 argv++;
102 if (argc < 1) {
103 fprintf(stderr, "Illegal indev\n");
104 return -1;
105 }
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700106 strncpy(d, *argv, sizeof(d) - 1);
osdl.net!shemmingerfa3a9932004-08-13 23:54:55 +0000107 addattr_l(n, MAX_MSG, TCA_FW_INDEV, d, strlen(d) + 1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000108 } else if (strcmp(*argv, "help") == 0) {
109 explain();
110 return -1;
111 } else {
112 fprintf(stderr, "What is \"%s\"?\n", *argv);
113 explain();
114 return -1;
115 }
116 argc--; argv++;
117 }
5!tgraf034102f2005-01-18 01:24:18 +0000118 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000119 return 0;
120}
121
122static int fw_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle)
123{
124 struct rtattr *tb[TCA_FW_MAX+1];
125
126 if (opt == NULL)
127 return 0;
128
5!tgraf021ed132005-01-18 22:11:58 +0000129 parse_rtattr_nested(tb, TCA_FW_MAX, opt);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000130
François Delawardee22b42a2007-11-26 18:13:24 +0100131 if (handle || tb[TCA_FW_MASK]) {
132 __u32 mark = 0, mask = 0;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700133
134 if (handle)
François Delawardee22b42a2007-11-26 18:13:24 +0100135 mark = handle;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700136 if (tb[TCA_FW_MASK] &&
Stephen Hemmingerff247462012-04-10 08:47:55 -0700137 (mask = rta_getattr_u32(tb[TCA_FW_MASK])) != 0xFFFFFFFF)
François Delawardee22b42a2007-11-26 18:13:24 +0100138 fprintf(f, "handle 0x%x/0x%x ", mark, mask);
139 else
140 fprintf(f, "handle 0x%x ", handle);
141 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000142
143 if (tb[TCA_FW_CLASSID]) {
144 SPRINT_BUF(b1);
Stephen Hemmingerff247462012-04-10 08:47:55 -0700145 fprintf(f, "classid %s ", sprint_tc_classid(rta_getattr_u32(tb[TCA_FW_CLASSID]), b1));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000146 }
147
148 if (tb[TCA_FW_POLICE])
149 tc_print_police(f, tb[TCA_FW_POLICE]);
osdl.net!shemmingerfa3a9932004-08-13 23:54:55 +0000150 if (tb[TCA_FW_INDEV]) {
151 struct rtattr *idev = tb[TCA_FW_INDEV];
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700152
153 fprintf(f, "input dev %s ", rta_getattr_str(idev));
osdl.net!shemmingerfa3a9932004-08-13 23:54:55 +0000154 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800155
osdl.net!shemmingerfa3a9932004-08-13 23:54:55 +0000156 if (tb[TCA_FW_ACT]) {
157 fprintf(f, "\n");
158 tc_print_action(f, tb[TCA_FW_ACT]);
159 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000160 return 0;
161}
162
osdl.net!shemminger6b7dff12004-09-28 18:35:49 +0000163struct filter_util fw_filter_util = {
164 .id = "fw",
165 .parse_fopt = fw_parse_opt,
166 .print_fopt = fw_print_opt,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000167};