netlink.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Netlink event notifications for SELinux.
  3. *
  4. * Author: James Morris <jmorris@redhat.com>
  5. *
  6. * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  7. *
  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.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/types.h>
  14. #include <linux/stddef.h>
  15. #include <linux/kernel.h>
  16. #include <linux/list.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/netlink.h>
  19. #include <linux/selinux_netlink.h>
  20. static struct sock *selnl;
  21. static int selnl_msglen(int msgtype)
  22. {
  23. int ret = 0;
  24. switch (msgtype) {
  25. case SELNL_MSG_SETENFORCE:
  26. ret = sizeof(struct selnl_msg_setenforce);
  27. break;
  28. case SELNL_MSG_POLICYLOAD:
  29. ret = sizeof(struct selnl_msg_policyload);
  30. break;
  31. default:
  32. BUG();
  33. }
  34. return ret;
  35. }
  36. static void selnl_add_payload(struct nlmsghdr *nlh, int len, int msgtype, void *data)
  37. {
  38. switch (msgtype) {
  39. case SELNL_MSG_SETENFORCE: {
  40. struct selnl_msg_setenforce *msg = NLMSG_DATA(nlh);
  41. memset(msg, 0, len);
  42. msg->val = *((int *)data);
  43. break;
  44. }
  45. case SELNL_MSG_POLICYLOAD: {
  46. struct selnl_msg_policyload *msg = NLMSG_DATA(nlh);
  47. memset(msg, 0, len);
  48. msg->seqno = *((u32 *)data);
  49. break;
  50. }
  51. default:
  52. BUG();
  53. }
  54. }
  55. static void selnl_notify(int msgtype, void *data)
  56. {
  57. int len;
  58. unsigned char *tmp;
  59. struct sk_buff *skb;
  60. struct nlmsghdr *nlh;
  61. len = selnl_msglen(msgtype);
  62. skb = alloc_skb(NLMSG_SPACE(len), GFP_USER);
  63. if (!skb)
  64. goto oom;
  65. tmp = skb->tail;
  66. nlh = NLMSG_PUT(skb, 0, 0, msgtype, len);
  67. selnl_add_payload(nlh, len, msgtype, data);
  68. nlh->nlmsg_len = skb->tail - tmp;
  69. NETLINK_CB(skb).dst_group = SELNLGRP_AVC;
  70. netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER);
  71. out:
  72. return;
  73. nlmsg_failure:
  74. kfree_skb(skb);
  75. oom:
  76. printk(KERN_ERR "SELinux: OOM in %s\n", __FUNCTION__);
  77. goto out;
  78. }
  79. void selnl_notify_setenforce(int val)
  80. {
  81. selnl_notify(SELNL_MSG_SETENFORCE, &val);
  82. }
  83. void selnl_notify_policyload(u32 seqno)
  84. {
  85. selnl_notify(SELNL_MSG_POLICYLOAD, &seqno);
  86. }
  87. static int __init selnl_init(void)
  88. {
  89. selnl = netlink_kernel_create(NETLINK_SELINUX, SELNLGRP_MAX, NULL,
  90. THIS_MODULE);
  91. if (selnl == NULL)
  92. panic("SELinux: Cannot create netlink socket.");
  93. netlink_set_nonroot(NETLINK_SELINUX, NL_NONROOT_RECV);
  94. return 0;
  95. }
  96. __initcall(selnl_init);