1 /* $OpenBSD: attribute.c,v 1.9 2003/06/03 14:28:16 ho Exp $ */ 2 /* $EOM: attribute.c,v 1.10 2000/02/20 19:58:36 niklas Exp $ */ 3 4 /* 5 * Copyright (c) 1998, 1999 Niklas Hallqvist. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* 29 * This code was written under funding by Ericsson Radio Systems. 30 */ 31 32 #include <sys/types.h> 33 #include <string.h> 34 35 #include "sysdep.h" 36 37 #include "attribute.h" 38 #include "conf.h" 39 #include "log.h" 40 #include "isakmp.h" 41 #include "util.h" 42 43 u_int8_t * 44 attribute_set_basic (u_int8_t *buf, u_int16_t type, u_int16_t value) 45 { 46 SET_ISAKMP_ATTR_TYPE (buf, ISAKMP_ATTR_MAKE (1, type)); 47 SET_ISAKMP_ATTR_LENGTH_VALUE (buf, value); 48 return buf + ISAKMP_ATTR_VALUE_OFF; 49 } 50 51 u_int8_t * 52 attribute_set_var (u_int8_t *buf, u_int16_t type, u_int8_t *value, 53 u_int16_t len) 54 { 55 SET_ISAKMP_ATTR_TYPE (buf, ISAKMP_ATTR_MAKE (0, type)); 56 SET_ISAKMP_ATTR_LENGTH_VALUE (buf, len); 57 memcpy (buf + ISAKMP_ATTR_VALUE_OFF, value, len); 58 return buf + ISAKMP_ATTR_VALUE_OFF + len; 59 } 60 61 /* 62 * Execute a function FUNC taking an attribute type, value, length and ARG 63 * as arguments for each attribute in the area of ISAKMP attributes located 64 * at BUF, sized SZ. If any invocation fails, the processing aborts with a 65 * -1 return value. If all goes well return zero. 66 */ 67 int 68 attribute_map (u_int8_t *buf, size_t sz, 69 int (*func) (u_int16_t, u_int8_t *, u_int16_t, void *), 70 void *arg) 71 { 72 u_int8_t *attr; 73 int fmt; 74 u_int16_t type; 75 u_int8_t *value; 76 u_int16_t len; 77 78 for (attr = buf; attr < buf + sz; attr = value + len) 79 { 80 if (attr + ISAKMP_ATTR_VALUE_OFF > buf + sz) 81 return -1; 82 type = GET_ISAKMP_ATTR_TYPE (attr); 83 fmt = ISAKMP_ATTR_FORMAT (type); 84 type = ISAKMP_ATTR_TYPE (type); 85 value 86 = attr + (fmt ? ISAKMP_ATTR_LENGTH_VALUE_OFF : ISAKMP_ATTR_VALUE_OFF); 87 len = (fmt ? ISAKMP_ATTR_LENGTH_VALUE_LEN 88 : GET_ISAKMP_ATTR_LENGTH_VALUE (attr)); 89 if (value + len > buf + sz) 90 return -1; 91 if (func (type, value, len, arg)) 92 return -1; 93 } 94 return 0; 95 } 96 97 int 98 attribute_set_constant (char *section, char *tag, struct constant_map *map, 99 int attr_class, u_int8_t **attr) 100 { 101 char *name; 102 int value; 103 104 name = conf_get_str (section, tag); 105 if (!name) 106 { 107 LOG_DBG ((LOG_MISC, 70, 108 "attribute_set_constant: no %s in the %s section", tag, 109 section)); 110 return -1; 111 } 112 value = constant_value (map, name); 113 *attr = attribute_set_basic (*attr, attr_class, value); 114 return 0; 115 } 116