blob: b8edc7237531e7a6f44a28ea7e9f31244c44c9cd [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 Ayusob976ffa2014-03-20 13:56:55 +010047 mnl_attr_put_u32(nlh, GTPA_VERSION, t->gtp_version);
Andreas Schultz49773302016-04-11 16:09:56 +020048 if (t->ifns >= 0)
49 mnl_attr_put_u32(nlh, GTPA_NET_NS_FD, t->ifns);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010050 mnl_attr_put_u32(nlh, GTPA_LINK, t->ifidx);
Harald Welte03cb4c22017-11-12 22:01:13 +090051 if (t->sgsn_addr.s_addr)
52 mnl_attr_put_u32(nlh, GTPA_PEER_ADDRESS, t->sgsn_addr.s_addr);
53 if (t->ms_addr.s_addr)
54 mnl_attr_put_u32(nlh, GTPA_MS_ADDRESS, t->ms_addr.s_addr);
Andreas Schultz17c816f2016-04-11 16:10:03 +020055 if (t->gtp_version == GTP_V0) {
56 mnl_attr_put_u64(nlh, GTPA_TID, t->u.v0.tid);
57 mnl_attr_put_u16(nlh, GTPA_FLOW, t->u.v0.flowid);
58 } else if (t->gtp_version == GTP_V1) {
59 mnl_attr_put_u32(nlh, GTPA_I_TEI, t->u.v1.i_tei);
60 mnl_attr_put_u32(nlh, GTPA_O_TEI, t->u.v1.o_tei);
61 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010062}
63
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010064int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010065{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010066 struct nlmsghdr *nlh;
67 char buf[MNL_SOCKET_BUFFER_SIZE];
68 uint32_t seq = time(NULL);
69
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010070 if (t->gtp_version > GTP_V1) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010071 fprintf(stderr, "wrong GTP version %u, use v0 or v1\n",
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010072 t->gtp_version);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010073 return -1;
74 }
75
76 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_EXCL | NLM_F_ACK, ++seq,
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020077 GTP_CMD_NEWPDP);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010078 gtp_build_payload(nlh, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010079
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +010080 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010081 perror("genl_socket_talk");
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +010082 return -1;
83 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010084
85 return 0;
86}
87EXPORT_SYMBOL(gtp_add_tunnel);
88
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010089int gtp_del_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010090{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010091 char buf[MNL_SOCKET_BUFFER_SIZE];
92 struct nlmsghdr *nlh;
93 uint32_t seq = time(NULL);
94
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010095 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_ACK, ++seq,
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020096 GTP_CMD_DELPDP);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010097 gtp_build_payload(nlh, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010098
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +010099 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100100 perror("genl_socket_talk");
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +0100101 return -1;
102 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100103
104 return 0;
105}
106EXPORT_SYMBOL(gtp_del_tunnel);
107
108struct gtp_pdp {
109 uint32_t version;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200110 union {
111 struct {
112 uint64_t tid;
113 } v0;
114 struct {
115 uint32_t i_tei;
116 uint32_t o_tei;
117 } v1;
118 } u;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100119 struct in_addr sgsn_addr;
120 struct in_addr ms_addr;
121};
122
123static int genl_gtp_validate_cb(const struct nlattr *attr, void *data)
124{
125 const struct nlattr **tb = data;
126 int type = mnl_attr_get_type(attr);
127
junpei yoshino345d6872016-08-25 13:06:53 +0900128 if (mnl_attr_type_valid(attr, GTPA_MAX) < 0)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100129 return MNL_CB_OK;
130
131 switch(type) {
132 case GTPA_TID:
133 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) {
134 perror("mnl_attr_validate");
135 return MNL_CB_ERROR;
136 }
137 break;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200138 case GTPA_O_TEI:
139 case GTPA_I_TEI:
Jonas Bonn6e9afbb2017-03-24 15:19:19 +0100140 case GTPA_PEER_ADDRESS:
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100141 case GTPA_MS_ADDRESS:
142 case GTPA_VERSION:
143 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
144 perror("mnl_attr_validate");
145 return MNL_CB_ERROR;
146 }
147 break;
148 default:
149 break;
150 }
151 tb[type] = attr;
152 return MNL_CB_OK;
153}
154
155static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
156{
157 struct nlattr *tb[GTPA_MAX + 1] = {};
Pablo Neira Ayusof69f8c72016-05-08 18:52:45 +0200158 char buf[INET_ADDRSTRLEN];
Pablo Neira Ayuso0829e0e2014-02-22 22:19:35 +0100159 struct gtp_pdp pdp = {};
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100160 struct genlmsghdr *genl;
161
162 mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb);
163 if (tb[GTPA_TID])
Andreas Schultz17c816f2016-04-11 16:10:03 +0200164 pdp.u.v0.tid = mnl_attr_get_u64(tb[GTPA_TID]);
165 if (tb[GTPA_I_TEI])
166 pdp.u.v1.i_tei = mnl_attr_get_u32(tb[GTPA_I_TEI]);
167 if (tb[GTPA_O_TEI])
168 pdp.u.v1.o_tei = mnl_attr_get_u32(tb[GTPA_O_TEI]);
Jonas Bonn6e9afbb2017-03-24 15:19:19 +0100169 if (tb[GTPA_PEER_ADDRESS]) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100170 pdp.sgsn_addr.s_addr =
Jonas Bonn6e9afbb2017-03-24 15:19:19 +0100171 mnl_attr_get_u32(tb[GTPA_PEER_ADDRESS]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100172 }
173 if (tb[GTPA_MS_ADDRESS]) {
174 pdp.ms_addr.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]);
175 }
176 if (tb[GTPA_VERSION]) {
177 pdp.version = mnl_attr_get_u32(tb[GTPA_VERSION]);
178 }
179
180 printf("version %u ", pdp.version);
Pablo Neira Ayusof69f8c72016-05-08 18:52:45 +0200181 if (pdp.version == GTP_V0) {
182 inet_ntop(AF_INET, &pdp.ms_addr, buf, sizeof(buf));
Andreas Schultz17c816f2016-04-11 16:10:03 +0200183 printf("tid %"PRIu64" ms_addr %s ",
Pablo Neira Ayusof69f8c72016-05-08 18:52:45 +0200184 pdp.u.v0.tid, buf);
185 } else if (pdp.version == GTP_V1) {
186 inet_ntop(AF_INET, &pdp.ms_addr, buf, sizeof(buf));
Andreas Schultz17c816f2016-04-11 16:10:03 +0200187 printf("tei %u/%u ms_addr %s ", pdp.u.v1.i_tei,
Pablo Neira Ayusof69f8c72016-05-08 18:52:45 +0200188 pdp.u.v1.o_tei, buf);
189 }
190 inet_ntop(AF_INET, &pdp.sgsn_addr, buf, sizeof(buf));
191 printf("sgsn_addr %s\n", buf);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100192
193 return MNL_CB_OK;
194}
195
196int gtp_list_tunnel(int genl_id, struct mnl_socket *nl)
197{
198 char buf[MNL_SOCKET_BUFFER_SIZE];
199 struct nlmsghdr *nlh;
200 uint32_t seq = time(NULL);
201
202 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_DUMP, 0,
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +0200203 GTP_CMD_GETPDP);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100204
205 if (genl_socket_talk(nl, nlh, seq, genl_gtp_attr_cb, NULL) < 0) {
206 perror("genl_socket_talk");
Neels Hofmeyr9fa76ec2022-01-25 04:30:24 +0100207 return -1;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100208 }
209
210 return 0;
211}
212EXPORT_SYMBOL(gtp_list_tunnel);