/* * Copyright (c) 2015 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef included_clib_elf_h #define included_clib_elf_h #include #include #include #include #define foreach_elf_file_class \ _ (CLASS_NONE) _ (32BIT) _ (64BIT) #define foreach_elf_data_encoding \ _ (ENCODING_NONE) \ _ (TWOS_COMPLEMENT_LITTLE_ENDIAN) \ _ (TWOS_COMPLEMENT_BIG_ENDIAN) #define ELF_VERSION_NONE (0) #define ELF_VERSION_CURRENT (1) #define foreach_elf_abi \ _ (SYSV, 0) \ _ (HPUX, 1) \ _ (NETBSD, 2) \ _ (LINUX, 3) \ _ (SOLARIS, 6) \ _ (AIX, 7) \ _ (IRIX, 8) \ _ (FREEBSD, 9) \ _ (COMPAQ_TRU64, 10) \ _ (MODESTO, 11) \ _ (OPENBSD, 12) \ _ (ARM, 97) \ _ (STANDALONE, 255) /* Legal values for type (object file type). */ #define foreach_elf_file_type \ _ (NONE, 0) \ _ (RELOC, 1) \ _ (EXEC, 2) \ _ (SHARED, 3) \ _ (CORE, 4) \ _ (OS_SPECIFIC_LO, 0xfe00) \ _ (OS_SPECIFIC_HI, 0xfeff) \ _ (ARCH_SPECIFIC_LO, 0xff00) \ _ (ARCH_SPECIFIC_HI, 0xffff) /* Legal values for architecture. */ #define foreach_elf_architecture \ _ (NONE, 0) /* No machine */ \ _ (M32, 1) /* AT&T WE 32100 */ \ _ (SPARC, 2) /* SUN SPARC */ \ _ (386, 3) /* Intel 80386 */ \ _ (68K, 4) /* Motorola m68k family */ \ _ (88K, 5) /* Motorola m88k family */ \ _ (860, 7) /* Intel 80860 */ \ _ (MIPS, 8) /* MIPS R3000 big-endian */ \ _ (S370, 9) /* IBM System/370 */ \ _ (MIPS_RS3_LE, 10) /* MIPS R3000 little-endian */ \ _ (PARISC, 15) /* HPPA */ \ _ (VPP500, 17) /* Fujitsu VPP500 */ \ _ (SPARC32PLUS, 18) /* Sun's "v8plus" */ \ _ (960, 19) /* Intel 80960 */ \ _ (PPC, 20) /* PowerPC */ \ _ (PPC64, 21) /* PowerPC 64-bit */ \ _ (S390, 22) /* IBM S390 */ \ _ (V800, 36) /* NEC V800 series */ \ _ (FR20, 37) /* Fujitsu FR20 */ \ _ (RH32, 38) /* TRW RH-32 */ \ _ (RCE, 39) /* Motorola RCE */ \ _ (ARM, 40) /* ARM */ \ _ (FAKE_ALPHA, 41) /* Digital Alpha */ \ _ (SH, 42) /* Hitachi SH */ \ _ (SPARCV9, 43) /* SPARC v9 64-bit */ \ _ (TRICORE, 44) /* Siemens Tricore */ \ _ (ARC, 45) /* Argonaut RISC Core */ \ _ (H8_300, 46) /* Hitachi H8/300 */ \ _ (H8_300H, 47) /* Hitachi H8/300H */ \ _ (H8S, 48) /* Hitachi H8S */ \ _ (H8_500, 49) /* Hitachi H8/500 */ \ _ (IA_64, 50) /* Intel Merced */ \ _ (MIPS_X, 51) /* Stanford MIPS-X */ \ _ (COLDFIRE, 52) /* Motorola Coldfire */ \ _ (68HC12, 53) /* Motorola M68HC12 */ \ _ (MMA, 54) /* Fujitsu MMA Multimedia Accel. */ \ _ (PCP, 55) /* Siemens PCP */ \ _ (NCPU, 56) /* Sony nCPU embedded RISC */ \ _ (NDR1, 57) /* Denso NDR1 microprocessor */ \ _ (STARCORE, 58) /* Motorola Start*Core processor */ \ _ (ME16, 59) /* Toyota ME16 processor */ \ _ (ST100, 60) /* STMicroelectronic ST100 */ \ _ (TINYJ, 61) /* Advanced Logic Corp. Tinyj */ \ _ (X86_64, 62) /* AMD x86-64 architecture */ \ _ (PDSP, 63) /* Sony DSP Processor */ \ _ (FX66, 66) /* Siemens FX66 microcontroller */ \ _ (ST9PLUS, 67) /* STMicroelectronics ST9+ 8/16 mc */ \ _ (ST7, 68) /* STmicroelectronics ST7 8 bit mc */ \ _ (68HC16, 69) /* Motorola MC68HC16 */ \ _ (68HC11, 70) /* Motorola MC68HC11 */ \ _ (68HC08, 71) /* Motorola MC68HC08 */ \ _ (68HC05, 72) /* Motorola MC68HC05 */ \ _ (SVX, 73) /* Silicon Graphics SVx */ \ _ (ST19, 74) /* STMicroelectronics ST19 8 bit mc */ \ _ (VAX, 75) /* Digital VAX */ \ _ (CRIS, 76) /* Axis 32-bit embedded proc. */ \ _ (JAVELIN, 77) /* Infineon 32-bit embedded proc. */ \ _ (FIREPATH, 78) /* Element 14 64-bit DSP Processor */ \ _ (ZSP, 79) /* LSI Logic 16-bit DSP Processor */ \ _ (MMIX, 80) /* Knuth's 64-bit processor */ \ _ (HUANY, 81) /* Harvard machine-independent */ \ _ (PRISM, 82) /* SiTera Prism */ \ _ (AVR, 83) /* Atmel AVR 8-bit microcontroller */ \ _ (FR30, 84) /* Fujitsu FR30 */ \ _ (D10V, 85) /* Mitsubishi D10V */ \ _ (D30V, 86) /* Mitsubishi D30V */ \ _ (V850, 87) /* NEC v850 */ \ _ (M32R, 88) /* Mitsubishi M32R */ \ _ (MN10300, 89) /* Matsushita MN10300 */ \ _ (MN10200, 90) /* Matsushita MN10200 */ \ _ (PJ, 91) /* picoJava */ \ _ (OPENRISC, 92) /* OpenRISC 32-bit processor */ \ _ (ARC_A5, 93) /* ARC Cores Tangent-A5 */ \ _ (XTENSA, 94) /* Tensilica Xtensa Architecture */ \ _ (ALPHA, 0x9026) #define _(f) ELF_##f, typedef enum { foreach_elf_file_class ELF_N_FILE_CLASS, } elf_file_class_t; typedef enum { foreach_elf_data_encoding ELF_N_DATA_ENCODING, } elf_data_encoding_t; #undef _ #define _(f,i) ELF_##f = i, typedef enum { foreach_elf_abi } elf_abi_t; typedef enum { foreach_elf_file_type } elf_file_type_t; #undef _ typedef enum { #define _(f,i) ELF_ARCH_##f = i, foreach_elf_architecture #undef _ } elf_architecture_t; typedef struct { /* 0x7f ELF */ u8 magic[4]; elf_file_class_t file_class:8; elf_data_encoding_t data_encoding:8; u8 file_version_ident; elf_abi_t abi:8; u8 abi_version; u8 pad[7]; elf_file_type_t file_type:16; elf_architecture_t architecture:16; u32 file_version; } elf_first_header_t; /* 32/64 bit file header following basic file header. */ #define foreach_elf32_file_header \ _ (u32, entry_point) \ _ (u32, segment_header_file_offset) \ _ (u32, section_header_file_offset) \ _ (u32, flags) \ _ (u16, n_bytes_this_header) \ _ (u16, segment_header_size) \ _ (u16, segment_header_count) \ _ (u16, section_header_size) \ _ (u16, section_header_count) \ _ (u16, section_header_string_table_index) #define foreach_elf64_file_header \ _ (u64, entry_point) \ _ (u64, segment_header_file_offset) \ _ (u64, section_header_file_offset) \ _ (u32, flags) \ _ (u16, n_bytes_this_header) \ _ (u16, segment_header_size) \ _ (u16, segment_header_count) \ _ (u16, section_header_size) \ _ (u16, section_header_count) \ _ (u16, section_header_string_table_index) /* Section header. */ #define foreach_elf32_section_header \ _ (u32, name) \ _ (u32, type) \ _ (u32, flags) \ _ (u32, exec_address) \ _ (u32, file_offset) \ _ (u32, file_size) \ _ (u32, link) \ _ (u32, additional_info) \ _ (u32, align) \ _ (u32, entry_size) #define foreach_elf64_section_header \ _ (u32, name) \ _ (u32, type) \ _ (u64, flags) \ _ (u64, exec_address) \ _ (u64, file_offset) \ _ (u64, file_size) \ _ (u32, link) \ _ (u32, additional_info) \ _ (u64, align) \ _ (u64, entry_size) /* Program segment header. */ #define foreach_elf32_segment_header \ _ (u32, type) \ _ (u32, file_offset) \ _ (u32, virtual_address) \ _ (u32, physical_address) \ _ (u32, file_size) \ _ (u32, memory_size) \ _ (u32, flags) \ _ (u32, align) #define foreach_elf64_segment_header \ _ (u32, type) \ _ (u32, flags) \ _ (u64, file_offset) \ _ (u64, virtual_address) \ _ (u64, physical_address) \ _ (u64, file_size) \ _ (u64, memory_size) \ _ (u64, align) /* Symbol table. */ #define foreach_elf32_symbol_header \ _ (u32, name) \ _ (u32, value) \ _ (u32, size) \ /* binding upper 4 bits; type lower 4 bits */ \ _ (u8, binding_and_type) \ _ (u8, visibility) \ _ (u16, section_index) #define foreach_elf64_symbol_header \ _ (u32, name) \ _ (u8, binding_and_type) \ _ (u8, visibility) \ _ (u16, section_index) \ _ (u64, value) \ _ (u64, size) #define _(t,f) t f; typedef struct { foreach_elf32_file_header} elf32_file_header_t; typedef struct { foreach_elf64_file_header} elf64_file_header_t; typedef struct { foreach_elf32_section_header} elf32_section_header_t; typedef struct { foreach_elf64_section_header} elf64_section_header_t; typedef struct { foreach_elf32_segment_h
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <vnet/vnet.h>
#include <vnet/api_errno.h>
#include <vnet/ip/ip.h>
#include <vnet/interface.h>

