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 <MUNGE>\n");
40 "Where: MUNGE := <RAW>|<LAYERED>\n"
41 "\t<RAW>:= <OFFSETC>[ATC]<CMD>\n "
42 "\t\tOFFSETC:= offset <offval> <u8|u16|u32>\n "
43 "\t\tATC:= at <atval> offmask <maskval> shift <shiftval>\n "
44 "\t\tNOTE: offval is byte offset, must be multiple of 4\n "
45 "\t\tNOTE: maskval is a 32 bit hex number\n "
46 "\t\tNOTE: shiftval is a is a shift value\n "
47 "\t\tCMD:= clear | invert | set <setval>| retain\n "
48 "\t<LAYERED>:= ip <ipdata> | ip6 <ip6data> \n "
49 " \t\t| udp <udpdata> | tcp <tcpdata> | icmp <icmpdata> \n"
50 "For Example usage look at the examples directory\n");
62 pedit_parse_nopopt (int *argc_p
, char ***argv_p
,struct tc_pedit_sel
*sel
,struct tc_pedit_key
*tkey
)
65 char **argv
= *argv_p
;
68 fprintf(stderr
, "Unknown action hence option \"%s\" is unparsable\n", *argv
);
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
;
121 pack_key(struct tc_pedit_sel
*sel
,struct tc_pedit_key
*tkey
)
123 int hwm
= sel
->nkeys
;
129 fprintf(stderr
, "offsets MUST be in 32 bit boundaries\n");
133 sel
->keys
[hwm
].val
= tkey
->val
;
134 sel
->keys
[hwm
].mask
= tkey
->mask
;
135 sel
->keys
[hwm
].off
= tkey
->off
;
136 sel
->keys
[hwm
].at
= tkey
->at
;
137 sel
->keys
[hwm
].offmask
= tkey
->offmask
;
138 sel
->keys
[hwm
].shift
= tkey
->shift
;
145 pack_key32(__u32 retain
,struct tc_pedit_sel
*sel
,struct tc_pedit_key
*tkey
)
147 if (tkey
->off
> (tkey
->off
& ~3)) {
149 "pack_key32: 32 bit offsets must begin in 32bit boundaries\n");
153 tkey
->val
= htonl(tkey
->val
& retain
);
154 tkey
->mask
= htonl(tkey
->mask
| ~retain
);
155 /* jamal remove this - it is not necessary given the if check above */
157 return pack_key(sel
,tkey
);
161 pack_key16(__u32 retain
,struct tc_pedit_sel
*sel
,struct tc_pedit_key
*tkey
)
163 int ind
= 0, stride
= 0;
164 __u32 m
[4] = {0xFFFF0000,0xFF0000FF,0x0000FFFF};
174 if (tkey
->val
> 0xFFFF || tkey
->mask
> 0xFFFF) {
175 fprintf(stderr
, "pack_key16 bad value\n");
181 if (0 > ind
|| 2 < ind
) {
182 fprintf(stderr
, "pack_key16 bad index value %d\n",ind
);
187 tkey
->val
= htons(tkey
->val
);
189 tkey
->val
<<= stride
;
190 tkey
->mask
<<= stride
;
193 tkey
->mask
= retain
|m
[ind
];
198 printf("pack_key16: Final val %08x mask %08x \n",tkey
->val
,tkey
->mask
);
199 return pack_key(sel
,tkey
);
204 pack_key8(__u32 retain
,struct tc_pedit_sel
*sel
,struct tc_pedit_key
*tkey
)
206 int ind
= 0, stride
= 0;
207 __u32 m
[4] = {0xFFFFFF00,0xFFFF00FF,0xFF00FFFF,0x00FFFFFF};
217 if (tkey
->val
> 0xFF || tkey
->mask
> 0xFF) {
218 fprintf(stderr
, "pack_key8 bad value (val %x mask %x\n", tkey
->val
, tkey
->mask
);
224 tkey
->val
<<= stride
;
225 tkey
->mask
<<= stride
;
227 tkey
->mask
= retain
|m
[ind
];
231 printf("pack_key8: Final word off %d val %08x mask %08x \n",tkey
->off
, tkey
->val
,tkey
->mask
);
232 return pack_key(sel
,tkey
);
236 parse_val(int *argc_p
, char ***argv_p
, __u32
* val
, int type
)
239 char **argv
= *argv_p
;
245 return get_integer((int *) val
, *argv
, 0);
248 return get_u32(val
, *argv
, 0);
252 if (get_prefix_1(&addr
, *argv
, AF_INET
)) {
259 /* not implemented yet */
267 parse_cmd(int *argc_p
, char ***argv_p
, __u32 len
, int type
,__u32 retain
,struct tc_pedit_sel
*sel
,struct tc_pedit_key
*tkey
)
269 __u32 mask
= 0, val
= 0;
273 char **argv
= *argv_p
;
279 printf("parse_cmd argc %d %s offset %d length %d\n",argc
,*argv
,tkey
->off
,len
);
286 if (matches(*argv
, "invert") == 0) {
287 retain
= val
= mask
= o
;
288 } else if (matches(*argv
, "set") == 0) {
290 if (parse_val(&argc
, &argv
, &val
, type
))
292 } else if (matches(*argv
, "preserve") == 0) {
295 if (matches(*argv
, "clear") != 0)
301 if (argc
&& matches(*argv
, "retain") == 0) {
303 if (parse_val(&argc
, &argv
, &retain
, TU32
))
312 res
= pack_key8(retain
,sel
,tkey
);
317 res
= pack_key16(retain
,sel
,tkey
);
322 res
= pack_key32(retain
,sel
,tkey
);
329 printf("parse_cmd done argc %d %s offset %d length %d\n",argc
,*argv
,tkey
->off
,len
);
337 parse_offset(int *argc_p
, char ***argv_p
,struct tc_pedit_sel
*sel
,struct tc_pedit_key
*tkey
)
342 char **argv
= *argv_p
;
348 if (get_integer(&off
, *argv
, 0))
359 if (matches(*argv
, "u32") == 0) {
364 if (matches(*argv
, "u16") == 0) {
369 if (matches(*argv
, "u8") == 0) {
381 /* [at <someval> offmask <maskval> shift <shiftval>] */
382 if (matches(*argv
, "at") == 0) {
384 __u32 atv
=0,offmask
=0x0,shift
=0;
387 if (get_u32(&atv
, *argv
, 0))
393 if (get_u32(&offmask
, *argv
, 16))
395 tkey
->offmask
= offmask
;
399 if (get_u32(&shift
, *argv
, 0))
406 res
= parse_cmd(&argc
, &argv
, len
, TU32
,retain
,sel
,tkey
);
414 parse_munge(int *argc_p
, char ***argv_p
,struct tc_pedit_sel
*sel
)
416 struct tc_pedit_key tkey
;
418 char **argv
= *argv_p
;
424 memset(&tkey
, 0, sizeof(tkey
));
426 if (matches(*argv
, "offset") == 0) {
428 res
= parse_offset(&argc
, &argv
,sel
,&tkey
);
432 struct m_pedit_util
*p
= NULL
;
434 strncpy(k
, *argv
, sizeof (k
) - 1);
437 p
= get_pedit_kind(k
);
440 res
= p
->parse_peopt(&argc
, &argv
, sel
,&tkey
);
442 fprintf(stderr
,"bad pedit parsing\n");
460 parse_pedit(struct action_util
*a
, int *argc_p
, char ***argv_p
, int tca_id
, struct nlmsghdr
*n
)
463 struct tc_pedit_sel sel
;
464 struct tc_pedit_key keys
[MAX_OFFS
];
468 char **argv
= *argv_p
;
472 memset(&sel
, 0, sizeof(sel
));
476 fprintf(stderr
, "while pedit (%d:%s)\n",argc
, *argv
);
477 if (matches(*argv
, "pedit") == 0) {
481 } else if (matches(*argv
, "help") == 0) {
483 } else if (matches(*argv
, "munge") == 0) {
485 fprintf(stderr
, "Illegal pedit construct (%s) \n", *argv
);
490 if (parse_munge(&argc
, &argv
,&sel
.sel
)) {
491 fprintf(stderr
, "Illegal pedit construct (%s) \n", *argv
);
508 if (matches(*argv
, "reclassify") == 0) {
509 sel
.sel
.action
= TC_ACT_RECLASSIFY
;
511 } else if (matches(*argv
, "pipe") == 0) {
512 sel
.sel
.action
= TC_ACT_PIPE
;
514 } else if (matches(*argv
, "drop") == 0 ||
515 matches(*argv
, "shot") == 0) {
516 sel
.sel
.action
= TC_ACT_SHOT
;
518 } else if (matches(*argv
, "continue") == 0) {
519 sel
.sel
.action
= TC_ACT_UNSPEC
;
521 } else if (matches(*argv
, "pass") == 0) {
522 sel
.sel
.action
= TC_ACT_OK
;
528 if (matches(*argv
, "index") == 0) {
530 if (get_u32(&sel
.sel
.index
, *argv
, 10)) {
531 fprintf(stderr
, "Pedit: Illegal \"index\"\n");
540 tail
= NLMSG_TAIL(n
);
541 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
542 addattr_l(n
, MAX_MSG
, TCA_PEDIT_PARMS
,&sel
, sizeof(sel
.sel
)+sel
.sel
.nkeys
*sizeof(struct tc_pedit_key
));
543 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
551 print_pedit(struct action_util
*au
,FILE * f
, struct rtattr
*arg
)
553 struct tc_pedit_sel
*sel
;
554 struct rtattr
*tb
[TCA_PEDIT_MAX
+ 1];
560 parse_rtattr_nested(tb
, TCA_PEDIT_MAX
, arg
);
562 if (tb
[TCA_PEDIT_PARMS
] == NULL
) {
563 fprintf(f
, "[NULL pedit parameters]");
566 sel
= RTA_DATA(tb
[TCA_PEDIT_PARMS
]);
568 fprintf(f
, " pedit action %s keys %d\n ", action_n2a(sel
->action
, b1
, sizeof (b1
)),sel
->nkeys
);
569 fprintf(f
, "\t index %d ref %d bind %d", sel
->index
,sel
->refcnt
, sel
->bindcnt
);
572 if (tb
[TCA_PEDIT_TM
]) {
573 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_PEDIT_TM
]);
579 struct tc_pedit_key
*key
= sel
->keys
;
581 for (i
=0; i
<sel
->nkeys
; i
++, key
++) {
582 fprintf(f
, "\n\t key #%d",i
);
583 fprintf(f
, " at %d: val %08x mask %08x",
584 (unsigned int)key
->off
,
585 (unsigned int)ntohl(key
->val
),
586 (unsigned int)ntohl(key
->mask
));
589 fprintf(f
, "\npedit %x keys %d is not LEGIT", sel
->index
,sel
->nkeys
);
598 pedit_print_xstats(struct action_util
*au
, FILE *f
, struct rtattr
*xstats
)
603 struct action_util pedit_action_util
= {
605 .parse_aopt
= parse_pedit
,
606 .print_aopt
= print_pedit
,