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