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