blob: 4f3a3318eb81ad035e6f3d0de604d11c9d28ff53 [file] [log] [blame]
Fabrice MARIE147a2be2001-05-02 15:45:57 +00001#include <stdio.h>
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +00002#include <xtables.h>
3#include <linux/netfilter/xt_length.h>
Fabrice MARIE147a2be2001-05-02 15:45:57 +00004
Jan Engelhardtc15f9e32011-03-06 17:00:49 +01005enum {
6 O_LENGTH = 0,
7};
8
Jan Engelhardt181dead2007-10-04 16:27:07 +00009static void length_help(void)
Fabrice MARIE147a2be2001-05-02 15:45:57 +000010{
11 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020012"length match options:\n"
Fabrice MARIE147a2be2001-05-02 15:45:57 +000013"[!] --length length[:length] Match packet length against value or range\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020014" of values (inclusive)\n");
Fabrice MARIE147a2be2001-05-02 15:45:57 +000015}
16
Jan Engelhardtc15f9e32011-03-06 17:00:49 +010017static const struct xt_option_entry length_opts[] = {
18 {.name = "length", .id = O_LENGTH, .type = XTTYPE_UINT16RC,
19 .flags = XTOPT_MAND | XTOPT_INVERT},
20 XTOPT_TABLEEND,
Fabrice MARIE147a2be2001-05-02 15:45:57 +000021};
22
Jan Engelhardtc15f9e32011-03-06 17:00:49 +010023static void length_parse(struct xt_option_call *cb)
Fabrice MARIE147a2be2001-05-02 15:45:57 +000024{
Jan Engelhardtc15f9e32011-03-06 17:00:49 +010025 struct xt_length_info *info = cb->data;
Fabrice MARIE147a2be2001-05-02 15:45:57 +000026
Jan Engelhardtc15f9e32011-03-06 17:00:49 +010027 xtables_option_parse(cb);
28 info->min = cb->val.u16_range[0];
29 info->max = (cb->nvals == 2) ? cb->val.u16_range[1] : UINT16_MAX;
30 if (cb->invert)
31 info->invert = 1;
Fabrice MARIE147a2be2001-05-02 15:45:57 +000032}
33
Fabrice MARIE147a2be2001-05-02 15:45:57 +000034static void
Jan Engelhardtcea9f712008-12-09 15:06:20 +010035length_print(const void *ip, const struct xt_entry_match *match, int numeric)
Fabrice MARIE147a2be2001-05-02 15:45:57 +000036{
Jan Engelhardtcea9f712008-12-09 15:06:20 +010037 const struct xt_length_info *info = (void *)match->data;
38
Jan Engelhardt73866352010-12-18 02:04:59 +010039 printf(" length %s", info->invert ? "!" : "");
Jan Engelhardtcea9f712008-12-09 15:06:20 +010040 if (info->min == info->max)
Jan Engelhardt73866352010-12-18 02:04:59 +010041 printf("%u", info->min);
Fabrice MARIE147a2be2001-05-02 15:45:57 +000042 else
Jan Engelhardt73866352010-12-18 02:04:59 +010043 printf("%u:%u", info->min, info->max);
Fabrice MARIE147a2be2001-05-02 15:45:57 +000044}
45
Jan Engelhardt181dead2007-10-04 16:27:07 +000046static void length_save(const void *ip, const struct xt_entry_match *match)
Fabrice MARIE147a2be2001-05-02 15:45:57 +000047{
Jan Engelhardtcea9f712008-12-09 15:06:20 +010048 const struct xt_length_info *info = (void *)match->data;
49
Jan Engelhardt73866352010-12-18 02:04:59 +010050 printf("%s --length ", info->invert ? " !" : "");
Jan Engelhardtcea9f712008-12-09 15:06:20 +010051 if (info->min == info->max)
Jan Engelhardt73866352010-12-18 02:04:59 +010052 printf("%u", info->min);
Jan Engelhardtcea9f712008-12-09 15:06:20 +010053 else
Jan Engelhardt73866352010-12-18 02:04:59 +010054 printf("%u:%u", info->min, info->max);
Fabrice MARIE147a2be2001-05-02 15:45:57 +000055}
56
Jan Engelhardt181dead2007-10-04 16:27:07 +000057static struct xtables_match length_match = {
Jan Engelhardt42979362009-06-01 11:56:23 +020058 .family = NFPROTO_UNSPEC,
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +000059 .name = "length",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020060 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI36087d92007-07-24 07:15:03 +000061 .size = XT_ALIGN(sizeof(struct xt_length_info)),
62 .userspacesize = XT_ALIGN(sizeof(struct xt_length_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +000063 .help = length_help,
Jan Engelhardt181dead2007-10-04 16:27:07 +000064 .print = length_print,
65 .save = length_save,
Jan Engelhardtc15f9e32011-03-06 17:00:49 +010066 .x6_parse = length_parse,
67 .x6_options = length_opts,
Fabrice MARIE147a2be2001-05-02 15:45:57 +000068};
69
70void _init(void)
71{
Jan Engelhardt181dead2007-10-04 16:27:07 +000072 xtables_register_match(&length_match);
Fabrice MARIE147a2be2001-05-02 15:45:57 +000073}