blob: 97dfa45743d1b6181f1d6e359c0af56e0f505c33 [file] [log] [blame]
Harald Welte51b00a62014-04-03 09:37:38 -04001/* Command line utility to create GTP link */
2
3/* (C) 2014 by sysmocom - s.f.m.c. GmbH
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +02004 * (C) 2016 by Pablo Neira Ayuso <pablo@netfilter.org>
5 *
Harald Welte51b00a62014-04-03 09:37:38 -04006 * Author: Pablo Neira Ayuso <pablo@gnumonks.org>
7 *
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010025#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <string.h>
29#include <time.h>
Pablo Neira Ayuso50826a52016-05-10 18:00:51 +020030#include <netinet/in.h>
31#include <arpa/inet.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010032
33#include <libmnl/libmnl.h>
34#include <linux/if.h>
35#include <linux/if_link.h>
36#include <linux/rtnetlink.h>
37
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020038#include <linux/gtp.h>
39#include <linux/if_link.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010040
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +020041#include <libgtpnl/gtpnl.h>
42
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010043int main(int argc, char *argv[])
44{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010045 char buf[MNL_SOCKET_BUFFER_SIZE];
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010046 int ret;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010047
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +020048 if (argc != 3) {
49 printf("Usage: %s <add|del> <device>\n", argv[0]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010050 exit(EXIT_FAILURE);
51 }
52
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +020053 if (!strcmp(argv[1], "del")) {
54 printf("destroying gtp interface...\n");
55 if (gtp_dev_destroy(argv[2]) < 0)
56 perror("gtp_dev_destroy");
57
58 return 0;
59 }
60
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010061 int fd1 = socket(AF_INET, SOCK_DGRAM, 0);
62 int fd2 = socket(AF_INET, SOCK_DGRAM, 0);
Pablo Neira Ayuso50826a52016-05-10 18:00:51 +020063 struct sockaddr_in sockaddr_fd1 = {
64 .sin_family = AF_INET,
65 .sin_port = htons(3386),
66 .sin_addr = {
67 .s_addr = INADDR_ANY,
68 },
69 };
70 struct sockaddr_in sockaddr_fd2 = {
71 .sin_family = AF_INET,
72 .sin_port = htons(2152),
73 .sin_addr = {
74 .s_addr = INADDR_ANY,
75 },
76 };
77
78 if (bind(fd1, (struct sockaddr *) &sockaddr_fd1,
79 sizeof(sockaddr_fd1)) < 0) {
80 perror("bind");
81 exit(EXIT_FAILURE);
82 }
83 if (bind(fd2, (struct sockaddr *) &sockaddr_fd2,
84 sizeof(sockaddr_fd2)) < 0) {
85 perror("bind");
86 exit(EXIT_FAILURE);
87 }
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010088
Harald Weltea7a4df32017-03-15 20:53:08 +010089 ret = gtp_dev_create(-1, argv[2], fd1, fd2);
90 if (ret < 0) {
91 perror("cannot create GTP device\n");
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010092 exit(EXIT_FAILURE);
93 }
94
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +020095 fprintf(stderr, "WARNING: attaching dummy socket descriptors. Keep "
96 "this process running for testing purposes.\n");
Pablo Neira Ayuso50826a52016-05-10 18:00:51 +020097
98 while (1) {
99 struct sockaddr_in addr;
100 socklen_t len = sizeof(addr);
101
102 ret = recvfrom(fd1, buf, sizeof(buf), 0,
103 (struct sockaddr *)&addr, &len);
104 printf("received %d bytes via UDP socket\n", ret);
105 }
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +0200106
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100107 return 0;
108}