blob: 26c36cccabdc2c8cc95cfd609672d412c493fc42 [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{
151 struct sockaddr_in sin;
Willem de Bruijn2efd4fc2018-07-23 19:36:48 -0400152 __be16 *ports;
153 int end;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800154
Willem de Bruijn2efd4fc2018-07-23 19:36:48 -0400155 end = skb_transport_offset(skb) + 4;
156 if (end > 0 && !pskb_may_pull(skb, end))
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800157 return;
158
159 /* All current transport protocols have the port numbers in the
160 * first four bytes of the transport header and this function is
161 * written with this assumption in mind.
162 */
Willem de Bruijn2efd4fc2018-07-23 19:36:48 -0400163 ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800164
165 sin.sin_family = AF_INET;
Eric Dumazet64199fc2018-09-30 11:33:39 -0700166 sin.sin_addr.s_addr = ip_hdr(skb)->daddr;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800167 sin.sin_port = ports[1];
168 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
169
170 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
171}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Paolo Abeniad959032016-11-04 11:28:58 +0100173void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk,
174 struct sk_buff *skb, int tlen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Paolo Abeniad959032016-11-04 11:28:58 +0100176 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000177 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 /* Ordered by supposed usage frequency */
Tom Herbertc44d13d2015-01-05 13:56:15 -0800180 if (flags & IP_CMSG_PKTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 ip_cmsg_recv_pktinfo(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Tom Herbertc44d13d2015-01-05 13:56:15 -0800183 flags &= ~IP_CMSG_PKTINFO;
184 if (!flags)
185 return;
186 }
187
188 if (flags & IP_CMSG_TTL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 ip_cmsg_recv_ttl(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Tom Herbertc44d13d2015-01-05 13:56:15 -0800191 flags &= ~IP_CMSG_TTL;
192 if (!flags)
193 return;
194 }
195
196 if (flags & IP_CMSG_TOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 ip_cmsg_recv_tos(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Tom Herbertc44d13d2015-01-05 13:56:15 -0800199 flags &= ~IP_CMSG_TOS;
200 if (!flags)
201 return;
202 }
203
204 if (flags & IP_CMSG_RECVOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 ip_cmsg_recv_opts(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Tom Herbertc44d13d2015-01-05 13:56:15 -0800207 flags &= ~IP_CMSG_RECVOPTS;
208 if (!flags)
209 return;
210 }
211
212 if (flags & IP_CMSG_RETOPTS) {
Paolo Abeni91ed1e62017-08-03 18:07:06 +0200213 ip_cmsg_recv_retopts(sock_net(sk), msg, skb);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800214
Tom Herbertc44d13d2015-01-05 13:56:15 -0800215 flags &= ~IP_CMSG_RETOPTS;
216 if (!flags)
217 return;
218 }
219
220 if (flags & IP_CMSG_PASSSEC) {
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800221 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800222
Tom Herbertc44d13d2015-01-05 13:56:15 -0800223 flags &= ~IP_CMSG_PASSSEC;
224 if (!flags)
225 return;
226 }
227
Tom Herbertad6f9392015-01-05 13:56:17 -0800228 if (flags & IP_CMSG_ORIGDSTADDR) {
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800229 ip_cmsg_recv_dstaddr(msg, skb);
230
Tom Herbertad6f9392015-01-05 13:56:17 -0800231 flags &= ~IP_CMSG_ORIGDSTADDR;
232 if (!flags)
233 return;
234 }
235
236 if (flags & IP_CMSG_CHECKSUM)
Eric Dumazet10df8e62016-10-23 18:03:06 -0700237 ip_cmsg_recv_checksum(msg, skb, tlen, offset);
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400238
239 if (flags & IP_CMSG_RECVFRAGSIZE)
240 ip_cmsg_recv_fragsize(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
Tom Herbert5961de92015-01-05 13:56:16 -0800242EXPORT_SYMBOL(ip_cmsg_recv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400244int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100245 bool allow_ipv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Francesco Fuscof02db312013-09-24 15:43:08 +0200247 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct cmsghdr *cmsg;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400249 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Gu Zhengf95b4142014-12-11 11:22:04 +0800251 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (!CMSG_OK(msg, cmsg))
253 return -EINVAL;
Eric Dumazet5337b5b2014-11-10 17:54:25 -0800254#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100255 if (allow_ipv6 &&
256 cmsg->cmsg_level == SOL_IPV6 &&
257 cmsg->cmsg_type == IPV6_PKTINFO) {
258 struct in6_pktinfo *src_info;
259
260 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
261 return -EINVAL;
262 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
263 if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
264 return -EINVAL;
David Ahern1cbec072018-02-16 11:03:03 -0800265 if (src_info->ipi6_ifindex)
266 ipc->oif = src_info->ipi6_ifindex;
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100267 ipc->addr = src_info->ipi6_addr.s6_addr32[3];
268 continue;
269 }
270#endif
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400271 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700272 err = __sock_cmsg_send(sk, msg, cmsg, &ipc->sockc);
273 if (err)
274 return err;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400275 continue;
276 }
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (cmsg->cmsg_level != SOL_IP)
279 continue;
280 switch (cmsg->cmsg_type) {
281 case IP_RETOPTS:
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800282 err = cmsg->cmsg_len - sizeof(struct cmsghdr);
Eric Dumazet91948302016-02-04 06:23:28 -0800283
284 /* Our caller is responsible for freeing ipc->opt */
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700285 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
286 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (err)
288 return err;
289 break;
290 case IP_PKTINFO:
291 {
292 struct in_pktinfo *info;
293 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
294 return -EINVAL;
295 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
David Ahern1cbec072018-02-16 11:03:03 -0800296 if (info->ipi_ifindex)
297 ipc->oif = info->ipi_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 ipc->addr = info->ipi_spec_dst.s_addr;
299 break;
300 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200301 case IP_TTL:
302 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
303 return -EINVAL;
304 val = *(int *)CMSG_DATA(cmsg);
305 if (val < 1 || val > 255)
306 return -EINVAL;
307 ipc->ttl = val;
308 break;
309 case IP_TOS:
Eric Dumazete895cdc2016-09-07 21:52:56 -0700310 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)))
311 val = *(int *)CMSG_DATA(cmsg);
312 else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8)))
313 val = *(u8 *)CMSG_DATA(cmsg);
314 else
Francesco Fuscof02db312013-09-24 15:43:08 +0200315 return -EINVAL;
Francesco Fuscof02db312013-09-24 15:43:08 +0200316 if (val < 0 || val > 255)
317 return -EINVAL;
318 ipc->tos = val;
319 ipc->priority = rt_tos2priority(ipc->tos);
320 break;
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 default:
323 return -EINVAL;
324 }
325 }
326 return 0;
327}
328
Eric Dumazet592fcb92010-06-09 16:21:07 +0000329static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000330{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000331 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
332
333 sock_put(ra->saved_sk);
334 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000335}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700337int ip_ra_control(struct sock *sk, unsigned char on,
338 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000340 struct ip_ra_chain *ra, *new_ra;
341 struct ip_ra_chain __rcu **rap;
Kirill Tkhai5796ef72018-03-22 12:45:32 +0300342 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000344 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return -EINVAL;
346
347 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
348
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300349 mutex_lock(&net->ipv4.ra_mutex);
Kirill Tkhai5796ef72018-03-22 12:45:32 +0300350 for (rap = &net->ipv4.ra_chain;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300351 (ra = rcu_dereference_protected(*rap,
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300352 lockdep_is_held(&net->ipv4.ra_mutex))) != NULL;
Eric Dumazet43a951e2010-10-25 03:32:44 +0000353 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (ra->sk == sk) {
355 if (on) {
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300356 mutex_unlock(&net->ipv4.ra_mutex);
Jesper Juhla51482b2005-11-08 09:41:34 -0800357 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return -EADDRINUSE;
359 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000360 /* dont let ip_call_ra_chain() use sk again */
361 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700362 RCU_INIT_POINTER(*rap, ra->next);
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300363 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 if (ra->destructor)
366 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000367 /*
368 * Delay sock_put(sk) and kfree(ra) after one rcu grace
369 * period. This guarantee ip_call_ra_chain() dont need
370 * to mess with socket refcounts.
371 */
372 ra->saved_sk = sk;
373 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375 }
376 }
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300377 if (!new_ra) {
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300378 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return -ENOBUFS;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 new_ra->sk = sk;
382 new_ra->destructor = destructor;
383
Eric Dumazet8e380f02014-09-09 08:11:41 -0700384 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000385 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 sock_hold(sk);
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300387 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 return 0;
390}
391
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900392void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700393 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct sock_exterr_skb *serr;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 skb = skb_clone(skb, GFP_ATOMIC);
398 if (!skb)
399 return;
400
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900401 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 serr->ee.ee_errno = err;
403 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300404 serr->ee.ee_type = icmp_hdr(skb)->type;
405 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 serr->ee.ee_pad = 0;
407 serr->ee.ee_info = info;
408 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300409 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700410 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 serr->port = port;
412
Ian Morris00db4122015-04-03 09:17:27 +0100413 if (skb_pull(skb, payload - skb->data)) {
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300414 skb_reset_transport_header(skb);
415 if (sock_queue_err_skb(sk, skb) == 0)
416 return;
417 }
418 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Al Viro05790162006-09-27 18:33:40 -0700421void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct inet_sock *inet = inet_sk(sk);
424 struct sock_exterr_skb *serr;
425 struct iphdr *iph;
426 struct sk_buff *skb;
427
428 if (!inet->recverr)
429 return;
430
431 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
432 if (!skb)
433 return;
434
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300435 skb_put(skb, sizeof(struct iphdr));
436 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700437 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 iph->daddr = daddr;
439
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900440 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 serr->ee.ee_errno = err;
442 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900443 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 serr->ee.ee_code = 0;
445 serr->ee.ee_pad = 0;
446 serr->ee.ee_info = info;
447 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700448 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 serr->port = port;
450
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700451 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300452 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 if (sock_queue_err_skb(sk, skb))
455 kfree_skb(skb);
456}
457
Julian Anastasov34b99df2015-06-23 08:34:39 +0300458/* For some errors we have valid addr_offset even with zero payload and
459 * zero port. Also, addr_offset should be supported if port is set.
460 */
461static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
462{
463 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
464 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
465}
466
Willem de Bruijnc247f052015-03-07 20:33:22 -0500467/* IPv4 supports cmsg on all imcp errors and some timestamps
468 *
469 * Timestamp code paths do not initialize the fields expected by cmsg:
470 * the PKTINFO fields in skb->cb[]. Fill those in here.
471 */
472static bool ipv4_datagram_support_cmsg(const struct sock *sk,
473 struct sk_buff *skb,
474 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500475{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500476 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500477
Willem de Bruijnc247f052015-03-07 20:33:22 -0500478 if (ee_origin == SO_EE_ORIGIN_ICMP)
479 return true;
480
481 if (ee_origin == SO_EE_ORIGIN_LOCAL)
482 return false;
483
484 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
Willem de Bruijn1862d622017-04-12 19:24:35 -0400485 * timestamp with egress dev. Not possible for packets without iif
Willem de Bruijnc247f052015-03-07 20:33:22 -0500486 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
487 */
Willem de Bruijn1862d622017-04-12 19:24:35 -0400488 info = PKTINFO_SKB_CB(skb);
489 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
490 !info->ipi_ifindex)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500491 return false;
492
493 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500494 return true;
495}
496
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900497/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * Handle MSG_ERRQUEUE
499 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100500int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400503 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100504 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct {
506 struct sock_extended_err ee;
507 struct sockaddr_in offender;
508 } errhdr;
509 int err;
510 int copied;
511
512 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400513 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100514 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 goto out;
516
517 copied = skb->len;
518 if (copied > len) {
519 msg->msg_flags |= MSG_TRUNC;
520 copied = len;
521 }
David S. Miller51f3d022014-11-05 16:46:40 -0500522 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700523 if (unlikely(err)) {
524 kfree_skb(skb);
525 return err;
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 sock_recv_timestamp(msg, sk, skb);
528
529 serr = SKB_EXT_ERR(skb);
530
Julian Anastasov34b99df2015-06-23 08:34:39 +0300531 if (sin && ipv4_datagram_support_addr(serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700533 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
534 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 sin->sin_port = serr->port;
536 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100537 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
540 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
541 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500542 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500543
Willem de Bruijnc247f052015-03-07 20:33:22 -0500544 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700546 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500547 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 ip_cmsg_recv(msg, skb);
549 }
550
551 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
552
553 /* Now we could try to dump offended packet options */
554
555 msg->msg_flags |= MSG_ERRQUEUE;
556 err = copied;
557
Eric Dumazet960a2622016-04-21 22:27:32 -0700558 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559out:
560 return err;
561}
562
563
564/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700565 * Socket option code for IP. This is the end of the line after any
566 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300568static bool setsockopt_needs_rtnl(int optname)
569{
570 switch (optname) {
571 case IP_ADD_MEMBERSHIP:
572 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300573 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300574 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300575 case IP_DROP_SOURCE_MEMBERSHIP:
576 case IP_MSFILTER:
577 case IP_UNBLOCK_SOURCE:
578 case MCAST_BLOCK_SOURCE:
579 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300580 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300581 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300582 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300583 case MCAST_LEAVE_SOURCE_GROUP:
584 case MCAST_UNBLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300585 return true;
586 }
587 return false;
588}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800590static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700591 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct inet_sock *inet = inet_sk(sk);
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200594 struct net *net = sock_net(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800595 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300596 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Xi Wang0c9f79b2012-11-11 11:20:01 +0000598 switch (optname) {
599 case IP_PKTINFO:
600 case IP_RECVTTL:
601 case IP_RECVOPTS:
602 case IP_RECVTOS:
603 case IP_RETOPTS:
604 case IP_TOS:
605 case IP_TTL:
606 case IP_HDRINCL:
607 case IP_MTU_DISCOVER:
608 case IP_RECVERR:
609 case IP_ROUTER_ALERT:
610 case IP_FREEBIND:
611 case IP_PASSSEC:
612 case IP_TRANSPARENT:
613 case IP_MINTTL:
614 case IP_NODEFRAG:
Eric Dumazet90c337d2015-06-06 21:17:57 -0700615 case IP_BIND_ADDRESS_NO_PORT:
Xi Wang0c9f79b2012-11-11 11:20:01 +0000616 case IP_UNICAST_IF:
617 case IP_MULTICAST_TTL:
618 case IP_MULTICAST_ALL:
619 case IP_MULTICAST_LOOP:
620 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800621 case IP_CHECKSUM:
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400622 case IP_RECVFRAGSIZE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (optlen >= sizeof(int)) {
624 if (get_user(val, (int __user *) optval))
625 return -EFAULT;
626 } else if (optlen >= sizeof(char)) {
627 unsigned char ucval;
628
629 if (get_user(ucval, (unsigned char __user *) optval))
630 return -EFAULT;
631 val = (int) ucval;
632 }
633 }
634
635 /* If optlen==0, it is equivalent to val == 0 */
636
Kirill Tkhai05269472018-03-22 12:45:12 +0300637 if (optname == IP_ROUTER_ALERT)
638 return ip_ra_control(sk, val ? 1 : 0, NULL);
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800639 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800640 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300643 if (needs_rtnl)
644 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 lock_sock(sk);
646
647 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800648 case IP_OPTIONS:
649 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000650 struct ip_options_rcu *old, *opt = NULL;
651
roel kluin65a1c4f2009-10-23 05:59:21 +0000652 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800653 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900654 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700655 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800656 if (err)
657 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000658 old = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200659 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800660 if (inet->is_icsk) {
661 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000662#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800663 if (sk->sk_family == PF_INET ||
664 (!((1 << sk->sk_state) &
665 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000666 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000668 if (old)
669 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800670 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000671 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800672 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000673#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000677 rcu_assign_pointer(inet->inet_opt, opt);
678 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800679 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800680 break;
681 }
682 case IP_PKTINFO:
683 if (val)
684 inet->cmsg_flags |= IP_CMSG_PKTINFO;
685 else
686 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
687 break;
688 case IP_RECVTTL:
689 if (val)
690 inet->cmsg_flags |= IP_CMSG_TTL;
691 else
692 inet->cmsg_flags &= ~IP_CMSG_TTL;
693 break;
694 case IP_RECVTOS:
695 if (val)
696 inet->cmsg_flags |= IP_CMSG_TOS;
697 else
698 inet->cmsg_flags &= ~IP_CMSG_TOS;
699 break;
700 case IP_RECVOPTS:
701 if (val)
702 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
703 else
704 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
705 break;
706 case IP_RETOPTS:
707 if (val)
708 inet->cmsg_flags |= IP_CMSG_RETOPTS;
709 else
710 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
711 break;
712 case IP_PASSSEC:
713 if (val)
714 inet->cmsg_flags |= IP_CMSG_PASSSEC;
715 else
716 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
717 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800718 case IP_RECVORIGDSTADDR:
719 if (val)
720 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
721 else
722 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
723 break;
Tom Herbertad6f9392015-01-05 13:56:17 -0800724 case IP_CHECKSUM:
725 if (val) {
726 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
727 inet_inc_convert_csum(sk);
728 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
729 }
730 } else {
731 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
732 inet_dec_convert_csum(sk);
733 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
734 }
735 }
736 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400737 case IP_RECVFRAGSIZE:
738 if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM)
739 goto e_inval;
740 if (val)
741 inet->cmsg_flags |= IP_CMSG_RECVFRAGSIZE;
742 else
743 inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE;
744 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800745 case IP_TOS: /* This sets both TOS and Precedence */
746 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400747 val &= ~INET_ECN_MASK;
748 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800750 if (inet->tos != val) {
751 inet->tos = val;
752 sk->sk_priority = rt_tos2priority(val);
753 sk_dst_reset(sk);
754 }
755 break;
756 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700757 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800758 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000759 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800760 goto e_inval;
761 inet->uc_ttl = val;
762 break;
763 case IP_HDRINCL:
764 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800767 }
768 inet->hdrincl = val ? 1 : 0;
769 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000770 case IP_NODEFRAG:
771 if (sk->sk_type != SOCK_RAW) {
772 err = -ENOPROTOOPT;
773 break;
774 }
775 inet->nodefrag = val ? 1 : 0;
776 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -0700777 case IP_BIND_ADDRESS_NO_PORT:
778 inet->bind_address_no_port = val ? 1 : 0;
779 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800780 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100781 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800782 goto e_inval;
783 inet->pmtudisc = val;
784 break;
785 case IP_RECVERR:
786 inet->recverr = !!val;
787 if (!val)
788 skb_queue_purge(&sk->sk_error_queue);
789 break;
790 case IP_MULTICAST_TTL:
791 if (sk->sk_type == SOCK_STREAM)
792 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700793 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800794 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800795 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800796 val = 1;
797 if (val < 0 || val > 255)
798 goto e_inval;
799 inet->mc_ttl = val;
800 break;
801 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700802 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800803 goto e_inval;
804 inet->mc_loop = !!val;
805 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000806 case IP_UNICAST_IF:
807 {
808 struct net_device *dev = NULL;
809 int ifindex;
David Ahern9515a2e2018-01-24 19:37:38 -0800810 int midx;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000811
812 if (optlen != sizeof(int))
813 goto e_inval;
814
815 ifindex = (__force int)ntohl((__force __be32)val);
816 if (ifindex == 0) {
817 inet->uc_index = 0;
818 err = 0;
819 break;
820 }
821
822 dev = dev_get_by_index(sock_net(sk), ifindex);
823 err = -EADDRNOTAVAIL;
824 if (!dev)
825 break;
David Ahern9515a2e2018-01-24 19:37:38 -0800826
827 midx = l3mdev_master_ifindex(dev);
Erich E. Hoover76e21052012-02-08 09:11:07 +0000828 dev_put(dev);
829
830 err = -EINVAL;
David Ahern9515a2e2018-01-24 19:37:38 -0800831 if (sk->sk_bound_dev_if &&
832 (!midx || midx != sk->sk_bound_dev_if))
Erich E. Hoover76e21052012-02-08 09:11:07 +0000833 break;
834
835 inet->uc_index = ifindex;
836 err = 0;
837 break;
838 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800839 case IP_MULTICAST_IF:
840 {
841 struct ip_mreqn mreq;
842 struct net_device *dev = NULL;
David Ahern7bb387c2016-12-29 15:39:37 -0800843 int midx;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800844
845 if (sk->sk_type == SOCK_STREAM)
846 goto e_inval;
847 /*
848 * Check the arguments are allowable
849 */
850
Shan Wei09159212009-09-22 15:41:10 +0000851 if (optlen < sizeof(struct in_addr))
852 goto e_inval;
853
Stephen Hemminger132adf52007-03-08 20:44:43 -0800854 err = -EFAULT;
855 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800856 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800857 break;
858 } else {
859 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000860 if (optlen >= sizeof(struct ip_mreq)) {
861 if (copy_from_user(&mreq, optval,
862 sizeof(struct ip_mreq)))
863 break;
864 } else if (optlen >= sizeof(struct in_addr)) {
865 if (copy_from_user(&mreq.imr_address, optval,
866 sizeof(struct in_addr)))
867 break;
868 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800869 }
870
871 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700872 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800873 inet->mc_index = 0;
874 inet->mc_addr = 0;
875 err = 0;
876 break;
877 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900878 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000879 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800880 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800881 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000882 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800883
884
885 err = -EADDRNOTAVAIL;
886 if (!dev)
887 break;
David Ahern7bb387c2016-12-29 15:39:37 -0800888
889 midx = l3mdev_master_ifindex(dev);
890
Eric Dumazet55b80502009-10-19 06:41:58 +0000891 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800892
893 err = -EINVAL;
894 if (sk->sk_bound_dev_if &&
David Ahern7bb387c2016-12-29 15:39:37 -0800895 mreq.imr_ifindex != sk->sk_bound_dev_if &&
896 (!midx || midx != sk->sk_bound_dev_if))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800897 break;
898
899 inet->mc_index = mreq.imr_ifindex;
900 inet->mc_addr = mreq.imr_address.s_addr;
901 err = 0;
902 break;
903 }
904
905 case IP_ADD_MEMBERSHIP:
906 case IP_DROP_MEMBERSHIP:
907 {
908 struct ip_mreqn mreq;
909
Flavio Leitnera96fb492007-08-24 22:16:39 -0700910 err = -EPROTO;
911 if (inet_sk(sk)->is_icsk)
912 break;
913
Stephen Hemminger132adf52007-03-08 20:44:43 -0800914 if (optlen < sizeof(struct ip_mreq))
915 goto e_inval;
916 err = -EFAULT;
917 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800918 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800919 break;
920 } else {
921 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800922 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800923 break;
924 }
925
926 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300927 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800928 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300929 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800930 break;
931 }
932 case IP_MSFILTER:
933 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800934 struct ip_msfilter *msf;
935
936 if (optlen < IP_MSFILTER_SIZE(0))
937 goto e_inval;
938 if (optlen > sysctl_optmem_max) {
939 err = -ENOBUFS;
940 break;
941 }
Al Viroa2c841d2017-05-13 18:26:06 -0400942 msf = memdup_user(optval, optlen);
943 if (IS_ERR(msf)) {
944 err = PTR_ERR(msf);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800945 break;
946 }
947 /* numsrc >= (1G-4) overflow in 32 bits */
948 if (msf->imsf_numsrc >= 0x3ffffffcU ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200949 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800950 kfree(msf);
951 err = -ENOBUFS;
952 break;
953 }
954 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
955 kfree(msf);
956 err = -EINVAL;
957 break;
958 }
959 err = ip_mc_msfilter(sk, msf, 0);
960 kfree(msf);
961 break;
962 }
963 case IP_BLOCK_SOURCE:
964 case IP_UNBLOCK_SOURCE:
965 case IP_ADD_SOURCE_MEMBERSHIP:
966 case IP_DROP_SOURCE_MEMBERSHIP:
967 {
968 struct ip_mreq_source mreqs;
969 int omode, add;
970
971 if (optlen != sizeof(struct ip_mreq_source))
972 goto e_inval;
973 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
974 err = -EFAULT;
975 break;
976 }
977 if (optname == IP_BLOCK_SOURCE) {
978 omode = MCAST_EXCLUDE;
979 add = 1;
980 } else if (optname == IP_UNBLOCK_SOURCE) {
981 omode = MCAST_EXCLUDE;
982 add = 0;
983 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
984 struct ip_mreqn mreq;
985
986 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
987 mreq.imr_address.s_addr = mreqs.imr_interface;
988 mreq.imr_ifindex = 0;
Hangbin Liu6e2059b2018-07-10 22:41:26 +0800989 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800990 if (err && err != -EADDRINUSE)
991 break;
992 omode = MCAST_INCLUDE;
993 add = 1;
994 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
995 omode = MCAST_INCLUDE;
996 add = 0;
997 }
998 err = ip_mc_source(add, omode, sk, &mreqs, 0);
999 break;
1000 }
1001 case MCAST_JOIN_GROUP:
1002 case MCAST_LEAVE_GROUP:
1003 {
1004 struct group_req greq;
1005 struct sockaddr_in *psin;
1006 struct ip_mreqn mreq;
1007
1008 if (optlen < sizeof(struct group_req))
1009 goto e_inval;
1010 err = -EFAULT;
1011 if (copy_from_user(&greq, optval, sizeof(greq)))
1012 break;
1013 psin = (struct sockaddr_in *)&greq.gr_group;
1014 if (psin->sin_family != AF_INET)
1015 goto e_inval;
1016 memset(&mreq, 0, sizeof(mreq));
1017 mreq.imr_multiaddr = psin->sin_addr;
1018 mreq.imr_ifindex = greq.gr_interface;
1019
1020 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001021 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001022 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001023 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001024 break;
1025 }
1026 case MCAST_JOIN_SOURCE_GROUP:
1027 case MCAST_LEAVE_SOURCE_GROUP:
1028 case MCAST_BLOCK_SOURCE:
1029 case MCAST_UNBLOCK_SOURCE:
1030 {
1031 struct group_source_req greqs;
1032 struct ip_mreq_source mreqs;
1033 struct sockaddr_in *psin;
1034 int omode, add;
1035
1036 if (optlen != sizeof(struct group_source_req))
1037 goto e_inval;
1038 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
1039 err = -EFAULT;
1040 break;
1041 }
1042 if (greqs.gsr_group.ss_family != AF_INET ||
1043 greqs.gsr_source.ss_family != AF_INET) {
1044 err = -EADDRNOTAVAIL;
1045 break;
1046 }
1047 psin = (struct sockaddr_in *)&greqs.gsr_group;
1048 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
1049 psin = (struct sockaddr_in *)&greqs.gsr_source;
1050 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
1051 mreqs.imr_interface = 0; /* use index for mc_source */
1052
1053 if (optname == MCAST_BLOCK_SOURCE) {
1054 omode = MCAST_EXCLUDE;
1055 add = 1;
1056 } else if (optname == MCAST_UNBLOCK_SOURCE) {
1057 omode = MCAST_EXCLUDE;
1058 add = 0;
1059 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
1060 struct ip_mreqn mreq;
1061
1062 psin = (struct sockaddr_in *)&greqs.gsr_group;
1063 mreq.imr_multiaddr = psin->sin_addr;
1064 mreq.imr_address.s_addr = 0;
1065 mreq.imr_ifindex = greqs.gsr_interface;
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001066 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001067 if (err && err != -EADDRINUSE)
1068 break;
1069 greqs.gsr_interface = mreq.imr_ifindex;
1070 omode = MCAST_INCLUDE;
1071 add = 1;
1072 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1073 omode = MCAST_INCLUDE;
1074 add = 0;
1075 }
1076 err = ip_mc_source(add, omode, sk, &mreqs,
1077 greqs.gsr_interface);
1078 break;
1079 }
1080 case MCAST_MSFILTER:
1081 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001082 struct sockaddr_in *psin;
1083 struct ip_msfilter *msf = NULL;
1084 struct group_filter *gsf = NULL;
1085 int msize, i, ifindex;
1086
1087 if (optlen < GROUP_FILTER_SIZE(0))
1088 goto e_inval;
1089 if (optlen > sysctl_optmem_max) {
1090 err = -ENOBUFS;
1091 break;
1092 }
Al Viroa2c841d2017-05-13 18:26:06 -04001093 gsf = memdup_user(optval, optlen);
1094 if (IS_ERR(gsf)) {
1095 err = PTR_ERR(gsf);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001096 break;
1097 }
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001098
Stephen Hemminger132adf52007-03-08 20:44:43 -08001099 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1100 if (gsf->gf_numsrc >= 0x1ffffff ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +02001101 gsf->gf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001102 err = -ENOBUFS;
1103 goto mc_msf_out;
1104 }
1105 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
1106 err = -EINVAL;
1107 goto mc_msf_out;
1108 }
1109 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -08001110 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001111 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001112 err = -ENOBUFS;
1113 goto mc_msf_out;
1114 }
1115 ifindex = gsf->gf_interface;
1116 psin = (struct sockaddr_in *)&gsf->gf_group;
1117 if (psin->sin_family != AF_INET) {
1118 err = -EADDRNOTAVAIL;
1119 goto mc_msf_out;
1120 }
1121 msf->imsf_multiaddr = psin->sin_addr.s_addr;
1122 msf->imsf_interface = 0;
1123 msf->imsf_fmode = gsf->gf_fmode;
1124 msf->imsf_numsrc = gsf->gf_numsrc;
1125 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001126 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001127 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
1128
1129 if (psin->sin_family != AF_INET)
1130 goto mc_msf_out;
1131 msf->imsf_slist[i] = psin->sin_addr.s_addr;
1132 }
1133 kfree(gsf);
1134 gsf = NULL;
1135
1136 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001137mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -08001138 kfree(msf);
1139 kfree(gsf);
1140 break;
1141 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001142 case IP_MULTICAST_ALL:
1143 if (optlen < 1)
1144 goto e_inval;
1145 if (val != 0 && val != 1)
1146 goto e_inval;
1147 inet->mc_all = val;
1148 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001149
1150 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001151 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001152 goto e_inval;
1153 inet->freebind = !!val;
1154 break;
1155
1156 case IP_IPSEC_POLICY:
1157 case IP_XFRM_POLICY:
1158 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001159 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001160 break;
1161 err = xfrm_user_policy(sk, optname, optval, optlen);
1162 break;
1163
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001164 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001165 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1166 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001167 err = -EPERM;
1168 break;
1169 }
1170 if (optlen < 1)
1171 goto e_inval;
1172 inet->transparent = !!val;
1173 break;
1174
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001175 case IP_MINTTL:
1176 if (optlen < 1)
1177 goto e_inval;
1178 if (val < 0 || val > 255)
1179 goto e_inval;
1180 inet->min_ttl = val;
1181 break;
1182
Stephen Hemminger132adf52007-03-08 20:44:43 -08001183 default:
1184 err = -ENOPROTOOPT;
1185 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001188 if (needs_rtnl)
1189 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return err;
1191
1192e_inval:
1193 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001194 if (needs_rtnl)
1195 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return -EINVAL;
1197}
1198
Eric Dumazetf84af322010-04-28 15:31:51 -07001199/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001200 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001201 * @sk: socket
1202 * @skb: buffer
1203 *
David S. Miller35ebf652012-06-28 03:59:11 -07001204 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1205 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001206 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001207 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001208void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001209{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001210 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001211 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1212 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001213
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001214 if (prepare && skb_rtable(skb)) {
David Ahern0b922b72016-05-10 11:19:51 -07001215 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1216 * which has interface index (iif) as the first member of the
1217 * underlying inet{6}_skb_parm struct. This code then overlays
1218 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001219 * element so the iif is picked up from the prior IPCB. If iif
1220 * is the loopback interface, then return the sending interface
1221 * (e.g., process binds socket to eth0 for Tx which is
1222 * redirected to loopback in the rtable/dst).
David Ahern0b922b72016-05-10 11:19:51 -07001223 */
David Aherncbea8f02017-09-13 17:11:37 -07001224 struct rtable *rt = skb_rtable(skb);
1225 bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags);
1226
1227 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001228 pktinfo->ipi_ifindex = inet_iif(skb);
David Aherncbea8f02017-09-13 17:11:37 -07001229 else if (l3slave && rt && rt->rt_iif)
1230 pktinfo->ipi_ifindex = rt->rt_iif;
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001231
David S. Miller35ebf652012-06-28 03:59:11 -07001232 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001233 } else {
1234 pktinfo->ipi_ifindex = 0;
1235 pktinfo->ipi_spec_dst.s_addr = 0;
1236 }
Paolo Abeni61a10302017-08-03 18:07:07 +02001237 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001238}
Eric Dumazetf84af322010-04-28 15:31:51 -07001239
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001240int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001241 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001242{
1243 int err;
1244
1245 if (level != SOL_IP)
1246 return -ENOPROTOOPT;
1247
1248 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001249#ifdef CONFIG_BPFILTER
1250 if (optname >= BPFILTER_IPT_SO_SET_REPLACE &&
1251 optname < BPFILTER_IPT_SET_MAX)
1252 err = bpfilter_ip_set_sockopt(sk, optname, optval, optlen);
1253#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001254#ifdef CONFIG_NETFILTER
1255 /* we need to exclude all possible ENOPROTOOPTs except default case */
1256 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001257 optname != IP_IPSEC_POLICY &&
1258 optname != IP_XFRM_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001259 !ip_mroute_opt(optname))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001260 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001261#endif
1262 return err;
1263}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001264EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001265
1266#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001267int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001268 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001269{
1270 int err;
1271
1272 if (level != SOL_IP)
1273 return -ENOPROTOOPT;
1274
David L Stevensdae50292008-04-27 01:06:07 -07001275 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1276 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1277 ip_setsockopt);
1278
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001279 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1280#ifdef CONFIG_NETFILTER
1281 /* we need to exclude all possible ENOPROTOOPTs except default case */
1282 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001283 optname != IP_IPSEC_POLICY &&
1284 optname != IP_XFRM_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001285 !ip_mroute_opt(optname))
1286 err = compat_nf_setsockopt(sk, PF_INET, optname, optval,
1287 optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001288#endif
1289 return err;
1290}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001291EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001292#endif
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001295 * Get the options. Note for future reference. The GET of IP options gets
1296 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
1298
WANG Cong87e9f032015-11-03 15:41:16 -08001299static bool getsockopt_needs_rtnl(int optname)
1300{
1301 switch (optname) {
1302 case IP_MSFILTER:
1303 case MCAST_MSFILTER:
1304 return true;
1305 }
1306 return false;
1307}
1308
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001309static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001310 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 struct inet_sock *inet = inet_sk(sk);
WANG Cong87e9f032015-11-03 15:41:16 -08001313 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1314 int val, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001316
Stephen Hemminger132adf52007-03-08 20:44:43 -08001317 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return -EOPNOTSUPP;
1319
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001320 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001321 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Jianjun Kong09cb1052008-11-03 00:27:11 -08001323 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001325 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001327
WANG Cong87e9f032015-11-03 15:41:16 -08001328 if (needs_rtnl)
1329 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 lock_sock(sk);
1331
Stephen Hemminger132adf52007-03-08 20:44:43 -08001332 switch (optname) {
1333 case IP_OPTIONS:
1334 {
1335 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001336 struct ip_options *opt = (struct ip_options *)optbuf;
1337 struct ip_options_rcu *inet_opt;
1338
1339 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001340 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001341 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001342 if (inet_opt)
1343 memcpy(optbuf, &inet_opt->opt,
1344 sizeof(struct ip_options) +
1345 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001346 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Stephen Hemminger132adf52007-03-08 20:44:43 -08001348 if (opt->optlen == 0)
1349 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Stephen Hemminger132adf52007-03-08 20:44:43 -08001351 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Stephen Hemminger132adf52007-03-08 20:44:43 -08001353 len = min_t(unsigned int, len, opt->optlen);
1354 if (put_user(len, optlen))
1355 return -EFAULT;
1356 if (copy_to_user(optval, opt->__data, len))
1357 return -EFAULT;
1358 return 0;
1359 }
1360 case IP_PKTINFO:
1361 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1362 break;
1363 case IP_RECVTTL:
1364 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1365 break;
1366 case IP_RECVTOS:
1367 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1368 break;
1369 case IP_RECVOPTS:
1370 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1371 break;
1372 case IP_RETOPTS:
1373 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1374 break;
1375 case IP_PASSSEC:
1376 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1377 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001378 case IP_RECVORIGDSTADDR:
1379 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1380 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001381 case IP_CHECKSUM:
1382 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1383 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -04001384 case IP_RECVFRAGSIZE:
1385 val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0;
1386 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001387 case IP_TOS:
1388 val = inet->tos;
1389 break;
1390 case IP_TTL:
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001391 {
1392 struct net *net = sock_net(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001393 val = (inet->uc_ttl == -1 ?
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001394 net->ipv4.sysctl_ip_default_ttl :
Stephen Hemminger132adf52007-03-08 20:44:43 -08001395 inet->uc_ttl);
1396 break;
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001397 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001398 case IP_HDRINCL:
1399 val = inet->hdrincl;
1400 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001401 case IP_NODEFRAG:
1402 val = inet->nodefrag;
1403 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001404 case IP_BIND_ADDRESS_NO_PORT:
1405 val = inet->bind_address_no_port;
1406 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001407 case IP_MTU_DISCOVER:
1408 val = inet->pmtudisc;
1409 break;
1410 case IP_MTU:
1411 {
1412 struct dst_entry *dst;
1413 val = 0;
1414 dst = sk_dst_get(sk);
1415 if (dst) {
1416 val = dst_mtu(dst);
1417 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001419 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001421 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001423 break;
1424 }
1425 case IP_RECVERR:
1426 val = inet->recverr;
1427 break;
1428 case IP_MULTICAST_TTL:
1429 val = inet->mc_ttl;
1430 break;
1431 case IP_MULTICAST_LOOP:
1432 val = inet->mc_loop;
1433 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001434 case IP_UNICAST_IF:
1435 val = (__force int)htonl((__u32) inet->uc_index);
1436 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001437 case IP_MULTICAST_IF:
1438 {
1439 struct in_addr addr;
1440 len = min_t(unsigned int, len, sizeof(struct in_addr));
1441 addr.s_addr = inet->mc_addr;
1442 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Stephen Hemminger132adf52007-03-08 20:44:43 -08001444 if (put_user(len, optlen))
1445 return -EFAULT;
1446 if (copy_to_user(optval, &addr, len))
1447 return -EFAULT;
1448 return 0;
1449 }
1450 case IP_MSFILTER:
1451 {
1452 struct ip_msfilter msf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001453
1454 if (len < IP_MSFILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001455 err = -EINVAL;
1456 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001458 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001459 err = -EFAULT;
1460 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001462 err = ip_mc_msfget(sk, &msf,
1463 (struct ip_msfilter __user *)optval, optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001464 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001465 }
1466 case MCAST_MSFILTER:
1467 {
1468 struct group_filter gsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Stephen Hemminger132adf52007-03-08 20:44:43 -08001470 if (len < GROUP_FILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001471 err = -EINVAL;
1472 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001474 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001475 err = -EFAULT;
1476 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001477 }
1478 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001479 (struct group_filter __user *)optval,
1480 optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001481 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001482 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001483 case IP_MULTICAST_ALL:
1484 val = inet->mc_all;
1485 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001486 case IP_PKTOPTIONS:
1487 {
1488 struct msghdr msg;
1489
1490 release_sock(sk);
1491
1492 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001494
Karoly Kemenyc54a5e02014-07-27 12:29:07 +02001495 msg.msg_control = (__force void *) optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001496 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001497 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001498
1499 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1500 struct in_pktinfo info;
1501
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001502 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1503 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001504 info.ipi_ifindex = inet->mc_index;
1505 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1506 }
1507 if (inet->cmsg_flags & IP_CMSG_TTL) {
1508 int hlim = inet->mc_ttl;
1509 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1510 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001511 if (inet->cmsg_flags & IP_CMSG_TOS) {
1512 int tos = inet->rcv_tos;
1513 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1514 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001515 len -= msg.msg_controllen;
1516 return put_user(len, optlen);
1517 }
1518 case IP_FREEBIND:
1519 val = inet->freebind;
1520 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001521 case IP_TRANSPARENT:
1522 val = inet->transparent;
1523 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001524 case IP_MINTTL:
1525 val = inet->min_ttl;
1526 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001527 default:
1528 release_sock(sk);
1529 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001532
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001533 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 unsigned char ucval = (unsigned char)val;
1535 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001536 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001538 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return -EFAULT;
1540 } else {
1541 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001542 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001544 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return -EFAULT;
1546 }
1547 return 0;
WANG Cong87e9f032015-11-03 15:41:16 -08001548
1549out:
1550 release_sock(sk);
1551 if (needs_rtnl)
1552 rtnl_unlock();
1553 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001556int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001557 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001558{
1559 int err;
1560
Daniel Balutadd23198e2011-08-07 22:31:07 -07001561 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001562#ifdef CONFIG_BPFILTER
1563 if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1564 optname < BPFILTER_IPT_GET_MAX)
1565 err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1566#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001567#ifdef CONFIG_NETFILTER
1568 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001569 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1570 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001571 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001572
Jianjun Kong09cb1052008-11-03 00:27:11 -08001573 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001574 return -EFAULT;
1575
Paolo Abeni01ea3062018-02-08 12:19:00 +01001576 err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001577 if (err >= 0)
1578 err = put_user(len, optlen);
1579 return err;
1580 }
1581#endif
1582 return err;
1583}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001584EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001585
1586#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001587int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1588 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001589{
David L Stevens42908c62008-04-29 03:23:22 -07001590 int err;
1591
1592 if (optname == MCAST_MSFILTER)
1593 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1594 ip_getsockopt);
1595
Daniel Balutadd23198e2011-08-07 22:31:07 -07001596 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1597 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001598
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001599#ifdef CONFIG_BPFILTER
1600 if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1601 optname < BPFILTER_IPT_GET_MAX)
1602 err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1603#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001604#ifdef CONFIG_NETFILTER
1605 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001606 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1607 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001608 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001609
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001610 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001611 return -EFAULT;
1612
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001613 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001614 if (err >= 0)
1615 err = put_user(len, optlen);
1616 return err;
1617 }
1618#endif
1619 return err;
1620}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001621EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001622#endif