blob: 71a52f4d4cffba2db9353f43dc817689bf4fab10 [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)
Willem de Bruijn31c2e492016-04-07 18:12:59 -0400109 csum = csum_sub(csum, csum_partial(skb_transport_header(skb),
110 offset, 0));
Tom Herbertad6f9392015-01-05 13:56:17 -0800111
112 put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum);
113}
114
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800115static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
116{
117 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700118 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800119 int err;
120
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700121 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
122 if (err)
123 return;
124
125 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800126 if (err)
127 return;
128
129 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700130 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800131}
132
Harvey Harrison21d1a162008-11-20 01:54:27 -0800133static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800134{
135 struct sockaddr_in sin;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000136 const struct iphdr *iph = ip_hdr(skb);
Harvey Harrison21d1a162008-11-20 01:54:27 -0800137 __be16 *ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800138
139 if (skb_transport_offset(skb) + 4 > skb->len)
140 return;
141
142 /* All current transport protocols have the port numbers in the
143 * first four bytes of the transport header and this function is
144 * written with this assumption in mind.
145 */
146
147 sin.sin_family = AF_INET;
148 sin.sin_addr.s_addr = iph->daddr;
149 sin.sin_port = ports[1];
150 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
151
152 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
153}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Tom Herbert5961de92015-01-05 13:56:16 -0800155void ip_cmsg_recv_offset(struct msghdr *msg, struct sk_buff *skb,
156 int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 struct inet_sock *inet = inet_sk(skb->sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000159 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 /* Ordered by supposed usage frequency */
Tom Herbertc44d13d2015-01-05 13:56:15 -0800162 if (flags & IP_CMSG_PKTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 ip_cmsg_recv_pktinfo(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Tom Herbertc44d13d2015-01-05 13:56:15 -0800165 flags &= ~IP_CMSG_PKTINFO;
166 if (!flags)
167 return;
168 }
169
170 if (flags & IP_CMSG_TTL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 ip_cmsg_recv_ttl(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Tom Herbertc44d13d2015-01-05 13:56:15 -0800173 flags &= ~IP_CMSG_TTL;
174 if (!flags)
175 return;
176 }
177
178 if (flags & IP_CMSG_TOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 ip_cmsg_recv_tos(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Tom Herbertc44d13d2015-01-05 13:56:15 -0800181 flags &= ~IP_CMSG_TOS;
182 if (!flags)
183 return;
184 }
185
186 if (flags & IP_CMSG_RECVOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 ip_cmsg_recv_opts(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Tom Herbertc44d13d2015-01-05 13:56:15 -0800189 flags &= ~IP_CMSG_RECVOPTS;
190 if (!flags)
191 return;
192 }
193
194 if (flags & IP_CMSG_RETOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 ip_cmsg_recv_retopts(msg, skb);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800196
Tom Herbertc44d13d2015-01-05 13:56:15 -0800197 flags &= ~IP_CMSG_RETOPTS;
198 if (!flags)
199 return;
200 }
201
202 if (flags & IP_CMSG_PASSSEC) {
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800203 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800204
Tom Herbertc44d13d2015-01-05 13:56:15 -0800205 flags &= ~IP_CMSG_PASSSEC;
206 if (!flags)
207 return;
208 }
209
Tom Herbertad6f9392015-01-05 13:56:17 -0800210 if (flags & IP_CMSG_ORIGDSTADDR) {
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800211 ip_cmsg_recv_dstaddr(msg, skb);
212
Tom Herbertad6f9392015-01-05 13:56:17 -0800213 flags &= ~IP_CMSG_ORIGDSTADDR;
214 if (!flags)
215 return;
216 }
217
218 if (flags & IP_CMSG_CHECKSUM)
219 ip_cmsg_recv_checksum(msg, skb, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
Tom Herbert5961de92015-01-05 13:56:16 -0800221EXPORT_SYMBOL(ip_cmsg_recv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400223int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100224 bool allow_ipv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Francesco Fuscof02db312013-09-24 15:43:08 +0200226 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct cmsghdr *cmsg;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400228 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Gu Zhengf95b4142014-12-11 11:22:04 +0800230 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (!CMSG_OK(msg, cmsg))
232 return -EINVAL;
Eric Dumazet5337b5b2014-11-10 17:54:25 -0800233#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100234 if (allow_ipv6 &&
235 cmsg->cmsg_level == SOL_IPV6 &&
236 cmsg->cmsg_type == IPV6_PKTINFO) {
237 struct in6_pktinfo *src_info;
238
239 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
240 return -EINVAL;
241 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
242 if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
243 return -EINVAL;
244 ipc->oif = src_info->ipi6_ifindex;
245 ipc->addr = src_info->ipi6_addr.s6_addr32[3];
246 continue;
247 }
248#endif
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400249 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700250 err = __sock_cmsg_send(sk, msg, cmsg, &ipc->sockc);
251 if (err)
252 return err;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400253 continue;
254 }
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (cmsg->cmsg_level != SOL_IP)
257 continue;
258 switch (cmsg->cmsg_type) {
259 case IP_RETOPTS:
260 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
Eric Dumazet91948302016-02-04 06:23:28 -0800261
262 /* Our caller is responsible for freeing ipc->opt */
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700263 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
264 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (err)
266 return err;
267 break;
268 case IP_PKTINFO:
269 {
270 struct in_pktinfo *info;
271 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
272 return -EINVAL;
273 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
274 ipc->oif = info->ipi_ifindex;
275 ipc->addr = info->ipi_spec_dst.s_addr;
276 break;
277 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200278 case IP_TTL:
279 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
280 return -EINVAL;
281 val = *(int *)CMSG_DATA(cmsg);
282 if (val < 1 || val > 255)
283 return -EINVAL;
284 ipc->ttl = val;
285 break;
286 case IP_TOS:
287 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
288 return -EINVAL;
289 val = *(int *)CMSG_DATA(cmsg);
290 if (val < 0 || val > 255)
291 return -EINVAL;
292 ipc->tos = val;
293 ipc->priority = rt_tos2priority(ipc->tos);
294 break;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 default:
297 return -EINVAL;
298 }
299 }
300 return 0;
301}
302
303
304/* Special input handler for packets caught by router alert option.
305 They are selected only by protocol field, and then processed likely
306 local ones; but only if someone wants them! Otherwise, router
307 not running rsvpd will kill RSVP.
308
309 It is user level problem, what it will make with them.
310 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
311 but receiver should be enough clever f.e. to forward mtrace requests,
312 sent to multicast group to reach destination designated router.
313 */
Eric Dumazet43a951e2010-10-25 03:32:44 +0000314struct ip_ra_chain __rcu *ip_ra_chain;
Eric Dumazet66018502010-06-07 03:12:08 +0000315static DEFINE_SPINLOCK(ip_ra_lock);
316
Eric Dumazet592fcb92010-06-09 16:21:07 +0000317
318static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000319{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000320 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
321
322 sock_put(ra->saved_sk);
323 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000324}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700326int ip_ra_control(struct sock *sk, unsigned char on,
327 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000329 struct ip_ra_chain *ra, *new_ra;
330 struct ip_ra_chain __rcu **rap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000332 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return -EINVAL;
334
335 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
336
Eric Dumazet66018502010-06-07 03:12:08 +0000337 spin_lock_bh(&ip_ra_lock);
Eric Dumazet43a951e2010-10-25 03:32:44 +0000338 for (rap = &ip_ra_chain;
339 (ra = rcu_dereference_protected(*rap,
340 lockdep_is_held(&ip_ra_lock))) != NULL;
341 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (ra->sk == sk) {
343 if (on) {
Eric Dumazet66018502010-06-07 03:12:08 +0000344 spin_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800345 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return -EADDRINUSE;
347 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000348 /* dont let ip_call_ra_chain() use sk again */
349 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700350 RCU_INIT_POINTER(*rap, ra->next);
Eric Dumazet66018502010-06-07 03:12:08 +0000351 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 if (ra->destructor)
354 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000355 /*
356 * Delay sock_put(sk) and kfree(ra) after one rcu grace
357 * period. This guarantee ip_call_ra_chain() dont need
358 * to mess with socket refcounts.
359 */
360 ra->saved_sk = sk;
361 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return 0;
363 }
364 }
Ian Morris51456b22015-04-03 09:17:26 +0100365 if (!new_ra) {
Eric Dumazet66018502010-06-07 03:12:08 +0000366 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return -ENOBUFS;
368 }
369 new_ra->sk = sk;
370 new_ra->destructor = destructor;
371
Eric Dumazet8e380f02014-09-09 08:11:41 -0700372 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000373 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 sock_hold(sk);
Eric Dumazet66018502010-06-07 03:12:08 +0000375 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 return 0;
378}
379
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900380void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700381 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct sock_exterr_skb *serr;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 skb = skb_clone(skb, GFP_ATOMIC);
386 if (!skb)
387 return;
388
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900389 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 serr->ee.ee_errno = err;
391 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300392 serr->ee.ee_type = icmp_hdr(skb)->type;
393 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 serr->ee.ee_pad = 0;
395 serr->ee.ee_info = info;
396 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300397 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700398 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 serr->port = port;
400
Ian Morris00db4122015-04-03 09:17:27 +0100401 if (skb_pull(skb, payload - skb->data)) {
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300402 skb_reset_transport_header(skb);
403 if (sock_queue_err_skb(sk, skb) == 0)
404 return;
405 }
406 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Al Viro05790162006-09-27 18:33:40 -0700409void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 struct inet_sock *inet = inet_sk(sk);
412 struct sock_exterr_skb *serr;
413 struct iphdr *iph;
414 struct sk_buff *skb;
415
416 if (!inet->recverr)
417 return;
418
419 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
420 if (!skb)
421 return;
422
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300423 skb_put(skb, sizeof(struct iphdr));
424 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700425 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 iph->daddr = daddr;
427
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900428 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 serr->ee.ee_errno = err;
430 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900431 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 serr->ee.ee_code = 0;
433 serr->ee.ee_pad = 0;
434 serr->ee.ee_info = info;
435 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700436 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 serr->port = port;
438
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700439 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300440 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 if (sock_queue_err_skb(sk, skb))
443 kfree_skb(skb);
444}
445
Julian Anastasov34b99df2015-06-23 08:34:39 +0300446/* For some errors we have valid addr_offset even with zero payload and
447 * zero port. Also, addr_offset should be supported if port is set.
448 */
449static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
450{
451 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
452 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
453}
454
Willem de Bruijnc247f052015-03-07 20:33:22 -0500455/* IPv4 supports cmsg on all imcp errors and some timestamps
456 *
457 * Timestamp code paths do not initialize the fields expected by cmsg:
458 * the PKTINFO fields in skb->cb[]. Fill those in here.
459 */
460static bool ipv4_datagram_support_cmsg(const struct sock *sk,
461 struct sk_buff *skb,
462 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500463{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500464 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500465
Willem de Bruijnc247f052015-03-07 20:33:22 -0500466 if (ee_origin == SO_EE_ORIGIN_ICMP)
467 return true;
468
469 if (ee_origin == SO_EE_ORIGIN_LOCAL)
470 return false;
471
472 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
473 * timestamp with egress dev. Not possible for packets without dev
474 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
475 */
476 if ((!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG)) ||
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500477 (!skb->dev))
478 return false;
479
Willem de Bruijnc247f052015-03-07 20:33:22 -0500480 info = PKTINFO_SKB_CB(skb);
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500481 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
482 info->ipi_ifindex = skb->dev->ifindex;
483 return true;
484}
485
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900486/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * Handle MSG_ERRQUEUE
488 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100489int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400492 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100493 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct {
495 struct sock_extended_err ee;
496 struct sockaddr_in offender;
497 } errhdr;
498 int err;
499 int copied;
500
Willem de Bruijn7ce875e2014-11-30 22:22:33 -0500501 WARN_ON_ONCE(sk->sk_family == AF_INET6);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400504 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100505 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto out;
507
508 copied = skb->len;
509 if (copied > len) {
510 msg->msg_flags |= MSG_TRUNC;
511 copied = len;
512 }
David S. Miller51f3d022014-11-05 16:46:40 -0500513 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700514 if (unlikely(err)) {
515 kfree_skb(skb);
516 return err;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 sock_recv_timestamp(msg, sk, skb);
519
520 serr = SKB_EXT_ERR(skb);
521
Julian Anastasov34b99df2015-06-23 08:34:39 +0300522 if (sin && ipv4_datagram_support_addr(serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700524 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
525 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 sin->sin_port = serr->port;
527 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100528 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
531 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
532 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500533 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500534
Willem de Bruijnc247f052015-03-07 20:33:22 -0500535 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700537 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500538 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 ip_cmsg_recv(msg, skb);
540 }
541
542 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
543
544 /* Now we could try to dump offended packet options */
545
546 msg->msg_flags |= MSG_ERRQUEUE;
547 err = copied;
548
Eric Dumazet960a2622016-04-21 22:27:32 -0700549 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550out:
551 return err;
552}
553
554
555/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700556 * Socket option code for IP. This is the end of the line after any
557 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300559static bool setsockopt_needs_rtnl(int optname)
560{
561 switch (optname) {
562 case IP_ADD_MEMBERSHIP:
563 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300564 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300565 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300566 case IP_DROP_SOURCE_MEMBERSHIP:
567 case IP_MSFILTER:
568 case IP_UNBLOCK_SOURCE:
569 case MCAST_BLOCK_SOURCE:
570 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300571 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300572 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300573 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300574 case MCAST_LEAVE_SOURCE_GROUP:
575 case MCAST_UNBLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300576 return true;
577 }
578 return false;
579}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800581static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700582 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 struct inet_sock *inet = inet_sk(sk);
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200585 struct net *net = sock_net(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800586 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300587 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Xi Wang0c9f79b2012-11-11 11:20:01 +0000589 switch (optname) {
590 case IP_PKTINFO:
591 case IP_RECVTTL:
592 case IP_RECVOPTS:
593 case IP_RECVTOS:
594 case IP_RETOPTS:
595 case IP_TOS:
596 case IP_TTL:
597 case IP_HDRINCL:
598 case IP_MTU_DISCOVER:
599 case IP_RECVERR:
600 case IP_ROUTER_ALERT:
601 case IP_FREEBIND:
602 case IP_PASSSEC:
603 case IP_TRANSPARENT:
604 case IP_MINTTL:
605 case IP_NODEFRAG:
Eric Dumazet90c337d2015-06-06 21:17:57 -0700606 case IP_BIND_ADDRESS_NO_PORT:
Xi Wang0c9f79b2012-11-11 11:20:01 +0000607 case IP_UNICAST_IF:
608 case IP_MULTICAST_TTL:
609 case IP_MULTICAST_ALL:
610 case IP_MULTICAST_LOOP:
611 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800612 case IP_CHECKSUM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (optlen >= sizeof(int)) {
614 if (get_user(val, (int __user *) optval))
615 return -EFAULT;
616 } else if (optlen >= sizeof(char)) {
617 unsigned char ucval;
618
619 if (get_user(ucval, (unsigned char __user *) optval))
620 return -EFAULT;
621 val = (int) ucval;
622 }
623 }
624
625 /* If optlen==0, it is equivalent to val == 0 */
626
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800627 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800628 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300631 if (needs_rtnl)
632 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 lock_sock(sk);
634
635 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800636 case IP_OPTIONS:
637 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000638 struct ip_options_rcu *old, *opt = NULL;
639
roel kluin65a1c4f2009-10-23 05:59:21 +0000640 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800641 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900642 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700643 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800644 if (err)
645 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000646 old = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200647 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800648 if (inet->is_icsk) {
649 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000650#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800651 if (sk->sk_family == PF_INET ||
652 (!((1 << sk->sk_state) &
653 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000654 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000656 if (old)
657 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800658 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000659 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800660 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000661#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000665 rcu_assign_pointer(inet->inet_opt, opt);
666 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800667 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800668 break;
669 }
670 case IP_PKTINFO:
671 if (val)
672 inet->cmsg_flags |= IP_CMSG_PKTINFO;
673 else
674 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
675 break;
676 case IP_RECVTTL:
677 if (val)
678 inet->cmsg_flags |= IP_CMSG_TTL;
679 else
680 inet->cmsg_flags &= ~IP_CMSG_TTL;
681 break;
682 case IP_RECVTOS:
683 if (val)
684 inet->cmsg_flags |= IP_CMSG_TOS;
685 else
686 inet->cmsg_flags &= ~IP_CMSG_TOS;
687 break;
688 case IP_RECVOPTS:
689 if (val)
690 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
691 else
692 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
693 break;
694 case IP_RETOPTS:
695 if (val)
696 inet->cmsg_flags |= IP_CMSG_RETOPTS;
697 else
698 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
699 break;
700 case IP_PASSSEC:
701 if (val)
702 inet->cmsg_flags |= IP_CMSG_PASSSEC;
703 else
704 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
705 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800706 case IP_RECVORIGDSTADDR:
707 if (val)
708 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
709 else
710 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
711 break;
Tom Herbertad6f9392015-01-05 13:56:17 -0800712 case IP_CHECKSUM:
713 if (val) {
714 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
715 inet_inc_convert_csum(sk);
716 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
717 }
718 } else {
719 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
720 inet_dec_convert_csum(sk);
721 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
722 }
723 }
724 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800725 case IP_TOS: /* This sets both TOS and Precedence */
726 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400727 val &= ~INET_ECN_MASK;
728 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800730 if (inet->tos != val) {
731 inet->tos = val;
732 sk->sk_priority = rt_tos2priority(val);
733 sk_dst_reset(sk);
734 }
735 break;
736 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700737 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800738 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000739 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800740 goto e_inval;
741 inet->uc_ttl = val;
742 break;
743 case IP_HDRINCL:
744 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800747 }
748 inet->hdrincl = val ? 1 : 0;
749 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000750 case IP_NODEFRAG:
751 if (sk->sk_type != SOCK_RAW) {
752 err = -ENOPROTOOPT;
753 break;
754 }
755 inet->nodefrag = val ? 1 : 0;
756 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -0700757 case IP_BIND_ADDRESS_NO_PORT:
758 inet->bind_address_no_port = val ? 1 : 0;
759 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800760 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100761 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800762 goto e_inval;
763 inet->pmtudisc = val;
764 break;
765 case IP_RECVERR:
766 inet->recverr = !!val;
767 if (!val)
768 skb_queue_purge(&sk->sk_error_queue);
769 break;
770 case IP_MULTICAST_TTL:
771 if (sk->sk_type == SOCK_STREAM)
772 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700773 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800774 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800775 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800776 val = 1;
777 if (val < 0 || val > 255)
778 goto e_inval;
779 inet->mc_ttl = val;
780 break;
781 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700782 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800783 goto e_inval;
784 inet->mc_loop = !!val;
785 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000786 case IP_UNICAST_IF:
787 {
788 struct net_device *dev = NULL;
789 int ifindex;
790
791 if (optlen != sizeof(int))
792 goto e_inval;
793
794 ifindex = (__force int)ntohl((__force __be32)val);
795 if (ifindex == 0) {
796 inet->uc_index = 0;
797 err = 0;
798 break;
799 }
800
801 dev = dev_get_by_index(sock_net(sk), ifindex);
802 err = -EADDRNOTAVAIL;
803 if (!dev)
804 break;
805 dev_put(dev);
806
807 err = -EINVAL;
808 if (sk->sk_bound_dev_if)
809 break;
810
811 inet->uc_index = ifindex;
812 err = 0;
813 break;
814 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800815 case IP_MULTICAST_IF:
816 {
817 struct ip_mreqn mreq;
818 struct net_device *dev = NULL;
819
820 if (sk->sk_type == SOCK_STREAM)
821 goto e_inval;
822 /*
823 * Check the arguments are allowable
824 */
825
Shan Wei09159212009-09-22 15:41:10 +0000826 if (optlen < sizeof(struct in_addr))
827 goto e_inval;
828
Stephen Hemminger132adf52007-03-08 20:44:43 -0800829 err = -EFAULT;
830 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800831 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800832 break;
833 } else {
834 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000835 if (optlen >= sizeof(struct ip_mreq)) {
836 if (copy_from_user(&mreq, optval,
837 sizeof(struct ip_mreq)))
838 break;
839 } else if (optlen >= sizeof(struct in_addr)) {
840 if (copy_from_user(&mreq.imr_address, optval,
841 sizeof(struct in_addr)))
842 break;
843 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800844 }
845
846 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700847 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800848 inet->mc_index = 0;
849 inet->mc_addr = 0;
850 err = 0;
851 break;
852 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900853 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000854 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800855 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800856 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000857 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800858
859
860 err = -EADDRNOTAVAIL;
861 if (!dev)
862 break;
Eric Dumazet55b80502009-10-19 06:41:58 +0000863 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800864
865 err = -EINVAL;
866 if (sk->sk_bound_dev_if &&
867 mreq.imr_ifindex != sk->sk_bound_dev_if)
868 break;
869
870 inet->mc_index = mreq.imr_ifindex;
871 inet->mc_addr = mreq.imr_address.s_addr;
872 err = 0;
873 break;
874 }
875
876 case IP_ADD_MEMBERSHIP:
877 case IP_DROP_MEMBERSHIP:
878 {
879 struct ip_mreqn mreq;
880
Flavio Leitnera96fb492007-08-24 22:16:39 -0700881 err = -EPROTO;
882 if (inet_sk(sk)->is_icsk)
883 break;
884
Stephen Hemminger132adf52007-03-08 20:44:43 -0800885 if (optlen < sizeof(struct ip_mreq))
886 goto e_inval;
887 err = -EFAULT;
888 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800889 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800890 break;
891 } else {
892 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800893 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800894 break;
895 }
896
897 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300898 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800899 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300900 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800901 break;
902 }
903 case IP_MSFILTER:
904 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800905 struct ip_msfilter *msf;
906
907 if (optlen < IP_MSFILTER_SIZE(0))
908 goto e_inval;
909 if (optlen > sysctl_optmem_max) {
910 err = -ENOBUFS;
911 break;
912 }
913 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700914 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800915 err = -ENOBUFS;
916 break;
917 }
918 err = -EFAULT;
919 if (copy_from_user(msf, optval, optlen)) {
920 kfree(msf);
921 break;
922 }
923 /* numsrc >= (1G-4) overflow in 32 bits */
924 if (msf->imsf_numsrc >= 0x3ffffffcU ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200925 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800926 kfree(msf);
927 err = -ENOBUFS;
928 break;
929 }
930 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
931 kfree(msf);
932 err = -EINVAL;
933 break;
934 }
935 err = ip_mc_msfilter(sk, msf, 0);
936 kfree(msf);
937 break;
938 }
939 case IP_BLOCK_SOURCE:
940 case IP_UNBLOCK_SOURCE:
941 case IP_ADD_SOURCE_MEMBERSHIP:
942 case IP_DROP_SOURCE_MEMBERSHIP:
943 {
944 struct ip_mreq_source mreqs;
945 int omode, add;
946
947 if (optlen != sizeof(struct ip_mreq_source))
948 goto e_inval;
949 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
950 err = -EFAULT;
951 break;
952 }
953 if (optname == IP_BLOCK_SOURCE) {
954 omode = MCAST_EXCLUDE;
955 add = 1;
956 } else if (optname == IP_UNBLOCK_SOURCE) {
957 omode = MCAST_EXCLUDE;
958 add = 0;
959 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
960 struct ip_mreqn mreq;
961
962 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
963 mreq.imr_address.s_addr = mreqs.imr_interface;
964 mreq.imr_ifindex = 0;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300965 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800966 if (err && err != -EADDRINUSE)
967 break;
968 omode = MCAST_INCLUDE;
969 add = 1;
970 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
971 omode = MCAST_INCLUDE;
972 add = 0;
973 }
974 err = ip_mc_source(add, omode, sk, &mreqs, 0);
975 break;
976 }
977 case MCAST_JOIN_GROUP:
978 case MCAST_LEAVE_GROUP:
979 {
980 struct group_req greq;
981 struct sockaddr_in *psin;
982 struct ip_mreqn mreq;
983
984 if (optlen < sizeof(struct group_req))
985 goto e_inval;
986 err = -EFAULT;
987 if (copy_from_user(&greq, optval, sizeof(greq)))
988 break;
989 psin = (struct sockaddr_in *)&greq.gr_group;
990 if (psin->sin_family != AF_INET)
991 goto e_inval;
992 memset(&mreq, 0, sizeof(mreq));
993 mreq.imr_multiaddr = psin->sin_addr;
994 mreq.imr_ifindex = greq.gr_interface;
995
996 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300997 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800998 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300999 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001000 break;
1001 }
1002 case MCAST_JOIN_SOURCE_GROUP:
1003 case MCAST_LEAVE_SOURCE_GROUP:
1004 case MCAST_BLOCK_SOURCE:
1005 case MCAST_UNBLOCK_SOURCE:
1006 {
1007 struct group_source_req greqs;
1008 struct ip_mreq_source mreqs;
1009 struct sockaddr_in *psin;
1010 int omode, add;
1011
1012 if (optlen != sizeof(struct group_source_req))
1013 goto e_inval;
1014 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
1015 err = -EFAULT;
1016 break;
1017 }
1018 if (greqs.gsr_group.ss_family != AF_INET ||
1019 greqs.gsr_source.ss_family != AF_INET) {
1020 err = -EADDRNOTAVAIL;
1021 break;
1022 }
1023 psin = (struct sockaddr_in *)&greqs.gsr_group;
1024 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
1025 psin = (struct sockaddr_in *)&greqs.gsr_source;
1026 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
1027 mreqs.imr_interface = 0; /* use index for mc_source */
1028
1029 if (optname == MCAST_BLOCK_SOURCE) {
1030 omode = MCAST_EXCLUDE;
1031 add = 1;
1032 } else if (optname == MCAST_UNBLOCK_SOURCE) {
1033 omode = MCAST_EXCLUDE;
1034 add = 0;
1035 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
1036 struct ip_mreqn mreq;
1037
1038 psin = (struct sockaddr_in *)&greqs.gsr_group;
1039 mreq.imr_multiaddr = psin->sin_addr;
1040 mreq.imr_address.s_addr = 0;
1041 mreq.imr_ifindex = greqs.gsr_interface;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001042 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001043 if (err && err != -EADDRINUSE)
1044 break;
1045 greqs.gsr_interface = mreq.imr_ifindex;
1046 omode = MCAST_INCLUDE;
1047 add = 1;
1048 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1049 omode = MCAST_INCLUDE;
1050 add = 0;
1051 }
1052 err = ip_mc_source(add, omode, sk, &mreqs,
1053 greqs.gsr_interface);
1054 break;
1055 }
1056 case MCAST_MSFILTER:
1057 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001058 struct sockaddr_in *psin;
1059 struct ip_msfilter *msf = NULL;
1060 struct group_filter *gsf = NULL;
1061 int msize, i, ifindex;
1062
1063 if (optlen < GROUP_FILTER_SIZE(0))
1064 goto e_inval;
1065 if (optlen > sysctl_optmem_max) {
1066 err = -ENOBUFS;
1067 break;
1068 }
Jianjun Kong09cb1052008-11-03 00:27:11 -08001069 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001070 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001071 err = -ENOBUFS;
1072 break;
1073 }
1074 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001075 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001076 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001077
Stephen Hemminger132adf52007-03-08 20:44:43 -08001078 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1079 if (gsf->gf_numsrc >= 0x1ffffff ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +02001080 gsf->gf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001081 err = -ENOBUFS;
1082 goto mc_msf_out;
1083 }
1084 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
1085 err = -EINVAL;
1086 goto mc_msf_out;
1087 }
1088 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -08001089 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001090 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001091 err = -ENOBUFS;
1092 goto mc_msf_out;
1093 }
1094 ifindex = gsf->gf_interface;
1095 psin = (struct sockaddr_in *)&gsf->gf_group;
1096 if (psin->sin_family != AF_INET) {
1097 err = -EADDRNOTAVAIL;
1098 goto mc_msf_out;
1099 }
1100 msf->imsf_multiaddr = psin->sin_addr.s_addr;
1101 msf->imsf_interface = 0;
1102 msf->imsf_fmode = gsf->gf_fmode;
1103 msf->imsf_numsrc = gsf->gf_numsrc;
1104 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001105 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001106 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
1107
1108 if (psin->sin_family != AF_INET)
1109 goto mc_msf_out;
1110 msf->imsf_slist[i] = psin->sin_addr.s_addr;
1111 }
1112 kfree(gsf);
1113 gsf = NULL;
1114
1115 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001116mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -08001117 kfree(msf);
1118 kfree(gsf);
1119 break;
1120 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001121 case IP_MULTICAST_ALL:
1122 if (optlen < 1)
1123 goto e_inval;
1124 if (val != 0 && val != 1)
1125 goto e_inval;
1126 inet->mc_all = val;
1127 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001128 case IP_ROUTER_ALERT:
1129 err = ip_ra_control(sk, val ? 1 : 0, NULL);
1130 break;
1131
1132 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001133 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001134 goto e_inval;
1135 inet->freebind = !!val;
1136 break;
1137
1138 case IP_IPSEC_POLICY:
1139 case IP_XFRM_POLICY:
1140 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001141 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001142 break;
1143 err = xfrm_user_policy(sk, optname, optval, optlen);
1144 break;
1145
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001146 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001147 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1148 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001149 err = -EPERM;
1150 break;
1151 }
1152 if (optlen < 1)
1153 goto e_inval;
1154 inet->transparent = !!val;
1155 break;
1156
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001157 case IP_MINTTL:
1158 if (optlen < 1)
1159 goto e_inval;
1160 if (val < 0 || val > 255)
1161 goto e_inval;
1162 inet->min_ttl = val;
1163 break;
1164
Stephen Hemminger132adf52007-03-08 20:44:43 -08001165 default:
1166 err = -ENOPROTOOPT;
1167 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
1169 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001170 if (needs_rtnl)
1171 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return err;
1173
1174e_inval:
1175 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001176 if (needs_rtnl)
1177 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return -EINVAL;
1179}
1180
Eric Dumazetf84af322010-04-28 15:31:51 -07001181/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001182 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001183 * @sk: socket
1184 * @skb: buffer
1185 *
David S. Miller35ebf652012-06-28 03:59:11 -07001186 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1187 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001188 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001189 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001190void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001191{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001192 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +01001193 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1194 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001195
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +01001196 if (prepare && skb_rtable(skb)) {
David Ahern0b922b72016-05-10 11:19:51 -07001197 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1198 * which has interface index (iif) as the first member of the
1199 * underlying inet{6}_skb_parm struct. This code then overlays
1200 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
1201 * element so the iif is picked up from the prior IPCB
1202 */
David S. Miller35ebf652012-06-28 03:59:11 -07001203 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001204 } else {
1205 pktinfo->ipi_ifindex = 0;
1206 pktinfo->ipi_spec_dst.s_addr = 0;
1207 }
1208 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001209}
Eric Dumazetf84af322010-04-28 15:31:51 -07001210
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001211int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001212 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001213{
1214 int err;
1215
1216 if (level != SOL_IP)
1217 return -ENOPROTOOPT;
1218
1219 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1220#ifdef CONFIG_NETFILTER
1221 /* we need to exclude all possible ENOPROTOOPTs except default case */
1222 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001223 optname != IP_IPSEC_POLICY &&
1224 optname != IP_XFRM_POLICY &&
1225 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001226 lock_sock(sk);
1227 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1228 release_sock(sk);
1229 }
1230#endif
1231 return err;
1232}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001233EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001234
1235#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001236int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001237 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001238{
1239 int err;
1240
1241 if (level != SOL_IP)
1242 return -ENOPROTOOPT;
1243
David L Stevensdae50292008-04-27 01:06:07 -07001244 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1245 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1246 ip_setsockopt);
1247
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001248 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1249#ifdef CONFIG_NETFILTER
1250 /* we need to exclude all possible ENOPROTOOPTs except default case */
1251 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001252 optname != IP_IPSEC_POLICY &&
1253 optname != IP_XFRM_POLICY &&
1254 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001255 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001256 err = compat_nf_setsockopt(sk, PF_INET, optname,
1257 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001258 release_sock(sk);
1259 }
1260#endif
1261 return err;
1262}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001263EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001264#endif
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001267 * Get the options. Note for future reference. The GET of IP options gets
1268 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 */
1270
WANG Cong87e9f032015-11-03 15:41:16 -08001271static bool getsockopt_needs_rtnl(int optname)
1272{
1273 switch (optname) {
1274 case IP_MSFILTER:
1275 case MCAST_MSFILTER:
1276 return true;
1277 }
1278 return false;
1279}
1280
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001281static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001282 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 struct inet_sock *inet = inet_sk(sk);
WANG Cong87e9f032015-11-03 15:41:16 -08001285 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1286 int val, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001288
Stephen Hemminger132adf52007-03-08 20:44:43 -08001289 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return -EOPNOTSUPP;
1291
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001292 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001293 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Jianjun Kong09cb1052008-11-03 00:27:11 -08001295 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001297 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001299
WANG Cong87e9f032015-11-03 15:41:16 -08001300 if (needs_rtnl)
1301 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 lock_sock(sk);
1303
Stephen Hemminger132adf52007-03-08 20:44:43 -08001304 switch (optname) {
1305 case IP_OPTIONS:
1306 {
1307 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001308 struct ip_options *opt = (struct ip_options *)optbuf;
1309 struct ip_options_rcu *inet_opt;
1310
1311 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001312 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001313 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001314 if (inet_opt)
1315 memcpy(optbuf, &inet_opt->opt,
1316 sizeof(struct ip_options) +
1317 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001318 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Stephen Hemminger132adf52007-03-08 20:44:43 -08001320 if (opt->optlen == 0)
1321 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Stephen Hemminger132adf52007-03-08 20:44:43 -08001323 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Stephen Hemminger132adf52007-03-08 20:44:43 -08001325 len = min_t(unsigned int, len, opt->optlen);
1326 if (put_user(len, optlen))
1327 return -EFAULT;
1328 if (copy_to_user(optval, opt->__data, len))
1329 return -EFAULT;
1330 return 0;
1331 }
1332 case IP_PKTINFO:
1333 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1334 break;
1335 case IP_RECVTTL:
1336 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1337 break;
1338 case IP_RECVTOS:
1339 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1340 break;
1341 case IP_RECVOPTS:
1342 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1343 break;
1344 case IP_RETOPTS:
1345 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1346 break;
1347 case IP_PASSSEC:
1348 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1349 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001350 case IP_RECVORIGDSTADDR:
1351 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1352 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001353 case IP_CHECKSUM:
1354 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1355 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001356 case IP_TOS:
1357 val = inet->tos;
1358 break;
1359 case IP_TTL:
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001360 {
1361 struct net *net = sock_net(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001362 val = (inet->uc_ttl == -1 ?
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001363 net->ipv4.sysctl_ip_default_ttl :
Stephen Hemminger132adf52007-03-08 20:44:43 -08001364 inet->uc_ttl);
1365 break;
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001366 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001367 case IP_HDRINCL:
1368 val = inet->hdrincl;
1369 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001370 case IP_NODEFRAG:
1371 val = inet->nodefrag;
1372 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001373 case IP_BIND_ADDRESS_NO_PORT:
1374 val = inet->bind_address_no_port;
1375 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001376 case IP_MTU_DISCOVER:
1377 val = inet->pmtudisc;
1378 break;
1379 case IP_MTU:
1380 {
1381 struct dst_entry *dst;
1382 val = 0;
1383 dst = sk_dst_get(sk);
1384 if (dst) {
1385 val = dst_mtu(dst);
1386 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001388 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001390 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001392 break;
1393 }
1394 case IP_RECVERR:
1395 val = inet->recverr;
1396 break;
1397 case IP_MULTICAST_TTL:
1398 val = inet->mc_ttl;
1399 break;
1400 case IP_MULTICAST_LOOP:
1401 val = inet->mc_loop;
1402 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001403 case IP_UNICAST_IF:
1404 val = (__force int)htonl((__u32) inet->uc_index);
1405 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001406 case IP_MULTICAST_IF:
1407 {
1408 struct in_addr addr;
1409 len = min_t(unsigned int, len, sizeof(struct in_addr));
1410 addr.s_addr = inet->mc_addr;
1411 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Stephen Hemminger132adf52007-03-08 20:44:43 -08001413 if (put_user(len, optlen))
1414 return -EFAULT;
1415 if (copy_to_user(optval, &addr, len))
1416 return -EFAULT;
1417 return 0;
1418 }
1419 case IP_MSFILTER:
1420 {
1421 struct ip_msfilter msf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001422
1423 if (len < IP_MSFILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001424 err = -EINVAL;
1425 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001427 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001428 err = -EFAULT;
1429 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001431 err = ip_mc_msfget(sk, &msf,
1432 (struct ip_msfilter __user *)optval, optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001433 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001434 }
1435 case MCAST_MSFILTER:
1436 {
1437 struct group_filter gsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Stephen Hemminger132adf52007-03-08 20:44:43 -08001439 if (len < GROUP_FILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001440 err = -EINVAL;
1441 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001443 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001444 err = -EFAULT;
1445 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001446 }
1447 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001448 (struct group_filter __user *)optval,
1449 optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001450 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001451 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001452 case IP_MULTICAST_ALL:
1453 val = inet->mc_all;
1454 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001455 case IP_PKTOPTIONS:
1456 {
1457 struct msghdr msg;
1458
1459 release_sock(sk);
1460
1461 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001463
Karoly Kemenyc54a5e02014-07-27 12:29:07 +02001464 msg.msg_control = (__force void *) optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001465 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001466 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001467
1468 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1469 struct in_pktinfo info;
1470
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001471 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1472 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001473 info.ipi_ifindex = inet->mc_index;
1474 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1475 }
1476 if (inet->cmsg_flags & IP_CMSG_TTL) {
1477 int hlim = inet->mc_ttl;
1478 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1479 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001480 if (inet->cmsg_flags & IP_CMSG_TOS) {
1481 int tos = inet->rcv_tos;
1482 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1483 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001484 len -= msg.msg_controllen;
1485 return put_user(len, optlen);
1486 }
1487 case IP_FREEBIND:
1488 val = inet->freebind;
1489 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001490 case IP_TRANSPARENT:
1491 val = inet->transparent;
1492 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001493 case IP_MINTTL:
1494 val = inet->min_ttl;
1495 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001496 default:
1497 release_sock(sk);
1498 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
1500 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001501
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001502 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 unsigned char ucval = (unsigned char)val;
1504 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001505 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001507 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return -EFAULT;
1509 } else {
1510 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001511 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001513 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return -EFAULT;
1515 }
1516 return 0;
WANG Cong87e9f032015-11-03 15:41:16 -08001517
1518out:
1519 release_sock(sk);
1520 if (needs_rtnl)
1521 rtnl_unlock();
1522 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001525int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001526 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001527{
1528 int err;
1529
Daniel Balutadd23198e2011-08-07 22:31:07 -07001530 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001531#ifdef CONFIG_NETFILTER
1532 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001533 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1534 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001535 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001536
Jianjun Kong09cb1052008-11-03 00:27:11 -08001537 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001538 return -EFAULT;
1539
1540 lock_sock(sk);
1541 err = nf_getsockopt(sk, PF_INET, optname, optval,
1542 &len);
1543 release_sock(sk);
1544 if (err >= 0)
1545 err = put_user(len, optlen);
1546 return err;
1547 }
1548#endif
1549 return err;
1550}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001551EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001552
1553#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001554int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1555 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001556{
David L Stevens42908c62008-04-29 03:23:22 -07001557 int err;
1558
1559 if (optname == MCAST_MSFILTER)
1560 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1561 ip_getsockopt);
1562
Daniel Balutadd23198e2011-08-07 22:31:07 -07001563 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1564 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001565
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001566#ifdef CONFIG_NETFILTER
1567 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001568 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1569 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001570 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001571
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001572 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001573 return -EFAULT;
1574
1575 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001576 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001577 release_sock(sk);
1578 if (err >= 0)
1579 err = put_user(len, optlen);
1580 return err;
1581 }
1582#endif
1583 return err;
1584}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001585EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001586#endif