| [21580dd] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Lukas Mejdrech
|
|---|
| 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 |
|
|---|
| [84c20da] | 29 | /** @addtogroup libnet
|
|---|
| 30 | * @{
|
|---|
| [21580dd] | 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /** @file
|
|---|
| [84c20da] | 34 | * Transport layer common functions implementation.
|
|---|
| 35 | * @see tl_common.h
|
|---|
| [21580dd] | 36 | */
|
|---|
| 37 |
|
|---|
| [84c20da] | 38 | #include <tl_common.h>
|
|---|
| 39 | #include <packet_client.h>
|
|---|
| 40 | #include <packet_remote.h>
|
|---|
| 41 | #include <icmp_interface.h>
|
|---|
| 42 | #include <ip_remote.h>
|
|---|
| 43 | #include <ip_interface.h>
|
|---|
| 44 | #include <tl_interface.h>
|
|---|
| 45 |
|
|---|
| [058edb6] | 46 | #include <net/socket_codes.h>
|
|---|
| [e4554d4] | 47 | #include <net/in.h>
|
|---|
| 48 | #include <net/in6.h>
|
|---|
| 49 | #include <net/inet.h>
|
|---|
| [84c20da] | 50 | #include <net/device.h>
|
|---|
| 51 | #include <net/packet.h>
|
|---|
| 52 |
|
|---|
| [21580dd] | 53 | #include <async.h>
|
|---|
| 54 | #include <ipc/services.h>
|
|---|
| [e98b1d5] | 55 | #include <errno.h>
|
|---|
| [c5b59ce] | 56 | #include <err.h>
|
|---|
| [21580dd] | 57 |
|
|---|
| [aadf01e] | 58 | DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t);
|
|---|
| [21580dd] | 59 |
|
|---|
| [84c20da] | 60 | /** Gets the address port.
|
|---|
| 61 | *
|
|---|
| 62 | * Supports AF_INET and AF_INET6 address families.
|
|---|
| 63 | *
|
|---|
| 64 | * @param[in,out] addr The address to be updated.
|
|---|
| 65 | * @param[in] addrlen The address length.
|
|---|
| 66 | * @param[out] port The set port.
|
|---|
| 67 | * @returns EOK on success.
|
|---|
| 68 | * @returns EINVAL if the address length does not match the address
|
|---|
| 69 | * family.
|
|---|
| 70 | * @returns EAFNOSUPPORT if the address family is not supported.
|
|---|
| 71 | */
|
|---|
| [2fa0ad9] | 72 | int
|
|---|
| 73 | tl_get_address_port(const struct sockaddr *addr, int addrlen, uint16_t *port)
|
|---|
| 74 | {
|
|---|
| 75 | const struct sockaddr_in *address_in;
|
|---|
| 76 | const struct sockaddr_in6 *address_in6;
|
|---|
| [21580dd] | 77 |
|
|---|
| [2fa0ad9] | 78 | if ((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr)))
|
|---|
| [aadf01e] | 79 | return EINVAL;
|
|---|
| [2fa0ad9] | 80 |
|
|---|
| 81 | switch (addr->sa_family) {
|
|---|
| 82 | case AF_INET:
|
|---|
| 83 | if (addrlen != sizeof(struct sockaddr_in))
|
|---|
| 84 | return EINVAL;
|
|---|
| 85 |
|
|---|
| 86 | address_in = (struct sockaddr_in *) addr;
|
|---|
| 87 | *port = ntohs(address_in->sin_port);
|
|---|
| 88 | break;
|
|---|
| [84c20da] | 89 |
|
|---|
| [2fa0ad9] | 90 | case AF_INET6:
|
|---|
| 91 | if (addrlen != sizeof(struct sockaddr_in6))
|
|---|
| [84c20da] | 92 | return EINVAL;
|
|---|
| [2fa0ad9] | 93 |
|
|---|
| 94 | address_in6 = (struct sockaddr_in6 *) addr;
|
|---|
| 95 | *port = ntohs(address_in6->sin6_port);
|
|---|
| 96 | break;
|
|---|
| [84c20da] | 97 |
|
|---|
| [2fa0ad9] | 98 | default:
|
|---|
| 99 | return EAFNOSUPPORT;
|
|---|
| [21580dd] | 100 | }
|
|---|
| [2fa0ad9] | 101 |
|
|---|
| [21580dd] | 102 | return EOK;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| [14f1db0] | 105 | /** Get IP packet dimensions.
|
|---|
| 106 | *
|
|---|
| 107 | * Try to search a cache and query the IP module if not found.
|
|---|
| 108 | * The reply is cached then.
|
|---|
| 109 | *
|
|---|
| [84c20da] | 110 | * @param[in] ip_phone The IP moduel phone for (semi)remote calls.
|
|---|
| 111 | * @param[in] packet_dimensions The packet dimensions cache.
|
|---|
| 112 | * @param[in] device_id The device identifier.
|
|---|
| 113 | * @param[out] packet_dimension The IP packet dimensions.
|
|---|
| 114 | * @return EOK on success.
|
|---|
| 115 | * @return EBADMEM if the packet_dimension parameter is NULL.
|
|---|
| 116 | * @return ENOMEM if there is not enough memory left.
|
|---|
| 117 | * @return EINVAL if the packet_dimensions cache is not valid.
|
|---|
| 118 | * @return Other codes as defined for the ip_packet_size_req()
|
|---|
| 119 | * function.
|
|---|
| [14f1db0] | 120 | */
|
|---|
| [84c20da] | 121 | int
|
|---|
| 122 | tl_get_ip_packet_dimension(int ip_phone,
|
|---|
| [14f1db0] | 123 | packet_dimensions_ref packet_dimensions, device_id_t device_id,
|
|---|
| 124 | packet_dimension_ref *packet_dimension)
|
|---|
| 125 | {
|
|---|
| [21580dd] | 126 | ERROR_DECLARE;
|
|---|
| [14f1db0] | 127 |
|
|---|
| 128 | if (!packet_dimension)
|
|---|
| [aadf01e] | 129 | return EBADMEM;
|
|---|
| [14f1db0] | 130 |
|
|---|
| [2fa0ad9] | 131 | *packet_dimension = packet_dimensions_find(packet_dimensions,
|
|---|
| 132 | device_id);
|
|---|
| [14f1db0] | 133 | if (!*packet_dimension) {
|
|---|
| 134 | /* Ask for and remember them if not found */
|
|---|
| 135 | *packet_dimension = malloc(sizeof(**packet_dimension));
|
|---|
| 136 | if(!*packet_dimension)
|
|---|
| [aadf01e] | 137 | return ENOMEM;
|
|---|
| [14f1db0] | 138 |
|
|---|
| 139 | if (ERROR_OCCURRED(ip_packet_size_req(ip_phone, device_id,
|
|---|
| 140 | *packet_dimension))) {
|
|---|
| [aadf01e] | 141 | free(*packet_dimension);
|
|---|
| [21580dd] | 142 | return ERROR_CODE;
|
|---|
| 143 | }
|
|---|
| [14f1db0] | 144 |
|
|---|
| 145 | ERROR_CODE = packet_dimensions_add(packet_dimensions, device_id,
|
|---|
| 146 | *packet_dimension);
|
|---|
| 147 | if (ERROR_CODE < 0) {
|
|---|
| [aadf01e] | 148 | free(*packet_dimension);
|
|---|
| [21580dd] | 149 | return ERROR_CODE;
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| [14f1db0] | 152 |
|
|---|
| [21580dd] | 153 | return EOK;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| [84c20da] | 156 | /** Updates IP device packet dimensions cache.
|
|---|
| 157 | *
|
|---|
| 158 | * @param[in,out] packet_dimensions The packet dimensions cache.
|
|---|
| 159 | * @param[in] device_id The device identifier.
|
|---|
| 160 | * @param[in] content The new maximum content size.
|
|---|
| 161 | * @returns EOK on success.
|
|---|
| 162 | * @return ENOENT if the packet dimension is not cached.
|
|---|
| 163 | */
|
|---|
| [2fa0ad9] | 164 | int
|
|---|
| 165 | tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions,
|
|---|
| 166 | device_id_t device_id, size_t content)
|
|---|
| 167 | {
|
|---|
| [aadf01e] | 168 | packet_dimension_ref packet_dimension;
|
|---|
| [b4ff8a6d] | 169 |
|
|---|
| [aadf01e] | 170 | packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
|
|---|
| [2fa0ad9] | 171 | if (!packet_dimension)
|
|---|
| [aadf01e] | 172 | return ENOENT;
|
|---|
| [84c20da] | 173 |
|
|---|
| [b4ff8a6d] | 174 | packet_dimension->content = content;
|
|---|
| [2fa0ad9] | 175 |
|
|---|
| 176 | if (device_id != DEVICE_INVALID_ID) {
|
|---|
| 177 | packet_dimension = packet_dimensions_find(packet_dimensions,
|
|---|
| 178 | DEVICE_INVALID_ID);
|
|---|
| 179 |
|
|---|
| 180 | if (packet_dimension) {
|
|---|
| 181 | if (packet_dimension->content >= content)
|
|---|
| [b4ff8a6d] | 182 | packet_dimension->content = content;
|
|---|
| [2fa0ad9] | 183 | else
|
|---|
| 184 | packet_dimensions_exclude(packet_dimensions,
|
|---|
| 185 | DEVICE_INVALID_ID);
|
|---|
| [b4ff8a6d] | 186 | }
|
|---|
| 187 | }
|
|---|
| [2fa0ad9] | 188 |
|
|---|
| [b4ff8a6d] | 189 | return EOK;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| [84c20da] | 192 | /** Sets the address port.
|
|---|
| 193 | *
|
|---|
| 194 | * Supports AF_INET and AF_INET6 address families.
|
|---|
| 195 | *
|
|---|
| 196 | * @param[in,out] addr The address to be updated.
|
|---|
| 197 | * @param[in] addrlen The address length.
|
|---|
| 198 | * @param[in] port The port to be set.
|
|---|
| 199 | * @returns EOK on success.
|
|---|
| 200 | * @returns EINVAL if the address length does not match the address
|
|---|
| 201 | * family.
|
|---|
| 202 | * @returns EAFNOSUPPORT if the address family is not supported.
|
|---|
| 203 | */
|
|---|
| [7c8267b] | 204 | int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port)
|
|---|
| 205 | {
|
|---|
| [2fa0ad9] | 206 | struct sockaddr_in *address_in;
|
|---|
| 207 | struct sockaddr_in6 *address_in6;
|
|---|
| [aadf01e] | 208 | size_t length;
|
|---|
| [21580dd] | 209 |
|
|---|
| [7c8267b] | 210 | if (addrlen < 0)
|
|---|
| [aadf01e] | 211 | return EINVAL;
|
|---|
| [7c8267b] | 212 |
|
|---|
| [aadf01e] | 213 | length = (size_t) addrlen;
|
|---|
| [7c8267b] | 214 | if (length < sizeof(struct sockaddr))
|
|---|
| [aadf01e] | 215 | return EINVAL;
|
|---|
| [7c8267b] | 216 |
|
|---|
| 217 | switch (addr->sa_family) {
|
|---|
| 218 | case AF_INET:
|
|---|
| 219 | if (length != sizeof(struct sockaddr_in))
|
|---|
| 220 | return EINVAL;
|
|---|
| 221 | address_in = (struct sockaddr_in *) addr;
|
|---|
| 222 | address_in->sin_port = htons(port);
|
|---|
| 223 | return EOK;
|
|---|
| [84c20da] | 224 |
|
|---|
| [7c8267b] | 225 | case AF_INET6:
|
|---|
| 226 | if (length != sizeof(struct sockaddr_in6))
|
|---|
| [aadf01e] | 227 | return EINVAL;
|
|---|
| [7c8267b] | 228 | address_in6 = (struct sockaddr_in6 *) addr;
|
|---|
| 229 | address_in6->sin6_port = htons(port);
|
|---|
| 230 | return EOK;
|
|---|
| [84c20da] | 231 |
|
|---|
| [7c8267b] | 232 | default:
|
|---|
| 233 | return EAFNOSUPPORT;
|
|---|
| [21580dd] | 234 | }
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| [84c20da] | 237 | /** Prepares the packet for ICMP error notification.
|
|---|
| 238 | *
|
|---|
| 239 | * Keeps the first packet and releases all the others.
|
|---|
| 240 | * Releases all the packets on error.
|
|---|
| 241 | *
|
|---|
| 242 | * @param[in] packet_phone The packet server module phone.
|
|---|
| 243 | * @param[in] icmp_phone The ICMP module phone.
|
|---|
| 244 | * @param[in] packet The packet to be send.
|
|---|
| 245 | * @param[in] error The packet error reporting service. Prefixes the
|
|---|
| 246 | * received packet.
|
|---|
| 247 | * @returns EOK on success.
|
|---|
| 248 | * @returns ENOENT if no packet may be sent.
|
|---|
| 249 | */
|
|---|
| [2fa0ad9] | 250 | int
|
|---|
| 251 | tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet,
|
|---|
| 252 | services_t error)
|
|---|
| 253 | {
|
|---|
| [aadf01e] | 254 | packet_t next;
|
|---|
| [2fa0ad9] | 255 | uint8_t *src;
|
|---|
| [aadf01e] | 256 | int length;
|
|---|
| [21580dd] | 257 |
|
|---|
| 258 | // detach the first packet and release the others
|
|---|
| [aadf01e] | 259 | next = pq_detach(packet);
|
|---|
| [14f1db0] | 260 | if (next)
|
|---|
| 261 | pq_release_remote(packet_phone, packet_get_id(next));
|
|---|
| 262 |
|
|---|
| [aadf01e] | 263 | length = packet_get_addr(packet, &src, NULL);
|
|---|
| [2fa0ad9] | 264 | if ((length > 0) && (!error) && (icmp_phone >= 0) &&
|
|---|
| 265 | // set both addresses to the source one (avoids the source address
|
|---|
| 266 | // deletion before setting the destination one)
|
|---|
| 267 | (packet_set_addr(packet, src, src, (size_t) length) == EOK)) {
|
|---|
| [21580dd] | 268 | return EOK;
|
|---|
| [2fa0ad9] | 269 | } else
|
|---|
| [14f1db0] | 270 | pq_release_remote(packet_phone, packet_get_id(packet));
|
|---|
| [2fa0ad9] | 271 |
|
|---|
| [21580dd] | 272 | return ENOENT;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| [84c20da] | 275 | /** Receives data from the socket into a packet.
|
|---|
| 276 | *
|
|---|
| 277 | * @param[in] packet_phone The packet server module phone.
|
|---|
| 278 | * @param[out] packet The new created packet.
|
|---|
| 279 | * @param[in] prefix Reserved packet data prefix length.
|
|---|
| 280 | * @param[in] dimension The packet dimension.
|
|---|
| 281 | * @param[in] addr The destination address.
|
|---|
| 282 | * @param[in] addrlen The address length.
|
|---|
| 283 | * @returns Number of bytes received.
|
|---|
| 284 | * @returns EINVAL if the client does not send data.
|
|---|
| 285 | * @returns ENOMEM if there is not enough memory left.
|
|---|
| 286 | * @returns Other error codes as defined for the
|
|---|
| 287 | * async_data_read_finalize() function.
|
|---|
| 288 | */
|
|---|
| [2fa0ad9] | 289 | int
|
|---|
| 290 | tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix,
|
|---|
| 291 | const packet_dimension_ref dimension, const struct sockaddr *addr,
|
|---|
| 292 | socklen_t addrlen)
|
|---|
| 293 | {
|
|---|
| [21580dd] | 294 | ERROR_DECLARE;
|
|---|
| 295 |
|
|---|
| [aadf01e] | 296 | ipc_callid_t callid;
|
|---|
| 297 | size_t length;
|
|---|
| 298 | void * data;
|
|---|
| [21580dd] | 299 |
|
|---|
| [2fa0ad9] | 300 | if (!dimension)
|
|---|
| [aadf01e] | 301 | return EINVAL;
|
|---|
| [2fa0ad9] | 302 |
|
|---|
| [21580dd] | 303 | // get the data length
|
|---|
| [2fa0ad9] | 304 | if (!async_data_write_receive(&callid, &length))
|
|---|
| [aadf01e] | 305 | return EINVAL;
|
|---|
| [2fa0ad9] | 306 |
|
|---|
| [21580dd] | 307 | // get a new packet
|
|---|
| [2fa0ad9] | 308 | *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len,
|
|---|
| 309 | prefix + dimension->prefix, dimension->suffix);
|
|---|
| 310 | if (!packet)
|
|---|
| [aadf01e] | 311 | return ENOMEM;
|
|---|
| [2fa0ad9] | 312 |
|
|---|
| [21580dd] | 313 | // allocate space in the packet
|
|---|
| [aadf01e] | 314 | data = packet_suffix(*packet, length);
|
|---|
| [2fa0ad9] | 315 | if (!data) {
|
|---|
| [14f1db0] | 316 | pq_release_remote(packet_phone, packet_get_id(*packet));
|
|---|
| [21580dd] | 317 | return ENOMEM;
|
|---|
| 318 | }
|
|---|
| [2fa0ad9] | 319 |
|
|---|
| [21580dd] | 320 | // read the data into the packet
|
|---|
| [2fa0ad9] | 321 | if (ERROR_OCCURRED(async_data_write_finalize(callid, data, length)) ||
|
|---|
| 322 | // set the packet destination address
|
|---|
| 323 | ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr,
|
|---|
| 324 | addrlen))) {
|
|---|
| [14f1db0] | 325 | pq_release_remote(packet_phone, packet_get_id(*packet));
|
|---|
| [21580dd] | 326 | return ERROR_CODE;
|
|---|
| 327 | }
|
|---|
| [2fa0ad9] | 328 |
|
|---|
| [aadf01e] | 329 | return (int) length;
|
|---|
| [21580dd] | 330 | }
|
|---|
| 331 |
|
|---|
| 332 | /** @}
|
|---|
| 333 | */
|
|---|