scm.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifndef __LINUX_NET_SCM_H
  2. #define __LINUX_NET_SCM_H
  3. #include <linux/limits.h>
  4. #include <linux/net.h>
  5. /* Well, we should have at least one descriptor open
  6. * to accept passed FDs 8)
  7. */
  8. #define SCM_MAX_FD (OPEN_MAX-1)
  9. struct scm_fp_list
  10. {
  11. int count;
  12. struct file *fp[SCM_MAX_FD];
  13. };
  14. struct scm_cookie
  15. {
  16. struct ucred creds; /* Skb credentials */
  17. struct scm_fp_list *fp; /* Passed files */
  18. #ifdef CONFIG_SECURITY_NETWORK
  19. char *secdata; /* Security context */
  20. u32 seclen; /* Security length */
  21. #endif
  22. unsigned long seq; /* Connection seqno */
  23. };
  24. extern void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm);
  25. extern void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm);
  26. extern int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm);
  27. extern void __scm_destroy(struct scm_cookie *scm);
  28. extern struct scm_fp_list * scm_fp_dup(struct scm_fp_list *fpl);
  29. static __inline__ void scm_destroy(struct scm_cookie *scm)
  30. {
  31. if (scm && scm->fp)
  32. __scm_destroy(scm);
  33. }
  34. static __inline__ int scm_send(struct socket *sock, struct msghdr *msg,
  35. struct scm_cookie *scm)
  36. {
  37. struct task_struct *p = current;
  38. scm->creds.uid = p->uid;
  39. scm->creds.gid = p->gid;
  40. scm->creds.pid = p->tgid;
  41. scm->fp = NULL;
  42. scm->seq = 0;
  43. if (msg->msg_controllen <= 0)
  44. return 0;
  45. return __scm_send(sock, msg, scm);
  46. }
  47. #ifdef CONFIG_SECURITY_NETWORK
  48. static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
  49. {
  50. if (test_bit(SOCK_PASSSEC, &sock->flags) && scm->secdata != NULL)
  51. put_cmsg(msg, SOL_SOCKET, SCM_SECURITY, scm->seclen, scm->secdata);
  52. }
  53. #else
  54. static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
  55. { }
  56. #endif /* CONFIG_SECURITY_NETWORK */
  57. static __inline__ void scm_recv(struct socket *sock, struct msghdr *msg,
  58. struct scm_cookie *scm, int flags)
  59. {
  60. if (!msg->msg_control)
  61. {
  62. if (test_bit(SOCK_PASSCRED, &sock->flags) || scm->fp)
  63. msg->msg_flags |= MSG_CTRUNC;
  64. scm_destroy(scm);
  65. return;
  66. }
  67. if (test_bit(SOCK_PASSCRED, &sock->flags))
  68. put_cmsg(msg, SOL_SOCKET, SCM_CREDENTIALS, sizeof(scm->creds), &scm->creds);
  69. scm_passec(sock, msg, scm);
  70. if (!scm->fp)
  71. return;
  72. scm_detach_fds(msg, scm);
  73. }
  74. #endif /* __LINUX_NET_SCM_H */