aboutsummaryrefslogtreecommitdiff
path: root/lwip/port/netif/hurdloopif.c
blob: ef64b8b6d37f7e16304e89a9b29c36d84417d7f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
   Copyright (C) 2017 Free Software Foundation, Inc.
   Written by Joan Lledó.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd.  If not, see <http://www.gnu.org/licenses/>.
*/

/* Loopback devices module */

#include <netif/hurdloopif.h>

#include <net/if.h>
#include <net/if_arp.h>
#include <string.h>

#include <lwip-util.h>

/* Set the device flags */
static error_t
hurdloopif_device_set_flags (struct netif *netif, uint16_t flags)
{
  error_t err = 0;
  hurdloopif *loopif;

  loopif = netif_get_state (netif);
  loopif->flags = flags;

  return err;
}

/*
 * Update the interface's MTU
 */
static error_t
hurdloopif_device_update_mtu (struct netif *netif, uint32_t mtu)
{
  error_t err = 0;

  netif->mtu = mtu;

  return err;
}

/*
 * Release all resources of this netif.
 *
 * Returns 0 on success.
 */
static error_t
hurdloopif_device_terminate (struct netif *netif)
{
  /* Free the hook */
  free (netif_get_state (netif)->devname);
  free (netif_get_state (netif));

  return 0;
}

/*
 * Set up the LwIP loopback interface
 */
error_t
hurdloopif_device_init (struct netif * netif)
{
  error_t err = 0;
  hurdloopif *loopif;

  /*
   * Replace the hook by a new one with the proper size.
   * The old one is in the stack and will be removed soon.
   */
  loopif = calloc (1, sizeof (hurdloopif));
  if (loopif == NULL)
    {
      LWIP_DEBUGF (NETIF_DEBUG, ("hurdloopif_init: out of memory\n"));
      return ERR_MEM;
    }
  memcpy (loopif, netif_get_state (netif), sizeof (struct ifcommon));
  netif->state = loopif;

  /* Device name and type */
  loopif->devname = LOOP_DEV_NAME;
  loopif->type = ARPHRD_LOOPBACK;

  /* MTU = MSS + IP header + TCP header */
  netif->mtu = TCP_MSS + 20 + 20;

  /* Set flags */
  hurdloopif_device_set_flags (netif, IFF_UP | IFF_RUNNING | IFF_LOOPBACK);

  /* Set callbacks */
  loopif->open = 0;
  loopif->close = 0;
  loopif->terminate = hurdloopif_device_terminate;
  loopif->update_mtu = hurdloopif_device_update_mtu;
  loopif->change_flags = hurdloopif_device_set_flags;

  return err;
}