linux-old/net/ipv4/ip_options.c
<<
>>
Prefs
   1/*
   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.16 1999/03/21 05:22:40 davem 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
  36void 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
  84int 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
 207void 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
 245int 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; 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
 448eol:
 449        if (!pp_ptr)
 450                return 0;
 451
 452error:
 453        if (skb) {
 454                icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((pp_ptr-iph)<<24));
 455                kfree_skb(skb);
 456        }
 457        return -EINVAL;
 458}
 459
 460
 461/*
 462 *      Undo all the changes done by ip_options_compile().
 463 */
 464
 465void ip_options_undo(struct ip_options * opt)
 466{
 467        if (opt->srr) {
 468                unsigned  char * optptr = opt->__data+opt->srr-sizeof(struct  iphdr);
 469                memmove(optptr+7, optptr+3, optptr[1]-7);
 470                memcpy(optptr+3, &opt->faddr, 4);
 471        }
 472        if (opt->rr_needaddr) {
 473                unsigned  char * optptr = opt->__data+opt->rr-sizeof(struct  iphdr);
 474                optptr[2] -= 4;
 475                memset(&optptr[optptr[2]-1], 0, 4);
 476        }
 477        if (opt->ts) {
 478                unsigned  char * optptr = opt->__data+opt->ts-sizeof(struct  iphdr);
 479                if (opt->ts_needtime) {
 480                        optptr[2] -= 4;
 481                        memset(&optptr[optptr[2]-1], 0, 4);
 482                        if ((optptr[3]&0xF) == IPOPT_TS_PRESPEC)
 483                                optptr[2] -= 4;
 484                }
 485                if (opt->ts_needaddr) {
 486                        optptr[2] -= 4;
 487                        memset(&optptr[optptr[2]-1], 0, 4);
 488                }
 489        }
 490}
 491
 492int ip_options_get(struct ip_options **optp, unsigned char *data, int optlen, int user)
 493{
 494        struct ip_options *opt;
 495
 496        opt = kmalloc(sizeof(struct ip_options)+((optlen+3)&~3), GFP_KERNEL);
 497        if (!opt)
 498                return -ENOMEM;
 499        memset(opt, 0, sizeof(struct ip_options));
 500        if (optlen) {
 501                if (user) {
 502                        if (copy_from_user(opt->__data, data, optlen))
 503                                return -EFAULT;
 504                } else
 505                        memcpy(opt->__data, data, optlen);
 506        }
 507        while (optlen & 3)
 508                opt->__data[optlen++] = IPOPT_END;
 509        opt->optlen = optlen;
 510        opt->is_data = 1;
 511        opt->is_setbyuser = 1;
 512        if (optlen && ip_options_compile(opt, NULL)) {
 513                kfree_s(opt, sizeof(struct ip_options) + optlen);
 514                return -EINVAL;
 515        }
 516        *optp = opt;
 517        return 0;
 518}
 519
 520void ip_forward_options(struct sk_buff *skb)
 521{
 522        struct   ip_options * opt       = &(IPCB(skb)->opt);
 523        unsigned char * optptr;
 524        struct rtable *rt = (struct rtable*)skb->dst;
 525        unsigned char *raw = skb->nh.raw;
 526
 527        if (opt->rr_needaddr) {
 528                optptr = (unsigned char *)raw + opt->rr;
 529                ip_rt_get_source(&optptr[optptr[2]-5], rt);
 530                opt->is_changed = 1;
 531        }
 532        if (opt->srr_is_hit) {
 533                int srrptr, srrspace;
 534
 535                optptr = raw + opt->srr;
 536
 537                for ( srrptr=optptr[2], srrspace = optptr[1];
 538                     srrptr <= srrspace;
 539                     srrptr += 4
 540                     ) {
 541                        if (srrptr + 3 > srrspace)
 542                                break;
 543                        if (memcmp(&rt->rt_dst, &optptr[srrptr-1], 4) == 0)
 544                                break;
 545                }
 546                if (srrptr + 3 <= srrspace) {
 547                        opt->is_changed = 1;
 548                        ip_rt_get_source(&optptr[srrptr-1], rt);
 549                        skb->nh.iph->daddr = rt->rt_dst;
 550                        optptr[2] = srrptr+4;
 551                } else
 552                        printk(KERN_CRIT "ip_forward(): Argh! Destination lost!\n");
 553                if (opt->ts_needaddr) {
 554                        optptr = raw + opt->ts;
 555                        ip_rt_get_source(&optptr[optptr[2]-9], rt);
 556                        opt->is_changed = 1;
 557                }
 558        }
 559        if (opt->is_changed) {
 560                opt->is_changed = 0;
 561                ip_send_check(skb->nh.iph);
 562        }
 563}
 564
 565int ip_options_rcv_srr(struct sk_buff *skb)
 566{
 567        struct ip_options *opt = &(IPCB(skb)->opt);
 568        int srrspace, srrptr;
 569        u32 nexthop;
 570        struct iphdr *iph = skb->nh.iph;
 571        unsigned char * optptr = skb->nh.raw + opt->srr;
 572        struct rtable *rt = (struct rtable*)skb->dst;
 573        struct rtable *rt2;
 574        int err;
 575
 576        if (!opt->srr)
 577                return 0;
 578
 579        if (skb->pkt_type != PACKET_HOST)
 580                return -EINVAL;
 581        if (rt->rt_type == RTN_UNICAST) {
 582                if (!opt->is_strictroute)
 583                        return 0;
 584                icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl(16<<24));
 585                return -EINVAL;
 586        }
 587        if (rt->rt_type != RTN_LOCAL)
 588                return -EINVAL;
 589
 590        for (srrptr=optptr[2], srrspace = optptr[1]; srrptr <= srrspace; srrptr += 4) {
 591                if (srrptr + 3 > srrspace) {
 592                        icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((opt->srr+2)<<24));
 593                        return -EINVAL;
 594                }
 595                memcpy(&nexthop, &optptr[srrptr-1], 4);
 596
 597                rt = (struct rtable*)skb->dst;
 598                skb->dst = NULL;
 599                err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, skb->dev);
 600                rt2 = (struct rtable*)skb->dst;
 601                if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) {
 602                        ip_rt_put(rt2);
 603                        skb->dst = &rt->u.dst;
 604                        return -EINVAL;
 605                }
 606                ip_rt_put(rt);
 607                if (rt2->rt_type != RTN_LOCAL)
 608                        break;
 609                /* Superfast 8) loopback forward */
 610                memcpy(&iph->daddr, &optptr[srrptr-1], 4);
 611                opt->is_changed = 1;
 612        }
 613        if (srrptr <= srrspace) {
 614                opt->srr_is_hit = 1;
 615                opt->is_changed = 1;
 616        }
 617        return 0;
 618}
 619
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.