OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [ipv4/] [ip_options.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * INET         An implementation of the TCP/IP protocol suite for the LINUX
3
 *              operating system.  INET is implemented using the  BSD Socket
4
 *              interface as the means of communication with the user level.
5
 *
6
 *              The options processing module for ip.c
7
 *
8
 * Version:     $Id: ip_options.c,v 1.1.1.1 2004-04-15 01:13:55 phoenix Exp $
9
 *
10
 * Authors:     A.N.Kuznetsov
11
 *
12
 */
13
 
14
#include <linux/types.h>
15
#include <asm/uaccess.h>
16
#include <linux/skbuff.h>
17
#include <linux/ip.h>
18
#include <linux/icmp.h>
19
#include <linux/netdevice.h>
20
#include <linux/rtnetlink.h>
21
#include <net/sock.h>
22
#include <net/ip.h>
23
#include <net/icmp.h>
24
 
25
/*
26
 * Write options to IP header, record destination address to
27
 * source route option, address of outgoing interface
28
 * (we should already know it, so that this  function is allowed be
29
 * called only after routing decision) and timestamp,
30
 * if we originate this datagram.
31
 *
32
 * daddr is real destination address, next hop is recorded in IP header.
33
 * saddr is address of outgoing interface.
34
 */
35
 
36
void ip_options_build(struct sk_buff * skb, struct ip_options * opt,
37
                            u32 daddr, struct rtable *rt, int is_frag)
38
{
39
        unsigned char * iph = skb->nh.raw;
40
 
41
        memcpy(&(IPCB(skb)->opt), opt, sizeof(struct ip_options));
42
        memcpy(iph+sizeof(struct iphdr), opt->__data, opt->optlen);
43
        opt = &(IPCB(skb)->opt);
44
        opt->is_data = 0;
45
 
46
        if (opt->srr)
47
                memcpy(iph+opt->srr+iph[opt->srr+1]-4, &daddr, 4);
48
 
49
        if (!is_frag) {
50
                if (opt->rr_needaddr)
51
                        ip_rt_get_source(iph+opt->rr+iph[opt->rr+2]-5, rt);
52
                if (opt->ts_needaddr)
53
                        ip_rt_get_source(iph+opt->ts+iph[opt->ts+2]-9, rt);
54
                if (opt->ts_needtime) {
55
                        struct timeval tv;
56
                        __u32 midtime;
57
                        do_gettimeofday(&tv);
58
                        midtime = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
59
                        memcpy(iph+opt->ts+iph[opt->ts+2]-5, &midtime, 4);
60
                }
61
                return;
62
        }
63
        if (opt->rr) {
64
                memset(iph+opt->rr, IPOPT_NOP, iph[opt->rr+1]);
65
                opt->rr = 0;
66
                opt->rr_needaddr = 0;
67
        }
68
        if (opt->ts) {
69
                memset(iph+opt->ts, IPOPT_NOP, iph[opt->ts+1]);
70
                opt->ts = 0;
71
                opt->ts_needaddr = opt->ts_needtime = 0;
72
        }
73
}
74
 
75
/*
76
 * Provided (sopt, skb) points to received options,
77
 * build in dopt compiled option set appropriate for answering.
78
 * i.e. invert SRR option, copy anothers,
79
 * and grab room in RR/TS options.
80
 *
81
 * NOTE: dopt cannot point to skb.
82
 */
83
 
84
int ip_options_echo(struct ip_options * dopt, struct sk_buff * skb)
85
{
86
        struct ip_options *sopt;
87
        unsigned char *sptr, *dptr;
88
        int soffset, doffset;
89
        int     optlen;
90
        u32     daddr;
91
 
92
        memset(dopt, 0, sizeof(struct ip_options));
93
 
94
        dopt->is_data = 1;
95
 
96
        sopt = &(IPCB(skb)->opt);
97
 
98
        if (sopt->optlen == 0) {
99
                dopt->optlen = 0;
100
                return 0;
101
        }
102
 
103
        sptr = skb->nh.raw;
104
        dptr = dopt->__data;
105
 
106
        if (skb->dst)
107
                daddr = ((struct rtable*)skb->dst)->rt_spec_dst;
108
        else
109
                daddr = skb->nh.iph->daddr;
110
 
111
        if (sopt->rr) {
112
                optlen  = sptr[sopt->rr+1];
113
                soffset = sptr[sopt->rr+2];
114
                dopt->rr = dopt->optlen + sizeof(struct iphdr);
115
                memcpy(dptr, sptr+sopt->rr, optlen);
116
                if (sopt->rr_needaddr && soffset <= optlen) {
117
                        if (soffset + 3 > optlen)
118
                                return -EINVAL;
119
                        dptr[2] = soffset + 4;
120
                        dopt->rr_needaddr = 1;
121
                }
122
                dptr += optlen;
123
                dopt->optlen += optlen;
124
        }
125
        if (sopt->ts) {
126
                optlen = sptr[sopt->ts+1];
127
                soffset = sptr[sopt->ts+2];
128
                dopt->ts = dopt->optlen + sizeof(struct iphdr);
129
                memcpy(dptr, sptr+sopt->ts, optlen);
130
                if (soffset <= optlen) {
131
                        if (sopt->ts_needaddr) {
132
                                if (soffset + 3 > optlen)
133
                                        return -EINVAL;
134
                                dopt->ts_needaddr = 1;
135
                                soffset += 4;
136
                        }
137
                        if (sopt->ts_needtime) {
138
                                if (soffset + 3 > optlen)
139
                                        return -EINVAL;
140
                                if ((dptr[3]&0xF) != IPOPT_TS_PRESPEC) {
141
                                        dopt->ts_needtime = 1;
142
                                        soffset += 4;
143
                                } else {
144
                                        dopt->ts_needtime = 0;
145
 
146
                                        if (soffset + 8 <= optlen) {
147
                                                __u32 addr;
148
 
149
                                                memcpy(&addr, sptr+soffset-1, 4);
150
                                                if (inet_addr_type(addr) != RTN_LOCAL) {
151
                                                        dopt->ts_needtime = 1;
152
                                                        soffset += 8;
153
                                                }
154
                                        }
155
                                }
156
                        }
157
                        dptr[2] = soffset;
158
                }
159
                dptr += optlen;
160
                dopt->optlen += optlen;
161
        }
162
        if (sopt->srr) {
163
                unsigned char * start = sptr+sopt->srr;
164
                u32 faddr;
165
 
166
                optlen  = start[1];
167
                soffset = start[2];
168
                doffset = 0;
169
                if (soffset > optlen)
170
                        soffset = optlen + 1;
171
                soffset -= 4;
172
                if (soffset > 3) {
173
                        memcpy(&faddr, &start[soffset-1], 4);
174
                        for (soffset-=4, doffset=4; soffset > 3; soffset-=4, doffset+=4)
175
                                memcpy(&dptr[doffset-1], &start[soffset-1], 4);
176
                        /*
177
                         * RFC1812 requires to fix illegal source routes.
178
                         */
179
                        if (memcmp(&skb->nh.iph->saddr, &start[soffset+3], 4) == 0)
180
                                doffset -= 4;
181
                }
182
                if (doffset > 3) {
183
                        memcpy(&start[doffset-1], &daddr, 4);
184
                        dopt->faddr = faddr;
185
                        dptr[0] = start[0];
186
                        dptr[1] = doffset+3;
187
                        dptr[2] = 4;
188
                        dptr += doffset+3;
189
                        dopt->srr = dopt->optlen + sizeof(struct iphdr);
190
                        dopt->optlen += doffset+3;
191
                        dopt->is_strictroute = sopt->is_strictroute;
192
                }
193
        }
194
        while (dopt->optlen & 3) {
195
                *dptr++ = IPOPT_END;
196
                dopt->optlen++;
197
        }
198
        return 0;
199
}
200
 
201
/*
202
 *      Options "fragmenting", just fill options not
203
 *      allowed in fragments with NOOPs.
204
 *      Simple and stupid 8), but the most efficient way.
205
 */
206
 
207
void ip_options_fragment(struct sk_buff * skb)
208
{
209
        unsigned char * optptr = skb->nh.raw;
210
        struct ip_options * opt = &(IPCB(skb)->opt);
211
        int  l = opt->optlen;
212
        int  optlen;
213
 
214
        while (l > 0) {
215
                switch (*optptr) {
216
                case IPOPT_END:
217
                        return;
218
                case IPOPT_NOOP:
219
                        l--;
220
                        optptr++;
221
                        continue;
222
                }
223
                optlen = optptr[1];
224
                if (optlen<2 || optlen>l)
225
                  return;
226
                if (!IPOPT_COPIED(*optptr))
227
                        memset(optptr, IPOPT_NOOP, optlen);
228
                l -= optlen;
229
                optptr += optlen;
230
        }
231
        opt->ts = 0;
232
        opt->rr = 0;
233
        opt->rr_needaddr = 0;
234
        opt->ts_needaddr = 0;
235
        opt->ts_needtime = 0;
236
        return;
237
}
238
 
239
/*
240
 * Verify options and fill pointers in struct options.
241
 * Caller should clear *opt, and set opt->data.
242
 * If opt == NULL, then skb->data should point to IP header.
243
 */
244
 
245
int ip_options_compile(struct ip_options * opt, struct sk_buff * skb)
246
{
247
        int l;
248
        unsigned char * iph;
249
        unsigned char * optptr;
250
        int optlen;
251
        unsigned char * pp_ptr = NULL;
252
        struct rtable *rt = skb ? (struct rtable*)skb->dst : NULL;
253
 
254
        if (!opt) {
255
                opt = &(IPCB(skb)->opt);
256
                memset(opt, 0, sizeof(struct ip_options));
257
                iph = skb->nh.raw;
258
                opt->optlen = ((struct iphdr *)iph)->ihl*4 - sizeof(struct iphdr);
259
                optptr = iph + sizeof(struct iphdr);
260
                opt->is_data = 0;
261
        } else {
262
                optptr = opt->is_data ? opt->__data : (unsigned char*)&(skb->nh.iph[1]);
263
                iph = optptr - sizeof(struct iphdr);
264
        }
265
 
266
        for (l = opt->optlen; l > 0; ) {
267
                switch (*optptr) {
268
                      case IPOPT_END:
269
                        for (optptr++, l--; l>0; optptr++, l--) {
270
                                if (*optptr != IPOPT_END) {
271
                                        *optptr = IPOPT_END;
272
                                        opt->is_changed = 1;
273
                                }
274
                        }
275
                        goto eol;
276
                      case IPOPT_NOOP:
277
                        l--;
278
                        optptr++;
279
                        continue;
280
                }
281
                optlen = optptr[1];
282
                if (optlen<2 || optlen>l) {
283
                        pp_ptr = optptr;
284
                        goto error;
285
                }
286
                switch (*optptr) {
287
                      case IPOPT_SSRR:
288
                      case IPOPT_LSRR:
289
                        if (optlen < 3) {
290
                                pp_ptr = optptr + 1;
291
                                goto error;
292
                        }
293
                        if (optptr[2] < 4) {
294
                                pp_ptr = optptr + 2;
295
                                goto error;
296
                        }
297
                        /* NB: cf RFC-1812 5.2.4.1 */
298
                        if (opt->srr) {
299
                                pp_ptr = optptr;
300
                                goto error;
301
                        }
302
                        if (!skb) {
303
                                if (optptr[2] != 4 || optlen < 7 || ((optlen-3) & 3)) {
304
                                        pp_ptr = optptr + 1;
305
                                        goto error;
306
                                }
307
                                memcpy(&opt->faddr, &optptr[3], 4);
308
                                if (optlen > 7)
309
                                        memmove(&optptr[3], &optptr[7], optlen-7);
310
                        }
311
                        opt->is_strictroute = (optptr[0] == IPOPT_SSRR);
312
                        opt->srr = optptr - iph;
313
                        break;
314
                      case IPOPT_RR:
315
                        if (opt->rr) {
316
                                pp_ptr = optptr;
317
                                goto error;
318
                        }
319
                        if (optlen < 3) {
320
                                pp_ptr = optptr + 1;
321
                                goto error;
322
                        }
323
                        if (optptr[2] < 4) {
324
                                pp_ptr = optptr + 2;
325
                                goto error;
326
                        }
327
                        if (optptr[2] <= optlen) {
328
                                if (optptr[2]+3 > optlen) {
329
                                        pp_ptr = optptr + 2;
330
                                        goto error;
331
                                }
332
                                if (skb) {
333
                                        memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4);
334
                                        opt->is_changed = 1;
335
                                }
336
                                optptr[2] += 4;
337
                                opt->rr_needaddr = 1;
338
                        }
339
                        opt->rr = optptr - iph;
340
                        break;
341
                      case IPOPT_TIMESTAMP:
342
                        if (opt->ts) {
343
                                pp_ptr = optptr;
344
                                goto error;
345
                        }
346
                        if (optlen < 4) {
347
                                pp_ptr = optptr + 1;
348
                                goto error;
349
                        }
350
                        if (optptr[2] < 5) {
351
                                pp_ptr = optptr + 2;
352
                                goto error;
353
                        }
354
                        if (optptr[2] <= optlen) {
355
                                __u32 * timeptr = NULL;
356
                                if (optptr[2]+3 > optptr[1]) {
357
                                        pp_ptr = optptr + 2;
358
                                        goto error;
359
                                }
360
                                switch (optptr[3]&0xF) {
361
                                      case IPOPT_TS_TSONLY:
362
                                        opt->ts = optptr - iph;
363
                                        if (skb)
364
                                                timeptr = (__u32*)&optptr[optptr[2]-1];
365
                                        opt->ts_needtime = 1;
366
                                        optptr[2] += 4;
367
                                        break;
368
                                      case IPOPT_TS_TSANDADDR:
369
                                        if (optptr[2]+7 > optptr[1]) {
370
                                                pp_ptr = optptr + 2;
371
                                                goto error;
372
                                        }
373
                                        opt->ts = optptr - iph;
374
                                        if (skb) {
375
                                                memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4);
376
                                                timeptr = (__u32*)&optptr[optptr[2]+3];
377
                                        }
378
                                        opt->ts_needaddr = 1;
379
                                        opt->ts_needtime = 1;
380
                                        optptr[2] += 8;
381
                                        break;
382
                                      case IPOPT_TS_PRESPEC:
383
                                        if (optptr[2]+7 > optptr[1]) {
384
                                                pp_ptr = optptr + 2;
385
                                                goto error;
386
                                        }
387
                                        opt->ts = optptr - iph;
388
                                        {
389
                                                u32 addr;
390
                                                memcpy(&addr, &optptr[optptr[2]-1], 4);
391
                                                if (inet_addr_type(addr) == RTN_UNICAST)
392
                                                        break;
393
                                                if (skb)
394
                                                        timeptr = (__u32*)&optptr[optptr[2]+3];
395
                                        }
396
                                        opt->ts_needtime = 1;
397
                                        optptr[2] += 8;
398
                                        break;
399
                                      default:
400
                                        if (!skb && !capable(CAP_NET_RAW)) {
401
                                                pp_ptr = optptr + 3;
402
                                                goto error;
403
                                        }
404
                                        break;
405
                                }
406
                                if (timeptr) {
407
                                        struct timeval tv;
408
                                        __u32  midtime;
409
                                        do_gettimeofday(&tv);
410
                                        midtime = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
411
                                        memcpy(timeptr, &midtime, sizeof(__u32));
412
                                        opt->is_changed = 1;
413
                                }
414
                        } else {
415
                                unsigned overflow = optptr[3]>>4;
416
                                if (overflow == 15) {
417
                                        pp_ptr = optptr + 3;
418
                                        goto error;
419
                                }
420
                                opt->ts = optptr - iph;
421
                                if (skb) {
422
                                        optptr[3] = (optptr[3]&0xF)|((overflow+1)<<4);
423
                                        opt->is_changed = 1;
424
                                }
425
                        }
426
                        break;
427
                      case IPOPT_RA:
428
                        if (optlen < 4) {
429
                                pp_ptr = optptr + 1;
430
                                goto error;
431
                        }
432
                        if (optptr[2] == 0 && optptr[3] == 0)
433
                                opt->router_alert = optptr - iph;
434
                        break;
435
                      case IPOPT_SEC:
436
                      case IPOPT_SID:
437
                      default:
438
                        if (!skb && !capable(CAP_NET_RAW)) {
439
                                pp_ptr = optptr;
440
                                goto error;
441
                        }
442
                        break;
443
                }
444
                l -= optlen;
445
                optptr += optlen;
446
        }
