blob: 4891b087b4a2092249c03058fcd2bfc22b12fd35 [file] [log] [blame]
Pablo Neira Ayuso14506662014-02-20 18:43:15 +01001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include <time.h>
6#include <arpa/inet.h>
7#include <sys/socket.h>
8#include <netinet/in.h>
9#include <inttypes.h>
10
11#include <libmnl/libmnl.h>
12#include <linux/genetlink.h>
13
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010014#include <libgtpnl/gtp.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010015#include <libgtpnl/gtpnl.h>
16
17#include <net/if.h>
18#include <linux/gtp_nl.h>
19
20#include "internal.h"
21
22static void gtp_build_payload(struct nlmsghdr *nlh, uint64_t tid,
23 uint32_t ifidx, uint32_t sgsn_addr,
24 uint32_t ms_addr, uint32_t version)
25{
26 mnl_attr_put_u32(nlh, GTPA_VERSION, version);
27 mnl_attr_put_u32(nlh, GTPA_LINK, ifidx);
28 mnl_attr_put_u32(nlh, GTPA_SGSN_ADDRESS, sgsn_addr);
29 mnl_attr_put_u32(nlh, GTPA_MS_ADDRESS, ms_addr);
30 mnl_attr_put_u64(nlh, GTPA_TID, tid);
31}
32
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010033int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010034{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010035 struct nlmsghdr *nlh;
36 char buf[MNL_SOCKET_BUFFER_SIZE];
37 uint32_t seq = time(NULL);
38
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010039 if (t->gtp_version > GTP_V1) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010040 fprintf(stderr, "wrong GTP version %u, use v0 or v1\n",
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010041 t->gtp_version);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010042 return -1;
43 }
44
45 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_EXCL | NLM_F_ACK, ++seq,
46 GTP_CMD_TUNNEL_NEW);
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010047 gtp_build_payload(nlh, t->tid, t->ifidx, t->sgsn_addr.s_addr,
48 t->ms_addr.s_addr, t->gtp_version);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010049
50 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0)
51 perror("genl_socket_talk");
52
53 return 0;
54}
55EXPORT_SYMBOL(gtp_add_tunnel);
56
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010057int gtp_del_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010058{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010059 char buf[MNL_SOCKET_BUFFER_SIZE];
60 struct nlmsghdr *nlh;
61 uint32_t seq = time(NULL);
62
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010063 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_ACK, ++seq,
64 GTP_CMD_TUNNEL_DELETE);
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010065 gtp_build_payload(nlh, t->tid, t->ifidx, 0, 0, t->gtp_version);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010066
67 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0)
68 perror("genl_socket_talk");
69
70 return 0;
71}
72EXPORT_SYMBOL(gtp_del_tunnel);
73
74struct gtp_pdp {
75 uint32_t version;
76 uint64_t tid;
77 struct in_addr sgsn_addr;
78 struct in_addr ms_addr;
79};
80
81static int genl_gtp_validate_cb(const struct nlattr *attr, void *data)
82{
83 const struct nlattr **tb = data;
84 int type = mnl_attr_get_type(attr);
85
86 if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0)
87 return MNL_CB_OK;
88
89 switch(type) {
90 case GTPA_TID:
91 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) {
92 perror("mnl_attr_validate");
93 return MNL_CB_ERROR;
94 }
95 break;
96 case GTPA_SGSN_ADDRESS:
97 case GTPA_MS_ADDRESS:
98 case GTPA_VERSION:
99 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
100 perror("mnl_attr_validate");
101 return MNL_CB_ERROR;
102 }
103 break;
104 default:
105 break;
106 }
107 tb[type] = attr;
108 return MNL_CB_OK;
109}
110
111static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
112{
113 struct nlattr *tb[GTPA_MAX + 1] = {};
114 struct gtp_pdp pdp;
115 struct genlmsghdr *genl;
116
117 mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb);
118 if (tb[GTPA_TID])
119 pdp.tid = mnl_attr_get_u64(tb[GTPA_TID]);
120 if (tb[GTPA_SGSN_ADDRESS]) {
121 pdp.sgsn_addr.s_addr =
122 mnl_attr_get_u32(tb[GTPA_SGSN_ADDRESS]);
123 }
124 if (tb[GTPA_MS_ADDRESS]) {
125 pdp.ms_addr.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]);
126 }
127 if (tb[GTPA_VERSION]) {
128 pdp.version = mnl_attr_get_u32(tb[GTPA_VERSION]);
129 }
130
131 printf("version %u ", pdp.version);
132 printf("tid %"PRIu64" ms_addr %s ", pdp.tid, inet_ntoa(pdp.sgsn_addr));
133 printf("sgsn_addr %s\n", inet_ntoa(pdp.ms_addr));
134
135 return MNL_CB_OK;
136}
137
138int gtp_list_tunnel(int genl_id, struct mnl_socket *nl)
139{
140 char buf[MNL_SOCKET_BUFFER_SIZE];
141 struct nlmsghdr *nlh;
142 uint32_t seq = time(NULL);
143
144 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_DUMP, 0,
145 GTP_CMD_TUNNEL_GET);
146
147 if (genl_socket_talk(nl, nlh, seq, genl_gtp_attr_cb, NULL) < 0) {
148 perror("genl_socket_talk");
149 return 0;
150 }
151
152 return 0;
153}
154EXPORT_SYMBOL(gtp_list_tunnel);