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

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