From patchwork Sun Mar 6 22:24:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: nevola X-Patchwork-Id: 592597 X-Patchwork-Delegate: pablo@netfilter.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 22E211401DE for ; Mon, 7 Mar 2016 09:24:49 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=bEpPJiW9; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751358AbcCFWYt (ORCPT ); Sun, 6 Mar 2016 17:24:49 -0500 Received: from mail-wm0-f42.google.com ([74.125.82.42]:38766 "EHLO mail-wm0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750848AbcCFWYs (ORCPT ); Sun, 6 Mar 2016 17:24:48 -0500 Received: by mail-wm0-f42.google.com with SMTP id l68so50671229wml.1 for ; Sun, 06 Mar 2016 14:24:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=I0FDnXw5QjqRhzyaeUmRDRiWL8itEi0Bmk8SZ0F5LFg=; b=bEpPJiW9aDKyPOWiYL5fUMmT7QfM7GtI5Sv03haBOLZcnNrPZ3loKpSAJ/r3zhtwhz OkIAZ4qyNz9HBaaXocX02HorsObsUquIekr0A1gMAHRAKYRDwrtkr+JkVp+IdpBd7/me 3UuekIfpSYmFXmf7EQaYJOOrTZ1B0Q08sARkm1VyN38EYN5hjpoxiVLypJX/qhczgKMW 4Frt9kL/ZnLgHs/hJz1S6JkhPzUjZXdtGF0Wv2vQm23fGt/Feo6H8loMVFDE3czTAiss uxr63ox3LC7iKh5wFiMye3CfjuiIlSfSiyQW7xnNpnAoQ7uselineBNNaTp3orUq9FYl 9cww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=I0FDnXw5QjqRhzyaeUmRDRiWL8itEi0Bmk8SZ0F5LFg=; b=EZhHmrGOePHpoIGPUU6jtb+sfRj2BKRGc/4BD9XqJwzNrTmDAdP0jvuPuTGipdUkpA fK82377zGOEHP9NdCXUynjt1dhVgt4sPP1RKclaMRhcqlqDqKdpXnxzscVK797v3XiHr yKfteUn95TW+64oIg/zhPhYnTa/eywlmtiatF0h+kk7NcllxVinjGva3aqHw651XCjNQ aA95OBgypvtxmHgkBjIFtOG5syMn0Wfbbuu6dGfvRV1g3+WQ5GEYcnHmfl+ZGbXPbBts cTnZBiDfGrE50jC39JHRZSTbmhjzDVoFU4TbbReVtIQii2df9LW9SB68OIYJguWI3le0 UwYg== X-Gm-Message-State: AD7BkJI9PUMrut6LlOynfGZtgOELxgp8rucTKv34792VSR5NrNKE5jFJcoLhDDAGwUvKKQ== X-Received: by 10.28.177.134 with SMTP id a128mr10261626wmf.55.1457303087113; Sun, 06 Mar 2016 14:24:47 -0800 (PST) Received: from sonyv (151.Red-79-150-198.dynamicIP.rima-tde.net. [79.150.198.151]) by smtp.gmail.com with ESMTPSA id e25sm2621301wmi.21.2016.03.06.14.24.46 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Sun, 06 Mar 2016 14:24:46 -0800 (PST) Date: Sun, 6 Mar 2016 23:24:44 +0100 From: Laura Garcia Liebana To: netfilter-devel@vger.kernel.org Cc: shivanib134@gmail.com, pablo@netfilter.org, outreachy-kernel@googlegroups.com Subject: [PATCHv2] extensions: libipt_icmp: Add translation to nft Message-ID: <20160306222442.GA20945@sonyv> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org Add translation for icmp to nftables. Not supported types in nftables are: any, network-unreachable, host-unreachable, protocol-unreachable, port-unreachable, fragmentation-needed, source-route-failed, network-unknown, host-unknown, network-prohibited, host-prohibited, TOS-network-unreachable, TOS-host-unreachable, communication-prohibited, host-precedence-violation, precedence-cutoff, network-redirect, host-redirect, TOS-network-redirect, TOS-host-redirect, router-advertisement, router-solicitation, ttl-zero-during-transit, ttl-zero-during-reassembly, ip-header-bad and required-option-missing. Examples: $ sudo iptables-translate -t filter -A INPUT -m icmp --icmp-type echo-reply -j LOG nft add rule ip filter INPUT icmp type echo-reply counter log level warn $ sudo iptables-translate -t filter -A INPUT -m icmp --icmp-type 3 -j LOG nft add rule ip filter INPUT icmp type destination-unreachable counter log level warn $ sudo iptables-translate -t filter -A INPUT -m icmp ! --icmp-type 3 -j LOG nft add rule ip filter INPUT icmp type != destination-unreachable counter log level warn Signed-off-by: Laura Garcia Liebana --- v2: - Detection of not supported types in nftables, as Shivani suggested. extensions/libipt_icmp.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/extensions/libipt_icmp.c b/extensions/libipt_icmp.c index 666e7da..89eb36e 100644 --- a/extensions/libipt_icmp.c +++ b/extensions/libipt_icmp.c @@ -218,7 +218,7 @@ static void print_icmptype(uint8_t type, } static void icmp_print(const void *ip, const struct xt_entry_match *match, - int numeric) + int numeric) { const struct ipt_icmp *icmp = (struct ipt_icmp *)match->data; @@ -249,6 +249,49 @@ static void icmp_save(const void *ip, const struct xt_entry_match *match) } } +static unsigned int type_xlate_print(struct xt_xlate *xl, unsigned int icmptype, + unsigned int code_min, unsigned int code_max) +{ + unsigned int i; + + if (code_min == code_max) + return 0; + + switch (icmptype) { + case 0xFF: + case 9: + case 10: + return 0; + default: + for (i = 0; ARRAY_SIZE(icmp_codes); ++i) + if (icmp_codes[i].type == icmptype && + icmp_codes[i].code_min == code_min && + icmp_codes[i].code_max == code_max) + break; + + xt_xlate_add(xl, icmp_codes[i].name); + } + + return 1; +} + +static int icmp_xlate(const struct xt_entry_match *match, struct xt_xlate *xl, + int numeric) +{ + const struct ipt_icmp *info = (struct ipt_icmp *)match->data; + + xt_xlate_add(xl, "icmp type%s ", + (info->invflags & IPT_ICMP_INV) ? " !=" : ""); + + if (!type_xlate_print(xl, info->type, info->code[0], info->code[1])) + return 0; + + xt_xlate_add(xl, " "); + + return 1; +} + + static struct xtables_match icmp_mt_reg = { .name = "icmp", .version = XTABLES_VERSION, @@ -261,6 +304,7 @@ static struct xtables_match icmp_mt_reg = { .save = icmp_save, .x6_parse = icmp_parse, .x6_options = icmp_opts, + .xlate = icmp_xlate, }; void _init(void)