Blame tc/em_cmp.c

Packit d3f73b
/*
Packit d3f73b
 * em_cmp.c		Simple comparison Ematch
Packit d3f73b
 *
Packit d3f73b
 *		This program is free software; you can distribute it and/or
Packit d3f73b
 *		modify it under the terms of the GNU General Public License
Packit d3f73b
 *		as published by the Free Software Foundation; either version
Packit d3f73b
 *		2 of the License, or (at your option) any later version.
Packit d3f73b
 *
Packit d3f73b
 * Authors:	Thomas Graf <tgraf@suug.ch>
Packit d3f73b
 */
Packit d3f73b
Packit d3f73b
#include <stdio.h>
Packit d3f73b
#include <stdlib.h>
Packit d3f73b
#include <unistd.h>
Packit d3f73b
#include <fcntl.h>
Packit d3f73b
#include <sys/socket.h>
Packit d3f73b
#include <netinet/in.h>
Packit d3f73b
#include <arpa/inet.h>
Packit d3f73b
#include <string.h>
Packit d3f73b
#include <errno.h>
Packit d3f73b
Packit d3f73b
#include "m_ematch.h"
Packit d3f73b
#include <linux/tc_ematch/tc_em_cmp.h>
Packit d3f73b
Packit d3f73b
extern struct ematch_util cmp_ematch_util;
Packit d3f73b
Packit d3f73b
static void cmp_print_usage(FILE *fd)
Packit d3f73b
{
Packit d3f73b
	fprintf(fd,
Packit d3f73b
	    "Usage: cmp(ALIGN at OFFSET [ ATTRS ] { eq | lt | gt } VALUE)\n" \
Packit d3f73b
	    "where: ALIGN  := { u8 | u16 | u32 }\n" \
Packit d3f73b
	    "       ATTRS  := [ layer LAYER ] [ mask MASK ] [ trans ]\n" \
Packit d3f73b
	    "       LAYER  := { link | network | transport | 0..%d }\n" \
Packit d3f73b
	    "\n" \
Packit d3f73b
	    "Example: cmp(u16 at 3 layer 2 mask 0xff00 gt 20)\n",
Packit d3f73b
	    TCF_LAYER_MAX);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int cmp_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
Packit d3f73b
			  struct bstr *args)
