File: pgw_gtp_path.c

package info (click to toggle)
nextepc 0.3.10+nods-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 23,304 kB
  • sloc: ansic: 164,982; yacc: 2,767; python: 2,567; makefile: 2,408; lex: 1,732; xml: 795; php: 486; sh: 349; sql: 337; perl: 75; awk: 35; cpp: 6
file content (528 lines) | stat: -rw-r--r-- 15,567 bytes parent folder | download
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#define TRACE_MODULE _pgw_gtp_path
#include "core_debug.h"
#include "core_pkbuf.h"

#include "3gpp_types.h"
#include "gtp/gtp_node.h"
#include "gtp/gtp_path.h"

#include "common/context.h"
#include "pgw_context.h"
#include "pgw_event.h"
#include "pgw_gtp_path.h"
#include "pgw_ipfw.h"

#define PGW_GTP_HANDLED     1

c_uint16_t in_cksum(c_uint16_t *addr, int len);
static status_t pgw_gtp_handle_multicast(pkbuf_t *recvbuf);
static status_t pgw_gtp_handle_slaac(pgw_sess_t *sess, pkbuf_t *recvbuf);
static status_t pgw_gtp_send_to_bearer(pgw_bearer_t *bearer, pkbuf_t *sendbuf);
static status_t pgw_gtp_send_router_advertisement(
        pgw_sess_t *sess, c_uint8_t *ip6_dst);

static int _gtpv1_tun_recv_cb(sock_id sock, void *data)
{
    pkbuf_t *recvbuf = NULL;
    int n;
    status_t rv;
    pgw_bearer_t *bearer = NULL;

    recvbuf = pkbuf_alloc(GTPV1U_HEADER_LEN, MAX_SDU_LEN);
    d_assert(recvbuf, return -1, "pkbuf_alloc error");

    n = sock_read(sock, recvbuf->payload, recvbuf->len);
    if (n <= 0)
    {
        pkbuf_free(recvbuf);
        return -1;
    }

    recvbuf->len = n;

    d_trace(50, "[TUN] RECV : ");
    d_trace_hex(50, recvbuf->payload, recvbuf->len);

    /* Find the bearer by packet filter */
    bearer = pgw_bearer_find_by_packet(recvbuf);
    if (bearer)
    {
        /* Unicast */
        rv = pgw_gtp_send_to_bearer(bearer, recvbuf);
        d_assert(rv == CORE_OK,, "pgw_gtp_send_to_bearer() failed");
    }
    else
    {
        if (context_self()->parameter.multicast)
        {
            rv = pgw_gtp_handle_multicast(recvbuf);
            d_assert(rv != CORE_ERROR,, "pgw_gtp_handle_multicast() failed");
        }
    }

    pkbuf_free(recvbuf);
    return 0;

}

static int _gtpv2_c_recv_cb(sock_id sock, void *data)
{
    event_t e;
    status_t rv;
    pkbuf_t *pkbuf = NULL;

    d_assert(sock, return -1, "Null param");

    rv = gtp_recv(sock, &pkbuf);
    if (rv != CORE_OK)
    {
        if (errno == EAGAIN)
            return 0;

        return -1;
    }

    event_set(&e, PGW_EVT_S5C_MESSAGE);
    event_set_param1(&e, (c_uintptr_t)pkbuf);
    rv = pgw_event_send(&e);
    if (rv != CORE_OK)
    {
        d_error("pgw_event_send error");
        pkbuf_free(pkbuf);
        return 0;
    }

    return 0;
}

