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 * NET: add new router to list
22 * @param struct router nghb - new router
23 * @return true or false
26 router_add (struct router *nghb )
31 if (first_router == NULL) {
33 last_router = first_router;
34 last_router->next = NULL;
36 last_router->next = nghb;
38 last_router->next = NULL;
40 return 1; /* no error */
44 * NET: create a new router
45 * @param uint8_t *packet - received packet (Ethernet/IPv6/ICMPv6/ND_NghSlct)
46 * @param struct packeth - pointers to headers in packet
47 * @return pointer to new router
50 router_create (uint8_t *mac, ip6_addr_t *ip)
52 struct router *new_router;
54 new_router = malloc (sizeof(struct router));
58 memset (new_router, 0, sizeof(struct router));
60 /* fill neighbor struct */
61 memcpy (new_router->mac, mac, 6);
62 memcpy (&(new_router->ip.addr[0]), &(ip->addr[0]), IPV6_ADDR_LENGTH);
68 find_router( ip6_addr_t *ip )
70 struct router *n = NULL;
72 for (n = first_router; n != NULL ; n=n->next)
73 if (ip6_cmp (&(n->ip), ip))
74 return n; /* router is already in list*/
76 return NULL; /* router is unknown */
80 * NET: add new neighbor to list
81 * @param struct neighbor nghb - new neighbor
82 * @return true or false
85 neighbor_add (struct neighbor *nghb)
90 if (first_neighbor == NULL) {
91 first_neighbor = nghb;
92 last_neighbor = first_neighbor;
93 last_neighbor->next = NULL;
95 last_neighbor->next = nghb;
97 last_neighbor->next = NULL;
100 return 1; /* no error */
104 * NET: create a new neighbor
105 * @param uint8_t *packet - received packet (Ethernet/IPv6/ICMPv6/ND_NghSlct)
106 * @param struct packeth - pointers to headers in packet
107 * @return pointer - pointer to new neighbor
108 * NULL - malloc failed
111 neighbor_create (uint8_t *packet, struct packeth *headers)
113 struct neighbor *new_neighbor;
115 new_neighbor = malloc (sizeof(struct neighbor));
119 /* fill neighbor struct */
120 memcpy (&(new_neighbor->mac),
121 &(headers->ethh->src_mac[0]), 6);
122 memcpy (&(new_neighbor->ip.addr), &(headers->ip6h->src), IPV6_ADDR_LENGTH);
123 new_neighbor->status = NB_INCOMPLETE;
129 * NET: Find neighbor with given IPv6 address in Neighbor Cache
131 * @param ip - Pointer to IPv6 address
132 * @return pointer - pointer to client IPv6 address (e.g. ::1)
133 * NULL - Neighbor not found
136 find_neighbor (ip6_addr_t *ip)
138 struct neighbor *n = NULL;
140 for (n = first_neighbor; n != NULL ; n=n->next) {
141 if (ip6_cmp (&(n->ip), ip)) {
142 return n; /* neighbor is already in cache */
146 return NULL; /* neighbor is unknown */