| [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 |
|
|---|
| 29 | /** @addtogroup net_tl
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /** @file
|
|---|
| 34 | * Transport layer common functions implementation.
|
|---|
| 35 | * @see tl_common.h
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| [058edb6] | 38 | #include <net/socket_codes.h>
|
|---|
| [e4554d4] | 39 | #include <net/in.h>
|
|---|
| 40 | #include <net/in6.h>
|
|---|
| 41 | #include <net/inet.h>
|
|---|
| [21580dd] | 42 | #include <async.h>
|
|---|
| 43 | #include <ipc/services.h>
|
|---|
| [e98b1d5] | 44 | #include <errno.h>
|
|---|
| [c5b59ce] | 45 | #include <err.h>
|
|---|
| [21580dd] | 46 |
|
|---|
| [c69d327] | 47 | #include <net/packet.h>
|
|---|
| [0a866eeb] | 48 | #include <packet_client.h>
|
|---|
| [14f1db0] | 49 | #include <packet_remote.h>
|
|---|
| [849ed54] | 50 | #include <net_device.h>
|
|---|
| 51 | #include <icmp_interface.h>
|
|---|
| [14f1db0] | 52 | #include <ip_remote.h>
|
|---|
| 53 | #include <ip_interface.h>
|
|---|
| 54 | #include <tl_interface.h>
|
|---|
| [849ed54] | 55 | #include <tl_common.h>
|
|---|
| [21580dd] | 56 |
|
|---|
| [aadf01e] | 57 | DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t);
|
|---|
| [21580dd] | 58 |
|
|---|
| [2fa0ad9] | 59 | int
|
|---|
| 60 | tl_get_address_port(const struct sockaddr *addr, int addrlen, uint16_t *port)
|
|---|
| 61 | {
|
|---|
| 62 | const struct sockaddr_in *address_in;
|
|---|
| 63 | const struct sockaddr_in6 *address_in6;
|
|---|
| [21580dd] | 64 |
|
|---|
| [2fa0ad9] | 65 | if ((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr)))
|
|---|
| [aadf01e] | 66 | return EINVAL;
|
|---|
| [2fa0ad9] | 67 |
|
|---|
| 68 | switch (addr->sa_family) {
|
|---|
| 69 | case AF_INET:
|
|---|
| 70 | if (addrlen != sizeof(struct sockaddr_in))
|
|---|
| 71 | return EINVAL;
|
|---|
| 72 |
|
|---|
| 73 | address_in = (struct sockaddr_in *) addr;
|
|---|
| 74 | *port = ntohs(address_in->sin_port);
|
|---|
| 75 | break;
|
|---|
| 76 | case AF_INET6:
|
|---|
| 77 | if (addrlen != sizeof(struct sockaddr_in6))
|
|---|
| [aadf01e] | 78 | return EINVAL;
|
|---|
| [2fa0ad9] | 79 |
|
|---|
| 80 | address_in6 = (struct sockaddr_in6 *) addr;
|
|---|
| 81 | *port = ntohs(address_in6->sin6_port);
|
|---|
| 82 | break;
|
|---|
| 83 | default:
|
|---|
| 84 | return EAFNOSUPPORT;
|
|---|
| [21580dd] | 85 | }
|
|---|
| [2fa0ad9] | 86 |
|
|---|
| [21580dd] | 87 | return EOK;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| [14f1db0] | 90 | /** Get IP packet dimensions.
|
|---|
| 91 | *
|
|---|
| 92 | * Try to search a cache and query the IP module if not found.
|
|---|
| 93 | * The reply is cached then.
|
|---|
| 94 | *
|
|---|
| 95 | * @param[in] ip_phone The IP moduel phone for (semi)remote calls.
|
|---|
| 96 | * @param[in] packet_dimensions The packet dimensions cache.
|
|---|
| 97 | * @param[in] device_id The device identifier.
|
|---|
| 98 | * @param[out] packet_dimension The IP packet dimensions.
|
|---|
| 99 | *
|
|---|
| 100 | * @return EOK on success.
|
|---|
| 101 | * @return EBADMEM if the packet_dimension parameter is NULL.
|
|---|
| 102 | * @return ENOMEM if there is not enough memory left.
|
|---|
| 103 | * @return EINVAL if the packet_dimensions cache is not valid.
|
|---|
| 104 | * @return Other codes as defined for the ip_packet_size_req() function.
|
|---|
| 105 | *
|
|---|
| 106 | */
|
|---|
| 107 | int tl_get_ip_packet_dimension(int ip_phone,
|
|---|
| 108 | packet_dimensions_ref packet_dimensions, device_id_t device_id,
|
|---|
| 109 | packet_dimension_ref *packet_dimension)
|
|---|
| 110 | {
|
|---|
| [21580dd] | 111 | ERROR_DECLARE;
|
|---|
| [14f1db0] | 112 |
|
|---|
| 113 | if (!packet_dimension)
|
|---|
| [aadf01e] | 114 | return EBADMEM;
|
|---|
| [14f1db0] | 115 |
|
|---|
| [2fa0ad9] | 116 | *packet_dimension = packet_dimensions_find(packet_dimensions,
|
|---|
| 117 | device_id);
|
|---|
| [14f1db0] | 118 | if (!*packet_dimension) {
|
|---|
| 119 | /* Ask for and remember them if not found */
|
|---|
| 120 | *packet_dimension = malloc(sizeof(**packet_dimension));
|
|---|
| 121 | if(!*packet_dimension)
|
|---|
| [aadf01e] | 122 | return ENOMEM;
|
|---|
| [14f1db0] | 123 |
|
|---|
| 124 | if (ERROR_OCCURRED(ip_packet_size_req(ip_phone, device_id,
|
|---|
| 125 | *packet_dimension))) {
|
|---|
| [aadf01e] | 126 | free(*packet_dimension);
|
|---|
| [21580dd] | 127 | return ERROR_CODE;
|
|---|
| 128 | }
|
|---|
| [14f1db0] | 129 |
|
|---|
| 130 | ERROR_CODE = packet_dimensions_add(packet_dimensions, device_id,
|
|---|
| 131 | *packet_dimension);
|
|---|
| 132 | if (ERROR_CODE < 0) {
|
|---|
| [aadf01e] | 133 | free(*packet_dimension);
|
|---|
| [21580dd] | 134 | return ERROR_CODE;
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| [14f1db0] | 137 |
|
|---|
| [21580dd] | 138 | return EOK;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| [2fa0ad9] | 141 | int
|
|---|
| 142 | tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions,
|
|---|
| 143 | device_id_t device_id, size_t content)
|
|---|
| 144 | {
|
|---|
| [aadf01e] | 145 | packet_dimension_ref packet_dimension;
|
|---|
| [b4ff8a6d] | 146 |
|
|---|
| [aadf01e] | 147 | packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
|
|---|
| [2fa0ad9] | 148 | if (!packet_dimension)
|
|---|
| [aadf01e] | 149 | return ENOENT;
|
|---|
| [b4ff8a6d] | 150 | packet_dimension->content = content;
|
|---|
| [2fa0ad9] | 151 |
|
|---|
| 152 | if (device_id != DEVICE_INVALID_ID) {
|
|---|
| 153 | packet_dimension = packet_dimensions_find(packet_dimensions,
|
|---|
| 154 | DEVICE_INVALID_ID);
|
|---|
| 155 |
|
|---|
| 156 | if (packet_dimension) {
|
|---|
| 157 | if (packet_dimension->content >= content)
|
|---|
| [b4ff8a6d] | 158 | packet_dimension->content = content;
|
|---|
| [2fa0ad9] | 159 | else
|
|---|
| 160 | packet_dimensions_exclude(packet_dimensions,
|
|---|
| 161 | DEVICE_INVALID_ID);
|
|---|
| 162 |
|
|---|
| [b4ff8a6d] | 163 | }
|
|---|
| 164 | }
|
|---|
| [2fa0ad9] | 165 |
|
|---|
| [b4ff8a6d] | 166 | return EOK;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| [7c8267b] | 169 | int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port)
|
|---|
| 170 | {
|
|---|
| [2fa0ad9] | 171 | struct sockaddr_in *address_in;
|
|---|
| 172 | struct sockaddr_in6 *address_in6;
|
|---|
| [aadf01e] | 173 | size_t length;
|
|---|
| [21580dd] | 174 |
|
|---|
| [7c8267b] | 175 | if (addrlen < 0)
|
|---|
| [aadf01e] | 176 | return EINVAL;
|
|---|
| [7c8267b] | 177 |
|
|---|
| [aadf01e] | 178 | length = (size_t) addrlen;
|
|---|
| [7c8267b] | 179 | if (length < sizeof(struct sockaddr))
|
|---|
| [aadf01e] | 180 | return EINVAL;
|
|---|
| [7c8267b] | 181 |
|
|---|
| 182 | switch (addr->sa_family) {
|
|---|
| 183 | case AF_INET:
|
|---|
| 184 | if (length != sizeof(struct sockaddr_in))
|
|---|
| 185 | return EINVAL;
|
|---|
| 186 | address_in = (struct sockaddr_in *) addr;
|
|---|
| 187 | address_in->sin_port = htons(port);
|
|---|
| 188 | return EOK;
|
|---|
| 189 | case AF_INET6:
|
|---|
| 190 | if (length != sizeof(struct sockaddr_in6))
|
|---|
| [aadf01e] | 191 | return EINVAL;
|
|---|
| [7c8267b] | 192 | address_in6 = (struct sockaddr_in6 *) addr;
|
|---|
| 193 | address_in6->sin6_port = htons(port);
|
|---|
| 194 | return EOK;
|
|---|
| 195 | default:
|
|---|
| 196 | return EAFNOSUPPORT;
|
|---|
| [21580dd] | 197 | }
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| [2fa0ad9] | 200 | int
|
|---|
| 201 | tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet,
|
|---|
| 202 | services_t error)
|
|---|
| 203 | {
|
|---|
| [aadf01e] | 204 | packet_t next;
|
|---|
| [2fa0ad9] | 205 | uint8_t *src;
|
|---|
| [aadf01e] | 206 | int length;
|
|---|
| [21580dd] | 207 |
|
|---|
| 208 | // detach the first packet and release the others
|
|---|
| [aadf01e] | 209 | next = pq_detach(packet);
|
|---|
| [14f1db0] | 210 | if (next)
|
|---|
| 211 | pq_release_remote(packet_phone, packet_get_id(next));
|
|---|
| 212 |
|
|---|
| [aadf01e] | 213 | length = packet_get_addr(packet, &src, NULL);
|
|---|
| [2fa0ad9] | 214 | if ((length > 0) && (!error) && (icmp_phone >= 0) &&
|
|---|
| 215 | // set both addresses to the source one (avoids the source address
|
|---|
| 216 | // deletion before setting the destination one)
|
|---|
| 217 | (packet_set_addr(packet, src, src, (size_t) length) == EOK)) {
|
|---|
| [21580dd] | 218 | return EOK;
|
|---|
| [2fa0ad9] | 219 | } else
|
|---|
| [14f1db0] | 220 | pq_release_remote(packet_phone, packet_get_id(packet));
|
|---|
| [2fa0ad9] | 221 |
|
|---|
| [21580dd] | 222 | return ENOENT;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| [2fa0ad9] | 225 | int
|
|---|
| 226 | tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix,
|
|---|
| 227 | const packet_dimension_ref dimension, const struct sockaddr *addr,
|
|---|
| 228 | socklen_t addrlen)
|
|---|
| 229 | {
|
|---|
| [21580dd] | 230 | ERROR_DECLARE;
|
|---|
| 231 |
|
|---|
| [aadf01e] | 232 | ipc_callid_t callid;
|
|---|
| 233 | size_t length;
|
|---|
| 234 | void * data;
|
|---|
| [21580dd] | 235 |
|
|---|
| [2fa0ad9] | 236 | if (!dimension)
|
|---|
| [aadf01e] | 237 | return EINVAL;
|
|---|
| [2fa0ad9] | 238 |
|
|---|
| [21580dd] | 239 | // get the data length
|
|---|
| [2fa0ad9] | 240 | if (!async_data_write_receive(&callid, &length))
|
|---|
| [aadf01e] | 241 | return EINVAL;
|
|---|
| [2fa0ad9] | 242 |
|
|---|
| [21580dd] | 243 | // get a new packet
|
|---|
| [2fa0ad9] | 244 | *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len,
|
|---|
| 245 | prefix + dimension->prefix, dimension->suffix);
|
|---|
| 246 | if (!packet)
|
|---|
| [aadf01e] | 247 | return ENOMEM;
|
|---|
| [2fa0ad9] | 248 |
|
|---|
| [21580dd] | 249 | // allocate space in the packet
|
|---|
| [aadf01e] | 250 | data = packet_suffix(*packet, length);
|
|---|
| [2fa0ad9] | 251 | if (!data) {
|
|---|
| [14f1db0] | 252 | pq_release_remote(packet_phone, packet_get_id(*packet));
|
|---|
| [21580dd] | 253 | return ENOMEM;
|
|---|
| 254 | }
|
|---|
| [2fa0ad9] | 255 |
|
|---|
| [21580dd] | 256 | // read the data into the packet
|
|---|
| [2fa0ad9] | 257 | if (ERROR_OCCURRED(async_data_write_finalize(callid, data, length)) ||
|
|---|
| 258 | // set the packet destination address
|
|---|
| 259 | ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr,
|
|---|
| 260 | addrlen))) {
|
|---|
| [14f1db0] | 261 | pq_release_remote(packet_phone, packet_get_id(*packet));
|
|---|
| [21580dd] | 262 | return ERROR_CODE;
|
|---|
| 263 | }
|
|---|
| [2fa0ad9] | 264 |
|
|---|
| [aadf01e] | 265 | return (int) length;
|
|---|
| [21580dd] | 266 | }
|
|---|
| 267 |
|
|---|
| 268 | /** @}
|
|---|
| 269 | */
|
|---|