blob: a5ea02d7a183eb60e03446c2a54db49f0f038815 [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)) {
Willem de Bruijneba75c52020-07-10 09:29:02 -0400414 if (inet_sk(sk)->recverr_rfc4884)
415 ip_icmp_error_rfc4884(skb, &serr->ee.ee_rfc4884);
416
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300417 skb_reset_transport_header(skb);
418 if (sock_queue_err_skb(sk, skb) == 0)
419 return;
420 }
421 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Al Viro05790162006-09-27 18:33:40 -0700424void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct inet_sock *inet = inet_sk(sk);
427 struct sock_exterr_skb *serr;
428 struct iphdr *iph;
429 struct sk_buff *skb;
430
431 if (!inet->recverr)
432 return;
433
434 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
435 if (!skb)
436 return;
437
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300438 skb_put(skb, sizeof(struct iphdr));
439 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700440 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 iph->daddr = daddr;
442
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900443 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 serr->ee.ee_errno = err;
445 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900446 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 serr->ee.ee_code = 0;
448 serr->ee.ee_pad = 0;
449 serr->ee.ee_info = info;
450 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700451 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 serr->port = port;
453
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700454 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300455 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 if (sock_queue_err_skb(sk, skb))
458 kfree_skb(skb);
459}
460
Julian Anastasov34b99df2015-06-23 08:34:39 +0300461/* For some errors we have valid addr_offset even with zero payload and
462 * zero port. Also, addr_offset should be supported if port is set.
463 */
464static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
465{
466 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
467 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
468}
469
Willem de Bruijnc247f052015-03-07 20:33:22 -0500470/* IPv4 supports cmsg on all imcp errors and some timestamps
471 *
472 * Timestamp code paths do not initialize the fields expected by cmsg:
473 * the PKTINFO fields in skb->cb[]. Fill those in here.
474 */
475static bool ipv4_datagram_support_cmsg(const struct sock *sk,
476 struct sk_buff *skb,
477 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500478{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500479 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500480
Willem de Bruijnc247f052015-03-07 20:33:22 -0500481 if (ee_origin == SO_EE_ORIGIN_ICMP)
482 return true;
483
484 if (ee_origin == SO_EE_ORIGIN_LOCAL)
485 return false;
486
487 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
Willem de Bruijn1862d622017-04-12 19:24:35 -0400488 * timestamp with egress dev. Not possible for packets without iif
Willem de Bruijnc247f052015-03-07 20:33:22 -0500489 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
490 */
Willem de Bruijn1862d622017-04-12 19:24:35 -0400491 info = PKTINFO_SKB_CB(skb);
492 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
493 !info->ipi_ifindex)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500494 return false;
495
496 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500497 return true;
498}
499
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900500/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * Handle MSG_ERRQUEUE
502 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100503int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400506 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100507 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 struct {
509 struct sock_extended_err ee;
510 struct sockaddr_in offender;
511 } errhdr;
512 int err;
513 int copied;
514
515 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400516 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100517 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 goto out;
519
520 copied = skb->len;
521 if (copied > len) {
522 msg->msg_flags |= MSG_TRUNC;
523 copied = len;
524 }
David S. Miller51f3d022014-11-05 16:46:40 -0500525 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700526 if (unlikely(err)) {
527 kfree_skb(skb);
528 return err;
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 sock_recv_timestamp(msg, sk, skb);
531
532 serr = SKB_EXT_ERR(skb);
533
Julian Anastasov34b99df2015-06-23 08:34:39 +0300534 if (sin && ipv4_datagram_support_addr(serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700536 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
537 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 sin->sin_port = serr->port;
539 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100540 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542
543 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
544 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500545 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500546
Willem de Bruijnc247f052015-03-07 20:33:22 -0500547 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700549 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500550 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 ip_cmsg_recv(msg, skb);
552 }
553
554 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
555
556 /* Now we could try to dump offended packet options */
557
558 msg->msg_flags |= MSG_ERRQUEUE;
559 err = copied;
560
Eric Dumazet960a2622016-04-21 22:27:32 -0700561 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562out:
563 return err;
564}
565
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +0200566static void __ip_sock_set_tos(struct sock *sk, int val)
567{
568 if (sk->sk_type == SOCK_STREAM) {
569 val &= ~INET_ECN_MASK;
570 val |= inet_sk(sk)->tos & INET_ECN_MASK;
571 }
572 if (inet_sk(sk)->tos != val) {
573 inet_sk(sk)->tos = val;
574 sk->sk_priority = rt_tos2priority(val);
575 sk_dst_reset(sk);
576 }
577}
578
579void ip_sock_set_tos(struct sock *sk, int val)
580{
581 lock_sock(sk);
582 __ip_sock_set_tos(sk, val);
583 release_sock(sk);
584}
585EXPORT_SYMBOL(ip_sock_set_tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Christoph Hellwigc4e446b2020-05-28 07:12:27 +0200587void ip_sock_set_freebind(struct sock *sk)
588{
589 lock_sock(sk);
590 inet_sk(sk)->freebind = true;
591 release_sock(sk);
592}
593EXPORT_SYMBOL(ip_sock_set_freebind);
594
Christoph Hellwigdb45c0e2020-05-28 07:12:28 +0200595void ip_sock_set_recverr(struct sock *sk)
596{
597 lock_sock(sk);
598 inet_sk(sk)->recverr = true;
599 release_sock(sk);
600}
601EXPORT_SYMBOL(ip_sock_set_recverr);
602
Christoph Hellwig2de569b2020-05-28 07:12:29 +0200603int ip_sock_set_mtu_discover(struct sock *sk, int val)
604{
605 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
606 return -EINVAL;
607 lock_sock(sk);
608 inet_sk(sk)->pmtudisc = val;
609 release_sock(sk);
610 return 0;
611}
612EXPORT_SYMBOL(ip_sock_set_mtu_discover);
613
Christoph Hellwigc1f9ec52020-05-28 07:12:30 +0200614void ip_sock_set_pktinfo(struct sock *sk)
615{
616 lock_sock(sk);
617 inet_sk(sk)->cmsg_flags |= IP_CMSG_PKTINFO;
618 release_sock(sk);
619}
620EXPORT_SYMBOL(ip_sock_set_pktinfo);
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700623 * Socket option code for IP. This is the end of the line after any
624 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300626static bool setsockopt_needs_rtnl(int optname)
627{
628 switch (optname) {
629 case IP_ADD_MEMBERSHIP:
630 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300631 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300632 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300633 case IP_DROP_SOURCE_MEMBERSHIP:
634 case IP_MSFILTER:
635 case IP_UNBLOCK_SOURCE:
636 case MCAST_BLOCK_SOURCE:
637 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300638 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300639 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300640 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300641 case MCAST_LEAVE_SOURCE_GROUP:
642 case MCAST_UNBLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300643 return true;
644 }
645 return false;
646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Al Viroe986d4d2020-03-29 22:37:56 -0400648static int set_mcast_msfilter(struct sock *sk, int ifindex,
649 int numsrc, int fmode,
650 struct sockaddr_storage *group,
651 struct sockaddr_storage *list)
652{
653 int msize = IP_MSFILTER_SIZE(numsrc);
654 struct ip_msfilter *msf;
655 struct sockaddr_in *psin;
656 int err, i;
657
658 msf = kmalloc(msize, GFP_KERNEL);
659 if (!msf)
660 return -ENOBUFS;
661
662 psin = (struct sockaddr_in *)group;
663 if (psin->sin_family != AF_INET)
664 goto Eaddrnotavail;
665 msf->imsf_multiaddr = psin->sin_addr.s_addr;
666 msf->imsf_interface = 0;
667 msf->imsf_fmode = fmode;
668 msf->imsf_numsrc = numsrc;
669 for (i = 0; i < numsrc; ++i) {
670 psin = (struct sockaddr_in *)&list[i];
671
672 if (psin->sin_family != AF_INET)
673 goto Eaddrnotavail;
674 msf->imsf_slist[i] = psin->sin_addr.s_addr;
675 }
676 err = ip_mc_msfilter(sk, msf, ifindex);
677 kfree(msf);
678 return err;
679
680Eaddrnotavail:
681 kfree(msf);
682 return -EADDRNOTAVAIL;
683}
684
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200685static int copy_group_source_from_user(struct group_source_req *greqs,
686 void __user *optval, int optlen)
Al Viro2bbf8c12020-04-27 10:49:26 -0400687{
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200688 if (in_compat_syscall()) {
689 struct compat_group_source_req gr32;
690
691 if (optlen != sizeof(gr32))
692 return -EINVAL;
693 if (copy_from_user(&gr32, optval, sizeof(gr32)))
694 return -EFAULT;
695 greqs->gsr_interface = gr32.gsr_interface;
696 greqs->gsr_group = gr32.gsr_group;
697 greqs->gsr_source = gr32.gsr_source;
698 } else {
699 if (optlen != sizeof(*greqs))
700 return -EINVAL;
701 if (copy_from_user(greqs, optval, sizeof(*greqs)))
702 return -EFAULT;
703 }
704
705 return 0;
706}
707
708static int do_mcast_group_source(struct sock *sk, int optname,
709 void __user *optval, int optlen)
710{
711 struct group_source_req greqs;
Al Viro2bbf8c12020-04-27 10:49:26 -0400712 struct ip_mreq_source mreqs;
713 struct sockaddr_in *psin;
714 int omode, add, err;
715
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200716 err = copy_group_source_from_user(&greqs, optval, optlen);
717 if (err)
718 return err;
719
720 if (greqs.gsr_group.ss_family != AF_INET ||
721 greqs.gsr_source.ss_family != AF_INET)
Al Viro2bbf8c12020-04-27 10:49:26 -0400722 return -EADDRNOTAVAIL;
723
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200724 psin = (struct sockaddr_in *)&greqs.gsr_group;
Al Viro2bbf8c12020-04-27 10:49:26 -0400725 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200726 psin = (struct sockaddr_in *)&greqs.gsr_source;
Al Viro2bbf8c12020-04-27 10:49:26 -0400727 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
728 mreqs.imr_interface = 0; /* use index for mc_source */
729
730 if (optname == MCAST_BLOCK_SOURCE) {
731 omode = MCAST_EXCLUDE;
732 add = 1;
733 } else if (optname == MCAST_UNBLOCK_SOURCE) {
734 omode = MCAST_EXCLUDE;
735 add = 0;
736 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
737 struct ip_mreqn mreq;
738
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200739 psin = (struct sockaddr_in *)&greqs.gsr_group;
Al Viro2bbf8c12020-04-27 10:49:26 -0400740 mreq.imr_multiaddr = psin->sin_addr;
741 mreq.imr_address.s_addr = 0;
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200742 mreq.imr_ifindex = greqs.gsr_interface;
Al Viro2bbf8c12020-04-27 10:49:26 -0400743 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
744 if (err && err != -EADDRINUSE)
745 return err;
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200746 greqs.gsr_interface = mreq.imr_ifindex;
Al Viro2bbf8c12020-04-27 10:49:26 -0400747 omode = MCAST_INCLUDE;
748 add = 1;
749 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
750 omode = MCAST_INCLUDE;
751 add = 0;
752 }
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200753 return ip_mc_source(add, omode, sk, &mreqs, greqs.gsr_interface);
Al Viro2bbf8c12020-04-27 10:49:26 -0400754}
755
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200756static int ip_set_mcast_msfilter(struct sock *sk, void __user *optval,
757 int optlen)
758{
759 struct group_filter *gsf = NULL;
760 int err;
761
762 if (optlen < GROUP_FILTER_SIZE(0))
763 return -EINVAL;
764 if (optlen > sysctl_optmem_max)
765 return -ENOBUFS;
766
767 gsf = memdup_user(optval, optlen);
768 if (IS_ERR(gsf))
769 return PTR_ERR(gsf);
770
771 /* numsrc >= (4G-140)/128 overflow in 32 bits */
772 err = -ENOBUFS;
773 if (gsf->gf_numsrc >= 0x1ffffff ||
774 gsf->gf_numsrc > sock_net(sk)->ipv4.sysctl_igmp_max_msf)
775 goto out_free_gsf;
776
777 err = -EINVAL;
778 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen)
779 goto out_free_gsf;
780
781 err = set_mcast_msfilter(sk, gsf->gf_interface, gsf->gf_numsrc,
782 gsf->gf_fmode, &gsf->gf_group, gsf->gf_slist);
783out_free_gsf:
784 kfree(gsf);
785 return err;
786}
787
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200788static int compat_ip_set_mcast_msfilter(struct sock *sk, void __user *optval,
789 int optlen)
790{
791 const int size0 = offsetof(struct compat_group_filter, gf_slist);
792 struct compat_group_filter *gf32;
793 unsigned int n;
794 void *p;
795 int err;
796
797 if (optlen < size0)
798 return -EINVAL;
799 if (optlen > sysctl_optmem_max - 4)
800 return -ENOBUFS;
801
802 p = kmalloc(optlen + 4, GFP_KERNEL);
803 if (!p)
804 return -ENOMEM;
805 gf32 = p + 4; /* we want ->gf_group and ->gf_slist aligned */
806
807 err = -EFAULT;
808 if (copy_from_user(gf32, optval, optlen))
809 goto out_free_gsf;
810
811 /* numsrc >= (4G-140)/128 overflow in 32 bits */
812 n = gf32->gf_numsrc;
813 err = -ENOBUFS;
814 if (n >= 0x1ffffff)
815 goto out_free_gsf;
816
817 err = -EINVAL;
818 if (offsetof(struct compat_group_filter, gf_slist[n]) > optlen)
819 goto out_free_gsf;
820
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200821 /* numsrc >= (4G-140)/128 overflow in 32 bits */
822 err = -ENOBUFS;
823 if (n > sock_net(sk)->ipv4.sysctl_igmp_max_msf)
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200824 goto out_free_gsf;
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200825 err = set_mcast_msfilter(sk, gf32->gf_interface, n, gf32->gf_fmode,
826 &gf32->gf_group, gf32->gf_slist);
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200827out_free_gsf:
828 kfree(p);
829 return err;
830}
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200831
Christoph Hellwig02caad72020-07-17 08:23:25 +0200832static int ip_mcast_join_leave(struct sock *sk, int optname,
833 void __user *optval, int optlen)
834{
835 struct ip_mreqn mreq = { };
836 struct sockaddr_in *psin;
837 struct group_req greq;
838
839 if (optlen < sizeof(struct group_req))
840 return -EINVAL;
841 if (copy_from_user(&greq, optval, sizeof(greq)))
842 return -EFAULT;
843
844 psin = (struct sockaddr_in *)&greq.gr_group;
845 if (psin->sin_family != AF_INET)
846 return -EINVAL;
847 mreq.imr_multiaddr = psin->sin_addr;
848 mreq.imr_ifindex = greq.gr_interface;
849 if (optname == MCAST_JOIN_GROUP)
850 return ip_mc_join_group(sk, &mreq);
851 return ip_mc_leave_group(sk, &mreq);
852}
853
Christoph Hellwig02caad72020-07-17 08:23:25 +0200854static int compat_ip_mcast_join_leave(struct sock *sk, int optname,
855 void __user *optval, int optlen)
856{
857 struct compat_group_req greq;
858 struct ip_mreqn mreq = { };
859 struct sockaddr_in *psin;
Christoph Hellwig02caad72020-07-17 08:23:25 +0200860
861 if (optlen < sizeof(struct compat_group_req))
862 return -EINVAL;
863 if (copy_from_user(&greq, optval, sizeof(greq)))
864 return -EFAULT;
865
866 psin = (struct sockaddr_in *)&greq.gr_group;
867 if (psin->sin_family != AF_INET)
868 return -EINVAL;
869 mreq.imr_multiaddr = psin->sin_addr;
870 mreq.imr_ifindex = greq.gr_interface;
871
Christoph Hellwig02caad72020-07-17 08:23:25 +0200872 if (optname == MCAST_JOIN_GROUP)
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200873 return ip_mc_join_group(sk, &mreq);
874 return ip_mc_leave_group(sk, &mreq);
Christoph Hellwig02caad72020-07-17 08:23:25 +0200875}
Christoph Hellwig02caad72020-07-17 08:23:25 +0200876
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800877static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700878 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 struct inet_sock *inet = inet_sk(sk);
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200881 struct net *net = sock_net(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800882 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300883 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Xi Wang0c9f79b2012-11-11 11:20:01 +0000885 switch (optname) {
886 case IP_PKTINFO:
887 case IP_RECVTTL:
888 case IP_RECVOPTS:
889 case IP_RECVTOS:
890 case IP_RETOPTS:
891 case IP_TOS:
892 case IP_TTL:
893 case IP_HDRINCL:
894 case IP_MTU_DISCOVER:
895 case IP_RECVERR:
896 case IP_ROUTER_ALERT:
897 case IP_FREEBIND:
898 case IP_PASSSEC:
899 case IP_TRANSPARENT:
900 case IP_MINTTL:
901 case IP_NODEFRAG:
Eric Dumazet90c337d2015-06-06 21:17:57 -0700902 case IP_BIND_ADDRESS_NO_PORT:
Xi Wang0c9f79b2012-11-11 11:20:01 +0000903 case IP_UNICAST_IF:
904 case IP_MULTICAST_TTL:
905 case IP_MULTICAST_ALL:
906 case IP_MULTICAST_LOOP:
907 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800908 case IP_CHECKSUM:
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400909 case IP_RECVFRAGSIZE:
Willem de Bruijneba75c52020-07-10 09:29:02 -0400910 case IP_RECVERR_RFC4884:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (optlen >= sizeof(int)) {
912 if (get_user(val, (int __user *) optval))
913 return -EFAULT;
914 } else if (optlen >= sizeof(char)) {
915 unsigned char ucval;
916
917 if (get_user(ucval, (unsigned char __user *) optval))
918 return -EFAULT;
919 val = (int) ucval;
920 }
921 }
922
923 /* If optlen==0, it is equivalent to val == 0 */
924
Kirill Tkhai05269472018-03-22 12:45:12 +0300925 if (optname == IP_ROUTER_ALERT)
926 return ip_ra_control(sk, val ? 1 : 0, NULL);
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800927 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800928 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300931 if (needs_rtnl)
932 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 lock_sock(sk);
934
935 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800936 case IP_OPTIONS:
937 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000938 struct ip_options_rcu *old, *opt = NULL;
939
roel kluin65a1c4f2009-10-23 05:59:21 +0000940 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800941 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900942 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700943 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800944 if (err)
945 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000946 old = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200947 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800948 if (inet->is_icsk) {
949 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000950#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800951 if (sk->sk_family == PF_INET ||
952 (!((1 << sk->sk_state) &
953 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000954 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000956 if (old)
957 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800958 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000959 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800960 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000961#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000965 rcu_assign_pointer(inet->inet_opt, opt);
966 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800967 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800968 break;
969 }
970 case IP_PKTINFO:
971 if (val)
972 inet->cmsg_flags |= IP_CMSG_PKTINFO;
973 else
974 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
975 break;
976 case IP_RECVTTL:
977 if (val)
978 inet->cmsg_flags |= IP_CMSG_TTL;
979 else
980 inet->cmsg_flags &= ~IP_CMSG_TTL;
981 break;
982 case IP_RECVTOS:
983 if (val)
984 inet->cmsg_flags |= IP_CMSG_TOS;
985 else
986 inet->cmsg_flags &= ~IP_CMSG_TOS;
987 break;
988 case IP_RECVOPTS:
989 if (val)
990 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
991 else
992 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
993 break;
994 case IP_RETOPTS:
995 if (val)
996 inet->cmsg_flags |= IP_CMSG_RETOPTS;
997 else
998 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
999 break;
1000 case IP_PASSSEC:
1001 if (val)
1002 inet->cmsg_flags |= IP_CMSG_PASSSEC;
1003 else
1004 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
1005 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001006 case IP_RECVORIGDSTADDR:
1007 if (val)
1008 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
1009 else
1010 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
1011 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001012 case IP_CHECKSUM:
1013 if (val) {
1014 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
1015 inet_inc_convert_csum(sk);
1016 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
1017 }
1018 } else {
1019 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
1020 inet_dec_convert_csum(sk);
1021 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
1022 }
1023 }
1024 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -04001025 case IP_RECVFRAGSIZE:
1026 if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM)
1027 goto e_inval;
1028 if (val)
1029 inet->cmsg_flags |= IP_CMSG_RECVFRAGSIZE;
1030 else
1031 inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE;
1032 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001033 case IP_TOS: /* This sets both TOS and Precedence */
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +02001034 __ip_sock_set_tos(sk, val);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001035 break;
1036 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001037 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001038 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +00001039 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001040 goto e_inval;
1041 inet->uc_ttl = val;
1042 break;
1043 case IP_HDRINCL:
1044 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001047 }
1048 inet->hdrincl = val ? 1 : 0;
1049 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +00001050 case IP_NODEFRAG:
1051 if (sk->sk_type != SOCK_RAW) {
1052 err = -ENOPROTOOPT;
1053 break;
1054 }
1055 inet->nodefrag = val ? 1 : 0;
1056 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001057 case IP_BIND_ADDRESS_NO_PORT:
1058 inet->bind_address_no_port = val ? 1 : 0;
1059 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001060 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +01001061 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001062 goto e_inval;
1063 inet->pmtudisc = val;
1064 break;
1065 case IP_RECVERR:
1066 inet->recverr = !!val;
1067 if (!val)
1068 skb_queue_purge(&sk->sk_error_queue);
1069 break;
Willem de Bruijneba75c52020-07-10 09:29:02 -04001070 case IP_RECVERR_RFC4884:
1071 if (val < 0 || val > 1)
1072 goto e_inval;
1073 inet->recverr_rfc4884 = !!val;
1074 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001075 case IP_MULTICAST_TTL:
1076 if (sk->sk_type == SOCK_STREAM)
1077 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001078 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001079 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001080 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001081 val = 1;
1082 if (val < 0 || val > 255)
1083 goto e_inval;
1084 inet->mc_ttl = val;
1085 break;
1086 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001087 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001088 goto e_inval;
1089 inet->mc_loop = !!val;
1090 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001091 case IP_UNICAST_IF:
1092 {
1093 struct net_device *dev = NULL;
1094 int ifindex;
David Ahern9515a2e2018-01-24 19:37:38 -08001095 int midx;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001096
1097 if (optlen != sizeof(int))
1098 goto e_inval;
1099
1100 ifindex = (__force int)ntohl((__force __be32)val);
1101 if (ifindex == 0) {
1102 inet->uc_index = 0;
1103 err = 0;
1104 break;
1105 }
1106
1107 dev = dev_get_by_index(sock_net(sk), ifindex);
1108 err = -EADDRNOTAVAIL;
1109 if (!dev)
1110 break;
David Ahern9515a2e2018-01-24 19:37:38 -08001111
1112 midx = l3mdev_master_ifindex(dev);
Erich E. Hoover76e21052012-02-08 09:11:07 +00001113 dev_put(dev);
1114
1115 err = -EINVAL;
David Ahern9515a2e2018-01-24 19:37:38 -08001116 if (sk->sk_bound_dev_if &&
1117 (!midx || midx != sk->sk_bound_dev_if))
Erich E. Hoover76e21052012-02-08 09:11:07 +00001118 break;
1119
1120 inet->uc_index = ifindex;
1121 err = 0;
1122 break;
1123 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001124 case IP_MULTICAST_IF:
1125 {
1126 struct ip_mreqn mreq;
1127 struct net_device *dev = NULL;
David Ahern7bb387c2016-12-29 15:39:37 -08001128 int midx;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001129
1130 if (sk->sk_type == SOCK_STREAM)
1131 goto e_inval;
1132 /*
1133 * Check the arguments are allowable
1134 */
1135
Shan Wei09159212009-09-22 15:41:10 +00001136 if (optlen < sizeof(struct in_addr))
1137 goto e_inval;
1138
Stephen Hemminger132adf52007-03-08 20:44:43 -08001139 err = -EFAULT;
1140 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -08001141 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001142 break;
1143 } else {
1144 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +00001145 if (optlen >= sizeof(struct ip_mreq)) {
1146 if (copy_from_user(&mreq, optval,
1147 sizeof(struct ip_mreq)))
1148 break;
1149 } else if (optlen >= sizeof(struct in_addr)) {
1150 if (copy_from_user(&mreq.imr_address, optval,
1151 sizeof(struct in_addr)))
1152 break;
1153 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001154 }
1155
1156 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -07001157 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001158 inet->mc_index = 0;
1159 inet->mc_addr = 0;
1160 err = 0;
1161 break;
1162 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001163 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +00001164 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001165 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001166 } else
Eric Dumazet55b80502009-10-19 06:41:58 +00001167 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001168
1169
1170 err = -EADDRNOTAVAIL;
1171 if (!dev)
1172 break;
David Ahern7bb387c2016-12-29 15:39:37 -08001173
1174 midx = l3mdev_master_ifindex(dev);
1175
Eric Dumazet55b80502009-10-19 06:41:58 +00001176 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001177
1178 err = -EINVAL;
1179 if (sk->sk_bound_dev_if &&
David Ahern7bb387c2016-12-29 15:39:37 -08001180 mreq.imr_ifindex != sk->sk_bound_dev_if &&
1181 (!midx || midx != sk->sk_bound_dev_if))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001182 break;
1183
1184 inet->mc_index = mreq.imr_ifindex;
1185 inet->mc_addr = mreq.imr_address.s_addr;
1186 err = 0;
1187 break;
1188 }
1189
1190 case IP_ADD_MEMBERSHIP:
1191 case IP_DROP_MEMBERSHIP:
1192 {
1193 struct ip_mreqn mreq;
1194
Flavio Leitnera96fb492007-08-24 22:16:39 -07001195 err = -EPROTO;
1196 if (inet_sk(sk)->is_icsk)
1197 break;
1198
Stephen Hemminger132adf52007-03-08 20:44:43 -08001199 if (optlen < sizeof(struct ip_mreq))
1200 goto e_inval;
1201 err = -EFAULT;
1202 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -08001203 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001204 break;
1205 } else {
1206 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -08001207 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001208 break;
1209 }
1210
1211 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001212 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001213 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001214 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001215 break;
1216 }
1217 case IP_MSFILTER:
1218 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001219 struct ip_msfilter *msf;
1220
1221 if (optlen < IP_MSFILTER_SIZE(0))
1222 goto e_inval;
1223 if (optlen > sysctl_optmem_max) {
1224 err = -ENOBUFS;
1225 break;
1226 }
Al Viroa2c841d2017-05-13 18:26:06 -04001227 msf = memdup_user(optval, optlen);
1228 if (IS_ERR(msf)) {
1229 err = PTR_ERR(msf);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001230 break;
1231 }
1232 /* numsrc >= (1G-4) overflow in 32 bits */
1233 if (msf->imsf_numsrc >= 0x3ffffffcU ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +02001234 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001235 kfree(msf);
1236 err = -ENOBUFS;
1237 break;
1238 }
1239 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
1240 kfree(msf);
1241 err = -EINVAL;
1242 break;
1243 }
1244 err = ip_mc_msfilter(sk, msf, 0);
1245 kfree(msf);
1246 break;
1247 }
1248 case IP_BLOCK_SOURCE:
1249 case IP_UNBLOCK_SOURCE:
1250 case IP_ADD_SOURCE_MEMBERSHIP:
1251 case IP_DROP_SOURCE_MEMBERSHIP:
1252 {
1253 struct ip_mreq_source mreqs;
1254 int omode, add;
1255
1256 if (optlen != sizeof(struct ip_mreq_source))
1257 goto e_inval;
1258 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
1259 err = -EFAULT;
1260 break;
1261 }
1262 if (optname == IP_BLOCK_SOURCE) {
1263 omode = MCAST_EXCLUDE;
1264 add = 1;
1265 } else if (optname == IP_UNBLOCK_SOURCE) {
1266 omode = MCAST_EXCLUDE;
1267 add = 0;
1268 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
1269 struct ip_mreqn mreq;
1270
1271 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
1272 mreq.imr_address.s_addr = mreqs.imr_interface;
1273 mreq.imr_ifindex = 0;
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001274 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001275 if (err && err != -EADDRINUSE)
1276 break;
1277 omode = MCAST_INCLUDE;
1278 add = 1;
1279 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
1280 omode = MCAST_INCLUDE;
1281 add = 0;
1282 }
1283 err = ip_mc_source(add, omode, sk, &mreqs, 0);
1284 break;
1285 }
1286 case MCAST_JOIN_GROUP:
1287 case MCAST_LEAVE_GROUP:
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001288 if (in_compat_syscall())
1289 err = compat_ip_mcast_join_leave(sk, optname, optval,
1290 optlen);
1291 else
1292 err = ip_mcast_join_leave(sk, optname, optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001293 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001294 case MCAST_JOIN_SOURCE_GROUP:
1295 case MCAST_LEAVE_SOURCE_GROUP:
1296 case MCAST_BLOCK_SOURCE:
1297 case MCAST_UNBLOCK_SOURCE:
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001298 err = do_mcast_group_source(sk, optname, optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001299 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001300 case MCAST_MSFILTER:
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001301 if (in_compat_syscall())
1302 err = compat_ip_set_mcast_msfilter(sk, optval, optlen);
1303 else
1304 err = ip_set_mcast_msfilter(sk, optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001305 break;
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001306 case IP_MULTICAST_ALL:
1307 if (optlen < 1)
1308 goto e_inval;
1309 if (val != 0 && val != 1)
1310 goto e_inval;
1311 inet->mc_all = val;
1312 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001313
1314 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001315 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001316 goto e_inval;
1317 inet->freebind = !!val;
1318 break;
1319
1320 case IP_IPSEC_POLICY:
1321 case IP_XFRM_POLICY:
1322 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001323 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001324 break;
1325 err = xfrm_user_policy(sk, optname, optval, optlen);
1326 break;
1327
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001328 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001329 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1330 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001331 err = -EPERM;
1332 break;
1333 }
1334 if (optlen < 1)
1335 goto e_inval;
1336 inet->transparent = !!val;
1337 break;
1338
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001339 case IP_MINTTL:
1340 if (optlen < 1)
1341 goto e_inval;
1342 if (val < 0 || val > 255)
1343 goto e_inval;
1344 inet->min_ttl = val;
1345 break;
1346
Stephen Hemminger132adf52007-03-08 20:44:43 -08001347 default:
1348 err = -ENOPROTOOPT;
1349 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001352 if (needs_rtnl)
1353 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return err;
1355
1356e_inval:
1357 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001358 if (needs_rtnl)
1359 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return -EINVAL;
1361}
1362
Eric Dumazetf84af322010-04-28 15:31:51 -07001363/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001364 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001365 * @sk: socket
1366 * @skb: buffer
1367 *
David S. Miller35ebf652012-06-28 03:59:11 -07001368 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1369 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001370 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001371 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001372void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001373{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001374 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001375 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1376 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001377
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001378 if (prepare && skb_rtable(skb)) {
David Ahern0b922b72016-05-10 11:19:51 -07001379 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1380 * which has interface index (iif) as the first member of the
1381 * underlying inet{6}_skb_parm struct. This code then overlays
1382 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001383 * element so the iif is picked up from the prior IPCB. If iif
1384 * is the loopback interface, then return the sending interface
1385 * (e.g., process binds socket to eth0 for Tx which is
1386 * redirected to loopback in the rtable/dst).
David Ahern0b922b72016-05-10 11:19:51 -07001387 */
David Aherncbea8f02017-09-13 17:11:37 -07001388 struct rtable *rt = skb_rtable(skb);
1389 bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags);
1390
1391 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001392 pktinfo->ipi_ifindex = inet_iif(skb);
David Aherncbea8f02017-09-13 17:11:37 -07001393 else if (l3slave && rt && rt->rt_iif)
1394 pktinfo->ipi_ifindex = rt->rt_iif;
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001395
David S. Miller35ebf652012-06-28 03:59:11 -07001396 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001397 } else {
1398 pktinfo->ipi_ifindex = 0;
1399 pktinfo->ipi_spec_dst.s_addr = 0;
1400 }
Paolo Abeni61a10302017-08-03 18:07:07 +02001401 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001402}
Eric Dumazetf84af322010-04-28 15:31:51 -07001403
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001404int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001405 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001406{
1407 int err;
1408
1409 if (level != SOL_IP)
1410 return -ENOPROTOOPT;
1411
1412 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
Taehee Yoo97adadd2018-11-05 22:31:41 +09001413#if IS_ENABLED(CONFIG_BPFILTER_UMH)
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001414 if (optname >= BPFILTER_IPT_SO_SET_REPLACE &&
1415 optname < BPFILTER_IPT_SET_MAX)
1416 err = bpfilter_ip_set_sockopt(sk, optname, optval, optlen);
1417#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001418#ifdef CONFIG_NETFILTER
1419 /* we need to exclude all possible ENOPROTOOPTs except default case */
1420 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001421 optname != IP_IPSEC_POLICY &&
1422 optname != IP_XFRM_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001423 !ip_mroute_opt(optname))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001424 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001425#endif
1426 return err;
1427}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001428EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001431 * Get the options. Note for future reference. The GET of IP options gets
1432 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 */
1434
WANG Cong87e9f032015-11-03 15:41:16 -08001435static bool getsockopt_needs_rtnl(int optname)
1436{
1437 switch (optname) {
1438 case IP_MSFILTER:
1439 case MCAST_MSFILTER:
1440 return true;
1441 }
1442 return false;
1443}
1444
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001445static int ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
1446 int __user *optlen, int len)
1447{
1448 const int size0 = offsetof(struct group_filter, gf_slist);
1449 struct group_filter __user *p = optval;
1450 struct group_filter gsf;
1451 int num;
1452 int err;
1453
1454 if (len < size0)
1455 return -EINVAL;
1456 if (copy_from_user(&gsf, p, size0))
1457 return -EFAULT;
1458
1459 num = gsf.gf_numsrc;
1460 err = ip_mc_gsfget(sk, &gsf, p->gf_slist);
1461 if (err)
1462 return err;
1463 if (gsf.gf_numsrc < num)
1464 num = gsf.gf_numsrc;
1465 if (put_user(GROUP_FILTER_SIZE(num), optlen) ||
1466 copy_to_user(p, &gsf, size0))
1467 return -EFAULT;
1468 return 0;
1469}
1470
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001471static int compat_ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001472 int __user *optlen, int len)
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001473{
1474 const int size0 = offsetof(struct compat_group_filter, gf_slist);
1475 struct compat_group_filter __user *p = optval;
1476 struct compat_group_filter gf32;
1477 struct group_filter gf;
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001478 int num;
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001479 int err;
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001480
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001481 if (len < size0)
1482 return -EINVAL;
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001483 if (copy_from_user(&gf32, p, size0))
1484 return -EFAULT;
1485
1486 gf.gf_interface = gf32.gf_interface;
1487 gf.gf_fmode = gf32.gf_fmode;
1488 num = gf.gf_numsrc = gf32.gf_numsrc;
1489 gf.gf_group = gf32.gf_group;
1490
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001491 err = ip_mc_gsfget(sk, &gf, p->gf_slist);
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001492 if (err)
1493 return err;
1494 if (gf.gf_numsrc < num)
1495 num = gf.gf_numsrc;
1496 len = GROUP_FILTER_SIZE(num) - (sizeof(gf) - sizeof(gf32));
1497 if (put_user(len, optlen) ||
1498 put_user(gf.gf_fmode, &p->gf_fmode) ||
1499 put_user(gf.gf_numsrc, &p->gf_numsrc))
1500 return -EFAULT;
1501 return 0;
1502}
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001503
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001504static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001505 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 struct inet_sock *inet = inet_sk(sk);
WANG Cong87e9f032015-11-03 15:41:16 -08001508 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1509 int val, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001511
Stephen Hemminger132adf52007-03-08 20:44:43 -08001512 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return -EOPNOTSUPP;
1514
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001515 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001516 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Jianjun Kong09cb1052008-11-03 00:27:11 -08001518 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001520 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001522
WANG Cong87e9f032015-11-03 15:41:16 -08001523 if (needs_rtnl)
1524 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 lock_sock(sk);
1526
Stephen Hemminger132adf52007-03-08 20:44:43 -08001527 switch (optname) {
1528 case IP_OPTIONS:
1529 {
1530 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001531 struct ip_options *opt = (struct ip_options *)optbuf;
1532 struct ip_options_rcu *inet_opt;
1533
1534 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001535 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001536 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001537 if (inet_opt)
1538 memcpy(optbuf, &inet_opt->opt,
1539 sizeof(struct ip_options) +
1540 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001541 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Stephen Hemminger132adf52007-03-08 20:44:43 -08001543 if (opt->optlen == 0)
1544 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Stephen Hemminger132adf52007-03-08 20:44:43 -08001546 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Stephen Hemminger132adf52007-03-08 20:44:43 -08001548 len = min_t(unsigned int, len, opt->optlen);
1549 if (put_user(len, optlen))
1550 return -EFAULT;
1551 if (copy_to_user(optval, opt->__data, len))
1552 return -EFAULT;
1553 return 0;
1554 }
1555 case IP_PKTINFO:
1556 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1557 break;
1558 case IP_RECVTTL:
1559 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1560 break;
1561 case IP_RECVTOS:
1562 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1563 break;
1564 case IP_RECVOPTS:
1565 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1566 break;
1567 case IP_RETOPTS:
1568 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1569 break;
1570 case IP_PASSSEC:
1571 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1572 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001573 case IP_RECVORIGDSTADDR:
1574 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1575 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001576 case IP_CHECKSUM:
1577 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1578 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -04001579 case IP_RECVFRAGSIZE:
1580 val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0;
1581 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001582 case IP_TOS:
1583 val = inet->tos;
1584 break;
1585 case IP_TTL:
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001586 {
1587 struct net *net = sock_net(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001588 val = (inet->uc_ttl == -1 ?
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001589 net->ipv4.sysctl_ip_default_ttl :
Stephen Hemminger132adf52007-03-08 20:44:43 -08001590 inet->uc_ttl);
1591 break;
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001592 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001593 case IP_HDRINCL:
1594 val = inet->hdrincl;
1595 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001596 case IP_NODEFRAG:
1597 val = inet->nodefrag;
1598 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001599 case IP_BIND_ADDRESS_NO_PORT:
1600 val = inet->bind_address_no_port;
1601 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001602 case IP_MTU_DISCOVER:
1603 val = inet->pmtudisc;
1604 break;
1605 case IP_MTU:
1606 {
1607 struct dst_entry *dst;
1608 val = 0;
1609 dst = sk_dst_get(sk);
1610 if (dst) {
1611 val = dst_mtu(dst);
1612 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001614 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001616 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001618 break;
1619 }
1620 case IP_RECVERR:
1621 val = inet->recverr;
1622 break;
Willem de Bruijneba75c52020-07-10 09:29:02 -04001623 case IP_RECVERR_RFC4884:
1624 val = inet->recverr_rfc4884;
1625 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001626 case IP_MULTICAST_TTL:
1627 val = inet->mc_ttl;
1628 break;
1629 case IP_MULTICAST_LOOP:
1630 val = inet->mc_loop;
1631 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001632 case IP_UNICAST_IF:
1633 val = (__force int)htonl((__u32) inet->uc_index);
1634 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001635 case IP_MULTICAST_IF:
1636 {
1637 struct in_addr addr;
1638 len = min_t(unsigned int, len, sizeof(struct in_addr));
1639 addr.s_addr = inet->mc_addr;
1640 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Stephen Hemminger132adf52007-03-08 20:44:43 -08001642 if (put_user(len, optlen))
1643 return -EFAULT;
1644 if (copy_to_user(optval, &addr, len))
1645 return -EFAULT;
1646 return 0;
1647 }
1648 case IP_MSFILTER:
1649 {
1650 struct ip_msfilter msf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001651
1652 if (len < IP_MSFILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001653 err = -EINVAL;
1654 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001656 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001657 err = -EFAULT;
1658 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001660 err = ip_mc_msfget(sk, &msf,
1661 (struct ip_msfilter __user *)optval, optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001662 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001663 }
1664 case MCAST_MSFILTER:
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001665 if (in_compat_syscall())
1666 err = compat_ip_get_mcast_msfilter(sk, optval, optlen,
1667 len);
1668 else
1669 err = ip_get_mcast_msfilter(sk, optval, optlen, len);
WANG Cong87e9f032015-11-03 15:41:16 -08001670 goto out;
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001671 case IP_MULTICAST_ALL:
1672 val = inet->mc_all;
1673 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001674 case IP_PKTOPTIONS:
1675 {
1676 struct msghdr msg;
1677
1678 release_sock(sk);
1679
1680 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001682
Christoph Hellwig1f466e12020-05-11 13:59:13 +02001683 msg.msg_control_is_user = true;
1684 msg.msg_control_user = optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001685 msg.msg_controllen = len;
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001686 msg.msg_flags = in_compat_syscall() ? MSG_CMSG_COMPAT : 0;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001687
1688 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1689 struct in_pktinfo info;
1690
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001691 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1692 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001693 info.ipi_ifindex = inet->mc_index;
1694 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1695 }
1696 if (inet->cmsg_flags & IP_CMSG_TTL) {
1697 int hlim = inet->mc_ttl;
1698 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1699 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001700 if (inet->cmsg_flags & IP_CMSG_TOS) {
1701 int tos = inet->rcv_tos;
1702 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1703 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001704 len -= msg.msg_controllen;
1705 return put_user(len, optlen);
1706 }
1707 case IP_FREEBIND:
1708 val = inet->freebind;
1709 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001710 case IP_TRANSPARENT:
1711 val = inet->transparent;
1712 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001713 case IP_MINTTL:
1714 val = inet->min_ttl;
1715 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001716 default:
1717 release_sock(sk);
1718 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
1720 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001721
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001722 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 unsigned char ucval = (unsigned char)val;
1724 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001725 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001727 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return -EFAULT;
1729 } else {
1730 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001731 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001733 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return -EFAULT;
1735 }
1736 return 0;
WANG Cong87e9f032015-11-03 15:41:16 -08001737
1738out:
1739 release_sock(sk);
1740 if (needs_rtnl)
1741 rtnl_unlock();
1742 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001745int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001746 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001747{
1748 int err;
1749
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001750 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1751
Taehee Yoo97adadd2018-11-05 22:31:41 +09001752#if IS_ENABLED(CONFIG_BPFILTER_UMH)
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001753 if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1754 optname < BPFILTER_IPT_GET_MAX)
1755 err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1756#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001757#ifdef CONFIG_NETFILTER
1758 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001759 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1760 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001761 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001762
Jianjun Kong09cb1052008-11-03 00:27:11 -08001763 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001764 return -EFAULT;
1765
Paolo Abeni01ea3062018-02-08 12:19:00 +01001766 err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001767 if (err >= 0)
1768 err = put_user(len, optlen);
1769 return err;
1770 }
1771#endif
1772 return err;
1773}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001774EXPORT_SYMBOL(ip_getsockopt);