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_SOLIC_NODE_PREFIX 0xFF02000000000000ULL
30 #define IPV6_SOLIC_NODE_IFACE_ID 0x00000001FF000000ULL
36 uint8_t addr[IPV6_ADDR_LENGTH];
39 uint64_t interface_id;
52 /** \struct prefix_info
54 * List of Prefixes we have adresses from
55 * Used for internal purposes, information derived from prefix option
56 * in Router Advertisements
57 * See RFC 4861 section 4.6.2
61 uint8_t on_link:1, /* When set prefix can be used for on-link
63 autoconf:1, /* Prefix can be used for stateless address
66 uint32_t valid_lifetime; /* Time until prefix expires */
67 uint32_t preferred_lifetime; /* Time until prefix becomes deprecated */
68 uint32_t start_time; /* Time when received */
70 struct prefix_info *next;
74 /* List of IPv6 addresses */
75 struct ip6addr_list_entry {
77 struct prefix_info prfx_info;
78 struct ip6addr_list_entry *next;
82 * A header for IPv6 packets.
83 * For more information see RFC 2460
86 uint32_t ver_tc_fl; /**< Version, Traffic class, Flow label */
87 uint16_t pl; /**< Payload length */
88 uint8_t nh; /**< Next header */
89 uint8_t hl; /**< Hop limit */
90 ip6_addr_t src; /**< IPv6 source address */
91 ip6_addr_t dst; /**< IPv6 destination address */
92 } __attribute((packed));
95 * Struct with pointers to headers within a packet
100 struct icmp6hdr *icmp6h;
105 /** \struct parseip6_state
106 * Stores information about state of IPv6 address parser
108 struct parseip6_state {
122 /** \struct ip6_config
123 * Stores flags wheter we use Stateless- or Stateful Autoconfiguration or DHCPv6
126 uint8_t managed_mode:1,
131 /******************** VARIABLES **********************************************/
132 /* Function pointer send_ip. Points either to send_ipv4() or send_ipv6() */
133 extern int (*send_ip) (int fd, void *, int);
135 /* IPv6 link-local multicast addresses */
136 struct ip6addr_list_entry all_routers_ll; // Routers
137 struct ip6addr_list_entry all_dhcpv6_ll; // DHCPv6 servers
138 struct ip6addr_list_entry all_nodes_ll; // All IPv6 nodes
140 /* List of Ipv6 Addresses */
141 struct ip6addr_list_entry *first_ip6;
142 struct ip6addr_list_entry *last_ip6;
145 struct neighbor *first_neighbor;
146 struct neighbor *last_neighbor;
149 struct router *first_router;
150 struct router *last_router;
152 /******************** FUNCTIONS *********************************************/
153 /* Handles IPv6-packets that are detected by receive_ether. */
154 int8_t handle_ipv6(int fd, uint8_t * ip6_packet, int32_t packetsize);
156 /* Fill IPv6 header */
157 void fill_ip6hdr(uint8_t * packet, uint16_t packetsize,
158 uint8_t ip_proto, ip6_addr_t *ip6_src, ip6_addr_t *ip6_dst);
160 /* Set own IPv6 address */
161 void set_ipv6_address(int fd, ip6_addr_t *own_ip6);
163 /* Get own IPv6 address */
164 ip6_addr_t *get_ipv6_address(void);
166 /* Create link-local address from a given Mac Address */
167 ip6_addr_t * ip6_create_ll_address (const uint8_t *own_mac);
169 /* For a given MAC calculates EUI64-Identifier.*/
170 uint64_t mac2eui64 (const uint8_t *mac);
172 /* Create empty element for prefix list and return a pointer to it */
173 struct prefix_info * ip6_create_prefix_info(void);
175 /* Create a new IPv6 address with a given network prefix
176 * and add it to our IPv6 address list */
177 void * ip6_prefix2addr (ip6_addr_t prefix);
179 /* Compare IPv6 adresses */
180 int8_t ip6_cmp( ip6_addr_t *ip_1, ip6_addr_t *ip_2 );
182 /* Check if prefix is already in our list */
183 int8_t unknown_prefix (ip6_addr_t *ip);
185 /* Send IPv6 packet */
186 int send_ipv6 (int fd, void* buffer, int len);
188 /* Add IPv6 address to list */
189 int8_t ip6addr_add (struct ip6addr_list_entry *new_address);
191 /* Parse an IPv6 address */
192 int parseip6(const char *addr, uint8_t *parsedaddr);
193 int str_to_ipv6(const char *str, uint8_t *ip);
194 void ipv6_to_str(const uint8_t *ip, char *str);