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