Fixed buffer overflow in mserver; fixed type of checkinfo () 2nd parameter; memory
[ZeXOS.git] / kernel / core / net / packet.c
blob828ae650cfad1431e8313aeb8ba5ad2aef62bbc0
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <config.h>
23 #include <system.h>
24 #include <string.h>
25 #include <dev.h>
26 #include <net/eth.h>
27 #include <net/net.h>
28 #include <net/if.h>
29 #include <net/packet.h>
30 #include <task.h>
31 #include <mutex.h>
33 MUTEX_CREATE (mutex_packet);
35 extern netif_t netif_list;
36 extern unsigned long timer_ticks;
38 static task_t *task_netcore = NULL;
40 unsigned net_packet_send (netif_t *netif, packet_t *packet, char *buf, unsigned len)
42 mutex_lock (&mutex_packet);
44 if (!netif) {
45 mutex_unlock (&mutex_packet);
46 return 0;
49 memcpy (netif->buf_tx, (char *) packet, 14);
50 memcpy (netif->buf_tx+14, buf, len);
52 netif->dev->write ((char *) netif->buf_tx, 14+len);
54 /* add transfered bytes to stats */
55 netif->dev->info_tx += 14+len;
57 mutex_unlock (&mutex_packet);
59 return 1;
62 /* Here we process a received packets */
63 unsigned net_packet_handler (char *buf, unsigned len)
65 packet_t *packet = (packet_t *) buf;
67 if (!packet)
68 return 0;
70 //printf ("net_packet->type: 0x%04x\n", swap16 (packet->type));
72 switch (packet->type) {
73 #ifdef CONFIG_PROTO_IPV4
74 case NET_PACKET_TYPE_IPV4:
75 return net_proto_ip_handler (packet, buf+sizeof (packet_t), len-sizeof (packet_t));
76 #endif
77 case NET_PACKET_TYPE_ARP:
78 return net_proto_arp_handler (packet, buf+sizeof (packet_t), len-sizeof (packet_t));
79 #ifdef CONFIG_PROTO_IPV6
80 case NET_PACKET_TYPE_IPV6:
81 return net_proto_ipv6_handler (packet, buf+sizeof (packet_t), len-sizeof (packet_t));
82 #endif
85 return 1;
88 /* Task for checking new incoming packets */
89 unsigned task_net_packet ()
91 /* let's find network device */
92 netif_t *netif;
93 for (netif = netif_list.next; netif != &netif_list; netif = netif->next) {
94 if (!netif) {
95 task_done (task_netcore);
96 schedule ();
97 return 0;
100 /* alloc 2k packet receive and transfer buffer */
101 netif->buf_rx = (char *) kmalloc (2048 * sizeof (char));
102 netif->buf_tx = (char *) kmalloc (2048 * sizeof (char));
104 if (!netif->buf_rx || !netif->buf_tx) {
105 task_done (task_netcore);
106 schedule ();
107 return 0;
111 /* thread loop */
112 for (;; schedule ()) {
113 for (netif = netif_list.next; netif != &netif_list; netif = netif->next) {
114 /* check for incoming data */
115 int ret = netdev_rx (netif->dev, netif->buf_rx, 2048);
117 /* are there some data ? */
118 if (ret) {
119 net_packet_handler (netif->buf_rx, ret);
121 /* clear buffer */
122 memset (netif->buf_rx, 0, 2048);
127 /* free tx and rx buffers for all ethernet devices */
128 for (netif = netif_list.next; netif != &netif_list; netif = netif->next) {
129 kfree (netif->buf_tx);
130 kfree (netif->buf_rx);
133 task_done (task_netcore);
134 schedule ();
136 return 1;
140 unsigned init_packet ()
142 /* initialize unix domain sockets - we could use it without any network devices */
143 if (!init_net_proto_unix ())
144 return 0;
146 /* setup hostname of the current machine */
147 if (!init_hostname ())
148 return 0;
150 /* let's find network device first */
151 netif_t *netif = netif_findbyname ("eth0"); /* TODO: all interfaces */
153 /* without one ethernet device it does not matter on netstack */
154 if (!netif)
155 return 1;
157 if (!init_net_proto_tcp ())
158 return 0;
160 if (!init_net_proto_tcp6 ())
161 return 0;
163 if (!init_net_proto_udp ())
164 return 0;
166 if (!init_net_proto_udp6 ())
167 return 0;
169 if (!init_net_proto_dns ())
170 return 0;
172 if (!init_net_proto_tun6 ())
173 return 0;
175 /* start new task/thread for network core */
176 task_netcore = (task_t *) task_create ("netcore", (unsigned) task_net_packet, 255);
178 if (!task_netcore)
179 return 0;
181 return 1;