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

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