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