blob: c62fb44f5be6ed02a60e11b5351ab55c24c63e83 [file] [log] [blame]
Patrick McHardyff968302006-05-24 16:15:03 +00001/*
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>
Yasuyuki KOZAKAIfa00a732007-07-24 07:27:02 +000012#include <xtables.h>
Patrick McHardyff968302006-05-24 16:15:03 +000013#include <linux/netfilter/xt_SECMARK.h>
14
15#define PFX "SECMARK target: "
16
Jan Engelhardt932e6482007-10-04 16:27:30 +000017static void SECMARK_help(void)
Patrick McHardyff968302006-05-24 16:15:03 +000018{
19 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020020"SECMARK target options:\n"
21" --selctx value Set the SELinux security context\n");
Patrick McHardyff968302006-05-24 16:15:03 +000022}
23
Jan Engelhardt932e6482007-10-04 16:27:30 +000024static const struct option SECMARK_opts[] = {
Patrick McHardy0ea82bc2008-06-07 15:15:29 +020025 { "selctx", 1, NULL, '1' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000026 { .name = NULL }
Patrick McHardyff968302006-05-24 16:15:03 +000027};
28
Jan Engelhardt932e6482007-10-04 16:27:30 +000029static int SECMARK_parse(int c, char **argv, int invert, unsigned int *flags,
30 const void *entry, struct xt_entry_target **target)
Patrick McHardyff968302006-05-24 16:15:03 +000031{
32 struct xt_secmark_target_info *info =
33 (struct xt_secmark_target_info*)(*target)->data;
34
35 switch (c) {
36 case '1':
37 if (*flags & SECMARK_MODE_SEL)
38 exit_error(PARAMETER_PROBLEM, PFX
39 "Can't specify --selctx twice");
40 info->mode = SECMARK_MODE_SEL;
41
42 if (strlen(optarg) > SECMARK_SELCTX_MAX-1)
43 exit_error(PARAMETER_PROBLEM, PFX
44 "Maximum length %u exceeded by --selctx"
45 " parameter (%zu)",
46 SECMARK_SELCTX_MAX-1, strlen(optarg));
47
48 strcpy(info->u.sel.selctx, optarg);
49 *flags |= SECMARK_MODE_SEL;
50 break;
51 default:
52 return 0;
53 }
54
55 return 1;
56}
57
Jan Engelhardt932e6482007-10-04 16:27:30 +000058static void SECMARK_check(unsigned int flags)
Patrick McHardyff968302006-05-24 16:15:03 +000059{
60 if (!flags)
61 exit_error(PARAMETER_PROBLEM, PFX "parameter required");
62}
63
64static void print_secmark(struct xt_secmark_target_info *info)
65{
66 switch (info->mode) {
67 case SECMARK_MODE_SEL:
68 printf("selctx %s ", info->u.sel.selctx);\
69 break;
70
71 default:
72 exit_error(OTHER_PROBLEM, PFX "invalid mode %hhu\n", info->mode);
73 }
74}
75
Jan Engelhardt932e6482007-10-04 16:27:30 +000076static void SECMARK_print(const void *ip, const struct xt_entry_target *target,
77 int numeric)
Patrick McHardyff968302006-05-24 16:15:03 +000078{
79 struct xt_secmark_target_info *info =
80 (struct xt_secmark_target_info*)(target)->data;
81
82 printf("SECMARK ");
83 print_secmark(info);
84}
85
Jan Engelhardt932e6482007-10-04 16:27:30 +000086static void SECMARK_save(const void *ip, const struct xt_entry_target *target)
Patrick McHardyff968302006-05-24 16:15:03 +000087{
88 struct xt_secmark_target_info *info =
89 (struct xt_secmark_target_info*)target->data;
90
91 printf("--");
92 print_secmark(info);
93}
94
Jan Engelhardt932e6482007-10-04 16:27:30 +000095static struct xtables_target secmark_target = {
Jan Engelhardt23545c22008-02-14 04:23:04 +010096 .family = AF_UNSPEC,
Yasuyuki KOZAKAIfa00a732007-07-24 07:27:02 +000097 .name = "SECMARK",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020098 .version = XTABLES_VERSION,
Yasuyuki KOZAKAIfa00a732007-07-24 07:27:02 +000099 .revision = 0,
100 .size = XT_ALIGN(sizeof(struct xt_secmark_target_info)),
101 .userspacesize = XT_ALIGN(sizeof(struct xt_secmark_target_info)),
Jan Engelhardt932e6482007-10-04 16:27:30 +0000102 .help = SECMARK_help,
103 .parse = SECMARK_parse,
104 .final_check = SECMARK_check,
105 .print = SECMARK_print,
106 .save = SECMARK_save,
107 .extra_opts = SECMARK_opts,
Patrick McHardyff968302006-05-24 16:15:03 +0000108};
109
110void _init(void)
111{
Jan Engelhardt932e6482007-10-04 16:27:30 +0000112 xtables_register_target(&secmark_target);
Patrick McHardyff968302006-05-24 16:15:03 +0000113}