Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NetLabel Management Support |
| 3 | * |
| 4 | * This file defines the management functions for the NetLabel system. The |
| 5 | * NetLabel system manages static and dynamic label mappings for network |
| 6 | * protocols such as CIPSO and RIPSO. |
| 7 | * |
| 8 | * Author: Paul Moore <paul.moore@hp.com> |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 |
| 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> |
| 32 | #include <linux/socket.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <linux/skbuff.h> |
| 35 | #include <net/sock.h> |
| 36 | #include <net/netlink.h> |
| 37 | #include <net/genetlink.h> |
| 38 | #include <net/netlabel.h> |
| 39 | #include <net/cipso_ipv4.h> |
| 40 | |
| 41 | #include "netlabel_domainhash.h" |
| 42 | #include "netlabel_user.h" |
| 43 | #include "netlabel_mgmt.h" |
| 44 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 45 | /* Argument struct for netlbl_domhsh_walk() */ |
| 46 | struct netlbl_domhsh_walk_arg { |
| 47 | struct netlink_callback *nl_cb; |
| 48 | struct sk_buff *skb; |
| 49 | u32 seq; |
| 50 | }; |
| 51 | |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 52 | /* NetLabel Generic NETLINK CIPSOv4 family */ |
| 53 | static struct genl_family netlbl_mgmt_gnl_family = { |
| 54 | .id = GENL_ID_GENERATE, |
| 55 | .hdrsize = 0, |
| 56 | .name = NETLBL_NLTYPE_MGMT_NAME, |
| 57 | .version = NETLBL_PROTO_VERSION, |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 58 | .maxattr = NLBL_MGMT_A_MAX, |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 61 | /* NetLabel Netlink attribute policy */ |
| 62 | static struct nla_policy netlbl_mgmt_genl_policy[NLBL_MGMT_A_MAX + 1] = { |
| 63 | [NLBL_MGMT_A_DOMAIN] = { .type = NLA_NUL_STRING }, |
| 64 | [NLBL_MGMT_A_PROTOCOL] = { .type = NLA_U32 }, |
| 65 | [NLBL_MGMT_A_VERSION] = { .type = NLA_U32 }, |
| 66 | [NLBL_MGMT_A_CV4DOI] = { .type = NLA_U32 }, |
| 67 | }; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * NetLabel Command Handlers |
| 71 | */ |
| 72 | |
| 73 | /** |
| 74 | * netlbl_mgmt_add - Handle an ADD message |
| 75 | * @skb: the NETLINK buffer |
| 76 | * @info: the Generic NETLINK info block |
| 77 | * |
| 78 | * Description: |
| 79 | * Process a user generated ADD message and add the domains from the message |
| 80 | * to the hash table. See netlabel.h for a description of the message format. |
| 81 | * Returns zero on success, negative values on failure. |
| 82 | * |
| 83 | */ |
| 84 | static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info) |
| 85 | { |
| 86 | int ret_val = -EINVAL; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 87 | struct netlbl_dom_map *entry = NULL; |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 88 | size_t tmp_size; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 89 | u32 tmp_val; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 90 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 91 | if (!info->attrs[NLBL_MGMT_A_DOMAIN] || |
| 92 | !info->attrs[NLBL_MGMT_A_PROTOCOL]) |
| 93 | goto add_failure; |
| 94 | |
| 95 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 96 | if (entry == NULL) { |
| 97 | ret_val = -ENOMEM; |
| 98 | goto add_failure; |
| 99 | } |
| 100 | tmp_size = nla_len(info->attrs[NLBL_MGMT_A_DOMAIN]); |
| 101 | entry->domain = kmalloc(tmp_size, GFP_KERNEL); |
| 102 | if (entry->domain == NULL) { |
| 103 | ret_val = -ENOMEM; |
| 104 | goto add_failure; |
| 105 | } |
| 106 | entry->type = nla_get_u32(info->attrs[NLBL_MGMT_A_PROTOCOL]); |
| 107 | nla_strlcpy(entry->domain, info->attrs[NLBL_MGMT_A_DOMAIN], tmp_size); |
| 108 | |
| 109 | switch (entry->type) { |
| 110 | case NETLBL_NLTYPE_UNLABELED: |
| 111 | ret_val = netlbl_domhsh_add(entry); |
| 112 | break; |
| 113 | case NETLBL_NLTYPE_CIPSOV4: |
| 114 | if (!info->attrs[NLBL_MGMT_A_CV4DOI]) |
| 115 | goto add_failure; |
| 116 | |
| 117 | tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CV4DOI]); |
| 118 | /* We should be holding a rcu_read_lock() here while we hold |
| 119 | * the result but since the entry will always be deleted when |
| 120 | * the CIPSO DOI is deleted we aren't going to keep the |
| 121 | * lock. */ |
| 122 | rcu_read_lock(); |
| 123 | entry->type_def.cipsov4 = cipso_v4_doi_getdef(tmp_val); |
| 124 | if (entry->type_def.cipsov4 == NULL) { |
| 125 | rcu_read_unlock(); |
| 126 | goto add_failure; |
| 127 | } |
| 128 | ret_val = netlbl_domhsh_add(entry); |
| 129 | rcu_read_unlock(); |
| 130 | break; |
| 131 | default: |
| 132 | goto add_failure; |
| 133 | } |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 134 | if (ret_val != 0) |
| 135 | goto add_failure; |
| 136 | |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 137 | return 0; |
| 138 | |
| 139 | add_failure: |
| 140 | if (entry) |
| 141 | kfree(entry->domain); |
| 142 | kfree(entry); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 143 | return ret_val; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * netlbl_mgmt_remove - Handle a REMOVE message |
| 148 | * @skb: the NETLINK buffer |
| 149 | * @info: the Generic NETLINK info block |
| 150 | * |
| 151 | * Description: |
| 152 | * Process a user generated REMOVE message and remove the specified domain |
| 153 | * mappings. Returns zero on success, negative values on failure. |
| 154 | * |
| 155 | */ |
| 156 | static int netlbl_mgmt_remove(struct sk_buff *skb, struct genl_info *info) |
| 157 | { |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 158 | char *domain; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 159 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 160 | if (!info->attrs[NLBL_MGMT_A_DOMAIN]) |
| 161 | return -EINVAL; |
| 162 | |
| 163 | domain = nla_data(info->attrs[NLBL_MGMT_A_DOMAIN]); |
| 164 | return netlbl_domhsh_remove(domain); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * netlbl_mgmt_listall_cb - netlbl_domhsh_walk() callback for LISTALL |
| 169 | * @entry: the domain mapping hash table entry |
| 170 | * @arg: the netlbl_domhsh_walk_arg structure |
| 171 | * |
| 172 | * Description: |
| 173 | * This function is designed to be used as a callback to the |
| 174 | * netlbl_domhsh_walk() function for use in generating a response for a LISTALL |
| 175 | * message. Returns the size of the message on success, negative values on |
| 176 | * failure. |
| 177 | * |
| 178 | */ |
| 179 | static int netlbl_mgmt_listall_cb(struct netlbl_dom_map *entry, void *arg) |
| 180 | { |
| 181 | int ret_val = -ENOMEM; |
| 182 | struct netlbl_domhsh_walk_arg *cb_arg = arg; |
| 183 | void *data; |
| 184 | |
| 185 | data = netlbl_netlink_hdr_put(cb_arg->skb, |
| 186 | NETLINK_CB(cb_arg->nl_cb->skb).pid, |
| 187 | cb_arg->seq, |
| 188 | netlbl_mgmt_gnl_family.id, |
| 189 | NLM_F_MULTI, |
| 190 | NLBL_MGMT_C_LISTALL); |
| 191 | if (data == NULL) |
| 192 | goto listall_cb_failure; |
| 193 | |
| 194 | ret_val = nla_put_string(cb_arg->skb, |
| 195 | NLBL_MGMT_A_DOMAIN, |
| 196 | entry->domain); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 197 | if (ret_val != 0) |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 198 | goto listall_cb_failure; |
| 199 | ret_val = nla_put_u32(cb_arg->skb, NLBL_MGMT_A_PROTOCOL, entry->type); |
| 200 | if (ret_val != 0) |
| 201 | goto listall_cb_failure; |
| 202 | switch (entry->type) { |
| 203 | case NETLBL_NLTYPE_CIPSOV4: |
| 204 | ret_val = nla_put_u32(cb_arg->skb, |
| 205 | NLBL_MGMT_A_CV4DOI, |
| 206 | entry->type_def.cipsov4->doi); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 207 | if (ret_val != 0) |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 208 | goto listall_cb_failure; |
| 209 | break; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 212 | cb_arg->seq++; |
| 213 | return genlmsg_end(cb_arg->skb, data); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 214 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 215 | listall_cb_failure: |
| 216 | genlmsg_cancel(cb_arg->skb, data); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 217 | return ret_val; |
| 218 | } |
| 219 | |
| 220 | /** |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 221 | * netlbl_mgmt_listall - Handle a LISTALL message |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 222 | * @skb: the NETLINK buffer |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 223 | * @cb: the NETLINK callback |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 224 | * |
| 225 | * Description: |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 226 | * Process a user generated LISTALL message and dumps the domain hash table in |
| 227 | * a form suitable for use in a kernel generated LISTALL message. Returns zero |
| 228 | * on success, negative values on failure. |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 229 | * |
| 230 | */ |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 231 | static int netlbl_mgmt_listall(struct sk_buff *skb, |
| 232 | struct netlink_callback *cb) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 233 | { |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 234 | struct netlbl_domhsh_walk_arg cb_arg; |
| 235 | u32 skip_bkt = cb->args[0]; |
| 236 | u32 skip_chain = cb->args[1]; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 237 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 238 | cb_arg.nl_cb = cb; |
| 239 | cb_arg.skb = skb; |
| 240 | cb_arg.seq = cb->nlh->nlmsg_seq; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 241 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 242 | netlbl_domhsh_walk(&skip_bkt, |
| 243 | &skip_chain, |
| 244 | netlbl_mgmt_listall_cb, |
| 245 | &cb_arg); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 246 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 247 | cb->args[0] = skip_bkt; |
| 248 | cb->args[1] = skip_chain; |
| 249 | return skb->len; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
| 253 | * netlbl_mgmt_adddef - Handle an ADDDEF message |
| 254 | * @skb: the NETLINK buffer |
| 255 | * @info: the Generic NETLINK info block |
| 256 | * |
| 257 | * Description: |
| 258 | * Process a user generated ADDDEF message and respond accordingly. Returns |
| 259 | * zero on success, negative values on failure. |
| 260 | * |
| 261 | */ |
| 262 | static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info) |
| 263 | { |
| 264 | int ret_val = -EINVAL; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 265 | struct netlbl_dom_map *entry = NULL; |
| 266 | u32 tmp_val; |
| 267 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 268 | if (!info->attrs[NLBL_MGMT_A_PROTOCOL]) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 269 | goto adddef_failure; |
| 270 | |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 271 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 272 | if (entry == NULL) { |
| 273 | ret_val = -ENOMEM; |
| 274 | goto adddef_failure; |
| 275 | } |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 276 | entry->type = nla_get_u32(info->attrs[NLBL_MGMT_A_PROTOCOL]); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 277 | |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 278 | switch (entry->type) { |
| 279 | case NETLBL_NLTYPE_UNLABELED: |
| 280 | ret_val = netlbl_domhsh_add_default(entry); |
| 281 | break; |
| 282 | case NETLBL_NLTYPE_CIPSOV4: |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 283 | if (!info->attrs[NLBL_MGMT_A_CV4DOI]) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 284 | goto adddef_failure; |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 285 | |
| 286 | tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CV4DOI]); |
| 287 | /* We should be holding a rcu_read_lock() here while we hold |
| 288 | * the result but since the entry will always be deleted when |
| 289 | * the CIPSO DOI is deleted we aren't going to keep the |
| 290 | * lock. */ |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 291 | rcu_read_lock(); |
| 292 | entry->type_def.cipsov4 = cipso_v4_doi_getdef(tmp_val); |
| 293 | if (entry->type_def.cipsov4 == NULL) { |
| 294 | rcu_read_unlock(); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 295 | goto adddef_failure; |
| 296 | } |
| 297 | ret_val = netlbl_domhsh_add_default(entry); |
| 298 | rcu_read_unlock(); |
| 299 | break; |
| 300 | default: |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 301 | goto adddef_failure; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 302 | } |
| 303 | if (ret_val != 0) |
| 304 | goto adddef_failure; |
| 305 | |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 306 | return 0; |
| 307 | |
| 308 | adddef_failure: |
| 309 | kfree(entry); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 310 | return ret_val; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * netlbl_mgmt_removedef - Handle a REMOVEDEF message |
| 315 | * @skb: the NETLINK buffer |
| 316 | * @info: the Generic NETLINK info block |
| 317 | * |
| 318 | * Description: |
| 319 | * Process a user generated REMOVEDEF message and remove the default domain |
| 320 | * mapping. Returns zero on success, negative values on failure. |
| 321 | * |
| 322 | */ |
| 323 | static int netlbl_mgmt_removedef(struct sk_buff *skb, struct genl_info *info) |
| 324 | { |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 325 | return netlbl_domhsh_remove_default(); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * netlbl_mgmt_listdef - Handle a LISTDEF message |
| 330 | * @skb: the NETLINK buffer |
| 331 | * @info: the Generic NETLINK info block |
| 332 | * |
| 333 | * Description: |
| 334 | * Process a user generated LISTDEF message and dumps the default domain |
| 335 | * mapping in a form suitable for use in a kernel generated LISTDEF message. |
| 336 | * Returns zero on success, negative values on failure. |
| 337 | * |
| 338 | */ |
| 339 | static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info) |
| 340 | { |
| 341 | int ret_val = -ENOMEM; |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 342 | struct sk_buff *ans_skb = NULL; |
| 343 | void *data; |
| 344 | struct netlbl_dom_map *entry; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 345 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 346 | ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 347 | if (ans_skb == NULL) |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 348 | return -ENOMEM; |
| 349 | data = netlbl_netlink_hdr_put(ans_skb, |
| 350 | info->snd_pid, |
| 351 | info->snd_seq, |
| 352 | netlbl_mgmt_gnl_family.id, |
| 353 | 0, |
| 354 | NLBL_MGMT_C_LISTDEF); |
| 355 | if (data == NULL) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 356 | goto listdef_failure; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 357 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 358 | rcu_read_lock(); |
| 359 | entry = netlbl_domhsh_getentry(NULL); |
| 360 | if (entry == NULL) { |
| 361 | ret_val = -ENOENT; |
| 362 | goto listdef_failure_lock; |
| 363 | } |
| 364 | ret_val = nla_put_u32(ans_skb, NLBL_MGMT_A_PROTOCOL, entry->type); |
| 365 | if (ret_val != 0) |
| 366 | goto listdef_failure_lock; |
| 367 | switch (entry->type) { |
| 368 | case NETLBL_NLTYPE_CIPSOV4: |
| 369 | ret_val = nla_put_u32(ans_skb, |
| 370 | NLBL_MGMT_A_CV4DOI, |
| 371 | entry->type_def.cipsov4->doi); |
| 372 | if (ret_val != 0) |
| 373 | goto listdef_failure_lock; |
| 374 | break; |
| 375 | } |
| 376 | rcu_read_unlock(); |
| 377 | |
| 378 | genlmsg_end(ans_skb, data); |
| 379 | |
| 380 | ret_val = genlmsg_unicast(ans_skb, info->snd_pid); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 381 | if (ret_val != 0) |
| 382 | goto listdef_failure; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 383 | return 0; |
| 384 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 385 | listdef_failure_lock: |
| 386 | rcu_read_unlock(); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 387 | listdef_failure: |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 388 | kfree_skb(ans_skb); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 389 | return ret_val; |
| 390 | } |
| 391 | |
| 392 | /** |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 393 | * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response |
| 394 | * @skb: the skb to write to |
| 395 | * @seq: the NETLINK sequence number |
| 396 | * @cb: the NETLINK callback |
| 397 | * @protocol: the NetLabel protocol to use in the message |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 398 | * |
| 399 | * Description: |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 400 | * This function is to be used in conjunction with netlbl_mgmt_protocols() to |
| 401 | * answer a application's PROTOCOLS message. Returns the size of the message |
| 402 | * on success, negative values on failure. |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 403 | * |
| 404 | */ |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 405 | static int netlbl_mgmt_protocols_cb(struct sk_buff *skb, |
| 406 | struct netlink_callback *cb, |
| 407 | u32 protocol) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 408 | { |
| 409 | int ret_val = -ENOMEM; |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 410 | void *data; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 411 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 412 | data = netlbl_netlink_hdr_put(skb, |
| 413 | NETLINK_CB(cb->skb).pid, |
| 414 | cb->nlh->nlmsg_seq, |
| 415 | netlbl_mgmt_gnl_family.id, |
| 416 | NLM_F_MULTI, |
| 417 | NLBL_MGMT_C_PROTOCOLS); |
| 418 | if (data == NULL) |
| 419 | goto protocols_cb_failure; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 420 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 421 | ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, protocol); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 422 | if (ret_val != 0) |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 423 | goto protocols_cb_failure; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 424 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 425 | return genlmsg_end(skb, data); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 426 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 427 | protocols_cb_failure: |
| 428 | genlmsg_cancel(skb, data); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 429 | return ret_val; |
| 430 | } |
| 431 | |
| 432 | /** |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 433 | * netlbl_mgmt_protocols - Handle a PROTOCOLS message |
| 434 | * @skb: the NETLINK buffer |
| 435 | * @cb: the NETLINK callback |
| 436 | * |
| 437 | * Description: |
| 438 | * Process a user generated PROTOCOLS message and respond accordingly. |
| 439 | * |
| 440 | */ |
| 441 | static int netlbl_mgmt_protocols(struct sk_buff *skb, |
| 442 | struct netlink_callback *cb) |
| 443 | { |
| 444 | u32 protos_sent = cb->args[0]; |
| 445 | |
| 446 | if (protos_sent == 0) { |
| 447 | if (netlbl_mgmt_protocols_cb(skb, |
| 448 | cb, |
| 449 | NETLBL_NLTYPE_UNLABELED) < 0) |
| 450 | goto protocols_return; |
| 451 | protos_sent++; |
| 452 | } |
| 453 | if (protos_sent == 1) { |
| 454 | if (netlbl_mgmt_protocols_cb(skb, |
| 455 | cb, |
| 456 | NETLBL_NLTYPE_CIPSOV4) < 0) |
| 457 | goto protocols_return; |
| 458 | protos_sent++; |
| 459 | } |
| 460 | |
| 461 | protocols_return: |
| 462 | cb->args[0] = protos_sent; |
| 463 | return skb->len; |
| 464 | } |
| 465 | |
| 466 | /** |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 467 | * netlbl_mgmt_version - Handle a VERSION message |
| 468 | * @skb: the NETLINK buffer |
| 469 | * @info: the Generic NETLINK info block |
| 470 | * |
| 471 | * Description: |
| 472 | * Process a user generated VERSION message and respond accordingly. Returns |
| 473 | * zero on success, negative values on failure. |
| 474 | * |
| 475 | */ |
| 476 | static int netlbl_mgmt_version(struct sk_buff *skb, struct genl_info *info) |
| 477 | { |
| 478 | int ret_val = -ENOMEM; |
| 479 | struct sk_buff *ans_skb = NULL; |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 480 | void *data; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 481 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 482 | ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 483 | if (ans_skb == NULL) |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 484 | return -ENOMEM; |
| 485 | data = netlbl_netlink_hdr_put(ans_skb, |
| 486 | info->snd_pid, |
| 487 | info->snd_seq, |
| 488 | netlbl_mgmt_gnl_family.id, |
| 489 | 0, |
| 490 | NLBL_MGMT_C_VERSION); |
| 491 | if (data == NULL) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 492 | goto version_failure; |
| 493 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 494 | ret_val = nla_put_u32(ans_skb, |
| 495 | NLBL_MGMT_A_VERSION, |
| 496 | NETLBL_PROTO_VERSION); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 497 | if (ret_val != 0) |
| 498 | goto version_failure; |
| 499 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 500 | genlmsg_end(ans_skb, data); |
| 501 | |
| 502 | ret_val = genlmsg_unicast(ans_skb, info->snd_pid); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 503 | if (ret_val != 0) |
| 504 | goto version_failure; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 505 | return 0; |
| 506 | |
| 507 | version_failure: |
| 508 | kfree_skb(ans_skb); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 509 | return ret_val; |
| 510 | } |
| 511 | |
| 512 | |
| 513 | /* |
| 514 | * NetLabel Generic NETLINK Command Definitions |
| 515 | */ |
| 516 | |
| 517 | static struct genl_ops netlbl_mgmt_genl_c_add = { |
| 518 | .cmd = NLBL_MGMT_C_ADD, |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 519 | .flags = GENL_ADMIN_PERM, |
| 520 | .policy = netlbl_mgmt_genl_policy, |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 521 | .doit = netlbl_mgmt_add, |
| 522 | .dumpit = NULL, |
| 523 | }; |
| 524 | |
| 525 | static struct genl_ops netlbl_mgmt_genl_c_remove = { |
| 526 | .cmd = NLBL_MGMT_C_REMOVE, |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 527 | .flags = GENL_ADMIN_PERM, |
| 528 | .policy = netlbl_mgmt_genl_policy, |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 529 | .doit = netlbl_mgmt_remove, |
| 530 | .dumpit = NULL, |
| 531 | }; |
| 532 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 533 | static struct genl_ops netlbl_mgmt_genl_c_listall = { |
| 534 | .cmd = NLBL_MGMT_C_LISTALL, |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 535 | .flags = 0, |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 536 | .policy = netlbl_mgmt_genl_policy, |
| 537 | .doit = NULL, |
| 538 | .dumpit = netlbl_mgmt_listall, |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 539 | }; |
| 540 | |
| 541 | static struct genl_ops netlbl_mgmt_genl_c_adddef = { |
| 542 | .cmd = NLBL_MGMT_C_ADDDEF, |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 543 | .flags = GENL_ADMIN_PERM, |
| 544 | .policy = netlbl_mgmt_genl_policy, |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 545 | .doit = netlbl_mgmt_adddef, |
| 546 | .dumpit = NULL, |
| 547 | }; |
| 548 | |
| 549 | static struct genl_ops netlbl_mgmt_genl_c_removedef = { |
| 550 | .cmd = NLBL_MGMT_C_REMOVEDEF, |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 551 | .flags = GENL_ADMIN_PERM, |
| 552 | .policy = netlbl_mgmt_genl_policy, |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 553 | .doit = netlbl_mgmt_removedef, |
| 554 | .dumpit = NULL, |
| 555 | }; |
| 556 | |
| 557 | static struct genl_ops netlbl_mgmt_genl_c_listdef = { |
| 558 | .cmd = NLBL_MGMT_C_LISTDEF, |
| 559 | .flags = 0, |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 560 | .policy = netlbl_mgmt_genl_policy, |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 561 | .doit = netlbl_mgmt_listdef, |
| 562 | .dumpit = NULL, |
| 563 | }; |
| 564 | |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 565 | static struct genl_ops netlbl_mgmt_genl_c_protocols = { |
| 566 | .cmd = NLBL_MGMT_C_PROTOCOLS, |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 567 | .flags = 0, |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 568 | .policy = netlbl_mgmt_genl_policy, |
| 569 | .doit = NULL, |
| 570 | .dumpit = netlbl_mgmt_protocols, |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 571 | }; |
| 572 | |
| 573 | static struct genl_ops netlbl_mgmt_genl_c_version = { |
| 574 | .cmd = NLBL_MGMT_C_VERSION, |
| 575 | .flags = 0, |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 576 | .policy = netlbl_mgmt_genl_policy, |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 577 | .doit = netlbl_mgmt_version, |
| 578 | .dumpit = NULL, |
| 579 | }; |
| 580 | |
| 581 | /* |
| 582 | * NetLabel Generic NETLINK Protocol Functions |
| 583 | */ |
| 584 | |
| 585 | /** |
| 586 | * netlbl_mgmt_genl_init - Register the NetLabel management component |
| 587 | * |
| 588 | * Description: |
| 589 | * Register the NetLabel management component with the Generic NETLINK |
| 590 | * mechanism. Returns zero on success, negative values on failure. |
| 591 | * |
| 592 | */ |
| 593 | int netlbl_mgmt_genl_init(void) |
| 594 | { |
| 595 | int ret_val; |
| 596 | |
| 597 | ret_val = genl_register_family(&netlbl_mgmt_gnl_family); |
| 598 | if (ret_val != 0) |
| 599 | return ret_val; |
| 600 | |
| 601 | ret_val = genl_register_ops(&netlbl_mgmt_gnl_family, |
| 602 | &netlbl_mgmt_genl_c_add); |
| 603 | if (ret_val != 0) |
| 604 | return ret_val; |
| 605 | ret_val = genl_register_ops(&netlbl_mgmt_gnl_family, |
| 606 | &netlbl_mgmt_genl_c_remove); |
| 607 | if (ret_val != 0) |
| 608 | return ret_val; |
| 609 | ret_val = genl_register_ops(&netlbl_mgmt_gnl_family, |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 610 | &netlbl_mgmt_genl_c_listall); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 611 | if (ret_val != 0) |
| 612 | return ret_val; |
| 613 | ret_val = genl_register_ops(&netlbl_mgmt_gnl_family, |
| 614 | &netlbl_mgmt_genl_c_adddef); |
| 615 | if (ret_val != 0) |
| 616 | return ret_val; |
| 617 | ret_val = genl_register_ops(&netlbl_mgmt_gnl_family, |
| 618 | &netlbl_mgmt_genl_c_removedef); |
| 619 | if (ret_val != 0) |
| 620 | return ret_val; |
| 621 | ret_val = genl_register_ops(&netlbl_mgmt_gnl_family, |
| 622 | &netlbl_mgmt_genl_c_listdef); |
| 623 | if (ret_val != 0) |
| 624 | return ret_val; |
| 625 | ret_val = genl_register_ops(&netlbl_mgmt_gnl_family, |
Paul Moore | fd38585 | 2006-09-25 15:56:37 -0700 | [diff] [blame^] | 626 | &netlbl_mgmt_genl_c_protocols); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 627 | if (ret_val != 0) |
| 628 | return ret_val; |
| 629 | ret_val = genl_register_ops(&netlbl_mgmt_gnl_family, |
| 630 | &netlbl_mgmt_genl_c_version); |
| 631 | if (ret_val != 0) |
| 632 | return ret_val; |
| 633 | |
| 634 | return 0; |
| 635 | } |