[prefix] Use garbage-collectable section names
[ipxe.git] / src / net / ndp.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 <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <byteswap.h>
26 #include <ipxe/in.h>
27 #include <ipxe/iobuf.h>
28 #include <ipxe/tcpip.h>
29 #include <ipxe/ipv6.h>
30 #include <ipxe/icmpv6.h>
31 #include <ipxe/neighbour.h>
32 #include <ipxe/dhcpv6.h>
33 #include <ipxe/ndp.h>
34
35 /** @file
36 *
37 * IPv6 neighbour discovery protocol
38 *
39 */
40
41 static int
42 ipv6conf_rx_router_advertisement ( struct net_device *netdev,
43 struct ndp_router_advertisement_header *radv,
44 size_t len );
45
46 /**
47 * Transmit NDP packet with link-layer address option
48 *
49 * @v netdev Network device
50 * @v sin6_src Source socket address
51 * @v sin6_dest Destination socket address
52 * @v data NDP header
53 * @v len Size of NDP header
54 * @v option_type NDP option type
55 * @ret rc Return status code
56 */
57 static int ndp_tx_ll_addr ( struct net_device *netdev,
58 struct sockaddr_in6 *sin6_src,
59 struct sockaddr_in6 *sin6_dest,
60 const void *data, size_t len,
61 unsigned int option_type ) {
62 struct sockaddr_tcpip *st_src =
63 ( ( struct sockaddr_tcpip * ) sin6_src );
64 struct sockaddr_tcpip *st_dest =
65 ( ( struct sockaddr_tcpip * ) sin6_dest );
66 struct ll_protocol *ll_protocol = netdev->ll_protocol;
67 struct io_buffer *iobuf;
68 struct ndp_ll_addr_option *ll_addr_opt;
69 union ndp_header *ndp;
70 size_t option_len;
71 int rc;
72
73 /* Allocate and populate buffer */
74 option_len = ( ( sizeof ( *ll_addr_opt ) +
75 ll_protocol->ll_addr_len + NDP_OPTION_BLKSZ - 1 ) &
76 ~( NDP_OPTION_BLKSZ - 1 ) );
77 iobuf = alloc_iob ( MAX_LL_NET_HEADER_LEN + len + option_len );
78 if ( ! iobuf )
79 return -ENOMEM;
80 iob_reserve ( iobuf, MAX_LL_NET_HEADER_LEN );
81 memcpy ( iob_put ( iobuf, len ), data, len );
82 ll_addr_opt = iob_put ( iobuf, option_len );
83 ll_addr_opt->header.type = option_type;
84 ll_addr_opt->header.blocks = ( option_len / NDP_OPTION_BLKSZ );
85 memcpy ( ll_addr_opt->ll_addr, netdev->ll_addr,
86 ll_protocol->ll_addr_len );
87 ndp = iobuf->data;
88 ndp->icmp.chksum = tcpip_chksum ( ndp, ( len + option_len ) );
89
90 /* Transmit packet */
91 if ( ( rc = tcpip_tx ( iobuf, &icmpv6_protocol, st_src, st_dest,
92 netdev, &ndp->icmp.chksum ) ) != 0 ) {
93 DBGC ( netdev, "NDP %s could not transmit packet: %s\n",
94 netdev->name, strerror ( rc ) );
95 return rc;
96 }
97
98 return 0;
99 }
100
101 /**
102 * Transmit NDP neighbour discovery request
103 *
104 * @v netdev Network device
105 * @v net_protocol Network-layer protocol
106 * @v net_dest Destination network-layer address
107 * @v net_source Source network-layer address
108 * @ret rc Return status code
109 */
110 static int ndp_tx_request ( struct net_device *netdev,
111 struct net_protocol *net_protocol __unused,
112 const void *net_dest, const void *net_source ) {
113 struct sockaddr_in6 sin6_src;
114 struct sockaddr_in6 sin6_dest;
115 struct ndp_neighbour_header neigh;
116 int rc;
117
118 /* Construct source address */
119 memset ( &sin6_src, 0, sizeof ( sin6_src ) );
120 sin6_src.sin6_family = AF_INET6;
121 memcpy ( &sin6_src.sin6_addr, net_source,
122 sizeof ( sin6_src.sin6_addr ) );
123
124 /* Construct multicast destination address */
125 memset ( &sin6_dest, 0, sizeof ( sin6_dest ) );
126 sin6_dest.sin6_family = AF_INET6;
127 sin6_dest.sin6_scope_id = netdev->index;
128 ipv6_solicited_node ( &sin6_dest.sin6_addr, net_dest );
129
130 /* Construct neighbour header */
131 memset ( &neigh, 0, sizeof ( neigh ) );
132 neigh.icmp.type = ICMPV6_NEIGHBOUR_SOLICITATION;
133 memcpy ( &neigh.target, net_dest, sizeof ( neigh.target ) );
134
135 /* Transmit neighbour discovery packet */
136 if ( ( rc = ndp_tx_ll_addr ( netdev, &sin6_src, &sin6_dest, &neigh,
137 sizeof ( neigh ),
138 NDP_OPT_LL_SOURCE ) ) != 0 )
139 return rc;
140
141 return 0;
142 }
143
144 /** NDP neighbour discovery protocol */
145 struct neighbour_discovery ndp_discovery = {
146 .name = "NDP",
147 .tx_request = ndp_tx_request,
148 };
149
150 /**
151 * Transmit NDP router solicitation
152 *
153 * @v netdev Network device
154 * @ret rc Return status code
155 */
156 static int ndp_tx_router_solicitation ( struct net_device *netdev ) {
157 struct ndp_router_solicitation_header rsol;
158 struct sockaddr_in6 sin6_dest;
159 int rc;
160
161 /* Construct multicast destination address */
162 memset ( &sin6_dest, 0, sizeof ( sin6_dest ) );
163 sin6_dest.sin6_family = AF_INET6;
164 sin6_dest.sin6_scope_id = netdev->index;
165 ipv6_all_routers ( &sin6_dest.sin6_addr );
166
167 /* Construct router solicitation */
168 memset ( &rsol, 0, sizeof ( rsol ) );
169 rsol.icmp.type = ICMPV6_ROUTER_SOLICITATION;
170
171 /* Transmit packet */
172 if ( ( rc = ndp_tx_ll_addr ( netdev, NULL, &sin6_dest, &rsol,
173 sizeof ( rsol ), NDP_OPT_LL_SOURCE ) ) !=0)
174 return rc;
175
176 return 0;
177 }
178
179 /**
180 * Process NDP neighbour solicitation source link-layer address option
181 *
182 * @v netdev Network device
183 * @v sin6_src Source socket address
184 * @v ndp NDP packet
185 * @v option NDP option
186 * @v len NDP option length
187 * @ret rc Return status code
188 */
189 static int
190 ndp_rx_neighbour_solicitation_ll_source ( struct net_device *netdev,
191 struct sockaddr_in6 *sin6_src,
192 union ndp_header *ndp,
193 union ndp_option *option,
194 size_t len ) {
195 struct ndp_neighbour_header *neigh = &ndp->neigh;
196 struct ndp_ll_addr_option *ll_addr_opt = &option->ll_addr;
197 struct ll_protocol *ll_protocol = netdev->ll_protocol;
198 int rc;
199
200 /* Silently ignore neighbour solicitations for addresses we do
201 * not own.
202 */
203 if ( ! ipv6_has_addr ( netdev, &neigh->target ) )
204 return 0;
205
206 /* Sanity check */
207 if ( offsetof ( typeof ( *ll_addr_opt ),
208 ll_addr[ll_protocol->ll_addr_len] ) > len ) {
209 DBGC ( netdev, "NDP %s neighbour solicitation link-layer "
210 "address option too short at %zd bytes\n",
211 netdev->name, len );
212 return -EINVAL;
213 }
214
215 /* Create or update neighbour cache entry */
216 if ( ( rc = neighbour_define ( netdev, &ipv6_protocol,
217 &sin6_src->sin6_addr,
218 ll_addr_opt->ll_addr ) ) != 0 ) {
219 DBGC ( netdev, "NDP %s could not define %s => %s: %s\n",
220 netdev->name, inet6_ntoa ( &sin6_src->sin6_addr ),
221 ll_protocol->ntoa ( ll_addr_opt->ll_addr ),
222 strerror ( rc ) );
223 return rc;
224 }
225
226 /* Convert neighbour header to advertisement */
227 memset ( neigh, 0, offsetof ( typeof ( *neigh ), target ) );
228 neigh->icmp.type = ICMPV6_NEIGHBOUR_ADVERTISEMENT;
229 neigh->flags = ( NDP_NEIGHBOUR_SOLICITED | NDP_NEIGHBOUR_OVERRIDE );
230
231 /* Send neighbour advertisement */
232 if ( ( rc = ndp_tx_ll_addr ( netdev, NULL, sin6_src, neigh,
233 sizeof ( *neigh ),
234 NDP_OPT_LL_TARGET ) ) != 0 )
235 return rc;
236
237 return 0;
238 }
239
240 /**
241 * Process NDP neighbour advertisement target link-layer address option
242 *
243 * @v netdev Network device
244 * @v sin6_src Source socket address
245 * @v ndp NDP packet
246 * @v option NDP option
247 * @v len NDP option length
248 * @ret rc Return status code
249 */
250 static int
251 ndp_rx_neighbour_advertisement_ll_target ( struct net_device *netdev,
252 struct sockaddr_in6 *sin6_src
253 __unused,
254 union ndp_header *ndp,
255 union ndp_option *option,
256 size_t len ) {
257 struct ndp_neighbour_header *neigh = &ndp->neigh;
258 struct ndp_ll_addr_option *ll_addr_opt = &option->ll_addr;
259 struct ll_protocol *ll_protocol = netdev->ll_protocol;
260 int rc;
261
262 /* Sanity check */
263 if ( offsetof ( typeof ( *ll_addr_opt ),
264 ll_addr[ll_protocol->ll_addr_len] ) > len ) {
265 DBGC ( netdev, "NDP %s neighbour advertisement link-layer "
266 "address option too short at %zd bytes\n",
267 netdev->name, len );
268 return -EINVAL;
269 }
270
271 /* Update neighbour cache entry, if any */
272 if ( ( rc = neighbour_update ( netdev, &ipv6_protocol, &neigh->target,
273 ll_addr_opt->ll_addr ) ) != 0 ) {
274 DBGC ( netdev, "NDP %s could not update %s => %s: %s\n",
275 netdev->name, inet6_ntoa ( &neigh->target ),
276 ll_protocol->ntoa ( ll_addr_opt->ll_addr ),
277 strerror ( rc ) );
278 return rc;
279 }
280
281 return 0;
282 }
283
284 /**
285 * Process NDP router advertisement source link-layer address option
286 *
287 * @v netdev Network device
288 * @v sin6_src Source socket address
289 * @v ndp NDP packet
290 * @v option NDP option
291 * @v len NDP option length
292 * @ret rc Return status code
293 */
294 static int
295 ndp_rx_router_advertisement_ll_source ( struct net_device *netdev,
296 struct sockaddr_in6 *sin6_src,
297 union ndp_header *ndp __unused,
298 union ndp_option *option, size_t len ) {
299 struct ndp_ll_addr_option *ll_addr_opt = &option->ll_addr;
300 struct ll_protocol *ll_protocol = netdev->ll_protocol;
301 int rc;
302
303 /* Sanity check */
304 if ( offsetof ( typeof ( *ll_addr_opt ),
305 ll_addr[ll_protocol->ll_addr_len] ) > len ) {
306 DBGC ( netdev, "NDP %s router advertisement link-layer address "
307 "option too short at %zd bytes\n", netdev->name, len );
308 return -EINVAL;
309 }
310
311 /* Define neighbour cache entry */
312 if ( ( rc = neighbour_define ( netdev, &ipv6_protocol,
313 &sin6_src->sin6_addr,
314 ll_addr_opt->ll_addr ) ) != 0 ) {
315 DBGC ( netdev, "NDP %s could not define %s => %s: %s\n",
316 netdev->name, inet6_ntoa ( &sin6_src->sin6_addr ),
317 ll_protocol->ntoa ( ll_addr_opt->ll_addr ),
318 strerror ( rc ) );
319 return rc;
320 }
321
322 return 0;
323 }
324
325 /**
326 * Process NDP router advertisement prefix information option
327 *
328 * @v netdev Network device
329 * @v sin6_src Source socket address
330 * @v ndp NDP packet
331 * @v option NDP option
332 * @v len NDP option length
333 * @ret rc Return status code
334 */
335 static int
336 ndp_rx_router_advertisement_prefix ( struct net_device *netdev,
337 struct sockaddr_in6 *sin6_src,
338 union ndp_header *ndp,
339 union ndp_option *option, size_t len ) {
340 struct ndp_router_advertisement_header *radv = &ndp->radv;
341 struct ndp_prefix_information_option *prefix_opt = &option->prefix;
342 struct in6_addr *router = &sin6_src->sin6_addr;
343 struct in6_addr address;
344 int prefix_len;
345 int rc;
346
347 /* Sanity check */
348 if ( sizeof ( *prefix_opt ) > len ) {
349 DBGC ( netdev, "NDP %s router advertisement prefix option too "
350 "short at %zd bytes\n", netdev->name, len );
351 return -EINVAL;
352 }
353 DBGC ( netdev, "NDP %s found %sdefault router %s ",
354 netdev->name, ( radv->lifetime ? "" : "non-" ),
355 inet6_ntoa ( &sin6_src->sin6_addr ) );
356 DBGC ( netdev, "for %s-link %sautonomous prefix %s/%d\n",
357 ( ( prefix_opt->flags & NDP_PREFIX_ON_LINK ) ? "on" : "off" ),
358 ( ( prefix_opt->flags & NDP_PREFIX_AUTONOMOUS ) ? "" : "non-" ),
359 inet6_ntoa ( &prefix_opt->prefix ),
360 prefix_opt->prefix_len );
361
362 /* Ignore off-link prefixes */
363 if ( ! ( prefix_opt->flags & NDP_PREFIX_ON_LINK ) )
364 return 0;
365
366 /* Define prefix */
367 if ( ( rc = ipv6_set_prefix ( netdev, &prefix_opt->prefix,
368 prefix_opt->prefix_len,
369 ( radv->lifetime ?
370 router : NULL ) ) ) != 0 ) {
371 DBGC ( netdev, "NDP %s could not define prefix %s/%d: %s\n",
372 netdev->name, inet6_ntoa ( &prefix_opt->prefix ),
373 prefix_opt->prefix_len, strerror ( rc ) );
374 return rc;
375 }
376
377 /* Perform stateless address autoconfiguration, if applicable */
378 if ( prefix_opt->flags & NDP_PREFIX_AUTONOMOUS ) {
379 memcpy ( &address, &prefix_opt->prefix, sizeof ( address ) );
380 prefix_len = ipv6_eui64 ( &address, netdev );
381 if ( prefix_len < 0 ) {
382 rc = prefix_len;
383 DBGC ( netdev, "NDP %s could not construct SLAAC "
384 "address: %s\n", netdev->name, strerror ( rc ) );
385 return rc;
386 }
387 if ( prefix_len != prefix_opt->prefix_len ) {
388 DBGC ( netdev, "NDP %s incorrect SLAAC prefix length "
389 "%d (expected %d)\n", netdev->name,
390 prefix_opt->prefix_len, prefix_len );
391 return -EINVAL;
392 }
393 if ( ( rc = ipv6_set_address ( netdev, &address ) ) != 0 ) {
394 DBGC ( netdev, "NDP %s could not set address %s: %s\n",
395 netdev->name, inet6_ntoa ( &address ),
396 strerror ( rc ) );
397 return rc;
398 }
399 }
400
401 return 0;
402 }
403
404 /** An NDP option handler */
405 struct ndp_option_handler {
406 /** ICMPv6 type */
407 uint8_t icmp_type;
408 /** Option type */
409 uint8_t option_type;
410 /**
411 * Handle received option
412 *
413 * @v netdev Network device
414 * @v sin6_src Source socket address
415 * @v ndp NDP packet
416 * @v option NDP option
417 * @ret rc Return status code
418 */
419 int ( * rx ) ( struct net_device *netdev, struct sockaddr_in6 *sin6_src,
420 union ndp_header *ndp, union ndp_option *option,
421 size_t len );
422 };
423
424 /** NDP option handlers */
425 static struct ndp_option_handler ndp_option_handlers[] = {
426 {
427 .icmp_type = ICMPV6_NEIGHBOUR_SOLICITATION,
428 .option_type = NDP_OPT_LL_SOURCE,
429 .rx = ndp_rx_neighbour_solicitation_ll_source,
430 },
431 {
432 .icmp_type = ICMPV6_NEIGHBOUR_ADVERTISEMENT,
433 .option_type = NDP_OPT_LL_TARGET,
434 .rx = ndp_rx_neighbour_advertisement_ll_target,
435 },
436 {
437 .icmp_type = ICMPV6_ROUTER_ADVERTISEMENT,
438 .option_type = NDP_OPT_LL_SOURCE,
439 .rx = ndp_rx_router_advertisement_ll_source,
440 },
441 {
442 .icmp_type = ICMPV6_ROUTER_ADVERTISEMENT,
443 .option_type = NDP_OPT_PREFIX,
444 .rx = ndp_rx_router_advertisement_prefix,
445 },
446 };
447
448 /**
449 * Process received NDP option
450 *
451 * @v netdev Network device
452 * @v sin6_src Source socket address
453 * @v ndp NDP packet
454 * @v option NDP option
455 * @v len Option length
456 * @ret rc Return status code
457 */
458 static int ndp_rx_option ( struct net_device *netdev,
459 struct sockaddr_in6 *sin6_src, union ndp_header *ndp,
460 union ndp_option *option, size_t len ) {
461 struct ndp_option_handler *handler;
462 unsigned int i;
463
464 /* Locate a suitable option handler, if any */
465 for ( i = 0 ; i < ( sizeof ( ndp_option_handlers ) /
466 sizeof ( ndp_option_handlers[0] ) ) ; i++ ) {
467 handler = &ndp_option_handlers[i];
468 if ( ( handler->icmp_type == ndp->icmp.type ) &&
469 ( handler->option_type == option->header.type ) ) {
470 return handler->rx ( netdev, sin6_src, ndp,
471 option, len );
472 }
473 }
474
475 /* Silently ignore unknown options as per RFC 4861 */
476 return 0;
477 }
478
479 /**
480 * Process received NDP packet options
481 *
482 * @v netdev Network device
483 * @v sin6_src Source socket address
484 * @v ndp NDP header
485 * @v offset Offset to NDP options
486 * @v len Length of NDP packet
487 * @ret rc Return status code
488 */
489 static int ndp_rx_options ( struct net_device *netdev,
490 struct sockaddr_in6 *sin6_src,
491 union ndp_header *ndp, size_t offset, size_t len ) {
492 union ndp_option *option;
493 size_t remaining;
494 size_t option_len;
495 int rc;
496
497 /* Sanity check */
498 if ( len < offset ) {
499 DBGC ( netdev, "NDP %s packet too short at %zd bytes (min %zd "
500 "bytes)\n", netdev->name, len, offset );
501 return -EINVAL;
502 }
503
504 /* Search for option */
505 option = ( ( ( void * ) ndp ) + offset );
506 remaining = ( len - offset );
507 while ( remaining ) {
508
509 /* Sanity check */
510 if ( ( remaining < sizeof ( option->header ) ) ||
511 ( option->header.blocks == 0 ) ||
512 ( remaining < ( option->header.blocks *
513 NDP_OPTION_BLKSZ ) ) ) {
514 DBGC ( netdev, "NDP %s bad option length:\n",
515 netdev->name );
516 DBGC_HDA ( netdev, 0, option, remaining );
517 return -EINVAL;
518 }
519 option_len = ( option->header.blocks * NDP_OPTION_BLKSZ );
520
521 /* Handle option */
522 if ( ( rc = ndp_rx_option ( netdev, sin6_src, ndp, option,
523 option_len ) ) != 0 )
524 return rc;
525
526 /* Move to next option */
527 option = ( ( ( void * ) option ) + option_len );
528 remaining -= option_len;
529 }
530
531 return 0;
532 }
533
534 /**
535 * Process received NDP neighbour solicitation or advertisement
536 *
537 * @v iobuf I/O buffer
538 * @v netdev Network device
539 * @v sin6_src Source socket address
540 * @v sin6_dest Destination socket address
541 * @ret rc Return status code
542 */
543 static int ndp_rx_neighbour ( struct io_buffer *iobuf,
544 struct net_device *netdev,
545 struct sockaddr_in6 *sin6_src,
546 struct sockaddr_in6 *sin6_dest __unused ) {
547 union ndp_header *ndp = iobuf->data;
548 struct ndp_neighbour_header *neigh = &ndp->neigh;
549 size_t len = iob_len ( iobuf );
550 int rc;
551
552 /* Process options */
553 if ( ( rc = ndp_rx_options ( netdev, sin6_src, ndp,
554 offsetof ( typeof ( *neigh ), option ),
555 len ) ) != 0 )
556 goto err_options;
557
558 err_options:
559 free_iob ( iobuf );
560 return rc;
561 }
562
563 /**
564 * Process received NDP router advertisement
565 *
566 * @v iobuf I/O buffer
567 * @v netdev Network device
568 * @v sin6_src Source socket address
569 * @v sin6_dest Destination socket address
570 * @ret rc Return status code
571 */
572 static int
573 ndp_rx_router_advertisement ( struct io_buffer *iobuf,
574 struct net_device *netdev,
575 struct sockaddr_in6 *sin6_src,
576 struct sockaddr_in6 *sin6_dest __unused ) {
577 union ndp_header *ndp = iobuf->data;
578 struct ndp_router_advertisement_header *radv = &ndp->radv;
579 size_t len = iob_len ( iobuf );
580 int rc;
581
582 /* Process options */
583 if ( ( rc = ndp_rx_options ( netdev, sin6_src, ndp,
584 offsetof ( typeof ( *radv ), option ),
585 len ) ) != 0 )
586 goto err_options;
587
588 /* Pass to IPv6 autoconfiguration */
589 if ( ( rc = ipv6conf_rx_router_advertisement ( netdev, radv,
590 len ) ) != 0 )
591 goto err_ipv6conf;
592
593 err_ipv6conf:
594 err_options:
595 free_iob ( iobuf );
596 return rc;
597 }
598
599 /** NDP ICMPv6 handlers */
600 struct icmpv6_handler ndp_handlers[] __icmpv6_handler = {
601 {
602 .type = ICMPV6_NEIGHBOUR_SOLICITATION,
603 .rx = ndp_rx_neighbour,
604 },
605 {
606 .type = ICMPV6_NEIGHBOUR_ADVERTISEMENT,
607 .rx = ndp_rx_neighbour,
608 },
609 {
610 .type = ICMPV6_ROUTER_ADVERTISEMENT,
611 .rx = ndp_rx_router_advertisement,
612 },
613 };
614
615 /****************************************************************************
616 *
617 * NDP settings
618 *
619 */
620
621 /** An NDP settings block */
622 struct ndp_settings {
623 /** Reference counter */
624 struct refcnt refcnt;
625 /** Settings interface */
626 struct settings settings;
627 /** Length of NDP options */
628 size_t len;
629 /** NDP options */
630 union ndp_option option[0];
631 };
632
633 /** NDP settings scope */
634 static const struct settings_scope ndp_settings_scope;
635
636 /**
637 * Construct NDP tag
638 *
639 * @v type NDP option type
640 * @v offset Starting offset of data
641 * @ret tag NDP tag
642 */
643 #define NDP_TAG( type, offset ) ( ( (offset) << 8 ) | (type) )
644
645 /**
646 * Extract NDP tag type
647 *
648 * @v tag NDP tag
649 * @ret type NDP option type
650 */
651 #define NDP_TAG_TYPE( tag ) ( (tag) & 0xff )
652
653 /**
654 * Extract NDP tag offset
655 *
656 * @v tag NDP tag
657 * @ret offset Starting offset of data
658 */
659 #define NDP_TAG_OFFSET( tag ) ( (tag) >> 8 )
660
661 /**
662 * Check applicability of NDP setting
663 *
664 * @v settings Settings block
665 * @v setting Setting to fetch
666 * @ret applies Setting applies within this settings block
667 */
668 static int ndp_applies ( struct settings *settings __unused,
669 const struct setting *setting ) {
670
671 return ( setting->scope == &ndp_settings_scope );
672 }
673
674 /**
675 * Fetch value of NDP setting
676 *
677 * @v settings Settings block
678 * @v setting Setting to fetch
679 * @v data Buffer to fill with setting data
680 * @v len Length of buffer
681 * @ret len Length of setting data, or negative error
682 */
683 static int ndp_fetch ( struct settings *settings,
684 struct setting *setting,
685 void *data, size_t len ) {
686 struct ndp_settings *ndpset =
687 container_of ( settings, struct ndp_settings, settings );
688 struct net_device *netdev =
689 container_of ( settings->parent, struct net_device,
690 settings.settings );
691 union ndp_option *option;
692 unsigned int type = NDP_TAG_TYPE ( setting->tag );
693 unsigned int offset = NDP_TAG_OFFSET ( setting->tag );
694 size_t remaining;
695 size_t option_len;
696 size_t payload_len;
697
698 /* Scan through NDP options for requested type. We can assume
699 * that the options are well-formed, otherwise they would have
700 * been rejected prior to being stored.
701 */
702 option = ndpset->option;
703 remaining = ndpset->len;
704 while ( remaining ) {
705
706 /* Calculate option length */
707 option_len = ( option->header.blocks * NDP_OPTION_BLKSZ );
708
709 /* If this is the requested option, return it */
710 if ( option->header.type == type ) {
711
712 /* Sanity check */
713 if ( offset > option_len ) {
714 DBGC ( netdev, "NDP %s option %d too short\n",
715 netdev->name, type );
716 return -EINVAL;
717 }
718 payload_len = ( option_len - offset );
719
720 /* Copy data to output buffer */
721 if ( len > payload_len )
722 len = payload_len;
723 memcpy ( data, ( ( ( void * ) option ) + offset ), len);
724 return payload_len;
725 }
726
727 /* Move to next option */
728 option = ( ( ( void * ) option ) + option_len );
729 remaining -= option_len;
730 }
731
732 return -ENOENT;
733 }
734
735 /** NDP settings operations */
736 static struct settings_operations ndp_settings_operations = {
737 .applies = ndp_applies,
738 .fetch = ndp_fetch,
739 };
740
741 /**
742 * Register NDP settings
743 *
744 * @v netdev Network device
745 * @v option NDP options
746 * @v len Length of options
747 * @ret rc Return status code
748 */
749 static int ndp_register_settings ( struct net_device *netdev,
750 union ndp_option *option, size_t len ) {
751 struct settings *parent = netdev_settings ( netdev );
752 struct ndp_settings *ndpset;
753 int rc;
754
755 /* Allocate and initialise structure */
756 ndpset = zalloc ( sizeof ( *ndpset ) + len );
757 if ( ! ndpset ) {
758 rc = -ENOMEM;
759 goto err_alloc;
760 }
761 ref_init ( &ndpset->refcnt, NULL );
762 settings_init ( &ndpset->settings, &ndp_settings_operations,
763 &ndpset->refcnt, &ndp_settings_scope );
764 ndpset->len = len;
765 memcpy ( ndpset->option, option, len );
766
767 /* Register settings */
768 if ( ( rc = register_settings ( &ndpset->settings, parent,
769 NDP_SETTINGS_NAME ) ) != 0 )
770 goto err_register;
771
772 err_register:
773 ref_put ( &ndpset->refcnt );
774 err_alloc:
775 return rc;
776 }
777
778 /** DNS server setting */
779 const struct setting ndp_dns6_setting __setting ( SETTING_IP_EXTRA, dns6 ) = {
780 .name = "dns6",
781 .description = "DNS server",
782 .tag = NDP_TAG ( NDP_OPT_RDNSS,
783 offsetof ( struct ndp_rdnss_option, addresses ) ),
784 .type = &setting_type_ipv6,
785 .scope = &ndp_settings_scope,
786 };
787
788 /** DNS search list setting */
789 const struct setting ndp_dnssl_setting __setting ( SETTING_IP_EXTRA, dnssl ) = {
790 .name = "dnssl",
791 .description = "DNS search list",
792 .tag = NDP_TAG ( NDP_OPT_DNSSL,
793 offsetof ( struct ndp_dnssl_option, names ) ),
794 .type = &setting_type_dnssl,
795 .scope = &ndp_settings_scope,
796 };
797
798 /****************************************************************************
799 *
800 * IPv6 autoconfiguration
801 *
802 */
803
804 /** An IPv6 configurator */
805 struct ipv6conf {
806 /** Reference count */
807 struct refcnt refcnt;
808 /** List of configurators */
809 struct list_head list;
810
811 /** Job control interface */
812 struct interface job;
813 /** DHCPv6 interface */
814 struct interface dhcp;
815
816 /** Network device being configured */
817 struct net_device *netdev;
818
819 /** Retransmission timer */
820 struct retry_timer timer;
821 };
822
823 /** List of IPv6 configurators */
824 static LIST_HEAD ( ipv6confs );
825
826 /**
827 * Free IPv6 configurator
828 *
829 * @v refcnt Reference count
830 */
831 static void ipv6conf_free ( struct refcnt *refcnt ) {
832 struct ipv6conf *ipv6conf =
833 container_of ( refcnt, struct ipv6conf, refcnt );
834
835 netdev_put ( ipv6conf->netdev );
836 free ( ipv6conf );
837 }
838
839 /**
840 * Identify IPv6 configurator by network device
841 *
842 * @v netdev Network device
843 * @ret ipv6 IPv6 configurator, or NULL
844 */
845 static struct ipv6conf * ipv6conf_demux ( struct net_device *netdev ) {
846 struct ipv6conf *ipv6conf;
847
848 list_for_each_entry ( ipv6conf, &ipv6confs, list ) {
849 if ( ipv6conf->netdev == netdev )
850 return ipv6conf;
851 }
852 return NULL;
853 }
854
855 /**
856 * Finish IPv6 autoconfiguration
857 *
858 * @v ipv6 IPv6 configurator
859 * @v rc Reason for finishing
860 */
861 static void ipv6conf_done ( struct ipv6conf *ipv6conf, int rc ) {
862
863 /* Shut down interfaces */
864 intf_shutdown ( &ipv6conf->job, rc );
865 intf_shutdown ( &ipv6conf->dhcp, rc );
866
867 /* Stop timer */
868 stop_timer ( &ipv6conf->timer );
869
870 /* Remove from list and drop list's reference */
871 list_del ( &ipv6conf->list );
872 ref_put ( &ipv6conf->refcnt );
873 }
874
875 /**
876 * Handle IPv6 configurator timer expiry
877 *
878 * @v timer Retry timer
879 * @v fail Failure indicator
880 */
881 static void ipv6conf_expired ( struct retry_timer *timer, int fail ) {
882 struct ipv6conf *ipv6conf =
883 container_of ( timer, struct ipv6conf, timer );
884
885 /* If we have failed, terminate autoconfiguration */
886 if ( fail ) {
887 ipv6conf_done ( ipv6conf, -ETIMEDOUT );
888 return;
889 }
890
891 /* Otherwise, transmit router solicitation and restart timer */
892 start_timer ( &ipv6conf->timer );
893 ndp_tx_router_solicitation ( ipv6conf->netdev );
894 }
895
896 /**
897 * Handle router advertisement during IPv6 autoconfiguration
898 *
899 * @v netdev Network device
900 * @v radv Router advertisement
901 * @v len Length of router advertisement
902 * @ret rc Return status code
903 *
904 * This function assumes that the router advertisement is well-formed,
905 * since it must have already passed through option processing.
906 */
907 static int
908 ipv6conf_rx_router_advertisement ( struct net_device *netdev,
909 struct ndp_router_advertisement_header *radv,
910 size_t len ) {
911 struct ipv6conf *ipv6conf;
912 size_t option_len;
913 int stateful;
914 int rc;
915
916 /* Identify IPv6 configurator, if any */
917 ipv6conf = ipv6conf_demux ( netdev );
918 if ( ! ipv6conf ) {
919 /* Not an error; router advertisements are processed
920 * as a background activity even when no explicit
921 * autoconfiguration is taking place.
922 */
923 return 0;
924 }
925
926 /* If this is not the first solicited router advertisement, ignore it */
927 if ( ! timer_running ( &ipv6conf->timer ) )
928 return 0;
929
930 /* Stop router solicitation timer */
931 stop_timer ( &ipv6conf->timer );
932
933 /* Register NDP settings */
934 option_len = ( len - offsetof ( typeof ( *radv ), option ) );
935 if ( ( rc = ndp_register_settings ( netdev, radv->option,
936 option_len ) ) != 0 )
937 return rc;
938
939 /* Start DHCPv6 if required */
940 if ( radv->flags & ( NDP_ROUTER_MANAGED | NDP_ROUTER_OTHER ) ) {
941 stateful = ( radv->flags & NDP_ROUTER_MANAGED );
942 if ( ( rc = start_dhcpv6 ( &ipv6conf->dhcp, netdev,
943 stateful ) ) != 0 ) {
944 DBGC ( netdev, "NDP %s could not start state%s DHCPv6: "
945 "%s\n", netdev->name,
946 ( stateful ? "ful" : "less" ), strerror ( rc ) );
947 ipv6conf_done ( ipv6conf, rc );
948 return rc;
949 }
950 return 0;
951 }
952
953 /* Otherwise, terminate autoconfiguration */
954 ipv6conf_done ( ipv6conf, 0 );
955
956 return 0;
957 }
958
959 /** IPv6 configurator job interface operations */
960 static struct interface_operation ipv6conf_job_op[] = {
961 INTF_OP ( intf_close, struct ipv6conf *, ipv6conf_done ),
962 };
963
964 /** IPv6 configurator job interface descriptor */
965 static struct interface_descriptor ipv6conf_job_desc =
966 INTF_DESC ( struct ipv6conf, job, ipv6conf_job_op );
967
968 /** IPv6 configurator DHCPv6 interface operations */
969 static struct interface_operation ipv6conf_dhcp_op[] = {
970 INTF_OP ( intf_close, struct ipv6conf *, ipv6conf_done ),
971 };
972
973 /** IPv6 configurator DHCPv6 interface descriptor */
974 static struct interface_descriptor ipv6conf_dhcp_desc =
975 INTF_DESC ( struct ipv6conf, dhcp, ipv6conf_dhcp_op );
976
977 /**
978 * Start IPv6 autoconfiguration
979 *
980 * @v job Job control interface
981 * @v netdev Network device
982 * @ret rc Return status code
983 */
984 int start_ipv6conf ( struct interface *job, struct net_device *netdev ) {
985 struct ipv6conf *ipv6conf;
986
987 /* Allocate and initialise structure */
988 ipv6conf = zalloc ( sizeof ( *ipv6conf ) );
989 if ( ! ipv6conf )
990 return -ENOMEM;
991 ref_init ( &ipv6conf->refcnt, ipv6conf_free );
992 intf_init ( &ipv6conf->job, &ipv6conf_job_desc, &ipv6conf->refcnt );
993 intf_init ( &ipv6conf->dhcp, &ipv6conf_dhcp_desc, &ipv6conf->refcnt );
994 timer_init ( &ipv6conf->timer, ipv6conf_expired, &ipv6conf->refcnt );
995 ipv6conf->netdev = netdev_get ( netdev );
996
997 /* Start timer to initiate router solicitation */
998 start_timer_nodelay ( &ipv6conf->timer );
999
1000 /* Attach parent interface, transfer reference to list, and return */
1001 intf_plug_plug ( &ipv6conf->job, job );
1002 list_add ( &ipv6conf->list, &ipv6confs );
1003 return 0;
1004 }
1005
1006 /** IPv6 network device configurator */
1007 struct net_device_configurator ipv6_configurator __net_device_configurator = {
1008 .name = "ipv6",
1009 .start = start_ipv6conf,
1010 };