static int _gtpv1_u_recv_cb(sock_id sock, void *data)
{
    status_t rv;
    pkbuf_t *pkbuf = NULL;
    c_uint32_t size = GTPV1U_HEADER_LEN;
    gtp_header_t *gtp_h = NULL;
    struct ip *ip_h = NULL;

    c_uint32_t teid;
    pgw_bearer_t *bearer = NULL;
    pgw_sess_t *sess = NULL;
    pgw_subnet_t *subnet = NULL;
    pgw_dev_t *dev = NULL;

    d_assert(sock, return -1, "Null param");

    rv = gtp_recv(sock, &pkbuf);
    if (rv != CORE_OK)
    {
        if (errno == EAGAIN)
            return 0;

        return -1;
    }

    d_assert(pkbuf, return 0,);
    d_assert(pkbuf->payload, goto cleanup,);
    d_trace(50, "[PGW] RECV : ");
    d_trace_hex(50, pkbuf->payload, pkbuf->len);

    gtp_h = pkbuf->payload;
    if (gtp_h->flags & GTPU_FLAGS_S) size += 4;
    teid = ntohl(gtp_h->teid);

    d_trace(3, "[PGW] RECV GPU-U from SGW : TEID[0x%x]\n", teid);

    /* Remove GTP header and send packets to TUN interface */
    d_assert(pkbuf_header(pkbuf, -size) == CORE_OK, goto cleanup,);

    ip_h = pkbuf->payload;
    d_assert(ip_h, goto cleanup,);

    bearer = pgw_bearer_find_by_pgw_s5u_teid(teid);
    d_assert(bearer, goto cleanup,);
    sess = bearer->sess;
    d_assert(sess, goto cleanup,);

    if (ip_h->ip_v == 4 && sess->ipv4)
        subnet = sess->ipv4->subnet;
    else if (ip_h->ip_v == 6 && sess->ipv6)
        subnet = sess->ipv6->subnet;

    if (!subnet)
    {
        d_trace_hex(9, pkbuf->payload, pkbuf->len);
        d_trace(9, "[DROP] Cannot find subnet V:%d, IPv4:%p, IPv6:%p\n",
                ip_h->ip_v, sess->ipv4, sess->ipv6);
        goto cleanup;
    }

    /* Check IPv6 */
    if (context_self()->parameter.no_slaac == 0 && ip_h->ip_v == 6)
    {
        rv = pgw_gtp_handle_slaac(sess, pkbuf);
        if (rv == PGW_GTP_HANDLED)
        {
            pkbuf_free(pkbuf);
            return 0;
        }
        d_assert(rv == CORE_OK,, "pgw_gtp_handle_slaac() failed");
    }

    dev = subnet->dev;
    d_assert(dev, goto cleanup,);
    if (sock_write(dev->sock, pkbuf->payload, pkbuf->len) <= 0)
        d_error("sock_write() failed");

cleanup:
    pkbuf_free(pkbuf);
    return 0;
}

status_t pgw_gtp_open()
{
    status_t rv;
    pgw_dev_t *dev = NULL;
    pgw_subnet_t *subnet = NULL;
    int rc;

    rv = gtp_server_list(&pgw_self()->gtpc_list, _gtpv2_c_recv_cb);
    d_assert(rv == CORE_OK, return CORE_ERROR,);
    rv = gtp_server_list(&pgw_self()->gtpc_list6, _gtpv2_c_recv_cb);
    d_assert(rv == CORE_OK, return CORE_ERROR,);

    pgw_self()->gtpc_sock = gtp_local_sock_first(&pgw_self()->gtpc_list);
    pgw_self()->gtpc_sock6 = gtp_local_sock_first(&pgw_self()->gtpc_list6);
    pgw_self()->gtpc_addr = gtp_local_addr_first(&pgw_self()->gtpc_list);
    pgw_self()->gtpc_addr6 = gtp_local_addr_first(&pgw_self()->gtpc_list6);

    d_assert(pgw_self()->gtpc_addr || pgw_self()->gtpc_addr6,
            return CORE_ERROR, "No GTP Server");

    rv = gtp_server_list(&pgw_self()->gtpu_list, _gtpv1_u_recv_cb);
    d_assert(rv == CORE_OK, return CORE_ERROR,);
    rv = gtp_server_list(&pgw_self()->gtpu_list6, _gtpv1_u_recv_cb);
    d_assert(rv == CORE_OK, return CORE_ERROR,);

    pgw_self()->gtpu_sock = gtp_local_sock_first(&pgw_self()->gtpu_list);
    pgw_self()->gtpu_sock6 = gtp_local_sock_first(&pgw_self()->gtpu_list6);
    pgw_self()->gtpu_addr = gtp_local_addr_first(&pgw_self()->gtpu_list);
    pgw_self()->gtpu_addr6 = gtp_local_addr_first(&pgw_self()->gtpu_list6);

    d_assert(pgw_self()->gtpu_addr || pgw_self()->gtpu_addr6,
            return CORE_ERROR, "No GTP Server");

    /* NOTE : tun device can be created via following command.
     *
     * $ sudo ip tuntap add name pgwtun mode tun
     *
     * Also, before running pgw, assign the one IP from IP pool of UE 
     * to pgwtun. The IP should not be assigned to UE
     *
     * $ sudo ifconfig pgwtun 45.45.0.1/16 up
     *
     */

    /* Open Tun interface */
    for (dev = pgw_dev_first(); dev; dev = pgw_dev_next(dev))
    {
        rc = tun_open(&dev->sock, (char *)dev->ifname, 0);
        if (rc != 0)
        {
            d_error("tun_open(dev:%s) failed", dev->ifname);
            return CORE_ERROR;
        }

        rc = sock_register(dev->sock, _gtpv1_tun_recv_cb, NULL);
        if (rc != 0)
        {
            d_error("sock_register(dev:%s) failed", dev->ifname);
            sock_delete(dev->sock);
            return CORE_ERROR;
        }
    }

    /* 
     * On Linux, it is possible to create a persistent tun/tap 
     * interface which will continue to exist even if nextepc quit, 
     * although this is normally not required. 
     * It can be useful to set up a tun/tap interface owned 
     * by a non-root user, so nextepc can be started without 
     * needing any root privileges at all.
     */

    /* Set P-to-P IP address with Netmask
     * Note that Linux will skip this configuration */
    for (subnet = pgw_subnet_first(); subnet; subnet = pgw_subnet_next(subnet))
    {
        d_assert(subnet->dev, return CORE_ERROR,);
        rc = tun_set_ip(subnet->dev->sock, &subnet->gw, &subnet->sub);
        if (rc != 0)
        {
            d_error("tun_set_ip(dev:%s) failed", subnet->dev->ifname);
            return CORE_ERROR;
        }
    }

    /* Link-Local Address for PGW_TUN */
    for (dev = pgw_dev_first(); dev; dev = pgw_dev_next(dev))
        dev->link_local_addr = core_link_local_addr_by_dev(dev->ifname);

    return CORE_OK;
}

