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