This source file includes following definitions.
- send_arp_q
- print_arp
- arp_sourceh
- arp_targeth
- arp_sourcep
- arp_targetp
- arp_free
- arp_malloc
- arp_response
- arp_lookup
- arp_destroy
- create_arp
- arp_rcv
- arp_snd
- arp_find
- arp_add
- arp_add_broad
- arp_queue
- arpreq_check
- arp_ioctl_set
- arp_ioctl_get
- arp_ioctl_del
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 #include <linux/types.h>
52 #include <linux/string.h>
53 #include <linux/kernel.h>
54 #include <linux/sched.h>
55 #include <linux/config.h>
56
57 #include <linux/socket.h>
58 #include <netinet/in.h>
59 #include <linux/sock_ioctl.h>
60 #include <linux/errno.h>
61 #include <asm/segment.h>
62 #include <asm/system.h>
63
64 #include "timer.h"
65 #include "ip.h"
66 #include "tcp.h"
67 #include "sock.h"
68 #include "arp.h"
69
70 #undef ARP_DEBUG
71
72 #ifdef ARP_DEBUG
73 #define PRINTK(x) printk x
74 #else
75 #define PRINTK(x)
76 #endif
77
78 static volatile struct arp_table *arp_table[ARP_TABLE_SIZE] ={NULL, };
79 volatile struct sk_buff *arp_q=NULL;
80
81
82 static void
83 send_arp_q(void)
84 {
85 struct sk_buff *skb;
86 struct sk_buff *next;
87
88 cli();
89 next = (struct sk_buff *)arp_q;
90 arp_q = NULL;
91 sti();
92 while ((skb = next) != NULL) {
93 if (skb->magic != ARP_QUEUE_MAGIC)
94 {
95 printk ("arp.c skb with bad magic - %X: squashing queue\n", skb->magic);
96 return;
97 }
98
99 if (skb->next == NULL
100 #ifdef CONFIG_MAX_16M
101 || (unsigned long)(skb->next) > 16*1024*1024
102 #endif
103 )
104 {
105 printk ("dev.c: *** bug bad skb->next, squashing queue \n");
106 return;
107 }
108
109
110 next = (struct sk_buff *)skb->next;
111 if (next == skb)
112 {
113 next = NULL;
114 }
115 else
116 {
117 skb->prev->next = next;
118 next->prev = skb->prev;
119 }
120
121 skb->magic = 0;
122 skb->next = NULL;
123 skb->prev = NULL;
124
125 if (!skb->dev->rebuild_header (skb+1, skb->dev))
126 {
127 skb->next = NULL;
128 skb->prev = NULL;
129 skb->arp = 1;
130 skb->dev->queue_xmit (skb, skb->dev, 0);
131 }
132 else
133 {
134 cli();
135 skb->magic = ARP_QUEUE_MAGIC;
136 if (arp_q == NULL)
137 {
138 skb->next = skb;
139 skb->prev = skb;
140 arp_q = skb;
141 }
142 else
143 {
144 skb->next = arp_q;
145 skb->prev = arp_q->prev;
146 arp_q->prev->next = skb;
147 arp_q->prev = skb;
148 }
149 sti();
150 }
151 }
152 }
153
154 static void
155 print_arp(struct arp *arp)
156 {
157 int i;
158 unsigned long *lptr;
159 unsigned char *ptr;
160 PRINTK (("arp: \n"));
161 if (arp == NULL)
162 {
163 PRINTK (("(null)\n"));
164 return;
165 }
166 PRINTK ((" hrd = %d\n",net16(arp->hrd)));
167 PRINTK ((" pro = %d\n",net16(arp->pro)));
168 PRINTK ((" hlen = %d plen = %d\n",arp->hlen, arp->plen));
169 PRINTK ((" op = %d\n", net16(arp->op)));
170 ptr = (unsigned char *)(arp+1);
171 PRINTK ((" sender haddr = "));
172 for (i = 0; i < arp->hlen; i++)
173 {
174 PRINTK (("0x%02X ",*ptr++));
175 }
176 lptr = (void *)ptr;
177 PRINTK ((" send paddr = %X\n",*lptr));
178 lptr ++;
179 ptr = (void *)lptr;
180 PRINTK ((" destination haddr = "));
181 for (i = 0; i < arp->hlen; i++)
182 {
183 PRINTK (("0x%02X ",*ptr++));
184 }
185 lptr = (void *)ptr;
186 PRINTK ((" destination paddr = %X\n",*lptr));
187 }
188
189 static unsigned char *
190 arp_sourceh(struct arp *arp)
191 {
192 unsigned char *ptr;
193 ptr = (unsigned char *)(arp + 1);
194 return (ptr);
195 }
196
197 static unsigned char *
198 arp_targeth(struct arp *arp)
199 {
200 unsigned char *ptr;
201 ptr = (unsigned char *)(arp + 1);
202 ptr += arp->hlen+4;
203 return (ptr);
204 }
205
206 static unsigned long *
207 arp_sourcep(struct arp *arp)
208 {
209 unsigned long *lptr;
210 unsigned char *ptr;
211 ptr = (unsigned char *)(arp + 1);
212 ptr += arp->hlen;
213 lptr = (unsigned long *)ptr;
214 return (lptr);
215 }
216
217
218 static unsigned long *
219 arp_targetp(struct arp *arp)
220 {
221 unsigned long *lptr;
222 unsigned char *ptr;
223 ptr = (unsigned char *)(arp + 1);
224 ptr += 2*arp->hlen+4;
225 lptr = (unsigned long *)ptr;
226 return (lptr);
227 }
228
229 static void
230 arp_free (void *ptr, unsigned long len)
231 {
232 kfree_s(ptr, len);
233 }
234
235 static void *
236 arp_malloc (unsigned long amount, int priority)
237 {
238 return (kmalloc (amount, priority));
239 }
240
241 static int
242 arp_response (struct arp *arp1, struct device *dev)
243 {
244 struct arp *arp2;
245 struct sk_buff *skb;
246 int tmp;
247
248
249 skb = arp_malloc (sizeof (*skb) + sizeof (*arp2) +
250 2*arp1->hlen + 2*arp1->plen + dev->hard_header_len,
251 GFP_ATOMIC);
252 if (skb == NULL) return (1);
253
254 skb->lock = 0;
255 skb->mem_addr = skb;
256 skb->mem_len = sizeof (*skb) + sizeof (*arp2) + 2*arp1->hlen +
257 2*arp1->plen + dev->hard_header_len;
258 skb->len = sizeof (*arp2) + 2*arp1->hlen +
259 2*arp1->plen + dev->hard_header_len;
260
261 tmp = dev->hard_header((unsigned char *)(skb+1), dev,
262 ETHERTYPE_ARP, *arp_sourcep(arp1),
263 *arp_targetp(arp1),skb->len);
264
265 if (tmp < 0) return (1);
266
267 arp2 =(struct arp *) ((unsigned char *)skb+sizeof (*skb) + tmp );
268 memcpy (arp2, arp1, sizeof (*arp2));
269
270
271 *arp_sourcep(arp2) = *arp_targetp(arp1);
272 memcpy(arp_sourceh(arp2), dev->dev_addr, arp1->hlen);
273
274 *arp_targetp(arp2) = *arp_sourcep(arp1);
275 memcpy(arp_targeth(arp2), arp_sourceh(arp1), arp1->hlen);
276
277 arp2->op = NET16(ARP_REPLY);
278 skb->free = 1;
279 skb->arp = 1;
280 skb->sk = NULL;
281 skb->next = NULL;
282 PRINTK ((">>"));
283 print_arp(arp2);
284
285 dev->queue_xmit (skb, dev, 0);
286 return (0);
287 }
288
289
290
291 static struct arp_table *
292 arp_lookup (unsigned long paddr)
293 {
294 unsigned long hash;
295 struct arp_table *apt;
296 PRINTK (("arp_lookup(paddr=%X)\n", paddr));
297
298 if (my_ip_addr(paddr)) return (NULL);
299 hash = net32(paddr) & (ARP_TABLE_SIZE - 1);
300 cli();
301 for (apt = (struct arp_table *)arp_table[hash];
302 apt != NULL;
303 apt = (struct arp_table *)apt->next)
304 {
305 if (apt->ip == paddr)
306 {
307 sti();
308 return (apt);
309 }
310 }
311 sti();
312 return (NULL);
313 }
314
315 void
316 arp_destroy(unsigned long paddr)
317 {
318 unsigned long hash;
319 struct arp_table *apt;
320 struct arp_table **lapt;
321 PRINTK (("arp_destroy (paddr=%X)\n",paddr));
322
323 if (my_ip_addr(paddr)) return;
324 hash = net32(paddr) & (ARP_TABLE_SIZE - 1);
325
326 cli();
327 lapt = (struct arp_table **) &arp_table[hash];
328 while ((apt = *lapt) != NULL) {
329 if (apt->ip == paddr)
330 {
331 *lapt = (struct arp_table *) apt->next;
332 arp_free(apt, sizeof(*apt));
333 sti();
334 return;
335 }
336 lapt = (struct arp_table **) &apt->next;
337 }
338 sti();
339 }
340
341
342
343 static struct arp_table *
344 create_arp (unsigned long paddr, unsigned char *addr, int hlen)
345 {
346 struct arp_table *apt;
347 unsigned long hash;
348 apt = arp_malloc (sizeof (*apt), GFP_ATOMIC);
349 if (apt == NULL) return (NULL);
350
351 hash = net32(paddr) & (ARP_TABLE_SIZE - 1);
352 apt->ip = paddr;
353 apt->hlen =hlen;
354 memcpy (apt->hard, addr, hlen);
355 apt->last_used=timer_seq;
356 cli();
357 apt->next = arp_table[hash];
358 arp_table[hash] = apt;
359 sti();
360 return (apt);
361 }
362
363 int
364 arp_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
365 {
366 struct arp *arp;
367 struct arp_table *tbl;
368 int ret;
369
370 PRINTK (("<<\n"));
371 arp = skb->h.arp;
372 print_arp(arp);
373
374
375 if (arp->hlen != dev->addr_len || dev->type !=NET16( arp->hrd))
376 {
377 kfree_skb(skb, FREE_READ);
378 return (0);
379 }
380
381
382 if (arp->pro != NET16(ARP_IP_PROT) || arp->plen != 4)
383 {
384 kfree_skb (skb, FREE_READ);
385 return (0);
386 }
387
388
389 tbl = arp_lookup (*arp_sourcep(arp));
390 if (tbl != NULL)
391 {
392 memcpy (tbl->hard, arp+1, arp->hlen);
393 tbl->hlen = arp->hlen;
394 tbl->last_used = timer_seq;
395 }
396
397 if (my_ip_addr(*arp_targetp(arp)) != IS_MYADDR)
398 {
399 kfree_skb (skb, FREE_READ);
400 return (0);
401 }
402
403 if (tbl == NULL)
404 create_arp (*arp_sourcep(arp), arp_sourceh(arp), arp->hlen);
405
406
407 send_arp_q();
408
409 if (arp->op != NET16(ARP_REQUEST))
410 {
411 kfree_skb (skb, FREE_READ);
412 return (0);
413 }
414
415
416 ret = arp_response(arp, dev);
417 kfree_skb (skb, FREE_READ);
418 return (ret);
419 }
420
421 void
422 arp_snd (unsigned long paddr, struct device *dev, unsigned long saddr)
423 {
424 struct sk_buff *skb;
425 struct arp *arp;
426 struct arp_table *apt;
427 int tmp;
428 PRINTK (("arp_snd (paddr=%X, dev=%X, saddr=%X)\n",paddr, dev, saddr));
429
430
431 apt = create_arp (paddr, NULL, 0);
432 if (apt == NULL) return;
433
434 skb = arp_malloc (sizeof (*arp) + sizeof (*skb) + dev->hard_header_len +
435 2*dev->addr_len+8, GFP_ATOMIC);
436 if (skb == NULL) return;
437
438 skb->lock = 0;
439 skb->sk = NULL;
440 skb->mem_addr = skb;
441 skb->mem_len = sizeof (*arp) + sizeof (*skb) + dev->hard_header_len +
442 2*dev->addr_len+8;
443 skb->arp = 1;
444 skb->dev = dev;
445 skb->len = sizeof (*arp) + dev->hard_header_len + 2*dev->addr_len+8;
446 skb->next = NULL;
447
448 tmp = dev->hard_header ((unsigned char *)(skb+1), dev,
449 ETHERTYPE_ARP, 0, saddr, skb->len);
450 if (tmp < 0)
451 {
452 arp_free (skb->mem_addr, skb->mem_len);
453 return;
454 }
455
456 arp =(struct arp *) ((unsigned char *)skb+sizeof (*skb) + tmp );
457 arp->hrd = net16(dev->type);
458 arp->pro = NET16(ARP_IP_PROT);
459 arp->hlen = dev->addr_len;
460 arp->plen = 4;
461 arp->op = NET16(ARP_REQUEST);
462 *arp_sourcep(arp) = saddr;
463 *arp_targetp(arp) = paddr;
464 memcpy (arp_sourceh(arp), dev->dev_addr, dev->addr_len);
465 memcpy (arp_targeth(arp), dev->broadcast, dev->addr_len);
466 PRINTK((">>\n"));
467 print_arp(arp);
468 dev->queue_xmit (skb, dev, 0);
469 }
470
471 int
472 arp_find(unsigned char *haddr, unsigned long paddr, struct device *dev,
473 unsigned long saddr)
474 {
475 struct arp_table *apt;
476 PRINTK (("arp_find(haddr=%X, paddr=%X, dev=%X, saddr=%X)\n",
477 haddr, paddr, dev, saddr));
478 if (my_ip_addr (paddr))
479 {
480 memcpy (haddr, dev->dev_addr, dev->addr_len);
481 return (0);
482 }
483 apt = arp_lookup (paddr);
484 if (apt != NULL)
485 {
486
487
488
489 if (!before (apt->last_used, timer_seq+ARP_TIMEOUT) &&
490 apt->hlen != 0)
491 {
492 apt->last_used=timer_seq;
493 memcpy (haddr, apt->hard, dev->addr_len);
494 return (0);
495 }
496 else
497 {
498 arp_destroy(paddr);
499 }
500 }
501
502
503
504
505 *(unsigned long *)haddr = paddr;
506
507
508
509 arp_snd(paddr,dev,saddr);
510
511 return (1);
512 }
513
514 void
515 arp_add (unsigned long addr, unsigned char *haddr, struct device *dev)
516 {
517 struct arp_table *apt;
518
519 apt = arp_lookup (addr);
520 if (apt != NULL)
521 {
522 apt->last_used = timer_seq;
523 memcpy (apt->hard, haddr , dev->addr_len);
524 return;
525 }
526 create_arp (addr, haddr, dev->addr_len);
527 }
528
529 void
530 arp_add_broad (unsigned long addr, struct device *dev)
531 {
532 arp_add (addr, dev->broadcast , dev);
533 }
534
535 void
536 arp_queue(struct sk_buff *skb)
537 {
538 cli();
539 if (skb->next != NULL)
540 {
541 sti();
542 printk ("arp.c: arp_queue skb already on queue magic=%X. \n",
543 skb->magic);
544 return;
545 }
546 if (arp_q == NULL)
547 {
548 arp_q = skb;
549 skb->next = skb;
550 skb->prev = skb;
551 }
552 else
553 {
554 skb->next = arp_q;
555 skb->prev = arp_q->prev;
556 skb->next->prev = skb;
557 skb->prev->next = skb;
558 }
559 skb->magic = ARP_QUEUE_MAGIC;
560 sti();
561 }
562
563 static int arpreq_check(struct arpreq *req)
564 {
565
566 if (req->arp_pa.sa_family != AF_INET) return -1;
567
568 if (req->arp_ha.sa_family != AF_UNSPEC) return -1;
569
570 return 0;
571 }
572
573 int arp_ioctl_set(struct arpreq *req)
574 {
575 struct arpreq r;
576 struct arp_table *apt;
577
578 memcpy_fromfs(&r, req, sizeof(r));
579 if (arpreq_check(&r) != 0) return -EPFNOSUPPORT;
580
581 apt = arp_lookup(*(unsigned long *)r.arp_pa.sa_data);
582 if (apt) {
583 apt->last_used = timer_seq;
584 memcpy(apt->hard, r.arp_ha.sa_data , 6);
585 } else {
586 if (!create_arp(*(unsigned long *)r.arp_pa.sa_data,
587 r.arp_ha.sa_data, 6)) {
588 return -ENOMEM;
589 }
590 }
591
592 return 0;
593 }
594
595 int arp_ioctl_get(struct arpreq *req)
596 {
597 struct arpreq r;
598 struct arp_table *apt;
599
600 memcpy_fromfs(&r, req, sizeof(r));
601 if (arpreq_check(&r) != 0) return -EPFNOSUPPORT;
602 apt = arp_lookup(*(unsigned long *)r.arp_pa.sa_data);
603 if (apt) {
604 memcpy(r.arp_ha.sa_data, apt->hard, apt->hlen);
605 } else {
606 return -ENXIO;
607 }
608
609
610 memcpy_tofs(req, &r, sizeof(r));
611 return 0;
612 }
613
614 int arp_ioctl_del(struct arpreq *req)
615 {
616 struct arpreq r;
617
618 memcpy_fromfs(&r, req, sizeof(r));
619 if (arpreq_check(&r) != 0) return -EPFNOSUPPORT;
620
621 arp_destroy(*(unsigned long *)r.arp_pa.sa_data);
622
623 return 0;
624 }