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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #include <errno.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <byteswap.h>
  7. #include <gpxe/in.h>
  8. #include <gpxe/ip6.h>
  9. #include <gpxe/ndp.h>
  10. #include <gpxe/list.h>
  11. #include <gpxe/icmp6.h>
  12. #include <gpxe/tcpip.h>
  13. #include <gpxe/socket.h>
  14. #include <gpxe/iobuf.h>
  15. #include <gpxe/netdevice.h>
  16. #include <gpxe/if_ether.h>
  17. struct net_protocol ipv6_protocol;
  18. /* Unspecified IP6 address */
  19. static struct in6_addr ip6_none = {
  20. .in6_u.u6_addr32 = { 0,0,0,0 }
  21. };
  22. /** An IPv6 routing table entry */
  23. struct ipv6_miniroute {
  24. /* List of miniroutes */
  25. struct list_head list;
  26. /* Network device */
  27. struct net_device *netdev;
  28. /* Destination prefix */
  29. struct in6_addr prefix;
  30. /* Prefix length */
  31. int prefix_len;
  32. /* IPv6 address of interface */
  33. struct in6_addr address;
  34. /* Gateway address */
  35. struct in6_addr gateway;
  36. };
  37. /** List of IPv6 miniroutes */
  38. static LIST_HEAD ( miniroutes );
  39. /**
  40. * Add IPv6 minirouting table entry
  41. *
  42. * @v netdev Network device
  43. * @v prefix Destination prefix
  44. * @v address Address of the interface
  45. * @v gateway Gateway address (or ::0 for no gateway)
  46. * @ret miniroute Routing table entry, or NULL
  47. */
  48. static struct ipv6_miniroute * __malloc
  49. add_ipv6_miniroute ( struct net_device *netdev, struct in6_addr prefix,
  50. int prefix_len, struct in6_addr address,
  51. struct in6_addr gateway ) {
  52. struct ipv6_miniroute *miniroute;
  53. miniroute = malloc ( sizeof ( *miniroute ) );
  54. if ( miniroute ) {
  55. /* Record routing information */
  56. miniroute->netdev = netdev_get ( netdev );
  57. miniroute->prefix = prefix;
  58. miniroute->prefix_len = prefix_len;
  59. miniroute->address = address;
  60. miniroute->gateway = gateway;
  61. /* Add miniroute to list of miniroutes */
  62. if ( !IP6_EQUAL ( gateway, ip6_none ) ) {
  63. list_add_tail ( &miniroute->list, &miniroutes );
  64. } else {
  65. list_add ( &miniroute->list, &miniroutes );
  66. }
  67. }
  68. return miniroute;
  69. }
  70. /**
  71. * Delete IPv6 minirouting table entry
  72. *
  73. * @v miniroute Routing table entry
  74. */
  75. static void del_ipv6_miniroute ( struct ipv6_miniroute *miniroute ) {
  76. netdev_put ( miniroute->netdev );
  77. list_del ( &miniroute->list );
  78. free ( miniroute );
  79. }
  80. /**
  81. * Add IPv6 interface
  82. *
  83. * @v netdev Network device
  84. * @v prefix Destination prefix
  85. * @v address Address of the interface
  86. * @v gateway Gateway address (or ::0 for no gateway)
  87. */
  88. int add_ipv6_address ( struct net_device *netdev, struct in6_addr prefix,
  89. int prefix_len, struct in6_addr address,
  90. struct in6_addr gateway ) {
  91. struct ipv6_miniroute *miniroute;
  92. /* Clear any existing address for this net device */
  93. del_ipv6_address ( netdev );
  94. /* Add new miniroute */
  95. miniroute = add_ipv6_miniroute ( netdev, prefix, prefix_len, address,
  96. gateway );
  97. if ( ! miniroute )
  98. return -ENOMEM;
  99. return 0;
  100. }
  101. /**
  102. * Remove IPv6 interface
  103. *
  104. * @v netdev Network device
  105. */
  106. void del_ipv6_address ( struct net_device *netdev ) {
  107. struct ipv6_miniroute *miniroute;
  108. list_for_each_entry ( miniroute, &miniroutes, list ) {
  109. if ( miniroute->netdev == netdev ) {
  110. del_ipv6_miniroute ( miniroute );
  111. break;
  112. }
  113. }
  114. }
  115. /**
  116. * Calculate TCPIP checksum
  117. *
  118. * @v iobuf I/O buffer
  119. * @v tcpip TCP/IP protocol
  120. *
  121. * This function constructs the pseudo header and completes the checksum in the
  122. * upper layer header.
  123. */
  124. static uint16_t ipv6_tx_csum ( struct io_buffer *iobuf, uint16_t csum ) {
  125. struct ip6_header *ip6hdr = iobuf->data;
  126. struct ipv6_pseudo_header pshdr;
  127. /* Calculate pseudo header */
  128. memset ( &pshdr, 0, sizeof ( pshdr ) );
  129. pshdr.src = ip6hdr->src;
  130. pshdr.dest = ip6hdr->dest;
  131. pshdr.len = htons ( iob_len ( iobuf ) - sizeof ( *ip6hdr ) );
  132. pshdr.nxt_hdr = ip6hdr->nxt_hdr;
  133. /* Update checksum value */
  134. return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
  135. }
  136. /**
  137. * Dump IP6 header for debugging
  138. *
  139. * ip6hdr IPv6 header
  140. */
  141. void ipv6_dump ( struct ip6_header *ip6hdr ) {
  142. DBG ( "IP6 %p src %s dest %s nxt_hdr %d len %d\n", ip6hdr,
  143. inet6_ntoa ( ip6hdr->src ), inet6_ntoa ( ip6hdr->dest ),
  144. ip6hdr->nxt_hdr, ntohs ( ip6hdr->payload_len ) );
  145. }
  146. /**
  147. * Transmit IP6 packet
  148. *
  149. * iobuf I/O buffer
  150. * tcpip TCP/IP protocol
  151. * st_dest Destination socket address
  152. *
  153. * This function prepends the IPv6 headers to the payload an transmits it.
  154. */
  155. static int ipv6_tx ( struct io_buffer *iobuf,
  156. struct tcpip_protocol *tcpip,
  157. struct sockaddr_tcpip *st_dest,
  158. struct net_device *netdev,
  159. uint16_t *trans_csum ) {
  160. struct sockaddr_in6 *dest = ( struct sockaddr_in6* ) st_dest;
  161. struct in6_addr next_hop;
  162. struct ipv6_miniroute *miniroute;
  163. uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
  164. const uint8_t *ll_dest = ll_dest_buf;
  165. int rc;
  166. /* Construct the IPv6 packet */
  167. struct ip6_header *ip6hdr = iob_push ( iobuf, sizeof ( *ip6hdr ) );
  168. memset ( ip6hdr, 0, sizeof ( *ip6hdr) );
  169. ip6hdr->ver_traffic_class_flow_label = htonl ( 0x60000000 );//IP6_VERSION;
  170. ip6hdr->payload_len = htons ( iob_len ( iobuf ) - sizeof ( *ip6hdr ) );
  171. ip6hdr->nxt_hdr = tcpip->tcpip_proto;
  172. ip6hdr->hop_limit = IP6_HOP_LIMIT; // 255
  173. /* Determine the next hop address and interface
  174. *
  175. * TODO: Implement the routing table.
  176. */
  177. next_hop = dest->sin6_addr;
  178. list_for_each_entry ( miniroute, &miniroutes, list ) {
  179. if ( ( memcmp ( &ip6hdr->dest, &miniroute->prefix,
  180. miniroute->prefix_len ) == 0 ) ||
  181. ( IP6_EQUAL ( miniroute->gateway, ip6_none ) ) ) {
  182. netdev = miniroute->netdev;
  183. ip6hdr->src = miniroute->address;
  184. if ( ! ( IS_UNSPECIFIED ( miniroute->gateway ) ) ) {
  185. next_hop = miniroute->gateway;
  186. }
  187. break;
  188. }
  189. }
  190. /* No network interface identified */
  191. if ( !netdev ) {
  192. DBG ( "No route to host %s\n", inet6_ntoa ( ip6hdr->dest ) );
  193. rc = -ENETUNREACH;
  194. goto err;
  195. }
  196. /* Complete the transport layer checksum */
  197. if ( trans_csum )
  198. *trans_csum = ipv6_tx_csum ( iobuf, *trans_csum );
  199. /* Print IPv6 header */
  200. ipv6_dump ( ip6hdr );
  201. /* Resolve link layer address */
  202. if ( next_hop.in6_u.u6_addr8[0] == 0xff ) {
  203. ll_dest_buf[0] = 0x33;
  204. ll_dest_buf[1] = 0x33;
  205. ll_dest_buf[2] = next_hop.in6_u.u6_addr8[12];
  206. ll_dest_buf[3] = next_hop.in6_u.u6_addr8[13];
  207. ll_dest_buf[4] = next_hop.in6_u.u6_addr8[14];
  208. ll_dest_buf[5] = next_hop.in6_u.u6_addr8[15];
  209. } else {
  210. /* Unicast address needs to be resolved by NDP */
  211. if ( ( rc = ndp_resolve ( netdev, &next_hop, &ip6hdr->src,
  212. ll_dest_buf ) ) != 0 ) {
  213. DBG ( "No entry for %s\n", inet6_ntoa ( next_hop ) );
  214. goto err;
  215. }
  216. }
  217. /* Transmit packet */
  218. return net_tx ( iobuf, netdev, &ipv6_protocol, ll_dest );
  219. err:
  220. free_iob ( iobuf );
  221. return rc;
  222. }
  223. /**
  224. * Process next IP6 header
  225. *
  226. * @v iobuf I/O buffer
  227. * @v nxt_hdr Next header number
  228. * @v src Source socket address
  229. * @v dest Destination socket address
  230. *
  231. * Refer http://www.iana.org/assignments/ipv6-parameters for the numbers
  232. */
  233. static int ipv6_process_nxt_hdr ( struct io_buffer *iobuf, uint8_t nxt_hdr,
  234. struct sockaddr_tcpip *src, struct sockaddr_tcpip *dest ) {
  235. switch ( nxt_hdr ) {
  236. case IP6_HOPBYHOP:
  237. case IP6_ROUTING:
  238. case IP6_FRAGMENT:
  239. case IP6_AUTHENTICATION:
  240. case IP6_DEST_OPTS:
  241. case IP6_ESP:
  242. DBG ( "Function not implemented for header %d\n", nxt_hdr );
  243. return -ENOSYS;
  244. case IP6_ICMP6:
  245. break;
  246. case IP6_NO_HEADER:
  247. DBG ( "No next header\n" );
  248. return 0;
  249. }
  250. /* Next header is not a IPv6 extension header */
  251. return tcpip_rx ( iobuf, nxt_hdr, src, dest, 0 /* fixme */ );
  252. }
  253. /**
  254. * Process incoming IP6 packets
  255. *
  256. * @v iobuf I/O buffer
  257. * @v netdev Network device
  258. * @v ll_source Link-layer source address
  259. *
  260. * This function processes a IPv6 packet
  261. */
  262. static int ipv6_rx ( struct io_buffer *iobuf,
  263. __unused struct net_device *netdev,
  264. __unused const void *ll_source ) {
  265. struct ip6_header *ip6hdr = iobuf->data;
  266. union {
  267. struct sockaddr_in6 sin6;
  268. struct sockaddr_tcpip st;
  269. } src, dest;
  270. /* Sanity check */
  271. if ( iob_len ( iobuf ) < sizeof ( *ip6hdr ) ) {
  272. DBG ( "Packet too short (%zd bytes)\n", iob_len ( iobuf ) );
  273. goto drop;
  274. }
  275. /* TODO: Verify checksum */
  276. /* Print IP6 header for debugging */
  277. ipv6_dump ( ip6hdr );
  278. /* Check header version */
  279. if ( ( ip6hdr->ver_traffic_class_flow_label & 0xf0000000 ) != 0x60000000 ) {
  280. DBG ( "Invalid protocol version\n" );
  281. goto drop;
  282. }
  283. /* Check the payload length */
  284. if ( ntohs ( ip6hdr->payload_len ) > iob_len ( iobuf ) ) {
  285. DBG ( "Inconsistent packet length (%d bytes)\n",
  286. ip6hdr->payload_len );
  287. goto drop;
  288. }
  289. /* Ignore the traffic class and flow control values */
  290. /* Construct socket address */
  291. memset ( &src, 0, sizeof ( src ) );
  292. src.sin6.sin_family = AF_INET6;
  293. src.sin6.sin6_addr = ip6hdr->src;
  294. memset ( &dest, 0, sizeof ( dest ) );
  295. dest.sin6.sin_family = AF_INET6;
  296. dest.sin6.sin6_addr = ip6hdr->dest;
  297. /* Strip header */
  298. iob_unput ( iobuf, iob_len ( iobuf ) - ntohs ( ip6hdr->payload_len ) -
  299. sizeof ( *ip6hdr ) );
  300. iob_pull ( iobuf, sizeof ( *ip6hdr ) );
  301. /* Send it to the transport layer */
  302. return ipv6_process_nxt_hdr ( iobuf, ip6hdr->nxt_hdr, &src.st, &dest.st );
  303. drop:
  304. DBG ( "Packet dropped\n" );
  305. free_iob ( iobuf );
  306. return -1;
  307. }
  308. /**
  309. * Print a IP6 address as xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
  310. */
  311. char * inet6_ntoa ( struct in6_addr in6 ) {
  312. static char buf[40];
  313. uint16_t *bytes = ( uint16_t* ) &in6;
  314. sprintf ( buf, "%x:%x:%x:%x:%x:%x:%x:%x", bytes[0], bytes[1], bytes[2],
  315. bytes[3], bytes[4], bytes[5], bytes[6], bytes[7] );
  316. return buf;
  317. }
  318. static const char * ipv6_ntoa ( const void *net_addr ) {
  319. return inet6_ntoa ( * ( ( struct in6_addr * ) net_addr ) );
  320. }
  321. /** IPv6 protocol */
  322. struct net_protocol ipv6_protocol __net_protocol = {
  323. .name = "IPv6",
  324. .net_proto = htons ( ETH_P_IPV6 ),
  325. .net_addr_len = sizeof ( struct in6_addr ),
  326. .rx = ipv6_rx,
  327. .ntoa = ipv6_ntoa,
  328. };
  329. /** IPv6 TCPIP net protocol */
  330. struct tcpip_net_protocol ipv6_tcpip_protocol __tcpip_net_protocol = {
  331. .name = "IPv6",
  332. .sa_family = AF_INET6,
  333. .tx = ipv6_tx,
  334. };