| [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 |
|
|---|
| [9b9d1c95] | 29 | /** @addtogroup libnet
|
|---|
| 30 | * @{
|
|---|
| [21580dd] | 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /** @file
|
|---|
| [9b9d1c95] | 34 | * IP client interface implementation.
|
|---|
| 35 | * @see ip_client.h
|
|---|
| [21580dd] | 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <sys/types.h>
|
|---|
| 40 |
|
|---|
| [849ed54] | 41 | #include <ip_client.h>
|
|---|
| [0a866eeb] | 42 | #include <packet_client.h>
|
|---|
| [849ed54] | 43 | #include <ip_header.h>
|
|---|
| [21580dd] | 44 |
|
|---|
| [c69d327] | 45 | #include <net/packet.h>
|
|---|
| [0a866eeb] | 46 |
|
|---|
| [9b9d1c95] | 47 | /** Returns the IP header length.
|
|---|
| 48 | *
|
|---|
| 49 | * @param[in] packet The packet.
|
|---|
| 50 | * @returns The IP header length in bytes.
|
|---|
| 51 | * @returns Zero if there is no IP header.
|
|---|
| 52 | */
|
|---|
| 53 | size_t ip_client_header_length(packet_t packet)
|
|---|
| 54 | {
|
|---|
| [a64c64d] | 55 | ip_header_ref header;
|
|---|
| 56 |
|
|---|
| 57 | header = (ip_header_ref) packet_get_data(packet);
|
|---|
| [9b9d1c95] | 58 | if (!header || (packet_get_data_length(packet) < sizeof(ip_header_t)))
|
|---|
| [a64c64d] | 59 | return 0;
|
|---|
| [9b9d1c95] | 60 |
|
|---|
| [a64c64d] | 61 | return IP_HEADER_LENGTH(header);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| [9b9d1c95] | 64 | /** Constructs the IPv4 pseudo header.
|
|---|
| 65 | *
|
|---|
| 66 | * @param[in] protocol The transport protocol.
|
|---|
| 67 | * @param[in] src The source address.
|
|---|
| 68 | * @param[in] srclen The source address length.
|
|---|
| 69 | * @param[in] dest The destination address.
|
|---|
| 70 | * @param[in] destlen The destination address length.
|
|---|
| 71 | * @param[in] data_length The data length to be set.
|
|---|
| 72 | * @param[out] header The constructed IPv4 pseudo header.
|
|---|
| 73 | * @param[out] headerlen The length of the IP pseudo header in bytes.
|
|---|
| 74 | * @returns EOK on success.
|
|---|
| 75 | * @returns EBADMEM if the header and/or the headerlen parameter is
|
|---|
| 76 | * NULL.
|
|---|
| 77 | * @returns EINVAL if the source address and/or the destination
|
|---|
| 78 | * address parameter is NULL.
|
|---|
| 79 | * @returns EINVAL if the source address length is less than struct
|
|---|
| 80 | * sockaddr length.
|
|---|
| 81 | * @returns EINVAL if the source address length differs from the
|
|---|
| 82 | * destination address length.
|
|---|
| 83 | * @returns EINVAL if the source address family differs from the
|
|---|
| 84 | * destination family.
|
|---|
| 85 | * @returns EAFNOSUPPORT if the address family is not supported.
|
|---|
| 86 | * @returns ENOMEM if there is not enough memory left.
|
|---|
| 87 | */
|
|---|
| 88 | int
|
|---|
| 89 | ip_client_get_pseudo_header(ip_protocol_t protocol, struct sockaddr *src,
|
|---|
| 90 | socklen_t srclen, struct sockaddr *dest, socklen_t destlen,
|
|---|
| 91 | size_t data_length, void **header, size_t *headerlen)
|
|---|
| 92 | {
|
|---|
| [a64c64d] | 93 | ipv4_pseudo_header_ref header_in;
|
|---|
| [9b9d1c95] | 94 | struct sockaddr_in *address_in;
|
|---|
| [a64c64d] | 95 |
|
|---|
| [9b9d1c95] | 96 | if (!header || !headerlen)
|
|---|
| [a64c64d] | 97 | return EBADMEM;
|
|---|
| [9b9d1c95] | 98 |
|
|---|
| 99 | if (!src || !dest || srclen <= 0 ||
|
|---|
| 100 | (((size_t) srclen < sizeof(struct sockaddr))) ||
|
|---|
| 101 | (srclen != destlen) || (src->sa_family != dest->sa_family)) {
|
|---|
| [a64c64d] | 102 | return EINVAL;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| [9b9d1c95] | 105 | switch (src->sa_family) {
|
|---|
| 106 | case AF_INET:
|
|---|
| 107 | if (srclen != sizeof(struct sockaddr_in))
|
|---|
| 108 | return EINVAL;
|
|---|
| 109 |
|
|---|
| 110 | *headerlen = sizeof(*header_in);
|
|---|
| 111 | header_in = (ipv4_pseudo_header_ref) malloc(*headerlen);
|
|---|
| 112 | if (!header_in)
|
|---|
| 113 | return ENOMEM;
|
|---|
| 114 |
|
|---|
| 115 | bzero(header_in, *headerlen);
|
|---|
| 116 | address_in = (struct sockaddr_in *) dest;
|
|---|
| 117 | header_in->destination_address = address_in->sin_addr.s_addr;
|
|---|
| 118 | address_in = (struct sockaddr_in *) src;
|
|---|
| 119 | header_in->source_address = address_in->sin_addr.s_addr;
|
|---|
| 120 | header_in->protocol = protocol;
|
|---|
| 121 | header_in->data_length = htons(data_length);
|
|---|
| 122 | *header = header_in;
|
|---|
| 123 | return EOK;
|
|---|
| 124 |
|
|---|
| 125 | // TODO IPv6
|
|---|
| 126 | /* case AF_INET6:
|
|---|
| 127 | if (addrlen != sizeof(struct sockaddr_in6))
|
|---|
| 128 | return EINVAL;
|
|---|
| 129 |
|
|---|
| 130 | address_in6 = (struct sockaddr_in6 *) addr;
|
|---|
| 131 | return EOK;
|
|---|
| 132 | */
|
|---|
| 133 |
|
|---|
| 134 | default:
|
|---|
| 135 | return EAFNOSUPPORT;
|
|---|
| [a64c64d] | 136 | }
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| [9b9d1c95] | 139 | /** Prepares the packet to be transfered via IP.
|
|---|
| 140 | *
|
|---|
| 141 | * The IP header is prefixed.
|
|---|
| 142 | *
|
|---|
| 143 | * @param[in,out] packet The packet to be prepared.
|
|---|
| 144 | * @param[in] protocol The transport protocol.
|
|---|
| 145 | * @param[in] ttl The time to live counter. The IPDEFTTL is set if zero.
|
|---|
| 146 | * @param[in] tos The type of service.
|
|---|
| 147 | * @param[in] dont_fragment The value indicating whether fragmentation is
|
|---|
| 148 | * disabled.
|
|---|
| 149 | * @param[in] ipopt_length The prefixed IP options length in bytes.
|
|---|
| 150 | * @returns EOK on success.
|
|---|
| 151 | * @returns ENOMEM if there is not enough memory left in the packet.
|
|---|
| 152 | */
|
|---|
| 153 | int
|
|---|
| 154 | ip_client_prepare_packet(packet_t packet, ip_protocol_t protocol, ip_ttl_t ttl,
|
|---|
| 155 | ip_tos_t tos, int dont_fragment, size_t ipopt_length)
|
|---|
| 156 | {
|
|---|
| [aadf01e] | 157 | ip_header_ref header;
|
|---|
| [9b9d1c95] | 158 | uint8_t *data;
|
|---|
| [aadf01e] | 159 | size_t padding;
|
|---|
| [21580dd] | 160 |
|
|---|
| [a64c64d] | 161 | // compute the padding if IP options are set
|
|---|
| 162 | // multiple of 4 bytes
|
|---|
| [21580dd] | 163 | padding = ipopt_length % 4;
|
|---|
| [9b9d1c95] | 164 | if (padding) {
|
|---|
| [21580dd] | 165 | padding = 4 - padding;
|
|---|
| 166 | ipopt_length += padding;
|
|---|
| 167 | }
|
|---|
| [a64c64d] | 168 |
|
|---|
| 169 | // prefix the header
|
|---|
| [aadf01e] | 170 | data = (uint8_t *) packet_prefix(packet, sizeof(ip_header_t) + padding);
|
|---|
| [9b9d1c95] | 171 | if (!data)
|
|---|
| [aadf01e] | 172 | return ENOMEM;
|
|---|
| [a64c64d] | 173 |
|
|---|
| 174 | // add the padding
|
|---|
| [9b9d1c95] | 175 | while (padding--)
|
|---|
| [aadf01e] | 176 | data[sizeof(ip_header_t) + padding] = IPOPT_NOOP;
|
|---|
| [a64c64d] | 177 |
|
|---|
| 178 | // set the header
|
|---|
| [aadf01e] | 179 | header = (ip_header_ref) data;
|
|---|
| [9b9d1c95] | 180 | header->header_length = IP_COMPUTE_HEADER_LENGTH(sizeof(ip_header_t) +
|
|---|
| 181 | ipopt_length);
|
|---|
| 182 | header->ttl = (ttl ? ttl : IPDEFTTL);
|
|---|
| [21580dd] | 183 | header->tos = tos;
|
|---|
| 184 | header->protocol = protocol;
|
|---|
| [a64c64d] | 185 |
|
|---|
| [9b9d1c95] | 186 | if (dont_fragment)
|
|---|
| [aadf01e] | 187 | header->flags = IPFLAG_DONT_FRAGMENT;
|
|---|
| [9b9d1c95] | 188 |
|
|---|
| [21580dd] | 189 | return EOK;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| [9b9d1c95] | 192 | /** Processes the received IP packet.
|
|---|
| 193 | *
|
|---|
| 194 | * Fills set header fields.
|
|---|
| 195 | * Returns the prefixed IP header length.
|
|---|
| 196 | *
|
|---|
| 197 | * @param[in] packet The received packet.
|
|---|
| 198 | * @param[out] protocol The transport protocol. May be NULL if not desired.
|
|---|
| 199 | * @param[out] ttl The time to live counter. May be NULL if not desired.
|
|---|
| 200 | * @param[out] tos The type of service. May be NULL if not desired.
|
|---|
| 201 | * @param[out] dont_fragment The value indicating whether the fragmentation is
|
|---|
| 202 | * disabled. May be NULL if not desired.
|
|---|
| 203 | * @param[out] ipopt_length The IP options length in bytes. May be NULL if not
|
|---|
| 204 | * desired.
|
|---|
| 205 | * @returns The prefixed IP header length in bytes on success.
|
|---|
| 206 | * @returns ENOMEM if the packet is too short to contain the IP
|
|---|
| 207 | * header.
|
|---|
| 208 | */
|
|---|
| 209 | int
|
|---|
| 210 | ip_client_process_packet(packet_t packet, ip_protocol_t *protocol,
|
|---|
| 211 | ip_ttl_t *ttl, ip_tos_t *tos, int *dont_fragment, size_t *ipopt_length)
|
|---|
| 212 | {
|
|---|
| [aadf01e] | 213 | ip_header_ref header;
|
|---|
| [21580dd] | 214 |
|
|---|
| [aadf01e] | 215 | header = (ip_header_ref) packet_get_data(packet);
|
|---|
| [9b9d1c95] | 216 | if (!header || (packet_get_data_length(packet) < sizeof(ip_header_t)))
|
|---|
| [21580dd] | 217 | return ENOMEM;
|
|---|
| [a64c64d] | 218 |
|
|---|
| [9b9d1c95] | 219 | if (protocol)
|
|---|
| [aadf01e] | 220 | *protocol = header->protocol;
|
|---|
| [9b9d1c95] | 221 | if (ttl)
|
|---|
| [aadf01e] | 222 | *ttl = header->ttl;
|
|---|
| [9b9d1c95] | 223 | if (tos)
|
|---|
| [aadf01e] | 224 | *tos = header->tos;
|
|---|
| [9b9d1c95] | 225 | if (dont_fragment)
|
|---|
| 226 | *dont_fragment = header->flags & IPFLAG_DONT_FRAGMENT;
|
|---|
| 227 | if (ipopt_length) {
|
|---|
| [aadf01e] | 228 | *ipopt_length = IP_HEADER_LENGTH(header) - sizeof(ip_header_t);
|
|---|
| 229 | return sizeof(ip_header_t);
|
|---|
| [9b9d1c95] | 230 | } else {
|
|---|
| [aadf01e] | 231 | return IP_HEADER_LENGTH(header);
|
|---|
| [21580dd] | 232 | }
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| [9b9d1c95] | 235 | /** Updates the IPv4 pseudo header data length field.
|
|---|
| 236 | *
|
|---|
| 237 | * @param[in,out] header The IPv4 pseudo header to be updated.
|
|---|
| 238 | * @param[in] headerlen The length of the IP pseudo header in bytes.
|
|---|
| 239 | * @param[in] data_length The data length to be set.
|
|---|
| 240 | * @returns EOK on success.
|
|---|
| 241 | * @returns EBADMEM if the header parameter is NULL.
|
|---|
| 242 | * @returns EINVAL if the headerlen parameter is not IPv4 pseudo
|
|---|
| 243 | * header length.
|
|---|
| 244 | */
|
|---|
| 245 | int
|
|---|
| 246 | ip_client_set_pseudo_header_data_length(void *header, size_t headerlen,
|
|---|
| 247 | size_t data_length)
|
|---|
| 248 | {
|
|---|
| [aadf01e] | 249 | ipv4_pseudo_header_ref header_in;
|
|---|
| [21580dd] | 250 |
|
|---|
| [9b9d1c95] | 251 | if (!header)
|
|---|
| [aadf01e] | 252 | return EBADMEM;
|
|---|
| [a64c64d] | 253 |
|
|---|
| [9b9d1c95] | 254 | if (headerlen == sizeof(ipv4_pseudo_header_t)) {
|
|---|
| [aadf01e] | 255 | header_in = (ipv4_pseudo_header_ref) header;
|
|---|
| 256 | header_in->data_length = htons(data_length);
|
|---|
| [21580dd] | 257 | return EOK;
|
|---|
| [a64c64d] | 258 | // TODO IPv6
|
|---|
| [9b9d1c95] | 259 | } else {
|
|---|
| [21580dd] | 260 | return EINVAL;
|
|---|
| 261 | }
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | /** @}
|
|---|
| 265 | */
|
|---|