Home | History | Annotate | Download | only in radius
      1 /*
      2  * RADIUS message processing
      3  * Copyright (c) 2002-2009, 2012, Jouni Malinen <j (at) w1.fi>
      4  *
      5  * This software may be distributed under the terms of the BSD license.
      6  * See README for more details.
      7  */
      8 
      9 #ifndef RADIUS_H
     10 #define RADIUS_H
     11 
     12 /* RFC 2865 - RADIUS */
     13 
     14 #ifdef _MSC_VER
     15 #pragma pack(push, 1)
     16 #endif /* _MSC_VER */
     17 
     18 struct radius_hdr {
     19 	u8 code;
     20 	u8 identifier;
     21 	be16 length; /* including this header */
     22 	u8 authenticator[16];
     23 	/* followed by length-20 octets of attributes */
     24 } STRUCT_PACKED;
     25 
     26 enum { RADIUS_CODE_ACCESS_REQUEST = 1,
     27        RADIUS_CODE_ACCESS_ACCEPT = 2,
     28        RADIUS_CODE_ACCESS_REJECT = 3,
     29        RADIUS_CODE_ACCOUNTING_REQUEST = 4,
     30        RADIUS_CODE_ACCOUNTING_RESPONSE = 5,
     31        RADIUS_CODE_ACCESS_CHALLENGE = 11,
     32        RADIUS_CODE_STATUS_SERVER = 12,
     33        RADIUS_CODE_STATUS_CLIENT = 13,
     34        RADIUS_CODE_DISCONNECT_REQUEST = 40,
     35        RADIUS_CODE_DISCONNECT_ACK = 41,
     36        RADIUS_CODE_DISCONNECT_NAK = 42,
     37        RADIUS_CODE_COA_REQUEST = 43,
     38        RADIUS_CODE_COA_ACK = 44,
     39        RADIUS_CODE_COA_NAK = 45,
     40        RADIUS_CODE_RESERVED = 255
     41 };
     42 
     43 struct radius_attr_hdr {
     44 	u8 type;
     45 	u8 length; /* including this header */
     46 	/* followed by length-2 octets of attribute value */
     47 } STRUCT_PACKED;
     48 
     49 #define RADIUS_MAX_ATTR_LEN (255 - sizeof(struct radius_attr_hdr))
     50 
     51 enum { RADIUS_ATTR_USER_NAME = 1,
     52        RADIUS_ATTR_USER_PASSWORD = 2,
     53        RADIUS_ATTR_NAS_IP_ADDRESS = 4,
     54        RADIUS_ATTR_NAS_PORT = 5,
     55        RADIUS_ATTR_FRAMED_MTU = 12,
     56        RADIUS_ATTR_REPLY_MESSAGE = 18,
     57        RADIUS_ATTR_STATE = 24,
     58        RADIUS_ATTR_CLASS = 25,
     59        RADIUS_ATTR_VENDOR_SPECIFIC = 26,
     60        RADIUS_ATTR_SESSION_TIMEOUT = 27,
     61        RADIUS_ATTR_IDLE_TIMEOUT = 28,
     62        RADIUS_ATTR_TERMINATION_ACTION = 29,
     63        RADIUS_ATTR_CALLED_STATION_ID = 30,
     64        RADIUS_ATTR_CALLING_STATION_ID = 31,
     65        RADIUS_ATTR_NAS_IDENTIFIER = 32,
     66        RADIUS_ATTR_PROXY_STATE = 33,
     67        RADIUS_ATTR_ACCT_STATUS_TYPE = 40,
     68        RADIUS_ATTR_ACCT_DELAY_TIME = 41,
     69        RADIUS_ATTR_ACCT_INPUT_OCTETS = 42,
     70        RADIUS_ATTR_ACCT_OUTPUT_OCTETS = 43,
     71        RADIUS_ATTR_ACCT_SESSION_ID = 44,
     72        RADIUS_ATTR_ACCT_AUTHENTIC = 45,
     73        RADIUS_ATTR_ACCT_SESSION_TIME = 46,
     74        RADIUS_ATTR_ACCT_INPUT_PACKETS = 47,
     75        RADIUS_ATTR_ACCT_OUTPUT_PACKETS = 48,
     76        RADIUS_ATTR_ACCT_TERMINATE_CAUSE = 49,
     77        RADIUS_ATTR_ACCT_MULTI_SESSION_ID = 50,
     78        RADIUS_ATTR_ACCT_LINK_COUNT = 51,
     79        RADIUS_ATTR_ACCT_INPUT_GIGAWORDS = 52,
     80        RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS = 53,
     81        RADIUS_ATTR_EVENT_TIMESTAMP = 55,
     82        RADIUS_ATTR_NAS_PORT_TYPE = 61,
     83        RADIUS_ATTR_TUNNEL_TYPE = 64,
     84        RADIUS_ATTR_TUNNEL_MEDIUM_TYPE = 65,
     85        RADIUS_ATTR_TUNNEL_PASSWORD = 69,
     86        RADIUS_ATTR_CONNECT_INFO = 77,
     87        RADIUS_ATTR_EAP_MESSAGE = 79,
     88        RADIUS_ATTR_MESSAGE_AUTHENTICATOR = 80,
     89        RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID = 81,
     90        RADIUS_ATTR_ACCT_INTERIM_INTERVAL = 85,
     91        RADIUS_ATTR_CHARGEABLE_USER_IDENTITY = 89,
     92        RADIUS_ATTR_NAS_IPV6_ADDRESS = 95,
     93        RADIUS_ATTR_ERROR_CAUSE = 101
     94 };
     95 
     96 
     97 /* Termination-Action */
     98 #define RADIUS_TERMINATION_ACTION_DEFAULT 0
     99 #define RADIUS_TERMINATION_ACTION_RADIUS_REQUEST 1
    100 
    101 /* NAS-Port-Type */
    102 #define RADIUS_NAS_PORT_TYPE_IEEE_802_11 19
    103 
    104 /* Acct-Status-Type */
    105 #define RADIUS_ACCT_STATUS_TYPE_START 1
    106 #define RADIUS_ACCT_STATUS_TYPE_STOP 2
    107 #define RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE 3
    108 #define RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON 7
    109 #define RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF 8
    110 
    111 /* Acct-Authentic */
    112 #define RADIUS_ACCT_AUTHENTIC_RADIUS 1
    113 #define RADIUS_ACCT_AUTHENTIC_LOCAL 2
    114 #define RADIUS_ACCT_AUTHENTIC_REMOTE 3
    115 
    116 /* Acct-Terminate-Cause */
    117 #define RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST 1
    118 #define RADIUS_ACCT_TERMINATE_CAUSE_LOST_CARRIER 2
    119 #define RADIUS_ACCT_TERMINATE_CAUSE_LOST_SERVICE 3
    120 #define RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT 4
    121 #define RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT 5
    122 #define RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_RESET 6
    123 #define RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT 7
    124 #define RADIUS_ACCT_TERMINATE_CAUSE_PORT_ERROR 8
    125 #define RADIUS_ACCT_TERMINATE_CAUSE_NAS_ERROR 9
    126 #define RADIUS_ACCT_TERMINATE_CAUSE_NAS_REQUEST 10
    127 #define RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT 11
    128 #define RADIUS_ACCT_TERMINATE_CAUSE_PORT_UNNEEDED 12
    129 #define RADIUS_ACCT_TERMINATE_CAUSE_PORT_PREEMPTED 13
    130 #define RADIUS_ACCT_TERMINATE_CAUSE_PORT_SUSPENDED 14
    131 #define RADIUS_ACCT_TERMINATE_CAUSE_SERVICE_UNAVAILABLE 15
    132 #define RADIUS_ACCT_TERMINATE_CAUSE_CALLBACK 16
    133 #define RADIUS_ACCT_TERMINATE_CAUSE_USER_ERROR 17
    134 #define RADIUS_ACCT_TERMINATE_CAUSE_HOST_REQUEST 18
    135 
    136 #define RADIUS_TUNNEL_TAGS 32
    137 
    138 /* Tunnel-Type */
    139 #define RADIUS_TUNNEL_TYPE_PPTP 1
    140 #define RADIUS_TUNNEL_TYPE_L2TP 3
    141 #define RADIUS_TUNNEL_TYPE_IPIP 7
    142 #define RADIUS_TUNNEL_TYPE_GRE 10
    143 #define RADIUS_TUNNEL_TYPE_VLAN 13
    144 
    145 /* Tunnel-Medium-Type */
    146 #define RADIUS_TUNNEL_MEDIUM_TYPE_IPV4 1
    147 #define RADIUS_TUNNEL_MEDIUM_TYPE_IPV6 2
    148 #define RADIUS_TUNNEL_MEDIUM_TYPE_802 6
    149 
    150 
    151 struct radius_attr_vendor {
    152 	u8 vendor_type;
    153 	u8 vendor_length;
    154 } STRUCT_PACKED;
    155 
    156 #define RADIUS_VENDOR_ID_CISCO 9
    157 #define RADIUS_CISCO_AV_PAIR 1
    158 
    159 /* RFC 2548 - Microsoft Vendor-specific RADIUS Attributes */
    160 #define RADIUS_VENDOR_ID_MICROSOFT 311
    161 
    162 enum { RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY = 16,
    163        RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY = 17
    164 };
    165 
    166 #ifdef _MSC_VER
    167 #pragma pack(pop)
    168 #endif /* _MSC_VER */
    169 
    170 struct radius_ms_mppe_keys {
    171 	u8 *send;
    172 	size_t send_len;
    173 	u8 *recv;
    174 	size_t recv_len;
    175 };
    176 
    177 
    178 struct radius_msg;
    179 
    180 /* Default size to be allocated for new RADIUS messages */
    181 #define RADIUS_DEFAULT_MSG_SIZE 1024
    182 
    183 /* Default size to be allocated for attribute array */
    184 #define RADIUS_DEFAULT_ATTR_COUNT 16
    185 
    186 
    187 /* MAC address ASCII format for IEEE 802.1X use
    188  * (draft-congdon-radius-8021x-20.txt) */
    189 #define RADIUS_802_1X_ADDR_FORMAT "%02X-%02X-%02X-%02X-%02X-%02X"
    190 /* MAC address ASCII format for non-802.1X use */
    191 #define RADIUS_ADDR_FORMAT "%02x%02x%02x%02x%02x%02x"
    192 
    193 struct radius_hdr * radius_msg_get_hdr(struct radius_msg *msg);
    194 struct wpabuf * radius_msg_get_buf(struct radius_msg *msg);
    195 struct radius_msg * radius_msg_new(u8 code, u8 identifier);
    196 void radius_msg_free(struct radius_msg *msg);
    197 void radius_msg_dump(struct radius_msg *msg);
    198 int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
    199 		      size_t secret_len);
    200 int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
    201 			  size_t secret_len, const u8 *req_authenticator);
    202 int radius_msg_finish_das_resp(struct radius_msg *msg, const u8 *secret,
    203 			       size_t secret_len,
    204 			       const struct radius_hdr *req_hdr);
    205 void radius_msg_finish_acct(struct radius_msg *msg, const u8 *secret,
    206 			    size_t secret_len);
    207 int radius_msg_verify_acct_req(struct radius_msg *msg, const u8 *secret,
    208 			       size_t secret_len);
    209 int radius_msg_verify_das_req(struct radius_msg *msg, const u8 *secret,
    210 			       size_t secret_len);
    211 struct radius_attr_hdr * radius_msg_add_attr(struct radius_msg *msg, u8 type,
    212 					     const u8 *data, size_t data_len);
    213 struct radius_msg * radius_msg_parse(const u8 *data, size_t len);
    214 int radius_msg_add_eap(struct radius_msg *msg, const u8 *data,
    215 		       size_t data_len);
    216 struct wpabuf * radius_msg_get_eap(struct radius_msg *msg);
    217 int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
    218 		      size_t secret_len, struct radius_msg *sent_msg,
    219 		      int auth);
    220 int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
    221 			       size_t secret_len, const u8 *req_auth);
    222 int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
    223 			 u8 type);
    224 void radius_msg_make_authenticator(struct radius_msg *msg,
    225 				   const u8 *data, size_t len);
    226 struct radius_ms_mppe_keys *
    227 radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
    228 		       const u8 *secret, size_t secret_len);
    229 struct radius_ms_mppe_keys *
    230 radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
    231 			  const u8 *secret, size_t secret_len);
    232 int radius_msg_add_mppe_keys(struct radius_msg *msg,
    233 			     const u8 *req_authenticator,
    234 			     const u8 *secret, size_t secret_len,
    235 			     const u8 *send_key, size_t send_key_len,
    236 			     const u8 *recv_key, size_t recv_key_len);
    237 struct radius_attr_hdr *
    238 radius_msg_add_attr_user_password(struct radius_msg *msg,
    239 				  const u8 *data, size_t data_len,
    240 				  const u8 *secret, size_t secret_len);
    241 int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len);
    242 int radius_msg_get_vlanid(struct radius_msg *msg);
    243 char * radius_msg_get_tunnel_password(struct radius_msg *msg, int *keylen,
    244 				      const u8 *secret, size_t secret_len,
    245 				      struct radius_msg *sent_msg, size_t n);
    246 
    247 static inline int radius_msg_add_attr_int32(struct radius_msg *msg, u8 type,
    248 					    u32 value)
    249 {
    250 	u32 val = htonl(value);
    251 	return radius_msg_add_attr(msg, type, (u8 *) &val, 4) != NULL;
    252 }
    253 
    254 static inline int radius_msg_get_attr_int32(struct radius_msg *msg, u8 type,
    255 					    u32 *value)
    256 {
    257 	u32 val;
    258 	int res;
    259 	res = radius_msg_get_attr(msg, type, (u8 *) &val, 4);
    260 	if (res != 4)
    261 		return -1;
    262 
    263 	*value = ntohl(val);
    264 	return 0;
    265 }
    266 int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
    267 			    size_t *len, const u8 *start);
    268 int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len);
    269 
    270 
    271 struct radius_attr_data {
    272 	u8 *data;
    273 	size_t len;
    274 };
    275 
    276 struct radius_class_data {
    277 	struct radius_attr_data *attr;
    278 	size_t count;
    279 };
    280 
    281 void radius_free_class(struct radius_class_data *c);
    282 int radius_copy_class(struct radius_class_data *dst,
    283 		      const struct radius_class_data *src);
    284 
    285 u8 radius_msg_find_unlisted_attr(struct radius_msg *msg, u8 *attrs);
    286 
    287 #endif /* RADIUS_H */
    288