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