Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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 rc Return status code
  73. */
  74. static int add_ipv4_miniroute ( struct net_device *netdev,
  75. struct in_addr address, struct in_addr netmask,
  76. 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 -ENOMEM;
  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 0;
  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. ( iphdr->dest.s_addr != INADDR_BROADCAST ) &&
  492. ipv4_has_any_addr ( netdev ) &&
  493. ( ! ipv4_has_addr ( netdev, iphdr->dest ) ) ) {
  494. DBGC ( iphdr->src, "IPv4 discarding non-local unicast packet "
  495. "for %s\n", inet_ntoa ( iphdr->dest ) );
  496. ipv4_stats.in_addr_errors++;
  497. goto err_other;
  498. }
  499. /* Perform fragment reassembly if applicable */
  500. if ( iphdr->frags & htons ( IP_MASK_OFFSET | IP_MASK_MOREFRAGS ) ) {
  501. /* Pass the fragment to fragment_reassemble() which returns
  502. * either a fully reassembled I/O buffer or NULL.
  503. */
  504. iobuf = fragment_reassemble ( &ipv4_reassembler, iobuf,
  505. &hdrlen );
  506. if ( ! iobuf )
  507. return 0;
  508. iphdr = iobuf->data;
  509. }
  510. /* Construct socket addresses, calculate pseudo-header
  511. * checksum, and hand off to transport layer
  512. */
  513. memset ( &src, 0, sizeof ( src ) );
  514. src.sin.sin_family = AF_INET;
  515. src.sin.sin_addr = iphdr->src;
  516. memset ( &dest, 0, sizeof ( dest ) );
  517. dest.sin.sin_family = AF_INET;
  518. dest.sin.sin_addr = iphdr->dest;
  519. pshdr_csum = ipv4_pshdr_chksum ( iobuf, TCPIP_EMPTY_CSUM );
  520. iob_pull ( iobuf, hdrlen );
  521. if ( ( rc = tcpip_rx ( iobuf, netdev, iphdr->protocol, &src.st,
  522. &dest.st, pshdr_csum, &ipv4_stats ) ) != 0 ) {
  523. DBGC ( src.sin.sin_addr, "IPv4 received packet rejected by "
  524. "stack: %s\n", strerror ( rc ) );
  525. return rc;
  526. }
  527. profile_stop ( &ipv4_rx_profiler );
  528. return 0;
  529. err_header:
  530. ipv4_stats.in_hdr_errors++;
  531. err_other:
  532. free_iob ( iobuf );
  533. return -EINVAL;
  534. }
  535. /**
  536. * Check existence of IPv4 address for ARP
  537. *
  538. * @v netdev Network device
  539. * @v net_addr Network-layer address
  540. * @ret rc Return status code
  541. */
  542. static int ipv4_arp_check ( struct net_device *netdev, const void *net_addr ) {
  543. const struct in_addr *address = net_addr;
  544. if ( ipv4_has_addr ( netdev, *address ) )
  545. return 0;
  546. return -ENOENT;
  547. }
  548. /**
  549. * Parse IPv4 address
  550. *
  551. * @v string IPv4 address string
  552. * @ret in IPv4 address to fill in
  553. * @ret ok IPv4 address is valid
  554. *
  555. * Note that this function returns nonzero iff the address is valid,
  556. * to match the standard BSD API function of the same name. Unlike
  557. * most other iPXE functions, a zero therefore indicates failure.
  558. */
  559. int inet_aton ( const char *string, struct in_addr *in ) {
  560. const char *separator = "...";
  561. uint8_t *byte = ( ( uint8_t * ) in );
  562. char *endp;
  563. unsigned long value;
  564. while ( 1 ) {
  565. value = strtoul ( string, &endp, 0 );
  566. if ( string == endp )
  567. return 0;
  568. if ( value > 0xff )
  569. return 0;
  570. *(byte++) = value;
  571. if ( *endp != *separator )
  572. return 0;
  573. if ( ! *(separator++) )
  574. return 1;
  575. string = ( endp + 1 );
  576. }
  577. }
  578. /**
  579. * Convert IPv4 address to dotted-quad notation
  580. *
  581. * @v in IPv4 address
  582. * @ret string IPv4 address in dotted-quad notation
  583. */
  584. char * inet_ntoa ( struct in_addr in ) {
  585. static char buf[16]; /* "xxx.xxx.xxx.xxx" */
  586. uint8_t *bytes = ( uint8_t * ) &in;
  587. sprintf ( buf, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3] );
  588. return buf;
  589. }
  590. /**
  591. * Transcribe IPv4 address
  592. *
  593. * @v net_addr IPv4 address
  594. * @ret string IPv4 address in dotted-quad notation
  595. *
  596. */
  597. static const char * ipv4_ntoa ( const void *net_addr ) {
  598. return inet_ntoa ( * ( ( struct in_addr * ) net_addr ) );
  599. }
  600. /**
  601. * Transcribe IPv4 socket address
  602. *
  603. * @v sa Socket address
  604. * @ret string Socket address in standard notation
  605. */
  606. static const char * ipv4_sock_ntoa ( struct sockaddr *sa ) {
  607. struct sockaddr_in *sin = ( ( struct sockaddr_in * ) sa );
  608. return inet_ntoa ( sin->sin_addr );
  609. }
  610. /**
  611. * Parse IPv4 socket address
  612. *
  613. * @v string Socket address string
  614. * @v sa Socket address to fill in
  615. * @ret rc Return status code
  616. */
  617. static int ipv4_sock_aton ( const char *string, struct sockaddr *sa ) {
  618. struct sockaddr_in *sin = ( ( struct sockaddr_in * ) sa );
  619. struct in_addr in;
  620. if ( inet_aton ( string, &in ) ) {
  621. sin->sin_addr = in;
  622. return 0;
  623. }
  624. return -EINVAL;
  625. }
  626. /** IPv4 protocol */
  627. struct net_protocol ipv4_protocol __net_protocol = {
  628. .name = "IP",
  629. .net_proto = htons ( ETH_P_IP ),
  630. .net_addr_len = sizeof ( struct in_addr ),
  631. .rx = ipv4_rx,
  632. .ntoa = ipv4_ntoa,
  633. };
  634. /** IPv4 TCPIP net protocol */
  635. struct tcpip_net_protocol ipv4_tcpip_protocol __tcpip_net_protocol = {
  636. .name = "IPv4",
  637. .sa_family = AF_INET,
  638. .header_len = sizeof ( struct iphdr ),
  639. .net_protocol = &ipv4_protocol,
  640. .tx = ipv4_tx,
  641. .netdev = ipv4_netdev,
  642. };
  643. /** IPv4 ARP protocol */
  644. struct arp_net_protocol ipv4_arp_protocol __arp_net_protocol = {
  645. .net_protocol = &ipv4_protocol,
  646. .check = ipv4_arp_check,
  647. };
  648. /** IPv4 socket address converter */
  649. struct sockaddr_converter ipv4_sockaddr_converter __sockaddr_converter = {
  650. .family = AF_INET,
  651. .ntoa = ipv4_sock_ntoa,
  652. .aton = ipv4_sock_aton,
  653. };
  654. /******************************************************************************
  655. *
  656. * Settings
  657. *
  658. ******************************************************************************
  659. */
  660. /**
  661. * Parse IPv4 address setting value
  662. *
  663. * @v type Setting type
  664. * @v value Formatted setting value
  665. * @v buf Buffer to contain raw value
  666. * @v len Length of buffer
  667. * @ret len Length of raw value, or negative error
  668. */
  669. int parse_ipv4_setting ( const struct setting_type *type __unused,
  670. const char *value, void *buf, size_t len ) {
  671. struct in_addr ipv4;
  672. /* Parse IPv4 address */
  673. if ( inet_aton ( value, &ipv4 ) == 0 )
  674. return -EINVAL;
  675. /* Copy to buffer */
  676. if ( len > sizeof ( ipv4 ) )
  677. len = sizeof ( ipv4 );
  678. memcpy ( buf, &ipv4, len );
  679. return ( sizeof ( ipv4 ) );
  680. }
  681. /**
  682. * Format IPv4 address setting value
  683. *
  684. * @v type Setting type
  685. * @v raw Raw setting value
  686. * @v raw_len Length of raw setting value
  687. * @v buf Buffer to contain formatted value
  688. * @v len Length of buffer
  689. * @ret len Length of formatted value, or negative error
  690. */
  691. int format_ipv4_setting ( const struct setting_type *type __unused,
  692. const void *raw, size_t raw_len, char *buf,
  693. size_t len ) {
  694. const struct in_addr *ipv4 = raw;
  695. if ( raw_len < sizeof ( *ipv4 ) )
  696. return -EINVAL;
  697. return snprintf ( buf, len, "%s", inet_ntoa ( *ipv4 ) );
  698. }
  699. /** IPv4 address setting */
  700. const struct setting ip_setting __setting ( SETTING_IP4, ip ) = {
  701. .name = "ip",
  702. .description = "IP address",
  703. .tag = DHCP_EB_YIADDR,
  704. .type = &setting_type_ipv4,
  705. };
  706. /** IPv4 subnet mask setting */
  707. const struct setting netmask_setting __setting ( SETTING_IP4, netmask ) = {
  708. .name = "netmask",
  709. .description = "Subnet mask",
  710. .tag = DHCP_SUBNET_MASK,
  711. .type = &setting_type_ipv4,
  712. };
  713. /** Default gateway setting */
  714. const struct setting gateway_setting __setting ( SETTING_IP4, gateway ) = {
  715. .name = "gateway",
  716. .description = "Default gateway",
  717. .tag = DHCP_ROUTERS,
  718. .type = &setting_type_ipv4,
  719. };
  720. /**
  721. * Send gratuitous ARP, if applicable
  722. *
  723. * @v netdev Network device
  724. * @v address IPv4 address
  725. * @v netmask Subnet mask
  726. * @v gateway Gateway address (if any)
  727. * @ret rc Return status code
  728. */
  729. static int ipv4_gratuitous_arp ( struct net_device *netdev,
  730. struct in_addr address,
  731. struct in_addr netmask __unused,
  732. struct in_addr gateway __unused ) {
  733. int rc;
  734. /* Do nothing if network device already has this IPv4 address */
  735. if ( ipv4_has_addr ( netdev, address ) )
  736. return 0;
  737. /* Transmit gratuitous ARP */
  738. DBGC ( netdev, "IPv4 sending gratuitous ARP for %s via %s\n",
  739. inet_ntoa ( address ), netdev->name );
  740. if ( ( rc = arp_tx_request ( netdev, &ipv4_protocol, &address,
  741. &address ) ) != 0 ) {
  742. DBGC ( netdev, "IPv4 could not transmit gratuitous ARP: %s\n",
  743. strerror ( rc ) );
  744. /* Treat failures as non-fatal */
  745. }
  746. return 0;
  747. }
  748. /**
  749. * Process IPv4 network device settings
  750. *
  751. * @v apply Application method
  752. * @ret rc Return status code
  753. */
  754. static int ipv4_settings ( int ( * apply ) ( struct net_device *netdev,
  755. struct in_addr address,
  756. struct in_addr netmask,
  757. struct in_addr gateway ) ) {
  758. struct net_device *netdev;
  759. struct settings *settings;
  760. struct in_addr address = { 0 };
  761. struct in_addr netmask = { 0 };
  762. struct in_addr gateway = { 0 };
  763. int rc;
  764. /* Process settings for each network device */
  765. for_each_netdev ( netdev ) {
  766. /* Get network device settings */
  767. settings = netdev_settings ( netdev );
  768. /* Get IPv4 address */
  769. address.s_addr = 0;
  770. fetch_ipv4_setting ( settings, &ip_setting, &address );
  771. if ( ! address.s_addr )
  772. continue;
  773. /* Get subnet mask */
  774. fetch_ipv4_setting ( settings, &netmask_setting, &netmask );
  775. /* Calculate default netmask, if necessary */
  776. if ( ! netmask.s_addr ) {
  777. if ( IN_IS_CLASSA ( address.s_addr ) ) {
  778. netmask.s_addr = INADDR_NET_CLASSA;
  779. } else if ( IN_IS_CLASSB ( address.s_addr ) ) {
  780. netmask.s_addr = INADDR_NET_CLASSB;
  781. } else if ( IN_IS_CLASSC ( address.s_addr ) ) {
  782. netmask.s_addr = INADDR_NET_CLASSC;
  783. }
  784. }
  785. /* Get default gateway, if present */
  786. fetch_ipv4_setting ( settings, &gateway_setting, &gateway );
  787. /* Apply settings */
  788. if ( ( rc = apply ( netdev, address, netmask, gateway ) ) != 0 )
  789. return rc;
  790. }
  791. return 0;
  792. }
  793. /**
  794. * Create IPv4 routing table based on configured settings
  795. *
  796. * @ret rc Return status code
  797. */
  798. static int ipv4_create_routes ( void ) {
  799. struct ipv4_miniroute *miniroute;
  800. struct ipv4_miniroute *tmp;
  801. int rc;
  802. /* Send gratuitous ARPs for any new IPv4 addresses */
  803. ipv4_settings ( ipv4_gratuitous_arp );
  804. /* Delete all existing routes */
  805. list_for_each_entry_safe ( miniroute, tmp, &ipv4_miniroutes, list )
  806. del_ipv4_miniroute ( miniroute );
  807. /* Create a route for each configured network device */
  808. if ( ( rc = ipv4_settings ( add_ipv4_miniroute ) ) != 0 )
  809. return rc;
  810. return 0;
  811. }
  812. /** IPv4 settings applicator */
  813. struct settings_applicator ipv4_settings_applicator __settings_applicator = {
  814. .apply = ipv4_create_routes,
  815. };
  816. /* Drag in objects via ipv4_protocol */
  817. REQUIRING_SYMBOL ( ipv4_protocol );
  818. /* Drag in ICMPv4 */
  819. REQUIRE_OBJECT ( icmpv4 );