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