447
 
448
eol:
449
        if (!pp_ptr)
450
                return 0;
451
 
452
error:
453
        if (skb) {
454
                icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((pp_ptr-iph)<<24));
455
        }
456
        return -EINVAL;
457
}
458
 
459
 
460
/*
461
 *      Undo all the changes done by ip_options_compile().
462
 */
463
 
464
void ip_options_undo(struct ip_options * opt)
465
{
466
        if (opt->srr) {
467
                unsigned  char * optptr = opt->__data+opt->srr-sizeof(struct  iphdr);
468
                memmove(optptr+7, optptr+3, optptr[1]-7);
469
                memcpy(optptr+3, &opt->faddr, 4);
470
        }
471
        if (opt->rr_needaddr) {
472
                unsigned  char * optptr = opt->__data+opt->rr-sizeof(struct  iphdr);
473
                optptr[2] -= 4;
474
                memset(&optptr[optptr[2]-1], 0, 4);
475
        }
476
        if (opt->ts) {
477
                unsigned  char * optptr = opt->__data+opt->ts-sizeof(struct  iphdr);
478
                if (opt->ts_needtime) {
479
                        optptr[2] -= 4;
480
                        memset(&optptr[optptr[2]-1], 0, 4);
481
                        if ((optptr[3]&0xF) == IPOPT_TS_PRESPEC)
482
                                optptr[2] -= 4;
483
                }
484
                if (opt->ts_needaddr) {
485
                        optptr[2] -= 4;
486
                        memset(&optptr[optptr[2]-1], 0, 4);
487
                }
488
        }
489
}
490
 
