1 | // =================================================================== |
---|
2 | // |
---|
3 | // Copyright (c) 2005, Intel Corp. |
---|
4 | // All rights reserved. |
---|
5 | // |
---|
6 | // Redistribution and use in source and binary forms, with or without |
---|
7 | // modification, are permitted provided that the following conditions |
---|
8 | // are met: |
---|
9 | // |
---|
10 | // * Redistributions of source code must retain the above copyright |
---|
11 | // notice, this list of conditions and the following disclaimer. |
---|
12 | // * Redistributions in binary form must reproduce the above |
---|
13 | // copyright notice, this list of conditions and the following |
---|
14 | // disclaimer in the documentation and/or other materials provided |
---|
15 | // with the distribution. |
---|
16 | // * Neither the name of Intel Corporation nor the names of its |
---|
17 | // contributors may be used to endorse or promote products derived |
---|
18 | // from this software without specific prior written permission. |
---|
19 | // |
---|
20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
---|
23 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
---|
24 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
25 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
26 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
---|
27 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
28 | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
---|
29 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
---|
31 | // OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | // =================================================================== |
---|
33 | |
---|
34 | #include <stdio.h> |
---|
35 | #include <sys/types.h> |
---|
36 | #include <sys/socket.h> |
---|
37 | #include <netinet/in.h> |
---|
38 | #include <arpa/inet.h> |
---|
39 | #include <string.h> |
---|
40 | |
---|
41 | #include "tcg.h" |
---|
42 | #include "log.h" |
---|
43 | #include "bsg.h" |
---|
44 | #include "buffer.h" |
---|
45 | #include "vtpm_migrator.h" |
---|
46 | |
---|
47 | void build_error_msg( buffer_t *buf, TPM_RESULT status) { |
---|
48 | TPM_TAG tag = VTPM_MTAG_RSP; |
---|
49 | UINT32 out_param_size = VTPM_COMMAND_HEADER_SIZE; |
---|
50 | |
---|
51 | buffer_init(buf, out_param_size, NULL); |
---|
52 | |
---|
53 | BSG_PackList(buf->bytes, 3, |
---|
54 | BSG_TPM_TAG, &tag, |
---|
55 | BSG_TYPE_UINT32, &out_param_size, |
---|
56 | BSG_TPM_RESULT, &status ); |
---|
57 | } |
---|
58 | |
---|
59 | int main() { |
---|
60 | |
---|
61 | /* network variables */ |
---|
62 | int sock_descr, client_sock=-1, len; |
---|
63 | struct sockaddr_in addr; |
---|
64 | struct sockaddr_in client_addr; |
---|
65 | unsigned int client_length; |
---|
66 | int bytes; |
---|
67 | |
---|
68 | /* variables for processing of command */ |
---|
69 | TPM_RESULT status = TPM_FAIL; |
---|
70 | BYTE cmd_header[VTPM_COMMAND_HEADER_SIZE]; |
---|
71 | TPM_TAG tag; |
---|
72 | TPM_COMMAND_CODE ord; |
---|
73 | UINT32 in_param_size, adj_param_size; |
---|
74 | int i, size_read, size_write; |
---|
75 | buffer_t in_param_buf=NULL_BUF, result_buf=NULL_BUF; |
---|
76 | |
---|
77 | |
---|
78 | /* setup socket */ |
---|
79 | sock_descr = socket(AF_INET, SOCK_STREAM, 0); |
---|
80 | |
---|
81 | memset(&addr, 0, sizeof(addr)); |
---|
82 | addr.sin_family = AF_INET; |
---|
83 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
84 | addr.sin_port = htons(VTPM_MIG_PORT); |
---|
85 | |
---|
86 | if (bind(sock_descr, (struct sockaddr *)&addr, sizeof(addr)) == -1 ) { |
---|
87 | vtpmlogerror(VTPM_LOG_VTPM, "Failed to bind to port %d.\n", VTPM_MIG_PORT); |
---|
88 | return 1; |
---|
89 | } |
---|
90 | |
---|
91 | listen(sock_descr, 10); |
---|
92 | |
---|
93 | for(;;) { |
---|
94 | // ============ clear client info and wait for connection ========== |
---|
95 | memset(&client_addr, 0, sizeof(client_addr)); |
---|
96 | client_length = sizeof(client_addr); |
---|
97 | |
---|
98 | vtpmloginfo(VTPM_LOG_VTPM, "Waiting for incoming migrations...\n"); |
---|
99 | client_sock=accept(sock_descr, &client_addr, &client_length); |
---|
100 | if (client_sock == -1) { |
---|
101 | vtpmlogerror(VTPM_LOG_VTPM, "Incoming connectionn failed.\n"); |
---|
102 | goto abort_command; |
---|
103 | } else { |
---|
104 | vtpmloginfo(VTPM_LOG_VTPM, "Incoming connection accepted.\n"); |
---|
105 | } |
---|
106 | |
---|
107 | // =================== Read incoming command ====================== |
---|
108 | size_read = read( client_sock, cmd_header, VTPM_COMMAND_HEADER_SIZE); |
---|
109 | if (size_read > 0) { |
---|
110 | vtpmloginfo(VTPM_LOG_VTPM_DEEP, "RECV: 0x"); |
---|
111 | for (i=0; i<size_read; i++) |
---|
112 | vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", cmd_header[i]); |
---|
113 | |
---|
114 | } else { |
---|
115 | vtpmlogerror(VTPM_LOG_VTPM, "Error reading from socket.\n"); |
---|
116 | build_error_msg(&result_buf, TPM_IOERROR); |
---|
117 | goto abort_command_with_error; |
---|
118 | } |
---|
119 | |
---|
120 | if (size_read < (int) VTPM_COMMAND_HEADER_SIZE) { |
---|
121 | vtpmlogerror(VTPM_LOG_VTPM, "Command from socket shorter than std header.\n"); |
---|
122 | build_error_msg(&result_buf, TPM_BAD_PARAMETER); |
---|
123 | goto abort_command_with_error; |
---|
124 | } |
---|
125 | |
---|
126 | // Unpack response from client |
---|
127 | BSG_UnpackList(cmd_header, 3, |
---|
128 | BSG_TPM_TAG, &tag, |
---|
129 | BSG_TYPE_UINT32, &in_param_size, |
---|
130 | BSG_TPM_COMMAND_CODE, &ord ); |
---|
131 | |
---|
132 | |
---|
133 | // If response has parameters, read them. |
---|
134 | // Note that out_param_size is in the client's context |
---|
135 | adj_param_size = in_param_size - VTPM_COMMAND_HEADER_SIZE; |
---|
136 | if (adj_param_size > 0) { |
---|
137 | buffer_init( &in_param_buf, adj_param_size, NULL); |
---|
138 | size_read = read(client_sock, in_param_buf.bytes, adj_param_size); |
---|
139 | if (size_read > 0) { |
---|
140 | for (i=0; i< size_read; i++) |
---|
141 | vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", in_param_buf.bytes[i]); |
---|
142 | |
---|
143 | } else { |
---|
144 | vtpmlogerror(VTPM_LOG_VTPM, "Error reading from socket.\n"); |
---|
145 | build_error_msg(&result_buf, TPM_IOERROR); |
---|
146 | goto abort_command_with_error; |
---|
147 | } |
---|
148 | vtpmloginfomore(VTPM_LOG_VTPM, "\n"); |
---|
149 | |
---|
150 | if (size_read < (int)adj_param_size) { |
---|
151 | vtpmloginfomore(VTPM_LOG_VTPM, "\n"); |
---|
152 | vtpmlogerror(VTPM_LOG_VTPM, "Command read(%d) is shorter than header indicates(%d).\n", size_read, adj_param_size); |
---|
153 | build_error_msg(&result_buf, TPM_BAD_PARAMETER); |
---|
154 | goto abort_command_with_error; |
---|
155 | } |
---|
156 | } else { |
---|
157 | vtpmloginfomore(VTPM_LOG_VTPM, "\n"); |
---|
158 | } |
---|
159 | |
---|
160 | /* Handle Command */ |
---|
161 | switch (ord) { |
---|
162 | case VTPM_MORD_MIG_STEP2: |
---|
163 | handle_vtpm_mig_step2(&in_param_buf, &result_buf); |
---|
164 | break; |
---|
165 | |
---|
166 | case VTPM_MORD_MIG_STEP3: |
---|
167 | handle_vtpm_mig_step3(&in_param_buf, &result_buf); |
---|
168 | break; |
---|
169 | |
---|
170 | default: |
---|
171 | build_error_msg(&result_buf, TPM_BAD_PARAMETER); |
---|
172 | goto abort_command_with_error; |
---|
173 | } |
---|
174 | |
---|
175 | abort_command_with_error: |
---|
176 | /* Write Response */ |
---|
177 | size_write = write(client_sock, result_buf.bytes, buffer_len(&result_buf)); |
---|
178 | |
---|
179 | if (size_write > 0) { |
---|
180 | vtpmloginfo(VTPM_LOG_VTPM_DEEP, "SENT: 0x"); |
---|
181 | for (i=0; i< buffer_len(&result_buf); i++) { |
---|
182 | vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", result_buf.bytes[i]); |
---|
183 | } |
---|
184 | vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n"); |
---|
185 | } else { |
---|
186 | vtpmlogerror(VTPM_LOG_VTPM, "Error writing response to client.\n"); |
---|
187 | goto abort_command; |
---|
188 | } |
---|
189 | |
---|
190 | if (size_write != (int) buffer_len(&result_buf) ) |
---|
191 | vtpmlogerror(VTPM_LOG_VTPM, "Could not send entire response to client(%d/%d)\n", size_write, buffer_len(&result_buf)); |
---|
192 | |
---|
193 | abort_command: |
---|
194 | close(client_sock); |
---|
195 | buffer_free(&in_param_buf); |
---|
196 | buffer_free(&result_buf); |
---|
197 | |
---|
198 | } // For (;;) |
---|
199 | |
---|
200 | return 0; |
---|
201 | } |
---|
202 | |
---|