ipv6.c 27KB

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