From patchwork Sat Dec 28 00:01:14 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pablo Neira Ayuso X-Patchwork-Id: 305504 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 13F112C00B0 for ; Sat, 28 Dec 2013 11:01:26 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754939Ab3L1ABZ (ORCPT ); Fri, 27 Dec 2013 19:01:25 -0500 Received: from mail.us.es ([193.147.175.20]:51026 "EHLO mail.us.es" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754925Ab3L1ABY (ORCPT ); Fri, 27 Dec 2013 19:01:24 -0500 Received: (qmail 7148 invoked from network); 28 Dec 2013 01:01:23 +0100 Received: from unknown (HELO us.es) (192.168.2.12) by us.es with SMTP; 28 Dec 2013 01:01:23 +0100 Received: (qmail 8502 invoked by uid 507); 28 Dec 2013 00:01:23 -0000 X-Qmail-Scanner-Diagnostics: from 127.0.0.1 by antivirus2 (envelope-from , uid 501) with qmail-scanner-2.10 (clamdscan: 0.98/18294. spamassassin: 3.3.2. Clear:RC:1(127.0.0.1):SA:0(-99.8/7.5):. Processed in 1.735132 secs); 28 Dec 2013 00:01:23 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on antivirus2 X-Spam-Level: X-Spam-Status: No, score=-99.8 required=7.5 tests=BAYES_50,RCVD_IN_PBL, RCVD_IN_RP_RNBL, RCVD_IN_SORBS_DUL, RDNS_DYNAMIC, SMTPAUTH_US, USER_IN_WHITELIST autolearn=disabled version=3.3.2 X-Spam-ASN: AS12715 95.20.0.0/16 X-Envelope-From: pablo@netfilter.org Received: from unknown (HELO antivirus2) (127.0.0.1) by us.es with SMTP; 28 Dec 2013 00:01:21 -0000 Received: from 192.168.1.13 (192.168.1.13) by antivirus2 (F-Secure/fsigk_smtp/412/antivirus2); Sat, 28 Dec 2013 01:01:21 +0100 (CET) X-Virus-Status: clean(F-Secure/fsigk_smtp/412/antivirus2) Received: (qmail 18963 invoked from network); 28 Dec 2013 01:01:21 +0100 Received: from 108.60.20.95.dynamic.jazztel.es (HELO localhost.localdomain) (pneira@us.es@95.20.60.108) by mail.us.es with SMTP; 28 Dec 2013 01:01:21 +0100 From: Pablo Neira Ayuso To: netfilter-devel@vger.kernel.org Subject: [PATCH 2/2 nft] netlink: fix dictionary feature Date: Sat, 28 Dec 2013 01:01:14 +0100 Message-Id: <1388188874-7667-2-git-send-email-pablo@netfilter.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1388188874-7667-1-git-send-email-pablo@netfilter.org> References: <1388188874-7667-1-git-send-email-pablo@netfilter.org> Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org This patch fixes dictionary feature, that allows you to conditionally set packet fields based on a given selector, eg. add rule ip filter input meta mark set tcp dport map { 22 => 1, 23 => 2, } This means that traffic flowing to tcp port 22 receives the packet mark 1 and tcp port 23 receives mark 2. This feature was partially broken by aae836a ("src: use libnftables") although it also needs the kernel fix ("netfilter: nf_tables: fix wrong datatype in nft_validate_data_load()"). This patch also fixes endianness issues when displaying the mark via `list table' related to list_setelem_cb() since the byteorder was left unset for the data part of a set element. meta mark set tcp dport map { telnet => 0x02000000, ssh => 0x01000000} ^ ^ Note the wrong endianness in the example above. Signed-off-by: Pablo Neira Ayuso --- src/netlink.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/netlink.c b/src/netlink.c index cab8cf4..59bd8e4 100644 --- a/src/netlink.c +++ b/src/netlink.c @@ -157,13 +157,22 @@ static struct nft_set_elem *alloc_nft_setelem(const struct expr *expr) nft_set_elem_attr_set(nlse, NFT_SET_ELEM_ATTR_KEY, &nld.value, nld.len); netlink_gen_data(expr->right, &nld); - if (expr->right->ops->type == EXPR_VERDICT) { + switch (expr->right->ops->type) { + case EXPR_VERDICT: nft_set_elem_attr_set_u32(nlse, NFT_SET_ELEM_ATTR_VERDICT, expr->right->verdict); if (expr->chain != NULL) { nft_set_elem_attr_set(nlse, NFT_SET_ELEM_ATTR_CHAIN, nld.chain, strlen(nld.chain)); } + break; + case EXPR_VALUE: + nft_set_elem_attr_set(nlse, NFT_SET_ELEM_ATTR_DATA, + nld.value, nld.len); + break; + default: + BUG("unexpected set element expression\n"); + break; } } @@ -994,6 +1003,9 @@ static int list_setelem_cb(struct nft_set_elem *nlse, void *arg) set->datatype->type == TYPE_VERDICT ? NFT_REG_VERDICT : NFT_REG_1); data->dtype = set->datatype; + data->byteorder = set->datatype->byteorder; + if (data->byteorder == BYTEORDER_HOST_ENDIAN) + mpz_switch_byteorder(data->value, data->len / BITS_PER_BYTE); expr = mapping_expr_alloc(&internal_location, expr, data); }