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

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