Blame ip/ipfou.c

Packit Service 3880ab
/*
Packit Service 3880ab
 * ipfou.c	FOU (foo over UDP) support
Packit Service 3880ab
 *
Packit Service 3880ab
 *              This program is free software; you can redistribute it and/or
Packit Service 3880ab
 *              modify it under the terms of the GNU General Public License
Packit Service 3880ab
 *              as published by the Free Software Foundation; either version
Packit Service 3880ab
 *              2 of the License, or (at your option) any later version.
Packit Service 3880ab
 *
Packit Service 3880ab
 * Authors:	Tom Herbert <therbert@google.com>
Packit Service 3880ab
 */
Packit Service 3880ab
Packit Service 3880ab
#include <netdb.h>
Packit Service 3880ab
#include <stdio.h>
Packit Service 3880ab
#include <stdlib.h>
Packit Service 3880ab
#include <string.h>
Packit Service 3880ab
#include <net/if.h>
Packit Service 3880ab
#include <linux/fou.h>
Packit Service 3880ab
#include <linux/genetlink.h>
Packit Service 3880ab
#include <linux/ip.h>
Packit Service 3880ab
#include <arpa/inet.h>
Packit Service 3880ab
Packit Service 3880ab
#include "libgenl.h"
Packit Service 3880ab
#include "utils.h"
Packit Service 3880ab
#include "ip_common.h"
Packit Service 3880ab
#include "json_print.h"
Packit Service 3880ab
Packit Service 3880ab
static void usage(void)
Packit Service 3880ab
{
Packit Service 3880ab
	fprintf(stderr,
Packit Service 3880ab
		"Usage: ip fou add port PORT { ipproto PROTO  | gue }\n"
Packit Service 3880ab
		"		   [ local IFADDR ] [ peer IFADDR ]\n"
Packit Service 3880ab
		"		   [ peer_port PORT ] [ dev IFNAME ]\n"
Packit Service 3880ab
		"       ip fou del port PORT [ local IFADDR ]\n"
Packit Service 3880ab
		"		   [ peer IFADDR ] [ peer_port PORT ]\n"
Packit Service 3880ab
		"		   [ dev IFNAME ]\n"
Packit Service 3880ab
		"       ip fou show\n"
Packit Service 3880ab
		"\n"
Packit Service 3880ab
		"Where: PROTO { ipproto-name | 1..255 }\n"
Packit Service 3880ab
		"       PORT { 1..65535 }\n"
Packit Service 3880ab
		"       IFADDR { addr }\n");
Packit Service 3880ab
Packit Service 3880ab
	exit(-1);
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
/* netlink socket */
Packit Service 3880ab
static struct rtnl_handle genl_rth = { .fd = -1 };
Packit Service 3880ab
static int genl_family = -1;
Packit Service 3880ab
Packit Service 3880ab
#define FOU_REQUEST(_req, _bufsiz, _cmd, _flags)	\
Packit Service 3880ab
	GENL_REQUEST(_req, _bufsiz, genl_family, 0,	\
Packit Service 3880ab
		     FOU_GENL_VERSION, _cmd, _flags)
Packit Service 3880ab
Packit Service 3880ab
static int fou_parse_opt(int argc, char **argv, struct nlmsghdr *n,
Packit Service 3880ab
			 bool adding)
Packit Service 3880ab
{
Packit Service 3880ab
	const char *local = NULL, *peer = NULL;
Packit Service 3880ab
	__u16 port, peer_port = 0;
Packit Service 3880ab
	__u8 family = preferred_family;
Packit Service 3880ab
	bool gue_set = false;
Packit Service 3880ab
	int ipproto_set = 0;
Packit Service 3880ab
	__u8 ipproto, type;
Packit Service 3880ab
	int port_set = 0;
Packit Service 3880ab
	int index = 0;
Packit Service 3880ab
Packit Service 3880ab
	if (preferred_family == AF_UNSPEC) {
Packit Service 3880ab
		family = AF_INET;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	while (argc > 0) {
Packit Service 3880ab
		if (!matches(*argv, "port")) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
Packit Service 3880ab
			if (get_be16(&port, *argv, 0) || port == 0)
Packit Service 3880ab
				invarg("invalid port", *argv);
Packit Service 3880ab
			port_set = 1;
Packit Service 3880ab
		} else if (!matches(*argv, "ipproto")) {
Packit Service 3880ab
			struct protoent *servptr;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
Packit Service 3880ab
			servptr = getprotobyname(*argv);
Packit Service 3880ab
			if (servptr)
Packit Service 3880ab
				ipproto = servptr->p_proto;
Packit Service 3880ab
			else if (get_u8(&ipproto, *argv, 0) || ipproto == 0)
Packit Service 3880ab
				invarg("invalid ipproto", *argv);
Packit Service 3880ab
			ipproto_set = 1;
Packit Service 3880ab
		} else if (!matches(*argv, "gue")) {
Packit Service 3880ab
			gue_set = true;
Packit Service 3880ab
		} else if (!matches(*argv, "-6")) {
Packit Service 3880ab
			family = AF_INET6;
Packit Service 3880ab
		} else if (!matches(*argv, "local")) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
Packit Service 3880ab
			local = *argv;
Packit Service 3880ab
		} else if (!matches(*argv, "peer")) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
Packit Service 3880ab
			peer = *argv;
Packit Service 3880ab
		} else if (!matches(*argv, "peer_port")) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
Packit Service 3880ab
			if (get_be16(&peer_port, *argv, 0) || peer_port == 0)
Packit Service 3880ab
				invarg("invalid peer port", *argv);
Packit Service 3880ab
		} else if (!matches(*argv, "dev")) {
Packit Service 3880ab
			const char *ifname;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
Packit Service 3880ab
			ifname = *argv;
Packit Service 3880ab
Packit Service 3880ab
			if (check_ifname(ifname)) {
Packit Service 3880ab
				fprintf(stderr, "fou: invalid device name\n");
Packit Service 3880ab
				exit(EXIT_FAILURE);
Packit Service 3880ab
			}
Packit Service 3880ab
Packit Service 3880ab
			index = ll_name_to_index(ifname);
Packit Service 3880ab
Packit Service 3880ab
			if (!index) {
Packit Service 3880ab
				fprintf(stderr, "fou: unknown device name\n");
Packit Service 3880ab
				exit(EXIT_FAILURE);
Packit Service 3880ab
			}
Packit Service 3880ab
		} else {
Packit Service 3880ab
			fprintf(stderr
Packit Service 3880ab
				, "fou: unknown command \"%s\"?\n", *argv);
Packit Service 3880ab
			usage();
Packit Service 3880ab
			return -1;
Packit Service 3880ab
		}
