Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NetLabel Unlabeled Support |
| 3 | * |
| 4 | * This file defines functions for dealing with unlabeled packets for the |
| 5 | * NetLabel system. The NetLabel system manages static and dynamic label |
| 6 | * mappings for network protocols such as CIPSO and RIPSO. |
| 7 | * |
| 8 | * Author: Paul Moore <paul.moore@hp.com> |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | /* |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 13 | * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 - 2008 |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 23 | * the GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write to the Free Software |
| 27 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | #include <linux/types.h> |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 32 | #include <linux/rcupdate.h> |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 33 | #include <linux/list.h> |
| 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/socket.h> |
| 36 | #include <linux/string.h> |
| 37 | #include <linux/skbuff.h> |
Paul Moore | de64688 | 2006-11-17 17:38:55 -0500 | [diff] [blame] | 38 | #include <linux/audit.h> |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 39 | #include <linux/in.h> |
| 40 | #include <linux/in6.h> |
| 41 | #include <linux/ip.h> |
| 42 | #include <linux/ipv6.h> |
| 43 | #include <linux/notifier.h> |
| 44 | #include <linux/netdevice.h> |
| 45 | #include <linux/security.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 46 | #include <linux/slab.h> |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 47 | #include <net/sock.h> |
| 48 | #include <net/netlink.h> |
| 49 | #include <net/genetlink.h> |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 50 | #include <net/ip.h> |
| 51 | #include <net/ipv6.h> |
| 52 | #include <net/net_namespace.h> |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 53 | #include <net/netlabel.h> |
| 54 | #include <asm/bug.h> |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 55 | #include <asm/atomic.h> |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 56 | |
| 57 | #include "netlabel_user.h" |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 58 | #include "netlabel_addrlist.h" |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 59 | #include "netlabel_domainhash.h" |
| 60 | #include "netlabel_unlabeled.h" |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 61 | #include "netlabel_mgmt.h" |
| 62 | |
| 63 | /* NOTE: at present we always use init's network namespace since we don't |
| 64 | * presently support different namespaces even though the majority of |
| 65 | * the functions in this file are "namespace safe" */ |
| 66 | |
| 67 | /* The unlabeled connection hash table which we use to map network interfaces |
| 68 | * and addresses of unlabeled packets to a user specified secid value for the |
| 69 | * LSM. The hash table is used to lookup the network interface entry |
| 70 | * (struct netlbl_unlhsh_iface) and then the interface entry is used to |
| 71 | * lookup an IP address match from an ordered list. If a network interface |
| 72 | * match can not be found in the hash table then the default entry |
| 73 | * (netlbl_unlhsh_def) is used. The IP address entry list |
| 74 | * (struct netlbl_unlhsh_addr) is ordered such that the entries with a |
| 75 | * larger netmask come first. |
| 76 | */ |
| 77 | struct netlbl_unlhsh_tbl { |
| 78 | struct list_head *tbl; |
| 79 | u32 size; |
| 80 | }; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 81 | #define netlbl_unlhsh_addr4_entry(iter) \ |
| 82 | container_of(iter, struct netlbl_unlhsh_addr4, list) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 83 | struct netlbl_unlhsh_addr4 { |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 84 | u32 secid; |
| 85 | |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 86 | struct netlbl_af4list list; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 87 | struct rcu_head rcu; |
| 88 | }; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 89 | #define netlbl_unlhsh_addr6_entry(iter) \ |
| 90 | container_of(iter, struct netlbl_unlhsh_addr6, list) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 91 | struct netlbl_unlhsh_addr6 { |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 92 | u32 secid; |
| 93 | |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 94 | struct netlbl_af6list list; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 95 | struct rcu_head rcu; |
| 96 | }; |
| 97 | struct netlbl_unlhsh_iface { |
| 98 | int ifindex; |
| 99 | struct list_head addr4_list; |
| 100 | struct list_head addr6_list; |
| 101 | |
| 102 | u32 valid; |
| 103 | struct list_head list; |
| 104 | struct rcu_head rcu; |
| 105 | }; |
| 106 | |
| 107 | /* Argument struct for netlbl_unlhsh_walk() */ |
| 108 | struct netlbl_unlhsh_walk_arg { |
| 109 | struct netlink_callback *nl_cb; |
| 110 | struct sk_buff *skb; |
| 111 | u32 seq; |
| 112 | }; |
| 113 | |
| 114 | /* Unlabeled connection hash table */ |
| 115 | /* updates should be so rare that having one spinlock for the entire |
| 116 | * hash table should be okay */ |
| 117 | static DEFINE_SPINLOCK(netlbl_unlhsh_lock); |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 118 | #define netlbl_unlhsh_rcu_deref(p) \ |
| 119 | rcu_dereference_check(p, rcu_read_lock_held() || \ |
| 120 | lockdep_is_held(&netlbl_unlhsh_lock)) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 121 | static struct netlbl_unlhsh_tbl *netlbl_unlhsh = NULL; |
| 122 | static struct netlbl_unlhsh_iface *netlbl_unlhsh_def = NULL; |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 123 | |
| 124 | /* Accept unlabeled packets flag */ |
Paul Moore | cd28786 | 2006-11-17 17:38:44 -0500 | [diff] [blame] | 125 | static u8 netlabel_unlabel_acceptflg = 0; |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 126 | |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 127 | /* NetLabel Generic NETLINK unlabeled family */ |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 128 | static struct genl_family netlbl_unlabel_gnl_family = { |
| 129 | .id = GENL_ID_GENERATE, |
| 130 | .hdrsize = 0, |
| 131 | .name = NETLBL_NLTYPE_UNLABELED_NAME, |
| 132 | .version = NETLBL_PROTO_VERSION, |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame] | 133 | .maxattr = NLBL_UNLABEL_A_MAX, |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame] | 136 | /* NetLabel Netlink attribute policy */ |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 137 | static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = { |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame] | 138 | [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 }, |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 139 | [NLBL_UNLABEL_A_IPV6ADDR] = { .type = NLA_BINARY, |
| 140 | .len = sizeof(struct in6_addr) }, |
| 141 | [NLBL_UNLABEL_A_IPV6MASK] = { .type = NLA_BINARY, |
| 142 | .len = sizeof(struct in6_addr) }, |
| 143 | [NLBL_UNLABEL_A_IPV4ADDR] = { .type = NLA_BINARY, |
| 144 | .len = sizeof(struct in_addr) }, |
| 145 | [NLBL_UNLABEL_A_IPV4MASK] = { .type = NLA_BINARY, |
| 146 | .len = sizeof(struct in_addr) }, |
| 147 | [NLBL_UNLABEL_A_IFACE] = { .type = NLA_NUL_STRING, |
| 148 | .len = IFNAMSIZ - 1 }, |
| 149 | [NLBL_UNLABEL_A_SECCTX] = { .type = NLA_BINARY } |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame] | 150 | }; |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 151 | |
| 152 | /* |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 153 | * Unlabeled Connection Hash Table Functions |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 154 | */ |
| 155 | |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 156 | /** |
| 157 | * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table |
| 158 | * @entry: the entry's RCU field |
| 159 | * |
| 160 | * Description: |
| 161 | * This function is designed to be used as a callback to the call_rcu() |
| 162 | * function so that memory allocated to a hash table interface entry can be |
| 163 | * released safely. It is important to note that this function does not free |
| 164 | * the IPv4 and IPv6 address lists contained as part of an interface entry. It |
| 165 | * is up to the rest of the code to make sure an interface entry is only freed |
| 166 | * once it's address lists are empty. |
| 167 | * |
| 168 | */ |
| 169 | static void netlbl_unlhsh_free_iface(struct rcu_head *entry) |
| 170 | { |
| 171 | struct netlbl_unlhsh_iface *iface; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 172 | struct netlbl_af4list *iter4; |
| 173 | struct netlbl_af4list *tmp4; |
| 174 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 175 | struct netlbl_af6list *iter6; |
| 176 | struct netlbl_af6list *tmp6; |
| 177 | #endif /* IPv6 */ |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 178 | |
| 179 | iface = container_of(entry, struct netlbl_unlhsh_iface, rcu); |
| 180 | |
| 181 | /* no need for locks here since we are the only one with access to this |
| 182 | * structure */ |
| 183 | |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 184 | netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) { |
| 185 | netlbl_af4list_remove_entry(iter4); |
| 186 | kfree(netlbl_unlhsh_addr4_entry(iter4)); |
| 187 | } |
| 188 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 189 | netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) { |
| 190 | netlbl_af6list_remove_entry(iter6); |
| 191 | kfree(netlbl_unlhsh_addr6_entry(iter6)); |
| 192 | } |
| 193 | #endif /* IPv6 */ |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 194 | kfree(iface); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * netlbl_unlhsh_hash - Hashing function for the hash table |
| 199 | * @ifindex: the network interface/device to hash |
| 200 | * |
| 201 | * Description: |
| 202 | * This is the hashing function for the unlabeled hash table, it returns the |
| 203 | * bucket number for the given device/interface. The caller is responsible for |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 204 | * ensuring that the hash table is protected with either a RCU read lock or |
| 205 | * the hash table lock. |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 206 | * |
| 207 | */ |
| 208 | static u32 netlbl_unlhsh_hash(int ifindex) |
| 209 | { |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 210 | return ifindex & (netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->size - 1); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 214 | * netlbl_unlhsh_search_iface - Search for a matching interface entry |
| 215 | * @ifindex: the network interface |
| 216 | * |
| 217 | * Description: |
| 218 | * Searches the unlabeled connection hash table and returns a pointer to the |
| 219 | * interface entry which matches @ifindex, otherwise NULL is returned. The |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 220 | * caller is responsible for ensuring that the hash table is protected with |
| 221 | * either a RCU read lock or the hash table lock. |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 222 | * |
| 223 | */ |
| 224 | static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex) |
| 225 | { |
| 226 | u32 bkt; |
Paul Moore | 5619670 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 227 | struct list_head *bkt_list; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 228 | struct netlbl_unlhsh_iface *iter; |
| 229 | |
| 230 | bkt = netlbl_unlhsh_hash(ifindex); |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 231 | bkt_list = &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt]; |
Paul Moore | 5619670 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 232 | list_for_each_entry_rcu(iter, bkt_list, list) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 233 | if (iter->valid && iter->ifindex == ifindex) |
| 234 | return iter; |
| 235 | |
| 236 | return NULL; |
| 237 | } |
| 238 | |
| 239 | /** |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 240 | * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table |
| 241 | * @iface: the associated interface entry |
| 242 | * @addr: IPv4 address in network byte order |
| 243 | * @mask: IPv4 address mask in network byte order |
| 244 | * @secid: LSM secid value for entry |
| 245 | * |
| 246 | * Description: |
| 247 | * Add a new address entry into the unlabeled connection hash table using the |
| 248 | * interface entry specified by @iface. On success zero is returned, otherwise |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 249 | * a negative value is returned. |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 250 | * |
| 251 | */ |
| 252 | static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface, |
| 253 | const struct in_addr *addr, |
| 254 | const struct in_addr *mask, |
| 255 | u32 secid) |
| 256 | { |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 257 | int ret_val; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 258 | struct netlbl_unlhsh_addr4 *entry; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 259 | |
| 260 | entry = kzalloc(sizeof(*entry), GFP_ATOMIC); |
| 261 | if (entry == NULL) |
| 262 | return -ENOMEM; |
| 263 | |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 264 | entry->list.addr = addr->s_addr & mask->s_addr; |
| 265 | entry->list.mask = mask->s_addr; |
| 266 | entry->list.valid = 1; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 267 | entry->secid = secid; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 268 | |
| 269 | spin_lock(&netlbl_unlhsh_lock); |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 270 | ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 271 | spin_unlock(&netlbl_unlhsh_lock); |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 272 | |
| 273 | if (ret_val != 0) |
| 274 | kfree(entry); |
| 275 | return ret_val; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 279 | /** |
| 280 | * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table |
| 281 | * @iface: the associated interface entry |
| 282 | * @addr: IPv6 address in network byte order |
| 283 | * @mask: IPv6 address mask in network byte order |
| 284 | * @secid: LSM secid value for entry |
| 285 | * |
| 286 | * Description: |
| 287 | * Add a new address entry into the unlabeled connection hash table using the |
| 288 | * interface entry specified by @iface. On success zero is returned, otherwise |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 289 | * a negative value is returned. |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 290 | * |
| 291 | */ |
| 292 | static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface, |
| 293 | const struct in6_addr *addr, |
| 294 | const struct in6_addr *mask, |
| 295 | u32 secid) |
| 296 | { |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 297 | int ret_val; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 298 | struct netlbl_unlhsh_addr6 *entry; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 299 | |
| 300 | entry = kzalloc(sizeof(*entry), GFP_ATOMIC); |
| 301 | if (entry == NULL) |
| 302 | return -ENOMEM; |
| 303 | |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 304 | ipv6_addr_copy(&entry->list.addr, addr); |
| 305 | entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0]; |
| 306 | entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1]; |
| 307 | entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2]; |
| 308 | entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3]; |
| 309 | ipv6_addr_copy(&entry->list.mask, mask); |
| 310 | entry->list.valid = 1; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 311 | entry->secid = secid; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 312 | |
| 313 | spin_lock(&netlbl_unlhsh_lock); |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 314 | ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 315 | spin_unlock(&netlbl_unlhsh_lock); |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 316 | |
| 317 | if (ret_val != 0) |
| 318 | kfree(entry); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 319 | return 0; |
| 320 | } |
| 321 | #endif /* IPv6 */ |
| 322 | |
| 323 | /** |
| 324 | * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table |
| 325 | * @ifindex: network interface |
| 326 | * |
| 327 | * Description: |
| 328 | * Add a new, empty, interface entry into the unlabeled connection hash table. |
| 329 | * On success a pointer to the new interface entry is returned, on failure NULL |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 330 | * is returned. |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 331 | * |
| 332 | */ |
| 333 | static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex) |
| 334 | { |
| 335 | u32 bkt; |
| 336 | struct netlbl_unlhsh_iface *iface; |
| 337 | |
| 338 | iface = kzalloc(sizeof(*iface), GFP_ATOMIC); |
| 339 | if (iface == NULL) |
| 340 | return NULL; |
| 341 | |
| 342 | iface->ifindex = ifindex; |
| 343 | INIT_LIST_HEAD(&iface->addr4_list); |
| 344 | INIT_LIST_HEAD(&iface->addr6_list); |
| 345 | iface->valid = 1; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 346 | |
| 347 | spin_lock(&netlbl_unlhsh_lock); |
| 348 | if (ifindex > 0) { |
| 349 | bkt = netlbl_unlhsh_hash(ifindex); |
| 350 | if (netlbl_unlhsh_search_iface(ifindex) != NULL) |
| 351 | goto add_iface_failure; |
| 352 | list_add_tail_rcu(&iface->list, |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 353 | &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt]); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 354 | } else { |
| 355 | INIT_LIST_HEAD(&iface->list); |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 356 | if (netlbl_unlhsh_rcu_deref(netlbl_unlhsh_def) != NULL) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 357 | goto add_iface_failure; |
| 358 | rcu_assign_pointer(netlbl_unlhsh_def, iface); |
| 359 | } |
| 360 | spin_unlock(&netlbl_unlhsh_lock); |
| 361 | |
| 362 | return iface; |
| 363 | |
| 364 | add_iface_failure: |
| 365 | spin_unlock(&netlbl_unlhsh_lock); |
| 366 | kfree(iface); |
| 367 | return NULL; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table |
| 372 | * @net: network namespace |
| 373 | * @dev_name: interface name |
| 374 | * @addr: IP address in network byte order |
| 375 | * @mask: address mask in network byte order |
| 376 | * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6) |
| 377 | * @secid: LSM secid value for the entry |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 378 | * @audit_info: NetLabel audit information |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 379 | * |
| 380 | * Description: |
| 381 | * Adds a new entry to the unlabeled connection hash table. Returns zero on |
| 382 | * success, negative values on failure. |
| 383 | * |
| 384 | */ |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 385 | int netlbl_unlhsh_add(struct net *net, |
| 386 | const char *dev_name, |
| 387 | const void *addr, |
| 388 | const void *mask, |
| 389 | u32 addr_len, |
| 390 | u32 secid, |
| 391 | struct netlbl_audit *audit_info) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 392 | { |
| 393 | int ret_val; |
| 394 | int ifindex; |
| 395 | struct net_device *dev; |
| 396 | struct netlbl_unlhsh_iface *iface; |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 397 | struct audit_buffer *audit_buf = NULL; |
| 398 | char *secctx = NULL; |
| 399 | u32 secctx_len; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 400 | |
| 401 | if (addr_len != sizeof(struct in_addr) && |
| 402 | addr_len != sizeof(struct in6_addr)) |
| 403 | return -EINVAL; |
| 404 | |
| 405 | rcu_read_lock(); |
| 406 | if (dev_name != NULL) { |
Eric Dumazet | 122ec6f | 2009-11-05 20:53:47 -0800 | [diff] [blame] | 407 | dev = dev_get_by_name_rcu(net, dev_name); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 408 | if (dev == NULL) { |
| 409 | ret_val = -ENODEV; |
| 410 | goto unlhsh_add_return; |
| 411 | } |
| 412 | ifindex = dev->ifindex; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 413 | iface = netlbl_unlhsh_search_iface(ifindex); |
| 414 | } else { |
| 415 | ifindex = 0; |
| 416 | iface = rcu_dereference(netlbl_unlhsh_def); |
| 417 | } |
| 418 | if (iface == NULL) { |
| 419 | iface = netlbl_unlhsh_add_iface(ifindex); |
| 420 | if (iface == NULL) { |
| 421 | ret_val = -ENOMEM; |
| 422 | goto unlhsh_add_return; |
| 423 | } |
| 424 | } |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 425 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD, |
| 426 | audit_info); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 427 | switch (addr_len) { |
Pavel Emelyanov | 56628b1 | 2008-02-12 22:37:19 -0800 | [diff] [blame] | 428 | case sizeof(struct in_addr): { |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame^] | 429 | const struct in_addr *addr4 = addr; |
| 430 | const struct in_addr *mask4 = mask; |
Pavel Emelyanov | 56628b1 | 2008-02-12 22:37:19 -0800 | [diff] [blame] | 431 | |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 432 | ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid); |
| 433 | if (audit_buf != NULL) |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 434 | netlbl_af4list_audit_addr(audit_buf, 1, |
| 435 | dev_name, |
| 436 | addr4->s_addr, |
| 437 | mask4->s_addr); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 438 | break; |
Pavel Emelyanov | 56628b1 | 2008-02-12 22:37:19 -0800 | [diff] [blame] | 439 | } |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 440 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
Pavel Emelyanov | 56628b1 | 2008-02-12 22:37:19 -0800 | [diff] [blame] | 441 | case sizeof(struct in6_addr): { |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame^] | 442 | const struct in6_addr *addr6 = addr; |
| 443 | const struct in6_addr *mask6 = mask; |
Pavel Emelyanov | 56628b1 | 2008-02-12 22:37:19 -0800 | [diff] [blame] | 444 | |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 445 | ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid); |
| 446 | if (audit_buf != NULL) |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 447 | netlbl_af6list_audit_addr(audit_buf, 1, |
| 448 | dev_name, |
| 449 | addr6, mask6); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 450 | break; |
Pavel Emelyanov | 56628b1 | 2008-02-12 22:37:19 -0800 | [diff] [blame] | 451 | } |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 452 | #endif /* IPv6 */ |
| 453 | default: |
| 454 | ret_val = -EINVAL; |
| 455 | } |
| 456 | if (ret_val == 0) |
| 457 | atomic_inc(&netlabel_mgmt_protocount); |
| 458 | |
| 459 | unlhsh_add_return: |
| 460 | rcu_read_unlock(); |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 461 | if (audit_buf != NULL) { |
| 462 | if (security_secid_to_secctx(secid, |
| 463 | &secctx, |
| 464 | &secctx_len) == 0) { |
| 465 | audit_log_format(audit_buf, " sec_obj=%s", secctx); |
| 466 | security_release_secctx(secctx, secctx_len); |
| 467 | } |
| 468 | audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0); |
| 469 | audit_log_end(audit_buf); |
| 470 | } |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 471 | return ret_val; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 476 | * @net: network namespace |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 477 | * @iface: interface entry |
| 478 | * @addr: IP address |
| 479 | * @mask: IP address mask |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 480 | * @audit_info: NetLabel audit information |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 481 | * |
| 482 | * Description: |
| 483 | * Remove an IP address entry from the unlabeled connection hash table. |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 484 | * Returns zero on success, negative values on failure. |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 485 | * |
| 486 | */ |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 487 | static int netlbl_unlhsh_remove_addr4(struct net *net, |
| 488 | struct netlbl_unlhsh_iface *iface, |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 489 | const struct in_addr *addr, |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 490 | const struct in_addr *mask, |
| 491 | struct netlbl_audit *audit_info) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 492 | { |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 493 | struct netlbl_af4list *list_entry; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 494 | struct netlbl_unlhsh_addr4 *entry; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 495 | struct audit_buffer *audit_buf; |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 496 | struct net_device *dev; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 497 | char *secctx; |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 498 | u32 secctx_len; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 499 | |
| 500 | spin_lock(&netlbl_unlhsh_lock); |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 501 | list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr, |
| 502 | &iface->addr4_list); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 503 | spin_unlock(&netlbl_unlhsh_lock); |
Paul Moore | d25830e | 2008-12-03 00:37:04 -0800 | [diff] [blame] | 504 | if (list_entry != NULL) |
| 505 | entry = netlbl_unlhsh_addr4_entry(list_entry); |
| 506 | else |
Paul Moore | ec8f237 | 2008-12-11 21:31:50 -0800 | [diff] [blame] | 507 | entry = NULL; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 508 | |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 509 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL, |
| 510 | audit_info); |
| 511 | if (audit_buf != NULL) { |
| 512 | dev = dev_get_by_index(net, iface->ifindex); |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 513 | netlbl_af4list_audit_addr(audit_buf, 1, |
| 514 | (dev != NULL ? dev->name : NULL), |
| 515 | addr->s_addr, mask->s_addr); |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 516 | if (dev != NULL) |
| 517 | dev_put(dev); |
Paul Moore | ec8f237 | 2008-12-11 21:31:50 -0800 | [diff] [blame] | 518 | if (entry != NULL && |
| 519 | security_secid_to_secctx(entry->secid, |
| 520 | &secctx, &secctx_len) == 0) { |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 521 | audit_log_format(audit_buf, " sec_obj=%s", secctx); |
| 522 | security_release_secctx(secctx, secctx_len); |
| 523 | } |
Paul Moore | ec8f237 | 2008-12-11 21:31:50 -0800 | [diff] [blame] | 524 | audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0); |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 525 | audit_log_end(audit_buf); |
| 526 | } |
| 527 | |
Paul Moore | ec8f237 | 2008-12-11 21:31:50 -0800 | [diff] [blame] | 528 | if (entry == NULL) |
| 529 | return -ENOENT; |
| 530 | |
Lai Jiangshan | c3b4942 | 2011-03-18 12:03:56 +0800 | [diff] [blame] | 531 | kfree_rcu(entry, rcu); |
Paul Moore | ec8f237 | 2008-12-11 21:31:50 -0800 | [diff] [blame] | 532 | return 0; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 536 | /** |
| 537 | * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 538 | * @net: network namespace |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 539 | * @iface: interface entry |
| 540 | * @addr: IP address |
| 541 | * @mask: IP address mask |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 542 | * @audit_info: NetLabel audit information |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 543 | * |
| 544 | * Description: |
| 545 | * Remove an IP address entry from the unlabeled connection hash table. |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 546 | * Returns zero on success, negative values on failure. |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 547 | * |
| 548 | */ |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 549 | static int netlbl_unlhsh_remove_addr6(struct net *net, |
| 550 | struct netlbl_unlhsh_iface *iface, |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 551 | const struct in6_addr *addr, |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 552 | const struct in6_addr *mask, |
| 553 | struct netlbl_audit *audit_info) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 554 | { |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 555 | struct netlbl_af6list *list_entry; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 556 | struct netlbl_unlhsh_addr6 *entry; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 557 | struct audit_buffer *audit_buf; |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 558 | struct net_device *dev; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 559 | char *secctx; |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 560 | u32 secctx_len; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 561 | |
| 562 | spin_lock(&netlbl_unlhsh_lock); |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 563 | list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 564 | spin_unlock(&netlbl_unlhsh_lock); |
Paul Moore | d25830e | 2008-12-03 00:37:04 -0800 | [diff] [blame] | 565 | if (list_entry != NULL) |
| 566 | entry = netlbl_unlhsh_addr6_entry(list_entry); |
| 567 | else |
Paul Moore | ec8f237 | 2008-12-11 21:31:50 -0800 | [diff] [blame] | 568 | entry = NULL; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 569 | |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 570 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL, |
| 571 | audit_info); |
| 572 | if (audit_buf != NULL) { |
| 573 | dev = dev_get_by_index(net, iface->ifindex); |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 574 | netlbl_af6list_audit_addr(audit_buf, 1, |
| 575 | (dev != NULL ? dev->name : NULL), |
| 576 | addr, mask); |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 577 | if (dev != NULL) |
| 578 | dev_put(dev); |
Paul Moore | ec8f237 | 2008-12-11 21:31:50 -0800 | [diff] [blame] | 579 | if (entry != NULL && |
| 580 | security_secid_to_secctx(entry->secid, |
| 581 | &secctx, &secctx_len) == 0) { |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 582 | audit_log_format(audit_buf, " sec_obj=%s", secctx); |
| 583 | security_release_secctx(secctx, secctx_len); |
| 584 | } |
Paul Moore | ec8f237 | 2008-12-11 21:31:50 -0800 | [diff] [blame] | 585 | audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0); |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 586 | audit_log_end(audit_buf); |
| 587 | } |
| 588 | |
Paul Moore | ec8f237 | 2008-12-11 21:31:50 -0800 | [diff] [blame] | 589 | if (entry == NULL) |
| 590 | return -ENOENT; |
| 591 | |
Lai Jiangshan | 6b26223 | 2011-03-18 12:04:50 +0800 | [diff] [blame] | 592 | kfree_rcu(entry, rcu); |
Paul Moore | ec8f237 | 2008-12-11 21:31:50 -0800 | [diff] [blame] | 593 | return 0; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 594 | } |
| 595 | #endif /* IPv6 */ |
| 596 | |
| 597 | /** |
| 598 | * netlbl_unlhsh_condremove_iface - Remove an interface entry |
| 599 | * @iface: the interface entry |
| 600 | * |
| 601 | * Description: |
| 602 | * Remove an interface entry from the unlabeled connection hash table if it is |
| 603 | * empty. An interface entry is considered to be empty if there are no |
| 604 | * address entries assigned to it. |
| 605 | * |
| 606 | */ |
| 607 | static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface) |
| 608 | { |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 609 | struct netlbl_af4list *iter4; |
| 610 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 611 | struct netlbl_af6list *iter6; |
| 612 | #endif /* IPv6 */ |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 613 | |
| 614 | spin_lock(&netlbl_unlhsh_lock); |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 615 | netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list) |
| 616 | goto unlhsh_condremove_failure; |
| 617 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 618 | netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list) |
| 619 | goto unlhsh_condremove_failure; |
| 620 | #endif /* IPv6 */ |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 621 | iface->valid = 0; |
| 622 | if (iface->ifindex > 0) |
| 623 | list_del_rcu(&iface->list); |
| 624 | else |
| 625 | rcu_assign_pointer(netlbl_unlhsh_def, NULL); |
| 626 | spin_unlock(&netlbl_unlhsh_lock); |
| 627 | |
| 628 | call_rcu(&iface->rcu, netlbl_unlhsh_free_iface); |
| 629 | return; |
| 630 | |
| 631 | unlhsh_condremove_failure: |
| 632 | spin_unlock(&netlbl_unlhsh_lock); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /** |
| 636 | * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table |
| 637 | * @net: network namespace |
| 638 | * @dev_name: interface name |
| 639 | * @addr: IP address in network byte order |
| 640 | * @mask: address mask in network byte order |
| 641 | * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6) |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 642 | * @audit_info: NetLabel audit information |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 643 | * |
| 644 | * Description: |
| 645 | * Removes and existing entry from the unlabeled connection hash table. |
| 646 | * Returns zero on success, negative values on failure. |
| 647 | * |
| 648 | */ |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 649 | int netlbl_unlhsh_remove(struct net *net, |
| 650 | const char *dev_name, |
| 651 | const void *addr, |
| 652 | const void *mask, |
| 653 | u32 addr_len, |
| 654 | struct netlbl_audit *audit_info) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 655 | { |
| 656 | int ret_val; |
| 657 | struct net_device *dev; |
| 658 | struct netlbl_unlhsh_iface *iface; |
| 659 | |
| 660 | if (addr_len != sizeof(struct in_addr) && |
| 661 | addr_len != sizeof(struct in6_addr)) |
| 662 | return -EINVAL; |
| 663 | |
| 664 | rcu_read_lock(); |
| 665 | if (dev_name != NULL) { |
Eric Dumazet | 122ec6f | 2009-11-05 20:53:47 -0800 | [diff] [blame] | 666 | dev = dev_get_by_name_rcu(net, dev_name); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 667 | if (dev == NULL) { |
| 668 | ret_val = -ENODEV; |
| 669 | goto unlhsh_remove_return; |
| 670 | } |
| 671 | iface = netlbl_unlhsh_search_iface(dev->ifindex); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 672 | } else |
| 673 | iface = rcu_dereference(netlbl_unlhsh_def); |
| 674 | if (iface == NULL) { |
| 675 | ret_val = -ENOENT; |
| 676 | goto unlhsh_remove_return; |
| 677 | } |
| 678 | switch (addr_len) { |
| 679 | case sizeof(struct in_addr): |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 680 | ret_val = netlbl_unlhsh_remove_addr4(net, |
| 681 | iface, addr, mask, |
| 682 | audit_info); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 683 | break; |
| 684 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 685 | case sizeof(struct in6_addr): |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 686 | ret_val = netlbl_unlhsh_remove_addr6(net, |
| 687 | iface, addr, mask, |
| 688 | audit_info); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 689 | break; |
| 690 | #endif /* IPv6 */ |
| 691 | default: |
| 692 | ret_val = -EINVAL; |
| 693 | } |
| 694 | if (ret_val == 0) { |
| 695 | netlbl_unlhsh_condremove_iface(iface); |
| 696 | atomic_dec(&netlabel_mgmt_protocount); |
| 697 | } |
| 698 | |
| 699 | unlhsh_remove_return: |
| 700 | rcu_read_unlock(); |
| 701 | return ret_val; |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * General Helper Functions |
| 706 | */ |
| 707 | |
| 708 | /** |
| 709 | * netlbl_unlhsh_netdev_handler - Network device notification handler |
| 710 | * @this: notifier block |
| 711 | * @event: the event |
| 712 | * @ptr: the network device (cast to void) |
| 713 | * |
| 714 | * Description: |
| 715 | * Handle network device events, although at present all we care about is a |
| 716 | * network device going away. In the case of a device going away we clear any |
| 717 | * related entries from the unlabeled connection hash table. |
| 718 | * |
| 719 | */ |
| 720 | static int netlbl_unlhsh_netdev_handler(struct notifier_block *this, |
| 721 | unsigned long event, |
| 722 | void *ptr) |
| 723 | { |
| 724 | struct net_device *dev = ptr; |
| 725 | struct netlbl_unlhsh_iface *iface = NULL; |
| 726 | |
YOSHIFUJI Hideaki | 721499e | 2008-07-19 22:34:43 -0700 | [diff] [blame] | 727 | if (!net_eq(dev_net(dev), &init_net)) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 728 | return NOTIFY_DONE; |
| 729 | |
| 730 | /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */ |
| 731 | if (event == NETDEV_DOWN) { |
| 732 | spin_lock(&netlbl_unlhsh_lock); |
| 733 | iface = netlbl_unlhsh_search_iface(dev->ifindex); |
| 734 | if (iface != NULL && iface->valid) { |
| 735 | iface->valid = 0; |
| 736 | list_del_rcu(&iface->list); |
| 737 | } else |
| 738 | iface = NULL; |
| 739 | spin_unlock(&netlbl_unlhsh_lock); |
| 740 | } |
| 741 | |
| 742 | if (iface != NULL) |
| 743 | call_rcu(&iface->rcu, netlbl_unlhsh_free_iface); |
| 744 | |
| 745 | return NOTIFY_DONE; |
| 746 | } |
| 747 | |
| 748 | /** |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 749 | * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag |
| 750 | * @value: desired value |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 751 | * @audit_info: NetLabel audit information |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 752 | * |
| 753 | * Description: |
| 754 | * Set the value of the unlabeled accept flag to @value. |
| 755 | * |
| 756 | */ |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 757 | static void netlbl_unlabel_acceptflg_set(u8 value, |
| 758 | struct netlbl_audit *audit_info) |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 759 | { |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 760 | struct audit_buffer *audit_buf; |
| 761 | u8 old_val; |
| 762 | |
Paul Moore | 4be2700 | 2007-10-26 04:29:08 -0700 | [diff] [blame] | 763 | old_val = netlabel_unlabel_acceptflg; |
Paul Moore | cd28786 | 2006-11-17 17:38:44 -0500 | [diff] [blame] | 764 | netlabel_unlabel_acceptflg = value; |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 765 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW, |
| 766 | audit_info); |
Paul Moore | de64688 | 2006-11-17 17:38:55 -0500 | [diff] [blame] | 767 | if (audit_buf != NULL) { |
| 768 | audit_log_format(audit_buf, |
| 769 | " unlbl_accept=%u old=%u", value, old_val); |
| 770 | audit_log_end(audit_buf); |
| 771 | } |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 774 | /** |
| 775 | * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information |
| 776 | * @info: the Generic NETLINK info block |
| 777 | * @addr: the IP address |
| 778 | * @mask: the IP address mask |
| 779 | * @len: the address length |
| 780 | * |
| 781 | * Description: |
| 782 | * Examine the Generic NETLINK message and extract the IP address information. |
| 783 | * Returns zero on success, negative values on failure. |
| 784 | * |
| 785 | */ |
| 786 | static int netlbl_unlabel_addrinfo_get(struct genl_info *info, |
| 787 | void **addr, |
| 788 | void **mask, |
| 789 | u32 *len) |
| 790 | { |
| 791 | u32 addr_len; |
| 792 | |
| 793 | if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) { |
| 794 | addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]); |
| 795 | if (addr_len != sizeof(struct in_addr) && |
| 796 | addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK])) |
| 797 | return -EINVAL; |
| 798 | *len = addr_len; |
| 799 | *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]); |
| 800 | *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]); |
| 801 | return 0; |
| 802 | } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) { |
| 803 | addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]); |
| 804 | if (addr_len != sizeof(struct in6_addr) && |
| 805 | addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK])) |
| 806 | return -EINVAL; |
| 807 | *len = addr_len; |
| 808 | *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]); |
| 809 | *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]); |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | return -EINVAL; |
| 814 | } |
| 815 | |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 816 | /* |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 817 | * NetLabel Command Handlers |
| 818 | */ |
| 819 | |
| 820 | /** |
| 821 | * netlbl_unlabel_accept - Handle an ACCEPT message |
| 822 | * @skb: the NETLINK buffer |
| 823 | * @info: the Generic NETLINK info block |
| 824 | * |
| 825 | * Description: |
| 826 | * Process a user generated ACCEPT message and set the accept flag accordingly. |
| 827 | * Returns zero on success, negative values on failure. |
| 828 | * |
| 829 | */ |
| 830 | static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info) |
| 831 | { |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame] | 832 | u8 value; |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 833 | struct netlbl_audit audit_info; |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 834 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame] | 835 | if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) { |
| 836 | value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]); |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 837 | if (value == 1 || value == 0) { |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 838 | netlbl_netlink_auditinfo(skb, &audit_info); |
| 839 | netlbl_unlabel_acceptflg_set(value, &audit_info); |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 840 | return 0; |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 844 | return -EINVAL; |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | /** |
| 848 | * netlbl_unlabel_list - Handle a LIST message |
| 849 | * @skb: the NETLINK buffer |
| 850 | * @info: the Generic NETLINK info block |
| 851 | * |
| 852 | * Description: |
| 853 | * Process a user generated LIST message and respond with the current status. |
| 854 | * Returns zero on success, negative values on failure. |
| 855 | * |
| 856 | */ |
| 857 | static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info) |
| 858 | { |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame] | 859 | int ret_val = -EINVAL; |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 860 | struct sk_buff *ans_skb; |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame] | 861 | void *data; |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 862 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 863 | ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 864 | if (ans_skb == NULL) |
| 865 | goto list_failure; |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 866 | data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family, |
| 867 | 0, NLBL_UNLABEL_C_LIST); |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame] | 868 | if (data == NULL) { |
| 869 | ret_val = -ENOMEM; |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 870 | goto list_failure; |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame] | 871 | } |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 872 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame] | 873 | ret_val = nla_put_u8(ans_skb, |
| 874 | NLBL_UNLABEL_A_ACPTFLG, |
Paul Moore | cd28786 | 2006-11-17 17:38:44 -0500 | [diff] [blame] | 875 | netlabel_unlabel_acceptflg); |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 876 | if (ret_val != 0) |
| 877 | goto list_failure; |
| 878 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame] | 879 | genlmsg_end(ans_skb, data); |
Denis V. Lunev | fe785be | 2008-07-10 16:53:39 -0700 | [diff] [blame] | 880 | return genlmsg_reply(ans_skb, info); |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 881 | |
| 882 | list_failure: |
Patrick McHardy | b08d584 | 2007-02-27 09:57:37 -0800 | [diff] [blame] | 883 | kfree_skb(ans_skb); |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 884 | return ret_val; |
| 885 | } |
| 886 | |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 887 | /** |
| 888 | * netlbl_unlabel_staticadd - Handle a STATICADD message |
| 889 | * @skb: the NETLINK buffer |
| 890 | * @info: the Generic NETLINK info block |
| 891 | * |
| 892 | * Description: |
| 893 | * Process a user generated STATICADD message and add a new unlabeled |
| 894 | * connection entry to the hash table. Returns zero on success, negative |
| 895 | * values on failure. |
| 896 | * |
| 897 | */ |
| 898 | static int netlbl_unlabel_staticadd(struct sk_buff *skb, |
| 899 | struct genl_info *info) |
| 900 | { |
| 901 | int ret_val; |
| 902 | char *dev_name; |
| 903 | void *addr; |
| 904 | void *mask; |
| 905 | u32 addr_len; |
| 906 | u32 secid; |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 907 | struct netlbl_audit audit_info; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 908 | |
| 909 | /* Don't allow users to add both IPv4 and IPv6 addresses for a |
| 910 | * single entry. However, allow users to create two entries, one each |
| 911 | * for IPv4 and IPv4, with the same LSM security context which should |
| 912 | * achieve the same result. */ |
| 913 | if (!info->attrs[NLBL_UNLABEL_A_SECCTX] || |
| 914 | !info->attrs[NLBL_UNLABEL_A_IFACE] || |
| 915 | !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] || |
| 916 | !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^ |
| 917 | (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] || |
| 918 | !info->attrs[NLBL_UNLABEL_A_IPV6MASK]))) |
| 919 | return -EINVAL; |
| 920 | |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 921 | netlbl_netlink_auditinfo(skb, &audit_info); |
| 922 | |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 923 | ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len); |
| 924 | if (ret_val != 0) |
| 925 | return ret_val; |
| 926 | dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]); |
| 927 | ret_val = security_secctx_to_secid( |
| 928 | nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]), |
| 929 | nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]), |
| 930 | &secid); |
| 931 | if (ret_val != 0) |
| 932 | return ret_val; |
| 933 | |
| 934 | return netlbl_unlhsh_add(&init_net, |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 935 | dev_name, addr, mask, addr_len, secid, |
| 936 | &audit_info); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | /** |
| 940 | * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message |
| 941 | * @skb: the NETLINK buffer |
| 942 | * @info: the Generic NETLINK info block |
| 943 | * |
| 944 | * Description: |
| 945 | * Process a user generated STATICADDDEF message and add a new default |
| 946 | * unlabeled connection entry. Returns zero on success, negative values on |
| 947 | * failure. |
| 948 | * |
| 949 | */ |
| 950 | static int netlbl_unlabel_staticadddef(struct sk_buff *skb, |
| 951 | struct genl_info *info) |
| 952 | { |
| 953 | int ret_val; |
| 954 | void *addr; |
| 955 | void *mask; |
| 956 | u32 addr_len; |
| 957 | u32 secid; |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 958 | struct netlbl_audit audit_info; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 959 | |
| 960 | /* Don't allow users to add both IPv4 and IPv6 addresses for a |
| 961 | * single entry. However, allow users to create two entries, one each |
| 962 | * for IPv4 and IPv6, with the same LSM security context which should |
| 963 | * achieve the same result. */ |
| 964 | if (!info->attrs[NLBL_UNLABEL_A_SECCTX] || |
| 965 | !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] || |
| 966 | !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^ |
| 967 | (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] || |
| 968 | !info->attrs[NLBL_UNLABEL_A_IPV6MASK]))) |
| 969 | return -EINVAL; |
| 970 | |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 971 | netlbl_netlink_auditinfo(skb, &audit_info); |
| 972 | |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 973 | ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len); |
| 974 | if (ret_val != 0) |
| 975 | return ret_val; |
| 976 | ret_val = security_secctx_to_secid( |
| 977 | nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]), |
| 978 | nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]), |
| 979 | &secid); |
| 980 | if (ret_val != 0) |
| 981 | return ret_val; |
| 982 | |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 983 | return netlbl_unlhsh_add(&init_net, |
| 984 | NULL, addr, mask, addr_len, secid, |
| 985 | &audit_info); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | /** |
| 989 | * netlbl_unlabel_staticremove - Handle a STATICREMOVE message |
| 990 | * @skb: the NETLINK buffer |
| 991 | * @info: the Generic NETLINK info block |
| 992 | * |
| 993 | * Description: |
| 994 | * Process a user generated STATICREMOVE message and remove the specified |
| 995 | * unlabeled connection entry. Returns zero on success, negative values on |
| 996 | * failure. |
| 997 | * |
| 998 | */ |
| 999 | static int netlbl_unlabel_staticremove(struct sk_buff *skb, |
| 1000 | struct genl_info *info) |
| 1001 | { |
| 1002 | int ret_val; |
| 1003 | char *dev_name; |
| 1004 | void *addr; |
| 1005 | void *mask; |
| 1006 | u32 addr_len; |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 1007 | struct netlbl_audit audit_info; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1008 | |
| 1009 | /* See the note in netlbl_unlabel_staticadd() about not allowing both |
| 1010 | * IPv4 and IPv6 in the same entry. */ |
| 1011 | if (!info->attrs[NLBL_UNLABEL_A_IFACE] || |
| 1012 | !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] || |
| 1013 | !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^ |
| 1014 | (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] || |
| 1015 | !info->attrs[NLBL_UNLABEL_A_IPV6MASK]))) |
| 1016 | return -EINVAL; |
| 1017 | |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 1018 | netlbl_netlink_auditinfo(skb, &audit_info); |
| 1019 | |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1020 | ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len); |
| 1021 | if (ret_val != 0) |
| 1022 | return ret_val; |
| 1023 | dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]); |
| 1024 | |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 1025 | return netlbl_unlhsh_remove(&init_net, |
| 1026 | dev_name, addr, mask, addr_len, |
| 1027 | &audit_info); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message |
| 1032 | * @skb: the NETLINK buffer |
| 1033 | * @info: the Generic NETLINK info block |
| 1034 | * |
| 1035 | * Description: |
| 1036 | * Process a user generated STATICREMOVEDEF message and remove the default |
| 1037 | * unlabeled connection entry. Returns zero on success, negative values on |
| 1038 | * failure. |
| 1039 | * |
| 1040 | */ |
| 1041 | static int netlbl_unlabel_staticremovedef(struct sk_buff *skb, |
| 1042 | struct genl_info *info) |
| 1043 | { |
| 1044 | int ret_val; |
| 1045 | void *addr; |
| 1046 | void *mask; |
| 1047 | u32 addr_len; |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 1048 | struct netlbl_audit audit_info; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1049 | |
| 1050 | /* See the note in netlbl_unlabel_staticadd() about not allowing both |
| 1051 | * IPv4 and IPv6 in the same entry. */ |
| 1052 | if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] || |
| 1053 | !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^ |
| 1054 | (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] || |
| 1055 | !info->attrs[NLBL_UNLABEL_A_IPV6MASK]))) |
| 1056 | return -EINVAL; |
| 1057 | |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 1058 | netlbl_netlink_auditinfo(skb, &audit_info); |
| 1059 | |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1060 | ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len); |
| 1061 | if (ret_val != 0) |
| 1062 | return ret_val; |
| 1063 | |
Paul Moore | 13541b3 | 2008-01-29 08:44:23 -0500 | [diff] [blame] | 1064 | return netlbl_unlhsh_remove(&init_net, |
| 1065 | NULL, addr, mask, addr_len, |
| 1066 | &audit_info); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | |
| 1070 | /** |
| 1071 | * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF] |
| 1072 | * @cmd: command/message |
| 1073 | * @iface: the interface entry |
| 1074 | * @addr4: the IPv4 address entry |
| 1075 | * @addr6: the IPv6 address entry |
| 1076 | * @arg: the netlbl_unlhsh_walk_arg structure |
| 1077 | * |
| 1078 | * Description: |
| 1079 | * This function is designed to be used to generate a response for a |
| 1080 | * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6 |
| 1081 | * can be specified, not both, the other unspecified entry should be set to |
| 1082 | * NULL by the caller. Returns the size of the message on success, negative |
| 1083 | * values on failure. |
| 1084 | * |
| 1085 | */ |
| 1086 | static int netlbl_unlabel_staticlist_gen(u32 cmd, |
| 1087 | const struct netlbl_unlhsh_iface *iface, |
| 1088 | const struct netlbl_unlhsh_addr4 *addr4, |
| 1089 | const struct netlbl_unlhsh_addr6 *addr6, |
| 1090 | void *arg) |
| 1091 | { |
| 1092 | int ret_val = -ENOMEM; |
| 1093 | struct netlbl_unlhsh_walk_arg *cb_arg = arg; |
| 1094 | struct net_device *dev; |
| 1095 | void *data; |
| 1096 | u32 secid; |
| 1097 | char *secctx; |
| 1098 | u32 secctx_len; |
| 1099 | |
| 1100 | data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid, |
| 1101 | cb_arg->seq, &netlbl_unlabel_gnl_family, |
| 1102 | NLM_F_MULTI, cmd); |
| 1103 | if (data == NULL) |
| 1104 | goto list_cb_failure; |
| 1105 | |
| 1106 | if (iface->ifindex > 0) { |
| 1107 | dev = dev_get_by_index(&init_net, iface->ifindex); |
Jesper Juhl | 794eb6b | 2008-04-17 23:22:54 -0700 | [diff] [blame] | 1108 | if (!dev) { |
| 1109 | ret_val = -ENODEV; |
| 1110 | goto list_cb_failure; |
| 1111 | } |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1112 | ret_val = nla_put_string(cb_arg->skb, |
| 1113 | NLBL_UNLABEL_A_IFACE, dev->name); |
| 1114 | dev_put(dev); |
| 1115 | if (ret_val != 0) |
| 1116 | goto list_cb_failure; |
| 1117 | } |
| 1118 | |
| 1119 | if (addr4) { |
| 1120 | struct in_addr addr_struct; |
| 1121 | |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1122 | addr_struct.s_addr = addr4->list.addr; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1123 | ret_val = nla_put(cb_arg->skb, |
| 1124 | NLBL_UNLABEL_A_IPV4ADDR, |
| 1125 | sizeof(struct in_addr), |
| 1126 | &addr_struct); |
| 1127 | if (ret_val != 0) |
| 1128 | goto list_cb_failure; |
| 1129 | |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1130 | addr_struct.s_addr = addr4->list.mask; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1131 | ret_val = nla_put(cb_arg->skb, |
| 1132 | NLBL_UNLABEL_A_IPV4MASK, |
| 1133 | sizeof(struct in_addr), |
| 1134 | &addr_struct); |
| 1135 | if (ret_val != 0) |
| 1136 | goto list_cb_failure; |
| 1137 | |
| 1138 | secid = addr4->secid; |
| 1139 | } else { |
| 1140 | ret_val = nla_put(cb_arg->skb, |
| 1141 | NLBL_UNLABEL_A_IPV6ADDR, |
| 1142 | sizeof(struct in6_addr), |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1143 | &addr6->list.addr); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1144 | if (ret_val != 0) |
| 1145 | goto list_cb_failure; |
| 1146 | |
| 1147 | ret_val = nla_put(cb_arg->skb, |
| 1148 | NLBL_UNLABEL_A_IPV6MASK, |
| 1149 | sizeof(struct in6_addr), |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1150 | &addr6->list.mask); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1151 | if (ret_val != 0) |
| 1152 | goto list_cb_failure; |
| 1153 | |
| 1154 | secid = addr6->secid; |
| 1155 | } |
| 1156 | |
| 1157 | ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len); |
| 1158 | if (ret_val != 0) |
| 1159 | goto list_cb_failure; |
| 1160 | ret_val = nla_put(cb_arg->skb, |
| 1161 | NLBL_UNLABEL_A_SECCTX, |
| 1162 | secctx_len, |
| 1163 | secctx); |
| 1164 | security_release_secctx(secctx, secctx_len); |
| 1165 | if (ret_val != 0) |
| 1166 | goto list_cb_failure; |
| 1167 | |
| 1168 | cb_arg->seq++; |
| 1169 | return genlmsg_end(cb_arg->skb, data); |
| 1170 | |
| 1171 | list_cb_failure: |
| 1172 | genlmsg_cancel(cb_arg->skb, data); |
| 1173 | return ret_val; |
| 1174 | } |
| 1175 | |
| 1176 | /** |
| 1177 | * netlbl_unlabel_staticlist - Handle a STATICLIST message |
| 1178 | * @skb: the NETLINK buffer |
| 1179 | * @cb: the NETLINK callback |
| 1180 | * |
| 1181 | * Description: |
| 1182 | * Process a user generated STATICLIST message and dump the unlabeled |
| 1183 | * connection hash table in a form suitable for use in a kernel generated |
| 1184 | * STATICLIST message. Returns the length of @skb. |
| 1185 | * |
| 1186 | */ |
| 1187 | static int netlbl_unlabel_staticlist(struct sk_buff *skb, |
| 1188 | struct netlink_callback *cb) |
| 1189 | { |
| 1190 | struct netlbl_unlhsh_walk_arg cb_arg; |
| 1191 | u32 skip_bkt = cb->args[0]; |
| 1192 | u32 skip_chain = cb->args[1]; |
| 1193 | u32 skip_addr4 = cb->args[2]; |
| 1194 | u32 skip_addr6 = cb->args[3]; |
| 1195 | u32 iter_bkt; |
| 1196 | u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0; |
| 1197 | struct netlbl_unlhsh_iface *iface; |
Paul Moore | 5619670 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 1198 | struct list_head *iter_list; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1199 | struct netlbl_af4list *addr4; |
| 1200 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 1201 | struct netlbl_af6list *addr6; |
| 1202 | #endif |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1203 | |
| 1204 | cb_arg.nl_cb = cb; |
| 1205 | cb_arg.skb = skb; |
| 1206 | cb_arg.seq = cb->nlh->nlmsg_seq; |
| 1207 | |
| 1208 | rcu_read_lock(); |
| 1209 | for (iter_bkt = skip_bkt; |
| 1210 | iter_bkt < rcu_dereference(netlbl_unlhsh)->size; |
| 1211 | iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) { |
Paul Moore | 5619670 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 1212 | iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt]; |
| 1213 | list_for_each_entry_rcu(iface, iter_list, list) { |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1214 | if (!iface->valid || |
| 1215 | iter_chain++ < skip_chain) |
| 1216 | continue; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1217 | netlbl_af4list_foreach_rcu(addr4, |
| 1218 | &iface->addr4_list) { |
| 1219 | if (iter_addr4++ < skip_addr4) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1220 | continue; |
| 1221 | if (netlbl_unlabel_staticlist_gen( |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1222 | NLBL_UNLABEL_C_STATICLIST, |
| 1223 | iface, |
| 1224 | netlbl_unlhsh_addr4_entry(addr4), |
| 1225 | NULL, |
| 1226 | &cb_arg) < 0) { |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1227 | iter_addr4--; |
| 1228 | iter_chain--; |
| 1229 | goto unlabel_staticlist_return; |
| 1230 | } |
| 1231 | } |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1232 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 1233 | netlbl_af6list_foreach_rcu(addr6, |
| 1234 | &iface->addr6_list) { |
| 1235 | if (iter_addr6++ < skip_addr6) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1236 | continue; |
| 1237 | if (netlbl_unlabel_staticlist_gen( |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1238 | NLBL_UNLABEL_C_STATICLIST, |
| 1239 | iface, |
| 1240 | NULL, |
| 1241 | netlbl_unlhsh_addr6_entry(addr6), |
| 1242 | &cb_arg) < 0) { |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1243 | iter_addr6--; |
| 1244 | iter_chain--; |
| 1245 | goto unlabel_staticlist_return; |
| 1246 | } |
| 1247 | } |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1248 | #endif /* IPv6 */ |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | unlabel_staticlist_return: |
| 1253 | rcu_read_unlock(); |
| 1254 | cb->args[0] = skip_bkt; |
| 1255 | cb->args[1] = skip_chain; |
| 1256 | cb->args[2] = skip_addr4; |
| 1257 | cb->args[3] = skip_addr6; |
| 1258 | return skb->len; |
| 1259 | } |
| 1260 | |
| 1261 | /** |
| 1262 | * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message |
| 1263 | * @skb: the NETLINK buffer |
| 1264 | * @cb: the NETLINK callback |
| 1265 | * |
| 1266 | * Description: |
| 1267 | * Process a user generated STATICLISTDEF message and dump the default |
| 1268 | * unlabeled connection entry in a form suitable for use in a kernel generated |
| 1269 | * STATICLISTDEF message. Returns the length of @skb. |
| 1270 | * |
| 1271 | */ |
| 1272 | static int netlbl_unlabel_staticlistdef(struct sk_buff *skb, |
| 1273 | struct netlink_callback *cb) |
| 1274 | { |
| 1275 | struct netlbl_unlhsh_walk_arg cb_arg; |
| 1276 | struct netlbl_unlhsh_iface *iface; |
| 1277 | u32 skip_addr4 = cb->args[0]; |
| 1278 | u32 skip_addr6 = cb->args[1]; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1279 | u32 iter_addr4 = 0; |
| 1280 | struct netlbl_af4list *addr4; |
| 1281 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 1282 | u32 iter_addr6 = 0; |
| 1283 | struct netlbl_af6list *addr6; |
| 1284 | #endif |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1285 | |
| 1286 | cb_arg.nl_cb = cb; |
| 1287 | cb_arg.skb = skb; |
| 1288 | cb_arg.seq = cb->nlh->nlmsg_seq; |
| 1289 | |
| 1290 | rcu_read_lock(); |
| 1291 | iface = rcu_dereference(netlbl_unlhsh_def); |
| 1292 | if (iface == NULL || !iface->valid) |
| 1293 | goto unlabel_staticlistdef_return; |
| 1294 | |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1295 | netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) { |
| 1296 | if (iter_addr4++ < skip_addr4) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1297 | continue; |
| 1298 | if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF, |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1299 | iface, |
| 1300 | netlbl_unlhsh_addr4_entry(addr4), |
| 1301 | NULL, |
| 1302 | &cb_arg) < 0) { |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1303 | iter_addr4--; |
| 1304 | goto unlabel_staticlistdef_return; |
| 1305 | } |
| 1306 | } |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1307 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 1308 | netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) { |
| 1309 | if (iter_addr6++ < skip_addr6) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1310 | continue; |
| 1311 | if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF, |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1312 | iface, |
| 1313 | NULL, |
| 1314 | netlbl_unlhsh_addr6_entry(addr6), |
| 1315 | &cb_arg) < 0) { |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1316 | iter_addr6--; |
| 1317 | goto unlabel_staticlistdef_return; |
| 1318 | } |
| 1319 | } |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1320 | #endif /* IPv6 */ |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1321 | |
| 1322 | unlabel_staticlistdef_return: |
| 1323 | rcu_read_unlock(); |
| 1324 | cb->args[0] = skip_addr4; |
| 1325 | cb->args[1] = skip_addr6; |
| 1326 | return skb->len; |
| 1327 | } |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1328 | |
| 1329 | /* |
| 1330 | * NetLabel Generic NETLINK Command Definitions |
| 1331 | */ |
| 1332 | |
Pavel Emelyanov | 227c43c | 2008-02-17 22:33:16 -0800 | [diff] [blame] | 1333 | static struct genl_ops netlbl_unlabel_genl_ops[] = { |
| 1334 | { |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1335 | .cmd = NLBL_UNLABEL_C_STATICADD, |
| 1336 | .flags = GENL_ADMIN_PERM, |
| 1337 | .policy = netlbl_unlabel_genl_policy, |
| 1338 | .doit = netlbl_unlabel_staticadd, |
| 1339 | .dumpit = NULL, |
Pavel Emelyanov | 227c43c | 2008-02-17 22:33:16 -0800 | [diff] [blame] | 1340 | }, |
| 1341 | { |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1342 | .cmd = NLBL_UNLABEL_C_STATICREMOVE, |
| 1343 | .flags = GENL_ADMIN_PERM, |
| 1344 | .policy = netlbl_unlabel_genl_policy, |
| 1345 | .doit = netlbl_unlabel_staticremove, |
| 1346 | .dumpit = NULL, |
Pavel Emelyanov | 227c43c | 2008-02-17 22:33:16 -0800 | [diff] [blame] | 1347 | }, |
| 1348 | { |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1349 | .cmd = NLBL_UNLABEL_C_STATICLIST, |
| 1350 | .flags = 0, |
| 1351 | .policy = netlbl_unlabel_genl_policy, |
| 1352 | .doit = NULL, |
| 1353 | .dumpit = netlbl_unlabel_staticlist, |
Pavel Emelyanov | 227c43c | 2008-02-17 22:33:16 -0800 | [diff] [blame] | 1354 | }, |
| 1355 | { |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1356 | .cmd = NLBL_UNLABEL_C_STATICADDDEF, |
| 1357 | .flags = GENL_ADMIN_PERM, |
| 1358 | .policy = netlbl_unlabel_genl_policy, |
| 1359 | .doit = netlbl_unlabel_staticadddef, |
| 1360 | .dumpit = NULL, |
Pavel Emelyanov | 227c43c | 2008-02-17 22:33:16 -0800 | [diff] [blame] | 1361 | }, |
| 1362 | { |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1363 | .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF, |
| 1364 | .flags = GENL_ADMIN_PERM, |
| 1365 | .policy = netlbl_unlabel_genl_policy, |
| 1366 | .doit = netlbl_unlabel_staticremovedef, |
| 1367 | .dumpit = NULL, |
Pavel Emelyanov | 227c43c | 2008-02-17 22:33:16 -0800 | [diff] [blame] | 1368 | }, |
| 1369 | { |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1370 | .cmd = NLBL_UNLABEL_C_STATICLISTDEF, |
| 1371 | .flags = 0, |
| 1372 | .policy = netlbl_unlabel_genl_policy, |
| 1373 | .doit = NULL, |
| 1374 | .dumpit = netlbl_unlabel_staticlistdef, |
Pavel Emelyanov | 227c43c | 2008-02-17 22:33:16 -0800 | [diff] [blame] | 1375 | }, |
| 1376 | { |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1377 | .cmd = NLBL_UNLABEL_C_ACCEPT, |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame] | 1378 | .flags = GENL_ADMIN_PERM, |
| 1379 | .policy = netlbl_unlabel_genl_policy, |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1380 | .doit = netlbl_unlabel_accept, |
| 1381 | .dumpit = NULL, |
Pavel Emelyanov | 227c43c | 2008-02-17 22:33:16 -0800 | [diff] [blame] | 1382 | }, |
| 1383 | { |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1384 | .cmd = NLBL_UNLABEL_C_LIST, |
| 1385 | .flags = 0, |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame] | 1386 | .policy = netlbl_unlabel_genl_policy, |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1387 | .doit = netlbl_unlabel_list, |
| 1388 | .dumpit = NULL, |
Pavel Emelyanov | 227c43c | 2008-02-17 22:33:16 -0800 | [diff] [blame] | 1389 | }, |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1390 | }; |
| 1391 | |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1392 | /* |
| 1393 | * NetLabel Generic NETLINK Protocol Functions |
| 1394 | */ |
| 1395 | |
| 1396 | /** |
| 1397 | * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component |
| 1398 | * |
| 1399 | * Description: |
| 1400 | * Register the unlabeled packet NetLabel component with the Generic NETLINK |
| 1401 | * mechanism. Returns zero on success, negative values on failure. |
| 1402 | * |
| 1403 | */ |
Pavel Emelyanov | 05705e4 | 2008-02-17 22:33:57 -0800 | [diff] [blame] | 1404 | int __init netlbl_unlabel_genl_init(void) |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1405 | { |
Michał Mirosław | 7ae740d | 2009-05-21 10:34:05 +0000 | [diff] [blame] | 1406 | return genl_register_family_with_ops(&netlbl_unlabel_gnl_family, |
| 1407 | netlbl_unlabel_genl_ops, ARRAY_SIZE(netlbl_unlabel_genl_ops)); |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | /* |
| 1411 | * NetLabel KAPI Hooks |
| 1412 | */ |
| 1413 | |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1414 | static struct notifier_block netlbl_unlhsh_netdev_notifier = { |
| 1415 | .notifier_call = netlbl_unlhsh_netdev_handler, |
| 1416 | }; |
| 1417 | |
| 1418 | /** |
| 1419 | * netlbl_unlabel_init - Initialize the unlabeled connection hash table |
| 1420 | * @size: the number of bits to use for the hash buckets |
| 1421 | * |
| 1422 | * Description: |
| 1423 | * Initializes the unlabeled connection hash table and registers a network |
| 1424 | * device notification handler. This function should only be called by the |
| 1425 | * NetLabel subsystem itself during initialization. Returns zero on success, |
| 1426 | * non-zero values on error. |
| 1427 | * |
| 1428 | */ |
Pavel Emelyanov | 05705e4 | 2008-02-17 22:33:57 -0800 | [diff] [blame] | 1429 | int __init netlbl_unlabel_init(u32 size) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1430 | { |
| 1431 | u32 iter; |
| 1432 | struct netlbl_unlhsh_tbl *hsh_tbl; |
| 1433 | |
| 1434 | if (size == 0) |
| 1435 | return -EINVAL; |
| 1436 | |
| 1437 | hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL); |
| 1438 | if (hsh_tbl == NULL) |
| 1439 | return -ENOMEM; |
| 1440 | hsh_tbl->size = 1 << size; |
| 1441 | hsh_tbl->tbl = kcalloc(hsh_tbl->size, |
| 1442 | sizeof(struct list_head), |
| 1443 | GFP_KERNEL); |
| 1444 | if (hsh_tbl->tbl == NULL) { |
| 1445 | kfree(hsh_tbl); |
| 1446 | return -ENOMEM; |
| 1447 | } |
| 1448 | for (iter = 0; iter < hsh_tbl->size; iter++) |
| 1449 | INIT_LIST_HEAD(&hsh_tbl->tbl[iter]); |
| 1450 | |
| 1451 | rcu_read_lock(); |
| 1452 | spin_lock(&netlbl_unlhsh_lock); |
| 1453 | rcu_assign_pointer(netlbl_unlhsh, hsh_tbl); |
| 1454 | spin_unlock(&netlbl_unlhsh_lock); |
| 1455 | rcu_read_unlock(); |
| 1456 | |
| 1457 | register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier); |
| 1458 | |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1462 | /** |
| 1463 | * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1464 | * @skb: the packet |
| 1465 | * @family: protocol family |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1466 | * @secattr: the security attributes |
| 1467 | * |
| 1468 | * Description: |
| 1469 | * Determine the security attributes, if any, for an unlabled packet and return |
| 1470 | * them in @secattr. Returns zero on success and negative values on failure. |
| 1471 | * |
| 1472 | */ |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1473 | int netlbl_unlabel_getattr(const struct sk_buff *skb, |
| 1474 | u16 family, |
| 1475 | struct netlbl_lsm_secattr *secattr) |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1476 | { |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1477 | struct netlbl_unlhsh_iface *iface; |
| 1478 | |
| 1479 | rcu_read_lock(); |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 1480 | iface = netlbl_unlhsh_search_iface(skb->skb_iif); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1481 | if (iface == NULL) |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 1482 | iface = rcu_dereference(netlbl_unlhsh_def); |
| 1483 | if (iface == NULL || !iface->valid) |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1484 | goto unlabel_getattr_nolabel; |
| 1485 | switch (family) { |
Pavel Emelyanov | 56628b1 | 2008-02-12 22:37:19 -0800 | [diff] [blame] | 1486 | case PF_INET: { |
| 1487 | struct iphdr *hdr4; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1488 | struct netlbl_af4list *addr4; |
Pavel Emelyanov | 56628b1 | 2008-02-12 22:37:19 -0800 | [diff] [blame] | 1489 | |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1490 | hdr4 = ip_hdr(skb); |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1491 | addr4 = netlbl_af4list_search(hdr4->saddr, |
| 1492 | &iface->addr4_list); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1493 | if (addr4 == NULL) |
| 1494 | goto unlabel_getattr_nolabel; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1495 | secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1496 | break; |
Pavel Emelyanov | 56628b1 | 2008-02-12 22:37:19 -0800 | [diff] [blame] | 1497 | } |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1498 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
Pavel Emelyanov | 56628b1 | 2008-02-12 22:37:19 -0800 | [diff] [blame] | 1499 | case PF_INET6: { |
| 1500 | struct ipv6hdr *hdr6; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1501 | struct netlbl_af6list *addr6; |
Pavel Emelyanov | 56628b1 | 2008-02-12 22:37:19 -0800 | [diff] [blame] | 1502 | |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1503 | hdr6 = ipv6_hdr(skb); |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1504 | addr6 = netlbl_af6list_search(&hdr6->saddr, |
| 1505 | &iface->addr6_list); |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1506 | if (addr6 == NULL) |
| 1507 | goto unlabel_getattr_nolabel; |
Paul Moore | 61e1068 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1508 | secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid; |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1509 | break; |
Pavel Emelyanov | 56628b1 | 2008-02-12 22:37:19 -0800 | [diff] [blame] | 1510 | } |
Paul Moore | 8cc4457 | 2008-01-29 08:44:21 -0500 | [diff] [blame] | 1511 | #endif /* IPv6 */ |
| 1512 | default: |
| 1513 | goto unlabel_getattr_nolabel; |
| 1514 | } |
| 1515 | rcu_read_unlock(); |
| 1516 | |
| 1517 | secattr->flags |= NETLBL_SECATTR_SECID; |
| 1518 | secattr->type = NETLBL_NLTYPE_UNLABELED; |
| 1519 | return 0; |
| 1520 | |
| 1521 | unlabel_getattr_nolabel: |
| 1522 | rcu_read_unlock(); |
Paul Moore | c783f1c | 2008-01-29 08:37:52 -0500 | [diff] [blame] | 1523 | if (netlabel_unlabel_acceptflg == 0) |
| 1524 | return -ENOMSG; |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1525 | secattr->type = NETLBL_NLTYPE_UNLABELED; |
Paul Moore | c783f1c | 2008-01-29 08:37:52 -0500 | [diff] [blame] | 1526 | return 0; |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /** |
| 1530 | * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets |
| 1531 | * |
| 1532 | * Description: |
| 1533 | * Set the default NetLabel configuration to allow incoming unlabeled packets |
| 1534 | * and to send unlabeled network traffic by default. |
| 1535 | * |
| 1536 | */ |
Pavel Emelyanov | 05705e4 | 2008-02-17 22:33:57 -0800 | [diff] [blame] | 1537 | int __init netlbl_unlabel_defconf(void) |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1538 | { |
| 1539 | int ret_val; |
| 1540 | struct netlbl_dom_map *entry; |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 1541 | struct netlbl_audit audit_info; |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 1542 | |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 1543 | /* Only the kernel is allowed to call this function and the only time |
| 1544 | * it is called is at bootup before the audit subsystem is reporting |
| 1545 | * messages so don't worry to much about these values. */ |
| 1546 | security_task_getsecid(current, &audit_info.secid); |
| 1547 | audit_info.loginuid = 0; |
Eric Paris | 2532386 | 2008-04-18 10:09:25 -0400 | [diff] [blame] | 1548 | audit_info.sessionid = 0; |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1549 | |
| 1550 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 1551 | if (entry == NULL) |
| 1552 | return -ENOMEM; |
| 1553 | entry->type = NETLBL_NLTYPE_UNLABELED; |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 1554 | ret_val = netlbl_domhsh_add_default(entry, &audit_info); |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1555 | if (ret_val != 0) |
| 1556 | return ret_val; |
| 1557 | |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 1558 | netlbl_unlabel_acceptflg_set(1, &audit_info); |
Paul Moore | 96cb8e3 | 2006-08-03 16:48:59 -0700 | [diff] [blame] | 1559 | |
| 1560 | return 0; |
| 1561 | } |