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

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