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