blob: fcb35cd5ccfd0d9346227c32a3afe22b87422d3b [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 * Version: $Id: ip_sockglue.c,v 1.62 2002/02/01 22:01:04 davem Exp $
9 *
10 * Authors: see ip.c
11 *
12 * Fixes:
13 * Many : Split from ip.c , see ip.c for history.
14 * Martin Mares : TOS setting fixed.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090015 * Alan Cox : Fixed a couple of oopses in Martin's
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * TOS tweaks.
17 * Mike McLagan : Routing by source
18 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/skbuff.h>
24#include <linux/ip.h>
25#include <linux/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020026#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/netdevice.h>
28#include <net/sock.h>
29#include <net/ip.h>
30#include <net/icmp.h>
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -080031#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/udp.h>
33#include <linux/igmp.h>
34#include <linux/netfilter.h>
35#include <linux/route.h>
36#include <linux/mroute.h>
37#include <net/route.h>
38#include <net/xfrm.h>
39#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
40#include <net/transp_v6.h>
41#endif
42
43#include <linux/errqueue.h>
44#include <asm/uaccess.h>
45
46#define IP_CMSG_PKTINFO 1
47#define IP_CMSG_TTL 2
48#define IP_CMSG_TOS 4
49#define IP_CMSG_RECVOPTS 8
50#define IP_CMSG_RETOPTS 16
Catherine Zhang2c7946a2006-03-20 22:41:23 -080051#define IP_CMSG_PASSSEC 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*
54 * SOL_IP control messages.
55 */
56
57static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
58{
59 struct in_pktinfo info;
60 struct rtable *rt = (struct rtable *)skb->dst;
61
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070062 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (rt) {
64 info.ipi_ifindex = rt->rt_iif;
65 info.ipi_spec_dst.s_addr = rt->rt_spec_dst;
66 } else {
67 info.ipi_ifindex = 0;
68 info.ipi_spec_dst.s_addr = 0;
69 }
70
71 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
72}
73
74static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
75{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070076 int ttl = ip_hdr(skb)->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
78}
79
80static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
81{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070082 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
86{
87 if (IPCB(skb)->opt.optlen == 0)
88 return;
89
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070090 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
91 ip_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94
95static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
96{
97 unsigned char optbuf[sizeof(struct ip_options) + 40];
98 struct ip_options * opt = (struct ip_options*)optbuf;
99
100 if (IPCB(skb)->opt.optlen == 0)
101 return;
102
103 if (ip_options_echo(opt, skb)) {
104 msg->msg_flags |= MSG_CTRUNC;
105 return;
106 }
107 ip_options_undo(opt);
108
109 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
110}
111
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800112static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
113{
114 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700115 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800116 int err;
117
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700118 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
119 if (err)
120 return;
121
122 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800123 if (err)
124 return;
125
126 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700127 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
132{
133 struct inet_sock *inet = inet_sk(skb->sk);
134 unsigned flags = inet->cmsg_flags;
135
136 /* Ordered by supposed usage frequency */
137 if (flags & 1)
138 ip_cmsg_recv_pktinfo(msg, skb);
139 if ((flags>>=1) == 0)
140 return;
141
142 if (flags & 1)
143 ip_cmsg_recv_ttl(msg, skb);
144 if ((flags>>=1) == 0)
145 return;
146
147 if (flags & 1)
148 ip_cmsg_recv_tos(msg, skb);
149 if ((flags>>=1) == 0)
150 return;
151
152 if (flags & 1)
153 ip_cmsg_recv_opts(msg, skb);
154 if ((flags>>=1) == 0)
155 return;
156
157 if (flags & 1)
158 ip_cmsg_recv_retopts(msg, skb);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800159 if ((flags>>=1) == 0)
160 return;
161
162 if (flags & 1)
163 ip_cmsg_recv_security(msg, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
166int ip_cmsg_send(struct msghdr *msg, struct ipcm_cookie *ipc)
167{
168 int err;
169 struct cmsghdr *cmsg;
170
171 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
172 if (!CMSG_OK(msg, cmsg))
173 return -EINVAL;
174 if (cmsg->cmsg_level != SOL_IP)
175 continue;
176 switch (cmsg->cmsg_type) {
177 case IP_RETOPTS:
178 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
Arnaldo Carvalho de Melo4c6ea292005-08-16 19:46:48 -0300179 err = ip_options_get(&ipc->opt, CMSG_DATA(cmsg), err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (err)
181 return err;
182 break;
183 case IP_PKTINFO:
184 {
185 struct in_pktinfo *info;
186 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
187 return -EINVAL;
188 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
189 ipc->oif = info->ipi_ifindex;
190 ipc->addr = info->ipi_spec_dst.s_addr;
191 break;
192 }
193 default:
194 return -EINVAL;
195 }
196 }
197 return 0;
198}
199
200
201/* Special input handler for packets caught by router alert option.
202 They are selected only by protocol field, and then processed likely
203 local ones; but only if someone wants them! Otherwise, router
204 not running rsvpd will kill RSVP.
205
206 It is user level problem, what it will make with them.
207 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
208 but receiver should be enough clever f.e. to forward mtrace requests,
209 sent to multicast group to reach destination designated router.
210 */
211struct ip_ra_chain *ip_ra_chain;
212DEFINE_RWLOCK(ip_ra_lock);
213
214int ip_ra_control(struct sock *sk, unsigned char on, void (*destructor)(struct sock *))
215{
216 struct ip_ra_chain *ra, *new_ra, **rap;
217
218 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num == IPPROTO_RAW)
219 return -EINVAL;
220
221 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
222
223 write_lock_bh(&ip_ra_lock);
224 for (rap = &ip_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
225 if (ra->sk == sk) {
226 if (on) {
227 write_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800228 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -EADDRINUSE;
230 }
231 *rap = ra->next;
232 write_unlock_bh(&ip_ra_lock);
233
234 if (ra->destructor)
235 ra->destructor(sk);
236 sock_put(sk);
237 kfree(ra);
238 return 0;
239 }
240 }
241 if (new_ra == NULL) {
242 write_unlock_bh(&ip_ra_lock);
243 return -ENOBUFS;
244 }
245 new_ra->sk = sk;
246 new_ra->destructor = destructor;
247
248 new_ra->next = ra;
249 *rap = new_ra;
250 sock_hold(sk);
251 write_unlock_bh(&ip_ra_lock);
252
253 return 0;
254}
255
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900256void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700257 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct inet_sock *inet = inet_sk(sk);
260 struct sock_exterr_skb *serr;
261
262 if (!inet->recverr)
263 return;
264
265 skb = skb_clone(skb, GFP_ATOMIC);
266 if (!skb)
267 return;
268
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900269 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 serr->ee.ee_errno = err;
271 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300272 serr->ee.ee_type = icmp_hdr(skb)->type;
273 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 serr->ee.ee_pad = 0;
275 serr->ee.ee_info = info;
276 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300277 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700278 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 serr->port = port;
280
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300281 if (skb_pull(skb, payload - skb->data) != NULL) {
282 skb_reset_transport_header(skb);
283 if (sock_queue_err_skb(sk, skb) == 0)
284 return;
285 }
286 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Al Viro05790162006-09-27 18:33:40 -0700289void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct inet_sock *inet = inet_sk(sk);
292 struct sock_exterr_skb *serr;
293 struct iphdr *iph;
294 struct sk_buff *skb;
295
296 if (!inet->recverr)
297 return;
298
299 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
300 if (!skb)
301 return;
302
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300303 skb_put(skb, sizeof(struct iphdr));
304 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700305 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 iph->daddr = daddr;
307
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900308 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 serr->ee.ee_errno = err;
310 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900311 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 serr->ee.ee_code = 0;
313 serr->ee.ee_pad = 0;
314 serr->ee.ee_info = info;
315 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700316 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 serr->port = port;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 __skb_pull(skb, skb->tail - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300320 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 if (sock_queue_err_skb(sk, skb))
323 kfree_skb(skb);
324}
325
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900326/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * Handle MSG_ERRQUEUE
328 */
329int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
330{
331 struct sock_exterr_skb *serr;
332 struct sk_buff *skb, *skb2;
333 struct sockaddr_in *sin;
334 struct {
335 struct sock_extended_err ee;
336 struct sockaddr_in offender;
337 } errhdr;
338 int err;
339 int copied;
340
341 err = -EAGAIN;
342 skb = skb_dequeue(&sk->sk_error_queue);
343 if (skb == NULL)
344 goto out;
345
346 copied = skb->len;
347 if (copied > len) {
348 msg->msg_flags |= MSG_TRUNC;
349 copied = len;
350 }
351 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
352 if (err)
353 goto out_free_skb;
354
355 sock_recv_timestamp(msg, sk, skb);
356
357 serr = SKB_EXT_ERR(skb);
358
359 sin = (struct sockaddr_in *)msg->msg_name;
360 if (sin) {
361 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700362 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
363 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 sin->sin_port = serr->port;
365 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
366 }
367
368 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
369 sin = &errhdr.offender;
370 sin->sin_family = AF_UNSPEC;
371 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
372 struct inet_sock *inet = inet_sk(sk);
373
374 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700375 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 sin->sin_port = 0;
377 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
378 if (inet->cmsg_flags)
379 ip_cmsg_recv(msg, skb);
380 }
381
382 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
383
384 /* Now we could try to dump offended packet options */
385
386 msg->msg_flags |= MSG_ERRQUEUE;
387 err = copied;
388
389 /* Reset and regenerate socket error */
Herbert Xue0f9f852005-06-18 22:56:18 -0700390 spin_lock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 sk->sk_err = 0;
392 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
393 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
Herbert Xue0f9f852005-06-18 22:56:18 -0700394 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 sk->sk_error_report(sk);
396 } else
Herbert Xue0f9f852005-06-18 22:56:18 -0700397 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900399out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 kfree_skb(skb);
401out:
402 return err;
403}
404
405
406/*
407 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
408 * an IP socket.
409 */
410
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800411static int do_ip_setsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -0800412 int optname, char __user *optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 struct inet_sock *inet = inet_sk(sk);
415 int val=0,err;
416
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900417 if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
Stephen Hemminger132adf52007-03-08 20:44:43 -0800418 (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
419 (1<<IP_RETOPTS) | (1<<IP_TOS) |
420 (1<<IP_TTL) | (1<<IP_HDRINCL) |
421 (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
422 (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
423 (1<<IP_PASSSEC))) ||
424 optname == IP_MULTICAST_TTL ||
425 optname == IP_MULTICAST_LOOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (optlen >= sizeof(int)) {
427 if (get_user(val, (int __user *) optval))
428 return -EFAULT;
429 } else if (optlen >= sizeof(char)) {
430 unsigned char ucval;
431
432 if (get_user(ucval, (unsigned char __user *) optval))
433 return -EFAULT;
434 val = (int) ucval;
435 }
436 }
437
438 /* If optlen==0, it is equivalent to val == 0 */
439
440#ifdef CONFIG_IP_MROUTE
441 if (optname >= MRT_BASE && optname <= (MRT_BASE + 10))
442 return ip_mroute_setsockopt(sk,optname,optval,optlen);
443#endif
444
445 err = 0;
446 lock_sock(sk);
447
448 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800449 case IP_OPTIONS:
450 {
451 struct ip_options * opt = NULL;
452 if (optlen > 40 || optlen < 0)
453 goto e_inval;
454 err = ip_options_get_from_user(&opt, optval, optlen);
455 if (err)
456 break;
457 if (inet->is_icsk) {
458 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800460 if (sk->sk_family == PF_INET ||
461 (!((1 << sk->sk_state) &
462 (TCPF_LISTEN | TCPF_CLOSE)) &&
463 inet->daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -0800465 if (inet->opt)
466 icsk->icsk_ext_hdr_len -= inet->opt->optlen;
467 if (opt)
468 icsk->icsk_ext_hdr_len += opt->optlen;
469 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800474 opt = xchg(&inet->opt, opt);
475 kfree(opt);
476 break;
477 }
478 case IP_PKTINFO:
479 if (val)
480 inet->cmsg_flags |= IP_CMSG_PKTINFO;
481 else
482 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
483 break;
484 case IP_RECVTTL:
485 if (val)
486 inet->cmsg_flags |= IP_CMSG_TTL;
487 else
488 inet->cmsg_flags &= ~IP_CMSG_TTL;
489 break;
490 case IP_RECVTOS:
491 if (val)
492 inet->cmsg_flags |= IP_CMSG_TOS;
493 else
494 inet->cmsg_flags &= ~IP_CMSG_TOS;
495 break;
496 case IP_RECVOPTS:
497 if (val)
498 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
499 else
500 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
501 break;
502 case IP_RETOPTS:
503 if (val)
504 inet->cmsg_flags |= IP_CMSG_RETOPTS;
505 else
506 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
507 break;
508 case IP_PASSSEC:
509 if (val)
510 inet->cmsg_flags |= IP_CMSG_PASSSEC;
511 else
512 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
513 break;
514 case IP_TOS: /* This sets both TOS and Precedence */
515 if (sk->sk_type == SOCK_STREAM) {
516 val &= ~3;
517 val |= inet->tos & 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800519 if (IPTOS_PREC(val) >= IPTOS_PREC_CRITIC_ECP &&
520 !capable(CAP_NET_ADMIN)) {
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700521 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800523 }
524 if (inet->tos != val) {
525 inet->tos = val;
526 sk->sk_priority = rt_tos2priority(val);
527 sk_dst_reset(sk);
528 }
529 break;
530 case IP_TTL:
531 if (optlen<1)
532 goto e_inval;
533 if (val != -1 && (val < 1 || val>255))
534 goto e_inval;
535 inet->uc_ttl = val;
536 break;
537 case IP_HDRINCL:
538 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800541 }
542 inet->hdrincl = val ? 1 : 0;
543 break;
544 case IP_MTU_DISCOVER:
545 if (val<0 || val>2)
546 goto e_inval;
547 inet->pmtudisc = val;
548 break;
549 case IP_RECVERR:
550 inet->recverr = !!val;
551 if (!val)
552 skb_queue_purge(&sk->sk_error_queue);
553 break;
554 case IP_MULTICAST_TTL:
555 if (sk->sk_type == SOCK_STREAM)
556 goto e_inval;
557 if (optlen<1)
558 goto e_inval;
559 if (val==-1)
560 val = 1;
561 if (val < 0 || val > 255)
562 goto e_inval;
563 inet->mc_ttl = val;
564 break;
565 case IP_MULTICAST_LOOP:
566 if (optlen<1)
567 goto e_inval;
568 inet->mc_loop = !!val;
569 break;
570 case IP_MULTICAST_IF:
571 {
572 struct ip_mreqn mreq;
573 struct net_device *dev = NULL;
574
575 if (sk->sk_type == SOCK_STREAM)
576 goto e_inval;
577 /*
578 * Check the arguments are allowable
579 */
580
581 err = -EFAULT;
582 if (optlen >= sizeof(struct ip_mreqn)) {
583 if (copy_from_user(&mreq,optval,sizeof(mreq)))
584 break;
585 } else {
586 memset(&mreq, 0, sizeof(mreq));
587 if (optlen >= sizeof(struct in_addr) &&
588 copy_from_user(&mreq.imr_address,optval,sizeof(struct in_addr)))
589 break;
590 }
591
592 if (!mreq.imr_ifindex) {
593 if (mreq.imr_address.s_addr == INADDR_ANY) {
594 inet->mc_index = 0;
595 inet->mc_addr = 0;
596 err = 0;
597 break;
598 }
599 dev = ip_dev_find(mreq.imr_address.s_addr);
600 if (dev) {
601 mreq.imr_ifindex = dev->ifindex;
602 dev_put(dev);
603 }
604 } else
605 dev = __dev_get_by_index(mreq.imr_ifindex);
606
607
608 err = -EADDRNOTAVAIL;
609 if (!dev)
610 break;
611
612 err = -EINVAL;
613 if (sk->sk_bound_dev_if &&
614 mreq.imr_ifindex != sk->sk_bound_dev_if)
615 break;
616
617 inet->mc_index = mreq.imr_ifindex;
618 inet->mc_addr = mreq.imr_address.s_addr;
619 err = 0;
620 break;
621 }
622
623 case IP_ADD_MEMBERSHIP:
624 case IP_DROP_MEMBERSHIP:
625 {
626 struct ip_mreqn mreq;
627
628 if (optlen < sizeof(struct ip_mreq))
629 goto e_inval;
630 err = -EFAULT;
631 if (optlen >= sizeof(struct ip_mreqn)) {
632 if (copy_from_user(&mreq,optval,sizeof(mreq)))
633 break;
634 } else {
635 memset(&mreq, 0, sizeof(mreq));
636 if (copy_from_user(&mreq,optval,sizeof(struct ip_mreq)))
637 break;
638 }
639
640 if (optname == IP_ADD_MEMBERSHIP)
641 err = ip_mc_join_group(sk, &mreq);
642 else
643 err = ip_mc_leave_group(sk, &mreq);
644 break;
645 }
646 case IP_MSFILTER:
647 {
648 extern int sysctl_igmp_max_msf;
649 struct ip_msfilter *msf;
650
651 if (optlen < IP_MSFILTER_SIZE(0))
652 goto e_inval;
653 if (optlen > sysctl_optmem_max) {
654 err = -ENOBUFS;
655 break;
656 }
657 msf = kmalloc(optlen, GFP_KERNEL);
658 if (msf == 0) {
659 err = -ENOBUFS;
660 break;
661 }
662 err = -EFAULT;
663 if (copy_from_user(msf, optval, optlen)) {
664 kfree(msf);
665 break;
666 }
667 /* numsrc >= (1G-4) overflow in 32 bits */
668 if (msf->imsf_numsrc >= 0x3ffffffcU ||
669 msf->imsf_numsrc > sysctl_igmp_max_msf) {
670 kfree(msf);
671 err = -ENOBUFS;
672 break;
673 }
674 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
675 kfree(msf);
676 err = -EINVAL;
677 break;
678 }
679 err = ip_mc_msfilter(sk, msf, 0);
680 kfree(msf);
681 break;
682 }
683 case IP_BLOCK_SOURCE:
684 case IP_UNBLOCK_SOURCE:
685 case IP_ADD_SOURCE_MEMBERSHIP:
686 case IP_DROP_SOURCE_MEMBERSHIP:
687 {
688 struct ip_mreq_source mreqs;
689 int omode, add;
690
691 if (optlen != sizeof(struct ip_mreq_source))
692 goto e_inval;
693 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
694 err = -EFAULT;
695 break;
696 }
697 if (optname == IP_BLOCK_SOURCE) {
698 omode = MCAST_EXCLUDE;
699 add = 1;
700 } else if (optname == IP_UNBLOCK_SOURCE) {
701 omode = MCAST_EXCLUDE;
702 add = 0;
703 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
704 struct ip_mreqn mreq;
705
706 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
707 mreq.imr_address.s_addr = mreqs.imr_interface;
708 mreq.imr_ifindex = 0;
709 err = ip_mc_join_group(sk, &mreq);
710 if (err && err != -EADDRINUSE)
711 break;
712 omode = MCAST_INCLUDE;
713 add = 1;
714 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
715 omode = MCAST_INCLUDE;
716 add = 0;
717 }
718 err = ip_mc_source(add, omode, sk, &mreqs, 0);
719 break;
720 }
721 case MCAST_JOIN_GROUP:
722 case MCAST_LEAVE_GROUP:
723 {
724 struct group_req greq;
725 struct sockaddr_in *psin;
726 struct ip_mreqn mreq;
727
728 if (optlen < sizeof(struct group_req))
729 goto e_inval;
730 err = -EFAULT;
731 if (copy_from_user(&greq, optval, sizeof(greq)))
732 break;
733 psin = (struct sockaddr_in *)&greq.gr_group;
734 if (psin->sin_family != AF_INET)
735 goto e_inval;
736 memset(&mreq, 0, sizeof(mreq));
737 mreq.imr_multiaddr = psin->sin_addr;
738 mreq.imr_ifindex = greq.gr_interface;
739
740 if (optname == MCAST_JOIN_GROUP)
741 err = ip_mc_join_group(sk, &mreq);
742 else
743 err = ip_mc_leave_group(sk, &mreq);
744 break;
745 }
746 case MCAST_JOIN_SOURCE_GROUP:
747 case MCAST_LEAVE_SOURCE_GROUP:
748 case MCAST_BLOCK_SOURCE:
749 case MCAST_UNBLOCK_SOURCE:
750 {
751 struct group_source_req greqs;
752 struct ip_mreq_source mreqs;
753 struct sockaddr_in *psin;
754 int omode, add;
755
756 if (optlen != sizeof(struct group_source_req))
757 goto e_inval;
758 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
759 err = -EFAULT;
760 break;
761 }
762 if (greqs.gsr_group.ss_family != AF_INET ||
763 greqs.gsr_source.ss_family != AF_INET) {
764 err = -EADDRNOTAVAIL;
765 break;
766 }
767 psin = (struct sockaddr_in *)&greqs.gsr_group;
768 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
769 psin = (struct sockaddr_in *)&greqs.gsr_source;
770 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
771 mreqs.imr_interface = 0; /* use index for mc_source */
772
773 if (optname == MCAST_BLOCK_SOURCE) {
774 omode = MCAST_EXCLUDE;
775 add = 1;
776 } else if (optname == MCAST_UNBLOCK_SOURCE) {
777 omode = MCAST_EXCLUDE;
778 add = 0;
779 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
780 struct ip_mreqn mreq;
781
782 psin = (struct sockaddr_in *)&greqs.gsr_group;
783 mreq.imr_multiaddr = psin->sin_addr;
784 mreq.imr_address.s_addr = 0;
785 mreq.imr_ifindex = greqs.gsr_interface;
786 err = ip_mc_join_group(sk, &mreq);
787 if (err && err != -EADDRINUSE)
788 break;
789 greqs.gsr_interface = mreq.imr_ifindex;
790 omode = MCAST_INCLUDE;
791 add = 1;
792 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
793 omode = MCAST_INCLUDE;
794 add = 0;
795 }
796 err = ip_mc_source(add, omode, sk, &mreqs,
797 greqs.gsr_interface);
798 break;
799 }
800 case MCAST_MSFILTER:
801 {
802 extern int sysctl_igmp_max_msf;
803 struct sockaddr_in *psin;
804 struct ip_msfilter *msf = NULL;
805 struct group_filter *gsf = NULL;
806 int msize, i, ifindex;
807
808 if (optlen < GROUP_FILTER_SIZE(0))
809 goto e_inval;
810 if (optlen > sysctl_optmem_max) {
811 err = -ENOBUFS;
812 break;
813 }
814 gsf = kmalloc(optlen,GFP_KERNEL);
815 if (gsf == 0) {
816 err = -ENOBUFS;
817 break;
818 }
819 err = -EFAULT;
820 if (copy_from_user(gsf, optval, optlen)) {
821 goto mc_msf_out;
822 }
823 /* numsrc >= (4G-140)/128 overflow in 32 bits */
824 if (gsf->gf_numsrc >= 0x1ffffff ||
825 gsf->gf_numsrc > sysctl_igmp_max_msf) {
826 err = -ENOBUFS;
827 goto mc_msf_out;
828 }
829 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
830 err = -EINVAL;
831 goto mc_msf_out;
832 }
833 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
834 msf = kmalloc(msize,GFP_KERNEL);
835 if (msf == 0) {
836 err = -ENOBUFS;
837 goto mc_msf_out;
838 }
839 ifindex = gsf->gf_interface;
840 psin = (struct sockaddr_in *)&gsf->gf_group;
841 if (psin->sin_family != AF_INET) {
842 err = -EADDRNOTAVAIL;
843 goto mc_msf_out;
844 }
845 msf->imsf_multiaddr = psin->sin_addr.s_addr;
846 msf->imsf_interface = 0;
847 msf->imsf_fmode = gsf->gf_fmode;
848 msf->imsf_numsrc = gsf->gf_numsrc;
849 err = -EADDRNOTAVAIL;
850 for (i=0; i<gsf->gf_numsrc; ++i) {
851 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
852
853 if (psin->sin_family != AF_INET)
854 goto mc_msf_out;
855 msf->imsf_slist[i] = psin->sin_addr.s_addr;
856 }
857 kfree(gsf);
858 gsf = NULL;
859
860 err = ip_mc_msfilter(sk, msf, ifindex);
861 mc_msf_out:
862 kfree(msf);
863 kfree(gsf);
864 break;
865 }
866 case IP_ROUTER_ALERT:
867 err = ip_ra_control(sk, val ? 1 : 0, NULL);
868 break;
869
870 case IP_FREEBIND:
871 if (optlen<1)
872 goto e_inval;
873 inet->freebind = !!val;
874 break;
875
876 case IP_IPSEC_POLICY:
877 case IP_XFRM_POLICY:
878 err = -EPERM;
879 if (!capable(CAP_NET_ADMIN))
880 break;
881 err = xfrm_user_policy(sk, optname, optval, optlen);
882 break;
883
884 default:
885 err = -ENOPROTOOPT;
886 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888 release_sock(sk);
889 return err;
890
891e_inval:
892 release_sock(sk);
893 return -EINVAL;
894}
895
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800896int ip_setsockopt(struct sock *sk, int level,
897 int optname, char __user *optval, int optlen)
898{
899 int err;
900
901 if (level != SOL_IP)
902 return -ENOPROTOOPT;
903
904 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
905#ifdef CONFIG_NETFILTER
906 /* we need to exclude all possible ENOPROTOOPTs except default case */
907 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
908 optname != IP_IPSEC_POLICY && optname != IP_XFRM_POLICY
909#ifdef CONFIG_IP_MROUTE
910 && (optname < MRT_BASE || optname > (MRT_BASE + 10))
911#endif
912 ) {
913 lock_sock(sk);
914 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
915 release_sock(sk);
916 }
917#endif
918 return err;
919}
920
921#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800922int compat_ip_setsockopt(struct sock *sk, int level, int optname,
923 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800924{
925 int err;
926
927 if (level != SOL_IP)
928 return -ENOPROTOOPT;
929
930 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
931#ifdef CONFIG_NETFILTER
932 /* we need to exclude all possible ENOPROTOOPTs except default case */
933 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800934 optname != IP_IPSEC_POLICY && optname != IP_XFRM_POLICY
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800935#ifdef CONFIG_IP_MROUTE
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800936 && (optname < MRT_BASE || optname > (MRT_BASE + 10))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800937#endif
938 ) {
939 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800940 err = compat_nf_setsockopt(sk, PF_INET, optname,
941 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800942 release_sock(sk);
943 }
944#endif
945 return err;
946}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800947
948EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800949#endif
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951/*
952 * Get the options. Note for future reference. The GET of IP options gets the
953 * _received_ ones. The set sets the _sent_ ones.
954 */
955
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800956static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Stephen Hemminger132adf52007-03-08 20:44:43 -0800957 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 struct inet_sock *inet = inet_sk(sk);
960 int val;
961 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900962
Stephen Hemminger132adf52007-03-08 20:44:43 -0800963 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return -EOPNOTSUPP;
965
966#ifdef CONFIG_IP_MROUTE
Stephen Hemminger132adf52007-03-08 20:44:43 -0800967 if (optname >= MRT_BASE && optname <= MRT_BASE+10) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return ip_mroute_getsockopt(sk,optname,optval,optlen);
969 }
970#endif
971
Stephen Hemminger132adf52007-03-08 20:44:43 -0800972 if (get_user(len,optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800974 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 lock_sock(sk);
978
Stephen Hemminger132adf52007-03-08 20:44:43 -0800979 switch (optname) {
980 case IP_OPTIONS:
981 {
982 unsigned char optbuf[sizeof(struct ip_options)+40];
983 struct ip_options * opt = (struct ip_options*)optbuf;
984 opt->optlen = 0;
985 if (inet->opt)
986 memcpy(optbuf, inet->opt,
987 sizeof(struct ip_options)+
988 inet->opt->optlen);
989 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Stephen Hemminger132adf52007-03-08 20:44:43 -0800991 if (opt->optlen == 0)
992 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Stephen Hemminger132adf52007-03-08 20:44:43 -0800994 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Stephen Hemminger132adf52007-03-08 20:44:43 -0800996 len = min_t(unsigned int, len, opt->optlen);
997 if (put_user(len, optlen))
998 return -EFAULT;
999 if (copy_to_user(optval, opt->__data, len))
1000 return -EFAULT;
1001 return 0;
1002 }
1003 case IP_PKTINFO:
1004 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1005 break;
1006 case IP_RECVTTL:
1007 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1008 break;
1009 case IP_RECVTOS:
1010 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1011 break;
1012 case IP_RECVOPTS:
1013 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1014 break;
1015 case IP_RETOPTS:
1016 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1017 break;
1018 case IP_PASSSEC:
1019 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1020 break;
1021 case IP_TOS:
1022 val = inet->tos;
1023 break;
1024 case IP_TTL:
1025 val = (inet->uc_ttl == -1 ?
1026 sysctl_ip_default_ttl :
1027 inet->uc_ttl);
1028 break;
1029 case IP_HDRINCL:
1030 val = inet->hdrincl;
1031 break;
1032 case IP_MTU_DISCOVER:
1033 val = inet->pmtudisc;
1034 break;
1035 case IP_MTU:
1036 {
1037 struct dst_entry *dst;
1038 val = 0;
1039 dst = sk_dst_get(sk);
1040 if (dst) {
1041 val = dst_mtu(dst);
1042 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001044 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001046 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001048 break;
1049 }
1050 case IP_RECVERR:
1051 val = inet->recverr;
1052 break;
1053 case IP_MULTICAST_TTL:
1054 val = inet->mc_ttl;
1055 break;
1056 case IP_MULTICAST_LOOP:
1057 val = inet->mc_loop;
1058 break;
1059 case IP_MULTICAST_IF:
1060 {
1061 struct in_addr addr;
1062 len = min_t(unsigned int, len, sizeof(struct in_addr));
1063 addr.s_addr = inet->mc_addr;
1064 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Stephen Hemminger132adf52007-03-08 20:44:43 -08001066 if (put_user(len, optlen))
1067 return -EFAULT;
1068 if (copy_to_user(optval, &addr, len))
1069 return -EFAULT;
1070 return 0;
1071 }
1072 case IP_MSFILTER:
1073 {
1074 struct ip_msfilter msf;
1075 int err;
1076
1077 if (len < IP_MSFILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001079 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001081 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001083 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001085 err = ip_mc_msfget(sk, &msf,
1086 (struct ip_msfilter __user *)optval, optlen);
1087 release_sock(sk);
1088 return err;
1089 }
1090 case MCAST_MSFILTER:
1091 {
1092 struct group_filter gsf;
1093 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Stephen Hemminger132adf52007-03-08 20:44:43 -08001095 if (len < GROUP_FILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001097 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001099 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001101 return -EFAULT;
1102 }
1103 err = ip_mc_gsfget(sk, &gsf,
1104 (struct group_filter __user *)optval, optlen);
1105 release_sock(sk);
1106 return err;
1107 }
1108 case IP_PKTOPTIONS:
1109 {
1110 struct msghdr msg;
1111
1112 release_sock(sk);
1113
1114 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001116
1117 msg.msg_control = optval;
1118 msg.msg_controllen = len;
1119 msg.msg_flags = 0;
1120
1121 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1122 struct in_pktinfo info;
1123
1124 info.ipi_addr.s_addr = inet->rcv_saddr;
1125 info.ipi_spec_dst.s_addr = inet->rcv_saddr;
1126 info.ipi_ifindex = inet->mc_index;
1127 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1128 }
1129 if (inet->cmsg_flags & IP_CMSG_TTL) {
1130 int hlim = inet->mc_ttl;
1131 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1132 }
1133 len -= msg.msg_controllen;
1134 return put_user(len, optlen);
1135 }
1136 case IP_FREEBIND:
1137 val = inet->freebind;
1138 break;
1139 default:
1140 release_sock(sk);
1141 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (len < sizeof(int) && len > 0 && val>=0 && val<255) {
1146 unsigned char ucval = (unsigned char)val;
1147 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001148 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001150 if (copy_to_user(optval,&ucval,1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return -EFAULT;
1152 } else {
1153 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001154 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001156 if (copy_to_user(optval,&val,len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return -EFAULT;
1158 }
1159 return 0;
1160}
1161
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001162int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001163 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001164{
1165 int err;
1166
1167 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1168#ifdef CONFIG_NETFILTER
1169 /* we need to exclude all possible ENOPROTOOPTs except default case */
1170 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS
1171#ifdef CONFIG_IP_MROUTE
1172 && (optname < MRT_BASE || optname > MRT_BASE+10)
1173#endif
1174 ) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001175 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001176
Stephen Hemminger132adf52007-03-08 20:44:43 -08001177 if (get_user(len,optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001178 return -EFAULT;
1179
1180 lock_sock(sk);
1181 err = nf_getsockopt(sk, PF_INET, optname, optval,
1182 &len);
1183 release_sock(sk);
1184 if (err >= 0)
1185 err = put_user(len, optlen);
1186 return err;
1187 }
1188#endif
1189 return err;
1190}
1191
1192#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001193int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1194 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001195{
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001196 int err = do_ip_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001197#ifdef CONFIG_NETFILTER
1198 /* we need to exclude all possible ENOPROTOOPTs except default case */
1199 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS
1200#ifdef CONFIG_IP_MROUTE
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001201 && (optname < MRT_BASE || optname > MRT_BASE+10)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001202#endif
1203 ) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001204 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001205
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001206 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001207 return -EFAULT;
1208
1209 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001210 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001211 release_sock(sk);
1212 if (err >= 0)
1213 err = put_user(len, optlen);
1214 return err;
1215 }
1216#endif
1217 return err;
1218}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001219
1220EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001221#endif
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223EXPORT_SYMBOL(ip_cmsg_recv);
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225EXPORT_SYMBOL(ip_getsockopt);
1226EXPORT_SYMBOL(ip_setsockopt);