blob: bffa7a5e44a7162405182c90f883cb60a250aee2 [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);
52 mnl_attr_put_u64(nlh, GTPA_TID, t->tid);
53 mnl_attr_put_u16(nlh, GTPA_FLOWID, t->flowid);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010054}
55
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010056int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010057{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010058 struct nlmsghdr *nlh;
59 char buf[MNL_SOCKET_BUFFER_SIZE];
60 uint32_t seq = time(NULL);
61
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010062 if (t->gtp_version > GTP_V1) {
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010063 fprintf(stderr, "wrong GTP version %u, use v0 or v1\n",
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010064 t->gtp_version);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010065 return -1;
66 }
67
68 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_EXCL | NLM_F_ACK, ++seq,
69 GTP_CMD_TUNNEL_NEW);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010070 gtp_build_payload(nlh, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010071
72 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0)
73 perror("genl_socket_talk");
74
75 return 0;
76}
77EXPORT_SYMBOL(gtp_add_tunnel);
78
Pablo Neira Ayuso18532952014-02-22 22:09:59 +010079int gtp_del_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010080{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010081 char buf[MNL_SOCKET_BUFFER_SIZE];
82 struct nlmsghdr *nlh;
83 uint32_t seq = time(NULL);
84
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010085 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_ACK, ++seq,
86 GTP_CMD_TUNNEL_DELETE);
Pablo Neira Ayusob976ffa2014-03-20 13:56:55 +010087 gtp_build_payload(nlh, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010088
89 if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0)
90 perror("genl_socket_talk");
91
92 return 0;
93}
94EXPORT_SYMBOL(gtp_del_tunnel);
95
96struct gtp_pdp {
97 uint32_t version;
98 uint64_t tid;
99 struct in_addr sgsn_addr;
100 struct in_addr ms_addr;
101};
102
103static int genl_gtp_validate_cb(const struct nlattr *attr, void *data)
104{
105 const struct nlattr **tb = data;
106 int type = mnl_attr_get_type(attr);
107
108 if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0)
109 return MNL_CB_OK;
110
111 switch(type) {
112 case GTPA_TID:
113 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) {
114 perror("mnl_attr_validate");
115 return MNL_CB_ERROR;
116 }
117 break;
118 case GTPA_SGSN_ADDRESS:
119 case GTPA_MS_ADDRESS:
120 case GTPA_VERSION:
121 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
122 perror("mnl_attr_validate");
123 return MNL_CB_ERROR;
124 }
125 break;
126 default:
127 break;
128 }
129 tb[type] = attr;
130 return MNL_CB_OK;
131}
132
133static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
134{
135 struct nlattr *tb[GTPA_MAX + 1] = {};
Pablo Neira Ayuso0829e0e2014-02-22 22:19:35 +0100136 struct gtp_pdp pdp = {};
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100137 struct genlmsghdr *genl;
138
139 mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb);
140 if (tb[GTPA_TID])
141 pdp.tid = mnl_attr_get_u64(tb[GTPA_TID]);
142 if (tb[GTPA_SGSN_ADDRESS]) {
143 pdp.sgsn_addr.s_addr =
144 mnl_attr_get_u32(tb[GTPA_SGSN_ADDRESS]);
145 }
146 if (tb[GTPA_MS_ADDRESS]) {
147 pdp.ms_addr.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]);
148 }
149 if (tb[GTPA_VERSION]) {
150 pdp.version = mnl_attr_get_u32(tb[GTPA_VERSION]);
151 }
152
153 printf("version %u ", pdp.version);
154 printf("tid %"PRIu64" ms_addr %s ", pdp.tid, inet_ntoa(pdp.sgsn_addr));
155 printf("sgsn_addr %s\n", inet_ntoa(pdp.ms_addr));
156
157 return MNL_CB_OK;
158}
159
160int gtp_list_tunnel(int genl_id, struct mnl_socket *nl)
161{
162 char buf[MNL_SOCKET_BUFFER_SIZE];
163 struct nlmsghdr *nlh;
164 uint32_t seq = time(NULL);
165
166 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_DUMP, 0,
167 GTP_CMD_TUNNEL_GET);
168
169 if (genl_socket_talk(nl, nlh, seq, genl_gtp_attr_cb, NULL) < 0) {
170 perror("genl_socket_talk");
171 return 0;
172 }
173
174 return 0;
175}
176EXPORT_SYMBOL(gtp_list_tunnel);