1 | /*
|
---|
2 | * Copyright (c) 2012 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup udp
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file UDP PDU encoding and decoding
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <bitops.h>
|
---|
38 | #include <byteorder.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <mem.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <inet/addr.h>
|
---|
43 | #include "msg.h"
|
---|
44 | #include "pdu.h"
|
---|
45 | #include "std.h"
|
---|
46 | #include "udp_type.h"
|
---|
47 |
|
---|
48 | #define UDP_CHECKSUM_INIT 0xffff
|
---|
49 |
|
---|
50 | /** One's complement addition.
|
---|
51 | *
|
---|
52 | * Result is a + b + carry.
|
---|
53 | */
|
---|
54 | static uint16_t udp_ocadd16(uint16_t a, uint16_t b)
|
---|
55 | {
|
---|
56 | uint32_t s;
|
---|
57 |
|
---|
58 | s = (uint32_t)a + (uint32_t)b;
|
---|
59 | return (s & 0xffff) + (s >> 16);
|
---|
60 | }
|
---|
61 |
|
---|
62 | static uint16_t udp_checksum_calc(uint16_t ivalue, void *data, size_t size)
|
---|
63 | {
|
---|
64 | uint16_t sum;
|
---|
65 | uint16_t w;
|
---|
66 | size_t words, i;
|
---|
67 | uint8_t *bdata;
|
---|
68 |
|
---|
69 | sum = ~ivalue;
|
---|
70 | words = size / 2;
|
---|
71 | bdata = (uint8_t *)data;
|
---|
72 |
|
---|
73 | for (i = 0; i < words; i++) {
|
---|
74 | w = ((uint16_t)bdata[2*i] << 8) | bdata[2*i + 1];
|
---|
75 | sum = udp_ocadd16(sum, w);
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (size % 2 != 0) {
|
---|
79 | w = ((uint16_t)bdata[2*words] << 8);
|
---|
80 | sum = udp_ocadd16(sum, w);
|
---|
81 | }
|
---|
82 |
|
---|
83 | return ~sum;
|
---|
84 | }
|
---|
85 |
|
---|
86 | static ip_ver_t udp_phdr_setup(udp_pdu_t *pdu, udp_phdr_t *phdr,
|
---|
87 | udp_phdr6_t *phdr6)
|
---|
88 | {
|
---|
89 | addr32_t src_v4;
|
---|
90 | addr128_t src_v6;
|
---|
91 | ip_ver_t src_ver = inet_addr_get(&pdu->src, &src_v4, &src_v6);
|
---|
92 |
|
---|
93 | addr32_t dest_v4;
|
---|
94 | addr128_t dest_v6;
|
---|
95 | ip_ver_t dest_ver = inet_addr_get(&pdu->dest, &dest_v4, &dest_v6);
|
---|
96 |
|
---|
97 | assert(src_ver == dest_ver);
|
---|
98 |
|
---|
99 | switch (src_ver) {
|
---|
100 | case ip_v4:
|
---|
101 | phdr->src_addr = host2uint32_t_be(src_v4);
|
---|
102 | phdr->dest_addr = host2uint32_t_be(dest_v4);
|
---|
103 | phdr->zero = 0;
|
---|
104 | phdr->protocol = IP_PROTO_UDP;
|
---|
105 | phdr->udp_length = host2uint16_t_be(pdu->data_size);
|
---|
106 | break;
|
---|
107 | case ip_v6:
|
---|
108 | host2addr128_t_be(src_v6, phdr6->src_addr);
|
---|
109 | host2addr128_t_be(dest_v6, phdr6->dest_addr);
|
---|
110 | phdr6->udp_length = host2uint32_t_be(pdu->data_size);
|
---|
111 | memset(phdr6->zeroes, 0, 3);
|
---|
112 | phdr6->next = IP_PROTO_UDP;
|
---|
113 | break;
|
---|
114 | default:
|
---|
115 | assert(false);
|
---|
116 | }
|
---|
117 |
|
---|
118 | return src_ver;
|
---|
119 | }
|
---|
120 |
|
---|
121 | udp_pdu_t *udp_pdu_new(void)
|
---|
122 | {
|
---|
123 | return calloc(1, sizeof(udp_pdu_t));
|
---|
124 | }
|
---|
125 |
|
---|
126 | void udp_pdu_delete(udp_pdu_t *pdu)
|
---|
127 | {
|
---|
128 | free(pdu->data);
|
---|
129 | free(pdu);
|
---|
130 | }
|
---|
131 |
|
---|
132 | static uint16_t udp_pdu_checksum_calc(udp_pdu_t *pdu)
|
---|
133 | {
|
---|
134 | uint16_t cs_phdr;
|
---|
135 | udp_phdr_t phdr;
|
---|
136 | udp_phdr6_t phdr6;
|
---|
137 |
|
---|
138 | ip_ver_t ver = udp_phdr_setup(pdu, &phdr, &phdr6);
|
---|
139 | switch (ver) {
|
---|
140 | case ip_v4:
|
---|
141 | cs_phdr = udp_checksum_calc(UDP_CHECKSUM_INIT, (void *) &phdr,
|
---|
142 | sizeof(udp_phdr_t));
|
---|
143 | break;
|
---|
144 | case ip_v6:
|
---|
145 | cs_phdr = udp_checksum_calc(UDP_CHECKSUM_INIT, (void *) &phdr6,
|
---|
146 | sizeof(udp_phdr6_t));
|
---|
147 | break;
|
---|
148 | default:
|
---|
149 | assert(false);
|
---|
150 | }
|
---|
151 |
|
---|
152 | return udp_checksum_calc(cs_phdr, pdu->data, pdu->data_size);
|
---|
153 | }
|
---|
154 |
|
---|
155 | static void udp_pdu_set_checksum(udp_pdu_t *pdu, uint16_t checksum)
|
---|
156 | {
|
---|
157 | udp_header_t *hdr;
|
---|
158 |
|
---|
159 | hdr = (udp_header_t *)pdu->data;
|
---|
160 | hdr->checksum = host2uint16_t_be(checksum);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /** Decode incoming PDU */
|
---|
164 | int udp_pdu_decode(udp_pdu_t *pdu, udp_sockpair_t *sp, udp_msg_t **msg)
|
---|
165 | {
|
---|
166 | udp_msg_t *nmsg;
|
---|
167 | udp_header_t *hdr;
|
---|
168 | void *text;
|
---|
169 | size_t text_size;
|
---|
170 | uint16_t length;
|
---|
171 | uint16_t checksum;
|
---|
172 |
|
---|
173 | if (pdu->data_size < sizeof(udp_header_t))
|
---|
174 | return EINVAL;
|
---|
175 |
|
---|
176 | text = pdu->data + sizeof(udp_header_t);
|
---|
177 | text_size = pdu->data_size - sizeof(udp_header_t);
|
---|
178 |
|
---|
179 | hdr = (udp_header_t *)pdu->data;
|
---|
180 |
|
---|
181 | sp->foreign.port = uint16_t_be2host(hdr->src_port);
|
---|
182 | sp->foreign.addr = pdu->src;
|
---|
183 | sp->local.port = uint16_t_be2host(hdr->dest_port);
|
---|
184 | sp->local.addr = pdu->dest;
|
---|
185 |
|
---|
186 | length = uint16_t_be2host(hdr->length);
|
---|
187 | checksum = uint16_t_be2host(hdr->checksum);
|
---|
188 | (void) checksum;
|
---|
189 |
|
---|
190 | if (length < sizeof(udp_header_t) ||
|
---|
191 | length > sizeof(udp_header_t) + text_size)
|
---|
192 | return EINVAL;
|
---|
193 |
|
---|
194 | nmsg = udp_msg_new();
|
---|
195 | if (nmsg == NULL)
|
---|
196 | return ENOMEM;
|
---|
197 |
|
---|
198 | nmsg->data = text;
|
---|
199 | nmsg->data_size = length - sizeof(udp_header_t);
|
---|
200 |
|
---|
201 | *msg = nmsg;
|
---|
202 | return EOK;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /** Encode outgoing PDU */
|
---|
206 | int udp_pdu_encode(udp_sockpair_t *sp, udp_msg_t *msg, udp_pdu_t **pdu)
|
---|
207 | {
|
---|
208 | udp_pdu_t *npdu;
|
---|
209 | udp_header_t *hdr;
|
---|
210 | uint16_t checksum;
|
---|
211 |
|
---|
212 | npdu = udp_pdu_new();
|
---|
213 | if (npdu == NULL)
|
---|
214 | return ENOMEM;
|
---|
215 |
|
---|
216 | npdu->iplink = sp->iplink;
|
---|
217 | npdu->src = sp->local.addr;
|
---|
218 | npdu->dest = sp->foreign.addr;
|
---|
219 |
|
---|
220 | npdu->data_size = sizeof(udp_header_t) + msg->data_size;
|
---|
221 | npdu->data = calloc(1, npdu->data_size);
|
---|
222 | if (npdu->data == NULL) {
|
---|
223 | udp_pdu_delete(npdu);
|
---|
224 | return ENOMEM;
|
---|
225 | }
|
---|
226 |
|
---|
227 | hdr = (udp_header_t *)npdu->data;
|
---|
228 | hdr->src_port = host2uint16_t_be(sp->local.port);
|
---|
229 | hdr->dest_port = host2uint16_t_be(sp->foreign.port);
|
---|
230 | hdr->length = host2uint16_t_be(npdu->data_size);
|
---|
231 | hdr->checksum = 0;
|
---|
232 |
|
---|
233 | memcpy((uint8_t *)npdu->data + sizeof(udp_header_t), msg->data,
|
---|
234 | msg->data_size);
|
---|
235 |
|
---|
236 | /* Checksum calculation */
|
---|
237 | checksum = udp_pdu_checksum_calc(npdu);
|
---|
238 | udp_pdu_set_checksum(npdu, checksum);
|
---|
239 |
|
---|
240 | *pdu = npdu;
|
---|
241 | return EOK;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * @}
|
---|
246 | */
|
---|