summaryrefslogtreecommitdiff
path: root/sbin/isakmpd/attribute.c
blob: 57e46ecd76a7003d49ada000cd49ca5835bf7e0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*	$OpenBSD: attribute.c,v 1.5 1999/02/26 03:33:02 niklas Exp $	*/
/*	$EOM: attribute.c,v 1.7 1999/02/25 11:38:44 niklas Exp $	*/

/*
 * Copyright (c) 1998 Niklas Hallqvist.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Ericsson Radio Systems.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * This code was written under funding by Ericsson Radio Systems.
 */

#include <sys/types.h>
#include <string.h>

#include "sysdep.h"

#include "attribute.h"
#include "conf.h"
#include "log.h"
#include "isakmp.h"
#include "util.h"

u_int8_t *
attribute_set_basic (u_int8_t *buf, u_int16_t type, u_int16_t value)
{
  SET_ISAKMP_ATTR_TYPE (buf, ISAKMP_ATTR_MAKE (1, type));
  SET_ISAKMP_ATTR_LENGTH_VALUE (buf, value);
  return buf + ISAKMP_ATTR_VALUE_OFF;
}

u_int8_t *
attribute_set_var (u_int8_t *buf, u_int16_t type, u_int8_t *value,
		   u_int16_t len)
{
  SET_ISAKMP_ATTR_TYPE (buf, ISAKMP_ATTR_MAKE (0, type));
  SET_ISAKMP_ATTR_LENGTH_VALUE (buf, len);
  memcpy (buf + ISAKMP_ATTR_VALUE_OFF, value, len);
  return buf + ISAKMP_ATTR_VALUE_OFF + len;
}

/* Validate an area of ISAKMP attributes.  */
int
attribute_map (u_int8_t *buf, size_t sz,
	       int (*func) (u_int16_t, u_int8_t *, u_int16_t, void *),
	       void *arg)
{
  u_int8_t *attr;
  int fmt;
  u_int16_t type;
  u_int8_t *value;
  u_int16_t len;

  for (attr = buf; attr < buf + sz; attr = value + len)
    {
      if (attr + ISAKMP_ATTR_VALUE_OFF > buf + sz)
	return -1;
      type = GET_ISAKMP_ATTR_TYPE (attr);
      fmt = ISAKMP_ATTR_FORMAT (type);
      type = ISAKMP_ATTR_TYPE (type);
      value
	= attr + (fmt ? ISAKMP_ATTR_LENGTH_VALUE_OFF : ISAKMP_ATTR_VALUE_OFF);
      len = (fmt ? ISAKMP_ATTR_LENGTH_VALUE_LEN
	     : GET_ISAKMP_ATTR_LENGTH_VALUE (attr));
      if (value + len > buf + sz)
	return -1;
      if (func (type, value, len, arg))
	return -1;
    }
  return 0;
}

int
attribute_set_constant (char *section, char *tag, struct constant_map *map,
			int attr_class, u_int8_t **attr)
{
  char *name;
  int value;

  name = conf_get_str (section, tag);
  if (!name)
    {
      log_debug (LOG_MISC, 70,
		 "attribute_set_constant: no %s in the %s section", tag,
		 section);
      return -1;
    }
  value = constant_value (map, name);
  *attr = attribute_set_basic (*attr, attr_class, value);
  return 0;
}