blob: 3c9d20880283de0f9b5244eae8184e76d9a20dcd [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>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000040#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <net/transp_v6.h>
42#endif
David S. Miller35ebf652012-06-28 03:59:11 -070043#include <net/ip_fib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <linux/errqueue.h>
46#include <asm/uaccess.h>
47
48#define IP_CMSG_PKTINFO 1
49#define IP_CMSG_TTL 2
50#define IP_CMSG_TOS 4
51#define IP_CMSG_RECVOPTS 8
52#define IP_CMSG_RETOPTS 16
Catherine Zhang2c7946a2006-03-20 22:41:23 -080053#define IP_CMSG_PASSSEC 32
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -080054#define IP_CMSG_ORIGDSTADDR 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * SOL_IP control messages.
58 */
Eric Dumazetd826eb12011-11-09 07:24:35 +000059#define PKTINFO_SKB_CB(__skb) ((struct in_pktinfo *)((__skb)->cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
62{
Eric Dumazetd826eb12011-11-09 07:24:35 +000063 struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070065 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
68}
69
70static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
71{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070072 int ttl = ip_hdr(skb)->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
74}
75
76static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
77{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070078 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
82{
83 if (IPCB(skb)->opt.optlen == 0)
84 return;
85
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070086 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
87 ip_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90
91static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
92{
93 unsigned char optbuf[sizeof(struct ip_options) + 40];
Daniel Baluta5e73ea12012-04-15 01:34:41 +000094 struct ip_options *opt = (struct ip_options *)optbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 if (IPCB(skb)->opt.optlen == 0)
97 return;
98
99 if (ip_options_echo(opt, skb)) {
100 msg->msg_flags |= MSG_CTRUNC;
101 return;
102 }
103 ip_options_undo(opt);
104
105 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
106}
107
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800108static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
109{
110 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700111 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800112 int err;
113
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700114 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
115 if (err)
116 return;
117
118 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800119 if (err)
120 return;
121
122 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700123 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800124}
125
Harvey Harrison21d1a162008-11-20 01:54:27 -0800126static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800127{
128 struct sockaddr_in sin;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000129 const struct iphdr *iph = ip_hdr(skb);
Harvey Harrison21d1a162008-11-20 01:54:27 -0800130 __be16 *ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800131
132 if (skb_transport_offset(skb) + 4 > skb->len)
133 return;
134
135 /* All current transport protocols have the port numbers in the
136 * first four bytes of the transport header and this function is
137 * written with this assumption in mind.
138 */
139
140 sin.sin_family = AF_INET;
141 sin.sin_addr.s_addr = iph->daddr;
142 sin.sin_port = ports[1];
143 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
144
145 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
149{
150 struct inet_sock *inet = inet_sk(skb->sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000151 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 /* Ordered by supposed usage frequency */
154 if (flags & 1)
155 ip_cmsg_recv_pktinfo(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700156 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return;
158
159 if (flags & 1)
160 ip_cmsg_recv_ttl(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700161 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return;
163
164 if (flags & 1)
165 ip_cmsg_recv_tos(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700166 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return;
168
169 if (flags & 1)
170 ip_cmsg_recv_opts(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700171 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return;
173
174 if (flags & 1)
175 ip_cmsg_recv_retopts(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700176 if ((flags >>= 1) == 0)
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800177 return;
178
179 if (flags & 1)
180 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800181
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700182 if ((flags >>= 1) == 0)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800183 return;
184 if (flags & 1)
185 ip_cmsg_recv_dstaddr(msg, skb);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700188EXPORT_SYMBOL(ip_cmsg_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Denis V. Lunev7a6adb92008-03-24 15:30:27 -0700190int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 int err;
193 struct cmsghdr *cmsg;
194
195 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
196 if (!CMSG_OK(msg, cmsg))
197 return -EINVAL;
198 if (cmsg->cmsg_level != SOL_IP)
199 continue;
200 switch (cmsg->cmsg_type) {
201 case IP_RETOPTS:
202 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700203 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
204 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (err)
206 return err;
207 break;
208 case IP_PKTINFO:
209 {
210 struct in_pktinfo *info;
211 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
212 return -EINVAL;
213 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
214 ipc->oif = info->ipi_ifindex;
215 ipc->addr = info->ipi_spec_dst.s_addr;
216 break;
217 }
218 default:
219 return -EINVAL;
220 }
221 }
222 return 0;
223}
224
225
226/* Special input handler for packets caught by router alert option.
227 They are selected only by protocol field, and then processed likely
228 local ones; but only if someone wants them! Otherwise, router
229 not running rsvpd will kill RSVP.
230
231 It is user level problem, what it will make with them.
232 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
233 but receiver should be enough clever f.e. to forward mtrace requests,
234 sent to multicast group to reach destination designated router.
235 */
Eric Dumazet43a951e2010-10-25 03:32:44 +0000236struct ip_ra_chain __rcu *ip_ra_chain;
Eric Dumazet66018502010-06-07 03:12:08 +0000237static DEFINE_SPINLOCK(ip_ra_lock);
238
Eric Dumazet592fcb92010-06-09 16:21:07 +0000239
240static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000241{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000242 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
243
244 sock_put(ra->saved_sk);
245 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000246}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700248int ip_ra_control(struct sock *sk, unsigned char on,
249 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000251 struct ip_ra_chain *ra, *new_ra;
252 struct ip_ra_chain __rcu **rap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000254 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -EINVAL;
256
257 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
258
Eric Dumazet66018502010-06-07 03:12:08 +0000259 spin_lock_bh(&ip_ra_lock);
Eric Dumazet43a951e2010-10-25 03:32:44 +0000260 for (rap = &ip_ra_chain;
261 (ra = rcu_dereference_protected(*rap,
262 lockdep_is_held(&ip_ra_lock))) != NULL;
263 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (ra->sk == sk) {
265 if (on) {
Eric Dumazet66018502010-06-07 03:12:08 +0000266 spin_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800267 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return -EADDRINUSE;
269 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000270 /* dont let ip_call_ra_chain() use sk again */
271 ra->sk = NULL;
Eric Dumazet66018502010-06-07 03:12:08 +0000272 rcu_assign_pointer(*rap, ra->next);
273 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 if (ra->destructor)
276 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000277 /*
278 * Delay sock_put(sk) and kfree(ra) after one rcu grace
279 * period. This guarantee ip_call_ra_chain() dont need
280 * to mess with socket refcounts.
281 */
282 ra->saved_sk = sk;
283 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0;
285 }
286 }
287 if (new_ra == NULL) {
Eric Dumazet66018502010-06-07 03:12:08 +0000288 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return -ENOBUFS;
290 }
291 new_ra->sk = sk;
292 new_ra->destructor = destructor;
293
294 new_ra->next = ra;
Eric Dumazet66018502010-06-07 03:12:08 +0000295 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 sock_hold(sk);
Eric Dumazet66018502010-06-07 03:12:08 +0000297 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 return 0;
300}
301
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900302void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700303 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct sock_exterr_skb *serr;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 skb = skb_clone(skb, GFP_ATOMIC);
308 if (!skb)
309 return;
310
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900311 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 serr->ee.ee_errno = err;
313 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300314 serr->ee.ee_type = icmp_hdr(skb)->type;
315 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 serr->ee.ee_pad = 0;
317 serr->ee.ee_info = info;
318 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300319 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700320 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 serr->port = port;
322
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300323 if (skb_pull(skb, payload - skb->data) != NULL) {
324 skb_reset_transport_header(skb);
325 if (sock_queue_err_skb(sk, skb) == 0)
326 return;
327 }
328 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Al Viro05790162006-09-27 18:33:40 -0700331void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct inet_sock *inet = inet_sk(sk);
334 struct sock_exterr_skb *serr;
335 struct iphdr *iph;
336 struct sk_buff *skb;
337
338 if (!inet->recverr)
339 return;
340
341 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
342 if (!skb)
343 return;
344
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300345 skb_put(skb, sizeof(struct iphdr));
346 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700347 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 iph->daddr = daddr;
349
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900350 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 serr->ee.ee_errno = err;
352 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900353 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 serr->ee.ee_code = 0;
355 serr->ee.ee_pad = 0;
356 serr->ee.ee_info = info;
357 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700358 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 serr->port = port;
360
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700361 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300362 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 if (sock_queue_err_skb(sk, skb))
365 kfree_skb(skb);
366}
367
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900368/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * Handle MSG_ERRQUEUE
370 */
371int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
372{
373 struct sock_exterr_skb *serr;
374 struct sk_buff *skb, *skb2;
375 struct sockaddr_in *sin;
376 struct {
377 struct sock_extended_err ee;
378 struct sockaddr_in offender;
379 } errhdr;
380 int err;
381 int copied;
382
383 err = -EAGAIN;
384 skb = skb_dequeue(&sk->sk_error_queue);
385 if (skb == NULL)
386 goto out;
387
388 copied = skb->len;
389 if (copied > len) {
390 msg->msg_flags |= MSG_TRUNC;
391 copied = len;
392 }
393 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
394 if (err)
395 goto out_free_skb;
396
397 sock_recv_timestamp(msg, sk, skb);
398
399 serr = SKB_EXT_ERR(skb);
400
401 sin = (struct sockaddr_in *)msg->msg_name;
402 if (sin) {
403 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700404 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
405 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 sin->sin_port = serr->port;
407 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
408 }
409
410 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
411 sin = &errhdr.offender;
412 sin->sin_family = AF_UNSPEC;
413 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
414 struct inet_sock *inet = inet_sk(sk);
415
416 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700417 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 sin->sin_port = 0;
419 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
420 if (inet->cmsg_flags)
421 ip_cmsg_recv(msg, skb);
422 }
423
424 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
425
426 /* Now we could try to dump offended packet options */
427
428 msg->msg_flags |= MSG_ERRQUEUE;
429 err = copied;
430
431 /* Reset and regenerate socket error */
Herbert Xue0f9f852005-06-18 22:56:18 -0700432 spin_lock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 sk->sk_err = 0;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700434 skb2 = skb_peek(&sk->sk_error_queue);
435 if (skb2 != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
Herbert Xue0f9f852005-06-18 22:56:18 -0700437 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 sk->sk_error_report(sk);
439 } else
Herbert Xue0f9f852005-06-18 22:56:18 -0700440 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900442out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 kfree_skb(skb);
444out:
445 return err;
446}
447
448
449/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700450 * Socket option code for IP. This is the end of the line after any
451 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
453
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800454static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700455 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 struct inet_sock *inet = inet_sk(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800458 int val = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Xi Wang0c9f79b2012-11-11 11:20:01 +0000460 switch (optname) {
461 case IP_PKTINFO:
462 case IP_RECVTTL:
463 case IP_RECVOPTS:
464 case IP_RECVTOS:
465 case IP_RETOPTS:
466 case IP_TOS:
467 case IP_TTL:
468 case IP_HDRINCL:
469 case IP_MTU_DISCOVER:
470 case IP_RECVERR:
471 case IP_ROUTER_ALERT:
472 case IP_FREEBIND:
473 case IP_PASSSEC:
474 case IP_TRANSPARENT:
475 case IP_MINTTL:
476 case IP_NODEFRAG:
477 case IP_UNICAST_IF:
478 case IP_MULTICAST_TTL:
479 case IP_MULTICAST_ALL:
480 case IP_MULTICAST_LOOP:
481 case IP_RECVORIGDSTADDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (optlen >= sizeof(int)) {
483 if (get_user(val, (int __user *) optval))
484 return -EFAULT;
485 } else if (optlen >= sizeof(char)) {
486 unsigned char ucval;
487
488 if (get_user(ucval, (unsigned char __user *) optval))
489 return -EFAULT;
490 val = (int) ucval;
491 }
492 }
493
494 /* If optlen==0, it is equivalent to val == 0 */
495
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800496 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800497 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 err = 0;
500 lock_sock(sk);
501
502 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800503 case IP_OPTIONS:
504 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000505 struct ip_options_rcu *old, *opt = NULL;
506
roel kluin65a1c4f2009-10-23 05:59:21 +0000507 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800508 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900509 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700510 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800511 if (err)
512 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000513 old = rcu_dereference_protected(inet->inet_opt,
514 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800515 if (inet->is_icsk) {
516 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000517#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800518 if (sk->sk_family == PF_INET ||
519 (!((1 << sk->sk_state) &
520 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000521 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000523 if (old)
524 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800525 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000526 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800527 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000528#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000532 rcu_assign_pointer(inet->inet_opt, opt);
533 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800534 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800535 break;
536 }
537 case IP_PKTINFO:
538 if (val)
539 inet->cmsg_flags |= IP_CMSG_PKTINFO;
540 else
541 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
542 break;
543 case IP_RECVTTL:
544 if (val)
545 inet->cmsg_flags |= IP_CMSG_TTL;
546 else
547 inet->cmsg_flags &= ~IP_CMSG_TTL;
548 break;
549 case IP_RECVTOS:
550 if (val)
551 inet->cmsg_flags |= IP_CMSG_TOS;
552 else
553 inet->cmsg_flags &= ~IP_CMSG_TOS;
554 break;
555 case IP_RECVOPTS:
556 if (val)
557 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
558 else
559 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
560 break;
561 case IP_RETOPTS:
562 if (val)
563 inet->cmsg_flags |= IP_CMSG_RETOPTS;
564 else
565 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
566 break;
567 case IP_PASSSEC:
568 if (val)
569 inet->cmsg_flags |= IP_CMSG_PASSSEC;
570 else
571 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
572 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800573 case IP_RECVORIGDSTADDR:
574 if (val)
575 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
576 else
577 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
578 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800579 case IP_TOS: /* This sets both TOS and Precedence */
580 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400581 val &= ~INET_ECN_MASK;
582 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800584 if (inet->tos != val) {
585 inet->tos = val;
586 sk->sk_priority = rt_tos2priority(val);
587 sk_dst_reset(sk);
588 }
589 break;
590 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700591 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800592 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700593 if (val != -1 && (val < 0 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800594 goto e_inval;
595 inet->uc_ttl = val;
596 break;
597 case IP_HDRINCL:
598 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800601 }
602 inet->hdrincl = val ? 1 : 0;
603 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000604 case IP_NODEFRAG:
605 if (sk->sk_type != SOCK_RAW) {
606 err = -ENOPROTOOPT;
607 break;
608 }
609 inet->nodefrag = val ? 1 : 0;
610 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800611 case IP_MTU_DISCOVER:
John Dykstra0eae7502009-10-19 21:53:53 -0700612 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800613 goto e_inval;
614 inet->pmtudisc = val;
615 break;
616 case IP_RECVERR:
617 inet->recverr = !!val;
618 if (!val)
619 skb_queue_purge(&sk->sk_error_queue);
620 break;
621 case IP_MULTICAST_TTL:
622 if (sk->sk_type == SOCK_STREAM)
623 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700624 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800625 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800626 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800627 val = 1;
628 if (val < 0 || val > 255)
629 goto e_inval;
630 inet->mc_ttl = val;
631 break;
632 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700633 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800634 goto e_inval;
635 inet->mc_loop = !!val;
636 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000637 case IP_UNICAST_IF:
638 {
639 struct net_device *dev = NULL;
640 int ifindex;
641
642 if (optlen != sizeof(int))
643 goto e_inval;
644
645 ifindex = (__force int)ntohl((__force __be32)val);
646 if (ifindex == 0) {
647 inet->uc_index = 0;
648 err = 0;
649 break;
650 }
651
652 dev = dev_get_by_index(sock_net(sk), ifindex);
653 err = -EADDRNOTAVAIL;
654 if (!dev)
655 break;
656 dev_put(dev);
657
658 err = -EINVAL;
659 if (sk->sk_bound_dev_if)
660 break;
661
662 inet->uc_index = ifindex;
663 err = 0;
664 break;
665 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800666 case IP_MULTICAST_IF:
667 {
668 struct ip_mreqn mreq;
669 struct net_device *dev = NULL;
670
671 if (sk->sk_type == SOCK_STREAM)
672 goto e_inval;
673 /*
674 * Check the arguments are allowable
675 */
676
Shan Wei09159212009-09-22 15:41:10 +0000677 if (optlen < sizeof(struct in_addr))
678 goto e_inval;
679
Stephen Hemminger132adf52007-03-08 20:44:43 -0800680 err = -EFAULT;
681 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800682 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800683 break;
684 } else {
685 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000686 if (optlen >= sizeof(struct ip_mreq)) {
687 if (copy_from_user(&mreq, optval,
688 sizeof(struct ip_mreq)))
689 break;
690 } else if (optlen >= sizeof(struct in_addr)) {
691 if (copy_from_user(&mreq.imr_address, optval,
692 sizeof(struct in_addr)))
693 break;
694 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800695 }
696
697 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700698 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800699 inet->mc_index = 0;
700 inet->mc_addr = 0;
701 err = 0;
702 break;
703 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900704 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000705 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800706 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800707 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000708 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800709
710
711 err = -EADDRNOTAVAIL;
712 if (!dev)
713 break;
Eric Dumazet55b80502009-10-19 06:41:58 +0000714 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800715
716 err = -EINVAL;
717 if (sk->sk_bound_dev_if &&
718 mreq.imr_ifindex != sk->sk_bound_dev_if)
719 break;
720
721 inet->mc_index = mreq.imr_ifindex;
722 inet->mc_addr = mreq.imr_address.s_addr;
723 err = 0;
724 break;
725 }
726
727 case IP_ADD_MEMBERSHIP:
728 case IP_DROP_MEMBERSHIP:
729 {
730 struct ip_mreqn mreq;
731
Flavio Leitnera96fb492007-08-24 22:16:39 -0700732 err = -EPROTO;
733 if (inet_sk(sk)->is_icsk)
734 break;
735
Stephen Hemminger132adf52007-03-08 20:44:43 -0800736 if (optlen < sizeof(struct ip_mreq))
737 goto e_inval;
738 err = -EFAULT;
739 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800740 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800741 break;
742 } else {
743 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800744 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800745 break;
746 }
747
748 if (optname == IP_ADD_MEMBERSHIP)
749 err = ip_mc_join_group(sk, &mreq);
750 else
751 err = ip_mc_leave_group(sk, &mreq);
752 break;
753 }
754 case IP_MSFILTER:
755 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800756 struct ip_msfilter *msf;
757
758 if (optlen < IP_MSFILTER_SIZE(0))
759 goto e_inval;
760 if (optlen > sysctl_optmem_max) {
761 err = -ENOBUFS;
762 break;
763 }
764 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700765 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800766 err = -ENOBUFS;
767 break;
768 }
769 err = -EFAULT;
770 if (copy_from_user(msf, optval, optlen)) {
771 kfree(msf);
772 break;
773 }
774 /* numsrc >= (1G-4) overflow in 32 bits */
775 if (msf->imsf_numsrc >= 0x3ffffffcU ||
776 msf->imsf_numsrc > sysctl_igmp_max_msf) {
777 kfree(msf);
778 err = -ENOBUFS;
779 break;
780 }
781 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
782 kfree(msf);
783 err = -EINVAL;
784 break;
785 }
786 err = ip_mc_msfilter(sk, msf, 0);
787 kfree(msf);
788 break;
789 }
790 case IP_BLOCK_SOURCE:
791 case IP_UNBLOCK_SOURCE:
792 case IP_ADD_SOURCE_MEMBERSHIP:
793 case IP_DROP_SOURCE_MEMBERSHIP:
794 {
795 struct ip_mreq_source mreqs;
796 int omode, add;
797
798 if (optlen != sizeof(struct ip_mreq_source))
799 goto e_inval;
800 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
801 err = -EFAULT;
802 break;
803 }
804 if (optname == IP_BLOCK_SOURCE) {
805 omode = MCAST_EXCLUDE;
806 add = 1;
807 } else if (optname == IP_UNBLOCK_SOURCE) {
808 omode = MCAST_EXCLUDE;
809 add = 0;
810 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
811 struct ip_mreqn mreq;
812
813 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
814 mreq.imr_address.s_addr = mreqs.imr_interface;
815 mreq.imr_ifindex = 0;
816 err = ip_mc_join_group(sk, &mreq);
817 if (err && err != -EADDRINUSE)
818 break;
819 omode = MCAST_INCLUDE;
820 add = 1;
821 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
822 omode = MCAST_INCLUDE;
823 add = 0;
824 }
825 err = ip_mc_source(add, omode, sk, &mreqs, 0);
826 break;
827 }
828 case MCAST_JOIN_GROUP:
829 case MCAST_LEAVE_GROUP:
830 {
831 struct group_req greq;
832 struct sockaddr_in *psin;
833 struct ip_mreqn mreq;
834
835 if (optlen < sizeof(struct group_req))
836 goto e_inval;
837 err = -EFAULT;
838 if (copy_from_user(&greq, optval, sizeof(greq)))
839 break;
840 psin = (struct sockaddr_in *)&greq.gr_group;
841 if (psin->sin_family != AF_INET)
842 goto e_inval;
843 memset(&mreq, 0, sizeof(mreq));
844 mreq.imr_multiaddr = psin->sin_addr;
845 mreq.imr_ifindex = greq.gr_interface;
846
847 if (optname == MCAST_JOIN_GROUP)
848 err = ip_mc_join_group(sk, &mreq);
849 else
850 err = ip_mc_leave_group(sk, &mreq);
851 break;
852 }
853 case MCAST_JOIN_SOURCE_GROUP:
854 case MCAST_LEAVE_SOURCE_GROUP:
855 case MCAST_BLOCK_SOURCE:
856 case MCAST_UNBLOCK_SOURCE:
857 {
858 struct group_source_req greqs;
859 struct ip_mreq_source mreqs;
860 struct sockaddr_in *psin;
861 int omode, add;
862
863 if (optlen != sizeof(struct group_source_req))
864 goto e_inval;
865 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
866 err = -EFAULT;
867 break;
868 }
869 if (greqs.gsr_group.ss_family != AF_INET ||
870 greqs.gsr_source.ss_family != AF_INET) {
871 err = -EADDRNOTAVAIL;
872 break;
873 }
874 psin = (struct sockaddr_in *)&greqs.gsr_group;
875 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
876 psin = (struct sockaddr_in *)&greqs.gsr_source;
877 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
878 mreqs.imr_interface = 0; /* use index for mc_source */
879
880 if (optname == MCAST_BLOCK_SOURCE) {
881 omode = MCAST_EXCLUDE;
882 add = 1;
883 } else if (optname == MCAST_UNBLOCK_SOURCE) {
884 omode = MCAST_EXCLUDE;
885 add = 0;
886 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
887 struct ip_mreqn mreq;
888
889 psin = (struct sockaddr_in *)&greqs.gsr_group;
890 mreq.imr_multiaddr = psin->sin_addr;
891 mreq.imr_address.s_addr = 0;
892 mreq.imr_ifindex = greqs.gsr_interface;
893 err = ip_mc_join_group(sk, &mreq);
894 if (err && err != -EADDRINUSE)
895 break;
896 greqs.gsr_interface = mreq.imr_ifindex;
897 omode = MCAST_INCLUDE;
898 add = 1;
899 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
900 omode = MCAST_INCLUDE;
901 add = 0;
902 }
903 err = ip_mc_source(add, omode, sk, &mreqs,
904 greqs.gsr_interface);
905 break;
906 }
907 case MCAST_MSFILTER:
908 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800909 struct sockaddr_in *psin;
910 struct ip_msfilter *msf = NULL;
911 struct group_filter *gsf = NULL;
912 int msize, i, ifindex;
913
914 if (optlen < GROUP_FILTER_SIZE(0))
915 goto e_inval;
916 if (optlen > sysctl_optmem_max) {
917 err = -ENOBUFS;
918 break;
919 }
Jianjun Kong09cb1052008-11-03 00:27:11 -0800920 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700921 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800922 err = -ENOBUFS;
923 break;
924 }
925 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700926 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800927 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700928
Stephen Hemminger132adf52007-03-08 20:44:43 -0800929 /* numsrc >= (4G-140)/128 overflow in 32 bits */
930 if (gsf->gf_numsrc >= 0x1ffffff ||
931 gsf->gf_numsrc > sysctl_igmp_max_msf) {
932 err = -ENOBUFS;
933 goto mc_msf_out;
934 }
935 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
936 err = -EINVAL;
937 goto mc_msf_out;
938 }
939 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800940 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700941 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800942 err = -ENOBUFS;
943 goto mc_msf_out;
944 }
945 ifindex = gsf->gf_interface;
946 psin = (struct sockaddr_in *)&gsf->gf_group;
947 if (psin->sin_family != AF_INET) {
948 err = -EADDRNOTAVAIL;
949 goto mc_msf_out;
950 }
951 msf->imsf_multiaddr = psin->sin_addr.s_addr;
952 msf->imsf_interface = 0;
953 msf->imsf_fmode = gsf->gf_fmode;
954 msf->imsf_numsrc = gsf->gf_numsrc;
955 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700956 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800957 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
958
959 if (psin->sin_family != AF_INET)
960 goto mc_msf_out;
961 msf->imsf_slist[i] = psin->sin_addr.s_addr;
962 }
963 kfree(gsf);
964 gsf = NULL;
965
966 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700967mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -0800968 kfree(msf);
969 kfree(gsf);
970 break;
971 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +0000972 case IP_MULTICAST_ALL:
973 if (optlen < 1)
974 goto e_inval;
975 if (val != 0 && val != 1)
976 goto e_inval;
977 inet->mc_all = val;
978 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800979 case IP_ROUTER_ALERT:
980 err = ip_ra_control(sk, val ? 1 : 0, NULL);
981 break;
982
983 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700984 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800985 goto e_inval;
986 inet->freebind = !!val;
987 break;
988
989 case IP_IPSEC_POLICY:
990 case IP_XFRM_POLICY:
991 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +0000992 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800993 break;
994 err = xfrm_user_policy(sk, optname, optval, optlen);
995 break;
996
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -0700997 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +0000998 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
999 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001000 err = -EPERM;
1001 break;
1002 }
1003 if (optlen < 1)
1004 goto e_inval;
1005 inet->transparent = !!val;
1006 break;
1007
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001008 case IP_MINTTL:
1009 if (optlen < 1)
1010 goto e_inval;
1011 if (val < 0 || val > 255)
1012 goto e_inval;
1013 inet->min_ttl = val;
1014 break;
1015
Stephen Hemminger132adf52007-03-08 20:44:43 -08001016 default:
1017 err = -ENOPROTOOPT;
1018 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020 release_sock(sk);
1021 return err;
1022
1023e_inval:
1024 release_sock(sk);
1025 return -EINVAL;
1026}
1027
Eric Dumazetf84af322010-04-28 15:31:51 -07001028/**
Eric Dumazetd826eb12011-11-09 07:24:35 +00001029 * ipv4_pktinfo_prepare - transfert some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001030 * @sk: socket
1031 * @skb: buffer
1032 *
David S. Miller35ebf652012-06-28 03:59:11 -07001033 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1034 * destination in skb->cb[] before dst drop.
Eric Dumazetd826eb12011-11-09 07:24:35 +00001035 * This way, receiver doesnt make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001036 */
Eric Dumazetd826eb12011-11-09 07:24:35 +00001037void ipv4_pktinfo_prepare(struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001038{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001039 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001040
David S. Miller92101b32012-07-23 16:29:00 -07001041 if (skb_rtable(skb)) {
1042 pktinfo->ipi_ifindex = inet_iif(skb);
David S. Miller35ebf652012-06-28 03:59:11 -07001043 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001044 } else {
1045 pktinfo->ipi_ifindex = 0;
1046 pktinfo->ipi_spec_dst.s_addr = 0;
1047 }
1048 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001049}
Eric Dumazetf84af322010-04-28 15:31:51 -07001050
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001051int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001052 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001053{
1054 int err;
1055
1056 if (level != SOL_IP)
1057 return -ENOPROTOOPT;
1058
1059 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1060#ifdef CONFIG_NETFILTER
1061 /* we need to exclude all possible ENOPROTOOPTs except default case */
1062 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001063 optname != IP_IPSEC_POLICY &&
1064 optname != IP_XFRM_POLICY &&
1065 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001066 lock_sock(sk);
1067 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1068 release_sock(sk);
1069 }
1070#endif
1071 return err;
1072}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001073EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001074
1075#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001076int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001077 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001078{
1079 int err;
1080
1081 if (level != SOL_IP)
1082 return -ENOPROTOOPT;
1083
David L Stevensdae50292008-04-27 01:06:07 -07001084 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1085 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1086 ip_setsockopt);
1087
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001088 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1089#ifdef CONFIG_NETFILTER
1090 /* we need to exclude all possible ENOPROTOOPTs except default case */
1091 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001092 optname != IP_IPSEC_POLICY &&
1093 optname != IP_XFRM_POLICY &&
1094 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001095 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001096 err = compat_nf_setsockopt(sk, PF_INET, optname,
1097 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001098 release_sock(sk);
1099 }
1100#endif
1101 return err;
1102}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001103EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001104#endif
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001107 * Get the options. Note for future reference. The GET of IP options gets
1108 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 */
1110
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001111static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001112 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 struct inet_sock *inet = inet_sk(sk);
1115 int val;
1116 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001117
Stephen Hemminger132adf52007-03-08 20:44:43 -08001118 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return -EOPNOTSUPP;
1120
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001121 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001122 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Jianjun Kong09cb1052008-11-03 00:27:11 -08001124 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001126 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 lock_sock(sk);
1130
Stephen Hemminger132adf52007-03-08 20:44:43 -08001131 switch (optname) {
1132 case IP_OPTIONS:
1133 {
1134 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001135 struct ip_options *opt = (struct ip_options *)optbuf;
1136 struct ip_options_rcu *inet_opt;
1137
1138 inet_opt = rcu_dereference_protected(inet->inet_opt,
1139 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001140 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001141 if (inet_opt)
1142 memcpy(optbuf, &inet_opt->opt,
1143 sizeof(struct ip_options) +
1144 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001145 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Stephen Hemminger132adf52007-03-08 20:44:43 -08001147 if (opt->optlen == 0)
1148 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Stephen Hemminger132adf52007-03-08 20:44:43 -08001150 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Stephen Hemminger132adf52007-03-08 20:44:43 -08001152 len = min_t(unsigned int, len, opt->optlen);
1153 if (put_user(len, optlen))
1154 return -EFAULT;
1155 if (copy_to_user(optval, opt->__data, len))
1156 return -EFAULT;
1157 return 0;
1158 }
1159 case IP_PKTINFO:
1160 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1161 break;
1162 case IP_RECVTTL:
1163 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1164 break;
1165 case IP_RECVTOS:
1166 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1167 break;
1168 case IP_RECVOPTS:
1169 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1170 break;
1171 case IP_RETOPTS:
1172 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1173 break;
1174 case IP_PASSSEC:
1175 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1176 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001177 case IP_RECVORIGDSTADDR:
1178 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1179 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001180 case IP_TOS:
1181 val = inet->tos;
1182 break;
1183 case IP_TTL:
1184 val = (inet->uc_ttl == -1 ?
1185 sysctl_ip_default_ttl :
1186 inet->uc_ttl);
1187 break;
1188 case IP_HDRINCL:
1189 val = inet->hdrincl;
1190 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001191 case IP_NODEFRAG:
1192 val = inet->nodefrag;
1193 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001194 case IP_MTU_DISCOVER:
1195 val = inet->pmtudisc;
1196 break;
1197 case IP_MTU:
1198 {
1199 struct dst_entry *dst;
1200 val = 0;
1201 dst = sk_dst_get(sk);
1202 if (dst) {
1203 val = dst_mtu(dst);
1204 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001206 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001208 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001210 break;
1211 }
1212 case IP_RECVERR:
1213 val = inet->recverr;
1214 break;
1215 case IP_MULTICAST_TTL:
1216 val = inet->mc_ttl;
1217 break;
1218 case IP_MULTICAST_LOOP:
1219 val = inet->mc_loop;
1220 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001221 case IP_UNICAST_IF:
1222 val = (__force int)htonl((__u32) inet->uc_index);
1223 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001224 case IP_MULTICAST_IF:
1225 {
1226 struct in_addr addr;
1227 len = min_t(unsigned int, len, sizeof(struct in_addr));
1228 addr.s_addr = inet->mc_addr;
1229 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Stephen Hemminger132adf52007-03-08 20:44:43 -08001231 if (put_user(len, optlen))
1232 return -EFAULT;
1233 if (copy_to_user(optval, &addr, len))
1234 return -EFAULT;
1235 return 0;
1236 }
1237 case IP_MSFILTER:
1238 {
1239 struct ip_msfilter msf;
1240 int err;
1241
1242 if (len < IP_MSFILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001244 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001246 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001248 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001250 err = ip_mc_msfget(sk, &msf,
1251 (struct ip_msfilter __user *)optval, optlen);
1252 release_sock(sk);
1253 return err;
1254 }
1255 case MCAST_MSFILTER:
1256 {
1257 struct group_filter gsf;
1258 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Stephen Hemminger132adf52007-03-08 20:44:43 -08001260 if (len < GROUP_FILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001262 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001264 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001266 return -EFAULT;
1267 }
1268 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001269 (struct group_filter __user *)optval,
1270 optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001271 release_sock(sk);
1272 return err;
1273 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001274 case IP_MULTICAST_ALL:
1275 val = inet->mc_all;
1276 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001277 case IP_PKTOPTIONS:
1278 {
1279 struct msghdr msg;
1280
1281 release_sock(sk);
1282
1283 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001285
1286 msg.msg_control = optval;
1287 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001288 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001289
1290 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1291 struct in_pktinfo info;
1292
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001293 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1294 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001295 info.ipi_ifindex = inet->mc_index;
1296 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1297 }
1298 if (inet->cmsg_flags & IP_CMSG_TTL) {
1299 int hlim = inet->mc_ttl;
1300 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1301 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001302 if (inet->cmsg_flags & IP_CMSG_TOS) {
1303 int tos = inet->rcv_tos;
1304 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1305 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001306 len -= msg.msg_controllen;
1307 return put_user(len, optlen);
1308 }
1309 case IP_FREEBIND:
1310 val = inet->freebind;
1311 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001312 case IP_TRANSPARENT:
1313 val = inet->transparent;
1314 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001315 case IP_MINTTL:
1316 val = inet->min_ttl;
1317 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001318 default:
1319 release_sock(sk);
1320 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001323
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001324 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 unsigned char ucval = (unsigned char)val;
1326 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001327 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001329 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return -EFAULT;
1331 } else {
1332 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001333 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001335 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return -EFAULT;
1337 }
1338 return 0;
1339}
1340
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001341int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001342 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001343{
1344 int err;
1345
Daniel Balutadd23198e2011-08-07 22:31:07 -07001346 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001347#ifdef CONFIG_NETFILTER
1348 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001349 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1350 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001351 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001352
Jianjun Kong09cb1052008-11-03 00:27:11 -08001353 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001354 return -EFAULT;
1355
1356 lock_sock(sk);
1357 err = nf_getsockopt(sk, PF_INET, optname, optval,
1358 &len);
1359 release_sock(sk);
1360 if (err >= 0)
1361 err = put_user(len, optlen);
1362 return err;
1363 }
1364#endif
1365 return err;
1366}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001367EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001368
1369#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001370int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1371 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001372{
David L Stevens42908c62008-04-29 03:23:22 -07001373 int err;
1374
1375 if (optname == MCAST_MSFILTER)
1376 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1377 ip_getsockopt);
1378
Daniel Balutadd23198e2011-08-07 22:31:07 -07001379 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1380 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001381
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001382#ifdef CONFIG_NETFILTER
1383 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001384 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1385 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001386 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001387
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001388 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001389 return -EFAULT;
1390
1391 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001392 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001393 release_sock(sk);
1394 if (err >= 0)
1395 err = put_user(len, optlen);
1396 return err;
1397 }
1398#endif
1399 return err;
1400}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001401EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001402#endif