2
* Userspace side of communications with eCryptfs kernel module.
4
* Copyright (C) 2008 International Business Machines Corp.
5
* Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License as
9
* published by the Free Software Foundation; either version 2 of the
10
* License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26
#include <sys/types.h>
28
#include "../include/ecryptfs.h"
31
* ecryptfs_write_packet_length
32
* @dest: The byte array target into which to write the
33
* length. Must have at least 5 bytes allocated.
34
* @size: The length to write.
35
* @packet_size_length: The number of bytes used to encode the
36
* packet length is written to this address.
38
* Returns zero on success; non-zero on error.
40
int ecryptfs_write_packet_length(char *dest, size_t size,
41
size_t *packet_size_length)
47
(*packet_size_length) = 1;
48
} else if (size < 65536) {
49
dest[0] = (((size - 192) / 256) + 192);
50
dest[1] = ((size - 192) % 256);
51
(*packet_size_length) = 2;
54
syslog(LOG_ERR, "Unsupported packet size: [%d]\n",
61
* ecryptfs_parse_packet_length
62
* @data: Pointer to memory containing length at offset
63
* @size: This function writes the decoded size to this memory
64
* address; zero on error
65
* @length_size: The number of bytes occupied by the encoded length
67
* Returns zero on success
69
int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
80
} else if (data[0] < 224) {
82
(*size) = ((data[0] - 192) * 256);
83
(*size) += (data[1] + 192);
85
} else if (data[0] == 255) {
86
/* Five-byte length; we're not supposed to see this */
88
syslog(LOG_ERR, "Five-byte packet length not "
93
syslog(LOG_ERR, "Error parsing packet length\n");
101
* Called with mctx_mux held
103
int ecryptfs_init_messaging(struct ecryptfs_messaging_ctx *mctx, uint32_t type)
107
memset(mctx, 0, sizeof(*mctx));
109
case ECRYPTFS_MESSAGING_TYPE_NETLINK:
110
mctx->type = ECRYPTFS_MESSAGING_TYPE_NETLINK;
111
rc = ecryptfs_init_netlink(&mctx->ctx.nl_ctx);
113
case ECRYPTFS_MESSAGING_TYPE_PROC:
114
mctx->type = ECRYPTFS_MESSAGING_TYPE_PROC;
115
rc = ecryptfs_init_proc(&mctx->ctx.proc_ctx);
125
int ecryptfs_messaging_exit(struct ecryptfs_messaging_ctx *mctx)
129
switch (mctx->type) {
130
case ECRYPTFS_MESSAGING_TYPE_NETLINK:
131
ecryptfs_release_netlink(&mctx->ctx.nl_ctx);
133
case ECRYPTFS_MESSAGING_TYPE_PROC:
134
ecryptfs_release_proc(&mctx->ctx.proc_ctx);
145
* ecryptfs_send_message
146
* @mctx: Parent context for eCryptfs messaging with the kernel
147
* @msg: Message to send (struct ecryptfs_message with data appended)
148
* @msg_type: Message type to send
149
* @msg_flags: Flags for sending message
150
* @msg_seq: Message sequence number
153
int ecryptfs_send_message(struct ecryptfs_messaging_ctx *mctx,
154
struct ecryptfs_message *msg,
155
unsigned char msg_type, uint16_t msg_flags,
160
switch (mctx->type) {
161
case ECRYPTFS_MESSAGING_TYPE_NETLINK:
162
rc = ecryptfs_send_netlink(&mctx->ctx.nl_ctx, msg, msg_type,
165
syslog(LOG_ERR, "%s: Failed to register netlink daemon "
166
"with the eCryptfs kernel module; rc = [%d]\n",
171
case ECRYPTFS_MESSAGING_TYPE_PROC:
172
rc = ecryptfs_send_proc(&mctx->ctx.proc_ctx, msg, msg_type,
175
syslog(LOG_ERR, "%s: Failed to register proc daemon "
176
"with the eCryptfs kernel module; rc = [%d]\n",
188
int ecryptfs_run_daemon(struct ecryptfs_messaging_ctx *mctx)
192
switch (mctx->type) {
193
case ECRYPTFS_MESSAGING_TYPE_NETLINK:
194
rc = ecryptfs_run_netlink_daemon(&mctx->ctx.nl_ctx);
198
case ECRYPTFS_MESSAGING_TYPE_PROC:
199
rc = ecryptfs_run_proc_daemon(&mctx->ctx.proc_ctx);