This source file includes following definitions.
- icmp_out_count
- icmp_glue_bits
- icmp_build_xmit
- icmp_send
- icmp_unreach
- icmp_redirect
- icmp_echo
- icmp_timestamp
- icmp_address
- icmp_discard
- icmp_rcv
- icmp_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 #include <linux/config.h>
86 #include <linux/types.h>
87 #include <linux/sched.h>
88 #include <linux/kernel.h>
89 #include <linux/fcntl.h>
90 #include <linux/socket.h>
91 #include <linux/in.h>
92 #include <linux/inet.h>
93 #include <linux/netdevice.h>
94 #include <linux/string.h>
95 #include <net/snmp.h>
96 #include <net/ip.h>
97 #include <net/route.h>
98 #include <net/protocol.h>
99 #include <net/icmp.h>
100 #include <net/tcp.h>
101 #include <net/snmp.h>
102 #include <linux/skbuff.h>
103 #include <net/sock.h>
104 #include <linux/errno.h>
105 #include <linux/timer.h>
106 #include <asm/system.h>
107 #include <asm/segment.h>
108 #include <net/checksum.h>
109
110 #define min(a,b) ((a)<(b)?(a):(b))
111
112
113
114
115
116 struct icmp_mib icmp_statistics;
117
118
119
120
121 struct icmp_err icmp_err_convert[] = {
122 { ENETUNREACH, 0 },
123 { EHOSTUNREACH, 0 },
124 { ENOPROTOOPT, 1 },
125 { ECONNREFUSED, 1 },
126 { EOPNOTSUPP, 0 },
127 { EOPNOTSUPP, 0 },
128 { ENETUNREACH, 1 },
129 { EHOSTDOWN, 1 },
130 { ENONET, 1 },
131 { ENETUNREACH, 1 },
132 { EHOSTUNREACH, 1 },
133 { EOPNOTSUPP, 0 },
134 { EOPNOTSUPP, 0 }
135 };
136
137
138
139
140
141 unsigned long dummy;
142
143
144
145
146
147 struct icmp_control
148 {
149 unsigned long *output;
150 unsigned long *input;
151 void (*handler)(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev, __u32 saddr, __u32 daddr, int len);
152 unsigned long error;
153 };
154
155 static struct icmp_control icmp_pointers[19];
156
157
158
159
160
161 struct icmp_bxm
162 {
163 void *data_ptr;
164 int data_len;
165 struct icmphdr icmph;
166 unsigned long csum;
167 struct options replyopts;
168 unsigned char optbuf[40];
169 };
170
171
172
173
174
175
176
177 struct socket icmp_socket;
178
179
180
181
182
183
184
185
186
187
188 static void icmp_out_count(int type)
189 {
190 if(type>18)
191 return;
192 (*icmp_pointers[type].output)++;
193 icmp_statistics.IcmpOutMsgs++;
194 }
195
196
197
198
199
200 static void icmp_glue_bits(const void *p, __u32 saddr, char *to, unsigned int offset, unsigned int fraglen)
201 {
202 struct icmp_bxm *icmp_param = (struct icmp_bxm *)p;
203 struct icmphdr *icmph;
204 unsigned long csum;
205
206 if (offset) {
207 icmp_param->csum=csum_partial_copy(icmp_param->data_ptr+offset-sizeof(struct icmphdr),
208 to, fraglen,icmp_param->csum);
209 return;
210 }
211
212
213
214
215
216
217 csum = csum_partial_copy((void *)&icmp_param->icmph,
218 to, sizeof(struct icmphdr),
219 icmp_param->csum);
220 csum = csum_partial_copy(icmp_param->data_ptr,
221 to+sizeof(struct icmphdr),
222 fraglen-sizeof(struct icmphdr), csum);
223 icmph=(struct icmphdr *)to;
224 icmph->checksum = csum_fold(csum);
225 }
226
227
228
229
230
231 static void icmp_build_xmit(struct icmp_bxm *icmp_param, __u32 saddr, __u32 daddr)
232 {
233 struct sock *sk=icmp_socket.data;
234 icmp_param->icmph.checksum=0;
235 icmp_param->csum=0;
236 icmp_out_count(icmp_param->icmph.type);
237 ip_build_xmit(sk, icmp_glue_bits, icmp_param,
238 icmp_param->data_len+sizeof(struct icmphdr),
239 daddr, saddr, &icmp_param->replyopts, 0, IPPROTO_ICMP, 1);
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253 void icmp_send(struct sk_buff *skb_in, int type, int code, unsigned long info, struct device *dev)
254 {
255 struct iphdr *iph;
256 struct icmphdr *icmph;
257 int atype;
258 struct icmp_bxm icmp_param;
259 __u32 saddr;
260
261
262
263
264
265 iph = skb_in->ip_hdr;
266
267
268
269
270
271 if(skb_in->pkt_type!=PACKET_HOST)
272 return;
273
274
275
276
277
278 atype=ip_chk_addr(iph->daddr);
279 if(atype==IS_BROADCAST||atype==IS_MULTICAST)
280 return;
281
282
283
284
285
286
287 if(iph->frag_off&htons(IP_OFFSET))
288 return;
289
290
291
292
293
294 if(icmp_pointers[type].error)
295 {
296
297
298
299
300 if(iph->protocol==IPPROTO_ICMP)
301 {
302 icmph = (struct icmphdr *)((char *)iph + (iph->ihl<<2));
303
304
305
306
307 if(icmph->type>18 || icmp_pointers[icmph->type].error)
308 return;
309 }
310 }
311
312
313
314
315
316 saddr=iph->daddr;
317 if(saddr!=dev->pa_addr && ip_chk_addr(saddr)!=IS_MYADDR)
318 saddr=dev->pa_addr;
319
320 icmp_param.icmph.type=type;
321 icmp_param.icmph.code=code;
322 icmp_param.icmph.un.gateway = info;
323 icmp_param.data_ptr=iph;
324 icmp_param.data_len=(iph->ihl<<2)+8;
325
326
327
328
329
330 if (ip_options_echo(&icmp_param.replyopts, NULL, saddr, iph->saddr, skb_in) == 0)
331 icmp_build_xmit(&icmp_param, saddr, iph->saddr);
332 }
333
334
335
336
337
338
339 static void icmp_unreach(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev, __u32 saddr, __u32 daddr, int len)
340 {
341 struct iphdr *iph;
342 int hash;
343 struct inet_protocol *ipprot;
344 unsigned char *dp;
345
346 iph = (struct iphdr *) (icmph + 1);
347
348 dp= ((unsigned char *)iph)+(iph->ihl<<2);
349
350 if(icmph->type==ICMP_DEST_UNREACH)
351 {
352 switch(icmph->code & 15)
353 {
354 case ICMP_NET_UNREACH:
355 break;
356 case ICMP_HOST_UNREACH:
357 break;
358 case ICMP_PROT_UNREACH:
359 printk("ICMP: %s:%d: protocol unreachable.\n",
360 in_ntoa(iph->daddr), ntohs(iph->protocol));
361 break;
362 case ICMP_PORT_UNREACH:
363 break;
364 case ICMP_FRAG_NEEDED:
365 printk("ICMP: %s: fragmentation needed and DF set.\n",
366 in_ntoa(iph->daddr));
367 break;
368 case ICMP_SR_FAILED:
369 printk("ICMP: %s: Source Route Failed.\n", in_ntoa(iph->daddr));
370 break;
371 default:
372 break;
373 }
374 if(icmph->code>12)
375 return;
376 }
377
378
379
380
381
382
383
384
385
386
387
388
389
390 hash = iph->protocol & (MAX_INET_PROTOS -1);
391
392
393
394
395
396
397
398 ipprot = (struct inet_protocol *) inet_protos[hash];
399 while(ipprot != NULL)
400 {
401 struct inet_protocol *nextip;
402
403 nextip = (struct inet_protocol *) ipprot->next;
404
405
406
407
408
409
410
411
412 if (iph->protocol == ipprot->protocol && ipprot->err_handler)
413 {
414 ipprot->err_handler(icmph->type, icmph->code, dp,
415 iph->daddr, iph->saddr, ipprot);
416 }
417
418 ipprot = nextip;
419 }
420 kfree_skb(skb, FREE_READ);
421 }
422
423
424
425
426
427
428 static void icmp_redirect(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev, __u32 source, __u32 daddr, int len)
429 {
430 #ifndef CONFIG_IP_FORWARD
431 struct rtable *rt;
432 #endif
433 struct iphdr *iph;
434 unsigned long ip;
435
436
437
438
439
440 iph = (struct iphdr *) (icmph + 1);
441 ip = iph->daddr;
442
443 #ifdef CONFIG_IP_FORWARD
444
445
446
447 printk("icmp: ICMP redirect from %s on %s ignored.\n", in_ntoa(source), dev->name);
448 #else
449 switch(icmph->code & 7)
450 {
451 case ICMP_REDIR_NET:
452
453
454
455
456
457 #ifdef not_a_good_idea
458 ip_rt_add((RTF_DYNAMIC | RTF_MODIFIED | RTF_GATEWAY),
459 ip, 0, icmph->un.gateway, dev,0, 0, 0);
460 #endif
461
462
463
464
465
466 case ICMP_REDIR_HOST:
467
468
469
470
471
472
473
474
475 rt = ip_rt_route(ip, NULL, NULL);
476 if (!rt)
477 break;
478 if (rt->rt_gateway != source ||
479 ((icmph->un.gateway^dev->pa_addr)&dev->pa_mask) ||
480 ip_chk_addr(icmph->un.gateway))
481 break;
482 printk("ICMP redirect from %s\n", in_ntoa(source));
483 ip_rt_add((RTF_DYNAMIC | RTF_MODIFIED | RTF_HOST | RTF_GATEWAY),
484 ip, 0, icmph->un.gateway, dev,0, 0, 0, 0);
485 break;
486 case ICMP_REDIR_NETTOS:
487 case ICMP_REDIR_HOSTTOS:
488 printk("ICMP: cannot handle TOS redirects yet!\n");
489 break;
490 default:
491 break;
492 }
493 #endif
494
495
496
497
498 kfree_skb(skb, FREE_READ);
499 }
500
501
502
503
504
505
506
507
508
509 static void icmp_echo(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev, __u32 saddr, __u32 daddr, int len)
510 {
511 struct icmp_bxm icmp_param;
512 icmp_param.icmph=*icmph;
513 icmp_param.icmph.type=ICMP_ECHOREPLY;
514 icmp_param.data_ptr=(icmph+1);
515 icmp_param.data_len=len;
516 if (ip_options_echo(&icmp_param.replyopts, NULL, daddr, saddr, skb)==0)
517 icmp_build_xmit(&icmp_param, daddr, saddr);
518 kfree_skb(skb, FREE_READ);
519 }
520
521
522
523
524
525
526
527
528
529 static void icmp_timestamp(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev, __u32 saddr, __u32 daddr, int len)
530 {
531 __u32 times[3];
532 struct icmp_bxm icmp_param;
533
534
535
536
537
538 if(len<12)
539 {
540 icmp_statistics.IcmpInErrors++;
541 kfree_skb(skb, FREE_READ);
542 return;
543 }
544
545
546
547
548
549 {
550 struct timeval tv;
551 do_gettimeofday(&tv);
552 times[1] = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
553 }
554 times[2] = times[1];
555 memcpy((void *)×[0], icmph+1, 4);
556 icmp_param.icmph=*icmph;
557 icmp_param.icmph.type=ICMP_TIMESTAMPREPLY;
558 icmp_param.icmph.code=0;
559 icmp_param.data_ptr=×
560 icmp_param.data_len=12;
561 if (ip_options_echo(&icmp_param.replyopts, NULL, daddr, saddr, skb)==0)
562 icmp_build_xmit(&icmp_param, daddr, saddr);
563 kfree_skb(skb,FREE_READ);
564 }
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579 static void icmp_address(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev, __u32 saddr, __u32 daddr, int len)
580 {
581 #ifdef CONFIG_IP_ADDR_AGENT
582 __u32 answer;
583 struct icmp_bxm icmp_param;
584 icmp_param.icmph.type=ICMP_ADDRESSREPLY;
585 icmp_param.icmph.code=0;
586 icmp_param.icmph.un.echo.id = icmph->un.echo.id;
587 icmp_param.icmph.un.echo.sequence = icmph->un.echo.sequence;
588 icmp_param.data_ptr=&dev->pa_mask;
589 icmp_param.data_len=4;
590 if (ip_options_echo(&icmp_param.replyopts, NULL, daddr, saddr, skb)==0)
591 icmp_build_xmit(&icmp_param, daddr, saddr);
592 #endif
593 kfree_skb(skb, FREE_READ);
594 }
595
596 static void icmp_discard(struct icmphdr *icmph, struct sk_buff *skb, struct device *dev, __u32 saddr, __u32 daddr, int len)
597 {
598 kfree_skb(skb, FREE_READ);
599 }
600
601
602
603
604
605 int icmp_rcv(struct sk_buff *skb, struct device *dev, struct options *opt,
606 __u32 daddr, unsigned short len,
607 __u32 saddr, int redo, struct inet_protocol *protocol)
608 {
609 struct icmphdr *icmph=(void *)skb->h.raw;
610 icmp_statistics.IcmpInMsgs++;
611
612
613
614
615
616 if (ip_compute_csum((unsigned char *) icmph, len))
617 {
618
619 icmp_statistics.IcmpInErrors++;
620 printk("ICMP: failed checksum from %s!\n", in_ntoa(saddr));
621 kfree_skb(skb, FREE_READ);
622 return(0);
623 }
624
625
626
627
628
629
630
631 if(icmph->type > 18)
632 {
633 icmp_statistics.IcmpInErrors++;
634 kfree_skb(skb,FREE_READ);
635 return(0);
636 }
637
638
639
640
641
642 if (daddr!=dev->pa_addr && ip_chk_addr(daddr) != IS_MYADDR)
643 {
644
645
646
647
648
649 if (icmph->type != ICMP_ECHO)
650 {
651 icmp_statistics.IcmpInErrors++;
652 kfree_skb(skb, FREE_READ);
653 return(0);
654 }
655
656
657
658
659
660 daddr=dev->pa_addr;
661 }
662
663 len-=sizeof(struct icmphdr);
664 (*icmp_pointers[icmph->type].input)++;
665 (icmp_pointers[icmph->type].handler)(icmph,skb,skb->dev,saddr,daddr,len);
666 return 0;
667 }
668
669
670
671
672
673 static struct icmp_control icmp_pointers[19] = {
674
675 { &icmp_statistics.IcmpOutEchoReps, &icmp_statistics.IcmpInEchoReps, icmp_discard, 0 },
676 { &dummy, &icmp_statistics.IcmpInErrors, icmp_discard, 1 },
677 { &dummy, &icmp_statistics.IcmpInErrors, icmp_discard, 1 },
678
679 { &icmp_statistics.IcmpOutDestUnreachs, &icmp_statistics.IcmpInDestUnreachs, icmp_unreach, 1 },
680
681 { &icmp_statistics.IcmpOutSrcQuenchs, &icmp_statistics.IcmpInSrcQuenchs, icmp_unreach, 1 },
682
683 { &icmp_statistics.IcmpOutRedirects, &icmp_statistics.IcmpInRedirects, icmp_redirect, 1 },
684 { &dummy, &icmp_statistics.IcmpInErrors, icmp_discard, 1 },
685 { &dummy, &icmp_statistics.IcmpInErrors, icmp_discard, 1 },
686
687 { &icmp_statistics.IcmpOutEchos, &icmp_statistics.IcmpInEchos, icmp_echo, 0 },
688 { &dummy, &icmp_statistics.IcmpInErrors, icmp_discard, 1 },
689 { &dummy, &icmp_statistics.IcmpInErrors, icmp_discard, 1 },
690
691 { &icmp_statistics.IcmpOutTimeExcds, &icmp_statistics.IcmpInTimeExcds, icmp_unreach, 1 },
692
693
694 { &icmp_statistics.IcmpOutParmProbs, &icmp_statistics.IcmpInParmProbs, icmp_discard, 1 },
695
696 { &icmp_statistics.IcmpOutTimestamps, &icmp_statistics.IcmpInTimestamps, icmp_timestamp, 0 },
697
698 { &icmp_statistics.IcmpOutTimestampReps, &icmp_statistics.IcmpInTimestampReps, icmp_discard, 0 },
699
700 { &dummy, &dummy, icmp_discard, 0 },
701
702 { &dummy, &dummy, icmp_discard, 0 },
703
704 { &icmp_statistics.IcmpOutAddrMasks, &icmp_statistics.IcmpInAddrMasks, icmp_address, 0 },
705
706 { &icmp_statistics.IcmpOutAddrMaskReps, &icmp_statistics.IcmpInAddrMaskReps, icmp_discard, 0 }
707 };
708
709 void icmp_init(struct proto_ops *ops)
710 {
711 struct sock *sk;
712 int err;
713 icmp_socket.type=SOCK_RAW;
714 icmp_socket.ops=ops;
715 if((err=ops->create(&icmp_socket, IPPROTO_ICMP))<0)
716 panic("Failed to create the ICMP control socket.\n");
717 sk=icmp_socket.data;
718 sk->allocation=GFP_ATOMIC;
719 sk->num = 256;
720 }
721