Sapan says vnet_tun is obsolete.
[iptables.git] / extensions / libxt_SECMARK.c
1 /*
2  * Shared library add-on to iptables to add SECMARK target support.
3  *
4  * Based on the MARK target.
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>
12 #include <xtables.h>
13 #include <linux/netfilter/xt_SECMARK.h>
14
15 #define PFX "SECMARK target: "
16
17 static void SECMARK_help(void)
18 {
19         printf(
20 "SECMARK target options:\n"
21 "  --selctx value                     Set the SELinux security context\n");
22 }
23
24 static const struct option SECMARK_opts[] = {
25         { "selctx", 1, NULL, '1' },
26         { .name = NULL }
27 };
28
29 /*
30  * Function which parses command options; returns true if it
31  * ate an option.
32  */
33 static int SECMARK_parse(int c, char **argv, int invert, unsigned int *flags,
34                          const void *entry, struct xt_entry_target **target)
35 {
36         struct xt_secmark_target_info *info =
37                 (struct xt_secmark_target_info*)(*target)->data;
38
39         switch (c) {
40         case '1':
41                 if (*flags & SECMARK_MODE_SEL)
42                         exit_error(PARAMETER_PROBLEM, PFX
43                                    "Can't specify --selctx twice");
44                 info->mode = SECMARK_MODE_SEL;
45
46                 if (strlen(optarg) > SECMARK_SELCTX_MAX-1)
47                         exit_error(PARAMETER_PROBLEM, PFX
48                                    "Maximum length %u exceeded by --selctx"
49                                    " parameter (%zu)",
50                                    SECMARK_SELCTX_MAX-1, strlen(optarg));
51
52                 strcpy(info->u.sel.selctx, optarg);
53                 *flags |= SECMARK_MODE_SEL;
54                 break;
55         default:
56                 return 0;
57         }
58
59         return 1;
60 }
61
62 static void SECMARK_check(unsigned int flags)
63 {
64         if (!flags)
65                 exit_error(PARAMETER_PROBLEM, PFX "parameter required");
66 }
67
68 static void print_secmark(struct xt_secmark_target_info *info)
69 {
70         switch (info->mode) {
71         case SECMARK_MODE_SEL:
72                 printf("selctx %s ", info->u.sel.selctx);\
73                 break;
74         
75         default:
76                 exit_error(OTHER_PROBLEM, PFX "invalid mode %hhu\n", info->mode);
77         }
78 }
79
80 static void SECMARK_print(const void *ip, const struct xt_entry_target *target,
81                           int numeric)
82 {
83         struct xt_secmark_target_info *info =
84                 (struct xt_secmark_target_info*)(target)->data;
85
86         printf("SECMARK ");
87         print_secmark(info);
88 }
89
90 /* Saves the target info in parsable form to stdout. */
91 static void SECMARK_save(const void *ip, const struct xt_entry_target *target)
92 {
93         struct xt_secmark_target_info *info =
94                 (struct xt_secmark_target_info*)target->data;
95
96         printf("--");
97         print_secmark(info);
98 }
99
100 static struct xtables_target secmark_target = {
101         .family         = AF_UNSPEC,
102         .name           = "SECMARK",
103         .version        = XTABLES_VERSION,
104         .revision       = 0,
105         .size           = XT_ALIGN(sizeof(struct xt_secmark_target_info)),
106         .userspacesize  = XT_ALIGN(sizeof(struct xt_secmark_target_info)),
107         .help           = SECMARK_help,
108         .parse          = SECMARK_parse,
109         .final_check    = SECMARK_check,
110         .print          = SECMARK_print,
111         .save           = SECMARK_save,
112         .extra_opts     = SECMARK_opts,
113 };
114
115 void _init(void)
116 {
117         xtables_register_target(&secmark_target);
118 }