blob: 33a78d8fe2f2e31072a6558330c5e309bb343473 [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
Harald Welted2bb0bc2016-07-28 20:34:45 +02009 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of the
11 * License, or (at your option) any later version.
Harald Welte51b00a62014-04-03 09:37:38 -040012 *
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
Harald Welted2bb0bc2016-07-28 20:34:45 +020016 * GNU Lesser General Public License for more details.
Harald Welte51b00a62014-04-03 09:37:38 -040017 *
Harald Welted2bb0bc2016-07-28 20:34:45 +020018 * You should have received a copy of the GNU Lesser General Public License
Harald Welte51b00a62014-04-03 09:37:38 -040019 * 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>
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020040#include <linux/gtp.h>
41#include <linux/if_link.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010042
43#include "internal.h"
44
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010045static void gtp_build_payload(struct nlmsghdr *nlh, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010046{
Pablo Neira Ayuso6f1c38f2024-01-31 13:40:35 +010047 if (t->flags & GTP_TUN_FAMILY)
48 mnl_attr_put_u8(nlh, GTPA_FAMILY, t->ms_addr.family);
49 if (t->flags & GTP_TUN_VERSION)
50 mnl_attr_put_u32(nlh, GTPA_VERSION, t->gtp_version);
51 if (t->flags & GTP_TUN_IFNS)
Andreas Schultz49773302016-04-11 16:09:56 +020052 mnl_attr_put_u32(nlh, GTPA_NET_NS_FD, t->ifns);
Pablo Neira Ayuso6f1c38f2024-01-31 13:40:35 +010053 if (t->flags & GTP_TUN_IFIDX)
54 mnl_attr_put_u32(nlh, GTPA_LINK, t->ifidx);
Oliver Smitha2956152023-10-19 14:11:19 +020055
Pablo Neira Ayuso6f1c38f2024-01-31 13:40:35 +010056 if (t->flags & GTP_TUN_MS_ADDR) {
57 switch (t->ms_addr.family) {
58 case AF_INET:
59 mnl_attr_put_u32(nlh, GTPA_MS_ADDRESS, t->ms_addr.ip4.s_addr);
60 break;
61 case AF_INET6:
62 mnl_attr_put(nlh, GTPA_MS_ADDR6, sizeof(t->ms_addr.ip6), &t->ms_addr.ip6);
63 break;
64 default:
65 /* No addr is set when deleting a tunnel */
66 break;
67 }
Oliver Smitha2956152023-10-19 14:11:19 +020068 }
69
Pablo Neira Ayuso6f1c38f2024-01-31 13:40:35 +010070 if (t->flags & GTP_TUN_SGSN_ADDR) {
71 switch (t->sgsn_addr.family) {
72 case AF_INET:
73 mnl_attr_put_u32(nlh, GTPA_PEER_ADDRESS, t->sgsn_addr.ip4.s_addr);
74 break;
75 case AF_INET6:
76 mnl_attr_put(nlh, GTPA_PEER_ADDR6, sizeof(t->sgsn_addr.ip6), &t->sgsn_addr.ip6);
77 break;
78 default:
79 /* No addr is set when deleting a tunnel */
80 break;
81 }
Oliver Smitha2956152023-10-19 14:11:19 +020082 }
83
Pablo Neira Ayuso6f1c38f2024-01-31 13:40:35 +010084 if (t->flags & GTP_TUN_VERSION) {
85 if (t->gtp_version == GTP_V0) {
86 if (t->flags & GTP_TUN_V0_TID)
87 mnl_attr_put_u64(nlh, GTPA_TID, t->u.v0.tid);
88 if (t->flags & GTP_TUN_V0_FLOWID)
89 mnl_attr_put_u16(nlh, GTPA_FLOW, t->u.v0.flowid);
90 } else if (t->gtp_version == GTP_V1) {
91 if (t->flags & GTP_TUN_V1_I_TEI)
92 mnl_attr_put_u32(nlh, GTPA_I_TEI, t->u.v1.i_tei);
93 if (t->flags & GTP_TUN_V1_O_TEI)
94 mnl_attr_put_u32(nlh, GTPA_O_TEI, t->u.v1.o_tei);
95 }
Andreas Schultz17c816f2016-04-11 16:10:03 +020096 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010097}
98
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010099int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100100{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100101 struct nlmsghdr *nlh;
102 char buf[MNL_SOCKET_BUFFER_SIZE];
103 uint32_t seq = time(NULL);
104
Pablo Neira Ayuso18532952014-02-22 22:09:59 +0100105 if (t->gtp_version > GTP_V1) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100106 fprintf(stderr, "wrong GTP version %u, use v0 or v1\n",
Pablo Neira Ayuso18532952014-02-22 22:09:59 +0100107 t->gtp_version);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100108 return -1;
109 }
110
111 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_EXCL | NLM_F_ACK, ++seq,
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +0200112 GTP_CMD_NEWPDP);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +0100113 gtp_build_payload(nlh, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100114
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +0100115 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100116 perror("genl_socket_talk");
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +0100117 return -1;
118 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100119
120 return 0;
121}
122EXPORT_SYMBOL(gtp_add_tunnel);
123
Pablo Neira Ayuso18532952014-02-22 22:09:59 +0100124int gtp_del_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100125{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100126 char buf[MNL_SOCKET_BUFFER_SIZE];
127 struct nlmsghdr *nlh;
128 uint32_t seq = time(NULL);
129
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100130 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_ACK, ++seq,
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +0200131 GTP_CMD_DELPDP);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +0100132 gtp_build_payload(nlh, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100133
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +0100134 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100135 perror("genl_socket_talk");
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +0100136 return -1;
137 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100138
139 return 0;
140}
141EXPORT_SYMBOL(gtp_del_tunnel);
142
143struct gtp_pdp {
Pablo Neira Ayusoaac20a72024-01-31 13:08:58 +0100144 uint32_t ifidx;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100145 uint32_t version;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200146 union {
147 struct {
148 uint64_t tid;
149 } v0;
150 struct {
151 uint32_t i_tei;
152 uint32_t o_tei;
153 } v1;
154 } u;
Oliver Smith717db092023-10-18 15:16:12 +0200155 struct gtp_addr sgsn_addr;
156 struct gtp_addr ms_addr;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100157};
158
159static int genl_gtp_validate_cb(const struct nlattr *attr, void *data)
160{
161 const struct nlattr **tb = data;
162 int type = mnl_attr_get_type(attr);
163
junpei yoshino345d6872016-08-25 13:06:53 +0900164 if (mnl_attr_type_valid(attr, GTPA_MAX) < 0)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100165 return MNL_CB_OK;
166
167 switch(type) {
Oliver Smitha2956152023-10-19 14:11:19 +0200168 case GTPA_FAMILY:
169 if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0) {
170 perror("mnl_attr_validate");
171 return MNL_CB_ERROR;
172 }
173 break;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100174 case GTPA_TID:
175 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) {
176 perror("mnl_attr_validate");
177 return MNL_CB_ERROR;
178 }
179 break;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200180 case GTPA_O_TEI:
181 case GTPA_I_TEI:
Jonas Bonn6e9afbb2017-03-24 15:19:19 +0100182 case GTPA_PEER_ADDRESS:
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100183 case GTPA_MS_ADDRESS:
184 case GTPA_VERSION:
Pablo Neira Ayusoaac20a72024-01-31 13:08:58 +0100185 case GTPA_LINK:
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100186 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
187 perror("mnl_attr_validate");
188 return MNL_CB_ERROR;
189 }
190 break;
Oliver Smitha2956152023-10-19 14:11:19 +0200191 case GTPA_PEER_ADDR6:
192 case GTPA_MS_ADDR6:
193 if (mnl_attr_validate2(attr, MNL_TYPE_BINARY,
194 sizeof(struct in6_addr)) < 0) {
195 perror("mnl_attr_validate");
196 return MNL_CB_ERROR;
197 }
198 break;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100199 default:
200 break;
201 }
202 tb[type] = attr;
203 return MNL_CB_OK;
204}
205
206static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
207{
208 struct nlattr *tb[GTPA_MAX + 1] = {};
Pablo Neira Ayuso99a5c322023-10-05 20:50:17 +0200209 char buf[INET6_ADDRSTRLEN];
Pablo Neira Ayuso0829e0e2014-02-22 22:19:35 +0100210 struct gtp_pdp pdp = {};
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100211 struct genlmsghdr *genl;
212
213 mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb);
Oliver Smitha2956152023-10-19 14:11:19 +0200214
Pablo Neira Ayusoaac20a72024-01-31 13:08:58 +0100215 if (tb[GTPA_LINK])
216 pdp.ifidx = mnl_attr_get_u32(tb[GTPA_LINK]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100217 if (tb[GTPA_TID])
Andreas Schultz17c816f2016-04-11 16:10:03 +0200218 pdp.u.v0.tid = mnl_attr_get_u64(tb[GTPA_TID]);
219 if (tb[GTPA_I_TEI])
220 pdp.u.v1.i_tei = mnl_attr_get_u32(tb[GTPA_I_TEI]);
221 if (tb[GTPA_O_TEI])
222 pdp.u.v1.o_tei = mnl_attr_get_u32(tb[GTPA_O_TEI]);
Oliver Smitha2956152023-10-19 14:11:19 +0200223
Jonas Bonn6e9afbb2017-03-24 15:19:19 +0100224 if (tb[GTPA_PEER_ADDRESS]) {
Oliver Smith717db092023-10-18 15:16:12 +0200225 pdp.sgsn_addr.family = AF_INET;
Oliver Smitha2956152023-10-19 14:11:19 +0200226 pdp.sgsn_addr.ip4.s_addr = mnl_attr_get_u32(tb[GTPA_PEER_ADDRESS]);
227 } else if (tb[GTPA_PEER_ADDR6]) {
228 pdp.sgsn_addr.family = AF_INET6;
229 memcpy(&pdp.sgsn_addr.ip6, mnl_attr_get_payload(tb[GTPA_PEER_ADDR6]),
230 sizeof(struct in6_addr));
231 } else {
232 fprintf(stderr, "sgsn_addr: no IPv4 nor IPv6 set\n");
233 return MNL_CB_ERROR;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100234 }
Oliver Smitha2956152023-10-19 14:11:19 +0200235
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100236 if (tb[GTPA_MS_ADDRESS]) {
Oliver Smith717db092023-10-18 15:16:12 +0200237 pdp.ms_addr.family = AF_INET;
238 pdp.ms_addr.ip4.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]);
Oliver Smitha2956152023-10-19 14:11:19 +0200239 } else if (tb[GTPA_MS_ADDR6]) {
240 pdp.ms_addr.family = AF_INET6;
241 memcpy(&pdp.ms_addr.ip6, mnl_attr_get_payload(tb[GTPA_MS_ADDR6]), sizeof(struct in6_addr));
242 } else {
243 fprintf(stderr, "ms_addr: no IPv4 nor IPv6 set\n");
244 return MNL_CB_ERROR;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100245 }
246
Oliver Smitha2956152023-10-19 14:11:19 +0200247 if (tb[GTPA_FAMILY] && mnl_attr_get_u32(tb[GTPA_FAMILY]) != pdp.ms_addr.family) {
248 fprintf(stderr, "ms_addr family does not match GTPA_FAMILY\n");
249 return MNL_CB_ERROR;
Pablo Neira Ayusof69f8c72016-05-08 18:52:45 +0200250 }
Oliver Smitha2956152023-10-19 14:11:19 +0200251
Pablo Neira Ayusoaac20a72024-01-31 13:08:58 +0100252 printf("%s ", if_indextoname(pdp.ifidx, buf));
253
Oliver Smitha2956152023-10-19 14:11:19 +0200254 if (tb[GTPA_VERSION])
255 pdp.version = mnl_attr_get_u32(tb[GTPA_VERSION]);
256
257 printf("version %u ", pdp.version);
258
259 if (pdp.version == GTP_V0)
260 printf("tid %"PRIu64" ", pdp.u.v0.tid);
261 else if (pdp.version == GTP_V1)
262 printf("tei %u/%u ", pdp.u.v1.i_tei, pdp.u.v1.o_tei);
263
264 inet_ntop(pdp.ms_addr.family, &pdp.ms_addr.ip4, buf, sizeof(buf));
265 printf("ms_addr %s ", buf);
266
267 inet_ntop(pdp.sgsn_addr.family, &pdp.sgsn_addr.ip4, buf, sizeof(buf));
Pablo Neira Ayusof69f8c72016-05-08 18:52:45 +0200268 printf("sgsn_addr %s\n", buf);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100269
270 return MNL_CB_OK;
271}
272
273int gtp_list_tunnel(int genl_id, struct mnl_socket *nl)
274{
275 char buf[MNL_SOCKET_BUFFER_SIZE];
276 struct nlmsghdr *nlh;
277 uint32_t seq = time(NULL);
278
279 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_DUMP, 0,
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +0200280 GTP_CMD_GETPDP);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100281
282 if (genl_socket_talk(nl, nlh, seq, genl_gtp_attr_cb, NULL) < 0) {
283 perror("genl_socket_talk");
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +0100284 return -1;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100285 }
286
287 return 0;
288}
289EXPORT_SYMBOL(gtp_list_tunnel);