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