491
int ip_options_get(struct ip_options **optp, unsigned char *data, int optlen, int user)
492
{
493
        struct ip_options *opt;
494
 
495
        opt = kmalloc(sizeof(struct ip_options)+((optlen+3)&~3), GFP_KERNEL);
496
        if (!opt)
497
                return -ENOMEM;
498
        memset(opt, 0, sizeof(struct ip_options));
499
        if (optlen) {
500
                if (user) {
501
                        if (copy_from_user(opt->__data, data, optlen)) {
502
                                kfree(opt);
503
                                return -EFAULT;
504
                        }
505
                } else
506
                        memcpy(opt->__data, data, optlen);
507
        }
508
        while (optlen & 3)
509
                opt->__data[optlen++] = IPOPT_END;
510
        opt->optlen = optlen;
511
        opt->is_data = 1;
512
        opt->is_setbyuser = 1;
513
        if (optlen && ip_options_compile(opt, NULL)) {
514
                kfree(opt);
515
                return -EINVAL;
516
        }
517
        *optp = opt;
518
        return 0;
519
}
520
 
521
void ip_forward_options(struct sk_buff *skb)
522
{
523
        struct   ip_options * opt       = &(IPCB(skb)->opt);
524
        unsigned char * optptr;
525
        struct rtable *rt = (struct rtable*)skb->dst;
526
        unsigned char *raw = skb->nh.raw;
527
 
528
        if (opt->rr_needaddr) {
529
                optptr = (unsigned char *)raw + opt->rr;
530
                ip_rt_get_source(&optptr[optptr[2]-5], rt);
531
                opt->is_changed = 1;
532
        }
533
        if (opt->srr_is_hit) {
534
                int srrptr, srrspace;
535
 
536
                optptr = raw + opt->srr;
537
 
538
                for ( srrptr=optptr[2], srrspace = optptr[1];
539
                     srrptr <= srrspace;
540
                     srrptr += 4
541
                     ) {
542
                        if (srrptr + 3 > srrspace)
543
                                break;
544
                        if (memcmp(&rt->rt_dst, &optptr[srrptr-1], 4) == 0)
545
                                break;
546
                }
547
                if (srrptr + 3 <= srrspace) {
548
                        opt->is_changed = 1;
549
                        ip_rt_get_source(&optptr[srrptr-1], rt);
550
                        skb->nh.iph->daddr = rt->rt_dst;
551
                        optptr[2] = srrptr+4;
552
                } else if (net_ratelimit())
553
                        printk(KERN_CRIT "ip_forward(): Argh! Destination lost!\n");
554
                if (opt->ts_needaddr) {
555
                        optptr = raw + opt->ts;
556
                        ip_rt_get_source(&optptr[optptr[2]-9], rt);
557
                        opt->is_changed = 1;
558
                }
559
        }
560
        if (opt->is_changed) {
561
                opt->is_changed = 0;
562
                ip_send_check(skb->nh.iph);
563
        }
564
}
565
 
