blob: a9e22a098872fa3a1a18b5654afc4944dec0ded9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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.
6 *
7 * The options processing module for ip.c
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Authors: A.N.Kuznetsov
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Joe Perchesafd465032012-03-12 07:03:32 +000013#define pr_fmt(fmt) "IPv4: " fmt
14
Randy Dunlap4fc268d2006-01-11 12:17:47 -080015#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/types.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080019#include <linux/uaccess.h>
Chris Metcalf48bdf0722011-05-29 10:55:44 +000020#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/skbuff.h>
22#include <linux/ip.h>
23#include <linux/icmp.h>
24#include <linux/netdevice.h>
25#include <linux/rtnetlink.h>
26#include <net/sock.h>
27#include <net/ip.h>
28#include <net/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020029#include <net/route.h>
Paul Moore11a03f72006-08-03 16:46:20 -070030#include <net/cipso_ipv4.h>
David S. Miller35ebf652012-06-28 03:59:11 -070031#include <net/ip_fib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090033/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * 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.
39 *
40 * daddr is real destination address, next hop is recorded in IP header.
41 * saddr is address of outgoing interface.
42 */
43
Eric Dumazetf6d8bd02011-04-21 09:45:37 +000044void ip_options_build(struct sk_buff *skb, struct ip_options *opt,
Jakub Kicinski4f0e3042022-01-28 08:06:54 -080045 __be32 daddr, struct rtable *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070047 unsigned char *iph = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 memcpy(&(IPCB(skb)->opt), opt, sizeof(struct ip_options));
Miaohe Lin343d8c62020-08-25 08:32:11 -040050 memcpy(iph + sizeof(struct iphdr), opt->__data, opt->optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 if (opt->srr)
Miaohe Lin343d8c62020-08-25 08:32:11 -040054 memcpy(iph + opt->srr + iph[opt->srr + 1] - 4, &daddr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jakub Kicinski4f0e3042022-01-28 08:06:54 -080056 if (opt->rr_needaddr)
57 ip_rt_get_source(iph + opt->rr + iph[opt->rr + 2] - 5, skb, rt);
58 if (opt->ts_needaddr)
59 ip_rt_get_source(iph + opt->ts + iph[opt->ts + 2] - 9, skb, rt);
60 if (opt->ts_needtime) {
61 __be32 midtime;
Deepa Dinamani822c8682016-02-27 00:32:15 -080062
Jakub Kicinski4f0e3042022-01-28 08:06:54 -080063 midtime = inet_current_timestamp();
64 memcpy(iph + opt->ts + iph[opt->ts + 2] - 5, &midtime, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
66}
67
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * 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.
73 *
74 * NOTE: dopt cannot point to skb.
75 */
76
Paolo Abeni91ed1e62017-08-03 18:07:06 +020077int __ip_options_echo(struct net *net, struct ip_options *dopt,
78 struct sk_buff *skb, const struct ip_options *sopt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 unsigned char *sptr, *dptr;
81 int soffset, doffset;
82 int optlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 memset(dopt, 0, sizeof(struct ip_options));
85
Eric Dumazetf6d8bd02011-04-21 09:45:37 +000086 if (sopt->optlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070089 sptr = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 dptr = dopt->__data;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (sopt->rr) {
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)
99 return -EINVAL;
100 dptr[2] = soffset + 4;
101 dopt->rr_needaddr = 1;
102 }
103 dptr += optlen;
104 dopt->optlen += optlen;
105 }
106 if (sopt->ts) {
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)
114 return -EINVAL;
115 dopt->ts_needaddr = 1;
116 soffset += 4;
117 }
118 if (sopt->ts_needtime) {
119 if (soffset + 3 > optlen)
120 return -EINVAL;
121 if ((dptr[3]&0xF) != IPOPT_TS_PRESPEC) {
122 dopt->ts_needtime = 1;
123 soffset += 4;
124 } else {
125 dopt->ts_needtime = 0;
126
Jan Luebbe8628bd82011-03-24 07:44:22 +0000127 if (soffset + 7 <= optlen) {
Al Virofd683222006-09-26 22:17:51 -0700128 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Jan Luebbe8628bd82011-03-24 07:44:22 +0000130 memcpy(&addr, dptr+soffset-1, 4);
Paolo Abeni91ed1e62017-08-03 18:07:06 +0200131 if (inet_addr_type(net, addr) != RTN_UNICAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 dopt->ts_needtime = 1;
133 soffset += 8;
134 }
135 }
136 }
137 }
138 dptr[2] = soffset;
139 }
140 dptr += optlen;
141 dopt->optlen += optlen;
142 }
143 if (sopt->srr) {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000144 unsigned char *start = sptr+sopt->srr;
Al Viro3ca3c682006-09-27 18:28:07 -0700145 __be32 faddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 optlen = start[1];
148 soffset = start[2];
149 doffset = 0;
150 if (soffset > optlen)
151 soffset = optlen + 1;
152 soffset -= 4;
153 if (soffset > 3) {
154 memcpy(&faddr, &start[soffset-1], 4);
Weilong Chena22318e2013-12-23 14:37:26 +0800155 for (soffset -= 4, doffset = 4; soffset > 3; soffset -= 4, doffset += 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 memcpy(&dptr[doffset-1], &start[soffset-1], 4);
157 /*
158 * RFC1812 requires to fix illegal source routes.
159 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700160 if (memcmp(&ip_hdr(skb)->saddr,
161 &start[soffset + 3], 4) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 doffset -= 4;
163 }
164 if (doffset > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 dopt->faddr = faddr;
166 dptr[0] = start[0];
167 dptr[1] = doffset+3;
168 dptr[2] = 4;
169 dptr += doffset+3;
170 dopt->srr = dopt->optlen + sizeof(struct iphdr);
171 dopt->optlen += doffset+3;
172 dopt->is_strictroute = sopt->is_strictroute;
173 }
174 }
Paul Moore11a03f72006-08-03 16:46:20 -0700175 if (sopt->cipso) {
176 optlen = sptr[sopt->cipso+1];
177 dopt->cipso = dopt->optlen+sizeof(struct iphdr);
178 memcpy(dptr, sptr+sopt->cipso, optlen);
179 dptr += optlen;
180 dopt->optlen += optlen;
181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 while (dopt->optlen & 3) {
183 *dptr++ = IPOPT_END;
184 dopt->optlen++;
185 }
186 return 0;
187}
188
189/*
190 * Options "fragmenting", just fill options not
191 * allowed in fragments with NOOPs.
192 * Simple and stupid 8), but the most efficient way.
193 */
194
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000195void ip_options_fragment(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700197 unsigned char *optptr = skb_network_header(skb) + sizeof(struct iphdr);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000198 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int l = opt->optlen;
200 int optlen;
201
202 while (l > 0) {
203 switch (*optptr) {
204 case IPOPT_END:
205 return;
206 case IPOPT_NOOP:
207 l--;
208 optptr++;
209 continue;
210 }
211 optlen = optptr[1];
Weilong Chena22318e2013-12-23 14:37:26 +0800212 if (optlen < 2 || optlen > l)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return;
214 if (!IPOPT_COPIED(*optptr))
215 memset(optptr, IPOPT_NOOP, optlen);
216 l -= optlen;
217 optptr += optlen;
218 }
219 opt->ts = 0;
220 opt->rr = 0;
221 opt->rr_needaddr = 0;
222 opt->ts_needaddr = 0;
223 opt->ts_needtime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Eric Dumazetbf5e53e2012-07-04 22:30:09 +0000226/* helper used by ip_options_compile() to call fib_compute_spec_dst()
227 * at most one time.
228 */
229static void spec_dst_fill(__be32 *spec_dst, struct sk_buff *skb)
230{
231 if (*spec_dst == htonl(INADDR_ANY))
232 *spec_dst = fib_compute_spec_dst(skb);
233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/*
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.
239 */
240
Nazarov Sergey3da1ed72019-02-25 19:27:15 +0300241int __ip_options_compile(struct net *net,
242 struct ip_options *opt, struct sk_buff *skb,
243 __be32 *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Eric Dumazetbf5e53e2012-07-04 22:30:09 +0000245 __be32 spec_dst = htonl(INADDR_ANY);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000246 unsigned char *pp_ptr = NULL;
David S. Miller11604722012-07-04 16:13:17 -0700247 struct rtable *rt = NULL;
David S. Miller35ebf652012-06-28 03:59:11 -0700248 unsigned char *optptr;
249 unsigned char *iph;
250 int optlen, l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Ian Morris00db4122015-04-03 09:17:27 +0100252 if (skb) {
David S. Miller11604722012-07-04 16:13:17 -0700253 rt = skb_rtable(skb);
Denis V. Lunev22aba382008-03-22 16:36:20 -0700254 optptr = (unsigned char *)&(ip_hdr(skb)[1]);
255 } else
Denis V. Lunev10fe7d82008-03-22 16:35:00 -0700256 optptr = opt->__data;
Denis V. Lunev22aba382008-03-22 16:36:20 -0700257 iph = optptr - sizeof(struct iphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 for (l = opt->optlen; l > 0; ) {
260 switch (*optptr) {
Weilong Chendd9b4552013-12-31 15:11:28 +0800261 case IPOPT_END:
Weilong Chena22318e2013-12-23 14:37:26 +0800262 for (optptr++, l--; l > 0; optptr++, l--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (*optptr != IPOPT_END) {
264 *optptr = IPOPT_END;
265 opt->is_changed = 1;
266 }
267 }
268 goto eol;
Weilong Chendd9b4552013-12-31 15:11:28 +0800269 case IPOPT_NOOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 l--;
271 optptr++;
272 continue;
273 }
Eric Dumazet10ec9472014-07-21 07:17:42 +0200274 if (unlikely(l < 2)) {
275 pp_ptr = optptr;
276 goto error;
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 optlen = optptr[1];
Weilong Chena22318e2013-12-23 14:37:26 +0800279 if (optlen < 2 || optlen > l) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 pp_ptr = optptr;
281 goto error;
282 }
283 switch (*optptr) {
Weilong Chendd9b4552013-12-31 15:11:28 +0800284 case IPOPT_SSRR:
285 case IPOPT_LSRR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (optlen < 3) {
287 pp_ptr = optptr + 1;
288 goto error;
289 }
290 if (optptr[2] < 4) {
291 pp_ptr = optptr + 2;
292 goto error;
293 }
294 /* NB: cf RFC-1812 5.2.4.1 */
295 if (opt->srr) {
296 pp_ptr = optptr;
297 goto error;
298 }
299 if (!skb) {
300 if (optptr[2] != 4 || optlen < 7 || ((optlen-3) & 3)) {
301 pp_ptr = optptr + 1;
302 goto error;
303 }
304 memcpy(&opt->faddr, &optptr[3], 4);
305 if (optlen > 7)
306 memmove(&optptr[3], &optptr[7], optlen-7);
307 }
308 opt->is_strictroute = (optptr[0] == IPOPT_SSRR);
309 opt->srr = optptr - iph;
310 break;
Weilong Chendd9b4552013-12-31 15:11:28 +0800311 case IPOPT_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (opt->rr) {
313 pp_ptr = optptr;
314 goto error;
315 }
316 if (optlen < 3) {
317 pp_ptr = optptr + 1;
318 goto error;
319 }
320 if (optptr[2] < 4) {
321 pp_ptr = optptr + 2;
322 goto error;
323 }
324 if (optptr[2] <= optlen) {
325 if (optptr[2]+3 > optlen) {
326 pp_ptr = optptr + 2;
327 goto error;
328 }
David S. Miller11604722012-07-04 16:13:17 -0700329 if (rt) {
Eric Dumazetbf5e53e2012-07-04 22:30:09 +0000330 spec_dst_fill(&spec_dst, skb);
David S. Miller35ebf652012-06-28 03:59:11 -0700331 memcpy(&optptr[optptr[2]-1], &spec_dst, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 opt->is_changed = 1;
333 }
334 optptr[2] += 4;
335 opt->rr_needaddr = 1;
336 }
337 opt->rr = optptr - iph;
338 break;
Weilong Chendd9b4552013-12-31 15:11:28 +0800339 case IPOPT_TIMESTAMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (opt->ts) {
341 pp_ptr = optptr;
342 goto error;
343 }
344 if (optlen < 4) {
345 pp_ptr = optptr + 1;
346 goto error;
347 }
348 if (optptr[2] < 5) {
349 pp_ptr = optptr + 2;
350 goto error;
351 }
352 if (optptr[2] <= optlen) {
Chris Metcalf48bdf0722011-05-29 10:55:44 +0000353 unsigned char *timeptr = NULL;
Hisao Tanabe5a2b6462014-04-27 19:03:45 +0900354 if (optptr[2]+3 > optlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 pp_ptr = optptr + 2;
356 goto error;
357 }
358 switch (optptr[3]&0xF) {
Weilong Chendd9b4552013-12-31 15:11:28 +0800359 case IPOPT_TS_TSONLY:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900360 if (skb)
Chris Metcalf48bdf0722011-05-29 10:55:44 +0000361 timeptr = &optptr[optptr[2]-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 opt->ts_needtime = 1;
363 optptr[2] += 4;
364 break;
Weilong Chendd9b4552013-12-31 15:11:28 +0800365 case IPOPT_TS_TSANDADDR:
Hisao Tanabe5a2b6462014-04-27 19:03:45 +0900366 if (optptr[2]+7 > optlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 pp_ptr = optptr + 2;
368 goto error;
369 }
David S. Miller11604722012-07-04 16:13:17 -0700370 if (rt) {
Eric Dumazetbf5e53e2012-07-04 22:30:09 +0000371 spec_dst_fill(&spec_dst, skb);
David S. Miller35ebf652012-06-28 03:59:11 -0700372 memcpy(&optptr[optptr[2]-1], &spec_dst, 4);
Chris Metcalf48bdf0722011-05-29 10:55:44 +0000373 timeptr = &optptr[optptr[2]+3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375 opt->ts_needaddr = 1;
376 opt->ts_needtime = 1;
377 optptr[2] += 8;
378 break;
Weilong Chendd9b4552013-12-31 15:11:28 +0800379 case IPOPT_TS_PRESPEC:
Hisao Tanabe5a2b6462014-04-27 19:03:45 +0900380 if (optptr[2]+7 > optlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 pp_ptr = optptr + 2;
382 goto error;
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 {
Al Virofd683222006-09-26 22:17:51 -0700385 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 memcpy(&addr, &optptr[optptr[2]-1], 4);
Denis V. Lunev0e6bd4a2008-03-24 15:29:23 -0700387 if (inet_addr_type(net, addr) == RTN_UNICAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 break;
389 if (skb)
Chris Metcalf48bdf0722011-05-29 10:55:44 +0000390 timeptr = &optptr[optptr[2]+3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392 opt->ts_needtime = 1;
393 optptr[2] += 8;
394 break;
Weilong Chendd9b4552013-12-31 15:11:28 +0800395 default:
Eric W. Biederman52e804c2012-11-16 03:03:05 +0000396 if (!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 pp_ptr = optptr + 3;
398 goto error;
399 }
400 break;
401 }
402 if (timeptr) {
Deepa Dinamani822c8682016-02-27 00:32:15 -0800403 __be32 midtime;
404
405 midtime = inet_current_timestamp();
406 memcpy(timeptr, &midtime, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 opt->is_changed = 1;
408 }
David Wardfa2b04f2013-03-05 17:06:32 +0000409 } else if ((optptr[3]&0xF) != IPOPT_TS_PRESPEC) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000410 unsigned int overflow = optptr[3]>>4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (overflow == 15) {
412 pp_ptr = optptr + 3;
413 goto error;
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (skb) {
416 optptr[3] = (optptr[3]&0xF)|((overflow+1)<<4);
417 opt->is_changed = 1;
418 }
419 }
David Ward4660c7f2013-03-11 10:43:39 +0000420 opt->ts = optptr - iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 break;
Weilong Chendd9b4552013-12-31 15:11:28 +0800422 case IPOPT_RA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (optlen < 4) {
424 pp_ptr = optptr + 1;
425 goto error;
426 }
427 if (optptr[2] == 0 && optptr[3] == 0)
428 opt->router_alert = optptr - iph;
429 break;
Weilong Chendd9b4552013-12-31 15:11:28 +0800430 case IPOPT_CIPSO:
Eric W. Biederman52e804c2012-11-16 03:03:05 +0000431 if ((!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) || opt->cipso) {
Paul Moore11a03f72006-08-03 16:46:20 -0700432 pp_ptr = optptr;
433 goto error;
434 }
435 opt->cipso = optptr - iph;
Paul Moore15c45f72008-10-10 10:16:34 -0400436 if (cipso_v4_validate(skb, &optptr)) {
Paul Moore11a03f72006-08-03 16:46:20 -0700437 pp_ptr = optptr;
438 goto error;
439 }
440 break;
Weilong Chendd9b4552013-12-31 15:11:28 +0800441 case IPOPT_SEC:
442 case IPOPT_SID:
443 default:
Eric W. Biederman52e804c2012-11-16 03:03:05 +0000444 if (!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 pp_ptr = optptr;
446 goto error;
447 }
448 break;
449 }
450 l -= optlen;
451 optptr += optlen;
452 }
453
454eol:
455 if (!pp_ptr)
456 return 0;
457
458error:
Nazarov Sergey3da1ed72019-02-25 19:27:15 +0300459 if (info)
460 *info = htonl((pp_ptr-iph)<<24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return -EINVAL;
462}
Stephen Suryaputradbb52812019-06-20 12:19:59 -0400463EXPORT_SYMBOL(__ip_options_compile);
Nazarov Sergey3da1ed72019-02-25 19:27:15 +0300464
465int ip_options_compile(struct net *net,
466 struct ip_options *opt, struct sk_buff *skb)
467{
468 int ret;
469 __be32 info;
470
471 ret = __ip_options_compile(net, opt, skb, &info);
472 if (ret != 0 && skb)
473 icmp_send(skb, ICMP_PARAMETERPROB, 0, info);
474 return ret;
475}
Bandan Das462fb2a2010-09-19 09:34:33 +0000476EXPORT_SYMBOL(ip_options_compile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478/*
479 * Undo all the changes done by ip_options_compile().
480 */
481
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000482void ip_options_undo(struct ip_options *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 if (opt->srr) {
Miaohe Lin343d8c62020-08-25 08:32:11 -0400485 unsigned char *optptr = opt->__data + opt->srr - sizeof(struct iphdr);
486
487 memmove(optptr + 7, optptr + 3, optptr[1] - 7);
488 memcpy(optptr + 3, &opt->faddr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490 if (opt->rr_needaddr) {
Miaohe Lin343d8c62020-08-25 08:32:11 -0400491 unsigned char *optptr = opt->__data + opt->rr - sizeof(struct iphdr);
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 optptr[2] -= 4;
Miaohe Lin343d8c62020-08-25 08:32:11 -0400494 memset(&optptr[optptr[2] - 1], 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 if (opt->ts) {
Miaohe Lin343d8c62020-08-25 08:32:11 -0400497 unsigned char *optptr = opt->__data + opt->ts - sizeof(struct iphdr);
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (opt->ts_needtime) {
500 optptr[2] -= 4;
Miaohe Lin343d8c62020-08-25 08:32:11 -0400501 memset(&optptr[optptr[2] - 1], 0, 4);
502 if ((optptr[3] & 0xF) == IPOPT_TS_PRESPEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 optptr[2] -= 4;
504 }
505 if (opt->ts_needaddr) {
506 optptr[2] -= 4;
Miaohe Lin343d8c62020-08-25 08:32:11 -0400507 memset(&optptr[optptr[2] - 1], 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 }
510}
511
Christoph Hellwigde40a3e2020-07-23 08:08:57 +0200512int ip_options_get(struct net *net, struct ip_options_rcu **optp,
513 sockptr_t data, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Christoph Hellwigde40a3e2020-07-23 08:08:57 +0200515 struct ip_options_rcu *opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Christoph Hellwigde40a3e2020-07-23 08:08:57 +0200517 opt = kzalloc(sizeof(struct ip_options_rcu) + ((optlen + 3) & ~3),
518 GFP_KERNEL);
519 if (!opt)
520 return -ENOMEM;
521 if (optlen && copy_from_sockptr(opt->opt.__data, data, optlen)) {
522 kfree(opt);
523 return -EFAULT;
524 }
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 while (optlen & 3)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000527 opt->opt.__data[optlen++] = IPOPT_END;
528 opt->opt.optlen = optlen;
529 if (optlen && ip_options_compile(net, &opt->opt, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 kfree(opt);
531 return -EINVAL;
532 }
Jesper Juhla51482b2005-11-08 09:41:34 -0800533 kfree(*optp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 *optp = opt;
535 return 0;
536}
537
538void ip_forward_options(struct sk_buff *skb)
539{
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000540 struct ip_options *opt = &(IPCB(skb)->opt);
541 unsigned char *optptr;
Eric Dumazet511c3f92009-06-02 05:14:27 +0000542 struct rtable *rt = skb_rtable(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700543 unsigned char *raw = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 if (opt->rr_needaddr) {
546 optptr = (unsigned char *)raw + opt->rr;
David S. Miller8e363602011-05-13 17:29:41 -0400547 ip_rt_get_source(&optptr[optptr[2]-5], skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 opt->is_changed = 1;
549 }
550 if (opt->srr_is_hit) {
551 int srrptr, srrspace;
552
553 optptr = raw + opt->srr;
554
Weilong Chena22318e2013-12-23 14:37:26 +0800555 for ( srrptr = optptr[2], srrspace = optptr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 srrptr <= srrspace;
557 srrptr += 4
558 ) {
559 if (srrptr + 3 > srrspace)
560 break;
Li Weiac8a4812011-11-22 23:33:10 +0000561 if (memcmp(&opt->nexthop, &optptr[srrptr-1], 4) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 break;
563 }
564 if (srrptr + 3 <= srrspace) {
565 opt->is_changed = 1;
Li Weiac8a4812011-11-22 23:33:10 +0000566 ip_hdr(skb)->daddr = opt->nexthop;
Li Wei5dc78832012-02-09 21:15:25 +0000567 ip_rt_get_source(&optptr[srrptr-1], skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 optptr[2] = srrptr+4;
Joe Perchese87cc472012-05-13 21:56:26 +0000569 } else {
570 net_crit_ratelimited("%s(): Argh! Destination lost!\n",
571 __func__);
572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (opt->ts_needaddr) {
574 optptr = raw + opt->ts;
David S. Miller8e363602011-05-13 17:29:41 -0400575 ip_rt_get_source(&optptr[optptr[2]-9], skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 opt->is_changed = 1;
577 }
578 }
579 if (opt->is_changed) {
580 opt->is_changed = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700581 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583}
584
Stephen Suryaputra8c83f2d2019-04-01 09:17:32 -0400585int ip_options_rcv_srr(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 struct ip_options *opt = &(IPCB(skb)->opt);
588 int srrspace, srrptr;
Al Viro9e12bb22006-09-26 21:25:20 -0700589 __be32 nexthop;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700590 struct iphdr *iph = ip_hdr(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700591 unsigned char *optptr = skb_network_header(skb) + opt->srr;
Eric Dumazet511c3f92009-06-02 05:14:27 +0000592 struct rtable *rt = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct rtable *rt2;
Eric Dumazet7fee2262010-05-11 23:19:48 +0000594 unsigned long orefdst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int err;
596
David S. Miller10949552011-05-12 19:26:57 -0400597 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 0;
599
600 if (skb->pkt_type != PACKET_HOST)
601 return -EINVAL;
602 if (rt->rt_type == RTN_UNICAST) {
603 if (!opt->is_strictroute)
604 return 0;
605 icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl(16<<24));
606 return -EINVAL;
607 }
608 if (rt->rt_type != RTN_LOCAL)
609 return -EINVAL;
610
Weilong Chena22318e2013-12-23 14:37:26 +0800611 for (srrptr = optptr[2], srrspace = optptr[1]; srrptr <= srrspace; srrptr += 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (srrptr + 3 > srrspace) {
613 icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((opt->srr+2)<<24));
614 return -EINVAL;
615 }
616 memcpy(&nexthop, &optptr[srrptr-1], 4);
617
Eric Dumazet7fee2262010-05-11 23:19:48 +0000618 orefdst = skb->_skb_refdst;
Eric Dumazetadf30902009-06-02 05:19:30 +0000619 skb_dst_set(skb, NULL);
Stephen Suryaputra8c83f2d2019-04-01 09:17:32 -0400620 err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +0000621 rt2 = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) {
Eric Dumazet7fee2262010-05-11 23:19:48 +0000623 skb_dst_drop(skb);
624 skb->_skb_refdst = orefdst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -EINVAL;
626 }
Eric Dumazet7fee2262010-05-11 23:19:48 +0000627 refdst_drop(orefdst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (rt2->rt_type != RTN_LOCAL)
629 break;
630 /* Superfast 8) loopback forward */
David S. Millerc30883b2011-05-12 19:30:58 -0400631 iph->daddr = nexthop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 opt->is_changed = 1;
633 }
634 if (srrptr <= srrspace) {
635 opt->srr_is_hit = 1;
Li Weiac8a4812011-11-22 23:33:10 +0000636 opt->nexthop = nexthop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 opt->is_changed = 1;
638 }
639 return 0;
640}
Bandan Das462fb2a2010-09-19 09:34:33 +0000641EXPORT_SYMBOL(ip_options_rcv_srr);