blob: 3c7deeda5eff8e1258edbc41a982cc5298830bf1 [file] [log] [blame]
Harald Welte51b00a62014-04-03 09:37:38 -04001/* GTP specific Generic Netlink helper functions */
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 <string.h>
26#include <unistd.h>
27#include <time.h>
28#include <arpa/inet.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <inttypes.h>
32
33#include <libmnl/libmnl.h>
34#include <linux/genetlink.h>
35
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010036#include <libgtpnl/gtp.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010037#include <libgtpnl/gtpnl.h>
38
39#include <net/if.h>
40#include <linux/gtp_nl.h>
41
42#include "internal.h"
43
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010044static void gtp_build_payload(struct nlmsghdr *nlh, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010045{
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010046 mnl_attr_put_u32(nlh, GTPA_VERSION, t->gtp_version);
Andreas Schultz49773302016-04-11 16:09:56 +020047 if (t->ifns >= 0)
48 mnl_attr_put_u32(nlh, GTPA_NET_NS_FD, t->ifns);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010049 mnl_attr_put_u32(nlh, GTPA_LINK, t->ifidx);
50 mnl_attr_put_u32(nlh, GTPA_SGSN_ADDRESS, t->sgsn_addr.s_addr);
51 mnl_attr_put_u32(nlh, GTPA_MS_ADDRESS, t->ms_addr.s_addr);
Andreas Schultz17c816f2016-04-11 16:10:03 +020052 if (t->gtp_version == GTP_V0) {
53 mnl_attr_put_u64(nlh, GTPA_TID, t->u.v0.tid);
54 mnl_attr_put_u16(nlh, GTPA_FLOW, t->u.v0.flowid);
55 } else if (t->gtp_version == GTP_V1) {
56 mnl_attr_put_u32(nlh, GTPA_I_TEI, t->u.v1.i_tei);
57 mnl_attr_put_u32(nlh, GTPA_O_TEI, t->u.v1.o_tei);
58 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010059}
60
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010061int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010062{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010063 struct nlmsghdr *nlh;
64 char buf[MNL_SOCKET_BUFFER_SIZE];
65 uint32_t seq = time(NULL);
66
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010067 if (t->gtp_version > GTP_V1) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010068 fprintf(stderr, "wrong GTP version %u, use v0 or v1\n",
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010069 t->gtp_version);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010070 return -1;
71 }
72
73 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_EXCL | NLM_F_ACK, ++seq,
74 GTP_CMD_TUNNEL_NEW);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010075 gtp_build_payload(nlh, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010076
77 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0)
78 perror("genl_socket_talk");
79
80 return 0;
81}
82EXPORT_SYMBOL(gtp_add_tunnel);
83
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010084int gtp_del_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010085{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010086 char buf[MNL_SOCKET_BUFFER_SIZE];
87 struct nlmsghdr *nlh;
88 uint32_t seq = time(NULL);
89
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010090 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_ACK, ++seq,
91 GTP_CMD_TUNNEL_DELETE);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010092 gtp_build_payload(nlh, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010093
94 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0)
95 perror("genl_socket_talk");
96
97 return 0;
98}
99EXPORT_SYMBOL(gtp_del_tunnel);
100
101struct gtp_pdp {
102 uint32_t version;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200103 union {
104 struct {
105 uint64_t tid;
106 } v0;
107 struct {
108 uint32_t i_tei;
109 uint32_t o_tei;
110 } v1;
111 } u;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100112 struct in_addr sgsn_addr;
113 struct in_addr ms_addr;
114};
115
116static int genl_gtp_validate_cb(const struct nlattr *attr, void *data)
117{
118 const struct nlattr **tb = data;
119 int type = mnl_attr_get_type(attr);
120
121 if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0)
122 return MNL_CB_OK;
123
124 switch(type) {
125 case GTPA_TID:
126 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) {
127 perror("mnl_attr_validate");
128 return MNL_CB_ERROR;
129 }
130 break;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200131 case GTPA_O_TEI:
132 case GTPA_I_TEI:
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100133 case GTPA_SGSN_ADDRESS:
134 case GTPA_MS_ADDRESS:
135 case GTPA_VERSION:
136 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
137 perror("mnl_attr_validate");
138 return MNL_CB_ERROR;
139 }
140 break;
141 default:
142 break;
143 }
144 tb[type] = attr;
145 return MNL_CB_OK;
146}
147
148static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
149{
150 struct nlattr *tb[GTPA_MAX + 1] = {};
Pablo Neira Ayuso0829e0e2014-02-22 22:19:35 +0100151 struct gtp_pdp pdp = {};
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100152 struct genlmsghdr *genl;
153
154 mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb);
155 if (tb[GTPA_TID])
Andreas Schultz17c816f2016-04-11 16:10:03 +0200156 pdp.u.v0.tid = mnl_attr_get_u64(tb[GTPA_TID]);
157 if (tb[GTPA_I_TEI])
158 pdp.u.v1.i_tei = mnl_attr_get_u32(tb[GTPA_I_TEI]);
159 if (tb[GTPA_O_TEI])
160 pdp.u.v1.o_tei = mnl_attr_get_u32(tb[GTPA_O_TEI]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100161 if (tb[GTPA_SGSN_ADDRESS]) {
162 pdp.sgsn_addr.s_addr =
163 mnl_attr_get_u32(tb[GTPA_SGSN_ADDRESS]);
164 }
165 if (tb[GTPA_MS_ADDRESS]) {
166 pdp.ms_addr.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]);
167 }
168 if (tb[GTPA_VERSION]) {
169 pdp.version = mnl_attr_get_u32(tb[GTPA_VERSION]);
170 }
171
172 printf("version %u ", pdp.version);
Andreas Schultz17c816f2016-04-11 16:10:03 +0200173 if (pdp.version == GTP_V0)
174 printf("tid %"PRIu64" ms_addr %s ",
175 pdp.u.v0.tid, inet_ntoa(pdp.sgsn_addr));
176 else if (pdp.version == GTP_V1)
177 printf("tei %u/%u ms_addr %s ", pdp.u.v1.i_tei,
178 pdp.u.v1.o_tei, inet_ntoa(pdp.sgsn_addr));
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100179 printf("sgsn_addr %s\n", inet_ntoa(pdp.ms_addr));
180
181 return MNL_CB_OK;
182}
183
184int gtp_list_tunnel(int genl_id, struct mnl_socket *nl)
185{
186 char buf[MNL_SOCKET_BUFFER_SIZE];
187 struct nlmsghdr *nlh;
188 uint32_t seq = time(NULL);
189
190 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_DUMP, 0,
191 GTP_CMD_TUNNEL_GET);
192
193 if (genl_socket_talk(nl, nlh, seq, genl_gtp_attr_cb, NULL) < 0) {
194 perror("genl_socket_talk");
195 return 0;
196 }
197
198 return 0;
199}
200EXPORT_SYMBOL(gtp_list_tunnel);