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 * Authors: A.N.Kuznetsov 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 12afd46503SJoe Perches #define pr_fmt(fmt) "IPv4: " fmt 13afd46503SJoe Perches 144fc268d2SRandy Dunlap #include <linux/capability.h> 151da177e4SLinus Torvalds #include <linux/module.h> 165a0e3ad6STejun Heo #include <linux/slab.h> 171da177e4SLinus Torvalds #include <linux/types.h> 181da177e4SLinus Torvalds #include <asm/uaccess.h> 1948bdf072SChris Metcalf #include <asm/unaligned.h> 201da177e4SLinus Torvalds #include <linux/skbuff.h> 211da177e4SLinus Torvalds #include <linux/ip.h> 221da177e4SLinus Torvalds #include <linux/icmp.h> 231da177e4SLinus Torvalds #include <linux/netdevice.h> 241da177e4SLinus Torvalds #include <linux/rtnetlink.h> 251da177e4SLinus Torvalds #include <net/sock.h> 261da177e4SLinus Torvalds #include <net/ip.h> 271da177e4SLinus Torvalds #include <net/icmp.h> 2814c85021SArnaldo Carvalho de Melo #include <net/route.h> 2911a03f78SPaul Moore #include <net/cipso_ipv4.h> 3035ebf65eSDavid S. Miller #include <net/ip_fib.h> 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds /* 331da177e4SLinus Torvalds * Write options to IP header, record destination address to 341da177e4SLinus Torvalds * source route option, address of outgoing interface 351da177e4SLinus Torvalds * (we should already know it, so that this function is allowed be 361da177e4SLinus Torvalds * called only after routing decision) and timestamp, 371da177e4SLinus Torvalds * if we originate this datagram. 381da177e4SLinus Torvalds * 391da177e4SLinus Torvalds * daddr is real destination address, next hop is recorded in IP header. 401da177e4SLinus Torvalds * saddr is address of outgoing interface. 411da177e4SLinus Torvalds */ 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds void ip_options_build(struct sk_buff *skb, struct ip_options *opt, 448712f774SAl Viro __be32 daddr, struct rtable *rt, int is_frag) 451da177e4SLinus Torvalds { 46d56f90a7SArnaldo Carvalho de Melo unsigned char *iph = skb_network_header(skb); 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds memcpy(&(IPCB(skb)->opt), opt, sizeof(struct ip_options)); 491da177e4SLinus Torvalds memcpy(iph+sizeof(struct iphdr), opt->__data, opt->optlen); 501da177e4SLinus Torvalds opt = &(IPCB(skb)->opt); 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds if (opt->srr) 531da177e4SLinus Torvalds memcpy(iph+opt->srr+iph[opt->srr+1]-4, &daddr, 4); 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds if (!is_frag) { 561da177e4SLinus Torvalds if (opt->rr_needaddr) 578e36360aSDavid S. Miller ip_rt_get_source(iph+opt->rr+iph[opt->rr+2]-5, skb, rt); 581da177e4SLinus Torvalds if (opt->ts_needaddr) 598e36360aSDavid S. Miller ip_rt_get_source(iph+opt->ts+iph[opt->ts+2]-9, skb, rt); 601da177e4SLinus Torvalds if (opt->ts_needtime) { 61f25c3d61SYOSHIFUJI Hideaki struct timespec tv; 62e25d2ca6SAl Viro __be32 midtime; 63f25c3d61SYOSHIFUJI Hideaki getnstimeofday(&tv); 64f25c3d61SYOSHIFUJI Hideaki midtime = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC + tv.tv_nsec / NSEC_PER_MSEC); 651da177e4SLinus Torvalds memcpy(iph+opt->ts+iph[opt->ts+2]-5, &midtime, 4); 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds return; 681da177e4SLinus Torvalds } 691da177e4SLinus Torvalds if (opt->rr) { 701da177e4SLinus Torvalds memset(iph+opt->rr, IPOPT_NOP, iph[opt->rr+1]); 711da177e4SLinus Torvalds opt->rr = 0; 721da177e4SLinus Torvalds opt->rr_needaddr = 0; 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds if (opt->ts) { 751da177e4SLinus Torvalds memset(iph+opt->ts, IPOPT_NOP, iph[opt->ts+1]); 761da177e4SLinus Torvalds opt->ts = 0; 771da177e4SLinus Torvalds opt->ts_needaddr = opt->ts_needtime = 0; 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /* 821da177e4SLinus Torvalds * Provided (sopt, skb) points to received options, 831da177e4SLinus Torvalds * build in dopt compiled option set appropriate for answering. 841da177e4SLinus Torvalds * i.e. invert SRR option, copy anothers, 851da177e4SLinus Torvalds * and grab room in RR/TS options. 861da177e4SLinus Torvalds * 871da177e4SLinus Torvalds * NOTE: dopt cannot point to skb. 881da177e4SLinus Torvalds */ 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds int ip_options_echo(struct ip_options *dopt, struct sk_buff *skb) 911da177e4SLinus Torvalds { 92f6d8bd05SEric Dumazet const struct ip_options *sopt; 931da177e4SLinus Torvalds unsigned char *sptr, *dptr; 941da177e4SLinus Torvalds int soffset, doffset; 951da177e4SLinus Torvalds int optlen; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds memset(dopt, 0, sizeof(struct ip_options)); 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds sopt = &(IPCB(skb)->opt); 1001da177e4SLinus Torvalds 101f6d8bd05SEric Dumazet if (sopt->optlen == 0) 1021da177e4SLinus Torvalds return 0; 1031da177e4SLinus Torvalds 104d56f90a7SArnaldo Carvalho de Melo sptr = skb_network_header(skb); 1051da177e4SLinus Torvalds dptr = dopt->__data; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds if (sopt->rr) { 1081da177e4SLinus Torvalds optlen = sptr[sopt->rr+1]; 1091da177e4SLinus Torvalds soffset = sptr[sopt->rr+2]; 1101da177e4SLinus Torvalds dopt->rr = dopt->optlen + sizeof(struct iphdr); 1111da177e4SLinus Torvalds memcpy(dptr, sptr+sopt->rr, optlen); 1121da177e4SLinus Torvalds if (sopt->rr_needaddr && soffset <= optlen) { 1131da177e4SLinus Torvalds if (soffset + 3 > optlen) 1141da177e4SLinus Torvalds return -EINVAL; 1151da177e4SLinus Torvalds dptr[2] = soffset + 4; 1161da177e4SLinus Torvalds dopt->rr_needaddr = 1; 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds dptr += optlen; 1191da177e4SLinus Torvalds dopt->optlen += optlen; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds if (sopt->ts) { 1221da177e4SLinus Torvalds optlen = sptr[sopt->ts+1]; 1231da177e4SLinus Torvalds soffset = sptr[sopt->ts+2]; 1241da177e4SLinus Torvalds dopt->ts = dopt->optlen + sizeof(struct iphdr); 1251da177e4SLinus Torvalds memcpy(dptr, sptr+sopt->ts, optlen); 1261da177e4SLinus Torvalds if (soffset <= optlen) { 1271da177e4SLinus Torvalds if (sopt->ts_needaddr) { 1281da177e4SLinus Torvalds if (soffset + 3 > optlen) 1291da177e4SLinus Torvalds return -EINVAL; 1301da177e4SLinus Torvalds dopt->ts_needaddr = 1; 1311da177e4SLinus Torvalds soffset += 4; 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds if (sopt->ts_needtime) { 1341da177e4SLinus Torvalds if (soffset + 3 > optlen) 1351da177e4SLinus Torvalds return -EINVAL; 1361da177e4SLinus Torvalds if ((dptr[3]&0xF) != IPOPT_TS_PRESPEC) { 1371da177e4SLinus Torvalds dopt->ts_needtime = 1; 1381da177e4SLinus Torvalds soffset += 4; 1391da177e4SLinus Torvalds } else { 1401da177e4SLinus Torvalds dopt->ts_needtime = 0; 1411da177e4SLinus Torvalds 1428628bd8aSJan Luebbe if (soffset + 7 <= optlen) { 143fd683222SAl Viro __be32 addr; 1441da177e4SLinus Torvalds 1458628bd8aSJan Luebbe memcpy(&addr, dptr+soffset-1, 4); 1468628bd8aSJan Luebbe if (inet_addr_type(dev_net(skb_dst(skb)->dev), addr) != RTN_UNICAST) { 1471da177e4SLinus Torvalds dopt->ts_needtime = 1; 1481da177e4SLinus Torvalds soffset += 8; 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds dptr[2] = soffset; 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds dptr += optlen; 1561da177e4SLinus Torvalds dopt->optlen += optlen; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds if (sopt->srr) { 1591da177e4SLinus Torvalds unsigned char *start = sptr+sopt->srr; 1603ca3c68eSAl Viro __be32 faddr; 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds optlen = start[1]; 1631da177e4SLinus Torvalds soffset = start[2]; 1641da177e4SLinus Torvalds doffset = 0; 1651da177e4SLinus Torvalds if (soffset > optlen) 1661da177e4SLinus Torvalds soffset = optlen + 1; 1671da177e4SLinus Torvalds soffset -= 4; 1681da177e4SLinus Torvalds if (soffset > 3) { 1691da177e4SLinus Torvalds memcpy(&faddr, &start[soffset-1], 4); 1701da177e4SLinus Torvalds for (soffset -= 4, doffset = 4; soffset > 3; soffset -= 4, doffset += 4) 1711da177e4SLinus Torvalds memcpy(&dptr[doffset-1], &start[soffset-1], 4); 1721da177e4SLinus Torvalds /* 1731da177e4SLinus Torvalds * RFC1812 requires to fix illegal source routes. 1741da177e4SLinus Torvalds */ 175eddc9ec5SArnaldo Carvalho de Melo if (memcmp(&ip_hdr(skb)->saddr, 176eddc9ec5SArnaldo Carvalho de Melo &start[soffset + 3], 4) == 0) 1771da177e4SLinus Torvalds doffset -= 4; 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds if (doffset > 3) { 1806255e5eaSJulian Anastasov __be32 daddr = fib_compute_spec_dst(skb); 1816255e5eaSJulian Anastasov 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 } 2431da177e4SLinus Torvalds 244bf5e53e3SEric Dumazet /* helper used by ip_options_compile() to call fib_compute_spec_dst() 245bf5e53e3SEric Dumazet * at most one time. 246bf5e53e3SEric Dumazet */ 247bf5e53e3SEric Dumazet static void spec_dst_fill(__be32 *spec_dst, struct sk_buff *skb) 248bf5e53e3SEric Dumazet { 249bf5e53e3SEric Dumazet if (*spec_dst == htonl(INADDR_ANY)) 250bf5e53e3SEric Dumazet *spec_dst = fib_compute_spec_dst(skb); 251bf5e53e3SEric Dumazet } 252bf5e53e3SEric Dumazet 2531da177e4SLinus Torvalds /* 2541da177e4SLinus Torvalds * Verify options and fill pointers in struct options. 2551da177e4SLinus Torvalds * Caller should clear *opt, and set opt->data. 2561da177e4SLinus Torvalds * If opt == NULL, then skb->data should point to IP header. 2571da177e4SLinus Torvalds */ 2581da177e4SLinus Torvalds 2590e6bd4a1SDenis V. Lunev int ip_options_compile(struct net *net, 2600e6bd4a1SDenis V. Lunev struct ip_options *opt, struct sk_buff *skb) 2611da177e4SLinus Torvalds { 262bf5e53e3SEric Dumazet __be32 spec_dst = htonl(INADDR_ANY); 2631da177e4SLinus Torvalds unsigned char *pp_ptr = NULL; 26411604721SDavid S. Miller struct rtable *rt = NULL; 26535ebf65eSDavid S. Miller unsigned char *optptr; 26635ebf65eSDavid S. Miller unsigned char *iph; 26735ebf65eSDavid S. Miller int optlen, l; 2681da177e4SLinus Torvalds 26922aba383SDenis V. Lunev if (skb != NULL) { 27011604721SDavid S. Miller rt = skb_rtable(skb); 27122aba383SDenis V. Lunev optptr = (unsigned char *)&(ip_hdr(skb)[1]); 27222aba383SDenis V. Lunev } else 27310fe7d85SDenis V. Lunev optptr = opt->__data; 2741da177e4SLinus Torvalds iph = optptr - sizeof(struct iphdr); 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds for (l = opt->optlen; l > 0; ) { 2771da177e4SLinus Torvalds switch (*optptr) { 2781da177e4SLinus Torvalds case IPOPT_END: 2791da177e4SLinus Torvalds for (optptr++, l--; l > 0; optptr++, l--) { 2801da177e4SLinus Torvalds if (*optptr != IPOPT_END) { 2811da177e4SLinus Torvalds *optptr = IPOPT_END; 2821da177e4SLinus Torvalds opt->is_changed = 1; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds goto eol; 2861da177e4SLinus Torvalds case IPOPT_NOOP: 2871da177e4SLinus Torvalds l--; 2881da177e4SLinus Torvalds optptr++; 2891da177e4SLinus Torvalds continue; 2901da177e4SLinus Torvalds } 291*10ec9472SEric Dumazet if (unlikely(l < 2)) { 292*10ec9472SEric Dumazet pp_ptr = optptr; 293*10ec9472SEric Dumazet goto error; 294*10ec9472SEric Dumazet } 2951da177e4SLinus Torvalds optlen = optptr[1]; 2961da177e4SLinus Torvalds if (optlen < 2 || optlen > l) { 2971da177e4SLinus Torvalds pp_ptr = optptr; 2981da177e4SLinus Torvalds goto error; 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds switch (*optptr) { 3011da177e4SLinus Torvalds case IPOPT_SSRR: 3021da177e4SLinus Torvalds case IPOPT_LSRR: 3031da177e4SLinus Torvalds if (optlen < 3) { 3041da177e4SLinus Torvalds pp_ptr = optptr + 1; 3051da177e4SLinus Torvalds goto error; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds if (optptr[2] < 4) { 3081da177e4SLinus Torvalds pp_ptr = optptr + 2; 3091da177e4SLinus Torvalds goto error; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds /* NB: cf RFC-1812 5.2.4.1 */ 3121da177e4SLinus Torvalds if (opt->srr) { 3131da177e4SLinus Torvalds pp_ptr = optptr; 3141da177e4SLinus Torvalds goto error; 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds if (!skb) { 3171da177e4SLinus Torvalds if (optptr[2] != 4 || optlen < 7 || ((optlen-3) & 3)) { 3181da177e4SLinus Torvalds pp_ptr = optptr + 1; 3191da177e4SLinus Torvalds goto error; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds memcpy(&opt->faddr, &optptr[3], 4); 3221da177e4SLinus Torvalds if (optlen > 7) 3231da177e4SLinus Torvalds memmove(&optptr[3], &optptr[7], optlen-7); 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds opt->is_strictroute = (optptr[0] == IPOPT_SSRR); 3261da177e4SLinus Torvalds opt->srr = optptr - iph; 3271da177e4SLinus Torvalds break; 3281da177e4SLinus Torvalds case IPOPT_RR: 3291da177e4SLinus Torvalds if (opt->rr) { 3301da177e4SLinus Torvalds pp_ptr = optptr; 3311da177e4SLinus Torvalds goto error; 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds if (optlen < 3) { 3341da177e4SLinus Torvalds pp_ptr = optptr + 1; 3351da177e4SLinus Torvalds goto error; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds if (optptr[2] < 4) { 3381da177e4SLinus Torvalds pp_ptr = optptr + 2; 3391da177e4SLinus Torvalds goto error; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds if (optptr[2] <= optlen) { 3421da177e4SLinus Torvalds if (optptr[2]+3 > optlen) { 3431da177e4SLinus Torvalds pp_ptr = optptr + 2; 3441da177e4SLinus Torvalds goto error; 3451da177e4SLinus Torvalds } 34611604721SDavid S. Miller if (rt) { 347bf5e53e3SEric Dumazet spec_dst_fill(&spec_dst, skb); 34835ebf65eSDavid S. Miller memcpy(&optptr[optptr[2]-1], &spec_dst, 4); 3491da177e4SLinus Torvalds opt->is_changed = 1; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds optptr[2] += 4; 3521da177e4SLinus Torvalds opt->rr_needaddr = 1; 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds opt->rr = optptr - iph; 3551da177e4SLinus Torvalds break; 3561da177e4SLinus Torvalds case IPOPT_TIMESTAMP: 3571da177e4SLinus Torvalds if (opt->ts) { 3581da177e4SLinus Torvalds pp_ptr = optptr; 3591da177e4SLinus Torvalds goto error; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds if (optlen < 4) { 3621da177e4SLinus Torvalds pp_ptr = optptr + 1; 3631da177e4SLinus Torvalds goto error; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds if (optptr[2] < 5) { 3661da177e4SLinus Torvalds pp_ptr = optptr + 2; 3671da177e4SLinus Torvalds goto error; 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds if (optptr[2] <= optlen) { 37048bdf072SChris Metcalf unsigned char *timeptr = NULL; 3715a2b646fSHisao Tanabe if (optptr[2]+3 > optlen) { 3721da177e4SLinus Torvalds pp_ptr = optptr + 2; 3731da177e4SLinus Torvalds goto error; 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds switch (optptr[3]&0xF) { 3761da177e4SLinus Torvalds case IPOPT_TS_TSONLY: 3771da177e4SLinus Torvalds if (skb) 37848bdf072SChris Metcalf timeptr = &optptr[optptr[2]-1]; 3791da177e4SLinus Torvalds opt->ts_needtime = 1; 3801da177e4SLinus Torvalds optptr[2] += 4; 3811da177e4SLinus Torvalds break; 3821da177e4SLinus Torvalds case IPOPT_TS_TSANDADDR: 3835a2b646fSHisao Tanabe if (optptr[2]+7 > optlen) { 3841da177e4SLinus Torvalds pp_ptr = optptr + 2; 3851da177e4SLinus Torvalds goto error; 3861da177e4SLinus Torvalds } 38711604721SDavid S. Miller if (rt) { 388bf5e53e3SEric Dumazet spec_dst_fill(&spec_dst, skb); 38935ebf65eSDavid S. Miller memcpy(&optptr[optptr[2]-1], &spec_dst, 4); 39048bdf072SChris Metcalf timeptr = &optptr[optptr[2]+3]; 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds opt->ts_needaddr = 1; 3931da177e4SLinus Torvalds opt->ts_needtime = 1; 3941da177e4SLinus Torvalds optptr[2] += 8; 3951da177e4SLinus Torvalds break; 3961da177e4SLinus Torvalds case IPOPT_TS_PRESPEC: 3975a2b646fSHisao Tanabe if (optptr[2]+7 > optlen) { 3981da177e4SLinus Torvalds pp_ptr = optptr + 2; 3991da177e4SLinus Torvalds goto error; 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds { 402fd683222SAl Viro __be32 addr; 4031da177e4SLinus Torvalds memcpy(&addr, &optptr[optptr[2]-1], 4); 4040e6bd4a1SDenis V. Lunev if (inet_addr_type(net, addr) == RTN_UNICAST) 4051da177e4SLinus Torvalds break; 4061da177e4SLinus Torvalds if (skb) 40748bdf072SChris Metcalf timeptr = &optptr[optptr[2]+3]; 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds opt->ts_needtime = 1; 4101da177e4SLinus Torvalds optptr[2] += 8; 4111da177e4SLinus Torvalds break; 4121da177e4SLinus Torvalds default: 41352e804c6SEric W. Biederman if (!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) { 4141da177e4SLinus Torvalds pp_ptr = optptr + 3; 4151da177e4SLinus Torvalds goto error; 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds break; 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds if (timeptr) { 420f25c3d61SYOSHIFUJI Hideaki struct timespec tv; 42148bdf072SChris Metcalf u32 midtime; 422f25c3d61SYOSHIFUJI Hideaki getnstimeofday(&tv); 42348bdf072SChris Metcalf midtime = (tv.tv_sec % 86400) * MSEC_PER_SEC + tv.tv_nsec / NSEC_PER_MSEC; 42448bdf072SChris Metcalf put_unaligned_be32(midtime, timeptr); 4251da177e4SLinus Torvalds opt->is_changed = 1; 4261da177e4SLinus Torvalds } 427fa2b04f4SDavid Ward } else if ((optptr[3]&0xF) != IPOPT_TS_PRESPEC) { 42895c96174SEric Dumazet unsigned int overflow = optptr[3]>>4; 4291da177e4SLinus Torvalds if (overflow == 15) { 4301da177e4SLinus Torvalds pp_ptr = optptr + 3; 4311da177e4SLinus Torvalds goto error; 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds if (skb) { 4341da177e4SLinus Torvalds optptr[3] = (optptr[3]&0xF)|((overflow+1)<<4); 4351da177e4SLinus Torvalds opt->is_changed = 1; 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds } 4384660c7f4SDavid Ward opt->ts = optptr - iph; 4391da177e4SLinus Torvalds break; 4401da177e4SLinus Torvalds case IPOPT_RA: 4411da177e4SLinus Torvalds if (optlen < 4) { 4421da177e4SLinus Torvalds pp_ptr = optptr + 1; 4431da177e4SLinus Torvalds goto error; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds if (optptr[2] == 0 && optptr[3] == 0) 4461da177e4SLinus Torvalds opt->router_alert = optptr - iph; 4471da177e4SLinus Torvalds break; 44811a03f78SPaul Moore case IPOPT_CIPSO: 44952e804c6SEric W. Biederman if ((!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) || opt->cipso) { 45011a03f78SPaul Moore pp_ptr = optptr; 45111a03f78SPaul Moore goto error; 45211a03f78SPaul Moore } 45311a03f78SPaul Moore opt->cipso = optptr - iph; 45415c45f7bSPaul Moore if (cipso_v4_validate(skb, &optptr)) { 45511a03f78SPaul Moore pp_ptr = optptr; 45611a03f78SPaul Moore goto error; 45711a03f78SPaul Moore } 45811a03f78SPaul Moore break; 4591da177e4SLinus Torvalds case IPOPT_SEC: 4601da177e4SLinus Torvalds case IPOPT_SID: 4611da177e4SLinus Torvalds default: 46252e804c6SEric W. Biederman if (!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) { 4631da177e4SLinus Torvalds pp_ptr = optptr; 4641da177e4SLinus Torvalds goto error; 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds break; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds l -= optlen; 4691da177e4SLinus Torvalds optptr += optlen; 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds eol: 4731da177e4SLinus Torvalds if (!pp_ptr) 4741da177e4SLinus Torvalds return 0; 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds error: 4771da177e4SLinus Torvalds if (skb) { 4781da177e4SLinus Torvalds icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((pp_ptr-iph)<<24)); 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds return -EINVAL; 4811da177e4SLinus Torvalds } 482462fb2afSBandan Das EXPORT_SYMBOL(ip_options_compile); 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds /* 4851da177e4SLinus Torvalds * Undo all the changes done by ip_options_compile(). 4861da177e4SLinus Torvalds */ 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds void ip_options_undo(struct ip_options *opt) 4891da177e4SLinus Torvalds { 4901da177e4SLinus Torvalds if (opt->srr) { 4911da177e4SLinus Torvalds unsigned char *optptr = opt->__data+opt->srr-sizeof(struct iphdr); 4921da177e4SLinus Torvalds memmove(optptr+7, optptr+3, optptr[1]-7); 4931da177e4SLinus Torvalds memcpy(optptr+3, &opt->faddr, 4); 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds if (opt->rr_needaddr) { 4961da177e4SLinus Torvalds unsigned char *optptr = opt->__data+opt->rr-sizeof(struct iphdr); 4971da177e4SLinus Torvalds optptr[2] -= 4; 4981da177e4SLinus Torvalds memset(&optptr[optptr[2]-1], 0, 4); 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds if (opt->ts) { 5011da177e4SLinus Torvalds unsigned char *optptr = opt->__data+opt->ts-sizeof(struct iphdr); 5021da177e4SLinus Torvalds if (opt->ts_needtime) { 5031da177e4SLinus Torvalds optptr[2] -= 4; 5041da177e4SLinus Torvalds memset(&optptr[optptr[2]-1], 0, 4); 5051da177e4SLinus Torvalds if ((optptr[3]&0xF) == IPOPT_TS_PRESPEC) 5061da177e4SLinus Torvalds optptr[2] -= 4; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds if (opt->ts_needaddr) { 5091da177e4SLinus Torvalds optptr[2] -= 4; 5101da177e4SLinus Torvalds memset(&optptr[optptr[2]-1], 0, 4); 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 515f6d8bd05SEric Dumazet static struct ip_options_rcu *ip_options_get_alloc(const int optlen) 5161da177e4SLinus Torvalds { 517f6d8bd05SEric Dumazet return kzalloc(sizeof(struct ip_options_rcu) + ((optlen + 3) & ~3), 5184c6ea29dSArnaldo Carvalho de Melo GFP_KERNEL); 5194c6ea29dSArnaldo Carvalho de Melo } 5201da177e4SLinus Torvalds 521f6d8bd05SEric Dumazet static int ip_options_get_finish(struct net *net, struct ip_options_rcu **optp, 522f6d8bd05SEric Dumazet struct ip_options_rcu *opt, int optlen) 5234c6ea29dSArnaldo Carvalho de Melo { 5241da177e4SLinus Torvalds while (optlen & 3) 525f6d8bd05SEric Dumazet opt->opt.__data[optlen++] = IPOPT_END; 526f6d8bd05SEric Dumazet opt->opt.optlen = optlen; 527f6d8bd05SEric Dumazet if (optlen && ip_options_compile(net, &opt->opt, NULL)) { 5281da177e4SLinus Torvalds kfree(opt); 5291da177e4SLinus Torvalds return -EINVAL; 5301da177e4SLinus Torvalds } 5311da177e4SLinus Torvalds kfree(*optp); 5321da177e4SLinus Torvalds *optp = opt; 5331da177e4SLinus Torvalds return 0; 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds 536f6d8bd05SEric Dumazet int ip_options_get_from_user(struct net *net, struct ip_options_rcu **optp, 537f2c4802bSDenis V. Lunev unsigned char __user *data, int optlen) 5384c6ea29dSArnaldo Carvalho de Melo { 539f6d8bd05SEric Dumazet struct ip_options_rcu *opt = ip_options_get_alloc(optlen); 5404c6ea29dSArnaldo Carvalho de Melo 5414c6ea29dSArnaldo Carvalho de Melo if (!opt) 5424c6ea29dSArnaldo Carvalho de Melo return -ENOMEM; 543f6d8bd05SEric Dumazet if (optlen && copy_from_user(opt->opt.__data, data, optlen)) { 5444c6ea29dSArnaldo Carvalho de Melo kfree(opt); 5454c6ea29dSArnaldo Carvalho de Melo return -EFAULT; 5464c6ea29dSArnaldo Carvalho de Melo } 547f2c4802bSDenis V. Lunev return ip_options_get_finish(net, optp, opt, optlen); 5484c6ea29dSArnaldo Carvalho de Melo } 5494c6ea29dSArnaldo Carvalho de Melo 550f6d8bd05SEric Dumazet int ip_options_get(struct net *net, struct ip_options_rcu **optp, 551f2c4802bSDenis V. Lunev unsigned char *data, int optlen) 5524c6ea29dSArnaldo Carvalho de Melo { 553f6d8bd05SEric Dumazet struct ip_options_rcu *opt = ip_options_get_alloc(optlen); 5544c6ea29dSArnaldo Carvalho de Melo 5554c6ea29dSArnaldo Carvalho de Melo if (!opt) 5564c6ea29dSArnaldo Carvalho de Melo return -ENOMEM; 5574c6ea29dSArnaldo Carvalho de Melo if (optlen) 558f6d8bd05SEric Dumazet memcpy(opt->opt.__data, data, optlen); 559f2c4802bSDenis V. Lunev return ip_options_get_finish(net, optp, opt, optlen); 5604c6ea29dSArnaldo Carvalho de Melo } 5614c6ea29dSArnaldo Carvalho de Melo 5621da177e4SLinus Torvalds void ip_forward_options(struct sk_buff *skb) 5631da177e4SLinus Torvalds { 5641da177e4SLinus Torvalds struct ip_options *opt = &(IPCB(skb)->opt); 5651da177e4SLinus Torvalds unsigned char *optptr; 566511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 567d56f90a7SArnaldo Carvalho de Melo unsigned char *raw = skb_network_header(skb); 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds if (opt->rr_needaddr) { 5701da177e4SLinus Torvalds optptr = (unsigned char *)raw + opt->rr; 5718e36360aSDavid S. Miller ip_rt_get_source(&optptr[optptr[2]-5], skb, rt); 5721da177e4SLinus Torvalds opt->is_changed = 1; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds if (opt->srr_is_hit) { 5751da177e4SLinus Torvalds int srrptr, srrspace; 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds optptr = raw + opt->srr; 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds for ( srrptr = optptr[2], srrspace = optptr[1]; 5801da177e4SLinus Torvalds srrptr <= srrspace; 5811da177e4SLinus Torvalds srrptr += 4 5821da177e4SLinus Torvalds ) { 5831da177e4SLinus Torvalds if (srrptr + 3 > srrspace) 5841da177e4SLinus Torvalds break; 585ac8a4810SLi Wei if (memcmp(&opt->nexthop, &optptr[srrptr-1], 4) == 0) 5861da177e4SLinus Torvalds break; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds if (srrptr + 3 <= srrspace) { 5891da177e4SLinus Torvalds opt->is_changed = 1; 590ac8a4810SLi Wei ip_hdr(skb)->daddr = opt->nexthop; 5915dc7883fSLi Wei ip_rt_get_source(&optptr[srrptr-1], skb, rt); 5921da177e4SLinus Torvalds optptr[2] = srrptr+4; 593e87cc472SJoe Perches } else { 594e87cc472SJoe Perches net_crit_ratelimited("%s(): Argh! Destination lost!\n", 595e87cc472SJoe Perches __func__); 596e87cc472SJoe Perches } 5971da177e4SLinus Torvalds if (opt->ts_needaddr) { 5981da177e4SLinus Torvalds optptr = raw + opt->ts; 5998e36360aSDavid S. Miller ip_rt_get_source(&optptr[optptr[2]-9], skb, rt); 6001da177e4SLinus Torvalds opt->is_changed = 1; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds if (opt->is_changed) { 6041da177e4SLinus Torvalds opt->is_changed = 0; 605eddc9ec5SArnaldo Carvalho de Melo ip_send_check(ip_hdr(skb)); 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds int ip_options_rcv_srr(struct sk_buff *skb) 6101da177e4SLinus Torvalds { 6111da177e4SLinus Torvalds struct ip_options *opt = &(IPCB(skb)->opt); 6121da177e4SLinus Torvalds int srrspace, srrptr; 6139e12bb22SAl Viro __be32 nexthop; 614eddc9ec5SArnaldo Carvalho de Melo struct iphdr *iph = ip_hdr(skb); 615d56f90a7SArnaldo Carvalho de Melo unsigned char *optptr = skb_network_header(skb) + opt->srr; 616511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 6171da177e4SLinus Torvalds struct rtable *rt2; 6187fee226aSEric Dumazet unsigned long orefdst; 6191da177e4SLinus Torvalds int err; 6201da177e4SLinus Torvalds 62110949550SDavid S. Miller if (!rt) 6221da177e4SLinus Torvalds return 0; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds if (skb->pkt_type != PACKET_HOST) 6251da177e4SLinus Torvalds return -EINVAL; 6261da177e4SLinus Torvalds if (rt->rt_type == RTN_UNICAST) { 6271da177e4SLinus Torvalds if (!opt->is_strictroute) 6281da177e4SLinus Torvalds return 0; 6291da177e4SLinus Torvalds icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl(16<<24)); 6301da177e4SLinus Torvalds return -EINVAL; 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds if (rt->rt_type != RTN_LOCAL) 6331da177e4SLinus Torvalds return -EINVAL; 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds for (srrptr = optptr[2], srrspace = optptr[1]; srrptr <= srrspace; srrptr += 4) { 6361da177e4SLinus Torvalds if (srrptr + 3 > srrspace) { 6371da177e4SLinus Torvalds icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((opt->srr+2)<<24)); 6381da177e4SLinus Torvalds return -EINVAL; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds memcpy(&nexthop, &optptr[srrptr-1], 4); 6411da177e4SLinus Torvalds 6427fee226aSEric Dumazet orefdst = skb->_skb_refdst; 643adf30907SEric Dumazet skb_dst_set(skb, NULL); 6441da177e4SLinus Torvalds err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, skb->dev); 645511c3f92SEric Dumazet rt2 = skb_rtable(skb); 6461da177e4SLinus Torvalds if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) { 6477fee226aSEric Dumazet skb_dst_drop(skb); 6487fee226aSEric Dumazet skb->_skb_refdst = orefdst; 6491da177e4SLinus Torvalds return -EINVAL; 6501da177e4SLinus Torvalds } 6517fee226aSEric Dumazet refdst_drop(orefdst); 6521da177e4SLinus Torvalds if (rt2->rt_type != RTN_LOCAL) 6531da177e4SLinus Torvalds break; 6541da177e4SLinus Torvalds /* Superfast 8) loopback forward */ 655c30883bdSDavid S. Miller iph->daddr = nexthop; 6561da177e4SLinus Torvalds opt->is_changed = 1; 6571da177e4SLinus Torvalds } 6581da177e4SLinus Torvalds if (srrptr <= srrspace) { 6591da177e4SLinus Torvalds opt->srr_is_hit = 1; 660ac8a4810SLi Wei opt->nexthop = nexthop; 6611da177e4SLinus Torvalds opt->is_changed = 1; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds return 0; 6641da177e4SLinus Torvalds } 665462fb2afSBandan Das EXPORT_SYMBOL(ip_options_rcv_srr); 666