status_t pgw_gtp_close()
{
    pgw_dev_t *dev = NULL;

    sock_delete_list(&pgw_self()->gtpc_list);
    sock_delete_list(&pgw_self()->gtpc_list6);

    sock_delete_list(&pgw_self()->gtpu_list);
    sock_delete_list(&pgw_self()->gtpu_list6);

    for (dev = pgw_dev_first(); dev; dev = pgw_dev_next(dev))
        sock_delete(dev->sock);

    return CORE_OK;
}

static status_t pgw_gtp_handle_multicast(pkbuf_t *recvbuf)
{
    status_t rv;
    struct ip *ip_h =  NULL;
    struct ip6_hdr *ip6_h =  NULL;

    ip_h = (struct ip *)recvbuf->payload;
    if (ip_h->ip_v == 6)
    {
#if COMPILE_ERROR_IN_MAC_OS_X  /* Compiler error in Mac OS X platform */
        ip6_h = (struct ip6_hdr *)recvbuf->payload;
        if (IN6_IS_ADDR_MULTICAST(&ip6_h->ip6_dst))
#else
        struct in6_addr ip6_dst;
        ip6_h = (struct ip6_hdr *)recvbuf->payload;
        memcpy(&ip6_dst, &ip6_h->ip6_dst, sizeof(struct in6_addr));
        if (IN6_IS_ADDR_MULTICAST(&ip6_dst))
#endif
        {
            hash_index_t *hi = NULL;

            /* IPv6 Multicast */
            for (hi = pgw_sess_first(); hi; hi = pgw_sess_next(hi))
            {
                pgw_sess_t *sess = pgw_sess_this(hi);
                d_assert(sess, return CORE_ERROR,);
                if (sess->ipv6)
                {
                    /* PDN IPv6 is avaiable */
                    pgw_bearer_t *bearer = pgw_default_bearer_in_sess(sess);
                    d_assert(bearer, return CORE_ERROR,);

                    rv = pgw_gtp_send_to_bearer(bearer, recvbuf);
                    d_assert(rv == CORE_OK,,
                            "pgw_gtp_send_to_bearer failed");

                    return PGW_GTP_HANDLED;
                }
            }
        }
    }

    return CORE_OK;
}

