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.
6 * The options processing module for ip.c
8 * Version: $Id: ip_options.c,v 1.21 2001/09/01 00:31:50 davem Exp $
10 * Authors: A.N.Kuznetsov
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <asm/uaccess.h>
17 #include <linux/skbuff.h>
19 #include <linux/icmp.h>
20 #include <linux/netdevice.h>
21 #include <linux/rtnetlink.h>
27 * Write options to IP header, record destination address to
28 * source route option, address of outgoing interface
29 * (we should already know it, so that this function is allowed be
30 * called only after routing decision) and timestamp,
31 * if we originate this datagram.
33 * daddr is real destination address, next hop is recorded in IP header.
34 * saddr is address of outgoing interface.
37 void ip_options_build(struct sk_buff * skb, struct ip_options * opt,
38 u32 daddr, struct rtable *rt, int is_frag)
40 unsigned char * iph = skb->nh.raw;
42 memcpy(&(IPCB(skb)->opt), opt, sizeof(struct ip_options));
43 memcpy(iph+sizeof(struct iphdr), opt->__data, opt->optlen);
44 opt = &(IPCB(skb)->opt);
48 memcpy(iph+opt->srr+iph[opt->srr+1]-4, &daddr, 4);
52 ip_rt_get_source(iph+opt->rr+iph[opt->rr+2]-5, rt);
54 ip_rt_get_source(iph+opt->ts+iph[opt->ts+2]-9, rt);
55 if (opt->ts_needtime) {
59 midtime = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
60 memcpy(iph+opt->ts+iph[opt->ts+2]-5, &midtime, 4);
65 memset(iph+opt->rr, IPOPT_NOP, iph[opt->rr+1]);
70 memset(iph+opt->ts, IPOPT_NOP, iph[opt->ts+1]);
72 opt->ts_needaddr = opt->ts_needtime = 0;
77 * Provided (sopt, skb) points to received options,
78 * build in dopt compiled option set appropriate for answering.
79 * i.e. invert SRR option, copy anothers,
80 * and grab room in RR/TS options.
82 * NOTE: dopt cannot point to skb.
85 int ip_options_echo(struct ip_options * dopt, struct sk_buff * skb)
87 struct ip_options *sopt;
88 unsigned char *sptr, *dptr;
93 memset(dopt, 0, sizeof(struct ip_options));
97 sopt = &(IPCB(skb)->opt);
99 if (sopt->optlen == 0) {
108 daddr = ((struct rtable*)skb->dst)->rt_spec_dst;
110 daddr = skb->nh.iph->daddr;
113 optlen = sptr[sopt->rr+1];
114 soffset = sptr[sopt->rr+2];
115 dopt->rr = dopt->optlen + sizeof(struct iphdr);
116 memcpy(dptr, sptr+sopt->rr, optlen);
117 if (sopt->rr_needaddr && soffset <= optlen) {
118 if (soffset + 3 > optlen)
120 dptr[2] = soffset + 4;
121 dopt->rr_needaddr = 1;
124 dopt->optlen += optlen;
127 optlen = sptr[sopt->ts+1];
128 soffset = sptr[sopt->ts+2];
129 dopt->ts = dopt->optlen + sizeof(struct iphdr);
130 memcpy(dptr, sptr+sopt->ts, optlen);
131 if (soffset <= optlen) {
132 if (sopt->ts_needaddr) {
133 if (soffset + 3 > optlen)
135 dopt->ts_needaddr = 1;
138 if (sopt->ts_needtime) {
139 if (soffset + 3 > optlen)
141 if ((dptr[3]&0xF) != IPOPT_TS_PRESPEC) {
142 dopt->ts_needtime = 1;
145 dopt->ts_needtime = 0;
147 if (soffset + 8 <= optlen) {
150 memcpy(&addr, sptr+soffset-1, 4);
151 if (inet_addr_type(addr) != RTN_LOCAL) {
152 dopt->ts_needtime = 1;
161 dopt->optlen += optlen;
164 unsigned char * start = sptr+sopt->srr;
170 if (soffset > optlen)
171 soffset = optlen + 1;
174 memcpy(&faddr, &start[soffset-1], 4);
175 for (soffset-=4, doffset=4; soffset > 3; soffset-=4, doffset+=4)
176 memcpy(&dptr[doffset-1], &start[soffset-1], 4);
178 * RFC1812 requires to fix illegal source routes.
180 if (memcmp(&skb->nh.iph->saddr, &start[soffset+3], 4) == 0)
184 memcpy(&start[doffset-1], &daddr, 4);
190 dopt->srr = dopt->optlen + sizeof(struct iphdr);
191 dopt->optlen += doffset+3;
192 dopt->is_strictroute = sopt->is_strictroute;
195 while (dopt->optlen & 3) {
203 * Options "fragmenting", just fill options not
204 * allowed in fragments with NOOPs.
205 * Simple and stupid 8), but the most efficient way.
208 void ip_options_fragment(struct sk_buff * skb)
210 unsigned char * optptr = skb->nh.raw;
211 struct ip_options * opt = &(IPCB(skb)->opt);
225 if (optlen<2 || optlen>l)
227 if (!IPOPT_COPIED(*optptr))
228 memset(optptr, IPOPT_NOOP, optlen);
234 opt->rr_needaddr = 0;
235 opt->ts_needaddr = 0;
236 opt->ts_needtime = 0;
241 * Verify options and fill pointers in struct options.
242 * Caller should clear *opt, and set opt->data.
243 * If opt == NULL, then skb->data should point to IP header.
246 int ip_options_compile(struct ip_options * opt, struct sk_buff * skb)
250 unsigned char * optptr;
252 unsigned char * pp_ptr = NULL;
253 struct rtable *rt = skb ? (struct rtable*)skb->dst : NULL;
256 opt = &(IPCB(skb)->opt);
257 memset(opt, 0, sizeof(struct ip_options));
259 opt->optlen = ((struct iphdr *)iph)->ihl*4 - sizeof(struct iphdr);
260 optptr = iph + sizeof(struct iphdr);
263 optptr = opt->is_data ? opt->__data : (unsigned char*)&(skb->nh.iph[1]);
264 iph = optptr - sizeof(struct iphdr);
267 for (l = opt->optlen; l > 0; ) {
270 for (optptr++, l--; l>0; optptr++, l--) {
271 if (*optptr != IPOPT_END) {
283 if (optlen<2 || optlen>l) {
298 /* NB: cf RFC-1812 5.2.4.1 */
304 if (optptr[2] != 4 || optlen < 7 || ((optlen-3) & 3)) {
308 memcpy(&opt->faddr, &optptr[3], 4);
310 memmove(&optptr[3], &optptr[7], optlen-7);
312 opt->is_strictroute = (optptr[0] == IPOPT_SSRR);
313 opt->srr = optptr - iph;
328 if (optptr[2] <= optlen) {
329 if (optptr[2]+3 > optlen) {
334 memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4);
338 opt->rr_needaddr = 1;
340 opt->rr = optptr - iph;
342 case IPOPT_TIMESTAMP:
355 if (optptr[2] <= optlen) {
356 __u32 * timeptr = NULL;
357 if (optptr[2]+3 > optptr[1]) {
361 switch (optptr[3]&0xF) {
362 case IPOPT_TS_TSONLY:
363 opt->ts = optptr - iph;
365 timeptr = (__u32*)&optptr[optptr[2]-1];
366 opt->ts_needtime = 1;
369 case IPOPT_TS_TSANDADDR:
370 if (optptr[2]+7 > optptr[1]) {
374 opt->ts = optptr - iph;
376 memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4);
377 timeptr = (__u32*)&optptr[optptr[2]+3];
379 opt->ts_needaddr = 1;
380 opt->ts_needtime = 1;
383 case IPOPT_TS_PRESPEC:
384 if (optptr[2]+7 > optptr[1]) {
388 opt->ts = optptr - iph;
391 memcpy(&addr, &optptr[optptr[2]-1], 4);
392 if (inet_addr_type(addr) == RTN_UNICAST)
395 timeptr = (__u32*)&optptr[optptr[2]+3];
397 opt->ts_needtime = 1;
401 if (!skb && !capable(CAP_NET_RAW)) {
410 do_gettimeofday(&tv);
411 midtime = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
412 memcpy(timeptr, &midtime, sizeof(__u32));
416 unsigned overflow = optptr[3]>>4;
417 if (overflow == 15) {
421 opt->ts = optptr - iph;
423 optptr[3] = (optptr[3]&0xF)|((overflow+1)<<4);
433 if (optptr[2] == 0 && optptr[3] == 0)
434 opt->router_alert = optptr - iph;
439 if (!skb && !capable(CAP_NET_RAW)) {
455 icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((pp_ptr-iph)<<24));
462 * Undo all the changes done by ip_options_compile().
465 void ip_options_undo(struct ip_options * opt)
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);
472 if (opt->rr_needaddr) {
473 unsigned char * optptr = opt->__data+opt->rr-sizeof(struct iphdr);
475 memset(&optptr[optptr[2]-1], 0, 4);
478 unsigned char * optptr = opt->__data+opt->ts-sizeof(struct iphdr);
479 if (opt->ts_needtime) {
481 memset(&optptr[optptr[2]-1], 0, 4);
482 if ((optptr[3]&0xF) == IPOPT_TS_PRESPEC)
485 if (opt->ts_needaddr) {
487 memset(&optptr[optptr[2]-1], 0, 4);
492 int ip_options_get(struct ip_options **optp, unsigned char *data, int optlen, int user)
494 struct ip_options *opt;
496 opt = kmalloc(sizeof(struct ip_options)+((optlen+3)&~3), GFP_KERNEL);
499 memset(opt, 0, sizeof(struct ip_options));
502 if (copy_from_user(opt->__data, data, optlen)) {
507 memcpy(opt->__data, data, optlen);
510 opt->__data[optlen++] = IPOPT_END;
511 opt->optlen = optlen;
513 opt->is_setbyuser = 1;
514 if (optlen && ip_options_compile(opt, NULL)) {
524 void ip_forward_options(struct sk_buff *skb)
526 struct ip_options * opt = &(IPCB(skb)->opt);
527 unsigned char * optptr;
528 struct rtable *rt = (struct rtable*)skb->dst;
529 unsigned char *raw = skb->nh.raw;
531 if (opt->rr_needaddr) {
532 optptr = (unsigned char *)raw + opt->rr;
533 ip_rt_get_source(&optptr[optptr[2]-5], rt);
536 if (opt->srr_is_hit) {
537 int srrptr, srrspace;
539 optptr = raw + opt->srr;
541 for ( srrptr=optptr[2], srrspace = optptr[1];
545 if (srrptr + 3 > srrspace)
547 if (memcmp(&rt->rt_dst, &optptr[srrptr-1], 4) == 0)
550 if (srrptr + 3 <= srrspace) {
552 ip_rt_get_source(&optptr[srrptr-1], rt);
553 skb->nh.iph->daddr = rt->rt_dst;
554 optptr[2] = srrptr+4;
555 } else if (net_ratelimit())
556 printk(KERN_CRIT "ip_forward(): Argh! Destination lost!\n");
557 if (opt->ts_needaddr) {
558 optptr = raw + opt->ts;
559 ip_rt_get_source(&optptr[optptr[2]-9], rt);
563 if (opt->is_changed) {
565 ip_send_check(skb->nh.iph);
569 int ip_options_rcv_srr(struct sk_buff *skb)
571 struct ip_options *opt = &(IPCB(skb)->opt);
572 int srrspace, srrptr;
574 struct iphdr *iph = skb->nh.iph;
575 unsigned char * optptr = skb->nh.raw + opt->srr;
576 struct rtable *rt = (struct rtable*)skb->dst;
583 if (skb->pkt_type != PACKET_HOST)
585 if (rt->rt_type == RTN_UNICAST) {
586 if (!opt->is_strictroute)
588 icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl(16<<24));
591 if (rt->rt_type != RTN_LOCAL)
594 for (srrptr=optptr[2], srrspace = optptr[1]; srrptr <= srrspace; srrptr += 4) {
595 if (srrptr + 3 > srrspace) {
596 icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((opt->srr+2)<<24));
599 memcpy(&nexthop, &optptr[srrptr-1], 4);
601 rt = (struct rtable*)skb->dst;
603 err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, skb->dev);
604 rt2 = (struct rtable*)skb->dst;
605 if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) {
607 skb->dst = &rt->u.dst;
611 if (rt2->rt_type != RTN_LOCAL)
613 /* Superfast 8) loopback forward */
614 memcpy(&iph->daddr, &optptr[srrptr-1], 4);
617 if (srrptr <= srrspace) {
624 EXPORT_SYMBOL(ip_options_compile);
625 EXPORT_SYMBOL(ip_options_undo);