blob: 8aa87c19fa008f00adbcf8e0fa1b4411d119288b [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
43
44#include <linux/errqueue.h>
45#include <asm/uaccess.h>
46
47#define IP_CMSG_PKTINFO 1
48#define IP_CMSG_TTL 2
49#define IP_CMSG_TOS 4
50#define IP_CMSG_RECVOPTS 8
51#define IP_CMSG_RETOPTS 16
Catherine Zhang2c7946a2006-03-20 22:41:23 -080052#define IP_CMSG_PASSSEC 32
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -080053#define IP_CMSG_ORIGDSTADDR 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * SOL_IP control messages.
57 */
Eric Dumazetd826eb12011-11-09 07:24:35 +000058#define PKTINFO_SKB_CB(__skb) ((struct in_pktinfo *)((__skb)->cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
61{
Eric Dumazetd826eb12011-11-09 07:24:35 +000062 struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070064 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
67}
68
69static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
70{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070071 int ttl = ip_hdr(skb)->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
73}
74
75static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
76{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070077 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
81{
82 if (IPCB(skb)->opt.optlen == 0)
83 return;
84
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070085 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
86 ip_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89
90static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
91{
92 unsigned char optbuf[sizeof(struct ip_options) + 40];
Jianjun Kong09cb1052008-11-03 00:27:11 -080093 struct ip_options * opt = (struct ip_options *)optbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 if (IPCB(skb)->opt.optlen == 0)
96 return;
97
98 if (ip_options_echo(opt, skb)) {
99 msg->msg_flags |= MSG_CTRUNC;
100 return;
101 }
102 ip_options_undo(opt);
103
104 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
105}
106
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800107static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
108{
109 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700110 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800111 int err;
112
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700113 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
114 if (err)
115 return;
116
117 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800118 if (err)
119 return;
120
121 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700122 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800123}
124
Harvey Harrison21d1a162008-11-20 01:54:27 -0800125static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800126{
127 struct sockaddr_in sin;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000128 const struct iphdr *iph = ip_hdr(skb);
Harvey Harrison21d1a162008-11-20 01:54:27 -0800129 __be16 *ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800130
131 if (skb_transport_offset(skb) + 4 > skb->len)
132 return;
133
134 /* All current transport protocols have the port numbers in the
135 * first four bytes of the transport header and this function is
136 * written with this assumption in mind.
137 */
138
139 sin.sin_family = AF_INET;
140 sin.sin_addr.s_addr = iph->daddr;
141 sin.sin_port = ports[1];
142 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
143
144 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
148{
149 struct inet_sock *inet = inet_sk(skb->sk);
150 unsigned flags = inet->cmsg_flags;
151
152 /* Ordered by supposed usage frequency */
153 if (flags & 1)
154 ip_cmsg_recv_pktinfo(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700155 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return;
157
158 if (flags & 1)
159 ip_cmsg_recv_ttl(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700160 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return;
162
163 if (flags & 1)
164 ip_cmsg_recv_tos(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700165 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return;
167
168 if (flags & 1)
169 ip_cmsg_recv_opts(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700170 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return;
172
173 if (flags & 1)
174 ip_cmsg_recv_retopts(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700175 if ((flags >>= 1) == 0)
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800176 return;
177
178 if (flags & 1)
179 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800180
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700181 if ((flags >>= 1) == 0)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800182 return;
183 if (flags & 1)
184 ip_cmsg_recv_dstaddr(msg, skb);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700187EXPORT_SYMBOL(ip_cmsg_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Denis V. Lunev7a6adb92008-03-24 15:30:27 -0700189int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 int err;
192 struct cmsghdr *cmsg;
193
194 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
195 if (!CMSG_OK(msg, cmsg))
196 return -EINVAL;
197 if (cmsg->cmsg_level != SOL_IP)
198 continue;
199 switch (cmsg->cmsg_type) {
200 case IP_RETOPTS:
201 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700202 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
203 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (err)
205 return err;
206 break;
207 case IP_PKTINFO:
208 {
209 struct in_pktinfo *info;
210 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
211 return -EINVAL;
212 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
213 ipc->oif = info->ipi_ifindex;
214 ipc->addr = info->ipi_spec_dst.s_addr;
215 break;
216 }
217 default:
218 return -EINVAL;
219 }
220 }
221 return 0;
222}
223
224
225/* Special input handler for packets caught by router alert option.
226 They are selected only by protocol field, and then processed likely
227 local ones; but only if someone wants them! Otherwise, router
228 not running rsvpd will kill RSVP.
229
230 It is user level problem, what it will make with them.
231 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
232 but receiver should be enough clever f.e. to forward mtrace requests,
233 sent to multicast group to reach destination designated router.
234 */
Eric Dumazet43a951e2010-10-25 03:32:44 +0000235struct ip_ra_chain __rcu *ip_ra_chain;
Eric Dumazet66018502010-06-07 03:12:08 +0000236static DEFINE_SPINLOCK(ip_ra_lock);
237
Eric Dumazet592fcb92010-06-09 16:21:07 +0000238
239static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000240{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000241 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
242
243 sock_put(ra->saved_sk);
244 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700247int ip_ra_control(struct sock *sk, unsigned char on,
248 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000250 struct ip_ra_chain *ra, *new_ra;
251 struct ip_ra_chain __rcu **rap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000253 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return -EINVAL;
255
256 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
257
Eric Dumazet66018502010-06-07 03:12:08 +0000258 spin_lock_bh(&ip_ra_lock);
Eric Dumazet43a951e2010-10-25 03:32:44 +0000259 for (rap = &ip_ra_chain;
260 (ra = rcu_dereference_protected(*rap,
261 lockdep_is_held(&ip_ra_lock))) != NULL;
262 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (ra->sk == sk) {
264 if (on) {
Eric Dumazet66018502010-06-07 03:12:08 +0000265 spin_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800266 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return -EADDRINUSE;
268 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000269 /* dont let ip_call_ra_chain() use sk again */
270 ra->sk = NULL;
Eric Dumazet66018502010-06-07 03:12:08 +0000271 rcu_assign_pointer(*rap, ra->next);
272 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if (ra->destructor)
275 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000276 /*
277 * Delay sock_put(sk) and kfree(ra) after one rcu grace
278 * period. This guarantee ip_call_ra_chain() dont need
279 * to mess with socket refcounts.
280 */
281 ra->saved_sk = sk;
282 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284 }
285 }
286 if (new_ra == NULL) {
Eric Dumazet66018502010-06-07 03:12:08 +0000287 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return -ENOBUFS;
289 }
290 new_ra->sk = sk;
291 new_ra->destructor = destructor;
292
293 new_ra->next = ra;
Eric Dumazet66018502010-06-07 03:12:08 +0000294 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 sock_hold(sk);
Eric Dumazet66018502010-06-07 03:12:08 +0000296 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 return 0;
299}
300
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900301void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700302 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct sock_exterr_skb *serr;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 skb = skb_clone(skb, GFP_ATOMIC);
307 if (!skb)
308 return;
309
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900310 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 serr->ee.ee_errno = err;
312 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300313 serr->ee.ee_type = icmp_hdr(skb)->type;
314 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 serr->ee.ee_pad = 0;
316 serr->ee.ee_info = info;
317 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300318 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700319 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 serr->port = port;
321
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300322 if (skb_pull(skb, payload - skb->data) != NULL) {
323 skb_reset_transport_header(skb);
324 if (sock_queue_err_skb(sk, skb) == 0)
325 return;
326 }
327 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Al Viro05790162006-09-27 18:33:40 -0700330void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct inet_sock *inet = inet_sk(sk);
333 struct sock_exterr_skb *serr;
334 struct iphdr *iph;
335 struct sk_buff *skb;
336
337 if (!inet->recverr)
338 return;
339
340 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
341 if (!skb)
342 return;
343
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300344 skb_put(skb, sizeof(struct iphdr));
345 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700346 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 iph->daddr = daddr;
348
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900349 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 serr->ee.ee_errno = err;
351 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900352 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 serr->ee.ee_code = 0;
354 serr->ee.ee_pad = 0;
355 serr->ee.ee_info = info;
356 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700357 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 serr->port = port;
359
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700360 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300361 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (sock_queue_err_skb(sk, skb))
364 kfree_skb(skb);
365}
366
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900367/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * Handle MSG_ERRQUEUE
369 */
370int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
371{
372 struct sock_exterr_skb *serr;
373 struct sk_buff *skb, *skb2;
374 struct sockaddr_in *sin;
375 struct {
376 struct sock_extended_err ee;
377 struct sockaddr_in offender;
378 } errhdr;
379 int err;
380 int copied;
381
382 err = -EAGAIN;
383 skb = skb_dequeue(&sk->sk_error_queue);
384 if (skb == NULL)
385 goto out;
386
387 copied = skb->len;
388 if (copied > len) {
389 msg->msg_flags |= MSG_TRUNC;
390 copied = len;
391 }
392 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
393 if (err)
394 goto out_free_skb;
395
396 sock_recv_timestamp(msg, sk, skb);
397
398 serr = SKB_EXT_ERR(skb);
399
400 sin = (struct sockaddr_in *)msg->msg_name;
401 if (sin) {
402 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700403 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
404 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 sin->sin_port = serr->port;
406 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
407 }
408
409 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
410 sin = &errhdr.offender;
411 sin->sin_family = AF_UNSPEC;
412 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
413 struct inet_sock *inet = inet_sk(sk);
414
415 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700416 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 sin->sin_port = 0;
418 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
419 if (inet->cmsg_flags)
420 ip_cmsg_recv(msg, skb);
421 }
422
423 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
424
425 /* Now we could try to dump offended packet options */
426
427 msg->msg_flags |= MSG_ERRQUEUE;
428 err = copied;
429
430 /* Reset and regenerate socket error */
Herbert Xue0f9f852005-06-18 22:56:18 -0700431 spin_lock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 sk->sk_err = 0;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700433 skb2 = skb_peek(&sk->sk_error_queue);
434 if (skb2 != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
Herbert Xue0f9f852005-06-18 22:56:18 -0700436 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 sk->sk_error_report(sk);
438 } else
Herbert Xue0f9f852005-06-18 22:56:18 -0700439 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900441out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 kfree_skb(skb);
443out:
444 return err;
445}
446
447
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000448static void opt_kfree_rcu(struct rcu_head *head)
449{
450 kfree(container_of(head, struct ip_options_rcu, rcu));
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700454 * Socket option code for IP. This is the end of the line after any
455 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
457
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800458static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700459 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct inet_sock *inet = inet_sk(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800462 int val = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900464 if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
Stephen Hemminger132adf52007-03-08 20:44:43 -0800465 (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
466 (1<<IP_RETOPTS) | (1<<IP_TOS) |
467 (1<<IP_TTL) | (1<<IP_HDRINCL) |
468 (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
469 (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
Stephen Hemmingerd218d112010-01-11 16:28:01 -0800470 (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT) |
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000471 (1<<IP_MINTTL) | (1<<IP_NODEFRAG))) ||
Stephen Hemminger132adf52007-03-08 20:44:43 -0800472 optname == IP_MULTICAST_TTL ||
Nivedita Singhvif771bef2009-05-28 07:00:46 +0000473 optname == IP_MULTICAST_ALL ||
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800474 optname == IP_MULTICAST_LOOP ||
475 optname == IP_RECVORIGDSTADDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (optlen >= sizeof(int)) {
477 if (get_user(val, (int __user *) optval))
478 return -EFAULT;
479 } else if (optlen >= sizeof(char)) {
480 unsigned char ucval;
481
482 if (get_user(ucval, (unsigned char __user *) optval))
483 return -EFAULT;
484 val = (int) ucval;
485 }
486 }
487
488 /* If optlen==0, it is equivalent to val == 0 */
489
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800490 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800491 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 err = 0;
494 lock_sock(sk);
495
496 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800497 case IP_OPTIONS:
498 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000499 struct ip_options_rcu *old, *opt = NULL;
500
roel kluin65a1c4f2009-10-23 05:59:21 +0000501 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800502 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900503 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700504 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800505 if (err)
506 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000507 old = rcu_dereference_protected(inet->inet_opt,
508 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800509 if (inet->is_icsk) {
510 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000511#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800512 if (sk->sk_family == PF_INET ||
513 (!((1 << sk->sk_state) &
514 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000515 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000517 if (old)
518 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800519 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000520 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800521 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000522#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000526 rcu_assign_pointer(inet->inet_opt, opt);
527 if (old)
528 call_rcu(&old->rcu, opt_kfree_rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800529 break;
530 }
531 case IP_PKTINFO:
532 if (val)
533 inet->cmsg_flags |= IP_CMSG_PKTINFO;
534 else
535 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
536 break;
537 case IP_RECVTTL:
538 if (val)
539 inet->cmsg_flags |= IP_CMSG_TTL;
540 else
541 inet->cmsg_flags &= ~IP_CMSG_TTL;
542 break;
543 case IP_RECVTOS:
544 if (val)
545 inet->cmsg_flags |= IP_CMSG_TOS;
546 else
547 inet->cmsg_flags &= ~IP_CMSG_TOS;
548 break;
549 case IP_RECVOPTS:
550 if (val)
551 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
552 else
553 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
554 break;
555 case IP_RETOPTS:
556 if (val)
557 inet->cmsg_flags |= IP_CMSG_RETOPTS;
558 else
559 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
560 break;
561 case IP_PASSSEC:
562 if (val)
563 inet->cmsg_flags |= IP_CMSG_PASSSEC;
564 else
565 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
566 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800567 case IP_RECVORIGDSTADDR:
568 if (val)
569 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
570 else
571 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
572 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800573 case IP_TOS: /* This sets both TOS and Precedence */
574 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400575 val &= ~INET_ECN_MASK;
576 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800578 if (inet->tos != val) {
579 inet->tos = val;
580 sk->sk_priority = rt_tos2priority(val);
581 sk_dst_reset(sk);
582 }
583 break;
584 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700585 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800586 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700587 if (val != -1 && (val < 0 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800588 goto e_inval;
589 inet->uc_ttl = val;
590 break;
591 case IP_HDRINCL:
592 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800595 }
596 inet->hdrincl = val ? 1 : 0;
597 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000598 case IP_NODEFRAG:
599 if (sk->sk_type != SOCK_RAW) {
600 err = -ENOPROTOOPT;
601 break;
602 }
603 inet->nodefrag = val ? 1 : 0;
604 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800605 case IP_MTU_DISCOVER:
John Dykstra0eae7502009-10-19 21:53:53 -0700606 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800607 goto e_inval;
608 inet->pmtudisc = val;
609 break;
610 case IP_RECVERR:
611 inet->recverr = !!val;
612 if (!val)
613 skb_queue_purge(&sk->sk_error_queue);
614 break;
615 case IP_MULTICAST_TTL:
616 if (sk->sk_type == SOCK_STREAM)
617 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700618 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800619 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800620 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800621 val = 1;
622 if (val < 0 || val > 255)
623 goto e_inval;
624 inet->mc_ttl = val;
625 break;
626 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700627 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800628 goto e_inval;
629 inet->mc_loop = !!val;
630 break;
631 case IP_MULTICAST_IF:
632 {
633 struct ip_mreqn mreq;
634 struct net_device *dev = NULL;
635
636 if (sk->sk_type == SOCK_STREAM)
637 goto e_inval;
638 /*
639 * Check the arguments are allowable
640 */
641
Shan Wei09159212009-09-22 15:41:10 +0000642 if (optlen < sizeof(struct in_addr))
643 goto e_inval;
644
Stephen Hemminger132adf52007-03-08 20:44:43 -0800645 err = -EFAULT;
646 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800647 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800648 break;
649 } else {
650 memset(&mreq, 0, sizeof(mreq));
651 if (optlen >= sizeof(struct in_addr) &&
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700652 copy_from_user(&mreq.imr_address, optval,
653 sizeof(struct in_addr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800654 break;
655 }
656
657 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700658 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800659 inet->mc_index = 0;
660 inet->mc_addr = 0;
661 err = 0;
662 break;
663 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900664 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000665 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800666 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800667 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000668 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800669
670
671 err = -EADDRNOTAVAIL;
672 if (!dev)
673 break;
Eric Dumazet55b80502009-10-19 06:41:58 +0000674 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800675
676 err = -EINVAL;
677 if (sk->sk_bound_dev_if &&
678 mreq.imr_ifindex != sk->sk_bound_dev_if)
679 break;
680
681 inet->mc_index = mreq.imr_ifindex;
682 inet->mc_addr = mreq.imr_address.s_addr;
683 err = 0;
684 break;
685 }
686
687 case IP_ADD_MEMBERSHIP:
688 case IP_DROP_MEMBERSHIP:
689 {
690 struct ip_mreqn mreq;
691
Flavio Leitnera96fb492007-08-24 22:16:39 -0700692 err = -EPROTO;
693 if (inet_sk(sk)->is_icsk)
694 break;
695
Stephen Hemminger132adf52007-03-08 20:44:43 -0800696 if (optlen < sizeof(struct ip_mreq))
697 goto e_inval;
698 err = -EFAULT;
699 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800700 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800701 break;
702 } else {
703 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800704 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800705 break;
706 }
707
708 if (optname == IP_ADD_MEMBERSHIP)
709 err = ip_mc_join_group(sk, &mreq);
710 else
711 err = ip_mc_leave_group(sk, &mreq);
712 break;
713 }
714 case IP_MSFILTER:
715 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800716 struct ip_msfilter *msf;
717
718 if (optlen < IP_MSFILTER_SIZE(0))
719 goto e_inval;
720 if (optlen > sysctl_optmem_max) {
721 err = -ENOBUFS;
722 break;
723 }
724 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700725 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800726 err = -ENOBUFS;
727 break;
728 }
729 err = -EFAULT;
730 if (copy_from_user(msf, optval, optlen)) {
731 kfree(msf);
732 break;
733 }
734 /* numsrc >= (1G-4) overflow in 32 bits */
735 if (msf->imsf_numsrc >= 0x3ffffffcU ||
736 msf->imsf_numsrc > sysctl_igmp_max_msf) {
737 kfree(msf);
738 err = -ENOBUFS;
739 break;
740 }
741 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
742 kfree(msf);
743 err = -EINVAL;
744 break;
745 }
746 err = ip_mc_msfilter(sk, msf, 0);
747 kfree(msf);
748 break;
749 }
750 case IP_BLOCK_SOURCE:
751 case IP_UNBLOCK_SOURCE:
752 case IP_ADD_SOURCE_MEMBERSHIP:
753 case IP_DROP_SOURCE_MEMBERSHIP:
754 {
755 struct ip_mreq_source mreqs;
756 int omode, add;
757
758 if (optlen != sizeof(struct ip_mreq_source))
759 goto e_inval;
760 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
761 err = -EFAULT;
762 break;
763 }
764 if (optname == IP_BLOCK_SOURCE) {
765 omode = MCAST_EXCLUDE;
766 add = 1;
767 } else if (optname == IP_UNBLOCK_SOURCE) {
768 omode = MCAST_EXCLUDE;
769 add = 0;
770 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
771 struct ip_mreqn mreq;
772
773 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
774 mreq.imr_address.s_addr = mreqs.imr_interface;
775 mreq.imr_ifindex = 0;
776 err = ip_mc_join_group(sk, &mreq);
777 if (err && err != -EADDRINUSE)
778 break;
779 omode = MCAST_INCLUDE;
780 add = 1;
781 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
782 omode = MCAST_INCLUDE;
783 add = 0;
784 }
785 err = ip_mc_source(add, omode, sk, &mreqs, 0);
786 break;
787 }
788 case MCAST_JOIN_GROUP:
789 case MCAST_LEAVE_GROUP:
790 {
791 struct group_req greq;
792 struct sockaddr_in *psin;
793 struct ip_mreqn mreq;
794
795 if (optlen < sizeof(struct group_req))
796 goto e_inval;
797 err = -EFAULT;
798 if (copy_from_user(&greq, optval, sizeof(greq)))
799 break;
800 psin = (struct sockaddr_in *)&greq.gr_group;
801 if (psin->sin_family != AF_INET)
802 goto e_inval;
803 memset(&mreq, 0, sizeof(mreq));
804 mreq.imr_multiaddr = psin->sin_addr;
805 mreq.imr_ifindex = greq.gr_interface;
806
807 if (optname == MCAST_JOIN_GROUP)
808 err = ip_mc_join_group(sk, &mreq);
809 else
810 err = ip_mc_leave_group(sk, &mreq);
811 break;
812 }
813 case MCAST_JOIN_SOURCE_GROUP:
814 case MCAST_LEAVE_SOURCE_GROUP:
815 case MCAST_BLOCK_SOURCE:
816 case MCAST_UNBLOCK_SOURCE:
817 {
818 struct group_source_req greqs;
819 struct ip_mreq_source mreqs;
820 struct sockaddr_in *psin;
821 int omode, add;
822
823 if (optlen != sizeof(struct group_source_req))
824 goto e_inval;
825 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
826 err = -EFAULT;
827 break;
828 }
829 if (greqs.gsr_group.ss_family != AF_INET ||
830 greqs.gsr_source.ss_family != AF_INET) {
831 err = -EADDRNOTAVAIL;
832 break;
833 }
834 psin = (struct sockaddr_in *)&greqs.gsr_group;
835 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
836 psin = (struct sockaddr_in *)&greqs.gsr_source;
837 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
838 mreqs.imr_interface = 0; /* use index for mc_source */
839
840 if (optname == MCAST_BLOCK_SOURCE) {
841 omode = MCAST_EXCLUDE;
842 add = 1;
843 } else if (optname == MCAST_UNBLOCK_SOURCE) {
844 omode = MCAST_EXCLUDE;
845 add = 0;
846 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
847 struct ip_mreqn mreq;
848
849 psin = (struct sockaddr_in *)&greqs.gsr_group;
850 mreq.imr_multiaddr = psin->sin_addr;
851 mreq.imr_address.s_addr = 0;
852 mreq.imr_ifindex = greqs.gsr_interface;
853 err = ip_mc_join_group(sk, &mreq);
854 if (err && err != -EADDRINUSE)
855 break;
856 greqs.gsr_interface = mreq.imr_ifindex;
857 omode = MCAST_INCLUDE;
858 add = 1;
859 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
860 omode = MCAST_INCLUDE;
861 add = 0;
862 }
863 err = ip_mc_source(add, omode, sk, &mreqs,
864 greqs.gsr_interface);
865 break;
866 }
867 case MCAST_MSFILTER:
868 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800869 struct sockaddr_in *psin;
870 struct ip_msfilter *msf = NULL;
871 struct group_filter *gsf = NULL;
872 int msize, i, ifindex;
873
874 if (optlen < GROUP_FILTER_SIZE(0))
875 goto e_inval;
876 if (optlen > sysctl_optmem_max) {
877 err = -ENOBUFS;
878 break;
879 }
Jianjun Kong09cb1052008-11-03 00:27:11 -0800880 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700881 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800882 err = -ENOBUFS;
883 break;
884 }
885 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700886 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800887 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700888
Stephen Hemminger132adf52007-03-08 20:44:43 -0800889 /* numsrc >= (4G-140)/128 overflow in 32 bits */
890 if (gsf->gf_numsrc >= 0x1ffffff ||
891 gsf->gf_numsrc > sysctl_igmp_max_msf) {
892 err = -ENOBUFS;
893 goto mc_msf_out;
894 }
895 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
896 err = -EINVAL;
897 goto mc_msf_out;
898 }
899 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800900 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700901 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800902 err = -ENOBUFS;
903 goto mc_msf_out;
904 }
905 ifindex = gsf->gf_interface;
906 psin = (struct sockaddr_in *)&gsf->gf_group;
907 if (psin->sin_family != AF_INET) {
908 err = -EADDRNOTAVAIL;
909 goto mc_msf_out;
910 }
911 msf->imsf_multiaddr = psin->sin_addr.s_addr;
912 msf->imsf_interface = 0;
913 msf->imsf_fmode = gsf->gf_fmode;
914 msf->imsf_numsrc = gsf->gf_numsrc;
915 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700916 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800917 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
918
919 if (psin->sin_family != AF_INET)
920 goto mc_msf_out;
921 msf->imsf_slist[i] = psin->sin_addr.s_addr;
922 }
923 kfree(gsf);
924 gsf = NULL;
925
926 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700927mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -0800928 kfree(msf);
929 kfree(gsf);
930 break;
931 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +0000932 case IP_MULTICAST_ALL:
933 if (optlen < 1)
934 goto e_inval;
935 if (val != 0 && val != 1)
936 goto e_inval;
937 inet->mc_all = val;
938 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800939 case IP_ROUTER_ALERT:
940 err = ip_ra_control(sk, val ? 1 : 0, NULL);
941 break;
942
943 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700944 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800945 goto e_inval;
946 inet->freebind = !!val;
947 break;
948
949 case IP_IPSEC_POLICY:
950 case IP_XFRM_POLICY:
951 err = -EPERM;
952 if (!capable(CAP_NET_ADMIN))
953 break;
954 err = xfrm_user_policy(sk, optname, optval, optlen);
955 break;
956
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -0700957 case IP_TRANSPARENT:
Maciej Żenczykowski6cc7a762011-10-20 18:21:36 -0400958 if (!!val && !capable(CAP_NET_RAW) && !capable(CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -0700959 err = -EPERM;
960 break;
961 }
962 if (optlen < 1)
963 goto e_inval;
964 inet->transparent = !!val;
965 break;
966
Stephen Hemmingerd218d112010-01-11 16:28:01 -0800967 case IP_MINTTL:
968 if (optlen < 1)
969 goto e_inval;
970 if (val < 0 || val > 255)
971 goto e_inval;
972 inet->min_ttl = val;
973 break;
974
Stephen Hemminger132adf52007-03-08 20:44:43 -0800975 default:
976 err = -ENOPROTOOPT;
977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979 release_sock(sk);
980 return err;
981
982e_inval:
983 release_sock(sk);
984 return -EINVAL;
985}
986
Eric Dumazetf84af322010-04-28 15:31:51 -0700987/**
Eric Dumazetd826eb12011-11-09 07:24:35 +0000988 * ipv4_pktinfo_prepare - transfert some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -0700989 * @sk: socket
990 * @skb: buffer
991 *
Eric Dumazetd826eb12011-11-09 07:24:35 +0000992 * To support IP_CMSG_PKTINFO option, we store rt_iif and rt_spec_dst
993 * in skb->cb[] before dst drop.
994 * This way, receiver doesnt make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -0700995 */
Eric Dumazetd826eb12011-11-09 07:24:35 +0000996void ipv4_pktinfo_prepare(struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -0700997{
Eric Dumazetd826eb12011-11-09 07:24:35 +0000998 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
999 const struct rtable *rt = skb_rtable(skb);
1000
1001 if (rt) {
1002 pktinfo->ipi_ifindex = rt->rt_iif;
1003 pktinfo->ipi_spec_dst.s_addr = rt->rt_spec_dst;
1004 } else {
1005 pktinfo->ipi_ifindex = 0;
1006 pktinfo->ipi_spec_dst.s_addr = 0;
1007 }
1008 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001009}
Eric Dumazetf84af322010-04-28 15:31:51 -07001010
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001011int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001012 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001013{
1014 int err;
1015
1016 if (level != SOL_IP)
1017 return -ENOPROTOOPT;
1018
1019 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1020#ifdef CONFIG_NETFILTER
1021 /* we need to exclude all possible ENOPROTOOPTs except default case */
1022 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001023 optname != IP_IPSEC_POLICY &&
1024 optname != IP_XFRM_POLICY &&
1025 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001026 lock_sock(sk);
1027 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1028 release_sock(sk);
1029 }
1030#endif
1031 return err;
1032}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001033EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001034
1035#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001036int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001037 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001038{
1039 int err;
1040
1041 if (level != SOL_IP)
1042 return -ENOPROTOOPT;
1043
David L Stevensdae50292008-04-27 01:06:07 -07001044 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1045 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1046 ip_setsockopt);
1047
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001048 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1049#ifdef CONFIG_NETFILTER
1050 /* we need to exclude all possible ENOPROTOOPTs except default case */
1051 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001052 optname != IP_IPSEC_POLICY &&
1053 optname != IP_XFRM_POLICY &&
1054 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001055 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001056 err = compat_nf_setsockopt(sk, PF_INET, optname,
1057 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001058 release_sock(sk);
1059 }
1060#endif
1061 return err;
1062}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001063EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001064#endif
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001067 * Get the options. Note for future reference. The GET of IP options gets
1068 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
1070
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001071static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Daniel Balutadd23198e2011-08-07 22:31:07 -07001072 char __user *optval, int __user *optlen, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 struct inet_sock *inet = inet_sk(sk);
1075 int val;
1076 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001077
Stephen Hemminger132adf52007-03-08 20:44:43 -08001078 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return -EOPNOTSUPP;
1080
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001081 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001082 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Jianjun Kong09cb1052008-11-03 00:27:11 -08001084 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001086 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 lock_sock(sk);
1090
Stephen Hemminger132adf52007-03-08 20:44:43 -08001091 switch (optname) {
1092 case IP_OPTIONS:
1093 {
1094 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001095 struct ip_options *opt = (struct ip_options *)optbuf;
1096 struct ip_options_rcu *inet_opt;
1097
1098 inet_opt = rcu_dereference_protected(inet->inet_opt,
1099 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001100 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001101 if (inet_opt)
1102 memcpy(optbuf, &inet_opt->opt,
1103 sizeof(struct ip_options) +
1104 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001105 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Stephen Hemminger132adf52007-03-08 20:44:43 -08001107 if (opt->optlen == 0)
1108 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Stephen Hemminger132adf52007-03-08 20:44:43 -08001110 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Stephen Hemminger132adf52007-03-08 20:44:43 -08001112 len = min_t(unsigned int, len, opt->optlen);
1113 if (put_user(len, optlen))
1114 return -EFAULT;
1115 if (copy_to_user(optval, opt->__data, len))
1116 return -EFAULT;
1117 return 0;
1118 }
1119 case IP_PKTINFO:
1120 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1121 break;
1122 case IP_RECVTTL:
1123 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1124 break;
1125 case IP_RECVTOS:
1126 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1127 break;
1128 case IP_RECVOPTS:
1129 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1130 break;
1131 case IP_RETOPTS:
1132 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1133 break;
1134 case IP_PASSSEC:
1135 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1136 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001137 case IP_RECVORIGDSTADDR:
1138 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1139 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001140 case IP_TOS:
1141 val = inet->tos;
1142 break;
1143 case IP_TTL:
1144 val = (inet->uc_ttl == -1 ?
1145 sysctl_ip_default_ttl :
1146 inet->uc_ttl);
1147 break;
1148 case IP_HDRINCL:
1149 val = inet->hdrincl;
1150 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001151 case IP_NODEFRAG:
1152 val = inet->nodefrag;
1153 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001154 case IP_MTU_DISCOVER:
1155 val = inet->pmtudisc;
1156 break;
1157 case IP_MTU:
1158 {
1159 struct dst_entry *dst;
1160 val = 0;
1161 dst = sk_dst_get(sk);
1162 if (dst) {
1163 val = dst_mtu(dst);
1164 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001166 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001168 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001170 break;
1171 }
1172 case IP_RECVERR:
1173 val = inet->recverr;
1174 break;
1175 case IP_MULTICAST_TTL:
1176 val = inet->mc_ttl;
1177 break;
1178 case IP_MULTICAST_LOOP:
1179 val = inet->mc_loop;
1180 break;
1181 case IP_MULTICAST_IF:
1182 {
1183 struct in_addr addr;
1184 len = min_t(unsigned int, len, sizeof(struct in_addr));
1185 addr.s_addr = inet->mc_addr;
1186 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Stephen Hemminger132adf52007-03-08 20:44:43 -08001188 if (put_user(len, optlen))
1189 return -EFAULT;
1190 if (copy_to_user(optval, &addr, len))
1191 return -EFAULT;
1192 return 0;
1193 }
1194 case IP_MSFILTER:
1195 {
1196 struct ip_msfilter msf;
1197 int err;
1198
1199 if (len < IP_MSFILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001201 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001203 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001205 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001207 err = ip_mc_msfget(sk, &msf,
1208 (struct ip_msfilter __user *)optval, optlen);
1209 release_sock(sk);
1210 return err;
1211 }
1212 case MCAST_MSFILTER:
1213 {
1214 struct group_filter gsf;
1215 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Stephen Hemminger132adf52007-03-08 20:44:43 -08001217 if (len < GROUP_FILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001219 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001221 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001223 return -EFAULT;
1224 }
1225 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001226 (struct group_filter __user *)optval,
1227 optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001228 release_sock(sk);
1229 return err;
1230 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001231 case IP_MULTICAST_ALL:
1232 val = inet->mc_all;
1233 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001234 case IP_PKTOPTIONS:
1235 {
1236 struct msghdr msg;
1237
1238 release_sock(sk);
1239
1240 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001242
1243 msg.msg_control = optval;
1244 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001245 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001246
1247 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1248 struct in_pktinfo info;
1249
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001250 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1251 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001252 info.ipi_ifindex = inet->mc_index;
1253 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1254 }
1255 if (inet->cmsg_flags & IP_CMSG_TTL) {
1256 int hlim = inet->mc_ttl;
1257 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1258 }
1259 len -= msg.msg_controllen;
1260 return put_user(len, optlen);
1261 }
1262 case IP_FREEBIND:
1263 val = inet->freebind;
1264 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001265 case IP_TRANSPARENT:
1266 val = inet->transparent;
1267 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001268 case IP_MINTTL:
1269 val = inet->min_ttl;
1270 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001271 default:
1272 release_sock(sk);
1273 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001276
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001277 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 unsigned char ucval = (unsigned char)val;
1279 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001280 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001282 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return -EFAULT;
1284 } else {
1285 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001286 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001288 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return -EFAULT;
1290 }
1291 return 0;
1292}
1293
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001294int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001295 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001296{
1297 int err;
1298
Daniel Balutadd23198e2011-08-07 22:31:07 -07001299 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001300#ifdef CONFIG_NETFILTER
1301 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001302 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1303 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001304 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001305
Jianjun Kong09cb1052008-11-03 00:27:11 -08001306 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001307 return -EFAULT;
1308
1309 lock_sock(sk);
1310 err = nf_getsockopt(sk, PF_INET, optname, optval,
1311 &len);
1312 release_sock(sk);
1313 if (err >= 0)
1314 err = put_user(len, optlen);
1315 return err;
1316 }
1317#endif
1318 return err;
1319}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001320EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001321
1322#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001323int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1324 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001325{
David L Stevens42908c62008-04-29 03:23:22 -07001326 int err;
1327
1328 if (optname == MCAST_MSFILTER)
1329 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1330 ip_getsockopt);
1331
Daniel Balutadd23198e2011-08-07 22:31:07 -07001332 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1333 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001334
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001335#ifdef CONFIG_NETFILTER
1336 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001337 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1338 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001339 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001340
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001341 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001342 return -EFAULT;
1343
1344 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001345 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001346 release_sock(sk);
1347 if (err >= 0)
1348 err = put_user(len, optlen);
1349 return err;
1350 }
1351#endif
1352 return err;
1353}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001354EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001355#endif