Merge branch 'akpm'
[linux-2.6/next.git] / security / selinux / netlink.c
blob34a012a73f39407c09c417fe27368527c77c64c7
1 /*
2 * Netlink event notifications for SELinux.
4 * Author: James Morris <jmorris@redhat.com>
6 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
12 #include <linux/init.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/stddef.h>
16 #include <linux/kernel.h>
17 #include <linux/export.h>
18 #include <linux/skbuff.h>
19 #include <linux/netlink.h>
20 #include <linux/selinux_netlink.h>
21 #include <net/net_namespace.h>
23 static struct sock *selnl;
25 static int selnl_msglen(int msgtype)
27 int ret = 0;
29 switch (msgtype) {
30 case SELNL_MSG_SETENFORCE:
31 ret = sizeof(struct selnl_msg_setenforce);
32 break;
34 case SELNL_MSG_POLICYLOAD:
35 ret = sizeof(struct selnl_msg_policyload);
36 break;
38 default:
39 BUG();
41 return ret;
44 static void selnl_add_payload(struct nlmsghdr *nlh, int len, int msgtype, void *data)
46 switch (msgtype) {
47 case SELNL_MSG_SETENFORCE: {
48 struct selnl_msg_setenforce *msg = NLMSG_DATA(nlh);
50 memset(msg, 0, len);
51 msg->val = *((int *)data);
52 break;
55 case SELNL_MSG_POLICYLOAD: {
56 struct selnl_msg_policyload *msg = NLMSG_DATA(nlh);
58 memset(msg, 0, len);
59 msg->seqno = *((u32 *)data);
60 break;
63 default:
64 BUG();
68 static void selnl_notify(int msgtype, void *data)
70 int len;
71 sk_buff_data_t tmp;
72 struct sk_buff *skb;
73 struct nlmsghdr *nlh;
75 len = selnl_msglen(msgtype);
77 skb = alloc_skb(NLMSG_SPACE(len), GFP_USER);
78 if (!skb)
79 goto oom;
81 tmp = skb->tail;
82 nlh = NLMSG_PUT(skb, 0, 0, msgtype, len);
83 selnl_add_payload(nlh, len, msgtype, data);
84 nlh->nlmsg_len = skb->tail - tmp;
85 NETLINK_CB(skb).dst_group = SELNLGRP_AVC;
86 netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER);
87 out:
88 return;
90 nlmsg_failure:
91 kfree_skb(skb);
92 oom:
93 printk(KERN_ERR "SELinux: OOM in %s\n", __func__);
94 goto out;
97 void selnl_notify_setenforce(int val)
99 selnl_notify(SELNL_MSG_SETENFORCE, &val);
102 void selnl_notify_policyload(u32 seqno)
104 selnl_notify(SELNL_MSG_POLICYLOAD, &seqno);
107 static int __init selnl_init(void)
109 selnl = netlink_kernel_create(&init_net, NETLINK_SELINUX,
110 SELNLGRP_MAX, NULL, NULL, THIS_MODULE);
111 if (selnl == NULL)
112 panic("SELinux: Cannot create netlink socket.");
113 netlink_set_nonroot(NETLINK_SELINUX, NL_NONROOT_RECV);
114 return 0;
117 __initcall(selnl_init);