BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file DHCPIpUdpDecoder.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #include <limits.h>
31 #include <string.h>
32  
33 #include <misc/ipv4_proto.h>
34 #include <misc/udp_proto.h>
35 #include <misc/byteorder.h>
36  
37 #include <dhcpclient/DHCPIpUdpDecoder.h>
38  
39 #define DHCP_SERVER_PORT 67
40 #define DHCP_CLIENT_PORT 68
41  
42 #define IPUDP_HEADER_SIZE (sizeof(struct ipv4_header) + sizeof(struct udp_header))
43  
44 static void input_handler_send (DHCPIpUdpDecoder *o, uint8_t *data, int data_len)
45 {
46 ASSERT(data_len >= 0)
47 DebugObject_Access(&o->d_obj);
48  
49 struct ipv4_header iph;
50 uint8_t *pl;
51 int pl_len;
52  
53 if (!ipv4_check(data, data_len, &iph, &pl, &pl_len)) {
54 goto fail;
55 }
56  
57 if (ntoh8(iph.protocol) != IPV4_PROTOCOL_UDP) {
58 goto fail;
59 }
60  
61 if (pl_len < sizeof(struct udp_header)) {
62 goto fail;
63 }
64 struct udp_header udph;
65 memcpy(&udph, pl, sizeof(udph));
66  
67 if (ntoh16(udph.source_port) != DHCP_SERVER_PORT) {
68 goto fail;
69 }
70  
71 if (ntoh16(udph.dest_port) != DHCP_CLIENT_PORT) {
72 goto fail;
73 }
74  
75 int udph_length = ntoh16(udph.length);
76 if (udph_length < sizeof(udph)) {
77 goto fail;
78 }
79 if (udph_length > data_len - (pl - data)) {
80 goto fail;
81 }
82  
83 if (ntoh16(udph.checksum) != 0) {
84 uint16_t checksum_in_packet = udph.checksum;
85 udph.checksum = 0;
86 uint16_t checksum_computed = udp_checksum(&udph, pl + sizeof(udph), udph_length - sizeof(udph), iph.source_address, iph.destination_address);
87 if (checksum_in_packet != checksum_computed) {
88 goto fail;
89 }
90 }
91  
92 // pass payload to output
93 PacketPassInterface_Sender_Send(o->output, pl + sizeof(udph), udph_length - sizeof(udph));
94  
95 return;
96  
97 fail:
98 PacketPassInterface_Done(&o->input);
99 }
100  
101 static void output_handler_done (DHCPIpUdpDecoder *o)
102 {
103 DebugObject_Access(&o->d_obj);
104  
105 PacketPassInterface_Done(&o->input);
106 }
107  
108 void DHCPIpUdpDecoder_Init (DHCPIpUdpDecoder *o, PacketPassInterface *output, BPendingGroup *pg)
109 {
110 ASSERT(PacketPassInterface_GetMTU(output) <= INT_MAX - IPUDP_HEADER_SIZE)
111  
112 // init arguments
113 o->output = output;
114  
115 // init output
116 PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
117  
118 // init input
119 PacketPassInterface_Init(&o->input, IPUDP_HEADER_SIZE + PacketPassInterface_GetMTU(o->output), (PacketPassInterface_handler_send)input_handler_send, o, pg);
120  
121 DebugObject_Init(&o->d_obj);
122 }
123  
124 void DHCPIpUdpDecoder_Free (DHCPIpUdpDecoder *o)
125 {
126 DebugObject_Free(&o->d_obj);
127  
128 // free input
129 PacketPassInterface_Free(&o->input);
130 }
131  
132 PacketPassInterface * DHCPIpUdpDecoder_GetInput (DHCPIpUdpDecoder *o)
133 {
134 DebugObject_Access(&o->d_obj);
135  
136 return &o->input;
137 }