summaryrefslogtreecommitdiff
path: root/cleopatre/application/managerd/src/bridge.c
blob: a72f943088732c4e36466e326888976e08221afa (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
/*
 * cleopatre/application/managerd/src/bridge.c
 *
 * (C) Copyright 2009 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 <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 "bridge.h"

/**
 * Processing a received frame before sending it.
 * Analyse received frame on buffer pointer, processing it
 * and give it on buffer pointer.
 *
 * \param  ctx  managerd context.
 * \param  buffer  received frame.
 * \param  len  received frame length that will be updated.
 * \param  max_len  size max of buffer.
 * \return  bridge status.
 */
enum bridge_status bridge_processing(struct managerd_ctx *ctx, uint8_t *buffer, int *len, int max_len)
{
    MME_t *mmehdr;
    uint8_t confirm_buffer[MAX_PKT_LEN];
    MME_t *confirm;

    //Check arguments
    assert(ctx != NULL);
    assert(buffer != NULL);
    assert(len != NULL);
    assert((*len > 0) && (*len <= max_len));

    mmehdr = (MME_t *) buffer;
    confirm = (MME_t *) confirm_buffer;

    /* check if this MME needs to be internally processed */
    switch(mmehdr->mmtype)
    {
    case MME_TYPE_VS_GET_VERSION | MME_TYPE_REQ:
        return vs_mme_get_version (ctx, mmehdr, confirm, MAX_PKT_LEN);

    case MME_TYPE_VS_ETH_STATS | MME_TYPE_REQ:
        return vs_mme_eth_stats (ctx, mmehdr, confirm, MAX_PKT_LEN);

    case MME_TYPE_VS_RESET | MME_TYPE_REQ:
        return vs_mme_reset (ctx, mmehdr, confirm, MAX_PKT_LEN);

    default:
        break;
    }

    if((mmehdr->mmtype < MME_TYPE_MS)
       || (mmehdr->mmtype >= MME_TYPE_VS))
    {
        /* AV or VS MME: give it to MME interface */
        mme_nl_send (ctx, buffer, *len);
    }

    return TO_DROP;
}

/**
 * Receive a frame from BR or LO interface.
 *
 * \param  ctx  managerd context.
 * \param  buffer  frame pointer.
 * \param  len  length allowed.
 * \param  is_local boolean; TRUE: get packet from loopback, else: get packet from bridge
 * \return received length or error code.
 */
int bridge_receive(struct managerd_ctx *ctx, uint8_t *buffer, int len, int is_local)
{
    //Receive a raw packet from BR interface
    if(is_local)
    {
        len = recv(ctx->sock_lo, buffer, len, 0);
    }
    else
    {
        len = recv(ctx->sock_br, buffer, len, 0);
    }
    if(0 >= len)
    {
        syslog(LOG_WARNING, "receive failed on %s (%s)", is_local ? LO_IFNAME : BR_IFNAME, strerror(errno));
        return -1;
    }
    return len;
}

/**
 * Send a frame to BR or LO interface.
 *
 * \param  ctx  managerd context.
 * \param  buffer  frame pointer.
 * \param  len  length to send.
 * \return  error code.
 */

int bridge_send(struct managerd_ctx *ctx, uint8_t *buffer, int len)
{
    struct ethhdr *ethhdr;
    int is_local = 0;
    ethhdr = (struct ethhdr *)buffer;

    /* check destination mac address */
    if(!memcmp (ethhdr->h_dest, ctx->br_mac_addr, ETH_ALEN))
    {
        is_local = 1;
    }
    //Send a raw packet to right interface
    len = send (is_local ? ctx->sock_lo : ctx->sock_br, buffer, len, 0);
    if(0 > len)
    {
        syslog(LOG_WARNING, "send failed on %s (%s)", is_local ? LO_IFNAME : BR_IFNAME, strerror(errno));
        return -1;
    }
    return 0;
}

/**
 * Initialize and prepare connections.
 *
 * \param  ctx  managerd context.
 * \return  error code.
 */
int bridge_init(struct managerd_ctx *ctx)
{
    struct sockaddr_ll sll;
    struct ifreq ifr;

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

    //Create a receive connection on BR interface
    if(0 > (ctx->sock_br = socket(AF_PACKET, SOCK_RAW, ETH_P_HPAV)))
    {
        syslog(LOG_WARNING, "cannot open socket on %s (%s)", BR_IFNAME, strerror(errno));
        return -1;
    }

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

    //Find BR mac address
    if(-1 == (ioctl(ctx->sock_br, SIOCGIFHWADDR, &ifr)))
    {
        syslog(LOG_WARNING, "cannot get interface %s mac address (%s)", BR_IFNAME, strerror(errno));
        close(ctx->sock_br);
        return -1;
    }
    memcpy(ctx->br_mac_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);

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

    //Create a receive connection on LO interface
    if(0 > (ctx->sock_lo = socket(AF_PACKET, SOCK_RAW, ETH_P_HPAV)))
    {
        syslog(LOG_WARNING, "cannot open socket on %s (%s)", LO_IFNAME, strerror(errno));
        close(ctx->sock_br);
        return -1;
    }

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

    //Bind LO socket to this interface
    if(-1 == (bind(ctx->sock_lo, (struct sockaddr *)&sll, sizeof(struct sockaddr_ll))))
    {
        syslog (LOG_WARNING, "cannot bind raw socket to interface %s (%s)", LO_IFNAME, strerror (errno));
        close (ctx->sock_br);
        close (ctx->sock_lo);
        return -1;
    }

    return 0;
}

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

    //Close BR connections
    close(ctx->sock_br);
    //Close LO connections
    close(ctx->sock_lo);
}