From patchwork Sat Jul 7 19:17:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harsha Sharma X-Patchwork-Id: 940859 X-Patchwork-Delegate: pablo@netfilter.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netfilter-devel-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="NhMLGUhw"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 41NLvP54Ztz9s00 for ; Sun, 8 Jul 2018 05:17:21 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754151AbeGGTRV (ORCPT ); Sat, 7 Jul 2018 15:17:21 -0400 Received: from mail-wm0-f54.google.com ([74.125.82.54]:52321 "EHLO mail-wm0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754052AbeGGTRU (ORCPT ); Sat, 7 Jul 2018 15:17:20 -0400 Received: by mail-wm0-f54.google.com with SMTP id w16-v6so17495703wmc.2 for ; Sat, 07 Jul 2018 12:17:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=WhbmulCEPKKJW9QsN2YksD/dSnAEC+dGhBEjpmSHzig=; b=NhMLGUhwKTqk0rB78yPVqksVy5qbHNqrWvNi1jLOyToRDG7SESFEEcUZE8hzEAzw8b aTXmAyctzdgtrs4Lc2WrVxfr+w+SnP0NkqoC/UOwwTUi589bBJpOFAvIvoa9jlyblhxJ 0T4+iNdraEiwRW/WNuMLGkYDzEKwJGsM6oJXupdcygHXJYLZMMPMyQqgMHLQavh8j5my /sPLdxj5FMtQLG47YscK4NDJCbj8R77iGVsju/ve/etqJ0ic6klf5emdLu/ukIh2sc8m VsyhRt1e6g/lsBnGei/aFK6tvCLx9jymAucpyRg13U1b0bKlaX4+qOZQ40EcXdKBDzA3 5zLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=WhbmulCEPKKJW9QsN2YksD/dSnAEC+dGhBEjpmSHzig=; b=N9+sh84i9aWqwyhSGhvRgBp406S+JEJiXKKLdiA5FYepyOamJYctjbj3FMZDTGVU47 lRo1J/9/vQ/mhyMZBu8QIa7aMlhlW/oxxT6ZY68Jk0kibYSyHyGFbb3U7Awy5QIqiKKu qjCXNaVN3DUDcFVT2wpqvWluHTyBNbS5IMEOqiMzDLeH3n+LA11dfkCT2pnVRLg8b/1T PAxnwFrnfV541bfvnRG/W/n4xklZn6nZ2TdZO97VIRA7sVJo7fy2bPDPUGbs+nSWqkD3 uiQZUXkmQDkhklEL6izqr1HRURrI/gcSlrieLxKFgSpm7i1li4tU2BeQNMx44Xhllxy0 0D7Q== X-Gm-Message-State: APt69E2JHSddMPTT7fCifM3OWe1B0p+hsPxUrEi/a3ATYgSX3vBm2ash fbEKHVNt/7XKiRYPCL/Hwp8= X-Google-Smtp-Source: AAOMgpfahPrZ2Fd14+ZolRA3h5e3nM9OV9p+fQLK1JzJ7upPZxHoY5NJSe8WREzqS4vEWpCF28WDMw== X-Received: by 2002:a1c:b609:: with SMTP id g9-v6mr8380181wmf.73.1530991039181; Sat, 07 Jul 2018 12:17:19 -0700 (PDT) Received: from XDDDDD.fritz.box ([2001:16b8:2d18:d000:3560:559d:d220:e342]) by smtp.gmail.com with ESMTPSA id j12-v6sm5109187wrn.29.2018.07.07.12.17.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 07 Jul 2018 12:17:18 -0700 (PDT) From: Harsha Sharma To: harshasharmaiitr@gmail.com, pablo@netfilter.org Cc: netfilter-devel@vger.kernel.org Subject: [PATCH libnftnl v3 3/3] examples: Add test for assigning timeout objects via rule Date: Sat, 7 Jul 2018 21:17:11 +0200 Message-Id: <20180707191711.25401-1-harshasharmaiitr@gmail.com> X-Mailer: git-send-email 2.14.1 Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org Usage: ./nft-rule-ct-timeout-add ip filter input some-name ./nft-rule-get ip filter ip filter input 6 [ objref type 5 name some-name ] nft list ruleset ... chain input { ct timeout set "some-name" } Signed-off-by: Harsha Sharma --- Changes in v3: - No changes Changes in v2: - Add this in example examples/Makefile.am | 6 +- examples/nft-rule-ct-timeout-add.c | 154 +++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 examples/nft-rule-ct-timeout-add.c diff --git a/examples/Makefile.am b/examples/Makefile.am index d5584e5..67f0156 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -37,7 +37,8 @@ check_PROGRAMS = nft-table-add \ nft-ct-helper-add \ nft-ct-helper-get \ nft-ct-helper-del \ - nft-rule-ct-helper-add + nft-rule-ct-helper-add \ + nft-rule-ct-timeout-add nft_table_add_SOURCES = nft-table-add.c nft_table_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} @@ -152,3 +153,6 @@ nft_ct_helper_del_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} nft_rule_ct_helper_add_SOURCES = nft-rule-ct-helper-add.c nft_rule_ct_helper_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} + +nft_rule_ct_timeout_add_SOURCES = nft-rule-ct-timeout-add.c +nft_rule_ct_timeout_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} diff --git a/examples/nft-rule-ct-timeout-add.c b/examples/nft-rule-ct-timeout-add.c new file mode 100644 index 0000000..159b2f0 --- /dev/null +++ b/examples/nft-rule-ct-timeout-add.c @@ -0,0 +1,154 @@ +/* + * (C) 2012 by Pablo Neira Ayuso + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This software has been sponsored by Sophos Astaro + */ + +#include +#include +#include +#include /* for offsetof */ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +static void add_ct_timeout(struct nftnl_rule *r, const char *obj_name) +{ + struct nftnl_expr *e; + + e = nftnl_expr_alloc("objref"); + if (e == NULL) { + perror("expr objref oom"); + exit(EXIT_FAILURE); + } + nftnl_expr_set_str(e, NFTNL_EXPR_OBJREF_IMM_NAME, obj_name); + nftnl_expr_set_u32(e, NFTNL_EXPR_OBJREF_IMM_TYPE, 6); + + nftnl_rule_add_expr(r, e); +} + +static struct nftnl_rule *setup_rule(uint8_t family, const char *table, + const char *chain, const char *handle, const char *obj_name) +{ + struct nftnl_rule *r = NULL; + uint64_t handle_num; + + r = nftnl_rule_alloc(); + if (r == NULL) { + perror("OOM"); + exit(EXIT_FAILURE); + } + + nftnl_rule_set(r, NFTNL_RULE_TABLE, table); + nftnl_rule_set(r, NFTNL_RULE_CHAIN, chain); + nftnl_rule_set_u32(r, NFTNL_RULE_FAMILY, family); + + if (handle != NULL) { + handle_num = atoll(handle); + nftnl_rule_set_u64(r, NFTNL_RULE_POSITION, handle_num); + } + + add_ct_timeout(r, obj_name); + + return r; +} + +int main(int argc, char *argv[]) +{ + struct mnl_socket *nl; + struct nftnl_rule *r; + struct nlmsghdr *nlh; + struct mnl_nlmsg_batch *batch; + uint8_t family; + char buf[MNL_SOCKET_BUFFER_SIZE]; + uint32_t seq = time(NULL); + int ret; + + if (argc < 5 || argc > 6) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } + if (strcmp(argv[1], "ip") == 0) + family = NFPROTO_IPV4; + else if (strcmp(argv[1], "ip6") == 0) + family = NFPROTO_IPV6; + else { + fprintf(stderr, "Unknown family: ip, ip6\n"); + exit(EXIT_FAILURE); + } + + if (argc != 6) + r = setup_rule(family, argv[2], argv[3], NULL, argv[4]); + else + r = setup_rule(family, argv[2], argv[3], argv[4], argv[5]); + + nl = mnl_socket_open(NETLINK_NETFILTER); + if (nl == NULL) { + perror("mnl_socket_open"); + exit(EXIT_FAILURE); + } + + if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { + perror("mnl_socket_bind"); + exit(EXIT_FAILURE); + } + + batch = mnl_nlmsg_batch_start(buf, sizeof(buf)); + + nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++); + mnl_nlmsg_batch_next(batch); + + nlh = nftnl_rule_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), + NFT_MSG_NEWRULE, + nftnl_rule_get_u32(r, NFTNL_RULE_FAMILY), + NLM_F_APPEND|NLM_F_CREATE|NLM_F_ACK, seq++); + + nftnl_rule_nlmsg_build_payload(nlh, r); + nftnl_rule_free(r); + mnl_nlmsg_batch_next(batch); + + nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++); + mnl_nlmsg_batch_next(batch); + + ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch), + mnl_nlmsg_batch_size(batch)); + if (ret == -1) { + perror("mnl_socket_sendto"); + exit(EXIT_FAILURE); + } + + mnl_nlmsg_batch_stop(batch); + + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + if (ret == -1) { + perror("mnl_socket_recvfrom"); + exit(EXIT_FAILURE); + } + + ret = mnl_cb_run(buf, ret, 0, mnl_socket_get_portid(nl), NULL, NULL); + if (ret < 0) { + perror("mnl_cb_run"); + exit(EXIT_FAILURE); + } + + mnl_socket_close(nl); + + return EXIT_SUCCESS; +}