Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Kernel module to match NFMARK values. */ |
| 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 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 13 | #include <linux/netfilter/xt_mark.h> |
| 14 | #include <linux/netfilter/x_tables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | MODULE_LICENSE("GPL"); |
| 17 | MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>"); |
| 18 | MODULE_DESCRIPTION("iptables mark matching module"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 19 | MODULE_ALIAS("ipt_mark"); |
| 20 | MODULE_ALIAS("ip6t_mark"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 22 | static bool |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | match(const struct sk_buff *skb, |
| 24 | const struct net_device *in, |
| 25 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 26 | const struct xt_match *match, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | const void *matchinfo, |
| 28 | int offset, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 29 | unsigned int protoff, |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 30 | bool *hotdrop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 32 | const struct xt_mark_info *info = matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 34 | return ((skb->mark & info->mask) == info->mark) ^ info->invert; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 37 | static bool |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | checkentry(const char *tablename, |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 39 | const void *entry, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 40 | const struct xt_match *match, |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 41 | void *matchinfo, |
| 42 | unsigned int hook_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
Patrick McHardy | 3e72b2f | 2006-05-29 18:19:19 -0700 | [diff] [blame] | 44 | const struct xt_mark_info *minfo = matchinfo; |
Harald Welte | bf3a46a | 2005-08-09 19:22:01 -0700 | [diff] [blame] | 45 | |
Harald Welte | bf3a46a | 2005-08-09 19:22:01 -0700 | [diff] [blame] | 46 | if (minfo->mark > 0xffffffff || minfo->mask > 0xffffffff) { |
| 47 | printk(KERN_WARNING "mark: only supports 32bit mark\n"); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 48 | return false; |
Harald Welte | bf3a46a | 2005-08-09 19:22:01 -0700 | [diff] [blame] | 49 | } |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 50 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Patrick McHardy | bc80b65 | 2006-09-20 12:05:54 -0700 | [diff] [blame] | 53 | #ifdef CONFIG_COMPAT |
| 54 | struct compat_xt_mark_info { |
| 55 | compat_ulong_t mark, mask; |
| 56 | u_int8_t invert; |
| 57 | u_int8_t __pad1; |
| 58 | u_int16_t __pad2; |
| 59 | }; |
| 60 | |
| 61 | static void compat_from_user(void *dst, void *src) |
| 62 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame^] | 63 | const struct compat_xt_mark_info *cm = src; |
Patrick McHardy | bc80b65 | 2006-09-20 12:05:54 -0700 | [diff] [blame] | 64 | struct xt_mark_info m = { |
| 65 | .mark = cm->mark, |
| 66 | .mask = cm->mask, |
| 67 | .invert = cm->invert, |
| 68 | }; |
| 69 | memcpy(dst, &m, sizeof(m)); |
| 70 | } |
| 71 | |
| 72 | static int compat_to_user(void __user *dst, void *src) |
| 73 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame^] | 74 | const struct xt_mark_info *m = src; |
Patrick McHardy | bc80b65 | 2006-09-20 12:05:54 -0700 | [diff] [blame] | 75 | struct compat_xt_mark_info cm = { |
| 76 | .mark = m->mark, |
| 77 | .mask = m->mask, |
| 78 | .invert = m->invert, |
| 79 | }; |
| 80 | return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0; |
| 81 | } |
| 82 | #endif /* CONFIG_COMPAT */ |
| 83 | |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 84 | static struct xt_match xt_mark_match[] = { |
| 85 | { |
| 86 | .name = "mark", |
| 87 | .family = AF_INET, |
| 88 | .checkentry = checkentry, |
| 89 | .match = match, |
| 90 | .matchsize = sizeof(struct xt_mark_info), |
Patrick McHardy | bc80b65 | 2006-09-20 12:05:54 -0700 | [diff] [blame] | 91 | #ifdef CONFIG_COMPAT |
| 92 | .compatsize = sizeof(struct compat_xt_mark_info), |
| 93 | .compat_from_user = compat_from_user, |
| 94 | .compat_to_user = compat_to_user, |
| 95 | #endif |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 96 | .me = THIS_MODULE, |
| 97 | }, |
| 98 | { |
| 99 | .name = "mark", |
| 100 | .family = AF_INET6, |
| 101 | .checkentry = checkentry, |
| 102 | .match = match, |
| 103 | .matchsize = sizeof(struct xt_mark_info), |
| 104 | .me = THIS_MODULE, |
| 105 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 108 | static int __init xt_mark_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 110 | return xt_register_matches(xt_mark_match, ARRAY_SIZE(xt_mark_match)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 113 | static void __exit xt_mark_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 115 | xt_unregister_matches(xt_mark_match, ARRAY_SIZE(xt_mark_match)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 118 | module_init(xt_mark_init); |
| 119 | module_exit(xt_mark_fini); |