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

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