blob: 0c0b6e363a20a9ac2e16b0ba22e816b7327ee6bc [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>
26#include <net/sock.h>
27#include <net/ip.h>
28#include <net/icmp.h>
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -080029#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/udp.h>
31#include <linux/igmp.h>
32#include <linux/netfilter.h>
33#include <linux/route.h>
34#include <linux/mroute.h>
35#include <net/route.h>
36#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070037#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
39#include <net/transp_v6.h>
40#endif
41
42#include <linux/errqueue.h>
43#include <asm/uaccess.h>
44
45#define IP_CMSG_PKTINFO 1
46#define IP_CMSG_TTL 2
47#define IP_CMSG_TOS 4
48#define IP_CMSG_RECVOPTS 8
49#define IP_CMSG_RETOPTS 16
Catherine Zhang2c7946a2006-03-20 22:41:23 -080050#define IP_CMSG_PASSSEC 32
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -080051#define IP_CMSG_ORIGDSTADDR 64
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;
Eric Dumazet511c3f92009-06-02 05:14:27 +000060 struct rtable *rt = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
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];
Jianjun Kong09cb1052008-11-03 00:27:11 -080098 struct ip_options * opt = (struct ip_options *)optbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
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
Harvey Harrison21d1a162008-11-20 01:54:27 -0800130static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800131{
132 struct sockaddr_in sin;
133 struct iphdr *iph = ip_hdr(skb);
Harvey Harrison21d1a162008-11-20 01:54:27 -0800134 __be16 *ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800135
136 if (skb_transport_offset(skb) + 4 > skb->len)
137 return;
138
139 /* All current transport protocols have the port numbers in the
140 * first four bytes of the transport header and this function is
141 * written with this assumption in mind.
142 */
143
144 sin.sin_family = AF_INET;
145 sin.sin_addr.s_addr = iph->daddr;
146 sin.sin_port = ports[1];
147 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
148
149 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
150}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
153{
154 struct inet_sock *inet = inet_sk(skb->sk);
155 unsigned flags = inet->cmsg_flags;
156
157 /* Ordered by supposed usage frequency */
158 if (flags & 1)
159 ip_cmsg_recv_pktinfo(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_ttl(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_tos(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_opts(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700175 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return;
177
178 if (flags & 1)
179 ip_cmsg_recv_retopts(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700180 if ((flags >>= 1) == 0)
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800181 return;
182
183 if (flags & 1)
184 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800185
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700186 if ((flags >>= 1) == 0)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800187 return;
188 if (flags & 1)
189 ip_cmsg_recv_dstaddr(msg, skb);
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700192EXPORT_SYMBOL(ip_cmsg_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Denis V. Lunev7a6adb92008-03-24 15:30:27 -0700194int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 int err;
197 struct cmsghdr *cmsg;
198
199 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
200 if (!CMSG_OK(msg, cmsg))
201 return -EINVAL;
202 if (cmsg->cmsg_level != SOL_IP)
203 continue;
204 switch (cmsg->cmsg_type) {
205 case IP_RETOPTS:
206 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700207 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
208 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (err)
210 return err;
211 break;
212 case IP_PKTINFO:
213 {
214 struct in_pktinfo *info;
215 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
216 return -EINVAL;
217 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
218 ipc->oif = info->ipi_ifindex;
219 ipc->addr = info->ipi_spec_dst.s_addr;
220 break;
221 }
222 default:
223 return -EINVAL;
224 }
225 }
226 return 0;
227}
228
229
230/* Special input handler for packets caught by router alert option.
231 They are selected only by protocol field, and then processed likely
232 local ones; but only if someone wants them! Otherwise, router
233 not running rsvpd will kill RSVP.
234
235 It is user level problem, what it will make with them.
236 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
237 but receiver should be enough clever f.e. to forward mtrace requests,
238 sent to multicast group to reach destination designated router.
239 */
240struct ip_ra_chain *ip_ra_chain;
241DEFINE_RWLOCK(ip_ra_lock);
242
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700243int ip_ra_control(struct sock *sk, unsigned char on,
244 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 struct ip_ra_chain *ra, *new_ra, **rap;
247
248 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num == IPPROTO_RAW)
249 return -EINVAL;
250
251 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
252
253 write_lock_bh(&ip_ra_lock);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700254 for (rap = &ip_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (ra->sk == sk) {
256 if (on) {
257 write_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800258 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return -EADDRINUSE;
260 }
261 *rap = ra->next;
262 write_unlock_bh(&ip_ra_lock);
263
264 if (ra->destructor)
265 ra->destructor(sk);
266 sock_put(sk);
267 kfree(ra);
268 return 0;
269 }
270 }
271 if (new_ra == NULL) {
272 write_unlock_bh(&ip_ra_lock);
273 return -ENOBUFS;
274 }
275 new_ra->sk = sk;
276 new_ra->destructor = destructor;
277
278 new_ra->next = ra;
279 *rap = new_ra;
280 sock_hold(sk);
281 write_unlock_bh(&ip_ra_lock);
282
283 return 0;
284}
285
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900286void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700287 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct inet_sock *inet = inet_sk(sk);
290 struct sock_exterr_skb *serr;
291
292 if (!inet->recverr)
293 return;
294
295 skb = skb_clone(skb, GFP_ATOMIC);
296 if (!skb)
297 return;
298
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900299 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 serr->ee.ee_errno = err;
301 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300302 serr->ee.ee_type = icmp_hdr(skb)->type;
303 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 serr->ee.ee_pad = 0;
305 serr->ee.ee_info = info;
306 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300307 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700308 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 serr->port = port;
310
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300311 if (skb_pull(skb, payload - skb->data) != NULL) {
312 skb_reset_transport_header(skb);
313 if (sock_queue_err_skb(sk, skb) == 0)
314 return;
315 }
316 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Al Viro05790162006-09-27 18:33:40 -0700319void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 struct inet_sock *inet = inet_sk(sk);
322 struct sock_exterr_skb *serr;
323 struct iphdr *iph;
324 struct sk_buff *skb;
325
326 if (!inet->recverr)
327 return;
328
329 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
330 if (!skb)
331 return;
332
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300333 skb_put(skb, sizeof(struct iphdr));
334 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700335 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 iph->daddr = daddr;
337
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900338 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 serr->ee.ee_errno = err;
340 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900341 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 serr->ee.ee_code = 0;
343 serr->ee.ee_pad = 0;
344 serr->ee.ee_info = info;
345 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700346 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 serr->port = port;
348
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700349 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300350 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (sock_queue_err_skb(sk, skb))
353 kfree_skb(skb);
354}
355
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900356/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * Handle MSG_ERRQUEUE
358 */
359int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
360{
361 struct sock_exterr_skb *serr;
362 struct sk_buff *skb, *skb2;
363 struct sockaddr_in *sin;
364 struct {
365 struct sock_extended_err ee;
366 struct sockaddr_in offender;
367 } errhdr;
368 int err;
369 int copied;
370
371 err = -EAGAIN;
372 skb = skb_dequeue(&sk->sk_error_queue);
373 if (skb == NULL)
374 goto out;
375
376 copied = skb->len;
377 if (copied > len) {
378 msg->msg_flags |= MSG_TRUNC;
379 copied = len;
380 }
381 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
382 if (err)
383 goto out_free_skb;
384
385 sock_recv_timestamp(msg, sk, skb);
386
387 serr = SKB_EXT_ERR(skb);
388
389 sin = (struct sockaddr_in *)msg->msg_name;
390 if (sin) {
391 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700392 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
393 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 sin->sin_port = serr->port;
395 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
396 }
397
398 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
399 sin = &errhdr.offender;
400 sin->sin_family = AF_UNSPEC;
401 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
402 struct inet_sock *inet = inet_sk(sk);
403
404 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700405 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 sin->sin_port = 0;
407 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
408 if (inet->cmsg_flags)
409 ip_cmsg_recv(msg, skb);
410 }
411
412 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
413
414 /* Now we could try to dump offended packet options */
415
416 msg->msg_flags |= MSG_ERRQUEUE;
417 err = copied;
418
419 /* Reset and regenerate socket error */
Herbert Xue0f9f852005-06-18 22:56:18 -0700420 spin_lock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 sk->sk_err = 0;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700422 skb2 = skb_peek(&sk->sk_error_queue);
423 if (skb2 != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
Herbert Xue0f9f852005-06-18 22:56:18 -0700425 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 sk->sk_error_report(sk);
427 } else
Herbert Xue0f9f852005-06-18 22:56:18 -0700428 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900430out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 kfree_skb(skb);
432out:
433 return err;
434}
435
436
437/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700438 * Socket option code for IP. This is the end of the line after any
439 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
441
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800442static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700443 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 struct inet_sock *inet = inet_sk(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800446 int val = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900448 if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
Stephen Hemminger132adf52007-03-08 20:44:43 -0800449 (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
450 (1<<IP_RETOPTS) | (1<<IP_TOS) |
451 (1<<IP_TTL) | (1<<IP_HDRINCL) |
452 (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
453 (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -0700454 (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT))) ||
Stephen Hemminger132adf52007-03-08 20:44:43 -0800455 optname == IP_MULTICAST_TTL ||
Nivedita Singhvif771bef2009-05-28 07:00:46 +0000456 optname == IP_MULTICAST_ALL ||
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800457 optname == IP_MULTICAST_LOOP ||
458 optname == IP_RECVORIGDSTADDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (optlen >= sizeof(int)) {
460 if (get_user(val, (int __user *) optval))
461 return -EFAULT;
462 } else if (optlen >= sizeof(char)) {
463 unsigned char ucval;
464
465 if (get_user(ucval, (unsigned char __user *) optval))
466 return -EFAULT;
467 val = (int) ucval;
468 }
469 }
470
471 /* If optlen==0, it is equivalent to val == 0 */
472
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800473 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800474 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 err = 0;
477 lock_sock(sk);
478
479 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800480 case IP_OPTIONS:
481 {
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700482 struct ip_options *opt = NULL;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800483 if (optlen > 40 || optlen < 0)
484 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900485 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700486 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800487 if (err)
488 break;
489 if (inet->is_icsk) {
490 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800492 if (sk->sk_family == PF_INET ||
493 (!((1 << sk->sk_state) &
494 (TCPF_LISTEN | TCPF_CLOSE)) &&
495 inet->daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -0800497 if (inet->opt)
498 icsk->icsk_ext_hdr_len -= inet->opt->optlen;
499 if (opt)
500 icsk->icsk_ext_hdr_len += opt->optlen;
501 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800506 opt = xchg(&inet->opt, opt);
507 kfree(opt);
508 break;
509 }
510 case IP_PKTINFO:
511 if (val)
512 inet->cmsg_flags |= IP_CMSG_PKTINFO;
513 else
514 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
515 break;
516 case IP_RECVTTL:
517 if (val)
518 inet->cmsg_flags |= IP_CMSG_TTL;
519 else
520 inet->cmsg_flags &= ~IP_CMSG_TTL;
521 break;
522 case IP_RECVTOS:
523 if (val)
524 inet->cmsg_flags |= IP_CMSG_TOS;
525 else
526 inet->cmsg_flags &= ~IP_CMSG_TOS;
527 break;
528 case IP_RECVOPTS:
529 if (val)
530 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
531 else
532 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
533 break;
534 case IP_RETOPTS:
535 if (val)
536 inet->cmsg_flags |= IP_CMSG_RETOPTS;
537 else
538 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
539 break;
540 case IP_PASSSEC:
541 if (val)
542 inet->cmsg_flags |= IP_CMSG_PASSSEC;
543 else
544 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
545 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800546 case IP_RECVORIGDSTADDR:
547 if (val)
548 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
549 else
550 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
551 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800552 case IP_TOS: /* This sets both TOS and Precedence */
553 if (sk->sk_type == SOCK_STREAM) {
554 val &= ~3;
555 val |= inet->tos & 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800557 if (inet->tos != val) {
558 inet->tos = val;
559 sk->sk_priority = rt_tos2priority(val);
560 sk_dst_reset(sk);
561 }
562 break;
563 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700564 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800565 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700566 if (val != -1 && (val < 0 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800567 goto e_inval;
568 inet->uc_ttl = val;
569 break;
570 case IP_HDRINCL:
571 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800574 }
575 inet->hdrincl = val ? 1 : 0;
576 break;
577 case IP_MTU_DISCOVER:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700578 if (val < 0 || val > 3)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800579 goto e_inval;
580 inet->pmtudisc = val;
581 break;
582 case IP_RECVERR:
583 inet->recverr = !!val;
584 if (!val)
585 skb_queue_purge(&sk->sk_error_queue);
586 break;
587 case IP_MULTICAST_TTL:
588 if (sk->sk_type == SOCK_STREAM)
589 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700590 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800591 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800592 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800593 val = 1;
594 if (val < 0 || val > 255)
595 goto e_inval;
596 inet->mc_ttl = val;
597 break;
598 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700599 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800600 goto e_inval;
601 inet->mc_loop = !!val;
602 break;
603 case IP_MULTICAST_IF:
604 {
605 struct ip_mreqn mreq;
606 struct net_device *dev = NULL;
607
608 if (sk->sk_type == SOCK_STREAM)
609 goto e_inval;
610 /*
611 * Check the arguments are allowable
612 */
613
Shan Wei09159212009-09-22 15:41:10 +0000614 if (optlen < sizeof(struct in_addr))
615 goto e_inval;
616
Stephen Hemminger132adf52007-03-08 20:44:43 -0800617 err = -EFAULT;
618 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800619 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800620 break;
621 } else {
622 memset(&mreq, 0, sizeof(mreq));
623 if (optlen >= sizeof(struct in_addr) &&
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700624 copy_from_user(&mreq.imr_address, optval,
625 sizeof(struct in_addr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800626 break;
627 }
628
629 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700630 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800631 inet->mc_index = 0;
632 inet->mc_addr = 0;
633 err = 0;
634 break;
635 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900636 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800637 if (dev) {
638 mreq.imr_ifindex = dev->ifindex;
639 dev_put(dev);
640 }
641 } else
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900642 dev = __dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800643
644
645 err = -EADDRNOTAVAIL;
646 if (!dev)
647 break;
648
649 err = -EINVAL;
650 if (sk->sk_bound_dev_if &&
651 mreq.imr_ifindex != sk->sk_bound_dev_if)
652 break;
653
654 inet->mc_index = mreq.imr_ifindex;
655 inet->mc_addr = mreq.imr_address.s_addr;
656 err = 0;
657 break;
658 }
659
660 case IP_ADD_MEMBERSHIP:
661 case IP_DROP_MEMBERSHIP:
662 {
663 struct ip_mreqn mreq;
664
Flavio Leitnera96fb492007-08-24 22:16:39 -0700665 err = -EPROTO;
666 if (inet_sk(sk)->is_icsk)
667 break;
668
Stephen Hemminger132adf52007-03-08 20:44:43 -0800669 if (optlen < sizeof(struct ip_mreq))
670 goto e_inval;
671 err = -EFAULT;
672 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800673 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800674 break;
675 } else {
676 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800677 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800678 break;
679 }
680
681 if (optname == IP_ADD_MEMBERSHIP)
682 err = ip_mc_join_group(sk, &mreq);
683 else
684 err = ip_mc_leave_group(sk, &mreq);
685 break;
686 }
687 case IP_MSFILTER:
688 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800689 struct ip_msfilter *msf;
690
691 if (optlen < IP_MSFILTER_SIZE(0))
692 goto e_inval;
693 if (optlen > sysctl_optmem_max) {
694 err = -ENOBUFS;
695 break;
696 }
697 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700698 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800699 err = -ENOBUFS;
700 break;
701 }
702 err = -EFAULT;
703 if (copy_from_user(msf, optval, optlen)) {
704 kfree(msf);
705 break;
706 }
707 /* numsrc >= (1G-4) overflow in 32 bits */
708 if (msf->imsf_numsrc >= 0x3ffffffcU ||
709 msf->imsf_numsrc > sysctl_igmp_max_msf) {
710 kfree(msf);
711 err = -ENOBUFS;
712 break;
713 }
714 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
715 kfree(msf);
716 err = -EINVAL;
717 break;
718 }
719 err = ip_mc_msfilter(sk, msf, 0);
720 kfree(msf);
721 break;
722 }
723 case IP_BLOCK_SOURCE:
724 case IP_UNBLOCK_SOURCE:
725 case IP_ADD_SOURCE_MEMBERSHIP:
726 case IP_DROP_SOURCE_MEMBERSHIP:
727 {
728 struct ip_mreq_source mreqs;
729 int omode, add;
730
731 if (optlen != sizeof(struct ip_mreq_source))
732 goto e_inval;
733 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
734 err = -EFAULT;
735 break;
736 }
737 if (optname == IP_BLOCK_SOURCE) {
738 omode = MCAST_EXCLUDE;
739 add = 1;
740 } else if (optname == IP_UNBLOCK_SOURCE) {
741 omode = MCAST_EXCLUDE;
742 add = 0;
743 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
744 struct ip_mreqn mreq;
745
746 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
747 mreq.imr_address.s_addr = mreqs.imr_interface;
748 mreq.imr_ifindex = 0;
749 err = ip_mc_join_group(sk, &mreq);
750 if (err && err != -EADDRINUSE)
751 break;
752 omode = MCAST_INCLUDE;
753 add = 1;
754 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
755 omode = MCAST_INCLUDE;
756 add = 0;
757 }
758 err = ip_mc_source(add, omode, sk, &mreqs, 0);
759 break;
760 }
761 case MCAST_JOIN_GROUP:
762 case MCAST_LEAVE_GROUP:
763 {
764 struct group_req greq;
765 struct sockaddr_in *psin;
766 struct ip_mreqn mreq;
767
768 if (optlen < sizeof(struct group_req))
769 goto e_inval;
770 err = -EFAULT;
771 if (copy_from_user(&greq, optval, sizeof(greq)))
772 break;
773 psin = (struct sockaddr_in *)&greq.gr_group;
774 if (psin->sin_family != AF_INET)
775 goto e_inval;
776 memset(&mreq, 0, sizeof(mreq));
777 mreq.imr_multiaddr = psin->sin_addr;
778 mreq.imr_ifindex = greq.gr_interface;
779
780 if (optname == MCAST_JOIN_GROUP)
781 err = ip_mc_join_group(sk, &mreq);
782 else
783 err = ip_mc_leave_group(sk, &mreq);
784 break;
785 }
786 case MCAST_JOIN_SOURCE_GROUP:
787 case MCAST_LEAVE_SOURCE_GROUP:
788 case MCAST_BLOCK_SOURCE:
789 case MCAST_UNBLOCK_SOURCE:
790 {
791 struct group_source_req greqs;
792 struct ip_mreq_source mreqs;
793 struct sockaddr_in *psin;
794 int omode, add;
795
796 if (optlen != sizeof(struct group_source_req))
797 goto e_inval;
798 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
799 err = -EFAULT;
800 break;
801 }
802 if (greqs.gsr_group.ss_family != AF_INET ||
803 greqs.gsr_source.ss_family != AF_INET) {
804 err = -EADDRNOTAVAIL;
805 break;
806 }
807 psin = (struct sockaddr_in *)&greqs.gsr_group;
808 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
809 psin = (struct sockaddr_in *)&greqs.gsr_source;
810 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
811 mreqs.imr_interface = 0; /* use index for mc_source */
812
813 if (optname == MCAST_BLOCK_SOURCE) {
814 omode = MCAST_EXCLUDE;
815 add = 1;
816 } else if (optname == MCAST_UNBLOCK_SOURCE) {
817 omode = MCAST_EXCLUDE;
818 add = 0;
819 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
820 struct ip_mreqn mreq;
821
822 psin = (struct sockaddr_in *)&greqs.gsr_group;
823 mreq.imr_multiaddr = psin->sin_addr;
824 mreq.imr_address.s_addr = 0;
825 mreq.imr_ifindex = greqs.gsr_interface;
826 err = ip_mc_join_group(sk, &mreq);
827 if (err && err != -EADDRINUSE)
828 break;
829 greqs.gsr_interface = mreq.imr_ifindex;
830 omode = MCAST_INCLUDE;
831 add = 1;
832 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
833 omode = MCAST_INCLUDE;
834 add = 0;
835 }
836 err = ip_mc_source(add, omode, sk, &mreqs,
837 greqs.gsr_interface);
838 break;
839 }
840 case MCAST_MSFILTER:
841 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800842 struct sockaddr_in *psin;
843 struct ip_msfilter *msf = NULL;
844 struct group_filter *gsf = NULL;
845 int msize, i, ifindex;
846
847 if (optlen < GROUP_FILTER_SIZE(0))
848 goto e_inval;
849 if (optlen > sysctl_optmem_max) {
850 err = -ENOBUFS;
851 break;
852 }
Jianjun Kong09cb1052008-11-03 00:27:11 -0800853 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700854 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800855 err = -ENOBUFS;
856 break;
857 }
858 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700859 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800860 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700861
Stephen Hemminger132adf52007-03-08 20:44:43 -0800862 /* numsrc >= (4G-140)/128 overflow in 32 bits */
863 if (gsf->gf_numsrc >= 0x1ffffff ||
864 gsf->gf_numsrc > sysctl_igmp_max_msf) {
865 err = -ENOBUFS;
866 goto mc_msf_out;
867 }
868 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
869 err = -EINVAL;
870 goto mc_msf_out;
871 }
872 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800873 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700874 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800875 err = -ENOBUFS;
876 goto mc_msf_out;
877 }
878 ifindex = gsf->gf_interface;
879 psin = (struct sockaddr_in *)&gsf->gf_group;
880 if (psin->sin_family != AF_INET) {
881 err = -EADDRNOTAVAIL;
882 goto mc_msf_out;
883 }
884 msf->imsf_multiaddr = psin->sin_addr.s_addr;
885 msf->imsf_interface = 0;
886 msf->imsf_fmode = gsf->gf_fmode;
887 msf->imsf_numsrc = gsf->gf_numsrc;
888 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700889 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800890 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
891
892 if (psin->sin_family != AF_INET)
893 goto mc_msf_out;
894 msf->imsf_slist[i] = psin->sin_addr.s_addr;
895 }
896 kfree(gsf);
897 gsf = NULL;
898
899 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700900mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -0800901 kfree(msf);
902 kfree(gsf);
903 break;
904 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +0000905 case IP_MULTICAST_ALL:
906 if (optlen < 1)
907 goto e_inval;
908 if (val != 0 && val != 1)
909 goto e_inval;
910 inet->mc_all = val;
911 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800912 case IP_ROUTER_ALERT:
913 err = ip_ra_control(sk, val ? 1 : 0, NULL);
914 break;
915
916 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700917 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800918 goto e_inval;
919 inet->freebind = !!val;
920 break;
921
922 case IP_IPSEC_POLICY:
923 case IP_XFRM_POLICY:
924 err = -EPERM;
925 if (!capable(CAP_NET_ADMIN))
926 break;
927 err = xfrm_user_policy(sk, optname, optval, optlen);
928 break;
929
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -0700930 case IP_TRANSPARENT:
931 if (!capable(CAP_NET_ADMIN)) {
932 err = -EPERM;
933 break;
934 }
935 if (optlen < 1)
936 goto e_inval;
937 inet->transparent = !!val;
938 break;
939
Stephen Hemminger132adf52007-03-08 20:44:43 -0800940 default:
941 err = -ENOPROTOOPT;
942 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944 release_sock(sk);
945 return err;
946
947e_inval:
948 release_sock(sk);
949 return -EINVAL;
950}
951
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800952int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700953 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800954{
955 int err;
956
957 if (level != SOL_IP)
958 return -ENOPROTOOPT;
959
960 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
961#ifdef CONFIG_NETFILTER
962 /* we need to exclude all possible ENOPROTOOPTs except default case */
963 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800964 optname != IP_IPSEC_POLICY &&
965 optname != IP_XFRM_POLICY &&
966 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800967 lock_sock(sk);
968 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
969 release_sock(sk);
970 }
971#endif
972 return err;
973}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700974EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800975
976#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800977int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700978 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800979{
980 int err;
981
982 if (level != SOL_IP)
983 return -ENOPROTOOPT;
984
David L Stevensdae50292008-04-27 01:06:07 -0700985 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
986 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
987 ip_setsockopt);
988
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800989 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
990#ifdef CONFIG_NETFILTER
991 /* we need to exclude all possible ENOPROTOOPTs except default case */
992 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800993 optname != IP_IPSEC_POLICY &&
994 optname != IP_XFRM_POLICY &&
995 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800996 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800997 err = compat_nf_setsockopt(sk, PF_INET, optname,
998 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800999 release_sock(sk);
1000 }
1001#endif
1002 return err;
1003}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001004EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001005#endif
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001008 * Get the options. Note for future reference. The GET of IP options gets
1009 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
1011
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001012static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001013 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 struct inet_sock *inet = inet_sk(sk);
1016 int val;
1017 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001018
Stephen Hemminger132adf52007-03-08 20:44:43 -08001019 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return -EOPNOTSUPP;
1021
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001022 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001023 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Jianjun Kong09cb1052008-11-03 00:27:11 -08001025 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001027 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 lock_sock(sk);
1031
Stephen Hemminger132adf52007-03-08 20:44:43 -08001032 switch (optname) {
1033 case IP_OPTIONS:
1034 {
1035 unsigned char optbuf[sizeof(struct ip_options)+40];
Jianjun Kong09cb1052008-11-03 00:27:11 -08001036 struct ip_options * opt = (struct ip_options *)optbuf;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001037 opt->optlen = 0;
1038 if (inet->opt)
1039 memcpy(optbuf, inet->opt,
1040 sizeof(struct ip_options)+
1041 inet->opt->optlen);
1042 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Stephen Hemminger132adf52007-03-08 20:44:43 -08001044 if (opt->optlen == 0)
1045 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Stephen Hemminger132adf52007-03-08 20:44:43 -08001047 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Stephen Hemminger132adf52007-03-08 20:44:43 -08001049 len = min_t(unsigned int, len, opt->optlen);
1050 if (put_user(len, optlen))
1051 return -EFAULT;
1052 if (copy_to_user(optval, opt->__data, len))
1053 return -EFAULT;
1054 return 0;
1055 }
1056 case IP_PKTINFO:
1057 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1058 break;
1059 case IP_RECVTTL:
1060 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1061 break;
1062 case IP_RECVTOS:
1063 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1064 break;
1065 case IP_RECVOPTS:
1066 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1067 break;
1068 case IP_RETOPTS:
1069 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1070 break;
1071 case IP_PASSSEC:
1072 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1073 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001074 case IP_RECVORIGDSTADDR:
1075 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1076 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001077 case IP_TOS:
1078 val = inet->tos;
1079 break;
1080 case IP_TTL:
1081 val = (inet->uc_ttl == -1 ?
1082 sysctl_ip_default_ttl :
1083 inet->uc_ttl);
1084 break;
1085 case IP_HDRINCL:
1086 val = inet->hdrincl;
1087 break;
1088 case IP_MTU_DISCOVER:
1089 val = inet->pmtudisc;
1090 break;
1091 case IP_MTU:
1092 {
1093 struct dst_entry *dst;
1094 val = 0;
1095 dst = sk_dst_get(sk);
1096 if (dst) {
1097 val = dst_mtu(dst);
1098 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001100 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001102 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001104 break;
1105 }
1106 case IP_RECVERR:
1107 val = inet->recverr;
1108 break;
1109 case IP_MULTICAST_TTL:
1110 val = inet->mc_ttl;
1111 break;
1112 case IP_MULTICAST_LOOP:
1113 val = inet->mc_loop;
1114 break;
1115 case IP_MULTICAST_IF:
1116 {
1117 struct in_addr addr;
1118 len = min_t(unsigned int, len, sizeof(struct in_addr));
1119 addr.s_addr = inet->mc_addr;
1120 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Stephen Hemminger132adf52007-03-08 20:44:43 -08001122 if (put_user(len, optlen))
1123 return -EFAULT;
1124 if (copy_to_user(optval, &addr, len))
1125 return -EFAULT;
1126 return 0;
1127 }
1128 case IP_MSFILTER:
1129 {
1130 struct ip_msfilter msf;
1131 int err;
1132
1133 if (len < IP_MSFILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001135 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001137 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001139 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001141 err = ip_mc_msfget(sk, &msf,
1142 (struct ip_msfilter __user *)optval, optlen);
1143 release_sock(sk);
1144 return err;
1145 }
1146 case MCAST_MSFILTER:
1147 {
1148 struct group_filter gsf;
1149 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Stephen Hemminger132adf52007-03-08 20:44:43 -08001151 if (len < GROUP_FILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001153 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001155 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001157 return -EFAULT;
1158 }
1159 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001160 (struct group_filter __user *)optval,
1161 optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001162 release_sock(sk);
1163 return err;
1164 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001165 case IP_MULTICAST_ALL:
1166 val = inet->mc_all;
1167 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001168 case IP_PKTOPTIONS:
1169 {
1170 struct msghdr msg;
1171
1172 release_sock(sk);
1173
1174 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001176
1177 msg.msg_control = optval;
1178 msg.msg_controllen = len;
1179 msg.msg_flags = 0;
1180
1181 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1182 struct in_pktinfo info;
1183
1184 info.ipi_addr.s_addr = inet->rcv_saddr;
1185 info.ipi_spec_dst.s_addr = inet->rcv_saddr;
1186 info.ipi_ifindex = inet->mc_index;
1187 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1188 }
1189 if (inet->cmsg_flags & IP_CMSG_TTL) {
1190 int hlim = inet->mc_ttl;
1191 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1192 }
1193 len -= msg.msg_controllen;
1194 return put_user(len, optlen);
1195 }
1196 case IP_FREEBIND:
1197 val = inet->freebind;
1198 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001199 case IP_TRANSPARENT:
1200 val = inet->transparent;
1201 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001202 default:
1203 release_sock(sk);
1204 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001207
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001208 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 unsigned char ucval = (unsigned char)val;
1210 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001211 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001213 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return -EFAULT;
1215 } else {
1216 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001217 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001219 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return -EFAULT;
1221 }
1222 return 0;
1223}
1224
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001225int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001226 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001227{
1228 int err;
1229
1230 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1231#ifdef CONFIG_NETFILTER
1232 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001233 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1234 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001235 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001236
Jianjun Kong09cb1052008-11-03 00:27:11 -08001237 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001238 return -EFAULT;
1239
1240 lock_sock(sk);
1241 err = nf_getsockopt(sk, PF_INET, optname, optval,
1242 &len);
1243 release_sock(sk);
1244 if (err >= 0)
1245 err = put_user(len, optlen);
1246 return err;
1247 }
1248#endif
1249 return err;
1250}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001251EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001252
1253#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001254int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1255 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001256{
David L Stevens42908c62008-04-29 03:23:22 -07001257 int err;
1258
1259 if (optname == MCAST_MSFILTER)
1260 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1261 ip_getsockopt);
1262
1263 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1264
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001265#ifdef CONFIG_NETFILTER
1266 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001267 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1268 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001269 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001270
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001271 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001272 return -EFAULT;
1273
1274 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001275 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001276 release_sock(sk);
1277 if (err >= 0)
1278 err = put_user(len, optlen);
1279 return err;
1280 }
1281#endif
1282 return err;
1283}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001284EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001285#endif