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