blob: 0038b9ea1fbd511a58b5e57085d5df1865e1358c [file] [log] [blame]
Pablo Neira Ayuso14506662014-02-20 18:43:15 +01001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <string.h>
5#include <time.h>
6
7#include <libmnl/libmnl.h>
8#include <linux/if.h>
9#include <linux/if_link.h>
10#include <linux/rtnetlink.h>
11
12#include <linux/gtp_nl.h>
13
14int main(int argc, char *argv[])
15{
16 struct mnl_socket *nl;
17 char buf[MNL_SOCKET_BUFFER_SIZE];
18 struct nlmsghdr *nlh;
19 struct ifinfomsg *ifm;
20 int ret;
21 unsigned int seq, portid, change = 0, flags = 0;
22 struct nlattr *nest, *nest2;
23
24 if (argc != 2) {
25 printf("Usage: %s [ifname]\n", argv[0]);
26 exit(EXIT_FAILURE);
27 }
28
29 nlh = mnl_nlmsg_put_header(buf);
30 nlh->nlmsg_type = RTM_NEWLINK;
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010031 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL |NLM_F_ACK;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010032 nlh->nlmsg_seq = seq = time(NULL);
33 ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
34 ifm->ifi_family = AF_INET;
35 ifm->ifi_change |= IFF_UP;
36 ifm->ifi_flags |= IFF_UP;
37
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010038 fprintf(stderr, "WARNING: attaching dummy socket descriptors. Use "
39 "this command for testing purposes only.\n"):
40 int fd1 = socket(AF_INET, SOCK_DGRAM, 0);
41 int fd2 = socket(AF_INET, SOCK_DGRAM, 0);
42
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010043 mnl_attr_put_u32(nlh, IFLA_LINK, if_nametoindex(argv[1]));
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010044 mnl_attr_put_str(nlh, IFLA_IFNAME, "gtp0");
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010045 nest = mnl_attr_nest_start(nlh, IFLA_LINKINFO);
46 mnl_attr_put_str(nlh, IFLA_INFO_KIND, "gtp");
47 nest2 = mnl_attr_nest_start(nlh, IFLA_INFO_DATA);
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +010048 mnl_attr_put_u32(nlh, IFLA_GTP_FD0, fd1);
49 mnl_attr_put_u32(nlh, IFLA_GTP_FD1, fd2);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010050 mnl_attr_put_u32(nlh, IFLA_GTP_HASHSIZE, 131072);
51 mnl_attr_nest_end(nlh, nest2);
52 mnl_attr_nest_end(nlh, nest);
53
54 nl = mnl_socket_open(NETLINK_ROUTE);
55 if (nl == NULL) {
56 perror("mnl_socket_open");
57 exit(EXIT_FAILURE);
58 }
59
60 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
61 perror("mnl_socket_bind");
62 exit(EXIT_FAILURE);
63 }
64 portid = mnl_socket_get_portid(nl);
65
66 mnl_nlmsg_fprintf(stdout, nlh, nlh->nlmsg_len,
67 sizeof(struct ifinfomsg));
68
69 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
70 perror("mnl_socket_send");
71 exit(EXIT_FAILURE);
72 }
73
74 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
75 if (ret == -1) {
76 perror("read");
77 exit(EXIT_FAILURE);
78 }
79
80 ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
81 if (ret == -1){
82 perror("callback");
83 exit(EXIT_FAILURE);
84 }
85
86 mnl_socket_close(nl);
87
88 return 0;
89}