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

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