From patchwork Fri Aug 10 14:57:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harsha Sharma X-Patchwork-Id: 956279 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=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="lS8NJi2I"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 41n7X9194gz9ryt for ; Sat, 11 Aug 2018 00:57:45 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728050AbeHJR16 (ORCPT ); Fri, 10 Aug 2018 13:27:58 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:38831 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727543AbeHJR15 (ORCPT ); Fri, 10 Aug 2018 13:27:57 -0400 Received: by mail-pg1-f195.google.com with SMTP id k3-v6so4525032pgq.5 for ; Fri, 10 Aug 2018 07:57:43 -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=wgXHYjXL96lJiTjiiyNopynQBCQS7D82yEEItCKFK/A=; b=lS8NJi2IuhOn/JVnWI3wBvRucX/6lkovO2ZNcqZuLCwmDHCswgX9OO6zND1O+ylfmG qRD3AKhHKIpL0bi/CgLgWvvk9ydIqV11sfEfc0Xit7uzYyf+/iGRTYcxNF2Z8AgS4X9e mZKcXW6odMlQpDZceojG+NX30UtIBDfmfx00zNdkEZfHC4Rh6e1Ag5uR1MEfsXUabzzI Yv7YqvHtndeMQsRnHDJUl/CPazPvlVpSz9CkzXAwS0/NQK5XK+4YaZ1Z4U4rJ9HGl2wI yESSdjrAdGSokMw+7e/saaJOepqI2wQQy9ICDfinZhsPnazyUsAkyU7nuK4rW7E5ENyw cnuw== 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=wgXHYjXL96lJiTjiiyNopynQBCQS7D82yEEItCKFK/A=; b=jcPAtwkCw5/91Smlgk4YhLoXpsdAYICdpqpG+M8EUzhcWOC3Mdn7xgOn9QBxWdP0AM WyijZ2Ich3/6lMEWsdrwsjbXW8lZzyh003kxlYuCcrd9yjvYOnnhzb3T3sS6PY3ZKhSu i0byPjH2svDGRNxguCERlBv4O4X80R4lLJqNrPk1P++ssOz8Cl1R76K0wOAMSW4X5lWn rPhU5t06llyUBZzQWNw5JFi5lETFvbTELst6DQwEzxY95cJqCrmP95+n9Ih66+HK/Hkm Jjaf+wJUDlHqoUsGu4TW+yJwdeobJ7LDVLG8SvmyyqkMHkyxQtppXUT446sUlEYlOh0H F3Vw== X-Gm-Message-State: AOUpUlEd2Kn/dVFFDqYjuBveSxb86E582UE52KK3no5N7kMpEAx3n1g2 OpFPuVBWIgCPWtzKwEoVOj28B13g X-Google-Smtp-Source: AA+uWPxDnunpCgi5ISzCnYnsDL2PVra06/Srw3G54XFpO1IOEuefAndEdwya+1hFOXoNUvwaJeb9Hg== X-Received: by 2002:a63:844:: with SMTP id 65-v6mr7002567pgi.406.1533913063305; Fri, 10 Aug 2018 07:57:43 -0700 (PDT) Received: from XDDDDD.iitr.ernet.in ([27.60.156.54]) by smtp.gmail.com with ESMTPSA id k64-v6sm19796631pfc.160.2018.08.10.07.57.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Aug 2018 07:57:42 -0700 (PDT) From: Harsha Sharma To: harshasharmaiitr@gmail.com, pablo@netfilter.org Cc: netfilter-devel@vger.kernel.org Subject: [PATCH libnftnl v6 2/3] examples: add nft-ct-timeout-{add,del,get} Date: Fri, 10 Aug 2018 20:27:07 +0530 Message-Id: <20180810145707.11764-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 Add, list and delete ct timeout objects from specified table Usage e.g.: % ./nft-ct-timeout-add ip filter some-name tcp % ./nft-ct-timeout-get ip filter table filter name some-name use 0 [ ct_timeout family 2 protocol 6 policy = {ESTABLISHED = 111,CLOSE_WAIT = 14, CLOSE = 16}] % ./nft-ct-timeout-del ip filter some-name Signed-off-by: Harsha Sharma --- Changes in v6: - No changes Changes in v5: - remove nftnl_timeout_policy_attr_set_u32 and use nftnl_obj_set Changes in v4: - No changes Changes in v3: - Change log message with update listing of timeout policies Changes in v2: - Updated timeout policy values examples/Makefile.am | 12 ++++ examples/nft-ct-timeout-add.c | 151 ++++++++++++++++++++++++++++++++++++++++++ examples/nft-ct-timeout-del.c | 124 ++++++++++++++++++++++++++++++++++ examples/nft-ct-timeout-get.c | 150 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 437 insertions(+) create mode 100644 examples/nft-ct-timeout-add.c create mode 100644 examples/nft-ct-timeout-del.c create mode 100644 examples/nft-ct-timeout-get.c diff --git a/examples/Makefile.am b/examples/Makefile.am index 3ec699b..d5584e5 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -25,6 +25,9 @@ check_PROGRAMS = nft-table-add \ nft-obj-add \ nft-obj-get \ nft-obj-del \ + nft-ct-timeout-add \ + nft-ct-timeout-get \ + nft-ct-timeout-del \ nft-flowtable-add \ nft-flowtable-del \ nft-flowtable-get \ @@ -111,6 +114,15 @@ nft_obj_del_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} nft_obj_get_SOURCES = nft-obj-get.c nft_obj_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} +nft_ct_timeout_add_SOURCES = nft-ct-timeout-add.c +nft_ct_timeout_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} + +nft_ct_timeout_get_SOURCES = nft-ct-timeout-get.c +nft_ct_timeout_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} + +nft_ct_timeout_del_SOURCES = nft-ct-timeout-del.c +nft_ct_timeout_del_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} + nft_flowtable_add_SOURCES = nft-flowtable-add.c nft_flowtable_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} diff --git a/examples/nft-ct-timeout-add.c b/examples/nft-ct-timeout-add.c new file mode 100644 index 0000000..6727913 --- /dev/null +++ b/examples/nft-ct-timeout-add.c @@ -0,0 +1,151 @@ +/* + * (C) 2012-2016 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. + */ + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +static struct nftnl_obj *obj_add_parse(int argc, char *argv[]) +{ + size_t timeout_array_size; + struct nftnl_obj *t; + uint32_t *timeout; + uint16_t family; + uint8_t l4proto; + + if (strcmp(argv[1], "ip") == 0) + family = NFPROTO_IPV4; + else if (strcmp(argv[1], "ip6") == 0) + family = NFPROTO_IPV6; + else if (strcmp(argv[1], "bridge") == 0) + family = NFPROTO_BRIDGE; + else if (strcmp(argv[1], "arp") == 0) + family = NFPROTO_ARP; + else { + fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n"); + return NULL; + } + + if (strcmp(argv[4], "udp") == 0) + l4proto = IPPROTO_UDP; + else if (strcmp(argv[4], "tcp") == 0) + l4proto = IPPROTO_TCP; + else { + fprintf(stderr, "Unknown layer 4 protocol\n"); + return NULL; + } + + t = nftnl_obj_alloc(); + if (t == NULL) { + perror("OOM"); + return NULL; + } + + timeout_array_size = sizeof(uint32_t) * (NFTNL_CTTIMEOUT_TCP_MAX); + timeout = calloc(1, timeout_array_size); + if (timeout == NULL) { + perror("OOM"); + return NULL; + } + + timeout[NFTNL_CTTIMEOUT_TCP_ESTABLISHED] = 111; + timeout[NFTNL_CTTIMEOUT_TCP_CLOSE] = 16; + timeout[NFTNL_CTTIMEOUT_TCP_CLOSE_WAIT] = 14; + nftnl_obj_set_u32(t, NFTNL_OBJ_FAMILY, family); + nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_CT_TIMEOUT); + nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]); + nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]); + nftnl_obj_set_u8(t, NFTNL_OBJ_CT_TIMEOUT_L4PROTO, l4proto); + nftnl_obj_set_u16(t, NFTNL_OBJ_CT_TIMEOUT_L3PROTO, NFPROTO_IPV4); + nftnl_obj_set(t, NFTNL_OBJ_CT_TIMEOUT_DATA, timeout); + return t; + +} + +int main(int argc, char *argv[]) +{ + struct mnl_socket *nl; + char buf[MNL_SOCKET_BUFFER_SIZE]; + struct nlmsghdr *nlh; + uint32_t portid, seq, obj_seq, family; + struct nftnl_obj *t; + struct mnl_nlmsg_batch *batch; + int ret; + + if (argc != 5) { + fprintf(stderr, "%s \n", argv[0]); + exit(EXIT_FAILURE); + } + + t = obj_add_parse(argc, argv); + if (t == NULL) { + exit(EXIT_FAILURE); + } + + seq = time(NULL); + batch = mnl_nlmsg_batch_start(buf, sizeof(buf)); + + nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++); + mnl_nlmsg_batch_next(batch); + + obj_seq = seq; + family = nftnl_obj_get_u32(t, NFTNL_OBJ_FAMILY); + nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), + NFT_MSG_NEWOBJ, family, NLM_F_ACK | NLM_F_CREATE, seq++); + nftnl_obj_nlmsg_build_payload(nlh, t); + nftnl_obj_free(t); + mnl_nlmsg_batch_next(batch); + + nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++); + mnl_nlmsg_batch_next(batch); + + 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); + } + portid = mnl_socket_get_portid(nl); + + if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch), + mnl_nlmsg_batch_size(batch)) < 0) { + perror("mnl_socket_send"); + exit(EXIT_FAILURE); + } + + mnl_nlmsg_batch_stop(batch); + + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + while (ret > 0) { + ret = mnl_cb_run(buf, ret, obj_seq, portid, NULL, NULL); + if (ret <= 0) + break; + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + } + if (ret == -1) { + perror("error"); + exit(EXIT_FAILURE); + } + mnl_socket_close(nl); + + return EXIT_SUCCESS; +} diff --git a/examples/nft-ct-timeout-del.c b/examples/nft-ct-timeout-del.c new file mode 100644 index 0000000..4581c39 --- /dev/null +++ b/examples/nft-ct-timeout-del.c @@ -0,0 +1,124 @@ +/* + * (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 + +#include +#include + +#include +#include + +static struct nftnl_obj *ct_timeout_del_parse(int argc, char *argv[]) +{ + struct nftnl_obj *t; + uint16_t family; + + if (strcmp(argv[1], "ip") == 0) + family = NFPROTO_IPV4; + else if (strcmp(argv[1], "ip6") == 0) + family = NFPROTO_IPV6; + else if (strcmp(argv[1], "inet") == 0) + family = NFPROTO_INET; + else { + fprintf(stderr, "Unknown family: ip, ip6, inet\n"); + return NULL; + } + + t = nftnl_obj_alloc(); + if (t == NULL) { + perror("OOM"); + return NULL; + } + + nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]); + nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]); + nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_CT_TIMEOUT); + nftnl_obj_set_u32(t, NFTNL_OBJ_FAMILY, family); + + return t; +} + +int main(int argc, char *argv[]) +{ + struct mnl_socket *nl; + char buf[MNL_SOCKET_BUFFER_SIZE]; + struct nlmsghdr *nlh; + uint32_t portid, seq, obj_seq, family; + struct nftnl_obj *t; + struct mnl_nlmsg_batch *batch; + int ret; + + if (argc != 4) { + fprintf(stderr, "%s
\n", argv[0]); + exit(EXIT_FAILURE); + } + + t = ct_timeout_del_parse(argc, argv); + if (t == NULL) + exit(EXIT_FAILURE); + + seq = time(NULL); + batch = mnl_nlmsg_batch_start(buf, sizeof(buf)); + + nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++); + mnl_nlmsg_batch_next(batch); + + obj_seq = seq; + family = nftnl_obj_get_u32(t, NFTNL_OBJ_FAMILY); + nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), + NFT_MSG_DELOBJ, family, NLM_F_ACK, + seq++); + nftnl_obj_nlmsg_build_payload(nlh, t); + mnl_nlmsg_batch_next(batch); + nftnl_obj_free(t); + + nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++); + mnl_nlmsg_batch_next(batch); + + 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); + } + portid = mnl_socket_get_portid(nl); + + if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch), + mnl_nlmsg_batch_size(batch)) < 0) { + perror("mnl_socket_send"); + exit(EXIT_FAILURE); + } + + mnl_nlmsg_batch_stop(batch); + + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + while (ret > 0) { + ret = mnl_cb_run(buf, ret, obj_seq, portid, NULL, NULL); + if (ret <= 0) + break; + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + } + if (ret == -1) { + perror("error"); + exit(EXIT_FAILURE); + } + mnl_socket_close(nl); + + return EXIT_SUCCESS; +} diff --git a/examples/nft-ct-timeout-get.c b/examples/nft-ct-timeout-get.c new file mode 100644 index 0000000..503c488 --- /dev/null +++ b/examples/nft-ct-timeout-get.c @@ -0,0 +1,150 @@ +/* + * (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 + +#include +#include + +#include +#include + +static int obj_cb(const struct nlmsghdr *nlh, void *data) +{ + struct nftnl_obj *t; + char buf[4096]; + uint32_t *type = data; + + t = nftnl_obj_alloc(); + if (t == NULL) { + perror("OOM"); + goto err; + } + + if (nftnl_obj_nlmsg_parse(nlh, t) < 0) { + perror("nftnl_obj_nlmsg_parse"); + goto err_free; + } + + nftnl_obj_snprintf(buf, sizeof(buf), t, *type, 0); + printf("%s\n", buf); + +err_free: + nftnl_obj_free(t); +err: + return MNL_CB_OK; +} + +int main(int argc, char *argv[]) +{ + struct mnl_socket *nl; + char buf[MNL_SOCKET_BUFFER_SIZE]; + struct nlmsghdr *nlh; + uint32_t portid, seq, family; + struct nftnl_obj *t = NULL; + int ret; + uint32_t type = NFTNL_OUTPUT_DEFAULT; + + if (argc < 3 || argc > 5) { + fprintf(stderr, "%s
[] []\n", + argv[0]); + return EXIT_FAILURE; + } + + if (strcmp(argv[1], "ip") == 0) + family = NFPROTO_IPV4; + else if (strcmp(argv[1], "ip6") == 0) + family = NFPROTO_IPV6; + else if (strcmp(argv[1], "inet") == 0) + family = NFPROTO_INET; + else if (strcmp(argv[1], "unspec") == 0) + family = NFPROTO_UNSPEC; + else { + fprintf(stderr, "Unknown family: ip, ip6, inet, unspec"); + exit(EXIT_FAILURE); + } + + if (strcmp(argv[argc-1], "xml") == 0) { + type = NFTNL_OUTPUT_XML; + argv[argc-1] = NULL; + argc--; + } else if (strcmp(argv[argc-1], "json") == 0) { + type = NFTNL_OUTPUT_JSON; + argv[argc-1] = NULL; + argc--; + } else if (strcmp(argv[argc - 1], "default") == 0) { + argc--; + } + + if (argc == 3 || argc == 4) { + t = nftnl_obj_alloc(); + if (t == NULL) { + perror("OOM"); + exit(EXIT_FAILURE); + } + } + + seq = time(NULL); + nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_CT_TIMEOUT); + if (argc < 4) { + nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family, + NLM_F_DUMP, seq); + if (argc == 3) { + nftnl_obj_set(t, NFTNL_OBJ_TABLE, argv[2]); + nftnl_obj_nlmsg_build_payload(nlh, t); + nftnl_obj_free(t); + } + } else { + nftnl_obj_set(t, NFTNL_OBJ_TABLE, argv[2]); + nftnl_obj_set(t, NFTNL_OBJ_NAME, argv[3]); + + nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family, + NLM_F_ACK, seq); + nftnl_obj_nlmsg_build_payload(nlh, t); + nftnl_obj_free(t); + } + + 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); + } + portid = mnl_socket_get_portid(nl); + + if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { + perror("mnl_socket_send"); + exit(EXIT_FAILURE); + } + + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + while (ret > 0) { + ret = mnl_cb_run(buf, ret, seq, portid, obj_cb, &type); + if (ret <= 0) + break; + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + } + if (ret == -1) { + perror("error"); + exit(EXIT_FAILURE); + } + mnl_socket_close(nl); + + return EXIT_SUCCESS; +}