| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2013-2015 Pablo Neira Ayuso <pablo@netfilter.org> | ||
| 3 | * Copyright (c) 2015 Arturo Borrero Gonzalez <arturo@debian.org> | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License version 2 (or any | ||
| 7 | * later) as published by the Free Software Foundation. | ||
| 8 | */ | ||
| 9 | |||
| 10 | #include <nft.h> | ||
| 11 | |||
| 12 | #include <time.h> | ||
| 13 | #include <net/if.h> | ||
| 14 | #include <getopt.h> | ||
| 15 | #include <ctype.h> /* for isspace */ | ||
| 16 | #include <statement.h> | ||
| 17 | #include <netlink.h> | ||
| 18 | #include <xt.h> | ||
| 19 | #include <erec.h> | ||
| 20 | |||
| 21 | #include <libmnl/libmnl.h> | ||
| 22 | #include <linux/netfilter/nfnetlink.h> | ||
| 23 | #include <linux/netfilter/nf_tables_compat.h> | ||
| 24 | #include <linux/netfilter_ipv4/ip_tables.h> | ||
| 25 | #include <linux/netfilter_ipv6/ip6_tables.h> | ||
| 26 | #include <linux/netfilter_arp/arp_tables.h> | ||
| 27 | #include <linux/netfilter_bridge/ebtables.h> | ||
| 28 | |||
| 29 | #ifdef HAVE_LIBXTABLES | ||
| 30 | #include <xtables.h> | ||
| 31 | |||
| 32 | static void *xt_entry_alloc(const struct xt_stmt *xt, uint32_t af); | ||
| 33 | #endif | ||
| 34 | |||
| 35 | ✗ | void xt_stmt_xlate(const struct stmt *stmt, struct output_ctx *octx) | |
| 36 | { | ||
| 37 | static const char *typename[NFT_XT_MAX] = { | ||
| 38 | [NFT_XT_MATCH] = "match", | ||
| 39 | [NFT_XT_TARGET] = "target", | ||
| 40 | [NFT_XT_WATCHER] = "watcher", | ||
| 41 | }; | ||
| 42 | ✗ | int rc = 0; | |
| 43 | #ifdef HAVE_LIBXTABLES | ||
| 44 | ✗ | struct xt_xlate *xl = xt_xlate_alloc(10240); | |
| 45 | struct xtables_target *tg; | ||
| 46 | struct xt_entry_target *t; | ||
| 47 | struct xtables_match *mt; | ||
| 48 | struct xt_entry_match *m; | ||
| 49 | size_t size; | ||
| 50 | void *entry; | ||
| 51 | |||
| 52 | ✗ | xtables_set_nfproto(stmt->xt.family); | |
| 53 | ✗ | entry = xt_entry_alloc(&stmt->xt, stmt->xt.family); | |
| 54 | |||
| 55 | ✗ | switch (stmt->xt.type) { | |
| 56 | ✗ | case NFT_XT_MATCH: | |
| 57 | ✗ | mt = xtables_find_match(stmt->xt.name, XTF_TRY_LOAD, NULL); | |
| 58 | ✗ | if (!mt) { | |
| 59 | ✗ | fprintf(octx->error_fp, | |
| 60 | "# Warning: XT match %s not found\n", | ||
| 61 | ✗ | stmt->xt.name); | |
| 62 | ✗ | break; | |
| 63 | } | ||
| 64 | ✗ | size = XT_ALIGN(sizeof(*m)) + stmt->xt.infolen; | |
| 65 | |||
| 66 | ✗ | m = xzalloc(size); | |
| 67 | ✗ | memcpy(&m->data, stmt->xt.info, stmt->xt.infolen); | |
| 68 | |||
| 69 | ✗ | m->u.match_size = size; | |
| 70 | ✗ | m->u.user.revision = stmt->xt.rev; | |
| 71 | |||
| 72 | ✗ | if (mt->xlate) { | |
| 73 | ✗ | struct xt_xlate_mt_params params = { | |
| 74 | .ip = entry, | ||
| 75 | .match = m, | ||
| 76 | .numeric = 1, | ||
| 77 | }; | ||
| 78 | |||
| 79 | ✗ | rc = mt->xlate(xl, ¶ms); | |
| 80 | } | ||
| 81 | ✗ | free(m); | |
| 82 | ✗ | break; | |
| 83 | ✗ | case NFT_XT_WATCHER: | |
| 84 | case NFT_XT_TARGET: | ||
| 85 | ✗ | tg = xtables_find_target(stmt->xt.name, XTF_TRY_LOAD); | |
| 86 | ✗ | if (!tg) { | |
| 87 | ✗ | fprintf(octx->error_fp, | |
| 88 | "# Warning: XT target %s not found\n", | ||
| 89 | ✗ | stmt->xt.name); | |
| 90 | ✗ | break; | |
| 91 | } | ||
| 92 | ✗ | size = XT_ALIGN(sizeof(*t)) + stmt->xt.infolen; | |
| 93 | |||
| 94 | ✗ | t = xzalloc(size); | |
| 95 | ✗ | memcpy(&t->data, stmt->xt.info, stmt->xt.infolen); | |
| 96 | |||
| 97 | ✗ | t->u.target_size = size; | |
| 98 | ✗ | t->u.user.revision = stmt->xt.rev; | |
| 99 | |||
| 100 | ✗ | strcpy(t->u.user.name, tg->name); | |
| 101 | |||
| 102 | ✗ | if (tg->xlate) { | |
| 103 | ✗ | struct xt_xlate_tg_params params = { | |
| 104 | .ip = entry, | ||
| 105 | .target = t, | ||
| 106 | .numeric = 1, | ||
| 107 | }; | ||
| 108 | |||
| 109 | ✗ | rc = tg->xlate(xl, ¶ms); | |
| 110 | } | ||
| 111 | ✗ | free(t); | |
| 112 | ✗ | break; | |
| 113 | } | ||
| 114 | |||
| 115 | ✗ | if (rc == 1) | |
| 116 | ✗ | nft_print(octx, "%s", xt_xlate_get(xl)); | |
| 117 | ✗ | xt_xlate_free(xl); | |
| 118 | ✗ | free(entry); | |
| 119 | #endif | ||
| 120 | ✗ | if (!rc) | |
| 121 | ✗ | nft_print(octx, "xt %s \"%s\"", | |
| 122 | ✗ | typename[stmt->xt.type], stmt->xt.name); | |
| 123 | ✗ | } | |
| 124 | |||
| 125 | ✗ | void xt_stmt_destroy(struct stmt *stmt) | |
| 126 | { | ||
| 127 | ✗ | free_const(stmt->xt.name); | |
| 128 | ✗ | free(stmt->xt.info); | |
| 129 | ✗ | } | |
| 130 | |||
| 131 | #ifdef HAVE_LIBXTABLES | ||
| 132 | ✗ | static void *xt_entry_alloc(const struct xt_stmt *xt, uint32_t af) | |
| 133 | { | ||
| 134 | union nft_entry { | ||
| 135 | struct ipt_entry ipt; | ||
| 136 | struct ip6t_entry ip6t; | ||
| 137 | struct arpt_entry arpt; | ||
| 138 | struct ebt_entry ebt; | ||
| 139 | } *entry; | ||
| 140 | |||
| 141 | ✗ | entry = xmalloc(sizeof(union nft_entry)); | |
| 142 | |||
| 143 | ✗ | switch (af) { | |
| 144 | ✗ | case NFPROTO_IPV4: | |
| 145 | ✗ | entry->ipt.ip.proto = xt->proto; | |
| 146 | ✗ | break; | |
| 147 | ✗ | case NFPROTO_IPV6: | |
| 148 | ✗ | entry->ip6t.ipv6.proto = xt->proto; | |
| 149 | ✗ | break; | |
| 150 | ✗ | case NFPROTO_BRIDGE: | |
| 151 | ✗ | entry->ebt.ethproto = xt->proto; | |
| 152 | ✗ | break; | |
| 153 | ✗ | case NFPROTO_ARP: | |
| 154 | ✗ | entry->arpt.arp.arhln_mask = 0xff; | |
| 155 | ✗ | entry->arpt.arp.arhln = 6; | |
| 156 | ✗ | break; | |
| 157 | ✗ | default: | |
| 158 | ✗ | break; | |
| 159 | } | ||
| 160 | |||
| 161 | ✗ | return entry; | |
| 162 | } | ||
| 163 | |||
| 164 | ✗ | static uint32_t xt_proto(const struct proto_ctx *pctx) | |
| 165 | { | ||
| 166 | ✗ | const struct proto_desc *desc = NULL; | |
| 167 | |||
| 168 | ✗ | if (pctx->family == NFPROTO_BRIDGE) { | |
| 169 | ✗ | desc = pctx->protocol[PROTO_BASE_NETWORK_HDR].desc; | |
| 170 | ✗ | if (desc == NULL) | |
| 171 | ✗ | return 0; | |
| 172 | ✗ | if (strcmp(desc->name, "ip") == 0) | |
| 173 | ✗ | return __constant_htons(ETH_P_IP); | |
| 174 | ✗ | if (strcmp(desc->name, "ip6") == 0) | |
| 175 | ✗ | return __constant_htons(ETH_P_IPV6); | |
| 176 | ✗ | return 0; | |
| 177 | } | ||
| 178 | |||
| 179 | ✗ | desc = pctx->protocol[PROTO_BASE_TRANSPORT_HDR].desc; | |
| 180 | ✗ | if (desc == NULL) | |
| 181 | ✗ | return 0; | |
| 182 | ✗ | if (strcmp(desc->name, "tcp") == 0) | |
| 183 | ✗ | return IPPROTO_TCP; | |
| 184 | ✗ | else if (strcmp(desc->name, "udp") == 0) | |
| 185 | ✗ | return IPPROTO_UDP; | |
| 186 | ✗ | else if (strcmp(desc->name, "udplite") == 0) | |
| 187 | ✗ | return IPPROTO_UDPLITE; | |
| 188 | ✗ | else if (strcmp(desc->name, "sctp") == 0) | |
| 189 | ✗ | return IPPROTO_SCTP; | |
| 190 | ✗ | else if (strcmp(desc->name, "dccp") == 0) | |
| 191 | ✗ | return IPPROTO_DCCP; | |
| 192 | ✗ | else if (strcmp(desc->name, "esp") == 0) | |
| 193 | ✗ | return IPPROTO_ESP; | |
| 194 | ✗ | else if (strcmp(desc->name, "ah") == 0) | |
| 195 | ✗ | return IPPROTO_AH; | |
| 196 | |||
| 197 | ✗ | return 0; | |
| 198 | } | ||
| 199 | #endif | ||
| 200 | |||
| 201 | /* | ||
| 202 | * Delinearization | ||
| 203 | */ | ||
| 204 | |||
| 205 | ✗ | void netlink_parse_match(struct netlink_parse_ctx *ctx, | |
| 206 | const struct location *loc, | ||
| 207 | const struct nftnl_expr *nle) | ||
| 208 | { | ||
| 209 | const char *mtinfo; | ||
| 210 | struct stmt *stmt; | ||
| 211 | uint32_t mt_len; | ||
| 212 | |||
| 213 | ✗ | mtinfo = nftnl_expr_get(nle, NFTNL_EXPR_MT_INFO, &mt_len); | |
| 214 | |||
| 215 | ✗ | stmt = xt_stmt_alloc(loc); | |
| 216 | ✗ | stmt->xt.name = strdup(nftnl_expr_get_str(nle, NFTNL_EXPR_MT_NAME)); | |
| 217 | ✗ | stmt->xt.type = NFT_XT_MATCH; | |
| 218 | ✗ | stmt->xt.rev = nftnl_expr_get_u32(nle, NFTNL_EXPR_MT_REV); | |
| 219 | ✗ | stmt->xt.family = ctx->table->handle.family; | |
| 220 | |||
| 221 | ✗ | stmt->xt.infolen = mt_len; | |
| 222 | ✗ | stmt->xt.info = xmalloc(mt_len); | |
| 223 | ✗ | memcpy(stmt->xt.info, mtinfo, mt_len); | |
| 224 | |||
| 225 | ✗ | ctx->table->has_xt_stmts = true; | |
| 226 | ✗ | rule_stmt_append(ctx->rule, stmt); | |
| 227 | ✗ | } | |
| 228 | |||
| 229 | ✗ | void netlink_parse_target(struct netlink_parse_ctx *ctx, | |
| 230 | const struct location *loc, | ||
| 231 | const struct nftnl_expr *nle) | ||
| 232 | { | ||
| 233 | const void *tginfo; | ||
| 234 | struct stmt *stmt; | ||
| 235 | uint32_t tg_len; | ||
| 236 | |||
| 237 | ✗ | tginfo = nftnl_expr_get(nle, NFTNL_EXPR_TG_INFO, &tg_len); | |
| 238 | |||
| 239 | ✗ | stmt = xt_stmt_alloc(loc); | |
| 240 | ✗ | stmt->xt.name = strdup(nftnl_expr_get_str(nle, NFTNL_EXPR_TG_NAME)); | |
| 241 | ✗ | stmt->xt.type = NFT_XT_TARGET; | |
| 242 | ✗ | stmt->xt.rev = nftnl_expr_get_u32(nle, NFTNL_EXPR_TG_REV); | |
| 243 | ✗ | stmt->xt.family = ctx->table->handle.family; | |
| 244 | |||
| 245 | ✗ | stmt->xt.infolen = tg_len; | |
| 246 | ✗ | stmt->xt.info = xmalloc(tg_len); | |
| 247 | ✗ | memcpy(stmt->xt.info, tginfo, tg_len); | |
| 248 | |||
| 249 | ✗ | ctx->table->has_xt_stmts = true; | |
| 250 | ✗ | rule_stmt_append(ctx->rule, stmt); | |
| 251 | ✗ | } | |
| 252 | |||
| 253 | #ifdef HAVE_LIBXTABLES | ||
| 254 | ✗ | static bool is_watcher(uint32_t family, struct stmt *stmt) | |
| 255 | { | ||
| 256 | ✗ | if (family != NFPROTO_BRIDGE || | |
| 257 | ✗ | stmt->xt.type != NFT_XT_TARGET) | |
| 258 | ✗ | return false; | |
| 259 | |||
| 260 | /* this has to be hardcoded :-( */ | ||
| 261 | ✗ | if (strcmp(stmt->xt.name, "log") == 0) | |
| 262 | ✗ | return true; | |
| 263 | ✗ | else if (strcmp(stmt->xt.name, "nflog") == 0) | |
| 264 | ✗ | return true; | |
| 265 | |||
| 266 | ✗ | return false; | |
| 267 | } | ||
| 268 | |||
| 269 | ✗ | void stmt_xt_postprocess(struct rule_pp_ctx *rctx, struct stmt *stmt, | |
| 270 | struct rule *rule) | ||
| 271 | { | ||
| 272 | ✗ | struct dl_proto_ctx *dl = dl_proto_ctx(rctx); | |
| 273 | |||
| 274 | ✗ | if (is_watcher(dl->pctx.family, stmt)) | |
| 275 | ✗ | stmt->xt.type = NFT_XT_WATCHER; | |
| 276 | |||
| 277 | ✗ | stmt->xt.proto = xt_proto(&dl->pctx); | |
| 278 | ✗ | } | |
| 279 | |||
| 280 | ✗ | static int nft_xt_compatible_revision(const char *name, uint8_t rev, int opt) | |
| 281 | ✗ | { | |
| 282 | struct mnl_socket *nl; | ||
| 283 | ✗ | char buf[MNL_SOCKET_BUFFER_SIZE]; | |
| 284 | struct nlmsghdr *nlh; | ||
| 285 | uint32_t portid, seq, type, family; | ||
| 286 | struct nfgenmsg *nfg; | ||
| 287 | ✗ | int ret = 0; | |
| 288 | |||
| 289 | ✗ | switch (opt) { | |
| 290 | ✗ | case IPT_SO_GET_REVISION_MATCH: | |
| 291 | ✗ | family = NFPROTO_IPV4; | |
| 292 | ✗ | type = 0; | |
| 293 | ✗ | break; | |
| 294 | ✗ | case IPT_SO_GET_REVISION_TARGET: | |
| 295 | ✗ | family = NFPROTO_IPV4; | |
| 296 | ✗ | type = 1; | |
| 297 | ✗ | break; | |
| 298 | ✗ | case IP6T_SO_GET_REVISION_MATCH: | |
| 299 | ✗ | family = NFPROTO_IPV6; | |
| 300 | ✗ | type = 0; | |
| 301 | ✗ | break; | |
| 302 | ✗ | case IP6T_SO_GET_REVISION_TARGET: | |
| 303 | ✗ | family = NFPROTO_IPV6; | |
| 304 | ✗ | type = 1; | |
| 305 | ✗ | break; | |
| 306 | ✗ | default: /* No revision support, assume ok */ | |
| 307 | ✗ | return 1; | |
| 308 | } | ||
| 309 | |||
| 310 | ✗ | nlh = mnl_nlmsg_put_header(buf); | |
| 311 | ✗ | nlh->nlmsg_type = (NFNL_SUBSYS_NFT_COMPAT << 8) | NFNL_MSG_COMPAT_GET; | |
| 312 | ✗ | nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; | |
| 313 | ✗ | nlh->nlmsg_seq = seq = time(NULL); | |
| 314 | |||
| 315 | ✗ | nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg)); | |
| 316 | ✗ | nfg->nfgen_family = family; | |
| 317 | ✗ | nfg->version = NFNETLINK_V0; | |
| 318 | ✗ | nfg->res_id = 0; | |
| 319 | |||
| 320 | ✗ | mnl_attr_put_strz(nlh, NFTA_COMPAT_NAME, name); | |
| 321 | ✗ | mnl_attr_put_u32(nlh, NFTA_COMPAT_REV, htonl(rev)); | |
| 322 | ✗ | mnl_attr_put_u32(nlh, NFTA_COMPAT_TYPE, htonl(type)); | |
| 323 | |||
| 324 | ✗ | nl = mnl_socket_open(NETLINK_NETFILTER); | |
| 325 | ✗ | if (nl == NULL) | |
| 326 | ✗ | return 0; | |
| 327 | |||
| 328 | ✗ | if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) | |
| 329 | ✗ | goto err; | |
| 330 | |||
| 331 | ✗ | portid = mnl_socket_get_portid(nl); | |
| 332 | |||
| 333 | ✗ | if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) | |
| 334 | ✗ | goto err; | |
| 335 | |||
| 336 | ✗ | ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); | |
| 337 | ✗ | if (ret == -1) | |
| 338 | ✗ | goto err; | |
| 339 | |||
| 340 | ✗ | ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL); | |
| 341 | ✗ | if (ret == -1) | |
| 342 | ✗ | goto err; | |
| 343 | |||
| 344 | ✗ | err: | |
| 345 | ✗ | mnl_socket_close(nl); | |
| 346 | |||
| 347 | ✗ | return ret < 0 ? 0 : 1; | |
| 348 | } | ||
| 349 | |||
| 350 | static struct option original_opts[] = { | ||
| 351 | { }, | ||
| 352 | }; | ||
| 353 | |||
| 354 | static struct xtables_globals xt_nft_globals = { | ||
| 355 | .program_name = "nft", | ||
| 356 | .program_version = PACKAGE_VERSION, | ||
| 357 | .orig_opts = original_opts, | ||
| 358 | .compat_rev = nft_xt_compatible_revision, | ||
| 359 | }; | ||
| 360 | |||
| 361 | 13027 | void xt_init(void) | |
| 362 | { | ||
| 363 | static bool init_once; | ||
| 364 | |||
| 365 |
1/2✓ Branch 0 (2→3) taken 13027 times.
✗ Branch 1 (2→4) not taken.
|
13027 | if (!init_once) { |
| 366 | /* libxtables is full of global variables and cannot be used | ||
| 367 | * concurrently by multiple threads. Hence, it's fine that the | ||
| 368 | * "init_once" guard is not thread-safe either. | ||
| 369 | * Don't link against xtables if you want thread safety. | ||
| 370 | */ | ||
| 371 | 13027 | init_once = true; | |
| 372 | |||
| 373 | /* Default to IPv4, but this changes in runtime */ | ||
| 374 | 13027 | xtables_init_all(&xt_nft_globals, NFPROTO_IPV4); | |
| 375 | } | ||
| 376 | 13027 | } | |
| 377 | #endif | ||
| 378 |