1 /* $OpenBSD: attribute.c,v 1.12 2005/04/08 22:32:09 cloder 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 "attribute.h" 36 #include "conf.h" 37 #include "log.h" 38 #include "isakmp.h" 39 #include "util.h" 40 41 u_int8_t * 42 attribute_set_basic(u_int8_t *buf, u_int16_t type, u_int16_t value) 43 { 44 SET_ISAKMP_ATTR_TYPE(buf, ISAKMP_ATTR_MAKE(1, type)); 45 SET_ISAKMP_ATTR_LENGTH_VALUE(buf, value); 46 return buf + ISAKMP_ATTR_VALUE_OFF; 47 } 48 49 u_int8_t * 50 attribute_set_var(u_int8_t *buf, u_int16_t type, u_int8_t *value, 51 u_int16_t len) 52 { 53 SET_ISAKMP_ATTR_TYPE(buf, ISAKMP_ATTR_MAKE(0, type)); 54 SET_ISAKMP_ATTR_LENGTH_VALUE(buf, len); 55 memcpy(buf + ISAKMP_ATTR_VALUE_OFF, value, len); 56 return buf + ISAKMP_ATTR_VALUE_OFF + len; 57 } 58 59 /* 60 * Execute a function FUNC taking an attribute type, value, length and ARG 61 * as arguments for each attribute in the area of ISAKMP attributes located 62 * at BUF, sized SZ. If any invocation fails, the processing aborts with a 63 * -1 return value. If all goes well return zero. 64 */ 65 int 66 attribute_map(u_int8_t *buf, size_t sz, int (*func)(u_int16_t, u_int8_t *, 67 u_int16_t, void *), void *arg) 68 { 69 u_int8_t *attr; 70 int fmt; 71 u_int16_t type; 72 u_int8_t *value; 73 u_int16_t len; 74 75 for (attr = buf; attr < buf + sz; attr = value + len) { 76 if (attr + ISAKMP_ATTR_VALUE_OFF > buf + sz) 77 return -1; 78 type = GET_ISAKMP_ATTR_TYPE(attr); 79 fmt = ISAKMP_ATTR_FORMAT(type); 80 type = ISAKMP_ATTR_TYPE(type); 81 value = attr + (fmt ? ISAKMP_ATTR_LENGTH_VALUE_OFF 82 : ISAKMP_ATTR_VALUE_OFF); 83 len = (fmt ? ISAKMP_ATTR_LENGTH_VALUE_LEN 84 : GET_ISAKMP_ATTR_LENGTH_VALUE(attr)); 85 if (value + len > buf + sz) 86 return -1; 87 if (func(type, value, len, arg)) 88 return -1; 89 } 90 return 0; 91 } 92 93 int 94 attribute_set_constant(char *section, char *tag, struct constant_map *map, 95 int attr_class, u_int8_t **attr) 96 { 97 char *name; 98 int value; 99 100 name = conf_get_str(section, tag); 101 if (!name) { 102 LOG_DBG((LOG_MISC, 70, 103 "attribute_set_constant: no %s in the %s section", tag, 104 section)); 105 return -1; 106 } 107 value = constant_value(map, name); 108 *attr = attribute_set_basic(*attr, attr_class, value); 109 return 0; 110 } 111