James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This module is used to copy security markings from packets |
| 3 | * to connections, and restore security markings from connections |
| 4 | * back to packets. This would normally be performed in conjunction |
| 5 | * with the SECMARK target and state match. |
| 6 | * |
| 7 | * Based somewhat on CONNMARK: |
| 8 | * Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com> |
| 9 | * by Henrik Nordstrom <hno@marasystems.com> |
| 10 | * |
| 11 | * (C) 2006 Red Hat, Inc., James Morris <jmorris@redhat.com> |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License version 2 as |
| 15 | * published by the Free Software Foundation. |
| 16 | * |
| 17 | */ |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/skbuff.h> |
| 20 | #include <linux/netfilter/x_tables.h> |
| 21 | #include <linux/netfilter/xt_CONNSECMARK.h> |
| 22 | #include <net/netfilter/nf_conntrack_compat.h> |
| 23 | |
| 24 | #define PFX "CONNSECMARK: " |
| 25 | |
| 26 | MODULE_LICENSE("GPL"); |
| 27 | MODULE_AUTHOR("James Morris <jmorris@redhat.com>"); |
| 28 | MODULE_DESCRIPTION("ip[6]tables CONNSECMARK module"); |
| 29 | MODULE_ALIAS("ipt_CONNSECMARK"); |
| 30 | MODULE_ALIAS("ip6t_CONNSECMARK"); |
| 31 | |
| 32 | /* |
| 33 | * If the packet has a security mark and the connection does not, copy |
| 34 | * the security mark from the packet to the connection. |
| 35 | */ |
| 36 | static void secmark_save(struct sk_buff *skb) |
| 37 | { |
| 38 | if (skb->secmark) { |
| 39 | u32 *connsecmark; |
| 40 | enum ip_conntrack_info ctinfo; |
| 41 | |
| 42 | connsecmark = nf_ct_get_secmark(skb, &ctinfo); |
| 43 | if (connsecmark && !*connsecmark) |
| 44 | if (*connsecmark != skb->secmark) |
| 45 | *connsecmark = skb->secmark; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * If packet has no security mark, and the connection does, restore the |
| 51 | * security mark from the connection to the packet. |
| 52 | */ |
| 53 | static void secmark_restore(struct sk_buff *skb) |
| 54 | { |
| 55 | if (!skb->secmark) { |
| 56 | u32 *connsecmark; |
| 57 | enum ip_conntrack_info ctinfo; |
| 58 | |
| 59 | connsecmark = nf_ct_get_secmark(skb, &ctinfo); |
| 60 | if (connsecmark && *connsecmark) |
| 61 | if (skb->secmark != *connsecmark) |
| 62 | skb->secmark = *connsecmark; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static unsigned int target(struct sk_buff **pskb, const struct net_device *in, |
| 67 | const struct net_device *out, unsigned int hooknum, |
| 68 | const struct xt_target *target, |
Patrick McHardy | fe1cb10 | 2006-08-22 00:35:47 -0700 | [diff] [blame] | 69 | const void *targinfo) |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 70 | { |
| 71 | struct sk_buff *skb = *pskb; |
| 72 | const struct xt_connsecmark_target_info *info = targinfo; |
| 73 | |
| 74 | switch (info->mode) { |
| 75 | case CONNSECMARK_SAVE: |
| 76 | secmark_save(skb); |
| 77 | break; |
| 78 | |
| 79 | case CONNSECMARK_RESTORE: |
| 80 | secmark_restore(skb); |
| 81 | break; |
| 82 | |
| 83 | default: |
| 84 | BUG(); |
| 85 | } |
| 86 | |
| 87 | return XT_CONTINUE; |
| 88 | } |
| 89 | |
| 90 | static int checkentry(const char *tablename, const void *entry, |
| 91 | const struct xt_target *target, void *targinfo, |
Patrick McHardy | efa7416 | 2006-08-22 00:36:37 -0700 | [diff] [blame] | 92 | unsigned int hook_mask) |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 93 | { |
| 94 | struct xt_connsecmark_target_info *info = targinfo; |
| 95 | |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 96 | if (nf_ct_l3proto_try_module_get(target->family) < 0) { |
| 97 | printk(KERN_WARNING "can't load conntrack support for " |
| 98 | "proto=%d\n", target->family); |
| 99 | return 0; |
| 100 | } |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 101 | switch (info->mode) { |
| 102 | case CONNSECMARK_SAVE: |
| 103 | case CONNSECMARK_RESTORE: |
| 104 | break; |
| 105 | |
| 106 | default: |
| 107 | printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | return 1; |
| 112 | } |
| 113 | |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 114 | static void |
| 115 | destroy(const struct xt_target *target, void *targinfo) |
| 116 | { |
| 117 | nf_ct_l3proto_module_put(target->family); |
| 118 | } |
| 119 | |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 120 | static struct xt_target xt_connsecmark_target[] = { |
| 121 | { |
| 122 | .name = "CONNSECMARK", |
| 123 | .family = AF_INET, |
| 124 | .checkentry = checkentry, |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 125 | .destroy = destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 126 | .target = target, |
| 127 | .targetsize = sizeof(struct xt_connsecmark_target_info), |
| 128 | .table = "mangle", |
| 129 | .me = THIS_MODULE, |
| 130 | }, |
| 131 | { |
| 132 | .name = "CONNSECMARK", |
| 133 | .family = AF_INET6, |
| 134 | .checkentry = checkentry, |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 135 | .destroy = destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 136 | .target = target, |
| 137 | .targetsize = sizeof(struct xt_connsecmark_target_info), |
| 138 | .table = "mangle", |
| 139 | .me = THIS_MODULE, |
| 140 | }, |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | static int __init xt_connsecmark_init(void) |
| 144 | { |
Thomas Graf | 2809486 | 2006-08-22 13:52:17 -0700 | [diff] [blame] | 145 | return xt_register_targets(xt_connsecmark_target, |
| 146 | ARRAY_SIZE(xt_connsecmark_target)); |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static void __exit xt_connsecmark_fini(void) |
| 150 | { |
Thomas Graf | 2809486 | 2006-08-22 13:52:17 -0700 | [diff] [blame] | 151 | xt_unregister_targets(xt_connsecmark_target, |
| 152 | ARRAY_SIZE(xt_connsecmark_target)); |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | module_init(xt_connsecmark_init); |
| 156 | module_exit(xt_connsecmark_fini); |