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/>.
28 #include <net/packet.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
);
44 mutex_unlock (&mutex_packet
);
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
);
65 /* Here we process a received packets */
66 unsigned net_packet_handler (char *buf
, unsigned len
)
68 packet_t
*packet
= (packet_t
*) buf
;
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
));
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
));
91 /* Task for checking new incoming packets */
92 unsigned task_net_packet ()
94 /* let's find network device */
96 for (netif
= netif_list
.next
; netif
!= &netif_list
; netif
= netif
->next
) {
98 task_done (task_netcore
);
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
);
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 ? */
122 net_packet_handler (netif
->buf_rx
, ret
);
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
);
143 unsigned init_packet ()
145 /* initialize unix domain sockets - we could use it without any network devices */
146 if (!init_net_proto_unix ())
149 /* setup hostname of the current machine */
150 if (!init_hostname ())
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 */
160 if (!init_net_proto_tcp ())
163 if (!init_net_proto_tcp6 ())
166 if (!init_net_proto_udp ())
169 if (!init_net_proto_udp6 ())
172 if (!init_net_proto_dns ())
175 if (!init_net_proto_tun6 ())
178 /* start new task/thread for network core */
179 task_netcore
= (task_t
*) task_create ("netcore", (unsigned) task_net_packet
, 255);