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.

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