Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file gprs_ns_frgre.c |
| 2 | * NS-over-FR-over-GRE implementation. |
| 3 | * GPRS Networks Service (NS) messages on the Gb interface, |
| 4 | * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05). */ |
| 5 | /* |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 6 | * (C) 2009-2010,2014,2017 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 7 | * |
| 8 | * All Rights Reserved |
| 9 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
| 11 | * |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 20 | * GNU General Public License for more details. |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 21 | * |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 22 | * You should have received a copy of the GNU General Public License |
Harald Welte | e4cbb3f | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 23 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include <errno.h> |
| 28 | #include <string.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #include <sys/socket.h> |
| 32 | #include <netinet/in.h> |
Harald Welte | 6c4136e | 2010-05-19 15:53:22 +0200 | [diff] [blame] | 33 | #include <netinet/ip.h> |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 34 | #include <arpa/inet.h> |
| 35 | |
Pablo Neira Ayuso | ff66323 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 36 | #include <osmocom/core/select.h> |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 37 | #include <osmocom/core/byteswap.h> |
Pablo Neira Ayuso | ff66323 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 38 | #include <osmocom/core/msgb.h> |
| 39 | #include <osmocom/core/talloc.h> |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 40 | #include <osmocom/core/socket.h> |
| 41 | #include <osmocom/gprs/gprs_ns.h> |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 42 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 43 | #include "common_vty.h" |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 44 | |
| 45 | #define GRE_PTYPE_FR 0x6559 |
Harald Welte | 57a9cf2 | 2010-05-28 16:06:53 +0200 | [diff] [blame] | 46 | #define GRE_PTYPE_IPv4 0x0800 |
| 47 | #define GRE_PTYPE_KAR 0x0000 /* keepalive response */ |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 48 | |
Evgeny Zverev | 06ddf8b | 2013-01-12 00:02:40 +0400 | [diff] [blame] | 49 | #ifndef IPPROTO_GRE |
| 50 | # define IPPROTO_GRE 47 |
| 51 | #endif |
| 52 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 53 | struct gre_hdr { |
| 54 | uint16_t flags; |
| 55 | uint16_t ptype; |
| 56 | } __attribute__ ((packed)); |
| 57 | |
Evgeny Zverev | 06ddf8b | 2013-01-12 00:02:40 +0400 | [diff] [blame] | 58 | #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__CYGWIN__) |
Holger Hans Peter Freyther | 3c16de2 | 2012-07-12 14:13:41 +0200 | [diff] [blame] | 59 | /** |
| 60 | * On BSD the IPv4 struct is called struct ip and instead of iXX |
| 61 | * the members are called ip_XX. One could change this code to use |
| 62 | * struct ip but that would require to define _BSD_SOURCE and that |
| 63 | * might have other complications. Instead make sure struct iphdr |
| 64 | * is present on FreeBSD. The below is taken from GLIBC. |
| 65 | * |
| 66 | * The GNU C Library is free software; you can redistribute it and/or |
| 67 | * modify it under the terms of the GNU Lesser General Public |
| 68 | * License as published by the Free Software Foundation; either |
| 69 | * version 2.1 of the License, or (at your option) any later version. |
| 70 | */ |
| 71 | struct iphdr |
| 72 | { |
| 73 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 74 | unsigned int ihl:4; |
| 75 | unsigned int version:4; |
| 76 | #elif BYTE_ORDER == BIG_ENDIAN |
| 77 | unsigned int version:4; |
| 78 | unsigned int ihl:4; |
| 79 | #endif |
| 80 | u_int8_t tos; |
| 81 | u_int16_t tot_len; |
| 82 | u_int16_t id; |
| 83 | u_int16_t frag_off; |
| 84 | u_int8_t ttl; |
| 85 | u_int8_t protocol; |
| 86 | u_int16_t check; |
| 87 | u_int32_t saddr; |
| 88 | u_int32_t daddr; |
| 89 | /*The options start here. */ |
| 90 | }; |
| 91 | #endif |
| 92 | |
| 93 | |
Harald Welte | 57a9cf2 | 2010-05-28 16:06:53 +0200 | [diff] [blame] | 94 | /* IPv4 messages inside the GRE tunnel might be GRE keepalives */ |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 95 | static int handle_rx_gre_ipv4(struct osmo_fd *bfd, struct msgb *msg, |
Harald Welte | 57a9cf2 | 2010-05-28 16:06:53 +0200 | [diff] [blame] | 96 | struct iphdr *iph, struct gre_hdr *greh) |
| 97 | { |
| 98 | struct gprs_ns_inst *nsi = bfd->data; |
| 99 | int gre_payload_len; |
| 100 | struct iphdr *inner_iph; |
| 101 | struct gre_hdr *inner_greh; |
| 102 | struct sockaddr_in daddr; |
| 103 | struct in_addr ia; |
| 104 | |
| 105 | gre_payload_len = msg->len - (iph->ihl*4 + sizeof(*greh)); |
| 106 | |
Harald Welte | 9681ce3 | 2010-05-31 11:02:57 +0200 | [diff] [blame] | 107 | inner_iph = (struct iphdr *) ((uint8_t *)greh + sizeof(*greh)); |
Harald Welte | 57a9cf2 | 2010-05-28 16:06:53 +0200 | [diff] [blame] | 108 | |
| 109 | if (gre_payload_len < inner_iph->ihl*4 + sizeof(*inner_greh)) { |
| 110 | LOGP(DNS, LOGL_ERROR, "GRE keepalive too short\n"); |
| 111 | return -EIO; |
| 112 | } |
| 113 | |
| 114 | if (inner_iph->saddr != iph->daddr || |
| 115 | inner_iph->daddr != iph->saddr) { |
| 116 | LOGP(DNS, LOGL_ERROR, |
| 117 | "GRE keepalive with wrong tunnel addresses\n"); |
| 118 | return -EIO; |
| 119 | } |
| 120 | |
| 121 | if (inner_iph->protocol != IPPROTO_GRE) { |
| 122 | LOGP(DNS, LOGL_ERROR, "GRE keepalive with wrong protocol\n"); |
| 123 | return -EIO; |
| 124 | } |
| 125 | |
Harald Welte | 9681ce3 | 2010-05-31 11:02:57 +0200 | [diff] [blame] | 126 | inner_greh = (struct gre_hdr *) ((uint8_t *)inner_iph + iph->ihl*4); |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 127 | if (inner_greh->ptype != osmo_htons(GRE_PTYPE_KAR)) { |
Harald Welte | 57a9cf2 | 2010-05-28 16:06:53 +0200 | [diff] [blame] | 128 | LOGP(DNS, LOGL_ERROR, "GRE keepalive inner GRE type != 0\n"); |
| 129 | return -EIO; |
| 130 | } |
| 131 | |
| 132 | /* Actually send the response back */ |
| 133 | |
| 134 | daddr.sin_family = AF_INET; |
| 135 | daddr.sin_addr.s_addr = inner_iph->daddr; |
| 136 | daddr.sin_port = IPPROTO_GRE; |
| 137 | |
| 138 | ia.s_addr = iph->saddr; |
| 139 | LOGP(DNS, LOGL_DEBUG, "GRE keepalive from %s, responding\n", |
| 140 | inet_ntoa(ia)); |
| 141 | |
| 142 | return sendto(nsi->frgre.fd.fd, inner_greh, |
| 143 | gre_payload_len - inner_iph->ihl*4, 0, |
| 144 | (struct sockaddr *)&daddr, sizeof(daddr)); |
| 145 | } |
| 146 | |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 147 | static struct msgb *read_nsfrgre_msg(struct osmo_fd *bfd, int *error, |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 148 | struct sockaddr_in *saddr) |
| 149 | { |
| 150 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Gb/NS/FR/GRE Rx"); |
| 151 | int ret = 0; |
| 152 | socklen_t saddr_len = sizeof(*saddr); |
Harald Welte | 6c4136e | 2010-05-19 15:53:22 +0200 | [diff] [blame] | 153 | struct iphdr *iph; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 154 | struct gre_hdr *greh; |
| 155 | uint8_t *frh; |
Harald Welte | 869aaa4 | 2010-05-19 16:48:12 +0200 | [diff] [blame] | 156 | uint16_t dlci; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 157 | |
| 158 | if (!msg) { |
| 159 | *error = -ENOMEM; |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, |
| 164 | (struct sockaddr *)saddr, &saddr_len); |
| 165 | if (ret < 0) { |
| 166 | LOGP(DNS, LOGL_ERROR, "recv error %s during NS-FR-GRE recv\n", |
| 167 | strerror(errno)); |
| 168 | *error = ret; |
Harald Welte | b11226d | 2010-05-19 15:46:49 +0200 | [diff] [blame] | 169 | goto out_err; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 170 | } else if (ret == 0) { |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 171 | *error = ret; |
Harald Welte | b11226d | 2010-05-19 15:46:49 +0200 | [diff] [blame] | 172 | goto out_err; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | msgb_put(msg, ret); |
| 176 | |
Harald Welte | 6c4136e | 2010-05-19 15:53:22 +0200 | [diff] [blame] | 177 | if (msg->len < sizeof(*iph) + sizeof(*greh) + 2) { |
| 178 | LOGP(DNS, LOGL_ERROR, "Short IP packet: %u bytes\n", msg->len); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 179 | *error = -EIO; |
| 180 | goto out_err; |
| 181 | } |
| 182 | |
Harald Welte | 6c4136e | 2010-05-19 15:53:22 +0200 | [diff] [blame] | 183 | iph = (struct iphdr *) msg->data; |
| 184 | if (msg->len < (iph->ihl*4 + sizeof(*greh) + 2)) { |
| 185 | LOGP(DNS, LOGL_ERROR, "Short IP packet: %u bytes\n", msg->len); |
| 186 | *error = -EIO; |
| 187 | goto out_err; |
| 188 | } |
| 189 | |
| 190 | greh = (struct gre_hdr *) (msg->data + iph->ihl*4); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 191 | if (greh->flags) { |
| 192 | LOGP(DNS, LOGL_NOTICE, "Unknown GRE flags 0x%04x\n", |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 193 | osmo_ntohs(greh->flags)); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 194 | } |
Harald Welte | 57a9cf2 | 2010-05-28 16:06:53 +0200 | [diff] [blame] | 195 | |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 196 | switch (osmo_ntohs(greh->ptype)) { |
Harald Welte | 57a9cf2 | 2010-05-28 16:06:53 +0200 | [diff] [blame] | 197 | case GRE_PTYPE_IPv4: |
| 198 | /* IPv4 messages might be GRE keepalives */ |
| 199 | *error = handle_rx_gre_ipv4(bfd, msg, iph, greh); |
| 200 | goto out_err; |
| 201 | break; |
| 202 | case GRE_PTYPE_FR: |
| 203 | /* continue as usual */ |
| 204 | break; |
| 205 | default: |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 206 | LOGP(DNS, LOGL_NOTICE, "Unknown GRE protocol 0x%04x != FR\n", |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 207 | osmo_ntohs(greh->ptype)); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 208 | *error = -EIO; |
| 209 | goto out_err; |
Harald Welte | 57a9cf2 | 2010-05-28 16:06:53 +0200 | [diff] [blame] | 210 | break; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | if (msg->len < sizeof(*greh) + 2) { |
| 214 | LOGP(DNS, LOGL_ERROR, "Short FR header: %u bytes\n", msg->len); |
| 215 | *error = -EIO; |
| 216 | goto out_err; |
| 217 | } |
| 218 | |
Harald Welte | 6c4136e | 2010-05-19 15:53:22 +0200 | [diff] [blame] | 219 | frh = (uint8_t *)greh + sizeof(*greh); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 220 | if (frh[0] & 0x01) { |
| 221 | LOGP(DNS, LOGL_NOTICE, "Unsupported single-byte FR address\n"); |
| 222 | *error = -EIO; |
| 223 | goto out_err; |
| 224 | } |
Harald Welte | 869aaa4 | 2010-05-19 16:48:12 +0200 | [diff] [blame] | 225 | dlci = ((frh[0] & 0xfc) << 2); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 226 | if ((frh[1] & 0x0f) != 0x01) { |
| 227 | LOGP(DNS, LOGL_NOTICE, "Unknown second FR octet 0x%02x\n", |
| 228 | frh[1]); |
| 229 | *error = -EIO; |
| 230 | goto out_err; |
| 231 | } |
Harald Welte | 869aaa4 | 2010-05-19 16:48:12 +0200 | [diff] [blame] | 232 | dlci |= (frh[1] >> 4); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 233 | |
Harald Welte | 6c4136e | 2010-05-19 15:53:22 +0200 | [diff] [blame] | 234 | msg->l2h = frh+2; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 235 | |
| 236 | /* Store DLCI in NETWORK BYTEORDER in sockaddr port member */ |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 237 | saddr->sin_port = osmo_htons(dlci); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 238 | |
| 239 | return msg; |
| 240 | |
| 241 | out_err: |
| 242 | msgb_free(msg); |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg, |
| 247 | struct sockaddr_in *saddr, enum gprs_ns_ll ll); |
| 248 | |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 249 | static int handle_nsfrgre_read(struct osmo_fd *bfd) |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 250 | { |
Harald Welte | f15497c | 2010-05-28 16:12:57 +0200 | [diff] [blame] | 251 | int rc; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 252 | struct sockaddr_in saddr; |
| 253 | struct gprs_ns_inst *nsi = bfd->data; |
Harald Welte | f15497c | 2010-05-28 16:12:57 +0200 | [diff] [blame] | 254 | struct msgb *msg; |
| 255 | uint16_t dlci; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 256 | |
Harald Welte | f15497c | 2010-05-28 16:12:57 +0200 | [diff] [blame] | 257 | msg = read_nsfrgre_msg(bfd, &rc, &saddr); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 258 | if (!msg) |
Harald Welte | f15497c | 2010-05-28 16:12:57 +0200 | [diff] [blame] | 259 | return rc; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 260 | |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 261 | dlci = osmo_ntohs(saddr.sin_port); |
Harald Welte | f15497c | 2010-05-28 16:12:57 +0200 | [diff] [blame] | 262 | if (dlci == 0 || dlci == 1023) { |
| 263 | LOGP(DNS, LOGL_INFO, "Received FR on LMI DLCI %u - ignoring\n", |
| 264 | dlci); |
| 265 | rc = 0; |
| 266 | goto out; |
| 267 | } |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 268 | |
Harald Welte | f15497c | 2010-05-28 16:12:57 +0200 | [diff] [blame] | 269 | rc = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_FR_GRE); |
| 270 | out: |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 271 | msgb_free(msg); |
| 272 | |
Harald Welte | f15497c | 2010-05-28 16:12:57 +0200 | [diff] [blame] | 273 | return rc; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 276 | static int handle_nsfrgre_write(struct osmo_fd *bfd) |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 277 | { |
| 278 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 279 | return -EIO; |
| 280 | } |
| 281 | |
| 282 | int gprs_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg) |
| 283 | { |
| 284 | int rc; |
| 285 | struct gprs_ns_inst *nsi = nsvc->nsi; |
| 286 | struct sockaddr_in daddr; |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 287 | uint16_t dlci = osmo_ntohs(nsvc->frgre.bts_addr.sin_port); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 288 | uint8_t *frh; |
| 289 | struct gre_hdr *greh; |
| 290 | |
| 291 | /* Build socket address for the packet destionation */ |
Harald Welte | ac914b8 | 2010-05-19 15:37:34 +0200 | [diff] [blame] | 292 | daddr.sin_family = AF_INET; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 293 | daddr.sin_addr = nsvc->frgre.bts_addr.sin_addr; |
| 294 | daddr.sin_port = IPPROTO_GRE; |
| 295 | |
| 296 | /* Prepend the FR header */ |
Harald Welte | ac914b8 | 2010-05-19 15:37:34 +0200 | [diff] [blame] | 297 | frh = msgb_push(msg, 2); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 298 | frh[0] = (dlci >> 2) & 0xfc; |
Harald Welte | ac914b8 | 2010-05-19 15:37:34 +0200 | [diff] [blame] | 299 | frh[1] = ((dlci & 0xf)<<4) | 0x01; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 300 | |
| 301 | /* Prepend the GRE header */ |
| 302 | greh = (struct gre_hdr *) msgb_push(msg, sizeof(*greh)); |
| 303 | greh->flags = 0; |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 304 | greh->ptype = osmo_htons(GRE_PTYPE_FR); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 305 | |
| 306 | rc = sendto(nsi->frgre.fd.fd, msg->data, msg->len, 0, |
| 307 | (struct sockaddr *)&daddr, sizeof(daddr)); |
| 308 | |
Holger Hans Peter Freyther | 5274646 | 2012-03-01 20:30:32 +0100 | [diff] [blame] | 309 | msgb_free(msg); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 310 | |
| 311 | return rc; |
| 312 | } |
| 313 | |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 314 | static int nsfrgre_fd_cb(struct osmo_fd *bfd, unsigned int what) |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 315 | { |
| 316 | int rc = 0; |
| 317 | |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 318 | if (what & OSMO_FD_READ) |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 319 | rc = handle_nsfrgre_read(bfd); |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 320 | if (what & OSMO_FD_WRITE) |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 321 | rc = handle_nsfrgre_write(bfd); |
| 322 | |
| 323 | return rc; |
| 324 | } |
| 325 | |
Harald Welte | 7fb0523 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 326 | int gprs_ns_frgre_listen(struct gprs_ns_inst *nsi) |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 327 | { |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 328 | struct in_addr in; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 329 | int rc; |
| 330 | |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 331 | in.s_addr = osmo_htonl(nsi->frgre.local_ip); |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 332 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 333 | /* Make sure we close any existing socket before changing it */ |
| 334 | if (nsi->frgre.fd.fd) |
| 335 | close(nsi->frgre.fd.fd); |
| 336 | |
Harald Welte | 7fb0523 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 337 | if (!nsi->frgre.enabled) |
| 338 | return 0; |
| 339 | |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 340 | nsi->frgre.fd.cb = nsfrgre_fd_cb; |
| 341 | nsi->frgre.fd.data = nsi; |
| 342 | rc = osmo_sock_init_ofd(&nsi->frgre.fd, AF_INET, SOCK_RAW, |
| 343 | IPPROTO_GRE, inet_ntoa(in), 0, |
| 344 | OSMO_SOCK_F_BIND); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 345 | if (rc < 0) { |
| 346 | LOGP(DNS, LOGL_ERROR, "Error creating GRE socket (%s)\n", |
| 347 | strerror(errno)); |
| 348 | return rc; |
| 349 | } |
| 350 | nsi->frgre.fd.data = nsi; |
| 351 | |
| 352 | return rc; |
| 353 | } |