1 |
606 |
jeremybenn |
/**
|
2 |
|
|
* @file
|
3 |
|
|
*
|
4 |
|
|
* Implementation of raw protocol PCBs for low-level handling of
|
5 |
|
|
* different types of protocols besides (or overriding) those
|
6 |
|
|
* already available in lwIP.
|
7 |
|
|
*
|
8 |
|
|
*/
|
9 |
|
|
/*
|
10 |
|
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
11 |
|
|
* All rights reserved.
|
12 |
|
|
*
|
13 |
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
14 |
|
|
* are permitted provided that the following conditions are met:
|
15 |
|
|
*
|
16 |
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
17 |
|
|
* this list of conditions and the following disclaimer.
|
18 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
19 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
20 |
|
|
* and/or other materials provided with the distribution.
|
21 |
|
|
* 3. The name of the author may not be used to endorse or promote products
|
22 |
|
|
* derived from this software without specific prior written permission.
|
23 |
|
|
*
|
24 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
25 |
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
26 |
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
27 |
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
28 |
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
29 |
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
30 |
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
31 |
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
32 |
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
33 |
|
|
* OF SUCH DAMAGE.
|
34 |
|
|
*
|
35 |
|
|
* This file is part of the lwIP TCP/IP stack.
|
36 |
|
|
*
|
37 |
|
|
* Author: Adam Dunkels <adam@sics.se>
|
38 |
|
|
*
|
39 |
|
|
*/
|
40 |
|
|
|
41 |
|
|
#include <string.h>
|
42 |
|
|
|
43 |
|
|
#include "lwip/opt.h"
|
44 |
|
|
|
45 |
|
|
#include "lwip/def.h"
|
46 |
|
|
#include "lwip/memp.h"
|
47 |
|
|
#include "lwip/inet.h"
|
48 |
|
|
#include "lwip/ip_addr.h"
|
49 |
|
|
#include "lwip/netif.h"
|
50 |
|
|
#include "lwip/raw.h"
|
51 |
|
|
|
52 |
|
|
#include "lwip/stats.h"
|
53 |
|
|
|
54 |
|
|
#include "arch/perf.h"
|
55 |
|
|
#include "lwip/snmp.h"
|
56 |
|
|
|
57 |
|
|
#if LWIP_RAW
|
58 |
|
|
|
59 |
|
|
/** The list of RAW PCBs */
|
60 |
|
|
static struct raw_pcb *raw_pcbs = NULL;
|
61 |
|
|
|
62 |
|
|
void
|
63 |
|
|
raw_init(void)
|
64 |
|
|
{
|
65 |
|
|
raw_pcbs = NULL;
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
/**
|
69 |
|
|
* Determine if in incoming IP packet is covered by a RAW PCB
|
70 |
|
|
* and if so, pass it to a user-provided receive callback function.
|
71 |
|
|
*
|
72 |
|
|
* Given an incoming IP datagram (as a chain of pbufs) this function
|
73 |
|
|
* finds a corresponding RAW PCB and calls the corresponding receive
|
74 |
|
|
* callback function.
|
75 |
|
|
*
|
76 |
|
|
* @param pbuf pbuf to be demultiplexed to a RAW PCB.
|
77 |
|
|
* @param netif network interface on which the datagram was received.
|
78 |
|
|
* @Return - 1 if the packet has been eaten by a RAW PCB receive
|
79 |
|
|
* callback function. The caller MAY NOT not reference the
|
80 |
|
|
* packet any longer, and MAY NOT call pbuf_free().
|
81 |
|
|
* @return - 0 if packet is not eaten (pbuf is still referenced by the
|
82 |
|
|
* caller).
|
83 |
|
|
*
|
84 |
|
|
*/
|
85 |
|
|
u8_t
|
86 |
|
|
raw_input(struct pbuf *p, struct netif *inp)
|
87 |
|
|
{
|
88 |
|
|
struct raw_pcb *pcb;
|
89 |
|
|
struct ip_hdr *iphdr;
|
90 |
|
|
s16_t proto;
|
91 |
|
|
u8_t eaten = 0;
|
92 |
|
|
|
93 |
|
|
iphdr = p->payload;
|
94 |
|
|
proto = IPH_PROTO(iphdr);
|
95 |
|
|
|
96 |
|
|
pcb = raw_pcbs;
|
97 |
|
|
/* loop through all raw pcbs until the packet is eaten by one */
|
98 |
|
|
/* this allows multiple pcbs to match against the packet by design */
|
99 |
|
|
while ((eaten == 0) && (pcb != NULL)) {
|
100 |
|
|
if (pcb->protocol == proto) {
|
101 |
|
|
/* receive callback function available? */
|
102 |
|
|
if (pcb->recv != NULL) {
|
103 |
|
|
/* the receive callback function did not eat the packet? */
|
104 |
|
|
if (pcb->recv(pcb->recv_arg, pcb, p, &(iphdr->src)) != 0)
|
105 |
|
|
{
|
106 |
|
|
/* receive function ate the packet */
|
107 |
|
|
p = NULL;
|
108 |
|
|
eaten = 1;
|
109 |
|
|
}
|
110 |
|
|
}
|
111 |
|
|
/* no receive callback function was set for this raw PCB */
|
112 |
|
|
/* drop the packet */
|
113 |
|
|
}
|
114 |
|
|
pcb = pcb->next;
|
115 |
|
|
}
|
116 |
|
|
return eaten;
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
/**
|
120 |
|
|
* Bind a RAW PCB.
|
121 |
|
|
*
|
122 |
|
|
* @param pcb RAW PCB to be bound with a local address ipaddr.
|
123 |
|
|
* @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
|
124 |
|
|
* bind to all local interfaces.
|
125 |
|
|
*
|
126 |
|
|
* @return lwIP error code.
|
127 |
|
|
* - ERR_OK. Successful. No error occured.
|
128 |
|
|
* - ERR_USE. The specified IP address is already bound to by
|
129 |
|
|
* another RAW PCB.
|
130 |
|
|
*
|
131 |
|
|
* @see raw_disconnect()
|
132 |
|
|
*/
|
133 |
|
|
err_t
|
134 |
|
|
raw_bind(struct raw_pcb *pcb, struct ip_addr *ipaddr)
|
135 |
|
|
{
|
136 |
|
|
ip_addr_set(&pcb->local_ip, ipaddr);
|
137 |
|
|
return ERR_OK;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
/**
|
141 |
|
|
* Connect an RAW PCB. This function is required by upper layers
|
142 |
|
|
* of lwip. Using the raw api you could use raw_sendto() instead
|
143 |
|
|
*
|
144 |
|
|
* This will associate the RAW PCB with the remote address.
|
145 |
|
|
*
|
146 |
|
|
* @param pcb RAW PCB to be connected with remote address ipaddr and port.
|
147 |
|
|
* @param ipaddr remote IP address to connect with.
|
148 |
|
|
*
|
149 |
|
|
* @return lwIP error code
|
150 |
|
|
*
|
151 |
|
|
* @see raw_disconnect() and raw_sendto()
|
152 |
|
|
*/
|
153 |
|
|
err_t
|
154 |
|
|
raw_connect(struct raw_pcb *pcb, struct ip_addr *ipaddr)
|
155 |
|
|
{
|
156 |
|
|
ip_addr_set(&pcb->remote_ip, ipaddr);
|
157 |
|
|
return ERR_OK;
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
|
161 |
|
|
/**
|
162 |
|
|
* Set the callback function for received packets that match the
|
163 |
|
|
* raw PCB's protocol and binding.
|
164 |
|
|
*
|
165 |
|
|
* The callback function MUST either
|
166 |
|
|
* - eat the packet by calling pbuf_free() and returning non-zero. The
|
167 |
|
|
* packet will not be passed to other raw PCBs or other protocol layers.
|
168 |
|
|
* - not free the packet, and return zero. The packet will be matched
|
169 |
|
|
* against further PCBs and/or forwarded to another protocol layers.
|
170 |
|
|
*
|
171 |
|
|
* @return non-zero if the packet was free()d, zero if the packet remains
|
172 |
|
|
* available for others.
|
173 |
|
|
*/
|
174 |
|
|
void
|
175 |
|
|
raw_recv(struct raw_pcb *pcb,
|
176 |
|
|
u8_t (* recv)(void *arg, struct raw_pcb *upcb, struct pbuf *p,
|
177 |
|
|
struct ip_addr *addr),
|
178 |
|
|
void *recv_arg)
|
179 |
|
|
{
|
180 |
|
|
/* remember recv() callback and user data */
|
181 |
|
|
pcb->recv = recv;
|
182 |
|
|
pcb->recv_arg = recv_arg;
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
/**
|
186 |
|
|
* Send the raw IP packet to the given address. Note that actually you cannot
|
187 |
|
|
* modify the IP headers (this is inconsistent with the receive callback where
|
188 |
|
|
* you actually get the IP headers), you can only specify the IP payload here.
|
189 |
|
|
* It requires some more changes in lwIP. (there will be a raw_send() function
|
190 |
|
|
* then.)
|
191 |
|
|
*
|
192 |
|
|
* @param pcb the raw pcb which to send
|
193 |
|
|
* @param p the IP payload to send
|
194 |
|
|
* @param ipaddr the destination address of the IP packet
|
195 |
|
|
*
|
196 |
|
|
*/
|
197 |
|
|
err_t
|
198 |
|
|
raw_sendto(struct raw_pcb *pcb, struct pbuf *p, struct ip_addr *ipaddr)
|
199 |
|
|
{
|
200 |
|
|
err_t err;
|
201 |
|
|
struct netif *netif;
|
202 |
|
|
struct ip_addr *src_ip;
|
203 |
|
|
struct pbuf *q; /* q will be sent down the stack */
|
204 |
|
|
|
205 |
|
|
LWIP_DEBUGF(RAW_DEBUG | DBG_TRACE | 3, ("raw_sendto\n"));
|
206 |
|
|
|
207 |
|
|
/* not enough space to add an IP header to first pbuf in given p chain? */
|
208 |
|
|
if (pbuf_header(p, IP_HLEN)) {
|
209 |
|
|
/* allocate header in new pbuf */
|
210 |
|
|
q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
|
211 |
|
|
/* new header pbuf could not be allocated? */
|
212 |
|
|
if (q == NULL) {
|
213 |
|
|
LWIP_DEBUGF(RAW_DEBUG | DBG_TRACE | 2, ("raw_sendto: could not allocate header\n"));
|
214 |
|
|
return ERR_MEM;
|
215 |
|
|
}
|
216 |
|
|
/* chain header q in front of given pbuf p */
|
217 |
|
|
pbuf_chain(q, p);
|
218 |
|
|
/* { first pbuf q points to header pbuf } */
|
219 |
|
|
LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
|
220 |
|
|
} else {
|
221 |
|
|
/* first pbuf q equals given pbuf */
|
222 |
|
|
q = p;
|
223 |
|
|
pbuf_header(q, -IP_HLEN);
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
if ((netif = ip_route(ipaddr)) == NULL) {
|
227 |
|
|
LWIP_DEBUGF(RAW_DEBUG | 1, ("raw_sendto: No route to 0x%"X32_F"\n", ipaddr->addr));
|
228 |
|
|
#if RAW_STATS
|
229 |
|
|
/* ++lwip_stats.raw.rterr;*/
|
230 |
|
|
#endif /* RAW_STATS */
|
231 |
|
|
/* free any temporary header pbuf allocated by pbuf_header() */
|
232 |
|
|
if (q != p) {
|
233 |
|
|
pbuf_free(q);
|
234 |
|
|
}
|
235 |
|
|
return ERR_RTE;
|
236 |
|
|
}
|
237 |
|
|
|
238 |
|
|
if (ip_addr_isany(&pcb->local_ip)) {
|
239 |
|
|
/* use outgoing network interface IP address as source address */
|
240 |
|
|
src_ip = &(netif->ip_addr);
|
241 |
|
|
} else {
|
242 |
|
|
/* use RAW PCB local IP address as source address */
|
243 |
|
|
src_ip = &(pcb->local_ip);
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
err = ip_output_if (q, src_ip, ipaddr, pcb->ttl, pcb->tos, pcb->protocol, netif);
|
247 |
|
|
|
248 |
|
|
/* did we chain a header earlier? */
|
249 |
|
|
if (q != p) {
|
250 |
|
|
/* free the header */
|
251 |
|
|
pbuf_free(q);
|
252 |
|
|
}
|
253 |
|
|
return err;
|
254 |
|
|
}
|
255 |
|
|
|
256 |
|
|
/**
|
257 |
|
|
* Send the raw IP packet to the address given by raw_connect()
|
258 |
|
|
*
|
259 |
|
|
* @param pcb the raw pcb which to send
|
260 |
|
|
* @param p the IP payload to send
|
261 |
|
|
* @param ipaddr the destination address of the IP packet
|
262 |
|
|
*
|
263 |
|
|
*/
|
264 |
|
|
err_t
|
265 |
|
|
raw_send(struct raw_pcb *pcb, struct pbuf *p)
|
266 |
|
|
{
|
267 |
|
|
return raw_sendto(pcb, p, &pcb->remote_ip);
|
268 |
|
|
}
|
269 |
|
|
|
270 |
|
|
/**
|
271 |
|
|
* Remove an RAW PCB.
|
272 |
|
|
*
|
273 |
|
|
* @param pcb RAW PCB to be removed. The PCB is removed from the list of
|
274 |
|
|
* RAW PCB's and the data structure is freed from memory.
|
275 |
|
|
*
|
276 |
|
|
* @see raw_new()
|
277 |
|
|
*/
|
278 |
|
|
void
|
279 |
|
|
raw_remove(struct raw_pcb *pcb)
|
280 |
|
|
{
|
281 |
|
|
struct raw_pcb *pcb2;
|
282 |
|
|
/* pcb to be removed is first in list? */
|
283 |
|
|
if (raw_pcbs == pcb) {
|
284 |
|
|
/* make list start at 2nd pcb */
|
285 |
|
|
raw_pcbs = raw_pcbs->next;
|
286 |
|
|
/* pcb not 1st in list */
|
287 |
|
|
} else for(pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
|
288 |
|
|
/* find pcb in raw_pcbs list */
|
289 |
|
|
if (pcb2->next != NULL && pcb2->next == pcb) {
|
290 |
|
|
/* remove pcb from list */
|
291 |
|
|
pcb2->next = pcb->next;
|
292 |
|
|
}
|
293 |
|
|
}
|
294 |
|
|
memp_free(MEMP_RAW_PCB, pcb);
|
295 |
|
|
}
|
296 |
|
|
|
297 |
|
|
/**
|
298 |
|
|
* Create a RAW PCB.
|
299 |
|
|
*
|
300 |
|
|
* @return The RAW PCB which was created. NULL if the PCB data structure
|
301 |
|
|
* could not be allocated.
|
302 |
|
|
*
|
303 |
|
|
* @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
|
304 |
|
|
*
|
305 |
|
|
* @see raw_remove()
|
306 |
|
|
*/
|
307 |
|
|
struct raw_pcb *
|
308 |
|
|
raw_new(u16_t proto) {
|
309 |
|
|
struct raw_pcb *pcb;
|
310 |
|
|
|
311 |
|
|
LWIP_DEBUGF(RAW_DEBUG | DBG_TRACE | 3, ("raw_new\n"));
|
312 |
|
|
|
313 |
|
|
pcb = memp_malloc(MEMP_RAW_PCB);
|
314 |
|
|
/* could allocate RAW PCB? */
|
315 |
|
|
if (pcb != NULL) {
|
316 |
|
|
/* initialize PCB to all zeroes */
|
317 |
|
|
memset(pcb, 0, sizeof(struct raw_pcb));
|
318 |
|
|
pcb->protocol = proto;
|
319 |
|
|
pcb->ttl = RAW_TTL;
|
320 |
|
|
pcb->next = raw_pcbs;
|
321 |
|
|
raw_pcbs = pcb;
|
322 |
|
|
}
|
323 |
|
|
return pcb;
|
324 |
|
|
}
|
325 |
|
|
|
326 |
|
|
#endif /* LWIP_RAW */
|