source: EcnlProtoTool/trunk/asp3_dcre/tinet/netinet/if_ether.c@ 321

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

文字コードを設定

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 15.2 KB
Line 
1/*
2 * TINET (TCP/IP Protocol Stack)
3 *
4 * Copyright (C) 2001-2009 by Dep. of Computer Science and Engineering
5 * Tomakomai National College of Technology, JAPAN
6 *
7 * 上記著作権者は,以下の (1)~(4) の条件か,Free Software Foundation
8 * によって公表されている GNU General Public License の Version 2 に記
9 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
10 * を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
11 * 利用と呼ぶ)することを無償で許諾する.
12 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
13 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
14 * スコード中に含まれていること.
15 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
16 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
17 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
18 * の無保証規定を掲載すること.
19 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
20 * 用できない形で再配布する場合には,次の条件を満たすこと.
21 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
22 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
23 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
24 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
25 *
26 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
27 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
28 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
29 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
30 *
31 * @(#) $Id$
32 */
33
34/*
35 * Copyright (c) 1982, 1986, 1988, 1993
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
67 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.52.2.6 1999/10/18 11:59:31 sheldonh Exp $
68 */
69
70#include <string.h>
71
72#ifdef TARGET_KERNEL_ASP
73
74#include <kernel.h>
75#include <sil.h>
76#include <t_syslog.h>
77#include "kernel_cfg.h"
78
79#endif /* of #ifdef TARGET_KERNEL_ASP */
80
81#ifdef TARGET_KERNEL_JSP
82
83#include <s_services.h>
84#include <t_services.h>
85#include "kernel_id.h"
86
87#endif /* of #ifdef TARGET_KERNEL_JSP */
88
89#include <tinet_defs.h>
90#include <tinet_config.h>
91
92#include <net/if.h>
93#include <net/ethernet.h>
94#include <net/if_arp.h>
95#include <net/net.h>
96#include <net/net_buf.h>
97#include <net/net_var.h>
98#include <net/net_timer.h>
99#include <net/net_count.h>
100
101#include <netinet/in.h>
102#include <netinet/in_var.h>
103#include <netinet/ip.h>
104#include <netinet/if_ether.h>
105
106#include <net/if_var.h>
107
108#if defined(SUPPORT_INET4) && defined(SUPPORT_ETHER)
109
110/*
111 * ARP キャッシュエントリ
112 */
113
114static T_ARP_ENTRY arp_cache[NUM_ARP_ENTRY];
115
116/*
117 * 関数
118 */
119
120static T_ARP_ENTRY *arp_lookup (T_IN4_ADDR addr, bool_t create);
121static void in_arpinput (T_IF_ADDR *ifaddr, T_NET_BUF *input);
122static void arp_timer (void *ignore);
123
124/*
125 * arp_lookup -- ARP キャッシュの探索と登録
126 *
127 * 注意: SEM_ARP_CACHE_LOCK を獲得した状態で呼出すこと
128 */
129
130static T_ARP_ENTRY *
131arp_lookup (T_IN4_ADDR addr, bool_t create)
132{
133 int_t ix, six;
134 uint16_t min;
135
136 for (ix = NUM_ARP_ENTRY; ix -- > 0; ) {
137 if (arp_cache[ix].expire && arp_cache[ix].ip_addr == addr)
138 return &arp_cache[ix];
139 }
140
141 /* create が真なら、新たなエントリを登録する。*/
142 if (create) {
143
144 /* まず、空きがあれば、その空きを利用する。*/
145 for (ix = NUM_ARP_ENTRY; ix -- > 0; ) {
146 if (arp_cache[ix].expire == 0) {
147 arp_cache[ix].ip_addr = addr;
148 return &arp_cache[ix];
149 }
150 }
151
152 /*
153 * 空きがなければ、タイムアウトまで時間が最短の
154 * エントリーを破棄して利用する。
155 */
156 syslog(LOG_NOTICE, "[ARP] cache busy, size=%d", NUM_ARP_ENTRY);
157 min = 0xffff;
158 for (six = ix = NUM_ARP_ENTRY; ix -- > 0; )
159 if (arp_cache[ix].expire < min) {
160 six = ix;
161 min = arp_cache[ix].expire;
162 }
163 arp_cache[six].expire = 0;
164 arp_cache[six].ip_addr = addr;
165 return &arp_cache[six];
166 }
167 else
168 return NULL;
169 }
170
171/*
172 * in_arpinput -- TCP/IP 用 ARP の入力関数
173 */
174
175static void
176in_arpinput (T_IF_ADDR *ifaddr, T_NET_BUF *input)
177{
178 T_NET_BUF *pending;
179 T_ETHER_HDR *eth;
180 T_ARP_HDR *arph;
181 T_ETHER_ARP_HDR *et_arph;
182 T_ARP_ENTRY *ent;
183 T_IFNET *ifp = IF_GET_IFNET();
184 T_IN4_ADDR saddr, taddr;
185
186 et_arph = GET_ETHER_ARP_HDR(input);
187
188 ntoahl(saddr, et_arph->sproto); /* 送信元 IP アドレス */
189 ntoahl(taddr, et_arph->tproto); /* 解決対象 IP アドレス */
190
191 /*
192 * 以下の場合はエラー
193 * ・送信ホストの物理アドレスが自分
194 * ・送信ホストの物理アドレスがブロードキャスト
195 */
196 if (memcmp(et_arph->shost, ifaddr->lladdr, ETHER_ADDR_LEN) == 0 ||
197 memcmp(et_arph->shost, ether_broad_cast_addr, ETHER_ADDR_LEN) == 0)
198 goto err_ret;
199
200 /*
201 * 送信ホストの IP アドレスが自分の場合は、重複しているので
202 * 相手にも知らせる。
203 */
204 if (saddr == ifp->in_ifaddr.addr) {
205
206#ifdef ARP_CFG_CALLBACK_DUPLICATED
207
208 if (arp_callback_duplicated(et_arph->shost)) {
209 syslog(LOG_ERROR, "[ARP] IP address duplicated: %s", mac2str(NULL, et_arph->shost));
210 taddr = saddr;
211 goto reply;
212 }
213 else
214 goto buf_rel;
215
216#else /* of #ifdef ARP_CFG_CALLBACK_DUPLICATED */
217
218 syslog(LOG_ERROR, "[ARP] IP address duplicated: %s", mac2str(NULL, et_arph->shost));
219 taddr = saddr;
220 goto reply;
221
222#endif /* of #ifdef ARP_CFG_CALLBACK_DUPLICATED */
223
224 }
225
226 /*
227 * 以下の場合は何もしない。
228 * ・解決対象 IP アドレスが自分ではない、注: 元の FreeBSD の
229 * 実装では、ARP PROXY 等のため、自分以外の IP アドレスの
230 * 解決も行っているが、本実装では、自分以外の IP
231 * アドレスの解決は行わない。
232 */
233 if (taddr != ifp->in_ifaddr.addr)
234 goto buf_rel;
235
236 /*
237 * 送信元 IP アドレスが ARP キャッシュにあるか調べる。
238 * キャッシュになければ、新たにエントリを登録する。
239 */
240 syscall(wai_sem(SEM_ARP_CACHE_LOCK));
241 ent = arp_lookup(saddr, true);
242
243 memcpy(ent->mac_addr, et_arph->shost, ETHER_ADDR_LEN);
244 ent->expire = ARP_CACHE_KEEP;
245
246 /*
247 * 送信がペンデングされているフレームがあれば送信する。
248 */
249 if (ent->hold) {
250
251 /* フレームの Ethernet ヘッダを設定する。*/
252 memcpy(GET_ETHER_HDR(ent->hold)->dhost, ent->mac_addr, ETHER_ADDR_LEN);
253
254 pending = ent->hold;
255 ent->hold = NULL;
256 syscall(sig_sem(SEM_ARP_CACHE_LOCK));
257
258 /* ペンディングされているフレームを送信する。*/
259 IF_RAW_OUTPUT(pending, TMO_FEVR);
260
261 }
262 else
263 syscall(sig_sem(SEM_ARP_CACHE_LOCK));
264
265reply:
266
267 arph = GET_ARP_HDR(input);
268
269 /* アドレス解決要求でなければ終了 */
270 if (ntohs(arph->opcode) != ARPOP_REQUEST)
271 goto buf_rel;
272
273 /* Ethernet ARP ヘッダを設定する。*/
274 memcpy(et_arph->thost, et_arph->shost, ETHER_ADDR_LEN);
275 memcpy(et_arph->shost, ifaddr->lladdr, ETHER_ADDR_LEN);
276 memcpy(et_arph->tproto, (uint8_t*)&et_arph->sproto, sizeof(T_IN_ADDR));
277 ahtonl(et_arph->sproto, taddr);
278
279 /* Ethernet ARP ヘッダを設定する。*/
280 arph->opcode = htons(ARPOP_REPLY);
281
282 /* Ethernet ヘッダを設定する。*/
283 eth = GET_ETHER_HDR(input);
284 memcpy(eth->dhost, eth->shost, ETHER_ADDR_LEN);
285 memcpy(eth->shost, ifaddr->lladdr, ETHER_ADDR_LEN);
286
287 /* ARP 応答を送信する。*/
288 IF_RAW_OUTPUT(input, TMO_FEVR);
289 return;
290
291err_ret:
292 NET_COUNT_ARP(net_count_arp.in_err_packets, 1);
293buf_rel:
294 syscall(rel_net_buf(input));
295 }
296
297/*
298 * arp_timer -- ARP キャッシュの更新
299 */
300
301static void
302arp_timer (void *ignore)
303{
304 int_t ix;
305
306 syscall(wai_sem(SEM_ARP_CACHE_LOCK));
307
308 for (ix = NUM_ARP_ENTRY; ix -- > 0; ) {
309 if (arp_cache[ix].expire) {
310 arp_cache[ix].expire -= ARP_TIMER_TMO;
311 if (arp_cache[ix].expire == 0) {
312 /* 送信がペンデングされているフレームがあれば捨てる。*/
313 if (arp_cache[ix].hold) {
314 NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_ERR_PACKETS], 1);
315 syscall(rel_net_buf(arp_cache[ix].hold));
316 }
317 memset(&arp_cache[ix], 0, sizeof(T_ARP_ENTRY));
318 }
319 }
320 }
321
322 syscall(sig_sem(SEM_ARP_CACHE_LOCK));
323 timeout(arp_timer, NULL, ARP_TIMER_TMO);
324 }
325
326/*
327 * arp_request -- MAC アドレス解決要求
328 */
329
330ER
331arp_request (T_IF_ADDR *ifaddr, T_IN4_ADDR dst)
332{
333 ER error;
334 T_IN4_ADDR src;
335 T_ETHER_HDR *eth;
336 T_ARP_HDR *arph;
337 T_ETHER_ARP_HDR *et_arph;
338 T_NET_BUF *arp_req;
339
340 NET_COUNT_ARP(net_count_arp.out_octets , IF_ARP_ETHER_HDR_SIZE - IF_HDR_SIZE);
341 NET_COUNT_ARP(net_count_arp.out_packets, 1);
342
343 if ((error = tget_net_buf(&arp_req, IF_ARP_ETHER_HDR_SIZE, TMO_ARP_GET_NET_BUF)) == E_OK) {
344
345 /* イーサネットヘッダを設定する。*/
346 eth = GET_ETHER_HDR(arp_req);
347 memcpy(eth->dhost, ether_broad_cast_addr, ETHER_ADDR_LEN);
348 memcpy(eth->shost, ifaddr->lladdr, ETHER_ADDR_LEN);
349 eth->type = htons(ETHER_TYPE_ARP);
350
351 /* ARP ヘッダを設定する。*/
352 arph = GET_ARP_HDR(arp_req);
353 arph->hrd_addr = htons(ARPHRD_ETHER);
354 arph->proto = htons(ETHER_TYPE_IP);
355 arph->hdr_len = sizeof(et_arph->shost);
356 arph->proto_len = sizeof(et_arph->sproto);
357 arph->opcode = htons(ARPOP_REQUEST);
358
359 /* イーサネット ARP ヘッダを設定する。*/
360 et_arph = GET_ETHER_ARP_HDR(arp_req);
361 src = IF_GET_IFNET()->in_ifaddr.addr;
362 memcpy(et_arph->shost, ifaddr->lladdr, ETHER_ADDR_LEN);
363 memset(et_arph->thost, 0, ETHER_ADDR_LEN);
364 ahtonl(et_arph->sproto, src);
365 ahtonl(et_arph->tproto, dst);
366
367 /* 送信する。*/
368 error = IF_RAW_OUTPUT(arp_req, TMO_ARP_OUTPUT);
369 }
370 if (error != E_OK)
371 NET_COUNT_ARP(net_count_arp.out_err_packets, 1);
372 return error;
373 }
374
375/*
376 * arp_get_cache -- ARP キャッシュを獲得する。
377 */
378
379const T_ARP_ENTRY *
380arp_get_cache (void)
381{
382 return arp_cache;
383 }
384
385/*
386 * arp_input -- ARP の入力関数
387 */
388
389void
390arp_input (T_IF_ADDR *ifaddr, T_NET_BUF *input)
391{
392 T_ARP_HDR *arph;
393
394 NET_COUNT_ARP(net_count_arp.in_octets , input->len - IF_HDR_SIZE);
395 NET_COUNT_ARP(net_count_arp.in_packets, 1);
396
397 /* ARP ヘッダの長さをチェックする。*/
398 if (input->len < IF_ARP_HDR_SIZE)
399 goto buf_rel;
400
401 arph = GET_ARP_HDR(input);
402
403 /*
404 * 物理アドレスフォーマットが Ehternet 以外
405 * または、プロトコルが IP 以外はエラー。
406 */
407 if (ntohs(arph->hrd_addr) != ARPHRD_ETHER ||
408 ntohs(arph->proto) != ETHER_TYPE_IP)
409 goto buf_rel;
410
411 /* ARP ヘッダ + Ether ARP ヘッダの長さをチェックする。*/
412 if (input->len < IF_ARP_ETHER_HDR_SIZE)
413 goto buf_rel;
414
415 in_arpinput(ifaddr, input);
416 return;
417
418buf_rel:
419 NET_COUNT_ARP(net_count_arp.in_err_packets, 1);
420 syscall(rel_net_buf(input));
421 }
422
423/*
424 * arp_resolve -- MAC アドレス解決
425 *
426 * アドレス解決ができれば、戻り値は true を返し、できなければ
427 * 送信をペンディングして、戻り値は false を返す。
428 */
429
430bool_t
431arp_resolve (T_IF_ADDR *ifaddr, T_NET_BUF *output, T_IN4_ADDR gw)
432{
433 T_ARP_ENTRY *ent;
434 T_ETHER_HDR *eth;
435 T_IFNET *ifp = IF_GET_IFNET();
436
437 eth = GET_ETHER_HDR(output);
438
439 /*
440 * 次の場合は、イーサネットのブロードキャストアドレスを返す。
441 *
442 * ・全ビットが 1
443 * ・ホスト部の全ビットが 1 で、ネットワーク部がローカルアドレス
444 */
445 if (gw == IPV4_ADDR_BROADCAST ||
446 gw == ((ifp->in_ifaddr.addr & ifp->in_ifaddr.mask) | ~ifp->in_ifaddr.mask)) {
447 memcpy(eth->dhost, ether_broad_cast_addr, ETHER_ADDR_LEN);
448 return true;
449 }
450
451 /* 送信先 GW の IP アドレスが ARP キャッシュにあるか調べる。*/
452 syscall(wai_sem(SEM_ARP_CACHE_LOCK));
453 ent = arp_lookup(gw, true);
454 if (ent->expire) {
455 memcpy(eth->dhost, ent->mac_addr, ETHER_ADDR_LEN);
456 syscall(sig_sem(SEM_ARP_CACHE_LOCK));
457 return true;
458 }
459 else {
460 /* 送信がペンデングされているフレームがあれば捨てる。*/
461 if (ent->hold) {
462 NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_ERR_PACKETS], 1);
463 syscall(rel_net_buf(ent->hold));
464 }
465
466 /*
467 * 送信をペンディングする。
468 * IF でネットワークバッファを開放しないフラグが設定されているときは、
469 * 送信をペンディングしない。
470 */
471 if ((output->flags & NB_FLG_NOREL_IFOUT) == 0)
472 ent->hold = output;
473 else {
474 output->flags &= (uint8_t)~NB_FLG_NOREL_IFOUT;
475 ent->hold = NULL;
476 }
477 syscall(sig_sem(SEM_ARP_CACHE_LOCK));
478
479 /* アドレス解決要求を送信する。*/
480 arp_request(ifaddr, gw);
481 return false;
482 }
483 }
484
485/*
486 * arp_init -- ARP 初期化
487 */
488
489void
490arp_init (void)
491{
492 timeout(arp_timer, NULL, ARP_TIMER_TMO);
493 }
494
495#ifdef ETHER_CFG_MULTICAST
496/*
497 * ether_map_ipv4_multicast -- IPv4 マルチキャストアドレスからイーサネット・マルチキャストアドレスへの変換
498 */
499
500void
501ether_map_ipv4_multicast (T_ETHER_ADDR *eaddr, T_IN4_ADDR maddr)
502{
503 eaddr->lladdr[0] = UINT_C(0x01);
504 eaddr->lladdr[1] = UINT_C(0x00);
505 eaddr->lladdr[2] = UINT_C(0x5E);
506 eaddr->lladdr[3] = (maddr & 0x7F0000) >> 16;
507 eaddr->lladdr[4] = (maddr & 0xFF00) >> 8;
508 eaddr->lladdr[5] = maddr & 0xFF;
509 }
510#endif /* ETHER_CFG_MULTICAST */
511
512#endif /* of #if defined(SUPPORT_INET4) && defined(SUPPORT_ETHER) */
Note: See TracBrowser for help on using the repository browser.