static status_t pgw_gtp_handle_slaac(pgw_sess_t *sess, pkbuf_t *recvbuf)
{
    status_t rv;
    struct ip *ip_h = NULL;

    d_assert(sess, return CORE_ERROR,);
    d_assert(recvbuf, return CORE_ERROR,);
    d_assert(recvbuf->payload, return CORE_ERROR,);
    ip_h = (struct ip *)recvbuf->payload;
    if (ip_h->ip_v == 6)
    {
        struct ip6_hdr *ip6_h = (struct ip6_hdr *)recvbuf->payload;
        if (ip6_h->ip6_nxt == IPPROTO_ICMPV6)
        {
            struct icmp6_hdr *icmp_h =
                (struct icmp6_hdr *)(recvbuf->payload + sizeof(struct ip6_hdr));
            if (icmp_h->icmp6_type == ND_ROUTER_SOLICIT)
            {
                d_trace(5, "[PGW]      Router Solict\n");
                if (sess->ipv6)
                {
                    rv = pgw_gtp_send_router_advertisement(
                            sess, ip6_h->ip6_src.s6_addr);
                    d_assert(rv == CORE_OK,,"send router advertisement failed");
                }
                return PGW_GTP_HANDLED;
            }
        }
    }

    return CORE_OK;
}

static status_t pgw_gtp_send_to_bearer(pgw_bearer_t *bearer, pkbuf_t *sendbuf)
{
    char buf[CORE_ADDRSTRLEN];
    status_t rv;
    gtp_header_t *gtp_h = NULL;

    d_assert(bearer, pkbuf_free(sendbuf); return CORE_ERROR,);
    d_assert(bearer->gnode, pkbuf_free(sendbuf); return CORE_ERROR,);
    d_assert(bearer->gnode->sock, pkbuf_free(sendbuf); return CORE_ERROR,);

    /* Add GTP-U header */
    rv = pkbuf_header(sendbuf, GTPV1U_HEADER_LEN);
    if (rv != CORE_OK)
    {
        d_error("pkbuf_header error");
        pkbuf_free(sendbuf);
        return CORE_ERROR;
    }
    
    gtp_h = (gtp_header_t *)sendbuf->payload;
    /* Bits    8  7  6  5  4  3  2  1
     *        +--+--+--+--+--+--+--+--+
     *        |version |PT| 1| E| S|PN|
     *        +--+--+--+--+--+--+--+--+
     *         0  0  1   1  0  0  0  0
     */
    gtp_h->flags = 0x30;
    gtp_h->type = GTPU_MSGTYPE_GPDU;
    gtp_h->length = htons(sendbuf->len - GTPV1U_HEADER_LEN);
    gtp_h->teid = htonl(bearer->sgw_s5u_teid);

    /* Send to SGW */
    d_trace(50, "[PGW] SEND : ");
    d_trace_hex(50, sendbuf->payload, sendbuf->len);

    d_trace(3, "[PGW] SEND GPU-U to SGW[%s] : TEID[0x%x]\n",
        CORE_ADDR(sock_remote_addr(bearer->gnode->sock), buf),
        bearer->sgw_s5u_teid);
    rv =  gtp_send(bearer->gnode, sendbuf);

    return rv;
}

