summaryrefslogtreecommitdiff
path: root/cleopatre/application/managerd/src/mme_nl.c
blob: 3bedce179afd8ce510a9dfc05ec3d9901fe2d3d6 (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
/*
 * 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 "mme_nl.h"

/**
 * 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;
    MME_t *mme_hdr;

    /* 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((msg_len = recvmsg (ctx->sock_mme, &msg, 0)) < 0)
    {
        syslog (LOG_WARNING, "mme receive error (%d)", errno);
        free (nlh);
        return -1;
    }

    memcpy (buffer, NLMSG_DATA (nlh), nlh->nlmsg_len);
    result = nlh->nlmsg_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 sockaddr_ll sll;
    struct ifreq ifr;

    //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));
        return -1;
    }

    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);
}