libxt_CHECKSUM.c revision 73866357e4a7a0fdc1b293bf8863fee2bd56da9e
1/* Shared library add-on to xtables for CHECKSUM
2 *
3 * (C) 2002 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010 by Red Hat, Inc
5 * Author: Michael S. Tsirkin <mst@redhat.com>
6 *
7 * This program is distributed under the terms of GNU GPL v2, 1991
8 *
9 * libxt_CHECKSUM.c borrowed some bits from libipt_ECN.c
10 */
11#include <stdbool.h>
12#include <stdio.h>
13#include <string.h>
14#include <stdlib.h>
15#include <getopt.h>
16
17#include <xtables.h>
18#include <linux/netfilter/xt_CHECKSUM.h>
19
20static void CHECKSUM_help(void)
21{
22	printf(
23"CHECKSUM target options\n"
24"  --checksum-fill			Fill in packet checksum.\n");
25}
26
27static const struct option CHECKSUM_opts[] = {
28	{.name = "checksum-fill", .has_arg = false, .val = 'F'},
29	XT_GETOPT_TABLEEND,
30};
31
32static int CHECKSUM_parse(int c, char **argv, int invert, unsigned int *flags,
33                     const void *entry, struct xt_entry_target **target)
34{
35	struct xt_CHECKSUM_info *einfo
36		= (struct xt_CHECKSUM_info *)(*target)->data;
37
38	switch (c) {
39	case 'F':
40		xtables_param_act(XTF_ONLY_ONCE, "CHECKSUM", "--checksum-fill",
41			*flags & XT_CHECKSUM_OP_FILL);
42		einfo->operation = XT_CHECKSUM_OP_FILL;
43		*flags |= XT_CHECKSUM_OP_FILL;
44		break;
45	}
46
47	return 1;
48}
49
50static void CHECKSUM_check(unsigned int flags)
51{
52	if (!flags)
53		xtables_error(PARAMETER_PROBLEM,
54		           "CHECKSUM target: Parameter --checksum-fill is required");
55}
56
57static void CHECKSUM_print(const void *ip, const struct xt_entry_target *target,
58                      int numeric)
59{
60	const struct xt_CHECKSUM_info *einfo =
61		(const struct xt_CHECKSUM_info *)target->data;
62
63	printf(" CHECKSUM");
64
65	if (einfo->operation & XT_CHECKSUM_OP_FILL)
66		printf(" fill");
67}
68
69static void CHECKSUM_save(const void *ip, const struct xt_entry_target *target)
70{
71	const struct xt_CHECKSUM_info *einfo =
72		(const struct xt_CHECKSUM_info *)target->data;
73
74	if (einfo->operation & XT_CHECKSUM_OP_FILL)
75		printf(" --checksum-fill");
76}
77
78static struct xtables_target checksum_tg_reg = {
79	.name		= "CHECKSUM",
80	.version	= XTABLES_VERSION,
81	.family		= NFPROTO_UNSPEC,
82	.size		= XT_ALIGN(sizeof(struct xt_CHECKSUM_info)),
83	.userspacesize	= XT_ALIGN(sizeof(struct xt_CHECKSUM_info)),
84	.help		= CHECKSUM_help,
85	.parse		= CHECKSUM_parse,
86	.final_check	= CHECKSUM_check,
87	.print		= CHECKSUM_print,
88	.save		= CHECKSUM_save,
89	.extra_opts	= CHECKSUM_opts,
90};
91
92void _init(void)
93{
94	xtables_register_target(&checksum_tg_reg);
95}
96