blob: ab0c9efd1efabe7c003daef3f6d258ec38604817 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * The IP to API glue.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: see ip.c
9 *
10 * Fixes:
11 * Many : Split from ip.c , see ip.c for history.
12 * Martin Mares : TOS setting fixed.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090013 * Alan Cox : Fixed a couple of oopses in Martin's
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * TOS tweaks.
15 * Mike McLagan : Routing by source
16 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/skbuff.h>
22#include <linux/ip.h>
23#include <linux/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020024#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/sock.h>
28#include <net/ip.h>
29#include <net/icmp.h>
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -080030#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/udp.h>
32#include <linux/igmp.h>
33#include <linux/netfilter.h>
34#include <linux/route.h>
35#include <linux/mroute.h>
36#include <net/route.h>
37#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070038#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -080052#define IP_CMSG_ORIGDSTADDR 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
55 * SOL_IP control messages.
56 */
57
58static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
59{
60 struct in_pktinfo info;
Eric Dumazet511c3f92009-06-02 05:14:27 +000061 struct rtable *rt = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070063 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (rt) {
65 info.ipi_ifindex = rt->rt_iif;
66 info.ipi_spec_dst.s_addr = rt->rt_spec_dst;
67 } else {
68 info.ipi_ifindex = 0;
69 info.ipi_spec_dst.s_addr = 0;
70 }
71
72 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
73}
74
75static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
76{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070077 int ttl = ip_hdr(skb)->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
79}
80
81static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
82{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070083 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
87{
88 if (IPCB(skb)->opt.optlen == 0)
89 return;
90
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070091 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
92 ip_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95
96static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
97{
98 unsigned char optbuf[sizeof(struct ip_options) + 40];
Jianjun Kong09cb1052008-11-03 00:27:11 -080099 struct ip_options * opt = (struct ip_options *)optbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 if (IPCB(skb)->opt.optlen == 0)
102 return;
103
104 if (ip_options_echo(opt, skb)) {
105 msg->msg_flags |= MSG_CTRUNC;
106 return;
107 }
108 ip_options_undo(opt);
109
110 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
111}
112
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800113static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
114{
115 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700116 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800117 int err;
118
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700119 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
120 if (err)
121 return;
122
123 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800124 if (err)
125 return;
126
127 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700128 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800129}
130
Harvey Harrison21d1a162008-11-20 01:54:27 -0800131static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800132{
133 struct sockaddr_in sin;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000134 const struct iphdr *iph = ip_hdr(skb);
Harvey Harrison21d1a162008-11-20 01:54:27 -0800135 __be16 *ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800136
137 if (skb_transport_offset(skb) + 4 > skb->len)
138 return;
139
140 /* All current transport protocols have the port numbers in the
141 * first four bytes of the transport header and this function is
142 * written with this assumption in mind.
143 */
144
145 sin.sin_family = AF_INET;
146 sin.sin_addr.s_addr = iph->daddr;
147 sin.sin_port = ports[1];
148 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
149
150 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
154{
155 struct inet_sock *inet = inet_sk(skb->sk);
156 unsigned flags = inet->cmsg_flags;
157
158 /* Ordered by supposed usage frequency */
159 if (flags & 1)
160 ip_cmsg_recv_pktinfo(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700161 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return;
163
164 if (flags & 1)
165 ip_cmsg_recv_ttl(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700166 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return;
168
169 if (flags & 1)
170 ip_cmsg_recv_tos(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700171 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return;
173
174 if (flags & 1)
175 ip_cmsg_recv_opts(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700176 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return;
178
179 if (flags & 1)
180 ip_cmsg_recv_retopts(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700181 if ((flags >>= 1) == 0)
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800182 return;
183
184 if (flags & 1)
185 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800186
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700187 if ((flags >>= 1) == 0)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800188 return;
189 if (flags & 1)
190 ip_cmsg_recv_dstaddr(msg, skb);
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700193EXPORT_SYMBOL(ip_cmsg_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Denis V. Lunev7a6adb92008-03-24 15:30:27 -0700195int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 int err;
198 struct cmsghdr *cmsg;
199
200 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
201 if (!CMSG_OK(msg, cmsg))
202 return -EINVAL;
203 if (cmsg->cmsg_level != SOL_IP)
204 continue;
205 switch (cmsg->cmsg_type) {
206 case IP_RETOPTS:
207 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700208 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
209 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (err)
211 return err;
212 break;
213 case IP_PKTINFO:
214 {
215 struct in_pktinfo *info;
216 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
217 return -EINVAL;
218 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
219 ipc->oif = info->ipi_ifindex;
220 ipc->addr = info->ipi_spec_dst.s_addr;
221 break;
222 }
223 default:
224 return -EINVAL;
225 }
226 }
227 return 0;
228}
229
230
231/* Special input handler for packets caught by router alert option.
232 They are selected only by protocol field, and then processed likely
233 local ones; but only if someone wants them! Otherwise, router
234 not running rsvpd will kill RSVP.
235
236 It is user level problem, what it will make with them.
237 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
238 but receiver should be enough clever f.e. to forward mtrace requests,
239 sent to multicast group to reach destination designated router.
240 */
Eric Dumazet43a951e2010-10-25 03:32:44 +0000241struct ip_ra_chain __rcu *ip_ra_chain;
Eric Dumazet66018502010-06-07 03:12:08 +0000242static DEFINE_SPINLOCK(ip_ra_lock);
243
Eric Dumazet592fcb92010-06-09 16:21:07 +0000244
245static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000246{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000247 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
248
249 sock_put(ra->saved_sk);
250 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000251}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700253int ip_ra_control(struct sock *sk, unsigned char on,
254 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000256 struct ip_ra_chain *ra, *new_ra;
257 struct ip_ra_chain __rcu **rap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000259 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return -EINVAL;
261
262 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
263
Eric Dumazet66018502010-06-07 03:12:08 +0000264 spin_lock_bh(&ip_ra_lock);
Eric Dumazet43a951e2010-10-25 03:32:44 +0000265 for (rap = &ip_ra_chain;
266 (ra = rcu_dereference_protected(*rap,
267 lockdep_is_held(&ip_ra_lock))) != NULL;
268 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (ra->sk == sk) {
270 if (on) {
Eric Dumazet66018502010-06-07 03:12:08 +0000271 spin_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800272 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return -EADDRINUSE;
274 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000275 /* dont let ip_call_ra_chain() use sk again */
276 ra->sk = NULL;
Eric Dumazet66018502010-06-07 03:12:08 +0000277 rcu_assign_pointer(*rap, ra->next);
278 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (ra->destructor)
281 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000282 /*
283 * Delay sock_put(sk) and kfree(ra) after one rcu grace
284 * period. This guarantee ip_call_ra_chain() dont need
285 * to mess with socket refcounts.
286 */
287 ra->saved_sk = sk;
288 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return 0;
290 }
291 }
292 if (new_ra == NULL) {
Eric Dumazet66018502010-06-07 03:12:08 +0000293 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return -ENOBUFS;
295 }
296 new_ra->sk = sk;
297 new_ra->destructor = destructor;
298
299 new_ra->next = ra;
Eric Dumazet66018502010-06-07 03:12:08 +0000300 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 sock_hold(sk);
Eric Dumazet66018502010-06-07 03:12:08 +0000302 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 return 0;
305}
306
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900307void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700308 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct sock_exterr_skb *serr;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 skb = skb_clone(skb, GFP_ATOMIC);
313 if (!skb)
314 return;
315
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900316 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 serr->ee.ee_errno = err;
318 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300319 serr->ee.ee_type = icmp_hdr(skb)->type;
320 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 serr->ee.ee_pad = 0;
322 serr->ee.ee_info = info;
323 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300324 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700325 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 serr->port = port;
327
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300328 if (skb_pull(skb, payload - skb->data) != NULL) {
329 skb_reset_transport_header(skb);
330 if (sock_queue_err_skb(sk, skb) == 0)
331 return;
332 }
333 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Al Viro05790162006-09-27 18:33:40 -0700336void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct inet_sock *inet = inet_sk(sk);
339 struct sock_exterr_skb *serr;
340 struct iphdr *iph;
341 struct sk_buff *skb;
342
343 if (!inet->recverr)
344 return;
345
346 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
347 if (!skb)
348 return;
349
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300350 skb_put(skb, sizeof(struct iphdr));
351 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700352 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 iph->daddr = daddr;
354
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900355 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 serr->ee.ee_errno = err;
357 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900358 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 serr->ee.ee_code = 0;
360 serr->ee.ee_pad = 0;
361 serr->ee.ee_info = info;
362 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700363 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 serr->port = port;
365
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700366 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300367 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if (sock_queue_err_skb(sk, skb))
370 kfree_skb(skb);
371}
372
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900373/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * Handle MSG_ERRQUEUE
375 */
376int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
377{
378 struct sock_exterr_skb *serr;
379 struct sk_buff *skb, *skb2;
380 struct sockaddr_in *sin;
381 struct {
382 struct sock_extended_err ee;
383 struct sockaddr_in offender;
384 } errhdr;
385 int err;
386 int copied;
387
388 err = -EAGAIN;
389 skb = skb_dequeue(&sk->sk_error_queue);
390 if (skb == NULL)
391 goto out;
392
393 copied = skb->len;
394 if (copied > len) {
395 msg->msg_flags |= MSG_TRUNC;
396 copied = len;
397 }
398 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
399 if (err)
400 goto out_free_skb;
401
402 sock_recv_timestamp(msg, sk, skb);
403
404 serr = SKB_EXT_ERR(skb);
405
406 sin = (struct sockaddr_in *)msg->msg_name;
407 if (sin) {
408 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700409 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
410 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 sin->sin_port = serr->port;
412 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
413 }
414
415 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
416 sin = &errhdr.offender;
417 sin->sin_family = AF_UNSPEC;
418 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
419 struct inet_sock *inet = inet_sk(sk);
420
421 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700422 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 sin->sin_port = 0;
424 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
425 if (inet->cmsg_flags)
426 ip_cmsg_recv(msg, skb);
427 }
428
429 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
430
431 /* Now we could try to dump offended packet options */
432
433 msg->msg_flags |= MSG_ERRQUEUE;
434 err = copied;
435
436 /* Reset and regenerate socket error */
Herbert Xue0f9f852005-06-18 22:56:18 -0700437 spin_lock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 sk->sk_err = 0;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700439 skb2 = skb_peek(&sk->sk_error_queue);
440 if (skb2 != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
Herbert Xue0f9f852005-06-18 22:56:18 -0700442 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 sk->sk_error_report(sk);
444 } else
Herbert Xue0f9f852005-06-18 22:56:18 -0700445 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900447out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 kfree_skb(skb);
449out:
450 return err;
451}
452
453
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000454static void opt_kfree_rcu(struct rcu_head *head)
455{
456 kfree(container_of(head, struct ip_options_rcu, rcu));
457}
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700460 * Socket option code for IP. This is the end of the line after any
461 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
463
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800464static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700465 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct inet_sock *inet = inet_sk(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800468 int val = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900470 if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
Stephen Hemminger132adf52007-03-08 20:44:43 -0800471 (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
472 (1<<IP_RETOPTS) | (1<<IP_TOS) |
473 (1<<IP_TTL) | (1<<IP_HDRINCL) |
474 (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
475 (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
Stephen Hemmingerd218d112010-01-11 16:28:01 -0800476 (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT) |
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000477 (1<<IP_MINTTL) | (1<<IP_NODEFRAG))) ||
Stephen Hemminger132adf52007-03-08 20:44:43 -0800478 optname == IP_MULTICAST_TTL ||
Nivedita Singhvif771bef2009-05-28 07:00:46 +0000479 optname == IP_MULTICAST_ALL ||
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800480 optname == IP_MULTICAST_LOOP ||
481 optname == IP_RECVORIGDSTADDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (optlen >= sizeof(int)) {
483 if (get_user(val, (int __user *) optval))
484 return -EFAULT;
485 } else if (optlen >= sizeof(char)) {
486 unsigned char ucval;
487
488 if (get_user(ucval, (unsigned char __user *) optval))
489 return -EFAULT;
490 val = (int) ucval;
491 }
492 }
493
494 /* If optlen==0, it is equivalent to val == 0 */
495
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800496 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800497 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 err = 0;
500 lock_sock(sk);
501
502 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800503 case IP_OPTIONS:
504 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000505 struct ip_options_rcu *old, *opt = NULL;
506
roel kluin65a1c4f2009-10-23 05:59:21 +0000507 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800508 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900509 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700510 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800511 if (err)
512 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000513 old = rcu_dereference_protected(inet->inet_opt,
514 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800515 if (inet->is_icsk) {
516 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800518 if (sk->sk_family == PF_INET ||
519 (!((1 << sk->sk_state) &
520 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000521 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000523 if (old)
524 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800525 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000526 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800527 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000532 rcu_assign_pointer(inet->inet_opt, opt);
533 if (old)
534 call_rcu(&old->rcu, opt_kfree_rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800535 break;
536 }
537 case IP_PKTINFO:
538 if (val)
539 inet->cmsg_flags |= IP_CMSG_PKTINFO;
540 else
541 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
542 break;
543 case IP_RECVTTL:
544 if (val)
545 inet->cmsg_flags |= IP_CMSG_TTL;
546 else
547 inet->cmsg_flags &= ~IP_CMSG_TTL;
548 break;
549 case IP_RECVTOS:
550 if (val)
551 inet->cmsg_flags |= IP_CMSG_TOS;
552 else
553 inet->cmsg_flags &= ~IP_CMSG_TOS;
554 break;
555 case IP_RECVOPTS:
556 if (val)
557 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
558 else
559 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
560 break;
561 case IP_RETOPTS:
562 if (val)
563 inet->cmsg_flags |= IP_CMSG_RETOPTS;
564 else
565 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
566 break;
567 case IP_PASSSEC:
568 if (val)
569 inet->cmsg_flags |= IP_CMSG_PASSSEC;
570 else
571 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
572 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800573 case IP_RECVORIGDSTADDR:
574 if (val)
575 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
576 else
577 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
578 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800579 case IP_TOS: /* This sets both TOS and Precedence */
580 if (sk->sk_type == SOCK_STREAM) {
581 val &= ~3;
582 val |= inet->tos & 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800584 if (inet->tos != val) {
585 inet->tos = val;
586 sk->sk_priority = rt_tos2priority(val);
587 sk_dst_reset(sk);
588 }
589 break;
590 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700591 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800592 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700593 if (val != -1 && (val < 0 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800594 goto e_inval;
595 inet->uc_ttl = val;
596 break;
597 case IP_HDRINCL:
598 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800601 }
602 inet->hdrincl = val ? 1 : 0;
603 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000604 case IP_NODEFRAG:
605 if (sk->sk_type != SOCK_RAW) {
606 err = -ENOPROTOOPT;
607 break;
608 }
609 inet->nodefrag = val ? 1 : 0;
610 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800611 case IP_MTU_DISCOVER:
John Dykstra0eae7502009-10-19 21:53:53 -0700612 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800613 goto e_inval;
614 inet->pmtudisc = val;
615 break;
616 case IP_RECVERR:
617 inet->recverr = !!val;
618 if (!val)
619 skb_queue_purge(&sk->sk_error_queue);
620 break;
621 case IP_MULTICAST_TTL:
622 if (sk->sk_type == SOCK_STREAM)
623 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700624 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800625 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800626 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800627 val = 1;
628 if (val < 0 || val > 255)
629 goto e_inval;
630 inet->mc_ttl = val;
631 break;
632 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700633 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800634 goto e_inval;
635 inet->mc_loop = !!val;
636 break;
637 case IP_MULTICAST_IF:
638 {
639 struct ip_mreqn mreq;
640 struct net_device *dev = NULL;
641
642 if (sk->sk_type == SOCK_STREAM)
643 goto e_inval;
644 /*
645 * Check the arguments are allowable
646 */
647
Shan Wei09159212009-09-22 15:41:10 +0000648 if (optlen < sizeof(struct in_addr))
649 goto e_inval;
650
Stephen Hemminger132adf52007-03-08 20:44:43 -0800651 err = -EFAULT;
652 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800653 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800654 break;
655 } else {
656 memset(&mreq, 0, sizeof(mreq));
657 if (optlen >= sizeof(struct in_addr) &&
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700658 copy_from_user(&mreq.imr_address, optval,
659 sizeof(struct in_addr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800660 break;
661 }
662
663 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700664 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800665 inet->mc_index = 0;
666 inet->mc_addr = 0;
667 err = 0;
668 break;
669 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900670 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000671 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800672 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800673 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000674 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800675
676
677 err = -EADDRNOTAVAIL;
678 if (!dev)
679 break;
Eric Dumazet55b80502009-10-19 06:41:58 +0000680 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800681
682 err = -EINVAL;
683 if (sk->sk_bound_dev_if &&
684 mreq.imr_ifindex != sk->sk_bound_dev_if)
685 break;
686
687 inet->mc_index = mreq.imr_ifindex;
688 inet->mc_addr = mreq.imr_address.s_addr;
689 err = 0;
690 break;
691 }
692
693 case IP_ADD_MEMBERSHIP:
694 case IP_DROP_MEMBERSHIP:
695 {
696 struct ip_mreqn mreq;
697
Flavio Leitnera96fb492007-08-24 22:16:39 -0700698 err = -EPROTO;
699 if (inet_sk(sk)->is_icsk)
700 break;
701
Stephen Hemminger132adf52007-03-08 20:44:43 -0800702 if (optlen < sizeof(struct ip_mreq))
703 goto e_inval;
704 err = -EFAULT;
705 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800706 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800707 break;
708 } else {
709 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800710 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800711 break;
712 }
713
714 if (optname == IP_ADD_MEMBERSHIP)
715 err = ip_mc_join_group(sk, &mreq);
716 else
717 err = ip_mc_leave_group(sk, &mreq);
718 break;
719 }
720 case IP_MSFILTER:
721 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800722 struct ip_msfilter *msf;
723
724 if (optlen < IP_MSFILTER_SIZE(0))
725 goto e_inval;
726 if (optlen > sysctl_optmem_max) {
727 err = -ENOBUFS;
728 break;
729 }
730 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700731 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800732 err = -ENOBUFS;
733 break;
734 }
735 err = -EFAULT;
736 if (copy_from_user(msf, optval, optlen)) {
737 kfree(msf);
738 break;
739 }
740 /* numsrc >= (1G-4) overflow in 32 bits */
741 if (msf->imsf_numsrc >= 0x3ffffffcU ||
742 msf->imsf_numsrc > sysctl_igmp_max_msf) {
743 kfree(msf);
744 err = -ENOBUFS;
745 break;
746 }
747 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
748 kfree(msf);
749 err = -EINVAL;
750 break;
751 }
752 err = ip_mc_msfilter(sk, msf, 0);
753 kfree(msf);
754 break;
755 }
756 case IP_BLOCK_SOURCE:
757 case IP_UNBLOCK_SOURCE:
758 case IP_ADD_SOURCE_MEMBERSHIP:
759 case IP_DROP_SOURCE_MEMBERSHIP:
760 {
761 struct ip_mreq_source mreqs;
762 int omode, add;
763
764 if (optlen != sizeof(struct ip_mreq_source))
765 goto e_inval;
766 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
767 err = -EFAULT;
768 break;
769 }
770 if (optname == IP_BLOCK_SOURCE) {
771 omode = MCAST_EXCLUDE;
772 add = 1;
773 } else if (optname == IP_UNBLOCK_SOURCE) {
774 omode = MCAST_EXCLUDE;
775 add = 0;
776 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
777 struct ip_mreqn mreq;
778
779 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
780 mreq.imr_address.s_addr = mreqs.imr_interface;
781 mreq.imr_ifindex = 0;
782 err = ip_mc_join_group(sk, &mreq);
783 if (err && err != -EADDRINUSE)
784 break;
785 omode = MCAST_INCLUDE;
786 add = 1;
787 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
788 omode = MCAST_INCLUDE;
789 add = 0;
790 }
791 err = ip_mc_source(add, omode, sk, &mreqs, 0);
792 break;
793 }
794 case MCAST_JOIN_GROUP:
795 case MCAST_LEAVE_GROUP:
796 {
797 struct group_req greq;
798 struct sockaddr_in *psin;
799 struct ip_mreqn mreq;
800
801 if (optlen < sizeof(struct group_req))
802 goto e_inval;
803 err = -EFAULT;
804 if (copy_from_user(&greq, optval, sizeof(greq)))
805 break;
806 psin = (struct sockaddr_in *)&greq.gr_group;
807 if (psin->sin_family != AF_INET)
808 goto e_inval;
809 memset(&mreq, 0, sizeof(mreq));
810 mreq.imr_multiaddr = psin->sin_addr;
811 mreq.imr_ifindex = greq.gr_interface;
812
813 if (optname == MCAST_JOIN_GROUP)
814 err = ip_mc_join_group(sk, &mreq);
815 else
816 err = ip_mc_leave_group(sk, &mreq);
817 break;
818 }
819 case MCAST_JOIN_SOURCE_GROUP:
820 case MCAST_LEAVE_SOURCE_GROUP:
821 case MCAST_BLOCK_SOURCE:
822 case MCAST_UNBLOCK_SOURCE:
823 {
824 struct group_source_req greqs;
825 struct ip_mreq_source mreqs;
826 struct sockaddr_in *psin;
827 int omode, add;
828
829 if (optlen != sizeof(struct group_source_req))
830 goto e_inval;
831 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
832 err = -EFAULT;
833 break;
834 }
835 if (greqs.gsr_group.ss_family != AF_INET ||
836 greqs.gsr_source.ss_family != AF_INET) {
837 err = -EADDRNOTAVAIL;
838 break;
839 }
840 psin = (struct sockaddr_in *)&greqs.gsr_group;
841 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
842 psin = (struct sockaddr_in *)&greqs.gsr_source;
843 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
844 mreqs.imr_interface = 0; /* use index for mc_source */
845
846 if (optname == MCAST_BLOCK_SOURCE) {
847 omode = MCAST_EXCLUDE;
848 add = 1;
849 } else if (optname == MCAST_UNBLOCK_SOURCE) {
850 omode = MCAST_EXCLUDE;
851 add = 0;
852 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
853 struct ip_mreqn mreq;
854
855 psin = (struct sockaddr_in *)&greqs.gsr_group;
856 mreq.imr_multiaddr = psin->sin_addr;
857 mreq.imr_address.s_addr = 0;
858 mreq.imr_ifindex = greqs.gsr_interface;
859 err = ip_mc_join_group(sk, &mreq);
860 if (err && err != -EADDRINUSE)
861 break;
862 greqs.gsr_interface = mreq.imr_ifindex;
863 omode = MCAST_INCLUDE;
864 add = 1;
865 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
866 omode = MCAST_INCLUDE;
867 add = 0;
868 }
869 err = ip_mc_source(add, omode, sk, &mreqs,
870 greqs.gsr_interface);
871 break;
872 }
873 case MCAST_MSFILTER:
874 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800875 struct sockaddr_in *psin;
876 struct ip_msfilter *msf = NULL;
877 struct group_filter *gsf = NULL;
878 int msize, i, ifindex;
879
880 if (optlen < GROUP_FILTER_SIZE(0))
881 goto e_inval;
882 if (optlen > sysctl_optmem_max) {
883 err = -ENOBUFS;
884 break;
885 }
Jianjun Kong09cb1052008-11-03 00:27:11 -0800886 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700887 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800888 err = -ENOBUFS;
889 break;
890 }
891 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700892 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800893 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700894
Stephen Hemminger132adf52007-03-08 20:44:43 -0800895 /* numsrc >= (4G-140)/128 overflow in 32 bits */
896 if (gsf->gf_numsrc >= 0x1ffffff ||
897 gsf->gf_numsrc > sysctl_igmp_max_msf) {
898 err = -ENOBUFS;
899 goto mc_msf_out;
900 }
901 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
902 err = -EINVAL;
903 goto mc_msf_out;
904 }
905 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800906 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700907 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800908 err = -ENOBUFS;
909 goto mc_msf_out;
910 }
911 ifindex = gsf->gf_interface;
912 psin = (struct sockaddr_in *)&gsf->gf_group;
913 if (psin->sin_family != AF_INET) {
914 err = -EADDRNOTAVAIL;
915 goto mc_msf_out;
916 }
917 msf->imsf_multiaddr = psin->sin_addr.s_addr;
918 msf->imsf_interface = 0;
919 msf->imsf_fmode = gsf->gf_fmode;
920 msf->imsf_numsrc = gsf->gf_numsrc;
921 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700922 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800923 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
924
925 if (psin->sin_family != AF_INET)
926 goto mc_msf_out;
927 msf->imsf_slist[i] = psin->sin_addr.s_addr;
928 }
929 kfree(gsf);
930 gsf = NULL;
931
932 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700933mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -0800934 kfree(msf);
935 kfree(gsf);
936 break;
937 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +0000938 case IP_MULTICAST_ALL:
939 if (optlen < 1)
940 goto e_inval;
941 if (val != 0 && val != 1)
942 goto e_inval;
943 inet->mc_all = val;
944 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800945 case IP_ROUTER_ALERT:
946 err = ip_ra_control(sk, val ? 1 : 0, NULL);
947 break;
948
949 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700950 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800951 goto e_inval;
952 inet->freebind = !!val;
953 break;
954
955 case IP_IPSEC_POLICY:
956 case IP_XFRM_POLICY:
957 err = -EPERM;
958 if (!capable(CAP_NET_ADMIN))
959 break;
960 err = xfrm_user_policy(sk, optname, optval, optlen);
961 break;
962
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -0700963 case IP_TRANSPARENT:
964 if (!capable(CAP_NET_ADMIN)) {
965 err = -EPERM;
966 break;
967 }
968 if (optlen < 1)
969 goto e_inval;
970 inet->transparent = !!val;
971 break;
972
Stephen Hemmingerd218d112010-01-11 16:28:01 -0800973 case IP_MINTTL:
974 if (optlen < 1)
975 goto e_inval;
976 if (val < 0 || val > 255)
977 goto e_inval;
978 inet->min_ttl = val;
979 break;
980
Stephen Hemminger132adf52007-03-08 20:44:43 -0800981 default:
982 err = -ENOPROTOOPT;
983 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 release_sock(sk);
986 return err;
987
988e_inval:
989 release_sock(sk);
990 return -EINVAL;
991}
992
Eric Dumazetf84af322010-04-28 15:31:51 -0700993/**
994 * ip_queue_rcv_skb - Queue an skb into sock receive queue
995 * @sk: socket
996 * @skb: buffer
997 *
998 * Queues an skb into socket receive queue. If IP_CMSG_PKTINFO option
999 * is not set, we drop skb dst entry now, while dst cache line is hot.
1000 */
1001int ip_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1002{
1003 if (!(inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO))
1004 skb_dst_drop(skb);
1005 return sock_queue_rcv_skb(sk, skb);
1006}
1007EXPORT_SYMBOL(ip_queue_rcv_skb);
1008
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001009int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001010 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001011{
1012 int err;
1013
1014 if (level != SOL_IP)
1015 return -ENOPROTOOPT;
1016
1017 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1018#ifdef CONFIG_NETFILTER
1019 /* we need to exclude all possible ENOPROTOOPTs except default case */
1020 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001021 optname != IP_IPSEC_POLICY &&
1022 optname != IP_XFRM_POLICY &&
1023 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001024 lock_sock(sk);
1025 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1026 release_sock(sk);
1027 }
1028#endif
1029 return err;
1030}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001031EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001032
1033#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001034int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001035 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001036{
1037 int err;
1038
1039 if (level != SOL_IP)
1040 return -ENOPROTOOPT;
1041
David L Stevensdae50292008-04-27 01:06:07 -07001042 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1043 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1044 ip_setsockopt);
1045
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001046 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1047#ifdef CONFIG_NETFILTER
1048 /* we need to exclude all possible ENOPROTOOPTs except default case */
1049 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001050 optname != IP_IPSEC_POLICY &&
1051 optname != IP_XFRM_POLICY &&
1052 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001053 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001054 err = compat_nf_setsockopt(sk, PF_INET, optname,
1055 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001056 release_sock(sk);
1057 }
1058#endif
1059 return err;
1060}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001061EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001062#endif
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001065 * Get the options. Note for future reference. The GET of IP options gets
1066 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 */
1068
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001069static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001070 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 struct inet_sock *inet = inet_sk(sk);
1073 int val;
1074 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001075
Stephen Hemminger132adf52007-03-08 20:44:43 -08001076 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -EOPNOTSUPP;
1078
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001079 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001080 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Jianjun Kong09cb1052008-11-03 00:27:11 -08001082 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001084 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 lock_sock(sk);
1088
Stephen Hemminger132adf52007-03-08 20:44:43 -08001089 switch (optname) {
1090 case IP_OPTIONS:
1091 {
1092 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001093 struct ip_options *opt = (struct ip_options *)optbuf;
1094 struct ip_options_rcu *inet_opt;
1095
1096 inet_opt = rcu_dereference_protected(inet->inet_opt,
1097 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001098 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001099 if (inet_opt)
1100 memcpy(optbuf, &inet_opt->opt,
1101 sizeof(struct ip_options) +
1102 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001103 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Stephen Hemminger132adf52007-03-08 20:44:43 -08001105 if (opt->optlen == 0)
1106 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Stephen Hemminger132adf52007-03-08 20:44:43 -08001108 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Stephen Hemminger132adf52007-03-08 20:44:43 -08001110 len = min_t(unsigned int, len, opt->optlen);
1111 if (put_user(len, optlen))
1112 return -EFAULT;
1113 if (copy_to_user(optval, opt->__data, len))
1114 return -EFAULT;
1115 return 0;
1116 }
1117 case IP_PKTINFO:
1118 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1119 break;
1120 case IP_RECVTTL:
1121 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1122 break;
1123 case IP_RECVTOS:
1124 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1125 break;
1126 case IP_RECVOPTS:
1127 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1128 break;
1129 case IP_RETOPTS:
1130 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1131 break;
1132 case IP_PASSSEC:
1133 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1134 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001135 case IP_RECVORIGDSTADDR:
1136 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1137 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001138 case IP_TOS:
1139 val = inet->tos;
1140 break;
1141 case IP_TTL:
1142 val = (inet->uc_ttl == -1 ?
1143 sysctl_ip_default_ttl :
1144 inet->uc_ttl);
1145 break;
1146 case IP_HDRINCL:
1147 val = inet->hdrincl;
1148 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001149 case IP_NODEFRAG:
1150 val = inet->nodefrag;
1151 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001152 case IP_MTU_DISCOVER:
1153 val = inet->pmtudisc;
1154 break;
1155 case IP_MTU:
1156 {
1157 struct dst_entry *dst;
1158 val = 0;
1159 dst = sk_dst_get(sk);
1160 if (dst) {
1161 val = dst_mtu(dst);
1162 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001164 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001166 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001168 break;
1169 }
1170 case IP_RECVERR:
1171 val = inet->recverr;
1172 break;
1173 case IP_MULTICAST_TTL:
1174 val = inet->mc_ttl;
1175 break;
1176 case IP_MULTICAST_LOOP:
1177 val = inet->mc_loop;
1178 break;
1179 case IP_MULTICAST_IF:
1180 {
1181 struct in_addr addr;
1182 len = min_t(unsigned int, len, sizeof(struct in_addr));
1183 addr.s_addr = inet->mc_addr;
1184 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Stephen Hemminger132adf52007-03-08 20:44:43 -08001186 if (put_user(len, optlen))
1187 return -EFAULT;
1188 if (copy_to_user(optval, &addr, len))
1189 return -EFAULT;
1190 return 0;
1191 }
1192 case IP_MSFILTER:
1193 {
1194 struct ip_msfilter msf;
1195 int err;
1196
1197 if (len < IP_MSFILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001199 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001201 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001203 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001205 err = ip_mc_msfget(sk, &msf,
1206 (struct ip_msfilter __user *)optval, optlen);
1207 release_sock(sk);
1208 return err;
1209 }
1210 case MCAST_MSFILTER:
1211 {
1212 struct group_filter gsf;
1213 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Stephen Hemminger132adf52007-03-08 20:44:43 -08001215 if (len < GROUP_FILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001217 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001219 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001221 return -EFAULT;
1222 }
1223 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001224 (struct group_filter __user *)optval,
1225 optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001226 release_sock(sk);
1227 return err;
1228 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001229 case IP_MULTICAST_ALL:
1230 val = inet->mc_all;
1231 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001232 case IP_PKTOPTIONS:
1233 {
1234 struct msghdr msg;
1235
1236 release_sock(sk);
1237
1238 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001240
1241 msg.msg_control = optval;
1242 msg.msg_controllen = len;
1243 msg.msg_flags = 0;
1244
1245 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1246 struct in_pktinfo info;
1247
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001248 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1249 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001250 info.ipi_ifindex = inet->mc_index;
1251 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1252 }
1253 if (inet->cmsg_flags & IP_CMSG_TTL) {
1254 int hlim = inet->mc_ttl;
1255 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1256 }
1257 len -= msg.msg_controllen;
1258 return put_user(len, optlen);
1259 }
1260 case IP_FREEBIND:
1261 val = inet->freebind;
1262 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001263 case IP_TRANSPARENT:
1264 val = inet->transparent;
1265 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001266 case IP_MINTTL:
1267 val = inet->min_ttl;
1268 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001269 default:
1270 release_sock(sk);
1271 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001274
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001275 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 unsigned char ucval = (unsigned char)val;
1277 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001278 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001280 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return -EFAULT;
1282 } else {
1283 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001284 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001286 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return -EFAULT;
1288 }
1289 return 0;
1290}
1291
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001292int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001293 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001294{
1295 int err;
1296
1297 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1298#ifdef CONFIG_NETFILTER
1299 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001300 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1301 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001302 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001303
Jianjun Kong09cb1052008-11-03 00:27:11 -08001304 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001305 return -EFAULT;
1306
1307 lock_sock(sk);
1308 err = nf_getsockopt(sk, PF_INET, optname, optval,
1309 &len);
1310 release_sock(sk);
1311 if (err >= 0)
1312 err = put_user(len, optlen);
1313 return err;
1314 }
1315#endif
1316 return err;
1317}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001318EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001319
1320#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001321int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1322 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001323{
David L Stevens42908c62008-04-29 03:23:22 -07001324 int err;
1325
1326 if (optname == MCAST_MSFILTER)
1327 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1328 ip_getsockopt);
1329
1330 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1331
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001332#ifdef CONFIG_NETFILTER
1333 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001334 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1335 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001336 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001337
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001338 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001339 return -EFAULT;
1340
1341 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001342 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001343 release_sock(sk);
1344 if (err >= 0)
1345 err = put_user(len, optlen);
1346 return err;
1347 }
1348#endif
1349 return err;
1350}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001351EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001352#endif