2 * m_pedit.c generic packet editor actions module
4 * This program is free software; you can distribute 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.
9 * Authors: J Hadi Salim (hadi@cyberus.ca)
12 * 1) Big endian broken in some spots
13 * 2) A lot of this stuff was added on the fly; get a big double-double
14 * and clean it up at some point.
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
32 static struct m_pedit_util
*pedit_list
;
33 static int pedit_debug
;
35 static void explain(void)
37 fprintf(stderr
, "Usage: ... pedit munge <MUNGE> [CONTROL]\n");
39 "Where: MUNGE := <RAW>|<LAYERED>\n"
40 "\t<RAW>:= <OFFSETC>[ATC]<CMD>\n \t\tOFFSETC:= offset <offval> <u8|u16|u32>\n"
41 "\t\tATC:= at <atval> offmask <maskval> shift <shiftval>\n"
42 "\t\tNOTE: offval is byte offset, must be multiple of 4\n"
43 "\t\tNOTE: maskval is a 32 bit hex number\n \t\tNOTE: shiftval is a is a shift value\n"
44 "\t\tCMD:= clear | invert | set <setval>| retain\n"
45 "\t<LAYERED>:= ip <ipdata> | ip6 <ip6data>\n"
46 " \t\t| udp <udpdata> | tcp <tcpdata> | icmp <icmpdata>\n"
47 "\tCONTROL:= reclassify | pipe | drop | continue | pass\n"
48 "For Example usage look at the examples directory\n");
52 static void usage(void)
58 static int pedit_parse_nopopt(int *argc_p
, char ***argv_p
,
59 struct tc_pedit_sel
*sel
,
60 struct tc_pedit_key
*tkey
)
63 char **argv
= *argv_p
;
67 "Unknown action hence option \"%s\" is unparsable\n",
76 static struct m_pedit_util
*get_pedit_kind(const char *str
)
81 struct m_pedit_util
*p
;
83 for (p
= pedit_list
; p
; p
= p
->next
) {
84 if (strcmp(p
->id
, str
) == 0)
88 snprintf(buf
, sizeof(buf
), "p_%s.so", str
);
89 dlh
= dlopen(buf
, RTLD_LAZY
);
93 dlh
= pBODY
= dlopen(NULL
, RTLD_LAZY
);
99 snprintf(buf
, sizeof(buf
), "p_pedit_%s", str
);
105 p
->next
= pedit_list
;
110 p
= malloc(sizeof(*p
));
112 memset(p
, 0, sizeof(*p
));
113 strncpy(p
->id
, str
, sizeof(p
->id
) - 1);
114 p
->parse_peopt
= pedit_parse_nopopt
;
120 int pack_key(struct tc_pedit_sel
*sel
, struct tc_pedit_key
*tkey
)
122 int hwm
= sel
->nkeys
;
128 fprintf(stderr
, "offsets MUST be in 32 bit boundaries\n");
132 sel
->keys
[hwm
].val
= tkey
->val
;
133 sel
->keys
[hwm
].mask
= tkey
->mask
;
134 sel
->keys
[hwm
].off
= tkey
->off
;
135 sel
->keys
[hwm
].at
= tkey
->at
;
136 sel
->keys
[hwm
].offmask
= tkey
->offmask
;
137 sel
->keys
[hwm
].shift
= tkey
->shift
;
142 int pack_key32(__u32 retain
, struct tc_pedit_sel
*sel
,
143 struct tc_pedit_key
*tkey
)
145 if (tkey
->off
> (tkey
->off
& ~3)) {
147 "pack_key32: 32 bit offsets must begin in 32bit boundaries\n");
151 tkey
->val
= htonl(tkey
->val
& retain
);
152 tkey
->mask
= htonl(tkey
->mask
| ~retain
);
153 return pack_key(sel
, tkey
);
156 int pack_key16(__u32 retain
, struct tc_pedit_sel
*sel
,
157 struct tc_pedit_key
*tkey
)
160 __u32 m
[4] = { 0x0000FFFF, 0xFF0000FF, 0xFFFF0000 };
162 if (tkey
->val
> 0xFFFF || tkey
->mask
> 0xFFFF) {
163 fprintf(stderr
, "pack_key16 bad value\n");
170 fprintf(stderr
, "pack_key16 bad index value %d\n", ind
);
174 stride
= 8 * (2 - ind
);
175 tkey
->val
= htonl((tkey
->val
& retain
) << stride
);
176 tkey
->mask
= htonl(((tkey
->mask
| ~retain
) << stride
) | m
[ind
]);
181 printf("pack_key16: Final val %08x mask %08x\n",
182 tkey
->val
, tkey
->mask
);
183 return pack_key(sel
, tkey
);
187 int pack_key8(__u32 retain
, struct tc_pedit_sel
*sel
, struct tc_pedit_key
*tkey
)
190 __u32 m
[4] = { 0x00FFFFFF, 0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00 };
192 if (tkey
->val
> 0xFF || tkey
->mask
> 0xFF) {
193 fprintf(stderr
, "pack_key8 bad value (val %x mask %x\n",
194 tkey
->val
, tkey
->mask
);
200 stride
= 8 * (3 - ind
);
201 tkey
->val
= htonl((tkey
->val
& retain
) << stride
);
202 tkey
->mask
= htonl(((tkey
->mask
| ~retain
) << stride
) | m
[ind
]);
207 printf("pack_key8: Final word off %d val %08x mask %08x\n",
208 tkey
->off
, tkey
->val
, tkey
->mask
);
209 return pack_key(sel
, tkey
);
212 int parse_val(int *argc_p
, char ***argv_p
, __u32
*val
, int type
)
215 char **argv
= *argv_p
;
221 return get_integer((int *)val
, *argv
, 0);
224 return get_u32(val
, *argv
, 0);
229 if (get_prefix_1(&addr
, *argv
, AF_INET
))
237 return -1; /* not implemented yet */
242 int parse_cmd(int *argc_p
, char ***argv_p
, __u32 len
, int type
, __u32 retain
,
243 struct tc_pedit_sel
*sel
, struct tc_pedit_key
*tkey
)
245 __u32 mask
= 0, val
= 0;
249 char **argv
= *argv_p
;
255 printf("parse_cmd argc %d %s offset %d length %d\n",
256 argc
, *argv
, tkey
->off
, len
);
263 if (matches(*argv
, "invert") == 0) {
265 } else if (matches(*argv
, "set") == 0) {
267 if (parse_val(&argc
, &argv
, &val
, type
))
269 } else if (matches(*argv
, "preserve") == 0) {
272 if (matches(*argv
, "clear") != 0)
279 if (argc
&& matches(*argv
, "retain") == 0) {
281 if (parse_val(&argc
, &argv
, &retain
, TU32
))
291 tkey
->val
= ntohl(tkey
->val
);
294 res
= pack_key8(retain
, sel
, tkey
);
298 res
= pack_key16(retain
, sel
, tkey
);
302 res
= pack_key32(retain
, sel
, tkey
);
309 printf("parse_cmd done argc %d %s offset %d length %d\n",
310 argc
, *argv
, tkey
->off
, len
);
317 int parse_offset(int *argc_p
, char ***argv_p
, struct tc_pedit_sel
*sel
,
318 struct tc_pedit_key
*tkey
)
323 char **argv
= *argv_p
;
329 if (get_integer(&off
, *argv
, 0))
339 if (matches(*argv
, "u32") == 0) {
344 if (matches(*argv
, "u16") == 0) {
349 if (matches(*argv
, "u8") == 0) {
361 /* [at <someval> offmask <maskval> shift <shiftval>] */
362 if (matches(*argv
, "at") == 0) {
364 __u32 atv
= 0, offmask
= 0x0, shift
= 0;
367 if (get_u32(&atv
, *argv
, 0))
373 if (get_u32(&offmask
, *argv
, 16))
375 tkey
->offmask
= offmask
;
379 if (get_u32(&shift
, *argv
, 0))
386 res
= parse_cmd(&argc
, &argv
, len
, TU32
, retain
, sel
, tkey
);
393 static int parse_munge(int *argc_p
, char ***argv_p
, struct tc_pedit_sel
*sel
)
395 struct tc_pedit_key tkey
;
397 char **argv
= *argv_p
;
403 memset(&tkey
, 0, sizeof(tkey
));
405 if (matches(*argv
, "offset") == 0) {
407 res
= parse_offset(&argc
, &argv
, sel
, &tkey
);
411 struct m_pedit_util
*p
= NULL
;
413 strncpy(k
, *argv
, sizeof(k
) - 1);
416 p
= get_pedit_kind(k
);
420 res
= p
->parse_peopt(&argc
, &argv
, sel
, &tkey
);
422 fprintf(stderr
, "bad pedit parsing\n");
439 int parse_pedit(struct action_util
*a
, int *argc_p
, char ***argv_p
, int tca_id
,
443 struct tc_pedit_sel sel
;
444 struct tc_pedit_key keys
[MAX_OFFS
];
448 char **argv
= *argv_p
;
452 memset(&sel
, 0, sizeof(sel
));
456 fprintf(stderr
, "while pedit (%d:%s)\n", argc
, *argv
);
457 if (matches(*argv
, "pedit") == 0) {
461 } else if (matches(*argv
, "help") == 0) {
463 } else if (matches(*argv
, "munge") == 0) {
465 fprintf(stderr
, "Bad pedit construct (%s)\n",
471 if (parse_munge(&argc
, &argv
, &sel
.sel
)) {
472 fprintf(stderr
, "Bad pedit construct (%s)\n",
490 if (matches(*argv
, "reclassify") == 0) {
491 sel
.sel
.action
= TC_ACT_RECLASSIFY
;
493 } else if (matches(*argv
, "pipe") == 0) {
494 sel
.sel
.action
= TC_ACT_PIPE
;
496 } else if (matches(*argv
, "drop") == 0 ||
497 matches(*argv
, "shot") == 0) {
498 sel
.sel
.action
= TC_ACT_SHOT
;
500 } else if (matches(*argv
, "continue") == 0) {
501 sel
.sel
.action
= TC_ACT_UNSPEC
;
503 } else if (matches(*argv
, "pass") == 0 ||
504 matches(*argv
, "ok") == 0) {
505 sel
.sel
.action
= TC_ACT_OK
;
511 if (matches(*argv
, "index") == 0) {
513 if (get_u32(&sel
.sel
.index
, *argv
, 10)) {
514 fprintf(stderr
, "Pedit: Illegal \"index\"\n");
523 tail
= NLMSG_TAIL(n
);
524 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
525 addattr_l(n
, MAX_MSG
, TCA_PEDIT_PARMS
, &sel
,
527 sel
.sel
.nkeys
* sizeof(struct tc_pedit_key
));
528 tail
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)tail
;
535 int print_pedit(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
537 struct tc_pedit_sel
*sel
;
538 struct rtattr
*tb
[TCA_PEDIT_MAX
+ 1];
545 parse_rtattr_nested(tb
, TCA_PEDIT_MAX
, arg
);
547 if (tb
[TCA_PEDIT_PARMS
] == NULL
) {
548 fprintf(f
, "[NULL pedit parameters]");
551 sel
= RTA_DATA(tb
[TCA_PEDIT_PARMS
]);
553 fprintf(f
, " pedit action %s keys %d\n ",
554 action_n2a(sel
->action
, b1
, sizeof(b1
)), sel
->nkeys
);
555 fprintf(f
, "\t index %d ref %d bind %d", sel
->index
, sel
->refcnt
,
559 if (tb
[TCA_PEDIT_TM
]) {
560 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_PEDIT_TM
]);
567 struct tc_pedit_key
*key
= sel
->keys
;
569 for (i
= 0; i
< sel
->nkeys
; i
++, key
++) {
570 fprintf(f
, "\n\t key #%d", i
);
571 fprintf(f
, " at %d: val %08x mask %08x",
572 (unsigned int)key
->off
,
573 (unsigned int)ntohl(key
->val
),
574 (unsigned int)ntohl(key
->mask
));
577 fprintf(f
, "\npedit %x keys %d is not LEGIT", sel
->index
,
585 int pedit_print_xstats(struct action_util
*au
, FILE *f
, struct rtattr
*xstats
)
590 struct action_util pedit_action_util
= {
592 .parse_aopt
= parse_pedit
,
593 .print_aopt
= print_pedit
,