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