Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / kernel / core / net / packet.c
blob269ac5b9b2be5293e82ed4bb2e395748843f00d6
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
22 #include <system.h>
23 #include <string.h>
24 #include <dev.h>
25 #include <net/eth.h>
26 #include <net/net.h>
27 #include <net/if.h>
28 #include <net/packet.h>
29 #include <task.h>
30 #include <mutex.h>
32 MUTEX_CREATE (mutex_packet);
34 extern netif_t netif_list;
35 extern unsigned long timer_ticks;
37 task_t *task_netcore = NULL;
39 unsigned net_packet_send (netif_t *netif, packet_t *packet, char *buf, unsigned len)
41 mutex_lock (&mutex_packet);
43 if (!netif) {
44 mutex_unlock (&mutex_packet);
45 return 0;
48 //memset (netif->buf_tx, 0, 2048);
50 memcpy (netif->buf_tx, (char *) packet, 14);
51 memcpy (netif->buf_tx+14, buf, len);
53 netif->buf_tx[14+len] = '\0';
55 netif->dev->write ((char *) netif->buf_tx, 14+len);
57 /* add transfered bytes to stats */
58 netif->dev->info_tx += 14+len;
60 mutex_unlock (&mutex_packet);
62 return 1;
65 /* Here we process a received packets */
66 unsigned net_packet_handler (char *buf, unsigned len)
68 packet_t *packet = (packet_t *) buf;
70 if (!packet)
71 return 0;
73 //printf ("net_packet->type: 0x%04x\n", swap16 (packet->type));
75 switch (packet->type) {
76 #ifdef CONFIG_PROTO_IPV4
77 case NET_PACKET_TYPE_IPV4:
78 return net_proto_ip_handler (packet, buf+sizeof (packet_t), len-sizeof (packet_t));
79 #endif
80 case NET_PACKET_TYPE_ARP:
81 return net_proto_arp_handler (packet, buf+sizeof (packet_t), len-sizeof (packet_t));
82 #ifdef CONFIG_PROTO_IPV6
83 case NET_PACKET_TYPE_IPV6:
84 return net_proto_ipv6_handler (packet, buf+sizeof (packet_t), len-sizeof (packet_t));
85 #endif
88 return 1;
91 /* Task for checking new incoming packets */
92 unsigned task_net_packet ()
94 /* let's find network device */
95 netif_t *netif;
96 for (netif = netif_list.next; netif != &netif_list; netif = netif->next) {
97 if (!netif) {
98 task_done (task_netcore);
99 schedule ();
100 return 0;
103 /* alloc 2k packet receive and transfer buffer */
104 netif->buf_rx = (char *) kmalloc (2048 * sizeof (char));
105 netif->buf_tx = (char *) kmalloc (2048 * sizeof (char));
107 if (!netif->buf_rx || !netif->buf_tx) {
108 task_done (task_netcore);
109 schedule ();
110 return 0;
114 /* thread loop */
115 for (;; schedule ()) {
116 for (netif = netif_list.next; netif != &netif_list; netif = netif->next) {
117 /* check for incoming data */
118 int ret = netdev_rx (netif->dev, netif->buf_rx, 2048);
120 /* are there some data ? */
121 if (ret) {
122 net_packet_handler (netif->buf_rx, ret);
124 /* clear buffer */
125 memset (netif->buf_rx, 0, 2048);
130 /* free tx and rx buffers for all ethernet devices */
131 for (netif = netif_list.next; netif != &netif_list; netif = netif->next) {
132 kfree (netif->buf_tx);
133 kfree (netif->buf_rx);
136 task_done (task_netcore);
137 schedule ();
139 return 1;
143 unsigned init_packet ()
145 /* initialize unix domain sockets - we could use it without any network devices */
146 if (!init_net_proto_unix ())
147 return 0;
149 /* setup hostname of the current machine */
150 if (!init_hostname ())
151 return 0;
153 /* let's find network device first */
154 netif_t *netif = netif_findbyname ("eth0"); /* TODO: all interfaces */
156 /* without one ethernet device it does not matter on netstack */
157 if (!netif)
158 return 1;
160 if (!init_net_proto_tcp ())
161 return 0;
163 if (!init_net_proto_tcp6 ())
164 return 0;
166 if (!init_net_proto_udp ())
167 return 0;
169 if (!init_net_proto_udp6 ())
170 return 0;
172 if (!init_net_proto_dns ())
173 return 0;
175 if (!init_net_proto_tun6 ())
176 return 0;
178 /* start new task/thread for network core */
179 task_netcore = (task_t *) task_create ("netcore", (unsigned) task_net_packet, 255);
181 if (!task_netcore)
182 return 0;
184 return 1;