Harald Welte | 51b00a6 | 2014-04-03 09:37:38 -0400 | [diff] [blame] | 1 | /* 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 Welte | d2bb0bc | 2016-07-28 20:34:45 +0200 | [diff] [blame] | 9 | * 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 Welte | 51b00a6 | 2014-04-03 09:37:38 -0400 | [diff] [blame] | 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 |
Harald Welte | d2bb0bc | 2016-07-28 20:34:45 +0200 | [diff] [blame] | 16 | * GNU Lesser General Public License for more details. |
Harald Welte | 51b00a6 | 2014-04-03 09:37:38 -0400 | [diff] [blame] | 17 | * |
Harald Welte | d2bb0bc | 2016-07-28 20:34:45 +0200 | [diff] [blame] | 18 | * You should have received a copy of the GNU Lesser General Public License |
Harald Welte | 51b00a6 | 2014-04-03 09:37:38 -0400 | [diff] [blame] | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
| 22 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 23 | #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 Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 36 | #include <libgtpnl/gtp.h> |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 37 | #include <libgtpnl/gtpnl.h> |
| 38 | |
| 39 | #include <net/if.h> |
Pablo Neira Ayuso | b9f6ffe | 2016-05-08 18:27:55 +0200 | [diff] [blame] | 40 | #include <linux/gtp.h> |
| 41 | #include <linux/if_link.h> |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 42 | |
| 43 | #include "internal.h" |
| 44 | |
Pablo Neira Ayuso | b976ffa | 2014-03-20 13:56:55 +0100 | [diff] [blame] | 45 | static void gtp_build_payload(struct nlmsghdr *nlh, struct gtp_tunnel *t) |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 46 | { |
Pablo Neira Ayuso | 6f1c38f | 2024-01-31 13:40:35 +0100 | [diff] [blame^] | 47 | 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 Schultz | 4977330 | 2016-04-11 16:09:56 +0200 | [diff] [blame] | 52 | mnl_attr_put_u32(nlh, GTPA_NET_NS_FD, t->ifns); |
Pablo Neira Ayuso | 6f1c38f | 2024-01-31 13:40:35 +0100 | [diff] [blame^] | 53 | if (t->flags & GTP_TUN_IFIDX) |
| 54 | mnl_attr_put_u32(nlh, GTPA_LINK, t->ifidx); |
Oliver Smith | a295615 | 2023-10-19 14:11:19 +0200 | [diff] [blame] | 55 | |
Pablo Neira Ayuso | 6f1c38f | 2024-01-31 13:40:35 +0100 | [diff] [blame^] | 56 | 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 Smith | a295615 | 2023-10-19 14:11:19 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Pablo Neira Ayuso | 6f1c38f | 2024-01-31 13:40:35 +0100 | [diff] [blame^] | 70 | 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 Smith | a295615 | 2023-10-19 14:11:19 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Pablo Neira Ayuso | 6f1c38f | 2024-01-31 13:40:35 +0100 | [diff] [blame^] | 84 | 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 Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 96 | } |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 99 | int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t) |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 100 | { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 101 | struct nlmsghdr *nlh; |
| 102 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 103 | uint32_t seq = time(NULL); |
| 104 | |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 105 | if (t->gtp_version > GTP_V1) { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 106 | fprintf(stderr, "wrong GTP version %u, use v0 or v1\n", |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 107 | t->gtp_version); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_EXCL | NLM_F_ACK, ++seq, |
Pablo Neira Ayuso | b9f6ffe | 2016-05-08 18:27:55 +0200 | [diff] [blame] | 112 | GTP_CMD_NEWPDP); |
Pablo Neira Ayuso | b976ffa | 2014-03-20 13:56:55 +0100 | [diff] [blame] | 113 | gtp_build_payload(nlh, t); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 114 | |
Neels Hofmeyr | 9fa76ec | 2022-01-25 04:30:24 +0100 | [diff] [blame] | 115 | if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 116 | perror("genl_socket_talk"); |
Neels Hofmeyr | 9fa76ec | 2022-01-25 04:30:24 +0100 | [diff] [blame] | 117 | return -1; |
| 118 | } |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | EXPORT_SYMBOL(gtp_add_tunnel); |
| 123 | |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 124 | int gtp_del_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t) |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 125 | { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 126 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 127 | struct nlmsghdr *nlh; |
| 128 | uint32_t seq = time(NULL); |
| 129 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 130 | nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_ACK, ++seq, |
Pablo Neira Ayuso | b9f6ffe | 2016-05-08 18:27:55 +0200 | [diff] [blame] | 131 | GTP_CMD_DELPDP); |
Pablo Neira Ayuso | b976ffa | 2014-03-20 13:56:55 +0100 | [diff] [blame] | 132 | gtp_build_payload(nlh, t); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 133 | |
Neels Hofmeyr | 9fa76ec | 2022-01-25 04:30:24 +0100 | [diff] [blame] | 134 | if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 135 | perror("genl_socket_talk"); |
Neels Hofmeyr | 9fa76ec | 2022-01-25 04:30:24 +0100 | [diff] [blame] | 136 | return -1; |
| 137 | } |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | EXPORT_SYMBOL(gtp_del_tunnel); |
| 142 | |
| 143 | struct gtp_pdp { |
| 144 | uint32_t version; |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 145 | union { |
| 146 | struct { |
| 147 | uint64_t tid; |
| 148 | } v0; |
| 149 | struct { |
| 150 | uint32_t i_tei; |
| 151 | uint32_t o_tei; |
| 152 | } v1; |
| 153 | } u; |
Oliver Smith | 717db09 | 2023-10-18 15:16:12 +0200 | [diff] [blame] | 154 | struct gtp_addr sgsn_addr; |
| 155 | struct gtp_addr ms_addr; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | static int genl_gtp_validate_cb(const struct nlattr *attr, void *data) |
| 159 | { |
| 160 | const struct nlattr **tb = data; |
| 161 | int type = mnl_attr_get_type(attr); |
| 162 | |
junpei yoshino | 345d687 | 2016-08-25 13:06:53 +0900 | [diff] [blame] | 163 | if (mnl_attr_type_valid(attr, GTPA_MAX) < 0) |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 164 | return MNL_CB_OK; |
| 165 | |
| 166 | switch(type) { |
Oliver Smith | a295615 | 2023-10-19 14:11:19 +0200 | [diff] [blame] | 167 | case GTPA_FAMILY: |
| 168 | if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0) { |
| 169 | perror("mnl_attr_validate"); |
| 170 | return MNL_CB_ERROR; |
| 171 | } |
| 172 | break; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 173 | case GTPA_TID: |
| 174 | if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) { |
| 175 | perror("mnl_attr_validate"); |
| 176 | return MNL_CB_ERROR; |
| 177 | } |
| 178 | break; |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 179 | case GTPA_O_TEI: |
| 180 | case GTPA_I_TEI: |
Jonas Bonn | 6e9afbb | 2017-03-24 15:19:19 +0100 | [diff] [blame] | 181 | case GTPA_PEER_ADDRESS: |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 182 | case GTPA_MS_ADDRESS: |
| 183 | case GTPA_VERSION: |
| 184 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) { |
| 185 | perror("mnl_attr_validate"); |
| 186 | return MNL_CB_ERROR; |
| 187 | } |
| 188 | break; |
Oliver Smith | a295615 | 2023-10-19 14:11:19 +0200 | [diff] [blame] | 189 | case GTPA_PEER_ADDR6: |
| 190 | case GTPA_MS_ADDR6: |
| 191 | if (mnl_attr_validate2(attr, MNL_TYPE_BINARY, |
| 192 | sizeof(struct in6_addr)) < 0) { |
| 193 | perror("mnl_attr_validate"); |
| 194 | return MNL_CB_ERROR; |
| 195 | } |
| 196 | break; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 197 | default: |
| 198 | break; |
| 199 | } |
| 200 | tb[type] = attr; |
| 201 | return MNL_CB_OK; |
| 202 | } |
| 203 | |
| 204 | static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data) |
| 205 | { |
| 206 | struct nlattr *tb[GTPA_MAX + 1] = {}; |
Pablo Neira Ayuso | 99a5c32 | 2023-10-05 20:50:17 +0200 | [diff] [blame] | 207 | char buf[INET6_ADDRSTRLEN]; |
Pablo Neira Ayuso | 0829e0e | 2014-02-22 22:19:35 +0100 | [diff] [blame] | 208 | struct gtp_pdp pdp = {}; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 209 | struct genlmsghdr *genl; |
| 210 | |
| 211 | mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb); |
Oliver Smith | a295615 | 2023-10-19 14:11:19 +0200 | [diff] [blame] | 212 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 213 | if (tb[GTPA_TID]) |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 214 | pdp.u.v0.tid = mnl_attr_get_u64(tb[GTPA_TID]); |
| 215 | if (tb[GTPA_I_TEI]) |
| 216 | pdp.u.v1.i_tei = mnl_attr_get_u32(tb[GTPA_I_TEI]); |
| 217 | if (tb[GTPA_O_TEI]) |
| 218 | pdp.u.v1.o_tei = mnl_attr_get_u32(tb[GTPA_O_TEI]); |
Oliver Smith | a295615 | 2023-10-19 14:11:19 +0200 | [diff] [blame] | 219 | |
Jonas Bonn | 6e9afbb | 2017-03-24 15:19:19 +0100 | [diff] [blame] | 220 | if (tb[GTPA_PEER_ADDRESS]) { |
Oliver Smith | 717db09 | 2023-10-18 15:16:12 +0200 | [diff] [blame] | 221 | pdp.sgsn_addr.family = AF_INET; |
Oliver Smith | a295615 | 2023-10-19 14:11:19 +0200 | [diff] [blame] | 222 | pdp.sgsn_addr.ip4.s_addr = mnl_attr_get_u32(tb[GTPA_PEER_ADDRESS]); |
| 223 | } else if (tb[GTPA_PEER_ADDR6]) { |
| 224 | pdp.sgsn_addr.family = AF_INET6; |
| 225 | memcpy(&pdp.sgsn_addr.ip6, mnl_attr_get_payload(tb[GTPA_PEER_ADDR6]), |
| 226 | sizeof(struct in6_addr)); |
| 227 | } else { |
| 228 | fprintf(stderr, "sgsn_addr: no IPv4 nor IPv6 set\n"); |
| 229 | return MNL_CB_ERROR; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 230 | } |
Oliver Smith | a295615 | 2023-10-19 14:11:19 +0200 | [diff] [blame] | 231 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 232 | if (tb[GTPA_MS_ADDRESS]) { |
Oliver Smith | 717db09 | 2023-10-18 15:16:12 +0200 | [diff] [blame] | 233 | pdp.ms_addr.family = AF_INET; |
| 234 | pdp.ms_addr.ip4.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]); |
Oliver Smith | a295615 | 2023-10-19 14:11:19 +0200 | [diff] [blame] | 235 | } else if (tb[GTPA_MS_ADDR6]) { |
| 236 | pdp.ms_addr.family = AF_INET6; |
| 237 | memcpy(&pdp.ms_addr.ip6, mnl_attr_get_payload(tb[GTPA_MS_ADDR6]), sizeof(struct in6_addr)); |
| 238 | } else { |
| 239 | fprintf(stderr, "ms_addr: no IPv4 nor IPv6 set\n"); |
| 240 | return MNL_CB_ERROR; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Oliver Smith | a295615 | 2023-10-19 14:11:19 +0200 | [diff] [blame] | 243 | if (tb[GTPA_FAMILY] && mnl_attr_get_u32(tb[GTPA_FAMILY]) != pdp.ms_addr.family) { |
| 244 | fprintf(stderr, "ms_addr family does not match GTPA_FAMILY\n"); |
| 245 | return MNL_CB_ERROR; |
Pablo Neira Ayuso | f69f8c7 | 2016-05-08 18:52:45 +0200 | [diff] [blame] | 246 | } |
Oliver Smith | a295615 | 2023-10-19 14:11:19 +0200 | [diff] [blame] | 247 | |
| 248 | if (tb[GTPA_VERSION]) |
| 249 | pdp.version = mnl_attr_get_u32(tb[GTPA_VERSION]); |
| 250 | |
| 251 | printf("version %u ", pdp.version); |
| 252 | |
| 253 | if (pdp.version == GTP_V0) |
| 254 | printf("tid %"PRIu64" ", pdp.u.v0.tid); |
| 255 | else if (pdp.version == GTP_V1) |
| 256 | printf("tei %u/%u ", pdp.u.v1.i_tei, pdp.u.v1.o_tei); |
| 257 | |
| 258 | inet_ntop(pdp.ms_addr.family, &pdp.ms_addr.ip4, buf, sizeof(buf)); |
| 259 | printf("ms_addr %s ", buf); |
| 260 | |
| 261 | inet_ntop(pdp.sgsn_addr.family, &pdp.sgsn_addr.ip4, buf, sizeof(buf)); |
Pablo Neira Ayuso | f69f8c7 | 2016-05-08 18:52:45 +0200 | [diff] [blame] | 262 | printf("sgsn_addr %s\n", buf); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 263 | |
| 264 | return MNL_CB_OK; |
| 265 | } |
| 266 | |
| 267 | int gtp_list_tunnel(int genl_id, struct mnl_socket *nl) |
| 268 | { |
| 269 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 270 | struct nlmsghdr *nlh; |
| 271 | uint32_t seq = time(NULL); |
| 272 | |
| 273 | nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_DUMP, 0, |
Pablo Neira Ayuso | b9f6ffe | 2016-05-08 18:27:55 +0200 | [diff] [blame] | 274 | GTP_CMD_GETPDP); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 275 | |
| 276 | if (genl_socket_talk(nl, nlh, seq, genl_gtp_attr_cb, NULL) < 0) { |
| 277 | perror("genl_socket_talk"); |
Neels Hofmeyr | 9fa76ec | 2022-01-25 04:30:24 +0100 | [diff] [blame] | 278 | return -1; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | EXPORT_SYMBOL(gtp_list_tunnel); |