選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ipv6.c 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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_LINKLOCAL ( *dest ) ||
  262. IN6_IS_ADDR_MULTICAST ( *dest ) ) {
  263. /* If destination is non-global, and the scope ID
  264. * matches this network device, then use this route.
  265. */
  266. if ( miniroute->netdev->index == scope_id )
  267. return miniroute;
  268. } else {
  269. /* If destination is an on-link global
  270. * address, then use this route.
  271. */
  272. if ( ipv6_is_on_link ( miniroute, *dest ) )
  273. return miniroute;
  274. /* If destination is an off-link global
  275. * address, and we have a default gateway,
  276. * then use this route.
  277. */
  278. if ( miniroute->flags & IPV6_HAS_ROUTER ) {
  279. *dest = &miniroute->router;
  280. return miniroute;
  281. }
  282. }
  283. }
  284. return NULL;
  285. }
  286. /**
  287. * Check that received options can be safely ignored
  288. *
  289. * @v iphdr IPv6 header
  290. * @v options Options extension header
  291. * @v len Maximum length of header
  292. * @ret rc Return status code
  293. */
  294. static int ipv6_check_options ( struct ipv6_header *iphdr,
  295. struct ipv6_options_header *options,
  296. size_t len ) {
  297. struct ipv6_option *option = options->options;
  298. struct ipv6_option *end = ( ( ( void * ) options ) + len );
  299. while ( option < end ) {
  300. if ( ! IPV6_CAN_IGNORE_OPT ( option->type ) ) {
  301. DBGC ( ipv6col ( &iphdr->src ), "IPv6 unrecognised "
  302. "option type %#02x:\n", option->type );
  303. DBGC_HDA ( ipv6col ( &iphdr->src ), 0,
  304. options, len );
  305. return -ENOTSUP_OPT;
  306. }
  307. if ( option->type == IPV6_OPT_PAD1 ) {
  308. option = ( ( ( void * ) option ) + 1 );
  309. } else {
  310. option = ( ( ( void * ) option->value ) + option->len );
  311. }
  312. }
  313. return 0;
  314. }
  315. /**
  316. * Check if fragment matches fragment reassembly buffer
  317. *
  318. * @v fragment Fragment reassembly buffer
  319. * @v iobuf I/O buffer
  320. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  321. * @ret is_fragment Fragment matches this reassembly buffer
  322. */
  323. static int ipv6_is_fragment ( struct fragment *fragment,
  324. struct io_buffer *iobuf, size_t hdrlen ) {
  325. struct ipv6_header *frag_iphdr = fragment->iobuf->data;
  326. struct ipv6_fragment_header *frag_fhdr =
  327. ( fragment->iobuf->data + fragment->hdrlen -
  328. sizeof ( *frag_fhdr ) );
  329. struct ipv6_header *iphdr = iobuf->data;
  330. struct ipv6_fragment_header *fhdr =
  331. ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
  332. return ( ( memcmp ( &iphdr->src, &frag_iphdr->src,
  333. sizeof ( iphdr->src ) ) == 0 ) &&
  334. ( fhdr->ident == frag_fhdr->ident ) );
  335. }
  336. /**
  337. * Get fragment offset
  338. *
  339. * @v iobuf I/O buffer
  340. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  341. * @ret offset Offset
  342. */
  343. static size_t ipv6_fragment_offset ( struct io_buffer *iobuf, size_t hdrlen ) {
  344. struct ipv6_fragment_header *fhdr =
  345. ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
  346. return ( ntohs ( fhdr->offset_more ) & IPV6_MASK_OFFSET );
  347. }
  348. /**
  349. * Check if more fragments exist
  350. *
  351. * @v iobuf I/O buffer
  352. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  353. * @ret more_frags More fragments exist
  354. */
  355. static int ipv6_more_fragments ( struct io_buffer *iobuf, size_t hdrlen ) {
  356. struct ipv6_fragment_header *fhdr =
  357. ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
  358. return ( fhdr->offset_more & htons ( IPV6_MASK_MOREFRAGS ) );
  359. }
  360. /** Fragment reassembler */
  361. static struct fragment_reassembler ipv6_reassembler = {
  362. .list = LIST_HEAD_INIT ( ipv6_reassembler.list ),
  363. .is_fragment = ipv6_is_fragment,
  364. .fragment_offset = ipv6_fragment_offset,
  365. .more_fragments = ipv6_more_fragments,
  366. .stats = &ipv6_stats,
  367. };
  368. /**
  369. * Calculate IPv6 pseudo-header checksum
  370. *
  371. * @v iphdr IPv6 header
  372. * @v len Payload length
  373. * @v next_header Next header type
  374. * @v csum Existing checksum
  375. * @ret csum Updated checksum
  376. */
  377. static uint16_t ipv6_pshdr_chksum ( struct ipv6_header *iphdr, size_t len,
  378. int next_header, uint16_t csum ) {
  379. struct ipv6_pseudo_header pshdr;
  380. /* Build pseudo-header */
  381. memcpy ( &pshdr.src, &iphdr->src, sizeof ( pshdr.src ) );
  382. memcpy ( &pshdr.dest, &iphdr->dest, sizeof ( pshdr.dest ) );
  383. pshdr.len = htonl ( len );
  384. memset ( pshdr.zero, 0, sizeof ( pshdr.zero ) );
  385. pshdr.next_header = next_header;
  386. /* Update the checksum value */
  387. return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
  388. }
  389. /**
  390. * Transmit IPv6 packet
  391. *
  392. * @v iobuf I/O buffer
  393. * @v tcpip Transport-layer protocol
  394. * @v st_src Source network-layer address
  395. * @v st_dest Destination network-layer address
  396. * @v netdev Network device to use if no route found, or NULL
  397. * @v trans_csum Transport-layer checksum to complete, or NULL
  398. * @ret rc Status
  399. *
  400. * This function expects a transport-layer segment and prepends the
  401. * IPv6 header
  402. */
  403. static int ipv6_tx ( struct io_buffer *iobuf,
  404. struct tcpip_protocol *tcpip_protocol,
  405. struct sockaddr_tcpip *st_src,
  406. struct sockaddr_tcpip *st_dest,
  407. struct net_device *netdev,
  408. uint16_t *trans_csum ) {
  409. struct sockaddr_in6 *sin6_src = ( ( struct sockaddr_in6 * ) st_src );
  410. struct sockaddr_in6 *sin6_dest = ( ( struct sockaddr_in6 * ) st_dest );
  411. struct ipv6_miniroute *miniroute;
  412. struct ipv6_header *iphdr;
  413. struct in6_addr *src = NULL;
  414. struct in6_addr *next_hop;
  415. uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
  416. const void *ll_dest;
  417. size_t len;
  418. int rc;
  419. /* Update statistics */
  420. ipv6_stats.out_requests++;
  421. /* Fill up the IPv6 header, except source address */
  422. len = iob_len ( iobuf );
  423. iphdr = iob_push ( iobuf, sizeof ( *iphdr ) );
  424. memset ( iphdr, 0, sizeof ( *iphdr ) );
  425. iphdr->ver_tc_label = htonl ( IPV6_VER );
  426. iphdr->len = htons ( len );
  427. iphdr->next_header = tcpip_protocol->tcpip_proto;
  428. iphdr->hop_limit = IPV6_HOP_LIMIT;
  429. memcpy ( &iphdr->dest, &sin6_dest->sin6_addr, sizeof ( iphdr->dest ) );
  430. /* Use routing table to identify next hop and transmitting netdev */
  431. next_hop = &iphdr->dest;
  432. if ( ( miniroute = ipv6_route ( sin6_dest->sin6_scope_id,
  433. &next_hop ) ) != NULL ) {
  434. src = &miniroute->address;
  435. netdev = miniroute->netdev;
  436. }
  437. if ( ! netdev ) {
  438. DBGC ( ipv6col ( &iphdr->dest ), "IPv6 has no route to %s\n",
  439. inet6_ntoa ( &iphdr->dest ) );
  440. ipv6_stats.out_no_routes++;
  441. rc = -ENETUNREACH;
  442. goto err;
  443. }
  444. if ( sin6_src && ! IN6_IS_ADDR_UNSPECIFIED ( &sin6_src->sin6_addr ) )
  445. src = &sin6_src->sin6_addr;
  446. memcpy ( &iphdr->src, src, sizeof ( iphdr->src ) );
  447. /* Fix up checksums */
  448. if ( trans_csum ) {
  449. *trans_csum = ipv6_pshdr_chksum ( iphdr, len,
  450. tcpip_protocol->tcpip_proto,
  451. *trans_csum );
  452. }
  453. /* Print IPv6 header for debugging */
  454. DBGC2 ( ipv6col ( &iphdr->dest ), "IPv6 TX %s->",
  455. inet6_ntoa ( &iphdr->src ) );
  456. DBGC2 ( ipv6col ( &iphdr->dest ), "%s len %zd next %d\n",
  457. inet6_ntoa ( &iphdr->dest ), len, iphdr->next_header );
  458. /* Calculate link-layer destination address, if possible */
  459. if ( IN6_IS_ADDR_MULTICAST ( next_hop ) ) {
  460. /* Multicast address */
  461. ipv6_stats.out_mcast_pkts++;
  462. if ( ( rc = netdev->ll_protocol->mc_hash ( AF_INET6, next_hop,
  463. ll_dest_buf ) ) !=0){
  464. DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not hash "
  465. "multicast %s: %s\n", inet6_ntoa ( next_hop ),
  466. strerror ( rc ) );
  467. goto err;
  468. }
  469. ll_dest = ll_dest_buf;
  470. } else {
  471. /* Unicast address */
  472. ll_dest = NULL;
  473. }
  474. /* Update statistics */
  475. ipv6_stats.out_transmits++;
  476. ipv6_stats.out_octets += iob_len ( iobuf );
  477. /* Hand off to link layer (via NDP if applicable) */
  478. if ( ll_dest ) {
  479. if ( ( rc = net_tx ( iobuf, netdev, &ipv6_protocol, ll_dest,
  480. netdev->ll_addr ) ) != 0 ) {
  481. DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not "
  482. "transmit packet via %s: %s\n",
  483. netdev->name, strerror ( rc ) );
  484. return rc;
  485. }
  486. } else {
  487. if ( ( rc = ndp_tx ( iobuf, netdev, next_hop, &iphdr->src,
  488. netdev->ll_addr ) ) != 0 ) {
  489. DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not "
  490. "transmit packet via %s: %s\n",
  491. netdev->name, strerror ( rc ) );
  492. return rc;
  493. }
  494. }
  495. return 0;
  496. err:
  497. free_iob ( iobuf );
  498. return rc;
  499. }
  500. /**
  501. * Process incoming IPv6 packets
  502. *
  503. * @v iobuf I/O buffer
  504. * @v netdev Network device
  505. * @v ll_dest Link-layer destination address
  506. * @v ll_source Link-layer destination source
  507. * @v flags Packet flags
  508. * @ret rc Return status code
  509. *
  510. * This function expects an IPv6 network datagram. It processes the
  511. * headers and sends it to the transport layer.
  512. */
  513. static int ipv6_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  514. const void *ll_dest __unused,
  515. const void *ll_source __unused,
  516. unsigned int flags __unused ) {
  517. struct ipv6_header *iphdr = iobuf->data;
  518. union ipv6_extension_header *ext;
  519. union {
  520. struct sockaddr_in6 sin6;
  521. struct sockaddr_tcpip st;
  522. } src, dest;
  523. uint16_t pshdr_csum;
  524. size_t len;
  525. size_t hdrlen;
  526. size_t extlen;
  527. int this_header;
  528. int next_header;
  529. int rc;
  530. /* Update statistics */
  531. ipv6_stats.in_receives++;
  532. ipv6_stats.in_octets += iob_len ( iobuf );
  533. if ( flags & LL_BROADCAST ) {
  534. ipv6_stats.in_bcast_pkts++;
  535. } else if ( flags & LL_MULTICAST ) {
  536. ipv6_stats.in_mcast_pkts++;
  537. }
  538. /* Sanity check the IPv6 header */
  539. if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
  540. DBGC ( ipv6col ( &iphdr->src ), "IPv6 packet too short at %zd "
  541. "bytes (min %zd bytes)\n", iob_len ( iobuf ),
  542. sizeof ( *iphdr ) );
  543. rc = -EINVAL_LEN;
  544. goto err_header;
  545. }
  546. if ( ( iphdr->ver_tc_label & htonl ( IPV6_MASK_VER ) ) !=
  547. htonl ( IPV6_VER ) ) {
  548. DBGC ( ipv6col ( &iphdr->src ), "IPv6 version %#08x not "
  549. "supported\n", ntohl ( iphdr->ver_tc_label ) );
  550. rc = -ENOTSUP_VER;
  551. goto err_header;
  552. }
  553. /* Truncate packet to specified length */
  554. len = ntohs ( iphdr->len );
  555. if ( len > iob_len ( iobuf ) ) {
  556. DBGC ( ipv6col ( &iphdr->src ), "IPv6 length too long at %zd "
  557. "bytes (packet is %zd bytes)\n", len, iob_len ( iobuf ));
  558. ipv6_stats.in_truncated_pkts++;
  559. rc = -EINVAL_LEN;
  560. goto err_other;
  561. }
  562. iob_unput ( iobuf, ( iob_len ( iobuf ) - len - sizeof ( *iphdr ) ) );
  563. hdrlen = sizeof ( *iphdr );
  564. /* Print IPv6 header for debugging */
  565. DBGC2 ( ipv6col ( &iphdr->src ), "IPv6 RX %s<-",
  566. inet6_ntoa ( &iphdr->dest ) );
  567. DBGC2 ( ipv6col ( &iphdr->src ), "%s len %zd next %d\n",
  568. inet6_ntoa ( &iphdr->src ), len, iphdr->next_header );
  569. /* Discard unicast packets not destined for us */
  570. if ( ( ! ( flags & LL_MULTICAST ) ) &&
  571. ( ! ipv6_has_addr ( netdev, &iphdr->dest ) ) ) {
  572. DBGC ( ipv6col ( &iphdr->src ), "IPv6 discarding non-local "
  573. "unicast packet for %s\n", inet6_ntoa ( &iphdr->dest ) );
  574. ipv6_stats.in_addr_errors++;
  575. rc = -EPIPE;
  576. goto err_other;
  577. }
  578. /* Process any extension headers */
  579. next_header = iphdr->next_header;
  580. while ( 1 ) {
  581. /* Extract extension header */
  582. this_header = next_header;
  583. ext = ( iobuf->data + hdrlen );
  584. extlen = sizeof ( ext->pad );
  585. if ( iob_len ( iobuf ) < ( hdrlen + extlen ) ) {
  586. DBGC ( ipv6col ( &iphdr->src ), "IPv6 too short for "
  587. "extension header type %d at %zd bytes (min "
  588. "%zd bytes)\n", this_header,
  589. ( iob_len ( iobuf ) - hdrlen ), extlen );
  590. rc = -EINVAL_LEN;
  591. goto err_header;
  592. }
  593. /* Determine size of extension header (if applicable) */
  594. if ( ( this_header == IPV6_HOPBYHOP ) ||
  595. ( this_header == IPV6_DESTINATION ) ||
  596. ( this_header == IPV6_ROUTING ) ) {
  597. /* Length field is present */
  598. extlen += ext->common.len;
  599. } else if ( this_header == IPV6_FRAGMENT ) {
  600. /* Length field is reserved and ignored (RFC2460) */
  601. } else {
  602. /* Not an extension header; assume rest is payload */
  603. break;
  604. }
  605. if ( iob_len ( iobuf ) < ( hdrlen + extlen ) ) {
  606. DBGC ( ipv6col ( &iphdr->src ), "IPv6 too short for "
  607. "extension header type %d at %zd bytes (min "
  608. "%zd bytes)\n", this_header,
  609. ( iob_len ( iobuf ) - hdrlen ), extlen );
  610. rc = -EINVAL_LEN;
  611. goto err_header;
  612. }
  613. hdrlen += extlen;
  614. next_header = ext->common.next_header;
  615. DBGC2 ( ipv6col ( &iphdr->src ), "IPv6 RX %s<-",
  616. inet6_ntoa ( &iphdr->dest ) );
  617. DBGC2 ( ipv6col ( &iphdr->src ), "%s ext type %d len %zd next "
  618. "%d\n", inet6_ntoa ( &iphdr->src ), this_header,
  619. extlen, next_header );
  620. /* Process this extension header */
  621. if ( ( this_header == IPV6_HOPBYHOP ) ||
  622. ( this_header == IPV6_DESTINATION ) ) {
  623. /* Check that all options can be ignored */
  624. if ( ( rc = ipv6_check_options ( iphdr, &ext->options,
  625. extlen ) ) != 0 )
  626. goto err_header;
  627. } else if ( this_header == IPV6_FRAGMENT ) {
  628. /* Reassemble fragments */
  629. iobuf = fragment_reassemble ( &ipv6_reassembler, iobuf,
  630. &hdrlen );
  631. if ( ! iobuf )
  632. return 0;
  633. iphdr = iobuf->data;
  634. }
  635. }
  636. /* Construct socket address, calculate pseudo-header checksum,
  637. * and hand off to transport layer
  638. */
  639. memset ( &src, 0, sizeof ( src ) );
  640. src.sin6.sin6_family = AF_INET6;
  641. memcpy ( &src.sin6.sin6_addr, &iphdr->src,
  642. sizeof ( src.sin6.sin6_addr ) );
  643. src.sin6.sin6_scope_id = netdev->index;
  644. memset ( &dest, 0, sizeof ( dest ) );
  645. dest.sin6.sin6_family = AF_INET6;
  646. memcpy ( &dest.sin6.sin6_addr, &iphdr->dest,
  647. sizeof ( dest.sin6.sin6_addr ) );
  648. dest.sin6.sin6_scope_id = netdev->index;
  649. iob_pull ( iobuf, hdrlen );
  650. pshdr_csum = ipv6_pshdr_chksum ( iphdr, iob_len ( iobuf ),
  651. next_header, TCPIP_EMPTY_CSUM );
  652. if ( ( rc = tcpip_rx ( iobuf, netdev, next_header, &src.st, &dest.st,
  653. pshdr_csum, &ipv6_stats ) ) != 0 ) {
  654. DBGC ( ipv6col ( &src.sin6.sin6_addr ), "IPv6 received packet "
  655. "rejected by stack: %s\n", strerror ( rc ) );
  656. return rc;
  657. }
  658. return 0;
  659. err_header:
  660. ipv6_stats.in_hdr_errors++;
  661. err_other:
  662. free_iob ( iobuf );
  663. return rc;
  664. }
  665. /**
  666. * Parse IPv6 address
  667. *
  668. * @v string IPv6 address string
  669. * @ret in IPv6 address to fill in
  670. * @ret rc Return status code
  671. */
  672. int inet6_aton ( const char *string, struct in6_addr *in ) {
  673. uint16_t *word = in->s6_addr16;
  674. uint16_t *end = ( word + ( sizeof ( in->s6_addr16 ) /
  675. sizeof ( in->s6_addr16[0] ) ) );
  676. uint16_t *pad = NULL;
  677. const char *nptr = string;
  678. char *endptr;
  679. unsigned long value;
  680. size_t pad_len;
  681. size_t move_len;
  682. /* Parse string */
  683. while ( 1 ) {
  684. /* Parse current word */
  685. value = strtoul ( nptr, &endptr, 16 );
  686. if ( value > 0xffff ) {
  687. DBG ( "IPv6 invalid word value %#lx in \"%s\"\n",
  688. value, string );
  689. return -EINVAL;
  690. }
  691. *(word++) = htons ( value );
  692. /* Parse separator */
  693. if ( ! *endptr )
  694. break;
  695. if ( *endptr != ':' ) {
  696. DBG ( "IPv6 invalid separator '%c' in \"%s\"\n",
  697. *endptr, string );
  698. return -EINVAL;
  699. }
  700. if ( ( endptr == nptr ) && ( nptr != string ) ) {
  701. if ( pad ) {
  702. DBG ( "IPv6 invalid multiple \"::\" in "
  703. "\"%s\"\n", string );
  704. return -EINVAL;
  705. }
  706. pad = word;
  707. }
  708. nptr = ( endptr + 1 );
  709. /* Check for overrun */
  710. if ( word == end ) {
  711. DBG ( "IPv6 too many words in \"%s\"\n", string );
  712. return -EINVAL;
  713. }
  714. }
  715. /* Insert padding if specified */
  716. if ( pad ) {
  717. move_len = ( ( ( void * ) word ) - ( ( void * ) pad ) );
  718. pad_len = ( ( ( void * ) end ) - ( ( void * ) word ) );
  719. memmove ( ( ( ( void * ) pad ) + pad_len ), pad, move_len );
  720. memset ( pad, 0, pad_len );
  721. } else if ( word != end ) {
  722. DBG ( "IPv6 underlength address \"%s\"\n", string );
  723. return -EINVAL;
  724. }
  725. return 0;
  726. }
  727. /**
  728. * Convert IPv6 address to standard notation
  729. *
  730. * @v in IPv6 address
  731. * @ret string IPv6 address string in canonical format
  732. *
  733. * RFC5952 defines the canonical format for IPv6 textual representation.
  734. */
  735. char * inet6_ntoa ( const struct in6_addr *in ) {
  736. static char buf[41]; /* ":xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */
  737. char *out = buf;
  738. char *longest_start = NULL;
  739. char *start = NULL;
  740. int longest_len = 1;
  741. int len = 0;
  742. char *dest;
  743. unsigned int i;
  744. uint16_t value;
  745. /* Format address, keeping track of longest run of zeros */
  746. for ( i = 0 ; i < ( sizeof ( in->s6_addr16 ) /
  747. sizeof ( in->s6_addr16[0] ) ) ; i++ ) {
  748. value = ntohs ( in->s6_addr16[i] );
  749. if ( value == 0 ) {
  750. if ( len++ == 0 )
  751. start = out;
  752. if ( len > longest_len ) {
  753. longest_start = start;
  754. longest_len = len;
  755. }
  756. } else {
  757. len = 0;
  758. }
  759. out += sprintf ( out, ":%x", value );
  760. }
  761. /* Abbreviate longest run of zeros, if applicable */
  762. if ( longest_start ) {
  763. dest = strcpy ( ( longest_start + 1 ),
  764. ( longest_start + ( 2 * longest_len ) ) );
  765. if ( dest[0] == '\0' )
  766. dest[1] = '\0';
  767. dest[0] = ':';
  768. }
  769. return ( ( longest_start == buf ) ? buf : ( buf + 1 ) );
  770. }
  771. /**
  772. * Transcribe IPv6 address
  773. *
  774. * @v net_addr IPv6 address
  775. * @ret string IPv6 address in standard notation
  776. *
  777. */
  778. static const char * ipv6_ntoa ( const void *net_addr ) {
  779. return inet6_ntoa ( net_addr );
  780. }
  781. /**
  782. * Transcribe IPv6 socket address
  783. *
  784. * @v sa Socket address
  785. * @ret string Socket address in standard notation
  786. */
  787. static const char * ipv6_sock_ntoa ( struct sockaddr *sa ) {
  788. static char buf[ 39 /* "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */ +
  789. 1 /* "%" */ + NETDEV_NAME_LEN + 1 /* NUL */ ];
  790. struct sockaddr_in6 *sin6 = ( ( struct sockaddr_in6 * ) sa );
  791. struct in6_addr *in = &sin6->sin6_addr;
  792. struct net_device *netdev;
  793. const char *netdev_name;
  794. /* Identify network device, if applicable */
  795. if ( IN6_IS_ADDR_LINKLOCAL ( in ) ) {
  796. netdev = find_netdev_by_index ( sin6->sin6_scope_id );
  797. netdev_name = ( netdev ? netdev->name : "UNKNOWN" );
  798. } else {
  799. netdev_name = NULL;
  800. }
  801. /* Format socket address */
  802. snprintf ( buf, sizeof ( buf ), "%s%s%s", inet6_ntoa ( in ),
  803. ( netdev_name ? "%" : "" ),
  804. ( netdev_name ? netdev_name : "" ) );
  805. return buf;
  806. }
  807. /**
  808. * Parse IPv6 socket address
  809. *
  810. * @v string Socket address string
  811. * @v sa Socket address to fill in
  812. * @ret rc Return status code
  813. */
  814. static int ipv6_sock_aton ( const char *string, struct sockaddr *sa ) {
  815. struct sockaddr_in6 *sin6 = ( ( struct sockaddr_in6 * ) sa );
  816. struct in6_addr in;
  817. struct net_device *netdev;
  818. size_t len;
  819. char *tmp;
  820. char *in_string;
  821. char *netdev_string;
  822. int rc;
  823. /* Create modifiable copy of string */
  824. tmp = strdup ( string );
  825. if ( ! tmp ) {
  826. rc = -ENOMEM;
  827. goto err_alloc;
  828. }
  829. in_string = tmp;
  830. /* Strip surrounding "[...]", if present */
  831. len = strlen ( in_string );
  832. if ( ( in_string[0] == '[' ) && ( in_string[ len - 1 ] == ']' ) ) {
  833. in_string[ len - 1 ] = '\0';
  834. in_string++;
  835. }
  836. /* Split at network device name, if present */
  837. netdev_string = strchr ( in_string, '%' );
  838. if ( netdev_string )
  839. *(netdev_string++) = '\0';
  840. /* Parse IPv6 address portion */
  841. if ( ( rc = inet6_aton ( in_string, &in ) ) != 0 )
  842. goto err_inet6_aton;
  843. /* Parse network device name, if present */
  844. if ( netdev_string ) {
  845. netdev = find_netdev ( netdev_string );
  846. if ( ! netdev ) {
  847. rc = -ENODEV;
  848. goto err_find_netdev;
  849. }
  850. sin6->sin6_scope_id = netdev->index;
  851. }
  852. /* Copy IPv6 address portion to socket address */
  853. memcpy ( &sin6->sin6_addr, &in, sizeof ( sin6->sin6_addr ) );
  854. err_find_netdev:
  855. err_inet6_aton:
  856. free ( tmp );
  857. err_alloc:
  858. return rc;
  859. }
  860. /** IPv6 protocol */
  861. struct net_protocol ipv6_protocol __net_protocol = {
  862. .name = "IPv6",
  863. .net_proto = htons ( ETH_P_IPV6 ),
  864. .net_addr_len = sizeof ( struct in6_addr ),
  865. .rx = ipv6_rx,
  866. .ntoa = ipv6_ntoa,
  867. };
  868. /** IPv6 TCPIP net protocol */
  869. struct tcpip_net_protocol ipv6_tcpip_protocol __tcpip_net_protocol = {
  870. .name = "IPv6",
  871. .sa_family = AF_INET6,
  872. .tx = ipv6_tx,
  873. };
  874. /** IPv6 socket address converter */
  875. struct sockaddr_converter ipv6_sockaddr_converter __sockaddr_converter = {
  876. .family = AF_INET6,
  877. .ntoa = ipv6_sock_ntoa,
  878. .aton = ipv6_sock_aton,
  879. };
  880. /**
  881. * Parse IPv6 address setting value
  882. *
  883. * @v type Setting type
  884. * @v value Formatted setting value
  885. * @v buf Buffer to contain raw value
  886. * @v len Length of buffer
  887. * @ret len Length of raw value, or negative error
  888. */
  889. int parse_ipv6_setting ( const struct setting_type *type __unused,
  890. const char *value, void *buf, size_t len ) {
  891. struct in6_addr ipv6;
  892. int rc;
  893. /* Parse IPv6 address */
  894. if ( ( rc = inet6_aton ( value, &ipv6 ) ) != 0 )
  895. return rc;
  896. /* Copy to buffer */
  897. if ( len > sizeof ( ipv6 ) )
  898. len = sizeof ( ipv6 );
  899. memcpy ( buf, &ipv6, len );
  900. return ( sizeof ( ipv6 ) );
  901. }
  902. /**
  903. * Format IPv6 address setting value
  904. *
  905. * @v type Setting type
  906. * @v raw Raw setting value
  907. * @v raw_len Length of raw setting value
  908. * @v buf Buffer to contain formatted value
  909. * @v len Length of buffer
  910. * @ret len Length of formatted value, or negative error
  911. */
  912. int format_ipv6_setting ( const struct setting_type *type __unused,
  913. const void *raw, size_t raw_len, char *buf,
  914. size_t len ) {
  915. const struct in6_addr *ipv6 = raw;
  916. if ( raw_len < sizeof ( *ipv6 ) )
  917. return -EINVAL;
  918. return snprintf ( buf, len, "%s", inet6_ntoa ( ipv6 ) );
  919. }
  920. /**
  921. * Create IPv6 network device
  922. *
  923. * @v netdev Network device
  924. * @ret rc Return status code
  925. */
  926. static int ipv6_probe ( struct net_device *netdev ) {
  927. struct ipv6_miniroute *miniroute;
  928. struct in6_addr address;
  929. int prefix_len;
  930. int rc;
  931. /* Construct link-local address from EUI-64 as per RFC 2464 */
  932. memset ( &address, 0, sizeof ( address ) );
  933. prefix_len = ipv6_link_local ( &address, netdev );
  934. if ( prefix_len < 0 ) {
  935. rc = prefix_len;
  936. DBGC ( netdev, "IPv6 %s could not construct link-local "
  937. "address: %s\n", netdev->name, strerror ( rc ) );
  938. return rc;
  939. }
  940. /* Create link-local address for this network device */
  941. miniroute = ipv6_add_miniroute ( netdev, &address, prefix_len,
  942. IPV6_HAS_ADDRESS );
  943. if ( ! miniroute )
  944. return -ENOMEM;
  945. return 0;
  946. }
  947. /**
  948. * Destroy IPv6 network device
  949. *
  950. * @v netdev Network device
  951. */
  952. static void ipv6_remove ( struct net_device *netdev ) {
  953. struct ipv6_miniroute *miniroute;
  954. struct ipv6_miniroute *tmp;
  955. /* Delete all miniroutes for this network device */
  956. list_for_each_entry_safe ( miniroute, tmp, &ipv6_miniroutes, list ) {
  957. if ( miniroute->netdev == netdev ) {
  958. netdev_put ( miniroute->netdev );
  959. list_del ( &miniroute->list );
  960. free ( miniroute );
  961. }
  962. }
  963. }
  964. /** IPv6 network device driver */
  965. struct net_driver ipv6_driver __net_driver = {
  966. .name = "IPv6",
  967. .probe = ipv6_probe,
  968. .remove = ipv6_remove,
  969. };
  970. /* Drag in ICMPv6 */
  971. REQUIRE_OBJECT ( icmpv6 );
  972. /* Drag in NDP */
  973. REQUIRE_OBJECT ( ndp );