#include <vnet/ipsec/ipsec.h>
#include <vnet/ipsec/ikev2.h>
#include <vnet/ipsec/ikev2_priv.h>

u8 *
format_ikev2_sa_transform (u8 * s, va_list * args)
{
  ikev2_sa_transform_t *tr = va_arg (*args, ikev2_sa_transform_t *);

  if (!tr)
    return s;

  if (tr->type >= IKEV2_TRANSFORM_NUM_TYPES)
    return s;

  s = format (s, "%U:", format_ikev2_transform_type, tr->type);

  switch (tr->type)
    {
    case IKEV2_TRANSFORM_TYPE_ENCR:
      s = format (s, "%U", format_ikev2_transform_encr_type, tr->encr_type);
      break;
    case IKEV2_TRANSFORM_TYPE_PRF:
      s = format (s, "%U", format_ikev2_transform_prf_type, tr->prf_type);
      break;
    case IKEV2_TRANSFORM_TYPE_INTEG:
      s = format (s, "%U", format_ikev2_transform_integ_type, tr->integ_type);
      break;
    case IKEV2_TRANSFORM_TYPE_DH:
      s = format (s, "%U", format_ikev2_transform_dh_type, tr->dh_type);
      break;
    case IKEV2_TRANSFORM_TYPE_ESN:
      s = format (s, "%U", format_ikev2_transform_esn_type, tr->esn_type);
      break;
    default:
      break;
    }

  if (tr->type == IKEV2_TRANSFORM_TYPE_ENCR &&
      tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
    s = format (s, "-%u", tr->key_len * 8);
  else if (vec_len (tr->attrs) == 4 && tr->attrs[0] == 0x80
	   && tr->attrs[1] == 0x0e)
    s = format (s, "-%u", tr->attrs[2] * 256 + tr->attrs[3]);
  else if (vec_len (tr->attrs))
    s = format (s, "(unknown attr %U)", format_hex_bytes,
		tr->attrs, vec_len (tr->attrs));

  return s;
}

#define MACRO_FORMAT(lc)                                \
u8 * format_ikev2_##lc (u8 * s, va_list * args)         \
{                                                       \
  u32 i = va_arg (*args, u32);                          \
  char * t = 0;                                         \
  switch (i) {                                          \
        foreach_ikev2_##lc                              \
      default:                                          \
        return format (s, "unknown (%u)", i);           \
    }                                                   \
  s = format (s, "%s", t);                              \
  return s;                                             \
}

#define MACRO_UNFORMAT(lc)                              \
uword                                                   \
unformat_ikev2_##lc (unformat_input_t * input,          \
                     va_list * args)                    \
{                                                       \
  u32 * r = va_arg (*args, u32 *);                      \
  if (0) ;                                              \
  foreach_ikev2_##lc                                    \
  else                                                  \
    return 0;                                           \
  return 1;                                             \
}

