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