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