1 /******************************************************************************
2 * Copyright (c) 2013 IBM Corporation
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
17 #include <netlib/ethernet.h>
19 #define __IPV6_DEBUG__
22 #define IPV6_DEBUG_PRINT(format, ...) do { printf(format, ## __VA_ARGS__); } while (0)
24 #define IPV6_DEBUG_PRINT(format, ...)
27 #define IPV6_ADDR_LENGTH 16 /* Size of IPv6 adress in bytes */
28 #define IPV6_LL_PREFIX 0xFE80000000000000ULL
29 #define IPV6_LL_PREFIX_MASK 0xFFC0000000000000ULL
30 #define IPV6_SOLIC_NODE_PREFIX 0xFF02000000000000ULL
31 #define IPV6_SOLIC_NODE_IFACE_ID 0x00000001FF000000ULL
37 uint8_t addr[IPV6_ADDR_LENGTH];
40 uint64_t interface_id;
53 /** \struct prefix_info
55 * List of Prefixes we have adresses from
56 * Used for internal purposes, information derived from prefix option
57 * in Router Advertisements
58 * See RFC 4861 section 4.6.2
62 uint8_t on_link:1, /* When set prefix can be used for on-link
64 autoconf:1, /* Prefix can be used for stateless address
67 uint32_t valid_lifetime; /* Time until prefix expires */
68 uint32_t preferred_lifetime; /* Time until prefix becomes deprecated */
69 uint32_t start_time; /* Time when received */
71 struct prefix_info *next;
75 /* List of IPv6 addresses */
76 struct ip6addr_list_entry {
78 struct prefix_info prfx_info;
79 struct ip6addr_list_entry *next;
83 * A header for IPv6 packets.
84 * For more information see RFC 2460
87 uint32_t ver_tc_fl; /**< Version, Traffic class, Flow label */
88 uint16_t pl; /**< Payload length */
89 uint8_t nh; /**< Next header */
90 uint8_t hl; /**< Hop limit */
91 ip6_addr_t src; /**< IPv6 source address */
92 ip6_addr_t dst; /**< IPv6 destination address */
93 } __attribute((packed));
96 * Struct with pointers to headers within a packet
101 struct icmp6hdr *icmp6h;
106 /** \struct parseip6_state
107 * Stores information about state of IPv6 address parser
109 struct parseip6_state {
123 /** \struct ip6_config
124 * Stores flags wheter we use Stateless- or Stateful Autoconfiguration or DHCPv6
127 uint8_t managed_mode:1,
132 /******************** VARIABLES **********************************************/
133 /* Function pointer send_ip. Points either to send_ipv4() or send_ipv6() */
134 extern int (*send_ip) (int fd, void *, int);
136 extern struct ip6_config ip6_state;
138 /******************** FUNCTIONS *********************************************/
139 /* Handles IPv6-packets that are detected by receive_ether. */
140 int8_t handle_ipv6(int fd, uint8_t * ip6_packet, uint32_t packetsize);
142 /* Fill IPv6 header */
143 void fill_ip6hdr(uint8_t * packet, uint16_t packetsize,
144 uint8_t ip_proto, ip6_addr_t *ip6_src, ip6_addr_t *ip6_dst);
146 /* Set own IPv6 address */
147 void set_ipv6_address(int fd, ip6_addr_t *own_ip6);
149 /* Get own IPv6 address */
150 ip6_addr_t *get_ipv6_address(void);
152 /* Create link-local address from a given Mac Address */
153 ip6_addr_t * ip6_create_ll_address (const uint8_t *own_mac);
155 /* For a given MAC calculates EUI64-Identifier.*/
156 uint64_t mac2eui64 (const uint8_t *mac);
158 /* Create empty element for prefix list and return a pointer to it */
159 struct prefix_info * ip6_create_prefix_info(void);
161 /* Create a new IPv6 address with a given network prefix
162 * and add it to our IPv6 address list */
163 void * ip6_prefix2addr (ip6_addr_t prefix);
165 /* Compare IPv6 adresses */
166 int8_t ip6_cmp( ip6_addr_t *ip_1, ip6_addr_t *ip_2 );
168 /* Check if it is a link-local address */
169 static inline int ip6_is_linklocal(ip6_addr_t *ip)
171 return (ip->part.prefix & IPV6_LL_PREFIX_MASK) == IPV6_LL_PREFIX;
174 /* Check if prefix is already in our list */
175 int8_t unknown_prefix (ip6_addr_t *ip);
177 /* Send IPv6 packet */
178 int send_ipv6 (int fd, void* buffer, int len);
180 /* Add IPv6 address to list */
181 int8_t ip6addr_add (struct ip6addr_list_entry *new_address);
183 /* Parse an IPv6 address */
184 int parseip6(const char *addr, uint8_t *parsedaddr);
185 int str_to_ipv6(const char *str, uint8_t *ip);
186 void ipv6_to_str(const uint8_t *ip, char *str);