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.

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