blob: 89b5f3bd669436f7215703d92aaf7700145cdc8d [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,
101 int offset)
102{
103 __wsum csum = skb->csum;
104
105 if (skb->ip_summed != CHECKSUM_COMPLETE)
106 return;
107
108 if (offset != 0)
109 csum = csum_sub(csum, csum_partial(skb->data, offset, 0));
110
111 put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum);
112}
113
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800114static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
115{
116 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700117 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800118 int err;
119
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700120 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
121 if (err)
122 return;
123
124 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800125 if (err)
126 return;
127
128 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700129 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800130}
131
Harvey Harrison21d1a162008-11-20 01:54:27 -0800132static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800133{
134 struct sockaddr_in sin;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000135 const struct iphdr *iph = ip_hdr(skb);
Harvey Harrison21d1a162008-11-20 01:54:27 -0800136 __be16 *ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800137
138 if (skb_transport_offset(skb) + 4 > skb->len)
139 return;
140
141 /* All current transport protocols have the port numbers in the
142 * first four bytes of the transport header and this function is
143 * written with this assumption in mind.
144 */
145
146 sin.sin_family = AF_INET;
147 sin.sin_addr.s_addr = iph->daddr;
148 sin.sin_port = ports[1];
149 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
150
151 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
152}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Tom Herbert5961de92015-01-05 13:56:16 -0800154void ip_cmsg_recv_offset(struct msghdr *msg, struct sk_buff *skb,
155 int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct inet_sock *inet = inet_sk(skb->sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000158 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 /* Ordered by supposed usage frequency */
Tom Herbertc44d13d2015-01-05 13:56:15 -0800161 if (flags & IP_CMSG_PKTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 ip_cmsg_recv_pktinfo(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Tom Herbertc44d13d2015-01-05 13:56:15 -0800164 flags &= ~IP_CMSG_PKTINFO;
165 if (!flags)
166 return;
167 }
168
169 if (flags & IP_CMSG_TTL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 ip_cmsg_recv_ttl(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Tom Herbertc44d13d2015-01-05 13:56:15 -0800172 flags &= ~IP_CMSG_TTL;
173 if (!flags)
174 return;
175 }
176
177 if (flags & IP_CMSG_TOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 ip_cmsg_recv_tos(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Tom Herbertc44d13d2015-01-05 13:56:15 -0800180 flags &= ~IP_CMSG_TOS;
181 if (!flags)
182 return;
183 }
184
185 if (flags & IP_CMSG_RECVOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 ip_cmsg_recv_opts(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Tom Herbertc44d13d2015-01-05 13:56:15 -0800188 flags &= ~IP_CMSG_RECVOPTS;
189 if (!flags)
190 return;
191 }
192
193 if (flags & IP_CMSG_RETOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 ip_cmsg_recv_retopts(msg, skb);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800195
Tom Herbertc44d13d2015-01-05 13:56:15 -0800196 flags &= ~IP_CMSG_RETOPTS;
197 if (!flags)
198 return;
199 }
200
201 if (flags & IP_CMSG_PASSSEC) {
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800202 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800203
Tom Herbertc44d13d2015-01-05 13:56:15 -0800204 flags &= ~IP_CMSG_PASSSEC;
205 if (!flags)
206 return;
207 }
208
Tom Herbertad6f9392015-01-05 13:56:17 -0800209 if (flags & IP_CMSG_ORIGDSTADDR) {
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800210 ip_cmsg_recv_dstaddr(msg, skb);
211
Tom Herbertad6f9392015-01-05 13:56:17 -0800212 flags &= ~IP_CMSG_ORIGDSTADDR;
213 if (!flags)
214 return;
215 }
216
217 if (flags & IP_CMSG_CHECKSUM)
218 ip_cmsg_recv_checksum(msg, skb, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
Tom Herbert5961de92015-01-05 13:56:16 -0800220EXPORT_SYMBOL(ip_cmsg_recv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400222int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100223 bool allow_ipv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Francesco Fuscof02db312013-09-24 15:43:08 +0200225 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 struct cmsghdr *cmsg;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400227 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Gu Zhengf95b4142014-12-11 11:22:04 +0800229 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (!CMSG_OK(msg, cmsg))
231 return -EINVAL;
Eric Dumazet5337b5b2014-11-10 17:54:25 -0800232#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100233 if (allow_ipv6 &&
234 cmsg->cmsg_level == SOL_IPV6 &&
235 cmsg->cmsg_type == IPV6_PKTINFO) {
236 struct in6_pktinfo *src_info;
237
238 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
239 return -EINVAL;
240 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
241 if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
242 return -EINVAL;
243 ipc->oif = src_info->ipi6_ifindex;
244 ipc->addr = src_info->ipi6_addr.s6_addr32[3];
245 continue;
246 }
247#endif
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400248 if (cmsg->cmsg_level == SOL_SOCKET) {
249 if (__sock_cmsg_send(sk, msg, cmsg, &ipc->sockc))
250 return -EINVAL;
251 continue;
252 }
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (cmsg->cmsg_level != SOL_IP)
255 continue;
256 switch (cmsg->cmsg_type) {
257 case IP_RETOPTS:
258 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
Eric Dumazet91948302016-02-04 06:23:28 -0800259
260 /* Our caller is responsible for freeing ipc->opt */
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700261 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
262 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (err)
264 return err;
265 break;
266 case IP_PKTINFO:
267 {
268 struct in_pktinfo *info;
269 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
270 return -EINVAL;
271 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
272 ipc->oif = info->ipi_ifindex;
273 ipc->addr = info->ipi_spec_dst.s_addr;
274 break;
275 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200276 case IP_TTL:
277 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
278 return -EINVAL;
279 val = *(int *)CMSG_DATA(cmsg);
280 if (val < 1 || val > 255)
281 return -EINVAL;
282 ipc->ttl = val;
283 break;
284 case IP_TOS:
285 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
286 return -EINVAL;
287 val = *(int *)CMSG_DATA(cmsg);
288 if (val < 0 || val > 255)
289 return -EINVAL;
290 ipc->tos = val;
291 ipc->priority = rt_tos2priority(ipc->tos);
292 break;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 default:
295 return -EINVAL;
296 }
297 }
298 return 0;
299}
300
301
302/* Special input handler for packets caught by router alert option.
303 They are selected only by protocol field, and then processed likely
304 local ones; but only if someone wants them! Otherwise, router
305 not running rsvpd will kill RSVP.
306
307 It is user level problem, what it will make with them.
308 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
309 but receiver should be enough clever f.e. to forward mtrace requests,
310 sent to multicast group to reach destination designated router.
311 */
Eric Dumazet43a951e2010-10-25 03:32:44 +0000312struct ip_ra_chain __rcu *ip_ra_chain;
Eric Dumazet66018502010-06-07 03:12:08 +0000313static DEFINE_SPINLOCK(ip_ra_lock);
314
Eric Dumazet592fcb92010-06-09 16:21:07 +0000315
316static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000317{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000318 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
319
320 sock_put(ra->saved_sk);
321 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000322}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700324int ip_ra_control(struct sock *sk, unsigned char on,
325 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000327 struct ip_ra_chain *ra, *new_ra;
328 struct ip_ra_chain __rcu **rap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000330 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return -EINVAL;
332
333 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
334
Eric Dumazet66018502010-06-07 03:12:08 +0000335 spin_lock_bh(&ip_ra_lock);
Eric Dumazet43a951e2010-10-25 03:32:44 +0000336 for (rap = &ip_ra_chain;
337 (ra = rcu_dereference_protected(*rap,
338 lockdep_is_held(&ip_ra_lock))) != NULL;
339 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (ra->sk == sk) {
341 if (on) {
Eric Dumazet66018502010-06-07 03:12:08 +0000342 spin_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800343 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return -EADDRINUSE;
345 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000346 /* dont let ip_call_ra_chain() use sk again */
347 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700348 RCU_INIT_POINTER(*rap, ra->next);
Eric Dumazet66018502010-06-07 03:12:08 +0000349 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 if (ra->destructor)
352 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000353 /*
354 * Delay sock_put(sk) and kfree(ra) after one rcu grace
355 * period. This guarantee ip_call_ra_chain() dont need
356 * to mess with socket refcounts.
357 */
358 ra->saved_sk = sk;
359 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361 }
362 }
Ian Morris51456b22015-04-03 09:17:26 +0100363 if (!new_ra) {
Eric Dumazet66018502010-06-07 03:12:08 +0000364 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return -ENOBUFS;
366 }
367 new_ra->sk = sk;
368 new_ra->destructor = destructor;
369
Eric Dumazet8e380f02014-09-09 08:11:41 -0700370 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000371 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 sock_hold(sk);
Eric Dumazet66018502010-06-07 03:12:08 +0000373 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 return 0;
376}
377
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900378void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700379 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct sock_exterr_skb *serr;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 skb = skb_clone(skb, GFP_ATOMIC);
384 if (!skb)
385 return;
386
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900387 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 serr->ee.ee_errno = err;
389 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300390 serr->ee.ee_type = icmp_hdr(skb)->type;
391 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 serr->ee.ee_pad = 0;
393 serr->ee.ee_info = info;
394 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300395 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700396 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 serr->port = port;
398
Ian Morris00db4122015-04-03 09:17:27 +0100399 if (skb_pull(skb, payload - skb->data)) {
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300400 skb_reset_transport_header(skb);
401 if (sock_queue_err_skb(sk, skb) == 0)
402 return;
403 }
404 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Al Viro05790162006-09-27 18:33:40 -0700407void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 struct inet_sock *inet = inet_sk(sk);
410 struct sock_exterr_skb *serr;
411 struct iphdr *iph;
412 struct sk_buff *skb;
413
414 if (!inet->recverr)
415 return;
416
417 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
418 if (!skb)
419 return;
420
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300421 skb_put(skb, sizeof(struct iphdr));
422 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700423 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 iph->daddr = daddr;
425
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900426 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 serr->ee.ee_errno = err;
428 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900429 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 serr->ee.ee_code = 0;
431 serr->ee.ee_pad = 0;
432 serr->ee.ee_info = info;
433 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700434 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 serr->port = port;
436
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700437 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300438 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if (sock_queue_err_skb(sk, skb))
441 kfree_skb(skb);
442}
443
Julian Anastasov34b99df2015-06-23 08:34:39 +0300444/* For some errors we have valid addr_offset even with zero payload and
445 * zero port. Also, addr_offset should be supported if port is set.
446 */
447static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
448{
449 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
450 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
451}
452
Willem de Bruijnc247f052015-03-07 20:33:22 -0500453/* IPv4 supports cmsg on all imcp errors and some timestamps
454 *
455 * Timestamp code paths do not initialize the fields expected by cmsg:
456 * the PKTINFO fields in skb->cb[]. Fill those in here.
457 */
458static bool ipv4_datagram_support_cmsg(const struct sock *sk,
459 struct sk_buff *skb,
460 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500461{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500462 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500463
Willem de Bruijnc247f052015-03-07 20:33:22 -0500464 if (ee_origin == SO_EE_ORIGIN_ICMP)
465 return true;
466
467 if (ee_origin == SO_EE_ORIGIN_LOCAL)
468 return false;
469
470 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
471 * timestamp with egress dev. Not possible for packets without dev
472 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
473 */
474 if ((!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG)) ||
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500475 (!skb->dev))
476 return false;
477
Willem de Bruijnc247f052015-03-07 20:33:22 -0500478 info = PKTINFO_SKB_CB(skb);
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500479 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
480 info->ipi_ifindex = skb->dev->ifindex;
481 return true;
482}
483
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900484/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * Handle MSG_ERRQUEUE
486 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100487int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400490 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100491 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct {
493 struct sock_extended_err ee;
494 struct sockaddr_in offender;
495 } errhdr;
496 int err;
497 int copied;
498
Willem de Bruijn7ce875e2014-11-30 22:22:33 -0500499 WARN_ON_ONCE(sk->sk_family == AF_INET6);
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400502 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100503 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto out;
505
506 copied = skb->len;
507 if (copied > len) {
508 msg->msg_flags |= MSG_TRUNC;
509 copied = len;
510 }
David S. Miller51f3d022014-11-05 16:46:40 -0500511 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (err)
513 goto out_free_skb;
514
515 sock_recv_timestamp(msg, sk, skb);
516
517 serr = SKB_EXT_ERR(skb);
518
Julian Anastasov34b99df2015-06-23 08:34:39 +0300519 if (sin && ipv4_datagram_support_addr(serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700521 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
522 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 sin->sin_port = serr->port;
524 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100525 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527
528 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
529 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500530 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500531
Willem de Bruijnc247f052015-03-07 20:33:22 -0500532 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700534 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500535 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 ip_cmsg_recv(msg, skb);
537 }
538
539 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
540
541 /* Now we could try to dump offended packet options */
542
543 msg->msg_flags |= MSG_ERRQUEUE;
544 err = copied;
545
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900546out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 kfree_skb(skb);
548out:
549 return err;
550}
551
552
553/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700554 * Socket option code for IP. This is the end of the line after any
555 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300557static bool setsockopt_needs_rtnl(int optname)
558{
559 switch (optname) {
560 case IP_ADD_MEMBERSHIP:
561 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300562 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300563 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300564 case IP_DROP_SOURCE_MEMBERSHIP:
565 case IP_MSFILTER:
566 case IP_UNBLOCK_SOURCE:
567 case MCAST_BLOCK_SOURCE:
568 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300569 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300570 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300571 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300572 case MCAST_LEAVE_SOURCE_GROUP:
573 case MCAST_UNBLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300574 return true;
575 }
576 return false;
577}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800579static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700580 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct inet_sock *inet = inet_sk(sk);
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200583 struct net *net = sock_net(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800584 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300585 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Xi Wang0c9f79b2012-11-11 11:20:01 +0000587 switch (optname) {
588 case IP_PKTINFO:
589 case IP_RECVTTL:
590 case IP_RECVOPTS:
591 case IP_RECVTOS:
592 case IP_RETOPTS:
593 case IP_TOS:
594 case IP_TTL:
595 case IP_HDRINCL:
596 case IP_MTU_DISCOVER:
597 case IP_RECVERR:
598 case IP_ROUTER_ALERT:
599 case IP_FREEBIND:
600 case IP_PASSSEC:
601 case IP_TRANSPARENT:
602 case IP_MINTTL:
603 case IP_NODEFRAG:
Eric Dumazet90c337d2015-06-06 21:17:57 -0700604 case IP_BIND_ADDRESS_NO_PORT:
Xi Wang0c9f79b2012-11-11 11:20:01 +0000605 case IP_UNICAST_IF:
606 case IP_MULTICAST_TTL:
607 case IP_MULTICAST_ALL:
608 case IP_MULTICAST_LOOP:
609 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800610 case IP_CHECKSUM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (optlen >= sizeof(int)) {
612 if (get_user(val, (int __user *) optval))
613 return -EFAULT;
614 } else if (optlen >= sizeof(char)) {
615 unsigned char ucval;
616
617 if (get_user(ucval, (unsigned char __user *) optval))
618 return -EFAULT;
619 val = (int) ucval;
620 }
621 }
622
623 /* If optlen==0, it is equivalent to val == 0 */
624
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800625 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800626 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300629 if (needs_rtnl)
630 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 lock_sock(sk);
632
633 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800634 case IP_OPTIONS:
635 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000636 struct ip_options_rcu *old, *opt = NULL;
637
roel kluin65a1c4f2009-10-23 05:59:21 +0000638 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800639 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900640 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700641 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800642 if (err)
643 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000644 old = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200645 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800646 if (inet->is_icsk) {
647 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000648#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800649 if (sk->sk_family == PF_INET ||
650 (!((1 << sk->sk_state) &
651 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000652 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000654 if (old)
655 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800656 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000657 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800658 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000659#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000663 rcu_assign_pointer(inet->inet_opt, opt);
664 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800665 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800666 break;
667 }
668 case IP_PKTINFO:
669 if (val)
670 inet->cmsg_flags |= IP_CMSG_PKTINFO;
671 else
672 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
673 break;
674 case IP_RECVTTL:
675 if (val)
676 inet->cmsg_flags |= IP_CMSG_TTL;
677 else
678 inet->cmsg_flags &= ~IP_CMSG_TTL;
679 break;
680 case IP_RECVTOS:
681 if (val)
682 inet->cmsg_flags |= IP_CMSG_TOS;
683 else
684 inet->cmsg_flags &= ~IP_CMSG_TOS;
685 break;
686 case IP_RECVOPTS:
687 if (val)
688 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
689 else
690 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
691 break;
692 case IP_RETOPTS:
693 if (val)
694 inet->cmsg_flags |= IP_CMSG_RETOPTS;
695 else
696 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
697 break;
698 case IP_PASSSEC:
699 if (val)
700 inet->cmsg_flags |= IP_CMSG_PASSSEC;
701 else
702 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
703 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800704 case IP_RECVORIGDSTADDR:
705 if (val)
706 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
707 else
708 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
709 break;
Tom Herbertad6f9392015-01-05 13:56:17 -0800710 case IP_CHECKSUM:
711 if (val) {
712 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
713 inet_inc_convert_csum(sk);
714 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
715 }
716 } else {
717 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
718 inet_dec_convert_csum(sk);
719 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
720 }
721 }
722 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800723 case IP_TOS: /* This sets both TOS and Precedence */
724 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400725 val &= ~INET_ECN_MASK;
726 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800728 if (inet->tos != val) {
729 inet->tos = val;
730 sk->sk_priority = rt_tos2priority(val);
731 sk_dst_reset(sk);
732 }
733 break;
734 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700735 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800736 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000737 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800738 goto e_inval;
739 inet->uc_ttl = val;
740 break;
741 case IP_HDRINCL:
742 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800745 }
746 inet->hdrincl = val ? 1 : 0;
747 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000748 case IP_NODEFRAG:
749 if (sk->sk_type != SOCK_RAW) {
750 err = -ENOPROTOOPT;
751 break;
752 }
753 inet->nodefrag = val ? 1 : 0;
754 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -0700755 case IP_BIND_ADDRESS_NO_PORT:
756 inet->bind_address_no_port = val ? 1 : 0;
757 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800758 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100759 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800760 goto e_inval;
761 inet->pmtudisc = val;
762 break;
763 case IP_RECVERR:
764 inet->recverr = !!val;
765 if (!val)
766 skb_queue_purge(&sk->sk_error_queue);
767 break;
768 case IP_MULTICAST_TTL:
769 if (sk->sk_type == SOCK_STREAM)
770 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700771 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800772 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800773 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800774 val = 1;
775 if (val < 0 || val > 255)
776 goto e_inval;
777 inet->mc_ttl = val;
778 break;
779 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700780 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800781 goto e_inval;
782 inet->mc_loop = !!val;
783 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000784 case IP_UNICAST_IF:
785 {
786 struct net_device *dev = NULL;
787 int ifindex;
788
789 if (optlen != sizeof(int))
790 goto e_inval;
791
792 ifindex = (__force int)ntohl((__force __be32)val);
793 if (ifindex == 0) {
794 inet->uc_index = 0;
795 err = 0;
796 break;
797 }
798
799 dev = dev_get_by_index(sock_net(sk), ifindex);
800 err = -EADDRNOTAVAIL;
801 if (!dev)
802 break;
803 dev_put(dev);
804
805 err = -EINVAL;
806 if (sk->sk_bound_dev_if)
807 break;
808
809 inet->uc_index = ifindex;
810 err = 0;
811 break;
812 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800813 case IP_MULTICAST_IF:
814 {
815 struct ip_mreqn mreq;
816 struct net_device *dev = NULL;
817
818 if (sk->sk_type == SOCK_STREAM)
819 goto e_inval;
820 /*
821 * Check the arguments are allowable
822 */
823
Shan Wei09159212009-09-22 15:41:10 +0000824 if (optlen < sizeof(struct in_addr))
825 goto e_inval;
826
Stephen Hemminger132adf52007-03-08 20:44:43 -0800827 err = -EFAULT;
828 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800829 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800830 break;
831 } else {
832 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000833 if (optlen >= sizeof(struct ip_mreq)) {
834 if (copy_from_user(&mreq, optval,
835 sizeof(struct ip_mreq)))
836 break;
837 } else if (optlen >= sizeof(struct in_addr)) {
838 if (copy_from_user(&mreq.imr_address, optval,
839 sizeof(struct in_addr)))
840 break;
841 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800842 }
843
844 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700845 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800846 inet->mc_index = 0;
847 inet->mc_addr = 0;
848 err = 0;
849 break;
850 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900851 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000852 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800853 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800854 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000855 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800856
857
858 err = -EADDRNOTAVAIL;
859 if (!dev)
860 break;
Eric Dumazet55b80502009-10-19 06:41:58 +0000861 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800862
863 err = -EINVAL;
864 if (sk->sk_bound_dev_if &&
865 mreq.imr_ifindex != sk->sk_bound_dev_if)
866 break;
867
868 inet->mc_index = mreq.imr_ifindex;
869 inet->mc_addr = mreq.imr_address.s_addr;
870 err = 0;
871 break;
872 }
873
874 case IP_ADD_MEMBERSHIP:
875 case IP_DROP_MEMBERSHIP:
876 {
877 struct ip_mreqn mreq;
878
Flavio Leitnera96fb492007-08-24 22:16:39 -0700879 err = -EPROTO;
880 if (inet_sk(sk)->is_icsk)
881 break;
882
Stephen Hemminger132adf52007-03-08 20:44:43 -0800883 if (optlen < sizeof(struct ip_mreq))
884 goto e_inval;
885 err = -EFAULT;
886 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800887 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800888 break;
889 } else {
890 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800891 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800892 break;
893 }
894
895 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300896 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800897 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300898 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800899 break;
900 }
901 case IP_MSFILTER:
902 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800903 struct ip_msfilter *msf;
904
905 if (optlen < IP_MSFILTER_SIZE(0))
906 goto e_inval;
907 if (optlen > sysctl_optmem_max) {
908 err = -ENOBUFS;
909 break;
910 }
911 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700912 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800913 err = -ENOBUFS;
914 break;
915 }
916 err = -EFAULT;
917 if (copy_from_user(msf, optval, optlen)) {
918 kfree(msf);
919 break;
920 }
921 /* numsrc >= (1G-4) overflow in 32 bits */
922 if (msf->imsf_numsrc >= 0x3ffffffcU ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200923 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800924 kfree(msf);
925 err = -ENOBUFS;
926 break;
927 }
928 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
929 kfree(msf);
930 err = -EINVAL;
931 break;
932 }
933 err = ip_mc_msfilter(sk, msf, 0);
934 kfree(msf);
935 break;
936 }
937 case IP_BLOCK_SOURCE:
938 case IP_UNBLOCK_SOURCE:
939 case IP_ADD_SOURCE_MEMBERSHIP:
940 case IP_DROP_SOURCE_MEMBERSHIP:
941 {
942 struct ip_mreq_source mreqs;
943 int omode, add;
944
945 if (optlen != sizeof(struct ip_mreq_source))
946 goto e_inval;
947 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
948 err = -EFAULT;
949 break;
950 }
951 if (optname == IP_BLOCK_SOURCE) {
952 omode = MCAST_EXCLUDE;
953 add = 1;
954 } else if (optname == IP_UNBLOCK_SOURCE) {
955 omode = MCAST_EXCLUDE;
956 add = 0;
957 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
958 struct ip_mreqn mreq;
959
960 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
961 mreq.imr_address.s_addr = mreqs.imr_interface;
962 mreq.imr_ifindex = 0;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300963 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800964 if (err && err != -EADDRINUSE)
965 break;
966 omode = MCAST_INCLUDE;
967 add = 1;
968 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
969 omode = MCAST_INCLUDE;
970 add = 0;
971 }
972 err = ip_mc_source(add, omode, sk, &mreqs, 0);
973 break;
974 }
975 case MCAST_JOIN_GROUP:
976 case MCAST_LEAVE_GROUP:
977 {
978 struct group_req greq;
979 struct sockaddr_in *psin;
980 struct ip_mreqn mreq;
981
982 if (optlen < sizeof(struct group_req))
983 goto e_inval;
984 err = -EFAULT;
985 if (copy_from_user(&greq, optval, sizeof(greq)))
986 break;
987 psin = (struct sockaddr_in *)&greq.gr_group;
988 if (psin->sin_family != AF_INET)
989 goto e_inval;
990 memset(&mreq, 0, sizeof(mreq));
991 mreq.imr_multiaddr = psin->sin_addr;
992 mreq.imr_ifindex = greq.gr_interface;
993
994 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300995 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800996 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300997 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800998 break;
999 }
1000 case MCAST_JOIN_SOURCE_GROUP:
1001 case MCAST_LEAVE_SOURCE_GROUP:
1002 case MCAST_BLOCK_SOURCE:
1003 case MCAST_UNBLOCK_SOURCE:
1004 {
1005 struct group_source_req greqs;
1006 struct ip_mreq_source mreqs;
1007 struct sockaddr_in *psin;
1008 int omode, add;
1009
1010 if (optlen != sizeof(struct group_source_req))
1011 goto e_inval;
1012 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
1013 err = -EFAULT;
1014 break;
1015 }
1016 if (greqs.gsr_group.ss_family != AF_INET ||
1017 greqs.gsr_source.ss_family != AF_INET) {
1018 err = -EADDRNOTAVAIL;
1019 break;
1020 }
1021 psin = (struct sockaddr_in *)&greqs.gsr_group;
1022 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
1023 psin = (struct sockaddr_in *)&greqs.gsr_source;
1024 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
1025 mreqs.imr_interface = 0; /* use index for mc_source */
1026
1027 if (optname == MCAST_BLOCK_SOURCE) {
1028 omode = MCAST_EXCLUDE;
1029 add = 1;
1030 } else if (optname == MCAST_UNBLOCK_SOURCE) {
1031 omode = MCAST_EXCLUDE;
1032 add = 0;
1033 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
1034 struct ip_mreqn mreq;
1035
1036 psin = (struct sockaddr_in *)&greqs.gsr_group;
1037 mreq.imr_multiaddr = psin->sin_addr;
1038 mreq.imr_address.s_addr = 0;
1039 mreq.imr_ifindex = greqs.gsr_interface;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001040 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001041 if (err && err != -EADDRINUSE)
1042 break;
1043 greqs.gsr_interface = mreq.imr_ifindex;
1044 omode = MCAST_INCLUDE;
1045 add = 1;
1046 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1047 omode = MCAST_INCLUDE;
1048 add = 0;
1049 }
1050 err = ip_mc_source(add, omode, sk, &mreqs,
1051 greqs.gsr_interface);
1052 break;
1053 }
1054 case MCAST_MSFILTER:
1055 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001056 struct sockaddr_in *psin;
1057 struct ip_msfilter *msf = NULL;
1058 struct group_filter *gsf = NULL;
1059 int msize, i, ifindex;
1060
1061 if (optlen < GROUP_FILTER_SIZE(0))
1062 goto e_inval;
1063 if (optlen > sysctl_optmem_max) {
1064 err = -ENOBUFS;
1065 break;
1066 }
Jianjun Kong09cb1052008-11-03 00:27:11 -08001067 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001068 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001069 err = -ENOBUFS;
1070 break;
1071 }
1072 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001073 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001074 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001075
Stephen Hemminger132adf52007-03-08 20:44:43 -08001076 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1077 if (gsf->gf_numsrc >= 0x1ffffff ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +02001078 gsf->gf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001079 err = -ENOBUFS;
1080 goto mc_msf_out;
1081 }
1082 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
1083 err = -EINVAL;
1084 goto mc_msf_out;
1085 }
1086 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -08001087 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001088 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001089 err = -ENOBUFS;
1090 goto mc_msf_out;
1091 }
1092 ifindex = gsf->gf_interface;
1093 psin = (struct sockaddr_in *)&gsf->gf_group;
1094 if (psin->sin_family != AF_INET) {
1095 err = -EADDRNOTAVAIL;
1096 goto mc_msf_out;
1097 }
1098 msf->imsf_multiaddr = psin->sin_addr.s_addr;
1099 msf->imsf_interface = 0;
1100 msf->imsf_fmode = gsf->gf_fmode;
1101 msf->imsf_numsrc = gsf->gf_numsrc;
1102 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001103 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001104 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
1105
1106 if (psin->sin_family != AF_INET)
1107 goto mc_msf_out;
1108 msf->imsf_slist[i] = psin->sin_addr.s_addr;
1109 }
1110 kfree(gsf);
1111 gsf = NULL;
1112
1113 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001114mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -08001115 kfree(msf);
1116 kfree(gsf);
1117 break;
1118 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001119 case IP_MULTICAST_ALL:
1120 if (optlen < 1)
1121 goto e_inval;
1122 if (val != 0 && val != 1)
1123 goto e_inval;
1124 inet->mc_all = val;
1125 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001126 case IP_ROUTER_ALERT:
1127 err = ip_ra_control(sk, val ? 1 : 0, NULL);
1128 break;
1129
1130 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001131 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001132 goto e_inval;
1133 inet->freebind = !!val;
1134 break;
1135
1136 case IP_IPSEC_POLICY:
1137 case IP_XFRM_POLICY:
1138 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001139 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001140 break;
1141 err = xfrm_user_policy(sk, optname, optval, optlen);
1142 break;
1143
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001144 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001145 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1146 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001147 err = -EPERM;
1148 break;
1149 }
1150 if (optlen < 1)
1151 goto e_inval;
1152 inet->transparent = !!val;
1153 break;
1154
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001155 case IP_MINTTL:
1156 if (optlen < 1)
1157 goto e_inval;
1158 if (val < 0 || val > 255)
1159 goto e_inval;
1160 inet->min_ttl = val;
1161 break;
1162
Stephen Hemminger132adf52007-03-08 20:44:43 -08001163 default:
1164 err = -ENOPROTOOPT;
1165 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001168 if (needs_rtnl)
1169 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return err;
1171
1172e_inval:
1173 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001174 if (needs_rtnl)
1175 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return -EINVAL;
1177}
1178
Eric Dumazetf84af322010-04-28 15:31:51 -07001179/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001180 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001181 * @sk: socket
1182 * @skb: buffer
1183 *
David S. Miller35ebf652012-06-28 03:59:11 -07001184 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1185 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001186 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001187 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001188void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001189{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001190 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001191 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1192 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001193
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001194 if (prepare && skb_rtable(skb)) {
David S. Miller92101b32012-07-23 16:29:00 -07001195 pktinfo->ipi_ifindex = inet_iif(skb);
David S. Miller35ebf652012-06-28 03:59:11 -07001196 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001197 } else {
1198 pktinfo->ipi_ifindex = 0;
1199 pktinfo->ipi_spec_dst.s_addr = 0;
1200 }
1201 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001202}
Eric Dumazetf84af322010-04-28 15:31:51 -07001203
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001204int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001205 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001206{
1207 int err;
1208
1209 if (level != SOL_IP)
1210 return -ENOPROTOOPT;
1211
1212 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1213#ifdef CONFIG_NETFILTER
1214 /* we need to exclude all possible ENOPROTOOPTs except default case */
1215 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001216 optname != IP_IPSEC_POLICY &&
1217 optname != IP_XFRM_POLICY &&
1218 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001219 lock_sock(sk);
1220 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1221 release_sock(sk);
1222 }
1223#endif
1224 return err;
1225}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001226EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001227
1228#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001229int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001230 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001231{
1232 int err;
1233
1234 if (level != SOL_IP)
1235 return -ENOPROTOOPT;
1236
David L Stevensdae50292008-04-27 01:06:07 -07001237 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1238 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1239 ip_setsockopt);
1240
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001241 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1242#ifdef CONFIG_NETFILTER
1243 /* we need to exclude all possible ENOPROTOOPTs except default case */
1244 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001245 optname != IP_IPSEC_POLICY &&
1246 optname != IP_XFRM_POLICY &&
1247 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001248 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001249 err = compat_nf_setsockopt(sk, PF_INET, optname,
1250 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001251 release_sock(sk);
1252 }
1253#endif
1254 return err;
1255}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001256EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001257#endif
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001260 * Get the options. Note for future reference. The GET of IP options gets
1261 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 */
1263
WANG Cong87e9f032015-11-03 15:41:16 -08001264static bool getsockopt_needs_rtnl(int optname)
1265{
1266 switch (optname) {
1267 case IP_MSFILTER:
1268 case MCAST_MSFILTER:
1269 return true;
1270 }
1271 return false;
1272}
1273
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001274static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001275 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 struct inet_sock *inet = inet_sk(sk);
WANG Cong87e9f032015-11-03 15:41:16 -08001278 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1279 int val, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001281
Stephen Hemminger132adf52007-03-08 20:44:43 -08001282 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return -EOPNOTSUPP;
1284
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001285 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001286 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Jianjun Kong09cb1052008-11-03 00:27:11 -08001288 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001290 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001292
WANG Cong87e9f032015-11-03 15:41:16 -08001293 if (needs_rtnl)
1294 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 lock_sock(sk);
1296
Stephen Hemminger132adf52007-03-08 20:44:43 -08001297 switch (optname) {
1298 case IP_OPTIONS:
1299 {
1300 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001301 struct ip_options *opt = (struct ip_options *)optbuf;
1302 struct ip_options_rcu *inet_opt;
1303
1304 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001305 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001306 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001307 if (inet_opt)
1308 memcpy(optbuf, &inet_opt->opt,
1309 sizeof(struct ip_options) +
1310 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001311 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Stephen Hemminger132adf52007-03-08 20:44:43 -08001313 if (opt->optlen == 0)
1314 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Stephen Hemminger132adf52007-03-08 20:44:43 -08001316 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Stephen Hemminger132adf52007-03-08 20:44:43 -08001318 len = min_t(unsigned int, len, opt->optlen);
1319 if (put_user(len, optlen))
1320 return -EFAULT;
1321 if (copy_to_user(optval, opt->__data, len))
1322 return -EFAULT;
1323 return 0;
1324 }
1325 case IP_PKTINFO:
1326 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1327 break;
1328 case IP_RECVTTL:
1329 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1330 break;
1331 case IP_RECVTOS:
1332 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1333 break;
1334 case IP_RECVOPTS:
1335 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1336 break;
1337 case IP_RETOPTS:
1338 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1339 break;
1340 case IP_PASSSEC:
1341 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1342 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001343 case IP_RECVORIGDSTADDR:
1344 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1345 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001346 case IP_CHECKSUM:
1347 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1348 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001349 case IP_TOS:
1350 val = inet->tos;
1351 break;
1352 case IP_TTL:
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001353 {
1354 struct net *net = sock_net(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001355 val = (inet->uc_ttl == -1 ?
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001356 net->ipv4.sysctl_ip_default_ttl :
Stephen Hemminger132adf52007-03-08 20:44:43 -08001357 inet->uc_ttl);
1358 break;
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001359 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001360 case IP_HDRINCL:
1361 val = inet->hdrincl;
1362 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001363 case IP_NODEFRAG:
1364 val = inet->nodefrag;
1365 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001366 case IP_BIND_ADDRESS_NO_PORT:
1367 val = inet->bind_address_no_port;
1368 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001369 case IP_MTU_DISCOVER:
1370 val = inet->pmtudisc;
1371 break;
1372 case IP_MTU:
1373 {
1374 struct dst_entry *dst;
1375 val = 0;
1376 dst = sk_dst_get(sk);
1377 if (dst) {
1378 val = dst_mtu(dst);
1379 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001381 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001383 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001385 break;
1386 }
1387 case IP_RECVERR:
1388 val = inet->recverr;
1389 break;
1390 case IP_MULTICAST_TTL:
1391 val = inet->mc_ttl;
1392 break;
1393 case IP_MULTICAST_LOOP:
1394 val = inet->mc_loop;
1395 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001396 case IP_UNICAST_IF:
1397 val = (__force int)htonl((__u32) inet->uc_index);
1398 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001399 case IP_MULTICAST_IF:
1400 {
1401 struct in_addr addr;
1402 len = min_t(unsigned int, len, sizeof(struct in_addr));
1403 addr.s_addr = inet->mc_addr;
1404 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Stephen Hemminger132adf52007-03-08 20:44:43 -08001406 if (put_user(len, optlen))
1407 return -EFAULT;
1408 if (copy_to_user(optval, &addr, len))
1409 return -EFAULT;
1410 return 0;
1411 }
1412 case IP_MSFILTER:
1413 {
1414 struct ip_msfilter msf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001415
1416 if (len < IP_MSFILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001417 err = -EINVAL;
1418 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001420 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001421 err = -EFAULT;
1422 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001424 err = ip_mc_msfget(sk, &msf,
1425 (struct ip_msfilter __user *)optval, optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001426 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001427 }
1428 case MCAST_MSFILTER:
1429 {
1430 struct group_filter gsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Stephen Hemminger132adf52007-03-08 20:44:43 -08001432 if (len < GROUP_FILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001433 err = -EINVAL;
1434 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001436 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001437 err = -EFAULT;
1438 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001439 }
1440 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001441 (struct group_filter __user *)optval,
1442 optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001443 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001444 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001445 case IP_MULTICAST_ALL:
1446 val = inet->mc_all;
1447 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001448 case IP_PKTOPTIONS:
1449 {
1450 struct msghdr msg;
1451
1452 release_sock(sk);
1453
1454 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001456
Karoly Kemenyc54a5e02014-07-27 12:29:07 +02001457 msg.msg_control = (__force void *) optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001458 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001459 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001460
1461 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1462 struct in_pktinfo info;
1463
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001464 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1465 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001466 info.ipi_ifindex = inet->mc_index;
1467 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1468 }
1469 if (inet->cmsg_flags & IP_CMSG_TTL) {
1470 int hlim = inet->mc_ttl;
1471 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1472 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001473 if (inet->cmsg_flags & IP_CMSG_TOS) {
1474 int tos = inet->rcv_tos;
1475 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1476 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001477 len -= msg.msg_controllen;
1478 return put_user(len, optlen);
1479 }
1480 case IP_FREEBIND:
1481 val = inet->freebind;
1482 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001483 case IP_TRANSPARENT:
1484 val = inet->transparent;
1485 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001486 case IP_MINTTL:
1487 val = inet->min_ttl;
1488 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001489 default:
1490 release_sock(sk);
1491 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
1493 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001494
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001495 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 unsigned char ucval = (unsigned char)val;
1497 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001498 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001500 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return -EFAULT;
1502 } else {
1503 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001504 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001506 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return -EFAULT;
1508 }
1509 return 0;
WANG Cong87e9f032015-11-03 15:41:16 -08001510
1511out:
1512 release_sock(sk);
1513 if (needs_rtnl)
1514 rtnl_unlock();
1515 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001518int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001519 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001520{
1521 int err;
1522
Daniel Balutadd23198e2011-08-07 22:31:07 -07001523 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001524#ifdef CONFIG_NETFILTER
1525 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001526 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1527 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001528 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001529
Jianjun Kong09cb1052008-11-03 00:27:11 -08001530 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001531 return -EFAULT;
1532
1533 lock_sock(sk);
1534 err = nf_getsockopt(sk, PF_INET, optname, optval,
1535 &len);
1536 release_sock(sk);
1537 if (err >= 0)
1538 err = put_user(len, optlen);
1539 return err;
1540 }
1541#endif
1542 return err;
1543}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001544EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001545
1546#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001547int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1548 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001549{
David L Stevens42908c62008-04-29 03:23:22 -07001550 int err;
1551
1552 if (optname == MCAST_MSFILTER)
1553 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1554 ip_getsockopt);
1555
Daniel Balutadd23198e2011-08-07 22:31:07 -07001556 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1557 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001558
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001559#ifdef CONFIG_NETFILTER
1560 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001561 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1562 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001563 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001564
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001565 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001566 return -EFAULT;
1567
1568 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001569 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001570 release_sock(sk);
1571 if (err >= 0)
1572 err = put_user(len, optlen);
1573 return err;
1574 }
1575#endif
1576 return err;
1577}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001578EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001579#endif