Packit Service 3880ab
		argc--, argv++;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (!port_set) {
Packit Service 3880ab
		fprintf(stderr, "fou: missing port\n");
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (!ipproto_set && !gue_set && adding) {
Packit Service 3880ab
		fprintf(stderr, "fou: must set ipproto or gue\n");
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (ipproto_set && gue_set) {
Packit Service 3880ab
		fprintf(stderr, "fou: cannot set ipproto and gue\n");
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if ((peer_port && !peer) || (peer && !peer_port)) {
Packit Service 3880ab
		fprintf(stderr, "fou: both peer and peer port must be set\n");
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	type = gue_set ? FOU_ENCAP_GUE : FOU_ENCAP_DIRECT;
Packit Service 3880ab
Packit Service 3880ab
	addattr16(n, 1024, FOU_ATTR_PORT, port);
Packit Service 3880ab
	addattr8(n, 1024, FOU_ATTR_TYPE, type);
Packit Service 3880ab
	addattr8(n, 1024, FOU_ATTR_AF, family);
Packit Service 3880ab
Packit Service 3880ab
	if (ipproto_set)
Packit Service 3880ab
		addattr8(n, 1024, FOU_ATTR_IPPROTO, ipproto);
Packit Service 3880ab
Packit Service 3880ab
	if (local) {
Packit Service 3880ab
		inet_prefix local_addr;
Packit Service 3880ab
		__u8 attr_type = family == AF_INET ? FOU_ATTR_LOCAL_V4 :
Packit Service 3880ab
						     FOU_ATTR_LOCAL_V6;
Packit Service 3880ab
Packit Service 3880ab
		if (get_addr(&local_addr, local, family)) {
Packit Service 3880ab
			fprintf(stderr, "fou: parsing local address failed\n");
Packit Service 3880ab
			exit(EXIT_FAILURE);
Packit Service 3880ab
		}
Packit Service 3880ab
		addattr_l(n, 1024, attr_type, &local_addr.data,
Packit Service 3880ab
			  local_addr.bytelen);
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (peer) {
Packit Service 3880ab
		inet_prefix peer_addr;
Packit Service 3880ab
		__u8 attr_type = family == AF_INET ? FOU_ATTR_PEER_V4 :
Packit Service 3880ab
						     FOU_ATTR_PEER_V6;
Packit Service 3880ab
Packit Service 3880ab
		if (get_addr(&peer_addr, peer, family)) {
Packit Service 3880ab
			fprintf(stderr, "fou: parsing peer address failed\n");
Packit Service 3880ab
			exit(EXIT_FAILURE);
Packit Service 3880ab
		}
Packit Service 3880ab
		addattr_l(n, 1024, attr_type, &peer_addr.data,
Packit Service 3880ab
			  peer_addr.bytelen);
Packit Service 3880ab
Packit Service 3880ab
		if (peer_port)
Packit Service 3880ab
			addattr16(n, 1024, FOU_ATTR_PEER_PORT, peer_port);
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (index)
Packit Service 3880ab
		addattr32(n, 1024, FOU_ATTR_IFINDEX, index);
Packit Service 3880ab
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int do_add(int argc, char **argv)
Packit Service 3880ab
{
Packit Service 3880ab
	FOU_REQUEST(req, 1024, FOU_CMD_ADD, NLM_F_REQUEST);
Packit Service 3880ab
Packit Service 3880ab
	fou_parse_opt(argc, argv, &req.n, true);
Packit Service 3880ab
Packit Service 3880ab
	if (rtnl_talk(&genl_rth, &req.n, NULL) < 0)
Packit Service 3880ab
		return -2;
Packit Service 3880ab
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int do_del(int argc, char **argv)
Packit Service 3880ab
{
Packit Service 3880ab
	FOU_REQUEST(req, 1024, FOU_CMD_DEL, NLM_F_REQUEST);
Packit Service 3880ab
Packit Service 3880ab
	fou_parse_opt(argc, argv, &req.n, false);
Packit Service 3880ab
Packit Service 3880ab
	if (rtnl_talk(&genl_rth, &req.n, NULL) < 0)
Packit Service 3880ab
		return -2;
Packit Service 3880ab
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int print_fou_mapping(struct nlmsghdr *n, void *arg)
Packit Service 3880ab
{
Packit Service 3880ab
	__u8 family = AF_INET, local_attr_type, peer_attr_type, byte_len;
Packit Service 3880ab
	struct rtattr *tb[FOU_ATTR_MAX + 1];
Packit Service 3880ab
	__u8 empty_buf[16] = {0};
Packit Service 3880ab
	struct genlmsghdr *ghdr;
Packit Service 3880ab
	int len = n->nlmsg_len;
Packit Service 3880ab
Packit Service 3880ab
	if (n->nlmsg_type != genl_family)
Packit Service 3880ab
		return 0;
Packit Service 3880ab
Packit Service 3880ab
	len -= NLMSG_LENGTH(GENL_HDRLEN);
Packit Service 3880ab
	if (len < 0)
Packit Service 3880ab
		return -1;
Packit Service 3880ab
Packit Service 3880ab
	ghdr = NLMSG_DATA(n);
Packit Service 3880ab
	parse_rtattr(tb, FOU_ATTR_MAX, (void *) ghdr + GENL_HDRLEN, len);
Packit Service 3880ab
Packit Service 3880ab
	open_json_object(NULL);
Packit Service 3880ab
	if (tb[FOU_ATTR_PORT])
Packit Service 3880ab
		print_uint(PRINT_ANY, "port", "port %u",
Packit Service 3880ab
			   ntohs(rta_getattr_u16(tb[FOU_ATTR_PORT])));
Packit Service 3880ab
Packit Service 3880ab
	if (tb[FOU_ATTR_TYPE] &&
Packit Service 3880ab
	    rta_getattr_u8(tb[FOU_ATTR_TYPE]) == FOU_ENCAP_GUE)
Packit Service 3880ab
		print_null(PRINT_ANY, "gue", " gue", NULL);
Packit Service 3880ab
	else if (tb[FOU_ATTR_IPPROTO])
Packit Service 3880ab
		print_uint(PRINT_ANY, "ipproto",
Packit Service 3880ab
			   " ipproto %u", rta_getattr_u8(tb[FOU_ATTR_IPPROTO]));
Packit Service 3880ab
Packit Service 3880ab
	if (tb[FOU_ATTR_AF]) {
Packit Service 3880ab
		family = rta_getattr_u8(tb[FOU_ATTR_AF]);
Packit Service 3880ab
Packit Service 3880ab
		print_string(PRINT_JSON, "family", NULL,
Packit Service 3880ab
			     family_name(family));
Packit Service 3880ab
Packit Service 3880ab
		if (family == AF_INET6)
Packit Service 3880ab
			print_string(PRINT_FP, NULL,
Packit Service 3880ab
				     " -6", NULL);
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	local_attr_type = family == AF_INET ? FOU_ATTR_LOCAL_V4 :
Packit Service 3880ab
					      FOU_ATTR_LOCAL_V6;
Packit Service 3880ab
	peer_attr_type = family == AF_INET ? FOU_ATTR_PEER_V4 :
Packit Service 3880ab
					     FOU_ATTR_PEER_V6;
Packit Service 3880ab
	byte_len = af_bit_len(family) / 8;
Packit Service 3880ab
Packit Service 3880ab
	if (tb[local_attr_type] && memcmp(RTA_DATA(tb[local_attr_type]),
Packit Service 3880ab
					  empty_buf, byte_len)) {
Packit Service 3880ab
		print_string(PRINT_ANY, "local", " local %s",
Packit Service 3880ab
			     format_host_rta(family, tb[local_attr_type]));
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (tb[peer_attr_type] && memcmp(RTA_DATA(tb[peer_attr_type]),
Packit Service 3880ab
					 empty_buf, byte_len)) {
Packit Service 3880ab
		print_string(PRINT_ANY, "peer", " peer %s",
Packit Service 3880ab
			     format_host_rta(family, tb[peer_attr_type]));
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (tb[FOU_ATTR_PEER_PORT]) {
Packit Service 3880ab
		__u16 p_port = ntohs(rta_getattr_u16(tb[FOU_ATTR_PEER_PORT]));
Packit Service 3880ab
Packit Service 3880ab
		if (p_port)
Packit Service 3880ab
			print_uint(PRINT_ANY, "peer_port", " peer_port %u",
Packit Service 3880ab
				   p_port);
Packit Service 3880ab
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (tb[FOU_ATTR_IFINDEX]) {
Packit Service 3880ab
		int index = rta_getattr_s32(tb[FOU_ATTR_IFINDEX]);
Packit Service 3880ab
Packit Service 3880ab
		if (index) {
Packit Service 3880ab
			const char *ifname;
Packit Service 3880ab
Packit Service 3880ab
			ifname = ll_index_to_name(index);
Packit Service 3880ab
Packit Service 3880ab
			if (ifname)
Packit Service 3880ab
				print_string(PRINT_ANY, "dev", " dev %s",
Packit Service 3880ab
					     ifname);
Packit Service 3880ab
		}
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	print_string(PRINT_FP, NULL, "\n", NULL);
Packit Service 3880ab
	close_json_object();
Packit Service 3880ab
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int do_show(int argc, char **argv)
Packit Service 3880ab
{
Packit Service 3880ab
	FOU_REQUEST(req, 4096, FOU_CMD_GET, NLM_F_REQUEST | NLM_F_DUMP);
Packit Service 3880ab
Packit Service 3880ab
	if (argc > 0) {
Packit Service 3880ab
		fprintf(stderr,
Packit Service 3880ab
			"\"ip fou show\" does not take any arguments.\n");
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (rtnl_send(&genl_rth, &req.n, req.n.nlmsg_len) < 0) {
Packit Service 3880ab
		perror("Cannot send show request");
Packit Service 3880ab
		exit(1);
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	new_json_obj(json);
Packit Service 3880ab
	if (rtnl_dump_filter(&genl_rth, print_fou_mapping, stdout) < 0) {
Packit Service 3880ab
		fprintf(stderr, "Dump terminated\n");
Packit Service 3880ab
		return 1;
Packit Service 3880ab
	}
Packit Service 3880ab
	delete_json_obj();
Packit Service 3880ab
	fflush(stdout);
Packit Service 3880ab
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
int do_ipfou(int argc, char **argv)
Packit Service 3880ab
{
Packit Service 3880ab
	if (argc < 1)
Packit Service 3880ab
		usage();
Packit Service 3880ab
Packit Service 3880ab
	if (matches(*argv, "help") == 0)
Packit Service 3880ab
		usage();
Packit Service 3880ab
Packit Service 3880ab
	if (genl_init_handle(&genl_rth, FOU_GENL_NAME, &genl_family))
Packit Service 3880ab
		exit(1);
Packit Service 3880ab
Packit Service 3880ab
	if (matches(*argv, "add") == 0)
Packit Service 3880ab
		return do_add(argc-1, argv+1);
Packit Service 3880ab
	if (matches(*argv, "delete") == 0)
Packit Service 3880ab
		return do_del(argc-1, argv+1);
Packit Service 3880ab
	if (matches(*argv, "show") == 0)
Packit Service 3880ab
		return do_show(argc-1, argv+1);
Packit Service 3880ab
Packit Service 3880ab
	fprintf(stderr,
Packit Service 3880ab
		"Command \"%s\" is unknown, try \"ip fou help\".\n", *argv);
Packit Service 3880ab
	exit(-1);
Packit Service 3880ab
}