blob: 56e34457ac070922ba7467f8c4fcba94d22827d7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * The IP to API glue.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: see ip.c
9 *
10 * Fixes:
11 * Many : Split from ip.c , see ip.c for history.
12 * Martin Mares : TOS setting fixed.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090013 * Alan Cox : Fixed a couple of oopses in Martin's
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * TOS tweaks.
15 * Mike McLagan : Routing by source
16 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/skbuff.h>
22#include <linux/ip.h>
23#include <linux/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020024#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/sock.h>
28#include <net/ip.h>
29#include <net/icmp.h>
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -080030#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/udp.h>
32#include <linux/igmp.h>
33#include <linux/netfilter.h>
34#include <linux/route.h>
35#include <linux/mroute.h>
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -040036#include <net/inet_ecn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <net/route.h>
38#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070039#include <net/compat.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000040#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <net/transp_v6.h>
42#endif
David S. Miller35ebf652012-06-28 03:59:11 -070043#include <net/ip_fib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <linux/errqueue.h>
46#include <asm/uaccess.h>
47
48#define IP_CMSG_PKTINFO 1
49#define IP_CMSG_TTL 2
50#define IP_CMSG_TOS 4
51#define IP_CMSG_RECVOPTS 8
52#define IP_CMSG_RETOPTS 16
Catherine Zhang2c7946a2006-03-20 22:41:23 -080053#define IP_CMSG_PASSSEC 32
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -080054#define IP_CMSG_ORIGDSTADDR 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * SOL_IP control messages.
58 */
Eric Dumazetd826eb12011-11-09 07:24:35 +000059#define PKTINFO_SKB_CB(__skb) ((struct in_pktinfo *)((__skb)->cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
62{
Eric Dumazetd826eb12011-11-09 07:24:35 +000063 struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070065 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
68}
69
70static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
71{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070072 int ttl = ip_hdr(skb)->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
74}
75
76static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
77{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070078 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
82{
83 if (IPCB(skb)->opt.optlen == 0)
84 return;
85
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070086 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
87 ip_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90
91static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
92{
93 unsigned char optbuf[sizeof(struct ip_options) + 40];
Daniel Baluta5e73ea12012-04-15 01:34:41 +000094 struct ip_options *opt = (struct ip_options *)optbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 if (IPCB(skb)->opt.optlen == 0)
97 return;
98
99 if (ip_options_echo(opt, skb)) {
100 msg->msg_flags |= MSG_CTRUNC;
101 return;
102 }
103 ip_options_undo(opt);
104
105 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
106}
107
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800108static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
109{
110 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700111 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800112 int err;
113
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700114 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
115 if (err)
116 return;
117
118 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800119 if (err)
120 return;
121
122 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700123 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800124}
125
Harvey Harrison21d1a162008-11-20 01:54:27 -0800126static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800127{
128 struct sockaddr_in sin;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000129 const struct iphdr *iph = ip_hdr(skb);
Harvey Harrison21d1a162008-11-20 01:54:27 -0800130 __be16 *ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800131
132 if (skb_transport_offset(skb) + 4 > skb->len)
133 return;
134
135 /* All current transport protocols have the port numbers in the
136 * first four bytes of the transport header and this function is
137 * written with this assumption in mind.
138 */
139
140 sin.sin_family = AF_INET;
141 sin.sin_addr.s_addr = iph->daddr;
142 sin.sin_port = ports[1];
143 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
144
145 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
149{
150 struct inet_sock *inet = inet_sk(skb->sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000151 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 /* Ordered by supposed usage frequency */
154 if (flags & 1)
155 ip_cmsg_recv_pktinfo(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700156 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return;
158
159 if (flags & 1)
160 ip_cmsg_recv_ttl(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_tos(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_opts(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_retopts(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700176 if ((flags >>= 1) == 0)
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800177 return;
178
179 if (flags & 1)
180 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800181
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700182 if ((flags >>= 1) == 0)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800183 return;
184 if (flags & 1)
185 ip_cmsg_recv_dstaddr(msg, skb);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700188EXPORT_SYMBOL(ip_cmsg_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Denis V. Lunev7a6adb92008-03-24 15:30:27 -0700190int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Francesco Fuscof02db312013-09-24 15:43:08 +0200192 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct cmsghdr *cmsg;
194
195 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
196 if (!CMSG_OK(msg, cmsg))
197 return -EINVAL;
198 if (cmsg->cmsg_level != SOL_IP)
199 continue;
200 switch (cmsg->cmsg_type) {
201 case IP_RETOPTS:
202 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700203 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
204 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (err)
206 return err;
207 break;
208 case IP_PKTINFO:
209 {
210 struct in_pktinfo *info;
211 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
212 return -EINVAL;
213 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
214 ipc->oif = info->ipi_ifindex;
215 ipc->addr = info->ipi_spec_dst.s_addr;
216 break;
217 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200218 case IP_TTL:
219 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
220 return -EINVAL;
221 val = *(int *)CMSG_DATA(cmsg);
222 if (val < 1 || val > 255)
223 return -EINVAL;
224 ipc->ttl = val;
225 break;
226 case IP_TOS:
227 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
228 return -EINVAL;
229 val = *(int *)CMSG_DATA(cmsg);
230 if (val < 0 || val > 255)
231 return -EINVAL;
232 ipc->tos = val;
233 ipc->priority = rt_tos2priority(ipc->tos);
234 break;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 default:
237 return -EINVAL;
238 }
239 }
240 return 0;
241}
242
243
244/* Special input handler for packets caught by router alert option.
245 They are selected only by protocol field, and then processed likely
246 local ones; but only if someone wants them! Otherwise, router
247 not running rsvpd will kill RSVP.
248
249 It is user level problem, what it will make with them.
250 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
251 but receiver should be enough clever f.e. to forward mtrace requests,
252 sent to multicast group to reach destination designated router.
253 */
Eric Dumazet43a951e2010-10-25 03:32:44 +0000254struct ip_ra_chain __rcu *ip_ra_chain;
Eric Dumazet66018502010-06-07 03:12:08 +0000255static DEFINE_SPINLOCK(ip_ra_lock);
256
Eric Dumazet592fcb92010-06-09 16:21:07 +0000257
258static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000259{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000260 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
261
262 sock_put(ra->saved_sk);
263 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700266int ip_ra_control(struct sock *sk, unsigned char on,
267 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000269 struct ip_ra_chain *ra, *new_ra;
270 struct ip_ra_chain __rcu **rap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000272 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return -EINVAL;
274
275 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
276
Eric Dumazet66018502010-06-07 03:12:08 +0000277 spin_lock_bh(&ip_ra_lock);
Eric Dumazet43a951e2010-10-25 03:32:44 +0000278 for (rap = &ip_ra_chain;
279 (ra = rcu_dereference_protected(*rap,
280 lockdep_is_held(&ip_ra_lock))) != NULL;
281 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (ra->sk == sk) {
283 if (on) {
Eric Dumazet66018502010-06-07 03:12:08 +0000284 spin_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800285 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return -EADDRINUSE;
287 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000288 /* dont let ip_call_ra_chain() use sk again */
289 ra->sk = NULL;
Eric Dumazet66018502010-06-07 03:12:08 +0000290 rcu_assign_pointer(*rap, ra->next);
291 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 if (ra->destructor)
294 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000295 /*
296 * Delay sock_put(sk) and kfree(ra) after one rcu grace
297 * period. This guarantee ip_call_ra_chain() dont need
298 * to mess with socket refcounts.
299 */
300 ra->saved_sk = sk;
301 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return 0;
303 }
304 }
305 if (new_ra == NULL) {
Eric Dumazet66018502010-06-07 03:12:08 +0000306 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return -ENOBUFS;
308 }
309 new_ra->sk = sk;
310 new_ra->destructor = destructor;
311
312 new_ra->next = ra;
Eric Dumazet66018502010-06-07 03:12:08 +0000313 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 sock_hold(sk);
Eric Dumazet66018502010-06-07 03:12:08 +0000315 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 return 0;
318}
319
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900320void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700321 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct sock_exterr_skb *serr;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 skb = skb_clone(skb, GFP_ATOMIC);
326 if (!skb)
327 return;
328
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900329 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 serr->ee.ee_errno = err;
331 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300332 serr->ee.ee_type = icmp_hdr(skb)->type;
333 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 serr->ee.ee_pad = 0;
335 serr->ee.ee_info = info;
336 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300337 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700338 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 serr->port = port;
340
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300341 if (skb_pull(skb, payload - skb->data) != NULL) {
342 skb_reset_transport_header(skb);
343 if (sock_queue_err_skb(sk, skb) == 0)
344 return;
345 }
346 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Al Viro05790162006-09-27 18:33:40 -0700349void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct inet_sock *inet = inet_sk(sk);
352 struct sock_exterr_skb *serr;
353 struct iphdr *iph;
354 struct sk_buff *skb;
355
356 if (!inet->recverr)
357 return;
358
359 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
360 if (!skb)
361 return;
362
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300363 skb_put(skb, sizeof(struct iphdr));
364 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700365 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 iph->daddr = daddr;
367
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900368 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 serr->ee.ee_errno = err;
370 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900371 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 serr->ee.ee_code = 0;
373 serr->ee.ee_pad = 0;
374 serr->ee.ee_info = info;
375 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700376 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 serr->port = port;
378
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700379 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300380 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 if (sock_queue_err_skb(sk, skb))
383 kfree_skb(skb);
384}
385
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900386/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * Handle MSG_ERRQUEUE
388 */
389int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
390{
391 struct sock_exterr_skb *serr;
392 struct sk_buff *skb, *skb2;
393 struct sockaddr_in *sin;
394 struct {
395 struct sock_extended_err ee;
396 struct sockaddr_in offender;
397 } errhdr;
398 int err;
399 int copied;
400
401 err = -EAGAIN;
402 skb = skb_dequeue(&sk->sk_error_queue);
403 if (skb == NULL)
404 goto out;
405
406 copied = skb->len;
407 if (copied > len) {
408 msg->msg_flags |= MSG_TRUNC;
409 copied = len;
410 }
411 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
412 if (err)
413 goto out_free_skb;
414
415 sock_recv_timestamp(msg, sk, skb);
416
417 serr = SKB_EXT_ERR(skb);
418
419 sin = (struct sockaddr_in *)msg->msg_name;
420 if (sin) {
421 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700422 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
423 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 sin->sin_port = serr->port;
425 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
426 }
427
428 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
429 sin = &errhdr.offender;
430 sin->sin_family = AF_UNSPEC;
431 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
432 struct inet_sock *inet = inet_sk(sk);
433
434 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700435 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 sin->sin_port = 0;
437 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
438 if (inet->cmsg_flags)
439 ip_cmsg_recv(msg, skb);
440 }
441
442 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
443
444 /* Now we could try to dump offended packet options */
445
446 msg->msg_flags |= MSG_ERRQUEUE;
447 err = copied;
448
449 /* Reset and regenerate socket error */
Herbert Xue0f9f852005-06-18 22:56:18 -0700450 spin_lock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 sk->sk_err = 0;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700452 skb2 = skb_peek(&sk->sk_error_queue);
453 if (skb2 != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
Herbert Xue0f9f852005-06-18 22:56:18 -0700455 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 sk->sk_error_report(sk);
457 } else
Herbert Xue0f9f852005-06-18 22:56:18 -0700458 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900460out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 kfree_skb(skb);
462out:
463 return err;
464}
465
466
467/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700468 * Socket option code for IP. This is the end of the line after any
469 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 */
471
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800472static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700473 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct inet_sock *inet = inet_sk(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800476 int val = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Xi Wang0c9f79b2012-11-11 11:20:01 +0000478 switch (optname) {
479 case IP_PKTINFO:
480 case IP_RECVTTL:
481 case IP_RECVOPTS:
482 case IP_RECVTOS:
483 case IP_RETOPTS:
484 case IP_TOS:
485 case IP_TTL:
486 case IP_HDRINCL:
487 case IP_MTU_DISCOVER:
488 case IP_RECVERR:
489 case IP_ROUTER_ALERT:
490 case IP_FREEBIND:
491 case IP_PASSSEC:
492 case IP_TRANSPARENT:
493 case IP_MINTTL:
494 case IP_NODEFRAG:
495 case IP_UNICAST_IF:
496 case IP_MULTICAST_TTL:
497 case IP_MULTICAST_ALL:
498 case IP_MULTICAST_LOOP:
499 case IP_RECVORIGDSTADDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (optlen >= sizeof(int)) {
501 if (get_user(val, (int __user *) optval))
502 return -EFAULT;
503 } else if (optlen >= sizeof(char)) {
504 unsigned char ucval;
505
506 if (get_user(ucval, (unsigned char __user *) optval))
507 return -EFAULT;
508 val = (int) ucval;
509 }
510 }
511
512 /* If optlen==0, it is equivalent to val == 0 */
513
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800514 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800515 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 err = 0;
518 lock_sock(sk);
519
520 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800521 case IP_OPTIONS:
522 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000523 struct ip_options_rcu *old, *opt = NULL;
524
roel kluin65a1c4f2009-10-23 05:59:21 +0000525 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800526 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900527 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700528 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800529 if (err)
530 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000531 old = rcu_dereference_protected(inet->inet_opt,
532 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800533 if (inet->is_icsk) {
534 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000535#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800536 if (sk->sk_family == PF_INET ||
537 (!((1 << sk->sk_state) &
538 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000539 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000541 if (old)
542 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800543 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000544 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800545 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000546#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000550 rcu_assign_pointer(inet->inet_opt, opt);
551 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800552 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800553 break;
554 }
555 case IP_PKTINFO:
556 if (val)
557 inet->cmsg_flags |= IP_CMSG_PKTINFO;
558 else
559 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
560 break;
561 case IP_RECVTTL:
562 if (val)
563 inet->cmsg_flags |= IP_CMSG_TTL;
564 else
565 inet->cmsg_flags &= ~IP_CMSG_TTL;
566 break;
567 case IP_RECVTOS:
568 if (val)
569 inet->cmsg_flags |= IP_CMSG_TOS;
570 else
571 inet->cmsg_flags &= ~IP_CMSG_TOS;
572 break;
573 case IP_RECVOPTS:
574 if (val)
575 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
576 else
577 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
578 break;
579 case IP_RETOPTS:
580 if (val)
581 inet->cmsg_flags |= IP_CMSG_RETOPTS;
582 else
583 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
584 break;
585 case IP_PASSSEC:
586 if (val)
587 inet->cmsg_flags |= IP_CMSG_PASSSEC;
588 else
589 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
590 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800591 case IP_RECVORIGDSTADDR:
592 if (val)
593 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
594 else
595 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
596 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800597 case IP_TOS: /* This sets both TOS and Precedence */
598 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400599 val &= ~INET_ECN_MASK;
600 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800602 if (inet->tos != val) {
603 inet->tos = val;
604 sk->sk_priority = rt_tos2priority(val);
605 sk_dst_reset(sk);
606 }
607 break;
608 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700609 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800610 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000611 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800612 goto e_inval;
613 inet->uc_ttl = val;
614 break;
615 case IP_HDRINCL:
616 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800619 }
620 inet->hdrincl = val ? 1 : 0;
621 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000622 case IP_NODEFRAG:
623 if (sk->sk_type != SOCK_RAW) {
624 err = -ENOPROTOOPT;
625 break;
626 }
627 inet->nodefrag = val ? 1 : 0;
628 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800629 case IP_MTU_DISCOVER:
John Dykstra0eae7502009-10-19 21:53:53 -0700630 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800631 goto e_inval;
632 inet->pmtudisc = val;
633 break;
634 case IP_RECVERR:
635 inet->recverr = !!val;
636 if (!val)
637 skb_queue_purge(&sk->sk_error_queue);
638 break;
639 case IP_MULTICAST_TTL:
640 if (sk->sk_type == SOCK_STREAM)
641 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700642 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800643 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800644 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800645 val = 1;
646 if (val < 0 || val > 255)
647 goto e_inval;
648 inet->mc_ttl = val;
649 break;
650 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700651 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800652 goto e_inval;
653 inet->mc_loop = !!val;
654 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000655 case IP_UNICAST_IF:
656 {
657 struct net_device *dev = NULL;
658 int ifindex;
659
660 if (optlen != sizeof(int))
661 goto e_inval;
662
663 ifindex = (__force int)ntohl((__force __be32)val);
664 if (ifindex == 0) {
665 inet->uc_index = 0;
666 err = 0;
667 break;
668 }
669
670 dev = dev_get_by_index(sock_net(sk), ifindex);
671 err = -EADDRNOTAVAIL;
672 if (!dev)
673 break;
674 dev_put(dev);
675
676 err = -EINVAL;
677 if (sk->sk_bound_dev_if)
678 break;
679
680 inet->uc_index = ifindex;
681 err = 0;
682 break;
683 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800684 case IP_MULTICAST_IF:
685 {
686 struct ip_mreqn mreq;
687 struct net_device *dev = NULL;
688
689 if (sk->sk_type == SOCK_STREAM)
690 goto e_inval;
691 /*
692 * Check the arguments are allowable
693 */
694
Shan Wei09159212009-09-22 15:41:10 +0000695 if (optlen < sizeof(struct in_addr))
696 goto e_inval;
697
Stephen Hemminger132adf52007-03-08 20:44:43 -0800698 err = -EFAULT;
699 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800700 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800701 break;
702 } else {
703 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000704 if (optlen >= sizeof(struct ip_mreq)) {
705 if (copy_from_user(&mreq, optval,
706 sizeof(struct ip_mreq)))
707 break;
708 } else if (optlen >= sizeof(struct in_addr)) {
709 if (copy_from_user(&mreq.imr_address, optval,
710 sizeof(struct in_addr)))
711 break;
712 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800713 }
714
715 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700716 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800717 inet->mc_index = 0;
718 inet->mc_addr = 0;
719 err = 0;
720 break;
721 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900722 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000723 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800724 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800725 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000726 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800727
728
729 err = -EADDRNOTAVAIL;
730 if (!dev)
731 break;
Eric Dumazet55b80502009-10-19 06:41:58 +0000732 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800733
734 err = -EINVAL;
735 if (sk->sk_bound_dev_if &&
736 mreq.imr_ifindex != sk->sk_bound_dev_if)
737 break;
738
739 inet->mc_index = mreq.imr_ifindex;
740 inet->mc_addr = mreq.imr_address.s_addr;
741 err = 0;
742 break;
743 }
744
745 case IP_ADD_MEMBERSHIP:
746 case IP_DROP_MEMBERSHIP:
747 {
748 struct ip_mreqn mreq;
749
Flavio Leitnera96fb492007-08-24 22:16:39 -0700750 err = -EPROTO;
751 if (inet_sk(sk)->is_icsk)
752 break;
753
Stephen Hemminger132adf52007-03-08 20:44:43 -0800754 if (optlen < sizeof(struct ip_mreq))
755 goto e_inval;
756 err = -EFAULT;
757 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800758 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800759 break;
760 } else {
761 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800762 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800763 break;
764 }
765
766 if (optname == IP_ADD_MEMBERSHIP)
767 err = ip_mc_join_group(sk, &mreq);
768 else
769 err = ip_mc_leave_group(sk, &mreq);
770 break;
771 }
772 case IP_MSFILTER:
773 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800774 struct ip_msfilter *msf;
775
776 if (optlen < IP_MSFILTER_SIZE(0))
777 goto e_inval;
778 if (optlen > sysctl_optmem_max) {
779 err = -ENOBUFS;
780 break;
781 }
782 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700783 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800784 err = -ENOBUFS;
785 break;
786 }
787 err = -EFAULT;
788 if (copy_from_user(msf, optval, optlen)) {
789 kfree(msf);
790 break;
791 }
792 /* numsrc >= (1G-4) overflow in 32 bits */
793 if (msf->imsf_numsrc >= 0x3ffffffcU ||
794 msf->imsf_numsrc > sysctl_igmp_max_msf) {
795 kfree(msf);
796 err = -ENOBUFS;
797 break;
798 }
799 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
800 kfree(msf);
801 err = -EINVAL;
802 break;
803 }
804 err = ip_mc_msfilter(sk, msf, 0);
805 kfree(msf);
806 break;
807 }
808 case IP_BLOCK_SOURCE:
809 case IP_UNBLOCK_SOURCE:
810 case IP_ADD_SOURCE_MEMBERSHIP:
811 case IP_DROP_SOURCE_MEMBERSHIP:
812 {
813 struct ip_mreq_source mreqs;
814 int omode, add;
815
816 if (optlen != sizeof(struct ip_mreq_source))
817 goto e_inval;
818 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
819 err = -EFAULT;
820 break;
821 }
822 if (optname == IP_BLOCK_SOURCE) {
823 omode = MCAST_EXCLUDE;
824 add = 1;
825 } else if (optname == IP_UNBLOCK_SOURCE) {
826 omode = MCAST_EXCLUDE;
827 add = 0;
828 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
829 struct ip_mreqn mreq;
830
831 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
832 mreq.imr_address.s_addr = mreqs.imr_interface;
833 mreq.imr_ifindex = 0;
834 err = ip_mc_join_group(sk, &mreq);
835 if (err && err != -EADDRINUSE)
836 break;
837 omode = MCAST_INCLUDE;
838 add = 1;
839 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
840 omode = MCAST_INCLUDE;
841 add = 0;
842 }
843 err = ip_mc_source(add, omode, sk, &mreqs, 0);
844 break;
845 }
846 case MCAST_JOIN_GROUP:
847 case MCAST_LEAVE_GROUP:
848 {
849 struct group_req greq;
850 struct sockaddr_in *psin;
851 struct ip_mreqn mreq;
852
853 if (optlen < sizeof(struct group_req))
854 goto e_inval;
855 err = -EFAULT;
856 if (copy_from_user(&greq, optval, sizeof(greq)))
857 break;
858 psin = (struct sockaddr_in *)&greq.gr_group;
859 if (psin->sin_family != AF_INET)
860 goto e_inval;
861 memset(&mreq, 0, sizeof(mreq));
862 mreq.imr_multiaddr = psin->sin_addr;
863 mreq.imr_ifindex = greq.gr_interface;
864
865 if (optname == MCAST_JOIN_GROUP)
866 err = ip_mc_join_group(sk, &mreq);
867 else
868 err = ip_mc_leave_group(sk, &mreq);
869 break;
870 }
871 case MCAST_JOIN_SOURCE_GROUP:
872 case MCAST_LEAVE_SOURCE_GROUP:
873 case MCAST_BLOCK_SOURCE:
874 case MCAST_UNBLOCK_SOURCE:
875 {
876 struct group_source_req greqs;
877 struct ip_mreq_source mreqs;
878 struct sockaddr_in *psin;
879 int omode, add;
880
881 if (optlen != sizeof(struct group_source_req))
882 goto e_inval;
883 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
884 err = -EFAULT;
885 break;
886 }
887 if (greqs.gsr_group.ss_family != AF_INET ||
888 greqs.gsr_source.ss_family != AF_INET) {
889 err = -EADDRNOTAVAIL;
890 break;
891 }
892 psin = (struct sockaddr_in *)&greqs.gsr_group;
893 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
894 psin = (struct sockaddr_in *)&greqs.gsr_source;
895 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
896 mreqs.imr_interface = 0; /* use index for mc_source */
897
898 if (optname == MCAST_BLOCK_SOURCE) {
899 omode = MCAST_EXCLUDE;
900 add = 1;
901 } else if (optname == MCAST_UNBLOCK_SOURCE) {
902 omode = MCAST_EXCLUDE;
903 add = 0;
904 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
905 struct ip_mreqn mreq;
906
907 psin = (struct sockaddr_in *)&greqs.gsr_group;
908 mreq.imr_multiaddr = psin->sin_addr;
909 mreq.imr_address.s_addr = 0;
910 mreq.imr_ifindex = greqs.gsr_interface;
911 err = ip_mc_join_group(sk, &mreq);
912 if (err && err != -EADDRINUSE)
913 break;
914 greqs.gsr_interface = mreq.imr_ifindex;
915 omode = MCAST_INCLUDE;
916 add = 1;
917 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
918 omode = MCAST_INCLUDE;
919 add = 0;
920 }
921 err = ip_mc_source(add, omode, sk, &mreqs,
922 greqs.gsr_interface);
923 break;
924 }
925 case MCAST_MSFILTER:
926 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800927 struct sockaddr_in *psin;
928 struct ip_msfilter *msf = NULL;
929 struct group_filter *gsf = NULL;
930 int msize, i, ifindex;
931
932 if (optlen < GROUP_FILTER_SIZE(0))
933 goto e_inval;
934 if (optlen > sysctl_optmem_max) {
935 err = -ENOBUFS;
936 break;
937 }
Jianjun Kong09cb1052008-11-03 00:27:11 -0800938 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700939 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800940 err = -ENOBUFS;
941 break;
942 }
943 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700944 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800945 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700946
Stephen Hemminger132adf52007-03-08 20:44:43 -0800947 /* numsrc >= (4G-140)/128 overflow in 32 bits */
948 if (gsf->gf_numsrc >= 0x1ffffff ||
949 gsf->gf_numsrc > sysctl_igmp_max_msf) {
950 err = -ENOBUFS;
951 goto mc_msf_out;
952 }
953 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
954 err = -EINVAL;
955 goto mc_msf_out;
956 }
957 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800958 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700959 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800960 err = -ENOBUFS;
961 goto mc_msf_out;
962 }
963 ifindex = gsf->gf_interface;
964 psin = (struct sockaddr_in *)&gsf->gf_group;
965 if (psin->sin_family != AF_INET) {
966 err = -EADDRNOTAVAIL;
967 goto mc_msf_out;
968 }
969 msf->imsf_multiaddr = psin->sin_addr.s_addr;
970 msf->imsf_interface = 0;
971 msf->imsf_fmode = gsf->gf_fmode;
972 msf->imsf_numsrc = gsf->gf_numsrc;
973 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700974 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800975 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
976
977 if (psin->sin_family != AF_INET)
978 goto mc_msf_out;
979 msf->imsf_slist[i] = psin->sin_addr.s_addr;
980 }
981 kfree(gsf);
982 gsf = NULL;
983
984 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700985mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -0800986 kfree(msf);
987 kfree(gsf);
988 break;
989 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +0000990 case IP_MULTICAST_ALL:
991 if (optlen < 1)
992 goto e_inval;
993 if (val != 0 && val != 1)
994 goto e_inval;
995 inet->mc_all = val;
996 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800997 case IP_ROUTER_ALERT:
998 err = ip_ra_control(sk, val ? 1 : 0, NULL);
999 break;
1000
1001 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001002 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001003 goto e_inval;
1004 inet->freebind = !!val;
1005 break;
1006
1007 case IP_IPSEC_POLICY:
1008 case IP_XFRM_POLICY:
1009 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001010 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001011 break;
1012 err = xfrm_user_policy(sk, optname, optval, optlen);
1013 break;
1014
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001015 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001016 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1017 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001018 err = -EPERM;
1019 break;
1020 }
1021 if (optlen < 1)
1022 goto e_inval;
1023 inet->transparent = !!val;
1024 break;
1025
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001026 case IP_MINTTL:
1027 if (optlen < 1)
1028 goto e_inval;
1029 if (val < 0 || val > 255)
1030 goto e_inval;
1031 inet->min_ttl = val;
1032 break;
1033
Stephen Hemminger132adf52007-03-08 20:44:43 -08001034 default:
1035 err = -ENOPROTOOPT;
1036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038 release_sock(sk);
1039 return err;
1040
1041e_inval:
1042 release_sock(sk);
1043 return -EINVAL;
1044}
1045
Eric Dumazetf84af322010-04-28 15:31:51 -07001046/**
Eric Dumazetd826eb12011-11-09 07:24:35 +00001047 * ipv4_pktinfo_prepare - transfert some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001048 * @sk: socket
1049 * @skb: buffer
1050 *
David S. Miller35ebf652012-06-28 03:59:11 -07001051 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1052 * destination in skb->cb[] before dst drop.
Eric Dumazetd826eb12011-11-09 07:24:35 +00001053 * This way, receiver doesnt make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001054 */
Eric Dumazetd826eb12011-11-09 07:24:35 +00001055void ipv4_pktinfo_prepare(struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001056{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001057 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001058
David S. Miller92101b32012-07-23 16:29:00 -07001059 if (skb_rtable(skb)) {
1060 pktinfo->ipi_ifindex = inet_iif(skb);
David S. Miller35ebf652012-06-28 03:59:11 -07001061 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001062 } else {
1063 pktinfo->ipi_ifindex = 0;
1064 pktinfo->ipi_spec_dst.s_addr = 0;
1065 }
1066 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001067}
Eric Dumazetf84af322010-04-28 15:31:51 -07001068
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001069int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001070 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001071{
1072 int err;
1073
1074 if (level != SOL_IP)
1075 return -ENOPROTOOPT;
1076
1077 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1078#ifdef CONFIG_NETFILTER
1079 /* we need to exclude all possible ENOPROTOOPTs except default case */
1080 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001081 optname != IP_IPSEC_POLICY &&
1082 optname != IP_XFRM_POLICY &&
1083 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001084 lock_sock(sk);
1085 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1086 release_sock(sk);
1087 }
1088#endif
1089 return err;
1090}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001091EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001092
1093#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001094int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001095 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001096{
1097 int err;
1098
1099 if (level != SOL_IP)
1100 return -ENOPROTOOPT;
1101
David L Stevensdae50292008-04-27 01:06:07 -07001102 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1103 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1104 ip_setsockopt);
1105
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001106 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1107#ifdef CONFIG_NETFILTER
1108 /* we need to exclude all possible ENOPROTOOPTs except default case */
1109 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001110 optname != IP_IPSEC_POLICY &&
1111 optname != IP_XFRM_POLICY &&
1112 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001113 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001114 err = compat_nf_setsockopt(sk, PF_INET, optname,
1115 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001116 release_sock(sk);
1117 }
1118#endif
1119 return err;
1120}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001121EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001122#endif
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001125 * Get the options. Note for future reference. The GET of IP options gets
1126 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 */
1128
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001129static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001130 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 struct inet_sock *inet = inet_sk(sk);
1133 int val;
1134 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001135
Stephen Hemminger132adf52007-03-08 20:44:43 -08001136 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return -EOPNOTSUPP;
1138
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001139 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001140 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Jianjun Kong09cb1052008-11-03 00:27:11 -08001142 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001144 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 lock_sock(sk);
1148
Stephen Hemminger132adf52007-03-08 20:44:43 -08001149 switch (optname) {
1150 case IP_OPTIONS:
1151 {
1152 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001153 struct ip_options *opt = (struct ip_options *)optbuf;
1154 struct ip_options_rcu *inet_opt;
1155
1156 inet_opt = rcu_dereference_protected(inet->inet_opt,
1157 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001158 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001159 if (inet_opt)
1160 memcpy(optbuf, &inet_opt->opt,
1161 sizeof(struct ip_options) +
1162 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001163 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Stephen Hemminger132adf52007-03-08 20:44:43 -08001165 if (opt->optlen == 0)
1166 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Stephen Hemminger132adf52007-03-08 20:44:43 -08001168 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Stephen Hemminger132adf52007-03-08 20:44:43 -08001170 len = min_t(unsigned int, len, opt->optlen);
1171 if (put_user(len, optlen))
1172 return -EFAULT;
1173 if (copy_to_user(optval, opt->__data, len))
1174 return -EFAULT;
1175 return 0;
1176 }
1177 case IP_PKTINFO:
1178 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1179 break;
1180 case IP_RECVTTL:
1181 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1182 break;
1183 case IP_RECVTOS:
1184 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1185 break;
1186 case IP_RECVOPTS:
1187 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1188 break;
1189 case IP_RETOPTS:
1190 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1191 break;
1192 case IP_PASSSEC:
1193 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1194 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001195 case IP_RECVORIGDSTADDR:
1196 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1197 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001198 case IP_TOS:
1199 val = inet->tos;
1200 break;
1201 case IP_TTL:
1202 val = (inet->uc_ttl == -1 ?
1203 sysctl_ip_default_ttl :
1204 inet->uc_ttl);
1205 break;
1206 case IP_HDRINCL:
1207 val = inet->hdrincl;
1208 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001209 case IP_NODEFRAG:
1210 val = inet->nodefrag;
1211 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001212 case IP_MTU_DISCOVER:
1213 val = inet->pmtudisc;
1214 break;
1215 case IP_MTU:
1216 {
1217 struct dst_entry *dst;
1218 val = 0;
1219 dst = sk_dst_get(sk);
1220 if (dst) {
1221 val = dst_mtu(dst);
1222 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001224 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001226 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001228 break;
1229 }
1230 case IP_RECVERR:
1231 val = inet->recverr;
1232 break;
1233 case IP_MULTICAST_TTL:
1234 val = inet->mc_ttl;
1235 break;
1236 case IP_MULTICAST_LOOP:
1237 val = inet->mc_loop;
1238 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001239 case IP_UNICAST_IF:
1240 val = (__force int)htonl((__u32) inet->uc_index);
1241 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001242 case IP_MULTICAST_IF:
1243 {
1244 struct in_addr addr;
1245 len = min_t(unsigned int, len, sizeof(struct in_addr));
1246 addr.s_addr = inet->mc_addr;
1247 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Stephen Hemminger132adf52007-03-08 20:44:43 -08001249 if (put_user(len, optlen))
1250 return -EFAULT;
1251 if (copy_to_user(optval, &addr, len))
1252 return -EFAULT;
1253 return 0;
1254 }
1255 case IP_MSFILTER:
1256 {
1257 struct ip_msfilter msf;
1258 int err;
1259
1260 if (len < IP_MSFILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001262 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001264 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001266 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001268 err = ip_mc_msfget(sk, &msf,
1269 (struct ip_msfilter __user *)optval, optlen);
1270 release_sock(sk);
1271 return err;
1272 }
1273 case MCAST_MSFILTER:
1274 {
1275 struct group_filter gsf;
1276 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Stephen Hemminger132adf52007-03-08 20:44:43 -08001278 if (len < GROUP_FILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001280 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001282 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001284 return -EFAULT;
1285 }
1286 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001287 (struct group_filter __user *)optval,
1288 optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001289 release_sock(sk);
1290 return err;
1291 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001292 case IP_MULTICAST_ALL:
1293 val = inet->mc_all;
1294 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001295 case IP_PKTOPTIONS:
1296 {
1297 struct msghdr msg;
1298
1299 release_sock(sk);
1300
1301 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001303
1304 msg.msg_control = optval;
1305 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001306 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001307
1308 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1309 struct in_pktinfo info;
1310
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001311 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1312 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001313 info.ipi_ifindex = inet->mc_index;
1314 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1315 }
1316 if (inet->cmsg_flags & IP_CMSG_TTL) {
1317 int hlim = inet->mc_ttl;
1318 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1319 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001320 if (inet->cmsg_flags & IP_CMSG_TOS) {
1321 int tos = inet->rcv_tos;
1322 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1323 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001324 len -= msg.msg_controllen;
1325 return put_user(len, optlen);
1326 }
1327 case IP_FREEBIND:
1328 val = inet->freebind;
1329 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001330 case IP_TRANSPARENT:
1331 val = inet->transparent;
1332 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001333 case IP_MINTTL:
1334 val = inet->min_ttl;
1335 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001336 default:
1337 release_sock(sk);
1338 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001341
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001342 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 unsigned char ucval = (unsigned char)val;
1344 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001345 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001347 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return -EFAULT;
1349 } else {
1350 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001351 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001353 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -EFAULT;
1355 }
1356 return 0;
1357}
1358
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001359int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001360 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001361{
1362 int err;
1363
Daniel Balutadd23198e2011-08-07 22:31:07 -07001364 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001365#ifdef CONFIG_NETFILTER
1366 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001367 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1368 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001369 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001370
Jianjun Kong09cb1052008-11-03 00:27:11 -08001371 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001372 return -EFAULT;
1373
1374 lock_sock(sk);
1375 err = nf_getsockopt(sk, PF_INET, optname, optval,
1376 &len);
1377 release_sock(sk);
1378 if (err >= 0)
1379 err = put_user(len, optlen);
1380 return err;
1381 }
1382#endif
1383 return err;
1384}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001385EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001386
1387#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001388int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1389 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001390{
David L Stevens42908c62008-04-29 03:23:22 -07001391 int err;
1392
1393 if (optname == MCAST_MSFILTER)
1394 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1395 ip_getsockopt);
1396
Daniel Balutadd23198e2011-08-07 22:31:07 -07001397 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1398 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001399
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001400#ifdef CONFIG_NETFILTER
1401 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001402 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1403 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001404 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001405
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001406 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001407 return -EFAULT;
1408
1409 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001410 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001411 release_sock(sk);
1412 if (err >= 0)
1413 err = put_user(len, optlen);
1414 return err;
1415 }
1416#endif
1417 return err;
1418}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001419EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001420#endif