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 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 57 | static void pkttype_help(void) |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 58 | { |
| 59 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 60 | "pkttype match options:\n" |
Jan Engelhardt | 9b488b9 | 2008-06-08 19:11:51 +0200 | [diff] [blame] | 61 | "[!] --pkt-type packettype match packet type\n"); |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 62 | print_types(); |
| 63 | } |
| 64 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 65 | static const struct option pkttype_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 66 | {"pkt-type", 1, NULL, '1'}, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 67 | { .name = NULL } |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 70 | static void parse_pkttype(const char *pkttype, struct xt_pkttype_info *info) |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 71 | { |
| 72 | unsigned int i; |
| 73 | |
| 74 | for (i = 0; i < sizeof(supported_types)/sizeof(struct pkttypes); i++) |
| 75 | { |
| 76 | if(strcasecmp(pkttype, supported_types[i].name)==0) |
| 77 | { |
| 78 | info->pkttype=supported_types[i].pkttype; |
| 79 | return; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | exit_error(PARAMETER_PROBLEM, "Bad packet type '%s'", pkttype); |
| 84 | } |
| 85 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 86 | static int pkttype_parse(int c, char **argv, int invert, unsigned int *flags, |
| 87 | const void *entry, struct xt_entry_match **match) |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 88 | { |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 89 | struct xt_pkttype_info *info = (struct xt_pkttype_info *)(*match)->data; |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 90 | |
| 91 | switch(c) |
| 92 | { |
| 93 | case '1': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 94 | check_inverse(optarg, &invert, &optind, 0); |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 95 | parse_pkttype(argv[optind-1], info); |
| 96 | if(invert) |
| 97 | info->invert=1; |
| 98 | *flags=1; |
| 99 | break; |
| 100 | |
| 101 | default: |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | return 1; |
| 106 | } |
| 107 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 108 | static void pkttype_check(unsigned int flags) |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 109 | { |
| 110 | if (!flags) |
| 111 | exit_error(PARAMETER_PROBLEM, "You must specify `--pkt-type'"); |
| 112 | } |
| 113 | |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 114 | static void print_pkttype(struct xt_pkttype_info *info) |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 115 | { |
| 116 | unsigned int i; |
| 117 | |
| 118 | for (i = 0; i < sizeof(supported_types)/sizeof(struct pkttypes); i++) |
| 119 | { |
| 120 | if(supported_types[i].pkttype==info->pkttype) |
| 121 | { |
| 122 | printf("%s ", supported_types[i].name); |
| 123 | return; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | printf("%d ", info->pkttype); /* in case we didn't find an entry in named-packtes */ |
| 128 | } |
| 129 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 130 | static void pkttype_print(const void *ip, const struct xt_entry_match *match, |
| 131 | int numeric) |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 132 | { |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 133 | struct xt_pkttype_info *info = (struct xt_pkttype_info *)match->data; |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 134 | |
| 135 | printf("PKTTYPE %s= ", info->invert?"!":""); |
| 136 | print_pkttype(info); |
| 137 | } |
| 138 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 139 | static void pkttype_save(const void *ip, const struct xt_entry_match *match) |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 140 | { |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 141 | struct xt_pkttype_info *info = (struct xt_pkttype_info *)match->data; |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 142 | |
Jan Engelhardt | cea9f71 | 2008-12-09 15:06:20 +0100 | [diff] [blame] | 143 | printf("%s--pkt-type ", info->invert ? "! " : ""); |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 144 | print_pkttype(info); |
| 145 | } |
| 146 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 147 | static struct xtables_match pkttype_match = { |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 148 | .family = AF_UNSPEC, |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 149 | .name = "pkttype", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 150 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | 5fd6ec8 | 2007-07-24 07:05:45 +0000 | [diff] [blame] | 151 | .size = XT_ALIGN(sizeof(struct xt_pkttype_info)), |
| 152 | .userspacesize = XT_ALIGN(sizeof(struct xt_pkttype_info)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 153 | .help = pkttype_help, |
| 154 | .parse = pkttype_parse, |
| 155 | .final_check = pkttype_check, |
| 156 | .print = pkttype_print, |
| 157 | .save = pkttype_save, |
| 158 | .extra_opts = pkttype_opts, |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | void _init(void) |
| 162 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 163 | xtables_register_match(&pkttype_match); |
Harald Welte | b516647 | 2001-04-19 16:35:39 +0000 | [diff] [blame] | 164 | } |