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;
38 fprintf(stderr, "Usage: ... pedit <MUNGE>\n");
40 "Where: MUNGE := <RAW>|<LAYERED>\n"
41 "<RAW>:= <OFFSETC>[ATC]<CMD>\n "
42 "OFFSETC:= offset <offval> <u8|u16|u32>\n "
43 "ATC:= at <atval> offmask <maskval> shift <shiftval>\n "
44 "NOTE: offval is byte offset, must be multiple of 4\n "
45 "NOTE: maskval is a 32 bit hex number\n "
46 "NOTE: shiftval is a is a shift value\n "
47 "CMD:= clear | invert | set <setval>| retain\n "
48 "<LAYERED>:= ip <ipdata> | ip6 <ip6data> \n "
49 " | udp <udpdata> | tcp <tcpdata> | icmp <icmpdata> \n"
50 "For Example usage look at the examples directory");
54 #define usage() return(-1)
57 pedit_parse_nopopt (int *argc_p, char ***argv_p,struct tc_pedit_sel *sel,struct tc_pedit_key *tkey)
60 char **argv = *argv_p;
63 fprintf(stderr, "Unknown action hence option \"%s\" is unparsable\n", *argv);
72 *get_pedit_kind(char *str)
77 struct m_pedit_util *p;
79 for (p = pedit_list; p; p = p->next) {
80 if (strcmp(p->id, str) == 0)
84 snprintf(buf, sizeof(buf), "p_%s.so", str);
85 dlh = dlopen(buf, RTLD_LAZY);
89 dlh = pBODY = dlopen(NULL, RTLD_LAZY);
95 snprintf(buf, sizeof(buf), "p_pedit_%s", str);
101 p->next = pedit_list;
106 p = malloc(sizeof(*p));
108 memset(p, 0, sizeof(*p));
109 strncpy(p->id, str, sizeof(p->id)-1);
110 p->parse_peopt = pedit_parse_nopopt;
117 pack_key(struct tc_pedit_sel *sel,struct tc_pedit_key *tkey)
119 int hwm = sel->nkeys;
125 fprintf(stderr, "offsets MUST be in 32 bit boundaries\n");
129 sel->keys[hwm].val = tkey->val;
130 sel->keys[hwm].mask = tkey->mask;
131 sel->keys[hwm].off = tkey->off;
132 sel->keys[hwm].at = tkey->at;
133 sel->keys[hwm].offmask = tkey->offmask;
134 sel->keys[hwm].shift = tkey->shift;
141 pack_key32(__u32 retain,struct tc_pedit_sel *sel,struct tc_pedit_key *tkey)
143 if (tkey->off > (tkey->off & ~3)) {
145 "pack_key32: 32 bit offsets must begin in 32bit boundaries\n");
149 tkey->val = htonl(tkey->val & retain);
150 tkey->mask = htonl(tkey->mask | ~retain);
151 /* jamal remove this - it is not necessary given the if check above */
153 return pack_key(sel,tkey);
157 pack_key16(__u32 retain,struct tc_pedit_sel *sel,struct tc_pedit_key *tkey)
159 int ind = 0, stride = 0;
160 __u32 m[4] = {0xFFFF0000,0xFF0000FF,0x0000FFFF};
170 if (tkey->val > 0xFFFF || tkey->mask > 0xFFFF) {
171 fprintf(stderr, "pack_key16 bad value\n");
177 if (0 > ind || 2 < ind) {
178 fprintf(stderr, "pack_key16 bad index value %d\n",ind);
183 tkey->val = htons(tkey->val);
185 tkey->val <<= stride;
186 tkey->mask <<= stride;
189 tkey->mask = retain|m[ind];
194 printf("pack_key16: Final val %08x mask %08x \n",tkey->val,tkey->mask);
195 return pack_key(sel,tkey);
200 pack_key8(__u32 retain,struct tc_pedit_sel *sel,struct tc_pedit_key *tkey)
202 int ind = 0, stride = 0;
203 __u32 m[4] = {0xFFFFFF00,0xFFFF00FF,0xFF00FFFF,0x00FFFFFF};
213 if (tkey->val > 0xFF || tkey->mask > 0xFF) {
214 fprintf(stderr, "pack_key8 bad value (val %x mask %x\n", tkey->val, tkey->mask);
220 tkey->val <<= stride;
221 tkey->mask <<= stride;
223 tkey->mask = retain|m[ind];
227 printf("pack_key8: Final word off %d val %08x mask %08x \n",tkey->off , tkey->val,tkey->mask);
228 return pack_key(sel,tkey);
232 parse_val(int *argc_p, char ***argv_p, __u32 * val, int type)
235 char **argv = *argv_p;
241 return get_integer((int *) val, *argv, 0);
244 return get_u32(val, *argv, 0);
248 if (get_prefix_1(&addr, *argv, AF_INET)) {
255 /* not implemented yet */
263 parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type,__u32 retain,struct tc_pedit_sel *sel,struct tc_pedit_key *tkey)
265 __u32 mask = 0, val = 0;
269 char **argv = *argv_p;
275 printf("parse_cmd argc %d %s offset %d length %d\n",argc,*argv,tkey->off,len);
282 if (matches(*argv, "invert") == 0) {
283 retain = val = mask = o;
284 } else if (matches(*argv, "set") == 0) {
286 if (parse_val(&argc, &argv, &val, type))
288 } else if (matches(*argv, "preserve") == 0) {
291 if (matches(*argv, "clear") != 0)
297 if (argc && matches(*argv, "retain") == 0) {
299 if (parse_val(&argc, &argv, &retain, TU32))
308 res = pack_key8(retain,sel,tkey);
313 res = pack_key16(retain,sel,tkey);
318 res = pack_key32(retain,sel,tkey);
325 printf("parse_cmd done argc %d %s offset %d length %d\n",argc,*argv,tkey->off,len);
333 parse_offset(int *argc_p, char ***argv_p,struct tc_pedit_sel *sel,struct tc_pedit_key *tkey)
338 char **argv = *argv_p;
344 if (get_integer(&off, *argv, 0))
355 if (matches(*argv, "u32") == 0) {
360 if (matches(*argv, "u16") == 0) {
365 if (matches(*argv, "u8") == 0) {
377 /* [at <someval> offmask <maskval> shift <shiftval>] */
378 if (matches(*argv, "at") == 0) {
380 __u32 atv=0,offmask=0x0,shift=0;
383 if (get_u32(&atv, *argv, 0))
389 if (get_u32(&offmask, *argv, 16))
391 tkey->offmask = offmask;
395 if (get_u32(&shift, *argv, 0))
402 res = parse_cmd(&argc, &argv, len, TU32,retain,sel,tkey);
410 parse_munge(int *argc_p, char ***argv_p,struct tc_pedit_sel *sel)
412 struct tc_pedit_key tkey;
414 char **argv = *argv_p;
420 memset(&tkey, 0, sizeof(tkey));
422 if (matches(*argv, "offset") == 0) {
424 res = parse_offset(&argc, &argv,sel,&tkey);
427 } else if (strcmp(*argv, "help") == 0) {
433 struct m_pedit_util *p = NULL;
435 strncpy(k, *argv, sizeof (k) - 1);
438 p = get_pedit_kind(k);
441 res = p->parse_peopt(&argc, &argv, sel,&tkey);
443 fprintf(stderr,"bad pedit parsing\n");
461 parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
464 struct tc_pedit_sel sel;
465 struct tc_pedit_key keys[MAX_OFFS];
469 char **argv = *argv_p;
473 memset(&sel, 0, sizeof(sel));
477 fprintf(stderr, "while pedit (%d:%s)\n",argc, *argv);
478 if (matches(*argv, "pedit") == 0) {
482 } else if (matches(*argv, "munge") == 0) {
484 fprintf(stderr, "Illegal pedit construct (%s) \n", *argv);
489 if (parse_munge(&argc, &argv,&sel.sel)) {
490 fprintf(stderr, "Illegal pedit construct (%s) \n", *argv);
507 if (matches(*argv, "reclassify") == 0) {
508 sel.sel.action = TC_ACT_RECLASSIFY;
510 } else if (matches(*argv, "pipe") == 0) {
511 sel.sel.action = TC_ACT_PIPE;
513 } else if (matches(*argv, "drop") == 0 ||
514 matches(*argv, "shot") == 0) {
515 sel.sel.action = TC_ACT_SHOT;
517 } else if (matches(*argv, "continue") == 0) {
518 sel.sel.action = TC_ACT_UNSPEC;
520 } else if (matches(*argv, "pass") == 0) {
521 sel.sel.action = TC_ACT_OK;
527 if (matches(*argv, "index") == 0) {
529 if (get_u32(&sel.sel.index, *argv, 10)) {
530 fprintf(stderr, "Pedit: Illegal \"index\"\n");
539 tail = NLMSG_TAIL(n);
540 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
541 addattr_l(n, MAX_MSG, TCA_PEDIT_PARMS,&sel, sizeof(sel.sel)+sel.sel.nkeys*sizeof(struct tc_pedit_key));
542 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
550 print_pedit(struct action_util *au,FILE * f, struct rtattr *arg)
552 struct tc_pedit_sel *sel;
553 struct rtattr *tb[TCA_PEDIT_MAX + 1];
559 parse_rtattr_nested(tb, TCA_PEDIT_MAX, arg);
561 if (tb[TCA_PEDIT_PARMS] == NULL) {
562 fprintf(f, "[NULL pedit parameters]");
565 sel = RTA_DATA(tb[TCA_PEDIT_PARMS]);
567 fprintf(f, " pedit action %s keys %d\n ", action_n2a(sel->action, b1, sizeof (b1)),sel->nkeys);
568 fprintf(f, "\t index %d ref %d bind %d", sel->index,sel->refcnt, sel->bindcnt);
571 if (tb[TCA_PEDIT_TM]) {
572 struct tcf_t *tm = RTA_DATA(tb[TCA_PEDIT_TM]);
578 struct tc_pedit_key *key = sel->keys;
580 for (i=0; i<sel->nkeys; i++, key++) {
581 fprintf(f, "\n\t key #%d",i);
582 fprintf(f, " at %d: val %08x mask %08x",
583 (unsigned int)key->off,
584 (unsigned int)ntohl(key->val),
585 (unsigned int)ntohl(key->mask));
588 fprintf(f, "\npedit %x keys %d is not LEGIT", sel->index,sel->nkeys);
597 pedit_print_xstats(struct action_util *au, FILE *f, struct rtattr *xstats)
602 struct action_util pedit_action_util = {
604 .parse_aopt = parse_pedit,
605 .print_aopt = print_pedit,