static status_t pgw_gtp_send_router_advertisement(
        pgw_sess_t *sess, c_uint8_t *ip6_dst)
{
    status_t rv;
    pkbuf_t *pkbuf = NULL;

    pgw_bearer_t *bearer = NULL;
    pgw_ue_ip_t *ue_ip = NULL;
    pgw_subnet_t *subnet = NULL;
    pgw_dev_t *dev = NULL;

    ipsubnet_t src_ipsub;
    c_uint16_t plen = 0;
    c_uint8_t nxt = 0;
    c_uint8_t *p = NULL;
    struct ip6_hdr *ip6_h =  NULL;
    struct nd_router_advert *advert_h = NULL;
    struct nd_opt_prefix_info *prefix = NULL;

    d_assert(sess, return CORE_ERROR,);
    bearer = pgw_default_bearer_in_sess(sess);
    d_assert(bearer, return CORE_ERROR,);
    ue_ip = sess->ipv6;
    d_assert(ue_ip, return CORE_ERROR,);
    subnet = ue_ip->subnet;
    d_assert(subnet, return CORE_ERROR,);
    dev = subnet->dev;
    d_assert(dev, return CORE_ERROR,);

    pkbuf = pkbuf_alloc(GTPV1U_HEADER_LEN, 200);
    d_assert(pkbuf, return CORE_ERROR,);
    pkbuf->len = sizeof *ip6_h + sizeof *advert_h + sizeof *prefix;
    memset(pkbuf->payload, 0, pkbuf->len);

    p = (c_uint8_t *)pkbuf->payload;
    ip6_h = (struct ip6_hdr *)p;
    advert_h = (struct nd_router_advert *)((c_uint8_t *)ip6_h + sizeof *ip6_h);
    prefix = (struct nd_opt_prefix_info *)
        ((c_uint8_t*)advert_h + sizeof *advert_h);

    rv = core_ipsubnet(&src_ipsub, "fe80::1", NULL);
    d_assert(rv == CORE_OK, return CORE_ERROR,);
    if (dev->link_local_addr)
        memcpy(src_ipsub.sub, dev->link_local_addr->sin6.sin6_addr.s6_addr,
                sizeof src_ipsub.sub);

    advert_h->nd_ra_type = ND_ROUTER_ADVERT;
    advert_h->nd_ra_code = 0;
    advert_h->nd_ra_curhoplimit = 64;
    advert_h->nd_ra_flags_reserved = 0;
    advert_h->nd_ra_router_lifetime = htons(64800);  /* 64800s */
    advert_h->nd_ra_reachable = 0;
    advert_h->nd_ra_retransmit = 0;

    prefix->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
    prefix->nd_opt_pi_len = 4; /* 32bytes */
    prefix->nd_opt_pi_prefix_len = subnet->prefixlen;
    prefix->nd_opt_pi_flags_reserved =
        ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO;
    prefix->nd_opt_pi_valid_time = htonl(0xffffffff); /* Infinite */
    prefix->nd_opt_pi_preferred_time = htonl(0xffffffff); /* Infinite */
    memcpy(prefix->nd_opt_pi_prefix.s6_addr,
            subnet->sub.sub, sizeof prefix->nd_opt_pi_prefix.s6_addr);

    /* For IPv6 Pseudo-Header */
    plen = htons(sizeof *advert_h + sizeof *prefix);
    nxt = IPPROTO_ICMPV6;

    memcpy(p, src_ipsub.sub, sizeof src_ipsub.sub);
    p += sizeof src_ipsub.sub;
    memcpy(p, ip6_dst, IPV6_LEN);
    p += IPV6_LEN;
    p += 2; memcpy(p, &plen, 2); p += 2;
    p += 3; *p = nxt; p += 1;
    advert_h->nd_ra_cksum = in_cksum((c_uint16_t *)pkbuf->payload, pkbuf->len);

    ip6_h->ip6_flow = htonl(0x60000001);
    ip6_h->ip6_plen = plen;
    ip6_h->ip6_nxt = nxt;  /* ICMPv6 */
    ip6_h->ip6_hlim = 0xff;
    memcpy(ip6_h->ip6_src.s6_addr, src_ipsub.sub, sizeof src_ipsub.sub);
    memcpy(ip6_h->ip6_dst.s6_addr, ip6_dst, IPV6_LEN);
    
    rv = pgw_gtp_send_to_bearer(bearer, pkbuf);
    d_assert(rv == CORE_OK,, "pgw_gtp_send_to_bearer() failed");

    d_trace(5, "[PGW]      Router Advertisement\n");

    pkbuf_free(pkbuf);
    return rv;
}

c_uint16_t in_cksum(c_uint16_t *addr, int len)
{
    int nleft = len;
    c_uint32_t sum = 0;
    c_uint16_t *w = addr;
    c_uint16_t answer = 0;

    // Adding 16 bits sequentially in sum
    while (nleft > 1)
    {
        sum += *w;
        nleft -= 2;
        w++;
    }

    // If an odd byte is left
    if (nleft == 1)
    {
        *(c_uint8_t *) (&answer) = *(c_uint8_t *) w;
        sum += answer;
    }

    sum = (sum >> 16) + (sum & 0xffff);
    sum += (sum >> 16);
    answer = ~sum;

    return answer;
}