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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. * Copyright (C) 2006 Nikhil Chandru Rao
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. #include <string.h>
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <byteswap.h>
  26. #include <ipxe/list.h>
  27. #include <ipxe/in.h>
  28. #include <ipxe/arp.h>
  29. #include <ipxe/if_ether.h>
  30. #include <ipxe/iobuf.h>
  31. #include <ipxe/netdevice.h>
  32. #include <ipxe/ip.h>
  33. #include <ipxe/tcpip.h>
  34. #include <ipxe/dhcp.h>
  35. #include <ipxe/settings.h>
  36. #include <ipxe/fragment.h>
  37. #include <ipxe/ipstat.h>
  38. #include <ipxe/profile.h>
  39. /** @file
  40. *
  41. * IPv4 protocol
  42. *
  43. */
  44. FILE_LICENCE ( GPL2_OR_LATER );
  45. /* Unique IP datagram identification number (high byte) */
  46. static uint8_t next_ident_high = 0;
  47. /** List of IPv4 miniroutes */
  48. struct list_head ipv4_miniroutes = LIST_HEAD_INIT ( ipv4_miniroutes );
  49. /** IPv4 statistics */
  50. static struct ip_statistics ipv4_stats;
  51. /** IPv4 statistics family */
  52. struct ip_statistics_family
  53. ipv4_stats_family __ip_statistics_family ( IP_STATISTICS_IPV4 ) = {
  54. .version = 4,
  55. .stats = &ipv4_stats,
  56. };
  57. /** Transmit profiler */
  58. static struct profiler ipv4_tx_profiler __profiler = { .name = "ipv4.tx" };
  59. /** Receive profiler */
  60. static struct profiler ipv4_rx_profiler __profiler = { .name = "ipv4.rx" };
  61. /**
  62. * Add IPv4 minirouting table entry
  63. *
  64. * @v netdev Network device
  65. * @v address IPv4 address
  66. * @v netmask Subnet mask
  67. * @v gateway Gateway address (if any)
  68. * @ret miniroute Routing table entry, or NULL
  69. */
  70. static struct ipv4_miniroute * __malloc
  71. add_ipv4_miniroute ( struct net_device *netdev, struct in_addr address,
  72. struct in_addr netmask, struct in_addr gateway ) {
  73. struct ipv4_miniroute *miniroute;
  74. DBGC ( netdev, "IPv4 add %s", inet_ntoa ( address ) );
  75. DBGC ( netdev, "/%s ", inet_ntoa ( netmask ) );
  76. if ( gateway.s_addr )
  77. DBGC ( netdev, "gw %s ", inet_ntoa ( gateway ) );
  78. DBGC ( netdev, "via %s\n", netdev->name );
  79. /* Allocate and populate miniroute structure */
  80. miniroute = malloc ( sizeof ( *miniroute ) );
  81. if ( ! miniroute ) {
  82. DBGC ( netdev, "IPv4 could not add miniroute\n" );
  83. return NULL;
  84. }
  85. /* Record routing information */
  86. miniroute->netdev = netdev_get ( netdev );
  87. miniroute->address = address;
  88. miniroute->netmask = netmask;
  89. miniroute->gateway = gateway;
  90. /* Add to end of list if we have a gateway, otherwise
  91. * to start of list.
  92. */
  93. if ( gateway.s_addr ) {
  94. list_add_tail ( &miniroute->list, &ipv4_miniroutes );
  95. } else {
  96. list_add ( &miniroute->list, &ipv4_miniroutes );
  97. }
  98. return miniroute;
  99. }
  100. /**
  101. * Delete IPv4 minirouting table entry
  102. *
  103. * @v miniroute Routing table entry
  104. */
  105. static void del_ipv4_miniroute ( struct ipv4_miniroute *miniroute ) {
  106. struct net_device *netdev = miniroute->netdev;
  107. DBGC ( netdev, "IPv4 del %s", inet_ntoa ( miniroute->address ) );
  108. DBGC ( netdev, "/%s ", inet_ntoa ( miniroute->netmask ) );
  109. if ( miniroute->gateway.s_addr )
  110. DBGC ( netdev, "gw %s ", inet_ntoa ( miniroute->gateway ) );
  111. DBGC ( netdev, "via %s\n", miniroute->netdev->name );
  112. netdev_put ( miniroute->netdev );
  113. list_del ( &miniroute->list );
  114. free ( miniroute );
  115. }
  116. /**
  117. * Perform IPv4 routing
  118. *
  119. * @v dest Final destination address
  120. * @ret dest Next hop destination address
  121. * @ret miniroute Routing table entry to use, or NULL if no route
  122. *
  123. * If the route requires use of a gateway, the next hop destination
  124. * address will be overwritten with the gateway address.
  125. */
  126. static struct ipv4_miniroute * ipv4_route ( struct in_addr *dest ) {
  127. struct ipv4_miniroute *miniroute;
  128. int local;
  129. int has_gw;
  130. /* Find first usable route in routing table */
  131. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  132. if ( ! netdev_is_open ( miniroute->netdev ) )
  133. continue;
  134. local = ( ( ( dest->s_addr ^ miniroute->address.s_addr )
  135. & miniroute->netmask.s_addr ) == 0 );
  136. has_gw = ( miniroute->gateway.s_addr );
  137. if ( local || has_gw ) {
  138. if ( ! local )
  139. *dest = miniroute->gateway;
  140. return miniroute;
  141. }
  142. }
  143. return NULL;
  144. }
  145. /**
  146. * Determine transmitting network device
  147. *
  148. * @v st_dest Destination network-layer address
  149. * @ret netdev Transmitting network device, or NULL
  150. */
  151. static struct net_device * ipv4_netdev ( struct sockaddr_tcpip *st_dest ) {
  152. struct sockaddr_in *sin_dest = ( ( struct sockaddr_in * ) st_dest );
  153. struct in_addr dest = sin_dest->sin_addr;
  154. struct ipv4_miniroute *miniroute;
  155. /* Find routing table entry */
  156. miniroute = ipv4_route ( &dest );
  157. if ( ! miniroute )
  158. return NULL;
  159. return miniroute->netdev;
  160. }
  161. /**
  162. * Check if IPv4 fragment matches fragment reassembly buffer
  163. *
  164. * @v fragment Fragment reassembly buffer
  165. * @v iobuf I/O buffer
  166. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  167. * @ret is_fragment Fragment matches this reassembly buffer
  168. */
  169. static int ipv4_is_fragment ( struct fragment *fragment,
  170. struct io_buffer *iobuf,
  171. size_t hdrlen __unused ) {
  172. struct iphdr *frag_iphdr = fragment->iobuf->data;
  173. struct iphdr *iphdr = iobuf->data;
  174. return ( ( iphdr->src.s_addr == frag_iphdr->src.s_addr ) &&
  175. ( iphdr->ident == frag_iphdr->ident ) );
  176. }
  177. /**
  178. * Get IPv4 fragment offset
  179. *
  180. * @v iobuf I/O buffer
  181. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  182. * @ret offset Offset
  183. */
  184. static size_t ipv4_fragment_offset ( struct io_buffer *iobuf,
  185. size_t hdrlen __unused ) {
  186. struct iphdr *iphdr = iobuf->data;
  187. return ( ( ntohs ( iphdr->frags ) & IP_MASK_OFFSET ) << 3 );
  188. }
  189. /**
  190. * Check if more fragments exist
  191. *
  192. * @v iobuf I/O buffer
  193. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  194. * @ret more_frags More fragments exist
  195. */
  196. static int ipv4_more_fragments ( struct io_buffer *iobuf,
  197. size_t hdrlen __unused ) {
  198. struct iphdr *iphdr = iobuf->data;
  199. return ( iphdr->frags & htons ( IP_MASK_MOREFRAGS ) );
  200. }
  201. /** IPv4 fragment reassembler */
  202. static struct fragment_reassembler ipv4_reassembler = {
  203. .list = LIST_HEAD_INIT ( ipv4_reassembler.list ),
  204. .is_fragment = ipv4_is_fragment,
  205. .fragment_offset = ipv4_fragment_offset,
  206. .more_fragments = ipv4_more_fragments,
  207. .stats = &ipv4_stats,
  208. };
  209. /**
  210. * Add IPv4 pseudo-header checksum to existing checksum
  211. *
  212. * @v iobuf I/O buffer
  213. * @v csum Existing checksum
  214. * @ret csum Updated checksum
  215. */
  216. static uint16_t ipv4_pshdr_chksum ( struct io_buffer *iobuf, uint16_t csum ) {
  217. struct ipv4_pseudo_header pshdr;
  218. struct iphdr *iphdr = iobuf->data;
  219. size_t hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
  220. /* Build pseudo-header */
  221. pshdr.src = iphdr->src;
  222. pshdr.dest = iphdr->dest;
  223. pshdr.zero_padding = 0x00;
  224. pshdr.protocol = iphdr->protocol;
  225. pshdr.len = htons ( iob_len ( iobuf ) - hdrlen );
  226. /* Update the checksum value */
  227. return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
  228. }
  229. /**
  230. * Transmit IP packet
  231. *
  232. * @v iobuf I/O buffer
  233. * @v tcpip Transport-layer protocol
  234. * @v st_src Source network-layer address
  235. * @v st_dest Destination network-layer address
  236. * @v netdev Network device to use if no route found, or NULL
  237. * @v trans_csum Transport-layer checksum to complete, or NULL
  238. * @ret rc Status
  239. *
  240. * This function expects a transport-layer segment and prepends the IP header
  241. */
  242. static int ipv4_tx ( struct io_buffer *iobuf,
  243. struct tcpip_protocol *tcpip_protocol,
  244. struct sockaddr_tcpip *st_src,
  245. struct sockaddr_tcpip *st_dest,
  246. struct net_device *netdev,
  247. uint16_t *trans_csum ) {
  248. struct iphdr *iphdr = iob_push ( iobuf, sizeof ( *iphdr ) );
  249. struct sockaddr_in *sin_src = ( ( struct sockaddr_in * ) st_src );
  250. struct sockaddr_in *sin_dest = ( ( struct sockaddr_in * ) st_dest );
  251. struct ipv4_miniroute *miniroute;
  252. struct in_addr next_hop;
  253. struct in_addr netmask = { .s_addr = 0 };
  254. uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
  255. const void *ll_dest;
  256. int rc;
  257. /* Start profiling */
  258. profile_start ( &ipv4_tx_profiler );
  259. /* Update statistics */
  260. ipv4_stats.out_requests++;
  261. /* Fill up the IP header, except source address */
  262. memset ( iphdr, 0, sizeof ( *iphdr ) );
  263. iphdr->verhdrlen = ( IP_VER | ( sizeof ( *iphdr ) / 4 ) );
  264. iphdr->service = IP_TOS;
  265. iphdr->len = htons ( iob_len ( iobuf ) );
  266. iphdr->ttl = IP_TTL;
  267. iphdr->protocol = tcpip_protocol->tcpip_proto;
  268. iphdr->dest = sin_dest->sin_addr;
  269. /* Use routing table to identify next hop and transmitting netdev */
  270. next_hop = iphdr->dest;
  271. if ( sin_src )
  272. iphdr->src = sin_src->sin_addr;
  273. if ( ( next_hop.s_addr != INADDR_BROADCAST ) &&
  274. ( ! IN_MULTICAST ( ntohl ( next_hop.s_addr ) ) ) &&
  275. ( ( miniroute = ipv4_route ( &next_hop ) ) != NULL ) ) {
  276. iphdr->src = miniroute->address;
  277. netmask = miniroute->netmask;
  278. netdev = miniroute->netdev;
  279. }
  280. if ( ! netdev ) {
  281. DBGC ( sin_dest->sin_addr, "IPv4 has no route to %s\n",
  282. inet_ntoa ( iphdr->dest ) );
  283. ipv4_stats.out_no_routes++;
  284. rc = -ENETUNREACH;
  285. goto err;
  286. }
  287. /* (Ab)use the "ident" field to convey metadata about the
  288. * network device statistics into packet traces. Useful for
  289. * extracting debug information from non-debug builds.
  290. */
  291. iphdr->ident = htons ( ( (++next_ident_high) << 8 ) |
  292. ( ( netdev->rx_stats.bad & 0xf ) << 4 ) |
  293. ( ( netdev->rx_stats.good & 0xf ) << 0 ) );
  294. /* Fix up checksums */
  295. if ( trans_csum )
  296. *trans_csum = ipv4_pshdr_chksum ( iobuf, *trans_csum );
  297. iphdr->chksum = tcpip_chksum ( iphdr, sizeof ( *iphdr ) );
  298. /* Print IP4 header for debugging */
  299. DBGC2 ( sin_dest->sin_addr, "IPv4 TX %s->", inet_ntoa ( iphdr->src ) );
  300. DBGC2 ( sin_dest->sin_addr, "%s len %d proto %d id %04x csum %04x\n",
  301. inet_ntoa ( iphdr->dest ), ntohs ( iphdr->len ),
  302. iphdr->protocol, ntohs ( iphdr->ident ),
  303. ntohs ( iphdr->chksum ) );
  304. /* Calculate link-layer destination address, if possible */
  305. if ( ( ( next_hop.s_addr ^ INADDR_BROADCAST ) & ~netmask.s_addr ) == 0){
  306. /* Broadcast address */
  307. ipv4_stats.out_bcast_pkts++;
  308. ll_dest = netdev->ll_broadcast;
  309. } else if ( IN_MULTICAST ( ntohl ( next_hop.s_addr ) ) ) {
  310. /* Multicast address */
  311. ipv4_stats.out_mcast_pkts++;
  312. if ( ( rc = netdev->ll_protocol->mc_hash ( AF_INET, &next_hop,
  313. ll_dest_buf ) ) !=0){
  314. DBGC ( sin_dest->sin_addr, "IPv4 could not hash "
  315. "multicast %s: %s\n",
  316. inet_ntoa ( next_hop ), strerror ( rc ) );
  317. goto err;
  318. }
  319. ll_dest = ll_dest_buf;
  320. } else {
  321. /* Unicast address */
  322. ll_dest = NULL;
  323. }
  324. /* Update statistics */
  325. ipv4_stats.out_transmits++;
  326. ipv4_stats.out_octets += iob_len ( iobuf );
  327. /* Hand off to link layer (via ARP if applicable) */
  328. if ( ll_dest ) {
  329. if ( ( rc = net_tx ( iobuf, netdev, &ipv4_protocol, ll_dest,
  330. netdev->ll_addr ) ) != 0 ) {
  331. DBGC ( sin_dest->sin_addr, "IPv4 could not transmit "
  332. "packet via %s: %s\n",
  333. netdev->name, strerror ( rc ) );
  334. return rc;
  335. }
  336. } else {
  337. if ( ( rc = arp_tx ( iobuf, netdev, &ipv4_protocol, &next_hop,
  338. &iphdr->src, netdev->ll_addr ) ) != 0 ) {
  339. DBGC ( sin_dest->sin_addr, "IPv4 could not transmit "
  340. "packet via %s: %s\n",
  341. netdev->name, strerror ( rc ) );
  342. return rc;
  343. }
  344. }
  345. profile_stop ( &ipv4_tx_profiler );
  346. return 0;
  347. err:
  348. free_iob ( iobuf );
  349. return rc;
  350. }
  351. /**
  352. * Check if network device has any IPv4 address
  353. *
  354. * @v netdev Network device
  355. * @ret has_any_addr Network device has any IPv4 address
  356. */
  357. int ipv4_has_any_addr ( struct net_device *netdev ) {
  358. struct ipv4_miniroute *miniroute;
  359. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  360. if ( miniroute->netdev == netdev )
  361. return 1;
  362. }
  363. return 0;
  364. }
  365. /**
  366. * Check if network device has a specific IPv4 address
  367. *
  368. * @v netdev Network device
  369. * @v addr IPv4 address
  370. * @ret has_addr Network device has this IPv4 address
  371. */
  372. static int ipv4_has_addr ( struct net_device *netdev, struct in_addr addr ) {
  373. struct ipv4_miniroute *miniroute;
  374. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  375. if ( ( miniroute->netdev == netdev ) &&
  376. ( miniroute->address.s_addr == addr.s_addr ) ) {
  377. /* Found matching address */
  378. return 1;
  379. }
  380. }
  381. return 0;
  382. }
  383. /**
  384. * Process incoming packets
  385. *
  386. * @v iobuf I/O buffer
  387. * @v netdev Network device
  388. * @v ll_dest Link-layer destination address
  389. * @v ll_source Link-layer destination source
  390. * @v flags Packet flags
  391. * @ret rc Return status code
  392. *
  393. * This function expects an IP4 network datagram. It processes the headers
  394. * and sends it to the transport layer.
  395. */
  396. static int ipv4_rx ( struct io_buffer *iobuf,
  397. struct net_device *netdev,
  398. const void *ll_dest __unused,
  399. const void *ll_source __unused,
  400. unsigned int flags ) {
  401. struct iphdr *iphdr = iobuf->data;
  402. size_t hdrlen;
  403. size_t len;
  404. union {
  405. struct sockaddr_in sin;
  406. struct sockaddr_tcpip st;
  407. } src, dest;
  408. uint16_t csum;
  409. uint16_t pshdr_csum;
  410. int rc;
  411. /* Start profiling */
  412. profile_start ( &ipv4_rx_profiler );
  413. /* Update statistics */
  414. ipv4_stats.in_receives++;
  415. ipv4_stats.in_octets += iob_len ( iobuf );
  416. if ( flags & LL_BROADCAST ) {
  417. ipv4_stats.in_bcast_pkts++;
  418. } else if ( flags & LL_MULTICAST ) {
  419. ipv4_stats.in_mcast_pkts++;
  420. }
  421. /* Sanity check the IPv4 header */
  422. if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
  423. DBGC ( iphdr->src, "IPv4 packet too short at %zd bytes (min "
  424. "%zd bytes)\n", iob_len ( iobuf ), sizeof ( *iphdr ) );
  425. goto err_header;
  426. }
  427. if ( ( iphdr->verhdrlen & IP_MASK_VER ) != IP_VER ) {
  428. DBGC ( iphdr->src, "IPv4 version %#02x not supported\n",
  429. iphdr->verhdrlen );
  430. goto err_header;
  431. }
  432. hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
  433. if ( hdrlen < sizeof ( *iphdr ) ) {
  434. DBGC ( iphdr->src, "IPv4 header too short at %zd bytes (min "
  435. "%zd bytes)\n", hdrlen, sizeof ( *iphdr ) );
  436. goto err_header;
  437. }
  438. if ( hdrlen > iob_len ( iobuf ) ) {
  439. DBGC ( iphdr->src, "IPv4 header too long at %zd bytes "
  440. "(packet is %zd bytes)\n", hdrlen, iob_len ( iobuf ) );
  441. goto err_header;
  442. }
  443. if ( ( csum = tcpip_chksum ( iphdr, hdrlen ) ) != 0 ) {
  444. DBGC ( iphdr->src, "IPv4 checksum incorrect (is %04x "
  445. "including checksum field, should be 0000)\n", csum );
  446. goto err_header;
  447. }
  448. len = ntohs ( iphdr->len );
  449. if ( len < hdrlen ) {
  450. DBGC ( iphdr->src, "IPv4 length too short at %zd bytes "
  451. "(header is %zd bytes)\n", len, hdrlen );
  452. goto err_header;
  453. }
  454. if ( len > iob_len ( iobuf ) ) {
  455. DBGC ( iphdr->src, "IPv4 length too long at %zd bytes "
  456. "(packet is %zd bytes)\n", len, iob_len ( iobuf ) );
  457. ipv4_stats.in_truncated_pkts++;
  458. goto err_other;
  459. }
  460. /* Truncate packet to correct length */
  461. iob_unput ( iobuf, ( iob_len ( iobuf ) - len ) );
  462. /* Print IPv4 header for debugging */
  463. DBGC2 ( iphdr->src, "IPv4 RX %s<-", inet_ntoa ( iphdr->dest ) );
  464. DBGC2 ( iphdr->src, "%s len %d proto %d id %04x csum %04x\n",
  465. inet_ntoa ( iphdr->src ), ntohs ( iphdr->len ), iphdr->protocol,
  466. ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );
  467. /* Discard unicast packets not destined for us */
  468. if ( ( ! ( flags & LL_MULTICAST ) ) &&
  469. ipv4_has_any_addr ( netdev ) &&
  470. ( ! ipv4_has_addr ( netdev, iphdr->dest ) ) ) {
  471. DBGC ( iphdr->src, "IPv4 discarding non-local unicast packet "
  472. "for %s\n", inet_ntoa ( iphdr->dest ) );
  473. ipv4_stats.in_addr_errors++;
  474. goto err_other;
  475. }
  476. /* Perform fragment reassembly if applicable */
  477. if ( iphdr->frags & htons ( IP_MASK_OFFSET | IP_MASK_MOREFRAGS ) ) {
  478. /* Pass the fragment to fragment_reassemble() which returns
  479. * either a fully reassembled I/O buffer or NULL.
  480. */
  481. iobuf = fragment_reassemble ( &ipv4_reassembler, iobuf,
  482. &hdrlen );
  483. if ( ! iobuf )
  484. return 0;
  485. iphdr = iobuf->data;
  486. }
  487. /* Construct socket addresses, calculate pseudo-header
  488. * checksum, and hand off to transport layer
  489. */
  490. memset ( &src, 0, sizeof ( src ) );
  491. src.sin.sin_family = AF_INET;
  492. src.sin.sin_addr = iphdr->src;
  493. memset ( &dest, 0, sizeof ( dest ) );
  494. dest.sin.sin_family = AF_INET;
  495. dest.sin.sin_addr = iphdr->dest;
  496. pshdr_csum = ipv4_pshdr_chksum ( iobuf, TCPIP_EMPTY_CSUM );
  497. iob_pull ( iobuf, hdrlen );
  498. if ( ( rc = tcpip_rx ( iobuf, netdev, iphdr->protocol, &src.st,
  499. &dest.st, pshdr_csum, &ipv4_stats ) ) != 0 ) {
  500. DBGC ( src.sin.sin_addr, "IPv4 received packet rejected by "
  501. "stack: %s\n", strerror ( rc ) );
  502. return rc;
  503. }
  504. profile_stop ( &ipv4_rx_profiler );
  505. return 0;
  506. err_header:
  507. ipv4_stats.in_hdr_errors++;
  508. err_other:
  509. free_iob ( iobuf );
  510. return -EINVAL;
  511. }
  512. /**
  513. * Check existence of IPv4 address for ARP
  514. *
  515. * @v netdev Network device
  516. * @v net_addr Network-layer address
  517. * @ret rc Return status code
  518. */
  519. static int ipv4_arp_check ( struct net_device *netdev, const void *net_addr ) {
  520. const struct in_addr *address = net_addr;
  521. if ( ipv4_has_addr ( netdev, *address ) )
  522. return 0;
  523. return -ENOENT;
  524. }
  525. /**
  526. * Parse IPv4 address
  527. *
  528. * @v string IPv4 address string
  529. * @ret in IPv4 address to fill in
  530. * @ret ok IPv4 address is valid
  531. *
  532. * Note that this function returns nonzero iff the address is valid,
  533. * to match the standard BSD API function of the same name. Unlike
  534. * most other iPXE functions, a zero therefore indicates failure.
  535. */
  536. int inet_aton ( const char *string, struct in_addr *in ) {
  537. const char *separator = "...";
  538. uint8_t *byte = ( ( uint8_t * ) in );
  539. char *endp;
  540. unsigned long value;
  541. while ( 1 ) {
  542. value = strtoul ( string, &endp, 0 );
  543. if ( string == endp )
  544. return 0;
  545. if ( value > 0xff )
  546. return 0;
  547. *(byte++) = value;
  548. if ( *endp != *separator )
  549. return 0;
  550. if ( ! *(separator++) )
  551. return 1;
  552. string = ( endp + 1 );
  553. }
  554. }
  555. /**
  556. * Convert IPv4 address to dotted-quad notation
  557. *
  558. * @v in IPv4 address
  559. * @ret string IPv4 address in dotted-quad notation
  560. */
  561. char * inet_ntoa ( struct in_addr in ) {
  562. static char buf[16]; /* "xxx.xxx.xxx.xxx" */
  563. uint8_t *bytes = ( uint8_t * ) &in;
  564. sprintf ( buf, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3] );
  565. return buf;
  566. }
  567. /**
  568. * Transcribe IPv4 address
  569. *
  570. * @v net_addr IPv4 address
  571. * @ret string IPv4 address in dotted-quad notation
  572. *
  573. */
  574. static const char * ipv4_ntoa ( const void *net_addr ) {
  575. return inet_ntoa ( * ( ( struct in_addr * ) net_addr ) );
  576. }
  577. /**
  578. * Transcribe IPv4 socket address
  579. *
  580. * @v sa Socket address
  581. * @ret string Socket address in standard notation
  582. */
  583. static const char * ipv4_sock_ntoa ( struct sockaddr *sa ) {
  584. struct sockaddr_in *sin = ( ( struct sockaddr_in * ) sa );
  585. return inet_ntoa ( sin->sin_addr );
  586. }
  587. /**
  588. * Parse IPv4 socket address
  589. *
  590. * @v string Socket address string
  591. * @v sa Socket address to fill in
  592. * @ret rc Return status code
  593. */
  594. static int ipv4_sock_aton ( const char *string, struct sockaddr *sa ) {
  595. struct sockaddr_in *sin = ( ( struct sockaddr_in * ) sa );
  596. struct in_addr in;
  597. if ( inet_aton ( string, &in ) ) {
  598. sin->sin_addr = in;
  599. return 0;
  600. }
  601. return -EINVAL;
  602. }
  603. /** IPv4 protocol */
  604. struct net_protocol ipv4_protocol __net_protocol = {
  605. .name = "IP",
  606. .net_proto = htons ( ETH_P_IP ),
  607. .net_addr_len = sizeof ( struct in_addr ),
  608. .rx = ipv4_rx,
  609. .ntoa = ipv4_ntoa,
  610. };
  611. /** IPv4 TCPIP net protocol */
  612. struct tcpip_net_protocol ipv4_tcpip_protocol __tcpip_net_protocol = {
  613. .name = "IPv4",
  614. .sa_family = AF_INET,
  615. .header_len = sizeof ( struct iphdr ),
  616. .tx = ipv4_tx,
  617. .netdev = ipv4_netdev,
  618. };
  619. /** IPv4 ARP protocol */
  620. struct arp_net_protocol ipv4_arp_protocol __arp_net_protocol = {
  621. .net_protocol = &ipv4_protocol,
  622. .check = ipv4_arp_check,
  623. };
  624. /** IPv4 socket address converter */
  625. struct sockaddr_converter ipv4_sockaddr_converter __sockaddr_converter = {
  626. .family = AF_INET,
  627. .ntoa = ipv4_sock_ntoa,
  628. .aton = ipv4_sock_aton,
  629. };
  630. /******************************************************************************
  631. *
  632. * Settings
  633. *
  634. ******************************************************************************
  635. */
  636. /**
  637. * Parse IPv4 address setting value
  638. *
  639. * @v type Setting type
  640. * @v value Formatted setting value
  641. * @v buf Buffer to contain raw value
  642. * @v len Length of buffer
  643. * @ret len Length of raw value, or negative error
  644. */
  645. int parse_ipv4_setting ( const struct setting_type *type __unused,
  646. const char *value, void *buf, size_t len ) {
  647. struct in_addr ipv4;
  648. /* Parse IPv4 address */
  649. if ( inet_aton ( value, &ipv4 ) == 0 )
  650. return -EINVAL;
  651. /* Copy to buffer */
  652. if ( len > sizeof ( ipv4 ) )
  653. len = sizeof ( ipv4 );
  654. memcpy ( buf, &ipv4, len );
  655. return ( sizeof ( ipv4 ) );
  656. }
  657. /**
  658. * Format IPv4 address setting value
  659. *
  660. * @v type Setting type
  661. * @v raw Raw setting value
  662. * @v raw_len Length of raw setting value
  663. * @v buf Buffer to contain formatted value
  664. * @v len Length of buffer
  665. * @ret len Length of formatted value, or negative error
  666. */
  667. int format_ipv4_setting ( const struct setting_type *type __unused,
  668. const void *raw, size_t raw_len, char *buf,
  669. size_t len ) {
  670. const struct in_addr *ipv4 = raw;
  671. if ( raw_len < sizeof ( *ipv4 ) )
  672. return -EINVAL;
  673. return snprintf ( buf, len, "%s", inet_ntoa ( *ipv4 ) );
  674. }
  675. /** IPv4 address setting */
  676. const struct setting ip_setting __setting ( SETTING_IP, ip ) = {
  677. .name = "ip",
  678. .description = "IP address",
  679. .tag = DHCP_EB_YIADDR,
  680. .type = &setting_type_ipv4,
  681. };
  682. /** IPv4 subnet mask setting */
  683. const struct setting netmask_setting __setting ( SETTING_IP, netmask ) = {
  684. .name = "netmask",
  685. .description = "Subnet mask",
  686. .tag = DHCP_SUBNET_MASK,
  687. .type = &setting_type_ipv4,
  688. };
  689. /** Default gateway setting */
  690. const struct setting gateway_setting __setting ( SETTING_IP, gateway ) = {
  691. .name = "gateway",
  692. .description = "Default gateway",
  693. .tag = DHCP_ROUTERS,
  694. .type = &setting_type_ipv4,
  695. };
  696. /**
  697. * Create IPv4 routing table based on configured settings
  698. *
  699. * @ret rc Return status code
  700. */
  701. static int ipv4_create_routes ( void ) {
  702. struct ipv4_miniroute *miniroute;
  703. struct ipv4_miniroute *tmp;
  704. struct net_device *netdev;
  705. struct settings *settings;
  706. struct in_addr address = { 0 };
  707. struct in_addr netmask = { 0 };
  708. struct in_addr gateway = { 0 };
  709. /* Delete all existing routes */
  710. list_for_each_entry_safe ( miniroute, tmp, &ipv4_miniroutes, list )
  711. del_ipv4_miniroute ( miniroute );
  712. /* Create a route for each configured network device */
  713. for_each_netdev ( netdev ) {
  714. settings = netdev_settings ( netdev );
  715. /* Get IPv4 address */
  716. address.s_addr = 0;
  717. fetch_ipv4_setting ( settings, &ip_setting, &address );
  718. if ( ! address.s_addr )
  719. continue;
  720. /* Get subnet mask */
  721. fetch_ipv4_setting ( settings, &netmask_setting, &netmask );
  722. /* Calculate default netmask, if necessary */
  723. if ( ! netmask.s_addr ) {
  724. if ( IN_CLASSA ( ntohl ( address.s_addr ) ) ) {
  725. netmask.s_addr = htonl ( IN_CLASSA_NET );
  726. } else if ( IN_CLASSB ( ntohl ( address.s_addr ) ) ) {
  727. netmask.s_addr = htonl ( IN_CLASSB_NET );
  728. } else if ( IN_CLASSC ( ntohl ( address.s_addr ) ) ) {
  729. netmask.s_addr = htonl ( IN_CLASSC_NET );
  730. }
  731. }
  732. /* Get default gateway, if present */
  733. fetch_ipv4_setting ( settings, &gateway_setting, &gateway );
  734. /* Configure route */
  735. miniroute = add_ipv4_miniroute ( netdev, address,
  736. netmask, gateway );
  737. if ( ! miniroute )
  738. return -ENOMEM;
  739. }
  740. return 0;
  741. }
  742. /** IPv4 settings applicator */
  743. struct settings_applicator ipv4_settings_applicator __settings_applicator = {
  744. .apply = ipv4_create_routes,
  745. };
  746. /* Drag in ICMPv4 */
  747. REQUIRE_OBJECT ( icmpv4 );