blob: 7cfb0893f2636bcc87537da3014643362f72b10f [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 Dumazet4d52cfb2009-06-02 00:42:16 -0700252 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
253 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (err)
255 return err;
256 break;
257 case IP_PKTINFO:
258 {
259 struct in_pktinfo *info;
260 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
261 return -EINVAL;
262 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
263 ipc->oif = info->ipi_ifindex;
264 ipc->addr = info->ipi_spec_dst.s_addr;
265 break;
266 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200267 case IP_TTL:
268 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
269 return -EINVAL;
270 val = *(int *)CMSG_DATA(cmsg);
271 if (val < 1 || val > 255)
272 return -EINVAL;
273 ipc->ttl = val;
274 break;
275 case IP_TOS:
276 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
277 return -EINVAL;
278 val = *(int *)CMSG_DATA(cmsg);
279 if (val < 0 || val > 255)
280 return -EINVAL;
281 ipc->tos = val;
282 ipc->priority = rt_tos2priority(ipc->tos);
283 break;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 default:
286 return -EINVAL;
287 }
288 }
289 return 0;
290}
291
292
293/* Special input handler for packets caught by router alert option.
294 They are selected only by protocol field, and then processed likely
295 local ones; but only if someone wants them! Otherwise, router
296 not running rsvpd will kill RSVP.
297
298 It is user level problem, what it will make with them.
299 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
300 but receiver should be enough clever f.e. to forward mtrace requests,
301 sent to multicast group to reach destination designated router.
302 */
Eric Dumazet43a951e2010-10-25 03:32:44 +0000303struct ip_ra_chain __rcu *ip_ra_chain;
Eric Dumazet66018502010-06-07 03:12:08 +0000304static DEFINE_SPINLOCK(ip_ra_lock);
305
Eric Dumazet592fcb92010-06-09 16:21:07 +0000306
307static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000308{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000309 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
310
311 sock_put(ra->saved_sk);
312 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000313}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700315int ip_ra_control(struct sock *sk, unsigned char on,
316 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000318 struct ip_ra_chain *ra, *new_ra;
319 struct ip_ra_chain __rcu **rap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000321 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return -EINVAL;
323
324 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
325
Eric Dumazet66018502010-06-07 03:12:08 +0000326 spin_lock_bh(&ip_ra_lock);
Eric Dumazet43a951e2010-10-25 03:32:44 +0000327 for (rap = &ip_ra_chain;
328 (ra = rcu_dereference_protected(*rap,
329 lockdep_is_held(&ip_ra_lock))) != NULL;
330 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (ra->sk == sk) {
332 if (on) {
Eric Dumazet66018502010-06-07 03:12:08 +0000333 spin_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800334 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return -EADDRINUSE;
336 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000337 /* dont let ip_call_ra_chain() use sk again */
338 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700339 RCU_INIT_POINTER(*rap, ra->next);
Eric Dumazet66018502010-06-07 03:12:08 +0000340 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (ra->destructor)
343 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000344 /*
345 * Delay sock_put(sk) and kfree(ra) after one rcu grace
346 * period. This guarantee ip_call_ra_chain() dont need
347 * to mess with socket refcounts.
348 */
349 ra->saved_sk = sk;
350 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return 0;
352 }
353 }
Ian Morris51456b22015-04-03 09:17:26 +0100354 if (!new_ra) {
Eric Dumazet66018502010-06-07 03:12:08 +0000355 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return -ENOBUFS;
357 }
358 new_ra->sk = sk;
359 new_ra->destructor = destructor;
360
Eric Dumazet8e380f02014-09-09 08:11:41 -0700361 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000362 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 sock_hold(sk);
Eric Dumazet66018502010-06-07 03:12:08 +0000364 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 return 0;
367}
368
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900369void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700370 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct sock_exterr_skb *serr;
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 skb = skb_clone(skb, GFP_ATOMIC);
375 if (!skb)
376 return;
377
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900378 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 serr->ee.ee_errno = err;
380 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300381 serr->ee.ee_type = icmp_hdr(skb)->type;
382 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 serr->ee.ee_pad = 0;
384 serr->ee.ee_info = info;
385 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300386 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700387 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 serr->port = port;
389
Ian Morris00db4122015-04-03 09:17:27 +0100390 if (skb_pull(skb, payload - skb->data)) {
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300391 skb_reset_transport_header(skb);
392 if (sock_queue_err_skb(sk, skb) == 0)
393 return;
394 }
395 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Al Viro05790162006-09-27 18:33:40 -0700398void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 struct inet_sock *inet = inet_sk(sk);
401 struct sock_exterr_skb *serr;
402 struct iphdr *iph;
403 struct sk_buff *skb;
404
405 if (!inet->recverr)
406 return;
407
408 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
409 if (!skb)
410 return;
411
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300412 skb_put(skb, sizeof(struct iphdr));
413 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700414 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 iph->daddr = daddr;
416
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900417 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 serr->ee.ee_errno = err;
419 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900420 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 serr->ee.ee_code = 0;
422 serr->ee.ee_pad = 0;
423 serr->ee.ee_info = info;
424 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700425 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 serr->port = port;
427
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700428 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300429 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if (sock_queue_err_skb(sk, skb))
432 kfree_skb(skb);
433}
434
Willem de Bruijnc247f052015-03-07 20:33:22 -0500435/* IPv4 supports cmsg on all imcp errors and some timestamps
436 *
437 * Timestamp code paths do not initialize the fields expected by cmsg:
438 * the PKTINFO fields in skb->cb[]. Fill those in here.
439 */
440static bool ipv4_datagram_support_cmsg(const struct sock *sk,
441 struct sk_buff *skb,
442 int ee_origin)
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500443{
Willem de Bruijnc247f052015-03-07 20:33:22 -0500444 struct in_pktinfo *info;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500445
Willem de Bruijnc247f052015-03-07 20:33:22 -0500446 if (ee_origin == SO_EE_ORIGIN_ICMP)
447 return true;
448
449 if (ee_origin == SO_EE_ORIGIN_LOCAL)
450 return false;
451
452 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
453 * timestamp with egress dev. Not possible for packets without dev
454 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
455 */
456 if ((!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG)) ||
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500457 (!skb->dev))
458 return false;
459
Willem de Bruijnc247f052015-03-07 20:33:22 -0500460 info = PKTINFO_SKB_CB(skb);
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500461 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
462 info->ipi_ifindex = skb->dev->ifindex;
463 return true;
464}
465
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900466/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * Handle MSG_ERRQUEUE
468 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100469int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400472 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100473 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct {
475 struct sock_extended_err ee;
476 struct sockaddr_in offender;
477 } errhdr;
478 int err;
479 int copied;
480
Willem de Bruijn7ce875e2014-11-30 22:22:33 -0500481 WARN_ON_ONCE(sk->sk_family == AF_INET6);
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400484 skb = sock_dequeue_err_skb(sk);
Ian Morris51456b22015-04-03 09:17:26 +0100485 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 goto out;
487
488 copied = skb->len;
489 if (copied > len) {
490 msg->msg_flags |= MSG_TRUNC;
491 copied = len;
492 }
David S. Miller51f3d022014-11-05 16:46:40 -0500493 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (err)
495 goto out_free_skb;
496
497 sock_recv_timestamp(msg, sk, skb);
498
499 serr = SKB_EXT_ERR(skb);
500
Willem de Bruijnc247f052015-03-07 20:33:22 -0500501 if (sin && serr->port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700503 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
504 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 sin->sin_port = serr->port;
506 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100507 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
510 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
511 sin = &errhdr.offender;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500512 memset(sin, 0, sizeof(*sin));
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500513
Willem de Bruijnc247f052015-03-07 20:33:22 -0500514 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700516 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Willem de Bruijnf8121162015-01-15 13:18:40 -0500517 if (inet_sk(sk)->cmsg_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 ip_cmsg_recv(msg, skb);
519 }
520
521 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
522
523 /* Now we could try to dump offended packet options */
524
525 msg->msg_flags |= MSG_ERRQUEUE;
526 err = copied;
527
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900528out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 kfree_skb(skb);
530out:
531 return err;
532}
533
534
535/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700536 * Socket option code for IP. This is the end of the line after any
537 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 */
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300539static bool setsockopt_needs_rtnl(int optname)
540{
541 switch (optname) {
542 case IP_ADD_MEMBERSHIP:
543 case IP_ADD_SOURCE_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300544 case IP_BLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300545 case IP_DROP_MEMBERSHIP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300546 case IP_DROP_SOURCE_MEMBERSHIP:
547 case IP_MSFILTER:
548 case IP_UNBLOCK_SOURCE:
549 case MCAST_BLOCK_SOURCE:
550 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300551 case MCAST_JOIN_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300552 case MCAST_JOIN_SOURCE_GROUP:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300553 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300554 case MCAST_LEAVE_SOURCE_GROUP:
555 case MCAST_UNBLOCK_SOURCE:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300556 return true;
557 }
558 return false;
559}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800561static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700562 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct inet_sock *inet = inet_sk(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800565 int val = 0, err;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300566 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Xi Wang0c9f79b2012-11-11 11:20:01 +0000568 switch (optname) {
569 case IP_PKTINFO:
570 case IP_RECVTTL:
571 case IP_RECVOPTS:
572 case IP_RECVTOS:
573 case IP_RETOPTS:
574 case IP_TOS:
575 case IP_TTL:
576 case IP_HDRINCL:
577 case IP_MTU_DISCOVER:
578 case IP_RECVERR:
579 case IP_ROUTER_ALERT:
580 case IP_FREEBIND:
581 case IP_PASSSEC:
582 case IP_TRANSPARENT:
583 case IP_MINTTL:
584 case IP_NODEFRAG:
585 case IP_UNICAST_IF:
586 case IP_MULTICAST_TTL:
587 case IP_MULTICAST_ALL:
588 case IP_MULTICAST_LOOP:
589 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800590 case IP_CHECKSUM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (optlen >= sizeof(int)) {
592 if (get_user(val, (int __user *) optval))
593 return -EFAULT;
594 } else if (optlen >= sizeof(char)) {
595 unsigned char ucval;
596
597 if (get_user(ucval, (unsigned char __user *) optval))
598 return -EFAULT;
599 val = (int) ucval;
600 }
601 }
602
603 /* If optlen==0, it is equivalent to val == 0 */
604
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800605 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800606 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 err = 0;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300609 if (needs_rtnl)
610 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 lock_sock(sk);
612
613 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800614 case IP_OPTIONS:
615 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000616 struct ip_options_rcu *old, *opt = NULL;
617
roel kluin65a1c4f2009-10-23 05:59:21 +0000618 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800619 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900620 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700621 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800622 if (err)
623 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000624 old = rcu_dereference_protected(inet->inet_opt,
625 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800626 if (inet->is_icsk) {
627 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000628#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800629 if (sk->sk_family == PF_INET ||
630 (!((1 << sk->sk_state) &
631 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000632 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000634 if (old)
635 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800636 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000637 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800638 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000639#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000643 rcu_assign_pointer(inet->inet_opt, opt);
644 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800645 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800646 break;
647 }
648 case IP_PKTINFO:
649 if (val)
650 inet->cmsg_flags |= IP_CMSG_PKTINFO;
651 else
652 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
653 break;
654 case IP_RECVTTL:
655 if (val)
656 inet->cmsg_flags |= IP_CMSG_TTL;
657 else
658 inet->cmsg_flags &= ~IP_CMSG_TTL;
659 break;
660 case IP_RECVTOS:
661 if (val)
662 inet->cmsg_flags |= IP_CMSG_TOS;
663 else
664 inet->cmsg_flags &= ~IP_CMSG_TOS;
665 break;
666 case IP_RECVOPTS:
667 if (val)
668 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
669 else
670 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
671 break;
672 case IP_RETOPTS:
673 if (val)
674 inet->cmsg_flags |= IP_CMSG_RETOPTS;
675 else
676 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
677 break;
678 case IP_PASSSEC:
679 if (val)
680 inet->cmsg_flags |= IP_CMSG_PASSSEC;
681 else
682 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
683 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800684 case IP_RECVORIGDSTADDR:
685 if (val)
686 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
687 else
688 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
689 break;
Tom Herbertad6f9392015-01-05 13:56:17 -0800690 case IP_CHECKSUM:
691 if (val) {
692 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
693 inet_inc_convert_csum(sk);
694 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
695 }
696 } else {
697 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
698 inet_dec_convert_csum(sk);
699 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
700 }
701 }
702 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800703 case IP_TOS: /* This sets both TOS and Precedence */
704 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400705 val &= ~INET_ECN_MASK;
706 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800708 if (inet->tos != val) {
709 inet->tos = val;
710 sk->sk_priority = rt_tos2priority(val);
711 sk_dst_reset(sk);
712 }
713 break;
714 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700715 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800716 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000717 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800718 goto e_inval;
719 inet->uc_ttl = val;
720 break;
721 case IP_HDRINCL:
722 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800725 }
726 inet->hdrincl = val ? 1 : 0;
727 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000728 case IP_NODEFRAG:
729 if (sk->sk_type != SOCK_RAW) {
730 err = -ENOPROTOOPT;
731 break;
732 }
733 inet->nodefrag = val ? 1 : 0;
734 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800735 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100736 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800737 goto e_inval;
738 inet->pmtudisc = val;
739 break;
740 case IP_RECVERR:
741 inet->recverr = !!val;
742 if (!val)
743 skb_queue_purge(&sk->sk_error_queue);
744 break;
745 case IP_MULTICAST_TTL:
746 if (sk->sk_type == SOCK_STREAM)
747 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700748 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800749 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800750 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800751 val = 1;
752 if (val < 0 || val > 255)
753 goto e_inval;
754 inet->mc_ttl = val;
755 break;
756 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700757 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800758 goto e_inval;
759 inet->mc_loop = !!val;
760 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000761 case IP_UNICAST_IF:
762 {
763 struct net_device *dev = NULL;
764 int ifindex;
765
766 if (optlen != sizeof(int))
767 goto e_inval;
768
769 ifindex = (__force int)ntohl((__force __be32)val);
770 if (ifindex == 0) {
771 inet->uc_index = 0;
772 err = 0;
773 break;
774 }
775
776 dev = dev_get_by_index(sock_net(sk), ifindex);
777 err = -EADDRNOTAVAIL;
778 if (!dev)
779 break;
780 dev_put(dev);
781
782 err = -EINVAL;
783 if (sk->sk_bound_dev_if)
784 break;
785
786 inet->uc_index = ifindex;
787 err = 0;
788 break;
789 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800790 case IP_MULTICAST_IF:
791 {
792 struct ip_mreqn mreq;
793 struct net_device *dev = NULL;
794
795 if (sk->sk_type == SOCK_STREAM)
796 goto e_inval;
797 /*
798 * Check the arguments are allowable
799 */
800
Shan Wei09159212009-09-22 15:41:10 +0000801 if (optlen < sizeof(struct in_addr))
802 goto e_inval;
803
Stephen Hemminger132adf52007-03-08 20:44:43 -0800804 err = -EFAULT;
805 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800806 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800807 break;
808 } else {
809 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000810 if (optlen >= sizeof(struct ip_mreq)) {
811 if (copy_from_user(&mreq, optval,
812 sizeof(struct ip_mreq)))
813 break;
814 } else if (optlen >= sizeof(struct in_addr)) {
815 if (copy_from_user(&mreq.imr_address, optval,
816 sizeof(struct in_addr)))
817 break;
818 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800819 }
820
821 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700822 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800823 inet->mc_index = 0;
824 inet->mc_addr = 0;
825 err = 0;
826 break;
827 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900828 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000829 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800830 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800831 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000832 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800833
834
835 err = -EADDRNOTAVAIL;
836 if (!dev)
837 break;
Eric Dumazet55b80502009-10-19 06:41:58 +0000838 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800839
840 err = -EINVAL;
841 if (sk->sk_bound_dev_if &&
842 mreq.imr_ifindex != sk->sk_bound_dev_if)
843 break;
844
845 inet->mc_index = mreq.imr_ifindex;
846 inet->mc_addr = mreq.imr_address.s_addr;
847 err = 0;
848 break;
849 }
850
851 case IP_ADD_MEMBERSHIP:
852 case IP_DROP_MEMBERSHIP:
853 {
854 struct ip_mreqn mreq;
855
Flavio Leitnera96fb492007-08-24 22:16:39 -0700856 err = -EPROTO;
857 if (inet_sk(sk)->is_icsk)
858 break;
859
Stephen Hemminger132adf52007-03-08 20:44:43 -0800860 if (optlen < sizeof(struct ip_mreq))
861 goto e_inval;
862 err = -EFAULT;
863 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800864 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800865 break;
866 } else {
867 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800868 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800869 break;
870 }
871
872 if (optname == IP_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300873 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800874 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300875 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800876 break;
877 }
878 case IP_MSFILTER:
879 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800880 struct ip_msfilter *msf;
881
882 if (optlen < IP_MSFILTER_SIZE(0))
883 goto e_inval;
884 if (optlen > sysctl_optmem_max) {
885 err = -ENOBUFS;
886 break;
887 }
888 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700889 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800890 err = -ENOBUFS;
891 break;
892 }
893 err = -EFAULT;
894 if (copy_from_user(msf, optval, optlen)) {
895 kfree(msf);
896 break;
897 }
898 /* numsrc >= (1G-4) overflow in 32 bits */
899 if (msf->imsf_numsrc >= 0x3ffffffcU ||
900 msf->imsf_numsrc > sysctl_igmp_max_msf) {
901 kfree(msf);
902 err = -ENOBUFS;
903 break;
904 }
905 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
906 kfree(msf);
907 err = -EINVAL;
908 break;
909 }
910 err = ip_mc_msfilter(sk, msf, 0);
911 kfree(msf);
912 break;
913 }
914 case IP_BLOCK_SOURCE:
915 case IP_UNBLOCK_SOURCE:
916 case IP_ADD_SOURCE_MEMBERSHIP:
917 case IP_DROP_SOURCE_MEMBERSHIP:
918 {
919 struct ip_mreq_source mreqs;
920 int omode, add;
921
922 if (optlen != sizeof(struct ip_mreq_source))
923 goto e_inval;
924 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
925 err = -EFAULT;
926 break;
927 }
928 if (optname == IP_BLOCK_SOURCE) {
929 omode = MCAST_EXCLUDE;
930 add = 1;
931 } else if (optname == IP_UNBLOCK_SOURCE) {
932 omode = MCAST_EXCLUDE;
933 add = 0;
934 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
935 struct ip_mreqn mreq;
936
937 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
938 mreq.imr_address.s_addr = mreqs.imr_interface;
939 mreq.imr_ifindex = 0;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300940 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800941 if (err && err != -EADDRINUSE)
942 break;
943 omode = MCAST_INCLUDE;
944 add = 1;
945 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
946 omode = MCAST_INCLUDE;
947 add = 0;
948 }
949 err = ip_mc_source(add, omode, sk, &mreqs, 0);
950 break;
951 }
952 case MCAST_JOIN_GROUP:
953 case MCAST_LEAVE_GROUP:
954 {
955 struct group_req greq;
956 struct sockaddr_in *psin;
957 struct ip_mreqn mreq;
958
959 if (optlen < sizeof(struct group_req))
960 goto e_inval;
961 err = -EFAULT;
962 if (copy_from_user(&greq, optval, sizeof(greq)))
963 break;
964 psin = (struct sockaddr_in *)&greq.gr_group;
965 if (psin->sin_family != AF_INET)
966 goto e_inval;
967 memset(&mreq, 0, sizeof(mreq));
968 mreq.imr_multiaddr = psin->sin_addr;
969 mreq.imr_ifindex = greq.gr_interface;
970
971 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300972 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800973 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300974 err = ip_mc_leave_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800975 break;
976 }
977 case MCAST_JOIN_SOURCE_GROUP:
978 case MCAST_LEAVE_SOURCE_GROUP:
979 case MCAST_BLOCK_SOURCE:
980 case MCAST_UNBLOCK_SOURCE:
981 {
982 struct group_source_req greqs;
983 struct ip_mreq_source mreqs;
984 struct sockaddr_in *psin;
985 int omode, add;
986
987 if (optlen != sizeof(struct group_source_req))
988 goto e_inval;
989 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
990 err = -EFAULT;
991 break;
992 }
993 if (greqs.gsr_group.ss_family != AF_INET ||
994 greqs.gsr_source.ss_family != AF_INET) {
995 err = -EADDRNOTAVAIL;
996 break;
997 }
998 psin = (struct sockaddr_in *)&greqs.gsr_group;
999 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
1000 psin = (struct sockaddr_in *)&greqs.gsr_source;
1001 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
1002 mreqs.imr_interface = 0; /* use index for mc_source */
1003
1004 if (optname == MCAST_BLOCK_SOURCE) {
1005 omode = MCAST_EXCLUDE;
1006 add = 1;
1007 } else if (optname == MCAST_UNBLOCK_SOURCE) {
1008 omode = MCAST_EXCLUDE;
1009 add = 0;
1010 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
1011 struct ip_mreqn mreq;
1012
1013 psin = (struct sockaddr_in *)&greqs.gsr_group;
1014 mreq.imr_multiaddr = psin->sin_addr;
1015 mreq.imr_address.s_addr = 0;
1016 mreq.imr_ifindex = greqs.gsr_interface;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03001017 err = ip_mc_join_group(sk, &mreq);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001018 if (err && err != -EADDRINUSE)
1019 break;
1020 greqs.gsr_interface = mreq.imr_ifindex;
1021 omode = MCAST_INCLUDE;
1022 add = 1;
1023 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1024 omode = MCAST_INCLUDE;
1025 add = 0;
1026 }
1027 err = ip_mc_source(add, omode, sk, &mreqs,
1028 greqs.gsr_interface);
1029 break;
1030 }
1031 case MCAST_MSFILTER:
1032 {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001033 struct sockaddr_in *psin;
1034 struct ip_msfilter *msf = NULL;
1035 struct group_filter *gsf = NULL;
1036 int msize, i, ifindex;
1037
1038 if (optlen < GROUP_FILTER_SIZE(0))
1039 goto e_inval;
1040 if (optlen > sysctl_optmem_max) {
1041 err = -ENOBUFS;
1042 break;
1043 }
Jianjun Kong09cb1052008-11-03 00:27:11 -08001044 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001045 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001046 err = -ENOBUFS;
1047 break;
1048 }
1049 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001050 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001051 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001052
Stephen Hemminger132adf52007-03-08 20:44:43 -08001053 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1054 if (gsf->gf_numsrc >= 0x1ffffff ||
1055 gsf->gf_numsrc > sysctl_igmp_max_msf) {
1056 err = -ENOBUFS;
1057 goto mc_msf_out;
1058 }
1059 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
1060 err = -EINVAL;
1061 goto mc_msf_out;
1062 }
1063 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -08001064 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001065 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001066 err = -ENOBUFS;
1067 goto mc_msf_out;
1068 }
1069 ifindex = gsf->gf_interface;
1070 psin = (struct sockaddr_in *)&gsf->gf_group;
1071 if (psin->sin_family != AF_INET) {
1072 err = -EADDRNOTAVAIL;
1073 goto mc_msf_out;
1074 }
1075 msf->imsf_multiaddr = psin->sin_addr.s_addr;
1076 msf->imsf_interface = 0;
1077 msf->imsf_fmode = gsf->gf_fmode;
1078 msf->imsf_numsrc = gsf->gf_numsrc;
1079 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001080 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001081 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
1082
1083 if (psin->sin_family != AF_INET)
1084 goto mc_msf_out;
1085 msf->imsf_slist[i] = psin->sin_addr.s_addr;
1086 }
1087 kfree(gsf);
1088 gsf = NULL;
1089
1090 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001091mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -08001092 kfree(msf);
1093 kfree(gsf);
1094 break;
1095 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001096 case IP_MULTICAST_ALL:
1097 if (optlen < 1)
1098 goto e_inval;
1099 if (val != 0 && val != 1)
1100 goto e_inval;
1101 inet->mc_all = val;
1102 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001103 case IP_ROUTER_ALERT:
1104 err = ip_ra_control(sk, val ? 1 : 0, NULL);
1105 break;
1106
1107 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001108 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001109 goto e_inval;
1110 inet->freebind = !!val;
1111 break;
1112
1113 case IP_IPSEC_POLICY:
1114 case IP_XFRM_POLICY:
1115 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001116 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001117 break;
1118 err = xfrm_user_policy(sk, optname, optval, optlen);
1119 break;
1120
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001121 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001122 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1123 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001124 err = -EPERM;
1125 break;
1126 }
1127 if (optlen < 1)
1128 goto e_inval;
1129 inet->transparent = !!val;
1130 break;
1131
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001132 case IP_MINTTL:
1133 if (optlen < 1)
1134 goto e_inval;
1135 if (val < 0 || val > 255)
1136 goto e_inval;
1137 inet->min_ttl = val;
1138 break;
1139
Stephen Hemminger132adf52007-03-08 20:44:43 -08001140 default:
1141 err = -ENOPROTOOPT;
1142 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001145 if (needs_rtnl)
1146 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return err;
1148
1149e_inval:
1150 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001151 if (needs_rtnl)
1152 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return -EINVAL;
1154}
1155
Eric Dumazetf84af322010-04-28 15:31:51 -07001156/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001157 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001158 * @sk: socket
1159 * @skb: buffer
1160 *
David S. Miller35ebf652012-06-28 03:59:11 -07001161 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1162 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001163 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001164 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001165void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001166{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001167 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +01001168 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1169 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001170
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +01001171 if (prepare && skb_rtable(skb)) {
David S. Miller92101b32012-07-23 16:29:00 -07001172 pktinfo->ipi_ifindex = inet_iif(skb);
David S. Miller35ebf652012-06-28 03:59:11 -07001173 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001174 } else {
1175 pktinfo->ipi_ifindex = 0;
1176 pktinfo->ipi_spec_dst.s_addr = 0;
1177 }
1178 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001179}
Eric Dumazetf84af322010-04-28 15:31:51 -07001180
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001181int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001182 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001183{
1184 int err;
1185
1186 if (level != SOL_IP)
1187 return -ENOPROTOOPT;
1188
1189 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1190#ifdef CONFIG_NETFILTER
1191 /* we need to exclude all possible ENOPROTOOPTs except default case */
1192 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001193 optname != IP_IPSEC_POLICY &&
1194 optname != IP_XFRM_POLICY &&
1195 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001196 lock_sock(sk);
1197 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1198 release_sock(sk);
1199 }
1200#endif
1201 return err;
1202}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001203EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001204
1205#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001206int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001207 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001208{
1209 int err;
1210
1211 if (level != SOL_IP)
1212 return -ENOPROTOOPT;
1213
David L Stevensdae50292008-04-27 01:06:07 -07001214 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1215 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1216 ip_setsockopt);
1217
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001218 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1219#ifdef CONFIG_NETFILTER
1220 /* we need to exclude all possible ENOPROTOOPTs except default case */
1221 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001222 optname != IP_IPSEC_POLICY &&
1223 optname != IP_XFRM_POLICY &&
1224 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001225 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001226 err = compat_nf_setsockopt(sk, PF_INET, optname,
1227 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001228 release_sock(sk);
1229 }
1230#endif
1231 return err;
1232}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001233EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001234#endif
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001237 * Get the options. Note for future reference. The GET of IP options gets
1238 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 */
1240
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001241static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001242 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct inet_sock *inet = inet_sk(sk);
1245 int val;
1246 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001247
Stephen Hemminger132adf52007-03-08 20:44:43 -08001248 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return -EOPNOTSUPP;
1250
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001251 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001252 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Jianjun Kong09cb1052008-11-03 00:27:11 -08001254 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001256 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 lock_sock(sk);
1260
Stephen Hemminger132adf52007-03-08 20:44:43 -08001261 switch (optname) {
1262 case IP_OPTIONS:
1263 {
1264 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001265 struct ip_options *opt = (struct ip_options *)optbuf;
1266 struct ip_options_rcu *inet_opt;
1267
1268 inet_opt = rcu_dereference_protected(inet->inet_opt,
1269 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001270 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001271 if (inet_opt)
1272 memcpy(optbuf, &inet_opt->opt,
1273 sizeof(struct ip_options) +
1274 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001275 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Stephen Hemminger132adf52007-03-08 20:44:43 -08001277 if (opt->optlen == 0)
1278 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Stephen Hemminger132adf52007-03-08 20:44:43 -08001280 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Stephen Hemminger132adf52007-03-08 20:44:43 -08001282 len = min_t(unsigned int, len, opt->optlen);
1283 if (put_user(len, optlen))
1284 return -EFAULT;
1285 if (copy_to_user(optval, opt->__data, len))
1286 return -EFAULT;
1287 return 0;
1288 }
1289 case IP_PKTINFO:
1290 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1291 break;
1292 case IP_RECVTTL:
1293 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1294 break;
1295 case IP_RECVTOS:
1296 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1297 break;
1298 case IP_RECVOPTS:
1299 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1300 break;
1301 case IP_RETOPTS:
1302 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1303 break;
1304 case IP_PASSSEC:
1305 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1306 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001307 case IP_RECVORIGDSTADDR:
1308 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1309 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001310 case IP_CHECKSUM:
1311 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1312 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001313 case IP_TOS:
1314 val = inet->tos;
1315 break;
1316 case IP_TTL:
1317 val = (inet->uc_ttl == -1 ?
1318 sysctl_ip_default_ttl :
1319 inet->uc_ttl);
1320 break;
1321 case IP_HDRINCL:
1322 val = inet->hdrincl;
1323 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001324 case IP_NODEFRAG:
1325 val = inet->nodefrag;
1326 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001327 case IP_MTU_DISCOVER:
1328 val = inet->pmtudisc;
1329 break;
1330 case IP_MTU:
1331 {
1332 struct dst_entry *dst;
1333 val = 0;
1334 dst = sk_dst_get(sk);
1335 if (dst) {
1336 val = dst_mtu(dst);
1337 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001339 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001341 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001343 break;
1344 }
1345 case IP_RECVERR:
1346 val = inet->recverr;
1347 break;
1348 case IP_MULTICAST_TTL:
1349 val = inet->mc_ttl;
1350 break;
1351 case IP_MULTICAST_LOOP:
1352 val = inet->mc_loop;
1353 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001354 case IP_UNICAST_IF:
1355 val = (__force int)htonl((__u32) inet->uc_index);
1356 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001357 case IP_MULTICAST_IF:
1358 {
1359 struct in_addr addr;
1360 len = min_t(unsigned int, len, sizeof(struct in_addr));
1361 addr.s_addr = inet->mc_addr;
1362 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Stephen Hemminger132adf52007-03-08 20:44:43 -08001364 if (put_user(len, optlen))
1365 return -EFAULT;
1366 if (copy_to_user(optval, &addr, len))
1367 return -EFAULT;
1368 return 0;
1369 }
1370 case IP_MSFILTER:
1371 {
1372 struct ip_msfilter msf;
1373 int err;
1374
1375 if (len < IP_MSFILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001377 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001379 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001381 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001383 err = ip_mc_msfget(sk, &msf,
1384 (struct ip_msfilter __user *)optval, optlen);
1385 release_sock(sk);
1386 return err;
1387 }
1388 case MCAST_MSFILTER:
1389 {
1390 struct group_filter gsf;
1391 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Stephen Hemminger132adf52007-03-08 20:44:43 -08001393 if (len < GROUP_FILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001395 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001397 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001399 return -EFAULT;
1400 }
1401 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001402 (struct group_filter __user *)optval,
1403 optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001404 release_sock(sk);
1405 return err;
1406 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001407 case IP_MULTICAST_ALL:
1408 val = inet->mc_all;
1409 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001410 case IP_PKTOPTIONS:
1411 {
1412 struct msghdr msg;
1413
1414 release_sock(sk);
1415
1416 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001418
Karoly Kemenyc54a5e02014-07-27 12:29:07 +02001419 msg.msg_control = (__force void *) optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001420 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001421 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001422
1423 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1424 struct in_pktinfo info;
1425
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001426 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1427 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001428 info.ipi_ifindex = inet->mc_index;
1429 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1430 }
1431 if (inet->cmsg_flags & IP_CMSG_TTL) {
1432 int hlim = inet->mc_ttl;
1433 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1434 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001435 if (inet->cmsg_flags & IP_CMSG_TOS) {
1436 int tos = inet->rcv_tos;
1437 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1438 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001439 len -= msg.msg_controllen;
1440 return put_user(len, optlen);
1441 }
1442 case IP_FREEBIND:
1443 val = inet->freebind;
1444 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001445 case IP_TRANSPARENT:
1446 val = inet->transparent;
1447 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001448 case IP_MINTTL:
1449 val = inet->min_ttl;
1450 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001451 default:
1452 release_sock(sk);
1453 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001456
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001457 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 unsigned char ucval = (unsigned char)val;
1459 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001460 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001462 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return -EFAULT;
1464 } else {
1465 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001466 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001468 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return -EFAULT;
1470 }
1471 return 0;
1472}
1473
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001474int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001475 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001476{
1477 int err;
1478
Daniel Balutadd23198e2011-08-07 22:31:07 -07001479 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001480#ifdef CONFIG_NETFILTER
1481 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001482 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1483 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001484 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001485
Jianjun Kong09cb1052008-11-03 00:27:11 -08001486 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001487 return -EFAULT;
1488
1489 lock_sock(sk);
1490 err = nf_getsockopt(sk, PF_INET, optname, optval,
1491 &len);
1492 release_sock(sk);
1493 if (err >= 0)
1494 err = put_user(len, optlen);
1495 return err;
1496 }
1497#endif
1498 return err;
1499}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001500EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001501
1502#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001503int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1504 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001505{
David L Stevens42908c62008-04-29 03:23:22 -07001506 int err;
1507
1508 if (optname == MCAST_MSFILTER)
1509 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1510 ip_getsockopt);
1511
Daniel Balutadd23198e2011-08-07 22:31:07 -07001512 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1513 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001514
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001515#ifdef CONFIG_NETFILTER
1516 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001517 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1518 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001519 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001520
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001521 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001522 return -EFAULT;
1523
1524 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001525 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001526 release_sock(sk);
1527 if (err >= 0)
1528 err = put_user(len, optlen);
1529 return err;
1530 }
1531#endif
1532 return err;
1533}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001534EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001535#endif