blob: 3ea7f356dfd12a2bd4281e418c0cd21f7747dc23 [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
4 * Author: Pablo Neira Ayuso <pablo@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010023#include <stdio.h>
24#include <stdlib.h>
25#include <unistd.h>
26#include <string.h>
27#include <time.h>
28
29#include <libmnl/libmnl.h>
30#include <linux/if.h>
31#include <linux/if_link.h>
32#include <linux/rtnetlink.h>
33
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020034#include <linux/gtp.h>
35#include <linux/if_link.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010036
37int main(int argc, char *argv[])
38{
39 struct mnl_socket *nl;
40 char buf[MNL_SOCKET_BUFFER_SIZE];
41 struct nlmsghdr *nlh;
42 struct ifinfomsg *ifm;
43 int ret;
44 unsigned int seq, portid, change = 0, flags = 0;
45 struct nlattr *nest, *nest2;
46
Andreas Schultzaf422a42016-04-11 16:10:02 +020047 if (argc != 1) {
48 printf("Usage: %s\n", argv[0]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010049 exit(EXIT_FAILURE);
50 }
51
52 nlh = mnl_nlmsg_put_header(buf);
53 nlh->nlmsg_type = RTM_NEWLINK;
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010054 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL |NLM_F_ACK;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010055 nlh->nlmsg_seq = seq = time(NULL);
56 ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
57 ifm->ifi_family = AF_INET;
58 ifm->ifi_change |= IFF_UP;
59 ifm->ifi_flags |= IFF_UP;
60
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010061 fprintf(stderr, "WARNING: attaching dummy socket descriptors. Use "
Pablo Neira Ayuso3a7dd0a2014-03-18 18:24:40 +010062 "this command for testing purposes only.\n");
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010063 int fd1 = socket(AF_INET, SOCK_DGRAM, 0);
64 int fd2 = socket(AF_INET, SOCK_DGRAM, 0);
65
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010066 mnl_attr_put_str(nlh, IFLA_IFNAME, "gtp0");
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010067 nest = mnl_attr_nest_start(nlh, IFLA_LINKINFO);
68 mnl_attr_put_str(nlh, IFLA_INFO_KIND, "gtp");
69 nest2 = mnl_attr_nest_start(nlh, IFLA_INFO_DATA);
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010070 mnl_attr_put_u32(nlh, IFLA_GTP_FD0, fd1);
71 mnl_attr_put_u32(nlh, IFLA_GTP_FD1, fd2);
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020072 mnl_attr_put_u32(nlh, IFLA_GTP_PDP_HASHSIZE, 131072);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010073 mnl_attr_nest_end(nlh, nest2);
74 mnl_attr_nest_end(nlh, nest);
75
76 nl = mnl_socket_open(NETLINK_ROUTE);
77 if (nl == NULL) {
78 perror("mnl_socket_open");
79 exit(EXIT_FAILURE);
80 }
81
82 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
83 perror("mnl_socket_bind");
84 exit(EXIT_FAILURE);
85 }
86 portid = mnl_socket_get_portid(nl);
87
88 mnl_nlmsg_fprintf(stdout, nlh, nlh->nlmsg_len,
89 sizeof(struct ifinfomsg));
90
91 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
92 perror("mnl_socket_send");
93 exit(EXIT_FAILURE);
94 }
95
96 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
97 if (ret == -1) {
98 perror("read");
99 exit(EXIT_FAILURE);
100 }
101
102 ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
103 if (ret == -1){
104 perror("callback");
105 exit(EXIT_FAILURE);
106 }
107
108 mnl_socket_close(nl);
109
110 return 0;
111}