blob: a50124260f5a4aaa98a3e4a582dbcbdbc236e370 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * The IP to API glue.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: see ip.c
9 *
10 * Fixes:
11 * Many : Split from ip.c , see ip.c for history.
12 * Martin Mares : TOS setting fixed.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090013 * Alan Cox : Fixed a couple of oopses in Martin's
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * TOS tweaks.
15 * Mike McLagan : Routing by source
16 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/skbuff.h>
22#include <linux/ip.h>
23#include <linux/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020024#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/sock.h>
28#include <net/ip.h>
29#include <net/icmp.h>
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -080030#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/udp.h>
32#include <linux/igmp.h>
33#include <linux/netfilter.h>
34#include <linux/route.h>
35#include <linux/mroute.h>
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -040036#include <net/inet_ecn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <net/route.h>
38#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070039#include <net/compat.h>
Tom Herbertad6f9392015-01-05 13:56:17 -080040#include <net/checksum.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000041#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <net/transp_v6.h>
43#endif
David S. Miller35ebf652012-06-28 03:59:11 -070044#include <net/ip_fib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <linux/errqueue.h>
47#include <asm/uaccess.h>
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * SOL_IP control messages.
51 */
52
53static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
54{
Eric Dumazetd826eb12011-11-09 07:24:35 +000055 struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070057 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
60}
61
62static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
63{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070064 int ttl = ip_hdr(skb)->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
66}
67
68static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
69{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070070 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
74{
75 if (IPCB(skb)->opt.optlen == 0)
76 return;
77
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070078 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
79 ip_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82
83static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
84{
85 unsigned char optbuf[sizeof(struct ip_options) + 40];
Daniel Baluta5e73ea12012-04-15 01:34:41 +000086 struct ip_options *opt = (struct ip_options *)optbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 if (IPCB(skb)->opt.optlen == 0)
89 return;
90
91 if (ip_options_echo(opt, skb)) {
92 msg->msg_flags |= MSG_CTRUNC;
93 return;
94 }
95 ip_options_undo(opt);
96
97 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
98}
99
Tom Herbertad6f9392015-01-05 13:56:17 -0800100static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb,
101 int offset)
102{
103 __wsum csum = skb->csum;
104
105 if (skb->ip_summed != CHECKSUM_COMPLETE)
106 return;
107
108 if (offset != 0)
109 csum = csum_sub(csum, csum_partial(skb->data, offset, 0));
110
111 put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum);
112}
113
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800114static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
115{
116 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700117 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800118 int err;
119
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700120 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
121 if (err)
122 return;
123
124 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800125 if (err)
126 return;
127
128 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700129 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800130}
131
Harvey Harrison21d1a162008-11-20 01:54:27 -0800132static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800133{
134 struct sockaddr_in sin;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000135 const struct iphdr *iph = ip_hdr(skb);
Harvey Harrison21d1a162008-11-20 01:54:27 -0800136 __be16 *ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800137
138 if (skb_transport_offset(skb) + 4 > skb->len)
139 return;
140
141 /* All current transport protocols have the port numbers in the
142 * first four bytes of the transport header and this function is
143 * written with this assumption in mind.
144 */
145
146 sin.sin_family = AF_INET;
147 sin.sin_addr.s_addr = iph->daddr;
148 sin.sin_port = ports[1];
149 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
150
151 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
152}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Tom Herbert5961de92015-01-05 13:56:16 -0800154void ip_cmsg_recv_offset(struct msghdr *msg, struct sk_buff *skb,
155 int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct inet_sock *inet = inet_sk(skb->sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000158 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 /* Ordered by supposed usage frequency */
Tom Herbertc44d13d2015-01-05 13:56:15 -0800161 if (flags & IP_CMSG_PKTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 ip_cmsg_recv_pktinfo(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Tom Herbertc44d13d2015-01-05 13:56:15 -0800164 flags &= ~IP_CMSG_PKTINFO;
165 if (!flags)
166 return;
167 }
168
169 if (flags & IP_CMSG_TTL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 ip_cmsg_recv_ttl(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Tom Herbertc44d13d2015-01-05 13:56:15 -0800172 flags &= ~IP_CMSG_TTL;
173 if (!flags)
174 return;
175 }
176
177 if (flags & IP_CMSG_TOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 ip_cmsg_recv_tos(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Tom Herbertc44d13d2015-01-05 13:56:15 -0800180 flags &= ~IP_CMSG_TOS;
181 if (!flags)
182 return;
183 }
184
185 if (flags & IP_CMSG_RECVOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 ip_cmsg_recv_opts(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Tom Herbertc44d13d2015-01-05 13:56:15 -0800188 flags &= ~IP_CMSG_RECVOPTS;
189 if (!flags)
190 return;
191 }
192
193 if (flags & IP_CMSG_RETOPTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 ip_cmsg_recv_retopts(msg, skb);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800195
Tom Herbertc44d13d2015-01-05 13:56:15 -0800196 flags &= ~IP_CMSG_RETOPTS;
197 if (!flags)
198 return;
199 }
200
201 if (flags & IP_CMSG_PASSSEC) {
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800202 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800203
Tom Herbertc44d13d2015-01-05 13:56:15 -0800204 flags &= ~IP_CMSG_PASSSEC;
205 if (!flags)
206 return;
207 }
208
Tom Herbertad6f9392015-01-05 13:56:17 -0800209 if (flags & IP_CMSG_ORIGDSTADDR) {
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800210 ip_cmsg_recv_dstaddr(msg, skb);
211
Tom Herbertad6f9392015-01-05 13:56:17 -0800212 flags &= ~IP_CMSG_ORIGDSTADDR;
213 if (!flags)
214 return;
215 }
216
217 if (flags & IP_CMSG_CHECKSUM)
218 ip_cmsg_recv_checksum(msg, skb, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
Tom Herbert5961de92015-01-05 13:56:16 -0800220EXPORT_SYMBOL(ip_cmsg_recv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100222int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc,
223 bool allow_ipv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Francesco Fuscof02db312013-09-24 15:43:08 +0200225 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 struct cmsghdr *cmsg;
227
Gu Zhengf95b4142014-12-11 11:22:04 +0800228 for_each_cmsghdr(cmsg, msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (!CMSG_OK(msg, cmsg))
230 return -EINVAL;
Eric Dumazet5337b5b2014-11-10 17:54:25 -0800231#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100232 if (allow_ipv6 &&
233 cmsg->cmsg_level == SOL_IPV6 &&
234 cmsg->cmsg_type == IPV6_PKTINFO) {
235 struct in6_pktinfo *src_info;
236
237 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
238 return -EINVAL;
239 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
240 if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
241 return -EINVAL;
242 ipc->oif = src_info->ipi6_ifindex;
243 ipc->addr = src_info->ipi6_addr.s6_addr32[3];
244 continue;
245 }
246#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (cmsg->cmsg_level != SOL_IP)
248 continue;
249 switch (cmsg->cmsg_type) {
250 case IP_RETOPTS:
251 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
Eric Dumazet91948302016-02-04 06:23:28 -0800252
253 /* Our caller is responsible for freeing ipc->opt */
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700254 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
255 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (err)
257 return err;
258 break;
259 case IP_PKTINFO:
260 {
261 struct in_pktinfo *info;
262 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
263 return -EINVAL;
264 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
265 ipc->oif = info->ipi_ifindex;
266 ipc->addr = info->ipi_spec_dst.s_addr;
267 break;
268 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200269 case IP_TTL:
270 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
271 return -EINVAL;
272 val = *(int *)CMSG_DATA(cmsg);
273 if (val < 1 || val > 255)
274 return -EINVAL;
275 ipc->ttl = val;
276 break;
277 case IP_TOS:
278 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
279 return -EINVAL;
280 val = *(int *)CMSG_DATA(cmsg);
281 if (val < 0 || val > 255)
282 return -EINVAL;
283 ipc->tos = val;
284 ipc->priority = rt_tos2priority(ipc->tos);
285 break;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 default:
288 return -EINVAL;
289 }
290 }
291 return 0;
292}
293
294
295/* Special input handler for packets caught by router alert option.
296 They are selected only by protocol field, and then processed likely
297 local ones; but only if someone wants them! Otherwise, router
298 not running rsvpd will kill RSVP.
299
300 It is user level problem, what it will make with them.
301 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
302 but receiver should be enough clever f.e. to forward mtrace requests,
303 sent to multicast group to reach destination designated router.
304 */
Eric Dumazet43a951e2010-10-25 03:32:44 +0000305struct ip_ra_chain __rcu *ip_ra_chain;
Eric Dumazet66018502010-06-07 03:12:08 +0000306static DEFINE_SPINLOCK(ip_ra_lock);
307
Eric Dumazet592fcb92010-06-09 16:21:07 +0000308
309static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000310{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000311 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
312
313 sock_put(ra->saved_sk);
314 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000315}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700317int ip_ra_control(struct sock *sk, unsigned char on,
318 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000320 struct ip_ra_chain *ra, *new_ra;
321 struct ip_ra_chain __rcu **rap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000323 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return -EINVAL;
325
326 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
327
Eric Dumazet66018502010-06-07 03:12:08 +0000328 spin_lock_bh(&ip_ra_lock);
Eric Dumazet43a951e2010-10-25 03:32:44 +0000329 for (rap = &ip_ra_chain;
330 (ra = rcu_dereference_protected(*rap,
331 lockdep_is_held(&ip_ra_lock))) != NULL;
332 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (ra->sk == sk) {
334 if (on) {
Eric Dumazet66018502010-06-07 03:12:08 +0000335 spin_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800336 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return -EADDRINUSE;
338 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000339 /* dont let ip_call_ra_chain() use sk again */
340 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700341 RCU_INIT_POINTER(*rap, ra->next);
Eric Dumazet66018502010-06-07 03:12:08 +0000342 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (ra->destructor)
345 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000346 /*
347 * Delay sock_put(sk) and kfree(ra) after one rcu grace
348 * period. This guarantee ip_call_ra_chain() dont need
349 * to mess with socket refcounts.
350 */
351 ra->saved_sk = sk;
352 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354 }
355 }
Ian Morris51456b22015-04-03 09:17:26 +0100356 if (!new_ra) {
Eric Dumazet66018502010-06-07 03:12:08 +0000357 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return -ENOBUFS;
359 }
360 new_ra->sk = sk;
361 new_ra->destructor = destructor;
362
Eric Dumazet8e380f02014-09-09 08:11:41 -0700363 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000364 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 sock_hold(sk);
Eric Dumazet66018502010-06-07 03:12:08 +0000366 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 return 0;
369}
370
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900371void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700372 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct sock_exterr_skb *serr;
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 skb = skb_clone(skb, GFP_ATOMIC);
377 if (!skb)
378 return;
379
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900380 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 serr->ee.ee_errno = err;
382 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300383 serr->ee.ee_type = icmp_hdr(skb)->type;
384 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 serr->ee.ee_pad = 0;
386 serr->ee.ee_info = info;
387 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300388 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700389 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 serr->port = port;
391
Ian Morris00db4122015-04-03 09:17:27 +0100392 if (skb_pull(skb, payload - skb->data)) {
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300393 skb_reset_transport_header(skb);
394 if (sock_queue_err_skb(sk, skb) == 0)
395 return;
396 }
397 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Al Viro05790162006-09-27 18:33:40 -0700400void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 struct inet_sock *inet = inet_sk(sk);
403 struct sock_exterr_skb *serr;
404 struct iphdr *iph;
405 struct sk_buff *skb;
406
407 if (!inet->recverr)
408 return;
409
410 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
411 if (!skb)
412 return;
413
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300414 skb_put(skb, sizeof(struct iphdr));
415 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700416 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 iph->daddr = daddr;
418
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900419 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 serr->ee.ee_errno = err;
421 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900422 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 serr->ee.ee_code = 0;
424 serr->ee.ee_pad = 0;
425 serr->ee.ee_info = info;
426 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700427 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 serr->port = port;
429
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700430 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300431 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (sock_queue_err_skb(sk, skb))
434 kfree_skb(skb);
435}
436
Julian Anastasov34b99df2015-06-23 08:34:39 +0300437/* For some errors we have valid addr_offset even with zero payload and
438 * zero port. Also, addr_offset should be supported if port is set.
439 */
440static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
441{
442 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
443 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
444}
445
Willem de Bruijnc247f052015-03-07 20:33:22 -0500446/* IPv4 supports cmsg on all imcp errors and some timestamps
447 *
448 * Timestamp code paths do not initialize the fields expected by cmsg:
449 * the PKTINFO fields in skb->cb[]. Fill those in here.
450 */
451static bool ipv4_datagram_support_cmsg(const struct sock *sk,
452 struct sk_buff *skb,
453 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500454{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500455 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500456
Willem de Bruijnc247f052015-03-07 20:33:22 -0500457 if (ee_origin == SO_EE_ORIGIN_ICMP)
458 return true;
459
460 if (ee_origin == SO_EE_ORIGIN_LOCAL)
461 return false;
462
463 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
464 * timestamp with egress dev. Not possible for packets without dev
465 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
466 */
467 if ((!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG)) ||
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500468 (!skb->dev))
469 return false;
470
Willem de Bruijnc247f052015-03-07 20:33:22 -0500471 info = PKTINFO_SKB_CB(skb);
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500472 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
473 info->ipi_ifindex = skb->dev->ifindex;
474 return true;
475}
476
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900477/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * Handle MSG_ERRQUEUE
479 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100480int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400483 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100484 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 struct {
486 struct sock_extended_err ee;
487 struct sockaddr_in offender;
488 } errhdr;
489 int err;
490 int copied;
491
Willem de Bruijn7ce875e2014-11-30 22:22:33 -0500492 WARN_ON_ONCE(sk->sk_family == AF_INET6);
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400495 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100496 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 goto out;
498
499 copied = skb->len;
500 if (copied > len) {
501 msg->msg_flags |= MSG_TRUNC;
502 copied = len;
503 }
David S. Miller51f3d022014-11-05 16:46:40 -0500504 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (err)
506 goto out_free_skb;
507
508 sock_recv_timestamp(msg, sk, skb);
509
510 serr = SKB_EXT_ERR(skb);
511
Julian Anastasov34b99df2015-06-23 08:34:39 +0300512 if (sin && ipv4_datagram_support_addr(serr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700514 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
515 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 sin->sin_port = serr->port;
517 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100518 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
521 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
522 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500523 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500524
Willem de Bruijnc247f052015-03-07 20:33:22 -0500525 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700527 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500528 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 ip_cmsg_recv(msg, skb);
530 }
531
532 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
533
534 /* Now we could try to dump offended packet options */
535
536 msg->msg_flags |= MSG_ERRQUEUE;
537 err = copied;
538
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900539out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 kfree_skb(skb);
541out:
542 return err;
543}
544
545
546/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700547 * Socket option code for IP. This is the end of the line after any
548 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300550static bool setsockopt_needs_rtnl(int optname)
551{
552 switch (optname) {
553 case IP_ADD_MEMBERSHIP:
554 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300555 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300556 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300557 case IP_DROP_SOURCE_MEMBERSHIP:
558 case IP_MSFILTER:
559 case IP_UNBLOCK_SOURCE:
560 case MCAST_BLOCK_SOURCE:
561 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300562 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300563 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300564 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300565 case MCAST_LEAVE_SOURCE_GROUP:
566 case MCAST_UNBLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300567 return true;
568 }
569 return false;
570}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800572static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700573 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 struct inet_sock *inet = inet_sk(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800576 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300577 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Xi Wang0c9f79b2012-11-11 11:20:01 +0000579 switch (optname) {
580 case IP_PKTINFO:
581 case IP_RECVTTL:
582 case IP_RECVOPTS:
583 case IP_RECVTOS:
584 case IP_RETOPTS:
585 case IP_TOS:
586 case IP_TTL:
587 case IP_HDRINCL:
588 case IP_MTU_DISCOVER:
589 case IP_RECVERR:
590 case IP_ROUTER_ALERT:
591 case IP_FREEBIND:
592 case IP_PASSSEC:
593 case IP_TRANSPARENT:
594 case IP_MINTTL:
595 case IP_NODEFRAG:
Eric Dumazet90c337d2015-06-06 21:17:57 -0700596 case IP_BIND_ADDRESS_NO_PORT:
Xi Wang0c9f79b2012-11-11 11:20:01 +0000597 case IP_UNICAST_IF:
598 case IP_MULTICAST_TTL:
599 case IP_MULTICAST_ALL:
600 case IP_MULTICAST_LOOP:
601 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800602 case IP_CHECKSUM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (optlen >= sizeof(int)) {
604 if (get_user(val, (int __user *) optval))
605 return -EFAULT;
606 } else if (optlen >= sizeof(char)) {
607 unsigned char ucval;
608
609 if (get_user(ucval, (unsigned char __user *) optval))
610 return -EFAULT;
611 val = (int) ucval;
612 }
613 }
614
615 /* If optlen==0, it is equivalent to val == 0 */
616
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800617 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800618 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300621 if (needs_rtnl)
622 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 lock_sock(sk);
624
625 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800626 case IP_OPTIONS:
627 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000628 struct ip_options_rcu *old, *opt = NULL;
629
roel kluin65a1c4f2009-10-23 05:59:21 +0000630 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800631 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900632 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700633 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800634 if (err)
635 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000636 old = rcu_dereference_protected(inet->inet_opt,
637 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800638 if (inet->is_icsk) {
639 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000640#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800641 if (sk->sk_family == PF_INET ||
642 (!((1 << sk->sk_state) &
643 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000644 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000646 if (old)
647 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800648 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000649 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800650 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000651#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000655 rcu_assign_pointer(inet->inet_opt, opt);
656 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800657 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800658 break;
659 }
660 case IP_PKTINFO:
661 if (val)
662 inet->cmsg_flags |= IP_CMSG_PKTINFO;
663 else
664 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
665 break;
666 case IP_RECVTTL:
667 if (val)
668 inet->cmsg_flags |= IP_CMSG_TTL;
669 else
670 inet->cmsg_flags &= ~IP_CMSG_TTL;
671 break;
672 case IP_RECVTOS:
673 if (val)
674 inet->cmsg_flags |= IP_CMSG_TOS;
675 else
676 inet->cmsg_flags &= ~IP_CMSG_TOS;
677 break;
678 case IP_RECVOPTS:
679 if (val)
680 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
681 else
682 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
683 break;
684 case IP_RETOPTS:
685 if (val)
686 inet->cmsg_flags |= IP_CMSG_RETOPTS;
687 else
688 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
689 break;
690 case IP_PASSSEC:
691 if (val)
692 inet->cmsg_flags |= IP_CMSG_PASSSEC;
693 else
694 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
695 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800696 case IP_RECVORIGDSTADDR:
697 if (val)
698 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
699 else
700 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
701 break;
Tom Herbertad6f9392015-01-05 13:56:17 -0800702 case IP_CHECKSUM:
703 if (val) {
704 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
705 inet_inc_convert_csum(sk);
706 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
707 }
708 } else {
709 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
710 inet_dec_convert_csum(sk);
711 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
712 }
713 }
714 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800715 case IP_TOS: /* This sets both TOS and Precedence */
716 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400717 val &= ~INET_ECN_MASK;
718 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800720 if (inet->tos != val) {
721 inet->tos = val;
722 sk->sk_priority = rt_tos2priority(val);
723 sk_dst_reset(sk);
724 }
725 break;
726 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700727 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800728 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000729 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800730 goto e_inval;
731 inet->uc_ttl = val;
732 break;
733 case IP_HDRINCL:
734 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800737 }
738 inet->hdrincl = val ? 1 : 0;
739 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000740 case IP_NODEFRAG:
741 if (sk->sk_type != SOCK_RAW) {
742 err = -ENOPROTOOPT;
743 break;
744 }
745 inet->nodefrag = val ? 1 : 0;
746 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -0700747 case IP_BIND_ADDRESS_NO_PORT:
748 inet->bind_address_no_port = val ? 1 : 0;
749 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800750 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100751 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800752 goto e_inval;
753 inet->pmtudisc = val;
754 break;
755 case IP_RECVERR:
756 inet->recverr = !!val;
757 if (!val)
758 skb_queue_purge(&sk->sk_error_queue);
759 break;
760 case IP_MULTICAST_TTL:
761 if (sk->sk_type == SOCK_STREAM)
762 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700763 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800764 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800765 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800766 val = 1;
767 if (val < 0 || val > 255)
768 goto e_inval;
769 inet->mc_ttl = val;
770 break;
771 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700772 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800773 goto e_inval;
774 inet->mc_loop = !!val;
775 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000776 case IP_UNICAST_IF:
777 {
778 struct net_device *dev = NULL;
779 int ifindex;
780
781 if (optlen != sizeof(int))
782 goto e_inval;
783
784 ifindex = (__force int)ntohl((__force __be32)val);
785 if (ifindex == 0) {
786 inet->uc_index = 0;
787 err = 0;
788 break;
789 }
790
791 dev = dev_get_by_index(sock_net(sk), ifindex);
792 err = -EADDRNOTAVAIL;
793 if (!dev)
794 break;
795 dev_put(dev);
796
797 err = -EINVAL;
798 if (sk->sk_bound_dev_if)
799 break;
800
801 inet->uc_index = ifindex;
802 err = 0;
803 break;
804 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800805 case IP_MULTICAST_IF:
806 {
807 struct ip_mreqn mreq;
808 struct net_device *dev = NULL;
809
810 if (sk->sk_type == SOCK_STREAM)
811 goto e_inval;
812 /*
813 * Check the arguments are allowable
814 */
815
Shan Wei09159212009-09-22 15:41:10 +0000816 if (optlen < sizeof(struct in_addr))
817 goto e_inval;
818
Stephen Hemminger132adf52007-03-08 20:44:43 -0800819 err = -EFAULT;
820 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800821 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800822 break;
823 } else {
824 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000825 if (optlen >= sizeof(struct ip_mreq)) {
826 if (copy_from_user(&mreq, optval,
827 sizeof(struct ip_mreq)))
828 break;
829 } else if (optlen >= sizeof(struct in_addr)) {
830 if (copy_from_user(&mreq.imr_address, optval,
831 sizeof(struct in_addr)))
832 break;
833 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800834 }
835
836 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700837 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800838 inet->mc_index = 0;
839 inet->mc_addr = 0;
840 err = 0;
841 break;
842 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900843 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000844 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800845 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800846 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000847 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800848
849
850 err = -EADDRNOTAVAIL;
851 if (!dev)
852 break;
Eric Dumazet55b80502009-10-19 06:41:58 +0000853 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800854
855 err = -EINVAL;
856 if (sk->sk_bound_dev_if &&
857 mreq.imr_ifindex != sk->sk_bound_dev_if)
858 break;
859
860 inet->mc_index = mreq.imr_ifindex;
861 inet->mc_addr = mreq.imr_address.s_addr;
862 err = 0;
863 break;
864 }
865
866 case IP_ADD_MEMBERSHIP:
867 case IP_DROP_MEMBERSHIP:
868 {
869 struct ip_mreqn mreq;
870
Flavio Leitnera96fb492007-08-24 22:16:39 -0700871 err = -EPROTO;
872 if (inet_sk(sk)->is_icsk)
873 break;
874
Stephen Hemminger132adf52007-03-08 20:44:43 -0800875 if (optlen < sizeof(struct ip_mreq))
876 goto e_inval;
877 err = -EFAULT;
878 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800879 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800880 break;
881 } else {
882 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800883 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800884 break;
885 }
886
887 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300888 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800889 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300890 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800891 break;
892 }
893 case IP_MSFILTER:
894 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800895 struct ip_msfilter *msf;
896
897 if (optlen < IP_MSFILTER_SIZE(0))
898 goto e_inval;
899 if (optlen > sysctl_optmem_max) {
900 err = -ENOBUFS;
901 break;
902 }
903 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700904 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800905 err = -ENOBUFS;
906 break;
907 }
908 err = -EFAULT;
909 if (copy_from_user(msf, optval, optlen)) {
910 kfree(msf);
911 break;
912 }
913 /* numsrc >= (1G-4) overflow in 32 bits */
914 if (msf->imsf_numsrc >= 0x3ffffffcU ||
915 msf->imsf_numsrc > sysctl_igmp_max_msf) {
916 kfree(msf);
917 err = -ENOBUFS;
918 break;
919 }
920 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
921 kfree(msf);
922 err = -EINVAL;
923 break;
924 }
925 err = ip_mc_msfilter(sk, msf, 0);
926 kfree(msf);
927 break;
928 }
929 case IP_BLOCK_SOURCE:
930 case IP_UNBLOCK_SOURCE:
931 case IP_ADD_SOURCE_MEMBERSHIP:
932 case IP_DROP_SOURCE_MEMBERSHIP:
933 {
934 struct ip_mreq_source mreqs;
935 int omode, add;
936
937 if (optlen != sizeof(struct ip_mreq_source))
938 goto e_inval;
939 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
940 err = -EFAULT;
941 break;
942 }
943 if (optname == IP_BLOCK_SOURCE) {
944 omode = MCAST_EXCLUDE;
945 add = 1;
946 } else if (optname == IP_UNBLOCK_SOURCE) {
947 omode = MCAST_EXCLUDE;
948 add = 0;
949 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
950 struct ip_mreqn mreq;
951
952 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
953 mreq.imr_address.s_addr = mreqs.imr_interface;
954 mreq.imr_ifindex = 0;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300955 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800956 if (err && err != -EADDRINUSE)
957 break;
958 omode = MCAST_INCLUDE;
959 add = 1;
960 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
961 omode = MCAST_INCLUDE;
962 add = 0;
963 }
964 err = ip_mc_source(add, omode, sk, &mreqs, 0);
965 break;
966 }
967 case MCAST_JOIN_GROUP:
968 case MCAST_LEAVE_GROUP:
969 {
970 struct group_req greq;
971 struct sockaddr_in *psin;
972 struct ip_mreqn mreq;
973
974 if (optlen < sizeof(struct group_req))
975 goto e_inval;
976 err = -EFAULT;
977 if (copy_from_user(&greq, optval, sizeof(greq)))
978 break;
979 psin = (struct sockaddr_in *)&greq.gr_group;
980 if (psin->sin_family != AF_INET)
981 goto e_inval;
982 memset(&mreq, 0, sizeof(mreq));
983 mreq.imr_multiaddr = psin->sin_addr;
984 mreq.imr_ifindex = greq.gr_interface;
985
986 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300987 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800988 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300989 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800990 break;
991 }
992 case MCAST_JOIN_SOURCE_GROUP:
993 case MCAST_LEAVE_SOURCE_GROUP:
994 case MCAST_BLOCK_SOURCE:
995 case MCAST_UNBLOCK_SOURCE:
996 {
997 struct group_source_req greqs;
998 struct ip_mreq_source mreqs;
999 struct sockaddr_in *psin;
1000 int omode, add;
1001
1002 if (optlen != sizeof(struct group_source_req))
1003 goto e_inval;
1004 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
1005 err = -EFAULT;
1006 break;
1007 }
1008 if (greqs.gsr_group.ss_family != AF_INET ||
1009 greqs.gsr_source.ss_family != AF_INET) {
1010 err = -EADDRNOTAVAIL;
1011 break;
1012 }
1013 psin = (struct sockaddr_in *)&greqs.gsr_group;
1014 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
1015 psin = (struct sockaddr_in *)&greqs.gsr_source;
1016 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
1017 mreqs.imr_interface = 0; /* use index for mc_source */
1018
1019 if (optname == MCAST_BLOCK_SOURCE) {
1020 omode = MCAST_EXCLUDE;
1021 add = 1;
1022 } else if (optname == MCAST_UNBLOCK_SOURCE) {
1023 omode = MCAST_EXCLUDE;
1024 add = 0;
1025 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
1026 struct ip_mreqn mreq;
1027
1028 psin = (struct sockaddr_in *)&greqs.gsr_group;
1029 mreq.imr_multiaddr = psin->sin_addr;
1030 mreq.imr_address.s_addr = 0;
1031 mreq.imr_ifindex = greqs.gsr_interface;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001032 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001033 if (err && err != -EADDRINUSE)
1034 break;
1035 greqs.gsr_interface = mreq.imr_ifindex;
1036 omode = MCAST_INCLUDE;
1037 add = 1;
1038 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1039 omode = MCAST_INCLUDE;
1040 add = 0;
1041 }
1042 err = ip_mc_source(add, omode, sk, &mreqs,
1043 greqs.gsr_interface);
1044 break;
1045 }
1046 case MCAST_MSFILTER:
1047 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001048 struct sockaddr_in *psin;
1049 struct ip_msfilter *msf = NULL;
1050 struct group_filter *gsf = NULL;
1051 int msize, i, ifindex;
1052
1053 if (optlen < GROUP_FILTER_SIZE(0))
1054 goto e_inval;
1055 if (optlen > sysctl_optmem_max) {
1056 err = -ENOBUFS;
1057 break;
1058 }
Jianjun Kong09cb1052008-11-03 00:27:11 -08001059 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001060 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001061 err = -ENOBUFS;
1062 break;
1063 }
1064 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001065 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001066 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001067
Stephen Hemminger132adf52007-03-08 20:44:43 -08001068 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1069 if (gsf->gf_numsrc >= 0x1ffffff ||
1070 gsf->gf_numsrc > sysctl_igmp_max_msf) {
1071 err = -ENOBUFS;
1072 goto mc_msf_out;
1073 }
1074 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
1075 err = -EINVAL;
1076 goto mc_msf_out;
1077 }
1078 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -08001079 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001080 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001081 err = -ENOBUFS;
1082 goto mc_msf_out;
1083 }
1084 ifindex = gsf->gf_interface;
1085 psin = (struct sockaddr_in *)&gsf->gf_group;
1086 if (psin->sin_family != AF_INET) {
1087 err = -EADDRNOTAVAIL;
1088 goto mc_msf_out;
1089 }
1090 msf->imsf_multiaddr = psin->sin_addr.s_addr;
1091 msf->imsf_interface = 0;
1092 msf->imsf_fmode = gsf->gf_fmode;
1093 msf->imsf_numsrc = gsf->gf_numsrc;
1094 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001095 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001096 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
1097
1098 if (psin->sin_family != AF_INET)
1099 goto mc_msf_out;
1100 msf->imsf_slist[i] = psin->sin_addr.s_addr;
1101 }
1102 kfree(gsf);
1103 gsf = NULL;
1104
1105 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001106mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -08001107 kfree(msf);
1108 kfree(gsf);
1109 break;
1110 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001111 case IP_MULTICAST_ALL:
1112 if (optlen < 1)
1113 goto e_inval;
1114 if (val != 0 && val != 1)
1115 goto e_inval;
1116 inet->mc_all = val;
1117 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001118 case IP_ROUTER_ALERT:
1119 err = ip_ra_control(sk, val ? 1 : 0, NULL);
1120 break;
1121
1122 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001123 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001124 goto e_inval;
1125 inet->freebind = !!val;
1126 break;
1127
1128 case IP_IPSEC_POLICY:
1129 case IP_XFRM_POLICY:
1130 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001131 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001132 break;
1133 err = xfrm_user_policy(sk, optname, optval, optlen);
1134 break;
1135
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001136 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001137 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1138 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001139 err = -EPERM;
1140 break;
1141 }
1142 if (optlen < 1)
1143 goto e_inval;
1144 inet->transparent = !!val;
1145 break;
1146
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001147 case IP_MINTTL:
1148 if (optlen < 1)
1149 goto e_inval;
1150 if (val < 0 || val > 255)
1151 goto e_inval;
1152 inet->min_ttl = val;
1153 break;
1154
Stephen Hemminger132adf52007-03-08 20:44:43 -08001155 default:
1156 err = -ENOPROTOOPT;
1157 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001160 if (needs_rtnl)
1161 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return err;
1163
1164e_inval:
1165 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001166 if (needs_rtnl)
1167 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return -EINVAL;
1169}
1170
Eric Dumazetf84af322010-04-28 15:31:51 -07001171/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001172 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001173 * @sk: socket
1174 * @skb: buffer
1175 *
David S. Miller35ebf652012-06-28 03:59:11 -07001176 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1177 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001178 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001179 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001180void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001181{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001182 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +01001183 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1184 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001185
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +01001186 if (prepare && skb_rtable(skb)) {
David S. Miller92101b32012-07-23 16:29:00 -07001187 pktinfo->ipi_ifindex = inet_iif(skb);
David S. Miller35ebf652012-06-28 03:59:11 -07001188 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001189 } else {
1190 pktinfo->ipi_ifindex = 0;
1191 pktinfo->ipi_spec_dst.s_addr = 0;
1192 }
1193 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001194}
Eric Dumazetf84af322010-04-28 15:31:51 -07001195
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001196int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001197 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001198{
1199 int err;
1200
1201 if (level != SOL_IP)
1202 return -ENOPROTOOPT;
1203
1204 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1205#ifdef CONFIG_NETFILTER
1206 /* we need to exclude all possible ENOPROTOOPTs except default case */
1207 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001208 optname != IP_IPSEC_POLICY &&
1209 optname != IP_XFRM_POLICY &&
1210 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001211 lock_sock(sk);
1212 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1213 release_sock(sk);
1214 }
1215#endif
1216 return err;
1217}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001218EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001219
1220#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001221int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001222 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001223{
1224 int err;
1225
1226 if (level != SOL_IP)
1227 return -ENOPROTOOPT;
1228
David L Stevensdae50292008-04-27 01:06:07 -07001229 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1230 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1231 ip_setsockopt);
1232
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001233 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1234#ifdef CONFIG_NETFILTER
1235 /* we need to exclude all possible ENOPROTOOPTs except default case */
1236 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001237 optname != IP_IPSEC_POLICY &&
1238 optname != IP_XFRM_POLICY &&
1239 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001240 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001241 err = compat_nf_setsockopt(sk, PF_INET, optname,
1242 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001243 release_sock(sk);
1244 }
1245#endif
1246 return err;
1247}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001248EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001249#endif
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001252 * Get the options. Note for future reference. The GET of IP options gets
1253 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 */
1255
WANG Cong87e9f032015-11-03 15:41:16 -08001256static bool getsockopt_needs_rtnl(int optname)
1257{
1258 switch (optname) {
1259 case IP_MSFILTER:
1260 case MCAST_MSFILTER:
1261 return true;
1262 }
1263 return false;
1264}
1265
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001266static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001267 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 struct inet_sock *inet = inet_sk(sk);
WANG Cong87e9f032015-11-03 15:41:16 -08001270 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1271 int val, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001273
Stephen Hemminger132adf52007-03-08 20:44:43 -08001274 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return -EOPNOTSUPP;
1276
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001277 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001278 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Jianjun Kong09cb1052008-11-03 00:27:11 -08001280 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001282 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001284
WANG Cong87e9f032015-11-03 15:41:16 -08001285 if (needs_rtnl)
1286 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 lock_sock(sk);
1288
Stephen Hemminger132adf52007-03-08 20:44:43 -08001289 switch (optname) {
1290 case IP_OPTIONS:
1291 {
1292 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001293 struct ip_options *opt = (struct ip_options *)optbuf;
1294 struct ip_options_rcu *inet_opt;
1295
1296 inet_opt = rcu_dereference_protected(inet->inet_opt,
1297 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001298 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001299 if (inet_opt)
1300 memcpy(optbuf, &inet_opt->opt,
1301 sizeof(struct ip_options) +
1302 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001303 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Stephen Hemminger132adf52007-03-08 20:44:43 -08001305 if (opt->optlen == 0)
1306 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Stephen Hemminger132adf52007-03-08 20:44:43 -08001308 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Stephen Hemminger132adf52007-03-08 20:44:43 -08001310 len = min_t(unsigned int, len, opt->optlen);
1311 if (put_user(len, optlen))
1312 return -EFAULT;
1313 if (copy_to_user(optval, opt->__data, len))
1314 return -EFAULT;
1315 return 0;
1316 }
1317 case IP_PKTINFO:
1318 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1319 break;
1320 case IP_RECVTTL:
1321 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1322 break;
1323 case IP_RECVTOS:
1324 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1325 break;
1326 case IP_RECVOPTS:
1327 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1328 break;
1329 case IP_RETOPTS:
1330 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1331 break;
1332 case IP_PASSSEC:
1333 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1334 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001335 case IP_RECVORIGDSTADDR:
1336 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1337 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001338 case IP_CHECKSUM:
1339 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1340 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001341 case IP_TOS:
1342 val = inet->tos;
1343 break;
1344 case IP_TTL:
1345 val = (inet->uc_ttl == -1 ?
1346 sysctl_ip_default_ttl :
1347 inet->uc_ttl);
1348 break;
1349 case IP_HDRINCL:
1350 val = inet->hdrincl;
1351 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001352 case IP_NODEFRAG:
1353 val = inet->nodefrag;
1354 break;
Eric Dumazet90c337d2015-06-06 21:17:57 -07001355 case IP_BIND_ADDRESS_NO_PORT:
1356 val = inet->bind_address_no_port;
1357 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001358 case IP_MTU_DISCOVER:
1359 val = inet->pmtudisc;
1360 break;
1361 case IP_MTU:
1362 {
1363 struct dst_entry *dst;
1364 val = 0;
1365 dst = sk_dst_get(sk);
1366 if (dst) {
1367 val = dst_mtu(dst);
1368 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001370 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001372 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001374 break;
1375 }
1376 case IP_RECVERR:
1377 val = inet->recverr;
1378 break;
1379 case IP_MULTICAST_TTL:
1380 val = inet->mc_ttl;
1381 break;
1382 case IP_MULTICAST_LOOP:
1383 val = inet->mc_loop;
1384 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001385 case IP_UNICAST_IF:
1386 val = (__force int)htonl((__u32) inet->uc_index);
1387 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001388 case IP_MULTICAST_IF:
1389 {
1390 struct in_addr addr;
1391 len = min_t(unsigned int, len, sizeof(struct in_addr));
1392 addr.s_addr = inet->mc_addr;
1393 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Stephen Hemminger132adf52007-03-08 20:44:43 -08001395 if (put_user(len, optlen))
1396 return -EFAULT;
1397 if (copy_to_user(optval, &addr, len))
1398 return -EFAULT;
1399 return 0;
1400 }
1401 case IP_MSFILTER:
1402 {
1403 struct ip_msfilter msf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001404
1405 if (len < IP_MSFILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001406 err = -EINVAL;
1407 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001409 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001410 err = -EFAULT;
1411 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001413 err = ip_mc_msfget(sk, &msf,
1414 (struct ip_msfilter __user *)optval, optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001415 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001416 }
1417 case MCAST_MSFILTER:
1418 {
1419 struct group_filter gsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Stephen Hemminger132adf52007-03-08 20:44:43 -08001421 if (len < GROUP_FILTER_SIZE(0)) {
WANG Cong87e9f032015-11-03 15:41:16 -08001422 err = -EINVAL;
1423 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001425 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
WANG Cong87e9f032015-11-03 15:41:16 -08001426 err = -EFAULT;
1427 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001428 }
1429 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001430 (struct group_filter __user *)optval,
1431 optlen);
WANG Cong87e9f032015-11-03 15:41:16 -08001432 goto out;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001433 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001434 case IP_MULTICAST_ALL:
1435 val = inet->mc_all;
1436 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001437 case IP_PKTOPTIONS:
1438 {
1439 struct msghdr msg;
1440
1441 release_sock(sk);
1442
1443 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001445
Karoly Kemenyc54a5e02014-07-27 12:29:07 +02001446 msg.msg_control = (__force void *) optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001447 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001448 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001449
1450 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1451 struct in_pktinfo info;
1452
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001453 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1454 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001455 info.ipi_ifindex = inet->mc_index;
1456 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1457 }
1458 if (inet->cmsg_flags & IP_CMSG_TTL) {
1459 int hlim = inet->mc_ttl;
1460 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1461 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001462 if (inet->cmsg_flags & IP_CMSG_TOS) {
1463 int tos = inet->rcv_tos;
1464 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1465 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001466 len -= msg.msg_controllen;
1467 return put_user(len, optlen);
1468 }
1469 case IP_FREEBIND:
1470 val = inet->freebind;
1471 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001472 case IP_TRANSPARENT:
1473 val = inet->transparent;
1474 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001475 case IP_MINTTL:
1476 val = inet->min_ttl;
1477 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001478 default:
1479 release_sock(sk);
1480 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001483
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001484 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 unsigned char ucval = (unsigned char)val;
1486 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001487 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001489 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return -EFAULT;
1491 } else {
1492 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001493 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001495 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return -EFAULT;
1497 }
1498 return 0;
WANG Cong87e9f032015-11-03 15:41:16 -08001499
1500out:
1501 release_sock(sk);
1502 if (needs_rtnl)
1503 rtnl_unlock();
1504 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001507int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001508 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001509{
1510 int err;
1511
Daniel Balutadd23198e2011-08-07 22:31:07 -07001512 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001513#ifdef CONFIG_NETFILTER
1514 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001515 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1516 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001517 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001518
Jianjun Kong09cb1052008-11-03 00:27:11 -08001519 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001520 return -EFAULT;
1521
1522 lock_sock(sk);
1523 err = nf_getsockopt(sk, PF_INET, optname, optval,
1524 &len);
1525 release_sock(sk);
1526 if (err >= 0)
1527 err = put_user(len, optlen);
1528 return err;
1529 }
1530#endif
1531 return err;
1532}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001533EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001534
1535#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001536int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1537 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001538{
David L Stevens42908c62008-04-29 03:23:22 -07001539 int err;
1540
1541 if (optname == MCAST_MSFILTER)
1542 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1543 ip_getsockopt);
1544
Daniel Balutadd23198e2011-08-07 22:31:07 -07001545 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1546 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001547
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001548#ifdef CONFIG_NETFILTER
1549 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001550 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1551 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001552 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001553
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001554 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001555 return -EFAULT;
1556
1557 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001558 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001559 release_sock(sk);
1560 if (err >= 0)
1561 err = put_user(len, optlen);
1562 return err;
1563 }
1564#endif
1565 return err;
1566}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001567EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001568#endif