blob: 5ad2d8ed3a3fe2aa51d814af442df7ff5e074d3e [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * SOL_IP control messages.
52 */
53
54static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
55{
Eric Dumazetd826eb12011-11-09 07:24:35 +000056 struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070058 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
61}
62
63static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
64{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070065 int ttl = ip_hdr(skb)->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
67}
68
69static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
70{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070071 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
75{
76 if (IPCB(skb)->opt.optlen == 0)
77 return;
78
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070079 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
80 ip_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83
Paolo Abeni91ed1e62017-08-03 18:07:06 +020084static void ip_cmsg_recv_retopts(struct net *net, struct msghdr *msg,
85 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 unsigned char optbuf[sizeof(struct ip_options) + 40];
Daniel Baluta5e73ea12012-04-15 01:34:41 +000088 struct ip_options *opt = (struct ip_options *)optbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 if (IPCB(skb)->opt.optlen == 0)
91 return;
92
Paolo Abeni91ed1e62017-08-03 18:07:06 +020093 if (ip_options_echo(net, opt, skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 msg->msg_flags |= MSG_CTRUNC;
95 return;
96 }
97 ip_options_undo(opt);
98
99 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
100}
101
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400102static void ip_cmsg_recv_fragsize(struct msghdr *msg, struct sk_buff *skb)
103{
104 int val;
105
106 if (IPCB(skb)->frag_max_size == 0)
107 return;
108
109 val = IPCB(skb)->frag_max_size;
110 put_cmsg(msg, SOL_IP, IP_RECVFRAGSIZE, sizeof(val), &val);
111}
112
Tom Herbertad6f9392015-01-05 13:56:17 -0800113static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb,
Eric Dumazet10df8e62016-10-23 18:03:06 -0700114 int tlen, int offset)
Tom Herbertad6f9392015-01-05 13:56:17 -0800115{
116 __wsum csum = skb->csum;
117
118 if (skb->ip_summed != CHECKSUM_COMPLETE)
119 return;
120
Paolo Abenica4ef452017-02-21 09:33:18 +0100121 if (offset != 0) {
122 int tend_off = skb_transport_offset(skb) + tlen;
123 csum = csum_sub(csum, skb_checksum(skb, tend_off, offset, 0));
124 }
Tom Herbertad6f9392015-01-05 13:56:17 -0800125
126 put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum);
127}
128
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800129static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
130{
131 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700132 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800133 int err;
134
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700135 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
136 if (err)
137 return;
138
139 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800140 if (err)
141 return;
142
143 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700144 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800145}
146
Harvey Harrison21d1a162008-11-20 01:54:27 -0800147static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800148{
149 struct sockaddr_in sin;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000150 const struct iphdr *iph = ip_hdr(skb);
Harvey Harrison21d1a162008-11-20 01:54:27 -0800151 __be16 *ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800152
Willem de Bruijn39b2dd72016-12-22 18:19:16 -0500153 if (skb_transport_offset(skb) + 4 > (int)skb->len)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800154 return;
155
156 /* All current transport protocols have the port numbers in the
157 * first four bytes of the transport header and this function is
158 * written with this assumption in mind.
159 */
160
161 sin.sin_family = AF_INET;
162 sin.sin_addr.s_addr = iph->daddr;
163 sin.sin_port = ports[1];
164 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
165
166 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
167}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Paolo Abeniad959032016-11-04 11:28:58 +0100169void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk,
170 struct sk_buff *skb, int tlen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Paolo Abeniad959032016-11-04 11:28:58 +0100172 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000173 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /* Ordered by supposed usage frequency */
Tom Herbertc44d13d2015-01-05 13:56:15 -0800176 if (flags & IP_CMSG_PKTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 ip_cmsg_recv_pktinfo(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Tom Herbertc44d13d2015-01-05 13:56:15 -0800179 flags &= ~IP_CMSG_PKTINFO;
180 if (!flags)
181 return;
182 }
183
184 if (flags & IP_CMSG_TTL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 ip_cmsg_recv_ttl(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Tom Herbertc44d13d2015-01-05 13:56:15 -0800187 flags &= ~IP_CMSG_TTL;
188 if (!flags)
189 return;
190 }
191
192 if (flags & IP_CMSG_TOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 ip_cmsg_recv_tos(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Tom Herbertc44d13d2015-01-05 13:56:15 -0800195 flags &= ~IP_CMSG_TOS;
196 if (!flags)
197 return;
198 }
199
200 if (flags & IP_CMSG_RECVOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 ip_cmsg_recv_opts(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Tom Herbertc44d13d2015-01-05 13:56:15 -0800203 flags &= ~IP_CMSG_RECVOPTS;
204 if (!flags)
205 return;
206 }
207
208 if (flags & IP_CMSG_RETOPTS) {
Paolo Abeni91ed1e62017-08-03 18:07:06 +0200209 ip_cmsg_recv_retopts(sock_net(sk), msg, skb);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800210
Tom Herbertc44d13d2015-01-05 13:56:15 -0800211 flags &= ~IP_CMSG_RETOPTS;
212 if (!flags)
213 return;
214 }
215
216 if (flags & IP_CMSG_PASSSEC) {
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800217 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800218
Tom Herbertc44d13d2015-01-05 13:56:15 -0800219 flags &= ~IP_CMSG_PASSSEC;
220 if (!flags)
221 return;
222 }
223
Tom Herbertad6f9392015-01-05 13:56:17 -0800224 if (flags & IP_CMSG_ORIGDSTADDR) {
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800225 ip_cmsg_recv_dstaddr(msg, skb);
226
Tom Herbertad6f9392015-01-05 13:56:17 -0800227 flags &= ~IP_CMSG_ORIGDSTADDR;
228 if (!flags)
229 return;
230 }
231
232 if (flags & IP_CMSG_CHECKSUM)
Eric Dumazet10df8e62016-10-23 18:03:06 -0700233 ip_cmsg_recv_checksum(msg, skb, tlen, offset);
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400234
235 if (flags & IP_CMSG_RECVFRAGSIZE)
236 ip_cmsg_recv_fragsize(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
Tom Herbert5961de92015-01-05 13:56:16 -0800238EXPORT_SYMBOL(ip_cmsg_recv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400240int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100241 bool allow_ipv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Francesco Fuscof02db312013-09-24 15:43:08 +0200243 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 struct cmsghdr *cmsg;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400245 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Gu Zhengf95b4142014-12-11 11:22:04 +0800247 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (!CMSG_OK(msg, cmsg))
249 return -EINVAL;
Eric Dumazet5337b5b2014-11-10 17:54:25 -0800250#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100251 if (allow_ipv6 &&
252 cmsg->cmsg_level == SOL_IPV6 &&
253 cmsg->cmsg_type == IPV6_PKTINFO) {
254 struct in6_pktinfo *src_info;
255
256 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
257 return -EINVAL;
258 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
259 if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
260 return -EINVAL;
David Ahern1cbec072018-02-16 11:03:03 -0800261 if (src_info->ipi6_ifindex)
262 ipc->oif = src_info->ipi6_ifindex;
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100263 ipc->addr = src_info->ipi6_addr.s6_addr32[3];
264 continue;
265 }
266#endif
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400267 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700268 err = __sock_cmsg_send(sk, msg, cmsg, &ipc->sockc);
269 if (err)
270 return err;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400271 continue;
272 }
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (cmsg->cmsg_level != SOL_IP)
275 continue;
276 switch (cmsg->cmsg_type) {
277 case IP_RETOPTS:
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800278 err = cmsg->cmsg_len - sizeof(struct cmsghdr);
Eric Dumazet91948302016-02-04 06:23:28 -0800279
280 /* Our caller is responsible for freeing ipc->opt */
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700281 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
282 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (err)
284 return err;
285 break;
286 case IP_PKTINFO:
287 {
288 struct in_pktinfo *info;
289 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
290 return -EINVAL;
291 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
David Ahern1cbec072018-02-16 11:03:03 -0800292 if (info->ipi_ifindex)
293 ipc->oif = info->ipi_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 ipc->addr = info->ipi_spec_dst.s_addr;
295 break;
296 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200297 case IP_TTL:
298 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
299 return -EINVAL;
300 val = *(int *)CMSG_DATA(cmsg);
301 if (val < 1 || val > 255)
302 return -EINVAL;
303 ipc->ttl = val;
304 break;
305 case IP_TOS:
Eric Dumazete895cdc2016-09-07 21:52:56 -0700306 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)))
307 val = *(int *)CMSG_DATA(cmsg);
308 else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8)))
309 val = *(u8 *)CMSG_DATA(cmsg);
310 else
Francesco Fuscof02db312013-09-24 15:43:08 +0200311 return -EINVAL;
Francesco Fuscof02db312013-09-24 15:43:08 +0200312 if (val < 0 || val > 255)
313 return -EINVAL;
314 ipc->tos = val;
315 ipc->priority = rt_tos2priority(ipc->tos);
316 break;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 default:
319 return -EINVAL;
320 }
321 }
322 return 0;
323}
324
Eric Dumazet592fcb92010-06-09 16:21:07 +0000325static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000326{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000327 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
328
329 sock_put(ra->saved_sk);
330 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000331}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700333int ip_ra_control(struct sock *sk, unsigned char on,
334 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000336 struct ip_ra_chain *ra, *new_ra;
337 struct ip_ra_chain __rcu **rap;
Kirill Tkhai5796ef72018-03-22 12:45:32 +0300338 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000340 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -EINVAL;
342
343 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
344
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300345 mutex_lock(&net->ipv4.ra_mutex);
Kirill Tkhai5796ef72018-03-22 12:45:32 +0300346 for (rap = &net->ipv4.ra_chain;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300347 (ra = rcu_dereference_protected(*rap,
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300348 lockdep_is_held(&net->ipv4.ra_mutex))) != NULL;
Eric Dumazet43a951e2010-10-25 03:32:44 +0000349 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (ra->sk == sk) {
351 if (on) {
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300352 mutex_unlock(&net->ipv4.ra_mutex);
Jesper Juhla51482b2005-11-08 09:41:34 -0800353 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -EADDRINUSE;
355 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000356 /* dont let ip_call_ra_chain() use sk again */
357 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700358 RCU_INIT_POINTER(*rap, ra->next);
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300359 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 if (ra->destructor)
362 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000363 /*
364 * Delay sock_put(sk) and kfree(ra) after one rcu grace
365 * period. This guarantee ip_call_ra_chain() dont need
366 * to mess with socket refcounts.
367 */
368 ra->saved_sk = sk;
369 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 0;
371 }
372 }
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300373 if (!new_ra) {
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300374 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -ENOBUFS;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 new_ra->sk = sk;
378 new_ra->destructor = destructor;
379
Eric Dumazet8e380f02014-09-09 08:11:41 -0700380 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000381 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 sock_hold(sk);
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300383 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 return 0;
386}
387
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900388void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700389 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct sock_exterr_skb *serr;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 skb = skb_clone(skb, GFP_ATOMIC);
394 if (!skb)
395 return;
396
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900397 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 serr->ee.ee_errno = err;
399 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300400 serr->ee.ee_type = icmp_hdr(skb)->type;
401 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 serr->ee.ee_pad = 0;
403 serr->ee.ee_info = info;
404 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300405 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700406 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 serr->port = port;
408
Ian Morris00db4122015-04-03 09:17:27 +0100409 if (skb_pull(skb, payload - skb->data)) {
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300410 skb_reset_transport_header(skb);
411 if (sock_queue_err_skb(sk, skb) == 0)
412 return;
413 }
414 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Al Viro05790162006-09-27 18:33:40 -0700417void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct inet_sock *inet = inet_sk(sk);
420 struct sock_exterr_skb *serr;
421 struct iphdr *iph;
422 struct sk_buff *skb;
423
424 if (!inet->recverr)
425 return;
426
427 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
428 if (!skb)
429 return;
430
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300431 skb_put(skb, sizeof(struct iphdr));
432 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700433 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 iph->daddr = daddr;
435
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900436 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 serr->ee.ee_errno = err;
438 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900439 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 serr->ee.ee_code = 0;
441 serr->ee.ee_pad = 0;
442 serr->ee.ee_info = info;
443 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700444 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 serr->port = port;
446
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700447 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300448 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 if (sock_queue_err_skb(sk, skb))
451 kfree_skb(skb);
452}
453
Julian Anastasov34b99df2015-06-23 08:34:39 +0300454/* For some errors we have valid addr_offset even with zero payload and
455 * zero port. Also, addr_offset should be supported if port is set.
456 */
457static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
458{
459 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
460 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
461}
462
Willem de Bruijnc247f052015-03-07 20:33:22 -0500463/* IPv4 supports cmsg on all imcp errors and some timestamps
464 *
465 * Timestamp code paths do not initialize the fields expected by cmsg:
466 * the PKTINFO fields in skb->cb[]. Fill those in here.
467 */
468static bool ipv4_datagram_support_cmsg(const struct sock *sk,
469 struct sk_buff *skb,
470 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500471{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500472 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500473
Willem de Bruijnc247f052015-03-07 20:33:22 -0500474 if (ee_origin == SO_EE_ORIGIN_ICMP)
475 return true;
476
477 if (ee_origin == SO_EE_ORIGIN_LOCAL)
478 return false;
479
480 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
Willem de Bruijn1862d622017-04-12 19:24:35 -0400481 * timestamp with egress dev. Not possible for packets without iif
Willem de Bruijnc247f052015-03-07 20:33:22 -0500482 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
483 */
Willem de Bruijn1862d622017-04-12 19:24:35 -0400484 info = PKTINFO_SKB_CB(skb);
485 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
486 !info->ipi_ifindex)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500487 return false;
488
489 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500490 return true;
491}
492
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900493/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * Handle MSG_ERRQUEUE
495 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100496int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400499 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100500 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct {
502 struct sock_extended_err ee;
503 struct sockaddr_in offender;
504 } errhdr;
505 int err;
506 int copied;
507
Willem de Bruijn7ce875e2014-11-30 22:22:33 -0500508 WARN_ON_ONCE(sk->sk_family == AF_INET6);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400511 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100512 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto out;
514
515 copied = skb->len;
516 if (copied > len) {
517 msg->msg_flags |= MSG_TRUNC;
518 copied = len;
519 }
David S. Miller51f3d022014-11-05 16:46:40 -0500520 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700521 if (unlikely(err)) {
522 kfree_skb(skb);
523 return err;
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 sock_recv_timestamp(msg, sk, skb);
526
527 serr = SKB_EXT_ERR(skb);
528
Julian Anastasov34b99df2015-06-23 08:34:39 +0300529 if (sin && ipv4_datagram_support_addr(serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700531 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
532 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 sin->sin_port = serr->port;
534 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100535 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537
538 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
539 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500540 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500541
Willem de Bruijnc247f052015-03-07 20:33:22 -0500542 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700544 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500545 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 ip_cmsg_recv(msg, skb);
547 }
548
549 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
550
551 /* Now we could try to dump offended packet options */
552
553 msg->msg_flags |= MSG_ERRQUEUE;
554 err = copied;
555
Eric Dumazet960a2622016-04-21 22:27:32 -0700556 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557out:
558 return err;
559}
560
561
562/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700563 * Socket option code for IP. This is the end of the line after any
564 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300566static bool setsockopt_needs_rtnl(int optname)
567{
568 switch (optname) {
569 case IP_ADD_MEMBERSHIP:
570 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300571 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300572 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300573 case IP_DROP_SOURCE_MEMBERSHIP:
574 case IP_MSFILTER:
575 case IP_UNBLOCK_SOURCE:
576 case MCAST_BLOCK_SOURCE:
577 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300578 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300579 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300580 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300581 case MCAST_LEAVE_SOURCE_GROUP:
582 case MCAST_UNBLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300583 return true;
584 }
585 return false;
586}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800588static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700589 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 struct inet_sock *inet = inet_sk(sk);
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200592 struct net *net = sock_net(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800593 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300594 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Xi Wang0c9f79b2012-11-11 11:20:01 +0000596 switch (optname) {
597 case IP_PKTINFO:
598 case IP_RECVTTL:
599 case IP_RECVOPTS:
600 case IP_RECVTOS:
601 case IP_RETOPTS:
602 case IP_TOS:
603 case IP_TTL:
604 case IP_HDRINCL:
605 case IP_MTU_DISCOVER:
606 case IP_RECVERR:
607 case IP_ROUTER_ALERT:
608 case IP_FREEBIND:
609 case IP_PASSSEC:
610 case IP_TRANSPARENT:
611 case IP_MINTTL:
612 case IP_NODEFRAG:
Eric Dumazet90c337d2015-06-06 21:17:57 -0700613 case IP_BIND_ADDRESS_NO_PORT:
Xi Wang0c9f79b2012-11-11 11:20:01 +0000614 case IP_UNICAST_IF:
615 case IP_MULTICAST_TTL:
616 case IP_MULTICAST_ALL:
617 case IP_MULTICAST_LOOP:
618 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800619 case IP_CHECKSUM:
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400620 case IP_RECVFRAGSIZE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (optlen >= sizeof(int)) {
622 if (get_user(val, (int __user *) optval))
623 return -EFAULT;
624 } else if (optlen >= sizeof(char)) {
625 unsigned char ucval;
626
627 if (get_user(ucval, (unsigned char __user *) optval))
628 return -EFAULT;
629 val = (int) ucval;
630 }
631 }
632
633 /* If optlen==0, it is equivalent to val == 0 */
634
Kirill Tkhai05269472018-03-22 12:45:12 +0300635 if (optname == IP_ROUTER_ALERT)
636 return ip_ra_control(sk, val ? 1 : 0, NULL);
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800637 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800638 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300641 if (needs_rtnl)
642 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 lock_sock(sk);
644
645 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800646 case IP_OPTIONS:
647 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000648 struct ip_options_rcu *old, *opt = NULL;
649
roel kluin65a1c4f2009-10-23 05:59:21 +0000650 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800651 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900652 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700653 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800654 if (err)
655 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000656 old = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200657 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800658 if (inet->is_icsk) {
659 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000660#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800661 if (sk->sk_family == PF_INET ||
662 (!((1 << sk->sk_state) &
663 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000664 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000666 if (old)
667 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800668 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000669 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800670 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000671#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000675 rcu_assign_pointer(inet->inet_opt, opt);
676 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800677 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800678 break;
679 }
680 case IP_PKTINFO:
681 if (val)
682 inet->cmsg_flags |= IP_CMSG_PKTINFO;
683 else
684 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
685 break;
686 case IP_RECVTTL:
687 if (val)
688 inet->cmsg_flags |= IP_CMSG_TTL;
689 else
690 inet->cmsg_flags &= ~IP_CMSG_TTL;
691 break;
692 case IP_RECVTOS:
693 if (val)
694 inet->cmsg_flags |= IP_CMSG_TOS;
695 else
696 inet->cmsg_flags &= ~IP_CMSG_TOS;
697 break;
698 case IP_RECVOPTS:
699 if (val)
700 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
701 else
702 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
703 break;
704 case IP_RETOPTS:
705 if (val)
706 inet->cmsg_flags |= IP_CMSG_RETOPTS;
707 else
708 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
709 break;
710 case IP_PASSSEC:
711 if (val)
712 inet->cmsg_flags |= IP_CMSG_PASSSEC;
713 else
714 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
715 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800716 case IP_RECVORIGDSTADDR:
717 if (val)
718 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
719 else
720 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
721 break;
Tom Herbertad6f9392015-01-05 13:56:17 -0800722 case IP_CHECKSUM:
723 if (val) {
724 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
725 inet_inc_convert_csum(sk);
726 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
727 }
728 } else {
729 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
730 inet_dec_convert_csum(sk);
731 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
732 }
733 }
734 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400735 case IP_RECVFRAGSIZE:
736 if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM)
737 goto e_inval;
738 if (val)
739 inet->cmsg_flags |= IP_CMSG_RECVFRAGSIZE;
740 else
741 inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE;
742 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800743 case IP_TOS: /* This sets both TOS and Precedence */
744 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400745 val &= ~INET_ECN_MASK;
746 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800748 if (inet->tos != val) {
749 inet->tos = val;
750 sk->sk_priority = rt_tos2priority(val);
751 sk_dst_reset(sk);
752 }
753 break;
754 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700755 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800756 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000757 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800758 goto e_inval;
759 inet->uc_ttl = val;
760 break;
761 case IP_HDRINCL:
762 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800765 }
766 inet->hdrincl = val ? 1 : 0;
767 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000768 case IP_NODEFRAG:
769 if (sk->sk_type != SOCK_RAW) {
770 err = -ENOPROTOOPT;
771 break;
772 }
773 inet->nodefrag = val ? 1 : 0;
774 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -0700775 case IP_BIND_ADDRESS_NO_PORT:
776 inet->bind_address_no_port = val ? 1 : 0;
777 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800778 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100779 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800780 goto e_inval;
781 inet->pmtudisc = val;
782 break;
783 case IP_RECVERR:
784 inet->recverr = !!val;
785 if (!val)
786 skb_queue_purge(&sk->sk_error_queue);
787 break;
788 case IP_MULTICAST_TTL:
789 if (sk->sk_type == SOCK_STREAM)
790 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700791 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800792 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800793 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800794 val = 1;
795 if (val < 0 || val > 255)
796 goto e_inval;
797 inet->mc_ttl = val;
798 break;
799 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700800 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800801 goto e_inval;
802 inet->mc_loop = !!val;
803 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000804 case IP_UNICAST_IF:
805 {
806 struct net_device *dev = NULL;
807 int ifindex;
David Ahern9515a2e2018-01-24 19:37:38 -0800808 int midx;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000809
810 if (optlen != sizeof(int))
811 goto e_inval;
812
813 ifindex = (__force int)ntohl((__force __be32)val);
814 if (ifindex == 0) {
815 inet->uc_index = 0;
816 err = 0;
817 break;
818 }
819
820 dev = dev_get_by_index(sock_net(sk), ifindex);
821 err = -EADDRNOTAVAIL;
822 if (!dev)
823 break;
David Ahern9515a2e2018-01-24 19:37:38 -0800824
825 midx = l3mdev_master_ifindex(dev);
Erich E. Hoover76e21052012-02-08 09:11:07 +0000826 dev_put(dev);
827
828 err = -EINVAL;
David Ahern9515a2e2018-01-24 19:37:38 -0800829 if (sk->sk_bound_dev_if &&
830 (!midx || midx != sk->sk_bound_dev_if))
Erich E. Hoover76e21052012-02-08 09:11:07 +0000831 break;
832
833 inet->uc_index = ifindex;
834 err = 0;
835 break;
836 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800837 case IP_MULTICAST_IF:
838 {
839 struct ip_mreqn mreq;
840 struct net_device *dev = NULL;
David Ahern7bb387c2016-12-29 15:39:37 -0800841 int midx;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800842
843 if (sk->sk_type == SOCK_STREAM)
844 goto e_inval;
845 /*
846 * Check the arguments are allowable
847 */
848
Shan Wei09159212009-09-22 15:41:10 +0000849 if (optlen < sizeof(struct in_addr))
850 goto e_inval;
851
Stephen Hemminger132adf52007-03-08 20:44:43 -0800852 err = -EFAULT;
853 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800854 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800855 break;
856 } else {
857 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000858 if (optlen >= sizeof(struct ip_mreq)) {
859 if (copy_from_user(&mreq, optval,
860 sizeof(struct ip_mreq)))
861 break;
862 } else if (optlen >= sizeof(struct in_addr)) {
863 if (copy_from_user(&mreq.imr_address, optval,
864 sizeof(struct in_addr)))
865 break;
866 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800867 }
868
869 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700870 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800871 inet->mc_index = 0;
872 inet->mc_addr = 0;
873 err = 0;
874 break;
875 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900876 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000877 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800878 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800879 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000880 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800881
882
883 err = -EADDRNOTAVAIL;
884 if (!dev)
885 break;
David Ahern7bb387c2016-12-29 15:39:37 -0800886
887 midx = l3mdev_master_ifindex(dev);
888
Eric Dumazet55b80502009-10-19 06:41:58 +0000889 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800890
891 err = -EINVAL;
892 if (sk->sk_bound_dev_if &&
David Ahern7bb387c2016-12-29 15:39:37 -0800893 mreq.imr_ifindex != sk->sk_bound_dev_if &&
894 (!midx || midx != sk->sk_bound_dev_if))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800895 break;
896
897 inet->mc_index = mreq.imr_ifindex;
898 inet->mc_addr = mreq.imr_address.s_addr;
899 err = 0;
900 break;
901 }
902
903 case IP_ADD_MEMBERSHIP:
904 case IP_DROP_MEMBERSHIP:
905 {
906 struct ip_mreqn mreq;
907
Flavio Leitnera96fb492007-08-24 22:16:39 -0700908 err = -EPROTO;
909 if (inet_sk(sk)->is_icsk)
910 break;
911
Stephen Hemminger132adf52007-03-08 20:44:43 -0800912 if (optlen < sizeof(struct ip_mreq))
913 goto e_inval;
914 err = -EFAULT;
915 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800916 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800917 break;
918 } else {
919 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800920 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800921 break;
922 }
923
924 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300925 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800926 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300927 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800928 break;
929 }
930 case IP_MSFILTER:
931 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800932 struct ip_msfilter *msf;
933
934 if (optlen < IP_MSFILTER_SIZE(0))
935 goto e_inval;
936 if (optlen > sysctl_optmem_max) {
937 err = -ENOBUFS;
938 break;
939 }
Al Viroa2c841d2017-05-13 18:26:06 -0400940 msf = memdup_user(optval, optlen);
941 if (IS_ERR(msf)) {
942 err = PTR_ERR(msf);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800943 break;
944 }
945 /* numsrc >= (1G-4) overflow in 32 bits */
946 if (msf->imsf_numsrc >= 0x3ffffffcU ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200947 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800948 kfree(msf);
949 err = -ENOBUFS;
950 break;
951 }
952 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
953 kfree(msf);
954 err = -EINVAL;
955 break;
956 }
957 err = ip_mc_msfilter(sk, msf, 0);
958 kfree(msf);
959 break;
960 }
961 case IP_BLOCK_SOURCE:
962 case IP_UNBLOCK_SOURCE:
963 case IP_ADD_SOURCE_MEMBERSHIP:
964 case IP_DROP_SOURCE_MEMBERSHIP:
965 {
966 struct ip_mreq_source mreqs;
967 int omode, add;
968
969 if (optlen != sizeof(struct ip_mreq_source))
970 goto e_inval;
971 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
972 err = -EFAULT;
973 break;
974 }
975 if (optname == IP_BLOCK_SOURCE) {
976 omode = MCAST_EXCLUDE;
977 add = 1;
978 } else if (optname == IP_UNBLOCK_SOURCE) {
979 omode = MCAST_EXCLUDE;
980 add = 0;
981 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
982 struct ip_mreqn mreq;
983
984 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
985 mreq.imr_address.s_addr = mreqs.imr_interface;
986 mreq.imr_ifindex = 0;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300987 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800988 if (err && err != -EADDRINUSE)
989 break;
990 omode = MCAST_INCLUDE;
991 add = 1;
992 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
993 omode = MCAST_INCLUDE;
994 add = 0;
995 }
996 err = ip_mc_source(add, omode, sk, &mreqs, 0);
997 break;
998 }
999 case MCAST_JOIN_GROUP:
1000 case MCAST_LEAVE_GROUP:
1001 {
1002 struct group_req greq;
1003 struct sockaddr_in *psin;
1004 struct ip_mreqn mreq;
1005
1006 if (optlen < sizeof(struct group_req))
1007 goto e_inval;
1008 err = -EFAULT;
1009 if (copy_from_user(&greq, optval, sizeof(greq)))
1010 break;
1011 psin = (struct sockaddr_in *)&greq.gr_group;
1012 if (psin->sin_family != AF_INET)
1013 goto e_inval;
1014 memset(&mreq, 0, sizeof(mreq));
1015 mreq.imr_multiaddr = psin->sin_addr;
1016 mreq.imr_ifindex = greq.gr_interface;
1017
1018 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001019 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001020 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001021 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001022 break;
1023 }
1024 case MCAST_JOIN_SOURCE_GROUP:
1025 case MCAST_LEAVE_SOURCE_GROUP:
1026 case MCAST_BLOCK_SOURCE:
1027 case MCAST_UNBLOCK_SOURCE:
1028 {
1029 struct group_source_req greqs;
1030 struct ip_mreq_source mreqs;
1031 struct sockaddr_in *psin;
1032 int omode, add;
1033
1034 if (optlen != sizeof(struct group_source_req))
1035 goto e_inval;
1036 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
1037 err = -EFAULT;
1038 break;
1039 }
1040 if (greqs.gsr_group.ss_family != AF_INET ||
1041 greqs.gsr_source.ss_family != AF_INET) {
1042 err = -EADDRNOTAVAIL;
1043 break;
1044 }
1045 psin = (struct sockaddr_in *)&greqs.gsr_group;
1046 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
1047 psin = (struct sockaddr_in *)&greqs.gsr_source;
1048 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
1049 mreqs.imr_interface = 0; /* use index for mc_source */
1050
1051 if (optname == MCAST_BLOCK_SOURCE) {
1052 omode = MCAST_EXCLUDE;
1053 add = 1;
1054 } else if (optname == MCAST_UNBLOCK_SOURCE) {
1055 omode = MCAST_EXCLUDE;
1056 add = 0;
1057 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
1058 struct ip_mreqn mreq;
1059
1060 psin = (struct sockaddr_in *)&greqs.gsr_group;
1061 mreq.imr_multiaddr = psin->sin_addr;
1062 mreq.imr_address.s_addr = 0;
1063 mreq.imr_ifindex = greqs.gsr_interface;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001064 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001065 if (err && err != -EADDRINUSE)
1066 break;
1067 greqs.gsr_interface = mreq.imr_ifindex;
1068 omode = MCAST_INCLUDE;
1069 add = 1;
1070 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1071 omode = MCAST_INCLUDE;
1072 add = 0;
1073 }
1074 err = ip_mc_source(add, omode, sk, &mreqs,
1075 greqs.gsr_interface);
1076 break;
1077 }
1078 case MCAST_MSFILTER:
1079 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001080 struct sockaddr_in *psin;
1081 struct ip_msfilter *msf = NULL;
1082 struct group_filter *gsf = NULL;
1083 int msize, i, ifindex;
1084
1085 if (optlen < GROUP_FILTER_SIZE(0))
1086 goto e_inval;
1087 if (optlen > sysctl_optmem_max) {
1088 err = -ENOBUFS;
1089 break;
1090 }
Al Viroa2c841d2017-05-13 18:26:06 -04001091 gsf = memdup_user(optval, optlen);
1092 if (IS_ERR(gsf)) {
1093 err = PTR_ERR(gsf);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001094 break;
1095 }
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001096
Stephen Hemminger132adf52007-03-08 20:44:43 -08001097 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1098 if (gsf->gf_numsrc >= 0x1ffffff ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +02001099 gsf->gf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001100 err = -ENOBUFS;
1101 goto mc_msf_out;
1102 }
1103 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
1104 err = -EINVAL;
1105 goto mc_msf_out;
1106 }
1107 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -08001108 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001109 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001110 err = -ENOBUFS;
1111 goto mc_msf_out;
1112 }
1113 ifindex = gsf->gf_interface;
1114 psin = (struct sockaddr_in *)&gsf->gf_group;
1115 if (psin->sin_family != AF_INET) {
1116 err = -EADDRNOTAVAIL;
1117 goto mc_msf_out;
1118 }
1119 msf->imsf_multiaddr = psin->sin_addr.s_addr;
1120 msf->imsf_interface = 0;
1121 msf->imsf_fmode = gsf->gf_fmode;
1122 msf->imsf_numsrc = gsf->gf_numsrc;
1123 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001124 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001125 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
1126
1127 if (psin->sin_family != AF_INET)
1128 goto mc_msf_out;
1129 msf->imsf_slist[i] = psin->sin_addr.s_addr;
1130 }
1131 kfree(gsf);
1132 gsf = NULL;
1133
1134 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001135mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -08001136 kfree(msf);
1137 kfree(gsf);
1138 break;
1139 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001140 case IP_MULTICAST_ALL:
1141 if (optlen < 1)
1142 goto e_inval;
1143 if (val != 0 && val != 1)
1144 goto e_inval;
1145 inet->mc_all = val;
1146 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001147
1148 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001149 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001150 goto e_inval;
1151 inet->freebind = !!val;
1152 break;
1153
1154 case IP_IPSEC_POLICY:
1155 case IP_XFRM_POLICY:
1156 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001157 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001158 break;
1159 err = xfrm_user_policy(sk, optname, optval, optlen);
1160 break;
1161
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001162 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001163 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1164 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001165 err = -EPERM;
1166 break;
1167 }
1168 if (optlen < 1)
1169 goto e_inval;
1170 inet->transparent = !!val;
1171 break;
1172
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001173 case IP_MINTTL:
1174 if (optlen < 1)
1175 goto e_inval;
1176 if (val < 0 || val > 255)
1177 goto e_inval;
1178 inet->min_ttl = val;
1179 break;
1180
Stephen Hemminger132adf52007-03-08 20:44:43 -08001181 default:
1182 err = -ENOPROTOOPT;
1183 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001186 if (needs_rtnl)
1187 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return err;
1189
1190e_inval:
1191 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001192 if (needs_rtnl)
1193 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return -EINVAL;
1195}
1196
Eric Dumazetf84af322010-04-28 15:31:51 -07001197/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001198 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001199 * @sk: socket
1200 * @skb: buffer
1201 *
David S. Miller35ebf652012-06-28 03:59:11 -07001202 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1203 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001204 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001205 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001206void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001207{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001208 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001209 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1210 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001211
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001212 if (prepare && skb_rtable(skb)) {
David Ahern0b922b72016-05-10 11:19:51 -07001213 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1214 * which has interface index (iif) as the first member of the
1215 * underlying inet{6}_skb_parm struct. This code then overlays
1216 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001217 * element so the iif is picked up from the prior IPCB. If iif
1218 * is the loopback interface, then return the sending interface
1219 * (e.g., process binds socket to eth0 for Tx which is
1220 * redirected to loopback in the rtable/dst).
David Ahern0b922b72016-05-10 11:19:51 -07001221 */
David Aherncbea8f02017-09-13 17:11:37 -07001222 struct rtable *rt = skb_rtable(skb);
1223 bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags);
1224
1225 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001226 pktinfo->ipi_ifindex = inet_iif(skb);
David Aherncbea8f02017-09-13 17:11:37 -07001227 else if (l3slave && rt && rt->rt_iif)
1228 pktinfo->ipi_ifindex = rt->rt_iif;
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001229
David S. Miller35ebf652012-06-28 03:59:11 -07001230 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001231 } else {
1232 pktinfo->ipi_ifindex = 0;
1233 pktinfo->ipi_spec_dst.s_addr = 0;
1234 }
Paolo Abeni61a10302017-08-03 18:07:07 +02001235 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001236}
Eric Dumazetf84af322010-04-28 15:31:51 -07001237
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001238int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001239 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001240{
1241 int err;
1242
1243 if (level != SOL_IP)
1244 return -ENOPROTOOPT;
1245
1246 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1247#ifdef CONFIG_NETFILTER
1248 /* we need to exclude all possible ENOPROTOOPTs except default case */
1249 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001250 optname != IP_IPSEC_POLICY &&
1251 optname != IP_XFRM_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001252 !ip_mroute_opt(optname))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001253 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001254#endif
1255 return err;
1256}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001257EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001258
1259#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001260int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001261 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001262{
1263 int err;
1264
1265 if (level != SOL_IP)
1266 return -ENOPROTOOPT;
1267
David L Stevensdae50292008-04-27 01:06:07 -07001268 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1269 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1270 ip_setsockopt);
1271
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001272 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1273#ifdef CONFIG_NETFILTER
1274 /* we need to exclude all possible ENOPROTOOPTs except default case */
1275 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001276 optname != IP_IPSEC_POLICY &&
1277 optname != IP_XFRM_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001278 !ip_mroute_opt(optname))
1279 err = compat_nf_setsockopt(sk, PF_INET, optname, optval,
1280 optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001281#endif
1282 return err;
1283}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001284EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001285#endif
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001288 * Get the options. Note for future reference. The GET of IP options gets
1289 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 */
1291
WANG Cong87e9f032015-11-03 15:41:16 -08001292static bool getsockopt_needs_rtnl(int optname)
1293{
1294 switch (optname) {
1295 case IP_MSFILTER:
1296 case MCAST_MSFILTER:
1297 return true;
1298 }
1299 return false;
1300}
1301
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001302static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001303 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 struct inet_sock *inet = inet_sk(sk);
WANG Cong87e9f032015-11-03 15:41:16 -08001306 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1307 int val, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001309
Stephen Hemminger132adf52007-03-08 20:44:43 -08001310 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return -EOPNOTSUPP;
1312
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001313 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001314 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Jianjun Kong09cb1052008-11-03 00:27:11 -08001316 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001318 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001320
WANG Cong87e9f032015-11-03 15:41:16 -08001321 if (needs_rtnl)
1322 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 lock_sock(sk);
1324
Stephen Hemminger132adf52007-03-08 20:44:43 -08001325 switch (optname) {
1326 case IP_OPTIONS:
1327 {
1328 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001329 struct ip_options *opt = (struct ip_options *)optbuf;
1330 struct ip_options_rcu *inet_opt;
1331
1332 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001333 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001334 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001335 if (inet_opt)
1336 memcpy(optbuf, &inet_opt->opt,
1337 sizeof(struct ip_options) +
1338 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001339 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Stephen Hemminger132adf52007-03-08 20:44:43 -08001341 if (opt->optlen == 0)
1342 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Stephen Hemminger132adf52007-03-08 20:44:43 -08001344 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Stephen Hemminger132adf52007-03-08 20:44:43 -08001346 len = min_t(unsigned int, len, opt->optlen);
1347 if (put_user(len, optlen))
1348 return -EFAULT;
1349 if (copy_to_user(optval, opt->__data, len))
1350 return -EFAULT;
1351 return 0;
1352 }
1353 case IP_PKTINFO:
1354 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1355 break;
1356 case IP_RECVTTL:
1357 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1358 break;
1359 case IP_RECVTOS:
1360 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1361 break;
1362 case IP_RECVOPTS:
1363 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1364 break;
1365 case IP_RETOPTS:
1366 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1367 break;
1368 case IP_PASSSEC:
1369 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1370 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001371 case IP_RECVORIGDSTADDR:
1372 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1373 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001374 case IP_CHECKSUM:
1375 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1376 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -04001377 case IP_RECVFRAGSIZE:
1378 val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0;
1379 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001380 case IP_TOS:
1381 val = inet->tos;
1382 break;
1383 case IP_TTL:
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001384 {
1385 struct net *net = sock_net(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001386 val = (inet->uc_ttl == -1 ?
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001387 net->ipv4.sysctl_ip_default_ttl :
Stephen Hemminger132adf52007-03-08 20:44:43 -08001388 inet->uc_ttl);
1389 break;
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001390 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001391 case IP_HDRINCL:
1392 val = inet->hdrincl;
1393 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001394 case IP_NODEFRAG:
1395 val = inet->nodefrag;
1396 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001397 case IP_BIND_ADDRESS_NO_PORT:
1398 val = inet->bind_address_no_port;
1399 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001400 case IP_MTU_DISCOVER:
1401 val = inet->pmtudisc;
1402 break;
1403 case IP_MTU:
1404 {
1405 struct dst_entry *dst;
1406 val = 0;
1407 dst = sk_dst_get(sk);
1408 if (dst) {
1409 val = dst_mtu(dst);
1410 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001412 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001414 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001416 break;
1417 }
1418 case IP_RECVERR:
1419 val = inet->recverr;
1420 break;
1421 case IP_MULTICAST_TTL:
1422 val = inet->mc_ttl;
1423 break;
1424 case IP_MULTICAST_LOOP:
1425 val = inet->mc_loop;
1426 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001427 case IP_UNICAST_IF:
1428 val = (__force int)htonl((__u32) inet->uc_index);
1429 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001430 case IP_MULTICAST_IF:
1431 {
1432 struct in_addr addr;
1433 len = min_t(unsigned int, len, sizeof(struct in_addr));
1434 addr.s_addr = inet->mc_addr;
1435 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Stephen Hemminger132adf52007-03-08 20:44:43 -08001437 if (put_user(len, optlen))
1438 return -EFAULT;
1439 if (copy_to_user(optval, &addr, len))
1440 return -EFAULT;
1441 return 0;
1442 }
1443 case IP_MSFILTER:
1444 {
1445 struct ip_msfilter msf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001446
1447 if (len < IP_MSFILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001448 err = -EINVAL;
1449 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001451 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001452 err = -EFAULT;
1453 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001455 err = ip_mc_msfget(sk, &msf,
1456 (struct ip_msfilter __user *)optval, optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001457 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001458 }
1459 case MCAST_MSFILTER:
1460 {
1461 struct group_filter gsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Stephen Hemminger132adf52007-03-08 20:44:43 -08001463 if (len < GROUP_FILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001464 err = -EINVAL;
1465 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001467 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001468 err = -EFAULT;
1469 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001470 }
1471 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001472 (struct group_filter __user *)optval,
1473 optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001474 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001475 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001476 case IP_MULTICAST_ALL:
1477 val = inet->mc_all;
1478 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001479 case IP_PKTOPTIONS:
1480 {
1481 struct msghdr msg;
1482
1483 release_sock(sk);
1484
1485 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001487
Karoly Kemenyc54a5e02014-07-27 12:29:07 +02001488 msg.msg_control = (__force void *) optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001489 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001490 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001491
1492 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1493 struct in_pktinfo info;
1494
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001495 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1496 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001497 info.ipi_ifindex = inet->mc_index;
1498 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1499 }
1500 if (inet->cmsg_flags & IP_CMSG_TTL) {
1501 int hlim = inet->mc_ttl;
1502 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1503 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001504 if (inet->cmsg_flags & IP_CMSG_TOS) {
1505 int tos = inet->rcv_tos;
1506 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1507 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001508 len -= msg.msg_controllen;
1509 return put_user(len, optlen);
1510 }
1511 case IP_FREEBIND:
1512 val = inet->freebind;
1513 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001514 case IP_TRANSPARENT:
1515 val = inet->transparent;
1516 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001517 case IP_MINTTL:
1518 val = inet->min_ttl;
1519 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001520 default:
1521 release_sock(sk);
1522 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001525
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001526 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 unsigned char ucval = (unsigned char)val;
1528 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001529 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001531 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return -EFAULT;
1533 } else {
1534 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001535 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001537 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return -EFAULT;
1539 }
1540 return 0;
WANG Cong87e9f032015-11-03 15:41:16 -08001541
1542out:
1543 release_sock(sk);
1544 if (needs_rtnl)
1545 rtnl_unlock();
1546 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001549int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001550 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001551{
1552 int err;
1553
Daniel Balutadd23198e2011-08-07 22:31:07 -07001554 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001555#ifdef CONFIG_NETFILTER
1556 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001557 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1558 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001559 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001560
Jianjun Kong09cb1052008-11-03 00:27:11 -08001561 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001562 return -EFAULT;
1563
Paolo Abeni01ea3062018-02-08 12:19:00 +01001564 err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001565 if (err >= 0)
1566 err = put_user(len, optlen);
1567 return err;
1568 }
1569#endif
1570 return err;
1571}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001572EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001573
1574#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001575int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1576 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001577{
David L Stevens42908c62008-04-29 03:23:22 -07001578 int err;
1579
1580 if (optname == MCAST_MSFILTER)
1581 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1582 ip_getsockopt);
1583
Daniel Balutadd23198e2011-08-07 22:31:07 -07001584 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1585 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001586
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001587#ifdef CONFIG_NETFILTER
1588 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001589 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1590 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001591 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001592
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001593 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001594 return -EFAULT;
1595
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001596 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001597 if (err >= 0)
1598 err = put_user(len, optlen);
1599 return err;
1600 }
1601#endif
1602 return err;
1603}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001604EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001605#endif