Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 1 | /* |
| 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 KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 17 | #include <xtables.h> |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 18 | #include <linux/if_packet.h> |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 19 | #include <linux/netfilter/xt_pkttype.h> |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 20 | |
| 21 | #define PKTTYPE_VERSION "0.1" |
| 22 | |
| 23 | struct pkttypes { |
| 24 | const char *name; |
| 25 | unsigned char pkttype; |
| 26 | unsigned char printhelp; |
| 27 | const char *help; |
| 28 | }; |
| 29 | |
| 30 | static const struct pkttypes supported_types[] = { |
Harald Welte | ef22543 | 2002-08-07 09:54:45 +0000 | [diff] [blame] | 31 | {"unicast", PACKET_HOST, 1, "to us"}, |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 32 | {"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 Welte | ef22543 | 2002-08-07 09:54:45 +0000 | [diff] [blame] | 41 | {"host", PACKET_HOST, 0, NULL} |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame^] | 44 | static void print_types(void) |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 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. */ |
| 58 | static 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 Engelhardt | 661f112 | 2007-07-30 14:46:51 +0000 | [diff] [blame] | 67 | static const struct option opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame^] | 68 | {"pkt-type", 1, NULL, '1'}, |
| 69 | { } |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 72 | static void parse_pkttype(const char *pkttype, struct xt_pkttype_info *info) |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 73 | { |
| 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 | |
| 88 | static int parse(int c, char **argv, int invert, unsigned int *flags, |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 89 | const void *entry, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 90 | struct xt_entry_match **match) |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 91 | { |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 92 | struct xt_pkttype_info *info = (struct xt_pkttype_info *)(*match)->data; |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 93 | |
| 94 | switch(c) |
| 95 | { |
| 96 | case '1': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 97 | check_inverse(optarg, &invert, &optind, 0); |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 98 | parse_pkttype(argv[optind-1], info); |
| 99 | if(invert) |
| 100 | info->invert=1; |
| 101 | *flags=1; |
| 102 | break; |
| 103 | |
| 104 | default: |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | static void final_check(unsigned int flags) |
| 112 | { |
| 113 | if (!flags) |
| 114 | exit_error(PARAMETER_PROBLEM, "You must specify `--pkt-type'"); |
| 115 | } |
| 116 | |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 117 | static void print_pkttype(struct xt_pkttype_info *info) |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 118 | { |
| 119 | unsigned int i; |
| 120 | |
| 121 | for (i = 0; i < sizeof(supported_types)/sizeof(struct pkttypes); i++) |
| 122 | { |
| 123 | if(supported_types[i].pkttype==info->pkttype) |
| 124 | { |
| 125 | printf("%s ", supported_types[i].name); |
| 126 | return; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | printf("%d ", info->pkttype); /* in case we didn't find an entry in named-packtes */ |
| 131 | } |
| 132 | |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 133 | static void print(const void *ip, const struct xt_entry_match *match, int numeric) |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 134 | { |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 135 | struct xt_pkttype_info *info = (struct xt_pkttype_info *)match->data; |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 136 | |
| 137 | printf("PKTTYPE %s= ", info->invert?"!":""); |
| 138 | print_pkttype(info); |
| 139 | } |
| 140 | |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 141 | static void save(const void *ip, const struct xt_entry_match *match) |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 142 | { |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 143 | struct xt_pkttype_info *info = (struct xt_pkttype_info *)match->data; |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 144 | |
| 145 | printf("--pkt-type %s", info->invert?"! ":""); |
| 146 | print_pkttype(info); |
| 147 | } |
| 148 | |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 149 | static struct xtables_match pkttype = { |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 150 | .family = AF_INET, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 151 | .name = "pkttype", |
| 152 | .version = IPTABLES_VERSION, |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 153 | .size = XT_ALIGN(sizeof(struct xt_pkttype_info)), |
| 154 | .userspacesize = XT_ALIGN(sizeof(struct xt_pkttype_info)), |
| 155 | .help = &help, |
| 156 | .parse = &parse, |
| 157 | .final_check = &final_check, |
| 158 | .print = &print, |
| 159 | .save = &save, |
| 160 | .extra_opts = opts |
| 161 | }; |
| 162 | |
| 163 | static struct xtables_match pkttype6 = { |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 164 | .family = AF_INET6, |
| 165 | .name = "pkttype", |
| 166 | .version = IPTABLES_VERSION, |
| 167 | .size = XT_ALIGN(sizeof(struct xt_pkttype_info)), |
| 168 | .userspacesize = XT_ALIGN(sizeof(struct xt_pkttype_info)), |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 169 | .help = &help, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 170 | .parse = &parse, |
| 171 | .final_check = &final_check, |
| 172 | .print = &print, |
| 173 | .save = &save, |
| 174 | .extra_opts = opts |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | void _init(void) |
| 178 | { |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 179 | xtables_register_match(&pkttype); |
| 180 | xtables_register_match(&pkttype6); |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 181 | } |