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

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