blob: ccdc59df015fe443c0caa9fa617fa6cc5778ea09 [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
281 skb->h.raw = payload;
282 if (!skb_pull(skb, payload - skb->data) ||
283 sock_queue_err_skb(sk, skb))
284 kfree_skb(skb);
285}
286
Al Viro05790162006-09-27 18:33:40 -0700287void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct inet_sock *inet = inet_sk(sk);
290 struct sock_exterr_skb *serr;
291 struct iphdr *iph;
292 struct sk_buff *skb;
293
294 if (!inet->recverr)
295 return;
296
297 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
298 if (!skb)
299 return;
300
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300301 skb_put(skb, sizeof(struct iphdr));
302 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700303 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 iph->daddr = daddr;
305
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900306 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 serr->ee.ee_errno = err;
308 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900309 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 serr->ee.ee_code = 0;
311 serr->ee.ee_pad = 0;
312 serr->ee.ee_info = info;
313 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700314 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 serr->port = port;
316
317 skb->h.raw = skb->tail;
318 __skb_pull(skb, skb->tail - skb->data);
319
320 if (sock_queue_err_skb(sk, skb))
321 kfree_skb(skb);
322}
323
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900324/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 * Handle MSG_ERRQUEUE
326 */
327int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
328{
329 struct sock_exterr_skb *serr;
330 struct sk_buff *skb, *skb2;
331 struct sockaddr_in *sin;
332 struct {
333 struct sock_extended_err ee;
334 struct sockaddr_in offender;
335 } errhdr;
336 int err;
337 int copied;
338
339 err = -EAGAIN;
340 skb = skb_dequeue(&sk->sk_error_queue);
341 if (skb == NULL)
342 goto out;
343
344 copied = skb->len;
345 if (copied > len) {
346 msg->msg_flags |= MSG_TRUNC;
347 copied = len;
348 }
349 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
350 if (err)
351 goto out_free_skb;
352
353 sock_recv_timestamp(msg, sk, skb);
354
355 serr = SKB_EXT_ERR(skb);
356
357 sin = (struct sockaddr_in *)msg->msg_name;
358 if (sin) {
359 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700360 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
361 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 sin->sin_port = serr->port;
363 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
364 }
365
366 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
367 sin = &errhdr.offender;
368 sin->sin_family = AF_UNSPEC;
369 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
370 struct inet_sock *inet = inet_sk(sk);
371
372 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700373 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 sin->sin_port = 0;
375 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
376 if (inet->cmsg_flags)
377 ip_cmsg_recv(msg, skb);
378 }
379
380 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
381
382 /* Now we could try to dump offended packet options */
383
384 msg->msg_flags |= MSG_ERRQUEUE;
385 err = copied;
386
387 /* Reset and regenerate socket error */
Herbert Xue0f9f852005-06-18 22:56:18 -0700388 spin_lock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 sk->sk_err = 0;
390 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
391 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
Herbert Xue0f9f852005-06-18 22:56:18 -0700392 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 sk->sk_error_report(sk);
394 } else
Herbert Xue0f9f852005-06-18 22:56:18 -0700395 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900397out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 kfree_skb(skb);
399out:
400 return err;
401}
402
403
404/*
405 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
406 * an IP socket.
407 */
408
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800409static int do_ip_setsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -0800410 int optname, char __user *optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 struct inet_sock *inet = inet_sk(sk);
413 int val=0,err;
414
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900415 if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
Stephen Hemminger132adf52007-03-08 20:44:43 -0800416 (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
417 (1<<IP_RETOPTS) | (1<<IP_TOS) |
418 (1<<IP_TTL) | (1<<IP_HDRINCL) |
419 (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
420 (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
421 (1<<IP_PASSSEC))) ||
422 optname == IP_MULTICAST_TTL ||
423 optname == IP_MULTICAST_LOOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (optlen >= sizeof(int)) {
425 if (get_user(val, (int __user *) optval))
426 return -EFAULT;
427 } else if (optlen >= sizeof(char)) {
428 unsigned char ucval;
429
430 if (get_user(ucval, (unsigned char __user *) optval))
431 return -EFAULT;
432 val = (int) ucval;
433 }
434 }
435
436 /* If optlen==0, it is equivalent to val == 0 */
437
438#ifdef CONFIG_IP_MROUTE
439 if (optname >= MRT_BASE && optname <= (MRT_BASE + 10))
440 return ip_mroute_setsockopt(sk,optname,optval,optlen);
441#endif
442
443 err = 0;
444 lock_sock(sk);
445
446 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800447 case IP_OPTIONS:
448 {
449 struct ip_options * opt = NULL;
450 if (optlen > 40 || optlen < 0)
451 goto e_inval;
452 err = ip_options_get_from_user(&opt, optval, optlen);
453 if (err)
454 break;
455 if (inet->is_icsk) {
456 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800458 if (sk->sk_family == PF_INET ||
459 (!((1 << sk->sk_state) &
460 (TCPF_LISTEN | TCPF_CLOSE)) &&
461 inet->daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -0800463 if (inet->opt)
464 icsk->icsk_ext_hdr_len -= inet->opt->optlen;
465 if (opt)
466 icsk->icsk_ext_hdr_len += opt->optlen;
467 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800472 opt = xchg(&inet->opt, opt);
473 kfree(opt);
474 break;
475 }
476 case IP_PKTINFO:
477 if (val)
478 inet->cmsg_flags |= IP_CMSG_PKTINFO;
479 else
480 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
481 break;
482 case IP_RECVTTL:
483 if (val)
484 inet->cmsg_flags |= IP_CMSG_TTL;
485 else
486 inet->cmsg_flags &= ~IP_CMSG_TTL;
487 break;
488 case IP_RECVTOS:
489 if (val)
490 inet->cmsg_flags |= IP_CMSG_TOS;
491 else
492 inet->cmsg_flags &= ~IP_CMSG_TOS;
493 break;
494 case IP_RECVOPTS:
495 if (val)
496 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
497 else
498 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
499 break;
500 case IP_RETOPTS:
501 if (val)
502 inet->cmsg_flags |= IP_CMSG_RETOPTS;
503 else
504 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
505 break;
506 case IP_PASSSEC:
507 if (val)
508 inet->cmsg_flags |= IP_CMSG_PASSSEC;
509 else
510 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
511 break;
512 case IP_TOS: /* This sets both TOS and Precedence */
513 if (sk->sk_type == SOCK_STREAM) {
514 val &= ~3;
515 val |= inet->tos & 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800517 if (IPTOS_PREC(val) >= IPTOS_PREC_CRITIC_ECP &&
518 !capable(CAP_NET_ADMIN)) {
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700519 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800521 }
522 if (inet->tos != val) {
523 inet->tos = val;
524 sk->sk_priority = rt_tos2priority(val);
525 sk_dst_reset(sk);
526 }
527 break;
528 case IP_TTL:
529 if (optlen<1)
530 goto e_inval;
531 if (val != -1 && (val < 1 || val>255))
532 goto e_inval;
533 inet->uc_ttl = val;
534 break;
535 case IP_HDRINCL:
536 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800539 }
540 inet->hdrincl = val ? 1 : 0;
541 break;
542 case IP_MTU_DISCOVER:
543 if (val<0 || val>2)
544 goto e_inval;
545 inet->pmtudisc = val;
546 break;
547 case IP_RECVERR:
548 inet->recverr = !!val;
549 if (!val)
550 skb_queue_purge(&sk->sk_error_queue);
551 break;
552 case IP_MULTICAST_TTL:
553 if (sk->sk_type == SOCK_STREAM)
554 goto e_inval;
555 if (optlen<1)
556 goto e_inval;
557 if (val==-1)
558 val = 1;
559 if (val < 0 || val > 255)
560 goto e_inval;
561 inet->mc_ttl = val;
562 break;
563 case IP_MULTICAST_LOOP:
564 if (optlen<1)
565 goto e_inval;
566 inet->mc_loop = !!val;
567 break;
568 case IP_MULTICAST_IF:
569 {
570 struct ip_mreqn mreq;
571 struct net_device *dev = NULL;
572
573 if (sk->sk_type == SOCK_STREAM)
574 goto e_inval;
575 /*
576 * Check the arguments are allowable
577 */
578
579 err = -EFAULT;
580 if (optlen >= sizeof(struct ip_mreqn)) {
581 if (copy_from_user(&mreq,optval,sizeof(mreq)))
582 break;
583 } else {
584 memset(&mreq, 0, sizeof(mreq));
585 if (optlen >= sizeof(struct in_addr) &&
586 copy_from_user(&mreq.imr_address,optval,sizeof(struct in_addr)))
587 break;
588 }
589
590 if (!mreq.imr_ifindex) {
591 if (mreq.imr_address.s_addr == INADDR_ANY) {
592 inet->mc_index = 0;
593 inet->mc_addr = 0;
594 err = 0;
595 break;
596 }
597 dev = ip_dev_find(mreq.imr_address.s_addr);
598 if (dev) {
599 mreq.imr_ifindex = dev->ifindex;
600 dev_put(dev);
601 }
602 } else
603 dev = __dev_get_by_index(mreq.imr_ifindex);
604
605
606 err = -EADDRNOTAVAIL;
607 if (!dev)
608 break;
609
610 err = -EINVAL;
611 if (sk->sk_bound_dev_if &&
612 mreq.imr_ifindex != sk->sk_bound_dev_if)
613 break;
614
615 inet->mc_index = mreq.imr_ifindex;
616 inet->mc_addr = mreq.imr_address.s_addr;
617 err = 0;
618 break;
619 }
620
621 case IP_ADD_MEMBERSHIP:
622 case IP_DROP_MEMBERSHIP:
623 {
624 struct ip_mreqn mreq;
625
626 if (optlen < sizeof(struct ip_mreq))
627 goto e_inval;
628 err = -EFAULT;
629 if (optlen >= sizeof(struct ip_mreqn)) {
630 if (copy_from_user(&mreq,optval,sizeof(mreq)))
631 break;
632 } else {
633 memset(&mreq, 0, sizeof(mreq));
634 if (copy_from_user(&mreq,optval,sizeof(struct ip_mreq)))
635 break;
636 }
637
638 if (optname == IP_ADD_MEMBERSHIP)
639 err = ip_mc_join_group(sk, &mreq);
640 else
641 err = ip_mc_leave_group(sk, &mreq);
642 break;
643 }
644 case IP_MSFILTER:
645 {
646 extern int sysctl_igmp_max_msf;
647 struct ip_msfilter *msf;
648
649 if (optlen < IP_MSFILTER_SIZE(0))
650 goto e_inval;
651 if (optlen > sysctl_optmem_max) {
652 err = -ENOBUFS;
653 break;
654 }
655 msf = kmalloc(optlen, GFP_KERNEL);
656 if (msf == 0) {
657 err = -ENOBUFS;
658 break;
659 }
660 err = -EFAULT;
661 if (copy_from_user(msf, optval, optlen)) {
662 kfree(msf);
663 break;
664 }
665 /* numsrc >= (1G-4) overflow in 32 bits */
666 if (msf->imsf_numsrc >= 0x3ffffffcU ||
667 msf->imsf_numsrc > sysctl_igmp_max_msf) {
668 kfree(msf);
669 err = -ENOBUFS;
670 break;
671 }
672 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
673 kfree(msf);
674 err = -EINVAL;
675 break;
676 }
677 err = ip_mc_msfilter(sk, msf, 0);
678 kfree(msf);
679 break;
680 }
681 case IP_BLOCK_SOURCE:
682 case IP_UNBLOCK_SOURCE:
683 case IP_ADD_SOURCE_MEMBERSHIP:
684 case IP_DROP_SOURCE_MEMBERSHIP:
685 {
686 struct ip_mreq_source mreqs;
687 int omode, add;
688
689 if (optlen != sizeof(struct ip_mreq_source))
690 goto e_inval;
691 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
692 err = -EFAULT;
693 break;
694 }
695 if (optname == IP_BLOCK_SOURCE) {
696 omode = MCAST_EXCLUDE;
697 add = 1;
698 } else if (optname == IP_UNBLOCK_SOURCE) {
699 omode = MCAST_EXCLUDE;
700 add = 0;
701 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
702 struct ip_mreqn mreq;
703
704 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
705 mreq.imr_address.s_addr = mreqs.imr_interface;
706 mreq.imr_ifindex = 0;
707 err = ip_mc_join_group(sk, &mreq);
708 if (err && err != -EADDRINUSE)
709 break;
710 omode = MCAST_INCLUDE;
711 add = 1;
712 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
713 omode = MCAST_INCLUDE;
714 add = 0;
715 }
716 err = ip_mc_source(add, omode, sk, &mreqs, 0);
717 break;
718 }
719 case MCAST_JOIN_GROUP:
720 case MCAST_LEAVE_GROUP:
721 {
722 struct group_req greq;
723 struct sockaddr_in *psin;
724 struct ip_mreqn mreq;
725
726 if (optlen < sizeof(struct group_req))
727 goto e_inval;
728 err = -EFAULT;
729 if (copy_from_user(&greq, optval, sizeof(greq)))
730 break;
731 psin = (struct sockaddr_in *)&greq.gr_group;
732 if (psin->sin_family != AF_INET)
733 goto e_inval;
734 memset(&mreq, 0, sizeof(mreq));
735 mreq.imr_multiaddr = psin->sin_addr;
736 mreq.imr_ifindex = greq.gr_interface;
737
738 if (optname == MCAST_JOIN_GROUP)
739 err = ip_mc_join_group(sk, &mreq);
740 else
741 err = ip_mc_leave_group(sk, &mreq);
742 break;
743 }
744 case MCAST_JOIN_SOURCE_GROUP:
745 case MCAST_LEAVE_SOURCE_GROUP:
746 case MCAST_BLOCK_SOURCE:
747 case MCAST_UNBLOCK_SOURCE:
748 {
749 struct group_source_req greqs;
750 struct ip_mreq_source mreqs;
751 struct sockaddr_in *psin;
752 int omode, add;
753
754 if (optlen != sizeof(struct group_source_req))
755 goto e_inval;
756 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
757 err = -EFAULT;
758 break;
759 }
760 if (greqs.gsr_group.ss_family != AF_INET ||
761 greqs.gsr_source.ss_family != AF_INET) {
762 err = -EADDRNOTAVAIL;
763 break;
764 }
765 psin = (struct sockaddr_in *)&greqs.gsr_group;
766 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
767 psin = (struct sockaddr_in *)&greqs.gsr_source;
768 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
769 mreqs.imr_interface = 0; /* use index for mc_source */
770
771 if (optname == MCAST_BLOCK_SOURCE) {
772 omode = MCAST_EXCLUDE;
773 add = 1;
774 } else if (optname == MCAST_UNBLOCK_SOURCE) {
775 omode = MCAST_EXCLUDE;
776 add = 0;
777 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
778 struct ip_mreqn mreq;
779
780 psin = (struct sockaddr_in *)&greqs.gsr_group;
781 mreq.imr_multiaddr = psin->sin_addr;
782 mreq.imr_address.s_addr = 0;
783 mreq.imr_ifindex = greqs.gsr_interface;
784 err = ip_mc_join_group(sk, &mreq);
785 if (err && err != -EADDRINUSE)
786 break;
787 greqs.gsr_interface = mreq.imr_ifindex;
788 omode = MCAST_INCLUDE;
789 add = 1;
790 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
791 omode = MCAST_INCLUDE;
792 add = 0;
793 }
794 err = ip_mc_source(add, omode, sk, &mreqs,
795 greqs.gsr_interface);
796 break;
797 }
798 case MCAST_MSFILTER:
799 {
800 extern int sysctl_igmp_max_msf;
801 struct sockaddr_in *psin;
802 struct ip_msfilter *msf = NULL;
803 struct group_filter *gsf = NULL;
804 int msize, i, ifindex;
805
806 if (optlen < GROUP_FILTER_SIZE(0))
807 goto e_inval;
808 if (optlen > sysctl_optmem_max) {
809 err = -ENOBUFS;
810 break;
811 }
812 gsf = kmalloc(optlen,GFP_KERNEL);
813 if (gsf == 0) {
814 err = -ENOBUFS;
815 break;
816 }
817 err = -EFAULT;
818 if (copy_from_user(gsf, optval, optlen)) {
819 goto mc_msf_out;
820 }
821 /* numsrc >= (4G-140)/128 overflow in 32 bits */
822 if (gsf->gf_numsrc >= 0x1ffffff ||
823 gsf->gf_numsrc > sysctl_igmp_max_msf) {
824 err = -ENOBUFS;
825 goto mc_msf_out;
826 }
827 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
828 err = -EINVAL;
829 goto mc_msf_out;
830 }
831 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
832 msf = kmalloc(msize,GFP_KERNEL);
833 if (msf == 0) {
834 err = -ENOBUFS;
835 goto mc_msf_out;
836 }
837 ifindex = gsf->gf_interface;
838 psin = (struct sockaddr_in *)&gsf->gf_group;
839 if (psin->sin_family != AF_INET) {
840 err = -EADDRNOTAVAIL;
841 goto mc_msf_out;
842 }
843 msf->imsf_multiaddr = psin->sin_addr.s_addr;
844 msf->imsf_interface = 0;
845 msf->imsf_fmode = gsf->gf_fmode;
846 msf->imsf_numsrc = gsf->gf_numsrc;
847 err = -EADDRNOTAVAIL;
848 for (i=0; i<gsf->gf_numsrc; ++i) {
849 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
850
851 if (psin->sin_family != AF_INET)
852 goto mc_msf_out;
853 msf->imsf_slist[i] = psin->sin_addr.s_addr;
854 }
855 kfree(gsf);
856 gsf = NULL;
857
858 err = ip_mc_msfilter(sk, msf, ifindex);
859 mc_msf_out:
860 kfree(msf);
861 kfree(gsf);
862 break;
863 }
864 case IP_ROUTER_ALERT:
865 err = ip_ra_control(sk, val ? 1 : 0, NULL);
866 break;
867
868 case IP_FREEBIND:
869 if (optlen<1)
870 goto e_inval;
871 inet->freebind = !!val;
872 break;
873
874 case IP_IPSEC_POLICY:
875 case IP_XFRM_POLICY:
876 err = -EPERM;
877 if (!capable(CAP_NET_ADMIN))
878 break;
879 err = xfrm_user_policy(sk, optname, optval, optlen);
880 break;
881
882 default:
883 err = -ENOPROTOOPT;
884 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886 release_sock(sk);
887 return err;
888
889e_inval:
890 release_sock(sk);
891 return -EINVAL;
892}
893
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800894int ip_setsockopt(struct sock *sk, int level,
895 int optname, char __user *optval, int optlen)
896{
897 int err;
898
899 if (level != SOL_IP)
900 return -ENOPROTOOPT;
901
902 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
903#ifdef CONFIG_NETFILTER
904 /* we need to exclude all possible ENOPROTOOPTs except default case */
905 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
906 optname != IP_IPSEC_POLICY && optname != IP_XFRM_POLICY
907#ifdef CONFIG_IP_MROUTE
908 && (optname < MRT_BASE || optname > (MRT_BASE + 10))
909#endif
910 ) {
911 lock_sock(sk);
912 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
913 release_sock(sk);
914 }
915#endif
916 return err;
917}
918
919#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800920int compat_ip_setsockopt(struct sock *sk, int level, int optname,
921 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800922{
923 int err;
924
925 if (level != SOL_IP)
926 return -ENOPROTOOPT;
927
928 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
929#ifdef CONFIG_NETFILTER
930 /* we need to exclude all possible ENOPROTOOPTs except default case */
931 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800932 optname != IP_IPSEC_POLICY && optname != IP_XFRM_POLICY
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800933#ifdef CONFIG_IP_MROUTE
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800934 && (optname < MRT_BASE || optname > (MRT_BASE + 10))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800935#endif
936 ) {
937 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800938 err = compat_nf_setsockopt(sk, PF_INET, optname,
939 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800940 release_sock(sk);
941 }
942#endif
943 return err;
944}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800945
946EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800947#endif
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949/*
950 * Get the options. Note for future reference. The GET of IP options gets the
951 * _received_ ones. The set sets the _sent_ ones.
952 */
953
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800954static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Stephen Hemminger132adf52007-03-08 20:44:43 -0800955 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 struct inet_sock *inet = inet_sk(sk);
958 int val;
959 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900960
Stephen Hemminger132adf52007-03-08 20:44:43 -0800961 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return -EOPNOTSUPP;
963
964#ifdef CONFIG_IP_MROUTE
Stephen Hemminger132adf52007-03-08 20:44:43 -0800965 if (optname >= MRT_BASE && optname <= MRT_BASE+10) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return ip_mroute_getsockopt(sk,optname,optval,optlen);
967 }
968#endif
969
Stephen Hemminger132adf52007-03-08 20:44:43 -0800970 if (get_user(len,optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800972 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 lock_sock(sk);
976
Stephen Hemminger132adf52007-03-08 20:44:43 -0800977 switch (optname) {
978 case IP_OPTIONS:
979 {
980 unsigned char optbuf[sizeof(struct ip_options)+40];
981 struct ip_options * opt = (struct ip_options*)optbuf;
982 opt->optlen = 0;
983 if (inet->opt)
984 memcpy(optbuf, inet->opt,
985 sizeof(struct ip_options)+
986 inet->opt->optlen);
987 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Stephen Hemminger132adf52007-03-08 20:44:43 -0800989 if (opt->optlen == 0)
990 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Stephen Hemminger132adf52007-03-08 20:44:43 -0800992 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Stephen Hemminger132adf52007-03-08 20:44:43 -0800994 len = min_t(unsigned int, len, opt->optlen);
995 if (put_user(len, optlen))
996 return -EFAULT;
997 if (copy_to_user(optval, opt->__data, len))
998 return -EFAULT;
999 return 0;
1000 }
1001 case IP_PKTINFO:
1002 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1003 break;
1004 case IP_RECVTTL:
1005 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1006 break;
1007 case IP_RECVTOS:
1008 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1009 break;
1010 case IP_RECVOPTS:
1011 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1012 break;
1013 case IP_RETOPTS:
1014 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1015 break;
1016 case IP_PASSSEC:
1017 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1018 break;
1019 case IP_TOS:
1020 val = inet->tos;
1021 break;
1022 case IP_TTL:
1023 val = (inet->uc_ttl == -1 ?
1024 sysctl_ip_default_ttl :
1025 inet->uc_ttl);
1026 break;
1027 case IP_HDRINCL:
1028 val = inet->hdrincl;
1029 break;
1030 case IP_MTU_DISCOVER:
1031 val = inet->pmtudisc;
1032 break;
1033 case IP_MTU:
1034 {
1035 struct dst_entry *dst;
1036 val = 0;
1037 dst = sk_dst_get(sk);
1038 if (dst) {
1039 val = dst_mtu(dst);
1040 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001042 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001044 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001046 break;
1047 }
1048 case IP_RECVERR:
1049 val = inet->recverr;
1050 break;
1051 case IP_MULTICAST_TTL:
1052 val = inet->mc_ttl;
1053 break;
1054 case IP_MULTICAST_LOOP:
1055 val = inet->mc_loop;
1056 break;
1057 case IP_MULTICAST_IF:
1058 {
1059 struct in_addr addr;
1060 len = min_t(unsigned int, len, sizeof(struct in_addr));
1061 addr.s_addr = inet->mc_addr;
1062 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Stephen Hemminger132adf52007-03-08 20:44:43 -08001064 if (put_user(len, optlen))
1065 return -EFAULT;
1066 if (copy_to_user(optval, &addr, len))
1067 return -EFAULT;
1068 return 0;
1069 }
1070 case IP_MSFILTER:
1071 {
1072 struct ip_msfilter msf;
1073 int err;
1074
1075 if (len < IP_MSFILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001077 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001079 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001081 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001083 err = ip_mc_msfget(sk, &msf,
1084 (struct ip_msfilter __user *)optval, optlen);
1085 release_sock(sk);
1086 return err;
1087 }
1088 case MCAST_MSFILTER:
1089 {
1090 struct group_filter gsf;
1091 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Stephen Hemminger132adf52007-03-08 20:44:43 -08001093 if (len < GROUP_FILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001095 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001097 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001099 return -EFAULT;
1100 }
1101 err = ip_mc_gsfget(sk, &gsf,
1102 (struct group_filter __user *)optval, optlen);
1103 release_sock(sk);
1104 return err;
1105 }
1106 case IP_PKTOPTIONS:
1107 {
1108 struct msghdr msg;
1109
1110 release_sock(sk);
1111
1112 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001114
1115 msg.msg_control = optval;
1116 msg.msg_controllen = len;
1117 msg.msg_flags = 0;
1118
1119 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1120 struct in_pktinfo info;
1121
1122 info.ipi_addr.s_addr = inet->rcv_saddr;
1123 info.ipi_spec_dst.s_addr = inet->rcv_saddr;
1124 info.ipi_ifindex = inet->mc_index;
1125 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1126 }
1127 if (inet->cmsg_flags & IP_CMSG_TTL) {
1128 int hlim = inet->mc_ttl;
1129 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1130 }
1131 len -= msg.msg_controllen;
1132 return put_user(len, optlen);
1133 }
1134 case IP_FREEBIND:
1135 val = inet->freebind;
1136 break;
1137 default:
1138 release_sock(sk);
1139 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (len < sizeof(int) && len > 0 && val>=0 && val<255) {
1144 unsigned char ucval = (unsigned char)val;
1145 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001146 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001148 if (copy_to_user(optval,&ucval,1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return -EFAULT;
1150 } else {
1151 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001152 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001154 if (copy_to_user(optval,&val,len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return -EFAULT;
1156 }
1157 return 0;
1158}
1159
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001160int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001161 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001162{
1163 int err;
1164
1165 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1166#ifdef CONFIG_NETFILTER
1167 /* we need to exclude all possible ENOPROTOOPTs except default case */
1168 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS
1169#ifdef CONFIG_IP_MROUTE
1170 && (optname < MRT_BASE || optname > MRT_BASE+10)
1171#endif
1172 ) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001173 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001174
Stephen Hemminger132adf52007-03-08 20:44:43 -08001175 if (get_user(len,optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001176 return -EFAULT;
1177
1178 lock_sock(sk);
1179 err = nf_getsockopt(sk, PF_INET, optname, optval,
1180 &len);
1181 release_sock(sk);
1182 if (err >= 0)
1183 err = put_user(len, optlen);
1184 return err;
1185 }
1186#endif
1187 return err;
1188}
1189
1190#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001191int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1192 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001193{
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001194 int err = do_ip_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001195#ifdef CONFIG_NETFILTER
1196 /* we need to exclude all possible ENOPROTOOPTs except default case */
1197 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS
1198#ifdef CONFIG_IP_MROUTE
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001199 && (optname < MRT_BASE || optname > MRT_BASE+10)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001200#endif
1201 ) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001202 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001203
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001204 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001205 return -EFAULT;
1206
1207 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001208 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001209 release_sock(sk);
1210 if (err >= 0)
1211 err = put_user(len, optlen);
1212 return err;
1213 }
1214#endif
1215 return err;
1216}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001217
1218EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001219#endif
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221EXPORT_SYMBOL(ip_cmsg_recv);
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223EXPORT_SYMBOL(ip_getsockopt);
1224EXPORT_SYMBOL(ip_setsockopt);