| 1 |
/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 |
/* |
| 3 |
* em_ipt.c IPtables extensions matching Ematch |
| 4 |
* |
| 5 |
* (C) 2018 Eyal Birger <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <getopt.h> |
| 9 |
|
| 10 |
#include <linux/tc_ematch/tc_em_ipt.h> |
| 11 |
#include <linux/pkt_cls.h> |
| 12 |
#include <xtables.h> |
| 13 |
#include "m_ematch.h" |
| 14 |
|
| 15 |
static void em_ipt_print_usage(FILE *fd) |
| 16 |
{ |
| 17 |
fprintf(fd, |
| 18 |
"Usage: ipt([-6] -m MATCH_NAME [MATCH_OPTS])\n" |
| 19 |
"Example: 'ipt(-m policy --reqid 1 --pol ipsec --dir in)'\n"); |
| 20 |
} |
| 21 |
|
| 22 |
static struct option original_opts[] = { |
| 23 |
{ |
| 24 |
.name = "match", |
| 25 |
.has_arg = 1, |
| 26 |
.val = 'm' |
| 27 |
}, |
| 28 |
{ |
| 29 |
.name = "ipv6", |
| 30 |
.val = '6' |
| 31 |
}, |
| 32 |
{} |
| 33 |
}; |
| 34 |
|
| 35 |
static struct xtables_globals em_tc_ipt_globals = { |
| 36 |
.option_offset = 0, |
| 37 |
.program_name = "tc-em-ipt", |
| 38 |
.program_version = "0.1", |
| 39 |
.orig_opts = original_opts, |
| 40 |
.opts = original_opts, |
| 41 |
#if (XTABLES_VERSION_CODE >= 11) |
| 42 |
.compat_rev = xtables_compatible_revision, |
| 43 |
#endif |
| 44 |
}; |
| 45 |
|
| 46 |
static struct xt_entry_match *fake_xt_entry_match(int data_size, void *data) |
| 47 |
{ |
| 48 |
struct xt_entry_match *m; |
| 49 |
|
| 50 |
m = xtables_calloc(1, XT_ALIGN(sizeof(*m)) + data_size); |
| 51 |
if (!m) |
| 52 |
return NULL; |
| 53 |
|
| 54 |
if (data) |
| 55 |
memcpy(m->data, data, data_size); |
| 56 |
|
| 57 |
m->u.user.match_size = data_size; |
| 58 |
return m; |
| 59 |
} |
| 60 |
|
| 61 |
static void scrub_match(struct xtables_match *match) |
| 62 |
{ |
| 63 |
match->mflags = 0; |
| 64 |
free(match->m); |
| 65 |
match->m = NULL; |
| 66 |
} |
| 67 |
|
| 68 |
/* IPv4 and IPv6 share the same hooking enumeration */ |
| 69 |
#define HOOK_PRE_ROUTING 0 |
| 70 |
#define HOOK_POST_ROUTING 4 |
| 71 |
|
| 72 |
static __u32 em_ipt_hook(struct nlmsghdr *n) |
| 73 |
{ |
| 74 |
struct tcmsg *t = NLMSG_DATA(n); |
| 75 |
|
| 76 |
if (t->tcm_parent != TC_H_ROOT && |
| 77 |
t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) |
| 78 |
return HOOK_PRE_ROUTING; |
| 79 |
|
| 80 |
return HOOK_POST_ROUTING; |
| 81 |
} |
| 82 |
|
| 83 |
static int em_ipt_parse_eopt_argv(struct nlmsghdr *n, |
| 84 |
struct tcf_ematch_hdr *hdr, |
| 85 |
int argc, char **argv) |
| 86 |
{ |
| 87 |
struct xtables_globals tmp_tcipt_globals = em_tc_ipt_globals; |
| 88 |
struct xtables_match *match = NULL; |
| 89 |
__u8 nfproto = NFPROTO_IPV4; |
| 90 |
|
| 91 |
while (1) { |
| 92 |
struct option *opts; |
| 93 |
int c; |
| 94 |
|
| 95 |
c = getopt_long(argc, argv, "6m:", tmp_tcipt_globals.opts, |
| 96 |
NULL); |
| 97 |
if (c == -1) |
| 98 |
break; |
| 99 |
|
| 100 |
switch (c) { |
| 101 |
case 'm': |
| 102 |
xtables_init_all(&tmp_tcipt_globals, nfproto); |
| 103 |
|
| 104 |
match = xtables_find_match(optarg, XTF_TRY_LOAD, NULL); |
| 105 |
if (!match || !match->x6_parse) { |
| 106 |
fprintf(stderr, " failed to find match %s\n\n", |
| 107 |
optarg); |
| 108 |
return -1; |
| 109 |
} |
| 110 |
|
| 111 |
match->m = fake_xt_entry_match(match->size, NULL); |
| 112 |
if (!match->m) { |
| 113 |
printf(" %s error\n", match->name); |
| 114 |
return -1; |
| 115 |
} |
| 116 |
|
| 117 |
if (match->init) |
| 118 |
match->init(match->m); |
| 119 |
|
| 120 |
opts = xtables_options_xfrm(tmp_tcipt_globals.orig_opts, |
| 121 |
tmp_tcipt_globals.opts, |
| 122 |
match->x6_options, |
| 123 |
&match->option_offset); |
| 124 |
if (!opts) { |
| 125 |
scrub_match(match); |
| 126 |
return -1; |
| 127 |
} |
| 128 |
|
| 129 |
tmp_tcipt_globals.opts = opts; |
| 130 |
break; |
| 131 |
|
| 132 |
case '6': |
| 133 |
nfproto = NFPROTO_IPV6; |
| 134 |
break; |
| 135 |
|
| 136 |
default: |
| 137 |
if (!match) { |
| 138 |
fprintf(stderr, "failed to find match %s\n\n", |
| 139 |
optarg); |
| 140 |
return -1; |
| 141 |
|
| 142 |
} |
| 143 |
xtables_option_mpcall(c, argv, 0, match, NULL); |
| 144 |
break; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if (!match) { |
| 149 |
fprintf(stderr, " failed to parse parameters (%s)\n", *argv); |
| 150 |
return -1; |
| 151 |
} |
| 152 |
|
| 153 |
/* check that we passed the correct parameters to the match */ |
| 154 |
xtables_option_mfcall(match); |
| 155 |
|
| 156 |
addraw_l(n, MAX_MSG, hdr, sizeof(*hdr)); |
| 157 |
addattr32(n, MAX_MSG, TCA_EM_IPT_HOOK, em_ipt_hook(n)); |
| 158 |
addattrstrz(n, MAX_MSG, TCA_EM_IPT_MATCH_NAME, match->name); |
| 159 |
addattr8(n, MAX_MSG, TCA_EM_IPT_MATCH_REVISION, match->revision); |
| 160 |
addattr8(n, MAX_MSG, TCA_EM_IPT_NFPROTO, nfproto); |
| 161 |
addattr_l(n, MAX_MSG, TCA_EM_IPT_MATCH_DATA, match->m->data, |
| 162 |
match->size); |
| 163 |
|
| 164 |
xtables_free_opts(1); |
| 165 |
|
| 166 |
scrub_match(match); |
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
static int em_ipt_print_epot(FILE *fd, struct tcf_ematch_hdr *hdr, void *data, |
| 171 |
int data_len) |
| 172 |
{ |
| 173 |
struct rtattr *tb[TCA_EM_IPT_MAX + 1]; |
| 174 |
struct xtables_match *match; |
| 175 |
const char *mname; |
| 176 |
__u8 nfproto; |
| 177 |
|
| 178 |
if (parse_rtattr(tb, TCA_EM_IPT_MAX, data, data_len) < 0) |
| 179 |
return -1; |
| 180 |
|
| 181 |
nfproto = rta_getattr_u8(tb[TCA_EM_IPT_NFPROTO]); |
| 182 |
|
| 183 |
xtables_init_all(&em_tc_ipt_globals, nfproto); |
| 184 |
|
| 185 |
mname = rta_getattr_str(tb[TCA_EM_IPT_MATCH_NAME]); |
| 186 |
match = xtables_find_match(mname, XTF_TRY_LOAD, NULL); |
| 187 |
if (!match) |
| 188 |
return -1; |
| 189 |
|
| 190 |
match->m = fake_xt_entry_match(RTA_PAYLOAD(tb[TCA_EM_IPT_MATCH_DATA]), |
| 191 |
RTA_DATA(tb[TCA_EM_IPT_MATCH_DATA])); |
| 192 |
if (!match->m) |
| 193 |
return -1; |
| 194 |
|
| 195 |
match->print(NULL, match->m, 0); |
| 196 |
|
| 197 |
scrub_match(match); |
| 198 |
return 0; |
| 199 |
} |
| 200 |
|
| 201 |
struct ematch_util ipt_ematch_util = { |
| 202 |
.kind = "ipt", |
| 203 |
.kind_num = TCF_EM_IPT, |
| 204 |
.parse_eopt_argv = em_ipt_parse_eopt_argv, |
| 205 |
.print_eopt = em_ipt_print_epot, |
| 206 |
.print_usage = em_ipt_print_usage |
| 207 |
}; |