Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ipv4.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. #include <string.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <errno.h>
  6. #include <byteswap.h>
  7. #include <gpxe/list.h>
  8. #include <gpxe/in.h>
  9. #include <gpxe/arp.h>
  10. #include <gpxe/if_ether.h>
  11. #include <gpxe/iobuf.h>
  12. #include <gpxe/netdevice.h>
  13. #include <gpxe/ip.h>
  14. #include <gpxe/tcpip.h>
  15. /** @file
  16. *
  17. * IPv4 protocol
  18. *
  19. */
  20. /* Unique IP datagram identification number */
  21. static uint16_t next_ident = 0;
  22. struct net_protocol ipv4_protocol;
  23. /** List of IPv4 miniroutes */
  24. struct list_head ipv4_miniroutes = LIST_HEAD_INIT ( ipv4_miniroutes );
  25. /** List of fragment reassembly buffers */
  26. static LIST_HEAD ( frag_buffers );
  27. /**
  28. * Add IPv4 minirouting table entry
  29. *
  30. * @v netdev Network device
  31. * @v address IPv4 address
  32. * @v netmask Subnet mask
  33. * @v gateway Gateway address (or @c INADDR_NONE for no gateway)
  34. * @ret miniroute Routing table entry, or NULL
  35. */
  36. static struct ipv4_miniroute * add_ipv4_miniroute ( struct net_device *netdev,
  37. struct in_addr address,
  38. struct in_addr netmask,
  39. struct in_addr gateway ) {
  40. struct ipv4_miniroute *miniroute;
  41. DBG ( "IPv4 add %s", inet_ntoa ( address ) );
  42. DBG ( "/%s ", inet_ntoa ( netmask ) );
  43. if ( gateway.s_addr != INADDR_NONE )
  44. DBG ( "gw %s ", inet_ntoa ( gateway ) );
  45. DBG ( "via %s\n", netdev->name );
  46. /* Allocate and populate miniroute structure */
  47. miniroute = malloc ( sizeof ( *miniroute ) );
  48. if ( ! miniroute ) {
  49. DBG ( "IPv4 could not add miniroute\n" );
  50. return NULL;
  51. }
  52. /* Record routing information */
  53. miniroute->netdev = netdev_get ( netdev );
  54. miniroute->address = address;
  55. miniroute->netmask = netmask;
  56. miniroute->gateway = gateway;
  57. /* Add to end of list if we have a gateway, otherwise
  58. * to start of list.
  59. */
  60. if ( gateway.s_addr != INADDR_NONE ) {
  61. list_add_tail ( &miniroute->list, &ipv4_miniroutes );
  62. } else {
  63. list_add ( &miniroute->list, &ipv4_miniroutes );
  64. }
  65. return miniroute;
  66. }
  67. /**
  68. * Delete IPv4 minirouting table entry
  69. *
  70. * @v miniroute Routing table entry
  71. */
  72. static void del_ipv4_miniroute ( struct ipv4_miniroute *miniroute ) {
  73. DBG ( "IPv4 del %s", inet_ntoa ( miniroute->address ) );
  74. DBG ( "/%s ", inet_ntoa ( miniroute->netmask ) );
  75. if ( miniroute->gateway.s_addr != INADDR_NONE )
  76. DBG ( "gw %s ", inet_ntoa ( miniroute->gateway ) );
  77. DBG ( "via %s\n", miniroute->netdev->name );
  78. netdev_put ( miniroute->netdev );
  79. list_del ( &miniroute->list );
  80. free ( miniroute );
  81. }
  82. /**
  83. * Add IPv4 interface
  84. *
  85. * @v netdev Network device
  86. * @v address IPv4 address
  87. * @v netmask Subnet mask
  88. * @v gateway Gateway address (or @c INADDR_NONE for no gateway)
  89. * @ret rc Return status code
  90. *
  91. */
  92. int add_ipv4_address ( struct net_device *netdev, struct in_addr address,
  93. struct in_addr netmask, struct in_addr gateway ) {
  94. struct ipv4_miniroute *miniroute;
  95. /* Clear any existing address for this net device */
  96. del_ipv4_address ( netdev );
  97. /* Add new miniroute */
  98. miniroute = add_ipv4_miniroute ( netdev, address, netmask, gateway );
  99. if ( ! miniroute )
  100. return -ENOMEM;
  101. return 0;
  102. }
  103. /**
  104. * Remove IPv4 interface
  105. *
  106. * @v netdev Network device
  107. */
  108. void del_ipv4_address ( struct net_device *netdev ) {
  109. struct ipv4_miniroute *miniroute;
  110. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  111. if ( miniroute->netdev == netdev ) {
  112. del_ipv4_miniroute ( miniroute );
  113. break;
  114. }
  115. }
  116. }
  117. /**
  118. * Perform IPv4 routing
  119. *
  120. * @v dest Final destination address
  121. * @ret dest Next hop destination address
  122. * @ret miniroute Routing table entry to use, or NULL if no route
  123. */
  124. static struct ipv4_miniroute * ipv4_route ( struct in_addr *dest ) {
  125. struct ipv4_miniroute *miniroute;
  126. int local;
  127. int has_gw;
  128. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  129. local = ( ( ( dest->s_addr ^ miniroute->address.s_addr )
  130. & miniroute->netmask.s_addr ) == 0 );
  131. has_gw = ( miniroute->gateway.s_addr != INADDR_NONE );
  132. if ( local || has_gw ) {
  133. if ( ! local )
  134. *dest = miniroute->gateway;
  135. return miniroute;
  136. }
  137. }
  138. return NULL;
  139. }
  140. /**
  141. * Fragment reassembly counter timeout
  142. *
  143. * @v timer Retry timer
  144. * @v over If asserted, the timer is greater than @c MAX_TIMEOUT
  145. */
  146. static void ipv4_frag_expired ( struct retry_timer *timer __unused,
  147. int over ) {
  148. if ( over ) {
  149. DBG ( "Fragment reassembly timeout" );
  150. /* Free the fragment buffer */
  151. }
  152. }
  153. /**
  154. * Free fragment buffer
  155. *
  156. * @v fragbug Fragment buffer
  157. */
  158. static void free_fragbuf ( struct frag_buffer *fragbuf ) {
  159. free ( fragbuf );
  160. }
  161. /**
  162. * Fragment reassembler
  163. *
  164. * @v iobuf I/O buffer, fragment of the datagram
  165. * @ret frag_iob Reassembled packet, or NULL
  166. */
  167. static struct io_buffer * ipv4_reassemble ( struct io_buffer * iobuf ) {
  168. struct iphdr *iphdr = iobuf->data;
  169. struct frag_buffer *fragbuf;
  170. /**
  171. * Check if the fragment belongs to any fragment series
  172. */
  173. list_for_each_entry ( fragbuf, &frag_buffers, list ) {
  174. if ( fragbuf->ident == iphdr->ident &&
  175. fragbuf->src.s_addr == iphdr->src.s_addr ) {
  176. /**
  177. * Check if the packet is the expected fragment
  178. *
  179. * The offset of the new packet must be equal to the
  180. * length of the data accumulated so far (the length of
  181. * the reassembled I/O buffer
  182. */
  183. if ( iob_len ( fragbuf->frag_iob ) ==
  184. ( iphdr->frags & IP_MASK_OFFSET ) ) {
  185. /**
  186. * Append the contents of the fragment to the
  187. * reassembled I/O buffer
  188. */
  189. iob_pull ( iobuf, sizeof ( *iphdr ) );
  190. memcpy ( iob_put ( fragbuf->frag_iob,
  191. iob_len ( iobuf ) ),
  192. iobuf->data, iob_len ( iobuf ) );
  193. free_iob ( iobuf );
  194. /** Check if the fragment series is over */
  195. if ( !iphdr->frags & IP_MASK_MOREFRAGS ) {
  196. iobuf = fragbuf->frag_iob;
  197. free_fragbuf ( fragbuf );
  198. return iobuf;
  199. }
  200. } else {
  201. /* Discard the fragment series */
  202. free_fragbuf ( fragbuf );
  203. free_iob ( iobuf );
  204. }
  205. return NULL;
  206. }
  207. }
  208. /** Check if the fragment is the first in the fragment series */
  209. if ( iphdr->frags & IP_MASK_MOREFRAGS &&
  210. ( ( iphdr->frags & IP_MASK_OFFSET ) == 0 ) ) {
  211. /** Create a new fragment buffer */
  212. fragbuf = ( struct frag_buffer* ) malloc ( sizeof( *fragbuf ) );
  213. fragbuf->ident = iphdr->ident;
  214. fragbuf->src = iphdr->src;
  215. /* Set up the reassembly I/O buffer */
  216. fragbuf->frag_iob = alloc_iob ( IP_FRAG_IOB_SIZE );
  217. iob_pull ( iobuf, sizeof ( *iphdr ) );
  218. memcpy ( iob_put ( fragbuf->frag_iob, iob_len ( iobuf ) ),
  219. iobuf->data, iob_len ( iobuf ) );
  220. free_iob ( iobuf );
  221. /* Set the reassembly timer */
  222. fragbuf->frag_timer.timeout = IP_FRAG_TIMEOUT;
  223. fragbuf->frag_timer.expired = ipv4_frag_expired;
  224. start_timer ( &fragbuf->frag_timer );
  225. /* Add the fragment buffer to the list of fragment buffers */
  226. list_add ( &fragbuf->list, &frag_buffers );
  227. }
  228. return NULL;
  229. }
  230. /**
  231. * Add IPv4 pseudo-header checksum to existing checksum
  232. *
  233. * @v iobuf I/O buffer
  234. * @v csum Existing checksum
  235. * @ret csum Updated checksum
  236. */
  237. static uint16_t ipv4_pshdr_chksum ( struct io_buffer *iobuf, uint16_t csum ) {
  238. struct ipv4_pseudo_header pshdr;
  239. struct iphdr *iphdr = iobuf->data;
  240. size_t hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
  241. /* Build pseudo-header */
  242. pshdr.src = iphdr->src;
  243. pshdr.dest = iphdr->dest;
  244. pshdr.zero_padding = 0x00;
  245. pshdr.protocol = iphdr->protocol;
  246. pshdr.len = htons ( iob_len ( iobuf ) - hdrlen );
  247. /* Update the checksum value */
  248. return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
  249. }
  250. /**
  251. * Determine link-layer address
  252. *
  253. * @v dest IPv4 destination address
  254. * @v src IPv4 source address
  255. * @v netdev Network device
  256. * @v ll_dest Link-layer destination address buffer
  257. * @ret rc Return status code
  258. */
  259. static int ipv4_ll_addr ( struct in_addr dest, struct in_addr src,
  260. struct net_device *netdev, uint8_t *ll_dest ) {
  261. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  262. uint8_t *dest_bytes = ( ( uint8_t * ) &dest );
  263. if ( dest.s_addr == INADDR_BROADCAST ) {
  264. /* Broadcast address */
  265. memcpy ( ll_dest, ll_protocol->ll_broadcast,
  266. ll_protocol->ll_addr_len );
  267. return 0;
  268. } else if ( IN_MULTICAST ( dest.s_addr ) ) {
  269. /* Special case: IPv4 multicast over Ethernet. This
  270. * code may need to be generalised once we find out
  271. * what happens for other link layers.
  272. */
  273. ll_dest[0] = 0x01;
  274. ll_dest[1] = 0x00;
  275. ll_dest[2] = 0x5e;
  276. ll_dest[3] = dest_bytes[1] & 0x7f;
  277. ll_dest[4] = dest_bytes[2];
  278. ll_dest[5] = dest_bytes[3];
  279. return 0;
  280. } else {
  281. /* Unicast address: resolve via ARP */
  282. return arp_resolve ( netdev, &ipv4_protocol, &dest,
  283. &src, ll_dest );
  284. }
  285. }
  286. /**
  287. * Transmit IP packet
  288. *
  289. * @v iobuf I/O buffer
  290. * @v tcpip Transport-layer protocol
  291. * @v st_dest Destination network-layer address
  292. * @v netdev Network device to use if no route found, or NULL
  293. * @v trans_csum Transport-layer checksum to complete, or NULL
  294. * @ret rc Status
  295. *
  296. * This function expects a transport-layer segment and prepends the IP header
  297. */
  298. static int ipv4_tx ( struct io_buffer *iobuf,
  299. struct tcpip_protocol *tcpip_protocol,
  300. struct sockaddr_tcpip *st_dest,
  301. struct net_device *netdev,
  302. uint16_t *trans_csum ) {
  303. struct iphdr *iphdr = iob_push ( iobuf, sizeof ( *iphdr ) );
  304. struct sockaddr_in *sin_dest = ( ( struct sockaddr_in * ) st_dest );
  305. struct ipv4_miniroute *miniroute;
  306. struct in_addr next_hop;
  307. uint8_t ll_dest[MAX_LL_ADDR_LEN];
  308. int rc;
  309. /* Fill up the IP header, except source address */
  310. memset ( iphdr, 0, sizeof ( *iphdr ) );
  311. iphdr->verhdrlen = ( IP_VER | ( sizeof ( *iphdr ) / 4 ) );
  312. iphdr->service = IP_TOS;
  313. iphdr->len = htons ( iob_len ( iobuf ) );
  314. iphdr->ident = htons ( ++next_ident );
  315. iphdr->ttl = IP_TTL;
  316. iphdr->protocol = tcpip_protocol->tcpip_proto;
  317. iphdr->dest = sin_dest->sin_addr;
  318. /* Use routing table to identify next hop and transmitting netdev */
  319. next_hop = iphdr->dest;
  320. if ( ( miniroute = ipv4_route ( &next_hop ) ) ) {
  321. iphdr->src = miniroute->address;
  322. netdev = miniroute->netdev;
  323. }
  324. if ( ! netdev ) {
  325. DBG ( "IPv4 has no route to %s\n", inet_ntoa ( iphdr->dest ) );
  326. rc = -ENETUNREACH;
  327. goto err;
  328. }
  329. /* Determine link-layer destination address */
  330. if ( ( rc = ipv4_ll_addr ( next_hop, iphdr->src, netdev,
  331. ll_dest ) ) != 0 ) {
  332. DBG ( "IPv4 has no link-layer address for %s: %s\n",
  333. inet_ntoa ( next_hop ), strerror ( rc ) );
  334. goto err;
  335. }
  336. /* Fix up checksums */
  337. if ( trans_csum )
  338. *trans_csum = ipv4_pshdr_chksum ( iobuf, *trans_csum );
  339. iphdr->chksum = tcpip_chksum ( iphdr, sizeof ( *iphdr ) );
  340. /* Print IP4 header for debugging */
  341. DBG ( "IPv4 TX %s->", inet_ntoa ( iphdr->src ) );
  342. DBG ( "%s len %d proto %d id %04x csum %04x\n",
  343. inet_ntoa ( iphdr->dest ), ntohs ( iphdr->len ), iphdr->protocol,
  344. ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );
  345. /* Hand off to link layer */
  346. if ( ( rc = net_tx ( iobuf, netdev, &ipv4_protocol, ll_dest ) ) != 0 ) {
  347. DBG ( "IPv4 could not transmit packet via %s: %s\n",
  348. netdev->name, strerror ( rc ) );
  349. return rc;
  350. }
  351. return 0;
  352. err:
  353. free_iob ( iobuf );
  354. return rc;
  355. }
  356. /**
  357. * Process incoming packets
  358. *
  359. * @v iobuf I/O buffer
  360. * @v netdev Network device
  361. * @v ll_source Link-layer destination source
  362. *
  363. * This function expects an IP4 network datagram. It processes the headers
  364. * and sends it to the transport layer.
  365. */
  366. static int ipv4_rx ( struct io_buffer *iobuf, struct net_device *netdev __unused,
  367. const void *ll_source __unused ) {
  368. struct iphdr *iphdr = iobuf->data;
  369. size_t hdrlen;
  370. size_t len;
  371. union {
  372. struct sockaddr_in sin;
  373. struct sockaddr_tcpip st;
  374. } src, dest;
  375. uint16_t csum;
  376. uint16_t pshdr_csum;
  377. int rc;
  378. /* Sanity check the IPv4 header */
  379. if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
  380. DBG ( "IPv4 packet too short at %d bytes (min %d bytes)\n",
  381. iob_len ( iobuf ), sizeof ( *iphdr ) );
  382. goto err;
  383. }
  384. if ( ( iphdr->verhdrlen & IP_MASK_VER ) != IP_VER ) {
  385. DBG ( "IPv4 version %#02x not supported\n", iphdr->verhdrlen );
  386. goto err;
  387. }
  388. hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
  389. if ( hdrlen < sizeof ( *iphdr ) ) {
  390. DBG ( "IPv4 header too short at %d bytes (min %d bytes)\n",
  391. hdrlen, sizeof ( *iphdr ) );
  392. goto err;
  393. }
  394. if ( hdrlen > iob_len ( iobuf ) ) {
  395. DBG ( "IPv4 header too long at %d bytes "
  396. "(packet is %d bytes)\n", hdrlen, iob_len ( iobuf ) );
  397. goto err;
  398. }
  399. if ( ( csum = tcpip_chksum ( iphdr, hdrlen ) ) != 0 ) {
  400. DBG ( "IPv4 checksum incorrect (is %04x including checksum "
  401. "field, should be 0000)\n", csum );
  402. goto err;
  403. }
  404. len = ntohs ( iphdr->len );
  405. if ( len < hdrlen ) {
  406. DBG ( "IPv4 length too short at %d bytes "
  407. "(header is %d bytes)\n", len, hdrlen );
  408. goto err;
  409. }
  410. if ( len > iob_len ( iobuf ) ) {
  411. DBG ( "IPv4 length too long at %d bytes "
  412. "(packet is %d bytes)\n", len, iob_len ( iobuf ) );
  413. goto err;
  414. }
  415. /* Print IPv4 header for debugging */
  416. DBG ( "IPv4 RX %s<-", inet_ntoa ( iphdr->dest ) );
  417. DBG ( "%s len %d proto %d id %04x csum %04x\n",
  418. inet_ntoa ( iphdr->src ), ntohs ( iphdr->len ), iphdr->protocol,
  419. ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );
  420. /* Truncate packet to correct length, calculate pseudo-header
  421. * checksum and then strip off the IPv4 header.
  422. */
  423. iob_unput ( iobuf, ( iob_len ( iobuf ) - len ) );
  424. pshdr_csum = ipv4_pshdr_chksum ( iobuf, TCPIP_EMPTY_CSUM );
  425. iob_pull ( iobuf, hdrlen );
  426. /* Fragment reassembly */
  427. if ( ( iphdr->frags & htons ( IP_MASK_MOREFRAGS ) ) ||
  428. ( ( iphdr->frags & htons ( IP_MASK_OFFSET ) ) != 0 ) ) {
  429. /* Pass the fragment to ipv4_reassemble() which either
  430. * returns a fully reassembled I/O buffer or NULL.
  431. */
  432. iobuf = ipv4_reassemble ( iobuf );
  433. if ( ! iobuf )
  434. return 0;
  435. }
  436. /* Construct socket addresses and hand off to transport layer */
  437. memset ( &src, 0, sizeof ( src ) );
  438. src.sin.sin_family = AF_INET;
  439. src.sin.sin_addr = iphdr->src;
  440. memset ( &dest, 0, sizeof ( dest ) );
  441. dest.sin.sin_family = AF_INET;
  442. dest.sin.sin_addr = iphdr->dest;
  443. if ( ( rc = tcpip_rx ( iobuf, iphdr->protocol, &src.st,
  444. &dest.st, pshdr_csum ) ) != 0 ) {
  445. DBG ( "IPv4 received packet rejected by stack: %s\n",
  446. strerror ( rc ) );
  447. return rc;
  448. }
  449. return 0;
  450. err:
  451. free_iob ( iobuf );
  452. return -EINVAL;
  453. }
  454. /**
  455. * Check existence of IPv4 address for ARP
  456. *
  457. * @v netdev Network device
  458. * @v net_addr Network-layer address
  459. * @ret rc Return status code
  460. */
  461. static int ipv4_arp_check ( struct net_device *netdev, const void *net_addr ) {
  462. const struct in_addr *address = net_addr;
  463. struct ipv4_miniroute *miniroute;
  464. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  465. if ( ( miniroute->netdev == netdev ) &&
  466. ( miniroute->address.s_addr == address->s_addr ) ) {
  467. /* Found matching address */
  468. return 0;
  469. }
  470. }
  471. return -ENOENT;
  472. }
  473. /**
  474. * Convert IPv4 address to dotted-quad notation
  475. *
  476. * @v in IP address
  477. * @ret string IP address in dotted-quad notation
  478. */
  479. char * inet_ntoa ( struct in_addr in ) {
  480. static char buf[16]; /* "xxx.xxx.xxx.xxx" */
  481. uint8_t *bytes = ( uint8_t * ) &in;
  482. sprintf ( buf, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3] );
  483. return buf;
  484. }
  485. /**
  486. * Transcribe IP address
  487. *
  488. * @v net_addr IP address
  489. * @ret string IP address in dotted-quad notation
  490. *
  491. */
  492. static const char * ipv4_ntoa ( const void *net_addr ) {
  493. return inet_ntoa ( * ( ( struct in_addr * ) net_addr ) );
  494. }
  495. /** IPv4 protocol */
  496. struct net_protocol ipv4_protocol __net_protocol = {
  497. .name = "IP",
  498. .net_proto = htons ( ETH_P_IP ),
  499. .net_addr_len = sizeof ( struct in_addr ),
  500. .rx = ipv4_rx,
  501. .ntoa = ipv4_ntoa,
  502. };
  503. /** IPv4 TCPIP net protocol */
  504. struct tcpip_net_protocol ipv4_tcpip_protocol __tcpip_net_protocol = {
  505. .name = "IPv4",
  506. .sa_family = AF_INET,
  507. .tx = ipv4_tx,
  508. };
  509. /** IPv4 ARP protocol */
  510. struct arp_net_protocol ipv4_arp_protocol __arp_net_protocol = {
  511. .net_protocol = &ipv4_protocol,
  512. .check = ipv4_arp_check,
  513. };