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> |
| 39 | #include <unistd.h> |
| 40 | |
| 41 | #include <sys/socket.h> |
| 42 | #include <sys/ioctl.h> |
| 43 | #include <net/if.h> |
| 44 | #include <features.h> |
| 45 | |
| 46 | #include <errno.h> |
| 47 | |
| 48 | #include <asm/types.h> |
| 49 | #include <sys/socket.h> |
| 50 | #include <linux/netlink.h> |
| 51 | |
| 52 | #include <time.h> |
| 53 | |
| 54 | #include "tun.h" |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 55 | #include "ippool.h" |
| 56 | #include "syserr.h" |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 57 | #include "../gtp/pdp.h" |
| 58 | #include "../gtp/gtp.h" |
| 59 | #include "cmdline.h" |
| 60 | |
| 61 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 62 | int maxfd = 0; /* For select() */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 63 | |
| 64 | struct in_addr listen_; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 65 | struct in_addr net, mask; /* Network interface */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 66 | struct in_addr dns1, dns2; /* PCO DNS address */ |
| 67 | char *ipup, *ipdown; /* Filename of scripts */ |
| 68 | int debug; /* Print debug output */ |
| 69 | struct ul255_t pco; |
| 70 | struct ul255_t qos; |
| 71 | struct ul255_t apn; |
| 72 | |
jjako | 9c7ff08 | 2003-04-11 10:01:41 +0000 | [diff] [blame] | 73 | struct gsn_t *gsn; /* GSN instance */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 74 | struct tun_t *tun; /* TUN instance */ |
| 75 | struct ippool_t *ippool; /* Pool of IP addresses */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | /* Used to write process ID to file. Assume someone else will delete */ |
| 79 | void log_pid(char *pidfile) { |
| 80 | FILE *file; |
| 81 | mode_t oldmask; |
| 82 | |
| 83 | oldmask = umask(022); |
| 84 | file = fopen(pidfile, "w"); |
| 85 | umask(oldmask); |
| 86 | if(!file) |
| 87 | return; |
| 88 | fprintf(file, "%d\n", getpid()); |
| 89 | fclose(file); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | int encaps_printf(void *p, void *packet, unsigned len) |
| 94 | { |
| 95 | int i; |
| 96 | if (debug) { |
| 97 | printf("The packet looks like this:\n"); |
| 98 | for( i=0; i<len; i++) { |
| 99 | printf("%02x ", (unsigned char)*(char *)(packet+i)); |
| 100 | if (!((i+1)%16)) printf("\n"); |
| 101 | }; |
| 102 | printf("\n"); |
| 103 | } |
| 104 | return 0; |
| 105 | } |
| 106 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 107 | int delete_context(struct pdp_t *pdp) { |
jjako | 4901471 | 2003-01-05 17:59:49 +0000 | [diff] [blame] | 108 | if (debug) printf("Deleting PDP context\n"); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 109 | ippool_freeip((struct ippoolm_t *) pdp->peer); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 114 | int create_context(struct pdp_t *pdp) { |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 115 | struct in_addr addr; |
| 116 | struct ippoolm_t *member; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 117 | |
| 118 | if (debug) printf("Received create PDP context request\n"); |
| 119 | |
| 120 | pdp->eua.l=0; /* TODO: Indicates dynamic IP */ |
| 121 | |
| 122 | /* ulcpy(&pdp->qos_neg, &pdp->qos_req, sizeof(pdp->qos_req.v)); */ |
| 123 | memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_neg)); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 124 | memcpy(&pdp->pco_neg, &pco, sizeof(pdp->pco_neg)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 125 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 126 | if (pdp_euaton(&pdp->eua, &addr)) { |
| 127 | addr.s_addr = 0; /* Request dynamic */ |
| 128 | } |
| 129 | |
| 130 | if (ippool_newip(ippool, &member, &addr)) { |
| 131 | return EOF; /* Allready in use, or no more available */ |
| 132 | } |
| 133 | |
| 134 | pdp_ntoeua(&member->addr, &pdp->eua); |
jjako | a7c3381 | 2003-04-11 11:51:39 +0000 | [diff] [blame^] | 135 | pdp->peer = member; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 136 | pdp->ipif = tun; /* TODO */ |
| 137 | member->peer = pdp; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 138 | |
| 139 | return 0; /* Success */ |
| 140 | } |
| 141 | |
| 142 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 143 | /* Callback for receiving messages from tun */ |
| 144 | int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) { |
| 145 | struct ippoolm_t *ipm; |
| 146 | struct in_addr dst; |
| 147 | struct tun_packet_t *iph = (struct tun_packet_t*) pack; |
| 148 | |
| 149 | dst.s_addr = iph->dst; |
| 150 | |
| 151 | if (ippool_getip(ippool, &ipm, &dst)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 152 | if (debug) printf("Received packet with no destination!!!\n"); |
| 153 | return 0; |
| 154 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 155 | |
| 156 | if (ipm->peer) /* Check if a peer protocol is defined */ |
| 157 | gtp_gpdu(gsn, (struct pdp_t*) ipm->peer, pack, len); |
| 158 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 159 | } |
| 160 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 161 | int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) { |
| 162 | /* printf("encaps_tun. Packet received: forwarding to tun\n");*/ |
| 163 | return tun_encaps((struct tun_t*) pdp->ipif, pack, len); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | int main(int argc, char **argv) |
| 168 | { |
| 169 | /* gengeopt declarations */ |
| 170 | struct gengetopt_args_info args_info; |
| 171 | |
| 172 | struct hostent *host; |
| 173 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 174 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 175 | fd_set fds; /* For select() */ |
| 176 | struct timeval idleTime; /* How long to select() */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 177 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 178 | |
| 179 | int timelimit; /* Number of seconds to be connected */ |
| 180 | int starttime; /* Time program was started */ |
| 181 | |
| 182 | /* open a connection to the syslog daemon */ |
| 183 | /*openlog(PACKAGE, LOG_PID, LOG_DAEMON);*/ |
| 184 | openlog(PACKAGE, (LOG_PID | LOG_PERROR), LOG_DAEMON); |
| 185 | |
| 186 | if (cmdline_parser (argc, argv, &args_info) != 0) |
| 187 | exit(1); |
| 188 | if (args_info.debug_flag) { |
| 189 | printf("listen: %s\n", args_info.listen_arg); |
| 190 | printf("conf: %s\n", args_info.conf_arg); |
| 191 | printf("fg: %d\n", args_info.fg_flag); |
| 192 | printf("debug: %d\n", args_info.debug_flag); |
| 193 | printf("qos: %#08x\n", args_info.qos_arg); |
| 194 | printf("apn: %s\n", args_info.apn_arg); |
| 195 | printf("net: %s\n", args_info.net_arg); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 196 | printf("dynip: %s\n", args_info.dynip_arg); |
| 197 | printf("statip: %s\n", args_info.statip_arg); |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 198 | printf("ipup: %s\n", args_info.ipup_arg); |
| 199 | printf("ipdown: %s\n", args_info.ipdown_arg); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 200 | printf("pidfile: %s\n", args_info.pidfile_arg); |
| 201 | printf("statedir: %s\n", args_info.statedir_arg); |
| 202 | printf("timelimit: %d\n", args_info.timelimit_arg); |
| 203 | } |
| 204 | |
| 205 | /* Try out our new parser */ |
| 206 | |
| 207 | if (cmdline_parser_configfile (args_info.conf_arg, &args_info, 0) != 0) |
| 208 | exit(1); |
| 209 | if (args_info.debug_flag) { |
| 210 | printf("cmdline_parser_configfile\n"); |
| 211 | printf("listen: %s\n", args_info.listen_arg); |
| 212 | printf("conf: %s\n", args_info.conf_arg); |
| 213 | printf("fg: %d\n", args_info.fg_flag); |
| 214 | printf("debug: %d\n", args_info.debug_flag); |
| 215 | printf("qos: %#08x\n", args_info.qos_arg); |
| 216 | printf("apn: %s\n", args_info.apn_arg); |
| 217 | printf("net: %s\n", args_info.net_arg); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 218 | printf("dynip: %s\n", args_info.dynip_arg); |
| 219 | printf("statip: %s\n", args_info.statip_arg); |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 220 | printf("ipup: %s\n", args_info.ipup_arg); |
| 221 | printf("ipdown: %s\n", args_info.ipdown_arg); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 222 | printf("pidfile: %s\n", args_info.pidfile_arg); |
| 223 | printf("statedir: %s\n", args_info.statedir_arg); |
| 224 | printf("timelimit: %d\n", args_info.timelimit_arg); |
| 225 | } |
| 226 | |
| 227 | /* Handle each option */ |
| 228 | |
| 229 | /* foreground */ |
| 230 | /* If flag not given run as a daemon */ |
| 231 | if (!args_info.fg_flag) |
| 232 | { |
| 233 | closelog(); |
| 234 | /* Close the standard file descriptors. */ |
| 235 | /* Is this really needed ? */ |
| 236 | freopen("/dev/null", "w", stdout); |
| 237 | freopen("/dev/null", "w", stderr); |
| 238 | freopen("/dev/null", "r", stdin); |
| 239 | daemon(0, 0); |
| 240 | /* Open log again. This time with new pid */ |
| 241 | openlog(PACKAGE, LOG_PID, LOG_DAEMON); |
| 242 | } |
| 243 | |
| 244 | /* debug */ |
| 245 | debug = args_info.debug_flag; |
| 246 | |
| 247 | /* pidfile */ |
| 248 | /* This has to be done after we have our final pid */ |
| 249 | if (args_info.pidfile_arg) { |
| 250 | log_pid(args_info.pidfile_arg); |
| 251 | } |
| 252 | |
| 253 | /* listen */ |
| 254 | /* If no listen option is specified listen to any local port */ |
| 255 | /* Do hostname lookup to translate hostname to IP address */ |
| 256 | if (args_info.listen_arg) { |
| 257 | if (!(host = gethostbyname(args_info.listen_arg))) { |
| 258 | fprintf(stderr, "%s: Invalid listening address: %s!\n", |
| 259 | PACKAGE, args_info.listen_arg); |
| 260 | syslog(LOG_ERR, "Invalid listening address: %s!", |
| 261 | args_info.listen_arg); |
jjako | a7c3381 | 2003-04-11 11:51:39 +0000 | [diff] [blame^] | 262 | exit(1); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 263 | } |
| 264 | else { |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 265 | memcpy(&listen_.s_addr, host->h_addr, host->h_length); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | else { |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 269 | listen_.s_addr = htonl(INADDR_ANY); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* net */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 273 | /* Store net as in_addr net and mask */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 274 | if (args_info.net_arg) { |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 275 | if(ippool_aton(&net, &mask, args_info.net_arg, 0)) { |
| 276 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 277 | "Invalid network address: %s!", args_info.net_arg); |
jjako | a7c3381 | 2003-04-11 11:51:39 +0000 | [diff] [blame^] | 278 | exit(1); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
jjako | a7c3381 | 2003-04-11 11:51:39 +0000 | [diff] [blame^] | 281 | else { |
| 282 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 283 | "Network address must be specified: %s!", args_info.net_arg); |
| 284 | exit(1); |
| 285 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 286 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 287 | /* dynip */ |
| 288 | if (!args_info.dynip_arg) { |
jjako | a7c3381 | 2003-04-11 11:51:39 +0000 | [diff] [blame^] | 289 | if (ippool_new(&ippool, args_info.net_arg, |
| 290 | IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) { |
| 291 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 292 | "Failed to allocate IP pool!"); |
| 293 | exit(1); |
| 294 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 295 | } |
| 296 | else { |
| 297 | if (ippool_new(&ippool, args_info.dynip_arg, |
| 298 | IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) { |
| 299 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 300 | "Failed to allocate IP pool!"); |
jjako | a7c3381 | 2003-04-11 11:51:39 +0000 | [diff] [blame^] | 301 | exit(1); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 305 | /* DNS1 and DNS2 */ |
| 306 | dns1.s_addr = 0; |
| 307 | if (args_info.pcodns1_arg) |
| 308 | inet_aton(args_info.pcodns1_arg, &dns1); |
| 309 | |
| 310 | dns2.s_addr = 0; |
| 311 | if (args_info.pcodns2_arg) |
| 312 | inet_aton(args_info.pcodns2_arg, &dns2); |
| 313 | |
| 314 | pco.l = 20; |
| 315 | pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */ |
| 316 | pco.v[1] = 0x80; /* IPCP */ |
| 317 | pco.v[2] = 0x21; |
| 318 | pco.v[3] = 0x10; /* Length of contents */ |
| 319 | pco.v[4] = 0x02; /* ACK */ |
| 320 | pco.v[5] = 0x00; /* ID: Need to match request */ |
| 321 | pco.v[6] = 0x00; /* Length */ |
| 322 | pco.v[7] = 0x10; |
| 323 | pco.v[8] = 0x81; /* DNS 1 */ |
| 324 | pco.v[9] = 0x06; |
| 325 | memcpy(&pco.v[10], &dns1, sizeof(dns1)); |
| 326 | pco.v[14] = 0x83; |
| 327 | pco.v[15] = 0x06; /* DNS 2 */ |
| 328 | memcpy(&pco.v[16], &dns2, sizeof(dns2)); |
| 329 | |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 330 | /* ipup */ |
| 331 | ipup = args_info.ipup_arg; |
| 332 | |
| 333 | /* ipdown */ |
| 334 | ipdown = args_info.ipdown_arg; |
| 335 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 336 | /* Timelimit */ |
| 337 | timelimit = args_info.timelimit_arg; |
| 338 | starttime = time(NULL); |
| 339 | |
| 340 | /* qos */ |
| 341 | qos.l = 3; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 342 | qos.v[2] = (args_info.qos_arg) & 0xff; |
| 343 | qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff; |
| 344 | qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 345 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 346 | /* apn */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 347 | if (strlen(args_info.apn_arg) > (sizeof(apn.v)-1)) { |
| 348 | printf("Invalid APN\n"); |
| 349 | return -1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 350 | } |
| 351 | apn.l = strlen(args_info.apn_arg) + 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 352 | apn.v[0] = (char) strlen(args_info.apn_arg); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 353 | strncpy(&apn.v[1], args_info.apn_arg, sizeof(apn.v)-1); |
| 354 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 355 | |
| 356 | if (debug) printf("gtpclient: Initialising GTP tunnel\n"); |
| 357 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 358 | if (gtp_new(&gsn, args_info.statedir_arg, &listen_)) { |
| 359 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 360 | "Failed to create gtp"); |
| 361 | exit(1); |
| 362 | } |
| 363 | if (gsn->fd > maxfd) maxfd = gsn->fd; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 364 | |
| 365 | gtp_set_cb_gpdu(gsn, encaps_tun); |
| 366 | gtp_set_cb_delete_context(gsn, delete_context); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 367 | gtp_set_cb_create_context(gsn, create_context); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 368 | |
| 369 | |
| 370 | /* Create a tunnel interface */ |
| 371 | if (tun_new((struct tun_t**) &tun)) { |
| 372 | sys_err(LOG_ERR, __FILE__, __LINE__, 0, |
| 373 | "Failed to create tun"); |
| 374 | exit(1); |
| 375 | } |
| 376 | |
| 377 | tun_setaddr(tun, &net, &net, &mask); |
| 378 | tun_set_cb_ind(tun, cb_tun_ind); |
| 379 | if (tun->fd > maxfd) maxfd = tun->fd; |
| 380 | |
jjako | 9c7ff08 | 2003-04-11 10:01:41 +0000 | [diff] [blame] | 381 | if (ipup) tun_runscript(tun, ipup); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 382 | |
| 383 | /******************************************************************/ |
| 384 | /* Main select loop */ |
| 385 | /******************************************************************/ |
| 386 | |
| 387 | while (((starttime + timelimit) > time(NULL)) || (0 == timelimit)) { |
| 388 | |
| 389 | FD_ZERO(&fds); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 390 | if (tun) FD_SET(tun->fd, &fds); |
| 391 | FD_SET(gsn->fd, &fds); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 392 | |
| 393 | gtp_retranstimeout(gsn, &idleTime); |
| 394 | switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) { |
| 395 | case -1: /* Error with select() * |
| 396 | if (errno != EINTR) |
| 397 | syslog(LOG_ERR, "CTRL: Error with select(), quitting"); |
| 398 | *goto leave_clear_call;*/ |
| 399 | syslog(LOG_ERR, "GGSN: select = -1"); |
| 400 | break; |
| 401 | case 0: |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 402 | /* printf("Select returned 0\n"); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 403 | gtp_retrans(gsn); /* Only retransmit if nothing else */ |
| 404 | break; |
| 405 | default: |
| 406 | break; |
| 407 | } |
| 408 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 409 | if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) && |
| 410 | tun_decaps(tun) < 0) { |
| 411 | syslog(LOG_ERR, "TUN read failed (fd)=(%d)", tun->fd); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 412 | } |
| 413 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 414 | if (FD_ISSET(gsn->fd, &fds)) |
| 415 | gtp_decaps(gsn); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 416 | |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 417 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 418 | |
| 419 | gtp_free(gsn); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 420 | tun_free(tun); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 421 | |
| 422 | return 1; |
| 423 | |
| 424 | } |
| 425 | |