This commit was generated by cvs2svn to compensate for changes in r2587,
[iproute2.git] / tc / f_basic.c
1 /*
2  * f_basic.c            Basic Classifier
3  *
4  *              This program is free software; you can u32istribute 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:     Thomas Graf <tgraf@suug.ch>
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>
22 #include <linux/if.h>
23
24 #include "utils.h"
25 #include "tc_util.h"
26 #include "m_ematch.h"
27
28 static void explain(void)
29 {
30         fprintf(stderr, "Usage: ... basic [ match EMATCH_TREE ] [ police POLICE_SPEC ]\n");
31         fprintf(stderr, "                 [ action ACTION_SPEC ] [ classid CLASSID ]\n");
32         fprintf(stderr, "\n");
33         fprintf(stderr, "Where: SELECTOR := SAMPLE SAMPLE ...\n");
34         fprintf(stderr, "       FILTERID := X:Y:Z\n");
35 }
36
37 static int basic_parse_opt(struct filter_util *qu, char *handle,
38                            int argc, char **argv, struct nlmsghdr *n)
39 {
40         struct tcmsg *t = NLMSG_DATA(n);
41         struct rtattr *tail;
42         long h = 0;
43
44         if (argc == 0)
45                 return 0;
46
47         if (handle) {
48                 h = strtol(handle, NULL, 0);
49                 if (h == LONG_MIN || h == LONG_MAX) {
50                         fprintf(stderr, "Illegal handle \"%s\", must be numeric.\n",
51                             handle);
52                         return -1;
53                 }
54         }
55                 
56         t->tcm_handle = h;
57
58         tail = (struct rtattr*)(((void*)n)+NLMSG_ALIGN(n->nlmsg_len));
59         addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0);
60
61         while (argc > 0) {
62                 if (matches(*argv, "match") == 0) {
63                         NEXT_ARG();
64                         if (parse_ematch(&argc, &argv, TCA_BASIC_EMATCHES, n)) {
65                                 fprintf(stderr, "Illegal \"ematch\"\n");
66                                 return -1;
67                         }
68                         continue;
69                 } else if (matches(*argv, "classid") == 0 ||
70                            strcmp(*argv, "flowid") == 0) {
71                         unsigned handle;
72                         NEXT_ARG();
73                         if (get_tc_classid(&handle, *argv)) {
74                                 fprintf(stderr, "Illegal \"classid\"\n");
75                                 return -1;
76                         }
77                         addattr_l(n, MAX_MSG, TCA_BASIC_CLASSID, &handle, 4);
78                 } else if (matches(*argv, "action") == 0) {
79                         NEXT_ARG();
80                         if (parse_action(&argc, &argv, TCA_BASIC_ACT, n)) {
81                                 fprintf(stderr, "Illegal \"action\"\n");
82                                 return -1;
83                         }
84                         continue;
85
86                 } else if (matches(*argv, "police") == 0) {
87                         NEXT_ARG();
88                         if (parse_police(&argc, &argv, TCA_BASIC_POLICE, n)) {
89                                 fprintf(stderr, "Illegal \"police\"\n");
90                                 return -1;
91                         }
92                         continue;
93                 } else if (strcmp(*argv, "help") == 0) {
94                         explain();
95                         return -1;
96                 } else {
97                         fprintf(stderr, "What is \"%s\"?\n", *argv);
98                         explain();
99                         return -1;
100                 }
101                 argc--; argv++;
102         }
103
104         tail->rta_len = (((void*)n)+n->nlmsg_len) - (void*)tail;
105         return 0;
106 }
107
108 static int basic_print_opt(struct filter_util *qu, FILE *f,
109                            struct rtattr *opt, __u32 handle)
110 {
111         struct rtattr *tb[TCA_BASIC_MAX+1];
112
113         if (opt == NULL)
114                 return 0;
115
116         parse_rtattr_nested(tb, TCA_BASIC_MAX, opt);
117
118         if (handle)
119                 fprintf(f, "handle 0x%x ", handle);
120
121         if (tb[TCA_BASIC_CLASSID]) {
122                 SPRINT_BUF(b1);
123                 fprintf(f, "flowid %s ",
124                         sprint_tc_classid(*(__u32*)RTA_DATA(tb[TCA_BASIC_CLASSID]), b1));
125         }
126
127         if (tb[TCA_BASIC_EMATCHES])
128                 print_ematch(f, tb[TCA_BASIC_EMATCHES]);
129
130         if (tb[TCA_BASIC_POLICE]) {
131                 fprintf(f, "\n");
132                 tc_print_police(f, tb[TCA_BASIC_POLICE]);
133         }
134
135         if (tb[TCA_BASIC_ACT]) {
136                 tc_print_action(f, tb[TCA_BASIC_ACT]);
137         }
138
139         return 0;
140 }
141
142 struct filter_util basic_filter_util = {
143         .id = "basic",
144         .parse_fopt = basic_parse_opt,
145         .print_fopt = basic_print_opt,
146 };