blob: c0fe5ad996f238091f5b9585adb586a571f653f0 [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;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000152 const struct iphdr *iph = ip_hdr(skb);
Willem de Bruijn2efd4fc2018-07-23 19:36:48 -0400153 __be16 *ports;
154 int end;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800155
Willem de Bruijn2efd4fc2018-07-23 19:36:48 -0400156 end = skb_transport_offset(skb) + 4;
157 if (end > 0 && !pskb_may_pull(skb, end))
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800158 return;
159
160 /* All current transport protocols have the port numbers in the
161 * first four bytes of the transport header and this function is
162 * written with this assumption in mind.
163 */
Willem de Bruijn2efd4fc2018-07-23 19:36:48 -0400164 ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800165
166 sin.sin_family = AF_INET;
167 sin.sin_addr.s_addr = iph->daddr;
168 sin.sin_port = ports[1];
169 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
170
171 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
172}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Paolo Abeniad959032016-11-04 11:28:58 +0100174void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk,
175 struct sk_buff *skb, int tlen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Paolo Abeniad959032016-11-04 11:28:58 +0100177 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000178 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /* Ordered by supposed usage frequency */
Tom Herbertc44d13d2015-01-05 13:56:15 -0800181 if (flags & IP_CMSG_PKTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 ip_cmsg_recv_pktinfo(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Tom Herbertc44d13d2015-01-05 13:56:15 -0800184 flags &= ~IP_CMSG_PKTINFO;
185 if (!flags)
186 return;
187 }
188
189 if (flags & IP_CMSG_TTL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 ip_cmsg_recv_ttl(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Tom Herbertc44d13d2015-01-05 13:56:15 -0800192 flags &= ~IP_CMSG_TTL;
193 if (!flags)
194 return;
195 }
196
197 if (flags & IP_CMSG_TOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 ip_cmsg_recv_tos(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Tom Herbertc44d13d2015-01-05 13:56:15 -0800200 flags &= ~IP_CMSG_TOS;
201 if (!flags)
202 return;
203 }
204
205 if (flags & IP_CMSG_RECVOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 ip_cmsg_recv_opts(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Tom Herbertc44d13d2015-01-05 13:56:15 -0800208 flags &= ~IP_CMSG_RECVOPTS;
209 if (!flags)
210 return;
211 }
212
213 if (flags & IP_CMSG_RETOPTS) {
Paolo Abeni91ed1e62017-08-03 18:07:06 +0200214 ip_cmsg_recv_retopts(sock_net(sk), msg, skb);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800215
Tom Herbertc44d13d2015-01-05 13:56:15 -0800216 flags &= ~IP_CMSG_RETOPTS;
217 if (!flags)
218 return;
219 }
220
221 if (flags & IP_CMSG_PASSSEC) {
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800222 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800223
Tom Herbertc44d13d2015-01-05 13:56:15 -0800224 flags &= ~IP_CMSG_PASSSEC;
225 if (!flags)
226 return;
227 }
228
Tom Herbertad6f9392015-01-05 13:56:17 -0800229 if (flags & IP_CMSG_ORIGDSTADDR) {
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800230 ip_cmsg_recv_dstaddr(msg, skb);
231
Tom Herbertad6f9392015-01-05 13:56:17 -0800232 flags &= ~IP_CMSG_ORIGDSTADDR;
233 if (!flags)
234 return;
235 }
236
237 if (flags & IP_CMSG_CHECKSUM)
Eric Dumazet10df8e62016-10-23 18:03:06 -0700238 ip_cmsg_recv_checksum(msg, skb, tlen, offset);
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400239
240 if (flags & IP_CMSG_RECVFRAGSIZE)
241 ip_cmsg_recv_fragsize(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
Tom Herbert5961de92015-01-05 13:56:16 -0800243EXPORT_SYMBOL(ip_cmsg_recv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400245int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100246 bool allow_ipv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Francesco Fuscof02db312013-09-24 15:43:08 +0200248 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct cmsghdr *cmsg;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400250 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Gu Zhengf95b4142014-12-11 11:22:04 +0800252 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!CMSG_OK(msg, cmsg))
254 return -EINVAL;
Eric Dumazet5337b5b2014-11-10 17:54:25 -0800255#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100256 if (allow_ipv6 &&
257 cmsg->cmsg_level == SOL_IPV6 &&
258 cmsg->cmsg_type == IPV6_PKTINFO) {
259 struct in6_pktinfo *src_info;
260
261 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
262 return -EINVAL;
263 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
264 if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
265 return -EINVAL;
David Ahern1cbec072018-02-16 11:03:03 -0800266 if (src_info->ipi6_ifindex)
267 ipc->oif = src_info->ipi6_ifindex;
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100268 ipc->addr = src_info->ipi6_addr.s6_addr32[3];
269 continue;
270 }
271#endif
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400272 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700273 err = __sock_cmsg_send(sk, msg, cmsg, &ipc->sockc);
274 if (err)
275 return err;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400276 continue;
277 }
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (cmsg->cmsg_level != SOL_IP)
280 continue;
281 switch (cmsg->cmsg_type) {
282 case IP_RETOPTS:
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800283 err = cmsg->cmsg_len - sizeof(struct cmsghdr);
Eric Dumazet91948302016-02-04 06:23:28 -0800284
285 /* Our caller is responsible for freeing ipc->opt */
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700286 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
287 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (err)
289 return err;
290 break;
291 case IP_PKTINFO:
292 {
293 struct in_pktinfo *info;
294 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
295 return -EINVAL;
296 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
David Ahern1cbec072018-02-16 11:03:03 -0800297 if (info->ipi_ifindex)
298 ipc->oif = info->ipi_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 ipc->addr = info->ipi_spec_dst.s_addr;
300 break;
301 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200302 case IP_TTL:
303 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
304 return -EINVAL;
305 val = *(int *)CMSG_DATA(cmsg);
306 if (val < 1 || val > 255)
307 return -EINVAL;
308 ipc->ttl = val;
309 break;
310 case IP_TOS:
Eric Dumazete895cdc2016-09-07 21:52:56 -0700311 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)))
312 val = *(int *)CMSG_DATA(cmsg);
313 else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8)))
314 val = *(u8 *)CMSG_DATA(cmsg);
315 else
Francesco Fuscof02db312013-09-24 15:43:08 +0200316 return -EINVAL;
Francesco Fuscof02db312013-09-24 15:43:08 +0200317 if (val < 0 || val > 255)
318 return -EINVAL;
319 ipc->tos = val;
320 ipc->priority = rt_tos2priority(ipc->tos);
321 break;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 default:
324 return -EINVAL;
325 }
326 }
327 return 0;
328}
329
Eric Dumazet592fcb92010-06-09 16:21:07 +0000330static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000331{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000332 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
333
334 sock_put(ra->saved_sk);
335 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000336}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700338int ip_ra_control(struct sock *sk, unsigned char on,
339 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000341 struct ip_ra_chain *ra, *new_ra;
342 struct ip_ra_chain __rcu **rap;
Kirill Tkhai5796ef72018-03-22 12:45:32 +0300343 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000345 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return -EINVAL;
347
348 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
349
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300350 mutex_lock(&net->ipv4.ra_mutex);
Kirill Tkhai5796ef72018-03-22 12:45:32 +0300351 for (rap = &net->ipv4.ra_chain;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300352 (ra = rcu_dereference_protected(*rap,
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300353 lockdep_is_held(&net->ipv4.ra_mutex))) != NULL;
Eric Dumazet43a951e2010-10-25 03:32:44 +0000354 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (ra->sk == sk) {
356 if (on) {
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300357 mutex_unlock(&net->ipv4.ra_mutex);
Jesper Juhla51482b2005-11-08 09:41:34 -0800358 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return -EADDRINUSE;
360 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000361 /* dont let ip_call_ra_chain() use sk again */
362 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700363 RCU_INIT_POINTER(*rap, ra->next);
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300364 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 if (ra->destructor)
367 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000368 /*
369 * Delay sock_put(sk) and kfree(ra) after one rcu grace
370 * period. This guarantee ip_call_ra_chain() dont need
371 * to mess with socket refcounts.
372 */
373 ra->saved_sk = sk;
374 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
376 }
377 }
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300378 if (!new_ra) {
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300379 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return -ENOBUFS;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 new_ra->sk = sk;
383 new_ra->destructor = destructor;
384
Eric Dumazet8e380f02014-09-09 08:11:41 -0700385 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000386 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 sock_hold(sk);
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300388 mutex_unlock(&net->ipv4.ra_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 return 0;
391}
392
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900393void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700394 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct sock_exterr_skb *serr;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 skb = skb_clone(skb, GFP_ATOMIC);
399 if (!skb)
400 return;
401
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900402 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 serr->ee.ee_errno = err;
404 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300405 serr->ee.ee_type = icmp_hdr(skb)->type;
406 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 serr->ee.ee_pad = 0;
408 serr->ee.ee_info = info;
409 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300410 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700411 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 serr->port = port;
413
Ian Morris00db4122015-04-03 09:17:27 +0100414 if (skb_pull(skb, payload - skb->data)) {
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300415 skb_reset_transport_header(skb);
416 if (sock_queue_err_skb(sk, skb) == 0)
417 return;
418 }
419 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Al Viro05790162006-09-27 18:33:40 -0700422void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct inet_sock *inet = inet_sk(sk);
425 struct sock_exterr_skb *serr;
426 struct iphdr *iph;
427 struct sk_buff *skb;
428
429 if (!inet->recverr)
430 return;
431
432 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
433 if (!skb)
434 return;
435
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300436 skb_put(skb, sizeof(struct iphdr));
437 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700438 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 iph->daddr = daddr;
440
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900441 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 serr->ee.ee_errno = err;
443 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900444 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 serr->ee.ee_code = 0;
446 serr->ee.ee_pad = 0;
447 serr->ee.ee_info = info;
448 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700449 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 serr->port = port;
451
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700452 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300453 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 if (sock_queue_err_skb(sk, skb))
456 kfree_skb(skb);
457}
458
Julian Anastasov34b99df2015-06-23 08:34:39 +0300459/* For some errors we have valid addr_offset even with zero payload and
460 * zero port. Also, addr_offset should be supported if port is set.
461 */
462static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
463{
464 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
465 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
466}
467
Willem de Bruijnc247f052015-03-07 20:33:22 -0500468/* IPv4 supports cmsg on all imcp errors and some timestamps
469 *
470 * Timestamp code paths do not initialize the fields expected by cmsg:
471 * the PKTINFO fields in skb->cb[]. Fill those in here.
472 */
473static bool ipv4_datagram_support_cmsg(const struct sock *sk,
474 struct sk_buff *skb,
475 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500476{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500477 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500478
Willem de Bruijnc247f052015-03-07 20:33:22 -0500479 if (ee_origin == SO_EE_ORIGIN_ICMP)
480 return true;
481
482 if (ee_origin == SO_EE_ORIGIN_LOCAL)
483 return false;
484
485 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
Willem de Bruijn1862d622017-04-12 19:24:35 -0400486 * timestamp with egress dev. Not possible for packets without iif
Willem de Bruijnc247f052015-03-07 20:33:22 -0500487 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
488 */
Willem de Bruijn1862d622017-04-12 19:24:35 -0400489 info = PKTINFO_SKB_CB(skb);
490 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
491 !info->ipi_ifindex)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500492 return false;
493
494 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500495 return true;
496}
497
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900498/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * Handle MSG_ERRQUEUE
500 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100501int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400504 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100505 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct {
507 struct sock_extended_err ee;
508 struct sockaddr_in offender;
509 } errhdr;
510 int err;
511 int copied;
512
513 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400514 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100515 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto out;
517
518 copied = skb->len;
519 if (copied > len) {
520 msg->msg_flags |= MSG_TRUNC;
521 copied = len;
522 }
David S. Miller51f3d022014-11-05 16:46:40 -0500523 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700524 if (unlikely(err)) {
525 kfree_skb(skb);
526 return err;
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 sock_recv_timestamp(msg, sk, skb);
529
530 serr = SKB_EXT_ERR(skb);
531
Julian Anastasov34b99df2015-06-23 08:34:39 +0300532 if (sin && ipv4_datagram_support_addr(serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700534 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
535 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 sin->sin_port = serr->port;
537 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100538 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540
541 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
542 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500543 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500544
Willem de Bruijnc247f052015-03-07 20:33:22 -0500545 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700547 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500548 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 ip_cmsg_recv(msg, skb);
550 }
551
552 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
553
554 /* Now we could try to dump offended packet options */
555
556 msg->msg_flags |= MSG_ERRQUEUE;
557 err = copied;
558
Eric Dumazet960a2622016-04-21 22:27:32 -0700559 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560out:
561 return err;
562}
563
564
565/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700566 * Socket option code for IP. This is the end of the line after any
567 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300569static bool setsockopt_needs_rtnl(int optname)
570{
571 switch (optname) {
572 case IP_ADD_MEMBERSHIP:
573 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300574 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300575 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300576 case IP_DROP_SOURCE_MEMBERSHIP:
577 case IP_MSFILTER:
578 case IP_UNBLOCK_SOURCE:
579 case MCAST_BLOCK_SOURCE:
580 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300581 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300582 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300583 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300584 case MCAST_LEAVE_SOURCE_GROUP:
585 case MCAST_UNBLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300586 return true;
587 }
588 return false;
589}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800591static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700592 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 struct inet_sock *inet = inet_sk(sk);
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200595 struct net *net = sock_net(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800596 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300597 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Xi Wang0c9f79b2012-11-11 11:20:01 +0000599 switch (optname) {
600 case IP_PKTINFO:
601 case IP_RECVTTL:
602 case IP_RECVOPTS:
603 case IP_RECVTOS:
604 case IP_RETOPTS:
605 case IP_TOS:
606 case IP_TTL:
607 case IP_HDRINCL:
608 case IP_MTU_DISCOVER:
609 case IP_RECVERR:
610 case IP_ROUTER_ALERT:
611 case IP_FREEBIND:
612 case IP_PASSSEC:
613 case IP_TRANSPARENT:
614 case IP_MINTTL:
615 case IP_NODEFRAG:
Eric Dumazet90c337d2015-06-06 21:17:57 -0700616 case IP_BIND_ADDRESS_NO_PORT:
Xi Wang0c9f79b2012-11-11 11:20:01 +0000617 case IP_UNICAST_IF:
618 case IP_MULTICAST_TTL:
619 case IP_MULTICAST_ALL:
620 case IP_MULTICAST_LOOP:
621 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800622 case IP_CHECKSUM:
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400623 case IP_RECVFRAGSIZE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (optlen >= sizeof(int)) {
625 if (get_user(val, (int __user *) optval))
626 return -EFAULT;
627 } else if (optlen >= sizeof(char)) {
628 unsigned char ucval;
629
630 if (get_user(ucval, (unsigned char __user *) optval))
631 return -EFAULT;
632 val = (int) ucval;
633 }
634 }
635
636 /* If optlen==0, it is equivalent to val == 0 */
637
Kirill Tkhai05269472018-03-22 12:45:12 +0300638 if (optname == IP_ROUTER_ALERT)
639 return ip_ra_control(sk, val ? 1 : 0, NULL);
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800640 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800641 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300644 if (needs_rtnl)
645 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 lock_sock(sk);
647
648 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800649 case IP_OPTIONS:
650 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000651 struct ip_options_rcu *old, *opt = NULL;
652
roel kluin65a1c4f2009-10-23 05:59:21 +0000653 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800654 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900655 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700656 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800657 if (err)
658 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000659 old = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200660 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800661 if (inet->is_icsk) {
662 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000663#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800664 if (sk->sk_family == PF_INET ||
665 (!((1 << sk->sk_state) &
666 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000667 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000669 if (old)
670 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800671 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000672 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800673 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000674#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000678 rcu_assign_pointer(inet->inet_opt, opt);
679 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800680 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800681 break;
682 }
683 case IP_PKTINFO:
684 if (val)
685 inet->cmsg_flags |= IP_CMSG_PKTINFO;
686 else
687 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
688 break;
689 case IP_RECVTTL:
690 if (val)
691 inet->cmsg_flags |= IP_CMSG_TTL;
692 else
693 inet->cmsg_flags &= ~IP_CMSG_TTL;
694 break;
695 case IP_RECVTOS:
696 if (val)
697 inet->cmsg_flags |= IP_CMSG_TOS;
698 else
699 inet->cmsg_flags &= ~IP_CMSG_TOS;
700 break;
701 case IP_RECVOPTS:
702 if (val)
703 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
704 else
705 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
706 break;
707 case IP_RETOPTS:
708 if (val)
709 inet->cmsg_flags |= IP_CMSG_RETOPTS;
710 else
711 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
712 break;
713 case IP_PASSSEC:
714 if (val)
715 inet->cmsg_flags |= IP_CMSG_PASSSEC;
716 else
717 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
718 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800719 case IP_RECVORIGDSTADDR:
720 if (val)
721 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
722 else
723 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
724 break;
Tom Herbertad6f9392015-01-05 13:56:17 -0800725 case IP_CHECKSUM:
726 if (val) {
727 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
728 inet_inc_convert_csum(sk);
729 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
730 }
731 } else {
732 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
733 inet_dec_convert_csum(sk);
734 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
735 }
736 }
737 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400738 case IP_RECVFRAGSIZE:
739 if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM)
740 goto e_inval;
741 if (val)
742 inet->cmsg_flags |= IP_CMSG_RECVFRAGSIZE;
743 else
744 inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE;
745 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800746 case IP_TOS: /* This sets both TOS and Precedence */
747 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400748 val &= ~INET_ECN_MASK;
749 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800751 if (inet->tos != val) {
752 inet->tos = val;
753 sk->sk_priority = rt_tos2priority(val);
754 sk_dst_reset(sk);
755 }
756 break;
757 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700758 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800759 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000760 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800761 goto e_inval;
762 inet->uc_ttl = val;
763 break;
764 case IP_HDRINCL:
765 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800768 }
769 inet->hdrincl = val ? 1 : 0;
770 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000771 case IP_NODEFRAG:
772 if (sk->sk_type != SOCK_RAW) {
773 err = -ENOPROTOOPT;
774 break;
775 }
776 inet->nodefrag = val ? 1 : 0;
777 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -0700778 case IP_BIND_ADDRESS_NO_PORT:
779 inet->bind_address_no_port = val ? 1 : 0;
780 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800781 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100782 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800783 goto e_inval;
784 inet->pmtudisc = val;
785 break;
786 case IP_RECVERR:
787 inet->recverr = !!val;
788 if (!val)
789 skb_queue_purge(&sk->sk_error_queue);
790 break;
791 case IP_MULTICAST_TTL:
792 if (sk->sk_type == SOCK_STREAM)
793 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700794 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800795 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800796 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800797 val = 1;
798 if (val < 0 || val > 255)
799 goto e_inval;
800 inet->mc_ttl = val;
801 break;
802 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700803 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800804 goto e_inval;
805 inet->mc_loop = !!val;
806 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000807 case IP_UNICAST_IF:
808 {
809 struct net_device *dev = NULL;
810 int ifindex;
David Ahern9515a2e2018-01-24 19:37:38 -0800811 int midx;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000812
813 if (optlen != sizeof(int))
814 goto e_inval;
815
816 ifindex = (__force int)ntohl((__force __be32)val);
817 if (ifindex == 0) {
818 inet->uc_index = 0;
819 err = 0;
820 break;
821 }
822
823 dev = dev_get_by_index(sock_net(sk), ifindex);
824 err = -EADDRNOTAVAIL;
825 if (!dev)
826 break;
David Ahern9515a2e2018-01-24 19:37:38 -0800827
828 midx = l3mdev_master_ifindex(dev);
Erich E. Hoover76e21052012-02-08 09:11:07 +0000829 dev_put(dev);
830
831 err = -EINVAL;
David Ahern9515a2e2018-01-24 19:37:38 -0800832 if (sk->sk_bound_dev_if &&
833 (!midx || midx != sk->sk_bound_dev_if))
Erich E. Hoover76e21052012-02-08 09:11:07 +0000834 break;
835
836 inet->uc_index = ifindex;
837 err = 0;
838 break;
839 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800840 case IP_MULTICAST_IF:
841 {
842 struct ip_mreqn mreq;
843 struct net_device *dev = NULL;
David Ahern7bb387c2016-12-29 15:39:37 -0800844 int midx;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800845
846 if (sk->sk_type == SOCK_STREAM)
847 goto e_inval;
848 /*
849 * Check the arguments are allowable
850 */
851
Shan Wei09159212009-09-22 15:41:10 +0000852 if (optlen < sizeof(struct in_addr))
853 goto e_inval;
854
Stephen Hemminger132adf52007-03-08 20:44:43 -0800855 err = -EFAULT;
856 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800857 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800858 break;
859 } else {
860 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000861 if (optlen >= sizeof(struct ip_mreq)) {
862 if (copy_from_user(&mreq, optval,
863 sizeof(struct ip_mreq)))
864 break;
865 } else if (optlen >= sizeof(struct in_addr)) {
866 if (copy_from_user(&mreq.imr_address, optval,
867 sizeof(struct in_addr)))
868 break;
869 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800870 }
871
872 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700873 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800874 inet->mc_index = 0;
875 inet->mc_addr = 0;
876 err = 0;
877 break;
878 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900879 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000880 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800881 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800882 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000883 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800884
885
886 err = -EADDRNOTAVAIL;
887 if (!dev)
888 break;
David Ahern7bb387c2016-12-29 15:39:37 -0800889
890 midx = l3mdev_master_ifindex(dev);
891
Eric Dumazet55b80502009-10-19 06:41:58 +0000892 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800893
894 err = -EINVAL;
895 if (sk->sk_bound_dev_if &&
David Ahern7bb387c2016-12-29 15:39:37 -0800896 mreq.imr_ifindex != sk->sk_bound_dev_if &&
897 (!midx || midx != sk->sk_bound_dev_if))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800898 break;
899
900 inet->mc_index = mreq.imr_ifindex;
901 inet->mc_addr = mreq.imr_address.s_addr;
902 err = 0;
903 break;
904 }
905
906 case IP_ADD_MEMBERSHIP:
907 case IP_DROP_MEMBERSHIP:
908 {
909 struct ip_mreqn mreq;
910
Flavio Leitnera96fb492007-08-24 22:16:39 -0700911 err = -EPROTO;
912 if (inet_sk(sk)->is_icsk)
913 break;
914
Stephen Hemminger132adf52007-03-08 20:44:43 -0800915 if (optlen < sizeof(struct ip_mreq))
916 goto e_inval;
917 err = -EFAULT;
918 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800919 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800920 break;
921 } else {
922 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800923 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800924 break;
925 }
926
927 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300928 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800929 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300930 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800931 break;
932 }
933 case IP_MSFILTER:
934 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800935 struct ip_msfilter *msf;
936
937 if (optlen < IP_MSFILTER_SIZE(0))
938 goto e_inval;
939 if (optlen > sysctl_optmem_max) {
940 err = -ENOBUFS;
941 break;
942 }
Al Viroa2c841d2017-05-13 18:26:06 -0400943 msf = memdup_user(optval, optlen);
944 if (IS_ERR(msf)) {
945 err = PTR_ERR(msf);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800946 break;
947 }
948 /* numsrc >= (1G-4) overflow in 32 bits */
949 if (msf->imsf_numsrc >= 0x3ffffffcU ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200950 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800951 kfree(msf);
952 err = -ENOBUFS;
953 break;
954 }
955 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
956 kfree(msf);
957 err = -EINVAL;
958 break;
959 }
960 err = ip_mc_msfilter(sk, msf, 0);
961 kfree(msf);
962 break;
963 }
964 case IP_BLOCK_SOURCE:
965 case IP_UNBLOCK_SOURCE:
966 case IP_ADD_SOURCE_MEMBERSHIP:
967 case IP_DROP_SOURCE_MEMBERSHIP:
968 {
969 struct ip_mreq_source mreqs;
970 int omode, add;
971
972 if (optlen != sizeof(struct ip_mreq_source))
973 goto e_inval;
974 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
975 err = -EFAULT;
976 break;
977 }
978 if (optname == IP_BLOCK_SOURCE) {
979 omode = MCAST_EXCLUDE;
980 add = 1;
981 } else if (optname == IP_UNBLOCK_SOURCE) {
982 omode = MCAST_EXCLUDE;
983 add = 0;
984 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
985 struct ip_mreqn mreq;
986
987 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
988 mreq.imr_address.s_addr = mreqs.imr_interface;
989 mreq.imr_ifindex = 0;
Hangbin Liu6e2059b2018-07-10 22:41:26 +0800990 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800991 if (err && err != -EADDRINUSE)
992 break;
993 omode = MCAST_INCLUDE;
994 add = 1;
995 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
996 omode = MCAST_INCLUDE;
997 add = 0;
998 }
999 err = ip_mc_source(add, omode, sk, &mreqs, 0);
1000 break;
1001 }
1002 case MCAST_JOIN_GROUP:
1003 case MCAST_LEAVE_GROUP:
1004 {
1005 struct group_req greq;
1006 struct sockaddr_in *psin;
1007 struct ip_mreqn mreq;
1008
1009 if (optlen < sizeof(struct group_req))
1010 goto e_inval;
1011 err = -EFAULT;
1012 if (copy_from_user(&greq, optval, sizeof(greq)))
1013 break;
1014 psin = (struct sockaddr_in *)&greq.gr_group;
1015 if (psin->sin_family != AF_INET)
1016 goto e_inval;
1017 memset(&mreq, 0, sizeof(mreq));
1018 mreq.imr_multiaddr = psin->sin_addr;
1019 mreq.imr_ifindex = greq.gr_interface;
1020
1021 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001022 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001023 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001024 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001025 break;
1026 }
1027 case MCAST_JOIN_SOURCE_GROUP:
1028 case MCAST_LEAVE_SOURCE_GROUP:
1029 case MCAST_BLOCK_SOURCE:
1030 case MCAST_UNBLOCK_SOURCE:
1031 {
1032 struct group_source_req greqs;
1033 struct ip_mreq_source mreqs;
1034 struct sockaddr_in *psin;
1035 int omode, add;
1036
1037 if (optlen != sizeof(struct group_source_req))
1038 goto e_inval;
1039 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
1040 err = -EFAULT;
1041 break;
1042 }
1043 if (greqs.gsr_group.ss_family != AF_INET ||
1044 greqs.gsr_source.ss_family != AF_INET) {
1045 err = -EADDRNOTAVAIL;
1046 break;
1047 }
1048 psin = (struct sockaddr_in *)&greqs.gsr_group;
1049 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
1050 psin = (struct sockaddr_in *)&greqs.gsr_source;
1051 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
1052 mreqs.imr_interface = 0; /* use index for mc_source */
1053
1054 if (optname == MCAST_BLOCK_SOURCE) {
1055 omode = MCAST_EXCLUDE;
1056 add = 1;
1057 } else if (optname == MCAST_UNBLOCK_SOURCE) {
1058 omode = MCAST_EXCLUDE;
1059 add = 0;
1060 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
1061 struct ip_mreqn mreq;
1062
1063 psin = (struct sockaddr_in *)&greqs.gsr_group;
1064 mreq.imr_multiaddr = psin->sin_addr;
1065 mreq.imr_address.s_addr = 0;
1066 mreq.imr_ifindex = greqs.gsr_interface;
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001067 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001068 if (err && err != -EADDRINUSE)
1069 break;
1070 greqs.gsr_interface = mreq.imr_ifindex;
1071 omode = MCAST_INCLUDE;
1072 add = 1;
1073 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1074 omode = MCAST_INCLUDE;
1075 add = 0;
1076 }
1077 err = ip_mc_source(add, omode, sk, &mreqs,
1078 greqs.gsr_interface);
1079 break;
1080 }
1081 case MCAST_MSFILTER:
1082 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001083 struct sockaddr_in *psin;
1084 struct ip_msfilter *msf = NULL;
1085 struct group_filter *gsf = NULL;
1086 int msize, i, ifindex;
1087
1088 if (optlen < GROUP_FILTER_SIZE(0))
1089 goto e_inval;
1090 if (optlen > sysctl_optmem_max) {
1091 err = -ENOBUFS;
1092 break;
1093 }
Al Viroa2c841d2017-05-13 18:26:06 -04001094 gsf = memdup_user(optval, optlen);
1095 if (IS_ERR(gsf)) {
1096 err = PTR_ERR(gsf);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001097 break;
1098 }
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001099
Stephen Hemminger132adf52007-03-08 20:44:43 -08001100 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1101 if (gsf->gf_numsrc >= 0x1ffffff ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +02001102 gsf->gf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001103 err = -ENOBUFS;
1104 goto mc_msf_out;
1105 }
1106 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
1107 err = -EINVAL;
1108 goto mc_msf_out;
1109 }
1110 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -08001111 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001112 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001113 err = -ENOBUFS;
1114 goto mc_msf_out;
1115 }
1116 ifindex = gsf->gf_interface;
1117 psin = (struct sockaddr_in *)&gsf->gf_group;
1118 if (psin->sin_family != AF_INET) {
1119 err = -EADDRNOTAVAIL;
1120 goto mc_msf_out;
1121 }
1122 msf->imsf_multiaddr = psin->sin_addr.s_addr;
1123 msf->imsf_interface = 0;
1124 msf->imsf_fmode = gsf->gf_fmode;
1125 msf->imsf_numsrc = gsf->gf_numsrc;
1126 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001127 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001128 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
1129
1130 if (psin->sin_family != AF_INET)
1131 goto mc_msf_out;
1132 msf->imsf_slist[i] = psin->sin_addr.s_addr;
1133 }
1134 kfree(gsf);
1135 gsf = NULL;
1136
1137 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001138mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -08001139 kfree(msf);
1140 kfree(gsf);
1141 break;
1142 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001143 case IP_MULTICAST_ALL:
1144 if (optlen < 1)
1145 goto e_inval;
1146 if (val != 0 && val != 1)
1147 goto e_inval;
1148 inet->mc_all = val;
1149 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001150
1151 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001152 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001153 goto e_inval;
1154 inet->freebind = !!val;
1155 break;
1156
1157 case IP_IPSEC_POLICY:
1158 case IP_XFRM_POLICY:
1159 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001160 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001161 break;
1162 err = xfrm_user_policy(sk, optname, optval, optlen);
1163 break;
1164
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001165 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001166 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1167 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001168 err = -EPERM;
1169 break;
1170 }
1171 if (optlen < 1)
1172 goto e_inval;
1173 inet->transparent = !!val;
1174 break;
1175
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001176 case IP_MINTTL:
1177 if (optlen < 1)
1178 goto e_inval;
1179 if (val < 0 || val > 255)
1180 goto e_inval;
1181 inet->min_ttl = val;
1182 break;
1183
Stephen Hemminger132adf52007-03-08 20:44:43 -08001184 default:
1185 err = -ENOPROTOOPT;
1186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001189 if (needs_rtnl)
1190 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return err;
1192
1193e_inval:
1194 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001195 if (needs_rtnl)
1196 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return -EINVAL;
1198}
1199
Eric Dumazetf84af322010-04-28 15:31:51 -07001200/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001201 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001202 * @sk: socket
1203 * @skb: buffer
1204 *
David S. Miller35ebf652012-06-28 03:59:11 -07001205 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1206 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001207 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001208 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001209void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001210{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001211 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001212 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1213 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001214
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001215 if (prepare && skb_rtable(skb)) {
David Ahern0b922b72016-05-10 11:19:51 -07001216 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1217 * which has interface index (iif) as the first member of the
1218 * underlying inet{6}_skb_parm struct. This code then overlays
1219 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001220 * element so the iif is picked up from the prior IPCB. If iif
1221 * is the loopback interface, then return the sending interface
1222 * (e.g., process binds socket to eth0 for Tx which is
1223 * redirected to loopback in the rtable/dst).
David Ahern0b922b72016-05-10 11:19:51 -07001224 */
David Aherncbea8f02017-09-13 17:11:37 -07001225 struct rtable *rt = skb_rtable(skb);
1226 bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags);
1227
1228 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001229 pktinfo->ipi_ifindex = inet_iif(skb);
David Aherncbea8f02017-09-13 17:11:37 -07001230 else if (l3slave && rt && rt->rt_iif)
1231 pktinfo->ipi_ifindex = rt->rt_iif;
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001232
David S. Miller35ebf652012-06-28 03:59:11 -07001233 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001234 } else {
1235 pktinfo->ipi_ifindex = 0;
1236 pktinfo->ipi_spec_dst.s_addr = 0;
1237 }
Paolo Abeni61a10302017-08-03 18:07:07 +02001238 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001239}
Eric Dumazetf84af322010-04-28 15:31:51 -07001240
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001241int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001242 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001243{
1244 int err;
1245
1246 if (level != SOL_IP)
1247 return -ENOPROTOOPT;
1248
1249 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001250#ifdef CONFIG_BPFILTER
1251 if (optname >= BPFILTER_IPT_SO_SET_REPLACE &&
1252 optname < BPFILTER_IPT_SET_MAX)
1253 err = bpfilter_ip_set_sockopt(sk, optname, optval, optlen);
1254#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001255#ifdef CONFIG_NETFILTER
1256 /* we need to exclude all possible ENOPROTOOPTs except default case */
1257 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001258 optname != IP_IPSEC_POLICY &&
1259 optname != IP_XFRM_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001260 !ip_mroute_opt(optname))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001261 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001262#endif
1263 return err;
1264}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001265EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001266
1267#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001268int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001269 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001270{
1271 int err;
1272
1273 if (level != SOL_IP)
1274 return -ENOPROTOOPT;
1275
David L Stevensdae50292008-04-27 01:06:07 -07001276 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1277 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1278 ip_setsockopt);
1279
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001280 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1281#ifdef CONFIG_NETFILTER
1282 /* we need to exclude all possible ENOPROTOOPTs except default case */
1283 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001284 optname != IP_IPSEC_POLICY &&
1285 optname != IP_XFRM_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001286 !ip_mroute_opt(optname))
1287 err = compat_nf_setsockopt(sk, PF_INET, optname, optval,
1288 optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001289#endif
1290 return err;
1291}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001292EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001293#endif
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001296 * Get the options. Note for future reference. The GET of IP options gets
1297 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 */
1299
WANG Cong87e9f032015-11-03 15:41:16 -08001300static bool getsockopt_needs_rtnl(int optname)
1301{
1302 switch (optname) {
1303 case IP_MSFILTER:
1304 case MCAST_MSFILTER:
1305 return true;
1306 }
1307 return false;
1308}
1309
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001310static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001311 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 struct inet_sock *inet = inet_sk(sk);
WANG Cong87e9f032015-11-03 15:41:16 -08001314 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1315 int val, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001317
Stephen Hemminger132adf52007-03-08 20:44:43 -08001318 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return -EOPNOTSUPP;
1320
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001321 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001322 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Jianjun Kong09cb1052008-11-03 00:27:11 -08001324 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001326 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001328
WANG Cong87e9f032015-11-03 15:41:16 -08001329 if (needs_rtnl)
1330 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 lock_sock(sk);
1332
Stephen Hemminger132adf52007-03-08 20:44:43 -08001333 switch (optname) {
1334 case IP_OPTIONS:
1335 {
1336 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001337 struct ip_options *opt = (struct ip_options *)optbuf;
1338 struct ip_options_rcu *inet_opt;
1339
1340 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001341 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001342 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001343 if (inet_opt)
1344 memcpy(optbuf, &inet_opt->opt,
1345 sizeof(struct ip_options) +
1346 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001347 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Stephen Hemminger132adf52007-03-08 20:44:43 -08001349 if (opt->optlen == 0)
1350 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Stephen Hemminger132adf52007-03-08 20:44:43 -08001352 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Stephen Hemminger132adf52007-03-08 20:44:43 -08001354 len = min_t(unsigned int, len, opt->optlen);
1355 if (put_user(len, optlen))
1356 return -EFAULT;
1357 if (copy_to_user(optval, opt->__data, len))
1358 return -EFAULT;
1359 return 0;
1360 }
1361 case IP_PKTINFO:
1362 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1363 break;
1364 case IP_RECVTTL:
1365 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1366 break;
1367 case IP_RECVTOS:
1368 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1369 break;
1370 case IP_RECVOPTS:
1371 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1372 break;
1373 case IP_RETOPTS:
1374 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1375 break;
1376 case IP_PASSSEC:
1377 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1378 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001379 case IP_RECVORIGDSTADDR:
1380 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1381 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001382 case IP_CHECKSUM:
1383 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1384 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -04001385 case IP_RECVFRAGSIZE:
1386 val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0;
1387 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001388 case IP_TOS:
1389 val = inet->tos;
1390 break;
1391 case IP_TTL:
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001392 {
1393 struct net *net = sock_net(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001394 val = (inet->uc_ttl == -1 ?
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001395 net->ipv4.sysctl_ip_default_ttl :
Stephen Hemminger132adf52007-03-08 20:44:43 -08001396 inet->uc_ttl);
1397 break;
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001398 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001399 case IP_HDRINCL:
1400 val = inet->hdrincl;
1401 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001402 case IP_NODEFRAG:
1403 val = inet->nodefrag;
1404 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001405 case IP_BIND_ADDRESS_NO_PORT:
1406 val = inet->bind_address_no_port;
1407 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001408 case IP_MTU_DISCOVER:
1409 val = inet->pmtudisc;
1410 break;
1411 case IP_MTU:
1412 {
1413 struct dst_entry *dst;
1414 val = 0;
1415 dst = sk_dst_get(sk);
1416 if (dst) {
1417 val = dst_mtu(dst);
1418 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001420 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001422 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001424 break;
1425 }
1426 case IP_RECVERR:
1427 val = inet->recverr;
1428 break;
1429 case IP_MULTICAST_TTL:
1430 val = inet->mc_ttl;
1431 break;
1432 case IP_MULTICAST_LOOP:
1433 val = inet->mc_loop;
1434 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001435 case IP_UNICAST_IF:
1436 val = (__force int)htonl((__u32) inet->uc_index);
1437 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001438 case IP_MULTICAST_IF:
1439 {
1440 struct in_addr addr;
1441 len = min_t(unsigned int, len, sizeof(struct in_addr));
1442 addr.s_addr = inet->mc_addr;
1443 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Stephen Hemminger132adf52007-03-08 20:44:43 -08001445 if (put_user(len, optlen))
1446 return -EFAULT;
1447 if (copy_to_user(optval, &addr, len))
1448 return -EFAULT;
1449 return 0;
1450 }
1451 case IP_MSFILTER:
1452 {
1453 struct ip_msfilter msf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001454
1455 if (len < IP_MSFILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001456 err = -EINVAL;
1457 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001459 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001460 err = -EFAULT;
1461 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001463 err = ip_mc_msfget(sk, &msf,
1464 (struct ip_msfilter __user *)optval, optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001465 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001466 }
1467 case MCAST_MSFILTER:
1468 {
1469 struct group_filter gsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Stephen Hemminger132adf52007-03-08 20:44:43 -08001471 if (len < GROUP_FILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001472 err = -EINVAL;
1473 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001475 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001476 err = -EFAULT;
1477 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001478 }
1479 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001480 (struct group_filter __user *)optval,
1481 optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001482 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001483 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001484 case IP_MULTICAST_ALL:
1485 val = inet->mc_all;
1486 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001487 case IP_PKTOPTIONS:
1488 {
1489 struct msghdr msg;
1490
1491 release_sock(sk);
1492
1493 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001495
Karoly Kemenyc54a5e02014-07-27 12:29:07 +02001496 msg.msg_control = (__force void *) optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001497 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001498 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001499
1500 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1501 struct in_pktinfo info;
1502
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001503 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1504 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001505 info.ipi_ifindex = inet->mc_index;
1506 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1507 }
1508 if (inet->cmsg_flags & IP_CMSG_TTL) {
1509 int hlim = inet->mc_ttl;
1510 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1511 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001512 if (inet->cmsg_flags & IP_CMSG_TOS) {
1513 int tos = inet->rcv_tos;
1514 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1515 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001516 len -= msg.msg_controllen;
1517 return put_user(len, optlen);
1518 }
1519 case IP_FREEBIND:
1520 val = inet->freebind;
1521 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001522 case IP_TRANSPARENT:
1523 val = inet->transparent;
1524 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001525 case IP_MINTTL:
1526 val = inet->min_ttl;
1527 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001528 default:
1529 release_sock(sk);
1530 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001533
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001534 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 unsigned char ucval = (unsigned char)val;
1536 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001537 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001539 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return -EFAULT;
1541 } else {
1542 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001543 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001545 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return -EFAULT;
1547 }
1548 return 0;
WANG Cong87e9f032015-11-03 15:41:16 -08001549
1550out:
1551 release_sock(sk);
1552 if (needs_rtnl)
1553 rtnl_unlock();
1554 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001557int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001558 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001559{
1560 int err;
1561
Daniel Balutadd23198e2011-08-07 22:31:07 -07001562 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001563#ifdef CONFIG_BPFILTER
1564 if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1565 optname < BPFILTER_IPT_GET_MAX)
1566 err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1567#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001568#ifdef CONFIG_NETFILTER
1569 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001570 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1571 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001572 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001573
Jianjun Kong09cb1052008-11-03 00:27:11 -08001574 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001575 return -EFAULT;
1576
Paolo Abeni01ea3062018-02-08 12:19:00 +01001577 err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001578 if (err >= 0)
1579 err = put_user(len, optlen);
1580 return err;
1581 }
1582#endif
1583 return err;
1584}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001585EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001586
1587#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001588int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1589 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001590{
David L Stevens42908c62008-04-29 03:23:22 -07001591 int err;
1592
1593 if (optname == MCAST_MSFILTER)
1594 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1595 ip_getsockopt);
1596
Daniel Balutadd23198e2011-08-07 22:31:07 -07001597 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1598 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001599
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001600#ifdef CONFIG_BPFILTER
1601 if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1602 optname < BPFILTER_IPT_GET_MAX)
1603 err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1604#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001605#ifdef CONFIG_NETFILTER
1606 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001607 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1608 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001609 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001610
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001611 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001612 return -EFAULT;
1613
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001614 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001615 if (err >= 0)
1616 err = put_user(len, optlen);
1617 return err;
1618 }
1619#endif
1620 return err;
1621}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001622EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001623#endif