Blame extensions/libxt_set.h

Packit Service d1fe03
#ifndef _LIBXT_SET_H
Packit Service d1fe03
#define _LIBXT_SET_H
Packit Service d1fe03
Packit Service d1fe03
#include <unistd.h>
Packit Service d1fe03
#include <fcntl.h>
Packit Service d1fe03
#include <sys/types.h>
Packit Service d1fe03
#include <sys/socket.h>
Packit Service d1fe03
#include <errno.h>
Packit Service d1fe03
#include "../iptables/xshared.h"
Packit Service d1fe03
Packit Service d1fe03
static int
Packit Service d1fe03
get_version(unsigned *version)
Packit Service d1fe03
{
Packit Service d1fe03
	int res, sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
Packit Service d1fe03
	struct ip_set_req_version req_version;
Packit Service d1fe03
	socklen_t size = sizeof(req_version);
Packit Service d1fe03
	
Packit Service d1fe03
	if (sockfd < 0)
Packit Service d1fe03
		xtables_error(OTHER_PROBLEM,
Packit Service d1fe03
			      "Can't open socket to ipset.\n");
Packit Service d1fe03
Packit Service d1fe03
	if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) == -1) {
Packit Service d1fe03
		xtables_error(OTHER_PROBLEM,
Packit Service d1fe03
			      "Could not set close on exec: %s\n",
Packit Service d1fe03
			      strerror(errno));
Packit Service d1fe03
	}
Packit Service d1fe03
Packit Service d1fe03
	req_version.op = IP_SET_OP_VERSION;
Packit Service d1fe03
	res = getsockopt(sockfd, SOL_IP, SO_IP_SET, &req_version, &size);
Packit Service d1fe03
	if (res != 0)
Packit Service d1fe03
		xtables_error(OTHER_PROBLEM,
Packit Service d1fe03
			      "Kernel module xt_set is not loaded in.\n");
Packit Service d1fe03
Packit Service d1fe03
	*version = req_version.version;
Packit Service d1fe03
	
