11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * The options processing module for ip.c 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Version: $Id: ip_options.c,v 1.21 2001/09/01 00:31:50 davem Exp $ 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * Authors: A.N.Kuznetsov 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 144fc268d2SRandy Dunlap #include <linux/capability.h> 151da177e4SLinus Torvalds #include <linux/module.h> 161da177e4SLinus Torvalds #include <linux/types.h> 171da177e4SLinus Torvalds #include <asm/uaccess.h> 181da177e4SLinus Torvalds #include <linux/skbuff.h> 191da177e4SLinus Torvalds #include <linux/ip.h> 201da177e4SLinus Torvalds #include <linux/icmp.h> 211da177e4SLinus Torvalds #include <linux/netdevice.h> 221da177e4SLinus Torvalds #include <linux/rtnetlink.h> 231da177e4SLinus Torvalds #include <net/sock.h> 241da177e4SLinus Torvalds #include <net/ip.h> 251da177e4SLinus Torvalds #include <net/icmp.h> 2614c85021SArnaldo Carvalho de Melo #include <net/route.h> 2711a03f78SPaul Moore #include <net/cipso_ipv4.h> 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds /* 301da177e4SLinus Torvalds * Write options to IP header, record destination address to 311da177e4SLinus Torvalds * source route option, address of outgoing interface 321da177e4SLinus Torvalds * (we should already know it, so that this function is allowed be 331da177e4SLinus Torvalds * called only after routing decision) and timestamp, 341da177e4SLinus Torvalds * if we originate this datagram. 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * daddr is real destination address, next hop is recorded in IP header. 371da177e4SLinus Torvalds * saddr is address of outgoing interface. 381da177e4SLinus Torvalds */ 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds void ip_options_build(struct sk_buff * skb, struct ip_options * opt, 418712f774SAl Viro __be32 daddr, struct rtable *rt, int is_frag) 421da177e4SLinus Torvalds { 43d56f90a7SArnaldo Carvalho de Melo unsigned char *iph = skb_network_header(skb); 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds memcpy(&(IPCB(skb)->opt), opt, sizeof(struct ip_options)); 461da177e4SLinus Torvalds memcpy(iph+sizeof(struct iphdr), opt->__data, opt->optlen); 471da177e4SLinus Torvalds opt = &(IPCB(skb)->opt); 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds if (opt->srr) 501da177e4SLinus Torvalds memcpy(iph+opt->srr+iph[opt->srr+1]-4, &daddr, 4); 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds if (!is_frag) { 531da177e4SLinus Torvalds if (opt->rr_needaddr) 541da177e4SLinus Torvalds ip_rt_get_source(iph+opt->rr+iph[opt->rr+2]-5, rt); 551da177e4SLinus Torvalds if (opt->ts_needaddr) 561da177e4SLinus Torvalds ip_rt_get_source(iph+opt->ts+iph[opt->ts+2]-9, rt); 571da177e4SLinus Torvalds if (opt->ts_needtime) { 581da177e4SLinus Torvalds struct timeval tv; 59e25d2ca6SAl Viro __be32 midtime; 601da177e4SLinus Torvalds do_gettimeofday(&tv); 611da177e4SLinus Torvalds midtime = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000); 621da177e4SLinus Torvalds memcpy(iph+opt->ts+iph[opt->ts+2]-5, &midtime, 4); 631da177e4SLinus Torvalds } 641da177e4SLinus Torvalds return; 651da177e4SLinus Torvalds } 661da177e4SLinus Torvalds if (opt->rr) { 671da177e4SLinus Torvalds memset(iph+opt->rr, IPOPT_NOP, iph[opt->rr+1]); 681da177e4SLinus Torvalds opt->rr = 0; 691da177e4SLinus Torvalds opt->rr_needaddr = 0; 701da177e4SLinus Torvalds } 711da177e4SLinus Torvalds if (opt->ts) { 721da177e4SLinus Torvalds memset(iph+opt->ts, IPOPT_NOP, iph[opt->ts+1]); 731da177e4SLinus Torvalds opt->ts = 0; 741da177e4SLinus Torvalds opt->ts_needaddr = opt->ts_needtime = 0; 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds /* 791da177e4SLinus Torvalds * Provided (sopt, skb) points to received options, 801da177e4SLinus Torvalds * build in dopt compiled option set appropriate for answering. 811da177e4SLinus Torvalds * i.e. invert SRR option, copy anothers, 821da177e4SLinus Torvalds * and grab room in RR/TS options. 831da177e4SLinus Torvalds * 841da177e4SLinus Torvalds * NOTE: dopt cannot point to skb. 851da177e4SLinus Torvalds */ 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds int ip_options_echo(struct ip_options * dopt, struct sk_buff * skb) 881da177e4SLinus Torvalds { 891da177e4SLinus Torvalds struct ip_options *sopt; 901da177e4SLinus Torvalds unsigned char *sptr, *dptr; 911da177e4SLinus Torvalds int soffset, doffset; 921da177e4SLinus Torvalds int optlen; 93e25d2ca6SAl Viro __be32 daddr; 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds memset(dopt, 0, sizeof(struct ip_options)); 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds sopt = &(IPCB(skb)->opt); 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds if (sopt->optlen == 0) { 1001da177e4SLinus Torvalds dopt->optlen = 0; 1011da177e4SLinus Torvalds return 0; 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 104d56f90a7SArnaldo Carvalho de Melo sptr = skb_network_header(skb); 1051da177e4SLinus Torvalds dptr = dopt->__data; 1061da177e4SLinus Torvalds 107ee6b9673SEric Dumazet daddr = skb->rtable->rt_spec_dst; 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds if (sopt->rr) { 1101da177e4SLinus Torvalds optlen = sptr[sopt->rr+1]; 1111da177e4SLinus Torvalds soffset = sptr[sopt->rr+2]; 1121da177e4SLinus Torvalds dopt->rr = dopt->optlen + sizeof(struct iphdr); 1131da177e4SLinus Torvalds memcpy(dptr, sptr+sopt->rr, optlen); 1141da177e4SLinus Torvalds if (sopt->rr_needaddr && soffset <= optlen) { 1151da177e4SLinus Torvalds if (soffset + 3 > optlen) 1161da177e4SLinus Torvalds return -EINVAL; 1171da177e4SLinus Torvalds dptr[2] = soffset + 4; 1181da177e4SLinus Torvalds dopt->rr_needaddr = 1; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds dptr += optlen; 1211da177e4SLinus Torvalds dopt->optlen += optlen; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds if (sopt->ts) { 1241da177e4SLinus Torvalds optlen = sptr[sopt->ts+1]; 1251da177e4SLinus Torvalds soffset = sptr[sopt->ts+2]; 1261da177e4SLinus Torvalds dopt->ts = dopt->optlen + sizeof(struct iphdr); 1271da177e4SLinus Torvalds memcpy(dptr, sptr+sopt->ts, optlen); 1281da177e4SLinus Torvalds if (soffset <= optlen) { 1291da177e4SLinus Torvalds if (sopt->ts_needaddr) { 1301da177e4SLinus Torvalds if (soffset + 3 > optlen) 1311da177e4SLinus Torvalds return -EINVAL; 1321da177e4SLinus Torvalds dopt->ts_needaddr = 1; 1331da177e4SLinus Torvalds soffset += 4; 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds if (sopt->ts_needtime) { 1361da177e4SLinus Torvalds if (soffset + 3 > optlen) 1371da177e4SLinus Torvalds return -EINVAL; 1381da177e4SLinus Torvalds if ((dptr[3]&0xF) != IPOPT_TS_PRESPEC) { 1391da177e4SLinus Torvalds dopt->ts_needtime = 1; 1401da177e4SLinus Torvalds soffset += 4; 1411da177e4SLinus Torvalds } else { 1421da177e4SLinus Torvalds dopt->ts_needtime = 0; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds if (soffset + 8 <= optlen) { 145fd683222SAl Viro __be32 addr; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds memcpy(&addr, sptr+soffset-1, 4); 1486b175b26SEric W. Biederman if (inet_addr_type(&init_net, addr) != RTN_LOCAL) { 1491da177e4SLinus Torvalds dopt->ts_needtime = 1; 1501da177e4SLinus Torvalds soffset += 8; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds dptr[2] = soffset; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds dptr += optlen; 1581da177e4SLinus Torvalds dopt->optlen += optlen; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds if (sopt->srr) { 1611da177e4SLinus Torvalds unsigned char * start = sptr+sopt->srr; 1623ca3c68eSAl Viro __be32 faddr; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds optlen = start[1]; 1651da177e4SLinus Torvalds soffset = start[2]; 1661da177e4SLinus Torvalds doffset = 0; 1671da177e4SLinus Torvalds if (soffset > optlen) 1681da177e4SLinus Torvalds soffset = optlen + 1; 1691da177e4SLinus Torvalds soffset -= 4; 1701da177e4SLinus Torvalds if (soffset > 3) { 1711da177e4SLinus Torvalds memcpy(&faddr, &start[soffset-1], 4); 1721da177e4SLinus Torvalds for (soffset-=4, doffset=4; soffset > 3; soffset-=4, doffset+=4) 1731da177e4SLinus Torvalds memcpy(&dptr[doffset-1], &start[soffset-1], 4); 1741da177e4SLinus Torvalds /* 1751da177e4SLinus Torvalds * RFC1812 requires to fix illegal source routes. 1761da177e4SLinus Torvalds */ 177eddc9ec5SArnaldo Carvalho de Melo if (memcmp(&ip_hdr(skb)->saddr, 178eddc9ec5SArnaldo Carvalho de Melo &start[soffset + 3], 4) == 0) 1791da177e4SLinus Torvalds doffset -= 4; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds if (doffset > 3) { 1821da177e4SLinus Torvalds memcpy(&start[doffset-1], &daddr, 4); 1831da177e4SLinus Torvalds dopt->faddr = faddr; 1841da177e4SLinus Torvalds dptr[0] = start[0]; 1851da177e4SLinus Torvalds dptr[1] = doffset+3; 1861da177e4SLinus Torvalds dptr[2] = 4; 1871da177e4SLinus Torvalds dptr += doffset+3; 1881da177e4SLinus Torvalds dopt->srr = dopt->optlen + sizeof(struct iphdr); 1891da177e4SLinus Torvalds dopt->optlen += doffset+3; 1901da177e4SLinus Torvalds dopt->is_strictroute = sopt->is_strictroute; 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds } 19311a03f78SPaul Moore if (sopt->cipso) { 19411a03f78SPaul Moore optlen = sptr[sopt->cipso+1]; 19511a03f78SPaul Moore dopt->cipso = dopt->optlen+sizeof(struct iphdr); 19611a03f78SPaul Moore memcpy(dptr, sptr+sopt->cipso, optlen); 19711a03f78SPaul Moore dptr += optlen; 19811a03f78SPaul Moore dopt->optlen += optlen; 19911a03f78SPaul Moore } 2001da177e4SLinus Torvalds while (dopt->optlen & 3) { 2011da177e4SLinus Torvalds *dptr++ = IPOPT_END; 2021da177e4SLinus Torvalds dopt->optlen++; 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds return 0; 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds /* 2081da177e4SLinus Torvalds * Options "fragmenting", just fill options not 2091da177e4SLinus Torvalds * allowed in fragments with NOOPs. 2101da177e4SLinus Torvalds * Simple and stupid 8), but the most efficient way. 2111da177e4SLinus Torvalds */ 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds void ip_options_fragment(struct sk_buff * skb) 2141da177e4SLinus Torvalds { 215d56f90a7SArnaldo Carvalho de Melo unsigned char *optptr = skb_network_header(skb) + sizeof(struct iphdr); 2161da177e4SLinus Torvalds struct ip_options * opt = &(IPCB(skb)->opt); 2171da177e4SLinus Torvalds int l = opt->optlen; 2181da177e4SLinus Torvalds int optlen; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds while (l > 0) { 2211da177e4SLinus Torvalds switch (*optptr) { 2221da177e4SLinus Torvalds case IPOPT_END: 2231da177e4SLinus Torvalds return; 2241da177e4SLinus Torvalds case IPOPT_NOOP: 2251da177e4SLinus Torvalds l--; 2261da177e4SLinus Torvalds optptr++; 2271da177e4SLinus Torvalds continue; 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds optlen = optptr[1]; 2301da177e4SLinus Torvalds if (optlen<2 || optlen>l) 2311da177e4SLinus Torvalds return; 2321da177e4SLinus Torvalds if (!IPOPT_COPIED(*optptr)) 2331da177e4SLinus Torvalds memset(optptr, IPOPT_NOOP, optlen); 2341da177e4SLinus Torvalds l -= optlen; 2351da177e4SLinus Torvalds optptr += optlen; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds opt->ts = 0; 2381da177e4SLinus Torvalds opt->rr = 0; 2391da177e4SLinus Torvalds opt->rr_needaddr = 0; 2401da177e4SLinus Torvalds opt->ts_needaddr = 0; 2411da177e4SLinus Torvalds opt->ts_needtime = 0; 2421da177e4SLinus Torvalds return; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds /* 2461da177e4SLinus Torvalds * Verify options and fill pointers in struct options. 2471da177e4SLinus Torvalds * Caller should clear *opt, and set opt->data. 2481da177e4SLinus Torvalds * If opt == NULL, then skb->data should point to IP header. 2491da177e4SLinus Torvalds */ 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds int ip_options_compile(struct ip_options * opt, struct sk_buff * skb) 2521da177e4SLinus Torvalds { 2531da177e4SLinus Torvalds int l; 2541da177e4SLinus Torvalds unsigned char * iph; 2551da177e4SLinus Torvalds unsigned char * optptr; 2561da177e4SLinus Torvalds int optlen; 2571da177e4SLinus Torvalds unsigned char * pp_ptr = NULL; 258*22aba383SDenis V. Lunev struct rtable *rt = NULL; 2591da177e4SLinus Torvalds 260*22aba383SDenis V. Lunev if (skb != NULL) { 261*22aba383SDenis V. Lunev rt = skb->rtable; 262*22aba383SDenis V. Lunev optptr = (unsigned char *)&(ip_hdr(skb)[1]); 263*22aba383SDenis V. Lunev } else 26410fe7d85SDenis V. Lunev optptr = opt->__data; 2651da177e4SLinus Torvalds iph = optptr - sizeof(struct iphdr); 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds for (l = opt->optlen; l > 0; ) { 2681da177e4SLinus Torvalds switch (*optptr) { 2691da177e4SLinus Torvalds case IPOPT_END: 2701da177e4SLinus Torvalds for (optptr++, l--; l>0; optptr++, l--) { 2711da177e4SLinus Torvalds if (*optptr != IPOPT_END) { 2721da177e4SLinus Torvalds *optptr = IPOPT_END; 2731da177e4SLinus Torvalds opt->is_changed = 1; 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds goto eol; 2771da177e4SLinus Torvalds case IPOPT_NOOP: 2781da177e4SLinus Torvalds l--; 2791da177e4SLinus Torvalds optptr++; 2801da177e4SLinus Torvalds continue; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds optlen = optptr[1]; 2831da177e4SLinus Torvalds if (optlen<2 || optlen>l) { 2841da177e4SLinus Torvalds pp_ptr = optptr; 2851da177e4SLinus Torvalds goto error; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds switch (*optptr) { 2881da177e4SLinus Torvalds case IPOPT_SSRR: 2891da177e4SLinus Torvalds case IPOPT_LSRR: 2901da177e4SLinus Torvalds if (optlen < 3) { 2911da177e4SLinus Torvalds pp_ptr = optptr + 1; 2921da177e4SLinus Torvalds goto error; 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds if (optptr[2] < 4) { 2951da177e4SLinus Torvalds pp_ptr = optptr + 2; 2961da177e4SLinus Torvalds goto error; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds /* NB: cf RFC-1812 5.2.4.1 */ 2991da177e4SLinus Torvalds if (opt->srr) { 3001da177e4SLinus Torvalds pp_ptr = optptr; 3011da177e4SLinus Torvalds goto error; 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds if (!skb) { 3041da177e4SLinus Torvalds if (optptr[2] != 4 || optlen < 7 || ((optlen-3) & 3)) { 3051da177e4SLinus Torvalds pp_ptr = optptr + 1; 3061da177e4SLinus Torvalds goto error; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds memcpy(&opt->faddr, &optptr[3], 4); 3091da177e4SLinus Torvalds if (optlen > 7) 3101da177e4SLinus Torvalds memmove(&optptr[3], &optptr[7], optlen-7); 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds opt->is_strictroute = (optptr[0] == IPOPT_SSRR); 3131da177e4SLinus Torvalds opt->srr = optptr - iph; 3141da177e4SLinus Torvalds break; 3151da177e4SLinus Torvalds case IPOPT_RR: 3161da177e4SLinus Torvalds if (opt->rr) { 3171da177e4SLinus Torvalds pp_ptr = optptr; 3181da177e4SLinus Torvalds goto error; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds if (optlen < 3) { 3211da177e4SLinus Torvalds pp_ptr = optptr + 1; 3221da177e4SLinus Torvalds goto error; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds if (optptr[2] < 4) { 3251da177e4SLinus Torvalds pp_ptr = optptr + 2; 3261da177e4SLinus Torvalds goto error; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds if (optptr[2] <= optlen) { 3291da177e4SLinus Torvalds if (optptr[2]+3 > optlen) { 3301da177e4SLinus Torvalds pp_ptr = optptr + 2; 3311da177e4SLinus Torvalds goto error; 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds if (skb) { 3341da177e4SLinus Torvalds memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4); 3351da177e4SLinus Torvalds opt->is_changed = 1; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds optptr[2] += 4; 3381da177e4SLinus Torvalds opt->rr_needaddr = 1; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds opt->rr = optptr - iph; 3411da177e4SLinus Torvalds break; 3421da177e4SLinus Torvalds case IPOPT_TIMESTAMP: 3431da177e4SLinus Torvalds if (opt->ts) { 3441da177e4SLinus Torvalds pp_ptr = optptr; 3451da177e4SLinus Torvalds goto error; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds if (optlen < 4) { 3481da177e4SLinus Torvalds pp_ptr = optptr + 1; 3491da177e4SLinus Torvalds goto error; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds if (optptr[2] < 5) { 3521da177e4SLinus Torvalds pp_ptr = optptr + 2; 3531da177e4SLinus Torvalds goto error; 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds if (optptr[2] <= optlen) { 356e25d2ca6SAl Viro __be32 *timeptr = NULL; 3571da177e4SLinus Torvalds if (optptr[2]+3 > optptr[1]) { 3581da177e4SLinus Torvalds pp_ptr = optptr + 2; 3591da177e4SLinus Torvalds goto error; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds switch (optptr[3]&0xF) { 3621da177e4SLinus Torvalds case IPOPT_TS_TSONLY: 3631da177e4SLinus Torvalds opt->ts = optptr - iph; 3641da177e4SLinus Torvalds if (skb) 365e25d2ca6SAl Viro timeptr = (__be32*)&optptr[optptr[2]-1]; 3661da177e4SLinus Torvalds opt->ts_needtime = 1; 3671da177e4SLinus Torvalds optptr[2] += 4; 3681da177e4SLinus Torvalds break; 3691da177e4SLinus Torvalds case IPOPT_TS_TSANDADDR: 3701da177e4SLinus Torvalds if (optptr[2]+7 > optptr[1]) { 3711da177e4SLinus Torvalds pp_ptr = optptr + 2; 3721da177e4SLinus Torvalds goto error; 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds opt->ts = optptr - iph; 3751da177e4SLinus Torvalds if (skb) { 3761da177e4SLinus Torvalds memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4); 377e25d2ca6SAl Viro timeptr = (__be32*)&optptr[optptr[2]+3]; 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds opt->ts_needaddr = 1; 3801da177e4SLinus Torvalds opt->ts_needtime = 1; 3811da177e4SLinus Torvalds optptr[2] += 8; 3821da177e4SLinus Torvalds break; 3831da177e4SLinus Torvalds case IPOPT_TS_PRESPEC: 3841da177e4SLinus Torvalds if (optptr[2]+7 > optptr[1]) { 3851da177e4SLinus Torvalds pp_ptr = optptr + 2; 3861da177e4SLinus Torvalds goto error; 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds opt->ts = optptr - iph; 3891da177e4SLinus Torvalds { 390fd683222SAl Viro __be32 addr; 3911da177e4SLinus Torvalds memcpy(&addr, &optptr[optptr[2]-1], 4); 3926b175b26SEric W. Biederman if (inet_addr_type(&init_net, addr) == RTN_UNICAST) 3931da177e4SLinus Torvalds break; 3941da177e4SLinus Torvalds if (skb) 395e25d2ca6SAl Viro timeptr = (__be32*)&optptr[optptr[2]+3]; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds opt->ts_needtime = 1; 3981da177e4SLinus Torvalds optptr[2] += 8; 3991da177e4SLinus Torvalds break; 4001da177e4SLinus Torvalds default: 4011da177e4SLinus Torvalds if (!skb && !capable(CAP_NET_RAW)) { 4021da177e4SLinus Torvalds pp_ptr = optptr + 3; 4031da177e4SLinus Torvalds goto error; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds break; 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds if (timeptr) { 4081da177e4SLinus Torvalds struct timeval tv; 409e25d2ca6SAl Viro __be32 midtime; 4101da177e4SLinus Torvalds do_gettimeofday(&tv); 4111da177e4SLinus Torvalds midtime = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000); 412e25d2ca6SAl Viro memcpy(timeptr, &midtime, sizeof(__be32)); 4131da177e4SLinus Torvalds opt->is_changed = 1; 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds } else { 4161da177e4SLinus Torvalds unsigned overflow = optptr[3]>>4; 4171da177e4SLinus Torvalds if (overflow == 15) { 4181da177e4SLinus Torvalds pp_ptr = optptr + 3; 4191da177e4SLinus Torvalds goto error; 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds opt->ts = optptr - iph; 4221da177e4SLinus Torvalds if (skb) { 4231da177e4SLinus Torvalds optptr[3] = (optptr[3]&0xF)|((overflow+1)<<4); 4241da177e4SLinus Torvalds opt->is_changed = 1; 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds break; 4281da177e4SLinus Torvalds case IPOPT_RA: 4291da177e4SLinus Torvalds if (optlen < 4) { 4301da177e4SLinus Torvalds pp_ptr = optptr + 1; 4311da177e4SLinus Torvalds goto error; 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds if (optptr[2] == 0 && optptr[3] == 0) 4341da177e4SLinus Torvalds opt->router_alert = optptr - iph; 4351da177e4SLinus Torvalds break; 43611a03f78SPaul Moore case IPOPT_CIPSO: 437f8687afeSPaul Moore if ((!skb && !capable(CAP_NET_RAW)) || opt->cipso) { 43811a03f78SPaul Moore pp_ptr = optptr; 43911a03f78SPaul Moore goto error; 44011a03f78SPaul Moore } 44111a03f78SPaul Moore opt->cipso = optptr - iph; 44211a03f78SPaul Moore if (cipso_v4_validate(&optptr)) { 44311a03f78SPaul Moore pp_ptr = optptr; 44411a03f78SPaul Moore goto error; 44511a03f78SPaul Moore } 44611a03f78SPaul Moore break; 4471da177e4SLinus Torvalds case IPOPT_SEC: 4481da177e4SLinus Torvalds case IPOPT_SID: 4491da177e4SLinus Torvalds default: 4501da177e4SLinus Torvalds if (!skb && !capable(CAP_NET_RAW)) { 4511da177e4SLinus Torvalds pp_ptr = optptr; 4521da177e4SLinus Torvalds goto error; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds break; 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds l -= optlen; 4571da177e4SLinus Torvalds optptr += optlen; 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds eol: 4611da177e4SLinus Torvalds if (!pp_ptr) 4621da177e4SLinus Torvalds return 0; 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds error: 4651da177e4SLinus Torvalds if (skb) { 4661da177e4SLinus Torvalds icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((pp_ptr-iph)<<24)); 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds return -EINVAL; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds /* 4731da177e4SLinus Torvalds * Undo all the changes done by ip_options_compile(). 4741da177e4SLinus Torvalds */ 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds void ip_options_undo(struct ip_options * opt) 4771da177e4SLinus Torvalds { 4781da177e4SLinus Torvalds if (opt->srr) { 4791da177e4SLinus Torvalds unsigned char * optptr = opt->__data+opt->srr-sizeof(struct iphdr); 4801da177e4SLinus Torvalds memmove(optptr+7, optptr+3, optptr[1]-7); 4811da177e4SLinus Torvalds memcpy(optptr+3, &opt->faddr, 4); 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds if (opt->rr_needaddr) { 4841da177e4SLinus Torvalds unsigned char * optptr = opt->__data+opt->rr-sizeof(struct iphdr); 4851da177e4SLinus Torvalds optptr[2] -= 4; 4861da177e4SLinus Torvalds memset(&optptr[optptr[2]-1], 0, 4); 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds if (opt->ts) { 4891da177e4SLinus Torvalds unsigned char * optptr = opt->__data+opt->ts-sizeof(struct iphdr); 4901da177e4SLinus Torvalds if (opt->ts_needtime) { 4911da177e4SLinus Torvalds optptr[2] -= 4; 4921da177e4SLinus Torvalds memset(&optptr[optptr[2]-1], 0, 4); 4931da177e4SLinus Torvalds if ((optptr[3]&0xF) == IPOPT_TS_PRESPEC) 4941da177e4SLinus Torvalds optptr[2] -= 4; 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds if (opt->ts_needaddr) { 4971da177e4SLinus Torvalds optptr[2] -= 4; 4981da177e4SLinus Torvalds memset(&optptr[optptr[2]-1], 0, 4); 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds 5034c6ea29dSArnaldo Carvalho de Melo static struct ip_options *ip_options_get_alloc(const int optlen) 5041da177e4SLinus Torvalds { 50537640703SMariusz Kozlowski return kzalloc(sizeof(struct ip_options) + ((optlen + 3) & ~3), 5064c6ea29dSArnaldo Carvalho de Melo GFP_KERNEL); 5074c6ea29dSArnaldo Carvalho de Melo } 5081da177e4SLinus Torvalds 5094c6ea29dSArnaldo Carvalho de Melo static int ip_options_get_finish(struct ip_options **optp, 5104c6ea29dSArnaldo Carvalho de Melo struct ip_options *opt, int optlen) 5114c6ea29dSArnaldo Carvalho de Melo { 5121da177e4SLinus Torvalds while (optlen & 3) 5131da177e4SLinus Torvalds opt->__data[optlen++] = IPOPT_END; 5141da177e4SLinus Torvalds opt->optlen = optlen; 5151da177e4SLinus Torvalds if (optlen && ip_options_compile(opt, NULL)) { 5161da177e4SLinus Torvalds kfree(opt); 5171da177e4SLinus Torvalds return -EINVAL; 5181da177e4SLinus Torvalds } 5191da177e4SLinus Torvalds kfree(*optp); 5201da177e4SLinus Torvalds *optp = opt; 5211da177e4SLinus Torvalds return 0; 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds 5244c6ea29dSArnaldo Carvalho de Melo int ip_options_get_from_user(struct ip_options **optp, unsigned char __user *data, int optlen) 5254c6ea29dSArnaldo Carvalho de Melo { 5264c6ea29dSArnaldo Carvalho de Melo struct ip_options *opt = ip_options_get_alloc(optlen); 5274c6ea29dSArnaldo Carvalho de Melo 5284c6ea29dSArnaldo Carvalho de Melo if (!opt) 5294c6ea29dSArnaldo Carvalho de Melo return -ENOMEM; 5304c6ea29dSArnaldo Carvalho de Melo if (optlen && copy_from_user(opt->__data, data, optlen)) { 5314c6ea29dSArnaldo Carvalho de Melo kfree(opt); 5324c6ea29dSArnaldo Carvalho de Melo return -EFAULT; 5334c6ea29dSArnaldo Carvalho de Melo } 5344c6ea29dSArnaldo Carvalho de Melo return ip_options_get_finish(optp, opt, optlen); 5354c6ea29dSArnaldo Carvalho de Melo } 5364c6ea29dSArnaldo Carvalho de Melo 5374c6ea29dSArnaldo Carvalho de Melo int ip_options_get(struct ip_options **optp, unsigned char *data, int optlen) 5384c6ea29dSArnaldo Carvalho de Melo { 5394c6ea29dSArnaldo Carvalho de Melo struct ip_options *opt = ip_options_get_alloc(optlen); 5404c6ea29dSArnaldo Carvalho de Melo 5414c6ea29dSArnaldo Carvalho de Melo if (!opt) 5424c6ea29dSArnaldo Carvalho de Melo return -ENOMEM; 5434c6ea29dSArnaldo Carvalho de Melo if (optlen) 5444c6ea29dSArnaldo Carvalho de Melo memcpy(opt->__data, data, optlen); 5454c6ea29dSArnaldo Carvalho de Melo return ip_options_get_finish(optp, opt, optlen); 5464c6ea29dSArnaldo Carvalho de Melo } 5474c6ea29dSArnaldo Carvalho de Melo 5481da177e4SLinus Torvalds void ip_forward_options(struct sk_buff *skb) 5491da177e4SLinus Torvalds { 5501da177e4SLinus Torvalds struct ip_options * opt = &(IPCB(skb)->opt); 5511da177e4SLinus Torvalds unsigned char * optptr; 552ee6b9673SEric Dumazet struct rtable *rt = skb->rtable; 553d56f90a7SArnaldo Carvalho de Melo unsigned char *raw = skb_network_header(skb); 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds if (opt->rr_needaddr) { 5561da177e4SLinus Torvalds optptr = (unsigned char *)raw + opt->rr; 5571da177e4SLinus Torvalds ip_rt_get_source(&optptr[optptr[2]-5], rt); 5581da177e4SLinus Torvalds opt->is_changed = 1; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds if (opt->srr_is_hit) { 5611da177e4SLinus Torvalds int srrptr, srrspace; 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds optptr = raw + opt->srr; 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds for ( srrptr=optptr[2], srrspace = optptr[1]; 5661da177e4SLinus Torvalds srrptr <= srrspace; 5671da177e4SLinus Torvalds srrptr += 4 5681da177e4SLinus Torvalds ) { 5691da177e4SLinus Torvalds if (srrptr + 3 > srrspace) 5701da177e4SLinus Torvalds break; 5711da177e4SLinus Torvalds if (memcmp(&rt->rt_dst, &optptr[srrptr-1], 4) == 0) 5721da177e4SLinus Torvalds break; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds if (srrptr + 3 <= srrspace) { 5751da177e4SLinus Torvalds opt->is_changed = 1; 5761da177e4SLinus Torvalds ip_rt_get_source(&optptr[srrptr-1], rt); 577eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->daddr = rt->rt_dst; 5781da177e4SLinus Torvalds optptr[2] = srrptr+4; 5791da177e4SLinus Torvalds } else if (net_ratelimit()) 5801da177e4SLinus Torvalds printk(KERN_CRIT "ip_forward(): Argh! Destination lost!\n"); 5811da177e4SLinus Torvalds if (opt->ts_needaddr) { 5821da177e4SLinus Torvalds optptr = raw + opt->ts; 5831da177e4SLinus Torvalds ip_rt_get_source(&optptr[optptr[2]-9], rt); 5841da177e4SLinus Torvalds opt->is_changed = 1; 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds if (opt->is_changed) { 5881da177e4SLinus Torvalds opt->is_changed = 0; 589eddc9ec5SArnaldo Carvalho de Melo ip_send_check(ip_hdr(skb)); 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds int ip_options_rcv_srr(struct sk_buff *skb) 5941da177e4SLinus Torvalds { 5951da177e4SLinus Torvalds struct ip_options *opt = &(IPCB(skb)->opt); 5961da177e4SLinus Torvalds int srrspace, srrptr; 5979e12bb22SAl Viro __be32 nexthop; 598eddc9ec5SArnaldo Carvalho de Melo struct iphdr *iph = ip_hdr(skb); 599d56f90a7SArnaldo Carvalho de Melo unsigned char *optptr = skb_network_header(skb) + opt->srr; 600ee6b9673SEric Dumazet struct rtable *rt = skb->rtable; 6011da177e4SLinus Torvalds struct rtable *rt2; 6021da177e4SLinus Torvalds int err; 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds if (!opt->srr) 6051da177e4SLinus Torvalds return 0; 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds if (skb->pkt_type != PACKET_HOST) 6081da177e4SLinus Torvalds return -EINVAL; 6091da177e4SLinus Torvalds if (rt->rt_type == RTN_UNICAST) { 6101da177e4SLinus Torvalds if (!opt->is_strictroute) 6111da177e4SLinus Torvalds return 0; 6121da177e4SLinus Torvalds icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl(16<<24)); 6131da177e4SLinus Torvalds return -EINVAL; 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds if (rt->rt_type != RTN_LOCAL) 6161da177e4SLinus Torvalds return -EINVAL; 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds for (srrptr=optptr[2], srrspace = optptr[1]; srrptr <= srrspace; srrptr += 4) { 6191da177e4SLinus Torvalds if (srrptr + 3 > srrspace) { 6201da177e4SLinus Torvalds icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((opt->srr+2)<<24)); 6211da177e4SLinus Torvalds return -EINVAL; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds memcpy(&nexthop, &optptr[srrptr-1], 4); 6241da177e4SLinus Torvalds 625ee6b9673SEric Dumazet rt = skb->rtable; 626ee6b9673SEric Dumazet skb->rtable = NULL; 6271da177e4SLinus Torvalds err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, skb->dev); 628ee6b9673SEric Dumazet rt2 = skb->rtable; 6291da177e4SLinus Torvalds if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) { 6301da177e4SLinus Torvalds ip_rt_put(rt2); 631ee6b9673SEric Dumazet skb->rtable = rt; 6321da177e4SLinus Torvalds return -EINVAL; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds ip_rt_put(rt); 6351da177e4SLinus Torvalds if (rt2->rt_type != RTN_LOCAL) 6361da177e4SLinus Torvalds break; 6371da177e4SLinus Torvalds /* Superfast 8) loopback forward */ 6381da177e4SLinus Torvalds memcpy(&iph->daddr, &optptr[srrptr-1], 4); 6391da177e4SLinus Torvalds opt->is_changed = 1; 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds if (srrptr <= srrspace) { 6421da177e4SLinus Torvalds opt->srr_is_hit = 1; 6431da177e4SLinus Torvalds opt->is_changed = 1; 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds return 0; 6461da177e4SLinus Torvalds } 647