ipv6: syncookies: free reqsk on xfrm_lookup error
[linux-2.6/mini2440.git] / net / netfilter / xt_SECMARK.c
blob94f87ee7552b0708b855ce998a3a355e71b13af4
1 /*
2 * Module for modifying the secmark field of the skb, for use by
3 * security subsystems.
5 * Based on the nfmark match by:
6 * (C) 1999-2001 Marc Boucher <marc@mbsi.ca>
8 * (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/module.h>
16 #include <linux/skbuff.h>
17 #include <linux/selinux.h>
18 #include <linux/netfilter/x_tables.h>
19 #include <linux/netfilter/xt_SECMARK.h>
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
23 MODULE_DESCRIPTION("Xtables: packet security mark modification");
24 MODULE_ALIAS("ipt_SECMARK");
25 MODULE_ALIAS("ip6t_SECMARK");
27 #define PFX "SECMARK: "
29 static u8 mode;
31 static unsigned int
32 secmark_tg(struct sk_buff *skb, const struct net_device *in,
33 const struct net_device *out, unsigned int hooknum,
34 const struct xt_target *target, const void *targinfo)
36 u32 secmark = 0;
37 const struct xt_secmark_target_info *info = targinfo;
39 BUG_ON(info->mode != mode);
41 switch (mode) {
42 case SECMARK_MODE_SEL:
43 secmark = info->u.sel.selsid;
44 break;
46 default:
47 BUG();
50 skb->secmark = secmark;
51 return XT_CONTINUE;
54 static bool checkentry_selinux(struct xt_secmark_target_info *info)
56 int err;
57 struct xt_secmark_target_selinux_info *sel = &info->u.sel;
59 sel->selctx[SECMARK_SELCTX_MAX - 1] = '\0';
61 err = selinux_string_to_sid(sel->selctx, &sel->selsid);
62 if (err) {
63 if (err == -EINVAL)
64 printk(KERN_INFO PFX "invalid SELinux context \'%s\'\n",
65 sel->selctx);
66 return false;
69 if (!sel->selsid) {
70 printk(KERN_INFO PFX "unable to map SELinux context \'%s\'\n",
71 sel->selctx);
72 return false;
75 err = selinux_secmark_relabel_packet_permission(sel->selsid);
76 if (err) {
77 printk(KERN_INFO PFX "unable to obtain relabeling permission\n");
78 return false;
81 selinux_secmark_refcount_inc();
82 return true;
85 static bool
86 secmark_tg_check(const char *tablename, const void *entry,
87 const struct xt_target *target, void *targinfo,
88 unsigned int hook_mask)
90 struct xt_secmark_target_info *info = targinfo;
92 if (strcmp(tablename, "mangle") && strcmp(tablename, "security")) {
93 printk(KERN_INFO PFX "target only valid in the \'mangle\' "
94 "or \'security\' tables, not \'%s\'.\n", tablename);
95 return false;
98 if (mode && mode != info->mode) {
99 printk(KERN_INFO PFX "mode already set to %hu cannot mix with "
100 "rules for mode %hu\n", mode, info->mode);
101 return false;
104 switch (info->mode) {
105 case SECMARK_MODE_SEL:
106 if (!checkentry_selinux(info))
107 return false;
108 break;
110 default:
111 printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
112 return false;
115 if (!mode)
116 mode = info->mode;
117 return true;
120 static void secmark_tg_destroy(const struct xt_target *target, void *targinfo)
122 switch (mode) {
123 case SECMARK_MODE_SEL:
124 selinux_secmark_refcount_dec();
128 static struct xt_target secmark_tg_reg[] __read_mostly = {
130 .name = "SECMARK",
131 .family = AF_INET,
132 .checkentry = secmark_tg_check,
133 .destroy = secmark_tg_destroy,
134 .target = secmark_tg,
135 .targetsize = sizeof(struct xt_secmark_target_info),
136 .me = THIS_MODULE,
139 .name = "SECMARK",
140 .family = AF_INET6,
141 .checkentry = secmark_tg_check,
142 .destroy = secmark_tg_destroy,
143 .target = secmark_tg,
144 .targetsize = sizeof(struct xt_secmark_target_info),
145 .me = THIS_MODULE,
149 static int __init secmark_tg_init(void)
151 return xt_register_targets(secmark_tg_reg, ARRAY_SIZE(secmark_tg_reg));
154 static void __exit secmark_tg_exit(void)
156 xt_unregister_targets(secmark_tg_reg, ARRAY_SIZE(secmark_tg_reg));
159 module_init(secmark_tg_init);
160 module_exit(secmark_tg_exit);