source: mainline/uspace/lib/net/tl/tl_common.c@ e2c50e1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e2c50e1 was 609243f4, checked in by Martin Decky <martin@…>, 14 years ago

cherrypick general networking improvements from lp:~helenos-nicf/helenos/nicf (after sanitization)
remove obsolete networking drivers
this renders the networking non-functional for the time being

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