blob: a317797b3cd020472f3490a82bf7c749b08b8b9e [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 Dumazetc720c7e82009-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 }
354 if (new_ra == NULL) {
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
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300390 if (skb_pull(skb, payload - skb->data) != NULL) {
391 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 Bruijn829ae9d2014-11-30 22:22:34 -0500435static bool ipv4_pktinfo_prepare_errqueue(const struct sock *sk,
436 const struct sk_buff *skb,
437 int ee_origin)
438{
439 struct in_pktinfo *info = PKTINFO_SKB_CB(skb);
440
441 if ((ee_origin != SO_EE_ORIGIN_TIMESTAMPING) ||
442 (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG)) ||
443 (!skb->dev))
444 return false;
445
446 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
447 info->ipi_ifindex = skb->dev->ifindex;
448 return true;
449}
450
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900451/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * Handle MSG_ERRQUEUE
453 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100454int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400457 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100458 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct {
460 struct sock_extended_err ee;
461 struct sockaddr_in offender;
462 } errhdr;
463 int err;
464 int copied;
465
Willem de Bruijn7ce875e2014-11-30 22:22:33 -0500466 WARN_ON_ONCE(sk->sk_family == AF_INET6);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400469 skb = sock_dequeue_err_skb(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (skb == NULL)
471 goto out;
472
473 copied = skb->len;
474 if (copied > len) {
475 msg->msg_flags |= MSG_TRUNC;
476 copied = len;
477 }
David S. Miller51f3d022014-11-05 16:46:40 -0500478 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (err)
480 goto out_free_skb;
481
482 sock_recv_timestamp(msg, sk, skb);
483
484 serr = SKB_EXT_ERR(skb);
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (sin) {
487 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700488 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
489 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 sin->sin_port = serr->port;
491 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100492 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494
495 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
496 sin = &errhdr.offender;
497 sin->sin_family = AF_UNSPEC;
Willem de Bruijn829ae9d2014-11-30 22:22:34 -0500498
499 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
500 ipv4_pktinfo_prepare_errqueue(sk, skb, serr->ee.ee_origin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct inet_sock *inet = inet_sk(sk);
502
503 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700504 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 sin->sin_port = 0;
506 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
507 if (inet->cmsg_flags)
508 ip_cmsg_recv(msg, skb);
509 }
510
511 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
512
513 /* Now we could try to dump offended packet options */
514
515 msg->msg_flags |= MSG_ERRQUEUE;
516 err = copied;
517
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900518out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 kfree_skb(skb);
520out:
521 return err;
522}
523
524
525/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700526 * Socket option code for IP. This is the end of the line after any
527 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
529
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800530static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700531 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 struct inet_sock *inet = inet_sk(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800534 int val = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Xi Wang0c9f79b2012-11-11 11:20:01 +0000536 switch (optname) {
537 case IP_PKTINFO:
538 case IP_RECVTTL:
539 case IP_RECVOPTS:
540 case IP_RECVTOS:
541 case IP_RETOPTS:
542 case IP_TOS:
543 case IP_TTL:
544 case IP_HDRINCL:
545 case IP_MTU_DISCOVER:
546 case IP_RECVERR:
547 case IP_ROUTER_ALERT:
548 case IP_FREEBIND:
549 case IP_PASSSEC:
550 case IP_TRANSPARENT:
551 case IP_MINTTL:
552 case IP_NODEFRAG:
553 case IP_UNICAST_IF:
554 case IP_MULTICAST_TTL:
555 case IP_MULTICAST_ALL:
556 case IP_MULTICAST_LOOP:
557 case IP_RECVORIGDSTADDR:
Tom Herbertad6f9392015-01-05 13:56:17 -0800558 case IP_CHECKSUM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (optlen >= sizeof(int)) {
560 if (get_user(val, (int __user *) optval))
561 return -EFAULT;
562 } else if (optlen >= sizeof(char)) {
563 unsigned char ucval;
564
565 if (get_user(ucval, (unsigned char __user *) optval))
566 return -EFAULT;
567 val = (int) ucval;
568 }
569 }
570
571 /* If optlen==0, it is equivalent to val == 0 */
572
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800573 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800574 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 err = 0;
577 lock_sock(sk);
578
579 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800580 case IP_OPTIONS:
581 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000582 struct ip_options_rcu *old, *opt = NULL;
583
roel kluin65a1c4f2009-10-23 05:59:21 +0000584 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800585 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900586 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700587 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800588 if (err)
589 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000590 old = rcu_dereference_protected(inet->inet_opt,
591 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800592 if (inet->is_icsk) {
593 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000594#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800595 if (sk->sk_family == PF_INET ||
596 (!((1 << sk->sk_state) &
597 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000598 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000600 if (old)
601 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800602 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000603 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800604 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000605#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000609 rcu_assign_pointer(inet->inet_opt, opt);
610 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800611 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800612 break;
613 }
614 case IP_PKTINFO:
615 if (val)
616 inet->cmsg_flags |= IP_CMSG_PKTINFO;
617 else
618 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
619 break;
620 case IP_RECVTTL:
621 if (val)
622 inet->cmsg_flags |= IP_CMSG_TTL;
623 else
624 inet->cmsg_flags &= ~IP_CMSG_TTL;
625 break;
626 case IP_RECVTOS:
627 if (val)
628 inet->cmsg_flags |= IP_CMSG_TOS;
629 else
630 inet->cmsg_flags &= ~IP_CMSG_TOS;
631 break;
632 case IP_RECVOPTS:
633 if (val)
634 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
635 else
636 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
637 break;
638 case IP_RETOPTS:
639 if (val)
640 inet->cmsg_flags |= IP_CMSG_RETOPTS;
641 else
642 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
643 break;
644 case IP_PASSSEC:
645 if (val)
646 inet->cmsg_flags |= IP_CMSG_PASSSEC;
647 else
648 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
649 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800650 case IP_RECVORIGDSTADDR:
651 if (val)
652 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
653 else
654 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
655 break;
Tom Herbertad6f9392015-01-05 13:56:17 -0800656 case IP_CHECKSUM:
657 if (val) {
658 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
659 inet_inc_convert_csum(sk);
660 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
661 }
662 } else {
663 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
664 inet_dec_convert_csum(sk);
665 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
666 }
667 }
668 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800669 case IP_TOS: /* This sets both TOS and Precedence */
670 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400671 val &= ~INET_ECN_MASK;
672 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800674 if (inet->tos != val) {
675 inet->tos = val;
676 sk->sk_priority = rt_tos2priority(val);
677 sk_dst_reset(sk);
678 }
679 break;
680 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700681 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800682 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000683 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800684 goto e_inval;
685 inet->uc_ttl = val;
686 break;
687 case IP_HDRINCL:
688 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800691 }
692 inet->hdrincl = val ? 1 : 0;
693 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000694 case IP_NODEFRAG:
695 if (sk->sk_type != SOCK_RAW) {
696 err = -ENOPROTOOPT;
697 break;
698 }
699 inet->nodefrag = val ? 1 : 0;
700 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800701 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100702 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800703 goto e_inval;
704 inet->pmtudisc = val;
705 break;
706 case IP_RECVERR:
707 inet->recverr = !!val;
708 if (!val)
709 skb_queue_purge(&sk->sk_error_queue);
710 break;
711 case IP_MULTICAST_TTL:
712 if (sk->sk_type == SOCK_STREAM)
713 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700714 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800715 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800716 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800717 val = 1;
718 if (val < 0 || val > 255)
719 goto e_inval;
720 inet->mc_ttl = val;
721 break;
722 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700723 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800724 goto e_inval;
725 inet->mc_loop = !!val;
726 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000727 case IP_UNICAST_IF:
728 {
729 struct net_device *dev = NULL;
730 int ifindex;
731
732 if (optlen != sizeof(int))
733 goto e_inval;
734
735 ifindex = (__force int)ntohl((__force __be32)val);
736 if (ifindex == 0) {
737 inet->uc_index = 0;
738 err = 0;
739 break;
740 }
741
742 dev = dev_get_by_index(sock_net(sk), ifindex);
743 err = -EADDRNOTAVAIL;
744 if (!dev)
745 break;
746 dev_put(dev);
747
748 err = -EINVAL;
749 if (sk->sk_bound_dev_if)
750 break;
751
752 inet->uc_index = ifindex;
753 err = 0;
754 break;
755 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800756 case IP_MULTICAST_IF:
757 {
758 struct ip_mreqn mreq;
759 struct net_device *dev = NULL;
760
761 if (sk->sk_type == SOCK_STREAM)
762 goto e_inval;
763 /*
764 * Check the arguments are allowable
765 */
766
Shan Wei09159212009-09-22 15:41:10 +0000767 if (optlen < sizeof(struct in_addr))
768 goto e_inval;
769
Stephen Hemminger132adf52007-03-08 20:44:43 -0800770 err = -EFAULT;
771 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800772 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800773 break;
774 } else {
775 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000776 if (optlen >= sizeof(struct ip_mreq)) {
777 if (copy_from_user(&mreq, optval,
778 sizeof(struct ip_mreq)))
779 break;
780 } else if (optlen >= sizeof(struct in_addr)) {
781 if (copy_from_user(&mreq.imr_address, optval,
782 sizeof(struct in_addr)))
783 break;
784 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800785 }
786
787 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700788 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800789 inet->mc_index = 0;
790 inet->mc_addr = 0;
791 err = 0;
792 break;
793 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900794 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000795 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800796 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800797 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000798 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800799
800
801 err = -EADDRNOTAVAIL;
802 if (!dev)
803 break;
Eric Dumazet55b80502009-10-19 06:41:58 +0000804 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800805
806 err = -EINVAL;
807 if (sk->sk_bound_dev_if &&
808 mreq.imr_ifindex != sk->sk_bound_dev_if)
809 break;
810
811 inet->mc_index = mreq.imr_ifindex;
812 inet->mc_addr = mreq.imr_address.s_addr;
813 err = 0;
814 break;
815 }
816
817 case IP_ADD_MEMBERSHIP:
818 case IP_DROP_MEMBERSHIP:
819 {
820 struct ip_mreqn mreq;
821
Flavio Leitnera96fb492007-08-24 22:16:39 -0700822 err = -EPROTO;
823 if (inet_sk(sk)->is_icsk)
824 break;
825
Stephen Hemminger132adf52007-03-08 20:44:43 -0800826 if (optlen < sizeof(struct ip_mreq))
827 goto e_inval;
828 err = -EFAULT;
829 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800830 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800831 break;
832 } else {
833 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800834 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800835 break;
836 }
837
838 if (optname == IP_ADD_MEMBERSHIP)
839 err = ip_mc_join_group(sk, &mreq);
840 else
841 err = ip_mc_leave_group(sk, &mreq);
842 break;
843 }
844 case IP_MSFILTER:
845 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800846 struct ip_msfilter *msf;
847
848 if (optlen < IP_MSFILTER_SIZE(0))
849 goto e_inval;
850 if (optlen > sysctl_optmem_max) {
851 err = -ENOBUFS;
852 break;
853 }
854 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700855 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800856 err = -ENOBUFS;
857 break;
858 }
859 err = -EFAULT;
860 if (copy_from_user(msf, optval, optlen)) {
861 kfree(msf);
862 break;
863 }
864 /* numsrc >= (1G-4) overflow in 32 bits */
865 if (msf->imsf_numsrc >= 0x3ffffffcU ||
866 msf->imsf_numsrc > sysctl_igmp_max_msf) {
867 kfree(msf);
868 err = -ENOBUFS;
869 break;
870 }
871 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
872 kfree(msf);
873 err = -EINVAL;
874 break;
875 }
876 err = ip_mc_msfilter(sk, msf, 0);
877 kfree(msf);
878 break;
879 }
880 case IP_BLOCK_SOURCE:
881 case IP_UNBLOCK_SOURCE:
882 case IP_ADD_SOURCE_MEMBERSHIP:
883 case IP_DROP_SOURCE_MEMBERSHIP:
884 {
885 struct ip_mreq_source mreqs;
886 int omode, add;
887
888 if (optlen != sizeof(struct ip_mreq_source))
889 goto e_inval;
890 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
891 err = -EFAULT;
892 break;
893 }
894 if (optname == IP_BLOCK_SOURCE) {
895 omode = MCAST_EXCLUDE;
896 add = 1;
897 } else if (optname == IP_UNBLOCK_SOURCE) {
898 omode = MCAST_EXCLUDE;
899 add = 0;
900 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
901 struct ip_mreqn mreq;
902
903 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
904 mreq.imr_address.s_addr = mreqs.imr_interface;
905 mreq.imr_ifindex = 0;
906 err = ip_mc_join_group(sk, &mreq);
907 if (err && err != -EADDRINUSE)
908 break;
909 omode = MCAST_INCLUDE;
910 add = 1;
911 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
912 omode = MCAST_INCLUDE;
913 add = 0;
914 }
915 err = ip_mc_source(add, omode, sk, &mreqs, 0);
916 break;
917 }
918 case MCAST_JOIN_GROUP:
919 case MCAST_LEAVE_GROUP:
920 {
921 struct group_req greq;
922 struct sockaddr_in *psin;
923 struct ip_mreqn mreq;
924
925 if (optlen < sizeof(struct group_req))
926 goto e_inval;
927 err = -EFAULT;
928 if (copy_from_user(&greq, optval, sizeof(greq)))
929 break;
930 psin = (struct sockaddr_in *)&greq.gr_group;
931 if (psin->sin_family != AF_INET)
932 goto e_inval;
933 memset(&mreq, 0, sizeof(mreq));
934 mreq.imr_multiaddr = psin->sin_addr;
935 mreq.imr_ifindex = greq.gr_interface;
936
937 if (optname == MCAST_JOIN_GROUP)
938 err = ip_mc_join_group(sk, &mreq);
939 else
940 err = ip_mc_leave_group(sk, &mreq);
941 break;
942 }
943 case MCAST_JOIN_SOURCE_GROUP:
944 case MCAST_LEAVE_SOURCE_GROUP:
945 case MCAST_BLOCK_SOURCE:
946 case MCAST_UNBLOCK_SOURCE:
947 {
948 struct group_source_req greqs;
949 struct ip_mreq_source mreqs;
950 struct sockaddr_in *psin;
951 int omode, add;
952
953 if (optlen != sizeof(struct group_source_req))
954 goto e_inval;
955 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
956 err = -EFAULT;
957 break;
958 }
959 if (greqs.gsr_group.ss_family != AF_INET ||
960 greqs.gsr_source.ss_family != AF_INET) {
961 err = -EADDRNOTAVAIL;
962 break;
963 }
964 psin = (struct sockaddr_in *)&greqs.gsr_group;
965 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
966 psin = (struct sockaddr_in *)&greqs.gsr_source;
967 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
968 mreqs.imr_interface = 0; /* use index for mc_source */
969
970 if (optname == MCAST_BLOCK_SOURCE) {
971 omode = MCAST_EXCLUDE;
972 add = 1;
973 } else if (optname == MCAST_UNBLOCK_SOURCE) {
974 omode = MCAST_EXCLUDE;
975 add = 0;
976 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
977 struct ip_mreqn mreq;
978
979 psin = (struct sockaddr_in *)&greqs.gsr_group;
980 mreq.imr_multiaddr = psin->sin_addr;
981 mreq.imr_address.s_addr = 0;
982 mreq.imr_ifindex = greqs.gsr_interface;
983 err = ip_mc_join_group(sk, &mreq);
984 if (err && err != -EADDRINUSE)
985 break;
986 greqs.gsr_interface = mreq.imr_ifindex;
987 omode = MCAST_INCLUDE;
988 add = 1;
989 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
990 omode = MCAST_INCLUDE;
991 add = 0;
992 }
993 err = ip_mc_source(add, omode, sk, &mreqs,
994 greqs.gsr_interface);
995 break;
996 }
997 case MCAST_MSFILTER:
998 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800999 struct sockaddr_in *psin;
1000 struct ip_msfilter *msf = NULL;
1001 struct group_filter *gsf = NULL;
1002 int msize, i, ifindex;
1003
1004 if (optlen < GROUP_FILTER_SIZE(0))
1005 goto e_inval;
1006 if (optlen > sysctl_optmem_max) {
1007 err = -ENOBUFS;
1008 break;
1009 }
Jianjun Kong09cb1052008-11-03 00:27:11 -08001010 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001011 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001012 err = -ENOBUFS;
1013 break;
1014 }
1015 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001016 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001017 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001018
Stephen Hemminger132adf52007-03-08 20:44:43 -08001019 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1020 if (gsf->gf_numsrc >= 0x1ffffff ||
1021 gsf->gf_numsrc > sysctl_igmp_max_msf) {
1022 err = -ENOBUFS;
1023 goto mc_msf_out;
1024 }
1025 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
1026 err = -EINVAL;
1027 goto mc_msf_out;
1028 }
1029 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -08001030 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001031 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001032 err = -ENOBUFS;
1033 goto mc_msf_out;
1034 }
1035 ifindex = gsf->gf_interface;
1036 psin = (struct sockaddr_in *)&gsf->gf_group;
1037 if (psin->sin_family != AF_INET) {
1038 err = -EADDRNOTAVAIL;
1039 goto mc_msf_out;
1040 }
1041 msf->imsf_multiaddr = psin->sin_addr.s_addr;
1042 msf->imsf_interface = 0;
1043 msf->imsf_fmode = gsf->gf_fmode;
1044 msf->imsf_numsrc = gsf->gf_numsrc;
1045 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001046 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001047 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
1048
1049 if (psin->sin_family != AF_INET)
1050 goto mc_msf_out;
1051 msf->imsf_slist[i] = psin->sin_addr.s_addr;
1052 }
1053 kfree(gsf);
1054 gsf = NULL;
1055
1056 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001057mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -08001058 kfree(msf);
1059 kfree(gsf);
1060 break;
1061 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001062 case IP_MULTICAST_ALL:
1063 if (optlen < 1)
1064 goto e_inval;
1065 if (val != 0 && val != 1)
1066 goto e_inval;
1067 inet->mc_all = val;
1068 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001069 case IP_ROUTER_ALERT:
1070 err = ip_ra_control(sk, val ? 1 : 0, NULL);
1071 break;
1072
1073 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001074 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001075 goto e_inval;
1076 inet->freebind = !!val;
1077 break;
1078
1079 case IP_IPSEC_POLICY:
1080 case IP_XFRM_POLICY:
1081 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001082 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001083 break;
1084 err = xfrm_user_policy(sk, optname, optval, optlen);
1085 break;
1086
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001087 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001088 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1089 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001090 err = -EPERM;
1091 break;
1092 }
1093 if (optlen < 1)
1094 goto e_inval;
1095 inet->transparent = !!val;
1096 break;
1097
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001098 case IP_MINTTL:
1099 if (optlen < 1)
1100 goto e_inval;
1101 if (val < 0 || val > 255)
1102 goto e_inval;
1103 inet->min_ttl = val;
1104 break;
1105
Stephen Hemminger132adf52007-03-08 20:44:43 -08001106 default:
1107 err = -ENOPROTOOPT;
1108 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110 release_sock(sk);
1111 return err;
1112
1113e_inval:
1114 release_sock(sk);
1115 return -EINVAL;
1116}
1117
Eric Dumazetf84af322010-04-28 15:31:51 -07001118/**
Willem de Bruijn829ae9d2014-11-30 22:22:34 -05001119 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001120 * @sk: socket
1121 * @skb: buffer
1122 *
David S. Miller35ebf652012-06-28 03:59:11 -07001123 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1124 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001125 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001126 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001127void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001128{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001129 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001130 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1131 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001132
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001133 if (prepare && skb_rtable(skb)) {
David S. Miller92101b32012-07-23 16:29:00 -07001134 pktinfo->ipi_ifindex = inet_iif(skb);
David S. Miller35ebf652012-06-28 03:59:11 -07001135 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001136 } else {
1137 pktinfo->ipi_ifindex = 0;
1138 pktinfo->ipi_spec_dst.s_addr = 0;
1139 }
1140 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001141}
Eric Dumazetf84af322010-04-28 15:31:51 -07001142
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001143int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001144 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001145{
1146 int err;
1147
1148 if (level != SOL_IP)
1149 return -ENOPROTOOPT;
1150
1151 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1152#ifdef CONFIG_NETFILTER
1153 /* we need to exclude all possible ENOPROTOOPTs except default case */
1154 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001155 optname != IP_IPSEC_POLICY &&
1156 optname != IP_XFRM_POLICY &&
1157 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001158 lock_sock(sk);
1159 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1160 release_sock(sk);
1161 }
1162#endif
1163 return err;
1164}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001165EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001166
1167#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001168int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001169 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001170{
1171 int err;
1172
1173 if (level != SOL_IP)
1174 return -ENOPROTOOPT;
1175
David L Stevensdae50292008-04-27 01:06:07 -07001176 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1177 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1178 ip_setsockopt);
1179
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001180 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1181#ifdef CONFIG_NETFILTER
1182 /* we need to exclude all possible ENOPROTOOPTs except default case */
1183 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001184 optname != IP_IPSEC_POLICY &&
1185 optname != IP_XFRM_POLICY &&
1186 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001187 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001188 err = compat_nf_setsockopt(sk, PF_INET, optname,
1189 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001190 release_sock(sk);
1191 }
1192#endif
1193 return err;
1194}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001195EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001196#endif
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001199 * Get the options. Note for future reference. The GET of IP options gets
1200 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 */
1202
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001203static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001204 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 struct inet_sock *inet = inet_sk(sk);
1207 int val;
1208 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001209
Stephen Hemminger132adf52007-03-08 20:44:43 -08001210 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return -EOPNOTSUPP;
1212
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001213 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001214 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Jianjun Kong09cb1052008-11-03 00:27:11 -08001216 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001218 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 lock_sock(sk);
1222
Stephen Hemminger132adf52007-03-08 20:44:43 -08001223 switch (optname) {
1224 case IP_OPTIONS:
1225 {
1226 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001227 struct ip_options *opt = (struct ip_options *)optbuf;
1228 struct ip_options_rcu *inet_opt;
1229
1230 inet_opt = rcu_dereference_protected(inet->inet_opt,
1231 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001232 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001233 if (inet_opt)
1234 memcpy(optbuf, &inet_opt->opt,
1235 sizeof(struct ip_options) +
1236 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001237 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Stephen Hemminger132adf52007-03-08 20:44:43 -08001239 if (opt->optlen == 0)
1240 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Stephen Hemminger132adf52007-03-08 20:44:43 -08001242 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Stephen Hemminger132adf52007-03-08 20:44:43 -08001244 len = min_t(unsigned int, len, opt->optlen);
1245 if (put_user(len, optlen))
1246 return -EFAULT;
1247 if (copy_to_user(optval, opt->__data, len))
1248 return -EFAULT;
1249 return 0;
1250 }
1251 case IP_PKTINFO:
1252 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1253 break;
1254 case IP_RECVTTL:
1255 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1256 break;
1257 case IP_RECVTOS:
1258 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1259 break;
1260 case IP_RECVOPTS:
1261 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1262 break;
1263 case IP_RETOPTS:
1264 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1265 break;
1266 case IP_PASSSEC:
1267 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1268 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001269 case IP_RECVORIGDSTADDR:
1270 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1271 break;
Tom Herbertad6f9392015-01-05 13:56:17 -08001272 case IP_CHECKSUM:
1273 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1274 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001275 case IP_TOS:
1276 val = inet->tos;
1277 break;
1278 case IP_TTL:
1279 val = (inet->uc_ttl == -1 ?
1280 sysctl_ip_default_ttl :
1281 inet->uc_ttl);
1282 break;
1283 case IP_HDRINCL:
1284 val = inet->hdrincl;
1285 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001286 case IP_NODEFRAG:
1287 val = inet->nodefrag;
1288 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001289 case IP_MTU_DISCOVER:
1290 val = inet->pmtudisc;
1291 break;
1292 case IP_MTU:
1293 {
1294 struct dst_entry *dst;
1295 val = 0;
1296 dst = sk_dst_get(sk);
1297 if (dst) {
1298 val = dst_mtu(dst);
1299 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001301 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001303 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001305 break;
1306 }
1307 case IP_RECVERR:
1308 val = inet->recverr;
1309 break;
1310 case IP_MULTICAST_TTL:
1311 val = inet->mc_ttl;
1312 break;
1313 case IP_MULTICAST_LOOP:
1314 val = inet->mc_loop;
1315 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001316 case IP_UNICAST_IF:
1317 val = (__force int)htonl((__u32) inet->uc_index);
1318 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001319 case IP_MULTICAST_IF:
1320 {
1321 struct in_addr addr;
1322 len = min_t(unsigned int, len, sizeof(struct in_addr));
1323 addr.s_addr = inet->mc_addr;
1324 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Stephen Hemminger132adf52007-03-08 20:44:43 -08001326 if (put_user(len, optlen))
1327 return -EFAULT;
1328 if (copy_to_user(optval, &addr, len))
1329 return -EFAULT;
1330 return 0;
1331 }
1332 case IP_MSFILTER:
1333 {
1334 struct ip_msfilter msf;
1335 int err;
1336
1337 if (len < IP_MSFILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001339 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001341 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001343 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001345 err = ip_mc_msfget(sk, &msf,
1346 (struct ip_msfilter __user *)optval, optlen);
1347 release_sock(sk);
1348 return err;
1349 }
1350 case MCAST_MSFILTER:
1351 {
1352 struct group_filter gsf;
1353 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Stephen Hemminger132adf52007-03-08 20:44:43 -08001355 if (len < GROUP_FILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001357 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001359 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001361 return -EFAULT;
1362 }
1363 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001364 (struct group_filter __user *)optval,
1365 optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001366 release_sock(sk);
1367 return err;
1368 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001369 case IP_MULTICAST_ALL:
1370 val = inet->mc_all;
1371 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001372 case IP_PKTOPTIONS:
1373 {
1374 struct msghdr msg;
1375
1376 release_sock(sk);
1377
1378 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001380
Karoly Kemenyc54a5e02014-07-27 12:29:07 +02001381 msg.msg_control = (__force void *) optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001382 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001383 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001384
1385 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1386 struct in_pktinfo info;
1387
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001388 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1389 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001390 info.ipi_ifindex = inet->mc_index;
1391 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1392 }
1393 if (inet->cmsg_flags & IP_CMSG_TTL) {
1394 int hlim = inet->mc_ttl;
1395 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1396 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001397 if (inet->cmsg_flags & IP_CMSG_TOS) {
1398 int tos = inet->rcv_tos;
1399 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1400 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001401 len -= msg.msg_controllen;
1402 return put_user(len, optlen);
1403 }
1404 case IP_FREEBIND:
1405 val = inet->freebind;
1406 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001407 case IP_TRANSPARENT:
1408 val = inet->transparent;
1409 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001410 case IP_MINTTL:
1411 val = inet->min_ttl;
1412 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001413 default:
1414 release_sock(sk);
1415 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001418
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001419 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 unsigned char ucval = (unsigned char)val;
1421 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001422 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001424 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return -EFAULT;
1426 } else {
1427 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001428 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001430 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return -EFAULT;
1432 }
1433 return 0;
1434}
1435
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001436int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001437 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001438{
1439 int err;
1440
Daniel Balutadd23198e2011-08-07 22:31:07 -07001441 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001442#ifdef CONFIG_NETFILTER
1443 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001444 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1445 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001446 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001447
Jianjun Kong09cb1052008-11-03 00:27:11 -08001448 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001449 return -EFAULT;
1450
1451 lock_sock(sk);
1452 err = nf_getsockopt(sk, PF_INET, optname, optval,
1453 &len);
1454 release_sock(sk);
1455 if (err >= 0)
1456 err = put_user(len, optlen);
1457 return err;
1458 }
1459#endif
1460 return err;
1461}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001462EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001463
1464#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001465int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1466 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001467{
David L Stevens42908c62008-04-29 03:23:22 -07001468 int err;
1469
1470 if (optname == MCAST_MSFILTER)
1471 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1472 ip_getsockopt);
1473
Daniel Balutadd23198e2011-08-07 22:31:07 -07001474 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1475 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001476
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001477#ifdef CONFIG_NETFILTER
1478 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001479 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1480 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001481 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001482
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001483 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001484 return -EFAULT;
1485
1486 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001487 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001488 release_sock(sk);
1489 if (err >= 0)
1490 err = put_user(len, optlen);
1491 return err;
1492 }
1493#endif
1494 return err;
1495}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001496EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001497#endif