Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* 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 Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 15 | #include <linux/netfilter/x_tables.h> |
| 16 | #include <linux/netfilter/xt_MARK.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | MODULE_LICENSE("GPL"); |
| 19 | MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 20 | MODULE_DESCRIPTION("ip[6]tables MARK modification module"); |
| 21 | MODULE_ALIAS("ipt_MARK"); |
| 22 | MODULE_ALIAS("ip6t_MARK"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | static unsigned int |
| 25 | target_v0(struct sk_buff **pskb, |
| 26 | const struct net_device *in, |
| 27 | const struct net_device *out, |
| 28 | unsigned int hooknum, |
| 29 | const void *targinfo, |
| 30 | void *userinfo) |
| 31 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 32 | const struct xt_mark_target_info *markinfo = targinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Harald Welte | 6869c4d | 2005-08-09 19:24:19 -0700 | [diff] [blame] | 34 | if((*pskb)->nfmark != markinfo->mark) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | (*pskb)->nfmark = markinfo->mark; |
Harald Welte | 6869c4d | 2005-08-09 19:24:19 -0700 | [diff] [blame] | 36 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 37 | return XT_CONTINUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static unsigned int |
| 41 | target_v1(struct sk_buff **pskb, |
| 42 | const struct net_device *in, |
| 43 | const struct net_device *out, |
| 44 | unsigned int hooknum, |
| 45 | const void *targinfo, |
| 46 | void *userinfo) |
| 47 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 48 | const struct xt_mark_target_info_v1 *markinfo = targinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | int mark = 0; |
| 50 | |
| 51 | switch (markinfo->mode) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 52 | case XT_MARK_SET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | mark = markinfo->mark; |
| 54 | break; |
| 55 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 56 | case XT_MARK_AND: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | mark = (*pskb)->nfmark & markinfo->mark; |
| 58 | break; |
| 59 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 60 | case XT_MARK_OR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | mark = (*pskb)->nfmark | markinfo->mark; |
| 62 | break; |
| 63 | } |
| 64 | |
Harald Welte | 6869c4d | 2005-08-09 19:24:19 -0700 | [diff] [blame] | 65 | if((*pskb)->nfmark != mark) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | (*pskb)->nfmark = mark; |
Harald Welte | 6869c4d | 2005-08-09 19:24:19 -0700 | [diff] [blame] | 67 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 68 | return XT_CONTINUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
| 72 | static int |
| 73 | checkentry_v0(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 74 | const void *entry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | void *targinfo, |
| 76 | unsigned int targinfosize, |
| 77 | unsigned int hook_mask) |
| 78 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 79 | struct xt_mark_target_info *markinfo = targinfo; |
Harald Welte | bf3a46a | 2005-08-09 19:22:01 -0700 | [diff] [blame] | 80 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 81 | if (targinfosize != XT_ALIGN(sizeof(struct xt_mark_target_info))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | printk(KERN_WARNING "MARK: targinfosize %u != %Zu\n", |
| 83 | targinfosize, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 84 | XT_ALIGN(sizeof(struct xt_mark_target_info))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | if (strcmp(tablename, "mangle") != 0) { |
| 89 | printk(KERN_WARNING "MARK: can only be called from \"mangle\" table, not \"%s\"\n", tablename); |
| 90 | return 0; |
| 91 | } |
| 92 | |
Harald Welte | bf3a46a | 2005-08-09 19:22:01 -0700 | [diff] [blame] | 93 | if (markinfo->mark > 0xffffffff) { |
| 94 | printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n"); |
| 95 | return 0; |
| 96 | } |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | static int |
| 102 | checkentry_v1(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 103 | const void *entry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | void *targinfo, |
| 105 | unsigned int targinfosize, |
| 106 | unsigned int hook_mask) |
| 107 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 108 | struct xt_mark_target_info_v1 *markinfo = targinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 110 | if (targinfosize != XT_ALIGN(sizeof(struct xt_mark_target_info_v1))){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | printk(KERN_WARNING "MARK: targinfosize %u != %Zu\n", |
| 112 | targinfosize, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 113 | XT_ALIGN(sizeof(struct xt_mark_target_info_v1))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | if (strcmp(tablename, "mangle") != 0) { |
| 118 | printk(KERN_WARNING "MARK: can only be called from \"mangle\" table, not \"%s\"\n", tablename); |
| 119 | return 0; |
| 120 | } |
| 121 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 122 | if (markinfo->mode != XT_MARK_SET |
| 123 | && markinfo->mode != XT_MARK_AND |
| 124 | && markinfo->mode != XT_MARK_OR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | printk(KERN_WARNING "MARK: unknown mode %u\n", |
| 126 | markinfo->mode); |
| 127 | return 0; |
| 128 | } |
| 129 | |
Harald Welte | bf3a46a | 2005-08-09 19:22:01 -0700 | [diff] [blame] | 130 | if (markinfo->mark > 0xffffffff) { |
| 131 | printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n"); |
| 132 | return 0; |
| 133 | } |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return 1; |
| 136 | } |
| 137 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 138 | static struct xt_target ipt_mark_reg_v0 = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | .name = "MARK", |
| 140 | .target = target_v0, |
| 141 | .checkentry = checkentry_v0, |
| 142 | .me = THIS_MODULE, |
| 143 | .revision = 0, |
| 144 | }; |
| 145 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 146 | static struct xt_target ipt_mark_reg_v1 = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | .name = "MARK", |
| 148 | .target = target_v1, |
| 149 | .checkentry = checkentry_v1, |
| 150 | .me = THIS_MODULE, |
| 151 | .revision = 1, |
| 152 | }; |
| 153 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 154 | static struct xt_target ip6t_mark_reg_v0 = { |
| 155 | .name = "MARK", |
| 156 | .target = target_v0, |
| 157 | .checkentry = checkentry_v0, |
| 158 | .me = THIS_MODULE, |
| 159 | .revision = 0, |
| 160 | }; |
| 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | static int __init init(void) |
| 163 | { |
| 164 | int err; |
| 165 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 166 | err = xt_register_target(AF_INET, &ipt_mark_reg_v0); |
| 167 | if (err) |
| 168 | return err; |
| 169 | |
| 170 | err = xt_register_target(AF_INET, &ipt_mark_reg_v1); |
| 171 | if (err) |
| 172 | xt_unregister_target(AF_INET, &ipt_mark_reg_v0); |
| 173 | |
| 174 | err = xt_register_target(AF_INET6, &ip6t_mark_reg_v0); |
| 175 | if (err) { |
| 176 | xt_unregister_target(AF_INET, &ipt_mark_reg_v0); |
| 177 | xt_unregister_target(AF_INET, &ipt_mark_reg_v1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return err; |
| 181 | } |
| 182 | |
| 183 | static void __exit fini(void) |
| 184 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 185 | xt_unregister_target(AF_INET, &ipt_mark_reg_v0); |
| 186 | xt_unregister_target(AF_INET, &ipt_mark_reg_v1); |
| 187 | xt_unregister_target(AF_INET6, &ip6t_mark_reg_v0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | module_init(init); |
| 191 | module_exit(fini); |