These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / net / ipv6.c
1 /*
2  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <assert.h>
28 #include <byteswap.h>
29 #include <ipxe/iobuf.h>
30 #include <ipxe/tcpip.h>
31 #include <ipxe/if_ether.h>
32 #include <ipxe/crc32.h>
33 #include <ipxe/fragment.h>
34 #include <ipxe/ipstat.h>
35 #include <ipxe/ndp.h>
36 #include <ipxe/ipv6.h>
37
38 /** @file
39  *
40  * IPv6 protocol
41  *
42  */
43
44 /* Disambiguate the various error causes */
45 #define EINVAL_LEN __einfo_error ( EINFO_EINVAL_LEN )
46 #define EINFO_EINVAL_LEN \
47         __einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid length" )
48 #define ENOTSUP_VER __einfo_error ( EINFO_ENOTSUP_VER )
49 #define EINFO_ENOTSUP_VER \
50         __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported version" )
51 #define ENOTSUP_HDR __einfo_error ( EINFO_ENOTSUP_HDR )
52 #define EINFO_ENOTSUP_HDR \
53         __einfo_uniqify ( EINFO_ENOTSUP, 0x02, "Unsupported header type" )
54 #define ENOTSUP_OPT __einfo_error ( EINFO_ENOTSUP_OPT )
55 #define EINFO_ENOTSUP_OPT \
56         __einfo_uniqify ( EINFO_ENOTSUP, 0x03, "Unsupported option" )
57
58 /** List of IPv6 miniroutes */
59 struct list_head ipv6_miniroutes = LIST_HEAD_INIT ( ipv6_miniroutes );
60
61 /** IPv6 statistics */
62 static struct ip_statistics ipv6_stats;
63
64 /** IPv6 statistics family */
65 struct ip_statistics_family
66 ipv6_statistics_family __ip_statistics_family ( IP_STATISTICS_IPV6 ) = {
67         .version = 6,
68         .stats = &ipv6_stats,
69 };
70
71 /**
72  * Determine debugging colour for IPv6 debug messages
73  *
74  * @v in                IPv6 address
75  * @ret col             Debugging colour (for DBGC())
76  */
77 static uint32_t ipv6col ( struct in6_addr *in ) {
78         return crc32_le ( 0, in, sizeof ( *in ) );
79 }
80
81 /**
82  * Dump IPv6 routing table entry
83  *
84  * @v miniroute         Routing table entry
85  */
86 static inline __attribute__ (( always_inline )) void
87 ipv6_dump_miniroute ( struct ipv6_miniroute *miniroute ) {
88         struct net_device *netdev = miniroute->netdev;
89
90         DBGC ( netdev, "IPv6 %s has %s %s/%d", netdev->name,
91                ( ( miniroute->flags & IPV6_HAS_ADDRESS ) ?
92                  "address" : "prefix" ),
93                inet6_ntoa ( &miniroute->address ), miniroute->prefix_len );
94         if ( miniroute->flags & IPV6_HAS_ROUTER )
95                 DBGC ( netdev, " router %s", inet6_ntoa ( &miniroute->router ));
96         DBGC ( netdev, "\n" );
97 }
98
99 /**
100  * Check if network device has a specific IPv6 address
101  *
102  * @v netdev            Network device
103  * @v addr              IPv6 address
104  * @ret has_addr        Network device has this IPv6 address
105  */
106 int ipv6_has_addr ( struct net_device *netdev, struct in6_addr *addr ) {
107         struct ipv6_miniroute *miniroute;
108
109         list_for_each_entry ( miniroute, &ipv6_miniroutes, list ) {
110                 if ( ( miniroute->netdev == netdev ) &&
111                      ( miniroute->flags & IPV6_HAS_ADDRESS ) &&
112                      ( memcmp ( &miniroute->address, addr,
113                                 sizeof ( miniroute->address ) ) == 0 ) ) {
114                         /* Found matching address */
115                         return 1;
116                 }
117         }
118         return 0;
119 }
120
121 /**
122  * Check if IPv6 address is within a routing table entry's local network
123  *
124  * @v miniroute         Routing table entry
125  * @v address           IPv6 address
126  * @ret is_on_link      Address is within this entry's local network
127  */
128 static int ipv6_is_on_link ( struct ipv6_miniroute *miniroute,
129                              struct in6_addr *address ) {
130         unsigned int i;
131
132         for ( i = 0 ; i < ( sizeof ( address->s6_addr32 ) /
133                             sizeof ( address->s6_addr32[0] ) ) ; i++ ) {
134                 if ( (( address->s6_addr32[i] ^ miniroute->address.s6_addr32[i])
135                       & miniroute->prefix_mask.s6_addr32[i] ) != 0 )
136                         return 0;
137         }
138         return 1;
139 }
140
141 /**
142  * Find IPv6 routing table entry for a given address
143  *
144  * @v netdev            Network device
145  * @v address           IPv6 address
146  * @ret miniroute       Routing table entry, or NULL if not found
147  */
148 static struct ipv6_miniroute * ipv6_miniroute ( struct net_device *netdev,
149                                                 struct in6_addr *address ) {
150         struct ipv6_miniroute *miniroute;
151
152         list_for_each_entry ( miniroute, &ipv6_miniroutes, list ) {
153                 if ( ( miniroute->netdev == netdev ) &&
154                      ipv6_is_on_link ( miniroute, address ) ) {
155                         return miniroute;
156                 }
157         }
158         return NULL;
159 }
160
161 /**
162  * Add IPv6 routing table entry
163  *
164  * @v netdev            Network device
165  * @v address           IPv6 address (or prefix)
166  * @v prefix_len        Prefix length
167  * @v flags             Flags
168  * @ret miniroute       Routing table entry, or NULL on failure
169  */
170 static struct ipv6_miniroute * ipv6_add_miniroute ( struct net_device *netdev,
171                                                     struct in6_addr *address,
172                                                     unsigned int prefix_len,
173                                                     unsigned int flags ) {
174         struct ipv6_miniroute *miniroute;
175         uint8_t *prefix_mask;
176
177         /* Create routing table entry */
178         miniroute = zalloc ( sizeof ( *miniroute ) );
179         if ( ! miniroute )
180                 return NULL;
181         miniroute->netdev = netdev_get ( netdev );
182         memcpy ( &miniroute->address, address, sizeof ( miniroute->address ) );
183         miniroute->prefix_len = prefix_len;
184         assert ( prefix_len <= ( 8 * sizeof ( miniroute->prefix_mask ) ) );
185         for ( prefix_mask = miniroute->prefix_mask.s6_addr ; prefix_len >= 8 ;
186               prefix_mask++, prefix_len -= 8 ) {
187                 *prefix_mask = 0xff;
188         }
189         if ( prefix_len )
190                 *prefix_mask <<= ( 8 - prefix_len );
191         miniroute->flags = flags;
192         list_add ( &miniroute->list, &ipv6_miniroutes );
193         ipv6_dump_miniroute ( miniroute );
194
195         return miniroute;
196 }
197
198 /**
199  * Define IPv6 on-link prefix
200  *
201  * @v netdev            Network device
202  * @v prefix            IPv6 address prefix
203  * @v prefix_len        Prefix length
204  * @v router            Router address (or NULL)
205  * @ret rc              Return status code
206  */
207 int ipv6_set_prefix ( struct net_device *netdev, struct in6_addr *prefix,
208                       unsigned int prefix_len, struct in6_addr *router ) {
209         struct ipv6_miniroute *miniroute;
210         int changed;
211
212         /* Find or create routing table entry */
213         miniroute = ipv6_miniroute ( netdev, prefix );
214         if ( ! miniroute )
215                 miniroute = ipv6_add_miniroute ( netdev, prefix, prefix_len, 0);
216         if ( ! miniroute )
217                 return -ENOMEM;
218
219         /* Record router and add to start or end of list as appropriate */
220         list_del ( &miniroute->list );
221         if ( router ) {
222                 changed = ( ( ! ( miniroute->flags & IPV6_HAS_ROUTER ) ) ||
223                             ( memcmp ( &miniroute->router, router,
224                                        sizeof ( miniroute->router ) ) != 0 ) );
225                 miniroute->flags |= IPV6_HAS_ROUTER;
226                 memcpy ( &miniroute->router, router,
227                          sizeof ( miniroute->router ) );
228                 list_add_tail ( &miniroute->list, &ipv6_miniroutes );
229         } else {
230                 changed = ( miniroute->flags & IPV6_HAS_ROUTER );
231                 miniroute->flags &= ~IPV6_HAS_ROUTER;
232                 list_add ( &miniroute->list, &ipv6_miniroutes );
233         }
234         if ( changed )
235                 ipv6_dump_miniroute ( miniroute );
236
237         return 0;
238 }
239
240 /**
241  * Add IPv6 on-link address
242  *
243  * @v netdev            Network device
244  * @v address           IPv6 address
245  * @ret rc              Return status code
246  *
247  * An on-link prefix for the address must already exist.
248  */
249 int ipv6_set_address ( struct net_device *netdev, struct in6_addr *address ) {
250         struct ipv6_miniroute *miniroute;
251         int changed;
252
253         /* Find routing table entry */
254         miniroute = ipv6_miniroute ( netdev, address );
255         if ( ! miniroute )
256                 return -EADDRNOTAVAIL;
257
258         /* Record address */
259         changed = ( ( ! ( miniroute->flags & IPV6_HAS_ADDRESS ) ) ||
260                     ( memcmp ( &miniroute->address, address,
261                                sizeof ( miniroute->address ) ) != 0 ) );
262         memcpy ( &miniroute->address, address, sizeof ( miniroute->address ) );
263         miniroute->flags |= IPV6_HAS_ADDRESS;
264         if ( changed )
265                 ipv6_dump_miniroute ( miniroute );
266
267         return 0;
268 }
269
270 /**
271  * Perform IPv6 routing
272  *
273  * @v scope_id          Destination address scope ID (for link-local addresses)
274  * @v dest              Final destination address
275  * @ret dest            Next hop destination address
276  * @ret miniroute       Routing table entry to use, or NULL if no route
277  */
278 static struct ipv6_miniroute * ipv6_route ( unsigned int scope_id,
279                                             struct in6_addr **dest ) {
280         struct ipv6_miniroute *miniroute;
281
282         /* Find first usable route in routing table */
283         list_for_each_entry ( miniroute, &ipv6_miniroutes, list ) {
284
285                 /* Skip closed network devices */
286                 if ( ! netdev_is_open ( miniroute->netdev ) )
287                         continue;
288
289                 /* Skip routing table entries with no usable source address */
290                 if ( ! ( miniroute->flags & IPV6_HAS_ADDRESS ) )
291                         continue;
292
293                 if ( IN6_IS_ADDR_NONGLOBAL ( *dest ) ) {
294
295                         /* If destination is non-global, and the scope ID
296                          * matches this network device, then use this route.
297                          */
298                         if ( miniroute->netdev->index == scope_id )
299                                 return miniroute;
300
301                 } else {
302
303                         /* If destination is an on-link global
304                          * address, then use this route.
305                          */
306                         if ( ipv6_is_on_link ( miniroute, *dest ) )
307                                 return miniroute;
308
309                         /* If destination is an off-link global
310                          * address, and we have a default gateway,
311                          * then use this route.
312                          */
313                         if ( miniroute->flags & IPV6_HAS_ROUTER ) {
314                                 *dest = &miniroute->router;
315                                 return miniroute;
316                         }
317                 }
318         }
319
320         return NULL;
321 }
322
323 /**
324  * Determine transmitting network device
325  *
326  * @v st_dest           Destination network-layer address
327  * @ret netdev          Transmitting network device, or NULL
328  */
329 static struct net_device * ipv6_netdev ( struct sockaddr_tcpip *st_dest ) {
330         struct sockaddr_in6 *sin6_dest = ( ( struct sockaddr_in6 * ) st_dest );
331         struct in6_addr *dest = &sin6_dest->sin6_addr;
332         struct ipv6_miniroute *miniroute;
333
334         /* Find routing table entry */
335         miniroute = ipv6_route ( sin6_dest->sin6_scope_id, &dest );
336         if ( ! miniroute )
337                 return NULL;
338
339         return miniroute->netdev;
340 }
341
342 /**
343  * Check that received options can be safely ignored
344  *
345  * @v iphdr             IPv6 header
346  * @v options           Options extension header
347  * @v len               Maximum length of header
348  * @ret rc              Return status code
349  */
350 static int ipv6_check_options ( struct ipv6_header *iphdr,
351                                 struct ipv6_options_header *options,
352                                 size_t len ) {
353         struct ipv6_option *option = options->options;
354         struct ipv6_option *end = ( ( ( void * ) options ) + len );
355
356         while ( option < end ) {
357                 if ( ! IPV6_CAN_IGNORE_OPT ( option->type ) ) {
358                         DBGC ( ipv6col ( &iphdr->src ), "IPv6 unrecognised "
359                                "option type %#02x:\n", option->type );
360                         DBGC_HDA ( ipv6col ( &iphdr->src ), 0,
361                                    options, len );
362                         return -ENOTSUP_OPT;
363                 }
364                 if ( option->type == IPV6_OPT_PAD1 ) {
365                         option = ( ( ( void * ) option ) + 1 );
366                 } else {
367                         option = ( ( ( void * ) option->value ) + option->len );
368                 }
369         }
370         return 0;
371 }
372
373 /**
374  * Check if fragment matches fragment reassembly buffer
375  *
376  * @v fragment          Fragment reassembly buffer
377  * @v iobuf             I/O buffer
378  * @v hdrlen            Length of non-fragmentable potion of I/O buffer
379  * @ret is_fragment     Fragment matches this reassembly buffer
380  */
381 static int ipv6_is_fragment ( struct fragment *fragment,
382                               struct io_buffer *iobuf, size_t hdrlen ) {
383         struct ipv6_header *frag_iphdr = fragment->iobuf->data;
384         struct ipv6_fragment_header *frag_fhdr =
385                 ( fragment->iobuf->data + fragment->hdrlen -
386                   sizeof ( *frag_fhdr ) );
387         struct ipv6_header *iphdr = iobuf->data;
388         struct ipv6_fragment_header *fhdr =
389                 ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
390
391         return ( ( memcmp ( &iphdr->src, &frag_iphdr->src,
392                             sizeof ( iphdr->src ) ) == 0 ) &&
393                  ( fhdr->ident == frag_fhdr->ident ) );
394 }
395
396 /**
397  * Get fragment offset
398  *
399  * @v iobuf             I/O buffer
400  * @v hdrlen            Length of non-fragmentable potion of I/O buffer
401  * @ret offset          Offset
402  */
403 static size_t ipv6_fragment_offset ( struct io_buffer *iobuf, size_t hdrlen ) {
404         struct ipv6_fragment_header *fhdr =
405                 ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
406
407         return ( ntohs ( fhdr->offset_more ) & IPV6_MASK_OFFSET );
408 }
409
410 /**
411  * Check if more fragments exist
412  *
413  * @v iobuf             I/O buffer
414  * @v hdrlen            Length of non-fragmentable potion of I/O buffer
415  * @ret more_frags      More fragments exist
416  */
417 static int ipv6_more_fragments ( struct io_buffer *iobuf, size_t hdrlen ) {
418         struct ipv6_fragment_header *fhdr =
419                 ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
420
421         return ( fhdr->offset_more & htons ( IPV6_MASK_MOREFRAGS ) );
422 }
423
424 /** Fragment reassembler */
425 static struct fragment_reassembler ipv6_reassembler = {
426         .list = LIST_HEAD_INIT ( ipv6_reassembler.list ),
427         .is_fragment = ipv6_is_fragment,
428         .fragment_offset = ipv6_fragment_offset,
429         .more_fragments = ipv6_more_fragments,
430         .stats = &ipv6_stats,
431 };
432
433 /**
434  * Calculate IPv6 pseudo-header checksum
435  *
436  * @v iphdr             IPv6 header
437  * @v len               Payload length
438  * @v next_header       Next header type
439  * @v csum              Existing checksum
440  * @ret csum            Updated checksum
441  */
442 static uint16_t ipv6_pshdr_chksum ( struct ipv6_header *iphdr, size_t len,
443                                     int next_header, uint16_t csum ) {
444         struct ipv6_pseudo_header pshdr;
445
446         /* Build pseudo-header */
447         memcpy ( &pshdr.src, &iphdr->src, sizeof ( pshdr.src ) );
448         memcpy ( &pshdr.dest, &iphdr->dest, sizeof ( pshdr.dest ) );
449         pshdr.len = htonl ( len );
450         memset ( pshdr.zero, 0, sizeof ( pshdr.zero ) );
451         pshdr.next_header = next_header;
452
453         /* Update the checksum value */
454         return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
455 }
456
457 /**
458  * Transmit IPv6 packet
459  *
460  * @v iobuf             I/O buffer
461  * @v tcpip             Transport-layer protocol
462  * @v st_src            Source network-layer address
463  * @v st_dest           Destination network-layer address
464  * @v netdev            Network device to use if no route found, or NULL
465  * @v trans_csum        Transport-layer checksum to complete, or NULL
466  * @ret rc              Status
467  *
468  * This function expects a transport-layer segment and prepends the
469  * IPv6 header
470  */
471 static int ipv6_tx ( struct io_buffer *iobuf,
472                      struct tcpip_protocol *tcpip_protocol,
473                      struct sockaddr_tcpip *st_src,
474                      struct sockaddr_tcpip *st_dest,
475                      struct net_device *netdev,
476                      uint16_t *trans_csum ) {
477         struct sockaddr_in6 *sin6_src = ( ( struct sockaddr_in6 * ) st_src );
478         struct sockaddr_in6 *sin6_dest = ( ( struct sockaddr_in6 * ) st_dest );
479         struct ipv6_miniroute *miniroute;
480         struct ipv6_header *iphdr;
481         struct in6_addr *src = NULL;
482         struct in6_addr *next_hop;
483         uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
484         const void *ll_dest;
485         size_t len;
486         int rc;
487
488         /* Update statistics */
489         ipv6_stats.out_requests++;
490
491         /* Fill up the IPv6 header, except source address */
492         len = iob_len ( iobuf );
493         iphdr = iob_push ( iobuf, sizeof ( *iphdr ) );
494         memset ( iphdr, 0, sizeof ( *iphdr ) );
495         iphdr->ver_tc_label = htonl ( IPV6_VER );
496         iphdr->len = htons ( len );
497         iphdr->next_header = tcpip_protocol->tcpip_proto;
498         iphdr->hop_limit = IPV6_HOP_LIMIT;
499         memcpy ( &iphdr->dest, &sin6_dest->sin6_addr, sizeof ( iphdr->dest ) );
500
501         /* Use routing table to identify next hop and transmitting netdev */
502         next_hop = &iphdr->dest;
503         if ( ( miniroute = ipv6_route ( sin6_dest->sin6_scope_id,
504                                         &next_hop ) ) != NULL ) {
505                 src = &miniroute->address;
506                 netdev = miniroute->netdev;
507         }
508         if ( ! netdev ) {
509                 DBGC ( ipv6col ( &iphdr->dest ), "IPv6 has no route to %s\n",
510                        inet6_ntoa ( &iphdr->dest ) );
511                 ipv6_stats.out_no_routes++;
512                 rc = -ENETUNREACH;
513                 goto err;
514         }
515         if ( sin6_src && ! IN6_IS_ADDR_UNSPECIFIED ( &sin6_src->sin6_addr ) )
516                 src = &sin6_src->sin6_addr;
517         if ( src )
518                 memcpy ( &iphdr->src, src, sizeof ( iphdr->src ) );
519
520         /* Fix up checksums */
521         if ( trans_csum ) {
522                 *trans_csum = ipv6_pshdr_chksum ( iphdr, len,
523                                                   tcpip_protocol->tcpip_proto,
524                                                   *trans_csum );
525         }
526
527         /* Print IPv6 header for debugging */
528         DBGC2 ( ipv6col ( &iphdr->dest ), "IPv6 TX %s->",
529                 inet6_ntoa ( &iphdr->src ) );
530         DBGC2 ( ipv6col ( &iphdr->dest ), "%s len %zd next %d\n",
531                 inet6_ntoa ( &iphdr->dest ), len, iphdr->next_header );
532
533         /* Calculate link-layer destination address, if possible */
534         if ( IN6_IS_ADDR_MULTICAST ( next_hop ) ) {
535                 /* Multicast address */
536                 ipv6_stats.out_mcast_pkts++;
537                 if ( ( rc = netdev->ll_protocol->mc_hash ( AF_INET6, next_hop,
538                                                            ll_dest_buf ) ) !=0){
539                         DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not hash "
540                                "multicast %s: %s\n", inet6_ntoa ( next_hop ),
541                                strerror ( rc ) );
542                         goto err;
543                 }
544                 ll_dest = ll_dest_buf;
545         } else {
546                 /* Unicast address */
547                 ll_dest = NULL;
548         }
549
550         /* Update statistics */
551         ipv6_stats.out_transmits++;
552         ipv6_stats.out_octets += iob_len ( iobuf );
553
554         /* Hand off to link layer (via NDP if applicable) */
555         if ( ll_dest ) {
556                 if ( ( rc = net_tx ( iobuf, netdev, &ipv6_protocol, ll_dest,
557                                      netdev->ll_addr ) ) != 0 ) {
558                         DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not "
559                                "transmit packet via %s: %s\n",
560                                netdev->name, strerror ( rc ) );
561                         return rc;
562                 }
563         } else {
564                 if ( ( rc = ndp_tx ( iobuf, netdev, next_hop, &iphdr->src,
565                                      netdev->ll_addr ) ) != 0 ) {
566                         DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not "
567                                "transmit packet via %s: %s\n",
568                                netdev->name, strerror ( rc ) );
569                         return rc;
570                 }
571         }
572
573         return 0;
574
575  err:
576         free_iob ( iobuf );
577         return rc;
578 }
579
580 /**
581  * Process incoming IPv6 packets
582  *
583  * @v iobuf             I/O buffer
584  * @v netdev            Network device
585  * @v ll_dest           Link-layer destination address
586  * @v ll_source         Link-layer destination source
587  * @v flags             Packet flags
588  * @ret rc              Return status code
589  *
590  * This function expects an IPv6 network datagram. It processes the
591  * headers and sends it to the transport layer.
592  */
593 static int ipv6_rx ( struct io_buffer *iobuf, struct net_device *netdev,
594                      const void *ll_dest __unused,
595                      const void *ll_source __unused,
596                      unsigned int flags __unused ) {
597         struct ipv6_header *iphdr = iobuf->data;
598         union ipv6_extension_header *ext;
599         union {
600                 struct sockaddr_in6 sin6;
601                 struct sockaddr_tcpip st;
602         } src, dest;
603         uint16_t pshdr_csum;
604         size_t len;
605         size_t hdrlen;
606         size_t extlen;
607         int this_header;
608         int next_header;
609         int rc;
610
611         /* Update statistics */
612         ipv6_stats.in_receives++;
613         ipv6_stats.in_octets += iob_len ( iobuf );
614         if ( flags & LL_BROADCAST ) {
615                 ipv6_stats.in_bcast_pkts++;
616         } else if ( flags & LL_MULTICAST ) {
617                 ipv6_stats.in_mcast_pkts++;
618         }
619
620         /* Sanity check the IPv6 header */
621         if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
622                 DBGC ( ipv6col ( &iphdr->src ), "IPv6 packet too short at %zd "
623                        "bytes (min %zd bytes)\n", iob_len ( iobuf ),
624                        sizeof ( *iphdr ) );
625                 rc = -EINVAL_LEN;
626                 goto err_header;
627         }
628         if ( ( iphdr->ver_tc_label & htonl ( IPV6_MASK_VER ) ) !=
629              htonl ( IPV6_VER ) ) {
630                 DBGC ( ipv6col ( &iphdr->src ), "IPv6 version %#08x not "
631                        "supported\n", ntohl ( iphdr->ver_tc_label ) );
632                 rc = -ENOTSUP_VER;
633                 goto err_header;
634         }
635
636         /* Truncate packet to specified length */
637         len = ntohs ( iphdr->len );
638         if ( len > iob_len ( iobuf ) ) {
639                 DBGC ( ipv6col ( &iphdr->src ), "IPv6 length too long at %zd "
640                        "bytes (packet is %zd bytes)\n", len, iob_len ( iobuf ));
641                 ipv6_stats.in_truncated_pkts++;
642                 rc = -EINVAL_LEN;
643                 goto err_other;
644         }
645         iob_unput ( iobuf, ( iob_len ( iobuf ) - len - sizeof ( *iphdr ) ) );
646         hdrlen = sizeof ( *iphdr );
647
648         /* Print IPv6 header for debugging */
649         DBGC2 ( ipv6col ( &iphdr->src ), "IPv6 RX %s<-",
650                 inet6_ntoa ( &iphdr->dest ) );
651         DBGC2 ( ipv6col ( &iphdr->src ), "%s len %zd next %d\n",
652                 inet6_ntoa ( &iphdr->src ), len, iphdr->next_header );
653
654         /* Discard unicast packets not destined for us */
655         if ( ( ! ( flags & LL_MULTICAST ) ) &&
656              ( ! ipv6_has_addr ( netdev, &iphdr->dest ) ) ) {
657                 DBGC ( ipv6col ( &iphdr->src ), "IPv6 discarding non-local "
658                        "unicast packet for %s\n", inet6_ntoa ( &iphdr->dest ) );
659                 ipv6_stats.in_addr_errors++;
660                 rc = -EPIPE;
661                 goto err_other;
662         }
663
664         /* Process any extension headers */
665         next_header = iphdr->next_header;
666         while ( 1 ) {
667
668                 /* Extract extension header */
669                 this_header = next_header;
670                 ext = ( iobuf->data + hdrlen );
671                 extlen = sizeof ( ext->pad );
672                 if ( iob_len ( iobuf ) < ( hdrlen + extlen ) ) {
673                         DBGC ( ipv6col ( &iphdr->src ), "IPv6 too short for "
674                                "extension header type %d at %zd bytes (min "
675                                "%zd bytes)\n", this_header,
676                                ( iob_len ( iobuf ) - hdrlen ), extlen );
677                         rc = -EINVAL_LEN;
678                         goto err_header;
679                 }
680
681                 /* Determine size of extension header (if applicable) */
682                 if ( ( this_header == IPV6_HOPBYHOP ) ||
683                      ( this_header == IPV6_DESTINATION ) ||
684                      ( this_header == IPV6_ROUTING ) ) {
685                         /* Length field is present */
686                         extlen += ext->common.len;
687                 } else if ( this_header == IPV6_FRAGMENT ) {
688                         /* Length field is reserved and ignored (RFC2460) */
689                 } else {
690                         /* Not an extension header; assume rest is payload */
691                         break;
692                 }
693                 if ( iob_len ( iobuf ) < ( hdrlen + extlen ) ) {
694                         DBGC ( ipv6col ( &iphdr->src ), "IPv6 too short for "
695                                "extension header type %d at %zd bytes (min "
696                                "%zd bytes)\n", this_header,
697                                ( iob_len ( iobuf ) - hdrlen ), extlen );
698                         rc = -EINVAL_LEN;
699                         goto err_header;
700                 }
701                 hdrlen += extlen;
702                 next_header = ext->common.next_header;
703                 DBGC2 ( ipv6col ( &iphdr->src ), "IPv6 RX %s<-",
704                         inet6_ntoa ( &iphdr->dest ) );
705                 DBGC2 ( ipv6col ( &iphdr->src ), "%s ext type %d len %zd next "
706                         "%d\n", inet6_ntoa ( &iphdr->src ), this_header,
707                         extlen, next_header );
708
709                 /* Process this extension header */
710                 if ( ( this_header == IPV6_HOPBYHOP ) ||
711                      ( this_header == IPV6_DESTINATION ) ) {
712
713                         /* Check that all options can be ignored */
714                         if ( ( rc = ipv6_check_options ( iphdr, &ext->options,
715                                                          extlen ) ) != 0 )
716                                 goto err_header;
717
718                 } else if ( this_header == IPV6_FRAGMENT ) {
719
720                         /* Reassemble fragments */
721                         iobuf = fragment_reassemble ( &ipv6_reassembler, iobuf,
722                                                       &hdrlen );
723                         if ( ! iobuf )
724                                 return 0;
725                         iphdr = iobuf->data;
726                 }
727         }
728
729         /* Construct socket address, calculate pseudo-header checksum,
730          * and hand off to transport layer
731          */
732         memset ( &src, 0, sizeof ( src ) );
733         src.sin6.sin6_family = AF_INET6;
734         memcpy ( &src.sin6.sin6_addr, &iphdr->src,
735                  sizeof ( src.sin6.sin6_addr ) );
736         src.sin6.sin6_scope_id = netdev->index;
737         memset ( &dest, 0, sizeof ( dest ) );
738         dest.sin6.sin6_family = AF_INET6;
739         memcpy ( &dest.sin6.sin6_addr, &iphdr->dest,
740                  sizeof ( dest.sin6.sin6_addr ) );
741         dest.sin6.sin6_scope_id = netdev->index;
742         iob_pull ( iobuf, hdrlen );
743         pshdr_csum = ipv6_pshdr_chksum ( iphdr, iob_len ( iobuf ),
744                                          next_header, TCPIP_EMPTY_CSUM );
745         if ( ( rc = tcpip_rx ( iobuf, netdev, next_header, &src.st, &dest.st,
746                                pshdr_csum, &ipv6_stats ) ) != 0 ) {
747                 DBGC ( ipv6col ( &src.sin6.sin6_addr ), "IPv6 received packet "
748                                 "rejected by stack: %s\n", strerror ( rc ) );
749                 return rc;
750         }
751
752         return 0;
753
754  err_header:
755         ipv6_stats.in_hdr_errors++;
756  err_other:
757         free_iob ( iobuf );
758         return rc;
759 }
760
761 /**
762  * Parse IPv6 address
763  *
764  * @v string            IPv6 address string
765  * @ret in              IPv6 address to fill in
766  * @ret rc              Return status code
767  */
768 int inet6_aton ( const char *string, struct in6_addr *in ) {
769         uint16_t *word = in->s6_addr16;
770         uint16_t *end = ( word + ( sizeof ( in->s6_addr16 ) /
771                                    sizeof ( in->s6_addr16[0] ) ) );
772         uint16_t *pad = NULL;
773         const char *nptr = string;
774         char *endptr;
775         unsigned long value;
776         size_t pad_len;
777         size_t move_len;
778
779         /* Parse string */
780         while ( 1 ) {
781
782                 /* Parse current word */
783                 value = strtoul ( nptr, &endptr, 16 );
784                 if ( value > 0xffff ) {
785                         DBG ( "IPv6 invalid word value %#lx in \"%s\"\n",
786                               value, string );
787                         return -EINVAL;
788                 }
789                 *(word++) = htons ( value );
790
791                 /* Parse separator */
792                 if ( ! *endptr )
793                         break;
794                 if ( *endptr != ':' ) {
795                         DBG ( "IPv6 invalid separator '%c' in \"%s\"\n",
796                               *endptr, string );
797                         return -EINVAL;
798                 }
799                 if ( ( endptr == nptr ) && ( nptr != string ) ) {
800                         if ( pad ) {
801                                 DBG ( "IPv6 invalid multiple \"::\" in "
802                                       "\"%s\"\n", string );
803                                 return -EINVAL;
804                         }
805                         pad = word;
806                 }
807                 nptr = ( endptr + 1 );
808
809                 /* Check for overrun */
810                 if ( word == end ) {
811                         DBG ( "IPv6 too many words in \"%s\"\n", string );
812                         return -EINVAL;
813                 }
814         }
815
816         /* Insert padding if specified */
817         if ( pad ) {
818                 move_len = ( ( ( void * ) word ) - ( ( void * ) pad ) );
819                 pad_len = ( ( ( void * ) end ) - ( ( void * ) word ) );
820                 memmove ( ( ( ( void * ) pad ) + pad_len ), pad, move_len );
821                 memset ( pad, 0, pad_len );
822         } else if ( word != end ) {
823                 DBG ( "IPv6 underlength address \"%s\"\n", string );
824                 return -EINVAL;
825         }
826
827         return 0;
828 }
829
830 /**
831  * Convert IPv6 address to standard notation
832  *
833  * @v in                IPv6 address
834  * @ret string          IPv6 address string in canonical format
835  *
836  * RFC5952 defines the canonical format for IPv6 textual representation.
837  */
838 char * inet6_ntoa ( const struct in6_addr *in ) {
839         static char buf[41]; /* ":xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */
840         char *out = buf;
841         char *longest_start = NULL;
842         char *start = NULL;
843         int longest_len = 1;
844         int len = 0;
845         char *dest;
846         unsigned int i;
847         uint16_t value;
848
849         /* Format address, keeping track of longest run of zeros */
850         for ( i = 0 ; i < ( sizeof ( in->s6_addr16 ) /
851                             sizeof ( in->s6_addr16[0] ) ) ; i++ ) {
852                 value = ntohs ( in->s6_addr16[i] );
853                 if ( value == 0 ) {
854                         if ( len++ == 0 )
855                                 start = out;
856                         if ( len > longest_len ) {
857                                 longest_start = start;
858                                 longest_len = len;
859                         }
860                 } else {
861                         len = 0;
862                 }
863                 out += sprintf ( out, ":%x", value );
864         }
865
866         /* Abbreviate longest run of zeros, if applicable */
867         if ( longest_start ) {
868                 dest = strcpy ( ( longest_start + 1 ),
869                                 ( longest_start + ( 2 * longest_len ) ) );
870                 if ( dest[0] == '\0' )
871                         dest[1] = '\0';
872                 dest[0] = ':';
873         }
874         return ( ( longest_start == buf ) ? buf : ( buf + 1 ) );
875 }
876
877 /**
878  * Transcribe IPv6 address
879  *
880  * @v net_addr          IPv6 address
881  * @ret string          IPv6 address in standard notation
882  *
883  */
884 static const char * ipv6_ntoa ( const void *net_addr ) {
885         return inet6_ntoa ( net_addr );
886 }
887
888 /**
889  * Transcribe IPv6 socket address
890  *
891  * @v sa                Socket address
892  * @ret string          Socket address in standard notation
893  */
894 static const char * ipv6_sock_ntoa ( struct sockaddr *sa ) {
895         static char buf[ 39 /* "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */ +
896                          1 /* "%" */ + NETDEV_NAME_LEN + 1 /* NUL */ ];
897         struct sockaddr_in6 *sin6 = ( ( struct sockaddr_in6 * ) sa );
898         struct in6_addr *in = &sin6->sin6_addr;
899         struct net_device *netdev;
900         const char *netdev_name;
901
902         /* Identify network device, if applicable */
903         if ( IN6_IS_ADDR_NONGLOBAL ( in ) ) {
904                 netdev = find_netdev_by_index ( sin6->sin6_scope_id );
905                 netdev_name = ( netdev ? netdev->name : "UNKNOWN" );
906         } else {
907                 netdev_name = NULL;
908         }
909
910         /* Format socket address */
911         snprintf ( buf, sizeof ( buf ), "%s%s%s", inet6_ntoa ( in ),
912                    ( netdev_name ? "%" : "" ),
913                    ( netdev_name ? netdev_name : "" ) );
914         return buf;
915 }
916
917 /**
918  * Parse IPv6 socket address
919  *
920  * @v string            Socket address string
921  * @v sa                Socket address to fill in
922  * @ret rc              Return status code
923  */
924 static int ipv6_sock_aton ( const char *string, struct sockaddr *sa ) {
925         struct sockaddr_in6 *sin6 = ( ( struct sockaddr_in6 * ) sa );
926         struct in6_addr in;
927         struct net_device *netdev;
928         size_t len;
929         char *tmp;
930         char *in_string;
931         char *netdev_string;
932         int rc;
933
934         /* Create modifiable copy of string */
935         tmp = strdup ( string );
936         if ( ! tmp ) {
937                 rc = -ENOMEM;
938                 goto err_alloc;
939         }
940         in_string = tmp;
941
942         /* Strip surrounding "[...]", if present */
943         len = strlen ( in_string );
944         if ( ( in_string[0] == '[' ) && ( in_string[ len - 1 ] == ']' ) ) {
945                 in_string[ len - 1 ] = '\0';
946                 in_string++;
947         }
948
949         /* Split at network device name, if present */
950         netdev_string = strchr ( in_string, '%' );
951         if ( netdev_string )
952                 *(netdev_string++) = '\0';
953
954         /* Parse IPv6 address portion */
955         if ( ( rc = inet6_aton ( in_string, &in ) ) != 0 )
956                 goto err_inet6_aton;
957
958         /* Parse scope ID, if applicable */
959         if ( netdev_string ) {
960
961                 /* Parse explicit network device name, if present */
962                 netdev = find_netdev ( netdev_string );
963                 if ( ! netdev ) {
964                         rc = -ENODEV;
965                         goto err_find_netdev;
966                 }
967                 sin6->sin6_scope_id = netdev->index;
968
969         } else if ( IN6_IS_ADDR_NONGLOBAL ( &in ) ) {
970
971                 /* If no network device is explicitly specified for a
972                  * link-local or multicast address, default to using
973                  * "netX" (if existent).
974                  */
975                 netdev = last_opened_netdev();
976                 if ( netdev )
977                         sin6->sin6_scope_id = netdev->index;
978         }
979
980         /* Copy IPv6 address portion to socket address */
981         memcpy ( &sin6->sin6_addr, &in, sizeof ( sin6->sin6_addr ) );
982
983  err_find_netdev:
984  err_inet6_aton:
985         free ( tmp );
986  err_alloc:
987         return rc;
988 }
989
990 /** IPv6 protocol */
991 struct net_protocol ipv6_protocol __net_protocol = {
992         .name = "IPv6",
993         .net_proto = htons ( ETH_P_IPV6 ),
994         .net_addr_len = sizeof ( struct in6_addr ),
995         .rx = ipv6_rx,
996         .ntoa = ipv6_ntoa,
997 };
998
999 /** IPv6 TCPIP net protocol */
1000 struct tcpip_net_protocol ipv6_tcpip_protocol __tcpip_net_protocol = {
1001         .name = "IPv6",
1002         .sa_family = AF_INET6,
1003         .header_len = sizeof ( struct ipv6_header ),
1004         .tx = ipv6_tx,
1005         .netdev = ipv6_netdev,
1006 };
1007
1008 /** IPv6 socket address converter */
1009 struct sockaddr_converter ipv6_sockaddr_converter __sockaddr_converter = {
1010         .family = AF_INET6,
1011         .ntoa = ipv6_sock_ntoa,
1012         .aton = ipv6_sock_aton,
1013 };
1014
1015 /**
1016  * Parse IPv6 address setting value
1017  *
1018  * @v type              Setting type
1019  * @v value             Formatted setting value
1020  * @v buf               Buffer to contain raw value
1021  * @v len               Length of buffer
1022  * @ret len             Length of raw value, or negative error
1023  */
1024 int parse_ipv6_setting ( const struct setting_type *type __unused,
1025                          const char *value, void *buf, size_t len ) {
1026         struct in6_addr ipv6;
1027         int rc;
1028
1029         /* Parse IPv6 address */
1030         if ( ( rc = inet6_aton ( value, &ipv6 ) ) != 0 )
1031                 return rc;
1032
1033         /* Copy to buffer */
1034         if ( len > sizeof ( ipv6 ) )
1035                 len = sizeof ( ipv6 );
1036         memcpy ( buf, &ipv6, len );
1037
1038         return ( sizeof ( ipv6 ) );
1039 }
1040
1041 /**
1042  * Format IPv6 address setting value
1043  *
1044  * @v type              Setting type
1045  * @v raw               Raw setting value
1046  * @v raw_len           Length of raw setting value
1047  * @v buf               Buffer to contain formatted value
1048  * @v len               Length of buffer
1049  * @ret len             Length of formatted value, or negative error
1050  */
1051 int format_ipv6_setting ( const struct setting_type *type __unused,
1052                           const void *raw, size_t raw_len, char *buf,
1053                           size_t len ) {
1054         const struct in6_addr *ipv6 = raw;
1055
1056         if ( raw_len < sizeof ( *ipv6 ) )
1057                 return -EINVAL;
1058         return snprintf ( buf, len, "%s", inet6_ntoa ( ipv6 ) );
1059 }
1060
1061 /**
1062  * Create IPv6 network device
1063  *
1064  * @v netdev            Network device
1065  * @ret rc              Return status code
1066  */
1067 static int ipv6_probe ( struct net_device *netdev ) {
1068         struct ipv6_miniroute *miniroute;
1069         struct in6_addr address;
1070         int prefix_len;
1071         int rc;
1072
1073         /* Construct link-local address from EUI-64 as per RFC 2464 */
1074         memset ( &address, 0, sizeof ( address ) );
1075         prefix_len = ipv6_link_local ( &address, netdev );
1076         if ( prefix_len < 0 ) {
1077                 rc = prefix_len;
1078                 DBGC ( netdev, "IPv6 %s could not construct link-local "
1079                        "address: %s\n", netdev->name, strerror ( rc ) );
1080                 return rc;
1081         }
1082
1083         /* Create link-local address for this network device */
1084         miniroute = ipv6_add_miniroute ( netdev, &address, prefix_len,
1085                                          IPV6_HAS_ADDRESS );
1086         if ( ! miniroute )
1087                 return -ENOMEM;
1088
1089         return 0;
1090 }
1091
1092 /**
1093  * Destroy IPv6 network device
1094  *
1095  * @v netdev            Network device
1096  */
1097 static void ipv6_remove ( struct net_device *netdev ) {
1098         struct ipv6_miniroute *miniroute;
1099         struct ipv6_miniroute *tmp;
1100
1101         /* Delete all miniroutes for this network device */
1102         list_for_each_entry_safe ( miniroute, tmp, &ipv6_miniroutes, list ) {
1103                 if ( miniroute->netdev == netdev ) {
1104                         netdev_put ( miniroute->netdev );
1105                         list_del ( &miniroute->list );
1106                         free ( miniroute );
1107                 }
1108         }
1109 }
1110
1111 /** IPv6 network device driver */
1112 struct net_driver ipv6_driver __net_driver = {
1113         .name = "IPv6",
1114         .probe = ipv6_probe,
1115         .remove = ipv6_remove,
1116 };
1117
1118 /* Drag in objects via ipv6_protocol */
1119 REQUIRING_SYMBOL ( ipv6_protocol );
1120
1121 /* Drag in ICMPv6 */
1122 REQUIRE_OBJECT ( icmpv6 );
1123
1124 /* Drag in NDP */
1125 REQUIRE_OBJECT ( ndp );