You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ipv6.c 29KB

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