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