netlink.c 2.4 KB

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