blob: 3d893eb9fcab295308bb866e0e59367c0c058fb0 [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
34#include <linux/gtp_nl.h>
35
36int main(int argc, char *argv[])
37{
38 struct mnl_socket *nl;
39 char buf[MNL_SOCKET_BUFFER_SIZE];
40 struct nlmsghdr *nlh;
41 struct ifinfomsg *ifm;
42 int ret;
43 unsigned int seq, portid, change = 0, flags = 0;
44 struct nlattr *nest, *nest2;
45
46 if (argc != 2) {
47 printf("Usage: %s [ifname]\n", argv[0]);
48 exit(EXIT_FAILURE);
49 }
50
51 nlh = mnl_nlmsg_put_header(buf);
52 nlh->nlmsg_type = RTM_NEWLINK;
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010053 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL |NLM_F_ACK;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010054 nlh->nlmsg_seq = seq = time(NULL);
55 ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
56 ifm->ifi_family = AF_INET;
57 ifm->ifi_change |= IFF_UP;
58 ifm->ifi_flags |= IFF_UP;
59
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010060 fprintf(stderr, "WARNING: attaching dummy socket descriptors. Use "
Pablo Neira Ayuso3a7dd0a2014-03-18 18:24:40 +010061 "this command for testing purposes only.\n");
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010062 int fd1 = socket(AF_INET, SOCK_DGRAM, 0);
63 int fd2 = socket(AF_INET, SOCK_DGRAM, 0);
64
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010065 mnl_attr_put_u32(nlh, IFLA_LINK, if_nametoindex(argv[1]));
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 Ayuso14506662014-02-20 18:43:15 +010072 mnl_attr_put_u32(nlh, IFLA_GTP_HASHSIZE, 131072);
73 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}