10 #include <ipxe/list.h>
11 #include <ipxe/icmp6.h>
12 #include <ipxe/tcpip.h>
13 #include <ipxe/socket.h>
14 #include <ipxe/iobuf.h>
15 #include <ipxe/netdevice.h>
16 #include <ipxe/if_ether.h>
18 /* Unspecified IP6 address */
19 static struct in6_addr ip6_none = {
20 .in6_u.u6_addr32 = { 0,0,0,0 }
23 /** An IPv6 routing table entry */
24 struct ipv6_miniroute {
25 /* List of miniroutes */
26 struct list_head list;
29 struct net_device *netdev;
31 /* Destination prefix */
32 struct in6_addr prefix;
35 /* IPv6 address of interface */
36 struct in6_addr address;
38 struct in6_addr gateway;
41 /** List of IPv6 miniroutes */
42 static LIST_HEAD ( miniroutes );
45 * Add IPv6 minirouting table entry
47 * @v netdev Network device
48 * @v prefix Destination prefix
49 * @v address Address of the interface
50 * @v gateway Gateway address (or ::0 for no gateway)
51 * @ret miniroute Routing table entry, or NULL
53 static struct ipv6_miniroute * __malloc
54 add_ipv6_miniroute ( struct net_device *netdev, struct in6_addr prefix,
55 int prefix_len, struct in6_addr address,
56 struct in6_addr gateway ) {
57 struct ipv6_miniroute *miniroute;
59 miniroute = malloc ( sizeof ( *miniroute ) );
61 /* Record routing information */
62 miniroute->netdev = netdev_get ( netdev );
63 miniroute->prefix = prefix;
64 miniroute->prefix_len = prefix_len;
65 miniroute->address = address;
66 miniroute->gateway = gateway;
68 /* Add miniroute to list of miniroutes */
69 if ( !IP6_EQUAL ( gateway, ip6_none ) ) {
70 list_add_tail ( &miniroute->list, &miniroutes );
72 list_add ( &miniroute->list, &miniroutes );
80 * Delete IPv6 minirouting table entry
82 * @v miniroute Routing table entry
84 static void del_ipv6_miniroute ( struct ipv6_miniroute *miniroute ) {
85 netdev_put ( miniroute->netdev );
86 list_del ( &miniroute->list );
93 * @v netdev Network device
94 * @v prefix Destination prefix
95 * @v address Address of the interface
96 * @v gateway Gateway address (or ::0 for no gateway)
98 int add_ipv6_address ( struct net_device *netdev, struct in6_addr prefix,
99 int prefix_len, struct in6_addr address,
100 struct in6_addr gateway ) {
101 struct ipv6_miniroute *miniroute;
103 /* Clear any existing address for this net device */
104 del_ipv6_address ( netdev );
106 /* Add new miniroute */
107 miniroute = add_ipv6_miniroute ( netdev, prefix, prefix_len, address,
116 * Remove IPv6 interface
118 * @v netdev Network device
120 void del_ipv6_address ( struct net_device *netdev ) {
121 struct ipv6_miniroute *miniroute;
123 list_for_each_entry ( miniroute, &miniroutes, list ) {
124 if ( miniroute->netdev == netdev ) {
125 del_ipv6_miniroute ( miniroute );
132 * Calculate TCPIP checksum
134 * @v iobuf I/O buffer
135 * @v tcpip TCP/IP protocol
137 * This function constructs the pseudo header and completes the checksum in the
138 * upper layer header.
140 static uint16_t ipv6_tx_csum ( struct io_buffer *iobuf, uint16_t csum ) {
141 struct ip6_header *ip6hdr = iobuf->data;
142 struct ipv6_pseudo_header pshdr;
144 /* Calculate pseudo header */
145 memset ( &pshdr, 0, sizeof ( pshdr ) );
146 pshdr.src = ip6hdr->src;
147 pshdr.dest = ip6hdr->dest;
148 pshdr.len = htons ( iob_len ( iobuf ) - sizeof ( *ip6hdr ) );
149 pshdr.nxt_hdr = ip6hdr->nxt_hdr;
151 /* Update checksum value */
152 return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
156 * Dump IP6 header for debugging
160 void ipv6_dump ( struct ip6_header *ip6hdr ) {
161 DBG ( "IP6 %p src %s dest %s nxt_hdr %d len %d\n", ip6hdr,
162 inet6_ntoa ( ip6hdr->src ), inet6_ntoa ( ip6hdr->dest ),
163 ip6hdr->nxt_hdr, ntohs ( ip6hdr->payload_len ) );
167 * Transmit IP6 packet
170 * tcpip TCP/IP protocol
171 * st_dest Destination socket address
173 * This function prepends the IPv6 headers to the payload an transmits it.
175 static int ipv6_tx ( struct io_buffer *iobuf,
176 struct tcpip_protocol *tcpip,
177 struct sockaddr_tcpip *st_src __unused,
178 struct sockaddr_tcpip *st_dest,
179 struct net_device *netdev,
180 uint16_t *trans_csum ) {
181 struct sockaddr_in6 *dest = ( struct sockaddr_in6* ) st_dest;
182 struct in6_addr next_hop;
183 struct ipv6_miniroute *miniroute;
184 uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
185 const uint8_t *ll_dest = ll_dest_buf;
188 /* Construct the IPv6 packet */
189 struct ip6_header *ip6hdr = iob_push ( iobuf, sizeof ( *ip6hdr ) );
190 memset ( ip6hdr, 0, sizeof ( *ip6hdr) );
191 ip6hdr->ver_traffic_class_flow_label = htonl ( 0x60000000 );//IP6_VERSION;
192 ip6hdr->payload_len = htons ( iob_len ( iobuf ) - sizeof ( *ip6hdr ) );
193 ip6hdr->nxt_hdr = tcpip->tcpip_proto;
194 ip6hdr->hop_limit = IP6_HOP_LIMIT; // 255
196 /* Determine the next hop address and interface
198 * TODO: Implement the routing table.
200 next_hop = dest->sin6_addr;
201 list_for_each_entry ( miniroute, &miniroutes, list ) {
202 if ( ( memcmp ( &ip6hdr->dest, &miniroute->prefix,
203 miniroute->prefix_len ) == 0 ) ||
204 ( IP6_EQUAL ( miniroute->gateway, ip6_none ) ) ) {
205 netdev = miniroute->netdev;
206 ip6hdr->src = miniroute->address;
207 if ( ! ( IS_UNSPECIFIED ( miniroute->gateway ) ) ) {
208 next_hop = miniroute->gateway;
213 /* No network interface identified */
215 DBG ( "No route to host %s\n", inet6_ntoa ( ip6hdr->dest ) );
220 /* Complete the transport layer checksum */
222 *trans_csum = ipv6_tx_csum ( iobuf, *trans_csum );
224 /* Print IPv6 header */
225 ipv6_dump ( ip6hdr );
227 /* Resolve link layer address */
228 if ( next_hop.in6_u.u6_addr8[0] == 0xff ) {
229 ll_dest_buf[0] = 0x33;
230 ll_dest_buf[1] = 0x33;
231 ll_dest_buf[2] = next_hop.in6_u.u6_addr8[12];
232 ll_dest_buf[3] = next_hop.in6_u.u6_addr8[13];
233 ll_dest_buf[4] = next_hop.in6_u.u6_addr8[14];
234 ll_dest_buf[5] = next_hop.in6_u.u6_addr8[15];
236 /* Unicast address needs to be resolved by NDP */
237 if ( ( rc = ndp_resolve ( netdev, &next_hop, &ip6hdr->src,
238 ll_dest_buf ) ) != 0 ) {
239 DBG ( "No entry for %s\n", inet6_ntoa ( next_hop ) );
244 /* Transmit packet */
245 return net_tx ( iobuf, netdev, &ipv6_protocol, ll_dest,
254 * Process next IP6 header
256 * @v iobuf I/O buffer
257 * @v nxt_hdr Next header number
258 * @v src Source socket address
259 * @v dest Destination socket address
261 * Refer http://www.iana.org/assignments/ipv6-parameters for the numbers
263 static int ipv6_process_nxt_hdr ( struct io_buffer *iobuf, uint8_t nxt_hdr,
264 struct sockaddr_tcpip *src, struct sockaddr_tcpip *dest ) {
269 case IP6_AUTHENTICATION:
272 DBG ( "Function not implemented for header %d\n", nxt_hdr );
277 DBG ( "No next header\n" );
280 /* Next header is not a IPv6 extension header */
281 return tcpip_rx ( iobuf, nxt_hdr, src, dest, 0 /* fixme */ );
285 * Process incoming IP6 packets
287 * @v iobuf I/O buffer
288 * @v netdev Network device
289 * @v ll_dest Link-layer destination address
290 * @v ll_source Link-layer source address
291 * @v flags Packet flags
293 * This function processes a IPv6 packet
295 static int ipv6_rx ( struct io_buffer *iobuf,
296 __unused struct net_device *netdev,
297 __unused const void *ll_dest,
298 __unused const void *ll_source,
299 __unused unsigned int flags ) {
301 struct ip6_header *ip6hdr = iobuf->data;
303 struct sockaddr_in6 sin6;
304 struct sockaddr_tcpip st;
308 if ( iob_len ( iobuf ) < sizeof ( *ip6hdr ) ) {
309 DBG ( "Packet too short (%zd bytes)\n", iob_len ( iobuf ) );
313 /* TODO: Verify checksum */
315 /* Print IP6 header for debugging */
316 ipv6_dump ( ip6hdr );
318 /* Check header version */
319 if ( ( ip6hdr->ver_traffic_class_flow_label & 0xf0000000 ) != 0x60000000 ) {
320 DBG ( "Invalid protocol version\n" );
324 /* Check the payload length */
325 if ( ntohs ( ip6hdr->payload_len ) > iob_len ( iobuf ) ) {
326 DBG ( "Inconsistent packet length (%d bytes)\n",
327 ip6hdr->payload_len );
331 /* Ignore the traffic class and flow control values */
333 /* Construct socket address */
334 memset ( &src, 0, sizeof ( src ) );
335 src.sin6.sin_family = AF_INET6;
336 src.sin6.sin6_addr = ip6hdr->src;
337 memset ( &dest, 0, sizeof ( dest ) );
338 dest.sin6.sin_family = AF_INET6;
339 dest.sin6.sin6_addr = ip6hdr->dest;
342 iob_unput ( iobuf, iob_len ( iobuf ) - ntohs ( ip6hdr->payload_len ) -
343 sizeof ( *ip6hdr ) );
344 iob_pull ( iobuf, sizeof ( *ip6hdr ) );
346 /* Send it to the transport layer */
347 return ipv6_process_nxt_hdr ( iobuf, ip6hdr->nxt_hdr, &src.st, &dest.st );
350 DBG ( "Packet dropped\n" );
356 * Print a IP6 address as xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
358 char * inet6_ntoa ( struct in6_addr in6 ) {
360 uint16_t *bytes = ( uint16_t* ) &in6;
361 sprintf ( buf, "%x:%x:%x:%x:%x:%x:%x:%x", bytes[0], bytes[1], bytes[2],
362 bytes[3], bytes[4], bytes[5], bytes[6], bytes[7] );
366 static const char * ipv6_ntoa ( const void *net_addr ) {
367 return inet6_ntoa ( * ( ( struct in6_addr * ) net_addr ) );
371 struct net_protocol ipv6_protocol __net_protocol = {
373 .net_proto = htons ( ETH_P_IPV6 ),
374 .net_addr_len = sizeof ( struct in6_addr ),
379 /** IPv6 TCPIP net protocol */
380 struct tcpip_net_protocol ipv6_tcpip_protocol __tcpip_net_protocol = {
382 .sa_family = AF_INET6,