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