spgw_config.c 31.7 KB
Newer Older
1
/*******************************************************************************
gauthier's avatar
GPLv3  
gauthier committed
2 3
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom
4

gauthier's avatar
GPLv3  
gauthier committed
5 6 7 8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
9 10


gauthier's avatar
GPLv3  
gauthier committed
11 12 13 14
    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
15

gauthier's avatar
GPLv3  
gauthier committed
16 17 18 19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
   see <http://www.gnu.org/licenses/>.
20

gauthier's avatar
GPLv3  
gauthier committed
21 22 23 24 25 26 27 28
  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr

  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.

 *******************************************************************************/
gauthier's avatar
Licence  
gauthier committed
29 30 31 32 33 34
/*! \file spgw_config.c
* \brief
* \author Lionel Gauthier
* \company Eurecom
* \email: lionel.gauthier@eurecom.fr
*/
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#define SGW_LITE
#define SPGW_CONFIG_C

#include <string.h>
#include <libconfig.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>

#include "log.h"
#include "assertions.h"
#include "spgw_config.h"
#include "sgw_lite_defs.h"
#include "intertask_interface.h"

gauthier's avatar
gauthier committed
54 55 56 57 58 59
#ifdef LIBCONFIG_LONG
#define libconfig_int long
#else
#define libconfig_int int
#endif

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#define NIPADDR(addr) \
        (uint8_t)(addr & 0x000000FF), \
        (uint8_t)((addr & 0x0000FF00) >> 8), \
        (uint8_t)((addr & 0x00FF0000) >> 16), \
        (uint8_t)((addr & 0xFF000000) >> 24)

#define HIPADDR(addr) \
        (uint8_t)((addr & 0xFF000000) >> 24),\
        (uint8_t)((addr & 0x00FF0000) >> 16),\
        (uint8_t)((addr & 0x0000FF00) >> 8), \
        (uint8_t)(addr & 0x000000FF)

#define NIP6ADDR(addr) \
        ntohs((addr)->s6_addr16[0]), \
        ntohs((addr)->s6_addr16[1]), \
        ntohs((addr)->s6_addr16[2]), \
        ntohs((addr)->s6_addr16[3]), \
        ntohs((addr)->s6_addr16[4]), \
        ntohs((addr)->s6_addr16[5]), \
        ntohs((addr)->s6_addr16[6]), \
        ntohs((addr)->s6_addr16[7])

#define IN6_ARE_ADDR_MASKED_EQUAL(a,b,m) \
           (((((__const uint32_t *) (a))[0] & (((__const uint32_t *) (m))[0])) == (((__const uint32_t *) (b))[0] & (((__const uint32_t *) (m))[0])))  \
         && ((((__const uint32_t *) (a))[1] & (((__const uint32_t *) (m))[1])) == (((__const uint32_t *) (b))[1] & (((__const uint32_t *) (m))[1])))  \
         && ((((__const uint32_t *) (a))[2] & (((__const uint32_t *) (m))[2])) == (((__const uint32_t *) (b))[2] & (((__const uint32_t *) (m))[2])))  \
         && ((((__const uint32_t *) (a))[3] & (((__const uint32_t *) (m))[3])) == (((__const uint32_t *) (b))[3] & (((__const uint32_t *) (m))[3]))))

88 89 90 91 92 93
void trim(char* srcP, int sizeP);
void sgw_ipv6_mask_in6_addr( struct in6_addr *addr6_pP, int maskP);


void
trim(
94 95
  char* srcP,
  int sizeP)
96
{
97 98 99 100 101 102 103 104 105
  if(srcP == NULL)
    return;

  const char* current = srcP;
  unsigned int i = 0;

  while((*current) != '\0' && (i < (sizeP-1))) {
    if((*current != ' ') && (*current != '\t')) {
      srcP[i++] = *current;
106
    }
107 108 109 110 111

    ++current;
  }

  srcP[i] = '\0';
112 113
}

114 115 116 117


void
sgw_ipv6_mask_in6_addr(
118 119
  struct in6_addr *addr6_pP,
  int maskP)
120
{
121
  int      addr8_idx;
122

123 124
  addr8_idx = maskP / 8;
  maskP     = maskP % 8;
125

126 127 128 129 130 131 132 133
  if (maskP > 0) {
    addr6_pP->s6_addr[addr8_idx] = addr6_pP->s6_addr[addr8_idx] & (0xFF << (8 - maskP));
    addr8_idx += 1;
  }

  while (addr8_idx < 16) {
    addr6_pP->s6_addr[addr8_idx++] = 0;
  }
134
}
135

gauthier's avatar
 
gauthier committed
136

