blob: 3732034cbc79b22b79144402c26b52af78aa64f2 [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>
8#include <netdb.h>
9#include <string.h>
10#include <stdlib.h>
11#include <getopt.h>
12#if defined(__GLIBC__) && __GLIBC__ == 2
13#include <net/ethernet.h>
14#else
15#include <linux/if_ether.h>
16#endif
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +000017#include <xtables.h>
Harald Welteb5166472001-04-19 16:35:39 +000018#include <linux/if_packet.h>
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +000019#include <linux/netfilter/xt_pkttype.h>
Harald Welteb5166472001-04-19 16:35:39 +000020
21#define PKTTYPE_VERSION "0.1"
22
23struct pkttypes {
24 const char *name;
25 unsigned char pkttype;
26 unsigned char printhelp;
27 const char *help;
28};
29
30static const struct pkttypes supported_types[] = {
Harald Welteef225432002-08-07 09:54:45 +000031 {"unicast", PACKET_HOST, 1, "to us"},
Harald Welteb5166472001-04-19 16:35:39 +000032 {"broadcast", PACKET_BROADCAST, 1, "to all"},
33 {"multicast", PACKET_MULTICAST, 1, "to group"},
34/*
35 {"otherhost", PACKET_OTHERHOST, 1, "to someone else"},
36 {"outgoing", PACKET_OUTGOING, 1, "outgoing of any type"},
37*/
38 /* aliases */
39 {"bcast", PACKET_BROADCAST, 0, NULL},
40 {"mcast", PACKET_MULTICAST, 0, NULL},
Harald Welteef225432002-08-07 09:54:45 +000041 {"host", PACKET_HOST, 0, NULL}
Harald Welteb5166472001-04-19 16:35:39 +000042};
43
44static void print_types()
45{
46 unsigned int i;
47
48 printf("Valid packet types:\n");
49 for (i = 0; i < sizeof(supported_types)/sizeof(struct pkttypes); i++)
50 {
51 if(supported_types[i].printhelp == 1)
52 printf("\t%-14s\t\t%s\n", supported_types[i].name, supported_types[i].help);
53 }
54 printf("\n");
55}
56
57/* Function which prints out usage message. */
58static void help(void)
59{
60 printf(
61"pkt_type v%s options:\n"
62" --pkt-type [!] packettype\tmatch packet type\n"
63"\n", PKTTYPE_VERSION);
64 print_types();
65}
66
Jan Engelhardt661f1122007-07-30 14:46:51 +000067static const struct option opts[] = {
Harald Welteb5166472001-04-19 16:35:39 +000068 {"pkt-type", 1, 0, '1'},
69 {0}
70};
71
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +000072static void parse_pkttype(const char *pkttype, struct xt_pkttype_info *info)
Harald Welteb5166472001-04-19 16:35:39 +000073{
74 unsigned int i;
75
76 for (i = 0; i < sizeof(supported_types)/sizeof(struct pkttypes); i++)
77 {
78 if(strcasecmp(pkttype, supported_types[i].name)==0)
79 {
80 info->pkttype=supported_types[i].pkttype;
81 return;
82 }
83 }
84
85 exit_error(PARAMETER_PROBLEM, "Bad packet type '%s'", pkttype);
86}
87
88static int parse(int c, char **argv, int invert, unsigned int *flags,
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +000089 const void *entry,
Harald Welteb5166472001-04-19 16:35:39 +000090 unsigned int *nfcache,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000091 struct xt_entry_match **match)
Harald Welteb5166472001-04-19 16:35:39 +000092{
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +000093 struct xt_pkttype_info *info = (struct xt_pkttype_info *)(*match)->data;
Harald Welteb5166472001-04-19 16:35:39 +000094
95 switch(c)
96 {
97 case '1':
Harald Welteb77f1da2002-03-14 11:35:58 +000098 check_inverse(optarg, &invert, &optind, 0);
Harald Welteb5166472001-04-19 16:35:39 +000099 parse_pkttype(argv[optind-1], info);
100 if(invert)
101 info->invert=1;
102 *flags=1;
103 break;
104
105 default:
106 return 0;
107 }
108
109 return 1;
110}
111
112static void final_check(unsigned int flags)
113{
114 if (!flags)
115 exit_error(PARAMETER_PROBLEM, "You must specify `--pkt-type'");
116}
117
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +0000118static void print_pkttype(struct xt_pkttype_info *info)
Harald Welteb5166472001-04-19 16:35:39 +0000119{
120 unsigned int i;
121
122 for (i = 0; i < sizeof(supported_types)/sizeof(struct pkttypes); i++)
123 {
124 if(supported_types[i].pkttype==info->pkttype)
125 {
126 printf("%s ", supported_types[i].name);
127 return;
128 }
129 }
130
131 printf("%d ", info->pkttype); /* in case we didn't find an entry in named-packtes */
132}
133
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000134static void print(const void *ip, const struct xt_entry_match *match, int numeric)
Harald Welteb5166472001-04-19 16:35:39 +0000135{
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +0000136 struct xt_pkttype_info *info = (struct xt_pkttype_info *)match->data;
Harald Welteb5166472001-04-19 16:35:39 +0000137
138 printf("PKTTYPE %s= ", info->invert?"!":"");
139 print_pkttype(info);
140}
141
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000142static void save(const void *ip, const struct xt_entry_match *match)
Harald Welteb5166472001-04-19 16:35:39 +0000143{
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +0000144 struct xt_pkttype_info *info = (struct xt_pkttype_info *)match->data;
Harald Welteb5166472001-04-19 16:35:39 +0000145
146 printf("--pkt-type %s", info->invert?"! ":"");
147 print_pkttype(info);
148}
149
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +0000150static struct xtables_match pkttype = {
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +0000151 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000152 .name = "pkttype",
153 .version = IPTABLES_VERSION,
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +0000154 .size = XT_ALIGN(sizeof(struct xt_pkttype_info)),
155 .userspacesize = XT_ALIGN(sizeof(struct xt_pkttype_info)),
156 .help = &help,
157 .parse = &parse,
158 .final_check = &final_check,
159 .print = &print,
160 .save = &save,
161 .extra_opts = opts
162};
163
164static struct xtables_match pkttype6 = {
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +0000165 .family = AF_INET6,
166 .name = "pkttype",
167 .version = IPTABLES_VERSION,
168 .size = XT_ALIGN(sizeof(struct xt_pkttype_info)),
169 .userspacesize = XT_ALIGN(sizeof(struct xt_pkttype_info)),
Pablo Neira8caee8b2004-12-28 13:11:59 +0000170 .help = &help,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000171 .parse = &parse,
172 .final_check = &final_check,
173 .print = &print,
174 .save = &save,
175 .extra_opts = opts
Harald Welteb5166472001-04-19 16:35:39 +0000176};
177
178void _init(void)
179{
Yasuyuki KOZAKAI5fd6ec82007-07-24 07:05:45 +0000180 xtables_register_match(&pkttype);
181 xtables_register_match(&pkttype6);
Harald Welteb5166472001-04-19 16:35:39 +0000182}