• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/nf-ct-add.c     Add Conntrack Entry
4  *
5  *	This library is free software; you can redistribute it and/or
6  *	modify it under the terms of the GNU Lesser General Public
7  *	License as published by the Free Software Foundation version 2.1
8  *	of the License.
9  *
10  * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
11  * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
12  * Copyright (c) 2007 Secure Computing Corporation
13  */
14 
15 #include <netlink/cli/utils.h>
16 #include <netlink/cli/ct.h>
17 
18 #include <linux/rtnetlink.h>
19 
20 static int quiet = 0;
21 
print_usage(void)22 static void print_usage(void)
23 {
24 	printf(
25 	"Usage: nf-ct-add [OPTION]... [CONNTRACK ENTRY]\n"
26 	"\n"
27 	"Options\n"
28 	" -q, --quiet           Do not print informal notifications.\n"
29 	" -h, --help            Show this help\n"
30 	" -v, --version         Show versioning information\n"
31 	"\n"
32 	"Conntrack Selection\n"
33 	" -p, --proto=PROTOCOL    Protocol\n"
34 	"     --orig-src=ADDR     Original source address\n"
35 	"     --orig-sport=PORT   Original source port\n"
36 	"     --orig-dst=ADDR     Original destination address\n"
37 	"     --orig-dport=PORT   Original destination port\n"
38 	"     --reply-src=ADDR    Reply source address\n"
39 	"     --reply-sport=PORT  Reply source port\n"
40 	"     --reply-dst=ADDR    Reply destination address\n"
41 	"     --reply-dport=PORT  Reply destination port\n"
42 	" -F, --family=FAMILY     Address family\n"
43 	"     --mark=NUM          Mark value\n"
44 	"     --timeout=NUM       Timeout value\n"
45 	"     --status            Bitset representing status of connection.\n"
46 	"     --zone=NUM          Zone value\n"
47 	);
48 	exit(0);
49 }
50 
main(int argc,char * argv[])51 int main(int argc, char *argv[])
52 {
53 	struct nl_sock *sock;
54 	struct nfnl_ct *ct;
55 	struct nl_dump_params params = {
56 		.dp_type = NL_DUMP_LINE,
57 		.dp_fd = stdout,
58 	};
59 	int err, nlflags = NLM_F_CREATE;
60 
61 	ct = nl_cli_ct_alloc();
62 
63 	for (;;) {
64 		int c, optidx = 0;
65 		enum {
66 			ARG_ORIG_SRC = 257,
67 			ARG_ORIG_SPORT = 258,
68 			ARG_ORIG_DST,
69 			ARG_ORIG_DPORT,
70 			ARG_REPLY_SRC,
71 			ARG_REPLY_SPORT,
72 			ARG_REPLY_DST,
73 			ARG_REPLY_DPORT,
74 			ARG_MARK,
75 			ARG_TIMEOUT,
76 			ARG_STATUS,
77 			ARG_ZONE,
78 		};
79 		static struct option long_opts[] = {
80 			{ "quiet", 0, 0, 'q' },
81 			{ "help", 0, 0, 'h' },
82 			{ "version", 0, 0, 'v' },
83 			{ "proto", 1, 0, 'p' },
84 			{ "orig-src", 1, 0, ARG_ORIG_SRC },
85 			{ "orig-sport", 1, 0, ARG_ORIG_SPORT },
86 			{ "orig-dst", 1, 0, ARG_ORIG_DST },
87 			{ "orig-dport", 1, 0, ARG_ORIG_DPORT },
88 			{ "reply-src", 1, 0, ARG_REPLY_SRC },
89 			{ "reply-sport", 1, 0, ARG_REPLY_SPORT },
90 			{ "reply-dst", 1, 0, ARG_REPLY_DST },
91 			{ "reply-dport", 1, 0, ARG_REPLY_DPORT },
92 			{ "family", 1, 0, 'F' },
93 			{ "mark", 1, 0, ARG_MARK },
94 			{ "timeout", 1, 0, ARG_TIMEOUT },
95 			{ "status", 1, 0, ARG_STATUS },
96 			{ "zone", 1, 0, ARG_ZONE },
97 			{ 0, 0, 0, 0 }
98 		};
99 
100 		c = getopt_long(argc, argv, "46q:hv:p:F:", long_opts, &optidx);
101 		if (c == -1)
102 			break;
103 
104 		switch (c) {
105 		case '?': exit(NLE_INVAL);
106 		case 'q': quiet = 1; break;
107 		case '4': nfnl_ct_set_family(ct, AF_INET); break;
108 		case '6': nfnl_ct_set_family(ct, AF_INET6); break;
109 		case 'h': print_usage(); break;
110 		case 'v': nl_cli_print_version(); break;
111 		case 'p': nl_cli_ct_parse_protocol(ct, optarg); break;
112 		case ARG_ORIG_SRC: nl_cli_ct_parse_src(ct, 0, optarg); break;
113 		case ARG_ORIG_SPORT: nl_cli_ct_parse_src_port(ct, 0, optarg); break;
114 		case ARG_ORIG_DST: nl_cli_ct_parse_dst(ct, 0, optarg); break;
115 		case ARG_ORIG_DPORT: nl_cli_ct_parse_dst_port(ct, 0, optarg); break;
116 		case ARG_REPLY_SRC: nl_cli_ct_parse_src(ct, 1, optarg); break;
117 		case ARG_REPLY_SPORT: nl_cli_ct_parse_src_port(ct, 1, optarg); break;
118 		case ARG_REPLY_DST: nl_cli_ct_parse_dst(ct, 1, optarg); break;
119 		case ARG_REPLY_DPORT: nl_cli_ct_parse_dst_port(ct, 1, optarg); break;
120 		case 'F': nl_cli_ct_parse_family(ct, optarg); break;
121 		case ARG_MARK: nl_cli_ct_parse_mark(ct, optarg); break;
122 		case ARG_TIMEOUT: nl_cli_ct_parse_timeout(ct, optarg); break;
123 		case ARG_STATUS: nl_cli_ct_parse_status(ct, optarg); break;
124 		case ARG_ZONE: nl_cli_ct_parse_zone(ct, optarg); break;
125 		}
126 	}
127 
128 	if (!quiet) {
129 		printf("Adding ");
130 		nl_object_dump(OBJ_CAST(ct), &params);
131 	}
132 
133 	sock = nl_cli_alloc_socket();
134 	nl_cli_connect(sock, NETLINK_NETFILTER);
135 
136 	if ((err = nfnl_ct_add(sock, ct, nlflags)) < 0)
137 		nl_cli_fatal(err, "Unable to add conntrack: %s", nl_geterror(err));
138 
139 	if (!quiet) {
140 		printf("Added ");
141 		nl_object_dump(OBJ_CAST(ct), &params);
142 	}
143 
144 	return 0;
145 }
146