#define _(v,f,str) case IKEV2_AUTH_METHOD_##f: t = str; break;
MACRO_FORMAT (auth_method)
#undef _
#define _(v,f,str) else if (unformat (input, str)) *r = IKEV2_AUTH_METHOD_##f;
  MACRO_UNFORMAT (auth_method)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_type)
#undef _
#define _(v,f,str) else if (unformat (input, str)) *r = IKEV2_TRANSFORM_TYPE_##f;
  MACRO_UNFORMAT (transform_type)
#undef _
#define _(v,f) case IKEV2_NOTIFY_MSG_##f: t = #f; break;
  MACRO_FORMAT (notify_msg_type)
#undef _
#define _(v,f,str) case IKEV2_ID_TYPE_##f: t = str; break;
  MACRO_FORMAT (id_type)
#undef _
#define _(v,f,str) else if (unformat (input, str)) *r = IKEV2_ID_TYPE_##f;
  MACRO_UNFORMAT (id_type)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_ENCR_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_encr_type)
#undef _
#define _(v,f,str) else if (unformat (input, str)) *r = IKEV2_TRANSFORM_ENCR_TYPE_##f;
  MACRO_UNFORMAT (transform_encr_type)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_PRF_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_prf_type)
#undef _
#define _(v,f,str) else if (unformat (input, str)) *r = IKEV2_TRANSFORM_PRF_TYPE_##f;
  MACRO_UNFORMAT (transform_prf_type)