blob: b12f39b52008a38544770a95be8638aa5534357c [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 */
Christoph Hellwigde40a3e2020-07-23 08:08:57 +0200283 err = ip_options_get(net, &ipc->opt,
284 KERNEL_SOCKPTR(CMSG_DATA(cmsg)),
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700285 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (err)
287 return err;
288 break;
289 case IP_PKTINFO:
290 {
291 struct in_pktinfo *info;
292 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
293 return -EINVAL;
294 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
David Ahern1cbec072018-02-16 11:03:03 -0800295 if (info->ipi_ifindex)
296 ipc->oif = info->ipi_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 ipc->addr = info->ipi_spec_dst.s_addr;
298 break;
299 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200300 case IP_TTL:
301 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
302 return -EINVAL;
303 val = *(int *)CMSG_DATA(cmsg);
304 if (val < 1 || val > 255)
305 return -EINVAL;
306 ipc->ttl = val;
307 break;
308 case IP_TOS:
Eric Dumazete895cdc2016-09-07 21:52:56 -0700309 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)))
310 val = *(int *)CMSG_DATA(cmsg);
311 else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8)))
312 val = *(u8 *)CMSG_DATA(cmsg);
313 else
Francesco Fuscof02db312013-09-24 15:43:08 +0200314 return -EINVAL;
Francesco Fuscof02db312013-09-24 15:43:08 +0200315 if (val < 0 || val > 255)
316 return -EINVAL;
317 ipc->tos = val;
318 ipc->priority = rt_tos2priority(ipc->tos);
319 break;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 default:
322 return -EINVAL;
323 }
324 }
325 return 0;
326}
327
Eric Dumazet592fcb92010-06-09 16:21:07 +0000328static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000329{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000330 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
331
332 sock_put(ra->saved_sk);
333 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000334}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700336int ip_ra_control(struct sock *sk, unsigned char on,
337 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000339 struct ip_ra_chain *ra, *new_ra;
340 struct ip_ra_chain __rcu **rap;
Kirill Tkhai5796ef72018-03-22 12:45:32 +0300341 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000343 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return -EINVAL;
345
346 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
Gen Zhang425aa0e2019-05-24 11:24:26 +0800347 if (on && !new_ra)
348 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300350 mutex_lock(&net->ipv4.ra_mutex);
Kirill Tkhai5796ef72018-03-22 12:45:32 +0300351 for (rap = &net->ipv4.ra_chain;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300352 (ra = rcu_dereference_protected(*rap,
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300353 lockdep_is_held(&net->ipv4.ra_mutex))) != NULL;
Eric Dumazet43a951e2010-10-25 03:32:44 +0000354 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (ra->sk == sk) {
356 if (on) {
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300357 mutex_unlock(&net->ipv4.ra_mutex);
Jesper Juhla51482b2005-11-08 09:41:34 -0800358 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return -EADDRINUSE;
360 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000361 /* dont let ip_call_ra_chain() use sk again */
362 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700363 RCU_INIT_POINTER(*rap, ra->next);
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300364 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 if (ra->destructor)
367 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000368 /*
369 * Delay sock_put(sk) and kfree(ra) after one rcu grace
370 * period. This guarantee ip_call_ra_chain() dont need
371 * to mess with socket refcounts.
372 */
373 ra->saved_sk = sk;
374 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
376 }
377 }
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300378 if (!new_ra) {
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300379 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return -ENOBUFS;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 new_ra->sk = sk;
383 new_ra->destructor = destructor;
384
Eric Dumazet8e380f02014-09-09 08:11:41 -0700385 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000386 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 sock_hold(sk);
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300388 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 return 0;
391}
392
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900393void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700394 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct sock_exterr_skb *serr;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 skb = skb_clone(skb, GFP_ATOMIC);
399 if (!skb)
400 return;
401
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900402 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 serr->ee.ee_errno = err;
404 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300405 serr->ee.ee_type = icmp_hdr(skb)->type;
406 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 serr->ee.ee_pad = 0;
408 serr->ee.ee_info = info;
409 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300410 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700411 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 serr->port = port;
413
Ian Morris00db4122015-04-03 09:17:27 +0100414 if (skb_pull(skb, payload - skb->data)) {
Willem de Bruijneba75c52020-07-10 09:29:02 -0400415 if (inet_sk(sk)->recverr_rfc4884)
416 ip_icmp_error_rfc4884(skb, &serr->ee.ee_rfc4884);
417
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300418 skb_reset_transport_header(skb);
419 if (sock_queue_err_skb(sk, skb) == 0)
420 return;
421 }
422 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Al Viro05790162006-09-27 18:33:40 -0700425void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct inet_sock *inet = inet_sk(sk);
428 struct sock_exterr_skb *serr;
429 struct iphdr *iph;
430 struct sk_buff *skb;
431
432 if (!inet->recverr)
433 return;
434
435 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
436 if (!skb)
437 return;
438
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300439 skb_put(skb, sizeof(struct iphdr));
440 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700441 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 iph->daddr = daddr;
443
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900444 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 serr->ee.ee_errno = err;
446 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900447 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 serr->ee.ee_code = 0;
449 serr->ee.ee_pad = 0;
450 serr->ee.ee_info = info;
451 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700452 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 serr->port = port;
454
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700455 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300456 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 if (sock_queue_err_skb(sk, skb))
459 kfree_skb(skb);
460}
461
Julian Anastasov34b99df2015-06-23 08:34:39 +0300462/* For some errors we have valid addr_offset even with zero payload and
463 * zero port. Also, addr_offset should be supported if port is set.
464 */
465static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
466{
467 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
468 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
469}
470
Willem de Bruijnc247f052015-03-07 20:33:22 -0500471/* IPv4 supports cmsg on all imcp errors and some timestamps
472 *
473 * Timestamp code paths do not initialize the fields expected by cmsg:
474 * the PKTINFO fields in skb->cb[]. Fill those in here.
475 */
476static bool ipv4_datagram_support_cmsg(const struct sock *sk,
477 struct sk_buff *skb,
478 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500479{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500480 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500481
Willem de Bruijnc247f052015-03-07 20:33:22 -0500482 if (ee_origin == SO_EE_ORIGIN_ICMP)
483 return true;
484
485 if (ee_origin == SO_EE_ORIGIN_LOCAL)
486 return false;
487
488 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
Willem de Bruijn1862d622017-04-12 19:24:35 -0400489 * timestamp with egress dev. Not possible for packets without iif
Willem de Bruijnc247f052015-03-07 20:33:22 -0500490 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
491 */
Willem de Bruijn1862d622017-04-12 19:24:35 -0400492 info = PKTINFO_SKB_CB(skb);
493 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
494 !info->ipi_ifindex)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500495 return false;
496
497 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500498 return true;
499}
500
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900501/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 * Handle MSG_ERRQUEUE
503 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100504int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400507 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100508 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 struct {
510 struct sock_extended_err ee;
511 struct sockaddr_in offender;
512 } errhdr;
513 int err;
514 int copied;
515
516 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400517 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100518 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto out;
520
521 copied = skb->len;
522 if (copied > len) {
523 msg->msg_flags |= MSG_TRUNC;
524 copied = len;
525 }
David S. Miller51f3d022014-11-05 16:46:40 -0500526 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700527 if (unlikely(err)) {
528 kfree_skb(skb);
529 return err;
530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 sock_recv_timestamp(msg, sk, skb);
532
533 serr = SKB_EXT_ERR(skb);
534
Julian Anastasov34b99df2015-06-23 08:34:39 +0300535 if (sin && ipv4_datagram_support_addr(serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700537 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
538 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 sin->sin_port = serr->port;
540 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100541 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
544 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
545 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500546 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500547
Willem de Bruijnc247f052015-03-07 20:33:22 -0500548 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700550 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500551 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 ip_cmsg_recv(msg, skb);
553 }
554
555 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
556
557 /* Now we could try to dump offended packet options */
558
559 msg->msg_flags |= MSG_ERRQUEUE;
560 err = copied;
561
Eric Dumazet960a2622016-04-21 22:27:32 -0700562 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563out:
564 return err;
565}
566
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +0200567static void __ip_sock_set_tos(struct sock *sk, int val)
568{
569 if (sk->sk_type == SOCK_STREAM) {
570 val &= ~INET_ECN_MASK;
571 val |= inet_sk(sk)->tos & INET_ECN_MASK;
572 }
573 if (inet_sk(sk)->tos != val) {
574 inet_sk(sk)->tos = val;
575 sk->sk_priority = rt_tos2priority(val);
576 sk_dst_reset(sk);
577 }
578}
579
580void ip_sock_set_tos(struct sock *sk, int val)
581{
582 lock_sock(sk);
583 __ip_sock_set_tos(sk, val);
584 release_sock(sk);
585}
586EXPORT_SYMBOL(ip_sock_set_tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Christoph Hellwigc4e446b2020-05-28 07:12:27 +0200588void ip_sock_set_freebind(struct sock *sk)
589{
590 lock_sock(sk);
591 inet_sk(sk)->freebind = true;
592 release_sock(sk);
593}
594EXPORT_SYMBOL(ip_sock_set_freebind);
595
Christoph Hellwigdb45c0e2020-05-28 07:12:28 +0200596void ip_sock_set_recverr(struct sock *sk)
597{
598 lock_sock(sk);
599 inet_sk(sk)->recverr = true;
600 release_sock(sk);
601}
602EXPORT_SYMBOL(ip_sock_set_recverr);
603
Christoph Hellwig2de569b2020-05-28 07:12:29 +0200604int ip_sock_set_mtu_discover(struct sock *sk, int val)
605{
606 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
607 return -EINVAL;
608 lock_sock(sk);
609 inet_sk(sk)->pmtudisc = val;
610 release_sock(sk);
611 return 0;
612}
613EXPORT_SYMBOL(ip_sock_set_mtu_discover);
614
Christoph Hellwigc1f9ec52020-05-28 07:12:30 +0200615void ip_sock_set_pktinfo(struct sock *sk)
616{
617 lock_sock(sk);
618 inet_sk(sk)->cmsg_flags |= IP_CMSG_PKTINFO;
619 release_sock(sk);
620}
621EXPORT_SYMBOL(ip_sock_set_pktinfo);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700624 * Socket option code for IP. This is the end of the line after any
625 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300627static bool setsockopt_needs_rtnl(int optname)
628{
629 switch (optname) {
630 case IP_ADD_MEMBERSHIP:
631 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300632 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300633 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300634 case IP_DROP_SOURCE_MEMBERSHIP:
635 case IP_MSFILTER:
636 case IP_UNBLOCK_SOURCE:
637 case MCAST_BLOCK_SOURCE:
638 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300639 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300640 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300641 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300642 case MCAST_LEAVE_SOURCE_GROUP:
643 case MCAST_UNBLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300644 return true;
645 }
646 return false;
647}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Al Viroe986d4d2020-03-29 22:37:56 -0400649static int set_mcast_msfilter(struct sock *sk, int ifindex,
650 int numsrc, int fmode,
651 struct sockaddr_storage *group,
652 struct sockaddr_storage *list)
653{
654 int msize = IP_MSFILTER_SIZE(numsrc);
655 struct ip_msfilter *msf;
656 struct sockaddr_in *psin;
657 int err, i;
658
659 msf = kmalloc(msize, GFP_KERNEL);
660 if (!msf)
661 return -ENOBUFS;
662
663 psin = (struct sockaddr_in *)group;
664 if (psin->sin_family != AF_INET)
665 goto Eaddrnotavail;
666 msf->imsf_multiaddr = psin->sin_addr.s_addr;
667 msf->imsf_interface = 0;
668 msf->imsf_fmode = fmode;
669 msf->imsf_numsrc = numsrc;
670 for (i = 0; i < numsrc; ++i) {
671 psin = (struct sockaddr_in *)&list[i];
672
673 if (psin->sin_family != AF_INET)
674 goto Eaddrnotavail;
675 msf->imsf_slist[i] = psin->sin_addr.s_addr;
676 }
677 err = ip_mc_msfilter(sk, msf, ifindex);
678 kfree(msf);
679 return err;
680
681Eaddrnotavail:
682 kfree(msf);
683 return -EADDRNOTAVAIL;
684}
685
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200686static int copy_group_source_from_user(struct group_source_req *greqs,
687 void __user *optval, int optlen)
Al Viro2bbf8c12020-04-27 10:49:26 -0400688{
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200689 if (in_compat_syscall()) {
690 struct compat_group_source_req gr32;
691
692 if (optlen != sizeof(gr32))
693 return -EINVAL;
694 if (copy_from_user(&gr32, optval, sizeof(gr32)))
695 return -EFAULT;
696 greqs->gsr_interface = gr32.gsr_interface;
697 greqs->gsr_group = gr32.gsr_group;
698 greqs->gsr_source = gr32.gsr_source;
699 } else {
700 if (optlen != sizeof(*greqs))
701 return -EINVAL;
702 if (copy_from_user(greqs, optval, sizeof(*greqs)))
703 return -EFAULT;
704 }
705
706 return 0;
707}
708
709static int do_mcast_group_source(struct sock *sk, int optname,
710 void __user *optval, int optlen)
711{
712 struct group_source_req greqs;
Al Viro2bbf8c12020-04-27 10:49:26 -0400713 struct ip_mreq_source mreqs;
714 struct sockaddr_in *psin;
715 int omode, add, err;
716
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200717 err = copy_group_source_from_user(&greqs, optval, optlen);
718 if (err)
719 return err;
720
721 if (greqs.gsr_group.ss_family != AF_INET ||
722 greqs.gsr_source.ss_family != AF_INET)
Al Viro2bbf8c12020-04-27 10:49:26 -0400723 return -EADDRNOTAVAIL;
724
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200725 psin = (struct sockaddr_in *)&greqs.gsr_group;
Al Viro2bbf8c12020-04-27 10:49:26 -0400726 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200727 psin = (struct sockaddr_in *)&greqs.gsr_source;
Al Viro2bbf8c12020-04-27 10:49:26 -0400728 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
729 mreqs.imr_interface = 0; /* use index for mc_source */
730
731 if (optname == MCAST_BLOCK_SOURCE) {
732 omode = MCAST_EXCLUDE;
733 add = 1;
734 } else if (optname == MCAST_UNBLOCK_SOURCE) {
735 omode = MCAST_EXCLUDE;
736 add = 0;
737 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
738 struct ip_mreqn mreq;
739
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200740 psin = (struct sockaddr_in *)&greqs.gsr_group;
Al Viro2bbf8c12020-04-27 10:49:26 -0400741 mreq.imr_multiaddr = psin->sin_addr;
742 mreq.imr_address.s_addr = 0;
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200743 mreq.imr_ifindex = greqs.gsr_interface;
Al Viro2bbf8c12020-04-27 10:49:26 -0400744 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
745 if (err && err != -EADDRINUSE)
746 return err;
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200747 greqs.gsr_interface = mreq.imr_ifindex;
Al Viro2bbf8c12020-04-27 10:49:26 -0400748 omode = MCAST_INCLUDE;
749 add = 1;
750 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
751 omode = MCAST_INCLUDE;
752 add = 0;
753 }
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200754 return ip_mc_source(add, omode, sk, &mreqs, greqs.gsr_interface);
Al Viro2bbf8c12020-04-27 10:49:26 -0400755}
756
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200757static int ip_set_mcast_msfilter(struct sock *sk, void __user *optval,
758 int optlen)
759{
760 struct group_filter *gsf = NULL;
761 int err;
762
763 if (optlen < GROUP_FILTER_SIZE(0))
764 return -EINVAL;
765 if (optlen > sysctl_optmem_max)
766 return -ENOBUFS;
767
768 gsf = memdup_user(optval, optlen);
769 if (IS_ERR(gsf))
770 return PTR_ERR(gsf);
771
772 /* numsrc >= (4G-140)/128 overflow in 32 bits */
773 err = -ENOBUFS;
774 if (gsf->gf_numsrc >= 0x1ffffff ||
775 gsf->gf_numsrc > sock_net(sk)->ipv4.sysctl_igmp_max_msf)
776 goto out_free_gsf;
777
778 err = -EINVAL;
779 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen)
780 goto out_free_gsf;
781
782 err = set_mcast_msfilter(sk, gsf->gf_interface, gsf->gf_numsrc,
783 gsf->gf_fmode, &gsf->gf_group, gsf->gf_slist);
784out_free_gsf:
785 kfree(gsf);
786 return err;
787}
788
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200789static int compat_ip_set_mcast_msfilter(struct sock *sk, void __user *optval,
790 int optlen)
791{
792 const int size0 = offsetof(struct compat_group_filter, gf_slist);
793 struct compat_group_filter *gf32;
794 unsigned int n;
795 void *p;
796 int err;
797
798 if (optlen < size0)
799 return -EINVAL;
800 if (optlen > sysctl_optmem_max - 4)
801 return -ENOBUFS;
802
803 p = kmalloc(optlen + 4, GFP_KERNEL);
804 if (!p)
805 return -ENOMEM;
806 gf32 = p + 4; /* we want ->gf_group and ->gf_slist aligned */
807
808 err = -EFAULT;
809 if (copy_from_user(gf32, optval, optlen))
810 goto out_free_gsf;
811
812 /* numsrc >= (4G-140)/128 overflow in 32 bits */
813 n = gf32->gf_numsrc;
814 err = -ENOBUFS;
815 if (n >= 0x1ffffff)
816 goto out_free_gsf;
817
818 err = -EINVAL;
819 if (offsetof(struct compat_group_filter, gf_slist[n]) > optlen)
820 goto out_free_gsf;
821
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200822 /* numsrc >= (4G-140)/128 overflow in 32 bits */
823 err = -ENOBUFS;
824 if (n > sock_net(sk)->ipv4.sysctl_igmp_max_msf)
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200825 goto out_free_gsf;
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200826 err = set_mcast_msfilter(sk, gf32->gf_interface, n, gf32->gf_fmode,
827 &gf32->gf_group, gf32->gf_slist);
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200828out_free_gsf:
829 kfree(p);
830 return err;
831}
Christoph Hellwigd62c38f2020-07-17 08:23:24 +0200832
Christoph Hellwig02caad72020-07-17 08:23:25 +0200833static int ip_mcast_join_leave(struct sock *sk, int optname,
834 void __user *optval, int optlen)
835{
836 struct ip_mreqn mreq = { };
837 struct sockaddr_in *psin;
838 struct group_req greq;
839
840 if (optlen < sizeof(struct group_req))
841 return -EINVAL;
842 if (copy_from_user(&greq, optval, sizeof(greq)))
843 return -EFAULT;
844
845 psin = (struct sockaddr_in *)&greq.gr_group;
846 if (psin->sin_family != AF_INET)
847 return -EINVAL;
848 mreq.imr_multiaddr = psin->sin_addr;
849 mreq.imr_ifindex = greq.gr_interface;
850 if (optname == MCAST_JOIN_GROUP)
851 return ip_mc_join_group(sk, &mreq);
852 return ip_mc_leave_group(sk, &mreq);
853}
854
Christoph Hellwig02caad72020-07-17 08:23:25 +0200855static int compat_ip_mcast_join_leave(struct sock *sk, int optname,
856 void __user *optval, int optlen)
857{
858 struct compat_group_req greq;
859 struct ip_mreqn mreq = { };
860 struct sockaddr_in *psin;
Christoph Hellwig02caad72020-07-17 08:23:25 +0200861
862 if (optlen < sizeof(struct compat_group_req))
863 return -EINVAL;
864 if (copy_from_user(&greq, optval, sizeof(greq)))
865 return -EFAULT;
866
867 psin = (struct sockaddr_in *)&greq.gr_group;
868 if (psin->sin_family != AF_INET)
869 return -EINVAL;
870 mreq.imr_multiaddr = psin->sin_addr;
871 mreq.imr_ifindex = greq.gr_interface;
872
Christoph Hellwig02caad72020-07-17 08:23:25 +0200873 if (optname == MCAST_JOIN_GROUP)
Christoph Hellwigb6238c02020-07-17 08:23:26 +0200874 return ip_mc_join_group(sk, &mreq);
875 return ip_mc_leave_group(sk, &mreq);
Christoph Hellwig02caad72020-07-17 08:23:25 +0200876}
Christoph Hellwig02caad72020-07-17 08:23:25 +0200877
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800878static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700879 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 struct inet_sock *inet = inet_sk(sk);
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200882 struct net *net = sock_net(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800883 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300884 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Xi Wang0c9f79b2012-11-11 11:20:01 +0000886 switch (optname) {
887 case IP_PKTINFO:
888 case IP_RECVTTL:
889 case IP_RECVOPTS:
890 case IP_RECVTOS:
891 case IP_RETOPTS:
892 case IP_TOS:
893 case IP_TTL:
894 case IP_HDRINCL:
895 case IP_MTU_DISCOVER:
896 case IP_RECVERR:
897 case IP_ROUTER_ALERT:
898 case IP_FREEBIND:
899 case IP_PASSSEC:
900 case IP_TRANSPARENT:
901 case IP_MINTTL:
902 case IP_NODEFRAG:
Eric Dumazet90c337d2015-06-06 21:17:57 -0700903 case IP_BIND_ADDRESS_NO_PORT:
Xi Wang0c9f79b2012-11-11 11:20:01 +0000904 case IP_UNICAST_IF:
905 case IP_MULTICAST_TTL:
906 case IP_MULTICAST_ALL:
907 case IP_MULTICAST_LOOP:
908 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800909 case IP_CHECKSUM:
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400910 case IP_RECVFRAGSIZE:
Willem de Bruijneba75c52020-07-10 09:29:02 -0400911 case IP_RECVERR_RFC4884:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (optlen >= sizeof(int)) {
913 if (get_user(val, (int __user *) optval))
914 return -EFAULT;
915 } else if (optlen >= sizeof(char)) {
916 unsigned char ucval;
917
918 if (get_user(ucval, (unsigned char __user *) optval))
919 return -EFAULT;
920 val = (int) ucval;
921 }
922 }
923
924 /* If optlen==0, it is equivalent to val == 0 */
925
Kirill Tkhai05269472018-03-22 12:45:12 +0300926 if (optname == IP_ROUTER_ALERT)
927 return ip_ra_control(sk, val ? 1 : 0, NULL);
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800928 if (ip_mroute_opt(optname))
Christoph Hellwig01ccb5b2020-07-23 08:08:56 +0200929 return ip_mroute_setsockopt(sk, optname, USER_SOCKPTR(optval),
930 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300933 if (needs_rtnl)
934 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 lock_sock(sk);
936
937 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800938 case IP_OPTIONS:
939 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000940 struct ip_options_rcu *old, *opt = NULL;
941
roel kluin65a1c4f2009-10-23 05:59:21 +0000942 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800943 goto e_inval;
Christoph Hellwigde40a3e2020-07-23 08:08:57 +0200944 err = ip_options_get(sock_net(sk), &opt, USER_SOCKPTR(optval),
945 optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800946 if (err)
947 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000948 old = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200949 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800950 if (inet->is_icsk) {
951 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000952#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800953 if (sk->sk_family == PF_INET ||
954 (!((1 << sk->sk_state) &
955 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000956 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000958 if (old)
959 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800960 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000961 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800962 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000963#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000967 rcu_assign_pointer(inet->inet_opt, opt);
968 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800969 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800970 break;
971 }
972 case IP_PKTINFO:
973 if (val)
974 inet->cmsg_flags |= IP_CMSG_PKTINFO;
975 else
976 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
977 break;
978 case IP_RECVTTL:
979 if (val)
980 inet->cmsg_flags |= IP_CMSG_TTL;
981 else
982 inet->cmsg_flags &= ~IP_CMSG_TTL;
983 break;
984 case IP_RECVTOS:
985 if (val)
986 inet->cmsg_flags |= IP_CMSG_TOS;
987 else
988 inet->cmsg_flags &= ~IP_CMSG_TOS;
989 break;
990 case IP_RECVOPTS:
991 if (val)
992 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
993 else
994 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
995 break;
996 case IP_RETOPTS:
997 if (val)
998 inet->cmsg_flags |= IP_CMSG_RETOPTS;
999 else
1000 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
1001 break;
1002 case IP_PASSSEC:
1003 if (val)
1004 inet->cmsg_flags |= IP_CMSG_PASSSEC;
1005 else
1006 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
1007 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001008 case IP_RECVORIGDSTADDR:
1009 if (val)
1010 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
1011 else
1012 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
1013 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001014 case IP_CHECKSUM:
1015 if (val) {
1016 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
1017 inet_inc_convert_csum(sk);
1018 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
1019 }
1020 } else {
1021 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
1022 inet_dec_convert_csum(sk);
1023 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
1024 }
1025 }
1026 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -04001027 case IP_RECVFRAGSIZE:
1028 if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM)
1029 goto e_inval;
1030 if (val)
1031 inet->cmsg_flags |= IP_CMSG_RECVFRAGSIZE;
1032 else
1033 inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE;
1034 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001035 case IP_TOS: /* This sets both TOS and Precedence */
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +02001036 __ip_sock_set_tos(sk, val);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001037 break;
1038 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001039 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001040 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +00001041 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001042 goto e_inval;
1043 inet->uc_ttl = val;
1044 break;
1045 case IP_HDRINCL:
1046 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001049 }
1050 inet->hdrincl = val ? 1 : 0;
1051 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +00001052 case IP_NODEFRAG:
1053 if (sk->sk_type != SOCK_RAW) {
1054 err = -ENOPROTOOPT;
1055 break;
1056 }
1057 inet->nodefrag = val ? 1 : 0;
1058 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001059 case IP_BIND_ADDRESS_NO_PORT:
1060 inet->bind_address_no_port = val ? 1 : 0;
1061 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001062 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +01001063 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001064 goto e_inval;
1065 inet->pmtudisc = val;
1066 break;
1067 case IP_RECVERR:
1068 inet->recverr = !!val;
1069 if (!val)
1070 skb_queue_purge(&sk->sk_error_queue);
1071 break;
Willem de Bruijneba75c52020-07-10 09:29:02 -04001072 case IP_RECVERR_RFC4884:
1073 if (val < 0 || val > 1)
1074 goto e_inval;
1075 inet->recverr_rfc4884 = !!val;
1076 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001077 case IP_MULTICAST_TTL:
1078 if (sk->sk_type == SOCK_STREAM)
1079 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001080 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001081 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001082 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001083 val = 1;
1084 if (val < 0 || val > 255)
1085 goto e_inval;
1086 inet->mc_ttl = val;
1087 break;
1088 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001089 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001090 goto e_inval;
1091 inet->mc_loop = !!val;
1092 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001093 case IP_UNICAST_IF:
1094 {
1095 struct net_device *dev = NULL;
1096 int ifindex;
David Ahern9515a2e2018-01-24 19:37:38 -08001097 int midx;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001098
1099 if (optlen != sizeof(int))
1100 goto e_inval;
1101
1102 ifindex = (__force int)ntohl((__force __be32)val);
1103 if (ifindex == 0) {
1104 inet->uc_index = 0;
1105 err = 0;
1106 break;
1107 }
1108
1109 dev = dev_get_by_index(sock_net(sk), ifindex);
1110 err = -EADDRNOTAVAIL;
1111 if (!dev)
1112 break;
David Ahern9515a2e2018-01-24 19:37:38 -08001113
1114 midx = l3mdev_master_ifindex(dev);
Erich E. Hoover76e21052012-02-08 09:11:07 +00001115 dev_put(dev);
1116
1117 err = -EINVAL;
David Ahern9515a2e2018-01-24 19:37:38 -08001118 if (sk->sk_bound_dev_if &&
1119 (!midx || midx != sk->sk_bound_dev_if))
Erich E. Hoover76e21052012-02-08 09:11:07 +00001120 break;
1121
1122 inet->uc_index = ifindex;
1123 err = 0;
1124 break;
1125 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001126 case IP_MULTICAST_IF:
1127 {
1128 struct ip_mreqn mreq;
1129 struct net_device *dev = NULL;
David Ahern7bb387c2016-12-29 15:39:37 -08001130 int midx;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001131
1132 if (sk->sk_type == SOCK_STREAM)
1133 goto e_inval;
1134 /*
1135 * Check the arguments are allowable
1136 */
1137
Shan Wei09159212009-09-22 15:41:10 +00001138 if (optlen < sizeof(struct in_addr))
1139 goto e_inval;
1140
Stephen Hemminger132adf52007-03-08 20:44:43 -08001141 err = -EFAULT;
1142 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -08001143 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001144 break;
1145 } else {
1146 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +00001147 if (optlen >= sizeof(struct ip_mreq)) {
1148 if (copy_from_user(&mreq, optval,
1149 sizeof(struct ip_mreq)))
1150 break;
1151 } else if (optlen >= sizeof(struct in_addr)) {
1152 if (copy_from_user(&mreq.imr_address, optval,
1153 sizeof(struct in_addr)))
1154 break;
1155 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001156 }
1157
1158 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -07001159 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001160 inet->mc_index = 0;
1161 inet->mc_addr = 0;
1162 err = 0;
1163 break;
1164 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001165 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +00001166 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001167 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001168 } else
Eric Dumazet55b80502009-10-19 06:41:58 +00001169 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001170
1171
1172 err = -EADDRNOTAVAIL;
1173 if (!dev)
1174 break;
David Ahern7bb387c2016-12-29 15:39:37 -08001175
1176 midx = l3mdev_master_ifindex(dev);
1177
Eric Dumazet55b80502009-10-19 06:41:58 +00001178 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001179
1180 err = -EINVAL;
1181 if (sk->sk_bound_dev_if &&
David Ahern7bb387c2016-12-29 15:39:37 -08001182 mreq.imr_ifindex != sk->sk_bound_dev_if &&
1183 (!midx || midx != sk->sk_bound_dev_if))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001184 break;
1185
1186 inet->mc_index = mreq.imr_ifindex;
1187 inet->mc_addr = mreq.imr_address.s_addr;
1188 err = 0;
1189 break;
1190 }
1191
1192 case IP_ADD_MEMBERSHIP:
1193 case IP_DROP_MEMBERSHIP:
1194 {
1195 struct ip_mreqn mreq;
1196
Flavio Leitnera96fb492007-08-24 22:16:39 -07001197 err = -EPROTO;
1198 if (inet_sk(sk)->is_icsk)
1199 break;
1200
Stephen Hemminger132adf52007-03-08 20:44:43 -08001201 if (optlen < sizeof(struct ip_mreq))
1202 goto e_inval;
1203 err = -EFAULT;
1204 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -08001205 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001206 break;
1207 } else {
1208 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -08001209 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001210 break;
1211 }
1212
1213 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001214 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001215 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001216 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001217 break;
1218 }
1219 case IP_MSFILTER:
1220 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001221 struct ip_msfilter *msf;
1222
1223 if (optlen < IP_MSFILTER_SIZE(0))
1224 goto e_inval;
1225 if (optlen > sysctl_optmem_max) {
1226 err = -ENOBUFS;
1227 break;
1228 }
Al Viroa2c841d2017-05-13 18:26:06 -04001229 msf = memdup_user(optval, optlen);
1230 if (IS_ERR(msf)) {
1231 err = PTR_ERR(msf);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001232 break;
1233 }
1234 /* numsrc >= (1G-4) overflow in 32 bits */
1235 if (msf->imsf_numsrc >= 0x3ffffffcU ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +02001236 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001237 kfree(msf);
1238 err = -ENOBUFS;
1239 break;
1240 }
1241 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
1242 kfree(msf);
1243 err = -EINVAL;
1244 break;
1245 }
1246 err = ip_mc_msfilter(sk, msf, 0);
1247 kfree(msf);
1248 break;
1249 }
1250 case IP_BLOCK_SOURCE:
1251 case IP_UNBLOCK_SOURCE:
1252 case IP_ADD_SOURCE_MEMBERSHIP:
1253 case IP_DROP_SOURCE_MEMBERSHIP:
1254 {
1255 struct ip_mreq_source mreqs;
1256 int omode, add;
1257
1258 if (optlen != sizeof(struct ip_mreq_source))
1259 goto e_inval;
1260 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
1261 err = -EFAULT;
1262 break;
1263 }
1264 if (optname == IP_BLOCK_SOURCE) {
1265 omode = MCAST_EXCLUDE;
1266 add = 1;
1267 } else if (optname == IP_UNBLOCK_SOURCE) {
1268 omode = MCAST_EXCLUDE;
1269 add = 0;
1270 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
1271 struct ip_mreqn mreq;
1272
1273 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
1274 mreq.imr_address.s_addr = mreqs.imr_interface;
1275 mreq.imr_ifindex = 0;
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001276 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001277 if (err && err != -EADDRINUSE)
1278 break;
1279 omode = MCAST_INCLUDE;
1280 add = 1;
1281 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
1282 omode = MCAST_INCLUDE;
1283 add = 0;
1284 }
1285 err = ip_mc_source(add, omode, sk, &mreqs, 0);
1286 break;
1287 }
1288 case MCAST_JOIN_GROUP:
1289 case MCAST_LEAVE_GROUP:
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001290 if (in_compat_syscall())
1291 err = compat_ip_mcast_join_leave(sk, optname, optval,
1292 optlen);
1293 else
1294 err = ip_mcast_join_leave(sk, optname, optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001295 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001296 case MCAST_JOIN_SOURCE_GROUP:
1297 case MCAST_LEAVE_SOURCE_GROUP:
1298 case MCAST_BLOCK_SOURCE:
1299 case MCAST_UNBLOCK_SOURCE:
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001300 err = do_mcast_group_source(sk, optname, optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001301 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001302 case MCAST_MSFILTER:
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001303 if (in_compat_syscall())
1304 err = compat_ip_set_mcast_msfilter(sk, optval, optlen);
1305 else
1306 err = ip_set_mcast_msfilter(sk, optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001307 break;
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001308 case IP_MULTICAST_ALL:
1309 if (optlen < 1)
1310 goto e_inval;
1311 if (val != 0 && val != 1)
1312 goto e_inval;
1313 inet->mc_all = val;
1314 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001315
1316 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001317 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001318 goto e_inval;
1319 inet->freebind = !!val;
1320 break;
1321
1322 case IP_IPSEC_POLICY:
1323 case IP_XFRM_POLICY:
1324 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001325 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001326 break;
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02001327 err = xfrm_user_policy(sk, optname, USER_SOCKPTR(optval),
1328 optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001329 break;
1330
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001331 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001332 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1333 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001334 err = -EPERM;
1335 break;
1336 }
1337 if (optlen < 1)
1338 goto e_inval;
1339 inet->transparent = !!val;
1340 break;
1341
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001342 case IP_MINTTL:
1343 if (optlen < 1)
1344 goto e_inval;
1345 if (val < 0 || val > 255)
1346 goto e_inval;
1347 inet->min_ttl = val;
1348 break;
1349
Stephen Hemminger132adf52007-03-08 20:44:43 -08001350 default:
1351 err = -ENOPROTOOPT;
1352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001355 if (needs_rtnl)
1356 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return err;
1358
1359e_inval:
1360 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001361 if (needs_rtnl)
1362 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return -EINVAL;
1364}
1365
Eric Dumazetf84af322010-04-28 15:31:51 -07001366/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001367 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001368 * @sk: socket
1369 * @skb: buffer
1370 *
David S. Miller35ebf652012-06-28 03:59:11 -07001371 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1372 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001373 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001374 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001375void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001376{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001377 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001378 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1379 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001380
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001381 if (prepare && skb_rtable(skb)) {
David Ahern0b922b72016-05-10 11:19:51 -07001382 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1383 * which has interface index (iif) as the first member of the
1384 * underlying inet{6}_skb_parm struct. This code then overlays
1385 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001386 * element so the iif is picked up from the prior IPCB. If iif
1387 * is the loopback interface, then return the sending interface
1388 * (e.g., process binds socket to eth0 for Tx which is
1389 * redirected to loopback in the rtable/dst).
David Ahern0b922b72016-05-10 11:19:51 -07001390 */
David Aherncbea8f02017-09-13 17:11:37 -07001391 struct rtable *rt = skb_rtable(skb);
1392 bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags);
1393
1394 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001395 pktinfo->ipi_ifindex = inet_iif(skb);
David Aherncbea8f02017-09-13 17:11:37 -07001396 else if (l3slave && rt && rt->rt_iif)
1397 pktinfo->ipi_ifindex = rt->rt_iif;
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001398
David S. Miller35ebf652012-06-28 03:59:11 -07001399 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001400 } else {
1401 pktinfo->ipi_ifindex = 0;
1402 pktinfo->ipi_spec_dst.s_addr = 0;
1403 }
Paolo Abeni61a10302017-08-03 18:07:07 +02001404 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001405}
Eric Dumazetf84af322010-04-28 15:31:51 -07001406
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001407int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001408 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001409{
1410 int err;
1411
1412 if (level != SOL_IP)
1413 return -ENOPROTOOPT;
1414
1415 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
Taehee Yoo97adadd2018-11-05 22:31:41 +09001416#if IS_ENABLED(CONFIG_BPFILTER_UMH)
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001417 if (optname >= BPFILTER_IPT_SO_SET_REPLACE &&
1418 optname < BPFILTER_IPT_SET_MAX)
Christoph Hellwigb03afaa2020-07-23 08:08:55 +02001419 err = bpfilter_ip_set_sockopt(sk, optname, USER_SOCKPTR(optval),
1420 optlen);
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001421#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001422#ifdef CONFIG_NETFILTER
1423 /* we need to exclude all possible ENOPROTOOPTs except default case */
1424 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001425 optname != IP_IPSEC_POLICY &&
1426 optname != IP_XFRM_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001427 !ip_mroute_opt(optname))
Christoph Hellwigc2f12632020-07-23 08:08:54 +02001428 err = nf_setsockopt(sk, PF_INET, optname, USER_SOCKPTR(optval),
1429 optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001430#endif
1431 return err;
1432}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001433EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001436 * Get the options. Note for future reference. The GET of IP options gets
1437 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
1439
WANG Cong87e9f032015-11-03 15:41:16 -08001440static bool getsockopt_needs_rtnl(int optname)
1441{
1442 switch (optname) {
1443 case IP_MSFILTER:
1444 case MCAST_MSFILTER:
1445 return true;
1446 }
1447 return false;
1448}
1449
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001450static int ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
1451 int __user *optlen, int len)
1452{
1453 const int size0 = offsetof(struct group_filter, gf_slist);
1454 struct group_filter __user *p = optval;
1455 struct group_filter gsf;
1456 int num;
1457 int err;
1458
1459 if (len < size0)
1460 return -EINVAL;
1461 if (copy_from_user(&gsf, p, size0))
1462 return -EFAULT;
1463
1464 num = gsf.gf_numsrc;
1465 err = ip_mc_gsfget(sk, &gsf, p->gf_slist);
1466 if (err)
1467 return err;
1468 if (gsf.gf_numsrc < num)
1469 num = gsf.gf_numsrc;
1470 if (put_user(GROUP_FILTER_SIZE(num), optlen) ||
1471 copy_to_user(p, &gsf, size0))
1472 return -EFAULT;
1473 return 0;
1474}
1475
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001476static int compat_ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001477 int __user *optlen, int len)
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001478{
1479 const int size0 = offsetof(struct compat_group_filter, gf_slist);
1480 struct compat_group_filter __user *p = optval;
1481 struct compat_group_filter gf32;
1482 struct group_filter gf;
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001483 int num;
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001484 int err;
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001485
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001486 if (len < size0)
1487 return -EINVAL;
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001488 if (copy_from_user(&gf32, p, size0))
1489 return -EFAULT;
1490
1491 gf.gf_interface = gf32.gf_interface;
1492 gf.gf_fmode = gf32.gf_fmode;
1493 num = gf.gf_numsrc = gf32.gf_numsrc;
1494 gf.gf_group = gf32.gf_group;
1495
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001496 err = ip_mc_gsfget(sk, &gf, p->gf_slist);
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001497 if (err)
1498 return err;
1499 if (gf.gf_numsrc < num)
1500 num = gf.gf_numsrc;
1501 len = GROUP_FILTER_SIZE(num) - (sizeof(gf) - sizeof(gf32));
1502 if (put_user(len, optlen) ||
1503 put_user(gf.gf_fmode, &p->gf_fmode) ||
1504 put_user(gf.gf_numsrc, &p->gf_numsrc))
1505 return -EFAULT;
1506 return 0;
1507}
Christoph Hellwig49e74c22020-07-17 08:23:23 +02001508
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001509static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001510 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 struct inet_sock *inet = inet_sk(sk);
WANG Cong87e9f032015-11-03 15:41:16 -08001513 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1514 int val, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001516
Stephen Hemminger132adf52007-03-08 20:44:43 -08001517 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return -EOPNOTSUPP;
1519
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001520 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001521 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Jianjun Kong09cb1052008-11-03 00:27:11 -08001523 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001525 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001527
WANG Cong87e9f032015-11-03 15:41:16 -08001528 if (needs_rtnl)
1529 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 lock_sock(sk);
1531
Stephen Hemminger132adf52007-03-08 20:44:43 -08001532 switch (optname) {
1533 case IP_OPTIONS:
1534 {
1535 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001536 struct ip_options *opt = (struct ip_options *)optbuf;
1537 struct ip_options_rcu *inet_opt;
1538
1539 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001540 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001541 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001542 if (inet_opt)
1543 memcpy(optbuf, &inet_opt->opt,
1544 sizeof(struct ip_options) +
1545 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001546 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Stephen Hemminger132adf52007-03-08 20:44:43 -08001548 if (opt->optlen == 0)
1549 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Stephen Hemminger132adf52007-03-08 20:44:43 -08001551 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Stephen Hemminger132adf52007-03-08 20:44:43 -08001553 len = min_t(unsigned int, len, opt->optlen);
1554 if (put_user(len, optlen))
1555 return -EFAULT;
1556 if (copy_to_user(optval, opt->__data, len))
1557 return -EFAULT;
1558 return 0;
1559 }
1560 case IP_PKTINFO:
1561 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1562 break;
1563 case IP_RECVTTL:
1564 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1565 break;
1566 case IP_RECVTOS:
1567 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1568 break;
1569 case IP_RECVOPTS:
1570 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1571 break;
1572 case IP_RETOPTS:
1573 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1574 break;
1575 case IP_PASSSEC:
1576 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1577 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001578 case IP_RECVORIGDSTADDR:
1579 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1580 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001581 case IP_CHECKSUM:
1582 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1583 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -04001584 case IP_RECVFRAGSIZE:
1585 val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0;
1586 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001587 case IP_TOS:
1588 val = inet->tos;
1589 break;
1590 case IP_TTL:
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001591 {
1592 struct net *net = sock_net(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001593 val = (inet->uc_ttl == -1 ?
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001594 net->ipv4.sysctl_ip_default_ttl :
Stephen Hemminger132adf52007-03-08 20:44:43 -08001595 inet->uc_ttl);
1596 break;
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001597 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001598 case IP_HDRINCL:
1599 val = inet->hdrincl;
1600 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001601 case IP_NODEFRAG:
1602 val = inet->nodefrag;
1603 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001604 case IP_BIND_ADDRESS_NO_PORT:
1605 val = inet->bind_address_no_port;
1606 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001607 case IP_MTU_DISCOVER:
1608 val = inet->pmtudisc;
1609 break;
1610 case IP_MTU:
1611 {
1612 struct dst_entry *dst;
1613 val = 0;
1614 dst = sk_dst_get(sk);
1615 if (dst) {
1616 val = dst_mtu(dst);
1617 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001619 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001621 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001623 break;
1624 }
1625 case IP_RECVERR:
1626 val = inet->recverr;
1627 break;
Willem de Bruijneba75c52020-07-10 09:29:02 -04001628 case IP_RECVERR_RFC4884:
1629 val = inet->recverr_rfc4884;
1630 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001631 case IP_MULTICAST_TTL:
1632 val = inet->mc_ttl;
1633 break;
1634 case IP_MULTICAST_LOOP:
1635 val = inet->mc_loop;
1636 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001637 case IP_UNICAST_IF:
1638 val = (__force int)htonl((__u32) inet->uc_index);
1639 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001640 case IP_MULTICAST_IF:
1641 {
1642 struct in_addr addr;
1643 len = min_t(unsigned int, len, sizeof(struct in_addr));
1644 addr.s_addr = inet->mc_addr;
1645 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Stephen Hemminger132adf52007-03-08 20:44:43 -08001647 if (put_user(len, optlen))
1648 return -EFAULT;
1649 if (copy_to_user(optval, &addr, len))
1650 return -EFAULT;
1651 return 0;
1652 }
1653 case IP_MSFILTER:
1654 {
1655 struct ip_msfilter msf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001656
1657 if (len < IP_MSFILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001658 err = -EINVAL;
1659 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001661 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001662 err = -EFAULT;
1663 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001665 err = ip_mc_msfget(sk, &msf,
1666 (struct ip_msfilter __user *)optval, optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001667 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001668 }
1669 case MCAST_MSFILTER:
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001670 if (in_compat_syscall())
1671 err = compat_ip_get_mcast_msfilter(sk, optval, optlen,
1672 len);
1673 else
1674 err = ip_get_mcast_msfilter(sk, optval, optlen, len);
WANG Cong87e9f032015-11-03 15:41:16 -08001675 goto out;
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001676 case IP_MULTICAST_ALL:
1677 val = inet->mc_all;
1678 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001679 case IP_PKTOPTIONS:
1680 {
1681 struct msghdr msg;
1682
1683 release_sock(sk);
1684
1685 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001687
Christoph Hellwig1f466e12020-05-11 13:59:13 +02001688 msg.msg_control_is_user = true;
1689 msg.msg_control_user = optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001690 msg.msg_controllen = len;
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001691 msg.msg_flags = in_compat_syscall() ? MSG_CMSG_COMPAT : 0;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001692
1693 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1694 struct in_pktinfo info;
1695
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001696 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1697 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001698 info.ipi_ifindex = inet->mc_index;
1699 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1700 }
1701 if (inet->cmsg_flags & IP_CMSG_TTL) {
1702 int hlim = inet->mc_ttl;
1703 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1704 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001705 if (inet->cmsg_flags & IP_CMSG_TOS) {
1706 int tos = inet->rcv_tos;
1707 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1708 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001709 len -= msg.msg_controllen;
1710 return put_user(len, optlen);
1711 }
1712 case IP_FREEBIND:
1713 val = inet->freebind;
1714 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001715 case IP_TRANSPARENT:
1716 val = inet->transparent;
1717 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001718 case IP_MINTTL:
1719 val = inet->min_ttl;
1720 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001721 default:
1722 release_sock(sk);
1723 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001726
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001727 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 unsigned char ucval = (unsigned char)val;
1729 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001730 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001732 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return -EFAULT;
1734 } else {
1735 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001736 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001738 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return -EFAULT;
1740 }
1741 return 0;
WANG Cong87e9f032015-11-03 15:41:16 -08001742
1743out:
1744 release_sock(sk);
1745 if (needs_rtnl)
1746 rtnl_unlock();
1747 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001750int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001751 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001752{
1753 int err;
1754
Christoph Hellwigb6238c02020-07-17 08:23:26 +02001755 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1756
Taehee Yoo97adadd2018-11-05 22:31:41 +09001757#if IS_ENABLED(CONFIG_BPFILTER_UMH)
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001758 if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1759 optname < BPFILTER_IPT_GET_MAX)
1760 err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1761#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001762#ifdef CONFIG_NETFILTER
1763 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001764 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1765 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001766 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001767
Jianjun Kong09cb1052008-11-03 00:27:11 -08001768 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001769 return -EFAULT;
1770
Paolo Abeni01ea3062018-02-08 12:19:00 +01001771 err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001772 if (err >= 0)
1773 err = put_user(len, optlen);
1774 return err;
1775 }
1776#endif
1777 return err;
1778}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001779EXPORT_SYMBOL(ip_getsockopt);