[21580dd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 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 |
|
---|
[e037e20e] | 29 | /** @addtogroup libnet
|
---|
[24ab58b3] | 30 | * @{
|
---|
[21580dd] | 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
[24ab58b3] | 34 | *
|
---|
[14f1db0] | 35 | * IP interface implementation for remote modules.
|
---|
[24ab58b3] | 36 | *
|
---|
| 37 | * @see ip_interface.h
|
---|
| 38 | * @see il_interface.h
|
---|
| 39 | *
|
---|
[21580dd] | 40 | */
|
---|
| 41 |
|
---|
[514ee46] | 42 | #include <ip_remote.h>
|
---|
| 43 | #include <ip_interface.h>
|
---|
| 44 | #include <packet_client.h>
|
---|
| 45 | #include <generic.h>
|
---|
| 46 |
|
---|
[21580dd] | 47 | #include <ipc/services.h>
|
---|
[522253c1] | 48 | #include <ipc/il.h>
|
---|
[779a47d] | 49 | #include <ipc/ip.h>
|
---|
[21580dd] | 50 |
|
---|
[c7a8442] | 51 | #include <net/modules.h>
|
---|
[e526f08] | 52 | #include <net/device.h>
|
---|
[e4554d4] | 53 | #include <net/inet.h>
|
---|
[21580dd] | 54 |
|
---|
[14f1db0] | 55 | /** Add a route to the device routing table.
|
---|
| 56 | *
|
---|
| 57 | * The target network is routed using this device.
|
---|
| 58 | *
|
---|
[d3cdb7f0] | 59 | * @param[in] ip_phone The IP module phone used for (semi)remote calls.
|
---|
| 60 | * @param[in] device_id The device identifier.
|
---|
| 61 | * @param[in] address The target network address.
|
---|
| 62 | * @param[in] netmask The target network mask.
|
---|
| 63 | * @param[in] gateway The target network gateway. Not used if zero.
|
---|
[14f1db0] | 64 | */
|
---|
| 65 | int ip_add_route_req_remote(int ip_phone, device_id_t device_id,
|
---|
| 66 | in_addr_t address, in_addr_t netmask, in_addr_t gateway)
|
---|
[24ab58b3] | 67 | {
|
---|
| 68 | return (int) async_req_4_0(ip_phone, NET_IP_ADD_ROUTE,
|
---|
[96b02eb9] | 69 | (sysarg_t) device_id, (sysarg_t) gateway.s_addr,
|
---|
| 70 | (sysarg_t) address.s_addr, (sysarg_t) netmask.s_addr);
|
---|
[21580dd] | 71 | }
|
---|
| 72 |
|
---|
[e037e20e] | 73 | /** Creates bidirectional connection with the ip module service and registers
|
---|
| 74 | * the message receiver.
|
---|
| 75 | *
|
---|
| 76 | * @param[in] service The IP module service.
|
---|
| 77 | * @param[in] protocol The transport layer protocol.
|
---|
| 78 | * @param[in] me The requesting module service.
|
---|
| 79 | * @param[in] receiver The message receiver. Used for remote connection.
|
---|
[1bfd3d3] | 80 | * @return The phone of the needed service.
|
---|
| 81 | * @return EOK on success.
|
---|
| 82 | * @return Other error codes as defined for the bind_service()
|
---|
[e037e20e] | 83 | * function.
|
---|
| 84 | */
|
---|
[24ab58b3] | 85 | int ip_bind_service(services_t service, int protocol, services_t me,
|
---|
[5a868d7] | 86 | async_client_conn_t receiver)
|
---|
[24ab58b3] | 87 | {
|
---|
[96b02eb9] | 88 | return (int) bind_service(service, (sysarg_t) protocol, me, service,
|
---|
[24ab58b3] | 89 | receiver);
|
---|
[21580dd] | 90 | }
|
---|
| 91 |
|
---|
[e037e20e] | 92 | /** Connects to the IP module.
|
---|
| 93 | *
|
---|
| 94 | * @param service The IP module service. Ignored parameter.
|
---|
[1bfd3d3] | 95 | * @return The IP module phone on success.
|
---|
[e037e20e] | 96 | */
|
---|
[24ab58b3] | 97 | int ip_connect_module(services_t service)
|
---|
| 98 | {
|
---|
[a64c64d] | 99 | return connect_to_service(SERVICE_IP);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[14f1db0] | 102 | /** Register the new device.
|
---|
| 103 | *
|
---|
| 104 | * Register itself as the ip packet receiver.
|
---|
| 105 | * If the device uses ARP registers also the new ARP device.
|
---|
| 106 | *
|
---|
[d3cdb7f0] | 107 | * @param[in] ip_phone The IP module phone used for (semi)remote calls.
|
---|
| 108 | * @param[in] device_id The new device identifier.
|
---|
| 109 | * @param[in] netif The underlying device network interface layer service.
|
---|
| 110 | * @return EOK on success.
|
---|
| 111 | * @return ENOMEM if there is not enough memory left.
|
---|
| 112 | * @return EINVAL if the device configuration is invalid.
|
---|
| 113 | * @return ENOTSUP if the device uses IPv6.
|
---|
| 114 | * @return ENOTSUP if the device uses DHCP.
|
---|
| 115 | * @return Other error codes as defined for the
|
---|
| 116 | * net_get_device_conf_req() function.
|
---|
| 117 | * @return Other error codes as defined for the arp_device_req()
|
---|
| 118 | * function.
|
---|
[14f1db0] | 119 | */
|
---|
| 120 | int ip_device_req_remote(int ip_phone, device_id_t device_id,
|
---|
| 121 | services_t service)
|
---|
[24ab58b3] | 122 | {
|
---|
[14f1db0] | 123 | return generic_device_req_remote(ip_phone, NET_IL_DEVICE, device_id, 0,
|
---|
| 124 | service);
|
---|
[21580dd] | 125 | }
|
---|
| 126 |
|
---|
[e037e20e] | 127 | /** Return the device identifier and the IP pseudo header based on the
|
---|
| 128 | * destination address.
|
---|
[14f1db0] | 129 | *
|
---|
[d3cdb7f0] | 130 | * @param[in] ip_phone The IP module phone used for (semi)remote calls.
|
---|
| 131 | * @param[in] protocol The transport protocol.
|
---|
| 132 | * @param[in] destination The destination address.
|
---|
| 133 | * @param[in] addrlen The destination address length.
|
---|
| 134 | * @param[out] device_id The device identifier.
|
---|
| 135 | * @param[out] header The constructed IP pseudo header.
|
---|
| 136 | * @param[out] headerlen The IP pseudo header length.
|
---|
[14f1db0] | 137 | *
|
---|
| 138 | */
|
---|
| 139 | int ip_get_route_req_remote(int ip_phone, ip_protocol_t protocol,
|
---|
[24ab58b3] | 140 | const struct sockaddr *destination, socklen_t addrlen,
|
---|
[14f1db0] | 141 | device_id_t *device_id, void **header, size_t *headerlen)
|
---|
[24ab58b3] | 142 | {
|
---|
[d3cdb7f0] | 143 | if (!destination || (addrlen == 0))
|
---|
[aadf01e] | 144 | return EINVAL;
|
---|
[24ab58b3] | 145 |
|
---|
[d3cdb7f0] | 146 | if (!device_id || !header || !headerlen)
|
---|
[aadf01e] | 147 | return EBADMEM;
|
---|
[24ab58b3] | 148 |
|
---|
[aadf01e] | 149 | *header = NULL;
|
---|
[24ab58b3] | 150 |
|
---|
| 151 | ipc_call_t answer;
|
---|
| 152 | aid_t message_id = async_send_1(ip_phone, NET_IP_GET_ROUTE,
|
---|
[96b02eb9] | 153 | (sysarg_t) protocol, &answer);
|
---|
[24ab58b3] | 154 |
|
---|
[e037e20e] | 155 | if ((async_data_write_start(ip_phone, destination, addrlen) == EOK) &&
|
---|
| 156 | (async_data_read_start(ip_phone, headerlen,
|
---|
| 157 | sizeof(*headerlen)) == EOK) && (*headerlen > 0)) {
|
---|
[14f1db0] | 158 | *header = malloc(*headerlen);
|
---|
[24ab58b3] | 159 | if (*header) {
|
---|
[e037e20e] | 160 | if (async_data_read_start(ip_phone, *header,
|
---|
| 161 | *headerlen) != EOK)
|
---|
[aadf01e] | 162 | free(*header);
|
---|
[21580dd] | 163 | }
|
---|
| 164 | }
|
---|
[24ab58b3] | 165 |
|
---|
[96b02eb9] | 166 | sysarg_t result;
|
---|
[aadf01e] | 167 | async_wait_for(message_id, &result);
|
---|
[24ab58b3] | 168 |
|
---|
[d3cdb7f0] | 169 | if ((result != EOK) && *header)
|
---|
[aadf01e] | 170 | free(*header);
|
---|
[24ab58b3] | 171 | else
|
---|
[774e6d1a] | 172 | *device_id = IPC_GET_DEVICE(answer);
|
---|
[24ab58b3] | 173 |
|
---|
[aadf01e] | 174 | return (int) result;
|
---|
[21580dd] | 175 | }
|
---|
| 176 |
|
---|
[14f1db0] | 177 | /** Return the device packet dimension for sending.
|
---|
| 178 | *
|
---|
[d3cdb7f0] | 179 | * @param[in] ip_phone The IP module phone used for (semi)remote calls.
|
---|
| 180 | * @param[in] device_id The device identifier.
|
---|
[14f1db0] | 181 | * @param[out] packet_dimension The packet dimension.
|
---|
[d3cdb7f0] | 182 | * @return EOK on success.
|
---|
| 183 | * @return ENOENT if there is no such device.
|
---|
| 184 | * @return Other error codes as defined for the
|
---|
| 185 | * generic_packet_size_req_remote() function.
|
---|
[14f1db0] | 186 | */
|
---|
| 187 | int ip_packet_size_req_remote(int ip_phone, device_id_t device_id,
|
---|
[f772bc55] | 188 | packet_dimension_t *packet_dimension)
|
---|
[24ab58b3] | 189 | {
|
---|
[e037e20e] | 190 | return generic_packet_size_req_remote(ip_phone, NET_IL_PACKET_SPACE,
|
---|
| 191 | device_id, packet_dimension);
|
---|
[a64c64d] | 192 | }
|
---|
| 193 |
|
---|
[14f1db0] | 194 | /** Notify the IP module about the received error notification packet.
|
---|
| 195 | *
|
---|
[d3cdb7f0] | 196 | * @param[in] ip_phone The IP module phone used for (semi)remote calls.
|
---|
| 197 | * @param[in] device_id The device identifier.
|
---|
| 198 | * @param[in] packet The received packet or the received packet queue.
|
---|
| 199 | * @param[in] target The target internetwork module service to be
|
---|
| 200 | * delivered to.
|
---|
| 201 | * @param[in] error The packet error reporting service. Prefixes the
|
---|
| 202 | * received packet.
|
---|
| 203 | * @return EOK on success.
|
---|
[14f1db0] | 204 | */
|
---|
| 205 | int ip_received_error_msg_remote(int ip_phone, device_id_t device_id,
|
---|
[46d4d9f] | 206 | packet_t *packet, services_t target, services_t error)
|
---|
[24ab58b3] | 207 | {
|
---|
[14f1db0] | 208 | return generic_received_msg_remote(ip_phone, NET_IP_RECEIVED_ERROR,
|
---|
| 209 | device_id, packet_get_id(packet), target, error);
|
---|
[a64c64d] | 210 | }
|
---|
| 211 |
|
---|
[14f1db0] | 212 | /** Send the packet queue.
|
---|
| 213 | *
|
---|
| 214 | * The packets may get fragmented if needed.
|
---|
| 215 | *
|
---|
[d3cdb7f0] | 216 | * @param[in] ip_phone The IP module phone used for (semi)remote calls.
|
---|
| 217 | * @param[in] device_id The device identifier.
|
---|
| 218 | * @param[in] packet The packet fragments as a packet queue. All the
|
---|
| 219 | * packets have to have the same destination address.
|
---|
| 220 | * @param[in] sender The sending module service.
|
---|
| 221 | * @param[in] error The packet error reporting service. Prefixes the
|
---|
| 222 | * received packet.
|
---|
| 223 | * @return EOK on success.
|
---|
| 224 | * @return Other error codes as defined for the generic_send_msg()
|
---|
| 225 | * function.
|
---|
[14f1db0] | 226 | */
|
---|
[46d4d9f] | 227 | int ip_send_msg_remote(int ip_phone, device_id_t device_id, packet_t *packet,
|
---|
[24ab58b3] | 228 | services_t sender, services_t error)
|
---|
| 229 | {
|
---|
[14f1db0] | 230 | return generic_send_msg_remote(ip_phone, NET_IL_SEND, device_id,
|
---|
[24ab58b3] | 231 | packet_get_id(packet), sender, error);
|
---|
[a64c64d] | 232 | }
|
---|
| 233 |
|
---|
[14f1db0] | 234 | /** Set the default gateway.
|
---|
| 235 | *
|
---|
| 236 | * This gateway is used if no other route is found.
|
---|
| 237 | *
|
---|
[d3cdb7f0] | 238 | * @param[in] ip_phone The IP module phone used for (semi)remote calls.
|
---|
| 239 | * @param[in] device_id The device identifier.
|
---|
| 240 | * @param[in] gateway The default gateway.
|
---|
[14f1db0] | 241 | */
|
---|
| 242 | int ip_set_gateway_req_remote(int ip_phone, device_id_t device_id,
|
---|
| 243 | in_addr_t gateway)
|
---|
[24ab58b3] | 244 | {
|
---|
| 245 | return (int) async_req_2_0(ip_phone, NET_IP_SET_GATEWAY,
|
---|
[96b02eb9] | 246 | (sysarg_t) device_id, (sysarg_t) gateway.s_addr);
|
---|
[a64c64d] | 247 | }
|
---|
| 248 |
|
---|
[21580dd] | 249 | /** @}
|
---|
| 250 | */
|
---|