blob: dcbf6afe27e7266788ea6351f7c607c19c60788f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * The IP to API glue.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Authors: see ip.c
10 *
11 * Fixes:
12 * Many : Split from ip.c , see ip.c for history.
13 * Martin Mares : TOS setting fixed.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090014 * Alan Cox : Fixed a couple of oopses in Martin's
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * TOS tweaks.
16 * Mike McLagan : Routing by source
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/types.h>
21#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/skbuff.h>
23#include <linux/ip.h>
24#include <linux/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020025#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/sock.h>
29#include <net/ip.h>
30#include <net/icmp.h>
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -080031#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/udp.h>
33#include <linux/igmp.h>
34#include <linux/netfilter.h>
35#include <linux/route.h>
36#include <linux/mroute.h>
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -040037#include <net/inet_ecn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <net/route.h>
39#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070040#include <net/compat.h>
Tom Herbertad6f9392015-01-05 13:56:17 -080041#include <net/checksum.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000042#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <net/transp_v6.h>
44#endif
David S. Miller35ebf652012-06-28 03:59:11 -070045#include <net/ip_fib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <linux/errqueue.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080048#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * SOL_IP control messages.
52 */
53
54static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
55{
Eric Dumazetd826eb12011-11-09 07:24:35 +000056 struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070058 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
61}
62
63static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
64{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070065 int ttl = ip_hdr(skb)->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
67}
68
69static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
70{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070071 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
75{
76 if (IPCB(skb)->opt.optlen == 0)
77 return;
78
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070079 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
80 ip_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83
Paolo Abeni91ed1e62017-08-03 18:07:06 +020084static void ip_cmsg_recv_retopts(struct net *net, struct msghdr *msg,
85 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 unsigned char optbuf[sizeof(struct ip_options) + 40];
Daniel Baluta5e73ea12012-04-15 01:34:41 +000088 struct ip_options *opt = (struct ip_options *)optbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 if (IPCB(skb)->opt.optlen == 0)
91 return;
92
Paolo Abeni91ed1e62017-08-03 18:07:06 +020093 if (ip_options_echo(net, opt, skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 msg->msg_flags |= MSG_CTRUNC;
95 return;
96 }
97 ip_options_undo(opt);
98
99 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
100}
101
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400102static void ip_cmsg_recv_fragsize(struct msghdr *msg, struct sk_buff *skb)
103{
104 int val;
105
106 if (IPCB(skb)->frag_max_size == 0)
107 return;
108
109 val = IPCB(skb)->frag_max_size;
110 put_cmsg(msg, SOL_IP, IP_RECVFRAGSIZE, sizeof(val), &val);
111}
112
Tom Herbertad6f9392015-01-05 13:56:17 -0800113static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb,
Eric Dumazet10df8e62016-10-23 18:03:06 -0700114 int tlen, int offset)
Tom Herbertad6f9392015-01-05 13:56:17 -0800115{
116 __wsum csum = skb->csum;
117
118 if (skb->ip_summed != CHECKSUM_COMPLETE)
119 return;
120
Paolo Abenica4ef452017-02-21 09:33:18 +0100121 if (offset != 0) {
122 int tend_off = skb_transport_offset(skb) + tlen;
123 csum = csum_sub(csum, skb_checksum(skb, tend_off, offset, 0));
124 }
Tom Herbertad6f9392015-01-05 13:56:17 -0800125
126 put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum);
127}
128
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800129static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
130{
131 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700132 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800133 int err;
134
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700135 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
136 if (err)
137 return;
138
139 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800140 if (err)
141 return;
142
143 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700144 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800145}
146
Harvey Harrison21d1a162008-11-20 01:54:27 -0800147static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800148{
149 struct sockaddr_in sin;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000150 const struct iphdr *iph = ip_hdr(skb);
Harvey Harrison21d1a162008-11-20 01:54:27 -0800151 __be16 *ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800152
Willem de Bruijn39b2dd72016-12-22 18:19:16 -0500153 if (skb_transport_offset(skb) + 4 > (int)skb->len)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800154 return;
155
156 /* All current transport protocols have the port numbers in the
157 * first four bytes of the transport header and this function is
158 * written with this assumption in mind.
159 */
160
161 sin.sin_family = AF_INET;
162 sin.sin_addr.s_addr = iph->daddr;
163 sin.sin_port = ports[1];
164 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
165
166 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
167}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Paolo Abeniad959032016-11-04 11:28:58 +0100169void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk,
170 struct sk_buff *skb, int tlen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Paolo Abeniad959032016-11-04 11:28:58 +0100172 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000173 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /* Ordered by supposed usage frequency */
Tom Herbertc44d13d2015-01-05 13:56:15 -0800176 if (flags & IP_CMSG_PKTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 ip_cmsg_recv_pktinfo(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Tom Herbertc44d13d2015-01-05 13:56:15 -0800179 flags &= ~IP_CMSG_PKTINFO;
180 if (!flags)
181 return;
182 }
183
184 if (flags & IP_CMSG_TTL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 ip_cmsg_recv_ttl(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Tom Herbertc44d13d2015-01-05 13:56:15 -0800187 flags &= ~IP_CMSG_TTL;
188 if (!flags)
189 return;
190 }
191
192 if (flags & IP_CMSG_TOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 ip_cmsg_recv_tos(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Tom Herbertc44d13d2015-01-05 13:56:15 -0800195 flags &= ~IP_CMSG_TOS;
196 if (!flags)
197 return;
198 }
199
200 if (flags & IP_CMSG_RECVOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 ip_cmsg_recv_opts(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Tom Herbertc44d13d2015-01-05 13:56:15 -0800203 flags &= ~IP_CMSG_RECVOPTS;
204 if (!flags)
205 return;
206 }
207
208 if (flags & IP_CMSG_RETOPTS) {
Paolo Abeni91ed1e62017-08-03 18:07:06 +0200209 ip_cmsg_recv_retopts(sock_net(sk), msg, skb);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800210
Tom Herbertc44d13d2015-01-05 13:56:15 -0800211 flags &= ~IP_CMSG_RETOPTS;
212 if (!flags)
213 return;
214 }
215
216 if (flags & IP_CMSG_PASSSEC) {
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800217 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800218
Tom Herbertc44d13d2015-01-05 13:56:15 -0800219 flags &= ~IP_CMSG_PASSSEC;
220 if (!flags)
221 return;
222 }
223
Tom Herbertad6f9392015-01-05 13:56:17 -0800224 if (flags & IP_CMSG_ORIGDSTADDR) {
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800225 ip_cmsg_recv_dstaddr(msg, skb);
226
Tom Herbertad6f9392015-01-05 13:56:17 -0800227 flags &= ~IP_CMSG_ORIGDSTADDR;
228 if (!flags)
229 return;
230 }
231
232 if (flags & IP_CMSG_CHECKSUM)
Eric Dumazet10df8e62016-10-23 18:03:06 -0700233 ip_cmsg_recv_checksum(msg, skb, tlen, offset);
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400234
235 if (flags & IP_CMSG_RECVFRAGSIZE)
236 ip_cmsg_recv_fragsize(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
Tom Herbert5961de92015-01-05 13:56:16 -0800238EXPORT_SYMBOL(ip_cmsg_recv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400240int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100241 bool allow_ipv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Francesco Fuscof02db312013-09-24 15:43:08 +0200243 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 struct cmsghdr *cmsg;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400245 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Gu Zhengf95b4142014-12-11 11:22:04 +0800247 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (!CMSG_OK(msg, cmsg))
249 return -EINVAL;
Eric Dumazet5337b5b2014-11-10 17:54:25 -0800250#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100251 if (allow_ipv6 &&
252 cmsg->cmsg_level == SOL_IPV6 &&
253 cmsg->cmsg_type == IPV6_PKTINFO) {
254 struct in6_pktinfo *src_info;
255
256 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
257 return -EINVAL;
258 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
259 if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
260 return -EINVAL;
David Ahern1cbec072018-02-16 11:03:03 -0800261 if (src_info->ipi6_ifindex)
262 ipc->oif = src_info->ipi6_ifindex;
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100263 ipc->addr = src_info->ipi6_addr.s6_addr32[3];
264 continue;
265 }
266#endif
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400267 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700268 err = __sock_cmsg_send(sk, msg, cmsg, &ipc->sockc);
269 if (err)
270 return err;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400271 continue;
272 }
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (cmsg->cmsg_level != SOL_IP)
275 continue;
276 switch (cmsg->cmsg_type) {
277 case IP_RETOPTS:
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800278 err = cmsg->cmsg_len - sizeof(struct cmsghdr);
Eric Dumazet91948302016-02-04 06:23:28 -0800279
280 /* Our caller is responsible for freeing ipc->opt */
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700281 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
282 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (err)
284 return err;
285 break;
286 case IP_PKTINFO:
287 {
288 struct in_pktinfo *info;
289 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
290 return -EINVAL;
291 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
David Ahern1cbec072018-02-16 11:03:03 -0800292 if (info->ipi_ifindex)
293 ipc->oif = info->ipi_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 ipc->addr = info->ipi_spec_dst.s_addr;
295 break;
296 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200297 case IP_TTL:
298 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
299 return -EINVAL;
300 val = *(int *)CMSG_DATA(cmsg);
301 if (val < 1 || val > 255)
302 return -EINVAL;
303 ipc->ttl = val;
304 break;
305 case IP_TOS:
Eric Dumazete895cdc2016-09-07 21:52:56 -0700306 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)))
307 val = *(int *)CMSG_DATA(cmsg);
308 else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8)))
309 val = *(u8 *)CMSG_DATA(cmsg);
310 else
Francesco Fuscof02db312013-09-24 15:43:08 +0200311 return -EINVAL;
Francesco Fuscof02db312013-09-24 15:43:08 +0200312 if (val < 0 || val > 255)
313 return -EINVAL;
314 ipc->tos = val;
315 ipc->priority = rt_tos2priority(ipc->tos);
316 break;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 default:
319 return -EINVAL;
320 }
321 }
322 return 0;
323}
324
325
326/* Special input handler for packets caught by router alert option.
327 They are selected only by protocol field, and then processed likely
328 local ones; but only if someone wants them! Otherwise, router
329 not running rsvpd will kill RSVP.
330
331 It is user level problem, what it will make with them.
332 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
333 but receiver should be enough clever f.e. to forward mtrace requests,
334 sent to multicast group to reach destination designated router.
335 */
Eric Dumazet43a951e2010-10-25 03:32:44 +0000336struct ip_ra_chain __rcu *ip_ra_chain;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300337static DEFINE_SPINLOCK(ip_ra_lock);
Eric Dumazet66018502010-06-07 03:12:08 +0000338
Eric Dumazet592fcb92010-06-09 16:21:07 +0000339
340static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000341{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000342 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
343
344 sock_put(ra->saved_sk);
345 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000346}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700348int ip_ra_control(struct sock *sk, unsigned char on,
349 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000351 struct ip_ra_chain *ra, *new_ra;
352 struct ip_ra_chain __rcu **rap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000354 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return -EINVAL;
356
357 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
358
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300359 spin_lock_bh(&ip_ra_lock);
Eric Dumazet43a951e2010-10-25 03:32:44 +0000360 for (rap = &ip_ra_chain;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300361 (ra = rcu_dereference_protected(*rap,
362 lockdep_is_held(&ip_ra_lock))) != NULL;
Eric Dumazet43a951e2010-10-25 03:32:44 +0000363 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (ra->sk == sk) {
365 if (on) {
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300366 spin_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800367 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return -EADDRINUSE;
369 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000370 /* dont let ip_call_ra_chain() use sk again */
371 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700372 RCU_INIT_POINTER(*rap, ra->next);
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300373 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 if (ra->destructor)
376 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000377 /*
378 * Delay sock_put(sk) and kfree(ra) after one rcu grace
379 * period. This guarantee ip_call_ra_chain() dont need
380 * to mess with socket refcounts.
381 */
382 ra->saved_sk = sk;
383 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return 0;
385 }
386 }
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300387 if (!new_ra) {
388 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return -ENOBUFS;
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 new_ra->sk = sk;
392 new_ra->destructor = destructor;
393
Eric Dumazet8e380f02014-09-09 08:11:41 -0700394 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000395 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 sock_hold(sk);
Kirill Tkhai76d3e152018-03-22 12:45:02 +0300397 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return 0;
400}
401
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900402void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700403 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct sock_exterr_skb *serr;
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 skb = skb_clone(skb, GFP_ATOMIC);
408 if (!skb)
409 return;
410
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900411 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 serr->ee.ee_errno = err;
413 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300414 serr->ee.ee_type = icmp_hdr(skb)->type;
415 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 serr->ee.ee_pad = 0;
417 serr->ee.ee_info = info;
418 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300419 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700420 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 serr->port = port;
422
Ian Morris00db4122015-04-03 09:17:27 +0100423 if (skb_pull(skb, payload - skb->data)) {
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300424 skb_reset_transport_header(skb);
425 if (sock_queue_err_skb(sk, skb) == 0)
426 return;
427 }
428 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Al Viro05790162006-09-27 18:33:40 -0700431void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 struct inet_sock *inet = inet_sk(sk);
434 struct sock_exterr_skb *serr;
435 struct iphdr *iph;
436 struct sk_buff *skb;
437
438 if (!inet->recverr)
439 return;
440
441 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
442 if (!skb)
443 return;
444
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300445 skb_put(skb, sizeof(struct iphdr));
446 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700447 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 iph->daddr = daddr;
449
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900450 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 serr->ee.ee_errno = err;
452 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900453 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 serr->ee.ee_code = 0;
455 serr->ee.ee_pad = 0;
456 serr->ee.ee_info = info;
457 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700458 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 serr->port = port;
460
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700461 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300462 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 if (sock_queue_err_skb(sk, skb))
465 kfree_skb(skb);
466}
467
Julian Anastasov34b99df2015-06-23 08:34:39 +0300468/* For some errors we have valid addr_offset even with zero payload and
469 * zero port. Also, addr_offset should be supported if port is set.
470 */
471static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
472{
473 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
474 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
475}
476
Willem de Bruijnc247f052015-03-07 20:33:22 -0500477/* IPv4 supports cmsg on all imcp errors and some timestamps
478 *
479 * Timestamp code paths do not initialize the fields expected by cmsg:
480 * the PKTINFO fields in skb->cb[]. Fill those in here.
481 */
482static bool ipv4_datagram_support_cmsg(const struct sock *sk,
483 struct sk_buff *skb,
484 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500485{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500486 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500487
Willem de Bruijnc247f052015-03-07 20:33:22 -0500488 if (ee_origin == SO_EE_ORIGIN_ICMP)
489 return true;
490
491 if (ee_origin == SO_EE_ORIGIN_LOCAL)
492 return false;
493
494 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
Willem de Bruijn1862d622017-04-12 19:24:35 -0400495 * timestamp with egress dev. Not possible for packets without iif
Willem de Bruijnc247f052015-03-07 20:33:22 -0500496 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
497 */
Willem de Bruijn1862d622017-04-12 19:24:35 -0400498 info = PKTINFO_SKB_CB(skb);
499 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
500 !info->ipi_ifindex)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500501 return false;
502
503 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500504 return true;
505}
506
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900507/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * Handle MSG_ERRQUEUE
509 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100510int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400513 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100514 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct {
516 struct sock_extended_err ee;
517 struct sockaddr_in offender;
518 } errhdr;
519 int err;
520 int copied;
521
Willem de Bruijn7ce875e2014-11-30 22:22:33 -0500522 WARN_ON_ONCE(sk->sk_family == AF_INET6);
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400525 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100526 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 goto out;
528
529 copied = skb->len;
530 if (copied > len) {
531 msg->msg_flags |= MSG_TRUNC;
532 copied = len;
533 }
David S. Miller51f3d022014-11-05 16:46:40 -0500534 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700535 if (unlikely(err)) {
536 kfree_skb(skb);
537 return err;
538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 sock_recv_timestamp(msg, sk, skb);
540
541 serr = SKB_EXT_ERR(skb);
542
Julian Anastasov34b99df2015-06-23 08:34:39 +0300543 if (sin && ipv4_datagram_support_addr(serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700545 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
546 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 sin->sin_port = serr->port;
548 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100549 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551
552 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
553 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500554 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500555
Willem de Bruijnc247f052015-03-07 20:33:22 -0500556 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700558 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500559 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 ip_cmsg_recv(msg, skb);
561 }
562
563 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
564
565 /* Now we could try to dump offended packet options */
566
567 msg->msg_flags |= MSG_ERRQUEUE;
568 err = copied;
569
Eric Dumazet960a2622016-04-21 22:27:32 -0700570 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571out:
572 return err;
573}
574
575
576/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700577 * Socket option code for IP. This is the end of the line after any
578 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300580static bool setsockopt_needs_rtnl(int optname)
581{
582 switch (optname) {
583 case IP_ADD_MEMBERSHIP:
584 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300585 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300586 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300587 case IP_DROP_SOURCE_MEMBERSHIP:
588 case IP_MSFILTER:
589 case IP_UNBLOCK_SOURCE:
590 case MCAST_BLOCK_SOURCE:
591 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300592 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300593 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300594 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300595 case MCAST_LEAVE_SOURCE_GROUP:
596 case MCAST_UNBLOCK_SOURCE:
WANG Cong1215e512017-04-12 12:32:13 -0700597 case IP_ROUTER_ALERT:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300598 return true;
599 }
600 return false;
601}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800603static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700604 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct inet_sock *inet = inet_sk(sk);
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200607 struct net *net = sock_net(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800608 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300609 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Xi Wang0c9f79b2012-11-11 11:20:01 +0000611 switch (optname) {
612 case IP_PKTINFO:
613 case IP_RECVTTL:
614 case IP_RECVOPTS:
615 case IP_RECVTOS:
616 case IP_RETOPTS:
617 case IP_TOS:
618 case IP_TTL:
619 case IP_HDRINCL:
620 case IP_MTU_DISCOVER:
621 case IP_RECVERR:
622 case IP_ROUTER_ALERT:
623 case IP_FREEBIND:
624 case IP_PASSSEC:
625 case IP_TRANSPARENT:
626 case IP_MINTTL:
627 case IP_NODEFRAG:
Eric Dumazet90c337d2015-06-06 21:17:57 -0700628 case IP_BIND_ADDRESS_NO_PORT:
Xi Wang0c9f79b2012-11-11 11:20:01 +0000629 case IP_UNICAST_IF:
630 case IP_MULTICAST_TTL:
631 case IP_MULTICAST_ALL:
632 case IP_MULTICAST_LOOP:
633 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800634 case IP_CHECKSUM:
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400635 case IP_RECVFRAGSIZE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (optlen >= sizeof(int)) {
637 if (get_user(val, (int __user *) optval))
638 return -EFAULT;
639 } else if (optlen >= sizeof(char)) {
640 unsigned char ucval;
641
642 if (get_user(ucval, (unsigned char __user *) optval))
643 return -EFAULT;
644 val = (int) ucval;
645 }
646 }
647
648 /* If optlen==0, it is equivalent to val == 0 */
649
Kirill Tkhai05269472018-03-22 12:45:12 +0300650 if (optname == IP_ROUTER_ALERT)
651 return ip_ra_control(sk, val ? 1 : 0, NULL);
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800652 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800653 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300656 if (needs_rtnl)
657 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 lock_sock(sk);
659
660 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800661 case IP_OPTIONS:
662 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000663 struct ip_options_rcu *old, *opt = NULL;
664
roel kluin65a1c4f2009-10-23 05:59:21 +0000665 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800666 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900667 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700668 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800669 if (err)
670 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000671 old = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200672 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800673 if (inet->is_icsk) {
674 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000675#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800676 if (sk->sk_family == PF_INET ||
677 (!((1 << sk->sk_state) &
678 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000679 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000681 if (old)
682 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800683 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000684 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800685 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000686#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000690 rcu_assign_pointer(inet->inet_opt, opt);
691 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800692 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800693 break;
694 }
695 case IP_PKTINFO:
696 if (val)
697 inet->cmsg_flags |= IP_CMSG_PKTINFO;
698 else
699 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
700 break;
701 case IP_RECVTTL:
702 if (val)
703 inet->cmsg_flags |= IP_CMSG_TTL;
704 else
705 inet->cmsg_flags &= ~IP_CMSG_TTL;
706 break;
707 case IP_RECVTOS:
708 if (val)
709 inet->cmsg_flags |= IP_CMSG_TOS;
710 else
711 inet->cmsg_flags &= ~IP_CMSG_TOS;
712 break;
713 case IP_RECVOPTS:
714 if (val)
715 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
716 else
717 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
718 break;
719 case IP_RETOPTS:
720 if (val)
721 inet->cmsg_flags |= IP_CMSG_RETOPTS;
722 else
723 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
724 break;
725 case IP_PASSSEC:
726 if (val)
727 inet->cmsg_flags |= IP_CMSG_PASSSEC;
728 else
729 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
730 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800731 case IP_RECVORIGDSTADDR:
732 if (val)
733 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
734 else
735 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
736 break;
Tom Herbertad6f9392015-01-05 13:56:17 -0800737 case IP_CHECKSUM:
738 if (val) {
739 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
740 inet_inc_convert_csum(sk);
741 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
742 }
743 } else {
744 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
745 inet_dec_convert_csum(sk);
746 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
747 }
748 }
749 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400750 case IP_RECVFRAGSIZE:
751 if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM)
752 goto e_inval;
753 if (val)
754 inet->cmsg_flags |= IP_CMSG_RECVFRAGSIZE;
755 else
756 inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE;
757 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800758 case IP_TOS: /* This sets both TOS and Precedence */
759 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400760 val &= ~INET_ECN_MASK;
761 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800763 if (inet->tos != val) {
764 inet->tos = val;
765 sk->sk_priority = rt_tos2priority(val);
766 sk_dst_reset(sk);
767 }
768 break;
769 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700770 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800771 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000772 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800773 goto e_inval;
774 inet->uc_ttl = val;
775 break;
776 case IP_HDRINCL:
777 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800780 }
781 inet->hdrincl = val ? 1 : 0;
782 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000783 case IP_NODEFRAG:
784 if (sk->sk_type != SOCK_RAW) {
785 err = -ENOPROTOOPT;
786 break;
787 }
788 inet->nodefrag = val ? 1 : 0;
789 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -0700790 case IP_BIND_ADDRESS_NO_PORT:
791 inet->bind_address_no_port = val ? 1 : 0;
792 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800793 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100794 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800795 goto e_inval;
796 inet->pmtudisc = val;
797 break;
798 case IP_RECVERR:
799 inet->recverr = !!val;
800 if (!val)
801 skb_queue_purge(&sk->sk_error_queue);
802 break;
803 case IP_MULTICAST_TTL:
804 if (sk->sk_type == SOCK_STREAM)
805 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700806 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800807 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800808 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800809 val = 1;
810 if (val < 0 || val > 255)
811 goto e_inval;
812 inet->mc_ttl = val;
813 break;
814 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700815 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800816 goto e_inval;
817 inet->mc_loop = !!val;
818 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000819 case IP_UNICAST_IF:
820 {
821 struct net_device *dev = NULL;
822 int ifindex;
David Ahern9515a2e2018-01-24 19:37:38 -0800823 int midx;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000824
825 if (optlen != sizeof(int))
826 goto e_inval;
827
828 ifindex = (__force int)ntohl((__force __be32)val);
829 if (ifindex == 0) {
830 inet->uc_index = 0;
831 err = 0;
832 break;
833 }
834
835 dev = dev_get_by_index(sock_net(sk), ifindex);
836 err = -EADDRNOTAVAIL;
837 if (!dev)
838 break;
David Ahern9515a2e2018-01-24 19:37:38 -0800839
840 midx = l3mdev_master_ifindex(dev);
Erich E. Hoover76e21052012-02-08 09:11:07 +0000841 dev_put(dev);
842
843 err = -EINVAL;
David Ahern9515a2e2018-01-24 19:37:38 -0800844 if (sk->sk_bound_dev_if &&
845 (!midx || midx != sk->sk_bound_dev_if))
Erich E. Hoover76e21052012-02-08 09:11:07 +0000846 break;
847
848 inet->uc_index = ifindex;
849 err = 0;
850 break;
851 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800852 case IP_MULTICAST_IF:
853 {
854 struct ip_mreqn mreq;
855 struct net_device *dev = NULL;
David Ahern7bb387c2016-12-29 15:39:37 -0800856 int midx;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800857
858 if (sk->sk_type == SOCK_STREAM)
859 goto e_inval;
860 /*
861 * Check the arguments are allowable
862 */
863
Shan Wei09159212009-09-22 15:41:10 +0000864 if (optlen < sizeof(struct in_addr))
865 goto e_inval;
866
Stephen Hemminger132adf52007-03-08 20:44:43 -0800867 err = -EFAULT;
868 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800869 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800870 break;
871 } else {
872 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000873 if (optlen >= sizeof(struct ip_mreq)) {
874 if (copy_from_user(&mreq, optval,
875 sizeof(struct ip_mreq)))
876 break;
877 } else if (optlen >= sizeof(struct in_addr)) {
878 if (copy_from_user(&mreq.imr_address, optval,
879 sizeof(struct in_addr)))
880 break;
881 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800882 }
883
884 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700885 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800886 inet->mc_index = 0;
887 inet->mc_addr = 0;
888 err = 0;
889 break;
890 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900891 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000892 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800893 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800894 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000895 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800896
897
898 err = -EADDRNOTAVAIL;
899 if (!dev)
900 break;
David Ahern7bb387c2016-12-29 15:39:37 -0800901
902 midx = l3mdev_master_ifindex(dev);
903
Eric Dumazet55b80502009-10-19 06:41:58 +0000904 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800905
906 err = -EINVAL;
907 if (sk->sk_bound_dev_if &&
David Ahern7bb387c2016-12-29 15:39:37 -0800908 mreq.imr_ifindex != sk->sk_bound_dev_if &&
909 (!midx || midx != sk->sk_bound_dev_if))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800910 break;
911
912 inet->mc_index = mreq.imr_ifindex;
913 inet->mc_addr = mreq.imr_address.s_addr;
914 err = 0;
915 break;
916 }
917
918 case IP_ADD_MEMBERSHIP:
919 case IP_DROP_MEMBERSHIP:
920 {
921 struct ip_mreqn mreq;
922
Flavio Leitnera96fb492007-08-24 22:16:39 -0700923 err = -EPROTO;
924 if (inet_sk(sk)->is_icsk)
925 break;
926
Stephen Hemminger132adf52007-03-08 20:44:43 -0800927 if (optlen < sizeof(struct ip_mreq))
928 goto e_inval;
929 err = -EFAULT;
930 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800931 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800932 break;
933 } else {
934 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800935 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800936 break;
937 }
938
939 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300940 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800941 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300942 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800943 break;
944 }
945 case IP_MSFILTER:
946 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800947 struct ip_msfilter *msf;
948
949 if (optlen < IP_MSFILTER_SIZE(0))
950 goto e_inval;
951 if (optlen > sysctl_optmem_max) {
952 err = -ENOBUFS;
953 break;
954 }
Al Viroa2c841d2017-05-13 18:26:06 -0400955 msf = memdup_user(optval, optlen);
956 if (IS_ERR(msf)) {
957 err = PTR_ERR(msf);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800958 break;
959 }
960 /* numsrc >= (1G-4) overflow in 32 bits */
961 if (msf->imsf_numsrc >= 0x3ffffffcU ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200962 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800963 kfree(msf);
964 err = -ENOBUFS;
965 break;
966 }
967 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
968 kfree(msf);
969 err = -EINVAL;
970 break;
971 }
972 err = ip_mc_msfilter(sk, msf, 0);
973 kfree(msf);
974 break;
975 }
976 case IP_BLOCK_SOURCE:
977 case IP_UNBLOCK_SOURCE:
978 case IP_ADD_SOURCE_MEMBERSHIP:
979 case IP_DROP_SOURCE_MEMBERSHIP:
980 {
981 struct ip_mreq_source mreqs;
982 int omode, add;
983
984 if (optlen != sizeof(struct ip_mreq_source))
985 goto e_inval;
986 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
987 err = -EFAULT;
988 break;
989 }
990 if (optname == IP_BLOCK_SOURCE) {
991 omode = MCAST_EXCLUDE;
992 add = 1;
993 } else if (optname == IP_UNBLOCK_SOURCE) {
994 omode = MCAST_EXCLUDE;
995 add = 0;
996 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
997 struct ip_mreqn mreq;
998
999 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
1000 mreq.imr_address.s_addr = mreqs.imr_interface;
1001 mreq.imr_ifindex = 0;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001002 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001003 if (err && err != -EADDRINUSE)
1004 break;
1005 omode = MCAST_INCLUDE;
1006 add = 1;
1007 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
1008 omode = MCAST_INCLUDE;
1009 add = 0;
1010 }
1011 err = ip_mc_source(add, omode, sk, &mreqs, 0);
1012 break;
1013 }
1014 case MCAST_JOIN_GROUP:
1015 case MCAST_LEAVE_GROUP:
1016 {
1017 struct group_req greq;
1018 struct sockaddr_in *psin;
1019 struct ip_mreqn mreq;
1020
1021 if (optlen < sizeof(struct group_req))
1022 goto e_inval;
1023 err = -EFAULT;
1024 if (copy_from_user(&greq, optval, sizeof(greq)))
1025 break;
1026 psin = (struct sockaddr_in *)&greq.gr_group;
1027 if (psin->sin_family != AF_INET)
1028 goto e_inval;
1029 memset(&mreq, 0, sizeof(mreq));
1030 mreq.imr_multiaddr = psin->sin_addr;
1031 mreq.imr_ifindex = greq.gr_interface;
1032
1033 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001034 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001035 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001036 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001037 break;
1038 }
1039 case MCAST_JOIN_SOURCE_GROUP:
1040 case MCAST_LEAVE_SOURCE_GROUP:
1041 case MCAST_BLOCK_SOURCE:
1042 case MCAST_UNBLOCK_SOURCE:
1043 {
1044 struct group_source_req greqs;
1045 struct ip_mreq_source mreqs;
1046 struct sockaddr_in *psin;
1047 int omode, add;
1048
1049 if (optlen != sizeof(struct group_source_req))
1050 goto e_inval;
1051 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
1052 err = -EFAULT;
1053 break;
1054 }
1055 if (greqs.gsr_group.ss_family != AF_INET ||
1056 greqs.gsr_source.ss_family != AF_INET) {
1057 err = -EADDRNOTAVAIL;
1058 break;
1059 }
1060 psin = (struct sockaddr_in *)&greqs.gsr_group;
1061 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
1062 psin = (struct sockaddr_in *)&greqs.gsr_source;
1063 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
1064 mreqs.imr_interface = 0; /* use index for mc_source */
1065
1066 if (optname == MCAST_BLOCK_SOURCE) {
1067 omode = MCAST_EXCLUDE;
1068 add = 1;
1069 } else if (optname == MCAST_UNBLOCK_SOURCE) {
1070 omode = MCAST_EXCLUDE;
1071 add = 0;
1072 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
1073 struct ip_mreqn mreq;
1074
1075 psin = (struct sockaddr_in *)&greqs.gsr_group;
1076 mreq.imr_multiaddr = psin->sin_addr;
1077 mreq.imr_address.s_addr = 0;
1078 mreq.imr_ifindex = greqs.gsr_interface;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001079 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001080 if (err && err != -EADDRINUSE)
1081 break;
1082 greqs.gsr_interface = mreq.imr_ifindex;
1083 omode = MCAST_INCLUDE;
1084 add = 1;
1085 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1086 omode = MCAST_INCLUDE;
1087 add = 0;
1088 }
1089 err = ip_mc_source(add, omode, sk, &mreqs,
1090 greqs.gsr_interface);
1091 break;
1092 }
1093 case MCAST_MSFILTER:
1094 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001095 struct sockaddr_in *psin;
1096 struct ip_msfilter *msf = NULL;
1097 struct group_filter *gsf = NULL;
1098 int msize, i, ifindex;
1099
1100 if (optlen < GROUP_FILTER_SIZE(0))
1101 goto e_inval;
1102 if (optlen > sysctl_optmem_max) {
1103 err = -ENOBUFS;
1104 break;
1105 }
Al Viroa2c841d2017-05-13 18:26:06 -04001106 gsf = memdup_user(optval, optlen);
1107 if (IS_ERR(gsf)) {
1108 err = PTR_ERR(gsf);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001109 break;
1110 }
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001111
Stephen Hemminger132adf52007-03-08 20:44:43 -08001112 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1113 if (gsf->gf_numsrc >= 0x1ffffff ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +02001114 gsf->gf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001115 err = -ENOBUFS;
1116 goto mc_msf_out;
1117 }
1118 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
1119 err = -EINVAL;
1120 goto mc_msf_out;
1121 }
1122 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -08001123 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001124 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001125 err = -ENOBUFS;
1126 goto mc_msf_out;
1127 }
1128 ifindex = gsf->gf_interface;
1129 psin = (struct sockaddr_in *)&gsf->gf_group;
1130 if (psin->sin_family != AF_INET) {
1131 err = -EADDRNOTAVAIL;
1132 goto mc_msf_out;
1133 }
1134 msf->imsf_multiaddr = psin->sin_addr.s_addr;
1135 msf->imsf_interface = 0;
1136 msf->imsf_fmode = gsf->gf_fmode;
1137 msf->imsf_numsrc = gsf->gf_numsrc;
1138 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001139 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001140 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
1141
1142 if (psin->sin_family != AF_INET)
1143 goto mc_msf_out;
1144 msf->imsf_slist[i] = psin->sin_addr.s_addr;
1145 }
1146 kfree(gsf);
1147 gsf = NULL;
1148
1149 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001150mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -08001151 kfree(msf);
1152 kfree(gsf);
1153 break;
1154 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001155 case IP_MULTICAST_ALL:
1156 if (optlen < 1)
1157 goto e_inval;
1158 if (val != 0 && val != 1)
1159 goto e_inval;
1160 inet->mc_all = val;
1161 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001162
1163 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001164 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001165 goto e_inval;
1166 inet->freebind = !!val;
1167 break;
1168
1169 case IP_IPSEC_POLICY:
1170 case IP_XFRM_POLICY:
1171 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001172 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001173 break;
1174 err = xfrm_user_policy(sk, optname, optval, optlen);
1175 break;
1176
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001177 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001178 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1179 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001180 err = -EPERM;
1181 break;
1182 }
1183 if (optlen < 1)
1184 goto e_inval;
1185 inet->transparent = !!val;
1186 break;
1187
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001188 case IP_MINTTL:
1189 if (optlen < 1)
1190 goto e_inval;
1191 if (val < 0 || val > 255)
1192 goto e_inval;
1193 inet->min_ttl = val;
1194 break;
1195
Stephen Hemminger132adf52007-03-08 20:44:43 -08001196 default:
1197 err = -ENOPROTOOPT;
1198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001201 if (needs_rtnl)
1202 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return err;
1204
1205e_inval:
1206 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001207 if (needs_rtnl)
1208 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return -EINVAL;
1210}
1211
Eric Dumazetf84af322010-04-28 15:31:51 -07001212/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001213 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001214 * @sk: socket
1215 * @skb: buffer
1216 *
David S. Miller35ebf652012-06-28 03:59:11 -07001217 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1218 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001219 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001220 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001221void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001222{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001223 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001224 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1225 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001226
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001227 if (prepare && skb_rtable(skb)) {
David Ahern0b922b72016-05-10 11:19:51 -07001228 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1229 * which has interface index (iif) as the first member of the
1230 * underlying inet{6}_skb_parm struct. This code then overlays
1231 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001232 * element so the iif is picked up from the prior IPCB. If iif
1233 * is the loopback interface, then return the sending interface
1234 * (e.g., process binds socket to eth0 for Tx which is
1235 * redirected to loopback in the rtable/dst).
David Ahern0b922b72016-05-10 11:19:51 -07001236 */
David Aherncbea8f02017-09-13 17:11:37 -07001237 struct rtable *rt = skb_rtable(skb);
1238 bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags);
1239
1240 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001241 pktinfo->ipi_ifindex = inet_iif(skb);
David Aherncbea8f02017-09-13 17:11:37 -07001242 else if (l3slave && rt && rt->rt_iif)
1243 pktinfo->ipi_ifindex = rt->rt_iif;
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001244
David S. Miller35ebf652012-06-28 03:59:11 -07001245 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001246 } else {
1247 pktinfo->ipi_ifindex = 0;
1248 pktinfo->ipi_spec_dst.s_addr = 0;
1249 }
Paolo Abeni61a10302017-08-03 18:07:07 +02001250 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001251}
Eric Dumazetf84af322010-04-28 15:31:51 -07001252
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001253int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001254 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001255{
1256 int err;
1257
1258 if (level != SOL_IP)
1259 return -ENOPROTOOPT;
1260
1261 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1262#ifdef CONFIG_NETFILTER
1263 /* we need to exclude all possible ENOPROTOOPTs except default case */
1264 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001265 optname != IP_IPSEC_POLICY &&
1266 optname != IP_XFRM_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001267 !ip_mroute_opt(optname))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001268 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001269#endif
1270 return err;
1271}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001272EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001273
1274#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001275int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001276 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001277{
1278 int err;
1279
1280 if (level != SOL_IP)
1281 return -ENOPROTOOPT;
1282
David L Stevensdae50292008-04-27 01:06:07 -07001283 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1284 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1285 ip_setsockopt);
1286
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001287 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1288#ifdef CONFIG_NETFILTER
1289 /* we need to exclude all possible ENOPROTOOPTs except default case */
1290 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001291 optname != IP_IPSEC_POLICY &&
1292 optname != IP_XFRM_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001293 !ip_mroute_opt(optname))
1294 err = compat_nf_setsockopt(sk, PF_INET, optname, optval,
1295 optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001296#endif
1297 return err;
1298}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001299EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001300#endif
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001303 * Get the options. Note for future reference. The GET of IP options gets
1304 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 */
1306
WANG Cong87e9f032015-11-03 15:41:16 -08001307static bool getsockopt_needs_rtnl(int optname)
1308{
1309 switch (optname) {
1310 case IP_MSFILTER:
1311 case MCAST_MSFILTER:
1312 return true;
1313 }
1314 return false;
1315}
1316
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001317static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001318 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct inet_sock *inet = inet_sk(sk);
WANG Cong87e9f032015-11-03 15:41:16 -08001321 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1322 int val, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001324
Stephen Hemminger132adf52007-03-08 20:44:43 -08001325 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return -EOPNOTSUPP;
1327
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001328 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001329 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Jianjun Kong09cb1052008-11-03 00:27:11 -08001331 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001333 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001335
WANG Cong87e9f032015-11-03 15:41:16 -08001336 if (needs_rtnl)
1337 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 lock_sock(sk);
1339
Stephen Hemminger132adf52007-03-08 20:44:43 -08001340 switch (optname) {
1341 case IP_OPTIONS:
1342 {
1343 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001344 struct ip_options *opt = (struct ip_options *)optbuf;
1345 struct ip_options_rcu *inet_opt;
1346
1347 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001348 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001349 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001350 if (inet_opt)
1351 memcpy(optbuf, &inet_opt->opt,
1352 sizeof(struct ip_options) +
1353 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001354 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Stephen Hemminger132adf52007-03-08 20:44:43 -08001356 if (opt->optlen == 0)
1357 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Stephen Hemminger132adf52007-03-08 20:44:43 -08001359 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Stephen Hemminger132adf52007-03-08 20:44:43 -08001361 len = min_t(unsigned int, len, opt->optlen);
1362 if (put_user(len, optlen))
1363 return -EFAULT;
1364 if (copy_to_user(optval, opt->__data, len))
1365 return -EFAULT;
1366 return 0;
1367 }
1368 case IP_PKTINFO:
1369 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1370 break;
1371 case IP_RECVTTL:
1372 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1373 break;
1374 case IP_RECVTOS:
1375 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1376 break;
1377 case IP_RECVOPTS:
1378 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1379 break;
1380 case IP_RETOPTS:
1381 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1382 break;
1383 case IP_PASSSEC:
1384 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1385 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001386 case IP_RECVORIGDSTADDR:
1387 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1388 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001389 case IP_CHECKSUM:
1390 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1391 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -04001392 case IP_RECVFRAGSIZE:
1393 val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0;
1394 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001395 case IP_TOS:
1396 val = inet->tos;
1397 break;
1398 case IP_TTL:
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001399 {
1400 struct net *net = sock_net(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001401 val = (inet->uc_ttl == -1 ?
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001402 net->ipv4.sysctl_ip_default_ttl :
Stephen Hemminger132adf52007-03-08 20:44:43 -08001403 inet->uc_ttl);
1404 break;
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001405 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001406 case IP_HDRINCL:
1407 val = inet->hdrincl;
1408 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001409 case IP_NODEFRAG:
1410 val = inet->nodefrag;
1411 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001412 case IP_BIND_ADDRESS_NO_PORT:
1413 val = inet->bind_address_no_port;
1414 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001415 case IP_MTU_DISCOVER:
1416 val = inet->pmtudisc;
1417 break;
1418 case IP_MTU:
1419 {
1420 struct dst_entry *dst;
1421 val = 0;
1422 dst = sk_dst_get(sk);
1423 if (dst) {
1424 val = dst_mtu(dst);
1425 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001427 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001429 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001431 break;
1432 }
1433 case IP_RECVERR:
1434 val = inet->recverr;
1435 break;
1436 case IP_MULTICAST_TTL:
1437 val = inet->mc_ttl;
1438 break;
1439 case IP_MULTICAST_LOOP:
1440 val = inet->mc_loop;
1441 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001442 case IP_UNICAST_IF:
1443 val = (__force int)htonl((__u32) inet->uc_index);
1444 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001445 case IP_MULTICAST_IF:
1446 {
1447 struct in_addr addr;
1448 len = min_t(unsigned int, len, sizeof(struct in_addr));
1449 addr.s_addr = inet->mc_addr;
1450 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Stephen Hemminger132adf52007-03-08 20:44:43 -08001452 if (put_user(len, optlen))
1453 return -EFAULT;
1454 if (copy_to_user(optval, &addr, len))
1455 return -EFAULT;
1456 return 0;
1457 }
1458 case IP_MSFILTER:
1459 {
1460 struct ip_msfilter msf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001461
1462 if (len < IP_MSFILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001463 err = -EINVAL;
1464 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001466 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001467 err = -EFAULT;
1468 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001470 err = ip_mc_msfget(sk, &msf,
1471 (struct ip_msfilter __user *)optval, optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001472 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001473 }
1474 case MCAST_MSFILTER:
1475 {
1476 struct group_filter gsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Stephen Hemminger132adf52007-03-08 20:44:43 -08001478 if (len < GROUP_FILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001479 err = -EINVAL;
1480 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001482 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001483 err = -EFAULT;
1484 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001485 }
1486 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001487 (struct group_filter __user *)optval,
1488 optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001489 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001490 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001491 case IP_MULTICAST_ALL:
1492 val = inet->mc_all;
1493 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001494 case IP_PKTOPTIONS:
1495 {
1496 struct msghdr msg;
1497
1498 release_sock(sk);
1499
1500 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001502
Karoly Kemenyc54a5e02014-07-27 12:29:07 +02001503 msg.msg_control = (__force void *) optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001504 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001505 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001506
1507 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1508 struct in_pktinfo info;
1509
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001510 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1511 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001512 info.ipi_ifindex = inet->mc_index;
1513 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1514 }
1515 if (inet->cmsg_flags & IP_CMSG_TTL) {
1516 int hlim = inet->mc_ttl;
1517 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1518 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001519 if (inet->cmsg_flags & IP_CMSG_TOS) {
1520 int tos = inet->rcv_tos;
1521 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1522 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001523 len -= msg.msg_controllen;
1524 return put_user(len, optlen);
1525 }
1526 case IP_FREEBIND:
1527 val = inet->freebind;
1528 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001529 case IP_TRANSPARENT:
1530 val = inet->transparent;
1531 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001532 case IP_MINTTL:
1533 val = inet->min_ttl;
1534 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001535 default:
1536 release_sock(sk);
1537 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001540
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001541 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 unsigned char ucval = (unsigned char)val;
1543 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001544 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001546 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return -EFAULT;
1548 } else {
1549 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001550 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001552 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return -EFAULT;
1554 }
1555 return 0;
WANG Cong87e9f032015-11-03 15:41:16 -08001556
1557out:
1558 release_sock(sk);
1559 if (needs_rtnl)
1560 rtnl_unlock();
1561 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001564int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001565 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001566{
1567 int err;
1568
Daniel Balutadd23198e2011-08-07 22:31:07 -07001569 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001570#ifdef CONFIG_NETFILTER
1571 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001572 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1573 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001574 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001575
Jianjun Kong09cb1052008-11-03 00:27:11 -08001576 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001577 return -EFAULT;
1578
Paolo Abeni01ea3062018-02-08 12:19:00 +01001579 err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001580 if (err >= 0)
1581 err = put_user(len, optlen);
1582 return err;
1583 }
1584#endif
1585 return err;
1586}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001587EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001588
1589#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001590int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1591 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001592{
David L Stevens42908c62008-04-29 03:23:22 -07001593 int err;
1594
1595 if (optname == MCAST_MSFILTER)
1596 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1597 ip_getsockopt);
1598
Daniel Balutadd23198e2011-08-07 22:31:07 -07001599 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1600 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001601
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001602#ifdef CONFIG_NETFILTER
1603 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001604 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1605 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001606 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001607
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001608 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001609 return -EFAULT;
1610
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001611 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001612 if (err >= 0)
1613 err = put_user(len, optlen);
1614 return err;
1615 }
1616#endif
1617 return err;
1618}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001619EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001620#endif