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 | b976ffa | 2014-03-20 13:56:55 +0100 | [diff] [blame] | 47 | mnl_attr_put_u32(nlh, GTPA_VERSION, t->gtp_version); |
Andreas Schultz | 4977330 | 2016-04-11 16:09:56 +0200 | [diff] [blame] | 48 | if (t->ifns >= 0) |
| 49 | mnl_attr_put_u32(nlh, GTPA_NET_NS_FD, t->ifns); |
Pablo Neira Ayuso | b976ffa | 2014-03-20 13:56:55 +0100 | [diff] [blame] | 50 | mnl_attr_put_u32(nlh, GTPA_LINK, t->ifidx); |
Harald Welte | 03cb4c2 | 2017-11-12 22:01:13 +0900 | [diff] [blame] | 51 | 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 Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 55 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 64 | 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] | 65 | { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 66 | struct nlmsghdr *nlh; |
| 67 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 68 | uint32_t seq = time(NULL); |
| 69 | |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 70 | if (t->gtp_version > GTP_V1) { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 71 | fprintf(stderr, "wrong GTP version %u, use v0 or v1\n", |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 72 | t->gtp_version); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | 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] | 77 | GTP_CMD_NEWPDP); |
Pablo Neira Ayuso | b976ffa | 2014-03-20 13:56:55 +0100 | [diff] [blame] | 78 | gtp_build_payload(nlh, t); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 79 | |
| 80 | if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) |
| 81 | perror("genl_socket_talk"); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | EXPORT_SYMBOL(gtp_add_tunnel); |
| 86 | |
Pablo Neira Ayuso | 1853295 | 2014-02-22 22:09:59 +0100 | [diff] [blame] | 87 | 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] | 88 | { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 89 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 90 | struct nlmsghdr *nlh; |
| 91 | uint32_t seq = time(NULL); |
| 92 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 93 | 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] | 94 | GTP_CMD_DELPDP); |
Pablo Neira Ayuso | b976ffa | 2014-03-20 13:56:55 +0100 | [diff] [blame] | 95 | gtp_build_payload(nlh, t); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 96 | |
| 97 | if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) |
| 98 | perror("genl_socket_talk"); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | EXPORT_SYMBOL(gtp_del_tunnel); |
| 103 | |
| 104 | struct gtp_pdp { |
| 105 | uint32_t version; |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 106 | union { |
| 107 | struct { |
| 108 | uint64_t tid; |
| 109 | } v0; |
| 110 | struct { |
| 111 | uint32_t i_tei; |
| 112 | uint32_t o_tei; |
| 113 | } v1; |
| 114 | } u; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 115 | struct in_addr sgsn_addr; |
| 116 | struct in_addr ms_addr; |
| 117 | }; |
| 118 | |
| 119 | static int genl_gtp_validate_cb(const struct nlattr *attr, void *data) |
| 120 | { |
| 121 | const struct nlattr **tb = data; |
| 122 | int type = mnl_attr_get_type(attr); |
| 123 | |
junpei yoshino | 345d687 | 2016-08-25 13:06:53 +0900 | [diff] [blame] | 124 | if (mnl_attr_type_valid(attr, GTPA_MAX) < 0) |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 125 | return MNL_CB_OK; |
| 126 | |
| 127 | switch(type) { |
| 128 | case GTPA_TID: |
| 129 | if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) { |
| 130 | perror("mnl_attr_validate"); |
| 131 | return MNL_CB_ERROR; |
| 132 | } |
| 133 | break; |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 134 | case GTPA_O_TEI: |
| 135 | case GTPA_I_TEI: |
Jonas Bonn | 6e9afbb | 2017-03-24 15:19:19 +0100 | [diff] [blame] | 136 | case GTPA_PEER_ADDRESS: |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 137 | case GTPA_MS_ADDRESS: |
| 138 | case GTPA_VERSION: |
| 139 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) { |
| 140 | perror("mnl_attr_validate"); |
| 141 | return MNL_CB_ERROR; |
| 142 | } |
| 143 | break; |
| 144 | default: |
| 145 | break; |
| 146 | } |
| 147 | tb[type] = attr; |
| 148 | return MNL_CB_OK; |
| 149 | } |
| 150 | |
| 151 | static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data) |
| 152 | { |
| 153 | struct nlattr *tb[GTPA_MAX + 1] = {}; |
Pablo Neira Ayuso | f69f8c7 | 2016-05-08 18:52:45 +0200 | [diff] [blame] | 154 | char buf[INET_ADDRSTRLEN]; |
Pablo Neira Ayuso | 0829e0e | 2014-02-22 22:19:35 +0100 | [diff] [blame] | 155 | struct gtp_pdp pdp = {}; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 156 | struct genlmsghdr *genl; |
| 157 | |
| 158 | mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb); |
| 159 | if (tb[GTPA_TID]) |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 160 | pdp.u.v0.tid = mnl_attr_get_u64(tb[GTPA_TID]); |
| 161 | if (tb[GTPA_I_TEI]) |
| 162 | pdp.u.v1.i_tei = mnl_attr_get_u32(tb[GTPA_I_TEI]); |
| 163 | if (tb[GTPA_O_TEI]) |
| 164 | pdp.u.v1.o_tei = mnl_attr_get_u32(tb[GTPA_O_TEI]); |
Jonas Bonn | 6e9afbb | 2017-03-24 15:19:19 +0100 | [diff] [blame] | 165 | if (tb[GTPA_PEER_ADDRESS]) { |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 166 | pdp.sgsn_addr.s_addr = |
Jonas Bonn | 6e9afbb | 2017-03-24 15:19:19 +0100 | [diff] [blame] | 167 | mnl_attr_get_u32(tb[GTPA_PEER_ADDRESS]); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 168 | } |
| 169 | if (tb[GTPA_MS_ADDRESS]) { |
| 170 | pdp.ms_addr.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]); |
| 171 | } |
| 172 | if (tb[GTPA_VERSION]) { |
| 173 | pdp.version = mnl_attr_get_u32(tb[GTPA_VERSION]); |
| 174 | } |
| 175 | |
| 176 | printf("version %u ", pdp.version); |
Pablo Neira Ayuso | f69f8c7 | 2016-05-08 18:52:45 +0200 | [diff] [blame] | 177 | if (pdp.version == GTP_V0) { |
| 178 | inet_ntop(AF_INET, &pdp.ms_addr, buf, sizeof(buf)); |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 179 | printf("tid %"PRIu64" ms_addr %s ", |
Pablo Neira Ayuso | f69f8c7 | 2016-05-08 18:52:45 +0200 | [diff] [blame] | 180 | pdp.u.v0.tid, buf); |
| 181 | } else if (pdp.version == GTP_V1) { |
| 182 | inet_ntop(AF_INET, &pdp.ms_addr, buf, sizeof(buf)); |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 183 | printf("tei %u/%u ms_addr %s ", pdp.u.v1.i_tei, |
Pablo Neira Ayuso | f69f8c7 | 2016-05-08 18:52:45 +0200 | [diff] [blame] | 184 | pdp.u.v1.o_tei, buf); |
| 185 | } |
| 186 | inet_ntop(AF_INET, &pdp.sgsn_addr, buf, sizeof(buf)); |
| 187 | printf("sgsn_addr %s\n", buf); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 188 | |
| 189 | return MNL_CB_OK; |
| 190 | } |
| 191 | |
| 192 | int gtp_list_tunnel(int genl_id, struct mnl_socket *nl) |
| 193 | { |
| 194 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 195 | struct nlmsghdr *nlh; |
| 196 | uint32_t seq = time(NULL); |
| 197 | |
| 198 | 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] | 199 | GTP_CMD_GETPDP); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 200 | |
| 201 | if (genl_socket_talk(nl, nlh, seq, genl_gtp_attr_cb, NULL) < 0) { |
| 202 | perror("genl_socket_talk"); |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | EXPORT_SYMBOL(gtp_list_tunnel); |