Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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. | |
e905a9ed | 7 | * |
1da177e4 LT |
8 | * Authors: see ip.c |
9 | * | |
10 | * Fixes: | |
11 | * Many : Split from ip.c , see ip.c for history. | |
12 | * Martin Mares : TOS setting fixed. | |
e905a9ed | 13 | * Alan Cox : Fixed a couple of oopses in Martin's |
1da177e4 LT |
14 | * TOS tweaks. |
15 | * Mike McLagan : Routing by source | |
16 | */ | |
17 | ||
1da177e4 LT |
18 | #include <linux/module.h> |
19 | #include <linux/types.h> | |
20 | #include <linux/mm.h> | |
1da177e4 LT |
21 | #include <linux/skbuff.h> |
22 | #include <linux/ip.h> | |
23 | #include <linux/icmp.h> | |
14c85021 | 24 | #include <linux/inetdevice.h> |
1da177e4 | 25 | #include <linux/netdevice.h> |
5a0e3ad6 | 26 | #include <linux/slab.h> |
1da177e4 LT |
27 | #include <net/sock.h> |
28 | #include <net/ip.h> | |
29 | #include <net/icmp.h> | |
d83d8461 | 30 | #include <net/tcp_states.h> |
1da177e4 LT |
31 | #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> | |
dae50295 | 38 | #include <net/compat.h> |
1da177e4 LT |
39 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
40 | #include <net/transp_v6.h> | |
41 | #endif | |
42 | ||
43 | #include <linux/errqueue.h> | |
44 | #include <asm/uaccess.h> | |
45 | ||
46 | #define IP_CMSG_PKTINFO 1 | |
47 | #define IP_CMSG_TTL 2 | |
48 | #define IP_CMSG_TOS 4 | |
49 | #define IP_CMSG_RECVOPTS 8 | |
50 | #define IP_CMSG_RETOPTS 16 | |
2c7946a7 | 51 | #define IP_CMSG_PASSSEC 32 |
e8b2dfe9 | 52 | #define IP_CMSG_ORIGDSTADDR 64 |
1da177e4 LT |
53 | |
54 | /* | |
55 | * SOL_IP control messages. | |
56 | */ | |
57 | ||
58 | static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) | |
59 | { | |
60 | struct in_pktinfo info; | |
511c3f92 | 61 | struct rtable *rt = skb_rtable(skb); |
1da177e4 | 62 | |
eddc9ec5 | 63 | info.ipi_addr.s_addr = ip_hdr(skb)->daddr; |
1da177e4 LT |
64 | 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 | ||
75 | static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb) | |
76 | { | |
eddc9ec5 | 77 | int ttl = ip_hdr(skb)->ttl; |
1da177e4 LT |
78 | put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl); |
79 | } | |
80 | ||
81 | static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb) | |
82 | { | |
eddc9ec5 | 83 | put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos); |
1da177e4 LT |
84 | } |
85 | ||
86 | static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb) | |
87 | { | |
88 | if (IPCB(skb)->opt.optlen == 0) | |
89 | return; | |
90 | ||
eddc9ec5 ACM |
91 | put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen, |
92 | ip_hdr(skb) + 1); | |
1da177e4 LT |
93 | } |
94 | ||
95 | ||
96 | static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb) | |
97 | { | |
98 | unsigned char optbuf[sizeof(struct ip_options) + 40]; | |
09cb105e | 99 | struct ip_options * opt = (struct ip_options *)optbuf; |
1da177e4 LT |
100 | |
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 | ||
2c7946a7 CZ |
113 | static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb) |
114 | { | |
115 | char *secdata; | |
dc49c1f9 | 116 | u32 seclen, secid; |
2c7946a7 CZ |
117 | int err; |
118 | ||
dc49c1f9 CZ |
119 | err = security_socket_getpeersec_dgram(NULL, skb, &secid); |
120 | if (err) | |
121 | return; | |
122 | ||
123 | err = security_secid_to_secctx(secid, &secdata, &seclen); | |
2c7946a7 CZ |
124 | if (err) |
125 | return; | |
126 | ||
127 | put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata); | |
dc49c1f9 | 128 | security_release_secctx(secdata, seclen); |
2c7946a7 CZ |
129 | } |
130 | ||
21d1a161 | 131 | static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb) |
e8b2dfe9 BS |
132 | { |
133 | struct sockaddr_in sin; | |
134 | struct iphdr *iph = ip_hdr(skb); | |
21d1a161 | 135 | __be16 *ports = (__be16 *)skb_transport_header(skb); |
e8b2dfe9 BS |
136 | |
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 | } | |
1da177e4 LT |
152 | |
153 | void 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); | |
4d52cfbe | 161 | if ((flags >>= 1) == 0) |
1da177e4 LT |
162 | return; |
163 | ||
164 | if (flags & 1) | |
165 | ip_cmsg_recv_ttl(msg, skb); | |
4d52cfbe | 166 | if ((flags >>= 1) == 0) |
1da177e4 LT |
167 | return; |
168 | ||
169 | if (flags & 1) | |
170 | ip_cmsg_recv_tos(msg, skb); | |
4d52cfbe | 171 | if ((flags >>= 1) == 0) |
1da177e4 LT |
172 | return; |
173 | ||
174 | if (flags & 1) | |
175 | ip_cmsg_recv_opts(msg, skb); | |
4d52cfbe | 176 | if ((flags >>= 1) == 0) |
1da177e4 LT |
177 | return; |
178 | ||
179 | if (flags & 1) | |
180 | ip_cmsg_recv_retopts(msg, skb); | |
4d52cfbe | 181 | if ((flags >>= 1) == 0) |
2c7946a7 CZ |
182 | return; |
183 | ||
184 | if (flags & 1) | |
185 | ip_cmsg_recv_security(msg, skb); | |
e8b2dfe9 | 186 | |
4d52cfbe | 187 | if ((flags >>= 1) == 0) |
e8b2dfe9 BS |
188 | return; |
189 | if (flags & 1) | |
190 | ip_cmsg_recv_dstaddr(msg, skb); | |
191 | ||
1da177e4 | 192 | } |
4d52cfbe | 193 | EXPORT_SYMBOL(ip_cmsg_recv); |
1da177e4 | 194 | |
7a6adb92 | 195 | int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc) |
1da177e4 LT |
196 | { |
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)); | |
4d52cfbe ED |
208 | err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg), |
209 | err < 40 ? err : 40); | |
1da177e4 LT |
210 | 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 | */ | |
241 | struct ip_ra_chain *ip_ra_chain; | |
66018506 ED |
242 | static DEFINE_SPINLOCK(ip_ra_lock); |
243 | ||
592fcb9d ED |
244 | |
245 | static void ip_ra_destroy_rcu(struct rcu_head *head) | |
66018506 | 246 | { |
592fcb9d ED |
247 | struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu); |
248 | ||
249 | sock_put(ra->saved_sk); | |
250 | kfree(ra); | |
66018506 | 251 | } |
1da177e4 | 252 | |
4d52cfbe ED |
253 | int ip_ra_control(struct sock *sk, unsigned char on, |
254 | void (*destructor)(struct sock *)) | |
1da177e4 LT |
255 | { |
256 | struct ip_ra_chain *ra, *new_ra, **rap; | |
257 | ||
c720c7e8 | 258 | if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW) |
1da177e4 LT |
259 | return -EINVAL; |
260 | ||
261 | new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; | |
262 | ||
66018506 | 263 | spin_lock_bh(&ip_ra_lock); |
4d52cfbe | 264 | for (rap = &ip_ra_chain; (ra = *rap) != NULL; rap = &ra->next) { |
1da177e4 LT |
265 | if (ra->sk == sk) { |
266 | if (on) { | |
66018506 | 267 | spin_unlock_bh(&ip_ra_lock); |
a51482bd | 268 | kfree(new_ra); |
1da177e4 LT |
269 | return -EADDRINUSE; |
270 | } | |
592fcb9d ED |
271 | /* dont let ip_call_ra_chain() use sk again */ |
272 | ra->sk = NULL; | |
66018506 ED |
273 | rcu_assign_pointer(*rap, ra->next); |
274 | spin_unlock_bh(&ip_ra_lock); | |
1da177e4 LT |
275 | |
276 | if (ra->destructor) | |
277 | ra->destructor(sk); | |
592fcb9d ED |
278 | /* |
279 | * Delay sock_put(sk) and kfree(ra) after one rcu grace | |
280 | * period. This guarantee ip_call_ra_chain() dont need | |
281 | * to mess with socket refcounts. | |
282 | */ | |
283 | ra->saved_sk = sk; | |
284 | call_rcu(&ra->rcu, ip_ra_destroy_rcu); | |
1da177e4 LT |
285 | return 0; |
286 | } | |
287 | } | |
288 | if (new_ra == NULL) { | |
66018506 | 289 | spin_unlock_bh(&ip_ra_lock); |
1da177e4 LT |
290 | return -ENOBUFS; |
291 | } | |
292 | new_ra->sk = sk; | |
293 | new_ra->destructor = destructor; | |
294 | ||
295 | new_ra->next = ra; | |
66018506 | 296 | rcu_assign_pointer(*rap, new_ra); |
1da177e4 | 297 | sock_hold(sk); |
66018506 | 298 | spin_unlock_bh(&ip_ra_lock); |
1da177e4 LT |
299 | |
300 | return 0; | |
301 | } | |
302 | ||
e905a9ed | 303 | void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, |
35986b32 | 304 | __be16 port, u32 info, u8 *payload) |
1da177e4 | 305 | { |
1da177e4 LT |
306 | struct sock_exterr_skb *serr; |
307 | ||
1da177e4 LT |
308 | skb = skb_clone(skb, GFP_ATOMIC); |
309 | if (!skb) | |
310 | return; | |
311 | ||
e905a9ed | 312 | serr = SKB_EXT_ERR(skb); |
1da177e4 LT |
313 | serr->ee.ee_errno = err; |
314 | serr->ee.ee_origin = SO_EE_ORIGIN_ICMP; | |
88c7664f ACM |
315 | serr->ee.ee_type = icmp_hdr(skb)->type; |
316 | serr->ee.ee_code = icmp_hdr(skb)->code; | |
1da177e4 LT |
317 | serr->ee.ee_pad = 0; |
318 | serr->ee.ee_info = info; | |
319 | serr->ee.ee_data = 0; | |
88c7664f | 320 | serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) - |
d56f90a7 | 321 | skb_network_header(skb); |
1da177e4 LT |
322 | serr->port = port; |
323 | ||
bd82393c ACM |
324 | if (skb_pull(skb, payload - skb->data) != NULL) { |
325 | skb_reset_transport_header(skb); | |
326 | if (sock_queue_err_skb(sk, skb) == 0) | |
327 | return; | |
328 | } | |
329 | kfree_skb(skb); | |
1da177e4 LT |
330 | } |
331 | ||
0579016e | 332 | void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info) |
1da177e4 LT |
333 | { |
334 | struct inet_sock *inet = inet_sk(sk); | |
335 | struct sock_exterr_skb *serr; | |
336 | struct iphdr *iph; | |
337 | struct sk_buff *skb; | |
338 | ||
339 | if (!inet->recverr) | |
340 | return; | |
341 | ||
342 | skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC); | |
343 | if (!skb) | |
344 | return; | |
345 | ||
2ca9e6f2 ACM |
346 | skb_put(skb, sizeof(struct iphdr)); |
347 | skb_reset_network_header(skb); | |
eddc9ec5 | 348 | iph = ip_hdr(skb); |
1da177e4 LT |
349 | iph->daddr = daddr; |
350 | ||
e905a9ed | 351 | serr = SKB_EXT_ERR(skb); |
1da177e4 LT |
352 | serr->ee.ee_errno = err; |
353 | serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; | |
e905a9ed | 354 | serr->ee.ee_type = 0; |
1da177e4 LT |
355 | serr->ee.ee_code = 0; |
356 | serr->ee.ee_pad = 0; | |
357 | serr->ee.ee_info = info; | |
358 | serr->ee.ee_data = 0; | |
d56f90a7 | 359 | serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); |
1da177e4 LT |
360 | serr->port = port; |
361 | ||
27a884dc | 362 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); |
bd82393c | 363 | skb_reset_transport_header(skb); |
1da177e4 LT |
364 | |
365 | if (sock_queue_err_skb(sk, skb)) | |
366 | kfree_skb(skb); | |
367 | } | |
368 | ||
e905a9ed | 369 | /* |
1da177e4 LT |
370 | * Handle MSG_ERRQUEUE |
371 | */ | |
372 | int ip_recv_error(struct sock *sk, struct msghdr *msg, int len) | |
373 | { | |
374 | struct sock_exterr_skb *serr; | |
375 | struct sk_buff *skb, *skb2; | |
376 | struct sockaddr_in *sin; | |
377 | struct { | |
378 | struct sock_extended_err ee; | |
379 | struct sockaddr_in offender; | |
380 | } errhdr; | |
381 | int err; | |
382 | int copied; | |
383 | ||
384 | err = -EAGAIN; | |
385 | skb = skb_dequeue(&sk->sk_error_queue); | |
386 | if (skb == NULL) | |
387 | goto out; | |
388 | ||
389 | copied = skb->len; | |
390 | if (copied > len) { | |
391 | msg->msg_flags |= MSG_TRUNC; | |
392 | copied = len; | |
393 | } | |
394 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); | |
395 | if (err) | |
396 | goto out_free_skb; | |
397 | ||
398 | sock_recv_timestamp(msg, sk, skb); | |
399 | ||
400 | serr = SKB_EXT_ERR(skb); | |
401 | ||
402 | sin = (struct sockaddr_in *)msg->msg_name; | |
403 | if (sin) { | |
404 | sin->sin_family = AF_INET; | |
d56f90a7 ACM |
405 | sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) + |
406 | serr->addr_offset); | |
1da177e4 LT |
407 | sin->sin_port = serr->port; |
408 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); | |
409 | } | |
410 | ||
411 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); | |
412 | sin = &errhdr.offender; | |
413 | sin->sin_family = AF_UNSPEC; | |
414 | if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) { | |
415 | struct inet_sock *inet = inet_sk(sk); | |
416 | ||
417 | sin->sin_family = AF_INET; | |
eddc9ec5 | 418 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
1da177e4 LT |
419 | sin->sin_port = 0; |
420 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); | |
421 | if (inet->cmsg_flags) | |
422 | ip_cmsg_recv(msg, skb); | |
423 | } | |
424 | ||
425 | put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr); | |
426 | ||
427 | /* Now we could try to dump offended packet options */ | |
428 | ||
429 | msg->msg_flags |= MSG_ERRQUEUE; | |
430 | err = copied; | |
431 | ||
432 | /* Reset and regenerate socket error */ | |
e0f9f858 | 433 | spin_lock_bh(&sk->sk_error_queue.lock); |
1da177e4 | 434 | sk->sk_err = 0; |
4d52cfbe ED |
435 | skb2 = skb_peek(&sk->sk_error_queue); |
436 | if (skb2 != NULL) { | |
1da177e4 | 437 | sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno; |
e0f9f858 | 438 | spin_unlock_bh(&sk->sk_error_queue.lock); |
1da177e4 LT |
439 | sk->sk_error_report(sk); |
440 | } else | |
e0f9f858 | 441 | spin_unlock_bh(&sk->sk_error_queue.lock); |
1da177e4 | 442 | |
e905a9ed | 443 | out_free_skb: |
1da177e4 LT |
444 | kfree_skb(skb); |
445 | out: | |
446 | return err; | |
447 | } | |
448 | ||
449 | ||
450 | /* | |
4d52cfbe ED |
451 | * Socket option code for IP. This is the end of the line after any |
452 | * TCP,UDP etc options on an IP socket. | |
1da177e4 LT |
453 | */ |
454 | ||
3fdadf7d | 455 | static int do_ip_setsockopt(struct sock *sk, int level, |
b7058842 | 456 | int optname, char __user *optval, unsigned int optlen) |
1da177e4 LT |
457 | { |
458 | struct inet_sock *inet = inet_sk(sk); | |
09cb105e | 459 | int val = 0, err; |
1da177e4 | 460 | |
e905a9ed | 461 | if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) | |
132adf54 SH |
462 | (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) | |
463 | (1<<IP_RETOPTS) | (1<<IP_TOS) | | |
464 | (1<<IP_TTL) | (1<<IP_HDRINCL) | | |
465 | (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) | | |
466 | (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) | | |
d218d111 SH |
467 | (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT) | |
468 | (1<<IP_MINTTL))) || | |
132adf54 | 469 | optname == IP_MULTICAST_TTL || |
f771bef9 | 470 | optname == IP_MULTICAST_ALL || |
e8b2dfe9 BS |
471 | optname == IP_MULTICAST_LOOP || |
472 | optname == IP_RECVORIGDSTADDR) { | |
1da177e4 LT |
473 | if (optlen >= sizeof(int)) { |
474 | if (get_user(val, (int __user *) optval)) | |
475 | return -EFAULT; | |
476 | } else if (optlen >= sizeof(char)) { | |
477 | unsigned char ucval; | |
478 | ||
479 | if (get_user(ucval, (unsigned char __user *) optval)) | |
480 | return -EFAULT; | |
481 | val = (int) ucval; | |
482 | } | |
483 | } | |
484 | ||
485 | /* If optlen==0, it is equivalent to val == 0 */ | |
486 | ||
6a9fb947 | 487 | if (ip_mroute_opt(optname)) |
09cb105e | 488 | return ip_mroute_setsockopt(sk, optname, optval, optlen); |
1da177e4 LT |
489 | |
490 | err = 0; | |
491 | lock_sock(sk); | |
492 | ||
493 | switch (optname) { | |
132adf54 SH |
494 | case IP_OPTIONS: |
495 | { | |
4d52cfbe | 496 | struct ip_options *opt = NULL; |
65a1c4ff | 497 | if (optlen > 40) |
132adf54 | 498 | goto e_inval; |
3b1e0a65 | 499 | err = ip_options_get_from_user(sock_net(sk), &opt, |
cb84663e | 500 | optval, optlen); |
132adf54 SH |
501 | if (err) |
502 | break; | |
503 | if (inet->is_icsk) { | |
504 | struct inet_connection_sock *icsk = inet_csk(sk); | |
1da177e4 | 505 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
132adf54 SH |
506 | if (sk->sk_family == PF_INET || |
507 | (!((1 << sk->sk_state) & | |
508 | (TCPF_LISTEN | TCPF_CLOSE)) && | |
c720c7e8 | 509 | inet->inet_daddr != LOOPBACK4_IPV6)) { |
1da177e4 | 510 | #endif |
132adf54 SH |
511 | if (inet->opt) |
512 | icsk->icsk_ext_hdr_len -= inet->opt->optlen; | |
513 | if (opt) | |
514 | icsk->icsk_ext_hdr_len += opt->optlen; | |
515 | icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie); | |
1da177e4 | 516 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
1da177e4 | 517 | } |
132adf54 | 518 | #endif |
1da177e4 | 519 | } |
132adf54 SH |
520 | opt = xchg(&inet->opt, opt); |
521 | kfree(opt); | |
522 | break; | |
523 | } | |
524 | case IP_PKTINFO: | |
525 | if (val) | |
526 | inet->cmsg_flags |= IP_CMSG_PKTINFO; | |
527 | else | |
528 | inet->cmsg_flags &= ~IP_CMSG_PKTINFO; | |
529 | break; | |
530 | case IP_RECVTTL: | |
531 | if (val) | |
532 | inet->cmsg_flags |= IP_CMSG_TTL; | |
533 | else | |
534 | inet->cmsg_flags &= ~IP_CMSG_TTL; | |
535 | break; | |
536 | case IP_RECVTOS: | |
537 | if (val) | |
538 | inet->cmsg_flags |= IP_CMSG_TOS; | |
539 | else | |
540 | inet->cmsg_flags &= ~IP_CMSG_TOS; | |
541 | break; | |
542 | case IP_RECVOPTS: | |
543 | if (val) | |
544 | inet->cmsg_flags |= IP_CMSG_RECVOPTS; | |
545 | else | |
546 | inet->cmsg_flags &= ~IP_CMSG_RECVOPTS; | |
547 | break; | |
548 | case IP_RETOPTS: | |
549 | if (val) | |
550 | inet->cmsg_flags |= IP_CMSG_RETOPTS; | |
551 | else | |
552 | inet->cmsg_flags &= ~IP_CMSG_RETOPTS; | |
553 | break; | |
554 | case IP_PASSSEC: | |
555 | if (val) | |
556 | inet->cmsg_flags |= IP_CMSG_PASSSEC; | |
557 | else | |
558 | inet->cmsg_flags &= ~IP_CMSG_PASSSEC; | |
559 | break; | |
e8b2dfe9 BS |
560 | case IP_RECVORIGDSTADDR: |
561 | if (val) | |
562 | inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR; | |
563 | else | |
564 | inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR; | |
565 | break; | |
132adf54 SH |
566 | case IP_TOS: /* This sets both TOS and Precedence */ |
567 | if (sk->sk_type == SOCK_STREAM) { | |
568 | val &= ~3; | |
569 | val |= inet->tos & 3; | |
570 | } | |
132adf54 SH |
571 | if (inet->tos != val) { |
572 | inet->tos = val; | |
573 | sk->sk_priority = rt_tos2priority(val); | |
574 | sk_dst_reset(sk); | |
575 | } | |
576 | break; | |
577 | case IP_TTL: | |
4d52cfbe | 578 | if (optlen < 1) |
132adf54 | 579 | goto e_inval; |
4d52cfbe | 580 | if (val != -1 && (val < 0 || val > 255)) |
132adf54 SH |
581 | goto e_inval; |
582 | inet->uc_ttl = val; | |
583 | break; | |
584 | case IP_HDRINCL: | |
585 | if (sk->sk_type != SOCK_RAW) { | |
586 | err = -ENOPROTOOPT; | |
2c7946a7 | 587 | break; |
132adf54 SH |
588 | } |
589 | inet->hdrincl = val ? 1 : 0; | |
590 | break; | |
591 | case IP_MTU_DISCOVER: | |
0eae750e | 592 | if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE) |
132adf54 SH |
593 | goto e_inval; |
594 | inet->pmtudisc = val; | |
595 | break; | |
596 | case IP_RECVERR: | |
597 | inet->recverr = !!val; | |
598 | if (!val) | |
599 | skb_queue_purge(&sk->sk_error_queue); | |
600 | break; | |
601 | case IP_MULTICAST_TTL: | |
602 | if (sk->sk_type == SOCK_STREAM) | |
603 | goto e_inval; | |
4d52cfbe | 604 | if (optlen < 1) |
132adf54 | 605 | goto e_inval; |
09cb105e | 606 | if (val == -1) |
132adf54 SH |
607 | val = 1; |
608 | if (val < 0 || val > 255) | |
609 | goto e_inval; | |
610 | inet->mc_ttl = val; | |
611 | break; | |
612 | case IP_MULTICAST_LOOP: | |
4d52cfbe | 613 | if (optlen < 1) |
132adf54 SH |
614 | goto e_inval; |
615 | inet->mc_loop = !!val; | |
616 | break; | |
617 | case IP_MULTICAST_IF: | |
618 | { | |
619 | struct ip_mreqn mreq; | |
620 | struct net_device *dev = NULL; | |
621 | ||
622 | if (sk->sk_type == SOCK_STREAM) | |
623 | goto e_inval; | |
624 | /* | |
625 | * Check the arguments are allowable | |
626 | */ | |
627 | ||
0915921b SW |
628 | if (optlen < sizeof(struct in_addr)) |
629 | goto e_inval; | |
630 | ||
132adf54 SH |
631 | err = -EFAULT; |
632 | if (optlen >= sizeof(struct ip_mreqn)) { | |
09cb105e | 633 | if (copy_from_user(&mreq, optval, sizeof(mreq))) |
1da177e4 | 634 | break; |
132adf54 SH |
635 | } else { |
636 | memset(&mreq, 0, sizeof(mreq)); | |
637 | if (optlen >= sizeof(struct in_addr) && | |
4d52cfbe ED |
638 | copy_from_user(&mreq.imr_address, optval, |
639 | sizeof(struct in_addr))) | |
132adf54 SH |
640 | break; |
641 | } | |
642 | ||
643 | if (!mreq.imr_ifindex) { | |
e6f1cebf | 644 | if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) { |
132adf54 SH |
645 | inet->mc_index = 0; |
646 | inet->mc_addr = 0; | |
647 | err = 0; | |
1da177e4 LT |
648 | break; |
649 | } | |
3b1e0a65 | 650 | dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr); |
55b80503 | 651 | if (dev) |
132adf54 | 652 | mreq.imr_ifindex = dev->ifindex; |
132adf54 | 653 | } else |
55b80503 | 654 | dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex); |
1da177e4 | 655 | |
1da177e4 | 656 | |
132adf54 SH |
657 | err = -EADDRNOTAVAIL; |
658 | if (!dev) | |
659 | break; | |
55b80503 | 660 | dev_put(dev); |
132adf54 SH |
661 | |
662 | err = -EINVAL; | |
663 | if (sk->sk_bound_dev_if && | |
664 | mreq.imr_ifindex != sk->sk_bound_dev_if) | |
665 | break; | |
1da177e4 | 666 | |
132adf54 SH |
667 | inet->mc_index = mreq.imr_ifindex; |
668 | inet->mc_addr = mreq.imr_address.s_addr; | |
669 | err = 0; | |
670 | break; | |
671 | } | |
1da177e4 | 672 | |
132adf54 SH |
673 | case IP_ADD_MEMBERSHIP: |
674 | case IP_DROP_MEMBERSHIP: | |
675 | { | |
676 | struct ip_mreqn mreq; | |
1da177e4 | 677 | |
a96fb49b FL |
678 | err = -EPROTO; |
679 | if (inet_sk(sk)->is_icsk) | |
680 | break; | |
681 | ||
132adf54 SH |
682 | if (optlen < sizeof(struct ip_mreq)) |
683 | goto e_inval; | |
684 | err = -EFAULT; | |
685 | if (optlen >= sizeof(struct ip_mreqn)) { | |
09cb105e | 686 | if (copy_from_user(&mreq, optval, sizeof(mreq))) |
1da177e4 | 687 | break; |
132adf54 SH |
688 | } else { |
689 | memset(&mreq, 0, sizeof(mreq)); | |
09cb105e | 690 | if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq))) |
1da177e4 | 691 | break; |
132adf54 | 692 | } |
1da177e4 | 693 | |
132adf54 SH |
694 | if (optname == IP_ADD_MEMBERSHIP) |
695 | err = ip_mc_join_group(sk, &mreq); | |
696 | else | |
697 | err = ip_mc_leave_group(sk, &mreq); | |
698 | break; | |
699 | } | |
700 | case IP_MSFILTER: | |
701 | { | |
132adf54 SH |
702 | struct ip_msfilter *msf; |
703 | ||
704 | if (optlen < IP_MSFILTER_SIZE(0)) | |
705 | goto e_inval; | |
706 | if (optlen > sysctl_optmem_max) { | |
707 | err = -ENOBUFS; | |
1da177e4 LT |
708 | break; |
709 | } | |
132adf54 | 710 | msf = kmalloc(optlen, GFP_KERNEL); |
cfcabdcc | 711 | if (!msf) { |
132adf54 SH |
712 | err = -ENOBUFS; |
713 | break; | |
714 | } | |
715 | err = -EFAULT; | |
716 | if (copy_from_user(msf, optval, optlen)) { | |
717 | kfree(msf); | |
718 | break; | |
719 | } | |
720 | /* numsrc >= (1G-4) overflow in 32 bits */ | |
721 | if (msf->imsf_numsrc >= 0x3ffffffcU || | |
722 | msf->imsf_numsrc > sysctl_igmp_max_msf) { | |
723 | kfree(msf); | |
724 | err = -ENOBUFS; | |
725 | break; | |
726 | } | |
727 | if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) { | |
728 | kfree(msf); | |
729 | err = -EINVAL; | |
730 | break; | |
731 | } | |
732 | err = ip_mc_msfilter(sk, msf, 0); | |
733 | kfree(msf); | |
734 | break; | |
735 | } | |
736 | case IP_BLOCK_SOURCE: | |
737 | case IP_UNBLOCK_SOURCE: | |
738 | case IP_ADD_SOURCE_MEMBERSHIP: | |
739 | case IP_DROP_SOURCE_MEMBERSHIP: | |
740 | { | |
741 | struct ip_mreq_source mreqs; | |
742 | int omode, add; | |
1da177e4 | 743 | |
132adf54 SH |
744 | if (optlen != sizeof(struct ip_mreq_source)) |
745 | goto e_inval; | |
746 | if (copy_from_user(&mreqs, optval, sizeof(mreqs))) { | |
1da177e4 | 747 | err = -EFAULT; |
1da177e4 LT |
748 | break; |
749 | } | |
132adf54 SH |
750 | if (optname == IP_BLOCK_SOURCE) { |
751 | omode = MCAST_EXCLUDE; | |
752 | add = 1; | |
753 | } else if (optname == IP_UNBLOCK_SOURCE) { | |
754 | omode = MCAST_EXCLUDE; | |
755 | add = 0; | |
756 | } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) { | |
757 | struct ip_mreqn mreq; | |
1da177e4 | 758 | |
132adf54 SH |
759 | mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr; |
760 | mreq.imr_address.s_addr = mreqs.imr_interface; | |
761 | mreq.imr_ifindex = 0; | |
762 | err = ip_mc_join_group(sk, &mreq); | |
763 | if (err && err != -EADDRINUSE) | |
1da177e4 | 764 | break; |
132adf54 SH |
765 | omode = MCAST_INCLUDE; |
766 | add = 1; | |
767 | } else /* IP_DROP_SOURCE_MEMBERSHIP */ { | |
768 | omode = MCAST_INCLUDE; | |
769 | add = 0; | |
770 | } | |
771 | err = ip_mc_source(add, omode, sk, &mreqs, 0); | |
772 | break; | |
773 | } | |
774 | case MCAST_JOIN_GROUP: | |
775 | case MCAST_LEAVE_GROUP: | |
776 | { | |
777 | struct group_req greq; | |
778 | struct sockaddr_in *psin; | |
779 | struct ip_mreqn mreq; | |
780 | ||
781 | if (optlen < sizeof(struct group_req)) | |
782 | goto e_inval; | |
783 | err = -EFAULT; | |
784 | if (copy_from_user(&greq, optval, sizeof(greq))) | |
785 | break; | |
786 | psin = (struct sockaddr_in *)&greq.gr_group; | |
787 | if (psin->sin_family != AF_INET) | |
788 | goto e_inval; | |
789 | memset(&mreq, 0, sizeof(mreq)); | |
790 | mreq.imr_multiaddr = psin->sin_addr; | |
791 | mreq.imr_ifindex = greq.gr_interface; | |
792 | ||
793 | if (optname == MCAST_JOIN_GROUP) | |
794 | err = ip_mc_join_group(sk, &mreq); | |
795 | else | |
796 | err = ip_mc_leave_group(sk, &mreq); | |
797 | break; | |
798 | } | |
799 | case MCAST_JOIN_SOURCE_GROUP: | |
800 | case MCAST_LEAVE_SOURCE_GROUP: | |
801 | case MCAST_BLOCK_SOURCE: | |
802 | case MCAST_UNBLOCK_SOURCE: | |
803 | { | |
804 | struct group_source_req greqs; | |
805 | struct ip_mreq_source mreqs; | |
806 | struct sockaddr_in *psin; | |
807 | int omode, add; | |
808 | ||
809 | if (optlen != sizeof(struct group_source_req)) | |
810 | goto e_inval; | |
811 | if (copy_from_user(&greqs, optval, sizeof(greqs))) { | |
1da177e4 | 812 | err = -EFAULT; |
1da177e4 LT |
813 | break; |
814 | } | |
132adf54 SH |
815 | if (greqs.gsr_group.ss_family != AF_INET || |
816 | greqs.gsr_source.ss_family != AF_INET) { | |
817 | err = -EADDRNOTAVAIL; | |
1da177e4 LT |
818 | break; |
819 | } | |
132adf54 SH |
820 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
821 | mreqs.imr_multiaddr = psin->sin_addr.s_addr; | |
822 | psin = (struct sockaddr_in *)&greqs.gsr_source; | |
823 | mreqs.imr_sourceaddr = psin->sin_addr.s_addr; | |
824 | mreqs.imr_interface = 0; /* use index for mc_source */ | |
825 | ||
826 | if (optname == MCAST_BLOCK_SOURCE) { | |
827 | omode = MCAST_EXCLUDE; | |
828 | add = 1; | |
829 | } else if (optname == MCAST_UNBLOCK_SOURCE) { | |
830 | omode = MCAST_EXCLUDE; | |
831 | add = 0; | |
832 | } else if (optname == MCAST_JOIN_SOURCE_GROUP) { | |
1da177e4 LT |
833 | struct ip_mreqn mreq; |
834 | ||
132adf54 | 835 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
1da177e4 | 836 | mreq.imr_multiaddr = psin->sin_addr; |
132adf54 SH |
837 | mreq.imr_address.s_addr = 0; |
838 | mreq.imr_ifindex = greqs.gsr_interface; | |
839 | err = ip_mc_join_group(sk, &mreq); | |
840 | if (err && err != -EADDRINUSE) | |
841 | break; | |
842 | greqs.gsr_interface = mreq.imr_ifindex; | |
843 | omode = MCAST_INCLUDE; | |
844 | add = 1; | |
845 | } else /* MCAST_LEAVE_SOURCE_GROUP */ { | |
846 | omode = MCAST_INCLUDE; | |
847 | add = 0; | |
848 | } | |
849 | err = ip_mc_source(add, omode, sk, &mreqs, | |
850 | greqs.gsr_interface); | |
851 | break; | |
852 | } | |
853 | case MCAST_MSFILTER: | |
854 | { | |
132adf54 SH |
855 | struct sockaddr_in *psin; |
856 | struct ip_msfilter *msf = NULL; | |
857 | struct group_filter *gsf = NULL; | |
858 | int msize, i, ifindex; | |
859 | ||
860 | if (optlen < GROUP_FILTER_SIZE(0)) | |
861 | goto e_inval; | |
862 | if (optlen > sysctl_optmem_max) { | |
863 | err = -ENOBUFS; | |
1da177e4 LT |
864 | break; |
865 | } | |
09cb105e | 866 | gsf = kmalloc(optlen, GFP_KERNEL); |
cfcabdcc | 867 | if (!gsf) { |
132adf54 | 868 | err = -ENOBUFS; |
1da177e4 LT |
869 | break; |
870 | } | |
132adf54 | 871 | err = -EFAULT; |
4d52cfbe | 872 | if (copy_from_user(gsf, optval, optlen)) |
132adf54 | 873 | goto mc_msf_out; |
4d52cfbe | 874 | |
132adf54 SH |
875 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
876 | if (gsf->gf_numsrc >= 0x1ffffff || | |
877 | gsf->gf_numsrc > sysctl_igmp_max_msf) { | |
878 | err = -ENOBUFS; | |
879 | goto mc_msf_out; | |
880 | } | |
881 | if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) { | |
882 | err = -EINVAL; | |
883 | goto mc_msf_out; | |
884 | } | |
885 | msize = IP_MSFILTER_SIZE(gsf->gf_numsrc); | |
09cb105e | 886 | msf = kmalloc(msize, GFP_KERNEL); |
cfcabdcc | 887 | if (!msf) { |
132adf54 SH |
888 | err = -ENOBUFS; |
889 | goto mc_msf_out; | |
890 | } | |
891 | ifindex = gsf->gf_interface; | |
892 | psin = (struct sockaddr_in *)&gsf->gf_group; | |
893 | if (psin->sin_family != AF_INET) { | |
1da177e4 | 894 | err = -EADDRNOTAVAIL; |
132adf54 | 895 | goto mc_msf_out; |
1da177e4 | 896 | } |
132adf54 SH |
897 | msf->imsf_multiaddr = psin->sin_addr.s_addr; |
898 | msf->imsf_interface = 0; | |
899 | msf->imsf_fmode = gsf->gf_fmode; | |
900 | msf->imsf_numsrc = gsf->gf_numsrc; | |
901 | err = -EADDRNOTAVAIL; | |
4d52cfbe | 902 | for (i = 0; i < gsf->gf_numsrc; ++i) { |
132adf54 | 903 | psin = (struct sockaddr_in *)&gsf->gf_slist[i]; |
e905a9ed | 904 | |
132adf54 SH |
905 | if (psin->sin_family != AF_INET) |
906 | goto mc_msf_out; | |
907 | msf->imsf_slist[i] = psin->sin_addr.s_addr; | |
908 | } | |
909 | kfree(gsf); | |
910 | gsf = NULL; | |
911 | ||
912 | err = ip_mc_msfilter(sk, msf, ifindex); | |
4d52cfbe | 913 | mc_msf_out: |
132adf54 SH |
914 | kfree(msf); |
915 | kfree(gsf); | |
916 | break; | |
917 | } | |
f771bef9 NS |
918 | case IP_MULTICAST_ALL: |
919 | if (optlen < 1) | |
920 | goto e_inval; | |
921 | if (val != 0 && val != 1) | |
922 | goto e_inval; | |
923 | inet->mc_all = val; | |
924 | break; | |
132adf54 SH |
925 | case IP_ROUTER_ALERT: |
926 | err = ip_ra_control(sk, val ? 1 : 0, NULL); | |
927 | break; | |
928 | ||
929 | case IP_FREEBIND: | |
4d52cfbe | 930 | if (optlen < 1) |
132adf54 SH |
931 | goto e_inval; |
932 | inet->freebind = !!val; | |
933 | break; | |
934 | ||
935 | case IP_IPSEC_POLICY: | |
936 | case IP_XFRM_POLICY: | |
937 | err = -EPERM; | |
938 | if (!capable(CAP_NET_ADMIN)) | |
1da177e4 | 939 | break; |
132adf54 SH |
940 | err = xfrm_user_policy(sk, optname, optval, optlen); |
941 | break; | |
1da177e4 | 942 | |
f5715aea KK |
943 | case IP_TRANSPARENT: |
944 | if (!capable(CAP_NET_ADMIN)) { | |
945 | err = -EPERM; | |
946 | break; | |
947 | } | |
948 | if (optlen < 1) | |
949 | goto e_inval; | |
950 | inet->transparent = !!val; | |
951 | break; | |
952 | ||
d218d111 SH |
953 | case IP_MINTTL: |
954 | if (optlen < 1) | |
955 | goto e_inval; | |
956 | if (val < 0 || val > 255) | |
957 | goto e_inval; | |
958 | inet->min_ttl = val; | |
959 | break; | |
960 | ||
132adf54 SH |
961 | default: |
962 | err = -ENOPROTOOPT; | |
963 | break; | |
1da177e4 LT |
964 | } |
965 | release_sock(sk); | |
966 | return err; | |
967 | ||
968 | e_inval: | |
969 | release_sock(sk); | |
970 | return -EINVAL; | |
971 | } | |
972 | ||
f84af32c ED |
973 | /** |
974 | * ip_queue_rcv_skb - Queue an skb into sock receive queue | |
975 | * @sk: socket | |
976 | * @skb: buffer | |
977 | * | |
978 | * Queues an skb into socket receive queue. If IP_CMSG_PKTINFO option | |
979 | * is not set, we drop skb dst entry now, while dst cache line is hot. | |
980 | */ | |
981 | int ip_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) | |
982 | { | |
983 | if (!(inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO)) | |
984 | skb_dst_drop(skb); | |
985 | return sock_queue_rcv_skb(sk, skb); | |
986 | } | |
987 | EXPORT_SYMBOL(ip_queue_rcv_skb); | |
988 | ||
3fdadf7d | 989 | int ip_setsockopt(struct sock *sk, int level, |
b7058842 | 990 | int optname, char __user *optval, unsigned int optlen) |
3fdadf7d DM |
991 | { |
992 | int err; | |
993 | ||
994 | if (level != SOL_IP) | |
995 | return -ENOPROTOOPT; | |
996 | ||
997 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); | |
998 | #ifdef CONFIG_NETFILTER | |
999 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
1000 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && | |
6a9fb947 PE |
1001 | optname != IP_IPSEC_POLICY && |
1002 | optname != IP_XFRM_POLICY && | |
1003 | !ip_mroute_opt(optname)) { | |
3fdadf7d DM |
1004 | lock_sock(sk); |
1005 | err = nf_setsockopt(sk, PF_INET, optname, optval, optlen); | |
1006 | release_sock(sk); | |
1007 | } | |
1008 | #endif | |
1009 | return err; | |
1010 | } | |
4d52cfbe | 1011 | EXPORT_SYMBOL(ip_setsockopt); |
3fdadf7d DM |
1012 | |
1013 | #ifdef CONFIG_COMPAT | |
543d9cfe | 1014 | int compat_ip_setsockopt(struct sock *sk, int level, int optname, |
b7058842 | 1015 | char __user *optval, unsigned int optlen) |
3fdadf7d DM |
1016 | { |
1017 | int err; | |
1018 | ||
1019 | if (level != SOL_IP) | |
1020 | return -ENOPROTOOPT; | |
1021 | ||
dae50295 DS |
1022 | if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER) |
1023 | return compat_mc_setsockopt(sk, level, optname, optval, optlen, | |
1024 | ip_setsockopt); | |
1025 | ||
3fdadf7d DM |
1026 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
1027 | #ifdef CONFIG_NETFILTER | |
1028 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
1029 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && | |
6a9fb947 PE |
1030 | optname != IP_IPSEC_POLICY && |
1031 | optname != IP_XFRM_POLICY && | |
1032 | !ip_mroute_opt(optname)) { | |
3fdadf7d | 1033 | lock_sock(sk); |
543d9cfe ACM |
1034 | err = compat_nf_setsockopt(sk, PF_INET, optname, |
1035 | optval, optlen); | |
3fdadf7d DM |
1036 | release_sock(sk); |
1037 | } | |
1038 | #endif | |
1039 | return err; | |
1040 | } | |
543d9cfe | 1041 | EXPORT_SYMBOL(compat_ip_setsockopt); |
3fdadf7d DM |
1042 | #endif |
1043 | ||
1da177e4 | 1044 | /* |
4d52cfbe ED |
1045 | * Get the options. Note for future reference. The GET of IP options gets |
1046 | * the _received_ ones. The set sets the _sent_ ones. | |
1da177e4 LT |
1047 | */ |
1048 | ||
3fdadf7d | 1049 | static int do_ip_getsockopt(struct sock *sk, int level, int optname, |
132adf54 | 1050 | char __user *optval, int __user *optlen) |
1da177e4 LT |
1051 | { |
1052 | struct inet_sock *inet = inet_sk(sk); | |
1053 | int val; | |
1054 | int len; | |
e905a9ed | 1055 | |
132adf54 | 1056 | if (level != SOL_IP) |
1da177e4 LT |
1057 | return -EOPNOTSUPP; |
1058 | ||
6a9fb947 | 1059 | if (ip_mroute_opt(optname)) |
09cb105e | 1060 | return ip_mroute_getsockopt(sk, optname, optval, optlen); |
1da177e4 | 1061 | |
09cb105e | 1062 | if (get_user(len, optlen)) |
1da177e4 | 1063 | return -EFAULT; |
132adf54 | 1064 | if (len < 0) |
1da177e4 | 1065 | return -EINVAL; |
e905a9ed | 1066 | |
1da177e4 LT |
1067 | lock_sock(sk); |
1068 | ||
132adf54 SH |
1069 | switch (optname) { |
1070 | case IP_OPTIONS: | |
1071 | { | |
1072 | unsigned char optbuf[sizeof(struct ip_options)+40]; | |
09cb105e | 1073 | struct ip_options * opt = (struct ip_options *)optbuf; |
132adf54 SH |
1074 | opt->optlen = 0; |
1075 | if (inet->opt) | |
1076 | memcpy(optbuf, inet->opt, | |
1077 | sizeof(struct ip_options)+ | |
1078 | inet->opt->optlen); | |
1079 | release_sock(sk); | |
1080 | ||
1081 | if (opt->optlen == 0) | |
1082 | return put_user(0, optlen); | |
1083 | ||
1084 | ip_options_undo(opt); | |
1085 | ||
1086 | len = min_t(unsigned int, len, opt->optlen); | |
1087 | if (put_user(len, optlen)) | |
1088 | return -EFAULT; | |
1089 | if (copy_to_user(optval, opt->__data, len)) | |
1090 | return -EFAULT; | |
1091 | return 0; | |
1092 | } | |
1093 | case IP_PKTINFO: | |
1094 | val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0; | |
1095 | break; | |
1096 | case IP_RECVTTL: | |
1097 | val = (inet->cmsg_flags & IP_CMSG_TTL) != 0; | |
1098 | break; | |
1099 | case IP_RECVTOS: | |
1100 | val = (inet->cmsg_flags & IP_CMSG_TOS) != 0; | |
1101 | break; | |
1102 | case IP_RECVOPTS: | |
1103 | val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0; | |
1104 | break; | |
1105 | case IP_RETOPTS: | |
1106 | val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0; | |
1107 | break; | |
1108 | case IP_PASSSEC: | |
1109 | val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0; | |
1110 | break; | |
e8b2dfe9 BS |
1111 | case IP_RECVORIGDSTADDR: |
1112 | val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0; | |
1113 | break; | |
132adf54 SH |
1114 | case IP_TOS: |
1115 | val = inet->tos; | |
1116 | break; | |
1117 | case IP_TTL: | |
1118 | val = (inet->uc_ttl == -1 ? | |
1119 | sysctl_ip_default_ttl : | |
1120 | inet->uc_ttl); | |
1121 | break; | |
1122 | case IP_HDRINCL: | |
1123 | val = inet->hdrincl; | |
1124 | break; | |
1125 | case IP_MTU_DISCOVER: | |
1126 | val = inet->pmtudisc; | |
1127 | break; | |
1128 | case IP_MTU: | |
1129 | { | |
1130 | struct dst_entry *dst; | |
1131 | val = 0; | |
1132 | dst = sk_dst_get(sk); | |
1133 | if (dst) { | |
1134 | val = dst_mtu(dst); | |
1135 | dst_release(dst); | |
1da177e4 | 1136 | } |
132adf54 | 1137 | if (!val) { |
1da177e4 | 1138 | release_sock(sk); |
132adf54 | 1139 | return -ENOTCONN; |
1da177e4 | 1140 | } |
132adf54 SH |
1141 | break; |
1142 | } | |
1143 | case IP_RECVERR: | |
1144 | val = inet->recverr; | |
1145 | break; | |
1146 | case IP_MULTICAST_TTL: | |
1147 | val = inet->mc_ttl; | |
1148 | break; | |
1149 | case IP_MULTICAST_LOOP: | |
1150 | val = inet->mc_loop; | |
1151 | break; | |
1152 | case IP_MULTICAST_IF: | |
1153 | { | |
1154 | struct in_addr addr; | |
1155 | len = min_t(unsigned int, len, sizeof(struct in_addr)); | |
1156 | addr.s_addr = inet->mc_addr; | |
1157 | release_sock(sk); | |
1da177e4 | 1158 | |
132adf54 SH |
1159 | if (put_user(len, optlen)) |
1160 | return -EFAULT; | |
1161 | if (copy_to_user(optval, &addr, len)) | |
1162 | return -EFAULT; | |
1163 | return 0; | |
1164 | } | |
1165 | case IP_MSFILTER: | |
1166 | { | |
1167 | struct ip_msfilter msf; | |
1168 | int err; | |
1169 | ||
1170 | if (len < IP_MSFILTER_SIZE(0)) { | |
1da177e4 | 1171 | release_sock(sk); |
132adf54 | 1172 | return -EINVAL; |
1da177e4 | 1173 | } |
132adf54 | 1174 | if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) { |
1da177e4 | 1175 | release_sock(sk); |
132adf54 | 1176 | return -EFAULT; |
1da177e4 | 1177 | } |
132adf54 SH |
1178 | err = ip_mc_msfget(sk, &msf, |
1179 | (struct ip_msfilter __user *)optval, optlen); | |
1180 | release_sock(sk); | |
1181 | return err; | |
1182 | } | |
1183 | case MCAST_MSFILTER: | |
1184 | { | |
1185 | struct group_filter gsf; | |
1186 | int err; | |
1da177e4 | 1187 | |
132adf54 | 1188 | if (len < GROUP_FILTER_SIZE(0)) { |
1da177e4 | 1189 | release_sock(sk); |
132adf54 SH |
1190 | return -EINVAL; |
1191 | } | |
1192 | if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) { | |
1193 | release_sock(sk); | |
1194 | return -EFAULT; | |
1195 | } | |
1196 | err = ip_mc_gsfget(sk, &gsf, | |
4d52cfbe ED |
1197 | (struct group_filter __user *)optval, |
1198 | optlen); | |
132adf54 SH |
1199 | release_sock(sk); |
1200 | return err; | |
1201 | } | |
f771bef9 NS |
1202 | case IP_MULTICAST_ALL: |
1203 | val = inet->mc_all; | |
1204 | break; | |
132adf54 SH |
1205 | case IP_PKTOPTIONS: |
1206 | { | |
1207 | struct msghdr msg; | |
1da177e4 | 1208 | |
132adf54 | 1209 | release_sock(sk); |
1da177e4 | 1210 | |
132adf54 SH |
1211 | if (sk->sk_type != SOCK_STREAM) |
1212 | return -ENOPROTOOPT; | |
1da177e4 | 1213 | |
132adf54 SH |
1214 | msg.msg_control = optval; |
1215 | msg.msg_controllen = len; | |
1216 | msg.msg_flags = 0; | |
1da177e4 | 1217 | |
132adf54 SH |
1218 | if (inet->cmsg_flags & IP_CMSG_PKTINFO) { |
1219 | struct in_pktinfo info; | |
1220 | ||
c720c7e8 ED |
1221 | info.ipi_addr.s_addr = inet->inet_rcv_saddr; |
1222 | info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr; | |
132adf54 SH |
1223 | info.ipi_ifindex = inet->mc_index; |
1224 | put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); | |
1da177e4 | 1225 | } |
132adf54 SH |
1226 | if (inet->cmsg_flags & IP_CMSG_TTL) { |
1227 | int hlim = inet->mc_ttl; | |
1228 | put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim); | |
1229 | } | |
1230 | len -= msg.msg_controllen; | |
1231 | return put_user(len, optlen); | |
1232 | } | |
1233 | case IP_FREEBIND: | |
1234 | val = inet->freebind; | |
1235 | break; | |
f5715aea KK |
1236 | case IP_TRANSPARENT: |
1237 | val = inet->transparent; | |
1238 | break; | |
d218d111 SH |
1239 | case IP_MINTTL: |
1240 | val = inet->min_ttl; | |
1241 | break; | |
132adf54 SH |
1242 | default: |
1243 | release_sock(sk); | |
1244 | return -ENOPROTOOPT; | |
1da177e4 LT |
1245 | } |
1246 | release_sock(sk); | |
e905a9ed | 1247 | |
4d52cfbe | 1248 | if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) { |
1da177e4 LT |
1249 | unsigned char ucval = (unsigned char)val; |
1250 | len = 1; | |
132adf54 | 1251 | if (put_user(len, optlen)) |
1da177e4 | 1252 | return -EFAULT; |
09cb105e | 1253 | if (copy_to_user(optval, &ucval, 1)) |
1da177e4 LT |
1254 | return -EFAULT; |
1255 | } else { | |
1256 | len = min_t(unsigned int, sizeof(int), len); | |
132adf54 | 1257 | if (put_user(len, optlen)) |
1da177e4 | 1258 | return -EFAULT; |
09cb105e | 1259 | if (copy_to_user(optval, &val, len)) |
1da177e4 LT |
1260 | return -EFAULT; |
1261 | } | |
1262 | return 0; | |
1263 | } | |
1264 | ||
3fdadf7d | 1265 | int ip_getsockopt(struct sock *sk, int level, |
132adf54 | 1266 | int optname, char __user *optval, int __user *optlen) |
3fdadf7d DM |
1267 | { |
1268 | int err; | |
1269 | ||
1270 | err = do_ip_getsockopt(sk, level, optname, optval, optlen); | |
1271 | #ifdef CONFIG_NETFILTER | |
1272 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
6a9fb947 PE |
1273 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
1274 | !ip_mroute_opt(optname)) { | |
e905a9ed | 1275 | int len; |
3fdadf7d | 1276 | |
09cb105e | 1277 | if (get_user(len, optlen)) |
3fdadf7d DM |
1278 | return -EFAULT; |
1279 | ||
1280 | lock_sock(sk); | |
1281 | err = nf_getsockopt(sk, PF_INET, optname, optval, | |
1282 | &len); | |
1283 | release_sock(sk); | |
1284 | if (err >= 0) | |
1285 | err = put_user(len, optlen); | |
1286 | return err; | |
1287 | } | |
1288 | #endif | |
1289 | return err; | |
1290 | } | |
4d52cfbe | 1291 | EXPORT_SYMBOL(ip_getsockopt); |
3fdadf7d DM |
1292 | |
1293 | #ifdef CONFIG_COMPAT | |
543d9cfe ACM |
1294 | int compat_ip_getsockopt(struct sock *sk, int level, int optname, |
1295 | char __user *optval, int __user *optlen) | |
3fdadf7d | 1296 | { |
42908c69 DS |
1297 | int err; |
1298 | ||
1299 | if (optname == MCAST_MSFILTER) | |
1300 | return compat_mc_getsockopt(sk, level, optname, optval, optlen, | |
1301 | ip_getsockopt); | |
1302 | ||
1303 | err = do_ip_getsockopt(sk, level, optname, optval, optlen); | |
1304 | ||
3fdadf7d DM |
1305 | #ifdef CONFIG_NETFILTER |
1306 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
6a9fb947 PE |
1307 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
1308 | !ip_mroute_opt(optname)) { | |
e905a9ed | 1309 | int len; |
3fdadf7d | 1310 | |
543d9cfe | 1311 | if (get_user(len, optlen)) |
3fdadf7d DM |
1312 | return -EFAULT; |
1313 | ||
1314 | lock_sock(sk); | |
543d9cfe | 1315 | err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len); |
3fdadf7d DM |
1316 | release_sock(sk); |
1317 | if (err >= 0) | |
1318 | err = put_user(len, optlen); | |
1319 | return err; | |
1320 | } | |
1321 | #endif | |
1322 | return err; | |
1323 | } | |
543d9cfe | 1324 | EXPORT_SYMBOL(compat_ip_getsockopt); |
3fdadf7d | 1325 | #endif |