Harald Welte | 51b00a6 | 2014-04-03 09:37:38 -0400 | [diff] [blame] | 1 | /* Command line utility to create GTP link */ |
| 2 | |
| 3 | /* (C) 2014 by sysmocom - s.f.m.c. GmbH |
Pablo Neira Ayuso | 448bce4 | 2016-05-08 21:43:40 +0200 | [diff] [blame] | 4 | * (C) 2016 by Pablo Neira Ayuso <pablo@netfilter.org> |
| 5 | * |
Harald Welte | 51b00a6 | 2014-04-03 09:37:38 -0400 | [diff] [blame] | 6 | * 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
| 28 | #include <string.h> |
| 29 | #include <time.h> |
Pablo Neira Ayuso | 50826a5 | 2016-05-10 18:00:51 +0200 | [diff] [blame] | 30 | #include <netinet/in.h> |
| 31 | #include <arpa/inet.h> |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 32 | |
| 33 | #include <libmnl/libmnl.h> |
| 34 | #include <linux/if.h> |
| 35 | #include <linux/if_link.h> |
| 36 | #include <linux/rtnetlink.h> |
| 37 | |
Pablo Neira Ayuso | b9f6ffe | 2016-05-08 18:27:55 +0200 | [diff] [blame] | 38 | #include <linux/gtp.h> |
| 39 | #include <linux/if_link.h> |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 40 | |
Pablo Neira Ayuso | 448bce4 | 2016-05-08 21:43:40 +0200 | [diff] [blame] | 41 | #include <libgtpnl/gtpnl.h> |
| 42 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 43 | int main(int argc, char *argv[]) |
| 44 | { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 45 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
Harald Welte | 3bf55c3 | 2017-03-15 18:03:42 +0100 | [diff] [blame] | 46 | int ret, sgsn_mode = 0; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 47 | |
Harald Welte | 3bf55c3 | 2017-03-15 18:03:42 +0100 | [diff] [blame] | 48 | if (argc < 3) { |
Pablo Neira Ayuso | 448bce4 | 2016-05-08 21:43:40 +0200 | [diff] [blame] | 49 | printf("Usage: %s <add|del> <device>\n", argv[0]); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 50 | exit(EXIT_FAILURE); |
| 51 | } |
| 52 | |
Pablo Neira Ayuso | 448bce4 | 2016-05-08 21:43:40 +0200 | [diff] [blame] | 53 | 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 | |
Harald Welte | 3bf55c3 | 2017-03-15 18:03:42 +0100 | [diff] [blame] | 61 | if (argc > 3 && !strcmp(argv[3], "--sgsn")) |
| 62 | sgsn_mode = 1; |
| 63 | |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame] | 64 | int fd1 = socket(AF_INET, SOCK_DGRAM, 0); |
| 65 | int fd2 = socket(AF_INET, SOCK_DGRAM, 0); |
Pablo Neira Ayuso | 50826a5 | 2016-05-10 18:00:51 +0200 | [diff] [blame] | 66 | struct sockaddr_in sockaddr_fd1 = { |
| 67 | .sin_family = AF_INET, |
| 68 | .sin_port = htons(3386), |
| 69 | .sin_addr = { |
| 70 | .s_addr = INADDR_ANY, |
| 71 | }, |
| 72 | }; |
| 73 | struct sockaddr_in sockaddr_fd2 = { |
| 74 | .sin_family = AF_INET, |
| 75 | .sin_port = htons(2152), |
| 76 | .sin_addr = { |
| 77 | .s_addr = INADDR_ANY, |
| 78 | }, |
| 79 | }; |
| 80 | |
| 81 | if (bind(fd1, (struct sockaddr *) &sockaddr_fd1, |
| 82 | sizeof(sockaddr_fd1)) < 0) { |
| 83 | perror("bind"); |
| 84 | exit(EXIT_FAILURE); |
| 85 | } |
| 86 | if (bind(fd2, (struct sockaddr *) &sockaddr_fd2, |
| 87 | sizeof(sockaddr_fd2)) < 0) { |
| 88 | perror("bind"); |
| 89 | exit(EXIT_FAILURE); |
| 90 | } |
Pablo Neira Ayuso | 7aa2087 | 2014-02-24 11:38:52 +0100 | [diff] [blame] | 91 | |
Harald Welte | 3bf55c3 | 2017-03-15 18:03:42 +0100 | [diff] [blame] | 92 | if (sgsn_mode) |
| 93 | ret = gtp_dev_create_sgsn(-1, argv[2], fd1, fd2); |
| 94 | else |
| 95 | ret = gtp_dev_create(-1, argv[2], fd1, fd2); |
Harald Welte | a7a4df3 | 2017-03-15 20:53:08 +0100 | [diff] [blame] | 96 | if (ret < 0) { |
| 97 | perror("cannot create GTP device\n"); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 98 | exit(EXIT_FAILURE); |
| 99 | } |
| 100 | |
Pablo Neira Ayuso | 448bce4 | 2016-05-08 21:43:40 +0200 | [diff] [blame] | 101 | fprintf(stderr, "WARNING: attaching dummy socket descriptors. Keep " |
| 102 | "this process running for testing purposes.\n"); |
Pablo Neira Ayuso | 50826a5 | 2016-05-10 18:00:51 +0200 | [diff] [blame] | 103 | |
| 104 | while (1) { |
| 105 | struct sockaddr_in addr; |
| 106 | socklen_t len = sizeof(addr); |
| 107 | |
| 108 | ret = recvfrom(fd1, buf, sizeof(buf), 0, |
| 109 | (struct sockaddr *)&addr, &len); |
| 110 | printf("received %d bytes via UDP socket\n", ret); |
| 111 | } |
Pablo Neira Ayuso | 448bce4 | 2016-05-08 21:43:40 +0200 | [diff] [blame] | 112 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 113 | return 0; |
| 114 | } |