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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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 scope_id Destination address scope ID
  124. * @v dest Final destination address
  125. * @ret dest Next hop destination address
  126. * @ret miniroute Routing table entry to use, or NULL if no route
  127. *
  128. * If the route requires use of a gateway, the next hop destination
  129. * address will be overwritten with the gateway address.
  130. */
  131. static struct ipv4_miniroute * ipv4_route ( unsigned int scope_id,
  132. struct in_addr *dest ) {
  133. struct ipv4_miniroute *miniroute;
  134. /* Find first usable route in routing table */
  135. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  136. /* Skip closed network devices */
  137. if ( ! netdev_is_open ( miniroute->netdev ) )
  138. continue;
  139. if ( IN_IS_MULTICAST ( dest->s_addr ) ) {
  140. /* If destination is non-global, and the scope ID
  141. * matches this network device, then use this route.
  142. */
  143. if ( miniroute->netdev->index == scope_id )
  144. return miniroute;
  145. } else {
  146. /* If destination is an on-link global
  147. * address, then use this route.
  148. */
  149. if ( ( ( dest->s_addr ^ miniroute->address.s_addr )
  150. & miniroute->netmask.s_addr ) == 0 )
  151. return miniroute;
  152. /* If destination is an off-link global
  153. * address, and we have a default gateway,
  154. * then use this route.
  155. */
  156. if ( miniroute->gateway.s_addr ) {
  157. *dest = miniroute->gateway;
  158. return miniroute;
  159. }
  160. }
  161. }
  162. return NULL;
  163. }
  164. /**
  165. * Determine transmitting network device
  166. *
  167. * @v st_dest Destination network-layer address
  168. * @ret netdev Transmitting network device, or NULL
  169. */
  170. static struct net_device * ipv4_netdev ( struct sockaddr_tcpip *st_dest ) {
  171. struct sockaddr_in *sin_dest = ( ( struct sockaddr_in * ) st_dest );
  172. struct in_addr dest = sin_dest->sin_addr;
  173. struct ipv4_miniroute *miniroute;
  174. /* Find routing table entry */
  175. miniroute = ipv4_route ( sin_dest->sin_scope_id, &dest );
  176. if ( ! miniroute )
  177. return NULL;
  178. return miniroute->netdev;
  179. }
  180. /**
  181. * Check if IPv4 fragment matches fragment reassembly buffer
  182. *
  183. * @v fragment Fragment reassembly buffer
  184. * @v iobuf I/O buffer
  185. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  186. * @ret is_fragment Fragment matches this reassembly buffer
  187. */
  188. static int ipv4_is_fragment ( struct fragment *fragment,
  189. struct io_buffer *iobuf,
  190. size_t hdrlen __unused ) {
  191. struct iphdr *frag_iphdr = fragment->iobuf->data;
  192. struct iphdr *iphdr = iobuf->data;
  193. return ( ( iphdr->src.s_addr == frag_iphdr->src.s_addr ) &&
  194. ( iphdr->ident == frag_iphdr->ident ) );
  195. }
  196. /**
  197. * Get IPv4 fragment offset
  198. *
  199. * @v iobuf I/O buffer
  200. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  201. * @ret offset Offset
  202. */
  203. static size_t ipv4_fragment_offset ( struct io_buffer *iobuf,
  204. size_t hdrlen __unused ) {
  205. struct iphdr *iphdr = iobuf->data;
  206. return ( ( ntohs ( iphdr->frags ) & IP_MASK_OFFSET ) << 3 );
  207. }
  208. /**
  209. * Check if more fragments exist
  210. *
  211. * @v iobuf I/O buffer
  212. * @v hdrlen Length of non-fragmentable potion of I/O buffer
  213. * @ret more_frags More fragments exist
  214. */
  215. static int ipv4_more_fragments ( struct io_buffer *iobuf,
  216. size_t hdrlen __unused ) {
  217. struct iphdr *iphdr = iobuf->data;
  218. return ( iphdr->frags & htons ( IP_MASK_MOREFRAGS ) );
  219. }
  220. /** IPv4 fragment reassembler */
  221. static struct fragment_reassembler ipv4_reassembler = {
  222. .list = LIST_HEAD_INIT ( ipv4_reassembler.list ),
  223. .is_fragment = ipv4_is_fragment,
  224. .fragment_offset = ipv4_fragment_offset,
  225. .more_fragments = ipv4_more_fragments,
  226. .stats = &ipv4_stats,
  227. };
  228. /**
  229. * Add IPv4 pseudo-header checksum to existing checksum
  230. *
  231. * @v iobuf I/O buffer
  232. * @v csum Existing checksum
  233. * @ret csum Updated checksum
  234. */
  235. static uint16_t ipv4_pshdr_chksum ( struct io_buffer *iobuf, uint16_t csum ) {
  236. struct ipv4_pseudo_header pshdr;
  237. struct iphdr *iphdr = iobuf->data;
  238. size_t hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
  239. /* Build pseudo-header */
  240. pshdr.src = iphdr->src;
  241. pshdr.dest = iphdr->dest;
  242. pshdr.zero_padding = 0x00;
  243. pshdr.protocol = iphdr->protocol;
  244. pshdr.len = htons ( iob_len ( iobuf ) - hdrlen );
  245. /* Update the checksum value */
  246. return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
  247. }
  248. /**
  249. * Transmit IP packet
  250. *
  251. * @v iobuf I/O buffer
  252. * @v tcpip Transport-layer protocol
  253. * @v st_src Source network-layer address
  254. * @v st_dest Destination network-layer address
  255. * @v netdev Network device to use if no route found, or NULL
  256. * @v trans_csum Transport-layer checksum to complete, or NULL
  257. * @ret rc Status
  258. *
  259. * This function expects a transport-layer segment and prepends the IP header
  260. */
  261. static int ipv4_tx ( struct io_buffer *iobuf,
  262. struct tcpip_protocol *tcpip_protocol,
  263. struct sockaddr_tcpip *st_src,
  264. struct sockaddr_tcpip *st_dest,
  265. struct net_device *netdev,
  266. uint16_t *trans_csum ) {
  267. struct iphdr *iphdr = iob_push ( iobuf, sizeof ( *iphdr ) );
  268. struct sockaddr_in *sin_src = ( ( struct sockaddr_in * ) st_src );
  269. struct sockaddr_in *sin_dest = ( ( struct sockaddr_in * ) st_dest );
  270. struct ipv4_miniroute *miniroute;
  271. struct in_addr next_hop;
  272. struct in_addr netmask = { .s_addr = 0 };
  273. uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
  274. const void *ll_dest;
  275. int rc;
  276. /* Start profiling */
  277. profile_start ( &ipv4_tx_profiler );
  278. /* Update statistics */
  279. ipv4_stats.out_requests++;
  280. /* Fill up the IP header, except source address */
  281. memset ( iphdr, 0, sizeof ( *iphdr ) );
  282. iphdr->verhdrlen = ( IP_VER | ( sizeof ( *iphdr ) / 4 ) );
  283. iphdr->service = IP_TOS;
  284. iphdr->len = htons ( iob_len ( iobuf ) );
  285. iphdr->ttl = IP_TTL;
  286. iphdr->protocol = tcpip_protocol->tcpip_proto;
  287. iphdr->dest = sin_dest->sin_addr;
  288. /* Use routing table to identify next hop and transmitting netdev */
  289. next_hop = iphdr->dest;
  290. if ( sin_src )
  291. iphdr->src = sin_src->sin_addr;
  292. if ( ( next_hop.s_addr != INADDR_BROADCAST ) &&
  293. ( ( miniroute = ipv4_route ( sin_dest->sin_scope_id,
  294. &next_hop ) ) != NULL ) ) {
  295. iphdr->src = miniroute->address;
  296. netmask = miniroute->netmask;
  297. netdev = miniroute->netdev;
  298. }
  299. if ( ! netdev ) {
  300. DBGC ( sin_dest->sin_addr, "IPv4 has no route to %s\n",
  301. inet_ntoa ( iphdr->dest ) );
  302. ipv4_stats.out_no_routes++;
  303. rc = -ENETUNREACH;
  304. goto err;
  305. }
  306. /* (Ab)use the "ident" field to convey metadata about the
  307. * network device statistics into packet traces. Useful for
  308. * extracting debug information from non-debug builds.
  309. */
  310. iphdr->ident = htons ( ( (++next_ident_high) << 8 ) |
  311. ( ( netdev->rx_stats.bad & 0xf ) << 4 ) |
  312. ( ( netdev->rx_stats.good & 0xf ) << 0 ) );
  313. /* Fix up checksums */
  314. if ( trans_csum ) {
  315. *trans_csum = ipv4_pshdr_chksum ( iobuf, *trans_csum );
  316. if ( ! *trans_csum )
  317. *trans_csum = tcpip_protocol->zero_csum;
  318. }
  319. iphdr->chksum = tcpip_chksum ( iphdr, sizeof ( *iphdr ) );
  320. /* Print IP4 header for debugging */
  321. DBGC2 ( sin_dest->sin_addr, "IPv4 TX %s->", inet_ntoa ( iphdr->src ) );
  322. DBGC2 ( sin_dest->sin_addr, "%s len %d proto %d id %04x csum %04x\n",
  323. inet_ntoa ( iphdr->dest ), ntohs ( iphdr->len ),
  324. iphdr->protocol, ntohs ( iphdr->ident ),
  325. ntohs ( iphdr->chksum ) );
  326. /* Calculate link-layer destination address, if possible */
  327. if ( ( ( next_hop.s_addr ^ INADDR_BROADCAST ) & ~netmask.s_addr ) == 0){
  328. /* Broadcast address */
  329. ipv4_stats.out_bcast_pkts++;
  330. ll_dest = netdev->ll_broadcast;
  331. } else if ( IN_IS_MULTICAST ( next_hop.s_addr ) ) {
  332. /* Multicast address */
  333. ipv4_stats.out_mcast_pkts++;
  334. if ( ( rc = netdev->ll_protocol->mc_hash ( AF_INET, &next_hop,
  335. ll_dest_buf ) ) !=0){
  336. DBGC ( sin_dest->sin_addr, "IPv4 could not hash "
  337. "multicast %s: %s\n",
  338. inet_ntoa ( next_hop ), strerror ( rc ) );
  339. goto err;
  340. }
  341. ll_dest = ll_dest_buf;
  342. } else {
  343. /* Unicast address */
  344. ll_dest = NULL;
  345. }
  346. /* Update statistics */
  347. ipv4_stats.out_transmits++;
  348. ipv4_stats.out_octets += iob_len ( iobuf );
  349. /* Hand off to link layer (via ARP if applicable) */
  350. if ( ll_dest ) {
  351. if ( ( rc = net_tx ( iobuf, netdev, &ipv4_protocol, ll_dest,
  352. netdev->ll_addr ) ) != 0 ) {
  353. DBGC ( sin_dest->sin_addr, "IPv4 could not transmit "
  354. "packet via %s: %s\n",
  355. netdev->name, strerror ( rc ) );
  356. return rc;
  357. }
  358. } else {
  359. if ( ( rc = arp_tx ( iobuf, netdev, &ipv4_protocol, &next_hop,
  360. &iphdr->src, netdev->ll_addr ) ) != 0 ) {
  361. DBGC ( sin_dest->sin_addr, "IPv4 could not transmit "
  362. "packet via %s: %s\n",
  363. netdev->name, strerror ( rc ) );
  364. return rc;
  365. }
  366. }
  367. profile_stop ( &ipv4_tx_profiler );
  368. return 0;
  369. err:
  370. free_iob ( iobuf );
  371. return rc;
  372. }
  373. /**
  374. * Check if network device has any IPv4 address
  375. *
  376. * @v netdev Network device
  377. * @ret has_any_addr Network device has any IPv4 address
  378. */
  379. int ipv4_has_any_addr ( struct net_device *netdev ) {
  380. struct ipv4_miniroute *miniroute;
  381. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  382. if ( miniroute->netdev == netdev )
  383. return 1;
  384. }
  385. return 0;
  386. }
  387. /**
  388. * Check if network device has a specific IPv4 address
  389. *
  390. * @v netdev Network device
  391. * @v addr IPv4 address
  392. * @ret has_addr Network device has this IPv4 address
  393. */
  394. static int ipv4_has_addr ( struct net_device *netdev, struct in_addr addr ) {
  395. struct ipv4_miniroute *miniroute;
  396. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  397. if ( ( miniroute->netdev == netdev ) &&
  398. ( miniroute->address.s_addr == addr.s_addr ) ) {
  399. /* Found matching address */
  400. return 1;
  401. }
  402. }
  403. return 0;
  404. }
  405. /**
  406. * Process incoming packets
  407. *
  408. * @v iobuf I/O buffer
  409. * @v netdev Network device
  410. * @v ll_dest Link-layer destination address
  411. * @v ll_source Link-layer destination source
  412. * @v flags Packet flags
  413. * @ret rc Return status code
  414. *
  415. * This function expects an IP4 network datagram. It processes the headers
  416. * and sends it to the transport layer.
  417. */
  418. static int ipv4_rx ( struct io_buffer *iobuf,
  419. struct net_device *netdev,
  420. const void *ll_dest __unused,
  421. const void *ll_source __unused,
  422. unsigned int flags ) {
  423. struct iphdr *iphdr = iobuf->data;
  424. size_t hdrlen;
  425. size_t len;
  426. union {
  427. struct sockaddr_in sin;
  428. struct sockaddr_tcpip st;
  429. } src, dest;
  430. uint16_t csum;
  431. uint16_t pshdr_csum;
  432. int rc;
  433. /* Start profiling */
  434. profile_start ( &ipv4_rx_profiler );
  435. /* Update statistics */
  436. ipv4_stats.in_receives++;
  437. ipv4_stats.in_octets += iob_len ( iobuf );
  438. if ( flags & LL_BROADCAST ) {
  439. ipv4_stats.in_bcast_pkts++;
  440. } else if ( flags & LL_MULTICAST ) {
  441. ipv4_stats.in_mcast_pkts++;
  442. }
  443. /* Sanity check the IPv4 header */
  444. if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
  445. DBGC ( iphdr->src, "IPv4 packet too short at %zd bytes (min "
  446. "%zd bytes)\n", iob_len ( iobuf ), sizeof ( *iphdr ) );
  447. goto err_header;
  448. }
  449. if ( ( iphdr->verhdrlen & IP_MASK_VER ) != IP_VER ) {
  450. DBGC ( iphdr->src, "IPv4 version %#02x not supported\n",
  451. iphdr->verhdrlen );
  452. goto err_header;
  453. }
  454. hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
  455. if ( hdrlen < sizeof ( *iphdr ) ) {
  456. DBGC ( iphdr->src, "IPv4 header too short at %zd bytes (min "
  457. "%zd bytes)\n", hdrlen, sizeof ( *iphdr ) );
  458. goto err_header;
  459. }
  460. if ( hdrlen > iob_len ( iobuf ) ) {
  461. DBGC ( iphdr->src, "IPv4 header too long at %zd bytes "
  462. "(packet is %zd bytes)\n", hdrlen, iob_len ( iobuf ) );
  463. goto err_header;
  464. }
  465. if ( ( csum = tcpip_chksum ( iphdr, hdrlen ) ) != 0 ) {
  466. DBGC ( iphdr->src, "IPv4 checksum incorrect (is %04x "
  467. "including checksum field, should be 0000)\n", csum );
  468. goto err_header;
  469. }
  470. len = ntohs ( iphdr->len );
  471. if ( len < hdrlen ) {
  472. DBGC ( iphdr->src, "IPv4 length too short at %zd bytes "
  473. "(header is %zd bytes)\n", len, hdrlen );
  474. goto err_header;
  475. }
  476. if ( len > iob_len ( iobuf ) ) {
  477. DBGC ( iphdr->src, "IPv4 length too long at %zd bytes "
  478. "(packet is %zd bytes)\n", len, iob_len ( iobuf ) );
  479. ipv4_stats.in_truncated_pkts++;
  480. goto err_other;
  481. }
  482. /* Truncate packet to correct length */
  483. iob_unput ( iobuf, ( iob_len ( iobuf ) - len ) );
  484. /* Print IPv4 header for debugging */
  485. DBGC2 ( iphdr->src, "IPv4 RX %s<-", inet_ntoa ( iphdr->dest ) );
  486. DBGC2 ( iphdr->src, "%s len %d proto %d id %04x csum %04x\n",
  487. inet_ntoa ( iphdr->src ), ntohs ( iphdr->len ), iphdr->protocol,
  488. ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );
  489. /* Discard unicast packets not destined for us */
  490. if ( ( ! ( flags & LL_MULTICAST ) ) &&
  491. ipv4_has_any_addr ( netdev ) &&
  492. ( ! ipv4_has_addr ( netdev, iphdr->dest ) ) ) {
  493. DBGC ( iphdr->src, "IPv4 discarding non-local unicast packet "
  494. "for %s\n", inet_ntoa ( iphdr->dest ) );
  495. ipv4_stats.in_addr_errors++;
  496. goto err_other;
  497. }
  498. /* Perform fragment reassembly if applicable */
  499. if ( iphdr->frags & htons ( IP_MASK_OFFSET | IP_MASK_MOREFRAGS ) ) {
  500. /* Pass the fragment to fragment_reassemble() which returns
  501. * either a fully reassembled I/O buffer or NULL.
  502. */
  503. iobuf = fragment_reassemble ( &ipv4_reassembler, iobuf,
  504. &hdrlen );
  505. if ( ! iobuf )
  506. return 0;
  507. iphdr = iobuf->data;
  508. }
  509. /* Construct socket addresses, calculate pseudo-header
  510. * checksum, and hand off to transport layer
  511. */
  512. memset ( &src, 0, sizeof ( src ) );
  513. src.sin.sin_family = AF_INET;
  514. src.sin.sin_addr = iphdr->src;
  515. memset ( &dest, 0, sizeof ( dest ) );
  516. dest.sin.sin_family = AF_INET;
  517. dest.sin.sin_addr = iphdr->dest;
  518. pshdr_csum = ipv4_pshdr_chksum ( iobuf, TCPIP_EMPTY_CSUM );
  519. iob_pull ( iobuf, hdrlen );
  520. if ( ( rc = tcpip_rx ( iobuf, netdev, iphdr->protocol, &src.st,
  521. &dest.st, pshdr_csum, &ipv4_stats ) ) != 0 ) {
  522. DBGC ( src.sin.sin_addr, "IPv4 received packet rejected by "
  523. "stack: %s\n", strerror ( rc ) );
  524. return rc;
  525. }
  526. profile_stop ( &ipv4_rx_profiler );
  527. return 0;
  528. err_header:
  529. ipv4_stats.in_hdr_errors++;
  530. err_other:
  531. free_iob ( iobuf );
  532. return -EINVAL;
  533. }
  534. /**
  535. * Check existence of IPv4 address for ARP
  536. *
  537. * @v netdev Network device
  538. * @v net_addr Network-layer address
  539. * @ret rc Return status code
  540. */
  541. static int ipv4_arp_check ( struct net_device *netdev, const void *net_addr ) {
  542. const struct in_addr *address = net_addr;
  543. if ( ipv4_has_addr ( netdev, *address ) )
  544. return 0;
  545. return -ENOENT;
  546. }
  547. /**
  548. * Parse IPv4 address
  549. *
  550. * @v string IPv4 address string
  551. * @ret in IPv4 address to fill in
  552. * @ret ok IPv4 address is valid
  553. *
  554. * Note that this function returns nonzero iff the address is valid,
  555. * to match the standard BSD API function of the same name. Unlike
  556. * most other iPXE functions, a zero therefore indicates failure.
  557. */
  558. int inet_aton ( const char *string, struct in_addr *in ) {
  559. const char *separator = "...";
  560. uint8_t *byte = ( ( uint8_t * ) in );
  561. char *endp;
  562. unsigned long value;
  563. while ( 1 ) {
  564. value = strtoul ( string, &endp, 0 );
  565. if ( string == endp )
  566. return 0;
  567. if ( value > 0xff )
  568. return 0;
  569. *(byte++) = value;
  570. if ( *endp != *separator )
  571. return 0;
  572. if ( ! *(separator++) )
  573. return 1;
  574. string = ( endp + 1 );
  575. }
  576. }
  577. /**
  578. * Convert IPv4 address to dotted-quad notation
  579. *
  580. * @v in IPv4 address
  581. * @ret string IPv4 address in dotted-quad notation
  582. */
  583. char * inet_ntoa ( struct in_addr in ) {
  584. static char buf[16]; /* "xxx.xxx.xxx.xxx" */
  585. uint8_t *bytes = ( uint8_t * ) &in;
  586. sprintf ( buf, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3] );
  587. return buf;
  588. }
  589. /**
  590. * Transcribe IPv4 address
  591. *
  592. * @v net_addr IPv4 address
  593. * @ret string IPv4 address in dotted-quad notation
  594. *
  595. */
  596. static const char * ipv4_ntoa ( const void *net_addr ) {
  597. return inet_ntoa ( * ( ( struct in_addr * ) net_addr ) );
  598. }
  599. /**
  600. * Transcribe IPv4 socket address
  601. *
  602. * @v sa Socket address
  603. * @ret string Socket address in standard notation
  604. */
  605. static const char * ipv4_sock_ntoa ( struct sockaddr *sa ) {
  606. struct sockaddr_in *sin = ( ( struct sockaddr_in * ) sa );
  607. return inet_ntoa ( sin->sin_addr );
  608. }
  609. /**
  610. * Parse IPv4 socket address
  611. *
  612. * @v string Socket address string
  613. * @v sa Socket address to fill in
  614. * @ret rc Return status code
  615. */
  616. static int ipv4_sock_aton ( const char *string, struct sockaddr *sa ) {
  617. struct sockaddr_in *sin = ( ( struct sockaddr_in * ) sa );
  618. struct in_addr in;
  619. if ( inet_aton ( string, &in ) ) {
  620. sin->sin_addr = in;
  621. return 0;
  622. }
  623. return -EINVAL;
  624. }
  625. /** IPv4 protocol */
  626. struct net_protocol ipv4_protocol __net_protocol = {
  627. .name = "IP",
  628. .net_proto = htons ( ETH_P_IP ),
  629. .net_addr_len = sizeof ( struct in_addr ),
  630. .rx = ipv4_rx,
  631. .ntoa = ipv4_ntoa,
  632. };
  633. /** IPv4 TCPIP net protocol */
  634. struct tcpip_net_protocol ipv4_tcpip_protocol __tcpip_net_protocol = {
  635. .name = "IPv4",
  636. .sa_family = AF_INET,
  637. .header_len = sizeof ( struct iphdr ),
  638. .net_protocol = &ipv4_protocol,
  639. .tx = ipv4_tx,
  640. .netdev = ipv4_netdev,
  641. };
  642. /** IPv4 ARP protocol */
  643. struct arp_net_protocol ipv4_arp_protocol __arp_net_protocol = {
  644. .net_protocol = &ipv4_protocol,
  645. .check = ipv4_arp_check,
  646. };
  647. /** IPv4 socket address converter */
  648. struct sockaddr_converter ipv4_sockaddr_converter __sockaddr_converter = {
  649. .family = AF_INET,
  650. .ntoa = ipv4_sock_ntoa,
  651. .aton = ipv4_sock_aton,
  652. };
  653. /******************************************************************************
  654. *
  655. * Settings
  656. *
  657. ******************************************************************************
  658. */
  659. /**
  660. * Parse IPv4 address setting value
  661. *
  662. * @v type Setting type
  663. * @v value Formatted setting value
  664. * @v buf Buffer to contain raw value
  665. * @v len Length of buffer
  666. * @ret len Length of raw value, or negative error
  667. */
  668. int parse_ipv4_setting ( const struct setting_type *type __unused,
  669. const char *value, void *buf, size_t len ) {
  670. struct in_addr ipv4;
  671. /* Parse IPv4 address */
  672. if ( inet_aton ( value, &ipv4 ) == 0 )
  673. return -EINVAL;
  674. /* Copy to buffer */
  675. if ( len > sizeof ( ipv4 ) )
  676. len = sizeof ( ipv4 );
  677. memcpy ( buf, &ipv4, len );
  678. return ( sizeof ( ipv4 ) );
  679. }
  680. /**
  681. * Format IPv4 address setting value
  682. *
  683. * @v type Setting type
  684. * @v raw Raw setting value
  685. * @v raw_len Length of raw setting value
  686. * @v buf Buffer to contain formatted value
  687. * @v len Length of buffer
  688. * @ret len Length of formatted value, or negative error
  689. */
  690. int format_ipv4_setting ( const struct setting_type *type __unused,
  691. const void *raw, size_t raw_len, char *buf,
  692. size_t len ) {
  693. const struct in_addr *ipv4 = raw;
  694. if ( raw_len < sizeof ( *ipv4 ) )
  695. return -EINVAL;
  696. return snprintf ( buf, len, "%s", inet_ntoa ( *ipv4 ) );
  697. }
  698. /** IPv4 address setting */
  699. const struct setting ip_setting __setting ( SETTING_IP, ip ) = {
  700. .name = "ip",
  701. .description = "IP address",
  702. .tag = DHCP_EB_YIADDR,
  703. .type = &setting_type_ipv4,
  704. };
  705. /** IPv4 subnet mask setting */
  706. const struct setting netmask_setting __setting ( SETTING_IP, netmask ) = {
  707. .name = "netmask",
  708. .description = "Subnet mask",
  709. .tag = DHCP_SUBNET_MASK,
  710. .type = &setting_type_ipv4,
  711. };
  712. /** Default gateway setting */
  713. const struct setting gateway_setting __setting ( SETTING_IP, gateway ) = {
  714. .name = "gateway",
  715. .description = "Default gateway",
  716. .tag = DHCP_ROUTERS,
  717. .type = &setting_type_ipv4,
  718. };
  719. /**
  720. * Create IPv4 routing table based on configured settings
  721. *
  722. * @ret rc Return status code
  723. */
  724. static int ipv4_create_routes ( void ) {
  725. struct ipv4_miniroute *miniroute;
  726. struct ipv4_miniroute *tmp;
  727. struct net_device *netdev;
  728. struct settings *settings;
  729. struct in_addr address = { 0 };
  730. struct in_addr netmask = { 0 };
  731. struct in_addr gateway = { 0 };
  732. /* Delete all existing routes */
  733. list_for_each_entry_safe ( miniroute, tmp, &ipv4_miniroutes, list )
  734. del_ipv4_miniroute ( miniroute );
  735. /* Create a route for each configured network device */
  736. for_each_netdev ( netdev ) {
  737. settings = netdev_settings ( netdev );
  738. /* Get IPv4 address */
  739. address.s_addr = 0;
  740. fetch_ipv4_setting ( settings, &ip_setting, &address );
  741. if ( ! address.s_addr )
  742. continue;
  743. /* Get subnet mask */
  744. fetch_ipv4_setting ( settings, &netmask_setting, &netmask );
  745. /* Calculate default netmask, if necessary */
  746. if ( ! netmask.s_addr ) {
  747. if ( IN_IS_CLASSA ( address.s_addr ) ) {
  748. netmask.s_addr = INADDR_NET_CLASSA;
  749. } else if ( IN_IS_CLASSB ( address.s_addr ) ) {
  750. netmask.s_addr = INADDR_NET_CLASSB;
  751. } else if ( IN_IS_CLASSC ( address.s_addr ) ) {
  752. netmask.s_addr = INADDR_NET_CLASSC;
  753. }
  754. }
  755. /* Get default gateway, if present */
  756. fetch_ipv4_setting ( settings, &gateway_setting, &gateway );
  757. /* Configure route */
  758. miniroute = add_ipv4_miniroute ( netdev, address,
  759. netmask, gateway );
  760. if ( ! miniroute )
  761. return -ENOMEM;
  762. }
  763. return 0;
  764. }
  765. /** IPv4 settings applicator */
  766. struct settings_applicator ipv4_settings_applicator __settings_applicator = {
  767. .apply = ipv4_create_routes,
  768. };
  769. /* Drag in objects via ipv4_protocol */
  770. REQUIRING_SYMBOL ( ipv4_protocol );
  771. /* Drag in ICMPv4 */
  772. REQUIRE_OBJECT ( icmpv4 );