Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / kernel / core / net / if.c
blobdc565bd109597b8a3550ecd564ccb97a0c5c5a4e
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 <system.h>
22 #include <string.h>
23 #include <net/eth.h>
24 #include <net/net.h>
25 #include <net/if.h>
26 #include <net/ip.h>
27 #include <net/tun.h>
29 netif_t netif_list;
31 static net_port net_port_curr;
33 #define DEFAULT_IP_ADDRESS NET_IPV4_TO_ADDR (192,168,0,50)
34 #define DEFAULT_GW_ADDRESS NET_IPV4_TO_ADDR (192,168,0,1)
36 /* type of the address config in text form */
37 static char *if_cfg_type[] = { "Static", "DHCP", "Advert" };
39 netif_t *netif_findbyname (char *name)
41 netif_t *netif;
42 for (netif = netif_list.next; netif != &netif_list; netif = netif->next) {
43 if (!strcmp (netif->dev->name, name))
44 return netif;
47 return 0;
50 unsigned netif_ip_addr (netif_t *netif, net_ipv4 ip, unsigned char cfg)
52 if (!netif)
53 return 0;
55 netif->ip = ip;
56 netif->cfg = cfg;
58 return 1;
61 unsigned netif_ipv6_addr (netif_t *netif, net_ipv6 ip, unsigned char cfg)
63 if (!netif)
64 return 0;
66 memcpy (netif->ipv6, ip, sizeof (net_ipv6));
67 netif->cfgv6 = cfg;
69 return 1;
72 unsigned netif_gw_addr (netif_t *netif, net_ipv4 gw)
74 if (!netif)
75 return 0;
77 netif->gw = gw;
79 return 1;
82 unsigned netif_gwv6_addr (netif_t *netif, net_ipv6 gw)
84 if (!netif)
85 return 0;
87 memcpy (netif->gwv6, gw, sizeof (net_ipv6));
89 return 1;
92 netif_t *netif_create (struct netdev_t *dev)
94 if (!dev)
95 return 0;
97 netif_t *netif;
99 /* alloc and init context */
100 netif = (netif_t *) kmalloc (sizeof (netif_t));
102 if (!netif)
103 return 0;
105 netif->dev = dev;
107 if (!dev->dev_addr)
108 return 0;
110 netif_ip_addr (netif, DEFAULT_IP_ADDRESS, IF_CFG_TYPE_STATIC);
111 netif_gw_addr (netif, DEFAULT_GW_ADDRESS);
113 net_ipv6 ipv6;
114 NET_IPV6_TO_ADDR (ipv6, 0xfc00, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10);
115 netif_ipv6_addr (netif, ipv6, IF_CFG_TYPE_STATIC);
117 NET_IPV6_TO_ADDR (ipv6, 0xfc00, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1);
118 netif_gwv6_addr (netif, ipv6);
120 /* add into list */
121 netif->next = &netif_list;
122 netif->prev = netif_list.prev;
123 netif->prev->next = netif;
124 netif->next->prev = netif;
126 return netif;
129 net_port netif_port_get ()
131 return net_port_curr ++;
134 void iflist_display ()
136 netif_t *netif;
137 for (netif = netif_list.next; netif != &netif_list; netif = netif->next) {
138 if (!netif)
139 continue;
141 printf ("if: %s - MAC addr: %02x:%02x:%02x:%02x:%02x:%02x\n\tRx bytes: %lu\tTx bytes: %lu\n", netif->dev->name,
142 netif->dev->dev_addr[0], netif->dev->dev_addr[1], netif->dev->dev_addr[2],
143 netif->dev->dev_addr[3], netif->dev->dev_addr[4], netif->dev->dev_addr[5],
144 netif->dev->info_rx, netif->dev->info_tx);
145 printf ("\tEthernet I/O base:\t0x%x\n", netif->dev->base_addr);
146 printf ("\tEthernet IP address:\t"); net_proto_ip_print (netif->ip); printf (" /%s", if_cfg_type[netif->cfg]);
147 printf ("\n\t\t\t\t"); net_proto_ipv6_print (netif->ipv6); printf (" /%s", if_cfg_type[netif->cfgv6]);
148 printf ("\n\tGateway IP address:\t"); net_proto_ip_print (netif->gw);
149 printf ("\n\t\t\t\t"); net_proto_ipv6_print (netif->gwv6);
150 if (tun6_addr_get ()) {
151 printf ("\n\tTunnel IP address:\t::"); net_proto_ip_print (tun6_addr_get ());
154 printf ("\n");
158 unsigned netif_read (struct netdev_t *dev, char *buf)
161 return 1;
164 unsigned init_netif ()
166 netif_list.next = &netif_list;
167 netif_list.prev = &netif_list;
169 if (!init_proto_arp ())
170 return 0;
172 if (!init_proto_ndp ())
173 return 0;
175 /* preset client-side port for new connections */
176 tm *t = rtc_getcurrtime ();
178 if (t)
179 net_port_curr = (net_port) (t->__tm_gmtoff & 0xfff + 1024);
180 else
181 net_port_curr = 1024;
183 return 1;