source: uKadecot/trunk/uip/uip/uip-fw.c@ 101

Last change on this file since 101 was 101, checked in by coas-nagasima, 9 years ago

TOPPERS/uKadecotのソースコードを追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/plain
File size: 14.2 KB
RevLine 
[101]1/*
2 * Copyright (c) 2004, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the uIP TCP/IP stack
30 *
31 * Author: Adam Dunkels <adam@sics.se>
32 *
33 * $Id: uip-fw.c 101 2015-06-02 15:37:23Z coas-nagasima $
34 */
35/**
36 * \addtogroup uip
37 * @{
38 */
39
40/**
41 * \defgroup uipfw uIP packet forwarding
42 * @{
43 *
44 */
45
46/**
47 * \file
48 * uIP packet forwarding.
49 * \author Adam Dunkels <adam@sics.se>
50 *
51 * This file implements a number of simple functions which do packet
52 * forwarding over multiple network interfaces with uIP.
53 *
54 */
55
56#include "uip.h"
57#include "uip_arch.h"
58#include "uip-fw.h"
59
60#include <string.h> /* for memcpy() */
61
62#ifdef __RX
63#pragma pack
64#elif _MSC_VER
65#pragma pack(push, 1)
66#endif
67
68/*
69 * The list of registered network interfaces.
70 */
71static struct uip_fw_netif *netifs = NULL;
72
73/*
74 * A pointer to the default network interface.
75 */
76static struct uip_fw_netif *defaultnetif = NULL;
77
78struct tcpip_hdr {
79 /* IP header. */
80 u8_t vhl,
81 tos;
82 u16_t len,
83 ipid,
84 ipoffset;
85 u8_t ttl,
86 proto;
87 u16_t ipchksum;
88 u16_t srcipaddr[2],
89 destipaddr[2];
90
91 /* TCP header. */
92 u16_t srcport,
93 destport;
94 u8_t seqno[4],
95 ackno[4],
96 tcpoffset,
97 flags,
98 wnd[2];
99 u16_t tcpchksum;
100 u8_t urgp[2];
101 u8_t optdata[4];
102};
103
104struct icmpip_hdr {
105 /* IP header. */
106 u8_t vhl,
107 tos,
108 len[2],
109 ipid[2],
110 ipoffset[2],
111 ttl,
112 proto;
113 u16_t ipchksum;
114 u16_t srcipaddr[2],
115 destipaddr[2];
116 /* ICMP (echo) header. */
117 u8_t type, icode;
118 u16_t icmpchksum;
119 u16_t id, seqno;
120 u8_t payload[1];
121};
122
123/* ICMP ECHO. */
124#define ICMP_ECHO 8
125
126/* ICMP TIME-EXCEEDED. */
127#define ICMP_TE 11
128
129/*
130 * Pointer to the TCP/IP headers of the packet in the uip_buf buffer.
131 */
132#define BUF ((struct tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
133
134/*
135 * Pointer to the ICMP/IP headers of the packet in the uip_buf buffer.
136 */
137#define ICMPBUF ((struct icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
138
139/*
140 * Certain fields of an IP packet that are used for identifying
141 * duplicate packets.
142 */
143struct fwcache_entry {
144 u16_t timer;
145
146 u16_t srcipaddr[2];
147 u16_t destipaddr[2];
148 u16_t ipid;
149 u8_t proto;
150 u8_t unused;
151
152#if notdef
153 u16_t payload[2];
154#endif
155
156#if UIP_REASSEMBLY > 0
157 u16_t len, offset;
158#endif
159};
160
161#ifdef __RX
162#pragma unpack
163#elif _MSC_VER
164#pragma pack(pop)
165#endif
166
167/*
168 * The number of packets to remember when looking for duplicates.
169 */
170#ifdef UIP_CONF_FWCACHE_SIZE
171#define FWCACHE_SIZE UIP_CONF_FWCACHE_SIZE
172#else
173#define FWCACHE_SIZE 2
174#endif
175
176
177/*
178 * A cache of packet header fields which are used for
179 * identifying duplicate packets.
180 */
181static struct fwcache_entry fwcache[FWCACHE_SIZE];
182
183/**
184 * \internal
185 * The time that a packet cache is active.
186 */
187#define FW_TIME 20
188
189/*------------------------------------------------------------------------------*/
190/**
191 * Initialize the uIP packet forwarding module.
192 */
193/*------------------------------------------------------------------------------*/
194void
195uip_fw_init(void)
196{
197 struct uip_fw_netif *t;
198 defaultnetif = NULL;
199 while(netifs != NULL) {
200 t = netifs;
201 netifs = netifs->next;
202 t->next = NULL;
203 }
204}
205/*------------------------------------------------------------------------------*/
206/**
207 * \internal
208 * Check if an IP address is within the network defined by an IP
209 * address and a netmask.
210 *
211 * \param ipaddr The IP address to be checked.
212 * \param netipaddr The IP address of the network.
213 * \param netmask The netmask of the network.
214 *
215 * \return Non-zero if IP address is in network, zero otherwise.
216 */
217/*------------------------------------------------------------------------------*/
218static unsigned char
219ipaddr_maskcmp(u16_t *ipaddr, u16_t *netipaddr, u16_t *netmask)
220{
221 return (ipaddr[0] & netmask [0]) == (netipaddr[0] & netmask[0]) &&
222 (ipaddr[1] & netmask[1]) == (netipaddr[1] & netmask[1]);
223}
224/*------------------------------------------------------------------------------*/
225/**
226 * \internal
227 * Send out an ICMP TIME-EXCEEDED message.
228 *
229 * This function replaces the packet in the uip_buf buffer with the
230 * ICMP packet.
231 */
232/*------------------------------------------------------------------------------*/
233static void
234time_exceeded(void)
235{
236 u16_t tmp16;
237
238 /* We don't send out ICMP errors for ICMP messages. */
239 if(ICMPBUF->proto == UIP_PROTO_ICMP) {
240 uip_len = 0;
241 return;
242 }
243 /* Copy fields from packet header into payload of this ICMP packet. */
244 memcpy(&(ICMPBUF->payload[0]), ICMPBUF, 28);
245
246 /* Set the ICMP type and code. */
247 ICMPBUF->type = ICMP_TE;
248 ICMPBUF->icode = 0;
249
250 /* Calculate the ICMP checksum. */
251 ICMPBUF->icmpchksum = 0;
252 ICMPBUF->icmpchksum = ~uip_chksum((u16_t *)&(ICMPBUF->type), 36);
253
254 /* Set the IP destination address to be the source address of the
255 original packet. */
256 tmp16= BUF->destipaddr[0];
257 BUF->destipaddr[0] = BUF->srcipaddr[0];
258 BUF->srcipaddr[0] = tmp16;
259 tmp16 = BUF->destipaddr[1];
260 BUF->destipaddr[1] = BUF->srcipaddr[1];
261 BUF->srcipaddr[1] = tmp16;
262
263 /* Set our IP address as the source address. */
264 BUF->srcipaddr[0] = uip_hostaddr[0];
265 BUF->srcipaddr[1] = uip_hostaddr[1];
266
267 /* The size of the ICMP time exceeded packet is 36 + the size of the
268 IP header (20) = 56. */
269 uip_len = 56;
270 ICMPBUF->len[0] = 0;
271 ICMPBUF->len[1] = uip_len;
272
273 /* Fill in the other fields in the IP header. */
274 ICMPBUF->vhl = 0x45;
275 ICMPBUF->tos = 0;
276 ICMPBUF->ipoffset[0] = ICMPBUF->ipoffset[1] = 0;
277 ICMPBUF->ttl = UIP_TTL;
278 ICMPBUF->proto = UIP_PROTO_ICMP;
279
280 /* Calculate IP checksum. */
281 ICMPBUF->ipchksum = 0;
282 ICMPBUF->ipchksum = ~(uip_ipchksum());
283
284
285}
286/*------------------------------------------------------------------------------*/
287/**
288 * \internal
289 * Register a packet in the forwarding cache so that it won't be
290 * forwarded again.
291 */
292/*------------------------------------------------------------------------------*/
293static void
294fwcache_register(void)
295{
296 struct fwcache_entry *fw;
297 int i, oldest;
298
299 oldest = FW_TIME;
300 fw = NULL;
301
302 /* Find the oldest entry in the cache. */
303 for(i = 0; i < FWCACHE_SIZE; ++i) {
304 if(fwcache[i].timer == 0) {
305 fw = &fwcache[i];
306 break;
307 } else if(fwcache[i].timer <= oldest) {
308 fw = &fwcache[i];
309 oldest = fwcache[i].timer;
310 }
311 }
312
313 fw->timer = FW_TIME;
314 fw->ipid = BUF->ipid;
315 fw->srcipaddr[0] = BUF->srcipaddr[0];
316 fw->srcipaddr[1] = BUF->srcipaddr[1];
317 fw->destipaddr[0] = BUF->destipaddr[0];
318 fw->destipaddr[1] = BUF->destipaddr[1];
319 fw->proto = BUF->proto;
320#if notdef
321 fw->payload[0] = BUF->srcport;
322 fw->payload[1] = BUF->destport;
323#endif
324#if UIP_REASSEMBLY > 0
325 fw->len = BUF->len;
326 fw->offset = BUF->ipoffset;
327#endif
328}
329/*------------------------------------------------------------------------------*/
330/**
331 * \internal
332 * Find a network interface for the IP packet in uip_buf.
333 */
334/*------------------------------------------------------------------------------*/
335static struct uip_fw_netif *
336find_netif(void)
337{
338 struct uip_fw_netif *netif;
339
340 /* Walk through every network interface to check for a match. */
341 for(netif = netifs; netif != NULL; netif = netif->next) {
342 if(ipaddr_maskcmp(BUF->destipaddr, netif->ipaddr,
343 netif->netmask)) {
344 /* If there was a match, we break the loop. */
345 return netif;
346 }
347 }
348
349 /* If no matching netif was found, we use default netif. */
350 return defaultnetif;
351}
352/*------------------------------------------------------------------------------*/
353/**
354 * Output an IP packet on the correct network interface.
355 *
356 * The IP packet should be present in the uip_buf buffer and its
357 * length in the global uip_len variable.
358 *
359 * \retval UIP_FW_ZEROLEN Indicates that a zero-length packet
360 * transmission was attempted and that no packet was sent.
361 *
362 * \retval UIP_FW_NOROUTE No suitable network interface could be found
363 * for the outbound packet, and the packet was not sent.
364 *
365 * \return The return value from the actual network interface output
366 * function is passed unmodified as a return value.
367 */
368/*------------------------------------------------------------------------------*/
369u8_t
370uip_fw_output(void)
371{
372 struct uip_fw_netif *netif;
373
374 if(uip_len == 0) {
375 return UIP_FW_ZEROLEN;
376 }
377
378 fwcache_register();
379
380#if UIP_BROADCAST
381 /* Link local broadcasts go out on all interfaces. */
382 if(/*BUF->proto == UIP_PROTO_UDP &&*/
383 BUF->destipaddr[0] == 0xffff &&
384 BUF->destipaddr[1] == 0xffff) {
385 if(defaultnetif != NULL) {
386 defaultnetif->output();
387 }
388 for(netif = netifs; netif != NULL; netif = netif->next) {
389 netif->output();
390 }
391 return UIP_FW_OK;
392 }
393#endif /* UIP_BROADCAST */
394
395 netif = find_netif();
396 /* printf("uip_fw_output: netif %p ->output %p len %d\n", netif,
397 netif->output,
398 uip_len);*/
399
400 if(netif == NULL) {
401 return UIP_FW_NOROUTE;
402 }
403 /* If we now have found a suitable network interface, we call its
404 output function to send out the packet. */
405 return netif->output();
406}
407/*------------------------------------------------------------------------------*/
408/**
409 * Forward an IP packet in the uip_buf buffer.
410 *
411 *
412 *
413 * \return UIP_FW_FORWARDED if the packet was forwarded, UIP_FW_LOCAL if
414 * the packet should be processed locally.
415 */
416/*------------------------------------------------------------------------------*/
417u8_t
418uip_fw_forward(void)
419{
420 struct fwcache_entry *fw;
421
422 /* First check if the packet is destined for ourselves and return 0
423 to indicate that the packet should be processed locally. */
424 if(BUF->destipaddr[0] == uip_hostaddr[0] &&
425 BUF->destipaddr[1] == uip_hostaddr[1]) {
426 return UIP_FW_LOCAL;
427 }
428
429 /* If we use ping IP address configuration, and our IP address is
430 not yet configured, we should intercept all ICMP echo packets. */
431#if UIP_PINGADDRCONF
432 if((uip_hostaddr[0] | uip_hostaddr[1]) == 0 &&
433 BUF->proto == UIP_PROTO_ICMP &&
434 ICMPBUF->type == ICMP_ECHO) {
435 return UIP_FW_LOCAL;
436 }
437#endif /* UIP_PINGADDRCONF */
438
439 /* Check if the packet is in the forwarding cache already, and if so
440 we drop it. */
441
442 for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
443 if(fw->timer != 0 &&
444#if UIP_REASSEMBLY > 0
445 fw->len == BUF->len &&
446 fw->offset == BUF->ipoffset &&
447#endif
448 fw->ipid == BUF->ipid &&
449 fw->srcipaddr[0] == BUF->srcipaddr[0] &&
450 fw->srcipaddr[1] == BUF->srcipaddr[1] &&
451 fw->destipaddr[0] == BUF->destipaddr[0] &&
452 fw->destipaddr[1] == BUF->destipaddr[1] &&
453#if notdef
454 fw->payload[0] == BUF->srcport &&
455 fw->payload[1] == BUF->destport &&
456#endif
457 fw->proto == BUF->proto) {
458 /* Drop packet. */
459 return UIP_FW_FORWARDED;
460 }
461 }
462
463 /* If the TTL reaches zero we produce an ICMP time exceeded message
464 in the uip_buf buffer and forward that packet back to the sender
465 of the packet. */
466 if(BUF->ttl <= 1) {
467 /* No time exceeded for broadcasts and multicasts! */
468 if(BUF->destipaddr[0] == 0xffff && BUF->destipaddr[1] == 0xffff) {
469 return UIP_FW_LOCAL;
470 }
471 time_exceeded();
472 }
473
474 /* Decrement the TTL (time-to-live) value in the IP header */
475 BUF->ttl = BUF->ttl - 1;
476
477 /* Update the IP checksum. */
478 if(BUF->ipchksum >= HTONS(0xffff - 0x0100)) {
479 BUF->ipchksum = BUF->ipchksum + HTONS(0x0100) + 1;
480 } else {
481 BUF->ipchksum = BUF->ipchksum + HTONS(0x0100);
482 }
483
484 if(uip_len > 0) {
485 uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN];
486 uip_fw_output();
487 }
488
489#if UIP_BROADCAST
490 if(BUF->destipaddr[0] == 0xffff && BUF->destipaddr[1] == 0xffff) {
491 return UIP_FW_LOCAL;
492 }
493#endif /* UIP_BROADCAST */
494
495 /* Return non-zero to indicate that the packet was forwarded and that no
496 other processing should be made. */
497 return UIP_FW_FORWARDED;
498}
499/*------------------------------------------------------------------------------*/
500/**
501 * Register a network interface with the forwarding module.
502 *
503 * \param netif A pointer to the network interface that is to be
504 * registered.
505 */
506/*------------------------------------------------------------------------------*/
507void
508uip_fw_register(struct uip_fw_netif *netif)
509{
510 netif->next = netifs;
511 netifs = netif;
512}
513/*------------------------------------------------------------------------------*/
514/**
515 * Register a default network interface.
516 *
517 * All packets that don't go out on any of the other interfaces will
518 * be routed to the default interface.
519 *
520 * \param netif A pointer to the network interface that is to be
521 * registered.
522 */
523/*------------------------------------------------------------------------------*/
524void
525uip_fw_default(struct uip_fw_netif *netif)
526{
527 defaultnetif = netif;
528}
529/*------------------------------------------------------------------------------*/
530/**
531 * Perform periodic processing.
532 */
533/*------------------------------------------------------------------------------*/
534void
535uip_fw_periodic(void)
536{
537 struct fwcache_entry *fw;
538 for(fw = fwcache; fw < &fwcache[FWCACHE_SIZE]; ++fw) {
539 if(fw->timer > 0) {
540 --fw->timer;
541 }
542 }
543}
544/*------------------------------------------------------------------------------*/
Note: See TracBrowser for help on using the repository browser.