18808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf/*
28808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf * src/lib/ct.c		CLI Conntrack Helpers
38808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf *
48808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf *	This library is free software; you can redistribute it and/or
58808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf *	modify it under the terms of the GNU Lesser General Public
68808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf *	License as published by the Free Software Foundation version 2.1
78808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf *	of the License.
88808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf *
98808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf * Copyright (c) 2008-2009 Thomas Graf <tgraf@suug.ch>
108808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf */
118808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
128808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf/**
138808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf * @ingroup cli
148808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf * @defgroup cli_ct Connection Tracking
158808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf *
168808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf * @{
178808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf */
188808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
198808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf#include <netlink/cli/utils.h>
208808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf#include <netlink/cli/ct.h>
218808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
228808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Grafstruct nfnl_ct *nl_cli_ct_alloc(void)
238808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf{
248808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	struct nfnl_ct *ct;
258808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
268808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	ct = nfnl_ct_alloc();
278808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	if (!ct)
288808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		nl_cli_fatal(ENOMEM, "Unable to allocate conntrack object");
298808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
308808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	return ct;
318808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf}
328808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
338808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Grafstruct nl_cache *nl_cli_ct_alloc_cache(struct nl_sock *sk)
348808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf{
358808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	return nl_cli_alloc_cache(sk, "conntrack", nfnl_ct_alloc_cache);
368808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf}
378808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
388808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Grafvoid nl_cli_ct_parse_family(struct nfnl_ct *ct, char *arg)
398808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf{
408808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	int family;
418808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
428808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	if ((family = nl_str2af(arg)) == AF_UNSPEC)
438808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		nl_cli_fatal(EINVAL,
448808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			     "Unable to nl_cli_ct_parse family \"%s\": %s",
458808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			     arg, nl_geterror(NLE_INVAL));
468808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
478808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	nfnl_ct_set_family(ct, family);
488808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf}
498808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
508808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Grafvoid nl_cli_ct_parse_protocol(struct nfnl_ct *ct, char *arg)
518808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf{
528808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	int proto;
538808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
548808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	if ((proto = nl_str2ip_proto(arg)) < 0)
558808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		nl_cli_fatal(proto,
568808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			     "Unable to nl_cli_ct_parse protocol \"%s\": %s",
578808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			     arg, nl_geterror(proto));
588808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
598808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	nfnl_ct_set_proto(ct, proto);
608808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf}
618808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
628808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Grafvoid nl_cli_ct_parse_mark(struct nfnl_ct *ct, char *arg)
638808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf{
648808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	uint32_t mark = nl_cli_parse_u32(arg);
658808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	nfnl_ct_set_mark(ct, mark);
668808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf}
678808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
688808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Grafvoid nl_cli_ct_parse_timeout(struct nfnl_ct *ct, char *arg)
698808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf{
708808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	uint32_t timeout = nl_cli_parse_u32(arg);
718808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	nfnl_ct_set_timeout(ct, timeout);
728808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf}
738808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
748808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Grafvoid nl_cli_ct_parse_id(struct nfnl_ct *ct, char *arg)
758808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf{
768808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	uint32_t id = nl_cli_parse_u32(arg);
778808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	nfnl_ct_set_id(ct, id);
788808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf}
798808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
808808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Grafvoid nl_cli_ct_parse_use(struct nfnl_ct *ct, char *arg)
818808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf{
828808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	uint32_t use = nl_cli_parse_u32(arg);
838808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	nfnl_ct_set_use(ct, use);
848808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf}
858808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
868808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Grafvoid nl_cli_ct_parse_src(struct nfnl_ct *ct, int reply, char *arg)
878808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf{
888808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	int err;
898808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	struct nl_addr *a = nl_cli_addr_parse(arg, nfnl_ct_get_family(ct));
908808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	if ((err = nfnl_ct_set_src(ct, reply, a)) < 0)
918808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		nl_cli_fatal(err, "Unable to set source address: %s",
928808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			     nl_geterror(err));
938808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf}
948808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
958808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Grafvoid nl_cli_ct_parse_dst(struct nfnl_ct *ct, int reply, char *arg)
968808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf{
978808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	int err;
988808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	struct nl_addr *a = nl_cli_addr_parse(arg, nfnl_ct_get_family(ct));
998808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	if ((err = nfnl_ct_set_dst(ct, reply, a)) < 0)
1008808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		nl_cli_fatal(err, "Unable to set destination address: %s",
1018808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			     nl_geterror(err));
1028808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf}
1038808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
1048808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Grafvoid nl_cli_ct_parse_src_port(struct nfnl_ct *ct, int reply, char *arg)
1058808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf{
1068808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	uint32_t port = nl_cli_parse_u32(arg);
1078808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	nfnl_ct_set_src_port(ct, reply, port);
1088808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf}
1098808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
1108808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Grafvoid nl_cli_ct_parse_dst_port(struct nfnl_ct *ct, int reply, char *arg)
1118808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf{
1128808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	uint32_t port = nl_cli_parse_u32(arg);
1138808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	nfnl_ct_set_dst_port(ct, reply, port);
1148808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf}
1158808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
1168808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Grafvoid nl_cli_ct_parse_tcp_state(struct nfnl_ct *ct, char *arg)
1178808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf{
1188808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	int state;
1198808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
1208808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	if ((state = nfnl_ct_str2tcp_state(arg)) < 0)
1218808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		nl_cli_fatal(state,
1228808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			     "Unable to nl_cli_ct_parse tcp state \"%s\": %s",
1238808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			     arg, nl_geterror(state));
1248808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
1258808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	nfnl_ct_set_tcp_state(ct, state);
1268808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf}
1278808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
1288808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Grafvoid nl_cli_ct_parse_status(struct nfnl_ct *ct, char *arg)
1298808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf{
1308808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	int status;
1318808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
1328808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	if ((status = nfnl_ct_str2status(arg)) < 0)
1338808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		nl_cli_fatal(status,
1348808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			     "Unable to nl_cli_ct_parse flags \"%s\": %s",
1358808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			     arg, nl_geterror(status));
1368808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
1378808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf	nfnl_ct_set_status(ct, status);
1388808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf}
1398808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
1408808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf#if 0
1418808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		} else if (arg_match("origicmpid")) {
1428808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			if (argc > ++idx)
1438808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf				nfnl_ct_set_icmp_id(ct, 0, strtoul(argv[idx++], NULL, 0));
1448808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		} else if (arg_match("origicmptype")) {
1458808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			if (argc > ++idx)
1468808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf				nfnl_ct_set_icmp_type(ct, 0, strtoul(argv[idx++], NULL, 0));
1478808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		} else if (arg_match("origicmpcode")) {
1488808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			if (argc > ++idx)
1498808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf				nfnl_ct_set_icmp_code(ct, 0, strtoul(argv[idx++], NULL, 0));
1508808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		} else if (arg_match("replyicmpid")) {
1518808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			if (argc > ++idx)
1528808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf				nfnl_ct_set_icmp_id(ct, 1, strtoul(argv[idx++], NULL, 0));
1538808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		} else if (arg_match("replyicmptype")) {
1548808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			if (argc > ++idx)
1558808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf				nfnl_ct_set_icmp_type(ct, 1, strtoul(argv[idx++], NULL, 0));
1568808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		} else if (arg_match("replyicmpcode")) {
1578808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf			if (argc > ++idx)
1588808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf				nfnl_ct_set_icmp_code(ct, 1, strtoul(argv[idx++], NULL, 0));
1598808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf		}
1608808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf#endif
1618808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf
1628808743839b0f459394ecd00cb0f7c1896c0ab7aThomas Graf/** @} */
163