Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / SLOF / clients / net-snk / app / netlib / ipv6.h
1 /******************************************************************************
2  * Copyright (c) 2013 IBM Corporation
3  * All rights reserved.
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
8  *
9  * Contributors:
10  *     IBM Corporation - initial implementation
11  *****************************************************************************/
12
13 #ifndef _IPV6_H_
14 #define _IPV6_H_
15
16 #include <stdint.h>
17 #include <netlib/ethernet.h>
18
19 #define __IPV6_DEBUG__
20
21 #ifdef __IPV6_DEBUG__
22 #define IPV6_DEBUG_PRINT(format, ...) do { printf(format, ## __VA_ARGS__); } while (0)
23 #else
24 #define IPV6_DEBUG_PRINT(format, ...)
25 #endif
26
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
31
32 /**
33  *  An IPv6 Address
34  */
35 typedef union {
36         uint8_t addr[IPV6_ADDR_LENGTH];
37         struct {
38                 uint64_t prefix;
39                 uint64_t interface_id;
40         } part;
41 } ip6_addr_t;
42
43 typedef struct {
44         uint8_t type;
45         uint8_t pad[7];
46         union {
47                 ip6_addr_t  v6;
48                 char        v4[4];
49         } addr;
50 } netaddr_t;
51
52 /** \struct prefix_info
53  *
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
58  */
59 struct prefix_info {
60         uint64_t prefix;
61         uint8_t  on_link:1,         /* When set prefix can be used for on-link
62                                      * determination */
63                  autoconf:1,        /* Prefix can be used for stateless address
64                                      * configuration */
65                  reserved1:6;
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 */
69         uint32_t reserved2;
70         struct   prefix_info *next;
71 };
72
73
74 /* List of IPv6 addresses */
75 struct ip6addr_list_entry {
76         ip6_addr_t addr;
77         struct prefix_info prfx_info;
78         struct ip6addr_list_entry *next;
79 };
80
81 /** \struct ip6hdr
82  *  A header for IPv6 packets.
83  *  For more information see RFC 2460
84  */
85 struct ip6hdr {
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));
93
94 /** \struct packeth
95  * Struct with pointers to headers within a packet
96  */
97 struct packeth {
98         struct ethhdr  *ethh;
99         struct ip6hdr  *ip6h;
100         struct icmp6hdr  *icmp6h;
101         struct udphdr  *udph;
102         /* ... */
103 };
104
105 /** \struct parseip6_state
106  * Stores information about state of IPv6 address parser
107  */
108 struct parseip6_state {
109         char *lookahead;
110         char *ptr;
111         const char *addr;
112         int state;
113         int s1ctr;
114         int s2ctr;
115         int blocknr;
116         int zeroblocks;
117         int i;
118         int done;
119         int errorcode;
120 };
121
122 /** \struct ip6_config
123  * Stores flags wheter we use Stateless- or Stateful Autoconfiguration or DHCPv6
124  */
125 struct ip6_config {
126         uint8_t managed_mode:1,
127                 other_config:1,
128                 reserved:6;
129 } ip6_state;
130
131 /******************** VARIABLES **********************************************/
132 /* Function pointer send_ip. Points either to send_ipv4() or send_ipv6() */
133 extern int   (*send_ip) (int fd, void *, int);
134
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
139
140 /* List of Ipv6 Addresses */
141 struct ip6addr_list_entry *first_ip6;
142 struct ip6addr_list_entry *last_ip6;
143
144 /* Neighbor cache */
145 struct neighbor *first_neighbor;
146 struct neighbor *last_neighbor;
147
148 /* Router list */
149 struct router *first_router;
150 struct router *last_router;
151
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);
155
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);
159
160 /* Set own IPv6 address */
161 void set_ipv6_address(int fd, ip6_addr_t *own_ip6);
162
163 /* Get own IPv6 address */
164 ip6_addr_t *get_ipv6_address(void);
165
166 /* Create link-local address from a given Mac Address */
167 ip6_addr_t * ip6_create_ll_address (const uint8_t *own_mac);
168
169 /* For a given MAC calculates EUI64-Identifier.*/
170 uint64_t mac2eui64 (const uint8_t *mac);
171
172 /* Create empty element for prefix list and return a pointer to it */
173 struct prefix_info * ip6_create_prefix_info(void);
174
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);
178
179 /* Compare IPv6 adresses */
180 int8_t ip6_cmp( ip6_addr_t *ip_1, ip6_addr_t *ip_2 );
181
182 /* Check if prefix is already in our list */
183 int8_t unknown_prefix (ip6_addr_t *ip);
184
185 /* Send IPv6 packet */
186 int send_ipv6 (int fd, void* buffer, int len);
187
188 /* Add IPv6 address to list */
189 int8_t ip6addr_add (struct ip6addr_list_entry *new_address);
190
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);
195
196 #endif