566
int ip_options_rcv_srr(struct sk_buff *skb)
567
{
568
        struct ip_options *opt = &(IPCB(skb)->opt);
569
        int srrspace, srrptr;
570
        u32 nexthop;
571
        struct iphdr *iph = skb->nh.iph;
572
        unsigned char * optptr = skb->nh.raw + opt->srr;
573
        struct rtable *rt = (struct rtable*)skb->dst;
574
        struct rtable *rt2;
575
        int err;
576
 
577
        if (!opt->srr)
578
                return 0;
579
 
580
        if (skb->pkt_type != PACKET_HOST)
581
                return -EINVAL;
582
        if (rt->rt_type == RTN_UNICAST) {
583
                if (!opt->is_strictroute)
584
                        return 0;
585
                icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl(16<<24));
586
                return -EINVAL;
587
        }
588
        if (rt->rt_type != RTN_LOCAL)
589
                return -EINVAL;
590
 
591
        for (srrptr=optptr[2], srrspace = optptr[1]; srrptr <= srrspace; srrptr += 4) {
592
                if (srrptr + 3 > srrspace) {
593
                        icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((opt->srr+2)<<24));
594
                        return -EINVAL;
595
                }
596
                memcpy(&nexthop, &optptr[srrptr-1], 4);
597
 
598
                rt = (struct rtable*)skb->dst;
599
                skb->dst = NULL;
600
                err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, skb->dev);
601
                rt2 = (struct rtable*)skb->dst;
602
                if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) {
603
                        ip_rt_put(rt2);
604
                        skb->dst = &rt->u.dst;
605
                        return -EINVAL;
606
                }
607
                ip_rt_put(rt);
608
                if (rt2->rt_type != RTN_LOCAL)
609
                        break;
610
                /* Superfast 8) loopback forward */
611
                memcpy(&iph->daddr, &optptr[srrptr-1], 4);
612
                opt->is_changed = 1;
613
        }
614
        if (srrptr <= srrspace) {
615
                opt->srr_is_hit = 1;
616
                opt->is_changed = 1;
617
        }
618
        return 0;
619
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.