1 // SPDX-License-Identifier: GPL-2.0
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * The options processing module for ip.c
9 * Authors: A.N.Kuznetsov
13 #define pr_fmt(fmt) "IPv4: " fmt
15 #include <linux/capability.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include <linux/uaccess.h>
20 #include <asm/unaligned.h>
21 #include <linux/skbuff.h>
23 #include <linux/icmp.h>
24 #include <linux/netdevice.h>
25 #include <linux/rtnetlink.h>
29 #include <net/route.h>
30 #include <net/cipso_ipv4.h>
31 #include <net/ip_fib.h>
34 * Write options to IP header, record destination address to
35 * source route option, address of outgoing interface
36 * (we should already know it, so that this function is allowed be
37 * called only after routing decision) and timestamp,
38 * if we originate this datagram.
40 * daddr is real destination address, next hop is recorded in IP header.
41 * saddr is address of outgoing interface.
44 void ip_options_build(struct sk_buff *skb, struct ip_options *opt,
45 __be32 daddr, struct rtable *rt)
47 unsigned char *iph = skb_network_header(skb);
49 memcpy(&(IPCB(skb)->opt), opt, sizeof(struct ip_options));
50 memcpy(iph + sizeof(struct iphdr), opt->__data, opt->optlen);
51 opt = &(IPCB(skb)->opt);
54 memcpy(iph + opt->srr + iph[opt->srr + 1] - 4, &daddr, 4);
57 ip_rt_get_source(iph + opt->rr + iph[opt->rr + 2] - 5, skb, rt);
59 ip_rt_get_source(iph + opt->ts + iph[opt->ts + 2] - 9, skb, rt);
60 if (opt->ts_needtime) {
63 midtime = inet_current_timestamp();
64 memcpy(iph + opt->ts + iph[opt->ts + 2] - 5, &midtime, 4);
69 * Provided (sopt, skb) points to received options,
70 * build in dopt compiled option set appropriate for answering.
71 * i.e. invert SRR option, copy anothers,
72 * and grab room in RR/TS options.
74 * NOTE: dopt cannot point to skb.
77 int __ip_options_echo(struct net *net, struct ip_options *dopt,
78 struct sk_buff *skb, const struct ip_options *sopt)
80 unsigned char *sptr, *dptr;
84 memset(dopt, 0, sizeof(struct ip_options));
86 if (sopt->optlen == 0)
89 sptr = skb_network_header(skb);
93 optlen = sptr[sopt->rr+1];
94 soffset = sptr[sopt->rr+2];
95 dopt->rr = dopt->optlen + sizeof(struct iphdr);
96 memcpy(dptr, sptr+sopt->rr, optlen);
97 if (sopt->rr_needaddr && soffset <= optlen) {
98 if (soffset + 3 > optlen)
100 dptr[2] = soffset + 4;
101 dopt->rr_needaddr = 1;
104 dopt->optlen += optlen;
107 optlen = sptr[sopt->ts+1];
108 soffset = sptr[sopt->ts+2];
109 dopt->ts = dopt->optlen + sizeof(struct iphdr);
110 memcpy(dptr, sptr+sopt->ts, optlen);
111 if (soffset <= optlen) {
112 if (sopt->ts_needaddr) {
113 if (soffset + 3 > optlen)
115 dopt->ts_needaddr = 1;
118 if (sopt->ts_needtime) {
119 if (soffset + 3 > optlen)
121 if ((dptr[3]&0xF) != IPOPT_TS_PRESPEC) {
122 dopt->ts_needtime = 1;
125 dopt->ts_needtime = 0;
127 if (soffset + 7 <= optlen) {
130 memcpy(&addr, dptr+soffset-1, 4);
131 if (inet_addr_type(net, addr) != RTN_UNICAST) {
132 dopt->ts_needtime = 1;
141 dopt->optlen += optlen;
144 unsigned char *start = sptr+sopt->srr;
150 if (soffset > optlen)
151 soffset = optlen + 1;
154 memcpy(&faddr, &start[soffset-1], 4);
155 for (soffset -= 4, doffset = 4; soffset > 3; soffset -= 4, doffset += 4)
156 memcpy(&dptr[doffset-1], &start[soffset-1], 4);
158 * RFC1812 requires to fix illegal source routes.
160 if (memcmp(&ip_hdr(skb)->saddr,
161 &start[soffset + 3], 4) == 0)
170 dopt->srr = dopt->optlen + sizeof(struct iphdr);
171 dopt->optlen += doffset+3;
172 dopt->is_strictroute = sopt->is_strictroute;
176 optlen = sptr[sopt->cipso+1];
177 dopt->cipso = dopt->optlen+sizeof(struct iphdr);
178 memcpy(dptr, sptr+sopt->cipso, optlen);
180 dopt->optlen += optlen;
182 while (dopt->optlen & 3) {
190 * Options "fragmenting", just fill options not
191 * allowed in fragments with NOOPs.
192 * Simple and stupid 8), but the most efficient way.
195 void ip_options_fragment(struct sk_buff *skb)
197 unsigned char *optptr = skb_network_header(skb) + sizeof(struct iphdr);
198 struct ip_options *opt = &(IPCB(skb)->opt);
212 if (optlen < 2 || optlen > l)
214 if (!IPOPT_COPIED(*optptr))
215 memset(optptr, IPOPT_NOOP, optlen);
221 opt->rr_needaddr = 0;
222 opt->ts_needaddr = 0;
223 opt->ts_needtime = 0;
226 /* helper used by ip_options_compile() to call fib_compute_spec_dst()
229 static void spec_dst_fill(__be32 *spec_dst, struct sk_buff *skb)
231 if (*spec_dst == htonl(INADDR_ANY))
232 *spec_dst = fib_compute_spec_dst(skb);
236 * Verify options and fill pointers in struct options.
237 * Caller should clear *opt, and set opt->data.
238 * If opt == NULL, then skb->data should point to IP header.
241 int __ip_options_compile(struct net *net,
242 struct ip_options *opt, struct sk_buff *skb,
245 __be32 spec_dst = htonl(INADDR_ANY);
246 unsigned char *pp_ptr = NULL;
247 struct rtable *rt = NULL;
248 unsigned char *optptr;
253 rt = skb_rtable(skb);
254 optptr = (unsigned char *)&(ip_hdr(skb)[1]);
256 optptr = opt->__data;
257 iph = optptr - sizeof(struct iphdr);
259 for (l = opt->optlen; l > 0; ) {
262 for (optptr++, l--; l > 0; optptr++, l--) {
263 if (*optptr != IPOPT_END) {
274 if (unlikely(l < 2)) {
279 if (optlen < 2 || optlen > l) {
294 /* NB: cf RFC-1812 5.2.4.1 */
300 if (optptr[2] != 4 || optlen < 7 || ((optlen-3) & 3)) {
304 memcpy(&opt->faddr, &optptr[3], 4);
306 memmove(&optptr[3], &optptr[7], optlen-7);
308 opt->is_strictroute = (optptr[0] == IPOPT_SSRR);
309 opt->srr = optptr - iph;
324 if (optptr[2] <= optlen) {
325 if (optptr[2]+3 > optlen) {
330 spec_dst_fill(&spec_dst, skb);
331 memcpy(&optptr[optptr[2]-1], &spec_dst, 4);
335 opt->rr_needaddr = 1;
337 opt->rr = optptr - iph;
339 case IPOPT_TIMESTAMP:
352 if (optptr[2] <= optlen) {
353 unsigned char *timeptr = NULL;
354 if (optptr[2]+3 > optlen) {
358 switch (optptr[3]&0xF) {
359 case IPOPT_TS_TSONLY:
361 timeptr = &optptr[optptr[2]-1];
362 opt->ts_needtime = 1;
365 case IPOPT_TS_TSANDADDR:
366 if (optptr[2]+7 > optlen) {
371 spec_dst_fill(&spec_dst, skb);
372 memcpy(&optptr[optptr[2]-1], &spec_dst, 4);
373 timeptr = &optptr[optptr[2]+3];
375 opt->ts_needaddr = 1;
376 opt->ts_needtime = 1;
379 case IPOPT_TS_PRESPEC:
380 if (optptr[2]+7 > optlen) {
386 memcpy(&addr, &optptr[optptr[2]-1], 4);
387 if (inet_addr_type(net, addr) == RTN_UNICAST)
390 timeptr = &optptr[optptr[2]+3];
392 opt->ts_needtime = 1;
396 if (!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) {
405 midtime = inet_current_timestamp();
406 memcpy(timeptr, &midtime, 4);
409 } else if ((optptr[3]&0xF) != IPOPT_TS_PRESPEC) {
410 unsigned int overflow = optptr[3]>>4;
411 if (overflow == 15) {
416 optptr[3] = (optptr[3]&0xF)|((overflow+1)<<4);
420 opt->ts = optptr - iph;
427 if (optptr[2] == 0 && optptr[3] == 0)
428 opt->router_alert = optptr - iph;
431 if ((!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) || opt->cipso) {
435 opt->cipso = optptr - iph;
436 if (cipso_v4_validate(skb, &optptr)) {
444 if (!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) {
460 *info = htonl((pp_ptr-iph)<<24);
463 EXPORT_SYMBOL(__ip_options_compile);
465 int ip_options_compile(struct net *net,
466 struct ip_options *opt, struct sk_buff *skb)
471 ret = __ip_options_compile(net, opt, skb, &info);
473 icmp_send(skb, ICMP_PARAMETERPROB, 0, info);
476 EXPORT_SYMBOL(ip_options_compile);
479 * Undo all the changes done by ip_options_compile().
482 void ip_options_undo(struct ip_options *opt)
485 unsigned char *optptr = opt->__data + opt->srr - sizeof(struct iphdr);
487 memmove(optptr + 7, optptr + 3, optptr[1] - 7);
488 memcpy(optptr + 3, &opt->faddr, 4);
490 if (opt->rr_needaddr) {
491 unsigned char *optptr = opt->__data + opt->rr - sizeof(struct iphdr);
494 memset(&optptr[optptr[2] - 1], 0, 4);
497 unsigned char *optptr = opt->__data + opt->ts - sizeof(struct iphdr);
499 if (opt->ts_needtime) {
501 memset(&optptr[optptr[2] - 1], 0, 4);
502 if ((optptr[3] & 0xF) == IPOPT_TS_PRESPEC)
505 if (opt->ts_needaddr) {
507 memset(&optptr[optptr[2] - 1], 0, 4);
512 int ip_options_get(struct net *net, struct ip_options_rcu **optp,
513 sockptr_t data, int optlen)
515 struct ip_options_rcu *opt;
517 opt = kzalloc(sizeof(struct ip_options_rcu) + ((optlen + 3) & ~3),
521 if (optlen && copy_from_sockptr(opt->opt.__data, data, optlen)) {
527 opt->opt.__data[optlen++] = IPOPT_END;
528 opt->opt.optlen = optlen;
529 if (optlen && ip_options_compile(net, &opt->opt, NULL)) {
538 void ip_forward_options(struct sk_buff *skb)
540 struct ip_options *opt = &(IPCB(skb)->opt);
541 unsigned char *optptr;
542 struct rtable *rt = skb_rtable(skb);
543 unsigned char *raw = skb_network_header(skb);
545 if (opt->rr_needaddr) {
546 optptr = (unsigned char *)raw + opt->rr;
547 ip_rt_get_source(&optptr[optptr[2]-5], skb, rt);
550 if (opt->srr_is_hit) {
551 int srrptr, srrspace;
553 optptr = raw + opt->srr;
555 for ( srrptr = optptr[2], srrspace = optptr[1];
559 if (srrptr + 3 > srrspace)
561 if (memcmp(&opt->nexthop, &optptr[srrptr-1], 4) == 0)
564 if (srrptr + 3 <= srrspace) {
566 ip_hdr(skb)->daddr = opt->nexthop;
567 ip_rt_get_source(&optptr[srrptr-1], skb, rt);
568 optptr[2] = srrptr+4;
570 net_crit_ratelimited("%s(): Argh! Destination lost!\n",
573 if (opt->ts_needaddr) {
574 optptr = raw + opt->ts;
575 ip_rt_get_source(&optptr[optptr[2]-9], skb, rt);
579 if (opt->is_changed) {
581 ip_send_check(ip_hdr(skb));
585 int ip_options_rcv_srr(struct sk_buff *skb, struct net_device *dev)
587 struct ip_options *opt = &(IPCB(skb)->opt);
588 int srrspace, srrptr;
590 struct iphdr *iph = ip_hdr(skb);
591 unsigned char *optptr = skb_network_header(skb) + opt->srr;
592 struct rtable *rt = skb_rtable(skb);
594 unsigned long orefdst;
600 if (skb->pkt_type != PACKET_HOST)
602 if (rt->rt_type == RTN_UNICAST) {
603 if (!opt->is_strictroute)
605 icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl(16<<24));
608 if (rt->rt_type != RTN_LOCAL)
611 for (srrptr = optptr[2], srrspace = optptr[1]; srrptr <= srrspace; srrptr += 4) {
612 if (srrptr + 3 > srrspace) {
613 icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((opt->srr+2)<<24));
616 memcpy(&nexthop, &optptr[srrptr-1], 4);
618 orefdst = skb->_skb_refdst;
619 skb_dst_set(skb, NULL);
620 err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, dev);
621 rt2 = skb_rtable(skb);
622 if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) {
624 skb->_skb_refdst = orefdst;
627 refdst_drop(orefdst);
628 if (rt2->rt_type != RTN_LOCAL)
630 /* Superfast 8) loopback forward */
631 iph->daddr = nexthop;
634 if (srrptr <= srrspace) {
636 opt->nexthop = nexthop;
641 EXPORT_SYMBOL(ip_options_rcv_srr);