blob: 8aa9e987a97748f663bad09a124c239d6f7bd5ee [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>
30
31#include <libmnl/libmnl.h>
32#include <linux/if.h>
33#include <linux/if_link.h>
34#include <linux/rtnetlink.h>
35
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020036#include <linux/gtp.h>
37#include <linux/if_link.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010038
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +020039#include <libgtpnl/gtpnl.h>
40
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010041int main(int argc, char *argv[])
42{
43 struct mnl_socket *nl;
44 char buf[MNL_SOCKET_BUFFER_SIZE];
45 struct nlmsghdr *nlh;
46 struct ifinfomsg *ifm;
47 int ret;
48 unsigned int seq, portid, change = 0, flags = 0;
49 struct nlattr *nest, *nest2;
50
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +020051 if (argc != 3) {
52 printf("Usage: %s <add|del> <device>\n", argv[0]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010053 exit(EXIT_FAILURE);
54 }
55
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +020056 if (!strcmp(argv[1], "del")) {
57 printf("destroying gtp interface...\n");
58 if (gtp_dev_destroy(argv[2]) < 0)
59 perror("gtp_dev_destroy");
60
61 return 0;
62 }
63
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010064 nlh = mnl_nlmsg_put_header(buf);
65 nlh->nlmsg_type = RTM_NEWLINK;
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010066 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL |NLM_F_ACK;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010067 nlh->nlmsg_seq = seq = time(NULL);
68 ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
69 ifm->ifi_family = AF_INET;
70 ifm->ifi_change |= IFF_UP;
71 ifm->ifi_flags |= IFF_UP;
72
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010073 int fd1 = socket(AF_INET, SOCK_DGRAM, 0);
74 int fd2 = socket(AF_INET, SOCK_DGRAM, 0);
75
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +020076 mnl_attr_put_str(nlh, IFLA_IFNAME, argv[2]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010077 nest = mnl_attr_nest_start(nlh, IFLA_LINKINFO);
78 mnl_attr_put_str(nlh, IFLA_INFO_KIND, "gtp");
79 nest2 = mnl_attr_nest_start(nlh, IFLA_INFO_DATA);
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010080 mnl_attr_put_u32(nlh, IFLA_GTP_FD0, fd1);
81 mnl_attr_put_u32(nlh, IFLA_GTP_FD1, fd2);
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020082 mnl_attr_put_u32(nlh, IFLA_GTP_PDP_HASHSIZE, 131072);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010083 mnl_attr_nest_end(nlh, nest2);
84 mnl_attr_nest_end(nlh, nest);
85
86 nl = mnl_socket_open(NETLINK_ROUTE);
87 if (nl == NULL) {
88 perror("mnl_socket_open");
89 exit(EXIT_FAILURE);
90 }
91
92 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
93 perror("mnl_socket_bind");
94 exit(EXIT_FAILURE);
95 }
96 portid = mnl_socket_get_portid(nl);
97
98 mnl_nlmsg_fprintf(stdout, nlh, nlh->nlmsg_len,
99 sizeof(struct ifinfomsg));
100
101 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
102 perror("mnl_socket_send");
103 exit(EXIT_FAILURE);
104 }
105
106 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
107 if (ret == -1) {
108 perror("read");
109 exit(EXIT_FAILURE);
110 }
111
112 ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
113 if (ret == -1){
114 perror("callback");
115 exit(EXIT_FAILURE);
116 }
117
118 mnl_socket_close(nl);
119
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +0200120 fprintf(stderr, "WARNING: attaching dummy socket descriptors. Keep "
121 "this process running for testing purposes.\n");
122 pause();
123
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100124 return 0;
125}