Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / SLOF / clients / net-snk / app / netlib / ndp.c
1 /******************************************************************************
2  * Copyright (c) 2013 IBM Corporation
3  * All rights reserved.
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
8  *
9  * Contributors:
10  *     IBM Corporation - initial implementation
11  *****************************************************************************/
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <netlib/ipv6.h>
17 #include <netlib/icmpv6.h>
18 #include <netlib/ndp.h>
19
20 /*
21  * NET: add new router to list
22  * @param  struct router nghb  - new router
23  * @return true or false
24  */
25 int8_t
26 router_add (struct router *nghb )
27 {
28         if (nghb == NULL)
29                 return -1;
30
31         if (first_router == NULL) {
32                 first_router= nghb;
33                 last_router = first_router;
34                 last_router->next = NULL;
35         } else {
36                 last_router->next = nghb;
37                 last_router = nghb;
38                 last_router->next = NULL;
39         }
40         return 1; /* no error */
41 }
42
43 /*
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
48  */
49 void *
50 router_create (uint8_t *mac, ip6_addr_t *ip)
51 {
52         struct router *new_router;
53
54         new_router = malloc (sizeof(struct router));
55         if( !new_router) {
56                 return 0;
57         }
58         memset (new_router, 0, sizeof(struct router));
59
60         /* fill neighbor struct */
61         memcpy (new_router->mac, mac, 6);
62         memcpy (&(new_router->ip.addr[0]), &(ip->addr[0]), IPV6_ADDR_LENGTH);
63
64         return new_router;
65 }
66
67 struct router *
68 find_router( ip6_addr_t *ip )
69 {
70         struct router *n = NULL;
71
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*/
75
76         return NULL; /* router is unknown */
77 }
78
79 /*
80  * NET: add new neighbor to list
81  * @param  struct neighbor nghb  - new neighbor
82  * @return true or false
83  */
84 int8_t
85 neighbor_add (struct neighbor *nghb)
86 {
87         if (nghb == NULL)
88                 return -1;
89
90         if (first_neighbor == NULL) {
91                 first_neighbor = nghb;
92                 last_neighbor = first_neighbor;
93                 last_neighbor->next = NULL;
94         } else {
95                 last_neighbor->next = nghb;
96                 last_neighbor = nghb;
97                 last_neighbor->next = NULL;
98         }
99
100         return 1; /* no error */
101 }
102
103 /*
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
109  */
110 void *
111 neighbor_create (uint8_t *packet, struct packeth *headers)
112 {
113         struct neighbor *new_neighbor;
114
115         new_neighbor = malloc (sizeof(struct neighbor));
116         if( !new_neighbor )
117                 return NULL;
118
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;
124
125         return new_neighbor;
126 }
127
128 /**
129  * NET: Find neighbor with given IPv6 address in Neighbor Cache
130  *
131  * @param  ip - Pointer to IPv6 address
132  * @return pointer - pointer to client IPv6 address (e.g. ::1)
133  *         NULL    - Neighbor not found
134  */
135 struct neighbor *
136 find_neighbor (ip6_addr_t *ip)
137 {
138         struct neighbor *n = NULL;
139
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 */
143                 }
144         }
145
146         return NULL; /* neighbor is unknown */
147 }