]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/f_fw.c
Fix issues with filter name conflicts and cleanup.
[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{
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
35static 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
43 if (handle) {
44 if (get_u32(&t->tcm_handle, handle, 0)) {
45 fprintf(stderr, "Illegal \"handle\"\n");
46 return -1;
47 }
48 }
49
50 if (argc == 0)
51 return 0;
52
53 tail = (struct rtattr*)(((void*)n)+NLMSG_ALIGN(n->nlmsg_len));
54 addattr_l(n, 4096, TCA_OPTIONS, NULL, 0);
55
56 while (argc > 0) {
57 if (matches(*argv, "classid") == 0 ||
58 matches(*argv, "flowid") == 0) {
59 unsigned handle;
60 NEXT_ARG();
61 if (get_tc_classid(&handle, *argv)) {
62 fprintf(stderr, "Illegal \"classid\"\n");
63 return -1;
64 }
65 addattr_l(n, 4096, TCA_FW_CLASSID, &handle, 4);
66 } else if (matches(*argv, "police") == 0) {
67 NEXT_ARG();
68 if (parse_police(&argc, &argv, TCA_FW_POLICE, n)) {
69 fprintf(stderr, "Illegal \"police\"\n");
70 return -1;
71 }
72 continue;
fa3a9930
SH
73 } else if (matches(*argv, "action") == 0) {
74 NEXT_ARG();
75 if (parse_action(&argc, &argv, TCA_FW_ACT, n)) {
76 fprintf(stderr, "Illegal fw \"action\"\n");
77 return -1;
78 }
79 continue;
80 } else if (strcmp(*argv, "indev") == 0) {
81 char d[IFNAMSIZ+1];
82 memset(d, 0, sizeof (d));
83 argc--;
84 argv++;
85 if (argc < 1) {
86 fprintf(stderr, "Illegal indev\n");
87 return -1;
88 }
89 strncpy(d, *argv, sizeof (d) - 1);
90 addattr_l(n, MAX_MSG, TCA_FW_INDEV, d, strlen(d) + 1);
aba5acdf
SH
91 } else if (strcmp(*argv, "help") == 0) {
92 explain();
93 return -1;
94 } else {
95 fprintf(stderr, "What is \"%s\"?\n", *argv);
96 explain();
97 return -1;
98 }
99 argc--; argv++;
100 }
101 tail->rta_len = (((void*)n)+n->nlmsg_len) - (void*)tail;
102 return 0;
103}
104
105static int fw_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle)
106{
107 struct rtattr *tb[TCA_FW_MAX+1];
108
109 if (opt == NULL)
110 return 0;
111
112 memset(tb, 0, sizeof(tb));
113 if (opt)
114 parse_rtattr(tb, TCA_FW_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt));
115
116 if (handle)
117 fprintf(f, "handle 0x%x ", handle);
118
119 if (tb[TCA_FW_CLASSID]) {
120 SPRINT_BUF(b1);
121 fprintf(f, "classid %s ", sprint_tc_classid(*(__u32*)RTA_DATA(tb[TCA_FW_CLASSID]), b1));
122 }
123
124 if (tb[TCA_FW_POLICE])
125 tc_print_police(f, tb[TCA_FW_POLICE]);
fa3a9930
SH
126 if (tb[TCA_FW_INDEV]) {
127 struct rtattr *idev = tb[TCA_FW_INDEV];
128 fprintf(f, "input dev %s ",(char *)RTA_DATA(idev));
129 }
130
131 if (tb[TCA_FW_ACT]) {
132 fprintf(f, "\n");
133 tc_print_action(f, tb[TCA_FW_ACT]);
134 }
aba5acdf
SH
135 return 0;
136}
137
138struct filter_util fw_util = {
139 NULL,
140 "fw",
141 fw_parse_opt,
142 fw_print_opt,
143};