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/>.
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
)
42 for (netif
= netif_list
.next
; netif
!= &netif_list
; netif
= netif
->next
) {
43 if (!strcmp (netif
->dev
->name
, name
))
50 unsigned netif_ip_addr (netif_t
*netif
, net_ipv4 ip
, unsigned char cfg
)
61 unsigned netif_ipv6_addr (netif_t
*netif
, net_ipv6 ip
, unsigned char cfg
)
66 memcpy (netif
->ipv6
, ip
, sizeof (net_ipv6
));
72 unsigned netif_gw_addr (netif_t
*netif
, net_ipv4 gw
)
82 unsigned netif_gwv6_addr (netif_t
*netif
, net_ipv6 gw
)
87 memcpy (netif
->gwv6
, gw
, sizeof (net_ipv6
));
92 netif_t
*netif_create (struct netdev_t
*dev
)
99 /* alloc and init context */
100 netif
= (netif_t
*) kmalloc (sizeof (netif_t
));
110 netif_ip_addr (netif
, DEFAULT_IP_ADDRESS
, IF_CFG_TYPE_STATIC
);
111 netif_gw_addr (netif
, DEFAULT_GW_ADDRESS
);
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
);
121 netif
->next
= &netif_list
;
122 netif
->prev
= netif_list
.prev
;
123 netif
->prev
->next
= netif
;
124 netif
->next
->prev
= netif
;
129 net_port
netif_port_get ()
131 return net_port_curr
++;
134 void iflist_display ()
137 for (netif
= netif_list
.next
; netif
!= &netif_list
; netif
= netif
->next
) {
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 ());
158 unsigned netif_read (struct netdev_t
*dev
, char *buf
)
164 unsigned init_netif ()
166 netif_list
.next
= &netif_list
;
167 netif_list
.prev
= &netif_list
;
169 if (!init_proto_arp ())
172 if (!init_proto_ndp ())
175 /* preset client-side port for new connections */
176 tm
*t
= rtc_getcurrtime ();
179 net_port_curr
= (net_port
) (t
->__tm_gmtoff
& 0xfff + 1024);
181 net_port_curr
= 1024;