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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 <gpxe/list.h>
  8. #include <gpxe/in.h>
  9. #include <gpxe/arp.h>
  10. #include <gpxe/if_ether.h>
  11. #include <gpxe/iobuf.h>
  12. #include <gpxe/netdevice.h>
  13. #include <gpxe/ip.h>
  14. #include <gpxe/tcpip.h>
  15. #include <gpxe/dhcp.h>
  16. #include <gpxe/settings.h>
  17. /** @file
  18. *
  19. * IPv4 protocol
  20. *
  21. */
  22. /* Unique IP datagram identification number */
  23. static uint16_t next_ident = 0;
  24. struct net_protocol ipv4_protocol;
  25. /** List of IPv4 miniroutes */
  26. struct list_head ipv4_miniroutes = LIST_HEAD_INIT ( ipv4_miniroutes );
  27. /** List of fragment reassembly buffers */
  28. static LIST_HEAD ( frag_buffers );
  29. /**
  30. * Add IPv4 minirouting table entry
  31. *
  32. * @v netdev Network device
  33. * @v address IPv4 address
  34. * @v netmask Subnet mask
  35. * @v gateway Gateway address (or @c INADDR_NONE for no gateway)
  36. * @ret miniroute Routing table entry, or NULL
  37. */
  38. static struct ipv4_miniroute * __malloc
  39. add_ipv4_miniroute ( struct net_device *netdev, struct in_addr address,
  40. struct in_addr netmask, struct in_addr gateway ) {
  41. struct ipv4_miniroute *miniroute;
  42. DBG ( "IPv4 add %s", inet_ntoa ( address ) );
  43. DBG ( "/%s ", inet_ntoa ( netmask ) );
  44. if ( gateway.s_addr != INADDR_NONE )
  45. DBG ( "gw %s ", inet_ntoa ( gateway ) );
  46. DBG ( "via %s\n", netdev->name );
  47. /* Allocate and populate miniroute structure */
  48. miniroute = malloc ( sizeof ( *miniroute ) );
  49. if ( ! miniroute ) {
  50. DBG ( "IPv4 could not add miniroute\n" );
  51. return NULL;
  52. }
  53. /* Record routing information */
  54. miniroute->netdev = netdev_get ( netdev );
  55. miniroute->address = address;
  56. miniroute->netmask = netmask;
  57. miniroute->gateway = gateway;
  58. /* Add to end of list if we have a gateway, otherwise
  59. * to start of list.
  60. */
  61. if ( gateway.s_addr != INADDR_NONE ) {
  62. list_add_tail ( &miniroute->list, &ipv4_miniroutes );
  63. } else {
  64. list_add ( &miniroute->list, &ipv4_miniroutes );
  65. }
  66. return miniroute;
  67. }
  68. /**
  69. * Delete IPv4 minirouting table entry
  70. *
  71. * @v miniroute Routing table entry
  72. */
  73. static void del_ipv4_miniroute ( struct ipv4_miniroute *miniroute ) {
  74. DBG ( "IPv4 del %s", inet_ntoa ( miniroute->address ) );
  75. DBG ( "/%s ", inet_ntoa ( miniroute->netmask ) );
  76. if ( miniroute->gateway.s_addr != INADDR_NONE )
  77. DBG ( "gw %s ", inet_ntoa ( miniroute->gateway ) );
  78. DBG ( "via %s\n", miniroute->netdev->name );
  79. netdev_put ( miniroute->netdev );
  80. list_del ( &miniroute->list );
  81. free ( miniroute );
  82. }
  83. /**
  84. * Create IPv4 routing table
  85. *
  86. * @ret rc Return status code
  87. */
  88. static int ipv4_create_routes ( void ) {
  89. struct ipv4_miniroute *miniroute;
  90. struct ipv4_miniroute *tmp;
  91. struct net_device *netdev;
  92. struct settings *settings;
  93. struct in_addr address = { 0 };
  94. struct in_addr netmask = { 0 };
  95. struct in_addr gateway = { INADDR_NONE };
  96. /* Delete all existing routes */
  97. list_for_each_entry_safe ( miniroute, tmp, &ipv4_miniroutes, list )
  98. del_ipv4_miniroute ( miniroute );
  99. /* Create a route for each configured network device */
  100. for_each_netdev ( netdev ) {
  101. settings = netdev_settings ( netdev );
  102. /* Get IPv4 address */
  103. address.s_addr = 0;
  104. fetch_ipv4_setting ( settings, DHCP_EB_YIADDR, &address );
  105. if ( ! address.s_addr )
  106. continue;
  107. /* Calculate default netmask */
  108. if ( IN_CLASSA ( ntohl ( address.s_addr ) ) ) {
  109. netmask.s_addr = htonl ( IN_CLASSA_NET );
  110. } else if ( IN_CLASSB ( ntohl ( address.s_addr ) ) ) {
  111. netmask.s_addr = htonl ( IN_CLASSB_NET );
  112. } else if ( IN_CLASSC ( ntohl ( address.s_addr ) ) ) {
  113. netmask.s_addr = htonl ( IN_CLASSC_NET );
  114. } else {
  115. netmask.s_addr = 0;
  116. }
  117. /* Override with subnet mask, if present */
  118. fetch_ipv4_setting ( settings, DHCP_SUBNET_MASK, &netmask );
  119. /* Get default gateway, if present */
  120. gateway.s_addr = INADDR_NONE;
  121. fetch_ipv4_setting ( settings, DHCP_ROUTERS, &gateway );
  122. /* Configure route */
  123. miniroute = add_ipv4_miniroute ( netdev, address,
  124. netmask, gateway );
  125. if ( ! miniroute )
  126. return -ENOMEM;
  127. }
  128. return 0;
  129. }
  130. /** IPv4 settings applicator */
  131. struct settings_applicator ipv4_settings_applicator __settings_applicator = {
  132. .apply = ipv4_create_routes,
  133. };
  134. /**
  135. * Perform IPv4 routing
  136. *
  137. * @v dest Final destination address
  138. * @ret dest Next hop destination address
  139. * @ret miniroute Routing table entry to use, or NULL if no route
  140. *
  141. * If the route requires use of a gateway, the next hop destination
  142. * address will be overwritten with the gateway address.
  143. */
  144. static struct ipv4_miniroute * ipv4_route ( struct in_addr *dest ) {
  145. struct ipv4_miniroute *miniroute;
  146. int local;
  147. int has_gw;
  148. /* Never attempt to route the broadcast address */
  149. if ( dest->s_addr == INADDR_BROADCAST )
  150. return NULL;
  151. /* Find first usable route in routing table */
  152. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  153. local = ( ( ( dest->s_addr ^ miniroute->address.s_addr )
  154. & miniroute->netmask.s_addr ) == 0 );
  155. has_gw = ( miniroute->gateway.s_addr != INADDR_NONE );
  156. if ( local || has_gw ) {
  157. if ( ! local )
  158. *dest = miniroute->gateway;
  159. return miniroute;
  160. }
  161. }
  162. return NULL;
  163. }
  164. /**
  165. * Fragment reassembly counter timeout
  166. *
  167. * @v timer Retry timer
  168. * @v over If asserted, the timer is greater than @c MAX_TIMEOUT
  169. */
  170. static void ipv4_frag_expired ( struct retry_timer *timer __unused,
  171. int over ) {
  172. if ( over ) {
  173. DBG ( "Fragment reassembly timeout" );
  174. /* Free the fragment buffer */
  175. }
  176. }
  177. /**
  178. * Free fragment buffer
  179. *
  180. * @v fragbug Fragment buffer
  181. */
  182. static void free_fragbuf ( struct frag_buffer *fragbuf ) {
  183. free ( fragbuf );
  184. }
  185. /**
  186. * Fragment reassembler
  187. *
  188. * @v iobuf I/O buffer, fragment of the datagram
  189. * @ret frag_iob Reassembled packet, or NULL
  190. */
  191. static struct io_buffer * ipv4_reassemble ( struct io_buffer * iobuf ) {
  192. struct iphdr *iphdr = iobuf->data;
  193. struct frag_buffer *fragbuf;
  194. /**
  195. * Check if the fragment belongs to any fragment series
  196. */
  197. list_for_each_entry ( fragbuf, &frag_buffers, list ) {
  198. if ( fragbuf->ident == iphdr->ident &&
  199. fragbuf->src.s_addr == iphdr->src.s_addr ) {
  200. /**
  201. * Check if the packet is the expected fragment
  202. *
  203. * The offset of the new packet must be equal to the
  204. * length of the data accumulated so far (the length of
  205. * the reassembled I/O buffer
  206. */
  207. if ( iob_len ( fragbuf->frag_iob ) ==
  208. ( iphdr->frags & IP_MASK_OFFSET ) ) {
  209. /**
  210. * Append the contents of the fragment to the
  211. * reassembled I/O buffer
  212. */
  213. iob_pull ( iobuf, sizeof ( *iphdr ) );
  214. memcpy ( iob_put ( fragbuf->frag_iob,
  215. iob_len ( iobuf ) ),
  216. iobuf->data, iob_len ( iobuf ) );
  217. free_iob ( iobuf );
  218. /** Check if the fragment series is over */
  219. if ( !iphdr->frags & IP_MASK_MOREFRAGS ) {
  220. iobuf = fragbuf->frag_iob;
  221. free_fragbuf ( fragbuf );
  222. return iobuf;
  223. }
  224. } else {
  225. /* Discard the fragment series */
  226. free_fragbuf ( fragbuf );
  227. free_iob ( iobuf );
  228. }
  229. return NULL;
  230. }
  231. }
  232. /** Check if the fragment is the first in the fragment series */
  233. if ( iphdr->frags & IP_MASK_MOREFRAGS &&
  234. ( ( iphdr->frags & IP_MASK_OFFSET ) == 0 ) ) {
  235. /** Create a new fragment buffer */
  236. fragbuf = ( struct frag_buffer* ) malloc ( sizeof( *fragbuf ) );
  237. fragbuf->ident = iphdr->ident;
  238. fragbuf->src = iphdr->src;
  239. /* Set up the reassembly I/O buffer */
  240. fragbuf->frag_iob = alloc_iob ( IP_FRAG_IOB_SIZE );
  241. iob_pull ( iobuf, sizeof ( *iphdr ) );
  242. memcpy ( iob_put ( fragbuf->frag_iob, iob_len ( iobuf ) ),
  243. iobuf->data, iob_len ( iobuf ) );
  244. free_iob ( iobuf );
  245. /* Set the reassembly timer */
  246. fragbuf->frag_timer.timeout = IP_FRAG_TIMEOUT;
  247. fragbuf->frag_timer.expired = ipv4_frag_expired;
  248. start_timer ( &fragbuf->frag_timer );
  249. /* Add the fragment buffer to the list of fragment buffers */
  250. list_add ( &fragbuf->list, &frag_buffers );
  251. }
  252. return NULL;
  253. }
  254. /**
  255. * Add IPv4 pseudo-header checksum to existing checksum
  256. *
  257. * @v iobuf I/O buffer
  258. * @v csum Existing checksum
  259. * @ret csum Updated checksum
  260. */
  261. static uint16_t ipv4_pshdr_chksum ( struct io_buffer *iobuf, uint16_t csum ) {
  262. struct ipv4_pseudo_header pshdr;
  263. struct iphdr *iphdr = iobuf->data;
  264. size_t hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
  265. /* Build pseudo-header */
  266. pshdr.src = iphdr->src;
  267. pshdr.dest = iphdr->dest;
  268. pshdr.zero_padding = 0x00;
  269. pshdr.protocol = iphdr->protocol;
  270. pshdr.len = htons ( iob_len ( iobuf ) - hdrlen );
  271. /* Update the checksum value */
  272. return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
  273. }
  274. /**
  275. * Determine link-layer address
  276. *
  277. * @v dest IPv4 destination address
  278. * @v src IPv4 source address
  279. * @v netdev Network device
  280. * @v ll_dest Link-layer destination address buffer
  281. * @ret rc Return status code
  282. */
  283. static int ipv4_ll_addr ( struct in_addr dest, struct in_addr src,
  284. struct net_device *netdev, uint8_t *ll_dest ) {
  285. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  286. uint8_t *dest_bytes = ( ( uint8_t * ) &dest );
  287. if ( dest.s_addr == INADDR_BROADCAST ) {
  288. /* Broadcast address */
  289. memcpy ( ll_dest, ll_protocol->ll_broadcast,
  290. ll_protocol->ll_addr_len );
  291. return 0;
  292. } else if ( IN_MULTICAST ( dest.s_addr ) ) {
  293. /* Special case: IPv4 multicast over Ethernet. This
  294. * code may need to be generalised once we find out
  295. * what happens for other link layers.
  296. */
  297. ll_dest[0] = 0x01;
  298. ll_dest[1] = 0x00;
  299. ll_dest[2] = 0x5e;
  300. ll_dest[3] = dest_bytes[1] & 0x7f;
  301. ll_dest[4] = dest_bytes[2];
  302. ll_dest[5] = dest_bytes[3];
  303. return 0;
  304. } else {
  305. /* Unicast address: resolve via ARP */
  306. return arp_resolve ( netdev, &ipv4_protocol, &dest,
  307. &src, ll_dest );
  308. }
  309. }
  310. /**
  311. * Transmit IP packet
  312. *
  313. * @v iobuf I/O buffer
  314. * @v tcpip Transport-layer protocol
  315. * @v st_dest Destination network-layer address
  316. * @v netdev Network device to use if no route found, or NULL
  317. * @v trans_csum Transport-layer checksum to complete, or NULL
  318. * @ret rc Status
  319. *
  320. * This function expects a transport-layer segment and prepends the IP header
  321. */
  322. static int ipv4_tx ( struct io_buffer *iobuf,
  323. struct tcpip_protocol *tcpip_protocol,
  324. struct sockaddr_tcpip *st_dest,
  325. struct net_device *netdev,
  326. uint16_t *trans_csum ) {
  327. struct iphdr *iphdr = iob_push ( iobuf, sizeof ( *iphdr ) );
  328. struct sockaddr_in *sin_dest = ( ( struct sockaddr_in * ) st_dest );
  329. struct ipv4_miniroute *miniroute;
  330. struct in_addr next_hop;
  331. uint8_t ll_dest[MAX_LL_ADDR_LEN];
  332. int rc;
  333. /* Fill up the IP header, except source address */
  334. memset ( iphdr, 0, sizeof ( *iphdr ) );
  335. iphdr->verhdrlen = ( IP_VER | ( sizeof ( *iphdr ) / 4 ) );
  336. iphdr->service = IP_TOS;
  337. iphdr->len = htons ( iob_len ( iobuf ) );
  338. iphdr->ident = htons ( ++next_ident );
  339. iphdr->ttl = IP_TTL;
  340. iphdr->protocol = tcpip_protocol->tcpip_proto;
  341. iphdr->dest = sin_dest->sin_addr;
  342. /* Use routing table to identify next hop and transmitting netdev */
  343. next_hop = iphdr->dest;
  344. if ( ( miniroute = ipv4_route ( &next_hop ) ) ) {
  345. iphdr->src = miniroute->address;
  346. netdev = miniroute->netdev;
  347. }
  348. if ( ! netdev ) {
  349. DBG ( "IPv4 has no route to %s\n", inet_ntoa ( iphdr->dest ) );
  350. rc = -ENETUNREACH;
  351. goto err;
  352. }
  353. /* Determine link-layer destination address */
  354. if ( ( rc = ipv4_ll_addr ( next_hop, iphdr->src, netdev,
  355. ll_dest ) ) != 0 ) {
  356. DBG ( "IPv4 has no link-layer address for %s: %s\n",
  357. inet_ntoa ( next_hop ), strerror ( rc ) );
  358. goto err;
  359. }
  360. /* Fix up checksums */
  361. if ( trans_csum )
  362. *trans_csum = ipv4_pshdr_chksum ( iobuf, *trans_csum );
  363. iphdr->chksum = tcpip_chksum ( iphdr, sizeof ( *iphdr ) );
  364. /* Print IP4 header for debugging */
  365. DBG ( "IPv4 TX %s->", inet_ntoa ( iphdr->src ) );
  366. DBG ( "%s len %d proto %d id %04x csum %04x\n",
  367. inet_ntoa ( iphdr->dest ), ntohs ( iphdr->len ), iphdr->protocol,
  368. ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );
  369. /* Hand off to link layer */
  370. if ( ( rc = net_tx ( iobuf, netdev, &ipv4_protocol, ll_dest ) ) != 0 ) {
  371. DBG ( "IPv4 could not transmit packet via %s: %s\n",
  372. netdev->name, strerror ( rc ) );
  373. return rc;
  374. }
  375. return 0;
  376. err:
  377. free_iob ( iobuf );
  378. return rc;
  379. }
  380. /**
  381. * Process incoming packets
  382. *
  383. * @v iobuf I/O buffer
  384. * @v netdev Network device
  385. * @v ll_source Link-layer destination source
  386. *
  387. * This function expects an IP4 network datagram. It processes the headers
  388. * and sends it to the transport layer.
  389. */
  390. static int ipv4_rx ( struct io_buffer *iobuf, struct net_device *netdev __unused,
  391. const void *ll_source __unused ) {
  392. struct iphdr *iphdr = iobuf->data;
  393. size_t hdrlen;
  394. size_t len;
  395. union {
  396. struct sockaddr_in sin;
  397. struct sockaddr_tcpip st;
  398. } src, dest;
  399. uint16_t csum;
  400. uint16_t pshdr_csum;
  401. int rc;
  402. /* Sanity check the IPv4 header */
  403. if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
  404. DBG ( "IPv4 packet too short at %zd bytes (min %zd bytes)\n",
  405. iob_len ( iobuf ), sizeof ( *iphdr ) );
  406. goto err;
  407. }
  408. if ( ( iphdr->verhdrlen & IP_MASK_VER ) != IP_VER ) {
  409. DBG ( "IPv4 version %#02x not supported\n", iphdr->verhdrlen );
  410. goto err;
  411. }
  412. hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
  413. if ( hdrlen < sizeof ( *iphdr ) ) {
  414. DBG ( "IPv4 header too short at %zd bytes (min %zd bytes)\n",
  415. hdrlen, sizeof ( *iphdr ) );
  416. goto err;
  417. }
  418. if ( hdrlen > iob_len ( iobuf ) ) {
  419. DBG ( "IPv4 header too long at %zd bytes "
  420. "(packet is %zd bytes)\n", hdrlen, iob_len ( iobuf ) );
  421. goto err;
  422. }
  423. if ( ( csum = tcpip_chksum ( iphdr, hdrlen ) ) != 0 ) {
  424. DBG ( "IPv4 checksum incorrect (is %04x including checksum "
  425. "field, should be 0000)\n", csum );
  426. goto err;
  427. }
  428. len = ntohs ( iphdr->len );
  429. if ( len < hdrlen ) {
  430. DBG ( "IPv4 length too short at %zd bytes "
  431. "(header is %zd bytes)\n", len, hdrlen );
  432. goto err;
  433. }
  434. if ( len > iob_len ( iobuf ) ) {
  435. DBG ( "IPv4 length too long at %zd bytes "
  436. "(packet is %zd bytes)\n", len, iob_len ( iobuf ) );
  437. goto err;
  438. }
  439. /* Print IPv4 header for debugging */
  440. DBG ( "IPv4 RX %s<-", inet_ntoa ( iphdr->dest ) );
  441. DBG ( "%s len %d proto %d id %04x csum %04x\n",
  442. inet_ntoa ( iphdr->src ), ntohs ( iphdr->len ), iphdr->protocol,
  443. ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );
  444. /* Truncate packet to correct length, calculate pseudo-header
  445. * checksum and then strip off the IPv4 header.
  446. */
  447. iob_unput ( iobuf, ( iob_len ( iobuf ) - len ) );
  448. pshdr_csum = ipv4_pshdr_chksum ( iobuf, TCPIP_EMPTY_CSUM );
  449. iob_pull ( iobuf, hdrlen );
  450. /* Fragment reassembly */
  451. if ( ( iphdr->frags & htons ( IP_MASK_MOREFRAGS ) ) ||
  452. ( ( iphdr->frags & htons ( IP_MASK_OFFSET ) ) != 0 ) ) {
  453. /* Pass the fragment to ipv4_reassemble() which either
  454. * returns a fully reassembled I/O buffer or NULL.
  455. */
  456. iobuf = ipv4_reassemble ( iobuf );
  457. if ( ! iobuf )
  458. return 0;
  459. }
  460. /* Construct socket addresses and hand off to transport layer */
  461. memset ( &src, 0, sizeof ( src ) );
  462. src.sin.sin_family = AF_INET;
  463. src.sin.sin_addr = iphdr->src;
  464. memset ( &dest, 0, sizeof ( dest ) );
  465. dest.sin.sin_family = AF_INET;
  466. dest.sin.sin_addr = iphdr->dest;
  467. if ( ( rc = tcpip_rx ( iobuf, iphdr->protocol, &src.st,
  468. &dest.st, pshdr_csum ) ) != 0 ) {
  469. DBG ( "IPv4 received packet rejected by stack: %s\n",
  470. strerror ( rc ) );
  471. return rc;
  472. }
  473. return 0;
  474. err:
  475. free_iob ( iobuf );
  476. return -EINVAL;
  477. }
  478. /**
  479. * Check existence of IPv4 address for ARP
  480. *
  481. * @v netdev Network device
  482. * @v net_addr Network-layer address
  483. * @ret rc Return status code
  484. */
  485. static int ipv4_arp_check ( struct net_device *netdev, const void *net_addr ) {
  486. const struct in_addr *address = net_addr;
  487. struct ipv4_miniroute *miniroute;
  488. list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
  489. if ( ( miniroute->netdev == netdev ) &&
  490. ( miniroute->address.s_addr == address->s_addr ) ) {
  491. /* Found matching address */
  492. return 0;
  493. }
  494. }
  495. return -ENOENT;
  496. }
  497. /**
  498. * Convert IPv4 address to dotted-quad notation
  499. *
  500. * @v in IP address
  501. * @ret string IP address in dotted-quad notation
  502. */
  503. char * inet_ntoa ( struct in_addr in ) {
  504. static char buf[16]; /* "xxx.xxx.xxx.xxx" */
  505. uint8_t *bytes = ( uint8_t * ) &in;
  506. sprintf ( buf, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3] );
  507. return buf;
  508. }
  509. /**
  510. * Transcribe IP address
  511. *
  512. * @v net_addr IP address
  513. * @ret string IP address in dotted-quad notation
  514. *
  515. */
  516. static const char * ipv4_ntoa ( const void *net_addr ) {
  517. return inet_ntoa ( * ( ( struct in_addr * ) net_addr ) );
  518. }
  519. /** IPv4 protocol */
  520. struct net_protocol ipv4_protocol __net_protocol = {
  521. .name = "IP",
  522. .net_proto = htons ( ETH_P_IP ),
  523. .net_addr_len = sizeof ( struct in_addr ),
  524. .rx = ipv4_rx,
  525. .ntoa = ipv4_ntoa,
  526. };
  527. /** IPv4 TCPIP net protocol */
  528. struct tcpip_net_protocol ipv4_tcpip_protocol __tcpip_net_protocol = {
  529. .name = "IPv4",
  530. .sa_family = AF_INET,
  531. .tx = ipv4_tx,
  532. };
  533. /** IPv4 ARP protocol */
  534. struct arp_net_protocol ipv4_arp_protocol __arp_net_protocol = {
  535. .net_protocol = &ipv4_protocol,
  536. .check = ipv4_arp_check,
  537. };