blob: b60c57eeaa32661c4996d8a139c1349caf2a990b [file] [log] [blame]
Sampsa Rantad6aa9662001-07-30 13:30:14 +00001#include <stdio.h>
Sampsa Rantad6aa9662001-07-30 13:30:14 +00002#include <string.h>
3#include <stdlib.h>
Simon Lodalca9d8c22006-09-02 12:37:48 +00004#include <errno.h>
Sampsa Rantad6aa9662001-07-30 13:30:14 +00005#if defined(__GLIBC__) && __GLIBC__ == 2
6#include <net/ethernet.h>
7#else
8#include <linux/if_ether.h>
9#endif
Jan Engelhardt5d9678a2008-11-20 10:15:35 +010010#include <xtables.h>
Sampsa Rantad6aa9662001-07-30 13:30:14 +000011#include <linux/netfilter_ipv4/ipt_realm.h>
12
Jan Engelhardtf04d4882011-03-06 16:38:51 +010013enum {
14 O_REALM = 0,
15};
16
Jan Engelhardt59d16402007-10-04 16:28:39 +000017static void realm_help(void)
Sampsa Rantad6aa9662001-07-30 13:30:14 +000018{
19 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020020"realm match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020021"[!] --realm value[/mask]\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020022" Match realm\n");
Sampsa Rantad6aa9662001-07-30 13:30:14 +000023}
24
Jan Engelhardtf04d4882011-03-06 16:38:51 +010025static 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 Lodalca9d8c22006-09-02 12:37:48 +000029};
30
31/* array of realms from /etc/iproute2/rt_realms */
Jan Engelhardtf04d4882011-03-06 16:38:51 +010032static struct xtables_lmap *realms;
Simon Lodalca9d8c22006-09-02 12:37:48 +000033
Jan Engelhardtf04d4882011-03-06 16:38:51 +010034static void realm_init(struct xt_entry_match *m)
Simon Lodalca9d8c22006-09-02 12:37:48 +000035{
Jan Engelhardtf04d4882011-03-06 16:38:51 +010036 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
42static void realm_parse(struct xt_option_call *cb)
43{
44 struct ipt_realm_info *realminfo = cb->data;
Simon Lodalca9d8c22006-09-02 12:37:48 +000045 int id;
Jan Engelhardtf04d4882011-03-06 16:38:51 +010046 char *end;
Simon Lodalca9d8c22006-09-02 12:37:48 +000047
Jan Engelhardtf04d4882011-03-06 16:38:51 +010048 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 Lodalca9d8c22006-09-02 12:37:48 +000053 else
Sampsa Rantad6aa9662001-07-30 13:30:14 +000054 realminfo->mask = 0xffffffff;
Jan Engelhardtf04d4882011-03-06 16:38:51 +010055 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 Rantad6aa9662001-07-30 13:30:14 +000065 }
Jan Engelhardtf04d4882011-03-06 16:38:51 +010066 if (cb->invert)
67 realminfo->invert = 1;
Sampsa Rantad6aa9662001-07-30 13:30:14 +000068}
69
70static void
Simon Lodalca9d8c22006-09-02 12:37:48 +000071print_realm(unsigned long id, unsigned long mask, int numeric)
Sampsa Rantad6aa9662001-07-30 13:30:14 +000072{
Simon Lodalca9d8c22006-09-02 12:37:48 +000073 const char* name = NULL;
74
Harald Welte61d274f2005-02-08 15:54:17 +000075 if (mask != 0xffffffff)
Jan Engelhardt73866352010-12-18 02:04:59 +010076 printf(" 0x%lx/0x%lx", id, mask);
Simon Lodalca9d8c22006-09-02 12:37:48 +000077 else {
78 if (numeric == 0)
Jan Engelhardtf04d4882011-03-06 16:38:51 +010079 name = xtables_lmap_id2name(realms, id);
Simon Lodalca9d8c22006-09-02 12:37:48 +000080 if (name)
Jan Engelhardt73866352010-12-18 02:04:59 +010081 printf(" %s", name);
Simon Lodalca9d8c22006-09-02 12:37:48 +000082 else
Jan Engelhardt73866352010-12-18 02:04:59 +010083 printf(" 0x%lx", id);
Simon Lodalca9d8c22006-09-02 12:37:48 +000084 }
Sampsa Rantad6aa9662001-07-30 13:30:14 +000085}
86
Jan Engelhardt59d16402007-10-04 16:28:39 +000087static void realm_print(const void *ip, const struct xt_entry_match *match,
88 int numeric)
Sampsa Rantad6aa9662001-07-30 13:30:14 +000089{
Jan Engelhardt69f564e2009-05-26 13:14:06 +020090 const struct ipt_realm_info *ri = (const void *)match->data;
Harald Welte61d274f2005-02-08 15:54:17 +000091
92 if (ri->invert)
Jan Engelhardt73866352010-12-18 02:04:59 +010093 printf(" !");
Harald Welte61d274f2005-02-08 15:54:17 +000094
Jan Engelhardt73866352010-12-18 02:04:59 +010095 printf(" realm");
Simon Lodalca9d8c22006-09-02 12:37:48 +000096 print_realm(ri->id, ri->mask, numeric);
Sampsa Rantad6aa9662001-07-30 13:30:14 +000097}
98
Jan Engelhardt59d16402007-10-04 16:28:39 +000099static void realm_save(const void *ip, const struct xt_entry_match *match)
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000100{
Jan Engelhardt69f564e2009-05-26 13:14:06 +0200101 const struct ipt_realm_info *ri = (const void *)match->data;
Harald Welte61d274f2005-02-08 15:54:17 +0000102
103 if (ri->invert)
Jan Engelhardt73866352010-12-18 02:04:59 +0100104 printf(" !");
Harald Welte61d274f2005-02-08 15:54:17 +0000105
Jan Engelhardt73866352010-12-18 02:04:59 +0100106 printf(" --realm");
Simon Lodalca9d8c22006-09-02 12:37:48 +0000107 print_realm(ri->id, ri->mask, 0);
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000108}
109
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200110static struct xtables_match realm_mt_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000111 .name = "realm",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200112 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100113 .family = NFPROTO_IPV4,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200114 .size = XT_ALIGN(sizeof(struct ipt_realm_info)),
115 .userspacesize = XT_ALIGN(sizeof(struct ipt_realm_info)),
Jan Engelhardt59d16402007-10-04 16:28:39 +0000116 .help = realm_help,
Jan Engelhardtf04d4882011-03-06 16:38:51 +0100117 .init = realm_init,
Jan Engelhardt59d16402007-10-04 16:28:39 +0000118 .print = realm_print,
119 .save = realm_save,
Jan Engelhardtf04d4882011-03-06 16:38:51 +0100120 .x6_parse = realm_parse,
121 .x6_options = realm_opts,
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000122};
123
124void _init(void)
125{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200126 xtables_register_match(&realm_mt_reg);
Sampsa Rantad6aa9662001-07-30 13:30:14 +0000127}