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