Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ipv6.c 10KB

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