Semi-decennial update. 50% code inflation.
[cbaos.git] / net / ip.c
blobac32c7cc30ca8511423b98441b12cdea014d49f9
1 /* Author: Domen Puncer Kugler <domen@cba.si>. License: WTFPL, see file LICENSE */
3 #include <net/ethernet.h>
4 #include <net/ip.h>
5 #include <helpers.h>
8 //#define ip_print_array print_array
9 #define ip_print_array(tag, buf, size)
12 static LIST_DECLARE_INIT(ip_handler_list);
15 static int ip_handle(u8 *buf, unsigned len)
17 struct ip_packet *ip = (struct ip_packet*)buf;
19 ip_print_array("IP RX", buf, sizeof(*ip));
20 if (len <= sizeof(*ip))
21 return 0;
23 len -= sizeof(*ip);
25 if (ip->vihl != 0x45) /* IPv4, 20B header */
26 goto ack;
28 struct list *it;
29 list_for_each(&ip_handler_list, it) {
30 struct ip_handler *han = list_entry(it, struct ip_handler, list);
32 if (ip->protocol == han->protocol)
33 return han->handler(ip->payload, len);
35 return -1;
37 ack:
38 return 0;
41 static struct eth_handler eth_handler_ip = {
42 .list = LIST_INIT(eth_handler_ip.list),
43 .ethertype = ETHERTYPE_IP,
44 .handler = ip_handle,
47 int ip_init(void)
49 return eth_register_handler(&eth_handler_ip);
52 int ip_register_handler(struct ip_handler *han)
54 list_add_tail(&ip_handler_list, &han->list);
55 return 0;