1 /* f_canprio.c Filter for CAN packets */
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
23 struct canprio_rule canprio_rules[32];
25 static void explain(void)
27 fprintf(stderr, "Usage: ... canprio match CANID:MASK flowid FLOWID\n");
31 static int canprio_parse_opt(struct filter_util *qu, char *handle,
32 int argc, char **argv, struct nlmsghdr *n)
34 struct tcmsg *t = NLMSG_DATA(n); // Why?
37 __u32 canid, canid_mask;
44 h = strtol(handle, NULL, 0);
45 if (h == LONG_MIN || h == LONG_MAX) {
46 fprintf(stderr, "Illegal handle \"%s\", must be numeric.\n",
52 t->tcm_handle = h; // Why?
54 tail = (struct rtattr*)(((void*)n)+NLMSG_ALIGN(n->nlmsg_len));
55 addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0);
58 if (matches(*argv, "match") == 0) {
59 //printf(" [parsing match ...");
62 //FIXME why it does not work with "%jx"?
63 if (sscanf(*argv, "%lx:%lx", &canid, &canid_mask) != 2) {
64 fprintf(stderr, "improperly formed Can ID & mask '%s'\n", *argv);
68 //printf("... 0x%x:0x%x]\n", canid, canid_mask);
69 if (canid > 0) {// FIXME boundary check
70 canprio_rules[rules_count].canid = canid;
71 canprio_rules[rules_count].canid_mask = canid_mask;
75 } else if (matches(*argv, "classid") == 0 ||
76 strcmp(*argv, "flowid") == 0) {
77 //printf(" [parsing flowid]\n");
80 if (get_tc_classid(&handle, *argv)) {
81 fprintf(stderr, "Illegal \"classid\"\n");
84 addattr_l(n, MAX_MSG, TCA_CANPRIO_CLASSID, &handle, 4);
86 } else if (strcmp(*argv, "help") == 0) {
91 fprintf(stderr, "What is \"%s\"?\n", *argv);
99 addattr_l(n, MAX_MSG, TCA_CANPRIO_RULES, &canprio_rules,
100 sizeof(struct canprio_rule) * rules_count);
102 tail->rta_len = (((void*)n)+n->nlmsg_len) - (void*)tail;
106 static int canprio_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt,
109 struct rtattr *tb[TCA_CANPRIO_MAX+1];
115 parse_rtattr_nested(tb, TCA_CANPRIO_MAX, opt);
118 fprintf(f, "handle 0x%x ", handle);
120 if (tb[TCA_BASIC_CLASSID]) {
121 SPRINT_BUF(b1); //FIXME wat?
122 fprintf(f, "flowid %s ",
123 sprint_tc_classid(*(__u32*)RTA_DATA(tb[TCA_BASIC_CLASSID]), b1));
127 //if (tb[TCA_CANPRIO_RULES])
128 // fprintf(f, "Canid 0x%x ",
129 // *(__u32 *)RTA_DATA(tb[TCA_CANPRIO_MATCH]));
136 struct filter_util canprio_filter_util = {
138 .parse_fopt = canprio_parse_opt,
139 .print_fopt = canprio_print_opt,