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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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 router Router address (if any)
  151. * @ret rc Return status code
  152. */
  153. static int ipv6_add_miniroute ( struct net_device *netdev,
  154. struct in6_addr *address,
  155. unsigned int prefix_len,
  156. struct in6_addr *router ) {
  157. struct ipv6_miniroute *miniroute;
  158. uint8_t *prefix_mask;
  159. unsigned int remaining;
  160. unsigned int i;
  161. /* Find or create routing table entry */
  162. miniroute = ipv6_miniroute ( netdev, address );
  163. if ( ! miniroute ) {
  164. /* Create new routing table entry */
  165. miniroute = zalloc ( sizeof ( *miniroute ) );
  166. if ( ! miniroute )
  167. return -ENOMEM;
  168. miniroute->netdev = netdev_get ( netdev );
  169. memcpy ( &miniroute->address, address,
  170. sizeof ( miniroute->address ) );
  171. /* Default to prefix length of 64 if none specified */
  172. if ( ! prefix_len )
  173. prefix_len = IPV6_DEFAULT_PREFIX_LEN;
  174. miniroute->prefix_len = prefix_len;
  175. assert ( prefix_len <= IPV6_MAX_PREFIX_LEN );
  176. /* Construct prefix mask */
  177. remaining = prefix_len;
  178. for ( prefix_mask = miniroute->prefix_mask.s6_addr ;
  179. remaining >= 8 ; prefix_mask++, remaining -= 8 ) {
  180. *prefix_mask = 0xff;
  181. }
  182. if ( remaining )
  183. *prefix_mask <<= ( 8 - remaining );
  184. /* Add to list of routes */
  185. list_add ( &miniroute->list, &ipv6_miniroutes );
  186. }
  187. /* Set or update address, if applicable */
  188. for ( i = 0 ; i < ( sizeof ( address->s6_addr32 ) /
  189. sizeof ( address->s6_addr32[0] ) ) ; i++ ) {
  190. if ( ( address->s6_addr32[i] &
  191. ~miniroute->prefix_mask.s6_addr32[i] ) != 0 ) {
  192. memcpy ( &miniroute->address, address,
  193. sizeof ( miniroute->address ) );
  194. miniroute->flags |= IPV6_HAS_ADDRESS;
  195. }
  196. }
  197. if ( miniroute->prefix_len == IPV6_MAX_PREFIX_LEN )
  198. miniroute->flags |= IPV6_HAS_ADDRESS;
  199. /* Set or update router, if applicable */
  200. if ( router ) {
  201. memcpy ( &miniroute->router, router,
  202. sizeof ( miniroute->router ) );
  203. miniroute->flags |= IPV6_HAS_ROUTER;
  204. list_del ( &miniroute->list );
  205. list_add_tail ( &miniroute->list, &ipv6_miniroutes );
  206. }
  207. ipv6_dump_miniroute ( miniroute );
  208. return 0;
  209. }
  210. /**
  211. * Delete IPv6 minirouting table entry
  212. *
  213. * @v miniroute Routing table entry
  214. */
  215. static void ipv6_del_miniroute ( struct ipv6_miniroute *miniroute ) {
  216. netdev_put ( miniroute->netdev );
  217. list_del ( &miniroute->list );
  218. free ( miniroute );
  219. }
  220. /**
  221. * Perform IPv6 routing
  222. *
  223. * @v scope_id Destination address scope ID (for link-local addresses)
  224. * @v dest Final destination address
  225. * @ret dest Next hop destination address
  226. * @ret miniroute Routing table entry to use, or NULL if no route
  227. */
  228. static struct ipv6_miniroute * ipv6_route ( unsigned int scope_id,
  229. struct in6_addr **dest ) {
  230. struct ipv6_miniroute *miniroute;
  231. /* Find first usable route in routing table */
  232. list_for_each_entry ( miniroute, &ipv6_miniroutes, list ) {
  233. /* Skip closed network devices */
  234. if ( ! netdev_is_open ( miniroute->netdev ) )
  235. continue;
  236. /* Skip routing table entries with no usable source address */
  237. if ( ! ( miniroute->flags & IPV6_HAS_ADDRESS ) )
  238. continue;
  239. if ( IN6_IS_ADDR_NONGLOBAL ( *dest ) ) {
  240. /* If destination is non-global, and the scope ID
  241. * matches this network device, then use this route.
  242. */
  243. if ( miniroute->netdev->index == scope_id )
  244. return miniroute;
  245. } else {
  246. /* If destination is an on-link global
  247. * address, then use this route.
  248. */
  249. if ( ipv6_is_on_link ( miniroute, *dest ) )
  250. return miniroute;
  251. /* If destination is an off-link global
  252. * address, and we have a default gateway,
  253. * then use this route.
  254. */
  255. if ( miniroute->flags & IPV6_HAS_ROUTER ) {
  256. *dest = &miniroute->router;
  257. return miniroute;
  258. }
  259. }
  260. }
  261. return NULL;
  262. }
  263. /**
  264. * Determine transmitting network device
  265. *
  266. * @v st_dest Destination network-layer address
  267. * @ret netdev Transmitting network device, or NULL
  268. */
  269. static struct net_device * ipv6_netdev ( struct sockaddr_tcpip *st_dest ) {
  270. struct sockaddr_in6 *sin6_dest = ( ( struct sockaddr_in6 * ) st_dest );
  271. struct in6_addr *dest = &sin6_dest->sin6_addr;
  272. struct ipv6_miniroute *miniroute;
  273. /* Find routing table entry */
  274. miniroute = ipv6_route ( sin6_dest->sin6_scope_id, &dest );
  275. if ( ! miniroute )
  276. return NULL;
  277. return miniroute->netdev;
  278. }
  279. /**
  280. * Check that received options can be safely ignored
  281. *
  282. * @v iphdr IPv6 header
  283. * @v options Options extension header
  284. * @v len Maximum length of header
  285. * @ret rc Return status code
  286. */
  287. static int ipv6_check_options ( struct ipv6_header *iphdr,
  288. struct ipv6_options_header *options,
  289. size_t len ) {
  290. struct ipv6_option *option = options->options;
  291. struct ipv6_option *end = ( ( ( void * ) options ) + len );
  292. while ( option < end ) {
  293. if ( ! IPV6_CAN_IGNORE_OPT ( option->type ) ) {
  294. DBGC ( ipv6col ( &iphdr->src ), "IPv6 unrecognised "
  295. "option type %#02x:\n", option->type );
  296. DBGC_HDA ( ipv6col ( &iphdr->src ), 0,
  297. options, len );
  298. return -ENOTSUP_OPT;
  299. }
  300. if ( option->type == IPV6_OPT_PAD1 ) {
  301. option = ( ( ( void * ) option ) + 1 );
  302. } else {
  303. option = ( ( ( void * ) option->value ) + option->len );
  304. }
  305. }
  306. return 0;
  307. }
  308. /**
  309. * Check if fragment matches fragment reassembly buffer
  310. *
  311. * @v fragment Fragment reassembly buffer
  312. * @v iobuf I/O buffer
  313. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  314. * @ret is_fragment Fragment matches this reassembly buffer
  315. */
  316. static int ipv6_is_fragment ( struct fragment *fragment,
  317. struct io_buffer *iobuf, size_t hdrlen ) {
  318. struct ipv6_header *frag_iphdr = fragment->iobuf->data;
  319. struct ipv6_fragment_header *frag_fhdr =
  320. ( fragment->iobuf->data + fragment->hdrlen -
  321. sizeof ( *frag_fhdr ) );
  322. struct ipv6_header *iphdr = iobuf->data;
  323. struct ipv6_fragment_header *fhdr =
  324. ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
  325. return ( ( memcmp ( &iphdr->src, &frag_iphdr->src,
  326. sizeof ( iphdr->src ) ) == 0 ) &&
  327. ( fhdr->ident == frag_fhdr->ident ) );
  328. }
  329. /**
  330. * Get fragment offset
  331. *
  332. * @v iobuf I/O buffer
  333. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  334. * @ret offset Offset
  335. */
  336. static size_t ipv6_fragment_offset ( struct io_buffer *iobuf, size_t hdrlen ) {
  337. struct ipv6_fragment_header *fhdr =
  338. ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
  339. return ( ntohs ( fhdr->offset_more ) & IPV6_MASK_OFFSET );
  340. }
  341. /**
  342. * Check if more fragments exist
  343. *
  344. * @v iobuf I/O buffer
  345. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  346. * @ret more_frags More fragments exist
  347. */
  348. static int ipv6_more_fragments ( struct io_buffer *iobuf, size_t hdrlen ) {
  349. struct ipv6_fragment_header *fhdr =
  350. ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
  351. return ( fhdr->offset_more & htons ( IPV6_MASK_MOREFRAGS ) );
  352. }
  353. /** Fragment reassembler */
  354. static struct fragment_reassembler ipv6_reassembler = {
  355. .list = LIST_HEAD_INIT ( ipv6_reassembler.list ),
  356. .is_fragment = ipv6_is_fragment,
  357. .fragment_offset = ipv6_fragment_offset,
  358. .more_fragments = ipv6_more_fragments,
  359. .stats = &ipv6_stats,
  360. };
  361. /**
  362. * Calculate IPv6 pseudo-header checksum
  363. *
  364. * @v iphdr IPv6 header
  365. * @v len Payload length
  366. * @v next_header Next header type
  367. * @v csum Existing checksum
  368. * @ret csum Updated checksum
  369. */
  370. static uint16_t ipv6_pshdr_chksum ( struct ipv6_header *iphdr, size_t len,
  371. int next_header, uint16_t csum ) {
  372. struct ipv6_pseudo_header pshdr;
  373. /* Build pseudo-header */
  374. memcpy ( &pshdr.src, &iphdr->src, sizeof ( pshdr.src ) );
  375. memcpy ( &pshdr.dest, &iphdr->dest, sizeof ( pshdr.dest ) );
  376. pshdr.len = htonl ( len );
  377. memset ( pshdr.zero, 0, sizeof ( pshdr.zero ) );
  378. pshdr.next_header = next_header;
  379. /* Update the checksum value */
  380. return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
  381. }
  382. /**
  383. * Transmit IPv6 packet
  384. *
  385. * @v iobuf I/O buffer
  386. * @v tcpip Transport-layer protocol
  387. * @v st_src Source network-layer address
  388. * @v st_dest Destination network-layer address
  389. * @v netdev Network device to use if no route found, or NULL
  390. * @v trans_csum Transport-layer checksum to complete, or NULL
  391. * @ret rc Status
  392. *
  393. * This function expects a transport-layer segment and prepends the
  394. * IPv6 header
  395. */
  396. static int ipv6_tx ( struct io_buffer *iobuf,
  397. struct tcpip_protocol *tcpip_protocol,
  398. struct sockaddr_tcpip *st_src,
  399. struct sockaddr_tcpip *st_dest,
  400. struct net_device *netdev,
  401. uint16_t *trans_csum ) {
  402. struct sockaddr_in6 *sin6_src = ( ( struct sockaddr_in6 * ) st_src );
  403. struct sockaddr_in6 *sin6_dest = ( ( struct sockaddr_in6 * ) st_dest );
  404. struct ipv6_miniroute *miniroute;
  405. struct ipv6_header *iphdr;
  406. struct in6_addr *src = NULL;
  407. struct in6_addr *next_hop;
  408. uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
  409. const void *ll_dest;
  410. size_t len;
  411. int rc;
  412. /* Update statistics */
  413. ipv6_stats.out_requests++;
  414. /* Fill up the IPv6 header, except source address */
  415. len = iob_len ( iobuf );
  416. iphdr = iob_push ( iobuf, sizeof ( *iphdr ) );
  417. memset ( iphdr, 0, sizeof ( *iphdr ) );
  418. iphdr->ver_tc_label = htonl ( IPV6_VER );
  419. iphdr->len = htons ( len );
  420. iphdr->next_header = tcpip_protocol->tcpip_proto;
  421. iphdr->hop_limit = IPV6_HOP_LIMIT;
  422. memcpy ( &iphdr->dest, &sin6_dest->sin6_addr, sizeof ( iphdr->dest ) );
  423. /* Use routing table to identify next hop and transmitting netdev */
  424. next_hop = &iphdr->dest;
  425. if ( ( miniroute = ipv6_route ( sin6_dest->sin6_scope_id,
  426. &next_hop ) ) != NULL ) {
  427. src = &miniroute->address;
  428. netdev = miniroute->netdev;
  429. }
  430. if ( ! netdev ) {
  431. DBGC ( ipv6col ( &iphdr->dest ), "IPv6 has no route to %s\n",
  432. inet6_ntoa ( &iphdr->dest ) );
  433. ipv6_stats.out_no_routes++;
  434. rc = -ENETUNREACH;
  435. goto err;
  436. }
  437. if ( sin6_src && ! IN6_IS_ADDR_UNSPECIFIED ( &sin6_src->sin6_addr ) )
  438. src = &sin6_src->sin6_addr;
  439. if ( src )
  440. memcpy ( &iphdr->src, src, sizeof ( iphdr->src ) );
  441. /* Fix up checksums */
  442. if ( trans_csum ) {
  443. *trans_csum = ipv6_pshdr_chksum ( iphdr, len,
  444. tcpip_protocol->tcpip_proto,
  445. *trans_csum );
  446. if ( ! *trans_csum )
  447. *trans_csum = tcpip_protocol->zero_csum;
  448. }
  449. /* Print IPv6 header for debugging */
  450. DBGC2 ( ipv6col ( &iphdr->dest ), "IPv6 TX %s->",
  451. inet6_ntoa ( &iphdr->src ) );
  452. DBGC2 ( ipv6col ( &iphdr->dest ), "%s len %zd next %d\n",
  453. inet6_ntoa ( &iphdr->dest ), len, iphdr->next_header );
  454. /* Calculate link-layer destination address, if possible */
  455. if ( IN6_IS_ADDR_MULTICAST ( next_hop ) ) {
  456. /* Multicast address */
  457. ipv6_stats.out_mcast_pkts++;
  458. if ( ( rc = netdev->ll_protocol->mc_hash ( AF_INET6, next_hop,
  459. ll_dest_buf ) ) !=0){
  460. DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not hash "
  461. "multicast %s: %s\n", inet6_ntoa ( next_hop ),
  462. strerror ( rc ) );
  463. goto err;
  464. }
  465. ll_dest = ll_dest_buf;
  466. } else {
  467. /* Unicast address */
  468. ll_dest = NULL;
  469. }
  470. /* Update statistics */
  471. ipv6_stats.out_transmits++;
  472. ipv6_stats.out_octets += iob_len ( iobuf );
  473. /* Hand off to link layer (via NDP if applicable) */
  474. if ( ll_dest ) {
  475. if ( ( rc = net_tx ( iobuf, netdev, &ipv6_protocol, ll_dest,
  476. netdev->ll_addr ) ) != 0 ) {
  477. DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not "
  478. "transmit packet via %s: %s\n",
  479. netdev->name, strerror ( rc ) );
  480. return rc;
  481. }
  482. } else {
  483. if ( ( rc = ndp_tx ( iobuf, netdev, next_hop, &iphdr->src,
  484. netdev->ll_addr ) ) != 0 ) {
  485. DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not "
  486. "transmit packet via %s: %s\n",
  487. netdev->name, strerror ( rc ) );
  488. return rc;
  489. }
  490. }
  491. return 0;
  492. err:
  493. free_iob ( iobuf );
  494. return rc;
  495. }
  496. /**
  497. * Process incoming IPv6 packets
  498. *
  499. * @v iobuf I/O buffer
  500. * @v netdev Network device
  501. * @v ll_dest Link-layer destination address
  502. * @v ll_source Link-layer destination source
  503. * @v flags Packet flags
  504. * @ret rc Return status code
  505. *
  506. * This function expects an IPv6 network datagram. It processes the
  507. * headers and sends it to the transport layer.
  508. */
  509. static int ipv6_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  510. const void *ll_dest __unused,
  511. const void *ll_source __unused,
  512. unsigned int flags __unused ) {
  513. struct ipv6_header *iphdr = iobuf->data;
  514. union ipv6_extension_header *ext;
  515. union {
  516. struct sockaddr_in6 sin6;
  517. struct sockaddr_tcpip st;
  518. } src, dest;
  519. uint16_t pshdr_csum;
  520. size_t len;
  521. size_t hdrlen;
  522. size_t extlen;
  523. int this_header;
  524. int next_header;
  525. int rc;
  526. /* Update statistics */
  527. ipv6_stats.in_receives++;
  528. ipv6_stats.in_octets += iob_len ( iobuf );
  529. if ( flags & LL_BROADCAST ) {
  530. ipv6_stats.in_bcast_pkts++;
  531. } else if ( flags & LL_MULTICAST ) {
  532. ipv6_stats.in_mcast_pkts++;
  533. }
  534. /* Sanity check the IPv6 header */
  535. if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
  536. DBGC ( ipv6col ( &iphdr->src ), "IPv6 packet too short at %zd "
  537. "bytes (min %zd bytes)\n", iob_len ( iobuf ),
  538. sizeof ( *iphdr ) );
  539. rc = -EINVAL_LEN;
  540. goto err_header;
  541. }
  542. if ( ( iphdr->ver_tc_label & htonl ( IPV6_MASK_VER ) ) !=
  543. htonl ( IPV6_VER ) ) {
  544. DBGC ( ipv6col ( &iphdr->src ), "IPv6 version %#08x not "
  545. "supported\n", ntohl ( iphdr->ver_tc_label ) );
  546. rc = -ENOTSUP_VER;
  547. goto err_header;
  548. }
  549. /* Truncate packet to specified length */
  550. len = ntohs ( iphdr->len );
  551. if ( len > iob_len ( iobuf ) ) {
  552. DBGC ( ipv6col ( &iphdr->src ), "IPv6 length too long at %zd "
  553. "bytes (packet is %zd bytes)\n", len, iob_len ( iobuf ));
  554. ipv6_stats.in_truncated_pkts++;
  555. rc = -EINVAL_LEN;
  556. goto err_other;
  557. }
  558. iob_unput ( iobuf, ( iob_len ( iobuf ) - len - sizeof ( *iphdr ) ) );
  559. hdrlen = sizeof ( *iphdr );
  560. /* Print IPv6 header for debugging */
  561. DBGC2 ( ipv6col ( &iphdr->src ), "IPv6 RX %s<-",
  562. inet6_ntoa ( &iphdr->dest ) );
  563. DBGC2 ( ipv6col ( &iphdr->src ), "%s len %zd next %d\n",
  564. inet6_ntoa ( &iphdr->src ), len, iphdr->next_header );
  565. /* Discard unicast packets not destined for us */
  566. if ( ( ! ( flags & LL_MULTICAST ) ) &&
  567. ( ! ipv6_has_addr ( netdev, &iphdr->dest ) ) ) {
  568. DBGC ( ipv6col ( &iphdr->src ), "IPv6 discarding non-local "
  569. "unicast packet for %s\n", inet6_ntoa ( &iphdr->dest ) );
  570. ipv6_stats.in_addr_errors++;
  571. rc = -EPIPE;
  572. goto err_other;
  573. }
  574. /* Process any extension headers */
  575. next_header = iphdr->next_header;
  576. while ( 1 ) {
  577. /* Extract extension header */
  578. this_header = next_header;
  579. ext = ( iobuf->data + hdrlen );
  580. extlen = sizeof ( ext->pad );
  581. if ( iob_len ( iobuf ) < ( hdrlen + extlen ) ) {
  582. DBGC ( ipv6col ( &iphdr->src ), "IPv6 too short for "
  583. "extension header type %d at %zd bytes (min "
  584. "%zd bytes)\n", this_header,
  585. ( iob_len ( iobuf ) - hdrlen ), extlen );
  586. rc = -EINVAL_LEN;
  587. goto err_header;
  588. }
  589. /* Determine size of extension header (if applicable) */
  590. if ( ( this_header == IPV6_HOPBYHOP ) ||
  591. ( this_header == IPV6_DESTINATION ) ||
  592. ( this_header == IPV6_ROUTING ) ) {
  593. /* Length field is present */
  594. extlen += ext->common.len;
  595. } else if ( this_header == IPV6_FRAGMENT ) {
  596. /* Length field is reserved and ignored (RFC2460) */
  597. } else {
  598. /* Not an extension header; assume rest is payload */
  599. break;
  600. }
  601. if ( iob_len ( iobuf ) < ( hdrlen + extlen ) ) {
  602. DBGC ( ipv6col ( &iphdr->src ), "IPv6 too short for "
  603. "extension header type %d at %zd bytes (min "
  604. "%zd bytes)\n", this_header,
  605. ( iob_len ( iobuf ) - hdrlen ), extlen );
  606. rc = -EINVAL_LEN;
  607. goto err_header;
  608. }
  609. hdrlen += extlen;
  610. next_header = ext->common.next_header;
  611. DBGC2 ( ipv6col ( &iphdr->src ), "IPv6 RX %s<-",
  612. inet6_ntoa ( &iphdr->dest ) );
  613. DBGC2 ( ipv6col ( &iphdr->src ), "%s ext type %d len %zd next "
  614. "%d\n", inet6_ntoa ( &iphdr->src ), this_header,
  615. extlen, next_header );
  616. /* Process this extension header */
  617. if ( ( this_header == IPV6_HOPBYHOP ) ||
  618. ( this_header == IPV6_DESTINATION ) ) {
  619. /* Check that all options can be ignored */
  620. if ( ( rc = ipv6_check_options ( iphdr, &ext->options,
  621. extlen ) ) != 0 )
  622. goto err_header;
  623. } else if ( this_header == IPV6_FRAGMENT ) {
  624. /* Reassemble fragments */
  625. iobuf = fragment_reassemble ( &ipv6_reassembler, iobuf,
  626. &hdrlen );
  627. if ( ! iobuf )
  628. return 0;
  629. iphdr = iobuf->data;
  630. }
  631. }
  632. /* Construct socket address, calculate pseudo-header checksum,
  633. * and hand off to transport layer
  634. */
  635. memset ( &src, 0, sizeof ( src ) );
  636. src.sin6.sin6_family = AF_INET6;
  637. memcpy ( &src.sin6.sin6_addr, &iphdr->src,
  638. sizeof ( src.sin6.sin6_addr ) );
  639. src.sin6.sin6_scope_id = netdev->index;
  640. memset ( &dest, 0, sizeof ( dest ) );
  641. dest.sin6.sin6_family = AF_INET6;
  642. memcpy ( &dest.sin6.sin6_addr, &iphdr->dest,
  643. sizeof ( dest.sin6.sin6_addr ) );
  644. dest.sin6.sin6_scope_id = netdev->index;
  645. iob_pull ( iobuf, hdrlen );
  646. pshdr_csum = ipv6_pshdr_chksum ( iphdr, iob_len ( iobuf ),
  647. next_header, TCPIP_EMPTY_CSUM );
  648. if ( ( rc = tcpip_rx ( iobuf, netdev, next_header, &src.st, &dest.st,
  649. pshdr_csum, &ipv6_stats ) ) != 0 ) {
  650. DBGC ( ipv6col ( &src.sin6.sin6_addr ), "IPv6 received packet "
  651. "rejected by stack: %s\n", strerror ( rc ) );
  652. return rc;
  653. }
  654. return 0;
  655. err_header:
  656. ipv6_stats.in_hdr_errors++;
  657. err_other:
  658. free_iob ( iobuf );
  659. return rc;
  660. }
  661. /**
  662. * Parse IPv6 address
  663. *
  664. * @v string IPv6 address string
  665. * @ret in IPv6 address to fill in
  666. * @ret rc Return status code
  667. */
  668. int inet6_aton ( const char *string, struct in6_addr *in ) {
  669. uint16_t *word = in->s6_addr16;
  670. uint16_t *end = ( word + ( sizeof ( in->s6_addr16 ) /
  671. sizeof ( in->s6_addr16[0] ) ) );
  672. uint16_t *pad = NULL;
  673. const char *nptr = string;
  674. char *endptr;
  675. unsigned long value;
  676. size_t pad_len;
  677. size_t move_len;
  678. /* Parse string */
  679. while ( 1 ) {
  680. /* Parse current word */
  681. value = strtoul ( nptr, &endptr, 16 );
  682. if ( value > 0xffff ) {
  683. DBG ( "IPv6 invalid word value %#lx in \"%s\"\n",
  684. value, string );
  685. return -EINVAL;
  686. }
  687. *(word++) = htons ( value );
  688. /* Parse separator */
  689. if ( ! *endptr )
  690. break;
  691. if ( *endptr != ':' ) {
  692. DBG ( "IPv6 invalid separator '%c' in \"%s\"\n",
  693. *endptr, string );
  694. return -EINVAL;
  695. }
  696. if ( ( endptr == nptr ) && ( nptr != string ) ) {
  697. if ( pad ) {
  698. DBG ( "IPv6 invalid multiple \"::\" in "
  699. "\"%s\"\n", string );
  700. return -EINVAL;
  701. }
  702. pad = word;
  703. }
  704. nptr = ( endptr + 1 );
  705. /* Check for overrun */
  706. if ( word == end ) {
  707. DBG ( "IPv6 too many words in \"%s\"\n", string );
  708. return -EINVAL;
  709. }
  710. }
  711. /* Insert padding if specified */
  712. if ( pad ) {
  713. move_len = ( ( ( void * ) word ) - ( ( void * ) pad ) );
  714. pad_len = ( ( ( void * ) end ) - ( ( void * ) word ) );
  715. memmove ( ( ( ( void * ) pad ) + pad_len ), pad, move_len );
  716. memset ( pad, 0, pad_len );
  717. } else if ( word != end ) {
  718. DBG ( "IPv6 underlength address \"%s\"\n", string );
  719. return -EINVAL;
  720. }
  721. return 0;
  722. }
  723. /**
  724. * Convert IPv6 address to standard notation
  725. *
  726. * @v in IPv6 address
  727. * @ret string IPv6 address string in canonical format
  728. *
  729. * RFC5952 defines the canonical format for IPv6 textual representation.
  730. */
  731. char * inet6_ntoa ( const struct in6_addr *in ) {
  732. static char buf[41]; /* ":xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */
  733. char *out = buf;
  734. char *longest_start = NULL;
  735. char *start = NULL;
  736. int longest_len = 1;
  737. int len = 0;
  738. char *dest;
  739. unsigned int i;
  740. uint16_t value;
  741. /* Format address, keeping track of longest run of zeros */
  742. for ( i = 0 ; i < ( sizeof ( in->s6_addr16 ) /
  743. sizeof ( in->s6_addr16[0] ) ) ; i++ ) {
  744. value = ntohs ( in->s6_addr16[i] );
  745. if ( value == 0 ) {
  746. if ( len++ == 0 )
  747. start = out;
  748. if ( len > longest_len ) {
  749. longest_start = start;
  750. longest_len = len;
  751. }
  752. } else {
  753. len = 0;
  754. }
  755. out += sprintf ( out, ":%x", value );
  756. }
  757. /* Abbreviate longest run of zeros, if applicable */
  758. if ( longest_start ) {
  759. dest = strcpy ( ( longest_start + 1 ),
  760. ( longest_start + ( 2 * longest_len ) ) );
  761. if ( dest[0] == '\0' )
  762. dest[1] = '\0';
  763. dest[0] = ':';
  764. }
  765. return ( ( longest_start == buf ) ? buf : ( buf + 1 ) );
  766. }
  767. /**
  768. * Transcribe IPv6 address
  769. *
  770. * @v net_addr IPv6 address
  771. * @ret string IPv6 address in standard notation
  772. *
  773. */
  774. static const char * ipv6_ntoa ( const void *net_addr ) {
  775. return inet6_ntoa ( net_addr );
  776. }
  777. /**
  778. * Transcribe IPv6 socket address
  779. *
  780. * @v sa Socket address
  781. * @ret string Socket address in standard notation
  782. */
  783. static const char * ipv6_sock_ntoa ( struct sockaddr *sa ) {
  784. static char buf[ 39 /* "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */ +
  785. 1 /* "%" */ + NETDEV_NAME_LEN + 1 /* NUL */ ];
  786. struct sockaddr_in6 *sin6 = ( ( struct sockaddr_in6 * ) sa );
  787. struct in6_addr *in = &sin6->sin6_addr;
  788. struct net_device *netdev;
  789. const char *netdev_name;
  790. /* Identify network device, if applicable */
  791. if ( IN6_IS_ADDR_NONGLOBAL ( in ) ) {
  792. netdev = find_netdev_by_index ( sin6->sin6_scope_id );
  793. netdev_name = ( netdev ? netdev->name : "UNKNOWN" );
  794. } else {
  795. netdev_name = NULL;
  796. }
  797. /* Format socket address */
  798. snprintf ( buf, sizeof ( buf ), "%s%s%s", inet6_ntoa ( in ),
  799. ( netdev_name ? "%" : "" ),
  800. ( netdev_name ? netdev_name : "" ) );
  801. return buf;
  802. }
  803. /**
  804. * Parse IPv6 socket address
  805. *
  806. * @v string Socket address string
  807. * @v sa Socket address to fill in
  808. * @ret rc Return status code
  809. */
  810. static int ipv6_sock_aton ( const char *string, struct sockaddr *sa ) {
  811. struct sockaddr_in6 *sin6 = ( ( struct sockaddr_in6 * ) sa );
  812. struct in6_addr in;
  813. struct net_device *netdev;
  814. size_t len;
  815. char *tmp;
  816. char *in_string;
  817. char *netdev_string;
  818. int rc;
  819. /* Create modifiable copy of string */
  820. tmp = strdup ( string );
  821. if ( ! tmp ) {
  822. rc = -ENOMEM;
  823. goto err_alloc;
  824. }
  825. in_string = tmp;
  826. /* Strip surrounding "[...]", if present */
  827. len = strlen ( in_string );
  828. if ( ( in_string[0] == '[' ) && ( in_string[ len - 1 ] == ']' ) ) {
  829. in_string[ len - 1 ] = '\0';
  830. in_string++;
  831. }
  832. /* Split at network device name, if present */
  833. netdev_string = strchr ( in_string, '%' );
  834. if ( netdev_string )
  835. *(netdev_string++) = '\0';
  836. /* Parse IPv6 address portion */
  837. if ( ( rc = inet6_aton ( in_string, &in ) ) != 0 )
  838. goto err_inet6_aton;
  839. /* Parse scope ID, if applicable */
  840. if ( netdev_string ) {
  841. /* Parse explicit network device name, if present */
  842. netdev = find_netdev ( netdev_string );
  843. if ( ! netdev ) {
  844. rc = -ENODEV;
  845. goto err_find_netdev;
  846. }
  847. sin6->sin6_scope_id = netdev->index;
  848. } else if ( IN6_IS_ADDR_NONGLOBAL ( &in ) ) {
  849. /* If no network device is explicitly specified for a
  850. * link-local or multicast address, default to using
  851. * "netX" (if existent).
  852. */
  853. netdev = last_opened_netdev();
  854. if ( netdev )
  855. sin6->sin6_scope_id = netdev->index;
  856. }
  857. /* Copy IPv6 address portion to socket address */
  858. memcpy ( &sin6->sin6_addr, &in, sizeof ( sin6->sin6_addr ) );
  859. err_find_netdev:
  860. err_inet6_aton:
  861. free ( tmp );
  862. err_alloc:
  863. return rc;
  864. }
  865. /** IPv6 protocol */
  866. struct net_protocol ipv6_protocol __net_protocol = {
  867. .name = "IPv6",
  868. .net_proto = htons ( ETH_P_IPV6 ),
  869. .net_addr_len = sizeof ( struct in6_addr ),
  870. .rx = ipv6_rx,
  871. .ntoa = ipv6_ntoa,
  872. };
  873. /** IPv6 TCPIP net protocol */
  874. struct tcpip_net_protocol ipv6_tcpip_protocol __tcpip_net_protocol = {
  875. .name = "IPv6",
  876. .sa_family = AF_INET6,
  877. .header_len = sizeof ( struct ipv6_header ),
  878. .net_protocol = &ipv6_protocol,
  879. .tx = ipv6_tx,
  880. .netdev = ipv6_netdev,
  881. };
  882. /** IPv6 socket address converter */
  883. struct sockaddr_converter ipv6_sockaddr_converter __sockaddr_converter = {
  884. .family = AF_INET6,
  885. .ntoa = ipv6_sock_ntoa,
  886. .aton = ipv6_sock_aton,
  887. };
  888. /**
  889. * Parse IPv6 address setting value
  890. *
  891. * @v type Setting type
  892. * @v value Formatted setting value
  893. * @v buf Buffer to contain raw value
  894. * @v len Length of buffer
  895. * @ret len Length of raw value, or negative error
  896. */
  897. int parse_ipv6_setting ( const struct setting_type *type __unused,
  898. const char *value, void *buf, size_t len ) {
  899. struct in6_addr ipv6;
  900. int rc;
  901. /* Parse IPv6 address */
  902. if ( ( rc = inet6_aton ( value, &ipv6 ) ) != 0 )
  903. return rc;
  904. /* Copy to buffer */
  905. if ( len > sizeof ( ipv6 ) )
  906. len = sizeof ( ipv6 );
  907. memcpy ( buf, &ipv6, len );
  908. return ( sizeof ( ipv6 ) );
  909. }
  910. /**
  911. * Format IPv6 address setting value
  912. *
  913. * @v type Setting type
  914. * @v raw Raw setting value
  915. * @v raw_len Length of raw setting value
  916. * @v buf Buffer to contain formatted value
  917. * @v len Length of buffer
  918. * @ret len Length of formatted value, or negative error
  919. */
  920. int format_ipv6_setting ( const struct setting_type *type __unused,
  921. const void *raw, size_t raw_len, char *buf,
  922. size_t len ) {
  923. const struct in6_addr *ipv6 = raw;
  924. if ( raw_len < sizeof ( *ipv6 ) )
  925. return -EINVAL;
  926. return snprintf ( buf, len, "%s", inet6_ntoa ( ipv6 ) );
  927. }
  928. /** IPv6 settings scope */
  929. const struct settings_scope ipv6_settings_scope;
  930. /** IPv6 address setting */
  931. const struct setting ip6_setting __setting ( SETTING_IP6, ip6 ) = {
  932. .name = "ip6",
  933. .description = "IPv6 address",
  934. .type = &setting_type_ipv6,
  935. .scope = &ipv6_settings_scope,
  936. };
  937. /** IPv6 prefix length setting */
  938. const struct setting len6_setting __setting ( SETTING_IP6, len6 ) = {
  939. .name = "len6",
  940. .description = "IPv6 prefix length",
  941. .type = &setting_type_int8,
  942. .scope = &ipv6_settings_scope,
  943. };
  944. /** Default gateway setting */
  945. const struct setting gateway6_setting __setting ( SETTING_IP6, gateway6 ) = {
  946. .name = "gateway6",
  947. .description = "IPv6 gateway",
  948. .type = &setting_type_ipv6,
  949. .scope = &ipv6_settings_scope,
  950. };
  951. /**
  952. * Check applicability of IPv6 link-local address setting
  953. *
  954. * @v settings Settings block
  955. * @v setting Setting to fetch
  956. * @ret applies Setting applies within this settings block
  957. */
  958. static int ipv6_applies ( struct settings *settings __unused,
  959. const struct setting *setting ) {
  960. return ( setting->scope == &ipv6_settings_scope );
  961. }
  962. /**
  963. * Fetch IPv6 link-local address setting
  964. *
  965. * @v settings Settings block
  966. * @v setting Setting to fetch
  967. * @v data Buffer to fill with setting data
  968. * @v len Length of buffer
  969. * @ret len Length of setting data, or negative error
  970. */
  971. static int ipv6_fetch ( struct settings *settings, struct setting *setting,
  972. void *data, size_t len ) {
  973. struct net_device *netdev =
  974. container_of ( settings->parent, struct net_device,
  975. settings.settings );
  976. struct in6_addr ip6;
  977. uint8_t *len6;
  978. int prefix_len;
  979. int rc;
  980. /* Construct link-local address from EUI-64 as per RFC 2464 */
  981. memset ( &ip6, 0, sizeof ( ip6 ) );
  982. prefix_len = ipv6_link_local ( &ip6, netdev );
  983. if ( prefix_len < 0 ) {
  984. rc = prefix_len;
  985. return rc;
  986. }
  987. /* Handle setting */
  988. if ( setting_cmp ( setting, &ip6_setting ) == 0 ) {
  989. /* Return link-local ip6 */
  990. if ( len > sizeof ( ip6 ) )
  991. len = sizeof ( ip6 );
  992. memcpy ( data, &ip6, len );
  993. return sizeof ( ip6 );
  994. } else if ( setting_cmp ( setting, &len6_setting ) == 0 ) {
  995. /* Return prefix length */
  996. if ( len ) {
  997. len6 = data;
  998. *len6 = prefix_len;
  999. }
  1000. return sizeof ( *len6 );
  1001. }
  1002. return -ENOENT;
  1003. }
  1004. /** IPv6 link-local address settings operations */
  1005. static struct settings_operations ipv6_settings_operations = {
  1006. .applies = ipv6_applies,
  1007. .fetch = ipv6_fetch,
  1008. };
  1009. /** IPv6 link-local address settings */
  1010. struct ipv6_settings {
  1011. /** Reference counter */
  1012. struct refcnt refcnt;
  1013. /** Settings interface */
  1014. struct settings settings;
  1015. };
  1016. /**
  1017. * Register IPv6 link-local address settings
  1018. *
  1019. * @v netdev Network device
  1020. * @ret rc Return status code
  1021. */
  1022. static int ipv6_register_settings ( struct net_device *netdev ) {
  1023. struct settings *parent = netdev_settings ( netdev );
  1024. struct ipv6_settings *ipv6set;
  1025. int rc;
  1026. /* Allocate and initialise structure */
  1027. ipv6set = zalloc ( sizeof ( *ipv6set ) );
  1028. if ( ! ipv6set ) {
  1029. rc = -ENOMEM;
  1030. goto err_alloc;
  1031. }
  1032. ref_init ( &ipv6set->refcnt, NULL );
  1033. settings_init ( &ipv6set->settings, &ipv6_settings_operations,
  1034. &ipv6set->refcnt, &ipv6_settings_scope );
  1035. ipv6set->settings.order = IPV6_ORDER_LINK_LOCAL;
  1036. /* Register settings */
  1037. if ( ( rc = register_settings ( &ipv6set->settings, parent,
  1038. IPV6_SETTINGS_NAME ) ) != 0 )
  1039. goto err_register;
  1040. err_register:
  1041. ref_put ( &ipv6set->refcnt );
  1042. err_alloc:
  1043. return rc;
  1044. }
  1045. /** IPv6 network device driver */
  1046. struct net_driver ipv6_driver __net_driver = {
  1047. .name = "IPv6",
  1048. .probe = ipv6_register_settings,
  1049. };
  1050. /**
  1051. * Create IPv6 routing table based on configured settings
  1052. *
  1053. * @v netdev Network device
  1054. * @v settings Settings block
  1055. * @ret rc Return status code
  1056. */
  1057. static int ipv6_create_routes ( struct net_device *netdev,
  1058. struct settings *settings ) {
  1059. struct settings *child;
  1060. struct settings *origin;
  1061. struct in6_addr ip6_buf;
  1062. struct in6_addr gateway6_buf;
  1063. struct in6_addr *ip6 = &ip6_buf;
  1064. struct in6_addr *gateway6 = &gateway6_buf;
  1065. uint8_t len6;
  1066. size_t len;
  1067. int rc;
  1068. /* First, create routing table for any child settings. We do
  1069. * this depth-first and in reverse order so that the end
  1070. * result reflects the relative priorities of the settings
  1071. * blocks.
  1072. */
  1073. list_for_each_entry_reverse ( child, &settings->children, siblings )
  1074. ipv6_create_routes ( netdev, child );
  1075. /* Fetch IPv6 address, if any */
  1076. len = fetch_setting ( settings, &ip6_setting, &origin, NULL,
  1077. ip6, sizeof ( *ip6 ) );
  1078. if ( ( len != sizeof ( *ip6 ) ) || ( origin != settings ) )
  1079. return 0;
  1080. /* Fetch prefix length, if defined */
  1081. len = fetch_setting ( settings, &len6_setting, &origin, NULL,
  1082. &len6, sizeof ( len6 ) );
  1083. if ( ( len != sizeof ( len6 ) ) || ( origin != settings ) )
  1084. len6 = 0;
  1085. if ( len6 > IPV6_MAX_PREFIX_LEN )
  1086. len6 = IPV6_MAX_PREFIX_LEN;
  1087. /* Fetch gateway, if defined */
  1088. len = fetch_setting ( settings, &gateway6_setting, &origin, NULL,
  1089. gateway6, sizeof ( *gateway6 ) );
  1090. if ( ( len != sizeof ( *gateway6 ) ) || ( origin != settings ) )
  1091. gateway6 = NULL;
  1092. /* Create or update route */
  1093. if ( ( rc = ipv6_add_miniroute ( netdev, ip6, len6, gateway6 ) ) != 0){
  1094. DBGC ( netdev, "IPv6 %s could not add route: %s\n",
  1095. netdev->name, strerror ( rc ) );
  1096. return rc;
  1097. }
  1098. return 0;
  1099. }
  1100. /**
  1101. * Create IPv6 routing table based on configured settings
  1102. *
  1103. * @ret rc Return status code
  1104. */
  1105. static int ipv6_create_all_routes ( void ) {
  1106. struct ipv6_miniroute *miniroute;
  1107. struct ipv6_miniroute *tmp;
  1108. struct net_device *netdev;
  1109. struct settings *settings;
  1110. int rc;
  1111. /* Delete all existing routes */
  1112. list_for_each_entry_safe ( miniroute, tmp, &ipv6_miniroutes, list )
  1113. ipv6_del_miniroute ( miniroute );
  1114. /* Create routes for each configured network device */
  1115. for_each_netdev ( netdev ) {
  1116. settings = netdev_settings ( netdev );
  1117. if ( ( rc = ipv6_create_routes ( netdev, settings ) ) != 0 )
  1118. return rc;
  1119. }
  1120. return 0;
  1121. }
  1122. /** IPv6 settings applicator */
  1123. struct settings_applicator ipv6_settings_applicator __settings_applicator = {
  1124. .apply = ipv6_create_all_routes,
  1125. };
  1126. /* Drag in objects via ipv6_protocol */
  1127. REQUIRING_SYMBOL ( ipv6_protocol );
  1128. /* Drag in ICMPv6 */
  1129. REQUIRE_OBJECT ( icmpv6 );
  1130. /* Drag in NDP */
  1131. REQUIRE_OBJECT ( ndp );