]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/f_fw.c
Use C99 style initializers everywhere
[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>
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>
fa3a9930 22#include <linux/if.h> /* IFNAMSIZ */
aba5acdf
SH
23#include "utils.h"
24#include "tc_util.h"
25
26static void explain(void)
27{
863ecb04
JHS
28 fprintf(stderr, "Usage: ... fw [ classid CLASSID ] [ action ACTION_SPEC ]\n");
29 fprintf(stderr, " ACTION_SPEC := ... look at individual actions\n");
aba5acdf 30 fprintf(stderr, " CLASSID := X:Y\n");
e9acc242 31 fprintf(stderr, "\nNOTE: CLASSID is parsed as hexadecimal input.\n");
aba5acdf
SH
32}
33
aba5acdf
SH
34static int fw_parse_opt(struct filter_util *qu, char *handle, int argc, char **argv, struct nlmsghdr *n)
35{
aba5acdf
SH
36 struct tcmsg *t = NLMSG_DATA(n);
37 struct rtattr *tail;
c90308ff
PM
38 __u32 mask = 0;
39 int mask_set = 0;
aba5acdf 40
aba5acdf 41 if (handle) {
e22b42a2 42 char *slash;
32a121cb 43
e22b42a2
FD
44 if ((slash = strchr(handle, '/')) != NULL)
45 *slash = '\0';
aba5acdf
SH
46 if (get_u32(&t->tcm_handle, handle, 0)) {
47 fprintf(stderr, "Illegal \"handle\"\n");
48 return -1;
49 }
e22b42a2
FD
50 if (slash) {
51 if (get_u32(&mask, slash+1, 0)) {
52 fprintf(stderr, "Illegal \"handle\" mask\n");
53 return -1;
54 }
c90308ff 55 mask_set = 1;
e22b42a2 56 }
aba5acdf
SH
57 }
58
59 if (argc == 0)
60 return 0;
61
c90308ff
PM
62 tail = NLMSG_TAIL(n);
63 addattr_l(n, 4096, TCA_OPTIONS, NULL, 0);
64
65 if (mask_set)
66 addattr32(n, MAX_MSG, TCA_FW_MASK, mask);
67
aba5acdf
SH
68 while (argc > 0) {
69 if (matches(*argv, "classid") == 0 ||
70 matches(*argv, "flowid") == 0) {
32a121cb
SH
71 unsigned int handle;
72
aba5acdf
SH
73 NEXT_ARG();
74 if (get_tc_classid(&handle, *argv)) {
75 fprintf(stderr, "Illegal \"classid\"\n");
76 return -1;
77 }
78 addattr_l(n, 4096, TCA_FW_CLASSID, &handle, 4);
79 } else if (matches(*argv, "police") == 0) {
80 NEXT_ARG();
81 if (parse_police(&argc, &argv, TCA_FW_POLICE, n)) {
82 fprintf(stderr, "Illegal \"police\"\n");
83 return -1;
84 }
85 continue;
fa3a9930
SH
86 } else if (matches(*argv, "action") == 0) {
87 NEXT_ARG();
88 if (parse_action(&argc, &argv, TCA_FW_ACT, n)) {
89 fprintf(stderr, "Illegal fw \"action\"\n");
90 return -1;
91 }
92 continue;
93 } else if (strcmp(*argv, "indev") == 0) {
d17b136f 94 char d[IFNAMSIZ+1] = {};
32a121cb 95
fa3a9930
SH
96 argc--;
97 argv++;
98 if (argc < 1) {
99 fprintf(stderr, "Illegal indev\n");
100 return -1;
101 }
32a121cb 102 strncpy(d, *argv, sizeof(d) - 1);
fa3a9930 103 addattr_l(n, MAX_MSG, TCA_FW_INDEV, d, strlen(d) + 1);
aba5acdf
SH
104 } else if (strcmp(*argv, "help") == 0) {
105 explain();
106 return -1;
107 } else {
108 fprintf(stderr, "What is \"%s\"?\n", *argv);
109 explain();
110 return -1;
111 }
112 argc--; argv++;
113 }
034102f2 114 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
aba5acdf
SH
115 return 0;
116}
117
118static int fw_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle)
119{
120 struct rtattr *tb[TCA_FW_MAX+1];
121
122 if (opt == NULL)
123 return 0;
124
021ed13f 125 parse_rtattr_nested(tb, TCA_FW_MAX, opt);
aba5acdf 126
e22b42a2
FD
127 if (handle || tb[TCA_FW_MASK]) {
128 __u32 mark = 0, mask = 0;
32a121cb
SH
129
130 if (handle)
e22b42a2 131 mark = handle;
32a121cb 132 if (tb[TCA_FW_MASK] &&
ff24746c 133 (mask = rta_getattr_u32(tb[TCA_FW_MASK])) != 0xFFFFFFFF)
e22b42a2
FD
134 fprintf(f, "handle 0x%x/0x%x ", mark, mask);
135 else
136 fprintf(f, "handle 0x%x ", handle);
137 }
aba5acdf
SH
138
139 if (tb[TCA_FW_CLASSID]) {
140 SPRINT_BUF(b1);
ff24746c 141 fprintf(f, "classid %s ", sprint_tc_classid(rta_getattr_u32(tb[TCA_FW_CLASSID]), b1));
aba5acdf
SH
142 }
143
144 if (tb[TCA_FW_POLICE])
145 tc_print_police(f, tb[TCA_FW_POLICE]);
fa3a9930
SH
146 if (tb[TCA_FW_INDEV]) {
147 struct rtattr *idev = tb[TCA_FW_INDEV];
32a121cb
SH
148
149 fprintf(f, "input dev %s ", rta_getattr_str(idev));
fa3a9930 150 }
ae665a52 151
fa3a9930
SH
152 if (tb[TCA_FW_ACT]) {
153 fprintf(f, "\n");
154 tc_print_action(f, tb[TCA_FW_ACT]);
155 }
aba5acdf
SH
156 return 0;
157}
158
6b7dff17
SH
159struct filter_util fw_filter_util = {
160 .id = "fw",
161 .parse_fopt = fw_parse_opt,
162 .print_fopt = fw_print_opt,
aba5acdf 163};