blob: ee9c34edc76c4e8d8b920013c8f4825b6de37a36 [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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 const void *targinfo,
31 void *userinfo)
32{
Harald Welte2e4e6a12006-01-12 13:30:04 -080033 const struct xt_mark_target_info *markinfo = targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Harald Welte6869c4d2005-08-09 19:24:19 -070035 if((*pskb)->nfmark != markinfo->mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 (*pskb)->nfmark = markinfo->mark;
Harald Welte6869c4d2005-08-09 19:24:19 -070037
Harald Welte2e4e6a12006-01-12 13:30:04 -080038 return XT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
41static unsigned int
42target_v1(struct sk_buff **pskb,
43 const struct net_device *in,
44 const struct net_device *out,
45 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -080046 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 const void *targinfo,
48 void *userinfo)
49{
Harald Welte2e4e6a12006-01-12 13:30:04 -080050 const struct xt_mark_target_info_v1 *markinfo = targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int mark = 0;
52
53 switch (markinfo->mode) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080054 case XT_MARK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 mark = markinfo->mark;
56 break;
57
Harald Welte2e4e6a12006-01-12 13:30:04 -080058 case XT_MARK_AND:
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 mark = (*pskb)->nfmark & markinfo->mark;
60 break;
61
Harald Welte2e4e6a12006-01-12 13:30:04 -080062 case XT_MARK_OR:
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 mark = (*pskb)->nfmark | markinfo->mark;
64 break;
65 }
66
Harald Welte6869c4d2005-08-09 19:24:19 -070067 if((*pskb)->nfmark != mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 (*pskb)->nfmark = mark;
Harald Welte6869c4d2005-08-09 19:24:19 -070069
Harald Welte2e4e6a12006-01-12 13:30:04 -080070 return XT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73
74static int
75checkentry_v0(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080076 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -080077 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 void *targinfo,
79 unsigned int targinfosize,
80 unsigned int hook_mask)
81{
Harald Welte2e4e6a12006-01-12 13:30:04 -080082 struct xt_mark_target_info *markinfo = targinfo;
Harald Weltebf3a46a2005-08-09 19:22:01 -070083
Harald Weltebf3a46a2005-08-09 19:22:01 -070084 if (markinfo->mark > 0xffffffff) {
85 printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n");
86 return 0;
87 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return 1;
89}
90
91static int
92checkentry_v1(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080093 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -080094 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 void *targinfo,
96 unsigned int targinfosize,
97 unsigned int hook_mask)
98{
Harald Welte2e4e6a12006-01-12 13:30:04 -080099 struct xt_mark_target_info_v1 *markinfo = targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Harald Welte2e4e6a12006-01-12 13:30:04 -0800101 if (markinfo->mode != XT_MARK_SET
102 && markinfo->mode != XT_MARK_AND
103 && markinfo->mode != XT_MARK_OR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 printk(KERN_WARNING "MARK: unknown mode %u\n",
105 markinfo->mode);
106 return 0;
107 }
Harald Weltebf3a46a2005-08-09 19:22:01 -0700108 if (markinfo->mark > 0xffffffff) {
109 printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n");
110 return 0;
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 1;
113}
114
Harald Welte2e4e6a12006-01-12 13:30:04 -0800115static struct xt_target ipt_mark_reg_v0 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .name = "MARK",
117 .target = target_v0,
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800118 .targetsize = sizeof(struct xt_mark_target_info),
119 .table = "mangle",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .checkentry = checkentry_v0,
121 .me = THIS_MODULE,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800122 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .revision = 0,
124};
125
Harald Welte2e4e6a12006-01-12 13:30:04 -0800126static struct xt_target ipt_mark_reg_v1 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .name = "MARK",
128 .target = target_v1,
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800129 .targetsize = sizeof(struct xt_mark_target_info_v1),
130 .table = "mangle",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .checkentry = checkentry_v1,
132 .me = THIS_MODULE,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800133 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .revision = 1,
135};
136
Harald Welte2e4e6a12006-01-12 13:30:04 -0800137static struct xt_target ip6t_mark_reg_v0 = {
138 .name = "MARK",
139 .target = target_v0,
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800140 .targetsize = sizeof(struct xt_mark_target_info),
141 .table = "mangle",
Harald Welte2e4e6a12006-01-12 13:30:04 -0800142 .checkentry = checkentry_v0,
143 .me = THIS_MODULE,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800144 .family = AF_INET6,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800145 .revision = 0,
146};
147
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800148static int __init xt_mark_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 int err;
151
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800152 err = xt_register_target(&ipt_mark_reg_v0);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800153 if (err)
154 return err;
155
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800156 err = xt_register_target(&ipt_mark_reg_v1);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800157 if (err)
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800158 xt_unregister_target(&ipt_mark_reg_v0);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800159
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800160 err = xt_register_target(&ip6t_mark_reg_v0);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800161 if (err) {
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800162 xt_unregister_target(&ipt_mark_reg_v0);
163 xt_unregister_target(&ipt_mark_reg_v1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return err;
167}
168
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800169static void __exit xt_mark_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800171 xt_unregister_target(&ipt_mark_reg_v0);
172 xt_unregister_target(&ipt_mark_reg_v1);
173 xt_unregister_target(&ip6t_mark_reg_v0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800176module_init(xt_mark_init);
177module_exit(xt_mark_fini);