blob: c9c50c866733d34dca8b4e04db113e68f889788e [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;
31 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
32 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
38 mnl_attr_put_u32(nlh, IFLA_LINK, if_nametoindex(argv[1]));
39 nest = mnl_attr_nest_start(nlh, IFLA_LINKINFO);
40 mnl_attr_put_str(nlh, IFLA_INFO_KIND, "gtp");
41 nest2 = mnl_attr_nest_start(nlh, IFLA_INFO_DATA);
42 mnl_attr_put_u32(nlh, IFLA_GTP_LOCAL_ADDR_IPV4, 0);
43 mnl_attr_put_u32(nlh, IFLA_GTP_HASHSIZE, 131072);
44 mnl_attr_nest_end(nlh, nest2);
45 mnl_attr_nest_end(nlh, nest);
46
47 nl = mnl_socket_open(NETLINK_ROUTE);
48 if (nl == NULL) {
49 perror("mnl_socket_open");
50 exit(EXIT_FAILURE);
51 }
52
53 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
54 perror("mnl_socket_bind");
55 exit(EXIT_FAILURE);
56 }
57 portid = mnl_socket_get_portid(nl);
58
59 mnl_nlmsg_fprintf(stdout, nlh, nlh->nlmsg_len,
60 sizeof(struct ifinfomsg));
61
62 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
63 perror("mnl_socket_send");
64 exit(EXIT_FAILURE);
65 }
66
67 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
68 if (ret == -1) {
69 perror("read");
70 exit(EXIT_FAILURE);
71 }
72
73 ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
74 if (ret == -1){
75 perror("callback");
76 exit(EXIT_FAILURE);
77 }
78
79 mnl_socket_close(nl);
80
81 return 0;
82}