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 <strings.h>
27 #include <errno.h>
28 #include <assert.h>
29 #include <byteswap.h>
30 #include <ipxe/iobuf.h>
31 #include <ipxe/tcpip.h>
32 #include <ipxe/if_ether.h>
33 #include <ipxe/crc32.h>
34 #include <ipxe/fragment.h>
35 #include <ipxe/ipstat.h>
36 #include <ipxe/ndp.h>
37 #include <ipxe/ipv6.h>
38
39 /** @file
40 *
41 * IPv6 protocol
42 *
43 */
44
45 /* Disambiguate the various error causes */
46 #define EINVAL_LEN __einfo_error ( EINFO_EINVAL_LEN )
47 #define EINFO_EINVAL_LEN \
48 __einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid length" )
49 #define ENOTSUP_VER __einfo_error ( EINFO_ENOTSUP_VER )
50 #define EINFO_ENOTSUP_VER \
51 __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported version" )
52 #define ENOTSUP_HDR __einfo_error ( EINFO_ENOTSUP_HDR )
53 #define EINFO_ENOTSUP_HDR \
54 __einfo_uniqify ( EINFO_ENOTSUP, 0x02, "Unsupported header type" )
55 #define ENOTSUP_OPT __einfo_error ( EINFO_ENOTSUP_OPT )
56 #define EINFO_ENOTSUP_OPT \
57 __einfo_uniqify ( EINFO_ENOTSUP, 0x03, "Unsupported option" )
58
59 /** List of IPv6 miniroutes */
60 struct list_head ipv6_miniroutes = LIST_HEAD_INIT ( ipv6_miniroutes );
61
62 /** IPv6 statistics */
63 static struct ip_statistics ipv6_stats;
64
65 /** IPv6 statistics family */
66 struct ip_statistics_family
67 ipv6_statistics_family __ip_statistics_family ( IP_STATISTICS_IPV6 ) = {
68 .version = 6,
69 .stats = &ipv6_stats,
70 };
71
72 /**
73 * Determine debugging colour for IPv6 debug messages
74 *
75 * @v in IPv6 address
76 * @ret col Debugging colour (for DBGC())
77 */
ipv6col(struct in6_addr * in)78 static uint32_t ipv6col ( struct in6_addr *in ) {
79 return crc32_le ( 0, in, sizeof ( *in ) );
80 }
81
82 /**
83 * Determine IPv6 address scope
84 *
85 * @v addr IPv6 address
86 * @ret scope Address scope
87 */
ipv6_scope(const struct in6_addr * addr)88 static unsigned int ipv6_scope ( const struct in6_addr *addr ) {
89
90 /* Multicast addresses directly include a scope field */
91 if ( IN6_IS_ADDR_MULTICAST ( addr ) )
92 return ipv6_multicast_scope ( addr );
93
94 /* Link-local addresses have link-local scope */
95 if ( IN6_IS_ADDR_LINKLOCAL ( addr ) )
96 return IPV6_SCOPE_LINK_LOCAL;
97
98 /* Site-local addresses have site-local scope */
99 if ( IN6_IS_ADDR_SITELOCAL ( addr ) )
100 return IPV6_SCOPE_SITE_LOCAL;
101
102 /* Unique local addresses do not directly map to a defined
103 * scope. They effectively have a scope which is wider than
104 * link-local but narrower than global. Since the only
105 * multicast packets that we transmit are link-local, we can
106 * simply choose an arbitrary scope between link-local and
107 * global.
108 */
109 if ( IN6_IS_ADDR_ULA ( addr ) )
110 return IPV6_SCOPE_ORGANISATION_LOCAL;
111
112 /* All other addresses are assumed to be global */
113 return IPV6_SCOPE_GLOBAL;
114 }
115
116 /**
117 * Dump IPv6 routing table entry
118 *
119 * @v miniroute Routing table entry
120 */
121 static inline __attribute__ (( always_inline )) void
ipv6_dump_miniroute(struct ipv6_miniroute * miniroute)122 ipv6_dump_miniroute ( struct ipv6_miniroute *miniroute ) {
123 struct net_device *netdev = miniroute->netdev;
124
125 DBGC ( netdev, "IPv6 %s has %s %s/%d", netdev->name,
126 ( ( miniroute->flags & IPV6_HAS_ADDRESS ) ?
127 "address" : "prefix" ),
128 inet6_ntoa ( &miniroute->address ), miniroute->prefix_len );
129 if ( miniroute->flags & IPV6_HAS_ROUTER )
130 DBGC ( netdev, " router %s", inet6_ntoa ( &miniroute->router ));
131 DBGC ( netdev, "\n" );
132 }
133
134 /**
135 * Check if network device has a specific IPv6 address
136 *
137 * @v netdev Network device
138 * @v addr IPv6 address
139 * @ret has_addr Network device has this IPv6 address
140 */
ipv6_has_addr(struct net_device * netdev,struct in6_addr * addr)141 int ipv6_has_addr ( struct net_device *netdev, struct in6_addr *addr ) {
142 struct ipv6_miniroute *miniroute;
143
144 list_for_each_entry ( miniroute, &ipv6_miniroutes, list ) {
145 if ( ( miniroute->netdev == netdev ) &&
146 ( miniroute->flags & IPV6_HAS_ADDRESS ) &&
147 ( memcmp ( &miniroute->address, addr,
148 sizeof ( miniroute->address ) ) == 0 ) ) {
149 /* Found matching address */
150 return 1;
151 }
152 }
153 return 0;
154 }
155
156 /**
157 * Count matching bits of an IPv6 routing table entry prefix
158 *
159 * @v miniroute Routing table entry
160 * @v address IPv6 address
161 * @ret match_len Number of matching prefix bits
162 */
ipv6_match_len(struct ipv6_miniroute * miniroute,struct in6_addr * address)163 static unsigned int ipv6_match_len ( struct ipv6_miniroute *miniroute,
164 struct in6_addr *address ) {
165 unsigned int match_len = 0;
166 unsigned int i;
167 uint32_t diff;
168
169 for ( i = 0 ; i < ( sizeof ( address->s6_addr32 ) /
170 sizeof ( address->s6_addr32[0] ) ) ; i++ ) {
171
172 diff = ntohl ( ~( ( ~( address->s6_addr32[i] ^
173 miniroute->address.s6_addr32[i] ) )
174 & miniroute->prefix_mask.s6_addr32[i] ) );
175 match_len += 32;
176 if ( diff ) {
177 match_len -= flsl ( diff );
178 break;
179 }
180 }
181
182 return match_len;
183 }
184
185 /**
186 * Find IPv6 routing table entry for a given address
187 *
188 * @v netdev Network device
189 * @v address IPv6 address
190 * @ret miniroute Routing table entry, or NULL if not found
191 */
ipv6_miniroute(struct net_device * netdev,struct in6_addr * address)192 static struct ipv6_miniroute * ipv6_miniroute ( struct net_device *netdev,
193 struct in6_addr *address ) {
194 struct ipv6_miniroute *miniroute;
195 unsigned int match_len;
196
197 list_for_each_entry ( miniroute, &ipv6_miniroutes, list ) {
198 if ( miniroute->netdev != netdev )
199 continue;
200 match_len = ipv6_match_len ( miniroute, address );
201 if ( match_len < miniroute->prefix_len )
202 continue;
203 return miniroute;
204 }
205 return NULL;
206 }
207
208 /**
209 * Add IPv6 routing table entry
210 *
211 * @v netdev Network device
212 * @v address IPv6 address (or prefix)
213 * @v prefix_len Prefix length
214 * @v router Router address (if any)
215 * @ret rc Return status code
216 */
ipv6_add_miniroute(struct net_device * netdev,struct in6_addr * address,unsigned int prefix_len,struct in6_addr * router)217 int ipv6_add_miniroute ( struct net_device *netdev, struct in6_addr *address,
218 unsigned int prefix_len, struct in6_addr *router ) {
219 struct ipv6_miniroute *miniroute;
220 uint8_t *prefix_mask;
221 unsigned int remaining;
222 unsigned int i;
223
224 /* Find or create routing table entry */
225 miniroute = ipv6_miniroute ( netdev, address );
226 if ( miniroute ) {
227
228 /* Remove from existing position in routing table */
229 list_del ( &miniroute->list );
230
231 } else {
232
233 /* Create new routing table entry */
234 miniroute = zalloc ( sizeof ( *miniroute ) );
235 if ( ! miniroute )
236 return -ENOMEM;
237 miniroute->netdev = netdev_get ( netdev );
238 memcpy ( &miniroute->address, address,
239 sizeof ( miniroute->address ) );
240
241 /* Default to prefix length of 64 if none specified */
242 if ( ! prefix_len )
243 prefix_len = IPV6_DEFAULT_PREFIX_LEN;
244 miniroute->prefix_len = prefix_len;
245 assert ( prefix_len <= IPV6_MAX_PREFIX_LEN );
246
247 /* Construct prefix mask */
248 remaining = prefix_len;
249 for ( prefix_mask = miniroute->prefix_mask.s6_addr ;
250 remaining >= 8 ; prefix_mask++, remaining -= 8 ) {
251 *prefix_mask = 0xff;
252 }
253 if ( remaining )
254 *prefix_mask <<= ( 8 - remaining );
255 }
256
257 /* Add to start of routing table */
258 list_add ( &miniroute->list, &ipv6_miniroutes );
259
260 /* Set or update address, if applicable */
261 for ( i = 0 ; i < ( sizeof ( address->s6_addr32 ) /
262 sizeof ( address->s6_addr32[0] ) ) ; i++ ) {
263 if ( ( address->s6_addr32[i] &
264 ~miniroute->prefix_mask.s6_addr32[i] ) != 0 ) {
265 memcpy ( &miniroute->address, address,
266 sizeof ( miniroute->address ) );
267 miniroute->flags |= IPV6_HAS_ADDRESS;
268 }
269 }
270 if ( miniroute->prefix_len == IPV6_MAX_PREFIX_LEN )
271 miniroute->flags |= IPV6_HAS_ADDRESS;
272
273 /* Update scope */
274 miniroute->scope = ipv6_scope ( &miniroute->address );
275
276 /* Set or update router, if applicable */
277 if ( router ) {
278 memcpy ( &miniroute->router, router,
279 sizeof ( miniroute->router ) );
280 miniroute->flags |= IPV6_HAS_ROUTER;
281 }
282
283 ipv6_dump_miniroute ( miniroute );
284 return 0;
285 }
286
287 /**
288 * Delete IPv6 minirouting table entry
289 *
290 * @v miniroute Routing table entry
291 */
ipv6_del_miniroute(struct ipv6_miniroute * miniroute)292 void ipv6_del_miniroute ( struct ipv6_miniroute *miniroute ) {
293
294 netdev_put ( miniroute->netdev );
295 list_del ( &miniroute->list );
296 free ( miniroute );
297 }
298
299 /**
300 * Perform IPv6 routing
301 *
302 * @v scope_id Destination address scope ID (for link-local addresses)
303 * @v dest Final destination address
304 * @ret dest Next hop destination address
305 * @ret miniroute Routing table entry to use, or NULL if no route
306 */
ipv6_route(unsigned int scope_id,struct in6_addr ** dest)307 struct ipv6_miniroute * ipv6_route ( unsigned int scope_id,
308 struct in6_addr **dest ) {
309 struct ipv6_miniroute *miniroute;
310 struct ipv6_miniroute *chosen = NULL;
311 unsigned int best = 0;
312 unsigned int match_len;
313 unsigned int score;
314 unsigned int scope;
315
316 /* Calculate destination address scope */
317 scope = ipv6_scope ( *dest );
318
319 /* Find first usable route in routing table */
320 list_for_each_entry ( miniroute, &ipv6_miniroutes, list ) {
321
322 /* Skip closed network devices */
323 if ( ! netdev_is_open ( miniroute->netdev ) )
324 continue;
325
326 /* Skip entries with no usable source address */
327 if ( ! ( miniroute->flags & IPV6_HAS_ADDRESS ) )
328 continue;
329
330 /* Skip entries with a non-matching scope ID, if
331 * destination specifies a scope ID.
332 */
333 if ( scope_id && ( miniroute->netdev->index != scope_id ) )
334 continue;
335
336 /* Skip entries that are out of scope */
337 if ( miniroute->scope < scope )
338 continue;
339
340 /* Calculate match length */
341 match_len = ipv6_match_len ( miniroute, *dest );
342
343 /* If destination is on-link, then use this route */
344 if ( match_len >= miniroute->prefix_len )
345 return miniroute;
346
347 /* If destination is unicast, then skip off-link
348 * entries with no router.
349 */
350 if ( ! ( IN6_IS_ADDR_MULTICAST ( *dest ) ||
351 ( miniroute->flags & IPV6_HAS_ROUTER ) ) )
352 continue;
353
354 /* Choose best route, defined as being the route with
355 * the smallest viable scope. If two routes both have
356 * the same scope, then prefer the route with the
357 * longest match length.
358 */
359 score = ( ( ( IPV6_SCOPE_MAX + 1 - miniroute->scope ) << 8 )
360 + match_len );
361 if ( score > best ) {
362 chosen = miniroute;
363 best = score;
364 }
365 }
366
367 /* Return chosen route, if any */
368 if ( chosen ) {
369 if ( ! IN6_IS_ADDR_MULTICAST ( *dest ) )
370 *dest = &chosen->router;
371 return chosen;
372 }
373
374 return NULL;
375 }
376
377 /**
378 * Determine transmitting network device
379 *
380 * @v st_dest Destination network-layer address
381 * @ret netdev Transmitting network device, or NULL
382 */
ipv6_netdev(struct sockaddr_tcpip * st_dest)383 static struct net_device * ipv6_netdev ( struct sockaddr_tcpip *st_dest ) {
384 struct sockaddr_in6 *sin6_dest = ( ( struct sockaddr_in6 * ) st_dest );
385 struct in6_addr *dest = &sin6_dest->sin6_addr;
386 struct ipv6_miniroute *miniroute;
387
388 /* Find routing table entry */
389 miniroute = ipv6_route ( sin6_dest->sin6_scope_id, &dest );
390 if ( ! miniroute )
391 return NULL;
392
393 return miniroute->netdev;
394 }
395
396 /**
397 * Check that received options can be safely ignored
398 *
399 * @v iphdr IPv6 header
400 * @v options Options extension header
401 * @v len Maximum length of header
402 * @ret rc Return status code
403 */
ipv6_check_options(struct ipv6_header * iphdr,struct ipv6_options_header * options,size_t len)404 static int ipv6_check_options ( struct ipv6_header *iphdr,
405 struct ipv6_options_header *options,
406 size_t len ) {
407 struct ipv6_option *option = options->options;
408 struct ipv6_option *end = ( ( ( void * ) options ) + len );
409
410 while ( option < end ) {
411 if ( ! IPV6_CAN_IGNORE_OPT ( option->type ) ) {
412 DBGC ( ipv6col ( &iphdr->src ), "IPv6 unrecognised "
413 "option type %#02x:\n", option->type );
414 DBGC_HDA ( ipv6col ( &iphdr->src ), 0,
415 options, len );
416 return -ENOTSUP_OPT;
417 }
418 if ( option->type == IPV6_OPT_PAD1 ) {
419 option = ( ( ( void * ) option ) + 1 );
420 } else {
421 option = ( ( ( void * ) option->value ) + option->len );
422 }
423 }
424 return 0;
425 }
426
427 /**
428 * Check if fragment matches fragment reassembly buffer
429 *
430 * @v fragment Fragment reassembly buffer
431 * @v iobuf I/O buffer
432 * @v hdrlen Length of non-fragmentable potion of I/O buffer
433 * @ret is_fragment Fragment matches this reassembly buffer
434 */
ipv6_is_fragment(struct fragment * fragment,struct io_buffer * iobuf,size_t hdrlen)435 static int ipv6_is_fragment ( struct fragment *fragment,
436 struct io_buffer *iobuf, size_t hdrlen ) {
437 struct ipv6_header *frag_iphdr = fragment->iobuf->data;
438 struct ipv6_fragment_header *frag_fhdr =
439 ( fragment->iobuf->data + fragment->hdrlen -
440 sizeof ( *frag_fhdr ) );
441 struct ipv6_header *iphdr = iobuf->data;
442 struct ipv6_fragment_header *fhdr =
443 ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
444
445 return ( ( memcmp ( &iphdr->src, &frag_iphdr->src,
446 sizeof ( iphdr->src ) ) == 0 ) &&
447 ( fhdr->ident == frag_fhdr->ident ) );
448 }
449
450 /**
451 * Get fragment offset
452 *
453 * @v iobuf I/O buffer
454 * @v hdrlen Length of non-fragmentable potion of I/O buffer
455 * @ret offset Offset
456 */
ipv6_fragment_offset(struct io_buffer * iobuf,size_t hdrlen)457 static size_t ipv6_fragment_offset ( struct io_buffer *iobuf, size_t hdrlen ) {
458 struct ipv6_fragment_header *fhdr =
459 ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
460
461 return ( ntohs ( fhdr->offset_more ) & IPV6_MASK_OFFSET );
462 }
463
464 /**
465 * Check if more fragments exist
466 *
467 * @v iobuf I/O buffer
468 * @v hdrlen Length of non-fragmentable potion of I/O buffer
469 * @ret more_frags More fragments exist
470 */
ipv6_more_fragments(struct io_buffer * iobuf,size_t hdrlen)471 static int ipv6_more_fragments ( struct io_buffer *iobuf, size_t hdrlen ) {
472 struct ipv6_fragment_header *fhdr =
473 ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
474
475 return ( fhdr->offset_more & htons ( IPV6_MASK_MOREFRAGS ) );
476 }
477
478 /** Fragment reassembler */
479 static struct fragment_reassembler ipv6_reassembler = {
480 .list = LIST_HEAD_INIT ( ipv6_reassembler.list ),
481 .is_fragment = ipv6_is_fragment,
482 .fragment_offset = ipv6_fragment_offset,
483 .more_fragments = ipv6_more_fragments,
484 .stats = &ipv6_stats,
485 };
486
487 /**
488 * Calculate IPv6 pseudo-header checksum
489 *
490 * @v iphdr IPv6 header
491 * @v len Payload length
492 * @v next_header Next header type
493 * @v csum Existing checksum
494 * @ret csum Updated checksum
495 */
ipv6_pshdr_chksum(struct ipv6_header * iphdr,size_t len,int next_header,uint16_t csum)496 static uint16_t ipv6_pshdr_chksum ( struct ipv6_header *iphdr, size_t len,
497 int next_header, uint16_t csum ) {
498 struct ipv6_pseudo_header pshdr;
499
500 /* Build pseudo-header */
501 memcpy ( &pshdr.src, &iphdr->src, sizeof ( pshdr.src ) );
502 memcpy ( &pshdr.dest, &iphdr->dest, sizeof ( pshdr.dest ) );
503 pshdr.len = htonl ( len );
504 memset ( pshdr.zero, 0, sizeof ( pshdr.zero ) );
505 pshdr.next_header = next_header;
506
507 /* Update the checksum value */
508 return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
509 }
510
511 /**
512 * Transmit IPv6 packet
513 *
514 * @v iobuf I/O buffer
515 * @v tcpip Transport-layer protocol
516 * @v st_src Source network-layer address
517 * @v st_dest Destination network-layer address
518 * @v netdev Network device to use if no route found, or NULL
519 * @v trans_csum Transport-layer checksum to complete, or NULL
520 * @ret rc Status
521 *
522 * This function expects a transport-layer segment and prepends the
523 * IPv6 header
524 */
ipv6_tx(struct io_buffer * iobuf,struct tcpip_protocol * tcpip_protocol,struct sockaddr_tcpip * st_src,struct sockaddr_tcpip * st_dest,struct net_device * netdev,uint16_t * trans_csum)525 static int ipv6_tx ( struct io_buffer *iobuf,
526 struct tcpip_protocol *tcpip_protocol,
527 struct sockaddr_tcpip *st_src,
528 struct sockaddr_tcpip *st_dest,
529 struct net_device *netdev,
530 uint16_t *trans_csum ) {
531 struct sockaddr_in6 *sin6_src = ( ( struct sockaddr_in6 * ) st_src );
532 struct sockaddr_in6 *sin6_dest = ( ( struct sockaddr_in6 * ) st_dest );
533 struct ipv6_miniroute *miniroute;
534 struct ipv6_header *iphdr;
535 struct in6_addr *src = NULL;
536 struct in6_addr *next_hop;
537 uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
538 const void *ll_dest;
539 size_t len;
540 int rc;
541
542 /* Update statistics */
543 ipv6_stats.out_requests++;
544
545 /* Fill up the IPv6 header, except source address */
546 len = iob_len ( iobuf );
547 iphdr = iob_push ( iobuf, sizeof ( *iphdr ) );
548 memset ( iphdr, 0, sizeof ( *iphdr ) );
549 iphdr->ver_tc_label = htonl ( IPV6_VER );
550 iphdr->len = htons ( len );
551 iphdr->next_header = tcpip_protocol->tcpip_proto;
552 iphdr->hop_limit = IPV6_HOP_LIMIT;
553 memcpy ( &iphdr->dest, &sin6_dest->sin6_addr, sizeof ( iphdr->dest ) );
554
555 /* Use routing table to identify next hop and transmitting netdev */
556 next_hop = &iphdr->dest;
557 if ( ( miniroute = ipv6_route ( sin6_dest->sin6_scope_id,
558 &next_hop ) ) != NULL ) {
559 src = &miniroute->address;
560 netdev = miniroute->netdev;
561 }
562 if ( ! netdev ) {
563 DBGC ( ipv6col ( &iphdr->dest ), "IPv6 has no route to %s\n",
564 inet6_ntoa ( &iphdr->dest ) );
565 ipv6_stats.out_no_routes++;
566 rc = -ENETUNREACH;
567 goto err;
568 }
569 if ( sin6_src && ! IN6_IS_ADDR_UNSPECIFIED ( &sin6_src->sin6_addr ) )
570 src = &sin6_src->sin6_addr;
571 if ( src )
572 memcpy ( &iphdr->src, src, sizeof ( iphdr->src ) );
573
574 /* Fix up checksums */
575 if ( trans_csum ) {
576 *trans_csum = ipv6_pshdr_chksum ( iphdr, len,
577 tcpip_protocol->tcpip_proto,
578 *trans_csum );
579 if ( ! *trans_csum )
580 *trans_csum = tcpip_protocol->zero_csum;
581 }
582
583 /* Print IPv6 header for debugging */
584 DBGC2 ( ipv6col ( &iphdr->dest ), "IPv6 TX %s->",
585 inet6_ntoa ( &iphdr->src ) );
586 DBGC2 ( ipv6col ( &iphdr->dest ), "%s len %zd next %d\n",
587 inet6_ntoa ( &iphdr->dest ), len, iphdr->next_header );
588
589 /* Calculate link-layer destination address, if possible */
590 if ( IN6_IS_ADDR_MULTICAST ( next_hop ) ) {
591 /* Multicast address */
592 ipv6_stats.out_mcast_pkts++;
593 if ( ( rc = netdev->ll_protocol->mc_hash ( AF_INET6, next_hop,
594 ll_dest_buf ) ) !=0){
595 DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not hash "
596 "multicast %s: %s\n", inet6_ntoa ( next_hop ),
597 strerror ( rc ) );
598 goto err;
599 }
600 ll_dest = ll_dest_buf;
601 } else {
602 /* Unicast address */
603 ll_dest = NULL;
604 }
605
606 /* Update statistics */
607 ipv6_stats.out_transmits++;
608 ipv6_stats.out_octets += iob_len ( iobuf );
609
610 /* Hand off to link layer (via NDP if applicable) */
611 if ( ll_dest ) {
612 if ( ( rc = net_tx ( iobuf, netdev, &ipv6_protocol, ll_dest,
613 netdev->ll_addr ) ) != 0 ) {
614 DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not "
615 "transmit packet via %s: %s\n",
616 netdev->name, strerror ( rc ) );
617 return rc;
618 }
619 } else {
620 if ( ( rc = ndp_tx ( iobuf, netdev, next_hop, &iphdr->src,
621 netdev->ll_addr ) ) != 0 ) {
622 DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not "
623 "transmit packet via %s: %s\n",
624 netdev->name, strerror ( rc ) );
625 return rc;
626 }
627 }
628
629 return 0;
630
631 err:
632 free_iob ( iobuf );
633 return rc;
634 }
635
636 /**
637 * Process incoming IPv6 packets
638 *
639 * @v iobuf I/O buffer
640 * @v netdev Network device
641 * @v ll_dest Link-layer destination address
642 * @v ll_source Link-layer destination source
643 * @v flags Packet flags
644 * @ret rc Return status code
645 *
646 * This function expects an IPv6 network datagram. It processes the
647 * headers and sends it to the transport layer.
648 */
ipv6_rx(struct io_buffer * iobuf,struct net_device * netdev,const void * ll_dest __unused,const void * ll_source __unused,unsigned int flags __unused)649 static int ipv6_rx ( struct io_buffer *iobuf, struct net_device *netdev,
650 const void *ll_dest __unused,
651 const void *ll_source __unused,
652 unsigned int flags __unused ) {
653 struct ipv6_header *iphdr = iobuf->data;
654 union ipv6_extension_header *ext;
655 union {
656 struct sockaddr_in6 sin6;
657 struct sockaddr_tcpip st;
658 } src, dest;
659 uint16_t pshdr_csum;
660 size_t len;
661 size_t hdrlen;
662 size_t extlen;
663 int this_header;
664 int next_header;
665 int rc;
666
667 /* Update statistics */
668 ipv6_stats.in_receives++;
669 ipv6_stats.in_octets += iob_len ( iobuf );
670 if ( flags & LL_BROADCAST ) {
671 ipv6_stats.in_bcast_pkts++;
672 } else if ( flags & LL_MULTICAST ) {
673 ipv6_stats.in_mcast_pkts++;
674 }
675
676 /* Sanity check the IPv6 header */
677 if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
678 DBGC ( ipv6col ( &iphdr->src ), "IPv6 packet too short at %zd "
679 "bytes (min %zd bytes)\n", iob_len ( iobuf ),
680 sizeof ( *iphdr ) );
681 rc = -EINVAL_LEN;
682 goto err_header;
683 }
684 if ( ( iphdr->ver_tc_label & htonl ( IPV6_MASK_VER ) ) !=
685 htonl ( IPV6_VER ) ) {
686 DBGC ( ipv6col ( &iphdr->src ), "IPv6 version %#08x not "
687 "supported\n", ntohl ( iphdr->ver_tc_label ) );
688 rc = -ENOTSUP_VER;
689 goto err_header;
690 }
691
692 /* Truncate packet to specified length */
693 len = ntohs ( iphdr->len );
694 if ( len > iob_len ( iobuf ) ) {
695 DBGC ( ipv6col ( &iphdr->src ), "IPv6 length too long at %zd "
696 "bytes (packet is %zd bytes)\n", len, iob_len ( iobuf ));
697 ipv6_stats.in_truncated_pkts++;
698 rc = -EINVAL_LEN;
699 goto err_other;
700 }
701 iob_unput ( iobuf, ( iob_len ( iobuf ) - len - sizeof ( *iphdr ) ) );
702 hdrlen = sizeof ( *iphdr );
703
704 /* Print IPv6 header for debugging */
705 DBGC2 ( ipv6col ( &iphdr->src ), "IPv6 RX %s<-",
706 inet6_ntoa ( &iphdr->dest ) );
707 DBGC2 ( ipv6col ( &iphdr->src ), "%s len %zd next %d\n",
708 inet6_ntoa ( &iphdr->src ), len, iphdr->next_header );
709
710 /* Discard unicast packets not destined for us */
711 if ( ( ! ( flags & LL_MULTICAST ) ) &&
712 ( ! ipv6_has_addr ( netdev, &iphdr->dest ) ) ) {
713 DBGC ( ipv6col ( &iphdr->src ), "IPv6 discarding non-local "
714 "unicast packet for %s\n", inet6_ntoa ( &iphdr->dest ) );
715 ipv6_stats.in_addr_errors++;
716 rc = -EPIPE;
717 goto err_other;
718 }
719
720 /* Process any extension headers */
721 next_header = iphdr->next_header;
722 while ( 1 ) {
723
724 /* Extract extension header */
725 this_header = next_header;
726 ext = ( iobuf->data + hdrlen );
727 extlen = sizeof ( ext->pad );
728 if ( iob_len ( iobuf ) < ( hdrlen + extlen ) ) {
729 DBGC ( ipv6col ( &iphdr->src ), "IPv6 too short for "
730 "extension header type %d at %zd bytes (min "
731 "%zd bytes)\n", this_header,
732 ( iob_len ( iobuf ) - hdrlen ), extlen );
733 rc = -EINVAL_LEN;
734 goto err_header;
735 }
736
737 /* Determine size of extension header (if applicable) */
738 if ( ( this_header == IPV6_HOPBYHOP ) ||
739 ( this_header == IPV6_DESTINATION ) ||
740 ( this_header == IPV6_ROUTING ) ) {
741 /* Length field is present */
742 extlen += ext->common.len;
743 } else if ( this_header == IPV6_FRAGMENT ) {
744 /* Length field is reserved and ignored (RFC2460) */
745 } else {
746 /* Not an extension header; assume rest is payload */
747 break;
748 }
749 if ( iob_len ( iobuf ) < ( hdrlen + extlen ) ) {
750 DBGC ( ipv6col ( &iphdr->src ), "IPv6 too short for "
751 "extension header type %d at %zd bytes (min "
752 "%zd bytes)\n", this_header,
753 ( iob_len ( iobuf ) - hdrlen ), extlen );
754 rc = -EINVAL_LEN;
755 goto err_header;
756 }
757 hdrlen += extlen;
758 next_header = ext->common.next_header;
759 DBGC2 ( ipv6col ( &iphdr->src ), "IPv6 RX %s<-",
760 inet6_ntoa ( &iphdr->dest ) );
761 DBGC2 ( ipv6col ( &iphdr->src ), "%s ext type %d len %zd next "
762 "%d\n", inet6_ntoa ( &iphdr->src ), this_header,
763 extlen, next_header );
764
765 /* Process this extension header */
766 if ( ( this_header == IPV6_HOPBYHOP ) ||
767 ( this_header == IPV6_DESTINATION ) ) {
768
769 /* Check that all options can be ignored */
770 if ( ( rc = ipv6_check_options ( iphdr, &ext->options,
771 extlen ) ) != 0 )
772 goto err_header;
773
774 } else if ( this_header == IPV6_FRAGMENT ) {
775
776 /* Reassemble fragments */
777 iobuf = fragment_reassemble ( &ipv6_reassembler, iobuf,
778 &hdrlen );
779 if ( ! iobuf )
780 return 0;
781 iphdr = iobuf->data;
782 }
783 }
784
785 /* Construct socket address, calculate pseudo-header checksum,
786 * and hand off to transport layer
787 */
788 memset ( &src, 0, sizeof ( src ) );
789 src.sin6.sin6_family = AF_INET6;
790 memcpy ( &src.sin6.sin6_addr, &iphdr->src,
791 sizeof ( src.sin6.sin6_addr ) );
792 src.sin6.sin6_scope_id = netdev->index;
793 memset ( &dest, 0, sizeof ( dest ) );
794 dest.sin6.sin6_family = AF_INET6;
795 memcpy ( &dest.sin6.sin6_addr, &iphdr->dest,
796 sizeof ( dest.sin6.sin6_addr ) );
797 dest.sin6.sin6_scope_id = netdev->index;
798 iob_pull ( iobuf, hdrlen );
799 pshdr_csum = ipv6_pshdr_chksum ( iphdr, iob_len ( iobuf ),
800 next_header, TCPIP_EMPTY_CSUM );
801 if ( ( rc = tcpip_rx ( iobuf, netdev, next_header, &src.st, &dest.st,
802 pshdr_csum, &ipv6_stats ) ) != 0 ) {
803 DBGC ( ipv6col ( &src.sin6.sin6_addr ), "IPv6 received packet "
804 "rejected by stack: %s\n", strerror ( rc ) );
805 return rc;
806 }
807
808 return 0;
809
810 err_header:
811 ipv6_stats.in_hdr_errors++;
812 err_other:
813 free_iob ( iobuf );
814 return rc;
815 }
816
817 /**
818 * Parse IPv6 address
819 *
820 * @v string IPv6 address string
821 * @ret in IPv6 address to fill in
822 * @ret rc Return status code
823 */
inet6_aton(const char * string,struct in6_addr * in)824 int inet6_aton ( const char *string, struct in6_addr *in ) {
825 uint16_t *word = in->s6_addr16;
826 uint16_t *end = ( word + ( sizeof ( in->s6_addr16 ) /
827 sizeof ( in->s6_addr16[0] ) ) );
828 uint16_t *pad = NULL;
829 const char *nptr = string;
830 char *endptr;
831 unsigned long value;
832 size_t pad_len;
833 size_t move_len;
834
835 /* Parse string */
836 while ( 1 ) {
837
838 /* Parse current word */
839 value = strtoul ( nptr, &endptr, 16 );
840 if ( value > 0xffff ) {
841 DBG ( "IPv6 invalid word value %#lx in \"%s\"\n",
842 value, string );
843 return -EINVAL;
844 }
845 *(word++) = htons ( value );
846
847 /* Parse separator */
848 if ( ! *endptr )
849 break;
850 if ( *endptr != ':' ) {
851 DBG ( "IPv6 invalid separator '%c' in \"%s\"\n",
852 *endptr, string );
853 return -EINVAL;
854 }
855 if ( ( endptr == nptr ) && ( nptr != string ) ) {
856 if ( pad ) {
857 DBG ( "IPv6 invalid multiple \"::\" in "
858 "\"%s\"\n", string );
859 return -EINVAL;
860 }
861 pad = word;
862 }
863 nptr = ( endptr + 1 );
864
865 /* Check for overrun */
866 if ( word == end ) {
867 DBG ( "IPv6 too many words in \"%s\"\n", string );
868 return -EINVAL;
869 }
870 }
871
872 /* Insert padding if specified */
873 if ( pad ) {
874 move_len = ( ( ( void * ) word ) - ( ( void * ) pad ) );
875 pad_len = ( ( ( void * ) end ) - ( ( void * ) word ) );
876 memmove ( ( ( ( void * ) pad ) + pad_len ), pad, move_len );
877 memset ( pad, 0, pad_len );
878 } else if ( word != end ) {
879 DBG ( "IPv6 underlength address \"%s\"\n", string );
880 return -EINVAL;
881 }
882
883 return 0;
884 }
885
886 /**
887 * Convert IPv6 address to standard notation
888 *
889 * @v in IPv6 address
890 * @ret string IPv6 address string in canonical format
891 *
892 * RFC5952 defines the canonical format for IPv6 textual representation.
893 */
inet6_ntoa(const struct in6_addr * in)894 char * inet6_ntoa ( const struct in6_addr *in ) {
895 static char buf[41]; /* ":xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */
896 char *out = buf;
897 char *longest_start = NULL;
898 char *start = NULL;
899 int longest_len = 1;
900 int len = 0;
901 char *dest;
902 unsigned int i;
903 uint16_t value;
904
905 /* Format address, keeping track of longest run of zeros */
906 for ( i = 0 ; i < ( sizeof ( in->s6_addr16 ) /
907 sizeof ( in->s6_addr16[0] ) ) ; i++ ) {
908 value = ntohs ( in->s6_addr16[i] );
909 if ( value == 0 ) {
910 if ( len++ == 0 )
911 start = out;
912 if ( len > longest_len ) {
913 longest_start = start;
914 longest_len = len;
915 }
916 } else {
917 len = 0;
918 }
919 out += sprintf ( out, ":%x", value );
920 }
921
922 /* Abbreviate longest run of zeros, if applicable */
923 if ( longest_start ) {
924 dest = strcpy ( ( longest_start + 1 ),
925 ( longest_start + ( 2 * longest_len ) ) );
926 if ( dest[0] == '\0' )
927 dest[1] = '\0';
928 dest[0] = ':';
929 }
930 return ( ( longest_start == buf ) ? buf : ( buf + 1 ) );
931 }
932
933 /**
934 * Transcribe IPv6 address
935 *
936 * @v net_addr IPv6 address
937 * @ret string IPv6 address in standard notation
938 *
939 */
ipv6_ntoa(const void * net_addr)940 static const char * ipv6_ntoa ( const void *net_addr ) {
941 return inet6_ntoa ( net_addr );
942 }
943
944 /**
945 * Transcribe IPv6 socket address
946 *
947 * @v sa Socket address
948 * @ret string Socket address in standard notation
949 */
ipv6_sock_ntoa(struct sockaddr * sa)950 static const char * ipv6_sock_ntoa ( struct sockaddr *sa ) {
951 static char buf[ 39 /* "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */ +
952 1 /* "%" */ + NETDEV_NAME_LEN + 1 /* NUL */ ];
953 struct sockaddr_in6 *sin6 = ( ( struct sockaddr_in6 * ) sa );
954 struct in6_addr *in = &sin6->sin6_addr;
955 struct net_device *netdev;
956 const char *netdev_name;
957
958 /* Identify network device, if applicable */
959 if ( IN6_IS_ADDR_LINKLOCAL ( in ) || IN6_IS_ADDR_MULTICAST ( in ) ) {
960 netdev = find_netdev_by_index ( sin6->sin6_scope_id );
961 netdev_name = ( netdev ? netdev->name : "UNKNOWN" );
962 } else {
963 netdev_name = NULL;
964 }
965
966 /* Format socket address */
967 snprintf ( buf, sizeof ( buf ), "%s%s%s", inet6_ntoa ( in ),
968 ( netdev_name ? "%" : "" ),
969 ( netdev_name ? netdev_name : "" ) );
970 return buf;
971 }
972
973 /**
974 * Parse IPv6 socket address
975 *
976 * @v string Socket address string
977 * @v sa Socket address to fill in
978 * @ret rc Return status code
979 */
ipv6_sock_aton(const char * string,struct sockaddr * sa)980 static int ipv6_sock_aton ( const char *string, struct sockaddr *sa ) {
981 struct sockaddr_in6 *sin6 = ( ( struct sockaddr_in6 * ) sa );
982 struct in6_addr in;
983 struct net_device *netdev;
984 size_t len;
985 char *tmp;
986 char *in_string;
987 char *netdev_string;
988 int rc;
989
990 /* Create modifiable copy of string */
991 tmp = strdup ( string );
992 if ( ! tmp ) {
993 rc = -ENOMEM;
994 goto err_alloc;
995 }
996 in_string = tmp;
997
998 /* Strip surrounding "[...]", if present */
999 len = strlen ( in_string );
1000 if ( ( in_string[0] == '[' ) && ( in_string[ len - 1 ] == ']' ) ) {
1001 in_string[ len - 1 ] = '\0';
1002 in_string++;
1003 }
1004
1005 /* Split at network device name, if present */
1006 netdev_string = strchr ( in_string, '%' );
1007 if ( netdev_string )
1008 *(netdev_string++) = '\0';
1009
1010 /* Parse IPv6 address portion */
1011 if ( ( rc = inet6_aton ( in_string, &in ) ) != 0 )
1012 goto err_inet6_aton;
1013
1014 /* Parse scope ID, if applicable */
1015 if ( netdev_string ) {
1016
1017 /* Parse explicit network device name, if present */
1018 netdev = find_netdev ( netdev_string );
1019 if ( ! netdev ) {
1020 rc = -ENODEV;
1021 goto err_find_netdev;
1022 }
1023 sin6->sin6_scope_id = netdev->index;
1024
1025 } else if ( IN6_IS_ADDR_LINKLOCAL ( &in ) ||
1026 IN6_IS_ADDR_MULTICAST ( &in ) ) {
1027
1028 /* If no network device is explicitly specified for a
1029 * link-local or multicast address, default to using
1030 * "netX" (if existent).
1031 */
1032 netdev = last_opened_netdev();
1033 if ( netdev )
1034 sin6->sin6_scope_id = netdev->index;
1035 }
1036
1037 /* Copy IPv6 address portion to socket address */
1038 memcpy ( &sin6->sin6_addr, &in, sizeof ( sin6->sin6_addr ) );
1039
1040 err_find_netdev:
1041 err_inet6_aton:
1042 free ( tmp );
1043 err_alloc:
1044 return rc;
1045 }
1046
1047 /** IPv6 protocol */
1048 struct net_protocol ipv6_protocol __net_protocol = {
1049 .name = "IPv6",
1050 .net_proto = htons ( ETH_P_IPV6 ),
1051 .net_addr_len = sizeof ( struct in6_addr ),
1052 .rx = ipv6_rx,
1053 .ntoa = ipv6_ntoa,
1054 };
1055
1056 /** IPv6 TCPIP net protocol */
1057 struct tcpip_net_protocol ipv6_tcpip_protocol __tcpip_net_protocol = {
1058 .name = "IPv6",
1059 .sa_family = AF_INET6,
1060 .header_len = sizeof ( struct ipv6_header ),
1061 .net_protocol = &ipv6_protocol,
1062 .tx = ipv6_tx,
1063 .netdev = ipv6_netdev,
1064 };
1065
1066 /** IPv6 socket address converter */
1067 struct sockaddr_converter ipv6_sockaddr_converter __sockaddr_converter = {
1068 .family = AF_INET6,
1069 .ntoa = ipv6_sock_ntoa,
1070 .aton = ipv6_sock_aton,
1071 };
1072
1073 /**
1074 * Parse IPv6 address setting value
1075 *
1076 * @v type Setting type
1077 * @v value Formatted setting value
1078 * @v buf Buffer to contain raw value
1079 * @v len Length of buffer
1080 * @ret len Length of raw value, or negative error
1081 */
parse_ipv6_setting(const struct setting_type * type __unused,const char * value,void * buf,size_t len)1082 int parse_ipv6_setting ( const struct setting_type *type __unused,
1083 const char *value, void *buf, size_t len ) {
1084 struct in6_addr ipv6;
1085 int rc;
1086
1087 /* Parse IPv6 address */
1088 if ( ( rc = inet6_aton ( value, &ipv6 ) ) != 0 )
1089 return rc;
1090
1091 /* Copy to buffer */
1092 if ( len > sizeof ( ipv6 ) )
1093 len = sizeof ( ipv6 );
1094 memcpy ( buf, &ipv6, len );
1095
1096 return ( sizeof ( ipv6 ) );
1097 }
1098
1099 /**
1100 * Format IPv6 address setting value
1101 *
1102 * @v type Setting type
1103 * @v raw Raw setting value
1104 * @v raw_len Length of raw setting value
1105 * @v buf Buffer to contain formatted value
1106 * @v len Length of buffer
1107 * @ret len Length of formatted value, or negative error
1108 */
format_ipv6_setting(const struct setting_type * type __unused,const void * raw,size_t raw_len,char * buf,size_t len)1109 int format_ipv6_setting ( const struct setting_type *type __unused,
1110 const void *raw, size_t raw_len, char *buf,
1111 size_t len ) {
1112 const struct in6_addr *ipv6 = raw;
1113
1114 if ( raw_len < sizeof ( *ipv6 ) )
1115 return -EINVAL;
1116 return snprintf ( buf, len, "%s", inet6_ntoa ( ipv6 ) );
1117 }
1118
1119 /** IPv6 settings scope */
1120 const struct settings_scope ipv6_settings_scope;
1121
1122 /** IPv6 address setting */
1123 const struct setting ip6_setting __setting ( SETTING_IP6, ip6 ) = {
1124 .name = "ip6",
1125 .description = "IPv6 address",
1126 .type = &setting_type_ipv6,
1127 .scope = &ipv6_settings_scope,
1128 };
1129
1130 /** IPv6 prefix length setting */
1131 const struct setting len6_setting __setting ( SETTING_IP6, len6 ) = {
1132 .name = "len6",
1133 .description = "IPv6 prefix length",
1134 .type = &setting_type_int8,
1135 .scope = &ipv6_settings_scope,
1136 };
1137
1138 /** Default gateway setting */
1139 const struct setting gateway6_setting __setting ( SETTING_IP6, gateway6 ) = {
1140 .name = "gateway6",
1141 .description = "IPv6 gateway",
1142 .type = &setting_type_ipv6,
1143 .scope = &ipv6_settings_scope,
1144 };
1145
1146 /**
1147 * Check applicability of IPv6 link-local address setting
1148 *
1149 * @v settings Settings block
1150 * @v setting Setting to fetch
1151 * @ret applies Setting applies within this settings block
1152 */
ipv6_applies(struct settings * settings __unused,const struct setting * setting)1153 static int ipv6_applies ( struct settings *settings __unused,
1154 const struct setting *setting ) {
1155
1156 return ( setting->scope == &ipv6_settings_scope );
1157 }
1158
1159 /**
1160 * Fetch IPv6 link-local address setting
1161 *
1162 * @v settings Settings block
1163 * @v setting Setting to fetch
1164 * @v data Buffer to fill with setting data
1165 * @v len Length of buffer
1166 * @ret len Length of setting data, or negative error
1167 */
ipv6_fetch(struct settings * settings,struct setting * setting,void * data,size_t len)1168 static int ipv6_fetch ( struct settings *settings, struct setting *setting,
1169 void *data, size_t len ) {
1170 struct net_device *netdev =
1171 container_of ( settings->parent, struct net_device,
1172 settings.settings );
1173 struct in6_addr ip6;
1174 uint8_t *len6;
1175 int prefix_len;
1176 int rc;
1177
1178 /* Construct link-local address from EUI-64 as per RFC 2464 */
1179 memset ( &ip6, 0, sizeof ( ip6 ) );
1180 prefix_len = ipv6_link_local ( &ip6, netdev );
1181 if ( prefix_len < 0 ) {
1182 rc = prefix_len;
1183 return rc;
1184 }
1185
1186 /* Handle setting */
1187 if ( setting_cmp ( setting, &ip6_setting ) == 0 ) {
1188
1189 /* Return link-local ip6 */
1190 if ( len > sizeof ( ip6 ) )
1191 len = sizeof ( ip6 );
1192 memcpy ( data, &ip6, len );
1193 return sizeof ( ip6 );
1194
1195 } else if ( setting_cmp ( setting, &len6_setting ) == 0 ) {
1196
1197 /* Return prefix length */
1198 if ( len ) {
1199 len6 = data;
1200 *len6 = prefix_len;
1201 }
1202 return sizeof ( *len6 );
1203
1204 }
1205
1206 return -ENOENT;
1207 }
1208
1209 /** IPv6 link-local address settings operations */
1210 static struct settings_operations ipv6_settings_operations = {
1211 .applies = ipv6_applies,
1212 .fetch = ipv6_fetch,
1213 };
1214
1215 /** IPv6 link-local address settings */
1216 struct ipv6_settings {
1217 /** Reference counter */
1218 struct refcnt refcnt;
1219 /** Settings interface */
1220 struct settings settings;
1221 };
1222
1223 /**
1224 * Register IPv6 link-local address settings
1225 *
1226 * @v netdev Network device
1227 * @ret rc Return status code
1228 */
ipv6_register_settings(struct net_device * netdev)1229 static int ipv6_register_settings ( struct net_device *netdev ) {
1230 struct settings *parent = netdev_settings ( netdev );
1231 struct ipv6_settings *ipv6set;
1232 int rc;
1233
1234 /* Allocate and initialise structure */
1235 ipv6set = zalloc ( sizeof ( *ipv6set ) );
1236 if ( ! ipv6set ) {
1237 rc = -ENOMEM;
1238 goto err_alloc;
1239 }
1240 ref_init ( &ipv6set->refcnt, NULL );
1241 settings_init ( &ipv6set->settings, &ipv6_settings_operations,
1242 &ipv6set->refcnt, &ipv6_settings_scope );
1243 ipv6set->settings.order = IPV6_ORDER_LINK_LOCAL;
1244
1245 /* Register settings */
1246 if ( ( rc = register_settings ( &ipv6set->settings, parent,
1247 IPV6_SETTINGS_NAME ) ) != 0 )
1248 goto err_register;
1249
1250 err_register:
1251 ref_put ( &ipv6set->refcnt );
1252 err_alloc:
1253 return rc;
1254 }
1255
1256 /** IPv6 network device driver */
1257 struct net_driver ipv6_driver __net_driver = {
1258 .name = "IPv6",
1259 .probe = ipv6_register_settings,
1260 };
1261
1262 /**
1263 * Create IPv6 routing table based on configured settings
1264 *
1265 * @v netdev Network device
1266 * @v settings Settings block
1267 * @ret rc Return status code
1268 */
ipv6_create_routes(struct net_device * netdev,struct settings * settings)1269 static int ipv6_create_routes ( struct net_device *netdev,
1270 struct settings *settings ) {
1271 struct settings *child;
1272 struct settings *origin;
1273 struct in6_addr ip6_buf;
1274 struct in6_addr gateway6_buf;
1275 struct in6_addr *ip6 = &ip6_buf;
1276 struct in6_addr *gateway6 = &gateway6_buf;
1277 uint8_t len6;
1278 size_t len;
1279 int rc;
1280
1281 /* First, create routing table for any child settings. We do
1282 * this depth-first and in reverse order so that the end
1283 * result reflects the relative priorities of the settings
1284 * blocks.
1285 */
1286 list_for_each_entry_reverse ( child, &settings->children, siblings )
1287 ipv6_create_routes ( netdev, child );
1288
1289 /* Fetch IPv6 address, if any */
1290 len = fetch_setting ( settings, &ip6_setting, &origin, NULL,
1291 ip6, sizeof ( *ip6 ) );
1292 if ( ( len != sizeof ( *ip6 ) ) || ( origin != settings ) )
1293 return 0;
1294
1295 /* Fetch prefix length, if defined */
1296 len = fetch_setting ( settings, &len6_setting, &origin, NULL,
1297 &len6, sizeof ( len6 ) );
1298 if ( ( len != sizeof ( len6 ) ) || ( origin != settings ) )
1299 len6 = 0;
1300 if ( len6 > IPV6_MAX_PREFIX_LEN )
1301 len6 = IPV6_MAX_PREFIX_LEN;
1302
1303 /* Fetch gateway, if defined */
1304 len = fetch_setting ( settings, &gateway6_setting, &origin, NULL,
1305 gateway6, sizeof ( *gateway6 ) );
1306 if ( ( len != sizeof ( *gateway6 ) ) || ( origin != settings ) )
1307 gateway6 = NULL;
1308
1309 /* Create or update route */
1310 if ( ( rc = ipv6_add_miniroute ( netdev, ip6, len6, gateway6 ) ) != 0){
1311 DBGC ( netdev, "IPv6 %s could not add route: %s\n",
1312 netdev->name, strerror ( rc ) );
1313 return rc;
1314 }
1315
1316 return 0;
1317 }
1318
1319 /**
1320 * Create IPv6 routing table based on configured settings
1321 *
1322 * @ret rc Return status code
1323 */
ipv6_create_all_routes(void)1324 static int ipv6_create_all_routes ( void ) {
1325 struct ipv6_miniroute *miniroute;
1326 struct ipv6_miniroute *tmp;
1327 struct net_device *netdev;
1328 struct settings *settings;
1329 int rc;
1330
1331 /* Delete all existing routes */
1332 list_for_each_entry_safe ( miniroute, tmp, &ipv6_miniroutes, list )
1333 ipv6_del_miniroute ( miniroute );
1334
1335 /* Create routes for each configured network device */
1336 for_each_netdev ( netdev ) {
1337 settings = netdev_settings ( netdev );
1338 if ( ( rc = ipv6_create_routes ( netdev, settings ) ) != 0 )
1339 return rc;
1340 }
1341
1342 return 0;
1343 }
1344
1345 /** IPv6 settings applicator */
1346 struct settings_applicator ipv6_settings_applicator __settings_applicator = {
1347 .apply = ipv6_create_all_routes,
1348 };
1349
1350 /* Drag in objects via ipv6_protocol */
1351 REQUIRING_SYMBOL ( ipv6_protocol );
1352
1353 /* Drag in ICMPv6 */
1354 REQUIRE_OBJECT ( icmpv6 );
1355
1356 /* Drag in NDP */
1357 REQUIRE_OBJECT ( ndp );
1358