Blame tc/em_nbyte.c

Packit Service 3880ab
/*
Packit Service 3880ab
 * em_nbyte.c		N-Byte Ematch
Packit Service 3880ab
 *
Packit Service 3880ab
 *		This program is free software; you can distribute 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:	Thomas Graf <tgraf@suug.ch>
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 <errno.h>
Packit Service 3880ab
Packit Service 3880ab
#include "m_ematch.h"
Packit Service 3880ab
#include <linux/tc_ematch/tc_em_nbyte.h>
Packit Service 3880ab
Packit Service 3880ab
extern struct ematch_util nbyte_ematch_util;
Packit Service 3880ab
Packit Service 3880ab
static void nbyte_print_usage(FILE *fd)
Packit Service 3880ab
{
Packit Service 3880ab
	fprintf(fd,
Packit Service 3880ab
	    "Usage: nbyte(NEEDLE at OFFSET [layer LAYER])\n" \
Packit Service 3880ab
	    "where: NEEDLE := { string | \"c-escape-sequence\" }\n" \
Packit Service 3880ab
	    "       OFFSET := int\n" \
Packit Service 3880ab
	    "       LAYER  := { link | network | transport | 0..%d }\n" \
Packit Service 3880ab
	    "\n" \
Packit Service 3880ab
	    "Example: nbyte(\"ababa\" at 12 layer 1)\n",
Packit Service 3880ab
	    TCF_LAYER_MAX);
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int nbyte_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
Packit Service 3880ab
			    struct bstr *args)
Packit Service 3880ab
{
Packit Service 3880ab
	struct bstr *a;
Packit Service 3880ab
	struct bstr *needle = args;
Packit Service 3880ab
	unsigned long offset = 0, layer = TCF_LAYER_NETWORK;
Packit Service 3880ab
	int offset_present = 0;
Packit Service 3880ab
	struct tcf_em_nbyte nb = {};
Packit Service 3880ab
Packit Service 3880ab
#define PARSE_ERR(CARG, FMT, ARGS...) \
Packit Service 3880ab
	em_parse_error(EINVAL, args, CARG, &nbyte_ematch_util, FMT, ##ARGS)
Packit Service 3880ab
Packit Service 3880ab
	if (args == NULL)
Packit Service 3880ab
		return PARSE_ERR(args, "nbyte: missing arguments");
Packit Service 3880ab
Packit Service 3880ab
	if (needle->len <= 0)
Packit Service 3880ab
		return PARSE_ERR(args, "nbyte: needle length is 0");
Packit Service 3880ab
Packit Service 3880ab
	for (a = bstr_next(args); a; a = bstr_next(a)) {
Packit Service 3880ab
		if (!bstrcmp(a, "at")) {
Packit Service 3880ab
			if (a->next == NULL)
Packit Service 3880ab
				return PARSE_ERR(a, "nbyte: missing argument");
Packit Service 3880ab
			a = bstr_next(a);
Packit Service 3880ab
Packit Service 3880ab
			offset = bstrtoul(a);
Packit Service 3880ab
			if (offset == ULONG_MAX)
Packit Service 3880ab
				return PARSE_ERR(a, "nbyte: invalid offset, " \
Packit Service 3880ab
				    "must be numeric");
Packit Service 3880ab
Packit Service 3880ab
			offset_present = 1;
Packit Service 3880ab
		} else if (!bstrcmp(a, "layer")) {
Packit Service 3880ab
			if (a->next == NULL)
Packit Service 3880ab
				return PARSE_ERR(a, "nbyte: missing argument");
Packit Service 3880ab
			a = bstr_next(a);
Packit Service 3880ab
Packit Service 3880ab
			layer = parse_layer(a);
Packit Service 3880ab
			if (layer == INT_MAX) {
Packit Service 3880ab
				layer = bstrtoul(a);
Packit Service 3880ab
				if (layer == ULONG_MAX)
Packit Service 3880ab
					return PARSE_ERR(a, "nbyte: invalid " \
Packit Service 3880ab
					    "layer");
Packit Service 3880ab
			}
Packit Service 3880ab
Packit Service 3880ab
			if (layer > TCF_LAYER_MAX)
Packit Service 3880ab
				return PARSE_ERR(a, "nbyte: illegal layer, " \
Packit Service 3880ab
				    "must be in 0..%d", TCF_LAYER_MAX);
Packit Service 3880ab
		} else
Packit Service 3880ab
			return PARSE_ERR(a, "nbyte: unknown parameter");
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (offset_present == 0)
Packit Service 3880ab
		return PARSE_ERR(a, "nbyte: offset required");
Packit Service 3880ab
Packit Service 3880ab
	nb.len = needle->len;
Packit Service 3880ab
	nb.layer = (__u8) layer;
Packit Service 3880ab
	nb.off = (__u16) offset;
Packit Service 3880ab
Packit Service 3880ab
	addraw_l(n, MAX_MSG, hdr, sizeof(*hdr));
Packit Service 3880ab
	addraw_l(n, MAX_MSG, &nb, sizeof(nb));
Packit Service 3880ab
	addraw_l(n, MAX_MSG, needle->data, needle->len);
Packit Service 3880ab
Packit Service 3880ab
#undef PARSE_ERR
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int nbyte_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
Packit Service 3880ab
			    int data_len)
Packit Service 3880ab
{
Packit Service 3880ab
	int i;
Packit Service 3880ab
	struct tcf_em_nbyte *nb = data;
Packit Service 3880ab
	__u8 *needle;
Packit Service 3880ab
Packit Service 3880ab
	if (data_len < sizeof(*nb)) {
Packit Service 3880ab
		fprintf(stderr, "NByte header size mismatch\n");
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (data_len < sizeof(*nb) + nb->len) {
Packit Service 3880ab
		fprintf(stderr, "NByte payload size mismatch\n");
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	needle = data + sizeof(*nb);
Packit Service 3880ab
Packit Service 3880ab
	for (i = 0; i < nb->len; i++)
Packit Service 3880ab
		fprintf(fd, "%02x ", needle[i]);
Packit Service 3880ab
Packit Service 3880ab
	fprintf(fd, "\"");
Packit Service 3880ab
	for (i = 0; i < nb->len; i++)
Packit Service 3880ab
		fprintf(fd, "%c", isprint(needle[i]) ? needle[i] : '.');
Packit Service 3880ab
	fprintf(fd, "\" at %d layer %d", nb->off, nb->layer);
Packit Service 3880ab
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
struct ematch_util nbyte_ematch_util = {
Packit Service 3880ab
	.kind = "nbyte",
Packit Service 3880ab
	.kind_num = TCF_EM_NBYTE,
Packit Service 3880ab
	.parse_eopt = nbyte_parse_eopt,
Packit Service 3880ab
	.print_eopt = nbyte_print_eopt,
Packit Service 3880ab
	.print_usage = nbyte_print_usage
Packit Service 3880ab
};