Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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_src __unused,
  158. struct sockaddr_tcpip *st_dest,
  159. struct net_device *netdev,
  160. uint16_t *trans_csum ) {
  161. struct sockaddr_in6 *dest = ( struct sockaddr_in6* ) st_dest;
  162. struct in6_addr next_hop;
  163. struct ipv6_miniroute *miniroute;
  164. uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
  165. const uint8_t *ll_dest = ll_dest_buf;
  166. int rc;
  167. /* Construct the IPv6 packet */
  168. struct ip6_header *ip6hdr = iob_push ( iobuf, sizeof ( *ip6hdr ) );
  169. memset ( ip6hdr, 0, sizeof ( *ip6hdr) );
  170. ip6hdr->ver_traffic_class_flow_label = htonl ( 0x60000000 );//IP6_VERSION;
  171. ip6hdr->payload_len = htons ( iob_len ( iobuf ) - sizeof ( *ip6hdr ) );
  172. ip6hdr->nxt_hdr = tcpip->tcpip_proto;
  173. ip6hdr->hop_limit = IP6_HOP_LIMIT; // 255
  174. /* Determine the next hop address and interface
  175. *
  176. * TODO: Implement the routing table.
  177. */
  178. next_hop = dest->sin6_addr;
  179. list_for_each_entry ( miniroute, &miniroutes, list ) {
  180. if ( ( memcmp ( &ip6hdr->dest, &miniroute->prefix,
  181. miniroute->prefix_len ) == 0 ) ||
  182. ( IP6_EQUAL ( miniroute->gateway, ip6_none ) ) ) {
  183. netdev = miniroute->netdev;
  184. ip6hdr->src = miniroute->address;
  185. if ( ! ( IS_UNSPECIFIED ( miniroute->gateway ) ) ) {
  186. next_hop = miniroute->gateway;
  187. }
  188. break;
  189. }
  190. }
  191. /* No network interface identified */
  192. if ( !netdev ) {
  193. DBG ( "No route to host %s\n", inet6_ntoa ( ip6hdr->dest ) );
  194. rc = -ENETUNREACH;
  195. goto err;
  196. }
  197. /* Complete the transport layer checksum */
  198. if ( trans_csum )
  199. *trans_csum = ipv6_tx_csum ( iobuf, *trans_csum );
  200. /* Print IPv6 header */
  201. ipv6_dump ( ip6hdr );
  202. /* Resolve link layer address */
  203. if ( next_hop.in6_u.u6_addr8[0] == 0xff ) {
  204. ll_dest_buf[0] = 0x33;
  205. ll_dest_buf[1] = 0x33;
  206. ll_dest_buf[2] = next_hop.in6_u.u6_addr8[12];
  207. ll_dest_buf[3] = next_hop.in6_u.u6_addr8[13];
  208. ll_dest_buf[4] = next_hop.in6_u.u6_addr8[14];
  209. ll_dest_buf[5] = next_hop.in6_u.u6_addr8[15];
  210. } else {
  211. /* Unicast address needs to be resolved by NDP */
  212. if ( ( rc = ndp_resolve ( netdev, &next_hop, &ip6hdr->src,
  213. ll_dest_buf ) ) != 0 ) {
  214. DBG ( "No entry for %s\n", inet6_ntoa ( next_hop ) );
  215. goto err;
  216. }
  217. }
  218. /* Transmit packet */
  219. return net_tx ( iobuf, netdev, &ipv6_protocol, ll_dest );
  220. err:
  221. free_iob ( iobuf );
  222. return rc;
  223. }
  224. /**
  225. * Process next IP6 header
  226. *
  227. * @v iobuf I/O buffer
  228. * @v nxt_hdr Next header number
  229. * @v src Source socket address
  230. * @v dest Destination socket address
  231. *
  232. * Refer http://www.iana.org/assignments/ipv6-parameters for the numbers
  233. */
  234. static int ipv6_process_nxt_hdr ( struct io_buffer *iobuf, uint8_t nxt_hdr,
  235. struct sockaddr_tcpip *src, struct sockaddr_tcpip *dest ) {
  236. switch ( nxt_hdr ) {
  237. case IP6_HOPBYHOP:
  238. case IP6_ROUTING:
  239. case IP6_FRAGMENT:
  240. case IP6_AUTHENTICATION:
  241. case IP6_DEST_OPTS:
  242. case IP6_ESP:
  243. DBG ( "Function not implemented for header %d\n", nxt_hdr );
  244. return -ENOSYS;
  245. case IP6_ICMP6:
  246. break;
  247. case IP6_NO_HEADER:
  248. DBG ( "No next header\n" );
  249. return 0;
  250. }
  251. /* Next header is not a IPv6 extension header */
  252. return tcpip_rx ( iobuf, nxt_hdr, src, dest, 0 /* fixme */ );
  253. }
  254. /**
  255. * Process incoming IP6 packets
  256. *
  257. * @v iobuf I/O buffer
  258. * @v netdev Network device
  259. * @v ll_source Link-layer source address
  260. *
  261. * This function processes a IPv6 packet
  262. */
  263. static int ipv6_rx ( struct io_buffer *iobuf,
  264. __unused struct net_device *netdev,
  265. __unused const void *ll_source ) {
  266. struct ip6_header *ip6hdr = iobuf->data;
  267. union {
  268. struct sockaddr_in6 sin6;
  269. struct sockaddr_tcpip st;
  270. } src, dest;
  271. /* Sanity check */
  272. if ( iob_len ( iobuf ) < sizeof ( *ip6hdr ) ) {
  273. DBG ( "Packet too short (%zd bytes)\n", iob_len ( iobuf ) );
  274. goto drop;
  275. }
  276. /* TODO: Verify checksum */
  277. /* Print IP6 header for debugging */
  278. ipv6_dump ( ip6hdr );
  279. /* Check header version */
  280. if ( ( ip6hdr->ver_traffic_class_flow_label & 0xf0000000 ) != 0x60000000 ) {
  281. DBG ( "Invalid protocol version\n" );
  282. goto drop;
  283. }
  284. /* Check the payload length */
  285. if ( ntohs ( ip6hdr->payload_len ) > iob_len ( iobuf ) ) {
  286. DBG ( "Inconsistent packet length (%d bytes)\n",
  287. ip6hdr->payload_len );
  288. goto drop;
  289. }
  290. /* Ignore the traffic class and flow control values */
  291. /* Construct socket address */
  292. memset ( &src, 0, sizeof ( src ) );
  293. src.sin6.sin_family = AF_INET6;
  294. src.sin6.sin6_addr = ip6hdr->src;
  295. memset ( &dest, 0, sizeof ( dest ) );
  296. dest.sin6.sin_family = AF_INET6;
  297. dest.sin6.sin6_addr = ip6hdr->dest;
  298. /* Strip header */
  299. iob_unput ( iobuf, iob_len ( iobuf ) - ntohs ( ip6hdr->payload_len ) -
  300. sizeof ( *ip6hdr ) );
  301. iob_pull ( iobuf, sizeof ( *ip6hdr ) );
  302. /* Send it to the transport layer */
  303. return ipv6_process_nxt_hdr ( iobuf, ip6hdr->nxt_hdr, &src.st, &dest.st );
  304. drop:
  305. DBG ( "Packet dropped\n" );
  306. free_iob ( iobuf );
  307. return -1;
  308. }
  309. /**
  310. * Print a IP6 address as xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
  311. */
  312. char * inet6_ntoa ( struct in6_addr in6 ) {
  313. static char buf[40];
  314. uint16_t *bytes = ( uint16_t* ) &in6;
  315. sprintf ( buf, "%x:%x:%x:%x:%x:%x:%x:%x", bytes[0], bytes[1], bytes[2],
  316. bytes[3], bytes[4], bytes[5], bytes[6], bytes[7] );
  317. return buf;
  318. }
  319. static const char * ipv6_ntoa ( const void *net_addr ) {
  320. return inet6_ntoa ( * ( ( struct in6_addr * ) net_addr ) );
  321. }
  322. /** IPv6 protocol */
  323. struct net_protocol ipv6_protocol __net_protocol = {
  324. .name = "IPv6",
  325. .net_proto = htons ( ETH_P_IPV6 ),
  326. .net_addr_len = sizeof ( struct in6_addr ),
  327. .rx = ipv6_rx,
  328. .ntoa = ipv6_ntoa,
  329. };
  330. /** IPv6 TCPIP net protocol */
  331. struct tcpip_net_protocol ipv6_tcpip_protocol __tcpip_net_protocol = {
  332. .name = "IPv6",
  333. .sa_family = AF_INET6,
  334. .tx = ipv6_tx,
  335. };