blob: 74fe1ccb62e3d3ca83cf03433042ca63b5ba6d60 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
jjakoa7cd2492003-04-11 09:40:12 +00002 * OpenGGSN - Gateway GPRS Support Node
3 * Copyright (C) 2002, 2003 Mondru AB.
jjako52c24142002-12-16 13:33:51 +00004 *
jjakoa7cd2492003-04-11 09:40:12 +00005 * 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.
jjako52c24142002-12-16 13:33:51 +00009 *
jjakoa7cd2492003-04-11 09:40:12 +000010 * The initial developer of the original code is
11 * Jens Jakobsen <jj@openggsn.org>
jjako52c24142002-12-16 13:33:51 +000012 *
jjakoa7cd2492003-04-11 09:40:12 +000013 * Contributor(s):
jjako52c24142002-12-16 13:33:51 +000014 *
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"
jjakoa7cd2492003-04-11 09:40:12 +000055#include "ippool.h"
56#include "syserr.h"
jjako52c24142002-12-16 13:33:51 +000057#include "../gtp/pdp.h"
58#include "../gtp/gtp.h"
59#include "cmdline.h"
60
61
jjakoa7cd2492003-04-11 09:40:12 +000062int maxfd = 0; /* For select() */
jjakoa7cd2492003-04-11 09:40:12 +000063
64struct in_addr listen_;
jjako52c24142002-12-16 13:33:51 +000065struct in_addr net, mask; /* Network interface */
jjakoa7cd2492003-04-11 09:40:12 +000066struct in_addr dns1, dns2; /* PCO DNS address */
67char *ipup, *ipdown; /* Filename of scripts */
68int debug; /* Print debug output */
69struct ul255_t pco;
70struct ul255_t qos;
71struct ul255_t apn;
72
jjako9c7ff082003-04-11 10:01:41 +000073struct gsn_t *gsn; /* GSN instance */
jjakoa7cd2492003-04-11 09:40:12 +000074struct tun_t *tun; /* TUN instance */
75struct ippool_t *ippool; /* Pool of IP addresses */
jjako52c24142002-12-16 13:33:51 +000076
77
78/* Used to write process ID to file. Assume someone else will delete */
79void 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
93int 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
jjako52c24142002-12-16 13:33:51 +0000107int delete_context(struct pdp_t *pdp) {
jjako49014712003-01-05 17:59:49 +0000108 if (debug) printf("Deleting PDP context\n");
jjakoa7cd2492003-04-11 09:40:12 +0000109 ippool_freeip((struct ippoolm_t *) pdp->peer);
jjako52c24142002-12-16 13:33:51 +0000110 return 0;
111}
112
113
jjako52c24142002-12-16 13:33:51 +0000114int create_context(struct pdp_t *pdp) {
jjakoa7cd2492003-04-11 09:40:12 +0000115 struct in_addr addr;
116 struct ippoolm_t *member;
jjako52c24142002-12-16 13:33:51 +0000117
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));
jjakoa7cd2492003-04-11 09:40:12 +0000124 memcpy(&pdp->pco_neg, &pco, sizeof(pdp->pco_neg));
jjako52c24142002-12-16 13:33:51 +0000125
jjakoa7cd2492003-04-11 09:40:12 +0000126 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);
135 pdp->peer = &member;
136 pdp->ipif = tun; /* TODO */
137 member->peer = pdp;
jjako52c24142002-12-16 13:33:51 +0000138
139 return 0; /* Success */
140}
141
142
jjakoa7cd2492003-04-11 09:40:12 +0000143/* Callback for receiving messages from tun */
144int 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)) {
jjako52c24142002-12-16 13:33:51 +0000152 if (debug) printf("Received packet with no destination!!!\n");
153 return 0;
154 }
jjakoa7cd2492003-04-11 09:40:12 +0000155
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;
jjako52c24142002-12-16 13:33:51 +0000159}
160
jjako52c24142002-12-16 13:33:51 +0000161int 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
167int main(int argc, char **argv)
168{
169 /* gengeopt declarations */
170 struct gengetopt_args_info args_info;
171
172 struct hostent *host;
173
jjako52c24142002-12-16 13:33:51 +0000174
jjako52c24142002-12-16 13:33:51 +0000175 fd_set fds; /* For select() */
176 struct timeval idleTime; /* How long to select() */
jjako52c24142002-12-16 13:33:51 +0000177
jjako52c24142002-12-16 13:33:51 +0000178
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);
jjakoa7cd2492003-04-11 09:40:12 +0000196 printf("dynip: %s\n", args_info.dynip_arg);
197 printf("statip: %s\n", args_info.statip_arg);
jjako4b26b512003-01-28 16:13:57 +0000198 printf("ipup: %s\n", args_info.ipup_arg);
199 printf("ipdown: %s\n", args_info.ipdown_arg);
jjako52c24142002-12-16 13:33:51 +0000200 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);
jjakoa7cd2492003-04-11 09:40:12 +0000218 printf("dynip: %s\n", args_info.dynip_arg);
219 printf("statip: %s\n", args_info.statip_arg);
jjako4b26b512003-01-28 16:13:57 +0000220 printf("ipup: %s\n", args_info.ipup_arg);
221 printf("ipdown: %s\n", args_info.ipdown_arg);
jjako52c24142002-12-16 13:33:51 +0000222 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);
262 return 1;
263 }
264 else {
jjakoa7cd2492003-04-11 09:40:12 +0000265 memcpy(&listen_.s_addr, host->h_addr, host->h_length);
jjako52c24142002-12-16 13:33:51 +0000266 }
267 }
268 else {
jjakoa7cd2492003-04-11 09:40:12 +0000269 listen_.s_addr = htonl(INADDR_ANY);
jjako52c24142002-12-16 13:33:51 +0000270 }
271
272 /* net */
jjakoa7cd2492003-04-11 09:40:12 +0000273 /* Store net as in_addr net and mask */
jjako52c24142002-12-16 13:33:51 +0000274 if (args_info.net_arg) {
jjakoa7cd2492003-04-11 09:40:12 +0000275 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);
278 return -1;
jjako52c24142002-12-16 13:33:51 +0000279 }
280 }
281
jjakoa7cd2492003-04-11 09:40:12 +0000282 /* dynip */
283 if (!args_info.dynip_arg) {
284 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
285 "No dynamic address pool given!");
286 return -1;
287 }
288 else {
289 if (ippool_new(&ippool, args_info.dynip_arg,
290 IPPOOL_NONETWORK | IPPOOL_NOBROADCAST)) {
291 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
292 "Failed to allocate IP pool!");
jjako52c24142002-12-16 13:33:51 +0000293 }
294 }
295
jjakoa7cd2492003-04-11 09:40:12 +0000296 /* DNS1 and DNS2 */
297 dns1.s_addr = 0;
298 if (args_info.pcodns1_arg)
299 inet_aton(args_info.pcodns1_arg, &dns1);
300
301 dns2.s_addr = 0;
302 if (args_info.pcodns2_arg)
303 inet_aton(args_info.pcodns2_arg, &dns2);
304
305 pco.l = 20;
306 pco.v[0] = 0x80; /* x0000yyy x=1, yyy=000: PPP */
307 pco.v[1] = 0x80; /* IPCP */
308 pco.v[2] = 0x21;
309 pco.v[3] = 0x10; /* Length of contents */
310 pco.v[4] = 0x02; /* ACK */
311 pco.v[5] = 0x00; /* ID: Need to match request */
312 pco.v[6] = 0x00; /* Length */
313 pco.v[7] = 0x10;
314 pco.v[8] = 0x81; /* DNS 1 */
315 pco.v[9] = 0x06;
316 memcpy(&pco.v[10], &dns1, sizeof(dns1));
317 pco.v[14] = 0x83;
318 pco.v[15] = 0x06; /* DNS 2 */
319 memcpy(&pco.v[16], &dns2, sizeof(dns2));
320
jjako4b26b512003-01-28 16:13:57 +0000321 /* ipup */
322 ipup = args_info.ipup_arg;
323
324 /* ipdown */
325 ipdown = args_info.ipdown_arg;
326
jjako52c24142002-12-16 13:33:51 +0000327 /* Timelimit */
328 timelimit = args_info.timelimit_arg;
329 starttime = time(NULL);
330
331 /* qos */
332 qos.l = 3;
jjako52c24142002-12-16 13:33:51 +0000333 qos.v[2] = (args_info.qos_arg) & 0xff;
334 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
335 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
jjakoa7cd2492003-04-11 09:40:12 +0000336
jjako52c24142002-12-16 13:33:51 +0000337 /* apn */
jjakoa7cd2492003-04-11 09:40:12 +0000338 if (strlen(args_info.apn_arg) > (sizeof(apn.v)-1)) {
339 printf("Invalid APN\n");
340 return -1;
jjako52c24142002-12-16 13:33:51 +0000341 }
342 apn.l = strlen(args_info.apn_arg) + 1;
jjako52c24142002-12-16 13:33:51 +0000343 apn.v[0] = (char) strlen(args_info.apn_arg);
jjakoa7cd2492003-04-11 09:40:12 +0000344 strncpy(&apn.v[1], args_info.apn_arg, sizeof(apn.v)-1);
345
jjako52c24142002-12-16 13:33:51 +0000346
347 if (debug) printf("gtpclient: Initialising GTP tunnel\n");
348
jjakoa7cd2492003-04-11 09:40:12 +0000349 if (gtp_new(&gsn, args_info.statedir_arg, &listen_)) {
350 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
351 "Failed to create gtp");
352 exit(1);
353 }
354 if (gsn->fd > maxfd) maxfd = gsn->fd;
jjako52c24142002-12-16 13:33:51 +0000355
356 gtp_set_cb_gpdu(gsn, encaps_tun);
357 gtp_set_cb_delete_context(gsn, delete_context);
jjako52c24142002-12-16 13:33:51 +0000358 gtp_set_cb_create_context(gsn, create_context);
jjakoa7cd2492003-04-11 09:40:12 +0000359
360
361 /* Create a tunnel interface */
362 if (tun_new((struct tun_t**) &tun)) {
363 sys_err(LOG_ERR, __FILE__, __LINE__, 0,
364 "Failed to create tun");
365 exit(1);
366 }
367
368 tun_setaddr(tun, &net, &net, &mask);
369 tun_set_cb_ind(tun, cb_tun_ind);
370 if (tun->fd > maxfd) maxfd = tun->fd;
371
jjako9c7ff082003-04-11 10:01:41 +0000372 if (ipup) tun_runscript(tun, ipup);
jjako52c24142002-12-16 13:33:51 +0000373
374 /******************************************************************/
375 /* Main select loop */
376 /******************************************************************/
377
378 while (((starttime + timelimit) > time(NULL)) || (0 == timelimit)) {
379
380 FD_ZERO(&fds);
jjakoa7cd2492003-04-11 09:40:12 +0000381 if (tun) FD_SET(tun->fd, &fds);
382 FD_SET(gsn->fd, &fds);
jjako52c24142002-12-16 13:33:51 +0000383
384 gtp_retranstimeout(gsn, &idleTime);
385 switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
386 case -1: /* Error with select() *
387 if (errno != EINTR)
388 syslog(LOG_ERR, "CTRL: Error with select(), quitting");
389 *goto leave_clear_call;*/
390 syslog(LOG_ERR, "GGSN: select = -1");
391 break;
392 case 0:
jjakoa7cd2492003-04-11 09:40:12 +0000393 /* printf("Select returned 0\n"); */
jjako52c24142002-12-16 13:33:51 +0000394 gtp_retrans(gsn); /* Only retransmit if nothing else */
395 break;
396 default:
397 break;
398 }
399
jjakoa7cd2492003-04-11 09:40:12 +0000400 if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) &&
401 tun_decaps(tun) < 0) {
402 syslog(LOG_ERR, "TUN read failed (fd)=(%d)", tun->fd);
jjako52c24142002-12-16 13:33:51 +0000403 }
404
jjakoa7cd2492003-04-11 09:40:12 +0000405 if (FD_ISSET(gsn->fd, &fds))
406 gtp_decaps(gsn);
jjako52c24142002-12-16 13:33:51 +0000407
jjako4b26b512003-01-28 16:13:57 +0000408 }
jjako52c24142002-12-16 13:33:51 +0000409
410 gtp_free(gsn);
jjakoa7cd2492003-04-11 09:40:12 +0000411 tun_free(tun);
jjako52c24142002-12-16 13:33:51 +0000412
413 return 1;
414
415}
416