blob: b587dee006f8828c69436c4d82e7388c8a6829f5 [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 IP to API glue.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Authors: see ip.c
10 *
11 * Fixes:
12 * Many : Split from ip.c , see ip.c for history.
13 * Martin Mares : TOS setting fixed.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090014 * Alan Cox : Fixed a couple of oopses in Martin's
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * TOS tweaks.
16 * Mike McLagan : Routing by source
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/types.h>
21#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/skbuff.h>
23#include <linux/ip.h>
24#include <linux/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020025#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/sock.h>
29#include <net/ip.h>
30#include <net/icmp.h>
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -080031#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/udp.h>
33#include <linux/igmp.h>
34#include <linux/netfilter.h>
35#include <linux/route.h>
36#include <linux/mroute.h>
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -040037#include <net/inet_ecn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <net/route.h>
39#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070040#include <net/compat.h>
Tom Herbertad6f9392015-01-05 13:56:17 -080041#include <net/checksum.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000042#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <net/transp_v6.h>
44#endif
David S. Miller35ebf652012-06-28 03:59:11 -070045#include <net/ip_fib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <linux/errqueue.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080048#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070050#include <linux/bpfilter.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * SOL_IP control messages.
54 */
55
56static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
57{
Eric Dumazetd826eb12011-11-09 07:24:35 +000058 struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070060 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
63}
64
65static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
66{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070067 int ttl = ip_hdr(skb)->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
69}
70
71static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
72{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070073 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
77{
78 if (IPCB(skb)->opt.optlen == 0)
79 return;
80
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070081 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
82 ip_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85
Paolo Abeni91ed1e62017-08-03 18:07:06 +020086static void ip_cmsg_recv_retopts(struct net *net, struct msghdr *msg,
87 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 unsigned char optbuf[sizeof(struct ip_options) + 40];
Daniel Baluta5e73ea12012-04-15 01:34:41 +000090 struct ip_options *opt = (struct ip_options *)optbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 if (IPCB(skb)->opt.optlen == 0)
93 return;
94
Paolo Abeni91ed1e62017-08-03 18:07:06 +020095 if (ip_options_echo(net, opt, skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 msg->msg_flags |= MSG_CTRUNC;
97 return;
98 }
99 ip_options_undo(opt);
100
101 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
102}
103
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400104static void ip_cmsg_recv_fragsize(struct msghdr *msg, struct sk_buff *skb)
105{
106 int val;
107
108 if (IPCB(skb)->frag_max_size == 0)
109 return;
110
111 val = IPCB(skb)->frag_max_size;
112 put_cmsg(msg, SOL_IP, IP_RECVFRAGSIZE, sizeof(val), &val);
113}
114
Tom Herbertad6f9392015-01-05 13:56:17 -0800115static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb,
Eric Dumazet10df8e62016-10-23 18:03:06 -0700116 int tlen, int offset)
Tom Herbertad6f9392015-01-05 13:56:17 -0800117{
118 __wsum csum = skb->csum;
119
120 if (skb->ip_summed != CHECKSUM_COMPLETE)
121 return;
122
Paolo Abenica4ef452017-02-21 09:33:18 +0100123 if (offset != 0) {
124 int tend_off = skb_transport_offset(skb) + tlen;
125 csum = csum_sub(csum, skb_checksum(skb, tend_off, offset, 0));
126 }
Tom Herbertad6f9392015-01-05 13:56:17 -0800127
128 put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum);
129}
130
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800131static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
132{
133 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700134 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800135 int err;
136
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700137 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
138 if (err)
139 return;
140
141 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800142 if (err)
143 return;
144
145 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700146 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800147}
148
Harvey Harrison21d1a162008-11-20 01:54:27 -0800149static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800150{
Willem de Bruijn4a06fa62019-01-07 16:47:33 -0500151 __be16 _ports[2], *ports;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800152 struct sockaddr_in sin;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800153
154 /* All current transport protocols have the port numbers in the
155 * first four bytes of the transport header and this function is
156 * written with this assumption in mind.
157 */
Willem de Bruijn4a06fa62019-01-07 16:47:33 -0500158 ports = skb_header_pointer(skb, skb_transport_offset(skb),
159 sizeof(_ports), &_ports);
160 if (!ports)
161 return;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800162
163 sin.sin_family = AF_INET;
Eric Dumazet64199fc2018-09-30 11:33:39 -0700164 sin.sin_addr.s_addr = ip_hdr(skb)->daddr;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800165 sin.sin_port = ports[1];
166 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
167
168 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Paolo Abeniad959032016-11-04 11:28:58 +0100171void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk,
172 struct sk_buff *skb, int tlen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Paolo Abeniad959032016-11-04 11:28:58 +0100174 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000175 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /* Ordered by supposed usage frequency */
Tom Herbertc44d13d2015-01-05 13:56:15 -0800178 if (flags & IP_CMSG_PKTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 ip_cmsg_recv_pktinfo(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Tom Herbertc44d13d2015-01-05 13:56:15 -0800181 flags &= ~IP_CMSG_PKTINFO;
182 if (!flags)
183 return;
184 }
185
186 if (flags & IP_CMSG_TTL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 ip_cmsg_recv_ttl(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Tom Herbertc44d13d2015-01-05 13:56:15 -0800189 flags &= ~IP_CMSG_TTL;
190 if (!flags)
191 return;
192 }
193
194 if (flags & IP_CMSG_TOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 ip_cmsg_recv_tos(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Tom Herbertc44d13d2015-01-05 13:56:15 -0800197 flags &= ~IP_CMSG_TOS;
198 if (!flags)
199 return;
200 }
201
202 if (flags & IP_CMSG_RECVOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 ip_cmsg_recv_opts(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Tom Herbertc44d13d2015-01-05 13:56:15 -0800205 flags &= ~IP_CMSG_RECVOPTS;
206 if (!flags)
207 return;
208 }
209
210 if (flags & IP_CMSG_RETOPTS) {
Paolo Abeni91ed1e62017-08-03 18:07:06 +0200211 ip_cmsg_recv_retopts(sock_net(sk), msg, skb);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800212
Tom Herbertc44d13d2015-01-05 13:56:15 -0800213 flags &= ~IP_CMSG_RETOPTS;
214 if (!flags)
215 return;
216 }
217
218 if (flags & IP_CMSG_PASSSEC) {
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800219 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800220
Tom Herbertc44d13d2015-01-05 13:56:15 -0800221 flags &= ~IP_CMSG_PASSSEC;
222 if (!flags)
223 return;
224 }
225
Tom Herbertad6f9392015-01-05 13:56:17 -0800226 if (flags & IP_CMSG_ORIGDSTADDR) {
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800227 ip_cmsg_recv_dstaddr(msg, skb);
228
Tom Herbertad6f9392015-01-05 13:56:17 -0800229 flags &= ~IP_CMSG_ORIGDSTADDR;
230 if (!flags)
231 return;
232 }
233
234 if (flags & IP_CMSG_CHECKSUM)
Eric Dumazet10df8e62016-10-23 18:03:06 -0700235 ip_cmsg_recv_checksum(msg, skb, tlen, offset);
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400236
237 if (flags & IP_CMSG_RECVFRAGSIZE)
238 ip_cmsg_recv_fragsize(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
Tom Herbert5961de92015-01-05 13:56:16 -0800240EXPORT_SYMBOL(ip_cmsg_recv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400242int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100243 bool allow_ipv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Francesco Fuscof02db312013-09-24 15:43:08 +0200245 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct cmsghdr *cmsg;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400247 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Gu Zhengf95b4142014-12-11 11:22:04 +0800249 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (!CMSG_OK(msg, cmsg))
251 return -EINVAL;
Eric Dumazet5337b5b2014-11-10 17:54:25 -0800252#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100253 if (allow_ipv6 &&
254 cmsg->cmsg_level == SOL_IPV6 &&
255 cmsg->cmsg_type == IPV6_PKTINFO) {
256 struct in6_pktinfo *src_info;
257
258 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
259 return -EINVAL;
260 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
261 if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
262 return -EINVAL;
David Ahern1cbec072018-02-16 11:03:03 -0800263 if (src_info->ipi6_ifindex)
264 ipc->oif = src_info->ipi6_ifindex;
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100265 ipc->addr = src_info->ipi6_addr.s6_addr32[3];
266 continue;
267 }
268#endif
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400269 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700270 err = __sock_cmsg_send(sk, msg, cmsg, &ipc->sockc);
271 if (err)
272 return err;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400273 continue;
274 }
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (cmsg->cmsg_level != SOL_IP)
277 continue;
278 switch (cmsg->cmsg_type) {
279 case IP_RETOPTS:
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800280 err = cmsg->cmsg_len - sizeof(struct cmsghdr);
Eric Dumazet91948302016-02-04 06:23:28 -0800281
282 /* Our caller is responsible for freeing ipc->opt */
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700283 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
284 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (err)
286 return err;
287 break;
288 case IP_PKTINFO:
289 {
290 struct in_pktinfo *info;
291 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
292 return -EINVAL;
293 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
David Ahern1cbec072018-02-16 11:03:03 -0800294 if (info->ipi_ifindex)
295 ipc->oif = info->ipi_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 ipc->addr = info->ipi_spec_dst.s_addr;
297 break;
298 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200299 case IP_TTL:
300 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
301 return -EINVAL;
302 val = *(int *)CMSG_DATA(cmsg);
303 if (val < 1 || val > 255)
304 return -EINVAL;
305 ipc->ttl = val;
306 break;
307 case IP_TOS:
Eric Dumazete895cdc2016-09-07 21:52:56 -0700308 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)))
309 val = *(int *)CMSG_DATA(cmsg);
310 else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8)))
311 val = *(u8 *)CMSG_DATA(cmsg);
312 else
Francesco Fuscof02db312013-09-24 15:43:08 +0200313 return -EINVAL;
Francesco Fuscof02db312013-09-24 15:43:08 +0200314 if (val < 0 || val > 255)
315 return -EINVAL;
316 ipc->tos = val;
317 ipc->priority = rt_tos2priority(ipc->tos);
318 break;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 default:
321 return -EINVAL;
322 }
323 }
324 return 0;
325}
326
Eric Dumazet592fcb92010-06-09 16:21:07 +0000327static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000328{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000329 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
330
331 sock_put(ra->saved_sk);
332 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000333}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700335int ip_ra_control(struct sock *sk, unsigned char on,
336 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000338 struct ip_ra_chain *ra, *new_ra;
339 struct ip_ra_chain __rcu **rap;
Kirill Tkhai5796ef72018-03-22 12:45:32 +0300340 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000342 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return -EINVAL;
344
345 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
Gen Zhang425aa0e2019-05-24 11:24:26 +0800346 if (on && !new_ra)
347 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300349 mutex_lock(&net->ipv4.ra_mutex);
Kirill Tkhai5796ef72018-03-22 12:45:32 +0300350 for (rap = &net->ipv4.ra_chain;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300351 (ra = rcu_dereference_protected(*rap,
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300352 lockdep_is_held(&net->ipv4.ra_mutex))) != NULL;
Eric Dumazet43a951e2010-10-25 03:32:44 +0000353 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (ra->sk == sk) {
355 if (on) {
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300356 mutex_unlock(&net->ipv4.ra_mutex);
Jesper Juhla51482b2005-11-08 09:41:34 -0800357 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return -EADDRINUSE;
359 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000360 /* dont let ip_call_ra_chain() use sk again */
361 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700362 RCU_INIT_POINTER(*rap, ra->next);
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300363 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 if (ra->destructor)
366 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000367 /*
368 * Delay sock_put(sk) and kfree(ra) after one rcu grace
369 * period. This guarantee ip_call_ra_chain() dont need
370 * to mess with socket refcounts.
371 */
372 ra->saved_sk = sk;
373 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375 }
376 }
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300377 if (!new_ra) {
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300378 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return -ENOBUFS;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 new_ra->sk = sk;
382 new_ra->destructor = destructor;
383
Eric Dumazet8e380f02014-09-09 08:11:41 -0700384 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000385 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 sock_hold(sk);
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300387 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 return 0;
390}
391
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900392void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700393 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct sock_exterr_skb *serr;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 skb = skb_clone(skb, GFP_ATOMIC);
398 if (!skb)
399 return;
400
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900401 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 serr->ee.ee_errno = err;
403 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300404 serr->ee.ee_type = icmp_hdr(skb)->type;
405 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 serr->ee.ee_pad = 0;
407 serr->ee.ee_info = info;
408 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300409 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700410 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 serr->port = port;
412
Ian Morris00db4122015-04-03 09:17:27 +0100413 if (skb_pull(skb, payload - skb->data)) {
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300414 skb_reset_transport_header(skb);
415 if (sock_queue_err_skb(sk, skb) == 0)
416 return;
417 }
418 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Al Viro05790162006-09-27 18:33:40 -0700421void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct inet_sock *inet = inet_sk(sk);
424 struct sock_exterr_skb *serr;
425 struct iphdr *iph;
426 struct sk_buff *skb;
427
428 if (!inet->recverr)
429 return;
430
431 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
432 if (!skb)
433 return;
434
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300435 skb_put(skb, sizeof(struct iphdr));
436 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700437 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 iph->daddr = daddr;
439
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900440 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 serr->ee.ee_errno = err;
442 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900443 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 serr->ee.ee_code = 0;
445 serr->ee.ee_pad = 0;
446 serr->ee.ee_info = info;
447 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700448 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 serr->port = port;
450
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700451 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300452 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 if (sock_queue_err_skb(sk, skb))
455 kfree_skb(skb);
456}
457
Julian Anastasov34b99df2015-06-23 08:34:39 +0300458/* For some errors we have valid addr_offset even with zero payload and
459 * zero port. Also, addr_offset should be supported if port is set.
460 */
461static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
462{
463 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
464 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
465}
466
Willem de Bruijnc247f052015-03-07 20:33:22 -0500467/* IPv4 supports cmsg on all imcp errors and some timestamps
468 *
469 * Timestamp code paths do not initialize the fields expected by cmsg:
470 * the PKTINFO fields in skb->cb[]. Fill those in here.
471 */
472static bool ipv4_datagram_support_cmsg(const struct sock *sk,
473 struct sk_buff *skb,
474 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500475{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500476 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500477
Willem de Bruijnc247f052015-03-07 20:33:22 -0500478 if (ee_origin == SO_EE_ORIGIN_ICMP)
479 return true;
480
481 if (ee_origin == SO_EE_ORIGIN_LOCAL)
482 return false;
483
484 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
Willem de Bruijn1862d622017-04-12 19:24:35 -0400485 * timestamp with egress dev. Not possible for packets without iif
Willem de Bruijnc247f052015-03-07 20:33:22 -0500486 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
487 */
Willem de Bruijn1862d622017-04-12 19:24:35 -0400488 info = PKTINFO_SKB_CB(skb);
489 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
490 !info->ipi_ifindex)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500491 return false;
492
493 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500494 return true;
495}
496
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900497/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * Handle MSG_ERRQUEUE
499 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100500int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400503 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100504 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct {
506 struct sock_extended_err ee;
507 struct sockaddr_in offender;
508 } errhdr;
509 int err;
510 int copied;
511
512 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400513 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100514 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 goto out;
516
517 copied = skb->len;
518 if (copied > len) {
519 msg->msg_flags |= MSG_TRUNC;
520 copied = len;
521 }
David S. Miller51f3d022014-11-05 16:46:40 -0500522 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700523 if (unlikely(err)) {
524 kfree_skb(skb);
525 return err;
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 sock_recv_timestamp(msg, sk, skb);
528
529 serr = SKB_EXT_ERR(skb);
530
Julian Anastasov34b99df2015-06-23 08:34:39 +0300531 if (sin && ipv4_datagram_support_addr(serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700533 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
534 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 sin->sin_port = serr->port;
536 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100537 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
540 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
541 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500542 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500543
Willem de Bruijnc247f052015-03-07 20:33:22 -0500544 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700546 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500547 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 ip_cmsg_recv(msg, skb);
549 }
550
551 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
552
553 /* Now we could try to dump offended packet options */
554
555 msg->msg_flags |= MSG_ERRQUEUE;
556 err = copied;
557
Eric Dumazet960a2622016-04-21 22:27:32 -0700558 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559out:
560 return err;
561}
562
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +0200563static void __ip_sock_set_tos(struct sock *sk, int val)
564{
565 if (sk->sk_type == SOCK_STREAM) {
566 val &= ~INET_ECN_MASK;
567 val |= inet_sk(sk)->tos & INET_ECN_MASK;
568 }
569 if (inet_sk(sk)->tos != val) {
570 inet_sk(sk)->tos = val;
571 sk->sk_priority = rt_tos2priority(val);
572 sk_dst_reset(sk);
573 }
574}
575
576void ip_sock_set_tos(struct sock *sk, int val)
577{
578 lock_sock(sk);
579 __ip_sock_set_tos(sk, val);
580 release_sock(sk);
581}
582EXPORT_SYMBOL(ip_sock_set_tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Christoph Hellwigc4e446b2020-05-28 07:12:27 +0200584void ip_sock_set_freebind(struct sock *sk)
585{
586 lock_sock(sk);
587 inet_sk(sk)->freebind = true;
588 release_sock(sk);
589}
590EXPORT_SYMBOL(ip_sock_set_freebind);
591
Christoph Hellwigdb45c0e2020-05-28 07:12:28 +0200592void ip_sock_set_recverr(struct sock *sk)
593{
594 lock_sock(sk);
595 inet_sk(sk)->recverr = true;
596 release_sock(sk);
597}
598EXPORT_SYMBOL(ip_sock_set_recverr);
599
Christoph Hellwig2de569b2020-05-28 07:12:29 +0200600int ip_sock_set_mtu_discover(struct sock *sk, int val)
601{
602 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
603 return -EINVAL;
604 lock_sock(sk);
605 inet_sk(sk)->pmtudisc = val;
606 release_sock(sk);
607 return 0;
608}
609EXPORT_SYMBOL(ip_sock_set_mtu_discover);
610
Christoph Hellwigc1f9ec52020-05-28 07:12:30 +0200611void ip_sock_set_pktinfo(struct sock *sk)
612{
613 lock_sock(sk);
614 inet_sk(sk)->cmsg_flags |= IP_CMSG_PKTINFO;
615 release_sock(sk);
616}
617EXPORT_SYMBOL(ip_sock_set_pktinfo);
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700620 * Socket option code for IP. This is the end of the line after any
621 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300623static bool setsockopt_needs_rtnl(int optname)
624{
625 switch (optname) {
626 case IP_ADD_MEMBERSHIP:
627 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300628 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300629 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300630 case IP_DROP_SOURCE_MEMBERSHIP:
631 case IP_MSFILTER:
632 case IP_UNBLOCK_SOURCE:
633 case MCAST_BLOCK_SOURCE:
634 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300635 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300636 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300637 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300638 case MCAST_LEAVE_SOURCE_GROUP:
639 case MCAST_UNBLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300640 return true;
641 }
642 return false;
643}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Al Viroe986d4d2020-03-29 22:37:56 -0400645static int set_mcast_msfilter(struct sock *sk, int ifindex,
646 int numsrc, int fmode,
647 struct sockaddr_storage *group,
648 struct sockaddr_storage *list)
649{
650 int msize = IP_MSFILTER_SIZE(numsrc);
651 struct ip_msfilter *msf;
652 struct sockaddr_in *psin;
653 int err, i;
654
655 msf = kmalloc(msize, GFP_KERNEL);
656 if (!msf)
657 return -ENOBUFS;
658
659 psin = (struct sockaddr_in *)group;
660 if (psin->sin_family != AF_INET)
661 goto Eaddrnotavail;
662 msf->imsf_multiaddr = psin->sin_addr.s_addr;
663 msf->imsf_interface = 0;
664 msf->imsf_fmode = fmode;
665 msf->imsf_numsrc = numsrc;
666 for (i = 0; i < numsrc; ++i) {
667 psin = (struct sockaddr_in *)&list[i];
668
669 if (psin->sin_family != AF_INET)
670 goto Eaddrnotavail;
671 msf->imsf_slist[i] = psin->sin_addr.s_addr;
672 }
673 err = ip_mc_msfilter(sk, msf, ifindex);
674 kfree(msf);
675 return err;
676
677Eaddrnotavail:
678 kfree(msf);
679 return -EADDRNOTAVAIL;
680}
681
Al Viro2bbf8c12020-04-27 10:49:26 -0400682static int do_mcast_group_source(struct sock *sk, int optname,
683 struct group_source_req *greqs)
684{
685 struct ip_mreq_source mreqs;
686 struct sockaddr_in *psin;
687 int omode, add, err;
688
689 if (greqs->gsr_group.ss_family != AF_INET ||
690 greqs->gsr_source.ss_family != AF_INET)
691 return -EADDRNOTAVAIL;
692
693 psin = (struct sockaddr_in *)&greqs->gsr_group;
694 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
695 psin = (struct sockaddr_in *)&greqs->gsr_source;
696 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
697 mreqs.imr_interface = 0; /* use index for mc_source */
698
699 if (optname == MCAST_BLOCK_SOURCE) {
700 omode = MCAST_EXCLUDE;
701 add = 1;
702 } else if (optname == MCAST_UNBLOCK_SOURCE) {
703 omode = MCAST_EXCLUDE;
704 add = 0;
705 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
706 struct ip_mreqn mreq;
707
708 psin = (struct sockaddr_in *)&greqs->gsr_group;
709 mreq.imr_multiaddr = psin->sin_addr;
710 mreq.imr_address.s_addr = 0;
711 mreq.imr_ifindex = greqs->gsr_interface;
712 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
713 if (err && err != -EADDRINUSE)
714 return err;
715 greqs->gsr_interface = mreq.imr_ifindex;
716 omode = MCAST_INCLUDE;
717 add = 1;
718 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
719 omode = MCAST_INCLUDE;
720 add = 0;
721 }
722 return ip_mc_source(add, omode, sk, &mreqs, greqs->gsr_interface);
723}
724
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200725static int ip_set_mcast_msfilter(struct sock *sk, void __user *optval,
726 int optlen)
727{
728 struct group_filter *gsf = NULL;
729 int err;
730
731 if (optlen < GROUP_FILTER_SIZE(0))
732 return -EINVAL;
733 if (optlen > sysctl_optmem_max)
734 return -ENOBUFS;
735
736 gsf = memdup_user(optval, optlen);
737 if (IS_ERR(gsf))
738 return PTR_ERR(gsf);
739
740 /* numsrc >= (4G-140)/128 overflow in 32 bits */
741 err = -ENOBUFS;
742 if (gsf->gf_numsrc >= 0x1ffffff ||
743 gsf->gf_numsrc > sock_net(sk)->ipv4.sysctl_igmp_max_msf)
744 goto out_free_gsf;
745
746 err = -EINVAL;
747 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen)
748 goto out_free_gsf;
749
750 err = set_mcast_msfilter(sk, gsf->gf_interface, gsf->gf_numsrc,
751 gsf->gf_fmode, &gsf->gf_group, gsf->gf_slist);
752out_free_gsf:
753 kfree(gsf);
754 return err;
755}
756
757#ifdef CONFIG_COMPAT
758static int compat_ip_set_mcast_msfilter(struct sock *sk, void __user *optval,
759 int optlen)
760{
761 const int size0 = offsetof(struct compat_group_filter, gf_slist);
762 struct compat_group_filter *gf32;
763 unsigned int n;
764 void *p;
765 int err;
766
767 if (optlen < size0)
768 return -EINVAL;
769 if (optlen > sysctl_optmem_max - 4)
770 return -ENOBUFS;
771
772 p = kmalloc(optlen + 4, GFP_KERNEL);
773 if (!p)
774 return -ENOMEM;
775 gf32 = p + 4; /* we want ->gf_group and ->gf_slist aligned */
776
777 err = -EFAULT;
778 if (copy_from_user(gf32, optval, optlen))
779 goto out_free_gsf;
780
781 /* numsrc >= (4G-140)/128 overflow in 32 bits */
782 n = gf32->gf_numsrc;
783 err = -ENOBUFS;
784 if (n >= 0x1ffffff)
785 goto out_free_gsf;
786
787 err = -EINVAL;
788 if (offsetof(struct compat_group_filter, gf_slist[n]) > optlen)
789 goto out_free_gsf;
790
791 rtnl_lock();
792 lock_sock(sk);
793
794 /* numsrc >= (4G-140)/128 overflow in 32 bits */
795 err = -ENOBUFS;
796 if (n > sock_net(sk)->ipv4.sysctl_igmp_max_msf)
797 goto out_unlock;
798 err = set_mcast_msfilter(sk, gf32->gf_interface, n, gf32->gf_fmode,
799 &gf32->gf_group, gf32->gf_slist);
800out_unlock:
801 release_sock(sk);
802 rtnl_unlock();
803out_free_gsf:
804 kfree(p);
805 return err;
806}
807#endif
808
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800809static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700810 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 struct inet_sock *inet = inet_sk(sk);
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200813 struct net *net = sock_net(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800814 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300815 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Xi Wang0c9f79b2012-11-11 11:20:01 +0000817 switch (optname) {
818 case IP_PKTINFO:
819 case IP_RECVTTL:
820 case IP_RECVOPTS:
821 case IP_RECVTOS:
822 case IP_RETOPTS:
823 case IP_TOS:
824 case IP_TTL:
825 case IP_HDRINCL:
826 case IP_MTU_DISCOVER:
827 case IP_RECVERR:
828 case IP_ROUTER_ALERT:
829 case IP_FREEBIND:
830 case IP_PASSSEC:
831 case IP_TRANSPARENT:
832 case IP_MINTTL:
833 case IP_NODEFRAG:
Eric Dumazet90c337d2015-06-06 21:17:57 -0700834 case IP_BIND_ADDRESS_NO_PORT:
Xi Wang0c9f79b2012-11-11 11:20:01 +0000835 case IP_UNICAST_IF:
836 case IP_MULTICAST_TTL:
837 case IP_MULTICAST_ALL:
838 case IP_MULTICAST_LOOP:
839 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800840 case IP_CHECKSUM:
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400841 case IP_RECVFRAGSIZE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (optlen >= sizeof(int)) {
843 if (get_user(val, (int __user *) optval))
844 return -EFAULT;
845 } else if (optlen >= sizeof(char)) {
846 unsigned char ucval;
847
848 if (get_user(ucval, (unsigned char __user *) optval))
849 return -EFAULT;
850 val = (int) ucval;
851 }
852 }
853
854 /* If optlen==0, it is equivalent to val == 0 */
855
Kirill Tkhai05269472018-03-22 12:45:12 +0300856 if (optname == IP_ROUTER_ALERT)
857 return ip_ra_control(sk, val ? 1 : 0, NULL);
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800858 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800859 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300862 if (needs_rtnl)
863 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 lock_sock(sk);
865
866 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800867 case IP_OPTIONS:
868 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000869 struct ip_options_rcu *old, *opt = NULL;
870
roel kluin65a1c4f2009-10-23 05:59:21 +0000871 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800872 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900873 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700874 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800875 if (err)
876 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000877 old = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200878 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800879 if (inet->is_icsk) {
880 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000881#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800882 if (sk->sk_family == PF_INET ||
883 (!((1 << sk->sk_state) &
884 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000885 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000887 if (old)
888 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800889 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000890 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800891 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000892#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000896 rcu_assign_pointer(inet->inet_opt, opt);
897 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800898 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800899 break;
900 }
901 case IP_PKTINFO:
902 if (val)
903 inet->cmsg_flags |= IP_CMSG_PKTINFO;
904 else
905 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
906 break;
907 case IP_RECVTTL:
908 if (val)
909 inet->cmsg_flags |= IP_CMSG_TTL;
910 else
911 inet->cmsg_flags &= ~IP_CMSG_TTL;
912 break;
913 case IP_RECVTOS:
914 if (val)
915 inet->cmsg_flags |= IP_CMSG_TOS;
916 else
917 inet->cmsg_flags &= ~IP_CMSG_TOS;
918 break;
919 case IP_RECVOPTS:
920 if (val)
921 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
922 else
923 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
924 break;
925 case IP_RETOPTS:
926 if (val)
927 inet->cmsg_flags |= IP_CMSG_RETOPTS;
928 else
929 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
930 break;
931 case IP_PASSSEC:
932 if (val)
933 inet->cmsg_flags |= IP_CMSG_PASSSEC;
934 else
935 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
936 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800937 case IP_RECVORIGDSTADDR:
938 if (val)
939 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
940 else
941 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
942 break;
Tom Herbertad6f9392015-01-05 13:56:17 -0800943 case IP_CHECKSUM:
944 if (val) {
945 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
946 inet_inc_convert_csum(sk);
947 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
948 }
949 } else {
950 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
951 inet_dec_convert_csum(sk);
952 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
953 }
954 }
955 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400956 case IP_RECVFRAGSIZE:
957 if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM)
958 goto e_inval;
959 if (val)
960 inet->cmsg_flags |= IP_CMSG_RECVFRAGSIZE;
961 else
962 inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE;
963 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800964 case IP_TOS: /* This sets both TOS and Precedence */
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +0200965 __ip_sock_set_tos(sk, val);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800966 break;
967 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700968 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800969 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000970 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800971 goto e_inval;
972 inet->uc_ttl = val;
973 break;
974 case IP_HDRINCL:
975 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800978 }
979 inet->hdrincl = val ? 1 : 0;
980 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000981 case IP_NODEFRAG:
982 if (sk->sk_type != SOCK_RAW) {
983 err = -ENOPROTOOPT;
984 break;
985 }
986 inet->nodefrag = val ? 1 : 0;
987 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -0700988 case IP_BIND_ADDRESS_NO_PORT:
989 inet->bind_address_no_port = val ? 1 : 0;
990 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800991 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100992 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800993 goto e_inval;
994 inet->pmtudisc = val;
995 break;
996 case IP_RECVERR:
997 inet->recverr = !!val;
998 if (!val)
999 skb_queue_purge(&sk->sk_error_queue);
1000 break;
1001 case IP_MULTICAST_TTL:
1002 if (sk->sk_type == SOCK_STREAM)
1003 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001004 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001005 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001006 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001007 val = 1;
1008 if (val < 0 || val > 255)
1009 goto e_inval;
1010 inet->mc_ttl = val;
1011 break;
1012 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001013 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001014 goto e_inval;
1015 inet->mc_loop = !!val;
1016 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001017 case IP_UNICAST_IF:
1018 {
1019 struct net_device *dev = NULL;
1020 int ifindex;
David Ahern9515a2e2018-01-24 19:37:38 -08001021 int midx;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001022
1023 if (optlen != sizeof(int))
1024 goto e_inval;
1025
1026 ifindex = (__force int)ntohl((__force __be32)val);
1027 if (ifindex == 0) {
1028 inet->uc_index = 0;
1029 err = 0;
1030 break;
1031 }
1032
1033 dev = dev_get_by_index(sock_net(sk), ifindex);
1034 err = -EADDRNOTAVAIL;
1035 if (!dev)
1036 break;
David Ahern9515a2e2018-01-24 19:37:38 -08001037
1038 midx = l3mdev_master_ifindex(dev);
Erich E. Hoover76e21052012-02-08 09:11:07 +00001039 dev_put(dev);
1040
1041 err = -EINVAL;
David Ahern9515a2e2018-01-24 19:37:38 -08001042 if (sk->sk_bound_dev_if &&
1043 (!midx || midx != sk->sk_bound_dev_if))
Erich E. Hoover76e21052012-02-08 09:11:07 +00001044 break;
1045
1046 inet->uc_index = ifindex;
1047 err = 0;
1048 break;
1049 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001050 case IP_MULTICAST_IF:
1051 {
1052 struct ip_mreqn mreq;
1053 struct net_device *dev = NULL;
David Ahern7bb387c2016-12-29 15:39:37 -08001054 int midx;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001055
1056 if (sk->sk_type == SOCK_STREAM)
1057 goto e_inval;
1058 /*
1059 * Check the arguments are allowable
1060 */
1061
Shan Wei09159212009-09-22 15:41:10 +00001062 if (optlen < sizeof(struct in_addr))
1063 goto e_inval;
1064
Stephen Hemminger132adf52007-03-08 20:44:43 -08001065 err = -EFAULT;
1066 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -08001067 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001068 break;
1069 } else {
1070 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +00001071 if (optlen >= sizeof(struct ip_mreq)) {
1072 if (copy_from_user(&mreq, optval,
1073 sizeof(struct ip_mreq)))
1074 break;
1075 } else if (optlen >= sizeof(struct in_addr)) {
1076 if (copy_from_user(&mreq.imr_address, optval,
1077 sizeof(struct in_addr)))
1078 break;
1079 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001080 }
1081
1082 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -07001083 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001084 inet->mc_index = 0;
1085 inet->mc_addr = 0;
1086 err = 0;
1087 break;
1088 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001089 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +00001090 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001091 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001092 } else
Eric Dumazet55b80502009-10-19 06:41:58 +00001093 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001094
1095
1096 err = -EADDRNOTAVAIL;
1097 if (!dev)
1098 break;
David Ahern7bb387c2016-12-29 15:39:37 -08001099
1100 midx = l3mdev_master_ifindex(dev);
1101
Eric Dumazet55b80502009-10-19 06:41:58 +00001102 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001103
1104 err = -EINVAL;
1105 if (sk->sk_bound_dev_if &&
David Ahern7bb387c2016-12-29 15:39:37 -08001106 mreq.imr_ifindex != sk->sk_bound_dev_if &&
1107 (!midx || midx != sk->sk_bound_dev_if))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001108 break;
1109
1110 inet->mc_index = mreq.imr_ifindex;
1111 inet->mc_addr = mreq.imr_address.s_addr;
1112 err = 0;
1113 break;
1114 }
1115
1116 case IP_ADD_MEMBERSHIP:
1117 case IP_DROP_MEMBERSHIP:
1118 {
1119 struct ip_mreqn mreq;
1120
Flavio Leitnera96fb492007-08-24 22:16:39 -07001121 err = -EPROTO;
1122 if (inet_sk(sk)->is_icsk)
1123 break;
1124
Stephen Hemminger132adf52007-03-08 20:44:43 -08001125 if (optlen < sizeof(struct ip_mreq))
1126 goto e_inval;
1127 err = -EFAULT;
1128 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -08001129 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001130 break;
1131 } else {
1132 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -08001133 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001134 break;
1135 }
1136
1137 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001138 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001139 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001140 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001141 break;
1142 }
1143 case IP_MSFILTER:
1144 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001145 struct ip_msfilter *msf;
1146
1147 if (optlen < IP_MSFILTER_SIZE(0))
1148 goto e_inval;
1149 if (optlen > sysctl_optmem_max) {
1150 err = -ENOBUFS;
1151 break;
1152 }
Al Viroa2c841d2017-05-13 18:26:06 -04001153 msf = memdup_user(optval, optlen);
1154 if (IS_ERR(msf)) {
1155 err = PTR_ERR(msf);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001156 break;
1157 }
1158 /* numsrc >= (1G-4) overflow in 32 bits */
1159 if (msf->imsf_numsrc >= 0x3ffffffcU ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +02001160 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001161 kfree(msf);
1162 err = -ENOBUFS;
1163 break;
1164 }
1165 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
1166 kfree(msf);
1167 err = -EINVAL;
1168 break;
1169 }
1170 err = ip_mc_msfilter(sk, msf, 0);
1171 kfree(msf);
1172 break;
1173 }
1174 case IP_BLOCK_SOURCE:
1175 case IP_UNBLOCK_SOURCE:
1176 case IP_ADD_SOURCE_MEMBERSHIP:
1177 case IP_DROP_SOURCE_MEMBERSHIP:
1178 {
1179 struct ip_mreq_source mreqs;
1180 int omode, add;
1181
1182 if (optlen != sizeof(struct ip_mreq_source))
1183 goto e_inval;
1184 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
1185 err = -EFAULT;
1186 break;
1187 }
1188 if (optname == IP_BLOCK_SOURCE) {
1189 omode = MCAST_EXCLUDE;
1190 add = 1;
1191 } else if (optname == IP_UNBLOCK_SOURCE) {
1192 omode = MCAST_EXCLUDE;
1193 add = 0;
1194 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
1195 struct ip_mreqn mreq;
1196
1197 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
1198 mreq.imr_address.s_addr = mreqs.imr_interface;
1199 mreq.imr_ifindex = 0;
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001200 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001201 if (err && err != -EADDRINUSE)
1202 break;
1203 omode = MCAST_INCLUDE;
1204 add = 1;
1205 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
1206 omode = MCAST_INCLUDE;
1207 add = 0;
1208 }
1209 err = ip_mc_source(add, omode, sk, &mreqs, 0);
1210 break;
1211 }
1212 case MCAST_JOIN_GROUP:
1213 case MCAST_LEAVE_GROUP:
1214 {
1215 struct group_req greq;
1216 struct sockaddr_in *psin;
1217 struct ip_mreqn mreq;
1218
1219 if (optlen < sizeof(struct group_req))
1220 goto e_inval;
1221 err = -EFAULT;
1222 if (copy_from_user(&greq, optval, sizeof(greq)))
1223 break;
1224 psin = (struct sockaddr_in *)&greq.gr_group;
1225 if (psin->sin_family != AF_INET)
1226 goto e_inval;
1227 memset(&mreq, 0, sizeof(mreq));
1228 mreq.imr_multiaddr = psin->sin_addr;
1229 mreq.imr_ifindex = greq.gr_interface;
1230
1231 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001232 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001233 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001234 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001235 break;
1236 }
1237 case MCAST_JOIN_SOURCE_GROUP:
1238 case MCAST_LEAVE_SOURCE_GROUP:
1239 case MCAST_BLOCK_SOURCE:
1240 case MCAST_UNBLOCK_SOURCE:
1241 {
1242 struct group_source_req greqs;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001243
1244 if (optlen != sizeof(struct group_source_req))
1245 goto e_inval;
1246 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
1247 err = -EFAULT;
1248 break;
1249 }
Al Viro2bbf8c12020-04-27 10:49:26 -04001250 err = do_mcast_group_source(sk, optname, &greqs);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001251 break;
1252 }
1253 case MCAST_MSFILTER:
Christoph Hellwigd62c38f2020-07-17 08:23:24 +02001254 err = ip_set_mcast_msfilter(sk, optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001255 break;
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001256 case IP_MULTICAST_ALL:
1257 if (optlen < 1)
1258 goto e_inval;
1259 if (val != 0 && val != 1)
1260 goto e_inval;
1261 inet->mc_all = val;
1262 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001263
1264 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001265 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001266 goto e_inval;
1267 inet->freebind = !!val;
1268 break;
1269
1270 case IP_IPSEC_POLICY:
1271 case IP_XFRM_POLICY:
1272 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001273 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001274 break;
1275 err = xfrm_user_policy(sk, optname, optval, optlen);
1276 break;
1277
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001278 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001279 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1280 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001281 err = -EPERM;
1282 break;
1283 }
1284 if (optlen < 1)
1285 goto e_inval;
1286 inet->transparent = !!val;
1287 break;
1288
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001289 case IP_MINTTL:
1290 if (optlen < 1)
1291 goto e_inval;
1292 if (val < 0 || val > 255)
1293 goto e_inval;
1294 inet->min_ttl = val;
1295 break;
1296
Stephen Hemminger132adf52007-03-08 20:44:43 -08001297 default:
1298 err = -ENOPROTOOPT;
1299 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001302 if (needs_rtnl)
1303 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return err;
1305
1306e_inval:
1307 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001308 if (needs_rtnl)
1309 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return -EINVAL;
1311}
1312
Eric Dumazetf84af322010-04-28 15:31:51 -07001313/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001314 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001315 * @sk: socket
1316 * @skb: buffer
1317 *
David S. Miller35ebf652012-06-28 03:59:11 -07001318 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1319 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001320 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001321 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001322void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001323{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001324 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001325 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1326 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001327
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001328 if (prepare && skb_rtable(skb)) {
David Ahern0b922b72016-05-10 11:19:51 -07001329 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1330 * which has interface index (iif) as the first member of the
1331 * underlying inet{6}_skb_parm struct. This code then overlays
1332 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001333 * element so the iif is picked up from the prior IPCB. If iif
1334 * is the loopback interface, then return the sending interface
1335 * (e.g., process binds socket to eth0 for Tx which is
1336 * redirected to loopback in the rtable/dst).
David Ahern0b922b72016-05-10 11:19:51 -07001337 */
David Aherncbea8f02017-09-13 17:11:37 -07001338 struct rtable *rt = skb_rtable(skb);
1339 bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags);
1340
1341 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001342 pktinfo->ipi_ifindex = inet_iif(skb);
David Aherncbea8f02017-09-13 17:11:37 -07001343 else if (l3slave && rt && rt->rt_iif)
1344 pktinfo->ipi_ifindex = rt->rt_iif;
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001345
David S. Miller35ebf652012-06-28 03:59:11 -07001346 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001347 } else {
1348 pktinfo->ipi_ifindex = 0;
1349 pktinfo->ipi_spec_dst.s_addr = 0;
1350 }
Paolo Abeni61a10302017-08-03 18:07:07 +02001351 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001352}
Eric Dumazetf84af322010-04-28 15:31:51 -07001353
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001354int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001355 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001356{
1357 int err;
1358
1359 if (level != SOL_IP)
1360 return -ENOPROTOOPT;
1361
1362 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
Taehee Yoo97adadd2018-11-05 22:31:41 +09001363#if IS_ENABLED(CONFIG_BPFILTER_UMH)
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001364 if (optname >= BPFILTER_IPT_SO_SET_REPLACE &&
1365 optname < BPFILTER_IPT_SET_MAX)
1366 err = bpfilter_ip_set_sockopt(sk, optname, optval, optlen);
1367#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001368#ifdef CONFIG_NETFILTER
1369 /* we need to exclude all possible ENOPROTOOPTs except default case */
1370 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001371 optname != IP_IPSEC_POLICY &&
1372 optname != IP_XFRM_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001373 !ip_mroute_opt(optname))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001374 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001375#endif
1376 return err;
1377}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001378EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001379
1380#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001381int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001382 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001383{
1384 int err;
1385
1386 if (level != SOL_IP)
1387 return -ENOPROTOOPT;
1388
Al Viroe9c375f2020-05-09 21:16:36 -04001389 switch (optname) {
1390 case MCAST_JOIN_GROUP:
1391 case MCAST_LEAVE_GROUP:
Al Viro2f984f12020-04-26 19:56:22 -04001392 {
1393 struct compat_group_req __user *gr32 = (void __user *)optval;
1394 struct group_req greq;
1395 struct sockaddr_in *psin = (struct sockaddr_in *)&greq.gr_group;
1396 struct ip_mreqn mreq;
1397
1398 if (optlen < sizeof(struct compat_group_req))
1399 return -EINVAL;
1400
1401 if (get_user(greq.gr_interface, &gr32->gr_interface) ||
1402 copy_from_user(&greq.gr_group, &gr32->gr_group,
1403 sizeof(greq.gr_group)))
1404 return -EFAULT;
1405
1406 if (psin->sin_family != AF_INET)
1407 return -EINVAL;
1408
1409 memset(&mreq, 0, sizeof(mreq));
1410 mreq.imr_multiaddr = psin->sin_addr;
1411 mreq.imr_ifindex = greq.gr_interface;
1412
1413 rtnl_lock();
1414 lock_sock(sk);
1415 if (optname == MCAST_JOIN_GROUP)
1416 err = ip_mc_join_group(sk, &mreq);
1417 else
1418 err = ip_mc_leave_group(sk, &mreq);
1419 release_sock(sk);
1420 rtnl_unlock();
1421 return err;
1422 }
Al Viroe9c375f2020-05-09 21:16:36 -04001423 case MCAST_JOIN_SOURCE_GROUP:
1424 case MCAST_LEAVE_SOURCE_GROUP:
1425 case MCAST_BLOCK_SOURCE:
1426 case MCAST_UNBLOCK_SOURCE:
Al Virob212c322020-04-27 11:37:02 -04001427 {
1428 struct compat_group_source_req __user *gsr32 = (void __user *)optval;
1429 struct group_source_req greqs;
1430
1431 if (optlen != sizeof(struct compat_group_source_req))
1432 return -EINVAL;
1433
1434 if (get_user(greqs.gsr_interface, &gsr32->gsr_interface) ||
1435 copy_from_user(&greqs.gsr_group, &gsr32->gsr_group,
1436 sizeof(greqs.gsr_group)) ||
1437 copy_from_user(&greqs.gsr_source, &gsr32->gsr_source,
1438 sizeof(greqs.gsr_source)))
1439 return -EFAULT;
1440
1441 rtnl_lock();
1442 lock_sock(sk);
1443 err = do_mcast_group_source(sk, optname, &greqs);
1444 release_sock(sk);
1445 rtnl_unlock();
1446 return err;
1447 }
Al Viro2e041722020-03-30 15:39:43 -04001448 case MCAST_MSFILTER:
Christoph Hellwigd62c38f2020-07-17 08:23:24 +02001449 return compat_ip_set_mcast_msfilter(sk, optval, optlen);
Al Viroe9c375f2020-05-09 21:16:36 -04001450 }
David L Stevensdae50292008-04-27 01:06:07 -07001451
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001452 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1453#ifdef CONFIG_NETFILTER
1454 /* we need to exclude all possible ENOPROTOOPTs except default case */
1455 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001456 optname != IP_IPSEC_POLICY &&
1457 optname != IP_XFRM_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001458 !ip_mroute_opt(optname))
Christoph Hellwig77d4df42020-07-17 08:23:20 +02001459 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001460#endif
1461 return err;
1462}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001463EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001464#endif
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001467 * Get the options. Note for future reference. The GET of IP options gets
1468 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 */
1470
WANG Cong87e9f032015-11-03 15:41:16 -08001471static bool getsockopt_needs_rtnl(int optname)
1472{
1473 switch (optname) {
1474 case IP_MSFILTER:
1475 case MCAST_MSFILTER:
1476 return true;
1477 }
1478 return false;
1479}
1480
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001481static int ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
1482 int __user *optlen, int len)
1483{
1484 const int size0 = offsetof(struct group_filter, gf_slist);
1485 struct group_filter __user *p = optval;
1486 struct group_filter gsf;
1487 int num;
1488 int err;
1489
1490 if (len < size0)
1491 return -EINVAL;
1492 if (copy_from_user(&gsf, p, size0))
1493 return -EFAULT;
1494
1495 num = gsf.gf_numsrc;
1496 err = ip_mc_gsfget(sk, &gsf, p->gf_slist);
1497 if (err)
1498 return err;
1499 if (gsf.gf_numsrc < num)
1500 num = gsf.gf_numsrc;
1501 if (put_user(GROUP_FILTER_SIZE(num), optlen) ||
1502 copy_to_user(p, &gsf, size0))
1503 return -EFAULT;
1504 return 0;
1505}
1506
1507#ifdef CONFIG_COMPAT
1508static int compat_ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
1509 int __user *optlen)
1510{
1511 const int size0 = offsetof(struct compat_group_filter, gf_slist);
1512 struct compat_group_filter __user *p = optval;
1513 struct compat_group_filter gf32;
1514 struct group_filter gf;
1515 int len, err;
1516 int num;
1517
1518 if (get_user(len, optlen))
1519 return -EFAULT;
1520 if (len < size0)
1521 return -EINVAL;
1522
1523 if (copy_from_user(&gf32, p, size0))
1524 return -EFAULT;
1525
1526 gf.gf_interface = gf32.gf_interface;
1527 gf.gf_fmode = gf32.gf_fmode;
1528 num = gf.gf_numsrc = gf32.gf_numsrc;
1529 gf.gf_group = gf32.gf_group;
1530
1531 rtnl_lock();
1532 lock_sock(sk);
1533 err = ip_mc_gsfget(sk, &gf, p->gf_slist);
1534 release_sock(sk);
1535 rtnl_unlock();
1536 if (err)
1537 return err;
1538 if (gf.gf_numsrc < num)
1539 num = gf.gf_numsrc;
1540 len = GROUP_FILTER_SIZE(num) - (sizeof(gf) - sizeof(gf32));
1541 if (put_user(len, optlen) ||
1542 put_user(gf.gf_fmode, &p->gf_fmode) ||
1543 put_user(gf.gf_numsrc, &p->gf_numsrc))
1544 return -EFAULT;
1545 return 0;
1546}
1547#endif
1548
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001549static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001550 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 struct inet_sock *inet = inet_sk(sk);
WANG Cong87e9f032015-11-03 15:41:16 -08001553 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1554 int val, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001556
Stephen Hemminger132adf52007-03-08 20:44:43 -08001557 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return -EOPNOTSUPP;
1559
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001560 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001561 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Jianjun Kong09cb1052008-11-03 00:27:11 -08001563 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001565 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001567
WANG Cong87e9f032015-11-03 15:41:16 -08001568 if (needs_rtnl)
1569 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 lock_sock(sk);
1571
Stephen Hemminger132adf52007-03-08 20:44:43 -08001572 switch (optname) {
1573 case IP_OPTIONS:
1574 {
1575 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001576 struct ip_options *opt = (struct ip_options *)optbuf;
1577 struct ip_options_rcu *inet_opt;
1578
1579 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001580 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001581 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001582 if (inet_opt)
1583 memcpy(optbuf, &inet_opt->opt,
1584 sizeof(struct ip_options) +
1585 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001586 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Stephen Hemminger132adf52007-03-08 20:44:43 -08001588 if (opt->optlen == 0)
1589 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Stephen Hemminger132adf52007-03-08 20:44:43 -08001591 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Stephen Hemminger132adf52007-03-08 20:44:43 -08001593 len = min_t(unsigned int, len, opt->optlen);
1594 if (put_user(len, optlen))
1595 return -EFAULT;
1596 if (copy_to_user(optval, opt->__data, len))
1597 return -EFAULT;
1598 return 0;
1599 }
1600 case IP_PKTINFO:
1601 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1602 break;
1603 case IP_RECVTTL:
1604 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1605 break;
1606 case IP_RECVTOS:
1607 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1608 break;
1609 case IP_RECVOPTS:
1610 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1611 break;
1612 case IP_RETOPTS:
1613 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1614 break;
1615 case IP_PASSSEC:
1616 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1617 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001618 case IP_RECVORIGDSTADDR:
1619 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1620 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001621 case IP_CHECKSUM:
1622 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1623 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -04001624 case IP_RECVFRAGSIZE:
1625 val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0;
1626 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001627 case IP_TOS:
1628 val = inet->tos;
1629 break;
1630 case IP_TTL:
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001631 {
1632 struct net *net = sock_net(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001633 val = (inet->uc_ttl == -1 ?
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001634 net->ipv4.sysctl_ip_default_ttl :
Stephen Hemminger132adf52007-03-08 20:44:43 -08001635 inet->uc_ttl);
1636 break;
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001637 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001638 case IP_HDRINCL:
1639 val = inet->hdrincl;
1640 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001641 case IP_NODEFRAG:
1642 val = inet->nodefrag;
1643 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001644 case IP_BIND_ADDRESS_NO_PORT:
1645 val = inet->bind_address_no_port;
1646 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001647 case IP_MTU_DISCOVER:
1648 val = inet->pmtudisc;
1649 break;
1650 case IP_MTU:
1651 {
1652 struct dst_entry *dst;
1653 val = 0;
1654 dst = sk_dst_get(sk);
1655 if (dst) {
1656 val = dst_mtu(dst);
1657 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001659 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001661 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001663 break;
1664 }
1665 case IP_RECVERR:
1666 val = inet->recverr;
1667 break;
1668 case IP_MULTICAST_TTL:
1669 val = inet->mc_ttl;
1670 break;
1671 case IP_MULTICAST_LOOP:
1672 val = inet->mc_loop;
1673 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001674 case IP_UNICAST_IF:
1675 val = (__force int)htonl((__u32) inet->uc_index);
1676 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001677 case IP_MULTICAST_IF:
1678 {
1679 struct in_addr addr;
1680 len = min_t(unsigned int, len, sizeof(struct in_addr));
1681 addr.s_addr = inet->mc_addr;
1682 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Stephen Hemminger132adf52007-03-08 20:44:43 -08001684 if (put_user(len, optlen))
1685 return -EFAULT;
1686 if (copy_to_user(optval, &addr, len))
1687 return -EFAULT;
1688 return 0;
1689 }
1690 case IP_MSFILTER:
1691 {
1692 struct ip_msfilter msf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001693
1694 if (len < IP_MSFILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001695 err = -EINVAL;
1696 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001698 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001699 err = -EFAULT;
1700 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001702 err = ip_mc_msfget(sk, &msf,
1703 (struct ip_msfilter __user *)optval, optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001704 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001705 }
1706 case MCAST_MSFILTER:
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001707 err = ip_get_mcast_msfilter(sk, optval, optlen, len);
WANG Cong87e9f032015-11-03 15:41:16 -08001708 goto out;
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001709 case IP_MULTICAST_ALL:
1710 val = inet->mc_all;
1711 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001712 case IP_PKTOPTIONS:
1713 {
1714 struct msghdr msg;
1715
1716 release_sock(sk);
1717
1718 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001720
Christoph Hellwig1f466e12020-05-11 13:59:13 +02001721 msg.msg_control_is_user = true;
1722 msg.msg_control_user = optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001723 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001724 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001725
1726 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1727 struct in_pktinfo info;
1728
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001729 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1730 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001731 info.ipi_ifindex = inet->mc_index;
1732 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1733 }
1734 if (inet->cmsg_flags & IP_CMSG_TTL) {
1735 int hlim = inet->mc_ttl;
1736 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1737 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001738 if (inet->cmsg_flags & IP_CMSG_TOS) {
1739 int tos = inet->rcv_tos;
1740 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1741 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001742 len -= msg.msg_controllen;
1743 return put_user(len, optlen);
1744 }
1745 case IP_FREEBIND:
1746 val = inet->freebind;
1747 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001748 case IP_TRANSPARENT:
1749 val = inet->transparent;
1750 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001751 case IP_MINTTL:
1752 val = inet->min_ttl;
1753 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001754 default:
1755 release_sock(sk);
1756 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001759
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001760 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 unsigned char ucval = (unsigned char)val;
1762 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001763 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001765 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return -EFAULT;
1767 } else {
1768 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001769 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001771 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return -EFAULT;
1773 }
1774 return 0;
WANG Cong87e9f032015-11-03 15:41:16 -08001775
1776out:
1777 release_sock(sk);
1778 if (needs_rtnl)
1779 rtnl_unlock();
1780 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001783int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001784 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001785{
1786 int err;
1787
Daniel Balutadd23198e2011-08-07 22:31:07 -07001788 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Taehee Yoo97adadd2018-11-05 22:31:41 +09001789#if IS_ENABLED(CONFIG_BPFILTER_UMH)
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001790 if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1791 optname < BPFILTER_IPT_GET_MAX)
1792 err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1793#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001794#ifdef CONFIG_NETFILTER
1795 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001796 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1797 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001798 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001799
Jianjun Kong09cb1052008-11-03 00:27:11 -08001800 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001801 return -EFAULT;
1802
Paolo Abeni01ea3062018-02-08 12:19:00 +01001803 err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001804 if (err >= 0)
1805 err = put_user(len, optlen);
1806 return err;
1807 }
1808#endif
1809 return err;
1810}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001811EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001812
1813#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001814int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1815 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001816{
David L Stevens42908c62008-04-29 03:23:22 -07001817 int err;
1818
Al Viro0dfe6582020-03-29 22:08:59 -04001819 if (optname == MCAST_MSFILTER) {
Al Viro0dfe6582020-03-29 22:08:59 -04001820 if (level != SOL_IP)
1821 return -EOPNOTSUPP;
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001822 return compat_ip_get_mcast_msfilter(sk, optval, optlen);
Al Viro0dfe6582020-03-29 22:08:59 -04001823 }
David L Stevens42908c62008-04-29 03:23:22 -07001824
Daniel Balutadd23198e2011-08-07 22:31:07 -07001825 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1826 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001827
Taehee Yoo97adadd2018-11-05 22:31:41 +09001828#if IS_ENABLED(CONFIG_BPFILTER_UMH)
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001829 if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1830 optname < BPFILTER_IPT_GET_MAX)
1831 err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1832#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001833#ifdef CONFIG_NETFILTER
1834 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001835 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1836 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001837 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001838
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001839 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001840 return -EFAULT;
1841
Christoph Hellwig77d4df42020-07-17 08:23:20 +02001842 err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001843 if (err >= 0)
1844 err = put_user(len, optlen);
1845 return err;
1846 }
1847#endif
1848 return err;
1849}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001850EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001851#endif