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

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