summaryrefslogtreecommitdiff
path: root/cleopatre/application/managerd/src/mme_nl.c
blob: f089c18d62e9559de4d9775fde8896d6dec3a6ea (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
/*
 * cleopatre/application/managerd/src/mme_nl.c
 *
 * (C) Copyright 2010 SPiDCOM Technologies
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <linux/if_packet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <syslog.h>
#include <assert.h>
#include <linux/if_ether.h>
#include <asm/byteorder.h>
#include <linux/netlink.h>

#include "ioctl.h"

#include "mme_nl.h"

#define PLC_IFNAME "plc0"

/**
 * Receive a frame from MME interface.
 *
 * \param  ctx  managerd context.
 * \param  buffer  frame pointer.
 * \param  len  length allowed.
 * \return  received length or error code.
 */
int mme_nl_receive(struct managerd_ctx *ctx, uint8_t *buffer, int len)
{
    struct msghdr msg;
    struct nlmsghdr *nlh;
    struct sockaddr_nl kernel_addr;
    struct iovec iov;
    int msg_len, result;

    /* create message from MME netlink */
    nlh=(struct nlmsghdr *)malloc (sizeof(struct nlmsghdr) + len);
    memset(nlh, 0, sizeof(struct nlmsghdr) + len);
    memset(&kernel_addr, 0, sizeof(kernel_addr));
    iov.iov_base = (void *)nlh;
    iov.iov_len = NLMSG_LENGTH(len);
    msg.msg_name = (void *)&kernel_addr;
    msg.msg_namelen = sizeof(kernel_addr);
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;

    if((recvmsg (ctx->sock_mme, &msg, 0)) < 0)
    {
        syslog (LOG_WARNING, "mme receive error (%d)", errno);
        free (nlh);
        return -1;
    }

    msg_len = nlh->nlmsg_len - NLMSG_HDRLEN;
    if (msg_len > len)
    {
        syslog (LOG_WARNING, "mme receive too big (%d)", errno);
        free (nlh);
        return -1;
    }

    memcpy (buffer, NLMSG_DATA (nlh), msg_len);
    result = msg_len;
    free (nlh);
    return result;
}

/**
 * Send a frame to MME interface.
 *
 * \param  ctx  managerd context.
 * \param  buffer  frame pointer.
 * \param  len  length to send.
 * \return  error code.
 */
int mme_nl_send(struct managerd_ctx *ctx, uint8_t *buffer, int len)
{
    struct sockaddr_nl kernel_addr;
    struct nlmsghdr *nlh;
    int msg_len, result;
    struct iovec iov;
    struct msghdr msg;

    /* fill the netlink address to send msg */
    memset(&kernel_addr, 0, sizeof(kernel_addr));
    kernel_addr.nl_family = AF_NETLINK;
    kernel_addr.nl_pid = 0;   /* For Linux Kernel */
    kernel_addr.nl_groups = 0; /* unicast */

    /* fill message */
    nlh=(struct nlmsghdr *)malloc (sizeof(struct nlmsghdr) + len);
    memset(nlh, 0, sizeof(struct nlmsghdr));
    memcpy (NLMSG_DATA (nlh), buffer, len);

    /* fill the netlink message header */
    nlh->nlmsg_len = NLMSG_LENGTH(len);
    nlh->nlmsg_pid = getpid();  /* self pid */
    nlh->nlmsg_flags = 0;

    /* fill message info */
    memset(&msg, 0, sizeof(msg));
    msg.msg_name = (void *)&kernel_addr;
    msg.msg_namelen = sizeof(kernel_addr);
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;
    iov.iov_base = (void *)nlh;
    iov.iov_len = NLMSG_LENGTH(len);

    if((msg_len = sendmsg (ctx->sock_mme, &msg, 0)) < 0)
    {
        syslog (LOG_WARNING, "sendmsg error (%d)", errno);
        free (nlh);
        return -1;
    }
    free (nlh);
    return (len);
}

/**
 * Initialize and prepare connections.
 *
 * \param  ctx  managerd context.
 * \return  error code.
 */
int mme_nl_init(struct managerd_ctx *ctx)
{
    struct plcdrv_setpid user_data = {0};
    struct ifreq ifr = {0};
    int sock_plc;
    struct sockaddr_ll plc_sll;

    //Check arguments
    assert(ctx != NULL);

    /* open netlink socket */
    if(0 > (ctx->sock_mme = socket(PF_NETLINK, SOCK_RAW, NETLINK_PLC_MME)))
    {
        syslog (LOG_WARNING, "cannot open socket on %s (%s)", MME_IFNAME, strerror(errno));
        return -1;
    }
    memset(&ctx->mme_snl, 0, sizeof(ctx->mme_snl));
    ctx->mme_snl.nl_family = AF_NETLINK;
    ctx->mme_snl.nl_pid = getpid();  /* self pid */
    ctx->mme_snl.nl_groups = 0;  /* not in mcast group */
    if (0 > bind(ctx->sock_mme, (struct sockaddr *)&ctx->mme_snl, sizeof(ctx->mme_snl)))
    {
        syslog (LOG_WARNING, "cannot bind socket on %s (%s)", MME_IFNAME, strerror (errno));
        close (ctx->sock_mme);
        return -1;
    }

    /* Create a receive connection on PLC interface */
    if (0 > (sock_plc = socket (AF_PACKET, SOCK_RAW, ETH_P_HPAV)))
    {
        syslog (LOG_WARNING, "cannot open socket on %s (%s)", PLC_IFNAME,
                strerror (errno));
        close (ctx->sock_mme);
        return -1;
    }

    /* Prepare PLC socket address */
    strncpy (ifr.ifr_name, (char*) PLC_IFNAME, IFNAMSIZ);
    if (-1 == (ioctl (sock_plc, SIOCGIFINDEX, &ifr)))
    {
        syslog (LOG_WARNING, "cannot get interface %s index (%s)",
                PLC_IFNAME, strerror (errno));
        close (ctx->sock_mme);
        close (sock_plc);
        return -1;
    }
    plc_sll.sll_family = AF_PACKET;
    plc_sll.sll_ifindex = ifr.ifr_ifindex;
    plc_sll.sll_protocol = htons (ETH_P_HPAV);

    /* Bind PLC socket to this interface */
    if (-1 == (bind (sock_plc, (struct sockaddr *) &plc_sll,
                     sizeof (struct sockaddr_ll))))
    {
        syslog (LOG_WARNING, "cannot bind raw socket to interface %s (%s)",
                PLC_IFNAME, strerror (errno));
        close (ctx->sock_mme);
        close (sock_plc);
        return -1;
    }

    /* Set managerd pid for reception on mme netlink */
    user_data.nl = NETLINK_PLC_MME;
    user_data.pid = getpid ();
    ifr.ifr_data = (void *) &user_data;
    strncpy (ifr.ifr_name, (char*) PLC_IFNAME, IFNAMSIZ);
    if (0 > (ioctl (sock_plc, PLCDRV_IOCTL_SETPID, &ifr)))
    {
        syslog (LOG_WARNING, "cannot call ioctl SETPID (%s)", strerror (errno));
        close (ctx->sock_mme);
        close (sock_plc);
        return -1;
    }

    /* We do not need PLC connection anymore => close it. */
    close (sock_plc);

    return 0;
}

/**
 * Close connections.
 *
 * \param  ctx  managerd context.
 */
void mme_nl_uninit(struct managerd_ctx *ctx)
{
    //Check arguments
    assert(ctx != NULL);

    //Close MME connections
    close(ctx->sock_mme);
}