summaryrefslogtreecommitdiff
path: root/mobicore/daemon/Daemon/Server/NetlinkServer.cpp
blob: e890877376ef6744af8988db5a2a048c5e651bb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/** @addtogroup MCD_MCDIMPL_DAEMON_SRV
 * @{
 * @file
 *
 * Connection server.
 *
 * Handles incoming socket connections from clients using the MobiCore driver.
 */
/*
 * Copyright (c) 2013 TRUSTONIC LIMITED
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the TRUSTONIC LIMITED nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "public/NetlinkServer.h"
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <linux/netlink.h>

#include <stdlib.h>
#include "NetlinkConnection.h"
#include <signal.h>

#define LOG_TAG "McDaemon"
#include "log.h"

//------------------------------------------------------------------------------
NetlinkServer::NetlinkServer(
    ConnectionHandler *connectionHandler
): Server(connectionHandler, "dummy")
{
}


//------------------------------------------------------------------------------
void NetlinkServer::run(
)
{
    do {
        LOG_I("NetlinkServer: Starting to listen on netlink bus");

        // Open a socket
        serverSock = socket(PF_NETLINK, SOCK_DGRAM,  MC_DAEMON_NETLINK);
        if (serverSock < 0) {
            LOG_ERRNO("Opening socket");
            break;
        }

        // Fill in address structure and bind to socket
        struct sockaddr_nl src_addr;
        struct nlmsghdr *nlh = NULL;
        struct iovec iov;
        struct msghdr msg;
        uint32_t len;

        memset(&src_addr, 0, sizeof(src_addr));
        src_addr.nl_family = AF_NETLINK;
        src_addr.nl_pid = MC_DAEMON_PID;  /* daemon pid */
        src_addr.nl_groups = 0;  /* not in mcast groups */
        if (bind(serverSock, (struct sockaddr *)&src_addr, sizeof(src_addr)) < 0) {
            LOG_ERRNO("Binding to server socket failed, because bind");
            close(serverSock);
            serverSock = -1;
            break;
        }

        // Start reading the socket
        LOG_I("\n********* successfully initialized *********\n");

        for (;;) {
            // This buffer will be taken over by the connection it was routed to
            nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
            if (nlh == NULL) {
                LOG_E("Allocation failure");
                break;
            }
            memset(&msg, 0, sizeof(msg));
            iov.iov_base = (void *)nlh;
            iov.iov_len = NLMSG_SPACE(MAX_PAYLOAD);
            msg.msg_iov = &iov;
            msg.msg_iovlen = 1;
            msg.msg_name = &src_addr;
            msg.msg_namelen = sizeof(src_addr);

            memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));

            // Read the incoming message and route it to the connection based
            // on the incoming PID
            if ((int) (len = recvmsg(serverSock, &msg, 0)) < 0) {
                LOG_ERRNO("recvmsg");
                break;
            }

            if (NLMSG_OK(nlh, len)) {
                handleMessage(nlh);
            } else {
                break;
            }
        }
        close(serverSock);
        serverSock = -1;
    } while (false);

    LOG_W("Could not open netlink socket. KernelAPI disabled");
}

//------------------------------------------------------------------------------
void NetlinkServer::handleMessage(
    struct nlmsghdr *nlh
)
{
    uint32_t seq = nlh->nlmsg_seq;
    uint32_t pid = nlh->nlmsg_pid;
    //LOG_I("%s: Handling NQ message for pid %u seq %u...", __FUNCTION__, pid, seq);
    uint64_t hash = hashConnection(pid, seq);
    /* First cleanup the connection list */
    cleanupConnections();

    NetlinkConnection *connection = findConnection(hash);
    // This is a message from a new client
    if (connection == NULL) {
        //LOG_I("%s: Cound't find the connection, creating a new one", __FUNCTION__);
        connection = new NetlinkConnection(this, serverSock, pid, seq);
        // Add the new connection
        insertConnection(hash, connection);
    }

    connection->handleMessage(nlh);

    // Only handle connections which have not been detached
    if (connection->detached == false) {
        if (!connectionHandler->handleConnection(connection)) {
            LOG_I("%s: No command processed.", __FUNCTION__);
            connection->socketDescriptor = -1;
            //Inform the driver
            connectionHandler->dropConnection(connection);

            // Remove connection from list
            removeConnection(hash);
            connection->socketDescriptor = -1;
            delete connection;
        }
        // If connection data is set to NULL then device close has been called
        // so we must remove all connections associated with this hash
        else if (connection->connectionData == NULL &&
                 connection->detached == false) {
            delete connection;
        }
    }
}


//------------------------------------------------------------------------------
void NetlinkServer::detachConnection(
    Connection *connection
)
{
    connection->detached = true;
}


//------------------------------------------------------------------------------
NetlinkServer::~NetlinkServer(
    void
)
{
    connectionMap_t::iterator i;
    // Shut down the server socket
    if(serverSock != -1) {
        close(serverSock);
        serverSock = -1;
    }

    // Destroy all client connections
    for (i = peerConnections.begin(); i != peerConnections.end(); i++) {
        if (i->second->detached == false) {
            delete i->second;
        }
    }
    peerConnections.clear();
}


//------------------------------------------------------------------------------
NetlinkConnection *NetlinkServer::findConnection(
    uint64_t hash
)
{
    connectionMap_t::iterator i = peerConnections.find(hash);
    if (i != peerConnections.end()) {
        return i->second;
    }

    return NULL;
}


//------------------------------------------------------------------------------
void NetlinkServer::insertConnection(
    uint64_t hash,
    NetlinkConnection *connection
)
{
    peerConnections[hash] = connection;
}

/* This is called from multiple threads! */
//------------------------------------------------------------------------------
void NetlinkServer::removeConnection(
    uint64_t hash
)
{
    connectionMap_t::iterator i = peerConnections.find(hash);
    if (i != peerConnections.end()) {
        peerConnections.erase(i);
    }
}

//------------------------------------------------------------------------------
void NetlinkServer::cleanupConnections(
    void
)
{
    connectionMap_t::reverse_iterator i;
    pid_t pid;
    NetlinkConnection *connection = NULL;
    // Destroy all client connections
    for (i = peerConnections.rbegin(); i != peerConnections.rend(); ++i) {
        connection = i->second;
        // Only 16 bits are for the actual PID, the rest is session magic
        pid = connection->peerPid & 0xFFFF;
        //LOG_I("%s: checking PID %u", __FUNCTION__, pid);
        // Check if the peer pid is still alive
        if (pid == 0) {
            continue;
        }
        if (kill(pid, 0)) {
            bool detached = connection->detached;
            LOG_I("%s: PID %u has died, cleaning up session 0x%X",
                  __FUNCTION__, pid, connection->peerPid);

            connection->socketDescriptor = -1;
            //Inform the driver
            connectionHandler->dropConnection(connection);

            // We aren't handling this connection anymore no matter what
            removeConnection(connection->hash);

            // Remove connection from list only if detached, the detached
            // connections are managed by the device
            if (detached == false) {
                delete connection;
            }
            if (peerConnections.size() == 0) {
                break;
            }
            i = peerConnections.rbegin();
        }
    }
}

/** @} */