blob: 05dbf70df53acd5498a30262d43837e8de08e9e5 [file] [log] [blame]
Patrick McHardyff968302006-05-24 16:15:03 +00001/*
2 * Shared library add-on to iptables to add CONNSECMARK target support.
3 *
4 * Based on the MARK and CONNMARK targets.
5 *
6 * Copyright (C) 2006 Red Hat, Inc., James Morris <jmorris@redhat.com>
7 */
8#include <stdio.h>
9#include <string.h>
10#include <stdlib.h>
11#include <getopt.h>
Yasuyuki KOZAKAI56799582007-08-04 08:05:46 +000012#include <xtables.h>
Patrick McHardyff968302006-05-24 16:15:03 +000013#include <linux/netfilter/xt_CONNSECMARK.h>
14
15#define PFX "CONNSECMARK target: "
16
Jan Engelhardt932e6482007-10-04 16:27:30 +000017static void CONNSECMARK_help(void)
Patrick McHardyff968302006-05-24 16:15:03 +000018{
19 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020020"CONNSECMARK target options:\n"
Patrick McHardyff968302006-05-24 16:15:03 +000021" --save Copy security mark from packet to conntrack\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020022" --restore Copy security mark from connection to packet\n");
Patrick McHardyff968302006-05-24 16:15:03 +000023}
24
Jan Engelhardt932e6482007-10-04 16:27:30 +000025static const struct option CONNSECMARK_opts[] = {
Patrick McHardy0ea82bc2008-06-07 15:15:29 +020026 { "save", 0, NULL, '1' },
27 { "restore", 0, NULL, '2' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000028 { .name = NULL }
Patrick McHardyff968302006-05-24 16:15:03 +000029};
30
Jan Engelhardt932e6482007-10-04 16:27:30 +000031static int
32CONNSECMARK_parse(int c, char **argv, int invert, unsigned int *flags,
33 const void *entry, struct xt_entry_target **target)
Patrick McHardyff968302006-05-24 16:15:03 +000034{
35 struct xt_connsecmark_target_info *info =
36 (struct xt_connsecmark_target_info*)(*target)->data;
37
38 switch (c) {
39 case '1':
40 if (*flags & CONNSECMARK_SAVE)
41 exit_error(PARAMETER_PROBLEM, PFX
42 "Can't specify --save twice");
43 info->mode = CONNSECMARK_SAVE;
44 *flags |= CONNSECMARK_SAVE;
45 break;
46
47 case '2':
48 if (*flags & CONNSECMARK_RESTORE)
49 exit_error(PARAMETER_PROBLEM, PFX
50 "Can't specify --restore twice");
51 info->mode = CONNSECMARK_RESTORE;
52 *flags |= CONNSECMARK_RESTORE;
53 break;
54
55 default:
56 return 0;
57 }
58
59 return 1;
60}
61
Jan Engelhardt932e6482007-10-04 16:27:30 +000062static void CONNSECMARK_check(unsigned int flags)
Patrick McHardyff968302006-05-24 16:15:03 +000063{
64 if (!flags)
65 exit_error(PARAMETER_PROBLEM, PFX "parameter required");
66
67 if (flags == (CONNSECMARK_SAVE|CONNSECMARK_RESTORE))
68 exit_error(PARAMETER_PROBLEM, PFX "only one flag of --save "
69 "or --restore is allowed");
70}
71
72static void print_connsecmark(struct xt_connsecmark_target_info *info)
73{
74 switch (info->mode) {
75 case CONNSECMARK_SAVE:
76 printf("save ");
77 break;
78
79 case CONNSECMARK_RESTORE:
80 printf("restore ");
81 break;
82
83 default:
84 exit_error(OTHER_PROBLEM, PFX "invalid mode %hhu\n", info->mode);
85 }
86}
87
Jan Engelhardt932e6482007-10-04 16:27:30 +000088static void
89CONNSECMARK_print(const void *ip, const struct xt_entry_target *target,
90 int numeric)
Patrick McHardyff968302006-05-24 16:15:03 +000091{
92 struct xt_connsecmark_target_info *info =
93 (struct xt_connsecmark_target_info*)(target)->data;
94
95 printf("CONNSECMARK ");
96 print_connsecmark(info);
97}
98
Jan Engelhardt932e6482007-10-04 16:27:30 +000099static void
100CONNSECMARK_save(const void *ip, const struct xt_entry_target *target)
Patrick McHardyff968302006-05-24 16:15:03 +0000101{
102 struct xt_connsecmark_target_info *info =
103 (struct xt_connsecmark_target_info*)target->data;
104
105 printf("--");
106 print_connsecmark(info);
107}
108
Jan Engelhardt932e6482007-10-04 16:27:30 +0000109static struct xtables_target connsecmark_target = {
Jan Engelhardt03d99482008-11-18 12:27:54 +0100110 .family = NFPROTO_IPV4,
Patrick McHardyff968302006-05-24 16:15:03 +0000111 .name = "CONNSECMARK",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200112 .version = XTABLES_VERSION,
Patrick McHardyff968302006-05-24 16:15:03 +0000113 .revision = 0,
Yasuyuki KOZAKAI56799582007-08-04 08:05:46 +0000114 .size = XT_ALIGN(sizeof(struct xt_connsecmark_target_info)),
115 .userspacesize = XT_ALIGN(sizeof(struct xt_connsecmark_target_info)),
Jan Engelhardt932e6482007-10-04 16:27:30 +0000116 .parse = CONNSECMARK_parse,
117 .help = CONNSECMARK_help,
118 .final_check = CONNSECMARK_check,
119 .print = CONNSECMARK_print,
120 .save = CONNSECMARK_save,
121 .extra_opts = CONNSECMARK_opts,
Yasuyuki KOZAKAI56799582007-08-04 08:05:46 +0000122};
123
Jan Engelhardt932e6482007-10-04 16:27:30 +0000124static struct xtables_target connsecmark_target6 = {
Jan Engelhardt03d99482008-11-18 12:27:54 +0100125 .family = NFPROTO_IPV6,
Yasuyuki KOZAKAI56799582007-08-04 08:05:46 +0000126 .name = "CONNSECMARK",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200127 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI56799582007-08-04 08:05:46 +0000128 .revision = 0,
129 .size = XT_ALIGN(sizeof(struct xt_connsecmark_target_info)),
130 .userspacesize = XT_ALIGN(sizeof(struct xt_connsecmark_target_info)),
Jan Engelhardt932e6482007-10-04 16:27:30 +0000131 .parse = CONNSECMARK_parse,
132 .help = CONNSECMARK_help,
133 .final_check = CONNSECMARK_check,
134 .print = CONNSECMARK_print,
135 .save = CONNSECMARK_save,
136 .extra_opts = CONNSECMARK_opts,
Patrick McHardyff968302006-05-24 16:15:03 +0000137};
138
139void _init(void)
140{
Jan Engelhardt932e6482007-10-04 16:27:30 +0000141 xtables_register_target(&connsecmark_target);
142 xtables_register_target(&connsecmark_target6);
Patrick McHardyff968302006-05-24 16:15:03 +0000143}