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.

ipv4.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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 <ipxe/list.h>
  8. #include <ipxe/in.h>
  9. #include <ipxe/arp.h>
  10. #include <ipxe/if_ether.h>
  11. #include <ipxe/iobuf.h>
  12. #include <ipxe/netdevice.h>
  13. #include <ipxe/ip.h>
  14. #include <ipxe/tcpip.h>
  15. #include <ipxe/dhcp.h>
  16. #include <ipxe/settings.h>
  17. #include <ipxe/timer.h>
  18. /** @file
  19. *
  20. * IPv4 protocol
  21. *
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER );
  24. /* Unique IP datagram identification number (high byte) */
  25. static uint8_t next_ident_high = 0;
  26. /** List of IPv4 miniroutes */
  27. struct list_head ipv4_miniroutes = LIST_HEAD_INIT ( ipv4_miniroutes );
  28. /** List of fragment reassembly buffers */
  29. static LIST_HEAD ( ipv4_fragments );
  30. /** Fragment reassembly timeout */
  31. #define IP_FRAG_TIMEOUT ( TICKS_PER_SEC / 2 )
  32. /**
  33. * Add IPv4 minirouting table entry
  34. *
  35. * @v netdev Network device
  36. * @v address IPv4 address
  37. * @v netmask Subnet mask
  38. * @v gateway Gateway address (if any)
  39. * @ret miniroute Routing table entry, or NULL
  40. */
  41. static struct ipv4_miniroute * __malloc
  42. add_ipv4_miniroute ( struct net_device *netdev, struct in_addr address,
  43. struct in_addr netmask, struct in_addr gateway ) {
  44. struct ipv4_miniroute *miniroute;
  45. DBGC ( netdev, "IPv4 add %s", inet_ntoa ( address ) );
  46. DBGC ( netdev, "/%s ", inet_ntoa ( netmask ) );
  47. if ( gateway.s_addr )
  48. DBGC ( netdev, "gw %s ", inet_ntoa ( gateway ) );
  49. DBGC ( netdev, "via %s\n", netdev->name );
  50. /* Allocate and populate miniroute structure */
  51. miniroute = malloc ( sizeof ( *miniroute ) );
  52. if ( ! miniroute ) {
  53. DBGC ( netdev, "IPv4 could not add miniroute\n" );
  54. return NULL;
  55. }
  56. /* Record routing information */
  57. miniroute->netdev = netdev_get ( netdev );
  58. miniroute->address = address;
  59. miniroute->netmask = netmask;
  60. miniroute->gateway = gateway;
  61. /* Add to end of list if we have a gateway, otherwise
  62. * to start of list.
  63. */
  64. if ( gateway.s_addr ) {
  65. list_add_tail ( &miniroute->list, &ipv4_miniroutes );
  66. } else {
  67. list_add ( &miniroute->list, &ipv4_miniroutes );
  68. }
  69. return miniroute;
  70. }
  71. /**
  72. * Delete IPv4 minirouting table entry
  73. *
  74. * @v miniroute Routing table entry
  75. */
  76. static void del_ipv4_miniroute ( struct ipv4_miniroute *miniroute ) {
  77. struct net_device *netdev = miniroute->netdev;
  78. DBGC ( netdev, "IPv4 del %s", inet_ntoa ( miniroute->address ) );
  79. DBGC ( netdev, "/%s ", inet_ntoa ( miniroute->netmask ) );
  80. if ( miniroute->gateway.s_addr )
  81. DBGC ( netdev, "gw %s ", inet_ntoa ( miniroute->gateway ) );
  82. DBGC ( netdev, "via %s\n", miniroute->netdev->name );
  83. netdev_put ( miniroute->netdev );
  84. list_del ( &miniroute->list );
  85. free ( miniroute );
  86. }
  87. /**
  88. * Perform IPv4 routing
  89. *
  90. * @v dest Final destination address
  91. * @ret dest Next hop destination address
  92. * @ret miniroute Routing table entry to use, or NULL if no route
  93. *
  94. * If the route requires use of a gateway, the next hop destination
  95. * address will be overwritten with the gateway address.
  96. */
  97. static struct ipv4_miniroute * ipv4_route ( struct in_addr *dest ) {
  98. struct ipv4_miniroute *miniroute;
  99. int local;
  100. int has_gw;
  101. /* Find first usable route in routing table */
  102. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  103. if ( ! netdev_is_open ( miniroute->netdev ) )
  104. continue;
  105. local = ( ( ( dest->s_addr ^ miniroute->address.s_addr )
  106. & miniroute->netmask.s_addr ) == 0 );
  107. has_gw = ( miniroute->gateway.s_addr );
  108. if ( local || has_gw ) {
  109. if ( ! local )
  110. *dest = miniroute->gateway;
  111. return miniroute;
  112. }
  113. }
  114. return NULL;
  115. }
  116. /**
  117. * Expire fragment reassembly buffer
  118. *
  119. * @v timer Retry timer
  120. * @v fail Failure indicator
  121. */
  122. static void ipv4_fragment_expired ( struct retry_timer *timer,
  123. int fail __unused ) {
  124. struct ipv4_fragment *frag =
  125. container_of ( timer, struct ipv4_fragment, timer );
  126. struct iphdr *iphdr = frag->iobuf->data;
  127. DBGC ( iphdr->src, "IPv4 fragment %04x expired\n",
  128. ntohs ( iphdr->ident ) );
  129. free_iob ( frag->iobuf );
  130. list_del ( &frag->list );
  131. free ( frag );
  132. }
  133. /**
  134. * Find matching fragment reassembly buffer
  135. *
  136. * @v iphdr IPv4 header
  137. * @ret frag Fragment reassembly buffer, or NULL
  138. */
  139. static struct ipv4_fragment * ipv4_fragment ( struct iphdr *iphdr ) {
  140. struct ipv4_fragment *frag;
  141. struct iphdr *frag_iphdr;
  142. list_for_each_entry ( frag, &ipv4_fragments, list ) {
  143. frag_iphdr = frag->iobuf->data;
  144. if ( ( iphdr->src.s_addr == frag_iphdr->src.s_addr ) &&
  145. ( iphdr->ident == frag_iphdr->ident ) ) {
  146. return frag;
  147. }
  148. }
  149. return NULL;
  150. }
  151. /**
  152. * Fragment reassembler
  153. *
  154. * @v iobuf I/O buffer
  155. * @ret iobuf Reassembled packet, or NULL
  156. */
  157. static struct io_buffer * ipv4_reassemble ( struct io_buffer *iobuf ) {
  158. struct iphdr *iphdr = iobuf->data;
  159. size_t offset = ( ( ntohs ( iphdr->frags ) & IP_MASK_OFFSET ) << 3 );
  160. unsigned int more_frags = ( iphdr->frags & htons ( IP_MASK_MOREFRAGS ));
  161. size_t hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
  162. struct ipv4_fragment *frag;
  163. size_t expected_offset;
  164. struct io_buffer *new_iobuf;
  165. /* Find matching fragment reassembly buffer, if any */
  166. frag = ipv4_fragment ( iphdr );
  167. /* Drop out-of-order fragments */
  168. expected_offset = ( frag ? frag->offset : 0 );
  169. if ( offset != expected_offset ) {
  170. DBGC ( iphdr->src, "IPv4 dropping out-of-sequence fragment "
  171. "%04x (%zd+%zd, expected %zd)\n",
  172. ntohs ( iphdr->ident ), offset,
  173. ( iob_len ( iobuf ) - hdrlen ), expected_offset );
  174. goto drop;
  175. }
  176. /* Create or extend fragment reassembly buffer as applicable */
  177. if ( frag == NULL ) {
  178. /* Create new fragment reassembly buffer */
  179. frag = zalloc ( sizeof ( *frag ) );
  180. if ( ! frag )
  181. goto drop;
  182. list_add ( &frag->list, &ipv4_fragments );
  183. frag->iobuf = iobuf;
  184. frag->offset = ( iob_len ( iobuf ) - hdrlen );
  185. timer_init ( &frag->timer, ipv4_fragment_expired, NULL );
  186. } else {
  187. /* Extend reassembly buffer */
  188. iob_pull ( iobuf, hdrlen );
  189. new_iobuf = alloc_iob ( iob_len ( frag->iobuf ) +
  190. iob_len ( iobuf ) );
  191. if ( ! new_iobuf ) {
  192. DBGC ( iphdr->src, "IPv4 could not extend reassembly "
  193. "buffer to %zd bytes\n",
  194. iob_len ( frag->iobuf ) + iob_len ( iobuf ) );
  195. goto drop;
  196. }
  197. memcpy ( iob_put ( new_iobuf, iob_len ( frag->iobuf ) ),
  198. frag->iobuf->data, iob_len ( frag->iobuf ) );
  199. memcpy ( iob_put ( new_iobuf, iob_len ( iobuf ) ),
  200. iobuf->data, iob_len ( iobuf ) );
  201. free_iob ( frag->iobuf );
  202. frag->iobuf = new_iobuf;
  203. frag->offset += iob_len ( iobuf );
  204. free_iob ( iobuf );
  205. iphdr = frag->iobuf->data;
  206. iphdr->len = ntohs ( iob_len ( frag->iobuf ) );
  207. /* Stop fragment reassembly timer */
  208. stop_timer ( &frag->timer );
  209. /* If this is the final fragment, return it */
  210. if ( ! more_frags ) {
  211. iobuf = frag->iobuf;
  212. list_del ( &frag->list );
  213. free ( frag );
  214. return iobuf;
  215. }
  216. }
  217. /* (Re)start fragment reassembly timer */
  218. start_timer_fixed ( &frag->timer, IP_FRAG_TIMEOUT );
  219. return NULL;
  220. drop:
  221. free_iob ( iobuf );
  222. return NULL;
  223. }
  224. /**
  225. * Add IPv4 pseudo-header checksum to existing checksum
  226. *
  227. * @v iobuf I/O buffer
  228. * @v csum Existing checksum
  229. * @ret csum Updated checksum
  230. */
  231. static uint16_t ipv4_pshdr_chksum ( struct io_buffer *iobuf, uint16_t csum ) {
  232. struct ipv4_pseudo_header pshdr;
  233. struct iphdr *iphdr = iobuf->data;
  234. size_t hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
  235. /* Build pseudo-header */
  236. pshdr.src = iphdr->src;
  237. pshdr.dest = iphdr->dest;
  238. pshdr.zero_padding = 0x00;
  239. pshdr.protocol = iphdr->protocol;
  240. pshdr.len = htons ( iob_len ( iobuf ) - hdrlen );
  241. /* Update the checksum value */
  242. return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
  243. }
  244. /**
  245. * Transmit IP packet
  246. *
  247. * @v iobuf I/O buffer
  248. * @v tcpip Transport-layer protocol
  249. * @v st_src Source network-layer address
  250. * @v st_dest Destination network-layer address
  251. * @v netdev Network device to use if no route found, or NULL
  252. * @v trans_csum Transport-layer checksum to complete, or NULL
  253. * @ret rc Status
  254. *
  255. * This function expects a transport-layer segment and prepends the IP header
  256. */
  257. static int ipv4_tx ( struct io_buffer *iobuf,
  258. struct tcpip_protocol *tcpip_protocol,
  259. struct sockaddr_tcpip *st_src,
  260. struct sockaddr_tcpip *st_dest,
  261. struct net_device *netdev,
  262. uint16_t *trans_csum ) {
  263. struct iphdr *iphdr = iob_push ( iobuf, sizeof ( *iphdr ) );
  264. struct sockaddr_in *sin_src = ( ( struct sockaddr_in * ) st_src );
  265. struct sockaddr_in *sin_dest = ( ( struct sockaddr_in * ) st_dest );
  266. struct ipv4_miniroute *miniroute;
  267. struct in_addr next_hop;
  268. struct in_addr netmask = { .s_addr = 0 };
  269. uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
  270. const void *ll_dest;
  271. int rc;
  272. /* Fill up the IP header, except source address */
  273. memset ( iphdr, 0, sizeof ( *iphdr ) );
  274. iphdr->verhdrlen = ( IP_VER | ( sizeof ( *iphdr ) / 4 ) );
  275. iphdr->service = IP_TOS;
  276. iphdr->len = htons ( iob_len ( iobuf ) );
  277. iphdr->ttl = IP_TTL;
  278. iphdr->protocol = tcpip_protocol->tcpip_proto;
  279. iphdr->dest = sin_dest->sin_addr;
  280. /* Use routing table to identify next hop and transmitting netdev */
  281. next_hop = iphdr->dest;
  282. if ( sin_src )
  283. iphdr->src = sin_src->sin_addr;
  284. if ( ( next_hop.s_addr != INADDR_BROADCAST ) &&
  285. ( ! IN_MULTICAST ( ntohl ( next_hop.s_addr ) ) ) &&
  286. ( ( miniroute = ipv4_route ( &next_hop ) ) != NULL ) ) {
  287. iphdr->src = miniroute->address;
  288. netmask = miniroute->netmask;
  289. netdev = miniroute->netdev;
  290. }
  291. if ( ! netdev ) {
  292. DBGC ( sin_dest->sin_addr, "IPv4 has no route to %s\n",
  293. inet_ntoa ( iphdr->dest ) );
  294. rc = -ENETUNREACH;
  295. goto err;
  296. }
  297. /* (Ab)use the "ident" field to convey metadata about the
  298. * network device statistics into packet traces. Useful for
  299. * extracting debug information from non-debug builds.
  300. */
  301. iphdr->ident = htons ( ( (++next_ident_high) << 8 ) |
  302. ( ( netdev->rx_stats.bad & 0xf ) << 4 ) |
  303. ( ( netdev->rx_stats.good & 0xf ) << 0 ) );
  304. /* Fix up checksums */
  305. if ( trans_csum )
  306. *trans_csum = ipv4_pshdr_chksum ( iobuf, *trans_csum );
  307. iphdr->chksum = tcpip_chksum ( iphdr, sizeof ( *iphdr ) );
  308. /* Print IP4 header for debugging */
  309. DBGC2 ( sin_dest->sin_addr, "IPv4 TX %s->", inet_ntoa ( iphdr->src ) );
  310. DBGC2 ( sin_dest->sin_addr, "%s len %d proto %d id %04x csum %04x\n",
  311. inet_ntoa ( iphdr->dest ), ntohs ( iphdr->len ),
  312. iphdr->protocol, ntohs ( iphdr->ident ),
  313. ntohs ( iphdr->chksum ) );
  314. /* Calculate link-layer destination address, if possible */
  315. if ( ( ( next_hop.s_addr ^ INADDR_BROADCAST ) & ~netmask.s_addr ) == 0){
  316. /* Broadcast address */
  317. ll_dest = netdev->ll_broadcast;
  318. } else if ( IN_MULTICAST ( ntohl ( next_hop.s_addr ) ) ) {
  319. /* Multicast address */
  320. if ( ( rc = netdev->ll_protocol->mc_hash ( AF_INET, &next_hop,
  321. ll_dest_buf ) ) !=0){
  322. DBGC ( sin_dest->sin_addr, "IPv4 could not hash "
  323. "multicast %s: %s\n",
  324. inet_ntoa ( next_hop ), strerror ( rc ) );
  325. return rc;
  326. }
  327. ll_dest = ll_dest_buf;
  328. } else {
  329. /* Unicast address */
  330. ll_dest = NULL;
  331. }
  332. /* Hand off to link layer (via ARP if applicable) */
  333. if ( ll_dest ) {
  334. if ( ( rc = net_tx ( iobuf, netdev, &ipv4_protocol, ll_dest,
  335. netdev->ll_addr ) ) != 0 ) {
  336. DBGC ( sin_dest->sin_addr, "IPv4 could not transmit "
  337. "packet via %s: %s\n",
  338. netdev->name, strerror ( rc ) );
  339. return rc;
  340. }
  341. } else {
  342. if ( ( rc = arp_tx ( iobuf, netdev, &ipv4_protocol, &next_hop,
  343. &iphdr->src, netdev->ll_addr ) ) != 0 ) {
  344. DBGC ( sin_dest->sin_addr, "IPv4 could not transmit "
  345. "packet via %s: %s\n",
  346. netdev->name, strerror ( rc ) );
  347. return rc;
  348. }
  349. }
  350. return 0;
  351. err:
  352. free_iob ( iobuf );
  353. return rc;
  354. }
  355. /**
  356. * Check if network device has any IPv4 address
  357. *
  358. * @v netdev Network device
  359. * @ret has_any_addr Network device has any IPv4 address
  360. */
  361. int ipv4_has_any_addr ( struct net_device *netdev ) {
  362. struct ipv4_miniroute *miniroute;
  363. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  364. if ( miniroute->netdev == netdev )
  365. return 1;
  366. }
  367. return 0;
  368. }
  369. /**
  370. * Check if network device has a specific IPv4 address
  371. *
  372. * @v netdev Network device
  373. * @v addr IPv4 address
  374. * @ret has_addr Network device has this IPv4 address
  375. */
  376. static int ipv4_has_addr ( struct net_device *netdev, struct in_addr addr ) {
  377. struct ipv4_miniroute *miniroute;
  378. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  379. if ( ( miniroute->netdev == netdev ) &&
  380. ( miniroute->address.s_addr == addr.s_addr ) ) {
  381. /* Found matching address */
  382. return 1;
  383. }
  384. }
  385. return 0;
  386. }
  387. /**
  388. * Process incoming packets
  389. *
  390. * @v iobuf I/O buffer
  391. * @v netdev Network device
  392. * @v ll_dest Link-layer destination address
  393. * @v ll_source Link-layer destination source
  394. * @v flags Packet flags
  395. * @ret rc Return status code
  396. *
  397. * This function expects an IP4 network datagram. It processes the headers
  398. * and sends it to the transport layer.
  399. */
  400. static int ipv4_rx ( struct io_buffer *iobuf,
  401. struct net_device *netdev,
  402. const void *ll_dest __unused,
  403. const void *ll_source __unused,
  404. unsigned int flags ) {
  405. struct iphdr *iphdr = iobuf->data;
  406. size_t hdrlen;
  407. size_t len;
  408. union {
  409. struct sockaddr_in sin;
  410. struct sockaddr_tcpip st;
  411. } src, dest;
  412. uint16_t csum;
  413. uint16_t pshdr_csum;
  414. int rc;
  415. /* Sanity check the IPv4 header */
  416. if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
  417. DBGC ( iphdr->src, "IPv4 packet too short at %zd bytes (min "
  418. "%zd bytes)\n", iob_len ( iobuf ), sizeof ( *iphdr ) );
  419. goto err;
  420. }
  421. if ( ( iphdr->verhdrlen & IP_MASK_VER ) != IP_VER ) {
  422. DBGC ( iphdr->src, "IPv4 version %#02x not supported\n",
  423. iphdr->verhdrlen );
  424. goto err;
  425. }
  426. hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
  427. if ( hdrlen < sizeof ( *iphdr ) ) {
  428. DBGC ( iphdr->src, "IPv4 header too short at %zd bytes (min "
  429. "%zd bytes)\n", hdrlen, sizeof ( *iphdr ) );
  430. goto err;
  431. }
  432. if ( hdrlen > iob_len ( iobuf ) ) {
  433. DBGC ( iphdr->src, "IPv4 header too long at %zd bytes "
  434. "(packet is %zd bytes)\n", hdrlen, iob_len ( iobuf ) );
  435. goto err;
  436. }
  437. if ( ( csum = tcpip_chksum ( iphdr, hdrlen ) ) != 0 ) {
  438. DBGC ( iphdr->src, "IPv4 checksum incorrect (is %04x "
  439. "including checksum field, should be 0000)\n", csum );
  440. goto err;
  441. }
  442. len = ntohs ( iphdr->len );
  443. if ( len < hdrlen ) {
  444. DBGC ( iphdr->src, "IPv4 length too short at %zd bytes "
  445. "(header is %zd bytes)\n", len, hdrlen );
  446. goto err;
  447. }
  448. if ( len > iob_len ( iobuf ) ) {
  449. DBGC ( iphdr->src, "IPv4 length too long at %zd bytes "
  450. "(packet is %zd bytes)\n", len, iob_len ( iobuf ) );
  451. goto err;
  452. }
  453. /* Truncate packet to correct length */
  454. iob_unput ( iobuf, ( iob_len ( iobuf ) - len ) );
  455. /* Print IPv4 header for debugging */
  456. DBGC2 ( iphdr->src, "IPv4 RX %s<-", inet_ntoa ( iphdr->dest ) );
  457. DBGC2 ( iphdr->src, "%s len %d proto %d id %04x csum %04x\n",
  458. inet_ntoa ( iphdr->src ), ntohs ( iphdr->len ), iphdr->protocol,
  459. ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );
  460. /* Discard unicast packets not destined for us */
  461. if ( ( ! ( flags & LL_MULTICAST ) ) &&
  462. ipv4_has_any_addr ( netdev ) &&
  463. ( ! ipv4_has_addr ( netdev, iphdr->dest ) ) ) {
  464. DBGC ( iphdr->src, "IPv4 discarding non-local unicast packet "
  465. "for %s\n", inet_ntoa ( iphdr->dest ) );
  466. goto err;
  467. }
  468. /* Perform fragment reassembly if applicable */
  469. if ( iphdr->frags & htons ( IP_MASK_OFFSET | IP_MASK_MOREFRAGS ) ) {
  470. /* Pass the fragment to ipv4_reassemble() which returns
  471. * either a fully reassembled I/O buffer or NULL.
  472. */
  473. iobuf = ipv4_reassemble ( iobuf );
  474. if ( ! iobuf )
  475. return 0;
  476. iphdr = iobuf->data;
  477. hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
  478. }
  479. /* Construct socket addresses, calculate pseudo-header
  480. * checksum, and hand off to transport layer
  481. */
  482. memset ( &src, 0, sizeof ( src ) );
  483. src.sin.sin_family = AF_INET;
  484. src.sin.sin_addr = iphdr->src;
  485. memset ( &dest, 0, sizeof ( dest ) );
  486. dest.sin.sin_family = AF_INET;
  487. dest.sin.sin_addr = iphdr->dest;
  488. pshdr_csum = ipv4_pshdr_chksum ( iobuf, TCPIP_EMPTY_CSUM );
  489. iob_pull ( iobuf, hdrlen );
  490. if ( ( rc = tcpip_rx ( iobuf, iphdr->protocol, &src.st,
  491. &dest.st, pshdr_csum ) ) != 0 ) {
  492. DBGC ( src.sin.sin_addr, "IPv4 received packet rejected by "
  493. "stack: %s\n", strerror ( rc ) );
  494. return rc;
  495. }
  496. return 0;
  497. err:
  498. free_iob ( iobuf );
  499. return -EINVAL;
  500. }
  501. /**
  502. * Check existence of IPv4 address for ARP
  503. *
  504. * @v netdev Network device
  505. * @v net_addr Network-layer address
  506. * @ret rc Return status code
  507. */
  508. static int ipv4_arp_check ( struct net_device *netdev, const void *net_addr ) {
  509. const struct in_addr *address = net_addr;
  510. if ( ipv4_has_addr ( netdev, *address ) )
  511. return 0;
  512. return -ENOENT;
  513. }
  514. /**
  515. * Convert IPv4 address to dotted-quad notation
  516. *
  517. * @v in IP address
  518. * @ret string IP address in dotted-quad notation
  519. */
  520. char * inet_ntoa ( struct in_addr in ) {
  521. static char buf[16]; /* "xxx.xxx.xxx.xxx" */
  522. uint8_t *bytes = ( uint8_t * ) &in;
  523. sprintf ( buf, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3] );
  524. return buf;
  525. }
  526. /**
  527. * Transcribe IP address
  528. *
  529. * @v net_addr IP address
  530. * @ret string IP address in dotted-quad notation
  531. *
  532. */
  533. static const char * ipv4_ntoa ( const void *net_addr ) {
  534. return inet_ntoa ( * ( ( struct in_addr * ) net_addr ) );
  535. }
  536. /** IPv4 protocol */
  537. struct net_protocol ipv4_protocol __net_protocol = {
  538. .name = "IP",
  539. .net_proto = htons ( ETH_P_IP ),
  540. .net_addr_len = sizeof ( struct in_addr ),
  541. .rx = ipv4_rx,
  542. .ntoa = ipv4_ntoa,
  543. };
  544. /** IPv4 TCPIP net protocol */
  545. struct tcpip_net_protocol ipv4_tcpip_protocol __tcpip_net_protocol = {
  546. .name = "IPv4",
  547. .sa_family = AF_INET,
  548. .tx = ipv4_tx,
  549. };
  550. /** IPv4 ARP protocol */
  551. struct arp_net_protocol ipv4_arp_protocol __arp_net_protocol = {
  552. .net_protocol = &ipv4_protocol,
  553. .check = ipv4_arp_check,
  554. };
  555. /******************************************************************************
  556. *
  557. * Settings
  558. *
  559. ******************************************************************************
  560. */
  561. /** IPv4 address setting */
  562. struct setting ip_setting __setting ( SETTING_IPv4 ) = {
  563. .name = "ip",
  564. .description = "IP address",
  565. .tag = DHCP_EB_YIADDR,
  566. .type = &setting_type_ipv4,
  567. };
  568. /** IPv4 subnet mask setting */
  569. struct setting netmask_setting __setting ( SETTING_IPv4 ) = {
  570. .name = "netmask",
  571. .description = "Subnet mask",
  572. .tag = DHCP_SUBNET_MASK,
  573. .type = &setting_type_ipv4,
  574. };
  575. /** Default gateway setting */
  576. struct setting gateway_setting __setting ( SETTING_IPv4 ) = {
  577. .name = "gateway",
  578. .description = "Default gateway",
  579. .tag = DHCP_ROUTERS,
  580. .type = &setting_type_ipv4,
  581. };
  582. /**
  583. * Create IPv4 routing table based on configured settings
  584. *
  585. * @ret rc Return status code
  586. */
  587. static int ipv4_create_routes ( void ) {
  588. struct ipv4_miniroute *miniroute;
  589. struct ipv4_miniroute *tmp;
  590. struct net_device *netdev;
  591. struct settings *settings;
  592. struct in_addr address = { 0 };
  593. struct in_addr netmask = { 0 };
  594. struct in_addr gateway = { 0 };
  595. /* Delete all existing routes */
  596. list_for_each_entry_safe ( miniroute, tmp, &ipv4_miniroutes, list )
  597. del_ipv4_miniroute ( miniroute );
  598. /* Create a route for each configured network device */
  599. for_each_netdev ( netdev ) {
  600. settings = netdev_settings ( netdev );
  601. /* Get IPv4 address */
  602. address.s_addr = 0;
  603. fetch_ipv4_setting ( settings, &ip_setting, &address );
  604. if ( ! address.s_addr )
  605. continue;
  606. /* Get subnet mask */
  607. fetch_ipv4_setting ( settings, &netmask_setting, &netmask );
  608. /* Calculate default netmask, if necessary */
  609. if ( ! netmask.s_addr ) {
  610. if ( IN_CLASSA ( ntohl ( address.s_addr ) ) ) {
  611. netmask.s_addr = htonl ( IN_CLASSA_NET );
  612. } else if ( IN_CLASSB ( ntohl ( address.s_addr ) ) ) {
  613. netmask.s_addr = htonl ( IN_CLASSB_NET );
  614. } else if ( IN_CLASSC ( ntohl ( address.s_addr ) ) ) {
  615. netmask.s_addr = htonl ( IN_CLASSC_NET );
  616. }
  617. }
  618. /* Get default gateway, if present */
  619. fetch_ipv4_setting ( settings, &gateway_setting, &gateway );
  620. /* Configure route */
  621. miniroute = add_ipv4_miniroute ( netdev, address,
  622. netmask, gateway );
  623. if ( ! miniroute )
  624. return -ENOMEM;
  625. }
  626. return 0;
  627. }
  628. /** IPv4 settings applicator */
  629. struct settings_applicator ipv4_settings_applicator __settings_applicator = {
  630. .apply = ipv4_create_routes,
  631. };
  632. /* Drag in ICMP */
  633. REQUIRE_OBJECT ( icmp );