Packit Service d1fe03
	return sockfd;
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static void
Packit Service d1fe03
get_set_byid(char *setname, ip_set_id_t idx)
Packit Service d1fe03
{
Packit Service d1fe03
	struct ip_set_req_get_set req;
Packit Service d1fe03
	socklen_t size = sizeof(struct ip_set_req_get_set);
Packit Service d1fe03
	int res, sockfd;
Packit Service d1fe03
Packit Service d1fe03
	sockfd = get_version(&req.version);
Packit Service d1fe03
	req.op = IP_SET_OP_GET_BYINDEX;
Packit Service d1fe03
	req.set.index = idx;
Packit Service d1fe03
	res = getsockopt(sockfd, SOL_IP, SO_IP_SET, &req, &size);
Packit Service d1fe03
	close(sockfd);
Packit Service d1fe03
Packit Service d1fe03
	if (res != 0)
Packit Service d1fe03
		xtables_error(OTHER_PROBLEM,
Packit Service d1fe03
			"Problem when communicating with ipset, errno=%d.\n",
Packit Service d1fe03
			errno);
Packit Service d1fe03
	if (size != sizeof(struct ip_set_req_get_set))
Packit Service d1fe03
		xtables_error(OTHER_PROBLEM,
Packit Service d1fe03
			"Incorrect return size from kernel during ipset lookup, "
Packit Service d1fe03
			"(want %zu, got %zu)\n",
Packit Service d1fe03
			sizeof(struct ip_set_req_get_set), (size_t)size);
Packit Service d1fe03
	if (req.set.name[0] == '\0')
Packit Service d1fe03
		xtables_error(PARAMETER_PROBLEM,
Packit Service d1fe03
			"Set with index %i in kernel doesn't exist.\n", idx);
Packit Service d1fe03
Packit Service d1fe03
	strncpy(setname, req.set.name, IPSET_MAXNAMELEN);
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static void
Packit Service d1fe03
get_set_byname_only(const char *setname, struct xt_set_info *info,
Packit Service d1fe03
		    int sockfd, unsigned int version)
Packit Service d1fe03
{
Packit Service d1fe03
	struct ip_set_req_get_set req = { .version = version };
Packit Service d1fe03
	socklen_t size = sizeof(struct ip_set_req_get_set);
Packit Service d1fe03
	int res;
Packit Service d1fe03
Packit Service d1fe03
	req.op = IP_SET_OP_GET_BYNAME;
Packit Service d1fe03
	strncpy(req.set.name, setname, IPSET_MAXNAMELEN);
Packit Service d1fe03
	req.set.name[IPSET_MAXNAMELEN - 1] = '\0';
Packit Service d1fe03
	res = getsockopt(sockfd, SOL_IP, SO_IP_SET, &req, &size);
Packit Service d1fe03
	close(sockfd);
Packit Service d1fe03
Packit Service d1fe03
	if (res != 0)
Packit Service d1fe03
		xtables_error(OTHER_PROBLEM,
Packit Service d1fe03
			"Problem when communicating with ipset, errno=%d.\n",
Packit Service d1fe03
			errno);
Packit Service d1fe03
	if (size != sizeof(struct ip_set_req_get_set))
Packit Service d1fe03
		xtables_error(OTHER_PROBLEM,
Packit Service d1fe03
			"Incorrect return size from kernel during ipset lookup, "
Packit Service d1fe03
			"(want %zu, got %zu)\n",
Packit Service d1fe03
			sizeof(struct ip_set_req_get_set), (size_t)size);
Packit Service d1fe03
	if (req.set.index == IPSET_INVALID_ID)
Packit Service d1fe03
		xtables_error(PARAMETER_PROBLEM,
Packit Service d1fe03
			      "Set %s doesn't exist.\n", setname);
Packit Service d1fe03
Packit Service d1fe03
	info->index = req.set.index;
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static void
Packit Service d1fe03
get_set_byname(const char *setname, struct xt_set_info *info)
Packit Service d1fe03
{
Packit Service d1fe03
	struct ip_set_req_get_set_family req;
Packit Service d1fe03
	socklen_t size = sizeof(struct ip_set_req_get_set_family);
Packit Service d1fe03
	int res, sockfd, version;
Packit Service d1fe03
Packit Service d1fe03
	sockfd = get_version(&req.version);
Packit Service d1fe03
	version = req.version;
Packit Service d1fe03
	req.op = IP_SET_OP_GET_FNAME;
Packit Service d1fe03
	strncpy(req.set.name, setname, IPSET_MAXNAMELEN);
Packit Service d1fe03
	req.set.name[IPSET_MAXNAMELEN - 1] = '\0';
Packit Service d1fe03
	res = getsockopt(sockfd, SOL_IP, SO_IP_SET, &req, &size);
Packit Service d1fe03
Packit Service d1fe03
	if (res != 0 && errno == EBADMSG)
Packit Service d1fe03
		/* Backward compatibility */
Packit Service d1fe03
		return get_set_byname_only(setname, info, sockfd, version);
Packit Service d1fe03
Packit Service d1fe03
	close(sockfd);
Packit Service d1fe03
	if (res != 0)
Packit Service d1fe03
		xtables_error(OTHER_PROBLEM,
Packit Service d1fe03
			"Problem when communicating with ipset, errno=%d.\n",
Packit Service d1fe03
			errno);
Packit Service d1fe03
	if (size != sizeof(struct ip_set_req_get_set_family))
Packit Service d1fe03
		xtables_error(OTHER_PROBLEM,
Packit Service d1fe03
			"Incorrect return size from kernel during ipset lookup, "
Packit Service d1fe03
			"(want %zu, got %zu)\n",
Packit Service d1fe03
			sizeof(struct ip_set_req_get_set_family),
Packit Service d1fe03
			(size_t)size);
Packit Service d1fe03
	if (req.set.index == IPSET_INVALID_ID)
Packit Service d1fe03
		xtables_error(PARAMETER_PROBLEM,
Packit Service d1fe03
			      "Set %s doesn't exist.\n", setname);
Packit Service d1fe03
	if (!(req.family == afinfo->family ||
Packit Service d1fe03
	      req.family == NFPROTO_UNSPEC))
Packit Service d1fe03
		xtables_error(PARAMETER_PROBLEM,
Packit Service d1fe03
			      "The protocol family of set %s is %s, "
Packit Service d1fe03
			      "which is not applicable.\n",
Packit Service d1fe03
			      setname,
Packit Service d1fe03
			      req.family == NFPROTO_IPV4 ? "IPv4" : "IPv6");
Packit Service d1fe03
Packit Service d1fe03
	info->index = req.set.index;
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static void
Packit Service d1fe03
parse_dirs_v0(const char *opt_arg, struct xt_set_info_v0 *info)
Packit Service d1fe03
{
Packit Service d1fe03
	char *saved = strdup(opt_arg);
Packit Service d1fe03
	char *ptr, *tmp = saved;
Packit Service d1fe03
	int i = 0;
Packit Service d1fe03
	
Packit Service d1fe03
	while (i < (IPSET_DIM_MAX - 1) && tmp != NULL) {
Packit Service d1fe03
		ptr = strsep(&tmp, ",");
Packit Service d1fe03
		if (strncmp(ptr, "src", 3) == 0)
Packit Service d1fe03
			info->u.flags[i++] |= IPSET_SRC;
Packit Service d1fe03
		else if (strncmp(ptr, "dst", 3) == 0)
Packit Service d1fe03
			info->u.flags[i++] |= IPSET_DST;
Packit Service d1fe03
		else
Packit Service d1fe03
			xtables_error(PARAMETER_PROBLEM,
Packit Service d1fe03
				"You must spefify (the comma separated list of) 'src' or 'dst'.");
Packit Service d1fe03
	}
Packit Service d1fe03
Packit Service d1fe03
	if (tmp)
Packit Service d1fe03
		xtables_error(PARAMETER_PROBLEM,
Packit Service d1fe03
			      "Can't be more src/dst options than %i.", 
Packit Service d1fe03
			      IPSET_DIM_MAX);
Packit Service d1fe03
Packit Service d1fe03
	free(saved);
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static void
Packit Service d1fe03
parse_dirs(const char *opt_arg, struct xt_set_info *info)
Packit Service d1fe03
{
Packit Service d1fe03
	char *saved = strdup(opt_arg);
Packit Service d1fe03
	char *ptr, *tmp = saved;
Packit Service d1fe03
	
Packit Service d1fe03
	while (info->dim < IPSET_DIM_MAX && tmp != NULL) {
Packit Service d1fe03
		info->dim++;
Packit Service d1fe03
		ptr = strsep(&tmp, ",");
Packit Service d1fe03
		if (strncmp(ptr, "src", 3) == 0)
Packit Service d1fe03
			info->flags |= (1 << info->dim);
Packit Service d1fe03
		else if (strncmp(ptr, "dst", 3) != 0)
Packit Service d1fe03
			xtables_error(PARAMETER_PROBLEM,
Packit Service d1fe03
				"You must spefify (the comma separated list of) 'src' or 'dst'.");
Packit Service d1fe03
	}
Packit Service d1fe03
Packit Service d1fe03
	if (tmp)
Packit Service d1fe03
		xtables_error(PARAMETER_PROBLEM,
Packit Service d1fe03
			      "Can't be more src/dst options than %i.", 
Packit Service d1fe03
			      IPSET_DIM_MAX);
Packit Service d1fe03
Packit Service d1fe03
	free(saved);
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
#endif /*_LIBXT_SET_H*/