137 138
int spgw_system(char *command_pP, spgw_system_abort_control_e abort_on_errorP, const char * const file_nameP, const int line_numberP)
{
gauthier's avatar
 
gauthier committed
139
  int ret = -1;
140

gauthier's avatar
 
gauthier committed
141
  if (command_pP) {
142 143 144 145 146
    SPGW_APP_INFO("system command: %s\n",command_pP);
    ret = system(command_pP);

    if (ret != 0) {
      SPGW_APP_ERROR("ERROR in system command %s: %d at %s:%u\n",
gauthier's avatar
 
gauthier committed
147
                     command_pP,ret, file_nameP, line_numberP);
148 149 150

      if (abort_on_errorP) {
        exit(-1); // may be not exit
gauthier's avatar
 
gauthier committed
151
      }
152
    }
gauthier's avatar
 
gauthier committed
153
  }
154

gauthier's avatar
 
gauthier committed
155 156 157
  return ret;
}

158 159
int spgw_config_process(spgw_config_t* config_pP)
{
gauthier's avatar
 
gauthier committed
160 161 162 163
  char              system_cmd[256];
  struct in_addr    inaddr;
  int               ret = 0;

164
  if (strncasecmp("tun",config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up, strlen("tun")) == 0) {
165
	config_pP->sgw_config.local_to_eNB = TRUE;
166 167 168 169
    if (snprintf(system_cmd, 256,
                 "ip link set %s down ;sync;openvpn --rmtun --dev %s;sync",
                 config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up,
                 config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up
gauthier's avatar
 
gauthier committed
170
                ) > 0) {
171 172 173 174 175
      ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
    } else {
      SPGW_APP_ERROR("Del %s\n", config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up);
      ret = -1;
    }
gauthier's avatar
 
gauthier committed
176

177 178 179 180
    if (snprintf(system_cmd, 256,
                 "openvpn --mktun --dev %s;sync",
                 config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up) > 0) {
      ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
gauthier's avatar
 
gauthier committed
181
    } else {
182 183
      SPGW_APP_ERROR("Create %s\n", config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up);
      ret = -1;
gauthier's avatar
 
gauthier committed
184 185
    }

186
    inaddr.s_addr = config_pP->sgw_config.ipv4.sgw_ipv4_address_for_S1u_S12_S4_up;
gauthier's avatar
 
gauthier committed
187 188

    if (snprintf(system_cmd, 256,
189 190 191 192 193
                 "ip -4 addr add %s/%d  dev %s;sync",
                 inet_ntoa(inaddr),
                 config_pP->sgw_config.ipv4.sgw_ip_netmask_for_S1u_S12_S4_up,
                 config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up) > 0) {
      ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
gauthier's avatar
 
gauthier committed
194
    } else {
195 196
      SPGW_APP_ERROR("Set IPv4 address on %s\n", config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up);
      ret = -1;
gauthier's avatar
 
gauthier committed
197 198
    }

199 200 201 202 203 204 205
    if (snprintf(system_cmd, 256,
                 "sync;ifconfig  %s up;sync",
                 config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up) > 0) {
      ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
    } else {
      SPGW_APP_ERROR("ifconfig up %s\n", config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up);
      ret = -1;
gauthier's avatar
 
gauthier committed
206 207
    }

208
    if (snprintf(system_cmd, 256,
209 210 211
                 "iptables -t filter -I INPUT -i lo -d %s --protocol sctp -j DROP",
                 inet_ntoa(inaddr)) > 0) {
      ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
212
    } else {
213 214
      SPGW_APP_ERROR("Drop SCTP traffic on S1U\n");
      ret = -1;
215 216
    }

gauthier's avatar
 
gauthier committed
217
    if (snprintf(system_cmd, 256,
218 219 220
                 "iptables -t filter -I INPUT -i lo -s %s --protocol sctp -j DROP",
                 inet_ntoa(inaddr)) > 0) {
      ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
gauthier's avatar
 
gauthier committed
221
    } else {
222 223
      SPGW_APP_ERROR("Drop SCTP traffic on S1U\n");
      ret = -1;
gauthier's avatar
 
gauthier committed
224
    }
gauthier's avatar
 
gauthier committed
225 226

    if (snprintf(system_cmd, 256,
227 228 229 230 231 232 233 234 235
                 "insmod $OPENAIR_TARGETS/bin/xt_GTPUAH.ko tunnel_local=1 gtpu_port=%u mtu=%u",
                 config_pP->sgw_config.sgw_udp_port_for_S1u_S12_S4_up,
                 config_pP->sgw_config.sgw_interface_mtu_for_S1u_S12_S4_up) > 0) {
      ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
    } else {
      SPGW_APP_ERROR("GTPUAH kernel module\n");
      ret = -1;
    }
  } else {
236
	config_pP->sgw_config.local_to_eNB = FALSE;
237 238 239 240 241
    if (snprintf(system_cmd, 256,
                 "insmod $OPENAIR_TARGETS/bin/xt_GTPUAH.ko tunnel_local=0 gtpu_port=%u mtu=%u",
                 config_pP->sgw_config.sgw_udp_port_for_S1u_S12_S4_up,
                 config_pP->sgw_config.sgw_interface_mtu_for_S1u_S12_S4_up) > 0) {
      ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
gauthier's avatar
 
gauthier committed
242
    } else {
243 244
      SPGW_APP_ERROR("GTPUAH kernel module\n");
      ret = -1;
gauthier's avatar
 
gauthier committed
245
    }
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  }

  spgw_system("insmod $OPENAIR_TARGETS/bin/xt_GTPURH.ko", SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);

#if defined (ENABLE_USE_GTPU_IN_KERNEL)
  ret += spgw_system("echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects", SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
#endif

  if (snprintf(system_cmd, 256,
               "ip link set dev %s mtu %u",
               config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up,
               config_pP->sgw_config.sgw_interface_mtu_for_S1u_S12_S4_up) > 0) {
    SPGW_APP_INFO("Set S1U interface MTU: %s\n",system_cmd);
    ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
  } else {
    SPGW_APP_ERROR("Set S1U interface MTU\n");
    ret = -1;
  }
gauthier's avatar
 
gauthier committed
264

265
  if (config_pP->sgw_config.sgw_drop_uplink_traffic) {
gauthier's avatar
 
gauthier committed
266
    if (snprintf(system_cmd, 256,
267 268 269 270
                 "iptables -t raw -I PREROUTING  -i %s --protocol udp --destination-port 2152  -j DROP",
                 config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up) > 0) {
      SPGW_APP_INFO("Drop uplink traffic: %s\n",system_cmd);
      ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
gauthier's avatar
 
gauthier committed
271
    } else {
272 273
      SPGW_APP_ERROR("Drop uplink traffic\n");
      ret = -1;
gauthier's avatar
 
gauthier committed
274
    }
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  }

  if (snprintf(system_cmd, 256,
               "ethtool -K %s tso off gso off gro off",
               config_pP->pgw_config.ipv4.pgw_interface_name_for_SGI) > 0) {
    SPGW_APP_INFO("Disable tcp segmentation offload, generic segmentation offload: %s\n",system_cmd);
    ret += spgw_system(system_cmd, SPGW_WARN_ON_ERROR, __FILE__, __LINE__);
  } else {
    SPGW_APP_ERROR("Disable tcp segmentation offload, generic segmentation offload\n");
    ret = -1;
  }


  //    if (config_pP->pgw_config.pgw_masquerade_SGI) {
  //        inaddr.s_addr = config_pP->pgw_config.ipv4.pgw_ipv4_address_for_SGI;
  //        if (snprintf(system_cmd, 256,
  //                     "iptables -t nat -I POSTROUTING  -o %s  ! --protocol sctp -j SNAT --to-source %s",
  //                     config_pP->pgw_config.ipv4.pgw_interface_name_for_SGI,
  //                     inet_ntoa(inaddr)) > 0) {
  //            SPGW_APP_INFO("Masquerade SGI: %s\n",system_cmd);
  //            ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
  //        } else {
  //            SPGW_APP_ERROR("Masquerade SGI\n");
  //            ret = -1;
  //        }
  //    }
#if defined (ENABLE_USE_GTPU_IN_KERNEL)

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  if (config_pP->sgw_config.local_to_eNB) {
	if (snprintf(system_cmd, 256,
	             "iptables -I OUTPUT -t mangle -m mark ! --mark 0 ! --protocol sctp  -j CONNMARK --save-mark") > 0) {
	  ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
	} else {
	  SPGW_APP_ERROR("Save mark\n");
	  ret = -1;
	}
	if (snprintf(system_cmd, 256,
	               "iptables -I INPUT -t mangle -i %s ! --protocol sctp   -j CONNMARK --restore-mark",
	               config_pP->pgw_config.ipv4.pgw_interface_name_for_SGI) > 0) {
	  ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
	} else {
	  SPGW_APP_ERROR("Restore mark\n");
	  ret = -1;
	}
  } else {
    if (snprintf(system_cmd, 256,
321 322
               "iptables -I POSTROUTING -t mangle -o %s -m mark ! --mark 0 ! --protocol sctp  -j CONNMARK --save-mark",
               config_pP->pgw_config.ipv4.pgw_interface_name_for_SGI) > 0) {
323 324 325 326 327 328 329 330 331 332 333 334 335
      ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
    } else {
      SPGW_APP_ERROR("Save mark\n");
      ret = -1;
    }
    if (snprintf(system_cmd, 256,
                 "iptables -I PREROUTING -t mangle -i %s ! --protocol sctp   -j CONNMARK --restore-mark",
                 config_pP->pgw_config.ipv4.pgw_interface_name_for_SGI) > 0) {
      ret += spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
    } else {
      SPGW_APP_ERROR("Restore mark\n");
      ret = -1;
    }
336 337 338
  }


gauthier's avatar
 
gauthier committed
339
#endif
340
  return ret;
gauthier's avatar
 
gauthier committed
341 342 343
}


344 345
int spgw_config_init(char* lib_config_file_name_pP, spgw_config_t* config_pP)
{
346 347 348 349 350 351 352 353 354

  config_t          cfg;
  config_setting_t *setting_sgw                          = NULL;
  char             *sgw_interface_name_for_S1u_S12_S4_up = NULL;
  char             *sgw_ipv4_address_for_S1u_S12_S4_up   = NULL;
  char             *sgw_interface_name_for_S5_S8_up      = NULL;
  char             *sgw_ipv4_address_for_S5_S8_up        = NULL;
  char             *sgw_interface_name_for_S11           = NULL;
  char             *sgw_ipv4_address_for_S11             = NULL;
gauthier's avatar
gauthier committed
355 356 357
  char             *sgw_drop_uplink_s1u_traffic          = NULL;
  char             *sgw_drop_downlink_s1u_traffic        = NULL;
  libconfig_int     sgw_interface_mtu_for_S1u_S12_S4_up  = 1500;
gauthier's avatar
 
gauthier committed
358
  libconfig_int     sgw_udp_port_for_S1u_S12_S4_up       = 2152;
359 360 361 362 363 364 365 366

  config_setting_t *setting_pgw                  = NULL;
  config_setting_t *subsetting                   = NULL;
  config_setting_t *sub2setting                  = NULL;
  char             *pgw_interface_name_for_S5_S8 = NULL;
  char             *pgw_ipv4_address_for_S5_S8   = NULL;
  char             *pgw_interface_name_for_SGI   = NULL;
  char             *pgw_ipv4_address_for_SGI     = NULL;
gauthier's avatar
gauthier committed
367
  char             *pgw_masquerade_SGI           = NULL;
gauthier's avatar
 
gauthier committed
368 369
  char             *pgw_default_dns_ipv4_address = NULL;
  char             *pgw_default_dns_sec_ipv4_address = NULL;
370

371 372 373 374 375 376 377 378
  char             *astring  = NULL;
  char             *atoken   = NULL;
  char             *atoken2  = NULL;
  char             *address  = NULL;
  char             *cidr     = NULL;
  char             *mask     = NULL;
  int               num      = 0;
  int               i        = 0;
379 380 381
  unsigned char     buf_in6_addr[sizeof(struct in6_addr)];
  struct in6_addr   addr6_start;
  struct in6_addr   addr6_mask;
382 383
  int               prefix_mask = 0;
  uint64_t          counter64   = 0;
384
  unsigned char     buf_in_addr[sizeof(struct in_addr)];
gauthier's avatar
gauthier committed
385
  struct in_addr    addr_start,in_addr_var;
386 387 388
  struct in_addr    addr_mask;
  pgw_lite_conf_ipv4_list_elm_t *ip4_ref = NULL;
  pgw_lite_conf_ipv6_list_elm_t *ip6_ref = NULL;
gauthier's avatar
 
gauthier committed
389
  char              system_cmd[256];
gauthier's avatar
gauthier committed
390
#if defined (ENABLE_USE_GTPU_IN_KERNEL)
391
  int               tun_id               = 21;
gauthier's avatar
 
gauthier committed
392
#endif
393 394

  memset((char*)config_pP, 0 , sizeof(spgw_config_t));
395 396
  STAILQ_INIT(&config_pP->pgw_config.pgw_lite_ipv4_pool_list);
  STAILQ_INIT(&config_pP->pgw_config.pgw_lite_ipv6_pool_list);
397 398 399

  config_init(&cfg);

400 401 402 403
  if(lib_config_file_name_pP != NULL) {
    /* Read the file. If there is an error, report it and exit. */
    if(! config_read_file(&cfg, lib_config_file_name_pP)) {
      SPGW_APP_ERROR("%s:%d - %s\n", lib_config_file_name_pP, config_error_line(&cfg), config_error_text(&cfg));
404
      config_destroy(&cfg);
405 406 407 408 409 410
      AssertFatal (1 == 0, "Failed to parse SP-GW configuration file %s!\n", lib_config_file_name_pP);
    }
  } else {
    SPGW_APP_ERROR("No SP-GW configuration file provided!\n");
    config_destroy(&cfg);
    AssertFatal (0, "No SP-GW configuration file provided!\n");
411
  }
412

413
  SPGW_APP_INFO("Parsing configuration file provided %s\n", lib_config_file_name_pP);
414 415

  setting_sgw = config_lookup(&cfg, SGW_CONFIG_STRING_SGW_CONFIG);
416

417
  if(setting_sgw != NULL) {
418
    subsetting = config_setting_get_member (setting_sgw, SGW_CONFIG_STRING_NETWORK_INTERFACES_CONFIG);
419

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    if(subsetting != NULL) {
      if(  (
             config_setting_lookup_string( subsetting, SGW_CONFIG_STRING_SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP, (const char **)&sgw_interface_name_for_S1u_S12_S4_up)
             && config_setting_lookup_string( subsetting, SGW_CONFIG_STRING_SGW_IPV4_ADDRESS_FOR_S1U_S12_S4_UP,   (const char **)&sgw_ipv4_address_for_S1u_S12_S4_up)
             && config_setting_lookup_string( subsetting, SGW_CONFIG_STRING_SGW_INTERFACE_NAME_FOR_S5_S8_UP,      (const char **)&sgw_interface_name_for_S5_S8_up)
             && config_setting_lookup_string( subsetting, SGW_CONFIG_STRING_SGW_IPV4_ADDRESS_FOR_S5_S8_UP,        (const char **)&sgw_ipv4_address_for_S5_S8_up)
             && config_setting_lookup_string( subsetting, SGW_CONFIG_STRING_SGW_INTERFACE_NAME_FOR_S11,           (const char **)&sgw_interface_name_for_S11)
             && config_setting_lookup_string( subsetting, SGW_CONFIG_STRING_SGW_IPV4_ADDRESS_FOR_S11,             (const char **)&sgw_ipv4_address_for_S11)
           )
        ) {
        config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up = strdup(sgw_interface_name_for_S1u_S12_S4_up);
        cidr = strdup(sgw_ipv4_address_for_S1u_S12_S4_up);
        address = strtok(cidr, "/");
        mask    = strtok(NULL, "/");
        IPV4_STR_ADDR_TO_INT_NWBO ( address, config_pP->sgw_config.ipv4.sgw_ipv4_address_for_S1u_S12_S4_up, "BAD IP ADDRESS FORMAT FOR S1u_S12_S4 !\n" )
        config_pP->sgw_config.ipv4.sgw_ip_netmask_for_S1u_S12_S4_up = atoi(mask);
        free(cidr);

        in_addr_var.s_addr = config_pP->sgw_config.ipv4.sgw_ipv4_address_for_S1u_S12_S4_up;
        SPGW_APP_INFO("Parsing configuration file found sgw_ipv4_address_for_S1u_S12_S4_up: %s/%d on %s\n",
gauthier's avatar
gauthier committed
440 441 442
                      inet_ntoa(in_addr_var),
                      config_pP->sgw_config.ipv4.sgw_ip_netmask_for_S1u_S12_S4_up,
                      config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up);
gauthier's avatar
gauthier committed
443 444


445 446 447 448 449 450 451 452 453
        config_pP->sgw_config.ipv4.sgw_interface_name_for_S5_S8_up = strdup(sgw_interface_name_for_S5_S8_up);
        cidr = strdup(sgw_ipv4_address_for_S5_S8_up);
        address = strtok(cidr, "/");
        mask    = strtok(NULL, "/");
        IPV4_STR_ADDR_TO_INT_NWBO ( address, config_pP->sgw_config.ipv4.sgw_ipv4_address_for_S5_S8_up, "BAD IP ADDRESS FORMAT FOR S5_S8 !\n" )
        config_pP->sgw_config.ipv4.sgw_ip_netmask_for_S5_S8_up = atoi(mask);
        free(cidr);
        in_addr_var.s_addr = config_pP->sgw_config.ipv4.sgw_ipv4_address_for_S5_S8_up;
        SPGW_APP_INFO("Parsing configuration file found sgw_ipv4_address_for_S5_S8_up: %s/%d on %s\n",
gauthier's avatar
gauthier committed
454 455 456
                      inet_ntoa(in_addr_var),
                      config_pP->sgw_config.ipv4.sgw_ip_netmask_for_S5_S8_up,
                      config_pP->sgw_config.ipv4.sgw_interface_name_for_S5_S8_up);
457

458 459 460 461 462 463 464 465 466
        config_pP->sgw_config.ipv4.sgw_interface_name_for_S11 = strdup(sgw_interface_name_for_S11);
        cidr = strdup(sgw_ipv4_address_for_S11);
        address = strtok(cidr, "/");
        mask    = strtok(NULL, "/");
        IPV4_STR_ADDR_TO_INT_NWBO ( address, config_pP->sgw_config.ipv4.sgw_ipv4_address_for_S11, "BAD IP ADDRESS FORMAT FOR S11 !\n" )
        config_pP->sgw_config.ipv4.sgw_ip_netmask_for_S11 = atoi(mask);
        free(cidr);
        in_addr_var.s_addr = config_pP->sgw_config.ipv4.sgw_ipv4_address_for_S11;
        SPGW_APP_INFO("Parsing configuration file found sgw_ipv4_address_for_S11: %s/%d on %s\n",
gauthier's avatar
gauthier committed
467 468 469
                      inet_ntoa(in_addr_var),
                      config_pP->sgw_config.ipv4.sgw_ip_netmask_for_S11,
                      config_pP->sgw_config.ipv4.sgw_interface_name_for_S11);
470
      }
gauthier's avatar
 
gauthier committed
471

472 473 474 475 476 477 478 479 480
      // optional
      if(config_setting_lookup_int(
            subsetting,
            SGW_CONFIG_STRING_SGW_INTERFACE_MTU_FOR_S1U_S12_S4_UP,
            &sgw_interface_mtu_for_S1u_S12_S4_up)
        ) {
        config_pP->sgw_config.sgw_interface_mtu_for_S1u_S12_S4_up = sgw_interface_mtu_for_S1u_S12_S4_up;
      } else {
        config_pP->sgw_config.sgw_interface_mtu_for_S1u_S12_S4_up = sgw_interface_mtu_for_S1u_S12_S4_up;
gauthier's avatar
gauthier committed
481
      }
482 483 484 485 486

      if(config_setting_lookup_int(
            subsetting,
            SGW_CONFIG_STRING_SGW_PORT_FOR_S1U_S12_S4_UP,
            &sgw_udp_port_for_S1u_S12_S4_up)
gauthier's avatar
gauthier committed
487
        ) {
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
        config_pP->sgw_config.sgw_udp_port_for_S1u_S12_S4_up = sgw_udp_port_for_S1u_S12_S4_up;
      } else {
        config_pP->sgw_config.sgw_udp_port_for_S1u_S12_S4_up = sgw_udp_port_for_S1u_S12_S4_up;
      }

    }

    if(  (
           config_setting_lookup_string( setting_sgw, SGW_CONFIG_STRING_SGW_DROP_UPLINK_S1U_TRAFFIC,
                                         (const char **)&sgw_drop_uplink_s1u_traffic)
           && config_setting_lookup_string( setting_sgw, SGW_CONFIG_STRING_SGW_DROP_DOWNLINK_S1U_TRAFFIC,
                                            (const char **)&sgw_drop_downlink_s1u_traffic)
         )
      ) {
      if (strcasecmp(sgw_drop_uplink_s1u_traffic, "yes") == 0) {
        config_pP->sgw_config.sgw_drop_uplink_traffic=1;
      } else {
        config_pP->sgw_config.sgw_drop_uplink_traffic=0;
      }

      if (strcasecmp(sgw_drop_downlink_s1u_traffic, "yes") == 0) {
        config_pP->sgw_config.sgw_drop_downlink_traffic=1;
      } else {
        config_pP->sgw_config.sgw_drop_downlink_traffic=0;
512
      }
513
    }
514 515 516
  }

  setting_pgw = config_lookup(&cfg, PGW_CONFIG_STRING_PGW_CONFIG);
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548

  if(setting_pgw != NULL) {
    subsetting = config_setting_get_member (setting_pgw, PGW_CONFIG_STRING_NETWORK_INTERFACES_CONFIG);

    if(subsetting != NULL) {
      if(  (
             config_setting_lookup_string(subsetting,
                                          PGW_CONFIG_STRING_PGW_INTERFACE_NAME_FOR_S5_S8,
                                          (const char **)&pgw_interface_name_for_S5_S8)
             && config_setting_lookup_string(subsetting,
                                             PGW_CONFIG_STRING_PGW_IPV4_ADDRESS_FOR_S5_S8,
                                             (const char **)&pgw_ipv4_address_for_S5_S8)
             && config_setting_lookup_string(subsetting,
                                             PGW_CONFIG_STRING_PGW_INTERFACE_NAME_FOR_SGI,
                                             (const char **)&pgw_interface_name_for_SGI)
             && config_setting_lookup_string(subsetting,
                                             PGW_CONFIG_STRING_PGW_IPV4_ADDR_FOR_SGI,
                                             (const char **)&pgw_ipv4_address_for_SGI)
             &&  config_setting_lookup_string(subsetting,
                                              PGW_CONFIG_STRING_PGW_MASQUERADE_SGI,
                                              (const char **)&pgw_masquerade_SGI)
           )
        ) {
        config_pP->pgw_config.ipv4.pgw_interface_name_for_S5_S8 = strdup(pgw_interface_name_for_S5_S8);
        cidr = strdup(pgw_ipv4_address_for_S5_S8);
        address = strtok(cidr, "/");
        mask    = strtok(NULL, "/");
        IPV4_STR_ADDR_TO_INT_NWBO ( address, config_pP->pgw_config.ipv4.pgw_ipv4_address_for_S5_S8, "BAD IP ADDRESS FORMAT FOR S5_S8 !\n" )
        config_pP->pgw_config.ipv4.pgw_ip_netmask_for_S5_S8 = atoi(mask);
        free(cidr);
        in_addr_var.s_addr = config_pP->pgw_config.ipv4.pgw_ipv4_address_for_S5_S8;
        SPGW_APP_INFO("Parsing configuration file found pgw_ipv4_address_for_S5_S8: %s/%d on %s\n",
gauthier's avatar
gauthier committed
549 550 551
                      inet_ntoa(in_addr_var),
                      config_pP->pgw_config.ipv4.pgw_ip_netmask_for_S5_S8,
                      config_pP->pgw_config.ipv4.pgw_interface_name_for_S5_S8);
552

553 554 555 556 557 558 559 560 561
        config_pP->pgw_config.ipv4.pgw_interface_name_for_SGI = strdup(pgw_interface_name_for_SGI);
        cidr = strdup(pgw_ipv4_address_for_SGI);
        address = strtok(cidr, "/");
        mask    = strtok(NULL, "/");
        IPV4_STR_ADDR_TO_INT_NWBO ( address, config_pP->pgw_config.ipv4.pgw_ipv4_address_for_SGI, "BAD IP ADDRESS FORMAT FOR SGI !\n" )
        config_pP->pgw_config.ipv4.pgw_ip_netmask_for_SGI = atoi(mask);
        free(cidr);
        in_addr_var.s_addr = config_pP->pgw_config.ipv4.pgw_ipv4_address_for_SGI;
        SPGW_APP_INFO("Parsing configuration file found pgw_ipv4_address_for_SGI: %s/%d on %s\n",
gauthier's avatar
gauthier committed
562 563 564 565
                      inet_ntoa(in_addr_var),
                      config_pP->pgw_config.ipv4.pgw_ip_netmask_for_SGI,
                      config_pP->pgw_config.ipv4.pgw_interface_name_for_SGI);

566 567 568 569 570 571
        if (strcasecmp(pgw_masquerade_SGI, "yes") == 0) {
          config_pP->pgw_config.pgw_masquerade_SGI=1;
        } else {
          config_pP->pgw_config.pgw_masquerade_SGI=0;
          SPGW_APP_INFO("No masquerading for SGI\n");
        }
gauthier's avatar
gauthier committed
572
      } else {
573
        SPGW_APP_WARN("CONFIG P-GW / NETWORK INTERFACES parsing failed\n");
574
      }
575 576 577 578 579 580 581 582 583 584 585 586
    } else {
      SPGW_APP_WARN("CONFIG P-GW / NETWORK INTERFACES not found\n");
    }

    //!!!------------------------------------!!!
    spgw_config_process(config_pP);
    //!!!------------------------------------!!!

    subsetting = config_setting_get_member (setting_pgw, PGW_CONFIG_STRING_IP_ADDRESS_POOL);

    if(subsetting != NULL) {
      sub2setting = config_setting_get_member (subsetting, PGW_CONFIG_STRING_IPV4_ADDRESS_LIST);
gauthier's avatar
 
gauthier committed
587

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
      if(sub2setting != NULL) {
        num     = config_setting_length(sub2setting);

        for (i = 0; i < num; i++) {
          astring = config_setting_get_string_elem(sub2setting,i);

          if (astring != NULL) {
            trim(astring, strlen(astring)+1);
            // failure, test if there is a range specified in the string
            atoken = strtok(astring, PGW_CONFIG_STRING_IPV4_PREFIX_DELIMITER);

            if (inet_pton(AF_INET, atoken, buf_in_addr) == 1) {
              memcpy (&addr_start, buf_in_addr, sizeof(struct in_addr));
              // valid address
              atoken2 = strtok(NULL, PGW_CONFIG_STRING_IPV4_PREFIX_DELIMITER);
gauthier's avatar
 
gauthier committed
603
#if defined (ENABLE_USE_GTPU_IN_KERNEL)
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
              in_addr_var.s_addr = config_pP->sgw_config.ipv4.sgw_ipv4_address_for_S1u_S12_S4_up;

              //                          if (snprintf(system_cmd, 128, "ip route add %s/%s via %s dev %s",
              //                                       astring,
              //                                       atoken2,
              //                                       inet_ntoa(in_addr_var),
              //                                       config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up) > 0) {
              if (snprintf(system_cmd, 128, "ip route add %s/%s dev %s",
                           astring,
                           atoken2,
                           config_pP->sgw_config.ipv4.sgw_interface_name_for_S1u_S12_S4_up) > 0) {
                spgw_system(system_cmd, SPGW_WARN_ON_ERROR, __FILE__, __LINE__);
              } else {
                SPGW_APP_ERROR("Add route: for %s\n", astring);
              }


              if (config_pP->sgw_config.sgw_drop_downlink_traffic) {
                if (snprintf(system_cmd, 128,
                             "iptables -t filter -I FORWARD  -d %s/%s  -j DROP",
                             astring, atoken2) > 0) {
                  spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
                } else {
                  SPGW_APP_ERROR("Drop downlink traffic\n");
                }

                if (snprintf(system_cmd, 128,
                             "iptables -t filter -I OUTPUT  -d %s/%s  -j DROP",
                             astring, atoken2) > 0) {
                  spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
                } else {
                  SPGW_APP_ERROR("Drop downlink traffic\n");
                }
              }

gauthier's avatar
 
gauthier committed
639
#endif
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
              prefix_mask = atoi(atoken2);

              if ((prefix_mask >= 2)&&(prefix_mask < 32)) {
                memcpy (&addr_start, buf_in_addr, sizeof(struct in_addr));
                memcpy (&addr_mask,  buf_in_addr, sizeof(struct in_addr));

                addr_mask.s_addr = addr_mask.s_addr & htonl(0xFFFFFFFF << (32 - prefix_mask));

                if (memcmp(&addr_start, &addr_mask, sizeof(struct in_addr)) != 0) {
                  AssertFatal(0, "BAD IPV4 ADDR CONFIG/MASK PAIRING %s/%d addr %X mask %X\n",
                              astring, prefix_mask, addr_start.s_addr, addr_mask.s_addr);
                }

                counter64 = 0x00000000FFFFFFFF >> prefix_mask; // address Prefix_mask/0..0 not valid
                counter64 = counter64 - 2;

                do {
                  addr_start.s_addr = addr_start.s_addr + htonl(2);
                  ip4_ref = calloc(1, sizeof(pgw_lite_conf_ipv4_list_elm_t));
                  ip4_ref->addr       = addr_start;

                  STAILQ_INSERT_TAIL(&config_pP->pgw_config.pgw_lite_ipv4_pool_list, ip4_ref, ipv4_entries);
                  counter64 = counter64 - 1;
                } while (counter64 > 0);

                //---------------
                if (config_pP->pgw_config.pgw_masquerade_SGI) {
                  in_addr_var.s_addr = config_pP->pgw_config.ipv4.pgw_ipv4_address_for_SGI;

                  if (snprintf(system_cmd, 256,
                               "iptables -t nat -I POSTROUTING -s %s/%s -o %s  ! --protocol sctp -j SNAT --to-source %s",
                               astring,
                               atoken2,
                               config_pP->pgw_config.ipv4.pgw_interface_name_for_SGI,
                               inet_ntoa(in_addr_var)) > 0) {
                    SPGW_APP_INFO("Masquerade SGI: %s\n",system_cmd);
                    spgw_system(system_cmd, SPGW_ABORT_ON_ERROR, __FILE__, __LINE__);
                  } else {
                    SPGW_APP_ERROR("Masquerade SGI\n");
679
                  }
680 681 682
                }
              } else {
                SPGW_APP_ERROR("CONFIG POOL ADDR IPV4: BAD MASQ: %s\n", atoken2);
683
              }
684
            }
685
          }
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
        }
      } else {
        SPGW_APP_WARN("CONFIG POOL ADDR IPV4: NO IPV4 ADDRESS FOUND\n");
      }

      sub2setting = config_setting_get_member (subsetting, PGW_CONFIG_STRING_IPV6_ADDRESS_LIST);

      if(sub2setting != NULL) {
        num     = config_setting_length(sub2setting);

        for (i = 0; i < num; i++) {
          astring = config_setting_get_string_elem(sub2setting,i);

          if (astring != NULL) {
            trim(astring, strlen(astring)+1);

            if (inet_pton(AF_INET6, astring, buf_in6_addr) < 1) {
              // failure, test if there is a range specified in the string
              atoken = strtok(astring, PGW_CONFIG_STRING_IPV6_PREFIX_DELIMITER);

              if (inet_pton(AF_INET6, astring, buf_in6_addr) == 1) {
                atoken2 = strtok(NULL, PGW_CONFIG_STRING_IPV6_PREFIX_DELIMITER);
                prefix_mask = atoi(atoken2);
                // arbitrary values
                DevAssert((prefix_mask < 128) && (prefix_mask >= 64));

                memcpy (&addr6_start, buf_in6_addr, sizeof(struct in6_addr));
                memcpy (&addr6_mask,  buf_in6_addr, sizeof(struct in6_addr));
                sgw_ipv6_mask_in6_addr(&addr6_mask, prefix_mask);

                if (memcmp(&addr6_start, &addr6_mask, sizeof(struct in6_addr)) != 0) {
                  AssertFatal(0, "BAD IPV6 ADDR CONFIG/MASK PAIRING %s/%d\n", astring, prefix_mask);
                }


                ip6_ref = calloc(1, sizeof(pgw_lite_conf_ipv6_list_elm_t));
                ip6_ref->addr       = addr6_start;
                ip6_ref->prefix_len = prefix_mask;
                STAILQ_INSERT_TAIL(&config_pP->pgw_config.pgw_lite_ipv6_pool_list, ip6_ref, ipv6_entries);

726
              }
727 728 729
            } else {
              SPGW_APP_WARN("CONFIG POOL ADDR IPV6: FAILED WHILE PARSING %s\n", astring);
            }
730
          }
731
        }
732
      }
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

      if(
        config_setting_lookup_string(subsetting,
                                     PGW_CONFIG_STRING_DEFAULT_DNS_IPV4_ADDRESS,
                                     (const char **)&pgw_default_dns_ipv4_address)
        && config_setting_lookup_string(subsetting,
                                        PGW_CONFIG_STRING_DEFAULT_DNS_SEC_IPV4_ADDRESS,
                                        (const char **)&pgw_default_dns_sec_ipv4_address)) {
        config_pP->pgw_config.ipv4.pgw_interface_name_for_S5_S8 = strdup(pgw_interface_name_for_S5_S8);
        IPV4_STR_ADDR_TO_INT_NWBO ( pgw_default_dns_ipv4_address,     config_pP->pgw_config.ipv4.default_dns_v4, "BAD IPv4 ADDRESS FORMAT FOR DEFAULT DNS !\n" )
        IPV4_STR_ADDR_TO_INT_NWBO ( pgw_default_dns_sec_ipv4_address, config_pP->pgw_config.ipv4.default_dns_sec_v4, "BAD IPv4 ADDRESS FORMAT FOR DEFAULT DNS SEC!\n" )
      } else {
        SPGW_APP_WARN("NO DNS CONFIGURATION FOUND\n");
      }
    }
gauthier's avatar
gauthier committed
748
  } else {
749
    SPGW_APP_WARN("CONFIG P-GW not found\n");
750
  }
751

752 753
  return 0;
}