blob: b8a2d63d1fb82f5084a0d98911b8110816dee963 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * The IP to API glue.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: see ip.c
9 *
10 * Fixes:
11 * Many : Split from ip.c , see ip.c for history.
12 * Martin Mares : TOS setting fixed.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090013 * Alan Cox : Fixed a couple of oopses in Martin's
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * TOS tweaks.
15 * Mike McLagan : Routing by source
16 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/skbuff.h>
22#include <linux/ip.h>
23#include <linux/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020024#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/sock.h>
28#include <net/ip.h>
29#include <net/icmp.h>
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -080030#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/udp.h>
32#include <linux/igmp.h>
33#include <linux/netfilter.h>
34#include <linux/route.h>
35#include <linux/mroute.h>
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -040036#include <net/inet_ecn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <net/route.h>
38#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070039#include <net/compat.h>
Tom Herbertad6f9392015-01-05 13:56:17 -080040#include <net/checksum.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000041#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <net/transp_v6.h>
43#endif
David S. Miller35ebf652012-06-28 03:59:11 -070044#include <net/ip_fib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <linux/errqueue.h>
47#include <asm/uaccess.h>
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * SOL_IP control messages.
51 */
52
53static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
54{
Eric Dumazetd826eb12011-11-09 07:24:35 +000055 struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070057 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
60}
61
62static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
63{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070064 int ttl = ip_hdr(skb)->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
66}
67
68static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
69{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070070 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
74{
75 if (IPCB(skb)->opt.optlen == 0)
76 return;
77
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070078 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
79 ip_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82
83static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
84{
85 unsigned char optbuf[sizeof(struct ip_options) + 40];
Daniel Baluta5e73ea12012-04-15 01:34:41 +000086 struct ip_options *opt = (struct ip_options *)optbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 if (IPCB(skb)->opt.optlen == 0)
89 return;
90
91 if (ip_options_echo(opt, skb)) {
92 msg->msg_flags |= MSG_CTRUNC;
93 return;
94 }
95 ip_options_undo(opt);
96
97 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
98}
99
Tom Herbertad6f9392015-01-05 13:56:17 -0800100static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb,
Eric Dumazet10df8e62016-10-23 18:03:06 -0700101 int tlen, int offset)
Tom Herbertad6f9392015-01-05 13:56:17 -0800102{
103 __wsum csum = skb->csum;
104
105 if (skb->ip_summed != CHECKSUM_COMPLETE)
106 return;
107
108 if (offset != 0)
Eric Dumazet10df8e62016-10-23 18:03:06 -0700109 csum = csum_sub(csum,
110 csum_partial(skb_transport_header(skb) + tlen,
111 offset, 0));
Tom Herbertad6f9392015-01-05 13:56:17 -0800112
113 put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum);
114}
115
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800116static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
117{
118 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700119 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800120 int err;
121
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700122 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
123 if (err)
124 return;
125
126 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800127 if (err)
128 return;
129
130 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700131 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800132}
133
Harvey Harrison21d1a162008-11-20 01:54:27 -0800134static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800135{
136 struct sockaddr_in sin;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000137 const struct iphdr *iph = ip_hdr(skb);
Harvey Harrison21d1a162008-11-20 01:54:27 -0800138 __be16 *ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800139
140 if (skb_transport_offset(skb) + 4 > skb->len)
141 return;
142
143 /* All current transport protocols have the port numbers in the
144 * first four bytes of the transport header and this function is
145 * written with this assumption in mind.
146 */
147
148 sin.sin_family = AF_INET;
149 sin.sin_addr.s_addr = iph->daddr;
150 sin.sin_port = ports[1];
151 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
152
153 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
154}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Tom Herbert5961de92015-01-05 13:56:16 -0800156void ip_cmsg_recv_offset(struct msghdr *msg, struct sk_buff *skb,
Eric Dumazet10df8e62016-10-23 18:03:06 -0700157 int tlen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 struct inet_sock *inet = inet_sk(skb->sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000160 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /* Ordered by supposed usage frequency */
Tom Herbertc44d13d2015-01-05 13:56:15 -0800163 if (flags & IP_CMSG_PKTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 ip_cmsg_recv_pktinfo(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Tom Herbertc44d13d2015-01-05 13:56:15 -0800166 flags &= ~IP_CMSG_PKTINFO;
167 if (!flags)
168 return;
169 }
170
171 if (flags & IP_CMSG_TTL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 ip_cmsg_recv_ttl(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Tom Herbertc44d13d2015-01-05 13:56:15 -0800174 flags &= ~IP_CMSG_TTL;
175 if (!flags)
176 return;
177 }
178
179 if (flags & IP_CMSG_TOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 ip_cmsg_recv_tos(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Tom Herbertc44d13d2015-01-05 13:56:15 -0800182 flags &= ~IP_CMSG_TOS;
183 if (!flags)
184 return;
185 }
186
187 if (flags & IP_CMSG_RECVOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 ip_cmsg_recv_opts(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Tom Herbertc44d13d2015-01-05 13:56:15 -0800190 flags &= ~IP_CMSG_RECVOPTS;
191 if (!flags)
192 return;
193 }
194
195 if (flags & IP_CMSG_RETOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 ip_cmsg_recv_retopts(msg, skb);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800197
Tom Herbertc44d13d2015-01-05 13:56:15 -0800198 flags &= ~IP_CMSG_RETOPTS;
199 if (!flags)
200 return;
201 }
202
203 if (flags & IP_CMSG_PASSSEC) {
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800204 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800205
Tom Herbertc44d13d2015-01-05 13:56:15 -0800206 flags &= ~IP_CMSG_PASSSEC;
207 if (!flags)
208 return;
209 }
210
Tom Herbertad6f9392015-01-05 13:56:17 -0800211 if (flags & IP_CMSG_ORIGDSTADDR) {
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800212 ip_cmsg_recv_dstaddr(msg, skb);
213
Tom Herbertad6f9392015-01-05 13:56:17 -0800214 flags &= ~IP_CMSG_ORIGDSTADDR;
215 if (!flags)
216 return;
217 }
218
219 if (flags & IP_CMSG_CHECKSUM)
Eric Dumazet10df8e62016-10-23 18:03:06 -0700220 ip_cmsg_recv_checksum(msg, skb, tlen, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
Tom Herbert5961de92015-01-05 13:56:16 -0800222EXPORT_SYMBOL(ip_cmsg_recv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400224int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100225 bool allow_ipv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Francesco Fuscof02db312013-09-24 15:43:08 +0200227 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct cmsghdr *cmsg;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400229 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Gu Zhengf95b4142014-12-11 11:22:04 +0800231 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (!CMSG_OK(msg, cmsg))
233 return -EINVAL;
Eric Dumazet5337b5b2014-11-10 17:54:25 -0800234#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100235 if (allow_ipv6 &&
236 cmsg->cmsg_level == SOL_IPV6 &&
237 cmsg->cmsg_type == IPV6_PKTINFO) {
238 struct in6_pktinfo *src_info;
239
240 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
241 return -EINVAL;
242 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
243 if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
244 return -EINVAL;
245 ipc->oif = src_info->ipi6_ifindex;
246 ipc->addr = src_info->ipi6_addr.s6_addr32[3];
247 continue;
248 }
249#endif
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400250 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700251 err = __sock_cmsg_send(sk, msg, cmsg, &ipc->sockc);
252 if (err)
253 return err;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400254 continue;
255 }
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (cmsg->cmsg_level != SOL_IP)
258 continue;
259 switch (cmsg->cmsg_type) {
260 case IP_RETOPTS:
261 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
Eric Dumazet91948302016-02-04 06:23:28 -0800262
263 /* Our caller is responsible for freeing ipc->opt */
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700264 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
265 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (err)
267 return err;
268 break;
269 case IP_PKTINFO:
270 {
271 struct in_pktinfo *info;
272 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
273 return -EINVAL;
274 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
275 ipc->oif = info->ipi_ifindex;
276 ipc->addr = info->ipi_spec_dst.s_addr;
277 break;
278 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200279 case IP_TTL:
280 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
281 return -EINVAL;
282 val = *(int *)CMSG_DATA(cmsg);
283 if (val < 1 || val > 255)
284 return -EINVAL;
285 ipc->ttl = val;
286 break;
287 case IP_TOS:
Eric Dumazete895cdc2016-09-07 21:52:56 -0700288 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)))
289 val = *(int *)CMSG_DATA(cmsg);
290 else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8)))
291 val = *(u8 *)CMSG_DATA(cmsg);
292 else
Francesco Fuscof02db312013-09-24 15:43:08 +0200293 return -EINVAL;
Francesco Fuscof02db312013-09-24 15:43:08 +0200294 if (val < 0 || val > 255)
295 return -EINVAL;
296 ipc->tos = val;
297 ipc->priority = rt_tos2priority(ipc->tos);
298 break;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 default:
301 return -EINVAL;
302 }
303 }
304 return 0;
305}
306
307
308/* Special input handler for packets caught by router alert option.
309 They are selected only by protocol field, and then processed likely
310 local ones; but only if someone wants them! Otherwise, router
311 not running rsvpd will kill RSVP.
312
313 It is user level problem, what it will make with them.
314 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
315 but receiver should be enough clever f.e. to forward mtrace requests,
316 sent to multicast group to reach destination designated router.
317 */
Eric Dumazet43a951e2010-10-25 03:32:44 +0000318struct ip_ra_chain __rcu *ip_ra_chain;
Eric Dumazet66018502010-06-07 03:12:08 +0000319static DEFINE_SPINLOCK(ip_ra_lock);
320
Eric Dumazet592fcb92010-06-09 16:21:07 +0000321
322static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000323{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000324 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
325
326 sock_put(ra->saved_sk);
327 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000328}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700330int ip_ra_control(struct sock *sk, unsigned char on,
331 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000333 struct ip_ra_chain *ra, *new_ra;
334 struct ip_ra_chain __rcu **rap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000336 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return -EINVAL;
338
339 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
340
Eric Dumazet66018502010-06-07 03:12:08 +0000341 spin_lock_bh(&ip_ra_lock);
Eric Dumazet43a951e2010-10-25 03:32:44 +0000342 for (rap = &ip_ra_chain;
343 (ra = rcu_dereference_protected(*rap,
344 lockdep_is_held(&ip_ra_lock))) != NULL;
345 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (ra->sk == sk) {
347 if (on) {
Eric Dumazet66018502010-06-07 03:12:08 +0000348 spin_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800349 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return -EADDRINUSE;
351 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000352 /* dont let ip_call_ra_chain() use sk again */
353 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700354 RCU_INIT_POINTER(*rap, ra->next);
Eric Dumazet66018502010-06-07 03:12:08 +0000355 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 if (ra->destructor)
358 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000359 /*
360 * Delay sock_put(sk) and kfree(ra) after one rcu grace
361 * period. This guarantee ip_call_ra_chain() dont need
362 * to mess with socket refcounts.
363 */
364 ra->saved_sk = sk;
365 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 0;
367 }
368 }
Ian Morris51456b22015-04-03 09:17:26 +0100369 if (!new_ra) {
Eric Dumazet66018502010-06-07 03:12:08 +0000370 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return -ENOBUFS;
372 }
373 new_ra->sk = sk;
374 new_ra->destructor = destructor;
375
Eric Dumazet8e380f02014-09-09 08:11:41 -0700376 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000377 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 sock_hold(sk);
Eric Dumazet66018502010-06-07 03:12:08 +0000379 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 return 0;
382}
383
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900384void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700385 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct sock_exterr_skb *serr;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 skb = skb_clone(skb, GFP_ATOMIC);
390 if (!skb)
391 return;
392
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900393 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 serr->ee.ee_errno = err;
395 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300396 serr->ee.ee_type = icmp_hdr(skb)->type;
397 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 serr->ee.ee_pad = 0;
399 serr->ee.ee_info = info;
400 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300401 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700402 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 serr->port = port;
404
Ian Morris00db4122015-04-03 09:17:27 +0100405 if (skb_pull(skb, payload - skb->data)) {
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300406 skb_reset_transport_header(skb);
407 if (sock_queue_err_skb(sk, skb) == 0)
408 return;
409 }
410 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Al Viro05790162006-09-27 18:33:40 -0700413void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct inet_sock *inet = inet_sk(sk);
416 struct sock_exterr_skb *serr;
417 struct iphdr *iph;
418 struct sk_buff *skb;
419
420 if (!inet->recverr)
421 return;
422
423 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
424 if (!skb)
425 return;
426
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300427 skb_put(skb, sizeof(struct iphdr));
428 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700429 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 iph->daddr = daddr;
431
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900432 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 serr->ee.ee_errno = err;
434 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900435 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 serr->ee.ee_code = 0;
437 serr->ee.ee_pad = 0;
438 serr->ee.ee_info = info;
439 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700440 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 serr->port = port;
442
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700443 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300444 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 if (sock_queue_err_skb(sk, skb))
447 kfree_skb(skb);
448}
449
Julian Anastasov34b99df2015-06-23 08:34:39 +0300450/* For some errors we have valid addr_offset even with zero payload and
451 * zero port. Also, addr_offset should be supported if port is set.
452 */
453static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
454{
455 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
456 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
457}
458
Willem de Bruijnc247f052015-03-07 20:33:22 -0500459/* IPv4 supports cmsg on all imcp errors and some timestamps
460 *
461 * Timestamp code paths do not initialize the fields expected by cmsg:
462 * the PKTINFO fields in skb->cb[]. Fill those in here.
463 */
464static bool ipv4_datagram_support_cmsg(const struct sock *sk,
465 struct sk_buff *skb,
466 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500467{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500468 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500469
Willem de Bruijnc247f052015-03-07 20:33:22 -0500470 if (ee_origin == SO_EE_ORIGIN_ICMP)
471 return true;
472
473 if (ee_origin == SO_EE_ORIGIN_LOCAL)
474 return false;
475
476 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
477 * timestamp with egress dev. Not possible for packets without dev
478 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
479 */
480 if ((!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG)) ||
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500481 (!skb->dev))
482 return false;
483
Willem de Bruijnc247f052015-03-07 20:33:22 -0500484 info = PKTINFO_SKB_CB(skb);
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500485 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
486 info->ipi_ifindex = skb->dev->ifindex;
487 return true;
488}
489
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900490/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * Handle MSG_ERRQUEUE
492 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100493int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400496 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100497 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct {
499 struct sock_extended_err ee;
500 struct sockaddr_in offender;
501 } errhdr;
502 int err;
503 int copied;
504
Willem de Bruijn7ce875e2014-11-30 22:22:33 -0500505 WARN_ON_ONCE(sk->sk_family == AF_INET6);
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400508 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100509 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 goto out;
511
512 copied = skb->len;
513 if (copied > len) {
514 msg->msg_flags |= MSG_TRUNC;
515 copied = len;
516 }
David S. Miller51f3d022014-11-05 16:46:40 -0500517 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700518 if (unlikely(err)) {
519 kfree_skb(skb);
520 return err;
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 sock_recv_timestamp(msg, sk, skb);
523
524 serr = SKB_EXT_ERR(skb);
525
Julian Anastasov34b99df2015-06-23 08:34:39 +0300526 if (sin && ipv4_datagram_support_addr(serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700528 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
529 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 sin->sin_port = serr->port;
531 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100532 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
535 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
536 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500537 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500538
Willem de Bruijnc247f052015-03-07 20:33:22 -0500539 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700541 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500542 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 ip_cmsg_recv(msg, skb);
544 }
545
546 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
547
548 /* Now we could try to dump offended packet options */
549
550 msg->msg_flags |= MSG_ERRQUEUE;
551 err = copied;
552
Eric Dumazet960a2622016-04-21 22:27:32 -0700553 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554out:
555 return err;
556}
557
558
559/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700560 * Socket option code for IP. This is the end of the line after any
561 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300563static bool setsockopt_needs_rtnl(int optname)
564{
565 switch (optname) {
566 case IP_ADD_MEMBERSHIP:
567 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300568 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300569 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300570 case IP_DROP_SOURCE_MEMBERSHIP:
571 case IP_MSFILTER:
572 case IP_UNBLOCK_SOURCE:
573 case MCAST_BLOCK_SOURCE:
574 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300575 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300576 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300577 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300578 case MCAST_LEAVE_SOURCE_GROUP:
579 case MCAST_UNBLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300580 return true;
581 }
582 return false;
583}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800585static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700586 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 struct inet_sock *inet = inet_sk(sk);
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200589 struct net *net = sock_net(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800590 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300591 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Xi Wang0c9f79b2012-11-11 11:20:01 +0000593 switch (optname) {
594 case IP_PKTINFO:
595 case IP_RECVTTL:
596 case IP_RECVOPTS:
597 case IP_RECVTOS:
598 case IP_RETOPTS:
599 case IP_TOS:
600 case IP_TTL:
601 case IP_HDRINCL:
602 case IP_MTU_DISCOVER:
603 case IP_RECVERR:
604 case IP_ROUTER_ALERT:
605 case IP_FREEBIND:
606 case IP_PASSSEC:
607 case IP_TRANSPARENT:
608 case IP_MINTTL:
609 case IP_NODEFRAG:
Eric Dumazet90c337d2015-06-06 21:17:57 -0700610 case IP_BIND_ADDRESS_NO_PORT:
Xi Wang0c9f79b2012-11-11 11:20:01 +0000611 case IP_UNICAST_IF:
612 case IP_MULTICAST_TTL:
613 case IP_MULTICAST_ALL:
614 case IP_MULTICAST_LOOP:
615 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800616 case IP_CHECKSUM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (optlen >= sizeof(int)) {
618 if (get_user(val, (int __user *) optval))
619 return -EFAULT;
620 } else if (optlen >= sizeof(char)) {
621 unsigned char ucval;
622
623 if (get_user(ucval, (unsigned char __user *) optval))
624 return -EFAULT;
625 val = (int) ucval;
626 }
627 }
628
629 /* If optlen==0, it is equivalent to val == 0 */
630
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800631 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800632 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300635 if (needs_rtnl)
636 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 lock_sock(sk);
638
639 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800640 case IP_OPTIONS:
641 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000642 struct ip_options_rcu *old, *opt = NULL;
643
roel kluin65a1c4f2009-10-23 05:59:21 +0000644 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800645 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900646 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700647 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800648 if (err)
649 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000650 old = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200651 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800652 if (inet->is_icsk) {
653 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000654#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800655 if (sk->sk_family == PF_INET ||
656 (!((1 << sk->sk_state) &
657 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000658 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000660 if (old)
661 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800662 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000663 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800664 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000665#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000669 rcu_assign_pointer(inet->inet_opt, opt);
670 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800671 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800672 break;
673 }
674 case IP_PKTINFO:
675 if (val)
676 inet->cmsg_flags |= IP_CMSG_PKTINFO;
677 else
678 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
679 break;
680 case IP_RECVTTL:
681 if (val)
682 inet->cmsg_flags |= IP_CMSG_TTL;
683 else
684 inet->cmsg_flags &= ~IP_CMSG_TTL;
685 break;
686 case IP_RECVTOS:
687 if (val)
688 inet->cmsg_flags |= IP_CMSG_TOS;
689 else
690 inet->cmsg_flags &= ~IP_CMSG_TOS;
691 break;
692 case IP_RECVOPTS:
693 if (val)
694 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
695 else
696 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
697 break;
698 case IP_RETOPTS:
699 if (val)
700 inet->cmsg_flags |= IP_CMSG_RETOPTS;
701 else
702 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
703 break;
704 case IP_PASSSEC:
705 if (val)
706 inet->cmsg_flags |= IP_CMSG_PASSSEC;
707 else
708 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
709 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800710 case IP_RECVORIGDSTADDR:
711 if (val)
712 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
713 else
714 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
715 break;
Tom Herbertad6f9392015-01-05 13:56:17 -0800716 case IP_CHECKSUM:
717 if (val) {
718 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
719 inet_inc_convert_csum(sk);
720 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
721 }
722 } else {
723 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
724 inet_dec_convert_csum(sk);
725 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
726 }
727 }
728 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800729 case IP_TOS: /* This sets both TOS and Precedence */
730 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400731 val &= ~INET_ECN_MASK;
732 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800734 if (inet->tos != val) {
735 inet->tos = val;
736 sk->sk_priority = rt_tos2priority(val);
737 sk_dst_reset(sk);
738 }
739 break;
740 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700741 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800742 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000743 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800744 goto e_inval;
745 inet->uc_ttl = val;
746 break;
747 case IP_HDRINCL:
748 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800751 }
752 inet->hdrincl = val ? 1 : 0;
753 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000754 case IP_NODEFRAG:
755 if (sk->sk_type != SOCK_RAW) {
756 err = -ENOPROTOOPT;
757 break;
758 }
759 inet->nodefrag = val ? 1 : 0;
760 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -0700761 case IP_BIND_ADDRESS_NO_PORT:
762 inet->bind_address_no_port = val ? 1 : 0;
763 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800764 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100765 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800766 goto e_inval;
767 inet->pmtudisc = val;
768 break;
769 case IP_RECVERR:
770 inet->recverr = !!val;
771 if (!val)
772 skb_queue_purge(&sk->sk_error_queue);
773 break;
774 case IP_MULTICAST_TTL:
775 if (sk->sk_type == SOCK_STREAM)
776 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700777 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800778 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800779 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800780 val = 1;
781 if (val < 0 || val > 255)
782 goto e_inval;
783 inet->mc_ttl = val;
784 break;
785 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700786 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800787 goto e_inval;
788 inet->mc_loop = !!val;
789 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000790 case IP_UNICAST_IF:
791 {
792 struct net_device *dev = NULL;
793 int ifindex;
794
795 if (optlen != sizeof(int))
796 goto e_inval;
797
798 ifindex = (__force int)ntohl((__force __be32)val);
799 if (ifindex == 0) {
800 inet->uc_index = 0;
801 err = 0;
802 break;
803 }
804
805 dev = dev_get_by_index(sock_net(sk), ifindex);
806 err = -EADDRNOTAVAIL;
807 if (!dev)
808 break;
809 dev_put(dev);
810
811 err = -EINVAL;
812 if (sk->sk_bound_dev_if)
813 break;
814
815 inet->uc_index = ifindex;
816 err = 0;
817 break;
818 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800819 case IP_MULTICAST_IF:
820 {
821 struct ip_mreqn mreq;
822 struct net_device *dev = NULL;
823
824 if (sk->sk_type == SOCK_STREAM)
825 goto e_inval;
826 /*
827 * Check the arguments are allowable
828 */
829
Shan Wei09159212009-09-22 15:41:10 +0000830 if (optlen < sizeof(struct in_addr))
831 goto e_inval;
832
Stephen Hemminger132adf52007-03-08 20:44:43 -0800833 err = -EFAULT;
834 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800835 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800836 break;
837 } else {
838 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000839 if (optlen >= sizeof(struct ip_mreq)) {
840 if (copy_from_user(&mreq, optval,
841 sizeof(struct ip_mreq)))
842 break;
843 } else if (optlen >= sizeof(struct in_addr)) {
844 if (copy_from_user(&mreq.imr_address, optval,
845 sizeof(struct in_addr)))
846 break;
847 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800848 }
849
850 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700851 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800852 inet->mc_index = 0;
853 inet->mc_addr = 0;
854 err = 0;
855 break;
856 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900857 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000858 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800859 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800860 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000861 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800862
863
864 err = -EADDRNOTAVAIL;
865 if (!dev)
866 break;
Eric Dumazet55b80502009-10-19 06:41:58 +0000867 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800868
869 err = -EINVAL;
870 if (sk->sk_bound_dev_if &&
871 mreq.imr_ifindex != sk->sk_bound_dev_if)
872 break;
873
874 inet->mc_index = mreq.imr_ifindex;
875 inet->mc_addr = mreq.imr_address.s_addr;
876 err = 0;
877 break;
878 }
879
880 case IP_ADD_MEMBERSHIP:
881 case IP_DROP_MEMBERSHIP:
882 {
883 struct ip_mreqn mreq;
884
Flavio Leitnera96fb492007-08-24 22:16:39 -0700885 err = -EPROTO;
886 if (inet_sk(sk)->is_icsk)
887 break;
888
Stephen Hemminger132adf52007-03-08 20:44:43 -0800889 if (optlen < sizeof(struct ip_mreq))
890 goto e_inval;
891 err = -EFAULT;
892 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800893 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800894 break;
895 } else {
896 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800897 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800898 break;
899 }
900
901 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300902 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800903 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300904 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800905 break;
906 }
907 case IP_MSFILTER:
908 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800909 struct ip_msfilter *msf;
910
911 if (optlen < IP_MSFILTER_SIZE(0))
912 goto e_inval;
913 if (optlen > sysctl_optmem_max) {
914 err = -ENOBUFS;
915 break;
916 }
917 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700918 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800919 err = -ENOBUFS;
920 break;
921 }
922 err = -EFAULT;
923 if (copy_from_user(msf, optval, optlen)) {
924 kfree(msf);
925 break;
926 }
927 /* numsrc >= (1G-4) overflow in 32 bits */
928 if (msf->imsf_numsrc >= 0x3ffffffcU ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200929 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800930 kfree(msf);
931 err = -ENOBUFS;
932 break;
933 }
934 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
935 kfree(msf);
936 err = -EINVAL;
937 break;
938 }
939 err = ip_mc_msfilter(sk, msf, 0);
940 kfree(msf);
941 break;
942 }
943 case IP_BLOCK_SOURCE:
944 case IP_UNBLOCK_SOURCE:
945 case IP_ADD_SOURCE_MEMBERSHIP:
946 case IP_DROP_SOURCE_MEMBERSHIP:
947 {
948 struct ip_mreq_source mreqs;
949 int omode, add;
950
951 if (optlen != sizeof(struct ip_mreq_source))
952 goto e_inval;
953 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
954 err = -EFAULT;
955 break;
956 }
957 if (optname == IP_BLOCK_SOURCE) {
958 omode = MCAST_EXCLUDE;
959 add = 1;
960 } else if (optname == IP_UNBLOCK_SOURCE) {
961 omode = MCAST_EXCLUDE;
962 add = 0;
963 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
964 struct ip_mreqn mreq;
965
966 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
967 mreq.imr_address.s_addr = mreqs.imr_interface;
968 mreq.imr_ifindex = 0;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300969 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800970 if (err && err != -EADDRINUSE)
971 break;
972 omode = MCAST_INCLUDE;
973 add = 1;
974 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
975 omode = MCAST_INCLUDE;
976 add = 0;
977 }
978 err = ip_mc_source(add, omode, sk, &mreqs, 0);
979 break;
980 }
981 case MCAST_JOIN_GROUP:
982 case MCAST_LEAVE_GROUP:
983 {
984 struct group_req greq;
985 struct sockaddr_in *psin;
986 struct ip_mreqn mreq;
987
988 if (optlen < sizeof(struct group_req))
989 goto e_inval;
990 err = -EFAULT;
991 if (copy_from_user(&greq, optval, sizeof(greq)))
992 break;
993 psin = (struct sockaddr_in *)&greq.gr_group;
994 if (psin->sin_family != AF_INET)
995 goto e_inval;
996 memset(&mreq, 0, sizeof(mreq));
997 mreq.imr_multiaddr = psin->sin_addr;
998 mreq.imr_ifindex = greq.gr_interface;
999
1000 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001001 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001002 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001003 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001004 break;
1005 }
1006 case MCAST_JOIN_SOURCE_GROUP:
1007 case MCAST_LEAVE_SOURCE_GROUP:
1008 case MCAST_BLOCK_SOURCE:
1009 case MCAST_UNBLOCK_SOURCE:
1010 {
1011 struct group_source_req greqs;
1012 struct ip_mreq_source mreqs;
1013 struct sockaddr_in *psin;
1014 int omode, add;
1015
1016 if (optlen != sizeof(struct group_source_req))
1017 goto e_inval;
1018 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
1019 err = -EFAULT;
1020 break;
1021 }
1022 if (greqs.gsr_group.ss_family != AF_INET ||
1023 greqs.gsr_source.ss_family != AF_INET) {
1024 err = -EADDRNOTAVAIL;
1025 break;
1026 }
1027 psin = (struct sockaddr_in *)&greqs.gsr_group;
1028 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
1029 psin = (struct sockaddr_in *)&greqs.gsr_source;
1030 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
1031 mreqs.imr_interface = 0; /* use index for mc_source */
1032
1033 if (optname == MCAST_BLOCK_SOURCE) {
1034 omode = MCAST_EXCLUDE;
1035 add = 1;
1036 } else if (optname == MCAST_UNBLOCK_SOURCE) {
1037 omode = MCAST_EXCLUDE;
1038 add = 0;
1039 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
1040 struct ip_mreqn mreq;
1041
1042 psin = (struct sockaddr_in *)&greqs.gsr_group;
1043 mreq.imr_multiaddr = psin->sin_addr;
1044 mreq.imr_address.s_addr = 0;
1045 mreq.imr_ifindex = greqs.gsr_interface;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001046 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001047 if (err && err != -EADDRINUSE)
1048 break;
1049 greqs.gsr_interface = mreq.imr_ifindex;
1050 omode = MCAST_INCLUDE;
1051 add = 1;
1052 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1053 omode = MCAST_INCLUDE;
1054 add = 0;
1055 }
1056 err = ip_mc_source(add, omode, sk, &mreqs,
1057 greqs.gsr_interface);
1058 break;
1059 }
1060 case MCAST_MSFILTER:
1061 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001062 struct sockaddr_in *psin;
1063 struct ip_msfilter *msf = NULL;
1064 struct group_filter *gsf = NULL;
1065 int msize, i, ifindex;
1066
1067 if (optlen < GROUP_FILTER_SIZE(0))
1068 goto e_inval;
1069 if (optlen > sysctl_optmem_max) {
1070 err = -ENOBUFS;
1071 break;
1072 }
Jianjun Kong09cb1052008-11-03 00:27:11 -08001073 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001074 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001075 err = -ENOBUFS;
1076 break;
1077 }
1078 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001079 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001080 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001081
Stephen Hemminger132adf52007-03-08 20:44:43 -08001082 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1083 if (gsf->gf_numsrc >= 0x1ffffff ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +02001084 gsf->gf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001085 err = -ENOBUFS;
1086 goto mc_msf_out;
1087 }
1088 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
1089 err = -EINVAL;
1090 goto mc_msf_out;
1091 }
1092 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -08001093 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001094 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001095 err = -ENOBUFS;
1096 goto mc_msf_out;
1097 }
1098 ifindex = gsf->gf_interface;
1099 psin = (struct sockaddr_in *)&gsf->gf_group;
1100 if (psin->sin_family != AF_INET) {
1101 err = -EADDRNOTAVAIL;
1102 goto mc_msf_out;
1103 }
1104 msf->imsf_multiaddr = psin->sin_addr.s_addr;
1105 msf->imsf_interface = 0;
1106 msf->imsf_fmode = gsf->gf_fmode;
1107 msf->imsf_numsrc = gsf->gf_numsrc;
1108 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001109 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001110 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
1111
1112 if (psin->sin_family != AF_INET)
1113 goto mc_msf_out;
1114 msf->imsf_slist[i] = psin->sin_addr.s_addr;
1115 }
1116 kfree(gsf);
1117 gsf = NULL;
1118
1119 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001120mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -08001121 kfree(msf);
1122 kfree(gsf);
1123 break;
1124 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001125 case IP_MULTICAST_ALL:
1126 if (optlen < 1)
1127 goto e_inval;
1128 if (val != 0 && val != 1)
1129 goto e_inval;
1130 inet->mc_all = val;
1131 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001132 case IP_ROUTER_ALERT:
1133 err = ip_ra_control(sk, val ? 1 : 0, NULL);
1134 break;
1135
1136 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001137 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001138 goto e_inval;
1139 inet->freebind = !!val;
1140 break;
1141
1142 case IP_IPSEC_POLICY:
1143 case IP_XFRM_POLICY:
1144 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001145 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001146 break;
1147 err = xfrm_user_policy(sk, optname, optval, optlen);
1148 break;
1149
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001150 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001151 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1152 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001153 err = -EPERM;
1154 break;
1155 }
1156 if (optlen < 1)
1157 goto e_inval;
1158 inet->transparent = !!val;
1159 break;
1160
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001161 case IP_MINTTL:
1162 if (optlen < 1)
1163 goto e_inval;
1164 if (val < 0 || val > 255)
1165 goto e_inval;
1166 inet->min_ttl = val;
1167 break;
1168
Stephen Hemminger132adf52007-03-08 20:44:43 -08001169 default:
1170 err = -ENOPROTOOPT;
1171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
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 err;
1177
1178e_inval:
1179 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001180 if (needs_rtnl)
1181 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return -EINVAL;
1183}
1184
Eric Dumazetf84af322010-04-28 15:31:51 -07001185/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001186 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001187 * @sk: socket
1188 * @skb: buffer
1189 *
David S. Miller35ebf652012-06-28 03:59:11 -07001190 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1191 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001192 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001193 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001194void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001195{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001196 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +01001197 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1198 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001199
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +01001200 if (prepare && skb_rtable(skb)) {
David Ahern0b922b72016-05-10 11:19:51 -07001201 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1202 * which has interface index (iif) as the first member of the
1203 * underlying inet{6}_skb_parm struct. This code then overlays
1204 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
1205 * element so the iif is picked up from the prior IPCB
1206 */
David S. Miller35ebf652012-06-28 03:59:11 -07001207 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001208 } else {
1209 pktinfo->ipi_ifindex = 0;
1210 pktinfo->ipi_spec_dst.s_addr = 0;
1211 }
1212 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001213}
Eric Dumazetf84af322010-04-28 15:31:51 -07001214
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001215int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001216 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001217{
1218 int err;
1219
1220 if (level != SOL_IP)
1221 return -ENOPROTOOPT;
1222
1223 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1224#ifdef CONFIG_NETFILTER
1225 /* we need to exclude all possible ENOPROTOOPTs except default case */
1226 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001227 optname != IP_IPSEC_POLICY &&
1228 optname != IP_XFRM_POLICY &&
1229 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001230 lock_sock(sk);
1231 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1232 release_sock(sk);
1233 }
1234#endif
1235 return err;
1236}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001237EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001238
1239#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001240int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001241 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001242{
1243 int err;
1244
1245 if (level != SOL_IP)
1246 return -ENOPROTOOPT;
1247
David L Stevensdae50292008-04-27 01:06:07 -07001248 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1249 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1250 ip_setsockopt);
1251
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001252 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1253#ifdef CONFIG_NETFILTER
1254 /* we need to exclude all possible ENOPROTOOPTs except default case */
1255 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001256 optname != IP_IPSEC_POLICY &&
1257 optname != IP_XFRM_POLICY &&
1258 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001259 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001260 err = compat_nf_setsockopt(sk, PF_INET, optname,
1261 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001262 release_sock(sk);
1263 }
1264#endif
1265 return err;
1266}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001267EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001268#endif
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001271 * Get the options. Note for future reference. The GET of IP options gets
1272 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
1274
WANG Cong87e9f032015-11-03 15:41:16 -08001275static bool getsockopt_needs_rtnl(int optname)
1276{
1277 switch (optname) {
1278 case IP_MSFILTER:
1279 case MCAST_MSFILTER:
1280 return true;
1281 }
1282 return false;
1283}
1284
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001285static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001286 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 struct inet_sock *inet = inet_sk(sk);
WANG Cong87e9f032015-11-03 15:41:16 -08001289 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1290 int val, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001292
Stephen Hemminger132adf52007-03-08 20:44:43 -08001293 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return -EOPNOTSUPP;
1295
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001296 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001297 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Jianjun Kong09cb1052008-11-03 00:27:11 -08001299 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001301 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001303
WANG Cong87e9f032015-11-03 15:41:16 -08001304 if (needs_rtnl)
1305 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 lock_sock(sk);
1307
Stephen Hemminger132adf52007-03-08 20:44:43 -08001308 switch (optname) {
1309 case IP_OPTIONS:
1310 {
1311 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001312 struct ip_options *opt = (struct ip_options *)optbuf;
1313 struct ip_options_rcu *inet_opt;
1314
1315 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001316 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001317 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001318 if (inet_opt)
1319 memcpy(optbuf, &inet_opt->opt,
1320 sizeof(struct ip_options) +
1321 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001322 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Stephen Hemminger132adf52007-03-08 20:44:43 -08001324 if (opt->optlen == 0)
1325 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Stephen Hemminger132adf52007-03-08 20:44:43 -08001327 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Stephen Hemminger132adf52007-03-08 20:44:43 -08001329 len = min_t(unsigned int, len, opt->optlen);
1330 if (put_user(len, optlen))
1331 return -EFAULT;
1332 if (copy_to_user(optval, opt->__data, len))
1333 return -EFAULT;
1334 return 0;
1335 }
1336 case IP_PKTINFO:
1337 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1338 break;
1339 case IP_RECVTTL:
1340 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1341 break;
1342 case IP_RECVTOS:
1343 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1344 break;
1345 case IP_RECVOPTS:
1346 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1347 break;
1348 case IP_RETOPTS:
1349 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1350 break;
1351 case IP_PASSSEC:
1352 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1353 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001354 case IP_RECVORIGDSTADDR:
1355 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1356 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001357 case IP_CHECKSUM:
1358 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1359 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001360 case IP_TOS:
1361 val = inet->tos;
1362 break;
1363 case IP_TTL:
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001364 {
1365 struct net *net = sock_net(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001366 val = (inet->uc_ttl == -1 ?
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001367 net->ipv4.sysctl_ip_default_ttl :
Stephen Hemminger132adf52007-03-08 20:44:43 -08001368 inet->uc_ttl);
1369 break;
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001370 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001371 case IP_HDRINCL:
1372 val = inet->hdrincl;
1373 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001374 case IP_NODEFRAG:
1375 val = inet->nodefrag;
1376 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001377 case IP_BIND_ADDRESS_NO_PORT:
1378 val = inet->bind_address_no_port;
1379 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001380 case IP_MTU_DISCOVER:
1381 val = inet->pmtudisc;
1382 break;
1383 case IP_MTU:
1384 {
1385 struct dst_entry *dst;
1386 val = 0;
1387 dst = sk_dst_get(sk);
1388 if (dst) {
1389 val = dst_mtu(dst);
1390 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001392 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001394 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001396 break;
1397 }
1398 case IP_RECVERR:
1399 val = inet->recverr;
1400 break;
1401 case IP_MULTICAST_TTL:
1402 val = inet->mc_ttl;
1403 break;
1404 case IP_MULTICAST_LOOP:
1405 val = inet->mc_loop;
1406 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001407 case IP_UNICAST_IF:
1408 val = (__force int)htonl((__u32) inet->uc_index);
1409 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001410 case IP_MULTICAST_IF:
1411 {
1412 struct in_addr addr;
1413 len = min_t(unsigned int, len, sizeof(struct in_addr));
1414 addr.s_addr = inet->mc_addr;
1415 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Stephen Hemminger132adf52007-03-08 20:44:43 -08001417 if (put_user(len, optlen))
1418 return -EFAULT;
1419 if (copy_to_user(optval, &addr, len))
1420 return -EFAULT;
1421 return 0;
1422 }
1423 case IP_MSFILTER:
1424 {
1425 struct ip_msfilter msf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001426
1427 if (len < IP_MSFILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001428 err = -EINVAL;
1429 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001431 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001432 err = -EFAULT;
1433 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001435 err = ip_mc_msfget(sk, &msf,
1436 (struct ip_msfilter __user *)optval, optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001437 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001438 }
1439 case MCAST_MSFILTER:
1440 {
1441 struct group_filter gsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Stephen Hemminger132adf52007-03-08 20:44:43 -08001443 if (len < GROUP_FILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001444 err = -EINVAL;
1445 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001447 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001448 err = -EFAULT;
1449 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001450 }
1451 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001452 (struct group_filter __user *)optval,
1453 optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001454 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001455 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001456 case IP_MULTICAST_ALL:
1457 val = inet->mc_all;
1458 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001459 case IP_PKTOPTIONS:
1460 {
1461 struct msghdr msg;
1462
1463 release_sock(sk);
1464
1465 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001467
Karoly Kemenyc54a5e02014-07-27 12:29:07 +02001468 msg.msg_control = (__force void *) optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001469 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001470 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001471
1472 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1473 struct in_pktinfo info;
1474
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001475 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1476 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001477 info.ipi_ifindex = inet->mc_index;
1478 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1479 }
1480 if (inet->cmsg_flags & IP_CMSG_TTL) {
1481 int hlim = inet->mc_ttl;
1482 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1483 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001484 if (inet->cmsg_flags & IP_CMSG_TOS) {
1485 int tos = inet->rcv_tos;
1486 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1487 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001488 len -= msg.msg_controllen;
1489 return put_user(len, optlen);
1490 }
1491 case IP_FREEBIND:
1492 val = inet->freebind;
1493 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001494 case IP_TRANSPARENT:
1495 val = inet->transparent;
1496 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001497 case IP_MINTTL:
1498 val = inet->min_ttl;
1499 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001500 default:
1501 release_sock(sk);
1502 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
1504 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001505
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001506 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 unsigned char ucval = (unsigned char)val;
1508 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001509 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001511 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return -EFAULT;
1513 } else {
1514 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001515 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001517 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return -EFAULT;
1519 }
1520 return 0;
WANG Cong87e9f032015-11-03 15:41:16 -08001521
1522out:
1523 release_sock(sk);
1524 if (needs_rtnl)
1525 rtnl_unlock();
1526 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001529int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001530 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001531{
1532 int err;
1533
Daniel Balutadd23198e2011-08-07 22:31:07 -07001534 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001535#ifdef CONFIG_NETFILTER
1536 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001537 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1538 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001539 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001540
Jianjun Kong09cb1052008-11-03 00:27:11 -08001541 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001542 return -EFAULT;
1543
1544 lock_sock(sk);
1545 err = nf_getsockopt(sk, PF_INET, optname, optval,
1546 &len);
1547 release_sock(sk);
1548 if (err >= 0)
1549 err = put_user(len, optlen);
1550 return err;
1551 }
1552#endif
1553 return err;
1554}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001555EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001556
1557#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001558int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1559 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001560{
David L Stevens42908c62008-04-29 03:23:22 -07001561 int err;
1562
1563 if (optname == MCAST_MSFILTER)
1564 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1565 ip_getsockopt);
1566
Daniel Balutadd23198e2011-08-07 22:31:07 -07001567 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1568 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001569
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001570#ifdef CONFIG_NETFILTER
1571 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001572 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1573 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001574 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001575
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001576 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001577 return -EFAULT;
1578
1579 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001580 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001581 release_sock(sk);
1582 if (err >= 0)
1583 err = put_user(len, optlen);
1584 return err;
1585 }
1586#endif
1587 return err;
1588}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001589EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001590#endif