libxt_CHECKSUM.c revision 32b8e61e4e5bd405d9ad07bf9468498dfbb19f9e
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	default:
46		return 0;
47	}
48
49	return 1;
50}
51
52static void CHECKSUM_check(unsigned int flags)
53{
54	if (!flags)
55		xtables_error(PARAMETER_PROBLEM,
56		           "CHECKSUM target: Parameter --checksum-fill is required");
57}
58
59static void CHECKSUM_print(const void *ip, const struct xt_entry_target *target,
60                      int numeric)
61{
62	const struct xt_CHECKSUM_info *einfo =
63		(const struct xt_CHECKSUM_info *)target->data;
64
65	printf("CHECKSUM ");
66
67	if (einfo->operation & XT_CHECKSUM_OP_FILL)
68		printf("fill ");
69}
70
71static void CHECKSUM_save(const void *ip, const struct xt_entry_target *target)
72{
73	const struct xt_CHECKSUM_info *einfo =
74		(const struct xt_CHECKSUM_info *)target->data;
75
76	if (einfo->operation & XT_CHECKSUM_OP_FILL)
77		printf("--checksum-fill ");
78}
79
80static struct xtables_target checksum_tg_reg = {
81	.name		= "CHECKSUM",
82	.version	= XTABLES_VERSION,
83	.family		= NFPROTO_UNSPEC,
84	.size		= XT_ALIGN(sizeof(struct xt_CHECKSUM_info)),
85	.userspacesize	= XT_ALIGN(sizeof(struct xt_CHECKSUM_info)),
86	.help		= CHECKSUM_help,
87	.parse		= CHECKSUM_parse,
88	.final_check	= CHECKSUM_check,
89	.print		= CHECKSUM_print,
90	.save		= CHECKSUM_save,
91	.extra_opts	= CHECKSUM_opts,
92};
93
94void _init(void)
95{
96	xtables_register_target(&checksum_tg_reg);
97}
98