blob: 95a171c8799410be5560d494b8a65823d47714e3 [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
Harald Welte6869c4d2005-08-09 19:24:19 -070034 if((*pskb)->nfmark != markinfo->mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 (*pskb)->nfmark = 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:
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 mark = (*pskb)->nfmark & markinfo->mark;
58 break;
59
Harald Welte2e4e6a12006-01-12 13:30:04 -080060 case XT_MARK_OR:
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 mark = (*pskb)->nfmark | markinfo->mark;
62 break;
63 }
64
Harald Welte6869c4d2005-08-09 19:24:19 -070065 if((*pskb)->nfmark != mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 (*pskb)->nfmark = 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,
77 unsigned int targinfosize,
78 unsigned int hook_mask)
79{
Harald Welte2e4e6a12006-01-12 13:30:04 -080080 struct xt_mark_target_info *markinfo = targinfo;
Harald Weltebf3a46a2005-08-09 19:22:01 -070081
Harald Weltebf3a46a2005-08-09 19:22:01 -070082 if (markinfo->mark > 0xffffffff) {
83 printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n");
84 return 0;
85 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return 1;
87}
88
89static int
90checkentry_v1(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080091 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -080092 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 void *targinfo,
94 unsigned int targinfosize,
95 unsigned int hook_mask)
96{
Harald Welte2e4e6a12006-01-12 13:30:04 -080097 struct xt_mark_target_info_v1 *markinfo = targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Harald Welte2e4e6a12006-01-12 13:30:04 -080099 if (markinfo->mode != XT_MARK_SET
100 && markinfo->mode != XT_MARK_AND
101 && markinfo->mode != XT_MARK_OR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 printk(KERN_WARNING "MARK: unknown mode %u\n",
103 markinfo->mode);
104 return 0;
105 }
Harald Weltebf3a46a2005-08-09 19:22:01 -0700106 if (markinfo->mark > 0xffffffff) {
107 printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n");
108 return 0;
109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return 1;
111}
112
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700113static struct xt_target xt_mark_target[] = {
114 {
115 .name = "MARK",
116 .family = AF_INET,
117 .revision = 0,
118 .checkentry = checkentry_v0,
119 .target = target_v0,
120 .targetsize = sizeof(struct xt_mark_target_info),
121 .table = "mangle",
122 .me = THIS_MODULE,
123 },
124 {
125 .name = "MARK",
126 .family = AF_INET,
127 .revision = 1,
128 .checkentry = checkentry_v1,
129 .target = target_v1,
130 .targetsize = sizeof(struct xt_mark_target_info_v1),
131 .table = "mangle",
132 .me = THIS_MODULE,
133 },
134 {
135 .name = "MARK",
136 .family = AF_INET6,
137 .revision = 0,
138 .checkentry = checkentry_v0,
139 .target = target_v0,
140 .targetsize = sizeof(struct xt_mark_target_info),
141 .table = "mangle",
142 .me = THIS_MODULE,
143 },
Harald Welte2e4e6a12006-01-12 13:30:04 -0800144};
145
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800146static int __init xt_mark_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700148 return xt_register_targets(xt_mark_target, ARRAY_SIZE(xt_mark_target));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800151static void __exit xt_mark_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700153 xt_unregister_targets(xt_mark_target, ARRAY_SIZE(xt_mark_target));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800156module_init(xt_mark_init);
157module_exit(xt_mark_fini);