10 #include <gpxe/list.h>
11 #include <gpxe/icmp6.h>
12 #include <gpxe/tcpip.h>
13 #include <gpxe/socket.h>
14 #include <gpxe/iobuf.h>
15 #include <gpxe/netdevice.h>
16 #include <gpxe/if_ether.h>
18 struct net_protocol ipv6_protocol;
20 /* Unspecified IP6 address */
21 static struct in6_addr ip6_none = {
22 .in6_u.u6_addr32[0] = 0,
23 .in6_u.u6_addr32[1] = 0,
24 .in6_u.u6_addr32[2] = 0,
25 .in6_u.u6_addr32[3] = 0,
28 /** An IPv6 routing table entry */
29 struct ipv6_miniroute {
30 /* List of miniroutes */
31 struct list_head list;
34 struct net_device *netdev;
36 /* Destination prefix */
37 struct in6_addr prefix;
40 /* IPv6 address of interface */
41 struct in6_addr address;
43 struct in6_addr gateway;
46 /** List of IPv6 miniroutes */
47 static LIST_HEAD ( miniroutes );
50 * Add IPv6 minirouting table entry
52 * @v netdev Network device
53 * @v prefix Destination prefix
54 * @v address Address of the interface
55 * @v gateway Gateway address (or ::0 for no gateway)
56 * @ret miniroute Routing table entry, or NULL
58 static struct ipv6_miniroute * add_ipv6_miniroute ( struct net_device *netdev,
59 struct in6_addr prefix,
61 struct in6_addr address,
62 struct in6_addr gateway ) {
63 struct ipv6_miniroute *miniroute;
65 miniroute = malloc ( sizeof ( *miniroute ) );
67 /* Record routing information */
68 miniroute->netdev = netdev_get ( netdev );
69 miniroute->prefix = prefix;
70 miniroute->prefix_len = prefix_len;
71 miniroute->address = address;
72 miniroute->gateway = gateway;
74 /* Add miniroute to list of miniroutes */
75 if ( !IP6_EQUAL ( gateway, ip6_none ) ) {
76 list_add_tail ( &miniroute->list, &miniroutes );
78 list_add ( &miniroute->list, &miniroutes );
86 * Delete IPv6 minirouting table entry
88 * @v miniroute Routing table entry
90 static void del_ipv6_miniroute ( struct ipv6_miniroute *miniroute ) {
91 netdev_put ( miniroute->netdev );
92 list_del ( &miniroute->list );
99 * @v netdev Network device
100 * @v prefix Destination prefix
101 * @v address Address of the interface
102 * @v gateway Gateway address (or ::0 for no gateway)
104 int add_ipv6_address ( struct net_device *netdev, struct in6_addr prefix,
105 int prefix_len, struct in6_addr address,
106 struct in6_addr gateway ) {
107 struct ipv6_miniroute *miniroute;
109 /* Clear any existing address for this net device */
110 del_ipv6_address ( netdev );
112 /* Add new miniroute */
113 miniroute = add_ipv6_miniroute ( netdev, prefix, prefix_len, address,
122 * Remove IPv6 interface
124 * @v netdev Network device
126 void del_ipv6_address ( struct net_device *netdev ) {
127 struct ipv6_miniroute *miniroute;
129 list_for_each_entry ( miniroute, &miniroutes, list ) {
130 if ( miniroute->netdev == netdev ) {
131 del_ipv6_miniroute ( miniroute );
138 * Calculate TCPIP checksum
140 * @v iobuf I/O buffer
141 * @v tcpip TCP/IP protocol
143 * This function constructs the pseudo header and completes the checksum in the
144 * upper layer header.
146 static uint16_t ipv6_tx_csum ( struct io_buffer *iobuf, uint16_t csum ) {
147 struct ip6_header *ip6hdr = iobuf->data;
148 struct ipv6_pseudo_header pshdr;
150 /* Calculate pseudo header */
151 memset ( &pshdr, 0, sizeof ( pshdr ) );
152 pshdr.src = ip6hdr->src;
153 pshdr.dest = ip6hdr->dest;
154 pshdr.len = htons ( iob_len ( iobuf ) - sizeof ( *ip6hdr ) );
155 pshdr.nxt_hdr = ip6hdr->nxt_hdr;
157 /* Update checksum value */
158 return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
162 * Dump IP6 header for debugging
166 void ipv6_dump ( struct ip6_header *ip6hdr ) {
167 DBG ( "IP6 %p src %s dest %s nxt_hdr %d len %d\n", ip6hdr,
168 inet6_ntoa ( ip6hdr->src ), inet6_ntoa ( ip6hdr->dest ),
169 ip6hdr->nxt_hdr, ntohs ( ip6hdr->payload_len ) );
173 * Transmit IP6 packet
176 * tcpip TCP/IP protocol
177 * st_dest Destination socket address
179 * This function prepends the IPv6 headers to the payload an transmits it.
181 static int ipv6_tx ( struct io_buffer *iobuf,
182 struct tcpip_protocol *tcpip,
183 struct sockaddr_tcpip *st_dest,
184 struct net_device *netdev,
185 uint16_t *trans_csum ) {
186 struct sockaddr_in6 *dest = ( struct sockaddr_in6* ) st_dest;
187 struct in6_addr next_hop;
188 struct ipv6_miniroute *miniroute;
189 uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
190 const uint8_t *ll_dest = ll_dest_buf;
193 /* Construct the IPv6 packet */
194 struct ip6_header *ip6hdr = iob_push ( iobuf, sizeof ( *ip6hdr ) );
195 memset ( ip6hdr, 0, sizeof ( *ip6hdr) );
196 ip6hdr->ver_traffic_class_flow_label = htonl ( 0x60000000 );//IP6_VERSION;
197 ip6hdr->payload_len = htons ( iob_len ( iobuf ) - sizeof ( *ip6hdr ) );
198 ip6hdr->nxt_hdr = tcpip->tcpip_proto;
199 ip6hdr->hop_limit = IP6_HOP_LIMIT; // 255
201 /* Determine the next hop address and interface
203 * TODO: Implement the routing table.
205 next_hop = dest->sin6_addr;
206 list_for_each_entry ( miniroute, &miniroutes, list ) {
207 if ( ( strncmp ( &ip6hdr->dest, &miniroute->prefix,
208 miniroute->prefix_len ) == 0 ) ||
209 ( IP6_EQUAL ( miniroute->gateway, ip6_none ) ) ) {
210 netdev = miniroute->netdev;
211 ip6hdr->src = miniroute->address;
212 if ( ! ( IS_UNSPECIFIED ( miniroute->gateway ) ) ) {
213 next_hop = miniroute->gateway;
218 /* No network interface identified */
220 DBG ( "No route to host %s\n", inet6_ntoa ( ip6hdr->dest ) );
225 /* Complete the transport layer checksum */
227 *trans_csum = ipv6_tx_csum ( iobuf, *trans_csum );
229 /* Print IPv6 header */
230 ipv6_dump ( ip6hdr );
232 /* Resolve link layer address */
233 if ( next_hop.in6_u.u6_addr8[0] == 0xff ) {
234 ll_dest_buf[0] = 0x33;
235 ll_dest_buf[1] = 0x33;
236 ll_dest_buf[2] = next_hop.in6_u.u6_addr8[12];
237 ll_dest_buf[3] = next_hop.in6_u.u6_addr8[13];
238 ll_dest_buf[4] = next_hop.in6_u.u6_addr8[14];
239 ll_dest_buf[5] = next_hop.in6_u.u6_addr8[15];
241 /* Unicast address needs to be resolved by NDP */
242 if ( ( rc = ndp_resolve ( netdev, &next_hop, &ip6hdr->src,
243 ll_dest_buf ) ) != 0 ) {
244 DBG ( "No entry for %s\n", inet6_ntoa ( next_hop ) );
249 /* Transmit packet */
250 return net_tx ( iobuf, netdev, &ipv6_protocol, ll_dest );
258 * Process next IP6 header
260 * @v iobuf I/O buffer
261 * @v nxt_hdr Next header number
262 * @v src Source socket address
263 * @v dest Destination socket address
265 * Refer http://www.iana.org/assignments/ipv6-parameters for the numbers
267 static int ipv6_process_nxt_hdr ( struct io_buffer *iobuf, uint8_t nxt_hdr,
268 struct sockaddr_tcpip *src, struct sockaddr_tcpip *dest ) {
273 case IP6_AUTHENTICATION:
276 DBG ( "Function not implemented for header %d\n", nxt_hdr );
281 DBG ( "No next header\n" );
284 /* Next header is not a IPv6 extension header */
285 return tcpip_rx ( iobuf, nxt_hdr, src, dest, 0 /* fixme */ );
289 * Process incoming IP6 packets
291 * @v iobuf I/O buffer
292 * @v netdev Network device
293 * @v ll_source Link-layer source address
295 * This function processes a IPv6 packet
297 static int ipv6_rx ( struct io_buffer *iobuf,
298 struct net_device *netdev,
299 const void *ll_source ) {
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 (%d 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,