| 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 |
|
|---|
| 29 | /** @addtogroup libnet
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /** @file
|
|---|
| 34 | * Transport layer common functions implementation.
|
|---|
| 35 | * @see tl_common.h
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 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 |
|
|---|
| 46 | #include <net/socket_codes.h>
|
|---|
| 47 | #include <net/in.h>
|
|---|
| 48 | #include <net/in6.h>
|
|---|
| 49 | #include <net/inet.h>
|
|---|
| 50 | #include <net/device.h>
|
|---|
| 51 | #include <net/packet.h>
|
|---|
| 52 |
|
|---|
| 53 | #include <async.h>
|
|---|
| 54 | #include <ipc/services.h>
|
|---|
| 55 | #include <errno.h>
|
|---|
| 56 |
|
|---|
| 57 | DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t);
|
|---|
| 58 |
|
|---|
| 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.
|
|---|
| 66 | * @returns EOK on success.
|
|---|
| 67 | * @returns EINVAL if the address length does not match the address
|
|---|
| 68 | * family.
|
|---|
| 69 | * @returns EAFNOSUPPORT if the address family is not supported.
|
|---|
| 70 | */
|
|---|
| 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;
|
|---|
| 76 |
|
|---|
| 77 | if ((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr)))
|
|---|
| 78 | return EINVAL;
|
|---|
| 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;
|
|---|
| 88 |
|
|---|
| 89 | case AF_INET6:
|
|---|
| 90 | if (addrlen != sizeof(struct sockaddr_in6))
|
|---|
| 91 | return EINVAL;
|
|---|
| 92 |
|
|---|
| 93 | address_in6 = (struct sockaddr_in6 *) addr;
|
|---|
| 94 | *port = ntohs(address_in6->sin6_port);
|
|---|
| 95 | break;
|
|---|
| 96 |
|
|---|
| 97 | default:
|
|---|
| 98 | return EAFNOSUPPORT;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | return EOK;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 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 | *
|
|---|
| 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.
|
|---|
| 119 | */
|
|---|
| 120 | int
|
|---|
| 121 | tl_get_ip_packet_dimension(int ip_phone,
|
|---|
| 122 | packet_dimensions_ref packet_dimensions, device_id_t device_id,
|
|---|
| 123 | packet_dimension_ref *packet_dimension)
|
|---|
| 124 | {
|
|---|
| 125 | int rc;
|
|---|
| 126 |
|
|---|
| 127 | if (!packet_dimension)
|
|---|
| 128 | return EBADMEM;
|
|---|
| 129 |
|
|---|
| 130 | *packet_dimension = packet_dimensions_find(packet_dimensions,
|
|---|
| 131 | device_id);
|
|---|
| 132 | if (!*packet_dimension) {
|
|---|
| 133 | /* Ask for and remember them if not found */
|
|---|
| 134 | *packet_dimension = malloc(sizeof(**packet_dimension));
|
|---|
| 135 | if(!*packet_dimension)
|
|---|
| 136 | return ENOMEM;
|
|---|
| 137 |
|
|---|
| 138 | rc = ip_packet_size_req(ip_phone, device_id, *packet_dimension);
|
|---|
| 139 | if (rc != EOK) {
|
|---|
| 140 | free(*packet_dimension);
|
|---|
| 141 | return rc;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | rc = packet_dimensions_add(packet_dimensions, device_id,
|
|---|
| 145 | *packet_dimension);
|
|---|
| 146 | if (rc < 0) {
|
|---|
| 147 | free(*packet_dimension);
|
|---|
| 148 | return rc;
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | return EOK;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 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.
|
|---|
| 160 | * @returns EOK on success.
|
|---|
| 161 | * @return ENOENT if the packet dimension is not cached.
|
|---|
| 162 | */
|
|---|
| 163 | int
|
|---|
| 164 | tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions,
|
|---|
| 165 | device_id_t device_id, size_t content)
|
|---|
| 166 | {
|
|---|
| 167 | packet_dimension_ref packet_dimension;
|
|---|
| 168 |
|
|---|
| 169 | packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
|
|---|
| 170 | if (!packet_dimension)
|
|---|
| 171 | return ENOENT;
|
|---|
| 172 |
|
|---|
| 173 | packet_dimension->content = content;
|
|---|
| 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)
|
|---|
| 181 | packet_dimension->content = content;
|
|---|
| 182 | else
|
|---|
| 183 | packet_dimensions_exclude(packet_dimensions,
|
|---|
| 184 | DEVICE_INVALID_ID);
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | return EOK;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 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.
|
|---|
| 198 | * @returns EOK on success.
|
|---|
| 199 | * @returns EINVAL if the address length does not match the address
|
|---|
| 200 | * family.
|
|---|
| 201 | * @returns EAFNOSUPPORT if the address family is not supported.
|
|---|
| 202 | */
|
|---|
| 203 | int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port)
|
|---|
| 204 | {
|
|---|
| 205 | struct sockaddr_in *address_in;
|
|---|
| 206 | struct sockaddr_in6 *address_in6;
|
|---|
| 207 | size_t length;
|
|---|
| 208 |
|
|---|
| 209 | if (addrlen < 0)
|
|---|
| 210 | return EINVAL;
|
|---|
| 211 |
|
|---|
| 212 | length = (size_t) addrlen;
|
|---|
| 213 | if (length < sizeof(struct sockaddr))
|
|---|
| 214 | return EINVAL;
|
|---|
| 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;
|
|---|
| 223 |
|
|---|
| 224 | case AF_INET6:
|
|---|
| 225 | if (length != sizeof(struct sockaddr_in6))
|
|---|
| 226 | return EINVAL;
|
|---|
| 227 | address_in6 = (struct sockaddr_in6 *) addr;
|
|---|
| 228 | address_in6->sin6_port = htons(port);
|
|---|
| 229 | return EOK;
|
|---|
| 230 |
|
|---|
| 231 | default:
|
|---|
| 232 | return EAFNOSUPPORT;
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 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.
|
|---|
| 246 | * @returns EOK on success.
|
|---|
| 247 | * @returns ENOENT if no packet may be sent.
|
|---|
| 248 | */
|
|---|
| 249 | int
|
|---|
| 250 | tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet,
|
|---|
| 251 | services_t error)
|
|---|
| 252 | {
|
|---|
| 253 | packet_t next;
|
|---|
| 254 | uint8_t *src;
|
|---|
| 255 | int length;
|
|---|
| 256 |
|
|---|
| 257 | // detach the first packet and release the others
|
|---|
| 258 | next = pq_detach(packet);
|
|---|
| 259 | if (next)
|
|---|
| 260 | pq_release_remote(packet_phone, packet_get_id(next));
|
|---|
| 261 |
|
|---|
| 262 | length = packet_get_addr(packet, &src, NULL);
|
|---|
| 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)) {
|
|---|
| 267 | return EOK;
|
|---|
| 268 | } else
|
|---|
| 269 | pq_release_remote(packet_phone, packet_get_id(packet));
|
|---|
| 270 |
|
|---|
| 271 | return ENOENT;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 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.
|
|---|
| 282 | * @returns Number of bytes received.
|
|---|
| 283 | * @returns EINVAL if the client does not send data.
|
|---|
| 284 | * @returns ENOMEM if there is not enough memory left.
|
|---|
| 285 | * @returns Other error codes as defined for the
|
|---|
| 286 | * async_data_read_finalize() function.
|
|---|
| 287 | */
|
|---|
| 288 | int
|
|---|
| 289 | tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix,
|
|---|
| 290 | const packet_dimension_ref dimension, const struct sockaddr *addr,
|
|---|
| 291 | socklen_t addrlen)
|
|---|
| 292 | {
|
|---|
| 293 | ipc_callid_t callid;
|
|---|
| 294 | size_t length;
|
|---|
| 295 | void *data;
|
|---|
| 296 | int rc;
|
|---|
| 297 |
|
|---|
| 298 | if (!dimension)
|
|---|
| 299 | return EINVAL;
|
|---|
| 300 |
|
|---|
| 301 | // get the data length
|
|---|
| 302 | if (!async_data_write_receive(&callid, &length))
|
|---|
| 303 | return EINVAL;
|
|---|
| 304 |
|
|---|
| 305 | // get a new packet
|
|---|
| 306 | *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len,
|
|---|
| 307 | prefix + dimension->prefix, dimension->suffix);
|
|---|
| 308 | if (!packet)
|
|---|
| 309 | return ENOMEM;
|
|---|
| 310 |
|
|---|
| 311 | // allocate space in the packet
|
|---|
| 312 | data = packet_suffix(*packet, length);
|
|---|
| 313 | if (!data) {
|
|---|
| 314 | pq_release_remote(packet_phone, packet_get_id(*packet));
|
|---|
| 315 | return ENOMEM;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | // read the data into the packet
|
|---|
| 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) {
|
|---|
| 328 | pq_release_remote(packet_phone, packet_get_id(*packet));
|
|---|
| 329 | return rc;
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | return (int) length;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | /** @}
|
|---|
| 336 | */
|
|---|