summaryrefslogtreecommitdiff
path: root/eth-multiplexer/ethernet.c
blob: c8849aa1cef7c8b0a64056afe55e3da124c463de (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
   Copyright (C) 1995, 1996, 1998, 1999, 2000, 2002, 2007, 2008
   Free Software Foundation, Inc.

   Written by Zheng Da

   Based on pfinet/ethernet.c, written by Michael I. Bushnell, p/BSG.

   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 this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */

#include <string.h>
#include <error.h>
#include <assert-backtrace.h>
#include <net/if.h>
#include <sys/ioctl.h>

#include <hurd/ports.h>
#include <device/device.h>
#include <device/net_status.h>

#include "ethernet.h"
#include "vdev.h"
#include "util.h"

#define ETH_HLEN 14

static struct port_info *readpt;

/* Port for writing message to the real network interface. */
mach_port_t ether_port;

/* The ethernet address of the real network interface.  */
char ether_address[ETH_ALEN];

/* Port for receiving messages from the interface. */
static mach_port_t readptname;

/* The BPF instruction allows IP and ARP packets */
static struct bpf_insn ether_filter[] =
{
    {NETF_IN|NETF_BPF, /* Header. */ 0, 0, 0},
    {40, 0, 0, 12},
    {21, 1, 0, 2054},
    {21, 0, 1, 2048},
    {6, 0, 0, 1500},
    {6, 0, 0, 0}
};
static int ether_filter_len = sizeof (ether_filter) / sizeof (short);

int ethernet_demuxer (mach_msg_header_t *inp,
		      mach_msg_header_t *outp)
{
  struct net_rcv_msg *msg = (struct net_rcv_msg *) inp;

  if (inp->msgh_id != NET_RCV_MSG_ID)
    return 0;

  broadcast_msg (msg);
  /* The data from the underlying network is inside the message,
   * so we don't need to deallocate the data. */
  return 1;
}

error_t
eth_set_clear_flags (int set_flags, int clear_flags)
{
  error_t err;
  int flags;
  size_t count;

  count = 1;
  err = device_get_status (ether_port, NET_FLAGS, (dev_status_t) &flags,
                           &count);
  if (err)
    {
      error (0, err, "device_get_status");
      return err;
    }

  flags |= set_flags;
  flags &= ~clear_flags;

  err = device_set_status(ether_port, NET_FLAGS, (dev_status_t) &flags, 1);
  if (err)
    {
      error (0, err, "device_set_status");
      return err;
    }

  return 0;
}

static error_t
get_ethernet_address (mach_port_t port, char *address)
{
  error_t err;
  int net_address[2];
  size_t count = 2;
  assert_backtrace (count * sizeof (int) >= ETH_ALEN);

  err = device_get_status (port, NET_ADDRESS, net_address, &count);
  if (err)
    return err;

  net_address[0] = ntohl (net_address[0]);
  net_address[1] = ntohl (net_address[1]);
  memcpy (address, net_address, ETH_ALEN);
  return 0;
}

int ethernet_open (char *dev_name, device_t master_device,
		   struct port_bucket *etherport_bucket,
		   struct port_class *etherreadclass)
{
  error_t err;

  assert_backtrace (ether_port == MACH_PORT_NULL);

  err = ports_create_port (etherreadclass, etherport_bucket,
			   sizeof (struct port_info), &readpt);
  if (err)
    error (2, err, "ports_create_port");
  readptname = ports_get_right (readpt);
  mach_port_insert_right (mach_task_self (), readptname, readptname,
			  MACH_MSG_TYPE_MAKE_SEND);

  mach_port_set_qlimit (mach_task_self (), readptname, MACH_PORT_QLIMIT_MAX);

  err = device_open (master_device, D_WRITE | D_READ, "eth", &ether_port);
  mach_port_deallocate (mach_task_self (), master_device);
  if (err)
    error (2, err, "device_open: %s", dev_name);

  err = device_set_filter (ether_port, ports_get_right (readpt),
			   MACH_MSG_TYPE_MAKE_SEND, 0,
			   (unsigned short *)ether_filter, ether_filter_len);
  if (err)
    error (2, err, "device_set_filter: %s", dev_name);

  err = eth_set_clear_flags (IFF_PROMISC, 0);
  if (err)
    error (2, err, "eth_set_clear_flags");

  err = get_ethernet_address (ether_port, ether_address);
  if (err)
    error (2, err, "%s: Cannot get hardware Ethernet address", dev_name);

  return 0;
}

int ethernet_close (char *dev_name)
{
  error_t err;

  err = eth_set_clear_flags (0, IFF_PROMISC);
  if (err)
    error (2, err, "eth_set_clear_flags");

  return 0;
}