Blame tc/f_fw.c

Packit Service 3880ab
/*
Packit Service 3880ab
 * f_fw.c		FW filter.
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:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Packit Service 3880ab
 *
Packit Service 3880ab
 */
Packit Service 3880ab
Packit Service 3880ab
#include <stdio.h>
Packit Service 3880ab
#include <stdlib.h>
Packit Service 3880ab
#include <unistd.h>
Packit Service 3880ab
#include <fcntl.h>
Packit Service 3880ab
#include <sys/socket.h>
Packit Service 3880ab
#include <netinet/in.h>
Packit Service 3880ab
#include <arpa/inet.h>
Packit Service 3880ab
#include <string.h>
Packit Service 3880ab
#include <linux/if.h> /* IFNAMSIZ */
Packit Service 3880ab
#include "utils.h"
Packit Service 3880ab
#include "tc_util.h"
Packit Service 3880ab
Packit Service 3880ab
static void explain(void)
Packit Service 3880ab
{
Packit Service 3880ab
	fprintf(stderr,
Packit Service 3880ab
		"Usage: ... fw [ classid CLASSID ] [ indev DEV ] [ action ACTION_SPEC ]\n"
Packit Service 3880ab
		"	CLASSID := Push matching packets to the class identified by CLASSID with format X:Y\n"
Packit Service 3880ab
		"		CLASSID is parsed as hexadecimal input.\n"
Packit Service 3880ab
		"	DEV := specify device for incoming device classification.\n"
Packit Service 3880ab
		"	ACTION_SPEC := Apply an action on matching packets.\n"
Packit Service 3880ab
		"	NOTE: handle is represented as HANDLE[/FWMASK].\n"
Packit Service 3880ab
		"		FWMASK is 0xffffffff by default.\n");
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int fw_parse_opt(struct filter_util *qu, char *handle, int argc, char **argv, struct nlmsghdr *n)
Packit Service 3880ab
{
Packit Service 3880ab
	struct tcmsg *t = NLMSG_DATA(n);
Packit Service 3880ab
	struct rtattr *tail;
Packit Service 3880ab
	__u32 mask = 0;
Packit Service 3880ab
	int mask_set = 0;
Packit Service 3880ab
Packit Service 3880ab
	if (handle) {
Packit Service 3880ab
		char *slash;
Packit Service 3880ab
Packit Service 3880ab
		if ((slash = strchr(handle, '/')) != NULL)
Packit Service 3880ab
			*slash = '\0';
Packit Service 3880ab
		if (get_u32(&t->tcm_handle, handle, 0)) {
Packit Service 3880ab
			fprintf(stderr, "Illegal \"handle\"\n");
Packit Service 3880ab
			return -1;
Packit Service 3880ab
		}
Packit Service 3880ab
		if (slash) {
Packit Service 3880ab
			if (get_u32(&mask, slash+1, 0)) {
Packit Service 3880ab
				fprintf(stderr, "Illegal \"handle\" mask\n");
Packit Service 3880ab
				return -1;
Packit Service 3880ab
			}
Packit Service 3880ab
			mask_set = 1;
Packit Service 3880ab
		}
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (argc == 0)
Packit Service 3880ab
		return 0;
Packit Service 3880ab
Packit Service 3880ab
	tail = addattr_nest(n, 4096, TCA_OPTIONS);
Packit Service 3880ab
Packit Service 3880ab
	if (mask_set)
Packit Service 3880ab
		addattr32(n, MAX_MSG, TCA_FW_MASK, mask);
Packit Service 3880ab
Packit Service 3880ab
	while (argc > 0) {
Packit Service 3880ab
		if (matches(*argv, "classid") == 0 ||
Packit Service 3880ab
		    matches(*argv, "flowid") == 0) {
Packit Service 3880ab
			unsigned int handle;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_tc_classid(&handle, *argv)) {
Packit Service 3880ab
				fprintf(stderr, "Illegal \"classid\"\n");
Packit Service 3880ab
				return -1;
Packit Service 3880ab
			}
Packit Service 3880ab
			addattr_l(n, 4096, TCA_FW_CLASSID, &handle, 4);
Packit Service 3880ab
		} else if (matches(*argv, "police") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (parse_police(&argc, &argv, TCA_FW_POLICE, n)) {
Packit Service 3880ab
				fprintf(stderr, "Illegal \"police\"\n");
Packit Service 3880ab
				return -1;
Packit Service 3880ab
			}
Packit Service 3880ab
			continue;
Packit Service 3880ab
		} else if (matches(*argv, "action") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (parse_action(&argc, &argv, TCA_FW_ACT, n)) {
Packit Service 3880ab
				fprintf(stderr, "Illegal fw \"action\"\n");
Packit Service 3880ab
				return -1;
Packit Service 3880ab
			}
Packit Service 3880ab
			continue;
Packit Service 3880ab
		} else if (strcmp(*argv, "indev") == 0) {
Packit Service 3880ab
			char d[IFNAMSIZ+1] = {};
Packit Service 3880ab
Packit Service 3880ab
			argc--;
Packit Service 3880ab
			argv++;
Packit Service 3880ab
			if (argc < 1) {
Packit Service 3880ab
				fprintf(stderr, "Illegal indev\n");
Packit Service 3880ab
				return -1;
Packit Service 3880ab
			}
Packit Service 3880ab
			strncpy(d, *argv, sizeof(d) - 1);
Packit Service 3880ab
			addattr_l(n, MAX_MSG, TCA_FW_INDEV, d, strlen(d) + 1);
Packit Service 3880ab
		} else if (strcmp(*argv, "help") == 0) {
Packit Service 3880ab
			explain();
Packit Service 3880ab
			return -1;
Packit Service 3880ab
		} else {
Packit Service 3880ab
			fprintf(stderr, "What is \"%s\"?\n", *argv);
Packit Service 3880ab
			explain();
Packit Service 3880ab
			return -1;
Packit Service 3880ab
		}
Packit Service 3880ab
		argc--; argv++;
Packit Service 3880ab
	}
Packit Service 3880ab
	addattr_nest_end(n, tail);
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int fw_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle)
Packit Service 3880ab
{
Packit Service 3880ab
	struct rtattr *tb[TCA_FW_MAX+1];
Packit Service 3880ab
Packit Service 3880ab
	if (opt == NULL)
Packit Service 3880ab
		return 0;
Packit Service 3880ab
Packit Service 3880ab
	parse_rtattr_nested(tb, TCA_FW_MAX, opt);
Packit Service 3880ab
Packit Service 3880ab
	if (handle || tb[TCA_FW_MASK]) {
Packit Service 3880ab
		__u32 mark = 0, mask = 0;
Packit Service 3880ab
Packit Service 3880ab
		if (handle)
Packit Service 3880ab
			mark = handle;
Packit Service 3880ab
		if (tb[TCA_FW_MASK] &&
Packit Service 3880ab
		    (mask = rta_getattr_u32(tb[TCA_FW_MASK])) != 0xFFFFFFFF)
Packit Service 3880ab
			fprintf(f, "handle 0x%x/0x%x ", mark, mask);
Packit Service 3880ab
		else
Packit Service 3880ab
			fprintf(f, "handle 0x%x ", handle);
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (tb[TCA_FW_CLASSID]) {
Packit Service 3880ab
		SPRINT_BUF(b1);
Packit Service 3880ab
		fprintf(f, "classid %s ", sprint_tc_classid(rta_getattr_u32(tb[TCA_FW_CLASSID]), b1));
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (tb[TCA_FW_POLICE])
Packit Service 3880ab
		tc_print_police(f, tb[TCA_FW_POLICE]);
Packit Service 3880ab
	if (tb[TCA_FW_INDEV]) {
Packit Service 3880ab
		struct rtattr *idev = tb[TCA_FW_INDEV];
Packit Service 3880ab
Packit Service 3880ab
		fprintf(f, "input dev %s ", rta_getattr_str(idev));
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (tb[TCA_FW_ACT]) {
Packit Service 3880ab
		fprintf(f, "\n");
Packit Service 3880ab
		tc_print_action(f, tb[TCA_FW_ACT], 0);
Packit Service 3880ab
	}
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
struct filter_util fw_filter_util = {
Packit Service 3880ab
	.id = "fw",
Packit Service 3880ab
	.parse_fopt = fw_parse_opt,
Packit Service 3880ab
	.print_fopt = fw_print_opt,
Packit Service 3880ab
};