blob: 5ddd64995e7387ebc00a80f6b8f52a354d5bbdaa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * The IP to API glue.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: see ip.c
9 *
10 * Fixes:
11 * Many : Split from ip.c , see ip.c for history.
12 * Martin Mares : TOS setting fixed.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090013 * Alan Cox : Fixed a couple of oopses in Martin's
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * TOS tweaks.
15 * Mike McLagan : Routing by source
16 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/skbuff.h>
22#include <linux/ip.h>
23#include <linux/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020024#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/sock.h>
28#include <net/ip.h>
29#include <net/icmp.h>
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -080030#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/udp.h>
32#include <linux/igmp.h>
33#include <linux/netfilter.h>
34#include <linux/route.h>
35#include <linux/mroute.h>
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -040036#include <net/inet_ecn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <net/route.h>
38#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070039#include <net/compat.h>
Tom Herbertad6f9392015-01-05 13:56:17 -080040#include <net/checksum.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000041#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <net/transp_v6.h>
43#endif
David S. Miller35ebf652012-06-28 03:59:11 -070044#include <net/ip_fib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <linux/errqueue.h>
47#include <asm/uaccess.h>
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * SOL_IP control messages.
51 */
52
53static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
54{
Eric Dumazetd826eb12011-11-09 07:24:35 +000055 struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070057 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
60}
61
62static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
63{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070064 int ttl = ip_hdr(skb)->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
66}
67
68static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
69{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070070 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
74{
75 if (IPCB(skb)->opt.optlen == 0)
76 return;
77
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070078 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
79 ip_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82
83static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
84{
85 unsigned char optbuf[sizeof(struct ip_options) + 40];
Daniel Baluta5e73ea12012-04-15 01:34:41 +000086 struct ip_options *opt = (struct ip_options *)optbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 if (IPCB(skb)->opt.optlen == 0)
89 return;
90
91 if (ip_options_echo(opt, skb)) {
92 msg->msg_flags |= MSG_CTRUNC;
93 return;
94 }
95 ip_options_undo(opt);
96
97 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
98}
99
Tom Herbertad6f9392015-01-05 13:56:17 -0800100static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb,
Eric Dumazet10df8e62016-10-23 18:03:06 -0700101 int tlen, int offset)
Tom Herbertad6f9392015-01-05 13:56:17 -0800102{
103 __wsum csum = skb->csum;
104
105 if (skb->ip_summed != CHECKSUM_COMPLETE)
106 return;
107
Paolo Abeni481aedf2017-02-21 09:33:18 +0100108 if (offset != 0) {
109 int tend_off = skb_transport_offset(skb) + tlen;
110 csum = csum_sub(csum, skb_checksum(skb, tend_off, offset, 0));
111 }
Tom Herbertad6f9392015-01-05 13:56:17 -0800112
113 put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum);
114}
115
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800116static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
117{
118 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700119 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800120 int err;
121
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700122 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
123 if (err)
124 return;
125
126 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800127 if (err)
128 return;
129
130 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700131 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800132}
133
Harvey Harrison21d1a162008-11-20 01:54:27 -0800134static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800135{
136 struct sockaddr_in sin;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000137 const struct iphdr *iph = ip_hdr(skb);
Harvey Harrison21d1a162008-11-20 01:54:27 -0800138 __be16 *ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800139
Willem de Bruijnd36a1cb2016-12-22 18:19:16 -0500140 if (skb_transport_offset(skb) + 4 > (int)skb->len)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800141 return;
142
143 /* All current transport protocols have the port numbers in the
144 * first four bytes of the transport header and this function is
145 * written with this assumption in mind.
146 */
147
148 sin.sin_family = AF_INET;
149 sin.sin_addr.s_addr = iph->daddr;
150 sin.sin_port = ports[1];
151 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
152
153 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
154}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Tom Herbert5961de92015-01-05 13:56:16 -0800156void ip_cmsg_recv_offset(struct msghdr *msg, struct sk_buff *skb,
Eric Dumazet10df8e62016-10-23 18:03:06 -0700157 int tlen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 struct inet_sock *inet = inet_sk(skb->sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000160 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /* Ordered by supposed usage frequency */
Tom Herbertc44d13d2015-01-05 13:56:15 -0800163 if (flags & IP_CMSG_PKTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 ip_cmsg_recv_pktinfo(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Tom Herbertc44d13d2015-01-05 13:56:15 -0800166 flags &= ~IP_CMSG_PKTINFO;
167 if (!flags)
168 return;
169 }
170
171 if (flags & IP_CMSG_TTL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 ip_cmsg_recv_ttl(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Tom Herbertc44d13d2015-01-05 13:56:15 -0800174 flags &= ~IP_CMSG_TTL;
175 if (!flags)
176 return;
177 }
178
179 if (flags & IP_CMSG_TOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 ip_cmsg_recv_tos(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Tom Herbertc44d13d2015-01-05 13:56:15 -0800182 flags &= ~IP_CMSG_TOS;
183 if (!flags)
184 return;
185 }
186
187 if (flags & IP_CMSG_RECVOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 ip_cmsg_recv_opts(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Tom Herbertc44d13d2015-01-05 13:56:15 -0800190 flags &= ~IP_CMSG_RECVOPTS;
191 if (!flags)
192 return;
193 }
194
195 if (flags & IP_CMSG_RETOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 ip_cmsg_recv_retopts(msg, skb);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800197
Tom Herbertc44d13d2015-01-05 13:56:15 -0800198 flags &= ~IP_CMSG_RETOPTS;
199 if (!flags)
200 return;
201 }
202
203 if (flags & IP_CMSG_PASSSEC) {
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800204 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800205
Tom Herbertc44d13d2015-01-05 13:56:15 -0800206 flags &= ~IP_CMSG_PASSSEC;
207 if (!flags)
208 return;
209 }
210
Tom Herbertad6f9392015-01-05 13:56:17 -0800211 if (flags & IP_CMSG_ORIGDSTADDR) {
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800212 ip_cmsg_recv_dstaddr(msg, skb);
213
Tom Herbertad6f9392015-01-05 13:56:17 -0800214 flags &= ~IP_CMSG_ORIGDSTADDR;
215 if (!flags)
216 return;
217 }
218
219 if (flags & IP_CMSG_CHECKSUM)
Eric Dumazet10df8e62016-10-23 18:03:06 -0700220 ip_cmsg_recv_checksum(msg, skb, tlen, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
Tom Herbert5961de92015-01-05 13:56:16 -0800222EXPORT_SYMBOL(ip_cmsg_recv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400224int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100225 bool allow_ipv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Francesco Fuscof02db312013-09-24 15:43:08 +0200227 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct cmsghdr *cmsg;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400229 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Gu Zhengf95b4142014-12-11 11:22:04 +0800231 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (!CMSG_OK(msg, cmsg))
233 return -EINVAL;
Eric Dumazet5337b5b2014-11-10 17:54:25 -0800234#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100235 if (allow_ipv6 &&
236 cmsg->cmsg_level == SOL_IPV6 &&
237 cmsg->cmsg_type == IPV6_PKTINFO) {
238 struct in6_pktinfo *src_info;
239
240 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
241 return -EINVAL;
242 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
243 if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
244 return -EINVAL;
David Ahern5f02dce2018-02-16 11:03:03 -0800245 if (src_info->ipi6_ifindex)
246 ipc->oif = src_info->ipi6_ifindex;
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100247 ipc->addr = src_info->ipi6_addr.s6_addr32[3];
248 continue;
249 }
250#endif
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400251 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700252 err = __sock_cmsg_send(sk, msg, cmsg, &ipc->sockc);
253 if (err)
254 return err;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400255 continue;
256 }
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (cmsg->cmsg_level != SOL_IP)
259 continue;
260 switch (cmsg->cmsg_type) {
261 case IP_RETOPTS:
262 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
Eric Dumazet91948302016-02-04 06:23:28 -0800263
264 /* Our caller is responsible for freeing ipc->opt */
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700265 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
266 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (err)
268 return err;
269 break;
270 case IP_PKTINFO:
271 {
272 struct in_pktinfo *info;
273 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
274 return -EINVAL;
275 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
David Ahern5f02dce2018-02-16 11:03:03 -0800276 if (info->ipi_ifindex)
277 ipc->oif = info->ipi_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 ipc->addr = info->ipi_spec_dst.s_addr;
279 break;
280 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200281 case IP_TTL:
282 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
283 return -EINVAL;
284 val = *(int *)CMSG_DATA(cmsg);
285 if (val < 1 || val > 255)
286 return -EINVAL;
287 ipc->ttl = val;
288 break;
289 case IP_TOS:
Eric Dumazete895cdc2016-09-07 21:52:56 -0700290 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)))
291 val = *(int *)CMSG_DATA(cmsg);
292 else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8)))
293 val = *(u8 *)CMSG_DATA(cmsg);
294 else
Francesco Fuscof02db312013-09-24 15:43:08 +0200295 return -EINVAL;
Francesco Fuscof02db312013-09-24 15:43:08 +0200296 if (val < 0 || val > 255)
297 return -EINVAL;
298 ipc->tos = val;
299 ipc->priority = rt_tos2priority(ipc->tos);
300 break;
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 default:
303 return -EINVAL;
304 }
305 }
306 return 0;
307}
308
309
310/* Special input handler for packets caught by router alert option.
311 They are selected only by protocol field, and then processed likely
312 local ones; but only if someone wants them! Otherwise, router
313 not running rsvpd will kill RSVP.
314
315 It is user level problem, what it will make with them.
316 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
317 but receiver should be enough clever f.e. to forward mtrace requests,
318 sent to multicast group to reach destination designated router.
319 */
Eric Dumazet43a951e2010-10-25 03:32:44 +0000320struct ip_ra_chain __rcu *ip_ra_chain;
Eric Dumazet66018502010-06-07 03:12:08 +0000321static DEFINE_SPINLOCK(ip_ra_lock);
322
Eric Dumazet592fcb92010-06-09 16:21:07 +0000323
324static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000325{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000326 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
327
328 sock_put(ra->saved_sk);
329 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700332int ip_ra_control(struct sock *sk, unsigned char on,
333 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000335 struct ip_ra_chain *ra, *new_ra;
336 struct ip_ra_chain __rcu **rap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000338 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return -EINVAL;
340
341 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
342
Eric Dumazet66018502010-06-07 03:12:08 +0000343 spin_lock_bh(&ip_ra_lock);
Eric Dumazet43a951e2010-10-25 03:32:44 +0000344 for (rap = &ip_ra_chain;
345 (ra = rcu_dereference_protected(*rap,
346 lockdep_is_held(&ip_ra_lock))) != NULL;
347 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (ra->sk == sk) {
349 if (on) {
Eric Dumazet66018502010-06-07 03:12:08 +0000350 spin_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800351 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return -EADDRINUSE;
353 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000354 /* dont let ip_call_ra_chain() use sk again */
355 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700356 RCU_INIT_POINTER(*rap, ra->next);
Eric Dumazet66018502010-06-07 03:12:08 +0000357 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 if (ra->destructor)
360 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000361 /*
362 * Delay sock_put(sk) and kfree(ra) after one rcu grace
363 * period. This guarantee ip_call_ra_chain() dont need
364 * to mess with socket refcounts.
365 */
366 ra->saved_sk = sk;
367 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369 }
370 }
Ian Morris51456b22015-04-03 09:17:26 +0100371 if (!new_ra) {
Eric Dumazet66018502010-06-07 03:12:08 +0000372 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return -ENOBUFS;
374 }
375 new_ra->sk = sk;
376 new_ra->destructor = destructor;
377
Eric Dumazet8e380f02014-09-09 08:11:41 -0700378 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000379 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 sock_hold(sk);
Eric Dumazet66018502010-06-07 03:12:08 +0000381 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 return 0;
384}
385
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900386void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700387 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct sock_exterr_skb *serr;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 skb = skb_clone(skb, GFP_ATOMIC);
392 if (!skb)
393 return;
394
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900395 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 serr->ee.ee_errno = err;
397 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300398 serr->ee.ee_type = icmp_hdr(skb)->type;
399 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 serr->ee.ee_pad = 0;
401 serr->ee.ee_info = info;
402 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300403 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700404 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 serr->port = port;
406
Ian Morris00db4122015-04-03 09:17:27 +0100407 if (skb_pull(skb, payload - skb->data)) {
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300408 skb_reset_transport_header(skb);
409 if (sock_queue_err_skb(sk, skb) == 0)
410 return;
411 }
412 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Al Viro05790162006-09-27 18:33:40 -0700415void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct inet_sock *inet = inet_sk(sk);
418 struct sock_exterr_skb *serr;
419 struct iphdr *iph;
420 struct sk_buff *skb;
421
422 if (!inet->recverr)
423 return;
424
425 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
426 if (!skb)
427 return;
428
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300429 skb_put(skb, sizeof(struct iphdr));
430 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700431 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 iph->daddr = daddr;
433
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900434 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 serr->ee.ee_errno = err;
436 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900437 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 serr->ee.ee_code = 0;
439 serr->ee.ee_pad = 0;
440 serr->ee.ee_info = info;
441 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700442 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 serr->port = port;
444
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700445 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300446 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (sock_queue_err_skb(sk, skb))
449 kfree_skb(skb);
450}
451
Julian Anastasov34b99df2015-06-23 08:34:39 +0300452/* For some errors we have valid addr_offset even with zero payload and
453 * zero port. Also, addr_offset should be supported if port is set.
454 */
455static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
456{
457 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
458 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
459}
460
Willem de Bruijnc247f052015-03-07 20:33:22 -0500461/* IPv4 supports cmsg on all imcp errors and some timestamps
462 *
463 * Timestamp code paths do not initialize the fields expected by cmsg:
464 * the PKTINFO fields in skb->cb[]. Fill those in here.
465 */
466static bool ipv4_datagram_support_cmsg(const struct sock *sk,
467 struct sk_buff *skb,
468 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500469{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500470 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500471
Willem de Bruijnc247f052015-03-07 20:33:22 -0500472 if (ee_origin == SO_EE_ORIGIN_ICMP)
473 return true;
474
475 if (ee_origin == SO_EE_ORIGIN_LOCAL)
476 return false;
477
478 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
Willem de Bruijn8c04e2a2017-04-12 19:24:35 -0400479 * timestamp with egress dev. Not possible for packets without iif
Willem de Bruijnc247f052015-03-07 20:33:22 -0500480 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
481 */
Willem de Bruijn8c04e2a2017-04-12 19:24:35 -0400482 info = PKTINFO_SKB_CB(skb);
483 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
484 !info->ipi_ifindex)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500485 return false;
486
487 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500488 return true;
489}
490
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900491/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * Handle MSG_ERRQUEUE
493 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100494int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400497 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100498 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct {
500 struct sock_extended_err ee;
501 struct sockaddr_in offender;
502 } errhdr;
503 int err;
504 int copied;
505
Willem de Bruijn7ce875e2014-11-30 22:22:33 -0500506 WARN_ON_ONCE(sk->sk_family == AF_INET6);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400509 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100510 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 goto out;
512
513 copied = skb->len;
514 if (copied > len) {
515 msg->msg_flags |= MSG_TRUNC;
516 copied = len;
517 }
David S. Miller51f3d022014-11-05 16:46:40 -0500518 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700519 if (unlikely(err)) {
520 kfree_skb(skb);
521 return err;
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 sock_recv_timestamp(msg, sk, skb);
524
525 serr = SKB_EXT_ERR(skb);
526
Julian Anastasov34b99df2015-06-23 08:34:39 +0300527 if (sin && ipv4_datagram_support_addr(serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700529 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
530 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 sin->sin_port = serr->port;
532 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100533 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
536 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
537 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500538 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500539
Willem de Bruijnc247f052015-03-07 20:33:22 -0500540 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700542 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500543 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 ip_cmsg_recv(msg, skb);
545 }
546
547 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
548
549 /* Now we could try to dump offended packet options */
550
551 msg->msg_flags |= MSG_ERRQUEUE;
552 err = copied;
553
Eric Dumazet960a2622016-04-21 22:27:32 -0700554 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555out:
556 return err;
557}
558
559
560/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700561 * Socket option code for IP. This is the end of the line after any
562 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300564static bool setsockopt_needs_rtnl(int optname)
565{
566 switch (optname) {
567 case IP_ADD_MEMBERSHIP:
568 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300569 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300570 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300571 case IP_DROP_SOURCE_MEMBERSHIP:
572 case IP_MSFILTER:
573 case IP_UNBLOCK_SOURCE:
574 case MCAST_BLOCK_SOURCE:
575 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300576 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300577 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300578 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300579 case MCAST_LEAVE_SOURCE_GROUP:
580 case MCAST_UNBLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300581 return true;
582 }
583 return false;
584}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800586static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700587 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct inet_sock *inet = inet_sk(sk);
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200590 struct net *net = sock_net(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800591 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300592 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Xi Wang0c9f79b2012-11-11 11:20:01 +0000594 switch (optname) {
595 case IP_PKTINFO:
596 case IP_RECVTTL:
597 case IP_RECVOPTS:
598 case IP_RECVTOS:
599 case IP_RETOPTS:
600 case IP_TOS:
601 case IP_TTL:
602 case IP_HDRINCL:
603 case IP_MTU_DISCOVER:
604 case IP_RECVERR:
605 case IP_ROUTER_ALERT:
606 case IP_FREEBIND:
607 case IP_PASSSEC:
608 case IP_TRANSPARENT:
609 case IP_MINTTL:
610 case IP_NODEFRAG:
Eric Dumazet90c337d2015-06-06 21:17:57 -0700611 case IP_BIND_ADDRESS_NO_PORT:
Xi Wang0c9f79b2012-11-11 11:20:01 +0000612 case IP_UNICAST_IF:
613 case IP_MULTICAST_TTL:
614 case IP_MULTICAST_ALL:
615 case IP_MULTICAST_LOOP:
616 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800617 case IP_CHECKSUM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (optlen >= sizeof(int)) {
619 if (get_user(val, (int __user *) optval))
620 return -EFAULT;
621 } else if (optlen >= sizeof(char)) {
622 unsigned char ucval;
623
624 if (get_user(ucval, (unsigned char __user *) optval))
625 return -EFAULT;
626 val = (int) ucval;
627 }
628 }
629
630 /* If optlen==0, it is equivalent to val == 0 */
631
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800632 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800633 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300636 if (needs_rtnl)
637 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 lock_sock(sk);
639
640 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800641 case IP_OPTIONS:
642 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000643 struct ip_options_rcu *old, *opt = NULL;
644
roel kluin65a1c4f2009-10-23 05:59:21 +0000645 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800646 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900647 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700648 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800649 if (err)
650 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000651 old = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200652 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800653 if (inet->is_icsk) {
654 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000655#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800656 if (sk->sk_family == PF_INET ||
657 (!((1 << sk->sk_state) &
658 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000659 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000661 if (old)
662 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800663 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000664 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800665 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000666#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000670 rcu_assign_pointer(inet->inet_opt, opt);
671 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800672 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800673 break;
674 }
675 case IP_PKTINFO:
676 if (val)
677 inet->cmsg_flags |= IP_CMSG_PKTINFO;
678 else
679 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
680 break;
681 case IP_RECVTTL:
682 if (val)
683 inet->cmsg_flags |= IP_CMSG_TTL;
684 else
685 inet->cmsg_flags &= ~IP_CMSG_TTL;
686 break;
687 case IP_RECVTOS:
688 if (val)
689 inet->cmsg_flags |= IP_CMSG_TOS;
690 else
691 inet->cmsg_flags &= ~IP_CMSG_TOS;
692 break;
693 case IP_RECVOPTS:
694 if (val)
695 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
696 else
697 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
698 break;
699 case IP_RETOPTS:
700 if (val)
701 inet->cmsg_flags |= IP_CMSG_RETOPTS;
702 else
703 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
704 break;
705 case IP_PASSSEC:
706 if (val)
707 inet->cmsg_flags |= IP_CMSG_PASSSEC;
708 else
709 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
710 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800711 case IP_RECVORIGDSTADDR:
712 if (val)
713 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
714 else
715 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
716 break;
Tom Herbertad6f9392015-01-05 13:56:17 -0800717 case IP_CHECKSUM:
718 if (val) {
719 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
720 inet_inc_convert_csum(sk);
721 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
722 }
723 } else {
724 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
725 inet_dec_convert_csum(sk);
726 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
727 }
728 }
729 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800730 case IP_TOS: /* This sets both TOS and Precedence */
731 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400732 val &= ~INET_ECN_MASK;
733 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800735 if (inet->tos != val) {
736 inet->tos = val;
737 sk->sk_priority = rt_tos2priority(val);
738 sk_dst_reset(sk);
739 }
740 break;
741 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700742 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800743 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000744 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800745 goto e_inval;
746 inet->uc_ttl = val;
747 break;
748 case IP_HDRINCL:
749 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800752 }
753 inet->hdrincl = val ? 1 : 0;
754 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000755 case IP_NODEFRAG:
756 if (sk->sk_type != SOCK_RAW) {
757 err = -ENOPROTOOPT;
758 break;
759 }
760 inet->nodefrag = val ? 1 : 0;
761 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -0700762 case IP_BIND_ADDRESS_NO_PORT:
763 inet->bind_address_no_port = val ? 1 : 0;
764 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800765 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100766 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800767 goto e_inval;
768 inet->pmtudisc = val;
769 break;
770 case IP_RECVERR:
771 inet->recverr = !!val;
772 if (!val)
773 skb_queue_purge(&sk->sk_error_queue);
774 break;
775 case IP_MULTICAST_TTL:
776 if (sk->sk_type == SOCK_STREAM)
777 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700778 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800779 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800780 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800781 val = 1;
782 if (val < 0 || val > 255)
783 goto e_inval;
784 inet->mc_ttl = val;
785 break;
786 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700787 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800788 goto e_inval;
789 inet->mc_loop = !!val;
790 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000791 case IP_UNICAST_IF:
792 {
793 struct net_device *dev = NULL;
794 int ifindex;
795
796 if (optlen != sizeof(int))
797 goto e_inval;
798
799 ifindex = (__force int)ntohl((__force __be32)val);
800 if (ifindex == 0) {
801 inet->uc_index = 0;
802 err = 0;
803 break;
804 }
805
806 dev = dev_get_by_index(sock_net(sk), ifindex);
807 err = -EADDRNOTAVAIL;
808 if (!dev)
809 break;
810 dev_put(dev);
811
812 err = -EINVAL;
813 if (sk->sk_bound_dev_if)
814 break;
815
816 inet->uc_index = ifindex;
817 err = 0;
818 break;
819 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800820 case IP_MULTICAST_IF:
821 {
822 struct ip_mreqn mreq;
823 struct net_device *dev = NULL;
David Ahern5c68a422016-12-29 15:39:37 -0800824 int midx;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800825
826 if (sk->sk_type == SOCK_STREAM)
827 goto e_inval;
828 /*
829 * Check the arguments are allowable
830 */
831
Shan Wei09159212009-09-22 15:41:10 +0000832 if (optlen < sizeof(struct in_addr))
833 goto e_inval;
834
Stephen Hemminger132adf52007-03-08 20:44:43 -0800835 err = -EFAULT;
836 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800837 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800838 break;
839 } else {
840 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000841 if (optlen >= sizeof(struct ip_mreq)) {
842 if (copy_from_user(&mreq, optval,
843 sizeof(struct ip_mreq)))
844 break;
845 } else if (optlen >= sizeof(struct in_addr)) {
846 if (copy_from_user(&mreq.imr_address, optval,
847 sizeof(struct in_addr)))
848 break;
849 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800850 }
851
852 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700853 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800854 inet->mc_index = 0;
855 inet->mc_addr = 0;
856 err = 0;
857 break;
858 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900859 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000860 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800861 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800862 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000863 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800864
865
866 err = -EADDRNOTAVAIL;
867 if (!dev)
868 break;
David Ahern5c68a422016-12-29 15:39:37 -0800869
870 midx = l3mdev_master_ifindex(dev);
871
Eric Dumazet55b80502009-10-19 06:41:58 +0000872 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800873
874 err = -EINVAL;
875 if (sk->sk_bound_dev_if &&
David Ahern5c68a422016-12-29 15:39:37 -0800876 mreq.imr_ifindex != sk->sk_bound_dev_if &&
877 (!midx || midx != sk->sk_bound_dev_if))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800878 break;
879
880 inet->mc_index = mreq.imr_ifindex;
881 inet->mc_addr = mreq.imr_address.s_addr;
882 err = 0;
883 break;
884 }
885
886 case IP_ADD_MEMBERSHIP:
887 case IP_DROP_MEMBERSHIP:
888 {
889 struct ip_mreqn mreq;
890
Flavio Leitnera96fb492007-08-24 22:16:39 -0700891 err = -EPROTO;
892 if (inet_sk(sk)->is_icsk)
893 break;
894
Stephen Hemminger132adf52007-03-08 20:44:43 -0800895 if (optlen < sizeof(struct ip_mreq))
896 goto e_inval;
897 err = -EFAULT;
898 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800899 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800900 break;
901 } else {
902 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800903 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800904 break;
905 }
906
907 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300908 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800909 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300910 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800911 break;
912 }
913 case IP_MSFILTER:
914 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800915 struct ip_msfilter *msf;
916
917 if (optlen < IP_MSFILTER_SIZE(0))
918 goto e_inval;
919 if (optlen > sysctl_optmem_max) {
920 err = -ENOBUFS;
921 break;
922 }
923 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700924 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800925 err = -ENOBUFS;
926 break;
927 }
928 err = -EFAULT;
929 if (copy_from_user(msf, optval, optlen)) {
930 kfree(msf);
931 break;
932 }
933 /* numsrc >= (1G-4) overflow in 32 bits */
934 if (msf->imsf_numsrc >= 0x3ffffffcU ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200935 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800936 kfree(msf);
937 err = -ENOBUFS;
938 break;
939 }
940 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
941 kfree(msf);
942 err = -EINVAL;
943 break;
944 }
945 err = ip_mc_msfilter(sk, msf, 0);
946 kfree(msf);
947 break;
948 }
949 case IP_BLOCK_SOURCE:
950 case IP_UNBLOCK_SOURCE:
951 case IP_ADD_SOURCE_MEMBERSHIP:
952 case IP_DROP_SOURCE_MEMBERSHIP:
953 {
954 struct ip_mreq_source mreqs;
955 int omode, add;
956
957 if (optlen != sizeof(struct ip_mreq_source))
958 goto e_inval;
959 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
960 err = -EFAULT;
961 break;
962 }
963 if (optname == IP_BLOCK_SOURCE) {
964 omode = MCAST_EXCLUDE;
965 add = 1;
966 } else if (optname == IP_UNBLOCK_SOURCE) {
967 omode = MCAST_EXCLUDE;
968 add = 0;
969 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
970 struct ip_mreqn mreq;
971
972 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
973 mreq.imr_address.s_addr = mreqs.imr_interface;
974 mreq.imr_ifindex = 0;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300975 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800976 if (err && err != -EADDRINUSE)
977 break;
978 omode = MCAST_INCLUDE;
979 add = 1;
980 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
981 omode = MCAST_INCLUDE;
982 add = 0;
983 }
984 err = ip_mc_source(add, omode, sk, &mreqs, 0);
985 break;
986 }
987 case MCAST_JOIN_GROUP:
988 case MCAST_LEAVE_GROUP:
989 {
990 struct group_req greq;
991 struct sockaddr_in *psin;
992 struct ip_mreqn mreq;
993
994 if (optlen < sizeof(struct group_req))
995 goto e_inval;
996 err = -EFAULT;
997 if (copy_from_user(&greq, optval, sizeof(greq)))
998 break;
999 psin = (struct sockaddr_in *)&greq.gr_group;
1000 if (psin->sin_family != AF_INET)
1001 goto e_inval;
1002 memset(&mreq, 0, sizeof(mreq));
1003 mreq.imr_multiaddr = psin->sin_addr;
1004 mreq.imr_ifindex = greq.gr_interface;
1005
1006 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001007 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001008 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001009 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001010 break;
1011 }
1012 case MCAST_JOIN_SOURCE_GROUP:
1013 case MCAST_LEAVE_SOURCE_GROUP:
1014 case MCAST_BLOCK_SOURCE:
1015 case MCAST_UNBLOCK_SOURCE:
1016 {
1017 struct group_source_req greqs;
1018 struct ip_mreq_source mreqs;
1019 struct sockaddr_in *psin;
1020 int omode, add;
1021
1022 if (optlen != sizeof(struct group_source_req))
1023 goto e_inval;
1024 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
1025 err = -EFAULT;
1026 break;
1027 }
1028 if (greqs.gsr_group.ss_family != AF_INET ||
1029 greqs.gsr_source.ss_family != AF_INET) {
1030 err = -EADDRNOTAVAIL;
1031 break;
1032 }
1033 psin = (struct sockaddr_in *)&greqs.gsr_group;
1034 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
1035 psin = (struct sockaddr_in *)&greqs.gsr_source;
1036 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
1037 mreqs.imr_interface = 0; /* use index for mc_source */
1038
1039 if (optname == MCAST_BLOCK_SOURCE) {
1040 omode = MCAST_EXCLUDE;
1041 add = 1;
1042 } else if (optname == MCAST_UNBLOCK_SOURCE) {
1043 omode = MCAST_EXCLUDE;
1044 add = 0;
1045 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
1046 struct ip_mreqn mreq;
1047
1048 psin = (struct sockaddr_in *)&greqs.gsr_group;
1049 mreq.imr_multiaddr = psin->sin_addr;
1050 mreq.imr_address.s_addr = 0;
1051 mreq.imr_ifindex = greqs.gsr_interface;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001052 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001053 if (err && err != -EADDRINUSE)
1054 break;
1055 greqs.gsr_interface = mreq.imr_ifindex;
1056 omode = MCAST_INCLUDE;
1057 add = 1;
1058 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1059 omode = MCAST_INCLUDE;
1060 add = 0;
1061 }
1062 err = ip_mc_source(add, omode, sk, &mreqs,
1063 greqs.gsr_interface);
1064 break;
1065 }
1066 case MCAST_MSFILTER:
1067 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001068 struct sockaddr_in *psin;
1069 struct ip_msfilter *msf = NULL;
1070 struct group_filter *gsf = NULL;
1071 int msize, i, ifindex;
1072
1073 if (optlen < GROUP_FILTER_SIZE(0))
1074 goto e_inval;
1075 if (optlen > sysctl_optmem_max) {
1076 err = -ENOBUFS;
1077 break;
1078 }
Jianjun Kong09cb1052008-11-03 00:27:11 -08001079 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001080 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001081 err = -ENOBUFS;
1082 break;
1083 }
1084 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001085 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001086 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001087
Stephen Hemminger132adf52007-03-08 20:44:43 -08001088 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1089 if (gsf->gf_numsrc >= 0x1ffffff ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +02001090 gsf->gf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001091 err = -ENOBUFS;
1092 goto mc_msf_out;
1093 }
1094 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
1095 err = -EINVAL;
1096 goto mc_msf_out;
1097 }
1098 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -08001099 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001100 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001101 err = -ENOBUFS;
1102 goto mc_msf_out;
1103 }
1104 ifindex = gsf->gf_interface;
1105 psin = (struct sockaddr_in *)&gsf->gf_group;
1106 if (psin->sin_family != AF_INET) {
1107 err = -EADDRNOTAVAIL;
1108 goto mc_msf_out;
1109 }
1110 msf->imsf_multiaddr = psin->sin_addr.s_addr;
1111 msf->imsf_interface = 0;
1112 msf->imsf_fmode = gsf->gf_fmode;
1113 msf->imsf_numsrc = gsf->gf_numsrc;
1114 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001115 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001116 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
1117
1118 if (psin->sin_family != AF_INET)
1119 goto mc_msf_out;
1120 msf->imsf_slist[i] = psin->sin_addr.s_addr;
1121 }
1122 kfree(gsf);
1123 gsf = NULL;
1124
1125 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001126mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -08001127 kfree(msf);
1128 kfree(gsf);
1129 break;
1130 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001131 case IP_MULTICAST_ALL:
1132 if (optlen < 1)
1133 goto e_inval;
1134 if (val != 0 && val != 1)
1135 goto e_inval;
1136 inet->mc_all = val;
1137 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001138 case IP_ROUTER_ALERT:
1139 err = ip_ra_control(sk, val ? 1 : 0, NULL);
1140 break;
1141
1142 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001143 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001144 goto e_inval;
1145 inet->freebind = !!val;
1146 break;
1147
1148 case IP_IPSEC_POLICY:
1149 case IP_XFRM_POLICY:
1150 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001151 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001152 break;
1153 err = xfrm_user_policy(sk, optname, optval, optlen);
1154 break;
1155
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001156 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001157 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1158 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001159 err = -EPERM;
1160 break;
1161 }
1162 if (optlen < 1)
1163 goto e_inval;
1164 inet->transparent = !!val;
1165 break;
1166
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001167 case IP_MINTTL:
1168 if (optlen < 1)
1169 goto e_inval;
1170 if (val < 0 || val > 255)
1171 goto e_inval;
1172 inet->min_ttl = val;
1173 break;
1174
Stephen Hemminger132adf52007-03-08 20:44:43 -08001175 default:
1176 err = -ENOPROTOOPT;
1177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001180 if (needs_rtnl)
1181 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return err;
1183
1184e_inval:
1185 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001186 if (needs_rtnl)
1187 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return -EINVAL;
1189}
1190
Eric Dumazetf84af322010-04-28 15:31:51 -07001191/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001192 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001193 * @sk: socket
1194 * @skb: buffer
1195 *
David S. Miller35ebf652012-06-28 03:59:11 -07001196 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1197 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001198 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001199 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001200void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001201{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001202 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +01001203 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1204 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001205
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +01001206 if (prepare && skb_rtable(skb)) {
David Ahern0b922b72016-05-10 11:19:51 -07001207 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1208 * which has interface index (iif) as the first member of the
1209 * underlying inet{6}_skb_parm struct. This code then overlays
1210 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
Wei Zhange7422082016-12-29 16:45:04 +08001211 * element so the iif is picked up from the prior IPCB. If iif
1212 * is the loopback interface, then return the sending interface
1213 * (e.g., process binds socket to eth0 for Tx which is
1214 * redirected to loopback in the rtable/dst).
David Ahern0b922b72016-05-10 11:19:51 -07001215 */
Wei Zhange7422082016-12-29 16:45:04 +08001216 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
1217 pktinfo->ipi_ifindex = inet_iif(skb);
1218
David S. Miller35ebf652012-06-28 03:59:11 -07001219 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001220 } else {
1221 pktinfo->ipi_ifindex = 0;
1222 pktinfo->ipi_spec_dst.s_addr = 0;
1223 }
Eric Dumazetf5b54442017-02-04 11:16:52 -08001224 /* We need to keep the dst for __ip_options_echo()
1225 * We could restrict the test to opt.ts_needtime || opt.srr,
1226 * but the following is good enough as IP options are not often used.
1227 */
1228 if (unlikely(IPCB(skb)->opt.optlen))
1229 skb_dst_force(skb);
1230 else
1231 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001232}
Eric Dumazetf84af322010-04-28 15:31:51 -07001233
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001234int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001235 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001236{
1237 int err;
1238
1239 if (level != SOL_IP)
1240 return -ENOPROTOOPT;
1241
1242 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1243#ifdef CONFIG_NETFILTER
1244 /* we need to exclude all possible ENOPROTOOPTs except default case */
1245 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001246 optname != IP_IPSEC_POLICY &&
1247 optname != IP_XFRM_POLICY &&
Paolo Abeni4ec264d2018-01-30 19:01:40 +01001248 !ip_mroute_opt(optname))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001249 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001250#endif
1251 return err;
1252}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001253EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001254
1255#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001256int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001257 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001258{
1259 int err;
1260
1261 if (level != SOL_IP)
1262 return -ENOPROTOOPT;
1263
David L Stevensdae50292008-04-27 01:06:07 -07001264 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1265 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1266 ip_setsockopt);
1267
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001268 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1269#ifdef CONFIG_NETFILTER
1270 /* we need to exclude all possible ENOPROTOOPTs except default case */
1271 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001272 optname != IP_IPSEC_POLICY &&
1273 optname != IP_XFRM_POLICY &&
Paolo Abeni4ec264d2018-01-30 19:01:40 +01001274 !ip_mroute_opt(optname))
1275 err = compat_nf_setsockopt(sk, PF_INET, optname, optval,
1276 optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001277#endif
1278 return err;
1279}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001280EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001281#endif
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001284 * Get the options. Note for future reference. The GET of IP options gets
1285 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 */
1287
WANG Cong87e9f032015-11-03 15:41:16 -08001288static bool getsockopt_needs_rtnl(int optname)
1289{
1290 switch (optname) {
1291 case IP_MSFILTER:
1292 case MCAST_MSFILTER:
1293 return true;
1294 }
1295 return false;
1296}
1297
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001298static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001299 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct inet_sock *inet = inet_sk(sk);
WANG Cong87e9f032015-11-03 15:41:16 -08001302 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1303 int val, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001305
Stephen Hemminger132adf52007-03-08 20:44:43 -08001306 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -EOPNOTSUPP;
1308
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001309 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001310 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Jianjun Kong09cb1052008-11-03 00:27:11 -08001312 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001314 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001316
WANG Cong87e9f032015-11-03 15:41:16 -08001317 if (needs_rtnl)
1318 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 lock_sock(sk);
1320
Stephen Hemminger132adf52007-03-08 20:44:43 -08001321 switch (optname) {
1322 case IP_OPTIONS:
1323 {
1324 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001325 struct ip_options *opt = (struct ip_options *)optbuf;
1326 struct ip_options_rcu *inet_opt;
1327
1328 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001329 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001330 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001331 if (inet_opt)
1332 memcpy(optbuf, &inet_opt->opt,
1333 sizeof(struct ip_options) +
1334 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001335 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Stephen Hemminger132adf52007-03-08 20:44:43 -08001337 if (opt->optlen == 0)
1338 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Stephen Hemminger132adf52007-03-08 20:44:43 -08001340 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Stephen Hemminger132adf52007-03-08 20:44:43 -08001342 len = min_t(unsigned int, len, opt->optlen);
1343 if (put_user(len, optlen))
1344 return -EFAULT;
1345 if (copy_to_user(optval, opt->__data, len))
1346 return -EFAULT;
1347 return 0;
1348 }
1349 case IP_PKTINFO:
1350 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1351 break;
1352 case IP_RECVTTL:
1353 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1354 break;
1355 case IP_RECVTOS:
1356 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1357 break;
1358 case IP_RECVOPTS:
1359 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1360 break;
1361 case IP_RETOPTS:
1362 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1363 break;
1364 case IP_PASSSEC:
1365 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1366 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001367 case IP_RECVORIGDSTADDR:
1368 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1369 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001370 case IP_CHECKSUM:
1371 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1372 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001373 case IP_TOS:
1374 val = inet->tos;
1375 break;
1376 case IP_TTL:
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001377 {
1378 struct net *net = sock_net(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001379 val = (inet->uc_ttl == -1 ?
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001380 net->ipv4.sysctl_ip_default_ttl :
Stephen Hemminger132adf52007-03-08 20:44:43 -08001381 inet->uc_ttl);
1382 break;
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001383 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001384 case IP_HDRINCL:
1385 val = inet->hdrincl;
1386 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001387 case IP_NODEFRAG:
1388 val = inet->nodefrag;
1389 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001390 case IP_BIND_ADDRESS_NO_PORT:
1391 val = inet->bind_address_no_port;
1392 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001393 case IP_MTU_DISCOVER:
1394 val = inet->pmtudisc;
1395 break;
1396 case IP_MTU:
1397 {
1398 struct dst_entry *dst;
1399 val = 0;
1400 dst = sk_dst_get(sk);
1401 if (dst) {
1402 val = dst_mtu(dst);
1403 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001405 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001407 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001409 break;
1410 }
1411 case IP_RECVERR:
1412 val = inet->recverr;
1413 break;
1414 case IP_MULTICAST_TTL:
1415 val = inet->mc_ttl;
1416 break;
1417 case IP_MULTICAST_LOOP:
1418 val = inet->mc_loop;
1419 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001420 case IP_UNICAST_IF:
1421 val = (__force int)htonl((__u32) inet->uc_index);
1422 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001423 case IP_MULTICAST_IF:
1424 {
1425 struct in_addr addr;
1426 len = min_t(unsigned int, len, sizeof(struct in_addr));
1427 addr.s_addr = inet->mc_addr;
1428 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Stephen Hemminger132adf52007-03-08 20:44:43 -08001430 if (put_user(len, optlen))
1431 return -EFAULT;
1432 if (copy_to_user(optval, &addr, len))
1433 return -EFAULT;
1434 return 0;
1435 }
1436 case IP_MSFILTER:
1437 {
1438 struct ip_msfilter msf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001439
1440 if (len < IP_MSFILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001441 err = -EINVAL;
1442 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001444 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001445 err = -EFAULT;
1446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001448 err = ip_mc_msfget(sk, &msf,
1449 (struct ip_msfilter __user *)optval, optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001450 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001451 }
1452 case MCAST_MSFILTER:
1453 {
1454 struct group_filter gsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Stephen Hemminger132adf52007-03-08 20:44:43 -08001456 if (len < GROUP_FILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001457 err = -EINVAL;
1458 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001460 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001461 err = -EFAULT;
1462 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001463 }
1464 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001465 (struct group_filter __user *)optval,
1466 optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001467 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001468 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001469 case IP_MULTICAST_ALL:
1470 val = inet->mc_all;
1471 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001472 case IP_PKTOPTIONS:
1473 {
1474 struct msghdr msg;
1475
1476 release_sock(sk);
1477
1478 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001480
Karoly Kemenyc54a5e02014-07-27 12:29:07 +02001481 msg.msg_control = (__force void *) optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001482 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001483 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001484
1485 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1486 struct in_pktinfo info;
1487
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001488 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1489 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001490 info.ipi_ifindex = inet->mc_index;
1491 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1492 }
1493 if (inet->cmsg_flags & IP_CMSG_TTL) {
1494 int hlim = inet->mc_ttl;
1495 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1496 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001497 if (inet->cmsg_flags & IP_CMSG_TOS) {
1498 int tos = inet->rcv_tos;
1499 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1500 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001501 len -= msg.msg_controllen;
1502 return put_user(len, optlen);
1503 }
1504 case IP_FREEBIND:
1505 val = inet->freebind;
1506 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001507 case IP_TRANSPARENT:
1508 val = inet->transparent;
1509 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001510 case IP_MINTTL:
1511 val = inet->min_ttl;
1512 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001513 default:
1514 release_sock(sk);
1515 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
1517 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001518
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001519 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 unsigned char ucval = (unsigned char)val;
1521 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001522 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001524 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return -EFAULT;
1526 } else {
1527 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001528 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001530 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return -EFAULT;
1532 }
1533 return 0;
WANG Cong87e9f032015-11-03 15:41:16 -08001534
1535out:
1536 release_sock(sk);
1537 if (needs_rtnl)
1538 rtnl_unlock();
1539 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001542int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001543 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001544{
1545 int err;
1546
Daniel Balutadd23198e2011-08-07 22:31:07 -07001547 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001548#ifdef CONFIG_NETFILTER
1549 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001550 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1551 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001552 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001553
Jianjun Kong09cb1052008-11-03 00:27:11 -08001554 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001555 return -EFAULT;
1556
Paolo Abenid58d78c2018-02-08 12:19:00 +01001557 err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001558 if (err >= 0)
1559 err = put_user(len, optlen);
1560 return err;
1561 }
1562#endif
1563 return err;
1564}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001565EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001566
1567#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001568int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1569 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001570{
David L Stevens42908c62008-04-29 03:23:22 -07001571 int err;
1572
1573 if (optname == MCAST_MSFILTER)
1574 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1575 ip_getsockopt);
1576
Daniel Balutadd23198e2011-08-07 22:31:07 -07001577 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1578 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001579
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001580#ifdef CONFIG_NETFILTER
1581 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001582 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1583 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001584 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001585
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001586 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001587 return -EFAULT;
1588
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001589 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001590 if (err >= 0)
1591 err = put_user(len, optlen);
1592 return err;
1593 }
1594#endif
1595 return err;
1596}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001597EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001598#endif