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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. }
  469. /* Print IPv6 header for debugging */
  470. DBGC2 ( ipv6col ( &iphdr->dest ), "IPv6 TX %s->",
  471. inet6_ntoa ( &iphdr->src ) );
  472. DBGC2 ( ipv6col ( &iphdr->dest ), "%s len %zd next %d\n",
  473. inet6_ntoa ( &iphdr->dest ), len, iphdr->next_header );
  474. /* Calculate link-layer destination address, if possible */
  475. if ( IN6_IS_ADDR_MULTICAST ( next_hop ) ) {
  476. /* Multicast address */
  477. ipv6_stats.out_mcast_pkts++;
  478. if ( ( rc = netdev->ll_protocol->mc_hash ( AF_INET6, next_hop,
  479. ll_dest_buf ) ) !=0){
  480. DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not hash "
  481. "multicast %s: %s\n", inet6_ntoa ( next_hop ),
  482. strerror ( rc ) );
  483. goto err;
  484. }
  485. ll_dest = ll_dest_buf;
  486. } else {
  487. /* Unicast address */
  488. ll_dest = NULL;
  489. }
  490. /* Update statistics */
  491. ipv6_stats.out_transmits++;
  492. ipv6_stats.out_octets += iob_len ( iobuf );
  493. /* Hand off to link layer (via NDP if applicable) */
  494. if ( ll_dest ) {
  495. if ( ( rc = net_tx ( iobuf, netdev, &ipv6_protocol, ll_dest,
  496. netdev->ll_addr ) ) != 0 ) {
  497. DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not "
  498. "transmit packet via %s: %s\n",
  499. netdev->name, strerror ( rc ) );
  500. return rc;
  501. }
  502. } else {
  503. if ( ( rc = ndp_tx ( iobuf, netdev, next_hop, &iphdr->src,
  504. netdev->ll_addr ) ) != 0 ) {
  505. DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not "
  506. "transmit packet via %s: %s\n",
  507. netdev->name, strerror ( rc ) );
  508. return rc;
  509. }
  510. }
  511. return 0;
  512. err:
  513. free_iob ( iobuf );
  514. return rc;
  515. }
  516. /**
  517. * Process incoming IPv6 packets
  518. *
  519. * @v iobuf I/O buffer
  520. * @v netdev Network device
  521. * @v ll_dest Link-layer destination address
  522. * @v ll_source Link-layer destination source
  523. * @v flags Packet flags
  524. * @ret rc Return status code
  525. *
  526. * This function expects an IPv6 network datagram. It processes the
  527. * headers and sends it to the transport layer.
  528. */
  529. static int ipv6_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  530. const void *ll_dest __unused,
  531. const void *ll_source __unused,
  532. unsigned int flags __unused ) {
  533. struct ipv6_header *iphdr = iobuf->data;
  534. union ipv6_extension_header *ext;
  535. union {
  536. struct sockaddr_in6 sin6;
  537. struct sockaddr_tcpip st;
  538. } src, dest;
  539. uint16_t pshdr_csum;
  540. size_t len;
  541. size_t hdrlen;
  542. size_t extlen;
  543. int this_header;
  544. int next_header;
  545. int rc;
  546. /* Update statistics */
  547. ipv6_stats.in_receives++;
  548. ipv6_stats.in_octets += iob_len ( iobuf );
  549. if ( flags & LL_BROADCAST ) {
  550. ipv6_stats.in_bcast_pkts++;
  551. } else if ( flags & LL_MULTICAST ) {
  552. ipv6_stats.in_mcast_pkts++;
  553. }
  554. /* Sanity check the IPv6 header */
  555. if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
  556. DBGC ( ipv6col ( &iphdr->src ), "IPv6 packet too short at %zd "
  557. "bytes (min %zd bytes)\n", iob_len ( iobuf ),
  558. sizeof ( *iphdr ) );
  559. rc = -EINVAL_LEN;
  560. goto err_header;
  561. }
  562. if ( ( iphdr->ver_tc_label & htonl ( IPV6_MASK_VER ) ) !=
  563. htonl ( IPV6_VER ) ) {
  564. DBGC ( ipv6col ( &iphdr->src ), "IPv6 version %#08x not "
  565. "supported\n", ntohl ( iphdr->ver_tc_label ) );
  566. rc = -ENOTSUP_VER;
  567. goto err_header;
  568. }
  569. /* Truncate packet to specified length */
  570. len = ntohs ( iphdr->len );
  571. if ( len > iob_len ( iobuf ) ) {
  572. DBGC ( ipv6col ( &iphdr->src ), "IPv6 length too long at %zd "
  573. "bytes (packet is %zd bytes)\n", len, iob_len ( iobuf ));
  574. ipv6_stats.in_truncated_pkts++;
  575. rc = -EINVAL_LEN;
  576. goto err_other;
  577. }
  578. iob_unput ( iobuf, ( iob_len ( iobuf ) - len - sizeof ( *iphdr ) ) );
  579. hdrlen = sizeof ( *iphdr );
  580. /* Print IPv6 header for debugging */
  581. DBGC2 ( ipv6col ( &iphdr->src ), "IPv6 RX %s<-",
  582. inet6_ntoa ( &iphdr->dest ) );
  583. DBGC2 ( ipv6col ( &iphdr->src ), "%s len %zd next %d\n",
  584. inet6_ntoa ( &iphdr->src ), len, iphdr->next_header );
  585. /* Discard unicast packets not destined for us */
  586. if ( ( ! ( flags & LL_MULTICAST ) ) &&
  587. ( ! ipv6_has_addr ( netdev, &iphdr->dest ) ) ) {
  588. DBGC ( ipv6col ( &iphdr->src ), "IPv6 discarding non-local "
  589. "unicast packet for %s\n", inet6_ntoa ( &iphdr->dest ) );
  590. ipv6_stats.in_addr_errors++;
  591. rc = -EPIPE;
  592. goto err_other;
  593. }
  594. /* Process any extension headers */
  595. next_header = iphdr->next_header;
  596. while ( 1 ) {
  597. /* Extract extension header */
  598. this_header = next_header;
  599. ext = ( iobuf->data + hdrlen );
  600. extlen = sizeof ( ext->pad );
  601. if ( iob_len ( iobuf ) < ( hdrlen + extlen ) ) {
  602. DBGC ( ipv6col ( &iphdr->src ), "IPv6 too short for "
  603. "extension header type %d at %zd bytes (min "
  604. "%zd bytes)\n", this_header,
  605. ( iob_len ( iobuf ) - hdrlen ), extlen );
  606. rc = -EINVAL_LEN;
  607. goto err_header;
  608. }
  609. /* Determine size of extension header (if applicable) */
  610. if ( ( this_header == IPV6_HOPBYHOP ) ||
  611. ( this_header == IPV6_DESTINATION ) ||
  612. ( this_header == IPV6_ROUTING ) ) {
  613. /* Length field is present */
  614. extlen += ext->common.len;
  615. } else if ( this_header == IPV6_FRAGMENT ) {
  616. /* Length field is reserved and ignored (RFC2460) */
  617. } else {
  618. /* Not an extension header; assume rest is payload */
  619. break;
  620. }
  621. if ( iob_len ( iobuf ) < ( hdrlen + extlen ) ) {
  622. DBGC ( ipv6col ( &iphdr->src ), "IPv6 too short for "
  623. "extension header type %d at %zd bytes (min "
  624. "%zd bytes)\n", this_header,
  625. ( iob_len ( iobuf ) - hdrlen ), extlen );
  626. rc = -EINVAL_LEN;
  627. goto err_header;
  628. }
  629. hdrlen += extlen;
  630. next_header = ext->common.next_header;
  631. DBGC2 ( ipv6col ( &iphdr->src ), "IPv6 RX %s<-",
  632. inet6_ntoa ( &iphdr->dest ) );
  633. DBGC2 ( ipv6col ( &iphdr->src ), "%s ext type %d len %zd next "
  634. "%d\n", inet6_ntoa ( &iphdr->src ), this_header,
  635. extlen, next_header );
  636. /* Process this extension header */
  637. if ( ( this_header == IPV6_HOPBYHOP ) ||
  638. ( this_header == IPV6_DESTINATION ) ) {
  639. /* Check that all options can be ignored */
  640. if ( ( rc = ipv6_check_options ( iphdr, &ext->options,
  641. extlen ) ) != 0 )
  642. goto err_header;
  643. } else if ( this_header == IPV6_FRAGMENT ) {
  644. /* Reassemble fragments */
  645. iobuf = fragment_reassemble ( &ipv6_reassembler, iobuf,
  646. &hdrlen );
  647. if ( ! iobuf )
  648. return 0;
  649. iphdr = iobuf->data;
  650. }
  651. }
  652. /* Construct socket address, calculate pseudo-header checksum,
  653. * and hand off to transport layer
  654. */
  655. memset ( &src, 0, sizeof ( src ) );
  656. src.sin6.sin6_family = AF_INET6;
  657. memcpy ( &src.sin6.sin6_addr, &iphdr->src,
  658. sizeof ( src.sin6.sin6_addr ) );
  659. src.sin6.sin6_scope_id = netdev->index;
  660. memset ( &dest, 0, sizeof ( dest ) );
  661. dest.sin6.sin6_family = AF_INET6;
  662. memcpy ( &dest.sin6.sin6_addr, &iphdr->dest,
  663. sizeof ( dest.sin6.sin6_addr ) );
  664. dest.sin6.sin6_scope_id = netdev->index;
  665. iob_pull ( iobuf, hdrlen );
  666. pshdr_csum = ipv6_pshdr_chksum ( iphdr, iob_len ( iobuf ),
  667. next_header, TCPIP_EMPTY_CSUM );
  668. if ( ( rc = tcpip_rx ( iobuf, netdev, next_header, &src.st, &dest.st,
  669. pshdr_csum, &ipv6_stats ) ) != 0 ) {
  670. DBGC ( ipv6col ( &src.sin6.sin6_addr ), "IPv6 received packet "
  671. "rejected by stack: %s\n", strerror ( rc ) );
  672. return rc;
  673. }
  674. return 0;
  675. err_header:
  676. ipv6_stats.in_hdr_errors++;
  677. err_other:
  678. free_iob ( iobuf );
  679. return rc;
  680. }
  681. /**
  682. * Parse IPv6 address
  683. *
  684. * @v string IPv6 address string
  685. * @ret in IPv6 address to fill in
  686. * @ret rc Return status code
  687. */
  688. int inet6_aton ( const char *string, struct in6_addr *in ) {
  689. uint16_t *word = in->s6_addr16;
  690. uint16_t *end = ( word + ( sizeof ( in->s6_addr16 ) /
  691. sizeof ( in->s6_addr16[0] ) ) );
  692. uint16_t *pad = NULL;
  693. const char *nptr = string;
  694. char *endptr;
  695. unsigned long value;
  696. size_t pad_len;
  697. size_t move_len;
  698. /* Parse string */
  699. while ( 1 ) {
  700. /* Parse current word */
  701. value = strtoul ( nptr, &endptr, 16 );
  702. if ( value > 0xffff ) {
  703. DBG ( "IPv6 invalid word value %#lx in \"%s\"\n",
  704. value, string );
  705. return -EINVAL;
  706. }
  707. *(word++) = htons ( value );
  708. /* Parse separator */
  709. if ( ! *endptr )
  710. break;
  711. if ( *endptr != ':' ) {
  712. DBG ( "IPv6 invalid separator '%c' in \"%s\"\n",
  713. *endptr, string );
  714. return -EINVAL;
  715. }
  716. if ( ( endptr == nptr ) && ( nptr != string ) ) {
  717. if ( pad ) {
  718. DBG ( "IPv6 invalid multiple \"::\" in "
  719. "\"%s\"\n", string );
  720. return -EINVAL;
  721. }
  722. pad = word;
  723. }
  724. nptr = ( endptr + 1 );
  725. /* Check for overrun */
  726. if ( word == end ) {
  727. DBG ( "IPv6 too many words in \"%s\"\n", string );
  728. return -EINVAL;
  729. }
  730. }
  731. /* Insert padding if specified */
  732. if ( pad ) {
  733. move_len = ( ( ( void * ) word ) - ( ( void * ) pad ) );
  734. pad_len = ( ( ( void * ) end ) - ( ( void * ) word ) );
  735. memmove ( ( ( ( void * ) pad ) + pad_len ), pad, move_len );
  736. memset ( pad, 0, pad_len );
  737. } else if ( word != end ) {
  738. DBG ( "IPv6 underlength address \"%s\"\n", string );
  739. return -EINVAL;
  740. }
  741. return 0;
  742. }
  743. /**
  744. * Convert IPv6 address to standard notation
  745. *
  746. * @v in IPv6 address
  747. * @ret string IPv6 address string in canonical format
  748. *
  749. * RFC5952 defines the canonical format for IPv6 textual representation.
  750. */
  751. char * inet6_ntoa ( const struct in6_addr *in ) {
  752. static char buf[41]; /* ":xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */
  753. char *out = buf;
  754. char *longest_start = NULL;
  755. char *start = NULL;
  756. int longest_len = 1;
  757. int len = 0;
  758. char *dest;
  759. unsigned int i;
  760. uint16_t value;
  761. /* Format address, keeping track of longest run of zeros */
  762. for ( i = 0 ; i < ( sizeof ( in->s6_addr16 ) /
  763. sizeof ( in->s6_addr16[0] ) ) ; i++ ) {
  764. value = ntohs ( in->s6_addr16[i] );
  765. if ( value == 0 ) {
  766. if ( len++ == 0 )
  767. start = out;
  768. if ( len > longest_len ) {
  769. longest_start = start;
  770. longest_len = len;
  771. }
  772. } else {
  773. len = 0;
  774. }
  775. out += sprintf ( out, ":%x", value );
  776. }
  777. /* Abbreviate longest run of zeros, if applicable */
  778. if ( longest_start ) {
  779. dest = strcpy ( ( longest_start + 1 ),
  780. ( longest_start + ( 2 * longest_len ) ) );
  781. if ( dest[0] == '\0' )
  782. dest[1] = '\0';
  783. dest[0] = ':';
  784. }
  785. return ( ( longest_start == buf ) ? buf : ( buf + 1 ) );
  786. }
  787. /**
  788. * Transcribe IPv6 address
  789. *
  790. * @v net_addr IPv6 address
  791. * @ret string IPv6 address in standard notation
  792. *
  793. */
  794. static const char * ipv6_ntoa ( const void *net_addr ) {
  795. return inet6_ntoa ( net_addr );
  796. }
  797. /**
  798. * Transcribe IPv6 socket address
  799. *
  800. * @v sa Socket address
  801. * @ret string Socket address in standard notation
  802. */
  803. static const char * ipv6_sock_ntoa ( struct sockaddr *sa ) {
  804. static char buf[ 39 /* "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */ +
  805. 1 /* "%" */ + NETDEV_NAME_LEN + 1 /* NUL */ ];
  806. struct sockaddr_in6 *sin6 = ( ( struct sockaddr_in6 * ) sa );
  807. struct in6_addr *in = &sin6->sin6_addr;
  808. struct net_device *netdev;
  809. const char *netdev_name;
  810. /* Identify network device, if applicable */
  811. if ( IN6_IS_ADDR_NONGLOBAL ( in ) ) {
  812. netdev = find_netdev_by_index ( sin6->sin6_scope_id );
  813. netdev_name = ( netdev ? netdev->name : "UNKNOWN" );
  814. } else {
  815. netdev_name = NULL;
  816. }
  817. /* Format socket address */
  818. snprintf ( buf, sizeof ( buf ), "%s%s%s", inet6_ntoa ( in ),
  819. ( netdev_name ? "%" : "" ),
  820. ( netdev_name ? netdev_name : "" ) );
  821. return buf;
  822. }
  823. /**
  824. * Parse IPv6 socket address
  825. *
  826. * @v string Socket address string
  827. * @v sa Socket address to fill in
  828. * @ret rc Return status code
  829. */
  830. static int ipv6_sock_aton ( const char *string, struct sockaddr *sa ) {
  831. struct sockaddr_in6 *sin6 = ( ( struct sockaddr_in6 * ) sa );
  832. struct in6_addr in;
  833. struct net_device *netdev;
  834. size_t len;
  835. char *tmp;
  836. char *in_string;
  837. char *netdev_string;
  838. int rc;
  839. /* Create modifiable copy of string */
  840. tmp = strdup ( string );
  841. if ( ! tmp ) {
  842. rc = -ENOMEM;
  843. goto err_alloc;
  844. }
  845. in_string = tmp;
  846. /* Strip surrounding "[...]", if present */
  847. len = strlen ( in_string );
  848. if ( ( in_string[0] == '[' ) && ( in_string[ len - 1 ] == ']' ) ) {
  849. in_string[ len - 1 ] = '\0';
  850. in_string++;
  851. }
  852. /* Split at network device name, if present */
  853. netdev_string = strchr ( in_string, '%' );
  854. if ( netdev_string )
  855. *(netdev_string++) = '\0';
  856. /* Parse IPv6 address portion */
  857. if ( ( rc = inet6_aton ( in_string, &in ) ) != 0 )
  858. goto err_inet6_aton;
  859. /* Parse scope ID, if applicable */
  860. if ( netdev_string ) {
  861. /* Parse explicit network device name, if present */
  862. netdev = find_netdev ( netdev_string );
  863. if ( ! netdev ) {
  864. rc = -ENODEV;
  865. goto err_find_netdev;
  866. }
  867. sin6->sin6_scope_id = netdev->index;
  868. } else if ( IN6_IS_ADDR_NONGLOBAL ( &in ) ) {
  869. /* If no network device is explicitly specified for a
  870. * link-local or multicast address, default to using
  871. * "netX" (if existent).
  872. */
  873. netdev = last_opened_netdev();
  874. if ( netdev )
  875. sin6->sin6_scope_id = netdev->index;
  876. }
  877. /* Copy IPv6 address portion to socket address */
  878. memcpy ( &sin6->sin6_addr, &in, sizeof ( sin6->sin6_addr ) );
  879. err_find_netdev:
  880. err_inet6_aton:
  881. free ( tmp );
  882. err_alloc:
  883. return rc;
  884. }
  885. /** IPv6 protocol */
  886. struct net_protocol ipv6_protocol __net_protocol = {
  887. .name = "IPv6",
  888. .net_proto = htons ( ETH_P_IPV6 ),
  889. .net_addr_len = sizeof ( struct in6_addr ),
  890. .rx = ipv6_rx,
  891. .ntoa = ipv6_ntoa,
  892. };
  893. /** IPv6 TCPIP net protocol */
  894. struct tcpip_net_protocol ipv6_tcpip_protocol __tcpip_net_protocol = {
  895. .name = "IPv6",
  896. .sa_family = AF_INET6,
  897. .header_len = sizeof ( struct ipv6_header ),
  898. .tx = ipv6_tx,
  899. .netdev = ipv6_netdev,
  900. };
  901. /** IPv6 socket address converter */
  902. struct sockaddr_converter ipv6_sockaddr_converter __sockaddr_converter = {
  903. .family = AF_INET6,
  904. .ntoa = ipv6_sock_ntoa,
  905. .aton = ipv6_sock_aton,
  906. };
  907. /**
  908. * Parse IPv6 address setting value
  909. *
  910. * @v type Setting type
  911. * @v value Formatted setting value
  912. * @v buf Buffer to contain raw value
  913. * @v len Length of buffer
  914. * @ret len Length of raw value, or negative error
  915. */
  916. int parse_ipv6_setting ( const struct setting_type *type __unused,
  917. const char *value, void *buf, size_t len ) {
  918. struct in6_addr ipv6;
  919. int rc;
  920. /* Parse IPv6 address */
  921. if ( ( rc = inet6_aton ( value, &ipv6 ) ) != 0 )
  922. return rc;
  923. /* Copy to buffer */
  924. if ( len > sizeof ( ipv6 ) )
  925. len = sizeof ( ipv6 );
  926. memcpy ( buf, &ipv6, len );
  927. return ( sizeof ( ipv6 ) );
  928. }
  929. /**
  930. * Format IPv6 address setting value
  931. *
  932. * @v type Setting type
  933. * @v raw Raw setting value
  934. * @v raw_len Length of raw setting value
  935. * @v buf Buffer to contain formatted value
  936. * @v len Length of buffer
  937. * @ret len Length of formatted value, or negative error
  938. */
  939. int format_ipv6_setting ( const struct setting_type *type __unused,
  940. const void *raw, size_t raw_len, char *buf,
  941. size_t len ) {
  942. const struct in6_addr *ipv6 = raw;
  943. if ( raw_len < sizeof ( *ipv6 ) )
  944. return -EINVAL;
  945. return snprintf ( buf, len, "%s", inet6_ntoa ( ipv6 ) );
  946. }
  947. /**
  948. * Create IPv6 network device
  949. *
  950. * @v netdev Network device
  951. * @ret rc Return status code
  952. */
  953. static int ipv6_probe ( struct net_device *netdev ) {
  954. struct ipv6_miniroute *miniroute;
  955. struct in6_addr address;
  956. int prefix_len;
  957. int rc;
  958. /* Construct link-local address from EUI-64 as per RFC 2464 */
  959. memset ( &address, 0, sizeof ( address ) );
  960. prefix_len = ipv6_link_local ( &address, netdev );
  961. if ( prefix_len < 0 ) {
  962. rc = prefix_len;
  963. DBGC ( netdev, "IPv6 %s could not construct link-local "
  964. "address: %s\n", netdev->name, strerror ( rc ) );
  965. return rc;
  966. }
  967. /* Create link-local address for this network device */
  968. miniroute = ipv6_add_miniroute ( netdev, &address, prefix_len,
  969. IPV6_HAS_ADDRESS );
  970. if ( ! miniroute )
  971. return -ENOMEM;
  972. return 0;
  973. }
  974. /**
  975. * Destroy IPv6 network device
  976. *
  977. * @v netdev Network device
  978. */
  979. static void ipv6_remove ( struct net_device *netdev ) {
  980. struct ipv6_miniroute *miniroute;
  981. struct ipv6_miniroute *tmp;
  982. /* Delete all miniroutes for this network device */
  983. list_for_each_entry_safe ( miniroute, tmp, &ipv6_miniroutes, list ) {
  984. if ( miniroute->netdev == netdev ) {
  985. netdev_put ( miniroute->netdev );
  986. list_del ( &miniroute->list );
  987. free ( miniroute );
  988. }
  989. }
  990. }
  991. /** IPv6 network device driver */
  992. struct net_driver ipv6_driver __net_driver = {
  993. .name = "IPv6",
  994. .probe = ipv6_probe,
  995. .remove = ipv6_remove,
  996. };
  997. /* Drag in objects via ipv6_protocol */
  998. REQUIRING_SYMBOL ( ipv6_protocol );
  999. /* Drag in ICMPv6 */
  1000. REQUIRE_OBJECT ( icmpv6 );
  1001. /* Drag in NDP */
  1002. REQUIRE_OBJECT ( ndp );