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.

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