blob: 1d46d05efb0ff067c35750ac43be8e7babd60446 [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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080047#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
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
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400100static void ip_cmsg_recv_fragsize(struct msghdr *msg, struct sk_buff *skb)
101{
102 int val;
103
104 if (IPCB(skb)->frag_max_size == 0)
105 return;
106
107 val = IPCB(skb)->frag_max_size;
108 put_cmsg(msg, SOL_IP, IP_RECVFRAGSIZE, sizeof(val), &val);
109}
110
Tom Herbertad6f9392015-01-05 13:56:17 -0800111static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb,
Eric Dumazet10df8e62016-10-23 18:03:06 -0700112 int tlen, int offset)
Tom Herbertad6f9392015-01-05 13:56:17 -0800113{
114 __wsum csum = skb->csum;
115
116 if (skb->ip_summed != CHECKSUM_COMPLETE)
117 return;
118
Paolo Abenica4ef452017-02-21 09:33:18 +0100119 if (offset != 0) {
120 int tend_off = skb_transport_offset(skb) + tlen;
121 csum = csum_sub(csum, skb_checksum(skb, tend_off, offset, 0));
122 }
Tom Herbertad6f9392015-01-05 13:56:17 -0800123
124 put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum);
125}
126
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800127static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
128{
129 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700130 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800131 int err;
132
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700133 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
134 if (err)
135 return;
136
137 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800138 if (err)
139 return;
140
141 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700142 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800143}
144
Harvey Harrison21d1a162008-11-20 01:54:27 -0800145static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800146{
147 struct sockaddr_in sin;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000148 const struct iphdr *iph = ip_hdr(skb);
Harvey Harrison21d1a162008-11-20 01:54:27 -0800149 __be16 *ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800150
Willem de Bruijn39b2dd72016-12-22 18:19:16 -0500151 if (skb_transport_offset(skb) + 4 > (int)skb->len)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800152 return;
153
154 /* All current transport protocols have the port numbers in the
155 * first four bytes of the transport header and this function is
156 * written with this assumption in mind.
157 */
158
159 sin.sin_family = AF_INET;
160 sin.sin_addr.s_addr = iph->daddr;
161 sin.sin_port = ports[1];
162 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
163
164 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Paolo Abeniad959032016-11-04 11:28:58 +0100167void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk,
168 struct sk_buff *skb, int tlen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Paolo Abeniad959032016-11-04 11:28:58 +0100170 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000171 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /* Ordered by supposed usage frequency */
Tom Herbertc44d13d2015-01-05 13:56:15 -0800174 if (flags & IP_CMSG_PKTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 ip_cmsg_recv_pktinfo(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Tom Herbertc44d13d2015-01-05 13:56:15 -0800177 flags &= ~IP_CMSG_PKTINFO;
178 if (!flags)
179 return;
180 }
181
182 if (flags & IP_CMSG_TTL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 ip_cmsg_recv_ttl(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Tom Herbertc44d13d2015-01-05 13:56:15 -0800185 flags &= ~IP_CMSG_TTL;
186 if (!flags)
187 return;
188 }
189
190 if (flags & IP_CMSG_TOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 ip_cmsg_recv_tos(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Tom Herbertc44d13d2015-01-05 13:56:15 -0800193 flags &= ~IP_CMSG_TOS;
194 if (!flags)
195 return;
196 }
197
198 if (flags & IP_CMSG_RECVOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 ip_cmsg_recv_opts(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Tom Herbertc44d13d2015-01-05 13:56:15 -0800201 flags &= ~IP_CMSG_RECVOPTS;
202 if (!flags)
203 return;
204 }
205
206 if (flags & IP_CMSG_RETOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 ip_cmsg_recv_retopts(msg, skb);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800208
Tom Herbertc44d13d2015-01-05 13:56:15 -0800209 flags &= ~IP_CMSG_RETOPTS;
210 if (!flags)
211 return;
212 }
213
214 if (flags & IP_CMSG_PASSSEC) {
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800215 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800216
Tom Herbertc44d13d2015-01-05 13:56:15 -0800217 flags &= ~IP_CMSG_PASSSEC;
218 if (!flags)
219 return;
220 }
221
Tom Herbertad6f9392015-01-05 13:56:17 -0800222 if (flags & IP_CMSG_ORIGDSTADDR) {
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800223 ip_cmsg_recv_dstaddr(msg, skb);
224
Tom Herbertad6f9392015-01-05 13:56:17 -0800225 flags &= ~IP_CMSG_ORIGDSTADDR;
226 if (!flags)
227 return;
228 }
229
230 if (flags & IP_CMSG_CHECKSUM)
Eric Dumazet10df8e62016-10-23 18:03:06 -0700231 ip_cmsg_recv_checksum(msg, skb, tlen, offset);
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400232
233 if (flags & IP_CMSG_RECVFRAGSIZE)
234 ip_cmsg_recv_fragsize(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
Tom Herbert5961de92015-01-05 13:56:16 -0800236EXPORT_SYMBOL(ip_cmsg_recv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400238int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100239 bool allow_ipv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Francesco Fuscof02db312013-09-24 15:43:08 +0200241 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 struct cmsghdr *cmsg;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400243 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Gu Zhengf95b4142014-12-11 11:22:04 +0800245 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!CMSG_OK(msg, cmsg))
247 return -EINVAL;
Eric Dumazet5337b5b2014-11-10 17:54:25 -0800248#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100249 if (allow_ipv6 &&
250 cmsg->cmsg_level == SOL_IPV6 &&
251 cmsg->cmsg_type == IPV6_PKTINFO) {
252 struct in6_pktinfo *src_info;
253
254 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
255 return -EINVAL;
256 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
257 if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
258 return -EINVAL;
259 ipc->oif = src_info->ipi6_ifindex;
260 ipc->addr = src_info->ipi6_addr.s6_addr32[3];
261 continue;
262 }
263#endif
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400264 if (cmsg->cmsg_level == SOL_SOCKET) {
Eric Dumazet26326162016-05-13 06:14:37 -0700265 err = __sock_cmsg_send(sk, msg, cmsg, &ipc->sockc);
266 if (err)
267 return err;
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400268 continue;
269 }
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (cmsg->cmsg_level != SOL_IP)
272 continue;
273 switch (cmsg->cmsg_type) {
274 case IP_RETOPTS:
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800275 err = cmsg->cmsg_len - sizeof(struct cmsghdr);
Eric Dumazet91948302016-02-04 06:23:28 -0800276
277 /* Our caller is responsible for freeing ipc->opt */
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700278 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
279 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (err)
281 return err;
282 break;
283 case IP_PKTINFO:
284 {
285 struct in_pktinfo *info;
286 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
287 return -EINVAL;
288 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
289 ipc->oif = info->ipi_ifindex;
290 ipc->addr = info->ipi_spec_dst.s_addr;
291 break;
292 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200293 case IP_TTL:
294 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
295 return -EINVAL;
296 val = *(int *)CMSG_DATA(cmsg);
297 if (val < 1 || val > 255)
298 return -EINVAL;
299 ipc->ttl = val;
300 break;
301 case IP_TOS:
Eric Dumazete895cdc2016-09-07 21:52:56 -0700302 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)))
303 val = *(int *)CMSG_DATA(cmsg);
304 else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8)))
305 val = *(u8 *)CMSG_DATA(cmsg);
306 else
Francesco Fuscof02db312013-09-24 15:43:08 +0200307 return -EINVAL;
Francesco Fuscof02db312013-09-24 15:43:08 +0200308 if (val < 0 || val > 255)
309 return -EINVAL;
310 ipc->tos = val;
311 ipc->priority = rt_tos2priority(ipc->tos);
312 break;
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 default:
315 return -EINVAL;
316 }
317 }
318 return 0;
319}
320
321
322/* Special input handler for packets caught by router alert option.
323 They are selected only by protocol field, and then processed likely
324 local ones; but only if someone wants them! Otherwise, router
325 not running rsvpd will kill RSVP.
326
327 It is user level problem, what it will make with them.
328 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
329 but receiver should be enough clever f.e. to forward mtrace requests,
330 sent to multicast group to reach destination designated router.
331 */
Eric Dumazet43a951e2010-10-25 03:32:44 +0000332struct ip_ra_chain __rcu *ip_ra_chain;
Eric Dumazet66018502010-06-07 03:12:08 +0000333static DEFINE_SPINLOCK(ip_ra_lock);
334
Eric Dumazet592fcb92010-06-09 16:21:07 +0000335
336static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000337{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000338 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
339
340 sock_put(ra->saved_sk);
341 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000342}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700344int ip_ra_control(struct sock *sk, unsigned char on,
345 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000347 struct ip_ra_chain *ra, *new_ra;
348 struct ip_ra_chain __rcu **rap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000350 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -EINVAL;
352
353 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
354
Eric Dumazet66018502010-06-07 03:12:08 +0000355 spin_lock_bh(&ip_ra_lock);
Eric Dumazet43a951e2010-10-25 03:32:44 +0000356 for (rap = &ip_ra_chain;
357 (ra = rcu_dereference_protected(*rap,
358 lockdep_is_held(&ip_ra_lock))) != NULL;
359 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (ra->sk == sk) {
361 if (on) {
Eric Dumazet66018502010-06-07 03:12:08 +0000362 spin_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800363 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return -EADDRINUSE;
365 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000366 /* dont let ip_call_ra_chain() use sk again */
367 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700368 RCU_INIT_POINTER(*rap, ra->next);
Eric Dumazet66018502010-06-07 03:12:08 +0000369 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 if (ra->destructor)
372 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000373 /*
374 * Delay sock_put(sk) and kfree(ra) after one rcu grace
375 * period. This guarantee ip_call_ra_chain() dont need
376 * to mess with socket refcounts.
377 */
378 ra->saved_sk = sk;
379 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return 0;
381 }
382 }
Ian Morris51456b22015-04-03 09:17:26 +0100383 if (!new_ra) {
Eric Dumazet66018502010-06-07 03:12:08 +0000384 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return -ENOBUFS;
386 }
387 new_ra->sk = sk;
388 new_ra->destructor = destructor;
389
Eric Dumazet8e380f02014-09-09 08:11:41 -0700390 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000391 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 sock_hold(sk);
Eric Dumazet66018502010-06-07 03:12:08 +0000393 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 return 0;
396}
397
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900398void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700399 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct sock_exterr_skb *serr;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 skb = skb_clone(skb, GFP_ATOMIC);
404 if (!skb)
405 return;
406
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900407 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 serr->ee.ee_errno = err;
409 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300410 serr->ee.ee_type = icmp_hdr(skb)->type;
411 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 serr->ee.ee_pad = 0;
413 serr->ee.ee_info = info;
414 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300415 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700416 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 serr->port = port;
418
Ian Morris00db4122015-04-03 09:17:27 +0100419 if (skb_pull(skb, payload - skb->data)) {
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300420 skb_reset_transport_header(skb);
421 if (sock_queue_err_skb(sk, skb) == 0)
422 return;
423 }
424 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Al Viro05790162006-09-27 18:33:40 -0700427void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct inet_sock *inet = inet_sk(sk);
430 struct sock_exterr_skb *serr;
431 struct iphdr *iph;
432 struct sk_buff *skb;
433
434 if (!inet->recverr)
435 return;
436
437 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
438 if (!skb)
439 return;
440
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300441 skb_put(skb, sizeof(struct iphdr));
442 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700443 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 iph->daddr = daddr;
445
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900446 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 serr->ee.ee_errno = err;
448 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900449 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 serr->ee.ee_code = 0;
451 serr->ee.ee_pad = 0;
452 serr->ee.ee_info = info;
453 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700454 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 serr->port = port;
456
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700457 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300458 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 if (sock_queue_err_skb(sk, skb))
461 kfree_skb(skb);
462}
463
Julian Anastasov34b99df2015-06-23 08:34:39 +0300464/* For some errors we have valid addr_offset even with zero payload and
465 * zero port. Also, addr_offset should be supported if port is set.
466 */
467static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
468{
469 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
470 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
471}
472
Willem de Bruijnc247f052015-03-07 20:33:22 -0500473/* IPv4 supports cmsg on all imcp errors and some timestamps
474 *
475 * Timestamp code paths do not initialize the fields expected by cmsg:
476 * the PKTINFO fields in skb->cb[]. Fill those in here.
477 */
478static bool ipv4_datagram_support_cmsg(const struct sock *sk,
479 struct sk_buff *skb,
480 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500481{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500482 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500483
Willem de Bruijnc247f052015-03-07 20:33:22 -0500484 if (ee_origin == SO_EE_ORIGIN_ICMP)
485 return true;
486
487 if (ee_origin == SO_EE_ORIGIN_LOCAL)
488 return false;
489
490 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
Willem de Bruijn1862d622017-04-12 19:24:35 -0400491 * timestamp with egress dev. Not possible for packets without iif
Willem de Bruijnc247f052015-03-07 20:33:22 -0500492 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
493 */
Willem de Bruijn1862d622017-04-12 19:24:35 -0400494 info = PKTINFO_SKB_CB(skb);
495 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
496 !info->ipi_ifindex)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500497 return false;
498
499 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500500 return true;
501}
502
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900503/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * Handle MSG_ERRQUEUE
505 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100506int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400509 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100510 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct {
512 struct sock_extended_err ee;
513 struct sockaddr_in offender;
514 } errhdr;
515 int err;
516 int copied;
517
Willem de Bruijn7ce875e2014-11-30 22:22:33 -0500518 WARN_ON_ONCE(sk->sk_family == AF_INET6);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400521 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100522 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto out;
524
525 copied = skb->len;
526 if (copied > len) {
527 msg->msg_flags |= MSG_TRUNC;
528 copied = len;
529 }
David S. Miller51f3d022014-11-05 16:46:40 -0500530 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Eric Dumazet960a2622016-04-21 22:27:32 -0700531 if (unlikely(err)) {
532 kfree_skb(skb);
533 return err;
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 sock_recv_timestamp(msg, sk, skb);
536
537 serr = SKB_EXT_ERR(skb);
538
Julian Anastasov34b99df2015-06-23 08:34:39 +0300539 if (sin && ipv4_datagram_support_addr(serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700541 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
542 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 sin->sin_port = serr->port;
544 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100545 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547
548 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
549 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500550 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500551
Willem de Bruijnc247f052015-03-07 20:33:22 -0500552 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700554 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500555 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 ip_cmsg_recv(msg, skb);
557 }
558
559 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
560
561 /* Now we could try to dump offended packet options */
562
563 msg->msg_flags |= MSG_ERRQUEUE;
564 err = copied;
565
Eric Dumazet960a2622016-04-21 22:27:32 -0700566 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567out:
568 return err;
569}
570
571
572/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700573 * Socket option code for IP. This is the end of the line after any
574 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300576static bool setsockopt_needs_rtnl(int optname)
577{
578 switch (optname) {
579 case IP_ADD_MEMBERSHIP:
580 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300581 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300582 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300583 case IP_DROP_SOURCE_MEMBERSHIP:
584 case IP_MSFILTER:
585 case IP_UNBLOCK_SOURCE:
586 case MCAST_BLOCK_SOURCE:
587 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300588 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300589 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300590 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300591 case MCAST_LEAVE_SOURCE_GROUP:
592 case MCAST_UNBLOCK_SOURCE:
WANG Cong1215e512017-04-12 12:32:13 -0700593 case IP_ROUTER_ALERT:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300594 return true;
595 }
596 return false;
597}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800599static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700600 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 struct inet_sock *inet = inet_sk(sk);
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200603 struct net *net = sock_net(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800604 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300605 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Xi Wang0c9f79b2012-11-11 11:20:01 +0000607 switch (optname) {
608 case IP_PKTINFO:
609 case IP_RECVTTL:
610 case IP_RECVOPTS:
611 case IP_RECVTOS:
612 case IP_RETOPTS:
613 case IP_TOS:
614 case IP_TTL:
615 case IP_HDRINCL:
616 case IP_MTU_DISCOVER:
617 case IP_RECVERR:
618 case IP_ROUTER_ALERT:
619 case IP_FREEBIND:
620 case IP_PASSSEC:
621 case IP_TRANSPARENT:
622 case IP_MINTTL:
623 case IP_NODEFRAG:
Eric Dumazet90c337d2015-06-06 21:17:57 -0700624 case IP_BIND_ADDRESS_NO_PORT:
Xi Wang0c9f79b2012-11-11 11:20:01 +0000625 case IP_UNICAST_IF:
626 case IP_MULTICAST_TTL:
627 case IP_MULTICAST_ALL:
628 case IP_MULTICAST_LOOP:
629 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800630 case IP_CHECKSUM:
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400631 case IP_RECVFRAGSIZE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (optlen >= sizeof(int)) {
633 if (get_user(val, (int __user *) optval))
634 return -EFAULT;
635 } else if (optlen >= sizeof(char)) {
636 unsigned char ucval;
637
638 if (get_user(ucval, (unsigned char __user *) optval))
639 return -EFAULT;
640 val = (int) ucval;
641 }
642 }
643
644 /* If optlen==0, it is equivalent to val == 0 */
645
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800646 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800647 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300650 if (needs_rtnl)
651 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 lock_sock(sk);
653
654 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800655 case IP_OPTIONS:
656 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000657 struct ip_options_rcu *old, *opt = NULL;
658
roel kluin65a1c4f2009-10-23 05:59:21 +0000659 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800660 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900661 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700662 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800663 if (err)
664 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000665 old = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200666 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800667 if (inet->is_icsk) {
668 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000669#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800670 if (sk->sk_family == PF_INET ||
671 (!((1 << sk->sk_state) &
672 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000673 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000675 if (old)
676 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800677 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000678 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800679 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000680#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000684 rcu_assign_pointer(inet->inet_opt, opt);
685 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800686 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800687 break;
688 }
689 case IP_PKTINFO:
690 if (val)
691 inet->cmsg_flags |= IP_CMSG_PKTINFO;
692 else
693 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
694 break;
695 case IP_RECVTTL:
696 if (val)
697 inet->cmsg_flags |= IP_CMSG_TTL;
698 else
699 inet->cmsg_flags &= ~IP_CMSG_TTL;
700 break;
701 case IP_RECVTOS:
702 if (val)
703 inet->cmsg_flags |= IP_CMSG_TOS;
704 else
705 inet->cmsg_flags &= ~IP_CMSG_TOS;
706 break;
707 case IP_RECVOPTS:
708 if (val)
709 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
710 else
711 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
712 break;
713 case IP_RETOPTS:
714 if (val)
715 inet->cmsg_flags |= IP_CMSG_RETOPTS;
716 else
717 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
718 break;
719 case IP_PASSSEC:
720 if (val)
721 inet->cmsg_flags |= IP_CMSG_PASSSEC;
722 else
723 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
724 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800725 case IP_RECVORIGDSTADDR:
726 if (val)
727 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
728 else
729 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
730 break;
Tom Herbertad6f9392015-01-05 13:56:17 -0800731 case IP_CHECKSUM:
732 if (val) {
733 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
734 inet_inc_convert_csum(sk);
735 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
736 }
737 } else {
738 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
739 inet_dec_convert_csum(sk);
740 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
741 }
742 }
743 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -0400744 case IP_RECVFRAGSIZE:
745 if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM)
746 goto e_inval;
747 if (val)
748 inet->cmsg_flags |= IP_CMSG_RECVFRAGSIZE;
749 else
750 inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE;
751 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800752 case IP_TOS: /* This sets both TOS and Precedence */
753 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400754 val &= ~INET_ECN_MASK;
755 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800757 if (inet->tos != val) {
758 inet->tos = val;
759 sk->sk_priority = rt_tos2priority(val);
760 sk_dst_reset(sk);
761 }
762 break;
763 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700764 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800765 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000766 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800767 goto e_inval;
768 inet->uc_ttl = val;
769 break;
770 case IP_HDRINCL:
771 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800774 }
775 inet->hdrincl = val ? 1 : 0;
776 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000777 case IP_NODEFRAG:
778 if (sk->sk_type != SOCK_RAW) {
779 err = -ENOPROTOOPT;
780 break;
781 }
782 inet->nodefrag = val ? 1 : 0;
783 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -0700784 case IP_BIND_ADDRESS_NO_PORT:
785 inet->bind_address_no_port = val ? 1 : 0;
786 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800787 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100788 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800789 goto e_inval;
790 inet->pmtudisc = val;
791 break;
792 case IP_RECVERR:
793 inet->recverr = !!val;
794 if (!val)
795 skb_queue_purge(&sk->sk_error_queue);
796 break;
797 case IP_MULTICAST_TTL:
798 if (sk->sk_type == SOCK_STREAM)
799 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700800 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800801 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800802 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800803 val = 1;
804 if (val < 0 || val > 255)
805 goto e_inval;
806 inet->mc_ttl = val;
807 break;
808 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700809 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800810 goto e_inval;
811 inet->mc_loop = !!val;
812 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000813 case IP_UNICAST_IF:
814 {
815 struct net_device *dev = NULL;
816 int ifindex;
817
818 if (optlen != sizeof(int))
819 goto e_inval;
820
821 ifindex = (__force int)ntohl((__force __be32)val);
822 if (ifindex == 0) {
823 inet->uc_index = 0;
824 err = 0;
825 break;
826 }
827
828 dev = dev_get_by_index(sock_net(sk), ifindex);
829 err = -EADDRNOTAVAIL;
830 if (!dev)
831 break;
832 dev_put(dev);
833
834 err = -EINVAL;
835 if (sk->sk_bound_dev_if)
836 break;
837
838 inet->uc_index = ifindex;
839 err = 0;
840 break;
841 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800842 case IP_MULTICAST_IF:
843 {
844 struct ip_mreqn mreq;
845 struct net_device *dev = NULL;
David Ahern7bb387c2016-12-29 15:39:37 -0800846 int midx;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800847
848 if (sk->sk_type == SOCK_STREAM)
849 goto e_inval;
850 /*
851 * Check the arguments are allowable
852 */
853
Shan Wei09159212009-09-22 15:41:10 +0000854 if (optlen < sizeof(struct in_addr))
855 goto e_inval;
856
Stephen Hemminger132adf52007-03-08 20:44:43 -0800857 err = -EFAULT;
858 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800859 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800860 break;
861 } else {
862 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000863 if (optlen >= sizeof(struct ip_mreq)) {
864 if (copy_from_user(&mreq, optval,
865 sizeof(struct ip_mreq)))
866 break;
867 } else if (optlen >= sizeof(struct in_addr)) {
868 if (copy_from_user(&mreq.imr_address, optval,
869 sizeof(struct in_addr)))
870 break;
871 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800872 }
873
874 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700875 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800876 inet->mc_index = 0;
877 inet->mc_addr = 0;
878 err = 0;
879 break;
880 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900881 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000882 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800883 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800884 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000885 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800886
887
888 err = -EADDRNOTAVAIL;
889 if (!dev)
890 break;
David Ahern7bb387c2016-12-29 15:39:37 -0800891
892 midx = l3mdev_master_ifindex(dev);
893
Eric Dumazet55b80502009-10-19 06:41:58 +0000894 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800895
896 err = -EINVAL;
897 if (sk->sk_bound_dev_if &&
David Ahern7bb387c2016-12-29 15:39:37 -0800898 mreq.imr_ifindex != sk->sk_bound_dev_if &&
899 (!midx || midx != sk->sk_bound_dev_if))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800900 break;
901
902 inet->mc_index = mreq.imr_ifindex;
903 inet->mc_addr = mreq.imr_address.s_addr;
904 err = 0;
905 break;
906 }
907
908 case IP_ADD_MEMBERSHIP:
909 case IP_DROP_MEMBERSHIP:
910 {
911 struct ip_mreqn mreq;
912
Flavio Leitnera96fb492007-08-24 22:16:39 -0700913 err = -EPROTO;
914 if (inet_sk(sk)->is_icsk)
915 break;
916
Stephen Hemminger132adf52007-03-08 20:44:43 -0800917 if (optlen < sizeof(struct ip_mreq))
918 goto e_inval;
919 err = -EFAULT;
920 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800921 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800922 break;
923 } else {
924 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800925 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800926 break;
927 }
928
929 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300930 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800931 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300932 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800933 break;
934 }
935 case IP_MSFILTER:
936 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800937 struct ip_msfilter *msf;
938
939 if (optlen < IP_MSFILTER_SIZE(0))
940 goto e_inval;
941 if (optlen > sysctl_optmem_max) {
942 err = -ENOBUFS;
943 break;
944 }
945 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700946 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800947 err = -ENOBUFS;
948 break;
949 }
950 err = -EFAULT;
951 if (copy_from_user(msf, optval, optlen)) {
952 kfree(msf);
953 break;
954 }
955 /* numsrc >= (1G-4) overflow in 32 bits */
956 if (msf->imsf_numsrc >= 0x3ffffffcU ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +0200957 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800958 kfree(msf);
959 err = -ENOBUFS;
960 break;
961 }
962 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
963 kfree(msf);
964 err = -EINVAL;
965 break;
966 }
967 err = ip_mc_msfilter(sk, msf, 0);
968 kfree(msf);
969 break;
970 }
971 case IP_BLOCK_SOURCE:
972 case IP_UNBLOCK_SOURCE:
973 case IP_ADD_SOURCE_MEMBERSHIP:
974 case IP_DROP_SOURCE_MEMBERSHIP:
975 {
976 struct ip_mreq_source mreqs;
977 int omode, add;
978
979 if (optlen != sizeof(struct ip_mreq_source))
980 goto e_inval;
981 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
982 err = -EFAULT;
983 break;
984 }
985 if (optname == IP_BLOCK_SOURCE) {
986 omode = MCAST_EXCLUDE;
987 add = 1;
988 } else if (optname == IP_UNBLOCK_SOURCE) {
989 omode = MCAST_EXCLUDE;
990 add = 0;
991 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
992 struct ip_mreqn mreq;
993
994 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
995 mreq.imr_address.s_addr = mreqs.imr_interface;
996 mreq.imr_ifindex = 0;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300997 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800998 if (err && err != -EADDRINUSE)
999 break;
1000 omode = MCAST_INCLUDE;
1001 add = 1;
1002 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
1003 omode = MCAST_INCLUDE;
1004 add = 0;
1005 }
1006 err = ip_mc_source(add, omode, sk, &mreqs, 0);
1007 break;
1008 }
1009 case MCAST_JOIN_GROUP:
1010 case MCAST_LEAVE_GROUP:
1011 {
1012 struct group_req greq;
1013 struct sockaddr_in *psin;
1014 struct ip_mreqn mreq;
1015
1016 if (optlen < sizeof(struct group_req))
1017 goto e_inval;
1018 err = -EFAULT;
1019 if (copy_from_user(&greq, optval, sizeof(greq)))
1020 break;
1021 psin = (struct sockaddr_in *)&greq.gr_group;
1022 if (psin->sin_family != AF_INET)
1023 goto e_inval;
1024 memset(&mreq, 0, sizeof(mreq));
1025 mreq.imr_multiaddr = psin->sin_addr;
1026 mreq.imr_ifindex = greq.gr_interface;
1027
1028 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001029 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001030 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001031 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001032 break;
1033 }
1034 case MCAST_JOIN_SOURCE_GROUP:
1035 case MCAST_LEAVE_SOURCE_GROUP:
1036 case MCAST_BLOCK_SOURCE:
1037 case MCAST_UNBLOCK_SOURCE:
1038 {
1039 struct group_source_req greqs;
1040 struct ip_mreq_source mreqs;
1041 struct sockaddr_in *psin;
1042 int omode, add;
1043
1044 if (optlen != sizeof(struct group_source_req))
1045 goto e_inval;
1046 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
1047 err = -EFAULT;
1048 break;
1049 }
1050 if (greqs.gsr_group.ss_family != AF_INET ||
1051 greqs.gsr_source.ss_family != AF_INET) {
1052 err = -EADDRNOTAVAIL;
1053 break;
1054 }
1055 psin = (struct sockaddr_in *)&greqs.gsr_group;
1056 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
1057 psin = (struct sockaddr_in *)&greqs.gsr_source;
1058 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
1059 mreqs.imr_interface = 0; /* use index for mc_source */
1060
1061 if (optname == MCAST_BLOCK_SOURCE) {
1062 omode = MCAST_EXCLUDE;
1063 add = 1;
1064 } else if (optname == MCAST_UNBLOCK_SOURCE) {
1065 omode = MCAST_EXCLUDE;
1066 add = 0;
1067 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
1068 struct ip_mreqn mreq;
1069
1070 psin = (struct sockaddr_in *)&greqs.gsr_group;
1071 mreq.imr_multiaddr = psin->sin_addr;
1072 mreq.imr_address.s_addr = 0;
1073 mreq.imr_ifindex = greqs.gsr_interface;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001074 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001075 if (err && err != -EADDRINUSE)
1076 break;
1077 greqs.gsr_interface = mreq.imr_ifindex;
1078 omode = MCAST_INCLUDE;
1079 add = 1;
1080 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1081 omode = MCAST_INCLUDE;
1082 add = 0;
1083 }
1084 err = ip_mc_source(add, omode, sk, &mreqs,
1085 greqs.gsr_interface);
1086 break;
1087 }
1088 case MCAST_MSFILTER:
1089 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001090 struct sockaddr_in *psin;
1091 struct ip_msfilter *msf = NULL;
1092 struct group_filter *gsf = NULL;
1093 int msize, i, ifindex;
1094
1095 if (optlen < GROUP_FILTER_SIZE(0))
1096 goto e_inval;
1097 if (optlen > sysctl_optmem_max) {
1098 err = -ENOBUFS;
1099 break;
1100 }
Jianjun Kong09cb1052008-11-03 00:27:11 -08001101 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001102 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001103 err = -ENOBUFS;
1104 break;
1105 }
1106 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001107 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001108 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001109
Stephen Hemminger132adf52007-03-08 20:44:43 -08001110 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1111 if (gsf->gf_numsrc >= 0x1ffffff ||
Nikolay Borisov166b6b22016-02-08 23:29:22 +02001112 gsf->gf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001113 err = -ENOBUFS;
1114 goto mc_msf_out;
1115 }
1116 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
1117 err = -EINVAL;
1118 goto mc_msf_out;
1119 }
1120 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -08001121 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001122 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001123 err = -ENOBUFS;
1124 goto mc_msf_out;
1125 }
1126 ifindex = gsf->gf_interface;
1127 psin = (struct sockaddr_in *)&gsf->gf_group;
1128 if (psin->sin_family != AF_INET) {
1129 err = -EADDRNOTAVAIL;
1130 goto mc_msf_out;
1131 }
1132 msf->imsf_multiaddr = psin->sin_addr.s_addr;
1133 msf->imsf_interface = 0;
1134 msf->imsf_fmode = gsf->gf_fmode;
1135 msf->imsf_numsrc = gsf->gf_numsrc;
1136 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001137 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001138 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
1139
1140 if (psin->sin_family != AF_INET)
1141 goto mc_msf_out;
1142 msf->imsf_slist[i] = psin->sin_addr.s_addr;
1143 }
1144 kfree(gsf);
1145 gsf = NULL;
1146
1147 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001148mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -08001149 kfree(msf);
1150 kfree(gsf);
1151 break;
1152 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001153 case IP_MULTICAST_ALL:
1154 if (optlen < 1)
1155 goto e_inval;
1156 if (val != 0 && val != 1)
1157 goto e_inval;
1158 inet->mc_all = val;
1159 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001160 case IP_ROUTER_ALERT:
1161 err = ip_ra_control(sk, val ? 1 : 0, NULL);
1162 break;
1163
1164 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001165 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001166 goto e_inval;
1167 inet->freebind = !!val;
1168 break;
1169
1170 case IP_IPSEC_POLICY:
1171 case IP_XFRM_POLICY:
1172 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001173 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001174 break;
1175 err = xfrm_user_policy(sk, optname, optval, optlen);
1176 break;
1177
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001178 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001179 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1180 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001181 err = -EPERM;
1182 break;
1183 }
1184 if (optlen < 1)
1185 goto e_inval;
1186 inet->transparent = !!val;
1187 break;
1188
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001189 case IP_MINTTL:
1190 if (optlen < 1)
1191 goto e_inval;
1192 if (val < 0 || val > 255)
1193 goto e_inval;
1194 inet->min_ttl = val;
1195 break;
1196
Stephen Hemminger132adf52007-03-08 20:44:43 -08001197 default:
1198 err = -ENOPROTOOPT;
1199 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001202 if (needs_rtnl)
1203 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return err;
1205
1206e_inval:
1207 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001208 if (needs_rtnl)
1209 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return -EINVAL;
1211}
1212
Eric Dumazetf84af322010-04-28 15:31:51 -07001213/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001214 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001215 * @sk: socket
1216 * @skb: buffer
1217 *
David S. Miller35ebf652012-06-28 03:59:11 -07001218 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1219 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001220 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001221 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001222void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001223{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001224 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001225 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1226 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001227
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001228 if (prepare && skb_rtable(skb)) {
David Ahern0b922b72016-05-10 11:19:51 -07001229 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1230 * which has interface index (iif) as the first member of the
1231 * underlying inet{6}_skb_parm struct. This code then overlays
1232 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001233 * element so the iif is picked up from the prior IPCB. If iif
1234 * is the loopback interface, then return the sending interface
1235 * (e.g., process binds socket to eth0 for Tx which is
1236 * redirected to loopback in the rtable/dst).
David Ahern0b922b72016-05-10 11:19:51 -07001237 */
Wei Zhangf0c16ba2016-12-29 16:45:04 +08001238 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
1239 pktinfo->ipi_ifindex = inet_iif(skb);
1240
David S. Miller35ebf652012-06-28 03:59:11 -07001241 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001242 } else {
1243 pktinfo->ipi_ifindex = 0;
1244 pktinfo->ipi_spec_dst.s_addr = 0;
1245 }
Eric Dumazet34b2cef2017-02-04 11:16:52 -08001246 /* We need to keep the dst for __ip_options_echo()
1247 * We could restrict the test to opt.ts_needtime || opt.srr,
1248 * but the following is good enough as IP options are not often used.
1249 */
1250 if (unlikely(IPCB(skb)->opt.optlen))
1251 skb_dst_force(skb);
1252 else
1253 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001254}
Eric Dumazetf84af322010-04-28 15:31:51 -07001255
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001256int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001257 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001258{
1259 int err;
1260
1261 if (level != SOL_IP)
1262 return -ENOPROTOOPT;
1263
1264 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1265#ifdef CONFIG_NETFILTER
1266 /* we need to exclude all possible ENOPROTOOPTs except default case */
1267 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001268 optname != IP_IPSEC_POLICY &&
1269 optname != IP_XFRM_POLICY &&
1270 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001271 lock_sock(sk);
1272 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1273 release_sock(sk);
1274 }
1275#endif
1276 return err;
1277}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001278EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001279
1280#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001281int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001282 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001283{
1284 int err;
1285
1286 if (level != SOL_IP)
1287 return -ENOPROTOOPT;
1288
David L Stevensdae50292008-04-27 01:06:07 -07001289 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1290 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1291 ip_setsockopt);
1292
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001293 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1294#ifdef CONFIG_NETFILTER
1295 /* we need to exclude all possible ENOPROTOOPTs except default case */
1296 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001297 optname != IP_IPSEC_POLICY &&
1298 optname != IP_XFRM_POLICY &&
1299 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001300 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001301 err = compat_nf_setsockopt(sk, PF_INET, optname,
1302 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001303 release_sock(sk);
1304 }
1305#endif
1306 return err;
1307}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001308EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001309#endif
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001312 * Get the options. Note for future reference. The GET of IP options gets
1313 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 */
1315
WANG Cong87e9f032015-11-03 15:41:16 -08001316static bool getsockopt_needs_rtnl(int optname)
1317{
1318 switch (optname) {
1319 case IP_MSFILTER:
1320 case MCAST_MSFILTER:
1321 return true;
1322 }
1323 return false;
1324}
1325
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001326static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001327 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 struct inet_sock *inet = inet_sk(sk);
WANG Cong87e9f032015-11-03 15:41:16 -08001330 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1331 int val, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001333
Stephen Hemminger132adf52007-03-08 20:44:43 -08001334 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return -EOPNOTSUPP;
1336
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001337 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001338 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Jianjun Kong09cb1052008-11-03 00:27:11 -08001340 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001342 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001344
WANG Cong87e9f032015-11-03 15:41:16 -08001345 if (needs_rtnl)
1346 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 lock_sock(sk);
1348
Stephen Hemminger132adf52007-03-08 20:44:43 -08001349 switch (optname) {
1350 case IP_OPTIONS:
1351 {
1352 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001353 struct ip_options *opt = (struct ip_options *)optbuf;
1354 struct ip_options_rcu *inet_opt;
1355
1356 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001357 lockdep_sock_is_held(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001358 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001359 if (inet_opt)
1360 memcpy(optbuf, &inet_opt->opt,
1361 sizeof(struct ip_options) +
1362 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001363 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Stephen Hemminger132adf52007-03-08 20:44:43 -08001365 if (opt->optlen == 0)
1366 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Stephen Hemminger132adf52007-03-08 20:44:43 -08001368 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Stephen Hemminger132adf52007-03-08 20:44:43 -08001370 len = min_t(unsigned int, len, opt->optlen);
1371 if (put_user(len, optlen))
1372 return -EFAULT;
1373 if (copy_to_user(optval, opt->__data, len))
1374 return -EFAULT;
1375 return 0;
1376 }
1377 case IP_PKTINFO:
1378 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1379 break;
1380 case IP_RECVTTL:
1381 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1382 break;
1383 case IP_RECVTOS:
1384 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1385 break;
1386 case IP_RECVOPTS:
1387 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1388 break;
1389 case IP_RETOPTS:
1390 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1391 break;
1392 case IP_PASSSEC:
1393 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1394 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001395 case IP_RECVORIGDSTADDR:
1396 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1397 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001398 case IP_CHECKSUM:
1399 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1400 break;
Willem de Bruijn70ecc242016-11-02 11:02:16 -04001401 case IP_RECVFRAGSIZE:
1402 val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0;
1403 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001404 case IP_TOS:
1405 val = inet->tos;
1406 break;
1407 case IP_TTL:
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001408 {
1409 struct net *net = sock_net(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001410 val = (inet->uc_ttl == -1 ?
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001411 net->ipv4.sysctl_ip_default_ttl :
Stephen Hemminger132adf52007-03-08 20:44:43 -08001412 inet->uc_ttl);
1413 break;
Nikolay Borisovfa50d972016-02-15 12:11:27 +02001414 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001415 case IP_HDRINCL:
1416 val = inet->hdrincl;
1417 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001418 case IP_NODEFRAG:
1419 val = inet->nodefrag;
1420 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001421 case IP_BIND_ADDRESS_NO_PORT:
1422 val = inet->bind_address_no_port;
1423 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001424 case IP_MTU_DISCOVER:
1425 val = inet->pmtudisc;
1426 break;
1427 case IP_MTU:
1428 {
1429 struct dst_entry *dst;
1430 val = 0;
1431 dst = sk_dst_get(sk);
1432 if (dst) {
1433 val = dst_mtu(dst);
1434 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001436 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001438 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001440 break;
1441 }
1442 case IP_RECVERR:
1443 val = inet->recverr;
1444 break;
1445 case IP_MULTICAST_TTL:
1446 val = inet->mc_ttl;
1447 break;
1448 case IP_MULTICAST_LOOP:
1449 val = inet->mc_loop;
1450 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001451 case IP_UNICAST_IF:
1452 val = (__force int)htonl((__u32) inet->uc_index);
1453 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001454 case IP_MULTICAST_IF:
1455 {
1456 struct in_addr addr;
1457 len = min_t(unsigned int, len, sizeof(struct in_addr));
1458 addr.s_addr = inet->mc_addr;
1459 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Stephen Hemminger132adf52007-03-08 20:44:43 -08001461 if (put_user(len, optlen))
1462 return -EFAULT;
1463 if (copy_to_user(optval, &addr, len))
1464 return -EFAULT;
1465 return 0;
1466 }
1467 case IP_MSFILTER:
1468 {
1469 struct ip_msfilter msf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001470
1471 if (len < IP_MSFILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001472 err = -EINVAL;
1473 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001475 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001476 err = -EFAULT;
1477 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001479 err = ip_mc_msfget(sk, &msf,
1480 (struct ip_msfilter __user *)optval, optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001481 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001482 }
1483 case MCAST_MSFILTER:
1484 {
1485 struct group_filter gsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Stephen Hemminger132adf52007-03-08 20:44:43 -08001487 if (len < GROUP_FILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001488 err = -EINVAL;
1489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001491 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001492 err = -EFAULT;
1493 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001494 }
1495 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001496 (struct group_filter __user *)optval,
1497 optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001498 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001499 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001500 case IP_MULTICAST_ALL:
1501 val = inet->mc_all;
1502 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001503 case IP_PKTOPTIONS:
1504 {
1505 struct msghdr msg;
1506
1507 release_sock(sk);
1508
1509 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001511
Karoly Kemenyc54a5e02014-07-27 12:29:07 +02001512 msg.msg_control = (__force void *) optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001513 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001514 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001515
1516 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1517 struct in_pktinfo info;
1518
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001519 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1520 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001521 info.ipi_ifindex = inet->mc_index;
1522 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1523 }
1524 if (inet->cmsg_flags & IP_CMSG_TTL) {
1525 int hlim = inet->mc_ttl;
1526 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1527 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001528 if (inet->cmsg_flags & IP_CMSG_TOS) {
1529 int tos = inet->rcv_tos;
1530 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1531 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001532 len -= msg.msg_controllen;
1533 return put_user(len, optlen);
1534 }
1535 case IP_FREEBIND:
1536 val = inet->freebind;
1537 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001538 case IP_TRANSPARENT:
1539 val = inet->transparent;
1540 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001541 case IP_MINTTL:
1542 val = inet->min_ttl;
1543 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001544 default:
1545 release_sock(sk);
1546 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001549
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001550 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 unsigned char ucval = (unsigned char)val;
1552 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001553 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001555 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return -EFAULT;
1557 } else {
1558 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001559 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001561 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return -EFAULT;
1563 }
1564 return 0;
WANG Cong87e9f032015-11-03 15:41:16 -08001565
1566out:
1567 release_sock(sk);
1568 if (needs_rtnl)
1569 rtnl_unlock();
1570 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001573int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001574 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001575{
1576 int err;
1577
Daniel Balutadd23198e2011-08-07 22:31:07 -07001578 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001579#ifdef CONFIG_NETFILTER
1580 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001581 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1582 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001583 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001584
Jianjun Kong09cb1052008-11-03 00:27:11 -08001585 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001586 return -EFAULT;
1587
1588 lock_sock(sk);
1589 err = nf_getsockopt(sk, PF_INET, optname, optval,
1590 &len);
1591 release_sock(sk);
1592 if (err >= 0)
1593 err = put_user(len, optlen);
1594 return err;
1595 }
1596#endif
1597 return err;
1598}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001599EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001600
1601#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001602int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1603 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001604{
David L Stevens42908c62008-04-29 03:23:22 -07001605 int err;
1606
1607 if (optname == MCAST_MSFILTER)
1608 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1609 ip_getsockopt);
1610
Daniel Balutadd23198e2011-08-07 22:31:07 -07001611 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1612 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001613
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001614#ifdef CONFIG_NETFILTER
1615 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001616 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1617 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001618 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001619
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001620 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001621 return -EFAULT;
1622
1623 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001624 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001625 release_sock(sk);
1626 if (err >= 0)
1627 err = put_user(len, optlen);
1628 return err;
1629 }
1630#endif
1631 return err;
1632}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001633EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001634#endif