blob: 0b48547e8d64bb0a2e41998733713a1b51730196 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* This is a module which is used for setting the NFMARK field of an skb. */
2
3/* (C) 1999-2001 Marc Boucher <marc@mbsi.ca>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/module.h>
11#include <linux/skbuff.h>
12#include <linux/ip.h>
13#include <net/checksum.h>
14
Harald Welte2e4e6a12006-01-12 13:30:04 -080015#include <linux/netfilter/x_tables.h>
16#include <linux/netfilter/xt_MARK.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18MODULE_LICENSE("GPL");
19MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
Harald Welte2e4e6a12006-01-12 13:30:04 -080020MODULE_DESCRIPTION("ip[6]tables MARK modification module");
21MODULE_ALIAS("ipt_MARK");
22MODULE_ALIAS("ip6t_MARK");
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24static unsigned int
25target_v0(struct sk_buff **pskb,
26 const struct net_device *in,
27 const struct net_device *out,
28 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -080029 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -070030 const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Harald Welte2e4e6a12006-01-12 13:30:04 -080032 const struct xt_mark_target_info *markinfo = targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Thomas Graf82e91ff2006-11-09 15:19:14 -080034 if((*pskb)->mark != markinfo->mark)
35 (*pskb)->mark = markinfo->mark;
Harald Welte6869c4d2005-08-09 19:24:19 -070036
Harald Welte2e4e6a12006-01-12 13:30:04 -080037 return XT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
40static unsigned int
41target_v1(struct sk_buff **pskb,
42 const struct net_device *in,
43 const struct net_device *out,
44 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -080045 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -070046 const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Harald Welte2e4e6a12006-01-12 13:30:04 -080048 const struct xt_mark_target_info_v1 *markinfo = targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 int mark = 0;
50
51 switch (markinfo->mode) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080052 case XT_MARK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 mark = markinfo->mark;
54 break;
55
Harald Welte2e4e6a12006-01-12 13:30:04 -080056 case XT_MARK_AND:
Thomas Graf82e91ff2006-11-09 15:19:14 -080057 mark = (*pskb)->mark & markinfo->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 break;
59
Harald Welte2e4e6a12006-01-12 13:30:04 -080060 case XT_MARK_OR:
Thomas Graf82e91ff2006-11-09 15:19:14 -080061 mark = (*pskb)->mark | markinfo->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 break;
63 }
64
Thomas Graf82e91ff2006-11-09 15:19:14 -080065 if((*pskb)->mark != mark)
66 (*pskb)->mark = mark;
Harald Welte6869c4d2005-08-09 19:24:19 -070067
Harald Welte2e4e6a12006-01-12 13:30:04 -080068 return XT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71
72static int
73checkentry_v0(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080074 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -080075 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 void *targinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned int hook_mask)
78{
Harald Welte2e4e6a12006-01-12 13:30:04 -080079 struct xt_mark_target_info *markinfo = targinfo;
Harald Weltebf3a46a2005-08-09 19:22:01 -070080
Harald Weltebf3a46a2005-08-09 19:22:01 -070081 if (markinfo->mark > 0xffffffff) {
82 printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n");
83 return 0;
84 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return 1;
86}
87
88static int
89checkentry_v1(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080090 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -080091 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 void *targinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 unsigned int hook_mask)
94{
Harald Welte2e4e6a12006-01-12 13:30:04 -080095 struct xt_mark_target_info_v1 *markinfo = targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Harald Welte2e4e6a12006-01-12 13:30:04 -080097 if (markinfo->mode != XT_MARK_SET
98 && markinfo->mode != XT_MARK_AND
99 && markinfo->mode != XT_MARK_OR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 printk(KERN_WARNING "MARK: unknown mode %u\n",
101 markinfo->mode);
102 return 0;
103 }
Harald Weltebf3a46a2005-08-09 19:22:01 -0700104 if (markinfo->mark > 0xffffffff) {
105 printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n");
106 return 0;
107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 1;
109}
110
Patrick McHardybe7263b72006-09-20 12:06:10 -0700111#ifdef CONFIG_COMPAT
112struct compat_xt_mark_target_info_v1 {
113 compat_ulong_t mark;
114 u_int8_t mode;
115 u_int8_t __pad1;
116 u_int16_t __pad2;
117};
118
119static void compat_from_user_v1(void *dst, void *src)
120{
121 struct compat_xt_mark_target_info_v1 *cm = src;
122 struct xt_mark_target_info_v1 m = {
123 .mark = cm->mark,
124 .mode = cm->mode,
125 };
126 memcpy(dst, &m, sizeof(m));
127}
128
129static int compat_to_user_v1(void __user *dst, void *src)
130{
131 struct xt_mark_target_info_v1 *m = src;
132 struct compat_xt_mark_target_info_v1 cm = {
133 .mark = m->mark,
134 .mode = m->mode,
135 };
136 return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
137}
138#endif /* CONFIG_COMPAT */
139
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700140static struct xt_target xt_mark_target[] = {
141 {
142 .name = "MARK",
143 .family = AF_INET,
144 .revision = 0,
145 .checkentry = checkentry_v0,
146 .target = target_v0,
147 .targetsize = sizeof(struct xt_mark_target_info),
148 .table = "mangle",
149 .me = THIS_MODULE,
150 },
151 {
152 .name = "MARK",
153 .family = AF_INET,
154 .revision = 1,
155 .checkentry = checkentry_v1,
156 .target = target_v1,
157 .targetsize = sizeof(struct xt_mark_target_info_v1),
Patrick McHardybe7263b72006-09-20 12:06:10 -0700158#ifdef CONFIG_COMPAT
159 .compatsize = sizeof(struct compat_xt_mark_target_info_v1),
160 .compat_from_user = compat_from_user_v1,
161 .compat_to_user = compat_to_user_v1,
162#endif
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700163 .table = "mangle",
164 .me = THIS_MODULE,
165 },
166 {
167 .name = "MARK",
168 .family = AF_INET6,
169 .revision = 0,
170 .checkentry = checkentry_v0,
171 .target = target_v0,
172 .targetsize = sizeof(struct xt_mark_target_info),
173 .table = "mangle",
174 .me = THIS_MODULE,
175 },
Harald Welte2e4e6a12006-01-12 13:30:04 -0800176};
177
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800178static int __init xt_mark_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700180 return xt_register_targets(xt_mark_target, ARRAY_SIZE(xt_mark_target));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800183static void __exit xt_mark_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700185 xt_unregister_targets(xt_mark_target, ARRAY_SIZE(xt_mark_target));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800188module_init(xt_mark_init);
189module_exit(xt_mark_fini);