Packit d3f73b
{
Packit d3f73b
	struct bstr *a;
Packit d3f73b
	int align, opnd = 0;
Packit d3f73b
	unsigned long offset = 0, layer = TCF_LAYER_NETWORK, mask = 0, value = 0;
Packit d3f73b
	int offset_present = 0, value_present = 0;
Packit d3f73b
	struct tcf_em_cmp cmp = {};
Packit d3f73b
Packit d3f73b
#define PARSE_ERR(CARG, FMT, ARGS...) \
Packit d3f73b
	em_parse_error(EINVAL, args, CARG, &cmp_ematch_util, FMT, ##ARGS)
Packit d3f73b
Packit d3f73b
	if (args == NULL)
Packit d3f73b
		return PARSE_ERR(args, "cmp: missing arguments");
Packit d3f73b
Packit d3f73b
	if (!bstrcmp(args, "u8"))
Packit d3f73b
		align = TCF_EM_ALIGN_U8;
Packit d3f73b
	else if (!bstrcmp(args, "u16"))
Packit d3f73b
		align = TCF_EM_ALIGN_U16;
Packit d3f73b
	else if (!bstrcmp(args, "u32"))
Packit d3f73b
		align = TCF_EM_ALIGN_U32;
Packit d3f73b
	else
Packit d3f73b
		return PARSE_ERR(args, "cmp: invalid alignment");
Packit d3f73b
Packit d3f73b
	for (a = bstr_next(args); a; a = bstr_next(a)) {
Packit d3f73b
		if (!bstrcmp(a, "at")) {
Packit d3f73b
			if (a->next == NULL)
Packit d3f73b
				return PARSE_ERR(a, "cmp: missing argument");
Packit d3f73b
			a = bstr_next(a);
Packit d3f73b
Packit d3f73b
			offset = bstrtoul(a);
Packit d3f73b
			if (offset == ULONG_MAX)
Packit d3f73b
				return PARSE_ERR(a, "cmp: invalid offset, " \
Packit d3f73b
				    "must be numeric");
Packit d3f73b
Packit d3f73b
			offset_present = 1;
Packit d3f73b
		} else if (!bstrcmp(a, "layer")) {
Packit d3f73b
			if (a->next == NULL)
Packit d3f73b
				return PARSE_ERR(a, "cmp: missing argument");
Packit d3f73b
			a = bstr_next(a);
Packit d3f73b
Packit d3f73b
			layer = parse_layer(a);
Packit d3f73b
			if (layer == INT_MAX) {
Packit d3f73b
				layer = bstrtoul(a);
Packit d3f73b
				if (layer == ULONG_MAX)
Packit d3f73b
					return PARSE_ERR(a, "cmp: invalid " \
Packit d3f73b
					    "layer");
Packit d3f73b
			}
Packit d3f73b
Packit d3f73b
			if (layer > TCF_LAYER_MAX)
Packit d3f73b
				return PARSE_ERR(a, "cmp: illegal layer, " \
Packit d3f73b
				    "must be in 0..%d", TCF_LAYER_MAX);
Packit d3f73b
		} else if (!bstrcmp(a, "mask")) {
Packit d3f73b
			if (a->next == NULL)
Packit d3f73b
				return PARSE_ERR(a, "cmp: missing argument");
Packit d3f73b
			a = bstr_next(a);
Packit d3f73b
Packit d3f73b
			mask = bstrtoul(a);
Packit d3f73b
			if (mask == ULONG_MAX)
Packit d3f73b
				return PARSE_ERR(a, "cmp: invalid mask");
Packit d3f73b
		} else if (!bstrcmp(a, "trans")) {
Packit d3f73b
			cmp.flags |= TCF_EM_CMP_TRANS;
Packit d3f73b
		} else if (!bstrcmp(a, "eq") || !bstrcmp(a, "gt") ||
Packit d3f73b
		    !bstrcmp(a, "lt")) {
Packit d3f73b
Packit d3f73b
			if (!bstrcmp(a, "eq"))
Packit d3f73b
				opnd = TCF_EM_OPND_EQ;
Packit d3f73b
			else if (!bstrcmp(a, "gt"))
Packit d3f73b
				opnd = TCF_EM_OPND_GT;
Packit d3f73b
			else if (!bstrcmp(a, "lt"))
Packit d3f73b
				opnd = TCF_EM_OPND_LT;
Packit d3f73b
Packit d3f73b
			if (a->next == NULL)
Packit d3f73b
				return PARSE_ERR(a, "cmp: missing argument");
Packit d3f73b
			a = bstr_next(a);
Packit d3f73b
Packit d3f73b
			value = bstrtoul(a);
Packit d3f73b
			if (value == ULONG_MAX)
Packit d3f73b
				return PARSE_ERR(a, "cmp: invalid value");
Packit d3f73b
Packit d3f73b
			value_present = 1;
Packit d3f73b
		} else
Packit d3f73b
			return PARSE_ERR(a, "nbyte: unknown parameter");
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (offset_present == 0 || value_present == 0)
Packit d3f73b
		return PARSE_ERR(a, "cmp: offset and value required");
Packit d3f73b
Packit d3f73b
	cmp.val = (__u32) value;
Packit d3f73b
	cmp.mask = (__u32) mask;
Packit d3f73b
	cmp.off = (__u16) offset;
Packit d3f73b
	cmp.align = (__u8) align;
Packit d3f73b
	cmp.layer = (__u8) layer;
Packit d3f73b
	cmp.opnd = (__u8) opnd;
Packit d3f73b
Packit d3f73b
	addraw_l(n, MAX_MSG, hdr, sizeof(*hdr));
Packit d3f73b
	addraw_l(n, MAX_MSG, &cmp, sizeof(cmp));
Packit d3f73b
Packit d3f73b
#undef PARSE_ERR
Packit d3f73b
	return 0;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int cmp_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
Packit d3f73b
			  int data_len)
Packit d3f73b
{
Packit d3f73b
	struct tcf_em_cmp *cmp = data;
Packit d3f73b
Packit d3f73b
	if (data_len < sizeof(*cmp)) {
Packit d3f73b
		fprintf(stderr, "CMP header size mismatch\n");
Packit d3f73b
		return -1;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	if (cmp->align == TCF_EM_ALIGN_U8)
Packit d3f73b
		fprintf(fd, "u8 ");
Packit d3f73b
	else if (cmp->align == TCF_EM_ALIGN_U16)
Packit d3f73b
		fprintf(fd, "u16 ");
Packit d3f73b
	else if (cmp->align == TCF_EM_ALIGN_U32)
Packit d3f73b
		fprintf(fd, "u32 ");
Packit d3f73b
Packit d3f73b
	fprintf(fd, "at %d layer %d ", cmp->off, cmp->layer);
Packit d3f73b
Packit d3f73b
	if (cmp->mask)
Packit d3f73b
		fprintf(fd, "mask 0x%x ", cmp->mask);
Packit d3f73b
Packit d3f73b
	if (cmp->flags & TCF_EM_CMP_TRANS)
Packit d3f73b
		fprintf(fd, "trans ");
Packit d3f73b
Packit d3f73b
	if (cmp->opnd == TCF_EM_OPND_EQ)
Packit d3f73b
		fprintf(fd, "eq ");
Packit d3f73b
	else if (cmp->opnd == TCF_EM_OPND_LT)
Packit d3f73b
		fprintf(fd, "lt ");
Packit d3f73b
	else if (cmp->opnd == TCF_EM_OPND_GT)
Packit d3f73b
		fprintf(fd, "gt ");
Packit d3f73b
Packit d3f73b
	fprintf(fd, "%d", cmp->val);
Packit d3f73b
Packit d3f73b
	return 0;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
struct ematch_util cmp_ematch_util = {
Packit d3f73b
	.kind = "cmp",
Packit d3f73b
	.kind_num = TCF_EM_CMP,
Packit d3f73b
	.parse_eopt = cmp_parse_eopt,
Packit d3f73b
	.print_eopt = cmp_print_eopt,
Packit d3f73b
	.print_usage = cmp_print_usage
Packit d3f73b
};