2 * NetLabel Unlabeled Support
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.
8 * Author: Paul Moore <paul.moore@hp.com>
13 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
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.
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.
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
31 #include <linux/types.h>
32 #include <linux/rcupdate.h>
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>
39 #include <net/netlink.h>
40 #include <net/genetlink.h>
42 #include <net/netlabel.h>
45 #include "netlabel_user.h"
46 #include "netlabel_domainhash.h"
47 #include "netlabel_unlabeled.h"
49 /* Accept unlabeled packets flag */
50 static DEFINE_SPINLOCK(netlabel_unlabel_acceptflg_lock
);
51 static u8 netlabel_unlabel_acceptflg
= 0;
53 /* NetLabel Generic NETLINK CIPSOv4 family */
54 static struct genl_family netlbl_unlabel_gnl_family
= {
55 .id
= GENL_ID_GENERATE
,
57 .name
= NETLBL_NLTYPE_UNLABELED_NAME
,
58 .version
= NETLBL_PROTO_VERSION
,
59 .maxattr
= NLBL_UNLABEL_A_MAX
,
62 /* NetLabel Netlink attribute policy */
63 static struct nla_policy netlbl_unlabel_genl_policy
[NLBL_UNLABEL_A_MAX
+ 1] = {
64 [NLBL_UNLABEL_A_ACPTFLG
] = { .type
= NLA_U8
},
72 * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
73 * @value: desired value
74 * @audit_info: NetLabel audit information
77 * Set the value of the unlabeled accept flag to @value.
80 static void netlbl_unlabel_acceptflg_set(u8 value
,
81 struct netlbl_audit
*audit_info
)
83 struct audit_buffer
*audit_buf
;
87 old_val
= netlabel_unlabel_acceptflg
;
88 spin_lock(&netlabel_unlabel_acceptflg_lock
);
89 netlabel_unlabel_acceptflg
= value
;
90 spin_unlock(&netlabel_unlabel_acceptflg_lock
);
93 audit_buf
= netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW
,
95 audit_log_format(audit_buf
, " unlbl_accept=%u old=%u", value
, old_val
);
96 audit_log_end(audit_buf
);
100 * NetLabel Command Handlers
104 * netlbl_unlabel_accept - Handle an ACCEPT message
105 * @skb: the NETLINK buffer
106 * @info: the Generic NETLINK info block
109 * Process a user generated ACCEPT message and set the accept flag accordingly.
110 * Returns zero on success, negative values on failure.
113 static int netlbl_unlabel_accept(struct sk_buff
*skb
, struct genl_info
*info
)
116 struct netlbl_audit audit_info
;
118 if (info
->attrs
[NLBL_UNLABEL_A_ACPTFLG
]) {
119 value
= nla_get_u8(info
->attrs
[NLBL_UNLABEL_A_ACPTFLG
]);
120 if (value
== 1 || value
== 0) {
121 netlbl_netlink_auditinfo(skb
, &audit_info
);
122 netlbl_unlabel_acceptflg_set(value
, &audit_info
);
131 * netlbl_unlabel_list - Handle a LIST message
132 * @skb: the NETLINK buffer
133 * @info: the Generic NETLINK info block
136 * Process a user generated LIST message and respond with the current status.
137 * Returns zero on success, negative values on failure.
140 static int netlbl_unlabel_list(struct sk_buff
*skb
, struct genl_info
*info
)
142 int ret_val
= -EINVAL
;
143 struct sk_buff
*ans_skb
;
146 ans_skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
149 data
= genlmsg_put_reply(ans_skb
, info
, &netlbl_unlabel_gnl_family
,
150 0, NLBL_UNLABEL_C_LIST
);
157 ret_val
= nla_put_u8(ans_skb
,
158 NLBL_UNLABEL_A_ACPTFLG
,
159 netlabel_unlabel_acceptflg
);
164 genlmsg_end(ans_skb
, data
);
166 ret_val
= genlmsg_reply(ans_skb
, info
);
178 * NetLabel Generic NETLINK Command Definitions
181 static struct genl_ops netlbl_unlabel_genl_c_accept
= {
182 .cmd
= NLBL_UNLABEL_C_ACCEPT
,
183 .flags
= GENL_ADMIN_PERM
,
184 .policy
= netlbl_unlabel_genl_policy
,
185 .doit
= netlbl_unlabel_accept
,
189 static struct genl_ops netlbl_unlabel_genl_c_list
= {
190 .cmd
= NLBL_UNLABEL_C_LIST
,
192 .policy
= netlbl_unlabel_genl_policy
,
193 .doit
= netlbl_unlabel_list
,
199 * NetLabel Generic NETLINK Protocol Functions
203 * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
206 * Register the unlabeled packet NetLabel component with the Generic NETLINK
207 * mechanism. Returns zero on success, negative values on failure.
210 int netlbl_unlabel_genl_init(void)
214 ret_val
= genl_register_family(&netlbl_unlabel_gnl_family
);
218 ret_val
= genl_register_ops(&netlbl_unlabel_gnl_family
,
219 &netlbl_unlabel_genl_c_accept
);
223 ret_val
= genl_register_ops(&netlbl_unlabel_gnl_family
,
224 &netlbl_unlabel_genl_c_list
);
232 * NetLabel KAPI Hooks
236 * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
237 * @secattr: the security attributes
240 * Determine the security attributes, if any, for an unlabled packet and return
241 * them in @secattr. Returns zero on success and negative values on failure.
244 int netlbl_unlabel_getattr(struct netlbl_lsm_secattr
*secattr
)
249 if (netlabel_unlabel_acceptflg
== 1) {
250 netlbl_secattr_init(secattr
);
260 * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
263 * Set the default NetLabel configuration to allow incoming unlabeled packets
264 * and to send unlabeled network traffic by default.
267 int netlbl_unlabel_defconf(void)
270 struct netlbl_dom_map
*entry
;
271 struct netlbl_audit audit_info
;
273 /* Only the kernel is allowed to call this function and the only time
274 * it is called is at bootup before the audit subsystem is reporting
275 * messages so don't worry to much about these values. */
276 security_task_getsecid(current
, &audit_info
.secid
);
277 audit_info
.loginuid
= 0;
279 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
282 entry
->type
= NETLBL_NLTYPE_UNLABELED
;
283 ret_val
= netlbl_domhsh_add_default(entry
, &audit_info
);
287 netlbl_unlabel_acceptflg_set(1, &audit_info
);