1 /******************************************************************************
2 * Copyright (c) 2013 IBM Corporation
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
16 #include <netlib/ipv6.h>
17 #include <netlib/icmpv6.h>
18 #include <netlib/ndp.h>
21 static struct neighbor *first_neighbor;
22 static struct neighbor *last_neighbor;
25 static struct router *first_router;
26 static struct router *last_router;
29 * NET: add new router to list
30 * @param struct router nghb - new router
31 * @return true or false
34 router_add (struct router *nghb )
39 if (first_router == NULL) {
41 last_router = first_router;
42 last_router->next = NULL;
44 last_router->next = nghb;
46 last_router->next = NULL;
48 return 1; /* no error */
52 * NET: create a new router
53 * @param uint8_t *packet - received packet (Ethernet/IPv6/ICMPv6/ND_NghSlct)
54 * @param struct packeth - pointers to headers in packet
55 * @return pointer to new router
58 router_create (uint8_t *mac, ip6_addr_t *ip)
60 struct router *new_router;
62 new_router = malloc (sizeof(struct router));
66 memset (new_router, 0, sizeof(struct router));
68 /* fill neighbor struct */
69 memcpy (new_router->mac, mac, 6);
70 memcpy (&(new_router->ip.addr[0]), &(ip->addr[0]), IPV6_ADDR_LENGTH);
76 find_router( ip6_addr_t *ip )
78 struct router *n = NULL;
80 for (n = first_router; n != NULL ; n=n->next)
81 if (ip6_cmp (&(n->ip), ip))
82 return n; /* router is already in list*/
84 return NULL; /* router is unknown */
88 * NET: add new neighbor to list
89 * @param struct neighbor nghb - new neighbor
90 * @return true or false
93 neighbor_add (struct neighbor *nghb)
98 if (first_neighbor == NULL) {
99 first_neighbor = nghb;
100 last_neighbor = first_neighbor;
101 last_neighbor->next = NULL;
103 last_neighbor->next = nghb;
104 last_neighbor = nghb;
105 last_neighbor->next = NULL;
108 return 1; /* no error */
112 * NET: create a new neighbor
113 * @param uint8_t *packet - received packet (Ethernet/IPv6/ICMPv6/ND_NghSlct)
114 * @param struct packeth - pointers to headers in packet
115 * @return pointer - pointer to new neighbor
116 * NULL - malloc failed
119 neighbor_create (uint8_t *packet, struct packeth *headers)
121 struct neighbor *new_neighbor;
123 new_neighbor = malloc (sizeof(struct neighbor));
127 /* fill neighbor struct */
128 memcpy (&(new_neighbor->mac),
129 &(headers->ethh->src_mac[0]), 6);
130 memcpy (&(new_neighbor->ip.addr), &(headers->ip6h->src), IPV6_ADDR_LENGTH);
131 new_neighbor->status = NB_INCOMPLETE;
137 * NET: Find neighbor with given IPv6 address in Neighbor Cache
139 * @param ip - Pointer to IPv6 address
140 * @return pointer - pointer to client IPv6 address (e.g. ::1)
141 * NULL - Neighbor not found
144 find_neighbor (ip6_addr_t *ip)
146 struct neighbor *n = NULL;
148 for (n = first_neighbor; n != NULL ; n=n->next) {
149 if (ip6_cmp (&(n->ip), ip)) {
150 return n; /* neighbor is already in cache */
154 return NULL; /* neighbor is unknown */
161 last_router = first_router;
163 /* Init Neighbour cache */
164 first_neighbor = NULL;
165 last_neighbor = first_neighbor;