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/>.
29 #include <net/packet.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
);
45 mutex_unlock (&mutex_packet
);
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
);
62 /* Here we process a received packets */
63 unsigned net_packet_handler (char *buf
, unsigned len
)
65 packet_t
*packet
= (packet_t
*) buf
;
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
));
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
));
88 /* Task for checking new incoming packets */
89 unsigned task_net_packet ()
91 /* let's find network device */
93 for (netif
= netif_list
.next
; netif
!= &netif_list
; netif
= netif
->next
) {
95 task_done (task_netcore
);
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
);
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 ? */
119 net_packet_handler (netif
->buf_rx
, ret
);
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
);
140 unsigned init_packet ()
142 /* initialize unix domain sockets - we could use it without any network devices */
143 if (!init_net_proto_unix ())
146 /* setup hostname of the current machine */
147 if (!init_hostname ())
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 */
157 if (!init_net_proto_tcp ())
160 if (!init_net_proto_tcp6 ())
163 if (!init_net_proto_udp ())
166 if (!init_net_proto_udp6 ())
169 if (!init_net_proto_dns ())
172 if (!init_net_proto_tun6 ())
175 /* start new task/thread for network core */
176 task_netcore
= (task_t
*) task_create ("netcore", (unsigned) task_net_packet
, 255);