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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. iphdr->chksum = tcpip_chksum ( iphdr, sizeof ( *iphdr ) );
  317. /* Print IP4 header for debugging */
  318. DBGC2 ( sin_dest->sin_addr, "IPv4 TX %s->", inet_ntoa ( iphdr->src ) );
  319. DBGC2 ( sin_dest->sin_addr, "%s len %d proto %d id %04x csum %04x\n",
  320. inet_ntoa ( iphdr->dest ), ntohs ( iphdr->len ),
  321. iphdr->protocol, ntohs ( iphdr->ident ),
  322. ntohs ( iphdr->chksum ) );
  323. /* Calculate link-layer destination address, if possible */
  324. if ( ( ( next_hop.s_addr ^ INADDR_BROADCAST ) & ~netmask.s_addr ) == 0){
  325. /* Broadcast address */
  326. ipv4_stats.out_bcast_pkts++;
  327. ll_dest = netdev->ll_broadcast;
  328. } else if ( IN_IS_MULTICAST ( next_hop.s_addr ) ) {
  329. /* Multicast address */
  330. ipv4_stats.out_mcast_pkts++;
  331. if ( ( rc = netdev->ll_protocol->mc_hash ( AF_INET, &next_hop,
  332. ll_dest_buf ) ) !=0){
  333. DBGC ( sin_dest->sin_addr, "IPv4 could not hash "
  334. "multicast %s: %s\n",
  335. inet_ntoa ( next_hop ), strerror ( rc ) );
  336. goto err;
  337. }
  338. ll_dest = ll_dest_buf;
  339. } else {
  340. /* Unicast address */
  341. ll_dest = NULL;
  342. }
  343. /* Update statistics */
  344. ipv4_stats.out_transmits++;
  345. ipv4_stats.out_octets += iob_len ( iobuf );
  346. /* Hand off to link layer (via ARP if applicable) */
  347. if ( ll_dest ) {
  348. if ( ( rc = net_tx ( iobuf, netdev, &ipv4_protocol, ll_dest,
  349. netdev->ll_addr ) ) != 0 ) {
  350. DBGC ( sin_dest->sin_addr, "IPv4 could not transmit "
  351. "packet via %s: %s\n",
  352. netdev->name, strerror ( rc ) );
  353. return rc;
  354. }
  355. } else {
  356. if ( ( rc = arp_tx ( iobuf, netdev, &ipv4_protocol, &next_hop,
  357. &iphdr->src, netdev->ll_addr ) ) != 0 ) {
  358. DBGC ( sin_dest->sin_addr, "IPv4 could not transmit "
  359. "packet via %s: %s\n",
  360. netdev->name, strerror ( rc ) );
  361. return rc;
  362. }
  363. }
  364. profile_stop ( &ipv4_tx_profiler );
  365. return 0;
  366. err:
  367. free_iob ( iobuf );
  368. return rc;
  369. }
  370. /**
  371. * Check if network device has any IPv4 address
  372. *
  373. * @v netdev Network device
  374. * @ret has_any_addr Network device has any IPv4 address
  375. */
  376. int ipv4_has_any_addr ( struct net_device *netdev ) {
  377. struct ipv4_miniroute *miniroute;
  378. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  379. if ( miniroute->netdev == netdev )
  380. return 1;
  381. }
  382. return 0;
  383. }
  384. /**
  385. * Check if network device has a specific IPv4 address
  386. *
  387. * @v netdev Network device
  388. * @v addr IPv4 address
  389. * @ret has_addr Network device has this IPv4 address
  390. */
  391. static int ipv4_has_addr ( struct net_device *netdev, struct in_addr addr ) {
  392. struct ipv4_miniroute *miniroute;
  393. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  394. if ( ( miniroute->netdev == netdev ) &&
  395. ( miniroute->address.s_addr == addr.s_addr ) ) {
  396. /* Found matching address */
  397. return 1;
  398. }
  399. }
  400. return 0;
  401. }
  402. /**
  403. * Process incoming packets
  404. *
  405. * @v iobuf I/O buffer
  406. * @v netdev Network device
  407. * @v ll_dest Link-layer destination address
  408. * @v ll_source Link-layer destination source
  409. * @v flags Packet flags
  410. * @ret rc Return status code
  411. *
  412. * This function expects an IP4 network datagram. It processes the headers
  413. * and sends it to the transport layer.
  414. */
  415. static int ipv4_rx ( struct io_buffer *iobuf,
  416. struct net_device *netdev,
  417. const void *ll_dest __unused,
  418. const void *ll_source __unused,
  419. unsigned int flags ) {
  420. struct iphdr *iphdr = iobuf->data;
  421. size_t hdrlen;
  422. size_t len;
  423. union {
  424. struct sockaddr_in sin;
  425. struct sockaddr_tcpip st;
  426. } src, dest;
  427. uint16_t csum;
  428. uint16_t pshdr_csum;
  429. int rc;
  430. /* Start profiling */
  431. profile_start ( &ipv4_rx_profiler );
  432. /* Update statistics */
  433. ipv4_stats.in_receives++;
  434. ipv4_stats.in_octets += iob_len ( iobuf );
  435. if ( flags & LL_BROADCAST ) {
  436. ipv4_stats.in_bcast_pkts++;
  437. } else if ( flags & LL_MULTICAST ) {
  438. ipv4_stats.in_mcast_pkts++;
  439. }
  440. /* Sanity check the IPv4 header */
  441. if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
  442. DBGC ( iphdr->src, "IPv4 packet too short at %zd bytes (min "
  443. "%zd bytes)\n", iob_len ( iobuf ), sizeof ( *iphdr ) );
  444. goto err_header;
  445. }
  446. if ( ( iphdr->verhdrlen & IP_MASK_VER ) != IP_VER ) {
  447. DBGC ( iphdr->src, "IPv4 version %#02x not supported\n",
  448. iphdr->verhdrlen );
  449. goto err_header;
  450. }
  451. hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
  452. if ( hdrlen < sizeof ( *iphdr ) ) {
  453. DBGC ( iphdr->src, "IPv4 header too short at %zd bytes (min "
  454. "%zd bytes)\n", hdrlen, sizeof ( *iphdr ) );
  455. goto err_header;
  456. }
  457. if ( hdrlen > iob_len ( iobuf ) ) {
  458. DBGC ( iphdr->src, "IPv4 header too long at %zd bytes "
  459. "(packet is %zd bytes)\n", hdrlen, iob_len ( iobuf ) );
  460. goto err_header;
  461. }
  462. if ( ( csum = tcpip_chksum ( iphdr, hdrlen ) ) != 0 ) {
  463. DBGC ( iphdr->src, "IPv4 checksum incorrect (is %04x "
  464. "including checksum field, should be 0000)\n", csum );
  465. goto err_header;
  466. }
  467. len = ntohs ( iphdr->len );
  468. if ( len < hdrlen ) {
  469. DBGC ( iphdr->src, "IPv4 length too short at %zd bytes "
  470. "(header is %zd bytes)\n", len, hdrlen );
  471. goto err_header;
  472. }
  473. if ( len > iob_len ( iobuf ) ) {
  474. DBGC ( iphdr->src, "IPv4 length too long at %zd bytes "
  475. "(packet is %zd bytes)\n", len, iob_len ( iobuf ) );
  476. ipv4_stats.in_truncated_pkts++;
  477. goto err_other;
  478. }
  479. /* Truncate packet to correct length */
  480. iob_unput ( iobuf, ( iob_len ( iobuf ) - len ) );
  481. /* Print IPv4 header for debugging */
  482. DBGC2 ( iphdr->src, "IPv4 RX %s<-", inet_ntoa ( iphdr->dest ) );
  483. DBGC2 ( iphdr->src, "%s len %d proto %d id %04x csum %04x\n",
  484. inet_ntoa ( iphdr->src ), ntohs ( iphdr->len ), iphdr->protocol,
  485. ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );
  486. /* Discard unicast packets not destined for us */
  487. if ( ( ! ( flags & LL_MULTICAST ) ) &&
  488. ipv4_has_any_addr ( netdev ) &&
  489. ( ! ipv4_has_addr ( netdev, iphdr->dest ) ) ) {
  490. DBGC ( iphdr->src, "IPv4 discarding non-local unicast packet "
  491. "for %s\n", inet_ntoa ( iphdr->dest ) );
  492. ipv4_stats.in_addr_errors++;
  493. goto err_other;
  494. }
  495. /* Perform fragment reassembly if applicable */
  496. if ( iphdr->frags & htons ( IP_MASK_OFFSET | IP_MASK_MOREFRAGS ) ) {
  497. /* Pass the fragment to fragment_reassemble() which returns
  498. * either a fully reassembled I/O buffer or NULL.
  499. */
  500. iobuf = fragment_reassemble ( &ipv4_reassembler, iobuf,
  501. &hdrlen );
  502. if ( ! iobuf )
  503. return 0;
  504. iphdr = iobuf->data;
  505. }
  506. /* Construct socket addresses, calculate pseudo-header
  507. * checksum, and hand off to transport layer
  508. */
  509. memset ( &src, 0, sizeof ( src ) );
  510. src.sin.sin_family = AF_INET;
  511. src.sin.sin_addr = iphdr->src;
  512. memset ( &dest, 0, sizeof ( dest ) );
  513. dest.sin.sin_family = AF_INET;
  514. dest.sin.sin_addr = iphdr->dest;
  515. pshdr_csum = ipv4_pshdr_chksum ( iobuf, TCPIP_EMPTY_CSUM );
  516. iob_pull ( iobuf, hdrlen );
  517. if ( ( rc = tcpip_rx ( iobuf, netdev, iphdr->protocol, &src.st,
  518. &dest.st, pshdr_csum, &ipv4_stats ) ) != 0 ) {
  519. DBGC ( src.sin.sin_addr, "IPv4 received packet rejected by "
  520. "stack: %s\n", strerror ( rc ) );
  521. return rc;
  522. }
  523. profile_stop ( &ipv4_rx_profiler );
  524. return 0;
  525. err_header:
  526. ipv4_stats.in_hdr_errors++;
  527. err_other:
  528. free_iob ( iobuf );
  529. return -EINVAL;
  530. }
  531. /**
  532. * Check existence of IPv4 address for ARP
  533. *
  534. * @v netdev Network device
  535. * @v net_addr Network-layer address
  536. * @ret rc Return status code
  537. */
  538. static int ipv4_arp_check ( struct net_device *netdev, const void *net_addr ) {
  539. const struct in_addr *address = net_addr;
  540. if ( ipv4_has_addr ( netdev, *address ) )
  541. return 0;
  542. return -ENOENT;
  543. }
  544. /**
  545. * Parse IPv4 address
  546. *
  547. * @v string IPv4 address string
  548. * @ret in IPv4 address to fill in
  549. * @ret ok IPv4 address is valid
  550. *
  551. * Note that this function returns nonzero iff the address is valid,
  552. * to match the standard BSD API function of the same name. Unlike
  553. * most other iPXE functions, a zero therefore indicates failure.
  554. */
  555. int inet_aton ( const char *string, struct in_addr *in ) {
  556. const char *separator = "...";
  557. uint8_t *byte = ( ( uint8_t * ) in );
  558. char *endp;
  559. unsigned long value;
  560. while ( 1 ) {
  561. value = strtoul ( string, &endp, 0 );
  562. if ( string == endp )
  563. return 0;
  564. if ( value > 0xff )
  565. return 0;
  566. *(byte++) = value;
  567. if ( *endp != *separator )
  568. return 0;
  569. if ( ! *(separator++) )
  570. return 1;
  571. string = ( endp + 1 );
  572. }
  573. }
  574. /**
  575. * Convert IPv4 address to dotted-quad notation
  576. *
  577. * @v in IPv4 address
  578. * @ret string IPv4 address in dotted-quad notation
  579. */
  580. char * inet_ntoa ( struct in_addr in ) {
  581. static char buf[16]; /* "xxx.xxx.xxx.xxx" */
  582. uint8_t *bytes = ( uint8_t * ) &in;
  583. sprintf ( buf, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3] );
  584. return buf;
  585. }
  586. /**
  587. * Transcribe IPv4 address
  588. *
  589. * @v net_addr IPv4 address
  590. * @ret string IPv4 address in dotted-quad notation
  591. *
  592. */
  593. static const char * ipv4_ntoa ( const void *net_addr ) {
  594. return inet_ntoa ( * ( ( struct in_addr * ) net_addr ) );
  595. }
  596. /**
  597. * Transcribe IPv4 socket address
  598. *
  599. * @v sa Socket address
  600. * @ret string Socket address in standard notation
  601. */
  602. static const char * ipv4_sock_ntoa ( struct sockaddr *sa ) {
  603. struct sockaddr_in *sin = ( ( struct sockaddr_in * ) sa );
  604. return inet_ntoa ( sin->sin_addr );
  605. }
  606. /**
  607. * Parse IPv4 socket address
  608. *
  609. * @v string Socket address string
  610. * @v sa Socket address to fill in
  611. * @ret rc Return status code
  612. */
  613. static int ipv4_sock_aton ( const char *string, struct sockaddr *sa ) {
  614. struct sockaddr_in *sin = ( ( struct sockaddr_in * ) sa );
  615. struct in_addr in;
  616. if ( inet_aton ( string, &in ) ) {
  617. sin->sin_addr = in;
  618. return 0;
  619. }
  620. return -EINVAL;
  621. }
  622. /** IPv4 protocol */
  623. struct net_protocol ipv4_protocol __net_protocol = {
  624. .name = "IP",
  625. .net_proto = htons ( ETH_P_IP ),
  626. .net_addr_len = sizeof ( struct in_addr ),
  627. .rx = ipv4_rx,
  628. .ntoa = ipv4_ntoa,
  629. };
  630. /** IPv4 TCPIP net protocol */
  631. struct tcpip_net_protocol ipv4_tcpip_protocol __tcpip_net_protocol = {
  632. .name = "IPv4",
  633. .sa_family = AF_INET,
  634. .header_len = sizeof ( struct iphdr ),
  635. .tx = ipv4_tx,
  636. .netdev = ipv4_netdev,
  637. };
  638. /** IPv4 ARP protocol */
  639. struct arp_net_protocol ipv4_arp_protocol __arp_net_protocol = {
  640. .net_protocol = &ipv4_protocol,
  641. .check = ipv4_arp_check,
  642. };
  643. /** IPv4 socket address converter */
  644. struct sockaddr_converter ipv4_sockaddr_converter __sockaddr_converter = {
  645. .family = AF_INET,
  646. .ntoa = ipv4_sock_ntoa,
  647. .aton = ipv4_sock_aton,
  648. };
  649. /******************************************************************************
  650. *
  651. * Settings
  652. *
  653. ******************************************************************************
  654. */
  655. /**
  656. * Parse IPv4 address setting value
  657. *
  658. * @v type Setting type
  659. * @v value Formatted setting value
  660. * @v buf Buffer to contain raw value
  661. * @v len Length of buffer
  662. * @ret len Length of raw value, or negative error
  663. */
  664. int parse_ipv4_setting ( const struct setting_type *type __unused,
  665. const char *value, void *buf, size_t len ) {
  666. struct in_addr ipv4;
  667. /* Parse IPv4 address */
  668. if ( inet_aton ( value, &ipv4 ) == 0 )
  669. return -EINVAL;
  670. /* Copy to buffer */
  671. if ( len > sizeof ( ipv4 ) )
  672. len = sizeof ( ipv4 );
  673. memcpy ( buf, &ipv4, len );
  674. return ( sizeof ( ipv4 ) );
  675. }
  676. /**
  677. * Format IPv4 address setting value
  678. *
  679. * @v type Setting type
  680. * @v raw Raw setting value
  681. * @v raw_len Length of raw setting value
  682. * @v buf Buffer to contain formatted value
  683. * @v len Length of buffer
  684. * @ret len Length of formatted value, or negative error
  685. */
  686. int format_ipv4_setting ( const struct setting_type *type __unused,
  687. const void *raw, size_t raw_len, char *buf,
  688. size_t len ) {
  689. const struct in_addr *ipv4 = raw;
  690. if ( raw_len < sizeof ( *ipv4 ) )
  691. return -EINVAL;
  692. return snprintf ( buf, len, "%s", inet_ntoa ( *ipv4 ) );
  693. }
  694. /** IPv4 address setting */
  695. const struct setting ip_setting __setting ( SETTING_IP, ip ) = {
  696. .name = "ip",
  697. .description = "IP address",
  698. .tag = DHCP_EB_YIADDR,
  699. .type = &setting_type_ipv4,
  700. };
  701. /** IPv4 subnet mask setting */
  702. const struct setting netmask_setting __setting ( SETTING_IP, netmask ) = {
  703. .name = "netmask",
  704. .description = "Subnet mask",
  705. .tag = DHCP_SUBNET_MASK,
  706. .type = &setting_type_ipv4,
  707. };
  708. /** Default gateway setting */
  709. const struct setting gateway_setting __setting ( SETTING_IP, gateway ) = {
  710. .name = "gateway",
  711. .description = "Default gateway",
  712. .tag = DHCP_ROUTERS,
  713. .type = &setting_type_ipv4,
  714. };
  715. /**
  716. * Create IPv4 routing table based on configured settings
  717. *
  718. * @ret rc Return status code
  719. */
  720. static int ipv4_create_routes ( void ) {
  721. struct ipv4_miniroute *miniroute;
  722. struct ipv4_miniroute *tmp;
  723. struct net_device *netdev;
  724. struct settings *settings;
  725. struct in_addr address = { 0 };
  726. struct in_addr netmask = { 0 };
  727. struct in_addr gateway = { 0 };
  728. /* Delete all existing routes */
  729. list_for_each_entry_safe ( miniroute, tmp, &ipv4_miniroutes, list )
  730. del_ipv4_miniroute ( miniroute );
  731. /* Create a route for each configured network device */
  732. for_each_netdev ( netdev ) {
  733. settings = netdev_settings ( netdev );
  734. /* Get IPv4 address */
  735. address.s_addr = 0;
  736. fetch_ipv4_setting ( settings, &ip_setting, &address );
  737. if ( ! address.s_addr )
  738. continue;
  739. /* Get subnet mask */
  740. fetch_ipv4_setting ( settings, &netmask_setting, &netmask );
  741. /* Calculate default netmask, if necessary */
  742. if ( ! netmask.s_addr ) {
  743. if ( IN_IS_CLASSA ( address.s_addr ) ) {
  744. netmask.s_addr = INADDR_NET_CLASSA;
  745. } else if ( IN_IS_CLASSB ( address.s_addr ) ) {
  746. netmask.s_addr = INADDR_NET_CLASSB;
  747. } else if ( IN_IS_CLASSC ( address.s_addr ) ) {
  748. netmask.s_addr = INADDR_NET_CLASSC;
  749. }
  750. }
  751. /* Get default gateway, if present */
  752. fetch_ipv4_setting ( settings, &gateway_setting, &gateway );
  753. /* Configure route */
  754. miniroute = add_ipv4_miniroute ( netdev, address,
  755. netmask, gateway );
  756. if ( ! miniroute )
  757. return -ENOMEM;
  758. }
  759. return 0;
  760. }
  761. /** IPv4 settings applicator */
  762. struct settings_applicator ipv4_settings_applicator __settings_applicator = {
  763. .apply = ipv4_create_routes,
  764. };
  765. /* Drag in objects via ipv4_protocol */
  766. REQUIRING_SYMBOL ( ipv4_protocol );
  767. /* Drag in ICMPv4 */
  768. REQUIRE_OBJECT ( icmpv4 );