Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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