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