Pau Espin Pedrol | 7c4de07 | 2017-12-14 13:59:02 +0100 | [diff] [blame] | 1 | /* |
Harald Welte | 632e843 | 2017-09-05 18:12:14 +0200 | [diff] [blame] | 2 | * OsmoGGSN - Gateway GPRS Support Node |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 3 | * Copyright (C) 2002, 2003, 2004 Mondru AB. |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 4 | * Copyright (C) 2017 Harald Welte <laforge@gnumonks.org> |
Pau Espin Pedrol | 7c4de07 | 2017-12-14 13:59:02 +0100 | [diff] [blame] | 5 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 6 | * The contents of this file may be used under the terms of the GNU |
| 7 | * General Public License Version 2, provided that the above copyright |
| 8 | * notice and this permission notice is included in all copies or |
| 9 | * substantial portions of the software. |
Pau Espin Pedrol | 7c4de07 | 2017-12-14 13:59:02 +0100 | [diff] [blame] | 10 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * sgsnemu.c |
| 15 | * |
| 16 | */ |
| 17 | |
Pau Espin Pedrol | ff2ebee | 2020-04-18 23:47:06 +0200 | [diff] [blame] | 18 | #include "config.h" |
| 19 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 20 | #ifdef __linux__ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 21 | #define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 22 | #endif |
| 23 | |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 24 | #include <osmocom/core/application.h> |
Pau Espin Pedrol | 042a445 | 2018-04-17 14:31:42 +0200 | [diff] [blame] | 25 | #include <osmocom/core/msgb.h> |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 26 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 27 | #include <ctype.h> |
| 28 | #include <netdb.h> |
| 29 | #include <signal.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/socket.h> |
| 35 | #include <netinet/in.h> |
Harald Welte | 63ebccd | 2017-08-02 21:10:09 +0200 | [diff] [blame] | 36 | #include <netinet/ip.h> |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 37 | #include <netinet/ip6.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 38 | #include <arpa/inet.h> |
| 39 | #include <sys/wait.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <unistd.h> |
| 42 | #include <sys/socket.h> |
| 43 | #include <sys/ioctl.h> |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 44 | #include <sys/time.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 45 | #include <net/if.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 46 | #include <errno.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 47 | #include <sys/socket.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 48 | #include <resolv.h> |
| 49 | #include <time.h> |
| 50 | |
Pau Espin Pedrol | 98f8126 | 2020-04-14 18:38:52 +0200 | [diff] [blame] | 51 | #if defined(__linux__) |
Pau Espin Pedrol | ff2ebee | 2020-04-18 23:47:06 +0200 | [diff] [blame] | 52 | #if defined(HAVE_IN6_ADDR_GEN_MODE_NONE) |
Pau Espin Pedrol | 98f8126 | 2020-04-14 18:38:52 +0200 | [diff] [blame] | 53 | #include <linux/if_link.h> |
Pau Espin Pedrol | ff2ebee | 2020-04-18 23:47:06 +0200 | [diff] [blame] | 54 | #endif // HAVE_IN6_ADDR_GEN_MODE_NONE |
Pau Espin Pedrol | 98f8126 | 2020-04-14 18:38:52 +0200 | [diff] [blame] | 55 | #endif |
| 56 | |
Emmanuel Bretelle | 2a10368 | 2010-09-07 17:01:20 +0200 | [diff] [blame] | 57 | #include "../lib/tun.h" |
| 58 | #include "../lib/ippool.h" |
| 59 | #include "../lib/syserr.h" |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 60 | #include "../lib/netns.h" |
Pau Espin Pedrol | e2b0961 | 2020-04-15 15:09:30 +0200 | [diff] [blame] | 61 | #include "../lib/icmpv6.h" |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 62 | #include "../gtp/pdp.h" |
| 63 | #include "../gtp/gtp.h" |
| 64 | #include "cmdline.h" |
| 65 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 66 | #define IPADDRLEN 256 /* Character length of addresses */ |
| 67 | #define MAXCONTEXTS 1024 /* Max number of allowed contexts */ |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 68 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 69 | /* HASH tables for IP address allocation */ |
| 70 | struct iphash_t { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 71 | uint8_t inuse; /* 0=free. 1=used by somebody */ |
| 72 | struct iphash_t *ipnext; |
| 73 | struct pdp_t *pdp; |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 74 | struct in46_addr addr; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 75 | }; |
| 76 | struct iphash_t iparr[MAXCONTEXTS]; |
| 77 | struct iphash_t *iphash[MAXCONTEXTS]; |
| 78 | |
| 79 | /* State variable used for ping */ |
| 80 | /* 0: Idle */ |
| 81 | /* 1: Wait_connect */ |
| 82 | /* 2: Connected */ |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 83 | /* 3: Done */ |
| 84 | /* 4: Wait_disconnect */ |
| 85 | /* 5: Disconnected */ |
bjovan | a8f71eb | 2017-02-24 17:39:20 +0100 | [diff] [blame] | 86 | volatile sig_atomic_t state = 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 87 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 88 | struct gsn_t *gsn = NULL; /* GSN instance */ |
| 89 | struct tun_t *tun = NULL; /* TUN instance */ |
| 90 | int maxfd = 0; /* For select() */ |
| 91 | int echoversion = 1; /* First try this version */ |
Pau Espin Pedrol | 042a445 | 2018-04-17 14:31:42 +0200 | [diff] [blame] | 92 | void *tall_sgsnemu_ctx; /* root talloc ctx */ |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 93 | #if defined(__linux__) |
| 94 | int netns = -1; /* network namespace */ |
| 95 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 96 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 97 | /* Struct with local versions of gengetopt options */ |
| 98 | struct { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 99 | int debug; /* Print debug messages */ |
| 100 | int createif; /* Create local network interface */ |
Harald Welte | 73abc38 | 2017-10-10 08:50:11 +0800 | [diff] [blame] | 101 | char *tun_dev_name; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 102 | char *netns; |
Pau Espin Pedrol | a1b3dee | 2020-04-15 14:39:10 +0200 | [diff] [blame] | 103 | struct in46_addr netaddr; /* Network interface */ |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 104 | size_t prefixlen; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 105 | char *ipup, *ipdown; /* Filename of scripts */ |
| 106 | int defaultroute; /* Set up default route */ |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 107 | struct in46_addr pinghost; /* Remote ping host */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 108 | int pingrate; |
| 109 | int pingsize; |
| 110 | int pingcount; |
| 111 | int pingquiet; |
| 112 | struct in_addr listen; |
| 113 | struct in_addr remote; |
| 114 | struct in_addr dns; |
| 115 | int contexts; /* Number of contexts to create */ |
| 116 | int timelimit; /* Number of seconds to be connected */ |
| 117 | char *statedir; |
| 118 | uint64_t imsi; |
| 119 | uint8_t nsapi; |
| 120 | int gtpversion; |
| 121 | struct ul255_t pco; |
| 122 | struct ul255_t qos; |
| 123 | uint16_t cch; |
| 124 | struct ul255_t apn; |
| 125 | uint8_t selmode; |
| 126 | struct ul255_t rattype; |
| 127 | int rattype_given; |
| 128 | struct ul255_t userloc; |
| 129 | int userloc_given; |
| 130 | struct ul255_t rai; |
| 131 | int rai_given; |
| 132 | struct ul255_t mstz; |
| 133 | int mstz_given; |
| 134 | struct ul255_t imeisv; |
| 135 | int imeisv_given; |
| 136 | struct ul16_t msisdn; |
| 137 | int norecovery_given; |
Harald Welte | fbb9c7f | 2017-09-24 11:50:20 +0800 | [diff] [blame] | 138 | int tx_gpdu_seq; |
Harald Welte | 840a8e9 | 2017-09-24 18:12:40 +0800 | [diff] [blame] | 139 | uint8_t pdp_type; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 140 | } options; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 141 | |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 142 | /* Definitions to use for PING. Most of the ping code was derived from */ |
| 143 | /* the original ping program by Mike Muuss */ |
| 144 | |
| 145 | /* IP header and ICMP echo header */ |
| 146 | #define CREATEPING_MAX 2048 |
| 147 | #define CREATEPING_IP 20 |
| 148 | #define CREATEPING_ICMP 8 |
| 149 | |
| 150 | struct ip_ping { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 151 | uint8_t ipver; /* Type and header length */ |
| 152 | uint8_t tos; /* Type of Service */ |
| 153 | uint16_t length; /* Total length */ |
| 154 | uint16_t fragid; /* Identifier */ |
| 155 | uint16_t offset; /* Flags and fragment offset */ |
| 156 | uint8_t ttl; /* Time to live */ |
| 157 | uint8_t protocol; /* Protocol */ |
| 158 | uint16_t ipcheck; /* Header checksum */ |
| 159 | uint32_t src; /* Source address */ |
| 160 | uint32_t dst; /* Destination */ |
| 161 | uint8_t type; /* Type and header length */ |
| 162 | uint8_t code; /* Code */ |
| 163 | uint16_t checksum; /* Header checksum */ |
| 164 | uint16_t ident; /* Identifier */ |
| 165 | uint16_t seq; /* Sequence number */ |
| 166 | uint8_t data[CREATEPING_MAX]; /* Data */ |
| 167 | } __attribute__ ((packed)); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 168 | |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 169 | struct ip6_ping { |
| 170 | struct icmpv6_echo_hdr hdr; |
| 171 | uint8_t data[CREATEPING_MAX]; /* Data */ |
| 172 | } __attribute__ ((packed)); |
| 173 | |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 174 | /* Statistical values for ping */ |
| 175 | int nreceived = 0; |
| 176 | int ntreceived = 0; |
| 177 | int ntransmitted = 0; |
| 178 | int tmin = 999999999; |
| 179 | int tmax = 0; |
| 180 | int tsum = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 181 | int pingseq = 0; /* Ping sequence counter */ |
jjako | afb2a97 | 2003-01-29 21:04:13 +0000 | [diff] [blame] | 182 | struct timeval firstping; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 183 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 184 | static void signal_handler(int signo) |
bjovan | a8f71eb | 2017-02-24 17:39:20 +0100 | [diff] [blame] | 185 | { |
| 186 | if (state == 2) |
| 187 | state = 3; /* Tell main loop to finish. */ |
| 188 | } |
| 189 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 190 | static int ipset(struct iphash_t *ipaddr, struct in46_addr *addr) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 191 | { |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 192 | int hash = ippool_hash(addr) % MAXCONTEXTS; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 193 | struct iphash_t *h; |
| 194 | struct iphash_t *prev = NULL; |
| 195 | ipaddr->ipnext = NULL; |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 196 | ipaddr->addr = *addr; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 197 | for (h = iphash[hash]; h; h = h->ipnext) |
| 198 | prev = h; |
| 199 | if (!prev) |
| 200 | iphash[hash] = ipaddr; |
| 201 | else |
| 202 | prev->ipnext = ipaddr; |
| 203 | return 0; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 206 | static int ipdel(struct iphash_t *ipaddr) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 207 | { |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 208 | int hash = ippool_hash(&ipaddr->addr) % MAXCONTEXTS; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 209 | struct iphash_t *h; |
| 210 | struct iphash_t *prev = NULL; |
| 211 | for (h = iphash[hash]; h; h = h->ipnext) { |
| 212 | if (h == ipaddr) { |
| 213 | if (!prev) |
| 214 | iphash[hash] = h->ipnext; |
| 215 | else |
| 216 | prev->ipnext = h->ipnext; |
| 217 | return 0; |
| 218 | } |
| 219 | prev = h; |
| 220 | } |
| 221 | return EOF; /* End of linked list and not found */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 224 | static int ipget(struct iphash_t **ipaddr, struct in46_addr *addr) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 225 | { |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 226 | int hash = ippool_hash(addr) % MAXCONTEXTS; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 227 | struct iphash_t *h; |
| 228 | for (h = iphash[hash]; h; h = h->ipnext) { |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 229 | if (in46a_equal(&h->addr, addr)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 230 | *ipaddr = h; |
| 231 | return 0; |
| 232 | } |
| 233 | } |
| 234 | return EOF; /* End of linked list and not found */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 235 | } |
| 236 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 237 | /* Used to write process ID to file. Assume someone else will delete */ |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 238 | static void log_pid(char *pidfile) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 239 | { |
| 240 | FILE *file; |
| 241 | mode_t oldmask; |
| 242 | |
| 243 | oldmask = umask(022); |
| 244 | file = fopen(pidfile, "w"); |
| 245 | umask(oldmask); |
| 246 | if (!file) |
| 247 | return; |
| 248 | fprintf(file, "%d\n", (int)getpid()); |
| 249 | fclose(file); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 252 | static int process_options(int argc, char **argv) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 253 | { |
| 254 | /* gengeopt declarations */ |
| 255 | struct gengetopt_args_info args_info; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 256 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 257 | struct hostent *host; |
| 258 | unsigned int n; |
| 259 | uint16_t i; |
| 260 | uint8_t a; |
| 261 | uint8_t b; |
| 262 | char *tmp; |
| 263 | char *pch; |
| 264 | char *type; |
| 265 | char *mcc; |
| 266 | char *mnc; |
Andreas Schultz | 10abfba | 2015-11-13 15:57:37 +0100 | [diff] [blame] | 267 | char *tok, *apn; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 268 | char *lac; |
| 269 | int lac_d; |
| 270 | char *rest; |
| 271 | char *userloc_el[] = { "TYPE", "MCC", "MNC", "LAC", "REST" }; |
| 272 | char *rai_el[] = { "MCC", "MNC", "LAC", "RAC" }; |
| 273 | char *mstz_el[] = { "SIGN", "QUARTERS", "DST" }; |
| 274 | int sign; |
| 275 | int nbquarters; |
| 276 | int DST; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 277 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 278 | if (cmdline_parser(argc, argv, &args_info) != 0) |
| 279 | return -1; |
| 280 | if (args_info.debug_flag) { |
| 281 | if (args_info.remote_arg) |
| 282 | printf("remote: %s\n", args_info.remote_arg); |
| 283 | if (args_info.listen_arg) |
| 284 | printf("listen: %s\n", args_info.listen_arg); |
| 285 | if (args_info.conf_arg) |
| 286 | printf("conf: %s\n", args_info.conf_arg); |
| 287 | printf("debug: %d\n", args_info.debug_flag); |
| 288 | if (args_info.imsi_arg) |
| 289 | printf("imsi: %s\n", args_info.imsi_arg); |
| 290 | printf("qos: %#08x\n", args_info.qos_arg); |
| 291 | printf("qose1: %#0.16llx\n", args_info.qose1_arg); |
| 292 | printf("qose2: %#04x\n", args_info.qose2_arg); |
| 293 | printf("qose3: %#06x\n", args_info.qose3_arg); |
| 294 | printf("qose4: %#06x\n", args_info.qose4_arg); |
| 295 | printf("charging: %#04x\n", args_info.charging_arg); |
| 296 | if (args_info.apn_arg) |
| 297 | printf("apn: %s\n", args_info.apn_arg); |
| 298 | if (args_info.msisdn_arg) |
| 299 | printf("msisdn: %s\n", args_info.msisdn_arg); |
| 300 | if (args_info.uid_arg) |
| 301 | printf("uid: %s\n", args_info.uid_arg); |
| 302 | if (args_info.pwd_arg) |
| 303 | printf("pwd: %s\n", args_info.pwd_arg); |
| 304 | if (args_info.pidfile_arg) |
| 305 | printf("pidfile: %s\n", args_info.pidfile_arg); |
| 306 | if (args_info.statedir_arg) |
| 307 | printf("statedir: %s\n", args_info.statedir_arg); |
| 308 | if (args_info.dns_arg) |
| 309 | printf("dns: %s\n", args_info.dns_arg); |
| 310 | printf("contexts: %d\n", args_info.contexts_arg); |
| 311 | printf("timelimit: %d\n", args_info.timelimit_arg); |
| 312 | printf("createif: %d\n", args_info.createif_flag); |
Harald Welte | 73abc38 | 2017-10-10 08:50:11 +0800 | [diff] [blame] | 313 | if (args_info.tun_device_arg) |
Harald Welte | 7b9230a | 2018-10-21 13:09:21 +0200 | [diff] [blame] | 314 | printf("tun-device: %s\n", args_info.tun_device_arg); |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 315 | if (args_info.netns_arg) |
| 316 | printf("netns: %s\n", args_info.netns_arg); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 317 | if (args_info.ipup_arg) |
| 318 | printf("ipup: %s\n", args_info.ipup_arg); |
| 319 | if (args_info.ipdown_arg) |
| 320 | printf("ipdown: %s\n", args_info.ipdown_arg); |
| 321 | printf("defaultroute: %d\n", args_info.defaultroute_flag); |
| 322 | if (args_info.pinghost_arg) |
| 323 | printf("pinghost: %s\n", args_info.pinghost_arg); |
| 324 | printf("pingrate: %d\n", args_info.pingrate_arg); |
| 325 | printf("pingsize: %d\n", args_info.pingsize_arg); |
| 326 | printf("pingcount: %d\n", args_info.pingcount_arg); |
| 327 | printf("pingquiet: %d\n", args_info.pingquiet_flag); |
| 328 | printf("norecovery: %d\n", args_info.norecovery_flag); |
Harald Welte | fbb9c7f | 2017-09-24 11:50:20 +0800 | [diff] [blame] | 329 | printf("no-tx-gpdu-seq: %d\n", args_info.no_tx_gpdu_seq_flag); |
Yann BONNAMY | 11a398f | 2010-11-18 10:01:21 +0100 | [diff] [blame] | 330 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 331 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 332 | /* Try out our new parser */ |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 333 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 334 | if (args_info.conf_arg) { |
| 335 | if (cmdline_parser_configfile |
| 336 | (args_info.conf_arg, &args_info, 0, 0, 0) != 0) |
| 337 | return -1; |
| 338 | if (args_info.debug_flag) { |
| 339 | printf("cmdline_parser_configfile\n"); |
| 340 | if (args_info.remote_arg) |
| 341 | printf("remote: %s\n", args_info.remote_arg); |
| 342 | if (args_info.listen_arg) |
| 343 | printf("listen: %s\n", args_info.listen_arg); |
| 344 | if (args_info.conf_arg) |
| 345 | printf("conf: %s\n", args_info.conf_arg); |
| 346 | printf("debug: %d\n", args_info.debug_flag); |
| 347 | if (args_info.imsi_arg) |
| 348 | printf("imsi: %s\n", args_info.imsi_arg); |
| 349 | printf("qos: %#08x\n", args_info.qos_arg); |
| 350 | printf("qose1: %#0.16llx\n", args_info.qose1_arg); |
| 351 | printf("qose2: %#04x\n", args_info.qose2_arg); |
| 352 | printf("qose3: %#06x\n", args_info.qose3_arg); |
| 353 | printf("qose4: %#06x\n", args_info.qose4_arg); |
| 354 | printf("charging: %#04x\n", args_info.charging_arg); |
| 355 | if (args_info.apn_arg) |
| 356 | printf("apn: %s\n", args_info.apn_arg); |
| 357 | if (args_info.msisdn_arg) |
| 358 | printf("msisdn: %s\n", args_info.msisdn_arg); |
| 359 | if (args_info.uid_arg) |
| 360 | printf("uid: %s\n", args_info.uid_arg); |
| 361 | if (args_info.pwd_arg) |
| 362 | printf("pwd: %s\n", args_info.pwd_arg); |
| 363 | if (args_info.pidfile_arg) |
| 364 | printf("pidfile: %s\n", args_info.pidfile_arg); |
| 365 | if (args_info.statedir_arg) |
| 366 | printf("statedir: %s\n", |
| 367 | args_info.statedir_arg); |
| 368 | if (args_info.dns_arg) |
| 369 | printf("dns: %s\n", args_info.dns_arg); |
| 370 | printf("contexts: %d\n", args_info.contexts_arg); |
| 371 | printf("timelimit: %d\n", args_info.timelimit_arg); |
| 372 | printf("createif: %d\n", args_info.createif_flag); |
Harald Welte | 73abc38 | 2017-10-10 08:50:11 +0800 | [diff] [blame] | 373 | if (args_info.tun_device_arg) |
Harald Welte | 9c33210 | 2017-11-06 02:44:42 +0900 | [diff] [blame] | 374 | printf("tun-device: %s\n", args_info.tun_device_arg); |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 375 | if (args_info.netns_arg) |
| 376 | printf("netns: %s\n", args_info.netns_arg); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 377 | if (args_info.ipup_arg) |
| 378 | printf("ipup: %s\n", args_info.ipup_arg); |
| 379 | if (args_info.ipdown_arg) |
| 380 | printf("ipdown: %s\n", args_info.ipdown_arg); |
| 381 | printf("defaultroute: %d\n", |
| 382 | args_info.defaultroute_flag); |
| 383 | if (args_info.pinghost_arg) |
| 384 | printf("pinghost: %s\n", |
| 385 | args_info.pinghost_arg); |
| 386 | printf("pingrate: %d\n", args_info.pingrate_arg); |
| 387 | printf("pingsize: %d\n", args_info.pingsize_arg); |
| 388 | printf("pingcount: %d\n", args_info.pingcount_arg); |
| 389 | printf("pingquiet: %d\n", args_info.pingquiet_flag); |
| 390 | printf("norecovery: %d\n", args_info.norecovery_flag); |
Harald Welte | fbb9c7f | 2017-09-24 11:50:20 +0800 | [diff] [blame] | 391 | printf("no-tx-gpdu-seq: %d\n", args_info.no_tx_gpdu_seq_flag); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 392 | } |
| 393 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 394 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 395 | /* Handle each option */ |
jjako | 1a51df7 | 2004-07-20 08:30:21 +0000 | [diff] [blame] | 396 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 397 | /* foreground */ |
| 398 | /* If fg flag not given run as a daemon */ |
Pau Espin Pedrol | 7c4de07 | 2017-12-14 13:59:02 +0100 | [diff] [blame] | 399 | /* Do not allow sgsnemu to run as deamon |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 400 | if (!args_info.fg_flag) |
| 401 | { |
Pau Espin Pedrol | 7c4de07 | 2017-12-14 13:59:02 +0100 | [diff] [blame] | 402 | closelog(); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 403 | freopen("/dev/null", "w", stdout); |
| 404 | freopen("/dev/null", "w", stderr); |
| 405 | freopen("/dev/null", "r", stdin); |
| 406 | daemon(0, 0); |
| 407 | openlog(PACKAGE, LOG_PID, LOG_DAEMON); |
| 408 | } */ |
jjako | 1a51df7 | 2004-07-20 08:30:21 +0000 | [diff] [blame] | 409 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 410 | /* debug */ |
| 411 | options.debug = args_info.debug_flag; |
jjako | 1a51df7 | 2004-07-20 08:30:21 +0000 | [diff] [blame] | 412 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 413 | /* pidfile */ |
| 414 | /* This has to be done after we have our final pid */ |
| 415 | if (args_info.pidfile_arg) { |
| 416 | log_pid(args_info.pidfile_arg); |
| 417 | } |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 418 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 419 | /* dns */ |
| 420 | /* If no dns option is given use system default */ |
| 421 | /* Do hostname lookup to translate hostname to IP address */ |
| 422 | printf("\n"); |
| 423 | if (args_info.dns_arg) { |
| 424 | if (!(host = gethostbyname(args_info.dns_arg))) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 425 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 426 | "Invalid DNS address: %s!", args_info.dns_arg); |
| 427 | return -1; |
| 428 | } else { |
| 429 | memcpy(&options.dns.s_addr, host->h_addr, |
| 430 | host->h_length); |
| 431 | _res.nscount = 1; |
| 432 | _res.nsaddr_list[0].sin_addr = options.dns; |
| 433 | printf("Using DNS server: %s (%s)\n", |
| 434 | args_info.dns_arg, inet_ntoa(options.dns)); |
| 435 | } |
| 436 | } else { |
| 437 | options.dns.s_addr = 0; |
| 438 | printf("Using default DNS server\n"); |
| 439 | } |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 440 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 441 | /* listen */ |
| 442 | /* If no listen option is specified listen to any local port */ |
| 443 | /* Do hostname lookup to translate hostname to IP address */ |
| 444 | if (args_info.listen_arg) { |
| 445 | if (!(host = gethostbyname(args_info.listen_arg))) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 446 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 447 | "Invalid listening address: %s!", |
| 448 | args_info.listen_arg); |
| 449 | return -1; |
| 450 | } else { |
| 451 | memcpy(&options.listen.s_addr, host->h_addr, |
| 452 | host->h_length); |
| 453 | printf("Local IP address is: %s (%s)\n", |
| 454 | args_info.listen_arg, inet_ntoa(options.listen)); |
| 455 | } |
| 456 | } else { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 457 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | fdf3358 | 2019-12-01 09:25:27 +0100 | [diff] [blame] | 458 | "Listening address must be specified!"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 459 | return -1; |
| 460 | } |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 461 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 462 | /* remote */ |
| 463 | /* If no remote option is specified terminate */ |
| 464 | /* Do hostname lookup to translate hostname to IP address */ |
| 465 | if (args_info.remote_arg) { |
| 466 | if (!(host = gethostbyname(args_info.remote_arg))) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 467 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 468 | "Invalid remote address: %s!", |
| 469 | args_info.remote_arg); |
| 470 | return -1; |
| 471 | } else { |
| 472 | memcpy(&options.remote.s_addr, host->h_addr, |
| 473 | host->h_length); |
| 474 | printf("Remote IP address is: %s (%s)\n", |
| 475 | args_info.remote_arg, inet_ntoa(options.remote)); |
| 476 | } |
| 477 | } else { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 478 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 479 | "No remote address given!"); |
| 480 | return -1; |
| 481 | } |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 482 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 483 | /* imsi */ |
| 484 | if (strlen(args_info.imsi_arg) != 15) { |
| 485 | printf("Invalid IMSI\n"); |
| 486 | return -1; |
| 487 | } |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 488 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 489 | options.imsi = 0xf000000000000000ull; |
| 490 | options.imsi |= ((uint64_t) (args_info.imsi_arg[0] - 48)); |
| 491 | options.imsi |= ((uint64_t) (args_info.imsi_arg[1] - 48)) << 4; |
| 492 | options.imsi |= ((uint64_t) (args_info.imsi_arg[2] - 48)) << 8; |
| 493 | options.imsi |= ((uint64_t) (args_info.imsi_arg[3] - 48)) << 12; |
| 494 | options.imsi |= ((uint64_t) (args_info.imsi_arg[4] - 48)) << 16; |
| 495 | options.imsi |= ((uint64_t) (args_info.imsi_arg[5] - 48)) << 20; |
| 496 | options.imsi |= ((uint64_t) (args_info.imsi_arg[6] - 48)) << 24; |
| 497 | options.imsi |= ((uint64_t) (args_info.imsi_arg[7] - 48)) << 28; |
| 498 | options.imsi |= ((uint64_t) (args_info.imsi_arg[8] - 48)) << 32; |
| 499 | options.imsi |= ((uint64_t) (args_info.imsi_arg[9] - 48)) << 36; |
| 500 | options.imsi |= ((uint64_t) (args_info.imsi_arg[10] - 48)) << 40; |
| 501 | options.imsi |= ((uint64_t) (args_info.imsi_arg[11] - 48)) << 44; |
| 502 | options.imsi |= ((uint64_t) (args_info.imsi_arg[12] - 48)) << 48; |
| 503 | options.imsi |= ((uint64_t) (args_info.imsi_arg[13] - 48)) << 52; |
| 504 | options.imsi |= ((uint64_t) (args_info.imsi_arg[14] - 48)) << 56; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 505 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 506 | printf("IMSI is: %s (%#08llx)\n", |
| 507 | args_info.imsi_arg, options.imsi); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 508 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 509 | /* nsapi */ |
| 510 | if ((args_info.nsapi_arg > 15) || (args_info.nsapi_arg < 0)) { |
| 511 | printf("Invalid NSAPI\n"); |
| 512 | return -1; |
| 513 | } |
| 514 | options.nsapi = args_info.nsapi_arg; |
| 515 | printf("Using NSAPI: %d\n", args_info.nsapi_arg); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 516 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 517 | /* qos */ |
| 518 | options.qos.l = 4; |
| 519 | options.qos.v[3] = (args_info.qos_arg) & 0xff; |
| 520 | options.qos.v[2] = ((args_info.qos_arg) >> 8) & 0xff; |
| 521 | options.qos.v[1] = ((args_info.qos_arg) >> 16) & 0xff; |
| 522 | options.qos.v[0] = ((args_info.qos_arg) >> 24) & 0xff; |
| 523 | /* Extensions according to 3GPP TS 24.008 */ |
| 524 | if (args_info.qose1_given == 1) { |
| 525 | options.qos.l = 12; |
| 526 | options.qos.v[11] = (args_info.qose1_arg) & 0xff; |
| 527 | options.qos.v[10] = ((args_info.qose1_arg) >> 8) & 0xff; |
| 528 | options.qos.v[9] = ((args_info.qose1_arg) >> 16) & 0xff; |
| 529 | options.qos.v[8] = ((args_info.qose1_arg) >> 24) & 0xff; |
| 530 | options.qos.v[7] = ((args_info.qose1_arg) >> 32) & 0xff; |
| 531 | options.qos.v[6] = ((args_info.qose1_arg) >> 40) & 0xff; |
| 532 | options.qos.v[5] = ((args_info.qose1_arg) >> 48) & 0xff; |
| 533 | options.qos.v[4] = ((args_info.qose1_arg) >> 56) & 0xff; |
| 534 | if (args_info.qose2_given == 1) { |
| 535 | options.qos.l = 13; |
| 536 | options.qos.v[12] = (args_info.qose2_arg) & 0xff; |
| 537 | if (args_info.qose3_given == 1) { |
| 538 | options.qos.l = 15; |
| 539 | options.qos.v[14] = |
| 540 | (args_info.qose3_arg) & 0xff; |
| 541 | options.qos.v[13] = |
| 542 | ((args_info.qose3_arg) >> 8) & 0xff; |
| 543 | if (args_info.qose4_given == 1) { |
| 544 | options.qos.l = 17; |
| 545 | options.qos.v[16] = |
| 546 | (args_info.qose4_arg) & 0xff; |
| 547 | options.qos.v[15] = |
| 548 | ((args_info.qose4_arg) >> 8) & 0xff; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | } |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 553 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 554 | /* charging */ |
| 555 | options.cch = args_info.charging_arg; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 556 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 557 | /* contexts */ |
| 558 | if (args_info.contexts_arg > MAXCONTEXTS) { |
| 559 | printf("Contexts has to be less than %d\n", MAXCONTEXTS); |
| 560 | return -1; |
| 561 | } |
| 562 | options.contexts = args_info.contexts_arg; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 563 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 564 | /* Timelimit */ |
| 565 | options.timelimit = args_info.timelimit_arg; |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 566 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 567 | /* gtpversion */ |
| 568 | if ((args_info.gtpversion_arg > 1) || (args_info.gtpversion_arg < 0)) { |
| 569 | printf("Invalid GTP version\n"); |
| 570 | return -1; |
| 571 | } |
| 572 | options.gtpversion = args_info.gtpversion_arg; |
| 573 | printf("Using GTP version: %d\n", args_info.gtpversion_arg); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 574 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 575 | /* apn */ |
| 576 | if (strlen(args_info.apn_arg) > (sizeof(options.apn.v) - 1)) { |
| 577 | printf("Invalid APN\n"); |
| 578 | return -1; |
| 579 | } |
Andreas Schultz | 10abfba | 2015-11-13 15:57:37 +0100 | [diff] [blame] | 580 | options.apn.l = strlen(args_info.apn_arg) + 1; |
| 581 | |
| 582 | apn = (char *)options.apn.v; |
| 583 | for (tok = strtok(args_info.apn_arg, "."); |
| 584 | tok != NULL; |
| 585 | tok = strtok(NULL, ".")) { |
| 586 | size_t len = strlen(tok); |
| 587 | |
| 588 | *apn++ = (char)len; |
| 589 | strncpy(apn, tok, len); |
| 590 | apn += len; |
| 591 | } |
| 592 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 593 | printf("Using APN: %s\n", args_info.apn_arg); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 594 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 595 | /* selmode */ |
| 596 | options.selmode = args_info.selmode_arg; |
| 597 | printf("Using selection mode: %d\n", args_info.selmode_arg); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 598 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 599 | /* rattype */ |
| 600 | if (args_info.rattype_given == 1) { |
| 601 | options.rattype_given = 1; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame] | 602 | options.rattype.l = 1; |
| 603 | options.rattype.v[0] = args_info.rattype_arg; |
| 604 | printf("Using RAT Type: %d\n", args_info.rattype_arg); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 605 | } |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 606 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 607 | /* userloc */ |
| 608 | if (args_info.userloc_given == 1) { |
| 609 | printf("Using User Location Information: %s\n", |
| 610 | args_info.userloc_arg); |
| 611 | tmp = args_info.userloc_arg; |
| 612 | n = 0; |
| 613 | pch = strtok(tmp, "."); |
| 614 | while (pch != NULL) { |
| 615 | userloc_el[n] = pch; |
| 616 | pch = strtok(NULL, "."); |
| 617 | n++; |
| 618 | } |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 619 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 620 | options.userloc_given = 1; |
| 621 | options.userloc.l = 8; |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 622 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 623 | /* 3GPP Geographic Location Type t0 / t1 / t2 */ |
| 624 | type = userloc_el[0]; |
| 625 | printf("->type : %c\n", type[0]); |
| 626 | if ((strlen(type) != 1) || (!isdigit(type[0]))) { |
| 627 | printf("Invalid type \n"); |
| 628 | return -1; |
| 629 | } |
| 630 | /* options.userloc.v[0] = 0x00 */ |
| 631 | options.userloc.v[0] = type[0] - 48; |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 632 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 633 | /* MCC */ |
| 634 | mcc = userloc_el[1]; |
| 635 | printf("->mcc : %s\n", mcc); |
| 636 | if (strlen(mcc) != 3) { |
Ruben Undheim | cc077ae | 2016-05-24 23:31:47 +0200 | [diff] [blame] | 637 | printf("Invalid MCC length\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 638 | return -1; |
| 639 | } |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 640 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 641 | /* MNC */ |
| 642 | mnc = userloc_el[2]; |
| 643 | printf("->mnc : %s\n", mnc); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 644 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 645 | /* octet 5 - MCC Digit 2 - MCC Digit 1 */ |
| 646 | /* options.userloc.v[1] = 0x52 */ |
| 647 | a = (uint8_t) (mcc[0] - 48); |
| 648 | b = (uint8_t) (mcc[1] - 48); |
| 649 | options.userloc.v[1] = 16 * b + a; |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 650 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 651 | /* octet 6 - MNC Digit 3 - MCC Digit 3 */ |
| 652 | /* options.userloc.v[2] = 0xf0 */ |
| 653 | a = (uint8_t) (mcc[2] - 48); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 654 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 655 | if ((strlen(mnc) > 3) || (strlen(mnc) < 2)) { |
Ruben Undheim | cc077ae | 2016-05-24 23:31:47 +0200 | [diff] [blame] | 656 | printf("Invalid MNC length\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 657 | return -1; |
| 658 | } |
| 659 | if (strlen(mnc) == 2) { |
| 660 | b = 15; |
| 661 | } |
| 662 | if (strlen(mnc) == 3) { |
| 663 | b = (uint8_t) (mnc[2] - 48); |
| 664 | } |
| 665 | options.userloc.v[2] = 16 * b + a; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 666 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 667 | /* octet 7 - MNC Digit 2 - MNC Digit 1 */ |
| 668 | /* options.userloc.v[3] = 0x99 */ |
| 669 | a = (uint8_t) (mnc[0] - 48); |
| 670 | b = (uint8_t) (mnc[1] - 48); |
| 671 | options.userloc.v[3] = 16 * b + a; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 672 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 673 | /* LAC */ |
| 674 | lac = userloc_el[3]; |
| 675 | /*options.userloc.v[4] = 0x12 ; */ |
| 676 | /*options.userloc.v[5] = 0x10 ; */ |
| 677 | printf("->LAC: %s\n", lac); |
| 678 | lac_d = atoi(lac); |
| 679 | if (lac_d > 65535 || lac_d < 1) { |
| 680 | printf("Invalid LAC\n"); |
| 681 | return -1; |
| 682 | } |
| 683 | i = lac_d >> 8; |
| 684 | options.userloc.v[4] = i; /* octet 8 - LAC */ |
| 685 | options.userloc.v[5] = lac_d; /* octet 9 - LAC */ |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 686 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 687 | /* CI/SAC/RAC */ |
| 688 | rest = userloc_el[4]; |
| 689 | printf("->CI/SAC/RAC : %s\n", rest); |
| 690 | lac_d = atoi(rest); |
| 691 | if (lac_d > 65535 || lac_d < 1) { |
| 692 | printf("Invalid CI/SAC/RAC\n"); |
| 693 | return -1; |
| 694 | } |
| 695 | /*options.userloc.v[6] = 0x04 ; */ |
| 696 | /*options.userloc.v[7] = 0xb7 ; */ |
| 697 | i = lac_d >> 8; |
| 698 | options.userloc.v[6] = i; /* octet 10 - t0,CI / t1,SAC / t2,RAC */ |
| 699 | options.userloc.v[7] = lac_d; /* octet 11 - t0,CI / t1,SAC / t2,RAC */ |
| 700 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 701 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 702 | /* RAI */ |
| 703 | if (args_info.rai_given == 1) { |
| 704 | printf("Using RAI: %s\n", args_info.rai_arg); |
| 705 | tmp = args_info.rai_arg; |
| 706 | n = 0; |
| 707 | pch = strtok(tmp, "."); |
| 708 | while (pch != NULL) { |
| 709 | rai_el[n] = pch; |
| 710 | pch = strtok(NULL, "."); |
| 711 | n++; |
| 712 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 713 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 714 | options.rai_given = 1; |
| 715 | options.rai.l = 6; |
jjako | c6762cf | 2004-04-28 14:52:58 +0000 | [diff] [blame] | 716 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 717 | /* MCC */ |
| 718 | mcc = rai_el[0]; |
| 719 | printf("->mcc : %s\n", mcc); |
| 720 | if (strlen(mcc) != 3) { |
Ruben Undheim | cc077ae | 2016-05-24 23:31:47 +0200 | [diff] [blame] | 721 | printf("Invalid MCC length\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 722 | return -1; |
| 723 | } |
| 724 | |
| 725 | /* MNC */ |
| 726 | mnc = rai_el[1]; |
| 727 | printf("->mnc : %s\n", mnc); |
| 728 | |
| 729 | a = (uint8_t) (mcc[0] - 48); |
| 730 | b = (uint8_t) (mcc[1] - 48); |
| 731 | options.rai.v[0] = 16 * b + a; |
| 732 | |
| 733 | /* octet 3 - MNC Digit 3 - MCC Digit 3 */ |
| 734 | a = (uint8_t) (mcc[2] - 48); |
| 735 | |
| 736 | if ((strlen(mnc) > 3) || (strlen(mnc) < 2)) { |
Ruben Undheim | cc077ae | 2016-05-24 23:31:47 +0200 | [diff] [blame] | 737 | printf("Invalid MNC length\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 738 | return -1; |
| 739 | } |
| 740 | if (strlen(mnc) == 2) { |
| 741 | b = 15; |
| 742 | } |
| 743 | if (strlen(mnc) == 3) { |
| 744 | b = (uint8_t) (mnc[2] - 48); |
| 745 | } |
| 746 | options.rai.v[1] = 16 * b + a; |
| 747 | |
| 748 | /* octet 4 - MNC Digit 2 - MNC Digit 1 */ |
| 749 | a = (uint8_t) (mnc[0] - 48); |
| 750 | b = (uint8_t) (mnc[1] - 48); |
| 751 | options.rai.v[2] = 16 * b + a; |
| 752 | |
| 753 | /* LAC */ |
| 754 | lac = rai_el[2]; |
| 755 | printf("->LAC: %s\n", lac); |
| 756 | lac_d = atoi(lac); |
| 757 | if (lac_d > 65535 || lac_d < 1) { |
| 758 | printf("Invalid LAC\n"); |
| 759 | return -1; |
| 760 | } |
| 761 | i = lac_d >> 8; |
| 762 | options.rai.v[3] = i; /* octet 5 - LAC */ |
| 763 | options.rai.v[4] = lac_d; /* octet 6 - LAC */ |
| 764 | |
| 765 | /* RAC */ |
| 766 | rest = rai_el[3]; |
| 767 | printf("->RAC : %s\n", rest); |
| 768 | lac_d = atoi(rest); |
| 769 | if (lac_d > 255 || lac_d < 1) { |
| 770 | printf("Invalid RAC\n"); |
| 771 | return -1; |
| 772 | } |
| 773 | options.rai.v[5] = lac_d; /* octet 7 - RAC */ |
| 774 | } |
| 775 | |
| 776 | /* mstz */ |
| 777 | if (args_info.mstz_given == 1) { |
| 778 | options.mstz_given = 1; |
| 779 | options.mstz.l = 2; |
| 780 | |
| 781 | printf("Using MS Time Zone: %s\n", args_info.mstz_arg); |
| 782 | tmp = args_info.mstz_arg; |
| 783 | n = 0; |
| 784 | pch = strtok(tmp, "."); |
| 785 | while (pch != NULL) { |
| 786 | mstz_el[n] = pch; |
| 787 | pch = strtok(NULL, "."); |
| 788 | n++; |
| 789 | } |
| 790 | |
| 791 | /* sign */ |
| 792 | sign = atoi(mstz_el[0]); |
| 793 | printf("->Sign (0=+ / 1=-): %d\n", sign); |
| 794 | if (sign != 0 && sign != 1) { |
| 795 | printf("Invalid Sign \n"); |
| 796 | return -1; |
| 797 | } |
| 798 | /* nbquarters */ |
| 799 | nbquarters = atoi(mstz_el[1]); |
| 800 | printf("->Number of Quarters of an Hour : %d\n", nbquarters); |
| 801 | if (nbquarters < 0 || nbquarters > 79) { |
| 802 | printf("Invalid Number of Quarters \n"); |
| 803 | return -1; |
| 804 | } |
| 805 | /* DST */ |
| 806 | DST = atoi(mstz_el[2]); |
| 807 | printf("->Daylight Saving Time Adjustment : %d\n", DST); |
| 808 | if (DST < 0 || DST > 3) { |
| 809 | printf("Invalid DST Adjustment \n"); |
| 810 | return -1; |
| 811 | } |
| 812 | /* 12345678 |
| 813 | bits 123 = unit of # of quarters of an hour |
| 814 | bits 678 = # of quarters of an hour / 10 |
| 815 | bit 5 = sign |
| 816 | */ |
| 817 | i = nbquarters % 10; |
| 818 | i = i << 4; |
| 819 | i = i + nbquarters / 10 + 8 * sign; |
| 820 | /* options.mstz.v[0] = 0x69 ; */ |
| 821 | /* options.mstz.v[1] = 0x01 ; */ |
| 822 | options.mstz.v[0] = i; |
| 823 | options.mstz.v[1] = DST; |
| 824 | n = (i & 0x08) ? '-' : '+'; |
| 825 | printf |
| 826 | ("->Human Readable MS Time Zone : GMT %c %d hours %d minutes\n", |
| 827 | n, nbquarters / 4, nbquarters % 4 * 15); |
| 828 | } |
| 829 | |
| 830 | /* imeisv */ |
| 831 | if (args_info.imeisv_given == 1) { |
| 832 | options.imeisv_given = 1; |
| 833 | if (strlen(args_info.imeisv_arg) != 16) { |
| 834 | printf("Invalid IMEI(SV)\n"); |
| 835 | return -1; |
| 836 | } |
| 837 | options.imeisv.l = 8; |
| 838 | for (n = 0; n < 8; n++) { |
| 839 | a = (uint8_t) (args_info.imeisv_arg[2 * n] - 48); |
| 840 | b = (uint8_t) (args_info.imeisv_arg[2 * n + 1] - 48); |
| 841 | options.imeisv.v[n] = 16 * b + a; |
| 842 | } |
| 843 | printf("Using IMEI(SV): %s\n", args_info.imeisv_arg); |
| 844 | } |
| 845 | |
| 846 | /* msisdn */ |
| 847 | if (strlen(args_info.msisdn_arg) > (sizeof(options.msisdn.v) - 1)) { |
| 848 | printf("Invalid MSISDN\n"); |
| 849 | return -1; |
| 850 | } |
| 851 | options.msisdn.l = 1; |
| 852 | options.msisdn.v[0] = 0x91; /* International format */ |
| 853 | for (n = 0; n < strlen(args_info.msisdn_arg); n++) { |
| 854 | if ((n % 2) == 0) { |
| 855 | options.msisdn.v[((int)n / 2) + 1] = |
| 856 | args_info.msisdn_arg[n] - 48 + 0xf0; |
| 857 | options.msisdn.l += 1; |
| 858 | } else { |
| 859 | options.msisdn.v[((int)n / 2) + 1] = |
| 860 | (options.msisdn.v[((int)n / 2) + 1] & 0x0f) + |
| 861 | (args_info.msisdn_arg[n] - 48) * 16; |
| 862 | } |
| 863 | } |
| 864 | printf("Using MSISDN: %s\n", args_info.msisdn_arg); |
| 865 | |
| 866 | /* UID and PWD */ |
| 867 | /* Might need to also insert stuff like DNS etc. */ |
| 868 | if ((strlen(args_info.uid_arg) + strlen(args_info.pwd_arg) + 10) > |
| 869 | (sizeof(options.pco.v) - 1)) { |
| 870 | printf("invalid UID and PWD\n"); |
| 871 | return -1; |
| 872 | } |
| 873 | options.pco.l = |
| 874 | strlen(args_info.uid_arg) + strlen(args_info.pwd_arg) + 10; |
| 875 | options.pco.v[0] = 0x80; /* PPP */ |
| 876 | options.pco.v[1] = 0xc0; /* PAP */ |
| 877 | options.pco.v[2] = 0x23; |
| 878 | options.pco.v[3] = |
| 879 | strlen(args_info.uid_arg) + strlen(args_info.pwd_arg) + 6; |
| 880 | options.pco.v[4] = 0x01; /* Authenticate request */ |
| 881 | options.pco.v[5] = 0x01; |
| 882 | options.pco.v[6] = 0x00; /* MSB of length */ |
| 883 | options.pco.v[7] = |
| 884 | strlen(args_info.uid_arg) + strlen(args_info.pwd_arg) + 6; |
| 885 | options.pco.v[8] = strlen(args_info.uid_arg); |
| 886 | memcpy(&options.pco.v[9], args_info.uid_arg, strlen(args_info.uid_arg)); |
| 887 | options.pco.v[9 + strlen(args_info.uid_arg)] = |
| 888 | strlen(args_info.pwd_arg); |
| 889 | memcpy(&options.pco.v[10 + strlen(args_info.uid_arg)], |
| 890 | args_info.pwd_arg, strlen(args_info.pwd_arg)); |
| 891 | |
| 892 | /* createif */ |
| 893 | options.createif = args_info.createif_flag; |
Harald Welte | 73abc38 | 2017-10-10 08:50:11 +0800 | [diff] [blame] | 894 | options.tun_dev_name = args_info.tun_device_arg; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 895 | options.netns = args_info.netns_arg; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 896 | |
| 897 | /* net */ |
| 898 | /* Store net as in_addr net and mask */ |
| 899 | if (args_info.net_arg) { |
| 900 | if (ippool_aton |
Pau Espin Pedrol | a1b3dee | 2020-04-15 14:39:10 +0200 | [diff] [blame] | 901 | (&options.netaddr, &options.prefixlen, args_info.net_arg, 0)) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 902 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 903 | "Invalid network address: %s!", |
| 904 | args_info.net_arg); |
| 905 | exit(1); |
| 906 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 907 | } else { |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 908 | options.prefixlen = 0; |
Pau Espin Pedrol | f5e40b7 | 2017-12-14 14:01:23 +0100 | [diff] [blame] | 909 | memset(&options.netaddr, 0, sizeof(options.netaddr)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 910 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 911 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 912 | /* ipup */ |
| 913 | options.ipup = args_info.ipup_arg; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 914 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 915 | /* ipdown */ |
| 916 | options.ipdown = args_info.ipdown_arg; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 917 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 918 | /* statedir */ |
| 919 | options.statedir = args_info.statedir_arg; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 920 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 921 | /* defaultroute */ |
| 922 | options.defaultroute = args_info.defaultroute_flag; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 923 | |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 924 | /* PDP Type */ |
| 925 | if (!strcmp(args_info.pdp_type_arg, "v6")) |
| 926 | options.pdp_type = PDP_EUA_TYPE_v6; |
| 927 | else if (!strcmp(args_info.pdp_type_arg, "v4")) |
| 928 | options.pdp_type = PDP_EUA_TYPE_v4; |
| 929 | else { |
| 930 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Unsupported/unknown PDP Type '%s'\n", |
| 931 | args_info.pdp_type_arg); |
| 932 | return -1; |
| 933 | } |
| 934 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 935 | /* pinghost */ |
| 936 | /* Store ping host as in_addr */ |
| 937 | if (args_info.pinghost_arg) { |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 938 | struct addrinfo hints; |
| 939 | struct addrinfo *result; |
| 940 | memset(&hints, 0, sizeof(struct addrinfo)); |
| 941 | switch (options.pdp_type) { |
| 942 | case PDP_EUA_TYPE_v4: |
| 943 | hints.ai_family = AF_INET; |
| 944 | break; |
| 945 | case PDP_EUA_TYPE_v6: |
| 946 | hints.ai_family = AF_INET6; |
| 947 | break; |
| 948 | default: |
| 949 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "lookup(AF_UNSPEC) %d", options.pdp_type); |
| 950 | hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ |
| 951 | } |
| 952 | hints.ai_socktype = SOCK_DGRAM; |
| 953 | hints.ai_flags = 0; |
| 954 | hints.ai_protocol = 0; |
| 955 | hints.ai_canonname = NULL; |
| 956 | hints.ai_addr = NULL; |
| 957 | hints.ai_next = NULL; |
| 958 | if ((i = getaddrinfo(args_info.pinghost_arg, NULL, &hints, &result)) != 0) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 959 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 960 | "Invalid ping host '%s': %s", |
| 961 | args_info.pinghost_arg, gai_strerror(i)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 962 | return -1; |
| 963 | } else { |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 964 | switch (result->ai_family) { |
| 965 | case AF_INET: |
| 966 | options.pinghost.len = sizeof(struct in_addr); |
| 967 | options.pinghost.v4 = ((struct sockaddr_in*)result->ai_addr)->sin_addr; |
| 968 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "AF_INET %d", options.pinghost.len); |
| 969 | break; |
| 970 | case AF_INET6: |
| 971 | options.pinghost.len = sizeof(struct in6_addr); |
| 972 | options.pinghost.v6 = ((struct sockaddr_in6*)result->ai_addr)->sin6_addr; |
| 973 | break; |
| 974 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 975 | printf("Using ping host: %s (%s)\n", |
| 976 | args_info.pinghost_arg, |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 977 | in46a_ntoa(&options.pinghost)); |
| 978 | freeaddrinfo(result); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 979 | } |
| 980 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 981 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 982 | /* Other ping parameters */ |
| 983 | options.pingrate = args_info.pingrate_arg; |
| 984 | options.pingsize = args_info.pingsize_arg; |
| 985 | options.pingcount = args_info.pingcount_arg; |
| 986 | options.pingquiet = args_info.pingquiet_flag; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 987 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 988 | /* norecovery */ |
| 989 | options.norecovery_given = args_info.norecovery_flag; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 990 | |
Harald Welte | fbb9c7f | 2017-09-24 11:50:20 +0800 | [diff] [blame] | 991 | if (args_info.no_tx_gpdu_seq_flag) |
| 992 | options.tx_gpdu_seq = 0; |
| 993 | else |
| 994 | options.tx_gpdu_seq = 1; |
| 995 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 996 | return 0; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 997 | |
| 998 | } |
| 999 | |
Pau Espin Pedrol | 98f8126 | 2020-04-14 18:38:52 +0200 | [diff] [blame] | 1000 | /* write a single value to a /proc file */ |
| 1001 | static int proc_write(const char *path, const char *value) |
| 1002 | { |
| 1003 | int ret; |
| 1004 | FILE *f; |
| 1005 | |
| 1006 | f = fopen(path, "w"); |
| 1007 | if (!f) { |
| 1008 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "fopen(%s) failed!\n", path); |
| 1009 | return -1; |
| 1010 | } |
| 1011 | |
| 1012 | if ((ret = fputs(value, f)) < 0) { |
| 1013 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "proc_write(%s, %s) failed!\n", path, value); |
| 1014 | } else { |
| 1015 | ret = 0; |
| 1016 | } |
| 1017 | fclose(f); |
| 1018 | return ret; |
| 1019 | } |
| 1020 | |
| 1021 | /* Write value of to /proc/sys/net/ipv6/conf file for given device. |
| 1022 | * Memory is dynamically allocated, caller must free it later. */ |
| 1023 | static int proc_ipv6_conf_write(const char *dev, const char *file, const char *value) |
| 1024 | { |
| 1025 | const char *fmt = "/proc/sys/net/ipv6/conf/%s/%s"; |
| 1026 | char path[strlen(fmt) + strlen(dev) + strlen(file)+1]; |
| 1027 | snprintf(path, sizeof(path), fmt, dev, file); |
| 1028 | return proc_write(path, value); |
| 1029 | } |
| 1030 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1031 | static char *print_ipprot(int t) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1032 | { |
Harald Welte | e37f48e | 2017-10-10 09:05:50 +0800 | [diff] [blame] | 1033 | struct protoent *pe = getprotobynumber(t); |
| 1034 | |
| 1035 | if (!pe) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1036 | return "Unknown"; |
Harald Welte | e37f48e | 2017-10-10 09:05:50 +0800 | [diff] [blame] | 1037 | else |
| 1038 | return pe->p_name; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1041 | static char *print_icmptype(int t) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1042 | { |
| 1043 | static char *ttab[] = { |
| 1044 | "Echo Reply", |
| 1045 | "ICMP 1", |
| 1046 | "ICMP 2", |
| 1047 | "Dest Unreachable", |
| 1048 | "Source Quench", |
| 1049 | "Redirect", |
| 1050 | "ICMP 6", |
| 1051 | "ICMP 7", |
| 1052 | "Echo", |
| 1053 | "ICMP 9", |
| 1054 | "ICMP 10", |
| 1055 | "Time Exceeded", |
| 1056 | "Parameter Problem", |
| 1057 | "Timestamp", |
| 1058 | "Timestamp Reply", |
| 1059 | "Info Request", |
| 1060 | "Info Reply" |
| 1061 | }; |
| 1062 | if (t < 0 || t > 16) |
| 1063 | return ("OUT-OF-RANGE"); |
| 1064 | return (ttab[t]); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1067 | static int msisdn_add(struct ul16_t *src, struct ul16_t *dst, int add) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1068 | { |
| 1069 | unsigned int n; |
| 1070 | uint64_t i64 = 0; |
| 1071 | uint8_t msa[sizeof(i64) * 3]; /* Allocate 3 digits per octet (0..255) */ |
| 1072 | unsigned int msalen = 0; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1073 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1074 | /* Convert to uint64_t from ul16_t format (most significant digit first) */ |
| 1075 | /* ul16_t format always starts with 0x91 to indicate international format */ |
| 1076 | /* In ul16_t format 0x0f/0xf0 indicates that digit is not used */ |
| 1077 | for (n = 0; n < src->l; n++) { |
| 1078 | if ((src->v[n] & 0x0f) != 0x0f) { |
| 1079 | i64 *= 10; |
| 1080 | i64 += src->v[n] & 0x0f; |
| 1081 | } |
| 1082 | if ((src->v[n] & 0xf0) != 0xf0) { |
| 1083 | i64 *= 10; |
| 1084 | i64 += (src->v[n] & 0xf0) >> 4; |
| 1085 | } |
| 1086 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1087 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1088 | i64 += add; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1089 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1090 | /* Generate array with least significant digit in first octet */ |
| 1091 | while (i64) { |
| 1092 | msa[msalen++] = i64 % 10; |
| 1093 | i64 = i64 / 10; |
| 1094 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1095 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1096 | /* Convert back to ul16_t format */ |
| 1097 | for (n = 0; n < msalen; n++) { |
| 1098 | if ((n % 2) == 0) { |
| 1099 | dst->v[((int)n / 2)] = msa[msalen - n - 1] + 0xf0; |
| 1100 | dst->l += 1; |
| 1101 | } else { |
| 1102 | dst->v[((int)n / 2)] = (dst->v[((int)n / 2)] & 0x0f) + |
| 1103 | msa[msalen - n - 1] * 16; |
| 1104 | } |
| 1105 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1106 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1107 | return 0; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1108 | |
| 1109 | } |
| 1110 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1111 | static int imsi_add(uint64_t src, uint64_t * dst, int add) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1112 | { |
| 1113 | /* TODO: big endian / small endian ??? */ |
| 1114 | uint64_t i64 = 0; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1115 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1116 | /* Convert from uint64_t bcd to uint64_t integer format */ |
| 1117 | /* The resulting integer format is multiplied by 10 */ |
| 1118 | while (src) { |
| 1119 | if ((src & 0x0f) != 0x0f) { |
| 1120 | i64 *= 10; |
| 1121 | i64 += (src & 0x0f); |
| 1122 | } |
| 1123 | if ((src & 0xf0) != 0xf0) { |
| 1124 | i64 *= 10; |
| 1125 | i64 += (src & 0xf0) >> 4; |
| 1126 | } |
| 1127 | src = src >> 8; |
| 1128 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1129 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1130 | i64 += add * 10; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1131 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1132 | *dst = 0; |
| 1133 | while (i64) { |
| 1134 | *dst = *dst << 4; |
| 1135 | *dst += (i64 % 10); |
| 1136 | i64 = i64 / 10; |
| 1137 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1138 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1139 | *dst |= 0xf000000000000000ull; |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 1140 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1141 | return 0; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1142 | |
| 1143 | } |
| 1144 | |
jjako | afb2a97 | 2003-01-29 21:04:13 +0000 | [diff] [blame] | 1145 | /* Calculate time left until we have to send off next ping packet */ |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1146 | static int ping_timeout(struct timeval *tp) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1147 | { |
| 1148 | struct timezone tz; |
| 1149 | struct timeval tv; |
| 1150 | int diff; |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 1151 | if ((options.pinghost.len) && (2 == state) && |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1152 | ((pingseq < options.pingcount) || (options.pingcount == 0))) { |
| 1153 | gettimeofday(&tv, &tz); |
| 1154 | diff = 1000000 / options.pingrate * pingseq - 1000000 * (tv.tv_sec - firstping.tv_sec) - (tv.tv_usec - firstping.tv_usec); /* Microseconds safe up to 500 sec */ |
| 1155 | tp->tv_sec = 0; |
| 1156 | if (diff > 0) |
| 1157 | tp->tv_usec = diff; |
| 1158 | else { |
| 1159 | /* For some reason we get packet loss if set to zero */ |
| 1160 | tp->tv_usec = 100000 / options.pingrate; /* 10 times pingrate */ |
| 1161 | tp->tv_usec = 0; |
| 1162 | } |
| 1163 | } |
| 1164 | return 0; |
jjako | afb2a97 | 2003-01-29 21:04:13 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1167 | /* Print out statistics when at the end of ping sequence */ |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1168 | static int ping_finish() |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1169 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1170 | struct timezone tz; |
| 1171 | struct timeval tv; |
| 1172 | int elapsed; |
| 1173 | gettimeofday(&tv, &tz); |
| 1174 | elapsed = 1000000 * (tv.tv_sec - firstping.tv_sec) + (tv.tv_usec - firstping.tv_usec); /* Microseconds */ |
| 1175 | printf("\n"); |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 1176 | printf("\n----%s PING Statistics----\n", in46a_ntoa(&options.pinghost)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1177 | printf("%d packets transmitted in %.3f seconds, ", ntransmitted, |
| 1178 | elapsed / 1000000.0); |
| 1179 | printf("%d packets received, ", nreceived); |
| 1180 | if (ntransmitted) { |
| 1181 | if (nreceived > ntransmitted) |
| 1182 | printf("-- somebody's printing up packets!"); |
| 1183 | else |
| 1184 | printf("%d%% packet loss", |
| 1185 | (int)(((ntransmitted - nreceived) * 100) / |
| 1186 | ntransmitted)); |
| 1187 | } |
| 1188 | printf("\n"); |
| 1189 | if (options.debug) |
| 1190 | printf("%d packets received in total\n", ntreceived); |
| 1191 | if (nreceived && tsum) |
| 1192 | printf("round-trip (ms) min/avg/max = %.3f/%.3f/%.3f\n\n", |
| 1193 | tmin / 1000.0, tsum / 1000.0 / nreceived, tmax / 1000.0); |
Pau Espin Pedrol | cdcaeda | 2020-04-15 16:37:58 +0200 | [diff] [blame] | 1194 | printf("%d packets transmitted \n", ntransmitted); |
jjako | afb2a97 | 2003-01-29 21:04:13 +0000 | [diff] [blame] | 1195 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1196 | ntransmitted = 0; |
| 1197 | return 0; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 1200 | static int encaps_ping4(struct pdp_t *pdp, void *pack, unsigned len) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1201 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1202 | struct timeval tv; |
| 1203 | struct timeval *tp; |
| 1204 | struct ip_ping *pingpack = pack; |
| 1205 | struct in_addr src; |
| 1206 | int triptime; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1207 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1208 | src.s_addr = pingpack->src; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1209 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1210 | if (len < CREATEPING_IP + CREATEPING_ICMP) { |
| 1211 | printf("packet too short (%d bytes) from %s\n", len, |
| 1212 | inet_ntoa(src)); |
| 1213 | return 0; |
| 1214 | } |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1215 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1216 | if (pingpack->protocol != 1) { |
| 1217 | if (!options.pingquiet) |
| 1218 | printf("%d bytes from %s: ip_protocol=%d (%s)\n", |
| 1219 | len, inet_ntoa(src), pingpack->protocol, |
| 1220 | print_ipprot(pingpack->protocol)); |
| 1221 | return 0; |
| 1222 | } |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1223 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1224 | if (pingpack->type != 0) { |
| 1225 | if (!options.pingquiet) |
| 1226 | printf |
| 1227 | ("%d bytes from %s: icmp_type=%d (%s) icmp_code=%d\n", |
| 1228 | len, inet_ntoa(src), pingpack->type, |
| 1229 | print_icmptype(pingpack->type), pingpack->code); |
| 1230 | return 0; |
| 1231 | } |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1232 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1233 | nreceived++; |
| 1234 | if (!options.pingquiet) |
| 1235 | printf("%d bytes from %s: icmp_seq=%d", len, |
| 1236 | inet_ntoa(src), ntohs(pingpack->seq)); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1237 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1238 | if (len >= sizeof(struct timeval) + CREATEPING_IP + CREATEPING_ICMP) { |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 1239 | gettimeofday(&tv, NULL); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1240 | tp = (struct timeval *)pingpack->data; |
| 1241 | if ((tv.tv_usec -= tp->tv_usec) < 0) { |
| 1242 | tv.tv_sec--; |
| 1243 | tv.tv_usec += 1000000; |
| 1244 | } |
| 1245 | tv.tv_sec -= tp->tv_sec; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1246 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1247 | triptime = tv.tv_sec * 1000000 + (tv.tv_usec); |
| 1248 | tsum += triptime; |
| 1249 | if (triptime < tmin) |
| 1250 | tmin = triptime; |
| 1251 | if (triptime > tmax) |
| 1252 | tmax = triptime; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1253 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1254 | if (!options.pingquiet) |
| 1255 | printf(" time=%.3f ms\n", triptime / 1000.0); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1256 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1257 | } else if (!options.pingquiet) |
| 1258 | printf("\n"); |
| 1259 | return 0; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 1262 | static int encaps_ping6(struct pdp_t *pdp, struct ip6_hdr *ip6h, unsigned len) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1263 | { |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 1264 | const struct icmpv6_echo_hdr *ic6h = (struct icmpv6_echo_hdr *) ((uint8_t*)ip6h + sizeof(*ip6h)); |
| 1265 | struct timeval tv; |
| 1266 | struct timeval tp; |
| 1267 | int triptime; |
| 1268 | char straddr[128]; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1269 | |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 1270 | if (len < sizeof(struct ip6_hdr)) { |
| 1271 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Packet len too small to contain IPv6 header (%d)", len); |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
| 1275 | if (ip6h->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_ICMPV6) { |
| 1276 | if (!options.pingquiet) |
| 1277 | printf("%d bytes from %s: ip6_protocol=%d (%s)\n", len, |
| 1278 | inet_ntop(AF_INET6, &ip6h->ip6_src, straddr, sizeof(straddr)), |
| 1279 | ip6h->ip6_ctlun.ip6_un1.ip6_un1_nxt, |
| 1280 | print_ipprot(ip6h->ip6_ctlun.ip6_un1.ip6_un1_nxt)); |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | if (len < sizeof(struct ip6_hdr) + sizeof(struct icmpv6_echo_hdr)) { |
| 1285 | LOGP(DSGSN, LOGL_ERROR, "Packet len too small to contain ICMPv6 echo header (%d)\n", len); |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
| 1289 | if (ic6h->hdr.type != 129 || ic6h->hdr.code != 0) { |
| 1290 | if (!options.pingquiet) |
| 1291 | printf |
| 1292 | ("%d bytes from %s: icmp_type=%d icmp_code=%d\n", len, |
| 1293 | inet_ntop(AF_INET6, &ip6h->ip6_src, straddr, sizeof(straddr)), |
| 1294 | ic6h->hdr.type, ic6h->hdr.code); |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | nreceived++; |
| 1299 | if (!options.pingquiet) |
| 1300 | printf("%d bytes from %s: icmp_seq=%d", len, |
| 1301 | inet_ntop(AF_INET6, &ip6h->ip6_src, straddr, sizeof(straddr)), |
| 1302 | ntohs(ic6h->seq)); |
| 1303 | |
| 1304 | if (len >= sizeof(struct ip6_hdr) + sizeof(struct icmpv6_echo_hdr) + sizeof(struct timeval)) { |
| 1305 | gettimeofday(&tv, NULL); |
| 1306 | memcpy(&tp, ic6h->data, sizeof(struct timeval)); |
| 1307 | if ((tv.tv_usec -= tp.tv_usec) < 0) { |
| 1308 | tv.tv_sec--; |
| 1309 | tv.tv_usec += 1000000; |
| 1310 | } |
| 1311 | tv.tv_sec -= tp.tv_sec; |
| 1312 | |
| 1313 | triptime = tv.tv_sec * 1000000 + (tv.tv_usec); |
| 1314 | tsum += triptime; |
| 1315 | if (triptime < tmin) |
| 1316 | tmin = triptime; |
| 1317 | if (triptime > tmax) |
| 1318 | tmax = triptime; |
| 1319 | |
| 1320 | if (!options.pingquiet) |
| 1321 | printf(" time=%.3f ms\n", triptime / 1000.0); |
| 1322 | |
| 1323 | } else if (!options.pingquiet) |
| 1324 | printf("\n"); |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | /* Handle a received ping packet. Print out line and update statistics. */ |
| 1329 | static int encaps_ping(struct pdp_t *pdp, void *pack, unsigned len) |
| 1330 | { |
| 1331 | struct iphdr *iph = (struct iphdr *)pack; |
| 1332 | struct timeval tv; |
| 1333 | |
| 1334 | |
| 1335 | gettimeofday(&tv, NULL); |
| 1336 | if (options.debug) |
| 1337 | printf("%d.%6d ", (int)tv.tv_sec, (int)tv.tv_usec); |
| 1338 | |
| 1339 | ntreceived++; |
| 1340 | |
| 1341 | if (len < sizeof(struct iphdr)) { |
| 1342 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Packet len too small to contain ip header (%d)", len); |
| 1343 | return -1; |
| 1344 | } |
| 1345 | switch(iph->version) { |
| 1346 | case 4: |
| 1347 | return encaps_ping4(pdp, pack, len); |
| 1348 | case 6: |
| 1349 | return encaps_ping6(pdp, (struct ip6_hdr *)pack, len); |
| 1350 | default: |
| 1351 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Unknown ip header version %d", iph->version); |
| 1352 | return -1; |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | static int create_ping4(void *gsn, struct pdp_t *pdp, struct in46_addr *src, |
| 1357 | struct in46_addr *dst, int seq, unsigned int datasize) |
| 1358 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1359 | struct ip_ping pack; |
Pau Espin Pedrol | e479329 | 2019-07-29 18:03:04 +0200 | [diff] [blame] | 1360 | uint16_t v16; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1361 | uint8_t *p8 = (uint8_t *) & pack; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1362 | unsigned int n; |
| 1363 | long int sum = 0; |
| 1364 | int count = 0; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1365 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1366 | struct timezone tz; |
| 1367 | struct timeval *tp = |
| 1368 | (struct timeval *)&p8[CREATEPING_IP + CREATEPING_ICMP]; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1369 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1370 | pack.ipver = 0x45; |
| 1371 | pack.tos = 0x00; |
| 1372 | pack.length = htons(CREATEPING_IP + CREATEPING_ICMP + datasize); |
| 1373 | pack.fragid = 0x0000; |
| 1374 | pack.offset = 0x0040; |
| 1375 | pack.ttl = 0x40; |
| 1376 | pack.protocol = 0x01; |
| 1377 | pack.ipcheck = 0x0000; |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 1378 | pack.src = src->v4.s_addr; |
| 1379 | pack.dst = dst->v4.s_addr; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1380 | pack.type = 0x08; |
| 1381 | pack.code = 0x00; |
| 1382 | pack.checksum = 0x0000; |
| 1383 | pack.ident = 0x0000; |
| 1384 | pack.seq = htons(seq); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1385 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1386 | /* Generate ICMP payload */ |
Pau Espin Pedrol | e479329 | 2019-07-29 18:03:04 +0200 | [diff] [blame] | 1387 | p8 = (uint8_t *) &pack + CREATEPING_IP + CREATEPING_ICMP; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1388 | for (n = 0; n < (datasize); n++) |
| 1389 | p8[n] = n; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1390 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1391 | if (datasize >= sizeof(struct timeval)) |
| 1392 | gettimeofday(tp, &tz); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1393 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1394 | /* Calculate IP header checksum */ |
Pau Espin Pedrol | e479329 | 2019-07-29 18:03:04 +0200 | [diff] [blame] | 1395 | p8 = (uint8_t *) &pack; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1396 | count = CREATEPING_IP; |
| 1397 | sum = 0; |
| 1398 | while (count > 1) { |
Pau Espin Pedrol | e479329 | 2019-07-29 18:03:04 +0200 | [diff] [blame] | 1399 | memcpy(&v16, p8, 2); |
| 1400 | sum += v16; |
| 1401 | p8 += 2; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1402 | count -= 2; |
| 1403 | } |
| 1404 | while (sum >> 16) |
| 1405 | sum = (sum & 0xffff) + (sum >> 16); |
| 1406 | pack.ipcheck = ~sum; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1407 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1408 | /* Calculate ICMP checksum */ |
| 1409 | count = CREATEPING_ICMP + datasize; /* Length of ICMP message */ |
| 1410 | sum = 0; |
Pau Espin Pedrol | e479329 | 2019-07-29 18:03:04 +0200 | [diff] [blame] | 1411 | p8 = (uint8_t *) &pack; |
| 1412 | p8 += CREATEPING_IP; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1413 | while (count > 1) { |
Pau Espin Pedrol | e479329 | 2019-07-29 18:03:04 +0200 | [diff] [blame] | 1414 | memcpy(&v16, p8, 2); |
| 1415 | sum += v16; |
| 1416 | p8 += 2; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1417 | count -= 2; |
| 1418 | } |
| 1419 | if (count > 0) |
Pau Espin Pedrol | e479329 | 2019-07-29 18:03:04 +0200 | [diff] [blame] | 1420 | sum += *(unsigned char *)p8; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1421 | while (sum >> 16) |
| 1422 | sum = (sum & 0xffff) + (sum >> 16); |
| 1423 | pack.checksum = ~sum; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1424 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1425 | ntransmitted++; |
| 1426 | return gtp_data_req(gsn, pdp, &pack, 28 + datasize); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 1429 | static int create_ping6(void *gsn, struct pdp_t *pdp, struct in46_addr *src, |
| 1430 | struct in46_addr *dst, int seq, unsigned int datasize) |
| 1431 | { |
| 1432 | struct ip6_ping *pack; |
| 1433 | uint8_t *p8; |
| 1434 | unsigned int n; |
| 1435 | struct timezone tz; |
| 1436 | struct timeval *tp; |
| 1437 | |
| 1438 | struct msgb *msg = msgb_alloc_headroom(sizeof(struct ip6_ping) + 128,128, "ICMPv6 echo"); |
| 1439 | OSMO_ASSERT(msg); |
| 1440 | pack = (struct ip6_ping *) msgb_put(msg, sizeof(struct icmpv6_echo_hdr) + datasize); |
| 1441 | pack->hdr.hdr.type = 128; |
| 1442 | pack->hdr.hdr.code = 0; |
| 1443 | pack->hdr.hdr.csum = 0; /* updated below */ |
| 1444 | pack->hdr.ident = 0x0000; |
| 1445 | pack->hdr.seq = htons(seq); |
| 1446 | |
| 1447 | p8 = pack->data; |
| 1448 | for (n = 0; n < (datasize); n++) |
| 1449 | p8[n] = n; |
| 1450 | |
| 1451 | if (datasize >= sizeof(struct timeval)) { |
| 1452 | tp = (struct timeval *)pack->data; |
| 1453 | gettimeofday(tp, &tz); |
| 1454 | } |
| 1455 | |
| 1456 | pack->hdr.hdr.csum = icmpv6_prepend_ip6hdr(msg, &src->v6, &dst->v6); |
| 1457 | |
| 1458 | ntransmitted++; |
| 1459 | return gtp_data_req(gsn, pdp, msgb_data(msg), msgb_length(msg)); |
| 1460 | } |
| 1461 | |
| 1462 | /* Create a new ping packet and send it off to peer. */ |
| 1463 | static int create_ping(void *gsn, struct pdp_t *pdp, |
| 1464 | struct in46_addr *dst, int seq, unsigned int datasize) |
| 1465 | { |
| 1466 | int num_addr; |
| 1467 | struct in46_addr addr[2]; |
| 1468 | struct in46_addr *src; |
| 1469 | |
| 1470 | if (datasize > CREATEPING_MAX) { |
| 1471 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
| 1472 | "Ping size to large: %d!", datasize); |
| 1473 | return -1; |
| 1474 | } |
| 1475 | |
| 1476 | if ((num_addr = in46a_from_eua(&pdp->eua, addr)) < 1) { |
| 1477 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
| 1478 | "in46a_from_eua() failed! %d", num_addr); |
| 1479 | return -1; |
| 1480 | } |
| 1481 | if (dst->len == addr[0].len) { |
| 1482 | src = &addr[0]; |
| 1483 | } else if (num_addr > 1 && dst->len == addr[1].len) { |
| 1484 | src = &addr[1]; |
| 1485 | } else { |
| 1486 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
| 1487 | "Mismaching source and destination IP addr types (%d vs %d)", dst->len, addr[0].len); |
| 1488 | return -1; |
| 1489 | } |
| 1490 | if (in46a_is_v4(dst)) |
| 1491 | return create_ping4(gsn, pdp, src, dst, seq, datasize); |
| 1492 | else |
| 1493 | return create_ping6(gsn, pdp, src, dst, seq, datasize); |
| 1494 | } |
| 1495 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1496 | static int delete_context(struct pdp_t *pdp) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1497 | { |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 1498 | int rc; |
| 1499 | |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1500 | if (tun && options.ipdown) { |
| 1501 | #if defined(__linux__) |
| 1502 | sigset_t oldmask; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1503 | |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1504 | if ((options.netns)) { |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 1505 | if ((rc = switch_ns(netns, &oldmask)) < 0) { |
| 1506 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
| 1507 | "Failed to switch to netns %s: %s\n", |
| 1508 | options.netns, strerror(-rc)); |
| 1509 | } |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1510 | } |
| 1511 | #endif |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1512 | tun_runscript(tun, options.ipdown); |
| 1513 | |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1514 | #if defined(__linux__) |
| 1515 | if ((options.netns)) { |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 1516 | if ((rc = restore_ns(&oldmask)) < 0) { |
| 1517 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
| 1518 | "Failed to switch to original netns: %s\n", |
| 1519 | strerror(-rc)); |
| 1520 | } |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1521 | } |
| 1522 | #endif |
| 1523 | } |
| 1524 | |
Pau Espin Pedrol | dbeaa04 | 2018-02-12 19:11:28 +0100 | [diff] [blame] | 1525 | ipdel((struct iphash_t *)pdp->peer[0]); |
| 1526 | memset(pdp->peer[0], 0, sizeof(struct iphash_t)); /* To be sure */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1527 | |
| 1528 | if (1 == options.contexts) |
| 1529 | state = 5; /* Disconnected */ |
| 1530 | |
| 1531 | return 0; |
| 1532 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1533 | |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 1534 | /* Link-Local address prefix fe80::/64 */ |
| 1535 | static const uint8_t ll_prefix[] = { 0xfe,0x80, 0,0, 0,0, 0,0 }; |
| 1536 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1537 | /* Callback for receiving messages from tun */ |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1538 | static int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1539 | { |
| 1540 | struct iphash_t *ipm; |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 1541 | struct in46_addr src; |
Harald Welte | 63ebccd | 2017-08-02 21:10:09 +0200 | [diff] [blame] | 1542 | struct iphdr *iph = (struct iphdr *)pack; |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 1543 | struct ip6_hdr *ip6h = (struct ip6_hdr *)pack; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1544 | |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 1545 | if (iph->version == 4) { |
| 1546 | if (len < sizeof(*iph) || len < 4*iph->ihl) { |
| 1547 | printf("Dropping packet with too short IP header\n"); |
| 1548 | return 0; |
| 1549 | } |
| 1550 | src.len = 4; |
| 1551 | src.v4.s_addr = iph->saddr; |
| 1552 | } else if (iph->version == 6) { |
| 1553 | /* We only have a single entry in the hash table, and it consists of the link-local |
| 1554 | * address "fe80::prefix". So we need to make sure to convert non-link-local source |
| 1555 | * addresses to that format before looking up the hash table via ippool_getip() */ |
| 1556 | src.len = 16; |
| 1557 | if (!memcmp(ip6h->ip6_src.s6_addr, ll_prefix, sizeof(ll_prefix))) { |
| 1558 | /* is a link-local address, we can do the hash lookup 1:1 */ |
| 1559 | src.v6 = ip6h->ip6_src; |
| 1560 | } else { |
| 1561 | /* it is not a link-local address, so we must convert from the /64 prefix |
| 1562 | * to the link-local format that's used in the hash table */ |
| 1563 | memcpy(&src.v6.s6_addr[0], ll_prefix, sizeof(ll_prefix)); |
| 1564 | memcpy(&src.v6.s6_addr[sizeof(ll_prefix)], ip6h->ip6_src.s6_addr, 16-sizeof(ll_prefix)); |
| 1565 | } |
| 1566 | } else { |
| 1567 | printf("Dropping packet with invalid IP version %u\n", iph->version); |
| 1568 | return 0; |
| 1569 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1570 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1571 | if (ipget(&ipm, &src)) { |
Neels Hofmeyr | 041824d | 2015-10-19 13:26:39 +0200 | [diff] [blame] | 1572 | printf("Dropping packet from invalid source address: %s\n", |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 1573 | in46a_ntoa(&src)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1574 | return 0; |
| 1575 | } |
| 1576 | |
| 1577 | if (ipm->pdp) /* Check if a peer protocol is defined */ |
| 1578 | gtp_data_req(gsn, ipm->pdp, pack, len); |
| 1579 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1582 | static int create_pdp_conf(struct pdp_t *pdp, void *cbp, int cause) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1583 | { |
Pau Espin Pedrol | 28c6a32 | 2020-04-09 17:52:19 +0200 | [diff] [blame] | 1584 | int rc, i, num_addr; |
| 1585 | struct in46_addr addr[2]; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1586 | #if defined(__linux__) |
| 1587 | sigset_t oldmask; |
| 1588 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1589 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1590 | struct iphash_t *iph = (struct iphash_t *)cbp; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1591 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1592 | if (cause < 0) { |
| 1593 | printf("Create PDP Context Request timed out\n"); |
| 1594 | if (iph->pdp->version == 1) { |
| 1595 | printf("Retrying with version 0\n"); |
| 1596 | iph->pdp->version = 0; |
| 1597 | gtp_create_context_req(gsn, iph->pdp, iph); |
| 1598 | return 0; |
| 1599 | } else { |
| 1600 | state = 0; |
| 1601 | pdp_freepdp(iph->pdp); |
| 1602 | iph->pdp = NULL; |
| 1603 | return EOF; |
| 1604 | } |
| 1605 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1606 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1607 | if (cause != 128) { |
| 1608 | printf |
| 1609 | ("Received create PDP context response. Cause value: %d\n", |
| 1610 | cause); |
| 1611 | state = 0; |
| 1612 | pdp_freepdp(iph->pdp); |
| 1613 | iph->pdp = NULL; |
| 1614 | return EOF; /* Not what we expected */ |
| 1615 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1616 | |
Pau Espin Pedrol | 28c6a32 | 2020-04-09 17:52:19 +0200 | [diff] [blame] | 1617 | if ((num_addr = in46a_from_eua(&pdp->eua, addr)) < 1) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1618 | printf |
| 1619 | ("Received create PDP context response. Cause value: %d\n", |
| 1620 | cause); |
| 1621 | pdp_freepdp(iph->pdp); |
| 1622 | iph->pdp = NULL; |
| 1623 | state = 0; |
| 1624 | return EOF; /* Not a valid IP address */ |
| 1625 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1626 | |
Pau Espin Pedrol | 28c6a32 | 2020-04-09 17:52:19 +0200 | [diff] [blame] | 1627 | printf("Received create PDP context response.\n"); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1628 | |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1629 | #if defined(__linux__) |
| 1630 | if ((options.createif) && (options.netns)) { |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 1631 | if ((rc = switch_ns(netns, &oldmask)) < 0) { |
| 1632 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
| 1633 | "Failed to switch to netns %s: %s\n", |
| 1634 | options.netns, strerror(-rc)); |
| 1635 | } |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1636 | } |
| 1637 | #endif |
| 1638 | |
Pau Espin Pedrol | 28c6a32 | 2020-04-09 17:52:19 +0200 | [diff] [blame] | 1639 | for (i = 0; i < num_addr; i++) { |
| 1640 | printf("PDP ctx: received EUA with IP address: %s\n", in46a_ntoa(&addr[i])); |
| 1641 | |
| 1642 | switch (addr[i].len) { |
| 1643 | case 16: /* IPv6 */ |
| 1644 | /* we have to enable the kernel to perform stateless autoconfiguration, |
| 1645 | * i.e. send a router solicitation using the lover 64bits of the allocated |
| 1646 | * EUA as interface identifier, as per 3GPP TS 29.061 Section 11.2.1.3.2 */ |
| 1647 | memcpy(addr[i].v6.s6_addr, ll_prefix, sizeof(ll_prefix)); |
| 1648 | printf("Derived IPv6 link-local address: %s\n", in46a_ntoa(&addr[i])); |
| 1649 | break; |
| 1650 | case 4: /* IPv4 */ |
| 1651 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1652 | } |
Pau Espin Pedrol | 28c6a32 | 2020-04-09 17:52:19 +0200 | [diff] [blame] | 1653 | |
Pau Espin Pedrol | a1b3dee | 2020-04-15 14:39:10 +0200 | [diff] [blame] | 1654 | if ((options.createif) && (!options.netaddr.len)) { |
Pau Espin Pedrol | 28c6a32 | 2020-04-09 17:52:19 +0200 | [diff] [blame] | 1655 | size_t prefixlen = 32; |
| 1656 | if (addr[i].len == 16) |
| 1657 | prefixlen = 64; |
| 1658 | /* printf("Setting up interface and routing\n"); */ |
Pau Espin Pedrol | e5d7163 | 2020-04-15 14:43:50 +0200 | [diff] [blame] | 1659 | tun_addaddr(tun, &addr[i], NULL, prefixlen); |
Pau Espin Pedrol | 28c6a32 | 2020-04-09 17:52:19 +0200 | [diff] [blame] | 1660 | if (options.defaultroute) { |
Pau Espin Pedrol | 2a1cedd | 2020-04-15 15:21:58 +0200 | [diff] [blame] | 1661 | if (in46a_is_v4(&addr[i])) { |
| 1662 | struct in_addr rm; |
| 1663 | rm.s_addr = 0; |
Pau Espin Pedrol | e2b0961 | 2020-04-15 15:09:30 +0200 | [diff] [blame] | 1664 | if (netdev_addroute4(&rm, &addr[i].v4, &rm) < 0) { |
| 1665 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed adding default route to %s", in46a_ntoa(&addr[i])); |
| 1666 | } |
| 1667 | } /* else: route will be set up once we have a global link address (Router Advertisement) */ |
Pau Espin Pedrol | 28c6a32 | 2020-04-09 17:52:19 +0200 | [diff] [blame] | 1668 | } |
| 1669 | if (options.ipup) |
| 1670 | tun_runscript(tun, options.ipup); |
| 1671 | } |
| 1672 | |
| 1673 | ipset(iph, &addr[i]); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1674 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1675 | |
Harald Welte | 081f30c | 2017-10-10 09:36:35 +0800 | [diff] [blame] | 1676 | if (options.createif && options.pdp_type == PDP_EUA_TYPE_v6) { |
Pau Espin Pedrol | e2b0961 | 2020-04-15 15:09:30 +0200 | [diff] [blame] | 1677 | struct in6_addr *saddr6; |
| 1678 | struct msgb *msg; |
| 1679 | if (in46a_is_v6(&addr[0])) { |
| 1680 | saddr6 = &addr[0].v6; |
| 1681 | } else if (num_addr > 1 && in46a_is_v6(&addr[1])) { |
| 1682 | saddr6 = &addr[1].v6; |
| 1683 | } else { |
| 1684 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed to find IPv6 EUA on IPv6 APN"); |
| 1685 | return EOF; /* Not a valid IP address */ |
Harald Welte | 081f30c | 2017-10-10 09:36:35 +0800 | [diff] [blame] | 1686 | } |
Pau Espin Pedrol | e2b0961 | 2020-04-15 15:09:30 +0200 | [diff] [blame] | 1687 | SYS_ERR(DSGSN, LOGL_INFO, 0, "Sending ICMPv6 Router Soliciation to GGSN..."); |
| 1688 | msg = icmpv6_construct_rs(saddr6); |
| 1689 | gtp_data_req(gsn, iph->pdp, msgb_data(msg), msgb_length(msg)); |
| 1690 | msgb_free(msg); |
Harald Welte | 081f30c | 2017-10-10 09:36:35 +0800 | [diff] [blame] | 1691 | } |
| 1692 | |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1693 | #if defined(__linux__) |
| 1694 | if ((options.createif) && (options.netns)) { |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 1695 | if ((rc = restore_ns(&oldmask)) < 0) { |
| 1696 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed to switch to original netns: %s\n", |
| 1697 | strerror(-rc)); |
| 1698 | } |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1699 | } |
| 1700 | #endif |
| 1701 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1702 | |
| 1703 | state = 2; /* Connected */ |
| 1704 | |
| 1705 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1708 | static int delete_pdp_conf(struct pdp_t *pdp, int cause) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1709 | { |
| 1710 | printf("Received delete PDP context response. Cause value: %d\n", |
| 1711 | cause); |
Oliver Smith | 1cde2c1 | 2019-05-13 11:35:03 +0200 | [diff] [blame] | 1712 | if (pdp) |
| 1713 | pdp_freepdp(pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1714 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1717 | static int echo_conf(int recovery) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1718 | { |
jjako | 91aaf22 | 2003-10-22 10:09:32 +0000 | [diff] [blame] | 1719 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1720 | if (recovery < 0) { |
| 1721 | printf("Echo Request timed out\n"); |
| 1722 | if (echoversion == 1) { |
| 1723 | printf("Retrying with version 0\n"); |
| 1724 | echoversion = 0; |
| 1725 | gtp_echo_req(gsn, echoversion, NULL, &options.remote); |
| 1726 | return 0; |
| 1727 | } else { |
| 1728 | state = 0; |
| 1729 | return EOF; |
| 1730 | } |
| 1731 | } else { |
| 1732 | printf("Received echo response\n"); |
| 1733 | if (!options.contexts) |
| 1734 | state = 5; |
| 1735 | } |
| 1736 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Pau Espin Pedrol | 9366f4c | 2020-04-09 18:01:42 +0200 | [diff] [blame] | 1739 | static int _gtp_cb_conf(int type, int cause, struct pdp_t *pdp, void *cbp) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1740 | { |
| 1741 | /* if (cause < 0) return 0; Some error occurred. We don't care */ |
| 1742 | switch (type) { |
| 1743 | case GTP_ECHO_REQ: |
| 1744 | return echo_conf(cause); |
| 1745 | case GTP_CREATE_PDP_REQ: |
| 1746 | return create_pdp_conf(pdp, cbp, cause); |
| 1747 | case GTP_DELETE_PDP_REQ: |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1748 | return delete_pdp_conf(pdp, cause); |
| 1749 | default: |
| 1750 | return 0; |
| 1751 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Pau Espin Pedrol | e2b0961 | 2020-04-15 15:09:30 +0200 | [diff] [blame] | 1754 | static void handle_router_adv(struct ip6_hdr *ip6h, struct icmpv6_radv_hdr *ra, size_t ra_len) |
| 1755 | { |
| 1756 | struct icmpv6_opt_hdr *opt_hdr; |
| 1757 | struct icmpv6_opt_prefix *opt_prefix; |
| 1758 | int rc; |
| 1759 | sigset_t oldmask; |
| 1760 | struct in6_addr rm; |
| 1761 | char ip6strbuf[200]; |
| 1762 | memset(&rm, 0, sizeof(rm)); |
| 1763 | |
| 1764 | SYS_ERR(DSGSN, LOGL_INFO, 0, "Received ICMPv6 Router Advertisement"); |
| 1765 | |
| 1766 | foreach_icmpv6_opt(ra, ra_len, opt_hdr) { |
| 1767 | if (opt_hdr->type == ICMPv6_OPT_TYPE_PREFIX_INFO) { |
| 1768 | opt_prefix = (struct icmpv6_opt_prefix *)opt_hdr; |
| 1769 | size_t prefix_len_bytes = (opt_prefix->prefix_len + 7)/8; |
| 1770 | SYS_ERR(DSGSN, LOGL_INFO, 0, "Parsing OPT Prefix info (prefix_len=%u): %s", |
| 1771 | opt_prefix->prefix_len, |
| 1772 | osmo_hexdump((const unsigned char *)opt_prefix->prefix, prefix_len_bytes)); |
| 1773 | if ((options.createif) && (!options.netaddr.len)) { |
| 1774 | struct in46_addr addr; |
| 1775 | addr.len = 16; |
| 1776 | memcpy(addr.v6.s6_addr, opt_prefix->prefix, prefix_len_bytes); |
| 1777 | memset(&addr.v6.s6_addr[prefix_len_bytes], 0, 16 - prefix_len_bytes); |
| 1778 | addr.v6.s6_addr[15] = 0x02; |
| 1779 | SYS_ERR(DSGSN, LOGL_INFO, 0, "Adding addr %s to tun %s", |
| 1780 | in46a_ntoa(&addr), tun->devname); |
| 1781 | |
| 1782 | #if defined(__linux__) |
| 1783 | if ((options.netns)) { |
| 1784 | if ((rc = switch_ns(netns, &oldmask)) < 0) { |
| 1785 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
| 1786 | "Failed to switch to netns %s: %s", |
| 1787 | options.netns, strerror(-rc)); |
| 1788 | } |
| 1789 | } |
| 1790 | #endif |
| 1791 | rc = tun_addaddr(tun, &addr, NULL, opt_prefix->prefix_len); |
| 1792 | if (rc < 0) { |
| 1793 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed to add addr %s to tun %s", |
| 1794 | in46a_ntoa(&addr), tun->devname); |
| 1795 | } |
| 1796 | |
| 1797 | struct in6_addr rm; |
| 1798 | memset(&rm, 0, sizeof(rm)); |
| 1799 | if (netdev_addroute6(&rm, &ip6h->ip6_src, 0, tun->devname) < 0) { |
| 1800 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed adding default route to %s", inet_ntop(AF_INET6, &ip6h->ip6_src, ip6strbuf, sizeof(ip6strbuf))); |
| 1801 | } |
| 1802 | |
| 1803 | #if defined(__linux__) |
| 1804 | if ((options.netns)) { |
| 1805 | if ((rc = restore_ns(&oldmask)) < 0) { |
| 1806 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
| 1807 | "Failed to switch to original netns: %s", |
| 1808 | strerror(-rc)); |
| 1809 | } |
| 1810 | } |
| 1811 | #endif |
| 1812 | } |
| 1813 | } |
| 1814 | } |
| 1815 | } |
| 1816 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1817 | static int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1818 | { |
Pau Espin Pedrol | e2b0961 | 2020-04-15 15:09:30 +0200 | [diff] [blame] | 1819 | struct iphdr *iph = (struct iphdr *)pack; |
| 1820 | struct icmpv6_radv_hdr *ra; |
| 1821 | switch (iph->version) { |
| 1822 | case 6: |
| 1823 | if ((ra = icmpv6_validate_router_adv(pack, len))) { |
| 1824 | size_t ra_len = (uint8_t*)ra - (uint8_t*)pack; |
| 1825 | handle_router_adv((struct ip6_hdr *)pack, ra, ra_len); |
| 1826 | return 0; |
| 1827 | } |
| 1828 | break; |
| 1829 | } |
| 1830 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1831 | /* printf("encaps_tun. Packet received: forwarding to tun\n"); */ |
| 1832 | return tun_encaps((struct tun_t *)pdp->ipif, pack, len); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | int main(int argc, char **argv) |
| 1836 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1837 | fd_set fds; /* For select() */ |
| 1838 | struct timeval idleTime; /* How long to select() */ |
| 1839 | struct pdp_t *pdp; |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 1840 | int n, rc; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1841 | int starttime = time(NULL); /* Time program was started */ |
| 1842 | int stoptime = 0; /* Time to exit */ |
| 1843 | int pingtimeout = 0; /* Time to print ping statistics */ |
bjovan | a8f71eb | 2017-02-24 17:39:20 +0100 | [diff] [blame] | 1844 | int signal_received; /* If select() on fd_set is interrupted by signal. */ |
jjako | afb2a97 | 2003-01-29 21:04:13 +0000 | [diff] [blame] | 1845 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1846 | struct timezone tz; /* Used for calculating ping times */ |
| 1847 | struct timeval tv; |
| 1848 | int diff; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1849 | #if defined(__linux__) |
Pau Espin Pedrol | 98f8126 | 2020-04-14 18:38:52 +0200 | [diff] [blame] | 1850 | char buf[10]; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1851 | sigset_t oldmask; |
| 1852 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1853 | |
bjovan | a8f71eb | 2017-02-24 17:39:20 +0100 | [diff] [blame] | 1854 | signal(SIGTERM, signal_handler); |
| 1855 | signal(SIGHUP, signal_handler); |
| 1856 | signal(SIGINT, signal_handler); |
| 1857 | |
Pau Espin Pedrol | 042a445 | 2018-04-17 14:31:42 +0200 | [diff] [blame] | 1858 | tall_sgsnemu_ctx = talloc_named_const(NULL, 0, "sgsnemu"); |
| 1859 | msgb_talloc_ctx_init(tall_sgsnemu_ctx, 0); |
| 1860 | osmo_init_logging2(tall_sgsnemu_ctx, &log_info); |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 1861 | |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1862 | #if defined(__linux__) |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 1863 | if ((rc = init_netns()) < 0) { |
| 1864 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed to initialize netns: %s", strerror(-rc)); |
| 1865 | exit(1); |
| 1866 | } |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1867 | #endif |
| 1868 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1869 | /* Process options given in configuration file and command line */ |
| 1870 | if (process_options(argc, argv)) |
| 1871 | exit(1); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1872 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1873 | printf("\nInitialising GTP library\n"); |
| 1874 | if (gtp_new(&gsn, options.statedir, &options.listen, GTP_MODE_SGSN)) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 1875 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed to create gtp"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1876 | exit(1); |
| 1877 | } |
| 1878 | if (gsn->fd0 > maxfd) |
| 1879 | maxfd = gsn->fd0; |
| 1880 | if (gsn->fd1c > maxfd) |
| 1881 | maxfd = gsn->fd1c; |
| 1882 | if (gsn->fd1u > maxfd) |
| 1883 | maxfd = gsn->fd1u; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1884 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1885 | gtp_set_cb_delete_context(gsn, delete_context); |
Pau Espin Pedrol | 9366f4c | 2020-04-09 18:01:42 +0200 | [diff] [blame] | 1886 | gtp_set_cb_conf(gsn, _gtp_cb_conf); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1887 | if (options.createif) |
| 1888 | gtp_set_cb_data_ind(gsn, encaps_tun); |
| 1889 | else |
| 1890 | gtp_set_cb_data_ind(gsn, encaps_ping); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1891 | |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1892 | #if defined(__linux__) |
| 1893 | if ((options.createif) && (options.netns)) { |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 1894 | if ((netns = get_nsfd(options.netns)) < 0) { |
| 1895 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed to obtain fd for netns %s: %s\n", |
| 1896 | options.netns, strerror(-netns)); |
| 1897 | exit(1); |
| 1898 | } |
| 1899 | if ((rc = switch_ns(netns, &oldmask)) < 0) { |
| 1900 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed to switch to netns %s: %s\n", |
| 1901 | options.netns, strerror(-rc)); |
| 1902 | exit(1); |
| 1903 | } |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1904 | } |
| 1905 | #endif |
| 1906 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1907 | if (options.createif) { |
| 1908 | printf("Setting up interface\n"); |
| 1909 | /* Create a tunnel interface */ |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 1910 | if (tun_new((struct tun_t **)&tun, options.tun_dev_name, false, -1, -1)) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 1911 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1912 | "Failed to create tun"); |
| 1913 | exit(1); |
| 1914 | } |
Pau Espin Pedrol | 98f8126 | 2020-04-14 18:38:52 +0200 | [diff] [blame] | 1915 | |
Pau Espin Pedrol | ff2ebee | 2020-04-18 23:47:06 +0200 | [diff] [blame] | 1916 | #if defined(__linux__) && defined(HAVE_IN6_ADDR_GEN_MODE_NONE) |
| 1917 | /* Avoid tunnel setting its own link-local addr automatically, |
| 1918 | we don't need it. Don't exit on error since this sysctl is |
| 1919 | only available starting with linux 4.11. */ |
Pau Espin Pedrol | 98f8126 | 2020-04-14 18:38:52 +0200 | [diff] [blame] | 1920 | snprintf(buf, sizeof(buf), "%u", IN6_ADDR_GEN_MODE_NONE); |
| 1921 | if (proc_ipv6_conf_write(options.tun_dev_name, "addr_gen_mode", buf) < 0) { |
| 1922 | SYS_ERR(DSGSN, LOGL_ERROR, errno, |
Pau Espin Pedrol | ff2ebee | 2020-04-18 23:47:06 +0200 | [diff] [blame] | 1923 | "Failed to disable addr_gen_mode on %s, an extra link-local " |
| 1924 | "ip address will appear on the tun device.\n", |
| 1925 | options.tun_dev_name); |
Pau Espin Pedrol | 98f8126 | 2020-04-14 18:38:52 +0200 | [diff] [blame] | 1926 | } |
| 1927 | #endif |
| 1928 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1929 | tun_set_cb_ind(tun, cb_tun_ind); |
| 1930 | if (tun->fd > maxfd) |
| 1931 | maxfd = tun->fd; |
Pau Espin Pedrol | e2b0961 | 2020-04-15 15:09:30 +0200 | [diff] [blame] | 1932 | |
| 1933 | if (proc_ipv6_conf_write(options.tun_dev_name, "accept_ra", "0") < 0) { |
| 1934 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
| 1935 | "Failed to disable IPv6 SLAAC on %s\n", options.tun_dev_name); |
| 1936 | exit(1); |
| 1937 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1938 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1939 | |
Pau Espin Pedrol | a1b3dee | 2020-04-15 14:39:10 +0200 | [diff] [blame] | 1940 | if ((options.createif) && (options.netaddr.len)) { |
Pau Espin Pedrol | 964f08a | 2020-04-15 14:29:25 +0200 | [diff] [blame] | 1941 | tun_addaddr(tun, &options.netaddr, NULL, options.prefixlen); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1942 | if (options.defaultroute) { |
Pau Espin Pedrol | 2a1cedd | 2020-04-15 15:21:58 +0200 | [diff] [blame] | 1943 | if (in46a_is_v4(&options.netaddr)) { |
| 1944 | struct in_addr rm; |
| 1945 | rm.s_addr = 0; |
| 1946 | netdev_addroute4(&rm, &options.netaddr.v4, &rm); |
Pau Espin Pedrol | e2b0961 | 2020-04-15 15:09:30 +0200 | [diff] [blame] | 1947 | } else { |
| 1948 | struct in6_addr rm; |
| 1949 | memset(&rm, 0, sizeof(rm)); |
| 1950 | netdev_addroute6(&rm, &options.netaddr.v6, 0, tun->devname); |
Pau Espin Pedrol | 2a1cedd | 2020-04-15 15:21:58 +0200 | [diff] [blame] | 1951 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1952 | } |
| 1953 | if (options.ipup) |
| 1954 | tun_runscript(tun, options.ipup); |
| 1955 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1956 | |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1957 | #if defined(__linux__) |
| 1958 | if ((options.createif) && (options.netns)) { |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 1959 | if ((rc = restore_ns(&oldmask)) < 0) { |
| 1960 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed to switch to original netns: %s\n", |
| 1961 | strerror(-rc)); |
| 1962 | exit(1); |
| 1963 | } |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1964 | } |
| 1965 | #endif |
| 1966 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1967 | /* Initialise hash tables */ |
| 1968 | memset(&iphash, 0, sizeof(iphash)); |
| 1969 | memset(&iparr, 0, sizeof(iparr)); |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1970 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1971 | printf("Done initialising GTP library\n\n"); |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1972 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1973 | /* See if anybody is there */ |
| 1974 | printf("Sending off echo request\n"); |
| 1975 | echoversion = options.gtpversion; |
| 1976 | gtp_echo_req(gsn, echoversion, NULL, &options.remote); /* Is remote alive? */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1977 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1978 | for (n = 0; n < options.contexts; n++) { |
| 1979 | uint64_t myimsi; |
| 1980 | printf("Setting up PDP context #%d\n", n); |
| 1981 | iparr[n].inuse = 1; /* TODO */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1982 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1983 | imsi_add(options.imsi, &myimsi, n); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1984 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1985 | /* Allocated here. */ |
| 1986 | /* If create context failes we have to deallocate ourselves. */ |
| 1987 | /* Otherwise it is deallocated by gtplib */ |
Pau Espin Pedrol | d1a2ddf | 2019-05-31 16:17:27 +0200 | [diff] [blame] | 1988 | gtp_pdp_newpdp(gsn, &pdp, myimsi, options.nsapi, NULL); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1989 | |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 1990 | pdp->peer[0] = &iparr[n]; /* FIXME: support v4v6, have 2 peers */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1991 | pdp->ipif = tun; /* TODO */ |
| 1992 | iparr[n].pdp = pdp; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1993 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1994 | if (options.gtpversion == 0) { |
| 1995 | if (options.qos.l - 1 > sizeof(pdp->qos_req0)) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 1996 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1997 | "QoS length too big"); |
| 1998 | exit(1); |
| 1999 | } else { |
| 2000 | memcpy(pdp->qos_req0, options.qos.v, |
| 2001 | options.qos.l); |
| 2002 | } |
| 2003 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2004 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2005 | pdp->qos_req.l = options.qos.l; |
| 2006 | memcpy(pdp->qos_req.v, options.qos.v, options.qos.l); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2007 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2008 | pdp->selmode = options.selmode; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2009 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2010 | pdp->rattype.l = options.rattype.l; |
| 2011 | memcpy(pdp->rattype.v, options.rattype.v, options.rattype.l); |
| 2012 | pdp->rattype_given = options.rattype_given; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 2013 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2014 | pdp->userloc.l = options.userloc.l; |
| 2015 | memcpy(pdp->userloc.v, options.userloc.v, options.userloc.l); |
| 2016 | pdp->userloc_given = options.userloc_given; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 2017 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2018 | pdp->rai.l = options.rai.l; |
| 2019 | memcpy(pdp->rai.v, options.rai.v, options.rai.l); |
| 2020 | pdp->rai_given = options.rai_given; |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 2021 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2022 | pdp->mstz.l = options.mstz.l; |
| 2023 | memcpy(pdp->mstz.v, options.mstz.v, options.mstz.l); |
| 2024 | pdp->mstz_given = options.mstz_given; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 2025 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2026 | pdp->imeisv.l = options.imeisv.l; |
| 2027 | memcpy(pdp->imeisv.v, options.imeisv.v, options.imeisv.l); |
| 2028 | pdp->imeisv_given = options.imeisv_given; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 2029 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2030 | pdp->norecovery_given = options.norecovery_given; |
Harald Welte | 3a4c67b | 2011-10-07 18:45:54 +0200 | [diff] [blame] | 2031 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2032 | if (options.apn.l > sizeof(pdp->apn_use.v)) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 2033 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2034 | "APN length too big"); |
| 2035 | exit(1); |
| 2036 | } else { |
| 2037 | pdp->apn_use.l = options.apn.l; |
| 2038 | memcpy(pdp->apn_use.v, options.apn.v, options.apn.l); |
| 2039 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 2040 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2041 | pdp->gsnlc.l = sizeof(options.listen); |
| 2042 | memcpy(pdp->gsnlc.v, &options.listen, sizeof(options.listen)); |
| 2043 | pdp->gsnlu.l = sizeof(options.listen); |
| 2044 | memcpy(pdp->gsnlu.v, &options.listen, sizeof(options.listen)); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2045 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2046 | if (options.msisdn.l > sizeof(pdp->msisdn.v)) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 2047 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2048 | "MSISDN length too big"); |
| 2049 | exit(1); |
| 2050 | } else { |
| 2051 | msisdn_add(&options.msisdn, &pdp->msisdn, n); |
| 2052 | } |
jjako | b62c3dd | 2004-05-27 18:51:55 +0000 | [diff] [blame] | 2053 | |
Harald Welte | 840a8e9 | 2017-09-24 18:12:40 +0800 | [diff] [blame] | 2054 | /* Request dynamic IP address */ |
| 2055 | pdp->eua.v[0] = PDP_EUA_ORG_IETF; |
| 2056 | pdp->eua.v[1] = options.pdp_type; |
| 2057 | pdp->eua.l = 2; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2058 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2059 | if (options.pco.l > sizeof(pdp->pco_req.v)) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 2060 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2061 | "PCO length too big"); |
| 2062 | exit(1); |
| 2063 | } else { |
| 2064 | pdp->pco_req.l = options.pco.l; |
| 2065 | memcpy(pdp->pco_req.v, options.pco.v, options.pco.l); |
| 2066 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2067 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2068 | pdp->version = options.gtpversion; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2069 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2070 | pdp->hisaddr0 = options.remote; |
| 2071 | pdp->hisaddr1 = options.remote; |
| 2072 | |
Pau Espin Pedrol | 7c4de07 | 2017-12-14 13:59:02 +0100 | [diff] [blame] | 2073 | pdp->cch_pdp = options.cch; /* 2048 = Normal, 1024 = Prepaid, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2074 | 512 = Flat rate, 256 = Hot billing */ |
| 2075 | |
Harald Welte | fbb9c7f | 2017-09-24 11:50:20 +0800 | [diff] [blame] | 2076 | pdp->tx_gpdu_seq = options.tx_gpdu_seq; |
| 2077 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2078 | /* Create context */ |
| 2079 | /* We send this of once. Retransmissions are handled by gtplib */ |
| 2080 | gtp_create_context_req(gsn, pdp, &iparr[n]); |
| 2081 | } |
| 2082 | |
| 2083 | state = 1; /* Enter wait_connection state */ |
| 2084 | |
| 2085 | printf("Waiting for response from ggsn........\n\n"); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 2086 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2087 | /******************************************************************/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2088 | /* Main select loop */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2089 | /******************************************************************/ |
| 2090 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2091 | while ((0 != state) && (5 != state)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2092 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2093 | /* Take down client after timeout after disconnect */ |
| 2094 | if ((4 == state) && ((stoptime) <= time(NULL))) { |
| 2095 | state = 5; |
| 2096 | } |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 2097 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2098 | /* Take down client after timelimit timeout */ |
| 2099 | if ((2 == state) && (options.timelimit) && |
| 2100 | ((starttime + options.timelimit) <= time(NULL))) { |
| 2101 | state = 3; |
| 2102 | } |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 2103 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2104 | /* Take down client after ping timeout */ |
| 2105 | if ((2 == state) && (pingtimeout) |
| 2106 | && (pingtimeout <= time(NULL))) { |
| 2107 | state = 3; |
| 2108 | } |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 2109 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2110 | /* Set pingtimeout for later disconnection */ |
| 2111 | if (options.pingcount && ntransmitted >= options.pingcount) { |
| 2112 | pingtimeout = time(NULL) + 5; /* Extra seconds */ |
| 2113 | } |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 2114 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2115 | /* Print statistics if no more ping packets are missing */ |
| 2116 | if (ntransmitted && options.pingcount |
| 2117 | && nreceived >= options.pingcount) { |
| 2118 | ping_finish(); |
| 2119 | if (!options.createif) |
| 2120 | state = 3; |
| 2121 | } |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 2122 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2123 | /* Send off disconnect */ |
| 2124 | if (3 == state) { |
| 2125 | state = 4; |
| 2126 | stoptime = time(NULL) + 5; /* Extra seconds to allow disconnect */ |
| 2127 | for (n = 0; n < options.contexts; n++) { |
| 2128 | /* Delete context */ |
| 2129 | printf("Disconnecting PDP context #%d\n", n); |
Oliver Smith | 1cde2c1 | 2019-05-13 11:35:03 +0200 | [diff] [blame] | 2130 | gtp_delete_context_req2(gsn, iparr[n].pdp, NULL, 1); |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 2131 | if ((options.pinghost.len) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2132 | && ntransmitted) |
| 2133 | ping_finish(); |
| 2134 | } |
| 2135 | } |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 2136 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2137 | /* Send of ping packets */ |
| 2138 | diff = 0; |
| 2139 | while ((diff <= 0) && |
| 2140 | /* Send off an ICMP ping packet */ |
Pau Espin Pedrol | 9621460 | 2020-04-14 19:39:09 +0200 | [diff] [blame^] | 2141 | /*if ( */ (options.pinghost.len) && (2 == state) && |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2142 | ((pingseq < options.pingcount) |
| 2143 | || (options.pingcount == 0))) { |
| 2144 | if (!pingseq) |
| 2145 | gettimeofday(&firstping, &tz); /* Set time of first ping */ |
| 2146 | gettimeofday(&tv, &tz); |
| 2147 | diff = 1000000 / options.pingrate * pingseq - 1000000 * (tv.tv_sec - firstping.tv_sec) - (tv.tv_usec - firstping.tv_usec); /* Microseconds safe up to 500 sec */ |
| 2148 | if (diff <= 0) { |
| 2149 | if (options.debug) |
| 2150 | printf("Create_ping %d\n", diff); |
| 2151 | create_ping(gsn, |
| 2152 | iparr[pingseq % |
| 2153 | options.contexts].pdp, |
| 2154 | &options.pinghost, pingseq, |
| 2155 | options.pingsize); |
| 2156 | pingseq++; |
| 2157 | } |
| 2158 | } |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 2159 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2160 | FD_ZERO(&fds); |
| 2161 | if (tun) |
| 2162 | FD_SET(tun->fd, &fds); |
| 2163 | FD_SET(gsn->fd0, &fds); |
| 2164 | FD_SET(gsn->fd1c, &fds); |
| 2165 | FD_SET(gsn->fd1u, &fds); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2166 | |
Pau Espin Pedrol | 1bf41e4 | 2019-08-28 19:44:54 +0200 | [diff] [blame] | 2167 | idleTime.tv_sec = 10; |
| 2168 | idleTime.tv_usec = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2169 | ping_timeout(&idleTime); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2170 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2171 | if (options.debug) |
| 2172 | printf("idletime.tv_sec %d, idleTime.tv_usec %d\n", |
| 2173 | (int)idleTime.tv_sec, (int)idleTime.tv_usec); |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 2174 | |
bjovan | a8f71eb | 2017-02-24 17:39:20 +0100 | [diff] [blame] | 2175 | signal_received = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2176 | switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) { |
| 2177 | case -1: |
bjovan | a8f71eb | 2017-02-24 17:39:20 +0100 | [diff] [blame] | 2178 | if (errno == EINTR) |
| 2179 | signal_received = 1; |
| 2180 | else |
| 2181 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
| 2182 | "Select returned -1"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2183 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2184 | default: |
| 2185 | break; |
| 2186 | } |
| 2187 | |
bjovan | a8f71eb | 2017-02-24 17:39:20 +0100 | [diff] [blame] | 2188 | if (!signal_received) { |
| 2189 | |
| 2190 | if ((tun) && FD_ISSET(tun->fd, &fds) && tun_decaps(tun) < 0) { |
| 2191 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
| 2192 | "TUN decaps failed"); |
| 2193 | } |
| 2194 | |
| 2195 | if (FD_ISSET(gsn->fd0, &fds)) |
| 2196 | gtp_decaps0(gsn); |
| 2197 | |
| 2198 | if (FD_ISSET(gsn->fd1c, &fds)) |
| 2199 | gtp_decaps1c(gsn); |
| 2200 | |
| 2201 | if (FD_ISSET(gsn->fd1u, &fds)) |
| 2202 | gtp_decaps1u(gsn); |
| 2203 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2204 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | gtp_free(gsn); /* Clean up the gsn instance */ |
| 2208 | |
| 2209 | if (options.createif) |
| 2210 | tun_free(tun); |
| 2211 | |
| 2212 | if (0 == state) |
| 2213 | exit(1); /* Indicate error */ |
| 2214 | |
| 2215 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2216 | } |