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 31KB

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