jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1 | /* |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2 | * OpenGGSN - Gateway GPRS Support Node |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 3 | * Copyright (C) 2002, 2003, 2004 Mondru AB. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 4 | * |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 5 | * The contents of this file may be used under the terms of the GNU |
| 6 | * General Public License Version 2, provided that the above copyright |
| 7 | * notice and this permission notice is included in all copies or |
| 8 | * substantial portions of the software. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 9 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | /* ggsn.c |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #ifdef __linux__ |
| 17 | #define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */ |
| 18 | #endif |
| 19 | |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 20 | #include "../config.h" |
| 21 | |
| 22 | #ifdef HAVE_STDINT_H |
| 23 | #include <stdint.h> |
| 24 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 25 | |
| 26 | #include <syslog.h> |
| 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> |
| 36 | #include <arpa/inet.h> |
| 37 | #include <sys/wait.h> |
| 38 | #include <sys/stat.h> |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 39 | #include <fcntl.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 40 | #include <unistd.h> |
| 41 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 42 | #include <sys/socket.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 43 | #include <sys/ioctl.h> |
| 44 | #include <net/if.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 45 | |
| 46 | #include <errno.h> |
| 47 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 48 | #include <time.h> |
| 49 | |
Emmanuel Bretelle | 2a10368 | 2010-09-07 17:01:20 +0200 | [diff] [blame] | 50 | #include "../lib/tun.h" |
| 51 | #include "../lib/ippool.h" |
| 52 | #include "../lib/syserr.h" |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 53 | #include "../gtp/pdp.h" |
| 54 | #include "../gtp/gtp.h" |
| 55 | #include "cmdline.h" |
| 56 | |
Harald Welte | c3dcba0 | 2010-05-04 11:02:54 +0200 | [diff] [blame] | 57 | int end = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 58 | int maxfd = 0; /* For select() */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 59 | |
| 60 | struct in_addr listen_; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 61 | struct in_addr netaddr, destaddr, net, mask; /* Network interface */ |
| 62 | struct in_addr dns1, dns2; /* PCO DNS address */ |
| 63 | char *ipup, *ipdown; /* Filename of scripts */ |
| 64 | int debug; /* Print debug output */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 65 | struct ul255_t pco; |
| 66 | struct ul255_t qos; |
| 67 | struct ul255_t apn; |
| 68 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 69 | struct gsn_t *gsn; /* GSN instance */ |
| 70 | struct tun_t *tun; /* TUN instance */ |
| 71 | struct ippool_t *ippool; /* Pool of IP addresses */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 72 | |
Harald Welte | c3dcba0 | 2010-05-04 11:02:54 +0200 | [diff] [blame] | 73 | /* To exit gracefully. Used with GCC compilation flag -pg and gprof */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 74 | void signal_handler(int s) |
| 75 | { |
| 76 | if (debug) |
| 77 | printf("Received signal %d, exiting.\n", s); |
| 78 | end = 1; |
Harald Welte | c3dcba0 | 2010-05-04 11:02:54 +0200 | [diff] [blame] | 79 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 80 | |
| 81 | /* Used to write process ID to file. Assume someone else will delete */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 82 | void log_pid(char *pidfile) |
| 83 | { |
| 84 | FILE *file; |
| 85 | mode_t oldmask; |
| 86 | |
| 87 | oldmask = umask(022); |
| 88 | file = fopen(pidfile, "w"); |
| 89 | umask(oldmask); |
| 90 | if (!file) { |
| 91 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 92 | "Failed to create process ID file: %s!", pidfile); |
| 93 | return; |
| 94 | } |
| 95 | fprintf(file, "%d\n", (int)getpid()); |
| 96 | fclose(file); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 97 | } |
| 98 | |
jjako | bd937b7 | 2004-12-30 16:22:42 +0000 | [diff] [blame] | 99 | #if defined(__sun__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 100 | int daemon(int nochdir, int noclose) |
| 101 | { |
| 102 | int fd; |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 103 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 104 | switch (fork()) { |
| 105 | case -1: |
| 106 | return (-1); |
| 107 | case 0: |
| 108 | break; |
| 109 | default: |
| 110 | _exit(0); |
| 111 | } |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 112 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 113 | if (setsid() == -1) |
| 114 | return (-1); |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 115 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 116 | if (!nochdir) |
| 117 | chdir("/"); |
| 118 | |
| 119 | if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) { |
| 120 | dup2(fd, STDIN_FILENO); |
| 121 | dup2(fd, STDOUT_FILENO); |
| 122 | dup2(fd, STDERR_FILENO); |
| 123 | if (fd > 2) |
| 124 | close(fd); |
| 125 | } |
| 126 | return (0); |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 127 | } |
| 128 | #endif |
| 129 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 130 | int encaps_printf(void *p, void *packet, unsigned len) |
| 131 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 132 | unsigned int i; |
| 133 | if (debug) { |
| 134 | printf("The packet looks like this:\n"); |
| 135 | for (i = 0; i < len; i++) { |
| 136 | printf("%02x ", (unsigned char)*(char *)(packet + i)); |
| 137 | if (!((i + 1) % 16)) |
| 138 | printf("\n"); |
| 139 | }; |
| 140 | printf("\n"); |
| 141 | } |
| 142 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 145 | int delete_context(struct pdp_t *pdp) |
| 146 | { |
| 147 | if (debug) |
| 148 | printf("Deleting PDP context\n"); |
| 149 | if (pdp->peer) |
| 150 | ippool_freeip(ippool, (struct ippoolm_t *)pdp->peer); |
| 151 | else |
| 152 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Peer not defined!"); |
| 153 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 156 | int create_context_ind(struct pdp_t *pdp) |
| 157 | { |
| 158 | struct in_addr addr; |
| 159 | struct ippoolm_t *member; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 160 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 161 | if (debug) |
| 162 | printf("Received create PDP context request\n"); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 163 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 164 | pdp->eua.l = 0; /* TODO: Indicates dynamic IP */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 165 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 166 | /* ulcpy(&pdp->qos_neg, &pdp->qos_req, sizeof(pdp->qos_req.v)); */ |
| 167 | memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_req0)); |
| 168 | memcpy(&pdp->pco_neg, &pco, sizeof(pdp->pco_neg)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 169 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 170 | memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */ |
| 171 | pdp->qos_neg.l = pdp->qos_req.l; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 172 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 173 | if (pdp_euaton(&pdp->eua, &addr)) { |
| 174 | addr.s_addr = 0; /* Request dynamic */ |
| 175 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 176 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 177 | if (ippool_newip(ippool, &member, &addr, 0)) { |
| 178 | gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES); |
| 179 | return 0; /* Allready in use, or no more available */ |
| 180 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 181 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 182 | pdp_ntoeua(&member->addr, &pdp->eua); |
| 183 | pdp->peer = member; |
| 184 | pdp->ipif = tun; /* TODO */ |
| 185 | member->peer = pdp; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 186 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 187 | gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ); |
| 188 | return 0; /* Success */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 189 | } |
| 190 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 191 | /* Callback for receiving messages from tun */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 192 | int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) |
| 193 | { |
| 194 | struct ippoolm_t *ipm; |
| 195 | struct in_addr dst; |
| 196 | struct tun_packet_t *iph = (struct tun_packet_t *)pack; |
jjako | c6762cf | 2004-04-28 14:52:58 +0000 | [diff] [blame] | 197 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 198 | dst.s_addr = iph->dst; |
jjako | c6762cf | 2004-04-28 14:52:58 +0000 | [diff] [blame] | 199 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 200 | if (debug) |
| 201 | printf("Received packet from tun!\n"); |
| 202 | |
| 203 | if (ippool_getip(ippool, &ipm, &dst)) { |
| 204 | if (debug) |
| 205 | printf("Received packet with no destination!!!\n"); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | if (ipm->peer) /* Check if a peer protocol is defined */ |
| 210 | gtp_data_req(gsn, (struct pdp_t *)ipm->peer, pack, len); |
| 211 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 214 | int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) |
| 215 | { |
| 216 | if (debug) |
| 217 | printf("encaps_tun. Packet received: forwarding to tun\n"); |
| 218 | return tun_encaps((struct tun_t *)pdp->ipif, pack, len); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 219 | } |
| 220 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 221 | int main(int argc, char **argv) |
| 222 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 223 | /* gengeopt declarations */ |
| 224 | struct gengetopt_args_info args_info; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 225 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 226 | struct hostent *host; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 227 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 228 | /* Handle keyboard interrupt SIGINT */ |
| 229 | struct sigaction s; |
| 230 | s.sa_handler = (void *)signal_handler; |
| 231 | if ((0 != sigemptyset(&s.sa_mask)) && debug) |
| 232 | printf("sigemptyset failed.\n"); |
| 233 | s.sa_flags = SA_RESETHAND; |
| 234 | if ((sigaction(SIGINT, &s, NULL) != 0) && debug) |
| 235 | printf("Could not register SIGINT signal handler.\n"); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 236 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 237 | fd_set fds; /* For select() */ |
| 238 | struct timeval idleTime; /* How long to select() */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 239 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 240 | int timelimit; /* Number of seconds to be connected */ |
| 241 | int starttime; /* Time program was started */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 242 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 243 | /* open a connection to the syslog daemon */ |
| 244 | /*openlog(PACKAGE, LOG_PID, LOG_DAEMON); */ |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 245 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 246 | /* TODO: Only use LOG__PERROR for linux */ |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 247 | #ifdef __linux__ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 248 | openlog(PACKAGE, (LOG_PID | LOG_PERROR), LOG_DAEMON); |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 249 | #else |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 250 | openlog(PACKAGE, (LOG_PID), LOG_DAEMON); |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 251 | #endif |
| 252 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 253 | if (cmdline_parser(argc, argv, &args_info) != 0) |
| 254 | exit(1); |
| 255 | if (args_info.debug_flag) { |
| 256 | printf("listen: %s\n", args_info.listen_arg); |
| 257 | if (args_info.conf_arg) |
| 258 | printf("conf: %s\n", args_info.conf_arg); |
| 259 | printf("fg: %d\n", args_info.fg_flag); |
| 260 | printf("debug: %d\n", args_info.debug_flag); |
| 261 | printf("qos: %#08x\n", args_info.qos_arg); |
| 262 | if (args_info.apn_arg) |
| 263 | printf("apn: %s\n", args_info.apn_arg); |
| 264 | if (args_info.net_arg) |
| 265 | printf("net: %s\n", args_info.net_arg); |
| 266 | if (args_info.dynip_arg) |
| 267 | printf("dynip: %s\n", args_info.dynip_arg); |
| 268 | if (args_info.statip_arg) |
| 269 | printf("statip: %s\n", args_info.statip_arg); |
| 270 | if (args_info.ipup_arg) |
| 271 | printf("ipup: %s\n", args_info.ipup_arg); |
| 272 | if (args_info.ipdown_arg) |
| 273 | printf("ipdown: %s\n", args_info.ipdown_arg); |
| 274 | if (args_info.pidfile_arg) |
| 275 | printf("pidfile: %s\n", args_info.pidfile_arg); |
| 276 | if (args_info.statedir_arg) |
| 277 | printf("statedir: %s\n", args_info.statedir_arg); |
| 278 | printf("timelimit: %d\n", args_info.timelimit_arg); |
| 279 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 280 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 281 | /* Try out our new parser */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 282 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 283 | if (cmdline_parser_configfile(args_info.conf_arg, &args_info, 0, 0, 0) |
| 284 | != 0) |
| 285 | exit(1); |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame] | 286 | |
| 287 | /* Open a log file */ |
| 288 | if (args_info.logfile_arg) { |
| 289 | FILE* log_file = fopen(args_info.logfile_arg, "a"); |
| 290 | if (!log_file) { |
| 291 | printf("Failed to open logfile: '%s'\n", |
| 292 | args_info.logfile_arg); |
| 293 | exit(1); |
| 294 | } |
| 295 | sys_err_setlogfile(log_file); |
| 296 | } |
| 297 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 298 | if (args_info.debug_flag) { |
| 299 | printf("cmdline_parser_configfile\n"); |
| 300 | printf("listen: %s\n", args_info.listen_arg); |
| 301 | printf("conf: %s\n", args_info.conf_arg); |
| 302 | printf("fg: %d\n", args_info.fg_flag); |
| 303 | printf("debug: %d\n", args_info.debug_flag); |
| 304 | printf("qos: %#08x\n", args_info.qos_arg); |
| 305 | if (args_info.apn_arg) |
| 306 | printf("apn: %s\n", args_info.apn_arg); |
| 307 | if (args_info.net_arg) |
| 308 | printf("net: %s\n", args_info.net_arg); |
| 309 | if (args_info.dynip_arg) |
| 310 | printf("dynip: %s\n", args_info.dynip_arg); |
| 311 | if (args_info.statip_arg) |
| 312 | printf("statip: %s\n", args_info.statip_arg); |
| 313 | if (args_info.ipup_arg) |
| 314 | printf("ipup: %s\n", args_info.ipup_arg); |
| 315 | if (args_info.ipdown_arg) |
| 316 | printf("ipdown: %s\n", args_info.ipdown_arg); |
| 317 | if (args_info.pidfile_arg) |
| 318 | printf("pidfile: %s\n", args_info.pidfile_arg); |
| 319 | if (args_info.statedir_arg) |
| 320 | printf("statedir: %s\n", args_info.statedir_arg); |
| 321 | printf("timelimit: %d\n", args_info.timelimit_arg); |
| 322 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 323 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 324 | /* Handle each option */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 325 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 326 | /* debug */ |
| 327 | debug = args_info.debug_flag; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 328 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 329 | /* listen */ |
| 330 | /* Do hostname lookup to translate hostname to IP address */ |
| 331 | /* Any port listening is not possible as a valid address is */ |
| 332 | /* required for create_pdp_context_response messages */ |
| 333 | if (args_info.listen_arg) { |
| 334 | if (!(host = gethostbyname(args_info.listen_arg))) { |
| 335 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 336 | "Invalid listening address: %s!", |
| 337 | args_info.listen_arg); |
| 338 | exit(1); |
| 339 | } else { |
| 340 | memcpy(&listen_.s_addr, host->h_addr, host->h_length); |
| 341 | } |
| 342 | } else { |
| 343 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 344 | "Listening address must be specified! " |
| 345 | "Please use command line option --listen or " |
| 346 | "edit %s configuration file\n", args_info.conf_arg); |
| 347 | exit(1); |
| 348 | } |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 349 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 350 | /* net */ |
| 351 | /* Store net as in_addr net and mask */ |
| 352 | if (args_info.net_arg) { |
| 353 | if (ippool_aton(&net, &mask, args_info.net_arg, 0)) { |
| 354 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 355 | "Invalid network address: %s!", |
| 356 | args_info.net_arg); |
| 357 | exit(1); |
| 358 | } |
| 359 | netaddr.s_addr = htonl(ntohl(net.s_addr) + 1); |
| 360 | destaddr.s_addr = htonl(ntohl(net.s_addr) + 1); |
| 361 | } else { |
| 362 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 363 | "Network address must be specified: %s!", |
| 364 | args_info.net_arg); |
| 365 | exit(1); |
| 366 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 367 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 368 | /* dynip */ |
| 369 | if (!args_info.dynip_arg) { |
| 370 | if (ippool_new(&ippool, args_info.net_arg, NULL, 1, 0, |
| 371 | IPPOOL_NONETWORK | IPPOOL_NOGATEWAY | |
| 372 | IPPOOL_NOBROADCAST)) { |
| 373 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 374 | "Failed to allocate IP pool!"); |
| 375 | exit(1); |
| 376 | } |
| 377 | } else { |
| 378 | if (ippool_new(&ippool, args_info.dynip_arg, NULL, 1, 0, |
| 379 | IPPOOL_NONETWORK | IPPOOL_NOGATEWAY | |
| 380 | IPPOOL_NOBROADCAST)) { |
| 381 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 382 | "Failed to allocate IP pool!"); |
| 383 | exit(1); |
| 384 | } |
| 385 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 386 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 387 | /* DNS1 and DNS2 */ |
jjako | ff9985c | 2004-01-16 11:05:22 +0000 | [diff] [blame] | 388 | #ifdef HAVE_INET_ATON |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 389 | dns1.s_addr = 0; |
| 390 | if (args_info.pcodns1_arg) { |
| 391 | if (0 == inet_aton(args_info.pcodns1_arg, &dns1)) { |
| 392 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 393 | "Failed to convert pcodns1!"); |
| 394 | exit(1); |
| 395 | } |
| 396 | } |
| 397 | dns2.s_addr = 0; |
| 398 | if (args_info.pcodns2_arg) { |
| 399 | if (0 == inet_aton(args_info.pcodns2_arg, &dns2)) { |
| 400 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 401 | "Failed to convert pcodns2!"); |
| 402 | exit(1); |
| 403 | } |
| 404 | } |
jjako | 1d3db97 | 2004-01-16 09:56:56 +0000 | [diff] [blame] | 405 | #else |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 406 | dns1.s_addr = 0; |
| 407 | if (args_info.pcodns1_arg) { |
| 408 | dns1.s_addr = inet_addr(args_info.pcodns1_arg); |
| 409 | if (dns1.s_addr == -1) { |
| 410 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 411 | "Failed to convert pcodns1!"); |
| 412 | exit(1); |
| 413 | } |
| 414 | } |
| 415 | dns2.s_addr = 0; |
| 416 | if (args_info.pcodns2_arg) { |
| 417 | dns2.s_addr = inet_addr(args_info.pcodns2_arg); |
| 418 | if (dns2.s_addr == -1) { |
| 419 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 420 | "Failed to convert pcodns2!"); |
| 421 | exit(1); |
| 422 | } |
| 423 | } |
jjako | 1d3db97 | 2004-01-16 09:56:56 +0000 | [diff] [blame] | 424 | #endif |
| 425 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 426 | pco.l = 20; |
| 427 | pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */ |
| 428 | pco.v[1] = 0x80; /* IPCP */ |
| 429 | pco.v[2] = 0x21; |
| 430 | pco.v[3] = 0x10; /* Length of contents */ |
| 431 | pco.v[4] = 0x02; /* ACK */ |
| 432 | pco.v[5] = 0x00; /* ID: Need to match request */ |
| 433 | pco.v[6] = 0x00; /* Length */ |
| 434 | pco.v[7] = 0x10; |
| 435 | pco.v[8] = 0x81; /* DNS 1 */ |
| 436 | pco.v[9] = 0x06; |
| 437 | memcpy(&pco.v[10], &dns1, sizeof(dns1)); |
| 438 | pco.v[14] = 0x83; |
| 439 | pco.v[15] = 0x06; /* DNS 2 */ |
| 440 | memcpy(&pco.v[16], &dns2, sizeof(dns2)); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 441 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 442 | /* ipup */ |
| 443 | ipup = args_info.ipup_arg; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 444 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 445 | /* ipdown */ |
| 446 | ipdown = args_info.ipdown_arg; |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 447 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 448 | /* Timelimit */ |
| 449 | timelimit = args_info.timelimit_arg; |
| 450 | starttime = time(NULL); |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 451 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 452 | /* qos */ |
| 453 | qos.l = 3; |
| 454 | qos.v[2] = (args_info.qos_arg) & 0xff; |
| 455 | qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff; |
| 456 | qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 457 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 458 | /* apn */ |
| 459 | if (strlen(args_info.apn_arg) > (sizeof(apn.v) - 1)) { |
| 460 | printf("Invalid APN\n"); |
| 461 | return -1; |
| 462 | } |
| 463 | apn.l = strlen(args_info.apn_arg) + 1; |
| 464 | apn.v[0] = (char)strlen(args_info.apn_arg); |
| 465 | strncpy((char *)&apn.v[1], args_info.apn_arg, sizeof(apn.v) - 1); |
jjako | e014978 | 2003-07-06 17:07:04 +0000 | [diff] [blame] | 466 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 467 | /* foreground */ |
| 468 | /* If flag not given run as a daemon */ |
| 469 | if (!args_info.fg_flag) { |
| 470 | FILE *f; |
| 471 | int rc; |
| 472 | closelog(); |
| 473 | /* Close the standard file descriptors. */ |
| 474 | /* Is this really needed ? */ |
| 475 | f = freopen("/dev/null", "w", stdout); |
| 476 | if (f == NULL) { |
| 477 | sys_err(LOG_WARNING, __FILE__, __LINE__, 0, |
| 478 | "Could not redirect stdout to /dev/null"); |
| 479 | } |
| 480 | f = freopen("/dev/null", "w", stderr); |
| 481 | if (f == NULL) { |
| 482 | sys_err(LOG_WARNING, __FILE__, __LINE__, 0, |
| 483 | "Could not redirect stderr to /dev/null"); |
| 484 | } |
| 485 | f = freopen("/dev/null", "r", stdin); |
| 486 | if (f == NULL) { |
| 487 | sys_err(LOG_WARNING, __FILE__, __LINE__, 0, |
| 488 | "Could not redirect stdin to /dev/null"); |
| 489 | } |
| 490 | rc = daemon(0, 0); |
| 491 | if (rc != 0) { |
| 492 | sys_err(LOG_ERR, __FILE__, __LINE__, rc, |
| 493 | "Could not daemonize"); |
| 494 | exit(1); |
| 495 | } |
| 496 | /* Open log again. This time with new pid */ |
| 497 | openlog(PACKAGE, LOG_PID, LOG_DAEMON); |
| 498 | } |
jjako | e014978 | 2003-07-06 17:07:04 +0000 | [diff] [blame] | 499 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 500 | /* pidfile */ |
| 501 | /* This has to be done after we have our final pid */ |
| 502 | if (args_info.pidfile_arg) { |
| 503 | log_pid(args_info.pidfile_arg); |
| 504 | } |
jjako | e014978 | 2003-07-06 17:07:04 +0000 | [diff] [blame] | 505 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 506 | if (debug) |
| 507 | printf("gtpclient: Initialising GTP tunnel\n"); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 508 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 509 | if (gtp_new(&gsn, args_info.statedir_arg, &listen_, GTP_MODE_GGSN)) { |
| 510 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Failed to create gtp"); |
| 511 | exit(1); |
| 512 | } |
| 513 | if (gsn->fd0 > maxfd) |
| 514 | maxfd = gsn->fd0; |
| 515 | if (gsn->fd1c > maxfd) |
| 516 | maxfd = gsn->fd1c; |
| 517 | if (gsn->fd1u > maxfd) |
| 518 | maxfd = gsn->fd1u; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 519 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 520 | gtp_set_cb_data_ind(gsn, encaps_tun); |
| 521 | gtp_set_cb_delete_context(gsn, delete_context); |
| 522 | gtp_set_cb_create_context_ind(gsn, create_context_ind); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 523 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 524 | /* Create a tunnel interface */ |
| 525 | if (debug) |
| 526 | printf("Creating tun interface\n"); |
| 527 | if (tun_new((struct tun_t **)&tun)) { |
| 528 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Failed to create tun"); |
| 529 | if (debug) |
| 530 | printf("Failed to create tun\n"); |
| 531 | exit(1); |
| 532 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 533 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 534 | if (debug) |
| 535 | printf("Setting tun IP address\n"); |
| 536 | if (tun_setaddr(tun, &netaddr, &destaddr, &mask)) { |
| 537 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 538 | "Failed to set tun IP address"); |
| 539 | if (debug) |
| 540 | printf("Failed to set tun IP address\n"); |
| 541 | exit(1); |
| 542 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 543 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 544 | tun_set_cb_ind(tun, cb_tun_ind); |
| 545 | if (tun->fd > maxfd) |
| 546 | maxfd = tun->fd; |
jjako | c6762cf | 2004-04-28 14:52:58 +0000 | [diff] [blame] | 547 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 548 | if (ipup) |
| 549 | tun_runscript(tun, ipup); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 550 | |
| 551 | /******************************************************************/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 552 | /* Main select loop */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 553 | /******************************************************************/ |
| 554 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 555 | while ((((starttime + timelimit) > time(NULL)) || (0 == timelimit)) |
| 556 | && (!end)) { |
Harald Welte | c3dcba0 | 2010-05-04 11:02:54 +0200 | [diff] [blame] | 557 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 558 | FD_ZERO(&fds); |
| 559 | if (tun) |
| 560 | FD_SET(tun->fd, &fds); |
| 561 | FD_SET(gsn->fd0, &fds); |
| 562 | FD_SET(gsn->fd1c, &fds); |
| 563 | FD_SET(gsn->fd1u, &fds); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 564 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 565 | gtp_retranstimeout(gsn, &idleTime); |
| 566 | switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) { |
| 567 | case -1: /* errno == EINTR : unblocked signal */ |
| 568 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 569 | "select() returned -1"); |
| 570 | /* On error, select returns without modifying fds */ |
| 571 | FD_ZERO(&fds); |
| 572 | break; |
| 573 | case 0: |
| 574 | /* printf("Select returned 0\n"); */ |
| 575 | gtp_retrans(gsn); /* Only retransmit if nothing else */ |
| 576 | break; |
| 577 | default: |
| 578 | break; |
| 579 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 580 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 581 | if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) && |
| 582 | tun_decaps(tun) < 0) { |
| 583 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 584 | "TUN read failed (fd)=(%d)", tun->fd); |
| 585 | } |
| 586 | |
| 587 | if (FD_ISSET(gsn->fd0, &fds)) |
| 588 | gtp_decaps0(gsn); |
| 589 | |
| 590 | if (FD_ISSET(gsn->fd1c, &fds)) |
| 591 | gtp_decaps1c(gsn); |
| 592 | |
| 593 | if (FD_ISSET(gsn->fd1u, &fds)) |
| 594 | gtp_decaps1u(gsn); |
| 595 | |
| 596 | } |
| 597 | |
| 598 | cmdline_parser_free(&args_info); |
| 599 | ippool_free(ippool); |
| 600 | gtp_free(gsn); |
| 601 | tun_free(tun); |
| 602 | |
| 603 | return 1; |
| 604 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 605 | } |