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> |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 22 | #include <net/netfilter/nf_conntrack.h> |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 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 | */ |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 36 | static void secmark_save(const struct sk_buff *skb) |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 37 | { |
| 38 | if (skb->secmark) { |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 39 | struct nf_conn *ct; |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 40 | enum ip_conntrack_info ctinfo; |
| 41 | |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 42 | ct = nf_ct_get(skb, &ctinfo); |
| 43 | if (ct && !ct->secmark) |
| 44 | ct->secmark = skb->secmark; |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * If packet has no security mark, and the connection does, restore the |
| 50 | * security mark from the connection to the packet. |
| 51 | */ |
| 52 | static void secmark_restore(struct sk_buff *skb) |
| 53 | { |
| 54 | if (!skb->secmark) { |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 55 | struct nf_conn *ct; |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 56 | enum ip_conntrack_info ctinfo; |
| 57 | |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 58 | ct = nf_ct_get(skb, &ctinfo); |
| 59 | if (ct && ct->secmark) |
| 60 | skb->secmark = ct->secmark; |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 64 | static unsigned int |
| 65 | connsecmark_tg(struct sk_buff *skb, const struct net_device *in, |
| 66 | const struct net_device *out, unsigned int hooknum, |
| 67 | const struct xt_target *target, const void *targinfo) |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 68 | { |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 69 | const struct xt_connsecmark_target_info *info = targinfo; |
| 70 | |
| 71 | switch (info->mode) { |
| 72 | case CONNSECMARK_SAVE: |
| 73 | secmark_save(skb); |
| 74 | break; |
| 75 | |
| 76 | case CONNSECMARK_RESTORE: |
| 77 | secmark_restore(skb); |
| 78 | break; |
| 79 | |
| 80 | default: |
| 81 | BUG(); |
| 82 | } |
| 83 | |
| 84 | return XT_CONTINUE; |
| 85 | } |
| 86 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 87 | static bool |
| 88 | connsecmark_tg_check(const char *tablename, const void *entry, |
| 89 | const struct xt_target *target, void *targinfo, |
| 90 | unsigned int hook_mask) |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 91 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 92 | const struct xt_connsecmark_target_info *info = targinfo; |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 93 | |
| 94 | switch (info->mode) { |
| 95 | case CONNSECMARK_SAVE: |
| 96 | case CONNSECMARK_RESTORE: |
| 97 | break; |
| 98 | |
| 99 | default: |
| 100 | printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode); |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 101 | return false; |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Jan Engelhardt | 67b4af2 | 2007-12-01 00:01:50 +1100 | [diff] [blame] | 104 | if (nf_ct_l3proto_try_module_get(target->family) < 0) { |
| 105 | printk(KERN_WARNING "can't load conntrack support for " |
| 106 | "proto=%d\n", target->family); |
| 107 | return false; |
| 108 | } |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 109 | return true; |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 112 | static void |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 113 | connsecmark_tg_destroy(const struct xt_target *target, void *targinfo) |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 114 | { |
| 115 | nf_ct_l3proto_module_put(target->family); |
| 116 | } |
| 117 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 118 | static struct xt_target connsecmark_tg_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 119 | { |
| 120 | .name = "CONNSECMARK", |
| 121 | .family = AF_INET, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 122 | .checkentry = connsecmark_tg_check, |
| 123 | .destroy = connsecmark_tg_destroy, |
| 124 | .target = connsecmark_tg, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 125 | .targetsize = sizeof(struct xt_connsecmark_target_info), |
| 126 | .table = "mangle", |
| 127 | .me = THIS_MODULE, |
| 128 | }, |
| 129 | { |
| 130 | .name = "CONNSECMARK", |
| 131 | .family = AF_INET6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 132 | .checkentry = connsecmark_tg_check, |
| 133 | .destroy = connsecmark_tg_destroy, |
| 134 | .target = connsecmark_tg, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 135 | .targetsize = sizeof(struct xt_connsecmark_target_info), |
| 136 | .table = "mangle", |
| 137 | .me = THIS_MODULE, |
| 138 | }, |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 141 | static int __init connsecmark_tg_init(void) |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 142 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 143 | return xt_register_targets(connsecmark_tg_reg, |
| 144 | ARRAY_SIZE(connsecmark_tg_reg)); |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 147 | static void __exit connsecmark_tg_exit(void) |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 148 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 149 | xt_unregister_targets(connsecmark_tg_reg, |
| 150 | ARRAY_SIZE(connsecmark_tg_reg)); |
James Morris | 100468e | 2006-06-09 00:32:39 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 153 | module_init(connsecmark_tg_init); |
| 154 | module_exit(connsecmark_tg_exit); |