All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
net.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 #ifndef _FR_NET_H
17 #define _FR_NET_H
18 /**
19  * $Id: 1190faf415bf91a64419bac896ebdaf6bd0160a6 $
20  *
21  * @file include/net.h
22  * @brief Structures and functions for parsing raw network packets.
23  *
24  * @author Arran Cudbard-Bell <a.cudbardb@freeradius.org>
25  * @copyright 2014 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
26  */
27 RCSIDH(net_h, "$Id: 1190faf415bf91a64419bac896ebdaf6bd0160a6 $")
28 
29 #ifdef HAVE_LIBPCAP
30 # include <pcap.h>
31 #endif
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 /*
37  * If we don't have libpcap, we still need an enumeration of link layers.
38  */
39 #ifndef HAVE_LIBPCAP
40 typedef enum {
47 } fr_dlt;
48 #endif
49 
50 /*
51  * The number of bytes in an ethernet (MAC) address.
52  */
53 #define ETHER_ADDR_LEN 6
54 
55 /*
56  * Length of a DEC/Intel/Xerox or 802.3 Ethernet header.
57  * Note that some compilers may pad "struct ether_header" to
58  * a multiple of 4 *bytes, for example, so "sizeof (struct
59  * ether_header)" may not give the right answer.
60  *
61  * 6 Byte SRC, 6 Byte DST, 2 Byte Ether type, 4 Byte CVID, 4 Byte SVID
62  */
63 #define ETHER_HDR_LEN 22
64 #define IP_HDR_LEN 60
65 
66 /*
67  * The number of bytes in a RADIUS packet header.
68  */
69 #define RADIUS_HDR_LEN 20
70 
71 /*
72  * RADIUS packet length.
73  * RFC 2865, Section 3., subsection 'length' says:
74  * " ... and maximum length is 4096."
75  */
76 #define MAX_RADIUS_LEN 4096
77 #define MIN_RADIUS_LEN 20
78 
79 
80 #define IP_V(ip) (((ip)->ip_vhl & 0xf0) >> 4)
81 #define IP_HL(ip) ((ip)->ip_vhl & 0x0f)
82 
83 #define IP_VHL(v, hl) ((v & 0x0f) << 4) | (hl & 0x0f)
84 
85 #define I_DF 0x4000 //!< Dont fragment flag.
86 #define IP_MF 0x2000 //!< More fragments flag.
87 #define IP_OFFMASK 0x1fff //!< Mask for fragmenting bits.
88 
89 /*
90  * Structure of a DEC/Intel/Xerox or 802.3 Ethernet header.
91  */
92 typedef struct CC_HINT(__packed__) ethernet_header {
93  uint8_t ether_dst[ETHER_ADDR_LEN];
94  uint8_t ether_src[ETHER_ADDR_LEN];
95  uint16_t ether_type;
97 
98 /*
99  * Structure of an internet header, naked of options.
100  */
101 typedef struct CC_HINT(__packed__) ip_header {
102  uint8_t ip_vhl; //!< Header length, version.
103 
104  uint8_t ip_tos; //!< Type of service.
105  uint16_t ip_len; //!< Total length.
106  uint16_t ip_id; //!< identification.
107  uint16_t ip_off; //!< Fragment offset field.
108 
109  uint8_t ip_ttl; //!< Time To Live.
110  uint8_t ip_p; //!< Protocol.
111  uint16_t ip_sum; //!< Checksum.
112  struct in_addr ip_src, ip_dst; //!< Src and Dst address
114 
115 typedef struct CC_HINT(__packed__) ip_header6 {
116  uint32_t ip_vtcfl; //!< Version, traffic class, flow label.
117  uint16_t ip_len; //!< Payload length
118 
119  uint8_t ip_next; //!< Next header (protocol)
120  uint8_t ip_hopl; //!< IP Hop Limit
121 
122  struct in6_addr ip_src, ip_dst; //!< Src and Dst address
124 
125 /*
126  * UDP protocol header.
127  * Per RFC 768, September, 1981.
128  */
129 typedef struct CC_HINT(__packed__) udp_header {
130  uint16_t src; //!< Source port.
131  uint16_t dst; //!< Destination port.
132  uint16_t len; //!< UDP length.
133  uint16_t checksum; //!< UDP checksum.
135 
136 typedef struct CC_HINT(__packed__) radius_packet_t {
137  uint8_t code;
138  uint8_t id;
139  uint8_t length[2];
140  uint8_t vector[AUTH_VECTOR_LEN];
141  uint8_t data[];
143 
146 extern FR_NAME_NUMBER const fr_net_af_table[];
147 
148 bool fr_link_layer_supported(int link_layer);
149 ssize_t fr_link_layer_offset(uint8_t const *data, size_t len, int link_layer);
150 uint16_t fr_udp_checksum(uint8_t const *data, uint16_t len, uint16_t checksum,
151  struct in_addr const src_addr, struct in_addr const dst_addr);
152 int fr_udp_header_check(uint8_t const *data, uint16_t remaining, ip_header_t const *ip);
153 uint16_t fr_ip_header_checksum(uint8_t const *data, uint8_t ihl);
154 #endif /* _FR_NET_H */
Definition: net.h:46
#define RCSIDH(h, id)
Definition: build.h:136
FR_NAME_NUMBER const fr_net_af_table[]
Strings for address families.
Definition: net.c:48
FR_NAME_NUMBER const fr_net_ip_proto_table[]
Strings for L4 protocols.
Definition: net.c:30
uint16_t fr_udp_checksum(uint8_t const *data, uint16_t len, uint16_t checksum, struct in_addr const src_addr, struct in_addr const dst_addr)
Calculate UDP checksum.
Definition: net.c:240
fr_dlt
Definition: net.h:40
bool fr_link_layer_supported(int link_layer)
Check whether fr_link_layer_offset can process a link_layer.
Definition: net.c:61
ip_header6_t
Definition: net.h:123
Definition: net.h:42
ssize_t fr_link_layer_offset(uint8_t const *data, size_t len, int link_layer)
Returns the length of the link layer header.
Definition: net.c:95
udp_header_t
Definition: net.h:134
uint8_t length
Definition: proto_bfd.c:203
Definition: net.h:43
#define AUTH_VECTOR_LEN
Definition: libradius.h:118
radius_packet_t
Definition: net.h:142
int fr_udp_header_check(uint8_t const *data, uint16_t remaining, ip_header_t const *ip)
Check UDP header is valid.
Definition: net.c:186
ethernet_header_t
Definition: net.h:96
uint8_t data[]
Definition: eap_pwd.h:625
uint16_t fr_ip_header_checksum(uint8_t const *data, uint8_t ihl)
Calculate IP header checksum.
Definition: net.c:275
#define ETHER_ADDR_LEN
Definition: net.h:53
Definition: net.h:41
ip_header_t
Definition: net.h:113
FR_NAME_NUMBER const fr_net_sock_type_table[]
Strings for socket types.
Definition: net.c:39
Definition: net.h:44