Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 4 | #include <errno.h> |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 5 | #if defined(__GLIBC__) && __GLIBC__ == 2 |
| 6 | #include <net/ethernet.h> |
| 7 | #else |
| 8 | #include <linux/if_ether.h> |
| 9 | #endif |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 10 | #include <xtables.h> |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 11 | #include <linux/netfilter_ipv4/ipt_realm.h> |
| 12 | |
Jan Engelhardt | f04d488 | 2011-03-06 16:38:51 +0100 | [diff] [blame] | 13 | enum { |
| 14 | O_REALM = 0, |
| 15 | }; |
| 16 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 17 | static void realm_help(void) |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 18 | { |
| 19 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 20 | "realm match options:\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 21 | "[!] --realm value[/mask]\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 22 | " Match realm\n"); |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Jan Engelhardt | f04d488 | 2011-03-06 16:38:51 +0100 | [diff] [blame] | 25 | static const struct xt_option_entry realm_opts[] = { |
| 26 | {.name = "realm", .id = O_REALM, .type = XTTYPE_STRING, |
| 27 | .flags = XTOPT_MAND | XTOPT_INVERT}, |
| 28 | XTOPT_TABLEEND, |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | /* array of realms from /etc/iproute2/rt_realms */ |
Jan Engelhardt | f04d488 | 2011-03-06 16:38:51 +0100 | [diff] [blame] | 32 | static struct xtables_lmap *realms; |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 33 | |
Jan Engelhardt | f04d488 | 2011-03-06 16:38:51 +0100 | [diff] [blame] | 34 | static void realm_init(struct xt_entry_match *m) |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 35 | { |
Jan Engelhardt | f04d488 | 2011-03-06 16:38:51 +0100 | [diff] [blame] | 36 | const char file[] = "/etc/iproute2/rt_realms"; |
| 37 | realms = xtables_lmap_init(file); |
| 38 | if (realms == NULL && errno != ENOENT) |
| 39 | fprintf(stderr, "Warning: %s: %s\n", file, strerror(errno)); |
| 40 | } |
| 41 | |
| 42 | static void realm_parse(struct xt_option_call *cb) |
| 43 | { |
| 44 | struct ipt_realm_info *realminfo = cb->data; |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 45 | int id; |
Jan Engelhardt | f04d488 | 2011-03-06 16:38:51 +0100 | [diff] [blame] | 46 | char *end; |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 47 | |
Jan Engelhardt | f04d488 | 2011-03-06 16:38:51 +0100 | [diff] [blame] | 48 | xtables_option_parse(cb); |
| 49 | realminfo->id = strtoul(cb->arg, &end, 0); |
| 50 | if (end != cb->arg && (*end == '/' || *end == '\0')) { |
| 51 | if (*end == '/') |
| 52 | realminfo->mask = strtoul(end+1, &end, 0); |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 53 | else |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 54 | realminfo->mask = 0xffffffff; |
Jan Engelhardt | f04d488 | 2011-03-06 16:38:51 +0100 | [diff] [blame] | 55 | if (*end != '\0' || end == cb->arg) |
| 56 | xtables_error(PARAMETER_PROBLEM, |
| 57 | "Bad realm value \"%s\"", cb->arg); |
| 58 | } else { |
| 59 | id = xtables_lmap_name2id(realms, cb->arg); |
| 60 | if (id == -1) |
| 61 | xtables_error(PARAMETER_PROBLEM, |
| 62 | "Realm \"%s\" not found", cb->arg); |
| 63 | realminfo->id = id; |
| 64 | realminfo->mask = 0xffffffff; |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 65 | } |
Jan Engelhardt | f04d488 | 2011-03-06 16:38:51 +0100 | [diff] [blame] | 66 | if (cb->invert) |
| 67 | realminfo->invert = 1; |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 71 | print_realm(unsigned long id, unsigned long mask, int numeric) |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 72 | { |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 73 | const char* name = NULL; |
| 74 | |
Harald Welte | 61d274f | 2005-02-08 15:54:17 +0000 | [diff] [blame] | 75 | if (mask != 0xffffffff) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 76 | printf(" 0x%lx/0x%lx", id, mask); |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 77 | else { |
| 78 | if (numeric == 0) |
Jan Engelhardt | f04d488 | 2011-03-06 16:38:51 +0100 | [diff] [blame] | 79 | name = xtables_lmap_id2name(realms, id); |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 80 | if (name) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 81 | printf(" %s", name); |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 82 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 83 | printf(" 0x%lx", id); |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 84 | } |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 87 | static void realm_print(const void *ip, const struct xt_entry_match *match, |
| 88 | int numeric) |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 89 | { |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame] | 90 | const struct ipt_realm_info *ri = (const void *)match->data; |
Harald Welte | 61d274f | 2005-02-08 15:54:17 +0000 | [diff] [blame] | 91 | |
| 92 | if (ri->invert) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 93 | printf(" !"); |
Harald Welte | 61d274f | 2005-02-08 15:54:17 +0000 | [diff] [blame] | 94 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 95 | printf(" realm"); |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 96 | print_realm(ri->id, ri->mask, numeric); |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 99 | static void realm_save(const void *ip, const struct xt_entry_match *match) |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 100 | { |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame] | 101 | const struct ipt_realm_info *ri = (const void *)match->data; |
Harald Welte | 61d274f | 2005-02-08 15:54:17 +0000 | [diff] [blame] | 102 | |
| 103 | if (ri->invert) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 104 | printf(" !"); |
Harald Welte | 61d274f | 2005-02-08 15:54:17 +0000 | [diff] [blame] | 105 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 106 | printf(" --realm"); |
Simon Lodal | ca9d8c2 | 2006-09-02 12:37:48 +0000 | [diff] [blame] | 107 | print_realm(ri->id, ri->mask, 0); |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 110 | static struct xtables_match realm_mt_reg = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 111 | .name = "realm", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 112 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 113 | .family = NFPROTO_IPV4, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 114 | .size = XT_ALIGN(sizeof(struct ipt_realm_info)), |
| 115 | .userspacesize = XT_ALIGN(sizeof(struct ipt_realm_info)), |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 116 | .help = realm_help, |
Jan Engelhardt | f04d488 | 2011-03-06 16:38:51 +0100 | [diff] [blame] | 117 | .init = realm_init, |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 118 | .print = realm_print, |
| 119 | .save = realm_save, |
Jan Engelhardt | f04d488 | 2011-03-06 16:38:51 +0100 | [diff] [blame] | 120 | .x6_parse = realm_parse, |
| 121 | .x6_options = realm_opts, |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | void _init(void) |
| 125 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 126 | xtables_register_match(&realm_mt_reg); |
Sampsa Ranta | d6aa966 | 2001-07-30 13:30:14 +0000 | [diff] [blame] | 127 | } |