blob: 1ed3b445c32cbce9a6638aa364b74c2538b65867 [file] [log] [blame]
Harald Welteb5166472001-04-19 16:35:39 +00001/*
2 * Shared library add-on to iptables to match
3 * packets by their type (BROADCAST, UNICAST, MULTICAST).
4 *
5 * Michal Ludvig <michal@logix.cz>
6 */
7#include <stdio.h>
Harald Welteb5166472001-04-19 16:35:39 +00008#include <string.h>
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +00009#include <xtables.h>
Harald Welteb5166472001-04-19 16:35:39 +000010#include <linux/if_packet.h>
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +000011#include <linux/netfilter/xt_pkttype.h>
Harald Welteb5166472001-04-19 16:35:39 +000012
Jan Engelhardtde31da32011-03-02 19:19:16 +010013enum {
14 O_PKTTYPE = 0,
15};
Harald Welteb5166472001-04-19 16:35:39 +000016
17struct pkttypes {
18 const char *name;
19 unsigned char pkttype;
20 unsigned char printhelp;
21 const char *help;
22};
23
24static const struct pkttypes supported_types[] = {
Harald Welteef225432002-08-07 09:54:45 +000025 {"unicast", PACKET_HOST, 1, "to us"},
Harald Welteb5166472001-04-19 16:35:39 +000026 {"broadcast", PACKET_BROADCAST, 1, "to all"},
27 {"multicast", PACKET_MULTICAST, 1, "to group"},
28/*
29 {"otherhost", PACKET_OTHERHOST, 1, "to someone else"},
30 {"outgoing", PACKET_OUTGOING, 1, "outgoing of any type"},
31*/
32 /* aliases */
33 {"bcast", PACKET_BROADCAST, 0, NULL},
34 {"mcast", PACKET_MULTICAST, 0, NULL},
Harald Welteef225432002-08-07 09:54:45 +000035 {"host", PACKET_HOST, 0, NULL}
Harald Welteb5166472001-04-19 16:35:39 +000036};
37
Patrick McHardy500f4832007-09-08 15:59:04 +000038static void print_types(void)
Harald Welteb5166472001-04-19 16:35:39 +000039{
40 unsigned int i;
41
42 printf("Valid packet types:\n");
Jan Engelhardt2c69b552009-04-30 19:32:02 +020043 for (i = 0; i < ARRAY_SIZE(supported_types); ++i)
Harald Welteb5166472001-04-19 16:35:39 +000044 if(supported_types[i].printhelp == 1)
45 printf("\t%-14s\t\t%s\n", supported_types[i].name, supported_types[i].help);
Harald Welteb5166472001-04-19 16:35:39 +000046 printf("\n");
47}
48
Jan Engelhardt181dead2007-10-04 16:27:07 +000049static void pkttype_help(void)
Harald Welteb5166472001-04-19 16:35:39 +000050{
51 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020052"pkttype match options:\n"
Jan Engelhardt9b488b92008-06-08 19:11:51 +020053"[!] --pkt-type packettype match packet type\n");
Harald Welteb5166472001-04-19 16:35:39 +000054 print_types();
55}
56
Jan Engelhardtde31da32011-03-02 19:19:16 +010057static const struct xt_option_entry pkttype_opts[] = {
58 {.name = "pkt-type", .id = O_PKTTYPE, .type = XTTYPE_STRING,
59 .flags = XTOPT_MAND | XTOPT_INVERT},
60 XTOPT_TABLEEND,
Harald Welteb5166472001-04-19 16:35:39 +000061};
62
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +000063static void parse_pkttype(const char *pkttype, struct xt_pkttype_info *info)
Harald Welteb5166472001-04-19 16:35:39 +000064{
65 unsigned int i;
66
Jan Engelhardt2c69b552009-04-30 19:32:02 +020067 for (i = 0; i < ARRAY_SIZE(supported_types); ++i)
Harald Welteb5166472001-04-19 16:35:39 +000068 if(strcasecmp(pkttype, supported_types[i].name)==0)
69 {
70 info->pkttype=supported_types[i].pkttype;
71 return;
72 }
Harald Welteb5166472001-04-19 16:35:39 +000073
Jan Engelhardt1829ed42009-02-21 03:29:44 +010074 xtables_error(PARAMETER_PROBLEM, "Bad packet type '%s'", pkttype);
Harald Welteb5166472001-04-19 16:35:39 +000075}
76
Jan Engelhardtde31da32011-03-02 19:19:16 +010077static void pkttype_parse(struct xt_option_call *cb)
Harald Welteb5166472001-04-19 16:35:39 +000078{
Jan Engelhardtde31da32011-03-02 19:19:16 +010079 struct xt_pkttype_info *info = cb->data;
Harald Welteb5166472001-04-19 16:35:39 +000080
Jan Engelhardtde31da32011-03-02 19:19:16 +010081 xtables_option_parse(cb);
82 parse_pkttype(cb->arg, info);
83 if (cb->invert)
84 info->invert = 1;
Harald Welteb5166472001-04-19 16:35:39 +000085}
86
Jan Engelhardt69f564e2009-05-26 13:14:06 +020087static void print_pkttype(const struct xt_pkttype_info *info)
Harald Welteb5166472001-04-19 16:35:39 +000088{
89 unsigned int i;
90
Jan Engelhardt2c69b552009-04-30 19:32:02 +020091 for (i = 0; i < ARRAY_SIZE(supported_types); ++i)
Harald Welteb5166472001-04-19 16:35:39 +000092 if(supported_types[i].pkttype==info->pkttype)
93 {
Jan Engelhardt73866352010-12-18 02:04:59 +010094 printf("%s", supported_types[i].name);
Harald Welteb5166472001-04-19 16:35:39 +000095 return;
96 }
Harald Welteb5166472001-04-19 16:35:39 +000097
Jan Engelhardt73866352010-12-18 02:04:59 +010098 printf("%d", info->pkttype); /* in case we didn't find an entry in named-packtes */
Harald Welteb5166472001-04-19 16:35:39 +000099}
100
Jan Engelhardt181dead2007-10-04 16:27:07 +0000101static void pkttype_print(const void *ip, const struct xt_entry_match *match,
102 int numeric)
Harald Welteb5166472001-04-19 16:35:39 +0000103{
Jan Engelhardt69f564e2009-05-26 13:14:06 +0200104 const struct xt_pkttype_info *info = (const void *)match->data;
Harald Welteb5166472001-04-19 16:35:39 +0000105
Jan Engelhardt73866352010-12-18 02:04:59 +0100106 printf(" PKTTYPE %s= ", info->invert ? "!" : "");
Harald Welteb5166472001-04-19 16:35:39 +0000107 print_pkttype(info);
108}
109
Jan Engelhardt181dead2007-10-04 16:27:07 +0000110static void pkttype_save(const void *ip, const struct xt_entry_match *match)
Harald Welteb5166472001-04-19 16:35:39 +0000111{
Jan Engelhardt69f564e2009-05-26 13:14:06 +0200112 const struct xt_pkttype_info *info = (const void *)match->data;
Harald Welteb5166472001-04-19 16:35:39 +0000113
Jan Engelhardt73866352010-12-18 02:04:59 +0100114 printf("%s --pkt-type ", info->invert ? " !" : "");
Harald Welteb5166472001-04-19 16:35:39 +0000115 print_pkttype(info);
116}
117
Jan Engelhardt181dead2007-10-04 16:27:07 +0000118static struct xtables_match pkttype_match = {
Jan Engelhardt42979362009-06-01 11:56:23 +0200119 .family = NFPROTO_UNSPEC,
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +0000120 .name = "pkttype",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200121 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +0000122 .size = XT_ALIGN(sizeof(struct xt_pkttype_info)),
123 .userspacesize = XT_ALIGN(sizeof(struct xt_pkttype_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000124 .help = pkttype_help,
Jan Engelhardt181dead2007-10-04 16:27:07 +0000125 .print = pkttype_print,
126 .save = pkttype_save,
Jan Engelhardtde31da32011-03-02 19:19:16 +0100127 .x6_parse = pkttype_parse,
128 .x6_options = pkttype_opts,
Harald Welteb5166472001-04-19 16:35:39 +0000129};
130
131void _init(void)
132{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000133 xtables_register_match(&pkttype_match);
Harald Welteb5166472001-04-19 16:35:39 +0000134}