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

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 <ipxe/in.h>
  8. #include <ipxe/ip6.h>
  9. #include <ipxe/ndp.h>
  10. #include <ipxe/list.h>
  11. #include <ipxe/icmp6.h>
  12. #include <ipxe/tcpip.h>
  13. #include <ipxe/socket.h>
  14. #include <ipxe/iobuf.h>
  15. #include <ipxe/netdevice.h>
  16. #include <ipxe/if_ether.h>
  17. /* Unspecified IP6 address */
  18. static struct in6_addr ip6_none = {
  19. .in6_u.u6_addr32 = { 0,0,0,0 }
  20. };
  21. /** An IPv6 routing table entry */
  22. struct ipv6_miniroute {
  23. /* List of miniroutes */
  24. struct list_head list;
  25. /* Network device */
  26. struct net_device *netdev;
  27. /* Destination prefix */
  28. struct in6_addr prefix;
  29. /* Prefix length */
  30. int prefix_len;
  31. /* IPv6 address of interface */
  32. struct in6_addr address;
  33. /* Gateway address */
  34. struct in6_addr gateway;
  35. };
  36. /** List of IPv6 miniroutes */
  37. static LIST_HEAD ( miniroutes );
  38. /**
  39. * Add IPv6 minirouting table entry
  40. *
  41. * @v netdev Network device
  42. * @v prefix Destination prefix
  43. * @v address Address of the interface
  44. * @v gateway Gateway address (or ::0 for no gateway)
  45. * @ret miniroute Routing table entry, or NULL
  46. */
  47. static struct ipv6_miniroute * __malloc
  48. add_ipv6_miniroute ( struct net_device *netdev, struct in6_addr prefix,
  49. int prefix_len, struct in6_addr address,
  50. struct in6_addr gateway ) {
  51. struct ipv6_miniroute *miniroute;
  52. miniroute = malloc ( sizeof ( *miniroute ) );
  53. if ( miniroute ) {
  54. /* Record routing information */
  55. miniroute->netdev = netdev_get ( netdev );
  56. miniroute->prefix = prefix;
  57. miniroute->prefix_len = prefix_len;
  58. miniroute->address = address;
  59. miniroute->gateway = gateway;
  60. /* Add miniroute to list of miniroutes */
  61. if ( !IP6_EQUAL ( gateway, ip6_none ) ) {
  62. list_add_tail ( &miniroute->list, &miniroutes );
  63. } else {
  64. list_add ( &miniroute->list, &miniroutes );
  65. }
  66. }
  67. return miniroute;
  68. }
  69. /**
  70. * Delete IPv6 minirouting table entry
  71. *
  72. * @v miniroute Routing table entry
  73. */
  74. static void del_ipv6_miniroute ( struct ipv6_miniroute *miniroute ) {
  75. netdev_put ( miniroute->netdev );
  76. list_del ( &miniroute->list );
  77. free ( miniroute );
  78. }
  79. /**
  80. * Add IPv6 interface
  81. *
  82. * @v netdev Network device
  83. * @v prefix Destination prefix
  84. * @v address Address of the interface
  85. * @v gateway Gateway address (or ::0 for no gateway)
  86. */
  87. int add_ipv6_address ( struct net_device *netdev, struct in6_addr prefix,
  88. int prefix_len, struct in6_addr address,
  89. struct in6_addr gateway ) {
  90. struct ipv6_miniroute *miniroute;
  91. /* Clear any existing address for this net device */
  92. del_ipv6_address ( netdev );
  93. /* Add new miniroute */
  94. miniroute = add_ipv6_miniroute ( netdev, prefix, prefix_len, address,
  95. gateway );
  96. if ( ! miniroute )
  97. return -ENOMEM;
  98. return 0;
  99. }
  100. /**
  101. * Remove IPv6 interface
  102. *
  103. * @v netdev Network device
  104. */
  105. void del_ipv6_address ( struct net_device *netdev ) {
  106. struct ipv6_miniroute *miniroute;
  107. list_for_each_entry ( miniroute, &miniroutes, list ) {
  108. if ( miniroute->netdev == netdev ) {
  109. del_ipv6_miniroute ( miniroute );
  110. break;
  111. }
  112. }
  113. }
  114. /**
  115. * Calculate TCPIP checksum
  116. *
  117. * @v iobuf I/O buffer
  118. * @v tcpip TCP/IP protocol
  119. *
  120. * This function constructs the pseudo header and completes the checksum in the
  121. * upper layer header.
  122. */
  123. static uint16_t ipv6_tx_csum ( struct io_buffer *iobuf, uint16_t csum ) {
  124. struct ip6_header *ip6hdr = iobuf->data;
  125. struct ipv6_pseudo_header pshdr;
  126. /* Calculate pseudo header */
  127. memset ( &pshdr, 0, sizeof ( pshdr ) );
  128. pshdr.src = ip6hdr->src;
  129. pshdr.dest = ip6hdr->dest;
  130. pshdr.len = htons ( iob_len ( iobuf ) - sizeof ( *ip6hdr ) );
  131. pshdr.nxt_hdr = ip6hdr->nxt_hdr;
  132. /* Update checksum value */
  133. return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
  134. }
  135. /**
  136. * Dump IP6 header for debugging
  137. *
  138. * ip6hdr IPv6 header
  139. */
  140. void ipv6_dump ( struct ip6_header *ip6hdr ) {
  141. DBG ( "IP6 %p src %s dest %s nxt_hdr %d len %d\n", ip6hdr,
  142. inet6_ntoa ( ip6hdr->src ), inet6_ntoa ( ip6hdr->dest ),
  143. ip6hdr->nxt_hdr, ntohs ( ip6hdr->payload_len ) );
  144. }
  145. /**
  146. * Transmit IP6 packet
  147. *
  148. * iobuf I/O buffer
  149. * tcpip TCP/IP protocol
  150. * st_dest Destination socket address
  151. *
  152. * This function prepends the IPv6 headers to the payload an transmits it.
  153. */
  154. static int ipv6_tx ( struct io_buffer *iobuf,
  155. struct tcpip_protocol *tcpip,
  156. struct sockaddr_tcpip *st_src __unused,
  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. netdev->ll_addr );
  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_dest Link-layer destination address
  260. * @v ll_source Link-layer source address
  261. * @v flags Packet flags
  262. *
  263. * This function processes a IPv6 packet
  264. */
  265. static int ipv6_rx ( struct io_buffer *iobuf,
  266. __unused struct net_device *netdev,
  267. __unused const void *ll_dest,
  268. __unused const void *ll_source,
  269. __unused unsigned int flags ) {
  270. struct ip6_header *ip6hdr = iobuf->data;
  271. union {
  272. struct sockaddr_in6 sin6;
  273. struct sockaddr_tcpip st;
  274. } src, dest;
  275. /* Sanity check */
  276. if ( iob_len ( iobuf ) < sizeof ( *ip6hdr ) ) {
  277. DBG ( "Packet too short (%zd bytes)\n", iob_len ( iobuf ) );
  278. goto drop;
  279. }
  280. /* TODO: Verify checksum */
  281. /* Print IP6 header for debugging */
  282. ipv6_dump ( ip6hdr );
  283. /* Check header version */
  284. if ( ( ip6hdr->ver_traffic_class_flow_label & 0xf0000000 ) != 0x60000000 ) {
  285. DBG ( "Invalid protocol version\n" );
  286. goto drop;
  287. }
  288. /* Check the payload length */
  289. if ( ntohs ( ip6hdr->payload_len ) > iob_len ( iobuf ) ) {
  290. DBG ( "Inconsistent packet length (%d bytes)\n",
  291. ip6hdr->payload_len );
  292. goto drop;
  293. }
  294. /* Ignore the traffic class and flow control values */
  295. /* Construct socket address */
  296. memset ( &src, 0, sizeof ( src ) );
  297. src.sin6.sin_family = AF_INET6;
  298. src.sin6.sin6_addr = ip6hdr->src;
  299. memset ( &dest, 0, sizeof ( dest ) );
  300. dest.sin6.sin_family = AF_INET6;
  301. dest.sin6.sin6_addr = ip6hdr->dest;
  302. /* Strip header */
  303. iob_unput ( iobuf, iob_len ( iobuf ) - ntohs ( ip6hdr->payload_len ) -
  304. sizeof ( *ip6hdr ) );
  305. iob_pull ( iobuf, sizeof ( *ip6hdr ) );
  306. /* Send it to the transport layer */
  307. return ipv6_process_nxt_hdr ( iobuf, ip6hdr->nxt_hdr, &src.st, &dest.st );
  308. drop:
  309. DBG ( "Packet dropped\n" );
  310. free_iob ( iobuf );
  311. return -1;
  312. }
  313. /**
  314. * Print a IP6 address as xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
  315. */
  316. char * inet6_ntoa ( struct in6_addr in6 ) {
  317. static char buf[40];
  318. uint16_t *bytes = ( uint16_t* ) &in6;
  319. sprintf ( buf, "%x:%x:%x:%x:%x:%x:%x:%x", bytes[0], bytes[1], bytes[2],
  320. bytes[3], bytes[4], bytes[5], bytes[6], bytes[7] );
  321. return buf;
  322. }
  323. static const char * ipv6_ntoa ( const void *net_addr ) {
  324. return inet6_ntoa ( * ( ( struct in6_addr * ) net_addr ) );
  325. }
  326. /** IPv6 protocol */
  327. struct net_protocol ipv6_protocol __net_protocol = {
  328. .name = "IPv6",
  329. .net_proto = htons ( ETH_P_IPV6 ),
  330. .net_addr_len = sizeof ( struct in6_addr ),
  331. .rx = ipv6_rx,
  332. .ntoa = ipv6_ntoa,
  333. };
  334. /** IPv6 TCPIP net protocol */
  335. struct tcpip_net_protocol ipv6_tcpip_protocol __tcpip_net_protocol = {
  336. .name = "IPv6",
  337. .sa_family = AF_INET6,
  338. .tx = ipv6_tx,
  339. };